1 /* sockets.c --- wrappers for Windows socket functions
3 Copyright (C) 2008-2011 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Simon Josefsson */
27 /* This includes winsock2.h on MinGW. */
28 # include <sys/socket.h>
32 /* Get set_winsock_errno, FD_TO_SOCKET etc. */
36 close_fd_maybe_socket (const struct fd_hook *remaining_list,
40 /* Note about multithread-safety: There is a race condition where, between
41 our calls to closesocket() and the primary close(), some other thread
42 could make system calls that allocate precisely the same HANDLE value
43 as sock; then the primary close() would call CloseHandle() on it. */
47 /* Test whether fd refers to a socket. */
48 sock = FD_TO_SOCKET (fd);
49 ev.lNetworkEvents = 0xDEADBEEF;
50 WSAEnumNetworkEvents (sock, NULL, &ev);
51 if (ev.lNetworkEvents != 0xDEADBEEF)
53 /* fd refers to a socket. */
54 /* FIXME: other applications, like squid, use an undocumented
55 _free_osfhnd free function. But this is not enough: The 'osfile'
56 flags for fd also needs to be cleared, but it is hard to access it.
57 Instead, here we just close twice the file descriptor. */
58 if (closesocket (sock))
65 /* This call frees the file descriptor and does a
66 CloseHandle ((HANDLE) _get_osfhandle (fd)), which fails. */
72 /* Some other type of file descriptor. */
73 return execute_close_hooks (remaining_list, primary, fd);
77 ioctl_fd_maybe_socket (const struct fd_hook *remaining_list,
79 int fd, int request, void *arg)
84 /* Test whether fd refers to a socket. */
85 sock = FD_TO_SOCKET (fd);
86 ev.lNetworkEvents = 0xDEADBEEF;
87 WSAEnumNetworkEvents (sock, NULL, &ev);
88 if (ev.lNetworkEvents != 0xDEADBEEF)
90 /* fd refers to a socket. */
91 if (ioctlsocket (sock, request, arg) < 0)
100 /* Some other type of file descriptor. */
101 return execute_ioctl_hooks (remaining_list, primary, fd, request, arg);
104 static struct fd_hook fd_sockets_hook;
106 static int initialized_sockets_version /* = 0 */;
108 #endif /* WINDOWS_SOCKETS */
111 gl_sockets_startup (int version _GL_UNUSED)
114 if (version > initialized_sockets_version)
119 err = WSAStartup (version, &data);
123 if (data.wVersion < version)
126 if (initialized_sockets_version == 0)
127 register_fd_hook (close_fd_maybe_socket, ioctl_fd_maybe_socket,
130 initialized_sockets_version = version;
138 gl_sockets_cleanup (void)
143 initialized_sockets_version = 0;
145 unregister_fd_hook (&fd_sockets_hook);