1 /* sockets.c --- wrappers for Windows socket functions
2 
3    Copyright (C) 2008-2021 Free Software Foundation, Inc.
4 
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.
9 
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.
14 
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/>.  */
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
close_fd_maybe_socket(const struct fd_hook * remaining_list,gl_close_fn primary,int fd)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   SOCKET sock;
50   WSANETWORKEVENTS ev;
51 
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)
57     {
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))
64         {
65           set_winsock_errno ();
66           return -1;
67         }
68       else
69         {
70           /* This call frees the file descriptor and does a
71              CloseHandle ((HANDLE) _get_osfhandle (fd)), which fails.  */
72           _close (fd);
73           return 0;
74         }
75     }
76   else
77     /* Some other type of file descriptor.  */
78     return execute_close_hooks (remaining_list, primary, fd);
79 }
80 
81 static int
ioctl_fd_maybe_socket(const struct fd_hook * remaining_list,gl_ioctl_fn primary,int fd,int request,void * arg)82 ioctl_fd_maybe_socket (const struct fd_hook *remaining_list,
83                        gl_ioctl_fn primary,
84                        int fd, int request, void *arg)
85 {
86   SOCKET sock;
87   WSANETWORKEVENTS ev;
88 
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)
94     {
95       /* fd refers to a socket.  */
96       if (ioctlsocket (sock, request, arg) < 0)
97         {
98           set_winsock_errno ();
99           return -1;
100         }
101       else
102         return 0;
103     }
104   else
105     /* Some other type of file descriptor.  */
106     return execute_ioctl_hooks (remaining_list, primary, fd, request, arg);
107 }
108 
109 static struct fd_hook fd_sockets_hook;
110 
111 static int initialized_sockets_version /* = 0 */;
112 
113 #endif /* WINDOWS_SOCKETS */
114 
115 int
gl_sockets_startup(int version _GL_UNUSED)116 gl_sockets_startup (int version _GL_UNUSED)
117 {
118 #if WINDOWS_SOCKETS
119   if (version > initialized_sockets_version)
120     {
121       WSADATA data;
122       int err;
123 
124       err = WSAStartup (version, &data);
125       if (err != 0)
126         return 1;
127 
128       if (data.wVersion != version)
129         {
130           WSACleanup ();
131           return 2;
132         }
133 
134       if (initialized_sockets_version == 0)
135         register_fd_hook (close_fd_maybe_socket, ioctl_fd_maybe_socket,
136                           &fd_sockets_hook);
137 
138       initialized_sockets_version = version;
139     }
140 #endif
141 
142   return 0;
143 }
144 
145 int
gl_sockets_cleanup(void)146 gl_sockets_cleanup (void)
147 {
148 #if WINDOWS_SOCKETS
149   int err;
150 
151   initialized_sockets_version = 0;
152 
153   unregister_fd_hook (&fd_sockets_hook);
154 
155   err = WSACleanup ();
156   if (err != 0)
157     return 1;
158 #endif
159 
160   return 0;
161 }
162