]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_win32wrapper.h
Use iostream instead of C-style file operations.
[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
72 /* Windows defines this already. */
73 #undef ERROR
74
75 /* strcasecmp is not defined on windows by default */
76 #define strcasecmp _stricmp
77 #define strncasecmp _strnicmp
78
79 /* Convert formatted (xxx.xxx.xxx.xxx) string to in_addr struct */
80 CoreExport int insp_inet_pton(int af, const char * src, void * dst);
81
82 /* Convert struct to formatted (xxx.xxx.xxx.xxx) string */
83 CoreExport const char * insp_inet_ntop(int af, const void * src, char * dst, socklen_t cnt);
84
85 /* inet_pton/ntop require at least NT 6.0 */
86 #define inet_pton insp_inet_pton
87 #define inet_ntop insp_inet_ntop
88
89 /* Safe printf functions aren't defined in VC++ */
90 #define snprintf _snprintf
91 #define vsnprintf _vsnprintf
92
93 #ifndef va_copy
94 #define va_copy(dest, src) (dest = src)
95 #endif
96
97 /* Unix-style sleep (argument is in seconds) */
98 __inline void sleep(int seconds) { Sleep(seconds * 1000); }
99
100 /* _popen, _pclose */
101 #define popen _popen
102 #define pclose _pclose
103
104 /* _access */
105 #define access _access
106
107 /* IPV4 only convert string to address struct */
108 __inline int inet_aton(const char *cp, struct in_addr *addr)
109
110         addr->s_addr = inet_addr(cp);
111         return (addr->s_addr == INADDR_NONE) ? 0 : 1;
112 };
113
114 /* getopt() wrapper */
115 #define no_argument            0
116 #define required_argument      1
117 #define optional_argument      2
118 struct option
119 {
120         char *name;
121         int has_arg;
122         int *flag;
123         int val;
124 };
125 extern int optind;
126 extern char optarg[514];
127 int getopt_long(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind);
128
129 struct dirent
130 {
131         char d_name[MAX_PATH];
132 };
133
134 struct DIR
135 {
136         dirent dirent_pointer;
137         HANDLE find_handle;
138         WIN32_FIND_DATAA find_data;
139         bool first;
140 };
141
142 struct timespec
143 {
144         time_t tv_sec;
145         long tv_nsec;
146 };
147
148 CoreExport DIR * opendir(const char * path);
149 CoreExport dirent * readdir(DIR * handle);
150 CoreExport void closedir(DIR * handle);
151
152 // warning: 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
153 // Normally, this is a huge problem, but due to our new/delete remap, we can ignore it.
154 #pragma warning(disable:4251)
155
156 // warning: DLL-interface classkey 'identifier' used as base for DLL-interface classkey 'identifier'
157 #pragma warning(disable:4275)
158
159 // warning: unreferenced formal parameter
160 // Unimportant for now, but for the next version, we should take a look at these again.
161 #pragma warning(disable:4100)
162
163 // warning: 'class' : assignment operator could not be generated
164 #pragma warning(disable:4512)
165
166 // warning C4127: conditional expression is constant
167 // This will be triggered like crazy because FOREACH_MOD and similar macros are wrapped in do { ... } while(0) constructs
168 #pragma warning(disable:4127)
169
170 // warning C4996: The POSIX name for this item is deprecated.
171 #pragma warning(disable:4996)
172
173 // warning C4244: conversion from 'x' to 'y', possible loss of data
174 #pragma warning(disable:4244)
175
176 // warning C4267: 'var' : conversion from 'size_t' to 'type', possible loss of data
177 #pragma warning(disable:4267)
178
179 // warning C4706: assignment within conditional expression
180 #pragma warning(disable:4706)
181
182 // warning C4355: 'this' : used in base member initializer list
183 // This warning is disabled by default since VC2012
184 #if _MSC_VER < 1700
185 #pragma warning(disable:4355)
186 #endif
187
188 /* Shared memory allocation functions */
189 void * ::operator new(size_t iSize);
190 void ::operator delete(void * ptr);
191
192 #define DISABLE_WRITEV
193
194 #include <exception>
195
196 class CWin32Exception : public std::exception
197 {
198 public:
199         CWin32Exception();
200         CWin32Exception(const CWin32Exception& other);
201         virtual const char* what() const throw();
202         DWORD GetErrorCode();
203
204 private:
205         char szErrorString[500];
206         DWORD dwErrorCode;
207 };