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 void 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
);
580 static unsigned int afd_poll_flag_to_win32( unsigned int flags
)
582 static const unsigned int map
[] =
586 FD_WRITE
, /* WRITE */
588 FD_CLOSE
, /* RESET */
590 FD_CONNECT
, /* CONNECT */
591 FD_ACCEPT
, /* ACCEPT */
592 FD_CONNECT
, /* CONNECT_ERR */
595 unsigned int i
, ret
= 0;
597 for (i
= 0; i
< ARRAY_SIZE(map
); ++i
)
599 if (flags
& (1 << i
)) ret
|= map
[i
];
605 /* wake anybody waiting on the socket event or send the associated message */
606 static void sock_wake_up( struct sock
*sock
)
608 unsigned int events
= sock
->pending_events
& sock
->mask
;
613 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
615 set_event( sock
->event
);
619 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
620 for (i
= 0; i
< ARRAY_SIZE(event_bitorder
); i
++)
622 enum afd_poll_bit event
= event_bitorder
[i
];
623 if (events
& (1 << event
))
625 lparam_t lparam
= afd_poll_flag_to_win32(1 << event
) | (sock_get_error( sock
->errors
[event
] ) << 16);
626 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
629 sock
->pending_events
= 0;
630 sock_reselect( sock
);
634 static inline int sock_error( struct fd
*fd
)
636 unsigned int optval
= 0;
637 socklen_t optlen
= sizeof(optval
);
639 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
643 static void free_accept_req( void *private )
645 struct accept_req
*req
= private;
646 list_remove( &req
->entry
);
649 req
->acceptsock
->accept_recv_req
= NULL
;
650 release_object( req
->acceptsock
);
652 release_object( req
->async
);
653 release_object( req
->iosb
);
654 release_object( req
->sock
);
658 static void fill_accept_output( struct accept_req
*req
)
660 const data_size_t out_size
= req
->iosb
->out_size
;
661 struct async
*async
= req
->async
;
662 union unix_sockaddr unix_addr
;
663 struct WS_sockaddr
*win_addr
;
664 unsigned int remote_len
;
670 if (!(out_data
= mem_alloc( out_size
)))
672 async_terminate( async
, get_error() );
676 fd
= get_unix_fd( req
->acceptsock
->fd
);
678 if (req
->recv_len
&& (size
= recv( fd
, out_data
, req
->recv_len
, 0 )) < 0)
680 if (!req
->accepted
&& errno
== EWOULDBLOCK
)
683 sock_reselect( req
->acceptsock
);
687 async_terminate( async
, sock_get_ntstatus( errno
) );
694 if (req
->local_len
< sizeof(int))
696 async_terminate( async
, STATUS_BUFFER_TOO_SMALL
);
701 unix_len
= sizeof(unix_addr
);
702 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ sizeof(int));
703 if (getsockname( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
704 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, req
->local_len
- sizeof(int) )) < 0)
706 async_terminate( async
, sock_get_ntstatus( errno
) );
710 memcpy( out_data
+ req
->recv_len
, &win_len
, sizeof(int) );
713 unix_len
= sizeof(unix_addr
);
714 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ req
->local_len
+ sizeof(int));
715 remote_len
= out_size
- req
->recv_len
- req
->local_len
;
716 if (getpeername( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
717 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, remote_len
- sizeof(int) )) < 0)
719 async_terminate( async
, sock_get_ntstatus( errno
) );
723 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
725 async_request_complete( req
->async
, STATUS_SUCCESS
, size
, out_size
, out_data
);
728 static void complete_async_accept( struct sock
*sock
, struct accept_req
*req
)
730 struct sock
*acceptsock
= req
->acceptsock
;
731 struct async
*async
= req
->async
;
733 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
737 if (!accept_into_socket( sock
, acceptsock
))
739 async_terminate( async
, get_error() );
742 fill_accept_output( req
);
748 if (!(acceptsock
= accept_socket( sock
)))
750 async_terminate( async
, get_error() );
753 handle
= alloc_handle_no_access_check( async_get_thread( async
)->process
, &acceptsock
->obj
,
754 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
755 acceptsock
->wparam
= handle
;
756 sock_reselect( acceptsock
);
757 release_object( acceptsock
);
760 async_terminate( async
, get_error() );
764 async_request_complete_alloc( req
->async
, STATUS_SUCCESS
, 0, sizeof(handle
), &handle
);
768 static void complete_async_accept_recv( struct accept_req
*req
)
770 if (debug_level
) fprintf( stderr
, "completing accept recv request for socket %p\n", req
->acceptsock
);
772 assert( req
->recv_len
);
774 fill_accept_output( req
);
777 static void free_connect_req( void *private )
779 struct connect_req
*req
= private;
781 req
->sock
->connect_req
= NULL
;
782 release_object( req
->async
);
783 release_object( req
->iosb
);
784 release_object( req
->sock
);
788 static void complete_async_connect( struct sock
*sock
)
790 struct connect_req
*req
= sock
->connect_req
;
791 const char *in_buffer
;
795 if (debug_level
) fprintf( stderr
, "completing connect request for socket %p\n", sock
);
797 sock
->state
= SOCK_CONNECTED
;
801 async_terminate( req
->async
, STATUS_SUCCESS
);
805 in_buffer
= (const char *)req
->iosb
->in_data
+ sizeof(struct afd_connect_params
) + req
->addr_len
;
806 len
= req
->send_len
- req
->send_cursor
;
808 ret
= send( get_unix_fd( sock
->fd
), in_buffer
+ req
->send_cursor
, len
, 0 );
809 if (ret
< 0 && errno
!= EWOULDBLOCK
)
810 async_terminate( req
->async
, sock_get_ntstatus( errno
) );
812 async_request_complete( req
->async
, STATUS_SUCCESS
, req
->send_len
, 0, NULL
);
814 req
->send_cursor
+= ret
;
817 static void free_poll_req( void *private )
819 struct poll_req
*req
= private;
822 if (req
->timeout
) remove_timeout_user( req
->timeout
);
824 for (i
= 0; i
< req
->count
; ++i
)
825 release_object( req
->sockets
[i
].sock
);
826 release_object( req
->async
);
827 release_object( req
->iosb
);
828 list_remove( &req
->entry
);
832 static int is_oobinline( struct sock
*sock
)
835 socklen_t len
= sizeof(oobinline
);
836 return !getsockopt( get_unix_fd( sock
->fd
), SOL_SOCKET
, SO_OOBINLINE
, (char *)&oobinline
, &len
) && oobinline
;
839 static int get_poll_flags( struct sock
*sock
, int event
)
843 /* A connection-mode socket which has never been connected does not return
844 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
845 if (sock
->state
== SOCK_UNCONNECTED
)
846 event
&= ~(POLLOUT
| POLLHUP
);
850 if (sock
->state
== SOCK_LISTENING
)
851 flags
|= AFD_POLL_ACCEPT
;
853 flags
|= AFD_POLL_READ
;
856 flags
|= is_oobinline( sock
) ? AFD_POLL_READ
: AFD_POLL_OOB
;
858 flags
|= AFD_POLL_WRITE
;
859 if (sock
->state
== SOCK_CONNECTED
)
860 flags
|= AFD_POLL_CONNECT
;
862 flags
|= AFD_POLL_HUP
;
864 flags
|= AFD_POLL_CONNECT_ERR
;
869 static void complete_async_poll( struct poll_req
*req
, unsigned int status
)
871 unsigned int i
, signaled_count
= 0;
873 for (i
= 0; i
< req
->count
; ++i
)
875 struct sock
*sock
= req
->sockets
[i
].sock
;
877 if (sock
->main_poll
== req
)
878 sock
->main_poll
= NULL
;
883 for (i
= 0; i
< req
->count
; ++i
)
885 if (req
->sockets
[i
].flags
)
890 if (is_machine_64bit( async_get_thread( req
->async
)->process
->machine
))
892 size_t output_size
= offsetof( struct afd_poll_params_64
, sockets
[signaled_count
] );
893 struct afd_poll_params_64
*output
;
895 if (!(output
= mem_alloc( output_size
)))
897 async_terminate( req
->async
, get_error() );
900 memset( output
, 0, output_size
);
901 output
->timeout
= req
->orig_timeout
;
902 output
->exclusive
= req
->exclusive
;
903 for (i
= 0; i
< req
->count
; ++i
)
905 if (!req
->sockets
[i
].flags
) continue;
906 output
->sockets
[output
->count
].socket
= req
->sockets
[i
].handle
;
907 output
->sockets
[output
->count
].flags
= req
->sockets
[i
].flags
;
908 output
->sockets
[output
->count
].status
= req
->sockets
[i
].status
;
911 assert( output
->count
== signaled_count
);
913 async_request_complete( req
->async
, status
, output_size
, output_size
, output
);
917 size_t output_size
= offsetof( struct afd_poll_params_32
, sockets
[signaled_count
] );
918 struct afd_poll_params_32
*output
;
920 if (!(output
= mem_alloc( output_size
)))
922 async_terminate( req
->async
, get_error() );
925 memset( output
, 0, output_size
);
926 output
->timeout
= req
->orig_timeout
;
927 output
->exclusive
= req
->exclusive
;
928 for (i
= 0; i
< req
->count
; ++i
)
930 if (!req
->sockets
[i
].flags
) continue;
931 output
->sockets
[output
->count
].socket
= req
->sockets
[i
].handle
;
932 output
->sockets
[output
->count
].flags
= req
->sockets
[i
].flags
;
933 output
->sockets
[output
->count
].status
= req
->sockets
[i
].status
;
936 assert( output
->count
== signaled_count
);
938 async_request_complete( req
->async
, status
, output_size
, output_size
, output
);
942 static void complete_async_polls( struct sock
*sock
, int event
, int error
)
944 int flags
= get_poll_flags( sock
, event
);
945 struct poll_req
*req
, *next
;
947 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &poll_list
, struct poll_req
, entry
)
951 if (req
->iosb
->status
!= STATUS_PENDING
) continue;
953 for (i
= 0; i
< req
->count
; ++i
)
955 if (req
->sockets
[i
].sock
!= sock
) continue;
956 if (!(req
->sockets
[i
].mask
& flags
)) continue;
959 fprintf( stderr
, "completing poll for socket %p, wanted %#x got %#x\n",
960 sock
, req
->sockets
[i
].mask
, flags
);
962 req
->sockets
[i
].flags
= req
->sockets
[i
].mask
& flags
;
963 req
->sockets
[i
].status
= sock_get_ntstatus( error
);
965 complete_async_poll( req
, STATUS_SUCCESS
);
971 static void async_poll_timeout( void *private )
973 struct poll_req
*req
= private;
977 if (req
->iosb
->status
!= STATUS_PENDING
) return;
979 complete_async_poll( req
, STATUS_TIMEOUT
);
982 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
984 if (event
& (POLLIN
| POLLPRI
))
986 struct accept_req
*req
;
988 LIST_FOR_EACH_ENTRY( req
, &sock
->accept_list
, struct accept_req
, entry
)
990 if (req
->iosb
->status
== STATUS_PENDING
&& !req
->accepted
)
992 complete_async_accept( sock
, req
);
997 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
998 complete_async_accept_recv( sock
->accept_recv_req
);
1001 if ((event
& POLLOUT
) && sock
->connect_req
&& sock
->connect_req
->iosb
->status
== STATUS_PENDING
)
1002 complete_async_connect( sock
);
1004 if (event
& (POLLIN
| POLLPRI
) && async_waiting( &sock
->read_q
))
1006 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
1007 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
1008 event
&= ~(POLLIN
| POLLPRI
);
1011 if (event
& POLLOUT
&& async_waiting( &sock
->write_q
))
1013 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
1014 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
1018 if (event
& (POLLERR
| POLLHUP
))
1020 int status
= sock_get_ntstatus( error
);
1021 struct accept_req
*req
, *next
;
1023 if (sock
->rd_shutdown
|| sock
->hangup
)
1024 async_wake_up( &sock
->read_q
, status
);
1025 if (sock
->wr_shutdown
)
1026 async_wake_up( &sock
->write_q
, status
);
1028 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
1030 if (req
->iosb
->status
== STATUS_PENDING
)
1031 async_terminate( req
->async
, status
);
1034 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
1035 async_terminate( sock
->accept_recv_req
->async
, status
);
1037 if (sock
->connect_req
)
1038 async_terminate( sock
->connect_req
->async
, status
);
1044 static void post_socket_event( struct sock
*sock
, enum afd_poll_bit event_bit
, int error
)
1046 unsigned int event
= (1 << event_bit
);
1048 if (!(sock
->reported_events
& event
))
1050 sock
->pending_events
|= event
;
1051 sock
->reported_events
|= event
;
1052 sock
->errors
[event_bit
] = error
;
1056 static void sock_dispatch_events( struct sock
*sock
, enum connection_state prevstate
, int event
, int error
)
1060 case SOCK_UNCONNECTED
:
1063 case SOCK_CONNECTING
:
1064 if (event
& POLLOUT
)
1066 post_socket_event( sock
, AFD_POLL_BIT_CONNECT
, 0 );
1067 sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] = 0;
1069 if (event
& (POLLERR
| POLLHUP
))
1070 post_socket_event( sock
, AFD_POLL_BIT_CONNECT_ERR
, error
);
1073 case SOCK_LISTENING
:
1074 if (event
& (POLLIN
| POLLERR
| POLLHUP
))
1075 post_socket_event( sock
, AFD_POLL_BIT_ACCEPT
, error
);
1078 case SOCK_CONNECTED
:
1079 case SOCK_CONNECTIONLESS
:
1081 post_socket_event( sock
, AFD_POLL_BIT_READ
, 0 );
1083 if (event
& POLLOUT
)
1084 post_socket_event( sock
, AFD_POLL_BIT_WRITE
, 0 );
1086 if (event
& POLLPRI
)
1087 post_socket_event( sock
, AFD_POLL_BIT_OOB
, 0 );
1089 if (event
& (POLLERR
| POLLHUP
))
1090 post_socket_event( sock
, AFD_POLL_BIT_HUP
, error
);
1094 sock_wake_up( sock
);
1097 static void sock_poll_event( struct fd
*fd
, int event
)
1099 struct sock
*sock
= get_fd_user( fd
);
1100 int hangup_seen
= 0;
1101 enum connection_state prevstate
= sock
->state
;
1104 assert( sock
->obj
.ops
== &sock_ops
);
1106 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
1108 /* we may change event later, remove from loop here */
1109 if (event
& (POLLERR
|POLLHUP
)) set_fd_events( sock
->fd
, -1 );
1111 switch (sock
->state
)
1113 case SOCK_UNCONNECTED
:
1116 case SOCK_CONNECTING
:
1117 if (event
& (POLLERR
|POLLHUP
))
1119 sock
->state
= SOCK_UNCONNECTED
;
1121 error
= sock_error( fd
);
1123 else if (event
& POLLOUT
)
1125 sock
->state
= SOCK_CONNECTED
;
1126 sock
->connect_time
= current_time
;
1130 case SOCK_LISTENING
:
1131 if (event
& (POLLERR
|POLLHUP
))
1132 error
= sock_error( fd
);
1135 case SOCK_CONNECTED
:
1136 case SOCK_CONNECTIONLESS
:
1137 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
1142 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
1143 * has been closed, so we need to check for it explicitly here */
1144 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
1153 /* EAGAIN can happen if an async recv() falls between the server's poll()
1154 call and the invocation of this routine */
1155 if ( errno
!= EAGAIN
)
1160 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
1165 if (hangup_seen
|| (sock_shutdown_type
== SOCK_SHUTDOWN_POLLHUP
&& (event
& POLLHUP
)))
1169 else if (event
& (POLLHUP
| POLLERR
))
1174 fprintf( stderr
, "socket %p aborted by error %d, event %#x\n", sock
, error
, event
);
1182 complete_async_polls( sock
, event
, error
);
1184 event
= sock_dispatch_asyncs( sock
, event
, error
);
1185 sock_dispatch_events( sock
, prevstate
, event
, error
);
1187 sock_reselect( sock
);
1190 static void sock_dump( struct object
*obj
, int verbose
)
1192 struct sock
*sock
= (struct sock
*)obj
;
1193 assert( obj
->ops
== &sock_ops
);
1194 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
1195 sock
->fd
, sock
->state
,
1196 sock
->mask
, sock
->pending_events
, sock
->reported_events
);
1199 static int poll_flags_from_afd( struct sock
*sock
, int flags
)
1203 /* A connection-mode socket which has never been connected does
1204 * not return write or hangup events, but Linux returns
1205 * POLLOUT | POLLHUP. */
1206 if (sock
->state
== SOCK_UNCONNECTED
)
1209 if (flags
& (AFD_POLL_READ
| AFD_POLL_ACCEPT
))
1211 if ((flags
& AFD_POLL_HUP
) && sock
->type
== WS_SOCK_STREAM
)
1213 if (flags
& AFD_POLL_OOB
)
1214 ev
|= is_oobinline( sock
) ? POLLIN
: POLLPRI
;
1215 if (flags
& AFD_POLL_WRITE
)
1221 static int sock_get_poll_events( struct fd
*fd
)
1223 struct sock
*sock
= get_fd_user( fd
);
1224 unsigned int mask
= sock
->mask
& ~sock
->reported_events
;
1225 struct poll_req
*req
;
1228 assert( sock
->obj
.ops
== &sock_ops
);
1230 if (!sock
->type
) /* not initialized yet */
1233 switch (sock
->state
)
1235 case SOCK_UNCONNECTED
:
1236 /* A connection-mode Windows socket which has never been connected does
1237 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1238 * need to return -1 here, to prevent the socket from being polled on at
1242 case SOCK_CONNECTING
:
1245 case SOCK_LISTENING
:
1246 if (!list_empty( &sock
->accept_list
) || (mask
& AFD_POLL_ACCEPT
))
1250 case SOCK_CONNECTED
:
1251 case SOCK_CONNECTIONLESS
:
1252 if (sock
->hangup
&& sock
->wr_shutdown
&& !sock
->wr_shutdown_pending
)
1254 /* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
1255 * if both the socket and its peer are SHUT_WR.
1257 * We don't use SHUT_RD, so we can only encounter this in the latter
1258 * case. In that case there can't be any pending read requests (they
1259 * would have already been completed with a length of zero), the
1260 * above condition ensures that we don't have any pending write
1261 * requests, and nothing that can change about the socket state that
1262 * would complete a pending poll request. */
1269 if (sock
->accept_recv_req
)
1273 else if (async_queued( &sock
->read_q
))
1275 if (async_waiting( &sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
1279 /* Don't ask for POLLIN if we got a hangup. We won't receive more
1280 * data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
1283 if (mask
& AFD_POLL_READ
)
1285 if (mask
& AFD_POLL_OOB
)
1289 /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
1290 if (sock
->state
== SOCK_CONNECTED
&& (mask
& AFD_POLL_HUP
) && !(sock
->reported_events
& AFD_POLL_READ
))
1294 if (async_queued( &sock
->write_q
))
1296 if (async_waiting( &sock
->write_q
)) ev
|= POLLOUT
;
1298 else if (!sock
->wr_shutdown
&& (mask
& AFD_POLL_WRITE
))
1306 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1310 for (i
= 0; i
< req
->count
; ++i
)
1312 if (req
->sockets
[i
].sock
!= sock
) continue;
1314 ev
|= poll_flags_from_afd( sock
, req
->sockets
[i
].mask
);
1321 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
1323 return FD_TYPE_SOCKET
;
1326 static void sock_cancel_async( struct fd
*fd
, struct async
*async
)
1328 struct poll_req
*req
;
1330 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1334 if (req
->async
!= async
)
1337 for (i
= 0; i
< req
->count
; i
++)
1339 struct sock
*sock
= req
->sockets
[i
].sock
;
1341 if (sock
->main_poll
== req
)
1342 sock
->main_poll
= NULL
;
1346 async_terminate( async
, STATUS_CANCELLED
);
1349 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
1351 struct sock
*sock
= get_fd_user( fd
);
1352 struct async_queue
*queue
;
1354 assert( sock
->obj
.ops
== &sock_ops
);
1358 case ASYNC_TYPE_READ
:
1359 if (sock
->rd_shutdown
)
1361 set_error( STATUS_PIPE_DISCONNECTED
);
1364 queue
= &sock
->read_q
;
1367 case ASYNC_TYPE_WRITE
:
1368 if (sock
->wr_shutdown
)
1370 set_error( STATUS_PIPE_DISCONNECTED
);
1373 queue
= &sock
->write_q
;
1377 set_error( STATUS_INVALID_PARAMETER
);
1381 if (sock
->state
!= SOCK_CONNECTED
)
1383 set_error( STATUS_PIPE_DISCONNECTED
);
1387 queue_async( queue
, async
);
1388 sock_reselect( sock
);
1390 set_error( STATUS_PENDING
);
1393 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
1395 struct sock
*sock
= get_fd_user( fd
);
1397 if (sock
->wr_shutdown_pending
&& list_empty( &sock
->write_q
.queue
))
1399 shutdown( get_unix_fd( sock
->fd
), SHUT_WR
);
1400 sock
->wr_shutdown_pending
= 0;
1403 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1404 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1406 if (queue
!= &sock
->ifchange_q
&& sock
->type
)
1407 sock_reselect( sock
);
1410 static struct fd
*sock_get_fd( struct object
*obj
)
1412 struct sock
*sock
= (struct sock
*)obj
;
1413 return (struct fd
*)grab_object( sock
->fd
);
1416 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
1418 struct sock
*sock
= (struct sock
*)obj
;
1420 if (sock
->obj
.handle_count
== 1) /* last handle */
1422 struct accept_req
*accept_req
, *accept_next
;
1423 struct poll_req
*poll_req
, *poll_next
;
1425 if (sock
->accept_recv_req
)
1426 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
1428 LIST_FOR_EACH_ENTRY_SAFE( accept_req
, accept_next
, &sock
->accept_list
, struct accept_req
, entry
)
1429 async_terminate( accept_req
->async
, STATUS_CANCELLED
);
1431 if (sock
->connect_req
)
1432 async_terminate( sock
->connect_req
->async
, STATUS_CANCELLED
);
1434 LIST_FOR_EACH_ENTRY_SAFE( poll_req
, poll_next
, &poll_list
, struct poll_req
, entry
)
1436 struct iosb
*iosb
= poll_req
->iosb
;
1437 BOOL signaled
= FALSE
;
1440 if (iosb
->status
!= STATUS_PENDING
) continue;
1442 for (i
= 0; i
< poll_req
->count
; ++i
)
1444 if (poll_req
->sockets
[i
].sock
== sock
)
1447 poll_req
->sockets
[i
].flags
= AFD_POLL_CLOSE
;
1448 poll_req
->sockets
[i
].status
= 0;
1452 if (signaled
) complete_async_poll( poll_req
, STATUS_SUCCESS
);
1459 static void sock_destroy( struct object
*obj
)
1461 struct sock
*sock
= (struct sock
*)obj
;
1463 assert( obj
->ops
== &sock_ops
);
1465 /* FIXME: special socket shutdown stuff? */
1467 if ( sock
->deferred
)
1468 release_object( sock
->deferred
);
1470 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
1471 sock_release_ifchange( sock
);
1472 free_async_queue( &sock
->read_q
);
1473 free_async_queue( &sock
->write_q
);
1474 free_async_queue( &sock
->ifchange_q
);
1475 free_async_queue( &sock
->accept_q
);
1476 free_async_queue( &sock
->connect_q
);
1477 free_async_queue( &sock
->poll_q
);
1478 if (sock
->event
) release_object( sock
->event
);
1481 /* shut the socket down to force pending poll() calls in the client to return */
1482 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
1483 release_object( sock
->fd
);
1487 static struct sock
*create_socket(void)
1491 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
1493 sock
->state
= SOCK_UNCONNECTED
;
1495 sock
->pending_events
= 0;
1496 sock
->reported_events
= 0;
1505 sock
->connect_time
= 0;
1506 sock
->deferred
= NULL
;
1507 sock
->ifchange_obj
= NULL
;
1508 sock
->accept_recv_req
= NULL
;
1509 sock
->connect_req
= NULL
;
1510 sock
->main_poll
= NULL
;
1511 memset( &sock
->addr
, 0, sizeof(sock
->addr
) );
1513 sock
->rd_shutdown
= 0;
1514 sock
->wr_shutdown
= 0;
1515 sock
->wr_shutdown_pending
= 0;
1518 sock
->nonblocking
= 0;
1524 init_async_queue( &sock
->read_q
);
1525 init_async_queue( &sock
->write_q
);
1526 init_async_queue( &sock
->ifchange_q
);
1527 init_async_queue( &sock
->accept_q
);
1528 init_async_queue( &sock
->connect_q
);
1529 init_async_queue( &sock
->poll_q
);
1530 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
1531 list_init( &sock
->accept_list
);
1535 static int get_unix_family( int family
)
1539 case WS_AF_INET
: return AF_INET
;
1540 case WS_AF_INET6
: return AF_INET6
;
1542 case WS_AF_IPX
: return AF_IPX
;
1545 case WS_AF_IRDA
: return AF_IRDA
;
1547 case WS_AF_UNSPEC
: return AF_UNSPEC
;
1552 static int get_unix_type( int type
)
1556 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
1557 case WS_SOCK_RAW
: return SOCK_RAW
;
1558 case WS_SOCK_STREAM
: return SOCK_STREAM
;
1563 static int get_unix_protocol( int protocol
)
1565 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1570 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
1571 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1572 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1573 case WS_IPPROTO_IPV4
: return IPPROTO_IPIP
;
1574 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1575 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1576 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1577 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1582 static void set_dont_fragment( int fd
, int level
, int value
)
1586 if (level
== IPPROTO_IP
)
1589 optname
= IP_DONTFRAG
;
1590 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1591 optname
= IP_MTU_DISCOVER
;
1592 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1599 #ifdef IPV6_DONTFRAG
1600 optname
= IPV6_DONTFRAG
;
1601 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1602 optname
= IPV6_MTU_DISCOVER
;
1603 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1609 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1612 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
, unsigned int flags
)
1614 unsigned int options
= 0;
1615 int sockfd
, unix_type
, unix_family
, unix_protocol
, value
;
1618 unix_family
= get_unix_family( family
);
1619 unix_type
= get_unix_type( type
);
1620 unix_protocol
= get_unix_protocol( protocol
);
1622 if (unix_protocol
< 0)
1624 if (type
&& unix_type
< 0)
1625 set_win32_error( WSAESOCKTNOSUPPORT
);
1627 set_win32_error( WSAEPROTONOSUPPORT
);
1630 if (unix_family
< 0)
1632 if (family
>= 0 && unix_type
< 0)
1633 set_win32_error( WSAESOCKTNOSUPPORT
);
1635 set_win32_error( WSAEAFNOSUPPORT
);
1639 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1642 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1643 else set_win32_error( sock_get_error( errno
));
1646 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1648 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1651 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1654 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1657 /* Should we retrieve val using a getsockopt call and then
1658 * set the modified one? */
1659 val
.ipx_pt
= ipx_type
;
1660 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1665 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1667 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1668 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1669 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1670 else if (unix_type
== SOCK_STREAM
)
1671 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1675 if (unix_family
== AF_INET6
)
1677 static const int enable
= 1;
1678 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1682 len
= sizeof(value
);
1683 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_RCVBUF
, &value
, &len
))
1684 sock
->rcvbuf
= value
;
1686 len
= sizeof(value
);
1687 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_SNDBUF
, &value
, &len
))
1688 sock
->sndbuf
= value
;
1690 sock
->state
= (type
== WS_SOCK_STREAM
? SOCK_UNCONNECTED
: SOCK_CONNECTIONLESS
);
1691 sock
->flags
= flags
;
1692 sock
->proto
= protocol
;
1694 sock
->family
= family
;
1698 options
= get_fd_options( sock
->fd
);
1699 release_object( sock
->fd
);
1702 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1707 /* We can't immediately allow caching for a connection-mode socket, since it
1708 * might be accepted into (changing the underlying fd object.) */
1709 if (sock
->type
!= WS_SOCK_STREAM
) allow_fd_caching( sock
->fd
);
1714 /* accepts a socket and inits it */
1715 static int accept_new_fd( struct sock
*sock
)
1718 /* Try to accept(2). We can't be safe that this an already connected socket
1719 * or that accept() is allowed on it. In those cases we will get -1/errno
1722 struct sockaddr saddr
;
1723 socklen_t slen
= sizeof(saddr
);
1724 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1726 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1728 set_error( sock_get_ntstatus( errno
));
1732 /* accept a socket (creates a new fd) */
1733 static struct sock
*accept_socket( struct sock
*sock
)
1735 struct sock
*acceptsock
;
1738 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1740 if ( sock
->deferred
)
1742 acceptsock
= sock
->deferred
;
1743 sock
->deferred
= NULL
;
1747 union unix_sockaddr unix_addr
;
1750 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1751 if (!(acceptsock
= create_socket()))
1757 /* newly created socket gets the same properties of the listening socket */
1758 acceptsock
->state
= SOCK_CONNECTED
;
1759 acceptsock
->bound
= 1;
1760 acceptsock
->nonblocking
= sock
->nonblocking
;
1761 acceptsock
->mask
= sock
->mask
;
1762 acceptsock
->proto
= sock
->proto
;
1763 acceptsock
->type
= sock
->type
;
1764 acceptsock
->family
= sock
->family
;
1765 acceptsock
->window
= sock
->window
;
1766 acceptsock
->message
= sock
->message
;
1767 acceptsock
->connect_time
= current_time
;
1768 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
1769 acceptsock
->flags
= sock
->flags
;
1770 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1771 get_fd_options( sock
->fd
) )))
1773 release_object( acceptsock
);
1776 unix_len
= sizeof(unix_addr
);
1777 if (!getsockname( acceptfd
, &unix_addr
.addr
, &unix_len
))
1778 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1781 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1782 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1783 sock_reselect( sock
);
1787 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
1789 union unix_sockaddr unix_addr
;
1794 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
1796 if ( sock
->deferred
)
1798 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
1799 get_fd_options( acceptsock
->fd
) );
1803 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
1805 release_object( sock
->deferred
);
1806 sock
->deferred
= NULL
;
1810 if ((acceptfd
= accept_new_fd( sock
)) == -1)
1813 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1814 get_fd_options( acceptsock
->fd
) )))
1818 acceptsock
->state
= SOCK_CONNECTED
;
1819 acceptsock
->pending_events
= 0;
1820 acceptsock
->reported_events
= 0;
1821 acceptsock
->proto
= sock
->proto
;
1822 acceptsock
->type
= sock
->type
;
1823 acceptsock
->family
= sock
->family
;
1824 acceptsock
->wparam
= 0;
1825 acceptsock
->deferred
= NULL
;
1826 acceptsock
->connect_time
= current_time
;
1827 fd_copy_completion( acceptsock
->fd
, newfd
);
1828 release_object( acceptsock
->fd
);
1829 acceptsock
->fd
= newfd
;
1831 unix_len
= sizeof(unix_addr
);
1832 if (!getsockname( get_unix_fd( newfd
), &unix_addr
.addr
, &unix_len
))
1833 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1836 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1837 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1838 sock_reselect( sock
);
1845 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
1847 static const int enable
= 1;
1850 if (!(index
= if_nametoindex( name
)))
1853 if (setsockopt( fd
, IPPROTO_IP
, IP_BOUND_IF
, &index
, sizeof(index
) ))
1856 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
1859 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER) && defined(SO_BINDTODEVICE)
1861 struct interface_filter
1863 struct sock_filter iface_memaddr
;
1864 struct sock_filter iface_rule
;
1865 struct sock_filter ip_memaddr
;
1866 struct sock_filter ip_rule
;
1867 struct sock_filter return_keep
;
1868 struct sock_filter return_dump
;
1870 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
1871 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1872 /sizeof(struct sock_filter)
1873 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
1874 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1875 /sizeof(struct sock_filter)
1876 # define FILTER_JUMP_NEXT() (u_char)(0)
1877 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
1878 static struct interface_filter generic_interface_filter
=
1880 /* This filter rule allows incoming packets on the specified interface, which works for all
1881 * remotely generated packets and for locally generated broadcast packets. */
1882 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
1883 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
1884 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
1885 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
1886 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
1887 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
1888 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
1889 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
1892 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
1894 struct interface_filter specific_interface_filter
;
1895 struct sock_fprog filter_prog
;
1896 static const int enable
= 1;
1900 if (!setsockopt( fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen( name
) + 1 ))
1903 /* SO_BINDTODEVICE requires NET_CAP_RAW until Linux 5.7. */
1905 fprintf( stderr
, "setsockopt SO_BINDTODEVICE fd %d, name %s failed: %s, falling back to SO_REUSE_ADDR\n",
1906 fd
, name
, strerror( errno
));
1908 if (!(index
= if_nametoindex( name
)))
1911 ifindex
= htonl( index
);
1912 if (setsockopt( fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
) ) < 0)
1915 specific_interface_filter
= generic_interface_filter
;
1916 specific_interface_filter
.iface_rule
.k
= index
;
1917 specific_interface_filter
.ip_rule
.k
= htonl( bind_addr
);
1918 filter_prog
.len
= sizeof(generic_interface_filter
) / sizeof(struct sock_filter
);
1919 filter_prog
.filter
= (struct sock_filter
*)&specific_interface_filter
;
1920 if (setsockopt( fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
) ))
1923 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
1928 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
1934 #endif /* LINUX_BOUND_IF */
1936 /* Take bind() calls on any name corresponding to a local network adapter and
1937 * restrict the given socket to operating only on the specified interface. This
1938 * restriction consists of two components:
1939 * 1) An outgoing packet restriction suggesting the egress interface for all
1941 * 2) An incoming packet restriction dropping packets not meant for the
1943 * If the function succeeds in placing these restrictions, then the name for the
1944 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
1945 * receipt of broadcast packets on the socket. This behavior is only relevant to
1946 * UDP sockets and is needed for applications that expect to be able to receive
1947 * broadcast packets on a socket that is bound to a specific network interface.
1949 static int bind_to_interface( struct sock
*sock
, const struct sockaddr_in
*addr
)
1951 in_addr_t bind_addr
= addr
->sin_addr
.s_addr
;
1952 struct ifaddrs
*ifaddrs
, *ifaddr
;
1953 int fd
= get_unix_fd( sock
->fd
);
1956 if (bind_addr
== htonl( INADDR_ANY
) || bind_addr
== htonl( INADDR_LOOPBACK
))
1958 if (sock
->type
!= WS_SOCK_DGRAM
)
1961 if (getifaddrs( &ifaddrs
) < 0) return 0;
1963 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
1965 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET
1966 && ((struct sockaddr_in
*)ifaddr
->ifa_addr
)->sin_addr
.s_addr
== bind_addr
)
1968 if ((err
= bind_to_iface_name( fd
, bind_addr
, ifaddr
->ifa_name
)) < 0)
1971 fprintf( stderr
, "failed to bind to interface: %s\n", strerror( errno
) );
1976 freeifaddrs( ifaddrs
);
1980 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1981 static unsigned int get_ipv6_interface_index( const struct in6_addr
*addr
)
1983 struct ifaddrs
*ifaddrs
, *ifaddr
;
1985 if (getifaddrs( &ifaddrs
) < 0) return 0;
1987 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
1989 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET6
1990 && !memcmp( &((struct sockaddr_in6
*)ifaddr
->ifa_addr
)->sin6_addr
, addr
, sizeof(*addr
) ))
1992 unsigned int index
= if_nametoindex( ifaddr
->ifa_name
);
1997 fprintf( stderr
, "Unable to look up interface index for %s: %s\n",
1998 ifaddr
->ifa_name
, strerror( errno
) );
2002 freeifaddrs( ifaddrs
);
2007 freeifaddrs( ifaddrs
);
2012 /* return an errno value mapped to a WSA error */
2013 static unsigned int sock_get_error( int err
)
2017 case EINTR
: return WSAEINTR
;
2018 case EBADF
: return WSAEBADF
;
2020 case EACCES
: return WSAEACCES
;
2021 case EFAULT
: return WSAEFAULT
;
2022 case EINVAL
: return WSAEINVAL
;
2023 case EMFILE
: return WSAEMFILE
;
2025 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
2026 case EALREADY
: return WSAEALREADY
;
2027 case ENOTSOCK
: return WSAENOTSOCK
;
2028 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
2029 case EMSGSIZE
: return WSAEMSGSIZE
;
2030 case EPROTOTYPE
: return WSAEPROTOTYPE
;
2031 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
2032 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
2033 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
2034 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
2035 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
2036 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
2037 case EADDRINUSE
: return WSAEADDRINUSE
;
2038 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
2039 case ENETDOWN
: return WSAENETDOWN
;
2040 case ENETUNREACH
: return WSAENETUNREACH
;
2041 case ENETRESET
: return WSAENETRESET
;
2042 case ECONNABORTED
: return WSAECONNABORTED
;
2044 case ECONNRESET
: return WSAECONNRESET
;
2045 case ENOBUFS
: return WSAENOBUFS
;
2046 case EISCONN
: return WSAEISCONN
;
2047 case ENOTCONN
: return WSAENOTCONN
;
2048 case ESHUTDOWN
: return WSAESHUTDOWN
;
2049 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
2050 case ETIMEDOUT
: return WSAETIMEDOUT
;
2051 case ECONNREFUSED
: return WSAECONNREFUSED
;
2052 case ELOOP
: return WSAELOOP
;
2053 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
2054 case EHOSTDOWN
: return WSAEHOSTDOWN
;
2055 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
2056 case ENOTEMPTY
: return WSAENOTEMPTY
;
2058 case EPROCLIM
: return WSAEPROCLIM
;
2061 case EUSERS
: return WSAEUSERS
;
2064 case EDQUOT
: return WSAEDQUOT
;
2067 case ESTALE
: return WSAESTALE
;
2070 case EREMOTE
: return WSAEREMOTE
;
2076 perror("wineserver: sock_get_error() can't map error");
2081 static int sock_get_ntstatus( int err
)
2085 case EBADF
: return STATUS_INVALID_HANDLE
;
2086 case EBUSY
: return STATUS_DEVICE_BUSY
;
2088 case EACCES
: return STATUS_ACCESS_DENIED
;
2089 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
2090 case EINVAL
: return STATUS_INVALID_PARAMETER
;
2092 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
2094 case EWOULDBLOCK
: return STATUS_DEVICE_NOT_READY
;
2095 case EALREADY
: return STATUS_NETWORK_BUSY
;
2096 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
2097 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
2098 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
2099 case EPROTONOSUPPORT
:
2100 case ESOCKTNOSUPPORT
:
2103 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
2104 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
2105 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
2106 case EADDRINUSE
: return STATUS_SHARING_VIOLATION
;
2107 /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
2108 * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
2110 case EADDRNOTAVAIL
: return STATUS_INVALID_ADDRESS_COMPONENT
;
2111 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
2112 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
2113 case ENOTCONN
: return STATUS_INVALID_CONNECTION
;
2114 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
2115 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
2116 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
2117 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
2119 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
2120 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
2121 case EISCONN
: return STATUS_CONNECTION_ACTIVE
;
2123 case 0: return STATUS_SUCCESS
;
2126 perror("wineserver: sock_get_ntstatus() can't map error");
2127 return STATUS_UNSUCCESSFUL
;
2131 static struct accept_req
*alloc_accept_req( struct sock
*sock
, struct sock
*acceptsock
, struct async
*async
,
2132 const struct afd_accept_into_params
*params
)
2134 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
2138 req
->async
= (struct async
*)grab_object( async
);
2139 req
->iosb
= async_get_iosb( async
);
2140 req
->sock
= (struct sock
*)grab_object( sock
);
2141 req
->acceptsock
= acceptsock
;
2142 if (acceptsock
) grab_object( acceptsock
);
2148 req
->recv_len
= params
->recv_len
;
2149 req
->local_len
= params
->local_len
;
2155 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2157 struct sock
*sock
= get_fd_user( fd
);
2160 assert( sock
->obj
.ops
== &sock_ops
);
2162 if (code
!= IOCTL_AFD_WINE_CREATE
&& (unix_fd
= get_unix_fd( fd
)) < 0) return;
2166 case IOCTL_AFD_WINE_CREATE
:
2168 const struct afd_create_params
*params
= get_req_data();
2170 if (get_req_data_size() != sizeof(*params
))
2172 set_error( STATUS_INVALID_PARAMETER
);
2175 init_socket( sock
, params
->family
, params
->type
, params
->protocol
, params
->flags
);
2179 case IOCTL_AFD_WINE_ACCEPT
:
2181 struct sock
*acceptsock
;
2182 obj_handle_t handle
;
2184 if (get_reply_max_size() != sizeof(handle
))
2186 set_error( STATUS_BUFFER_TOO_SMALL
);
2190 if (!(acceptsock
= accept_socket( sock
)))
2192 struct accept_req
*req
;
2194 if (sock
->nonblocking
) return;
2195 if (get_error() != STATUS_DEVICE_NOT_READY
) return;
2197 if (!(req
= alloc_accept_req( sock
, NULL
, async
, NULL
))) return;
2198 list_add_tail( &sock
->accept_list
, &req
->entry
);
2200 async_set_completion_callback( async
, free_accept_req
, req
);
2201 queue_async( &sock
->accept_q
, async
);
2202 sock_reselect( sock
);
2203 set_error( STATUS_PENDING
);
2206 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
2207 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
2208 acceptsock
->wparam
= handle
;
2209 sock_reselect( acceptsock
);
2210 release_object( acceptsock
);
2211 set_reply_data( &handle
, sizeof(handle
) );
2215 case IOCTL_AFD_WINE_ACCEPT_INTO
:
2217 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
2218 const struct afd_accept_into_params
*params
= get_req_data();
2219 struct sock
*acceptsock
;
2220 unsigned int remote_len
;
2221 struct accept_req
*req
;
2223 if (get_req_data_size() != sizeof(*params
) ||
2224 get_reply_max_size() < params
->recv_len
||
2225 get_reply_max_size() - params
->recv_len
< params
->local_len
)
2227 set_error( STATUS_BUFFER_TOO_SMALL
);
2231 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
2232 if (remote_len
< sizeof(int))
2234 set_error( STATUS_INVALID_PARAMETER
);
2238 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
2241 if (acceptsock
->accept_recv_req
)
2243 release_object( acceptsock
);
2244 set_error( STATUS_INVALID_PARAMETER
);
2248 if (!(req
= alloc_accept_req( sock
, acceptsock
, async
, params
)))
2250 release_object( acceptsock
);
2253 list_add_tail( &sock
->accept_list
, &req
->entry
);
2254 acceptsock
->accept_recv_req
= req
;
2255 release_object( acceptsock
);
2257 acceptsock
->wparam
= params
->accept_handle
;
2258 async_set_completion_callback( async
, free_accept_req
, req
);
2259 queue_async( &sock
->accept_q
, async
);
2260 sock_reselect( sock
);
2261 set_error( STATUS_PENDING
);
2265 case IOCTL_AFD_LISTEN
:
2267 const struct afd_listen_params
*params
= get_req_data();
2269 if (get_req_data_size() < sizeof(*params
))
2271 set_error( STATUS_INVALID_PARAMETER
);
2277 set_error( STATUS_INVALID_PARAMETER
);
2281 if (listen( unix_fd
, params
->backlog
) < 0)
2283 set_error( sock_get_ntstatus( errno
) );
2287 sock
->state
= SOCK_LISTENING
;
2289 /* a listening socket can no longer be accepted into */
2290 allow_fd_caching( sock
->fd
);
2292 /* we may already be selecting for AFD_POLL_ACCEPT */
2293 sock_reselect( sock
);
2297 case IOCTL_AFD_WINE_CONNECT
:
2299 const struct afd_connect_params
*params
= get_req_data();
2300 const struct WS_sockaddr
*addr
;
2301 union unix_sockaddr unix_addr
;
2302 struct connect_req
*req
;
2306 if (get_req_data_size() < sizeof(*params
) ||
2307 get_req_data_size() - sizeof(*params
) < params
->addr_len
)
2309 set_error( STATUS_BUFFER_TOO_SMALL
);
2312 send_len
= get_req_data_size() - sizeof(*params
) - params
->addr_len
;
2313 addr
= (const struct WS_sockaddr
*)(params
+ 1);
2315 if (!params
->synchronous
&& !sock
->bound
)
2317 set_error( STATUS_INVALID_PARAMETER
);
2321 if (sock
->accept_recv_req
)
2323 set_error( STATUS_INVALID_PARAMETER
);
2327 if (sock
->connect_req
)
2329 set_error( STATUS_INVALID_PARAMETER
);
2333 switch (sock
->state
)
2335 case SOCK_LISTENING
:
2336 set_error( STATUS_INVALID_PARAMETER
);
2339 case SOCK_CONNECTING
:
2340 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2341 * but there's no status code that maps to WSAEALREADY... */
2342 set_error( params
->synchronous
? STATUS_ADDRESS_ALREADY_ASSOCIATED
: STATUS_INVALID_PARAMETER
);
2345 case SOCK_CONNECTED
:
2346 set_error( STATUS_CONNECTION_ACTIVE
);
2349 case SOCK_UNCONNECTED
:
2350 case SOCK_CONNECTIONLESS
:
2354 unix_len
= sockaddr_to_unix( addr
, params
->addr_len
, &unix_addr
);
2357 set_error( STATUS_INVALID_ADDRESS
);
2360 if (unix_addr
.addr
.sa_family
== AF_INET
&& !memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 ))
2361 unix_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_LOOPBACK
);
2363 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2364 if (ret
< 0 && errno
!= EINPROGRESS
)
2366 set_error( sock_get_ntstatus( errno
) );
2370 /* a connected or connecting socket can no longer be accepted into */
2371 allow_fd_caching( sock
->fd
);
2373 unix_len
= sizeof(unix_addr
);
2374 if (!getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
))
2375 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2380 sock
->state
= SOCK_CONNECTED
;
2382 if (!send_len
) return;
2385 sock
->state
= SOCK_CONNECTING
;
2387 if (params
->synchronous
&& sock
->nonblocking
)
2389 sock_reselect( sock
);
2390 set_error( STATUS_DEVICE_NOT_READY
);
2394 if (!(req
= mem_alloc( sizeof(*req
) )))
2397 req
->async
= (struct async
*)grab_object( async
);
2398 req
->iosb
= async_get_iosb( async
);
2399 req
->sock
= (struct sock
*)grab_object( sock
);
2400 req
->addr_len
= params
->addr_len
;
2401 req
->send_len
= send_len
;
2402 req
->send_cursor
= 0;
2404 async_set_completion_callback( async
, free_connect_req
, req
);
2405 sock
->connect_req
= req
;
2406 queue_async( &sock
->connect_q
, async
);
2407 sock_reselect( sock
);
2408 set_error( STATUS_PENDING
);
2412 case IOCTL_AFD_WINE_SHUTDOWN
:
2416 if (get_req_data_size() < sizeof(int))
2418 set_error( STATUS_BUFFER_TOO_SMALL
);
2421 how
= *(int *)get_req_data();
2425 set_error( STATUS_INVALID_PARAMETER
);
2429 if (sock
->state
!= SOCK_CONNECTED
&& sock
->state
!= SOCK_CONNECTIONLESS
)
2431 set_error( STATUS_INVALID_CONNECTION
);
2437 sock
->rd_shutdown
= 1;
2439 if (how
!= SD_RECEIVE
)
2441 sock
->wr_shutdown
= 1;
2442 if (list_empty( &sock
->write_q
.queue
))
2443 shutdown( unix_fd
, SHUT_WR
);
2445 sock
->wr_shutdown_pending
= 1;
2450 if (sock
->event
) release_object( sock
->event
);
2454 sock
->nonblocking
= 1;
2457 sock_reselect( sock
);
2461 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE
:
2465 if (get_req_data_size() < sizeof(int))
2467 set_error( STATUS_BUFFER_TOO_SMALL
);
2470 force_async
= *(int *)get_req_data();
2472 if (sock
->nonblocking
&& !force_async
)
2474 set_error( STATUS_DEVICE_NOT_READY
);
2477 if (!sock_get_ifchange( sock
)) return;
2478 queue_async( &sock
->ifchange_q
, async
);
2479 set_error( STATUS_PENDING
);
2483 case IOCTL_AFD_WINE_FIONBIO
:
2484 if (get_req_data_size() < sizeof(int))
2486 set_error( STATUS_BUFFER_TOO_SMALL
);
2489 if (*(int *)get_req_data())
2491 sock
->nonblocking
= 1;
2497 set_error( STATUS_INVALID_PARAMETER
);
2500 sock
->nonblocking
= 0;
2504 case IOCTL_AFD_GET_EVENTS
:
2506 struct afd_get_events_params params
= {0};
2509 if (get_reply_max_size() < sizeof(params
))
2511 set_error( STATUS_INVALID_PARAMETER
);
2515 params
.flags
= sock
->pending_events
& sock
->mask
;
2516 for (i
= 0; i
< ARRAY_SIZE( params
.status
); ++i
)
2517 params
.status
[i
] = sock_get_ntstatus( sock
->errors
[i
] );
2519 sock
->pending_events
= 0;
2520 sock_reselect( sock
);
2522 set_reply_data( ¶ms
, sizeof(params
) );
2526 case IOCTL_AFD_EVENT_SELECT
:
2528 struct event
*event
= NULL
;
2529 obj_handle_t event_handle
;
2532 set_async_pending( async
);
2534 if (is_machine_64bit( current
->process
->machine
))
2536 const struct afd_event_select_params_64
*params
= get_req_data();
2538 if (get_req_data_size() < sizeof(*params
))
2540 set_error( STATUS_INVALID_PARAMETER
);
2544 event_handle
= params
->event
;
2545 mask
= params
->mask
;
2549 const struct afd_event_select_params_32
*params
= get_req_data();
2551 if (get_req_data_size() < sizeof(*params
))
2553 set_error( STATUS_INVALID_PARAMETER
);
2557 event_handle
= params
->event
;
2558 mask
= params
->mask
;
2561 if ((event_handle
|| mask
) &&
2562 !(event
= get_event_obj( current
->process
, event_handle
, EVENT_MODIFY_STATE
)))
2564 set_error( STATUS_INVALID_PARAMETER
);
2568 if (sock
->event
) release_object( sock
->event
);
2569 sock
->event
= event
;
2574 sock
->nonblocking
= 1;
2576 sock_reselect( sock
);
2577 /* Explicitly wake the socket up if the mask causes it to become
2578 * signaled. Note that reselecting isn't enough, since we might already
2579 * have had events recorded in sock->reported_events and we don't want
2580 * to select for them again. */
2581 sock_wake_up( sock
);
2586 case IOCTL_AFD_WINE_MESSAGE_SELECT
:
2588 const struct afd_message_select_params
*params
= get_req_data();
2590 if (get_req_data_size() < sizeof(params
))
2592 set_error( STATUS_BUFFER_TOO_SMALL
);
2596 if (sock
->event
) release_object( sock
->event
);
2600 sock
->pending_events
= 0;
2601 sock
->reported_events
= 0;
2604 sock
->mask
= params
->mask
;
2605 sock
->window
= params
->window
;
2606 sock
->message
= params
->message
;
2607 sock
->wparam
= params
->handle
;
2608 sock
->nonblocking
= 1;
2610 sock_reselect( sock
);
2615 case IOCTL_AFD_BIND
:
2617 const struct afd_bind_params
*params
= get_req_data();
2618 union unix_sockaddr unix_addr
, bind_addr
;
2619 data_size_t in_size
;
2622 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2624 if (get_req_data_size() < get_reply_max_size())
2626 set_error( STATUS_BUFFER_TOO_SMALL
);
2629 in_size
= get_req_data_size() - get_reply_max_size();
2630 if (in_size
< offsetof(struct afd_bind_params
, addr
.sa_data
)
2631 || get_reply_max_size() < in_size
- sizeof(int))
2633 set_error( STATUS_INVALID_PARAMETER
);
2639 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED
);
2643 unix_len
= sockaddr_to_unix( ¶ms
->addr
, in_size
- sizeof(int), &unix_addr
);
2646 set_error( STATUS_INVALID_ADDRESS
);
2649 bind_addr
= unix_addr
;
2651 if (unix_addr
.addr
.sa_family
== AF_INET
)
2653 if (!memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 )
2654 || bind_to_interface( sock
, &unix_addr
.in
))
2655 bind_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
2657 else if (unix_addr
.addr
.sa_family
== AF_INET6
)
2659 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2660 /* Windows allows specifying zero to use the default scope. Linux
2661 * interprets it as an interface index and requires that it be
2663 if (!unix_addr
.in6
.sin6_scope_id
)
2664 bind_addr
.in6
.sin6_scope_id
= get_ipv6_interface_index( &unix_addr
.in6
.sin6_addr
);
2668 set_async_pending( async
);
2670 if (bind( unix_fd
, &bind_addr
.addr
, unix_len
) < 0)
2672 if (errno
== EADDRINUSE
)
2675 socklen_t len
= sizeof(reuse
);
2677 if (!getsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&reuse
, &len
) && reuse
)
2681 set_error( sock_get_ntstatus( errno
) );
2687 unix_len
= sizeof(bind_addr
);
2688 if (!getsockname( unix_fd
, &bind_addr
.addr
, &unix_len
))
2690 /* store the interface or magic loopback address instead of the
2691 * actual unix address */
2692 if (bind_addr
.addr
.sa_family
== AF_INET
)
2693 bind_addr
.in
.sin_addr
= unix_addr
.in
.sin_addr
;
2694 sock
->addr_len
= sockaddr_from_unix( &bind_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2697 if (get_reply_max_size() >= sock
->addr_len
)
2698 set_reply_data( &sock
->addr
, sock
->addr_len
);
2702 case IOCTL_AFD_GETSOCKNAME
:
2705 set_error( STATUS_INVALID_PARAMETER
);
2709 if (get_reply_max_size() < sock
->addr_len
)
2711 set_error( STATUS_BUFFER_TOO_SMALL
);
2715 set_reply_data( &sock
->addr
, sock
->addr_len
);
2718 case IOCTL_AFD_WINE_DEFER
:
2720 const obj_handle_t
*handle
= get_req_data();
2721 struct sock
*acceptsock
;
2723 if (get_req_data_size() < sizeof(*handle
))
2725 set_error( STATUS_BUFFER_TOO_SMALL
);
2729 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, *handle
, 0, &sock_ops
);
2730 if (!acceptsock
) return;
2732 sock
->deferred
= acceptsock
;
2736 case IOCTL_AFD_WINE_GET_INFO
:
2738 struct afd_get_info_params params
;
2740 if (get_reply_max_size() < sizeof(params
))
2742 set_error( STATUS_BUFFER_TOO_SMALL
);
2746 params
.family
= sock
->family
;
2747 params
.type
= sock
->type
;
2748 params
.protocol
= sock
->proto
;
2749 set_reply_data( ¶ms
, sizeof(params
) );
2753 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN
:
2755 int listening
= (sock
->state
== SOCK_LISTENING
);
2757 if (get_reply_max_size() < sizeof(listening
))
2759 set_error( STATUS_BUFFER_TOO_SMALL
);
2763 set_reply_data( &listening
, sizeof(listening
) );
2767 case IOCTL_AFD_WINE_GET_SO_ERROR
:
2770 socklen_t len
= sizeof(error
);
2773 if (get_reply_max_size() < sizeof(error
))
2775 set_error( STATUS_BUFFER_TOO_SMALL
);
2779 if (getsockopt( unix_fd
, SOL_SOCKET
, SO_ERROR
, (char *)&error
, &len
) < 0)
2781 set_error( sock_get_ntstatus( errno
) );
2787 for (i
= 0; i
< ARRAY_SIZE( sock
->errors
); ++i
)
2789 if (sock
->errors
[i
])
2791 error
= sock_get_error( sock
->errors
[i
] );
2797 set_reply_data( &error
, sizeof(error
) );
2801 case IOCTL_AFD_WINE_GET_SO_RCVBUF
:
2803 int rcvbuf
= sock
->rcvbuf
;
2805 if (get_reply_max_size() < sizeof(rcvbuf
))
2807 set_error( STATUS_BUFFER_TOO_SMALL
);
2811 set_reply_data( &rcvbuf
, sizeof(rcvbuf
) );
2815 case IOCTL_AFD_WINE_SET_SO_RCVBUF
:
2819 if (get_req_data_size() < sizeof(rcvbuf
))
2821 set_error( STATUS_BUFFER_TOO_SMALL
);
2824 rcvbuf
= *(DWORD
*)get_req_data();
2826 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_RCVBUF
, (char *)&rcvbuf
, sizeof(rcvbuf
) ))
2827 sock
->rcvbuf
= rcvbuf
;
2829 set_error( sock_get_ntstatus( errno
) );
2833 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO
:
2835 DWORD rcvtimeo
= sock
->rcvtimeo
;
2837 if (get_reply_max_size() < sizeof(rcvtimeo
))
2839 set_error( STATUS_BUFFER_TOO_SMALL
);
2843 set_reply_data( &rcvtimeo
, sizeof(rcvtimeo
) );
2847 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO
:
2851 if (get_req_data_size() < sizeof(rcvtimeo
))
2853 set_error( STATUS_BUFFER_TOO_SMALL
);
2856 rcvtimeo
= *(DWORD
*)get_req_data();
2858 sock
->rcvtimeo
= rcvtimeo
;
2862 case IOCTL_AFD_WINE_GET_SO_SNDBUF
:
2864 int sndbuf
= sock
->sndbuf
;
2866 if (get_reply_max_size() < sizeof(sndbuf
))
2868 set_error( STATUS_BUFFER_TOO_SMALL
);
2872 set_reply_data( &sndbuf
, sizeof(sndbuf
) );
2876 case IOCTL_AFD_WINE_SET_SO_SNDBUF
:
2880 if (get_req_data_size() < sizeof(sndbuf
))
2882 set_error( STATUS_BUFFER_TOO_SMALL
);
2885 sndbuf
= *(DWORD
*)get_req_data();
2890 /* setsockopt fails if a zero value is passed */
2891 sock
->sndbuf
= sndbuf
;
2896 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_SNDBUF
, (char *)&sndbuf
, sizeof(sndbuf
) ))
2897 sock
->sndbuf
= sndbuf
;
2899 set_error( sock_get_ntstatus( errno
) );
2903 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO
:
2905 DWORD sndtimeo
= sock
->sndtimeo
;
2907 if (get_reply_max_size() < sizeof(sndtimeo
))
2909 set_error( STATUS_BUFFER_TOO_SMALL
);
2913 set_reply_data( &sndtimeo
, sizeof(sndtimeo
) );
2917 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO
:
2921 if (get_req_data_size() < sizeof(sndtimeo
))
2923 set_error( STATUS_BUFFER_TOO_SMALL
);
2926 sndtimeo
= *(DWORD
*)get_req_data();
2928 sock
->sndtimeo
= sndtimeo
;
2932 case IOCTL_AFD_WINE_GET_SO_CONNECT_TIME
:
2936 if (get_reply_max_size() < sizeof(time
))
2938 set_error( STATUS_BUFFER_TOO_SMALL
);
2942 if (sock
->state
== SOCK_CONNECTED
)
2943 time
= (current_time
- sock
->connect_time
) / 10000000;
2945 set_reply_data( &time
, sizeof(time
) );
2949 case IOCTL_AFD_POLL
:
2951 if (get_reply_max_size() < get_req_data_size())
2953 set_error( STATUS_INVALID_PARAMETER
);
2957 if (is_machine_64bit( current
->process
->machine
))
2959 const struct afd_poll_params_64
*params
= get_req_data();
2961 if (get_req_data_size() < sizeof(struct afd_poll_params_64
) ||
2962 get_req_data_size() < offsetof( struct afd_poll_params_64
, sockets
[params
->count
] ))
2964 set_error( STATUS_INVALID_PARAMETER
);
2968 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, params
->sockets
);
2972 const struct afd_poll_params_32
*params
= get_req_data();
2973 struct afd_poll_socket_64
*sockets
;
2976 if (get_req_data_size() < sizeof(struct afd_poll_params_32
) ||
2977 get_req_data_size() < offsetof( struct afd_poll_params_32
, sockets
[params
->count
] ))
2979 set_error( STATUS_INVALID_PARAMETER
);
2983 if (!(sockets
= mem_alloc( params
->count
* sizeof(*sockets
) ))) return;
2984 for (i
= 0; i
< params
->count
; ++i
)
2986 sockets
[i
].socket
= params
->sockets
[i
].socket
;
2987 sockets
[i
].flags
= params
->sockets
[i
].flags
;
2988 sockets
[i
].status
= params
->sockets
[i
].status
;
2991 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, sockets
);
2999 set_error( STATUS_NOT_SUPPORTED
);
3004 static int poll_single_socket( struct sock
*sock
, int mask
)
3006 struct pollfd pollfd
;
3008 pollfd
.fd
= get_unix_fd( sock
->fd
);
3009 pollfd
.events
= poll_flags_from_afd( sock
, mask
);
3010 if (pollfd
.events
< 0 || poll( &pollfd
, 1, 0 ) < 0)
3013 if (sock
->state
== SOCK_CONNECTING
&& (pollfd
.revents
& (POLLERR
| POLLHUP
)))
3014 pollfd
.revents
&= ~POLLOUT
;
3016 if ((mask
& AFD_POLL_HUP
) && (pollfd
.revents
& POLLIN
) && sock
->type
== WS_SOCK_STREAM
)
3020 if (!recv( get_unix_fd( sock
->fd
), &dummy
, 1, MSG_PEEK
))
3022 pollfd
.revents
&= ~POLLIN
;
3023 pollfd
.revents
|= POLLHUP
;
3027 return get_poll_flags( sock
, pollfd
.revents
) & mask
;
3030 static void handle_exclusive_poll(struct poll_req
*req
)
3034 for (i
= 0; i
< req
->count
; ++i
)
3036 struct sock
*sock
= req
->sockets
[i
].sock
;
3037 struct poll_req
*main_poll
= sock
->main_poll
;
3039 if (main_poll
&& main_poll
->exclusive
&& req
->exclusive
)
3041 complete_async_poll( main_poll
, STATUS_SUCCESS
);
3046 sock
->main_poll
= req
;
3050 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
3051 unsigned int count
, const struct afd_poll_socket_64
*sockets
)
3053 BOOL signaled
= FALSE
;
3054 struct poll_req
*req
;
3059 set_error( STATUS_INVALID_PARAMETER
);
3063 if (!(req
= mem_alloc( offsetof( struct poll_req
, sockets
[count
] ) )))
3066 req
->timeout
= NULL
;
3067 if (timeout
&& timeout
!= TIMEOUT_INFINITE
&&
3068 !(req
->timeout
= add_timeout_user( timeout
, async_poll_timeout
, req
)))
3073 req
->orig_timeout
= timeout
;
3075 for (i
= 0; i
< count
; ++i
)
3077 req
->sockets
[i
].sock
= (struct sock
*)get_handle_obj( current
->process
, sockets
[i
].socket
, 0, &sock_ops
);
3078 if (!req
->sockets
[i
].sock
)
3080 for (j
= 0; j
< i
; ++j
) release_object( req
->sockets
[j
].sock
);
3081 if (req
->timeout
) remove_timeout_user( req
->timeout
);
3085 req
->sockets
[i
].handle
= sockets
[i
].socket
;
3086 req
->sockets
[i
].mask
= sockets
[i
].flags
;
3087 req
->sockets
[i
].flags
= 0;
3090 req
->exclusive
= exclusive
;
3092 req
->async
= (struct async
*)grab_object( async
);
3093 req
->iosb
= async_get_iosb( async
);
3095 handle_exclusive_poll(req
);
3097 list_add_tail( &poll_list
, &req
->entry
);
3098 async_set_completion_callback( async
, free_poll_req
, req
);
3099 queue_async( &poll_sock
->poll_q
, async
);
3101 for (i
= 0; i
< count
; ++i
)
3103 struct sock
*sock
= req
->sockets
[i
].sock
;
3104 int mask
= req
->sockets
[i
].mask
;
3105 int flags
= poll_single_socket( sock
, mask
);
3110 req
->sockets
[i
].flags
= flags
;
3111 req
->sockets
[i
].status
= sock_get_ntstatus( sock_error( sock
->fd
) );
3114 /* FIXME: do other error conditions deserve a similar treatment? */
3115 if (sock
->state
!= SOCK_CONNECTING
&& sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] && (mask
& AFD_POLL_CONNECT_ERR
))
3118 req
->sockets
[i
].flags
|= AFD_POLL_CONNECT_ERR
;
3119 req
->sockets
[i
].status
= sock_get_ntstatus( sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] );
3123 if (!timeout
|| signaled
)
3124 complete_async_poll( req
, STATUS_SUCCESS
);
3126 for (i
= 0; i
< req
->count
; ++i
)
3127 sock_reselect( req
->sockets
[i
].sock
);
3128 set_error( STATUS_PENDING
);
3131 #ifdef HAVE_LINUX_RTNETLINK_H
3133 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
3134 static struct object
*ifchange_object
;
3136 static void ifchange_dump( struct object
*obj
, int verbose
);
3137 static struct fd
*ifchange_get_fd( struct object
*obj
);
3138 static void ifchange_destroy( struct object
*obj
);
3140 static int ifchange_get_poll_events( struct fd
*fd
);
3141 static void ifchange_poll_event( struct fd
*fd
, int event
);
3145 struct object obj
; /* object header */
3146 struct fd
*fd
; /* interface change file descriptor */
3147 struct list sockets
; /* list of sockets to send interface change notifications */
3150 static const struct object_ops ifchange_ops
=
3152 sizeof(struct ifchange
), /* size */
3153 &no_type
, /* type */
3154 ifchange_dump
, /* dump */
3155 no_add_queue
, /* add_queue */
3156 NULL
, /* remove_queue */
3157 NULL
, /* signaled */
3158 no_satisfied
, /* satisfied */
3159 no_signal
, /* signal */
3160 ifchange_get_fd
, /* get_fd */
3161 default_map_access
, /* map_access */
3162 default_get_sd
, /* get_sd */
3163 default_set_sd
, /* set_sd */
3164 no_get_full_name
, /* get_full_name */
3165 no_lookup_name
, /* lookup_name */
3166 no_link_name
, /* link_name */
3167 NULL
, /* unlink_name */
3168 no_open_file
, /* open_file */
3169 no_kernel_obj_list
, /* get_kernel_obj_list */
3170 no_close_handle
, /* close_handle */
3171 ifchange_destroy
/* destroy */
3174 static const struct fd_ops ifchange_fd_ops
=
3176 ifchange_get_poll_events
, /* get_poll_events */
3177 ifchange_poll_event
, /* poll_event */
3178 NULL
, /* get_fd_type */
3179 no_fd_read
, /* read */
3180 no_fd_write
, /* write */
3181 no_fd_flush
, /* flush */
3182 no_fd_get_file_info
, /* get_file_info */
3183 no_fd_get_volume_info
, /* get_volume_info */
3184 no_fd_ioctl
, /* ioctl */
3185 NULL
, /* cancel_async */
3186 NULL
, /* queue_async */
3187 NULL
/* reselect_async */
3190 static void ifchange_dump( struct object
*obj
, int verbose
)
3192 assert( obj
->ops
== &ifchange_ops
);
3193 fprintf( stderr
, "Interface change\n" );
3196 static struct fd
*ifchange_get_fd( struct object
*obj
)
3198 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3199 return (struct fd
*)grab_object( ifchange
->fd
);
3202 static void ifchange_destroy( struct object
*obj
)
3204 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3205 assert( obj
->ops
== &ifchange_ops
);
3207 release_object( ifchange
->fd
);
3209 /* reset the global ifchange object so that it will be recreated if it is needed again */
3210 assert( obj
== ifchange_object
);
3211 ifchange_object
= NULL
;
3214 static int ifchange_get_poll_events( struct fd
*fd
)
3219 /* wake up all the sockets waiting for a change notification event */
3220 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
3222 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3223 struct list
*ptr
, *next
;
3224 assert( obj
->ops
== &ifchange_ops
);
3225 assert( obj
== ifchange_object
);
3227 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
3229 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
3231 assert( sock
->ifchange_obj
);
3232 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
3233 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
3237 static void ifchange_poll_event( struct fd
*fd
, int event
)
3239 struct object
*ifchange
= get_fd_user( fd
);
3240 unsigned int status
= STATUS_PENDING
;
3241 char buffer
[PIPE_BUF
];
3244 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
3247 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
3248 return; /* retry when poll() says the socket is ready */
3249 status
= sock_get_ntstatus( errno
);
3253 struct nlmsghdr
*nlh
;
3255 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
3257 if (nlh
->nlmsg_type
== NLMSG_DONE
)
3259 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
3260 status
= STATUS_SUCCESS
;
3263 else status
= STATUS_CANCELLED
;
3265 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
3270 /* we only need one of these interface notification objects, all of the sockets dependent upon
3271 * it will wake up when a notification event occurs */
3272 static struct object
*get_ifchange( void )
3274 #ifdef HAVE_LINUX_RTNETLINK_H
3275 struct ifchange
*ifchange
;
3276 struct sockaddr_nl addr
;
3279 if (ifchange_object
)
3281 /* increment the refcount for each socket that uses the ifchange object */
3282 return grab_object( ifchange_object
);
3285 /* create the socket we need for processing interface change notifications */
3286 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
3289 set_error( sock_get_ntstatus( errno
));
3292 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
3293 memset( &addr
, 0, sizeof(addr
) );
3294 addr
.nl_family
= AF_NETLINK
;
3295 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
3296 /* bind the socket to the special netlink kernel interface */
3297 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
3300 set_error( sock_get_ntstatus( errno
));
3303 if (!(ifchange
= alloc_object( &ifchange_ops
)))
3306 set_error( STATUS_NO_MEMORY
);
3309 list_init( &ifchange
->sockets
);
3310 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
3312 release_object( ifchange
);
3313 set_error( STATUS_NO_MEMORY
);
3316 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
3318 /* the ifchange object is now successfully configured */
3319 ifchange_object
= &ifchange
->obj
;
3320 return &ifchange
->obj
;
3322 set_error( STATUS_NOT_SUPPORTED
);
3327 /* add the socket to the interface change notification list */
3328 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
3330 #ifdef HAVE_LINUX_RTNETLINK_H
3331 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3333 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
3337 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3338 static struct object
*sock_get_ifchange( struct sock
*sock
)
3340 struct object
*ifchange
;
3342 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
3343 return sock
->ifchange_obj
;
3345 if (!(ifchange
= get_ifchange()))
3348 /* add the socket to the ifchange notification list */
3349 ifchange_add_sock( ifchange
, sock
);
3350 sock
->ifchange_obj
= ifchange
;
3354 /* destroy an existing ifchange queue for a specific socket */
3355 static void sock_release_ifchange( struct sock
*sock
)
3357 if (sock
->ifchange_obj
)
3359 list_remove( &sock
->ifchange_entry
);
3360 release_object( sock
->ifchange_obj
);
3361 sock
->ifchange_obj
= NULL
;
3365 static void socket_device_dump( struct object
*obj
, int verbose
);
3366 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3367 unsigned int attr
, struct object
*root
);
3368 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3369 unsigned int sharing
, unsigned int options
);
3371 static const struct object_ops socket_device_ops
=
3373 sizeof(struct object
), /* size */
3374 &device_type
, /* type */
3375 socket_device_dump
, /* dump */
3376 no_add_queue
, /* add_queue */
3377 NULL
, /* remove_queue */
3378 NULL
, /* signaled */
3379 no_satisfied
, /* satisfied */
3380 no_signal
, /* signal */
3381 no_get_fd
, /* get_fd */
3382 default_map_access
, /* map_access */
3383 default_get_sd
, /* get_sd */
3384 default_set_sd
, /* set_sd */
3385 default_get_full_name
, /* get_full_name */
3386 socket_device_lookup_name
, /* lookup_name */
3387 directory_link_name
, /* link_name */
3388 default_unlink_name
, /* unlink_name */
3389 socket_device_open_file
, /* open_file */
3390 no_kernel_obj_list
, /* get_kernel_obj_list */
3391 no_close_handle
, /* close_handle */
3392 no_destroy
/* destroy */
3395 static void socket_device_dump( struct object
*obj
, int verbose
)
3397 fputs( "Socket device\n", stderr
);
3400 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3401 unsigned int attr
, struct object
*root
)
3403 if (name
) name
->len
= 0;
3407 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3408 unsigned int sharing
, unsigned int options
)
3412 if (!(sock
= create_socket())) return NULL
;
3413 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
3415 release_object( sock
);
3421 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
3422 unsigned int attr
, const struct security_descriptor
*sd
)
3424 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
3427 DECL_HANDLER(recv_socket
)
3429 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3430 unsigned int status
= STATUS_PENDING
;
3431 timeout_t timeout
= 0;
3432 struct async
*async
;
3438 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3439 timeout
= (timeout_t
)sock
->rcvtimeo
* -10000;
3441 if (sock
->rd_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3442 else if (!async_queued( &sock
->read_q
))
3444 /* If read_q is not empty, we cannot really tell if the already queued
3445 * asyncs will not consume all available data; if there's no data
3446 * available, the current request won't be immediately satiable.
3448 struct pollfd pollfd
;
3449 pollfd
.fd
= get_unix_fd( sock
->fd
);
3450 pollfd
.events
= req
->oob
? POLLPRI
: POLLIN
;
3452 if (poll(&pollfd
, 1, 0) >= 0 && pollfd
.revents
)
3454 /* Give the client opportunity to complete synchronously.
3455 * If it turns out that the I/O request is not actually immediately satiable,
3456 * the client may then choose to re-queue the async (with STATUS_PENDING). */
3457 status
= STATUS_ALERTED
;
3461 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3462 status
= STATUS_DEVICE_NOT_READY
;
3464 sock
->pending_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3465 sock
->reported_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3467 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3469 set_error( status
);
3472 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3474 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3475 queue_async( &sock
->read_q
, async
);
3477 /* always reselect; we changed reported_events above */
3478 sock_reselect( sock
);
3480 reply
->wait
= async_handoff( async
, NULL
, 0 );
3481 reply
->options
= get_fd_options( fd
);
3482 reply
->nonblocking
= sock
->nonblocking
;
3483 release_object( async
);
3485 release_object( sock
);
3488 static void send_socket_completion_callback( void *private )
3490 struct send_req
*send_req
= private;
3491 struct iosb
*iosb
= send_req
->iosb
;
3492 struct sock
*sock
= send_req
->sock
;
3494 if (iosb
->status
!= STATUS_SUCCESS
)
3496 /* send() calls only clear and reselect events if unsuccessful. */
3497 sock
->pending_events
&= ~AFD_POLL_WRITE
;
3498 sock
->reported_events
&= ~AFD_POLL_WRITE
;
3499 sock_reselect( sock
);
3502 release_object( iosb
);
3503 release_object( sock
);
3507 DECL_HANDLER(send_socket
)
3509 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3510 unsigned int status
= STATUS_PENDING
;
3511 timeout_t timeout
= 0;
3512 struct async
*async
;
3519 if (sock
->type
== WS_SOCK_DGRAM
&& !sock
->bound
)
3521 union unix_sockaddr unix_addr
;
3523 int unix_fd
= get_unix_fd( fd
);
3525 unix_len
= get_unix_sockaddr_any( &unix_addr
, sock
->family
);
3526 if (bind( unix_fd
, &unix_addr
.addr
, unix_len
) < 0)
3529 if (getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
) >= 0)
3531 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
3534 else if (!bind_errno
) bind_errno
= errno
;
3537 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3538 timeout
= (timeout_t
)sock
->sndtimeo
* -10000;
3540 if (bind_errno
) status
= sock_get_ntstatus( bind_errno
);
3541 else if (sock
->wr_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3542 else if (!async_queued( &sock
->write_q
))
3544 /* If write_q is not empty, we cannot really tell if the already queued
3545 * asyncs will not consume all available space; if there's no space
3546 * available, the current request won't be immediately satiable.
3548 struct pollfd pollfd
;
3549 pollfd
.fd
= get_unix_fd( sock
->fd
);
3550 pollfd
.events
= POLLOUT
;
3552 if (poll(&pollfd
, 1, 0) >= 0 && pollfd
.revents
)
3554 /* Give the client opportunity to complete synchronously.
3555 * If it turns out that the I/O request is not actually immediately satiable,
3556 * the client may then choose to re-queue the async (with STATUS_PENDING). */
3557 status
= STATUS_ALERTED
;
3561 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3562 status
= STATUS_DEVICE_NOT_READY
;
3564 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3566 struct send_req
*send_req
;
3567 struct iosb
*iosb
= async_get_iosb( async
);
3569 if ((send_req
= mem_alloc( sizeof(*send_req
) )))
3571 send_req
->iosb
= (struct iosb
*)grab_object( iosb
);
3572 send_req
->sock
= (struct sock
*)grab_object( sock
);
3573 async_set_completion_callback( async
, send_socket_completion_callback
, send_req
);
3575 else if (status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
)
3576 status
= STATUS_NO_MEMORY
;
3578 release_object( iosb
);
3580 set_error( status
);
3583 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3585 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3587 queue_async( &sock
->write_q
, async
);
3588 sock_reselect( sock
);
3591 reply
->wait
= async_handoff( async
, NULL
, 0 );
3592 reply
->options
= get_fd_options( fd
);
3593 reply
->nonblocking
= sock
->nonblocking
;
3594 release_object( async
);
3596 release_object( sock
);