winmm/tests: Add tests for visibility of video window.
[wine.git] / server / sock.c
blob7d7e470be2859a31c05882ab42868392b31df084
1 /*
2 * Server-side socket management
4 * Copyright (C) 1999 Marcus Meissner, Ove Kåven
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * FIXME: we use read|write access in all cases. Shouldn't we depend that
21 * on the access of the current handle?
24 #include "config.h"
26 #include <assert.h>
27 #include <fcntl.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #ifdef HAVE_IFADDRS_H
34 # include <ifaddrs.h>
35 #endif
36 #ifdef HAVE_NET_IF_H
37 # include <net/if.h>
38 #endif
39 #ifdef HAVE_NETINET_IN_H
40 # include <netinet/in.h>
41 #endif
42 #include <poll.h>
43 #include <sys/time.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47 #ifdef HAVE_SYS_FILIO_H
48 # include <sys/filio.h>
49 #endif
50 #include <time.h>
51 #include <unistd.h>
52 #include <limits.h>
53 #ifdef HAVE_LINUX_FILTER_H
54 # include <linux/filter.h>
55 #endif
56 #ifdef HAVE_LINUX_RTNETLINK_H
57 # include <linux/rtnetlink.h>
58 #endif
60 #ifdef HAVE_NETIPX_IPX_H
61 # include <netipx/ipx.h>
62 #elif defined(HAVE_LINUX_IPX_H)
63 # ifdef HAVE_ASM_TYPES_H
64 # include <asm/types.h>
65 # endif
66 # ifdef HAVE_LINUX_TYPES_H
67 # include <linux/types.h>
68 # endif
69 # include <linux/ipx.h>
70 #endif
71 #if defined(SOL_IPX) || defined(SO_DEFAULT_HEADERS)
72 # define HAS_IPX
73 #endif
75 #ifdef HAVE_LINUX_IRDA_H
76 # ifdef HAVE_LINUX_TYPES_H
77 # include <linux/types.h>
78 # endif
79 # include <linux/irda.h>
80 # define HAS_IRDA
81 #endif
83 #include "ntstatus.h"
84 #define WIN32_NO_STATUS
85 #include "windef.h"
86 #include "winternl.h"
87 #include "winerror.h"
88 #define USE_WS_PREFIX
89 #include "winsock2.h"
90 #include "ws2tcpip.h"
91 #include "wsipx.h"
92 #include "af_irda.h"
93 #include "wine/afd.h"
95 #include "process.h"
96 #include "file.h"
97 #include "handle.h"
98 #include "thread.h"
99 #include "request.h"
100 #include "user.h"
102 #if defined(linux) && !defined(IP_UNICAST_IF)
103 #define IP_UNICAST_IF 50
104 #endif
106 static const char magic_loopback_addr[] = {127, 12, 34, 56};
108 union win_sockaddr
110 struct WS_sockaddr addr;
111 struct WS_sockaddr_in in;
112 struct WS_sockaddr_in6 in6;
113 struct WS_sockaddr_ipx ipx;
114 SOCKADDR_IRDA irda;
117 static struct list poll_list = LIST_INIT( poll_list );
119 struct poll_req
121 struct list entry;
122 struct async *async;
123 struct iosb *iosb;
124 struct timeout_user *timeout;
125 timeout_t orig_timeout;
126 int exclusive;
127 int pending;
128 unsigned int count;
129 struct
131 struct sock *sock;
132 int mask;
133 obj_handle_t handle;
134 int flags;
135 unsigned int status;
136 } sockets[1];
139 struct accept_req
141 struct list entry;
142 struct async *async;
143 struct iosb *iosb;
144 struct sock *sock, *acceptsock;
145 int accepted;
146 unsigned int recv_len, local_len;
149 struct connect_req
151 struct async *async;
152 struct iosb *iosb;
153 struct sock *sock;
154 unsigned int addr_len, send_len, send_cursor;
157 struct send_req
159 struct iosb *iosb;
160 struct sock *sock;
163 enum connection_state
165 SOCK_LISTENING,
166 SOCK_UNCONNECTED,
167 SOCK_CONNECTING,
168 SOCK_CONNECTED,
169 SOCK_CONNECTIONLESS,
172 #define MAX_ICMP_HISTORY_LENGTH 8
174 struct sock
176 struct object obj; /* object header */
177 struct fd *fd; /* socket file descriptor */
178 enum connection_state state; /* connection state */
179 unsigned int mask; /* event mask */
180 /* pending AFD_POLL_* events which have not yet been reported to the application */
181 unsigned int pending_events;
182 /* AFD_POLL_* events which have already been reported and should not be
183 * selected for again until reset by a relevant call.
185 * For example, if AFD_POLL_READ is set here and not in pending_events, it
186 * has already been reported and consumed, and we should not report it
187 * again, even if POLLIN is signaled, until it is reset by e.g recv().
189 * If an event has been signaled and not consumed yet, it will be set in
190 * both pending_events and reported_events (as we should only ever report
191 * any event once until it is reset.) */
192 unsigned int reported_events;
193 unsigned short proto; /* socket protocol */
194 unsigned short type; /* socket type */
195 unsigned short family; /* socket family */
196 struct event *event; /* event object */
197 user_handle_t window; /* window to send the message to */
198 unsigned int message; /* message to send */
199 obj_handle_t wparam; /* message wparam (socket handle) */
200 int errors[AFD_POLL_BIT_COUNT]; /* event errors */
201 timeout_t connect_time;/* time the socket was connected */
202 struct sock *deferred; /* socket that waits for a deferred accept */
203 struct async_queue read_q; /* queue for asynchronous reads */
204 struct async_queue write_q; /* queue for asynchronous writes */
205 struct async_queue ifchange_q; /* queue for interface change notifications */
206 struct async_queue accept_q; /* queue for asynchronous accepts */
207 struct async_queue connect_q; /* queue for asynchronous connects */
208 struct async_queue poll_q; /* queue for asynchronous polls */
209 struct object *ifchange_obj; /* the interface change notification object */
210 struct list ifchange_entry; /* entry in ifchange notification list */
211 struct list accept_list; /* list of pending accept requests */
212 struct accept_req *accept_recv_req; /* pending accept-into request which will recv on this socket */
213 struct connect_req *connect_req; /* pending connection request */
214 struct poll_req *main_poll; /* main poll */
215 union win_sockaddr addr; /* socket name */
216 int addr_len; /* socket name length */
217 unsigned int rcvbuf; /* advisory recv buffer size */
218 unsigned int sndbuf; /* advisory send buffer size */
219 unsigned int rcvtimeo; /* receive timeout in ms */
220 unsigned int sndtimeo; /* send timeout in ms */
221 struct
223 unsigned short icmp_id;
224 unsigned short icmp_seq;
226 icmp_fixup_data[MAX_ICMP_HISTORY_LENGTH]; /* Sent ICMP packets history used to fixup reply id. */
227 unsigned int icmp_fixup_data_len; /* Sent ICMP packets history length. */
228 unsigned int rd_shutdown : 1; /* is the read end shut down? */
229 unsigned int wr_shutdown : 1; /* is the write end shut down? */
230 unsigned int wr_shutdown_pending : 1; /* is a write shutdown pending? */
231 unsigned int hangup : 1; /* has the read end received a hangup? */
232 unsigned int aborted : 1; /* did we get a POLLERR or irregular POLLHUP? */
233 unsigned int nonblocking : 1; /* is the socket nonblocking? */
234 unsigned int bound : 1; /* is the socket bound? */
235 unsigned int reset : 1; /* did we get a TCP reset? */
238 static void sock_dump( struct object *obj, int verbose );
239 static struct fd *sock_get_fd( struct object *obj );
240 static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
241 static void sock_destroy( struct object *obj );
242 static struct object *sock_get_ifchange( struct sock *sock );
243 static void sock_release_ifchange( struct sock *sock );
245 static int sock_get_poll_events( struct fd *fd );
246 static void sock_poll_event( struct fd *fd, int event );
247 static enum server_fd_type sock_get_fd_type( struct fd *fd );
248 static void sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
249 static void sock_cancel_async( struct fd *fd, struct async *async );
250 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
252 static int accept_into_socket( struct sock *sock, struct sock *acceptsock );
253 static struct sock *accept_socket( struct sock *sock );
254 static int sock_get_ntstatus( int err );
255 static unsigned int sock_get_error( int err );
256 static void poll_socket( struct sock *poll_sock, struct async *async, int exclusive, timeout_t timeout,
257 unsigned int count, const struct afd_poll_socket_64 *sockets );
259 static const struct object_ops sock_ops =
261 sizeof(struct sock), /* size */
262 &file_type, /* type */
263 sock_dump, /* dump */
264 add_queue, /* add_queue */
265 remove_queue, /* remove_queue */
266 default_fd_signaled, /* signaled */
267 no_satisfied, /* satisfied */
268 no_signal, /* signal */
269 sock_get_fd, /* get_fd */
270 default_map_access, /* map_access */
271 default_get_sd, /* get_sd */
272 default_set_sd, /* set_sd */
273 no_get_full_name, /* get_full_name */
274 no_lookup_name, /* lookup_name */
275 no_link_name, /* link_name */
276 NULL, /* unlink_name */
277 no_open_file, /* open_file */
278 no_kernel_obj_list, /* get_kernel_obj_list */
279 sock_close_handle, /* close_handle */
280 sock_destroy /* destroy */
283 static const struct fd_ops sock_fd_ops =
285 sock_get_poll_events, /* get_poll_events */
286 sock_poll_event, /* poll_event */
287 sock_get_fd_type, /* get_fd_type */
288 no_fd_read, /* read */
289 no_fd_write, /* write */
290 no_fd_flush, /* flush */
291 default_fd_get_file_info, /* get_file_info */
292 no_fd_get_volume_info, /* get_volume_info */
293 sock_ioctl, /* ioctl */
294 sock_cancel_async, /* cancel_async */
295 no_fd_queue_async, /* queue_async */
296 sock_reselect_async /* reselect_async */
299 union unix_sockaddr
301 struct sockaddr addr;
302 struct sockaddr_in in;
303 struct sockaddr_in6 in6;
304 #ifdef HAS_IPX
305 struct sockaddr_ipx ipx;
306 #endif
307 #ifdef HAS_IRDA
308 struct sockaddr_irda irda;
309 #endif
312 static int sockaddr_from_unix( const union unix_sockaddr *uaddr, struct WS_sockaddr *wsaddr, socklen_t wsaddrlen )
314 memset( wsaddr, 0, wsaddrlen );
316 switch (uaddr->addr.sa_family)
318 case AF_INET:
320 struct WS_sockaddr_in win = {0};
322 if (wsaddrlen < sizeof(win)) return -1;
323 win.sin_family = WS_AF_INET;
324 win.sin_port = uaddr->in.sin_port;
325 memcpy( &win.sin_addr, &uaddr->in.sin_addr, sizeof(win.sin_addr) );
326 memcpy( wsaddr, &win, sizeof(win) );
327 return sizeof(win);
330 case AF_INET6:
332 struct WS_sockaddr_in6 win = {0};
334 if (wsaddrlen < sizeof(win)) return -1;
335 win.sin6_family = WS_AF_INET6;
336 win.sin6_port = uaddr->in6.sin6_port;
337 win.sin6_flowinfo = uaddr->in6.sin6_flowinfo;
338 memcpy( &win.sin6_addr, &uaddr->in6.sin6_addr, sizeof(win.sin6_addr) );
339 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
340 win.sin6_scope_id = uaddr->in6.sin6_scope_id;
341 #endif
342 memcpy( wsaddr, &win, sizeof(win) );
343 return sizeof(win);
346 #ifdef HAS_IPX
347 case AF_IPX:
349 struct WS_sockaddr_ipx win = {0};
351 if (wsaddrlen < sizeof(win)) return -1;
352 win.sa_family = WS_AF_IPX;
353 memcpy( win.sa_netnum, &uaddr->ipx.sipx_network, sizeof(win.sa_netnum) );
354 memcpy( win.sa_nodenum, &uaddr->ipx.sipx_node, sizeof(win.sa_nodenum) );
355 win.sa_socket = uaddr->ipx.sipx_port;
356 memcpy( wsaddr, &win, sizeof(win) );
357 return sizeof(win);
359 #endif
361 #ifdef HAS_IRDA
362 case AF_IRDA:
364 SOCKADDR_IRDA win;
366 if (wsaddrlen < sizeof(win)) return -1;
367 win.irdaAddressFamily = WS_AF_IRDA;
368 memcpy( win.irdaDeviceID, &uaddr->irda.sir_addr, sizeof(win.irdaDeviceID) );
369 if (uaddr->irda.sir_lsap_sel != LSAP_ANY)
370 snprintf( win.irdaServiceName, sizeof(win.irdaServiceName), "LSAP-SEL%u", uaddr->irda.sir_lsap_sel );
371 else
372 memcpy( win.irdaServiceName, uaddr->irda.sir_name, sizeof(win.irdaServiceName) );
373 memcpy( wsaddr, &win, sizeof(win) );
374 return sizeof(win);
376 #endif
378 case AF_UNSPEC:
379 return 0;
381 default:
382 return -1;
387 static socklen_t sockaddr_to_unix( const struct WS_sockaddr *wsaddr, int wsaddrlen, union unix_sockaddr *uaddr )
389 memset( uaddr, 0, sizeof(*uaddr) );
391 switch (wsaddr->sa_family)
393 case WS_AF_INET:
395 struct WS_sockaddr_in win = {0};
397 if (wsaddrlen < sizeof(win)) return 0;
398 memcpy( &win, wsaddr, sizeof(win) );
399 uaddr->in.sin_family = AF_INET;
400 uaddr->in.sin_port = win.sin_port;
401 memcpy( &uaddr->in.sin_addr, &win.sin_addr, sizeof(win.sin_addr) );
402 return sizeof(uaddr->in);
405 case WS_AF_INET6:
407 struct WS_sockaddr_in6 win = {0};
409 if (wsaddrlen < sizeof(win)) return 0;
410 memcpy( &win, wsaddr, sizeof(win) );
411 uaddr->in6.sin6_family = AF_INET6;
412 uaddr->in6.sin6_port = win.sin6_port;
413 uaddr->in6.sin6_flowinfo = win.sin6_flowinfo;
414 memcpy( &uaddr->in6.sin6_addr, &win.sin6_addr, sizeof(win.sin6_addr) );
415 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
416 uaddr->in6.sin6_scope_id = win.sin6_scope_id;
417 #endif
418 return sizeof(uaddr->in6);
421 #ifdef HAS_IPX
422 case WS_AF_IPX:
424 struct WS_sockaddr_ipx win = {0};
426 if (wsaddrlen < sizeof(win)) return 0;
427 memcpy( &win, wsaddr, sizeof(win) );
428 uaddr->ipx.sipx_family = AF_IPX;
429 memcpy( &uaddr->ipx.sipx_network, win.sa_netnum, sizeof(win.sa_netnum) );
430 memcpy( &uaddr->ipx.sipx_node, win.sa_nodenum, sizeof(win.sa_nodenum) );
431 uaddr->ipx.sipx_port = win.sa_socket;
432 return sizeof(uaddr->ipx);
434 #endif
436 #ifdef HAS_IRDA
437 case WS_AF_IRDA:
439 SOCKADDR_IRDA win = {0};
440 unsigned int lsap_sel;
442 if (wsaddrlen < sizeof(win)) return 0;
443 memcpy( &win, wsaddr, sizeof(win) );
444 uaddr->irda.sir_family = AF_IRDA;
445 if (sscanf( win.irdaServiceName, "LSAP-SEL%u", &lsap_sel ) == 1)
446 uaddr->irda.sir_lsap_sel = lsap_sel;
447 else
449 uaddr->irda.sir_lsap_sel = LSAP_ANY;
450 memcpy( uaddr->irda.sir_name, win.irdaServiceName, sizeof(win.irdaServiceName) );
452 memcpy( &uaddr->irda.sir_addr, win.irdaDeviceID, sizeof(win.irdaDeviceID) );
453 return sizeof(uaddr->irda);
455 #endif
457 case WS_AF_UNSPEC:
458 switch (wsaddrlen)
460 default: /* likely an ipv4 address */
461 case sizeof(struct WS_sockaddr_in):
462 return sizeof(uaddr->in);
464 #ifdef HAS_IPX
465 case sizeof(struct WS_sockaddr_ipx):
466 return sizeof(uaddr->ipx);
467 #endif
469 #ifdef HAS_IRDA
470 case sizeof(SOCKADDR_IRDA):
471 return sizeof(uaddr->irda);
472 #endif
474 case sizeof(struct WS_sockaddr_in6):
475 return sizeof(uaddr->in6);
478 default:
479 return 0;
483 static socklen_t get_unix_sockaddr_any( union unix_sockaddr *uaddr, int ws_family )
485 memset( uaddr, 0, sizeof(*uaddr) );
486 switch (ws_family)
488 case WS_AF_INET:
489 uaddr->in.sin_family = AF_INET;
490 return sizeof(uaddr->in);
491 case WS_AF_INET6:
492 uaddr->in6.sin6_family = AF_INET6;
493 return sizeof(uaddr->in6);
494 #ifdef HAS_IPX
495 case WS_AF_IPX:
496 uaddr->ipx.sipx_family = AF_IPX;
497 return sizeof(uaddr->ipx);
498 #endif
499 #ifdef HAS_IRDA
500 case WS_AF_IRDA:
501 uaddr->irda.sir_family = AF_IRDA;
502 return sizeof(uaddr->irda);
503 #endif
504 default:
505 return 0;
509 /* some events are generated at the same time but must be sent in a particular
510 * order (e.g. CONNECT must be sent before READ) */
511 static const enum afd_poll_bit event_bitorder[] =
513 AFD_POLL_BIT_CONNECT,
514 AFD_POLL_BIT_CONNECT_ERR,
515 AFD_POLL_BIT_ACCEPT,
516 AFD_POLL_BIT_OOB,
517 AFD_POLL_BIT_READ,
518 AFD_POLL_BIT_WRITE,
519 AFD_POLL_BIT_RESET,
520 AFD_POLL_BIT_HUP,
521 AFD_POLL_BIT_CLOSE,
524 typedef enum {
525 SOCK_SHUTDOWN_ERROR = -1,
526 SOCK_SHUTDOWN_EOF = 0,
527 SOCK_SHUTDOWN_POLLHUP = 1
528 } sock_shutdown_t;
530 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
532 static sock_shutdown_t sock_check_pollhup(void)
534 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
535 int fd[2], n;
536 struct pollfd pfd;
537 char dummy;
539 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
540 if ( shutdown( fd[0], 1 ) ) goto out;
542 pfd.fd = fd[1];
543 pfd.events = POLLIN;
544 pfd.revents = 0;
546 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
547 n = poll( &pfd, 1, 1 );
548 if ( n != 1 ) goto out; /* error or timeout */
549 if ( pfd.revents & POLLHUP )
550 ret = SOCK_SHUTDOWN_POLLHUP;
551 else if ( pfd.revents & POLLIN &&
552 read( fd[1], &dummy, 1 ) == 0 )
553 ret = SOCK_SHUTDOWN_EOF;
555 out:
556 close( fd[0] );
557 close( fd[1] );
558 return ret;
561 void sock_init(void)
563 sock_shutdown_type = sock_check_pollhup();
565 switch ( sock_shutdown_type )
567 case SOCK_SHUTDOWN_EOF:
568 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
569 break;
570 case SOCK_SHUTDOWN_POLLHUP:
571 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
572 break;
573 default:
574 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
575 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
579 static void sock_reselect( struct sock *sock )
581 int ev = sock_get_poll_events( sock->fd );
583 if (debug_level)
584 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
586 set_fd_events( sock->fd, ev );
589 static unsigned int afd_poll_flag_to_win32( unsigned int flags )
591 static const unsigned int map[] =
593 FD_READ, /* READ */
594 FD_OOB, /* OOB */
595 FD_WRITE, /* WRITE */
596 FD_CLOSE, /* HUP */
597 FD_CLOSE, /* RESET */
598 0, /* CLOSE */
599 FD_CONNECT, /* CONNECT */
600 FD_ACCEPT, /* ACCEPT */
601 FD_CONNECT, /* CONNECT_ERR */
604 unsigned int i, ret = 0;
606 for (i = 0; i < ARRAY_SIZE(map); ++i)
608 if (flags & (1 << i)) ret |= map[i];
611 return ret;
614 /* wake anybody waiting on the socket event or send the associated message */
615 static void sock_wake_up( struct sock *sock )
617 unsigned int events = sock->pending_events & sock->mask;
618 int i;
620 if (sock->event)
622 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
623 if (events)
624 set_event( sock->event );
626 if (sock->window)
628 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
629 for (i = 0; i < ARRAY_SIZE(event_bitorder); i++)
631 enum afd_poll_bit event = event_bitorder[i];
632 if (events & (1 << event))
634 lparam_t lparam = afd_poll_flag_to_win32(1 << event) | (sock_get_error( sock->errors[event] ) << 16);
635 post_message( sock->window, sock->message, sock->wparam, lparam );
638 sock->pending_events = 0;
639 sock_reselect( sock );
643 static inline int sock_error( struct sock *sock )
645 int error = 0;
646 socklen_t len = sizeof(error);
648 getsockopt( get_unix_fd(sock->fd), SOL_SOCKET, SO_ERROR, (void *)&error, &len);
650 switch (sock->state)
652 case SOCK_UNCONNECTED:
653 break;
655 case SOCK_CONNECTING:
656 if (error)
657 sock->errors[AFD_POLL_BIT_CONNECT_ERR] = error;
658 else
659 error = sock->errors[AFD_POLL_BIT_CONNECT_ERR];
660 break;
662 case SOCK_LISTENING:
663 if (error)
664 sock->errors[AFD_POLL_BIT_ACCEPT] = error;
665 else
666 error = sock->errors[AFD_POLL_BIT_ACCEPT];
667 break;
669 case SOCK_CONNECTED:
670 case SOCK_CONNECTIONLESS:
671 if (error == ECONNRESET || error == EPIPE)
673 sock->reset = 1;
674 error = 0;
676 else if (error)
677 sock->errors[AFD_POLL_BIT_HUP] = error;
678 else
679 error = sock->errors[AFD_POLL_BIT_HUP];
680 break;
683 return error;
686 static void free_accept_req( void *private )
688 struct accept_req *req = private;
689 list_remove( &req->entry );
690 if (req->acceptsock)
692 req->acceptsock->accept_recv_req = NULL;
693 release_object( req->acceptsock );
695 release_object( req->async );
696 release_object( req->iosb );
697 release_object( req->sock );
698 free( req );
701 static void fill_accept_output( struct accept_req *req )
703 const data_size_t out_size = req->iosb->out_size;
704 struct async *async = req->async;
705 union unix_sockaddr unix_addr;
706 struct WS_sockaddr *win_addr;
707 unsigned int remote_len;
708 socklen_t unix_len;
709 int fd, size = 0;
710 char *out_data;
711 int win_len;
713 if (!(out_data = mem_alloc( out_size )))
715 async_terminate( async, get_error() );
716 return;
719 fd = get_unix_fd( req->acceptsock->fd );
721 if (req->recv_len && (size = recv( fd, out_data, req->recv_len, 0 )) < 0)
723 if (!req->accepted && errno == EWOULDBLOCK)
725 req->accepted = 1;
726 sock_reselect( req->acceptsock );
727 return;
730 async_terminate( async, sock_get_ntstatus( errno ) );
731 free( out_data );
732 return;
735 if (req->local_len)
737 if (req->local_len < sizeof(int))
739 async_terminate( async, STATUS_BUFFER_TOO_SMALL );
740 free( out_data );
741 return;
744 unix_len = sizeof(unix_addr);
745 win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + sizeof(int));
746 if (getsockname( fd, &unix_addr.addr, &unix_len ) < 0 ||
747 (win_len = sockaddr_from_unix( &unix_addr, win_addr, req->local_len - sizeof(int) )) < 0)
749 async_terminate( async, sock_get_ntstatus( errno ) );
750 free( out_data );
751 return;
753 memcpy( out_data + req->recv_len, &win_len, sizeof(int) );
756 unix_len = sizeof(unix_addr);
757 win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + req->local_len + sizeof(int));
758 remote_len = out_size - req->recv_len - req->local_len;
759 if (getpeername( fd, &unix_addr.addr, &unix_len ) < 0 ||
760 (win_len = sockaddr_from_unix( &unix_addr, win_addr, remote_len - sizeof(int) )) < 0)
762 async_terminate( async, sock_get_ntstatus( errno ) );
763 free( out_data );
764 return;
766 memcpy( out_data + req->recv_len + req->local_len, &win_len, sizeof(int) );
768 async_request_complete( req->async, STATUS_SUCCESS, size, out_size, out_data );
771 static void complete_async_accept( struct sock *sock, struct accept_req *req )
773 struct sock *acceptsock = req->acceptsock;
774 struct async *async = req->async;
776 if (debug_level) fprintf( stderr, "completing accept request for socket %p\n", sock );
778 if (acceptsock)
780 if (!accept_into_socket( sock, acceptsock ))
782 async_terminate( async, get_error() );
783 return;
785 fill_accept_output( req );
787 else
789 obj_handle_t handle;
791 if (!(acceptsock = accept_socket( sock )))
793 async_terminate( async, get_error() );
794 return;
796 handle = alloc_handle_no_access_check( async_get_thread( async )->process, &acceptsock->obj,
797 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
798 acceptsock->wparam = handle;
799 sock_reselect( acceptsock );
800 release_object( acceptsock );
801 if (!handle)
803 async_terminate( async, get_error() );
804 return;
807 async_request_complete_alloc( req->async, STATUS_SUCCESS, 0, sizeof(handle), &handle );
811 static void complete_async_accept_recv( struct accept_req *req )
813 if (debug_level) fprintf( stderr, "completing accept recv request for socket %p\n", req->acceptsock );
815 assert( req->recv_len );
817 fill_accept_output( req );
820 static void free_connect_req( void *private )
822 struct connect_req *req = private;
824 req->sock->connect_req = NULL;
825 release_object( req->async );
826 release_object( req->iosb );
827 release_object( req->sock );
828 free( req );
831 static void complete_async_connect( struct sock *sock )
833 struct connect_req *req = sock->connect_req;
834 const char *in_buffer;
835 size_t len;
836 int ret;
838 if (debug_level) fprintf( stderr, "completing connect request for socket %p\n", sock );
840 if (!req->send_len)
842 async_terminate( req->async, STATUS_SUCCESS );
843 return;
846 in_buffer = (const char *)req->iosb->in_data + sizeof(struct afd_connect_params) + req->addr_len;
847 len = req->send_len - req->send_cursor;
849 ret = send( get_unix_fd( sock->fd ), in_buffer + req->send_cursor, len, 0 );
850 if (ret < 0 && errno != EWOULDBLOCK)
851 async_terminate( req->async, sock_get_ntstatus( errno ) );
852 else if (ret == len)
853 async_request_complete( req->async, STATUS_SUCCESS, req->send_len, 0, NULL );
854 else
855 req->send_cursor += ret;
858 static void free_poll_req( void *private )
860 struct poll_req *req = private;
861 unsigned int i;
863 if (req->timeout) remove_timeout_user( req->timeout );
865 for (i = 0; i < req->count; ++i)
866 release_object( req->sockets[i].sock );
867 release_object( req->async );
868 release_object( req->iosb );
869 list_remove( &req->entry );
870 free( req );
873 static int is_oobinline( struct sock *sock )
875 int oobinline;
876 socklen_t len = sizeof(oobinline);
877 return !getsockopt( get_unix_fd( sock->fd ), SOL_SOCKET, SO_OOBINLINE, (char *)&oobinline, &len ) && oobinline;
880 static int get_poll_flags( struct sock *sock, int event )
882 int flags = 0;
884 /* A connection-mode socket which has never been connected does not return
885 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
886 if (sock->state == SOCK_UNCONNECTED)
887 event &= ~(POLLOUT | POLLHUP);
889 if (event & POLLIN)
891 if (sock->state == SOCK_LISTENING)
892 flags |= AFD_POLL_ACCEPT;
893 else
894 flags |= AFD_POLL_READ;
896 if (event & POLLPRI)
897 flags |= is_oobinline( sock ) ? AFD_POLL_READ : AFD_POLL_OOB;
898 if (event & POLLOUT)
899 flags |= AFD_POLL_WRITE;
900 if (sock->state == SOCK_CONNECTED)
901 flags |= AFD_POLL_CONNECT;
902 if (event & POLLHUP)
903 flags |= AFD_POLL_HUP;
904 if (event & POLLERR)
905 flags |= AFD_POLL_CONNECT_ERR;
906 if (sock->reset)
907 flags |= AFD_POLL_RESET;
909 return flags;
912 static void complete_async_poll( struct poll_req *req, unsigned int status )
914 unsigned int i, signaled_count = 0;
916 for (i = 0; i < req->count; ++i)
918 struct sock *sock = req->sockets[i].sock;
920 if (sock->main_poll == req)
921 sock->main_poll = NULL;
924 if (!status)
926 for (i = 0; i < req->count; ++i)
928 if (req->sockets[i].flags)
929 ++signaled_count;
933 if (is_machine_64bit( async_get_thread( req->async )->process->machine ))
935 size_t output_size = offsetof( struct afd_poll_params_64, sockets[signaled_count] );
936 struct afd_poll_params_64 *output;
938 if (!(output = mem_alloc( output_size )))
940 async_terminate( req->async, get_error() );
941 return;
943 memset( output, 0, output_size );
944 output->timeout = req->orig_timeout;
945 output->exclusive = req->exclusive;
946 for (i = 0; i < req->count; ++i)
948 if (!req->sockets[i].flags) continue;
949 output->sockets[output->count].socket = req->sockets[i].handle;
950 output->sockets[output->count].flags = req->sockets[i].flags;
951 output->sockets[output->count].status = req->sockets[i].status;
952 ++output->count;
954 assert( output->count == signaled_count );
956 async_request_complete( req->async, status, output_size, output_size, output );
958 else
960 size_t output_size = offsetof( struct afd_poll_params_32, sockets[signaled_count] );
961 struct afd_poll_params_32 *output;
963 if (!(output = mem_alloc( output_size )))
965 async_terminate( req->async, get_error() );
966 return;
968 memset( output, 0, output_size );
969 output->timeout = req->orig_timeout;
970 output->exclusive = req->exclusive;
971 for (i = 0; i < req->count; ++i)
973 if (!req->sockets[i].flags) continue;
974 output->sockets[output->count].socket = req->sockets[i].handle;
975 output->sockets[output->count].flags = req->sockets[i].flags;
976 output->sockets[output->count].status = req->sockets[i].status;
977 ++output->count;
979 assert( output->count == signaled_count );
981 async_request_complete( req->async, status, output_size, output_size, output );
985 static void complete_async_polls( struct sock *sock, int event, int error )
987 int flags = get_poll_flags( sock, event );
988 struct poll_req *req, *next;
990 LIST_FOR_EACH_ENTRY_SAFE( req, next, &poll_list, struct poll_req, entry )
992 unsigned int i;
994 if (req->iosb->status != STATUS_PENDING) continue;
996 for (i = 0; i < req->count; ++i)
998 if (req->sockets[i].sock != sock) continue;
999 if (!(req->sockets[i].mask & flags)) continue;
1001 if (debug_level)
1002 fprintf( stderr, "completing poll for socket %p, wanted %#x got %#x\n",
1003 sock, req->sockets[i].mask, flags );
1005 req->sockets[i].flags = req->sockets[i].mask & flags;
1006 req->sockets[i].status = sock_get_ntstatus( error );
1008 if (req->pending)
1010 complete_async_poll( req, STATUS_SUCCESS );
1011 break;
1017 static void async_poll_timeout( void *private )
1019 struct poll_req *req = private;
1021 req->timeout = NULL;
1023 if (req->iosb->status != STATUS_PENDING) return;
1025 complete_async_poll( req, STATUS_TIMEOUT );
1028 static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
1030 if (event & (POLLIN | POLLPRI))
1032 struct accept_req *req;
1034 LIST_FOR_EACH_ENTRY( req, &sock->accept_list, struct accept_req, entry )
1036 if (req->iosb->status == STATUS_PENDING && !req->accepted)
1038 complete_async_accept( sock, req );
1039 event &= ~POLLIN;
1040 break;
1044 if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
1045 complete_async_accept_recv( sock->accept_recv_req );
1048 if ((event & POLLOUT) && sock->connect_req && sock->connect_req->iosb->status == STATUS_PENDING)
1049 complete_async_connect( sock );
1051 if ((event & (POLLIN | POLLPRI)) && async_queued( &sock->read_q ))
1053 if (async_waiting( &sock->read_q ))
1055 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
1056 async_wake_up( &sock->read_q, STATUS_ALERTED );
1058 event &= ~(POLLIN | POLLPRI);
1061 if ((event & POLLOUT) && async_queued( &sock->write_q ))
1063 if (async_waiting( &sock->write_q ))
1065 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
1066 async_wake_up( &sock->write_q, STATUS_ALERTED );
1068 event &= ~POLLOUT;
1071 if (event & (POLLERR | POLLHUP))
1073 int status = sock_get_ntstatus( error );
1074 struct accept_req *req, *next;
1076 async_wake_up( &sock->read_q, status );
1077 async_wake_up( &sock->write_q, status );
1079 LIST_FOR_EACH_ENTRY_SAFE( req, next, &sock->accept_list, struct accept_req, entry )
1081 if (req->iosb->status == STATUS_PENDING)
1082 async_terminate( req->async, status );
1085 if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
1086 async_terminate( sock->accept_recv_req->async, status );
1088 if (sock->connect_req)
1089 async_terminate( sock->connect_req->async, status );
1092 if (sock->reset)
1094 async_wake_up( &sock->read_q, STATUS_CONNECTION_RESET );
1095 async_wake_up( &sock->write_q, STATUS_CONNECTION_RESET );
1097 if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
1098 async_terminate( sock->accept_recv_req->async, STATUS_CONNECTION_RESET );
1101 return event;
1104 static void post_socket_event( struct sock *sock, enum afd_poll_bit event_bit )
1106 unsigned int event = (1 << event_bit);
1108 if (!(sock->reported_events & event))
1110 sock->pending_events |= event;
1111 sock->reported_events |= event;
1115 static void sock_dispatch_events( struct sock *sock, enum connection_state prevstate, int event )
1117 switch (prevstate)
1119 case SOCK_UNCONNECTED:
1120 break;
1122 case SOCK_CONNECTING:
1123 if (event & POLLOUT)
1124 post_socket_event( sock, AFD_POLL_BIT_CONNECT );
1125 if (event & (POLLERR | POLLHUP))
1126 post_socket_event( sock, AFD_POLL_BIT_CONNECT_ERR );
1127 break;
1129 case SOCK_LISTENING:
1130 if (event & (POLLIN | POLLERR | POLLHUP))
1131 post_socket_event( sock, AFD_POLL_BIT_ACCEPT );
1132 break;
1134 case SOCK_CONNECTED:
1135 case SOCK_CONNECTIONLESS:
1136 if (sock->reset)
1137 post_socket_event( sock, AFD_POLL_BIT_RESET );
1139 if (event & POLLIN)
1140 post_socket_event( sock, AFD_POLL_BIT_READ );
1142 if (event & POLLOUT)
1143 post_socket_event( sock, AFD_POLL_BIT_WRITE );
1145 if (event & POLLPRI)
1146 post_socket_event( sock, AFD_POLL_BIT_OOB );
1148 if (event & (POLLERR | POLLHUP))
1149 post_socket_event( sock, AFD_POLL_BIT_HUP );
1150 break;
1153 sock_wake_up( sock );
1156 static void sock_poll_event( struct fd *fd, int event )
1158 struct sock *sock = get_fd_user( fd );
1159 int hangup_seen = 0;
1160 enum connection_state prevstate = sock->state;
1161 int error = 0;
1163 assert( sock->obj.ops == &sock_ops );
1164 if (debug_level)
1165 fprintf(stderr, "socket %p select event: %x\n", sock, event);
1167 if (event & (POLLERR | POLLHUP))
1168 error = sock_error( sock );
1170 switch (sock->state)
1172 case SOCK_UNCONNECTED:
1173 break;
1175 case SOCK_CONNECTING:
1176 if (event & (POLLERR|POLLHUP))
1178 sock->state = SOCK_UNCONNECTED;
1179 event &= ~POLLOUT;
1181 else if (event & POLLOUT)
1183 sock->state = SOCK_CONNECTED;
1184 sock->connect_time = current_time;
1185 sock->errors[AFD_POLL_BIT_CONNECT_ERR] = 0;
1187 break;
1189 case SOCK_LISTENING:
1190 break;
1192 case SOCK_CONNECTED:
1193 case SOCK_CONNECTIONLESS:
1194 if (sock->reset)
1195 event &= ~(POLLIN | POLLERR | POLLHUP);
1197 if (sock->type == WS_SOCK_STREAM && (event & POLLIN))
1199 char dummy;
1200 int nr;
1202 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
1203 * has been closed, so we need to check for it explicitly here */
1204 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
1205 if ( nr == 0 )
1207 hangup_seen = 1;
1208 event &= ~POLLIN;
1210 else if ( nr < 0 )
1212 event &= ~POLLIN;
1213 /* EAGAIN can happen if an async recv() falls between the server's poll()
1214 call and the invocation of this routine */
1215 if (errno == ECONNRESET || errno == EPIPE)
1217 sock->reset = 1;
1219 else if (errno != EAGAIN)
1221 error = errno;
1222 event |= POLLERR;
1223 sock->errors[AFD_POLL_BIT_HUP] = error;
1224 if ( debug_level )
1225 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
1230 if (hangup_seen || (sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP)))
1232 sock->hangup = 1;
1234 else if (event & (POLLHUP | POLLERR))
1236 sock->aborted = 1;
1238 if (debug_level)
1239 fprintf( stderr, "socket %p aborted by error %d, event %#x\n", sock, error, event );
1242 if (hangup_seen)
1243 event |= POLLHUP;
1244 break;
1247 event = sock_dispatch_asyncs( sock, event, error );
1248 sock_dispatch_events( sock, prevstate, event );
1249 complete_async_polls( sock, event, error );
1251 sock_reselect( sock );
1254 static void sock_dump( struct object *obj, int verbose )
1256 struct sock *sock = (struct sock *)obj;
1257 assert( obj->ops == &sock_ops );
1258 fprintf( stderr, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
1259 sock->fd, sock->state,
1260 sock->mask, sock->pending_events, sock->reported_events );
1263 static int poll_flags_from_afd( struct sock *sock, int flags )
1265 int ev = 0;
1267 /* A connection-mode socket which has never been connected does
1268 * not return write or hangup events, but Linux returns
1269 * POLLOUT | POLLHUP. */
1270 if (sock->state == SOCK_UNCONNECTED)
1271 return -1;
1273 if (flags & (AFD_POLL_READ | AFD_POLL_ACCEPT))
1274 ev |= POLLIN;
1275 if ((flags & AFD_POLL_HUP) && sock->type == WS_SOCK_STREAM)
1276 ev |= POLLIN;
1277 if (flags & AFD_POLL_OOB)
1278 ev |= is_oobinline( sock ) ? POLLIN : POLLPRI;
1279 if (flags & AFD_POLL_WRITE)
1280 ev |= POLLOUT;
1282 return ev;
1285 static int sock_get_poll_events( struct fd *fd )
1287 struct sock *sock = get_fd_user( fd );
1288 unsigned int mask = sock->mask & ~sock->reported_events;
1289 struct poll_req *req;
1290 int ev = 0;
1292 assert( sock->obj.ops == &sock_ops );
1294 if (!sock->type) /* not initialized yet */
1295 return -1;
1297 LIST_FOR_EACH_ENTRY( req, &poll_list, struct poll_req, entry )
1299 unsigned int i;
1301 for (i = 0; i < req->count; ++i)
1303 if (req->sockets[i].sock != sock) continue;
1305 ev |= poll_flags_from_afd( sock, req->sockets[i].mask );
1309 switch (sock->state)
1311 case SOCK_UNCONNECTED:
1312 /* A connection-mode Windows socket which has never been connected does
1313 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1314 * need to return -1 here, to prevent the socket from being polled on at
1315 * all. */
1316 return -1;
1318 case SOCK_CONNECTING:
1319 return POLLOUT;
1321 case SOCK_LISTENING:
1322 if (!list_empty( &sock->accept_list ) || (mask & AFD_POLL_ACCEPT))
1323 ev |= POLLIN;
1324 break;
1326 case SOCK_CONNECTED:
1327 case SOCK_CONNECTIONLESS:
1328 if (sock->hangup && sock->wr_shutdown && !sock->wr_shutdown_pending)
1330 /* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
1331 * if both the socket and its peer are SHUT_WR.
1333 * We don't use SHUT_RD, so we can only encounter this in the latter
1334 * case. In that case there can't be any pending read requests (they
1335 * would have already been completed with a length of zero), the
1336 * above condition ensures that we don't have any pending write
1337 * requests, and nothing that can change about the socket state that
1338 * would complete a pending poll request. */
1339 return -1;
1342 if (sock->aborted || sock->reset)
1343 return -1;
1345 if (sock->accept_recv_req)
1347 ev |= POLLIN;
1349 else if (async_queued( &sock->read_q ))
1351 /* Clear POLLIN and POLLPRI if we have an alerted async, even if
1352 * we're polling this socket for READ or OOB. We can't signal the
1353 * poll if the pending async will read all of the data [cf. the
1354 * matching logic in sock_dispatch_asyncs()], but we also don't
1355 * want to spin polling for POLLIN if we're not going to use it. */
1356 if (async_waiting( &sock->read_q ))
1357 ev |= POLLIN | POLLPRI;
1358 else
1359 ev &= ~(POLLIN | POLLPRI);
1361 else
1363 /* Don't ask for POLLIN if we got a hangup. We won't receive more
1364 * data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
1365 if (!sock->hangup)
1367 if (mask & AFD_POLL_READ)
1368 ev |= POLLIN;
1369 if (mask & AFD_POLL_OOB)
1370 ev |= POLLPRI;
1373 /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
1374 if (sock->state == SOCK_CONNECTED && (mask & AFD_POLL_HUP) && !(sock->reported_events & AFD_POLL_READ))
1375 ev |= POLLIN;
1378 if (async_queued( &sock->write_q ))
1380 /* As with read asyncs above, clear POLLOUT if we have an alerted
1381 * async. */
1382 if (async_waiting( &sock->write_q ))
1383 ev |= POLLOUT;
1384 else
1385 ev &= ~POLLOUT;
1387 else if (!sock->wr_shutdown && (mask & AFD_POLL_WRITE))
1389 ev |= POLLOUT;
1392 break;
1395 return ev;
1398 static enum server_fd_type sock_get_fd_type( struct fd *fd )
1400 return FD_TYPE_SOCKET;
1403 static void sock_cancel_async( struct fd *fd, struct async *async )
1405 struct poll_req *req;
1407 LIST_FOR_EACH_ENTRY( req, &poll_list, struct poll_req, entry )
1409 unsigned int i;
1411 if (req->async != async)
1412 continue;
1414 for (i = 0; i < req->count; i++)
1416 struct sock *sock = req->sockets[i].sock;
1418 if (sock->main_poll == req)
1419 sock->main_poll = NULL;
1423 async_terminate( async, STATUS_CANCELLED );
1426 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
1428 struct sock *sock = get_fd_user( fd );
1430 if (sock->wr_shutdown_pending && list_empty( &sock->write_q.queue ))
1432 shutdown( get_unix_fd( sock->fd ), SHUT_WR );
1433 sock->wr_shutdown_pending = 0;
1436 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1437 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1438 * a pseudo-fd. */
1439 if (queue != &sock->ifchange_q && sock->type)
1440 sock_reselect( sock );
1443 static struct fd *sock_get_fd( struct object *obj )
1445 struct sock *sock = (struct sock *)obj;
1446 return (struct fd *)grab_object( sock->fd );
1449 static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
1451 struct sock *sock = (struct sock *)obj;
1453 if (sock->obj.handle_count == 1) /* last handle */
1455 struct accept_req *accept_req, *accept_next;
1456 struct poll_req *poll_req, *poll_next;
1458 if (sock->accept_recv_req)
1459 async_terminate( sock->accept_recv_req->async, STATUS_CANCELLED );
1461 LIST_FOR_EACH_ENTRY_SAFE( accept_req, accept_next, &sock->accept_list, struct accept_req, entry )
1462 async_terminate( accept_req->async, STATUS_CANCELLED );
1464 if (sock->connect_req)
1465 async_terminate( sock->connect_req->async, STATUS_CANCELLED );
1467 LIST_FOR_EACH_ENTRY_SAFE( poll_req, poll_next, &poll_list, struct poll_req, entry )
1469 struct iosb *iosb = poll_req->iosb;
1470 BOOL signaled = FALSE;
1471 unsigned int i;
1473 if (iosb->status != STATUS_PENDING) continue;
1475 for (i = 0; i < poll_req->count; ++i)
1477 if (poll_req->sockets[i].sock == sock)
1479 signaled = TRUE;
1480 poll_req->sockets[i].flags = AFD_POLL_CLOSE;
1481 poll_req->sockets[i].status = 0;
1485 if (signaled) complete_async_poll( poll_req, STATUS_SUCCESS );
1489 return 1;
1492 static void sock_destroy( struct object *obj )
1494 struct sock *sock = (struct sock *)obj;
1496 assert( obj->ops == &sock_ops );
1498 /* FIXME: special socket shutdown stuff? */
1500 if ( sock->deferred )
1501 release_object( sock->deferred );
1503 async_wake_up( &sock->ifchange_q, STATUS_CANCELLED );
1504 sock_release_ifchange( sock );
1505 free_async_queue( &sock->read_q );
1506 free_async_queue( &sock->write_q );
1507 free_async_queue( &sock->ifchange_q );
1508 free_async_queue( &sock->accept_q );
1509 free_async_queue( &sock->connect_q );
1510 free_async_queue( &sock->poll_q );
1511 if (sock->event) release_object( sock->event );
1512 if (sock->fd) release_object( sock->fd );
1515 static struct sock *create_socket(void)
1517 struct sock *sock;
1519 if (!(sock = alloc_object( &sock_ops ))) return NULL;
1520 sock->fd = NULL;
1521 sock->state = SOCK_UNCONNECTED;
1522 sock->mask = 0;
1523 sock->pending_events = 0;
1524 sock->reported_events = 0;
1525 sock->proto = 0;
1526 sock->type = 0;
1527 sock->family = 0;
1528 sock->event = NULL;
1529 sock->window = 0;
1530 sock->message = 0;
1531 sock->wparam = 0;
1532 sock->connect_time = 0;
1533 sock->deferred = NULL;
1534 sock->ifchange_obj = NULL;
1535 sock->accept_recv_req = NULL;
1536 sock->connect_req = NULL;
1537 sock->main_poll = NULL;
1538 memset( &sock->addr, 0, sizeof(sock->addr) );
1539 sock->addr_len = 0;
1540 sock->rd_shutdown = 0;
1541 sock->wr_shutdown = 0;
1542 sock->wr_shutdown_pending = 0;
1543 sock->hangup = 0;
1544 sock->aborted = 0;
1545 sock->nonblocking = 0;
1546 sock->bound = 0;
1547 sock->reset = 0;
1548 sock->rcvbuf = 0;
1549 sock->sndbuf = 0;
1550 sock->rcvtimeo = 0;
1551 sock->sndtimeo = 0;
1552 sock->icmp_fixup_data_len = 0;
1553 init_async_queue( &sock->read_q );
1554 init_async_queue( &sock->write_q );
1555 init_async_queue( &sock->ifchange_q );
1556 init_async_queue( &sock->accept_q );
1557 init_async_queue( &sock->connect_q );
1558 init_async_queue( &sock->poll_q );
1559 memset( sock->errors, 0, sizeof(sock->errors) );
1560 list_init( &sock->accept_list );
1561 return sock;
1564 static int get_unix_family( int family )
1566 switch (family)
1568 case WS_AF_INET: return AF_INET;
1569 case WS_AF_INET6: return AF_INET6;
1570 #ifdef HAS_IPX
1571 case WS_AF_IPX: return AF_IPX;
1572 #endif
1573 #ifdef AF_IRDA
1574 case WS_AF_IRDA: return AF_IRDA;
1575 #endif
1576 case WS_AF_UNSPEC: return AF_UNSPEC;
1577 default: return -1;
1581 static int get_unix_type( int type )
1583 switch (type)
1585 case WS_SOCK_DGRAM: return SOCK_DGRAM;
1586 case WS_SOCK_RAW: return SOCK_RAW;
1587 case WS_SOCK_STREAM: return SOCK_STREAM;
1588 default: return -1;
1592 static int get_unix_protocol( int protocol )
1594 if (protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
1595 return protocol;
1597 switch (protocol)
1599 case WS_IPPROTO_ICMP: return IPPROTO_ICMP;
1600 case WS_IPPROTO_IGMP: return IPPROTO_IGMP;
1601 case WS_IPPROTO_IP: return IPPROTO_IP;
1602 case WS_IPPROTO_IPV4: return IPPROTO_IPIP;
1603 case WS_IPPROTO_IPV6: return IPPROTO_IPV6;
1604 case WS_IPPROTO_RAW: return IPPROTO_RAW;
1605 case WS_IPPROTO_TCP: return IPPROTO_TCP;
1606 case WS_IPPROTO_UDP: return IPPROTO_UDP;
1607 default: return -1;
1611 static void set_dont_fragment( int fd, int level, int value )
1613 int optname;
1615 if (level == IPPROTO_IP)
1617 #ifdef IP_DONTFRAG
1618 optname = IP_DONTFRAG;
1619 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1620 optname = IP_MTU_DISCOVER;
1621 value = value ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
1622 #else
1623 return;
1624 #endif
1626 else
1628 #ifdef IPV6_DONTFRAG
1629 optname = IPV6_DONTFRAG;
1630 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1631 optname = IPV6_MTU_DISCOVER;
1632 value = value ? IPV6_PMTUDISC_DO : IPV6_PMTUDISC_DONT;
1633 #else
1634 return;
1635 #endif
1638 setsockopt( fd, level, optname, &value, sizeof(value) );
1641 static int init_socket( struct sock *sock, int family, int type, int protocol )
1643 unsigned int options = 0;
1644 int sockfd, unix_type, unix_family, unix_protocol, value;
1645 socklen_t len;
1647 unix_family = get_unix_family( family );
1648 unix_type = get_unix_type( type );
1649 unix_protocol = get_unix_protocol( protocol );
1651 if (unix_protocol < 0)
1653 if (type && unix_type < 0)
1654 set_win32_error( WSAESOCKTNOSUPPORT );
1655 else
1656 set_win32_error( WSAEPROTONOSUPPORT );
1657 return -1;
1659 if (unix_family < 0)
1661 if (family >= 0 && unix_type < 0)
1662 set_win32_error( WSAESOCKTNOSUPPORT );
1663 else
1664 set_win32_error( WSAEAFNOSUPPORT );
1665 return -1;
1668 sockfd = socket( unix_family, unix_type, unix_protocol );
1670 #ifdef linux
1671 if (sockfd == -1 && errno == EPERM && unix_family == AF_INET
1672 && unix_type == SOCK_RAW && unix_protocol == IPPROTO_ICMP)
1674 sockfd = socket( unix_family, SOCK_DGRAM, unix_protocol );
1675 if (sockfd != -1)
1677 const int val = 1;
1679 setsockopt( sockfd, IPPROTO_IP, IP_RECVTTL, (const char *)&val, sizeof(val) );
1680 setsockopt( sockfd, IPPROTO_IP, IP_RECVTOS, (const char *)&val, sizeof(val) );
1681 setsockopt( sockfd, IPPROTO_IP, IP_PKTINFO, (const char *)&val, sizeof(val) );
1684 #endif
1686 if (sockfd == -1)
1688 if (errno == EINVAL) set_win32_error( WSAESOCKTNOSUPPORT );
1689 else set_win32_error( sock_get_error( errno ));
1690 return -1;
1692 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
1694 if (family == WS_AF_IPX && protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
1696 #ifdef HAS_IPX
1697 int ipx_type = protocol - WS_NSPROTO_IPX;
1699 #ifdef SOL_IPX
1700 setsockopt( sockfd, SOL_IPX, IPX_TYPE, &ipx_type, sizeof(ipx_type) );
1701 #else
1702 struct ipx val;
1703 /* Should we retrieve val using a getsockopt call and then
1704 * set the modified one? */
1705 val.ipx_pt = ipx_type;
1706 setsockopt( sockfd, 0, SO_DEFAULT_HEADERS, &val, sizeof(val) );
1707 #endif
1708 #endif
1711 if (unix_family == AF_INET || unix_family == AF_INET6)
1713 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1714 if (unix_type == SOCK_DGRAM || unix_type == SOCK_RAW) /* in Linux the global default can be enabled */
1715 set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, FALSE );
1716 else if (unix_type == SOCK_STREAM)
1717 set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, TRUE );
1720 #ifdef IPV6_V6ONLY
1721 if (unix_family == AF_INET6)
1723 static const int enable = 1;
1724 setsockopt( sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &enable, sizeof(enable) );
1726 #endif
1728 len = sizeof(value);
1729 if (!getsockopt( sockfd, SOL_SOCKET, SO_RCVBUF, &value, &len ))
1730 sock->rcvbuf = value;
1732 len = sizeof(value);
1733 if (!getsockopt( sockfd, SOL_SOCKET, SO_SNDBUF, &value, &len ))
1734 sock->sndbuf = value;
1736 sock->state = (type == WS_SOCK_STREAM ? SOCK_UNCONNECTED : SOCK_CONNECTIONLESS);
1737 sock->proto = protocol;
1738 sock->type = type;
1739 sock->family = family;
1741 if (sock->fd)
1743 options = get_fd_options( sock->fd );
1744 release_object( sock->fd );
1747 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj, options )))
1749 return -1;
1752 /* We can't immediately allow caching for a connection-mode socket, since it
1753 * might be accepted into (changing the underlying fd object.) */
1754 if (sock->type != WS_SOCK_STREAM) allow_fd_caching( sock->fd );
1756 return 0;
1759 /* accepts a socket and inits it */
1760 static int accept_new_fd( struct sock *sock )
1763 /* Try to accept(2). We can't be safe that this an already connected socket
1764 * or that accept() is allowed on it. In those cases we will get -1/errno
1765 * return.
1767 struct sockaddr saddr;
1768 socklen_t slen = sizeof(saddr);
1769 int acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen );
1770 if (acceptfd != -1)
1771 fcntl( acceptfd, F_SETFL, O_NONBLOCK );
1772 else
1773 set_error( sock_get_ntstatus( errno ));
1774 return acceptfd;
1777 /* accept a socket (creates a new fd) */
1778 static struct sock *accept_socket( struct sock *sock )
1780 struct sock *acceptsock;
1781 int acceptfd;
1783 if (get_unix_fd( sock->fd ) == -1) return NULL;
1785 if ( sock->deferred )
1787 acceptsock = sock->deferred;
1788 sock->deferred = NULL;
1790 else
1792 union unix_sockaddr unix_addr;
1793 socklen_t unix_len;
1795 if ((acceptfd = accept_new_fd( sock )) == -1) return NULL;
1796 if (!(acceptsock = create_socket()))
1798 close( acceptfd );
1799 return NULL;
1802 /* newly created socket gets the same properties of the listening socket */
1803 acceptsock->state = SOCK_CONNECTED;
1804 acceptsock->bound = 1;
1805 acceptsock->nonblocking = sock->nonblocking;
1806 acceptsock->mask = sock->mask;
1807 acceptsock->proto = sock->proto;
1808 acceptsock->type = sock->type;
1809 acceptsock->family = sock->family;
1810 acceptsock->window = sock->window;
1811 acceptsock->message = sock->message;
1812 acceptsock->connect_time = current_time;
1813 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
1814 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
1815 get_fd_options( sock->fd ) )))
1817 release_object( acceptsock );
1818 return NULL;
1820 unix_len = sizeof(unix_addr);
1821 if (!getsockname( acceptfd, &unix_addr.addr, &unix_len ))
1822 acceptsock->addr_len = sockaddr_from_unix( &unix_addr, &acceptsock->addr.addr, sizeof(acceptsock->addr) );
1824 clear_error();
1825 sock->pending_events &= ~AFD_POLL_ACCEPT;
1826 sock->reported_events &= ~AFD_POLL_ACCEPT;
1827 sock_reselect( sock );
1828 return acceptsock;
1831 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
1833 union unix_sockaddr unix_addr;
1834 socklen_t unix_len;
1835 int acceptfd;
1836 struct fd *newfd;
1838 if (get_unix_fd( sock->fd ) == -1) return FALSE;
1840 if ( sock->deferred )
1842 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
1843 get_fd_options( acceptsock->fd ) );
1844 if ( !newfd )
1845 return FALSE;
1847 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
1849 release_object( sock->deferred );
1850 sock->deferred = NULL;
1852 else
1854 if ((acceptfd = accept_new_fd( sock )) == -1)
1855 return FALSE;
1857 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
1858 get_fd_options( acceptsock->fd ) )))
1859 return FALSE;
1862 acceptsock->state = SOCK_CONNECTED;
1863 acceptsock->bound = 1;
1864 acceptsock->pending_events = 0;
1865 acceptsock->reported_events = 0;
1866 acceptsock->proto = sock->proto;
1867 acceptsock->type = sock->type;
1868 acceptsock->family = sock->family;
1869 acceptsock->wparam = 0;
1870 acceptsock->deferred = NULL;
1871 acceptsock->connect_time = current_time;
1872 fd_copy_completion( acceptsock->fd, newfd );
1873 release_object( acceptsock->fd );
1874 acceptsock->fd = newfd;
1876 unix_len = sizeof(unix_addr);
1877 if (!getsockname( get_unix_fd( newfd ), &unix_addr.addr, &unix_len ))
1878 acceptsock->addr_len = sockaddr_from_unix( &unix_addr, &acceptsock->addr.addr, sizeof(acceptsock->addr) );
1880 clear_error();
1881 sock->pending_events &= ~AFD_POLL_ACCEPT;
1882 sock->reported_events &= ~AFD_POLL_ACCEPT;
1883 sock_reselect( sock );
1885 return TRUE;
1888 #ifdef IP_BOUND_IF
1890 static int bind_to_iface_name( int fd, in_addr_t bind_addr, const char *name )
1892 static const int enable = 1;
1893 unsigned int index;
1895 if (!(index = if_nametoindex( name )))
1896 return -1;
1898 if (setsockopt( fd, IPPROTO_IP, IP_BOUND_IF, &index, sizeof(index) ))
1899 return -1;
1901 return setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable) );
1904 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER) && defined(SO_BINDTODEVICE)
1906 struct interface_filter
1908 struct sock_filter iface_memaddr;
1909 struct sock_filter iface_rule;
1910 struct sock_filter ip_memaddr;
1911 struct sock_filter ip_rule;
1912 struct sock_filter return_keep;
1913 struct sock_filter return_dump;
1915 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
1916 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1917 /sizeof(struct sock_filter)
1918 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
1919 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1920 /sizeof(struct sock_filter)
1921 # define FILTER_JUMP_NEXT() (u_char)(0)
1922 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
1923 static struct interface_filter generic_interface_filter =
1925 /* This filter rule allows incoming packets on the specified interface, which works for all
1926 * remotely generated packets and for locally generated broadcast packets. */
1927 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, SKF_AD_OFF+SKF_AD_IFINDEX),
1928 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule), FILTER_JUMP_NEXT()),
1929 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
1930 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
1931 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, SKF_NET_OFF+SKF_NET_DESTIP),
1932 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule), FILTER_JUMP_DUMP(ip_rule)),
1933 BPF_STMT(BPF_RET+BPF_K, (u_int)-1), /* keep packet */
1934 BPF_STMT(BPF_RET+BPF_K, 0) /* dump packet */
1937 static int bind_to_iface_name( int fd, in_addr_t bind_addr, const char *name )
1939 struct interface_filter specific_interface_filter;
1940 struct sock_fprog filter_prog;
1941 static const int enable = 1;
1942 unsigned int index;
1943 in_addr_t ifindex;
1945 if (!setsockopt( fd, SOL_SOCKET, SO_BINDTODEVICE, name, strlen( name ) + 1 ))
1946 return 0;
1948 /* SO_BINDTODEVICE requires NET_CAP_RAW until Linux 5.7. */
1949 if (debug_level)
1950 fprintf( stderr, "setsockopt SO_BINDTODEVICE fd %d, name %s failed: %s, falling back to SO_REUSE_ADDR\n",
1951 fd, name, strerror( errno ));
1953 if (!(index = if_nametoindex( name )))
1954 return -1;
1956 ifindex = htonl( index );
1957 if (setsockopt( fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex, sizeof(ifindex) ) < 0)
1958 return -1;
1960 specific_interface_filter = generic_interface_filter;
1961 specific_interface_filter.iface_rule.k = index;
1962 specific_interface_filter.ip_rule.k = htonl( bind_addr );
1963 filter_prog.len = sizeof(generic_interface_filter) / sizeof(struct sock_filter);
1964 filter_prog.filter = (struct sock_filter *)&specific_interface_filter;
1965 if (setsockopt( fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, sizeof(filter_prog) ))
1966 return -1;
1968 return setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable) );
1971 #else
1973 static int bind_to_iface_name( int fd, in_addr_t bind_addr, const char *name )
1975 errno = EOPNOTSUPP;
1976 return -1;
1979 #endif /* LINUX_BOUND_IF */
1981 /* Take bind() calls on any name corresponding to a local network adapter and
1982 * restrict the given socket to operating only on the specified interface. This
1983 * restriction consists of two components:
1984 * 1) An outgoing packet restriction suggesting the egress interface for all
1985 * packets.
1986 * 2) An incoming packet restriction dropping packets not meant for the
1987 * interface.
1988 * If the function succeeds in placing these restrictions, then the name for the
1989 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
1990 * receipt of broadcast packets on the socket. This behavior is only relevant to
1991 * UDP sockets and is needed for applications that expect to be able to receive
1992 * broadcast packets on a socket that is bound to a specific network interface.
1994 static int bind_to_interface( struct sock *sock, const struct sockaddr_in *addr )
1996 in_addr_t bind_addr = addr->sin_addr.s_addr;
1997 struct ifaddrs *ifaddrs, *ifaddr;
1998 int fd = get_unix_fd( sock->fd );
1999 int err = 0;
2001 if (bind_addr == htonl( INADDR_ANY ) || bind_addr == htonl( INADDR_LOOPBACK ))
2002 return 0;
2003 if (sock->type != WS_SOCK_DGRAM)
2004 return 0;
2006 if (getifaddrs( &ifaddrs ) < 0) return 0;
2008 for (ifaddr = ifaddrs; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2010 if (ifaddr->ifa_addr && ifaddr->ifa_addr->sa_family == AF_INET
2011 && ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr == bind_addr)
2013 if ((err = bind_to_iface_name( fd, bind_addr, ifaddr->ifa_name )) < 0)
2015 if (debug_level)
2016 fprintf( stderr, "failed to bind to interface: %s\n", strerror( errno ) );
2018 break;
2021 freeifaddrs( ifaddrs );
2022 return !err;
2025 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2026 static unsigned int get_ipv6_interface_index( const struct in6_addr *addr )
2028 struct ifaddrs *ifaddrs, *ifaddr;
2030 if (getifaddrs( &ifaddrs ) < 0) return 0;
2032 for (ifaddr = ifaddrs; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2034 if (ifaddr->ifa_addr && ifaddr->ifa_addr->sa_family == AF_INET6
2035 && !memcmp( &((struct sockaddr_in6 *)ifaddr->ifa_addr)->sin6_addr, addr, sizeof(*addr) ))
2037 unsigned int index = if_nametoindex( ifaddr->ifa_name );
2039 if (!index)
2041 if (debug_level)
2042 fprintf( stderr, "Unable to look up interface index for %s: %s\n",
2043 ifaddr->ifa_name, strerror( errno ) );
2044 continue;
2047 freeifaddrs( ifaddrs );
2048 return index;
2052 freeifaddrs( ifaddrs );
2053 return 0;
2055 #endif
2057 /* return an errno value mapped to a WSA error */
2058 static unsigned int sock_get_error( int err )
2060 switch (err)
2062 case EINTR: return WSAEINTR;
2063 case EBADF: return WSAEBADF;
2064 case EPERM:
2065 case EACCES: return WSAEACCES;
2066 case EFAULT: return WSAEFAULT;
2067 case EINVAL: return WSAEINVAL;
2068 case EMFILE: return WSAEMFILE;
2069 case EINPROGRESS:
2070 case EWOULDBLOCK: return WSAEWOULDBLOCK;
2071 case EALREADY: return WSAEALREADY;
2072 case ENOTSOCK: return WSAENOTSOCK;
2073 case EDESTADDRREQ: return WSAEDESTADDRREQ;
2074 case EMSGSIZE: return WSAEMSGSIZE;
2075 case EPROTOTYPE: return WSAEPROTOTYPE;
2076 case ENOPROTOOPT: return WSAENOPROTOOPT;
2077 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
2078 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
2079 case EOPNOTSUPP: return WSAEOPNOTSUPP;
2080 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
2081 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
2082 case EADDRINUSE: return WSAEADDRINUSE;
2083 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
2084 case ENETDOWN: return WSAENETDOWN;
2085 case ENETUNREACH: return WSAENETUNREACH;
2086 case ENETRESET: return WSAENETRESET;
2087 case ECONNABORTED: return WSAECONNABORTED;
2088 case EPIPE:
2089 case ECONNRESET: return WSAECONNRESET;
2090 case ENOBUFS: return WSAENOBUFS;
2091 case EISCONN: return WSAEISCONN;
2092 case ENOTCONN: return WSAENOTCONN;
2093 case ESHUTDOWN: return WSAESHUTDOWN;
2094 case ETOOMANYREFS: return WSAETOOMANYREFS;
2095 case ETIMEDOUT: return WSAETIMEDOUT;
2096 case ECONNREFUSED: return WSAECONNREFUSED;
2097 case ELOOP: return WSAELOOP;
2098 case ENAMETOOLONG: return WSAENAMETOOLONG;
2099 case EHOSTDOWN: return WSAEHOSTDOWN;
2100 case EHOSTUNREACH: return WSAEHOSTUNREACH;
2101 case ENOTEMPTY: return WSAENOTEMPTY;
2102 #ifdef EPROCLIM
2103 case EPROCLIM: return WSAEPROCLIM;
2104 #endif
2105 #ifdef EUSERS
2106 case EUSERS: return WSAEUSERS;
2107 #endif
2108 #ifdef EDQUOT
2109 case EDQUOT: return WSAEDQUOT;
2110 #endif
2111 #ifdef ESTALE
2112 case ESTALE: return WSAESTALE;
2113 #endif
2114 #ifdef EREMOTE
2115 case EREMOTE: return WSAEREMOTE;
2116 #endif
2118 case 0: return 0;
2119 default:
2120 errno = err;
2121 perror("wineserver: sock_get_error() can't map error");
2122 return WSAEFAULT;
2126 static int sock_get_ntstatus( int err )
2128 switch ( err )
2130 case EBADF: return STATUS_INVALID_HANDLE;
2131 case EBUSY: return STATUS_DEVICE_BUSY;
2132 case EPERM:
2133 case EACCES: return STATUS_ACCESS_DENIED;
2134 case EFAULT: return STATUS_ACCESS_VIOLATION;
2135 case EINVAL: return STATUS_INVALID_PARAMETER;
2136 case ENFILE:
2137 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
2138 case EINPROGRESS:
2139 case EWOULDBLOCK: return STATUS_DEVICE_NOT_READY;
2140 case EALREADY: return STATUS_NETWORK_BUSY;
2141 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
2142 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
2143 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
2144 case EPROTONOSUPPORT:
2145 case ESOCKTNOSUPPORT:
2146 case EPFNOSUPPORT:
2147 case EAFNOSUPPORT:
2148 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
2149 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
2150 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
2151 case EADDRINUSE: return STATUS_SHARING_VIOLATION;
2152 /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
2153 * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
2154 case ENODEV:
2155 case EADDRNOTAVAIL: return STATUS_INVALID_ADDRESS_COMPONENT;
2156 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
2157 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
2158 case ENOTCONN: return STATUS_INVALID_CONNECTION;
2159 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
2160 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
2161 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
2162 case ENETDOWN: return STATUS_NETWORK_BUSY;
2163 case EPIPE:
2164 case ECONNRESET: return STATUS_CONNECTION_RESET;
2165 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
2166 case EISCONN: return STATUS_CONNECTION_ACTIVE;
2168 case 0: return STATUS_SUCCESS;
2169 default:
2170 errno = err;
2171 perror("wineserver: sock_get_ntstatus() can't map error");
2172 return STATUS_UNSUCCESSFUL;
2176 static struct accept_req *alloc_accept_req( struct sock *sock, struct sock *acceptsock, struct async *async,
2177 const struct afd_accept_into_params *params )
2179 struct accept_req *req = mem_alloc( sizeof(*req) );
2181 if (req)
2183 req->async = (struct async *)grab_object( async );
2184 req->iosb = async_get_iosb( async );
2185 req->sock = (struct sock *)grab_object( sock );
2186 req->acceptsock = acceptsock;
2187 if (acceptsock) grab_object( acceptsock );
2188 req->accepted = 0;
2189 req->recv_len = 0;
2190 req->local_len = 0;
2191 if (params)
2193 req->recv_len = params->recv_len;
2194 req->local_len = params->local_len;
2197 return req;
2200 static void sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2202 struct sock *sock = get_fd_user( fd );
2203 int unix_fd = -1;
2205 assert( sock->obj.ops == &sock_ops );
2207 if (code != IOCTL_AFD_WINE_CREATE && code != IOCTL_AFD_POLL && (unix_fd = get_unix_fd( fd )) < 0)
2208 return;
2210 switch(code)
2212 case IOCTL_AFD_WINE_CREATE:
2214 const struct afd_create_params *params = get_req_data();
2216 if (get_req_data_size() != sizeof(*params))
2218 set_error( STATUS_INVALID_PARAMETER );
2219 return;
2221 init_socket( sock, params->family, params->type, params->protocol );
2222 return;
2225 case IOCTL_AFD_WINE_ACCEPT:
2227 struct sock *acceptsock;
2228 obj_handle_t handle;
2230 if (get_reply_max_size() != sizeof(handle))
2232 set_error( STATUS_BUFFER_TOO_SMALL );
2233 return;
2236 if (!(acceptsock = accept_socket( sock )))
2238 struct accept_req *req;
2240 if (sock->nonblocking) return;
2241 if (get_error() != STATUS_DEVICE_NOT_READY) return;
2243 if (!(req = alloc_accept_req( sock, NULL, async, NULL ))) return;
2244 list_add_tail( &sock->accept_list, &req->entry );
2246 async_set_completion_callback( async, free_accept_req, req );
2247 queue_async( &sock->accept_q, async );
2248 sock_reselect( sock );
2249 set_error( STATUS_PENDING );
2250 return;
2252 handle = alloc_handle( current->process, &acceptsock->obj,
2253 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
2254 acceptsock->wparam = handle;
2255 sock_reselect( acceptsock );
2256 release_object( acceptsock );
2257 set_reply_data( &handle, sizeof(handle) );
2258 return;
2261 case IOCTL_AFD_WINE_ACCEPT_INTO:
2263 static const int access = FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | FILE_READ_DATA;
2264 const struct afd_accept_into_params *params = get_req_data();
2265 struct sock *acceptsock;
2266 unsigned int remote_len;
2267 struct accept_req *req;
2269 if (get_req_data_size() != sizeof(*params) ||
2270 get_reply_max_size() < params->recv_len ||
2271 get_reply_max_size() - params->recv_len < params->local_len)
2273 set_error( STATUS_BUFFER_TOO_SMALL );
2274 return;
2277 remote_len = get_reply_max_size() - params->recv_len - params->local_len;
2278 if (remote_len < sizeof(int))
2280 set_error( STATUS_INVALID_PARAMETER );
2281 return;
2284 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, params->accept_handle, access, &sock_ops )))
2285 return;
2287 if (acceptsock->accept_recv_req)
2289 release_object( acceptsock );
2290 set_error( STATUS_INVALID_PARAMETER );
2291 return;
2294 if (!(req = alloc_accept_req( sock, acceptsock, async, params )))
2296 release_object( acceptsock );
2297 return;
2299 list_add_tail( &sock->accept_list, &req->entry );
2300 acceptsock->accept_recv_req = req;
2301 release_object( acceptsock );
2303 acceptsock->wparam = params->accept_handle;
2304 async_set_completion_callback( async, free_accept_req, req );
2305 queue_async( &sock->accept_q, async );
2306 sock_reselect( sock );
2307 set_error( STATUS_PENDING );
2308 return;
2311 case IOCTL_AFD_LISTEN:
2313 const struct afd_listen_params *params = get_req_data();
2315 if (get_req_data_size() < sizeof(*params))
2317 set_error( STATUS_INVALID_PARAMETER );
2318 return;
2321 if (!sock->bound)
2323 set_error( STATUS_INVALID_PARAMETER );
2324 return;
2327 if (listen( unix_fd, params->backlog ) < 0)
2329 set_error( sock_get_ntstatus( errno ) );
2330 return;
2333 sock->state = SOCK_LISTENING;
2335 /* a listening socket can no longer be accepted into */
2336 allow_fd_caching( sock->fd );
2338 /* we may already be selecting for AFD_POLL_ACCEPT */
2339 sock_reselect( sock );
2340 return;
2343 case IOCTL_AFD_WINE_CONNECT:
2345 const struct afd_connect_params *params = get_req_data();
2346 const struct WS_sockaddr *addr;
2347 union unix_sockaddr unix_addr;
2348 struct connect_req *req;
2349 socklen_t unix_len;
2350 int send_len, ret;
2352 if (get_req_data_size() < sizeof(*params) ||
2353 get_req_data_size() - sizeof(*params) < params->addr_len)
2355 set_error( STATUS_BUFFER_TOO_SMALL );
2356 return;
2358 send_len = get_req_data_size() - sizeof(*params) - params->addr_len;
2359 addr = (const struct WS_sockaddr *)(params + 1);
2361 if (!params->synchronous && !sock->bound)
2363 set_error( STATUS_INVALID_PARAMETER );
2364 return;
2367 if (sock->accept_recv_req)
2369 set_error( STATUS_INVALID_PARAMETER );
2370 return;
2373 if (sock->connect_req)
2375 set_error( STATUS_INVALID_PARAMETER );
2376 return;
2379 switch (sock->state)
2381 case SOCK_LISTENING:
2382 set_error( STATUS_INVALID_PARAMETER );
2383 return;
2385 case SOCK_CONNECTING:
2386 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2387 * but there's no status code that maps to WSAEALREADY... */
2388 set_error( params->synchronous ? STATUS_ADDRESS_ALREADY_ASSOCIATED : STATUS_INVALID_PARAMETER );
2389 return;
2391 case SOCK_CONNECTED:
2392 set_error( STATUS_CONNECTION_ACTIVE );
2393 return;
2395 case SOCK_UNCONNECTED:
2396 case SOCK_CONNECTIONLESS:
2397 break;
2400 unix_len = sockaddr_to_unix( addr, params->addr_len, &unix_addr );
2401 if (!unix_len)
2403 set_error( STATUS_INVALID_ADDRESS );
2404 return;
2406 if (unix_addr.addr.sa_family == AF_INET && !memcmp( &unix_addr.in.sin_addr, magic_loopback_addr, 4 ))
2407 unix_addr.in.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
2409 ret = connect( unix_fd, &unix_addr.addr, unix_len );
2410 if (ret < 0 && errno != EINPROGRESS)
2412 set_error( sock_get_ntstatus( errno ) );
2413 return;
2416 /* a connected or connecting socket can no longer be accepted into */
2417 allow_fd_caching( sock->fd );
2419 unix_len = sizeof(unix_addr);
2420 if (!getsockname( unix_fd, &unix_addr.addr, &unix_len ))
2421 sock->addr_len = sockaddr_from_unix( &unix_addr, &sock->addr.addr, sizeof(sock->addr) );
2422 sock->bound = 1;
2424 if (!ret)
2426 if (sock->type != WS_SOCK_DGRAM)
2428 sock->state = SOCK_CONNECTED;
2429 sock->connect_time = current_time;
2432 if (!send_len) return;
2435 if (sock->type != WS_SOCK_DGRAM)
2436 sock->state = SOCK_CONNECTING;
2438 if (params->synchronous && sock->nonblocking)
2440 sock_reselect( sock );
2441 set_error( STATUS_DEVICE_NOT_READY );
2442 return;
2445 if (!(req = mem_alloc( sizeof(*req) )))
2446 return;
2448 req->async = (struct async *)grab_object( async );
2449 req->iosb = async_get_iosb( async );
2450 req->sock = (struct sock *)grab_object( sock );
2451 req->addr_len = params->addr_len;
2452 req->send_len = send_len;
2453 req->send_cursor = 0;
2455 async_set_completion_callback( async, free_connect_req, req );
2456 sock->connect_req = req;
2457 queue_async( &sock->connect_q, async );
2458 sock_reselect( sock );
2459 set_error( STATUS_PENDING );
2460 return;
2463 case IOCTL_AFD_WINE_SHUTDOWN:
2465 unsigned int how;
2467 if (get_req_data_size() < sizeof(int))
2469 set_error( STATUS_BUFFER_TOO_SMALL );
2470 return;
2472 how = *(int *)get_req_data();
2474 if (how > SD_BOTH)
2476 set_error( STATUS_INVALID_PARAMETER );
2477 return;
2480 if (sock->state != SOCK_CONNECTED && sock->state != SOCK_CONNECTIONLESS)
2482 set_error( STATUS_INVALID_CONNECTION );
2483 return;
2486 if (how != SD_SEND)
2488 sock->rd_shutdown = 1;
2490 if (how != SD_RECEIVE)
2492 sock->wr_shutdown = 1;
2493 if (list_empty( &sock->write_q.queue ))
2494 shutdown( unix_fd, SHUT_WR );
2495 else
2496 sock->wr_shutdown_pending = 1;
2499 if (how == SD_BOTH)
2501 if (sock->event) release_object( sock->event );
2502 sock->event = NULL;
2503 sock->window = 0;
2504 sock->mask = 0;
2505 sock->nonblocking = 1;
2508 sock_reselect( sock );
2509 return;
2512 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE:
2514 int force_async;
2516 if (get_req_data_size() < sizeof(int))
2518 set_error( STATUS_BUFFER_TOO_SMALL );
2519 return;
2521 force_async = *(int *)get_req_data();
2523 if (sock->nonblocking && !force_async)
2525 set_error( STATUS_DEVICE_NOT_READY );
2526 return;
2528 if (!sock_get_ifchange( sock )) return;
2529 queue_async( &sock->ifchange_q, async );
2530 set_error( STATUS_PENDING );
2531 return;
2534 case IOCTL_AFD_WINE_FIONBIO:
2535 if (get_req_data_size() < sizeof(int))
2537 set_error( STATUS_BUFFER_TOO_SMALL );
2538 return;
2540 if (*(int *)get_req_data())
2542 sock->nonblocking = 1;
2544 else
2546 if (sock->mask)
2548 set_error( STATUS_INVALID_PARAMETER );
2549 return;
2551 sock->nonblocking = 0;
2553 return;
2555 case IOCTL_AFD_GET_EVENTS:
2557 struct afd_get_events_params params = {0};
2558 unsigned int i;
2560 if (get_reply_max_size() < sizeof(params))
2562 set_error( STATUS_INVALID_PARAMETER );
2563 return;
2566 params.flags = sock->pending_events & sock->mask;
2567 for (i = 0; i < ARRAY_SIZE( params.status ); ++i)
2568 params.status[i] = sock_get_ntstatus( sock->errors[i] );
2570 sock->pending_events &= ~sock->mask;
2571 sock_reselect( sock );
2573 set_reply_data( &params, sizeof(params) );
2574 return;
2577 case IOCTL_AFD_EVENT_SELECT:
2579 struct event *event = NULL;
2580 obj_handle_t event_handle;
2581 int mask;
2583 set_async_pending( async );
2585 if (is_machine_64bit( current->process->machine ))
2587 const struct afd_event_select_params_64 *params = get_req_data();
2589 if (get_req_data_size() < sizeof(*params))
2591 set_error( STATUS_INVALID_PARAMETER );
2592 return;
2595 event_handle = params->event;
2596 mask = params->mask;
2598 else
2600 const struct afd_event_select_params_32 *params = get_req_data();
2602 if (get_req_data_size() < sizeof(*params))
2604 set_error( STATUS_INVALID_PARAMETER );
2605 return;
2608 event_handle = params->event;
2609 mask = params->mask;
2612 if ((event_handle || mask) &&
2613 !(event = get_event_obj( current->process, event_handle, EVENT_MODIFY_STATE )))
2615 set_error( STATUS_INVALID_PARAMETER );
2616 return;
2619 if (sock->event) release_object( sock->event );
2620 sock->event = event;
2621 sock->mask = mask;
2622 sock->window = 0;
2623 sock->message = 0;
2624 sock->wparam = 0;
2625 sock->nonblocking = 1;
2627 sock_reselect( sock );
2628 /* Explicitly wake the socket up if the mask causes it to become
2629 * signaled. Note that reselecting isn't enough, since we might already
2630 * have had events recorded in sock->reported_events and we don't want
2631 * to select for them again. */
2632 sock_wake_up( sock );
2634 return;
2637 case IOCTL_AFD_WINE_MESSAGE_SELECT:
2639 const struct afd_message_select_params *params = get_req_data();
2641 if (get_req_data_size() < sizeof(params))
2643 set_error( STATUS_BUFFER_TOO_SMALL );
2644 return;
2647 if (sock->event) release_object( sock->event );
2649 if (params->window)
2651 sock->pending_events = 0;
2652 sock->reported_events = 0;
2654 sock->event = NULL;
2655 sock->mask = params->mask;
2656 sock->window = params->window;
2657 sock->message = params->message;
2658 sock->wparam = params->handle;
2659 sock->nonblocking = 1;
2661 sock_reselect( sock );
2663 return;
2666 case IOCTL_AFD_BIND:
2668 const struct afd_bind_params *params = get_req_data();
2669 union unix_sockaddr unix_addr, bind_addr;
2670 data_size_t in_size;
2671 socklen_t unix_len;
2673 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2674 * input */
2675 if (get_req_data_size() < get_reply_max_size())
2677 set_error( STATUS_BUFFER_TOO_SMALL );
2678 return;
2680 in_size = get_req_data_size() - get_reply_max_size();
2681 if (in_size < offsetof(struct afd_bind_params, addr.sa_data)
2682 || get_reply_max_size() < in_size - sizeof(int))
2684 set_error( STATUS_INVALID_PARAMETER );
2685 return;
2688 if (sock->bound)
2690 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED );
2691 return;
2694 unix_len = sockaddr_to_unix( &params->addr, in_size - sizeof(int), &unix_addr );
2695 if (!unix_len)
2697 set_error( STATUS_INVALID_ADDRESS );
2698 return;
2700 bind_addr = unix_addr;
2702 if (unix_addr.addr.sa_family == AF_INET)
2704 if (!memcmp( &unix_addr.in.sin_addr, magic_loopback_addr, 4 )
2705 || bind_to_interface( sock, &unix_addr.in ))
2706 bind_addr.in.sin_addr.s_addr = htonl( INADDR_ANY );
2708 else if (unix_addr.addr.sa_family == AF_INET6)
2710 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2711 /* Windows allows specifying zero to use the default scope. Linux
2712 * interprets it as an interface index and requires that it be
2713 * nonzero. */
2714 if (!unix_addr.in6.sin6_scope_id)
2715 bind_addr.in6.sin6_scope_id = get_ipv6_interface_index( &unix_addr.in6.sin6_addr );
2716 #endif
2719 set_async_pending( async );
2721 if (bind( unix_fd, &bind_addr.addr, unix_len ) < 0)
2723 if (errno == EADDRINUSE)
2725 int reuse;
2726 socklen_t len = sizeof(reuse);
2728 if (!getsockopt( unix_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, &len ) && reuse)
2729 errno = EACCES;
2732 set_error( sock_get_ntstatus( errno ) );
2733 return;
2736 sock->bound = 1;
2738 unix_len = sizeof(bind_addr);
2739 if (!getsockname( unix_fd, &bind_addr.addr, &unix_len ))
2741 /* store the interface or magic loopback address instead of the
2742 * actual unix address */
2743 if (bind_addr.addr.sa_family == AF_INET)
2744 bind_addr.in.sin_addr = unix_addr.in.sin_addr;
2745 sock->addr_len = sockaddr_from_unix( &bind_addr, &sock->addr.addr, sizeof(sock->addr) );
2748 if (get_reply_max_size() >= sock->addr_len)
2749 set_reply_data( &sock->addr, sock->addr_len );
2750 return;
2753 case IOCTL_AFD_GETSOCKNAME:
2754 if (!sock->bound)
2756 set_error( STATUS_INVALID_PARAMETER );
2757 return;
2760 if (get_reply_max_size() < sock->addr_len)
2762 set_error( STATUS_BUFFER_TOO_SMALL );
2763 return;
2766 set_reply_data( &sock->addr, sock->addr_len );
2767 return;
2769 case IOCTL_AFD_WINE_DEFER:
2771 const obj_handle_t *handle = get_req_data();
2772 struct sock *acceptsock;
2774 if (get_req_data_size() < sizeof(*handle))
2776 set_error( STATUS_BUFFER_TOO_SMALL );
2777 return;
2780 acceptsock = (struct sock *)get_handle_obj( current->process, *handle, 0, &sock_ops );
2781 if (!acceptsock) return;
2783 sock->deferred = acceptsock;
2784 return;
2787 case IOCTL_AFD_WINE_GET_INFO:
2789 struct afd_get_info_params params;
2791 if (get_reply_max_size() < sizeof(params))
2793 set_error( STATUS_BUFFER_TOO_SMALL );
2794 return;
2797 params.family = sock->family;
2798 params.type = sock->type;
2799 params.protocol = sock->proto;
2800 set_reply_data( &params, sizeof(params) );
2801 return;
2804 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN:
2806 int listening = (sock->state == SOCK_LISTENING);
2808 if (get_reply_max_size() < sizeof(listening))
2810 set_error( STATUS_BUFFER_TOO_SMALL );
2811 return;
2814 set_reply_data( &listening, sizeof(listening) );
2815 return;
2818 case IOCTL_AFD_WINE_GET_SO_ERROR:
2820 int error;
2821 unsigned int i;
2823 if (get_reply_max_size() < sizeof(error))
2825 set_error( STATUS_BUFFER_TOO_SMALL );
2826 return;
2829 error = sock_error( sock );
2830 if (!error)
2832 for (i = 0; i < ARRAY_SIZE( sock->errors ); ++i)
2834 if (sock->errors[i])
2836 error = sock->errors[i];
2837 break;
2842 error = sock_get_error( error );
2843 set_reply_data( &error, sizeof(error) );
2844 return;
2847 case IOCTL_AFD_WINE_GET_SO_RCVBUF:
2849 int rcvbuf = sock->rcvbuf;
2851 if (get_reply_max_size() < sizeof(rcvbuf))
2853 set_error( STATUS_BUFFER_TOO_SMALL );
2854 return;
2857 set_reply_data( &rcvbuf, sizeof(rcvbuf) );
2858 return;
2861 case IOCTL_AFD_WINE_SET_SO_RCVBUF:
2863 DWORD rcvbuf;
2865 if (get_req_data_size() < sizeof(rcvbuf))
2867 set_error( STATUS_BUFFER_TOO_SMALL );
2868 return;
2870 rcvbuf = *(DWORD *)get_req_data();
2872 if (!setsockopt( unix_fd, SOL_SOCKET, SO_RCVBUF, (char *)&rcvbuf, sizeof(rcvbuf) ))
2873 sock->rcvbuf = rcvbuf;
2874 else
2875 set_error( sock_get_ntstatus( errno ) );
2876 return;
2879 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO:
2881 DWORD rcvtimeo = sock->rcvtimeo;
2883 if (get_reply_max_size() < sizeof(rcvtimeo))
2885 set_error( STATUS_BUFFER_TOO_SMALL );
2886 return;
2889 set_reply_data( &rcvtimeo, sizeof(rcvtimeo) );
2890 return;
2893 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO:
2895 DWORD rcvtimeo;
2897 if (get_req_data_size() < sizeof(rcvtimeo))
2899 set_error( STATUS_BUFFER_TOO_SMALL );
2900 return;
2902 rcvtimeo = *(DWORD *)get_req_data();
2904 sock->rcvtimeo = rcvtimeo;
2905 return;
2908 case IOCTL_AFD_WINE_GET_SO_SNDBUF:
2910 int sndbuf = sock->sndbuf;
2912 if (get_reply_max_size() < sizeof(sndbuf))
2914 set_error( STATUS_BUFFER_TOO_SMALL );
2915 return;
2918 set_reply_data( &sndbuf, sizeof(sndbuf) );
2919 return;
2922 case IOCTL_AFD_WINE_SET_SO_SNDBUF:
2924 DWORD sndbuf;
2926 if (get_req_data_size() < sizeof(sndbuf))
2928 set_error( STATUS_BUFFER_TOO_SMALL );
2929 return;
2931 sndbuf = *(DWORD *)get_req_data();
2933 #ifdef __APPLE__
2934 if (!sndbuf)
2936 /* setsockopt fails if a zero value is passed */
2937 sock->sndbuf = sndbuf;
2938 return;
2940 #endif
2942 if (!setsockopt( unix_fd, SOL_SOCKET, SO_SNDBUF, (char *)&sndbuf, sizeof(sndbuf) ))
2943 sock->sndbuf = sndbuf;
2944 else
2945 set_error( sock_get_ntstatus( errno ) );
2946 return;
2949 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO:
2951 DWORD sndtimeo = sock->sndtimeo;
2953 if (get_reply_max_size() < sizeof(sndtimeo))
2955 set_error( STATUS_BUFFER_TOO_SMALL );
2956 return;
2959 set_reply_data( &sndtimeo, sizeof(sndtimeo) );
2960 return;
2963 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO:
2965 DWORD sndtimeo;
2967 if (get_req_data_size() < sizeof(sndtimeo))
2969 set_error( STATUS_BUFFER_TOO_SMALL );
2970 return;
2972 sndtimeo = *(DWORD *)get_req_data();
2974 sock->sndtimeo = sndtimeo;
2975 return;
2978 case IOCTL_AFD_WINE_GET_SO_CONNECT_TIME:
2980 DWORD time = ~0u;
2982 if (get_reply_max_size() < sizeof(time))
2984 set_error( STATUS_BUFFER_TOO_SMALL );
2985 return;
2988 if (sock->state == SOCK_CONNECTED)
2989 time = (current_time - sock->connect_time) / 10000000;
2991 set_reply_data( &time, sizeof(time) );
2992 return;
2995 case IOCTL_AFD_POLL:
2997 if (get_reply_max_size() < get_req_data_size())
2999 set_error( STATUS_INVALID_PARAMETER );
3000 return;
3003 if (is_machine_64bit( current->process->machine ))
3005 const struct afd_poll_params_64 *params = get_req_data();
3007 if (get_req_data_size() < sizeof(struct afd_poll_params_64) ||
3008 get_req_data_size() < offsetof( struct afd_poll_params_64, sockets[params->count] ))
3010 set_error( STATUS_INVALID_PARAMETER );
3011 return;
3014 poll_socket( sock, async, params->exclusive, params->timeout, params->count, params->sockets );
3016 else
3018 const struct afd_poll_params_32 *params = get_req_data();
3019 struct afd_poll_socket_64 *sockets;
3020 unsigned int i;
3022 if (get_req_data_size() < sizeof(struct afd_poll_params_32) ||
3023 get_req_data_size() < offsetof( struct afd_poll_params_32, sockets[params->count] ))
3025 set_error( STATUS_INVALID_PARAMETER );
3026 return;
3029 if (!(sockets = mem_alloc( params->count * sizeof(*sockets) ))) return;
3030 for (i = 0; i < params->count; ++i)
3032 sockets[i].socket = params->sockets[i].socket;
3033 sockets[i].flags = params->sockets[i].flags;
3034 sockets[i].status = params->sockets[i].status;
3037 poll_socket( sock, async, params->exclusive, params->timeout, params->count, sockets );
3038 free( sockets );
3041 return;
3044 default:
3045 set_error( STATUS_NOT_SUPPORTED );
3046 return;
3050 static void handle_exclusive_poll(struct poll_req *req)
3052 unsigned int i;
3054 for (i = 0; i < req->count; ++i)
3056 struct sock *sock = req->sockets[i].sock;
3057 struct poll_req *main_poll = sock->main_poll;
3059 if (main_poll && main_poll->exclusive && req->exclusive)
3061 complete_async_poll( main_poll, STATUS_SUCCESS );
3062 main_poll = NULL;
3065 if (!main_poll)
3066 sock->main_poll = req;
3070 static void poll_socket( struct sock *poll_sock, struct async *async, int exclusive, timeout_t timeout,
3071 unsigned int count, const struct afd_poll_socket_64 *sockets )
3073 BOOL signaled = FALSE;
3074 struct poll_req *req;
3075 unsigned int i, j;
3077 if (!count)
3079 set_error( STATUS_INVALID_PARAMETER );
3080 return;
3083 if (!(req = mem_alloc( offsetof( struct poll_req, sockets[count] ) )))
3084 return;
3086 req->timeout = NULL;
3087 req->pending = 0;
3088 if (timeout && timeout != TIMEOUT_INFINITE &&
3089 !(req->timeout = add_timeout_user( timeout, async_poll_timeout, req )))
3091 free( req );
3092 return;
3094 req->orig_timeout = timeout;
3096 for (i = 0; i < count; ++i)
3098 req->sockets[i].sock = (struct sock *)get_handle_obj( current->process, sockets[i].socket, 0, &sock_ops );
3099 if (!req->sockets[i].sock)
3101 for (j = 0; j < i; ++j) release_object( req->sockets[j].sock );
3102 if (req->timeout) remove_timeout_user( req->timeout );
3103 free( req );
3104 return;
3106 req->sockets[i].handle = sockets[i].socket;
3107 req->sockets[i].mask = sockets[i].flags;
3108 req->sockets[i].flags = 0;
3111 req->exclusive = exclusive;
3112 req->count = count;
3113 req->async = (struct async *)grab_object( async );
3114 req->iosb = async_get_iosb( async );
3116 handle_exclusive_poll(req);
3118 list_add_tail( &poll_list, &req->entry );
3119 async_set_completion_callback( async, free_poll_req, req );
3120 queue_async( &poll_sock->poll_q, async );
3122 for (i = 0; i < count; ++i)
3124 struct sock *sock = req->sockets[i].sock;
3125 int mask = req->sockets[i].mask;
3126 struct pollfd pollfd;
3128 pollfd.fd = get_unix_fd( sock->fd );
3129 pollfd.events = poll_flags_from_afd( sock, mask );
3130 if (pollfd.events >= 0 && poll( &pollfd, 1, 0 ) >= 0)
3131 sock_poll_event( sock->fd, pollfd.revents );
3133 /* FIXME: do other error conditions deserve a similar treatment? */
3134 if (sock->state != SOCK_CONNECTING && sock->errors[AFD_POLL_BIT_CONNECT_ERR] && (mask & AFD_POLL_CONNECT_ERR))
3136 req->sockets[i].flags |= AFD_POLL_CONNECT_ERR;
3137 req->sockets[i].status = sock_get_ntstatus( sock->errors[AFD_POLL_BIT_CONNECT_ERR] );
3140 if (req->sockets[i].flags)
3141 signaled = TRUE;
3144 if (!timeout || signaled)
3145 complete_async_poll( req, STATUS_SUCCESS );
3146 else
3147 req->pending = 1;
3149 for (i = 0; i < req->count; ++i)
3150 sock_reselect( req->sockets[i].sock );
3151 set_error( STATUS_PENDING );
3154 #ifdef HAVE_LINUX_RTNETLINK_H
3156 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
3157 static struct object *ifchange_object;
3159 static void ifchange_dump( struct object *obj, int verbose );
3160 static struct fd *ifchange_get_fd( struct object *obj );
3161 static void ifchange_destroy( struct object *obj );
3163 static int ifchange_get_poll_events( struct fd *fd );
3164 static void ifchange_poll_event( struct fd *fd, int event );
3166 struct ifchange
3168 struct object obj; /* object header */
3169 struct fd *fd; /* interface change file descriptor */
3170 struct list sockets; /* list of sockets to send interface change notifications */
3173 static const struct object_ops ifchange_ops =
3175 sizeof(struct ifchange), /* size */
3176 &no_type, /* type */
3177 ifchange_dump, /* dump */
3178 no_add_queue, /* add_queue */
3179 NULL, /* remove_queue */
3180 NULL, /* signaled */
3181 no_satisfied, /* satisfied */
3182 no_signal, /* signal */
3183 ifchange_get_fd, /* get_fd */
3184 default_map_access, /* map_access */
3185 default_get_sd, /* get_sd */
3186 default_set_sd, /* set_sd */
3187 no_get_full_name, /* get_full_name */
3188 no_lookup_name, /* lookup_name */
3189 no_link_name, /* link_name */
3190 NULL, /* unlink_name */
3191 no_open_file, /* open_file */
3192 no_kernel_obj_list, /* get_kernel_obj_list */
3193 no_close_handle, /* close_handle */
3194 ifchange_destroy /* destroy */
3197 static const struct fd_ops ifchange_fd_ops =
3199 ifchange_get_poll_events, /* get_poll_events */
3200 ifchange_poll_event, /* poll_event */
3201 NULL, /* get_fd_type */
3202 no_fd_read, /* read */
3203 no_fd_write, /* write */
3204 no_fd_flush, /* flush */
3205 no_fd_get_file_info, /* get_file_info */
3206 no_fd_get_volume_info, /* get_volume_info */
3207 no_fd_ioctl, /* ioctl */
3208 NULL, /* cancel_async */
3209 NULL, /* queue_async */
3210 NULL /* reselect_async */
3213 static void ifchange_dump( struct object *obj, int verbose )
3215 assert( obj->ops == &ifchange_ops );
3216 fprintf( stderr, "Interface change\n" );
3219 static struct fd *ifchange_get_fd( struct object *obj )
3221 struct ifchange *ifchange = (struct ifchange *)obj;
3222 return (struct fd *)grab_object( ifchange->fd );
3225 static void ifchange_destroy( struct object *obj )
3227 struct ifchange *ifchange = (struct ifchange *)obj;
3228 assert( obj->ops == &ifchange_ops );
3230 release_object( ifchange->fd );
3232 /* reset the global ifchange object so that it will be recreated if it is needed again */
3233 assert( obj == ifchange_object );
3234 ifchange_object = NULL;
3237 static int ifchange_get_poll_events( struct fd *fd )
3239 return POLLIN;
3242 /* wake up all the sockets waiting for a change notification event */
3243 static void ifchange_wake_up( struct object *obj, unsigned int status )
3245 struct ifchange *ifchange = (struct ifchange *)obj;
3246 struct list *ptr, *next;
3247 assert( obj->ops == &ifchange_ops );
3248 assert( obj == ifchange_object );
3250 LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
3252 struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
3254 assert( sock->ifchange_obj );
3255 async_wake_up( &sock->ifchange_q, status ); /* issue ifchange notification for the socket */
3256 sock_release_ifchange( sock ); /* remove socket from list and decrement ifchange refcount */
3260 static void ifchange_poll_event( struct fd *fd, int event )
3262 struct object *ifchange = get_fd_user( fd );
3263 unsigned int status = STATUS_PENDING;
3264 char buffer[PIPE_BUF];
3265 int r;
3267 r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
3268 if (r < 0)
3270 if (errno == EWOULDBLOCK || (EWOULDBLOCK != EAGAIN && errno == EAGAIN))
3271 return; /* retry when poll() says the socket is ready */
3272 status = sock_get_ntstatus( errno );
3274 else if (r > 0)
3276 struct nlmsghdr *nlh;
3278 for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
3280 if (nlh->nlmsg_type == NLMSG_DONE)
3281 break;
3282 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
3283 status = STATUS_SUCCESS;
3286 else status = STATUS_CANCELLED;
3288 if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
3291 #endif
3293 /* we only need one of these interface notification objects, all of the sockets dependent upon
3294 * it will wake up when a notification event occurs */
3295 static struct object *get_ifchange( void )
3297 #ifdef HAVE_LINUX_RTNETLINK_H
3298 struct ifchange *ifchange;
3299 struct sockaddr_nl addr;
3300 int unix_fd;
3302 if (ifchange_object)
3304 /* increment the refcount for each socket that uses the ifchange object */
3305 return grab_object( ifchange_object );
3308 /* create the socket we need for processing interface change notifications */
3309 unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
3310 if (unix_fd == -1)
3312 set_error( sock_get_ntstatus( errno ));
3313 return NULL;
3315 fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
3316 memset( &addr, 0, sizeof(addr) );
3317 addr.nl_family = AF_NETLINK;
3318 addr.nl_groups = RTMGRP_IPV4_IFADDR;
3319 /* bind the socket to the special netlink kernel interface */
3320 if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
3322 close( unix_fd );
3323 set_error( sock_get_ntstatus( errno ));
3324 return NULL;
3326 if (!(ifchange = alloc_object( &ifchange_ops )))
3328 close( unix_fd );
3329 set_error( STATUS_NO_MEMORY );
3330 return NULL;
3332 list_init( &ifchange->sockets );
3333 if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
3335 release_object( ifchange );
3336 set_error( STATUS_NO_MEMORY );
3337 return NULL;
3339 set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
3341 /* the ifchange object is now successfully configured */
3342 ifchange_object = &ifchange->obj;
3343 return &ifchange->obj;
3344 #else
3345 set_error( STATUS_NOT_SUPPORTED );
3346 return NULL;
3347 #endif
3350 /* add the socket to the interface change notification list */
3351 static void ifchange_add_sock( struct object *obj, struct sock *sock )
3353 #ifdef HAVE_LINUX_RTNETLINK_H
3354 struct ifchange *ifchange = (struct ifchange *)obj;
3356 list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
3357 #endif
3360 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3361 static struct object *sock_get_ifchange( struct sock *sock )
3363 struct object *ifchange;
3365 if (sock->ifchange_obj) /* reuse existing ifchange_obj for this socket */
3366 return sock->ifchange_obj;
3368 if (!(ifchange = get_ifchange()))
3369 return NULL;
3371 /* add the socket to the ifchange notification list */
3372 ifchange_add_sock( ifchange, sock );
3373 sock->ifchange_obj = ifchange;
3374 return ifchange;
3377 /* destroy an existing ifchange queue for a specific socket */
3378 static void sock_release_ifchange( struct sock *sock )
3380 if (sock->ifchange_obj)
3382 list_remove( &sock->ifchange_entry );
3383 release_object( sock->ifchange_obj );
3384 sock->ifchange_obj = NULL;
3388 static void socket_device_dump( struct object *obj, int verbose );
3389 static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
3390 unsigned int attr, struct object *root );
3391 static struct object *socket_device_open_file( struct object *obj, unsigned int access,
3392 unsigned int sharing, unsigned int options );
3394 static const struct object_ops socket_device_ops =
3396 sizeof(struct object), /* size */
3397 &device_type, /* type */
3398 socket_device_dump, /* dump */
3399 no_add_queue, /* add_queue */
3400 NULL, /* remove_queue */
3401 NULL, /* signaled */
3402 no_satisfied, /* satisfied */
3403 no_signal, /* signal */
3404 no_get_fd, /* get_fd */
3405 default_map_access, /* map_access */
3406 default_get_sd, /* get_sd */
3407 default_set_sd, /* set_sd */
3408 default_get_full_name, /* get_full_name */
3409 socket_device_lookup_name, /* lookup_name */
3410 directory_link_name, /* link_name */
3411 default_unlink_name, /* unlink_name */
3412 socket_device_open_file, /* open_file */
3413 no_kernel_obj_list, /* get_kernel_obj_list */
3414 no_close_handle, /* close_handle */
3415 no_destroy /* destroy */
3418 static void socket_device_dump( struct object *obj, int verbose )
3420 fputs( "Socket device\n", stderr );
3423 static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
3424 unsigned int attr, struct object *root )
3426 if (name) name->len = 0;
3427 return NULL;
3430 static struct object *socket_device_open_file( struct object *obj, unsigned int access,
3431 unsigned int sharing, unsigned int options )
3433 struct sock *sock;
3435 if (!(sock = create_socket())) return NULL;
3436 if (!(sock->fd = alloc_pseudo_fd( &sock_fd_ops, &sock->obj, options )))
3438 release_object( sock );
3439 return NULL;
3441 return &sock->obj;
3444 struct object *create_socket_device( struct object *root, const struct unicode_str *name,
3445 unsigned int attr, const struct security_descriptor *sd )
3447 return create_named_object( root, &socket_device_ops, name, attr, sd );
3450 DECL_HANDLER(recv_socket)
3452 struct sock *sock = (struct sock *)get_handle_obj( current->process, req->async.handle, 0, &sock_ops );
3453 unsigned int status = STATUS_PENDING;
3454 timeout_t timeout = 0;
3455 struct async *async;
3456 struct fd *fd;
3458 if (!sock) return;
3459 fd = sock->fd;
3461 if (!req->force_async && !sock->nonblocking && is_fd_overlapped( fd ))
3462 timeout = (timeout_t)sock->rcvtimeo * -10000;
3464 if (sock->rd_shutdown) status = STATUS_PIPE_DISCONNECTED;
3465 else if (!async_queued( &sock->read_q ))
3467 /* If read_q is not empty, we cannot really tell if the already queued
3468 * asyncs will not consume all available data; if there's no data
3469 * available, the current request won't be immediately satiable.
3471 struct pollfd pollfd;
3472 pollfd.fd = get_unix_fd( sock->fd );
3473 pollfd.events = req->oob ? POLLPRI : POLLIN;
3474 pollfd.revents = 0;
3475 if (poll(&pollfd, 1, 0) >= 0 && pollfd.revents)
3477 /* Give the client opportunity to complete synchronously.
3478 * If it turns out that the I/O request is not actually immediately satiable,
3479 * the client may then choose to re-queue the async (with STATUS_PENDING). */
3480 status = STATUS_ALERTED;
3484 if (status == STATUS_PENDING && !req->force_async && sock->nonblocking)
3485 status = STATUS_DEVICE_NOT_READY;
3487 sock->pending_events &= ~(req->oob ? AFD_POLL_OOB : AFD_POLL_READ);
3488 sock->reported_events &= ~(req->oob ? AFD_POLL_OOB : AFD_POLL_READ);
3490 if ((async = create_request_async( fd, get_fd_comp_flags( fd ), &req->async )))
3492 set_error( status );
3494 if (timeout)
3495 async_set_timeout( async, timeout, STATUS_IO_TIMEOUT );
3497 if (status == STATUS_PENDING || status == STATUS_ALERTED)
3498 queue_async( &sock->read_q, async );
3500 /* always reselect; we changed reported_events above */
3501 sock_reselect( sock );
3503 reply->wait = async_handoff( async, NULL, 0 );
3504 reply->options = get_fd_options( fd );
3505 reply->nonblocking = sock->nonblocking;
3506 release_object( async );
3508 release_object( sock );
3511 static void send_socket_completion_callback( void *private )
3513 struct send_req *send_req = private;
3514 struct iosb *iosb = send_req->iosb;
3515 struct sock *sock = send_req->sock;
3517 if (iosb->status != STATUS_SUCCESS)
3519 /* send() calls only clear and reselect events if unsuccessful. */
3520 sock->pending_events &= ~AFD_POLL_WRITE;
3521 sock->reported_events &= ~AFD_POLL_WRITE;
3522 sock_reselect( sock );
3525 release_object( iosb );
3526 release_object( sock );
3527 free( send_req );
3530 DECL_HANDLER(send_socket)
3532 struct sock *sock = (struct sock *)get_handle_obj( current->process, req->async.handle, 0, &sock_ops );
3533 unsigned int status = STATUS_PENDING;
3534 timeout_t timeout = 0;
3535 struct async *async;
3536 struct fd *fd;
3537 int bind_errno = 0;
3539 if (!sock) return;
3540 fd = sock->fd;
3542 if (sock->type == WS_SOCK_DGRAM && !sock->bound)
3544 union unix_sockaddr unix_addr;
3545 socklen_t unix_len;
3546 int unix_fd = get_unix_fd( fd );
3548 unix_len = get_unix_sockaddr_any( &unix_addr, sock->family );
3549 if (bind( unix_fd, &unix_addr.addr, unix_len ) < 0)
3550 bind_errno = errno;
3552 if (getsockname( unix_fd, &unix_addr.addr, &unix_len ) >= 0)
3554 sock->addr_len = sockaddr_from_unix( &unix_addr, &sock->addr.addr, sizeof(sock->addr) );
3555 sock->bound = 1;
3557 else if (!bind_errno) bind_errno = errno;
3560 if (!req->force_async && !sock->nonblocking && is_fd_overlapped( fd ))
3561 timeout = (timeout_t)sock->sndtimeo * -10000;
3563 if (bind_errno) status = sock_get_ntstatus( bind_errno );
3564 else if (sock->wr_shutdown) status = STATUS_PIPE_DISCONNECTED;
3565 else if (!async_queued( &sock->write_q ))
3567 /* If write_q is not empty, we cannot really tell if the already queued
3568 * asyncs will not consume all available space; if there's no space
3569 * available, the current request won't be immediately satiable.
3571 struct pollfd pollfd;
3572 pollfd.fd = get_unix_fd( sock->fd );
3573 pollfd.events = POLLOUT;
3574 pollfd.revents = 0;
3575 if (poll(&pollfd, 1, 0) >= 0 && pollfd.revents)
3577 /* Give the client opportunity to complete synchronously.
3578 * If it turns out that the I/O request is not actually immediately satiable,
3579 * the client may then choose to re-queue the async (with STATUS_PENDING). */
3580 status = STATUS_ALERTED;
3584 if (status == STATUS_PENDING && !req->force_async && sock->nonblocking)
3585 status = STATUS_DEVICE_NOT_READY;
3587 if ((async = create_request_async( fd, get_fd_comp_flags( fd ), &req->async )))
3589 struct send_req *send_req;
3590 struct iosb *iosb = async_get_iosb( async );
3592 if ((send_req = mem_alloc( sizeof(*send_req) )))
3594 send_req->iosb = (struct iosb *)grab_object( iosb );
3595 send_req->sock = (struct sock *)grab_object( sock );
3596 async_set_completion_callback( async, send_socket_completion_callback, send_req );
3598 else if (status == STATUS_PENDING || status == STATUS_DEVICE_NOT_READY)
3599 status = STATUS_NO_MEMORY;
3601 release_object( iosb );
3603 set_error( status );
3605 if (timeout)
3606 async_set_timeout( async, timeout, STATUS_IO_TIMEOUT );
3608 if (status == STATUS_PENDING || status == STATUS_ALERTED)
3610 queue_async( &sock->write_q, async );
3611 sock_reselect( sock );
3614 reply->wait = async_handoff( async, NULL, 0 );
3615 reply->options = get_fd_options( fd );
3616 reply->nonblocking = sock->nonblocking;
3617 release_object( async );
3619 release_object( sock );
3622 DECL_HANDLER(socket_send_icmp_id)
3624 struct sock *sock = (struct sock *)get_handle_obj( current->process, req->handle, 0, &sock_ops );
3626 if (!sock) return;
3628 if (sock->icmp_fixup_data_len == MAX_ICMP_HISTORY_LENGTH)
3630 memmove( sock->icmp_fixup_data, sock->icmp_fixup_data + 1,
3631 sizeof(*sock->icmp_fixup_data) * (MAX_ICMP_HISTORY_LENGTH - 1) );
3632 --sock->icmp_fixup_data_len;
3635 sock->icmp_fixup_data[sock->icmp_fixup_data_len].icmp_id = req->icmp_id;
3636 sock->icmp_fixup_data[sock->icmp_fixup_data_len].icmp_seq = req->icmp_seq;
3637 ++sock->icmp_fixup_data_len;
3639 release_object( sock );
3642 DECL_HANDLER(socket_get_icmp_id)
3644 struct sock *sock = (struct sock *)get_handle_obj( current->process, req->handle, 0, &sock_ops );
3645 unsigned int i;
3647 if (!sock) return;
3649 for (i = 0; i < sock->icmp_fixup_data_len; ++i)
3651 if (sock->icmp_fixup_data[i].icmp_seq == req->icmp_seq)
3653 reply->icmp_id = sock->icmp_fixup_data[i].icmp_id;
3654 --sock->icmp_fixup_data_len;
3655 memmove( &sock->icmp_fixup_data[i], &sock->icmp_fixup_data[i + 1],
3656 (sock->icmp_fixup_data_len - i) * sizeof(*sock->icmp_fixup_data) );
3657 release_object( sock );
3658 return;
3662 set_error( STATUS_NOT_FOUND );
3663 release_object( sock );