sockets.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* sockets.c --- wrappers for Windows socket functions
  2. Copyright (C) 2008-2010 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 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 General Public License for more details.
  11. You should have received a copy of the GNU 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 "close-hook.h"
  21. /* Get set_winsock_errno, FD_TO_SOCKET etc. */
  22. # include "w32sock.h"
  23. static int
  24. close_fd_maybe_socket (int fd, const struct close_hook *remaining_list)
  25. {
  26. SOCKET sock;
  27. WSANETWORKEVENTS ev;
  28. /* Test whether fd refers to a socket. */
  29. sock = FD_TO_SOCKET (fd);
  30. ev.lNetworkEvents = 0xDEADBEEF;
  31. WSAEnumNetworkEvents (sock, NULL, &ev);
  32. if (ev.lNetworkEvents != 0xDEADBEEF)
  33. {
  34. /* fd refers to a socket. */
  35. /* FIXME: other applications, like squid, use an undocumented
  36. _free_osfhnd free function. But this is not enough: The 'osfile'
  37. flags for fd also needs to be cleared, but it is hard to access it.
  38. Instead, here we just close twice the file descriptor. */
  39. if (closesocket (sock))
  40. {
  41. set_winsock_errno ();
  42. return -1;
  43. }
  44. else
  45. {
  46. /* This call frees the file descriptor and does a
  47. CloseHandle ((HANDLE) _get_osfhandle (fd)), which fails. */
  48. _close (fd);
  49. return 0;
  50. }
  51. }
  52. else
  53. /* Some other type of file descriptor. */
  54. return execute_close_hooks (fd, remaining_list);
  55. }
  56. static struct close_hook close_sockets_hook;
  57. static int initialized_sockets_version /* = 0 */;
  58. #endif /* WINDOWS_SOCKETS */
  59. int
  60. gl_sockets_startup (int version _GL_UNUSED)
  61. {
  62. #if WINDOWS_SOCKETS
  63. if (version > initialized_sockets_version)
  64. {
  65. WSADATA data;
  66. int err;
  67. err = WSAStartup (version, &data);
  68. if (err != 0)
  69. return 1;
  70. if (data.wVersion < version)
  71. return 2;
  72. if (initialized_sockets_version == 0)
  73. register_close_hook (close_fd_maybe_socket, &close_sockets_hook);
  74. initialized_sockets_version = version;
  75. }
  76. #endif
  77. return 0;
  78. }
  79. int
  80. gl_sockets_cleanup (void)
  81. {
  82. #if WINDOWS_SOCKETS
  83. int err;
  84. initialized_sockets_version = 0;
  85. unregister_close_hook (&close_sockets_hook);
  86. err = WSACleanup ();
  87. if (err != 0)
  88. return 1;
  89. #endif
  90. return 0;
  91. }