]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_win32wrapper.h
Remove DISABLE_WRITEV and StreamSocket code for platforms lacking writev() support
[user/henk/code/inspircd.git] / win / inspircd_win32wrapper.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
5  *   Copyright (C) 2007 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #pragma once
23
24 /* Windows Port
25    Wrapper Functions/Definitions
26    By Burlex */
27 /*
28  * Starting with PSAPI version 2 for Windows 7 and Windows Server 2008 R2, this function is defined as K32GetProcessMemoryInfo in Psapi.h and exported
29  * in Kernel32.lib and Kernel32.dll. However, you should always call this function as GetProcessMemoryInfo. To ensure correct resolution of symbols
30  * 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.
31  *
32  * We do this before anything to make sure it's done.
33  */
34 #define PSAPI_VERSION 1
35
36 #include "win32service.h"
37
38 /* This defaults to 64, way too small for an ircd! */
39
40 #define FD_SETSIZE 24000
41
42 /* Make builds smaller, leaner and faster */
43 #define VC_EXTRALEAN
44 #define WIN32_LEAN_AND_MEAN
45
46 /* Macros for exporting symbols - dependant on what is being compiled */
47
48 #ifdef DLL_BUILD
49 #define CoreExport __declspec(dllimport)
50 #define DllExport __declspec(dllexport)
51 #else
52 #define CoreExport __declspec(dllexport)
53 #define DllExport __declspec(dllimport)
54 #endif
55
56 /* Redirect main() through a different method in win32service.cpp, to intercept service startup */
57 #define ENTRYPOINT CoreExport int smain(int argc, char** argv)
58
59 /* Disable the deprecation warnings.. it spams :P */
60 #define _CRT_SECURE_NO_DEPRECATE
61
62 /* Normal windows (platform-specific) includes */
63 #include <winsock2.h>
64 #pragma comment(lib, "Ws2_32.lib")
65 #include <windows.h>
66 #include <ws2tcpip.h>
67 #include <sys/types.h>
68 #include <sys/stat.h>
69 #include <direct.h>
70 #include <process.h>
71 #include <io.h>
72
73 #define F_OK            0       /* test for existence of file */
74 #define X_OK            (1<<0)  /* test for execute or search permission */
75 #define W_OK            (1<<1)  /* test for write permission */
76 #define R_OK            (1<<2)  /* test for read permission */
77
78 // Windows defines these already.
79 #undef ERROR
80 #undef min
81 #undef max
82
83 /* strcasecmp is not defined on windows by default */
84 #define strcasecmp _stricmp
85 #define strncasecmp _strnicmp
86
87 typedef int ssize_t;
88
89 /* Convert formatted (xxx.xxx.xxx.xxx) string to in_addr struct */
90 CoreExport int insp_inet_pton(int af, const char * src, void * dst);
91
92 /* Convert struct to formatted (xxx.xxx.xxx.xxx) string */
93 CoreExport const char * insp_inet_ntop(int af, const void * src, char * dst, socklen_t cnt);
94
95 /* inet_pton/ntop require at least NT 6.0 */
96 #define inet_pton insp_inet_pton
97 #define inet_ntop insp_inet_ntop
98
99 /* Safe printf functions aren't defined in VC++ */
100 #define snprintf _snprintf
101 #define vsnprintf _vsnprintf
102
103 #ifndef va_copy
104 #define va_copy(dest, src) (dest = src)
105 #endif
106
107 /* Unix-style sleep (argument is in seconds) */
108 __inline void sleep(int seconds) { Sleep(seconds * 1000); }
109
110 /* _popen, _pclose */
111 #define popen _popen
112 #define pclose _pclose
113
114 /* _access */
115 #define access _access
116
117 /* IPV4 only convert string to address struct */
118 __inline int inet_aton(const char *cp, struct in_addr *addr)
119 {
120         addr->s_addr = inet_addr(cp);
121         return (addr->s_addr == INADDR_NONE) ? 0 : 1;
122 };
123
124 /* getopt() wrapper */
125 #define no_argument            0
126 #define required_argument      1
127 #define optional_argument      2
128 struct option
129 {
130         char *name;
131         int has_arg;
132         int *flag;
133         int val;
134 };
135 extern int optind;
136 extern char optarg[514];
137 int getopt_long(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind);
138
139 struct dirent
140 {
141         char d_name[MAX_PATH];
142 };
143
144 struct DIR
145 {
146         dirent dirent_pointer;
147         HANDLE find_handle;
148         WIN32_FIND_DATAA find_data;
149         bool first;
150 };
151
152 struct timespec
153 {
154         time_t tv_sec;
155         long tv_nsec;
156 };
157
158 CoreExport DIR * opendir(const char * path);
159 CoreExport dirent * readdir(DIR * handle);
160 CoreExport void closedir(DIR * handle);
161
162 // warning: 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
163 // Normally, this is a huge problem, but due to our new/delete remap, we can ignore it.
164 #pragma warning(disable:4251)
165
166 // warning: DLL-interface classkey 'identifier' used as base for DLL-interface classkey 'identifier'
167 #pragma warning(disable:4275)
168
169 // warning: unreferenced formal parameter
170 // Unimportant for now, but for the next version, we should take a look at these again.
171 #pragma warning(disable:4100)
172
173 // warning: 'class' : assignment operator could not be generated
174 #pragma warning(disable:4512)
175
176 // warning C4127: conditional expression is constant
177 // This will be triggered like crazy because FOREACH_MOD and similar macros are wrapped in do { ... } while(0) constructs
178 #pragma warning(disable:4127)
179
180 // warning C4996: The POSIX name for this item is deprecated.
181 #pragma warning(disable:4996)
182
183 // warning C4244: conversion from 'x' to 'y', possible loss of data
184 #pragma warning(disable:4244)
185
186 // warning C4267: 'var' : conversion from 'size_t' to 'type', possible loss of data
187 #pragma warning(disable:4267)
188
189 // warning C4706: assignment within conditional expression
190 #pragma warning(disable:4706)
191
192 // warning C4355: 'this' : used in base member initializer list
193 // This warning is disabled by default since VC2012
194 #if _MSC_VER < 1700
195 #pragma warning(disable:4355)
196 #endif
197
198 /* Shared memory allocation functions */
199 void * ::operator new(size_t iSize);
200 void ::operator delete(void * ptr);
201
202 #include <exception>
203
204 class CWin32Exception : public std::exception
205 {
206 public:
207         CWin32Exception();
208         CWin32Exception(const CWin32Exception& other);
209         virtual const char* what() const throw();
210         DWORD GetErrorCode();
211
212 private:
213         char szErrorString[500];
214         DWORD dwErrorCode;
215 };
216
217 // Same value as EXIT_STATUS_FORK (EXIT_STATUS_FORK is unused on Windows)
218 #define EXIT_STATUS_SERVICE 4
219
220 // POSIX iovec
221 struct iovec
222 {
223         void* iov_base; // Starting address
224         size_t iov_len; // Number of bytes to transfer
225 };
226
227 // Windows WSABUF with POSIX field names
228 struct WindowsIOVec
229 {
230         // POSIX iovec has iov_base then iov_len, WSABUF in Windows has the fields in reverse order
231         u_long iov_len; // Number of bytes to transfer
232         char FAR* iov_base; // Starting address
233 };
234
235 inline ssize_t writev(int fd, const WindowsIOVec* iov, int count)
236 {
237         DWORD sent;
238         int ret = WSASend(fd, reinterpret_cast<LPWSABUF>(const_cast<WindowsIOVec*>(iov)), count, &sent, 0, NULL, NULL);
239         if (ret == 0)
240                 return sent;
241         return -1;
242 }