windows: Wrap most Winsock2 Posix functions to set errno
[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 * shutdown(2) setting errno
277 */
278 #define shutdown windows_shutdown
279 int windows_shutdown(int sockfd, int how);
280
281 /**
282 * accept(2) setting errno
283 */
284 #define accept windows_accept
285 int windows_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
286
287 /**
288 * bind(2) setting errno
289 */
290 #define bind windows_bind
291 int windows_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
292
293 /**
294 * connect(2) setting errno
295 */
296 #define connect windows_connect
297 int windows_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
298
299 /**
300 * getsockname(2) setting errno
301 */
302 #define getsockname windows_getsockname
303 int windows_getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
304
305 /**
306 * getsockopt(2) setting errno
307 */
308 #define getsockopt windows_getsockopt
309 int windows_getsockopt(int sockfd, int level, int optname,
310 void *optval, socklen_t *optlen);
311
312 /**
313 * setsockopt(2) setting errno
314 */
315 #define setsockopt windows_setsockopt
316 int windows_setsockopt(int sockfd, int level, int optname,
317 const void *optval, socklen_t optlen);
318
319 /**
320 * socket(2) setting errno
321 */
322 #define socket windows_socket
323 int windows_socket(int domain, int type, int protocol);
324
325 /**
326 * select(2) setting errno
327 */
328 #define select windows_select
329 int windows_select(int nfds, fd_set *readfds, fd_set *writefds,
330 fd_set *exceptfds, struct timeval *timeout);
331
332 /**
333 * close(2) working for file handles and Winsock sockets
334 */
335 #define close windows_close
336 int windows_close(int fd);
337
338 /**
339 * recv(2) with support for MSG_DONTWAIT
340 */
341 #define recv windows_recv
342 ssize_t windows_recv(int sockfd, void *buf, size_t len, int flags);
343
344 /**
345 * recvfrom(2) with support for MSG_DONTWAIT
346 */
347 #define recvfrom windows_recvfrom
348 ssize_t windows_recvfrom(int sockfd, void *buf, size_t len, int flags,
349 struct sockaddr *src_addr, socklen_t *addrlen);
350
351 /**
352 * recvfrom(2) with support for MSG_DONTWAIT
353 */
354 #define send windows_send
355 ssize_t windows_send(int sockfd, const void *buf, size_t len, int flags);
356
357 /**
358 * recvfrom(2) with support for MSG_DONTWAIT
359 */
360 #define sendto windows_send
361 ssize_t windows_sendto(int sockfd, const void *buf, size_t len, int flags,
362 const struct sockaddr *dest_addr, socklen_t addrlen);
363
364 /**
365 * MinGW does provide extended errno values. Windows itself knowns them
366 * for POSIX compatibility; we define them as well.
367 */
368 #ifndef EADDRINUSE
369 #define EADDRINUSE 100
370 #endif
371 #ifndef EADDRNOTAVAIL
372 #define EADDRNOTAVAIL 101
373 #endif
374 #ifndef EAFNOSUPPORT
375 #define EAFNOSUPPORT 102
376 #endif
377 #ifndef EALREADY
378 #define EALREADY 103
379 #endif
380 #ifndef EBADMSG
381 #define EBADMSG 104
382 #endif
383 #ifndef ECANCELED
384 #define ECANCELED 105
385 #endif
386 #ifndef ECONNABORTED
387 #define ECONNABORTED 106
388 #endif
389 #ifndef ECONNREFUSED
390 #define ECONNREFUSED 107
391 #endif
392 #ifndef ECONNRESET
393 #define ECONNRESET 108
394 #endif
395 #ifndef EDESTADDRREQ
396 #define EDESTADDRREQ 109
397 #endif
398 #ifndef EHOSTUNREACH
399 #define EHOSTUNREACH 110
400 #endif
401 #ifndef EIDRM
402 #define EIDRM 111
403 #endif
404 #ifndef EINPROGRESS
405 #define EINPROGRESS 112
406 #endif
407 #ifndef EISCONN
408 #define EISCONN 113
409 #endif
410 #ifndef ELOOP
411 #define ELOOP 114
412 #endif
413 #ifndef EMSGSIZE
414 #define EMSGSIZE 115
415 #endif
416 #ifndef ENETDOWN
417 #define ENETDOWN 116
418 #endif
419 #ifndef ENETRESET
420 #define ENETRESET 117
421 #endif
422 #ifndef ENETUNREACH
423 #define ENETUNREACH 118
424 #endif
425 #ifndef ENOBUFS
426 #define ENOBUFS 119
427 #endif
428 #ifndef ENODATA
429 #define ENODATA 120
430 #endif
431 #ifndef ENOLINK
432 #define ENOLINK 121
433 #endif
434 #ifndef ENOMSG
435 #define ENOMSG 122
436 #endif
437 #ifndef ENOPROTOOPT
438 #define ENOPROTOOPT 123
439 #endif
440 #ifndef ENOSR
441 #define ENOSR 124
442 #endif
443 #ifndef ENOSTR
444 #define ENOSTR 125
445 #endif
446 #ifndef ENOTCONN
447 #define ENOTCONN 126
448 #endif
449 #ifndef ENOTRECOVERABLE
450 #define ENOTRECOVERABLE 127
451 #endif
452 #ifndef ENOTSOCK
453 #define ENOTSOCK 128
454 #endif
455 #ifndef ENOTSUP
456 #define ENOTSUP 129
457 #endif
458 #ifndef EOPNOTSUPP
459 #define EOPNOTSUPP 130
460 #endif
461 #ifndef EOTHER
462 #define EOTHER 131
463 #endif
464 #ifndef EOVERFLOW
465 #define EOVERFLOW 132
466 #endif
467 #ifndef EOWNERDEAD
468 #define EOWNERDEAD 133
469 #endif
470 #ifndef EPROTO
471 #define EPROTO 134
472 #endif
473 #ifndef EPROTONOSUPPORT
474 #define EPROTONOSUPPORT 135
475 #endif
476 #ifndef EPROTOTYPE
477 #define EPROTOTYPE 136
478 #endif
479 #ifndef ETIME
480 #define ETIME 137
481 #endif
482 #ifndef ETIMEDOUT
483 #define ETIMEDOUT 138
484 #endif
485 #ifndef ETXTBSY
486 #define ETXTBSY 139
487 #endif
488 #ifndef EWOULDBLOCK
489 #define EWOULDBLOCK 140
490 #endif
491
492
493 /* Windows does not support "ll" format printf length modifiers. Mingw
494 * therefore maps these to the Windows specific I64 length modifier. That
495 * won't work for us, as we use our own printf backend on Windows, which works
496 * just fine with "ll". */
497 #undef PRId64
498 #define PRId64 "lld"
499 #undef PRId64
500 #define PRId64 "lld"
501 #undef PRIdLEAST64
502 #define PRIdLEAST64 "lld"
503 #undef PRIdFAST64
504 #define PRIdFAST64 "lld"
505 #undef PRIdMAX
506 #define PRIdMAX "lld"
507 #undef PRIi64
508 #define PRIi64 "lli"
509 #undef PRIiLEAST64
510 #define PRIiLEAST64 "lli"
511 #undef PRIiFAST64
512 #define PRIiFAST64 "lli"
513 #undef PRIiMAX
514 #define PRIiMAX "lli"
515 #undef PRIo64
516 #define PRIo64 "llo"
517 #undef PRIoLEAST64
518 #define PRIoLEAST64 "llo"
519 #undef PRIoFAST64
520 #define PRIoFAST64 "llo"
521 #undef PRIoMAX
522 #define PRIoMAX "llo"
523 #undef PRIu64
524 #define PRIu64 "llu"
525 #undef PRIuLEAST64
526 #define PRIuLEAST64 "llu"
527 #undef PRIuFAST64
528 #define PRIuFAST64 "llu"
529 #undef PRIuMAX
530 #define PRIuMAX "llu"
531 #undef PRIx64
532 #define PRIx64 "llx"
533 #undef PRIxLEAST64
534 #define PRIxLEAST64 "llx"
535 #undef PRIxFAST64
536 #define PRIxFAST64 "llx"
537 #undef PRIxMAX
538 #define PRIxMAX "llx"
539 #undef PRIX64
540 #define PRIX64 "llX"
541 #undef PRIXLEAST64
542 #define PRIXLEAST64 "llX"
543 #undef PRIXFAST64
544 #define PRIXFAST64 "llX"
545 #undef PRIXMAX
546 #define PRIXMAX "llX"
547
548 #ifdef _WIN64
549 # undef PRIdPTR
550 # define PRIdPTR "lld"
551 # undef PRIiPTR
552 # define PRIiPTR "lli"
553 # undef PRIoPTR
554 # define PRIoPTR "llo"
555 # undef PRIuPTR
556 # define PRIuPTR "llu"
557 # undef PRIxPTR
558 # define PRIxPTR "llx"
559 # undef PRIXPTR
560 # define PRIXPTR "llX"
561 #endif /* _WIN64 */
562
563 #endif /** WINDOWS_H_ @}*/