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
)
1297 post_socket_event( sock
, AFD_POLL_BIT_CONNECT
);
1298 post_socket_event( sock
, AFD_POLL_BIT_WRITE
);
1300 if (event
& (POLLERR
| POLLHUP
))
1301 post_socket_event( sock
, AFD_POLL_BIT_CONNECT_ERR
);
1304 case SOCK_LISTENING
:
1305 if (event
& (POLLIN
| POLLERR
| POLLHUP
))
1306 post_socket_event( sock
, AFD_POLL_BIT_ACCEPT
);
1309 case SOCK_CONNECTED
:
1310 case SOCK_CONNECTIONLESS
:
1312 post_socket_event( sock
, AFD_POLL_BIT_RESET
);
1315 post_socket_event( sock
, AFD_POLL_BIT_READ
);
1317 if (event
& POLLOUT
)
1318 post_socket_event( sock
, AFD_POLL_BIT_WRITE
);
1320 if (event
& POLLPRI
)
1321 post_socket_event( sock
, AFD_POLL_BIT_OOB
);
1323 if (event
& (POLLERR
| POLLHUP
))
1324 post_socket_event( sock
, AFD_POLL_BIT_HUP
);
1328 sock_wake_up( sock
);
1331 static void sock_poll_event( struct fd
*fd
, int event
)
1333 struct sock
*sock
= get_fd_user( fd
);
1334 int hangup_seen
= 0;
1335 enum connection_state prevstate
= sock
->state
;
1338 assert( sock
->obj
.ops
== &sock_ops
);
1340 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
1342 if (event
& (POLLERR
| POLLHUP
))
1343 error
= sock_error( sock
);
1345 switch (sock
->state
)
1347 case SOCK_UNCONNECTED
:
1350 case SOCK_CONNECTING
:
1351 if (event
& (POLLERR
|POLLHUP
))
1353 sock
->state
= SOCK_UNCONNECTED
;
1356 else if (event
& POLLOUT
)
1358 sock
->state
= SOCK_CONNECTED
;
1359 sock
->connect_time
= current_time
;
1360 sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] = 0;
1364 case SOCK_LISTENING
:
1367 case SOCK_CONNECTED
:
1368 case SOCK_CONNECTIONLESS
:
1370 event
&= ~(POLLIN
| POLLERR
| POLLHUP
);
1372 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
1377 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
1378 * has been closed, so we need to check for it explicitly here */
1379 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
1388 /* EAGAIN can happen if an async recv() falls between the server's poll()
1389 call and the invocation of this routine */
1390 if (errno
== ECONNRESET
|| errno
== EPIPE
)
1394 else if (errno
!= EAGAIN
)
1398 sock
->errors
[AFD_POLL_BIT_HUP
] = error
;
1400 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
1405 if (hangup_seen
|| (sock_shutdown_type
== SOCK_SHUTDOWN_POLLHUP
&& (event
& POLLHUP
)))
1409 else if (event
& (POLLHUP
| POLLERR
))
1414 fprintf( stderr
, "socket %p aborted by error %d, event %#x\n", sock
, error
, event
);
1422 event
= sock_dispatch_asyncs( sock
, event
, error
);
1423 sock_dispatch_events( sock
, prevstate
, event
);
1424 complete_async_polls( sock
, event
, error
);
1426 sock_reselect( sock
);
1429 static void sock_dump( struct object
*obj
, int verbose
)
1431 struct sock
*sock
= (struct sock
*)obj
;
1432 assert( obj
->ops
== &sock_ops
);
1433 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
1434 sock
->fd
, sock
->state
,
1435 sock
->mask
, sock
->pending_events
, sock
->reported_events
);
1438 static int poll_flags_from_afd( struct sock
*sock
, int flags
)
1442 /* A connection-mode socket which has never been connected does
1443 * not return write or hangup events, but Linux returns
1444 * POLLOUT | POLLHUP. */
1445 if (sock
->state
== SOCK_UNCONNECTED
)
1448 if (flags
& (AFD_POLL_READ
| AFD_POLL_ACCEPT
))
1450 if ((flags
& AFD_POLL_HUP
) && sock
->type
== WS_SOCK_STREAM
)
1452 if (flags
& AFD_POLL_OOB
)
1453 ev
|= is_oobinline( sock
) ? POLLIN
: POLLPRI
;
1454 if (flags
& AFD_POLL_WRITE
)
1460 static int sock_get_poll_events( struct fd
*fd
)
1462 struct sock
*sock
= get_fd_user( fd
);
1463 unsigned int mask
= sock
->mask
& ~sock
->reported_events
;
1464 struct poll_req
*req
;
1467 assert( sock
->obj
.ops
== &sock_ops
);
1469 if (!sock
->type
) /* not initialized yet */
1472 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1476 if (req
->iosb
->status
!= STATUS_PENDING
) continue;
1478 for (i
= 0; i
< req
->count
; ++i
)
1480 if (req
->sockets
[i
].sock
!= sock
) continue;
1482 ev
|= poll_flags_from_afd( sock
, req
->sockets
[i
].mask
);
1486 switch (sock
->state
)
1488 case SOCK_UNCONNECTED
:
1489 /* A connection-mode Windows socket which has never been connected does
1490 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1491 * need to return -1 here, to prevent the socket from being polled on at
1495 case SOCK_CONNECTING
:
1498 case SOCK_LISTENING
:
1499 if (!list_empty( &sock
->accept_list
) || (mask
& AFD_POLL_ACCEPT
))
1503 case SOCK_CONNECTED
:
1504 case SOCK_CONNECTIONLESS
:
1505 if (sock
->hangup
&& sock
->wr_shutdown
&& !sock
->wr_shutdown_pending
)
1507 /* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
1508 * if both the socket and its peer are SHUT_WR.
1510 * We don't use SHUT_RD, so we can only encounter this in the latter
1511 * case. In that case there can't be any pending read requests (they
1512 * would have already been completed with a length of zero), the
1513 * above condition ensures that we don't have any pending write
1514 * requests, and nothing that can change about the socket state that
1515 * would complete a pending poll request. */
1519 if (sock
->aborted
|| sock
->reset
)
1522 if (sock
->accept_recv_req
)
1526 else if (async_queued( &sock
->read_q
))
1528 /* Clear POLLIN and POLLPRI if we have an alerted async, even if
1529 * we're polling this socket for READ or OOB. We can't signal the
1530 * poll if the pending async will read all of the data [cf. the
1531 * matching logic in sock_dispatch_asyncs()], but we also don't
1532 * want to spin polling for POLLIN if we're not going to use it. */
1533 if (async_waiting( &sock
->read_q
))
1534 ev
|= POLLIN
| POLLPRI
;
1536 ev
&= ~(POLLIN
| POLLPRI
);
1540 /* Don't ask for POLLIN if we got a hangup. We won't receive more
1541 * data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
1544 if (mask
& AFD_POLL_READ
)
1546 if (mask
& AFD_POLL_OOB
)
1550 /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
1551 if (sock
->state
== SOCK_CONNECTED
&& (mask
& AFD_POLL_HUP
) && !(sock
->reported_events
& AFD_POLL_READ
))
1555 if (async_queued( &sock
->write_q
))
1557 /* As with read asyncs above, clear POLLOUT if we have an alerted
1559 if (async_waiting( &sock
->write_q
))
1564 else if (!sock
->wr_shutdown
&& (mask
& AFD_POLL_WRITE
))
1575 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
1577 return FD_TYPE_SOCKET
;
1580 static void sock_cancel_async( struct fd
*fd
, struct async
*async
)
1582 struct poll_req
*req
;
1584 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1588 if (req
->async
!= async
)
1591 for (i
= 0; i
< req
->count
; i
++)
1593 struct sock
*sock
= req
->sockets
[i
].sock
;
1595 if (sock
->main_poll
== req
)
1596 sock
->main_poll
= NULL
;
1600 async_terminate( async
, STATUS_CANCELLED
);
1603 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
1605 struct sock
*sock
= get_fd_user( fd
);
1607 if (sock
->wr_shutdown_pending
&& list_empty( &sock
->write_q
.queue
))
1609 shutdown( get_unix_fd( sock
->fd
), SHUT_WR
);
1610 sock
->wr_shutdown_pending
= 0;
1613 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1614 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1616 if (queue
!= &sock
->ifchange_q
&& sock
->type
)
1617 sock_reselect( sock
);
1620 static struct fd
*sock_get_fd( struct object
*obj
)
1622 struct sock
*sock
= (struct sock
*)obj
;
1623 return (struct fd
*)grab_object( sock
->fd
);
1626 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
1628 struct sock
*sock
= (struct sock
*)obj
;
1630 if (sock
->obj
.handle_count
== 1) /* last handle */
1632 struct accept_req
*accept_req
, *accept_next
;
1633 struct poll_req
*poll_req
, *poll_next
;
1635 if (sock
->accept_recv_req
)
1636 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
1638 LIST_FOR_EACH_ENTRY_SAFE( accept_req
, accept_next
, &sock
->accept_list
, struct accept_req
, entry
)
1639 async_terminate( accept_req
->async
, STATUS_CANCELLED
);
1641 if (sock
->connect_req
)
1642 async_terminate( sock
->connect_req
->async
, STATUS_CANCELLED
);
1644 LIST_FOR_EACH_ENTRY_SAFE( poll_req
, poll_next
, &poll_list
, struct poll_req
, entry
)
1646 struct iosb
*iosb
= poll_req
->iosb
;
1647 BOOL signaled
= FALSE
;
1650 if (iosb
->status
!= STATUS_PENDING
) continue;
1652 for (i
= 0; i
< poll_req
->count
; ++i
)
1654 if (poll_req
->sockets
[i
].sock
== sock
)
1657 poll_req
->sockets
[i
].flags
= AFD_POLL_CLOSE
;
1658 poll_req
->sockets
[i
].status
= 0;
1662 if (signaled
) complete_async_poll( poll_req
, STATUS_SUCCESS
);
1665 return async_close_obj_handle( obj
, process
, handle
);
1668 static void sock_destroy( struct object
*obj
)
1670 struct sock
*sock
= (struct sock
*)obj
;
1673 assert( obj
->ops
== &sock_ops
);
1675 /* FIXME: special socket shutdown stuff? */
1677 for (i
= 0; i
< 2; ++i
)
1679 if (sock
->bound_addr
[i
] && --sock
->bound_addr
[i
]->reuse_count
<= 0)
1681 rb_remove( &bound_addresses_tree
, &sock
->bound_addr
[i
]->entry
);
1682 free( sock
->bound_addr
[i
] );
1686 if ( sock
->deferred
)
1687 release_object( sock
->deferred
);
1689 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
1690 sock_release_ifchange( sock
);
1691 free_async_queue( &sock
->read_q
);
1692 free_async_queue( &sock
->write_q
);
1693 free_async_queue( &sock
->ifchange_q
);
1694 free_async_queue( &sock
->accept_q
);
1695 free_async_queue( &sock
->connect_q
);
1696 free_async_queue( &sock
->poll_q
);
1697 if (sock
->event
) release_object( sock
->event
);
1698 if (sock
->fd
) release_object( sock
->fd
);
1701 static struct sock
*create_socket(void)
1705 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
1707 sock
->state
= SOCK_UNCONNECTED
;
1709 sock
->pending_events
= 0;
1710 sock
->reported_events
= 0;
1718 sock
->connect_time
= 0;
1719 sock
->deferred
= NULL
;
1720 sock
->ifchange_obj
= NULL
;
1721 sock
->accept_recv_req
= NULL
;
1722 sock
->connect_req
= NULL
;
1723 sock
->main_poll
= NULL
;
1724 memset( &sock
->addr
, 0, sizeof(sock
->addr
) );
1726 sock
->rd_shutdown
= 0;
1727 sock
->wr_shutdown
= 0;
1728 sock
->wr_shutdown_pending
= 0;
1731 sock
->nonblocking
= 0;
1734 sock
->reuseaddr
= 0;
1735 sock
->exclusiveaddruse
= 0;
1740 sock
->icmp_fixup_data_len
= 0;
1741 sock
->bound_addr
[0] = sock
->bound_addr
[1] = NULL
;
1742 init_async_queue( &sock
->read_q
);
1743 init_async_queue( &sock
->write_q
);
1744 init_async_queue( &sock
->ifchange_q
);
1745 init_async_queue( &sock
->accept_q
);
1746 init_async_queue( &sock
->connect_q
);
1747 init_async_queue( &sock
->poll_q
);
1748 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
1749 list_init( &sock
->accept_list
);
1753 static int get_unix_family( int family
)
1757 case WS_AF_INET
: return AF_INET
;
1758 case WS_AF_INET6
: return AF_INET6
;
1760 case WS_AF_IPX
: return AF_IPX
;
1763 case WS_AF_IRDA
: return AF_IRDA
;
1765 case WS_AF_UNSPEC
: return AF_UNSPEC
;
1770 static int get_unix_type( int type
)
1774 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
1775 case WS_SOCK_RAW
: return SOCK_RAW
;
1776 case WS_SOCK_STREAM
: return SOCK_STREAM
;
1781 static int get_unix_protocol( int protocol
)
1783 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1788 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
1789 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1790 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1791 case WS_IPPROTO_IPV4
: return IPPROTO_IPIP
;
1792 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1793 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1794 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1795 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1800 static void set_dont_fragment( int fd
, int level
, int value
)
1804 if (level
== IPPROTO_IP
)
1807 optname
= IP_DONTFRAG
;
1808 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1809 optname
= IP_MTU_DISCOVER
;
1810 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1817 #ifdef IPV6_DONTFRAG
1818 optname
= IPV6_DONTFRAG
;
1819 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1820 optname
= IPV6_MTU_DISCOVER
;
1821 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1827 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1830 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
)
1832 unsigned int options
= 0;
1833 int sockfd
, unix_type
, unix_family
, unix_protocol
, value
;
1836 unix_family
= get_unix_family( family
);
1837 unix_type
= get_unix_type( type
);
1838 unix_protocol
= get_unix_protocol( protocol
);
1840 if (unix_protocol
< 0)
1842 if (type
&& unix_type
< 0)
1843 set_win32_error( WSAESOCKTNOSUPPORT
);
1845 set_win32_error( WSAEPROTONOSUPPORT
);
1848 if (unix_family
< 0)
1850 if (family
>= 0 && unix_type
< 0)
1851 set_win32_error( WSAESOCKTNOSUPPORT
);
1853 set_win32_error( WSAEAFNOSUPPORT
);
1857 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1860 if (sockfd
== -1 && errno
== EPERM
&& unix_family
== AF_INET
1861 && unix_type
== SOCK_RAW
&& unix_protocol
== IPPROTO_ICMP
)
1863 sockfd
= socket( unix_family
, SOCK_DGRAM
, unix_protocol
);
1868 setsockopt( sockfd
, IPPROTO_IP
, IP_RECVTTL
, (const char *)&val
, sizeof(val
) );
1869 setsockopt( sockfd
, IPPROTO_IP
, IP_RECVTOS
, (const char *)&val
, sizeof(val
) );
1870 setsockopt( sockfd
, IPPROTO_IP
, IP_PKTINFO
, (const char *)&val
, sizeof(val
) );
1877 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1878 else set_win32_error( sock_get_error( errno
));
1881 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1883 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1886 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1889 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1892 /* Should we retrieve val using a getsockopt call and then
1893 * set the modified one? */
1894 val
.ipx_pt
= ipx_type
;
1895 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1900 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1902 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1903 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1904 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1905 else if (unix_type
== SOCK_STREAM
)
1906 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1910 if (unix_family
== AF_INET6
)
1912 static const int enable
= 1;
1913 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1917 len
= sizeof(value
);
1918 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_RCVBUF
, &value
, &len
))
1919 sock
->rcvbuf
= value
;
1921 len
= sizeof(value
);
1922 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_SNDBUF
, &value
, &len
))
1923 sock
->sndbuf
= value
;
1925 sock
->state
= (type
== WS_SOCK_STREAM
? SOCK_UNCONNECTED
: SOCK_CONNECTIONLESS
);
1926 sock
->proto
= protocol
;
1928 sock
->family
= family
;
1930 if (is_tcp_socket( sock
))
1933 setsockopt( sockfd
, SOL_SOCKET
, SO_REUSEADDR
, &value
, sizeof(value
) );
1936 setsockopt( sockfd
, IPPROTO_TCP
, TCP_SYNCNT
, &value
, sizeof(value
) );
1942 options
= get_fd_options( sock
->fd
);
1943 release_object( sock
->fd
);
1946 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1951 /* We can't immediately allow caching for a connection-mode socket, since it
1952 * might be accepted into (changing the underlying fd object.) */
1953 if (sock
->type
!= WS_SOCK_STREAM
) allow_fd_caching( sock
->fd
);
1958 /* accepts a socket and inits it */
1959 static int accept_new_fd( struct sock
*sock
)
1962 /* Try to accept(2). We can't be safe that this an already connected socket
1963 * or that accept() is allowed on it. In those cases we will get -1/errno
1966 struct sockaddr saddr
;
1967 socklen_t slen
= sizeof(saddr
);
1968 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1970 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1972 set_error( sock_get_ntstatus( errno
));
1976 /* accept a socket (creates a new fd) */
1977 static struct sock
*accept_socket( struct sock
*sock
)
1979 struct sock
*acceptsock
;
1982 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1984 if ( sock
->deferred
)
1986 acceptsock
= sock
->deferred
;
1987 sock
->deferred
= NULL
;
1991 union unix_sockaddr unix_addr
;
1994 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1995 if (!(acceptsock
= create_socket()))
2001 /* newly created socket gets the same properties of the listening socket */
2002 acceptsock
->state
= SOCK_CONNECTED
;
2003 acceptsock
->bound
= 1;
2004 acceptsock
->nonblocking
= sock
->nonblocking
;
2005 acceptsock
->mask
= sock
->mask
;
2006 acceptsock
->proto
= sock
->proto
;
2007 acceptsock
->type
= sock
->type
;
2008 acceptsock
->family
= sock
->family
;
2009 acceptsock
->window
= sock
->window
;
2010 acceptsock
->message
= sock
->message
;
2011 acceptsock
->reuseaddr
= sock
->reuseaddr
;
2012 acceptsock
->exclusiveaddruse
= sock
->exclusiveaddruse
;
2013 acceptsock
->sndbuf
= sock
->sndbuf
;
2014 acceptsock
->rcvbuf
= sock
->rcvbuf
;
2015 acceptsock
->sndtimeo
= sock
->sndtimeo
;
2016 acceptsock
->rcvtimeo
= sock
->rcvtimeo
;
2017 acceptsock
->connect_time
= current_time
;
2019 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
2020 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
2021 get_fd_options( sock
->fd
) )))
2023 release_object( acceptsock
);
2026 unix_len
= sizeof(unix_addr
);
2027 if (!getsockname( acceptfd
, &unix_addr
.addr
, &unix_len
))
2028 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
2031 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
2032 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
2033 sock_reselect( sock
);
2037 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
2039 union unix_sockaddr unix_addr
;
2044 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
2046 if ( sock
->deferred
)
2048 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
2049 get_fd_options( acceptsock
->fd
) );
2053 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
2055 release_object( sock
->deferred
);
2056 sock
->deferred
= NULL
;
2060 if ((acceptfd
= accept_new_fd( sock
)) == -1)
2063 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
2064 get_fd_options( acceptsock
->fd
) )))
2068 acceptsock
->state
= SOCK_CONNECTED
;
2069 acceptsock
->bound
= 1;
2070 acceptsock
->pending_events
= 0;
2071 acceptsock
->reported_events
= 0;
2072 acceptsock
->proto
= sock
->proto
;
2073 acceptsock
->type
= sock
->type
;
2074 acceptsock
->family
= sock
->family
;
2075 acceptsock
->wparam
= 0;
2076 acceptsock
->deferred
= NULL
;
2077 acceptsock
->connect_time
= current_time
;
2078 fd_copy_completion( acceptsock
->fd
, newfd
);
2079 release_object( acceptsock
->fd
);
2080 acceptsock
->fd
= newfd
;
2082 unix_len
= sizeof(unix_addr
);
2083 if (!getsockname( get_unix_fd( newfd
), &unix_addr
.addr
, &unix_len
))
2084 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
2087 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
2088 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
2089 sock_reselect( sock
);
2096 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
2098 static const int enable
= 1;
2101 if (!(index
= if_nametoindex( name
)))
2104 if (setsockopt( fd
, IPPROTO_IP
, IP_BOUND_IF
, &index
, sizeof(index
) ))
2107 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
2110 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER) && defined(SO_BINDTODEVICE)
2112 struct interface_filter
2114 struct sock_filter iface_memaddr
;
2115 struct sock_filter iface_rule
;
2116 struct sock_filter ip_memaddr
;
2117 struct sock_filter ip_rule
;
2118 struct sock_filter return_keep
;
2119 struct sock_filter return_dump
;
2121 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
2122 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
2123 /sizeof(struct sock_filter)
2124 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
2125 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
2126 /sizeof(struct sock_filter)
2127 # define FILTER_JUMP_NEXT() (u_char)(0)
2128 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
2129 static struct interface_filter generic_interface_filter
=
2131 /* This filter rule allows incoming packets on the specified interface, which works for all
2132 * remotely generated packets and for locally generated broadcast packets. */
2133 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
2134 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
2135 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
2136 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
2137 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
2138 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
2139 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
2140 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
2143 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
2145 struct interface_filter specific_interface_filter
;
2146 struct sock_fprog filter_prog
;
2147 static const int enable
= 1;
2151 if (!setsockopt( fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen( name
) + 1 ))
2154 /* SO_BINDTODEVICE requires NET_CAP_RAW until Linux 5.7. */
2156 fprintf( stderr
, "setsockopt SO_BINDTODEVICE fd %d, name %s failed: %s, falling back to SO_REUSE_ADDR\n",
2157 fd
, name
, strerror( errno
));
2159 if (!(index
= if_nametoindex( name
)))
2162 ifindex
= htonl( index
);
2163 if (setsockopt( fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
) ) < 0)
2166 specific_interface_filter
= generic_interface_filter
;
2167 specific_interface_filter
.iface_rule
.k
= index
;
2168 specific_interface_filter
.ip_rule
.k
= htonl( bind_addr
);
2169 filter_prog
.len
= sizeof(generic_interface_filter
) / sizeof(struct sock_filter
);
2170 filter_prog
.filter
= (struct sock_filter
*)&specific_interface_filter
;
2171 if (setsockopt( fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
) ))
2174 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
2179 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
2185 #endif /* LINUX_BOUND_IF */
2187 /* Take bind() calls on any name corresponding to a local network adapter and
2188 * restrict the given socket to operating only on the specified interface. This
2189 * restriction consists of two components:
2190 * 1) An outgoing packet restriction suggesting the egress interface for all
2192 * 2) An incoming packet restriction dropping packets not meant for the
2194 * If the function succeeds in placing these restrictions, then the name for the
2195 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
2196 * receipt of broadcast packets on the socket. This behavior is only relevant to
2197 * UDP sockets and is needed for applications that expect to be able to receive
2198 * broadcast packets on a socket that is bound to a specific network interface.
2200 static int bind_to_interface( struct sock
*sock
, const struct sockaddr_in
*addr
)
2202 in_addr_t bind_addr
= addr
->sin_addr
.s_addr
;
2203 struct ifaddrs
*ifaddrs
, *ifaddr
;
2204 int fd
= get_unix_fd( sock
->fd
);
2207 if (bind_addr
== htonl( INADDR_ANY
) || bind_addr
== htonl( INADDR_LOOPBACK
))
2209 if (sock
->type
!= WS_SOCK_DGRAM
)
2212 if (getifaddrs( &ifaddrs
) < 0) return 0;
2214 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
2216 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET
2217 && ((struct sockaddr_in
*)ifaddr
->ifa_addr
)->sin_addr
.s_addr
== bind_addr
)
2219 if ((err
= bind_to_iface_name( fd
, bind_addr
, ifaddr
->ifa_name
)) < 0)
2222 fprintf( stderr
, "failed to bind to interface: %s\n", strerror( errno
) );
2227 freeifaddrs( ifaddrs
);
2231 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2232 static unsigned int get_ipv6_interface_index( const struct in6_addr
*addr
)
2234 struct ifaddrs
*ifaddrs
, *ifaddr
;
2236 if (getifaddrs( &ifaddrs
) < 0) return 0;
2238 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
2240 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET6
2241 && !memcmp( &((struct sockaddr_in6
*)ifaddr
->ifa_addr
)->sin6_addr
, addr
, sizeof(*addr
) ))
2243 unsigned int index
= if_nametoindex( ifaddr
->ifa_name
);
2248 fprintf( stderr
, "Unable to look up interface index for %s: %s\n",
2249 ifaddr
->ifa_name
, strerror( errno
) );
2253 freeifaddrs( ifaddrs
);
2258 freeifaddrs( ifaddrs
);
2263 /* return an errno value mapped to a WSA error */
2264 static unsigned int sock_get_error( int err
)
2268 case EINTR
: return WSAEINTR
;
2269 case EBADF
: return WSAEBADF
;
2271 case EACCES
: return WSAEACCES
;
2272 case EFAULT
: return WSAEFAULT
;
2273 case EINVAL
: return WSAEINVAL
;
2274 case EMFILE
: return WSAEMFILE
;
2276 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
2277 case EALREADY
: return WSAEALREADY
;
2278 case ENOTSOCK
: return WSAENOTSOCK
;
2279 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
2280 case EMSGSIZE
: return WSAEMSGSIZE
;
2281 case EPROTOTYPE
: return WSAEPROTOTYPE
;
2282 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
2283 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
2284 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
2285 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
2286 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
2287 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
2288 case EADDRINUSE
: return WSAEADDRINUSE
;
2289 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
2290 case ENETDOWN
: return WSAENETDOWN
;
2291 case ENETUNREACH
: return WSAENETUNREACH
;
2292 case ENETRESET
: return WSAENETRESET
;
2293 case ECONNABORTED
: return WSAECONNABORTED
;
2295 case ECONNRESET
: return WSAECONNRESET
;
2296 case ENOBUFS
: return WSAENOBUFS
;
2297 case EISCONN
: return WSAEISCONN
;
2298 case ENOTCONN
: return WSAENOTCONN
;
2299 case ESHUTDOWN
: return WSAESHUTDOWN
;
2300 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
2301 case ETIMEDOUT
: return WSAETIMEDOUT
;
2302 case ECONNREFUSED
: return WSAECONNREFUSED
;
2303 case ELOOP
: return WSAELOOP
;
2304 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
2305 case EHOSTDOWN
: return WSAEHOSTDOWN
;
2306 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
2307 case ENOTEMPTY
: return WSAENOTEMPTY
;
2309 case EPROCLIM
: return WSAEPROCLIM
;
2312 case EUSERS
: return WSAEUSERS
;
2315 case EDQUOT
: return WSAEDQUOT
;
2318 case ESTALE
: return WSAESTALE
;
2321 case EREMOTE
: return WSAEREMOTE
;
2327 perror("wineserver: sock_get_error() can't map error");
2332 static int sock_get_ntstatus( int err
)
2336 case EBADF
: return STATUS_INVALID_HANDLE
;
2337 case EBUSY
: return STATUS_DEVICE_BUSY
;
2339 case EACCES
: return STATUS_ACCESS_DENIED
;
2340 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
2341 case EINVAL
: return STATUS_INVALID_PARAMETER
;
2343 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
2345 case EWOULDBLOCK
: return STATUS_DEVICE_NOT_READY
;
2346 case EALREADY
: return STATUS_NETWORK_BUSY
;
2347 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
2348 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
2349 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
2350 case EPROTONOSUPPORT
:
2351 case ESOCKTNOSUPPORT
:
2354 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
2355 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
2356 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
2357 case EADDRINUSE
: return STATUS_SHARING_VIOLATION
;
2358 /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
2359 * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
2361 case EADDRNOTAVAIL
: return STATUS_INVALID_ADDRESS_COMPONENT
;
2362 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
2363 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
2364 case ENOTCONN
: return STATUS_INVALID_CONNECTION
;
2365 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
2366 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
2367 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
2368 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
2370 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
2371 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
2372 case EISCONN
: return STATUS_CONNECTION_ACTIVE
;
2374 case 0: return STATUS_SUCCESS
;
2377 perror("wineserver: sock_get_ntstatus() can't map error");
2378 return STATUS_UNSUCCESSFUL
;
2382 static struct accept_req
*alloc_accept_req( struct sock
*sock
, struct sock
*acceptsock
, struct async
*async
,
2383 const struct afd_accept_into_params
*params
)
2385 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
2389 req
->async
= (struct async
*)grab_object( async
);
2390 req
->iosb
= async_get_iosb( async
);
2391 req
->sock
= (struct sock
*)grab_object( sock
);
2392 req
->acceptsock
= acceptsock
;
2393 if (acceptsock
) grab_object( acceptsock
);
2399 req
->recv_len
= params
->recv_len
;
2400 req
->local_len
= params
->local_len
;
2406 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2408 struct sock
*sock
= get_fd_user( fd
);
2411 assert( sock
->obj
.ops
== &sock_ops
);
2413 if (code
!= IOCTL_AFD_WINE_CREATE
&& code
!= IOCTL_AFD_POLL
&& (unix_fd
= get_unix_fd( fd
)) < 0)
2418 case IOCTL_AFD_WINE_CREATE
:
2420 const struct afd_create_params
*params
= get_req_data();
2422 if (get_req_data_size() != sizeof(*params
))
2424 set_error( STATUS_INVALID_PARAMETER
);
2427 init_socket( sock
, params
->family
, params
->type
, params
->protocol
);
2431 case IOCTL_AFD_WINE_ACCEPT
:
2433 struct sock
*acceptsock
;
2434 obj_handle_t handle
;
2436 if (get_reply_max_size() != sizeof(handle
))
2438 set_error( STATUS_BUFFER_TOO_SMALL
);
2442 if (!(acceptsock
= accept_socket( sock
)))
2444 struct accept_req
*req
;
2446 if (sock
->nonblocking
) return;
2447 if (get_error() != STATUS_DEVICE_NOT_READY
) return;
2449 if (!(req
= alloc_accept_req( sock
, NULL
, async
, NULL
))) return;
2450 list_add_tail( &sock
->accept_list
, &req
->entry
);
2452 async_set_completion_callback( async
, free_accept_req
, req
);
2453 queue_async( &sock
->accept_q
, async
);
2454 sock_reselect( sock
);
2455 set_error( STATUS_PENDING
);
2458 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
2459 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
2460 acceptsock
->wparam
= handle
;
2461 sock_reselect( acceptsock
);
2462 release_object( acceptsock
);
2463 set_reply_data( &handle
, sizeof(handle
) );
2467 case IOCTL_AFD_WINE_ACCEPT_INTO
:
2469 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
2470 const struct afd_accept_into_params
*params
= get_req_data();
2471 struct sock
*acceptsock
;
2472 unsigned int remote_len
;
2473 struct accept_req
*req
;
2475 if (get_req_data_size() != sizeof(*params
) ||
2476 get_reply_max_size() < params
->recv_len
||
2477 get_reply_max_size() - params
->recv_len
< params
->local_len
)
2479 set_error( STATUS_BUFFER_TOO_SMALL
);
2483 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
2484 if (remote_len
< sizeof(int))
2486 set_error( STATUS_INVALID_PARAMETER
);
2490 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
2493 if (acceptsock
->accept_recv_req
)
2495 release_object( acceptsock
);
2496 set_error( STATUS_INVALID_PARAMETER
);
2500 if (!(req
= alloc_accept_req( sock
, acceptsock
, async
, params
)))
2502 release_object( acceptsock
);
2505 list_add_tail( &sock
->accept_list
, &req
->entry
);
2506 acceptsock
->accept_recv_req
= req
;
2507 release_object( acceptsock
);
2509 acceptsock
->wparam
= params
->accept_handle
;
2510 async_set_completion_callback( async
, free_accept_req
, req
);
2511 queue_async( &sock
->accept_q
, async
);
2512 sock_reselect( sock
);
2513 set_error( STATUS_PENDING
);
2517 case IOCTL_AFD_LISTEN
:
2519 const struct afd_listen_params
*params
= get_req_data();
2521 if (get_req_data_size() < sizeof(*params
))
2523 set_error( STATUS_INVALID_PARAMETER
);
2529 set_error( STATUS_INVALID_PARAMETER
);
2533 if (listen( unix_fd
, params
->backlog
) < 0)
2535 set_error( sock_get_ntstatus( errno
) );
2539 sock
->state
= SOCK_LISTENING
;
2541 /* a listening socket can no longer be accepted into */
2542 allow_fd_caching( sock
->fd
);
2544 /* we may already be selecting for AFD_POLL_ACCEPT */
2545 sock_reselect( sock
);
2549 case IOCTL_AFD_WINE_CONNECT
:
2551 const struct afd_connect_params
*params
= get_req_data();
2552 const struct WS_sockaddr
*addr
;
2553 union unix_sockaddr unix_addr
;
2554 struct connect_req
*req
;
2558 if (get_req_data_size() < sizeof(*params
) ||
2559 get_req_data_size() - sizeof(*params
) < params
->addr_len
)
2561 set_error( STATUS_BUFFER_TOO_SMALL
);
2564 send_len
= get_req_data_size() - sizeof(*params
) - params
->addr_len
;
2565 addr
= (const struct WS_sockaddr
*)(params
+ 1);
2567 if (!params
->synchronous
&& !sock
->bound
)
2569 set_error( STATUS_INVALID_PARAMETER
);
2573 if (sock
->accept_recv_req
)
2575 set_error( STATUS_INVALID_PARAMETER
);
2579 if (sock
->connect_req
)
2581 set_error( STATUS_INVALID_PARAMETER
);
2585 switch (sock
->state
)
2587 case SOCK_LISTENING
:
2588 set_error( STATUS_INVALID_PARAMETER
);
2591 case SOCK_CONNECTING
:
2592 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2593 * but there's no status code that maps to WSAEALREADY... */
2594 set_error( params
->synchronous
? STATUS_ADDRESS_ALREADY_ASSOCIATED
: STATUS_INVALID_PARAMETER
);
2597 case SOCK_CONNECTED
:
2598 set_error( STATUS_CONNECTION_ACTIVE
);
2601 case SOCK_UNCONNECTED
:
2602 case SOCK_CONNECTIONLESS
:
2606 unix_len
= sockaddr_to_unix( addr
, params
->addr_len
, &unix_addr
);
2609 set_error( STATUS_INVALID_ADDRESS
);
2612 if (unix_addr
.addr
.sa_family
== AF_INET
&& !memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 ))
2613 unix_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_LOOPBACK
);
2615 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2616 if (ret
< 0 && errno
== ECONNABORTED
)
2618 /* On Linux with nonblocking socket if the previous connect() failed for any reason (including
2619 * timeout), next connect will fail. If the error code was queried by getsockopt( SO_ERROR )
2620 * the error code returned now is ECONNABORTED (otherwise that is the actual connect() failure
2621 * error code). If we got here after previous connect attempt on the socket that means
2622 * we already queried SO_ERROR in sock_error(), so retrying on ECONNABORTED only is
2624 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2627 if (ret
< 0 && errno
!= EINPROGRESS
)
2629 set_error( sock_get_ntstatus( errno
) );
2633 /* a connected or connecting socket can no longer be accepted into */
2634 allow_fd_caching( sock
->fd
);
2636 unix_len
= sizeof(unix_addr
);
2637 if (!getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
))
2638 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2643 if (sock
->type
!= WS_SOCK_DGRAM
)
2645 sock
->state
= SOCK_CONNECTED
;
2646 sock
->connect_time
= current_time
;
2649 if (!send_len
) return;
2652 if (sock
->type
!= WS_SOCK_DGRAM
)
2653 sock
->state
= SOCK_CONNECTING
;
2655 if (params
->synchronous
&& sock
->nonblocking
)
2657 sock_reselect( sock
);
2658 set_error( STATUS_DEVICE_NOT_READY
);
2662 if (!(req
= mem_alloc( sizeof(*req
) )))
2665 req
->async
= (struct async
*)grab_object( async
);
2666 req
->iosb
= async_get_iosb( async
);
2667 req
->sock
= (struct sock
*)grab_object( sock
);
2668 req
->addr_len
= params
->addr_len
;
2669 req
->send_len
= send_len
;
2670 req
->send_cursor
= 0;
2672 async_set_completion_callback( async
, free_connect_req
, req
);
2673 sock
->connect_req
= req
;
2674 queue_async( &sock
->connect_q
, async
);
2675 sock_reselect( sock
);
2676 set_error( STATUS_PENDING
);
2680 case IOCTL_AFD_WINE_SHUTDOWN
:
2684 if (get_req_data_size() < sizeof(int))
2686 set_error( STATUS_BUFFER_TOO_SMALL
);
2689 how
= *(int *)get_req_data();
2693 set_error( STATUS_INVALID_PARAMETER
);
2697 if (sock
->state
!= SOCK_CONNECTED
&& sock
->state
!= SOCK_CONNECTIONLESS
)
2699 set_error( STATUS_INVALID_CONNECTION
);
2705 sock
->rd_shutdown
= 1;
2707 if (how
!= SD_RECEIVE
)
2709 sock
->wr_shutdown
= 1;
2710 if (list_empty( &sock
->write_q
.queue
))
2711 shutdown( unix_fd
, SHUT_WR
);
2713 sock
->wr_shutdown_pending
= 1;
2718 if (sock
->event
) release_object( sock
->event
);
2722 sock
->nonblocking
= 1;
2725 sock_reselect( sock
);
2729 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE
:
2733 if (get_req_data_size() < sizeof(int))
2735 set_error( STATUS_BUFFER_TOO_SMALL
);
2738 force_async
= *(int *)get_req_data();
2740 if (sock
->nonblocking
&& !force_async
)
2742 set_error( STATUS_DEVICE_NOT_READY
);
2745 if (!sock_get_ifchange( sock
)) return;
2746 queue_async( &sock
->ifchange_q
, async
);
2747 set_error( STATUS_PENDING
);
2751 case IOCTL_AFD_WINE_FIONBIO
:
2752 if (get_req_data_size() < sizeof(int))
2754 set_error( STATUS_BUFFER_TOO_SMALL
);
2757 if (*(int *)get_req_data())
2759 sock
->nonblocking
= 1;
2765 set_error( STATUS_INVALID_PARAMETER
);
2768 sock
->nonblocking
= 0;
2772 case IOCTL_AFD_EVENT_SELECT
:
2774 struct event
*event
= NULL
;
2775 obj_handle_t event_handle
;
2778 set_async_pending( async
);
2780 if (is_machine_64bit( current
->process
->machine
))
2782 const struct afd_event_select_params_64
*params
= get_req_data();
2784 if (get_req_data_size() < sizeof(*params
))
2786 set_error( STATUS_INVALID_PARAMETER
);
2790 event_handle
= params
->event
;
2791 mask
= params
->mask
;
2795 const struct afd_event_select_params_32
*params
= get_req_data();
2797 if (get_req_data_size() < sizeof(*params
))
2799 set_error( STATUS_INVALID_PARAMETER
);
2803 event_handle
= params
->event
;
2804 mask
= params
->mask
;
2807 if ((event_handle
|| mask
) &&
2808 !(event
= get_event_obj( current
->process
, event_handle
, EVENT_MODIFY_STATE
)))
2810 set_error( STATUS_INVALID_PARAMETER
);
2814 if (sock
->event
) release_object( sock
->event
);
2815 sock
->event
= event
;
2820 sock
->nonblocking
= 1;
2822 sock_reselect( sock
);
2823 /* Explicitly wake the socket up if the mask causes it to become
2824 * signaled. Note that reselecting isn't enough, since we might already
2825 * have had events recorded in sock->reported_events and we don't want
2826 * to select for them again. */
2827 sock_wake_up( sock
);
2832 case IOCTL_AFD_WINE_MESSAGE_SELECT
:
2834 const struct afd_message_select_params
*params
= get_req_data();
2836 if (get_req_data_size() < sizeof(params
))
2838 set_error( STATUS_BUFFER_TOO_SMALL
);
2842 if (sock
->event
) release_object( sock
->event
);
2846 sock
->pending_events
= 0;
2847 sock
->reported_events
= 0;
2850 sock
->mask
= params
->mask
;
2851 sock
->window
= params
->window
;
2852 sock
->message
= params
->message
;
2853 sock
->wparam
= params
->handle
;
2854 sock
->nonblocking
= 1;
2856 sock_reselect( sock
);
2861 case IOCTL_AFD_BIND
:
2863 const struct afd_bind_params
*params
= get_req_data();
2864 union unix_sockaddr unix_addr
, bind_addr
;
2865 data_size_t in_size
;
2869 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2871 if (get_req_data_size() < get_reply_max_size())
2873 set_error( STATUS_BUFFER_TOO_SMALL
);
2876 in_size
= get_req_data_size() - get_reply_max_size();
2877 if (in_size
< offsetof(struct afd_bind_params
, addr
.sa_data
)
2878 || get_reply_max_size() < in_size
- sizeof(int))
2880 set_error( STATUS_INVALID_PARAMETER
);
2886 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED
);
2890 unix_len
= sockaddr_to_unix( ¶ms
->addr
, in_size
- sizeof(int), &unix_addr
);
2893 set_error( STATUS_INVALID_ADDRESS
);
2896 bind_addr
= unix_addr
;
2898 if (unix_addr
.addr
.sa_family
== AF_INET
)
2900 if (!memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 )
2901 || bind_to_interface( sock
, &unix_addr
.in
))
2902 bind_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
2904 else if (unix_addr
.addr
.sa_family
== AF_INET6
)
2906 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2907 /* Windows allows specifying zero to use the default scope. Linux
2908 * interprets it as an interface index and requires that it be
2910 if (!unix_addr
.in6
.sin6_scope_id
)
2911 bind_addr
.in6
.sin6_scope_id
= get_ipv6_interface_index( &unix_addr
.in6
.sin6_addr
);
2915 set_async_pending( async
);
2918 if (sock
->family
== WS_AF_INET6
)
2920 socklen_t len
= sizeof(v6only
);
2922 getsockopt( get_unix_fd(sock
->fd
), IPPROTO_IPV6
, IPV6_V6ONLY
, &v6only
, &len
);
2926 if (check_addr_usage( sock
, &bind_addr
, v6only
))
2929 if (bind( unix_fd
, &bind_addr
.addr
, unix_len
) < 0)
2931 if (errno
== EADDRINUSE
&& sock
->reuseaddr
)
2934 set_error( sock_get_ntstatus( errno
) );
2940 unix_len
= sizeof(bind_addr
);
2941 if (!getsockname( unix_fd
, &bind_addr
.addr
, &unix_len
))
2943 /* store the interface or magic loopback address instead of the
2944 * actual unix address */
2945 if (bind_addr
.addr
.sa_family
== AF_INET
)
2946 bind_addr
.in
.sin_addr
= unix_addr
.in
.sin_addr
;
2947 sock
->addr_len
= sockaddr_from_unix( &bind_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2950 update_addr_usage( sock
, &bind_addr
, v6only
);
2952 if (get_reply_max_size() >= sock
->addr_len
)
2953 set_reply_data( &sock
->addr
, sock
->addr_len
);
2957 case IOCTL_AFD_GETSOCKNAME
:
2960 set_error( STATUS_INVALID_PARAMETER
);
2964 if (get_reply_max_size() < sock
->addr_len
)
2966 set_error( STATUS_BUFFER_TOO_SMALL
);
2970 set_reply_data( &sock
->addr
, sock
->addr_len
);
2973 case IOCTL_AFD_WINE_DEFER
:
2975 const obj_handle_t
*handle
= get_req_data();
2976 struct sock
*acceptsock
;
2978 if (get_req_data_size() < sizeof(*handle
))
2980 set_error( STATUS_BUFFER_TOO_SMALL
);
2984 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, *handle
, 0, &sock_ops
);
2985 if (!acceptsock
) return;
2987 sock
->deferred
= acceptsock
;
2991 case IOCTL_AFD_WINE_GET_INFO
:
2993 struct afd_get_info_params params
;
2995 if (get_reply_max_size() < sizeof(params
))
2997 set_error( STATUS_BUFFER_TOO_SMALL
);
3001 params
.family
= sock
->family
;
3002 params
.type
= sock
->type
;
3003 params
.protocol
= sock
->proto
;
3004 set_reply_data( ¶ms
, sizeof(params
) );
3008 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN
:
3010 int listening
= (sock
->state
== SOCK_LISTENING
);
3012 if (get_reply_max_size() < sizeof(listening
))
3014 set_error( STATUS_BUFFER_TOO_SMALL
);
3018 set_reply_data( &listening
, sizeof(listening
) );
3022 case IOCTL_AFD_WINE_GET_SO_ERROR
:
3027 if (get_reply_max_size() < sizeof(error
))
3029 set_error( STATUS_BUFFER_TOO_SMALL
);
3033 error
= sock_error( sock
);
3036 for (i
= 0; i
< ARRAY_SIZE( sock
->errors
); ++i
)
3038 if (sock
->errors
[i
])
3040 error
= sock
->errors
[i
];
3046 error
= sock_get_error( error
);
3047 set_reply_data( &error
, sizeof(error
) );
3051 case IOCTL_AFD_WINE_GET_SO_RCVBUF
:
3053 int rcvbuf
= sock
->rcvbuf
;
3055 if (get_reply_max_size() < sizeof(rcvbuf
))
3057 set_error( STATUS_BUFFER_TOO_SMALL
);
3061 set_reply_data( &rcvbuf
, sizeof(rcvbuf
) );
3065 case IOCTL_AFD_WINE_SET_SO_RCVBUF
:
3069 if (get_req_data_size() < sizeof(rcvbuf
))
3071 set_error( STATUS_BUFFER_TOO_SMALL
);
3074 rcvbuf
= *(DWORD
*)get_req_data();
3076 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_RCVBUF
, (char *)&rcvbuf
, sizeof(rcvbuf
) ))
3077 sock
->rcvbuf
= rcvbuf
;
3079 set_error( sock_get_ntstatus( errno
) );
3083 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO
:
3085 DWORD rcvtimeo
= sock
->rcvtimeo
;
3087 if (get_reply_max_size() < sizeof(rcvtimeo
))
3089 set_error( STATUS_BUFFER_TOO_SMALL
);
3093 set_reply_data( &rcvtimeo
, sizeof(rcvtimeo
) );
3097 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO
:
3101 if (get_req_data_size() < sizeof(rcvtimeo
))
3103 set_error( STATUS_BUFFER_TOO_SMALL
);
3106 rcvtimeo
= *(DWORD
*)get_req_data();
3108 sock
->rcvtimeo
= rcvtimeo
;
3112 /* BSD socket SO_REUSEADDR is not compatible with winsock semantics. */
3113 case IOCTL_AFD_WINE_SET_SO_REUSEADDR
:
3117 if (get_req_data_size() < sizeof(reuse
))
3119 set_error( STATUS_BUFFER_TOO_SMALL
);
3123 reuse
= *(int *)get_req_data();
3125 if (reuse
&& sock
->exclusiveaddruse
)
3127 set_error( STATUS_INVALID_PARAMETER
);
3131 if (is_tcp_socket( sock
))
3134 ret
= setsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuse
, sizeof(reuse
) );
3136 if (!ret
) ret
= setsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEPORT
, &reuse
, sizeof(reuse
) );
3139 set_error( sock_get_ntstatus( errno
) );
3141 sock
->reuseaddr
= !!reuse
;
3145 case IOCTL_AFD_WINE_SET_SO_EXCLUSIVEADDRUSE
:
3149 if (get_req_data_size() < sizeof(exclusive
))
3151 set_error( STATUS_BUFFER_TOO_SMALL
);
3155 exclusive
= *(int *)get_req_data();
3156 if (exclusive
&& sock
->reuseaddr
)
3158 set_error( STATUS_INVALID_PARAMETER
);
3161 sock
->exclusiveaddruse
= !!exclusive
;
3165 case IOCTL_AFD_WINE_GET_SO_SNDBUF
:
3167 int sndbuf
= sock
->sndbuf
;
3169 if (get_reply_max_size() < sizeof(sndbuf
))
3171 set_error( STATUS_BUFFER_TOO_SMALL
);
3175 set_reply_data( &sndbuf
, sizeof(sndbuf
) );
3179 case IOCTL_AFD_WINE_SET_SO_SNDBUF
:
3183 if (get_req_data_size() < sizeof(sndbuf
))
3185 set_error( STATUS_BUFFER_TOO_SMALL
);
3188 sndbuf
= *(DWORD
*)get_req_data();
3193 /* setsockopt fails if a zero value is passed */
3194 sock
->sndbuf
= sndbuf
;
3199 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_SNDBUF
, (char *)&sndbuf
, sizeof(sndbuf
) ))
3200 sock
->sndbuf
= sndbuf
;
3202 set_error( sock_get_ntstatus( errno
) );
3206 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO
:
3208 DWORD sndtimeo
= sock
->sndtimeo
;
3210 if (get_reply_max_size() < sizeof(sndtimeo
))
3212 set_error( STATUS_BUFFER_TOO_SMALL
);
3216 set_reply_data( &sndtimeo
, sizeof(sndtimeo
) );
3220 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO
:
3224 if (get_req_data_size() < sizeof(sndtimeo
))
3226 set_error( STATUS_BUFFER_TOO_SMALL
);
3229 sndtimeo
= *(DWORD
*)get_req_data();
3231 sock
->sndtimeo
= sndtimeo
;
3235 case IOCTL_AFD_WINE_GET_SO_CONNECT_TIME
:
3239 if (get_reply_max_size() < sizeof(time
))
3241 set_error( STATUS_BUFFER_TOO_SMALL
);
3245 if (sock
->state
== SOCK_CONNECTED
)
3246 time
= (current_time
- sock
->connect_time
) / 10000000;
3248 set_reply_data( &time
, sizeof(time
) );
3252 case IOCTL_AFD_WINE_GET_SO_REUSEADDR
:
3256 if (!get_reply_max_size())
3258 set_error( STATUS_BUFFER_TOO_SMALL
);
3262 reuse
= sock
->reuseaddr
;
3263 set_reply_data( &reuse
, min( sizeof(reuse
), get_reply_max_size() ));
3267 case IOCTL_AFD_WINE_GET_SO_EXCLUSIVEADDRUSE
:
3271 if (!get_reply_max_size())
3273 set_error( STATUS_BUFFER_TOO_SMALL
);
3277 exclusive
= sock
->exclusiveaddruse
;
3278 set_reply_data( &exclusive
, min( sizeof(exclusive
), get_reply_max_size() ));
3282 case IOCTL_AFD_POLL
:
3284 if (get_reply_max_size() < get_req_data_size())
3286 set_error( STATUS_INVALID_PARAMETER
);
3290 if (is_machine_64bit( current
->process
->machine
))
3292 const struct afd_poll_params_64
*params
= get_req_data();
3294 if (get_req_data_size() < sizeof(struct afd_poll_params_64
) ||
3295 get_req_data_size() < offsetof( struct afd_poll_params_64
, sockets
[params
->count
] ))
3297 set_error( STATUS_INVALID_PARAMETER
);
3301 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, params
->sockets
);
3305 const struct afd_poll_params_32
*params
= get_req_data();
3306 struct afd_poll_socket_64
*sockets
;
3309 if (get_req_data_size() < sizeof(struct afd_poll_params_32
) ||
3310 get_req_data_size() < offsetof( struct afd_poll_params_32
, sockets
[params
->count
] ))
3312 set_error( STATUS_INVALID_PARAMETER
);
3316 if (!(sockets
= mem_alloc( params
->count
* sizeof(*sockets
) ))) return;
3317 for (i
= 0; i
< params
->count
; ++i
)
3319 sockets
[i
].socket
= params
->sockets
[i
].socket
;
3320 sockets
[i
].flags
= params
->sockets
[i
].flags
;
3321 sockets
[i
].status
= params
->sockets
[i
].status
;
3324 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, sockets
);
3332 set_error( STATUS_NOT_SUPPORTED
);
3337 static void handle_exclusive_poll(struct poll_req
*req
)
3341 for (i
= 0; i
< req
->count
; ++i
)
3343 struct sock
*sock
= req
->sockets
[i
].sock
;
3344 struct poll_req
*main_poll
= sock
->main_poll
;
3346 if (main_poll
&& main_poll
->exclusive
&& req
->exclusive
)
3348 complete_async_poll( main_poll
, STATUS_SUCCESS
);
3353 sock
->main_poll
= req
;
3357 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
3358 unsigned int count
, const struct afd_poll_socket_64
*sockets
)
3360 BOOL signaled
= FALSE
;
3361 struct poll_req
*req
;
3366 set_error( STATUS_INVALID_PARAMETER
);
3370 if (!(req
= mem_alloc( offsetof( struct poll_req
, sockets
[count
] ) )))
3373 req
->timeout
= NULL
;
3375 if (timeout
&& timeout
!= TIMEOUT_INFINITE
&&
3376 !(req
->timeout
= add_timeout_user( timeout
, async_poll_timeout
, req
)))
3381 req
->orig_timeout
= timeout
;
3383 for (i
= 0; i
< count
; ++i
)
3385 req
->sockets
[i
].sock
= (struct sock
*)get_handle_obj( current
->process
, sockets
[i
].socket
, 0, &sock_ops
);
3386 if (!req
->sockets
[i
].sock
)
3388 for (j
= 0; j
< i
; ++j
) release_object( req
->sockets
[j
].sock
);
3389 if (req
->timeout
) remove_timeout_user( req
->timeout
);
3393 req
->sockets
[i
].handle
= sockets
[i
].socket
;
3394 req
->sockets
[i
].mask
= sockets
[i
].flags
;
3395 req
->sockets
[i
].flags
= 0;
3398 req
->exclusive
= exclusive
;
3400 req
->async
= (struct async
*)grab_object( async
);
3401 req
->iosb
= async_get_iosb( async
);
3403 handle_exclusive_poll(req
);
3405 list_add_tail( &poll_list
, &req
->entry
);
3406 async_set_completion_callback( async
, free_poll_req
, req
);
3407 queue_async( &poll_sock
->poll_q
, async
);
3409 for (i
= 0; i
< count
; ++i
)
3411 struct sock
*sock
= req
->sockets
[i
].sock
;
3412 int mask
= req
->sockets
[i
].mask
;
3413 struct pollfd pollfd
;
3415 pollfd
.fd
= get_unix_fd( sock
->fd
);
3416 pollfd
.events
= poll_flags_from_afd( sock
, mask
);
3417 if (pollfd
.events
>= 0 && poll( &pollfd
, 1, 0 ) >= 0)
3418 sock_poll_event( sock
->fd
, pollfd
.revents
);
3420 /* FIXME: do other error conditions deserve a similar treatment? */
3421 if (sock
->state
!= SOCK_CONNECTING
&& sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] && (mask
& AFD_POLL_CONNECT_ERR
))
3423 req
->sockets
[i
].flags
|= AFD_POLL_CONNECT_ERR
;
3424 req
->sockets
[i
].status
= sock_get_ntstatus( sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] );
3427 if (req
->sockets
[i
].flags
)
3431 if (!timeout
|| signaled
)
3432 complete_async_poll( req
, STATUS_SUCCESS
);
3436 for (i
= 0; i
< req
->count
; ++i
)
3437 sock_reselect( req
->sockets
[i
].sock
);
3438 set_error( STATUS_PENDING
);
3441 #ifdef HAVE_LINUX_RTNETLINK_H
3443 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
3444 static struct object
*ifchange_object
;
3446 static void ifchange_dump( struct object
*obj
, int verbose
);
3447 static struct fd
*ifchange_get_fd( struct object
*obj
);
3448 static void ifchange_destroy( struct object
*obj
);
3450 static int ifchange_get_poll_events( struct fd
*fd
);
3451 static void ifchange_poll_event( struct fd
*fd
, int event
);
3455 struct object obj
; /* object header */
3456 struct fd
*fd
; /* interface change file descriptor */
3457 struct list sockets
; /* list of sockets to send interface change notifications */
3460 static const struct object_ops ifchange_ops
=
3462 sizeof(struct ifchange
), /* size */
3463 &no_type
, /* type */
3464 ifchange_dump
, /* dump */
3465 no_add_queue
, /* add_queue */
3466 NULL
, /* remove_queue */
3467 NULL
, /* signaled */
3468 no_satisfied
, /* satisfied */
3469 no_signal
, /* signal */
3470 ifchange_get_fd
, /* get_fd */
3471 default_map_access
, /* map_access */
3472 default_get_sd
, /* get_sd */
3473 default_set_sd
, /* set_sd */
3474 no_get_full_name
, /* get_full_name */
3475 no_lookup_name
, /* lookup_name */
3476 no_link_name
, /* link_name */
3477 NULL
, /* unlink_name */
3478 no_open_file
, /* open_file */
3479 no_kernel_obj_list
, /* get_kernel_obj_list */
3480 no_close_handle
, /* close_handle */
3481 ifchange_destroy
/* destroy */
3484 static const struct fd_ops ifchange_fd_ops
=
3486 ifchange_get_poll_events
, /* get_poll_events */
3487 ifchange_poll_event
, /* poll_event */
3488 NULL
, /* get_fd_type */
3489 no_fd_read
, /* read */
3490 no_fd_write
, /* write */
3491 no_fd_flush
, /* flush */
3492 no_fd_get_file_info
, /* get_file_info */
3493 no_fd_get_volume_info
, /* get_volume_info */
3494 no_fd_ioctl
, /* ioctl */
3495 NULL
, /* cancel_async */
3496 NULL
, /* queue_async */
3497 NULL
/* reselect_async */
3500 static void ifchange_dump( struct object
*obj
, int verbose
)
3502 assert( obj
->ops
== &ifchange_ops
);
3503 fprintf( stderr
, "Interface change\n" );
3506 static struct fd
*ifchange_get_fd( struct object
*obj
)
3508 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3509 return (struct fd
*)grab_object( ifchange
->fd
);
3512 static void ifchange_destroy( struct object
*obj
)
3514 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3515 assert( obj
->ops
== &ifchange_ops
);
3517 release_object( ifchange
->fd
);
3519 /* reset the global ifchange object so that it will be recreated if it is needed again */
3520 assert( obj
== ifchange_object
);
3521 ifchange_object
= NULL
;
3524 static int ifchange_get_poll_events( struct fd
*fd
)
3529 /* wake up all the sockets waiting for a change notification event */
3530 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
3532 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3533 struct list
*ptr
, *next
;
3534 assert( obj
->ops
== &ifchange_ops
);
3535 assert( obj
== ifchange_object
);
3537 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
3539 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
3541 assert( sock
->ifchange_obj
);
3542 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
3543 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
3547 static void ifchange_poll_event( struct fd
*fd
, int event
)
3549 struct object
*ifchange
= get_fd_user( fd
);
3550 unsigned int status
= STATUS_PENDING
;
3551 char buffer
[PIPE_BUF
];
3554 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
3557 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
3558 return; /* retry when poll() says the socket is ready */
3559 status
= sock_get_ntstatus( errno
);
3563 struct nlmsghdr
*nlh
;
3565 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
3567 if (nlh
->nlmsg_type
== NLMSG_DONE
)
3569 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
3570 status
= STATUS_SUCCESS
;
3573 else status
= STATUS_CANCELLED
;
3575 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
3580 /* we only need one of these interface notification objects, all of the sockets dependent upon
3581 * it will wake up when a notification event occurs */
3582 static struct object
*get_ifchange( void )
3584 #ifdef HAVE_LINUX_RTNETLINK_H
3585 struct ifchange
*ifchange
;
3586 struct sockaddr_nl addr
;
3589 if (ifchange_object
)
3591 /* increment the refcount for each socket that uses the ifchange object */
3592 return grab_object( ifchange_object
);
3595 /* create the socket we need for processing interface change notifications */
3596 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
3599 set_error( sock_get_ntstatus( errno
));
3602 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
3603 memset( &addr
, 0, sizeof(addr
) );
3604 addr
.nl_family
= AF_NETLINK
;
3605 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
3606 /* bind the socket to the special netlink kernel interface */
3607 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
3610 set_error( sock_get_ntstatus( errno
));
3613 if (!(ifchange
= alloc_object( &ifchange_ops
)))
3616 set_error( STATUS_NO_MEMORY
);
3619 list_init( &ifchange
->sockets
);
3620 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
3622 release_object( ifchange
);
3623 set_error( STATUS_NO_MEMORY
);
3626 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
3628 /* the ifchange object is now successfully configured */
3629 ifchange_object
= &ifchange
->obj
;
3630 return &ifchange
->obj
;
3632 set_error( STATUS_NOT_SUPPORTED
);
3637 /* add the socket to the interface change notification list */
3638 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
3640 #ifdef HAVE_LINUX_RTNETLINK_H
3641 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3643 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
3647 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3648 static struct object
*sock_get_ifchange( struct sock
*sock
)
3650 struct object
*ifchange
;
3652 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
3653 return sock
->ifchange_obj
;
3655 if (!(ifchange
= get_ifchange()))
3658 /* add the socket to the ifchange notification list */
3659 ifchange_add_sock( ifchange
, sock
);
3660 sock
->ifchange_obj
= ifchange
;
3664 /* destroy an existing ifchange queue for a specific socket */
3665 static void sock_release_ifchange( struct sock
*sock
)
3667 if (sock
->ifchange_obj
)
3669 list_remove( &sock
->ifchange_entry
);
3670 release_object( sock
->ifchange_obj
);
3671 sock
->ifchange_obj
= NULL
;
3675 static void socket_device_dump( struct object
*obj
, int verbose
);
3676 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3677 unsigned int attr
, struct object
*root
);
3678 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3679 unsigned int sharing
, unsigned int options
);
3681 static const struct object_ops socket_device_ops
=
3683 sizeof(struct object
), /* size */
3684 &device_type
, /* type */
3685 socket_device_dump
, /* dump */
3686 no_add_queue
, /* add_queue */
3687 NULL
, /* remove_queue */
3688 NULL
, /* signaled */
3689 no_satisfied
, /* satisfied */
3690 no_signal
, /* signal */
3691 no_get_fd
, /* get_fd */
3692 default_map_access
, /* map_access */
3693 default_get_sd
, /* get_sd */
3694 default_set_sd
, /* set_sd */
3695 default_get_full_name
, /* get_full_name */
3696 socket_device_lookup_name
, /* lookup_name */
3697 directory_link_name
, /* link_name */
3698 default_unlink_name
, /* unlink_name */
3699 socket_device_open_file
, /* open_file */
3700 no_kernel_obj_list
, /* get_kernel_obj_list */
3701 no_close_handle
, /* close_handle */
3702 no_destroy
/* destroy */
3705 static void socket_device_dump( struct object
*obj
, int verbose
)
3707 fputs( "Socket device\n", stderr
);
3710 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3711 unsigned int attr
, struct object
*root
)
3713 if (name
) name
->len
= 0;
3717 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3718 unsigned int sharing
, unsigned int options
)
3722 if (!(sock
= create_socket())) return NULL
;
3723 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
3725 release_object( sock
);
3731 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
3732 unsigned int attr
, const struct security_descriptor
*sd
)
3734 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
3737 DECL_HANDLER(recv_socket
)
3739 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3740 unsigned int status
= STATUS_PENDING
;
3741 timeout_t timeout
= 0;
3742 struct async
*async
;
3748 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3749 timeout
= (timeout_t
)sock
->rcvtimeo
* -10000;
3751 if (sock
->rd_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3752 else if (!async_queued( &sock
->read_q
))
3754 /* If read_q is not empty, we cannot really tell if the already queued
3755 * asyncs will not consume all available data; if there's no data
3756 * available, the current request won't be immediately satiable.
3758 if ((!req
->force_async
&& sock
->nonblocking
) ||
3759 check_fd_events( sock
->fd
, req
->oob
&& !is_oobinline( sock
) ? POLLPRI
: POLLIN
))
3761 /* Give the client opportunity to complete synchronously.
3762 * If it turns out that the I/O request is not actually immediately satiable,
3763 * the client may then choose to re-queue the async (with STATUS_PENDING).
3765 * Note: If the nonblocking flag is set, we don't poll the socket
3766 * here and always opt for synchronous completion first. This is
3767 * because the application has probably seen POLLIN already from a
3768 * preceding select()/poll() call before it requested to receive
3771 status
= STATUS_ALERTED
;
3775 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3776 status
= STATUS_DEVICE_NOT_READY
;
3778 sock
->pending_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3779 sock
->reported_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3781 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3783 set_error( status
);
3786 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3788 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3789 queue_async( &sock
->read_q
, async
);
3791 /* always reselect; we changed reported_events above */
3792 sock_reselect( sock
);
3794 reply
->wait
= async_handoff( async
, NULL
, 0 );
3795 reply
->options
= get_fd_options( fd
);
3796 reply
->nonblocking
= sock
->nonblocking
;
3797 release_object( async
);
3799 release_object( sock
);
3802 static void send_socket_completion_callback( void *private )
3804 struct send_req
*send_req
= private;
3805 struct iosb
*iosb
= send_req
->iosb
;
3806 struct sock
*sock
= send_req
->sock
;
3808 if (iosb
->status
!= STATUS_SUCCESS
)
3810 /* send() calls only clear and reselect events if unsuccessful. */
3811 sock
->pending_events
&= ~AFD_POLL_WRITE
;
3812 sock
->reported_events
&= ~AFD_POLL_WRITE
;
3813 sock_reselect( sock
);
3816 release_object( iosb
);
3817 release_object( sock
);
3821 DECL_HANDLER(send_socket
)
3823 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3824 unsigned int status
= STATUS_PENDING
;
3825 timeout_t timeout
= 0;
3826 struct async
*async
;
3833 if (sock
->type
== WS_SOCK_DGRAM
&& !sock
->bound
)
3835 union unix_sockaddr unix_addr
;
3837 int unix_fd
= get_unix_fd( fd
);
3839 unix_len
= get_unix_sockaddr_any( &unix_addr
, sock
->family
);
3840 if (bind( unix_fd
, &unix_addr
.addr
, unix_len
) < 0)
3843 if (getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
) >= 0)
3845 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
3848 else if (!bind_errno
) bind_errno
= errno
;
3851 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3852 timeout
= (timeout_t
)sock
->sndtimeo
* -10000;
3854 if (bind_errno
) status
= sock_get_ntstatus( bind_errno
);
3855 else if (sock
->wr_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3856 else if (!async_queued( &sock
->write_q
))
3858 /* If write_q is not empty, we cannot really tell if the already queued
3859 * asyncs will not consume all available space; if there's no space
3860 * available, the current request won't be immediately satiable.
3862 if ((!req
->force_async
&& sock
->nonblocking
) || check_fd_events( sock
->fd
, POLLOUT
))
3864 /* Give the client opportunity to complete synchronously.
3865 * If it turns out that the I/O request is not actually immediately satiable,
3866 * the client may then choose to re-queue the async (with STATUS_PENDING).
3868 * Note: If the nonblocking flag is set, we don't poll the socket
3869 * here and always opt for synchronous completion first. This is
3870 * because the application has probably seen POLLOUT already from a
3871 * preceding select()/poll() call before it requested to send data.
3873 * Furthermore, some applications expect that any send() call on a
3874 * socket that has indicated POLLOUT beforehand never fails with
3875 * WSAEWOULDBLOCK. It's possible that Linux poll() may yield
3876 * POLLOUT on the first call but not the second, even if no send()
3877 * call has been made in the meanwhile. This can happen for a
3878 * number of reasons; for example, TCP fragmentation may consume
3879 * extra buffer space for each packet that has been split out, or
3880 * the TCP/IP networking stack may decide to shrink the send buffer
3881 * due to memory pressure.
3883 status
= STATUS_ALERTED
;
3887 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3888 status
= STATUS_DEVICE_NOT_READY
;
3890 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3892 struct send_req
*send_req
;
3893 struct iosb
*iosb
= async_get_iosb( async
);
3895 if ((send_req
= mem_alloc( sizeof(*send_req
) )))
3897 send_req
->iosb
= (struct iosb
*)grab_object( iosb
);
3898 send_req
->sock
= (struct sock
*)grab_object( sock
);
3899 async_set_completion_callback( async
, send_socket_completion_callback
, send_req
);
3901 else if (status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
)
3902 status
= STATUS_NO_MEMORY
;
3904 release_object( iosb
);
3906 set_error( status
);
3909 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3911 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3913 queue_async( &sock
->write_q
, async
);
3914 sock_reselect( sock
);
3917 reply
->wait
= async_handoff( async
, NULL
, 0 );
3918 reply
->options
= get_fd_options( fd
);
3919 reply
->nonblocking
= sock
->nonblocking
;
3920 release_object( async
);
3922 release_object( sock
);
3925 DECL_HANDLER(socket_get_events
)
3927 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, 0, &sock_ops
);
3928 unsigned int status
[13];
3929 struct event
*event
= NULL
;
3932 if (get_reply_max_size() < sizeof(status
))
3934 set_error( STATUS_INVALID_PARAMETER
);
3942 if (!(event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
)))
3944 release_object( sock
);
3949 reply
->flags
= sock
->pending_events
& sock
->mask
;
3950 for (i
= 0; i
< ARRAY_SIZE( status
); ++i
)
3951 status
[i
] = sock_get_ntstatus( sock
->errors
[i
] );
3953 sock
->pending_events
&= ~sock
->mask
;
3954 sock_reselect( sock
);
3958 reset_event( event
);
3959 release_object( event
);
3962 set_reply_data( status
, sizeof(status
) );
3964 release_object( sock
);
3967 DECL_HANDLER(socket_send_icmp_id
)
3969 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, 0, &sock_ops
);
3973 if (sock
->icmp_fixup_data_len
== MAX_ICMP_HISTORY_LENGTH
)
3975 memmove( sock
->icmp_fixup_data
, sock
->icmp_fixup_data
+ 1,
3976 sizeof(*sock
->icmp_fixup_data
) * (MAX_ICMP_HISTORY_LENGTH
- 1) );
3977 --sock
->icmp_fixup_data_len
;
3980 sock
->icmp_fixup_data
[sock
->icmp_fixup_data_len
].icmp_id
= req
->icmp_id
;
3981 sock
->icmp_fixup_data
[sock
->icmp_fixup_data_len
].icmp_seq
= req
->icmp_seq
;
3982 ++sock
->icmp_fixup_data_len
;
3984 release_object( sock
);
3987 DECL_HANDLER(socket_get_icmp_id
)
3989 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, 0, &sock_ops
);
3994 for (i
= 0; i
< sock
->icmp_fixup_data_len
; ++i
)
3996 if (sock
->icmp_fixup_data
[i
].icmp_seq
== req
->icmp_seq
)
3998 reply
->icmp_id
= sock
->icmp_fixup_data
[i
].icmp_id
;
3999 --sock
->icmp_fixup_data_len
;
4000 memmove( &sock
->icmp_fixup_data
[i
], &sock
->icmp_fixup_data
[i
+ 1],
4001 (sock
->icmp_fixup_data_len
- i
) * sizeof(*sock
->icmp_fixup_data
) );
4002 release_object( sock
);
4007 set_error( STATUS_NOT_FOUND
);
4008 release_object( sock
);