fd433845555243af30b0e22327db41d0a11a9d73
[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 #include <unistd.h>
29
30 /* undef Windows variants evaluating values more than once */
31 #undef min
32 #undef max
33
34 /* interface is defined as an alias to "struct" in basetypes.h, but
35 * we use it here and there as ordinary identifier. */
36 #undef interface
37
38 /* used by Windows API, but we have our own */
39 #undef CALLBACK
40
41 /* UID/GID types for capabilities, even if not supported */
42 typedef u_int uid_t;
43 typedef u_int gid_t;
44
45 /**
46 * Initialize Windows libraries
47 */
48 void windows_init();
49
50 /**
51 * Deinitialize windows libraries
52 */
53 void windows_deinit();
54
55 /**
56 * Replacement for random(3)
57 */
58 static inline long random(void)
59 {
60 return rand();
61 }
62
63 /**
64 * Replacement for srandom(3)
65 */
66 static inline void srandom(unsigned int seed)
67 {
68 srand(seed);
69 }
70
71 /**
72 * Replacement of sched_yield(2) from <sched.h>
73 */
74 static inline int sched_yield(void)
75 {
76 Sleep(0);
77 return 0;
78 }
79
80 /**
81 * Replacement of sleep(3), cancellable by thread_cancel()
82 */
83 #define sleep sleep_cancellable
84 static inline int sleep_cancellable(unsigned int seconds)
85 {
86 SleepEx(seconds * 1000, TRUE);
87 return 0;
88 }
89
90 /**
91 * Replacement of usleep(3), cancellable, ms resolution only
92 */
93 int usleep(useconds_t usec);
94
95 /**
96 * strdup(3), the Windows variant can't free(strdup("")) and others
97 */
98 #define strdup strdup_windows
99 static inline char* strdup_windows(const char *src)
100 {
101 size_t len;
102 char *dst;
103
104 len = strlen(src) + 1;
105 dst = malloc(len);
106 memcpy(dst, src, len);
107 return dst;
108 }
109
110 /**
111 * strndup(3)
112 */
113 char* strndup(const char *s, size_t n);
114
115 /**
116 * Provided via ws2_32
117 */
118 #ifndef InetNtop
119 const char WINAPI *inet_ntop(int af, const void *src, char *dst, socklen_t size);
120 #endif
121
122 /**
123 * Provided via ws2_32
124 */
125 #ifndef InetPton
126 int WINAPI inet_pton(int af, const char *src, void *dst);
127 #endif
128
129 /**
130 * Provided by printf hook backend
131 */
132 int asprintf(char **strp, const char *fmt, ...);
133
134 /**
135 * Provided by printf hook backend
136 */
137 int vasprintf(char **strp, const char *fmt, va_list ap);
138
139 /**
140 * timeradd(3) from <sys/time.h>
141 */
142 static inline void timeradd(struct timeval *a, struct timeval *b,
143 struct timeval *res)
144 {
145 res->tv_sec = a->tv_sec + b->tv_sec;
146 res->tv_usec = a->tv_usec + b->tv_usec;
147 if (res->tv_usec >= 1000000)
148 {
149 res->tv_usec -= 1000000;
150 res->tv_sec++;
151 }
152 }
153
154 /**
155 * timersub(3) from <sys/time.h>
156 */
157 static inline void timersub(struct timeval *a, struct timeval *b,
158 struct timeval *res)
159 {
160 res->tv_sec = a->tv_sec - b->tv_sec;
161 res->tv_usec = a->tv_usec - b->tv_usec;
162 if (res->tv_usec < 0)
163 {
164 res->tv_usec += 1000000;
165 res->tv_sec--;
166 }
167 }
168
169 /**
170 * gmtime_r(3) from <time.h>
171 */
172 static inline struct tm *gmtime_r(const time_t *timep, struct tm *result)
173 {
174 struct tm *ret;
175
176 /* gmtime_s() and friends seem not to be implemented/functioning.
177 * Relying on gmtime() on Windows works as well, as it uses thread
178 * specific buffers. */
179 ret = gmtime(timep);
180 if (ret)
181 {
182 memcpy(result, ret, sizeof(*result));
183 }
184 return ret;
185 }
186
187 /**
188 * localtime_r(3) from <time.h>
189 */
190 static inline struct tm *localtime_r(const time_t *timep, struct tm *result)
191 {
192 struct tm *ret;
193
194 /* localtime_s() and friends seem not to be implemented/functioning.
195 * Relying on localtime() on Windows works as well, as it uses thread
196 * specific buffers. */
197 ret = localtime(timep);
198 if (ret)
199 {
200 memcpy(result, ret, sizeof(*result));
201 }
202 return ret;
203 }
204
205 /**
206 * setenv(3) from <stdlib.h>, overwrite flag is ignored
207 */
208 static inline int setenv(const char *name, const char *value, int overwrite)
209 {
210 if (SetEnvironmentVariableA(name, value) == 0)
211 { /* failed */
212 return -1;
213 }
214 return 0;
215 }
216
217 /**
218 * Lazy binding, ignored on Windows
219 */
220 #define RTLD_LAZY 1
221
222 /**
223 * Default handle targeting .exe
224 */
225 #define RTLD_DEFAULT (NULL)
226
227 /**
228 * Find symbol in next library
229 */
230 #define RTLD_NEXT ((void*)~(uintptr_t)0)
231
232 /**
233 * dlopen(3) from <dlfcn.h>
234 */
235 void* dlopen(const char *filename, int flag);
236
237 /**
238 * dlsym() from <dlfcn.h>
239 */
240 void* dlsym(void *handle, const char *symbol);
241
242 /**
243 * dlerror(3) from <dlfcn.h>, currently not thread save
244 */
245 char* dlerror(void);
246
247 /**
248 * dlclose() from <dlfcn.h>
249 */
250 int dlclose(void *handle);
251
252 /**
253 * socketpair(2) for SOCK_STREAM, uses TCP on loopback
254 */
255 int socketpair(int domain, int type, int protocol, int sv[2]);
256
257 /**
258 * getpass(3) on Windows consoles
259 */
260 char* getpass(const char *prompt);
261 #define HAVE_GETPASS
262
263 /**
264 * Map MSG_DONTWAIT to the reserved, but deprecated MSG_INTERRUPT
265 */
266 #define MSG_DONTWAIT MSG_INTERRUPT
267
268 /**
269 * shutdown(2) "how"-aliases, to use Unix variant on Windows
270 */
271 #define SHUT_RD SD_RECEIVE
272 #define SHUT_WR SD_SEND
273 #define SHUT_RDWR SD_BOTH
274
275 /**
276 * close(2) working for file handles and Winsock sockets
277 */
278 #define close windows_close
279 int windows_close(int fd);
280
281 /**
282 * recv(2) with support for MSG_DONTWAIT
283 */
284 #define recv windows_recv
285 ssize_t windows_recv(int sockfd, void *buf, size_t len, int flags);
286
287 /**
288 * recvfrom(2) with support for MSG_DONTWAIT
289 */
290 #define recvfrom windows_recvfrom
291 ssize_t windows_recvfrom(int sockfd, void *buf, size_t len, int flags,
292 struct sockaddr *src_addr, socklen_t *addrlen);
293
294 /**
295 * recvfrom(2) with support for MSG_DONTWAIT
296 */
297 #define send windows_send
298 ssize_t windows_send(int sockfd, const void *buf, size_t len, int flags);
299
300 /**
301 * recvfrom(2) with support for MSG_DONTWAIT
302 */
303 #define sendto windows_send
304 ssize_t windows_sendto(int sockfd, const void *buf, size_t len, int flags,
305 const struct sockaddr *dest_addr, socklen_t addrlen);
306
307 /**
308 * MinGW does provide extended errno values. Windows itself knowns them
309 * for POSIX compatibility; we define them as well.
310 */
311 #ifndef EADDRINUSE
312 #define EADDRINUSE 100
313 #endif
314 #ifndef EADDRNOTAVAIL
315 #define EADDRNOTAVAIL 101
316 #endif
317 #ifndef EAFNOSUPPORT
318 #define EAFNOSUPPORT 102
319 #endif
320 #ifndef EALREADY
321 #define EALREADY 103
322 #endif
323 #ifndef EBADMSG
324 #define EBADMSG 104
325 #endif
326 #ifndef ECANCELED
327 #define ECANCELED 105
328 #endif
329 #ifndef ECONNABORTED
330 #define ECONNABORTED 106
331 #endif
332 #ifndef ECONNREFUSED
333 #define ECONNREFUSED 107
334 #endif
335 #ifndef ECONNRESET
336 #define ECONNRESET 108
337 #endif
338 #ifndef EDESTADDRREQ
339 #define EDESTADDRREQ 109
340 #endif
341 #ifndef EHOSTUNREACH
342 #define EHOSTUNREACH 110
343 #endif
344 #ifndef EIDRM
345 #define EIDRM 111
346 #endif
347 #ifndef EINPROGRESS
348 #define EINPROGRESS 112
349 #endif
350 #ifndef EISCONN
351 #define EISCONN 113
352 #endif
353 #ifndef ELOOP
354 #define ELOOP 114
355 #endif
356 #ifndef EMSGSIZE
357 #define EMSGSIZE 115
358 #endif
359 #ifndef ENETDOWN
360 #define ENETDOWN 116
361 #endif
362 #ifndef ENETRESET
363 #define ENETRESET 117
364 #endif
365 #ifndef ENETUNREACH
366 #define ENETUNREACH 118
367 #endif
368 #ifndef ENOBUFS
369 #define ENOBUFS 119
370 #endif
371 #ifndef ENODATA
372 #define ENODATA 120
373 #endif
374 #ifndef ENOLINK
375 #define ENOLINK 121
376 #endif
377 #ifndef ENOMSG
378 #define ENOMSG 122
379 #endif
380 #ifndef ENOPROTOOPT
381 #define ENOPROTOOPT 123
382 #endif
383 #ifndef ENOSR
384 #define ENOSR 124
385 #endif
386 #ifndef ENOSTR
387 #define ENOSTR 125
388 #endif
389 #ifndef ENOTCONN
390 #define ENOTCONN 126
391 #endif
392 #ifndef ENOTRECOVERABLE
393 #define ENOTRECOVERABLE 127
394 #endif
395 #ifndef ENOTSOCK
396 #define ENOTSOCK 128
397 #endif
398 #ifndef ENOTSUP
399 #define ENOTSUP 129
400 #endif
401 #ifndef EOPNOTSUPP
402 #define EOPNOTSUPP 130
403 #endif
404 #ifndef EOTHER
405 #define EOTHER 131
406 #endif
407 #ifndef EOVERFLOW
408 #define EOVERFLOW 132
409 #endif
410 #ifndef EOWNERDEAD
411 #define EOWNERDEAD 133
412 #endif
413 #ifndef EPROTO
414 #define EPROTO 134
415 #endif
416 #ifndef EPROTONOSUPPORT
417 #define EPROTONOSUPPORT 135
418 #endif
419 #ifndef EPROTOTYPE
420 #define EPROTOTYPE 136
421 #endif
422 #ifndef ETIME
423 #define ETIME 137
424 #endif
425 #ifndef ETIMEDOUT
426 #define ETIMEDOUT 138
427 #endif
428 #ifndef ETXTBSY
429 #define ETXTBSY 139
430 #endif
431 #ifndef EWOULDBLOCK
432 #define EWOULDBLOCK 140
433 #endif
434
435
436 /* Windows does not support "ll" format printf length modifiers. Mingw
437 * therefore maps these to the Windows specific I64 length modifier. That
438 * won't work for us, as we use our own printf backend on Windows, which works
439 * just fine with "ll". */
440 #undef PRId64
441 #define PRId64 "lld"
442 #undef PRId64
443 #define PRId64 "lld"
444 #undef PRIdLEAST64
445 #define PRIdLEAST64 "lld"
446 #undef PRIdFAST64
447 #define PRIdFAST64 "lld"
448 #undef PRIdMAX
449 #define PRIdMAX "lld"
450 #undef PRIi64
451 #define PRIi64 "lli"
452 #undef PRIiLEAST64
453 #define PRIiLEAST64 "lli"
454 #undef PRIiFAST64
455 #define PRIiFAST64 "lli"
456 #undef PRIiMAX
457 #define PRIiMAX "lli"
458 #undef PRIo64
459 #define PRIo64 "llo"
460 #undef PRIoLEAST64
461 #define PRIoLEAST64 "llo"
462 #undef PRIoFAST64
463 #define PRIoFAST64 "llo"
464 #undef PRIoMAX
465 #define PRIoMAX "llo"
466 #undef PRIu64
467 #define PRIu64 "llu"
468 #undef PRIuLEAST64
469 #define PRIuLEAST64 "llu"
470 #undef PRIuFAST64
471 #define PRIuFAST64 "llu"
472 #undef PRIuMAX
473 #define PRIuMAX "llu"
474 #undef PRIx64
475 #define PRIx64 "llx"
476 #undef PRIxLEAST64
477 #define PRIxLEAST64 "llx"
478 #undef PRIxFAST64
479 #define PRIxFAST64 "llx"
480 #undef PRIxMAX
481 #define PRIxMAX "llx"
482 #undef PRIX64
483 #define PRIX64 "llX"
484 #undef PRIXLEAST64
485 #define PRIXLEAST64 "llX"
486 #undef PRIXFAST64
487 #define PRIXFAST64 "llX"
488 #undef PRIXMAX
489 #define PRIXMAX "llX"
490
491 #ifdef _WIN64
492 # undef PRIdPTR
493 # define PRIdPTR "lld"
494 # undef PRIiPTR
495 # define PRIiPTR "lli"
496 # undef PRIoPTR
497 # define PRIoPTR "llo"
498 # undef PRIuPTR
499 # define PRIuPTR "llu"
500 # undef PRIxPTR
501 # define PRIxPTR "llx"
502 # undef PRIXPTR
503 # define PRIXPTR "llX"
504 #endif /* _WIN64 */
505
506 #endif /** WINDOWS_H_ @}*/