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