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
);
1658 return async_close_obj_handle( obj
, process
, handle
);
1661 static void sock_destroy( struct object
*obj
)
1663 struct sock
*sock
= (struct sock
*)obj
;
1666 assert( obj
->ops
== &sock_ops
);
1668 /* FIXME: special socket shutdown stuff? */
1670 for (i
= 0; i
< 2; ++i
)
1672 if (sock
->bound_addr
[i
] && --sock
->bound_addr
[i
]->reuse_count
<= 0)
1674 rb_remove( &bound_addresses_tree
, &sock
->bound_addr
[i
]->entry
);
1675 free( sock
->bound_addr
[i
] );
1679 if ( sock
->deferred
)
1680 release_object( sock
->deferred
);
1682 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
1683 sock_release_ifchange( sock
);
1684 free_async_queue( &sock
->read_q
);
1685 free_async_queue( &sock
->write_q
);
1686 free_async_queue( &sock
->ifchange_q
);
1687 free_async_queue( &sock
->accept_q
);
1688 free_async_queue( &sock
->connect_q
);
1689 free_async_queue( &sock
->poll_q
);
1690 if (sock
->event
) release_object( sock
->event
);
1691 if (sock
->fd
) release_object( sock
->fd
);
1694 static struct sock
*create_socket(void)
1698 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
1700 sock
->state
= SOCK_UNCONNECTED
;
1702 sock
->pending_events
= 0;
1703 sock
->reported_events
= 0;
1711 sock
->connect_time
= 0;
1712 sock
->deferred
= NULL
;
1713 sock
->ifchange_obj
= NULL
;
1714 sock
->accept_recv_req
= NULL
;
1715 sock
->connect_req
= NULL
;
1716 sock
->main_poll
= NULL
;
1717 memset( &sock
->addr
, 0, sizeof(sock
->addr
) );
1719 sock
->rd_shutdown
= 0;
1720 sock
->wr_shutdown
= 0;
1721 sock
->wr_shutdown_pending
= 0;
1724 sock
->nonblocking
= 0;
1727 sock
->reuseaddr
= 0;
1728 sock
->exclusiveaddruse
= 0;
1733 sock
->icmp_fixup_data_len
= 0;
1734 sock
->bound_addr
[0] = sock
->bound_addr
[1] = NULL
;
1735 init_async_queue( &sock
->read_q
);
1736 init_async_queue( &sock
->write_q
);
1737 init_async_queue( &sock
->ifchange_q
);
1738 init_async_queue( &sock
->accept_q
);
1739 init_async_queue( &sock
->connect_q
);
1740 init_async_queue( &sock
->poll_q
);
1741 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
1742 list_init( &sock
->accept_list
);
1746 static int get_unix_family( int family
)
1750 case WS_AF_INET
: return AF_INET
;
1751 case WS_AF_INET6
: return AF_INET6
;
1753 case WS_AF_IPX
: return AF_IPX
;
1756 case WS_AF_IRDA
: return AF_IRDA
;
1758 case WS_AF_UNSPEC
: return AF_UNSPEC
;
1763 static int get_unix_type( int type
)
1767 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
1768 case WS_SOCK_RAW
: return SOCK_RAW
;
1769 case WS_SOCK_STREAM
: return SOCK_STREAM
;
1774 static int get_unix_protocol( int protocol
)
1776 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1781 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
1782 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1783 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1784 case WS_IPPROTO_IPV4
: return IPPROTO_IPIP
;
1785 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1786 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1787 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1788 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1793 static void set_dont_fragment( int fd
, int level
, int value
)
1797 if (level
== IPPROTO_IP
)
1800 optname
= IP_DONTFRAG
;
1801 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1802 optname
= IP_MTU_DISCOVER
;
1803 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1810 #ifdef IPV6_DONTFRAG
1811 optname
= IPV6_DONTFRAG
;
1812 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1813 optname
= IPV6_MTU_DISCOVER
;
1814 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1820 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1823 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
)
1825 unsigned int options
= 0;
1826 int sockfd
, unix_type
, unix_family
, unix_protocol
, value
;
1829 unix_family
= get_unix_family( family
);
1830 unix_type
= get_unix_type( type
);
1831 unix_protocol
= get_unix_protocol( protocol
);
1833 if (unix_protocol
< 0)
1835 if (type
&& unix_type
< 0)
1836 set_win32_error( WSAESOCKTNOSUPPORT
);
1838 set_win32_error( WSAEPROTONOSUPPORT
);
1841 if (unix_family
< 0)
1843 if (family
>= 0 && unix_type
< 0)
1844 set_win32_error( WSAESOCKTNOSUPPORT
);
1846 set_win32_error( WSAEAFNOSUPPORT
);
1850 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1853 if (sockfd
== -1 && errno
== EPERM
&& unix_family
== AF_INET
1854 && unix_type
== SOCK_RAW
&& unix_protocol
== IPPROTO_ICMP
)
1856 sockfd
= socket( unix_family
, SOCK_DGRAM
, unix_protocol
);
1861 setsockopt( sockfd
, IPPROTO_IP
, IP_RECVTTL
, (const char *)&val
, sizeof(val
) );
1862 setsockopt( sockfd
, IPPROTO_IP
, IP_RECVTOS
, (const char *)&val
, sizeof(val
) );
1863 setsockopt( sockfd
, IPPROTO_IP
, IP_PKTINFO
, (const char *)&val
, sizeof(val
) );
1870 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1871 else set_win32_error( sock_get_error( errno
));
1874 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1876 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1879 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1882 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1885 /* Should we retrieve val using a getsockopt call and then
1886 * set the modified one? */
1887 val
.ipx_pt
= ipx_type
;
1888 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1893 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1895 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1896 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1897 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1898 else if (unix_type
== SOCK_STREAM
)
1899 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1903 if (unix_family
== AF_INET6
)
1905 static const int enable
= 1;
1906 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1910 len
= sizeof(value
);
1911 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_RCVBUF
, &value
, &len
))
1912 sock
->rcvbuf
= value
;
1914 len
= sizeof(value
);
1915 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_SNDBUF
, &value
, &len
))
1916 sock
->sndbuf
= value
;
1918 sock
->state
= (type
== WS_SOCK_STREAM
? SOCK_UNCONNECTED
: SOCK_CONNECTIONLESS
);
1919 sock
->proto
= protocol
;
1921 sock
->family
= family
;
1923 if (is_tcp_socket( sock
))
1926 setsockopt( sockfd
, SOL_SOCKET
, SO_REUSEADDR
, &value
, sizeof(value
) );
1929 setsockopt( sockfd
, IPPROTO_TCP
, TCP_SYNCNT
, &value
, sizeof(value
) );
1935 options
= get_fd_options( sock
->fd
);
1936 release_object( sock
->fd
);
1939 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1944 /* We can't immediately allow caching for a connection-mode socket, since it
1945 * might be accepted into (changing the underlying fd object.) */
1946 if (sock
->type
!= WS_SOCK_STREAM
) allow_fd_caching( sock
->fd
);
1951 /* accepts a socket and inits it */
1952 static int accept_new_fd( struct sock
*sock
)
1955 /* Try to accept(2). We can't be safe that this an already connected socket
1956 * or that accept() is allowed on it. In those cases we will get -1/errno
1959 struct sockaddr saddr
;
1960 socklen_t slen
= sizeof(saddr
);
1961 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1963 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1965 set_error( sock_get_ntstatus( errno
));
1969 /* accept a socket (creates a new fd) */
1970 static struct sock
*accept_socket( struct sock
*sock
)
1972 struct sock
*acceptsock
;
1975 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1977 if ( sock
->deferred
)
1979 acceptsock
= sock
->deferred
;
1980 sock
->deferred
= NULL
;
1984 union unix_sockaddr unix_addr
;
1987 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1988 if (!(acceptsock
= create_socket()))
1994 /* newly created socket gets the same properties of the listening socket */
1995 acceptsock
->state
= SOCK_CONNECTED
;
1996 acceptsock
->bound
= 1;
1997 acceptsock
->nonblocking
= sock
->nonblocking
;
1998 acceptsock
->mask
= sock
->mask
;
1999 acceptsock
->proto
= sock
->proto
;
2000 acceptsock
->type
= sock
->type
;
2001 acceptsock
->family
= sock
->family
;
2002 acceptsock
->window
= sock
->window
;
2003 acceptsock
->message
= sock
->message
;
2004 acceptsock
->reuseaddr
= sock
->reuseaddr
;
2005 acceptsock
->exclusiveaddruse
= sock
->exclusiveaddruse
;
2006 acceptsock
->sndbuf
= sock
->sndbuf
;
2007 acceptsock
->rcvbuf
= sock
->rcvbuf
;
2008 acceptsock
->sndtimeo
= sock
->sndtimeo
;
2009 acceptsock
->rcvtimeo
= sock
->rcvtimeo
;
2010 acceptsock
->connect_time
= current_time
;
2012 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
2013 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
2014 get_fd_options( sock
->fd
) )))
2016 release_object( acceptsock
);
2019 unix_len
= sizeof(unix_addr
);
2020 if (!getsockname( acceptfd
, &unix_addr
.addr
, &unix_len
))
2021 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
2024 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
2025 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
2026 sock_reselect( sock
);
2030 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
2032 union unix_sockaddr unix_addr
;
2037 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
2039 if ( sock
->deferred
)
2041 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
2042 get_fd_options( acceptsock
->fd
) );
2046 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
2048 release_object( sock
->deferred
);
2049 sock
->deferred
= NULL
;
2053 if ((acceptfd
= accept_new_fd( sock
)) == -1)
2056 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
2057 get_fd_options( acceptsock
->fd
) )))
2061 acceptsock
->state
= SOCK_CONNECTED
;
2062 acceptsock
->bound
= 1;
2063 acceptsock
->pending_events
= 0;
2064 acceptsock
->reported_events
= 0;
2065 acceptsock
->proto
= sock
->proto
;
2066 acceptsock
->type
= sock
->type
;
2067 acceptsock
->family
= sock
->family
;
2068 acceptsock
->wparam
= 0;
2069 acceptsock
->deferred
= NULL
;
2070 acceptsock
->connect_time
= current_time
;
2071 fd_copy_completion( acceptsock
->fd
, newfd
);
2072 release_object( acceptsock
->fd
);
2073 acceptsock
->fd
= newfd
;
2075 unix_len
= sizeof(unix_addr
);
2076 if (!getsockname( get_unix_fd( newfd
), &unix_addr
.addr
, &unix_len
))
2077 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
2080 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
2081 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
2082 sock_reselect( sock
);
2089 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
2091 static const int enable
= 1;
2094 if (!(index
= if_nametoindex( name
)))
2097 if (setsockopt( fd
, IPPROTO_IP
, IP_BOUND_IF
, &index
, sizeof(index
) ))
2100 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
2103 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER) && defined(SO_BINDTODEVICE)
2105 struct interface_filter
2107 struct sock_filter iface_memaddr
;
2108 struct sock_filter iface_rule
;
2109 struct sock_filter ip_memaddr
;
2110 struct sock_filter ip_rule
;
2111 struct sock_filter return_keep
;
2112 struct sock_filter return_dump
;
2114 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
2115 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
2116 /sizeof(struct sock_filter)
2117 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
2118 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
2119 /sizeof(struct sock_filter)
2120 # define FILTER_JUMP_NEXT() (u_char)(0)
2121 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
2122 static struct interface_filter generic_interface_filter
=
2124 /* This filter rule allows incoming packets on the specified interface, which works for all
2125 * remotely generated packets and for locally generated broadcast packets. */
2126 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
2127 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
2128 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
2129 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
2130 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
2131 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
2132 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
2133 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
2136 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
2138 struct interface_filter specific_interface_filter
;
2139 struct sock_fprog filter_prog
;
2140 static const int enable
= 1;
2144 if (!setsockopt( fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen( name
) + 1 ))
2147 /* SO_BINDTODEVICE requires NET_CAP_RAW until Linux 5.7. */
2149 fprintf( stderr
, "setsockopt SO_BINDTODEVICE fd %d, name %s failed: %s, falling back to SO_REUSE_ADDR\n",
2150 fd
, name
, strerror( errno
));
2152 if (!(index
= if_nametoindex( name
)))
2155 ifindex
= htonl( index
);
2156 if (setsockopt( fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
) ) < 0)
2159 specific_interface_filter
= generic_interface_filter
;
2160 specific_interface_filter
.iface_rule
.k
= index
;
2161 specific_interface_filter
.ip_rule
.k
= htonl( bind_addr
);
2162 filter_prog
.len
= sizeof(generic_interface_filter
) / sizeof(struct sock_filter
);
2163 filter_prog
.filter
= (struct sock_filter
*)&specific_interface_filter
;
2164 if (setsockopt( fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
) ))
2167 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
2172 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
2178 #endif /* LINUX_BOUND_IF */
2180 /* Take bind() calls on any name corresponding to a local network adapter and
2181 * restrict the given socket to operating only on the specified interface. This
2182 * restriction consists of two components:
2183 * 1) An outgoing packet restriction suggesting the egress interface for all
2185 * 2) An incoming packet restriction dropping packets not meant for the
2187 * If the function succeeds in placing these restrictions, then the name for the
2188 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
2189 * receipt of broadcast packets on the socket. This behavior is only relevant to
2190 * UDP sockets and is needed for applications that expect to be able to receive
2191 * broadcast packets on a socket that is bound to a specific network interface.
2193 static int bind_to_interface( struct sock
*sock
, const struct sockaddr_in
*addr
)
2195 in_addr_t bind_addr
= addr
->sin_addr
.s_addr
;
2196 struct ifaddrs
*ifaddrs
, *ifaddr
;
2197 int fd
= get_unix_fd( sock
->fd
);
2200 if (bind_addr
== htonl( INADDR_ANY
) || bind_addr
== htonl( INADDR_LOOPBACK
))
2202 if (sock
->type
!= WS_SOCK_DGRAM
)
2205 if (getifaddrs( &ifaddrs
) < 0) return 0;
2207 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
2209 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET
2210 && ((struct sockaddr_in
*)ifaddr
->ifa_addr
)->sin_addr
.s_addr
== bind_addr
)
2212 if ((err
= bind_to_iface_name( fd
, bind_addr
, ifaddr
->ifa_name
)) < 0)
2215 fprintf( stderr
, "failed to bind to interface: %s\n", strerror( errno
) );
2220 freeifaddrs( ifaddrs
);
2224 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2225 static unsigned int get_ipv6_interface_index( const struct in6_addr
*addr
)
2227 struct ifaddrs
*ifaddrs
, *ifaddr
;
2229 if (getifaddrs( &ifaddrs
) < 0) return 0;
2231 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
2233 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET6
2234 && !memcmp( &((struct sockaddr_in6
*)ifaddr
->ifa_addr
)->sin6_addr
, addr
, sizeof(*addr
) ))
2236 unsigned int index
= if_nametoindex( ifaddr
->ifa_name
);
2241 fprintf( stderr
, "Unable to look up interface index for %s: %s\n",
2242 ifaddr
->ifa_name
, strerror( errno
) );
2246 freeifaddrs( ifaddrs
);
2251 freeifaddrs( ifaddrs
);
2256 /* return an errno value mapped to a WSA error */
2257 static unsigned int sock_get_error( int err
)
2261 case EINTR
: return WSAEINTR
;
2262 case EBADF
: return WSAEBADF
;
2264 case EACCES
: return WSAEACCES
;
2265 case EFAULT
: return WSAEFAULT
;
2266 case EINVAL
: return WSAEINVAL
;
2267 case EMFILE
: return WSAEMFILE
;
2269 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
2270 case EALREADY
: return WSAEALREADY
;
2271 case ENOTSOCK
: return WSAENOTSOCK
;
2272 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
2273 case EMSGSIZE
: return WSAEMSGSIZE
;
2274 case EPROTOTYPE
: return WSAEPROTOTYPE
;
2275 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
2276 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
2277 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
2278 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
2279 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
2280 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
2281 case EADDRINUSE
: return WSAEADDRINUSE
;
2282 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
2283 case ENETDOWN
: return WSAENETDOWN
;
2284 case ENETUNREACH
: return WSAENETUNREACH
;
2285 case ENETRESET
: return WSAENETRESET
;
2286 case ECONNABORTED
: return WSAECONNABORTED
;
2288 case ECONNRESET
: return WSAECONNRESET
;
2289 case ENOBUFS
: return WSAENOBUFS
;
2290 case EISCONN
: return WSAEISCONN
;
2291 case ENOTCONN
: return WSAENOTCONN
;
2292 case ESHUTDOWN
: return WSAESHUTDOWN
;
2293 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
2294 case ETIMEDOUT
: return WSAETIMEDOUT
;
2295 case ECONNREFUSED
: return WSAECONNREFUSED
;
2296 case ELOOP
: return WSAELOOP
;
2297 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
2298 case EHOSTDOWN
: return WSAEHOSTDOWN
;
2299 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
2300 case ENOTEMPTY
: return WSAENOTEMPTY
;
2302 case EPROCLIM
: return WSAEPROCLIM
;
2305 case EUSERS
: return WSAEUSERS
;
2308 case EDQUOT
: return WSAEDQUOT
;
2311 case ESTALE
: return WSAESTALE
;
2314 case EREMOTE
: return WSAEREMOTE
;
2320 perror("wineserver: sock_get_error() can't map error");
2325 static int sock_get_ntstatus( int err
)
2329 case EBADF
: return STATUS_INVALID_HANDLE
;
2330 case EBUSY
: return STATUS_DEVICE_BUSY
;
2332 case EACCES
: return STATUS_ACCESS_DENIED
;
2333 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
2334 case EINVAL
: return STATUS_INVALID_PARAMETER
;
2336 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
2338 case EWOULDBLOCK
: return STATUS_DEVICE_NOT_READY
;
2339 case EALREADY
: return STATUS_NETWORK_BUSY
;
2340 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
2341 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
2342 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
2343 case EPROTONOSUPPORT
:
2344 case ESOCKTNOSUPPORT
:
2347 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
2348 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
2349 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
2350 case EADDRINUSE
: return STATUS_SHARING_VIOLATION
;
2351 /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
2352 * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
2354 case EADDRNOTAVAIL
: return STATUS_INVALID_ADDRESS_COMPONENT
;
2355 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
2356 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
2357 case ENOTCONN
: return STATUS_INVALID_CONNECTION
;
2358 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
2359 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
2360 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
2361 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
2363 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
2364 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
2365 case EISCONN
: return STATUS_CONNECTION_ACTIVE
;
2367 case 0: return STATUS_SUCCESS
;
2370 perror("wineserver: sock_get_ntstatus() can't map error");
2371 return STATUS_UNSUCCESSFUL
;
2375 static struct accept_req
*alloc_accept_req( struct sock
*sock
, struct sock
*acceptsock
, struct async
*async
,
2376 const struct afd_accept_into_params
*params
)
2378 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
2382 req
->async
= (struct async
*)grab_object( async
);
2383 req
->iosb
= async_get_iosb( async
);
2384 req
->sock
= (struct sock
*)grab_object( sock
);
2385 req
->acceptsock
= acceptsock
;
2386 if (acceptsock
) grab_object( acceptsock
);
2392 req
->recv_len
= params
->recv_len
;
2393 req
->local_len
= params
->local_len
;
2399 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2401 struct sock
*sock
= get_fd_user( fd
);
2404 assert( sock
->obj
.ops
== &sock_ops
);
2406 if (code
!= IOCTL_AFD_WINE_CREATE
&& code
!= IOCTL_AFD_POLL
&& (unix_fd
= get_unix_fd( fd
)) < 0)
2411 case IOCTL_AFD_WINE_CREATE
:
2413 const struct afd_create_params
*params
= get_req_data();
2415 if (get_req_data_size() != sizeof(*params
))
2417 set_error( STATUS_INVALID_PARAMETER
);
2420 init_socket( sock
, params
->family
, params
->type
, params
->protocol
);
2424 case IOCTL_AFD_WINE_ACCEPT
:
2426 struct sock
*acceptsock
;
2427 obj_handle_t handle
;
2429 if (get_reply_max_size() != sizeof(handle
))
2431 set_error( STATUS_BUFFER_TOO_SMALL
);
2435 if (!(acceptsock
= accept_socket( sock
)))
2437 struct accept_req
*req
;
2439 if (sock
->nonblocking
) return;
2440 if (get_error() != STATUS_DEVICE_NOT_READY
) return;
2442 if (!(req
= alloc_accept_req( sock
, NULL
, async
, NULL
))) return;
2443 list_add_tail( &sock
->accept_list
, &req
->entry
);
2445 async_set_completion_callback( async
, free_accept_req
, req
);
2446 queue_async( &sock
->accept_q
, async
);
2447 sock_reselect( sock
);
2448 set_error( STATUS_PENDING
);
2451 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
2452 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
2453 acceptsock
->wparam
= handle
;
2454 sock_reselect( acceptsock
);
2455 release_object( acceptsock
);
2456 set_reply_data( &handle
, sizeof(handle
) );
2460 case IOCTL_AFD_WINE_ACCEPT_INTO
:
2462 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
2463 const struct afd_accept_into_params
*params
= get_req_data();
2464 struct sock
*acceptsock
;
2465 unsigned int remote_len
;
2466 struct accept_req
*req
;
2468 if (get_req_data_size() != sizeof(*params
) ||
2469 get_reply_max_size() < params
->recv_len
||
2470 get_reply_max_size() - params
->recv_len
< params
->local_len
)
2472 set_error( STATUS_BUFFER_TOO_SMALL
);
2476 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
2477 if (remote_len
< sizeof(int))
2479 set_error( STATUS_INVALID_PARAMETER
);
2483 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
2486 if (acceptsock
->accept_recv_req
)
2488 release_object( acceptsock
);
2489 set_error( STATUS_INVALID_PARAMETER
);
2493 if (!(req
= alloc_accept_req( sock
, acceptsock
, async
, params
)))
2495 release_object( acceptsock
);
2498 list_add_tail( &sock
->accept_list
, &req
->entry
);
2499 acceptsock
->accept_recv_req
= req
;
2500 release_object( acceptsock
);
2502 acceptsock
->wparam
= params
->accept_handle
;
2503 async_set_completion_callback( async
, free_accept_req
, req
);
2504 queue_async( &sock
->accept_q
, async
);
2505 sock_reselect( sock
);
2506 set_error( STATUS_PENDING
);
2510 case IOCTL_AFD_LISTEN
:
2512 const struct afd_listen_params
*params
= get_req_data();
2514 if (get_req_data_size() < sizeof(*params
))
2516 set_error( STATUS_INVALID_PARAMETER
);
2522 set_error( STATUS_INVALID_PARAMETER
);
2526 if (listen( unix_fd
, params
->backlog
) < 0)
2528 set_error( sock_get_ntstatus( errno
) );
2532 sock
->state
= SOCK_LISTENING
;
2534 /* a listening socket can no longer be accepted into */
2535 allow_fd_caching( sock
->fd
);
2537 /* we may already be selecting for AFD_POLL_ACCEPT */
2538 sock_reselect( sock
);
2542 case IOCTL_AFD_WINE_CONNECT
:
2544 const struct afd_connect_params
*params
= get_req_data();
2545 const struct WS_sockaddr
*addr
;
2546 union unix_sockaddr unix_addr
;
2547 struct connect_req
*req
;
2551 if (get_req_data_size() < sizeof(*params
) ||
2552 get_req_data_size() - sizeof(*params
) < params
->addr_len
)
2554 set_error( STATUS_BUFFER_TOO_SMALL
);
2557 send_len
= get_req_data_size() - sizeof(*params
) - params
->addr_len
;
2558 addr
= (const struct WS_sockaddr
*)(params
+ 1);
2560 if (!params
->synchronous
&& !sock
->bound
)
2562 set_error( STATUS_INVALID_PARAMETER
);
2566 if (sock
->accept_recv_req
)
2568 set_error( STATUS_INVALID_PARAMETER
);
2572 if (sock
->connect_req
)
2574 set_error( STATUS_INVALID_PARAMETER
);
2578 switch (sock
->state
)
2580 case SOCK_LISTENING
:
2581 set_error( STATUS_INVALID_PARAMETER
);
2584 case SOCK_CONNECTING
:
2585 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2586 * but there's no status code that maps to WSAEALREADY... */
2587 set_error( params
->synchronous
? STATUS_ADDRESS_ALREADY_ASSOCIATED
: STATUS_INVALID_PARAMETER
);
2590 case SOCK_CONNECTED
:
2591 set_error( STATUS_CONNECTION_ACTIVE
);
2594 case SOCK_UNCONNECTED
:
2595 case SOCK_CONNECTIONLESS
:
2599 unix_len
= sockaddr_to_unix( addr
, params
->addr_len
, &unix_addr
);
2602 set_error( STATUS_INVALID_ADDRESS
);
2605 if (unix_addr
.addr
.sa_family
== AF_INET
&& !memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 ))
2606 unix_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_LOOPBACK
);
2608 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2609 if (ret
< 0 && errno
== ECONNABORTED
)
2611 /* On Linux with nonblocking socket if the previous connect() failed for any reason (including
2612 * timeout), next connect will fail. If the error code was queried by getsockopt( SO_ERROR )
2613 * the error code returned now is ECONNABORTED (otherwise that is the actual connect() failure
2614 * error code). If we got here after previous connect attempt on the socket that means
2615 * we already queried SO_ERROR in sock_error(), so retrying on ECONNABORTED only is
2617 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2620 if (ret
< 0 && errno
!= EINPROGRESS
)
2622 set_error( sock_get_ntstatus( errno
) );
2626 /* a connected or connecting socket can no longer be accepted into */
2627 allow_fd_caching( sock
->fd
);
2629 unix_len
= sizeof(unix_addr
);
2630 if (!getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
))
2631 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2636 if (sock
->type
!= WS_SOCK_DGRAM
)
2638 sock
->state
= SOCK_CONNECTED
;
2639 sock
->connect_time
= current_time
;
2642 if (!send_len
) return;
2645 if (sock
->type
!= WS_SOCK_DGRAM
)
2646 sock
->state
= SOCK_CONNECTING
;
2648 if (params
->synchronous
&& sock
->nonblocking
)
2650 sock_reselect( sock
);
2651 set_error( STATUS_DEVICE_NOT_READY
);
2655 if (!(req
= mem_alloc( sizeof(*req
) )))
2658 req
->async
= (struct async
*)grab_object( async
);
2659 req
->iosb
= async_get_iosb( async
);
2660 req
->sock
= (struct sock
*)grab_object( sock
);
2661 req
->addr_len
= params
->addr_len
;
2662 req
->send_len
= send_len
;
2663 req
->send_cursor
= 0;
2665 async_set_completion_callback( async
, free_connect_req
, req
);
2666 sock
->connect_req
= req
;
2667 queue_async( &sock
->connect_q
, async
);
2668 sock_reselect( sock
);
2669 set_error( STATUS_PENDING
);
2673 case IOCTL_AFD_WINE_SHUTDOWN
:
2677 if (get_req_data_size() < sizeof(int))
2679 set_error( STATUS_BUFFER_TOO_SMALL
);
2682 how
= *(int *)get_req_data();
2686 set_error( STATUS_INVALID_PARAMETER
);
2690 if (sock
->state
!= SOCK_CONNECTED
&& sock
->state
!= SOCK_CONNECTIONLESS
)
2692 set_error( STATUS_INVALID_CONNECTION
);
2698 sock
->rd_shutdown
= 1;
2700 if (how
!= SD_RECEIVE
)
2702 sock
->wr_shutdown
= 1;
2703 if (list_empty( &sock
->write_q
.queue
))
2704 shutdown( unix_fd
, SHUT_WR
);
2706 sock
->wr_shutdown_pending
= 1;
2711 if (sock
->event
) release_object( sock
->event
);
2715 sock
->nonblocking
= 1;
2718 sock_reselect( sock
);
2722 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE
:
2726 if (get_req_data_size() < sizeof(int))
2728 set_error( STATUS_BUFFER_TOO_SMALL
);
2731 force_async
= *(int *)get_req_data();
2733 if (sock
->nonblocking
&& !force_async
)
2735 set_error( STATUS_DEVICE_NOT_READY
);
2738 if (!sock_get_ifchange( sock
)) return;
2739 queue_async( &sock
->ifchange_q
, async
);
2740 set_error( STATUS_PENDING
);
2744 case IOCTL_AFD_WINE_FIONBIO
:
2745 if (get_req_data_size() < sizeof(int))
2747 set_error( STATUS_BUFFER_TOO_SMALL
);
2750 if (*(int *)get_req_data())
2752 sock
->nonblocking
= 1;
2758 set_error( STATUS_INVALID_PARAMETER
);
2761 sock
->nonblocking
= 0;
2765 case IOCTL_AFD_EVENT_SELECT
:
2767 struct event
*event
= NULL
;
2768 obj_handle_t event_handle
;
2771 set_async_pending( async
);
2773 if (is_machine_64bit( current
->process
->machine
))
2775 const struct afd_event_select_params_64
*params
= get_req_data();
2777 if (get_req_data_size() < sizeof(*params
))
2779 set_error( STATUS_INVALID_PARAMETER
);
2783 event_handle
= params
->event
;
2784 mask
= params
->mask
;
2788 const struct afd_event_select_params_32
*params
= get_req_data();
2790 if (get_req_data_size() < sizeof(*params
))
2792 set_error( STATUS_INVALID_PARAMETER
);
2796 event_handle
= params
->event
;
2797 mask
= params
->mask
;
2800 if ((event_handle
|| mask
) &&
2801 !(event
= get_event_obj( current
->process
, event_handle
, EVENT_MODIFY_STATE
)))
2803 set_error( STATUS_INVALID_PARAMETER
);
2807 if (sock
->event
) release_object( sock
->event
);
2808 sock
->event
= event
;
2813 sock
->nonblocking
= 1;
2815 sock_reselect( sock
);
2816 /* Explicitly wake the socket up if the mask causes it to become
2817 * signaled. Note that reselecting isn't enough, since we might already
2818 * have had events recorded in sock->reported_events and we don't want
2819 * to select for them again. */
2820 sock_wake_up( sock
);
2825 case IOCTL_AFD_WINE_MESSAGE_SELECT
:
2827 const struct afd_message_select_params
*params
= get_req_data();
2829 if (get_req_data_size() < sizeof(params
))
2831 set_error( STATUS_BUFFER_TOO_SMALL
);
2835 if (sock
->event
) release_object( sock
->event
);
2839 sock
->pending_events
= 0;
2840 sock
->reported_events
= 0;
2843 sock
->mask
= params
->mask
;
2844 sock
->window
= params
->window
;
2845 sock
->message
= params
->message
;
2846 sock
->wparam
= params
->handle
;
2847 sock
->nonblocking
= 1;
2849 sock_reselect( sock
);
2854 case IOCTL_AFD_BIND
:
2856 const struct afd_bind_params
*params
= get_req_data();
2857 union unix_sockaddr unix_addr
, bind_addr
;
2858 data_size_t in_size
;
2862 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2864 if (get_req_data_size() < get_reply_max_size())
2866 set_error( STATUS_BUFFER_TOO_SMALL
);
2869 in_size
= get_req_data_size() - get_reply_max_size();
2870 if (in_size
< offsetof(struct afd_bind_params
, addr
.sa_data
)
2871 || get_reply_max_size() < in_size
- sizeof(int))
2873 set_error( STATUS_INVALID_PARAMETER
);
2879 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED
);
2883 unix_len
= sockaddr_to_unix( ¶ms
->addr
, in_size
- sizeof(int), &unix_addr
);
2886 set_error( STATUS_INVALID_ADDRESS
);
2889 bind_addr
= unix_addr
;
2891 if (unix_addr
.addr
.sa_family
== AF_INET
)
2893 if (!memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 )
2894 || bind_to_interface( sock
, &unix_addr
.in
))
2895 bind_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
2897 else if (unix_addr
.addr
.sa_family
== AF_INET6
)
2899 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2900 /* Windows allows specifying zero to use the default scope. Linux
2901 * interprets it as an interface index and requires that it be
2903 if (!unix_addr
.in6
.sin6_scope_id
)
2904 bind_addr
.in6
.sin6_scope_id
= get_ipv6_interface_index( &unix_addr
.in6
.sin6_addr
);
2908 set_async_pending( async
);
2911 if (sock
->family
== WS_AF_INET6
)
2913 socklen_t len
= sizeof(v6only
);
2915 getsockopt( get_unix_fd(sock
->fd
), IPPROTO_IPV6
, IPV6_V6ONLY
, &v6only
, &len
);
2919 if (check_addr_usage( sock
, &bind_addr
, v6only
))
2922 if (bind( unix_fd
, &bind_addr
.addr
, unix_len
) < 0)
2924 if (errno
== EADDRINUSE
&& sock
->reuseaddr
)
2927 set_error( sock_get_ntstatus( errno
) );
2933 unix_len
= sizeof(bind_addr
);
2934 if (!getsockname( unix_fd
, &bind_addr
.addr
, &unix_len
))
2936 /* store the interface or magic loopback address instead of the
2937 * actual unix address */
2938 if (bind_addr
.addr
.sa_family
== AF_INET
)
2939 bind_addr
.in
.sin_addr
= unix_addr
.in
.sin_addr
;
2940 sock
->addr_len
= sockaddr_from_unix( &bind_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2943 update_addr_usage( sock
, &bind_addr
, v6only
);
2945 if (get_reply_max_size() >= sock
->addr_len
)
2946 set_reply_data( &sock
->addr
, sock
->addr_len
);
2950 case IOCTL_AFD_GETSOCKNAME
:
2953 set_error( STATUS_INVALID_PARAMETER
);
2957 if (get_reply_max_size() < sock
->addr_len
)
2959 set_error( STATUS_BUFFER_TOO_SMALL
);
2963 set_reply_data( &sock
->addr
, sock
->addr_len
);
2966 case IOCTL_AFD_WINE_DEFER
:
2968 const obj_handle_t
*handle
= get_req_data();
2969 struct sock
*acceptsock
;
2971 if (get_req_data_size() < sizeof(*handle
))
2973 set_error( STATUS_BUFFER_TOO_SMALL
);
2977 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, *handle
, 0, &sock_ops
);
2978 if (!acceptsock
) return;
2980 sock
->deferred
= acceptsock
;
2984 case IOCTL_AFD_WINE_GET_INFO
:
2986 struct afd_get_info_params params
;
2988 if (get_reply_max_size() < sizeof(params
))
2990 set_error( STATUS_BUFFER_TOO_SMALL
);
2994 params
.family
= sock
->family
;
2995 params
.type
= sock
->type
;
2996 params
.protocol
= sock
->proto
;
2997 set_reply_data( ¶ms
, sizeof(params
) );
3001 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN
:
3003 int listening
= (sock
->state
== SOCK_LISTENING
);
3005 if (get_reply_max_size() < sizeof(listening
))
3007 set_error( STATUS_BUFFER_TOO_SMALL
);
3011 set_reply_data( &listening
, sizeof(listening
) );
3015 case IOCTL_AFD_WINE_GET_SO_ERROR
:
3020 if (get_reply_max_size() < sizeof(error
))
3022 set_error( STATUS_BUFFER_TOO_SMALL
);
3026 error
= sock_error( sock
);
3029 for (i
= 0; i
< ARRAY_SIZE( sock
->errors
); ++i
)
3031 if (sock
->errors
[i
])
3033 error
= sock
->errors
[i
];
3039 error
= sock_get_error( error
);
3040 set_reply_data( &error
, sizeof(error
) );
3044 case IOCTL_AFD_WINE_GET_SO_RCVBUF
:
3046 int rcvbuf
= sock
->rcvbuf
;
3048 if (get_reply_max_size() < sizeof(rcvbuf
))
3050 set_error( STATUS_BUFFER_TOO_SMALL
);
3054 set_reply_data( &rcvbuf
, sizeof(rcvbuf
) );
3058 case IOCTL_AFD_WINE_SET_SO_RCVBUF
:
3062 if (get_req_data_size() < sizeof(rcvbuf
))
3064 set_error( STATUS_BUFFER_TOO_SMALL
);
3067 rcvbuf
= *(DWORD
*)get_req_data();
3069 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_RCVBUF
, (char *)&rcvbuf
, sizeof(rcvbuf
) ))
3070 sock
->rcvbuf
= rcvbuf
;
3072 set_error( sock_get_ntstatus( errno
) );
3076 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO
:
3078 DWORD rcvtimeo
= sock
->rcvtimeo
;
3080 if (get_reply_max_size() < sizeof(rcvtimeo
))
3082 set_error( STATUS_BUFFER_TOO_SMALL
);
3086 set_reply_data( &rcvtimeo
, sizeof(rcvtimeo
) );
3090 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO
:
3094 if (get_req_data_size() < sizeof(rcvtimeo
))
3096 set_error( STATUS_BUFFER_TOO_SMALL
);
3099 rcvtimeo
= *(DWORD
*)get_req_data();
3101 sock
->rcvtimeo
= rcvtimeo
;
3105 /* BSD socket SO_REUSEADDR is not compatible with winsock semantics. */
3106 case IOCTL_AFD_WINE_SET_SO_REUSEADDR
:
3110 if (get_req_data_size() < sizeof(reuse
))
3112 set_error( STATUS_BUFFER_TOO_SMALL
);
3116 reuse
= *(int *)get_req_data();
3118 if (reuse
&& sock
->exclusiveaddruse
)
3120 set_error( STATUS_INVALID_PARAMETER
);
3124 if (is_tcp_socket( sock
))
3127 ret
= setsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuse
, sizeof(reuse
) );
3129 if (!ret
) ret
= setsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEPORT
, &reuse
, sizeof(reuse
) );
3132 set_error( sock_get_ntstatus( errno
) );
3134 sock
->reuseaddr
= !!reuse
;
3138 case IOCTL_AFD_WINE_SET_SO_EXCLUSIVEADDRUSE
:
3142 if (get_req_data_size() < sizeof(exclusive
))
3144 set_error( STATUS_BUFFER_TOO_SMALL
);
3148 exclusive
= *(int *)get_req_data();
3149 if (exclusive
&& sock
->reuseaddr
)
3151 set_error( STATUS_INVALID_PARAMETER
);
3154 sock
->exclusiveaddruse
= !!exclusive
;
3158 case IOCTL_AFD_WINE_GET_SO_SNDBUF
:
3160 int sndbuf
= sock
->sndbuf
;
3162 if (get_reply_max_size() < sizeof(sndbuf
))
3164 set_error( STATUS_BUFFER_TOO_SMALL
);
3168 set_reply_data( &sndbuf
, sizeof(sndbuf
) );
3172 case IOCTL_AFD_WINE_SET_SO_SNDBUF
:
3176 if (get_req_data_size() < sizeof(sndbuf
))
3178 set_error( STATUS_BUFFER_TOO_SMALL
);
3181 sndbuf
= *(DWORD
*)get_req_data();
3186 /* setsockopt fails if a zero value is passed */
3187 sock
->sndbuf
= sndbuf
;
3192 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_SNDBUF
, (char *)&sndbuf
, sizeof(sndbuf
) ))
3193 sock
->sndbuf
= sndbuf
;
3195 set_error( sock_get_ntstatus( errno
) );
3199 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO
:
3201 DWORD sndtimeo
= sock
->sndtimeo
;
3203 if (get_reply_max_size() < sizeof(sndtimeo
))
3205 set_error( STATUS_BUFFER_TOO_SMALL
);
3209 set_reply_data( &sndtimeo
, sizeof(sndtimeo
) );
3213 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO
:
3217 if (get_req_data_size() < sizeof(sndtimeo
))
3219 set_error( STATUS_BUFFER_TOO_SMALL
);
3222 sndtimeo
= *(DWORD
*)get_req_data();
3224 sock
->sndtimeo
= sndtimeo
;
3228 case IOCTL_AFD_WINE_GET_SO_CONNECT_TIME
:
3232 if (get_reply_max_size() < sizeof(time
))
3234 set_error( STATUS_BUFFER_TOO_SMALL
);
3238 if (sock
->state
== SOCK_CONNECTED
)
3239 time
= (current_time
- sock
->connect_time
) / 10000000;
3241 set_reply_data( &time
, sizeof(time
) );
3245 case IOCTL_AFD_WINE_GET_SO_REUSEADDR
:
3249 if (!get_reply_max_size())
3251 set_error( STATUS_BUFFER_TOO_SMALL
);
3255 reuse
= sock
->reuseaddr
;
3256 set_reply_data( &reuse
, min( sizeof(reuse
), get_reply_max_size() ));
3260 case IOCTL_AFD_WINE_GET_SO_EXCLUSIVEADDRUSE
:
3264 if (!get_reply_max_size())
3266 set_error( STATUS_BUFFER_TOO_SMALL
);
3270 exclusive
= sock
->exclusiveaddruse
;
3271 set_reply_data( &exclusive
, min( sizeof(exclusive
), get_reply_max_size() ));
3275 case IOCTL_AFD_POLL
:
3277 if (get_reply_max_size() < get_req_data_size())
3279 set_error( STATUS_INVALID_PARAMETER
);
3283 if (is_machine_64bit( current
->process
->machine
))
3285 const struct afd_poll_params_64
*params
= get_req_data();
3287 if (get_req_data_size() < sizeof(struct afd_poll_params_64
) ||
3288 get_req_data_size() < offsetof( struct afd_poll_params_64
, sockets
[params
->count
] ))
3290 set_error( STATUS_INVALID_PARAMETER
);
3294 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, params
->sockets
);
3298 const struct afd_poll_params_32
*params
= get_req_data();
3299 struct afd_poll_socket_64
*sockets
;
3302 if (get_req_data_size() < sizeof(struct afd_poll_params_32
) ||
3303 get_req_data_size() < offsetof( struct afd_poll_params_32
, sockets
[params
->count
] ))
3305 set_error( STATUS_INVALID_PARAMETER
);
3309 if (!(sockets
= mem_alloc( params
->count
* sizeof(*sockets
) ))) return;
3310 for (i
= 0; i
< params
->count
; ++i
)
3312 sockets
[i
].socket
= params
->sockets
[i
].socket
;
3313 sockets
[i
].flags
= params
->sockets
[i
].flags
;
3314 sockets
[i
].status
= params
->sockets
[i
].status
;
3317 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, sockets
);
3325 set_error( STATUS_NOT_SUPPORTED
);
3330 static void handle_exclusive_poll(struct poll_req
*req
)
3334 for (i
= 0; i
< req
->count
; ++i
)
3336 struct sock
*sock
= req
->sockets
[i
].sock
;
3337 struct poll_req
*main_poll
= sock
->main_poll
;
3339 if (main_poll
&& main_poll
->exclusive
&& req
->exclusive
)
3341 complete_async_poll( main_poll
, STATUS_SUCCESS
);
3346 sock
->main_poll
= req
;
3350 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
3351 unsigned int count
, const struct afd_poll_socket_64
*sockets
)
3353 BOOL signaled
= FALSE
;
3354 struct poll_req
*req
;
3359 set_error( STATUS_INVALID_PARAMETER
);
3363 if (!(req
= mem_alloc( offsetof( struct poll_req
, sockets
[count
] ) )))
3366 req
->timeout
= NULL
;
3368 if (timeout
&& timeout
!= TIMEOUT_INFINITE
&&
3369 !(req
->timeout
= add_timeout_user( timeout
, async_poll_timeout
, req
)))
3374 req
->orig_timeout
= timeout
;
3376 for (i
= 0; i
< count
; ++i
)
3378 req
->sockets
[i
].sock
= (struct sock
*)get_handle_obj( current
->process
, sockets
[i
].socket
, 0, &sock_ops
);
3379 if (!req
->sockets
[i
].sock
)
3381 for (j
= 0; j
< i
; ++j
) release_object( req
->sockets
[j
].sock
);
3382 if (req
->timeout
) remove_timeout_user( req
->timeout
);
3386 req
->sockets
[i
].handle
= sockets
[i
].socket
;
3387 req
->sockets
[i
].mask
= sockets
[i
].flags
;
3388 req
->sockets
[i
].flags
= 0;
3391 req
->exclusive
= exclusive
;
3393 req
->async
= (struct async
*)grab_object( async
);
3394 req
->iosb
= async_get_iosb( async
);
3396 handle_exclusive_poll(req
);
3398 list_add_tail( &poll_list
, &req
->entry
);
3399 async_set_completion_callback( async
, free_poll_req
, req
);
3400 queue_async( &poll_sock
->poll_q
, async
);
3402 for (i
= 0; i
< count
; ++i
)
3404 struct sock
*sock
= req
->sockets
[i
].sock
;
3405 int mask
= req
->sockets
[i
].mask
;
3406 struct pollfd pollfd
;
3408 pollfd
.fd
= get_unix_fd( sock
->fd
);
3409 pollfd
.events
= poll_flags_from_afd( sock
, mask
);
3410 if (pollfd
.events
>= 0 && poll( &pollfd
, 1, 0 ) >= 0)
3411 sock_poll_event( sock
->fd
, pollfd
.revents
);
3413 /* FIXME: do other error conditions deserve a similar treatment? */
3414 if (sock
->state
!= SOCK_CONNECTING
&& sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] && (mask
& AFD_POLL_CONNECT_ERR
))
3416 req
->sockets
[i
].flags
|= AFD_POLL_CONNECT_ERR
;
3417 req
->sockets
[i
].status
= sock_get_ntstatus( sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] );
3420 if (req
->sockets
[i
].flags
)
3424 if (!timeout
|| signaled
)
3425 complete_async_poll( req
, STATUS_SUCCESS
);
3429 for (i
= 0; i
< req
->count
; ++i
)
3430 sock_reselect( req
->sockets
[i
].sock
);
3431 set_error( STATUS_PENDING
);
3434 #ifdef HAVE_LINUX_RTNETLINK_H
3436 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
3437 static struct object
*ifchange_object
;
3439 static void ifchange_dump( struct object
*obj
, int verbose
);
3440 static struct fd
*ifchange_get_fd( struct object
*obj
);
3441 static void ifchange_destroy( struct object
*obj
);
3443 static int ifchange_get_poll_events( struct fd
*fd
);
3444 static void ifchange_poll_event( struct fd
*fd
, int event
);
3448 struct object obj
; /* object header */
3449 struct fd
*fd
; /* interface change file descriptor */
3450 struct list sockets
; /* list of sockets to send interface change notifications */
3453 static const struct object_ops ifchange_ops
=
3455 sizeof(struct ifchange
), /* size */
3456 &no_type
, /* type */
3457 ifchange_dump
, /* dump */
3458 no_add_queue
, /* add_queue */
3459 NULL
, /* remove_queue */
3460 NULL
, /* signaled */
3461 no_satisfied
, /* satisfied */
3462 no_signal
, /* signal */
3463 ifchange_get_fd
, /* get_fd */
3464 default_map_access
, /* map_access */
3465 default_get_sd
, /* get_sd */
3466 default_set_sd
, /* set_sd */
3467 no_get_full_name
, /* get_full_name */
3468 no_lookup_name
, /* lookup_name */
3469 no_link_name
, /* link_name */
3470 NULL
, /* unlink_name */
3471 no_open_file
, /* open_file */
3472 no_kernel_obj_list
, /* get_kernel_obj_list */
3473 no_close_handle
, /* close_handle */
3474 ifchange_destroy
/* destroy */
3477 static const struct fd_ops ifchange_fd_ops
=
3479 ifchange_get_poll_events
, /* get_poll_events */
3480 ifchange_poll_event
, /* poll_event */
3481 NULL
, /* get_fd_type */
3482 no_fd_read
, /* read */
3483 no_fd_write
, /* write */
3484 no_fd_flush
, /* flush */
3485 no_fd_get_file_info
, /* get_file_info */
3486 no_fd_get_volume_info
, /* get_volume_info */
3487 no_fd_ioctl
, /* ioctl */
3488 NULL
, /* cancel_async */
3489 NULL
, /* queue_async */
3490 NULL
/* reselect_async */
3493 static void ifchange_dump( struct object
*obj
, int verbose
)
3495 assert( obj
->ops
== &ifchange_ops
);
3496 fprintf( stderr
, "Interface change\n" );
3499 static struct fd
*ifchange_get_fd( struct object
*obj
)
3501 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3502 return (struct fd
*)grab_object( ifchange
->fd
);
3505 static void ifchange_destroy( struct object
*obj
)
3507 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3508 assert( obj
->ops
== &ifchange_ops
);
3510 release_object( ifchange
->fd
);
3512 /* reset the global ifchange object so that it will be recreated if it is needed again */
3513 assert( obj
== ifchange_object
);
3514 ifchange_object
= NULL
;
3517 static int ifchange_get_poll_events( struct fd
*fd
)
3522 /* wake up all the sockets waiting for a change notification event */
3523 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
3525 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3526 struct list
*ptr
, *next
;
3527 assert( obj
->ops
== &ifchange_ops
);
3528 assert( obj
== ifchange_object
);
3530 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
3532 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
3534 assert( sock
->ifchange_obj
);
3535 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
3536 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
3540 static void ifchange_poll_event( struct fd
*fd
, int event
)
3542 struct object
*ifchange
= get_fd_user( fd
);
3543 unsigned int status
= STATUS_PENDING
;
3544 char buffer
[PIPE_BUF
];
3547 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
3550 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
3551 return; /* retry when poll() says the socket is ready */
3552 status
= sock_get_ntstatus( errno
);
3556 struct nlmsghdr
*nlh
;
3558 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
3560 if (nlh
->nlmsg_type
== NLMSG_DONE
)
3562 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
3563 status
= STATUS_SUCCESS
;
3566 else status
= STATUS_CANCELLED
;
3568 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
3573 /* we only need one of these interface notification objects, all of the sockets dependent upon
3574 * it will wake up when a notification event occurs */
3575 static struct object
*get_ifchange( void )
3577 #ifdef HAVE_LINUX_RTNETLINK_H
3578 struct ifchange
*ifchange
;
3579 struct sockaddr_nl addr
;
3582 if (ifchange_object
)
3584 /* increment the refcount for each socket that uses the ifchange object */
3585 return grab_object( ifchange_object
);
3588 /* create the socket we need for processing interface change notifications */
3589 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
3592 set_error( sock_get_ntstatus( errno
));
3595 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
3596 memset( &addr
, 0, sizeof(addr
) );
3597 addr
.nl_family
= AF_NETLINK
;
3598 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
3599 /* bind the socket to the special netlink kernel interface */
3600 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
3603 set_error( sock_get_ntstatus( errno
));
3606 if (!(ifchange
= alloc_object( &ifchange_ops
)))
3609 set_error( STATUS_NO_MEMORY
);
3612 list_init( &ifchange
->sockets
);
3613 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
3615 release_object( ifchange
);
3616 set_error( STATUS_NO_MEMORY
);
3619 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
3621 /* the ifchange object is now successfully configured */
3622 ifchange_object
= &ifchange
->obj
;
3623 return &ifchange
->obj
;
3625 set_error( STATUS_NOT_SUPPORTED
);
3630 /* add the socket to the interface change notification list */
3631 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
3633 #ifdef HAVE_LINUX_RTNETLINK_H
3634 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3636 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
3640 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3641 static struct object
*sock_get_ifchange( struct sock
*sock
)
3643 struct object
*ifchange
;
3645 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
3646 return sock
->ifchange_obj
;
3648 if (!(ifchange
= get_ifchange()))
3651 /* add the socket to the ifchange notification list */
3652 ifchange_add_sock( ifchange
, sock
);
3653 sock
->ifchange_obj
= ifchange
;
3657 /* destroy an existing ifchange queue for a specific socket */
3658 static void sock_release_ifchange( struct sock
*sock
)
3660 if (sock
->ifchange_obj
)
3662 list_remove( &sock
->ifchange_entry
);
3663 release_object( sock
->ifchange_obj
);
3664 sock
->ifchange_obj
= NULL
;
3668 static void socket_device_dump( struct object
*obj
, int verbose
);
3669 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3670 unsigned int attr
, struct object
*root
);
3671 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3672 unsigned int sharing
, unsigned int options
);
3674 static const struct object_ops socket_device_ops
=
3676 sizeof(struct object
), /* size */
3677 &device_type
, /* type */
3678 socket_device_dump
, /* dump */
3679 no_add_queue
, /* add_queue */
3680 NULL
, /* remove_queue */
3681 NULL
, /* signaled */
3682 no_satisfied
, /* satisfied */
3683 no_signal
, /* signal */
3684 no_get_fd
, /* get_fd */
3685 default_map_access
, /* map_access */
3686 default_get_sd
, /* get_sd */
3687 default_set_sd
, /* set_sd */
3688 default_get_full_name
, /* get_full_name */
3689 socket_device_lookup_name
, /* lookup_name */
3690 directory_link_name
, /* link_name */
3691 default_unlink_name
, /* unlink_name */
3692 socket_device_open_file
, /* open_file */
3693 no_kernel_obj_list
, /* get_kernel_obj_list */
3694 no_close_handle
, /* close_handle */
3695 no_destroy
/* destroy */
3698 static void socket_device_dump( struct object
*obj
, int verbose
)
3700 fputs( "Socket device\n", stderr
);
3703 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3704 unsigned int attr
, struct object
*root
)
3706 if (name
) name
->len
= 0;
3710 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3711 unsigned int sharing
, unsigned int options
)
3715 if (!(sock
= create_socket())) return NULL
;
3716 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
3718 release_object( sock
);
3724 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
3725 unsigned int attr
, const struct security_descriptor
*sd
)
3727 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
3730 DECL_HANDLER(recv_socket
)
3732 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3733 unsigned int status
= STATUS_PENDING
;
3734 timeout_t timeout
= 0;
3735 struct async
*async
;
3741 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3742 timeout
= (timeout_t
)sock
->rcvtimeo
* -10000;
3744 if (sock
->rd_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3745 else if (!async_queued( &sock
->read_q
))
3747 /* If read_q is not empty, we cannot really tell if the already queued
3748 * asyncs will not consume all available data; if there's no data
3749 * available, the current request won't be immediately satiable.
3751 if ((!req
->force_async
&& sock
->nonblocking
) ||
3752 check_fd_events( sock
->fd
, req
->oob
&& !is_oobinline( sock
) ? POLLPRI
: POLLIN
))
3754 /* Give the client opportunity to complete synchronously.
3755 * If it turns out that the I/O request is not actually immediately satiable,
3756 * the client may then choose to re-queue the async (with STATUS_PENDING).
3758 * Note: If the nonblocking flag is set, we don't poll the socket
3759 * here and always opt for synchronous completion first. This is
3760 * because the application has probably seen POLLIN already from a
3761 * preceding select()/poll() call before it requested to receive
3764 status
= STATUS_ALERTED
;
3768 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3769 status
= STATUS_DEVICE_NOT_READY
;
3771 sock
->pending_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3772 sock
->reported_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3774 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3776 set_error( status
);
3779 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3781 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3782 queue_async( &sock
->read_q
, async
);
3784 /* always reselect; we changed reported_events above */
3785 sock_reselect( sock
);
3787 reply
->wait
= async_handoff( async
, NULL
, 0 );
3788 reply
->options
= get_fd_options( fd
);
3789 reply
->nonblocking
= sock
->nonblocking
;
3790 release_object( async
);
3792 release_object( sock
);
3795 static void send_socket_completion_callback( void *private )
3797 struct send_req
*send_req
= private;
3798 struct iosb
*iosb
= send_req
->iosb
;
3799 struct sock
*sock
= send_req
->sock
;
3801 if (iosb
->status
!= STATUS_SUCCESS
)
3803 /* send() calls only clear and reselect events if unsuccessful. */
3804 sock
->pending_events
&= ~AFD_POLL_WRITE
;
3805 sock
->reported_events
&= ~AFD_POLL_WRITE
;
3806 sock_reselect( sock
);
3809 release_object( iosb
);
3810 release_object( sock
);
3814 DECL_HANDLER(send_socket
)
3816 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3817 unsigned int status
= STATUS_PENDING
;
3818 timeout_t timeout
= 0;
3819 struct async
*async
;
3826 if (sock
->type
== WS_SOCK_DGRAM
&& !sock
->bound
)
3828 union unix_sockaddr unix_addr
;
3830 int unix_fd
= get_unix_fd( fd
);
3832 unix_len
= get_unix_sockaddr_any( &unix_addr
, sock
->family
);
3833 if (bind( unix_fd
, &unix_addr
.addr
, unix_len
) < 0)
3836 if (getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
) >= 0)
3838 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
3841 else if (!bind_errno
) bind_errno
= errno
;
3844 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3845 timeout
= (timeout_t
)sock
->sndtimeo
* -10000;
3847 if (bind_errno
) status
= sock_get_ntstatus( bind_errno
);
3848 else if (sock
->wr_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3849 else if (!async_queued( &sock
->write_q
))
3851 /* If write_q is not empty, we cannot really tell if the already queued
3852 * asyncs will not consume all available space; if there's no space
3853 * available, the current request won't be immediately satiable.
3855 if ((!req
->force_async
&& sock
->nonblocking
) || check_fd_events( sock
->fd
, POLLOUT
))
3857 /* Give the client opportunity to complete synchronously.
3858 * If it turns out that the I/O request is not actually immediately satiable,
3859 * the client may then choose to re-queue the async (with STATUS_PENDING).
3861 * Note: If the nonblocking flag is set, we don't poll the socket
3862 * here and always opt for synchronous completion first. This is
3863 * because the application has probably seen POLLOUT already from a
3864 * preceding select()/poll() call before it requested to send data.
3866 * Furthermore, some applications expect that any send() call on a
3867 * socket that has indicated POLLOUT beforehand never fails with
3868 * WSAEWOULDBLOCK. It's possible that Linux poll() may yield
3869 * POLLOUT on the first call but not the second, even if no send()
3870 * call has been made in the meanwhile. This can happen for a
3871 * number of reasons; for example, TCP fragmentation may consume
3872 * extra buffer space for each packet that has been split out, or
3873 * the TCP/IP networking stack may decide to shrink the send buffer
3874 * due to memory pressure.
3876 status
= STATUS_ALERTED
;
3880 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3881 status
= STATUS_DEVICE_NOT_READY
;
3883 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3885 struct send_req
*send_req
;
3886 struct iosb
*iosb
= async_get_iosb( async
);
3888 if ((send_req
= mem_alloc( sizeof(*send_req
) )))
3890 send_req
->iosb
= (struct iosb
*)grab_object( iosb
);
3891 send_req
->sock
= (struct sock
*)grab_object( sock
);
3892 async_set_completion_callback( async
, send_socket_completion_callback
, send_req
);
3894 else if (status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
)
3895 status
= STATUS_NO_MEMORY
;
3897 release_object( iosb
);
3899 set_error( status
);
3902 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3904 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3906 queue_async( &sock
->write_q
, async
);
3907 sock_reselect( sock
);
3910 reply
->wait
= async_handoff( async
, NULL
, 0 );
3911 reply
->options
= get_fd_options( fd
);
3912 reply
->nonblocking
= sock
->nonblocking
;
3913 release_object( async
);
3915 release_object( sock
);
3918 DECL_HANDLER(socket_get_events
)
3920 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, 0, &sock_ops
);
3921 unsigned int status
[13];
3922 struct event
*event
= NULL
;
3925 if (get_reply_max_size() < sizeof(status
))
3927 set_error( STATUS_INVALID_PARAMETER
);
3935 if (!(event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
)))
3937 release_object( sock
);
3942 reply
->flags
= sock
->pending_events
& sock
->mask
;
3943 for (i
= 0; i
< ARRAY_SIZE( status
); ++i
)
3944 status
[i
] = sock_get_ntstatus( sock
->errors
[i
] );
3946 sock
->pending_events
&= ~sock
->mask
;
3947 sock_reselect( sock
);
3951 reset_event( event
);
3952 release_object( event
);
3955 set_reply_data( status
, sizeof(status
) );
3957 release_object( sock
);
3960 DECL_HANDLER(socket_send_icmp_id
)
3962 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, 0, &sock_ops
);
3966 if (sock
->icmp_fixup_data_len
== MAX_ICMP_HISTORY_LENGTH
)
3968 memmove( sock
->icmp_fixup_data
, sock
->icmp_fixup_data
+ 1,
3969 sizeof(*sock
->icmp_fixup_data
) * (MAX_ICMP_HISTORY_LENGTH
- 1) );
3970 --sock
->icmp_fixup_data_len
;
3973 sock
->icmp_fixup_data
[sock
->icmp_fixup_data_len
].icmp_id
= req
->icmp_id
;
3974 sock
->icmp_fixup_data
[sock
->icmp_fixup_data_len
].icmp_seq
= req
->icmp_seq
;
3975 ++sock
->icmp_fixup_data_len
;
3977 release_object( sock
);
3980 DECL_HANDLER(socket_get_icmp_id
)
3982 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, 0, &sock_ops
);
3987 for (i
= 0; i
< sock
->icmp_fixup_data_len
; ++i
)
3989 if (sock
->icmp_fixup_data
[i
].icmp_seq
== req
->icmp_seq
)
3991 reply
->icmp_id
= sock
->icmp_fixup_data
[i
].icmp_id
;
3992 --sock
->icmp_fixup_data_len
;
3993 memmove( &sock
->icmp_fixup_data
[i
], &sock
->icmp_fixup_data
[i
+ 1],
3994 (sock
->icmp_fixup_data_len
- i
) * sizeof(*sock
->icmp_fixup_data
) );
3995 release_object( sock
);
4000 set_error( STATUS_NOT_FOUND
);
4001 release_object( sock
);