InspIRCd  3.0
inspircd_win32wrapper.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2020 Matt Schatz <[email protected]>
5  * Copyright (C) 2013-2015 Attila Molnar <[email protected]>
6  * Copyright (C) 2013, 2015, 2018-2019, 2021 Sadie Powell <[email protected]>
7  * Copyright (C) 2012-2013 ChrisTX <[email protected]>
8  * Copyright (C) 2012 Robby <[email protected]belgie.be>
9  * Copyright (C) 2011, 2014, 2019 Adam <[email protected]>
10  * Copyright (C) 2009 Uli Schlachter <[email protected]>
11  * Copyright (C) 2007-2009 Robin Burchell <[email protected]>
12  * Copyright (C) 2007-2008 Craig Edwards <[email protected]>
13  * Copyright (C) 2007, 2009 Dennis Friis <[email protected]>
14  * Copyright (C) 2007 burlex <[email protected]>
15  *
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.
19  *
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
23  * details.
24  *
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/>.
27  */
28 
29 
30 #pragma once
31 
32 /* Windows Port
33  Wrapper Functions/Definitions
34  By Burlex */
35 /*
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.
39  *
40  * We do this before anything to make sure it's done.
41  */
42 #define PSAPI_VERSION 1
43 
44 #include "win32service.h"
45 
46 /* This defaults to 64, way too small for an ircd! */
47 
48 #define FD_SETSIZE 24000
49 
50 /* Make builds smaller, leaner and faster */
51 #define VC_EXTRALEAN
52 #define WIN32_LEAN_AND_MEAN
53 
54 /* Macros for exporting symbols - dependent on what is being compiled */
55 
56 #ifdef DLL_BUILD
57 #define CoreExport __declspec(dllimport)
58 #define DllExport __declspec(dllexport)
59 #else
60 #define CoreExport __declspec(dllexport)
61 #define DllExport __declspec(dllimport)
62 #endif
63 
64 // File numbers for standard streams.
65 #define STDIN_FILENO 0
66 #define STDOUT_FILENO 1
67 #define STDERR_FILENO 2
68 
69 /* Redirect main() through a different method in win32service.cpp, to intercept service startup */
70 #define ENTRYPOINT CoreExport int smain(int argc, char** argv)
71 
72 /* Disable the deprecation warnings.. it spams :P */
73 #define _CRT_SECURE_NO_DEPRECATE
74 #define _WINSOCK_DEPRECATED_NO_WARNINGS
75 
76 // Windows doesn't support getopt_long so we use ya_getopt instead.
77 #include "ya_getopt.h"
78 
79 /* Normal windows (platform-specific) includes */
80 #include <winsock2.h>
81 #pragma comment(lib, "Ws2_32.lib")
82 #include <windows.h>
83 #include <ws2tcpip.h>
84 #include <sys/types.h>
85 #include <sys/stat.h>
86 #include <direct.h>
87 #include <process.h>
88 #include <io.h>
89 
90 #define F_OK 0 /* test for existence of file */
91 #define X_OK (1<<0) /* test for execute or search permission */
92 #define W_OK (1<<1) /* test for write permission */
93 #define R_OK (1<<2) /* test for read permission */
94 
95 // Windows defines these already.
96 #undef ERROR
97 #undef min
98 #undef max
99 
100 /* strcasecmp is not defined on windows by default */
101 #define strcasecmp _stricmp
102 #define strncasecmp _strnicmp
103 
104 typedef SSIZE_T ssize_t;
105 
106 /* _popen, _pclose */
107 #define popen _popen
108 #define pclose _pclose
109 #define getpid _getpid
110 #define dup _dup
111 #define fdopen _fdopen
112 
113 // warning: 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
114 // Normally, this is a huge problem, but due to our new/delete remap, we can ignore it.
115 #pragma warning(disable:4251)
116 
117 // warning: DLL-interface classkey 'identifier' used as base for DLL-interface classkey 'identifier'
118 #pragma warning(disable:4275)
119 
120 // warning: unreferenced formal parameter
121 // Unimportant for now, but for the next version, we should take a look at these again.
122 #pragma warning(disable:4100)
123 
124 // warning: 'class' : assignment operator could not be generated
125 #pragma warning(disable:4512)
126 
127 // warning C4127: conditional expression is constant
128 // This will be triggered like crazy because FOREACH_MOD and similar macros are wrapped in do { ... } while(0) constructs
129 #pragma warning(disable:4127)
130 
131 // warning C4996: The POSIX name for this item is deprecated.
132 #pragma warning(disable:4996)
133 
134 // warning C4244: conversion from 'x' to 'y', possible loss of data
135 #pragma warning(disable:4244)
136 
137 // warning C4267: 'var' : conversion from 'size_t' to 'type', possible loss of data
138 #pragma warning(disable:4267)
139 
140 // warning C4706: assignment within conditional expression
141 #pragma warning(disable:4706)
142 
143 // warning C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning)
144 #pragma warning(disable:4800)
145 
146 /* Shared memory allocation functions */
147 void * ::operator new(size_t iSize);
148 void ::operator delete(void * ptr);
149 
150 #include <exception>
151 
152 class CWin32Exception : public std::exception
153 {
154 public:
155  CWin32Exception();
156  CWin32Exception(const CWin32Exception& other);
157  virtual const char* what() const throw();
158  DWORD GetErrorCode();
159 
160 private:
161  char szErrorString[500];
162  DWORD dwErrorCode;
163 };
164 
165 // Same value as EXIT_STATUS_FORK (EXIT_STATUS_FORK is unused on Windows)
166 #define EXIT_STATUS_SERVICE 4
167 
168 // POSIX iovec
169 struct iovec
170 {
171  void* iov_base; // Starting address
172  size_t iov_len; // Number of bytes to transfer
173 };
174 
175 // Windows WSABUF with POSIX field names
177 {
178  // POSIX iovec has iov_base then iov_len, WSABUF in Windows has the fields in reverse order
179  u_long iov_len; // Number of bytes to transfer
180  char FAR* iov_base; // Starting address
181 };
182 
183 inline ssize_t writev(int fd, const WindowsIOVec* iov, int count)
184 {
185  DWORD sent;
186  int ret = WSASend(fd, reinterpret_cast<LPWSABUF>(const_cast<WindowsIOVec*>(iov)), count, &sent, 0, NULL, NULL);
187  if (ret == 0)
188  return sent;
189  return -1;
190 }
191 
192 // This wrapper is just so we don't need to do #ifdef _WIN32 everywhere in the socket code. It is
193 // not actually used and does not need to be the same size as sockaddr_un on UNIX systems.
195 {
196  ADDRESS_FAMILY sun_family;
197  char sun_path[6];
198 };
199 
201 {
202  WORD BackgroundColor;
203  WORD ForegroundColor;
204  HANDLE Handle;
205  WindowsStream(DWORD handle);
206 };
Definition: inspircd_win32wrapper.h:153
Definition: inspircd_win32wrapper.h:177
Definition: inspircd_win32wrapper.h:201
Definition: inspircd_win32wrapper.h:170
Definition: inspircd_win32wrapper.h:195