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