]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_win32wrapper.h
Merge the latest changes from insp20 into master.
[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 #define _WINSOCK_DEPRECATED_NO_WARNINGS
62
63 /* Normal windows (platform-specific) includes */
64 #include <winsock2.h>
65 #pragma comment(lib, "Ws2_32.lib")
66 #include <windows.h>
67 #include <ws2tcpip.h>
68 #include <sys/types.h>
69 #include <sys/stat.h>
70 #include <direct.h>
71 #include <process.h>
72 #include <io.h>
73
74 #define F_OK            0       /* test for existence of file */
75 #define X_OK            (1<<0)  /* test for execute or search permission */
76 #define W_OK            (1<<1)  /* test for write permission */
77 #define R_OK            (1<<2)  /* test for read permission */
78
79 // Windows defines these already.
80 #undef ERROR
81 #undef min
82 #undef max
83
84 /* strcasecmp is not defined on windows by default */
85 #define strcasecmp _stricmp
86 #define strncasecmp _strnicmp
87
88 typedef int ssize_t;
89
90 /* _popen, _pclose */
91 #define popen _popen
92 #define pclose _pclose
93
94 /* getopt() wrapper */
95 #define no_argument            0
96 #define required_argument      1
97 #define optional_argument      2
98 struct option
99 {
100         char *name;
101         int has_arg;
102         int *flag;
103         int val;
104 };
105 extern int optind;
106 extern char optarg[514];
107 int getopt_long(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind);
108
109 struct dirent
110 {
111         char d_name[MAX_PATH];
112 };
113
114 struct DIR
115 {
116         dirent dirent_pointer;
117         HANDLE find_handle;
118         WIN32_FIND_DATAA find_data;
119         bool first;
120 };
121
122 CoreExport DIR * opendir(const char * path);
123 CoreExport dirent * readdir(DIR * handle);
124 CoreExport void closedir(DIR * handle);
125
126 // warning: 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
127 // Normally, this is a huge problem, but due to our new/delete remap, we can ignore it.
128 #pragma warning(disable:4251)
129
130 // warning: DLL-interface classkey 'identifier' used as base for DLL-interface classkey 'identifier'
131 #pragma warning(disable:4275)
132
133 // warning: unreferenced formal parameter
134 // Unimportant for now, but for the next version, we should take a look at these again.
135 #pragma warning(disable:4100)
136
137 // warning: 'class' : assignment operator could not be generated
138 #pragma warning(disable:4512)
139
140 // warning C4127: conditional expression is constant
141 // This will be triggered like crazy because FOREACH_MOD and similar macros are wrapped in do { ... } while(0) constructs
142 #pragma warning(disable:4127)
143
144 // warning C4996: The POSIX name for this item is deprecated.
145 #pragma warning(disable:4996)
146
147 // warning C4244: conversion from 'x' to 'y', possible loss of data
148 #pragma warning(disable:4244)
149
150 // warning C4267: 'var' : conversion from 'size_t' to 'type', possible loss of data
151 #pragma warning(disable:4267)
152
153 // warning C4706: assignment within conditional expression
154 #pragma warning(disable:4706)
155
156 /* Shared memory allocation functions */
157 void * ::operator new(size_t iSize);
158 void ::operator delete(void * ptr);
159
160 #include <exception>
161
162 class CWin32Exception : public std::exception
163 {
164 public:
165         CWin32Exception();
166         CWin32Exception(const CWin32Exception& other);
167         virtual const char* what() const throw();
168         DWORD GetErrorCode();
169
170 private:
171         char szErrorString[500];
172         DWORD dwErrorCode;
173 };
174
175 // Same value as EXIT_STATUS_FORK (EXIT_STATUS_FORK is unused on Windows)
176 #define EXIT_STATUS_SERVICE 4
177
178 // POSIX iovec
179 struct iovec
180 {
181         void* iov_base; // Starting address
182         size_t iov_len; // Number of bytes to transfer
183 };
184
185 // Windows WSABUF with POSIX field names
186 struct WindowsIOVec
187 {
188         // POSIX iovec has iov_base then iov_len, WSABUF in Windows has the fields in reverse order
189         u_long iov_len; // Number of bytes to transfer
190         char FAR* iov_base; // Starting address
191 };
192
193 inline ssize_t writev(int fd, const WindowsIOVec* iov, int count)
194 {
195         DWORD sent;
196         int ret = WSASend(fd, reinterpret_cast<LPWSABUF>(const_cast<WindowsIOVec*>(iov)), count, &sent, 0, NULL, NULL);
197         if (ret == 0)
198                 return sent;
199         return -1;
200 }