documented fix
[gnutls.git] / gl / sockets.c
blob3daec454d31c84cfda82ffc398fe83b1328817ca
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 */
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 # include "msvc-nothrow.h"
33 /* Get set_winsock_errno, FD_TO_SOCKET etc. */
34 # include "w32sock.h"
36 static int
37 close_fd_maybe_socket (const struct fd_hook *remaining_list,
38 gl_close_fn primary,
39 int fd)
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. */
45 SOCKET sock;
46 WSANETWORKEVENTS ev;
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))
61 set_winsock_errno ();
62 return -1;
64 else
66 /* This call frees the file descriptor and does a
67 CloseHandle ((HANDLE) _get_osfhandle (fd)), which fails. */
68 _close (fd);
69 return 0;
72 else
73 /* Some other type of file descriptor. */
74 return execute_close_hooks (remaining_list, primary, fd);
77 static int
78 ioctl_fd_maybe_socket (const struct fd_hook *remaining_list,
79 gl_ioctl_fn primary,
80 int fd, int request, void *arg)
82 SOCKET sock;
83 WSANETWORKEVENTS ev;
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)
94 set_winsock_errno ();
95 return -1;
97 else
98 return 0;
100 else
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)
114 #if WINDOWS_SOCKETS
115 if (version > initialized_sockets_version)
117 WSADATA data;
118 int err;
120 err = WSAStartup (version, &data);
121 if (err != 0)
122 return 1;
124 if (data.wVersion < version)
125 return 2;
127 if (initialized_sockets_version == 0)
128 register_fd_hook (close_fd_maybe_socket, ioctl_fd_maybe_socket,
129 &fd_sockets_hook);
131 initialized_sockets_version = version;
133 #endif
135 return 0;
139 gl_sockets_cleanup (void)
141 #if WINDOWS_SOCKETS
142 int err;
144 initialized_sockets_version = 0;
146 unregister_fd_hook (&fd_sockets_hook);
148 err = WSACleanup ();
149 if (err != 0)
150 return 1;
151 #endif
153 return 0;