w32sock.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* w32sock.h --- internal auxiliary functions for Windows socket functions
  2. Copyright (C) 2008-2021 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Paolo Bonzini */
  14. #include <errno.h>
  15. /* Get O_RDWR and O_BINARY. */
  16. #include <fcntl.h>
  17. /* Get _open_osfhandle(). */
  18. #include <io.h>
  19. /* Get _get_osfhandle(). */
  20. #if GNULIB_MSVC_NOTHROW
  21. # include "msvc-nothrow.h"
  22. #else
  23. # include <io.h>
  24. #endif
  25. #define FD_TO_SOCKET(fd) ((SOCKET) _get_osfhandle ((fd)))
  26. #define SOCKET_TO_FD(fh) (_open_osfhandle ((intptr_t) (fh), O_RDWR | O_BINARY))
  27. static inline void
  28. set_winsock_errno (void)
  29. {
  30. int err = WSAGetLastError ();
  31. /* Map some WSAE* errors to the runtime library's error codes. */
  32. switch (err)
  33. {
  34. case WSA_INVALID_HANDLE:
  35. errno = EBADF;
  36. break;
  37. case WSA_NOT_ENOUGH_MEMORY:
  38. errno = ENOMEM;
  39. break;
  40. case WSA_INVALID_PARAMETER:
  41. errno = EINVAL;
  42. break;
  43. case WSAENAMETOOLONG:
  44. errno = ENAMETOOLONG;
  45. break;
  46. case WSAENOTEMPTY:
  47. errno = ENOTEMPTY;
  48. break;
  49. case WSAEWOULDBLOCK:
  50. errno = EWOULDBLOCK;
  51. break;
  52. case WSAEINPROGRESS:
  53. errno = EINPROGRESS;
  54. break;
  55. case WSAEALREADY:
  56. errno = EALREADY;
  57. break;
  58. case WSAENOTSOCK:
  59. errno = ENOTSOCK;
  60. break;
  61. case WSAEDESTADDRREQ:
  62. errno = EDESTADDRREQ;
  63. break;
  64. case WSAEMSGSIZE:
  65. errno = EMSGSIZE;
  66. break;
  67. case WSAEPROTOTYPE:
  68. errno = EPROTOTYPE;
  69. break;
  70. case WSAENOPROTOOPT:
  71. errno = ENOPROTOOPT;
  72. break;
  73. case WSAEPROTONOSUPPORT:
  74. errno = EPROTONOSUPPORT;
  75. break;
  76. case WSAEOPNOTSUPP:
  77. errno = EOPNOTSUPP;
  78. break;
  79. case WSAEAFNOSUPPORT:
  80. errno = EAFNOSUPPORT;
  81. break;
  82. case WSAEADDRINUSE:
  83. errno = EADDRINUSE;
  84. break;
  85. case WSAEADDRNOTAVAIL:
  86. errno = EADDRNOTAVAIL;
  87. break;
  88. case WSAENETDOWN:
  89. errno = ENETDOWN;
  90. break;
  91. case WSAENETUNREACH:
  92. errno = ENETUNREACH;
  93. break;
  94. case WSAENETRESET:
  95. errno = ENETRESET;
  96. break;
  97. case WSAECONNABORTED:
  98. errno = ECONNABORTED;
  99. break;
  100. case WSAECONNRESET:
  101. errno = ECONNRESET;
  102. break;
  103. case WSAENOBUFS:
  104. errno = ENOBUFS;
  105. break;
  106. case WSAEISCONN:
  107. errno = EISCONN;
  108. break;
  109. case WSAENOTCONN:
  110. errno = ENOTCONN;
  111. break;
  112. case WSAETIMEDOUT:
  113. errno = ETIMEDOUT;
  114. break;
  115. case WSAECONNREFUSED:
  116. errno = ECONNREFUSED;
  117. break;
  118. case WSAELOOP:
  119. errno = ELOOP;
  120. break;
  121. case WSAEHOSTUNREACH:
  122. errno = EHOSTUNREACH;
  123. break;
  124. default:
  125. errno = (err > 10000 && err < 10025) ? err - 10000 : err;
  126. break;
  127. }
  128. }