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
102 #if defined(linux) && !defined(IP_UNICAST_IF)
103 #define IP_UNICAST_IF 50
106 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
110 struct WS_sockaddr addr
;
111 struct WS_sockaddr_in in
;
112 struct WS_sockaddr_in6 in6
;
113 struct WS_sockaddr_ipx ipx
;
117 static struct list poll_list
= LIST_INIT( poll_list
);
124 struct timeout_user
*timeout
;
125 timeout_t orig_timeout
;
143 struct sock
*sock
, *acceptsock
;
145 unsigned int recv_len
, local_len
;
153 unsigned int addr_len
, send_len
, send_cursor
;
162 enum connection_state
173 struct object obj
; /* object header */
174 struct fd
*fd
; /* socket file descriptor */
175 enum connection_state state
; /* connection state */
176 unsigned int mask
; /* event mask */
177 /* pending AFD_POLL_* events which have not yet been reported to the application */
178 unsigned int pending_events
;
179 /* AFD_POLL_* events which have already been reported and should not be
180 * selected for again until reset by a relevant call.
182 * For example, if AFD_POLL_READ is set here and not in pending_events, it
183 * has already been reported and consumed, and we should not report it
184 * again, even if POLLIN is signaled, until it is reset by e.g recv().
186 * If an event has been signaled and not consumed yet, it will be set in
187 * both pending_events and reported_events (as we should only ever report
188 * any event once until it is reset.) */
189 unsigned int reported_events
;
190 unsigned int flags
; /* socket flags */
191 unsigned short proto
; /* socket protocol */
192 unsigned short type
; /* socket type */
193 unsigned short family
; /* socket family */
194 struct event
*event
; /* event object */
195 user_handle_t window
; /* window to send the message to */
196 unsigned int message
; /* message to send */
197 obj_handle_t wparam
; /* message wparam (socket handle) */
198 int errors
[AFD_POLL_BIT_COUNT
]; /* event errors */
199 timeout_t connect_time
;/* time the socket was connected */
200 struct sock
*deferred
; /* socket that waits for a deferred accept */
201 struct async_queue read_q
; /* queue for asynchronous reads */
202 struct async_queue write_q
; /* queue for asynchronous writes */
203 struct async_queue ifchange_q
; /* queue for interface change notifications */
204 struct async_queue accept_q
; /* queue for asynchronous accepts */
205 struct async_queue connect_q
; /* queue for asynchronous connects */
206 struct async_queue poll_q
; /* queue for asynchronous polls */
207 struct object
*ifchange_obj
; /* the interface change notification object */
208 struct list ifchange_entry
; /* entry in ifchange notification list */
209 struct list accept_list
; /* list of pending accept requests */
210 struct accept_req
*accept_recv_req
; /* pending accept-into request which will recv on this socket */
211 struct connect_req
*connect_req
; /* pending connection request */
212 struct poll_req
*main_poll
; /* main poll */
213 union win_sockaddr addr
; /* socket name */
214 int addr_len
; /* socket name length */
215 unsigned int rcvbuf
; /* advisory recv buffer size */
216 unsigned int sndbuf
; /* advisory send buffer size */
217 unsigned int rcvtimeo
; /* receive timeout in ms */
218 unsigned int sndtimeo
; /* send timeout in ms */
219 unsigned int rd_shutdown
: 1; /* is the read end shut down? */
220 unsigned int wr_shutdown
: 1; /* is the write end shut down? */
221 unsigned int wr_shutdown_pending
: 1; /* is a write shutdown pending? */
222 unsigned int hangup
: 1; /* has the read end received a hangup? */
223 unsigned int aborted
: 1; /* did we get a POLLERR or irregular POLLHUP? */
224 unsigned int nonblocking
: 1; /* is the socket nonblocking? */
225 unsigned int bound
: 1; /* is the socket bound? */
228 static void sock_dump( struct object
*obj
, int verbose
);
229 static struct fd
*sock_get_fd( struct object
*obj
);
230 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
231 static void sock_destroy( struct object
*obj
);
232 static struct object
*sock_get_ifchange( struct sock
*sock
);
233 static void sock_release_ifchange( struct sock
*sock
);
235 static int sock_get_poll_events( struct fd
*fd
);
236 static void sock_poll_event( struct fd
*fd
, int event
);
237 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
238 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
239 static void sock_cancel_async( struct fd
*fd
, struct async
*async
);
240 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
);
241 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
243 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
);
244 static struct sock
*accept_socket( struct sock
*sock
);
245 static int sock_get_ntstatus( int err
);
246 static unsigned int sock_get_error( int err
);
247 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
248 unsigned int count
, const struct afd_poll_socket_64
*sockets
);
250 static const struct object_ops sock_ops
=
252 sizeof(struct sock
), /* size */
253 &file_type
, /* type */
254 sock_dump
, /* dump */
255 add_queue
, /* add_queue */
256 remove_queue
, /* remove_queue */
257 default_fd_signaled
, /* signaled */
258 no_satisfied
, /* satisfied */
259 no_signal
, /* signal */
260 sock_get_fd
, /* get_fd */
261 default_map_access
, /* map_access */
262 default_get_sd
, /* get_sd */
263 default_set_sd
, /* set_sd */
264 no_get_full_name
, /* get_full_name */
265 no_lookup_name
, /* lookup_name */
266 no_link_name
, /* link_name */
267 NULL
, /* unlink_name */
268 no_open_file
, /* open_file */
269 no_kernel_obj_list
, /* get_kernel_obj_list */
270 sock_close_handle
, /* close_handle */
271 sock_destroy
/* destroy */
274 static const struct fd_ops sock_fd_ops
=
276 sock_get_poll_events
, /* get_poll_events */
277 sock_poll_event
, /* poll_event */
278 sock_get_fd_type
, /* get_fd_type */
279 no_fd_read
, /* read */
280 no_fd_write
, /* write */
281 no_fd_flush
, /* flush */
282 default_fd_get_file_info
, /* get_file_info */
283 no_fd_get_volume_info
, /* get_volume_info */
284 sock_ioctl
, /* ioctl */
285 sock_cancel_async
, /* cancel_async */
286 sock_queue_async
, /* queue_async */
287 sock_reselect_async
/* reselect_async */
292 struct sockaddr addr
;
293 struct sockaddr_in in
;
294 struct sockaddr_in6 in6
;
296 struct sockaddr_ipx ipx
;
299 struct sockaddr_irda irda
;
303 static int sockaddr_from_unix( const union unix_sockaddr
*uaddr
, struct WS_sockaddr
*wsaddr
, socklen_t wsaddrlen
)
305 memset( wsaddr
, 0, wsaddrlen
);
307 switch (uaddr
->addr
.sa_family
)
311 struct WS_sockaddr_in win
= {0};
313 if (wsaddrlen
< sizeof(win
)) return -1;
314 win
.sin_family
= WS_AF_INET
;
315 win
.sin_port
= uaddr
->in
.sin_port
;
316 memcpy( &win
.sin_addr
, &uaddr
->in
.sin_addr
, sizeof(win
.sin_addr
) );
317 memcpy( wsaddr
, &win
, sizeof(win
) );
323 struct WS_sockaddr_in6 win
= {0};
325 if (wsaddrlen
< sizeof(win
)) return -1;
326 win
.sin6_family
= WS_AF_INET6
;
327 win
.sin6_port
= uaddr
->in6
.sin6_port
;
328 win
.sin6_flowinfo
= uaddr
->in6
.sin6_flowinfo
;
329 memcpy( &win
.sin6_addr
, &uaddr
->in6
.sin6_addr
, sizeof(win
.sin6_addr
) );
330 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
331 win
.sin6_scope_id
= uaddr
->in6
.sin6_scope_id
;
333 memcpy( wsaddr
, &win
, sizeof(win
) );
340 struct WS_sockaddr_ipx win
= {0};
342 if (wsaddrlen
< sizeof(win
)) return -1;
343 win
.sa_family
= WS_AF_IPX
;
344 memcpy( win
.sa_netnum
, &uaddr
->ipx
.sipx_network
, sizeof(win
.sa_netnum
) );
345 memcpy( win
.sa_nodenum
, &uaddr
->ipx
.sipx_node
, sizeof(win
.sa_nodenum
) );
346 win
.sa_socket
= uaddr
->ipx
.sipx_port
;
347 memcpy( wsaddr
, &win
, sizeof(win
) );
357 if (wsaddrlen
< sizeof(win
)) return -1;
358 win
.irdaAddressFamily
= WS_AF_IRDA
;
359 memcpy( win
.irdaDeviceID
, &uaddr
->irda
.sir_addr
, sizeof(win
.irdaDeviceID
) );
360 if (uaddr
->irda
.sir_lsap_sel
!= LSAP_ANY
)
361 snprintf( win
.irdaServiceName
, sizeof(win
.irdaServiceName
), "LSAP-SEL%u", uaddr
->irda
.sir_lsap_sel
);
363 memcpy( win
.irdaServiceName
, uaddr
->irda
.sir_name
, sizeof(win
.irdaServiceName
) );
364 memcpy( wsaddr
, &win
, sizeof(win
) );
378 static socklen_t
sockaddr_to_unix( const struct WS_sockaddr
*wsaddr
, int wsaddrlen
, union unix_sockaddr
*uaddr
)
380 memset( uaddr
, 0, sizeof(*uaddr
) );
382 switch (wsaddr
->sa_family
)
386 struct WS_sockaddr_in win
= {0};
388 if (wsaddrlen
< sizeof(win
)) return 0;
389 memcpy( &win
, wsaddr
, sizeof(win
) );
390 uaddr
->in
.sin_family
= AF_INET
;
391 uaddr
->in
.sin_port
= win
.sin_port
;
392 memcpy( &uaddr
->in
.sin_addr
, &win
.sin_addr
, sizeof(win
.sin_addr
) );
393 return sizeof(uaddr
->in
);
398 struct WS_sockaddr_in6 win
= {0};
400 if (wsaddrlen
< sizeof(win
)) return 0;
401 memcpy( &win
, wsaddr
, sizeof(win
) );
402 uaddr
->in6
.sin6_family
= AF_INET6
;
403 uaddr
->in6
.sin6_port
= win
.sin6_port
;
404 uaddr
->in6
.sin6_flowinfo
= win
.sin6_flowinfo
;
405 memcpy( &uaddr
->in6
.sin6_addr
, &win
.sin6_addr
, sizeof(win
.sin6_addr
) );
406 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
407 uaddr
->in6
.sin6_scope_id
= win
.sin6_scope_id
;
409 return sizeof(uaddr
->in6
);
415 struct WS_sockaddr_ipx win
= {0};
417 if (wsaddrlen
< sizeof(win
)) return 0;
418 memcpy( &win
, wsaddr
, sizeof(win
) );
419 uaddr
->ipx
.sipx_family
= AF_IPX
;
420 memcpy( &uaddr
->ipx
.sipx_network
, win
.sa_netnum
, sizeof(win
.sa_netnum
) );
421 memcpy( &uaddr
->ipx
.sipx_node
, win
.sa_nodenum
, sizeof(win
.sa_nodenum
) );
422 uaddr
->ipx
.sipx_port
= win
.sa_socket
;
423 return sizeof(uaddr
->ipx
);
430 SOCKADDR_IRDA win
= {0};
431 unsigned int lsap_sel
;
433 if (wsaddrlen
< sizeof(win
)) return 0;
434 memcpy( &win
, wsaddr
, sizeof(win
) );
435 uaddr
->irda
.sir_family
= AF_IRDA
;
436 if (sscanf( win
.irdaServiceName
, "LSAP-SEL%u", &lsap_sel
) == 1)
437 uaddr
->irda
.sir_lsap_sel
= lsap_sel
;
440 uaddr
->irda
.sir_lsap_sel
= LSAP_ANY
;
441 memcpy( uaddr
->irda
.sir_name
, win
.irdaServiceName
, sizeof(win
.irdaServiceName
) );
443 memcpy( &uaddr
->irda
.sir_addr
, win
.irdaDeviceID
, sizeof(win
.irdaDeviceID
) );
444 return sizeof(uaddr
->irda
);
451 default: /* likely an ipv4 address */
452 case sizeof(struct WS_sockaddr_in
):
453 return sizeof(uaddr
->in
);
456 case sizeof(struct WS_sockaddr_ipx
):
457 return sizeof(uaddr
->ipx
);
461 case sizeof(SOCKADDR_IRDA
):
462 return sizeof(uaddr
->irda
);
465 case sizeof(struct WS_sockaddr_in6
):
466 return sizeof(uaddr
->in6
);
474 static socklen_t
get_unix_sockaddr_any( union unix_sockaddr
*uaddr
, int ws_family
)
476 memset( uaddr
, 0, sizeof(*uaddr
) );
480 uaddr
->in
.sin_family
= AF_INET
;
481 return sizeof(uaddr
->in
);
483 uaddr
->in6
.sin6_family
= AF_INET6
;
484 return sizeof(uaddr
->in6
);
487 uaddr
->ipx
.sipx_family
= AF_IPX
;
488 return sizeof(uaddr
->ipx
);
492 uaddr
->irda
.sir_family
= AF_IRDA
;
493 return sizeof(uaddr
->irda
);
500 /* some events are generated at the same time but must be sent in a particular
501 * order (e.g. CONNECT must be sent before READ) */
502 static const enum afd_poll_bit event_bitorder
[] =
504 AFD_POLL_BIT_CONNECT
,
505 AFD_POLL_BIT_CONNECT_ERR
,
516 SOCK_SHUTDOWN_ERROR
= -1,
517 SOCK_SHUTDOWN_EOF
= 0,
518 SOCK_SHUTDOWN_POLLHUP
= 1
521 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
523 static sock_shutdown_t
sock_check_pollhup(void)
525 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
530 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
531 if ( shutdown( fd
[0], 1 ) ) goto out
;
537 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
538 n
= poll( &pfd
, 1, 1 );
539 if ( n
!= 1 ) goto out
; /* error or timeout */
540 if ( pfd
.revents
& POLLHUP
)
541 ret
= SOCK_SHUTDOWN_POLLHUP
;
542 else if ( pfd
.revents
& POLLIN
&&
543 read( fd
[1], &dummy
, 1 ) == 0 )
544 ret
= SOCK_SHUTDOWN_EOF
;
554 sock_shutdown_type
= sock_check_pollhup();
556 switch ( sock_shutdown_type
)
558 case SOCK_SHUTDOWN_EOF
:
559 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
561 case SOCK_SHUTDOWN_POLLHUP
:
562 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
565 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
566 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
570 static int sock_reselect( struct sock
*sock
)
572 int ev
= sock_get_poll_events( sock
->fd
);
575 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
577 set_fd_events( sock
->fd
, ev
);
581 static unsigned int afd_poll_flag_to_win32( unsigned int flags
)
583 static const unsigned int map
[] =
587 FD_WRITE
, /* WRITE */
589 FD_CLOSE
, /* RESET */
591 FD_CONNECT
, /* CONNECT */
592 FD_ACCEPT
, /* ACCEPT */
593 FD_CONNECT
, /* CONNECT_ERR */
596 unsigned int i
, ret
= 0;
598 for (i
= 0; i
< ARRAY_SIZE(map
); ++i
)
600 if (flags
& (1 << i
)) ret
|= map
[i
];
606 /* wake anybody waiting on the socket event or send the associated message */
607 static void sock_wake_up( struct sock
*sock
)
609 unsigned int events
= sock
->pending_events
& sock
->mask
;
614 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
616 set_event( sock
->event
);
620 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
621 for (i
= 0; i
< ARRAY_SIZE(event_bitorder
); i
++)
623 enum afd_poll_bit event
= event_bitorder
[i
];
624 if (events
& (1 << event
))
626 lparam_t lparam
= afd_poll_flag_to_win32(1 << event
) | (sock_get_error( sock
->errors
[event
] ) << 16);
627 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
630 sock
->pending_events
= 0;
631 sock_reselect( sock
);
635 static inline int sock_error( struct fd
*fd
)
637 unsigned int optval
= 0;
638 socklen_t optlen
= sizeof(optval
);
640 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
644 static void free_accept_req( void *private )
646 struct accept_req
*req
= private;
647 list_remove( &req
->entry
);
650 req
->acceptsock
->accept_recv_req
= NULL
;
651 release_object( req
->acceptsock
);
653 release_object( req
->async
);
654 release_object( req
->iosb
);
655 release_object( req
->sock
);
659 static void fill_accept_output( struct accept_req
*req
)
661 const data_size_t out_size
= req
->iosb
->out_size
;
662 struct async
*async
= req
->async
;
663 union unix_sockaddr unix_addr
;
664 struct WS_sockaddr
*win_addr
;
665 unsigned int remote_len
;
671 if (!(out_data
= mem_alloc( out_size
)))
673 async_terminate( async
, get_error() );
677 fd
= get_unix_fd( req
->acceptsock
->fd
);
679 if (req
->recv_len
&& (size
= recv( fd
, out_data
, req
->recv_len
, 0 )) < 0)
681 if (!req
->accepted
&& errno
== EWOULDBLOCK
)
684 sock_reselect( req
->acceptsock
);
688 async_terminate( async
, sock_get_ntstatus( errno
) );
695 if (req
->local_len
< sizeof(int))
697 async_terminate( async
, STATUS_BUFFER_TOO_SMALL
);
702 unix_len
= sizeof(unix_addr
);
703 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ sizeof(int));
704 if (getsockname( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
705 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, req
->local_len
- sizeof(int) )) < 0)
707 async_terminate( async
, sock_get_ntstatus( errno
) );
711 memcpy( out_data
+ req
->recv_len
, &win_len
, sizeof(int) );
714 unix_len
= sizeof(unix_addr
);
715 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ req
->local_len
+ sizeof(int));
716 remote_len
= out_size
- req
->recv_len
- req
->local_len
;
717 if (getpeername( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
718 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, remote_len
- sizeof(int) )) < 0)
720 async_terminate( async
, sock_get_ntstatus( errno
) );
724 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
726 async_request_complete( req
->async
, STATUS_SUCCESS
, size
, out_size
, out_data
);
729 static void complete_async_accept( struct sock
*sock
, struct accept_req
*req
)
731 struct sock
*acceptsock
= req
->acceptsock
;
732 struct async
*async
= req
->async
;
734 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
738 if (!accept_into_socket( sock
, acceptsock
))
740 async_terminate( async
, get_error() );
743 fill_accept_output( req
);
749 if (!(acceptsock
= accept_socket( sock
)))
751 async_terminate( async
, get_error() );
754 handle
= alloc_handle_no_access_check( async_get_thread( async
)->process
, &acceptsock
->obj
,
755 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
756 acceptsock
->wparam
= handle
;
757 sock_reselect( acceptsock
);
758 release_object( acceptsock
);
761 async_terminate( async
, get_error() );
765 async_request_complete_alloc( req
->async
, STATUS_SUCCESS
, 0, sizeof(handle
), &handle
);
769 static void complete_async_accept_recv( struct accept_req
*req
)
771 if (debug_level
) fprintf( stderr
, "completing accept recv request for socket %p\n", req
->acceptsock
);
773 assert( req
->recv_len
);
775 fill_accept_output( req
);
778 static void free_connect_req( void *private )
780 struct connect_req
*req
= private;
782 req
->sock
->connect_req
= NULL
;
783 release_object( req
->async
);
784 release_object( req
->iosb
);
785 release_object( req
->sock
);
789 static void complete_async_connect( struct sock
*sock
)
791 struct connect_req
*req
= sock
->connect_req
;
792 const char *in_buffer
;
796 if (debug_level
) fprintf( stderr
, "completing connect request for socket %p\n", sock
);
798 sock
->state
= SOCK_CONNECTED
;
802 async_terminate( req
->async
, STATUS_SUCCESS
);
806 in_buffer
= (const char *)req
->iosb
->in_data
+ sizeof(struct afd_connect_params
) + req
->addr_len
;
807 len
= req
->send_len
- req
->send_cursor
;
809 ret
= send( get_unix_fd( sock
->fd
), in_buffer
+ req
->send_cursor
, len
, 0 );
810 if (ret
< 0 && errno
!= EWOULDBLOCK
)
811 async_terminate( req
->async
, sock_get_ntstatus( errno
) );
813 async_request_complete( req
->async
, STATUS_SUCCESS
, req
->send_len
, 0, NULL
);
815 req
->send_cursor
+= ret
;
818 static void free_poll_req( void *private )
820 struct poll_req
*req
= private;
823 if (req
->timeout
) remove_timeout_user( req
->timeout
);
825 for (i
= 0; i
< req
->count
; ++i
)
826 release_object( req
->sockets
[i
].sock
);
827 release_object( req
->async
);
828 release_object( req
->iosb
);
829 list_remove( &req
->entry
);
833 static int is_oobinline( struct sock
*sock
)
836 socklen_t len
= sizeof(oobinline
);
837 return !getsockopt( get_unix_fd( sock
->fd
), SOL_SOCKET
, SO_OOBINLINE
, (char *)&oobinline
, &len
) && oobinline
;
840 static int get_poll_flags( struct sock
*sock
, int event
)
844 /* A connection-mode socket which has never been connected does not return
845 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
846 if (sock
->state
== SOCK_UNCONNECTED
)
847 event
&= ~(POLLOUT
| POLLHUP
);
851 if (sock
->state
== SOCK_LISTENING
)
852 flags
|= AFD_POLL_ACCEPT
;
854 flags
|= AFD_POLL_READ
;
857 flags
|= is_oobinline( sock
) ? AFD_POLL_READ
: AFD_POLL_OOB
;
859 flags
|= AFD_POLL_WRITE
;
860 if (sock
->state
== SOCK_CONNECTED
)
861 flags
|= AFD_POLL_CONNECT
;
863 flags
|= AFD_POLL_HUP
;
865 flags
|= AFD_POLL_CONNECT_ERR
;
870 static void complete_async_poll( struct poll_req
*req
, unsigned int status
)
872 unsigned int i
, signaled_count
= 0;
874 for (i
= 0; i
< req
->count
; ++i
)
876 struct sock
*sock
= req
->sockets
[i
].sock
;
878 if (sock
->main_poll
== req
)
879 sock
->main_poll
= NULL
;
884 for (i
= 0; i
< req
->count
; ++i
)
886 if (req
->sockets
[i
].flags
)
891 if (is_machine_64bit( async_get_thread( req
->async
)->process
->machine
))
893 size_t output_size
= offsetof( struct afd_poll_params_64
, sockets
[signaled_count
] );
894 struct afd_poll_params_64
*output
;
896 if (!(output
= mem_alloc( output_size
)))
898 async_terminate( req
->async
, get_error() );
901 memset( output
, 0, output_size
);
902 output
->timeout
= req
->orig_timeout
;
903 output
->exclusive
= req
->exclusive
;
904 for (i
= 0; i
< req
->count
; ++i
)
906 if (!req
->sockets
[i
].flags
) continue;
907 output
->sockets
[output
->count
].socket
= req
->sockets
[i
].handle
;
908 output
->sockets
[output
->count
].flags
= req
->sockets
[i
].flags
;
909 output
->sockets
[output
->count
].status
= req
->sockets
[i
].status
;
912 assert( output
->count
== signaled_count
);
914 async_request_complete( req
->async
, status
, output_size
, output_size
, output
);
918 size_t output_size
= offsetof( struct afd_poll_params_32
, sockets
[signaled_count
] );
919 struct afd_poll_params_32
*output
;
921 if (!(output
= mem_alloc( output_size
)))
923 async_terminate( req
->async
, get_error() );
926 memset( output
, 0, output_size
);
927 output
->timeout
= req
->orig_timeout
;
928 output
->exclusive
= req
->exclusive
;
929 for (i
= 0; i
< req
->count
; ++i
)
931 if (!req
->sockets
[i
].flags
) continue;
932 output
->sockets
[output
->count
].socket
= req
->sockets
[i
].handle
;
933 output
->sockets
[output
->count
].flags
= req
->sockets
[i
].flags
;
934 output
->sockets
[output
->count
].status
= req
->sockets
[i
].status
;
937 assert( output
->count
== signaled_count
);
939 async_request_complete( req
->async
, status
, output_size
, output_size
, output
);
943 static void complete_async_polls( struct sock
*sock
, int event
, int error
)
945 int flags
= get_poll_flags( sock
, event
);
946 struct poll_req
*req
, *next
;
948 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &poll_list
, struct poll_req
, entry
)
952 if (req
->iosb
->status
!= STATUS_PENDING
) continue;
954 for (i
= 0; i
< req
->count
; ++i
)
956 if (req
->sockets
[i
].sock
!= sock
) continue;
957 if (!(req
->sockets
[i
].mask
& flags
)) continue;
960 fprintf( stderr
, "completing poll for socket %p, wanted %#x got %#x\n",
961 sock
, req
->sockets
[i
].mask
, flags
);
963 req
->sockets
[i
].flags
= req
->sockets
[i
].mask
& flags
;
964 req
->sockets
[i
].status
= sock_get_ntstatus( error
);
966 complete_async_poll( req
, STATUS_SUCCESS
);
972 static void async_poll_timeout( void *private )
974 struct poll_req
*req
= private;
978 if (req
->iosb
->status
!= STATUS_PENDING
) return;
980 complete_async_poll( req
, STATUS_TIMEOUT
);
983 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
985 if (event
& (POLLIN
| POLLPRI
))
987 struct accept_req
*req
;
989 LIST_FOR_EACH_ENTRY( req
, &sock
->accept_list
, struct accept_req
, entry
)
991 if (req
->iosb
->status
== STATUS_PENDING
&& !req
->accepted
)
993 complete_async_accept( sock
, req
);
998 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
999 complete_async_accept_recv( sock
->accept_recv_req
);
1002 if ((event
& POLLOUT
) && sock
->connect_req
&& sock
->connect_req
->iosb
->status
== STATUS_PENDING
)
1003 complete_async_connect( sock
);
1005 if (event
& (POLLIN
| POLLPRI
) && async_waiting( &sock
->read_q
))
1007 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
1008 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
1009 event
&= ~(POLLIN
| POLLPRI
);
1012 if (event
& POLLOUT
&& async_waiting( &sock
->write_q
))
1014 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
1015 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
1019 if (event
& (POLLERR
| POLLHUP
))
1021 int status
= sock_get_ntstatus( error
);
1022 struct accept_req
*req
, *next
;
1024 if (sock
->rd_shutdown
|| sock
->hangup
)
1025 async_wake_up( &sock
->read_q
, status
);
1026 if (sock
->wr_shutdown
)
1027 async_wake_up( &sock
->write_q
, status
);
1029 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
1031 if (req
->iosb
->status
== STATUS_PENDING
)
1032 async_terminate( req
->async
, status
);
1035 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
1036 async_terminate( sock
->accept_recv_req
->async
, status
);
1038 if (sock
->connect_req
)
1039 async_terminate( sock
->connect_req
->async
, status
);
1045 static void post_socket_event( struct sock
*sock
, enum afd_poll_bit event_bit
, int error
)
1047 unsigned int event
= (1 << event_bit
);
1049 if (!(sock
->reported_events
& event
))
1051 sock
->pending_events
|= event
;
1052 sock
->reported_events
|= event
;
1053 sock
->errors
[event_bit
] = error
;
1057 static void sock_dispatch_events( struct sock
*sock
, enum connection_state prevstate
, int event
, int error
)
1061 case SOCK_UNCONNECTED
:
1064 case SOCK_CONNECTING
:
1065 if (event
& POLLOUT
)
1067 post_socket_event( sock
, AFD_POLL_BIT_CONNECT
, 0 );
1068 sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] = 0;
1070 if (event
& (POLLERR
| POLLHUP
))
1071 post_socket_event( sock
, AFD_POLL_BIT_CONNECT_ERR
, error
);
1074 case SOCK_LISTENING
:
1075 if (event
& (POLLIN
| POLLERR
| POLLHUP
))
1076 post_socket_event( sock
, AFD_POLL_BIT_ACCEPT
, error
);
1079 case SOCK_CONNECTED
:
1080 case SOCK_CONNECTIONLESS
:
1082 post_socket_event( sock
, AFD_POLL_BIT_READ
, 0 );
1084 if (event
& POLLOUT
)
1085 post_socket_event( sock
, AFD_POLL_BIT_WRITE
, 0 );
1087 if (event
& POLLPRI
)
1088 post_socket_event( sock
, AFD_POLL_BIT_OOB
, 0 );
1090 if (event
& (POLLERR
| POLLHUP
))
1091 post_socket_event( sock
, AFD_POLL_BIT_HUP
, error
);
1095 sock_wake_up( sock
);
1098 static void sock_poll_event( struct fd
*fd
, int event
)
1100 struct sock
*sock
= get_fd_user( fd
);
1101 int hangup_seen
= 0;
1102 enum connection_state prevstate
= sock
->state
;
1105 assert( sock
->obj
.ops
== &sock_ops
);
1107 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
1109 /* we may change event later, remove from loop here */
1110 if (event
& (POLLERR
|POLLHUP
)) set_fd_events( sock
->fd
, -1 );
1112 switch (sock
->state
)
1114 case SOCK_UNCONNECTED
:
1117 case SOCK_CONNECTING
:
1118 if (event
& (POLLERR
|POLLHUP
))
1120 sock
->state
= SOCK_UNCONNECTED
;
1122 error
= sock_error( fd
);
1124 else if (event
& POLLOUT
)
1126 sock
->state
= SOCK_CONNECTED
;
1127 sock
->connect_time
= current_time
;
1131 case SOCK_LISTENING
:
1132 if (event
& (POLLERR
|POLLHUP
))
1133 error
= sock_error( fd
);
1136 case SOCK_CONNECTED
:
1137 case SOCK_CONNECTIONLESS
:
1138 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
1143 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
1144 * has been closed, so we need to check for it explicitly here */
1145 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
1154 /* EAGAIN can happen if an async recv() falls between the server's poll()
1155 call and the invocation of this routine */
1156 if ( errno
!= EAGAIN
)
1161 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
1166 if (hangup_seen
|| (sock_shutdown_type
== SOCK_SHUTDOWN_POLLHUP
&& (event
& POLLHUP
)))
1170 else if (event
& (POLLHUP
| POLLERR
))
1175 fprintf( stderr
, "socket %p aborted by error %d, event %#x\n", sock
, error
, event
);
1183 complete_async_polls( sock
, event
, error
);
1185 event
= sock_dispatch_asyncs( sock
, event
, error
);
1186 sock_dispatch_events( sock
, prevstate
, event
, error
);
1188 sock_reselect( sock
);
1191 static void sock_dump( struct object
*obj
, int verbose
)
1193 struct sock
*sock
= (struct sock
*)obj
;
1194 assert( obj
->ops
== &sock_ops
);
1195 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
1196 sock
->fd
, sock
->state
,
1197 sock
->mask
, sock
->pending_events
, sock
->reported_events
);
1200 static int poll_flags_from_afd( struct sock
*sock
, int flags
)
1204 /* A connection-mode socket which has never been connected does
1205 * not return write or hangup events, but Linux returns
1206 * POLLOUT | POLLHUP. */
1207 if (sock
->state
== SOCK_UNCONNECTED
)
1210 if (flags
& (AFD_POLL_READ
| AFD_POLL_ACCEPT
))
1212 if ((flags
& AFD_POLL_HUP
) && sock
->type
== WS_SOCK_STREAM
)
1214 if (flags
& AFD_POLL_OOB
)
1215 ev
|= is_oobinline( sock
) ? POLLIN
: POLLPRI
;
1216 if (flags
& AFD_POLL_WRITE
)
1222 static int sock_get_poll_events( struct fd
*fd
)
1224 struct sock
*sock
= get_fd_user( fd
);
1225 unsigned int mask
= sock
->mask
& ~sock
->reported_events
;
1226 struct poll_req
*req
;
1229 assert( sock
->obj
.ops
== &sock_ops
);
1231 if (!sock
->type
) /* not initialized yet */
1234 switch (sock
->state
)
1236 case SOCK_UNCONNECTED
:
1237 /* A connection-mode Windows socket which has never been connected does
1238 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1239 * need to return -1 here, to prevent the socket from being polled on at
1243 case SOCK_CONNECTING
:
1246 case SOCK_LISTENING
:
1247 if (!list_empty( &sock
->accept_list
) || (mask
& AFD_POLL_ACCEPT
))
1251 case SOCK_CONNECTED
:
1252 case SOCK_CONNECTIONLESS
:
1253 if (sock
->hangup
&& sock
->wr_shutdown
&& !sock
->wr_shutdown_pending
)
1255 /* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
1256 * if both the socket and its peer are SHUT_WR.
1258 * We don't use SHUT_RD, so we can only encounter this in the latter
1259 * case. In that case there can't be any pending read requests (they
1260 * would have already been completed with a length of zero), the
1261 * above condition ensures that we don't have any pending write
1262 * requests, and nothing that can change about the socket state that
1263 * would complete a pending poll request. */
1270 if (sock
->accept_recv_req
)
1274 else if (async_queued( &sock
->read_q
))
1276 if (async_waiting( &sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
1280 /* Don't ask for POLLIN if we got a hangup. We won't receive more
1281 * data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
1284 if (mask
& AFD_POLL_READ
)
1286 if (mask
& AFD_POLL_OOB
)
1290 /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
1291 if (sock
->state
== SOCK_CONNECTED
&& (mask
& AFD_POLL_HUP
) && !(sock
->reported_events
& AFD_POLL_READ
))
1295 if (async_queued( &sock
->write_q
))
1297 if (async_waiting( &sock
->write_q
)) ev
|= POLLOUT
;
1299 else if (!sock
->wr_shutdown
&& (mask
& AFD_POLL_WRITE
))
1307 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1311 for (i
= 0; i
< req
->count
; ++i
)
1313 if (req
->sockets
[i
].sock
!= sock
) continue;
1315 ev
|= poll_flags_from_afd( sock
, req
->sockets
[i
].mask
);
1322 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
1324 return FD_TYPE_SOCKET
;
1327 static void sock_cancel_async( struct fd
*fd
, struct async
*async
)
1329 struct poll_req
*req
;
1331 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1335 if (req
->async
!= async
)
1338 for (i
= 0; i
< req
->count
; i
++)
1340 struct sock
*sock
= req
->sockets
[i
].sock
;
1342 if (sock
->main_poll
== req
)
1343 sock
->main_poll
= NULL
;
1347 async_terminate( async
, STATUS_CANCELLED
);
1350 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
1352 struct sock
*sock
= get_fd_user( fd
);
1353 struct async_queue
*queue
;
1355 assert( sock
->obj
.ops
== &sock_ops
);
1359 case ASYNC_TYPE_READ
:
1360 if (sock
->rd_shutdown
)
1362 set_error( STATUS_PIPE_DISCONNECTED
);
1365 queue
= &sock
->read_q
;
1368 case ASYNC_TYPE_WRITE
:
1369 if (sock
->wr_shutdown
)
1371 set_error( STATUS_PIPE_DISCONNECTED
);
1374 queue
= &sock
->write_q
;
1378 set_error( STATUS_INVALID_PARAMETER
);
1382 if (sock
->state
!= SOCK_CONNECTED
)
1384 set_error( STATUS_PIPE_DISCONNECTED
);
1388 queue_async( queue
, async
);
1389 sock_reselect( sock
);
1391 set_error( STATUS_PENDING
);
1394 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
1396 struct sock
*sock
= get_fd_user( fd
);
1398 if (sock
->wr_shutdown_pending
&& list_empty( &sock
->write_q
.queue
))
1400 shutdown( get_unix_fd( sock
->fd
), SHUT_WR
);
1401 sock
->wr_shutdown_pending
= 0;
1404 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1405 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1407 if (queue
!= &sock
->ifchange_q
&& sock
->type
)
1408 sock_reselect( sock
);
1411 static struct fd
*sock_get_fd( struct object
*obj
)
1413 struct sock
*sock
= (struct sock
*)obj
;
1414 return (struct fd
*)grab_object( sock
->fd
);
1417 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
1419 struct sock
*sock
= (struct sock
*)obj
;
1421 if (sock
->obj
.handle_count
== 1) /* last handle */
1423 struct accept_req
*accept_req
, *accept_next
;
1424 struct poll_req
*poll_req
, *poll_next
;
1426 if (sock
->accept_recv_req
)
1427 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
1429 LIST_FOR_EACH_ENTRY_SAFE( accept_req
, accept_next
, &sock
->accept_list
, struct accept_req
, entry
)
1430 async_terminate( accept_req
->async
, STATUS_CANCELLED
);
1432 if (sock
->connect_req
)
1433 async_terminate( sock
->connect_req
->async
, STATUS_CANCELLED
);
1435 LIST_FOR_EACH_ENTRY_SAFE( poll_req
, poll_next
, &poll_list
, struct poll_req
, entry
)
1437 struct iosb
*iosb
= poll_req
->iosb
;
1438 BOOL signaled
= FALSE
;
1441 if (iosb
->status
!= STATUS_PENDING
) continue;
1443 for (i
= 0; i
< poll_req
->count
; ++i
)
1445 if (poll_req
->sockets
[i
].sock
== sock
)
1448 poll_req
->sockets
[i
].flags
= AFD_POLL_CLOSE
;
1449 poll_req
->sockets
[i
].status
= 0;
1453 if (signaled
) complete_async_poll( poll_req
, STATUS_SUCCESS
);
1460 static void sock_destroy( struct object
*obj
)
1462 struct sock
*sock
= (struct sock
*)obj
;
1464 assert( obj
->ops
== &sock_ops
);
1466 /* FIXME: special socket shutdown stuff? */
1468 if ( sock
->deferred
)
1469 release_object( sock
->deferred
);
1471 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
1472 sock_release_ifchange( sock
);
1473 free_async_queue( &sock
->read_q
);
1474 free_async_queue( &sock
->write_q
);
1475 free_async_queue( &sock
->ifchange_q
);
1476 free_async_queue( &sock
->accept_q
);
1477 free_async_queue( &sock
->connect_q
);
1478 free_async_queue( &sock
->poll_q
);
1479 if (sock
->event
) release_object( sock
->event
);
1482 /* shut the socket down to force pending poll() calls in the client to return */
1483 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
1484 release_object( sock
->fd
);
1488 static struct sock
*create_socket(void)
1492 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
1494 sock
->state
= SOCK_UNCONNECTED
;
1496 sock
->pending_events
= 0;
1497 sock
->reported_events
= 0;
1506 sock
->connect_time
= 0;
1507 sock
->deferred
= NULL
;
1508 sock
->ifchange_obj
= NULL
;
1509 sock
->accept_recv_req
= NULL
;
1510 sock
->connect_req
= NULL
;
1511 sock
->main_poll
= NULL
;
1512 memset( &sock
->addr
, 0, sizeof(sock
->addr
) );
1514 sock
->rd_shutdown
= 0;
1515 sock
->wr_shutdown
= 0;
1516 sock
->wr_shutdown_pending
= 0;
1519 sock
->nonblocking
= 0;
1525 init_async_queue( &sock
->read_q
);
1526 init_async_queue( &sock
->write_q
);
1527 init_async_queue( &sock
->ifchange_q
);
1528 init_async_queue( &sock
->accept_q
);
1529 init_async_queue( &sock
->connect_q
);
1530 init_async_queue( &sock
->poll_q
);
1531 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
1532 list_init( &sock
->accept_list
);
1536 static int get_unix_family( int family
)
1540 case WS_AF_INET
: return AF_INET
;
1541 case WS_AF_INET6
: return AF_INET6
;
1543 case WS_AF_IPX
: return AF_IPX
;
1546 case WS_AF_IRDA
: return AF_IRDA
;
1548 case WS_AF_UNSPEC
: return AF_UNSPEC
;
1553 static int get_unix_type( int type
)
1557 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
1558 case WS_SOCK_RAW
: return SOCK_RAW
;
1559 case WS_SOCK_STREAM
: return SOCK_STREAM
;
1564 static int get_unix_protocol( int protocol
)
1566 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1571 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
1572 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1573 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1574 case WS_IPPROTO_IPV4
: return IPPROTO_IPIP
;
1575 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1576 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1577 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1578 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1583 static void set_dont_fragment( int fd
, int level
, int value
)
1587 if (level
== IPPROTO_IP
)
1590 optname
= IP_DONTFRAG
;
1591 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1592 optname
= IP_MTU_DISCOVER
;
1593 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1600 #ifdef IPV6_DONTFRAG
1601 optname
= IPV6_DONTFRAG
;
1602 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1603 optname
= IPV6_MTU_DISCOVER
;
1604 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1610 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1613 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
, unsigned int flags
)
1615 unsigned int options
= 0;
1616 int sockfd
, unix_type
, unix_family
, unix_protocol
, value
;
1619 unix_family
= get_unix_family( family
);
1620 unix_type
= get_unix_type( type
);
1621 unix_protocol
= get_unix_protocol( protocol
);
1623 if (unix_protocol
< 0)
1625 if (type
&& unix_type
< 0)
1626 set_win32_error( WSAESOCKTNOSUPPORT
);
1628 set_win32_error( WSAEPROTONOSUPPORT
);
1631 if (unix_family
< 0)
1633 if (family
>= 0 && unix_type
< 0)
1634 set_win32_error( WSAESOCKTNOSUPPORT
);
1636 set_win32_error( WSAEAFNOSUPPORT
);
1640 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1643 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1644 else set_win32_error( sock_get_error( errno
));
1647 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1649 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1652 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1655 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1658 /* Should we retrieve val using a getsockopt call and then
1659 * set the modified one? */
1660 val
.ipx_pt
= ipx_type
;
1661 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1666 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1668 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1669 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1670 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1671 else if (unix_type
== SOCK_STREAM
)
1672 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1676 if (unix_family
== AF_INET6
)
1678 static const int enable
= 1;
1679 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1683 len
= sizeof(value
);
1684 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_RCVBUF
, &value
, &len
))
1685 sock
->rcvbuf
= value
;
1687 len
= sizeof(value
);
1688 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_SNDBUF
, &value
, &len
))
1689 sock
->sndbuf
= value
;
1691 sock
->state
= (type
== WS_SOCK_STREAM
? SOCK_UNCONNECTED
: SOCK_CONNECTIONLESS
);
1692 sock
->flags
= flags
;
1693 sock
->proto
= protocol
;
1695 sock
->family
= family
;
1699 options
= get_fd_options( sock
->fd
);
1700 release_object( sock
->fd
);
1703 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1708 /* We can't immediately allow caching for a connection-mode socket, since it
1709 * might be accepted into (changing the underlying fd object.) */
1710 if (sock
->type
!= WS_SOCK_STREAM
) allow_fd_caching( sock
->fd
);
1715 /* accepts a socket and inits it */
1716 static int accept_new_fd( struct sock
*sock
)
1719 /* Try to accept(2). We can't be safe that this an already connected socket
1720 * or that accept() is allowed on it. In those cases we will get -1/errno
1723 struct sockaddr saddr
;
1724 socklen_t slen
= sizeof(saddr
);
1725 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1727 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1729 set_error( sock_get_ntstatus( errno
));
1733 /* accept a socket (creates a new fd) */
1734 static struct sock
*accept_socket( struct sock
*sock
)
1736 struct sock
*acceptsock
;
1739 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1741 if ( sock
->deferred
)
1743 acceptsock
= sock
->deferred
;
1744 sock
->deferred
= NULL
;
1748 union unix_sockaddr unix_addr
;
1751 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1752 if (!(acceptsock
= create_socket()))
1758 /* newly created socket gets the same properties of the listening socket */
1759 acceptsock
->state
= SOCK_CONNECTED
;
1760 acceptsock
->bound
= 1;
1761 acceptsock
->nonblocking
= sock
->nonblocking
;
1762 acceptsock
->mask
= sock
->mask
;
1763 acceptsock
->proto
= sock
->proto
;
1764 acceptsock
->type
= sock
->type
;
1765 acceptsock
->family
= sock
->family
;
1766 acceptsock
->window
= sock
->window
;
1767 acceptsock
->message
= sock
->message
;
1768 acceptsock
->connect_time
= current_time
;
1769 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
1770 acceptsock
->flags
= sock
->flags
;
1771 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1772 get_fd_options( sock
->fd
) )))
1774 release_object( acceptsock
);
1777 unix_len
= sizeof(unix_addr
);
1778 if (!getsockname( acceptfd
, &unix_addr
.addr
, &unix_len
))
1779 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1782 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1783 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1784 sock_reselect( sock
);
1788 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
1790 union unix_sockaddr unix_addr
;
1795 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
1797 if ( sock
->deferred
)
1799 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
1800 get_fd_options( acceptsock
->fd
) );
1804 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
1806 release_object( sock
->deferred
);
1807 sock
->deferred
= NULL
;
1811 if ((acceptfd
= accept_new_fd( sock
)) == -1)
1814 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1815 get_fd_options( acceptsock
->fd
) )))
1819 acceptsock
->state
= SOCK_CONNECTED
;
1820 acceptsock
->pending_events
= 0;
1821 acceptsock
->reported_events
= 0;
1822 acceptsock
->proto
= sock
->proto
;
1823 acceptsock
->type
= sock
->type
;
1824 acceptsock
->family
= sock
->family
;
1825 acceptsock
->wparam
= 0;
1826 acceptsock
->deferred
= NULL
;
1827 acceptsock
->connect_time
= current_time
;
1828 fd_copy_completion( acceptsock
->fd
, newfd
);
1829 release_object( acceptsock
->fd
);
1830 acceptsock
->fd
= newfd
;
1832 unix_len
= sizeof(unix_addr
);
1833 if (!getsockname( get_unix_fd( newfd
), &unix_addr
.addr
, &unix_len
))
1834 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1837 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1838 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1839 sock_reselect( sock
);
1846 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
1848 static const int enable
= 1;
1851 if (!(index
= if_nametoindex( name
)))
1854 if (setsockopt( fd
, IPPROTO_IP
, IP_BOUND_IF
, &index
, sizeof(index
) ))
1857 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
1860 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER) && defined(SO_BINDTODEVICE)
1862 struct interface_filter
1864 struct sock_filter iface_memaddr
;
1865 struct sock_filter iface_rule
;
1866 struct sock_filter ip_memaddr
;
1867 struct sock_filter ip_rule
;
1868 struct sock_filter return_keep
;
1869 struct sock_filter return_dump
;
1871 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
1872 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1873 /sizeof(struct sock_filter)
1874 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
1875 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1876 /sizeof(struct sock_filter)
1877 # define FILTER_JUMP_NEXT() (u_char)(0)
1878 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
1879 static struct interface_filter generic_interface_filter
=
1881 /* This filter rule allows incoming packets on the specified interface, which works for all
1882 * remotely generated packets and for locally generated broadcast packets. */
1883 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
1884 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
1885 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
1886 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
1887 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
1888 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
1889 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
1890 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
1893 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
1895 struct interface_filter specific_interface_filter
;
1896 struct sock_fprog filter_prog
;
1897 static const int enable
= 1;
1901 if (!setsockopt( fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen( name
) + 1 ))
1904 /* SO_BINDTODEVICE requires NET_CAP_RAW until Linux 5.7. */
1906 fprintf( stderr
, "setsockopt SO_BINDTODEVICE fd %d, name %s failed: %s, falling back to SO_REUSE_ADDR\n",
1907 fd
, name
, strerror( errno
));
1909 if (!(index
= if_nametoindex( name
)))
1912 ifindex
= htonl( index
);
1913 if (setsockopt( fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
) ) < 0)
1916 specific_interface_filter
= generic_interface_filter
;
1917 specific_interface_filter
.iface_rule
.k
= index
;
1918 specific_interface_filter
.ip_rule
.k
= htonl( bind_addr
);
1919 filter_prog
.len
= sizeof(generic_interface_filter
) / sizeof(struct sock_filter
);
1920 filter_prog
.filter
= (struct sock_filter
*)&specific_interface_filter
;
1921 if (setsockopt( fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
) ))
1924 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
1929 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
1935 #endif /* LINUX_BOUND_IF */
1937 /* Take bind() calls on any name corresponding to a local network adapter and
1938 * restrict the given socket to operating only on the specified interface. This
1939 * restriction consists of two components:
1940 * 1) An outgoing packet restriction suggesting the egress interface for all
1942 * 2) An incoming packet restriction dropping packets not meant for the
1944 * If the function succeeds in placing these restrictions, then the name for the
1945 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
1946 * receipt of broadcast packets on the socket. This behavior is only relevant to
1947 * UDP sockets and is needed for applications that expect to be able to receive
1948 * broadcast packets on a socket that is bound to a specific network interface.
1950 static int bind_to_interface( struct sock
*sock
, const struct sockaddr_in
*addr
)
1952 in_addr_t bind_addr
= addr
->sin_addr
.s_addr
;
1953 struct ifaddrs
*ifaddrs
, *ifaddr
;
1954 int fd
= get_unix_fd( sock
->fd
);
1957 if (bind_addr
== htonl( INADDR_ANY
) || bind_addr
== htonl( INADDR_LOOPBACK
))
1959 if (sock
->type
!= WS_SOCK_DGRAM
)
1962 if (getifaddrs( &ifaddrs
) < 0) return 0;
1964 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
1966 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET
1967 && ((struct sockaddr_in
*)ifaddr
->ifa_addr
)->sin_addr
.s_addr
== bind_addr
)
1969 if ((err
= bind_to_iface_name( fd
, bind_addr
, ifaddr
->ifa_name
)) < 0)
1972 fprintf( stderr
, "failed to bind to interface: %s\n", strerror( errno
) );
1977 freeifaddrs( ifaddrs
);
1981 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1982 static unsigned int get_ipv6_interface_index( const struct in6_addr
*addr
)
1984 struct ifaddrs
*ifaddrs
, *ifaddr
;
1986 if (getifaddrs( &ifaddrs
) < 0) return 0;
1988 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
1990 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET6
1991 && !memcmp( &((struct sockaddr_in6
*)ifaddr
->ifa_addr
)->sin6_addr
, addr
, sizeof(*addr
) ))
1993 unsigned int index
= if_nametoindex( ifaddr
->ifa_name
);
1998 fprintf( stderr
, "Unable to look up interface index for %s: %s\n",
1999 ifaddr
->ifa_name
, strerror( errno
) );
2003 freeifaddrs( ifaddrs
);
2008 freeifaddrs( ifaddrs
);
2013 /* return an errno value mapped to a WSA error */
2014 static unsigned int sock_get_error( int err
)
2018 case EINTR
: return WSAEINTR
;
2019 case EBADF
: return WSAEBADF
;
2021 case EACCES
: return WSAEACCES
;
2022 case EFAULT
: return WSAEFAULT
;
2023 case EINVAL
: return WSAEINVAL
;
2024 case EMFILE
: return WSAEMFILE
;
2026 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
2027 case EALREADY
: return WSAEALREADY
;
2028 case ENOTSOCK
: return WSAENOTSOCK
;
2029 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
2030 case EMSGSIZE
: return WSAEMSGSIZE
;
2031 case EPROTOTYPE
: return WSAEPROTOTYPE
;
2032 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
2033 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
2034 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
2035 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
2036 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
2037 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
2038 case EADDRINUSE
: return WSAEADDRINUSE
;
2039 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
2040 case ENETDOWN
: return WSAENETDOWN
;
2041 case ENETUNREACH
: return WSAENETUNREACH
;
2042 case ENETRESET
: return WSAENETRESET
;
2043 case ECONNABORTED
: return WSAECONNABORTED
;
2045 case ECONNRESET
: return WSAECONNRESET
;
2046 case ENOBUFS
: return WSAENOBUFS
;
2047 case EISCONN
: return WSAEISCONN
;
2048 case ENOTCONN
: return WSAENOTCONN
;
2049 case ESHUTDOWN
: return WSAESHUTDOWN
;
2050 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
2051 case ETIMEDOUT
: return WSAETIMEDOUT
;
2052 case ECONNREFUSED
: return WSAECONNREFUSED
;
2053 case ELOOP
: return WSAELOOP
;
2054 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
2055 case EHOSTDOWN
: return WSAEHOSTDOWN
;
2056 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
2057 case ENOTEMPTY
: return WSAENOTEMPTY
;
2059 case EPROCLIM
: return WSAEPROCLIM
;
2062 case EUSERS
: return WSAEUSERS
;
2065 case EDQUOT
: return WSAEDQUOT
;
2068 case ESTALE
: return WSAESTALE
;
2071 case EREMOTE
: return WSAEREMOTE
;
2077 perror("wineserver: sock_get_error() can't map error");
2082 static int sock_get_ntstatus( int err
)
2086 case EBADF
: return STATUS_INVALID_HANDLE
;
2087 case EBUSY
: return STATUS_DEVICE_BUSY
;
2089 case EACCES
: return STATUS_ACCESS_DENIED
;
2090 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
2091 case EINVAL
: return STATUS_INVALID_PARAMETER
;
2093 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
2095 case EWOULDBLOCK
: return STATUS_DEVICE_NOT_READY
;
2096 case EALREADY
: return STATUS_NETWORK_BUSY
;
2097 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
2098 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
2099 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
2100 case EPROTONOSUPPORT
:
2101 case ESOCKTNOSUPPORT
:
2104 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
2105 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
2106 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
2107 case EADDRINUSE
: return STATUS_SHARING_VIOLATION
;
2108 /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
2109 * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
2111 case EADDRNOTAVAIL
: return STATUS_INVALID_ADDRESS_COMPONENT
;
2112 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
2113 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
2114 case ENOTCONN
: return STATUS_INVALID_CONNECTION
;
2115 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
2116 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
2117 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
2118 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
2120 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
2121 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
2122 case EISCONN
: return STATUS_CONNECTION_ACTIVE
;
2124 case 0: return STATUS_SUCCESS
;
2127 perror("wineserver: sock_get_ntstatus() can't map error");
2128 return STATUS_UNSUCCESSFUL
;
2132 static struct accept_req
*alloc_accept_req( struct sock
*sock
, struct sock
*acceptsock
, struct async
*async
,
2133 const struct afd_accept_into_params
*params
)
2135 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
2139 req
->async
= (struct async
*)grab_object( async
);
2140 req
->iosb
= async_get_iosb( async
);
2141 req
->sock
= (struct sock
*)grab_object( sock
);
2142 req
->acceptsock
= acceptsock
;
2143 if (acceptsock
) grab_object( acceptsock
);
2149 req
->recv_len
= params
->recv_len
;
2150 req
->local_len
= params
->local_len
;
2156 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2158 struct sock
*sock
= get_fd_user( fd
);
2161 assert( sock
->obj
.ops
== &sock_ops
);
2163 if (code
!= IOCTL_AFD_WINE_CREATE
&& (unix_fd
= get_unix_fd( fd
)) < 0) return;
2167 case IOCTL_AFD_WINE_CREATE
:
2169 const struct afd_create_params
*params
= get_req_data();
2171 if (get_req_data_size() != sizeof(*params
))
2173 set_error( STATUS_INVALID_PARAMETER
);
2176 init_socket( sock
, params
->family
, params
->type
, params
->protocol
, params
->flags
);
2180 case IOCTL_AFD_WINE_ACCEPT
:
2182 struct sock
*acceptsock
;
2183 obj_handle_t handle
;
2185 if (get_reply_max_size() != sizeof(handle
))
2187 set_error( STATUS_BUFFER_TOO_SMALL
);
2191 if (!(acceptsock
= accept_socket( sock
)))
2193 struct accept_req
*req
;
2195 if (sock
->nonblocking
) return;
2196 if (get_error() != STATUS_DEVICE_NOT_READY
) return;
2198 if (!(req
= alloc_accept_req( sock
, NULL
, async
, NULL
))) return;
2199 list_add_tail( &sock
->accept_list
, &req
->entry
);
2201 async_set_completion_callback( async
, free_accept_req
, req
);
2202 queue_async( &sock
->accept_q
, async
);
2203 sock_reselect( sock
);
2204 set_error( STATUS_PENDING
);
2207 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
2208 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
2209 acceptsock
->wparam
= handle
;
2210 sock_reselect( acceptsock
);
2211 release_object( acceptsock
);
2212 set_reply_data( &handle
, sizeof(handle
) );
2216 case IOCTL_AFD_WINE_ACCEPT_INTO
:
2218 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
2219 const struct afd_accept_into_params
*params
= get_req_data();
2220 struct sock
*acceptsock
;
2221 unsigned int remote_len
;
2222 struct accept_req
*req
;
2224 if (get_req_data_size() != sizeof(*params
) ||
2225 get_reply_max_size() < params
->recv_len
||
2226 get_reply_max_size() - params
->recv_len
< params
->local_len
)
2228 set_error( STATUS_BUFFER_TOO_SMALL
);
2232 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
2233 if (remote_len
< sizeof(int))
2235 set_error( STATUS_INVALID_PARAMETER
);
2239 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
2242 if (acceptsock
->accept_recv_req
)
2244 release_object( acceptsock
);
2245 set_error( STATUS_INVALID_PARAMETER
);
2249 if (!(req
= alloc_accept_req( sock
, acceptsock
, async
, params
)))
2251 release_object( acceptsock
);
2254 list_add_tail( &sock
->accept_list
, &req
->entry
);
2255 acceptsock
->accept_recv_req
= req
;
2256 release_object( acceptsock
);
2258 acceptsock
->wparam
= params
->accept_handle
;
2259 async_set_completion_callback( async
, free_accept_req
, req
);
2260 queue_async( &sock
->accept_q
, async
);
2261 sock_reselect( sock
);
2262 set_error( STATUS_PENDING
);
2266 case IOCTL_AFD_LISTEN
:
2268 const struct afd_listen_params
*params
= get_req_data();
2270 if (get_req_data_size() < sizeof(*params
))
2272 set_error( STATUS_INVALID_PARAMETER
);
2278 set_error( STATUS_INVALID_PARAMETER
);
2282 if (listen( unix_fd
, params
->backlog
) < 0)
2284 set_error( sock_get_ntstatus( errno
) );
2288 sock
->state
= SOCK_LISTENING
;
2290 /* a listening socket can no longer be accepted into */
2291 allow_fd_caching( sock
->fd
);
2293 /* we may already be selecting for AFD_POLL_ACCEPT */
2294 sock_reselect( sock
);
2298 case IOCTL_AFD_WINE_CONNECT
:
2300 const struct afd_connect_params
*params
= get_req_data();
2301 const struct WS_sockaddr
*addr
;
2302 union unix_sockaddr unix_addr
;
2303 struct connect_req
*req
;
2307 if (get_req_data_size() < sizeof(*params
) ||
2308 get_req_data_size() - sizeof(*params
) < params
->addr_len
)
2310 set_error( STATUS_BUFFER_TOO_SMALL
);
2313 send_len
= get_req_data_size() - sizeof(*params
) - params
->addr_len
;
2314 addr
= (const struct WS_sockaddr
*)(params
+ 1);
2316 if (!params
->synchronous
&& !sock
->bound
)
2318 set_error( STATUS_INVALID_PARAMETER
);
2322 if (sock
->accept_recv_req
)
2324 set_error( STATUS_INVALID_PARAMETER
);
2328 if (sock
->connect_req
)
2330 set_error( STATUS_INVALID_PARAMETER
);
2334 switch (sock
->state
)
2336 case SOCK_LISTENING
:
2337 set_error( STATUS_INVALID_PARAMETER
);
2340 case SOCK_CONNECTING
:
2341 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2342 * but there's no status code that maps to WSAEALREADY... */
2343 set_error( params
->synchronous
? STATUS_ADDRESS_ALREADY_ASSOCIATED
: STATUS_INVALID_PARAMETER
);
2346 case SOCK_CONNECTED
:
2347 set_error( STATUS_CONNECTION_ACTIVE
);
2350 case SOCK_UNCONNECTED
:
2351 case SOCK_CONNECTIONLESS
:
2355 unix_len
= sockaddr_to_unix( addr
, params
->addr_len
, &unix_addr
);
2358 set_error( STATUS_INVALID_ADDRESS
);
2361 if (unix_addr
.addr
.sa_family
== AF_INET
&& !memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 ))
2362 unix_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_LOOPBACK
);
2364 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2365 if (ret
< 0 && errno
!= EINPROGRESS
)
2367 set_error( sock_get_ntstatus( errno
) );
2371 /* a connected or connecting socket can no longer be accepted into */
2372 allow_fd_caching( sock
->fd
);
2374 unix_len
= sizeof(unix_addr
);
2375 if (!getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
))
2376 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2381 sock
->state
= SOCK_CONNECTED
;
2383 if (!send_len
) return;
2386 sock
->state
= SOCK_CONNECTING
;
2388 if (params
->synchronous
&& sock
->nonblocking
)
2390 sock_reselect( sock
);
2391 set_error( STATUS_DEVICE_NOT_READY
);
2395 if (!(req
= mem_alloc( sizeof(*req
) )))
2398 req
->async
= (struct async
*)grab_object( async
);
2399 req
->iosb
= async_get_iosb( async
);
2400 req
->sock
= (struct sock
*)grab_object( sock
);
2401 req
->addr_len
= params
->addr_len
;
2402 req
->send_len
= send_len
;
2403 req
->send_cursor
= 0;
2405 async_set_completion_callback( async
, free_connect_req
, req
);
2406 sock
->connect_req
= req
;
2407 queue_async( &sock
->connect_q
, async
);
2408 sock_reselect( sock
);
2409 set_error( STATUS_PENDING
);
2413 case IOCTL_AFD_WINE_SHUTDOWN
:
2417 if (get_req_data_size() < sizeof(int))
2419 set_error( STATUS_BUFFER_TOO_SMALL
);
2422 how
= *(int *)get_req_data();
2426 set_error( STATUS_INVALID_PARAMETER
);
2430 if (sock
->state
!= SOCK_CONNECTED
&& sock
->state
!= SOCK_CONNECTIONLESS
)
2432 set_error( STATUS_INVALID_CONNECTION
);
2438 sock
->rd_shutdown
= 1;
2440 if (how
!= SD_RECEIVE
)
2442 sock
->wr_shutdown
= 1;
2443 if (list_empty( &sock
->write_q
.queue
))
2444 shutdown( unix_fd
, SHUT_WR
);
2446 sock
->wr_shutdown_pending
= 1;
2451 if (sock
->event
) release_object( sock
->event
);
2455 sock
->nonblocking
= 1;
2458 sock_reselect( sock
);
2462 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE
:
2466 if (get_req_data_size() < sizeof(int))
2468 set_error( STATUS_BUFFER_TOO_SMALL
);
2471 force_async
= *(int *)get_req_data();
2473 if (sock
->nonblocking
&& !force_async
)
2475 set_error( STATUS_DEVICE_NOT_READY
);
2478 if (!sock_get_ifchange( sock
)) return;
2479 queue_async( &sock
->ifchange_q
, async
);
2480 set_error( STATUS_PENDING
);
2484 case IOCTL_AFD_WINE_FIONBIO
:
2485 if (get_req_data_size() < sizeof(int))
2487 set_error( STATUS_BUFFER_TOO_SMALL
);
2490 if (*(int *)get_req_data())
2492 sock
->nonblocking
= 1;
2498 set_error( STATUS_INVALID_PARAMETER
);
2501 sock
->nonblocking
= 0;
2505 case IOCTL_AFD_GET_EVENTS
:
2507 struct afd_get_events_params params
= {0};
2510 if (get_reply_max_size() < sizeof(params
))
2512 set_error( STATUS_INVALID_PARAMETER
);
2516 params
.flags
= sock
->pending_events
& sock
->mask
;
2517 for (i
= 0; i
< ARRAY_SIZE( params
.status
); ++i
)
2518 params
.status
[i
] = sock_get_ntstatus( sock
->errors
[i
] );
2520 sock
->pending_events
= 0;
2521 sock_reselect( sock
);
2523 set_reply_data( ¶ms
, sizeof(params
) );
2527 case IOCTL_AFD_EVENT_SELECT
:
2529 struct event
*event
= NULL
;
2530 obj_handle_t event_handle
;
2533 set_async_pending( async
);
2535 if (is_machine_64bit( current
->process
->machine
))
2537 const struct afd_event_select_params_64
*params
= get_req_data();
2539 if (get_req_data_size() < sizeof(*params
))
2541 set_error( STATUS_INVALID_PARAMETER
);
2545 event_handle
= params
->event
;
2546 mask
= params
->mask
;
2550 const struct afd_event_select_params_32
*params
= get_req_data();
2552 if (get_req_data_size() < sizeof(*params
))
2554 set_error( STATUS_INVALID_PARAMETER
);
2558 event_handle
= params
->event
;
2559 mask
= params
->mask
;
2562 if ((event_handle
|| mask
) &&
2563 !(event
= get_event_obj( current
->process
, event_handle
, EVENT_MODIFY_STATE
)))
2565 set_error( STATUS_INVALID_PARAMETER
);
2569 if (sock
->event
) release_object( sock
->event
);
2570 sock
->event
= event
;
2575 sock
->nonblocking
= 1;
2577 sock_reselect( sock
);
2578 /* Explicitly wake the socket up if the mask causes it to become
2579 * signaled. Note that reselecting isn't enough, since we might already
2580 * have had events recorded in sock->reported_events and we don't want
2581 * to select for them again. */
2582 sock_wake_up( sock
);
2587 case IOCTL_AFD_WINE_MESSAGE_SELECT
:
2589 const struct afd_message_select_params
*params
= get_req_data();
2591 if (get_req_data_size() < sizeof(params
))
2593 set_error( STATUS_BUFFER_TOO_SMALL
);
2597 if (sock
->event
) release_object( sock
->event
);
2601 sock
->pending_events
= 0;
2602 sock
->reported_events
= 0;
2605 sock
->mask
= params
->mask
;
2606 sock
->window
= params
->window
;
2607 sock
->message
= params
->message
;
2608 sock
->wparam
= params
->handle
;
2609 sock
->nonblocking
= 1;
2611 sock_reselect( sock
);
2616 case IOCTL_AFD_BIND
:
2618 const struct afd_bind_params
*params
= get_req_data();
2619 union unix_sockaddr unix_addr
, bind_addr
;
2620 data_size_t in_size
;
2623 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2625 if (get_req_data_size() < get_reply_max_size())
2627 set_error( STATUS_BUFFER_TOO_SMALL
);
2630 in_size
= get_req_data_size() - get_reply_max_size();
2631 if (in_size
< offsetof(struct afd_bind_params
, addr
.sa_data
)
2632 || get_reply_max_size() < in_size
- sizeof(int))
2634 set_error( STATUS_INVALID_PARAMETER
);
2640 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED
);
2644 unix_len
= sockaddr_to_unix( ¶ms
->addr
, in_size
- sizeof(int), &unix_addr
);
2647 set_error( STATUS_INVALID_ADDRESS
);
2650 bind_addr
= unix_addr
;
2652 if (unix_addr
.addr
.sa_family
== AF_INET
)
2654 if (!memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 )
2655 || bind_to_interface( sock
, &unix_addr
.in
))
2656 bind_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
2658 else if (unix_addr
.addr
.sa_family
== AF_INET6
)
2660 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2661 /* Windows allows specifying zero to use the default scope. Linux
2662 * interprets it as an interface index and requires that it be
2664 if (!unix_addr
.in6
.sin6_scope_id
)
2665 bind_addr
.in6
.sin6_scope_id
= get_ipv6_interface_index( &unix_addr
.in6
.sin6_addr
);
2669 set_async_pending( async
);
2671 if (bind( unix_fd
, &bind_addr
.addr
, unix_len
) < 0)
2673 if (errno
== EADDRINUSE
)
2676 socklen_t len
= sizeof(reuse
);
2678 if (!getsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&reuse
, &len
) && reuse
)
2682 set_error( sock_get_ntstatus( errno
) );
2688 unix_len
= sizeof(bind_addr
);
2689 if (!getsockname( unix_fd
, &bind_addr
.addr
, &unix_len
))
2691 /* store the interface or magic loopback address instead of the
2692 * actual unix address */
2693 if (bind_addr
.addr
.sa_family
== AF_INET
)
2694 bind_addr
.in
.sin_addr
= unix_addr
.in
.sin_addr
;
2695 sock
->addr_len
= sockaddr_from_unix( &bind_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2698 if (get_reply_max_size() >= sock
->addr_len
)
2699 set_reply_data( &sock
->addr
, sock
->addr_len
);
2703 case IOCTL_AFD_GETSOCKNAME
:
2706 set_error( STATUS_INVALID_PARAMETER
);
2710 if (get_reply_max_size() < sock
->addr_len
)
2712 set_error( STATUS_BUFFER_TOO_SMALL
);
2716 set_reply_data( &sock
->addr
, sock
->addr_len
);
2719 case IOCTL_AFD_WINE_DEFER
:
2721 const obj_handle_t
*handle
= get_req_data();
2722 struct sock
*acceptsock
;
2724 if (get_req_data_size() < sizeof(*handle
))
2726 set_error( STATUS_BUFFER_TOO_SMALL
);
2730 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, *handle
, 0, &sock_ops
);
2731 if (!acceptsock
) return;
2733 sock
->deferred
= acceptsock
;
2737 case IOCTL_AFD_WINE_GET_INFO
:
2739 struct afd_get_info_params params
;
2741 if (get_reply_max_size() < sizeof(params
))
2743 set_error( STATUS_BUFFER_TOO_SMALL
);
2747 params
.family
= sock
->family
;
2748 params
.type
= sock
->type
;
2749 params
.protocol
= sock
->proto
;
2750 set_reply_data( ¶ms
, sizeof(params
) );
2754 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN
:
2756 int listening
= (sock
->state
== SOCK_LISTENING
);
2758 if (get_reply_max_size() < sizeof(listening
))
2760 set_error( STATUS_BUFFER_TOO_SMALL
);
2764 set_reply_data( &listening
, sizeof(listening
) );
2768 case IOCTL_AFD_WINE_GET_SO_ERROR
:
2771 socklen_t len
= sizeof(error
);
2774 if (get_reply_max_size() < sizeof(error
))
2776 set_error( STATUS_BUFFER_TOO_SMALL
);
2780 if (getsockopt( unix_fd
, SOL_SOCKET
, SO_ERROR
, (char *)&error
, &len
) < 0)
2782 set_error( sock_get_ntstatus( errno
) );
2788 for (i
= 0; i
< ARRAY_SIZE( sock
->errors
); ++i
)
2790 if (sock
->errors
[i
])
2792 error
= sock_get_error( sock
->errors
[i
] );
2798 set_reply_data( &error
, sizeof(error
) );
2802 case IOCTL_AFD_WINE_GET_SO_RCVBUF
:
2804 int rcvbuf
= sock
->rcvbuf
;
2806 if (get_reply_max_size() < sizeof(rcvbuf
))
2808 set_error( STATUS_BUFFER_TOO_SMALL
);
2812 set_reply_data( &rcvbuf
, sizeof(rcvbuf
) );
2816 case IOCTL_AFD_WINE_SET_SO_RCVBUF
:
2820 if (get_req_data_size() < sizeof(rcvbuf
))
2822 set_error( STATUS_BUFFER_TOO_SMALL
);
2825 rcvbuf
= *(DWORD
*)get_req_data();
2827 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_RCVBUF
, (char *)&rcvbuf
, sizeof(rcvbuf
) ))
2828 sock
->rcvbuf
= rcvbuf
;
2830 set_error( sock_get_ntstatus( errno
) );
2834 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO
:
2836 DWORD rcvtimeo
= sock
->rcvtimeo
;
2838 if (get_reply_max_size() < sizeof(rcvtimeo
))
2840 set_error( STATUS_BUFFER_TOO_SMALL
);
2844 set_reply_data( &rcvtimeo
, sizeof(rcvtimeo
) );
2848 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO
:
2852 if (get_req_data_size() < sizeof(rcvtimeo
))
2854 set_error( STATUS_BUFFER_TOO_SMALL
);
2857 rcvtimeo
= *(DWORD
*)get_req_data();
2859 sock
->rcvtimeo
= rcvtimeo
;
2863 case IOCTL_AFD_WINE_GET_SO_SNDBUF
:
2865 int sndbuf
= sock
->sndbuf
;
2867 if (get_reply_max_size() < sizeof(sndbuf
))
2869 set_error( STATUS_BUFFER_TOO_SMALL
);
2873 set_reply_data( &sndbuf
, sizeof(sndbuf
) );
2877 case IOCTL_AFD_WINE_SET_SO_SNDBUF
:
2881 if (get_req_data_size() < sizeof(sndbuf
))
2883 set_error( STATUS_BUFFER_TOO_SMALL
);
2886 sndbuf
= *(DWORD
*)get_req_data();
2891 /* setsockopt fails if a zero value is passed */
2892 sock
->sndbuf
= sndbuf
;
2897 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_SNDBUF
, (char *)&sndbuf
, sizeof(sndbuf
) ))
2898 sock
->sndbuf
= sndbuf
;
2900 set_error( sock_get_ntstatus( errno
) );
2904 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO
:
2906 DWORD sndtimeo
= sock
->sndtimeo
;
2908 if (get_reply_max_size() < sizeof(sndtimeo
))
2910 set_error( STATUS_BUFFER_TOO_SMALL
);
2914 set_reply_data( &sndtimeo
, sizeof(sndtimeo
) );
2918 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO
:
2922 if (get_req_data_size() < sizeof(sndtimeo
))
2924 set_error( STATUS_BUFFER_TOO_SMALL
);
2927 sndtimeo
= *(DWORD
*)get_req_data();
2929 sock
->sndtimeo
= sndtimeo
;
2933 case IOCTL_AFD_WINE_GET_SO_CONNECT_TIME
:
2937 if (get_reply_max_size() < sizeof(time
))
2939 set_error( STATUS_BUFFER_TOO_SMALL
);
2943 if (sock
->state
== SOCK_CONNECTED
)
2944 time
= (current_time
- sock
->connect_time
) / 10000000;
2946 set_reply_data( &time
, sizeof(time
) );
2950 case IOCTL_AFD_POLL
:
2952 if (get_reply_max_size() < get_req_data_size())
2954 set_error( STATUS_INVALID_PARAMETER
);
2958 if (is_machine_64bit( current
->process
->machine
))
2960 const struct afd_poll_params_64
*params
= get_req_data();
2962 if (get_req_data_size() < sizeof(struct afd_poll_params_64
) ||
2963 get_req_data_size() < offsetof( struct afd_poll_params_64
, sockets
[params
->count
] ))
2965 set_error( STATUS_INVALID_PARAMETER
);
2969 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, params
->sockets
);
2973 const struct afd_poll_params_32
*params
= get_req_data();
2974 struct afd_poll_socket_64
*sockets
;
2977 if (get_req_data_size() < sizeof(struct afd_poll_params_32
) ||
2978 get_req_data_size() < offsetof( struct afd_poll_params_32
, sockets
[params
->count
] ))
2980 set_error( STATUS_INVALID_PARAMETER
);
2984 if (!(sockets
= mem_alloc( params
->count
* sizeof(*sockets
) ))) return;
2985 for (i
= 0; i
< params
->count
; ++i
)
2987 sockets
[i
].socket
= params
->sockets
[i
].socket
;
2988 sockets
[i
].flags
= params
->sockets
[i
].flags
;
2989 sockets
[i
].status
= params
->sockets
[i
].status
;
2992 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, sockets
);
3000 set_error( STATUS_NOT_SUPPORTED
);
3005 static int poll_single_socket( struct sock
*sock
, int mask
)
3007 struct pollfd pollfd
;
3009 pollfd
.fd
= get_unix_fd( sock
->fd
);
3010 pollfd
.events
= poll_flags_from_afd( sock
, mask
);
3011 if (pollfd
.events
< 0 || poll( &pollfd
, 1, 0 ) < 0)
3014 if (sock
->state
== SOCK_CONNECTING
&& (pollfd
.revents
& (POLLERR
| POLLHUP
)))
3015 pollfd
.revents
&= ~POLLOUT
;
3017 if ((mask
& AFD_POLL_HUP
) && (pollfd
.revents
& POLLIN
) && sock
->type
== WS_SOCK_STREAM
)
3021 if (!recv( get_unix_fd( sock
->fd
), &dummy
, 1, MSG_PEEK
))
3023 pollfd
.revents
&= ~POLLIN
;
3024 pollfd
.revents
|= POLLHUP
;
3028 return get_poll_flags( sock
, pollfd
.revents
) & mask
;
3031 static void handle_exclusive_poll(struct poll_req
*req
)
3035 for (i
= 0; i
< req
->count
; ++i
)
3037 struct sock
*sock
= req
->sockets
[i
].sock
;
3038 struct poll_req
*main_poll
= sock
->main_poll
;
3040 if (main_poll
&& main_poll
->exclusive
&& req
->exclusive
)
3042 complete_async_poll( main_poll
, STATUS_SUCCESS
);
3047 sock
->main_poll
= req
;
3051 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
3052 unsigned int count
, const struct afd_poll_socket_64
*sockets
)
3054 BOOL signaled
= FALSE
;
3055 struct poll_req
*req
;
3060 set_error( STATUS_INVALID_PARAMETER
);
3064 if (!(req
= mem_alloc( offsetof( struct poll_req
, sockets
[count
] ) )))
3067 req
->timeout
= NULL
;
3068 if (timeout
&& timeout
!= TIMEOUT_INFINITE
&&
3069 !(req
->timeout
= add_timeout_user( timeout
, async_poll_timeout
, req
)))
3074 req
->orig_timeout
= timeout
;
3076 for (i
= 0; i
< count
; ++i
)
3078 req
->sockets
[i
].sock
= (struct sock
*)get_handle_obj( current
->process
, sockets
[i
].socket
, 0, &sock_ops
);
3079 if (!req
->sockets
[i
].sock
)
3081 for (j
= 0; j
< i
; ++j
) release_object( req
->sockets
[j
].sock
);
3082 if (req
->timeout
) remove_timeout_user( req
->timeout
);
3086 req
->sockets
[i
].handle
= sockets
[i
].socket
;
3087 req
->sockets
[i
].mask
= sockets
[i
].flags
;
3088 req
->sockets
[i
].flags
= 0;
3091 req
->exclusive
= exclusive
;
3093 req
->async
= (struct async
*)grab_object( async
);
3094 req
->iosb
= async_get_iosb( async
);
3096 handle_exclusive_poll(req
);
3098 list_add_tail( &poll_list
, &req
->entry
);
3099 async_set_completion_callback( async
, free_poll_req
, req
);
3100 queue_async( &poll_sock
->poll_q
, async
);
3102 for (i
= 0; i
< count
; ++i
)
3104 struct sock
*sock
= req
->sockets
[i
].sock
;
3105 int mask
= req
->sockets
[i
].mask
;
3106 int flags
= poll_single_socket( sock
, mask
);
3111 req
->sockets
[i
].flags
= flags
;
3112 req
->sockets
[i
].status
= sock_get_ntstatus( sock_error( sock
->fd
) );
3115 /* FIXME: do other error conditions deserve a similar treatment? */
3116 if (sock
->state
!= SOCK_CONNECTING
&& sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] && (mask
& AFD_POLL_CONNECT_ERR
))
3119 req
->sockets
[i
].flags
|= AFD_POLL_CONNECT_ERR
;
3120 req
->sockets
[i
].status
= sock_get_ntstatus( sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] );
3124 if (!timeout
|| signaled
)
3125 complete_async_poll( req
, STATUS_SUCCESS
);
3127 for (i
= 0; i
< req
->count
; ++i
)
3128 sock_reselect( req
->sockets
[i
].sock
);
3129 set_error( STATUS_PENDING
);
3132 #ifdef HAVE_LINUX_RTNETLINK_H
3134 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
3135 static struct object
*ifchange_object
;
3137 static void ifchange_dump( struct object
*obj
, int verbose
);
3138 static struct fd
*ifchange_get_fd( struct object
*obj
);
3139 static void ifchange_destroy( struct object
*obj
);
3141 static int ifchange_get_poll_events( struct fd
*fd
);
3142 static void ifchange_poll_event( struct fd
*fd
, int event
);
3146 struct object obj
; /* object header */
3147 struct fd
*fd
; /* interface change file descriptor */
3148 struct list sockets
; /* list of sockets to send interface change notifications */
3151 static const struct object_ops ifchange_ops
=
3153 sizeof(struct ifchange
), /* size */
3154 &no_type
, /* type */
3155 ifchange_dump
, /* dump */
3156 no_add_queue
, /* add_queue */
3157 NULL
, /* remove_queue */
3158 NULL
, /* signaled */
3159 no_satisfied
, /* satisfied */
3160 no_signal
, /* signal */
3161 ifchange_get_fd
, /* get_fd */
3162 default_map_access
, /* map_access */
3163 default_get_sd
, /* get_sd */
3164 default_set_sd
, /* set_sd */
3165 no_get_full_name
, /* get_full_name */
3166 no_lookup_name
, /* lookup_name */
3167 no_link_name
, /* link_name */
3168 NULL
, /* unlink_name */
3169 no_open_file
, /* open_file */
3170 no_kernel_obj_list
, /* get_kernel_obj_list */
3171 no_close_handle
, /* close_handle */
3172 ifchange_destroy
/* destroy */
3175 static const struct fd_ops ifchange_fd_ops
=
3177 ifchange_get_poll_events
, /* get_poll_events */
3178 ifchange_poll_event
, /* poll_event */
3179 NULL
, /* get_fd_type */
3180 no_fd_read
, /* read */
3181 no_fd_write
, /* write */
3182 no_fd_flush
, /* flush */
3183 no_fd_get_file_info
, /* get_file_info */
3184 no_fd_get_volume_info
, /* get_volume_info */
3185 no_fd_ioctl
, /* ioctl */
3186 NULL
, /* cancel_async */
3187 NULL
, /* queue_async */
3188 NULL
/* reselect_async */
3191 static void ifchange_dump( struct object
*obj
, int verbose
)
3193 assert( obj
->ops
== &ifchange_ops
);
3194 fprintf( stderr
, "Interface change\n" );
3197 static struct fd
*ifchange_get_fd( struct object
*obj
)
3199 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3200 return (struct fd
*)grab_object( ifchange
->fd
);
3203 static void ifchange_destroy( struct object
*obj
)
3205 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3206 assert( obj
->ops
== &ifchange_ops
);
3208 release_object( ifchange
->fd
);
3210 /* reset the global ifchange object so that it will be recreated if it is needed again */
3211 assert( obj
== ifchange_object
);
3212 ifchange_object
= NULL
;
3215 static int ifchange_get_poll_events( struct fd
*fd
)
3220 /* wake up all the sockets waiting for a change notification event */
3221 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
3223 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3224 struct list
*ptr
, *next
;
3225 assert( obj
->ops
== &ifchange_ops
);
3226 assert( obj
== ifchange_object
);
3228 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
3230 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
3232 assert( sock
->ifchange_obj
);
3233 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
3234 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
3238 static void ifchange_poll_event( struct fd
*fd
, int event
)
3240 struct object
*ifchange
= get_fd_user( fd
);
3241 unsigned int status
= STATUS_PENDING
;
3242 char buffer
[PIPE_BUF
];
3245 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
3248 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
3249 return; /* retry when poll() says the socket is ready */
3250 status
= sock_get_ntstatus( errno
);
3254 struct nlmsghdr
*nlh
;
3256 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
3258 if (nlh
->nlmsg_type
== NLMSG_DONE
)
3260 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
3261 status
= STATUS_SUCCESS
;
3264 else status
= STATUS_CANCELLED
;
3266 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
3271 /* we only need one of these interface notification objects, all of the sockets dependent upon
3272 * it will wake up when a notification event occurs */
3273 static struct object
*get_ifchange( void )
3275 #ifdef HAVE_LINUX_RTNETLINK_H
3276 struct ifchange
*ifchange
;
3277 struct sockaddr_nl addr
;
3280 if (ifchange_object
)
3282 /* increment the refcount for each socket that uses the ifchange object */
3283 return grab_object( ifchange_object
);
3286 /* create the socket we need for processing interface change notifications */
3287 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
3290 set_error( sock_get_ntstatus( errno
));
3293 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
3294 memset( &addr
, 0, sizeof(addr
) );
3295 addr
.nl_family
= AF_NETLINK
;
3296 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
3297 /* bind the socket to the special netlink kernel interface */
3298 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
3301 set_error( sock_get_ntstatus( errno
));
3304 if (!(ifchange
= alloc_object( &ifchange_ops
)))
3307 set_error( STATUS_NO_MEMORY
);
3310 list_init( &ifchange
->sockets
);
3311 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
3313 release_object( ifchange
);
3314 set_error( STATUS_NO_MEMORY
);
3317 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
3319 /* the ifchange object is now successfully configured */
3320 ifchange_object
= &ifchange
->obj
;
3321 return &ifchange
->obj
;
3323 set_error( STATUS_NOT_SUPPORTED
);
3328 /* add the socket to the interface change notification list */
3329 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
3331 #ifdef HAVE_LINUX_RTNETLINK_H
3332 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3334 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
3338 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3339 static struct object
*sock_get_ifchange( struct sock
*sock
)
3341 struct object
*ifchange
;
3343 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
3344 return sock
->ifchange_obj
;
3346 if (!(ifchange
= get_ifchange()))
3349 /* add the socket to the ifchange notification list */
3350 ifchange_add_sock( ifchange
, sock
);
3351 sock
->ifchange_obj
= ifchange
;
3355 /* destroy an existing ifchange queue for a specific socket */
3356 static void sock_release_ifchange( struct sock
*sock
)
3358 if (sock
->ifchange_obj
)
3360 list_remove( &sock
->ifchange_entry
);
3361 release_object( sock
->ifchange_obj
);
3362 sock
->ifchange_obj
= NULL
;
3366 static void socket_device_dump( struct object
*obj
, int verbose
);
3367 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3368 unsigned int attr
, struct object
*root
);
3369 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3370 unsigned int sharing
, unsigned int options
);
3372 static const struct object_ops socket_device_ops
=
3374 sizeof(struct object
), /* size */
3375 &device_type
, /* type */
3376 socket_device_dump
, /* dump */
3377 no_add_queue
, /* add_queue */
3378 NULL
, /* remove_queue */
3379 NULL
, /* signaled */
3380 no_satisfied
, /* satisfied */
3381 no_signal
, /* signal */
3382 no_get_fd
, /* get_fd */
3383 default_map_access
, /* map_access */
3384 default_get_sd
, /* get_sd */
3385 default_set_sd
, /* set_sd */
3386 default_get_full_name
, /* get_full_name */
3387 socket_device_lookup_name
, /* lookup_name */
3388 directory_link_name
, /* link_name */
3389 default_unlink_name
, /* unlink_name */
3390 socket_device_open_file
, /* open_file */
3391 no_kernel_obj_list
, /* get_kernel_obj_list */
3392 no_close_handle
, /* close_handle */
3393 no_destroy
/* destroy */
3396 static void socket_device_dump( struct object
*obj
, int verbose
)
3398 fputs( "Socket device\n", stderr
);
3401 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3402 unsigned int attr
, struct object
*root
)
3404 if (name
) name
->len
= 0;
3408 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3409 unsigned int sharing
, unsigned int options
)
3413 if (!(sock
= create_socket())) return NULL
;
3414 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
3416 release_object( sock
);
3422 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
3423 unsigned int attr
, const struct security_descriptor
*sd
)
3425 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
3428 DECL_HANDLER(recv_socket
)
3430 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3431 unsigned int status
= STATUS_PENDING
;
3432 timeout_t timeout
= 0;
3433 struct async
*async
;
3439 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3440 timeout
= (timeout_t
)sock
->rcvtimeo
* -10000;
3442 if (sock
->rd_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3443 else if (!async_queued( &sock
->read_q
))
3445 /* If read_q is not empty, we cannot really tell if the already queued
3446 * asyncs will not consume all available data; if there's no data
3447 * available, the current request won't be immediately satiable.
3449 struct pollfd pollfd
;
3450 pollfd
.fd
= get_unix_fd( sock
->fd
);
3451 pollfd
.events
= req
->oob
? POLLPRI
: POLLIN
;
3453 if (poll(&pollfd
, 1, 0) >= 0 && pollfd
.revents
)
3455 /* Give the client opportunity to complete synchronously.
3456 * If it turns out that the I/O request is not actually immediately satiable,
3457 * the client may then choose to re-queue the async (with STATUS_PENDING). */
3458 status
= STATUS_ALERTED
;
3462 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3463 status
= STATUS_DEVICE_NOT_READY
;
3465 sock
->pending_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3466 sock
->reported_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3468 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3470 set_error( status
);
3473 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3475 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3476 queue_async( &sock
->read_q
, async
);
3478 /* always reselect; we changed reported_events above */
3479 sock_reselect( sock
);
3481 reply
->wait
= async_handoff( async
, NULL
, 0 );
3482 reply
->options
= get_fd_options( fd
);
3483 reply
->nonblocking
= sock
->nonblocking
;
3484 release_object( async
);
3486 release_object( sock
);
3489 static void send_socket_completion_callback( void *private )
3491 struct send_req
*send_req
= private;
3492 struct iosb
*iosb
= send_req
->iosb
;
3493 struct sock
*sock
= send_req
->sock
;
3495 if (iosb
->status
!= STATUS_SUCCESS
)
3497 /* send() calls only clear and reselect events if unsuccessful. */
3498 sock
->pending_events
&= ~AFD_POLL_WRITE
;
3499 sock
->reported_events
&= ~AFD_POLL_WRITE
;
3500 sock_reselect( sock
);
3503 release_object( iosb
);
3504 release_object( sock
);
3508 DECL_HANDLER(send_socket
)
3510 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3511 unsigned int status
= STATUS_PENDING
;
3512 timeout_t timeout
= 0;
3513 struct async
*async
;
3520 if (sock
->type
== WS_SOCK_DGRAM
&& !sock
->bound
)
3522 union unix_sockaddr unix_addr
;
3524 int unix_fd
= get_unix_fd( fd
);
3526 unix_len
= get_unix_sockaddr_any( &unix_addr
, sock
->family
);
3527 if (bind( unix_fd
, &unix_addr
.addr
, unix_len
) < 0)
3530 if (getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
) >= 0)
3532 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
3535 else if (!bind_errno
) bind_errno
= errno
;
3538 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3539 timeout
= (timeout_t
)sock
->sndtimeo
* -10000;
3541 if (bind_errno
) status
= sock_get_ntstatus( bind_errno
);
3542 else if (sock
->wr_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3543 else if (!async_queued( &sock
->write_q
))
3545 /* If write_q is not empty, we cannot really tell if the already queued
3546 * asyncs will not consume all available space; if there's no space
3547 * available, the current request won't be immediately satiable.
3549 struct pollfd pollfd
;
3550 pollfd
.fd
= get_unix_fd( sock
->fd
);
3551 pollfd
.events
= POLLOUT
;
3553 if (poll(&pollfd
, 1, 0) >= 0 && pollfd
.revents
)
3555 /* Give the client opportunity to complete synchronously.
3556 * If it turns out that the I/O request is not actually immediately satiable,
3557 * the client may then choose to re-queue the async (with STATUS_PENDING). */
3558 status
= STATUS_ALERTED
;
3562 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3563 status
= STATUS_DEVICE_NOT_READY
;
3565 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3567 struct send_req
*send_req
;
3568 struct iosb
*iosb
= async_get_iosb( async
);
3570 if ((send_req
= mem_alloc( sizeof(*send_req
) )))
3572 send_req
->iosb
= (struct iosb
*)grab_object( iosb
);
3573 send_req
->sock
= (struct sock
*)grab_object( sock
);
3574 async_set_completion_callback( async
, send_socket_completion_callback
, send_req
);
3576 else if (status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
)
3577 status
= STATUS_NO_MEMORY
;
3579 release_object( iosb
);
3581 set_error( status
);
3584 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3586 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3588 queue_async( &sock
->write_q
, async
);
3589 sock_reselect( sock
);
3592 reply
->wait
= async_handoff( async
, NULL
, 0 );
3593 reply
->options
= get_fd_options( fd
);
3594 reply
->nonblocking
= sock
->nonblocking
;
3595 release_object( async
);
3597 release_object( sock
);