]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_win32wrapper.h
901f0fdf0a3e2b8102517068dbe6b819d5782c71
[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 /* Windows Port
23    Wrapper Functions/Definitions
24    By Burlex */
25
26 #ifndef INSPIRCD_WIN32WRAPPER_H
27 #define INSPIRCD_WIN32WRAPPER_H
28
29 /*
30  * Starting with PSAPI version 2 for Windows 7 and Windows Server 2008 R2, this function is defined as K32GetProcessMemoryInfo in Psapi.h and exported
31  * in Kernel32.lib and Kernel32.dll. However, you should always call this function as GetProcessMemoryInfo. To ensure correct resolution of symbols
32  * for programs that will run on earlier versions ofWindows, add Psapi.lib to the TARGETLIBS macro and compile the program with PSAPI_VERSION=1.
33  * 
34  * We do this before anything to make sure it's done.
35  */
36 #define PSAPI_VERSION 1
37
38 #ifndef CONFIGURE_BUILD
39 #include "win32service.h"
40 #endif
41
42 /* Define the WINDOWS macro. This means we're building on windows to the rest of the server.
43    I think this is more reasonable than using WIN32, especially if we're gonna be doing 64-bit compiles */
44 #define WINDOWS 1
45 #define ENABLE_CRASHDUMPS 0
46
47 /* This defaults to 64, way too small for an ircd! */
48 /* CRT memory debugging */
49 #ifdef DEBUG
50 #define _CRTDBG_MAP_ALLOC
51 #include <stdlib.h>
52 #include <crtdbg.h>
53 #endif
54
55 #define FD_SETSIZE 24000
56
57 typedef unsigned __int16 uint16_t;
58 typedef unsigned __int32 uint32_t;
59
60 /* Make builds smaller, leaner and faster */
61 #define VC_EXTRALEAN
62 #define WIN32_LEAN_AND_MEAN
63
64 /* Not defined in windows */
65 #define SIGHUP 1
66
67 /* Not defined in windows, parameter to shutdown() */
68 #define SHUT_WR 2
69
70 /* They just have to be *different*, don't they. */
71 #define PATH_MAX MAX_PATH
72
73 /* Begone shitty 'safe STL' warnings */
74 #define _SCL_SECURE_NO_WARNINGS
75 #define _CRT_SECURE_NO_WARNINGS
76 #define _AFX_SECURE_NO_WARNINGS
77 #define _ATL_SECURE_NO_WARNINGS
78
79 /* Macros for exporting symbols - dependant on what is being compiled */
80
81 #ifdef DLL_BUILD
82 #define CoreExport __declspec(dllimport)
83 #define DllExport __declspec(dllexport)
84 #else
85 #define CoreExport __declspec(dllexport)
86 #define DllExport __declspec(dllimport)
87 #endif
88
89 /* Redirect main() through a different method in win32service.cpp, to intercept service startup */
90 #define ENTRYPOINT CoreExport int smain(int argc, char** argv)
91
92 /* Disable the deprecation warnings.. it spams :P */
93 #define _CRT_SECURE_NO_DEPRECATE
94 #define _SCL_SECURE_NO_DEPRECATE
95
96 #include <string>
97
98 /* Say we're building on windows 2000. Anyone running something older than this
99  * reeeeeeeally needs to upgrade! */
100
101 /* Normal windows (platform-specific) includes */
102 #include <winsock2.h>
103 #include <windows.h>
104 #include <ws2tcpip.h>
105 #include <sys/types.h>
106 #include <sys/stat.h>
107 #include <direct.h>
108 #include <process.h>
109 #include <stdio.h>
110 #include <algorithm>
111 #include <io.h>
112 #include <psapi.h>
113
114 #ifdef ENABLE_CRASHDUMPS
115 #include <DbgHelp.h>
116 #endif
117
118 /* strcasecmp is not defined on windows by default */
119 #define strcasecmp _stricmp
120
121 /* this standard function is nonstarard. go figure. */
122 #define popen _popen
123 #define pclose _pclose
124
125 /* Error macros need to be redirected to winsock error codes, apart from on VS2010 *SIGH* */
126 #if _MSC_VER < 1600
127         #define ETIMEDOUT WSAETIMEDOUT
128         #define ECONNREFUSED WSAECONNREFUSED
129         #define EADDRINUSE WSAEADDRINUSE
130         #define EINPROGRESS WSAEWOULDBLOCK
131         #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
132 #endif
133
134 /* Convert formatted (xxx.xxx.xxx.xxx) string to in_addr struct */
135 CoreExport int insp_inet_pton(int af, const char * src, void * dst);
136
137 /* Convert struct to formatted (xxx.xxx.xxx.xxx) string */
138 CoreExport const char * insp_inet_ntop(int af, const void * src, char * dst, socklen_t cnt);
139
140 /* we don't want to use windows' broken inet_pton and ntop */
141 #define inet_pton insp_inet_pton
142 #define inet_ntop insp_inet_ntop
143
144 /* Safe printf functions aren't defined in VC2003 */
145 #define snprintf _snprintf
146 #define vsnprintf _vsnprintf
147
148 /* Since when does the ISO C++ standard *remove* C functions?! */
149 #define mkdir(file,mode) _mkdir(file)
150
151 #define strncasecmp strnicmp
152
153 /* Unix-style sleep (argument is in seconds) */
154 __inline void sleep(int seconds) { Sleep(seconds * 1000); }
155
156 /* IPV4 only convert string to address struct */
157 CoreExport int inet_aton(const char *, struct in_addr *);
158
159 /* Unix-style get running user id */
160 CoreExport int geteuid();
161
162 /* Handles colors in printf */
163 CoreExport int printf_c(const char * format, ...);
164
165 /* getopt() wrapper */
166 # define no_argument            0
167 # define required_argument      1
168 # define optional_argument      2
169 struct option
170 {
171         char *name;
172         int has_arg;
173         int *flag;
174         int val;
175 };
176 extern int optind;
177 extern char optarg[514];
178 int getopt_long(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind);
179
180 /* Module Loading */
181 #define dlopen(path, state) (void*)LoadLibrary(path)
182 #define dlsym(handle, export) (void*)GetProcAddress((HMODULE)handle, export)
183 #define dlclose(handle) FreeLibrary((HMODULE)handle)
184 const char * dlerror();
185
186 /* Unix-style directory searching functions */
187 #define chmod(filename, mode)  
188
189 struct dirent
190 {
191         char d_name[MAX_PATH];
192 };
193
194 struct DIR
195 {
196         dirent dirent_pointer;
197         HANDLE find_handle;
198         WIN32_FIND_DATA find_data;
199         bool first;
200 };
201
202 struct timespec
203 {
204         time_t tv_sec;
205         long tv_nsec;
206 };
207
208 CoreExport DIR * opendir(const char * path);
209 CoreExport dirent * readdir(DIR * handle);
210 CoreExport void closedir(DIR * handle);
211
212 const int CLOCK_REALTIME = 0;
213 CoreExport int clock_gettime(int clock, struct timespec * tv);
214
215 /* Disable these stupid warnings.. */
216 #pragma warning(disable:4800)
217 #pragma warning(disable:4251)
218 #pragma warning(disable:4275)
219 #pragma warning(disable:4244)           // warning C4244: '=' : conversion from 'long' to 'short', possible loss of data
220 #pragma warning(disable:4267)           // warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
221 #pragma warning(disable:4805)           // warning C4805: '!=' : unsafe mix of type 'char' and type 'bool' in operation
222 #pragma warning(disable:4311)           // warning C4311: 'type cast' : pointer truncation from 'accept_overlap *' to 'int'
223 #pragma warning(disable:4312)           // warning C4312: 'type cast' : conversion from 'int' to 'HANDLE' of greater size
224 #pragma warning(disable:4355)           // warning C4355: 'this' : used in base member initializer list
225 #pragma warning(disable:4996)           // warning C4996: 'std::_Traits_helper::move_s' was declared deprecated
226 #pragma warning(disable:4706)           // warning C4706: assignment within conditional expression
227 #pragma warning(disable:4201)           // mmsystem.h generates this warning
228
229 /* Mehhhh... typedefs. */
230
231 typedef unsigned char uint8_t;
232 typedef unsigned long long uint64_t;
233 typedef signed char int8_t;
234 typedef signed long long int64_t;
235 typedef signed long ssize_t;
236
237 /* Shared memory allocation functions */
238 void * ::operator new(size_t iSize);
239 void ::operator delete(void * ptr);
240
241 /* IPC Handlers */
242 class ValueItem;
243 class ServerConfig;
244
245 #define DISABLE_WRITEV
246
247 /* Clear a windows console */
248 CoreExport void ClearConsole();
249
250 CoreExport DWORD WindowsForkStart();
251
252 CoreExport void WindowsForkKillOwner();
253
254 CoreExport void ChangeWindowsSpecificPointers();
255
256 CoreExport void FindDNS(std::string& server);
257
258 CoreExport bool initwmi();
259 CoreExport void donewmi();
260 CoreExport int getcpu();
261 CoreExport int random();
262 CoreExport void srandom(unsigned seed);
263 CoreExport int gettimeofday(timeval *tv, void *);
264
265 #endif
266