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>
66 #elif defined(HAVE_LINUX_IPX_H)
67 # ifdef HAVE_ASM_TYPES_H
68 # include <asm/types.h>
70 # ifdef HAVE_LINUX_TYPES_H
71 # include <linux/types.h>
73 # include <linux/ipx.h>
77 #ifdef HAVE_LINUX_IRDA_H
78 # ifdef HAVE_LINUX_TYPES_H
79 # include <linux/types.h>
81 # include <linux/irda.h>
86 #define WIN32_NO_STATUS
96 #include "wine/rbtree.h"
105 #if defined(linux) && !defined(IP_UNICAST_IF)
106 #define IP_UNICAST_IF 50
109 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
113 struct WS_sockaddr addr
;
114 struct WS_sockaddr_in in
;
115 struct WS_sockaddr_in6 in6
;
116 struct WS_sockaddr_ipx ipx
;
122 struct sockaddr addr
;
123 struct sockaddr_in in
;
124 struct sockaddr_in6 in6
;
126 struct sockaddr_ipx ipx
;
129 struct sockaddr_irda irda
;
133 static struct list poll_list
= LIST_INIT( poll_list
);
140 struct timeout_user
*timeout
;
141 timeout_t orig_timeout
;
160 struct sock
*sock
, *acceptsock
;
162 unsigned int recv_len
, local_len
;
170 unsigned int addr_len
, send_len
, send_cursor
;
179 enum connection_state
190 struct rb_entry entry
;
191 union unix_sockaddr addr
;
196 #define MAX_ICMP_HISTORY_LENGTH 8
200 struct object obj
; /* object header */
201 struct fd
*fd
; /* socket file descriptor */
202 enum connection_state state
; /* connection state */
203 unsigned int mask
; /* event mask */
204 /* pending AFD_POLL_* events which have not yet been reported to the application */
205 unsigned int pending_events
;
206 /* AFD_POLL_* events which have already been reported and should not be
207 * selected for again until reset by a relevant call.
209 * For example, if AFD_POLL_READ is set here and not in pending_events, it
210 * has already been reported and consumed, and we should not report it
211 * again, even if POLLIN is signaled, until it is reset by e.g recv().
213 * If an event has been signaled and not consumed yet, it will be set in
214 * both pending_events and reported_events (as we should only ever report
215 * any event once until it is reset.) */
216 unsigned int reported_events
;
217 unsigned short proto
; /* socket protocol */
218 unsigned short type
; /* socket type */
219 unsigned short family
; /* socket family */
220 struct event
*event
; /* event object */
221 user_handle_t window
; /* window to send the message to */
222 unsigned int message
; /* message to send */
223 obj_handle_t wparam
; /* message wparam (socket handle) */
224 int errors
[AFD_POLL_BIT_COUNT
]; /* event errors */
225 timeout_t connect_time
;/* time the socket was connected */
226 struct sock
*deferred
; /* socket that waits for a deferred accept */
227 struct async_queue read_q
; /* queue for asynchronous reads */
228 struct async_queue write_q
; /* queue for asynchronous writes */
229 struct async_queue ifchange_q
; /* queue for interface change notifications */
230 struct async_queue accept_q
; /* queue for asynchronous accepts */
231 struct async_queue connect_q
; /* queue for asynchronous connects */
232 struct async_queue poll_q
; /* queue for asynchronous polls */
233 struct object
*ifchange_obj
; /* the interface change notification object */
234 struct list ifchange_entry
; /* entry in ifchange notification list */
235 struct list accept_list
; /* list of pending accept requests */
236 struct accept_req
*accept_recv_req
; /* pending accept-into request which will recv on this socket */
237 struct connect_req
*connect_req
; /* pending connection request */
238 struct poll_req
*main_poll
; /* main poll */
239 union win_sockaddr addr
; /* socket name */
240 int addr_len
; /* socket name length */
241 unsigned int rcvbuf
; /* advisory recv buffer size */
242 unsigned int sndbuf
; /* advisory send buffer size */
243 unsigned int rcvtimeo
; /* receive timeout in ms */
244 unsigned int sndtimeo
; /* send timeout in ms */
247 unsigned short icmp_id
;
248 unsigned short icmp_seq
;
250 icmp_fixup_data
[MAX_ICMP_HISTORY_LENGTH
]; /* Sent ICMP packets history used to fixup reply id. */
251 struct bound_addr
*bound_addr
[2]; /* Links to the entries in bound addresses tree. */
252 unsigned int icmp_fixup_data_len
; /* Sent ICMP packets history length. */
253 unsigned int rd_shutdown
: 1; /* is the read end shut down? */
254 unsigned int wr_shutdown
: 1; /* is the write end shut down? */
255 unsigned int wr_shutdown_pending
: 1; /* is a write shutdown pending? */
256 unsigned int hangup
: 1; /* has the read end received a hangup? */
257 unsigned int aborted
: 1; /* did we get a POLLERR or irregular POLLHUP? */
258 unsigned int nonblocking
: 1; /* is the socket nonblocking? */
259 unsigned int bound
: 1; /* is the socket bound? */
260 unsigned int reset
: 1; /* did we get a TCP reset? */
261 unsigned int reuseaddr
: 1; /* winsock SO_REUSEADDR option value */
262 unsigned int exclusiveaddruse
: 1; /* winsock SO_EXCLUSIVEADDRUSE option value */
265 static int is_tcp_socket( struct sock
*sock
)
267 return sock
->type
== WS_SOCK_STREAM
&& (sock
->family
== WS_AF_INET
|| sock
->family
== WS_AF_INET6
);
270 static int addr_compare( const void *key
, const struct wine_rb_entry
*entry
)
272 const struct bound_addr
*bound_addr
= RB_ENTRY_VALUE(entry
, struct bound_addr
, entry
);
273 const struct bound_addr
*addr
= key
;
275 if (addr
->addr
.addr
.sa_family
!= bound_addr
->addr
.addr
.sa_family
)
276 return addr
->addr
.addr
.sa_family
< bound_addr
->addr
.addr
.sa_family
? -1 : 1;
278 if (addr
->addr
.addr
.sa_family
== AF_INET
)
280 if (addr
->addr
.in
.sin_port
!= bound_addr
->addr
.in
.sin_port
)
281 return addr
->addr
.in
.sin_port
< bound_addr
->addr
.in
.sin_port
? -1 : 1;
282 if (bound_addr
->match_any_addr
|| addr
->match_any_addr
283 || addr
->addr
.in
.sin_addr
.s_addr
== bound_addr
->addr
.in
.sin_addr
.s_addr
)
285 return addr
->addr
.in
.sin_addr
.s_addr
< bound_addr
->addr
.in
.sin_addr
.s_addr
? -1 : 1;
288 assert( addr
->addr
.addr
.sa_family
== AF_INET6
);
289 if (addr
->addr
.in6
.sin6_port
!= bound_addr
->addr
.in6
.sin6_port
)
290 return addr
->addr
.in6
.sin6_port
< bound_addr
->addr
.in6
.sin6_port
? -1 : 1;
291 if (bound_addr
->match_any_addr
|| addr
->match_any_addr
) return 0;
292 return memcmp( &addr
->addr
.in6
.sin6_addr
, &bound_addr
->addr
.in6
.sin6_addr
, sizeof(addr
->addr
.in6
.sin6_addr
) );
295 static int ipv4addr_from_v6( union unix_sockaddr
*v4addr
, const struct sockaddr_in6
*in6
, int map_unspecified
)
297 v4addr
->in
.sin_family
= AF_INET
;
298 v4addr
->in
.sin_port
= in6
->sin6_port
;
300 if (map_unspecified
&& IN6_IS_ADDR_UNSPECIFIED(&in6
->sin6_addr
))
302 v4addr
->in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
305 if (IN6_IS_ADDR_V4COMPAT(&in6
->sin6_addr
) || IN6_IS_ADDR_V4MAPPED(&in6
->sin6_addr
))
307 memcpy( &v4addr
->in
.sin_addr
.s_addr
, &in6
->sin6_addr
.s6_addr
[12], sizeof(v4addr
->in
.sin_addr
.s_addr
) );
313 static struct rb_tree bound_addresses_tree
= { addr_compare
};
315 static int should_track_conflicts_for_addr( struct sock
*sock
, const union unix_sockaddr
*addr
)
317 if (!is_tcp_socket( sock
)) return 0;
319 if (sock
->family
== WS_AF_INET
&& addr
->addr
.sa_family
== AF_INET
&& addr
->in
.sin_port
)
321 else if (sock
->family
== WS_AF_INET6
&& addr
->addr
.sa_family
== AF_INET6
&& addr
->in6
.sin6_port
)
327 static int is_any_addr( const union unix_sockaddr
*addr
)
329 if (addr
->addr
.sa_family
== AF_INET
&& addr
->in
.sin_addr
.s_addr
== htonl( INADDR_ANY
))
331 if (addr
->addr
.sa_family
== AF_INET6
&& IN6_IS_ADDR_UNSPECIFIED(&addr
->in6
.sin6_addr
))
336 static int check_addr_usage( struct sock
*sock
, const union unix_sockaddr
*addr
, int v6only
)
338 struct bound_addr
*bound_addr
, search_addr
;
339 struct rb_entry
*entry
;
341 if (!should_track_conflicts_for_addr( sock
, addr
)) return 0;
343 search_addr
.addr
= *addr
;
344 search_addr
.match_any_addr
= sock
->exclusiveaddruse
&& is_any_addr( addr
);
346 if ((entry
= rb_get( &bound_addresses_tree
, &search_addr
)))
348 bound_addr
= WINE_RB_ENTRY_VALUE(entry
, struct bound_addr
, entry
);
349 if (bound_addr
->reuse_count
== -1 || !sock
->reuseaddr
)
351 set_error( sock
->reuseaddr
|| bound_addr
->match_any_addr
352 ? STATUS_ACCESS_DENIED
: STATUS_SHARING_VIOLATION
);
357 if (sock
->family
!= WS_AF_INET6
|| v6only
) return 0;
358 if (!ipv4addr_from_v6( &search_addr
.addr
, &addr
->in6
, sock
->exclusiveaddruse
)) return 0;
360 search_addr
.match_any_addr
= sock
->exclusiveaddruse
&& is_any_addr( &search_addr
.addr
);
361 if ((entry
= rb_get( &bound_addresses_tree
, &search_addr
)))
363 bound_addr
= WINE_RB_ENTRY_VALUE(entry
, struct bound_addr
, entry
);
364 if (bound_addr
->reuse_count
== -1 || !sock
->reuseaddr
)
366 set_error( sock
->reuseaddr
|| bound_addr
->match_any_addr
367 ? STATUS_ACCESS_DENIED
: STATUS_SHARING_VIOLATION
);
374 static struct bound_addr
*register_bound_address( struct sock
*sock
, const union unix_sockaddr
*addr
)
376 struct bound_addr
*bound_addr
, *temp
;
378 if (!(bound_addr
= mem_alloc( sizeof(*bound_addr
) )))
381 bound_addr
->addr
= *addr
;
382 bound_addr
->match_any_addr
= sock
->exclusiveaddruse
&& is_any_addr( addr
);
384 if (rb_put( &bound_addresses_tree
, bound_addr
, &bound_addr
->entry
))
387 bound_addr
= WINE_RB_ENTRY_VALUE(rb_get( &bound_addresses_tree
, temp
), struct bound_addr
, entry
);
389 if (bound_addr
->reuse_count
== -1)
392 fprintf( stderr
, "register_bound_address: address being updated is already exclusively bound\n" );
395 ++bound_addr
->reuse_count
;
399 bound_addr
->reuse_count
= sock
->reuseaddr
? 1 : -1;
404 static void update_addr_usage( struct sock
*sock
, const union unix_sockaddr
*addr
, int v6only
)
406 union unix_sockaddr v4addr
;
408 assert( !sock
->bound_addr
[0] && !sock
->bound_addr
[1] );
410 if (!should_track_conflicts_for_addr( sock
, addr
)) return;
412 sock
->bound_addr
[0] = register_bound_address( sock
, addr
);
414 if (sock
->family
!= WS_AF_INET6
|| v6only
) return;
416 if (!ipv4addr_from_v6( &v4addr
, &addr
->in6
, sock
->exclusiveaddruse
)) return;
418 sock
->bound_addr
[1] = register_bound_address( sock
, &v4addr
);
421 static void sock_dump( struct object
*obj
, int verbose
);
422 static struct fd
*sock_get_fd( struct object
*obj
);
423 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
424 static void sock_destroy( struct object
*obj
);
425 static struct object
*sock_get_ifchange( struct sock
*sock
);
426 static void sock_release_ifchange( struct sock
*sock
);
428 static int sock_get_poll_events( struct fd
*fd
);
429 static void sock_poll_event( struct fd
*fd
, int event
);
430 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
431 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
432 static void sock_cancel_async( struct fd
*fd
, struct async
*async
);
433 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
435 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
);
436 static struct sock
*accept_socket( struct sock
*sock
);
437 static int sock_get_ntstatus( int err
);
438 static unsigned int sock_get_error( int err
);
439 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
440 unsigned int count
, const struct afd_poll_socket_64
*sockets
);
442 static const struct object_ops sock_ops
=
444 sizeof(struct sock
), /* size */
445 &file_type
, /* type */
446 sock_dump
, /* dump */
447 add_queue
, /* add_queue */
448 remove_queue
, /* remove_queue */
449 default_fd_signaled
, /* signaled */
450 no_satisfied
, /* satisfied */
451 no_signal
, /* signal */
452 sock_get_fd
, /* get_fd */
453 default_map_access
, /* map_access */
454 default_get_sd
, /* get_sd */
455 default_set_sd
, /* set_sd */
456 no_get_full_name
, /* get_full_name */
457 no_lookup_name
, /* lookup_name */
458 no_link_name
, /* link_name */
459 NULL
, /* unlink_name */
460 no_open_file
, /* open_file */
461 no_kernel_obj_list
, /* get_kernel_obj_list */
462 sock_close_handle
, /* close_handle */
463 sock_destroy
/* destroy */
466 static const struct fd_ops sock_fd_ops
=
468 sock_get_poll_events
, /* get_poll_events */
469 sock_poll_event
, /* poll_event */
470 sock_get_fd_type
, /* get_fd_type */
471 no_fd_read
, /* read */
472 no_fd_write
, /* write */
473 no_fd_flush
, /* flush */
474 default_fd_get_file_info
, /* get_file_info */
475 no_fd_get_volume_info
, /* get_volume_info */
476 sock_ioctl
, /* ioctl */
477 sock_cancel_async
, /* cancel_async */
478 no_fd_queue_async
, /* queue_async */
479 sock_reselect_async
/* reselect_async */
482 static int sockaddr_from_unix( const union unix_sockaddr
*uaddr
, struct WS_sockaddr
*wsaddr
, socklen_t wsaddrlen
)
484 memset( wsaddr
, 0, wsaddrlen
);
486 switch (uaddr
->addr
.sa_family
)
490 struct WS_sockaddr_in win
= {0};
492 if (wsaddrlen
< sizeof(win
)) return -1;
493 win
.sin_family
= WS_AF_INET
;
494 win
.sin_port
= uaddr
->in
.sin_port
;
495 memcpy( &win
.sin_addr
, &uaddr
->in
.sin_addr
, sizeof(win
.sin_addr
) );
496 memcpy( wsaddr
, &win
, sizeof(win
) );
502 struct WS_sockaddr_in6 win
= {0};
504 if (wsaddrlen
< sizeof(win
)) return -1;
505 win
.sin6_family
= WS_AF_INET6
;
506 win
.sin6_port
= uaddr
->in6
.sin6_port
;
507 win
.sin6_flowinfo
= uaddr
->in6
.sin6_flowinfo
;
508 memcpy( &win
.sin6_addr
, &uaddr
->in6
.sin6_addr
, sizeof(win
.sin6_addr
) );
509 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
510 win
.sin6_scope_id
= uaddr
->in6
.sin6_scope_id
;
512 memcpy( wsaddr
, &win
, sizeof(win
) );
519 struct WS_sockaddr_ipx win
= {0};
521 if (wsaddrlen
< sizeof(win
)) return -1;
522 win
.sa_family
= WS_AF_IPX
;
523 memcpy( win
.sa_netnum
, &uaddr
->ipx
.sipx_network
, sizeof(win
.sa_netnum
) );
524 memcpy( win
.sa_nodenum
, &uaddr
->ipx
.sipx_node
, sizeof(win
.sa_nodenum
) );
525 win
.sa_socket
= uaddr
->ipx
.sipx_port
;
526 memcpy( wsaddr
, &win
, sizeof(win
) );
536 if (wsaddrlen
< sizeof(win
)) return -1;
537 win
.irdaAddressFamily
= WS_AF_IRDA
;
538 memcpy( win
.irdaDeviceID
, &uaddr
->irda
.sir_addr
, sizeof(win
.irdaDeviceID
) );
539 if (uaddr
->irda
.sir_lsap_sel
!= LSAP_ANY
)
540 snprintf( win
.irdaServiceName
, sizeof(win
.irdaServiceName
), "LSAP-SEL%u", uaddr
->irda
.sir_lsap_sel
);
542 memcpy( win
.irdaServiceName
, uaddr
->irda
.sir_name
, sizeof(win
.irdaServiceName
) );
543 memcpy( wsaddr
, &win
, sizeof(win
) );
557 static socklen_t
sockaddr_to_unix( const struct WS_sockaddr
*wsaddr
, int wsaddrlen
, union unix_sockaddr
*uaddr
)
559 memset( uaddr
, 0, sizeof(*uaddr
) );
561 switch (wsaddr
->sa_family
)
565 struct WS_sockaddr_in win
= {0};
567 if (wsaddrlen
< sizeof(win
)) return 0;
568 memcpy( &win
, wsaddr
, sizeof(win
) );
569 uaddr
->in
.sin_family
= AF_INET
;
570 uaddr
->in
.sin_port
= win
.sin_port
;
571 memcpy( &uaddr
->in
.sin_addr
, &win
.sin_addr
, sizeof(win
.sin_addr
) );
572 return sizeof(uaddr
->in
);
577 struct WS_sockaddr_in6 win
= {0};
579 if (wsaddrlen
< sizeof(win
)) return 0;
580 memcpy( &win
, wsaddr
, sizeof(win
) );
581 uaddr
->in6
.sin6_family
= AF_INET6
;
582 uaddr
->in6
.sin6_port
= win
.sin6_port
;
583 uaddr
->in6
.sin6_flowinfo
= win
.sin6_flowinfo
;
584 memcpy( &uaddr
->in6
.sin6_addr
, &win
.sin6_addr
, sizeof(win
.sin6_addr
) );
585 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
586 uaddr
->in6
.sin6_scope_id
= win
.sin6_scope_id
;
588 return sizeof(uaddr
->in6
);
594 struct WS_sockaddr_ipx win
= {0};
596 if (wsaddrlen
< sizeof(win
)) return 0;
597 memcpy( &win
, wsaddr
, sizeof(win
) );
598 uaddr
->ipx
.sipx_family
= AF_IPX
;
599 memcpy( &uaddr
->ipx
.sipx_network
, win
.sa_netnum
, sizeof(win
.sa_netnum
) );
600 memcpy( &uaddr
->ipx
.sipx_node
, win
.sa_nodenum
, sizeof(win
.sa_nodenum
) );
601 uaddr
->ipx
.sipx_port
= win
.sa_socket
;
602 return sizeof(uaddr
->ipx
);
609 SOCKADDR_IRDA win
= {0};
610 unsigned int lsap_sel
;
612 if (wsaddrlen
< sizeof(win
)) return 0;
613 memcpy( &win
, wsaddr
, sizeof(win
) );
614 uaddr
->irda
.sir_family
= AF_IRDA
;
615 if (sscanf( win
.irdaServiceName
, "LSAP-SEL%u", &lsap_sel
) == 1)
616 uaddr
->irda
.sir_lsap_sel
= lsap_sel
;
619 uaddr
->irda
.sir_lsap_sel
= LSAP_ANY
;
620 memcpy( uaddr
->irda
.sir_name
, win
.irdaServiceName
, sizeof(win
.irdaServiceName
) );
622 memcpy( &uaddr
->irda
.sir_addr
, win
.irdaDeviceID
, sizeof(win
.irdaDeviceID
) );
623 return sizeof(uaddr
->irda
);
630 default: /* likely an ipv4 address */
631 case sizeof(struct WS_sockaddr_in
):
632 return sizeof(uaddr
->in
);
635 case sizeof(struct WS_sockaddr_ipx
):
636 return sizeof(uaddr
->ipx
);
640 case sizeof(SOCKADDR_IRDA
):
641 return sizeof(uaddr
->irda
);
644 case sizeof(struct WS_sockaddr_in6
):
645 return sizeof(uaddr
->in6
);
653 static socklen_t
get_unix_sockaddr_any( union unix_sockaddr
*uaddr
, int ws_family
)
655 memset( uaddr
, 0, sizeof(*uaddr
) );
659 uaddr
->in
.sin_family
= AF_INET
;
660 return sizeof(uaddr
->in
);
662 uaddr
->in6
.sin6_family
= AF_INET6
;
663 return sizeof(uaddr
->in6
);
666 uaddr
->ipx
.sipx_family
= AF_IPX
;
667 return sizeof(uaddr
->ipx
);
671 uaddr
->irda
.sir_family
= AF_IRDA
;
672 return sizeof(uaddr
->irda
);
679 /* some events are generated at the same time but must be sent in a particular
680 * order (e.g. CONNECT must be sent before READ) */
681 static const enum afd_poll_bit event_bitorder
[] =
683 AFD_POLL_BIT_CONNECT
,
684 AFD_POLL_BIT_CONNECT_ERR
,
695 SOCK_SHUTDOWN_ERROR
= -1,
696 SOCK_SHUTDOWN_EOF
= 0,
697 SOCK_SHUTDOWN_POLLHUP
= 1
700 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
702 static sock_shutdown_t
sock_check_pollhup(void)
704 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
709 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
710 if ( shutdown( fd
[0], 1 ) ) goto out
;
716 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
717 n
= poll( &pfd
, 1, 1 );
718 if ( n
!= 1 ) goto out
; /* error or timeout */
719 if ( pfd
.revents
& POLLHUP
)
720 ret
= SOCK_SHUTDOWN_POLLHUP
;
721 else if ( pfd
.revents
& POLLIN
&&
722 read( fd
[1], &dummy
, 1 ) == 0 )
723 ret
= SOCK_SHUTDOWN_EOF
;
733 sock_shutdown_type
= sock_check_pollhup();
735 switch ( sock_shutdown_type
)
737 case SOCK_SHUTDOWN_EOF
:
738 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
740 case SOCK_SHUTDOWN_POLLHUP
:
741 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
744 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
745 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
749 static void sock_reselect( struct sock
*sock
)
751 int ev
= sock_get_poll_events( sock
->fd
);
754 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
756 set_fd_events( sock
->fd
, ev
);
759 static unsigned int afd_poll_flag_to_win32( unsigned int flags
)
761 static const unsigned int map
[] =
765 FD_WRITE
, /* WRITE */
767 FD_CLOSE
, /* RESET */
769 FD_CONNECT
, /* CONNECT */
770 FD_ACCEPT
, /* ACCEPT */
771 FD_CONNECT
, /* CONNECT_ERR */
774 unsigned int i
, ret
= 0;
776 for (i
= 0; i
< ARRAY_SIZE(map
); ++i
)
778 if (flags
& (1 << i
)) ret
|= map
[i
];
784 /* wake anybody waiting on the socket event or send the associated message */
785 static void sock_wake_up( struct sock
*sock
)
787 unsigned int events
= sock
->pending_events
& sock
->mask
;
792 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
794 set_event( sock
->event
);
798 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
799 for (i
= 0; i
< ARRAY_SIZE(event_bitorder
); i
++)
801 enum afd_poll_bit event
= event_bitorder
[i
];
802 if (events
& (1 << event
))
804 lparam_t lparam
= afd_poll_flag_to_win32(1 << event
) | (sock_get_error( sock
->errors
[event
] ) << 16);
805 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
808 sock
->pending_events
= 0;
809 sock_reselect( sock
);
813 static inline int sock_error( struct sock
*sock
)
816 socklen_t len
= sizeof(error
);
818 getsockopt( get_unix_fd(sock
->fd
), SOL_SOCKET
, SO_ERROR
, (void *)&error
, &len
);
822 case SOCK_UNCONNECTED
:
825 case SOCK_CONNECTING
:
827 sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] = error
;
829 error
= sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
];
834 sock
->errors
[AFD_POLL_BIT_ACCEPT
] = error
;
836 error
= sock
->errors
[AFD_POLL_BIT_ACCEPT
];
840 case SOCK_CONNECTIONLESS
:
841 if (error
== ECONNRESET
|| error
== EPIPE
)
847 sock
->errors
[AFD_POLL_BIT_HUP
] = error
;
849 error
= sock
->errors
[AFD_POLL_BIT_HUP
];
856 static void free_accept_req( void *private )
858 struct accept_req
*req
= private;
859 list_remove( &req
->entry
);
862 req
->acceptsock
->accept_recv_req
= NULL
;
863 release_object( req
->acceptsock
);
865 release_object( req
->async
);
866 release_object( req
->iosb
);
867 release_object( req
->sock
);
871 static void fill_accept_output( struct accept_req
*req
)
873 const data_size_t out_size
= req
->iosb
->out_size
;
874 struct async
*async
= req
->async
;
875 union unix_sockaddr unix_addr
;
876 struct WS_sockaddr
*win_addr
;
877 unsigned int remote_len
;
883 if (!(out_data
= mem_alloc( out_size
)))
885 async_terminate( async
, get_error() );
889 fd
= get_unix_fd( req
->acceptsock
->fd
);
891 if (req
->recv_len
&& (size
= recv( fd
, out_data
, req
->recv_len
, 0 )) < 0)
893 if (!req
->accepted
&& errno
== EWOULDBLOCK
)
896 sock_reselect( req
->acceptsock
);
900 async_terminate( async
, sock_get_ntstatus( errno
) );
907 if (req
->local_len
< sizeof(int))
909 async_terminate( async
, STATUS_BUFFER_TOO_SMALL
);
914 unix_len
= sizeof(unix_addr
);
915 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ sizeof(int));
916 if (getsockname( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
917 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, req
->local_len
- sizeof(int) )) < 0)
919 async_terminate( async
, sock_get_ntstatus( errno
) );
923 memcpy( out_data
+ req
->recv_len
, &win_len
, sizeof(int) );
926 unix_len
= sizeof(unix_addr
);
927 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ req
->local_len
+ sizeof(int));
928 remote_len
= out_size
- req
->recv_len
- req
->local_len
;
929 if (getpeername( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
930 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, remote_len
- sizeof(int) )) < 0)
932 async_terminate( async
, sock_get_ntstatus( errno
) );
936 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
938 async_request_complete( req
->async
, STATUS_SUCCESS
, size
, out_size
, out_data
);
941 static void complete_async_accept( struct sock
*sock
, struct accept_req
*req
)
943 struct sock
*acceptsock
= req
->acceptsock
;
944 struct async
*async
= req
->async
;
946 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
950 if (!accept_into_socket( sock
, acceptsock
))
952 async_terminate( async
, get_error() );
955 fill_accept_output( req
);
961 if (!(acceptsock
= accept_socket( sock
)))
963 async_terminate( async
, get_error() );
966 handle
= alloc_handle_no_access_check( async_get_thread( async
)->process
, &acceptsock
->obj
,
967 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
968 acceptsock
->wparam
= handle
;
969 sock_reselect( acceptsock
);
970 release_object( acceptsock
);
973 async_terminate( async
, get_error() );
977 async_request_complete_alloc( req
->async
, STATUS_SUCCESS
, 0, sizeof(handle
), &handle
);
981 static void complete_async_accept_recv( struct accept_req
*req
)
983 if (debug_level
) fprintf( stderr
, "completing accept recv request for socket %p\n", req
->acceptsock
);
985 assert( req
->recv_len
);
987 fill_accept_output( req
);
990 static void free_connect_req( void *private )
992 struct connect_req
*req
= private;
994 req
->sock
->connect_req
= NULL
;
995 release_object( req
->async
);
996 release_object( req
->iosb
);
997 release_object( req
->sock
);
1001 static void complete_async_connect( struct sock
*sock
)
1003 struct connect_req
*req
= sock
->connect_req
;
1004 const char *in_buffer
;
1008 if (debug_level
) fprintf( stderr
, "completing connect request for socket %p\n", sock
);
1012 async_terminate( req
->async
, STATUS_SUCCESS
);
1016 in_buffer
= (const char *)req
->iosb
->in_data
+ sizeof(struct afd_connect_params
) + req
->addr_len
;
1017 len
= req
->send_len
- req
->send_cursor
;
1019 ret
= send( get_unix_fd( sock
->fd
), in_buffer
+ req
->send_cursor
, len
, 0 );
1020 if (ret
< 0 && errno
!= EWOULDBLOCK
)
1021 async_terminate( req
->async
, sock_get_ntstatus( errno
) );
1022 else if (ret
== len
)
1023 async_request_complete( req
->async
, STATUS_SUCCESS
, req
->send_len
, 0, NULL
);
1025 req
->send_cursor
+= ret
;
1028 static void free_poll_req( void *private )
1030 struct poll_req
*req
= private;
1033 if (req
->timeout
) remove_timeout_user( req
->timeout
);
1035 for (i
= 0; i
< req
->count
; ++i
)
1036 release_object( req
->sockets
[i
].sock
);
1037 release_object( req
->async
);
1038 release_object( req
->iosb
);
1039 list_remove( &req
->entry
);
1043 static int is_oobinline( struct sock
*sock
)
1046 socklen_t len
= sizeof(oobinline
);
1047 return !getsockopt( get_unix_fd( sock
->fd
), SOL_SOCKET
, SO_OOBINLINE
, (char *)&oobinline
, &len
) && oobinline
;
1050 static int get_poll_flags( struct sock
*sock
, int event
)
1054 /* A connection-mode socket which has never been connected does not return
1055 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
1056 if (sock
->state
== SOCK_UNCONNECTED
)
1057 event
&= ~(POLLOUT
| POLLHUP
);
1061 if (sock
->state
== SOCK_LISTENING
)
1062 flags
|= AFD_POLL_ACCEPT
;
1064 flags
|= AFD_POLL_READ
;
1066 if (event
& POLLPRI
)
1067 flags
|= is_oobinline( sock
) ? AFD_POLL_READ
: AFD_POLL_OOB
;
1068 if (event
& POLLOUT
)
1069 flags
|= AFD_POLL_WRITE
;
1070 if (sock
->state
== SOCK_CONNECTED
)
1071 flags
|= AFD_POLL_CONNECT
;
1072 if (event
& POLLHUP
)
1073 flags
|= AFD_POLL_HUP
;
1074 if (event
& POLLERR
)
1075 flags
|= AFD_POLL_CONNECT_ERR
;
1077 flags
|= AFD_POLL_RESET
;
1082 static void complete_async_poll( struct poll_req
*req
, unsigned int status
)
1084 unsigned int i
, signaled_count
= 0;
1086 for (i
= 0; i
< req
->count
; ++i
)
1088 struct sock
*sock
= req
->sockets
[i
].sock
;
1090 if (sock
->main_poll
== req
)
1091 sock
->main_poll
= NULL
;
1096 for (i
= 0; i
< req
->count
; ++i
)
1098 if (req
->sockets
[i
].flags
)
1103 if (is_machine_64bit( async_get_thread( req
->async
)->process
->machine
))
1105 size_t output_size
= offsetof( struct afd_poll_params_64
, sockets
[signaled_count
] );
1106 struct afd_poll_params_64
*output
;
1108 if (!(output
= mem_alloc( output_size
)))
1110 async_terminate( req
->async
, get_error() );
1113 memset( output
, 0, output_size
);
1114 output
->timeout
= req
->orig_timeout
;
1115 output
->exclusive
= req
->exclusive
;
1116 for (i
= 0; i
< req
->count
; ++i
)
1118 if (!req
->sockets
[i
].flags
) continue;
1119 output
->sockets
[output
->count
].socket
= req
->sockets
[i
].handle
;
1120 output
->sockets
[output
->count
].flags
= req
->sockets
[i
].flags
;
1121 output
->sockets
[output
->count
].status
= req
->sockets
[i
].status
;
1124 assert( output
->count
== signaled_count
);
1126 async_request_complete( req
->async
, status
, output_size
, output_size
, output
);
1130 size_t output_size
= offsetof( struct afd_poll_params_32
, sockets
[signaled_count
] );
1131 struct afd_poll_params_32
*output
;
1133 if (!(output
= mem_alloc( output_size
)))
1135 async_terminate( req
->async
, get_error() );
1138 memset( output
, 0, output_size
);
1139 output
->timeout
= req
->orig_timeout
;
1140 output
->exclusive
= req
->exclusive
;
1141 for (i
= 0; i
< req
->count
; ++i
)
1143 if (!req
->sockets
[i
].flags
) continue;
1144 output
->sockets
[output
->count
].socket
= req
->sockets
[i
].handle
;
1145 output
->sockets
[output
->count
].flags
= req
->sockets
[i
].flags
;
1146 output
->sockets
[output
->count
].status
= req
->sockets
[i
].status
;
1149 assert( output
->count
== signaled_count
);
1151 async_request_complete( req
->async
, status
, output_size
, output_size
, output
);
1155 static void complete_async_polls( struct sock
*sock
, int event
, int error
)
1157 int flags
= get_poll_flags( sock
, event
);
1158 struct poll_req
*req
, *next
;
1160 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &poll_list
, struct poll_req
, entry
)
1164 if (req
->iosb
->status
!= STATUS_PENDING
) continue;
1166 for (i
= 0; i
< req
->count
; ++i
)
1168 if (req
->sockets
[i
].sock
!= sock
) continue;
1169 if (!(req
->sockets
[i
].mask
& flags
)) continue;
1172 fprintf( stderr
, "completing poll for socket %p, wanted %#x got %#x\n",
1173 sock
, req
->sockets
[i
].mask
, flags
);
1175 req
->sockets
[i
].flags
= req
->sockets
[i
].mask
& flags
;
1176 req
->sockets
[i
].status
= sock_get_ntstatus( error
);
1180 complete_async_poll( req
, STATUS_SUCCESS
);
1187 static void async_poll_timeout( void *private )
1189 struct poll_req
*req
= private;
1191 req
->timeout
= NULL
;
1193 if (req
->iosb
->status
!= STATUS_PENDING
) return;
1195 complete_async_poll( req
, STATUS_TIMEOUT
);
1198 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
1200 if (event
& (POLLIN
| POLLPRI
))
1202 struct accept_req
*req
;
1204 LIST_FOR_EACH_ENTRY( req
, &sock
->accept_list
, struct accept_req
, entry
)
1206 if (req
->iosb
->status
== STATUS_PENDING
&& !req
->accepted
)
1208 complete_async_accept( sock
, req
);
1214 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
1215 complete_async_accept_recv( sock
->accept_recv_req
);
1218 if ((event
& POLLOUT
) && sock
->connect_req
&& sock
->connect_req
->iosb
->status
== STATUS_PENDING
)
1219 complete_async_connect( sock
);
1221 if ((event
& (POLLIN
| POLLPRI
)) && async_queued( &sock
->read_q
))
1223 if (async_waiting( &sock
->read_q
))
1225 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
1226 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
1228 event
&= ~(POLLIN
| POLLPRI
);
1231 if ((event
& POLLOUT
) && async_queued( &sock
->write_q
))
1233 if (async_waiting( &sock
->write_q
))
1235 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
1236 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
1241 if (event
& (POLLERR
| POLLHUP
))
1243 int status
= sock_get_ntstatus( error
);
1244 struct accept_req
*req
, *next
;
1246 async_wake_up( &sock
->read_q
, status
);
1247 async_wake_up( &sock
->write_q
, status
);
1249 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
1251 if (req
->iosb
->status
== STATUS_PENDING
)
1252 async_terminate( req
->async
, status
);
1255 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
1256 async_terminate( sock
->accept_recv_req
->async
, status
);
1258 if (sock
->connect_req
)
1259 async_terminate( sock
->connect_req
->async
, status
);
1264 async_wake_up( &sock
->read_q
, STATUS_CONNECTION_RESET
);
1265 async_wake_up( &sock
->write_q
, STATUS_CONNECTION_RESET
);
1267 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
1268 async_terminate( sock
->accept_recv_req
->async
, STATUS_CONNECTION_RESET
);
1274 static void post_socket_event( struct sock
*sock
, enum afd_poll_bit event_bit
)
1276 unsigned int event
= (1 << event_bit
);
1278 if (!(sock
->reported_events
& event
))
1280 sock
->pending_events
|= event
;
1281 sock
->reported_events
|= event
;
1285 static void sock_dispatch_events( struct sock
*sock
, enum connection_state prevstate
, int event
)
1289 case SOCK_UNCONNECTED
:
1292 case SOCK_CONNECTING
:
1293 if (event
& POLLOUT
)
1294 post_socket_event( sock
, AFD_POLL_BIT_CONNECT
);
1295 if (event
& (POLLERR
| POLLHUP
))
1296 post_socket_event( sock
, AFD_POLL_BIT_CONNECT_ERR
);
1299 case SOCK_LISTENING
:
1300 if (event
& (POLLIN
| POLLERR
| POLLHUP
))
1301 post_socket_event( sock
, AFD_POLL_BIT_ACCEPT
);
1304 case SOCK_CONNECTED
:
1305 case SOCK_CONNECTIONLESS
:
1307 post_socket_event( sock
, AFD_POLL_BIT_RESET
);
1310 post_socket_event( sock
, AFD_POLL_BIT_READ
);
1312 if (event
& POLLOUT
)
1313 post_socket_event( sock
, AFD_POLL_BIT_WRITE
);
1315 if (event
& POLLPRI
)
1316 post_socket_event( sock
, AFD_POLL_BIT_OOB
);
1318 if (event
& (POLLERR
| POLLHUP
))
1319 post_socket_event( sock
, AFD_POLL_BIT_HUP
);
1323 sock_wake_up( sock
);
1326 static void sock_poll_event( struct fd
*fd
, int event
)
1328 struct sock
*sock
= get_fd_user( fd
);
1329 int hangup_seen
= 0;
1330 enum connection_state prevstate
= sock
->state
;
1333 assert( sock
->obj
.ops
== &sock_ops
);
1335 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
1337 if (event
& (POLLERR
| POLLHUP
))
1338 error
= sock_error( sock
);
1340 switch (sock
->state
)
1342 case SOCK_UNCONNECTED
:
1345 case SOCK_CONNECTING
:
1346 if (event
& (POLLERR
|POLLHUP
))
1348 sock
->state
= SOCK_UNCONNECTED
;
1351 else if (event
& POLLOUT
)
1353 sock
->state
= SOCK_CONNECTED
;
1354 sock
->connect_time
= current_time
;
1355 sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] = 0;
1359 case SOCK_LISTENING
:
1362 case SOCK_CONNECTED
:
1363 case SOCK_CONNECTIONLESS
:
1365 event
&= ~(POLLIN
| POLLERR
| POLLHUP
);
1367 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
1372 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
1373 * has been closed, so we need to check for it explicitly here */
1374 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
1383 /* EAGAIN can happen if an async recv() falls between the server's poll()
1384 call and the invocation of this routine */
1385 if (errno
== ECONNRESET
|| errno
== EPIPE
)
1389 else if (errno
!= EAGAIN
)
1393 sock
->errors
[AFD_POLL_BIT_HUP
] = error
;
1395 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
1400 if (hangup_seen
|| (sock_shutdown_type
== SOCK_SHUTDOWN_POLLHUP
&& (event
& POLLHUP
)))
1404 else if (event
& (POLLHUP
| POLLERR
))
1409 fprintf( stderr
, "socket %p aborted by error %d, event %#x\n", sock
, error
, event
);
1417 event
= sock_dispatch_asyncs( sock
, event
, error
);
1418 sock_dispatch_events( sock
, prevstate
, event
);
1419 complete_async_polls( sock
, event
, error
);
1421 sock_reselect( sock
);
1424 static void sock_dump( struct object
*obj
, int verbose
)
1426 struct sock
*sock
= (struct sock
*)obj
;
1427 assert( obj
->ops
== &sock_ops
);
1428 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
1429 sock
->fd
, sock
->state
,
1430 sock
->mask
, sock
->pending_events
, sock
->reported_events
);
1433 static int poll_flags_from_afd( struct sock
*sock
, int flags
)
1437 /* A connection-mode socket which has never been connected does
1438 * not return write or hangup events, but Linux returns
1439 * POLLOUT | POLLHUP. */
1440 if (sock
->state
== SOCK_UNCONNECTED
)
1443 if (flags
& (AFD_POLL_READ
| AFD_POLL_ACCEPT
))
1445 if ((flags
& AFD_POLL_HUP
) && sock
->type
== WS_SOCK_STREAM
)
1447 if (flags
& AFD_POLL_OOB
)
1448 ev
|= is_oobinline( sock
) ? POLLIN
: POLLPRI
;
1449 if (flags
& AFD_POLL_WRITE
)
1455 static int sock_get_poll_events( struct fd
*fd
)
1457 struct sock
*sock
= get_fd_user( fd
);
1458 unsigned int mask
= sock
->mask
& ~sock
->reported_events
;
1459 struct poll_req
*req
;
1462 assert( sock
->obj
.ops
== &sock_ops
);
1464 if (!sock
->type
) /* not initialized yet */
1467 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1471 for (i
= 0; i
< req
->count
; ++i
)
1473 if (req
->sockets
[i
].sock
!= sock
) continue;
1475 ev
|= poll_flags_from_afd( sock
, req
->sockets
[i
].mask
);
1479 switch (sock
->state
)
1481 case SOCK_UNCONNECTED
:
1482 /* A connection-mode Windows socket which has never been connected does
1483 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1484 * need to return -1 here, to prevent the socket from being polled on at
1488 case SOCK_CONNECTING
:
1491 case SOCK_LISTENING
:
1492 if (!list_empty( &sock
->accept_list
) || (mask
& AFD_POLL_ACCEPT
))
1496 case SOCK_CONNECTED
:
1497 case SOCK_CONNECTIONLESS
:
1498 if (sock
->hangup
&& sock
->wr_shutdown
&& !sock
->wr_shutdown_pending
)
1500 /* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
1501 * if both the socket and its peer are SHUT_WR.
1503 * We don't use SHUT_RD, so we can only encounter this in the latter
1504 * case. In that case there can't be any pending read requests (they
1505 * would have already been completed with a length of zero), the
1506 * above condition ensures that we don't have any pending write
1507 * requests, and nothing that can change about the socket state that
1508 * would complete a pending poll request. */
1512 if (sock
->aborted
|| sock
->reset
)
1515 if (sock
->accept_recv_req
)
1519 else if (async_queued( &sock
->read_q
))
1521 /* Clear POLLIN and POLLPRI if we have an alerted async, even if
1522 * we're polling this socket for READ or OOB. We can't signal the
1523 * poll if the pending async will read all of the data [cf. the
1524 * matching logic in sock_dispatch_asyncs()], but we also don't
1525 * want to spin polling for POLLIN if we're not going to use it. */
1526 if (async_waiting( &sock
->read_q
))
1527 ev
|= POLLIN
| POLLPRI
;
1529 ev
&= ~(POLLIN
| POLLPRI
);
1533 /* Don't ask for POLLIN if we got a hangup. We won't receive more
1534 * data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
1537 if (mask
& AFD_POLL_READ
)
1539 if (mask
& AFD_POLL_OOB
)
1543 /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
1544 if (sock
->state
== SOCK_CONNECTED
&& (mask
& AFD_POLL_HUP
) && !(sock
->reported_events
& AFD_POLL_READ
))
1548 if (async_queued( &sock
->write_q
))
1550 /* As with read asyncs above, clear POLLOUT if we have an alerted
1552 if (async_waiting( &sock
->write_q
))
1557 else if (!sock
->wr_shutdown
&& (mask
& AFD_POLL_WRITE
))
1568 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
1570 return FD_TYPE_SOCKET
;
1573 static void sock_cancel_async( struct fd
*fd
, struct async
*async
)
1575 struct poll_req
*req
;
1577 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1581 if (req
->async
!= async
)
1584 for (i
= 0; i
< req
->count
; i
++)
1586 struct sock
*sock
= req
->sockets
[i
].sock
;
1588 if (sock
->main_poll
== req
)
1589 sock
->main_poll
= NULL
;
1593 async_terminate( async
, STATUS_CANCELLED
);
1596 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
1598 struct sock
*sock
= get_fd_user( fd
);
1600 if (sock
->wr_shutdown_pending
&& list_empty( &sock
->write_q
.queue
))
1602 shutdown( get_unix_fd( sock
->fd
), SHUT_WR
);
1603 sock
->wr_shutdown_pending
= 0;
1606 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1607 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1609 if (queue
!= &sock
->ifchange_q
&& sock
->type
)
1610 sock_reselect( sock
);
1613 static struct fd
*sock_get_fd( struct object
*obj
)
1615 struct sock
*sock
= (struct sock
*)obj
;
1616 return (struct fd
*)grab_object( sock
->fd
);
1619 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
1621 struct sock
*sock
= (struct sock
*)obj
;
1623 if (sock
->obj
.handle_count
== 1) /* last handle */
1625 struct accept_req
*accept_req
, *accept_next
;
1626 struct poll_req
*poll_req
, *poll_next
;
1628 if (sock
->accept_recv_req
)
1629 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
1631 LIST_FOR_EACH_ENTRY_SAFE( accept_req
, accept_next
, &sock
->accept_list
, struct accept_req
, entry
)
1632 async_terminate( accept_req
->async
, STATUS_CANCELLED
);
1634 if (sock
->connect_req
)
1635 async_terminate( sock
->connect_req
->async
, STATUS_CANCELLED
);
1637 LIST_FOR_EACH_ENTRY_SAFE( poll_req
, poll_next
, &poll_list
, struct poll_req
, entry
)
1639 struct iosb
*iosb
= poll_req
->iosb
;
1640 BOOL signaled
= FALSE
;
1643 if (iosb
->status
!= STATUS_PENDING
) continue;
1645 for (i
= 0; i
< poll_req
->count
; ++i
)
1647 if (poll_req
->sockets
[i
].sock
== sock
)
1650 poll_req
->sockets
[i
].flags
= AFD_POLL_CLOSE
;
1651 poll_req
->sockets
[i
].status
= 0;
1655 if (signaled
) complete_async_poll( poll_req
, STATUS_SUCCESS
);
1662 static void sock_destroy( struct object
*obj
)
1664 struct sock
*sock
= (struct sock
*)obj
;
1667 assert( obj
->ops
== &sock_ops
);
1669 /* FIXME: special socket shutdown stuff? */
1671 for (i
= 0; i
< 2; ++i
)
1673 if (sock
->bound_addr
[i
] && --sock
->bound_addr
[i
]->reuse_count
<= 0)
1675 rb_remove( &bound_addresses_tree
, &sock
->bound_addr
[i
]->entry
);
1676 free( sock
->bound_addr
[i
] );
1680 if ( sock
->deferred
)
1681 release_object( sock
->deferred
);
1683 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
1684 sock_release_ifchange( sock
);
1685 free_async_queue( &sock
->read_q
);
1686 free_async_queue( &sock
->write_q
);
1687 free_async_queue( &sock
->ifchange_q
);
1688 free_async_queue( &sock
->accept_q
);
1689 free_async_queue( &sock
->connect_q
);
1690 free_async_queue( &sock
->poll_q
);
1691 if (sock
->event
) release_object( sock
->event
);
1692 if (sock
->fd
) release_object( sock
->fd
);
1695 static struct sock
*create_socket(void)
1699 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
1701 sock
->state
= SOCK_UNCONNECTED
;
1703 sock
->pending_events
= 0;
1704 sock
->reported_events
= 0;
1712 sock
->connect_time
= 0;
1713 sock
->deferred
= NULL
;
1714 sock
->ifchange_obj
= NULL
;
1715 sock
->accept_recv_req
= NULL
;
1716 sock
->connect_req
= NULL
;
1717 sock
->main_poll
= NULL
;
1718 memset( &sock
->addr
, 0, sizeof(sock
->addr
) );
1720 sock
->rd_shutdown
= 0;
1721 sock
->wr_shutdown
= 0;
1722 sock
->wr_shutdown_pending
= 0;
1725 sock
->nonblocking
= 0;
1728 sock
->reuseaddr
= 0;
1729 sock
->exclusiveaddruse
= 0;
1734 sock
->icmp_fixup_data_len
= 0;
1735 sock
->bound_addr
[0] = sock
->bound_addr
[1] = NULL
;
1736 init_async_queue( &sock
->read_q
);
1737 init_async_queue( &sock
->write_q
);
1738 init_async_queue( &sock
->ifchange_q
);
1739 init_async_queue( &sock
->accept_q
);
1740 init_async_queue( &sock
->connect_q
);
1741 init_async_queue( &sock
->poll_q
);
1742 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
1743 list_init( &sock
->accept_list
);
1747 static int get_unix_family( int family
)
1751 case WS_AF_INET
: return AF_INET
;
1752 case WS_AF_INET6
: return AF_INET6
;
1754 case WS_AF_IPX
: return AF_IPX
;
1757 case WS_AF_IRDA
: return AF_IRDA
;
1759 case WS_AF_UNSPEC
: return AF_UNSPEC
;
1764 static int get_unix_type( int type
)
1768 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
1769 case WS_SOCK_RAW
: return SOCK_RAW
;
1770 case WS_SOCK_STREAM
: return SOCK_STREAM
;
1775 static int get_unix_protocol( int protocol
)
1777 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1782 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
1783 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1784 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1785 case WS_IPPROTO_IPV4
: return IPPROTO_IPIP
;
1786 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1787 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1788 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1789 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1794 static void set_dont_fragment( int fd
, int level
, int value
)
1798 if (level
== IPPROTO_IP
)
1801 optname
= IP_DONTFRAG
;
1802 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1803 optname
= IP_MTU_DISCOVER
;
1804 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1811 #ifdef IPV6_DONTFRAG
1812 optname
= IPV6_DONTFRAG
;
1813 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1814 optname
= IPV6_MTU_DISCOVER
;
1815 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1821 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1824 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
)
1826 unsigned int options
= 0;
1827 int sockfd
, unix_type
, unix_family
, unix_protocol
, value
;
1830 unix_family
= get_unix_family( family
);
1831 unix_type
= get_unix_type( type
);
1832 unix_protocol
= get_unix_protocol( protocol
);
1834 if (unix_protocol
< 0)
1836 if (type
&& unix_type
< 0)
1837 set_win32_error( WSAESOCKTNOSUPPORT
);
1839 set_win32_error( WSAEPROTONOSUPPORT
);
1842 if (unix_family
< 0)
1844 if (family
>= 0 && unix_type
< 0)
1845 set_win32_error( WSAESOCKTNOSUPPORT
);
1847 set_win32_error( WSAEAFNOSUPPORT
);
1851 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1854 if (sockfd
== -1 && errno
== EPERM
&& unix_family
== AF_INET
1855 && unix_type
== SOCK_RAW
&& unix_protocol
== IPPROTO_ICMP
)
1857 sockfd
= socket( unix_family
, SOCK_DGRAM
, unix_protocol
);
1862 setsockopt( sockfd
, IPPROTO_IP
, IP_RECVTTL
, (const char *)&val
, sizeof(val
) );
1863 setsockopt( sockfd
, IPPROTO_IP
, IP_RECVTOS
, (const char *)&val
, sizeof(val
) );
1864 setsockopt( sockfd
, IPPROTO_IP
, IP_PKTINFO
, (const char *)&val
, sizeof(val
) );
1871 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1872 else set_win32_error( sock_get_error( errno
));
1875 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1877 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1880 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1883 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1886 /* Should we retrieve val using a getsockopt call and then
1887 * set the modified one? */
1888 val
.ipx_pt
= ipx_type
;
1889 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1894 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1896 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1897 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1898 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1899 else if (unix_type
== SOCK_STREAM
)
1900 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1904 if (unix_family
== AF_INET6
)
1906 static const int enable
= 1;
1907 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1911 len
= sizeof(value
);
1912 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_RCVBUF
, &value
, &len
))
1913 sock
->rcvbuf
= value
;
1915 len
= sizeof(value
);
1916 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_SNDBUF
, &value
, &len
))
1917 sock
->sndbuf
= value
;
1919 sock
->state
= (type
== WS_SOCK_STREAM
? SOCK_UNCONNECTED
: SOCK_CONNECTIONLESS
);
1920 sock
->proto
= protocol
;
1922 sock
->family
= family
;
1924 if (is_tcp_socket( sock
))
1927 setsockopt( sockfd
, SOL_SOCKET
, SO_REUSEADDR
, &value
, sizeof(value
) );
1930 setsockopt( sockfd
, IPPROTO_TCP
, TCP_SYNCNT
, &value
, sizeof(value
) );
1936 options
= get_fd_options( sock
->fd
);
1937 release_object( sock
->fd
);
1940 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1945 /* We can't immediately allow caching for a connection-mode socket, since it
1946 * might be accepted into (changing the underlying fd object.) */
1947 if (sock
->type
!= WS_SOCK_STREAM
) allow_fd_caching( sock
->fd
);
1952 /* accepts a socket and inits it */
1953 static int accept_new_fd( struct sock
*sock
)
1956 /* Try to accept(2). We can't be safe that this an already connected socket
1957 * or that accept() is allowed on it. In those cases we will get -1/errno
1960 struct sockaddr saddr
;
1961 socklen_t slen
= sizeof(saddr
);
1962 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1964 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1966 set_error( sock_get_ntstatus( errno
));
1970 /* accept a socket (creates a new fd) */
1971 static struct sock
*accept_socket( struct sock
*sock
)
1973 struct sock
*acceptsock
;
1976 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1978 if ( sock
->deferred
)
1980 acceptsock
= sock
->deferred
;
1981 sock
->deferred
= NULL
;
1985 union unix_sockaddr unix_addr
;
1988 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1989 if (!(acceptsock
= create_socket()))
1995 /* newly created socket gets the same properties of the listening socket */
1996 acceptsock
->state
= SOCK_CONNECTED
;
1997 acceptsock
->bound
= 1;
1998 acceptsock
->nonblocking
= sock
->nonblocking
;
1999 acceptsock
->mask
= sock
->mask
;
2000 acceptsock
->proto
= sock
->proto
;
2001 acceptsock
->type
= sock
->type
;
2002 acceptsock
->family
= sock
->family
;
2003 acceptsock
->window
= sock
->window
;
2004 acceptsock
->message
= sock
->message
;
2005 acceptsock
->reuseaddr
= sock
->reuseaddr
;
2006 acceptsock
->exclusiveaddruse
= sock
->exclusiveaddruse
;
2007 acceptsock
->sndbuf
= sock
->sndbuf
;
2008 acceptsock
->rcvbuf
= sock
->rcvbuf
;
2009 acceptsock
->sndtimeo
= sock
->sndtimeo
;
2010 acceptsock
->rcvtimeo
= sock
->rcvtimeo
;
2011 acceptsock
->connect_time
= current_time
;
2013 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
2014 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
2015 get_fd_options( sock
->fd
) )))
2017 release_object( acceptsock
);
2020 unix_len
= sizeof(unix_addr
);
2021 if (!getsockname( acceptfd
, &unix_addr
.addr
, &unix_len
))
2022 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
2025 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
2026 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
2027 sock_reselect( sock
);
2031 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
2033 union unix_sockaddr unix_addr
;
2038 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
2040 if ( sock
->deferred
)
2042 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
2043 get_fd_options( acceptsock
->fd
) );
2047 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
2049 release_object( sock
->deferred
);
2050 sock
->deferred
= NULL
;
2054 if ((acceptfd
= accept_new_fd( sock
)) == -1)
2057 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
2058 get_fd_options( acceptsock
->fd
) )))
2062 acceptsock
->state
= SOCK_CONNECTED
;
2063 acceptsock
->bound
= 1;
2064 acceptsock
->pending_events
= 0;
2065 acceptsock
->reported_events
= 0;
2066 acceptsock
->proto
= sock
->proto
;
2067 acceptsock
->type
= sock
->type
;
2068 acceptsock
->family
= sock
->family
;
2069 acceptsock
->wparam
= 0;
2070 acceptsock
->deferred
= NULL
;
2071 acceptsock
->connect_time
= current_time
;
2072 fd_copy_completion( acceptsock
->fd
, newfd
);
2073 release_object( acceptsock
->fd
);
2074 acceptsock
->fd
= newfd
;
2076 unix_len
= sizeof(unix_addr
);
2077 if (!getsockname( get_unix_fd( newfd
), &unix_addr
.addr
, &unix_len
))
2078 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
2081 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
2082 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
2083 sock_reselect( sock
);
2090 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
2092 static const int enable
= 1;
2095 if (!(index
= if_nametoindex( name
)))
2098 if (setsockopt( fd
, IPPROTO_IP
, IP_BOUND_IF
, &index
, sizeof(index
) ))
2101 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
2104 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER) && defined(SO_BINDTODEVICE)
2106 struct interface_filter
2108 struct sock_filter iface_memaddr
;
2109 struct sock_filter iface_rule
;
2110 struct sock_filter ip_memaddr
;
2111 struct sock_filter ip_rule
;
2112 struct sock_filter return_keep
;
2113 struct sock_filter return_dump
;
2115 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
2116 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
2117 /sizeof(struct sock_filter)
2118 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
2119 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
2120 /sizeof(struct sock_filter)
2121 # define FILTER_JUMP_NEXT() (u_char)(0)
2122 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
2123 static struct interface_filter generic_interface_filter
=
2125 /* This filter rule allows incoming packets on the specified interface, which works for all
2126 * remotely generated packets and for locally generated broadcast packets. */
2127 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
2128 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
2129 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
2130 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
2131 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
2132 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
2133 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
2134 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
2137 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
2139 struct interface_filter specific_interface_filter
;
2140 struct sock_fprog filter_prog
;
2141 static const int enable
= 1;
2145 if (!setsockopt( fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen( name
) + 1 ))
2148 /* SO_BINDTODEVICE requires NET_CAP_RAW until Linux 5.7. */
2150 fprintf( stderr
, "setsockopt SO_BINDTODEVICE fd %d, name %s failed: %s, falling back to SO_REUSE_ADDR\n",
2151 fd
, name
, strerror( errno
));
2153 if (!(index
= if_nametoindex( name
)))
2156 ifindex
= htonl( index
);
2157 if (setsockopt( fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
) ) < 0)
2160 specific_interface_filter
= generic_interface_filter
;
2161 specific_interface_filter
.iface_rule
.k
= index
;
2162 specific_interface_filter
.ip_rule
.k
= htonl( bind_addr
);
2163 filter_prog
.len
= sizeof(generic_interface_filter
) / sizeof(struct sock_filter
);
2164 filter_prog
.filter
= (struct sock_filter
*)&specific_interface_filter
;
2165 if (setsockopt( fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
) ))
2168 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
2173 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
2179 #endif /* LINUX_BOUND_IF */
2181 /* Take bind() calls on any name corresponding to a local network adapter and
2182 * restrict the given socket to operating only on the specified interface. This
2183 * restriction consists of two components:
2184 * 1) An outgoing packet restriction suggesting the egress interface for all
2186 * 2) An incoming packet restriction dropping packets not meant for the
2188 * If the function succeeds in placing these restrictions, then the name for the
2189 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
2190 * receipt of broadcast packets on the socket. This behavior is only relevant to
2191 * UDP sockets and is needed for applications that expect to be able to receive
2192 * broadcast packets on a socket that is bound to a specific network interface.
2194 static int bind_to_interface( struct sock
*sock
, const struct sockaddr_in
*addr
)
2196 in_addr_t bind_addr
= addr
->sin_addr
.s_addr
;
2197 struct ifaddrs
*ifaddrs
, *ifaddr
;
2198 int fd
= get_unix_fd( sock
->fd
);
2201 if (bind_addr
== htonl( INADDR_ANY
) || bind_addr
== htonl( INADDR_LOOPBACK
))
2203 if (sock
->type
!= WS_SOCK_DGRAM
)
2206 if (getifaddrs( &ifaddrs
) < 0) return 0;
2208 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
2210 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET
2211 && ((struct sockaddr_in
*)ifaddr
->ifa_addr
)->sin_addr
.s_addr
== bind_addr
)
2213 if ((err
= bind_to_iface_name( fd
, bind_addr
, ifaddr
->ifa_name
)) < 0)
2216 fprintf( stderr
, "failed to bind to interface: %s\n", strerror( errno
) );
2221 freeifaddrs( ifaddrs
);
2225 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2226 static unsigned int get_ipv6_interface_index( const struct in6_addr
*addr
)
2228 struct ifaddrs
*ifaddrs
, *ifaddr
;
2230 if (getifaddrs( &ifaddrs
) < 0) return 0;
2232 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
2234 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET6
2235 && !memcmp( &((struct sockaddr_in6
*)ifaddr
->ifa_addr
)->sin6_addr
, addr
, sizeof(*addr
) ))
2237 unsigned int index
= if_nametoindex( ifaddr
->ifa_name
);
2242 fprintf( stderr
, "Unable to look up interface index for %s: %s\n",
2243 ifaddr
->ifa_name
, strerror( errno
) );
2247 freeifaddrs( ifaddrs
);
2252 freeifaddrs( ifaddrs
);
2257 /* return an errno value mapped to a WSA error */
2258 static unsigned int sock_get_error( int err
)
2262 case EINTR
: return WSAEINTR
;
2263 case EBADF
: return WSAEBADF
;
2265 case EACCES
: return WSAEACCES
;
2266 case EFAULT
: return WSAEFAULT
;
2267 case EINVAL
: return WSAEINVAL
;
2268 case EMFILE
: return WSAEMFILE
;
2270 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
2271 case EALREADY
: return WSAEALREADY
;
2272 case ENOTSOCK
: return WSAENOTSOCK
;
2273 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
2274 case EMSGSIZE
: return WSAEMSGSIZE
;
2275 case EPROTOTYPE
: return WSAEPROTOTYPE
;
2276 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
2277 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
2278 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
2279 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
2280 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
2281 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
2282 case EADDRINUSE
: return WSAEADDRINUSE
;
2283 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
2284 case ENETDOWN
: return WSAENETDOWN
;
2285 case ENETUNREACH
: return WSAENETUNREACH
;
2286 case ENETRESET
: return WSAENETRESET
;
2287 case ECONNABORTED
: return WSAECONNABORTED
;
2289 case ECONNRESET
: return WSAECONNRESET
;
2290 case ENOBUFS
: return WSAENOBUFS
;
2291 case EISCONN
: return WSAEISCONN
;
2292 case ENOTCONN
: return WSAENOTCONN
;
2293 case ESHUTDOWN
: return WSAESHUTDOWN
;
2294 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
2295 case ETIMEDOUT
: return WSAETIMEDOUT
;
2296 case ECONNREFUSED
: return WSAECONNREFUSED
;
2297 case ELOOP
: return WSAELOOP
;
2298 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
2299 case EHOSTDOWN
: return WSAEHOSTDOWN
;
2300 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
2301 case ENOTEMPTY
: return WSAENOTEMPTY
;
2303 case EPROCLIM
: return WSAEPROCLIM
;
2306 case EUSERS
: return WSAEUSERS
;
2309 case EDQUOT
: return WSAEDQUOT
;
2312 case ESTALE
: return WSAESTALE
;
2315 case EREMOTE
: return WSAEREMOTE
;
2321 perror("wineserver: sock_get_error() can't map error");
2326 static int sock_get_ntstatus( int err
)
2330 case EBADF
: return STATUS_INVALID_HANDLE
;
2331 case EBUSY
: return STATUS_DEVICE_BUSY
;
2333 case EACCES
: return STATUS_ACCESS_DENIED
;
2334 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
2335 case EINVAL
: return STATUS_INVALID_PARAMETER
;
2337 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
2339 case EWOULDBLOCK
: return STATUS_DEVICE_NOT_READY
;
2340 case EALREADY
: return STATUS_NETWORK_BUSY
;
2341 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
2342 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
2343 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
2344 case EPROTONOSUPPORT
:
2345 case ESOCKTNOSUPPORT
:
2348 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
2349 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
2350 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
2351 case EADDRINUSE
: return STATUS_SHARING_VIOLATION
;
2352 /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
2353 * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
2355 case EADDRNOTAVAIL
: return STATUS_INVALID_ADDRESS_COMPONENT
;
2356 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
2357 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
2358 case ENOTCONN
: return STATUS_INVALID_CONNECTION
;
2359 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
2360 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
2361 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
2362 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
2364 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
2365 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
2366 case EISCONN
: return STATUS_CONNECTION_ACTIVE
;
2368 case 0: return STATUS_SUCCESS
;
2371 perror("wineserver: sock_get_ntstatus() can't map error");
2372 return STATUS_UNSUCCESSFUL
;
2376 static struct accept_req
*alloc_accept_req( struct sock
*sock
, struct sock
*acceptsock
, struct async
*async
,
2377 const struct afd_accept_into_params
*params
)
2379 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
2383 req
->async
= (struct async
*)grab_object( async
);
2384 req
->iosb
= async_get_iosb( async
);
2385 req
->sock
= (struct sock
*)grab_object( sock
);
2386 req
->acceptsock
= acceptsock
;
2387 if (acceptsock
) grab_object( acceptsock
);
2393 req
->recv_len
= params
->recv_len
;
2394 req
->local_len
= params
->local_len
;
2400 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2402 struct sock
*sock
= get_fd_user( fd
);
2405 assert( sock
->obj
.ops
== &sock_ops
);
2407 if (code
!= IOCTL_AFD_WINE_CREATE
&& code
!= IOCTL_AFD_POLL
&& (unix_fd
= get_unix_fd( fd
)) < 0)
2412 case IOCTL_AFD_WINE_CREATE
:
2414 const struct afd_create_params
*params
= get_req_data();
2416 if (get_req_data_size() != sizeof(*params
))
2418 set_error( STATUS_INVALID_PARAMETER
);
2421 init_socket( sock
, params
->family
, params
->type
, params
->protocol
);
2425 case IOCTL_AFD_WINE_ACCEPT
:
2427 struct sock
*acceptsock
;
2428 obj_handle_t handle
;
2430 if (get_reply_max_size() != sizeof(handle
))
2432 set_error( STATUS_BUFFER_TOO_SMALL
);
2436 if (!(acceptsock
= accept_socket( sock
)))
2438 struct accept_req
*req
;
2440 if (sock
->nonblocking
) return;
2441 if (get_error() != STATUS_DEVICE_NOT_READY
) return;
2443 if (!(req
= alloc_accept_req( sock
, NULL
, async
, NULL
))) return;
2444 list_add_tail( &sock
->accept_list
, &req
->entry
);
2446 async_set_completion_callback( async
, free_accept_req
, req
);
2447 queue_async( &sock
->accept_q
, async
);
2448 sock_reselect( sock
);
2449 set_error( STATUS_PENDING
);
2452 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
2453 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
2454 acceptsock
->wparam
= handle
;
2455 sock_reselect( acceptsock
);
2456 release_object( acceptsock
);
2457 set_reply_data( &handle
, sizeof(handle
) );
2461 case IOCTL_AFD_WINE_ACCEPT_INTO
:
2463 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
2464 const struct afd_accept_into_params
*params
= get_req_data();
2465 struct sock
*acceptsock
;
2466 unsigned int remote_len
;
2467 struct accept_req
*req
;
2469 if (get_req_data_size() != sizeof(*params
) ||
2470 get_reply_max_size() < params
->recv_len
||
2471 get_reply_max_size() - params
->recv_len
< params
->local_len
)
2473 set_error( STATUS_BUFFER_TOO_SMALL
);
2477 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
2478 if (remote_len
< sizeof(int))
2480 set_error( STATUS_INVALID_PARAMETER
);
2484 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
2487 if (acceptsock
->accept_recv_req
)
2489 release_object( acceptsock
);
2490 set_error( STATUS_INVALID_PARAMETER
);
2494 if (!(req
= alloc_accept_req( sock
, acceptsock
, async
, params
)))
2496 release_object( acceptsock
);
2499 list_add_tail( &sock
->accept_list
, &req
->entry
);
2500 acceptsock
->accept_recv_req
= req
;
2501 release_object( acceptsock
);
2503 acceptsock
->wparam
= params
->accept_handle
;
2504 async_set_completion_callback( async
, free_accept_req
, req
);
2505 queue_async( &sock
->accept_q
, async
);
2506 sock_reselect( sock
);
2507 set_error( STATUS_PENDING
);
2511 case IOCTL_AFD_LISTEN
:
2513 const struct afd_listen_params
*params
= get_req_data();
2515 if (get_req_data_size() < sizeof(*params
))
2517 set_error( STATUS_INVALID_PARAMETER
);
2523 set_error( STATUS_INVALID_PARAMETER
);
2527 if (listen( unix_fd
, params
->backlog
) < 0)
2529 set_error( sock_get_ntstatus( errno
) );
2533 sock
->state
= SOCK_LISTENING
;
2535 /* a listening socket can no longer be accepted into */
2536 allow_fd_caching( sock
->fd
);
2538 /* we may already be selecting for AFD_POLL_ACCEPT */
2539 sock_reselect( sock
);
2543 case IOCTL_AFD_WINE_CONNECT
:
2545 const struct afd_connect_params
*params
= get_req_data();
2546 const struct WS_sockaddr
*addr
;
2547 union unix_sockaddr unix_addr
;
2548 struct connect_req
*req
;
2552 if (get_req_data_size() < sizeof(*params
) ||
2553 get_req_data_size() - sizeof(*params
) < params
->addr_len
)
2555 set_error( STATUS_BUFFER_TOO_SMALL
);
2558 send_len
= get_req_data_size() - sizeof(*params
) - params
->addr_len
;
2559 addr
= (const struct WS_sockaddr
*)(params
+ 1);
2561 if (!params
->synchronous
&& !sock
->bound
)
2563 set_error( STATUS_INVALID_PARAMETER
);
2567 if (sock
->accept_recv_req
)
2569 set_error( STATUS_INVALID_PARAMETER
);
2573 if (sock
->connect_req
)
2575 set_error( STATUS_INVALID_PARAMETER
);
2579 switch (sock
->state
)
2581 case SOCK_LISTENING
:
2582 set_error( STATUS_INVALID_PARAMETER
);
2585 case SOCK_CONNECTING
:
2586 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2587 * but there's no status code that maps to WSAEALREADY... */
2588 set_error( params
->synchronous
? STATUS_ADDRESS_ALREADY_ASSOCIATED
: STATUS_INVALID_PARAMETER
);
2591 case SOCK_CONNECTED
:
2592 set_error( STATUS_CONNECTION_ACTIVE
);
2595 case SOCK_UNCONNECTED
:
2596 case SOCK_CONNECTIONLESS
:
2600 unix_len
= sockaddr_to_unix( addr
, params
->addr_len
, &unix_addr
);
2603 set_error( STATUS_INVALID_ADDRESS
);
2606 if (unix_addr
.addr
.sa_family
== AF_INET
&& !memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 ))
2607 unix_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_LOOPBACK
);
2609 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2610 if (ret
< 0 && errno
== ECONNABORTED
)
2612 /* On Linux with nonblocking socket if the previous connect() failed for any reason (including
2613 * timeout), next connect will fail. If the error code was queried by getsockopt( SO_ERROR )
2614 * the error code returned now is ECONNABORTED (otherwise that is the actual connect() failure
2615 * error code). If we got here after previous connect attempt on the socket that means
2616 * we already queried SO_ERROR in sock_error(), so retrying on ECONNABORTED only is
2618 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2621 if (ret
< 0 && errno
!= EINPROGRESS
)
2623 set_error( sock_get_ntstatus( errno
) );
2627 /* a connected or connecting socket can no longer be accepted into */
2628 allow_fd_caching( sock
->fd
);
2630 unix_len
= sizeof(unix_addr
);
2631 if (!getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
))
2632 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2637 if (sock
->type
!= WS_SOCK_DGRAM
)
2639 sock
->state
= SOCK_CONNECTED
;
2640 sock
->connect_time
= current_time
;
2643 if (!send_len
) return;
2646 if (sock
->type
!= WS_SOCK_DGRAM
)
2647 sock
->state
= SOCK_CONNECTING
;
2649 if (params
->synchronous
&& sock
->nonblocking
)
2651 sock_reselect( sock
);
2652 set_error( STATUS_DEVICE_NOT_READY
);
2656 if (!(req
= mem_alloc( sizeof(*req
) )))
2659 req
->async
= (struct async
*)grab_object( async
);
2660 req
->iosb
= async_get_iosb( async
);
2661 req
->sock
= (struct sock
*)grab_object( sock
);
2662 req
->addr_len
= params
->addr_len
;
2663 req
->send_len
= send_len
;
2664 req
->send_cursor
= 0;
2666 async_set_completion_callback( async
, free_connect_req
, req
);
2667 sock
->connect_req
= req
;
2668 queue_async( &sock
->connect_q
, async
);
2669 sock_reselect( sock
);
2670 set_error( STATUS_PENDING
);
2674 case IOCTL_AFD_WINE_SHUTDOWN
:
2678 if (get_req_data_size() < sizeof(int))
2680 set_error( STATUS_BUFFER_TOO_SMALL
);
2683 how
= *(int *)get_req_data();
2687 set_error( STATUS_INVALID_PARAMETER
);
2691 if (sock
->state
!= SOCK_CONNECTED
&& sock
->state
!= SOCK_CONNECTIONLESS
)
2693 set_error( STATUS_INVALID_CONNECTION
);
2699 sock
->rd_shutdown
= 1;
2701 if (how
!= SD_RECEIVE
)
2703 sock
->wr_shutdown
= 1;
2704 if (list_empty( &sock
->write_q
.queue
))
2705 shutdown( unix_fd
, SHUT_WR
);
2707 sock
->wr_shutdown_pending
= 1;
2712 if (sock
->event
) release_object( sock
->event
);
2716 sock
->nonblocking
= 1;
2719 sock_reselect( sock
);
2723 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE
:
2727 if (get_req_data_size() < sizeof(int))
2729 set_error( STATUS_BUFFER_TOO_SMALL
);
2732 force_async
= *(int *)get_req_data();
2734 if (sock
->nonblocking
&& !force_async
)
2736 set_error( STATUS_DEVICE_NOT_READY
);
2739 if (!sock_get_ifchange( sock
)) return;
2740 queue_async( &sock
->ifchange_q
, async
);
2741 set_error( STATUS_PENDING
);
2745 case IOCTL_AFD_WINE_FIONBIO
:
2746 if (get_req_data_size() < sizeof(int))
2748 set_error( STATUS_BUFFER_TOO_SMALL
);
2751 if (*(int *)get_req_data())
2753 sock
->nonblocking
= 1;
2759 set_error( STATUS_INVALID_PARAMETER
);
2762 sock
->nonblocking
= 0;
2766 case IOCTL_AFD_GET_EVENTS
:
2768 struct afd_get_events_params params
= {0};
2771 if (get_reply_max_size() < sizeof(params
))
2773 set_error( STATUS_INVALID_PARAMETER
);
2777 params
.flags
= sock
->pending_events
& sock
->mask
;
2778 for (i
= 0; i
< ARRAY_SIZE( params
.status
); ++i
)
2779 params
.status
[i
] = sock_get_ntstatus( sock
->errors
[i
] );
2781 sock
->pending_events
&= ~sock
->mask
;
2782 sock_reselect( sock
);
2784 set_reply_data( ¶ms
, sizeof(params
) );
2788 case IOCTL_AFD_EVENT_SELECT
:
2790 struct event
*event
= NULL
;
2791 obj_handle_t event_handle
;
2794 set_async_pending( async
);
2796 if (is_machine_64bit( current
->process
->machine
))
2798 const struct afd_event_select_params_64
*params
= get_req_data();
2800 if (get_req_data_size() < sizeof(*params
))
2802 set_error( STATUS_INVALID_PARAMETER
);
2806 event_handle
= params
->event
;
2807 mask
= params
->mask
;
2811 const struct afd_event_select_params_32
*params
= get_req_data();
2813 if (get_req_data_size() < sizeof(*params
))
2815 set_error( STATUS_INVALID_PARAMETER
);
2819 event_handle
= params
->event
;
2820 mask
= params
->mask
;
2823 if ((event_handle
|| mask
) &&
2824 !(event
= get_event_obj( current
->process
, event_handle
, EVENT_MODIFY_STATE
)))
2826 set_error( STATUS_INVALID_PARAMETER
);
2830 if (sock
->event
) release_object( sock
->event
);
2831 sock
->event
= event
;
2836 sock
->nonblocking
= 1;
2838 sock_reselect( sock
);
2839 /* Explicitly wake the socket up if the mask causes it to become
2840 * signaled. Note that reselecting isn't enough, since we might already
2841 * have had events recorded in sock->reported_events and we don't want
2842 * to select for them again. */
2843 sock_wake_up( sock
);
2848 case IOCTL_AFD_WINE_MESSAGE_SELECT
:
2850 const struct afd_message_select_params
*params
= get_req_data();
2852 if (get_req_data_size() < sizeof(params
))
2854 set_error( STATUS_BUFFER_TOO_SMALL
);
2858 if (sock
->event
) release_object( sock
->event
);
2862 sock
->pending_events
= 0;
2863 sock
->reported_events
= 0;
2866 sock
->mask
= params
->mask
;
2867 sock
->window
= params
->window
;
2868 sock
->message
= params
->message
;
2869 sock
->wparam
= params
->handle
;
2870 sock
->nonblocking
= 1;
2872 sock_reselect( sock
);
2877 case IOCTL_AFD_BIND
:
2879 const struct afd_bind_params
*params
= get_req_data();
2880 union unix_sockaddr unix_addr
, bind_addr
;
2881 data_size_t in_size
;
2885 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2887 if (get_req_data_size() < get_reply_max_size())
2889 set_error( STATUS_BUFFER_TOO_SMALL
);
2892 in_size
= get_req_data_size() - get_reply_max_size();
2893 if (in_size
< offsetof(struct afd_bind_params
, addr
.sa_data
)
2894 || get_reply_max_size() < in_size
- sizeof(int))
2896 set_error( STATUS_INVALID_PARAMETER
);
2902 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED
);
2906 unix_len
= sockaddr_to_unix( ¶ms
->addr
, in_size
- sizeof(int), &unix_addr
);
2909 set_error( STATUS_INVALID_ADDRESS
);
2912 bind_addr
= unix_addr
;
2914 if (unix_addr
.addr
.sa_family
== AF_INET
)
2916 if (!memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 )
2917 || bind_to_interface( sock
, &unix_addr
.in
))
2918 bind_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
2920 else if (unix_addr
.addr
.sa_family
== AF_INET6
)
2922 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2923 /* Windows allows specifying zero to use the default scope. Linux
2924 * interprets it as an interface index and requires that it be
2926 if (!unix_addr
.in6
.sin6_scope_id
)
2927 bind_addr
.in6
.sin6_scope_id
= get_ipv6_interface_index( &unix_addr
.in6
.sin6_addr
);
2931 set_async_pending( async
);
2934 if (sock
->family
== WS_AF_INET6
)
2936 socklen_t len
= sizeof(v6only
);
2938 getsockopt( get_unix_fd(sock
->fd
), IPPROTO_IPV6
, IPV6_V6ONLY
, &v6only
, &len
);
2942 if (check_addr_usage( sock
, &bind_addr
, v6only
))
2945 if (bind( unix_fd
, &bind_addr
.addr
, unix_len
) < 0)
2947 if (errno
== EADDRINUSE
&& sock
->reuseaddr
)
2950 set_error( sock_get_ntstatus( errno
) );
2956 unix_len
= sizeof(bind_addr
);
2957 if (!getsockname( unix_fd
, &bind_addr
.addr
, &unix_len
))
2959 /* store the interface or magic loopback address instead of the
2960 * actual unix address */
2961 if (bind_addr
.addr
.sa_family
== AF_INET
)
2962 bind_addr
.in
.sin_addr
= unix_addr
.in
.sin_addr
;
2963 sock
->addr_len
= sockaddr_from_unix( &bind_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2966 update_addr_usage( sock
, &bind_addr
, v6only
);
2968 if (get_reply_max_size() >= sock
->addr_len
)
2969 set_reply_data( &sock
->addr
, sock
->addr_len
);
2973 case IOCTL_AFD_GETSOCKNAME
:
2976 set_error( STATUS_INVALID_PARAMETER
);
2980 if (get_reply_max_size() < sock
->addr_len
)
2982 set_error( STATUS_BUFFER_TOO_SMALL
);
2986 set_reply_data( &sock
->addr
, sock
->addr_len
);
2989 case IOCTL_AFD_WINE_DEFER
:
2991 const obj_handle_t
*handle
= get_req_data();
2992 struct sock
*acceptsock
;
2994 if (get_req_data_size() < sizeof(*handle
))
2996 set_error( STATUS_BUFFER_TOO_SMALL
);
3000 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, *handle
, 0, &sock_ops
);
3001 if (!acceptsock
) return;
3003 sock
->deferred
= acceptsock
;
3007 case IOCTL_AFD_WINE_GET_INFO
:
3009 struct afd_get_info_params params
;
3011 if (get_reply_max_size() < sizeof(params
))
3013 set_error( STATUS_BUFFER_TOO_SMALL
);
3017 params
.family
= sock
->family
;
3018 params
.type
= sock
->type
;
3019 params
.protocol
= sock
->proto
;
3020 set_reply_data( ¶ms
, sizeof(params
) );
3024 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN
:
3026 int listening
= (sock
->state
== SOCK_LISTENING
);
3028 if (get_reply_max_size() < sizeof(listening
))
3030 set_error( STATUS_BUFFER_TOO_SMALL
);
3034 set_reply_data( &listening
, sizeof(listening
) );
3038 case IOCTL_AFD_WINE_GET_SO_ERROR
:
3043 if (get_reply_max_size() < sizeof(error
))
3045 set_error( STATUS_BUFFER_TOO_SMALL
);
3049 error
= sock_error( sock
);
3052 for (i
= 0; i
< ARRAY_SIZE( sock
->errors
); ++i
)
3054 if (sock
->errors
[i
])
3056 error
= sock
->errors
[i
];
3062 error
= sock_get_error( error
);
3063 set_reply_data( &error
, sizeof(error
) );
3067 case IOCTL_AFD_WINE_GET_SO_RCVBUF
:
3069 int rcvbuf
= sock
->rcvbuf
;
3071 if (get_reply_max_size() < sizeof(rcvbuf
))
3073 set_error( STATUS_BUFFER_TOO_SMALL
);
3077 set_reply_data( &rcvbuf
, sizeof(rcvbuf
) );
3081 case IOCTL_AFD_WINE_SET_SO_RCVBUF
:
3085 if (get_req_data_size() < sizeof(rcvbuf
))
3087 set_error( STATUS_BUFFER_TOO_SMALL
);
3090 rcvbuf
= *(DWORD
*)get_req_data();
3092 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_RCVBUF
, (char *)&rcvbuf
, sizeof(rcvbuf
) ))
3093 sock
->rcvbuf
= rcvbuf
;
3095 set_error( sock_get_ntstatus( errno
) );
3099 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO
:
3101 DWORD rcvtimeo
= sock
->rcvtimeo
;
3103 if (get_reply_max_size() < sizeof(rcvtimeo
))
3105 set_error( STATUS_BUFFER_TOO_SMALL
);
3109 set_reply_data( &rcvtimeo
, sizeof(rcvtimeo
) );
3113 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO
:
3117 if (get_req_data_size() < sizeof(rcvtimeo
))
3119 set_error( STATUS_BUFFER_TOO_SMALL
);
3122 rcvtimeo
= *(DWORD
*)get_req_data();
3124 sock
->rcvtimeo
= rcvtimeo
;
3128 /* BSD socket SO_REUSEADDR is not compatible with winsock semantics. */
3129 case IOCTL_AFD_WINE_SET_SO_REUSEADDR
:
3133 if (get_req_data_size() < sizeof(reuse
))
3135 set_error( STATUS_BUFFER_TOO_SMALL
);
3139 reuse
= *(int *)get_req_data();
3141 if (reuse
&& sock
->exclusiveaddruse
)
3143 set_error( STATUS_INVALID_PARAMETER
);
3147 if (is_tcp_socket( sock
))
3150 ret
= setsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuse
, sizeof(reuse
) );
3152 if (!ret
) ret
= setsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEPORT
, &reuse
, sizeof(reuse
) );
3155 set_error( sock_get_ntstatus( errno
) );
3157 sock
->reuseaddr
= !!reuse
;
3161 case IOCTL_AFD_WINE_SET_SO_EXCLUSIVEADDRUSE
:
3165 if (get_req_data_size() < sizeof(exclusive
))
3167 set_error( STATUS_BUFFER_TOO_SMALL
);
3171 exclusive
= *(int *)get_req_data();
3172 if (exclusive
&& sock
->reuseaddr
)
3174 set_error( STATUS_INVALID_PARAMETER
);
3177 sock
->exclusiveaddruse
= !!exclusive
;
3181 case IOCTL_AFD_WINE_GET_SO_SNDBUF
:
3183 int sndbuf
= sock
->sndbuf
;
3185 if (get_reply_max_size() < sizeof(sndbuf
))
3187 set_error( STATUS_BUFFER_TOO_SMALL
);
3191 set_reply_data( &sndbuf
, sizeof(sndbuf
) );
3195 case IOCTL_AFD_WINE_SET_SO_SNDBUF
:
3199 if (get_req_data_size() < sizeof(sndbuf
))
3201 set_error( STATUS_BUFFER_TOO_SMALL
);
3204 sndbuf
= *(DWORD
*)get_req_data();
3209 /* setsockopt fails if a zero value is passed */
3210 sock
->sndbuf
= sndbuf
;
3215 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_SNDBUF
, (char *)&sndbuf
, sizeof(sndbuf
) ))
3216 sock
->sndbuf
= sndbuf
;
3218 set_error( sock_get_ntstatus( errno
) );
3222 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO
:
3224 DWORD sndtimeo
= sock
->sndtimeo
;
3226 if (get_reply_max_size() < sizeof(sndtimeo
))
3228 set_error( STATUS_BUFFER_TOO_SMALL
);
3232 set_reply_data( &sndtimeo
, sizeof(sndtimeo
) );
3236 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO
:
3240 if (get_req_data_size() < sizeof(sndtimeo
))
3242 set_error( STATUS_BUFFER_TOO_SMALL
);
3245 sndtimeo
= *(DWORD
*)get_req_data();
3247 sock
->sndtimeo
= sndtimeo
;
3251 case IOCTL_AFD_WINE_GET_SO_CONNECT_TIME
:
3255 if (get_reply_max_size() < sizeof(time
))
3257 set_error( STATUS_BUFFER_TOO_SMALL
);
3261 if (sock
->state
== SOCK_CONNECTED
)
3262 time
= (current_time
- sock
->connect_time
) / 10000000;
3264 set_reply_data( &time
, sizeof(time
) );
3268 case IOCTL_AFD_WINE_GET_SO_REUSEADDR
:
3272 if (!get_reply_max_size())
3274 set_error( STATUS_BUFFER_TOO_SMALL
);
3278 reuse
= sock
->reuseaddr
;
3279 set_reply_data( &reuse
, min( sizeof(reuse
), get_reply_max_size() ));
3283 case IOCTL_AFD_WINE_GET_SO_EXCLUSIVEADDRUSE
:
3287 if (!get_reply_max_size())
3289 set_error( STATUS_BUFFER_TOO_SMALL
);
3293 exclusive
= sock
->exclusiveaddruse
;
3294 set_reply_data( &exclusive
, min( sizeof(exclusive
), get_reply_max_size() ));
3298 case IOCTL_AFD_POLL
:
3300 if (get_reply_max_size() < get_req_data_size())
3302 set_error( STATUS_INVALID_PARAMETER
);
3306 if (is_machine_64bit( current
->process
->machine
))
3308 const struct afd_poll_params_64
*params
= get_req_data();
3310 if (get_req_data_size() < sizeof(struct afd_poll_params_64
) ||
3311 get_req_data_size() < offsetof( struct afd_poll_params_64
, sockets
[params
->count
] ))
3313 set_error( STATUS_INVALID_PARAMETER
);
3317 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, params
->sockets
);
3321 const struct afd_poll_params_32
*params
= get_req_data();
3322 struct afd_poll_socket_64
*sockets
;
3325 if (get_req_data_size() < sizeof(struct afd_poll_params_32
) ||
3326 get_req_data_size() < offsetof( struct afd_poll_params_32
, sockets
[params
->count
] ))
3328 set_error( STATUS_INVALID_PARAMETER
);
3332 if (!(sockets
= mem_alloc( params
->count
* sizeof(*sockets
) ))) return;
3333 for (i
= 0; i
< params
->count
; ++i
)
3335 sockets
[i
].socket
= params
->sockets
[i
].socket
;
3336 sockets
[i
].flags
= params
->sockets
[i
].flags
;
3337 sockets
[i
].status
= params
->sockets
[i
].status
;
3340 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, sockets
);
3348 set_error( STATUS_NOT_SUPPORTED
);
3353 static void handle_exclusive_poll(struct poll_req
*req
)
3357 for (i
= 0; i
< req
->count
; ++i
)
3359 struct sock
*sock
= req
->sockets
[i
].sock
;
3360 struct poll_req
*main_poll
= sock
->main_poll
;
3362 if (main_poll
&& main_poll
->exclusive
&& req
->exclusive
)
3364 complete_async_poll( main_poll
, STATUS_SUCCESS
);
3369 sock
->main_poll
= req
;
3373 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
3374 unsigned int count
, const struct afd_poll_socket_64
*sockets
)
3376 BOOL signaled
= FALSE
;
3377 struct poll_req
*req
;
3382 set_error( STATUS_INVALID_PARAMETER
);
3386 if (!(req
= mem_alloc( offsetof( struct poll_req
, sockets
[count
] ) )))
3389 req
->timeout
= NULL
;
3391 if (timeout
&& timeout
!= TIMEOUT_INFINITE
&&
3392 !(req
->timeout
= add_timeout_user( timeout
, async_poll_timeout
, req
)))
3397 req
->orig_timeout
= timeout
;
3399 for (i
= 0; i
< count
; ++i
)
3401 req
->sockets
[i
].sock
= (struct sock
*)get_handle_obj( current
->process
, sockets
[i
].socket
, 0, &sock_ops
);
3402 if (!req
->sockets
[i
].sock
)
3404 for (j
= 0; j
< i
; ++j
) release_object( req
->sockets
[j
].sock
);
3405 if (req
->timeout
) remove_timeout_user( req
->timeout
);
3409 req
->sockets
[i
].handle
= sockets
[i
].socket
;
3410 req
->sockets
[i
].mask
= sockets
[i
].flags
;
3411 req
->sockets
[i
].flags
= 0;
3414 req
->exclusive
= exclusive
;
3416 req
->async
= (struct async
*)grab_object( async
);
3417 req
->iosb
= async_get_iosb( async
);
3419 handle_exclusive_poll(req
);
3421 list_add_tail( &poll_list
, &req
->entry
);
3422 async_set_completion_callback( async
, free_poll_req
, req
);
3423 queue_async( &poll_sock
->poll_q
, async
);
3425 for (i
= 0; i
< count
; ++i
)
3427 struct sock
*sock
= req
->sockets
[i
].sock
;
3428 int mask
= req
->sockets
[i
].mask
;
3429 struct pollfd pollfd
;
3431 pollfd
.fd
= get_unix_fd( sock
->fd
);
3432 pollfd
.events
= poll_flags_from_afd( sock
, mask
);
3433 if (pollfd
.events
>= 0 && poll( &pollfd
, 1, 0 ) >= 0)
3434 sock_poll_event( sock
->fd
, pollfd
.revents
);
3436 /* FIXME: do other error conditions deserve a similar treatment? */
3437 if (sock
->state
!= SOCK_CONNECTING
&& sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] && (mask
& AFD_POLL_CONNECT_ERR
))
3439 req
->sockets
[i
].flags
|= AFD_POLL_CONNECT_ERR
;
3440 req
->sockets
[i
].status
= sock_get_ntstatus( sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] );
3443 if (req
->sockets
[i
].flags
)
3447 if (!timeout
|| signaled
)
3448 complete_async_poll( req
, STATUS_SUCCESS
);
3452 for (i
= 0; i
< req
->count
; ++i
)
3453 sock_reselect( req
->sockets
[i
].sock
);
3454 set_error( STATUS_PENDING
);
3457 #ifdef HAVE_LINUX_RTNETLINK_H
3459 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
3460 static struct object
*ifchange_object
;
3462 static void ifchange_dump( struct object
*obj
, int verbose
);
3463 static struct fd
*ifchange_get_fd( struct object
*obj
);
3464 static void ifchange_destroy( struct object
*obj
);
3466 static int ifchange_get_poll_events( struct fd
*fd
);
3467 static void ifchange_poll_event( struct fd
*fd
, int event
);
3471 struct object obj
; /* object header */
3472 struct fd
*fd
; /* interface change file descriptor */
3473 struct list sockets
; /* list of sockets to send interface change notifications */
3476 static const struct object_ops ifchange_ops
=
3478 sizeof(struct ifchange
), /* size */
3479 &no_type
, /* type */
3480 ifchange_dump
, /* dump */
3481 no_add_queue
, /* add_queue */
3482 NULL
, /* remove_queue */
3483 NULL
, /* signaled */
3484 no_satisfied
, /* satisfied */
3485 no_signal
, /* signal */
3486 ifchange_get_fd
, /* get_fd */
3487 default_map_access
, /* map_access */
3488 default_get_sd
, /* get_sd */
3489 default_set_sd
, /* set_sd */
3490 no_get_full_name
, /* get_full_name */
3491 no_lookup_name
, /* lookup_name */
3492 no_link_name
, /* link_name */
3493 NULL
, /* unlink_name */
3494 no_open_file
, /* open_file */
3495 no_kernel_obj_list
, /* get_kernel_obj_list */
3496 no_close_handle
, /* close_handle */
3497 ifchange_destroy
/* destroy */
3500 static const struct fd_ops ifchange_fd_ops
=
3502 ifchange_get_poll_events
, /* get_poll_events */
3503 ifchange_poll_event
, /* poll_event */
3504 NULL
, /* get_fd_type */
3505 no_fd_read
, /* read */
3506 no_fd_write
, /* write */
3507 no_fd_flush
, /* flush */
3508 no_fd_get_file_info
, /* get_file_info */
3509 no_fd_get_volume_info
, /* get_volume_info */
3510 no_fd_ioctl
, /* ioctl */
3511 NULL
, /* cancel_async */
3512 NULL
, /* queue_async */
3513 NULL
/* reselect_async */
3516 static void ifchange_dump( struct object
*obj
, int verbose
)
3518 assert( obj
->ops
== &ifchange_ops
);
3519 fprintf( stderr
, "Interface change\n" );
3522 static struct fd
*ifchange_get_fd( struct object
*obj
)
3524 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3525 return (struct fd
*)grab_object( ifchange
->fd
);
3528 static void ifchange_destroy( struct object
*obj
)
3530 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3531 assert( obj
->ops
== &ifchange_ops
);
3533 release_object( ifchange
->fd
);
3535 /* reset the global ifchange object so that it will be recreated if it is needed again */
3536 assert( obj
== ifchange_object
);
3537 ifchange_object
= NULL
;
3540 static int ifchange_get_poll_events( struct fd
*fd
)
3545 /* wake up all the sockets waiting for a change notification event */
3546 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
3548 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3549 struct list
*ptr
, *next
;
3550 assert( obj
->ops
== &ifchange_ops
);
3551 assert( obj
== ifchange_object
);
3553 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
3555 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
3557 assert( sock
->ifchange_obj
);
3558 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
3559 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
3563 static void ifchange_poll_event( struct fd
*fd
, int event
)
3565 struct object
*ifchange
= get_fd_user( fd
);
3566 unsigned int status
= STATUS_PENDING
;
3567 char buffer
[PIPE_BUF
];
3570 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
3573 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
3574 return; /* retry when poll() says the socket is ready */
3575 status
= sock_get_ntstatus( errno
);
3579 struct nlmsghdr
*nlh
;
3581 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
3583 if (nlh
->nlmsg_type
== NLMSG_DONE
)
3585 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
3586 status
= STATUS_SUCCESS
;
3589 else status
= STATUS_CANCELLED
;
3591 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
3596 /* we only need one of these interface notification objects, all of the sockets dependent upon
3597 * it will wake up when a notification event occurs */
3598 static struct object
*get_ifchange( void )
3600 #ifdef HAVE_LINUX_RTNETLINK_H
3601 struct ifchange
*ifchange
;
3602 struct sockaddr_nl addr
;
3605 if (ifchange_object
)
3607 /* increment the refcount for each socket that uses the ifchange object */
3608 return grab_object( ifchange_object
);
3611 /* create the socket we need for processing interface change notifications */
3612 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
3615 set_error( sock_get_ntstatus( errno
));
3618 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
3619 memset( &addr
, 0, sizeof(addr
) );
3620 addr
.nl_family
= AF_NETLINK
;
3621 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
3622 /* bind the socket to the special netlink kernel interface */
3623 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
3626 set_error( sock_get_ntstatus( errno
));
3629 if (!(ifchange
= alloc_object( &ifchange_ops
)))
3632 set_error( STATUS_NO_MEMORY
);
3635 list_init( &ifchange
->sockets
);
3636 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
3638 release_object( ifchange
);
3639 set_error( STATUS_NO_MEMORY
);
3642 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
3644 /* the ifchange object is now successfully configured */
3645 ifchange_object
= &ifchange
->obj
;
3646 return &ifchange
->obj
;
3648 set_error( STATUS_NOT_SUPPORTED
);
3653 /* add the socket to the interface change notification list */
3654 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
3656 #ifdef HAVE_LINUX_RTNETLINK_H
3657 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3659 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
3663 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3664 static struct object
*sock_get_ifchange( struct sock
*sock
)
3666 struct object
*ifchange
;
3668 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
3669 return sock
->ifchange_obj
;
3671 if (!(ifchange
= get_ifchange()))
3674 /* add the socket to the ifchange notification list */
3675 ifchange_add_sock( ifchange
, sock
);
3676 sock
->ifchange_obj
= ifchange
;
3680 /* destroy an existing ifchange queue for a specific socket */
3681 static void sock_release_ifchange( struct sock
*sock
)
3683 if (sock
->ifchange_obj
)
3685 list_remove( &sock
->ifchange_entry
);
3686 release_object( sock
->ifchange_obj
);
3687 sock
->ifchange_obj
= NULL
;
3691 static void socket_device_dump( struct object
*obj
, int verbose
);
3692 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3693 unsigned int attr
, struct object
*root
);
3694 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3695 unsigned int sharing
, unsigned int options
);
3697 static const struct object_ops socket_device_ops
=
3699 sizeof(struct object
), /* size */
3700 &device_type
, /* type */
3701 socket_device_dump
, /* dump */
3702 no_add_queue
, /* add_queue */
3703 NULL
, /* remove_queue */
3704 NULL
, /* signaled */
3705 no_satisfied
, /* satisfied */
3706 no_signal
, /* signal */
3707 no_get_fd
, /* get_fd */
3708 default_map_access
, /* map_access */
3709 default_get_sd
, /* get_sd */
3710 default_set_sd
, /* set_sd */
3711 default_get_full_name
, /* get_full_name */
3712 socket_device_lookup_name
, /* lookup_name */
3713 directory_link_name
, /* link_name */
3714 default_unlink_name
, /* unlink_name */
3715 socket_device_open_file
, /* open_file */
3716 no_kernel_obj_list
, /* get_kernel_obj_list */
3717 no_close_handle
, /* close_handle */
3718 no_destroy
/* destroy */
3721 static void socket_device_dump( struct object
*obj
, int verbose
)
3723 fputs( "Socket device\n", stderr
);
3726 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3727 unsigned int attr
, struct object
*root
)
3729 if (name
) name
->len
= 0;
3733 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3734 unsigned int sharing
, unsigned int options
)
3738 if (!(sock
= create_socket())) return NULL
;
3739 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
3741 release_object( sock
);
3747 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
3748 unsigned int attr
, const struct security_descriptor
*sd
)
3750 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
3753 DECL_HANDLER(recv_socket
)
3755 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3756 unsigned int status
= STATUS_PENDING
;
3757 timeout_t timeout
= 0;
3758 struct async
*async
;
3764 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3765 timeout
= (timeout_t
)sock
->rcvtimeo
* -10000;
3767 if (sock
->rd_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3768 else if (!async_queued( &sock
->read_q
))
3770 /* If read_q is not empty, we cannot really tell if the already queued
3771 * asyncs will not consume all available data; if there's no data
3772 * available, the current request won't be immediately satiable.
3774 if ((!req
->force_async
&& sock
->nonblocking
) ||
3775 check_fd_events( sock
->fd
, req
->oob
&& !is_oobinline( sock
) ? POLLPRI
: POLLIN
))
3777 /* Give the client opportunity to complete synchronously.
3778 * If it turns out that the I/O request is not actually immediately satiable,
3779 * the client may then choose to re-queue the async (with STATUS_PENDING).
3781 * Note: If the nonblocking flag is set, we don't poll the socket
3782 * here and always opt for synchronous completion first. This is
3783 * because the application has probably seen POLLIN already from a
3784 * preceding select()/poll() call before it requested to receive
3787 status
= STATUS_ALERTED
;
3791 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3792 status
= STATUS_DEVICE_NOT_READY
;
3794 sock
->pending_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3795 sock
->reported_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3797 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3799 set_error( status
);
3802 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3804 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3805 queue_async( &sock
->read_q
, async
);
3807 /* always reselect; we changed reported_events above */
3808 sock_reselect( sock
);
3810 reply
->wait
= async_handoff( async
, NULL
, 0 );
3811 reply
->options
= get_fd_options( fd
);
3812 reply
->nonblocking
= sock
->nonblocking
;
3813 release_object( async
);
3815 release_object( sock
);
3818 static void send_socket_completion_callback( void *private )
3820 struct send_req
*send_req
= private;
3821 struct iosb
*iosb
= send_req
->iosb
;
3822 struct sock
*sock
= send_req
->sock
;
3824 if (iosb
->status
!= STATUS_SUCCESS
)
3826 /* send() calls only clear and reselect events if unsuccessful. */
3827 sock
->pending_events
&= ~AFD_POLL_WRITE
;
3828 sock
->reported_events
&= ~AFD_POLL_WRITE
;
3829 sock_reselect( sock
);
3832 release_object( iosb
);
3833 release_object( sock
);
3837 DECL_HANDLER(send_socket
)
3839 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3840 unsigned int status
= STATUS_PENDING
;
3841 timeout_t timeout
= 0;
3842 struct async
*async
;
3849 if (sock
->type
== WS_SOCK_DGRAM
&& !sock
->bound
)
3851 union unix_sockaddr unix_addr
;
3853 int unix_fd
= get_unix_fd( fd
);
3855 unix_len
= get_unix_sockaddr_any( &unix_addr
, sock
->family
);
3856 if (bind( unix_fd
, &unix_addr
.addr
, unix_len
) < 0)
3859 if (getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
) >= 0)
3861 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
3864 else if (!bind_errno
) bind_errno
= errno
;
3867 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3868 timeout
= (timeout_t
)sock
->sndtimeo
* -10000;
3870 if (bind_errno
) status
= sock_get_ntstatus( bind_errno
);
3871 else if (sock
->wr_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3872 else if (!async_queued( &sock
->write_q
))
3874 /* If write_q is not empty, we cannot really tell if the already queued
3875 * asyncs will not consume all available space; if there's no space
3876 * available, the current request won't be immediately satiable.
3878 if ((!req
->force_async
&& sock
->nonblocking
) || check_fd_events( sock
->fd
, POLLOUT
))
3880 /* Give the client opportunity to complete synchronously.
3881 * If it turns out that the I/O request is not actually immediately satiable,
3882 * the client may then choose to re-queue the async (with STATUS_PENDING).
3884 * Note: If the nonblocking flag is set, we don't poll the socket
3885 * here and always opt for synchronous completion first. This is
3886 * because the application has probably seen POLLOUT already from a
3887 * preceding select()/poll() call before it requested to send data.
3889 * Furthermore, some applications expect that any send() call on a
3890 * socket that has indicated POLLOUT beforehand never fails with
3891 * WSAEWOULDBLOCK. It's possible that Linux poll() may yield
3892 * POLLOUT on the first call but not the second, even if no send()
3893 * call has been made in the meanwhile. This can happen for a
3894 * number of reasons; for example, TCP fragmentation may consume
3895 * extra buffer space for each packet that has been split out, or
3896 * the TCP/IP networking stack may decide to shrink the send buffer
3897 * due to memory pressure.
3899 status
= STATUS_ALERTED
;
3903 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3904 status
= STATUS_DEVICE_NOT_READY
;
3906 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3908 struct send_req
*send_req
;
3909 struct iosb
*iosb
= async_get_iosb( async
);
3911 if ((send_req
= mem_alloc( sizeof(*send_req
) )))
3913 send_req
->iosb
= (struct iosb
*)grab_object( iosb
);
3914 send_req
->sock
= (struct sock
*)grab_object( sock
);
3915 async_set_completion_callback( async
, send_socket_completion_callback
, send_req
);
3917 else if (status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
)
3918 status
= STATUS_NO_MEMORY
;
3920 release_object( iosb
);
3922 set_error( status
);
3925 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3927 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3929 queue_async( &sock
->write_q
, async
);
3930 sock_reselect( sock
);
3933 reply
->wait
= async_handoff( async
, NULL
, 0 );
3934 reply
->options
= get_fd_options( fd
);
3935 reply
->nonblocking
= sock
->nonblocking
;
3936 release_object( async
);
3938 release_object( sock
);
3941 DECL_HANDLER(socket_send_icmp_id
)
3943 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, 0, &sock_ops
);
3947 if (sock
->icmp_fixup_data_len
== MAX_ICMP_HISTORY_LENGTH
)
3949 memmove( sock
->icmp_fixup_data
, sock
->icmp_fixup_data
+ 1,
3950 sizeof(*sock
->icmp_fixup_data
) * (MAX_ICMP_HISTORY_LENGTH
- 1) );
3951 --sock
->icmp_fixup_data_len
;
3954 sock
->icmp_fixup_data
[sock
->icmp_fixup_data_len
].icmp_id
= req
->icmp_id
;
3955 sock
->icmp_fixup_data
[sock
->icmp_fixup_data_len
].icmp_seq
= req
->icmp_seq
;
3956 ++sock
->icmp_fixup_data_len
;
3958 release_object( sock
);
3961 DECL_HANDLER(socket_get_icmp_id
)
3963 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, 0, &sock_ops
);
3968 for (i
= 0; i
< sock
->icmp_fixup_data_len
; ++i
)
3970 if (sock
->icmp_fixup_data
[i
].icmp_seq
== req
->icmp_seq
)
3972 reply
->icmp_id
= sock
->icmp_fixup_data
[i
].icmp_id
;
3973 --sock
->icmp_fixup_data_len
;
3974 memmove( &sock
->icmp_fixup_data
[i
], &sock
->icmp_fixup_data
[i
+ 1],
3975 (sock
->icmp_fixup_data_len
- i
) * sizeof(*sock
->icmp_fixup_data
) );
3976 release_object( sock
);
3981 set_error( STATUS_NOT_FOUND
);
3982 release_object( sock
);