windows: Extend strerror_r/s by extended POSIX errno strings
[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 * strerror_s, but supporting POSIX compatiblity errno >= 100
366 */
367 #define strerror_s strerror_s_extended
368 int strerror_s_extended(char *buf, size_t buflen, int errnum);
369
370 /**
371 * strerror_r(2) replacement, XSI variant
372 */
373 static inline int strerror_r(int errnum, char *buf, size_t buflen)
374 {
375 return strerror_s(buf, buflen, errnum);
376 }
377 #define HAVE_STRERROR_R /* but not STRERROR_R_CHAR_P */
378
379 /**
380 * MinGW does provide extended errno values. Windows itself knowns them
381 * for POSIX compatibility; we define them as well.
382 */
383 #ifndef EADDRINUSE
384 #define EADDRINUSE 100
385 #endif
386 #ifndef EADDRNOTAVAIL
387 #define EADDRNOTAVAIL 101
388 #endif
389 #ifndef EAFNOSUPPORT
390 #define EAFNOSUPPORT 102
391 #endif
392 #ifndef EALREADY
393 #define EALREADY 103
394 #endif
395 #ifndef EBADMSG
396 #define EBADMSG 104
397 #endif
398 #ifndef ECANCELED
399 #define ECANCELED 105
400 #endif
401 #ifndef ECONNABORTED
402 #define ECONNABORTED 106
403 #endif
404 #ifndef ECONNREFUSED
405 #define ECONNREFUSED 107
406 #endif
407 #ifndef ECONNRESET
408 #define ECONNRESET 108
409 #endif
410 #ifndef EDESTADDRREQ
411 #define EDESTADDRREQ 109
412 #endif
413 #ifndef EHOSTUNREACH
414 #define EHOSTUNREACH 110
415 #endif
416 #ifndef EIDRM
417 #define EIDRM 111
418 #endif
419 #ifndef EINPROGRESS
420 #define EINPROGRESS 112
421 #endif
422 #ifndef EISCONN
423 #define EISCONN 113
424 #endif
425 #ifndef ELOOP
426 #define ELOOP 114
427 #endif
428 #ifndef EMSGSIZE
429 #define EMSGSIZE 115
430 #endif
431 #ifndef ENETDOWN
432 #define ENETDOWN 116
433 #endif
434 #ifndef ENETRESET
435 #define ENETRESET 117
436 #endif
437 #ifndef ENETUNREACH
438 #define ENETUNREACH 118
439 #endif
440 #ifndef ENOBUFS
441 #define ENOBUFS 119
442 #endif
443 #ifndef ENODATA
444 #define ENODATA 120
445 #endif
446 #ifndef ENOLINK
447 #define ENOLINK 121
448 #endif
449 #ifndef ENOMSG
450 #define ENOMSG 122
451 #endif
452 #ifndef ENOPROTOOPT
453 #define ENOPROTOOPT 123
454 #endif
455 #ifndef ENOSR
456 #define ENOSR 124
457 #endif
458 #ifndef ENOSTR
459 #define ENOSTR 125
460 #endif
461 #ifndef ENOTCONN
462 #define ENOTCONN 126
463 #endif
464 #ifndef ENOTRECOVERABLE
465 #define ENOTRECOVERABLE 127
466 #endif
467 #ifndef ENOTSOCK
468 #define ENOTSOCK 128
469 #endif
470 #ifndef ENOTSUP
471 #define ENOTSUP 129
472 #endif
473 #ifndef EOPNOTSUPP
474 #define EOPNOTSUPP 130
475 #endif
476 #ifndef EOTHER
477 #define EOTHER 131
478 #endif
479 #ifndef EOVERFLOW
480 #define EOVERFLOW 132
481 #endif
482 #ifndef EOWNERDEAD
483 #define EOWNERDEAD 133
484 #endif
485 #ifndef EPROTO
486 #define EPROTO 134
487 #endif
488 #ifndef EPROTONOSUPPORT
489 #define EPROTONOSUPPORT 135
490 #endif
491 #ifndef EPROTOTYPE
492 #define EPROTOTYPE 136
493 #endif
494 #ifndef ETIME
495 #define ETIME 137
496 #endif
497 #ifndef ETIMEDOUT
498 #define ETIMEDOUT 138
499 #endif
500 #ifndef ETXTBSY
501 #define ETXTBSY 139
502 #endif
503 #ifndef EWOULDBLOCK
504 #define EWOULDBLOCK 140
505 #endif
506
507
508 /* Windows does not support "ll" format printf length modifiers. Mingw
509 * therefore maps these to the Windows specific I64 length modifier. That
510 * won't work for us, as we use our own printf backend on Windows, which works
511 * just fine with "ll". */
512 #undef PRId64
513 #define PRId64 "lld"
514 #undef PRId64
515 #define PRId64 "lld"
516 #undef PRIdLEAST64
517 #define PRIdLEAST64 "lld"
518 #undef PRIdFAST64
519 #define PRIdFAST64 "lld"
520 #undef PRIdMAX
521 #define PRIdMAX "lld"
522 #undef PRIi64
523 #define PRIi64 "lli"
524 #undef PRIiLEAST64
525 #define PRIiLEAST64 "lli"
526 #undef PRIiFAST64
527 #define PRIiFAST64 "lli"
528 #undef PRIiMAX
529 #define PRIiMAX "lli"
530 #undef PRIo64
531 #define PRIo64 "llo"
532 #undef PRIoLEAST64
533 #define PRIoLEAST64 "llo"
534 #undef PRIoFAST64
535 #define PRIoFAST64 "llo"
536 #undef PRIoMAX
537 #define PRIoMAX "llo"
538 #undef PRIu64
539 #define PRIu64 "llu"
540 #undef PRIuLEAST64
541 #define PRIuLEAST64 "llu"
542 #undef PRIuFAST64
543 #define PRIuFAST64 "llu"
544 #undef PRIuMAX
545 #define PRIuMAX "llu"
546 #undef PRIx64
547 #define PRIx64 "llx"
548 #undef PRIxLEAST64
549 #define PRIxLEAST64 "llx"
550 #undef PRIxFAST64
551 #define PRIxFAST64 "llx"
552 #undef PRIxMAX
553 #define PRIxMAX "llx"
554 #undef PRIX64
555 #define PRIX64 "llX"
556 #undef PRIXLEAST64
557 #define PRIXLEAST64 "llX"
558 #undef PRIXFAST64
559 #define PRIXFAST64 "llX"
560 #undef PRIXMAX
561 #define PRIXMAX "llX"
562
563 #ifdef _WIN64
564 # undef PRIdPTR
565 # define PRIdPTR "lld"
566 # undef PRIiPTR
567 # define PRIiPTR "lli"
568 # undef PRIoPTR
569 # define PRIoPTR "llo"
570 # undef PRIuPTR
571 # define PRIuPTR "llu"
572 # undef PRIxPTR
573 # define PRIxPTR "llx"
574 # undef PRIXPTR
575 # define PRIXPTR "llX"
576 #endif /* _WIN64 */
577
578 #endif /** WINDOWS_H_ @}*/