Debian Codegraph @

lib/sockets.c

raw
1 /* sockets.c --- wrappers for Windows socket functions
2
3 Copyright (C) 2008-2026 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>. */
17
18 /* Written by Simon Josefsson */
19
20 #include <config.h>
21
22 /* Specification. */
23 #include "sockets.h"
24
25 #if WINDOWS_SOCKETS
26
27 /* This includes winsock2.h on MinGW. */
28 # include <sys/socket.h>
29
30 # include "fd-hook.h"
31 # if GNULIB_MSVC_NOTHROW
32 # include "msvc-nothrow.h"
33 # else
34 # include <io.h>
35 # endif
36
37 /* Get set_winsock_errno, FD_TO_SOCKET etc. */
38 # include "w32sock.h"
39
40 static int
41 close_fd_maybe_socket (const struct fd_hook *remaining_list,
42 gl_close_fn primary,
43 int fd)
44 {
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. */
49
50 /* Test whether fd refers to a socket. */
51 SOCKET sock = FD_TO_SOCKET (fd);
52 WSANETWORKEVENTS ev;
53 ev.lNetworkEvents = 0xDEADBEEF;
54 WSAEnumNetworkEvents (sock, NULL, &ev);
55 if (ev.lNetworkEvents != 0xDEADBEEF)
56 {
57 /* fd refers to a socket. */
58 /* FIXME: other applications, like squid, use an undocumented
59 _free_osfhnd free function. But this is not enough: The 'osfile'
60 flags for fd also needs to be cleared, but it is hard to access it.
61 Instead, here we just close twice the file descriptor. */
62 if (closesocket (sock))
63 {
64 set_winsock_errno ();
65 return -1;
66 }
67 else
68 {
69 /* This call frees the file descriptor and does a
70 CloseHandle ((HANDLE) _get_osfhandle (fd)), which fails. */
71 _close (fd);
72 return 0;
73 }
74 }
75 else
76 /* Some other type of file descriptor. */
77 return execute_close_hooks (remaining_list, primary, fd);
78 }
79
80 static int
81 ioctl_fd_maybe_socket (const struct fd_hook *remaining_list,
82 gl_ioctl_fn primary,
83 int fd, int request, void *arg)
84 {
85 /* Test whether fd refers to a socket. */
86 SOCKET sock = FD_TO_SOCKET (fd);
87 WSANETWORKEVENTS ev;
88 ev.lNetworkEvents = 0xDEADBEEF;
89 WSAEnumNetworkEvents (sock, NULL, &ev);
90 if (ev.lNetworkEvents != 0xDEADBEEF)
91 {
92 /* fd refers to a socket. */
93 if (ioctlsocket (sock, request, arg) < 0)
94 {
95 set_winsock_errno ();
96 return -1;
97 }
98 else
99 return 0;
100 }
101 else
102 /* Some other type of file descriptor. */
103 return execute_ioctl_hooks (remaining_list, primary, fd, request, arg);
104 }
105
106 static struct fd_hook fd_sockets_hook;
107
108 static int initialized_sockets_version /* = 0 */;
109
110 #endif /* WINDOWS_SOCKETS */
111
112 int
113 gl_sockets_startup (_GL_UNUSED int version)
114 {
115 #if WINDOWS_SOCKETS
116 if (version > initialized_sockets_version)
117 {
118 WSADATA data;
119 int err = WSAStartup (version, &data);
120 if (err != 0)
121 return 1;
122
123 if (data.wVersion != version)
124 {
125 WSACleanup ();
126 return 2;
127 }
128
129 if (initialized_sockets_version == 0)
130 register_fd_hook (close_fd_maybe_socket, ioctl_fd_maybe_socket,
131 &fd_sockets_hook);
132
133 initialized_sockets_version = version;
134 }
135 #endif
136
137 return 0;
138 }
139
140 int
141 gl_sockets_cleanup (void)
142 {
143 #if WINDOWS_SOCKETS
144 initialized_sockets_version = 0;
145
146 unregister_fd_hook (&fd_sockets_hook);
147
148 int err = WSACleanup ();
149 if (err != 0)
150 return 1;
151 #endif
152
153 return 0;
154 }