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>
79 #ifdef HAVE_LINUX_IRDA_H
80 # ifdef HAVE_LINUX_TYPES_H
81 # include <linux/types.h>
83 # include <linux/irda.h>
88 #define WIN32_NO_STATUS
98 #include "wine/rbtree.h"
107 #if defined(linux) && !defined(IP_UNICAST_IF)
108 #define IP_UNICAST_IF 50
111 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
115 struct WS_sockaddr addr
;
116 struct WS_sockaddr_in in
;
117 struct WS_sockaddr_in6 in6
;
118 struct WS_sockaddr_ipx ipx
;
124 struct sockaddr addr
;
125 struct sockaddr_in in
;
126 struct sockaddr_in6 in6
;
128 struct sockaddr_ipx ipx
;
131 struct sockaddr_irda irda
;
135 static struct list poll_list
= LIST_INIT( poll_list
);
142 struct timeout_user
*timeout
;
143 timeout_t orig_timeout
;
162 struct sock
*sock
, *acceptsock
;
164 unsigned int recv_len
, local_len
;
172 unsigned int addr_len
, send_len
, send_cursor
;
181 enum connection_state
192 struct rb_entry entry
;
193 union unix_sockaddr addr
;
198 #define MAX_ICMP_HISTORY_LENGTH 8
202 struct object obj
; /* object header */
203 struct fd
*fd
; /* socket file descriptor */
204 enum connection_state state
; /* connection state */
205 unsigned int mask
; /* event mask */
206 /* pending AFD_POLL_* events which have not yet been reported to the application */
207 unsigned int pending_events
;
208 /* AFD_POLL_* events which have already been reported and should not be
209 * selected for again until reset by a relevant call.
211 * For example, if AFD_POLL_READ is set here and not in pending_events, it
212 * has already been reported and consumed, and we should not report it
213 * again, even if POLLIN is signaled, until it is reset by e.g recv().
215 * If an event has been signaled and not consumed yet, it will be set in
216 * both pending_events and reported_events (as we should only ever report
217 * any event once until it is reset.) */
218 unsigned int reported_events
;
219 unsigned short proto
; /* socket protocol */
220 unsigned short type
; /* socket type */
221 unsigned short family
; /* socket family */
222 struct event
*event
; /* event object */
223 user_handle_t window
; /* window to send the message to */
224 unsigned int message
; /* message to send */
225 obj_handle_t wparam
; /* message wparam (socket handle) */
226 int errors
[AFD_POLL_BIT_COUNT
]; /* event errors */
227 timeout_t connect_time
;/* time the socket was connected */
228 struct sock
*deferred
; /* socket that waits for a deferred accept */
229 struct async_queue read_q
; /* queue for asynchronous reads */
230 struct async_queue write_q
; /* queue for asynchronous writes */
231 struct async_queue ifchange_q
; /* queue for interface change notifications */
232 struct async_queue accept_q
; /* queue for asynchronous accepts */
233 struct async_queue connect_q
; /* queue for asynchronous connects */
234 struct async_queue poll_q
; /* queue for asynchronous polls */
235 struct object
*ifchange_obj
; /* the interface change notification object */
236 struct list ifchange_entry
; /* entry in ifchange notification list */
237 struct list accept_list
; /* list of pending accept requests */
238 struct accept_req
*accept_recv_req
; /* pending accept-into request which will recv on this socket */
239 struct connect_req
*connect_req
; /* pending connection request */
240 struct poll_req
*main_poll
; /* main poll */
241 union win_sockaddr addr
; /* socket name */
242 int addr_len
; /* socket name length */
243 unsigned int rcvbuf
; /* advisory recv buffer size */
244 unsigned int sndbuf
; /* advisory send buffer size */
245 unsigned int rcvtimeo
; /* receive timeout in ms */
246 unsigned int sndtimeo
; /* send timeout in ms */
249 unsigned short icmp_id
;
250 unsigned short icmp_seq
;
252 icmp_fixup_data
[MAX_ICMP_HISTORY_LENGTH
]; /* Sent ICMP packets history used to fixup reply id. */
253 struct bound_addr
*bound_addr
[2]; /* Links to the entries in bound addresses tree. */
254 unsigned int icmp_fixup_data_len
; /* Sent ICMP packets history length. */
255 unsigned int rd_shutdown
: 1; /* is the read end shut down? */
256 unsigned int wr_shutdown
: 1; /* is the write end shut down? */
257 unsigned int wr_shutdown_pending
: 1; /* is a write shutdown pending? */
258 unsigned int hangup
: 1; /* has the read end received a hangup? */
259 unsigned int aborted
: 1; /* did we get a POLLERR or irregular POLLHUP? */
260 unsigned int nonblocking
: 1; /* is the socket nonblocking? */
261 unsigned int bound
: 1; /* is the socket bound? */
262 unsigned int reset
: 1; /* did we get a TCP reset? */
263 unsigned int reuseaddr
: 1; /* winsock SO_REUSEADDR option value */
264 unsigned int exclusiveaddruse
: 1; /* winsock SO_EXCLUSIVEADDRUSE option value */
267 static int is_tcp_socket( struct sock
*sock
)
269 return sock
->type
== WS_SOCK_STREAM
&& (sock
->family
== WS_AF_INET
|| sock
->family
== WS_AF_INET6
);
272 static int addr_compare( const void *key
, const struct wine_rb_entry
*entry
)
274 const struct bound_addr
*bound_addr
= RB_ENTRY_VALUE(entry
, struct bound_addr
, entry
);
275 const struct bound_addr
*addr
= key
;
277 if (addr
->addr
.addr
.sa_family
!= bound_addr
->addr
.addr
.sa_family
)
278 return addr
->addr
.addr
.sa_family
< bound_addr
->addr
.addr
.sa_family
? -1 : 1;
280 if (addr
->addr
.addr
.sa_family
== AF_INET
)
282 if (addr
->addr
.in
.sin_port
!= bound_addr
->addr
.in
.sin_port
)
283 return addr
->addr
.in
.sin_port
< bound_addr
->addr
.in
.sin_port
? -1 : 1;
284 if (bound_addr
->match_any_addr
|| addr
->match_any_addr
285 || addr
->addr
.in
.sin_addr
.s_addr
== bound_addr
->addr
.in
.sin_addr
.s_addr
)
287 return addr
->addr
.in
.sin_addr
.s_addr
< bound_addr
->addr
.in
.sin_addr
.s_addr
? -1 : 1;
290 assert( addr
->addr
.addr
.sa_family
== AF_INET6
);
291 if (addr
->addr
.in6
.sin6_port
!= bound_addr
->addr
.in6
.sin6_port
)
292 return addr
->addr
.in6
.sin6_port
< bound_addr
->addr
.in6
.sin6_port
? -1 : 1;
293 if (bound_addr
->match_any_addr
|| addr
->match_any_addr
) return 0;
294 return memcmp( &addr
->addr
.in6
.sin6_addr
, &bound_addr
->addr
.in6
.sin6_addr
, sizeof(addr
->addr
.in6
.sin6_addr
) );
297 static int ipv4addr_from_v6( union unix_sockaddr
*v4addr
, const struct sockaddr_in6
*in6
, int map_unspecified
)
299 v4addr
->in
.sin_family
= AF_INET
;
300 v4addr
->in
.sin_port
= in6
->sin6_port
;
302 if (map_unspecified
&& IN6_IS_ADDR_UNSPECIFIED(&in6
->sin6_addr
))
304 v4addr
->in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
307 if (IN6_IS_ADDR_V4COMPAT(&in6
->sin6_addr
) || IN6_IS_ADDR_V4MAPPED(&in6
->sin6_addr
))
309 memcpy( &v4addr
->in
.sin_addr
.s_addr
, &in6
->sin6_addr
.s6_addr
[12], sizeof(v4addr
->in
.sin_addr
.s_addr
) );
315 static struct rb_tree bound_addresses_tree
= { addr_compare
};
317 static int should_track_conflicts_for_addr( struct sock
*sock
, const union unix_sockaddr
*addr
)
319 if (!is_tcp_socket( sock
)) return 0;
321 if (sock
->family
== WS_AF_INET
&& addr
->addr
.sa_family
== AF_INET
&& addr
->in
.sin_port
)
323 else if (sock
->family
== WS_AF_INET6
&& addr
->addr
.sa_family
== AF_INET6
&& addr
->in6
.sin6_port
)
329 static int is_any_addr( const union unix_sockaddr
*addr
)
331 if (addr
->addr
.sa_family
== AF_INET
&& addr
->in
.sin_addr
.s_addr
== htonl( INADDR_ANY
))
333 if (addr
->addr
.sa_family
== AF_INET6
&& IN6_IS_ADDR_UNSPECIFIED(&addr
->in6
.sin6_addr
))
338 static int check_addr_usage( struct sock
*sock
, const union unix_sockaddr
*addr
, int v6only
)
340 struct bound_addr
*bound_addr
, search_addr
;
341 struct rb_entry
*entry
;
343 if (!should_track_conflicts_for_addr( sock
, addr
)) return 0;
345 search_addr
.addr
= *addr
;
346 search_addr
.match_any_addr
= sock
->exclusiveaddruse
&& is_any_addr( addr
);
348 if ((entry
= rb_get( &bound_addresses_tree
, &search_addr
)))
350 bound_addr
= WINE_RB_ENTRY_VALUE(entry
, struct bound_addr
, entry
);
351 if (bound_addr
->reuse_count
== -1 || !sock
->reuseaddr
)
353 set_error( sock
->reuseaddr
|| bound_addr
->match_any_addr
354 ? STATUS_ACCESS_DENIED
: STATUS_SHARING_VIOLATION
);
359 if (sock
->family
!= WS_AF_INET6
|| v6only
) return 0;
360 if (!ipv4addr_from_v6( &search_addr
.addr
, &addr
->in6
, sock
->exclusiveaddruse
)) return 0;
362 search_addr
.match_any_addr
= sock
->exclusiveaddruse
&& is_any_addr( &search_addr
.addr
);
363 if ((entry
= rb_get( &bound_addresses_tree
, &search_addr
)))
365 bound_addr
= WINE_RB_ENTRY_VALUE(entry
, struct bound_addr
, entry
);
366 if (bound_addr
->reuse_count
== -1 || !sock
->reuseaddr
)
368 set_error( sock
->reuseaddr
|| bound_addr
->match_any_addr
369 ? STATUS_ACCESS_DENIED
: STATUS_SHARING_VIOLATION
);
376 static struct bound_addr
*register_bound_address( struct sock
*sock
, const union unix_sockaddr
*addr
)
378 struct bound_addr
*bound_addr
, *temp
;
380 if (!(bound_addr
= mem_alloc( sizeof(*bound_addr
) )))
383 bound_addr
->addr
= *addr
;
384 bound_addr
->match_any_addr
= sock
->exclusiveaddruse
&& is_any_addr( addr
);
386 if (rb_put( &bound_addresses_tree
, bound_addr
, &bound_addr
->entry
))
389 bound_addr
= WINE_RB_ENTRY_VALUE(rb_get( &bound_addresses_tree
, temp
), struct bound_addr
, entry
);
391 if (bound_addr
->reuse_count
== -1)
394 fprintf( stderr
, "register_bound_address: address being updated is already exclusively bound\n" );
397 ++bound_addr
->reuse_count
;
401 bound_addr
->reuse_count
= sock
->reuseaddr
? 1 : -1;
406 static void update_addr_usage( struct sock
*sock
, const union unix_sockaddr
*addr
, int v6only
)
408 union unix_sockaddr v4addr
;
410 assert( !sock
->bound_addr
[0] && !sock
->bound_addr
[1] );
412 if (!should_track_conflicts_for_addr( sock
, addr
)) return;
414 sock
->bound_addr
[0] = register_bound_address( sock
, addr
);
416 if (sock
->family
!= WS_AF_INET6
|| v6only
) return;
418 if (!ipv4addr_from_v6( &v4addr
, &addr
->in6
, sock
->exclusiveaddruse
)) return;
420 sock
->bound_addr
[1] = register_bound_address( sock
, &v4addr
);
423 static void sock_dump( struct object
*obj
, int verbose
);
424 static struct fd
*sock_get_fd( struct object
*obj
);
425 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
426 static void sock_destroy( struct object
*obj
);
427 static struct object
*sock_get_ifchange( struct sock
*sock
);
428 static void sock_release_ifchange( struct sock
*sock
);
430 static int sock_get_poll_events( struct fd
*fd
);
431 static void sock_poll_event( struct fd
*fd
, int event
);
432 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
433 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
434 static void sock_cancel_async( struct fd
*fd
, struct async
*async
);
435 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
437 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
);
438 static struct sock
*accept_socket( struct sock
*sock
);
439 static int sock_get_ntstatus( int err
);
440 static unsigned int sock_get_error( int err
);
441 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
442 unsigned int count
, const struct afd_poll_socket_64
*sockets
);
444 static const struct object_ops sock_ops
=
446 sizeof(struct sock
), /* size */
447 &file_type
, /* type */
448 sock_dump
, /* dump */
449 add_queue
, /* add_queue */
450 remove_queue
, /* remove_queue */
451 default_fd_signaled
, /* signaled */
452 no_satisfied
, /* satisfied */
453 no_signal
, /* signal */
454 sock_get_fd
, /* get_fd */
455 default_map_access
, /* map_access */
456 default_get_sd
, /* get_sd */
457 default_set_sd
, /* set_sd */
458 no_get_full_name
, /* get_full_name */
459 no_lookup_name
, /* lookup_name */
460 no_link_name
, /* link_name */
461 NULL
, /* unlink_name */
462 no_open_file
, /* open_file */
463 no_kernel_obj_list
, /* get_kernel_obj_list */
464 sock_close_handle
, /* close_handle */
465 sock_destroy
/* destroy */
468 static const struct fd_ops sock_fd_ops
=
470 sock_get_poll_events
, /* get_poll_events */
471 sock_poll_event
, /* poll_event */
472 sock_get_fd_type
, /* get_fd_type */
473 no_fd_read
, /* read */
474 no_fd_write
, /* write */
475 no_fd_flush
, /* flush */
476 default_fd_get_file_info
, /* get_file_info */
477 no_fd_get_volume_info
, /* get_volume_info */
478 sock_ioctl
, /* ioctl */
479 sock_cancel_async
, /* cancel_async */
480 no_fd_queue_async
, /* queue_async */
481 sock_reselect_async
/* reselect_async */
484 static int sockaddr_from_unix( const union unix_sockaddr
*uaddr
, struct WS_sockaddr
*wsaddr
, socklen_t wsaddrlen
)
486 memset( wsaddr
, 0, wsaddrlen
);
488 switch (uaddr
->addr
.sa_family
)
492 struct WS_sockaddr_in win
= {0};
494 if (wsaddrlen
< sizeof(win
)) return -1;
495 win
.sin_family
= WS_AF_INET
;
496 win
.sin_port
= uaddr
->in
.sin_port
;
497 memcpy( &win
.sin_addr
, &uaddr
->in
.sin_addr
, sizeof(win
.sin_addr
) );
498 memcpy( wsaddr
, &win
, sizeof(win
) );
504 struct WS_sockaddr_in6 win
= {0};
506 if (wsaddrlen
< sizeof(win
)) return -1;
507 win
.sin6_family
= WS_AF_INET6
;
508 win
.sin6_port
= uaddr
->in6
.sin6_port
;
509 win
.sin6_flowinfo
= uaddr
->in6
.sin6_flowinfo
;
510 memcpy( &win
.sin6_addr
, &uaddr
->in6
.sin6_addr
, sizeof(win
.sin6_addr
) );
511 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
512 win
.sin6_scope_id
= uaddr
->in6
.sin6_scope_id
;
514 memcpy( wsaddr
, &win
, sizeof(win
) );
521 struct WS_sockaddr_ipx win
= {0};
523 if (wsaddrlen
< sizeof(win
)) return -1;
524 win
.sa_family
= WS_AF_IPX
;
525 memcpy( win
.sa_netnum
, &uaddr
->ipx
.sipx_network
, sizeof(win
.sa_netnum
) );
526 memcpy( win
.sa_nodenum
, &uaddr
->ipx
.sipx_node
, sizeof(win
.sa_nodenum
) );
527 win
.sa_socket
= uaddr
->ipx
.sipx_port
;
528 memcpy( wsaddr
, &win
, sizeof(win
) );
538 if (wsaddrlen
< sizeof(win
)) return -1;
539 win
.irdaAddressFamily
= WS_AF_IRDA
;
540 memcpy( win
.irdaDeviceID
, &uaddr
->irda
.sir_addr
, sizeof(win
.irdaDeviceID
) );
541 if (uaddr
->irda
.sir_lsap_sel
!= LSAP_ANY
)
542 snprintf( win
.irdaServiceName
, sizeof(win
.irdaServiceName
), "LSAP-SEL%u", uaddr
->irda
.sir_lsap_sel
);
544 memcpy( win
.irdaServiceName
, uaddr
->irda
.sir_name
, sizeof(win
.irdaServiceName
) );
545 memcpy( wsaddr
, &win
, sizeof(win
) );
559 static socklen_t
sockaddr_to_unix( const struct WS_sockaddr
*wsaddr
, int wsaddrlen
, union unix_sockaddr
*uaddr
)
561 memset( uaddr
, 0, sizeof(*uaddr
) );
563 switch (wsaddr
->sa_family
)
567 struct WS_sockaddr_in win
= {0};
569 if (wsaddrlen
< sizeof(win
)) return 0;
570 memcpy( &win
, wsaddr
, sizeof(win
) );
571 uaddr
->in
.sin_family
= AF_INET
;
572 uaddr
->in
.sin_port
= win
.sin_port
;
573 memcpy( &uaddr
->in
.sin_addr
, &win
.sin_addr
, sizeof(win
.sin_addr
) );
574 return sizeof(uaddr
->in
);
579 struct WS_sockaddr_in6 win
= {0};
581 if (wsaddrlen
< sizeof(win
)) return 0;
582 memcpy( &win
, wsaddr
, sizeof(win
) );
583 uaddr
->in6
.sin6_family
= AF_INET6
;
584 uaddr
->in6
.sin6_port
= win
.sin6_port
;
585 uaddr
->in6
.sin6_flowinfo
= win
.sin6_flowinfo
;
586 memcpy( &uaddr
->in6
.sin6_addr
, &win
.sin6_addr
, sizeof(win
.sin6_addr
) );
587 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
588 uaddr
->in6
.sin6_scope_id
= win
.sin6_scope_id
;
590 return sizeof(uaddr
->in6
);
596 struct WS_sockaddr_ipx win
= {0};
598 if (wsaddrlen
< sizeof(win
)) return 0;
599 memcpy( &win
, wsaddr
, sizeof(win
) );
600 uaddr
->ipx
.sipx_family
= AF_IPX
;
601 memcpy( &uaddr
->ipx
.sipx_network
, win
.sa_netnum
, sizeof(win
.sa_netnum
) );
602 memcpy( &uaddr
->ipx
.sipx_node
, win
.sa_nodenum
, sizeof(win
.sa_nodenum
) );
603 uaddr
->ipx
.sipx_port
= win
.sa_socket
;
604 return sizeof(uaddr
->ipx
);
611 SOCKADDR_IRDA win
= {0};
612 unsigned int lsap_sel
;
614 if (wsaddrlen
< sizeof(win
)) return 0;
615 memcpy( &win
, wsaddr
, sizeof(win
) );
616 uaddr
->irda
.sir_family
= AF_IRDA
;
617 if (sscanf( win
.irdaServiceName
, "LSAP-SEL%u", &lsap_sel
) == 1)
618 uaddr
->irda
.sir_lsap_sel
= lsap_sel
;
621 uaddr
->irda
.sir_lsap_sel
= LSAP_ANY
;
622 memcpy( uaddr
->irda
.sir_name
, win
.irdaServiceName
, sizeof(win
.irdaServiceName
) );
624 memcpy( &uaddr
->irda
.sir_addr
, win
.irdaDeviceID
, sizeof(win
.irdaDeviceID
) );
625 return sizeof(uaddr
->irda
);
632 default: /* likely an ipv4 address */
633 case sizeof(struct WS_sockaddr_in
):
634 return sizeof(uaddr
->in
);
637 case sizeof(struct WS_sockaddr_ipx
):
638 return sizeof(uaddr
->ipx
);
642 case sizeof(SOCKADDR_IRDA
):
643 return sizeof(uaddr
->irda
);
646 case sizeof(struct WS_sockaddr_in6
):
647 return sizeof(uaddr
->in6
);
655 static socklen_t
get_unix_sockaddr_any( union unix_sockaddr
*uaddr
, int ws_family
)
657 memset( uaddr
, 0, sizeof(*uaddr
) );
661 uaddr
->in
.sin_family
= AF_INET
;
662 return sizeof(uaddr
->in
);
664 uaddr
->in6
.sin6_family
= AF_INET6
;
665 return sizeof(uaddr
->in6
);
668 uaddr
->ipx
.sipx_family
= AF_IPX
;
669 return sizeof(uaddr
->ipx
);
673 uaddr
->irda
.sir_family
= AF_IRDA
;
674 return sizeof(uaddr
->irda
);
681 /* some events are generated at the same time but must be sent in a particular
682 * order (e.g. CONNECT must be sent before READ) */
683 static const enum afd_poll_bit event_bitorder
[] =
685 AFD_POLL_BIT_CONNECT
,
686 AFD_POLL_BIT_CONNECT_ERR
,
697 SOCK_SHUTDOWN_ERROR
= -1,
698 SOCK_SHUTDOWN_EOF
= 0,
699 SOCK_SHUTDOWN_POLLHUP
= 1
702 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
704 static sock_shutdown_t
sock_check_pollhup(void)
706 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
711 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
712 if ( shutdown( fd
[0], 1 ) ) goto out
;
718 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
719 n
= poll( &pfd
, 1, 1 );
720 if ( n
!= 1 ) goto out
; /* error or timeout */
721 if ( pfd
.revents
& POLLHUP
)
722 ret
= SOCK_SHUTDOWN_POLLHUP
;
723 else if ( pfd
.revents
& POLLIN
&&
724 read( fd
[1], &dummy
, 1 ) == 0 )
725 ret
= SOCK_SHUTDOWN_EOF
;
735 sock_shutdown_type
= sock_check_pollhup();
737 switch ( sock_shutdown_type
)
739 case SOCK_SHUTDOWN_EOF
:
740 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
742 case SOCK_SHUTDOWN_POLLHUP
:
743 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
746 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
747 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
751 static void sock_reselect( struct sock
*sock
)
753 int ev
= sock_get_poll_events( sock
->fd
);
756 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
758 set_fd_events( sock
->fd
, ev
);
761 static unsigned int afd_poll_flag_to_win32( unsigned int flags
)
763 static const unsigned int map
[] =
767 FD_WRITE
, /* WRITE */
769 FD_CLOSE
, /* RESET */
771 FD_CONNECT
, /* CONNECT */
772 FD_ACCEPT
, /* ACCEPT */
773 FD_CONNECT
, /* CONNECT_ERR */
776 unsigned int i
, ret
= 0;
778 for (i
= 0; i
< ARRAY_SIZE(map
); ++i
)
780 if (flags
& (1 << i
)) ret
|= map
[i
];
786 /* wake anybody waiting on the socket event or send the associated message */
787 static void sock_wake_up( struct sock
*sock
)
789 unsigned int events
= sock
->pending_events
& sock
->mask
;
794 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
796 set_event( sock
->event
);
800 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
801 for (i
= 0; i
< ARRAY_SIZE(event_bitorder
); i
++)
803 enum afd_poll_bit event
= event_bitorder
[i
];
804 if (events
& (1 << event
))
806 lparam_t lparam
= afd_poll_flag_to_win32(1 << event
) | (sock_get_error( sock
->errors
[event
] ) << 16);
807 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
810 sock
->pending_events
= 0;
811 sock_reselect( sock
);
815 static inline int sock_error( struct sock
*sock
)
818 socklen_t len
= sizeof(error
);
820 getsockopt( get_unix_fd(sock
->fd
), SOL_SOCKET
, SO_ERROR
, (void *)&error
, &len
);
824 case SOCK_UNCONNECTED
:
827 case SOCK_CONNECTING
:
829 sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] = error
;
831 error
= sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
];
836 sock
->errors
[AFD_POLL_BIT_ACCEPT
] = error
;
838 error
= sock
->errors
[AFD_POLL_BIT_ACCEPT
];
842 case SOCK_CONNECTIONLESS
:
843 if (error
== ECONNRESET
|| error
== EPIPE
)
849 sock
->errors
[AFD_POLL_BIT_HUP
] = error
;
851 error
= sock
->errors
[AFD_POLL_BIT_HUP
];
858 static void free_accept_req( void *private )
860 struct accept_req
*req
= private;
861 list_remove( &req
->entry
);
864 req
->acceptsock
->accept_recv_req
= NULL
;
865 release_object( req
->acceptsock
);
867 release_object( req
->async
);
868 release_object( req
->iosb
);
869 release_object( req
->sock
);
873 static void fill_accept_output( struct accept_req
*req
)
875 const data_size_t out_size
= req
->iosb
->out_size
;
876 struct async
*async
= req
->async
;
877 union unix_sockaddr unix_addr
;
878 struct WS_sockaddr
*win_addr
;
879 unsigned int remote_len
;
885 if (!(out_data
= mem_alloc( out_size
)))
887 async_terminate( async
, get_error() );
891 fd
= get_unix_fd( req
->acceptsock
->fd
);
893 if (req
->recv_len
&& (size
= recv( fd
, out_data
, req
->recv_len
, 0 )) < 0)
895 if (!req
->accepted
&& errno
== EWOULDBLOCK
)
898 sock_reselect( req
->acceptsock
);
902 async_terminate( async
, sock_get_ntstatus( errno
) );
909 if (req
->local_len
< sizeof(int))
911 async_terminate( async
, STATUS_BUFFER_TOO_SMALL
);
916 unix_len
= sizeof(unix_addr
);
917 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ sizeof(int));
918 if (getsockname( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
919 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, req
->local_len
- sizeof(int) )) < 0)
921 async_terminate( async
, sock_get_ntstatus( errno
) );
925 memcpy( out_data
+ req
->recv_len
, &win_len
, sizeof(int) );
928 unix_len
= sizeof(unix_addr
);
929 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ req
->local_len
+ sizeof(int));
930 remote_len
= out_size
- req
->recv_len
- req
->local_len
;
931 if (getpeername( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
932 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, remote_len
- sizeof(int) )) < 0)
934 async_terminate( async
, sock_get_ntstatus( errno
) );
938 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
940 async_request_complete( req
->async
, STATUS_SUCCESS
, size
, out_size
, out_data
);
943 static void complete_async_accept( struct sock
*sock
, struct accept_req
*req
)
945 struct sock
*acceptsock
= req
->acceptsock
;
946 struct async
*async
= req
->async
;
948 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
952 if (!accept_into_socket( sock
, acceptsock
))
954 async_terminate( async
, get_error() );
957 fill_accept_output( req
);
963 if (!(acceptsock
= accept_socket( sock
)))
965 async_terminate( async
, get_error() );
968 handle
= alloc_handle_no_access_check( async_get_thread( async
)->process
, &acceptsock
->obj
,
969 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
970 acceptsock
->wparam
= handle
;
971 sock_reselect( acceptsock
);
972 release_object( acceptsock
);
975 async_terminate( async
, get_error() );
979 async_request_complete_alloc( req
->async
, STATUS_SUCCESS
, 0, sizeof(handle
), &handle
);
983 static void complete_async_accept_recv( struct accept_req
*req
)
985 if (debug_level
) fprintf( stderr
, "completing accept recv request for socket %p\n", req
->acceptsock
);
987 assert( req
->recv_len
);
989 fill_accept_output( req
);
992 static void free_connect_req( void *private )
994 struct connect_req
*req
= private;
996 req
->sock
->connect_req
= NULL
;
997 release_object( req
->async
);
998 release_object( req
->iosb
);
999 release_object( req
->sock
);
1003 static void complete_async_connect( struct sock
*sock
)
1005 struct connect_req
*req
= sock
->connect_req
;
1006 const char *in_buffer
;
1010 if (debug_level
) fprintf( stderr
, "completing connect request for socket %p\n", sock
);
1014 async_terminate( req
->async
, STATUS_SUCCESS
);
1018 in_buffer
= (const char *)req
->iosb
->in_data
+ sizeof(struct afd_connect_params
) + req
->addr_len
;
1019 len
= req
->send_len
- req
->send_cursor
;
1021 ret
= send( get_unix_fd( sock
->fd
), in_buffer
+ req
->send_cursor
, len
, 0 );
1022 if (ret
< 0 && errno
!= EWOULDBLOCK
)
1023 async_terminate( req
->async
, sock_get_ntstatus( errno
) );
1024 else if (ret
== len
)
1025 async_request_complete( req
->async
, STATUS_SUCCESS
, req
->send_len
, 0, NULL
);
1027 req
->send_cursor
+= ret
;
1030 static void free_poll_req( void *private )
1032 struct poll_req
*req
= private;
1035 if (req
->timeout
) remove_timeout_user( req
->timeout
);
1037 for (i
= 0; i
< req
->count
; ++i
)
1038 release_object( req
->sockets
[i
].sock
);
1039 release_object( req
->async
);
1040 release_object( req
->iosb
);
1041 list_remove( &req
->entry
);
1045 static int is_oobinline( struct sock
*sock
)
1048 socklen_t len
= sizeof(oobinline
);
1049 return !getsockopt( get_unix_fd( sock
->fd
), SOL_SOCKET
, SO_OOBINLINE
, (char *)&oobinline
, &len
) && oobinline
;
1052 static int get_poll_flags( struct sock
*sock
, int event
)
1056 /* A connection-mode socket which has never been connected does not return
1057 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
1058 if (sock
->state
== SOCK_UNCONNECTED
)
1059 event
&= ~(POLLOUT
| POLLHUP
);
1063 if (sock
->state
== SOCK_LISTENING
)
1064 flags
|= AFD_POLL_ACCEPT
;
1066 flags
|= AFD_POLL_READ
;
1068 if (event
& POLLPRI
)
1069 flags
|= is_oobinline( sock
) ? AFD_POLL_READ
: AFD_POLL_OOB
;
1070 if (event
& POLLOUT
)
1071 flags
|= AFD_POLL_WRITE
;
1072 if (sock
->state
== SOCK_CONNECTED
)
1073 flags
|= AFD_POLL_CONNECT
;
1074 if (event
& POLLHUP
)
1075 flags
|= AFD_POLL_HUP
;
1076 if (event
& POLLERR
)
1077 flags
|= AFD_POLL_CONNECT_ERR
;
1079 flags
|= AFD_POLL_RESET
;
1084 static void complete_async_poll( struct poll_req
*req
, unsigned int status
)
1086 unsigned int i
, signaled_count
= 0;
1088 for (i
= 0; i
< req
->count
; ++i
)
1090 struct sock
*sock
= req
->sockets
[i
].sock
;
1092 if (sock
->main_poll
== req
)
1093 sock
->main_poll
= NULL
;
1098 for (i
= 0; i
< req
->count
; ++i
)
1100 if (req
->sockets
[i
].flags
)
1105 if (is_machine_64bit( async_get_thread( req
->async
)->process
->machine
))
1107 size_t output_size
= offsetof( struct afd_poll_params_64
, sockets
[signaled_count
] );
1108 struct afd_poll_params_64
*output
;
1110 if (!(output
= mem_alloc( output_size
)))
1112 async_terminate( req
->async
, get_error() );
1115 memset( output
, 0, output_size
);
1116 output
->timeout
= req
->orig_timeout
;
1117 output
->exclusive
= req
->exclusive
;
1118 for (i
= 0; i
< req
->count
; ++i
)
1120 if (!req
->sockets
[i
].flags
) continue;
1121 output
->sockets
[output
->count
].socket
= req
->sockets
[i
].handle
;
1122 output
->sockets
[output
->count
].flags
= req
->sockets
[i
].flags
;
1123 output
->sockets
[output
->count
].status
= req
->sockets
[i
].status
;
1126 assert( output
->count
== signaled_count
);
1128 async_request_complete( req
->async
, status
, output_size
, output_size
, output
);
1132 size_t output_size
= offsetof( struct afd_poll_params_32
, sockets
[signaled_count
] );
1133 struct afd_poll_params_32
*output
;
1135 if (!(output
= mem_alloc( output_size
)))
1137 async_terminate( req
->async
, get_error() );
1140 memset( output
, 0, output_size
);
1141 output
->timeout
= req
->orig_timeout
;
1142 output
->exclusive
= req
->exclusive
;
1143 for (i
= 0; i
< req
->count
; ++i
)
1145 if (!req
->sockets
[i
].flags
) continue;
1146 output
->sockets
[output
->count
].socket
= req
->sockets
[i
].handle
;
1147 output
->sockets
[output
->count
].flags
= req
->sockets
[i
].flags
;
1148 output
->sockets
[output
->count
].status
= req
->sockets
[i
].status
;
1151 assert( output
->count
== signaled_count
);
1153 async_request_complete( req
->async
, status
, output_size
, output_size
, output
);
1157 static void complete_async_polls( struct sock
*sock
, int event
, int error
)
1159 int flags
= get_poll_flags( sock
, event
);
1160 struct poll_req
*req
, *next
;
1162 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &poll_list
, struct poll_req
, entry
)
1166 if (req
->iosb
->status
!= STATUS_PENDING
) continue;
1168 for (i
= 0; i
< req
->count
; ++i
)
1170 if (req
->sockets
[i
].sock
!= sock
) continue;
1171 if (!(req
->sockets
[i
].mask
& flags
)) continue;
1174 fprintf( stderr
, "completing poll for socket %p, wanted %#x got %#x\n",
1175 sock
, req
->sockets
[i
].mask
, flags
);
1177 req
->sockets
[i
].flags
= req
->sockets
[i
].mask
& flags
;
1178 req
->sockets
[i
].status
= sock_get_ntstatus( error
);
1182 complete_async_poll( req
, STATUS_SUCCESS
);
1189 static void async_poll_timeout( void *private )
1191 struct poll_req
*req
= private;
1193 req
->timeout
= NULL
;
1195 if (req
->iosb
->status
!= STATUS_PENDING
) return;
1197 complete_async_poll( req
, STATUS_TIMEOUT
);
1200 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
1202 if (event
& (POLLIN
| POLLPRI
))
1204 struct accept_req
*req
;
1206 LIST_FOR_EACH_ENTRY( req
, &sock
->accept_list
, struct accept_req
, entry
)
1208 if (req
->iosb
->status
== STATUS_PENDING
&& !req
->accepted
)
1210 complete_async_accept( sock
, req
);
1216 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
1217 complete_async_accept_recv( sock
->accept_recv_req
);
1220 if ((event
& POLLOUT
) && sock
->connect_req
&& sock
->connect_req
->iosb
->status
== STATUS_PENDING
)
1221 complete_async_connect( sock
);
1223 if ((event
& (POLLIN
| POLLPRI
)) && async_queued( &sock
->read_q
))
1225 if (async_waiting( &sock
->read_q
))
1227 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
1228 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
1230 event
&= ~(POLLIN
| POLLPRI
);
1233 if ((event
& POLLOUT
) && async_queued( &sock
->write_q
))
1235 if (async_waiting( &sock
->write_q
))
1237 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
1238 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
1243 if (event
& (POLLERR
| POLLHUP
))
1245 int status
= sock_get_ntstatus( error
);
1246 struct accept_req
*req
, *next
;
1248 async_wake_up( &sock
->read_q
, status
);
1249 async_wake_up( &sock
->write_q
, status
);
1251 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
1253 if (req
->iosb
->status
== STATUS_PENDING
)
1254 async_terminate( req
->async
, status
);
1257 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
1258 async_terminate( sock
->accept_recv_req
->async
, status
);
1260 if (sock
->connect_req
)
1261 async_terminate( sock
->connect_req
->async
, status
);
1266 async_wake_up( &sock
->read_q
, STATUS_CONNECTION_RESET
);
1267 async_wake_up( &sock
->write_q
, STATUS_CONNECTION_RESET
);
1269 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
1270 async_terminate( sock
->accept_recv_req
->async
, STATUS_CONNECTION_RESET
);
1276 static void post_socket_event( struct sock
*sock
, enum afd_poll_bit event_bit
)
1278 unsigned int event
= (1 << event_bit
);
1280 if (!(sock
->reported_events
& event
))
1282 sock
->pending_events
|= event
;
1283 sock
->reported_events
|= event
;
1287 static void sock_dispatch_events( struct sock
*sock
, enum connection_state prevstate
, int event
)
1291 case SOCK_UNCONNECTED
:
1294 case SOCK_CONNECTING
:
1295 if (event
& POLLOUT
)
1296 post_socket_event( sock
, AFD_POLL_BIT_CONNECT
);
1297 if (event
& (POLLERR
| POLLHUP
))
1298 post_socket_event( sock
, AFD_POLL_BIT_CONNECT_ERR
);
1301 case SOCK_LISTENING
:
1302 if (event
& (POLLIN
| POLLERR
| POLLHUP
))
1303 post_socket_event( sock
, AFD_POLL_BIT_ACCEPT
);
1306 case SOCK_CONNECTED
:
1307 case SOCK_CONNECTIONLESS
:
1309 post_socket_event( sock
, AFD_POLL_BIT_RESET
);
1312 post_socket_event( sock
, AFD_POLL_BIT_READ
);
1314 if (event
& POLLOUT
)
1315 post_socket_event( sock
, AFD_POLL_BIT_WRITE
);
1317 if (event
& POLLPRI
)
1318 post_socket_event( sock
, AFD_POLL_BIT_OOB
);
1320 if (event
& (POLLERR
| POLLHUP
))
1321 post_socket_event( sock
, AFD_POLL_BIT_HUP
);
1325 sock_wake_up( sock
);
1328 static void sock_poll_event( struct fd
*fd
, int event
)
1330 struct sock
*sock
= get_fd_user( fd
);
1331 int hangup_seen
= 0;
1332 enum connection_state prevstate
= sock
->state
;
1335 assert( sock
->obj
.ops
== &sock_ops
);
1337 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
1339 if (event
& (POLLERR
| POLLHUP
))
1340 error
= sock_error( sock
);
1342 switch (sock
->state
)
1344 case SOCK_UNCONNECTED
:
1347 case SOCK_CONNECTING
:
1348 if (event
& (POLLERR
|POLLHUP
))
1350 sock
->state
= SOCK_UNCONNECTED
;
1353 else if (event
& POLLOUT
)
1355 sock
->state
= SOCK_CONNECTED
;
1356 sock
->connect_time
= current_time
;
1357 sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] = 0;
1361 case SOCK_LISTENING
:
1364 case SOCK_CONNECTED
:
1365 case SOCK_CONNECTIONLESS
:
1367 event
&= ~(POLLIN
| POLLERR
| POLLHUP
);
1369 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
1374 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
1375 * has been closed, so we need to check for it explicitly here */
1376 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
1385 /* EAGAIN can happen if an async recv() falls between the server's poll()
1386 call and the invocation of this routine */
1387 if (errno
== ECONNRESET
|| errno
== EPIPE
)
1391 else if (errno
!= EAGAIN
)
1395 sock
->errors
[AFD_POLL_BIT_HUP
] = error
;
1397 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
1402 if (hangup_seen
|| (sock_shutdown_type
== SOCK_SHUTDOWN_POLLHUP
&& (event
& POLLHUP
)))
1406 else if (event
& (POLLHUP
| POLLERR
))
1411 fprintf( stderr
, "socket %p aborted by error %d, event %#x\n", sock
, error
, event
);
1419 event
= sock_dispatch_asyncs( sock
, event
, error
);
1420 sock_dispatch_events( sock
, prevstate
, event
);
1421 complete_async_polls( sock
, event
, error
);
1423 sock_reselect( sock
);
1426 static void sock_dump( struct object
*obj
, int verbose
)
1428 struct sock
*sock
= (struct sock
*)obj
;
1429 assert( obj
->ops
== &sock_ops
);
1430 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
1431 sock
->fd
, sock
->state
,
1432 sock
->mask
, sock
->pending_events
, sock
->reported_events
);
1435 static int poll_flags_from_afd( struct sock
*sock
, int flags
)
1439 /* A connection-mode socket which has never been connected does
1440 * not return write or hangup events, but Linux returns
1441 * POLLOUT | POLLHUP. */
1442 if (sock
->state
== SOCK_UNCONNECTED
)
1445 if (flags
& (AFD_POLL_READ
| AFD_POLL_ACCEPT
))
1447 if ((flags
& AFD_POLL_HUP
) && sock
->type
== WS_SOCK_STREAM
)
1449 if (flags
& AFD_POLL_OOB
)
1450 ev
|= is_oobinline( sock
) ? POLLIN
: POLLPRI
;
1451 if (flags
& AFD_POLL_WRITE
)
1457 static int sock_get_poll_events( struct fd
*fd
)
1459 struct sock
*sock
= get_fd_user( fd
);
1460 unsigned int mask
= sock
->mask
& ~sock
->reported_events
;
1461 struct poll_req
*req
;
1464 assert( sock
->obj
.ops
== &sock_ops
);
1466 if (!sock
->type
) /* not initialized yet */
1469 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1473 for (i
= 0; i
< req
->count
; ++i
)
1475 if (req
->sockets
[i
].sock
!= sock
) continue;
1477 ev
|= poll_flags_from_afd( sock
, req
->sockets
[i
].mask
);
1481 switch (sock
->state
)
1483 case SOCK_UNCONNECTED
:
1484 /* A connection-mode Windows socket which has never been connected does
1485 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1486 * need to return -1 here, to prevent the socket from being polled on at
1490 case SOCK_CONNECTING
:
1493 case SOCK_LISTENING
:
1494 if (!list_empty( &sock
->accept_list
) || (mask
& AFD_POLL_ACCEPT
))
1498 case SOCK_CONNECTED
:
1499 case SOCK_CONNECTIONLESS
:
1500 if (sock
->hangup
&& sock
->wr_shutdown
&& !sock
->wr_shutdown_pending
)
1502 /* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
1503 * if both the socket and its peer are SHUT_WR.
1505 * We don't use SHUT_RD, so we can only encounter this in the latter
1506 * case. In that case there can't be any pending read requests (they
1507 * would have already been completed with a length of zero), the
1508 * above condition ensures that we don't have any pending write
1509 * requests, and nothing that can change about the socket state that
1510 * would complete a pending poll request. */
1514 if (sock
->aborted
|| sock
->reset
)
1517 if (sock
->accept_recv_req
)
1521 else if (async_queued( &sock
->read_q
))
1523 /* Clear POLLIN and POLLPRI if we have an alerted async, even if
1524 * we're polling this socket for READ or OOB. We can't signal the
1525 * poll if the pending async will read all of the data [cf. the
1526 * matching logic in sock_dispatch_asyncs()], but we also don't
1527 * want to spin polling for POLLIN if we're not going to use it. */
1528 if (async_waiting( &sock
->read_q
))
1529 ev
|= POLLIN
| POLLPRI
;
1531 ev
&= ~(POLLIN
| POLLPRI
);
1535 /* Don't ask for POLLIN if we got a hangup. We won't receive more
1536 * data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
1539 if (mask
& AFD_POLL_READ
)
1541 if (mask
& AFD_POLL_OOB
)
1545 /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
1546 if (sock
->state
== SOCK_CONNECTED
&& (mask
& AFD_POLL_HUP
) && !(sock
->reported_events
& AFD_POLL_READ
))
1550 if (async_queued( &sock
->write_q
))
1552 /* As with read asyncs above, clear POLLOUT if we have an alerted
1554 if (async_waiting( &sock
->write_q
))
1559 else if (!sock
->wr_shutdown
&& (mask
& AFD_POLL_WRITE
))
1570 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
1572 return FD_TYPE_SOCKET
;
1575 static void sock_cancel_async( struct fd
*fd
, struct async
*async
)
1577 struct poll_req
*req
;
1579 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1583 if (req
->async
!= async
)
1586 for (i
= 0; i
< req
->count
; i
++)
1588 struct sock
*sock
= req
->sockets
[i
].sock
;
1590 if (sock
->main_poll
== req
)
1591 sock
->main_poll
= NULL
;
1595 async_terminate( async
, STATUS_CANCELLED
);
1598 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
1600 struct sock
*sock
= get_fd_user( fd
);
1602 if (sock
->wr_shutdown_pending
&& list_empty( &sock
->write_q
.queue
))
1604 shutdown( get_unix_fd( sock
->fd
), SHUT_WR
);
1605 sock
->wr_shutdown_pending
= 0;
1608 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1609 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1611 if (queue
!= &sock
->ifchange_q
&& sock
->type
)
1612 sock_reselect( sock
);
1615 static struct fd
*sock_get_fd( struct object
*obj
)
1617 struct sock
*sock
= (struct sock
*)obj
;
1618 return (struct fd
*)grab_object( sock
->fd
);
1621 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
1623 struct sock
*sock
= (struct sock
*)obj
;
1625 if (sock
->obj
.handle_count
== 1) /* last handle */
1627 struct accept_req
*accept_req
, *accept_next
;
1628 struct poll_req
*poll_req
, *poll_next
;
1630 if (sock
->accept_recv_req
)
1631 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
1633 LIST_FOR_EACH_ENTRY_SAFE( accept_req
, accept_next
, &sock
->accept_list
, struct accept_req
, entry
)
1634 async_terminate( accept_req
->async
, STATUS_CANCELLED
);
1636 if (sock
->connect_req
)
1637 async_terminate( sock
->connect_req
->async
, STATUS_CANCELLED
);
1639 LIST_FOR_EACH_ENTRY_SAFE( poll_req
, poll_next
, &poll_list
, struct poll_req
, entry
)
1641 struct iosb
*iosb
= poll_req
->iosb
;
1642 BOOL signaled
= FALSE
;
1645 if (iosb
->status
!= STATUS_PENDING
) continue;
1647 for (i
= 0; i
< poll_req
->count
; ++i
)
1649 if (poll_req
->sockets
[i
].sock
== sock
)
1652 poll_req
->sockets
[i
].flags
= AFD_POLL_CLOSE
;
1653 poll_req
->sockets
[i
].status
= 0;
1657 if (signaled
) complete_async_poll( poll_req
, STATUS_SUCCESS
);
1660 return async_close_obj_handle( obj
, process
, handle
);
1663 static void sock_destroy( struct object
*obj
)
1665 struct sock
*sock
= (struct sock
*)obj
;
1668 assert( obj
->ops
== &sock_ops
);
1670 /* FIXME: special socket shutdown stuff? */
1672 for (i
= 0; i
< 2; ++i
)
1674 if (sock
->bound_addr
[i
] && --sock
->bound_addr
[i
]->reuse_count
<= 0)
1676 rb_remove( &bound_addresses_tree
, &sock
->bound_addr
[i
]->entry
);
1677 free( sock
->bound_addr
[i
] );
1681 if ( sock
->deferred
)
1682 release_object( sock
->deferred
);
1684 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
1685 sock_release_ifchange( sock
);
1686 free_async_queue( &sock
->read_q
);
1687 free_async_queue( &sock
->write_q
);
1688 free_async_queue( &sock
->ifchange_q
);
1689 free_async_queue( &sock
->accept_q
);
1690 free_async_queue( &sock
->connect_q
);
1691 free_async_queue( &sock
->poll_q
);
1692 if (sock
->event
) release_object( sock
->event
);
1693 if (sock
->fd
) release_object( sock
->fd
);
1696 static struct sock
*create_socket(void)
1700 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
1702 sock
->state
= SOCK_UNCONNECTED
;
1704 sock
->pending_events
= 0;
1705 sock
->reported_events
= 0;
1713 sock
->connect_time
= 0;
1714 sock
->deferred
= NULL
;
1715 sock
->ifchange_obj
= NULL
;
1716 sock
->accept_recv_req
= NULL
;
1717 sock
->connect_req
= NULL
;
1718 sock
->main_poll
= NULL
;
1719 memset( &sock
->addr
, 0, sizeof(sock
->addr
) );
1721 sock
->rd_shutdown
= 0;
1722 sock
->wr_shutdown
= 0;
1723 sock
->wr_shutdown_pending
= 0;
1726 sock
->nonblocking
= 0;
1729 sock
->reuseaddr
= 0;
1730 sock
->exclusiveaddruse
= 0;
1735 sock
->icmp_fixup_data_len
= 0;
1736 sock
->bound_addr
[0] = sock
->bound_addr
[1] = NULL
;
1737 init_async_queue( &sock
->read_q
);
1738 init_async_queue( &sock
->write_q
);
1739 init_async_queue( &sock
->ifchange_q
);
1740 init_async_queue( &sock
->accept_q
);
1741 init_async_queue( &sock
->connect_q
);
1742 init_async_queue( &sock
->poll_q
);
1743 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
1744 list_init( &sock
->accept_list
);
1748 static int get_unix_family( int family
)
1752 case WS_AF_INET
: return AF_INET
;
1753 case WS_AF_INET6
: return AF_INET6
;
1755 case WS_AF_IPX
: return AF_IPX
;
1758 case WS_AF_IRDA
: return AF_IRDA
;
1760 case WS_AF_UNSPEC
: return AF_UNSPEC
;
1765 static int get_unix_type( int type
)
1769 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
1770 case WS_SOCK_RAW
: return SOCK_RAW
;
1771 case WS_SOCK_STREAM
: return SOCK_STREAM
;
1776 static int get_unix_protocol( int protocol
)
1778 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1783 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
1784 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1785 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1786 case WS_IPPROTO_IPV4
: return IPPROTO_IPIP
;
1787 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1788 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1789 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1790 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1795 static void set_dont_fragment( int fd
, int level
, int value
)
1799 if (level
== IPPROTO_IP
)
1802 optname
= IP_DONTFRAG
;
1803 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1804 optname
= IP_MTU_DISCOVER
;
1805 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1812 #ifdef IPV6_DONTFRAG
1813 optname
= IPV6_DONTFRAG
;
1814 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1815 optname
= IPV6_MTU_DISCOVER
;
1816 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1822 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1825 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
)
1827 unsigned int options
= 0;
1828 int sockfd
, unix_type
, unix_family
, unix_protocol
, value
;
1831 unix_family
= get_unix_family( family
);
1832 unix_type
= get_unix_type( type
);
1833 unix_protocol
= get_unix_protocol( protocol
);
1835 if (unix_protocol
< 0)
1837 if (type
&& unix_type
< 0)
1838 set_win32_error( WSAESOCKTNOSUPPORT
);
1840 set_win32_error( WSAEPROTONOSUPPORT
);
1843 if (unix_family
< 0)
1845 if (family
>= 0 && unix_type
< 0)
1846 set_win32_error( WSAESOCKTNOSUPPORT
);
1848 set_win32_error( WSAEAFNOSUPPORT
);
1852 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1855 if (sockfd
== -1 && errno
== EPERM
&& unix_family
== AF_INET
1856 && unix_type
== SOCK_RAW
&& unix_protocol
== IPPROTO_ICMP
)
1858 sockfd
= socket( unix_family
, SOCK_DGRAM
, unix_protocol
);
1863 setsockopt( sockfd
, IPPROTO_IP
, IP_RECVTTL
, (const char *)&val
, sizeof(val
) );
1864 setsockopt( sockfd
, IPPROTO_IP
, IP_RECVTOS
, (const char *)&val
, sizeof(val
) );
1865 setsockopt( sockfd
, IPPROTO_IP
, IP_PKTINFO
, (const char *)&val
, sizeof(val
) );
1872 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1873 else set_win32_error( sock_get_error( errno
));
1876 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1878 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1881 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1884 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1887 /* Should we retrieve val using a getsockopt call and then
1888 * set the modified one? */
1889 val
.ipx_pt
= ipx_type
;
1890 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1895 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1897 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1898 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1899 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1900 else if (unix_type
== SOCK_STREAM
)
1901 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1905 if (unix_family
== AF_INET6
)
1907 static const int enable
= 1;
1908 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1912 len
= sizeof(value
);
1913 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_RCVBUF
, &value
, &len
))
1914 sock
->rcvbuf
= value
;
1916 len
= sizeof(value
);
1917 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_SNDBUF
, &value
, &len
))
1918 sock
->sndbuf
= value
;
1920 sock
->state
= (type
== WS_SOCK_STREAM
? SOCK_UNCONNECTED
: SOCK_CONNECTIONLESS
);
1921 sock
->proto
= protocol
;
1923 sock
->family
= family
;
1925 if (is_tcp_socket( sock
))
1928 setsockopt( sockfd
, SOL_SOCKET
, SO_REUSEADDR
, &value
, sizeof(value
) );
1931 setsockopt( sockfd
, IPPROTO_TCP
, TCP_SYNCNT
, &value
, sizeof(value
) );
1937 options
= get_fd_options( sock
->fd
);
1938 release_object( sock
->fd
);
1941 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1946 /* We can't immediately allow caching for a connection-mode socket, since it
1947 * might be accepted into (changing the underlying fd object.) */
1948 if (sock
->type
!= WS_SOCK_STREAM
) allow_fd_caching( sock
->fd
);
1953 /* accepts a socket and inits it */
1954 static int accept_new_fd( struct sock
*sock
)
1957 /* Try to accept(2). We can't be safe that this an already connected socket
1958 * or that accept() is allowed on it. In those cases we will get -1/errno
1961 struct sockaddr saddr
;
1962 socklen_t slen
= sizeof(saddr
);
1963 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1965 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1967 set_error( sock_get_ntstatus( errno
));
1971 /* accept a socket (creates a new fd) */
1972 static struct sock
*accept_socket( struct sock
*sock
)
1974 struct sock
*acceptsock
;
1977 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1979 if ( sock
->deferred
)
1981 acceptsock
= sock
->deferred
;
1982 sock
->deferred
= NULL
;
1986 union unix_sockaddr unix_addr
;
1989 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1990 if (!(acceptsock
= create_socket()))
1996 /* newly created socket gets the same properties of the listening socket */
1997 acceptsock
->state
= SOCK_CONNECTED
;
1998 acceptsock
->bound
= 1;
1999 acceptsock
->nonblocking
= sock
->nonblocking
;
2000 acceptsock
->mask
= sock
->mask
;
2001 acceptsock
->proto
= sock
->proto
;
2002 acceptsock
->type
= sock
->type
;
2003 acceptsock
->family
= sock
->family
;
2004 acceptsock
->window
= sock
->window
;
2005 acceptsock
->message
= sock
->message
;
2006 acceptsock
->reuseaddr
= sock
->reuseaddr
;
2007 acceptsock
->exclusiveaddruse
= sock
->exclusiveaddruse
;
2008 acceptsock
->sndbuf
= sock
->sndbuf
;
2009 acceptsock
->rcvbuf
= sock
->rcvbuf
;
2010 acceptsock
->sndtimeo
= sock
->sndtimeo
;
2011 acceptsock
->rcvtimeo
= sock
->rcvtimeo
;
2012 acceptsock
->connect_time
= current_time
;
2014 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
2015 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
2016 get_fd_options( sock
->fd
) )))
2018 release_object( acceptsock
);
2021 unix_len
= sizeof(unix_addr
);
2022 if (!getsockname( acceptfd
, &unix_addr
.addr
, &unix_len
))
2023 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
2026 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
2027 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
2028 sock_reselect( sock
);
2032 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
2034 union unix_sockaddr unix_addr
;
2039 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
2041 if ( sock
->deferred
)
2043 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
2044 get_fd_options( acceptsock
->fd
) );
2048 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
2050 release_object( sock
->deferred
);
2051 sock
->deferred
= NULL
;
2055 if ((acceptfd
= accept_new_fd( sock
)) == -1)
2058 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
2059 get_fd_options( acceptsock
->fd
) )))
2063 acceptsock
->state
= SOCK_CONNECTED
;
2064 acceptsock
->bound
= 1;
2065 acceptsock
->pending_events
= 0;
2066 acceptsock
->reported_events
= 0;
2067 acceptsock
->proto
= sock
->proto
;
2068 acceptsock
->type
= sock
->type
;
2069 acceptsock
->family
= sock
->family
;
2070 acceptsock
->wparam
= 0;
2071 acceptsock
->deferred
= NULL
;
2072 acceptsock
->connect_time
= current_time
;
2073 fd_copy_completion( acceptsock
->fd
, newfd
);
2074 release_object( acceptsock
->fd
);
2075 acceptsock
->fd
= newfd
;
2077 unix_len
= sizeof(unix_addr
);
2078 if (!getsockname( get_unix_fd( newfd
), &unix_addr
.addr
, &unix_len
))
2079 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
2082 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
2083 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
2084 sock_reselect( sock
);
2091 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
2093 static const int enable
= 1;
2096 if (!(index
= if_nametoindex( name
)))
2099 if (setsockopt( fd
, IPPROTO_IP
, IP_BOUND_IF
, &index
, sizeof(index
) ))
2102 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
2105 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER) && defined(SO_BINDTODEVICE)
2107 struct interface_filter
2109 struct sock_filter iface_memaddr
;
2110 struct sock_filter iface_rule
;
2111 struct sock_filter ip_memaddr
;
2112 struct sock_filter ip_rule
;
2113 struct sock_filter return_keep
;
2114 struct sock_filter return_dump
;
2116 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
2117 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
2118 /sizeof(struct sock_filter)
2119 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
2120 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
2121 /sizeof(struct sock_filter)
2122 # define FILTER_JUMP_NEXT() (u_char)(0)
2123 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
2124 static struct interface_filter generic_interface_filter
=
2126 /* This filter rule allows incoming packets on the specified interface, which works for all
2127 * remotely generated packets and for locally generated broadcast packets. */
2128 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
2129 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
2130 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
2131 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
2132 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
2133 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
2134 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
2135 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
2138 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
2140 struct interface_filter specific_interface_filter
;
2141 struct sock_fprog filter_prog
;
2142 static const int enable
= 1;
2146 if (!setsockopt( fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen( name
) + 1 ))
2149 /* SO_BINDTODEVICE requires NET_CAP_RAW until Linux 5.7. */
2151 fprintf( stderr
, "setsockopt SO_BINDTODEVICE fd %d, name %s failed: %s, falling back to SO_REUSE_ADDR\n",
2152 fd
, name
, strerror( errno
));
2154 if (!(index
= if_nametoindex( name
)))
2157 ifindex
= htonl( index
);
2158 if (setsockopt( fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
) ) < 0)
2161 specific_interface_filter
= generic_interface_filter
;
2162 specific_interface_filter
.iface_rule
.k
= index
;
2163 specific_interface_filter
.ip_rule
.k
= htonl( bind_addr
);
2164 filter_prog
.len
= sizeof(generic_interface_filter
) / sizeof(struct sock_filter
);
2165 filter_prog
.filter
= (struct sock_filter
*)&specific_interface_filter
;
2166 if (setsockopt( fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
) ))
2169 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
2174 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
2180 #endif /* LINUX_BOUND_IF */
2182 /* Take bind() calls on any name corresponding to a local network adapter and
2183 * restrict the given socket to operating only on the specified interface. This
2184 * restriction consists of two components:
2185 * 1) An outgoing packet restriction suggesting the egress interface for all
2187 * 2) An incoming packet restriction dropping packets not meant for the
2189 * If the function succeeds in placing these restrictions, then the name for the
2190 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
2191 * receipt of broadcast packets on the socket. This behavior is only relevant to
2192 * UDP sockets and is needed for applications that expect to be able to receive
2193 * broadcast packets on a socket that is bound to a specific network interface.
2195 static int bind_to_interface( struct sock
*sock
, const struct sockaddr_in
*addr
)
2197 in_addr_t bind_addr
= addr
->sin_addr
.s_addr
;
2198 struct ifaddrs
*ifaddrs
, *ifaddr
;
2199 int fd
= get_unix_fd( sock
->fd
);
2202 if (bind_addr
== htonl( INADDR_ANY
) || bind_addr
== htonl( INADDR_LOOPBACK
))
2204 if (sock
->type
!= WS_SOCK_DGRAM
)
2207 if (getifaddrs( &ifaddrs
) < 0) return 0;
2209 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
2211 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET
2212 && ((struct sockaddr_in
*)ifaddr
->ifa_addr
)->sin_addr
.s_addr
== bind_addr
)
2214 if ((err
= bind_to_iface_name( fd
, bind_addr
, ifaddr
->ifa_name
)) < 0)
2217 fprintf( stderr
, "failed to bind to interface: %s\n", strerror( errno
) );
2222 freeifaddrs( ifaddrs
);
2226 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2227 static unsigned int get_ipv6_interface_index( const struct in6_addr
*addr
)
2229 struct ifaddrs
*ifaddrs
, *ifaddr
;
2231 if (getifaddrs( &ifaddrs
) < 0) return 0;
2233 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
2235 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET6
2236 && !memcmp( &((struct sockaddr_in6
*)ifaddr
->ifa_addr
)->sin6_addr
, addr
, sizeof(*addr
) ))
2238 unsigned int index
= if_nametoindex( ifaddr
->ifa_name
);
2243 fprintf( stderr
, "Unable to look up interface index for %s: %s\n",
2244 ifaddr
->ifa_name
, strerror( errno
) );
2248 freeifaddrs( ifaddrs
);
2253 freeifaddrs( ifaddrs
);
2258 /* return an errno value mapped to a WSA error */
2259 static unsigned int sock_get_error( int err
)
2263 case EINTR
: return WSAEINTR
;
2264 case EBADF
: return WSAEBADF
;
2266 case EACCES
: return WSAEACCES
;
2267 case EFAULT
: return WSAEFAULT
;
2268 case EINVAL
: return WSAEINVAL
;
2269 case EMFILE
: return WSAEMFILE
;
2271 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
2272 case EALREADY
: return WSAEALREADY
;
2273 case ENOTSOCK
: return WSAENOTSOCK
;
2274 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
2275 case EMSGSIZE
: return WSAEMSGSIZE
;
2276 case EPROTOTYPE
: return WSAEPROTOTYPE
;
2277 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
2278 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
2279 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
2280 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
2281 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
2282 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
2283 case EADDRINUSE
: return WSAEADDRINUSE
;
2284 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
2285 case ENETDOWN
: return WSAENETDOWN
;
2286 case ENETUNREACH
: return WSAENETUNREACH
;
2287 case ENETRESET
: return WSAENETRESET
;
2288 case ECONNABORTED
: return WSAECONNABORTED
;
2290 case ECONNRESET
: return WSAECONNRESET
;
2291 case ENOBUFS
: return WSAENOBUFS
;
2292 case EISCONN
: return WSAEISCONN
;
2293 case ENOTCONN
: return WSAENOTCONN
;
2294 case ESHUTDOWN
: return WSAESHUTDOWN
;
2295 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
2296 case ETIMEDOUT
: return WSAETIMEDOUT
;
2297 case ECONNREFUSED
: return WSAECONNREFUSED
;
2298 case ELOOP
: return WSAELOOP
;
2299 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
2300 case EHOSTDOWN
: return WSAEHOSTDOWN
;
2301 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
2302 case ENOTEMPTY
: return WSAENOTEMPTY
;
2304 case EPROCLIM
: return WSAEPROCLIM
;
2307 case EUSERS
: return WSAEUSERS
;
2310 case EDQUOT
: return WSAEDQUOT
;
2313 case ESTALE
: return WSAESTALE
;
2316 case EREMOTE
: return WSAEREMOTE
;
2322 perror("wineserver: sock_get_error() can't map error");
2327 static int sock_get_ntstatus( int err
)
2331 case EBADF
: return STATUS_INVALID_HANDLE
;
2332 case EBUSY
: return STATUS_DEVICE_BUSY
;
2334 case EACCES
: return STATUS_ACCESS_DENIED
;
2335 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
2336 case EINVAL
: return STATUS_INVALID_PARAMETER
;
2338 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
2340 case EWOULDBLOCK
: return STATUS_DEVICE_NOT_READY
;
2341 case EALREADY
: return STATUS_NETWORK_BUSY
;
2342 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
2343 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
2344 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
2345 case EPROTONOSUPPORT
:
2346 case ESOCKTNOSUPPORT
:
2349 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
2350 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
2351 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
2352 case EADDRINUSE
: return STATUS_SHARING_VIOLATION
;
2353 /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
2354 * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
2356 case EADDRNOTAVAIL
: return STATUS_INVALID_ADDRESS_COMPONENT
;
2357 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
2358 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
2359 case ENOTCONN
: return STATUS_INVALID_CONNECTION
;
2360 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
2361 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
2362 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
2363 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
2365 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
2366 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
2367 case EISCONN
: return STATUS_CONNECTION_ACTIVE
;
2369 case 0: return STATUS_SUCCESS
;
2372 perror("wineserver: sock_get_ntstatus() can't map error");
2373 return STATUS_UNSUCCESSFUL
;
2377 static struct accept_req
*alloc_accept_req( struct sock
*sock
, struct sock
*acceptsock
, struct async
*async
,
2378 const struct afd_accept_into_params
*params
)
2380 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
2384 req
->async
= (struct async
*)grab_object( async
);
2385 req
->iosb
= async_get_iosb( async
);
2386 req
->sock
= (struct sock
*)grab_object( sock
);
2387 req
->acceptsock
= acceptsock
;
2388 if (acceptsock
) grab_object( acceptsock
);
2394 req
->recv_len
= params
->recv_len
;
2395 req
->local_len
= params
->local_len
;
2401 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2403 struct sock
*sock
= get_fd_user( fd
);
2406 assert( sock
->obj
.ops
== &sock_ops
);
2408 if (code
!= IOCTL_AFD_WINE_CREATE
&& code
!= IOCTL_AFD_POLL
&& (unix_fd
= get_unix_fd( fd
)) < 0)
2413 case IOCTL_AFD_WINE_CREATE
:
2415 const struct afd_create_params
*params
= get_req_data();
2417 if (get_req_data_size() != sizeof(*params
))
2419 set_error( STATUS_INVALID_PARAMETER
);
2422 init_socket( sock
, params
->family
, params
->type
, params
->protocol
);
2426 case IOCTL_AFD_WINE_ACCEPT
:
2428 struct sock
*acceptsock
;
2429 obj_handle_t handle
;
2431 if (get_reply_max_size() != sizeof(handle
))
2433 set_error( STATUS_BUFFER_TOO_SMALL
);
2437 if (!(acceptsock
= accept_socket( sock
)))
2439 struct accept_req
*req
;
2441 if (sock
->nonblocking
) return;
2442 if (get_error() != STATUS_DEVICE_NOT_READY
) return;
2444 if (!(req
= alloc_accept_req( sock
, NULL
, async
, NULL
))) return;
2445 list_add_tail( &sock
->accept_list
, &req
->entry
);
2447 async_set_completion_callback( async
, free_accept_req
, req
);
2448 queue_async( &sock
->accept_q
, async
);
2449 sock_reselect( sock
);
2450 set_error( STATUS_PENDING
);
2453 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
2454 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
2455 acceptsock
->wparam
= handle
;
2456 sock_reselect( acceptsock
);
2457 release_object( acceptsock
);
2458 set_reply_data( &handle
, sizeof(handle
) );
2462 case IOCTL_AFD_WINE_ACCEPT_INTO
:
2464 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
2465 const struct afd_accept_into_params
*params
= get_req_data();
2466 struct sock
*acceptsock
;
2467 unsigned int remote_len
;
2468 struct accept_req
*req
;
2470 if (get_req_data_size() != sizeof(*params
) ||
2471 get_reply_max_size() < params
->recv_len
||
2472 get_reply_max_size() - params
->recv_len
< params
->local_len
)
2474 set_error( STATUS_BUFFER_TOO_SMALL
);
2478 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
2479 if (remote_len
< sizeof(int))
2481 set_error( STATUS_INVALID_PARAMETER
);
2485 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
2488 if (acceptsock
->accept_recv_req
)
2490 release_object( acceptsock
);
2491 set_error( STATUS_INVALID_PARAMETER
);
2495 if (!(req
= alloc_accept_req( sock
, acceptsock
, async
, params
)))
2497 release_object( acceptsock
);
2500 list_add_tail( &sock
->accept_list
, &req
->entry
);
2501 acceptsock
->accept_recv_req
= req
;
2502 release_object( acceptsock
);
2504 acceptsock
->wparam
= params
->accept_handle
;
2505 async_set_completion_callback( async
, free_accept_req
, req
);
2506 queue_async( &sock
->accept_q
, async
);
2507 sock_reselect( sock
);
2508 set_error( STATUS_PENDING
);
2512 case IOCTL_AFD_LISTEN
:
2514 const struct afd_listen_params
*params
= get_req_data();
2516 if (get_req_data_size() < sizeof(*params
))
2518 set_error( STATUS_INVALID_PARAMETER
);
2524 set_error( STATUS_INVALID_PARAMETER
);
2528 if (listen( unix_fd
, params
->backlog
) < 0)
2530 set_error( sock_get_ntstatus( errno
) );
2534 sock
->state
= SOCK_LISTENING
;
2536 /* a listening socket can no longer be accepted into */
2537 allow_fd_caching( sock
->fd
);
2539 /* we may already be selecting for AFD_POLL_ACCEPT */
2540 sock_reselect( sock
);
2544 case IOCTL_AFD_WINE_CONNECT
:
2546 const struct afd_connect_params
*params
= get_req_data();
2547 const struct WS_sockaddr
*addr
;
2548 union unix_sockaddr unix_addr
;
2549 struct connect_req
*req
;
2553 if (get_req_data_size() < sizeof(*params
) ||
2554 get_req_data_size() - sizeof(*params
) < params
->addr_len
)
2556 set_error( STATUS_BUFFER_TOO_SMALL
);
2559 send_len
= get_req_data_size() - sizeof(*params
) - params
->addr_len
;
2560 addr
= (const struct WS_sockaddr
*)(params
+ 1);
2562 if (!params
->synchronous
&& !sock
->bound
)
2564 set_error( STATUS_INVALID_PARAMETER
);
2568 if (sock
->accept_recv_req
)
2570 set_error( STATUS_INVALID_PARAMETER
);
2574 if (sock
->connect_req
)
2576 set_error( STATUS_INVALID_PARAMETER
);
2580 switch (sock
->state
)
2582 case SOCK_LISTENING
:
2583 set_error( STATUS_INVALID_PARAMETER
);
2586 case SOCK_CONNECTING
:
2587 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2588 * but there's no status code that maps to WSAEALREADY... */
2589 set_error( params
->synchronous
? STATUS_ADDRESS_ALREADY_ASSOCIATED
: STATUS_INVALID_PARAMETER
);
2592 case SOCK_CONNECTED
:
2593 set_error( STATUS_CONNECTION_ACTIVE
);
2596 case SOCK_UNCONNECTED
:
2597 case SOCK_CONNECTIONLESS
:
2601 unix_len
= sockaddr_to_unix( addr
, params
->addr_len
, &unix_addr
);
2604 set_error( STATUS_INVALID_ADDRESS
);
2607 if (unix_addr
.addr
.sa_family
== AF_INET
&& !memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 ))
2608 unix_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_LOOPBACK
);
2610 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2611 if (ret
< 0 && errno
== ECONNABORTED
)
2613 /* On Linux with nonblocking socket if the previous connect() failed for any reason (including
2614 * timeout), next connect will fail. If the error code was queried by getsockopt( SO_ERROR )
2615 * the error code returned now is ECONNABORTED (otherwise that is the actual connect() failure
2616 * error code). If we got here after previous connect attempt on the socket that means
2617 * we already queried SO_ERROR in sock_error(), so retrying on ECONNABORTED only is
2619 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2622 if (ret
< 0 && errno
!= EINPROGRESS
)
2624 set_error( sock_get_ntstatus( errno
) );
2628 /* a connected or connecting socket can no longer be accepted into */
2629 allow_fd_caching( sock
->fd
);
2631 unix_len
= sizeof(unix_addr
);
2632 if (!getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
))
2633 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2638 if (sock
->type
!= WS_SOCK_DGRAM
)
2640 sock
->state
= SOCK_CONNECTED
;
2641 sock
->connect_time
= current_time
;
2644 if (!send_len
) return;
2647 if (sock
->type
!= WS_SOCK_DGRAM
)
2648 sock
->state
= SOCK_CONNECTING
;
2650 if (params
->synchronous
&& sock
->nonblocking
)
2652 sock_reselect( sock
);
2653 set_error( STATUS_DEVICE_NOT_READY
);
2657 if (!(req
= mem_alloc( sizeof(*req
) )))
2660 req
->async
= (struct async
*)grab_object( async
);
2661 req
->iosb
= async_get_iosb( async
);
2662 req
->sock
= (struct sock
*)grab_object( sock
);
2663 req
->addr_len
= params
->addr_len
;
2664 req
->send_len
= send_len
;
2665 req
->send_cursor
= 0;
2667 async_set_completion_callback( async
, free_connect_req
, req
);
2668 sock
->connect_req
= req
;
2669 queue_async( &sock
->connect_q
, async
);
2670 sock_reselect( sock
);
2671 set_error( STATUS_PENDING
);
2675 case IOCTL_AFD_WINE_SHUTDOWN
:
2679 if (get_req_data_size() < sizeof(int))
2681 set_error( STATUS_BUFFER_TOO_SMALL
);
2684 how
= *(int *)get_req_data();
2688 set_error( STATUS_INVALID_PARAMETER
);
2692 if (sock
->state
!= SOCK_CONNECTED
&& sock
->state
!= SOCK_CONNECTIONLESS
)
2694 set_error( STATUS_INVALID_CONNECTION
);
2700 sock
->rd_shutdown
= 1;
2702 if (how
!= SD_RECEIVE
)
2704 sock
->wr_shutdown
= 1;
2705 if (list_empty( &sock
->write_q
.queue
))
2706 shutdown( unix_fd
, SHUT_WR
);
2708 sock
->wr_shutdown_pending
= 1;
2713 if (sock
->event
) release_object( sock
->event
);
2717 sock
->nonblocking
= 1;
2720 sock_reselect( sock
);
2724 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE
:
2728 if (get_req_data_size() < sizeof(int))
2730 set_error( STATUS_BUFFER_TOO_SMALL
);
2733 force_async
= *(int *)get_req_data();
2735 if (sock
->nonblocking
&& !force_async
)
2737 set_error( STATUS_DEVICE_NOT_READY
);
2740 if (!sock_get_ifchange( sock
)) return;
2741 queue_async( &sock
->ifchange_q
, async
);
2742 set_error( STATUS_PENDING
);
2746 case IOCTL_AFD_WINE_FIONBIO
:
2747 if (get_req_data_size() < sizeof(int))
2749 set_error( STATUS_BUFFER_TOO_SMALL
);
2752 if (*(int *)get_req_data())
2754 sock
->nonblocking
= 1;
2760 set_error( STATUS_INVALID_PARAMETER
);
2763 sock
->nonblocking
= 0;
2767 case IOCTL_AFD_EVENT_SELECT
:
2769 struct event
*event
= NULL
;
2770 obj_handle_t event_handle
;
2773 set_async_pending( async
);
2775 if (is_machine_64bit( current
->process
->machine
))
2777 const struct afd_event_select_params_64
*params
= get_req_data();
2779 if (get_req_data_size() < sizeof(*params
))
2781 set_error( STATUS_INVALID_PARAMETER
);
2785 event_handle
= params
->event
;
2786 mask
= params
->mask
;
2790 const struct afd_event_select_params_32
*params
= get_req_data();
2792 if (get_req_data_size() < sizeof(*params
))
2794 set_error( STATUS_INVALID_PARAMETER
);
2798 event_handle
= params
->event
;
2799 mask
= params
->mask
;
2802 if ((event_handle
|| mask
) &&
2803 !(event
= get_event_obj( current
->process
, event_handle
, EVENT_MODIFY_STATE
)))
2805 set_error( STATUS_INVALID_PARAMETER
);
2809 if (sock
->event
) release_object( sock
->event
);
2810 sock
->event
= event
;
2815 sock
->nonblocking
= 1;
2817 sock_reselect( sock
);
2818 /* Explicitly wake the socket up if the mask causes it to become
2819 * signaled. Note that reselecting isn't enough, since we might already
2820 * have had events recorded in sock->reported_events and we don't want
2821 * to select for them again. */
2822 sock_wake_up( sock
);
2827 case IOCTL_AFD_WINE_MESSAGE_SELECT
:
2829 const struct afd_message_select_params
*params
= get_req_data();
2831 if (get_req_data_size() < sizeof(params
))
2833 set_error( STATUS_BUFFER_TOO_SMALL
);
2837 if (sock
->event
) release_object( sock
->event
);
2841 sock
->pending_events
= 0;
2842 sock
->reported_events
= 0;
2845 sock
->mask
= params
->mask
;
2846 sock
->window
= params
->window
;
2847 sock
->message
= params
->message
;
2848 sock
->wparam
= params
->handle
;
2849 sock
->nonblocking
= 1;
2851 sock_reselect( sock
);
2856 case IOCTL_AFD_BIND
:
2858 const struct afd_bind_params
*params
= get_req_data();
2859 union unix_sockaddr unix_addr
, bind_addr
;
2860 data_size_t in_size
;
2864 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2866 if (get_req_data_size() < get_reply_max_size())
2868 set_error( STATUS_BUFFER_TOO_SMALL
);
2871 in_size
= get_req_data_size() - get_reply_max_size();
2872 if (in_size
< offsetof(struct afd_bind_params
, addr
.sa_data
)
2873 || get_reply_max_size() < in_size
- sizeof(int))
2875 set_error( STATUS_INVALID_PARAMETER
);
2881 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED
);
2885 unix_len
= sockaddr_to_unix( ¶ms
->addr
, in_size
- sizeof(int), &unix_addr
);
2888 set_error( STATUS_INVALID_ADDRESS
);
2891 bind_addr
= unix_addr
;
2893 if (unix_addr
.addr
.sa_family
== AF_INET
)
2895 if (!memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 )
2896 || bind_to_interface( sock
, &unix_addr
.in
))
2897 bind_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
2899 else if (unix_addr
.addr
.sa_family
== AF_INET6
)
2901 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2902 /* Windows allows specifying zero to use the default scope. Linux
2903 * interprets it as an interface index and requires that it be
2905 if (!unix_addr
.in6
.sin6_scope_id
)
2906 bind_addr
.in6
.sin6_scope_id
= get_ipv6_interface_index( &unix_addr
.in6
.sin6_addr
);
2910 set_async_pending( async
);
2913 if (sock
->family
== WS_AF_INET6
)
2915 socklen_t len
= sizeof(v6only
);
2917 getsockopt( get_unix_fd(sock
->fd
), IPPROTO_IPV6
, IPV6_V6ONLY
, &v6only
, &len
);
2921 if (check_addr_usage( sock
, &bind_addr
, v6only
))
2924 if (bind( unix_fd
, &bind_addr
.addr
, unix_len
) < 0)
2926 if (errno
== EADDRINUSE
&& sock
->reuseaddr
)
2929 set_error( sock_get_ntstatus( errno
) );
2935 unix_len
= sizeof(bind_addr
);
2936 if (!getsockname( unix_fd
, &bind_addr
.addr
, &unix_len
))
2938 /* store the interface or magic loopback address instead of the
2939 * actual unix address */
2940 if (bind_addr
.addr
.sa_family
== AF_INET
)
2941 bind_addr
.in
.sin_addr
= unix_addr
.in
.sin_addr
;
2942 sock
->addr_len
= sockaddr_from_unix( &bind_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2945 update_addr_usage( sock
, &bind_addr
, v6only
);
2947 if (get_reply_max_size() >= sock
->addr_len
)
2948 set_reply_data( &sock
->addr
, sock
->addr_len
);
2952 case IOCTL_AFD_GETSOCKNAME
:
2955 set_error( STATUS_INVALID_PARAMETER
);
2959 if (get_reply_max_size() < sock
->addr_len
)
2961 set_error( STATUS_BUFFER_TOO_SMALL
);
2965 set_reply_data( &sock
->addr
, sock
->addr_len
);
2968 case IOCTL_AFD_WINE_DEFER
:
2970 const obj_handle_t
*handle
= get_req_data();
2971 struct sock
*acceptsock
;
2973 if (get_req_data_size() < sizeof(*handle
))
2975 set_error( STATUS_BUFFER_TOO_SMALL
);
2979 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, *handle
, 0, &sock_ops
);
2980 if (!acceptsock
) return;
2982 sock
->deferred
= acceptsock
;
2986 case IOCTL_AFD_WINE_GET_INFO
:
2988 struct afd_get_info_params params
;
2990 if (get_reply_max_size() < sizeof(params
))
2992 set_error( STATUS_BUFFER_TOO_SMALL
);
2996 params
.family
= sock
->family
;
2997 params
.type
= sock
->type
;
2998 params
.protocol
= sock
->proto
;
2999 set_reply_data( ¶ms
, sizeof(params
) );
3003 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN
:
3005 int listening
= (sock
->state
== SOCK_LISTENING
);
3007 if (get_reply_max_size() < sizeof(listening
))
3009 set_error( STATUS_BUFFER_TOO_SMALL
);
3013 set_reply_data( &listening
, sizeof(listening
) );
3017 case IOCTL_AFD_WINE_GET_SO_ERROR
:
3022 if (get_reply_max_size() < sizeof(error
))
3024 set_error( STATUS_BUFFER_TOO_SMALL
);
3028 error
= sock_error( sock
);
3031 for (i
= 0; i
< ARRAY_SIZE( sock
->errors
); ++i
)
3033 if (sock
->errors
[i
])
3035 error
= sock
->errors
[i
];
3041 error
= sock_get_error( error
);
3042 set_reply_data( &error
, sizeof(error
) );
3046 case IOCTL_AFD_WINE_GET_SO_RCVBUF
:
3048 int rcvbuf
= sock
->rcvbuf
;
3050 if (get_reply_max_size() < sizeof(rcvbuf
))
3052 set_error( STATUS_BUFFER_TOO_SMALL
);
3056 set_reply_data( &rcvbuf
, sizeof(rcvbuf
) );
3060 case IOCTL_AFD_WINE_SET_SO_RCVBUF
:
3064 if (get_req_data_size() < sizeof(rcvbuf
))
3066 set_error( STATUS_BUFFER_TOO_SMALL
);
3069 rcvbuf
= *(DWORD
*)get_req_data();
3071 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_RCVBUF
, (char *)&rcvbuf
, sizeof(rcvbuf
) ))
3072 sock
->rcvbuf
= rcvbuf
;
3074 set_error( sock_get_ntstatus( errno
) );
3078 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO
:
3080 DWORD rcvtimeo
= sock
->rcvtimeo
;
3082 if (get_reply_max_size() < sizeof(rcvtimeo
))
3084 set_error( STATUS_BUFFER_TOO_SMALL
);
3088 set_reply_data( &rcvtimeo
, sizeof(rcvtimeo
) );
3092 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO
:
3096 if (get_req_data_size() < sizeof(rcvtimeo
))
3098 set_error( STATUS_BUFFER_TOO_SMALL
);
3101 rcvtimeo
= *(DWORD
*)get_req_data();
3103 sock
->rcvtimeo
= rcvtimeo
;
3107 /* BSD socket SO_REUSEADDR is not compatible with winsock semantics. */
3108 case IOCTL_AFD_WINE_SET_SO_REUSEADDR
:
3112 if (get_req_data_size() < sizeof(reuse
))
3114 set_error( STATUS_BUFFER_TOO_SMALL
);
3118 reuse
= *(int *)get_req_data();
3120 if (reuse
&& sock
->exclusiveaddruse
)
3122 set_error( STATUS_INVALID_PARAMETER
);
3126 if (is_tcp_socket( sock
))
3129 ret
= setsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuse
, sizeof(reuse
) );
3131 if (!ret
) ret
= setsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEPORT
, &reuse
, sizeof(reuse
) );
3134 set_error( sock_get_ntstatus( errno
) );
3136 sock
->reuseaddr
= !!reuse
;
3140 case IOCTL_AFD_WINE_SET_SO_EXCLUSIVEADDRUSE
:
3144 if (get_req_data_size() < sizeof(exclusive
))
3146 set_error( STATUS_BUFFER_TOO_SMALL
);
3150 exclusive
= *(int *)get_req_data();
3151 if (exclusive
&& sock
->reuseaddr
)
3153 set_error( STATUS_INVALID_PARAMETER
);
3156 sock
->exclusiveaddruse
= !!exclusive
;
3160 case IOCTL_AFD_WINE_GET_SO_SNDBUF
:
3162 int sndbuf
= sock
->sndbuf
;
3164 if (get_reply_max_size() < sizeof(sndbuf
))
3166 set_error( STATUS_BUFFER_TOO_SMALL
);
3170 set_reply_data( &sndbuf
, sizeof(sndbuf
) );
3174 case IOCTL_AFD_WINE_SET_SO_SNDBUF
:
3178 if (get_req_data_size() < sizeof(sndbuf
))
3180 set_error( STATUS_BUFFER_TOO_SMALL
);
3183 sndbuf
= *(DWORD
*)get_req_data();
3188 /* setsockopt fails if a zero value is passed */
3189 sock
->sndbuf
= sndbuf
;
3194 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_SNDBUF
, (char *)&sndbuf
, sizeof(sndbuf
) ))
3195 sock
->sndbuf
= sndbuf
;
3197 set_error( sock_get_ntstatus( errno
) );
3201 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO
:
3203 DWORD sndtimeo
= sock
->sndtimeo
;
3205 if (get_reply_max_size() < sizeof(sndtimeo
))
3207 set_error( STATUS_BUFFER_TOO_SMALL
);
3211 set_reply_data( &sndtimeo
, sizeof(sndtimeo
) );
3215 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO
:
3219 if (get_req_data_size() < sizeof(sndtimeo
))
3221 set_error( STATUS_BUFFER_TOO_SMALL
);
3224 sndtimeo
= *(DWORD
*)get_req_data();
3226 sock
->sndtimeo
= sndtimeo
;
3230 case IOCTL_AFD_WINE_GET_SO_CONNECT_TIME
:
3234 if (get_reply_max_size() < sizeof(time
))
3236 set_error( STATUS_BUFFER_TOO_SMALL
);
3240 if (sock
->state
== SOCK_CONNECTED
)
3241 time
= (current_time
- sock
->connect_time
) / 10000000;
3243 set_reply_data( &time
, sizeof(time
) );
3247 case IOCTL_AFD_WINE_GET_SO_REUSEADDR
:
3251 if (!get_reply_max_size())
3253 set_error( STATUS_BUFFER_TOO_SMALL
);
3257 reuse
= sock
->reuseaddr
;
3258 set_reply_data( &reuse
, min( sizeof(reuse
), get_reply_max_size() ));
3262 case IOCTL_AFD_WINE_GET_SO_EXCLUSIVEADDRUSE
:
3266 if (!get_reply_max_size())
3268 set_error( STATUS_BUFFER_TOO_SMALL
);
3272 exclusive
= sock
->exclusiveaddruse
;
3273 set_reply_data( &exclusive
, min( sizeof(exclusive
), get_reply_max_size() ));
3277 case IOCTL_AFD_POLL
:
3279 if (get_reply_max_size() < get_req_data_size())
3281 set_error( STATUS_INVALID_PARAMETER
);
3285 if (is_machine_64bit( current
->process
->machine
))
3287 const struct afd_poll_params_64
*params
= get_req_data();
3289 if (get_req_data_size() < sizeof(struct afd_poll_params_64
) ||
3290 get_req_data_size() < offsetof( struct afd_poll_params_64
, sockets
[params
->count
] ))
3292 set_error( STATUS_INVALID_PARAMETER
);
3296 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, params
->sockets
);
3300 const struct afd_poll_params_32
*params
= get_req_data();
3301 struct afd_poll_socket_64
*sockets
;
3304 if (get_req_data_size() < sizeof(struct afd_poll_params_32
) ||
3305 get_req_data_size() < offsetof( struct afd_poll_params_32
, sockets
[params
->count
] ))
3307 set_error( STATUS_INVALID_PARAMETER
);
3311 if (!(sockets
= mem_alloc( params
->count
* sizeof(*sockets
) ))) return;
3312 for (i
= 0; i
< params
->count
; ++i
)
3314 sockets
[i
].socket
= params
->sockets
[i
].socket
;
3315 sockets
[i
].flags
= params
->sockets
[i
].flags
;
3316 sockets
[i
].status
= params
->sockets
[i
].status
;
3319 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, sockets
);
3327 set_error( STATUS_NOT_SUPPORTED
);
3332 static void handle_exclusive_poll(struct poll_req
*req
)
3336 for (i
= 0; i
< req
->count
; ++i
)
3338 struct sock
*sock
= req
->sockets
[i
].sock
;
3339 struct poll_req
*main_poll
= sock
->main_poll
;
3341 if (main_poll
&& main_poll
->exclusive
&& req
->exclusive
)
3343 complete_async_poll( main_poll
, STATUS_SUCCESS
);
3348 sock
->main_poll
= req
;
3352 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
3353 unsigned int count
, const struct afd_poll_socket_64
*sockets
)
3355 BOOL signaled
= FALSE
;
3356 struct poll_req
*req
;
3361 set_error( STATUS_INVALID_PARAMETER
);
3365 if (!(req
= mem_alloc( offsetof( struct poll_req
, sockets
[count
] ) )))
3368 req
->timeout
= NULL
;
3370 if (timeout
&& timeout
!= TIMEOUT_INFINITE
&&
3371 !(req
->timeout
= add_timeout_user( timeout
, async_poll_timeout
, req
)))
3376 req
->orig_timeout
= timeout
;
3378 for (i
= 0; i
< count
; ++i
)
3380 req
->sockets
[i
].sock
= (struct sock
*)get_handle_obj( current
->process
, sockets
[i
].socket
, 0, &sock_ops
);
3381 if (!req
->sockets
[i
].sock
)
3383 for (j
= 0; j
< i
; ++j
) release_object( req
->sockets
[j
].sock
);
3384 if (req
->timeout
) remove_timeout_user( req
->timeout
);
3388 req
->sockets
[i
].handle
= sockets
[i
].socket
;
3389 req
->sockets
[i
].mask
= sockets
[i
].flags
;
3390 req
->sockets
[i
].flags
= 0;
3393 req
->exclusive
= exclusive
;
3395 req
->async
= (struct async
*)grab_object( async
);
3396 req
->iosb
= async_get_iosb( async
);
3398 handle_exclusive_poll(req
);
3400 list_add_tail( &poll_list
, &req
->entry
);
3401 async_set_completion_callback( async
, free_poll_req
, req
);
3402 queue_async( &poll_sock
->poll_q
, async
);
3404 for (i
= 0; i
< count
; ++i
)
3406 struct sock
*sock
= req
->sockets
[i
].sock
;
3407 int mask
= req
->sockets
[i
].mask
;
3408 struct pollfd pollfd
;
3410 pollfd
.fd
= get_unix_fd( sock
->fd
);
3411 pollfd
.events
= poll_flags_from_afd( sock
, mask
);
3412 if (pollfd
.events
>= 0 && poll( &pollfd
, 1, 0 ) >= 0)
3413 sock_poll_event( sock
->fd
, pollfd
.revents
);
3415 /* FIXME: do other error conditions deserve a similar treatment? */
3416 if (sock
->state
!= SOCK_CONNECTING
&& sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] && (mask
& AFD_POLL_CONNECT_ERR
))
3418 req
->sockets
[i
].flags
|= AFD_POLL_CONNECT_ERR
;
3419 req
->sockets
[i
].status
= sock_get_ntstatus( sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] );
3422 if (req
->sockets
[i
].flags
)
3426 if (!timeout
|| signaled
)
3427 complete_async_poll( req
, STATUS_SUCCESS
);
3431 for (i
= 0; i
< req
->count
; ++i
)
3432 sock_reselect( req
->sockets
[i
].sock
);
3433 set_error( STATUS_PENDING
);
3436 #ifdef HAVE_LINUX_RTNETLINK_H
3438 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
3439 static struct object
*ifchange_object
;
3441 static void ifchange_dump( struct object
*obj
, int verbose
);
3442 static struct fd
*ifchange_get_fd( struct object
*obj
);
3443 static void ifchange_destroy( struct object
*obj
);
3445 static int ifchange_get_poll_events( struct fd
*fd
);
3446 static void ifchange_poll_event( struct fd
*fd
, int event
);
3450 struct object obj
; /* object header */
3451 struct fd
*fd
; /* interface change file descriptor */
3452 struct list sockets
; /* list of sockets to send interface change notifications */
3455 static const struct object_ops ifchange_ops
=
3457 sizeof(struct ifchange
), /* size */
3458 &no_type
, /* type */
3459 ifchange_dump
, /* dump */
3460 no_add_queue
, /* add_queue */
3461 NULL
, /* remove_queue */
3462 NULL
, /* signaled */
3463 no_satisfied
, /* satisfied */
3464 no_signal
, /* signal */
3465 ifchange_get_fd
, /* get_fd */
3466 default_map_access
, /* map_access */
3467 default_get_sd
, /* get_sd */
3468 default_set_sd
, /* set_sd */
3469 no_get_full_name
, /* get_full_name */
3470 no_lookup_name
, /* lookup_name */
3471 no_link_name
, /* link_name */
3472 NULL
, /* unlink_name */
3473 no_open_file
, /* open_file */
3474 no_kernel_obj_list
, /* get_kernel_obj_list */
3475 no_close_handle
, /* close_handle */
3476 ifchange_destroy
/* destroy */
3479 static const struct fd_ops ifchange_fd_ops
=
3481 ifchange_get_poll_events
, /* get_poll_events */
3482 ifchange_poll_event
, /* poll_event */
3483 NULL
, /* get_fd_type */
3484 no_fd_read
, /* read */
3485 no_fd_write
, /* write */
3486 no_fd_flush
, /* flush */
3487 no_fd_get_file_info
, /* get_file_info */
3488 no_fd_get_volume_info
, /* get_volume_info */
3489 no_fd_ioctl
, /* ioctl */
3490 NULL
, /* cancel_async */
3491 NULL
, /* queue_async */
3492 NULL
/* reselect_async */
3495 static void ifchange_dump( struct object
*obj
, int verbose
)
3497 assert( obj
->ops
== &ifchange_ops
);
3498 fprintf( stderr
, "Interface change\n" );
3501 static struct fd
*ifchange_get_fd( struct object
*obj
)
3503 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3504 return (struct fd
*)grab_object( ifchange
->fd
);
3507 static void ifchange_destroy( struct object
*obj
)
3509 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3510 assert( obj
->ops
== &ifchange_ops
);
3512 release_object( ifchange
->fd
);
3514 /* reset the global ifchange object so that it will be recreated if it is needed again */
3515 assert( obj
== ifchange_object
);
3516 ifchange_object
= NULL
;
3519 static int ifchange_get_poll_events( struct fd
*fd
)
3524 /* wake up all the sockets waiting for a change notification event */
3525 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
3527 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3528 struct list
*ptr
, *next
;
3529 assert( obj
->ops
== &ifchange_ops
);
3530 assert( obj
== ifchange_object
);
3532 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
3534 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
3536 assert( sock
->ifchange_obj
);
3537 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
3538 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
3542 static void ifchange_poll_event( struct fd
*fd
, int event
)
3544 struct object
*ifchange
= get_fd_user( fd
);
3545 unsigned int status
= STATUS_PENDING
;
3546 char buffer
[PIPE_BUF
];
3549 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
3552 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
3553 return; /* retry when poll() says the socket is ready */
3554 status
= sock_get_ntstatus( errno
);
3558 struct nlmsghdr
*nlh
;
3560 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
3562 if (nlh
->nlmsg_type
== NLMSG_DONE
)
3564 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
3565 status
= STATUS_SUCCESS
;
3568 else status
= STATUS_CANCELLED
;
3570 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
3575 /* we only need one of these interface notification objects, all of the sockets dependent upon
3576 * it will wake up when a notification event occurs */
3577 static struct object
*get_ifchange( void )
3579 #ifdef HAVE_LINUX_RTNETLINK_H
3580 struct ifchange
*ifchange
;
3581 struct sockaddr_nl addr
;
3584 if (ifchange_object
)
3586 /* increment the refcount for each socket that uses the ifchange object */
3587 return grab_object( ifchange_object
);
3590 /* create the socket we need for processing interface change notifications */
3591 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
3594 set_error( sock_get_ntstatus( errno
));
3597 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
3598 memset( &addr
, 0, sizeof(addr
) );
3599 addr
.nl_family
= AF_NETLINK
;
3600 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
3601 /* bind the socket to the special netlink kernel interface */
3602 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
3605 set_error( sock_get_ntstatus( errno
));
3608 if (!(ifchange
= alloc_object( &ifchange_ops
)))
3611 set_error( STATUS_NO_MEMORY
);
3614 list_init( &ifchange
->sockets
);
3615 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
3617 release_object( ifchange
);
3618 set_error( STATUS_NO_MEMORY
);
3621 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
3623 /* the ifchange object is now successfully configured */
3624 ifchange_object
= &ifchange
->obj
;
3625 return &ifchange
->obj
;
3627 set_error( STATUS_NOT_SUPPORTED
);
3632 /* add the socket to the interface change notification list */
3633 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
3635 #ifdef HAVE_LINUX_RTNETLINK_H
3636 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3638 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
3642 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3643 static struct object
*sock_get_ifchange( struct sock
*sock
)
3645 struct object
*ifchange
;
3647 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
3648 return sock
->ifchange_obj
;
3650 if (!(ifchange
= get_ifchange()))
3653 /* add the socket to the ifchange notification list */
3654 ifchange_add_sock( ifchange
, sock
);
3655 sock
->ifchange_obj
= ifchange
;
3659 /* destroy an existing ifchange queue for a specific socket */
3660 static void sock_release_ifchange( struct sock
*sock
)
3662 if (sock
->ifchange_obj
)
3664 list_remove( &sock
->ifchange_entry
);
3665 release_object( sock
->ifchange_obj
);
3666 sock
->ifchange_obj
= NULL
;
3670 static void socket_device_dump( struct object
*obj
, int verbose
);
3671 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3672 unsigned int attr
, struct object
*root
);
3673 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3674 unsigned int sharing
, unsigned int options
);
3676 static const struct object_ops socket_device_ops
=
3678 sizeof(struct object
), /* size */
3679 &device_type
, /* type */
3680 socket_device_dump
, /* dump */
3681 no_add_queue
, /* add_queue */
3682 NULL
, /* remove_queue */
3683 NULL
, /* signaled */
3684 no_satisfied
, /* satisfied */
3685 no_signal
, /* signal */
3686 no_get_fd
, /* get_fd */
3687 default_map_access
, /* map_access */
3688 default_get_sd
, /* get_sd */
3689 default_set_sd
, /* set_sd */
3690 default_get_full_name
, /* get_full_name */
3691 socket_device_lookup_name
, /* lookup_name */
3692 directory_link_name
, /* link_name */
3693 default_unlink_name
, /* unlink_name */
3694 socket_device_open_file
, /* open_file */
3695 no_kernel_obj_list
, /* get_kernel_obj_list */
3696 no_close_handle
, /* close_handle */
3697 no_destroy
/* destroy */
3700 static void socket_device_dump( struct object
*obj
, int verbose
)
3702 fputs( "Socket device\n", stderr
);
3705 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3706 unsigned int attr
, struct object
*root
)
3708 if (name
) name
->len
= 0;
3712 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3713 unsigned int sharing
, unsigned int options
)
3717 if (!(sock
= create_socket())) return NULL
;
3718 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
3720 release_object( sock
);
3726 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
3727 unsigned int attr
, const struct security_descriptor
*sd
)
3729 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
3732 DECL_HANDLER(recv_socket
)
3734 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3735 unsigned int status
= STATUS_PENDING
;
3736 timeout_t timeout
= 0;
3737 struct async
*async
;
3743 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3744 timeout
= (timeout_t
)sock
->rcvtimeo
* -10000;
3746 if (sock
->rd_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3747 else if (!async_queued( &sock
->read_q
))
3749 /* If read_q is not empty, we cannot really tell if the already queued
3750 * asyncs will not consume all available data; if there's no data
3751 * available, the current request won't be immediately satiable.
3753 if ((!req
->force_async
&& sock
->nonblocking
) ||
3754 check_fd_events( sock
->fd
, req
->oob
&& !is_oobinline( sock
) ? POLLPRI
: POLLIN
))
3756 /* Give the client opportunity to complete synchronously.
3757 * If it turns out that the I/O request is not actually immediately satiable,
3758 * the client may then choose to re-queue the async (with STATUS_PENDING).
3760 * Note: If the nonblocking flag is set, we don't poll the socket
3761 * here and always opt for synchronous completion first. This is
3762 * because the application has probably seen POLLIN already from a
3763 * preceding select()/poll() call before it requested to receive
3766 status
= STATUS_ALERTED
;
3770 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3771 status
= STATUS_DEVICE_NOT_READY
;
3773 sock
->pending_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3774 sock
->reported_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3776 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3778 set_error( status
);
3781 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3783 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3784 queue_async( &sock
->read_q
, async
);
3786 /* always reselect; we changed reported_events above */
3787 sock_reselect( sock
);
3789 reply
->wait
= async_handoff( async
, NULL
, 0 );
3790 reply
->options
= get_fd_options( fd
);
3791 reply
->nonblocking
= sock
->nonblocking
;
3792 release_object( async
);
3794 release_object( sock
);
3797 static void send_socket_completion_callback( void *private )
3799 struct send_req
*send_req
= private;
3800 struct iosb
*iosb
= send_req
->iosb
;
3801 struct sock
*sock
= send_req
->sock
;
3803 if (iosb
->status
!= STATUS_SUCCESS
)
3805 /* send() calls only clear and reselect events if unsuccessful. */
3806 sock
->pending_events
&= ~AFD_POLL_WRITE
;
3807 sock
->reported_events
&= ~AFD_POLL_WRITE
;
3808 sock_reselect( sock
);
3811 release_object( iosb
);
3812 release_object( sock
);
3816 DECL_HANDLER(send_socket
)
3818 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3819 unsigned int status
= STATUS_PENDING
;
3820 timeout_t timeout
= 0;
3821 struct async
*async
;
3828 if (sock
->type
== WS_SOCK_DGRAM
&& !sock
->bound
)
3830 union unix_sockaddr unix_addr
;
3832 int unix_fd
= get_unix_fd( fd
);
3834 unix_len
= get_unix_sockaddr_any( &unix_addr
, sock
->family
);
3835 if (bind( unix_fd
, &unix_addr
.addr
, unix_len
) < 0)
3838 if (getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
) >= 0)
3840 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
3843 else if (!bind_errno
) bind_errno
= errno
;
3846 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3847 timeout
= (timeout_t
)sock
->sndtimeo
* -10000;
3849 if (bind_errno
) status
= sock_get_ntstatus( bind_errno
);
3850 else if (sock
->wr_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3851 else if (!async_queued( &sock
->write_q
))
3853 /* If write_q is not empty, we cannot really tell if the already queued
3854 * asyncs will not consume all available space; if there's no space
3855 * available, the current request won't be immediately satiable.
3857 if ((!req
->force_async
&& sock
->nonblocking
) || check_fd_events( sock
->fd
, POLLOUT
))
3859 /* Give the client opportunity to complete synchronously.
3860 * If it turns out that the I/O request is not actually immediately satiable,
3861 * the client may then choose to re-queue the async (with STATUS_PENDING).
3863 * Note: If the nonblocking flag is set, we don't poll the socket
3864 * here and always opt for synchronous completion first. This is
3865 * because the application has probably seen POLLOUT already from a
3866 * preceding select()/poll() call before it requested to send data.
3868 * Furthermore, some applications expect that any send() call on a
3869 * socket that has indicated POLLOUT beforehand never fails with
3870 * WSAEWOULDBLOCK. It's possible that Linux poll() may yield
3871 * POLLOUT on the first call but not the second, even if no send()
3872 * call has been made in the meanwhile. This can happen for a
3873 * number of reasons; for example, TCP fragmentation may consume
3874 * extra buffer space for each packet that has been split out, or
3875 * the TCP/IP networking stack may decide to shrink the send buffer
3876 * due to memory pressure.
3878 status
= STATUS_ALERTED
;
3882 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3883 status
= STATUS_DEVICE_NOT_READY
;
3885 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3887 struct send_req
*send_req
;
3888 struct iosb
*iosb
= async_get_iosb( async
);
3890 if ((send_req
= mem_alloc( sizeof(*send_req
) )))
3892 send_req
->iosb
= (struct iosb
*)grab_object( iosb
);
3893 send_req
->sock
= (struct sock
*)grab_object( sock
);
3894 async_set_completion_callback( async
, send_socket_completion_callback
, send_req
);
3896 else if (status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
)
3897 status
= STATUS_NO_MEMORY
;
3899 release_object( iosb
);
3901 set_error( status
);
3904 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3906 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3908 queue_async( &sock
->write_q
, async
);
3909 sock_reselect( sock
);
3912 reply
->wait
= async_handoff( async
, NULL
, 0 );
3913 reply
->options
= get_fd_options( fd
);
3914 reply
->nonblocking
= sock
->nonblocking
;
3915 release_object( async
);
3917 release_object( sock
);
3920 DECL_HANDLER(socket_get_events
)
3922 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, 0, &sock_ops
);
3923 unsigned int status
[13];
3924 struct event
*event
= NULL
;
3927 if (get_reply_max_size() < sizeof(status
))
3929 set_error( STATUS_INVALID_PARAMETER
);
3937 if (!(event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
)))
3939 release_object( sock
);
3944 reply
->flags
= sock
->pending_events
& sock
->mask
;
3945 for (i
= 0; i
< ARRAY_SIZE( status
); ++i
)
3946 status
[i
] = sock_get_ntstatus( sock
->errors
[i
] );
3948 sock
->pending_events
&= ~sock
->mask
;
3949 sock_reselect( sock
);
3953 reset_event( event
);
3954 release_object( event
);
3957 set_reply_data( status
, sizeof(status
) );
3959 release_object( sock
);
3962 DECL_HANDLER(socket_send_icmp_id
)
3964 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, 0, &sock_ops
);
3968 if (sock
->icmp_fixup_data_len
== MAX_ICMP_HISTORY_LENGTH
)
3970 memmove( sock
->icmp_fixup_data
, sock
->icmp_fixup_data
+ 1,
3971 sizeof(*sock
->icmp_fixup_data
) * (MAX_ICMP_HISTORY_LENGTH
- 1) );
3972 --sock
->icmp_fixup_data_len
;
3975 sock
->icmp_fixup_data
[sock
->icmp_fixup_data_len
].icmp_id
= req
->icmp_id
;
3976 sock
->icmp_fixup_data
[sock
->icmp_fixup_data_len
].icmp_seq
= req
->icmp_seq
;
3977 ++sock
->icmp_fixup_data_len
;
3979 release_object( sock
);
3982 DECL_HANDLER(socket_get_icmp_id
)
3984 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, 0, &sock_ops
);
3989 for (i
= 0; i
< sock
->icmp_fixup_data_len
; ++i
)
3991 if (sock
->icmp_fixup_data
[i
].icmp_seq
== req
->icmp_seq
)
3993 reply
->icmp_id
= sock
->icmp_fixup_data
[i
].icmp_id
;
3994 --sock
->icmp_fixup_data_len
;
3995 memmove( &sock
->icmp_fixup_data
[i
], &sock
->icmp_fixup_data
[i
+ 1],
3996 (sock
->icmp_fixup_data_len
- i
) * sizeof(*sock
->icmp_fixup_data
) );
3997 release_object( sock
);
4002 set_error( STATUS_NOT_FOUND
);
4003 release_object( sock
);