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>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47 #ifdef HAVE_SYS_FILIO_H
48 # include <sys/filio.h>
53 #ifdef HAVE_LINUX_FILTER_H
54 # include <linux/filter.h>
56 #ifdef HAVE_LINUX_RTNETLINK_H
57 # include <linux/rtnetlink.h>
60 #ifdef HAVE_NETIPX_IPX_H
61 # include <netipx/ipx.h>
62 #elif defined(HAVE_LINUX_IPX_H)
63 # ifdef HAVE_ASM_TYPES_H
64 # include <asm/types.h>
66 # ifdef HAVE_LINUX_TYPES_H
67 # include <linux/types.h>
69 # include <linux/ipx.h>
71 #if defined(SOL_IPX) || defined(SO_DEFAULT_HEADERS)
75 #ifdef HAVE_LINUX_IRDA_H
76 # ifdef HAVE_LINUX_TYPES_H
77 # include <linux/types.h>
79 # include <linux/irda.h>
84 #define WIN32_NO_STATUS
94 #include "wine/rbtree.h"
103 #if defined(linux) && !defined(IP_UNICAST_IF)
104 #define IP_UNICAST_IF 50
107 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
111 struct WS_sockaddr addr
;
112 struct WS_sockaddr_in in
;
113 struct WS_sockaddr_in6 in6
;
114 struct WS_sockaddr_ipx ipx
;
120 struct sockaddr addr
;
121 struct sockaddr_in in
;
122 struct sockaddr_in6 in6
;
124 struct sockaddr_ipx ipx
;
127 struct sockaddr_irda irda
;
131 static struct list poll_list
= LIST_INIT( poll_list
);
138 struct timeout_user
*timeout
;
139 timeout_t orig_timeout
;
158 struct sock
*sock
, *acceptsock
;
160 unsigned int recv_len
, local_len
;
168 unsigned int addr_len
, send_len
, send_cursor
;
177 enum connection_state
188 struct rb_entry entry
;
189 union unix_sockaddr addr
;
194 #define MAX_ICMP_HISTORY_LENGTH 8
198 struct object obj
; /* object header */
199 struct fd
*fd
; /* socket file descriptor */
200 enum connection_state state
; /* connection state */
201 unsigned int mask
; /* event mask */
202 /* pending AFD_POLL_* events which have not yet been reported to the application */
203 unsigned int pending_events
;
204 /* AFD_POLL_* events which have already been reported and should not be
205 * selected for again until reset by a relevant call.
207 * For example, if AFD_POLL_READ is set here and not in pending_events, it
208 * has already been reported and consumed, and we should not report it
209 * again, even if POLLIN is signaled, until it is reset by e.g recv().
211 * If an event has been signaled and not consumed yet, it will be set in
212 * both pending_events and reported_events (as we should only ever report
213 * any event once until it is reset.) */
214 unsigned int reported_events
;
215 unsigned short proto
; /* socket protocol */
216 unsigned short type
; /* socket type */
217 unsigned short family
; /* socket family */
218 struct event
*event
; /* event object */
219 user_handle_t window
; /* window to send the message to */
220 unsigned int message
; /* message to send */
221 obj_handle_t wparam
; /* message wparam (socket handle) */
222 int errors
[AFD_POLL_BIT_COUNT
]; /* event errors */
223 timeout_t connect_time
;/* time the socket was connected */
224 struct sock
*deferred
; /* socket that waits for a deferred accept */
225 struct async_queue read_q
; /* queue for asynchronous reads */
226 struct async_queue write_q
; /* queue for asynchronous writes */
227 struct async_queue ifchange_q
; /* queue for interface change notifications */
228 struct async_queue accept_q
; /* queue for asynchronous accepts */
229 struct async_queue connect_q
; /* queue for asynchronous connects */
230 struct async_queue poll_q
; /* queue for asynchronous polls */
231 struct object
*ifchange_obj
; /* the interface change notification object */
232 struct list ifchange_entry
; /* entry in ifchange notification list */
233 struct list accept_list
; /* list of pending accept requests */
234 struct accept_req
*accept_recv_req
; /* pending accept-into request which will recv on this socket */
235 struct connect_req
*connect_req
; /* pending connection request */
236 struct poll_req
*main_poll
; /* main poll */
237 union win_sockaddr addr
; /* socket name */
238 int addr_len
; /* socket name length */
239 unsigned int rcvbuf
; /* advisory recv buffer size */
240 unsigned int sndbuf
; /* advisory send buffer size */
241 unsigned int rcvtimeo
; /* receive timeout in ms */
242 unsigned int sndtimeo
; /* send timeout in ms */
245 unsigned short icmp_id
;
246 unsigned short icmp_seq
;
248 icmp_fixup_data
[MAX_ICMP_HISTORY_LENGTH
]; /* Sent ICMP packets history used to fixup reply id. */
249 struct bound_addr
*bound_addr
[2]; /* Links to the entries in bound addresses tree. */
250 unsigned int icmp_fixup_data_len
; /* Sent ICMP packets history length. */
251 unsigned int rd_shutdown
: 1; /* is the read end shut down? */
252 unsigned int wr_shutdown
: 1; /* is the write end shut down? */
253 unsigned int wr_shutdown_pending
: 1; /* is a write shutdown pending? */
254 unsigned int hangup
: 1; /* has the read end received a hangup? */
255 unsigned int aborted
: 1; /* did we get a POLLERR or irregular POLLHUP? */
256 unsigned int nonblocking
: 1; /* is the socket nonblocking? */
257 unsigned int bound
: 1; /* is the socket bound? */
258 unsigned int reset
: 1; /* did we get a TCP reset? */
259 unsigned int reuseaddr
: 1; /* winsock SO_REUSEADDR option value */
260 unsigned int exclusiveaddruse
: 1; /* winsock SO_EXCLUSIVEADDRUSE option value */
263 static int is_tcp_socket( struct sock
*sock
)
265 return sock
->type
== WS_SOCK_STREAM
&& (sock
->family
== WS_AF_INET
|| sock
->family
== WS_AF_INET6
);
268 static int addr_compare( const void *key
, const struct wine_rb_entry
*entry
)
270 const struct bound_addr
*bound_addr
= RB_ENTRY_VALUE(entry
, struct bound_addr
, entry
);
271 const struct bound_addr
*addr
= key
;
273 if (addr
->addr
.addr
.sa_family
!= bound_addr
->addr
.addr
.sa_family
)
274 return addr
->addr
.addr
.sa_family
< bound_addr
->addr
.addr
.sa_family
? -1 : 1;
276 if (addr
->addr
.addr
.sa_family
== AF_INET
)
278 if (addr
->addr
.in
.sin_port
!= bound_addr
->addr
.in
.sin_port
)
279 return addr
->addr
.in
.sin_port
< bound_addr
->addr
.in
.sin_port
? -1 : 1;
280 if (bound_addr
->match_any_addr
|| addr
->match_any_addr
281 || addr
->addr
.in
.sin_addr
.s_addr
== bound_addr
->addr
.in
.sin_addr
.s_addr
)
283 return addr
->addr
.in
.sin_addr
.s_addr
< bound_addr
->addr
.in
.sin_addr
.s_addr
? -1 : 1;
286 assert( addr
->addr
.addr
.sa_family
== AF_INET6
);
287 if (addr
->addr
.in6
.sin6_port
!= bound_addr
->addr
.in6
.sin6_port
)
288 return addr
->addr
.in6
.sin6_port
< bound_addr
->addr
.in6
.sin6_port
? -1 : 1;
289 if (bound_addr
->match_any_addr
|| addr
->match_any_addr
) return 0;
290 return memcmp( &addr
->addr
.in6
.sin6_addr
, &bound_addr
->addr
.in6
.sin6_addr
, sizeof(addr
->addr
.in6
.sin6_addr
) );
293 static int ipv4addr_from_v6( union unix_sockaddr
*v4addr
, const struct sockaddr_in6
*in6
, int map_unspecified
)
295 v4addr
->in
.sin_family
= AF_INET
;
296 v4addr
->in
.sin_port
= in6
->sin6_port
;
298 if (map_unspecified
&& IN6_IS_ADDR_UNSPECIFIED(&in6
->sin6_addr
))
300 v4addr
->in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
303 if (IN6_IS_ADDR_V4COMPAT(&in6
->sin6_addr
) || IN6_IS_ADDR_V4MAPPED(&in6
->sin6_addr
))
305 memcpy( &v4addr
->in
.sin_addr
.s_addr
, &in6
->sin6_addr
.s6_addr
[12], sizeof(v4addr
->in
.sin_addr
.s_addr
) );
311 static struct rb_tree bound_addresses_tree
= { addr_compare
};
313 static int should_track_conflicts_for_addr( struct sock
*sock
, const union unix_sockaddr
*addr
)
315 if (!is_tcp_socket( sock
)) return 0;
317 if (sock
->family
== WS_AF_INET
&& addr
->addr
.sa_family
== AF_INET
&& addr
->in
.sin_port
)
319 else if (sock
->family
== WS_AF_INET6
&& addr
->addr
.sa_family
== AF_INET6
&& addr
->in6
.sin6_port
)
325 static int is_any_addr( const union unix_sockaddr
*addr
)
327 if (addr
->addr
.sa_family
== AF_INET
&& addr
->in
.sin_addr
.s_addr
== htonl( INADDR_ANY
))
329 if (addr
->addr
.sa_family
== AF_INET6
&& IN6_IS_ADDR_UNSPECIFIED(&addr
->in6
.sin6_addr
))
334 static int check_addr_usage( struct sock
*sock
, const union unix_sockaddr
*addr
, int v6only
)
336 struct bound_addr
*bound_addr
, search_addr
;
337 struct rb_entry
*entry
;
339 if (!should_track_conflicts_for_addr( sock
, addr
)) return 0;
341 search_addr
.addr
= *addr
;
342 search_addr
.match_any_addr
= sock
->exclusiveaddruse
&& is_any_addr( addr
);
344 if ((entry
= rb_get( &bound_addresses_tree
, &search_addr
)))
346 bound_addr
= WINE_RB_ENTRY_VALUE(entry
, struct bound_addr
, entry
);
347 if (bound_addr
->reuse_count
== -1 || !sock
->reuseaddr
)
349 set_error( sock
->reuseaddr
|| bound_addr
->match_any_addr
350 ? STATUS_ACCESS_DENIED
: STATUS_SHARING_VIOLATION
);
355 if (sock
->family
!= WS_AF_INET6
|| v6only
) return 0;
356 if (!ipv4addr_from_v6( &search_addr
.addr
, &addr
->in6
, sock
->exclusiveaddruse
)) return 0;
358 search_addr
.match_any_addr
= sock
->exclusiveaddruse
&& is_any_addr( &search_addr
.addr
);
359 if ((entry
= rb_get( &bound_addresses_tree
, &search_addr
)))
361 bound_addr
= WINE_RB_ENTRY_VALUE(entry
, struct bound_addr
, entry
);
362 if (bound_addr
->reuse_count
== -1 || !sock
->reuseaddr
)
364 set_error( sock
->reuseaddr
|| bound_addr
->match_any_addr
365 ? STATUS_ACCESS_DENIED
: STATUS_SHARING_VIOLATION
);
372 static struct bound_addr
*register_bound_address( struct sock
*sock
, const union unix_sockaddr
*addr
)
374 struct bound_addr
*bound_addr
, *temp
;
376 if (!(bound_addr
= mem_alloc( sizeof(*bound_addr
) )))
379 bound_addr
->addr
= *addr
;
380 bound_addr
->match_any_addr
= sock
->exclusiveaddruse
&& is_any_addr( addr
);
382 if (rb_put( &bound_addresses_tree
, bound_addr
, &bound_addr
->entry
))
385 bound_addr
= WINE_RB_ENTRY_VALUE(rb_get( &bound_addresses_tree
, temp
), struct bound_addr
, entry
);
387 if (bound_addr
->reuse_count
== -1)
390 fprintf( stderr
, "register_bound_address: address being updated is already exclusively bound\n" );
393 ++bound_addr
->reuse_count
;
397 bound_addr
->reuse_count
= sock
->reuseaddr
? 1 : -1;
402 static void update_addr_usage( struct sock
*sock
, const union unix_sockaddr
*addr
, int v6only
)
404 union unix_sockaddr v4addr
;
406 assert( !sock
->bound_addr
[0] && !sock
->bound_addr
[1] );
408 if (!should_track_conflicts_for_addr( sock
, addr
)) return;
410 sock
->bound_addr
[0] = register_bound_address( sock
, addr
);
412 if (sock
->family
!= WS_AF_INET6
|| v6only
) return;
414 if (!ipv4addr_from_v6( &v4addr
, &addr
->in6
, sock
->exclusiveaddruse
)) return;
416 sock
->bound_addr
[1] = register_bound_address( sock
, &v4addr
);
419 static void sock_dump( struct object
*obj
, int verbose
);
420 static struct fd
*sock_get_fd( struct object
*obj
);
421 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
422 static void sock_destroy( struct object
*obj
);
423 static struct object
*sock_get_ifchange( struct sock
*sock
);
424 static void sock_release_ifchange( struct sock
*sock
);
426 static int sock_get_poll_events( struct fd
*fd
);
427 static void sock_poll_event( struct fd
*fd
, int event
);
428 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
429 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
430 static void sock_cancel_async( struct fd
*fd
, struct async
*async
);
431 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
433 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
);
434 static struct sock
*accept_socket( struct sock
*sock
);
435 static int sock_get_ntstatus( int err
);
436 static unsigned int sock_get_error( int err
);
437 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
438 unsigned int count
, const struct afd_poll_socket_64
*sockets
);
440 static const struct object_ops sock_ops
=
442 sizeof(struct sock
), /* size */
443 &file_type
, /* type */
444 sock_dump
, /* dump */
445 add_queue
, /* add_queue */
446 remove_queue
, /* remove_queue */
447 default_fd_signaled
, /* signaled */
448 no_satisfied
, /* satisfied */
449 no_signal
, /* signal */
450 sock_get_fd
, /* get_fd */
451 default_map_access
, /* map_access */
452 default_get_sd
, /* get_sd */
453 default_set_sd
, /* set_sd */
454 no_get_full_name
, /* get_full_name */
455 no_lookup_name
, /* lookup_name */
456 no_link_name
, /* link_name */
457 NULL
, /* unlink_name */
458 no_open_file
, /* open_file */
459 no_kernel_obj_list
, /* get_kernel_obj_list */
460 sock_close_handle
, /* close_handle */
461 sock_destroy
/* destroy */
464 static const struct fd_ops sock_fd_ops
=
466 sock_get_poll_events
, /* get_poll_events */
467 sock_poll_event
, /* poll_event */
468 sock_get_fd_type
, /* get_fd_type */
469 no_fd_read
, /* read */
470 no_fd_write
, /* write */
471 no_fd_flush
, /* flush */
472 default_fd_get_file_info
, /* get_file_info */
473 no_fd_get_volume_info
, /* get_volume_info */
474 sock_ioctl
, /* ioctl */
475 sock_cancel_async
, /* cancel_async */
476 no_fd_queue_async
, /* queue_async */
477 sock_reselect_async
/* reselect_async */
480 static int sockaddr_from_unix( const union unix_sockaddr
*uaddr
, struct WS_sockaddr
*wsaddr
, socklen_t wsaddrlen
)
482 memset( wsaddr
, 0, wsaddrlen
);
484 switch (uaddr
->addr
.sa_family
)
488 struct WS_sockaddr_in win
= {0};
490 if (wsaddrlen
< sizeof(win
)) return -1;
491 win
.sin_family
= WS_AF_INET
;
492 win
.sin_port
= uaddr
->in
.sin_port
;
493 memcpy( &win
.sin_addr
, &uaddr
->in
.sin_addr
, sizeof(win
.sin_addr
) );
494 memcpy( wsaddr
, &win
, sizeof(win
) );
500 struct WS_sockaddr_in6 win
= {0};
502 if (wsaddrlen
< sizeof(win
)) return -1;
503 win
.sin6_family
= WS_AF_INET6
;
504 win
.sin6_port
= uaddr
->in6
.sin6_port
;
505 win
.sin6_flowinfo
= uaddr
->in6
.sin6_flowinfo
;
506 memcpy( &win
.sin6_addr
, &uaddr
->in6
.sin6_addr
, sizeof(win
.sin6_addr
) );
507 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
508 win
.sin6_scope_id
= uaddr
->in6
.sin6_scope_id
;
510 memcpy( wsaddr
, &win
, sizeof(win
) );
517 struct WS_sockaddr_ipx win
= {0};
519 if (wsaddrlen
< sizeof(win
)) return -1;
520 win
.sa_family
= WS_AF_IPX
;
521 memcpy( win
.sa_netnum
, &uaddr
->ipx
.sipx_network
, sizeof(win
.sa_netnum
) );
522 memcpy( win
.sa_nodenum
, &uaddr
->ipx
.sipx_node
, sizeof(win
.sa_nodenum
) );
523 win
.sa_socket
= uaddr
->ipx
.sipx_port
;
524 memcpy( wsaddr
, &win
, sizeof(win
) );
534 if (wsaddrlen
< sizeof(win
)) return -1;
535 win
.irdaAddressFamily
= WS_AF_IRDA
;
536 memcpy( win
.irdaDeviceID
, &uaddr
->irda
.sir_addr
, sizeof(win
.irdaDeviceID
) );
537 if (uaddr
->irda
.sir_lsap_sel
!= LSAP_ANY
)
538 snprintf( win
.irdaServiceName
, sizeof(win
.irdaServiceName
), "LSAP-SEL%u", uaddr
->irda
.sir_lsap_sel
);
540 memcpy( win
.irdaServiceName
, uaddr
->irda
.sir_name
, sizeof(win
.irdaServiceName
) );
541 memcpy( wsaddr
, &win
, sizeof(win
) );
555 static socklen_t
sockaddr_to_unix( const struct WS_sockaddr
*wsaddr
, int wsaddrlen
, union unix_sockaddr
*uaddr
)
557 memset( uaddr
, 0, sizeof(*uaddr
) );
559 switch (wsaddr
->sa_family
)
563 struct WS_sockaddr_in win
= {0};
565 if (wsaddrlen
< sizeof(win
)) return 0;
566 memcpy( &win
, wsaddr
, sizeof(win
) );
567 uaddr
->in
.sin_family
= AF_INET
;
568 uaddr
->in
.sin_port
= win
.sin_port
;
569 memcpy( &uaddr
->in
.sin_addr
, &win
.sin_addr
, sizeof(win
.sin_addr
) );
570 return sizeof(uaddr
->in
);
575 struct WS_sockaddr_in6 win
= {0};
577 if (wsaddrlen
< sizeof(win
)) return 0;
578 memcpy( &win
, wsaddr
, sizeof(win
) );
579 uaddr
->in6
.sin6_family
= AF_INET6
;
580 uaddr
->in6
.sin6_port
= win
.sin6_port
;
581 uaddr
->in6
.sin6_flowinfo
= win
.sin6_flowinfo
;
582 memcpy( &uaddr
->in6
.sin6_addr
, &win
.sin6_addr
, sizeof(win
.sin6_addr
) );
583 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
584 uaddr
->in6
.sin6_scope_id
= win
.sin6_scope_id
;
586 return sizeof(uaddr
->in6
);
592 struct WS_sockaddr_ipx win
= {0};
594 if (wsaddrlen
< sizeof(win
)) return 0;
595 memcpy( &win
, wsaddr
, sizeof(win
) );
596 uaddr
->ipx
.sipx_family
= AF_IPX
;
597 memcpy( &uaddr
->ipx
.sipx_network
, win
.sa_netnum
, sizeof(win
.sa_netnum
) );
598 memcpy( &uaddr
->ipx
.sipx_node
, win
.sa_nodenum
, sizeof(win
.sa_nodenum
) );
599 uaddr
->ipx
.sipx_port
= win
.sa_socket
;
600 return sizeof(uaddr
->ipx
);
607 SOCKADDR_IRDA win
= {0};
608 unsigned int lsap_sel
;
610 if (wsaddrlen
< sizeof(win
)) return 0;
611 memcpy( &win
, wsaddr
, sizeof(win
) );
612 uaddr
->irda
.sir_family
= AF_IRDA
;
613 if (sscanf( win
.irdaServiceName
, "LSAP-SEL%u", &lsap_sel
) == 1)
614 uaddr
->irda
.sir_lsap_sel
= lsap_sel
;
617 uaddr
->irda
.sir_lsap_sel
= LSAP_ANY
;
618 memcpy( uaddr
->irda
.sir_name
, win
.irdaServiceName
, sizeof(win
.irdaServiceName
) );
620 memcpy( &uaddr
->irda
.sir_addr
, win
.irdaDeviceID
, sizeof(win
.irdaDeviceID
) );
621 return sizeof(uaddr
->irda
);
628 default: /* likely an ipv4 address */
629 case sizeof(struct WS_sockaddr_in
):
630 return sizeof(uaddr
->in
);
633 case sizeof(struct WS_sockaddr_ipx
):
634 return sizeof(uaddr
->ipx
);
638 case sizeof(SOCKADDR_IRDA
):
639 return sizeof(uaddr
->irda
);
642 case sizeof(struct WS_sockaddr_in6
):
643 return sizeof(uaddr
->in6
);
651 static socklen_t
get_unix_sockaddr_any( union unix_sockaddr
*uaddr
, int ws_family
)
653 memset( uaddr
, 0, sizeof(*uaddr
) );
657 uaddr
->in
.sin_family
= AF_INET
;
658 return sizeof(uaddr
->in
);
660 uaddr
->in6
.sin6_family
= AF_INET6
;
661 return sizeof(uaddr
->in6
);
664 uaddr
->ipx
.sipx_family
= AF_IPX
;
665 return sizeof(uaddr
->ipx
);
669 uaddr
->irda
.sir_family
= AF_IRDA
;
670 return sizeof(uaddr
->irda
);
677 /* some events are generated at the same time but must be sent in a particular
678 * order (e.g. CONNECT must be sent before READ) */
679 static const enum afd_poll_bit event_bitorder
[] =
681 AFD_POLL_BIT_CONNECT
,
682 AFD_POLL_BIT_CONNECT_ERR
,
693 SOCK_SHUTDOWN_ERROR
= -1,
694 SOCK_SHUTDOWN_EOF
= 0,
695 SOCK_SHUTDOWN_POLLHUP
= 1
698 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
700 static sock_shutdown_t
sock_check_pollhup(void)
702 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
707 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
708 if ( shutdown( fd
[0], 1 ) ) goto out
;
714 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
715 n
= poll( &pfd
, 1, 1 );
716 if ( n
!= 1 ) goto out
; /* error or timeout */
717 if ( pfd
.revents
& POLLHUP
)
718 ret
= SOCK_SHUTDOWN_POLLHUP
;
719 else if ( pfd
.revents
& POLLIN
&&
720 read( fd
[1], &dummy
, 1 ) == 0 )
721 ret
= SOCK_SHUTDOWN_EOF
;
731 sock_shutdown_type
= sock_check_pollhup();
733 switch ( sock_shutdown_type
)
735 case SOCK_SHUTDOWN_EOF
:
736 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
738 case SOCK_SHUTDOWN_POLLHUP
:
739 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
742 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
743 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
747 static void sock_reselect( struct sock
*sock
)
749 int ev
= sock_get_poll_events( sock
->fd
);
752 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
754 set_fd_events( sock
->fd
, ev
);
757 static unsigned int afd_poll_flag_to_win32( unsigned int flags
)
759 static const unsigned int map
[] =
763 FD_WRITE
, /* WRITE */
765 FD_CLOSE
, /* RESET */
767 FD_CONNECT
, /* CONNECT */
768 FD_ACCEPT
, /* ACCEPT */
769 FD_CONNECT
, /* CONNECT_ERR */
772 unsigned int i
, ret
= 0;
774 for (i
= 0; i
< ARRAY_SIZE(map
); ++i
)
776 if (flags
& (1 << i
)) ret
|= map
[i
];
782 /* wake anybody waiting on the socket event or send the associated message */
783 static void sock_wake_up( struct sock
*sock
)
785 unsigned int events
= sock
->pending_events
& sock
->mask
;
790 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
792 set_event( sock
->event
);
796 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
797 for (i
= 0; i
< ARRAY_SIZE(event_bitorder
); i
++)
799 enum afd_poll_bit event
= event_bitorder
[i
];
800 if (events
& (1 << event
))
802 lparam_t lparam
= afd_poll_flag_to_win32(1 << event
) | (sock_get_error( sock
->errors
[event
] ) << 16);
803 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
806 sock
->pending_events
= 0;
807 sock_reselect( sock
);
811 static inline int sock_error( struct sock
*sock
)
814 socklen_t len
= sizeof(error
);
816 getsockopt( get_unix_fd(sock
->fd
), SOL_SOCKET
, SO_ERROR
, (void *)&error
, &len
);
820 case SOCK_UNCONNECTED
:
823 case SOCK_CONNECTING
:
825 sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] = error
;
827 error
= sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
];
832 sock
->errors
[AFD_POLL_BIT_ACCEPT
] = error
;
834 error
= sock
->errors
[AFD_POLL_BIT_ACCEPT
];
838 case SOCK_CONNECTIONLESS
:
839 if (error
== ECONNRESET
|| error
== EPIPE
)
845 sock
->errors
[AFD_POLL_BIT_HUP
] = error
;
847 error
= sock
->errors
[AFD_POLL_BIT_HUP
];
854 static void free_accept_req( void *private )
856 struct accept_req
*req
= private;
857 list_remove( &req
->entry
);
860 req
->acceptsock
->accept_recv_req
= NULL
;
861 release_object( req
->acceptsock
);
863 release_object( req
->async
);
864 release_object( req
->iosb
);
865 release_object( req
->sock
);
869 static void fill_accept_output( struct accept_req
*req
)
871 const data_size_t out_size
= req
->iosb
->out_size
;
872 struct async
*async
= req
->async
;
873 union unix_sockaddr unix_addr
;
874 struct WS_sockaddr
*win_addr
;
875 unsigned int remote_len
;
881 if (!(out_data
= mem_alloc( out_size
)))
883 async_terminate( async
, get_error() );
887 fd
= get_unix_fd( req
->acceptsock
->fd
);
889 if (req
->recv_len
&& (size
= recv( fd
, out_data
, req
->recv_len
, 0 )) < 0)
891 if (!req
->accepted
&& errno
== EWOULDBLOCK
)
894 sock_reselect( req
->acceptsock
);
898 async_terminate( async
, sock_get_ntstatus( errno
) );
905 if (req
->local_len
< sizeof(int))
907 async_terminate( async
, STATUS_BUFFER_TOO_SMALL
);
912 unix_len
= sizeof(unix_addr
);
913 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ sizeof(int));
914 if (getsockname( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
915 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, req
->local_len
- sizeof(int) )) < 0)
917 async_terminate( async
, sock_get_ntstatus( errno
) );
921 memcpy( out_data
+ req
->recv_len
, &win_len
, sizeof(int) );
924 unix_len
= sizeof(unix_addr
);
925 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ req
->local_len
+ sizeof(int));
926 remote_len
= out_size
- req
->recv_len
- req
->local_len
;
927 if (getpeername( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
928 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, remote_len
- sizeof(int) )) < 0)
930 async_terminate( async
, sock_get_ntstatus( errno
) );
934 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
936 async_request_complete( req
->async
, STATUS_SUCCESS
, size
, out_size
, out_data
);
939 static void complete_async_accept( struct sock
*sock
, struct accept_req
*req
)
941 struct sock
*acceptsock
= req
->acceptsock
;
942 struct async
*async
= req
->async
;
944 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
948 if (!accept_into_socket( sock
, acceptsock
))
950 async_terminate( async
, get_error() );
953 fill_accept_output( req
);
959 if (!(acceptsock
= accept_socket( sock
)))
961 async_terminate( async
, get_error() );
964 handle
= alloc_handle_no_access_check( async_get_thread( async
)->process
, &acceptsock
->obj
,
965 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
966 acceptsock
->wparam
= handle
;
967 sock_reselect( acceptsock
);
968 release_object( acceptsock
);
971 async_terminate( async
, get_error() );
975 async_request_complete_alloc( req
->async
, STATUS_SUCCESS
, 0, sizeof(handle
), &handle
);
979 static void complete_async_accept_recv( struct accept_req
*req
)
981 if (debug_level
) fprintf( stderr
, "completing accept recv request for socket %p\n", req
->acceptsock
);
983 assert( req
->recv_len
);
985 fill_accept_output( req
);
988 static void free_connect_req( void *private )
990 struct connect_req
*req
= private;
992 req
->sock
->connect_req
= NULL
;
993 release_object( req
->async
);
994 release_object( req
->iosb
);
995 release_object( req
->sock
);
999 static void complete_async_connect( struct sock
*sock
)
1001 struct connect_req
*req
= sock
->connect_req
;
1002 const char *in_buffer
;
1006 if (debug_level
) fprintf( stderr
, "completing connect request for socket %p\n", sock
);
1010 async_terminate( req
->async
, STATUS_SUCCESS
);
1014 in_buffer
= (const char *)req
->iosb
->in_data
+ sizeof(struct afd_connect_params
) + req
->addr_len
;
1015 len
= req
->send_len
- req
->send_cursor
;
1017 ret
= send( get_unix_fd( sock
->fd
), in_buffer
+ req
->send_cursor
, len
, 0 );
1018 if (ret
< 0 && errno
!= EWOULDBLOCK
)
1019 async_terminate( req
->async
, sock_get_ntstatus( errno
) );
1020 else if (ret
== len
)
1021 async_request_complete( req
->async
, STATUS_SUCCESS
, req
->send_len
, 0, NULL
);
1023 req
->send_cursor
+= ret
;
1026 static void free_poll_req( void *private )
1028 struct poll_req
*req
= private;
1031 if (req
->timeout
) remove_timeout_user( req
->timeout
);
1033 for (i
= 0; i
< req
->count
; ++i
)
1034 release_object( req
->sockets
[i
].sock
);
1035 release_object( req
->async
);
1036 release_object( req
->iosb
);
1037 list_remove( &req
->entry
);
1041 static int is_oobinline( struct sock
*sock
)
1044 socklen_t len
= sizeof(oobinline
);
1045 return !getsockopt( get_unix_fd( sock
->fd
), SOL_SOCKET
, SO_OOBINLINE
, (char *)&oobinline
, &len
) && oobinline
;
1048 static int get_poll_flags( struct sock
*sock
, int event
)
1052 /* A connection-mode socket which has never been connected does not return
1053 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
1054 if (sock
->state
== SOCK_UNCONNECTED
)
1055 event
&= ~(POLLOUT
| POLLHUP
);
1059 if (sock
->state
== SOCK_LISTENING
)
1060 flags
|= AFD_POLL_ACCEPT
;
1062 flags
|= AFD_POLL_READ
;
1064 if (event
& POLLPRI
)
1065 flags
|= is_oobinline( sock
) ? AFD_POLL_READ
: AFD_POLL_OOB
;
1066 if (event
& POLLOUT
)
1067 flags
|= AFD_POLL_WRITE
;
1068 if (sock
->state
== SOCK_CONNECTED
)
1069 flags
|= AFD_POLL_CONNECT
;
1070 if (event
& POLLHUP
)
1071 flags
|= AFD_POLL_HUP
;
1072 if (event
& POLLERR
)
1073 flags
|= AFD_POLL_CONNECT_ERR
;
1075 flags
|= AFD_POLL_RESET
;
1080 static void complete_async_poll( struct poll_req
*req
, unsigned int status
)
1082 unsigned int i
, signaled_count
= 0;
1084 for (i
= 0; i
< req
->count
; ++i
)
1086 struct sock
*sock
= req
->sockets
[i
].sock
;
1088 if (sock
->main_poll
== req
)
1089 sock
->main_poll
= NULL
;
1094 for (i
= 0; i
< req
->count
; ++i
)
1096 if (req
->sockets
[i
].flags
)
1101 if (is_machine_64bit( async_get_thread( req
->async
)->process
->machine
))
1103 size_t output_size
= offsetof( struct afd_poll_params_64
, sockets
[signaled_count
] );
1104 struct afd_poll_params_64
*output
;
1106 if (!(output
= mem_alloc( output_size
)))
1108 async_terminate( req
->async
, get_error() );
1111 memset( output
, 0, output_size
);
1112 output
->timeout
= req
->orig_timeout
;
1113 output
->exclusive
= req
->exclusive
;
1114 for (i
= 0; i
< req
->count
; ++i
)
1116 if (!req
->sockets
[i
].flags
) continue;
1117 output
->sockets
[output
->count
].socket
= req
->sockets
[i
].handle
;
1118 output
->sockets
[output
->count
].flags
= req
->sockets
[i
].flags
;
1119 output
->sockets
[output
->count
].status
= req
->sockets
[i
].status
;
1122 assert( output
->count
== signaled_count
);
1124 async_request_complete( req
->async
, status
, output_size
, output_size
, output
);
1128 size_t output_size
= offsetof( struct afd_poll_params_32
, sockets
[signaled_count
] );
1129 struct afd_poll_params_32
*output
;
1131 if (!(output
= mem_alloc( output_size
)))
1133 async_terminate( req
->async
, get_error() );
1136 memset( output
, 0, output_size
);
1137 output
->timeout
= req
->orig_timeout
;
1138 output
->exclusive
= req
->exclusive
;
1139 for (i
= 0; i
< req
->count
; ++i
)
1141 if (!req
->sockets
[i
].flags
) continue;
1142 output
->sockets
[output
->count
].socket
= req
->sockets
[i
].handle
;
1143 output
->sockets
[output
->count
].flags
= req
->sockets
[i
].flags
;
1144 output
->sockets
[output
->count
].status
= req
->sockets
[i
].status
;
1147 assert( output
->count
== signaled_count
);
1149 async_request_complete( req
->async
, status
, output_size
, output_size
, output
);
1153 static void complete_async_polls( struct sock
*sock
, int event
, int error
)
1155 int flags
= get_poll_flags( sock
, event
);
1156 struct poll_req
*req
, *next
;
1158 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &poll_list
, struct poll_req
, entry
)
1162 if (req
->iosb
->status
!= STATUS_PENDING
) continue;
1164 for (i
= 0; i
< req
->count
; ++i
)
1166 if (req
->sockets
[i
].sock
!= sock
) continue;
1167 if (!(req
->sockets
[i
].mask
& flags
)) continue;
1170 fprintf( stderr
, "completing poll for socket %p, wanted %#x got %#x\n",
1171 sock
, req
->sockets
[i
].mask
, flags
);
1173 req
->sockets
[i
].flags
= req
->sockets
[i
].mask
& flags
;
1174 req
->sockets
[i
].status
= sock_get_ntstatus( error
);
1178 complete_async_poll( req
, STATUS_SUCCESS
);
1185 static void async_poll_timeout( void *private )
1187 struct poll_req
*req
= private;
1189 req
->timeout
= NULL
;
1191 if (req
->iosb
->status
!= STATUS_PENDING
) return;
1193 complete_async_poll( req
, STATUS_TIMEOUT
);
1196 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
1198 if (event
& (POLLIN
| POLLPRI
))
1200 struct accept_req
*req
;
1202 LIST_FOR_EACH_ENTRY( req
, &sock
->accept_list
, struct accept_req
, entry
)
1204 if (req
->iosb
->status
== STATUS_PENDING
&& !req
->accepted
)
1206 complete_async_accept( sock
, req
);
1212 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
1213 complete_async_accept_recv( sock
->accept_recv_req
);
1216 if ((event
& POLLOUT
) && sock
->connect_req
&& sock
->connect_req
->iosb
->status
== STATUS_PENDING
)
1217 complete_async_connect( sock
);
1219 if ((event
& (POLLIN
| POLLPRI
)) && async_queued( &sock
->read_q
))
1221 if (async_waiting( &sock
->read_q
))
1223 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
1224 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
1226 event
&= ~(POLLIN
| POLLPRI
);
1229 if ((event
& POLLOUT
) && async_queued( &sock
->write_q
))
1231 if (async_waiting( &sock
->write_q
))
1233 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
1234 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
1239 if (event
& (POLLERR
| POLLHUP
))
1241 int status
= sock_get_ntstatus( error
);
1242 struct accept_req
*req
, *next
;
1244 async_wake_up( &sock
->read_q
, status
);
1245 async_wake_up( &sock
->write_q
, status
);
1247 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
1249 if (req
->iosb
->status
== STATUS_PENDING
)
1250 async_terminate( req
->async
, status
);
1253 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
1254 async_terminate( sock
->accept_recv_req
->async
, status
);
1256 if (sock
->connect_req
)
1257 async_terminate( sock
->connect_req
->async
, status
);
1262 async_wake_up( &sock
->read_q
, STATUS_CONNECTION_RESET
);
1263 async_wake_up( &sock
->write_q
, STATUS_CONNECTION_RESET
);
1265 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
1266 async_terminate( sock
->accept_recv_req
->async
, STATUS_CONNECTION_RESET
);
1272 static void post_socket_event( struct sock
*sock
, enum afd_poll_bit event_bit
)
1274 unsigned int event
= (1 << event_bit
);
1276 if (!(sock
->reported_events
& event
))
1278 sock
->pending_events
|= event
;
1279 sock
->reported_events
|= event
;
1283 static void sock_dispatch_events( struct sock
*sock
, enum connection_state prevstate
, int event
)
1287 case SOCK_UNCONNECTED
:
1290 case SOCK_CONNECTING
:
1291 if (event
& POLLOUT
)
1292 post_socket_event( sock
, AFD_POLL_BIT_CONNECT
);
1293 if (event
& (POLLERR
| POLLHUP
))
1294 post_socket_event( sock
, AFD_POLL_BIT_CONNECT_ERR
);
1297 case SOCK_LISTENING
:
1298 if (event
& (POLLIN
| POLLERR
| POLLHUP
))
1299 post_socket_event( sock
, AFD_POLL_BIT_ACCEPT
);
1302 case SOCK_CONNECTED
:
1303 case SOCK_CONNECTIONLESS
:
1305 post_socket_event( sock
, AFD_POLL_BIT_RESET
);
1308 post_socket_event( sock
, AFD_POLL_BIT_READ
);
1310 if (event
& POLLOUT
)
1311 post_socket_event( sock
, AFD_POLL_BIT_WRITE
);
1313 if (event
& POLLPRI
)
1314 post_socket_event( sock
, AFD_POLL_BIT_OOB
);
1316 if (event
& (POLLERR
| POLLHUP
))
1317 post_socket_event( sock
, AFD_POLL_BIT_HUP
);
1321 sock_wake_up( sock
);
1324 static void sock_poll_event( struct fd
*fd
, int event
)
1326 struct sock
*sock
= get_fd_user( fd
);
1327 int hangup_seen
= 0;
1328 enum connection_state prevstate
= sock
->state
;
1331 assert( sock
->obj
.ops
== &sock_ops
);
1333 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
1335 if (event
& (POLLERR
| POLLHUP
))
1336 error
= sock_error( sock
);
1338 switch (sock
->state
)
1340 case SOCK_UNCONNECTED
:
1343 case SOCK_CONNECTING
:
1344 if (event
& (POLLERR
|POLLHUP
))
1346 sock
->state
= SOCK_UNCONNECTED
;
1349 else if (event
& POLLOUT
)
1351 sock
->state
= SOCK_CONNECTED
;
1352 sock
->connect_time
= current_time
;
1353 sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] = 0;
1357 case SOCK_LISTENING
:
1360 case SOCK_CONNECTED
:
1361 case SOCK_CONNECTIONLESS
:
1363 event
&= ~(POLLIN
| POLLERR
| POLLHUP
);
1365 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
1370 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
1371 * has been closed, so we need to check for it explicitly here */
1372 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
1381 /* EAGAIN can happen if an async recv() falls between the server's poll()
1382 call and the invocation of this routine */
1383 if (errno
== ECONNRESET
|| errno
== EPIPE
)
1387 else if (errno
!= EAGAIN
)
1391 sock
->errors
[AFD_POLL_BIT_HUP
] = error
;
1393 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
1398 if (hangup_seen
|| (sock_shutdown_type
== SOCK_SHUTDOWN_POLLHUP
&& (event
& POLLHUP
)))
1402 else if (event
& (POLLHUP
| POLLERR
))
1407 fprintf( stderr
, "socket %p aborted by error %d, event %#x\n", sock
, error
, event
);
1415 event
= sock_dispatch_asyncs( sock
, event
, error
);
1416 sock_dispatch_events( sock
, prevstate
, event
);
1417 complete_async_polls( sock
, event
, error
);
1419 sock_reselect( sock
);
1422 static void sock_dump( struct object
*obj
, int verbose
)
1424 struct sock
*sock
= (struct sock
*)obj
;
1425 assert( obj
->ops
== &sock_ops
);
1426 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
1427 sock
->fd
, sock
->state
,
1428 sock
->mask
, sock
->pending_events
, sock
->reported_events
);
1431 static int poll_flags_from_afd( struct sock
*sock
, int flags
)
1435 /* A connection-mode socket which has never been connected does
1436 * not return write or hangup events, but Linux returns
1437 * POLLOUT | POLLHUP. */
1438 if (sock
->state
== SOCK_UNCONNECTED
)
1441 if (flags
& (AFD_POLL_READ
| AFD_POLL_ACCEPT
))
1443 if ((flags
& AFD_POLL_HUP
) && sock
->type
== WS_SOCK_STREAM
)
1445 if (flags
& AFD_POLL_OOB
)
1446 ev
|= is_oobinline( sock
) ? POLLIN
: POLLPRI
;
1447 if (flags
& AFD_POLL_WRITE
)
1453 static int sock_get_poll_events( struct fd
*fd
)
1455 struct sock
*sock
= get_fd_user( fd
);
1456 unsigned int mask
= sock
->mask
& ~sock
->reported_events
;
1457 struct poll_req
*req
;
1460 assert( sock
->obj
.ops
== &sock_ops
);
1462 if (!sock
->type
) /* not initialized yet */
1465 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1469 for (i
= 0; i
< req
->count
; ++i
)
1471 if (req
->sockets
[i
].sock
!= sock
) continue;
1473 ev
|= poll_flags_from_afd( sock
, req
->sockets
[i
].mask
);
1477 switch (sock
->state
)
1479 case SOCK_UNCONNECTED
:
1480 /* A connection-mode Windows socket which has never been connected does
1481 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1482 * need to return -1 here, to prevent the socket from being polled on at
1486 case SOCK_CONNECTING
:
1489 case SOCK_LISTENING
:
1490 if (!list_empty( &sock
->accept_list
) || (mask
& AFD_POLL_ACCEPT
))
1494 case SOCK_CONNECTED
:
1495 case SOCK_CONNECTIONLESS
:
1496 if (sock
->hangup
&& sock
->wr_shutdown
&& !sock
->wr_shutdown_pending
)
1498 /* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
1499 * if both the socket and its peer are SHUT_WR.
1501 * We don't use SHUT_RD, so we can only encounter this in the latter
1502 * case. In that case there can't be any pending read requests (they
1503 * would have already been completed with a length of zero), the
1504 * above condition ensures that we don't have any pending write
1505 * requests, and nothing that can change about the socket state that
1506 * would complete a pending poll request. */
1510 if (sock
->aborted
|| sock
->reset
)
1513 if (sock
->accept_recv_req
)
1517 else if (async_queued( &sock
->read_q
))
1519 /* Clear POLLIN and POLLPRI if we have an alerted async, even if
1520 * we're polling this socket for READ or OOB. We can't signal the
1521 * poll if the pending async will read all of the data [cf. the
1522 * matching logic in sock_dispatch_asyncs()], but we also don't
1523 * want to spin polling for POLLIN if we're not going to use it. */
1524 if (async_waiting( &sock
->read_q
))
1525 ev
|= POLLIN
| POLLPRI
;
1527 ev
&= ~(POLLIN
| POLLPRI
);
1531 /* Don't ask for POLLIN if we got a hangup. We won't receive more
1532 * data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
1535 if (mask
& AFD_POLL_READ
)
1537 if (mask
& AFD_POLL_OOB
)
1541 /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
1542 if (sock
->state
== SOCK_CONNECTED
&& (mask
& AFD_POLL_HUP
) && !(sock
->reported_events
& AFD_POLL_READ
))
1546 if (async_queued( &sock
->write_q
))
1548 /* As with read asyncs above, clear POLLOUT if we have an alerted
1550 if (async_waiting( &sock
->write_q
))
1555 else if (!sock
->wr_shutdown
&& (mask
& AFD_POLL_WRITE
))
1566 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
1568 return FD_TYPE_SOCKET
;
1571 static void sock_cancel_async( struct fd
*fd
, struct async
*async
)
1573 struct poll_req
*req
;
1575 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1579 if (req
->async
!= async
)
1582 for (i
= 0; i
< req
->count
; i
++)
1584 struct sock
*sock
= req
->sockets
[i
].sock
;
1586 if (sock
->main_poll
== req
)
1587 sock
->main_poll
= NULL
;
1591 async_terminate( async
, STATUS_CANCELLED
);
1594 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
1596 struct sock
*sock
= get_fd_user( fd
);
1598 if (sock
->wr_shutdown_pending
&& list_empty( &sock
->write_q
.queue
))
1600 shutdown( get_unix_fd( sock
->fd
), SHUT_WR
);
1601 sock
->wr_shutdown_pending
= 0;
1604 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1605 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1607 if (queue
!= &sock
->ifchange_q
&& sock
->type
)
1608 sock_reselect( sock
);
1611 static struct fd
*sock_get_fd( struct object
*obj
)
1613 struct sock
*sock
= (struct sock
*)obj
;
1614 return (struct fd
*)grab_object( sock
->fd
);
1617 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
1619 struct sock
*sock
= (struct sock
*)obj
;
1621 if (sock
->obj
.handle_count
== 1) /* last handle */
1623 struct accept_req
*accept_req
, *accept_next
;
1624 struct poll_req
*poll_req
, *poll_next
;
1626 if (sock
->accept_recv_req
)
1627 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
1629 LIST_FOR_EACH_ENTRY_SAFE( accept_req
, accept_next
, &sock
->accept_list
, struct accept_req
, entry
)
1630 async_terminate( accept_req
->async
, STATUS_CANCELLED
);
1632 if (sock
->connect_req
)
1633 async_terminate( sock
->connect_req
->async
, STATUS_CANCELLED
);
1635 LIST_FOR_EACH_ENTRY_SAFE( poll_req
, poll_next
, &poll_list
, struct poll_req
, entry
)
1637 struct iosb
*iosb
= poll_req
->iosb
;
1638 BOOL signaled
= FALSE
;
1641 if (iosb
->status
!= STATUS_PENDING
) continue;
1643 for (i
= 0; i
< poll_req
->count
; ++i
)
1645 if (poll_req
->sockets
[i
].sock
== sock
)
1648 poll_req
->sockets
[i
].flags
= AFD_POLL_CLOSE
;
1649 poll_req
->sockets
[i
].status
= 0;
1653 if (signaled
) complete_async_poll( poll_req
, STATUS_SUCCESS
);
1660 static void sock_destroy( struct object
*obj
)
1662 struct sock
*sock
= (struct sock
*)obj
;
1665 assert( obj
->ops
== &sock_ops
);
1667 /* FIXME: special socket shutdown stuff? */
1669 for (i
= 0; i
< 2; ++i
)
1671 if (sock
->bound_addr
[i
] && --sock
->bound_addr
[i
]->reuse_count
<= 0)
1673 rb_remove( &bound_addresses_tree
, &sock
->bound_addr
[i
]->entry
);
1674 free( sock
->bound_addr
[i
] );
1678 if ( sock
->deferred
)
1679 release_object( sock
->deferred
);
1681 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
1682 sock_release_ifchange( sock
);
1683 free_async_queue( &sock
->read_q
);
1684 free_async_queue( &sock
->write_q
);
1685 free_async_queue( &sock
->ifchange_q
);
1686 free_async_queue( &sock
->accept_q
);
1687 free_async_queue( &sock
->connect_q
);
1688 free_async_queue( &sock
->poll_q
);
1689 if (sock
->event
) release_object( sock
->event
);
1690 if (sock
->fd
) release_object( sock
->fd
);
1693 static struct sock
*create_socket(void)
1697 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
1699 sock
->state
= SOCK_UNCONNECTED
;
1701 sock
->pending_events
= 0;
1702 sock
->reported_events
= 0;
1710 sock
->connect_time
= 0;
1711 sock
->deferred
= NULL
;
1712 sock
->ifchange_obj
= NULL
;
1713 sock
->accept_recv_req
= NULL
;
1714 sock
->connect_req
= NULL
;
1715 sock
->main_poll
= NULL
;
1716 memset( &sock
->addr
, 0, sizeof(sock
->addr
) );
1718 sock
->rd_shutdown
= 0;
1719 sock
->wr_shutdown
= 0;
1720 sock
->wr_shutdown_pending
= 0;
1723 sock
->nonblocking
= 0;
1726 sock
->reuseaddr
= 0;
1727 sock
->exclusiveaddruse
= 0;
1732 sock
->icmp_fixup_data_len
= 0;
1733 sock
->bound_addr
[0] = sock
->bound_addr
[1] = NULL
;
1734 init_async_queue( &sock
->read_q
);
1735 init_async_queue( &sock
->write_q
);
1736 init_async_queue( &sock
->ifchange_q
);
1737 init_async_queue( &sock
->accept_q
);
1738 init_async_queue( &sock
->connect_q
);
1739 init_async_queue( &sock
->poll_q
);
1740 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
1741 list_init( &sock
->accept_list
);
1745 static int get_unix_family( int family
)
1749 case WS_AF_INET
: return AF_INET
;
1750 case WS_AF_INET6
: return AF_INET6
;
1752 case WS_AF_IPX
: return AF_IPX
;
1755 case WS_AF_IRDA
: return AF_IRDA
;
1757 case WS_AF_UNSPEC
: return AF_UNSPEC
;
1762 static int get_unix_type( int type
)
1766 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
1767 case WS_SOCK_RAW
: return SOCK_RAW
;
1768 case WS_SOCK_STREAM
: return SOCK_STREAM
;
1773 static int get_unix_protocol( int protocol
)
1775 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1780 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
1781 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1782 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1783 case WS_IPPROTO_IPV4
: return IPPROTO_IPIP
;
1784 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1785 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1786 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1787 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1792 static void set_dont_fragment( int fd
, int level
, int value
)
1796 if (level
== IPPROTO_IP
)
1799 optname
= IP_DONTFRAG
;
1800 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1801 optname
= IP_MTU_DISCOVER
;
1802 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1809 #ifdef IPV6_DONTFRAG
1810 optname
= IPV6_DONTFRAG
;
1811 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1812 optname
= IPV6_MTU_DISCOVER
;
1813 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1819 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1822 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
)
1824 unsigned int options
= 0;
1825 int sockfd
, unix_type
, unix_family
, unix_protocol
, value
;
1828 unix_family
= get_unix_family( family
);
1829 unix_type
= get_unix_type( type
);
1830 unix_protocol
= get_unix_protocol( protocol
);
1832 if (unix_protocol
< 0)
1834 if (type
&& unix_type
< 0)
1835 set_win32_error( WSAESOCKTNOSUPPORT
);
1837 set_win32_error( WSAEPROTONOSUPPORT
);
1840 if (unix_family
< 0)
1842 if (family
>= 0 && unix_type
< 0)
1843 set_win32_error( WSAESOCKTNOSUPPORT
);
1845 set_win32_error( WSAEAFNOSUPPORT
);
1849 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1852 if (sockfd
== -1 && errno
== EPERM
&& unix_family
== AF_INET
1853 && unix_type
== SOCK_RAW
&& unix_protocol
== IPPROTO_ICMP
)
1855 sockfd
= socket( unix_family
, SOCK_DGRAM
, unix_protocol
);
1860 setsockopt( sockfd
, IPPROTO_IP
, IP_RECVTTL
, (const char *)&val
, sizeof(val
) );
1861 setsockopt( sockfd
, IPPROTO_IP
, IP_RECVTOS
, (const char *)&val
, sizeof(val
) );
1862 setsockopt( sockfd
, IPPROTO_IP
, IP_PKTINFO
, (const char *)&val
, sizeof(val
) );
1869 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1870 else set_win32_error( sock_get_error( errno
));
1873 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1875 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1878 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1881 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1884 /* Should we retrieve val using a getsockopt call and then
1885 * set the modified one? */
1886 val
.ipx_pt
= ipx_type
;
1887 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1892 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1894 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1895 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1896 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1897 else if (unix_type
== SOCK_STREAM
)
1898 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1902 if (unix_family
== AF_INET6
)
1904 static const int enable
= 1;
1905 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1909 len
= sizeof(value
);
1910 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_RCVBUF
, &value
, &len
))
1911 sock
->rcvbuf
= value
;
1913 len
= sizeof(value
);
1914 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_SNDBUF
, &value
, &len
))
1915 sock
->sndbuf
= value
;
1917 sock
->state
= (type
== WS_SOCK_STREAM
? SOCK_UNCONNECTED
: SOCK_CONNECTIONLESS
);
1918 sock
->proto
= protocol
;
1920 sock
->family
= family
;
1922 if (is_tcp_socket( sock
))
1926 setsockopt( sockfd
, SOL_SOCKET
, SO_REUSEADDR
, &reuse
, sizeof(reuse
) );
1931 options
= get_fd_options( sock
->fd
);
1932 release_object( sock
->fd
);
1935 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1940 /* We can't immediately allow caching for a connection-mode socket, since it
1941 * might be accepted into (changing the underlying fd object.) */
1942 if (sock
->type
!= WS_SOCK_STREAM
) allow_fd_caching( sock
->fd
);
1947 /* accepts a socket and inits it */
1948 static int accept_new_fd( struct sock
*sock
)
1951 /* Try to accept(2). We can't be safe that this an already connected socket
1952 * or that accept() is allowed on it. In those cases we will get -1/errno
1955 struct sockaddr saddr
;
1956 socklen_t slen
= sizeof(saddr
);
1957 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1959 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1961 set_error( sock_get_ntstatus( errno
));
1965 /* accept a socket (creates a new fd) */
1966 static struct sock
*accept_socket( struct sock
*sock
)
1968 struct sock
*acceptsock
;
1971 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1973 if ( sock
->deferred
)
1975 acceptsock
= sock
->deferred
;
1976 sock
->deferred
= NULL
;
1980 union unix_sockaddr unix_addr
;
1983 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1984 if (!(acceptsock
= create_socket()))
1990 /* newly created socket gets the same properties of the listening socket */
1991 acceptsock
->state
= SOCK_CONNECTED
;
1992 acceptsock
->bound
= 1;
1993 acceptsock
->nonblocking
= sock
->nonblocking
;
1994 acceptsock
->mask
= sock
->mask
;
1995 acceptsock
->proto
= sock
->proto
;
1996 acceptsock
->type
= sock
->type
;
1997 acceptsock
->family
= sock
->family
;
1998 acceptsock
->window
= sock
->window
;
1999 acceptsock
->message
= sock
->message
;
2000 acceptsock
->connect_time
= current_time
;
2001 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
2002 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
2003 get_fd_options( sock
->fd
) )))
2005 release_object( acceptsock
);
2008 unix_len
= sizeof(unix_addr
);
2009 if (!getsockname( acceptfd
, &unix_addr
.addr
, &unix_len
))
2010 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
2013 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
2014 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
2015 sock_reselect( sock
);
2019 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
2021 union unix_sockaddr unix_addr
;
2026 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
2028 if ( sock
->deferred
)
2030 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
2031 get_fd_options( acceptsock
->fd
) );
2035 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
2037 release_object( sock
->deferred
);
2038 sock
->deferred
= NULL
;
2042 if ((acceptfd
= accept_new_fd( sock
)) == -1)
2045 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
2046 get_fd_options( acceptsock
->fd
) )))
2050 acceptsock
->state
= SOCK_CONNECTED
;
2051 acceptsock
->bound
= 1;
2052 acceptsock
->pending_events
= 0;
2053 acceptsock
->reported_events
= 0;
2054 acceptsock
->proto
= sock
->proto
;
2055 acceptsock
->type
= sock
->type
;
2056 acceptsock
->family
= sock
->family
;
2057 acceptsock
->wparam
= 0;
2058 acceptsock
->deferred
= NULL
;
2059 acceptsock
->connect_time
= current_time
;
2060 fd_copy_completion( acceptsock
->fd
, newfd
);
2061 release_object( acceptsock
->fd
);
2062 acceptsock
->fd
= newfd
;
2064 unix_len
= sizeof(unix_addr
);
2065 if (!getsockname( get_unix_fd( newfd
), &unix_addr
.addr
, &unix_len
))
2066 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
2069 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
2070 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
2071 sock_reselect( sock
);
2078 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
2080 static const int enable
= 1;
2083 if (!(index
= if_nametoindex( name
)))
2086 if (setsockopt( fd
, IPPROTO_IP
, IP_BOUND_IF
, &index
, sizeof(index
) ))
2089 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
2092 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER) && defined(SO_BINDTODEVICE)
2094 struct interface_filter
2096 struct sock_filter iface_memaddr
;
2097 struct sock_filter iface_rule
;
2098 struct sock_filter ip_memaddr
;
2099 struct sock_filter ip_rule
;
2100 struct sock_filter return_keep
;
2101 struct sock_filter return_dump
;
2103 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
2104 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
2105 /sizeof(struct sock_filter)
2106 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
2107 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
2108 /sizeof(struct sock_filter)
2109 # define FILTER_JUMP_NEXT() (u_char)(0)
2110 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
2111 static struct interface_filter generic_interface_filter
=
2113 /* This filter rule allows incoming packets on the specified interface, which works for all
2114 * remotely generated packets and for locally generated broadcast packets. */
2115 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
2116 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
2117 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
2118 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
2119 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
2120 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
2121 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
2122 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
2125 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
2127 struct interface_filter specific_interface_filter
;
2128 struct sock_fprog filter_prog
;
2129 static const int enable
= 1;
2133 if (!setsockopt( fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen( name
) + 1 ))
2136 /* SO_BINDTODEVICE requires NET_CAP_RAW until Linux 5.7. */
2138 fprintf( stderr
, "setsockopt SO_BINDTODEVICE fd %d, name %s failed: %s, falling back to SO_REUSE_ADDR\n",
2139 fd
, name
, strerror( errno
));
2141 if (!(index
= if_nametoindex( name
)))
2144 ifindex
= htonl( index
);
2145 if (setsockopt( fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
) ) < 0)
2148 specific_interface_filter
= generic_interface_filter
;
2149 specific_interface_filter
.iface_rule
.k
= index
;
2150 specific_interface_filter
.ip_rule
.k
= htonl( bind_addr
);
2151 filter_prog
.len
= sizeof(generic_interface_filter
) / sizeof(struct sock_filter
);
2152 filter_prog
.filter
= (struct sock_filter
*)&specific_interface_filter
;
2153 if (setsockopt( fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
) ))
2156 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
2161 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
2167 #endif /* LINUX_BOUND_IF */
2169 /* Take bind() calls on any name corresponding to a local network adapter and
2170 * restrict the given socket to operating only on the specified interface. This
2171 * restriction consists of two components:
2172 * 1) An outgoing packet restriction suggesting the egress interface for all
2174 * 2) An incoming packet restriction dropping packets not meant for the
2176 * If the function succeeds in placing these restrictions, then the name for the
2177 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
2178 * receipt of broadcast packets on the socket. This behavior is only relevant to
2179 * UDP sockets and is needed for applications that expect to be able to receive
2180 * broadcast packets on a socket that is bound to a specific network interface.
2182 static int bind_to_interface( struct sock
*sock
, const struct sockaddr_in
*addr
)
2184 in_addr_t bind_addr
= addr
->sin_addr
.s_addr
;
2185 struct ifaddrs
*ifaddrs
, *ifaddr
;
2186 int fd
= get_unix_fd( sock
->fd
);
2189 if (bind_addr
== htonl( INADDR_ANY
) || bind_addr
== htonl( INADDR_LOOPBACK
))
2191 if (sock
->type
!= WS_SOCK_DGRAM
)
2194 if (getifaddrs( &ifaddrs
) < 0) return 0;
2196 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
2198 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET
2199 && ((struct sockaddr_in
*)ifaddr
->ifa_addr
)->sin_addr
.s_addr
== bind_addr
)
2201 if ((err
= bind_to_iface_name( fd
, bind_addr
, ifaddr
->ifa_name
)) < 0)
2204 fprintf( stderr
, "failed to bind to interface: %s\n", strerror( errno
) );
2209 freeifaddrs( ifaddrs
);
2213 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2214 static unsigned int get_ipv6_interface_index( const struct in6_addr
*addr
)
2216 struct ifaddrs
*ifaddrs
, *ifaddr
;
2218 if (getifaddrs( &ifaddrs
) < 0) return 0;
2220 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
2222 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET6
2223 && !memcmp( &((struct sockaddr_in6
*)ifaddr
->ifa_addr
)->sin6_addr
, addr
, sizeof(*addr
) ))
2225 unsigned int index
= if_nametoindex( ifaddr
->ifa_name
);
2230 fprintf( stderr
, "Unable to look up interface index for %s: %s\n",
2231 ifaddr
->ifa_name
, strerror( errno
) );
2235 freeifaddrs( ifaddrs
);
2240 freeifaddrs( ifaddrs
);
2245 /* return an errno value mapped to a WSA error */
2246 static unsigned int sock_get_error( int err
)
2250 case EINTR
: return WSAEINTR
;
2251 case EBADF
: return WSAEBADF
;
2253 case EACCES
: return WSAEACCES
;
2254 case EFAULT
: return WSAEFAULT
;
2255 case EINVAL
: return WSAEINVAL
;
2256 case EMFILE
: return WSAEMFILE
;
2258 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
2259 case EALREADY
: return WSAEALREADY
;
2260 case ENOTSOCK
: return WSAENOTSOCK
;
2261 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
2262 case EMSGSIZE
: return WSAEMSGSIZE
;
2263 case EPROTOTYPE
: return WSAEPROTOTYPE
;
2264 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
2265 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
2266 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
2267 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
2268 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
2269 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
2270 case EADDRINUSE
: return WSAEADDRINUSE
;
2271 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
2272 case ENETDOWN
: return WSAENETDOWN
;
2273 case ENETUNREACH
: return WSAENETUNREACH
;
2274 case ENETRESET
: return WSAENETRESET
;
2275 case ECONNABORTED
: return WSAECONNABORTED
;
2277 case ECONNRESET
: return WSAECONNRESET
;
2278 case ENOBUFS
: return WSAENOBUFS
;
2279 case EISCONN
: return WSAEISCONN
;
2280 case ENOTCONN
: return WSAENOTCONN
;
2281 case ESHUTDOWN
: return WSAESHUTDOWN
;
2282 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
2283 case ETIMEDOUT
: return WSAETIMEDOUT
;
2284 case ECONNREFUSED
: return WSAECONNREFUSED
;
2285 case ELOOP
: return WSAELOOP
;
2286 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
2287 case EHOSTDOWN
: return WSAEHOSTDOWN
;
2288 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
2289 case ENOTEMPTY
: return WSAENOTEMPTY
;
2291 case EPROCLIM
: return WSAEPROCLIM
;
2294 case EUSERS
: return WSAEUSERS
;
2297 case EDQUOT
: return WSAEDQUOT
;
2300 case ESTALE
: return WSAESTALE
;
2303 case EREMOTE
: return WSAEREMOTE
;
2309 perror("wineserver: sock_get_error() can't map error");
2314 static int sock_get_ntstatus( int err
)
2318 case EBADF
: return STATUS_INVALID_HANDLE
;
2319 case EBUSY
: return STATUS_DEVICE_BUSY
;
2321 case EACCES
: return STATUS_ACCESS_DENIED
;
2322 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
2323 case EINVAL
: return STATUS_INVALID_PARAMETER
;
2325 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
2327 case EWOULDBLOCK
: return STATUS_DEVICE_NOT_READY
;
2328 case EALREADY
: return STATUS_NETWORK_BUSY
;
2329 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
2330 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
2331 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
2332 case EPROTONOSUPPORT
:
2333 case ESOCKTNOSUPPORT
:
2336 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
2337 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
2338 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
2339 case EADDRINUSE
: return STATUS_SHARING_VIOLATION
;
2340 /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
2341 * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
2343 case EADDRNOTAVAIL
: return STATUS_INVALID_ADDRESS_COMPONENT
;
2344 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
2345 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
2346 case ENOTCONN
: return STATUS_INVALID_CONNECTION
;
2347 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
2348 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
2349 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
2350 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
2352 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
2353 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
2354 case EISCONN
: return STATUS_CONNECTION_ACTIVE
;
2356 case 0: return STATUS_SUCCESS
;
2359 perror("wineserver: sock_get_ntstatus() can't map error");
2360 return STATUS_UNSUCCESSFUL
;
2364 static struct accept_req
*alloc_accept_req( struct sock
*sock
, struct sock
*acceptsock
, struct async
*async
,
2365 const struct afd_accept_into_params
*params
)
2367 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
2371 req
->async
= (struct async
*)grab_object( async
);
2372 req
->iosb
= async_get_iosb( async
);
2373 req
->sock
= (struct sock
*)grab_object( sock
);
2374 req
->acceptsock
= acceptsock
;
2375 if (acceptsock
) grab_object( acceptsock
);
2381 req
->recv_len
= params
->recv_len
;
2382 req
->local_len
= params
->local_len
;
2388 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2390 struct sock
*sock
= get_fd_user( fd
);
2393 assert( sock
->obj
.ops
== &sock_ops
);
2395 if (code
!= IOCTL_AFD_WINE_CREATE
&& code
!= IOCTL_AFD_POLL
&& (unix_fd
= get_unix_fd( fd
)) < 0)
2400 case IOCTL_AFD_WINE_CREATE
:
2402 const struct afd_create_params
*params
= get_req_data();
2404 if (get_req_data_size() != sizeof(*params
))
2406 set_error( STATUS_INVALID_PARAMETER
);
2409 init_socket( sock
, params
->family
, params
->type
, params
->protocol
);
2413 case IOCTL_AFD_WINE_ACCEPT
:
2415 struct sock
*acceptsock
;
2416 obj_handle_t handle
;
2418 if (get_reply_max_size() != sizeof(handle
))
2420 set_error( STATUS_BUFFER_TOO_SMALL
);
2424 if (!(acceptsock
= accept_socket( sock
)))
2426 struct accept_req
*req
;
2428 if (sock
->nonblocking
) return;
2429 if (get_error() != STATUS_DEVICE_NOT_READY
) return;
2431 if (!(req
= alloc_accept_req( sock
, NULL
, async
, NULL
))) return;
2432 list_add_tail( &sock
->accept_list
, &req
->entry
);
2434 async_set_completion_callback( async
, free_accept_req
, req
);
2435 queue_async( &sock
->accept_q
, async
);
2436 sock_reselect( sock
);
2437 set_error( STATUS_PENDING
);
2440 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
2441 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
2442 acceptsock
->wparam
= handle
;
2443 sock_reselect( acceptsock
);
2444 release_object( acceptsock
);
2445 set_reply_data( &handle
, sizeof(handle
) );
2449 case IOCTL_AFD_WINE_ACCEPT_INTO
:
2451 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
2452 const struct afd_accept_into_params
*params
= get_req_data();
2453 struct sock
*acceptsock
;
2454 unsigned int remote_len
;
2455 struct accept_req
*req
;
2457 if (get_req_data_size() != sizeof(*params
) ||
2458 get_reply_max_size() < params
->recv_len
||
2459 get_reply_max_size() - params
->recv_len
< params
->local_len
)
2461 set_error( STATUS_BUFFER_TOO_SMALL
);
2465 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
2466 if (remote_len
< sizeof(int))
2468 set_error( STATUS_INVALID_PARAMETER
);
2472 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
2475 if (acceptsock
->accept_recv_req
)
2477 release_object( acceptsock
);
2478 set_error( STATUS_INVALID_PARAMETER
);
2482 if (!(req
= alloc_accept_req( sock
, acceptsock
, async
, params
)))
2484 release_object( acceptsock
);
2487 list_add_tail( &sock
->accept_list
, &req
->entry
);
2488 acceptsock
->accept_recv_req
= req
;
2489 release_object( acceptsock
);
2491 acceptsock
->wparam
= params
->accept_handle
;
2492 async_set_completion_callback( async
, free_accept_req
, req
);
2493 queue_async( &sock
->accept_q
, async
);
2494 sock_reselect( sock
);
2495 set_error( STATUS_PENDING
);
2499 case IOCTL_AFD_LISTEN
:
2501 const struct afd_listen_params
*params
= get_req_data();
2503 if (get_req_data_size() < sizeof(*params
))
2505 set_error( STATUS_INVALID_PARAMETER
);
2511 set_error( STATUS_INVALID_PARAMETER
);
2515 if (listen( unix_fd
, params
->backlog
) < 0)
2517 set_error( sock_get_ntstatus( errno
) );
2521 sock
->state
= SOCK_LISTENING
;
2523 /* a listening socket can no longer be accepted into */
2524 allow_fd_caching( sock
->fd
);
2526 /* we may already be selecting for AFD_POLL_ACCEPT */
2527 sock_reselect( sock
);
2531 case IOCTL_AFD_WINE_CONNECT
:
2533 const struct afd_connect_params
*params
= get_req_data();
2534 const struct WS_sockaddr
*addr
;
2535 union unix_sockaddr unix_addr
;
2536 struct connect_req
*req
;
2540 if (get_req_data_size() < sizeof(*params
) ||
2541 get_req_data_size() - sizeof(*params
) < params
->addr_len
)
2543 set_error( STATUS_BUFFER_TOO_SMALL
);
2546 send_len
= get_req_data_size() - sizeof(*params
) - params
->addr_len
;
2547 addr
= (const struct WS_sockaddr
*)(params
+ 1);
2549 if (!params
->synchronous
&& !sock
->bound
)
2551 set_error( STATUS_INVALID_PARAMETER
);
2555 if (sock
->accept_recv_req
)
2557 set_error( STATUS_INVALID_PARAMETER
);
2561 if (sock
->connect_req
)
2563 set_error( STATUS_INVALID_PARAMETER
);
2567 switch (sock
->state
)
2569 case SOCK_LISTENING
:
2570 set_error( STATUS_INVALID_PARAMETER
);
2573 case SOCK_CONNECTING
:
2574 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2575 * but there's no status code that maps to WSAEALREADY... */
2576 set_error( params
->synchronous
? STATUS_ADDRESS_ALREADY_ASSOCIATED
: STATUS_INVALID_PARAMETER
);
2579 case SOCK_CONNECTED
:
2580 set_error( STATUS_CONNECTION_ACTIVE
);
2583 case SOCK_UNCONNECTED
:
2584 case SOCK_CONNECTIONLESS
:
2588 unix_len
= sockaddr_to_unix( addr
, params
->addr_len
, &unix_addr
);
2591 set_error( STATUS_INVALID_ADDRESS
);
2594 if (unix_addr
.addr
.sa_family
== AF_INET
&& !memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 ))
2595 unix_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_LOOPBACK
);
2597 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2598 if (ret
< 0 && errno
!= EINPROGRESS
)
2600 set_error( sock_get_ntstatus( errno
) );
2604 /* a connected or connecting socket can no longer be accepted into */
2605 allow_fd_caching( sock
->fd
);
2607 unix_len
= sizeof(unix_addr
);
2608 if (!getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
))
2609 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2614 if (sock
->type
!= WS_SOCK_DGRAM
)
2616 sock
->state
= SOCK_CONNECTED
;
2617 sock
->connect_time
= current_time
;
2620 if (!send_len
) return;
2623 if (sock
->type
!= WS_SOCK_DGRAM
)
2624 sock
->state
= SOCK_CONNECTING
;
2626 if (params
->synchronous
&& sock
->nonblocking
)
2628 sock_reselect( sock
);
2629 set_error( STATUS_DEVICE_NOT_READY
);
2633 if (!(req
= mem_alloc( sizeof(*req
) )))
2636 req
->async
= (struct async
*)grab_object( async
);
2637 req
->iosb
= async_get_iosb( async
);
2638 req
->sock
= (struct sock
*)grab_object( sock
);
2639 req
->addr_len
= params
->addr_len
;
2640 req
->send_len
= send_len
;
2641 req
->send_cursor
= 0;
2643 async_set_completion_callback( async
, free_connect_req
, req
);
2644 sock
->connect_req
= req
;
2645 queue_async( &sock
->connect_q
, async
);
2646 sock_reselect( sock
);
2647 set_error( STATUS_PENDING
);
2651 case IOCTL_AFD_WINE_SHUTDOWN
:
2655 if (get_req_data_size() < sizeof(int))
2657 set_error( STATUS_BUFFER_TOO_SMALL
);
2660 how
= *(int *)get_req_data();
2664 set_error( STATUS_INVALID_PARAMETER
);
2668 if (sock
->state
!= SOCK_CONNECTED
&& sock
->state
!= SOCK_CONNECTIONLESS
)
2670 set_error( STATUS_INVALID_CONNECTION
);
2676 sock
->rd_shutdown
= 1;
2678 if (how
!= SD_RECEIVE
)
2680 sock
->wr_shutdown
= 1;
2681 if (list_empty( &sock
->write_q
.queue
))
2682 shutdown( unix_fd
, SHUT_WR
);
2684 sock
->wr_shutdown_pending
= 1;
2689 if (sock
->event
) release_object( sock
->event
);
2693 sock
->nonblocking
= 1;
2696 sock_reselect( sock
);
2700 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE
:
2704 if (get_req_data_size() < sizeof(int))
2706 set_error( STATUS_BUFFER_TOO_SMALL
);
2709 force_async
= *(int *)get_req_data();
2711 if (sock
->nonblocking
&& !force_async
)
2713 set_error( STATUS_DEVICE_NOT_READY
);
2716 if (!sock_get_ifchange( sock
)) return;
2717 queue_async( &sock
->ifchange_q
, async
);
2718 set_error( STATUS_PENDING
);
2722 case IOCTL_AFD_WINE_FIONBIO
:
2723 if (get_req_data_size() < sizeof(int))
2725 set_error( STATUS_BUFFER_TOO_SMALL
);
2728 if (*(int *)get_req_data())
2730 sock
->nonblocking
= 1;
2736 set_error( STATUS_INVALID_PARAMETER
);
2739 sock
->nonblocking
= 0;
2743 case IOCTL_AFD_GET_EVENTS
:
2745 struct afd_get_events_params params
= {0};
2748 if (get_reply_max_size() < sizeof(params
))
2750 set_error( STATUS_INVALID_PARAMETER
);
2754 params
.flags
= sock
->pending_events
& sock
->mask
;
2755 for (i
= 0; i
< ARRAY_SIZE( params
.status
); ++i
)
2756 params
.status
[i
] = sock_get_ntstatus( sock
->errors
[i
] );
2758 sock
->pending_events
&= ~sock
->mask
;
2759 sock_reselect( sock
);
2761 set_reply_data( ¶ms
, sizeof(params
) );
2765 case IOCTL_AFD_EVENT_SELECT
:
2767 struct event
*event
= NULL
;
2768 obj_handle_t event_handle
;
2771 set_async_pending( async
);
2773 if (is_machine_64bit( current
->process
->machine
))
2775 const struct afd_event_select_params_64
*params
= get_req_data();
2777 if (get_req_data_size() < sizeof(*params
))
2779 set_error( STATUS_INVALID_PARAMETER
);
2783 event_handle
= params
->event
;
2784 mask
= params
->mask
;
2788 const struct afd_event_select_params_32
*params
= get_req_data();
2790 if (get_req_data_size() < sizeof(*params
))
2792 set_error( STATUS_INVALID_PARAMETER
);
2796 event_handle
= params
->event
;
2797 mask
= params
->mask
;
2800 if ((event_handle
|| mask
) &&
2801 !(event
= get_event_obj( current
->process
, event_handle
, EVENT_MODIFY_STATE
)))
2803 set_error( STATUS_INVALID_PARAMETER
);
2807 if (sock
->event
) release_object( sock
->event
);
2808 sock
->event
= event
;
2813 sock
->nonblocking
= 1;
2815 sock_reselect( sock
);
2816 /* Explicitly wake the socket up if the mask causes it to become
2817 * signaled. Note that reselecting isn't enough, since we might already
2818 * have had events recorded in sock->reported_events and we don't want
2819 * to select for them again. */
2820 sock_wake_up( sock
);
2825 case IOCTL_AFD_WINE_MESSAGE_SELECT
:
2827 const struct afd_message_select_params
*params
= get_req_data();
2829 if (get_req_data_size() < sizeof(params
))
2831 set_error( STATUS_BUFFER_TOO_SMALL
);
2835 if (sock
->event
) release_object( sock
->event
);
2839 sock
->pending_events
= 0;
2840 sock
->reported_events
= 0;
2843 sock
->mask
= params
->mask
;
2844 sock
->window
= params
->window
;
2845 sock
->message
= params
->message
;
2846 sock
->wparam
= params
->handle
;
2847 sock
->nonblocking
= 1;
2849 sock_reselect( sock
);
2854 case IOCTL_AFD_BIND
:
2856 const struct afd_bind_params
*params
= get_req_data();
2857 union unix_sockaddr unix_addr
, bind_addr
;
2858 data_size_t in_size
;
2862 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2864 if (get_req_data_size() < get_reply_max_size())
2866 set_error( STATUS_BUFFER_TOO_SMALL
);
2869 in_size
= get_req_data_size() - get_reply_max_size();
2870 if (in_size
< offsetof(struct afd_bind_params
, addr
.sa_data
)
2871 || get_reply_max_size() < in_size
- sizeof(int))
2873 set_error( STATUS_INVALID_PARAMETER
);
2879 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED
);
2883 unix_len
= sockaddr_to_unix( ¶ms
->addr
, in_size
- sizeof(int), &unix_addr
);
2886 set_error( STATUS_INVALID_ADDRESS
);
2889 bind_addr
= unix_addr
;
2891 if (unix_addr
.addr
.sa_family
== AF_INET
)
2893 if (!memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 )
2894 || bind_to_interface( sock
, &unix_addr
.in
))
2895 bind_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
2897 else if (unix_addr
.addr
.sa_family
== AF_INET6
)
2899 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2900 /* Windows allows specifying zero to use the default scope. Linux
2901 * interprets it as an interface index and requires that it be
2903 if (!unix_addr
.in6
.sin6_scope_id
)
2904 bind_addr
.in6
.sin6_scope_id
= get_ipv6_interface_index( &unix_addr
.in6
.sin6_addr
);
2908 set_async_pending( async
);
2911 if (sock
->family
== WS_AF_INET6
)
2913 socklen_t len
= sizeof(v6only
);
2915 getsockopt( get_unix_fd(sock
->fd
), IPPROTO_IPV6
, IPV6_V6ONLY
, &v6only
, &len
);
2919 if (check_addr_usage( sock
, &bind_addr
, v6only
))
2922 if (bind( unix_fd
, &bind_addr
.addr
, unix_len
) < 0)
2924 if (errno
== EADDRINUSE
&& sock
->reuseaddr
)
2927 set_error( sock_get_ntstatus( errno
) );
2933 unix_len
= sizeof(bind_addr
);
2934 if (!getsockname( unix_fd
, &bind_addr
.addr
, &unix_len
))
2936 /* store the interface or magic loopback address instead of the
2937 * actual unix address */
2938 if (bind_addr
.addr
.sa_family
== AF_INET
)
2939 bind_addr
.in
.sin_addr
= unix_addr
.in
.sin_addr
;
2940 sock
->addr_len
= sockaddr_from_unix( &bind_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2943 update_addr_usage( sock
, &bind_addr
, v6only
);
2945 if (get_reply_max_size() >= sock
->addr_len
)
2946 set_reply_data( &sock
->addr
, sock
->addr_len
);
2950 case IOCTL_AFD_GETSOCKNAME
:
2953 set_error( STATUS_INVALID_PARAMETER
);
2957 if (get_reply_max_size() < sock
->addr_len
)
2959 set_error( STATUS_BUFFER_TOO_SMALL
);
2963 set_reply_data( &sock
->addr
, sock
->addr_len
);
2966 case IOCTL_AFD_WINE_DEFER
:
2968 const obj_handle_t
*handle
= get_req_data();
2969 struct sock
*acceptsock
;
2971 if (get_req_data_size() < sizeof(*handle
))
2973 set_error( STATUS_BUFFER_TOO_SMALL
);
2977 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, *handle
, 0, &sock_ops
);
2978 if (!acceptsock
) return;
2980 sock
->deferred
= acceptsock
;
2984 case IOCTL_AFD_WINE_GET_INFO
:
2986 struct afd_get_info_params params
;
2988 if (get_reply_max_size() < sizeof(params
))
2990 set_error( STATUS_BUFFER_TOO_SMALL
);
2994 params
.family
= sock
->family
;
2995 params
.type
= sock
->type
;
2996 params
.protocol
= sock
->proto
;
2997 set_reply_data( ¶ms
, sizeof(params
) );
3001 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN
:
3003 int listening
= (sock
->state
== SOCK_LISTENING
);
3005 if (get_reply_max_size() < sizeof(listening
))
3007 set_error( STATUS_BUFFER_TOO_SMALL
);
3011 set_reply_data( &listening
, sizeof(listening
) );
3015 case IOCTL_AFD_WINE_GET_SO_ERROR
:
3020 if (get_reply_max_size() < sizeof(error
))
3022 set_error( STATUS_BUFFER_TOO_SMALL
);
3026 error
= sock_error( sock
);
3029 for (i
= 0; i
< ARRAY_SIZE( sock
->errors
); ++i
)
3031 if (sock
->errors
[i
])
3033 error
= sock
->errors
[i
];
3039 error
= sock_get_error( error
);
3040 set_reply_data( &error
, sizeof(error
) );
3044 case IOCTL_AFD_WINE_GET_SO_RCVBUF
:
3046 int rcvbuf
= sock
->rcvbuf
;
3048 if (get_reply_max_size() < sizeof(rcvbuf
))
3050 set_error( STATUS_BUFFER_TOO_SMALL
);
3054 set_reply_data( &rcvbuf
, sizeof(rcvbuf
) );
3058 case IOCTL_AFD_WINE_SET_SO_RCVBUF
:
3062 if (get_req_data_size() < sizeof(rcvbuf
))
3064 set_error( STATUS_BUFFER_TOO_SMALL
);
3067 rcvbuf
= *(DWORD
*)get_req_data();
3069 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_RCVBUF
, (char *)&rcvbuf
, sizeof(rcvbuf
) ))
3070 sock
->rcvbuf
= rcvbuf
;
3072 set_error( sock_get_ntstatus( errno
) );
3076 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO
:
3078 DWORD rcvtimeo
= sock
->rcvtimeo
;
3080 if (get_reply_max_size() < sizeof(rcvtimeo
))
3082 set_error( STATUS_BUFFER_TOO_SMALL
);
3086 set_reply_data( &rcvtimeo
, sizeof(rcvtimeo
) );
3090 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO
:
3094 if (get_req_data_size() < sizeof(rcvtimeo
))
3096 set_error( STATUS_BUFFER_TOO_SMALL
);
3099 rcvtimeo
= *(DWORD
*)get_req_data();
3101 sock
->rcvtimeo
= rcvtimeo
;
3105 /* BSD socket SO_REUSEADDR is not compatible with winsock semantics. */
3106 case IOCTL_AFD_WINE_SET_SO_REUSEADDR
:
3110 if (get_req_data_size() < sizeof(reuse
))
3112 set_error( STATUS_BUFFER_TOO_SMALL
);
3116 reuse
= *(int *)get_req_data();
3118 if (reuse
&& sock
->exclusiveaddruse
)
3120 set_error( STATUS_INVALID_PARAMETER
);
3124 if (is_tcp_socket( sock
))
3127 ret
= setsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuse
, sizeof(reuse
) );
3129 if (!ret
) ret
= setsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEPORT
, &reuse
, sizeof(reuse
) );
3132 set_error( sock_get_ntstatus( errno
) );
3134 sock
->reuseaddr
= !!reuse
;
3138 case IOCTL_AFD_WINE_SET_SO_EXCLUSIVEADDRUSE
:
3142 if (get_req_data_size() < sizeof(exclusive
))
3144 set_error( STATUS_BUFFER_TOO_SMALL
);
3148 exclusive
= *(int *)get_req_data();
3149 if (exclusive
&& sock
->reuseaddr
)
3151 set_error( STATUS_INVALID_PARAMETER
);
3154 sock
->exclusiveaddruse
= !!exclusive
;
3158 case IOCTL_AFD_WINE_GET_SO_SNDBUF
:
3160 int sndbuf
= sock
->sndbuf
;
3162 if (get_reply_max_size() < sizeof(sndbuf
))
3164 set_error( STATUS_BUFFER_TOO_SMALL
);
3168 set_reply_data( &sndbuf
, sizeof(sndbuf
) );
3172 case IOCTL_AFD_WINE_SET_SO_SNDBUF
:
3176 if (get_req_data_size() < sizeof(sndbuf
))
3178 set_error( STATUS_BUFFER_TOO_SMALL
);
3181 sndbuf
= *(DWORD
*)get_req_data();
3186 /* setsockopt fails if a zero value is passed */
3187 sock
->sndbuf
= sndbuf
;
3192 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_SNDBUF
, (char *)&sndbuf
, sizeof(sndbuf
) ))
3193 sock
->sndbuf
= sndbuf
;
3195 set_error( sock_get_ntstatus( errno
) );
3199 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO
:
3201 DWORD sndtimeo
= sock
->sndtimeo
;
3203 if (get_reply_max_size() < sizeof(sndtimeo
))
3205 set_error( STATUS_BUFFER_TOO_SMALL
);
3209 set_reply_data( &sndtimeo
, sizeof(sndtimeo
) );
3213 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO
:
3217 if (get_req_data_size() < sizeof(sndtimeo
))
3219 set_error( STATUS_BUFFER_TOO_SMALL
);
3222 sndtimeo
= *(DWORD
*)get_req_data();
3224 sock
->sndtimeo
= sndtimeo
;
3228 case IOCTL_AFD_WINE_GET_SO_CONNECT_TIME
:
3232 if (get_reply_max_size() < sizeof(time
))
3234 set_error( STATUS_BUFFER_TOO_SMALL
);
3238 if (sock
->state
== SOCK_CONNECTED
)
3239 time
= (current_time
- sock
->connect_time
) / 10000000;
3241 set_reply_data( &time
, sizeof(time
) );
3245 case IOCTL_AFD_WINE_GET_SO_REUSEADDR
:
3249 if (!get_reply_max_size())
3251 set_error( STATUS_BUFFER_TOO_SMALL
);
3255 reuse
= sock
->reuseaddr
;
3256 set_reply_data( &reuse
, min( sizeof(reuse
), get_reply_max_size() ));
3260 case IOCTL_AFD_WINE_GET_SO_EXCLUSIVEADDRUSE
:
3264 if (!get_reply_max_size())
3266 set_error( STATUS_BUFFER_TOO_SMALL
);
3270 exclusive
= sock
->exclusiveaddruse
;
3271 set_reply_data( &exclusive
, min( sizeof(exclusive
), get_reply_max_size() ));
3275 case IOCTL_AFD_POLL
:
3277 if (get_reply_max_size() < get_req_data_size())
3279 set_error( STATUS_INVALID_PARAMETER
);
3283 if (is_machine_64bit( current
->process
->machine
))
3285 const struct afd_poll_params_64
*params
= get_req_data();
3287 if (get_req_data_size() < sizeof(struct afd_poll_params_64
) ||
3288 get_req_data_size() < offsetof( struct afd_poll_params_64
, sockets
[params
->count
] ))
3290 set_error( STATUS_INVALID_PARAMETER
);
3294 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, params
->sockets
);
3298 const struct afd_poll_params_32
*params
= get_req_data();
3299 struct afd_poll_socket_64
*sockets
;
3302 if (get_req_data_size() < sizeof(struct afd_poll_params_32
) ||
3303 get_req_data_size() < offsetof( struct afd_poll_params_32
, sockets
[params
->count
] ))
3305 set_error( STATUS_INVALID_PARAMETER
);
3309 if (!(sockets
= mem_alloc( params
->count
* sizeof(*sockets
) ))) return;
3310 for (i
= 0; i
< params
->count
; ++i
)
3312 sockets
[i
].socket
= params
->sockets
[i
].socket
;
3313 sockets
[i
].flags
= params
->sockets
[i
].flags
;
3314 sockets
[i
].status
= params
->sockets
[i
].status
;
3317 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, sockets
);
3325 set_error( STATUS_NOT_SUPPORTED
);
3330 static void handle_exclusive_poll(struct poll_req
*req
)
3334 for (i
= 0; i
< req
->count
; ++i
)
3336 struct sock
*sock
= req
->sockets
[i
].sock
;
3337 struct poll_req
*main_poll
= sock
->main_poll
;
3339 if (main_poll
&& main_poll
->exclusive
&& req
->exclusive
)
3341 complete_async_poll( main_poll
, STATUS_SUCCESS
);
3346 sock
->main_poll
= req
;
3350 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
3351 unsigned int count
, const struct afd_poll_socket_64
*sockets
)
3353 BOOL signaled
= FALSE
;
3354 struct poll_req
*req
;
3359 set_error( STATUS_INVALID_PARAMETER
);
3363 if (!(req
= mem_alloc( offsetof( struct poll_req
, sockets
[count
] ) )))
3366 req
->timeout
= NULL
;
3368 if (timeout
&& timeout
!= TIMEOUT_INFINITE
&&
3369 !(req
->timeout
= add_timeout_user( timeout
, async_poll_timeout
, req
)))
3374 req
->orig_timeout
= timeout
;
3376 for (i
= 0; i
< count
; ++i
)
3378 req
->sockets
[i
].sock
= (struct sock
*)get_handle_obj( current
->process
, sockets
[i
].socket
, 0, &sock_ops
);
3379 if (!req
->sockets
[i
].sock
)
3381 for (j
= 0; j
< i
; ++j
) release_object( req
->sockets
[j
].sock
);
3382 if (req
->timeout
) remove_timeout_user( req
->timeout
);
3386 req
->sockets
[i
].handle
= sockets
[i
].socket
;
3387 req
->sockets
[i
].mask
= sockets
[i
].flags
;
3388 req
->sockets
[i
].flags
= 0;
3391 req
->exclusive
= exclusive
;
3393 req
->async
= (struct async
*)grab_object( async
);
3394 req
->iosb
= async_get_iosb( async
);
3396 handle_exclusive_poll(req
);
3398 list_add_tail( &poll_list
, &req
->entry
);
3399 async_set_completion_callback( async
, free_poll_req
, req
);
3400 queue_async( &poll_sock
->poll_q
, async
);
3402 for (i
= 0; i
< count
; ++i
)
3404 struct sock
*sock
= req
->sockets
[i
].sock
;
3405 int mask
= req
->sockets
[i
].mask
;
3406 struct pollfd pollfd
;
3408 pollfd
.fd
= get_unix_fd( sock
->fd
);
3409 pollfd
.events
= poll_flags_from_afd( sock
, mask
);
3410 if (pollfd
.events
>= 0 && poll( &pollfd
, 1, 0 ) >= 0)
3411 sock_poll_event( sock
->fd
, pollfd
.revents
);
3413 /* FIXME: do other error conditions deserve a similar treatment? */
3414 if (sock
->state
!= SOCK_CONNECTING
&& sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] && (mask
& AFD_POLL_CONNECT_ERR
))
3416 req
->sockets
[i
].flags
|= AFD_POLL_CONNECT_ERR
;
3417 req
->sockets
[i
].status
= sock_get_ntstatus( sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] );
3420 if (req
->sockets
[i
].flags
)
3424 if (!timeout
|| signaled
)
3425 complete_async_poll( req
, STATUS_SUCCESS
);
3429 for (i
= 0; i
< req
->count
; ++i
)
3430 sock_reselect( req
->sockets
[i
].sock
);
3431 set_error( STATUS_PENDING
);
3434 #ifdef HAVE_LINUX_RTNETLINK_H
3436 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
3437 static struct object
*ifchange_object
;
3439 static void ifchange_dump( struct object
*obj
, int verbose
);
3440 static struct fd
*ifchange_get_fd( struct object
*obj
);
3441 static void ifchange_destroy( struct object
*obj
);
3443 static int ifchange_get_poll_events( struct fd
*fd
);
3444 static void ifchange_poll_event( struct fd
*fd
, int event
);
3448 struct object obj
; /* object header */
3449 struct fd
*fd
; /* interface change file descriptor */
3450 struct list sockets
; /* list of sockets to send interface change notifications */
3453 static const struct object_ops ifchange_ops
=
3455 sizeof(struct ifchange
), /* size */
3456 &no_type
, /* type */
3457 ifchange_dump
, /* dump */
3458 no_add_queue
, /* add_queue */
3459 NULL
, /* remove_queue */
3460 NULL
, /* signaled */
3461 no_satisfied
, /* satisfied */
3462 no_signal
, /* signal */
3463 ifchange_get_fd
, /* get_fd */
3464 default_map_access
, /* map_access */
3465 default_get_sd
, /* get_sd */
3466 default_set_sd
, /* set_sd */
3467 no_get_full_name
, /* get_full_name */
3468 no_lookup_name
, /* lookup_name */
3469 no_link_name
, /* link_name */
3470 NULL
, /* unlink_name */
3471 no_open_file
, /* open_file */
3472 no_kernel_obj_list
, /* get_kernel_obj_list */
3473 no_close_handle
, /* close_handle */
3474 ifchange_destroy
/* destroy */
3477 static const struct fd_ops ifchange_fd_ops
=
3479 ifchange_get_poll_events
, /* get_poll_events */
3480 ifchange_poll_event
, /* poll_event */
3481 NULL
, /* get_fd_type */
3482 no_fd_read
, /* read */
3483 no_fd_write
, /* write */
3484 no_fd_flush
, /* flush */
3485 no_fd_get_file_info
, /* get_file_info */
3486 no_fd_get_volume_info
, /* get_volume_info */
3487 no_fd_ioctl
, /* ioctl */
3488 NULL
, /* cancel_async */
3489 NULL
, /* queue_async */
3490 NULL
/* reselect_async */
3493 static void ifchange_dump( struct object
*obj
, int verbose
)
3495 assert( obj
->ops
== &ifchange_ops
);
3496 fprintf( stderr
, "Interface change\n" );
3499 static struct fd
*ifchange_get_fd( struct object
*obj
)
3501 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3502 return (struct fd
*)grab_object( ifchange
->fd
);
3505 static void ifchange_destroy( struct object
*obj
)
3507 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3508 assert( obj
->ops
== &ifchange_ops
);
3510 release_object( ifchange
->fd
);
3512 /* reset the global ifchange object so that it will be recreated if it is needed again */
3513 assert( obj
== ifchange_object
);
3514 ifchange_object
= NULL
;
3517 static int ifchange_get_poll_events( struct fd
*fd
)
3522 /* wake up all the sockets waiting for a change notification event */
3523 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
3525 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3526 struct list
*ptr
, *next
;
3527 assert( obj
->ops
== &ifchange_ops
);
3528 assert( obj
== ifchange_object
);
3530 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
3532 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
3534 assert( sock
->ifchange_obj
);
3535 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
3536 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
3540 static void ifchange_poll_event( struct fd
*fd
, int event
)
3542 struct object
*ifchange
= get_fd_user( fd
);
3543 unsigned int status
= STATUS_PENDING
;
3544 char buffer
[PIPE_BUF
];
3547 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
3550 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
3551 return; /* retry when poll() says the socket is ready */
3552 status
= sock_get_ntstatus( errno
);
3556 struct nlmsghdr
*nlh
;
3558 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
3560 if (nlh
->nlmsg_type
== NLMSG_DONE
)
3562 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
3563 status
= STATUS_SUCCESS
;
3566 else status
= STATUS_CANCELLED
;
3568 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
3573 /* we only need one of these interface notification objects, all of the sockets dependent upon
3574 * it will wake up when a notification event occurs */
3575 static struct object
*get_ifchange( void )
3577 #ifdef HAVE_LINUX_RTNETLINK_H
3578 struct ifchange
*ifchange
;
3579 struct sockaddr_nl addr
;
3582 if (ifchange_object
)
3584 /* increment the refcount for each socket that uses the ifchange object */
3585 return grab_object( ifchange_object
);
3588 /* create the socket we need for processing interface change notifications */
3589 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
3592 set_error( sock_get_ntstatus( errno
));
3595 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
3596 memset( &addr
, 0, sizeof(addr
) );
3597 addr
.nl_family
= AF_NETLINK
;
3598 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
3599 /* bind the socket to the special netlink kernel interface */
3600 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
3603 set_error( sock_get_ntstatus( errno
));
3606 if (!(ifchange
= alloc_object( &ifchange_ops
)))
3609 set_error( STATUS_NO_MEMORY
);
3612 list_init( &ifchange
->sockets
);
3613 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
3615 release_object( ifchange
);
3616 set_error( STATUS_NO_MEMORY
);
3619 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
3621 /* the ifchange object is now successfully configured */
3622 ifchange_object
= &ifchange
->obj
;
3623 return &ifchange
->obj
;
3625 set_error( STATUS_NOT_SUPPORTED
);
3630 /* add the socket to the interface change notification list */
3631 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
3633 #ifdef HAVE_LINUX_RTNETLINK_H
3634 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3636 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
3640 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3641 static struct object
*sock_get_ifchange( struct sock
*sock
)
3643 struct object
*ifchange
;
3645 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
3646 return sock
->ifchange_obj
;
3648 if (!(ifchange
= get_ifchange()))
3651 /* add the socket to the ifchange notification list */
3652 ifchange_add_sock( ifchange
, sock
);
3653 sock
->ifchange_obj
= ifchange
;
3657 /* destroy an existing ifchange queue for a specific socket */
3658 static void sock_release_ifchange( struct sock
*sock
)
3660 if (sock
->ifchange_obj
)
3662 list_remove( &sock
->ifchange_entry
);
3663 release_object( sock
->ifchange_obj
);
3664 sock
->ifchange_obj
= NULL
;
3668 static void socket_device_dump( struct object
*obj
, int verbose
);
3669 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3670 unsigned int attr
, struct object
*root
);
3671 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3672 unsigned int sharing
, unsigned int options
);
3674 static const struct object_ops socket_device_ops
=
3676 sizeof(struct object
), /* size */
3677 &device_type
, /* type */
3678 socket_device_dump
, /* dump */
3679 no_add_queue
, /* add_queue */
3680 NULL
, /* remove_queue */
3681 NULL
, /* signaled */
3682 no_satisfied
, /* satisfied */
3683 no_signal
, /* signal */
3684 no_get_fd
, /* get_fd */
3685 default_map_access
, /* map_access */
3686 default_get_sd
, /* get_sd */
3687 default_set_sd
, /* set_sd */
3688 default_get_full_name
, /* get_full_name */
3689 socket_device_lookup_name
, /* lookup_name */
3690 directory_link_name
, /* link_name */
3691 default_unlink_name
, /* unlink_name */
3692 socket_device_open_file
, /* open_file */
3693 no_kernel_obj_list
, /* get_kernel_obj_list */
3694 no_close_handle
, /* close_handle */
3695 no_destroy
/* destroy */
3698 static void socket_device_dump( struct object
*obj
, int verbose
)
3700 fputs( "Socket device\n", stderr
);
3703 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3704 unsigned int attr
, struct object
*root
)
3706 if (name
) name
->len
= 0;
3710 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3711 unsigned int sharing
, unsigned int options
)
3715 if (!(sock
= create_socket())) return NULL
;
3716 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
3718 release_object( sock
);
3724 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
3725 unsigned int attr
, const struct security_descriptor
*sd
)
3727 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
3730 DECL_HANDLER(recv_socket
)
3732 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3733 unsigned int status
= STATUS_PENDING
;
3734 timeout_t timeout
= 0;
3735 struct async
*async
;
3741 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3742 timeout
= (timeout_t
)sock
->rcvtimeo
* -10000;
3744 if (sock
->rd_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3745 else if (!async_queued( &sock
->read_q
))
3747 /* If read_q is not empty, we cannot really tell if the already queued
3748 * asyncs will not consume all available data; if there's no data
3749 * available, the current request won't be immediately satiable.
3751 if ((!req
->force_async
&& sock
->nonblocking
) ||
3752 check_fd_events( sock
->fd
, req
->oob
&& !is_oobinline( sock
) ? POLLPRI
: POLLIN
))
3754 /* Give the client opportunity to complete synchronously.
3755 * If it turns out that the I/O request is not actually immediately satiable,
3756 * the client may then choose to re-queue the async (with STATUS_PENDING).
3758 * Note: If the nonblocking flag is set, we don't poll the socket
3759 * here and always opt for synchronous completion first. This is
3760 * because the application has probably seen POLLIN already from a
3761 * preceding select()/poll() call before it requested to receive
3764 status
= STATUS_ALERTED
;
3768 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3769 status
= STATUS_DEVICE_NOT_READY
;
3771 sock
->pending_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3772 sock
->reported_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3774 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3776 set_error( status
);
3779 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3781 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3782 queue_async( &sock
->read_q
, async
);
3784 /* always reselect; we changed reported_events above */
3785 sock_reselect( sock
);
3787 reply
->wait
= async_handoff( async
, NULL
, 0 );
3788 reply
->options
= get_fd_options( fd
);
3789 reply
->nonblocking
= sock
->nonblocking
;
3790 release_object( async
);
3792 release_object( sock
);
3795 static void send_socket_completion_callback( void *private )
3797 struct send_req
*send_req
= private;
3798 struct iosb
*iosb
= send_req
->iosb
;
3799 struct sock
*sock
= send_req
->sock
;
3801 if (iosb
->status
!= STATUS_SUCCESS
)
3803 /* send() calls only clear and reselect events if unsuccessful. */
3804 sock
->pending_events
&= ~AFD_POLL_WRITE
;
3805 sock
->reported_events
&= ~AFD_POLL_WRITE
;
3806 sock_reselect( sock
);
3809 release_object( iosb
);
3810 release_object( sock
);
3814 DECL_HANDLER(send_socket
)
3816 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3817 unsigned int status
= STATUS_PENDING
;
3818 timeout_t timeout
= 0;
3819 struct async
*async
;
3826 if (sock
->type
== WS_SOCK_DGRAM
&& !sock
->bound
)
3828 union unix_sockaddr unix_addr
;
3830 int unix_fd
= get_unix_fd( fd
);
3832 unix_len
= get_unix_sockaddr_any( &unix_addr
, sock
->family
);
3833 if (bind( unix_fd
, &unix_addr
.addr
, unix_len
) < 0)
3836 if (getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
) >= 0)
3838 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
3841 else if (!bind_errno
) bind_errno
= errno
;
3844 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3845 timeout
= (timeout_t
)sock
->sndtimeo
* -10000;
3847 if (bind_errno
) status
= sock_get_ntstatus( bind_errno
);
3848 else if (sock
->wr_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3849 else if (!async_queued( &sock
->write_q
))
3851 /* If write_q is not empty, we cannot really tell if the already queued
3852 * asyncs will not consume all available space; if there's no space
3853 * available, the current request won't be immediately satiable.
3855 if ((!req
->force_async
&& sock
->nonblocking
) || check_fd_events( sock
->fd
, POLLOUT
))
3857 /* Give the client opportunity to complete synchronously.
3858 * If it turns out that the I/O request is not actually immediately satiable,
3859 * the client may then choose to re-queue the async (with STATUS_PENDING).
3861 * Note: If the nonblocking flag is set, we don't poll the socket
3862 * here and always opt for synchronous completion first. This is
3863 * because the application has probably seen POLLOUT already from a
3864 * preceding select()/poll() call before it requested to send data.
3866 * Furthermore, some applications expect that any send() call on a
3867 * socket that has indicated POLLOUT beforehand never fails with
3868 * WSAEWOULDBLOCK. It's possible that Linux poll() may yield
3869 * POLLOUT on the first call but not the second, even if no send()
3870 * call has been made in the meanwhile. This can happen for a
3871 * number of reasons; for example, TCP fragmentation may consume
3872 * extra buffer space for each packet that has been split out, or
3873 * the TCP/IP networking stack may decide to shrink the send buffer
3874 * due to memory pressure.
3876 status
= STATUS_ALERTED
;
3880 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3881 status
= STATUS_DEVICE_NOT_READY
;
3883 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3885 struct send_req
*send_req
;
3886 struct iosb
*iosb
= async_get_iosb( async
);
3888 if ((send_req
= mem_alloc( sizeof(*send_req
) )))
3890 send_req
->iosb
= (struct iosb
*)grab_object( iosb
);
3891 send_req
->sock
= (struct sock
*)grab_object( sock
);
3892 async_set_completion_callback( async
, send_socket_completion_callback
, send_req
);
3894 else if (status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
)
3895 status
= STATUS_NO_MEMORY
;
3897 release_object( iosb
);
3899 set_error( status
);
3902 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3904 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3906 queue_async( &sock
->write_q
, async
);
3907 sock_reselect( sock
);
3910 reply
->wait
= async_handoff( async
, NULL
, 0 );
3911 reply
->options
= get_fd_options( fd
);
3912 reply
->nonblocking
= sock
->nonblocking
;
3913 release_object( async
);
3915 release_object( sock
);
3918 DECL_HANDLER(socket_send_icmp_id
)
3920 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, 0, &sock_ops
);
3924 if (sock
->icmp_fixup_data_len
== MAX_ICMP_HISTORY_LENGTH
)
3926 memmove( sock
->icmp_fixup_data
, sock
->icmp_fixup_data
+ 1,
3927 sizeof(*sock
->icmp_fixup_data
) * (MAX_ICMP_HISTORY_LENGTH
- 1) );
3928 --sock
->icmp_fixup_data_len
;
3931 sock
->icmp_fixup_data
[sock
->icmp_fixup_data_len
].icmp_id
= req
->icmp_id
;
3932 sock
->icmp_fixup_data
[sock
->icmp_fixup_data_len
].icmp_seq
= req
->icmp_seq
;
3933 ++sock
->icmp_fixup_data_len
;
3935 release_object( sock
);
3938 DECL_HANDLER(socket_get_icmp_id
)
3940 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, 0, &sock_ops
);
3945 for (i
= 0; i
< sock
->icmp_fixup_data_len
; ++i
)
3947 if (sock
->icmp_fixup_data
[i
].icmp_seq
== req
->icmp_seq
)
3949 reply
->icmp_id
= sock
->icmp_fixup_data
[i
].icmp_id
;
3950 --sock
->icmp_fixup_data_len
;
3951 memmove( &sock
->icmp_fixup_data
[i
], &sock
->icmp_fixup_data
[i
+ 1],
3952 (sock
->icmp_fixup_data_len
- i
) * sizeof(*sock
->icmp_fixup_data
) );
3953 release_object( sock
);
3958 set_error( STATUS_NOT_FOUND
);
3959 release_object( sock
);