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