windows: Provide a write(2) wrapper that uses send(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 /**
375 * write(2) working on files and sockets
376 */
377 #define write windows_write
378 ssize_t windows_write(int fd, void *buf, size_t count);
379
380 #if _WIN32_WINNT < 0x0600
381 /**
382 * Define pollfd and flags on our own if not specified
383 */
384 struct pollfd {
385 SOCKET fd;
386 short events;
387 short revents;
388 };
389 enum {
390 POLLERR = 0x0001,
391 POLLHUP = 0x0002,
392 POLLNVAL = 0x0004,
393 POLLWRNORM = 0x0010,
394 POLLWRBAND = 0x0020,
395 POLLPRI = 0x0400,
396 POLLRDNORM = 0x0100,
397 POLLRDBAND = 0x0200,
398 POLLIN = POLLRDNORM | POLLRDBAND,
399 POLLOUT = POLLWRNORM,
400 };
401 #endif /* _WIN32_WINNT < 0x0600 */
402
403 /**
404 * poll(2), implemented using Winsock2 WSAPoll()
405 */
406 int poll(struct pollfd *fds, int nfds, int timeout);
407
408 /**
409 * Declaration missing on older WinGW
410 */
411 _CRTIMP errno_t strerror_s(char *buf, size_t size, int errnum);
412
413 /**
414 * strerror_s, but supporting POSIX compatibility errno >= 100
415 */
416 #define strerror_s strerror_s_extended
417 int strerror_s_extended(char *buf, size_t buflen, int errnum);
418
419 /**
420 * strerror_r(2) replacement, XSI variant
421 */
422 static inline int strerror_r(int errnum, char *buf, size_t buflen)
423 {
424 return strerror_s(buf, buflen, errnum);
425 }
426 #define HAVE_STRERROR_R /* but not STRERROR_R_CHAR_P */
427
428 /**
429 * MinGW does provide extended errno values. Windows itself knowns them
430 * for POSIX compatibility; we define them as well.
431 */
432 #ifndef EADDRINUSE
433 #define EADDRINUSE 100
434 #endif
435 #ifndef EADDRNOTAVAIL
436 #define EADDRNOTAVAIL 101
437 #endif
438 #ifndef EAFNOSUPPORT
439 #define EAFNOSUPPORT 102
440 #endif
441 #ifndef EALREADY
442 #define EALREADY 103
443 #endif
444 #ifndef EBADMSG
445 #define EBADMSG 104
446 #endif
447 #ifndef ECANCELED
448 #define ECANCELED 105
449 #endif
450 #ifndef ECONNABORTED
451 #define ECONNABORTED 106
452 #endif
453 #ifndef ECONNREFUSED
454 #define ECONNREFUSED 107
455 #endif
456 #ifndef ECONNRESET
457 #define ECONNRESET 108
458 #endif
459 #ifndef EDESTADDRREQ
460 #define EDESTADDRREQ 109
461 #endif
462 #ifndef EHOSTUNREACH
463 #define EHOSTUNREACH 110
464 #endif
465 #ifndef EIDRM
466 #define EIDRM 111
467 #endif
468 #ifndef EINPROGRESS
469 #define EINPROGRESS 112
470 #endif
471 #ifndef EISCONN
472 #define EISCONN 113
473 #endif
474 #ifndef ELOOP
475 #define ELOOP 114
476 #endif
477 #ifndef EMSGSIZE
478 #define EMSGSIZE 115
479 #endif
480 #ifndef ENETDOWN
481 #define ENETDOWN 116
482 #endif
483 #ifndef ENETRESET
484 #define ENETRESET 117
485 #endif
486 #ifndef ENETUNREACH
487 #define ENETUNREACH 118
488 #endif
489 #ifndef ENOBUFS
490 #define ENOBUFS 119
491 #endif
492 #ifndef ENODATA
493 #define ENODATA 120
494 #endif
495 #ifndef ENOLINK
496 #define ENOLINK 121
497 #endif
498 #ifndef ENOMSG
499 #define ENOMSG 122
500 #endif
501 #ifndef ENOPROTOOPT
502 #define ENOPROTOOPT 123
503 #endif
504 #ifndef ENOSR
505 #define ENOSR 124
506 #endif
507 #ifndef ENOSTR
508 #define ENOSTR 125
509 #endif
510 #ifndef ENOTCONN
511 #define ENOTCONN 126
512 #endif
513 #ifndef ENOTRECOVERABLE
514 #define ENOTRECOVERABLE 127
515 #endif
516 #ifndef ENOTSOCK
517 #define ENOTSOCK 128
518 #endif
519 #ifndef ENOTSUP
520 #define ENOTSUP 129
521 #endif
522 #ifndef EOPNOTSUPP
523 #define EOPNOTSUPP 130
524 #endif
525 #ifndef EOTHER
526 #define EOTHER 131
527 #endif
528 #ifndef EOVERFLOW
529 #define EOVERFLOW 132
530 #endif
531 #ifndef EOWNERDEAD
532 #define EOWNERDEAD 133
533 #endif
534 #ifndef EPROTO
535 #define EPROTO 134
536 #endif
537 #ifndef EPROTONOSUPPORT
538 #define EPROTONOSUPPORT 135
539 #endif
540 #ifndef EPROTOTYPE
541 #define EPROTOTYPE 136
542 #endif
543 #ifndef ETIME
544 #define ETIME 137
545 #endif
546 #ifndef ETIMEDOUT
547 #define ETIMEDOUT 138
548 #endif
549 #ifndef ETXTBSY
550 #define ETXTBSY 139
551 #endif
552 #ifndef EWOULDBLOCK
553 #define EWOULDBLOCK 140
554 #endif
555
556
557 /* Windows does not support "ll" format printf length modifiers. Mingw
558 * therefore maps these to the Windows specific I64 length modifier. That
559 * won't work for us, as we use our own printf backend on Windows, which works
560 * just fine with "ll". */
561 #undef PRId64
562 #define PRId64 "lld"
563 #undef PRId64
564 #define PRId64 "lld"
565 #undef PRIdLEAST64
566 #define PRIdLEAST64 "lld"
567 #undef PRIdFAST64
568 #define PRIdFAST64 "lld"
569 #undef PRIdMAX
570 #define PRIdMAX "lld"
571 #undef PRIi64
572 #define PRIi64 "lli"
573 #undef PRIiLEAST64
574 #define PRIiLEAST64 "lli"
575 #undef PRIiFAST64
576 #define PRIiFAST64 "lli"
577 #undef PRIiMAX
578 #define PRIiMAX "lli"
579 #undef PRIo64
580 #define PRIo64 "llo"
581 #undef PRIoLEAST64
582 #define PRIoLEAST64 "llo"
583 #undef PRIoFAST64
584 #define PRIoFAST64 "llo"
585 #undef PRIoMAX
586 #define PRIoMAX "llo"
587 #undef PRIu64
588 #define PRIu64 "llu"
589 #undef PRIuLEAST64
590 #define PRIuLEAST64 "llu"
591 #undef PRIuFAST64
592 #define PRIuFAST64 "llu"
593 #undef PRIuMAX
594 #define PRIuMAX "llu"
595 #undef PRIx64
596 #define PRIx64 "llx"
597 #undef PRIxLEAST64
598 #define PRIxLEAST64 "llx"
599 #undef PRIxFAST64
600 #define PRIxFAST64 "llx"
601 #undef PRIxMAX
602 #define PRIxMAX "llx"
603 #undef PRIX64
604 #define PRIX64 "llX"
605 #undef PRIXLEAST64
606 #define PRIXLEAST64 "llX"
607 #undef PRIXFAST64
608 #define PRIXFAST64 "llX"
609 #undef PRIXMAX
610 #define PRIXMAX "llX"
611
612 #ifdef _WIN64
613 # undef PRIdPTR
614 # define PRIdPTR "lld"
615 # undef PRIiPTR
616 # define PRIiPTR "lli"
617 # undef PRIoPTR
618 # define PRIoPTR "llo"
619 # undef PRIuPTR
620 # define PRIuPTR "llu"
621 # undef PRIxPTR
622 # define PRIxPTR "llx"
623 # undef PRIXPTR
624 # define PRIXPTR "llX"
625 #endif /* _WIN64 */
626
627 #endif /** WINDOWS_H_ @}*/