sockets.c 3.7 KB

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