winegstreamer: Implement MF_LOW_LATENCY attribute and latency query.
[wine.git] / server / sock.c
blob088e6d630794979554a7df0bc2e6cdf6fa287555
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 #ifdef HAVE_NETINET_TCP_H
43 # include <netinet/tcp.h>
44 #endif
45 #include <poll.h>
46 #include <sys/time.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <sys/ioctl.h>
50 #ifdef HAVE_SYS_FILIO_H
51 # include <sys/filio.h>
52 #endif
53 #include <time.h>
54 #include <unistd.h>
55 #include <limits.h>
56 #ifdef HAVE_LINUX_FILTER_H
57 # include <linux/filter.h>
58 #endif
59 #ifdef HAVE_LINUX_RTNETLINK_H
60 # include <linux/rtnetlink.h>
61 #endif
63 #ifdef HAVE_NETIPX_IPX_H
64 # include <netipx/ipx.h>
65 #elif defined(HAVE_LINUX_IPX_H)
66 # ifdef HAVE_ASM_TYPES_H
67 # include <asm/types.h>
68 # endif
69 # ifdef HAVE_LINUX_TYPES_H
70 # include <linux/types.h>
71 # endif
72 # include <linux/ipx.h>
73 #endif
74 #if defined(SOL_IPX) || defined(SO_DEFAULT_HEADERS)
75 # define HAS_IPX
76 #endif
78 #ifdef HAVE_LINUX_IRDA_H
79 # ifdef HAVE_LINUX_TYPES_H
80 # include <linux/types.h>
81 # endif
82 # include <linux/irda.h>
83 # define HAS_IRDA
84 #endif
86 #include "ntstatus.h"
87 #define WIN32_NO_STATUS
88 #include "windef.h"
89 #include "winternl.h"
90 #include "winerror.h"
91 #define USE_WS_PREFIX
92 #include "winsock2.h"
93 #include "ws2tcpip.h"
94 #include "wsipx.h"
95 #include "af_irda.h"
96 #include "wine/afd.h"
97 #include "wine/rbtree.h"
99 #include "process.h"
100 #include "file.h"
101 #include "handle.h"
102 #include "thread.h"
103 #include "request.h"
104 #include "user.h"
106 #if defined(linux) && !defined(IP_UNICAST_IF)
107 #define IP_UNICAST_IF 50
108 #endif
110 static const char magic_loopback_addr[] = {127, 12, 34, 56};
112 union win_sockaddr
114 struct WS_sockaddr addr;
115 struct WS_sockaddr_in in;
116 struct WS_sockaddr_in6 in6;
117 struct WS_sockaddr_ipx ipx;
118 SOCKADDR_IRDA irda;
121 union unix_sockaddr
123 struct sockaddr addr;
124 struct sockaddr_in in;
125 struct sockaddr_in6 in6;
126 #ifdef HAS_IPX
127 struct sockaddr_ipx ipx;
128 #endif
129 #ifdef HAS_IRDA
130 struct sockaddr_irda irda;
131 #endif
134 static struct list poll_list = LIST_INIT( poll_list );
136 struct poll_req
138 struct list entry;
139 struct async *async;
140 struct iosb *iosb;
141 struct timeout_user *timeout;
142 timeout_t orig_timeout;
143 int exclusive;
144 int pending;
145 unsigned int count;
146 struct
148 struct sock *sock;
149 int mask;
150 obj_handle_t handle;
151 int flags;
152 unsigned int status;
153 } sockets[1];
156 struct accept_req
158 struct list entry;
159 struct async *async;
160 struct iosb *iosb;
161 struct sock *sock, *acceptsock;
162 int accepted;
163 unsigned int recv_len, local_len;
166 struct connect_req
168 struct async *async;
169 struct iosb *iosb;
170 struct sock *sock;
171 unsigned int addr_len, send_len, send_cursor;
174 struct send_req
176 struct iosb *iosb;
177 struct sock *sock;
180 enum connection_state
182 SOCK_LISTENING,
183 SOCK_UNCONNECTED,
184 SOCK_CONNECTING,
185 SOCK_CONNECTED,
186 SOCK_CONNECTIONLESS,
189 struct bound_addr
191 struct rb_entry entry;
192 union unix_sockaddr addr;
193 int match_any_addr;
194 int reuse_count;
197 #define MAX_ICMP_HISTORY_LENGTH 8
199 struct sock
201 struct object obj; /* object header */
202 struct fd *fd; /* socket file descriptor */
203 enum connection_state state; /* connection state */
204 unsigned int mask; /* event mask */
205 /* pending AFD_POLL_* events which have not yet been reported to the application */
206 unsigned int pending_events;
207 /* AFD_POLL_* events which have already been reported and should not be
208 * selected for again until reset by a relevant call.
210 * For example, if AFD_POLL_READ is set here and not in pending_events, it
211 * has already been reported and consumed, and we should not report it
212 * again, even if POLLIN is signaled, until it is reset by e.g recv().
214 * If an event has been signaled and not consumed yet, it will be set in
215 * both pending_events and reported_events (as we should only ever report
216 * any event once until it is reset.) */
217 unsigned int reported_events;
218 unsigned short proto; /* socket protocol */
219 unsigned short type; /* socket type */
220 unsigned short family; /* socket family */
221 struct event *event; /* event object */
222 user_handle_t window; /* window to send the message to */
223 unsigned int message; /* message to send */
224 obj_handle_t wparam; /* message wparam (socket handle) */
225 int errors[AFD_POLL_BIT_COUNT]; /* event errors */
226 timeout_t connect_time;/* time the socket was connected */
227 struct sock *deferred; /* socket that waits for a deferred accept */
228 struct async_queue read_q; /* queue for asynchronous reads */
229 struct async_queue write_q; /* queue for asynchronous writes */
230 struct async_queue ifchange_q; /* queue for interface change notifications */
231 struct async_queue accept_q; /* queue for asynchronous accepts */
232 struct async_queue connect_q; /* queue for asynchronous connects */
233 struct async_queue poll_q; /* queue for asynchronous polls */
234 struct object *ifchange_obj; /* the interface change notification object */
235 struct list ifchange_entry; /* entry in ifchange notification list */
236 struct list accept_list; /* list of pending accept requests */
237 struct accept_req *accept_recv_req; /* pending accept-into request which will recv on this socket */
238 struct connect_req *connect_req; /* pending connection request */
239 struct poll_req *main_poll; /* main poll */
240 union win_sockaddr addr; /* socket name */
241 int addr_len; /* socket name length */
242 unsigned int rcvbuf; /* advisory recv buffer size */
243 unsigned int sndbuf; /* advisory send buffer size */
244 unsigned int rcvtimeo; /* receive timeout in ms */
245 unsigned int sndtimeo; /* send timeout in ms */
246 struct
248 unsigned short icmp_id;
249 unsigned short icmp_seq;
251 icmp_fixup_data[MAX_ICMP_HISTORY_LENGTH]; /* Sent ICMP packets history used to fixup reply id. */
252 struct bound_addr *bound_addr[2]; /* Links to the entries in bound addresses tree. */
253 unsigned int icmp_fixup_data_len; /* Sent ICMP packets history length. */
254 unsigned int rd_shutdown : 1; /* is the read end shut down? */
255 unsigned int wr_shutdown : 1; /* is the write end shut down? */
256 unsigned int wr_shutdown_pending : 1; /* is a write shutdown pending? */
257 unsigned int hangup : 1; /* has the read end received a hangup? */
258 unsigned int aborted : 1; /* did we get a POLLERR or irregular POLLHUP? */
259 unsigned int nonblocking : 1; /* is the socket nonblocking? */
260 unsigned int bound : 1; /* is the socket bound? */
261 unsigned int reset : 1; /* did we get a TCP reset? */
262 unsigned int reuseaddr : 1; /* winsock SO_REUSEADDR option value */
263 unsigned int exclusiveaddruse : 1; /* winsock SO_EXCLUSIVEADDRUSE option value */
266 static int is_tcp_socket( struct sock *sock )
268 return sock->type == WS_SOCK_STREAM && (sock->family == WS_AF_INET || sock->family == WS_AF_INET6);
271 static int addr_compare( const void *key, const struct wine_rb_entry *entry )
273 const struct bound_addr *bound_addr = RB_ENTRY_VALUE(entry, struct bound_addr, entry);
274 const struct bound_addr *addr = key;
276 if (addr->addr.addr.sa_family != bound_addr->addr.addr.sa_family)
277 return addr->addr.addr.sa_family < bound_addr->addr.addr.sa_family ? -1 : 1;
279 if (addr->addr.addr.sa_family == AF_INET)
281 if (addr->addr.in.sin_port != bound_addr->addr.in.sin_port)
282 return addr->addr.in.sin_port < bound_addr->addr.in.sin_port ? -1 : 1;
283 if (bound_addr->match_any_addr || addr->match_any_addr
284 || addr->addr.in.sin_addr.s_addr == bound_addr->addr.in.sin_addr.s_addr)
285 return 0;
286 return addr->addr.in.sin_addr.s_addr < bound_addr->addr.in.sin_addr.s_addr ? -1 : 1;
289 assert( addr->addr.addr.sa_family == AF_INET6 );
290 if (addr->addr.in6.sin6_port != bound_addr->addr.in6.sin6_port)
291 return addr->addr.in6.sin6_port < bound_addr->addr.in6.sin6_port ? -1 : 1;
292 if (bound_addr->match_any_addr || addr->match_any_addr) return 0;
293 return memcmp( &addr->addr.in6.sin6_addr, &bound_addr->addr.in6.sin6_addr, sizeof(addr->addr.in6.sin6_addr) );
296 static int ipv4addr_from_v6( union unix_sockaddr *v4addr, const struct sockaddr_in6 *in6, int map_unspecified )
298 v4addr->in.sin_family = AF_INET;
299 v4addr->in.sin_port = in6->sin6_port;
301 if (map_unspecified && IN6_IS_ADDR_UNSPECIFIED(&in6->sin6_addr))
303 v4addr->in.sin_addr.s_addr = htonl( INADDR_ANY );
304 return 1;
306 if (IN6_IS_ADDR_V4COMPAT(&in6->sin6_addr) || IN6_IS_ADDR_V4MAPPED(&in6->sin6_addr))
308 memcpy( &v4addr->in.sin_addr.s_addr, &in6->sin6_addr.s6_addr[12], sizeof(v4addr->in.sin_addr.s_addr) );
309 return 1;
311 return 0;
314 static struct rb_tree bound_addresses_tree = { addr_compare };
316 static int should_track_conflicts_for_addr( struct sock *sock, const union unix_sockaddr *addr )
318 if (!is_tcp_socket( sock )) return 0;
320 if (sock->family == WS_AF_INET && addr->addr.sa_family == AF_INET && addr->in.sin_port)
321 return 1;
322 else if (sock->family == WS_AF_INET6 && addr->addr.sa_family == AF_INET6 && addr->in6.sin6_port)
323 return 1;
325 return 0;
328 static int is_any_addr( const union unix_sockaddr *addr )
330 if (addr->addr.sa_family == AF_INET && addr->in.sin_addr.s_addr == htonl( INADDR_ANY ))
331 return 1;
332 if (addr->addr.sa_family == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(&addr->in6.sin6_addr))
333 return 1;
334 return 0;
337 static int check_addr_usage( struct sock *sock, const union unix_sockaddr *addr, int v6only )
339 struct bound_addr *bound_addr, search_addr;
340 struct rb_entry *entry;
342 if (!should_track_conflicts_for_addr( sock, addr )) return 0;
344 search_addr.addr = *addr;
345 search_addr.match_any_addr = sock->exclusiveaddruse && is_any_addr( addr );
347 if ((entry = rb_get( &bound_addresses_tree, &search_addr )))
349 bound_addr = WINE_RB_ENTRY_VALUE(entry, struct bound_addr, entry);
350 if (bound_addr->reuse_count == -1 || !sock->reuseaddr)
352 set_error( sock->reuseaddr || bound_addr->match_any_addr
353 ? STATUS_ACCESS_DENIED : STATUS_SHARING_VIOLATION );
354 return 1;
358 if (sock->family != WS_AF_INET6 || v6only) return 0;
359 if (!ipv4addr_from_v6( &search_addr.addr, &addr->in6, sock->exclusiveaddruse )) return 0;
361 search_addr.match_any_addr = sock->exclusiveaddruse && is_any_addr( &search_addr.addr );
362 if ((entry = rb_get( &bound_addresses_tree, &search_addr )))
364 bound_addr = WINE_RB_ENTRY_VALUE(entry, struct bound_addr, entry);
365 if (bound_addr->reuse_count == -1 || !sock->reuseaddr)
367 set_error( sock->reuseaddr || bound_addr->match_any_addr
368 ? STATUS_ACCESS_DENIED : STATUS_SHARING_VIOLATION );
369 return 1;
372 return 0;
375 static struct bound_addr *register_bound_address( struct sock *sock, const union unix_sockaddr *addr )
377 struct bound_addr *bound_addr, *temp;
379 if (!(bound_addr = mem_alloc( sizeof(*bound_addr) )))
380 return NULL;
382 bound_addr->addr = *addr;
383 bound_addr->match_any_addr = sock->exclusiveaddruse && is_any_addr( addr );
385 if (rb_put( &bound_addresses_tree, bound_addr, &bound_addr->entry ))
387 temp = bound_addr;
388 bound_addr = WINE_RB_ENTRY_VALUE(rb_get( &bound_addresses_tree, temp ), struct bound_addr, entry);
389 free( temp );
390 if (bound_addr->reuse_count == -1)
392 if (debug_level)
393 fprintf( stderr, "register_bound_address: address being updated is already exclusively bound\n" );
394 return NULL;
396 ++bound_addr->reuse_count;
398 else
400 bound_addr->reuse_count = sock->reuseaddr ? 1 : -1;
402 return bound_addr;
405 static void update_addr_usage( struct sock *sock, const union unix_sockaddr *addr, int v6only )
407 union unix_sockaddr v4addr;
409 assert( !sock->bound_addr[0] && !sock->bound_addr[1] );
411 if (!should_track_conflicts_for_addr( sock, addr )) return;
413 sock->bound_addr[0] = register_bound_address( sock, addr );
415 if (sock->family != WS_AF_INET6 || v6only) return;
417 if (!ipv4addr_from_v6( &v4addr, &addr->in6, sock->exclusiveaddruse )) return;
419 sock->bound_addr[1] = register_bound_address( sock, &v4addr );
422 static void sock_dump( struct object *obj, int verbose );
423 static struct fd *sock_get_fd( struct object *obj );
424 static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
425 static void sock_destroy( struct object *obj );
426 static struct object *sock_get_ifchange( struct sock *sock );
427 static void sock_release_ifchange( struct sock *sock );
429 static int sock_get_poll_events( struct fd *fd );
430 static void sock_poll_event( struct fd *fd, int event );
431 static enum server_fd_type sock_get_fd_type( struct fd *fd );
432 static void sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
433 static void sock_cancel_async( struct fd *fd, struct async *async );
434 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
436 static int accept_into_socket( struct sock *sock, struct sock *acceptsock );
437 static struct sock *accept_socket( struct sock *sock );
438 static int sock_get_ntstatus( int err );
439 static unsigned int sock_get_error( int err );
440 static void poll_socket( struct sock *poll_sock, struct async *async, int exclusive, timeout_t timeout,
441 unsigned int count, const struct afd_poll_socket_64 *sockets );
443 static const struct object_ops sock_ops =
445 sizeof(struct sock), /* size */
446 &file_type, /* type */
447 sock_dump, /* dump */
448 add_queue, /* add_queue */
449 remove_queue, /* remove_queue */
450 default_fd_signaled, /* signaled */
451 no_satisfied, /* satisfied */
452 no_signal, /* signal */
453 sock_get_fd, /* get_fd */
454 default_map_access, /* map_access */
455 default_get_sd, /* get_sd */
456 default_set_sd, /* set_sd */
457 no_get_full_name, /* get_full_name */
458 no_lookup_name, /* lookup_name */
459 no_link_name, /* link_name */
460 NULL, /* unlink_name */
461 no_open_file, /* open_file */
462 no_kernel_obj_list, /* get_kernel_obj_list */
463 sock_close_handle, /* close_handle */
464 sock_destroy /* destroy */
467 static const struct fd_ops sock_fd_ops =
469 sock_get_poll_events, /* get_poll_events */
470 sock_poll_event, /* poll_event */
471 sock_get_fd_type, /* get_fd_type */
472 no_fd_read, /* read */
473 no_fd_write, /* write */
474 no_fd_flush, /* flush */
475 default_fd_get_file_info, /* get_file_info */
476 no_fd_get_volume_info, /* get_volume_info */
477 sock_ioctl, /* ioctl */
478 sock_cancel_async, /* cancel_async */
479 no_fd_queue_async, /* queue_async */
480 sock_reselect_async /* reselect_async */
483 static int sockaddr_from_unix( const union unix_sockaddr *uaddr, struct WS_sockaddr *wsaddr, socklen_t wsaddrlen )
485 memset( wsaddr, 0, wsaddrlen );
487 switch (uaddr->addr.sa_family)
489 case AF_INET:
491 struct WS_sockaddr_in win = {0};
493 if (wsaddrlen < sizeof(win)) return -1;
494 win.sin_family = WS_AF_INET;
495 win.sin_port = uaddr->in.sin_port;
496 memcpy( &win.sin_addr, &uaddr->in.sin_addr, sizeof(win.sin_addr) );
497 memcpy( wsaddr, &win, sizeof(win) );
498 return sizeof(win);
501 case AF_INET6:
503 struct WS_sockaddr_in6 win = {0};
505 if (wsaddrlen < sizeof(win)) return -1;
506 win.sin6_family = WS_AF_INET6;
507 win.sin6_port = uaddr->in6.sin6_port;
508 win.sin6_flowinfo = uaddr->in6.sin6_flowinfo;
509 memcpy( &win.sin6_addr, &uaddr->in6.sin6_addr, sizeof(win.sin6_addr) );
510 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
511 win.sin6_scope_id = uaddr->in6.sin6_scope_id;
512 #endif
513 memcpy( wsaddr, &win, sizeof(win) );
514 return sizeof(win);
517 #ifdef HAS_IPX
518 case AF_IPX:
520 struct WS_sockaddr_ipx win = {0};
522 if (wsaddrlen < sizeof(win)) return -1;
523 win.sa_family = WS_AF_IPX;
524 memcpy( win.sa_netnum, &uaddr->ipx.sipx_network, sizeof(win.sa_netnum) );
525 memcpy( win.sa_nodenum, &uaddr->ipx.sipx_node, sizeof(win.sa_nodenum) );
526 win.sa_socket = uaddr->ipx.sipx_port;
527 memcpy( wsaddr, &win, sizeof(win) );
528 return sizeof(win);
530 #endif
532 #ifdef HAS_IRDA
533 case AF_IRDA:
535 SOCKADDR_IRDA win;
537 if (wsaddrlen < sizeof(win)) return -1;
538 win.irdaAddressFamily = WS_AF_IRDA;
539 memcpy( win.irdaDeviceID, &uaddr->irda.sir_addr, sizeof(win.irdaDeviceID) );
540 if (uaddr->irda.sir_lsap_sel != LSAP_ANY)
541 snprintf( win.irdaServiceName, sizeof(win.irdaServiceName), "LSAP-SEL%u", uaddr->irda.sir_lsap_sel );
542 else
543 memcpy( win.irdaServiceName, uaddr->irda.sir_name, sizeof(win.irdaServiceName) );
544 memcpy( wsaddr, &win, sizeof(win) );
545 return sizeof(win);
547 #endif
549 case AF_UNSPEC:
550 return 0;
552 default:
553 return -1;
558 static socklen_t sockaddr_to_unix( const struct WS_sockaddr *wsaddr, int wsaddrlen, union unix_sockaddr *uaddr )
560 memset( uaddr, 0, sizeof(*uaddr) );
562 switch (wsaddr->sa_family)
564 case WS_AF_INET:
566 struct WS_sockaddr_in win = {0};
568 if (wsaddrlen < sizeof(win)) return 0;
569 memcpy( &win, wsaddr, sizeof(win) );
570 uaddr->in.sin_family = AF_INET;
571 uaddr->in.sin_port = win.sin_port;
572 memcpy( &uaddr->in.sin_addr, &win.sin_addr, sizeof(win.sin_addr) );
573 return sizeof(uaddr->in);
576 case WS_AF_INET6:
578 struct WS_sockaddr_in6 win = {0};
580 if (wsaddrlen < sizeof(win)) return 0;
581 memcpy( &win, wsaddr, sizeof(win) );
582 uaddr->in6.sin6_family = AF_INET6;
583 uaddr->in6.sin6_port = win.sin6_port;
584 uaddr->in6.sin6_flowinfo = win.sin6_flowinfo;
585 memcpy( &uaddr->in6.sin6_addr, &win.sin6_addr, sizeof(win.sin6_addr) );
586 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
587 uaddr->in6.sin6_scope_id = win.sin6_scope_id;
588 #endif
589 return sizeof(uaddr->in6);
592 #ifdef HAS_IPX
593 case WS_AF_IPX:
595 struct WS_sockaddr_ipx win = {0};
597 if (wsaddrlen < sizeof(win)) return 0;
598 memcpy( &win, wsaddr, sizeof(win) );
599 uaddr->ipx.sipx_family = AF_IPX;
600 memcpy( &uaddr->ipx.sipx_network, win.sa_netnum, sizeof(win.sa_netnum) );
601 memcpy( &uaddr->ipx.sipx_node, win.sa_nodenum, sizeof(win.sa_nodenum) );
602 uaddr->ipx.sipx_port = win.sa_socket;
603 return sizeof(uaddr->ipx);
605 #endif
607 #ifdef HAS_IRDA
608 case WS_AF_IRDA:
610 SOCKADDR_IRDA win = {0};
611 unsigned int lsap_sel;
613 if (wsaddrlen < sizeof(win)) return 0;
614 memcpy( &win, wsaddr, sizeof(win) );
615 uaddr->irda.sir_family = AF_IRDA;
616 if (sscanf( win.irdaServiceName, "LSAP-SEL%u", &lsap_sel ) == 1)
617 uaddr->irda.sir_lsap_sel = lsap_sel;
618 else
620 uaddr->irda.sir_lsap_sel = LSAP_ANY;
621 memcpy( uaddr->irda.sir_name, win.irdaServiceName, sizeof(win.irdaServiceName) );
623 memcpy( &uaddr->irda.sir_addr, win.irdaDeviceID, sizeof(win.irdaDeviceID) );
624 return sizeof(uaddr->irda);
626 #endif
628 case WS_AF_UNSPEC:
629 switch (wsaddrlen)
631 default: /* likely an ipv4 address */
632 case sizeof(struct WS_sockaddr_in):
633 return sizeof(uaddr->in);
635 #ifdef HAS_IPX
636 case sizeof(struct WS_sockaddr_ipx):
637 return sizeof(uaddr->ipx);
638 #endif
640 #ifdef HAS_IRDA
641 case sizeof(SOCKADDR_IRDA):
642 return sizeof(uaddr->irda);
643 #endif
645 case sizeof(struct WS_sockaddr_in6):
646 return sizeof(uaddr->in6);
649 default:
650 return 0;
654 static socklen_t get_unix_sockaddr_any( union unix_sockaddr *uaddr, int ws_family )
656 memset( uaddr, 0, sizeof(*uaddr) );
657 switch (ws_family)
659 case WS_AF_INET:
660 uaddr->in.sin_family = AF_INET;
661 return sizeof(uaddr->in);
662 case WS_AF_INET6:
663 uaddr->in6.sin6_family = AF_INET6;
664 return sizeof(uaddr->in6);
665 #ifdef HAS_IPX
666 case WS_AF_IPX:
667 uaddr->ipx.sipx_family = AF_IPX;
668 return sizeof(uaddr->ipx);
669 #endif
670 #ifdef HAS_IRDA
671 case WS_AF_IRDA:
672 uaddr->irda.sir_family = AF_IRDA;
673 return sizeof(uaddr->irda);
674 #endif
675 default:
676 return 0;
680 /* some events are generated at the same time but must be sent in a particular
681 * order (e.g. CONNECT must be sent before READ) */
682 static const enum afd_poll_bit event_bitorder[] =
684 AFD_POLL_BIT_CONNECT,
685 AFD_POLL_BIT_CONNECT_ERR,
686 AFD_POLL_BIT_ACCEPT,
687 AFD_POLL_BIT_OOB,
688 AFD_POLL_BIT_READ,
689 AFD_POLL_BIT_WRITE,
690 AFD_POLL_BIT_RESET,
691 AFD_POLL_BIT_HUP,
692 AFD_POLL_BIT_CLOSE,
695 typedef enum {
696 SOCK_SHUTDOWN_ERROR = -1,
697 SOCK_SHUTDOWN_EOF = 0,
698 SOCK_SHUTDOWN_POLLHUP = 1
699 } sock_shutdown_t;
701 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
703 static sock_shutdown_t sock_check_pollhup(void)
705 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
706 int fd[2], n;
707 struct pollfd pfd;
708 char dummy;
710 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
711 if ( shutdown( fd[0], 1 ) ) goto out;
713 pfd.fd = fd[1];
714 pfd.events = POLLIN;
715 pfd.revents = 0;
717 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
718 n = poll( &pfd, 1, 1 );
719 if ( n != 1 ) goto out; /* error or timeout */
720 if ( pfd.revents & POLLHUP )
721 ret = SOCK_SHUTDOWN_POLLHUP;
722 else if ( pfd.revents & POLLIN &&
723 read( fd[1], &dummy, 1 ) == 0 )
724 ret = SOCK_SHUTDOWN_EOF;
726 out:
727 close( fd[0] );
728 close( fd[1] );
729 return ret;
732 void sock_init(void)
734 sock_shutdown_type = sock_check_pollhup();
736 switch ( sock_shutdown_type )
738 case SOCK_SHUTDOWN_EOF:
739 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
740 break;
741 case SOCK_SHUTDOWN_POLLHUP:
742 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
743 break;
744 default:
745 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
746 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
750 static void sock_reselect( struct sock *sock )
752 int ev = sock_get_poll_events( sock->fd );
754 if (debug_level)
755 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
757 set_fd_events( sock->fd, ev );
760 static unsigned int afd_poll_flag_to_win32( unsigned int flags )
762 static const unsigned int map[] =
764 FD_READ, /* READ */
765 FD_OOB, /* OOB */
766 FD_WRITE, /* WRITE */
767 FD_CLOSE, /* HUP */
768 FD_CLOSE, /* RESET */
769 0, /* CLOSE */
770 FD_CONNECT, /* CONNECT */
771 FD_ACCEPT, /* ACCEPT */
772 FD_CONNECT, /* CONNECT_ERR */
775 unsigned int i, ret = 0;
777 for (i = 0; i < ARRAY_SIZE(map); ++i)
779 if (flags & (1 << i)) ret |= map[i];
782 return ret;
785 /* wake anybody waiting on the socket event or send the associated message */
786 static void sock_wake_up( struct sock *sock )
788 unsigned int events = sock->pending_events & sock->mask;
789 int i;
791 if (sock->event)
793 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
794 if (events)
795 set_event( sock->event );
797 if (sock->window)
799 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
800 for (i = 0; i < ARRAY_SIZE(event_bitorder); i++)
802 enum afd_poll_bit event = event_bitorder[i];
803 if (events & (1 << event))
805 lparam_t lparam = afd_poll_flag_to_win32(1 << event) | (sock_get_error( sock->errors[event] ) << 16);
806 post_message( sock->window, sock->message, sock->wparam, lparam );
809 sock->pending_events = 0;
810 sock_reselect( sock );
814 static inline int sock_error( struct sock *sock )
816 int error = 0;
817 socklen_t len = sizeof(error);
819 getsockopt( get_unix_fd(sock->fd), SOL_SOCKET, SO_ERROR, (void *)&error, &len);
821 switch (sock->state)
823 case SOCK_UNCONNECTED:
824 break;
826 case SOCK_CONNECTING:
827 if (error)
828 sock->errors[AFD_POLL_BIT_CONNECT_ERR] = error;
829 else
830 error = sock->errors[AFD_POLL_BIT_CONNECT_ERR];
831 break;
833 case SOCK_LISTENING:
834 if (error)
835 sock->errors[AFD_POLL_BIT_ACCEPT] = error;
836 else
837 error = sock->errors[AFD_POLL_BIT_ACCEPT];
838 break;
840 case SOCK_CONNECTED:
841 case SOCK_CONNECTIONLESS:
842 if (error == ECONNRESET || error == EPIPE)
844 sock->reset = 1;
845 error = 0;
847 else if (error)
848 sock->errors[AFD_POLL_BIT_HUP] = error;
849 else
850 error = sock->errors[AFD_POLL_BIT_HUP];
851 break;
854 return error;
857 static void free_accept_req( void *private )
859 struct accept_req *req = private;
860 list_remove( &req->entry );
861 if (req->acceptsock)
863 req->acceptsock->accept_recv_req = NULL;
864 release_object( req->acceptsock );
866 release_object( req->async );
867 release_object( req->iosb );
868 release_object( req->sock );
869 free( req );
872 static void fill_accept_output( struct accept_req *req )
874 const data_size_t out_size = req->iosb->out_size;
875 struct async *async = req->async;
876 union unix_sockaddr unix_addr;
877 struct WS_sockaddr *win_addr;
878 unsigned int remote_len;
879 socklen_t unix_len;
880 int fd, size = 0;
881 char *out_data;
882 int win_len;
884 if (!(out_data = mem_alloc( out_size )))
886 async_terminate( async, get_error() );
887 return;
890 fd = get_unix_fd( req->acceptsock->fd );
892 if (req->recv_len && (size = recv( fd, out_data, req->recv_len, 0 )) < 0)
894 if (!req->accepted && errno == EWOULDBLOCK)
896 req->accepted = 1;
897 sock_reselect( req->acceptsock );
898 return;
901 async_terminate( async, sock_get_ntstatus( errno ) );
902 free( out_data );
903 return;
906 if (req->local_len)
908 if (req->local_len < sizeof(int))
910 async_terminate( async, STATUS_BUFFER_TOO_SMALL );
911 free( out_data );
912 return;
915 unix_len = sizeof(unix_addr);
916 win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + sizeof(int));
917 if (getsockname( fd, &unix_addr.addr, &unix_len ) < 0 ||
918 (win_len = sockaddr_from_unix( &unix_addr, win_addr, req->local_len - sizeof(int) )) < 0)
920 async_terminate( async, sock_get_ntstatus( errno ) );
921 free( out_data );
922 return;
924 memcpy( out_data + req->recv_len, &win_len, sizeof(int) );
927 unix_len = sizeof(unix_addr);
928 win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + req->local_len + sizeof(int));
929 remote_len = out_size - req->recv_len - req->local_len;
930 if (getpeername( fd, &unix_addr.addr, &unix_len ) < 0 ||
931 (win_len = sockaddr_from_unix( &unix_addr, win_addr, remote_len - sizeof(int) )) < 0)
933 async_terminate( async, sock_get_ntstatus( errno ) );
934 free( out_data );
935 return;
937 memcpy( out_data + req->recv_len + req->local_len, &win_len, sizeof(int) );
939 async_request_complete( req->async, STATUS_SUCCESS, size, out_size, out_data );
942 static void complete_async_accept( struct sock *sock, struct accept_req *req )
944 struct sock *acceptsock = req->acceptsock;
945 struct async *async = req->async;
947 if (debug_level) fprintf( stderr, "completing accept request for socket %p\n", sock );
949 if (acceptsock)
951 if (!accept_into_socket( sock, acceptsock ))
953 async_terminate( async, get_error() );
954 return;
956 fill_accept_output( req );
958 else
960 obj_handle_t handle;
962 if (!(acceptsock = accept_socket( sock )))
964 async_terminate( async, get_error() );
965 return;
967 handle = alloc_handle_no_access_check( async_get_thread( async )->process, &acceptsock->obj,
968 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
969 acceptsock->wparam = handle;
970 sock_reselect( acceptsock );
971 release_object( acceptsock );
972 if (!handle)
974 async_terminate( async, get_error() );
975 return;
978 async_request_complete_alloc( req->async, STATUS_SUCCESS, 0, sizeof(handle), &handle );
982 static void complete_async_accept_recv( struct accept_req *req )
984 if (debug_level) fprintf( stderr, "completing accept recv request for socket %p\n", req->acceptsock );
986 assert( req->recv_len );
988 fill_accept_output( req );
991 static void free_connect_req( void *private )
993 struct connect_req *req = private;
995 req->sock->connect_req = NULL;
996 release_object( req->async );
997 release_object( req->iosb );
998 release_object( req->sock );
999 free( req );
1002 static void complete_async_connect( struct sock *sock )
1004 struct connect_req *req = sock->connect_req;
1005 const char *in_buffer;
1006 size_t len;
1007 int ret;
1009 if (debug_level) fprintf( stderr, "completing connect request for socket %p\n", sock );
1011 if (!req->send_len)
1013 async_terminate( req->async, STATUS_SUCCESS );
1014 return;
1017 in_buffer = (const char *)req->iosb->in_data + sizeof(struct afd_connect_params) + req->addr_len;
1018 len = req->send_len - req->send_cursor;
1020 ret = send( get_unix_fd( sock->fd ), in_buffer + req->send_cursor, len, 0 );
1021 if (ret < 0 && errno != EWOULDBLOCK)
1022 async_terminate( req->async, sock_get_ntstatus( errno ) );
1023 else if (ret == len)
1024 async_request_complete( req->async, STATUS_SUCCESS, req->send_len, 0, NULL );
1025 else
1026 req->send_cursor += ret;
1029 static void free_poll_req( void *private )
1031 struct poll_req *req = private;
1032 unsigned int i;
1034 if (req->timeout) remove_timeout_user( req->timeout );
1036 for (i = 0; i < req->count; ++i)
1037 release_object( req->sockets[i].sock );
1038 release_object( req->async );
1039 release_object( req->iosb );
1040 list_remove( &req->entry );
1041 free( req );
1044 static int is_oobinline( struct sock *sock )
1046 int oobinline;
1047 socklen_t len = sizeof(oobinline);
1048 return !getsockopt( get_unix_fd( sock->fd ), SOL_SOCKET, SO_OOBINLINE, (char *)&oobinline, &len ) && oobinline;
1051 static int get_poll_flags( struct sock *sock, int event )
1053 int flags = 0;
1055 /* A connection-mode socket which has never been connected does not return
1056 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
1057 if (sock->state == SOCK_UNCONNECTED)
1058 event &= ~(POLLOUT | POLLHUP);
1060 if (event & POLLIN)
1062 if (sock->state == SOCK_LISTENING)
1063 flags |= AFD_POLL_ACCEPT;
1064 else
1065 flags |= AFD_POLL_READ;
1067 if (event & POLLPRI)
1068 flags |= is_oobinline( sock ) ? AFD_POLL_READ : AFD_POLL_OOB;
1069 if (event & POLLOUT)
1070 flags |= AFD_POLL_WRITE;
1071 if (sock->state == SOCK_CONNECTED)
1072 flags |= AFD_POLL_CONNECT;
1073 if (event & POLLHUP)
1074 flags |= AFD_POLL_HUP;
1075 if (event & POLLERR)
1076 flags |= AFD_POLL_CONNECT_ERR;
1077 if (sock->reset)
1078 flags |= AFD_POLL_RESET;
1080 return flags;
1083 static void complete_async_poll( struct poll_req *req, unsigned int status )
1085 unsigned int i, signaled_count = 0;
1087 for (i = 0; i < req->count; ++i)
1089 struct sock *sock = req->sockets[i].sock;
1091 if (sock->main_poll == req)
1092 sock->main_poll = NULL;
1095 if (!status)
1097 for (i = 0; i < req->count; ++i)
1099 if (req->sockets[i].flags)
1100 ++signaled_count;
1104 if (is_machine_64bit( async_get_thread( req->async )->process->machine ))
1106 size_t output_size = offsetof( struct afd_poll_params_64, sockets[signaled_count] );
1107 struct afd_poll_params_64 *output;
1109 if (!(output = mem_alloc( output_size )))
1111 async_terminate( req->async, get_error() );
1112 return;
1114 memset( output, 0, output_size );
1115 output->timeout = req->orig_timeout;
1116 output->exclusive = req->exclusive;
1117 for (i = 0; i < req->count; ++i)
1119 if (!req->sockets[i].flags) continue;
1120 output->sockets[output->count].socket = req->sockets[i].handle;
1121 output->sockets[output->count].flags = req->sockets[i].flags;
1122 output->sockets[output->count].status = req->sockets[i].status;
1123 ++output->count;
1125 assert( output->count == signaled_count );
1127 async_request_complete( req->async, status, output_size, output_size, output );
1129 else
1131 size_t output_size = offsetof( struct afd_poll_params_32, sockets[signaled_count] );
1132 struct afd_poll_params_32 *output;
1134 if (!(output = mem_alloc( output_size )))
1136 async_terminate( req->async, get_error() );
1137 return;
1139 memset( output, 0, output_size );
1140 output->timeout = req->orig_timeout;
1141 output->exclusive = req->exclusive;
1142 for (i = 0; i < req->count; ++i)
1144 if (!req->sockets[i].flags) continue;
1145 output->sockets[output->count].socket = req->sockets[i].handle;
1146 output->sockets[output->count].flags = req->sockets[i].flags;
1147 output->sockets[output->count].status = req->sockets[i].status;
1148 ++output->count;
1150 assert( output->count == signaled_count );
1152 async_request_complete( req->async, status, output_size, output_size, output );
1156 static void complete_async_polls( struct sock *sock, int event, int error )
1158 int flags = get_poll_flags( sock, event );
1159 struct poll_req *req, *next;
1161 LIST_FOR_EACH_ENTRY_SAFE( req, next, &poll_list, struct poll_req, entry )
1163 unsigned int i;
1165 if (req->iosb->status != STATUS_PENDING) continue;
1167 for (i = 0; i < req->count; ++i)
1169 if (req->sockets[i].sock != sock) continue;
1170 if (!(req->sockets[i].mask & flags)) continue;
1172 if (debug_level)
1173 fprintf( stderr, "completing poll for socket %p, wanted %#x got %#x\n",
1174 sock, req->sockets[i].mask, flags );
1176 req->sockets[i].flags = req->sockets[i].mask & flags;
1177 req->sockets[i].status = sock_get_ntstatus( error );
1179 if (req->pending)
1181 complete_async_poll( req, STATUS_SUCCESS );
1182 break;
1188 static void async_poll_timeout( void *private )
1190 struct poll_req *req = private;
1192 req->timeout = NULL;
1194 if (req->iosb->status != STATUS_PENDING) return;
1196 complete_async_poll( req, STATUS_TIMEOUT );
1199 static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
1201 if (event & (POLLIN | POLLPRI))
1203 struct accept_req *req;
1205 LIST_FOR_EACH_ENTRY( req, &sock->accept_list, struct accept_req, entry )
1207 if (req->iosb->status == STATUS_PENDING && !req->accepted)
1209 complete_async_accept( sock, req );
1210 event &= ~POLLIN;
1211 break;
1215 if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
1216 complete_async_accept_recv( sock->accept_recv_req );
1219 if ((event & POLLOUT) && sock->connect_req && sock->connect_req->iosb->status == STATUS_PENDING)
1220 complete_async_connect( sock );
1222 if ((event & (POLLIN | POLLPRI)) && async_queued( &sock->read_q ))
1224 if (async_waiting( &sock->read_q ))
1226 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
1227 async_wake_up( &sock->read_q, STATUS_ALERTED );
1229 event &= ~(POLLIN | POLLPRI);
1232 if ((event & POLLOUT) && async_queued( &sock->write_q ))
1234 if (async_waiting( &sock->write_q ))
1236 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
1237 async_wake_up( &sock->write_q, STATUS_ALERTED );
1239 event &= ~POLLOUT;
1242 if (event & (POLLERR | POLLHUP))
1244 int status = sock_get_ntstatus( error );
1245 struct accept_req *req, *next;
1247 async_wake_up( &sock->read_q, status );
1248 async_wake_up( &sock->write_q, status );
1250 LIST_FOR_EACH_ENTRY_SAFE( req, next, &sock->accept_list, struct accept_req, entry )
1252 if (req->iosb->status == STATUS_PENDING)
1253 async_terminate( req->async, status );
1256 if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
1257 async_terminate( sock->accept_recv_req->async, status );
1259 if (sock->connect_req)
1260 async_terminate( sock->connect_req->async, status );
1263 if (sock->reset)
1265 async_wake_up( &sock->read_q, STATUS_CONNECTION_RESET );
1266 async_wake_up( &sock->write_q, STATUS_CONNECTION_RESET );
1268 if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
1269 async_terminate( sock->accept_recv_req->async, STATUS_CONNECTION_RESET );
1272 return event;
1275 static void post_socket_event( struct sock *sock, enum afd_poll_bit event_bit )
1277 unsigned int event = (1 << event_bit);
1279 if (!(sock->reported_events & event))
1281 sock->pending_events |= event;
1282 sock->reported_events |= event;
1286 static void sock_dispatch_events( struct sock *sock, enum connection_state prevstate, int event )
1288 switch (prevstate)
1290 case SOCK_UNCONNECTED:
1291 break;
1293 case SOCK_CONNECTING:
1294 if (event & POLLOUT)
1295 post_socket_event( sock, AFD_POLL_BIT_CONNECT );
1296 if (event & (POLLERR | POLLHUP))
1297 post_socket_event( sock, AFD_POLL_BIT_CONNECT_ERR );
1298 break;
1300 case SOCK_LISTENING:
1301 if (event & (POLLIN | POLLERR | POLLHUP))
1302 post_socket_event( sock, AFD_POLL_BIT_ACCEPT );
1303 break;
1305 case SOCK_CONNECTED:
1306 case SOCK_CONNECTIONLESS:
1307 if (sock->reset)
1308 post_socket_event( sock, AFD_POLL_BIT_RESET );
1310 if (event & POLLIN)
1311 post_socket_event( sock, AFD_POLL_BIT_READ );
1313 if (event & POLLOUT)
1314 post_socket_event( sock, AFD_POLL_BIT_WRITE );
1316 if (event & POLLPRI)
1317 post_socket_event( sock, AFD_POLL_BIT_OOB );
1319 if (event & (POLLERR | POLLHUP))
1320 post_socket_event( sock, AFD_POLL_BIT_HUP );
1321 break;
1324 sock_wake_up( sock );
1327 static void sock_poll_event( struct fd *fd, int event )
1329 struct sock *sock = get_fd_user( fd );
1330 int hangup_seen = 0;
1331 enum connection_state prevstate = sock->state;
1332 int error = 0;
1334 assert( sock->obj.ops == &sock_ops );
1335 if (debug_level)
1336 fprintf(stderr, "socket %p select event: %x\n", sock, event);
1338 if (event & (POLLERR | POLLHUP))
1339 error = sock_error( sock );
1341 switch (sock->state)
1343 case SOCK_UNCONNECTED:
1344 break;
1346 case SOCK_CONNECTING:
1347 if (event & (POLLERR|POLLHUP))
1349 sock->state = SOCK_UNCONNECTED;
1350 event &= ~POLLOUT;
1352 else if (event & POLLOUT)
1354 sock->state = SOCK_CONNECTED;
1355 sock->connect_time = current_time;
1356 sock->errors[AFD_POLL_BIT_CONNECT_ERR] = 0;
1358 break;
1360 case SOCK_LISTENING:
1361 break;
1363 case SOCK_CONNECTED:
1364 case SOCK_CONNECTIONLESS:
1365 if (sock->reset)
1366 event &= ~(POLLIN | POLLERR | POLLHUP);
1368 if (sock->type == WS_SOCK_STREAM && (event & POLLIN))
1370 char dummy;
1371 int nr;
1373 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
1374 * has been closed, so we need to check for it explicitly here */
1375 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
1376 if ( nr == 0 )
1378 hangup_seen = 1;
1379 event &= ~POLLIN;
1381 else if ( nr < 0 )
1383 event &= ~POLLIN;
1384 /* EAGAIN can happen if an async recv() falls between the server's poll()
1385 call and the invocation of this routine */
1386 if (errno == ECONNRESET || errno == EPIPE)
1388 sock->reset = 1;
1390 else if (errno != EAGAIN)
1392 error = errno;
1393 event |= POLLERR;
1394 sock->errors[AFD_POLL_BIT_HUP] = error;
1395 if ( debug_level )
1396 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
1401 if (hangup_seen || (sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP)))
1403 sock->hangup = 1;
1405 else if (event & (POLLHUP | POLLERR))
1407 sock->aborted = 1;
1409 if (debug_level)
1410 fprintf( stderr, "socket %p aborted by error %d, event %#x\n", sock, error, event );
1413 if (hangup_seen)
1414 event |= POLLHUP;
1415 break;
1418 event = sock_dispatch_asyncs( sock, event, error );
1419 sock_dispatch_events( sock, prevstate, event );
1420 complete_async_polls( sock, event, error );
1422 sock_reselect( sock );
1425 static void sock_dump( struct object *obj, int verbose )
1427 struct sock *sock = (struct sock *)obj;
1428 assert( obj->ops == &sock_ops );
1429 fprintf( stderr, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
1430 sock->fd, sock->state,
1431 sock->mask, sock->pending_events, sock->reported_events );
1434 static int poll_flags_from_afd( struct sock *sock, int flags )
1436 int ev = 0;
1438 /* A connection-mode socket which has never been connected does
1439 * not return write or hangup events, but Linux returns
1440 * POLLOUT | POLLHUP. */
1441 if (sock->state == SOCK_UNCONNECTED)
1442 return -1;
1444 if (flags & (AFD_POLL_READ | AFD_POLL_ACCEPT))
1445 ev |= POLLIN;
1446 if ((flags & AFD_POLL_HUP) && sock->type == WS_SOCK_STREAM)
1447 ev |= POLLIN;
1448 if (flags & AFD_POLL_OOB)
1449 ev |= is_oobinline( sock ) ? POLLIN : POLLPRI;
1450 if (flags & AFD_POLL_WRITE)
1451 ev |= POLLOUT;
1453 return ev;
1456 static int sock_get_poll_events( struct fd *fd )
1458 struct sock *sock = get_fd_user( fd );
1459 unsigned int mask = sock->mask & ~sock->reported_events;
1460 struct poll_req *req;
1461 int ev = 0;
1463 assert( sock->obj.ops == &sock_ops );
1465 if (!sock->type) /* not initialized yet */
1466 return -1;
1468 LIST_FOR_EACH_ENTRY( req, &poll_list, struct poll_req, entry )
1470 unsigned int i;
1472 for (i = 0; i < req->count; ++i)
1474 if (req->sockets[i].sock != sock) continue;
1476 ev |= poll_flags_from_afd( sock, req->sockets[i].mask );
1480 switch (sock->state)
1482 case SOCK_UNCONNECTED:
1483 /* A connection-mode Windows socket which has never been connected does
1484 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1485 * need to return -1 here, to prevent the socket from being polled on at
1486 * all. */
1487 return -1;
1489 case SOCK_CONNECTING:
1490 return POLLOUT;
1492 case SOCK_LISTENING:
1493 if (!list_empty( &sock->accept_list ) || (mask & AFD_POLL_ACCEPT))
1494 ev |= POLLIN;
1495 break;
1497 case SOCK_CONNECTED:
1498 case SOCK_CONNECTIONLESS:
1499 if (sock->hangup && sock->wr_shutdown && !sock->wr_shutdown_pending)
1501 /* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
1502 * if both the socket and its peer are SHUT_WR.
1504 * We don't use SHUT_RD, so we can only encounter this in the latter
1505 * case. In that case there can't be any pending read requests (they
1506 * would have already been completed with a length of zero), the
1507 * above condition ensures that we don't have any pending write
1508 * requests, and nothing that can change about the socket state that
1509 * would complete a pending poll request. */
1510 return -1;
1513 if (sock->aborted || sock->reset)
1514 return -1;
1516 if (sock->accept_recv_req)
1518 ev |= POLLIN;
1520 else if (async_queued( &sock->read_q ))
1522 /* Clear POLLIN and POLLPRI if we have an alerted async, even if
1523 * we're polling this socket for READ or OOB. We can't signal the
1524 * poll if the pending async will read all of the data [cf. the
1525 * matching logic in sock_dispatch_asyncs()], but we also don't
1526 * want to spin polling for POLLIN if we're not going to use it. */
1527 if (async_waiting( &sock->read_q ))
1528 ev |= POLLIN | POLLPRI;
1529 else
1530 ev &= ~(POLLIN | POLLPRI);
1532 else
1534 /* Don't ask for POLLIN if we got a hangup. We won't receive more
1535 * data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
1536 if (!sock->hangup)
1538 if (mask & AFD_POLL_READ)
1539 ev |= POLLIN;
1540 if (mask & AFD_POLL_OOB)
1541 ev |= POLLPRI;
1544 /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
1545 if (sock->state == SOCK_CONNECTED && (mask & AFD_POLL_HUP) && !(sock->reported_events & AFD_POLL_READ))
1546 ev |= POLLIN;
1549 if (async_queued( &sock->write_q ))
1551 /* As with read asyncs above, clear POLLOUT if we have an alerted
1552 * async. */
1553 if (async_waiting( &sock->write_q ))
1554 ev |= POLLOUT;
1555 else
1556 ev &= ~POLLOUT;
1558 else if (!sock->wr_shutdown && (mask & AFD_POLL_WRITE))
1560 ev |= POLLOUT;
1563 break;
1566 return ev;
1569 static enum server_fd_type sock_get_fd_type( struct fd *fd )
1571 return FD_TYPE_SOCKET;
1574 static void sock_cancel_async( struct fd *fd, struct async *async )
1576 struct poll_req *req;
1578 LIST_FOR_EACH_ENTRY( req, &poll_list, struct poll_req, entry )
1580 unsigned int i;
1582 if (req->async != async)
1583 continue;
1585 for (i = 0; i < req->count; i++)
1587 struct sock *sock = req->sockets[i].sock;
1589 if (sock->main_poll == req)
1590 sock->main_poll = NULL;
1594 async_terminate( async, STATUS_CANCELLED );
1597 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
1599 struct sock *sock = get_fd_user( fd );
1601 if (sock->wr_shutdown_pending && list_empty( &sock->write_q.queue ))
1603 shutdown( get_unix_fd( sock->fd ), SHUT_WR );
1604 sock->wr_shutdown_pending = 0;
1607 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1608 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1609 * a pseudo-fd. */
1610 if (queue != &sock->ifchange_q && sock->type)
1611 sock_reselect( sock );
1614 static struct fd *sock_get_fd( struct object *obj )
1616 struct sock *sock = (struct sock *)obj;
1617 return (struct fd *)grab_object( sock->fd );
1620 static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
1622 struct sock *sock = (struct sock *)obj;
1624 if (sock->obj.handle_count == 1) /* last handle */
1626 struct accept_req *accept_req, *accept_next;
1627 struct poll_req *poll_req, *poll_next;
1629 if (sock->accept_recv_req)
1630 async_terminate( sock->accept_recv_req->async, STATUS_CANCELLED );
1632 LIST_FOR_EACH_ENTRY_SAFE( accept_req, accept_next, &sock->accept_list, struct accept_req, entry )
1633 async_terminate( accept_req->async, STATUS_CANCELLED );
1635 if (sock->connect_req)
1636 async_terminate( sock->connect_req->async, STATUS_CANCELLED );
1638 LIST_FOR_EACH_ENTRY_SAFE( poll_req, poll_next, &poll_list, struct poll_req, entry )
1640 struct iosb *iosb = poll_req->iosb;
1641 BOOL signaled = FALSE;
1642 unsigned int i;
1644 if (iosb->status != STATUS_PENDING) continue;
1646 for (i = 0; i < poll_req->count; ++i)
1648 if (poll_req->sockets[i].sock == sock)
1650 signaled = TRUE;
1651 poll_req->sockets[i].flags = AFD_POLL_CLOSE;
1652 poll_req->sockets[i].status = 0;
1656 if (signaled) complete_async_poll( poll_req, STATUS_SUCCESS );
1660 return 1;
1663 static void sock_destroy( struct object *obj )
1665 struct sock *sock = (struct sock *)obj;
1666 unsigned int i;
1668 assert( obj->ops == &sock_ops );
1670 /* FIXME: special socket shutdown stuff? */
1672 for (i = 0; i < 2; ++i)
1674 if (sock->bound_addr[i] && --sock->bound_addr[i]->reuse_count <= 0)
1676 rb_remove( &bound_addresses_tree, &sock->bound_addr[i]->entry );
1677 free( sock->bound_addr[i] );
1681 if ( sock->deferred )
1682 release_object( sock->deferred );
1684 async_wake_up( &sock->ifchange_q, STATUS_CANCELLED );
1685 sock_release_ifchange( sock );
1686 free_async_queue( &sock->read_q );
1687 free_async_queue( &sock->write_q );
1688 free_async_queue( &sock->ifchange_q );
1689 free_async_queue( &sock->accept_q );
1690 free_async_queue( &sock->connect_q );
1691 free_async_queue( &sock->poll_q );
1692 if (sock->event) release_object( sock->event );
1693 if (sock->fd) release_object( sock->fd );
1696 static struct sock *create_socket(void)
1698 struct sock *sock;
1700 if (!(sock = alloc_object( &sock_ops ))) return NULL;
1701 sock->fd = NULL;
1702 sock->state = SOCK_UNCONNECTED;
1703 sock->mask = 0;
1704 sock->pending_events = 0;
1705 sock->reported_events = 0;
1706 sock->proto = 0;
1707 sock->type = 0;
1708 sock->family = 0;
1709 sock->event = NULL;
1710 sock->window = 0;
1711 sock->message = 0;
1712 sock->wparam = 0;
1713 sock->connect_time = 0;
1714 sock->deferred = NULL;
1715 sock->ifchange_obj = NULL;
1716 sock->accept_recv_req = NULL;
1717 sock->connect_req = NULL;
1718 sock->main_poll = NULL;
1719 memset( &sock->addr, 0, sizeof(sock->addr) );
1720 sock->addr_len = 0;
1721 sock->rd_shutdown = 0;
1722 sock->wr_shutdown = 0;
1723 sock->wr_shutdown_pending = 0;
1724 sock->hangup = 0;
1725 sock->aborted = 0;
1726 sock->nonblocking = 0;
1727 sock->bound = 0;
1728 sock->reset = 0;
1729 sock->reuseaddr = 0;
1730 sock->exclusiveaddruse = 0;
1731 sock->rcvbuf = 0;
1732 sock->sndbuf = 0;
1733 sock->rcvtimeo = 0;
1734 sock->sndtimeo = 0;
1735 sock->icmp_fixup_data_len = 0;
1736 sock->bound_addr[0] = sock->bound_addr[1] = NULL;
1737 init_async_queue( &sock->read_q );
1738 init_async_queue( &sock->write_q );
1739 init_async_queue( &sock->ifchange_q );
1740 init_async_queue( &sock->accept_q );
1741 init_async_queue( &sock->connect_q );
1742 init_async_queue( &sock->poll_q );
1743 memset( sock->errors, 0, sizeof(sock->errors) );
1744 list_init( &sock->accept_list );
1745 return sock;
1748 static int get_unix_family( int family )
1750 switch (family)
1752 case WS_AF_INET: return AF_INET;
1753 case WS_AF_INET6: return AF_INET6;
1754 #ifdef HAS_IPX
1755 case WS_AF_IPX: return AF_IPX;
1756 #endif
1757 #ifdef AF_IRDA
1758 case WS_AF_IRDA: return AF_IRDA;
1759 #endif
1760 case WS_AF_UNSPEC: return AF_UNSPEC;
1761 default: return -1;
1765 static int get_unix_type( int type )
1767 switch (type)
1769 case WS_SOCK_DGRAM: return SOCK_DGRAM;
1770 case WS_SOCK_RAW: return SOCK_RAW;
1771 case WS_SOCK_STREAM: return SOCK_STREAM;
1772 default: return -1;
1776 static int get_unix_protocol( int protocol )
1778 if (protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
1779 return protocol;
1781 switch (protocol)
1783 case WS_IPPROTO_ICMP: return IPPROTO_ICMP;
1784 case WS_IPPROTO_IGMP: return IPPROTO_IGMP;
1785 case WS_IPPROTO_IP: return IPPROTO_IP;
1786 case WS_IPPROTO_IPV4: return IPPROTO_IPIP;
1787 case WS_IPPROTO_IPV6: return IPPROTO_IPV6;
1788 case WS_IPPROTO_RAW: return IPPROTO_RAW;
1789 case WS_IPPROTO_TCP: return IPPROTO_TCP;
1790 case WS_IPPROTO_UDP: return IPPROTO_UDP;
1791 default: return -1;
1795 static void set_dont_fragment( int fd, int level, int value )
1797 int optname;
1799 if (level == IPPROTO_IP)
1801 #ifdef IP_DONTFRAG
1802 optname = IP_DONTFRAG;
1803 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1804 optname = IP_MTU_DISCOVER;
1805 value = value ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
1806 #else
1807 return;
1808 #endif
1810 else
1812 #ifdef IPV6_DONTFRAG
1813 optname = IPV6_DONTFRAG;
1814 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1815 optname = IPV6_MTU_DISCOVER;
1816 value = value ? IPV6_PMTUDISC_DO : IPV6_PMTUDISC_DONT;
1817 #else
1818 return;
1819 #endif
1822 setsockopt( fd, level, optname, &value, sizeof(value) );
1825 static int init_socket( struct sock *sock, int family, int type, int protocol )
1827 unsigned int options = 0;
1828 int sockfd, unix_type, unix_family, unix_protocol, value;
1829 socklen_t len;
1831 unix_family = get_unix_family( family );
1832 unix_type = get_unix_type( type );
1833 unix_protocol = get_unix_protocol( protocol );
1835 if (unix_protocol < 0)
1837 if (type && unix_type < 0)
1838 set_win32_error( WSAESOCKTNOSUPPORT );
1839 else
1840 set_win32_error( WSAEPROTONOSUPPORT );
1841 return -1;
1843 if (unix_family < 0)
1845 if (family >= 0 && unix_type < 0)
1846 set_win32_error( WSAESOCKTNOSUPPORT );
1847 else
1848 set_win32_error( WSAEAFNOSUPPORT );
1849 return -1;
1852 sockfd = socket( unix_family, unix_type, unix_protocol );
1854 #ifdef linux
1855 if (sockfd == -1 && errno == EPERM && unix_family == AF_INET
1856 && unix_type == SOCK_RAW && unix_protocol == IPPROTO_ICMP)
1858 sockfd = socket( unix_family, SOCK_DGRAM, unix_protocol );
1859 if (sockfd != -1)
1861 const int val = 1;
1863 setsockopt( sockfd, IPPROTO_IP, IP_RECVTTL, (const char *)&val, sizeof(val) );
1864 setsockopt( sockfd, IPPROTO_IP, IP_RECVTOS, (const char *)&val, sizeof(val) );
1865 setsockopt( sockfd, IPPROTO_IP, IP_PKTINFO, (const char *)&val, sizeof(val) );
1868 #endif
1870 if (sockfd == -1)
1872 if (errno == EINVAL) set_win32_error( WSAESOCKTNOSUPPORT );
1873 else set_win32_error( sock_get_error( errno ));
1874 return -1;
1876 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
1878 if (family == WS_AF_IPX && protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
1880 #ifdef HAS_IPX
1881 int ipx_type = protocol - WS_NSPROTO_IPX;
1883 #ifdef SOL_IPX
1884 setsockopt( sockfd, SOL_IPX, IPX_TYPE, &ipx_type, sizeof(ipx_type) );
1885 #else
1886 struct ipx val;
1887 /* Should we retrieve val using a getsockopt call and then
1888 * set the modified one? */
1889 val.ipx_pt = ipx_type;
1890 setsockopt( sockfd, 0, SO_DEFAULT_HEADERS, &val, sizeof(val) );
1891 #endif
1892 #endif
1895 if (unix_family == AF_INET || unix_family == AF_INET6)
1897 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1898 if (unix_type == SOCK_DGRAM || unix_type == SOCK_RAW) /* in Linux the global default can be enabled */
1899 set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, FALSE );
1900 else if (unix_type == SOCK_STREAM)
1901 set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, TRUE );
1904 #ifdef IPV6_V6ONLY
1905 if (unix_family == AF_INET6)
1907 static const int enable = 1;
1908 setsockopt( sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &enable, sizeof(enable) );
1910 #endif
1912 len = sizeof(value);
1913 if (!getsockopt( sockfd, SOL_SOCKET, SO_RCVBUF, &value, &len ))
1914 sock->rcvbuf = value;
1916 len = sizeof(value);
1917 if (!getsockopt( sockfd, SOL_SOCKET, SO_SNDBUF, &value, &len ))
1918 sock->sndbuf = value;
1920 sock->state = (type == WS_SOCK_STREAM ? SOCK_UNCONNECTED : SOCK_CONNECTIONLESS);
1921 sock->proto = protocol;
1922 sock->type = type;
1923 sock->family = family;
1925 if (is_tcp_socket( sock ))
1927 value = 1;
1928 setsockopt( sockfd, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value) );
1929 #ifdef TCP_SYNCNT
1930 value = 4;
1931 setsockopt( sockfd, IPPROTO_TCP, TCP_SYNCNT, &value, sizeof(value) );
1932 #endif
1935 if (sock->fd)
1937 options = get_fd_options( sock->fd );
1938 release_object( sock->fd );
1941 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj, options )))
1943 return -1;
1946 /* We can't immediately allow caching for a connection-mode socket, since it
1947 * might be accepted into (changing the underlying fd object.) */
1948 if (sock->type != WS_SOCK_STREAM) allow_fd_caching( sock->fd );
1950 return 0;
1953 /* accepts a socket and inits it */
1954 static int accept_new_fd( struct sock *sock )
1957 /* Try to accept(2). We can't be safe that this an already connected socket
1958 * or that accept() is allowed on it. In those cases we will get -1/errno
1959 * return.
1961 struct sockaddr saddr;
1962 socklen_t slen = sizeof(saddr);
1963 int acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen );
1964 if (acceptfd != -1)
1965 fcntl( acceptfd, F_SETFL, O_NONBLOCK );
1966 else
1967 set_error( sock_get_ntstatus( errno ));
1968 return acceptfd;
1971 /* accept a socket (creates a new fd) */
1972 static struct sock *accept_socket( struct sock *sock )
1974 struct sock *acceptsock;
1975 int acceptfd;
1977 if (get_unix_fd( sock->fd ) == -1) return NULL;
1979 if ( sock->deferred )
1981 acceptsock = sock->deferred;
1982 sock->deferred = NULL;
1984 else
1986 union unix_sockaddr unix_addr;
1987 socklen_t unix_len;
1989 if ((acceptfd = accept_new_fd( sock )) == -1) return NULL;
1990 if (!(acceptsock = create_socket()))
1992 close( acceptfd );
1993 return NULL;
1996 /* newly created socket gets the same properties of the listening socket */
1997 acceptsock->state = SOCK_CONNECTED;
1998 acceptsock->bound = 1;
1999 acceptsock->nonblocking = sock->nonblocking;
2000 acceptsock->mask = sock->mask;
2001 acceptsock->proto = sock->proto;
2002 acceptsock->type = sock->type;
2003 acceptsock->family = sock->family;
2004 acceptsock->window = sock->window;
2005 acceptsock->message = sock->message;
2006 acceptsock->reuseaddr = sock->reuseaddr;
2007 acceptsock->exclusiveaddruse = sock->exclusiveaddruse;
2008 acceptsock->sndbuf = sock->sndbuf;
2009 acceptsock->rcvbuf = sock->rcvbuf;
2010 acceptsock->sndtimeo = sock->sndtimeo;
2011 acceptsock->rcvtimeo = sock->rcvtimeo;
2012 acceptsock->connect_time = current_time;
2014 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
2015 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
2016 get_fd_options( sock->fd ) )))
2018 release_object( acceptsock );
2019 return NULL;
2021 unix_len = sizeof(unix_addr);
2022 if (!getsockname( acceptfd, &unix_addr.addr, &unix_len ))
2023 acceptsock->addr_len = sockaddr_from_unix( &unix_addr, &acceptsock->addr.addr, sizeof(acceptsock->addr) );
2025 clear_error();
2026 sock->pending_events &= ~AFD_POLL_ACCEPT;
2027 sock->reported_events &= ~AFD_POLL_ACCEPT;
2028 sock_reselect( sock );
2029 return acceptsock;
2032 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
2034 union unix_sockaddr unix_addr;
2035 socklen_t unix_len;
2036 int acceptfd;
2037 struct fd *newfd;
2039 if (get_unix_fd( sock->fd ) == -1) return FALSE;
2041 if ( sock->deferred )
2043 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
2044 get_fd_options( acceptsock->fd ) );
2045 if ( !newfd )
2046 return FALSE;
2048 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
2050 release_object( sock->deferred );
2051 sock->deferred = NULL;
2053 else
2055 if ((acceptfd = accept_new_fd( sock )) == -1)
2056 return FALSE;
2058 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
2059 get_fd_options( acceptsock->fd ) )))
2060 return FALSE;
2063 acceptsock->state = SOCK_CONNECTED;
2064 acceptsock->bound = 1;
2065 acceptsock->pending_events = 0;
2066 acceptsock->reported_events = 0;
2067 acceptsock->proto = sock->proto;
2068 acceptsock->type = sock->type;
2069 acceptsock->family = sock->family;
2070 acceptsock->wparam = 0;
2071 acceptsock->deferred = NULL;
2072 acceptsock->connect_time = current_time;
2073 fd_copy_completion( acceptsock->fd, newfd );
2074 release_object( acceptsock->fd );
2075 acceptsock->fd = newfd;
2077 unix_len = sizeof(unix_addr);
2078 if (!getsockname( get_unix_fd( newfd ), &unix_addr.addr, &unix_len ))
2079 acceptsock->addr_len = sockaddr_from_unix( &unix_addr, &acceptsock->addr.addr, sizeof(acceptsock->addr) );
2081 clear_error();
2082 sock->pending_events &= ~AFD_POLL_ACCEPT;
2083 sock->reported_events &= ~AFD_POLL_ACCEPT;
2084 sock_reselect( sock );
2086 return TRUE;
2089 #ifdef IP_BOUND_IF
2091 static int bind_to_iface_name( int fd, in_addr_t bind_addr, const char *name )
2093 static const int enable = 1;
2094 unsigned int index;
2096 if (!(index = if_nametoindex( name )))
2097 return -1;
2099 if (setsockopt( fd, IPPROTO_IP, IP_BOUND_IF, &index, sizeof(index) ))
2100 return -1;
2102 return setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable) );
2105 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER) && defined(SO_BINDTODEVICE)
2107 struct interface_filter
2109 struct sock_filter iface_memaddr;
2110 struct sock_filter iface_rule;
2111 struct sock_filter ip_memaddr;
2112 struct sock_filter ip_rule;
2113 struct sock_filter return_keep;
2114 struct sock_filter return_dump;
2116 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
2117 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
2118 /sizeof(struct sock_filter)
2119 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
2120 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
2121 /sizeof(struct sock_filter)
2122 # define FILTER_JUMP_NEXT() (u_char)(0)
2123 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
2124 static struct interface_filter generic_interface_filter =
2126 /* This filter rule allows incoming packets on the specified interface, which works for all
2127 * remotely generated packets and for locally generated broadcast packets. */
2128 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, SKF_AD_OFF+SKF_AD_IFINDEX),
2129 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule), FILTER_JUMP_NEXT()),
2130 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
2131 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
2132 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, SKF_NET_OFF+SKF_NET_DESTIP),
2133 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule), FILTER_JUMP_DUMP(ip_rule)),
2134 BPF_STMT(BPF_RET+BPF_K, (u_int)-1), /* keep packet */
2135 BPF_STMT(BPF_RET+BPF_K, 0) /* dump packet */
2138 static int bind_to_iface_name( int fd, in_addr_t bind_addr, const char *name )
2140 struct interface_filter specific_interface_filter;
2141 struct sock_fprog filter_prog;
2142 static const int enable = 1;
2143 unsigned int index;
2144 in_addr_t ifindex;
2146 if (!setsockopt( fd, SOL_SOCKET, SO_BINDTODEVICE, name, strlen( name ) + 1 ))
2147 return 0;
2149 /* SO_BINDTODEVICE requires NET_CAP_RAW until Linux 5.7. */
2150 if (debug_level)
2151 fprintf( stderr, "setsockopt SO_BINDTODEVICE fd %d, name %s failed: %s, falling back to SO_REUSE_ADDR\n",
2152 fd, name, strerror( errno ));
2154 if (!(index = if_nametoindex( name )))
2155 return -1;
2157 ifindex = htonl( index );
2158 if (setsockopt( fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex, sizeof(ifindex) ) < 0)
2159 return -1;
2161 specific_interface_filter = generic_interface_filter;
2162 specific_interface_filter.iface_rule.k = index;
2163 specific_interface_filter.ip_rule.k = htonl( bind_addr );
2164 filter_prog.len = sizeof(generic_interface_filter) / sizeof(struct sock_filter);
2165 filter_prog.filter = (struct sock_filter *)&specific_interface_filter;
2166 if (setsockopt( fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, sizeof(filter_prog) ))
2167 return -1;
2169 return setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable) );
2172 #else
2174 static int bind_to_iface_name( int fd, in_addr_t bind_addr, const char *name )
2176 errno = EOPNOTSUPP;
2177 return -1;
2180 #endif /* LINUX_BOUND_IF */
2182 /* Take bind() calls on any name corresponding to a local network adapter and
2183 * restrict the given socket to operating only on the specified interface. This
2184 * restriction consists of two components:
2185 * 1) An outgoing packet restriction suggesting the egress interface for all
2186 * packets.
2187 * 2) An incoming packet restriction dropping packets not meant for the
2188 * interface.
2189 * If the function succeeds in placing these restrictions, then the name for the
2190 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
2191 * receipt of broadcast packets on the socket. This behavior is only relevant to
2192 * UDP sockets and is needed for applications that expect to be able to receive
2193 * broadcast packets on a socket that is bound to a specific network interface.
2195 static int bind_to_interface( struct sock *sock, const struct sockaddr_in *addr )
2197 in_addr_t bind_addr = addr->sin_addr.s_addr;
2198 struct ifaddrs *ifaddrs, *ifaddr;
2199 int fd = get_unix_fd( sock->fd );
2200 int err = 0;
2202 if (bind_addr == htonl( INADDR_ANY ) || bind_addr == htonl( INADDR_LOOPBACK ))
2203 return 0;
2204 if (sock->type != WS_SOCK_DGRAM)
2205 return 0;
2207 if (getifaddrs( &ifaddrs ) < 0) return 0;
2209 for (ifaddr = ifaddrs; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2211 if (ifaddr->ifa_addr && ifaddr->ifa_addr->sa_family == AF_INET
2212 && ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr == bind_addr)
2214 if ((err = bind_to_iface_name( fd, bind_addr, ifaddr->ifa_name )) < 0)
2216 if (debug_level)
2217 fprintf( stderr, "failed to bind to interface: %s\n", strerror( errno ) );
2219 break;
2222 freeifaddrs( ifaddrs );
2223 return !err;
2226 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2227 static unsigned int get_ipv6_interface_index( const struct in6_addr *addr )
2229 struct ifaddrs *ifaddrs, *ifaddr;
2231 if (getifaddrs( &ifaddrs ) < 0) return 0;
2233 for (ifaddr = ifaddrs; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2235 if (ifaddr->ifa_addr && ifaddr->ifa_addr->sa_family == AF_INET6
2236 && !memcmp( &((struct sockaddr_in6 *)ifaddr->ifa_addr)->sin6_addr, addr, sizeof(*addr) ))
2238 unsigned int index = if_nametoindex( ifaddr->ifa_name );
2240 if (!index)
2242 if (debug_level)
2243 fprintf( stderr, "Unable to look up interface index for %s: %s\n",
2244 ifaddr->ifa_name, strerror( errno ) );
2245 continue;
2248 freeifaddrs( ifaddrs );
2249 return index;
2253 freeifaddrs( ifaddrs );
2254 return 0;
2256 #endif
2258 /* return an errno value mapped to a WSA error */
2259 static unsigned int sock_get_error( int err )
2261 switch (err)
2263 case EINTR: return WSAEINTR;
2264 case EBADF: return WSAEBADF;
2265 case EPERM:
2266 case EACCES: return WSAEACCES;
2267 case EFAULT: return WSAEFAULT;
2268 case EINVAL: return WSAEINVAL;
2269 case EMFILE: return WSAEMFILE;
2270 case EINPROGRESS:
2271 case EWOULDBLOCK: return WSAEWOULDBLOCK;
2272 case EALREADY: return WSAEALREADY;
2273 case ENOTSOCK: return WSAENOTSOCK;
2274 case EDESTADDRREQ: return WSAEDESTADDRREQ;
2275 case EMSGSIZE: return WSAEMSGSIZE;
2276 case EPROTOTYPE: return WSAEPROTOTYPE;
2277 case ENOPROTOOPT: return WSAENOPROTOOPT;
2278 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
2279 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
2280 case EOPNOTSUPP: return WSAEOPNOTSUPP;
2281 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
2282 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
2283 case EADDRINUSE: return WSAEADDRINUSE;
2284 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
2285 case ENETDOWN: return WSAENETDOWN;
2286 case ENETUNREACH: return WSAENETUNREACH;
2287 case ENETRESET: return WSAENETRESET;
2288 case ECONNABORTED: return WSAECONNABORTED;
2289 case EPIPE:
2290 case ECONNRESET: return WSAECONNRESET;
2291 case ENOBUFS: return WSAENOBUFS;
2292 case EISCONN: return WSAEISCONN;
2293 case ENOTCONN: return WSAENOTCONN;
2294 case ESHUTDOWN: return WSAESHUTDOWN;
2295 case ETOOMANYREFS: return WSAETOOMANYREFS;
2296 case ETIMEDOUT: return WSAETIMEDOUT;
2297 case ECONNREFUSED: return WSAECONNREFUSED;
2298 case ELOOP: return WSAELOOP;
2299 case ENAMETOOLONG: return WSAENAMETOOLONG;
2300 case EHOSTDOWN: return WSAEHOSTDOWN;
2301 case EHOSTUNREACH: return WSAEHOSTUNREACH;
2302 case ENOTEMPTY: return WSAENOTEMPTY;
2303 #ifdef EPROCLIM
2304 case EPROCLIM: return WSAEPROCLIM;
2305 #endif
2306 #ifdef EUSERS
2307 case EUSERS: return WSAEUSERS;
2308 #endif
2309 #ifdef EDQUOT
2310 case EDQUOT: return WSAEDQUOT;
2311 #endif
2312 #ifdef ESTALE
2313 case ESTALE: return WSAESTALE;
2314 #endif
2315 #ifdef EREMOTE
2316 case EREMOTE: return WSAEREMOTE;
2317 #endif
2319 case 0: return 0;
2320 default:
2321 errno = err;
2322 perror("wineserver: sock_get_error() can't map error");
2323 return WSAEFAULT;
2327 static int sock_get_ntstatus( int err )
2329 switch ( err )
2331 case EBADF: return STATUS_INVALID_HANDLE;
2332 case EBUSY: return STATUS_DEVICE_BUSY;
2333 case EPERM:
2334 case EACCES: return STATUS_ACCESS_DENIED;
2335 case EFAULT: return STATUS_ACCESS_VIOLATION;
2336 case EINVAL: return STATUS_INVALID_PARAMETER;
2337 case ENFILE:
2338 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
2339 case EINPROGRESS:
2340 case EWOULDBLOCK: return STATUS_DEVICE_NOT_READY;
2341 case EALREADY: return STATUS_NETWORK_BUSY;
2342 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
2343 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
2344 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
2345 case EPROTONOSUPPORT:
2346 case ESOCKTNOSUPPORT:
2347 case EPFNOSUPPORT:
2348 case EAFNOSUPPORT:
2349 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
2350 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
2351 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
2352 case EADDRINUSE: return STATUS_SHARING_VIOLATION;
2353 /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
2354 * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
2355 case ENODEV:
2356 case EADDRNOTAVAIL: return STATUS_INVALID_ADDRESS_COMPONENT;
2357 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
2358 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
2359 case ENOTCONN: return STATUS_INVALID_CONNECTION;
2360 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
2361 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
2362 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
2363 case ENETDOWN: return STATUS_NETWORK_BUSY;
2364 case EPIPE:
2365 case ECONNRESET: return STATUS_CONNECTION_RESET;
2366 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
2367 case EISCONN: return STATUS_CONNECTION_ACTIVE;
2369 case 0: return STATUS_SUCCESS;
2370 default:
2371 errno = err;
2372 perror("wineserver: sock_get_ntstatus() can't map error");
2373 return STATUS_UNSUCCESSFUL;
2377 static struct accept_req *alloc_accept_req( struct sock *sock, struct sock *acceptsock, struct async *async,
2378 const struct afd_accept_into_params *params )
2380 struct accept_req *req = mem_alloc( sizeof(*req) );
2382 if (req)
2384 req->async = (struct async *)grab_object( async );
2385 req->iosb = async_get_iosb( async );
2386 req->sock = (struct sock *)grab_object( sock );
2387 req->acceptsock = acceptsock;
2388 if (acceptsock) grab_object( acceptsock );
2389 req->accepted = 0;
2390 req->recv_len = 0;
2391 req->local_len = 0;
2392 if (params)
2394 req->recv_len = params->recv_len;
2395 req->local_len = params->local_len;
2398 return req;
2401 static void sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2403 struct sock *sock = get_fd_user( fd );
2404 int unix_fd = -1;
2406 assert( sock->obj.ops == &sock_ops );
2408 if (code != IOCTL_AFD_WINE_CREATE && code != IOCTL_AFD_POLL && (unix_fd = get_unix_fd( fd )) < 0)
2409 return;
2411 switch(code)
2413 case IOCTL_AFD_WINE_CREATE:
2415 const struct afd_create_params *params = get_req_data();
2417 if (get_req_data_size() != sizeof(*params))
2419 set_error( STATUS_INVALID_PARAMETER );
2420 return;
2422 init_socket( sock, params->family, params->type, params->protocol );
2423 return;
2426 case IOCTL_AFD_WINE_ACCEPT:
2428 struct sock *acceptsock;
2429 obj_handle_t handle;
2431 if (get_reply_max_size() != sizeof(handle))
2433 set_error( STATUS_BUFFER_TOO_SMALL );
2434 return;
2437 if (!(acceptsock = accept_socket( sock )))
2439 struct accept_req *req;
2441 if (sock->nonblocking) return;
2442 if (get_error() != STATUS_DEVICE_NOT_READY) return;
2444 if (!(req = alloc_accept_req( sock, NULL, async, NULL ))) return;
2445 list_add_tail( &sock->accept_list, &req->entry );
2447 async_set_completion_callback( async, free_accept_req, req );
2448 queue_async( &sock->accept_q, async );
2449 sock_reselect( sock );
2450 set_error( STATUS_PENDING );
2451 return;
2453 handle = alloc_handle( current->process, &acceptsock->obj,
2454 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
2455 acceptsock->wparam = handle;
2456 sock_reselect( acceptsock );
2457 release_object( acceptsock );
2458 set_reply_data( &handle, sizeof(handle) );
2459 return;
2462 case IOCTL_AFD_WINE_ACCEPT_INTO:
2464 static const int access = FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | FILE_READ_DATA;
2465 const struct afd_accept_into_params *params = get_req_data();
2466 struct sock *acceptsock;
2467 unsigned int remote_len;
2468 struct accept_req *req;
2470 if (get_req_data_size() != sizeof(*params) ||
2471 get_reply_max_size() < params->recv_len ||
2472 get_reply_max_size() - params->recv_len < params->local_len)
2474 set_error( STATUS_BUFFER_TOO_SMALL );
2475 return;
2478 remote_len = get_reply_max_size() - params->recv_len - params->local_len;
2479 if (remote_len < sizeof(int))
2481 set_error( STATUS_INVALID_PARAMETER );
2482 return;
2485 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, params->accept_handle, access, &sock_ops )))
2486 return;
2488 if (acceptsock->accept_recv_req)
2490 release_object( acceptsock );
2491 set_error( STATUS_INVALID_PARAMETER );
2492 return;
2495 if (!(req = alloc_accept_req( sock, acceptsock, async, params )))
2497 release_object( acceptsock );
2498 return;
2500 list_add_tail( &sock->accept_list, &req->entry );
2501 acceptsock->accept_recv_req = req;
2502 release_object( acceptsock );
2504 acceptsock->wparam = params->accept_handle;
2505 async_set_completion_callback( async, free_accept_req, req );
2506 queue_async( &sock->accept_q, async );
2507 sock_reselect( sock );
2508 set_error( STATUS_PENDING );
2509 return;
2512 case IOCTL_AFD_LISTEN:
2514 const struct afd_listen_params *params = get_req_data();
2516 if (get_req_data_size() < sizeof(*params))
2518 set_error( STATUS_INVALID_PARAMETER );
2519 return;
2522 if (!sock->bound)
2524 set_error( STATUS_INVALID_PARAMETER );
2525 return;
2528 if (listen( unix_fd, params->backlog ) < 0)
2530 set_error( sock_get_ntstatus( errno ) );
2531 return;
2534 sock->state = SOCK_LISTENING;
2536 /* a listening socket can no longer be accepted into */
2537 allow_fd_caching( sock->fd );
2539 /* we may already be selecting for AFD_POLL_ACCEPT */
2540 sock_reselect( sock );
2541 return;
2544 case IOCTL_AFD_WINE_CONNECT:
2546 const struct afd_connect_params *params = get_req_data();
2547 const struct WS_sockaddr *addr;
2548 union unix_sockaddr unix_addr;
2549 struct connect_req *req;
2550 socklen_t unix_len;
2551 int send_len, ret;
2553 if (get_req_data_size() < sizeof(*params) ||
2554 get_req_data_size() - sizeof(*params) < params->addr_len)
2556 set_error( STATUS_BUFFER_TOO_SMALL );
2557 return;
2559 send_len = get_req_data_size() - sizeof(*params) - params->addr_len;
2560 addr = (const struct WS_sockaddr *)(params + 1);
2562 if (!params->synchronous && !sock->bound)
2564 set_error( STATUS_INVALID_PARAMETER );
2565 return;
2568 if (sock->accept_recv_req)
2570 set_error( STATUS_INVALID_PARAMETER );
2571 return;
2574 if (sock->connect_req)
2576 set_error( STATUS_INVALID_PARAMETER );
2577 return;
2580 switch (sock->state)
2582 case SOCK_LISTENING:
2583 set_error( STATUS_INVALID_PARAMETER );
2584 return;
2586 case SOCK_CONNECTING:
2587 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2588 * but there's no status code that maps to WSAEALREADY... */
2589 set_error( params->synchronous ? STATUS_ADDRESS_ALREADY_ASSOCIATED : STATUS_INVALID_PARAMETER );
2590 return;
2592 case SOCK_CONNECTED:
2593 set_error( STATUS_CONNECTION_ACTIVE );
2594 return;
2596 case SOCK_UNCONNECTED:
2597 case SOCK_CONNECTIONLESS:
2598 break;
2601 unix_len = sockaddr_to_unix( addr, params->addr_len, &unix_addr );
2602 if (!unix_len)
2604 set_error( STATUS_INVALID_ADDRESS );
2605 return;
2607 if (unix_addr.addr.sa_family == AF_INET && !memcmp( &unix_addr.in.sin_addr, magic_loopback_addr, 4 ))
2608 unix_addr.in.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
2610 ret = connect( unix_fd, &unix_addr.addr, unix_len );
2611 if (ret < 0 && errno == ECONNABORTED)
2613 /* On Linux with nonblocking socket if the previous connect() failed for any reason (including
2614 * timeout), next connect will fail. If the error code was queried by getsockopt( SO_ERROR )
2615 * the error code returned now is ECONNABORTED (otherwise that is the actual connect() failure
2616 * error code). If we got here after previous connect attempt on the socket that means
2617 * we already queried SO_ERROR in sock_error(), so retrying on ECONNABORTED only is
2618 * sufficient. */
2619 ret = connect( unix_fd, &unix_addr.addr, unix_len );
2622 if (ret < 0 && errno != EINPROGRESS)
2624 set_error( sock_get_ntstatus( errno ) );
2625 return;
2628 /* a connected or connecting socket can no longer be accepted into */
2629 allow_fd_caching( sock->fd );
2631 unix_len = sizeof(unix_addr);
2632 if (!getsockname( unix_fd, &unix_addr.addr, &unix_len ))
2633 sock->addr_len = sockaddr_from_unix( &unix_addr, &sock->addr.addr, sizeof(sock->addr) );
2634 sock->bound = 1;
2636 if (!ret)
2638 if (sock->type != WS_SOCK_DGRAM)
2640 sock->state = SOCK_CONNECTED;
2641 sock->connect_time = current_time;
2644 if (!send_len) return;
2647 if (sock->type != WS_SOCK_DGRAM)
2648 sock->state = SOCK_CONNECTING;
2650 if (params->synchronous && sock->nonblocking)
2652 sock_reselect( sock );
2653 set_error( STATUS_DEVICE_NOT_READY );
2654 return;
2657 if (!(req = mem_alloc( sizeof(*req) )))
2658 return;
2660 req->async = (struct async *)grab_object( async );
2661 req->iosb = async_get_iosb( async );
2662 req->sock = (struct sock *)grab_object( sock );
2663 req->addr_len = params->addr_len;
2664 req->send_len = send_len;
2665 req->send_cursor = 0;
2667 async_set_completion_callback( async, free_connect_req, req );
2668 sock->connect_req = req;
2669 queue_async( &sock->connect_q, async );
2670 sock_reselect( sock );
2671 set_error( STATUS_PENDING );
2672 return;
2675 case IOCTL_AFD_WINE_SHUTDOWN:
2677 unsigned int how;
2679 if (get_req_data_size() < sizeof(int))
2681 set_error( STATUS_BUFFER_TOO_SMALL );
2682 return;
2684 how = *(int *)get_req_data();
2686 if (how > SD_BOTH)
2688 set_error( STATUS_INVALID_PARAMETER );
2689 return;
2692 if (sock->state != SOCK_CONNECTED && sock->state != SOCK_CONNECTIONLESS)
2694 set_error( STATUS_INVALID_CONNECTION );
2695 return;
2698 if (how != SD_SEND)
2700 sock->rd_shutdown = 1;
2702 if (how != SD_RECEIVE)
2704 sock->wr_shutdown = 1;
2705 if (list_empty( &sock->write_q.queue ))
2706 shutdown( unix_fd, SHUT_WR );
2707 else
2708 sock->wr_shutdown_pending = 1;
2711 if (how == SD_BOTH)
2713 if (sock->event) release_object( sock->event );
2714 sock->event = NULL;
2715 sock->window = 0;
2716 sock->mask = 0;
2717 sock->nonblocking = 1;
2720 sock_reselect( sock );
2721 return;
2724 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE:
2726 int force_async;
2728 if (get_req_data_size() < sizeof(int))
2730 set_error( STATUS_BUFFER_TOO_SMALL );
2731 return;
2733 force_async = *(int *)get_req_data();
2735 if (sock->nonblocking && !force_async)
2737 set_error( STATUS_DEVICE_NOT_READY );
2738 return;
2740 if (!sock_get_ifchange( sock )) return;
2741 queue_async( &sock->ifchange_q, async );
2742 set_error( STATUS_PENDING );
2743 return;
2746 case IOCTL_AFD_WINE_FIONBIO:
2747 if (get_req_data_size() < sizeof(int))
2749 set_error( STATUS_BUFFER_TOO_SMALL );
2750 return;
2752 if (*(int *)get_req_data())
2754 sock->nonblocking = 1;
2756 else
2758 if (sock->mask)
2760 set_error( STATUS_INVALID_PARAMETER );
2761 return;
2763 sock->nonblocking = 0;
2765 return;
2767 case IOCTL_AFD_GET_EVENTS:
2769 struct afd_get_events_params params = {0};
2770 unsigned int i;
2772 if (get_reply_max_size() < sizeof(params))
2774 set_error( STATUS_INVALID_PARAMETER );
2775 return;
2778 params.flags = sock->pending_events & sock->mask;
2779 for (i = 0; i < ARRAY_SIZE( params.status ); ++i)
2780 params.status[i] = sock_get_ntstatus( sock->errors[i] );
2782 sock->pending_events &= ~sock->mask;
2783 sock_reselect( sock );
2785 set_reply_data( &params, sizeof(params) );
2786 return;
2789 case IOCTL_AFD_EVENT_SELECT:
2791 struct event *event = NULL;
2792 obj_handle_t event_handle;
2793 int mask;
2795 set_async_pending( async );
2797 if (is_machine_64bit( current->process->machine ))
2799 const struct afd_event_select_params_64 *params = get_req_data();
2801 if (get_req_data_size() < sizeof(*params))
2803 set_error( STATUS_INVALID_PARAMETER );
2804 return;
2807 event_handle = params->event;
2808 mask = params->mask;
2810 else
2812 const struct afd_event_select_params_32 *params = get_req_data();
2814 if (get_req_data_size() < sizeof(*params))
2816 set_error( STATUS_INVALID_PARAMETER );
2817 return;
2820 event_handle = params->event;
2821 mask = params->mask;
2824 if ((event_handle || mask) &&
2825 !(event = get_event_obj( current->process, event_handle, EVENT_MODIFY_STATE )))
2827 set_error( STATUS_INVALID_PARAMETER );
2828 return;
2831 if (sock->event) release_object( sock->event );
2832 sock->event = event;
2833 sock->mask = mask;
2834 sock->window = 0;
2835 sock->message = 0;
2836 sock->wparam = 0;
2837 sock->nonblocking = 1;
2839 sock_reselect( sock );
2840 /* Explicitly wake the socket up if the mask causes it to become
2841 * signaled. Note that reselecting isn't enough, since we might already
2842 * have had events recorded in sock->reported_events and we don't want
2843 * to select for them again. */
2844 sock_wake_up( sock );
2846 return;
2849 case IOCTL_AFD_WINE_MESSAGE_SELECT:
2851 const struct afd_message_select_params *params = get_req_data();
2853 if (get_req_data_size() < sizeof(params))
2855 set_error( STATUS_BUFFER_TOO_SMALL );
2856 return;
2859 if (sock->event) release_object( sock->event );
2861 if (params->window)
2863 sock->pending_events = 0;
2864 sock->reported_events = 0;
2866 sock->event = NULL;
2867 sock->mask = params->mask;
2868 sock->window = params->window;
2869 sock->message = params->message;
2870 sock->wparam = params->handle;
2871 sock->nonblocking = 1;
2873 sock_reselect( sock );
2875 return;
2878 case IOCTL_AFD_BIND:
2880 const struct afd_bind_params *params = get_req_data();
2881 union unix_sockaddr unix_addr, bind_addr;
2882 data_size_t in_size;
2883 socklen_t unix_len;
2884 int v6only = 1;
2886 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2887 * input */
2888 if (get_req_data_size() < get_reply_max_size())
2890 set_error( STATUS_BUFFER_TOO_SMALL );
2891 return;
2893 in_size = get_req_data_size() - get_reply_max_size();
2894 if (in_size < offsetof(struct afd_bind_params, addr.sa_data)
2895 || get_reply_max_size() < in_size - sizeof(int))
2897 set_error( STATUS_INVALID_PARAMETER );
2898 return;
2901 if (sock->bound)
2903 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED );
2904 return;
2907 unix_len = sockaddr_to_unix( &params->addr, in_size - sizeof(int), &unix_addr );
2908 if (!unix_len)
2910 set_error( STATUS_INVALID_ADDRESS );
2911 return;
2913 bind_addr = unix_addr;
2915 if (unix_addr.addr.sa_family == AF_INET)
2917 if (!memcmp( &unix_addr.in.sin_addr, magic_loopback_addr, 4 )
2918 || bind_to_interface( sock, &unix_addr.in ))
2919 bind_addr.in.sin_addr.s_addr = htonl( INADDR_ANY );
2921 else if (unix_addr.addr.sa_family == AF_INET6)
2923 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2924 /* Windows allows specifying zero to use the default scope. Linux
2925 * interprets it as an interface index and requires that it be
2926 * nonzero. */
2927 if (!unix_addr.in6.sin6_scope_id)
2928 bind_addr.in6.sin6_scope_id = get_ipv6_interface_index( &unix_addr.in6.sin6_addr );
2929 #endif
2932 set_async_pending( async );
2934 #ifdef IPV6_V6ONLY
2935 if (sock->family == WS_AF_INET6)
2937 socklen_t len = sizeof(v6only);
2939 getsockopt( get_unix_fd(sock->fd), IPPROTO_IPV6, IPV6_V6ONLY, &v6only, &len );
2941 #endif
2943 if (check_addr_usage( sock, &bind_addr, v6only ))
2944 return;
2946 if (bind( unix_fd, &bind_addr.addr, unix_len ) < 0)
2948 if (errno == EADDRINUSE && sock->reuseaddr)
2949 errno = EACCES;
2951 set_error( sock_get_ntstatus( errno ) );
2952 return;
2955 sock->bound = 1;
2957 unix_len = sizeof(bind_addr);
2958 if (!getsockname( unix_fd, &bind_addr.addr, &unix_len ))
2960 /* store the interface or magic loopback address instead of the
2961 * actual unix address */
2962 if (bind_addr.addr.sa_family == AF_INET)
2963 bind_addr.in.sin_addr = unix_addr.in.sin_addr;
2964 sock->addr_len = sockaddr_from_unix( &bind_addr, &sock->addr.addr, sizeof(sock->addr) );
2967 update_addr_usage( sock, &bind_addr, v6only );
2969 if (get_reply_max_size() >= sock->addr_len)
2970 set_reply_data( &sock->addr, sock->addr_len );
2971 return;
2974 case IOCTL_AFD_GETSOCKNAME:
2975 if (!sock->bound)
2977 set_error( STATUS_INVALID_PARAMETER );
2978 return;
2981 if (get_reply_max_size() < sock->addr_len)
2983 set_error( STATUS_BUFFER_TOO_SMALL );
2984 return;
2987 set_reply_data( &sock->addr, sock->addr_len );
2988 return;
2990 case IOCTL_AFD_WINE_DEFER:
2992 const obj_handle_t *handle = get_req_data();
2993 struct sock *acceptsock;
2995 if (get_req_data_size() < sizeof(*handle))
2997 set_error( STATUS_BUFFER_TOO_SMALL );
2998 return;
3001 acceptsock = (struct sock *)get_handle_obj( current->process, *handle, 0, &sock_ops );
3002 if (!acceptsock) return;
3004 sock->deferred = acceptsock;
3005 return;
3008 case IOCTL_AFD_WINE_GET_INFO:
3010 struct afd_get_info_params params;
3012 if (get_reply_max_size() < sizeof(params))
3014 set_error( STATUS_BUFFER_TOO_SMALL );
3015 return;
3018 params.family = sock->family;
3019 params.type = sock->type;
3020 params.protocol = sock->proto;
3021 set_reply_data( &params, sizeof(params) );
3022 return;
3025 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN:
3027 int listening = (sock->state == SOCK_LISTENING);
3029 if (get_reply_max_size() < sizeof(listening))
3031 set_error( STATUS_BUFFER_TOO_SMALL );
3032 return;
3035 set_reply_data( &listening, sizeof(listening) );
3036 return;
3039 case IOCTL_AFD_WINE_GET_SO_ERROR:
3041 int error;
3042 unsigned int i;
3044 if (get_reply_max_size() < sizeof(error))
3046 set_error( STATUS_BUFFER_TOO_SMALL );
3047 return;
3050 error = sock_error( sock );
3051 if (!error)
3053 for (i = 0; i < ARRAY_SIZE( sock->errors ); ++i)
3055 if (sock->errors[i])
3057 error = sock->errors[i];
3058 break;
3063 error = sock_get_error( error );
3064 set_reply_data( &error, sizeof(error) );
3065 return;
3068 case IOCTL_AFD_WINE_GET_SO_RCVBUF:
3070 int rcvbuf = sock->rcvbuf;
3072 if (get_reply_max_size() < sizeof(rcvbuf))
3074 set_error( STATUS_BUFFER_TOO_SMALL );
3075 return;
3078 set_reply_data( &rcvbuf, sizeof(rcvbuf) );
3079 return;
3082 case IOCTL_AFD_WINE_SET_SO_RCVBUF:
3084 DWORD rcvbuf;
3086 if (get_req_data_size() < sizeof(rcvbuf))
3088 set_error( STATUS_BUFFER_TOO_SMALL );
3089 return;
3091 rcvbuf = *(DWORD *)get_req_data();
3093 if (!setsockopt( unix_fd, SOL_SOCKET, SO_RCVBUF, (char *)&rcvbuf, sizeof(rcvbuf) ))
3094 sock->rcvbuf = rcvbuf;
3095 else
3096 set_error( sock_get_ntstatus( errno ) );
3097 return;
3100 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO:
3102 DWORD rcvtimeo = sock->rcvtimeo;
3104 if (get_reply_max_size() < sizeof(rcvtimeo))
3106 set_error( STATUS_BUFFER_TOO_SMALL );
3107 return;
3110 set_reply_data( &rcvtimeo, sizeof(rcvtimeo) );
3111 return;
3114 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO:
3116 DWORD rcvtimeo;
3118 if (get_req_data_size() < sizeof(rcvtimeo))
3120 set_error( STATUS_BUFFER_TOO_SMALL );
3121 return;
3123 rcvtimeo = *(DWORD *)get_req_data();
3125 sock->rcvtimeo = rcvtimeo;
3126 return;
3129 /* BSD socket SO_REUSEADDR is not compatible with winsock semantics. */
3130 case IOCTL_AFD_WINE_SET_SO_REUSEADDR:
3132 int reuse, ret;
3134 if (get_req_data_size() < sizeof(reuse))
3136 set_error( STATUS_BUFFER_TOO_SMALL );
3137 return;
3140 reuse = *(int *)get_req_data();
3142 if (reuse && sock->exclusiveaddruse)
3144 set_error( STATUS_INVALID_PARAMETER );
3145 return;
3148 if (is_tcp_socket( sock ))
3149 ret = 0;
3150 else
3151 ret = setsockopt( unix_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) );
3152 #ifdef __APPLE__
3153 if (!ret) ret = setsockopt( unix_fd, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(reuse) );
3154 #endif
3155 if (ret)
3156 set_error( sock_get_ntstatus( errno ) );
3157 else
3158 sock->reuseaddr = !!reuse;
3159 return;
3162 case IOCTL_AFD_WINE_SET_SO_EXCLUSIVEADDRUSE:
3164 int exclusive;
3166 if (get_req_data_size() < sizeof(exclusive))
3168 set_error( STATUS_BUFFER_TOO_SMALL );
3169 return;
3172 exclusive = *(int *)get_req_data();
3173 if (exclusive && sock->reuseaddr)
3175 set_error( STATUS_INVALID_PARAMETER );
3176 return;
3178 sock->exclusiveaddruse = !!exclusive;
3179 return;
3182 case IOCTL_AFD_WINE_GET_SO_SNDBUF:
3184 int sndbuf = sock->sndbuf;
3186 if (get_reply_max_size() < sizeof(sndbuf))
3188 set_error( STATUS_BUFFER_TOO_SMALL );
3189 return;
3192 set_reply_data( &sndbuf, sizeof(sndbuf) );
3193 return;
3196 case IOCTL_AFD_WINE_SET_SO_SNDBUF:
3198 DWORD sndbuf;
3200 if (get_req_data_size() < sizeof(sndbuf))
3202 set_error( STATUS_BUFFER_TOO_SMALL );
3203 return;
3205 sndbuf = *(DWORD *)get_req_data();
3207 #ifdef __APPLE__
3208 if (!sndbuf)
3210 /* setsockopt fails if a zero value is passed */
3211 sock->sndbuf = sndbuf;
3212 return;
3214 #endif
3216 if (!setsockopt( unix_fd, SOL_SOCKET, SO_SNDBUF, (char *)&sndbuf, sizeof(sndbuf) ))
3217 sock->sndbuf = sndbuf;
3218 else
3219 set_error( sock_get_ntstatus( errno ) );
3220 return;
3223 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO:
3225 DWORD sndtimeo = sock->sndtimeo;
3227 if (get_reply_max_size() < sizeof(sndtimeo))
3229 set_error( STATUS_BUFFER_TOO_SMALL );
3230 return;
3233 set_reply_data( &sndtimeo, sizeof(sndtimeo) );
3234 return;
3237 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO:
3239 DWORD sndtimeo;
3241 if (get_req_data_size() < sizeof(sndtimeo))
3243 set_error( STATUS_BUFFER_TOO_SMALL );
3244 return;
3246 sndtimeo = *(DWORD *)get_req_data();
3248 sock->sndtimeo = sndtimeo;
3249 return;
3252 case IOCTL_AFD_WINE_GET_SO_CONNECT_TIME:
3254 DWORD time = ~0u;
3256 if (get_reply_max_size() < sizeof(time))
3258 set_error( STATUS_BUFFER_TOO_SMALL );
3259 return;
3262 if (sock->state == SOCK_CONNECTED)
3263 time = (current_time - sock->connect_time) / 10000000;
3265 set_reply_data( &time, sizeof(time) );
3266 return;
3269 case IOCTL_AFD_WINE_GET_SO_REUSEADDR:
3271 int reuse;
3273 if (!get_reply_max_size())
3275 set_error( STATUS_BUFFER_TOO_SMALL );
3276 return;
3279 reuse = sock->reuseaddr;
3280 set_reply_data( &reuse, min( sizeof(reuse), get_reply_max_size() ));
3281 return;
3284 case IOCTL_AFD_WINE_GET_SO_EXCLUSIVEADDRUSE:
3286 int exclusive;
3288 if (!get_reply_max_size())
3290 set_error( STATUS_BUFFER_TOO_SMALL );
3291 return;
3294 exclusive = sock->exclusiveaddruse;
3295 set_reply_data( &exclusive, min( sizeof(exclusive), get_reply_max_size() ));
3296 return;
3299 case IOCTL_AFD_POLL:
3301 if (get_reply_max_size() < get_req_data_size())
3303 set_error( STATUS_INVALID_PARAMETER );
3304 return;
3307 if (is_machine_64bit( current->process->machine ))
3309 const struct afd_poll_params_64 *params = get_req_data();
3311 if (get_req_data_size() < sizeof(struct afd_poll_params_64) ||
3312 get_req_data_size() < offsetof( struct afd_poll_params_64, sockets[params->count] ))
3314 set_error( STATUS_INVALID_PARAMETER );
3315 return;
3318 poll_socket( sock, async, params->exclusive, params->timeout, params->count, params->sockets );
3320 else
3322 const struct afd_poll_params_32 *params = get_req_data();
3323 struct afd_poll_socket_64 *sockets;
3324 unsigned int i;
3326 if (get_req_data_size() < sizeof(struct afd_poll_params_32) ||
3327 get_req_data_size() < offsetof( struct afd_poll_params_32, sockets[params->count] ))
3329 set_error( STATUS_INVALID_PARAMETER );
3330 return;
3333 if (!(sockets = mem_alloc( params->count * sizeof(*sockets) ))) return;
3334 for (i = 0; i < params->count; ++i)
3336 sockets[i].socket = params->sockets[i].socket;
3337 sockets[i].flags = params->sockets[i].flags;
3338 sockets[i].status = params->sockets[i].status;
3341 poll_socket( sock, async, params->exclusive, params->timeout, params->count, sockets );
3342 free( sockets );
3345 return;
3348 default:
3349 set_error( STATUS_NOT_SUPPORTED );
3350 return;
3354 static void handle_exclusive_poll(struct poll_req *req)
3356 unsigned int i;
3358 for (i = 0; i < req->count; ++i)
3360 struct sock *sock = req->sockets[i].sock;
3361 struct poll_req *main_poll = sock->main_poll;
3363 if (main_poll && main_poll->exclusive && req->exclusive)
3365 complete_async_poll( main_poll, STATUS_SUCCESS );
3366 main_poll = NULL;
3369 if (!main_poll)
3370 sock->main_poll = req;
3374 static void poll_socket( struct sock *poll_sock, struct async *async, int exclusive, timeout_t timeout,
3375 unsigned int count, const struct afd_poll_socket_64 *sockets )
3377 BOOL signaled = FALSE;
3378 struct poll_req *req;
3379 unsigned int i, j;
3381 if (!count)
3383 set_error( STATUS_INVALID_PARAMETER );
3384 return;
3387 if (!(req = mem_alloc( offsetof( struct poll_req, sockets[count] ) )))
3388 return;
3390 req->timeout = NULL;
3391 req->pending = 0;
3392 if (timeout && timeout != TIMEOUT_INFINITE &&
3393 !(req->timeout = add_timeout_user( timeout, async_poll_timeout, req )))
3395 free( req );
3396 return;
3398 req->orig_timeout = timeout;
3400 for (i = 0; i < count; ++i)
3402 req->sockets[i].sock = (struct sock *)get_handle_obj( current->process, sockets[i].socket, 0, &sock_ops );
3403 if (!req->sockets[i].sock)
3405 for (j = 0; j < i; ++j) release_object( req->sockets[j].sock );
3406 if (req->timeout) remove_timeout_user( req->timeout );
3407 free( req );
3408 return;
3410 req->sockets[i].handle = sockets[i].socket;
3411 req->sockets[i].mask = sockets[i].flags;
3412 req->sockets[i].flags = 0;
3415 req->exclusive = exclusive;
3416 req->count = count;
3417 req->async = (struct async *)grab_object( async );
3418 req->iosb = async_get_iosb( async );
3420 handle_exclusive_poll(req);
3422 list_add_tail( &poll_list, &req->entry );
3423 async_set_completion_callback( async, free_poll_req, req );
3424 queue_async( &poll_sock->poll_q, async );
3426 for (i = 0; i < count; ++i)
3428 struct sock *sock = req->sockets[i].sock;
3429 int mask = req->sockets[i].mask;
3430 struct pollfd pollfd;
3432 pollfd.fd = get_unix_fd( sock->fd );
3433 pollfd.events = poll_flags_from_afd( sock, mask );
3434 if (pollfd.events >= 0 && poll( &pollfd, 1, 0 ) >= 0)
3435 sock_poll_event( sock->fd, pollfd.revents );
3437 /* FIXME: do other error conditions deserve a similar treatment? */
3438 if (sock->state != SOCK_CONNECTING && sock->errors[AFD_POLL_BIT_CONNECT_ERR] && (mask & AFD_POLL_CONNECT_ERR))
3440 req->sockets[i].flags |= AFD_POLL_CONNECT_ERR;
3441 req->sockets[i].status = sock_get_ntstatus( sock->errors[AFD_POLL_BIT_CONNECT_ERR] );
3444 if (req->sockets[i].flags)
3445 signaled = TRUE;
3448 if (!timeout || signaled)
3449 complete_async_poll( req, STATUS_SUCCESS );
3450 else
3451 req->pending = 1;
3453 for (i = 0; i < req->count; ++i)
3454 sock_reselect( req->sockets[i].sock );
3455 set_error( STATUS_PENDING );
3458 #ifdef HAVE_LINUX_RTNETLINK_H
3460 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
3461 static struct object *ifchange_object;
3463 static void ifchange_dump( struct object *obj, int verbose );
3464 static struct fd *ifchange_get_fd( struct object *obj );
3465 static void ifchange_destroy( struct object *obj );
3467 static int ifchange_get_poll_events( struct fd *fd );
3468 static void ifchange_poll_event( struct fd *fd, int event );
3470 struct ifchange
3472 struct object obj; /* object header */
3473 struct fd *fd; /* interface change file descriptor */
3474 struct list sockets; /* list of sockets to send interface change notifications */
3477 static const struct object_ops ifchange_ops =
3479 sizeof(struct ifchange), /* size */
3480 &no_type, /* type */
3481 ifchange_dump, /* dump */
3482 no_add_queue, /* add_queue */
3483 NULL, /* remove_queue */
3484 NULL, /* signaled */
3485 no_satisfied, /* satisfied */
3486 no_signal, /* signal */
3487 ifchange_get_fd, /* get_fd */
3488 default_map_access, /* map_access */
3489 default_get_sd, /* get_sd */
3490 default_set_sd, /* set_sd */
3491 no_get_full_name, /* get_full_name */
3492 no_lookup_name, /* lookup_name */
3493 no_link_name, /* link_name */
3494 NULL, /* unlink_name */
3495 no_open_file, /* open_file */
3496 no_kernel_obj_list, /* get_kernel_obj_list */
3497 no_close_handle, /* close_handle */
3498 ifchange_destroy /* destroy */
3501 static const struct fd_ops ifchange_fd_ops =
3503 ifchange_get_poll_events, /* get_poll_events */
3504 ifchange_poll_event, /* poll_event */
3505 NULL, /* get_fd_type */
3506 no_fd_read, /* read */
3507 no_fd_write, /* write */
3508 no_fd_flush, /* flush */
3509 no_fd_get_file_info, /* get_file_info */
3510 no_fd_get_volume_info, /* get_volume_info */
3511 no_fd_ioctl, /* ioctl */
3512 NULL, /* cancel_async */
3513 NULL, /* queue_async */
3514 NULL /* reselect_async */
3517 static void ifchange_dump( struct object *obj, int verbose )
3519 assert( obj->ops == &ifchange_ops );
3520 fprintf( stderr, "Interface change\n" );
3523 static struct fd *ifchange_get_fd( struct object *obj )
3525 struct ifchange *ifchange = (struct ifchange *)obj;
3526 return (struct fd *)grab_object( ifchange->fd );
3529 static void ifchange_destroy( struct object *obj )
3531 struct ifchange *ifchange = (struct ifchange *)obj;
3532 assert( obj->ops == &ifchange_ops );
3534 release_object( ifchange->fd );
3536 /* reset the global ifchange object so that it will be recreated if it is needed again */
3537 assert( obj == ifchange_object );
3538 ifchange_object = NULL;
3541 static int ifchange_get_poll_events( struct fd *fd )
3543 return POLLIN;
3546 /* wake up all the sockets waiting for a change notification event */
3547 static void ifchange_wake_up( struct object *obj, unsigned int status )
3549 struct ifchange *ifchange = (struct ifchange *)obj;
3550 struct list *ptr, *next;
3551 assert( obj->ops == &ifchange_ops );
3552 assert( obj == ifchange_object );
3554 LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
3556 struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
3558 assert( sock->ifchange_obj );
3559 async_wake_up( &sock->ifchange_q, status ); /* issue ifchange notification for the socket */
3560 sock_release_ifchange( sock ); /* remove socket from list and decrement ifchange refcount */
3564 static void ifchange_poll_event( struct fd *fd, int event )
3566 struct object *ifchange = get_fd_user( fd );
3567 unsigned int status = STATUS_PENDING;
3568 char buffer[PIPE_BUF];
3569 int r;
3571 r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
3572 if (r < 0)
3574 if (errno == EWOULDBLOCK || (EWOULDBLOCK != EAGAIN && errno == EAGAIN))
3575 return; /* retry when poll() says the socket is ready */
3576 status = sock_get_ntstatus( errno );
3578 else if (r > 0)
3580 struct nlmsghdr *nlh;
3582 for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
3584 if (nlh->nlmsg_type == NLMSG_DONE)
3585 break;
3586 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
3587 status = STATUS_SUCCESS;
3590 else status = STATUS_CANCELLED;
3592 if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
3595 #endif
3597 /* we only need one of these interface notification objects, all of the sockets dependent upon
3598 * it will wake up when a notification event occurs */
3599 static struct object *get_ifchange( void )
3601 #ifdef HAVE_LINUX_RTNETLINK_H
3602 struct ifchange *ifchange;
3603 struct sockaddr_nl addr;
3604 int unix_fd;
3606 if (ifchange_object)
3608 /* increment the refcount for each socket that uses the ifchange object */
3609 return grab_object( ifchange_object );
3612 /* create the socket we need for processing interface change notifications */
3613 unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
3614 if (unix_fd == -1)
3616 set_error( sock_get_ntstatus( errno ));
3617 return NULL;
3619 fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
3620 memset( &addr, 0, sizeof(addr) );
3621 addr.nl_family = AF_NETLINK;
3622 addr.nl_groups = RTMGRP_IPV4_IFADDR;
3623 /* bind the socket to the special netlink kernel interface */
3624 if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
3626 close( unix_fd );
3627 set_error( sock_get_ntstatus( errno ));
3628 return NULL;
3630 if (!(ifchange = alloc_object( &ifchange_ops )))
3632 close( unix_fd );
3633 set_error( STATUS_NO_MEMORY );
3634 return NULL;
3636 list_init( &ifchange->sockets );
3637 if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
3639 release_object( ifchange );
3640 set_error( STATUS_NO_MEMORY );
3641 return NULL;
3643 set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
3645 /* the ifchange object is now successfully configured */
3646 ifchange_object = &ifchange->obj;
3647 return &ifchange->obj;
3648 #else
3649 set_error( STATUS_NOT_SUPPORTED );
3650 return NULL;
3651 #endif
3654 /* add the socket to the interface change notification list */
3655 static void ifchange_add_sock( struct object *obj, struct sock *sock )
3657 #ifdef HAVE_LINUX_RTNETLINK_H
3658 struct ifchange *ifchange = (struct ifchange *)obj;
3660 list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
3661 #endif
3664 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3665 static struct object *sock_get_ifchange( struct sock *sock )
3667 struct object *ifchange;
3669 if (sock->ifchange_obj) /* reuse existing ifchange_obj for this socket */
3670 return sock->ifchange_obj;
3672 if (!(ifchange = get_ifchange()))
3673 return NULL;
3675 /* add the socket to the ifchange notification list */
3676 ifchange_add_sock( ifchange, sock );
3677 sock->ifchange_obj = ifchange;
3678 return ifchange;
3681 /* destroy an existing ifchange queue for a specific socket */
3682 static void sock_release_ifchange( struct sock *sock )
3684 if (sock->ifchange_obj)
3686 list_remove( &sock->ifchange_entry );
3687 release_object( sock->ifchange_obj );
3688 sock->ifchange_obj = NULL;
3692 static void socket_device_dump( struct object *obj, int verbose );
3693 static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
3694 unsigned int attr, struct object *root );
3695 static struct object *socket_device_open_file( struct object *obj, unsigned int access,
3696 unsigned int sharing, unsigned int options );
3698 static const struct object_ops socket_device_ops =
3700 sizeof(struct object), /* size */
3701 &device_type, /* type */
3702 socket_device_dump, /* dump */
3703 no_add_queue, /* add_queue */
3704 NULL, /* remove_queue */
3705 NULL, /* signaled */
3706 no_satisfied, /* satisfied */
3707 no_signal, /* signal */
3708 no_get_fd, /* get_fd */
3709 default_map_access, /* map_access */
3710 default_get_sd, /* get_sd */
3711 default_set_sd, /* set_sd */
3712 default_get_full_name, /* get_full_name */
3713 socket_device_lookup_name, /* lookup_name */
3714 directory_link_name, /* link_name */
3715 default_unlink_name, /* unlink_name */
3716 socket_device_open_file, /* open_file */
3717 no_kernel_obj_list, /* get_kernel_obj_list */
3718 no_close_handle, /* close_handle */
3719 no_destroy /* destroy */
3722 static void socket_device_dump( struct object *obj, int verbose )
3724 fputs( "Socket device\n", stderr );
3727 static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
3728 unsigned int attr, struct object *root )
3730 if (name) name->len = 0;
3731 return NULL;
3734 static struct object *socket_device_open_file( struct object *obj, unsigned int access,
3735 unsigned int sharing, unsigned int options )
3737 struct sock *sock;
3739 if (!(sock = create_socket())) return NULL;
3740 if (!(sock->fd = alloc_pseudo_fd( &sock_fd_ops, &sock->obj, options )))
3742 release_object( sock );
3743 return NULL;
3745 return &sock->obj;
3748 struct object *create_socket_device( struct object *root, const struct unicode_str *name,
3749 unsigned int attr, const struct security_descriptor *sd )
3751 return create_named_object( root, &socket_device_ops, name, attr, sd );
3754 DECL_HANDLER(recv_socket)
3756 struct sock *sock = (struct sock *)get_handle_obj( current->process, req->async.handle, 0, &sock_ops );
3757 unsigned int status = STATUS_PENDING;
3758 timeout_t timeout = 0;
3759 struct async *async;
3760 struct fd *fd;
3762 if (!sock) return;
3763 fd = sock->fd;
3765 if (!req->force_async && !sock->nonblocking && is_fd_overlapped( fd ))
3766 timeout = (timeout_t)sock->rcvtimeo * -10000;
3768 if (sock->rd_shutdown) status = STATUS_PIPE_DISCONNECTED;
3769 else if (!async_queued( &sock->read_q ))
3771 /* If read_q is not empty, we cannot really tell if the already queued
3772 * asyncs will not consume all available data; if there's no data
3773 * available, the current request won't be immediately satiable.
3775 if ((!req->force_async && sock->nonblocking) ||
3776 check_fd_events( sock->fd, req->oob && !is_oobinline( sock ) ? POLLPRI : POLLIN ))
3778 /* Give the client opportunity to complete synchronously.
3779 * If it turns out that the I/O request is not actually immediately satiable,
3780 * the client may then choose to re-queue the async (with STATUS_PENDING).
3782 * Note: If the nonblocking flag is set, we don't poll the socket
3783 * here and always opt for synchronous completion first. This is
3784 * because the application has probably seen POLLIN already from a
3785 * preceding select()/poll() call before it requested to receive
3786 * data.
3788 status = STATUS_ALERTED;
3792 if (status == STATUS_PENDING && !req->force_async && sock->nonblocking)
3793 status = STATUS_DEVICE_NOT_READY;
3795 sock->pending_events &= ~(req->oob ? AFD_POLL_OOB : AFD_POLL_READ);
3796 sock->reported_events &= ~(req->oob ? AFD_POLL_OOB : AFD_POLL_READ);
3798 if ((async = create_request_async( fd, get_fd_comp_flags( fd ), &req->async )))
3800 set_error( status );
3802 if (timeout)
3803 async_set_timeout( async, timeout, STATUS_IO_TIMEOUT );
3805 if (status == STATUS_PENDING || status == STATUS_ALERTED)
3806 queue_async( &sock->read_q, async );
3808 /* always reselect; we changed reported_events above */
3809 sock_reselect( sock );
3811 reply->wait = async_handoff( async, NULL, 0 );
3812 reply->options = get_fd_options( fd );
3813 reply->nonblocking = sock->nonblocking;
3814 release_object( async );
3816 release_object( sock );
3819 static void send_socket_completion_callback( void *private )
3821 struct send_req *send_req = private;
3822 struct iosb *iosb = send_req->iosb;
3823 struct sock *sock = send_req->sock;
3825 if (iosb->status != STATUS_SUCCESS)
3827 /* send() calls only clear and reselect events if unsuccessful. */
3828 sock->pending_events &= ~AFD_POLL_WRITE;
3829 sock->reported_events &= ~AFD_POLL_WRITE;
3830 sock_reselect( sock );
3833 release_object( iosb );
3834 release_object( sock );
3835 free( send_req );
3838 DECL_HANDLER(send_socket)
3840 struct sock *sock = (struct sock *)get_handle_obj( current->process, req->async.handle, 0, &sock_ops );
3841 unsigned int status = STATUS_PENDING;
3842 timeout_t timeout = 0;
3843 struct async *async;
3844 struct fd *fd;
3845 int bind_errno = 0;
3847 if (!sock) return;
3848 fd = sock->fd;
3850 if (sock->type == WS_SOCK_DGRAM && !sock->bound)
3852 union unix_sockaddr unix_addr;
3853 socklen_t unix_len;
3854 int unix_fd = get_unix_fd( fd );
3856 unix_len = get_unix_sockaddr_any( &unix_addr, sock->family );
3857 if (bind( unix_fd, &unix_addr.addr, unix_len ) < 0)
3858 bind_errno = errno;
3860 if (getsockname( unix_fd, &unix_addr.addr, &unix_len ) >= 0)
3862 sock->addr_len = sockaddr_from_unix( &unix_addr, &sock->addr.addr, sizeof(sock->addr) );
3863 sock->bound = 1;
3865 else if (!bind_errno) bind_errno = errno;
3868 if (!req->force_async && !sock->nonblocking && is_fd_overlapped( fd ))
3869 timeout = (timeout_t)sock->sndtimeo * -10000;
3871 if (bind_errno) status = sock_get_ntstatus( bind_errno );
3872 else if (sock->wr_shutdown) status = STATUS_PIPE_DISCONNECTED;
3873 else if (!async_queued( &sock->write_q ))
3875 /* If write_q is not empty, we cannot really tell if the already queued
3876 * asyncs will not consume all available space; if there's no space
3877 * available, the current request won't be immediately satiable.
3879 if ((!req->force_async && sock->nonblocking) || check_fd_events( sock->fd, POLLOUT ))
3881 /* Give the client opportunity to complete synchronously.
3882 * If it turns out that the I/O request is not actually immediately satiable,
3883 * the client may then choose to re-queue the async (with STATUS_PENDING).
3885 * Note: If the nonblocking flag is set, we don't poll the socket
3886 * here and always opt for synchronous completion first. This is
3887 * because the application has probably seen POLLOUT already from a
3888 * preceding select()/poll() call before it requested to send data.
3890 * Furthermore, some applications expect that any send() call on a
3891 * socket that has indicated POLLOUT beforehand never fails with
3892 * WSAEWOULDBLOCK. It's possible that Linux poll() may yield
3893 * POLLOUT on the first call but not the second, even if no send()
3894 * call has been made in the meanwhile. This can happen for a
3895 * number of reasons; for example, TCP fragmentation may consume
3896 * extra buffer space for each packet that has been split out, or
3897 * the TCP/IP networking stack may decide to shrink the send buffer
3898 * due to memory pressure.
3900 status = STATUS_ALERTED;
3904 if (status == STATUS_PENDING && !req->force_async && sock->nonblocking)
3905 status = STATUS_DEVICE_NOT_READY;
3907 if ((async = create_request_async( fd, get_fd_comp_flags( fd ), &req->async )))
3909 struct send_req *send_req;
3910 struct iosb *iosb = async_get_iosb( async );
3912 if ((send_req = mem_alloc( sizeof(*send_req) )))
3914 send_req->iosb = (struct iosb *)grab_object( iosb );
3915 send_req->sock = (struct sock *)grab_object( sock );
3916 async_set_completion_callback( async, send_socket_completion_callback, send_req );
3918 else if (status == STATUS_PENDING || status == STATUS_DEVICE_NOT_READY)
3919 status = STATUS_NO_MEMORY;
3921 release_object( iosb );
3923 set_error( status );
3925 if (timeout)
3926 async_set_timeout( async, timeout, STATUS_IO_TIMEOUT );
3928 if (status == STATUS_PENDING || status == STATUS_ALERTED)
3930 queue_async( &sock->write_q, async );
3931 sock_reselect( sock );
3934 reply->wait = async_handoff( async, NULL, 0 );
3935 reply->options = get_fd_options( fd );
3936 reply->nonblocking = sock->nonblocking;
3937 release_object( async );
3939 release_object( sock );
3942 DECL_HANDLER(socket_send_icmp_id)
3944 struct sock *sock = (struct sock *)get_handle_obj( current->process, req->handle, 0, &sock_ops );
3946 if (!sock) return;
3948 if (sock->icmp_fixup_data_len == MAX_ICMP_HISTORY_LENGTH)
3950 memmove( sock->icmp_fixup_data, sock->icmp_fixup_data + 1,
3951 sizeof(*sock->icmp_fixup_data) * (MAX_ICMP_HISTORY_LENGTH - 1) );
3952 --sock->icmp_fixup_data_len;
3955 sock->icmp_fixup_data[sock->icmp_fixup_data_len].icmp_id = req->icmp_id;
3956 sock->icmp_fixup_data[sock->icmp_fixup_data_len].icmp_seq = req->icmp_seq;
3957 ++sock->icmp_fixup_data_len;
3959 release_object( sock );
3962 DECL_HANDLER(socket_get_icmp_id)
3964 struct sock *sock = (struct sock *)get_handle_obj( current->process, req->handle, 0, &sock_ops );
3965 unsigned int i;
3967 if (!sock) return;
3969 for (i = 0; i < sock->icmp_fixup_data_len; ++i)
3971 if (sock->icmp_fixup_data[i].icmp_seq == req->icmp_seq)
3973 reply->icmp_id = sock->icmp_fixup_data[i].icmp_id;
3974 --sock->icmp_fixup_data_len;
3975 memmove( &sock->icmp_fixup_data[i], &sock->icmp_fixup_data[i + 1],
3976 (sock->icmp_fixup_data_len - i) * sizeof(*sock->icmp_fixup_data) );
3977 release_object( sock );
3978 return;
3982 set_error( STATUS_NOT_FOUND );
3983 release_object( sock );