2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2020 Matt Schatz <genius3000@g3k.solutions>
5 * Copyright (C) 2013-2015 Attila Molnar <attilamolnar@hush.com>
6 * Copyright (C) 2013, 2015, 2018-2019 Sadie Powell <sadie@witchery.services>
7 * Copyright (C) 2012-2013 ChrisTX <xpipe@hotmail.de>
8 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
9 * Copyright (C) 2011, 2014, 2019 Adam <Adam@anope.org>
10 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11 * Copyright (C) 2007-2009 Robin Burchell <robin+git@viroteck.net>
12 * Copyright (C) 2007-2008 Craig Edwards <brain@inspircd.org>
13 * Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
14 * Copyright (C) 2007 burlex <burlex@e03df62e-2008-0410-955e-edbf42e46eb7>
16 * This file is part of InspIRCd. InspIRCd is free software: you can
17 * redistribute it and/or modify it under the terms of the GNU General Public
18 * License as published by the Free Software Foundation, version 2.
20 * This program is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 Wrapper Functions/Definitions
36 * Starting with PSAPI version 2 for Windows 7 and Windows Server 2008 R2, this function is defined as K32GetProcessMemoryInfo in Psapi.h and exported
37 * in Kernel32.lib and Kernel32.dll. However, you should always call this function as GetProcessMemoryInfo. To ensure correct resolution of symbols
38 * for programs that will run on earlier versions of Windows, add Psapi.lib to the TARGETLIBS macro and compile the program with PSAPI_VERSION=1.
40 * We do this before anything to make sure it's done.
42 #define PSAPI_VERSION 1
44 #include "win32service.h"
46 /* This defaults to 64, way too small for an ircd! */
48 #define FD_SETSIZE 24000
50 /* Make builds smaller, leaner and faster */
52 #define WIN32_LEAN_AND_MEAN
54 /* Macros for exporting symbols - dependent on what is being compiled */
57 #define CoreExport __declspec(dllimport)
58 #define DllExport __declspec(dllexport)
60 #define CoreExport __declspec(dllexport)
61 #define DllExport __declspec(dllimport)
64 /* Redirect main() through a different method in win32service.cpp, to intercept service startup */
65 #define ENTRYPOINT CoreExport int smain(int argc, char** argv)
67 /* Disable the deprecation warnings.. it spams :P */
68 #define _CRT_SECURE_NO_DEPRECATE
69 #define _WINSOCK_DEPRECATED_NO_WARNINGS
71 // Windows doesn't support getopt_long so we use ya_getopt instead.
72 #include "ya_getopt.h"
74 /* Normal windows (platform-specific) includes */
76 #pragma comment(lib, "Ws2_32.lib")
79 #include <sys/types.h>
85 #define F_OK 0 /* test for existence of file */
86 #define X_OK (1<<0) /* test for execute or search permission */
87 #define W_OK (1<<1) /* test for write permission */
88 #define R_OK (1<<2) /* test for read permission */
90 // Windows defines these already.
95 /* strcasecmp is not defined on windows by default */
96 #define strcasecmp _stricmp
97 #define strncasecmp _strnicmp
99 typedef SSIZE_T ssize_t;
101 /* _popen, _pclose */
103 #define pclose _pclose
104 #define getpid _getpid
106 #define fdopen _fdopen
108 // warning: 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
109 // Normally, this is a huge problem, but due to our new/delete remap, we can ignore it.
110 #pragma warning(disable:4251)
112 // warning: DLL-interface classkey 'identifier' used as base for DLL-interface classkey 'identifier'
113 #pragma warning(disable:4275)
115 // warning: unreferenced formal parameter
116 // Unimportant for now, but for the next version, we should take a look at these again.
117 #pragma warning(disable:4100)
119 // warning: 'class' : assignment operator could not be generated
120 #pragma warning(disable:4512)
122 // warning C4127: conditional expression is constant
123 // This will be triggered like crazy because FOREACH_MOD and similar macros are wrapped in do { ... } while(0) constructs
124 #pragma warning(disable:4127)
126 // warning C4996: The POSIX name for this item is deprecated.
127 #pragma warning(disable:4996)
129 // warning C4244: conversion from 'x' to 'y', possible loss of data
130 #pragma warning(disable:4244)
132 // warning C4267: 'var' : conversion from 'size_t' to 'type', possible loss of data
133 #pragma warning(disable:4267)
135 // warning C4706: assignment within conditional expression
136 #pragma warning(disable:4706)
138 // warning C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning)
139 #pragma warning(disable:4800)
141 /* Shared memory allocation functions */
142 void * ::operator new(size_t iSize);
143 void ::operator delete(void * ptr);
147 class CWin32Exception : public std::exception
151 CWin32Exception(const CWin32Exception& other);
152 virtual const char* what() const throw();
153 DWORD GetErrorCode();
156 char szErrorString[500];
160 // Same value as EXIT_STATUS_FORK (EXIT_STATUS_FORK is unused on Windows)
161 #define EXIT_STATUS_SERVICE 4
166 void* iov_base; // Starting address
167 size_t iov_len; // Number of bytes to transfer
170 // Windows WSABUF with POSIX field names
173 // POSIX iovec has iov_base then iov_len, WSABUF in Windows has the fields in reverse order
174 u_long iov_len; // Number of bytes to transfer
175 char FAR* iov_base; // Starting address
178 inline ssize_t writev(int fd, const WindowsIOVec* iov, int count)
181 int ret = WSASend(fd, reinterpret_cast<LPWSABUF>(const_cast<WindowsIOVec*>(iov)), count, &sent, 0, NULL, NULL);
187 // This wrapper is just so we don't need to do #ifdef _WIN32 everywhere in the socket code. It is
188 // not actually used and does not need to be the same size as sockaddr_un on UNIX systems.
191 ADDRESS_FAMILY sun_family;