sockets.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* sockets.c --- wrappers for Windows socket functions
  2. Copyright (C) 2008-2017 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 Simon Josefsson */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "sockets.h"
  17. #if WINDOWS_SOCKETS
  18. /* This includes winsock2.h on MinGW. */
  19. # include <sys/socket.h>
  20. # include "fd-hook.h"
  21. # include "msvc-nothrow.h"
  22. /* Get set_winsock_errno, FD_TO_SOCKET etc. */
  23. # include "w32sock.h"
  24. static int
  25. close_fd_maybe_socket (const struct fd_hook *remaining_list,
  26. gl_close_fn primary,
  27. int fd)
  28. {
  29. /* Note about multithread-safety: There is a race condition where, between
  30. our calls to closesocket() and the primary close(), some other thread
  31. could make system calls that allocate precisely the same HANDLE value
  32. as sock; then the primary close() would call CloseHandle() on it. */
  33. SOCKET sock;
  34. WSANETWORKEVENTS ev;
  35. /* Test whether fd refers to a socket. */
  36. sock = FD_TO_SOCKET (fd);
  37. ev.lNetworkEvents = 0xDEADBEEF;
  38. WSAEnumNetworkEvents (sock, NULL, &ev);
  39. if (ev.lNetworkEvents != 0xDEADBEEF)
  40. {
  41. /* fd refers to a socket. */
  42. /* FIXME: other applications, like squid, use an undocumented
  43. _free_osfhnd free function. But this is not enough: The 'osfile'
  44. flags for fd also needs to be cleared, but it is hard to access it.
  45. Instead, here we just close twice the file descriptor. */
  46. if (closesocket (sock))
  47. {
  48. set_winsock_errno ();
  49. return -1;
  50. }
  51. else
  52. {
  53. /* This call frees the file descriptor and does a
  54. CloseHandle ((HANDLE) _get_osfhandle (fd)), which fails. */
  55. _close (fd);
  56. return 0;
  57. }
  58. }
  59. else
  60. /* Some other type of file descriptor. */
  61. return execute_close_hooks (remaining_list, primary, fd);
  62. }
  63. static int
  64. ioctl_fd_maybe_socket (const struct fd_hook *remaining_list,
  65. gl_ioctl_fn primary,
  66. int fd, int request, void *arg)
  67. {
  68. SOCKET sock;
  69. WSANETWORKEVENTS ev;
  70. /* Test whether fd refers to a socket. */
  71. sock = FD_TO_SOCKET (fd);
  72. ev.lNetworkEvents = 0xDEADBEEF;
  73. WSAEnumNetworkEvents (sock, NULL, &ev);
  74. if (ev.lNetworkEvents != 0xDEADBEEF)
  75. {
  76. /* fd refers to a socket. */
  77. if (ioctlsocket (sock, request, arg) < 0)
  78. {
  79. set_winsock_errno ();
  80. return -1;
  81. }
  82. else
  83. return 0;
  84. }
  85. else
  86. /* Some other type of file descriptor. */
  87. return execute_ioctl_hooks (remaining_list, primary, fd, request, arg);
  88. }
  89. static struct fd_hook fd_sockets_hook;
  90. static int initialized_sockets_version /* = 0 */;
  91. #endif /* WINDOWS_SOCKETS */
  92. int
  93. gl_sockets_startup (int version _GL_UNUSED)
  94. {
  95. #if WINDOWS_SOCKETS
  96. if (version > initialized_sockets_version)
  97. {
  98. WSADATA data;
  99. int err;
  100. err = WSAStartup (version, &data);
  101. if (err != 0)
  102. return 1;
  103. if (data.wVersion != version)
  104. {
  105. WSACleanup ();
  106. return 2;
  107. }
  108. if (initialized_sockets_version == 0)
  109. register_fd_hook (close_fd_maybe_socket, ioctl_fd_maybe_socket,
  110. &fd_sockets_hook);
  111. initialized_sockets_version = version;
  112. }
  113. #endif
  114. return 0;
  115. }
  116. int
  117. gl_sockets_cleanup (void)
  118. {
  119. #if WINDOWS_SOCKETS
  120. int err;
  121. initialized_sockets_version = 0;
  122. unregister_fd_hook (&fd_sockets_hook);
  123. err = WSACleanup ();
  124. if (err != 0)
  125. return 1;
  126. #endif
  127. return 0;
  128. }