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