]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_win32wrapper.h
No forget using windows' broken inet_pton and inet_ntop, they are not POSIX compliant...
[user/henk/code/inspircd.git] / win / inspircd_win32wrapper.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* Windows Port
15    Wrapper Functions/Definitions
16    By Burlex */
17
18 #ifndef INSPIRCD_WIN32WRAPPER_H
19 #define INSPIRCD_WIN32WRAPPER_H
20
21 #ifndef CONFIGURE_BUILD
22 #include "win32service.h"
23 #endif
24
25 /* Define the WINDOWS macro. This means we're building on windows to the rest of the server.
26    I think this is more reasonable than using WIN32, especially if we're gonna be doing 64-bit compiles */
27 #define WINDOWS 1
28 #define ENABLE_CRASHDUMPS 0
29
30 /* This defaults to 64, way too small for an ircd! */
31 /* CRT memory debugging */
32 #ifdef DEBUG
33 #define _CRTDBG_MAP_ALLOC
34 #include <stdlib.h>
35 #include <crtdbg.h>
36 #endif
37
38 #define FD_SETSIZE 24000
39
40 #define uint32_t DWORD
41 #define uint16_t WORD
42
43 /* Make builds smaller, leaner and faster */
44 #define VC_EXTRALEAN
45 #define WIN32_LEAN_AND_MEAN
46
47 /* Not defined in windows */
48 #define SIGHUP 1
49
50 /* Not defined in windows, parameter to shutdown() */
51 #define SHUT_WR 2
52
53 /* They just have to be *different*, don't they. */
54 #define PATH_MAX MAX_PATH
55
56 /* Begone shitty 'safe STL' warnings */
57 #define _SCL_SECURE_NO_WARNINGS
58 #define _CRT_SECURE_NO_WARNINGS
59 #define _AFX_SECURE_NO_WARNINGS
60 #define _ATL_SECURE_NO_WARNINGS
61
62 /* Macros for exporting symbols - dependant on what is being compiled */
63
64 #ifdef DLL_BUILD
65 #define CoreExport __declspec(dllimport)
66 #define DllExport __declspec(dllexport)
67 #else
68 #define CoreExport __declspec(dllexport)
69 #define DllExport __declspec(dllimport)
70 #endif
71
72 /* Redirect main() through a different method in win32service.cpp, to intercept service startup */
73 #define ENTRYPOINT CoreExport int smain(int argc, char** argv)
74
75 /* Disable the deprecation warnings.. it spams :P */
76 #define _CRT_SECURE_NO_DEPRECATE
77 #define _SCL_SECURE_NO_DEPRECATE
78
79 #include <string>
80
81 /* Say we're building on windows 2000. Anyone running something older than this
82  * reeeeeeeally needs to upgrade! */
83
84 /* Normal windows (platform-specific) includes */
85 #include <winsock2.h>
86 #include <windows.h>
87 #include <ws2tcpip.h>
88 #include <sys/types.h>
89 #include <sys/stat.h>
90 #include <direct.h>
91 #include <process.h>
92 #include <stdio.h>
93 #include <algorithm>
94
95 #ifdef ENABLE_CRASHDUMPS
96 #include <DbgHelp.h>
97 #endif
98
99 /* strcasecmp is not defined on windows by default */
100 #define strcasecmp _stricmp
101
102 /* this standard function is nonstarard. go figure. */
103 #define popen _popen
104 #define pclose _pclose
105
106 /* Error macros need to be redirected to winsock error codes */
107 #define ETIMEDOUT WSAETIMEDOUT
108 #define ECONNREFUSED WSAECONNREFUSED
109 #define EADDRINUSE WSAEADDRINUSE
110 #define EINPROGRESS WSAEWOULDBLOCK
111 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
112
113 /* Convert formatted (xxx.xxx.xxx.xxx) string to in_addr struct */
114 CoreExport int insp_inet_pton(int af, const char * src, void * dst);
115
116 /* Convert struct to formatted (xxx.xxx.xxx.xxx) string */
117 CoreExport const char * insp_inet_ntop(int af, const void * src, char * dst, socklen_t cnt);
118
119 /* we don't want to use windows' broken inet_pton and ntop */
120 #define inet_pton insp_inet_pton
121 #define inet_ntop insp_inet_ntop
122
123 /* Safe printf functions aren't defined in VC2003 */
124 #define snprintf _snprintf
125 #define vsnprintf _vsnprintf
126
127 /* Since when does the ISO C++ standard *remove* C functions?! */
128 #define mkdir(file,mode) _mkdir(file)
129
130 /* Unix-style sleep (argument is in seconds) */
131 __inline void sleep(int seconds) { Sleep(seconds * 1000); }
132
133 /* IPV4 only convert string to address struct */
134 CoreExport int inet_aton(const char *, struct in_addr *);
135
136 /* Unix-style get running user id */
137 CoreExport int geteuid();
138
139 /* Handles colors in printf */
140 CoreExport int printf_c(const char * format, ...);
141
142 /* getopt() wrapper */
143 # define no_argument            0
144 # define required_argument      1
145 # define optional_argument      2
146 struct option
147 {
148         char *name;
149         int has_arg;
150         int *flag;
151         int val;
152 };
153 extern char optarg[514];
154 int getopt_long_only (int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind);
155
156 /* Module Loading */
157 #define dlopen(path, state) (void*)LoadLibrary(path)
158 #define dlsym(handle, export) (void*)GetProcAddress((HMODULE)handle, export)
159 #define dlclose(handle) FreeLibrary((HMODULE)handle)
160 const char * dlerror();
161
162 /* Unix-style directory searching functions */
163 #define chmod(filename, mode)  
164
165 struct dirent
166 {
167         char d_name[MAX_PATH];
168 };
169
170 struct DIR
171 {
172         dirent dirent_pointer;
173         HANDLE find_handle;
174         WIN32_FIND_DATA find_data;
175         bool first;
176 };
177
178 CoreExport DIR * opendir(const char * path);
179 CoreExport dirent * readdir(DIR * handle);
180 CoreExport void closedir(DIR * handle);
181
182 CoreExport int gettimeofday(struct timeval * tv, void * tz);
183
184 /* Disable these stupid warnings.. */
185 #pragma warning(disable:4800)
186 #pragma warning(disable:4251)
187 #pragma warning(disable:4275)
188 #pragma warning(disable:4244)           // warning C4244: '=' : conversion from 'long' to 'short', possible loss of data
189 #pragma warning(disable:4267)           // warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
190 #pragma warning(disable:4805)           // warning C4805: '!=' : unsafe mix of type 'char' and type 'bool' in operation
191 #pragma warning(disable:4311)           // warning C4311: 'type cast' : pointer truncation from 'accept_overlap *' to 'int'
192 #pragma warning(disable:4312)           // warning C4312: 'type cast' : conversion from 'int' to 'HANDLE' of greater size
193 #pragma warning(disable:4355)           // warning C4355: 'this' : used in base member initializer list
194 #pragma warning(disable:4996)           // warning C4996: 'std::_Traits_helper::move_s' was declared deprecated
195 #pragma warning(disable:4706)           // warning C4706: assignment within conditional expression
196 #pragma warning(disable:4201)           // mmsystem.h generates this warning
197
198 /* Mehhhh... typedefs. */
199
200 typedef unsigned char uint8_t;
201 typedef unsigned long long uint64_t;
202 typedef signed char int8_t;
203 typedef signed long int32_t;
204 typedef signed long long int64_t;
205
206 /* Shared memory allocation functions */
207 void * ::operator new(size_t iSize);
208 void ::operator delete(void * ptr);
209
210 /* IPC Handlers */
211 class InspIRCd;
212 class ValueItem;
213 class ServerConfig;
214
215 /* Look up the nameserver in use from the registry on windows */
216 CoreExport std::string FindNameServerWin();
217
218 /* Clear a windows console */
219 CoreExport void ClearConsole();
220
221 CoreExport DWORD WindowsForkStart(InspIRCd* Instance);
222
223 CoreExport void WindowsForkKillOwner(InspIRCd* Instance);
224
225 CoreExport void ChangeWindowsSpecificPointers(InspIRCd* Instance);
226
227 CoreExport bool ValidateWindowsDnsServer(ServerConfig* conf, const char* tag, const char* value, ValueItem &data);
228
229 CoreExport bool initwmi();
230 CoreExport void donewmi();
231 CoreExport int getcpu();
232
233 #endif
234