e070a79a2f545d405a20e928d617550ac5670276
[strongswan.git] / src / libstrongswan / utils / windows.h
1 /*
2 * Copyright (C) 2013 Martin Willi
3 * Copyright (C) 2013 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 /**
17 * @defgroup windows windows
18 * @{ @ingroup utils
19 */
20
21 #ifndef WINDOWS_H_
22 #define WINDOWS_H_
23
24 #include <winsock2.h>
25 #include <ws2tcpip.h>
26 #include <direct.h>
27 #include <inttypes.h>
28
29 /* undef Windows variants evaluating values more than once */
30 #undef min
31 #undef max
32
33 /* interface is defined as an alias to "struct" in basetypes.h, but
34 * we use it here and there as ordinary identifier. */
35 #undef interface
36
37 /* used by Windows API, but we have our own */
38 #undef CALLBACK
39
40 /* UID/GID types for capabilities, even if not supported */
41 typedef u_int uid_t;
42 typedef u_int gid_t;
43
44 /**
45 * Initialize Windows libraries
46 */
47 void windows_init();
48
49 /**
50 * Deinitialize windows libraries
51 */
52 void windows_deinit();
53
54 /**
55 * Replacement for random(3)
56 */
57 static inline long random(void)
58 {
59 return rand();
60 }
61
62 /**
63 * Replacement for srandom(3)
64 */
65 static inline void srandom(unsigned int seed)
66 {
67 srand(seed);
68 }
69
70 /**
71 * Replacement of sched_yield(2) from <sched.h>
72 */
73 static inline int sched_yield(void)
74 {
75 Sleep(0);
76 return 0;
77 }
78
79 /**
80 * Replacement of sleep(3), cancellable by thread_cancel()
81 */
82 static inline int sleep(unsigned int seconds)
83 {
84 SleepEx(seconds * 1000, TRUE);
85 return 0;
86 }
87
88 /**
89 * Replacement of usleep(3), cancellable, ms resolution only
90 */
91 int usleep(useconds_t usec);
92
93 /**
94 * strdup(3), the Windows variant can't free(strdup("")) and others
95 */
96 #define strdup strdup_windows
97 static inline char* strdup_windows(const char *src)
98 {
99 size_t len;
100 char *dst;
101
102 len = strlen(src) + 1;
103 dst = malloc(len);
104 memcpy(dst, src, len);
105 return dst;
106 }
107
108 /**
109 * Provided via ws2_32
110 */
111 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
112
113 /**
114 * Provided via ws2_32
115 */
116 int inet_pton(int af, const char *src, void *dst);
117
118 /**
119 * Provided by printf hook backend
120 */
121 int asprintf(char **strp, const char *fmt, ...);
122
123 /**
124 * Provided by printf hook backend
125 */
126 int vasprintf(char **strp, const char *fmt, va_list ap);
127
128 /**
129 * timeradd(3) from <sys/time.h>
130 */
131 static inline void timeradd(struct timeval *a, struct timeval *b,
132 struct timeval *res)
133 {
134 res->tv_sec = a->tv_sec + b->tv_sec;
135 res->tv_usec = a->tv_usec + b->tv_usec;
136 if (res->tv_usec >= 1000000)
137 {
138 res->tv_usec -= 1000000;
139 res->tv_sec++;
140 }
141 }
142
143 /**
144 * timersub(3) from <sys/time.h>
145 */
146 static inline void timersub(struct timeval *a, struct timeval *b,
147 struct timeval *res)
148 {
149 res->tv_sec = a->tv_sec - b->tv_sec;
150 res->tv_usec = a->tv_usec - b->tv_usec;
151 if (res->tv_usec < 0)
152 {
153 res->tv_usec += 1000000;
154 res->tv_sec--;
155 }
156 }
157
158 /**
159 * gmtime_r(3) from <time.h>
160 */
161 static inline struct tm *gmtime_r(const time_t *timep, struct tm *result)
162 {
163 struct tm *ret;
164
165 /* gmtime_s() and friends seem not to be implemented/functioning.
166 * Relying on gmtime() on Windows works as well, as it uses thread
167 * specific buffers. */
168 ret = gmtime(timep);
169 if (ret)
170 {
171 memcpy(result, ret, sizeof(*result));
172 }
173 return ret;
174 }
175
176 /**
177 * localtime_r(3) from <time.h>
178 */
179 static inline struct tm *localtime_r(const time_t *timep, struct tm *result)
180 {
181 struct tm *ret;
182
183 /* localtime_s() and friends seem not to be implemented/functioning.
184 * Relying on localtime() on Windows works as well, as it uses thread
185 * specific buffers. */
186 ret = localtime(timep);
187 if (ret)
188 {
189 memcpy(result, ret, sizeof(*result));
190 }
191 return ret;
192 }
193
194 /**
195 * setenv(3) from <stdlib.h>, overwrite flag is ignored
196 */
197 static inline int setenv(const char *name, const char *value, int overwrite)
198 {
199 if (SetEnvironmentVariableA(name, value) == 0)
200 { /* failed */
201 return -1;
202 }
203 return 0;
204 }
205
206 /**
207 * Lazy binding, ignored on Windows
208 */
209 #define RTLD_LAZY 1
210
211 /**
212 * Default handle targeting .exe
213 */
214 #define RTLD_DEFAULT (NULL)
215
216 /**
217 * Find symbol in next library
218 */
219 #define RTLD_NEXT ((void*)~(uintptr_t)0)
220
221 /**
222 * dlopen(3) from <dlfcn.h>
223 */
224 void* dlopen(const char *filename, int flag);
225
226 /**
227 * dlsym() from <dlfcn.h>
228 */
229 void* dlsym(void *handle, const char *symbol);
230
231 /**
232 * dlerror(3) from <dlfcn.h>, currently not thread save
233 */
234 char* dlerror(void);
235
236 /**
237 * dlclose() from <dlfcn.h>
238 */
239 int dlclose(void *handle);
240
241 /**
242 * socketpair(2) for SOCK_STREAM, uses TCP on loopback
243 */
244 int socketpair(int domain, int type, int protocol, int sv[2]);
245
246 /**
247 * Map MSG_DONTWAIT to the reserved, but deprecated MSG_INTERRUPT
248 */
249 #define MSG_DONTWAIT MSG_INTERRUPT
250
251 /**
252 * EWOULDBLOCK is EAGAIN on other systems as well
253 */
254 #define EWOULDBLOCK EAGAIN
255
256 /**
257 * ECONNRESET is mapped to something arbitrary. It is returned by
258 * stream->read_all() but should not be mapped from a send/recv WSA error.
259 */
260 #define ECONNRESET ENXIO
261
262 /**
263 * recv(2) with support for MSG_DONTWAIT
264 */
265 #define recv(...) windows_recv(__VA_ARGS__)
266 ssize_t windows_recv(int sockfd, void *buf, size_t len, int flags);
267
268 /**
269 * recvfrom(2) with support for MSG_DONTWAIT
270 */
271 #define recvfrom(...) windows_recvfrom(__VA_ARGS__)
272 ssize_t windows_recvfrom(int sockfd, void *buf, size_t len, int flags,
273 struct sockaddr *src_addr, socklen_t *addrlen);
274
275 /**
276 * recvfrom(2) with support for MSG_DONTWAIT
277 */
278 #define send(...) windows_send(__VA_ARGS__)
279 ssize_t windows_send(int sockfd, const void *buf, size_t len, int flags);
280
281 /**
282 * recvfrom(2) with support for MSG_DONTWAIT
283 */
284 #define sendto(...) windows_send(__VA_ARGS__)
285 ssize_t windows_sendto(int sockfd, const void *buf, size_t len, int flags,
286 const struct sockaddr *dest_addr, socklen_t addrlen);
287
288 /* Windows does not support "ll" format printf length modifiers. Mingw
289 * therefore maps these to the Windows specific I64 length modifier. That
290 * won't work for us, as we use our own printf backend on Windows, which works
291 * just fine with "ll". */
292 #undef PRId64
293 #define PRId64 "lld"
294 #undef PRId64
295 #define PRId64 "lld"
296 #undef PRIdLEAST64
297 #define PRIdLEAST64 "lld"
298 #undef PRIdFAST64
299 #define PRIdFAST64 "lld"
300 #undef PRIdMAX
301 #define PRIdMAX "lld"
302 #undef PRIi64
303 #define PRIi64 "lli"
304 #undef PRIiLEAST64
305 #define PRIiLEAST64 "lli"
306 #undef PRIiFAST64
307 #define PRIiFAST64 "lli"
308 #undef PRIiMAX
309 #define PRIiMAX "lli"
310 #undef PRIo64
311 #define PRIo64 "llo"
312 #undef PRIoLEAST64
313 #define PRIoLEAST64 "llo"
314 #undef PRIoFAST64
315 #define PRIoFAST64 "llo"
316 #undef PRIoMAX
317 #define PRIoMAX "llo"
318 #undef PRIu64
319 #define PRIu64 "llu"
320 #undef PRIuLEAST64
321 #define PRIuLEAST64 "llu"
322 #undef PRIuFAST64
323 #define PRIuFAST64 "llu"
324 #undef PRIuMAX
325 #define PRIuMAX "llu"
326 #undef PRIx64
327 #define PRIx64 "llx"
328 #undef PRIxLEAST64
329 #define PRIxLEAST64 "llx"
330 #undef PRIxFAST64
331 #define PRIxFAST64 "llx"
332 #undef PRIxMAX
333 #define PRIxMAX "llx"
334 #undef PRIX64
335 #define PRIX64 "llX"
336 #undef PRIXLEAST64
337 #define PRIXLEAST64 "llX"
338 #undef PRIXFAST64
339 #define PRIXFAST64 "llX"
340 #undef PRIXMAX
341 #define PRIXMAX "llX"
342
343 #ifdef _WIN64
344 # undef PRIdPTR
345 # define PRIdPTR "lld"
346 # undef PRIiPTR
347 # define PRIiPTR "lli"
348 # undef PRIoPTR
349 # define PRIoPTR "llo"
350 # undef PRIuPTR
351 # define PRIuPTR "llu"
352 # undef PRIxPTR
353 # define PRIxPTR "llx"
354 # undef PRIXPTR
355 # define PRIXPTR "llX"
356 #endif /* _WIN64 */
357
358 #endif /** WINDOWS_H_ @}*/