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