sockets.c 4.1 KB

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