This source file includes following definitions.
- close_fd_maybe_socket
- ioctl_fd_maybe_socket
- gl_sockets_startup
- gl_sockets_cleanup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <config.h>
21
22
23 #include "sockets.h"
24
25 #if WINDOWS_SOCKETS
26
27
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
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
46
47
48
49 SOCKET sock;
50 WSANETWORKEVENTS ev;
51
52
53 sock = FD_TO_SOCKET (fd);
54 ev.lNetworkEvents = 0xDEADBEEF;
55 WSAEnumNetworkEvents (sock, NULL, &ev);
56 if (ev.lNetworkEvents != 0xDEADBEEF)
57 {
58
59
60
61
62
63 if (closesocket (sock))
64 {
65 set_winsock_errno ();
66 return -1;
67 }
68 else
69 {
70
71
72 _close (fd);
73 return 0;
74 }
75 }
76 else
77
78 return execute_close_hooks (remaining_list, primary, fd);
79 }
80
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)
85 {
86 SOCKET sock;
87 WSANETWORKEVENTS ev;
88
89
90 sock = FD_TO_SOCKET (fd);
91 ev.lNetworkEvents = 0xDEADBEEF;
92 WSAEnumNetworkEvents (sock, NULL, &ev);
93 if (ev.lNetworkEvents != 0xDEADBEEF)
94 {
95
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
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 ;
112
113 #endif
114
115 int
116 gl_sockets_startup (_GL_UNUSED int version)
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
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 }