w32sock.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* w32sock.h --- internal auxilliary functions for Windows socket functions
  2. Copyright (C) 2008-2012 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 <http://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. #include "msvc-nothrow.h"
  21. #define FD_TO_SOCKET(fd) ((SOCKET) _get_osfhandle ((fd)))
  22. #define SOCKET_TO_FD(fh) (_open_osfhandle ((long) (fh), O_RDWR | O_BINARY))
  23. static inline void
  24. set_winsock_errno (void)
  25. {
  26. int err = WSAGetLastError ();
  27. /* Map some WSAE* errors to the runtime library's error codes. */
  28. switch (err)
  29. {
  30. case WSA_INVALID_HANDLE:
  31. errno = EBADF;
  32. break;
  33. case WSA_NOT_ENOUGH_MEMORY:
  34. errno = ENOMEM;
  35. break;
  36. case WSA_INVALID_PARAMETER:
  37. errno = EINVAL;
  38. break;
  39. case WSAENAMETOOLONG:
  40. errno = ENAMETOOLONG;
  41. break;
  42. case WSAENOTEMPTY:
  43. errno = ENOTEMPTY;
  44. break;
  45. case WSAEWOULDBLOCK:
  46. errno = EWOULDBLOCK;
  47. break;
  48. case WSAEINPROGRESS:
  49. errno = EINPROGRESS;
  50. break;
  51. case WSAEALREADY:
  52. errno = EALREADY;
  53. break;
  54. case WSAENOTSOCK:
  55. errno = ENOTSOCK;
  56. break;
  57. case WSAEDESTADDRREQ:
  58. errno = EDESTADDRREQ;
  59. break;
  60. case WSAEMSGSIZE:
  61. errno = EMSGSIZE;
  62. break;
  63. case WSAEPROTOTYPE:
  64. errno = EPROTOTYPE;
  65. break;
  66. case WSAENOPROTOOPT:
  67. errno = ENOPROTOOPT;
  68. break;
  69. case WSAEPROTONOSUPPORT:
  70. errno = EPROTONOSUPPORT;
  71. break;
  72. case WSAEOPNOTSUPP:
  73. errno = EOPNOTSUPP;
  74. break;
  75. case WSAEAFNOSUPPORT:
  76. errno = EAFNOSUPPORT;
  77. break;
  78. case WSAEADDRINUSE:
  79. errno = EADDRINUSE;
  80. break;
  81. case WSAEADDRNOTAVAIL:
  82. errno = EADDRNOTAVAIL;
  83. break;
  84. case WSAENETDOWN:
  85. errno = ENETDOWN;
  86. break;
  87. case WSAENETUNREACH:
  88. errno = ENETUNREACH;
  89. break;
  90. case WSAENETRESET:
  91. errno = ENETRESET;
  92. break;
  93. case WSAECONNABORTED:
  94. errno = ECONNABORTED;
  95. break;
  96. case WSAECONNRESET:
  97. errno = ECONNRESET;
  98. break;
  99. case WSAENOBUFS:
  100. errno = ENOBUFS;
  101. break;
  102. case WSAEISCONN:
  103. errno = EISCONN;
  104. break;
  105. case WSAENOTCONN:
  106. errno = ENOTCONN;
  107. break;
  108. case WSAETIMEDOUT:
  109. errno = ETIMEDOUT;
  110. break;
  111. case WSAECONNREFUSED:
  112. errno = ECONNREFUSED;
  113. break;
  114. case WSAELOOP:
  115. errno = ELOOP;
  116. break;
  117. case WSAEHOSTUNREACH:
  118. errno = EHOSTUNREACH;
  119. break;
  120. default:
  121. errno = (err > 10000 && err < 10025) ? err - 10000 : err;
  122. break;
  123. }
  124. }