1 /* sockets.c --- wrappers for Windows socket functions
3 Copyright (C) 2008-2012 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>
31 # include "msvc-nothrow.h"
33 /* Get set_winsock_errno, FD_TO_SOCKET etc. */
37 close_fd_maybe_socket (const struct fd_hook
*remaining_list
,
41 /* Note about multithread-safety: There is a race condition where, between
42 our calls to closesocket() and the primary close(), some other thread
43 could make system calls that allocate precisely the same HANDLE value
44 as sock; then the primary close() would call CloseHandle() on it. */
48 /* Test whether fd refers to a socket. */
49 sock
= FD_TO_SOCKET (fd
);
50 ev
.lNetworkEvents
= 0xDEADBEEF;
51 WSAEnumNetworkEvents (sock
, NULL
, &ev
);
52 if (ev
.lNetworkEvents
!= 0xDEADBEEF)
54 /* fd refers to a socket. */
55 /* FIXME: other applications, like squid, use an undocumented
56 _free_osfhnd free function. But this is not enough: The 'osfile'
57 flags for fd also needs to be cleared, but it is hard to access it.
58 Instead, here we just close twice the file descriptor. */
59 if (closesocket (sock
))
66 /* This call frees the file descriptor and does a
67 CloseHandle ((HANDLE) _get_osfhandle (fd)), which fails. */
73 /* Some other type of file descriptor. */
74 return execute_close_hooks (remaining_list
, primary
, fd
);
78 ioctl_fd_maybe_socket (const struct fd_hook
*remaining_list
,
80 int fd
, int request
, void *arg
)
85 /* Test whether fd refers to a socket. */
86 sock
= FD_TO_SOCKET (fd
);
87 ev
.lNetworkEvents
= 0xDEADBEEF;
88 WSAEnumNetworkEvents (sock
, NULL
, &ev
);
89 if (ev
.lNetworkEvents
!= 0xDEADBEEF)
91 /* fd refers to a socket. */
92 if (ioctlsocket (sock
, request
, arg
) < 0)
101 /* Some other type of file descriptor. */
102 return execute_ioctl_hooks (remaining_list
, primary
, fd
, request
, arg
);
105 static struct fd_hook fd_sockets_hook
;
107 static int initialized_sockets_version
/* = 0 */;
109 #endif /* WINDOWS_SOCKETS */
112 gl_sockets_startup (int version _GL_UNUSED
)
115 if (version
> initialized_sockets_version
)
120 err
= WSAStartup (version
, &data
);
124 if (data
.wVersion
< version
)
127 if (initialized_sockets_version
== 0)
128 register_fd_hook (close_fd_maybe_socket
, ioctl_fd_maybe_socket
,
131 initialized_sockets_version
= version
;
139 gl_sockets_cleanup (void)
144 initialized_sockets_version
= 0;
146 unregister_fd_hook (&fd_sockets_hook
);