msvcrt: Add platform-specific helpers to call C++ exception handlers.
[wine.git] / server / sock.c
blob8a95caded8b8422abde7d1c39ba3cf516f565369
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 # define HAS_IPX
66 #elif defined(HAVE_LINUX_IPX_H)
67 # ifdef HAVE_ASM_TYPES_H
68 # include <asm/types.h>
69 # endif
70 # ifdef HAVE_LINUX_TYPES_H
71 # include <linux/types.h>
72 # endif
73 # include <linux/ipx.h>
74 # ifdef SOL_IPX
75 # define HAS_IPX
76 # endif
77 #endif
79 #ifdef HAVE_LINUX_IRDA_H
80 # ifdef HAVE_LINUX_TYPES_H
81 # include <linux/types.h>
82 # endif
83 # include <linux/irda.h>
84 # define HAS_IRDA
85 #endif
87 #include "ntstatus.h"
88 #define WIN32_NO_STATUS
89 #include "windef.h"
90 #include "winternl.h"
91 #include "winerror.h"
92 #define USE_WS_PREFIX
93 #include "winsock2.h"
94 #include "ws2tcpip.h"
95 #include "wsipx.h"
96 #include "af_irda.h"
97 #include "wine/afd.h"
98 #include "wine/rbtree.h"
100 #include "process.h"
101 #include "file.h"
102 #include "handle.h"
103 #include "thread.h"
104 #include "request.h"
105 #include "user.h"
107 #if defined(linux) && !defined(IP_UNICAST_IF)
108 #define IP_UNICAST_IF 50
109 #endif
111 static const char magic_loopback_addr[] = {127, 12, 34, 56};
113 union win_sockaddr
115 struct WS_sockaddr addr;
116 struct WS_sockaddr_in in;
117 struct WS_sockaddr_in6 in6;
118 struct WS_sockaddr_ipx ipx;
119 SOCKADDR_IRDA irda;
122 union unix_sockaddr
124 struct sockaddr addr;
125 struct sockaddr_in in;
126 struct sockaddr_in6 in6;
127 #ifdef HAS_IPX
128 struct sockaddr_ipx ipx;
129 #endif
130 #ifdef HAS_IRDA
131 struct sockaddr_irda irda;
132 #endif
135 static struct list poll_list = LIST_INIT( poll_list );
137 struct poll_req
139 struct list entry;
140 struct async *async;
141 struct iosb *iosb;
142 struct timeout_user *timeout;
143 timeout_t orig_timeout;
144 int exclusive;
145 int pending;
146 unsigned int count;
147 struct
149 struct sock *sock;
150 int mask;
151 obj_handle_t handle;
152 int flags;
153 unsigned int status;
154 } sockets[1];
157 struct accept_req
159 struct list entry;
160 struct async *async;
161 struct iosb *iosb;
162 struct sock *sock, *acceptsock;
163 int accepted;
164 unsigned int recv_len, local_len;
167 struct connect_req
169 struct async *async;
170 struct iosb *iosb;
171 struct sock *sock;
172 unsigned int addr_len, send_len, send_cursor;
175 struct send_req
177 struct iosb *iosb;
178 struct sock *sock;
181 enum connection_state
183 SOCK_LISTENING,
184 SOCK_UNCONNECTED,
185 SOCK_CONNECTING,
186 SOCK_CONNECTED,
187 SOCK_CONNECTIONLESS,
190 struct bound_addr
192 struct rb_entry entry;
193 union unix_sockaddr addr;
194 int match_any_addr;
195 int reuse_count;
198 #define MAX_ICMP_HISTORY_LENGTH 8
200 #define MIN_RCVBUF 65536
202 struct sock
204 struct object obj; /* object header */
205 struct fd *fd; /* socket file descriptor */
206 enum connection_state state; /* connection state */
207 unsigned int mask; /* event mask */
208 /* pending AFD_POLL_* events which have not yet been reported to the application */
209 unsigned int pending_events;
210 /* AFD_POLL_* events which have already been reported and should not be
211 * selected for again until reset by a relevant call.
213 * For example, if AFD_POLL_READ is set here and not in pending_events, it
214 * has already been reported and consumed, and we should not report it
215 * again, even if POLLIN is signaled, until it is reset by e.g recv().
217 * If an event has been signaled and not consumed yet, it will be set in
218 * both pending_events and reported_events (as we should only ever report
219 * any event once until it is reset.) */
220 unsigned int reported_events;
221 unsigned short proto; /* socket protocol */
222 unsigned short type; /* socket type */
223 unsigned short family; /* socket family */
224 struct event *event; /* event object */
225 user_handle_t window; /* window to send the message to */
226 unsigned int message; /* message to send */
227 obj_handle_t wparam; /* message wparam (socket handle) */
228 int errors[AFD_POLL_BIT_COUNT]; /* event errors */
229 timeout_t connect_time;/* time the socket was connected */
230 struct sock *deferred; /* socket that waits for a deferred accept */
231 struct async_queue read_q; /* queue for asynchronous reads */
232 struct async_queue write_q; /* queue for asynchronous writes */
233 struct async_queue ifchange_q; /* queue for interface change notifications */
234 struct async_queue accept_q; /* queue for asynchronous accepts */
235 struct async_queue connect_q; /* queue for asynchronous connects */
236 struct async_queue poll_q; /* queue for asynchronous polls */
237 struct object *ifchange_obj; /* the interface change notification object */
238 struct list ifchange_entry; /* entry in ifchange notification list */
239 struct list accept_list; /* list of pending accept requests */
240 struct accept_req *accept_recv_req; /* pending accept-into request which will recv on this socket */
241 struct connect_req *connect_req; /* pending connection request */
242 struct poll_req *main_poll; /* main poll */
243 union win_sockaddr addr; /* socket name */
244 int addr_len; /* socket name length */
245 union win_sockaddr peer_addr; /* peer name */
246 int peer_addr_len; /* peer name length */
247 unsigned int rcvbuf; /* advisory recv buffer size */
248 unsigned int sndbuf; /* advisory send buffer size */
249 unsigned int rcvtimeo; /* receive timeout in ms */
250 unsigned int sndtimeo; /* send timeout in ms */
251 struct
253 unsigned short icmp_id;
254 unsigned short icmp_seq;
256 icmp_fixup_data[MAX_ICMP_HISTORY_LENGTH]; /* Sent ICMP packets history used to fixup reply id. */
257 struct bound_addr *bound_addr[2]; /* Links to the entries in bound addresses tree. */
258 unsigned int icmp_fixup_data_len; /* Sent ICMP packets history length. */
259 unsigned int rd_shutdown : 1; /* is the read end shut down? */
260 unsigned int wr_shutdown : 1; /* is the write end shut down? */
261 unsigned int wr_shutdown_pending : 1; /* is a write shutdown pending? */
262 unsigned int hangup : 1; /* has the read end received a hangup? */
263 unsigned int aborted : 1; /* did we get a POLLERR or irregular POLLHUP? */
264 unsigned int nonblocking : 1; /* is the socket nonblocking? */
265 unsigned int bound : 1; /* is the socket bound? */
266 unsigned int reset : 1; /* did we get a TCP reset? */
267 unsigned int reuseaddr : 1; /* winsock SO_REUSEADDR option value */
268 unsigned int exclusiveaddruse : 1; /* winsock SO_EXCLUSIVEADDRUSE option value */
271 static int is_tcp_socket( struct sock *sock )
273 return sock->type == WS_SOCK_STREAM && (sock->family == WS_AF_INET || sock->family == WS_AF_INET6);
276 static int addr_compare( const void *key, const struct wine_rb_entry *entry )
278 const struct bound_addr *bound_addr = RB_ENTRY_VALUE(entry, struct bound_addr, entry);
279 const struct bound_addr *addr = key;
281 if (addr->addr.addr.sa_family != bound_addr->addr.addr.sa_family)
282 return addr->addr.addr.sa_family < bound_addr->addr.addr.sa_family ? -1 : 1;
284 if (addr->addr.addr.sa_family == AF_INET)
286 if (addr->addr.in.sin_port != bound_addr->addr.in.sin_port)
287 return addr->addr.in.sin_port < bound_addr->addr.in.sin_port ? -1 : 1;
288 if (bound_addr->match_any_addr || addr->match_any_addr
289 || addr->addr.in.sin_addr.s_addr == bound_addr->addr.in.sin_addr.s_addr)
290 return 0;
291 return addr->addr.in.sin_addr.s_addr < bound_addr->addr.in.sin_addr.s_addr ? -1 : 1;
294 assert( addr->addr.addr.sa_family == AF_INET6 );
295 if (addr->addr.in6.sin6_port != bound_addr->addr.in6.sin6_port)
296 return addr->addr.in6.sin6_port < bound_addr->addr.in6.sin6_port ? -1 : 1;
297 if (bound_addr->match_any_addr || addr->match_any_addr) return 0;
298 return memcmp( &addr->addr.in6.sin6_addr, &bound_addr->addr.in6.sin6_addr, sizeof(addr->addr.in6.sin6_addr) );
301 static int ipv4addr_from_v6( union unix_sockaddr *v4addr, const struct sockaddr_in6 *in6, int map_unspecified )
303 v4addr->in.sin_family = AF_INET;
304 v4addr->in.sin_port = in6->sin6_port;
306 if (map_unspecified && IN6_IS_ADDR_UNSPECIFIED(&in6->sin6_addr))
308 v4addr->in.sin_addr.s_addr = htonl( INADDR_ANY );
309 return 1;
311 if (IN6_IS_ADDR_V4COMPAT(&in6->sin6_addr) || IN6_IS_ADDR_V4MAPPED(&in6->sin6_addr))
313 memcpy( &v4addr->in.sin_addr.s_addr, &in6->sin6_addr.s6_addr[12], sizeof(v4addr->in.sin_addr.s_addr) );
314 return 1;
316 return 0;
319 static struct rb_tree bound_addresses_tree = { addr_compare };
321 static int should_track_conflicts_for_addr( struct sock *sock, const union unix_sockaddr *addr )
323 if (!is_tcp_socket( sock )) return 0;
325 if (sock->family == WS_AF_INET && addr->addr.sa_family == AF_INET && addr->in.sin_port)
326 return 1;
327 else if (sock->family == WS_AF_INET6 && addr->addr.sa_family == AF_INET6 && addr->in6.sin6_port)
328 return 1;
330 return 0;
333 static int is_any_addr( const union unix_sockaddr *addr )
335 if (addr->addr.sa_family == AF_INET && addr->in.sin_addr.s_addr == htonl( INADDR_ANY ))
336 return 1;
337 if (addr->addr.sa_family == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(&addr->in6.sin6_addr))
338 return 1;
339 return 0;
342 static int check_addr_usage( struct sock *sock, const union unix_sockaddr *addr, int v6only )
344 struct bound_addr *bound_addr, search_addr;
345 struct rb_entry *entry;
347 if (!should_track_conflicts_for_addr( sock, addr )) return 0;
349 search_addr.addr = *addr;
350 search_addr.match_any_addr = sock->exclusiveaddruse && is_any_addr( addr );
352 if ((entry = rb_get( &bound_addresses_tree, &search_addr )))
354 bound_addr = WINE_RB_ENTRY_VALUE(entry, struct bound_addr, entry);
355 if (bound_addr->reuse_count == -1 || !sock->reuseaddr)
357 set_error( sock->reuseaddr || bound_addr->match_any_addr
358 ? STATUS_ACCESS_DENIED : STATUS_SHARING_VIOLATION );
359 return 1;
363 if (sock->family != WS_AF_INET6 || v6only) return 0;
364 if (!ipv4addr_from_v6( &search_addr.addr, &addr->in6, sock->exclusiveaddruse )) return 0;
366 search_addr.match_any_addr = sock->exclusiveaddruse && is_any_addr( &search_addr.addr );
367 if ((entry = rb_get( &bound_addresses_tree, &search_addr )))
369 bound_addr = WINE_RB_ENTRY_VALUE(entry, struct bound_addr, entry);
370 if (bound_addr->reuse_count == -1 || !sock->reuseaddr)
372 set_error( sock->reuseaddr || bound_addr->match_any_addr
373 ? STATUS_ACCESS_DENIED : STATUS_SHARING_VIOLATION );
374 return 1;
377 return 0;
380 static struct bound_addr *register_bound_address( struct sock *sock, const union unix_sockaddr *addr )
382 struct bound_addr *bound_addr, *temp;
384 if (!(bound_addr = mem_alloc( sizeof(*bound_addr) )))
385 return NULL;
387 bound_addr->addr = *addr;
388 bound_addr->match_any_addr = sock->exclusiveaddruse && is_any_addr( addr );
390 if (rb_put( &bound_addresses_tree, bound_addr, &bound_addr->entry ))
392 temp = bound_addr;
393 bound_addr = WINE_RB_ENTRY_VALUE(rb_get( &bound_addresses_tree, temp ), struct bound_addr, entry);
394 free( temp );
395 if (bound_addr->reuse_count == -1)
397 if (debug_level)
398 fprintf( stderr, "register_bound_address: address being updated is already exclusively bound\n" );
399 return NULL;
401 ++bound_addr->reuse_count;
403 else
405 bound_addr->reuse_count = sock->reuseaddr ? 1 : -1;
407 return bound_addr;
410 static void update_addr_usage( struct sock *sock, const union unix_sockaddr *addr, int v6only )
412 union unix_sockaddr v4addr;
414 assert( !sock->bound_addr[0] && !sock->bound_addr[1] );
416 if (!should_track_conflicts_for_addr( sock, addr )) return;
418 sock->bound_addr[0] = register_bound_address( sock, addr );
420 if (sock->family != WS_AF_INET6 || v6only) return;
422 if (!ipv4addr_from_v6( &v4addr, &addr->in6, sock->exclusiveaddruse )) return;
424 sock->bound_addr[1] = register_bound_address( sock, &v4addr );
427 static void sock_dump( struct object *obj, int verbose );
428 static struct fd *sock_get_fd( struct object *obj );
429 static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
430 static void sock_destroy( struct object *obj );
431 static struct object *sock_get_ifchange( struct sock *sock );
432 static void sock_release_ifchange( struct sock *sock );
434 static int sock_get_poll_events( struct fd *fd );
435 static void sock_poll_event( struct fd *fd, int event );
436 static enum server_fd_type sock_get_fd_type( struct fd *fd );
437 static void sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
438 static void sock_cancel_async( struct fd *fd, struct async *async );
439 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
441 static int accept_into_socket( struct sock *sock, struct sock *acceptsock );
442 static struct sock *accept_socket( struct sock *sock );
443 static int sock_get_ntstatus( int err );
444 static unsigned int sock_get_error( int err );
445 static void poll_socket( struct sock *poll_sock, struct async *async, int exclusive, timeout_t timeout,
446 unsigned int count, const struct afd_poll_socket_64 *sockets );
448 static const struct object_ops sock_ops =
450 sizeof(struct sock), /* size */
451 &file_type, /* type */
452 sock_dump, /* dump */
453 add_queue, /* add_queue */
454 remove_queue, /* remove_queue */
455 default_fd_signaled, /* signaled */
456 no_satisfied, /* satisfied */
457 no_signal, /* signal */
458 sock_get_fd, /* get_fd */
459 default_map_access, /* map_access */
460 default_get_sd, /* get_sd */
461 default_set_sd, /* set_sd */
462 no_get_full_name, /* get_full_name */
463 no_lookup_name, /* lookup_name */
464 no_link_name, /* link_name */
465 NULL, /* unlink_name */
466 no_open_file, /* open_file */
467 no_kernel_obj_list, /* get_kernel_obj_list */
468 sock_close_handle, /* close_handle */
469 sock_destroy /* destroy */
472 static const struct fd_ops sock_fd_ops =
474 sock_get_poll_events, /* get_poll_events */
475 sock_poll_event, /* poll_event */
476 sock_get_fd_type, /* get_fd_type */
477 no_fd_read, /* read */
478 no_fd_write, /* write */
479 no_fd_flush, /* flush */
480 default_fd_get_file_info, /* get_file_info */
481 no_fd_get_volume_info, /* get_volume_info */
482 sock_ioctl, /* ioctl */
483 sock_cancel_async, /* cancel_async */
484 no_fd_queue_async, /* queue_async */
485 sock_reselect_async /* reselect_async */
488 static int sockaddr_from_unix( const union unix_sockaddr *uaddr, struct WS_sockaddr *wsaddr, socklen_t wsaddrlen )
490 memset( wsaddr, 0, wsaddrlen );
492 switch (uaddr->addr.sa_family)
494 case AF_INET:
496 struct WS_sockaddr_in win = {0};
498 if (wsaddrlen < sizeof(win)) return -1;
499 win.sin_family = WS_AF_INET;
500 win.sin_port = uaddr->in.sin_port;
501 memcpy( &win.sin_addr, &uaddr->in.sin_addr, sizeof(win.sin_addr) );
502 memcpy( wsaddr, &win, sizeof(win) );
503 return sizeof(win);
506 case AF_INET6:
508 struct WS_sockaddr_in6 win = {0};
510 if (wsaddrlen < sizeof(win)) return -1;
511 win.sin6_family = WS_AF_INET6;
512 win.sin6_port = uaddr->in6.sin6_port;
513 win.sin6_flowinfo = uaddr->in6.sin6_flowinfo;
514 memcpy( &win.sin6_addr, &uaddr->in6.sin6_addr, sizeof(win.sin6_addr) );
515 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
516 win.sin6_scope_id = uaddr->in6.sin6_scope_id;
517 #endif
518 memcpy( wsaddr, &win, sizeof(win) );
519 return sizeof(win);
522 #ifdef HAS_IPX
523 case AF_IPX:
525 struct WS_sockaddr_ipx win = {0};
527 if (wsaddrlen < sizeof(win)) return -1;
528 win.sa_family = WS_AF_IPX;
529 memcpy( win.sa_netnum, &uaddr->ipx.sipx_network, sizeof(win.sa_netnum) );
530 memcpy( win.sa_nodenum, &uaddr->ipx.sipx_node, sizeof(win.sa_nodenum) );
531 win.sa_socket = uaddr->ipx.sipx_port;
532 memcpy( wsaddr, &win, sizeof(win) );
533 return sizeof(win);
535 #endif
537 #ifdef HAS_IRDA
538 case AF_IRDA:
540 SOCKADDR_IRDA win;
542 if (wsaddrlen < sizeof(win)) return -1;
543 win.irdaAddressFamily = WS_AF_IRDA;
544 memcpy( win.irdaDeviceID, &uaddr->irda.sir_addr, sizeof(win.irdaDeviceID) );
545 if (uaddr->irda.sir_lsap_sel != LSAP_ANY)
546 snprintf( win.irdaServiceName, sizeof(win.irdaServiceName), "LSAP-SEL%u", uaddr->irda.sir_lsap_sel );
547 else
548 memcpy( win.irdaServiceName, uaddr->irda.sir_name, sizeof(win.irdaServiceName) );
549 memcpy( wsaddr, &win, sizeof(win) );
550 return sizeof(win);
552 #endif
554 case AF_UNSPEC:
555 return 0;
557 default:
558 return -1;
563 static socklen_t sockaddr_to_unix( const struct WS_sockaddr *wsaddr, int wsaddrlen, union unix_sockaddr *uaddr )
565 memset( uaddr, 0, sizeof(*uaddr) );
567 switch (wsaddr->sa_family)
569 case WS_AF_INET:
571 struct WS_sockaddr_in win = {0};
573 if (wsaddrlen < sizeof(win)) return 0;
574 memcpy( &win, wsaddr, sizeof(win) );
575 uaddr->in.sin_family = AF_INET;
576 uaddr->in.sin_port = win.sin_port;
577 memcpy( &uaddr->in.sin_addr, &win.sin_addr, sizeof(win.sin_addr) );
578 return sizeof(uaddr->in);
581 case WS_AF_INET6:
583 struct WS_sockaddr_in6 win = {0};
585 if (wsaddrlen < sizeof(win)) return 0;
586 memcpy( &win, wsaddr, sizeof(win) );
587 uaddr->in6.sin6_family = AF_INET6;
588 uaddr->in6.sin6_port = win.sin6_port;
589 uaddr->in6.sin6_flowinfo = win.sin6_flowinfo;
590 memcpy( &uaddr->in6.sin6_addr, &win.sin6_addr, sizeof(win.sin6_addr) );
591 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
592 uaddr->in6.sin6_scope_id = win.sin6_scope_id;
593 #endif
594 return sizeof(uaddr->in6);
597 #ifdef HAS_IPX
598 case WS_AF_IPX:
600 struct WS_sockaddr_ipx win = {0};
602 if (wsaddrlen < sizeof(win)) return 0;
603 memcpy( &win, wsaddr, sizeof(win) );
604 uaddr->ipx.sipx_family = AF_IPX;
605 memcpy( &uaddr->ipx.sipx_network, win.sa_netnum, sizeof(win.sa_netnum) );
606 memcpy( &uaddr->ipx.sipx_node, win.sa_nodenum, sizeof(win.sa_nodenum) );
607 uaddr->ipx.sipx_port = win.sa_socket;
608 return sizeof(uaddr->ipx);
610 #endif
612 #ifdef HAS_IRDA
613 case WS_AF_IRDA:
615 SOCKADDR_IRDA win = {0};
616 unsigned int lsap_sel;
618 if (wsaddrlen < sizeof(win)) return 0;
619 memcpy( &win, wsaddr, sizeof(win) );
620 uaddr->irda.sir_family = AF_IRDA;
621 if (sscanf( win.irdaServiceName, "LSAP-SEL%u", &lsap_sel ) == 1)
622 uaddr->irda.sir_lsap_sel = lsap_sel;
623 else
625 uaddr->irda.sir_lsap_sel = LSAP_ANY;
626 memcpy( uaddr->irda.sir_name, win.irdaServiceName, sizeof(win.irdaServiceName) );
628 memcpy( &uaddr->irda.sir_addr, win.irdaDeviceID, sizeof(win.irdaDeviceID) );
629 return sizeof(uaddr->irda);
631 #endif
633 case WS_AF_UNSPEC:
634 switch (wsaddrlen)
636 default: /* likely an ipv4 address */
637 case sizeof(struct WS_sockaddr_in):
638 return sizeof(uaddr->in);
640 #ifdef HAS_IPX
641 case sizeof(struct WS_sockaddr_ipx):
642 return sizeof(uaddr->ipx);
643 #endif
645 #ifdef HAS_IRDA
646 case sizeof(SOCKADDR_IRDA):
647 return sizeof(uaddr->irda);
648 #endif
650 case sizeof(struct WS_sockaddr_in6):
651 return sizeof(uaddr->in6);
654 default:
655 return 0;
659 static socklen_t get_unix_sockaddr_any( union unix_sockaddr *uaddr, int ws_family )
661 memset( uaddr, 0, sizeof(*uaddr) );
662 switch (ws_family)
664 case WS_AF_INET:
665 uaddr->in.sin_family = AF_INET;
666 return sizeof(uaddr->in);
667 case WS_AF_INET6:
668 uaddr->in6.sin6_family = AF_INET6;
669 return sizeof(uaddr->in6);
670 #ifdef HAS_IPX
671 case WS_AF_IPX:
672 uaddr->ipx.sipx_family = AF_IPX;
673 return sizeof(uaddr->ipx);
674 #endif
675 #ifdef HAS_IRDA
676 case WS_AF_IRDA:
677 uaddr->irda.sir_family = AF_IRDA;
678 return sizeof(uaddr->irda);
679 #endif
680 default:
681 return 0;
685 /* some events are generated at the same time but must be sent in a particular
686 * order (e.g. CONNECT must be sent before READ) */
687 static const enum afd_poll_bit event_bitorder[] =
689 AFD_POLL_BIT_CONNECT,
690 AFD_POLL_BIT_CONNECT_ERR,
691 AFD_POLL_BIT_ACCEPT,
692 AFD_POLL_BIT_OOB,
693 AFD_POLL_BIT_READ,
694 AFD_POLL_BIT_WRITE,
695 AFD_POLL_BIT_RESET,
696 AFD_POLL_BIT_HUP,
697 AFD_POLL_BIT_CLOSE,
700 typedef enum {
701 SOCK_SHUTDOWN_ERROR = -1,
702 SOCK_SHUTDOWN_EOF = 0,
703 SOCK_SHUTDOWN_POLLHUP = 1
704 } sock_shutdown_t;
706 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
708 static sock_shutdown_t sock_check_pollhup(void)
710 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
711 int fd[2], n;
712 struct pollfd pfd;
713 char dummy;
715 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
716 if ( shutdown( fd[0], 1 ) ) goto out;
718 pfd.fd = fd[1];
719 pfd.events = POLLIN;
720 pfd.revents = 0;
722 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
723 n = poll( &pfd, 1, 1 );
724 if ( n != 1 ) goto out; /* error or timeout */
725 if ( pfd.revents & POLLHUP )
726 ret = SOCK_SHUTDOWN_POLLHUP;
727 else if ( pfd.revents & POLLIN &&
728 read( fd[1], &dummy, 1 ) == 0 )
729 ret = SOCK_SHUTDOWN_EOF;
731 out:
732 close( fd[0] );
733 close( fd[1] );
734 return ret;
737 void sock_init(void)
739 sock_shutdown_type = sock_check_pollhup();
741 switch ( sock_shutdown_type )
743 case SOCK_SHUTDOWN_EOF:
744 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
745 break;
746 case SOCK_SHUTDOWN_POLLHUP:
747 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
748 break;
749 default:
750 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
751 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
755 static void sock_reselect( struct sock *sock )
757 int ev = sock_get_poll_events( sock->fd );
759 if (debug_level)
760 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
762 set_fd_events( sock->fd, ev );
765 static unsigned int afd_poll_flag_to_win32( unsigned int flags )
767 static const unsigned int map[] =
769 FD_READ, /* READ */
770 FD_OOB, /* OOB */
771 FD_WRITE, /* WRITE */
772 FD_CLOSE, /* HUP */
773 FD_CLOSE, /* RESET */
774 0, /* CLOSE */
775 FD_CONNECT, /* CONNECT */
776 FD_ACCEPT, /* ACCEPT */
777 FD_CONNECT, /* CONNECT_ERR */
780 unsigned int i, ret = 0;
782 for (i = 0; i < ARRAY_SIZE(map); ++i)
784 if (flags & (1 << i)) ret |= map[i];
787 return ret;
790 static void post_sock_messages( struct sock *sock )
792 unsigned int events = sock->pending_events & sock->mask;
793 int i;
795 if (sock->window)
797 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
798 for (i = 0; i < ARRAY_SIZE(event_bitorder); i++)
800 enum afd_poll_bit event = event_bitorder[i];
801 if (events & (1 << event))
803 lparam_t lparam;
804 if (event == AFD_POLL_BIT_RESET)
805 lparam = FD_CLOSE | (WSAECONNABORTED << 16);
806 else
807 lparam = afd_poll_flag_to_win32(1 << event) | (sock_get_error( sock->errors[event] ) << 16);
808 post_message( sock->window, sock->message, sock->wparam, lparam );
811 sock->pending_events = 0;
812 sock_reselect( sock );
816 static inline int sock_error( struct sock *sock, int *poll_event )
818 int error = 0;
819 socklen_t len = sizeof(error);
821 getsockopt( get_unix_fd(sock->fd), SOL_SOCKET, SO_ERROR, (void *)&error, &len);
823 switch (sock->state)
825 case SOCK_UNCONNECTED:
826 break;
828 case SOCK_CONNECTING:
829 if (error)
830 sock->errors[AFD_POLL_BIT_CONNECT_ERR] = error;
831 else
832 error = sock->errors[AFD_POLL_BIT_CONNECT_ERR];
833 break;
835 case SOCK_LISTENING:
836 if (error)
837 sock->errors[AFD_POLL_BIT_ACCEPT] = error;
838 else
839 error = sock->errors[AFD_POLL_BIT_ACCEPT];
840 break;
842 case SOCK_CONNECTIONLESS:
843 if (error == ENETUNREACH || error == EHOSTUNREACH || error == ECONNRESET)
845 if (poll_event) *poll_event &= ~POLLERR;
846 return 0;
848 /* fallthrough */
849 case SOCK_CONNECTED:
850 if (error == ECONNRESET || error == EPIPE)
852 sock->reset = 1;
853 error = 0;
855 else if (error)
856 sock->errors[AFD_POLL_BIT_HUP] = error;
857 else
858 error = sock->errors[AFD_POLL_BIT_HUP];
859 break;
862 return error;
865 static void free_accept_req( void *private )
867 struct accept_req *req = private;
868 list_remove( &req->entry );
869 if (req->acceptsock)
871 req->acceptsock->accept_recv_req = NULL;
872 release_object( req->acceptsock );
874 release_object( req->async );
875 release_object( req->iosb );
876 release_object( req->sock );
877 free( req );
880 static void fill_accept_output( struct accept_req *req )
882 const data_size_t out_size = req->iosb->out_size;
883 struct async *async = req->async;
884 union unix_sockaddr unix_addr;
885 struct WS_sockaddr *win_addr;
886 unsigned int remote_len;
887 socklen_t unix_len;
888 int fd, size = 0;
889 char *out_data;
890 int win_len;
892 if (!(out_data = mem_alloc( out_size )))
894 async_terminate( async, get_error() );
895 return;
898 fd = get_unix_fd( req->acceptsock->fd );
900 if (req->recv_len && (size = recv( fd, out_data, req->recv_len, 0 )) < 0)
902 if (!req->accepted && errno == EWOULDBLOCK)
904 req->accepted = 1;
905 sock_reselect( req->acceptsock );
906 return;
909 async_terminate( async, sock_get_ntstatus( errno ) );
910 free( out_data );
911 return;
914 if (req->local_len)
916 if (req->local_len < sizeof(int))
918 async_terminate( async, STATUS_BUFFER_TOO_SMALL );
919 free( out_data );
920 return;
923 unix_len = sizeof(unix_addr);
924 win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + sizeof(int));
925 if (getsockname( fd, &unix_addr.addr, &unix_len ) < 0 ||
926 (win_len = sockaddr_from_unix( &unix_addr, win_addr, req->local_len - sizeof(int) )) < 0)
928 async_terminate( async, sock_get_ntstatus( errno ) );
929 free( out_data );
930 return;
932 memcpy( out_data + req->recv_len, &win_len, sizeof(int) );
935 unix_len = sizeof(unix_addr);
936 win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + req->local_len + sizeof(int));
937 remote_len = out_size - req->recv_len - req->local_len;
938 if (getpeername( fd, &unix_addr.addr, &unix_len ) < 0 ||
939 (win_len = sockaddr_from_unix( &unix_addr, win_addr, remote_len - sizeof(int) )) < 0)
941 async_terminate( async, sock_get_ntstatus( errno ) );
942 free( out_data );
943 return;
945 memcpy( out_data + req->recv_len + req->local_len, &win_len, sizeof(int) );
947 async_request_complete( req->async, STATUS_SUCCESS, size, out_size, out_data );
950 static void complete_async_accept( struct sock *sock, struct accept_req *req )
952 struct sock *acceptsock = req->acceptsock;
953 struct async *async = req->async;
955 if (debug_level) fprintf( stderr, "completing accept request for socket %p\n", sock );
957 if (acceptsock)
959 if (!accept_into_socket( sock, acceptsock ))
961 async_terminate( async, get_error() );
962 return;
964 fill_accept_output( req );
966 else
968 obj_handle_t handle;
970 if (!(acceptsock = accept_socket( sock )))
972 async_terminate( async, get_error() );
973 return;
975 handle = alloc_handle_no_access_check( async_get_thread( async )->process, &acceptsock->obj,
976 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
977 acceptsock->wparam = handle;
978 sock_reselect( acceptsock );
979 release_object( acceptsock );
980 if (!handle)
982 async_terminate( async, get_error() );
983 return;
986 async_request_complete_alloc( req->async, STATUS_SUCCESS, 0, sizeof(handle), &handle );
990 static void complete_async_accept_recv( struct accept_req *req )
992 if (debug_level) fprintf( stderr, "completing accept recv request for socket %p\n", req->acceptsock );
994 assert( req->recv_len );
996 fill_accept_output( req );
999 static void free_connect_req( void *private )
1001 struct connect_req *req = private;
1003 req->sock->connect_req = NULL;
1004 release_object( req->async );
1005 release_object( req->iosb );
1006 release_object( req->sock );
1007 free( req );
1010 static void complete_async_connect( struct sock *sock )
1012 struct connect_req *req = sock->connect_req;
1013 const char *in_buffer;
1014 size_t len;
1015 int ret;
1017 if (debug_level) fprintf( stderr, "completing connect request for socket %p\n", sock );
1019 if (!req->send_len)
1021 async_terminate( req->async, STATUS_SUCCESS );
1022 return;
1025 in_buffer = (const char *)req->iosb->in_data + sizeof(struct afd_connect_params) + req->addr_len;
1026 len = req->send_len - req->send_cursor;
1028 ret = send( get_unix_fd( sock->fd ), in_buffer + req->send_cursor, len, 0 );
1029 if (ret < 0 && errno != EWOULDBLOCK)
1030 async_terminate( req->async, sock_get_ntstatus( errno ) );
1031 else if (ret == len)
1032 async_request_complete( req->async, STATUS_SUCCESS, req->send_len, 0, NULL );
1033 else
1034 req->send_cursor += ret;
1037 static void free_poll_req( void *private )
1039 struct poll_req *req = private;
1040 unsigned int i;
1042 if (req->timeout) remove_timeout_user( req->timeout );
1044 for (i = 0; i < req->count; ++i)
1045 release_object( req->sockets[i].sock );
1046 release_object( req->async );
1047 release_object( req->iosb );
1048 list_remove( &req->entry );
1049 free( req );
1052 static int is_oobinline( struct sock *sock )
1054 int oobinline;
1055 socklen_t len = sizeof(oobinline);
1056 return !getsockopt( get_unix_fd( sock->fd ), SOL_SOCKET, SO_OOBINLINE, (char *)&oobinline, &len ) && oobinline;
1059 static int get_poll_flags( struct sock *sock, int event )
1061 int flags = 0;
1063 /* A connection-mode socket which has never been connected does not return
1064 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
1065 if (sock->state == SOCK_UNCONNECTED)
1066 event &= ~(POLLOUT | POLLHUP);
1068 if (event & POLLIN)
1070 if (sock->state == SOCK_LISTENING)
1071 flags |= AFD_POLL_ACCEPT;
1072 else
1073 flags |= AFD_POLL_READ;
1075 if (event & POLLPRI)
1076 flags |= is_oobinline( sock ) ? AFD_POLL_READ : AFD_POLL_OOB;
1077 if (event & POLLOUT)
1078 flags |= AFD_POLL_WRITE;
1079 if (sock->state == SOCK_CONNECTED)
1080 flags |= AFD_POLL_CONNECT;
1081 if (event & POLLHUP)
1082 flags |= AFD_POLL_HUP;
1083 if (event & POLLERR)
1084 flags |= AFD_POLL_CONNECT_ERR;
1085 if (sock->reset)
1086 flags |= AFD_POLL_RESET;
1088 return flags;
1091 static void complete_async_poll( struct poll_req *req, unsigned int status )
1093 unsigned int i, signaled_count = 0;
1095 for (i = 0; i < req->count; ++i)
1097 struct sock *sock = req->sockets[i].sock;
1099 if (sock->main_poll == req)
1100 sock->main_poll = NULL;
1103 if (!status)
1105 for (i = 0; i < req->count; ++i)
1107 if (req->sockets[i].flags)
1108 ++signaled_count;
1112 if (is_machine_64bit( async_get_thread( req->async )->process->machine ))
1114 size_t output_size = offsetof( struct afd_poll_params_64, sockets[signaled_count] );
1115 struct afd_poll_params_64 *output;
1117 if (!(output = mem_alloc( output_size )))
1119 async_terminate( req->async, get_error() );
1120 return;
1122 memset( output, 0, output_size );
1123 output->timeout = req->orig_timeout;
1124 output->exclusive = req->exclusive;
1125 for (i = 0; i < req->count; ++i)
1127 if (!req->sockets[i].flags) continue;
1128 output->sockets[output->count].socket = req->sockets[i].handle;
1129 output->sockets[output->count].flags = req->sockets[i].flags;
1130 output->sockets[output->count].status = req->sockets[i].status;
1131 ++output->count;
1133 assert( output->count == signaled_count );
1135 async_request_complete( req->async, status, output_size, output_size, output );
1137 else
1139 size_t output_size = offsetof( struct afd_poll_params_32, sockets[signaled_count] );
1140 struct afd_poll_params_32 *output;
1142 if (!(output = mem_alloc( output_size )))
1144 async_terminate( req->async, get_error() );
1145 return;
1147 memset( output, 0, output_size );
1148 output->timeout = req->orig_timeout;
1149 output->exclusive = req->exclusive;
1150 for (i = 0; i < req->count; ++i)
1152 if (!req->sockets[i].flags) continue;
1153 output->sockets[output->count].socket = req->sockets[i].handle;
1154 output->sockets[output->count].flags = req->sockets[i].flags;
1155 output->sockets[output->count].status = req->sockets[i].status;
1156 ++output->count;
1158 assert( output->count == signaled_count );
1160 async_request_complete( req->async, status, output_size, output_size, output );
1164 static void complete_async_polls( struct sock *sock, int event, int error )
1166 int flags = get_poll_flags( sock, event );
1167 struct poll_req *req, *next;
1169 LIST_FOR_EACH_ENTRY_SAFE( req, next, &poll_list, struct poll_req, entry )
1171 unsigned int i;
1173 if (req->iosb->status != STATUS_PENDING) continue;
1175 for (i = 0; i < req->count; ++i)
1177 if (req->sockets[i].sock != sock) continue;
1178 if (!(req->sockets[i].mask & flags)) continue;
1180 if (debug_level)
1181 fprintf( stderr, "completing poll for socket %p, wanted %#x got %#x\n",
1182 sock, req->sockets[i].mask, flags );
1184 req->sockets[i].flags = req->sockets[i].mask & flags;
1185 req->sockets[i].status = sock_get_ntstatus( error );
1187 if (req->pending)
1189 complete_async_poll( req, STATUS_SUCCESS );
1190 break;
1196 static void async_poll_timeout( void *private )
1198 struct poll_req *req = private;
1200 req->timeout = NULL;
1202 if (req->iosb->status != STATUS_PENDING) return;
1204 complete_async_poll( req, STATUS_TIMEOUT );
1207 static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
1209 if (event & (POLLIN | POLLPRI))
1211 struct accept_req *req;
1213 LIST_FOR_EACH_ENTRY( req, &sock->accept_list, struct accept_req, entry )
1215 if (req->iosb->status == STATUS_PENDING && !req->accepted)
1217 complete_async_accept( sock, req );
1218 event &= ~POLLIN;
1219 break;
1223 if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
1224 complete_async_accept_recv( sock->accept_recv_req );
1227 if ((event & POLLOUT) && sock->connect_req && sock->connect_req->iosb->status == STATUS_PENDING)
1228 complete_async_connect( sock );
1230 if ((event & (POLLIN | POLLPRI)) && async_queued( &sock->read_q ))
1232 if (async_waiting( &sock->read_q ))
1234 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
1235 async_wake_up( &sock->read_q, STATUS_ALERTED );
1237 event &= ~(POLLIN | POLLPRI);
1240 if ((event & POLLOUT) && async_queue_has_waiting_asyncs( &sock->write_q ))
1242 if (async_waiting( &sock->write_q ))
1244 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
1245 async_wake_up( &sock->write_q, STATUS_ALERTED );
1247 event &= ~POLLOUT;
1250 if (event & (POLLERR | POLLHUP))
1252 int status = sock_get_ntstatus( error );
1253 struct accept_req *req, *next;
1255 async_wake_up( &sock->read_q, status );
1256 async_wake_up( &sock->write_q, status );
1258 LIST_FOR_EACH_ENTRY_SAFE( req, next, &sock->accept_list, struct accept_req, entry )
1260 if (req->iosb->status == STATUS_PENDING)
1261 async_terminate( req->async, status );
1264 if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
1265 async_terminate( sock->accept_recv_req->async, status );
1267 if (sock->connect_req)
1268 async_terminate( sock->connect_req->async, status );
1271 if (sock->reset)
1273 async_wake_up( &sock->read_q, STATUS_CONNECTION_RESET );
1274 async_wake_up( &sock->write_q, STATUS_CONNECTION_RESET );
1276 if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
1277 async_terminate( sock->accept_recv_req->async, STATUS_CONNECTION_RESET );
1280 return event;
1283 static void post_socket_event( struct sock *sock, enum afd_poll_bit event_bit )
1285 unsigned int event = (1 << event_bit);
1287 if (!(sock->reported_events & event))
1289 sock->pending_events |= event;
1290 sock->reported_events |= event;
1292 if ((sock->mask & event) && sock->event)
1293 set_event( sock->event );
1297 static void sock_dispatch_events( struct sock *sock, enum connection_state prevstate, int event )
1299 switch (prevstate)
1301 case SOCK_UNCONNECTED:
1302 break;
1304 case SOCK_CONNECTING:
1305 if (event & POLLOUT)
1307 post_socket_event( sock, AFD_POLL_BIT_CONNECT );
1308 post_socket_event( sock, AFD_POLL_BIT_WRITE );
1310 if (event & (POLLERR | POLLHUP))
1311 post_socket_event( sock, AFD_POLL_BIT_CONNECT_ERR );
1312 break;
1314 case SOCK_LISTENING:
1315 if (event & (POLLIN | POLLERR | POLLHUP))
1316 post_socket_event( sock, AFD_POLL_BIT_ACCEPT );
1317 break;
1319 case SOCK_CONNECTED:
1320 case SOCK_CONNECTIONLESS:
1321 if (sock->reset)
1322 post_socket_event( sock, AFD_POLL_BIT_RESET );
1324 if (event & POLLIN)
1325 post_socket_event( sock, AFD_POLL_BIT_READ );
1327 if (event & POLLOUT)
1328 post_socket_event( sock, AFD_POLL_BIT_WRITE );
1330 if (event & POLLPRI)
1331 post_socket_event( sock, AFD_POLL_BIT_OOB );
1333 if (event & (POLLERR | POLLHUP))
1334 post_socket_event( sock, AFD_POLL_BIT_HUP );
1335 break;
1338 post_sock_messages( sock );
1341 static void sock_poll_event( struct fd *fd, int event )
1343 struct sock *sock = get_fd_user( fd );
1344 int hangup_seen = 0;
1345 enum connection_state prevstate = sock->state;
1346 int error = 0;
1348 assert( sock->obj.ops == &sock_ops );
1349 grab_object( sock );
1351 if (debug_level)
1352 fprintf(stderr, "socket %p select event: %x\n", sock, event);
1354 if (event & (POLLERR | POLLHUP))
1355 error = sock_error( sock, &event );
1357 switch (sock->state)
1359 case SOCK_UNCONNECTED:
1360 break;
1362 case SOCK_CONNECTING:
1363 if (event & (POLLERR|POLLHUP))
1365 sock->state = SOCK_UNCONNECTED;
1366 event &= ~POLLOUT;
1368 else if (event & POLLOUT)
1370 sock->state = SOCK_CONNECTED;
1371 sock->connect_time = current_time;
1372 sock->errors[AFD_POLL_BIT_CONNECT_ERR] = 0;
1374 break;
1376 case SOCK_LISTENING:
1377 break;
1379 case SOCK_CONNECTED:
1380 case SOCK_CONNECTIONLESS:
1381 if (sock->reset)
1382 event &= ~(POLLIN | POLLERR | POLLHUP);
1384 if (sock->type == WS_SOCK_STREAM && (event & POLLIN))
1386 char dummy;
1387 int nr;
1389 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
1390 * has been closed, so we need to check for it explicitly here */
1391 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
1392 if ( nr == 0 )
1394 hangup_seen = 1;
1395 event &= ~POLLIN;
1397 else if ( nr < 0 )
1399 event &= ~POLLIN;
1400 /* EAGAIN can happen if an async recv() falls between the server's poll()
1401 call and the invocation of this routine */
1402 if (errno == ECONNRESET || errno == EPIPE)
1404 sock->reset = 1;
1406 else if (errno != EAGAIN)
1408 error = errno;
1409 event |= POLLERR;
1410 sock->errors[AFD_POLL_BIT_HUP] = error;
1411 if ( debug_level )
1412 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
1417 if (hangup_seen || (sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP)))
1419 sock->hangup = 1;
1421 else if (event & (POLLHUP | POLLERR))
1423 sock->aborted = 1;
1425 if (debug_level)
1426 fprintf( stderr, "socket %p aborted by error %d, event %#x\n", sock, error, event );
1429 if (hangup_seen)
1430 event |= POLLHUP;
1431 break;
1434 event = sock_dispatch_asyncs( sock, event, error );
1435 sock_dispatch_events( sock, prevstate, event );
1436 complete_async_polls( sock, event, error );
1438 sock_reselect( sock );
1439 release_object( sock );
1442 static void sock_dump( struct object *obj, int verbose )
1444 struct sock *sock = (struct sock *)obj;
1445 assert( obj->ops == &sock_ops );
1446 fprintf( stderr, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
1447 sock->fd, sock->state,
1448 sock->mask, sock->pending_events, sock->reported_events );
1451 static int poll_flags_from_afd( struct sock *sock, int flags )
1453 int ev = 0;
1455 /* A connection-mode socket which has never been connected does
1456 * not return write or hangup events, but Linux returns
1457 * POLLOUT | POLLHUP. */
1458 if (sock->state == SOCK_UNCONNECTED)
1459 return -1;
1461 if (flags & (AFD_POLL_READ | AFD_POLL_ACCEPT))
1462 ev |= POLLIN;
1463 if ((flags & AFD_POLL_HUP) && sock->type == WS_SOCK_STREAM)
1464 ev |= POLLIN;
1465 if (flags & AFD_POLL_OOB)
1466 ev |= is_oobinline( sock ) ? POLLIN : POLLPRI;
1467 if (flags & AFD_POLL_WRITE)
1468 ev |= POLLOUT;
1470 return ev;
1473 static int sock_get_poll_events( struct fd *fd )
1475 struct sock *sock = get_fd_user( fd );
1476 unsigned int mask = sock->mask & ~sock->reported_events;
1477 struct poll_req *req;
1478 int ev = 0;
1480 assert( sock->obj.ops == &sock_ops );
1482 if (!sock->type) /* not initialized yet */
1483 return -1;
1485 LIST_FOR_EACH_ENTRY( req, &poll_list, struct poll_req, entry )
1487 unsigned int i;
1489 if (req->iosb->status != STATUS_PENDING) continue;
1491 for (i = 0; i < req->count; ++i)
1493 if (req->sockets[i].sock != sock) continue;
1495 ev |= poll_flags_from_afd( sock, req->sockets[i].mask );
1499 switch (sock->state)
1501 case SOCK_UNCONNECTED:
1502 /* A connection-mode Windows socket which has never been connected does
1503 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1504 * need to return -1 here, to prevent the socket from being polled on at
1505 * all. */
1506 return -1;
1508 case SOCK_CONNECTING:
1509 return POLLOUT;
1511 case SOCK_LISTENING:
1512 if (!list_empty( &sock->accept_list ) || (mask & AFD_POLL_ACCEPT))
1513 ev |= POLLIN;
1514 break;
1516 case SOCK_CONNECTED:
1517 case SOCK_CONNECTIONLESS:
1518 if (sock->hangup && sock->wr_shutdown && !sock->wr_shutdown_pending)
1520 /* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
1521 * if both the socket and its peer are SHUT_WR.
1523 * We don't use SHUT_RD, so we can only encounter this in the latter
1524 * case. In that case there can't be any pending read requests (they
1525 * would have already been completed with a length of zero), the
1526 * above condition ensures that we don't have any pending write
1527 * requests, and nothing that can change about the socket state that
1528 * would complete a pending poll request. */
1529 return -1;
1532 if (sock->aborted || sock->reset)
1533 return -1;
1535 if (sock->accept_recv_req)
1537 ev |= POLLIN;
1539 else if (async_queued( &sock->read_q ))
1541 /* Clear POLLIN and POLLPRI if we have an alerted async, even if
1542 * we're polling this socket for READ or OOB. We can't signal the
1543 * poll if the pending async will read all of the data [cf. the
1544 * matching logic in sock_dispatch_asyncs()], but we also don't
1545 * want to spin polling for POLLIN if we're not going to use it. */
1546 if (async_waiting( &sock->read_q ))
1547 ev |= POLLIN | POLLPRI;
1548 else
1549 ev &= ~(POLLIN | POLLPRI);
1551 else
1553 /* Don't ask for POLLIN if we got a hangup. We won't receive more
1554 * data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
1555 if (!sock->hangup)
1557 if (mask & AFD_POLL_READ)
1558 ev |= POLLIN;
1559 if (mask & AFD_POLL_OOB)
1560 ev |= POLLPRI;
1563 /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
1564 if (sock->state == SOCK_CONNECTED && (mask & AFD_POLL_HUP) && !(sock->reported_events & AFD_POLL_READ))
1565 ev |= POLLIN;
1568 if (async_queued( &sock->write_q ))
1570 /* As with read asyncs above, clear POLLOUT if we have an alerted
1571 * async. */
1572 if (async_waiting( &sock->write_q ))
1573 ev |= POLLOUT;
1574 else
1575 ev &= ~POLLOUT;
1577 else if (!sock->wr_shutdown && (mask & AFD_POLL_WRITE))
1579 ev |= POLLOUT;
1582 break;
1585 return ev;
1588 static enum server_fd_type sock_get_fd_type( struct fd *fd )
1590 return FD_TYPE_SOCKET;
1593 static void sock_cancel_async( struct fd *fd, struct async *async )
1595 struct poll_req *req;
1597 LIST_FOR_EACH_ENTRY( req, &poll_list, struct poll_req, entry )
1599 unsigned int i;
1601 if (req->async != async)
1602 continue;
1604 for (i = 0; i < req->count; i++)
1606 struct sock *sock = req->sockets[i].sock;
1608 if (sock->main_poll == req)
1609 sock->main_poll = NULL;
1613 async_terminate( async, STATUS_CANCELLED );
1616 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
1618 struct sock *sock = get_fd_user( fd );
1620 if (sock->wr_shutdown_pending && list_empty( &sock->write_q.queue ))
1622 shutdown( get_unix_fd( sock->fd ), SHUT_WR );
1623 sock->wr_shutdown_pending = 0;
1626 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1627 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1628 * a pseudo-fd. */
1629 if (queue != &sock->ifchange_q && sock->type)
1630 sock_reselect( sock );
1633 static struct fd *sock_get_fd( struct object *obj )
1635 struct sock *sock = (struct sock *)obj;
1636 return (struct fd *)grab_object( sock->fd );
1639 static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
1641 struct sock *sock = (struct sock *)obj;
1643 if (sock->obj.handle_count == 1) /* last handle */
1645 struct accept_req *accept_req, *accept_next;
1646 struct poll_req *poll_req, *poll_next;
1648 if (sock->accept_recv_req)
1649 async_terminate( sock->accept_recv_req->async, STATUS_CANCELLED );
1651 LIST_FOR_EACH_ENTRY_SAFE( accept_req, accept_next, &sock->accept_list, struct accept_req, entry )
1652 async_terminate( accept_req->async, STATUS_CANCELLED );
1654 if (sock->connect_req)
1655 async_terminate( sock->connect_req->async, STATUS_CANCELLED );
1657 LIST_FOR_EACH_ENTRY_SAFE( poll_req, poll_next, &poll_list, struct poll_req, entry )
1659 struct iosb *iosb = poll_req->iosb;
1660 BOOL signaled = FALSE;
1661 unsigned int i;
1663 if (iosb->status != STATUS_PENDING) continue;
1665 for (i = 0; i < poll_req->count; ++i)
1667 if (poll_req->sockets[i].sock == sock)
1669 signaled = TRUE;
1670 poll_req->sockets[i].flags = AFD_POLL_CLOSE;
1671 poll_req->sockets[i].status = 0;
1675 if (signaled) complete_async_poll( poll_req, STATUS_SUCCESS );
1678 return async_close_obj_handle( obj, process, handle );
1681 static void sock_destroy( struct object *obj )
1683 struct sock *sock = (struct sock *)obj;
1684 unsigned int i;
1686 assert( obj->ops == &sock_ops );
1688 /* FIXME: special socket shutdown stuff? */
1690 for (i = 0; i < 2; ++i)
1692 if (sock->bound_addr[i] && --sock->bound_addr[i]->reuse_count <= 0)
1694 rb_remove( &bound_addresses_tree, &sock->bound_addr[i]->entry );
1695 free( sock->bound_addr[i] );
1699 if ( sock->deferred )
1700 release_object( sock->deferred );
1702 async_wake_up( &sock->ifchange_q, STATUS_CANCELLED );
1703 sock_release_ifchange( sock );
1704 free_async_queue( &sock->read_q );
1705 free_async_queue( &sock->write_q );
1706 free_async_queue( &sock->ifchange_q );
1707 free_async_queue( &sock->accept_q );
1708 free_async_queue( &sock->connect_q );
1709 free_async_queue( &sock->poll_q );
1710 if (sock->event) release_object( sock->event );
1711 if (sock->fd) release_object( sock->fd );
1714 static struct sock *create_socket(void)
1716 struct sock *sock;
1718 if (!(sock = alloc_object( &sock_ops ))) return NULL;
1719 sock->fd = NULL;
1720 sock->state = SOCK_UNCONNECTED;
1721 sock->mask = 0;
1722 sock->pending_events = 0;
1723 sock->reported_events = 0;
1724 sock->proto = 0;
1725 sock->type = 0;
1726 sock->family = 0;
1727 sock->event = NULL;
1728 sock->window = 0;
1729 sock->message = 0;
1730 sock->wparam = 0;
1731 sock->connect_time = 0;
1732 sock->deferred = NULL;
1733 sock->ifchange_obj = NULL;
1734 sock->accept_recv_req = NULL;
1735 sock->connect_req = NULL;
1736 sock->main_poll = NULL;
1737 memset( &sock->addr, 0, sizeof(sock->addr) );
1738 sock->addr_len = 0;
1739 memset( &sock->peer_addr, 0, sizeof(sock->peer_addr) );
1740 sock->peer_addr_len = 0;
1741 sock->rd_shutdown = 0;
1742 sock->wr_shutdown = 0;
1743 sock->wr_shutdown_pending = 0;
1744 sock->hangup = 0;
1745 sock->aborted = 0;
1746 sock->nonblocking = 0;
1747 sock->bound = 0;
1748 sock->reset = 0;
1749 sock->reuseaddr = 0;
1750 sock->exclusiveaddruse = 0;
1751 sock->rcvbuf = 0;
1752 sock->sndbuf = 0;
1753 sock->rcvtimeo = 0;
1754 sock->sndtimeo = 0;
1755 sock->icmp_fixup_data_len = 0;
1756 sock->bound_addr[0] = sock->bound_addr[1] = NULL;
1757 init_async_queue( &sock->read_q );
1758 init_async_queue( &sock->write_q );
1759 init_async_queue( &sock->ifchange_q );
1760 init_async_queue( &sock->accept_q );
1761 init_async_queue( &sock->connect_q );
1762 init_async_queue( &sock->poll_q );
1763 memset( sock->errors, 0, sizeof(sock->errors) );
1764 list_init( &sock->accept_list );
1765 return sock;
1768 static int get_unix_family( int family )
1770 switch (family)
1772 case WS_AF_INET: return AF_INET;
1773 case WS_AF_INET6: return AF_INET6;
1774 #ifdef HAS_IPX
1775 case WS_AF_IPX: return AF_IPX;
1776 #endif
1777 #ifdef AF_IRDA
1778 case WS_AF_IRDA: return AF_IRDA;
1779 #endif
1780 case WS_AF_UNSPEC: return AF_UNSPEC;
1781 default: return -1;
1785 static int get_unix_type( int type )
1787 switch (type)
1789 case WS_SOCK_DGRAM: return SOCK_DGRAM;
1790 case WS_SOCK_RAW: return SOCK_RAW;
1791 case WS_SOCK_STREAM: return SOCK_STREAM;
1792 default: return -1;
1796 static int get_unix_protocol( int protocol )
1798 if (protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
1799 return protocol;
1801 switch (protocol)
1803 case WS_IPPROTO_ICMP: return IPPROTO_ICMP;
1804 case WS_IPPROTO_IGMP: return IPPROTO_IGMP;
1805 case WS_IPPROTO_IP: return IPPROTO_IP;
1806 case WS_IPPROTO_IPV4: return IPPROTO_IPIP;
1807 case WS_IPPROTO_IPV6: return IPPROTO_IPV6;
1808 case WS_IPPROTO_RAW: return IPPROTO_RAW;
1809 case WS_IPPROTO_TCP: return IPPROTO_TCP;
1810 case WS_IPPROTO_UDP: return IPPROTO_UDP;
1811 default: return -1;
1815 static void set_dont_fragment( int fd, int level, int value )
1817 int optname;
1819 if (level == IPPROTO_IP)
1821 #ifdef IP_DONTFRAG
1822 optname = IP_DONTFRAG;
1823 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1824 optname = IP_MTU_DISCOVER;
1825 value = value ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
1826 #else
1827 return;
1828 #endif
1830 else
1832 #ifdef IPV6_DONTFRAG
1833 optname = IPV6_DONTFRAG;
1834 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1835 optname = IPV6_MTU_DISCOVER;
1836 value = value ? IPV6_PMTUDISC_DO : IPV6_PMTUDISC_DONT;
1837 #else
1838 return;
1839 #endif
1842 setsockopt( fd, level, optname, &value, sizeof(value) );
1845 static int init_socket( struct sock *sock, int family, int type, int protocol )
1847 unsigned int options = 0;
1848 int sockfd, unix_type, unix_family, unix_protocol, value;
1849 socklen_t len;
1851 unix_family = get_unix_family( family );
1852 unix_type = get_unix_type( type );
1853 unix_protocol = get_unix_protocol( protocol );
1855 if (unix_protocol < 0)
1857 if (type && unix_type < 0)
1858 set_win32_error( WSAESOCKTNOSUPPORT );
1859 else
1860 set_win32_error( WSAEPROTONOSUPPORT );
1861 return -1;
1863 if (unix_family < 0)
1865 if (family >= 0 && unix_type < 0)
1866 set_win32_error( WSAESOCKTNOSUPPORT );
1867 else
1868 set_win32_error( WSAEAFNOSUPPORT );
1869 return -1;
1872 sockfd = socket( unix_family, unix_type, unix_protocol );
1874 #ifdef linux
1875 if (sockfd == -1 && errno == EPERM && unix_family == AF_INET
1876 && unix_type == SOCK_RAW && unix_protocol == IPPROTO_ICMP)
1878 sockfd = socket( unix_family, SOCK_DGRAM, unix_protocol );
1879 if (sockfd != -1)
1881 const int val = 1;
1883 setsockopt( sockfd, IPPROTO_IP, IP_RECVTTL, (const char *)&val, sizeof(val) );
1884 setsockopt( sockfd, IPPROTO_IP, IP_RECVTOS, (const char *)&val, sizeof(val) );
1885 setsockopt( sockfd, IPPROTO_IP, IP_PKTINFO, (const char *)&val, sizeof(val) );
1888 #endif
1890 if (sockfd == -1)
1892 if (errno == EINVAL) set_win32_error( WSAESOCKTNOSUPPORT );
1893 else set_win32_error( sock_get_error( errno ));
1894 return -1;
1896 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
1898 if (family == WS_AF_IPX && protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
1900 #ifdef HAS_IPX
1901 int ipx_type = protocol - WS_NSPROTO_IPX;
1903 #ifdef SOL_IPX
1904 setsockopt( sockfd, SOL_IPX, IPX_TYPE, &ipx_type, sizeof(ipx_type) );
1905 #else
1906 struct ipx val;
1907 /* Should we retrieve val using a getsockopt call and then
1908 * set the modified one? */
1909 val.ipx_pt = ipx_type;
1910 setsockopt( sockfd, 0, SO_DEFAULT_HEADERS, &val, sizeof(val) );
1911 #endif
1912 #endif
1915 if (unix_family == AF_INET || unix_family == AF_INET6)
1917 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1918 if (unix_type == SOCK_DGRAM || unix_type == SOCK_RAW) /* in Linux the global default can be enabled */
1919 set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, FALSE );
1920 else if (unix_type == SOCK_STREAM)
1921 set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, TRUE );
1924 #ifdef IPV6_V6ONLY
1925 if (unix_family == AF_INET6)
1927 static const int enable = 1;
1928 setsockopt( sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &enable, sizeof(enable) );
1930 #endif
1932 len = sizeof(value);
1933 if (!getsockopt( sockfd, SOL_SOCKET, SO_RCVBUF, &value, &len ))
1935 if (value < MIN_RCVBUF)
1937 value = MIN_RCVBUF;
1938 setsockopt( sockfd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value) );
1940 sock->rcvbuf = value;
1943 len = sizeof(value);
1944 if (!getsockopt( sockfd, SOL_SOCKET, SO_SNDBUF, &value, &len ))
1945 sock->sndbuf = value;
1947 sock->state = (type == WS_SOCK_STREAM ? SOCK_UNCONNECTED : SOCK_CONNECTIONLESS);
1948 sock->proto = protocol;
1949 sock->type = type;
1950 sock->family = family;
1952 if (is_tcp_socket( sock ))
1954 value = 1;
1955 setsockopt( sockfd, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value) );
1956 #ifdef TCP_SYNCNT
1957 value = 4;
1958 setsockopt( sockfd, IPPROTO_TCP, TCP_SYNCNT, &value, sizeof(value) );
1959 #endif
1962 if (sock->fd)
1964 options = get_fd_options( sock->fd );
1965 release_object( sock->fd );
1968 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj, options )))
1970 return -1;
1973 /* We can't immediately allow caching for a connection-mode socket, since it
1974 * might be accepted into (changing the underlying fd object.) */
1975 if (sock->type != WS_SOCK_STREAM) allow_fd_caching( sock->fd );
1977 return 0;
1980 /* accepts a socket and inits it */
1981 static int accept_new_fd( struct sock *sock )
1984 /* Try to accept(2). We can't be safe that this an already connected socket
1985 * or that accept() is allowed on it. In those cases we will get -1/errno
1986 * return.
1988 struct sockaddr saddr;
1989 socklen_t slen = sizeof(saddr);
1990 int acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen );
1991 if (acceptfd != -1)
1992 fcntl( acceptfd, F_SETFL, O_NONBLOCK );
1993 else
1994 set_error( sock_get_ntstatus( errno ));
1995 return acceptfd;
1998 /* accept a socket (creates a new fd) */
1999 static struct sock *accept_socket( struct sock *sock )
2001 struct sock *acceptsock;
2002 int acceptfd;
2004 if (get_unix_fd( sock->fd ) == -1) return NULL;
2006 if ( sock->deferred )
2008 acceptsock = sock->deferred;
2009 sock->deferred = NULL;
2011 else
2013 union unix_sockaddr unix_addr;
2014 socklen_t unix_len;
2016 if ((acceptfd = accept_new_fd( sock )) == -1) return NULL;
2017 if (!(acceptsock = create_socket()))
2019 close( acceptfd );
2020 return NULL;
2023 /* newly created socket gets the same properties of the listening socket */
2024 acceptsock->state = SOCK_CONNECTED;
2025 acceptsock->bound = 1;
2026 acceptsock->nonblocking = sock->nonblocking;
2027 acceptsock->mask = sock->mask;
2028 acceptsock->proto = sock->proto;
2029 acceptsock->type = sock->type;
2030 acceptsock->family = sock->family;
2031 acceptsock->window = sock->window;
2032 acceptsock->message = sock->message;
2033 acceptsock->reuseaddr = sock->reuseaddr;
2034 acceptsock->exclusiveaddruse = sock->exclusiveaddruse;
2035 acceptsock->sndbuf = sock->sndbuf;
2036 acceptsock->rcvbuf = sock->rcvbuf;
2037 acceptsock->sndtimeo = sock->sndtimeo;
2038 acceptsock->rcvtimeo = sock->rcvtimeo;
2039 acceptsock->connect_time = current_time;
2041 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
2042 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
2043 get_fd_options( sock->fd ) )))
2045 release_object( acceptsock );
2046 return NULL;
2048 unix_len = sizeof(unix_addr);
2049 if (!getsockname( acceptfd, &unix_addr.addr, &unix_len ))
2051 acceptsock->addr_len = sockaddr_from_unix( &unix_addr, &acceptsock->addr.addr, sizeof(acceptsock->addr) );
2052 if (!getpeername( acceptfd, &unix_addr.addr, &unix_len ))
2053 acceptsock->peer_addr_len = sockaddr_from_unix( &unix_addr,
2054 &acceptsock->peer_addr.addr,
2055 sizeof(acceptsock->peer_addr) );
2059 clear_error();
2060 sock->pending_events &= ~AFD_POLL_ACCEPT;
2061 sock->reported_events &= ~AFD_POLL_ACCEPT;
2062 sock_reselect( sock );
2063 return acceptsock;
2066 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
2068 union unix_sockaddr unix_addr;
2069 socklen_t unix_len;
2070 int acceptfd;
2071 struct fd *newfd;
2073 if (get_unix_fd( sock->fd ) == -1) return FALSE;
2075 if ( sock->deferred )
2077 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
2078 get_fd_options( acceptsock->fd ) );
2079 if ( !newfd )
2080 return FALSE;
2082 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
2084 release_object( sock->deferred );
2085 sock->deferred = NULL;
2087 else
2089 if ((acceptfd = accept_new_fd( sock )) == -1)
2090 return FALSE;
2092 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
2093 get_fd_options( acceptsock->fd ) )))
2094 return FALSE;
2097 acceptsock->state = SOCK_CONNECTED;
2098 acceptsock->bound = 1;
2099 acceptsock->pending_events = 0;
2100 acceptsock->reported_events = 0;
2101 acceptsock->proto = sock->proto;
2102 acceptsock->type = sock->type;
2103 acceptsock->family = sock->family;
2104 acceptsock->wparam = 0;
2105 acceptsock->deferred = NULL;
2106 acceptsock->connect_time = current_time;
2107 fd_copy_completion( acceptsock->fd, newfd );
2108 release_object( acceptsock->fd );
2109 acceptsock->fd = newfd;
2111 unix_len = sizeof(unix_addr);
2112 if (!getsockname( get_unix_fd( newfd ), &unix_addr.addr, &unix_len ))
2114 acceptsock->addr_len = sockaddr_from_unix( &unix_addr, &acceptsock->addr.addr, sizeof(acceptsock->addr) );
2115 if (!getpeername( get_unix_fd( newfd ), &unix_addr.addr, &unix_len ))
2116 acceptsock->peer_addr_len = sockaddr_from_unix( &unix_addr,
2117 &acceptsock->peer_addr.addr,
2118 sizeof(acceptsock->peer_addr) );
2121 clear_error();
2122 sock->pending_events &= ~AFD_POLL_ACCEPT;
2123 sock->reported_events &= ~AFD_POLL_ACCEPT;
2124 sock_reselect( sock );
2126 return TRUE;
2129 #ifdef IP_BOUND_IF
2131 static int bind_to_iface_name( int fd, in_addr_t bind_addr, const char *name )
2133 static const int enable = 1;
2134 unsigned int index;
2136 if (!(index = if_nametoindex( name )))
2137 return -1;
2139 if (setsockopt( fd, IPPROTO_IP, IP_BOUND_IF, &index, sizeof(index) ))
2140 return -1;
2142 return setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable) );
2145 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER) && defined(SO_BINDTODEVICE)
2147 struct interface_filter
2149 struct sock_filter iface_memaddr;
2150 struct sock_filter iface_rule;
2151 struct sock_filter ip_memaddr;
2152 struct sock_filter ip_rule;
2153 struct sock_filter return_keep;
2154 struct sock_filter return_dump;
2156 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
2157 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
2158 /sizeof(struct sock_filter)
2159 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
2160 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
2161 /sizeof(struct sock_filter)
2162 # define FILTER_JUMP_NEXT() (u_char)(0)
2163 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
2164 static struct interface_filter generic_interface_filter =
2166 /* This filter rule allows incoming packets on the specified interface, which works for all
2167 * remotely generated packets and for locally generated broadcast packets. */
2168 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, SKF_AD_OFF+SKF_AD_IFINDEX),
2169 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule), FILTER_JUMP_NEXT()),
2170 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
2171 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
2172 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, SKF_NET_OFF+SKF_NET_DESTIP),
2173 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule), FILTER_JUMP_DUMP(ip_rule)),
2174 BPF_STMT(BPF_RET+BPF_K, (u_int)-1), /* keep packet */
2175 BPF_STMT(BPF_RET+BPF_K, 0) /* dump packet */
2178 static int bind_to_iface_name( int fd, in_addr_t bind_addr, const char *name )
2180 struct interface_filter specific_interface_filter;
2181 struct sock_fprog filter_prog;
2182 static const int enable = 1;
2183 unsigned int index;
2184 in_addr_t ifindex;
2186 if (!setsockopt( fd, SOL_SOCKET, SO_BINDTODEVICE, name, strlen( name ) + 1 ))
2187 return 0;
2189 /* SO_BINDTODEVICE requires NET_CAP_RAW until Linux 5.7. */
2190 if (debug_level)
2191 fprintf( stderr, "setsockopt SO_BINDTODEVICE fd %d, name %s failed: %s, falling back to SO_REUSE_ADDR\n",
2192 fd, name, strerror( errno ));
2194 if (!(index = if_nametoindex( name )))
2195 return -1;
2197 ifindex = htonl( index );
2198 if (setsockopt( fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex, sizeof(ifindex) ) < 0)
2199 return -1;
2201 specific_interface_filter = generic_interface_filter;
2202 specific_interface_filter.iface_rule.k = index;
2203 specific_interface_filter.ip_rule.k = htonl( bind_addr );
2204 filter_prog.len = sizeof(generic_interface_filter) / sizeof(struct sock_filter);
2205 filter_prog.filter = (struct sock_filter *)&specific_interface_filter;
2206 if (setsockopt( fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, sizeof(filter_prog) ))
2207 return -1;
2209 return setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable) );
2212 #else
2214 static int bind_to_iface_name( int fd, in_addr_t bind_addr, const char *name )
2216 errno = EOPNOTSUPP;
2217 return -1;
2220 #endif /* LINUX_BOUND_IF */
2222 /* Take bind() calls on any name corresponding to a local network adapter and
2223 * restrict the given socket to operating only on the specified interface. This
2224 * restriction consists of two components:
2225 * 1) An outgoing packet restriction suggesting the egress interface for all
2226 * packets.
2227 * 2) An incoming packet restriction dropping packets not meant for the
2228 * interface.
2229 * If the function succeeds in placing these restrictions, then the name for the
2230 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
2231 * receipt of broadcast packets on the socket. This behavior is only relevant to
2232 * UDP sockets and is needed for applications that expect to be able to receive
2233 * broadcast packets on a socket that is bound to a specific network interface.
2235 static int bind_to_interface( struct sock *sock, const struct sockaddr_in *addr )
2237 in_addr_t bind_addr = addr->sin_addr.s_addr;
2238 struct ifaddrs *ifaddrs, *ifaddr;
2239 int fd = get_unix_fd( sock->fd );
2240 int err = -1;
2242 if (bind_addr == htonl( INADDR_ANY ) || bind_addr == htonl( INADDR_LOOPBACK ))
2243 return 0;
2244 if (sock->type != WS_SOCK_DGRAM)
2245 return 0;
2247 if (getifaddrs( &ifaddrs ) < 0) return 0;
2249 for (ifaddr = ifaddrs; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2251 if (ifaddr->ifa_addr && ifaddr->ifa_addr->sa_family == AF_INET
2252 && ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr == bind_addr)
2254 if ((err = bind_to_iface_name( fd, bind_addr, ifaddr->ifa_name )) < 0)
2256 if (debug_level)
2257 fprintf( stderr, "failed to bind to interface: %s\n", strerror( errno ) );
2259 break;
2262 freeifaddrs( ifaddrs );
2263 return !err;
2266 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2267 static unsigned int get_ipv6_interface_index( const struct in6_addr *addr )
2269 struct ifaddrs *ifaddrs, *ifaddr;
2271 if (getifaddrs( &ifaddrs ) < 0) return 0;
2273 for (ifaddr = ifaddrs; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2275 if (ifaddr->ifa_addr && ifaddr->ifa_addr->sa_family == AF_INET6
2276 && !memcmp( &((struct sockaddr_in6 *)ifaddr->ifa_addr)->sin6_addr, addr, sizeof(*addr) ))
2278 unsigned int index = if_nametoindex( ifaddr->ifa_name );
2280 if (!index)
2282 if (debug_level)
2283 fprintf( stderr, "Unable to look up interface index for %s: %s\n",
2284 ifaddr->ifa_name, strerror( errno ) );
2285 continue;
2288 freeifaddrs( ifaddrs );
2289 return index;
2293 freeifaddrs( ifaddrs );
2294 return 0;
2296 #endif
2298 /* return an errno value mapped to a WSA error */
2299 static unsigned int sock_get_error( int err )
2301 switch (err)
2303 case EINTR: return WSAEINTR;
2304 case EBADF: return WSAEBADF;
2305 case EPERM:
2306 case EACCES: return WSAEACCES;
2307 case EFAULT: return WSAEFAULT;
2308 case EINVAL: return WSAEINVAL;
2309 case EMFILE: return WSAEMFILE;
2310 case EINPROGRESS:
2311 case EWOULDBLOCK: return WSAEWOULDBLOCK;
2312 case EALREADY: return WSAEALREADY;
2313 case ENOTSOCK: return WSAENOTSOCK;
2314 case EDESTADDRREQ: return WSAEDESTADDRREQ;
2315 case EMSGSIZE: return WSAEMSGSIZE;
2316 case EPROTOTYPE: return WSAEPROTOTYPE;
2317 case ENOPROTOOPT: return WSAENOPROTOOPT;
2318 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
2319 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
2320 case EOPNOTSUPP: return WSAEOPNOTSUPP;
2321 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
2322 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
2323 case EADDRINUSE: return WSAEADDRINUSE;
2324 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
2325 case ENETDOWN: return WSAENETDOWN;
2326 case ENETUNREACH: return WSAENETUNREACH;
2327 case ENETRESET: return WSAENETRESET;
2328 case ECONNABORTED: return WSAECONNABORTED;
2329 case EPIPE:
2330 case ECONNRESET: return WSAECONNRESET;
2331 case ENOBUFS: return WSAENOBUFS;
2332 case EISCONN: return WSAEISCONN;
2333 case ENOTCONN: return WSAENOTCONN;
2334 case ESHUTDOWN: return WSAESHUTDOWN;
2335 case ETOOMANYREFS: return WSAETOOMANYREFS;
2336 case ETIMEDOUT: return WSAETIMEDOUT;
2337 case ECONNREFUSED: return WSAECONNREFUSED;
2338 case ELOOP: return WSAELOOP;
2339 case ENAMETOOLONG: return WSAENAMETOOLONG;
2340 case EHOSTDOWN: return WSAEHOSTDOWN;
2341 case EHOSTUNREACH: return WSAEHOSTUNREACH;
2342 case ENOTEMPTY: return WSAENOTEMPTY;
2343 #ifdef EPROCLIM
2344 case EPROCLIM: return WSAEPROCLIM;
2345 #endif
2346 #ifdef EUSERS
2347 case EUSERS: return WSAEUSERS;
2348 #endif
2349 #ifdef EDQUOT
2350 case EDQUOT: return WSAEDQUOT;
2351 #endif
2352 #ifdef ESTALE
2353 case ESTALE: return WSAESTALE;
2354 #endif
2355 #ifdef EREMOTE
2356 case EREMOTE: return WSAEREMOTE;
2357 #endif
2359 case 0: return 0;
2360 default:
2361 errno = err;
2362 perror("wineserver: sock_get_error() can't map error");
2363 return WSAEFAULT;
2367 static int sock_get_ntstatus( int err )
2369 switch ( err )
2371 case EBADF: return STATUS_INVALID_HANDLE;
2372 case EBUSY: return STATUS_DEVICE_BUSY;
2373 case EPERM:
2374 case EACCES: return STATUS_ACCESS_DENIED;
2375 case EFAULT: return STATUS_ACCESS_VIOLATION;
2376 case EINVAL: return STATUS_INVALID_PARAMETER;
2377 case ENFILE:
2378 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
2379 case EINPROGRESS:
2380 case EWOULDBLOCK: return STATUS_DEVICE_NOT_READY;
2381 case EALREADY: return STATUS_NETWORK_BUSY;
2382 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
2383 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
2384 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
2385 case EPROTONOSUPPORT:
2386 case ESOCKTNOSUPPORT:
2387 case EPFNOSUPPORT:
2388 case EAFNOSUPPORT:
2389 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
2390 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
2391 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
2392 case EADDRINUSE: return STATUS_SHARING_VIOLATION;
2393 /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
2394 * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
2395 case ENODEV:
2396 case EADDRNOTAVAIL: return STATUS_INVALID_ADDRESS_COMPONENT;
2397 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
2398 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
2399 case ENOTCONN: return STATUS_INVALID_CONNECTION;
2400 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
2401 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
2402 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
2403 case ENETDOWN: return STATUS_NETWORK_BUSY;
2404 case EPIPE:
2405 case ECONNRESET: return STATUS_CONNECTION_RESET;
2406 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
2407 case EISCONN: return STATUS_CONNECTION_ACTIVE;
2409 case 0: return STATUS_SUCCESS;
2410 default:
2411 errno = err;
2412 perror("wineserver: sock_get_ntstatus() can't map error");
2413 return STATUS_UNSUCCESSFUL;
2417 static struct accept_req *alloc_accept_req( struct sock *sock, struct sock *acceptsock, struct async *async,
2418 const struct afd_accept_into_params *params )
2420 struct accept_req *req = mem_alloc( sizeof(*req) );
2422 if (req)
2424 req->async = (struct async *)grab_object( async );
2425 req->iosb = async_get_iosb( async );
2426 req->sock = (struct sock *)grab_object( sock );
2427 req->acceptsock = acceptsock;
2428 if (acceptsock) grab_object( acceptsock );
2429 req->accepted = 0;
2430 req->recv_len = 0;
2431 req->local_len = 0;
2432 if (params)
2434 req->recv_len = params->recv_len;
2435 req->local_len = params->local_len;
2438 return req;
2441 static void sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2443 struct sock *sock = get_fd_user( fd );
2444 int unix_fd = -1;
2446 assert( sock->obj.ops == &sock_ops );
2448 if (code != IOCTL_AFD_WINE_CREATE && code != IOCTL_AFD_POLL && (unix_fd = get_unix_fd( fd )) < 0)
2449 return;
2451 switch(code)
2453 case IOCTL_AFD_WINE_CREATE:
2455 const struct afd_create_params *params = get_req_data();
2457 if (get_req_data_size() != sizeof(*params))
2459 set_error( STATUS_INVALID_PARAMETER );
2460 return;
2462 init_socket( sock, params->family, params->type, params->protocol );
2463 return;
2466 case IOCTL_AFD_WINE_ACCEPT:
2468 struct sock *acceptsock;
2469 obj_handle_t handle;
2471 if (get_reply_max_size() != sizeof(handle))
2473 set_error( STATUS_BUFFER_TOO_SMALL );
2474 return;
2477 if (!(acceptsock = accept_socket( sock )))
2479 struct accept_req *req;
2481 if (sock->nonblocking) return;
2482 if (get_error() != STATUS_DEVICE_NOT_READY) return;
2484 if (!(req = alloc_accept_req( sock, NULL, async, NULL ))) return;
2485 list_add_tail( &sock->accept_list, &req->entry );
2487 async_set_completion_callback( async, free_accept_req, req );
2488 queue_async( &sock->accept_q, async );
2489 sock_reselect( sock );
2490 set_error( STATUS_PENDING );
2491 return;
2493 handle = alloc_handle( current->process, &acceptsock->obj,
2494 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
2495 acceptsock->wparam = handle;
2496 sock_reselect( acceptsock );
2497 release_object( acceptsock );
2498 set_reply_data( &handle, sizeof(handle) );
2499 return;
2502 case IOCTL_AFD_WINE_ACCEPT_INTO:
2504 static const int access = FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | FILE_READ_DATA;
2505 const struct afd_accept_into_params *params = get_req_data();
2506 struct sock *acceptsock;
2507 unsigned int remote_len;
2508 struct accept_req *req;
2510 if (get_req_data_size() != sizeof(*params) ||
2511 get_reply_max_size() < params->recv_len ||
2512 get_reply_max_size() - params->recv_len < params->local_len)
2514 set_error( STATUS_BUFFER_TOO_SMALL );
2515 return;
2518 remote_len = get_reply_max_size() - params->recv_len - params->local_len;
2519 if (remote_len < sizeof(int))
2521 set_error( STATUS_INVALID_PARAMETER );
2522 return;
2525 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, params->accept_handle, access, &sock_ops )))
2526 return;
2528 if (acceptsock->accept_recv_req)
2530 release_object( acceptsock );
2531 set_error( STATUS_INVALID_PARAMETER );
2532 return;
2535 if (!(req = alloc_accept_req( sock, acceptsock, async, params )))
2537 release_object( acceptsock );
2538 return;
2540 list_add_tail( &sock->accept_list, &req->entry );
2541 acceptsock->accept_recv_req = req;
2542 release_object( acceptsock );
2544 acceptsock->wparam = params->accept_handle;
2545 async_set_completion_callback( async, free_accept_req, req );
2546 queue_async( &sock->accept_q, async );
2547 sock_reselect( sock );
2548 set_error( STATUS_PENDING );
2549 return;
2552 case IOCTL_AFD_LISTEN:
2554 const struct afd_listen_params *params = get_req_data();
2556 if (get_req_data_size() < sizeof(*params))
2558 set_error( STATUS_INVALID_PARAMETER );
2559 return;
2562 if (sock->type == WS_SOCK_DGRAM)
2564 set_error( STATUS_NOT_SUPPORTED );
2565 return;
2568 if (!sock->bound)
2570 set_error( STATUS_INVALID_PARAMETER );
2571 return;
2574 if (listen( unix_fd, params->backlog ) < 0)
2576 set_error( sock_get_ntstatus( errno ) );
2577 return;
2580 sock->state = SOCK_LISTENING;
2582 /* a listening socket can no longer be accepted into */
2583 allow_fd_caching( sock->fd );
2585 /* we may already be selecting for AFD_POLL_ACCEPT */
2586 sock_reselect( sock );
2587 return;
2590 case IOCTL_AFD_WINE_CONNECT:
2592 const struct afd_connect_params *params = get_req_data();
2593 const struct WS_sockaddr *addr;
2594 union unix_sockaddr unix_addr, peer_addr;
2595 struct connect_req *req;
2596 socklen_t unix_len;
2597 int send_len, ret;
2599 if (get_req_data_size() < sizeof(*params) ||
2600 get_req_data_size() - sizeof(*params) < params->addr_len)
2602 set_error( STATUS_BUFFER_TOO_SMALL );
2603 return;
2605 send_len = get_req_data_size() - sizeof(*params) - params->addr_len;
2606 addr = (const struct WS_sockaddr *)(params + 1);
2608 if (!params->synchronous && !sock->bound)
2610 set_error( STATUS_INVALID_PARAMETER );
2611 return;
2614 if (sock->accept_recv_req)
2616 set_error( STATUS_INVALID_PARAMETER );
2617 return;
2620 if (sock->connect_req)
2622 set_error( STATUS_INVALID_PARAMETER );
2623 return;
2626 switch (sock->state)
2628 case SOCK_LISTENING:
2629 set_error( STATUS_INVALID_PARAMETER );
2630 return;
2632 case SOCK_CONNECTING:
2633 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2634 * but there's no status code that maps to WSAEALREADY... */
2635 set_error( params->synchronous ? STATUS_ADDRESS_ALREADY_ASSOCIATED : STATUS_INVALID_PARAMETER );
2636 return;
2638 case SOCK_CONNECTED:
2639 set_error( STATUS_CONNECTION_ACTIVE );
2640 return;
2642 case SOCK_UNCONNECTED:
2643 case SOCK_CONNECTIONLESS:
2644 break;
2647 unix_len = sockaddr_to_unix( addr, params->addr_len, &unix_addr );
2648 if (!unix_len)
2650 set_error( STATUS_INVALID_ADDRESS );
2651 return;
2653 if (unix_addr.addr.sa_family == AF_INET && !memcmp( &unix_addr.in.sin_addr, magic_loopback_addr, 4 ))
2654 unix_addr.in.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
2656 memcpy( &peer_addr, &unix_addr, sizeof(unix_addr) );
2657 ret = connect( unix_fd, &unix_addr.addr, unix_len );
2658 if (ret < 0 && errno == ECONNABORTED)
2660 /* On Linux with nonblocking socket if the previous connect() failed for any reason (including
2661 * timeout), next connect will fail. If the error code was queried by getsockopt( SO_ERROR )
2662 * the error code returned now is ECONNABORTED (otherwise that is the actual connect() failure
2663 * error code). If we got here after previous connect attempt on the socket that means
2664 * we already queried SO_ERROR in sock_error(), so retrying on ECONNABORTED only is
2665 * sufficient. */
2666 ret = connect( unix_fd, &unix_addr.addr, unix_len );
2669 if (ret < 0 && errno != EINPROGRESS)
2671 set_error( sock_get_ntstatus( errno ) );
2672 return;
2675 /* a connected or connecting socket can no longer be accepted into */
2676 allow_fd_caching( sock->fd );
2678 unix_len = sizeof(unix_addr);
2679 getsockname( unix_fd, &unix_addr.addr, &unix_len );
2680 sock->addr_len = sockaddr_from_unix( &unix_addr, &sock->addr.addr, sizeof(sock->addr) );
2681 sock->peer_addr_len = sockaddr_from_unix( &peer_addr, &sock->peer_addr.addr, sizeof(sock->peer_addr));
2683 sock->bound = 1;
2685 if (!ret)
2687 if (sock->type != WS_SOCK_DGRAM)
2689 sock->state = SOCK_CONNECTED;
2690 sock->connect_time = current_time;
2693 if (!send_len) return;
2696 if (sock->type != WS_SOCK_DGRAM)
2697 sock->state = SOCK_CONNECTING;
2699 if (params->synchronous && sock->nonblocking)
2701 sock_reselect( sock );
2702 set_error( STATUS_DEVICE_NOT_READY );
2703 return;
2706 if (!(req = mem_alloc( sizeof(*req) )))
2707 return;
2709 req->async = (struct async *)grab_object( async );
2710 req->iosb = async_get_iosb( async );
2711 req->sock = (struct sock *)grab_object( sock );
2712 req->addr_len = params->addr_len;
2713 req->send_len = send_len;
2714 req->send_cursor = 0;
2716 async_set_completion_callback( async, free_connect_req, req );
2717 sock->connect_req = req;
2718 queue_async( &sock->connect_q, async );
2719 sock_reselect( sock );
2720 set_error( STATUS_PENDING );
2721 return;
2724 case IOCTL_AFD_WINE_SHUTDOWN:
2726 unsigned int how;
2728 if (get_req_data_size() < sizeof(int))
2730 set_error( STATUS_BUFFER_TOO_SMALL );
2731 return;
2733 how = *(int *)get_req_data();
2735 if (how > SD_BOTH)
2737 set_error( STATUS_INVALID_PARAMETER );
2738 return;
2741 if (sock->state != SOCK_CONNECTED && sock->state != SOCK_CONNECTIONLESS)
2743 set_error( STATUS_INVALID_CONNECTION );
2744 return;
2747 if (how != SD_SEND)
2749 sock->rd_shutdown = 1;
2751 if (how != SD_RECEIVE)
2753 sock->wr_shutdown = 1;
2754 if (list_empty( &sock->write_q.queue ))
2755 shutdown( unix_fd, SHUT_WR );
2756 else
2757 sock->wr_shutdown_pending = 1;
2760 if (how == SD_BOTH)
2762 if (sock->event) release_object( sock->event );
2763 sock->event = NULL;
2764 sock->window = 0;
2765 sock->mask = 0;
2766 sock->nonblocking = 1;
2769 sock_reselect( sock );
2770 return;
2773 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE:
2775 int force_async;
2777 if (get_req_data_size() < sizeof(int))
2779 set_error( STATUS_BUFFER_TOO_SMALL );
2780 return;
2782 force_async = *(int *)get_req_data();
2784 if (sock->nonblocking && !force_async)
2786 set_error( STATUS_DEVICE_NOT_READY );
2787 return;
2789 if (!sock_get_ifchange( sock )) return;
2790 queue_async( &sock->ifchange_q, async );
2791 set_error( STATUS_PENDING );
2792 return;
2795 case IOCTL_AFD_WINE_FIONBIO:
2796 if (get_req_data_size() < sizeof(int))
2798 set_error( STATUS_BUFFER_TOO_SMALL );
2799 return;
2801 if (*(int *)get_req_data())
2803 sock->nonblocking = 1;
2805 else
2807 if (sock->mask)
2809 set_error( STATUS_INVALID_PARAMETER );
2810 return;
2812 sock->nonblocking = 0;
2814 return;
2816 case IOCTL_AFD_EVENT_SELECT:
2818 struct event *event = NULL;
2819 obj_handle_t event_handle;
2820 int mask;
2822 set_async_pending( async );
2824 if (is_machine_64bit( current->process->machine ))
2826 const struct afd_event_select_params_64 *params = get_req_data();
2828 if (get_req_data_size() < sizeof(*params))
2830 set_error( STATUS_INVALID_PARAMETER );
2831 return;
2834 event_handle = params->event;
2835 mask = params->mask;
2837 else
2839 const struct afd_event_select_params_32 *params = get_req_data();
2841 if (get_req_data_size() < sizeof(*params))
2843 set_error( STATUS_INVALID_PARAMETER );
2844 return;
2847 event_handle = params->event;
2848 mask = params->mask;
2851 if ((event_handle || mask) &&
2852 !(event = get_event_obj( current->process, event_handle, EVENT_MODIFY_STATE )))
2854 set_error( STATUS_INVALID_PARAMETER );
2855 return;
2858 if (sock->event) release_object( sock->event );
2859 sock->event = event;
2860 sock->mask = mask;
2861 sock->window = 0;
2862 sock->message = 0;
2863 sock->wparam = 0;
2864 sock->nonblocking = 1;
2866 sock_reselect( sock );
2868 /* Explicitly wake the socket up if the mask matches pending_events.
2870 * The logic here is a bit surprising. We always set the event if the
2871 * socket has events that haven't been consumed by
2872 * WSAEnumNetworkEvents() yet, including if WSAEventSelect() is called
2873 * multiple times without consuming the events.
2874 * However, once the events are consumed by WSAEnumNetworkEvents(), we
2875 * don't set the event again (even though e.g. data is still available)
2876 * until a "reset" call (i.e. that clears reported_events). */
2878 if (event && (sock->pending_events & mask))
2880 if (debug_level) fprintf( stderr, "signalling pending events %#x due to event select\n",
2881 sock->pending_events & mask );
2882 set_event( event );
2885 return;
2888 case IOCTL_AFD_WINE_MESSAGE_SELECT:
2890 const struct afd_message_select_params *params = get_req_data();
2892 if (get_req_data_size() < sizeof(params))
2894 set_error( STATUS_BUFFER_TOO_SMALL );
2895 return;
2898 if (sock->event) release_object( sock->event );
2900 if (params->window)
2902 sock->pending_events = 0;
2903 sock->reported_events = 0;
2905 sock->event = NULL;
2906 sock->mask = params->mask;
2907 sock->window = params->window;
2908 sock->message = params->message;
2909 sock->wparam = params->handle;
2910 sock->nonblocking = 1;
2912 sock_reselect( sock );
2914 return;
2917 case IOCTL_AFD_BIND:
2919 const struct afd_bind_params *params = get_req_data();
2920 union unix_sockaddr unix_addr, bind_addr;
2921 data_size_t in_size;
2922 socklen_t unix_len;
2923 int v6only = 1;
2925 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2926 * input */
2927 if (get_req_data_size() < get_reply_max_size())
2929 set_error( STATUS_BUFFER_TOO_SMALL );
2930 return;
2932 in_size = get_req_data_size() - get_reply_max_size();
2933 if (in_size < offsetof(struct afd_bind_params, addr.sa_data)
2934 || get_reply_max_size() < in_size - sizeof(int))
2936 set_error( STATUS_INVALID_PARAMETER );
2937 return;
2940 if (sock->bound)
2942 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED );
2943 return;
2946 unix_len = sockaddr_to_unix( &params->addr, in_size - sizeof(int), &unix_addr );
2947 if (!unix_len)
2949 set_error( STATUS_INVALID_ADDRESS );
2950 return;
2952 bind_addr = unix_addr;
2954 if (unix_addr.addr.sa_family == AF_INET)
2956 if (!memcmp( &unix_addr.in.sin_addr, magic_loopback_addr, 4 )
2957 || bind_to_interface( sock, &unix_addr.in ))
2958 bind_addr.in.sin_addr.s_addr = htonl( INADDR_ANY );
2960 else if (unix_addr.addr.sa_family == AF_INET6)
2962 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2963 /* Windows allows specifying zero to use the default scope. Linux
2964 * interprets it as an interface index and requires that it be
2965 * nonzero. */
2966 if (!unix_addr.in6.sin6_scope_id)
2967 bind_addr.in6.sin6_scope_id = get_ipv6_interface_index( &unix_addr.in6.sin6_addr );
2968 #endif
2971 set_async_pending( async );
2973 #ifdef IPV6_V6ONLY
2974 if (sock->family == WS_AF_INET6)
2976 socklen_t len = sizeof(v6only);
2978 getsockopt( get_unix_fd(sock->fd), IPPROTO_IPV6, IPV6_V6ONLY, &v6only, &len );
2980 #endif
2982 if (check_addr_usage( sock, &bind_addr, v6only ))
2983 return;
2985 if (bind( unix_fd, &bind_addr.addr, unix_len ) < 0)
2987 if (errno == EADDRINUSE && sock->reuseaddr)
2988 errno = EACCES;
2990 set_error( sock_get_ntstatus( errno ) );
2991 return;
2994 sock->bound = 1;
2996 unix_len = sizeof(bind_addr);
2997 if (!getsockname( unix_fd, &bind_addr.addr, &unix_len ))
2999 /* store the interface or magic loopback address instead of the
3000 * actual unix address */
3001 if (bind_addr.addr.sa_family == AF_INET)
3002 bind_addr.in.sin_addr = unix_addr.in.sin_addr;
3003 sock->addr_len = sockaddr_from_unix( &bind_addr, &sock->addr.addr, sizeof(sock->addr) );
3006 update_addr_usage( sock, &bind_addr, v6only );
3008 if (get_reply_max_size() >= sock->addr_len)
3009 set_reply_data( &sock->addr, sock->addr_len );
3010 return;
3013 case IOCTL_AFD_GETSOCKNAME:
3014 if (!sock->bound)
3016 set_error( STATUS_INVALID_PARAMETER );
3017 return;
3020 if (get_reply_max_size() < sock->addr_len)
3022 set_error( STATUS_BUFFER_TOO_SMALL );
3023 return;
3026 set_reply_data( &sock->addr, sock->addr_len );
3027 return;
3029 case IOCTL_AFD_WINE_GETPEERNAME:
3030 if (sock->state != SOCK_CONNECTED &&
3031 sock->state != SOCK_CONNECTING &&
3032 sock->state != SOCK_CONNECTIONLESS)
3034 set_error( STATUS_INVALID_CONNECTION );
3035 return;
3038 /* If ConnectEx() hasn't finished connecting (or failing to connect) the provided
3039 * socket, getpeername() can't be called on it. This seems to be undocumented
3040 * and is *not* the case for connect(), but we do test for it in ws2_32.
3041 * connect_req is non-NULL iff ConnectEx() was used and has not finished,
3042 * so we can use it as a check for ConnectEx() usage here. */
3043 if (sock->connect_req)
3045 set_error( STATUS_INVALID_CONNECTION );
3046 return;
3049 if (!sock->peer_addr_len && sock->type == WS_SOCK_DGRAM)
3051 set_error( STATUS_INVALID_CONNECTION );
3052 return;
3055 if (get_reply_max_size() < sock->peer_addr_len)
3057 set_error( STATUS_BUFFER_TOO_SMALL );
3058 return;
3061 set_reply_data( &sock->peer_addr, sock->peer_addr_len );
3062 return;
3064 case IOCTL_AFD_WINE_DEFER:
3066 const obj_handle_t *handle = get_req_data();
3067 struct sock *acceptsock;
3069 if (get_req_data_size() < sizeof(*handle))
3071 set_error( STATUS_BUFFER_TOO_SMALL );
3072 return;
3075 acceptsock = (struct sock *)get_handle_obj( current->process, *handle, 0, &sock_ops );
3076 if (!acceptsock) return;
3078 sock->deferred = acceptsock;
3079 return;
3082 case IOCTL_AFD_WINE_GET_INFO:
3084 struct afd_get_info_params params;
3086 if (get_reply_max_size() < sizeof(params))
3088 set_error( STATUS_BUFFER_TOO_SMALL );
3089 return;
3092 params.family = sock->family;
3093 params.type = sock->type;
3094 params.protocol = sock->proto;
3095 set_reply_data( &params, sizeof(params) );
3096 return;
3099 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN:
3101 int listening = (sock->state == SOCK_LISTENING);
3103 if (get_reply_max_size() < sizeof(listening))
3105 set_error( STATUS_BUFFER_TOO_SMALL );
3106 return;
3109 set_reply_data( &listening, sizeof(listening) );
3110 return;
3113 case IOCTL_AFD_WINE_GET_SO_ERROR:
3115 int error;
3116 unsigned int i;
3118 if (get_reply_max_size() < sizeof(error))
3120 set_error( STATUS_BUFFER_TOO_SMALL );
3121 return;
3124 error = sock_error( sock, NULL );
3125 if (!error)
3127 for (i = 0; i < ARRAY_SIZE( sock->errors ); ++i)
3129 if (sock->errors[i])
3131 error = sock->errors[i];
3132 break;
3137 error = sock_get_error( error );
3138 set_reply_data( &error, sizeof(error) );
3139 return;
3142 case IOCTL_AFD_WINE_GET_SO_RCVBUF:
3144 int rcvbuf = sock->rcvbuf;
3146 if (get_reply_max_size() < sizeof(rcvbuf))
3148 set_error( STATUS_BUFFER_TOO_SMALL );
3149 return;
3152 set_reply_data( &rcvbuf, sizeof(rcvbuf) );
3153 return;
3156 case IOCTL_AFD_WINE_SET_SO_RCVBUF:
3158 DWORD rcvbuf, set_rcvbuf;
3160 if (get_req_data_size() < sizeof(rcvbuf))
3162 set_error( STATUS_BUFFER_TOO_SMALL );
3163 return;
3165 rcvbuf = *(DWORD *)get_req_data();
3166 set_rcvbuf = max( rcvbuf, MIN_RCVBUF );
3168 if (!setsockopt( unix_fd, SOL_SOCKET, SO_RCVBUF, (char *)&set_rcvbuf, sizeof(set_rcvbuf) ))
3169 sock->rcvbuf = rcvbuf;
3170 else
3171 set_error( sock_get_ntstatus( errno ) );
3172 return;
3175 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO:
3177 DWORD rcvtimeo = sock->rcvtimeo;
3179 if (get_reply_max_size() < sizeof(rcvtimeo))
3181 set_error( STATUS_BUFFER_TOO_SMALL );
3182 return;
3185 set_reply_data( &rcvtimeo, sizeof(rcvtimeo) );
3186 return;
3189 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO:
3191 DWORD rcvtimeo;
3193 if (get_req_data_size() < sizeof(rcvtimeo))
3195 set_error( STATUS_BUFFER_TOO_SMALL );
3196 return;
3198 rcvtimeo = *(DWORD *)get_req_data();
3200 sock->rcvtimeo = rcvtimeo;
3201 return;
3204 /* BSD socket SO_REUSEADDR is not compatible with winsock semantics. */
3205 case IOCTL_AFD_WINE_SET_SO_REUSEADDR:
3207 int reuse, ret;
3209 if (get_req_data_size() < sizeof(reuse))
3211 set_error( STATUS_BUFFER_TOO_SMALL );
3212 return;
3215 reuse = *(int *)get_req_data();
3217 if (reuse && sock->exclusiveaddruse)
3219 set_error( STATUS_INVALID_PARAMETER );
3220 return;
3223 if (is_tcp_socket( sock ))
3224 ret = 0;
3225 else
3226 ret = setsockopt( unix_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) );
3227 #ifdef __APPLE__
3228 if (!ret) ret = setsockopt( unix_fd, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(reuse) );
3229 #endif
3230 if (ret)
3231 set_error( sock_get_ntstatus( errno ) );
3232 else
3233 sock->reuseaddr = !!reuse;
3234 return;
3237 case IOCTL_AFD_WINE_SET_SO_EXCLUSIVEADDRUSE:
3239 int exclusive;
3241 if (get_req_data_size() < sizeof(exclusive))
3243 set_error( STATUS_BUFFER_TOO_SMALL );
3244 return;
3247 exclusive = *(int *)get_req_data();
3248 if (exclusive && sock->reuseaddr)
3250 set_error( STATUS_INVALID_PARAMETER );
3251 return;
3253 sock->exclusiveaddruse = !!exclusive;
3254 return;
3257 case IOCTL_AFD_WINE_GET_SO_SNDBUF:
3259 int sndbuf = sock->sndbuf;
3261 if (get_reply_max_size() < sizeof(sndbuf))
3263 set_error( STATUS_BUFFER_TOO_SMALL );
3264 return;
3267 set_reply_data( &sndbuf, sizeof(sndbuf) );
3268 return;
3271 case IOCTL_AFD_WINE_SET_SO_SNDBUF:
3273 DWORD sndbuf;
3275 if (get_req_data_size() < sizeof(sndbuf))
3277 set_error( STATUS_BUFFER_TOO_SMALL );
3278 return;
3280 sndbuf = *(DWORD *)get_req_data();
3282 #ifdef __APPLE__
3283 if (!sndbuf)
3285 /* setsockopt fails if a zero value is passed */
3286 sock->sndbuf = sndbuf;
3287 return;
3289 #endif
3291 if (!setsockopt( unix_fd, SOL_SOCKET, SO_SNDBUF, (char *)&sndbuf, sizeof(sndbuf) ))
3292 sock->sndbuf = sndbuf;
3293 else
3294 set_error( sock_get_ntstatus( errno ) );
3295 return;
3298 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO:
3300 DWORD sndtimeo = sock->sndtimeo;
3302 if (get_reply_max_size() < sizeof(sndtimeo))
3304 set_error( STATUS_BUFFER_TOO_SMALL );
3305 return;
3308 set_reply_data( &sndtimeo, sizeof(sndtimeo) );
3309 return;
3312 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO:
3314 DWORD sndtimeo;
3316 if (get_req_data_size() < sizeof(sndtimeo))
3318 set_error( STATUS_BUFFER_TOO_SMALL );
3319 return;
3321 sndtimeo = *(DWORD *)get_req_data();
3323 sock->sndtimeo = sndtimeo;
3324 return;
3327 case IOCTL_AFD_WINE_GET_SO_CONNECT_TIME:
3329 DWORD time = ~0u;
3331 if (get_reply_max_size() < sizeof(time))
3333 set_error( STATUS_BUFFER_TOO_SMALL );
3334 return;
3337 if (sock->state == SOCK_CONNECTED)
3338 time = (current_time - sock->connect_time) / 10000000;
3340 set_reply_data( &time, sizeof(time) );
3341 return;
3344 case IOCTL_AFD_WINE_GET_SO_REUSEADDR:
3346 int reuse;
3348 if (!get_reply_max_size())
3350 set_error( STATUS_BUFFER_TOO_SMALL );
3351 return;
3354 reuse = sock->reuseaddr;
3355 set_reply_data( &reuse, min( sizeof(reuse), get_reply_max_size() ));
3356 return;
3359 case IOCTL_AFD_WINE_GET_SO_EXCLUSIVEADDRUSE:
3361 int exclusive;
3363 if (!get_reply_max_size())
3365 set_error( STATUS_BUFFER_TOO_SMALL );
3366 return;
3369 exclusive = sock->exclusiveaddruse;
3370 set_reply_data( &exclusive, min( sizeof(exclusive), get_reply_max_size() ));
3371 return;
3374 case IOCTL_AFD_POLL:
3376 if (get_reply_max_size() < get_req_data_size())
3378 set_error( STATUS_INVALID_PARAMETER );
3379 return;
3382 if (is_machine_64bit( current->process->machine ))
3384 const struct afd_poll_params_64 *params = get_req_data();
3386 if (get_req_data_size() < sizeof(struct afd_poll_params_64) ||
3387 get_req_data_size() < offsetof( struct afd_poll_params_64, sockets[params->count] ))
3389 set_error( STATUS_INVALID_PARAMETER );
3390 return;
3393 poll_socket( sock, async, params->exclusive, params->timeout, params->count, params->sockets );
3395 else
3397 const struct afd_poll_params_32 *params = get_req_data();
3398 struct afd_poll_socket_64 *sockets;
3399 unsigned int i;
3401 if (get_req_data_size() < sizeof(struct afd_poll_params_32) ||
3402 get_req_data_size() < offsetof( struct afd_poll_params_32, sockets[params->count] ))
3404 set_error( STATUS_INVALID_PARAMETER );
3405 return;
3408 if (!(sockets = mem_alloc( params->count * sizeof(*sockets) ))) return;
3409 for (i = 0; i < params->count; ++i)
3411 sockets[i].socket = params->sockets[i].socket;
3412 sockets[i].flags = params->sockets[i].flags;
3413 sockets[i].status = params->sockets[i].status;
3416 poll_socket( sock, async, params->exclusive, params->timeout, params->count, sockets );
3417 free( sockets );
3420 return;
3423 default:
3424 set_error( STATUS_NOT_SUPPORTED );
3425 return;
3429 static void handle_exclusive_poll(struct poll_req *req)
3431 unsigned int i;
3433 for (i = 0; i < req->count; ++i)
3435 struct sock *sock = req->sockets[i].sock;
3436 struct poll_req *main_poll = sock->main_poll;
3438 if (main_poll && main_poll->exclusive && req->exclusive)
3440 complete_async_poll( main_poll, STATUS_SUCCESS );
3441 main_poll = NULL;
3444 if (!main_poll)
3445 sock->main_poll = req;
3449 static void poll_socket( struct sock *poll_sock, struct async *async, int exclusive, timeout_t timeout,
3450 unsigned int count, const struct afd_poll_socket_64 *sockets )
3452 BOOL signaled = FALSE;
3453 struct poll_req *req;
3454 unsigned int i, j;
3456 if (!count)
3458 set_error( STATUS_INVALID_PARAMETER );
3459 return;
3462 if (!(req = mem_alloc( offsetof( struct poll_req, sockets[count] ) )))
3463 return;
3465 req->timeout = NULL;
3466 req->pending = 0;
3467 if (timeout && timeout != TIMEOUT_INFINITE &&
3468 !(req->timeout = add_timeout_user( timeout, async_poll_timeout, req )))
3470 free( req );
3471 return;
3473 req->orig_timeout = timeout;
3475 for (i = 0; i < count; ++i)
3477 req->sockets[i].sock = (struct sock *)get_handle_obj( current->process, sockets[i].socket, 0, &sock_ops );
3478 if (!req->sockets[i].sock)
3480 for (j = 0; j < i; ++j) release_object( req->sockets[j].sock );
3481 if (req->timeout) remove_timeout_user( req->timeout );
3482 free( req );
3483 return;
3485 req->sockets[i].handle = sockets[i].socket;
3486 req->sockets[i].mask = sockets[i].flags;
3487 req->sockets[i].flags = 0;
3490 req->exclusive = exclusive;
3491 req->count = count;
3492 req->async = (struct async *)grab_object( async );
3493 req->iosb = async_get_iosb( async );
3495 handle_exclusive_poll(req);
3497 list_add_tail( &poll_list, &req->entry );
3498 async_set_completion_callback( async, free_poll_req, req );
3499 queue_async( &poll_sock->poll_q, async );
3501 for (i = 0; i < count; ++i)
3503 struct sock *sock = req->sockets[i].sock;
3504 int mask = req->sockets[i].mask;
3505 struct pollfd pollfd;
3507 pollfd.fd = get_unix_fd( sock->fd );
3508 pollfd.events = poll_flags_from_afd( sock, mask );
3509 if (pollfd.events >= 0 && poll( &pollfd, 1, 0 ) >= 0)
3510 sock_poll_event( sock->fd, pollfd.revents );
3512 /* FIXME: do other error conditions deserve a similar treatment? */
3513 if (sock->state != SOCK_CONNECTING && sock->errors[AFD_POLL_BIT_CONNECT_ERR] && (mask & AFD_POLL_CONNECT_ERR))
3515 req->sockets[i].flags |= AFD_POLL_CONNECT_ERR;
3516 req->sockets[i].status = sock_get_ntstatus( sock->errors[AFD_POLL_BIT_CONNECT_ERR] );
3520 for (i = 0; i < count; ++i)
3522 if (req->sockets[i].flags)
3523 signaled = TRUE;
3526 if (!timeout || signaled)
3527 complete_async_poll( req, STATUS_SUCCESS );
3528 else
3529 req->pending = 1;
3531 for (i = 0; i < req->count; ++i)
3532 sock_reselect( req->sockets[i].sock );
3533 set_error( STATUS_PENDING );
3536 #ifdef HAVE_LINUX_RTNETLINK_H
3538 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
3539 static struct object *ifchange_object;
3541 static void ifchange_dump( struct object *obj, int verbose );
3542 static struct fd *ifchange_get_fd( struct object *obj );
3543 static void ifchange_destroy( struct object *obj );
3545 static int ifchange_get_poll_events( struct fd *fd );
3546 static void ifchange_poll_event( struct fd *fd, int event );
3548 struct ifchange
3550 struct object obj; /* object header */
3551 struct fd *fd; /* interface change file descriptor */
3552 struct list sockets; /* list of sockets to send interface change notifications */
3555 static const struct object_ops ifchange_ops =
3557 sizeof(struct ifchange), /* size */
3558 &no_type, /* type */
3559 ifchange_dump, /* dump */
3560 no_add_queue, /* add_queue */
3561 NULL, /* remove_queue */
3562 NULL, /* signaled */
3563 no_satisfied, /* satisfied */
3564 no_signal, /* signal */
3565 ifchange_get_fd, /* get_fd */
3566 default_map_access, /* map_access */
3567 default_get_sd, /* get_sd */
3568 default_set_sd, /* set_sd */
3569 no_get_full_name, /* get_full_name */
3570 no_lookup_name, /* lookup_name */
3571 no_link_name, /* link_name */
3572 NULL, /* unlink_name */
3573 no_open_file, /* open_file */
3574 no_kernel_obj_list, /* get_kernel_obj_list */
3575 no_close_handle, /* close_handle */
3576 ifchange_destroy /* destroy */
3579 static const struct fd_ops ifchange_fd_ops =
3581 ifchange_get_poll_events, /* get_poll_events */
3582 ifchange_poll_event, /* poll_event */
3583 NULL, /* get_fd_type */
3584 no_fd_read, /* read */
3585 no_fd_write, /* write */
3586 no_fd_flush, /* flush */
3587 no_fd_get_file_info, /* get_file_info */
3588 no_fd_get_volume_info, /* get_volume_info */
3589 no_fd_ioctl, /* ioctl */
3590 NULL, /* cancel_async */
3591 NULL, /* queue_async */
3592 NULL /* reselect_async */
3595 static void ifchange_dump( struct object *obj, int verbose )
3597 assert( obj->ops == &ifchange_ops );
3598 fprintf( stderr, "Interface change\n" );
3601 static struct fd *ifchange_get_fd( struct object *obj )
3603 struct ifchange *ifchange = (struct ifchange *)obj;
3604 return (struct fd *)grab_object( ifchange->fd );
3607 static void ifchange_destroy( struct object *obj )
3609 struct ifchange *ifchange = (struct ifchange *)obj;
3610 assert( obj->ops == &ifchange_ops );
3612 release_object( ifchange->fd );
3614 /* reset the global ifchange object so that it will be recreated if it is needed again */
3615 assert( obj == ifchange_object );
3616 ifchange_object = NULL;
3619 static int ifchange_get_poll_events( struct fd *fd )
3621 return POLLIN;
3624 /* wake up all the sockets waiting for a change notification event */
3625 static void ifchange_wake_up( struct object *obj, unsigned int status )
3627 struct ifchange *ifchange = (struct ifchange *)obj;
3628 struct list *ptr, *next;
3629 assert( obj->ops == &ifchange_ops );
3630 assert( obj == ifchange_object );
3632 LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
3634 struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
3636 assert( sock->ifchange_obj );
3637 async_wake_up( &sock->ifchange_q, status ); /* issue ifchange notification for the socket */
3638 sock_release_ifchange( sock ); /* remove socket from list and decrement ifchange refcount */
3642 static void ifchange_poll_event( struct fd *fd, int event )
3644 struct object *ifchange = get_fd_user( fd );
3645 unsigned int status = STATUS_PENDING;
3646 char buffer[PIPE_BUF];
3647 int r;
3649 r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
3650 if (r < 0)
3652 if (errno == EWOULDBLOCK || (EWOULDBLOCK != EAGAIN && errno == EAGAIN))
3653 return; /* retry when poll() says the socket is ready */
3654 status = sock_get_ntstatus( errno );
3656 else if (r > 0)
3658 struct nlmsghdr *nlh;
3660 for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
3662 if (nlh->nlmsg_type == NLMSG_DONE)
3663 break;
3664 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
3665 status = STATUS_SUCCESS;
3668 else status = STATUS_CANCELLED;
3670 if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
3673 #endif
3675 /* we only need one of these interface notification objects, all of the sockets dependent upon
3676 * it will wake up when a notification event occurs */
3677 static struct object *get_ifchange( void )
3679 #ifdef HAVE_LINUX_RTNETLINK_H
3680 struct ifchange *ifchange;
3681 struct sockaddr_nl addr;
3682 int unix_fd;
3684 if (ifchange_object)
3686 /* increment the refcount for each socket that uses the ifchange object */
3687 return grab_object( ifchange_object );
3690 /* create the socket we need for processing interface change notifications */
3691 unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
3692 if (unix_fd == -1)
3694 set_error( sock_get_ntstatus( errno ));
3695 return NULL;
3697 fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
3698 memset( &addr, 0, sizeof(addr) );
3699 addr.nl_family = AF_NETLINK;
3700 addr.nl_groups = RTMGRP_IPV4_IFADDR;
3701 /* bind the socket to the special netlink kernel interface */
3702 if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
3704 close( unix_fd );
3705 set_error( sock_get_ntstatus( errno ));
3706 return NULL;
3708 if (!(ifchange = alloc_object( &ifchange_ops )))
3710 close( unix_fd );
3711 set_error( STATUS_NO_MEMORY );
3712 return NULL;
3714 list_init( &ifchange->sockets );
3715 if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
3717 release_object( ifchange );
3718 set_error( STATUS_NO_MEMORY );
3719 return NULL;
3721 set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
3723 /* the ifchange object is now successfully configured */
3724 ifchange_object = &ifchange->obj;
3725 return &ifchange->obj;
3726 #else
3727 set_error( STATUS_NOT_SUPPORTED );
3728 return NULL;
3729 #endif
3732 /* add the socket to the interface change notification list */
3733 static void ifchange_add_sock( struct object *obj, struct sock *sock )
3735 #ifdef HAVE_LINUX_RTNETLINK_H
3736 struct ifchange *ifchange = (struct ifchange *)obj;
3738 list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
3739 #endif
3742 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3743 static struct object *sock_get_ifchange( struct sock *sock )
3745 struct object *ifchange;
3747 if (sock->ifchange_obj) /* reuse existing ifchange_obj for this socket */
3748 return sock->ifchange_obj;
3750 if (!(ifchange = get_ifchange()))
3751 return NULL;
3753 /* add the socket to the ifchange notification list */
3754 ifchange_add_sock( ifchange, sock );
3755 sock->ifchange_obj = ifchange;
3756 return ifchange;
3759 /* destroy an existing ifchange queue for a specific socket */
3760 static void sock_release_ifchange( struct sock *sock )
3762 if (sock->ifchange_obj)
3764 list_remove( &sock->ifchange_entry );
3765 release_object( sock->ifchange_obj );
3766 sock->ifchange_obj = NULL;
3770 static void socket_device_dump( struct object *obj, int verbose );
3771 static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
3772 unsigned int attr, struct object *root );
3773 static struct object *socket_device_open_file( struct object *obj, unsigned int access,
3774 unsigned int sharing, unsigned int options );
3776 static const struct object_ops socket_device_ops =
3778 sizeof(struct object), /* size */
3779 &device_type, /* type */
3780 socket_device_dump, /* dump */
3781 no_add_queue, /* add_queue */
3782 NULL, /* remove_queue */
3783 NULL, /* signaled */
3784 no_satisfied, /* satisfied */
3785 no_signal, /* signal */
3786 no_get_fd, /* get_fd */
3787 default_map_access, /* map_access */
3788 default_get_sd, /* get_sd */
3789 default_set_sd, /* set_sd */
3790 default_get_full_name, /* get_full_name */
3791 socket_device_lookup_name, /* lookup_name */
3792 directory_link_name, /* link_name */
3793 default_unlink_name, /* unlink_name */
3794 socket_device_open_file, /* open_file */
3795 no_kernel_obj_list, /* get_kernel_obj_list */
3796 no_close_handle, /* close_handle */
3797 no_destroy /* destroy */
3800 static void socket_device_dump( struct object *obj, int verbose )
3802 fputs( "Socket device\n", stderr );
3805 static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
3806 unsigned int attr, struct object *root )
3808 if (name) name->len = 0;
3809 return NULL;
3812 static struct object *socket_device_open_file( struct object *obj, unsigned int access,
3813 unsigned int sharing, unsigned int options )
3815 struct sock *sock;
3817 if (!(sock = create_socket())) return NULL;
3818 if (!(sock->fd = alloc_pseudo_fd( &sock_fd_ops, &sock->obj, options )))
3820 release_object( sock );
3821 return NULL;
3823 return &sock->obj;
3826 struct object *create_socket_device( struct object *root, const struct unicode_str *name,
3827 unsigned int attr, const struct security_descriptor *sd )
3829 return create_named_object( root, &socket_device_ops, name, attr, sd );
3832 DECL_HANDLER(recv_socket)
3834 struct sock *sock = (struct sock *)get_handle_obj( current->process, req->async.handle, 0, &sock_ops );
3835 unsigned int status = STATUS_PENDING;
3836 timeout_t timeout = 0;
3837 struct async *async;
3838 struct fd *fd;
3840 if (!sock) return;
3841 fd = sock->fd;
3843 if (!req->force_async && !sock->nonblocking && is_fd_overlapped( fd ))
3844 timeout = (timeout_t)sock->rcvtimeo * -10000;
3846 if (sock->rd_shutdown)
3847 status = STATUS_PIPE_DISCONNECTED;
3848 else if (sock->reset)
3849 status = STATUS_CONNECTION_RESET;
3850 else if (!async_queued( &sock->read_q ))
3852 /* If read_q is not empty, we cannot really tell if the already queued
3853 * asyncs will not consume all available data; if there's no data
3854 * available, the current request won't be immediately satiable.
3856 if ((!req->force_async && sock->nonblocking) ||
3857 check_fd_events( sock->fd, req->oob && !is_oobinline( sock ) ? POLLPRI : POLLIN ))
3859 /* Give the client opportunity to complete synchronously.
3860 * If it turns out that the I/O request is not actually immediately satiable,
3861 * the client may then choose to re-queue the async (with STATUS_PENDING).
3863 * Note: If the nonblocking flag is set, we don't poll the socket
3864 * here and always opt for synchronous completion first. This is
3865 * because the application has probably seen POLLIN already from a
3866 * preceding select()/poll() call before it requested to receive
3867 * data.
3869 status = STATUS_ALERTED;
3873 if (status == STATUS_PENDING && !req->force_async && sock->nonblocking)
3874 status = STATUS_DEVICE_NOT_READY;
3876 sock->pending_events &= ~(req->oob ? AFD_POLL_OOB : AFD_POLL_READ);
3877 sock->reported_events &= ~(req->oob ? AFD_POLL_OOB : AFD_POLL_READ);
3879 if ((async = create_request_async( fd, get_fd_comp_flags( fd ), &req->async )))
3881 set_error( status );
3883 if (timeout)
3884 async_set_timeout( async, timeout, STATUS_IO_TIMEOUT );
3886 if (status == STATUS_PENDING || status == STATUS_ALERTED)
3887 queue_async( &sock->read_q, async );
3889 /* always reselect; we changed reported_events above */
3890 sock_reselect( sock );
3892 reply->wait = async_handoff( async, NULL, 0 );
3893 reply->options = get_fd_options( fd );
3894 reply->nonblocking = sock->nonblocking;
3895 release_object( async );
3897 release_object( sock );
3900 static void send_socket_completion_callback( void *private )
3902 struct send_req *send_req = private;
3903 struct iosb *iosb = send_req->iosb;
3904 struct sock *sock = send_req->sock;
3906 if (iosb->status != STATUS_SUCCESS)
3908 /* send() calls only clear and reselect events if unsuccessful. */
3909 sock->pending_events &= ~AFD_POLL_WRITE;
3910 sock->reported_events &= ~AFD_POLL_WRITE;
3911 sock_reselect( sock );
3914 release_object( iosb );
3915 release_object( sock );
3916 free( send_req );
3919 DECL_HANDLER(send_socket)
3921 struct sock *sock = (struct sock *)get_handle_obj( current->process, req->async.handle, 0, &sock_ops );
3922 unsigned int status = STATUS_PENDING;
3923 timeout_t timeout = 0;
3924 struct async *async;
3925 struct fd *fd;
3926 int bind_errno = 0;
3928 if (!sock) return;
3929 fd = sock->fd;
3931 if (sock->type == WS_SOCK_DGRAM && !sock->bound)
3933 union unix_sockaddr unix_addr;
3934 socklen_t unix_len;
3935 int unix_fd = get_unix_fd( fd );
3937 unix_len = get_unix_sockaddr_any( &unix_addr, sock->family );
3938 if (bind( unix_fd, &unix_addr.addr, unix_len ) < 0)
3939 bind_errno = errno;
3941 if (getsockname( unix_fd, &unix_addr.addr, &unix_len ) >= 0)
3943 sock->addr_len = sockaddr_from_unix( &unix_addr, &sock->addr.addr, sizeof(sock->addr) );
3944 sock->bound = 1;
3946 else if (!bind_errno) bind_errno = errno;
3949 if (!req->force_async && !sock->nonblocking && is_fd_overlapped( fd ))
3950 timeout = (timeout_t)sock->sndtimeo * -10000;
3952 if (bind_errno) status = sock_get_ntstatus( bind_errno );
3953 else if (sock->wr_shutdown) status = STATUS_PIPE_DISCONNECTED;
3954 else if (!async_queue_has_waiting_asyncs( &sock->write_q ))
3956 /* If write_q is not empty, we cannot really tell if the already queued
3957 * asyncs will not consume all available space; if there's no space
3958 * available, the current request won't be immediately satiable.
3960 if ((!req->force_async && sock->nonblocking) || check_fd_events( sock->fd, POLLOUT ))
3962 /* Give the client opportunity to complete synchronously.
3963 * If it turns out that the I/O request is not actually immediately satiable,
3964 * the client may then choose to re-queue the async (with STATUS_PENDING).
3966 * Note: If the nonblocking flag is set, we don't poll the socket
3967 * here and always opt for synchronous completion first. This is
3968 * because the application has probably seen POLLOUT already from a
3969 * preceding select()/poll() call before it requested to send data.
3971 * Furthermore, some applications expect that any send() call on a
3972 * socket that has indicated POLLOUT beforehand never fails with
3973 * WSAEWOULDBLOCK. It's possible that Linux poll() may yield
3974 * POLLOUT on the first call but not the second, even if no send()
3975 * call has been made in the meanwhile. This can happen for a
3976 * number of reasons; for example, TCP fragmentation may consume
3977 * extra buffer space for each packet that has been split out, or
3978 * the TCP/IP networking stack may decide to shrink the send buffer
3979 * due to memory pressure.
3981 status = STATUS_ALERTED;
3985 if (status == STATUS_PENDING && !req->force_async && sock->nonblocking)
3986 status = STATUS_DEVICE_NOT_READY;
3988 if ((async = create_request_async( fd, get_fd_comp_flags( fd ), &req->async )))
3990 struct send_req *send_req;
3991 struct iosb *iosb = async_get_iosb( async );
3993 if ((send_req = mem_alloc( sizeof(*send_req) )))
3995 send_req->iosb = (struct iosb *)grab_object( iosb );
3996 send_req->sock = (struct sock *)grab_object( sock );
3997 async_set_completion_callback( async, send_socket_completion_callback, send_req );
3999 else if (status == STATUS_PENDING || status == STATUS_DEVICE_NOT_READY)
4000 status = STATUS_NO_MEMORY;
4002 release_object( iosb );
4004 set_error( status );
4006 if (timeout)
4007 async_set_timeout( async, timeout, STATUS_IO_TIMEOUT );
4009 if (status == STATUS_PENDING || status == STATUS_ALERTED)
4011 queue_async( &sock->write_q, async );
4012 sock_reselect( sock );
4015 reply->wait = async_handoff( async, NULL, 0 );
4016 reply->options = get_fd_options( fd );
4017 reply->nonblocking = sock->nonblocking;
4018 release_object( async );
4020 release_object( sock );
4023 DECL_HANDLER(socket_get_events)
4025 struct sock *sock = (struct sock *)get_handle_obj( current->process, req->handle, 0, &sock_ops );
4026 unsigned int status[13];
4027 struct event *event = NULL;
4028 unsigned int i;
4030 if (get_reply_max_size() < sizeof(status))
4032 set_error( STATUS_INVALID_PARAMETER );
4033 return;
4036 if (!sock) return;
4038 if (req->event)
4040 if (!(event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE )))
4042 release_object( sock );
4043 return;
4047 reply->flags = sock->pending_events & sock->mask;
4048 for (i = 0; i < ARRAY_SIZE( status ); ++i)
4049 status[i] = sock_get_ntstatus( sock->errors[i] );
4051 sock->pending_events &= ~sock->mask;
4052 sock_reselect( sock );
4054 if (event)
4056 reset_event( event );
4057 release_object( event );
4060 set_reply_data( status, sizeof(status) );
4062 release_object( sock );
4065 DECL_HANDLER(socket_send_icmp_id)
4067 struct sock *sock = (struct sock *)get_handle_obj( current->process, req->handle, 0, &sock_ops );
4069 if (!sock) return;
4071 if (sock->icmp_fixup_data_len == MAX_ICMP_HISTORY_LENGTH)
4073 memmove( sock->icmp_fixup_data, sock->icmp_fixup_data + 1,
4074 sizeof(*sock->icmp_fixup_data) * (MAX_ICMP_HISTORY_LENGTH - 1) );
4075 --sock->icmp_fixup_data_len;
4078 sock->icmp_fixup_data[sock->icmp_fixup_data_len].icmp_id = req->icmp_id;
4079 sock->icmp_fixup_data[sock->icmp_fixup_data_len].icmp_seq = req->icmp_seq;
4080 ++sock->icmp_fixup_data_len;
4082 release_object( sock );
4085 DECL_HANDLER(socket_get_icmp_id)
4087 struct sock *sock = (struct sock *)get_handle_obj( current->process, req->handle, 0, &sock_ops );
4088 unsigned int i;
4090 if (!sock) return;
4092 for (i = 0; i < sock->icmp_fixup_data_len; ++i)
4094 if (sock->icmp_fixup_data[i].icmp_seq == req->icmp_seq)
4096 reply->icmp_id = sock->icmp_fixup_data[i].icmp_id;
4097 --sock->icmp_fixup_data_len;
4098 memmove( &sock->icmp_fixup_data[i], &sock->icmp_fixup_data[i + 1],
4099 (sock->icmp_fixup_data_len - i) * sizeof(*sock->icmp_fixup_data) );
4100 release_object( sock );
4101 return;
4105 set_error( STATUS_NOT_FOUND );
4106 release_object( sock );