InspIRCd  2.0
inspircd_win32wrapper.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2007 Dennis Friis <[email protected]>
5  * Copyright (C) 2007 Craig Edwards <[email protected]>
6  * Copyright (C) 2007 Robin Burchell <[email protected]>
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 of Windows, 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 #include "win32service.h"
39 
40 /* This defaults to 64, way too small for an ircd! */
41 
42 #define FD_SETSIZE 24000
43 
44 /* Make builds smaller, leaner and faster */
45 #define VC_EXTRALEAN
46 #define WIN32_LEAN_AND_MEAN
47 
48 /* They just have to be *different*, don't they. */
49 #define PATH_MAX MAX_PATH
50 
51 /* Macros for exporting symbols - dependant on what is being compiled */
52 
53 #ifdef DLL_BUILD
54 #define CoreExport __declspec(dllimport)
55 #define DllExport __declspec(dllexport)
56 #else
57 #define CoreExport __declspec(dllexport)
58 #define DllExport __declspec(dllimport)
59 #endif
60 
61 /* Redirect main() through a different method in win32service.cpp, to intercept service startup */
62 #define ENTRYPOINT CoreExport int smain(int argc, char** argv)
63 
64 /* Disable the deprecation warnings.. it spams :P */
65 #define _CRT_SECURE_NO_DEPRECATE
66 #define _WINSOCK_DEPRECATED_NO_WARNINGS
67 
68 /* Normal windows (platform-specific) includes */
69 #include <winsock2.h>
70 #pragma comment(lib, "Ws2_32.lib")
71 #include <windows.h>
72 #include <ws2tcpip.h>
73 #include <sys/types.h>
74 #include <sys/stat.h>
75 #include <direct.h>
76 #include <process.h>
77 
78 /* strcasecmp is not defined on windows by default */
79 #define strcasecmp _stricmp
80 #define strncasecmp _strnicmp
81 
82 typedef int ssize_t;
83 
84 /* Convert formatted (xxx.xxx.xxx.xxx) string to in_addr struct */
85 CoreExport int insp_inet_pton(int af, const char * src, void * dst);
86 
87 /* Convert struct to formatted (xxx.xxx.xxx.xxx) string */
88 CoreExport const char * insp_inet_ntop(int af, const void * src, char * dst, socklen_t cnt);
89 
90 /* inet_pton/ntop require at least NT 6.0 */
91 #define inet_pton insp_inet_pton
92 #define inet_ntop insp_inet_ntop
93 
94 /* Safe printf functions aren't defined in VC++ releases older than v14 */
95 #if _MSC_VER <= 1800
96 #define snprintf _snprintf
97 #define vsnprintf _vsnprintf
98 #endif
99 
100 /* Unix-style sleep (argument is in seconds) */
101 __inline void sleep(int seconds) { Sleep(seconds * 1000); }
102 
103 /* _popen, _pclose */
104 #define popen _popen
105 #define pclose _pclose
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 #if _MSC_VER <= 1800
143 struct timespec
144 {
145  time_t tv_sec;
146  long tv_nsec;
147 };
148 #endif
149 
150 CoreExport DIR * opendir(const char * path);
151 CoreExport dirent * readdir(DIR * handle);
152 CoreExport void closedir(DIR * handle);
153 
154 // warning: 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
155 // Normally, this is a huge problem, but due to our new/delete remap, we can ignore it.
156 #pragma warning(disable:4251)
157 
158 // warning: DLL-interface classkey 'identifier' used as base for DLL-interface classkey 'identifier'
159 #pragma warning(disable:4275)
160 
161 // warning: unreferenced formal parameter
162 // Unimportant for now, but for the next version, we should take a look at these again.
163 #pragma warning(disable:4100)
164 
165 // warning: 'class' : assignment operator could not be generated
166 #pragma warning(disable:4512)
167 
168 // warning C4127: conditional expression is constant
169 // This will be triggered like crazy because FOREACH_MOD and similar macros are wrapped in do { ... } while(0) constructs
170 #pragma warning(disable:4127)
171 
172 // warning C4996: The POSIX name for this item is deprecated.
173 #pragma warning(disable:4996)
174 
175 // warning C4244: conversion from 'x' to 'y', possible loss of data
176 #pragma warning(disable:4244)
177 
178 // warning C4267: 'var' : conversion from 'size_t' to 'type', possible loss of data
179 #pragma warning(disable:4267)
180 
181 // warning C4706: assignment within conditional expression
182 #pragma warning(disable:4706)
183 
184 // warning C4355: 'this' : used in base member initializer list
185 // This warning is disabled by default since VC2012
186 #if _MSC_VER < 1700
187 #pragma warning(disable:4355)
188 #endif
189 
190 /* Shared memory allocation functions */
191 void * ::operator new(size_t iSize);
192 void ::operator delete(void * ptr);
193 
194 #define DISABLE_WRITEV
195 
196 #include <exception>
197 
198 class CWin32Exception : public std::exception
199 {
200 public:
201  CWin32Exception();
202  CWin32Exception(const CWin32Exception& other);
203  virtual const char* what() const throw();
204  DWORD GetErrorCode();
205 
206 private:
207  char szErrorString[500];
208  DWORD dwErrorCode;
209 };
210 
211 #endif
212 
Definition: inspircd_win32wrapper.h:134
Definition: inspircd_win32wrapper.h:129
Definition: inspircd_win32wrapper.h:118
Definition: inspircd_win32wrapper.h:143
Definition: inspircd_win32wrapper.h:198