]>
Commit | Line | Data |
---|---|---|
859d215b CV |
1 | /* fhandler_socket_inet.cc. |
2 | ||
3 | See fhandler.h for a description of the fhandler classes. | |
4 | ||
5 | This file is part of Cygwin. | |
6 | ||
7 | This software is a copyrighted work licensed under the terms of the | |
8 | Cygwin license. Please consult the file "CYGWIN_LICENSE" for | |
9 | details. */ | |
10 | ||
11 | #define __INSIDE_CYGWIN_NET__ | |
12 | #define USE_SYS_TYPES_FD_SET | |
13 | ||
14 | #include "winsup.h" | |
15 | #ifdef __x86_64__ | |
16 | /* 2014-04-24: Current Mingw headers define sockaddr_in6 using u_long (8 byte) | |
17 | because a redefinition for LP64 systems is missing. This leads to a wrong | |
18 | definition and size of sockaddr_in6 when building with winsock headers. | |
19 | This definition is also required to use the right u_long type in subsequent | |
20 | function calls. */ | |
21 | #undef u_long | |
22 | #define u_long __ms_u_long | |
23 | #endif | |
25ea6af1 CV |
24 | #include <w32api/ws2tcpip.h> |
25 | #include <w32api/mswsock.h> | |
8ccffddc | 26 | #include <w32api/mstcpip.h> |
e037192b | 27 | #include <netinet/tcp.h> |
25ea6af1 CV |
28 | #include <unistd.h> |
29 | #include <asm/byteorder.h> | |
30 | #include <sys/socket.h> | |
31 | #include <sys/param.h> | |
32 | #include <sys/statvfs.h> | |
33 | #include <cygwin/acl.h> | |
859d215b | 34 | #include "cygerrno.h" |
859d215b CV |
35 | #include "path.h" |
36 | #include "fhandler.h" | |
37 | #include "dtable.h" | |
38 | #include "cygheap.h" | |
859d215b | 39 | #include "shared_info.h" |
859d215b | 40 | #include "wininfo.h" |
859d215b CV |
41 | |
42 | #define ASYNC_MASK (FD_READ|FD_WRITE|FD_OOB|FD_ACCEPT|FD_CONNECT) | |
43 | #define EVENT_MASK (FD_READ|FD_WRITE|FD_OOB|FD_ACCEPT|FD_CONNECT|FD_CLOSE) | |
44 | ||
45 | #define LOCK_EVENTS \ | |
46 | if (wsock_mtx && \ | |
47 | WaitForSingleObject (wsock_mtx, INFINITE) != WAIT_FAILED) \ | |
48 | { | |
49 | ||
50 | #define UNLOCK_EVENTS \ | |
51 | ReleaseMutex (wsock_mtx); \ | |
52 | } | |
53 | ||
b79018ee CV |
54 | /* Maximum number of concurrently opened sockets from all Cygwin processes |
55 | per session. Note that shared sockets (through dup/fork/exec) are | |
56 | counted as one socket. */ | |
57 | #define NUM_SOCKS 2048U | |
58 | ||
59 | #define LOCK_EVENTS \ | |
60 | if (wsock_mtx && \ | |
61 | WaitForSingleObject (wsock_mtx, INFINITE) != WAIT_FAILED) \ | |
62 | { | |
63 | ||
64 | #define UNLOCK_EVENTS \ | |
65 | ReleaseMutex (wsock_mtx); \ | |
66 | } | |
67 | ||
68 | static wsa_event wsa_events[NUM_SOCKS] __attribute__((section (".cygwin_dll_common"), shared)); | |
69 | ||
70 | static LONG socket_serial_number __attribute__((section (".cygwin_dll_common"), shared)); | |
71 | ||
72 | static HANDLE wsa_slot_mtx; | |
73 | ||
74 | static PWCHAR | |
75 | sock_shared_name (PWCHAR buf, LONG num) | |
76 | { | |
77 | __small_swprintf (buf, L"socket.%d", num); | |
78 | return buf; | |
79 | } | |
80 | ||
81 | static wsa_event * | |
82 | search_wsa_event_slot (LONG new_serial_number) | |
83 | { | |
84 | WCHAR name[32], searchname[32]; | |
85 | UNICODE_STRING uname; | |
86 | OBJECT_ATTRIBUTES attr; | |
87 | NTSTATUS status; | |
88 | ||
89 | if (!wsa_slot_mtx) | |
90 | { | |
91 | RtlInitUnicodeString (&uname, sock_shared_name (name, 0)); | |
92 | InitializeObjectAttributes (&attr, &uname, OBJ_INHERIT | OBJ_OPENIF, | |
93 | get_session_parent_dir (), | |
94 | everyone_sd (CYG_MUTANT_ACCESS)); | |
95 | status = NtCreateMutant (&wsa_slot_mtx, CYG_MUTANT_ACCESS, &attr, FALSE); | |
96 | if (!NT_SUCCESS (status)) | |
97 | api_fatal ("Couldn't create/open shared socket mutex %S, %y", | |
98 | &uname, status); | |
99 | } | |
100 | switch (WaitForSingleObject (wsa_slot_mtx, INFINITE)) | |
101 | { | |
102 | case WAIT_OBJECT_0: | |
103 | case WAIT_ABANDONED: | |
104 | break; | |
105 | default: | |
106 | api_fatal ("WFSO failed for shared socket mutex, %E"); | |
107 | break; | |
108 | } | |
109 | unsigned int slot = new_serial_number % NUM_SOCKS; | |
110 | while (wsa_events[slot].serial_number) | |
111 | { | |
112 | HANDLE searchmtx; | |
113 | RtlInitUnicodeString (&uname, sock_shared_name (searchname, | |
114 | wsa_events[slot].serial_number)); | |
115 | InitializeObjectAttributes (&attr, &uname, 0, get_session_parent_dir (), | |
116 | NULL); | |
117 | status = NtOpenMutant (&searchmtx, READ_CONTROL, &attr); | |
118 | if (!NT_SUCCESS (status)) | |
119 | break; | |
120 | /* Mutex still exists, attached socket is active, try next slot. */ | |
121 | NtClose (searchmtx); | |
122 | slot = (slot + 1) % NUM_SOCKS; | |
123 | if (slot == (new_serial_number % NUM_SOCKS)) | |
124 | { | |
125 | /* Did the whole array once. Too bad. */ | |
126 | debug_printf ("No free socket slot"); | |
127 | ReleaseMutex (wsa_slot_mtx); | |
128 | return NULL; | |
129 | } | |
130 | } | |
131 | memset (&wsa_events[slot], 0, sizeof (wsa_event)); | |
132 | wsa_events[slot].serial_number = new_serial_number; | |
133 | ReleaseMutex (wsa_slot_mtx); | |
134 | return wsa_events + slot; | |
135 | } | |
136 | ||
859d215b CV |
137 | /* cygwin internal: map sockaddr into internet domain address */ |
138 | static int | |
139 | get_inet_addr_inet (const struct sockaddr *in, int inlen, | |
140 | struct sockaddr_storage *out, int *outlen) | |
141 | { | |
142 | switch (in->sa_family) | |
143 | { | |
144 | case AF_INET: | |
145 | memcpy (out, in, inlen); | |
146 | *outlen = inlen; | |
147 | /* If the peer address given in connect or sendto is the ANY address, | |
148 | Winsock fails with WSAEADDRNOTAVAIL, while Linux converts that into | |
149 | a connection/send attempt to LOOPBACK. We're doing the same here. */ | |
150 | if (((struct sockaddr_in *) out)->sin_addr.s_addr == htonl (INADDR_ANY)) | |
151 | ((struct sockaddr_in *) out)->sin_addr.s_addr = htonl (INADDR_LOOPBACK); | |
152 | return 0; | |
153 | case AF_INET6: | |
154 | memcpy (out, in, inlen); | |
155 | *outlen = inlen; | |
156 | /* See comment in AF_INET case. */ | |
157 | if (IN6_IS_ADDR_UNSPECIFIED (&((struct sockaddr_in6 *) out)->sin6_addr)) | |
158 | ((struct sockaddr_in6 *) out)->sin6_addr = in6addr_loopback; | |
159 | return 0; | |
160 | default: | |
161 | set_errno (EAFNOSUPPORT); | |
162 | return SOCKET_ERROR; | |
163 | } | |
164 | } | |
165 | ||
233bde31 CV |
166 | /* There's no DLL which exports the symbol WSARecvMsg. One has to call |
167 | WSAIoctl as below to fetch the function pointer. Why on earth did the | |
168 | MS developers decide not to export a normal symbol for these extension | |
169 | functions? */ | |
170 | inline int | |
171 | get_ext_funcptr (SOCKET sock, void *funcptr) | |
172 | { | |
173 | DWORD bret; | |
174 | const GUID guid = WSAID_WSARECVMSG; | |
175 | return WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER, | |
176 | (void *) &guid, sizeof (GUID), funcptr, sizeof (void *), | |
177 | &bret, NULL, NULL); | |
178 | } | |
179 | ||
859d215b CV |
180 | static int |
181 | convert_ws1_ip_optname (int optname) | |
182 | { | |
183 | static int ws2_optname[] = | |
184 | { | |
185 | 0, | |
186 | IP_OPTIONS, | |
187 | IP_MULTICAST_IF, | |
188 | IP_MULTICAST_TTL, | |
189 | IP_MULTICAST_LOOP, | |
190 | IP_ADD_MEMBERSHIP, | |
191 | IP_DROP_MEMBERSHIP, | |
192 | IP_TTL, | |
193 | IP_TOS, | |
194 | IP_DONTFRAGMENT | |
195 | }; | |
196 | return (optname < 1 || optname > _WS1_IP_DONTFRAGMENT) | |
197 | ? optname | |
198 | : ws2_optname[optname]; | |
199 | } | |
200 | ||
b79018ee CV |
201 | fhandler_socket_wsock::fhandler_socket_wsock () : |
202 | fhandler_socket (), | |
203 | wsock_events (NULL), | |
204 | wsock_mtx (NULL), | |
205 | wsock_evt (NULL), | |
72517661 | 206 | status (), |
7f7532fa | 207 | prot_info_ptr (NULL) |
b79018ee CV |
208 | { |
209 | need_fork_fixup (true); | |
210 | } | |
211 | ||
212 | fhandler_socket_wsock::~fhandler_socket_wsock () | |
213 | { | |
214 | if (prot_info_ptr) | |
215 | cfree (prot_info_ptr); | |
216 | } | |
217 | ||
218 | bool | |
219 | fhandler_socket_wsock::init_events () | |
220 | { | |
221 | LONG new_serial_number; | |
222 | WCHAR name[32]; | |
223 | UNICODE_STRING uname; | |
224 | OBJECT_ATTRIBUTES attr; | |
225 | NTSTATUS status; | |
226 | ||
227 | do | |
228 | { | |
229 | new_serial_number = | |
230 | InterlockedIncrement (&socket_serial_number); | |
231 | if (!new_serial_number) /* 0 is reserved for global mutex */ | |
232 | InterlockedIncrement (&socket_serial_number); | |
233 | set_ino (new_serial_number); | |
234 | RtlInitUnicodeString (&uname, sock_shared_name (name, new_serial_number)); | |
235 | InitializeObjectAttributes (&attr, &uname, OBJ_INHERIT | OBJ_OPENIF, | |
236 | get_session_parent_dir (), | |
237 | everyone_sd (CYG_MUTANT_ACCESS)); | |
238 | status = NtCreateMutant (&wsock_mtx, CYG_MUTANT_ACCESS, &attr, FALSE); | |
239 | if (!NT_SUCCESS (status)) | |
240 | { | |
241 | debug_printf ("NtCreateMutant(%S), %y", &uname, status); | |
242 | set_errno (ENOBUFS); | |
243 | return false; | |
244 | } | |
245 | if (status == STATUS_OBJECT_NAME_EXISTS) | |
246 | NtClose (wsock_mtx); | |
247 | } | |
248 | while (status == STATUS_OBJECT_NAME_EXISTS); | |
249 | if ((wsock_evt = CreateEvent (&sec_all, TRUE, FALSE, NULL)) | |
250 | == WSA_INVALID_EVENT) | |
251 | { | |
252 | debug_printf ("CreateEvent, %E"); | |
253 | set_errno (ENOBUFS); | |
254 | NtClose (wsock_mtx); | |
255 | return false; | |
256 | } | |
257 | if (WSAEventSelect (get_socket (), wsock_evt, EVENT_MASK) == SOCKET_ERROR) | |
258 | { | |
259 | debug_printf ("WSAEventSelect, %E"); | |
260 | set_winsock_errno (); | |
261 | NtClose (wsock_evt); | |
262 | NtClose (wsock_mtx); | |
263 | return false; | |
264 | } | |
265 | if (!(wsock_events = search_wsa_event_slot (new_serial_number))) | |
266 | { | |
267 | set_errno (ENOBUFS); | |
268 | NtClose (wsock_evt); | |
269 | NtClose (wsock_mtx); | |
270 | return false; | |
271 | } | |
272 | if (get_socket_type () == SOCK_DGRAM) | |
273 | wsock_events->events = FD_WRITE; | |
274 | return true; | |
275 | } | |
276 | ||
277 | int | |
278 | fhandler_socket_wsock::evaluate_events (const long event_mask, long &events, | |
279 | const bool erase) | |
280 | { | |
281 | int ret = 0; | |
282 | long events_now = 0; | |
283 | ||
284 | WSANETWORKEVENTS evts = { 0 }; | |
285 | if (!(WSAEnumNetworkEvents (get_socket (), wsock_evt, &evts))) | |
286 | { | |
287 | if (evts.lNetworkEvents) | |
288 | { | |
289 | LOCK_EVENTS; | |
290 | wsock_events->events |= evts.lNetworkEvents; | |
291 | events_now = (wsock_events->events & event_mask); | |
292 | if (evts.lNetworkEvents & FD_CONNECT) | |
293 | { | |
294 | wsock_events->connect_errorcode = evts.iErrorCode[FD_CONNECT_BIT]; | |
295 | ||
b74bc883 | 296 | /* Setting the connect_state and calling the AF_LOCAL handshake |
b79018ee CV |
297 | here allows to handle this stuff from a single point. This |
298 | is independent of FD_CONNECT being requested. Consider a | |
299 | server calling connect(2) and then immediately poll(2) with | |
300 | only polling for POLLIN (example: postfix), or select(2) just | |
301 | asking for descriptors ready to read. | |
302 | ||
303 | Something weird occurs in Winsock: If you fork off and call | |
304 | recv/send on the duplicated, already connected socket, another | |
305 | FD_CONNECT event is generated in the child process. This | |
b74bc883 | 306 | would trigger a call to af_local_connect which obviously fail. |
b79018ee CV |
307 | Avoid this by calling set_connect_state only if connect_state |
308 | is connect_pending. */ | |
309 | if (connect_state () == connect_pending) | |
310 | { | |
311 | if (wsock_events->connect_errorcode) | |
312 | connect_state (connect_failed); | |
313 | else if (af_local_connect ()) | |
314 | { | |
315 | wsock_events->connect_errorcode = WSAGetLastError (); | |
316 | connect_state (connect_failed); | |
317 | } | |
318 | else | |
319 | connect_state (connected); | |
320 | } | |
321 | } | |
322 | UNLOCK_EVENTS; | |
323 | if ((evts.lNetworkEvents & FD_OOB) && wsock_events->owner) | |
324 | kill (wsock_events->owner, SIGURG); | |
325 | } | |
326 | } | |
327 | ||
328 | LOCK_EVENTS; | |
329 | if ((events = events_now) != 0 | |
330 | || (events = (wsock_events->events & event_mask)) != 0) | |
331 | { | |
332 | if (events & FD_CONNECT) | |
333 | { | |
334 | int wsa_err = wsock_events->connect_errorcode; | |
335 | if (wsa_err) | |
336 | { | |
337 | /* CV 2014-04-23: This is really weird. If you call connect | |
338 | asynchronously on a socket and then select, an error like | |
339 | "Connection refused" is set in the event and in the SO_ERROR | |
340 | socket option. If you call connect, then dup, then select, | |
341 | the error is set in the event, but not in the SO_ERROR socket | |
342 | option, despite the dup'ed socket handle referring to the same | |
343 | socket. We're trying to workaround this problem here by | |
344 | taking the connect errorcode from the event and write it back | |
345 | into the SO_ERROR socket option. | |
3bb346d5 | 346 | |
b79018ee CV |
347 | CV 2014-06-16: Call WSASetLastError *after* setsockopt since, |
348 | apparently, setsockopt sets the last WSA error code to 0 on | |
349 | success. */ | |
350 | ::setsockopt (get_socket (), SOL_SOCKET, SO_ERROR, | |
351 | (const char *) &wsa_err, sizeof wsa_err); | |
352 | WSASetLastError (wsa_err); | |
353 | ret = SOCKET_ERROR; | |
354 | } | |
355 | else | |
356 | wsock_events->events |= FD_WRITE; | |
357 | wsock_events->events &= ~FD_CONNECT; | |
358 | wsock_events->connect_errorcode = 0; | |
359 | } | |
360 | /* This test makes accept/connect behave as on Linux when accept/connect | |
361 | is called on a socket for which shutdown has been called. The second | |
362 | half of this code is in the shutdown method. */ | |
363 | if (events & FD_CLOSE) | |
364 | { | |
365 | if ((event_mask & FD_ACCEPT) && saw_shutdown_read ()) | |
366 | { | |
367 | WSASetLastError (WSAEINVAL); | |
368 | ret = SOCKET_ERROR; | |
369 | } | |
370 | if (event_mask & FD_CONNECT) | |
371 | { | |
372 | WSASetLastError (WSAECONNRESET); | |
373 | ret = SOCKET_ERROR; | |
374 | } | |
375 | } | |
376 | if (erase) | |
377 | wsock_events->events &= ~(events & ~(FD_WRITE | FD_CLOSE)); | |
378 | } | |
379 | UNLOCK_EVENTS; | |
380 | ||
381 | return ret; | |
382 | } | |
383 | ||
384 | int | |
385 | fhandler_socket_wsock::wait_for_events (const long event_mask, | |
386 | const DWORD flags) | |
387 | { | |
388 | if (async_io ()) | |
389 | return 0; | |
390 | ||
391 | int ret; | |
392 | long events = 0; | |
393 | DWORD wfmo_timeout = 50; | |
394 | DWORD timeout; | |
395 | ||
396 | WSAEVENT ev[3] = { wsock_evt, NULL, NULL }; | |
397 | wait_signal_arrived here (ev[1]); | |
398 | DWORD ev_cnt = 2; | |
399 | if ((ev[2] = pthread::get_cancel_event ()) != NULL) | |
400 | ++ev_cnt; | |
401 | ||
402 | if (is_nonblocking () || (flags & MSG_DONTWAIT)) | |
403 | timeout = 0; | |
404 | else if (event_mask & FD_READ) | |
405 | timeout = rcvtimeo (); | |
406 | else if (event_mask & FD_WRITE) | |
407 | timeout = sndtimeo (); | |
408 | else | |
409 | timeout = INFINITE; | |
410 | ||
411 | while (!(ret = evaluate_events (event_mask, events, !(flags & MSG_PEEK))) | |
412 | && !events) | |
413 | { | |
414 | if (timeout == 0) | |
415 | { | |
416 | WSASetLastError (WSAEWOULDBLOCK); | |
417 | return SOCKET_ERROR; | |
418 | } | |
419 | ||
420 | if (timeout < wfmo_timeout) | |
421 | wfmo_timeout = timeout; | |
422 | switch (WSAWaitForMultipleEvents (ev_cnt, ev, FALSE, wfmo_timeout, FALSE)) | |
423 | { | |
424 | case WSA_WAIT_TIMEOUT: | |
425 | case WSA_WAIT_EVENT_0: | |
426 | if (timeout != INFINITE) | |
427 | timeout -= wfmo_timeout; | |
428 | break; | |
429 | ||
430 | case WSA_WAIT_EVENT_0 + 1: | |
431 | if (_my_tls.call_signal_handler ()) | |
432 | break; | |
433 | WSASetLastError (WSAEINTR); | |
434 | return SOCKET_ERROR; | |
435 | ||
436 | case WSA_WAIT_EVENT_0 + 2: | |
437 | pthread::static_cancel_self (); | |
438 | break; | |
439 | ||
440 | default: | |
441 | /* wsock_evt can be NULL. We're generating the same errno values | |
442 | as for sockets on which shutdown has been called. */ | |
443 | if (WSAGetLastError () != WSA_INVALID_HANDLE) | |
444 | WSASetLastError (WSAEFAULT); | |
445 | else | |
446 | WSASetLastError ((event_mask & FD_CONNECT) ? WSAECONNRESET | |
447 | : WSAEINVAL); | |
448 | return SOCKET_ERROR; | |
449 | } | |
450 | } | |
451 | return ret; | |
452 | } | |
453 | ||
454 | void | |
455 | fhandler_socket_wsock::release_events () | |
456 | { | |
457 | if (WaitForSingleObject (wsock_mtx, INFINITE) != WAIT_FAILED) | |
458 | { | |
459 | HANDLE evt = wsock_evt; | |
460 | HANDLE mtx = wsock_mtx; | |
461 | ||
462 | wsock_evt = wsock_mtx = NULL; | |
463 | ReleaseMutex (mtx); | |
464 | NtClose (evt); | |
465 | NtClose (mtx); | |
466 | } | |
467 | } | |
468 | ||
469 | void | |
470 | fhandler_socket_wsock::set_close_on_exec (bool val) | |
471 | { | |
472 | set_no_inheritance (wsock_mtx, val); | |
473 | set_no_inheritance (wsock_evt, val); | |
474 | if (need_fixup_before ()) | |
475 | { | |
476 | close_on_exec (val); | |
477 | debug_printf ("set close_on_exec for %s to %d", get_name (), val); | |
478 | } | |
479 | else | |
480 | fhandler_base::set_close_on_exec (val); | |
481 | } | |
482 | ||
483 | /* Called if a freshly created socket is not inheritable. In that case we | |
484 | have to use fixup_before_fork_exec. See comment in set_socket_handle for | |
485 | a description of the problem. */ | |
486 | void | |
487 | fhandler_socket_wsock::init_fixup_before () | |
488 | { | |
489 | prot_info_ptr = (LPWSAPROTOCOL_INFOW) | |
490 | cmalloc_abort (HEAP_BUF, sizeof (WSAPROTOCOL_INFOW)); | |
491 | cygheap->fdtab.inc_need_fixup_before (); | |
492 | } | |
493 | ||
494 | int | |
495 | fhandler_socket_wsock::fixup_before_fork_exec (DWORD win_pid) | |
496 | { | |
497 | SOCKET ret = WSADuplicateSocketW (get_socket (), win_pid, prot_info_ptr); | |
498 | if (ret) | |
499 | set_winsock_errno (); | |
500 | else | |
501 | debug_printf ("WSADuplicateSocket succeeded (%x)", prot_info_ptr->dwProviderReserved); | |
502 | return (int) ret; | |
503 | } | |
504 | ||
505 | void | |
506 | fhandler_socket_wsock::fixup_after_fork (HANDLE parent) | |
507 | { | |
508 | fork_fixup (parent, wsock_mtx, "wsock_mtx"); | |
509 | fork_fixup (parent, wsock_evt, "wsock_evt"); | |
510 | ||
511 | if (!need_fixup_before ()) | |
512 | { | |
513 | fhandler_base::fixup_after_fork (parent); | |
514 | return; | |
515 | } | |
516 | ||
517 | SOCKET new_sock = WSASocketW (FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, | |
518 | FROM_PROTOCOL_INFO, prot_info_ptr, 0, | |
519 | WSA_FLAG_OVERLAPPED); | |
520 | if (new_sock == INVALID_SOCKET) | |
521 | { | |
522 | set_winsock_errno (); | |
a9c661a9 | 523 | set_handle ((HANDLE) INVALID_SOCKET); |
b79018ee CV |
524 | } |
525 | else | |
526 | { | |
527 | /* Even though the original socket was not inheritable, the duplicated | |
528 | socket is potentially inheritable again. */ | |
529 | SetHandleInformation ((HANDLE) new_sock, HANDLE_FLAG_INHERIT, 0); | |
a9c661a9 | 530 | set_handle ((HANDLE) new_sock); |
b79018ee CV |
531 | debug_printf ("WSASocket succeeded (%p)", new_sock); |
532 | } | |
533 | } | |
534 | ||
535 | void | |
536 | fhandler_socket_wsock::fixup_after_exec () | |
537 | { | |
538 | if (need_fixup_before () && !close_on_exec ()) | |
729cb70b | 539 | fixup_after_fork (NULL); /* No parent handle required. */ |
b79018ee CV |
540 | } |
541 | ||
542 | int | |
543 | fhandler_socket_wsock::dup (fhandler_base *child, int flags) | |
544 | { | |
545 | debug_printf ("here"); | |
546 | fhandler_socket_wsock *fhs = (fhandler_socket_wsock *) child; | |
547 | ||
548 | if (!DuplicateHandle (GetCurrentProcess (), wsock_mtx, | |
549 | GetCurrentProcess (), &fhs->wsock_mtx, | |
550 | 0, TRUE, DUPLICATE_SAME_ACCESS)) | |
551 | { | |
552 | __seterrno (); | |
553 | return -1; | |
554 | } | |
555 | if (!DuplicateHandle (GetCurrentProcess (), wsock_evt, | |
556 | GetCurrentProcess (), &fhs->wsock_evt, | |
557 | 0, TRUE, DUPLICATE_SAME_ACCESS)) | |
558 | { | |
559 | __seterrno (); | |
560 | NtClose (fhs->wsock_mtx); | |
561 | return -1; | |
562 | } | |
563 | if (!need_fixup_before ()) | |
564 | { | |
565 | int ret = fhandler_base::dup (child, flags); | |
566 | if (ret) | |
567 | { | |
568 | NtClose (fhs->wsock_evt); | |
569 | NtClose (fhs->wsock_mtx); | |
570 | } | |
571 | return ret; | |
572 | } | |
573 | ||
574 | cygheap->user.deimpersonate (); | |
575 | fhs->init_fixup_before (); | |
a9c661a9 | 576 | fhs->set_handle (get_handle ()); |
b79018ee CV |
577 | int ret = fhs->fixup_before_fork_exec (GetCurrentProcessId ()); |
578 | cygheap->user.reimpersonate (); | |
579 | if (!ret) | |
580 | { | |
581 | fhs->fixup_after_fork (GetCurrentProcess ()); | |
a9c661a9 | 582 | if (fhs->get_handle() != (HANDLE) INVALID_SOCKET) |
b79018ee CV |
583 | return 0; |
584 | } | |
585 | cygheap->fdtab.dec_need_fixup_before (); | |
586 | NtClose (fhs->wsock_evt); | |
587 | NtClose (fhs->wsock_mtx); | |
588 | return -1; | |
589 | } | |
590 | ||
591 | int | |
592 | fhandler_socket_wsock::set_socket_handle (SOCKET sock, int af, int type, | |
593 | int flags) | |
594 | { | |
595 | DWORD hdl_flags; | |
596 | bool lsp_fixup = false; | |
53590369 | 597 | int file_flags = O_RDWR | O_BINARY; |
b79018ee CV |
598 | |
599 | /* Usually sockets are inheritable IFS objects. Unfortunately some virus | |
600 | scanners or other network-oriented software replace normal sockets | |
601 | with their own kind, which is running through a filter driver called | |
602 | "layered service provider" (LSP) which, fortunately, are deprecated. | |
603 | ||
604 | LSP sockets are not kernel objects. They are typically not marked as | |
605 | inheritable, nor are they IFS handles. They are in fact not inheritable | |
606 | to child processes, and it does not help to mark them inheritable via | |
607 | SetHandleInformation. Subsequent socket calls in the child process fail | |
608 | with error 10038, WSAENOTSOCK. | |
609 | ||
610 | There's a neat way to workaround these annoying LSP sockets. WSAIoctl | |
611 | allows to fetch the underlying base socket, which is a normal, inheritable | |
612 | IFS handle. So we fetch the base socket, duplicate it, and close the | |
613 | original socket. Now we have a standard IFS socket which (hopefully) | |
614 | works as expected. | |
615 | ||
616 | If that doesn't work for some reason, mark the sockets for duplication | |
617 | via WSADuplicateSocket/WSASocket. This requires to start the child | |
618 | process in SUSPENDED state so we only do this if really necessary. */ | |
619 | if (!GetHandleInformation ((HANDLE) sock, &hdl_flags) | |
620 | || !(hdl_flags & HANDLE_FLAG_INHERIT)) | |
621 | { | |
622 | int ret; | |
623 | SOCKET base_sock; | |
624 | DWORD bret; | |
625 | ||
626 | lsp_fixup = true; | |
627 | debug_printf ("LSP handle: %p", sock); | |
628 | ret = WSAIoctl (sock, SIO_BASE_HANDLE, NULL, 0, (void *) &base_sock, | |
629 | sizeof (base_sock), &bret, NULL, NULL); | |
630 | if (ret) | |
631 | debug_printf ("WSAIoctl: %u", WSAGetLastError ()); | |
632 | else if (base_sock != sock) | |
633 | { | |
634 | if (GetHandleInformation ((HANDLE) base_sock, &hdl_flags) | |
635 | && (flags & HANDLE_FLAG_INHERIT)) | |
636 | { | |
637 | if (!DuplicateHandle (GetCurrentProcess (), (HANDLE) base_sock, | |
638 | GetCurrentProcess (), (PHANDLE) &base_sock, | |
639 | 0, TRUE, DUPLICATE_SAME_ACCESS)) | |
640 | debug_printf ("DuplicateHandle failed, %E"); | |
641 | else | |
642 | { | |
643 | ::closesocket (sock); | |
644 | sock = base_sock; | |
645 | lsp_fixup = false; | |
646 | } | |
647 | } | |
648 | } | |
649 | } | |
a9c661a9 | 650 | set_handle ((HANDLE) sock); |
b79018ee CV |
651 | set_addr_family (af); |
652 | set_socket_type (type); | |
b79018ee CV |
653 | if (!init_events ()) |
654 | return -1; | |
53590369 CV |
655 | if (flags & SOCK_NONBLOCK) |
656 | file_flags |= O_NONBLOCK; | |
657 | if (flags & SOCK_CLOEXEC) | |
658 | { | |
659 | set_close_on_exec (true); | |
660 | file_flags |= O_CLOEXEC; | |
661 | } | |
662 | set_flags (file_flags); | |
b79018ee CV |
663 | if (lsp_fixup) |
664 | init_fixup_before (); | |
b79018ee CV |
665 | set_unique_id (); |
666 | if (get_socket_type () == SOCK_DGRAM) | |
667 | { | |
668 | /* Workaround the problem that a missing listener on a UDP socket | |
669 | in a call to sendto will result in select/WSAEnumNetworkEvents | |
670 | reporting that the socket has pending data and a subsequent call | |
671 | to recvfrom will return -1 with error set to WSAECONNRESET. | |
672 | ||
673 | This problem is a regression introduced in Windows 2000. | |
674 | Instead of fixing the problem, a new socket IOCTL code has | |
675 | been added, see http://support.microsoft.com/kb/263823 */ | |
676 | BOOL cr = FALSE; | |
677 | DWORD blen; | |
678 | if (WSAIoctl (sock, SIO_UDP_CONNRESET, &cr, sizeof cr, NULL, 0, | |
679 | &blen, NULL, NULL) == SOCKET_ERROR) | |
680 | debug_printf ("Reset SIO_UDP_CONNRESET: WinSock error %u", | |
681 | WSAGetLastError ()); | |
682 | } | |
683 | #ifdef __x86_64__ | |
684 | rmem () = 212992; | |
685 | wmem () = 212992; | |
686 | #else | |
687 | rmem () = 64512; | |
688 | wmem () = 64512; | |
689 | #endif | |
690 | return 0; | |
691 | } | |
692 | ||
859d215b | 693 | fhandler_socket_inet::fhandler_socket_inet () : |
9c84bfd4 | 694 | fhandler_socket_wsock (), |
0feb77c2 | 695 | oobinline (false), |
8ccffddc CV |
696 | tcp_fastopen (false), |
697 | tcp_keepidle (7200), /* WinSock default */ | |
698 | tcp_keepcnt (10), /* WinSock default */ | |
699 | tcp_keepintvl (1) /* WinSock default */ | |
859d215b CV |
700 | { |
701 | } | |
702 | ||
703 | fhandler_socket_inet::~fhandler_socket_inet () | |
704 | { | |
705 | } | |
706 | ||
707 | int | |
708 | fhandler_socket_inet::socket (int af, int type, int protocol, int flags) | |
709 | { | |
710 | SOCKET sock; | |
711 | int ret; | |
712 | ||
d35bd229 CV |
713 | /* This test should be covered by ::socket, but make sure we don't |
714 | accidentally try anything else. */ | |
715 | if (type != SOCK_STREAM && type != SOCK_DGRAM && type != SOCK_RAW) | |
716 | { | |
717 | set_errno (EINVAL); | |
718 | return -1; | |
719 | } | |
859d215b CV |
720 | sock = ::socket (af, type, protocol); |
721 | if (sock == INVALID_SOCKET) | |
722 | { | |
723 | set_winsock_errno (); | |
724 | return -1; | |
725 | } | |
726 | ret = set_socket_handle (sock, af, type, flags); | |
727 | if (ret < 0) | |
728 | ::closesocket (sock); | |
729 | return ret; | |
730 | } | |
731 | ||
1e5e44a9 CV |
732 | int |
733 | fhandler_socket_inet::socketpair (int af, int type, int protocol, int flags, | |
734 | fhandler_socket *fh_out) | |
735 | { | |
736 | set_errno (EAFNOSUPPORT); | |
737 | return -1; | |
738 | } | |
739 | ||
859d215b CV |
740 | int |
741 | fhandler_socket_inet::bind (const struct sockaddr *name, int namelen) | |
742 | { | |
743 | int res = -1; | |
744 | ||
745 | if (!saw_reuseaddr ()) | |
746 | { | |
747 | /* If the application didn't explicitely request SO_REUSEADDR, | |
748 | enforce POSIX standard socket binding behaviour by setting the | |
749 | SO_EXCLUSIVEADDRUSE socket option. See cygwin_setsockopt() | |
750 | for a more detailed description. */ | |
751 | int on = 1; | |
752 | int ret = ::setsockopt (get_socket (), SOL_SOCKET, | |
753 | SO_EXCLUSIVEADDRUSE, | |
754 | (const char *) &on, sizeof on); | |
755 | debug_printf ("%d = setsockopt(SO_EXCLUSIVEADDRUSE), %E", ret); | |
756 | } | |
757 | if (::bind (get_socket (), name, namelen)) | |
758 | set_winsock_errno (); | |
759 | else | |
760 | res = 0; | |
761 | ||
762 | return res; | |
763 | } | |
764 | ||
765 | int | |
766 | fhandler_socket_inet::connect (const struct sockaddr *name, int namelen) | |
767 | { | |
768 | struct sockaddr_storage sst; | |
769 | ||
770 | if (get_inet_addr_inet (name, namelen, &sst, &namelen) == SOCKET_ERROR) | |
771 | return SOCKET_ERROR; | |
772 | ||
773 | /* Initialize connect state to "connect_pending". State is ultimately set | |
774 | to "connected" or "connect_failed" in wait_for_events when the FD_CONNECT | |
775 | event occurs. Note that the underlying OS sockets are always non-blocking | |
776 | and a successfully initiated non-blocking Winsock connect always returns | |
777 | WSAEWOULDBLOCK. Thus it's safe to rely on event handling. | |
778 | ||
779 | Check for either unconnected or connect_failed since in both cases it's | |
780 | allowed to retry connecting the socket. It's also ok (albeit ugly) to | |
781 | call connect to check if a previous non-blocking connect finished. | |
782 | ||
783 | Set connect_state before calling connect, otherwise a race condition with | |
784 | an already running select or poll might occur. */ | |
785 | if (connect_state () == unconnected || connect_state () == connect_failed) | |
786 | connect_state (connect_pending); | |
787 | ||
788 | int res = ::connect (get_socket (), (struct sockaddr *) &sst, namelen); | |
789 | if (!is_nonblocking () | |
790 | && res == SOCKET_ERROR | |
791 | && WSAGetLastError () == WSAEWOULDBLOCK) | |
792 | res = wait_for_events (FD_CONNECT | FD_CLOSE, 0); | |
793 | ||
794 | if (res) | |
795 | { | |
796 | DWORD err = WSAGetLastError (); | |
8906a4d3 | 797 | |
859d215b CV |
798 | /* Some applications use the ugly technique to check if a non-blocking |
799 | connect succeeded by calling connect again, until it returns EISCONN. | |
800 | This circumvents the event handling and connect_state is never set. | |
801 | Thus we check for this situation here. */ | |
802 | if (err == WSAEISCONN) | |
803 | connect_state (connected); | |
804 | /* Winsock returns WSAEWOULDBLOCK if the non-blocking socket cannot be | |
805 | conected immediately. Convert to POSIX/Linux compliant EINPROGRESS. */ | |
806 | else if (is_nonblocking () && err == WSAEWOULDBLOCK) | |
807 | WSASetLastError (WSAEINPROGRESS); | |
808 | /* Winsock returns WSAEINVAL if the socket is already a listener. | |
8906a4d3 | 809 | Convert to POSIX/Linux compliant EISCONN. */ |
859d215b CV |
810 | else if (err == WSAEINVAL && connect_state () == listener) |
811 | WSASetLastError (WSAEISCONN); | |
812 | /* Any other error except WSAEALREADY during connect_pending means the | |
813 | connect failed. */ | |
814 | else if (connect_state () == connect_pending && err != WSAEALREADY) | |
8906a4d3 | 815 | connect_state (connect_failed); |
859d215b CV |
816 | set_winsock_errno (); |
817 | } | |
818 | ||
819 | return res; | |
820 | } | |
821 | ||
822 | int | |
823 | fhandler_socket_inet::listen (int backlog) | |
824 | { | |
825 | int res = ::listen (get_socket (), backlog); | |
826 | if (res && WSAGetLastError () == WSAEINVAL) | |
827 | { | |
828 | /* It's perfectly valid to call listen on an unbound INET socket. | |
829 | In this case the socket is automatically bound to an unused | |
830 | port number, listening on all interfaces. On WinSock, listen | |
831 | fails with WSAEINVAL when it's called on an unbound socket. | |
832 | So we have to bind manually here to have POSIX semantics. */ | |
833 | if (get_addr_family () == AF_INET) | |
834 | { | |
835 | struct sockaddr_in sin; | |
836 | sin.sin_family = AF_INET; | |
837 | sin.sin_port = 0; | |
838 | sin.sin_addr.s_addr = INADDR_ANY; | |
839 | if (!::bind (get_socket (), (struct sockaddr *) &sin, sizeof sin)) | |
840 | res = ::listen (get_socket (), backlog); | |
841 | } | |
842 | else if (get_addr_family () == AF_INET6) | |
843 | { | |
844 | struct sockaddr_in6 sin6; | |
845 | memset (&sin6, 0, sizeof sin6); | |
846 | sin6.sin6_family = AF_INET6; | |
847 | if (!::bind (get_socket (), (struct sockaddr *) &sin6, sizeof sin6)) | |
848 | res = ::listen (get_socket (), backlog); | |
849 | } | |
850 | } | |
851 | if (!res) | |
852 | connect_state (listener); /* gets set to connected on accepted socket. */ | |
853 | else | |
854 | set_winsock_errno (); | |
855 | return res; | |
856 | } | |
857 | ||
858 | int | |
859 | fhandler_socket_inet::accept4 (struct sockaddr *peer, int *len, int flags) | |
860 | { | |
861 | int ret = -1; | |
862 | /* Allows NULL peer and len parameters. */ | |
863 | struct sockaddr_storage lpeer; | |
864 | int llen = sizeof (struct sockaddr_storage); | |
865 | ||
866 | /* Windows event handling does not check for the validity of the desired | |
867 | flags so we have to do it here. */ | |
868 | if (connect_state () != listener) | |
869 | { | |
870 | WSASetLastError (WSAEINVAL); | |
871 | set_winsock_errno (); | |
872 | return -1; | |
873 | } | |
874 | ||
875 | SOCKET res = INVALID_SOCKET; | |
876 | while (!(res = wait_for_events (FD_ACCEPT | FD_CLOSE, 0)) | |
877 | && (res = ::accept (get_socket (), (struct sockaddr *) &lpeer, &llen)) | |
878 | == INVALID_SOCKET | |
879 | && WSAGetLastError () == WSAEWOULDBLOCK) | |
880 | ; | |
881 | if (res == INVALID_SOCKET) | |
882 | set_winsock_errno (); | |
883 | else | |
884 | { | |
885 | cygheap_fdnew fd; | |
886 | ||
887 | if (fd >= 0) | |
888 | { | |
889 | fhandler_socket_inet *sock = (fhandler_socket_inet *) | |
890 | build_fh_dev (dev ()); | |
891 | if (sock && sock->set_socket_handle (res, get_addr_family (), | |
892 | get_socket_type (), | |
cfe5d362 | 893 | get_socket_flags ()) == 0) |
859d215b CV |
894 | { |
895 | sock->async_io (false); /* set_socket_handle disables async. */ | |
896 | /* No locking necessary at this point. */ | |
897 | sock->wsock_events->events = wsock_events->events | FD_WRITE; | |
898 | sock->wsock_events->owner = wsock_events->owner; | |
899 | sock->connect_state (connected); | |
900 | fd = sock; | |
901 | if (fd <= 2) | |
902 | set_std_handle (fd); | |
903 | ret = fd; | |
904 | if (peer) | |
905 | { | |
906 | memcpy (peer, &lpeer, MIN (*len, llen)); | |
907 | *len = llen; | |
908 | } | |
909 | } | |
00e87078 | 910 | else |
2bbe8697 | 911 | delete sock; |
859d215b CV |
912 | } |
913 | if (ret == -1) | |
914 | ::closesocket (res); | |
915 | } | |
916 | return ret; | |
917 | } | |
918 | ||
919 | int | |
920 | fhandler_socket_inet::getsockname (struct sockaddr *name, int *namelen) | |
921 | { | |
922 | int res = -1; | |
923 | ||
924 | /* WinSock just returns WSAEFAULT if the buffer is too small. Use a | |
925 | big enough local buffer and truncate later as necessary, per POSIX. */ | |
926 | struct sockaddr_storage sock; | |
927 | int len = sizeof sock; | |
928 | res = ::getsockname (get_socket (), (struct sockaddr *) &sock, &len); | |
929 | if (!res) | |
930 | { | |
931 | memcpy (name, &sock, MIN (*namelen, len)); | |
932 | *namelen = len; | |
933 | } | |
934 | else | |
935 | { | |
936 | if (WSAGetLastError () == WSAEINVAL) | |
937 | { | |
938 | /* WinSock returns WSAEINVAL if the socket is locally | |
939 | unbound. Per SUSv3 this is not an error condition. | |
940 | We're faking a valid return value here by creating the | |
941 | same content in the sockaddr structure as on Linux. */ | |
942 | memset (&sock, 0, sizeof sock); | |
943 | sock.ss_family = get_addr_family (); | |
944 | switch (get_addr_family ()) | |
945 | { | |
946 | case AF_INET: | |
947 | res = 0; | |
948 | len = (int) sizeof (struct sockaddr_in); | |
949 | break; | |
950 | case AF_INET6: | |
951 | res = 0; | |
952 | len = (int) sizeof (struct sockaddr_in6); | |
953 | break; | |
954 | default: | |
955 | WSASetLastError (WSAEOPNOTSUPP); | |
956 | break; | |
957 | } | |
958 | if (!res) | |
959 | { | |
960 | memcpy (name, &sock, MIN (*namelen, len)); | |
961 | *namelen = len; | |
962 | } | |
963 | } | |
964 | if (res) | |
965 | set_winsock_errno (); | |
966 | } | |
967 | return res; | |
968 | } | |
969 | ||
970 | int | |
971 | fhandler_socket_inet::getpeername (struct sockaddr *name, int *namelen) | |
972 | { | |
973 | /* Always use a local big enough buffer and truncate later as necessary | |
974 | per POSIX. WinSock unfortunately only returns WSAEFAULT if the buffer | |
975 | is too small. */ | |
976 | struct sockaddr_storage sock; | |
977 | int len = sizeof sock; | |
978 | int res = ::getpeername (get_socket (), (struct sockaddr *) &sock, &len); | |
979 | if (res) | |
980 | set_winsock_errno (); | |
981 | else | |
982 | { | |
983 | memcpy (name, &sock, MIN (*namelen, len)); | |
984 | *namelen = len; | |
985 | } | |
986 | return res; | |
987 | } | |
988 | ||
233bde31 | 989 | int |
b79018ee | 990 | fhandler_socket_wsock::shutdown (int how) |
859d215b | 991 | { |
233bde31 CV |
992 | int res = ::shutdown (get_socket (), how); |
993 | ||
994 | /* Linux allows to call shutdown for any socket, even if it's not connected. | |
995 | This also disables to call accept on this socket, if shutdown has been | |
996 | called with the SHUT_RD or SHUT_RDWR parameter. In contrast, WinSock | |
997 | only allows to call shutdown on a connected socket. The accept function | |
998 | is in no way affected. So, what we do here is to fake success, and to | |
999 | change the event settings so that an FD_CLOSE event is triggered for the | |
1000 | calling Cygwin function. The evaluate_events method handles the call | |
1001 | from accept specially to generate a Linux-compatible behaviour. */ | |
1002 | if (res && WSAGetLastError () != WSAENOTCONN) | |
1003 | set_winsock_errno (); | |
1004 | else | |
1005 | { | |
1006 | res = 0; | |
1007 | switch (how) | |
1008 | { | |
1009 | case SHUT_RD: | |
1010 | saw_shutdown_read (true); | |
1011 | wsock_events->events |= FD_CLOSE; | |
1012 | SetEvent (wsock_evt); | |
1013 | break; | |
1014 | case SHUT_WR: | |
1015 | saw_shutdown_write (true); | |
1016 | break; | |
1017 | case SHUT_RDWR: | |
1018 | saw_shutdown_read (true); | |
1019 | saw_shutdown_write (true); | |
1020 | wsock_events->events |= FD_CLOSE; | |
1021 | SetEvent (wsock_evt); | |
1022 | break; | |
1023 | } | |
1024 | } | |
1025 | return res; | |
1026 | } | |
1027 | ||
1028 | int | |
b79018ee | 1029 | fhandler_socket_wsock::close () |
233bde31 CV |
1030 | { |
1031 | int res = 0; | |
1032 | ||
1033 | release_events (); | |
1034 | while ((res = ::closesocket (get_socket ())) != 0) | |
1035 | { | |
1036 | if (WSAGetLastError () != WSAEWOULDBLOCK) | |
1037 | { | |
1038 | set_winsock_errno (); | |
1039 | res = -1; | |
1040 | break; | |
1041 | } | |
1042 | if (cygwait (10) == WAIT_SIGNALED) | |
1043 | { | |
1044 | set_errno (EINTR); | |
1045 | res = -1; | |
1046 | break; | |
1047 | } | |
1048 | WSASetLastError (0); | |
1049 | } | |
233bde31 | 1050 | return res; |
859d215b CV |
1051 | } |
1052 | ||
b79018ee | 1053 | ssize_t |
859d215b CV |
1054 | fhandler_socket_inet::recv_internal (LPWSAMSG wsamsg, bool use_recvmsg) |
1055 | { | |
1056 | ssize_t res = 0; | |
1057 | DWORD ret = 0, wret; | |
9c84bfd4 | 1058 | int evt_mask = (wsamsg->dwFlags & MSG_OOB) ? FD_OOB : FD_READ; |
859d215b CV |
1059 | LPWSABUF &wsabuf = wsamsg->lpBuffers; |
1060 | ULONG &wsacnt = wsamsg->dwBufferCount; | |
1061 | static NO_COPY LPFN_WSARECVMSG WSARecvMsg; | |
9c84bfd4 | 1062 | bool read_oob = false; |
859d215b CV |
1063 | |
1064 | /* CV 2014-10-26: Do not check for the connect_state at this point. In | |
1065 | certain scenarios there's no way to check the connect state reliably. | |
1066 | Example (hexchat): Parent process creates socket, forks, child process | |
1067 | calls connect, parent process calls read. Even if the event handling | |
1068 | allows to check for FD_CONNECT in the parent, there is always yet another | |
1069 | scenario we can easily break. */ | |
1070 | ||
1071 | DWORD wait_flags = wsamsg->dwFlags; | |
1072 | bool waitall = !!(wait_flags & MSG_WAITALL); | |
1073 | wsamsg->dwFlags &= (MSG_OOB | MSG_PEEK | MSG_DONTROUTE); | |
1074 | if (use_recvmsg) | |
1075 | { | |
1076 | if (!WSARecvMsg | |
1077 | && get_ext_funcptr (get_socket (), &WSARecvMsg) == SOCKET_ERROR) | |
1078 | { | |
1079 | if (wsamsg->Control.len > 0) | |
1080 | { | |
1081 | set_winsock_errno (); | |
1082 | return SOCKET_ERROR; | |
1083 | } | |
1084 | use_recvmsg = false; | |
1085 | } | |
1086 | else /* Only MSG_PEEK is supported by WSARecvMsg. */ | |
1087 | wsamsg->dwFlags &= MSG_PEEK; | |
1088 | } | |
1089 | if (waitall) | |
1090 | { | |
1091 | if (get_socket_type () != SOCK_STREAM) | |
1092 | { | |
1093 | WSASetLastError (WSAEOPNOTSUPP); | |
1094 | set_winsock_errno (); | |
1095 | return SOCKET_ERROR; | |
1096 | } | |
1097 | if (is_nonblocking () || (wsamsg->dwFlags & (MSG_OOB | MSG_PEEK))) | |
1098 | waitall = false; | |
1099 | } | |
1100 | ||
9c84bfd4 TY |
1101 | /* recv() returns EINVAL if MSG_OOB flag is set in inline mode. */ |
1102 | if (oobinline && (wsamsg->dwFlags & MSG_OOB)) | |
1103 | { | |
1104 | set_errno (EINVAL); | |
1105 | return SOCKET_ERROR; | |
1106 | } | |
1107 | ||
1108 | /* Check whether OOB data is ready or not */ | |
1109 | if (get_socket_type () == SOCK_STREAM) | |
1110 | if ((wsamsg->dwFlags & MSG_OOB) || oobinline) | |
1111 | { | |
1112 | u_long atmark = 0; | |
1113 | #ifdef __x86_64__ | |
1114 | /* SIOCATMARK = _IOR('s',7,u_long) */ | |
1115 | int err = ::ioctlsocket (get_socket (), _IOR('s',7,u_long), &atmark); | |
1116 | #else | |
1117 | int err = ::ioctlsocket (get_socket (), SIOCATMARK, &atmark); | |
1118 | #endif | |
1119 | if (err) | |
1120 | { | |
1121 | set_winsock_errno (); | |
1122 | return SOCKET_ERROR; | |
1123 | } | |
1124 | /* If there is no OOB data, recv() with MSG_OOB returns EINVAL. | |
1125 | Note: The return value of SIOCATMARK in non-inline mode of | |
1126 | winsock is FALSE if OOB data exists, TRUE otherwise. */ | |
1127 | if (atmark && (wsamsg->dwFlags & MSG_OOB)) | |
1128 | { | |
1129 | /* No OOB data */ | |
1130 | set_errno (EINVAL); | |
1131 | return SOCKET_ERROR; | |
1132 | } | |
1133 | /* Inline mode for out-of-band (OOB) data of winsock is | |
1134 | completely broken. That is, SIOCATMARK always returns | |
1135 | TRUE in inline mode. Due to this problem, application | |
1136 | cannot determine OOB data at all. Therefore the behavior | |
1137 | of a socket with SO_OOBINLINE set is simulated using | |
1138 | a socket with SO_OOBINLINE not set. In this fake inline | |
1139 | mode, the order of the OOB and non-OOB data is not | |
1140 | preserved. OOB data is read before non-OOB data sent | |
1141 | prior to the OOB data. However, this most likely is | |
1142 | not a problem in most cases. */ | |
1143 | /* If there is OOB data, read OOB data using MSG_OOB in | |
1144 | fake inline mode. */ | |
1145 | if (!atmark && oobinline) | |
1146 | { | |
1147 | read_oob = true; | |
1148 | evt_mask = FD_OOB; | |
1149 | } | |
1150 | } | |
1151 | ||
859d215b CV |
1152 | /* Note: Don't call WSARecvFrom(MSG_PEEK) without actually having data |
1153 | waiting in the buffers, otherwise the event handling gets messed up | |
1154 | for some reason. */ | |
1155 | while (!(res = wait_for_events (evt_mask | FD_CLOSE, wait_flags)) | |
1156 | || saw_shutdown_read ()) | |
1157 | { | |
9c84bfd4 | 1158 | DWORD dwFlags = wsamsg->dwFlags | (read_oob ? MSG_OOB : 0); |
859d215b CV |
1159 | if (use_recvmsg) |
1160 | res = WSARecvMsg (get_socket (), wsamsg, &wret, NULL, NULL); | |
1161 | /* This is working around a really weird problem in WinSock. | |
1162 | ||
1163 | Assume you create a socket, fork the process (thus duplicating | |
1164 | the socket), connect the socket in the child, then call recv | |
1165 | on the original socket handle in the parent process. | |
1166 | In this scenario, calls to WinSock's recvfrom and WSARecvFrom | |
1167 | in the parent will fail with WSAEINVAL, regardless whether both | |
1168 | address parameters, name and namelen, are NULL or point to valid | |
1169 | storage. However, calls to recv and WSARecv succeed as expected. | |
1170 | Per MSDN, WSAEINVAL in the context of recv means "The socket has not | |
1171 | been bound". It is as if the recvfrom functions test if the socket | |
1172 | is bound locally, but in the parent process, WinSock doesn't know | |
1173 | about that and fails, while the same test is omitted in the recv | |
1174 | functions. | |
1175 | ||
1176 | This also covers another weird case: WinSock returns WSAEFAULT if | |
1177 | namelen is a valid pointer while name is NULL. Both parameters are | |
1178 | ignored for TCP sockets, so this only occurs when using UDP socket. */ | |
1179 | else if (!wsamsg->name || get_socket_type () == SOCK_STREAM) | |
9c84bfd4 | 1180 | res = WSARecv (get_socket (), wsabuf, wsacnt, &wret, &dwFlags, |
859d215b CV |
1181 | NULL, NULL); |
1182 | else | |
1183 | res = WSARecvFrom (get_socket (), wsabuf, wsacnt, &wret, | |
9c84bfd4 | 1184 | &dwFlags, wsamsg->name, &wsamsg->namelen, |
859d215b CV |
1185 | NULL, NULL); |
1186 | if (!res) | |
1187 | { | |
1188 | ret += wret; | |
1189 | if (!waitall) | |
1190 | break; | |
1191 | while (wret && wsacnt) | |
1192 | { | |
1193 | if (wsabuf->len > wret) | |
1194 | { | |
1195 | wsabuf->len -= wret; | |
1196 | wsabuf->buf += wret; | |
1197 | wret = 0; | |
1198 | } | |
1199 | else | |
1200 | { | |
1201 | wret -= wsabuf->len; | |
1202 | ++wsabuf; | |
1203 | --wsacnt; | |
1204 | } | |
1205 | } | |
1206 | if (!wret) | |
1207 | break; | |
1208 | } | |
1209 | else if (WSAGetLastError () != WSAEWOULDBLOCK) | |
1210 | break; | |
1211 | } | |
1212 | ||
1213 | if (res) | |
1214 | { | |
1215 | /* According to SUSv3, errno isn't set in that case and no error | |
1216 | condition is returned. */ | |
1217 | if (WSAGetLastError () == WSAEMSGSIZE) | |
1218 | ret += wret; | |
1219 | else if (!ret) | |
1220 | { | |
1221 | /* ESHUTDOWN isn't defined for recv in SUSv3. Simply EOF is returned | |
1222 | in this case. */ | |
1223 | if (WSAGetLastError () == WSAESHUTDOWN) | |
1224 | ret = 0; | |
1225 | else | |
1226 | { | |
1227 | set_winsock_errno (); | |
1228 | return SOCKET_ERROR; | |
1229 | } | |
1230 | } | |
1231 | } | |
1232 | ||
1233 | return ret; | |
1234 | } | |
1235 | ||
1236 | ssize_t | |
b79018ee CV |
1237 | fhandler_socket_wsock::recvfrom (void *in_ptr, size_t len, int flags, |
1238 | struct sockaddr *from, int *fromlen) | |
859d215b CV |
1239 | { |
1240 | char *ptr = (char *) in_ptr; | |
1241 | ||
1242 | #ifdef __x86_64__ | |
1243 | /* size_t is 64 bit, but the len member in WSABUF is 32 bit. | |
1244 | Split buffer if necessary. */ | |
1245 | DWORD bufcnt = len / UINT32_MAX + ((!len || (len % UINT32_MAX)) ? 1 : 0); | |
1246 | WSABUF wsabuf[bufcnt]; | |
1247 | WSAMSG wsamsg = { from, from && fromlen ? *fromlen : 0, | |
1248 | wsabuf, bufcnt, | |
1249 | { 0, NULL }, | |
1250 | (DWORD) flags }; | |
1251 | /* Don't use len as loop condition, it could be 0. */ | |
1252 | for (WSABUF *wsaptr = wsabuf; bufcnt--; ++wsaptr) | |
1253 | { | |
1254 | wsaptr->len = MIN (len, UINT32_MAX); | |
1255 | wsaptr->buf = ptr; | |
1256 | len -= wsaptr->len; | |
1257 | ptr += wsaptr->len; | |
1258 | } | |
1259 | #else | |
1260 | WSABUF wsabuf = { len, ptr }; | |
1261 | WSAMSG wsamsg = { from, from && fromlen ? *fromlen : 0, | |
1262 | &wsabuf, 1, | |
1263 | { 0, NULL}, | |
1264 | (DWORD) flags }; | |
1265 | #endif | |
1266 | ssize_t ret = recv_internal (&wsamsg, false); | |
1267 | if (fromlen) | |
1268 | *fromlen = wsamsg.namelen; | |
1269 | return ret; | |
1270 | } | |
1271 | ||
1272 | ssize_t | |
b79018ee | 1273 | fhandler_socket_wsock::recvmsg (struct msghdr *msg, int flags) |
859d215b CV |
1274 | { |
1275 | /* Disappointing but true: Even if WSARecvMsg is supported, it's only | |
1276 | supported for datagram and raw sockets. */ | |
1277 | bool use_recvmsg = true; | |
1278 | if (get_socket_type () == SOCK_STREAM || get_addr_family () == AF_LOCAL) | |
1279 | { | |
1280 | use_recvmsg = false; | |
1281 | msg->msg_controllen = 0; | |
1282 | } | |
1283 | ||
1284 | WSABUF wsabuf[msg->msg_iovlen]; | |
1285 | WSABUF *wsaptr = wsabuf + msg->msg_iovlen; | |
1286 | const struct iovec *iovptr = msg->msg_iov + msg->msg_iovlen; | |
1287 | while (--wsaptr >= wsabuf) | |
1288 | { | |
1289 | wsaptr->len = (--iovptr)->iov_len; | |
1290 | wsaptr->buf = (char *) iovptr->iov_base; | |
1291 | } | |
1292 | WSAMSG wsamsg = { (struct sockaddr *) msg->msg_name, msg->msg_namelen, | |
1293 | wsabuf, (DWORD) msg->msg_iovlen, | |
1294 | { (DWORD) msg->msg_controllen, (char *) msg->msg_control }, | |
1295 | (DWORD) flags }; | |
1296 | ssize_t ret = recv_internal (&wsamsg, use_recvmsg); | |
1297 | if (ret >= 0) | |
1298 | { | |
1299 | msg->msg_namelen = wsamsg.namelen; | |
1300 | msg->msg_controllen = wsamsg.Control.len; | |
1301 | if (!CYGWIN_VERSION_CHECK_FOR_USING_ANCIENT_MSGHDR) | |
1302 | msg->msg_flags = wsamsg.dwFlags; | |
1303 | } | |
1304 | return ret; | |
1305 | } | |
1306 | ||
1307 | void __reg3 | |
b79018ee | 1308 | fhandler_socket_wsock::read (void *in_ptr, size_t& len) |
859d215b CV |
1309 | { |
1310 | char *ptr = (char *) in_ptr; | |
1311 | ||
1312 | #ifdef __x86_64__ | |
1313 | /* size_t is 64 bit, but the len member in WSABUF is 32 bit. | |
1314 | Split buffer if necessary. */ | |
1315 | DWORD bufcnt = len / UINT32_MAX + ((!len || (len % UINT32_MAX)) ? 1 : 0); | |
1316 | WSABUF wsabuf[bufcnt]; | |
1317 | WSAMSG wsamsg = { NULL, 0, wsabuf, bufcnt, { 0, NULL }, 0 }; | |
1318 | /* Don't use len as loop condition, it could be 0. */ | |
1319 | for (WSABUF *wsaptr = wsabuf; bufcnt--; ++wsaptr) | |
1320 | { | |
1321 | wsaptr->len = MIN (len, UINT32_MAX); | |
1322 | wsaptr->buf = ptr; | |
1323 | len -= wsaptr->len; | |
1324 | ptr += wsaptr->len; | |
1325 | } | |
1326 | #else | |
1327 | WSABUF wsabuf = { len, ptr }; | |
1328 | WSAMSG wsamsg = { NULL, 0, &wsabuf, 1, { 0, NULL }, 0 }; | |
1329 | #endif | |
8906a4d3 | 1330 | |
859d215b CV |
1331 | len = recv_internal (&wsamsg, false); |
1332 | } | |
1333 | ||
1334 | ssize_t | |
b79018ee CV |
1335 | fhandler_socket_wsock::readv (const struct iovec *const iov, const int iovcnt, |
1336 | ssize_t tot) | |
859d215b CV |
1337 | { |
1338 | WSABUF wsabuf[iovcnt]; | |
1339 | WSABUF *wsaptr = wsabuf + iovcnt; | |
1340 | const struct iovec *iovptr = iov + iovcnt; | |
1341 | while (--wsaptr >= wsabuf) | |
1342 | { | |
1343 | wsaptr->len = (--iovptr)->iov_len; | |
1344 | wsaptr->buf = (char *) iovptr->iov_base; | |
1345 | } | |
1346 | WSAMSG wsamsg = { NULL, 0, wsabuf, (DWORD) iovcnt, { 0, NULL}, 0 }; | |
1347 | return recv_internal (&wsamsg, false); | |
1348 | } | |
1349 | ||
b79018ee CV |
1350 | ssize_t |
1351 | fhandler_socket_wsock::send_internal (struct _WSAMSG *wsamsg, int flags) | |
859d215b CV |
1352 | { |
1353 | ssize_t res = 0; | |
1354 | DWORD ret = 0, sum = 0; | |
1355 | WSABUF out_buf[wsamsg->dwBufferCount]; | |
1356 | bool use_sendmsg = false; | |
1357 | DWORD wait_flags = flags & MSG_DONTWAIT; | |
1358 | bool nosignal = !!(flags & MSG_NOSIGNAL); | |
1359 | ||
f527171a CV |
1360 | /* MSG_EOR not supported by any protocol */ |
1361 | if (flags & MSG_EOR) | |
1362 | { | |
1363 | set_errno (EOPNOTSUPP); | |
1364 | return SOCKET_ERROR; | |
1365 | } | |
1366 | ||
859d215b CV |
1367 | flags &= (MSG_OOB | MSG_DONTROUTE); |
1368 | if (wsamsg->Control.len > 0) | |
1369 | use_sendmsg = true; | |
1370 | /* Workaround for MSDN KB 823764: Split a message into chunks <= SO_SNDBUF. | |
1371 | in_idx is the index of the current lpBuffers from the input wsamsg buffer. | |
1372 | in_off is used to keep track of the next byte to write from a wsamsg | |
1373 | buffer which only gets partially written. */ | |
1374 | for (DWORD in_idx = 0, in_off = 0; | |
1375 | in_idx < wsamsg->dwBufferCount; | |
1376 | in_off >= wsamsg->lpBuffers[in_idx].len && (++in_idx, in_off = 0)) | |
1377 | { | |
1378 | /* Split a message into the least number of pieces to minimize the | |
1379 | number of WsaSendTo calls. Don't split datagram messages (bad idea). | |
1380 | out_idx is the index of the next buffer in the out_buf WSABUF, | |
1381 | also the number of buffers given to WSASendTo. | |
1382 | out_len is the number of bytes in the buffers given to WSASendTo. | |
1383 | Don't split datagram messages (very bad idea). */ | |
1384 | DWORD out_idx = 0; | |
1385 | DWORD out_len = 0; | |
1386 | if (get_socket_type () == SOCK_STREAM) | |
1387 | { | |
1388 | do | |
1389 | { | |
1390 | out_buf[out_idx].buf = wsamsg->lpBuffers[in_idx].buf + in_off; | |
1391 | out_buf[out_idx].len = wsamsg->lpBuffers[in_idx].len - in_off; | |
1392 | out_len += out_buf[out_idx].len; | |
1393 | out_idx++; | |
1394 | } | |
1395 | while (out_len < (unsigned) wmem () | |
1396 | && (in_off = 0, ++in_idx < wsamsg->dwBufferCount)); | |
1397 | /* Tweak len of the last out_buf buffer so the entire number of bytes | |
1398 | is (less than or) equal to wmem (). Fix out_len as well since it's | |
1399 | used in a subsequent test expression. */ | |
1400 | if (out_len > (unsigned) wmem ()) | |
1401 | { | |
1402 | out_buf[out_idx - 1].len -= out_len - (unsigned) wmem (); | |
1403 | out_len = (unsigned) wmem (); | |
1404 | } | |
1405 | /* Add the bytes written from the current last buffer to in_off, | |
1406 | so in_off points to the next byte to be written from that buffer, | |
1407 | or beyond which lets the outper loop skip to the next buffer. */ | |
1408 | in_off += out_buf[out_idx - 1].len; | |
1409 | } | |
1410 | ||
1411 | do | |
1412 | { | |
1413 | if (use_sendmsg) | |
1414 | res = WSASendMsg (get_socket (), wsamsg, flags, &ret, NULL, NULL); | |
1415 | else if (get_socket_type () == SOCK_STREAM) | |
1416 | res = WSASendTo (get_socket (), out_buf, out_idx, &ret, flags, | |
1417 | wsamsg->name, wsamsg->namelen, NULL, NULL); | |
1418 | else | |
1419 | res = WSASendTo (get_socket (), wsamsg->lpBuffers, | |
1420 | wsamsg->dwBufferCount, &ret, flags, | |
1421 | wsamsg->name, wsamsg->namelen, NULL, NULL); | |
1422 | if (res && (WSAGetLastError () == WSAEWOULDBLOCK)) | |
1423 | { | |
1424 | LOCK_EVENTS; | |
1425 | wsock_events->events &= ~FD_WRITE; | |
1426 | UNLOCK_EVENTS; | |
1427 | } | |
1428 | } | |
1429 | while (res && (WSAGetLastError () == WSAEWOULDBLOCK) | |
1430 | && !(res = wait_for_events (FD_WRITE | FD_CLOSE, wait_flags))); | |
1431 | ||
1432 | if (!res) | |
1433 | { | |
1434 | sum += ret; | |
1435 | /* For streams, return to application if the number of bytes written | |
1436 | is less than the number of bytes we intended to write in a single | |
1437 | call to WSASendTo. Otherwise we would have to add code to | |
1438 | backtrack in the input buffers, which is questionable. There was | |
1439 | probably a good reason we couldn't write more. */ | |
1440 | if (get_socket_type () != SOCK_STREAM || ret < out_len) | |
1441 | break; | |
1442 | } | |
1443 | else if (is_nonblocking () || WSAGetLastError() != WSAEWOULDBLOCK) | |
1444 | break; | |
1445 | } | |
1446 | ||
1447 | if (sum) | |
1448 | res = sum; | |
1449 | else if (res == SOCKET_ERROR) | |
1450 | { | |
1451 | set_winsock_errno (); | |
1452 | ||
1453 | /* Special handling for EPIPE and SIGPIPE. | |
1454 | ||
1455 | EPIPE is generated if the local end has been shut down on a connection | |
1456 | oriented socket. In this case the process will also receive a SIGPIPE | |
1457 | unless MSG_NOSIGNAL is set. */ | |
1458 | if ((get_errno () == ECONNABORTED || get_errno () == ESHUTDOWN) | |
1459 | && get_socket_type () == SOCK_STREAM) | |
1460 | { | |
1461 | set_errno (EPIPE); | |
1462 | if (!nosignal) | |
1463 | raise (SIGPIPE); | |
1464 | } | |
1465 | } | |
1466 | ||
1467 | return res; | |
1468 | } | |
1469 | ||
1470 | ssize_t | |
1471 | fhandler_socket_inet::sendto (const void *in_ptr, size_t len, int flags, | |
1472 | const struct sockaddr *to, int tolen) | |
1473 | { | |
1474 | char *ptr = (char *) in_ptr; | |
1475 | struct sockaddr_storage sst; | |
1476 | ||
1477 | if (to && get_inet_addr_inet (to, tolen, &sst, &tolen) == SOCKET_ERROR) | |
1478 | return SOCKET_ERROR; | |
1479 | ||
1480 | #ifdef __x86_64__ | |
1481 | /* size_t is 64 bit, but the len member in WSABUF is 32 bit. | |
1482 | Split buffer if necessary. */ | |
1483 | DWORD bufcnt = len / UINT32_MAX + ((!len || (len % UINT32_MAX)) ? 1 : 0); | |
1484 | WSABUF wsabuf[bufcnt]; | |
1485 | WSAMSG wsamsg = { to ? (struct sockaddr *) &sst : NULL, tolen, | |
1486 | wsabuf, bufcnt, | |
1487 | { 0, NULL }, | |
1488 | 0 }; | |
1489 | /* Don't use len as loop condition, it could be 0. */ | |
1490 | for (WSABUF *wsaptr = wsabuf; bufcnt--; ++wsaptr) | |
1491 | { | |
1492 | wsaptr->len = MIN (len, UINT32_MAX); | |
1493 | wsaptr->buf = ptr; | |
1494 | len -= wsaptr->len; | |
1495 | ptr += wsaptr->len; | |
1496 | } | |
1497 | #else | |
1498 | WSABUF wsabuf = { len, ptr }; | |
1499 | WSAMSG wsamsg = { to ? (struct sockaddr *) &sst : NULL, tolen, | |
1500 | &wsabuf, 1, | |
1501 | { 0, NULL}, | |
1502 | 0 }; | |
1503 | #endif | |
1504 | return send_internal (&wsamsg, flags); | |
1505 | } | |
1506 | ||
1507 | ssize_t | |
1508 | fhandler_socket_inet::sendmsg (const struct msghdr *msg, int flags) | |
1509 | { | |
859d215b CV |
1510 | struct sockaddr_storage sst; |
1511 | int len = 0; | |
1512 | ||
1513 | if (msg->msg_name | |
1514 | && get_inet_addr_inet ((struct sockaddr *) msg->msg_name, | |
1515 | msg->msg_namelen, &sst, &len) == SOCKET_ERROR) | |
1516 | return SOCKET_ERROR; | |
1517 | ||
1518 | WSABUF wsabuf[msg->msg_iovlen]; | |
1519 | WSABUF *wsaptr = wsabuf; | |
1520 | const struct iovec *iovptr = msg->msg_iov; | |
1521 | for (int i = 0; i < msg->msg_iovlen; ++i) | |
1522 | { | |
1523 | wsaptr->len = iovptr->iov_len; | |
1524 | (wsaptr++)->buf = (char *) (iovptr++)->iov_base; | |
1525 | } | |
1526 | /* Disappointing but true: Even if WSASendMsg is supported, it's only | |
1527 | supported for datagram and raw sockets. */ | |
7f7532fa | 1528 | DWORD controllen = (DWORD) ((get_socket_type () == SOCK_STREAM) |
859d215b CV |
1529 | ? 0 : msg->msg_controllen); |
1530 | WSAMSG wsamsg = { msg->msg_name ? (struct sockaddr *) &sst : NULL, len, | |
1531 | wsabuf, (DWORD) msg->msg_iovlen, | |
1532 | { controllen, (char *) msg->msg_control }, | |
1533 | 0 }; | |
1534 | return send_internal (&wsamsg, flags); | |
1535 | } | |
1536 | ||
1537 | ssize_t | |
b79018ee | 1538 | fhandler_socket_wsock::write (const void *in_ptr, size_t len) |
859d215b CV |
1539 | { |
1540 | char *ptr = (char *) in_ptr; | |
1541 | ||
1542 | #ifdef __x86_64__ | |
1543 | /* size_t is 64 bit, but the len member in WSABUF is 32 bit. | |
1544 | Split buffer if necessary. */ | |
1545 | DWORD bufcnt = len / UINT32_MAX + ((!len || (len % UINT32_MAX)) ? 1 : 0); | |
1546 | WSABUF wsabuf[bufcnt]; | |
1547 | WSAMSG wsamsg = { NULL, 0, wsabuf, bufcnt, { 0, NULL }, 0 }; | |
1548 | /* Don't use len as loop condition, it could be 0. */ | |
1549 | for (WSABUF *wsaptr = wsabuf; bufcnt--; ++wsaptr) | |
1550 | { | |
1551 | wsaptr->len = MIN (len, UINT32_MAX); | |
1552 | wsaptr->buf = ptr; | |
1553 | len -= wsaptr->len; | |
1554 | ptr += wsaptr->len; | |
1555 | } | |
1556 | #else | |
1557 | WSABUF wsabuf = { len, ptr }; | |
1558 | WSAMSG wsamsg = { NULL, 0, &wsabuf, 1, { 0, NULL }, 0 }; | |
1559 | #endif | |
1560 | return send_internal (&wsamsg, 0); | |
1561 | } | |
1562 | ||
1563 | ssize_t | |
b79018ee CV |
1564 | fhandler_socket_wsock::writev (const struct iovec *const iov, const int iovcnt, |
1565 | ssize_t tot) | |
859d215b CV |
1566 | { |
1567 | WSABUF wsabuf[iovcnt]; | |
1568 | WSABUF *wsaptr = wsabuf; | |
1569 | const struct iovec *iovptr = iov; | |
1570 | for (int i = 0; i < iovcnt; ++i) | |
1571 | { | |
1572 | wsaptr->len = iovptr->iov_len; | |
1573 | (wsaptr++)->buf = (char *) (iovptr++)->iov_base; | |
1574 | } | |
1575 | WSAMSG wsamsg = { NULL, 0, wsabuf, (DWORD) iovcnt, { 0, NULL}, 0 }; | |
1576 | return send_internal (&wsamsg, 0); | |
1577 | } | |
1578 | ||
ffb07b41 CV |
1579 | #define TCP_MAXRT 5 /* Older systems don't support TCP_MAXRTMS |
1580 | TCP_MAXRT takes secs, not msecs. */ | |
1581 | ||
8ccffddc CV |
1582 | #define MAX_TCP_KEEPIDLE 32767 |
1583 | #define MAX_TCP_KEEPCNT 255 | |
1584 | #define MAX_TCP_KEEPINTVL 32767 | |
1585 | ||
1586 | #define FIXED_WSOCK_TCP_KEEPCNT 10 | |
1587 | ||
1588 | int | |
1589 | fhandler_socket_inet::set_keepalive (int keepidle, int keepcnt, int keepintvl) | |
1590 | { | |
1591 | struct tcp_keepalive tka; | |
1592 | int so_keepalive = 0; | |
1593 | int len = sizeof so_keepalive; | |
1594 | int ret; | |
1595 | DWORD dummy; | |
1596 | ||
1597 | /* Per MSDN, | |
1598 | https://docs.microsoft.com/en-us/windows/win32/winsock/sio-keepalive-vals | |
1599 | the subsequent keep-alive settings in struct tcp_keepalive are only used | |
1600 | if the onoff member is != 0. Request the current state of SO_KEEPALIVE, | |
1601 | then set the keep-alive options with onoff set to 1. On success, if | |
1602 | SO_KEEPALIVE was 0, restore to the original SO_KEEPALIVE setting. Per | |
1603 | the above MSDN doc, the SIO_KEEPALIVE_VALS settings are persistent | |
1604 | across switching SO_KEEPALIVE. */ | |
1605 | ret = ::getsockopt (get_socket (), SOL_SOCKET, SO_KEEPALIVE, | |
1606 | (char *) &so_keepalive, &len); | |
1607 | if (ret == SOCKET_ERROR) | |
1608 | debug_printf ("getsockopt (SO_KEEPALIVE) failed, %u\n", WSAGetLastError ()); | |
1609 | tka.onoff = 1; | |
1610 | tka.keepalivetime = keepidle * MSPERSEC; | |
1611 | /* WinSock TCP_KEEPCNT is fixed. But we still want that the keep-alive | |
1612 | times out after TCP_KEEPIDLE + TCP_KEEPCNT * TCP_KEEPINTVL secs. | |
1613 | To that end, we set keepaliveinterval so that | |
1614 | ||
1615 | keepaliveinterval * FIXED_WSOCK_TCP_KEEPCNT == TCP_KEEPINTVL * TCP_KEEPCNT | |
1616 | ||
1617 | FIXME? Does that make sense? | |
1618 | ||
1619 | Sidenote: Given the max values, the entire operation fits into an int. */ | |
1620 | tka.keepaliveinterval = MSPERSEC / FIXED_WSOCK_TCP_KEEPCNT * keepcnt | |
1621 | * keepintvl; | |
1622 | if (WSAIoctl (get_socket (), SIO_KEEPALIVE_VALS, (LPVOID) &tka, sizeof tka, | |
1623 | NULL, 0, &dummy, NULL, NULL) == SOCKET_ERROR) | |
1624 | { | |
1625 | set_winsock_errno (); | |
1626 | return -1; | |
1627 | } | |
1628 | if (!so_keepalive) | |
1629 | { | |
1630 | ret = ::setsockopt (get_socket (), SOL_SOCKET, SO_KEEPALIVE, | |
1631 | (const char *) &so_keepalive, sizeof so_keepalive); | |
1632 | if (ret == SOCKET_ERROR) | |
1633 | debug_printf ("setsockopt (SO_KEEPALIVE) failed, %u\n", | |
1634 | WSAGetLastError ()); | |
1635 | } | |
1636 | return 0; | |
1637 | } | |
1638 | ||
859d215b CV |
1639 | int |
1640 | fhandler_socket_inet::setsockopt (int level, int optname, const void *optval, | |
1641 | socklen_t optlen) | |
1642 | { | |
1643 | bool ignore = false; | |
1644 | int ret = -1; | |
ffb07b41 | 1645 | unsigned int timeout; |
859d215b CV |
1646 | |
1647 | /* Preprocessing setsockopt. Set ignore to true if setsockopt call should | |
1648 | get skipped entirely. */ | |
1649 | switch (level) | |
1650 | { | |
1651 | case SOL_SOCKET: | |
1652 | switch (optname) | |
1653 | { | |
1654 | case SO_PEERCRED: | |
1655 | set_errno (ENOPROTOOPT); | |
1656 | return -1; | |
1657 | ||
1658 | case SO_REUSEADDR: | |
1659 | /* Per POSIX we must not be able to reuse a complete duplicate of a | |
1660 | local TCP address (same IP, same port), even if SO_REUSEADDR has | |
1661 | been set. This behaviour is maintained in WinSock for backward | |
1662 | compatibility, while the WinSock standard behaviour of stream | |
1663 | socket binding is equivalent to the POSIX behaviour as if | |
1664 | SO_REUSEADDR has been set. The SO_EXCLUSIVEADDRUSE option has | |
1665 | been added to allow an application to request POSIX standard | |
1666 | behaviour in the non-SO_REUSEADDR case. | |
1667 | ||
1668 | To emulate POSIX socket binding behaviour, note that SO_REUSEADDR | |
1669 | has been set but don't call setsockopt. Instead | |
1670 | fhandler_socket::bind sets SO_EXCLUSIVEADDRUSE if the application | |
1671 | did not set SO_REUSEADDR. */ | |
1672 | if (optlen < (socklen_t) sizeof (int)) | |
1673 | { | |
1674 | set_errno (EINVAL); | |
1675 | return ret; | |
1676 | } | |
1677 | if (get_socket_type () == SOCK_STREAM) | |
1678 | ignore = true; | |
1679 | break; | |
1680 | ||
1681 | case SO_RCVTIMEO: | |
1682 | case SO_SNDTIMEO: | |
1683 | if (optlen < (socklen_t) sizeof (struct timeval)) | |
1684 | { | |
1685 | set_errno (EINVAL); | |
1686 | return ret; | |
1687 | } | |
1688 | if (timeval_to_ms ((struct timeval *) optval, | |
1689 | (optname == SO_RCVTIMEO) ? rcvtimeo () | |
1690 | : sndtimeo ())) | |
1691 | ret = 0; | |
1692 | else | |
1693 | set_errno (EDOM); | |
1694 | return ret; | |
1695 | ||
9c84bfd4 TY |
1696 | case SO_OOBINLINE: |
1697 | /* Inline mode for out-of-band (OOB) data of winsock is | |
1698 | completely broken. That is, SIOCATMARK always returns | |
1699 | TRUE in inline mode. Due to this problem, application | |
1700 | cannot determine OOB data at all. Therefore the behavior | |
1701 | of a socket with SO_OOBINLINE set is simulated using | |
1702 | a socket with SO_OOBINLINE not set. In this fake inline | |
1703 | mode, the order of the OOB and non-OOB data is not | |
1704 | preserved. OOB data is read before non-OOB data sent | |
1705 | prior to the OOB data. However, this most likely is | |
1706 | not a problem in most cases. */ | |
1707 | /* Here, instead of actually setting inline mode, simply | |
1708 | set the variable oobinline. */ | |
1709 | oobinline = *(int *) optval ? true : false; | |
1710 | ignore = true; | |
1711 | break; | |
1712 | ||
859d215b CV |
1713 | default: |
1714 | break; | |
1715 | } | |
1716 | break; | |
1717 | ||
1718 | case IPPROTO_IP: | |
1719 | /* Old applications still use the old WinSock1 IPPROTO_IP values. */ | |
1720 | if (CYGWIN_VERSION_CHECK_FOR_USING_WINSOCK1_VALUES) | |
1721 | optname = convert_ws1_ip_optname (optname); | |
1722 | switch (optname) | |
1723 | { | |
1724 | case IP_TOS: | |
1725 | /* Winsock doesn't support setting the IP_TOS field with setsockopt | |
1726 | and TOS was never implemented for TCP anyway. setsockopt returns | |
1727 | WinSock error 10022, WSAEINVAL when trying to set the IP_TOS | |
1728 | field. We just return 0 instead. */ | |
1729 | ignore = true; | |
1730 | break; | |
1731 | ||
1732 | default: | |
1733 | break; | |
1734 | } | |
1735 | break; | |
1736 | ||
1737 | case IPPROTO_IPV6: | |
1738 | { | |
1739 | switch (optname) | |
1740 | { | |
1741 | case IPV6_TCLASS: | |
1742 | /* Unsupported */ | |
1743 | ignore = true; | |
1744 | break; | |
1745 | ||
1746 | default: | |
1747 | break; | |
1748 | } | |
1749 | } | |
1750 | default: | |
1751 | break; | |
e037192b CV |
1752 | |
1753 | case IPPROTO_TCP: | |
8ccffddc CV |
1754 | /* Check for stream socket early on, so we don't have to do this for |
1755 | every option. Also, WinSock returns EINVAL. */ | |
1756 | if (type != SOCK_STREAM) | |
1757 | { | |
1758 | set_errno (EOPNOTSUPP); | |
1759 | return -1; | |
1760 | } | |
1761 | ||
e037192b CV |
1762 | switch (optname) |
1763 | { | |
1764 | case TCP_MAXSEG: | |
1765 | /* Winsock doesn't support setting TCP_MAXSEG, only requesting it | |
1766 | via getsockopt. Make this a no-op. */ | |
1767 | ignore = true; | |
1768 | break; | |
1769 | ||
ffb07b41 CV |
1770 | case TCP_MAXRT: |
1771 | /* Don't let this option slip through from user space. */ | |
1772 | set_errno (EOPNOTSUPP); | |
1773 | return -1; | |
1774 | ||
1775 | case TCP_USER_TIMEOUT: | |
1776 | if (!wincap.has_tcp_maxrtms ()) | |
1777 | { | |
1778 | /* convert msecs to secs. Values < 1000 ms are converted to | |
1779 | 0 secs, just as in WinSock. */ | |
1780 | timeout = *(unsigned int *) optval / MSPERSEC; | |
1781 | optname = TCP_MAXRT; | |
1782 | optval = (const void *) &timeout; | |
1783 | } | |
1784 | break; | |
1785 | ||
0feb77c2 CV |
1786 | case TCP_FASTOPEN: |
1787 | /* Fake FastOpen on older systems. */ | |
1788 | if (!wincap.has_tcp_fastopen ()) | |
1789 | { | |
8ccffddc CV |
1790 | ignore = true; |
1791 | tcp_fastopen = *(int *) optval ? true : false; | |
1792 | } | |
1793 | break; | |
1794 | ||
1795 | case TCP_KEEPIDLE: | |
1796 | /* Handle TCP_KEEPIDLE on older systems. */ | |
1797 | if (!wincap.has_linux_tcp_keepalive_sockopts ()) | |
1798 | { | |
1799 | if (*(int *) optval < 1 || *(int *) optval > MAX_TCP_KEEPIDLE) | |
0feb77c2 | 1800 | { |
8ccffddc | 1801 | set_errno (EINVAL); |
0feb77c2 CV |
1802 | return -1; |
1803 | } | |
8ccffddc CV |
1804 | if (set_keepalive (*(int *) optval, tcp_keepcnt, tcp_keepintvl)) |
1805 | return -1; | |
0feb77c2 | 1806 | ignore = true; |
8ccffddc CV |
1807 | tcp_keepidle = *(int *) optval; |
1808 | } | |
1809 | break; | |
1810 | ||
1811 | case TCP_KEEPCNT: | |
1812 | /* Fake TCP_KEEPCNT on older systems. */ | |
1813 | if (!wincap.has_linux_tcp_keepalive_sockopts ()) | |
1814 | { | |
1815 | if (*(int *) optval < 1 || *(int *) optval > MAX_TCP_KEEPCNT) | |
1816 | { | |
1817 | set_errno (EINVAL); | |
1818 | return -1; | |
1819 | } | |
1820 | if (set_keepalive (tcp_keepidle, *(int *) optval, tcp_keepintvl)) | |
1821 | return -1; | |
1822 | ignore = true; | |
1823 | tcp_keepcnt = *(int *) optval; | |
1824 | } | |
1825 | break; | |
1826 | ||
1827 | case TCP_KEEPINTVL: | |
1828 | /* Handle TCP_KEEPINTVL on older systems. */ | |
1829 | if (!wincap.has_linux_tcp_keepalive_sockopts ()) | |
1830 | { | |
1831 | if (*(int *) optval < 1 || *(int *) optval > MAX_TCP_KEEPINTVL) | |
1832 | { | |
1833 | set_errno (EINVAL); | |
1834 | return -1; | |
1835 | } | |
1836 | if (set_keepalive (tcp_keepidle, tcp_keepcnt, *(int *) optval)) | |
1837 | return -1; | |
1838 | ignore = true; | |
1839 | tcp_keepintvl = *(int *) optval; | |
0feb77c2 CV |
1840 | } |
1841 | break; | |
1842 | ||
e037192b CV |
1843 | default: |
1844 | break; | |
1845 | } | |
1846 | break; | |
859d215b CV |
1847 | } |
1848 | ||
1849 | /* Call Winsock setsockopt (or not) */ | |
1850 | if (ignore) | |
1851 | ret = 0; | |
1852 | else | |
1853 | { | |
1854 | ret = ::setsockopt (get_socket (), level, optname, (const char *) optval, | |
1855 | optlen); | |
1856 | if (ret == SOCKET_ERROR) | |
1857 | { | |
1858 | set_winsock_errno (); | |
1859 | return ret; | |
1860 | } | |
1861 | } | |
1862 | ||
1863 | if (optlen == (socklen_t) sizeof (int)) | |
1864 | debug_printf ("setsockopt optval=%x", *(int *) optval); | |
1865 | ||
1866 | /* Postprocessing setsockopt, setting fhandler_socket members, etc. */ | |
1867 | switch (level) | |
1868 | { | |
1869 | case SOL_SOCKET: | |
1870 | switch (optname) | |
1871 | { | |
1872 | case SO_REUSEADDR: | |
1873 | saw_reuseaddr (*(int *) optval); | |
1874 | break; | |
1875 | ||
1876 | case SO_RCVBUF: | |
1877 | rmem (*(int *) optval); | |
1878 | break; | |
1879 | ||
1880 | case SO_SNDBUF: | |
1881 | wmem (*(int *) optval); | |
1882 | break; | |
1883 | ||
1884 | default: | |
1885 | break; | |
1886 | } | |
1887 | break; | |
1888 | ||
1889 | default: | |
1890 | break; | |
1891 | } | |
1892 | ||
1893 | return ret; | |
1894 | } | |
1895 | ||
1896 | int | |
1897 | fhandler_socket_inet::getsockopt (int level, int optname, const void *optval, | |
1898 | socklen_t *optlen) | |
1899 | { | |
1900 | bool onebyte = false; | |
1901 | int ret = -1; | |
1902 | ||
1903 | /* Preprocessing getsockopt. */ | |
1904 | switch (level) | |
1905 | { | |
1906 | case SOL_SOCKET: | |
1907 | switch (optname) | |
1908 | { | |
1909 | case SO_PEERCRED: | |
1910 | set_errno (ENOPROTOOPT); | |
1911 | return -1; | |
1912 | ||
1913 | case SO_REUSEADDR: | |
1914 | { | |
1915 | unsigned int *reuseaddr = (unsigned int *) optval; | |
1916 | ||
1917 | if (*optlen < (socklen_t) sizeof *reuseaddr) | |
1918 | { | |
1919 | set_errno (EINVAL); | |
1920 | return -1; | |
1921 | } | |
1922 | *reuseaddr = saw_reuseaddr(); | |
1923 | *optlen = (socklen_t) sizeof *reuseaddr; | |
1924 | return 0; | |
1925 | } | |
1926 | ||
1927 | case SO_RCVTIMEO: | |
1928 | case SO_SNDTIMEO: | |
1929 | { | |
1930 | struct timeval *time_out = (struct timeval *) optval; | |
1931 | ||
1932 | if (*optlen < (socklen_t) sizeof *time_out) | |
1933 | { | |
1934 | set_errno (EINVAL); | |
1935 | return -1; | |
1936 | } | |
1937 | DWORD ms = (optname == SO_RCVTIMEO) ? rcvtimeo () : sndtimeo (); | |
1938 | if (ms == 0 || ms == INFINITE) | |
1939 | { | |
1940 | time_out->tv_sec = 0; | |
1941 | time_out->tv_usec = 0; | |
1942 | } | |
1943 | else | |
1944 | { | |
1945 | time_out->tv_sec = ms / MSPERSEC; | |
1946 | time_out->tv_usec = ((ms % MSPERSEC) * USPERSEC) / MSPERSEC; | |
1947 | } | |
1948 | *optlen = (socklen_t) sizeof *time_out; | |
1949 | return 0; | |
1950 | } | |
1951 | ||
1952 | case SO_TYPE: | |
1953 | { | |
1954 | unsigned int *type = (unsigned int *) optval; | |
1955 | *type = get_socket_type (); | |
1956 | *optlen = (socklen_t) sizeof *type; | |
1957 | return 0; | |
1958 | } | |
1959 | ||
9c84bfd4 TY |
1960 | case SO_OOBINLINE: |
1961 | *(int *) optval = oobinline ? 1 : 0; | |
1962 | return 0; | |
1963 | ||
859d215b CV |
1964 | default: |
1965 | break; | |
1966 | } | |
1967 | break; | |
1968 | ||
1969 | case IPPROTO_IP: | |
1970 | /* Old applications still use the old WinSock1 IPPROTO_IP values. */ | |
1971 | if (CYGWIN_VERSION_CHECK_FOR_USING_WINSOCK1_VALUES) | |
1972 | optname = convert_ws1_ip_optname (optname); | |
1973 | break; | |
1974 | ||
0feb77c2 | 1975 | case IPPROTO_TCP: |
8ccffddc CV |
1976 | /* Check for stream socket early on, so we don't have to do this for |
1977 | every option. Also, WinSock returns EINVAL. */ | |
1978 | if (type != SOCK_STREAM) | |
1979 | { | |
1980 | set_errno (EOPNOTSUPP); | |
1981 | return -1; | |
1982 | } | |
1983 | ||
0feb77c2 CV |
1984 | switch (optname) |
1985 | { | |
ffb07b41 CV |
1986 | case TCP_MAXRT: |
1987 | /* Don't let this option slip through from user space. */ | |
1988 | set_errno (EOPNOTSUPP); | |
1989 | return -1; | |
1990 | ||
1991 | case TCP_USER_TIMEOUT: | |
1992 | /* Older systems don't support TCP_MAXRTMS, just call TCP_MAXRT. */ | |
1993 | if (!wincap.has_tcp_maxrtms ()) | |
1994 | optname = TCP_MAXRT; | |
1995 | break; | |
1996 | ||
0feb77c2 CV |
1997 | case TCP_FASTOPEN: |
1998 | /* Fake FastOpen on older systems */ | |
1999 | if (!wincap.has_tcp_fastopen ()) | |
2000 | { | |
0feb77c2 CV |
2001 | *(int *) optval = tcp_fastopen ? 1 : 0; |
2002 | *optlen = sizeof (int); | |
2003 | return 0; | |
2004 | } | |
2005 | break; | |
2006 | ||
8ccffddc CV |
2007 | case TCP_KEEPIDLE: |
2008 | /* Use stored value on older systems */ | |
2009 | if (!wincap.has_linux_tcp_keepalive_sockopts ()) | |
2010 | { | |
2011 | *(int *) optval = tcp_keepidle; | |
2012 | *optlen = sizeof (int); | |
2013 | return 0; | |
2014 | } | |
2015 | break; | |
2016 | ||
2017 | case TCP_KEEPCNT: | |
2018 | /* Use stored value on older systems */ | |
2019 | if (!wincap.has_linux_tcp_keepalive_sockopts ()) | |
2020 | { | |
2021 | *(int *) optval = tcp_keepcnt; | |
2022 | *optlen = sizeof (int); | |
2023 | return 0; | |
2024 | } | |
2025 | break; | |
2026 | ||
2027 | case TCP_KEEPINTVL: | |
2028 | /* Use stored value on older systems */ | |
2029 | if (!wincap.has_linux_tcp_keepalive_sockopts ()) | |
2030 | { | |
2031 | *(int *) optval = tcp_keepintvl; | |
2032 | *optlen = sizeof (int); | |
2033 | return 0; | |
2034 | } | |
2035 | break; | |
2036 | ||
0feb77c2 CV |
2037 | default: |
2038 | break; | |
2039 | } | |
2040 | break; | |
2041 | ||
859d215b CV |
2042 | default: |
2043 | break; | |
2044 | } | |
2045 | ||
2046 | /* Call Winsock getsockopt */ | |
2047 | ret = ::getsockopt (get_socket (), level, optname, (char *) optval, | |
2048 | (int *) optlen); | |
2049 | if (ret == SOCKET_ERROR) | |
2050 | { | |
2051 | set_winsock_errno (); | |
2052 | return ret; | |
2053 | } | |
2054 | ||
2055 | /* Postprocessing getsockopt, setting fhandler_socket members, etc. Set | |
2056 | onebyte true for options returning BOOLEAN instead of a boolean DWORD. */ | |
2057 | switch (level) | |
2058 | { | |
2059 | case SOL_SOCKET: | |
2060 | switch (optname) | |
2061 | { | |
2062 | case SO_ERROR: | |
2063 | { | |
2064 | int *e = (int *) optval; | |
2065 | debug_printf ("WinSock SO_ERROR = %d", *e); | |
2066 | *e = find_winsock_errno (*e); | |
2067 | } | |
2068 | break; | |
2069 | ||
2070 | case SO_KEEPALIVE: | |
2071 | case SO_DONTROUTE: | |
2072 | onebyte = true; | |
2073 | break; | |
2074 | ||
2075 | default: | |
2076 | break; | |
2077 | } | |
2078 | break; | |
8906a4d3 | 2079 | case IPPROTO_TCP: |
859d215b CV |
2080 | switch (optname) |
2081 | { | |
2082 | case TCP_NODELAY: | |
2083 | onebyte = true; | |
2084 | break; | |
2085 | ||
ffb07b41 CV |
2086 | case TCP_MAXRT: /* After above conversion from TCP_USER_TIMEOUT */ |
2087 | /* convert secs to msecs */ | |
2088 | *(unsigned int *) optval *= MSPERSEC; | |
2089 | break; | |
2090 | ||
0feb77c2 CV |
2091 | case TCP_FASTOPEN: |
2092 | onebyte = true; | |
2093 | break; | |
2094 | ||
859d215b CV |
2095 | default: |
2096 | break; | |
2097 | } | |
2098 | default: | |
2099 | break; | |
2100 | } | |
2101 | ||
2102 | if (onebyte) | |
2103 | { | |
2104 | /* Regression in Vista and later: instead of a 4 byte BOOL value, a | |
2105 | 1 byte BOOLEAN value is returned, in contrast to older systems and | |
2106 | the documentation. Since an int type is expected by the calling | |
0feb77c2 | 2107 | application, we convert the result here. */ |
859d215b CV |
2108 | BOOLEAN *in = (BOOLEAN *) optval; |
2109 | int *out = (int *) optval; | |
2110 | *out = *in; | |
2111 | *optlen = 4; | |
2112 | } | |
2113 | ||
2114 | return ret; | |
2115 | } | |
79598f94 CV |
2116 | |
2117 | int | |
b79018ee | 2118 | fhandler_socket_wsock::ioctl (unsigned int cmd, void *p) |
79598f94 CV |
2119 | { |
2120 | int res; | |
2121 | ||
2122 | switch (cmd) | |
2123 | { | |
2124 | /* Here we handle only ioctl commands which are understood by Winsock. | |
2125 | However, we have a problem, which is, the different size of u_long | |
2126 | in Windows and 64 bit Cygwin. This affects the definitions of | |
2127 | FIOASYNC, etc, because they are defined in terms of sizeof(u_long). | |
2128 | So we have to use case labels which are independent of the sizeof | |
2129 | u_long. Since we're redefining u_long at the start of this file to | |
2130 | matching Winsock's idea of u_long, we can use the real definitions in | |
2131 | calls to Windows. In theory we also have to make sure to convert the | |
2132 | different ideas of u_long between the application and Winsock, but | |
2133 | fortunately, the parameters defined as u_long pointers are on Linux | |
2134 | and BSD systems defined as int pointer, so the applications will | |
2135 | use a type of the expected size. Hopefully. */ | |
2136 | case FIOASYNC: | |
2137 | #ifdef __x86_64__ | |
2138 | case _IOW('f', 125, u_long): | |
2139 | #endif | |
2140 | res = WSAAsyncSelect (get_socket (), winmsg, WM_ASYNCIO, | |
2141 | *(int *) p ? ASYNC_MASK : 0); | |
2142 | syscall_printf ("Async I/O on socket %s", | |
2143 | *(int *) p ? "started" : "cancelled"); | |
2144 | async_io (*(int *) p != 0); | |
2145 | /* If async_io is switched off, revert the event handling. */ | |
2146 | if (*(int *) p == 0) | |
2147 | WSAEventSelect (get_socket (), wsock_evt, EVENT_MASK); | |
2148 | break; | |
2149 | case FIONREAD: | |
2150 | #ifdef __x86_64__ | |
2151 | case _IOR('f', 127, u_long): | |
2152 | #endif | |
2153 | /* Make sure to use the Winsock definition of FIONREAD. */ | |
2154 | res = ::ioctlsocket (get_socket (), _IOR('f', 127, u_long), (u_long *) p); | |
2155 | if (res == SOCKET_ERROR) | |
2156 | set_winsock_errno (); | |
2157 | break; | |
2158 | case FIONBIO: | |
2159 | case SIOCATMARK: | |
2160 | /* Sockets are always non-blocking internally. So we just note the | |
2161 | state here. */ | |
2162 | #ifdef __x86_64__ | |
2163 | /* Convert the different idea of u_long in the definition of cmd. */ | |
2164 | if (((cmd >> 16) & IOCPARM_MASK) == sizeof (unsigned long)) | |
2165 | cmd = (cmd & ~(IOCPARM_MASK << 16)) | (sizeof (u_long) << 16); | |
2166 | #endif | |
2167 | if (cmd == FIONBIO) | |
2168 | { | |
2169 | syscall_printf ("socket is now %sblocking", | |
2170 | *(int *) p ? "non" : ""); | |
2171 | set_nonblocking (*(int *) p); | |
2172 | res = 0; | |
2173 | } | |
2174 | else | |
2175 | res = ::ioctlsocket (get_socket (), cmd, (u_long *) p); | |
9c84bfd4 TY |
2176 | /* In winsock, the return value of SIOCATMARK is FALSE if |
2177 | OOB data exists, TRUE otherwise. This is almost opposite | |
2178 | to expectation. */ | |
2179 | #ifdef __x86_64__ | |
2180 | /* SIOCATMARK = _IOR('s',7,u_long) */ | |
2181 | if (cmd == _IOR('s',7,u_long) && !res) | |
2182 | *(u_long *)p = !*(u_long *)p; | |
2183 | #else | |
2184 | if (cmd == SIOCATMARK && !res) | |
2185 | *(u_long *)p = !*(u_long *)p; | |
2186 | #endif | |
79598f94 CV |
2187 | break; |
2188 | default: | |
2189 | res = fhandler_socket::ioctl (cmd, p); | |
2190 | break; | |
2191 | } | |
2192 | syscall_printf ("%d = ioctl_socket(%x, %p)", res, cmd, p); | |
2193 | return res; | |
2194 | } | |
9c593d9b CV |
2195 | |
2196 | int | |
b79018ee | 2197 | fhandler_socket_wsock::fcntl (int cmd, intptr_t arg) |
9c593d9b CV |
2198 | { |
2199 | int res = 0; | |
2200 | ||
2201 | switch (cmd) | |
2202 | { | |
2203 | case F_SETOWN: | |
2204 | { | |
2205 | pid_t pid = (pid_t) arg; | |
2206 | LOCK_EVENTS; | |
2207 | wsock_events->owner = pid; | |
2208 | UNLOCK_EVENTS; | |
2209 | debug_printf ("owner set to %d", pid); | |
2210 | } | |
2211 | break; | |
2212 | case F_GETOWN: | |
2213 | res = wsock_events->owner; | |
2214 | break; | |
2215 | default: | |
2216 | res = fhandler_socket::fcntl (cmd, arg); | |
2217 | break; | |
2218 | } | |
2219 | return res; | |
2220 | } |