1 | /* |
2 | * Copyright (c) 2019, Marcus Geelnard <m at bitsnbites dot eu> |
3 | * |
4 | * All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions are met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright notice, |
10 | * this list of conditions and the following disclaimer. |
11 | * * Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
14 | * * Neither the name of Redis nor the names of its contributors may be used |
15 | * to endorse or promote products derived from this software without |
16 | * specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
28 | * POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
30 | |
31 | #ifndef __SOCKCOMPAT_H |
32 | #define __SOCKCOMPAT_H |
33 | |
34 | #ifndef _WIN32 |
35 | /* For POSIX systems we use the standard BSD socket API. */ |
36 | #include <unistd.h> |
37 | #include <sys/socket.h> |
38 | #include <sys/select.h> |
39 | #include <sys/un.h> |
40 | #include <netinet/in.h> |
41 | #include <netinet/tcp.h> |
42 | #include <arpa/inet.h> |
43 | #include <netdb.h> |
44 | #include <poll.h> |
45 | #else |
46 | /* For Windows we use winsock. */ |
47 | #undef _WIN32_WINNT |
48 | #define _WIN32_WINNT 0x0600 /* To get WSAPoll etc. */ |
49 | #include <winsock2.h> |
50 | #include <ws2tcpip.h> |
51 | #include <stddef.h> |
52 | #include <errno.h> |
53 | |
54 | #ifdef _MSC_VER |
55 | typedef long long ssize_t; |
56 | #endif |
57 | |
58 | /* Emulate the parts of the BSD socket API that we need (override the winsock signatures). */ |
59 | int win32_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res); |
60 | const char *win32_gai_strerror(int errcode); |
61 | void win32_freeaddrinfo(struct addrinfo *res); |
62 | SOCKET win32_socket(int domain, int type, int protocol); |
63 | int win32_ioctl(SOCKET fd, unsigned long request, unsigned long *argp); |
64 | int win32_bind(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen); |
65 | int win32_connect(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen); |
66 | int win32_getsockopt(SOCKET sockfd, int level, int optname, void *optval, socklen_t *optlen); |
67 | int win32_setsockopt(SOCKET sockfd, int level, int optname, const void *optval, socklen_t optlen); |
68 | int win32_close(SOCKET fd); |
69 | ssize_t win32_recv(SOCKET sockfd, void *buf, size_t len, int flags); |
70 | ssize_t win32_send(SOCKET sockfd, const void *buf, size_t len, int flags); |
71 | typedef ULONG nfds_t; |
72 | int win32_poll(struct pollfd *fds, nfds_t nfds, int timeout); |
73 | |
74 | #ifndef REDIS_SOCKCOMPAT_IMPLEMENTATION |
75 | #define getaddrinfo(node, service, hints, res) win32_getaddrinfo(node, service, hints, res) |
76 | #undef gai_strerror |
77 | #define gai_strerror(errcode) win32_gai_strerror(errcode) |
78 | #define freeaddrinfo(res) win32_freeaddrinfo(res) |
79 | #define socket(domain, type, protocol) win32_socket(domain, type, protocol) |
80 | #define ioctl(fd, request, argp) win32_ioctl(fd, request, argp) |
81 | #define bind(sockfd, addr, addrlen) win32_bind(sockfd, addr, addrlen) |
82 | #define connect(sockfd, addr, addrlen) win32_connect(sockfd, addr, addrlen) |
83 | #define getsockopt(sockfd, level, optname, optval, optlen) win32_getsockopt(sockfd, level, optname, optval, optlen) |
84 | #define setsockopt(sockfd, level, optname, optval, optlen) win32_setsockopt(sockfd, level, optname, optval, optlen) |
85 | #define close(fd) win32_close(fd) |
86 | #define recv(sockfd, buf, len, flags) win32_recv(sockfd, buf, len, flags) |
87 | #define send(sockfd, buf, len, flags) win32_send(sockfd, buf, len, flags) |
88 | #define poll(fds, nfds, timeout) win32_poll(fds, nfds, timeout) |
89 | #endif /* REDIS_SOCKCOMPAT_IMPLEMENTATION */ |
90 | #endif /* _WIN32 */ |
91 | |
92 | #endif /* __SOCKCOMPAT_H */ |
93 | |