sockcompat.h 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #ifndef __SOCKCOMPAT_H
  31. #define __SOCKCOMPAT_H
  32. #ifndef _WIN32
  33. /* For POSIX systems we use the standard BSD socket API. */
  34. #include <unistd.h>
  35. #include <sys/socket.h>
  36. #include <sys/select.h>
  37. #include <sys/un.h>
  38. #include <netinet/in.h>
  39. #include <netinet/tcp.h>
  40. #include <arpa/inet.h>
  41. #include <netdb.h>
  42. #include <poll.h>
  43. #else
  44. /* For Windows we use winsock. */
  45. #undef _WIN32_WINNT
  46. #define _WIN32_WINNT 0x0600 /* To get WSAPoll etc. */
  47. #include <winsock2.h>
  48. #include <ws2tcpip.h>
  49. #include <stddef.h>
  50. #include <errno.h>
  51. #ifdef _MSC_VER
  52. typedef long long ssize_t;
  53. #endif
  54. /* Emulate the parts of the BSD socket API that we need (override the winsock signatures). */
  55. int win32_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
  56. const char *win32_gai_strerror(int errcode);
  57. void win32_freeaddrinfo(struct addrinfo *res);
  58. SOCKET win32_socket(int domain, int type, int protocol);
  59. int win32_ioctl(SOCKET fd, unsigned long request, unsigned long *argp);
  60. int win32_bind(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen);
  61. int win32_connect(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen);
  62. int win32_getsockopt(SOCKET sockfd, int level, int optname, void *optval, socklen_t *optlen);
  63. int win32_setsockopt(SOCKET sockfd, int level, int optname, const void *optval, socklen_t optlen);
  64. int win32_close(SOCKET fd);
  65. ssize_t win32_recv(SOCKET sockfd, void *buf, size_t len, int flags);
  66. ssize_t win32_send(SOCKET sockfd, const void *buf, size_t len, int flags);
  67. typedef ULONG nfds_t;
  68. int win32_poll(struct pollfd *fds, nfds_t nfds, int timeout);
  69. #ifndef REDIS_SOCKCOMPAT_IMPLEMENTATION
  70. #define getaddrinfo(node, service, hints, res) win32_getaddrinfo(node, service, hints, res)
  71. #undef gai_strerror
  72. #define gai_strerror(errcode) win32_gai_strerror(errcode)
  73. #define freeaddrinfo(res) win32_freeaddrinfo(res)
  74. #define socket(domain, type, protocol) win32_socket(domain, type, protocol)
  75. #define ioctl(fd, request, argp) win32_ioctl(fd, request, argp)
  76. #define bind(sockfd, addr, addrlen) win32_bind(sockfd, addr, addrlen)
  77. #define connect(sockfd, addr, addrlen) win32_connect(sockfd, addr, addrlen)
  78. #define getsockopt(sockfd, level, optname, optval, optlen) win32_getsockopt(sockfd, level, optname, optval, optlen)
  79. #define setsockopt(sockfd, level, optname, optval, optlen) win32_setsockopt(sockfd, level, optname, optval, optlen)
  80. #define close(fd) win32_close(fd)
  81. #define recv(sockfd, buf, len, flags) win32_recv(sockfd, buf, len, flags)
  82. #define send(sockfd, buf, len, flags) win32_send(sockfd, buf, len, flags)
  83. #define poll(fds, nfds, timeout) win32_poll(fds, nfds, timeout)
  84. #endif /* REDIS_SOCKCOMPAT_IMPLEMENTATION */
  85. #endif /* _WIN32 */
  86. #endif /* __SOCKCOMPAT_H */