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