Annotation of embedaddon/strongswan/src/libstrongswan/utils/compat/windows.h, revision 1.1.1.1

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>