findprog-in: Relicense under LGPLv2+.
[gnulib.git] / lib / sockets.c
blob9e3251aa446f3abb33890bb8833cceca358c487b
1 /* sockets.c --- wrappers for Windows socket functions
3 Copyright (C) 2008-2020 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 <https://www.gnu.org/licenses/>. */
18 /* Written by Simon Josefsson */
20 #include <config.h>
22 /* Specification. */
23 #include "sockets.h"
25 #if WINDOWS_SOCKETS
27 /* This includes winsock2.h on MinGW. */
28 # include <sys/socket.h>
30 # include "fd-hook.h"
31 # if GNULIB_MSVC_NOTHROW
32 # include "msvc-nothrow.h"
33 # else
34 # include <io.h>
35 # endif
37 /* Get set_winsock_errno, FD_TO_SOCKET etc. */
38 # include "w32sock.h"
40 static int
41 close_fd_maybe_socket (const struct fd_hook *remaining_list,
42 gl_close_fn primary,
43 int fd)
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 SOCKET sock;
50 WSANETWORKEVENTS ev;
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))
65 set_winsock_errno ();
66 return -1;
68 else
70 /* This call frees the file descriptor and does a
71 CloseHandle ((HANDLE) _get_osfhandle (fd)), which fails. */
72 _close (fd);
73 return 0;
76 else
77 /* Some other type of file descriptor. */
78 return execute_close_hooks (remaining_list, primary, fd);
81 static int
82 ioctl_fd_maybe_socket (const struct fd_hook *remaining_list,
83 gl_ioctl_fn primary,
84 int fd, int request, void *arg)
86 SOCKET sock;
87 WSANETWORKEVENTS ev;
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)
98 set_winsock_errno ();
99 return -1;
101 else
102 return 0;
104 else
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 (int version _GL_UNUSED)
118 #if WINDOWS_SOCKETS
119 if (version > initialized_sockets_version)
121 WSADATA data;
122 int err;
124 err = WSAStartup (version, &data);
125 if (err != 0)
126 return 1;
128 if (data.wVersion != version)
130 WSACleanup ();
131 return 2;
134 if (initialized_sockets_version == 0)
135 register_fd_hook (close_fd_maybe_socket, ioctl_fd_maybe_socket,
136 &fd_sockets_hook);
138 initialized_sockets_version = version;
140 #endif
142 return 0;
146 gl_sockets_cleanup (void)
148 #if WINDOWS_SOCKETS
149 int err;
151 initialized_sockets_version = 0;
153 unregister_fd_hook (&fd_sockets_hook);
155 err = WSACleanup ();
156 if (err != 0)
157 return 1;
158 #endif
160 return 0;