1 /* sockets.c --- wrappers for Windows socket functions
3 Copyright (C) 2008-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Simon Josefsson */
27 /* This includes winsock2.h on MinGW. */
28 # include <sys/socket.h>
31 # if GNULIB_MSVC_NOTHROW
32 # include "msvc-nothrow.h"
37 /* Get set_winsock_errno, FD_TO_SOCKET etc. */
41 close_fd_maybe_socket (const struct fd_hook
*remaining_list
,
45 /* Note about multithread-safety: There is a race condition where, between
46 our calls to closesocket() and the primary close(), some other thread
47 could make system calls that allocate precisely the same HANDLE value
48 as sock; then the primary close() would call CloseHandle() on it. */
52 /* Test whether fd refers to a socket. */
53 sock
= FD_TO_SOCKET (fd
);
54 ev
.lNetworkEvents
= 0xDEADBEEF;
55 WSAEnumNetworkEvents (sock
, NULL
, &ev
);
56 if (ev
.lNetworkEvents
!= 0xDEADBEEF)
58 /* fd refers to a socket. */
59 /* FIXME: other applications, like squid, use an undocumented
60 _free_osfhnd free function. But this is not enough: The 'osfile'
61 flags for fd also needs to be cleared, but it is hard to access it.
62 Instead, here we just close twice the file descriptor. */
63 if (closesocket (sock
))
70 /* This call frees the file descriptor and does a
71 CloseHandle ((HANDLE) _get_osfhandle (fd)), which fails. */
77 /* Some other type of file descriptor. */
78 return execute_close_hooks (remaining_list
, primary
, fd
);
82 ioctl_fd_maybe_socket (const struct fd_hook
*remaining_list
,
84 int fd
, int request
, void *arg
)
89 /* Test whether fd refers to a socket. */
90 sock
= FD_TO_SOCKET (fd
);
91 ev
.lNetworkEvents
= 0xDEADBEEF;
92 WSAEnumNetworkEvents (sock
, NULL
, &ev
);
93 if (ev
.lNetworkEvents
!= 0xDEADBEEF)
95 /* fd refers to a socket. */
96 if (ioctlsocket (sock
, request
, arg
) < 0)
105 /* Some other type of file descriptor. */
106 return execute_ioctl_hooks (remaining_list
, primary
, fd
, request
, arg
);
109 static struct fd_hook fd_sockets_hook
;
111 static int initialized_sockets_version
/* = 0 */;
113 #endif /* WINDOWS_SOCKETS */
116 gl_sockets_startup (_GL_UNUSED
int version
)
119 if (version
> initialized_sockets_version
)
124 err
= WSAStartup (version
, &data
);
128 if (data
.wVersion
!= version
)
134 if (initialized_sockets_version
== 0)
135 register_fd_hook (close_fd_maybe_socket
, ioctl_fd_maybe_socket
,
138 initialized_sockets_version
= version
;
146 gl_sockets_cleanup (void)
151 initialized_sockets_version
= 0;
153 unregister_fd_hook (&fd_sockets_hook
);