2
0

sockcompat.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. #define REDIS_SOCKCOMPAT_IMPLEMENTATION
  31. #include "sockcompat.h"
  32. #ifdef _WIN32
  33. static int _wsaErrorToErrno(int err) {
  34. switch (err) {
  35. case WSAEWOULDBLOCK:
  36. return EWOULDBLOCK;
  37. case WSAEINPROGRESS:
  38. return EINPROGRESS;
  39. case WSAEALREADY:
  40. return EALREADY;
  41. case WSAENOTSOCK:
  42. return ENOTSOCK;
  43. case WSAEDESTADDRREQ:
  44. return EDESTADDRREQ;
  45. case WSAEMSGSIZE:
  46. return EMSGSIZE;
  47. case WSAEPROTOTYPE:
  48. return EPROTOTYPE;
  49. case WSAENOPROTOOPT:
  50. return ENOPROTOOPT;
  51. case WSAEPROTONOSUPPORT:
  52. return EPROTONOSUPPORT;
  53. case WSAEOPNOTSUPP:
  54. return EOPNOTSUPP;
  55. case WSAEAFNOSUPPORT:
  56. return EAFNOSUPPORT;
  57. case WSAEADDRINUSE:
  58. return EADDRINUSE;
  59. case WSAEADDRNOTAVAIL:
  60. return EADDRNOTAVAIL;
  61. case WSAENETDOWN:
  62. return ENETDOWN;
  63. case WSAENETUNREACH:
  64. return ENETUNREACH;
  65. case WSAENETRESET:
  66. return ENETRESET;
  67. case WSAECONNABORTED:
  68. return ECONNABORTED;
  69. case WSAECONNRESET:
  70. return ECONNRESET;
  71. case WSAENOBUFS:
  72. return ENOBUFS;
  73. case WSAEISCONN:
  74. return EISCONN;
  75. case WSAENOTCONN:
  76. return ENOTCONN;
  77. case WSAETIMEDOUT:
  78. return ETIMEDOUT;
  79. case WSAECONNREFUSED:
  80. return ECONNREFUSED;
  81. case WSAELOOP:
  82. return ELOOP;
  83. case WSAENAMETOOLONG:
  84. return ENAMETOOLONG;
  85. case WSAEHOSTUNREACH:
  86. return EHOSTUNREACH;
  87. case WSAENOTEMPTY:
  88. return ENOTEMPTY;
  89. default:
  90. /* We just return a generic I/O error if we could not find a relevant error. */
  91. return EIO;
  92. }
  93. }
  94. static void _updateErrno(int success) {
  95. errno = success ? 0 : _wsaErrorToErrno(WSAGetLastError());
  96. }
  97. static int _initWinsock() {
  98. static int s_initialized = 0;
  99. if (!s_initialized) {
  100. static WSADATA wsadata;
  101. int err = WSAStartup(MAKEWORD(2,2), &wsadata);
  102. if (err != 0) {
  103. errno = _wsaErrorToErrno(err);
  104. return 0;
  105. }
  106. s_initialized = 1;
  107. }
  108. return 1;
  109. }
  110. int win32_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) {
  111. /* Note: This function is likely to be called before other functions, so run init here. */
  112. if (!_initWinsock()) {
  113. return EAI_FAIL;
  114. }
  115. switch (getaddrinfo(node, service, hints, res)) {
  116. case 0: return 0;
  117. case WSATRY_AGAIN: return EAI_AGAIN;
  118. case WSAEINVAL: return EAI_BADFLAGS;
  119. case WSAEAFNOSUPPORT: return EAI_FAMILY;
  120. case WSA_NOT_ENOUGH_MEMORY: return EAI_MEMORY;
  121. case WSAHOST_NOT_FOUND: return EAI_NONAME;
  122. case WSATYPE_NOT_FOUND: return EAI_SERVICE;
  123. case WSAESOCKTNOSUPPORT: return EAI_SOCKTYPE;
  124. default: return EAI_FAIL; /* Including WSANO_RECOVERY */
  125. }
  126. }
  127. const char *win32_gai_strerror(int errcode) {
  128. switch (errcode) {
  129. case 0: errcode = 0; break;
  130. case EAI_AGAIN: errcode = WSATRY_AGAIN; break;
  131. case EAI_BADFLAGS: errcode = WSAEINVAL; break;
  132. case EAI_FAMILY: errcode = WSAEAFNOSUPPORT; break;
  133. case EAI_MEMORY: errcode = WSA_NOT_ENOUGH_MEMORY; break;
  134. case EAI_NONAME: errcode = WSAHOST_NOT_FOUND; break;
  135. case EAI_SERVICE: errcode = WSATYPE_NOT_FOUND; break;
  136. case EAI_SOCKTYPE: errcode = WSAESOCKTNOSUPPORT; break;
  137. default: errcode = WSANO_RECOVERY; break; /* Including EAI_FAIL */
  138. }
  139. return gai_strerror(errcode);
  140. }
  141. void win32_freeaddrinfo(struct addrinfo *res) {
  142. freeaddrinfo(res);
  143. }
  144. SOCKET win32_socket(int domain, int type, int protocol) {
  145. SOCKET s;
  146. /* Note: This function is likely to be called before other functions, so run init here. */
  147. if (!_initWinsock()) {
  148. return INVALID_SOCKET;
  149. }
  150. _updateErrno((s = socket(domain, type, protocol)) != INVALID_SOCKET);
  151. return s;
  152. }
  153. int win32_ioctl(SOCKET fd, unsigned long request, unsigned long *argp) {
  154. int ret = ioctlsocket(fd, (long)request, argp);
  155. _updateErrno(ret != SOCKET_ERROR);
  156. return ret != SOCKET_ERROR ? ret : -1;
  157. }
  158. int win32_bind(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen) {
  159. int ret = bind(sockfd, addr, addrlen);
  160. _updateErrno(ret != SOCKET_ERROR);
  161. return ret != SOCKET_ERROR ? ret : -1;
  162. }
  163. int win32_connect(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen) {
  164. int ret = connect(sockfd, addr, addrlen);
  165. _updateErrno(ret != SOCKET_ERROR);
  166. /* For Winsock connect(), the WSAEWOULDBLOCK error means the same thing as
  167. * EINPROGRESS for POSIX connect(), so we do that translation to keep POSIX
  168. * logic consistent. */
  169. if (errno == EWOULDBLOCK) {
  170. errno = EINPROGRESS;
  171. }
  172. return ret != SOCKET_ERROR ? ret : -1;
  173. }
  174. int win32_getsockopt(SOCKET sockfd, int level, int optname, void *optval, socklen_t *optlen) {
  175. int ret = 0;
  176. if ((level == SOL_SOCKET) && ((optname == SO_RCVTIMEO) || (optname == SO_SNDTIMEO))) {
  177. if (*optlen >= sizeof (struct timeval)) {
  178. struct timeval *tv = optval;
  179. DWORD timeout = 0;
  180. socklen_t dwlen = 0;
  181. ret = getsockopt(sockfd, level, optname, (char *)&timeout, &dwlen);
  182. tv->tv_sec = timeout / 1000;
  183. tv->tv_usec = (timeout * 1000) % 1000000;
  184. } else {
  185. ret = WSAEFAULT;
  186. }
  187. *optlen = sizeof (struct timeval);
  188. } else {
  189. ret = getsockopt(sockfd, level, optname, (char*)optval, optlen);
  190. }
  191. _updateErrno(ret != SOCKET_ERROR);
  192. return ret != SOCKET_ERROR ? ret : -1;
  193. }
  194. int win32_setsockopt(SOCKET sockfd, int level, int optname, const void *optval, socklen_t optlen) {
  195. int ret = 0;
  196. if ((level == SOL_SOCKET) && ((optname == SO_RCVTIMEO) || (optname == SO_SNDTIMEO))) {
  197. struct timeval *tv = optval;
  198. DWORD timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
  199. ret = setsockopt(sockfd, level, optname, (const char*)&timeout, sizeof(DWORD));
  200. } else {
  201. ret = setsockopt(sockfd, level, optname, (const char*)optval, optlen);
  202. }
  203. _updateErrno(ret != SOCKET_ERROR);
  204. return ret != SOCKET_ERROR ? ret : -1;
  205. }
  206. int win32_close(SOCKET fd) {
  207. int ret = closesocket(fd);
  208. _updateErrno(ret != SOCKET_ERROR);
  209. return ret != SOCKET_ERROR ? ret : -1;
  210. }
  211. ssize_t win32_recv(SOCKET sockfd, void *buf, size_t len, int flags) {
  212. int ret = recv(sockfd, (char*)buf, (int)len, flags);
  213. _updateErrno(ret != SOCKET_ERROR);
  214. return ret != SOCKET_ERROR ? ret : -1;
  215. }
  216. ssize_t win32_send(SOCKET sockfd, const void *buf, size_t len, int flags) {
  217. int ret = send(sockfd, (const char*)buf, (int)len, flags);
  218. _updateErrno(ret != SOCKET_ERROR);
  219. return ret != SOCKET_ERROR ? ret : -1;
  220. }
  221. int win32_poll(struct pollfd *fds, nfds_t nfds, int timeout) {
  222. int ret = WSAPoll(fds, nfds, timeout);
  223. _updateErrno(ret != SOCKET_ERROR);
  224. return ret != SOCKET_ERROR ? ret : -1;
  225. }
  226. #endif /* _WIN32 */