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