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