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
;
144 struct sock
*sock
, *acceptsock
;
146 unsigned int recv_len
, local_len
;
154 unsigned int addr_len
, send_len
, send_cursor
;
163 enum connection_state
172 #define MAX_ICMP_HISTORY_LENGTH 8
176 struct object obj
; /* object header */
177 struct fd
*fd
; /* socket file descriptor */
178 enum connection_state state
; /* connection state */
179 unsigned int mask
; /* event mask */
180 /* pending AFD_POLL_* events which have not yet been reported to the application */
181 unsigned int pending_events
;
182 /* AFD_POLL_* events which have already been reported and should not be
183 * selected for again until reset by a relevant call.
185 * For example, if AFD_POLL_READ is set here and not in pending_events, it
186 * has already been reported and consumed, and we should not report it
187 * again, even if POLLIN is signaled, until it is reset by e.g recv().
189 * If an event has been signaled and not consumed yet, it will be set in
190 * both pending_events and reported_events (as we should only ever report
191 * any event once until it is reset.) */
192 unsigned int reported_events
;
193 unsigned short proto
; /* socket protocol */
194 unsigned short type
; /* socket type */
195 unsigned short family
; /* socket family */
196 struct event
*event
; /* event object */
197 user_handle_t window
; /* window to send the message to */
198 unsigned int message
; /* message to send */
199 obj_handle_t wparam
; /* message wparam (socket handle) */
200 int errors
[AFD_POLL_BIT_COUNT
]; /* event errors */
201 timeout_t connect_time
;/* time the socket was connected */
202 struct sock
*deferred
; /* socket that waits for a deferred accept */
203 struct async_queue read_q
; /* queue for asynchronous reads */
204 struct async_queue write_q
; /* queue for asynchronous writes */
205 struct async_queue ifchange_q
; /* queue for interface change notifications */
206 struct async_queue accept_q
; /* queue for asynchronous accepts */
207 struct async_queue connect_q
; /* queue for asynchronous connects */
208 struct async_queue poll_q
; /* queue for asynchronous polls */
209 struct object
*ifchange_obj
; /* the interface change notification object */
210 struct list ifchange_entry
; /* entry in ifchange notification list */
211 struct list accept_list
; /* list of pending accept requests */
212 struct accept_req
*accept_recv_req
; /* pending accept-into request which will recv on this socket */
213 struct connect_req
*connect_req
; /* pending connection request */
214 struct poll_req
*main_poll
; /* main poll */
215 union win_sockaddr addr
; /* socket name */
216 int addr_len
; /* socket name length */
217 unsigned int rcvbuf
; /* advisory recv buffer size */
218 unsigned int sndbuf
; /* advisory send buffer size */
219 unsigned int rcvtimeo
; /* receive timeout in ms */
220 unsigned int sndtimeo
; /* send timeout in ms */
223 unsigned short icmp_id
;
224 unsigned short icmp_seq
;
226 icmp_fixup_data
[MAX_ICMP_HISTORY_LENGTH
]; /* Sent ICMP packets history used to fixup reply id. */
227 unsigned int icmp_fixup_data_len
; /* Sent ICMP packets history length. */
228 unsigned int rd_shutdown
: 1; /* is the read end shut down? */
229 unsigned int wr_shutdown
: 1; /* is the write end shut down? */
230 unsigned int wr_shutdown_pending
: 1; /* is a write shutdown pending? */
231 unsigned int hangup
: 1; /* has the read end received a hangup? */
232 unsigned int aborted
: 1; /* did we get a POLLERR or irregular POLLHUP? */
233 unsigned int nonblocking
: 1; /* is the socket nonblocking? */
234 unsigned int bound
: 1; /* is the socket bound? */
235 unsigned int reset
: 1; /* did we get a TCP reset? */
238 static void sock_dump( struct object
*obj
, int verbose
);
239 static struct fd
*sock_get_fd( struct object
*obj
);
240 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
241 static void sock_destroy( struct object
*obj
);
242 static struct object
*sock_get_ifchange( struct sock
*sock
);
243 static void sock_release_ifchange( struct sock
*sock
);
245 static int sock_get_poll_events( struct fd
*fd
);
246 static void sock_poll_event( struct fd
*fd
, int event
);
247 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
248 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
249 static void sock_cancel_async( struct fd
*fd
, struct async
*async
);
250 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
252 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
);
253 static struct sock
*accept_socket( struct sock
*sock
);
254 static int sock_get_ntstatus( int err
);
255 static unsigned int sock_get_error( int err
);
256 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
257 unsigned int count
, const struct afd_poll_socket_64
*sockets
);
259 static const struct object_ops sock_ops
=
261 sizeof(struct sock
), /* size */
262 &file_type
, /* type */
263 sock_dump
, /* dump */
264 add_queue
, /* add_queue */
265 remove_queue
, /* remove_queue */
266 default_fd_signaled
, /* signaled */
267 no_satisfied
, /* satisfied */
268 no_signal
, /* signal */
269 sock_get_fd
, /* get_fd */
270 default_map_access
, /* map_access */
271 default_get_sd
, /* get_sd */
272 default_set_sd
, /* set_sd */
273 no_get_full_name
, /* get_full_name */
274 no_lookup_name
, /* lookup_name */
275 no_link_name
, /* link_name */
276 NULL
, /* unlink_name */
277 no_open_file
, /* open_file */
278 no_kernel_obj_list
, /* get_kernel_obj_list */
279 sock_close_handle
, /* close_handle */
280 sock_destroy
/* destroy */
283 static const struct fd_ops sock_fd_ops
=
285 sock_get_poll_events
, /* get_poll_events */
286 sock_poll_event
, /* poll_event */
287 sock_get_fd_type
, /* get_fd_type */
288 no_fd_read
, /* read */
289 no_fd_write
, /* write */
290 no_fd_flush
, /* flush */
291 default_fd_get_file_info
, /* get_file_info */
292 no_fd_get_volume_info
, /* get_volume_info */
293 sock_ioctl
, /* ioctl */
294 sock_cancel_async
, /* cancel_async */
295 no_fd_queue_async
, /* queue_async */
296 sock_reselect_async
/* reselect_async */
301 struct sockaddr addr
;
302 struct sockaddr_in in
;
303 struct sockaddr_in6 in6
;
305 struct sockaddr_ipx ipx
;
308 struct sockaddr_irda irda
;
312 static int sockaddr_from_unix( const union unix_sockaddr
*uaddr
, struct WS_sockaddr
*wsaddr
, socklen_t wsaddrlen
)
314 memset( wsaddr
, 0, wsaddrlen
);
316 switch (uaddr
->addr
.sa_family
)
320 struct WS_sockaddr_in win
= {0};
322 if (wsaddrlen
< sizeof(win
)) return -1;
323 win
.sin_family
= WS_AF_INET
;
324 win
.sin_port
= uaddr
->in
.sin_port
;
325 memcpy( &win
.sin_addr
, &uaddr
->in
.sin_addr
, sizeof(win
.sin_addr
) );
326 memcpy( wsaddr
, &win
, sizeof(win
) );
332 struct WS_sockaddr_in6 win
= {0};
334 if (wsaddrlen
< sizeof(win
)) return -1;
335 win
.sin6_family
= WS_AF_INET6
;
336 win
.sin6_port
= uaddr
->in6
.sin6_port
;
337 win
.sin6_flowinfo
= uaddr
->in6
.sin6_flowinfo
;
338 memcpy( &win
.sin6_addr
, &uaddr
->in6
.sin6_addr
, sizeof(win
.sin6_addr
) );
339 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
340 win
.sin6_scope_id
= uaddr
->in6
.sin6_scope_id
;
342 memcpy( wsaddr
, &win
, sizeof(win
) );
349 struct WS_sockaddr_ipx win
= {0};
351 if (wsaddrlen
< sizeof(win
)) return -1;
352 win
.sa_family
= WS_AF_IPX
;
353 memcpy( win
.sa_netnum
, &uaddr
->ipx
.sipx_network
, sizeof(win
.sa_netnum
) );
354 memcpy( win
.sa_nodenum
, &uaddr
->ipx
.sipx_node
, sizeof(win
.sa_nodenum
) );
355 win
.sa_socket
= uaddr
->ipx
.sipx_port
;
356 memcpy( wsaddr
, &win
, sizeof(win
) );
366 if (wsaddrlen
< sizeof(win
)) return -1;
367 win
.irdaAddressFamily
= WS_AF_IRDA
;
368 memcpy( win
.irdaDeviceID
, &uaddr
->irda
.sir_addr
, sizeof(win
.irdaDeviceID
) );
369 if (uaddr
->irda
.sir_lsap_sel
!= LSAP_ANY
)
370 snprintf( win
.irdaServiceName
, sizeof(win
.irdaServiceName
), "LSAP-SEL%u", uaddr
->irda
.sir_lsap_sel
);
372 memcpy( win
.irdaServiceName
, uaddr
->irda
.sir_name
, sizeof(win
.irdaServiceName
) );
373 memcpy( wsaddr
, &win
, sizeof(win
) );
387 static socklen_t
sockaddr_to_unix( const struct WS_sockaddr
*wsaddr
, int wsaddrlen
, union unix_sockaddr
*uaddr
)
389 memset( uaddr
, 0, sizeof(*uaddr
) );
391 switch (wsaddr
->sa_family
)
395 struct WS_sockaddr_in win
= {0};
397 if (wsaddrlen
< sizeof(win
)) return 0;
398 memcpy( &win
, wsaddr
, sizeof(win
) );
399 uaddr
->in
.sin_family
= AF_INET
;
400 uaddr
->in
.sin_port
= win
.sin_port
;
401 memcpy( &uaddr
->in
.sin_addr
, &win
.sin_addr
, sizeof(win
.sin_addr
) );
402 return sizeof(uaddr
->in
);
407 struct WS_sockaddr_in6 win
= {0};
409 if (wsaddrlen
< sizeof(win
)) return 0;
410 memcpy( &win
, wsaddr
, sizeof(win
) );
411 uaddr
->in6
.sin6_family
= AF_INET6
;
412 uaddr
->in6
.sin6_port
= win
.sin6_port
;
413 uaddr
->in6
.sin6_flowinfo
= win
.sin6_flowinfo
;
414 memcpy( &uaddr
->in6
.sin6_addr
, &win
.sin6_addr
, sizeof(win
.sin6_addr
) );
415 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
416 uaddr
->in6
.sin6_scope_id
= win
.sin6_scope_id
;
418 return sizeof(uaddr
->in6
);
424 struct WS_sockaddr_ipx win
= {0};
426 if (wsaddrlen
< sizeof(win
)) return 0;
427 memcpy( &win
, wsaddr
, sizeof(win
) );
428 uaddr
->ipx
.sipx_family
= AF_IPX
;
429 memcpy( &uaddr
->ipx
.sipx_network
, win
.sa_netnum
, sizeof(win
.sa_netnum
) );
430 memcpy( &uaddr
->ipx
.sipx_node
, win
.sa_nodenum
, sizeof(win
.sa_nodenum
) );
431 uaddr
->ipx
.sipx_port
= win
.sa_socket
;
432 return sizeof(uaddr
->ipx
);
439 SOCKADDR_IRDA win
= {0};
440 unsigned int lsap_sel
;
442 if (wsaddrlen
< sizeof(win
)) return 0;
443 memcpy( &win
, wsaddr
, sizeof(win
) );
444 uaddr
->irda
.sir_family
= AF_IRDA
;
445 if (sscanf( win
.irdaServiceName
, "LSAP-SEL%u", &lsap_sel
) == 1)
446 uaddr
->irda
.sir_lsap_sel
= lsap_sel
;
449 uaddr
->irda
.sir_lsap_sel
= LSAP_ANY
;
450 memcpy( uaddr
->irda
.sir_name
, win
.irdaServiceName
, sizeof(win
.irdaServiceName
) );
452 memcpy( &uaddr
->irda
.sir_addr
, win
.irdaDeviceID
, sizeof(win
.irdaDeviceID
) );
453 return sizeof(uaddr
->irda
);
460 default: /* likely an ipv4 address */
461 case sizeof(struct WS_sockaddr_in
):
462 return sizeof(uaddr
->in
);
465 case sizeof(struct WS_sockaddr_ipx
):
466 return sizeof(uaddr
->ipx
);
470 case sizeof(SOCKADDR_IRDA
):
471 return sizeof(uaddr
->irda
);
474 case sizeof(struct WS_sockaddr_in6
):
475 return sizeof(uaddr
->in6
);
483 static socklen_t
get_unix_sockaddr_any( union unix_sockaddr
*uaddr
, int ws_family
)
485 memset( uaddr
, 0, sizeof(*uaddr
) );
489 uaddr
->in
.sin_family
= AF_INET
;
490 return sizeof(uaddr
->in
);
492 uaddr
->in6
.sin6_family
= AF_INET6
;
493 return sizeof(uaddr
->in6
);
496 uaddr
->ipx
.sipx_family
= AF_IPX
;
497 return sizeof(uaddr
->ipx
);
501 uaddr
->irda
.sir_family
= AF_IRDA
;
502 return sizeof(uaddr
->irda
);
509 /* some events are generated at the same time but must be sent in a particular
510 * order (e.g. CONNECT must be sent before READ) */
511 static const enum afd_poll_bit event_bitorder
[] =
513 AFD_POLL_BIT_CONNECT
,
514 AFD_POLL_BIT_CONNECT_ERR
,
525 SOCK_SHUTDOWN_ERROR
= -1,
526 SOCK_SHUTDOWN_EOF
= 0,
527 SOCK_SHUTDOWN_POLLHUP
= 1
530 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
532 static sock_shutdown_t
sock_check_pollhup(void)
534 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
539 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
540 if ( shutdown( fd
[0], 1 ) ) goto out
;
546 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
547 n
= poll( &pfd
, 1, 1 );
548 if ( n
!= 1 ) goto out
; /* error or timeout */
549 if ( pfd
.revents
& POLLHUP
)
550 ret
= SOCK_SHUTDOWN_POLLHUP
;
551 else if ( pfd
.revents
& POLLIN
&&
552 read( fd
[1], &dummy
, 1 ) == 0 )
553 ret
= SOCK_SHUTDOWN_EOF
;
563 sock_shutdown_type
= sock_check_pollhup();
565 switch ( sock_shutdown_type
)
567 case SOCK_SHUTDOWN_EOF
:
568 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
570 case SOCK_SHUTDOWN_POLLHUP
:
571 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
574 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
575 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
579 static void sock_reselect( struct sock
*sock
)
581 int ev
= sock_get_poll_events( sock
->fd
);
584 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
586 set_fd_events( sock
->fd
, ev
);
589 static unsigned int afd_poll_flag_to_win32( unsigned int flags
)
591 static const unsigned int map
[] =
595 FD_WRITE
, /* WRITE */
597 FD_CLOSE
, /* RESET */
599 FD_CONNECT
, /* CONNECT */
600 FD_ACCEPT
, /* ACCEPT */
601 FD_CONNECT
, /* CONNECT_ERR */
604 unsigned int i
, ret
= 0;
606 for (i
= 0; i
< ARRAY_SIZE(map
); ++i
)
608 if (flags
& (1 << i
)) ret
|= map
[i
];
614 /* wake anybody waiting on the socket event or send the associated message */
615 static void sock_wake_up( struct sock
*sock
)
617 unsigned int events
= sock
->pending_events
& sock
->mask
;
622 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
624 set_event( sock
->event
);
628 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
629 for (i
= 0; i
< ARRAY_SIZE(event_bitorder
); i
++)
631 enum afd_poll_bit event
= event_bitorder
[i
];
632 if (events
& (1 << event
))
634 lparam_t lparam
= afd_poll_flag_to_win32(1 << event
) | (sock_get_error( sock
->errors
[event
] ) << 16);
635 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
638 sock
->pending_events
= 0;
639 sock_reselect( sock
);
643 static inline int sock_error( struct sock
*sock
)
646 socklen_t len
= sizeof(error
);
648 getsockopt( get_unix_fd(sock
->fd
), SOL_SOCKET
, SO_ERROR
, (void *)&error
, &len
);
652 case SOCK_UNCONNECTED
:
655 case SOCK_CONNECTING
:
657 sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] = error
;
659 error
= sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
];
664 sock
->errors
[AFD_POLL_BIT_ACCEPT
] = error
;
666 error
= sock
->errors
[AFD_POLL_BIT_ACCEPT
];
670 case SOCK_CONNECTIONLESS
:
671 if (error
== ECONNRESET
|| error
== EPIPE
)
677 sock
->errors
[AFD_POLL_BIT_HUP
] = error
;
679 error
= sock
->errors
[AFD_POLL_BIT_HUP
];
686 static void free_accept_req( void *private )
688 struct accept_req
*req
= private;
689 list_remove( &req
->entry
);
692 req
->acceptsock
->accept_recv_req
= NULL
;
693 release_object( req
->acceptsock
);
695 release_object( req
->async
);
696 release_object( req
->iosb
);
697 release_object( req
->sock
);
701 static void fill_accept_output( struct accept_req
*req
)
703 const data_size_t out_size
= req
->iosb
->out_size
;
704 struct async
*async
= req
->async
;
705 union unix_sockaddr unix_addr
;
706 struct WS_sockaddr
*win_addr
;
707 unsigned int remote_len
;
713 if (!(out_data
= mem_alloc( out_size
)))
715 async_terminate( async
, get_error() );
719 fd
= get_unix_fd( req
->acceptsock
->fd
);
721 if (req
->recv_len
&& (size
= recv( fd
, out_data
, req
->recv_len
, 0 )) < 0)
723 if (!req
->accepted
&& errno
== EWOULDBLOCK
)
726 sock_reselect( req
->acceptsock
);
730 async_terminate( async
, sock_get_ntstatus( errno
) );
737 if (req
->local_len
< sizeof(int))
739 async_terminate( async
, STATUS_BUFFER_TOO_SMALL
);
744 unix_len
= sizeof(unix_addr
);
745 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ sizeof(int));
746 if (getsockname( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
747 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, req
->local_len
- sizeof(int) )) < 0)
749 async_terminate( async
, sock_get_ntstatus( errno
) );
753 memcpy( out_data
+ req
->recv_len
, &win_len
, sizeof(int) );
756 unix_len
= sizeof(unix_addr
);
757 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ req
->local_len
+ sizeof(int));
758 remote_len
= out_size
- req
->recv_len
- req
->local_len
;
759 if (getpeername( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
760 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, remote_len
- sizeof(int) )) < 0)
762 async_terminate( async
, sock_get_ntstatus( errno
) );
766 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
768 async_request_complete( req
->async
, STATUS_SUCCESS
, size
, out_size
, out_data
);
771 static void complete_async_accept( struct sock
*sock
, struct accept_req
*req
)
773 struct sock
*acceptsock
= req
->acceptsock
;
774 struct async
*async
= req
->async
;
776 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
780 if (!accept_into_socket( sock
, acceptsock
))
782 async_terminate( async
, get_error() );
785 fill_accept_output( req
);
791 if (!(acceptsock
= accept_socket( sock
)))
793 async_terminate( async
, get_error() );
796 handle
= alloc_handle_no_access_check( async_get_thread( async
)->process
, &acceptsock
->obj
,
797 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
798 acceptsock
->wparam
= handle
;
799 sock_reselect( acceptsock
);
800 release_object( acceptsock
);
803 async_terminate( async
, get_error() );
807 async_request_complete_alloc( req
->async
, STATUS_SUCCESS
, 0, sizeof(handle
), &handle
);
811 static void complete_async_accept_recv( struct accept_req
*req
)
813 if (debug_level
) fprintf( stderr
, "completing accept recv request for socket %p\n", req
->acceptsock
);
815 assert( req
->recv_len
);
817 fill_accept_output( req
);
820 static void free_connect_req( void *private )
822 struct connect_req
*req
= private;
824 req
->sock
->connect_req
= NULL
;
825 release_object( req
->async
);
826 release_object( req
->iosb
);
827 release_object( req
->sock
);
831 static void complete_async_connect( struct sock
*sock
)
833 struct connect_req
*req
= sock
->connect_req
;
834 const char *in_buffer
;
838 if (debug_level
) fprintf( stderr
, "completing connect request for socket %p\n", sock
);
842 async_terminate( req
->async
, STATUS_SUCCESS
);
846 in_buffer
= (const char *)req
->iosb
->in_data
+ sizeof(struct afd_connect_params
) + req
->addr_len
;
847 len
= req
->send_len
- req
->send_cursor
;
849 ret
= send( get_unix_fd( sock
->fd
), in_buffer
+ req
->send_cursor
, len
, 0 );
850 if (ret
< 0 && errno
!= EWOULDBLOCK
)
851 async_terminate( req
->async
, sock_get_ntstatus( errno
) );
853 async_request_complete( req
->async
, STATUS_SUCCESS
, req
->send_len
, 0, NULL
);
855 req
->send_cursor
+= ret
;
858 static void free_poll_req( void *private )
860 struct poll_req
*req
= private;
863 if (req
->timeout
) remove_timeout_user( req
->timeout
);
865 for (i
= 0; i
< req
->count
; ++i
)
866 release_object( req
->sockets
[i
].sock
);
867 release_object( req
->async
);
868 release_object( req
->iosb
);
869 list_remove( &req
->entry
);
873 static int is_oobinline( struct sock
*sock
)
876 socklen_t len
= sizeof(oobinline
);
877 return !getsockopt( get_unix_fd( sock
->fd
), SOL_SOCKET
, SO_OOBINLINE
, (char *)&oobinline
, &len
) && oobinline
;
880 static int get_poll_flags( struct sock
*sock
, int event
)
884 /* A connection-mode socket which has never been connected does not return
885 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
886 if (sock
->state
== SOCK_UNCONNECTED
)
887 event
&= ~(POLLOUT
| POLLHUP
);
891 if (sock
->state
== SOCK_LISTENING
)
892 flags
|= AFD_POLL_ACCEPT
;
894 flags
|= AFD_POLL_READ
;
897 flags
|= is_oobinline( sock
) ? AFD_POLL_READ
: AFD_POLL_OOB
;
899 flags
|= AFD_POLL_WRITE
;
900 if (sock
->state
== SOCK_CONNECTED
)
901 flags
|= AFD_POLL_CONNECT
;
903 flags
|= AFD_POLL_HUP
;
905 flags
|= AFD_POLL_CONNECT_ERR
;
907 flags
|= AFD_POLL_RESET
;
912 static void complete_async_poll( struct poll_req
*req
, unsigned int status
)
914 unsigned int i
, signaled_count
= 0;
916 for (i
= 0; i
< req
->count
; ++i
)
918 struct sock
*sock
= req
->sockets
[i
].sock
;
920 if (sock
->main_poll
== req
)
921 sock
->main_poll
= NULL
;
926 for (i
= 0; i
< req
->count
; ++i
)
928 if (req
->sockets
[i
].flags
)
933 if (is_machine_64bit( async_get_thread( req
->async
)->process
->machine
))
935 size_t output_size
= offsetof( struct afd_poll_params_64
, sockets
[signaled_count
] );
936 struct afd_poll_params_64
*output
;
938 if (!(output
= mem_alloc( output_size
)))
940 async_terminate( req
->async
, get_error() );
943 memset( output
, 0, output_size
);
944 output
->timeout
= req
->orig_timeout
;
945 output
->exclusive
= req
->exclusive
;
946 for (i
= 0; i
< req
->count
; ++i
)
948 if (!req
->sockets
[i
].flags
) continue;
949 output
->sockets
[output
->count
].socket
= req
->sockets
[i
].handle
;
950 output
->sockets
[output
->count
].flags
= req
->sockets
[i
].flags
;
951 output
->sockets
[output
->count
].status
= req
->sockets
[i
].status
;
954 assert( output
->count
== signaled_count
);
956 async_request_complete( req
->async
, status
, output_size
, output_size
, output
);
960 size_t output_size
= offsetof( struct afd_poll_params_32
, sockets
[signaled_count
] );
961 struct afd_poll_params_32
*output
;
963 if (!(output
= mem_alloc( output_size
)))
965 async_terminate( req
->async
, get_error() );
968 memset( output
, 0, output_size
);
969 output
->timeout
= req
->orig_timeout
;
970 output
->exclusive
= req
->exclusive
;
971 for (i
= 0; i
< req
->count
; ++i
)
973 if (!req
->sockets
[i
].flags
) continue;
974 output
->sockets
[output
->count
].socket
= req
->sockets
[i
].handle
;
975 output
->sockets
[output
->count
].flags
= req
->sockets
[i
].flags
;
976 output
->sockets
[output
->count
].status
= req
->sockets
[i
].status
;
979 assert( output
->count
== signaled_count
);
981 async_request_complete( req
->async
, status
, output_size
, output_size
, output
);
985 static void complete_async_polls( struct sock
*sock
, int event
, int error
)
987 int flags
= get_poll_flags( sock
, event
);
988 struct poll_req
*req
, *next
;
990 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &poll_list
, struct poll_req
, entry
)
994 if (req
->iosb
->status
!= STATUS_PENDING
) continue;
996 for (i
= 0; i
< req
->count
; ++i
)
998 if (req
->sockets
[i
].sock
!= sock
) continue;
999 if (!(req
->sockets
[i
].mask
& flags
)) continue;
1002 fprintf( stderr
, "completing poll for socket %p, wanted %#x got %#x\n",
1003 sock
, req
->sockets
[i
].mask
, flags
);
1005 req
->sockets
[i
].flags
= req
->sockets
[i
].mask
& flags
;
1006 req
->sockets
[i
].status
= sock_get_ntstatus( error
);
1010 complete_async_poll( req
, STATUS_SUCCESS
);
1017 static void async_poll_timeout( void *private )
1019 struct poll_req
*req
= private;
1021 req
->timeout
= NULL
;
1023 if (req
->iosb
->status
!= STATUS_PENDING
) return;
1025 complete_async_poll( req
, STATUS_TIMEOUT
);
1028 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
1030 if (event
& (POLLIN
| POLLPRI
))
1032 struct accept_req
*req
;
1034 LIST_FOR_EACH_ENTRY( req
, &sock
->accept_list
, struct accept_req
, entry
)
1036 if (req
->iosb
->status
== STATUS_PENDING
&& !req
->accepted
)
1038 complete_async_accept( sock
, req
);
1044 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
1045 complete_async_accept_recv( sock
->accept_recv_req
);
1048 if ((event
& POLLOUT
) && sock
->connect_req
&& sock
->connect_req
->iosb
->status
== STATUS_PENDING
)
1049 complete_async_connect( sock
);
1051 if ((event
& (POLLIN
| POLLPRI
)) && async_queued( &sock
->read_q
))
1053 if (async_waiting( &sock
->read_q
))
1055 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
1056 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
1058 event
&= ~(POLLIN
| POLLPRI
);
1061 if ((event
& POLLOUT
) && async_queued( &sock
->write_q
))
1063 if (async_waiting( &sock
->write_q
))
1065 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
1066 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
1071 if (event
& (POLLERR
| POLLHUP
))
1073 int status
= sock_get_ntstatus( error
);
1074 struct accept_req
*req
, *next
;
1076 async_wake_up( &sock
->read_q
, status
);
1077 async_wake_up( &sock
->write_q
, status
);
1079 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
1081 if (req
->iosb
->status
== STATUS_PENDING
)
1082 async_terminate( req
->async
, status
);
1085 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
1086 async_terminate( sock
->accept_recv_req
->async
, status
);
1088 if (sock
->connect_req
)
1089 async_terminate( sock
->connect_req
->async
, status
);
1094 async_wake_up( &sock
->read_q
, STATUS_CONNECTION_RESET
);
1095 async_wake_up( &sock
->write_q
, STATUS_CONNECTION_RESET
);
1097 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
1098 async_terminate( sock
->accept_recv_req
->async
, STATUS_CONNECTION_RESET
);
1104 static void post_socket_event( struct sock
*sock
, enum afd_poll_bit event_bit
)
1106 unsigned int event
= (1 << event_bit
);
1108 if (!(sock
->reported_events
& event
))
1110 sock
->pending_events
|= event
;
1111 sock
->reported_events
|= event
;
1115 static void sock_dispatch_events( struct sock
*sock
, enum connection_state prevstate
, int event
)
1119 case SOCK_UNCONNECTED
:
1122 case SOCK_CONNECTING
:
1123 if (event
& POLLOUT
)
1124 post_socket_event( sock
, AFD_POLL_BIT_CONNECT
);
1125 if (event
& (POLLERR
| POLLHUP
))
1126 post_socket_event( sock
, AFD_POLL_BIT_CONNECT_ERR
);
1129 case SOCK_LISTENING
:
1130 if (event
& (POLLIN
| POLLERR
| POLLHUP
))
1131 post_socket_event( sock
, AFD_POLL_BIT_ACCEPT
);
1134 case SOCK_CONNECTED
:
1135 case SOCK_CONNECTIONLESS
:
1137 post_socket_event( sock
, AFD_POLL_BIT_RESET
);
1140 post_socket_event( sock
, AFD_POLL_BIT_READ
);
1142 if (event
& POLLOUT
)
1143 post_socket_event( sock
, AFD_POLL_BIT_WRITE
);
1145 if (event
& POLLPRI
)
1146 post_socket_event( sock
, AFD_POLL_BIT_OOB
);
1148 if (event
& (POLLERR
| POLLHUP
))
1149 post_socket_event( sock
, AFD_POLL_BIT_HUP
);
1153 sock_wake_up( sock
);
1156 static void sock_poll_event( struct fd
*fd
, int event
)
1158 struct sock
*sock
= get_fd_user( fd
);
1159 int hangup_seen
= 0;
1160 enum connection_state prevstate
= sock
->state
;
1163 assert( sock
->obj
.ops
== &sock_ops
);
1165 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
1167 if (event
& (POLLERR
| POLLHUP
))
1168 error
= sock_error( sock
);
1170 switch (sock
->state
)
1172 case SOCK_UNCONNECTED
:
1175 case SOCK_CONNECTING
:
1176 if (event
& (POLLERR
|POLLHUP
))
1178 sock
->state
= SOCK_UNCONNECTED
;
1181 else if (event
& POLLOUT
)
1183 sock
->state
= SOCK_CONNECTED
;
1184 sock
->connect_time
= current_time
;
1185 sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] = 0;
1189 case SOCK_LISTENING
:
1192 case SOCK_CONNECTED
:
1193 case SOCK_CONNECTIONLESS
:
1195 event
&= ~(POLLIN
| POLLERR
| POLLHUP
);
1197 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
1202 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
1203 * has been closed, so we need to check for it explicitly here */
1204 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
1213 /* EAGAIN can happen if an async recv() falls between the server's poll()
1214 call and the invocation of this routine */
1215 if (errno
== ECONNRESET
|| errno
== EPIPE
)
1219 else if (errno
!= EAGAIN
)
1223 sock
->errors
[AFD_POLL_BIT_HUP
] = error
;
1225 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
1230 if (hangup_seen
|| (sock_shutdown_type
== SOCK_SHUTDOWN_POLLHUP
&& (event
& POLLHUP
)))
1234 else if (event
& (POLLHUP
| POLLERR
))
1239 fprintf( stderr
, "socket %p aborted by error %d, event %#x\n", sock
, error
, event
);
1247 event
= sock_dispatch_asyncs( sock
, event
, error
);
1248 sock_dispatch_events( sock
, prevstate
, event
);
1249 complete_async_polls( sock
, event
, error
);
1251 sock_reselect( sock
);
1254 static void sock_dump( struct object
*obj
, int verbose
)
1256 struct sock
*sock
= (struct sock
*)obj
;
1257 assert( obj
->ops
== &sock_ops
);
1258 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
1259 sock
->fd
, sock
->state
,
1260 sock
->mask
, sock
->pending_events
, sock
->reported_events
);
1263 static int poll_flags_from_afd( struct sock
*sock
, int flags
)
1267 /* A connection-mode socket which has never been connected does
1268 * not return write or hangup events, but Linux returns
1269 * POLLOUT | POLLHUP. */
1270 if (sock
->state
== SOCK_UNCONNECTED
)
1273 if (flags
& (AFD_POLL_READ
| AFD_POLL_ACCEPT
))
1275 if ((flags
& AFD_POLL_HUP
) && sock
->type
== WS_SOCK_STREAM
)
1277 if (flags
& AFD_POLL_OOB
)
1278 ev
|= is_oobinline( sock
) ? POLLIN
: POLLPRI
;
1279 if (flags
& AFD_POLL_WRITE
)
1285 static int sock_get_poll_events( struct fd
*fd
)
1287 struct sock
*sock
= get_fd_user( fd
);
1288 unsigned int mask
= sock
->mask
& ~sock
->reported_events
;
1289 struct poll_req
*req
;
1292 assert( sock
->obj
.ops
== &sock_ops
);
1294 if (!sock
->type
) /* not initialized yet */
1297 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1301 for (i
= 0; i
< req
->count
; ++i
)
1303 if (req
->sockets
[i
].sock
!= sock
) continue;
1305 ev
|= poll_flags_from_afd( sock
, req
->sockets
[i
].mask
);
1309 switch (sock
->state
)
1311 case SOCK_UNCONNECTED
:
1312 /* A connection-mode Windows socket which has never been connected does
1313 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1314 * need to return -1 here, to prevent the socket from being polled on at
1318 case SOCK_CONNECTING
:
1321 case SOCK_LISTENING
:
1322 if (!list_empty( &sock
->accept_list
) || (mask
& AFD_POLL_ACCEPT
))
1326 case SOCK_CONNECTED
:
1327 case SOCK_CONNECTIONLESS
:
1328 if (sock
->hangup
&& sock
->wr_shutdown
&& !sock
->wr_shutdown_pending
)
1330 /* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
1331 * if both the socket and its peer are SHUT_WR.
1333 * We don't use SHUT_RD, so we can only encounter this in the latter
1334 * case. In that case there can't be any pending read requests (they
1335 * would have already been completed with a length of zero), the
1336 * above condition ensures that we don't have any pending write
1337 * requests, and nothing that can change about the socket state that
1338 * would complete a pending poll request. */
1342 if (sock
->aborted
|| sock
->reset
)
1345 if (sock
->accept_recv_req
)
1349 else if (async_queued( &sock
->read_q
))
1351 /* Clear POLLIN and POLLPRI if we have an alerted async, even if
1352 * we're polling this socket for READ or OOB. We can't signal the
1353 * poll if the pending async will read all of the data [cf. the
1354 * matching logic in sock_dispatch_asyncs()], but we also don't
1355 * want to spin polling for POLLIN if we're not going to use it. */
1356 if (async_waiting( &sock
->read_q
))
1357 ev
|= POLLIN
| POLLPRI
;
1359 ev
&= ~(POLLIN
| POLLPRI
);
1363 /* Don't ask for POLLIN if we got a hangup. We won't receive more
1364 * data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
1367 if (mask
& AFD_POLL_READ
)
1369 if (mask
& AFD_POLL_OOB
)
1373 /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
1374 if (sock
->state
== SOCK_CONNECTED
&& (mask
& AFD_POLL_HUP
) && !(sock
->reported_events
& AFD_POLL_READ
))
1378 if (async_queued( &sock
->write_q
))
1380 /* As with read asyncs above, clear POLLOUT if we have an alerted
1382 if (async_waiting( &sock
->write_q
))
1387 else if (!sock
->wr_shutdown
&& (mask
& AFD_POLL_WRITE
))
1398 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
1400 return FD_TYPE_SOCKET
;
1403 static void sock_cancel_async( struct fd
*fd
, struct async
*async
)
1405 struct poll_req
*req
;
1407 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1411 if (req
->async
!= async
)
1414 for (i
= 0; i
< req
->count
; i
++)
1416 struct sock
*sock
= req
->sockets
[i
].sock
;
1418 if (sock
->main_poll
== req
)
1419 sock
->main_poll
= NULL
;
1423 async_terminate( async
, STATUS_CANCELLED
);
1426 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
1428 struct sock
*sock
= get_fd_user( fd
);
1430 if (sock
->wr_shutdown_pending
&& list_empty( &sock
->write_q
.queue
))
1432 shutdown( get_unix_fd( sock
->fd
), SHUT_WR
);
1433 sock
->wr_shutdown_pending
= 0;
1436 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1437 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1439 if (queue
!= &sock
->ifchange_q
&& sock
->type
)
1440 sock_reselect( sock
);
1443 static struct fd
*sock_get_fd( struct object
*obj
)
1445 struct sock
*sock
= (struct sock
*)obj
;
1446 return (struct fd
*)grab_object( sock
->fd
);
1449 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
1451 struct sock
*sock
= (struct sock
*)obj
;
1453 if (sock
->obj
.handle_count
== 1) /* last handle */
1455 struct accept_req
*accept_req
, *accept_next
;
1456 struct poll_req
*poll_req
, *poll_next
;
1458 if (sock
->accept_recv_req
)
1459 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
1461 LIST_FOR_EACH_ENTRY_SAFE( accept_req
, accept_next
, &sock
->accept_list
, struct accept_req
, entry
)
1462 async_terminate( accept_req
->async
, STATUS_CANCELLED
);
1464 if (sock
->connect_req
)
1465 async_terminate( sock
->connect_req
->async
, STATUS_CANCELLED
);
1467 LIST_FOR_EACH_ENTRY_SAFE( poll_req
, poll_next
, &poll_list
, struct poll_req
, entry
)
1469 struct iosb
*iosb
= poll_req
->iosb
;
1470 BOOL signaled
= FALSE
;
1473 if (iosb
->status
!= STATUS_PENDING
) continue;
1475 for (i
= 0; i
< poll_req
->count
; ++i
)
1477 if (poll_req
->sockets
[i
].sock
== sock
)
1480 poll_req
->sockets
[i
].flags
= AFD_POLL_CLOSE
;
1481 poll_req
->sockets
[i
].status
= 0;
1485 if (signaled
) complete_async_poll( poll_req
, STATUS_SUCCESS
);
1492 static void sock_destroy( struct object
*obj
)
1494 struct sock
*sock
= (struct sock
*)obj
;
1496 assert( obj
->ops
== &sock_ops
);
1498 /* FIXME: special socket shutdown stuff? */
1500 if ( sock
->deferred
)
1501 release_object( sock
->deferred
);
1503 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
1504 sock_release_ifchange( sock
);
1505 free_async_queue( &sock
->read_q
);
1506 free_async_queue( &sock
->write_q
);
1507 free_async_queue( &sock
->ifchange_q
);
1508 free_async_queue( &sock
->accept_q
);
1509 free_async_queue( &sock
->connect_q
);
1510 free_async_queue( &sock
->poll_q
);
1511 if (sock
->event
) release_object( sock
->event
);
1512 if (sock
->fd
) release_object( sock
->fd
);
1515 static struct sock
*create_socket(void)
1519 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
1521 sock
->state
= SOCK_UNCONNECTED
;
1523 sock
->pending_events
= 0;
1524 sock
->reported_events
= 0;
1532 sock
->connect_time
= 0;
1533 sock
->deferred
= NULL
;
1534 sock
->ifchange_obj
= NULL
;
1535 sock
->accept_recv_req
= NULL
;
1536 sock
->connect_req
= NULL
;
1537 sock
->main_poll
= NULL
;
1538 memset( &sock
->addr
, 0, sizeof(sock
->addr
) );
1540 sock
->rd_shutdown
= 0;
1541 sock
->wr_shutdown
= 0;
1542 sock
->wr_shutdown_pending
= 0;
1545 sock
->nonblocking
= 0;
1552 sock
->icmp_fixup_data_len
= 0;
1553 init_async_queue( &sock
->read_q
);
1554 init_async_queue( &sock
->write_q
);
1555 init_async_queue( &sock
->ifchange_q
);
1556 init_async_queue( &sock
->accept_q
);
1557 init_async_queue( &sock
->connect_q
);
1558 init_async_queue( &sock
->poll_q
);
1559 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
1560 list_init( &sock
->accept_list
);
1564 static int get_unix_family( int family
)
1568 case WS_AF_INET
: return AF_INET
;
1569 case WS_AF_INET6
: return AF_INET6
;
1571 case WS_AF_IPX
: return AF_IPX
;
1574 case WS_AF_IRDA
: return AF_IRDA
;
1576 case WS_AF_UNSPEC
: return AF_UNSPEC
;
1581 static int get_unix_type( int type
)
1585 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
1586 case WS_SOCK_RAW
: return SOCK_RAW
;
1587 case WS_SOCK_STREAM
: return SOCK_STREAM
;
1592 static int get_unix_protocol( int protocol
)
1594 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1599 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
1600 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1601 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1602 case WS_IPPROTO_IPV4
: return IPPROTO_IPIP
;
1603 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1604 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1605 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1606 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1611 static void set_dont_fragment( int fd
, int level
, int value
)
1615 if (level
== IPPROTO_IP
)
1618 optname
= IP_DONTFRAG
;
1619 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1620 optname
= IP_MTU_DISCOVER
;
1621 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1628 #ifdef IPV6_DONTFRAG
1629 optname
= IPV6_DONTFRAG
;
1630 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1631 optname
= IPV6_MTU_DISCOVER
;
1632 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1638 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1641 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
)
1643 unsigned int options
= 0;
1644 int sockfd
, unix_type
, unix_family
, unix_protocol
, value
;
1647 unix_family
= get_unix_family( family
);
1648 unix_type
= get_unix_type( type
);
1649 unix_protocol
= get_unix_protocol( protocol
);
1651 if (unix_protocol
< 0)
1653 if (type
&& unix_type
< 0)
1654 set_win32_error( WSAESOCKTNOSUPPORT
);
1656 set_win32_error( WSAEPROTONOSUPPORT
);
1659 if (unix_family
< 0)
1661 if (family
>= 0 && unix_type
< 0)
1662 set_win32_error( WSAESOCKTNOSUPPORT
);
1664 set_win32_error( WSAEAFNOSUPPORT
);
1668 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1671 if (sockfd
== -1 && errno
== EPERM
&& unix_family
== AF_INET
1672 && unix_type
== SOCK_RAW
&& unix_protocol
== IPPROTO_ICMP
)
1674 sockfd
= socket( unix_family
, SOCK_DGRAM
, unix_protocol
);
1679 setsockopt( sockfd
, IPPROTO_IP
, IP_RECVTTL
, (const char *)&val
, sizeof(val
) );
1680 setsockopt( sockfd
, IPPROTO_IP
, IP_RECVTOS
, (const char *)&val
, sizeof(val
) );
1681 setsockopt( sockfd
, IPPROTO_IP
, IP_PKTINFO
, (const char *)&val
, sizeof(val
) );
1688 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1689 else set_win32_error( sock_get_error( errno
));
1692 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1694 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1697 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1700 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1703 /* Should we retrieve val using a getsockopt call and then
1704 * set the modified one? */
1705 val
.ipx_pt
= ipx_type
;
1706 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1711 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1713 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1714 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1715 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1716 else if (unix_type
== SOCK_STREAM
)
1717 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1721 if (unix_family
== AF_INET6
)
1723 static const int enable
= 1;
1724 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1728 len
= sizeof(value
);
1729 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_RCVBUF
, &value
, &len
))
1730 sock
->rcvbuf
= value
;
1732 len
= sizeof(value
);
1733 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_SNDBUF
, &value
, &len
))
1734 sock
->sndbuf
= value
;
1736 sock
->state
= (type
== WS_SOCK_STREAM
? SOCK_UNCONNECTED
: SOCK_CONNECTIONLESS
);
1737 sock
->proto
= protocol
;
1739 sock
->family
= family
;
1743 options
= get_fd_options( sock
->fd
);
1744 release_object( sock
->fd
);
1747 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1752 /* We can't immediately allow caching for a connection-mode socket, since it
1753 * might be accepted into (changing the underlying fd object.) */
1754 if (sock
->type
!= WS_SOCK_STREAM
) allow_fd_caching( sock
->fd
);
1759 /* accepts a socket and inits it */
1760 static int accept_new_fd( struct sock
*sock
)
1763 /* Try to accept(2). We can't be safe that this an already connected socket
1764 * or that accept() is allowed on it. In those cases we will get -1/errno
1767 struct sockaddr saddr
;
1768 socklen_t slen
= sizeof(saddr
);
1769 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1771 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1773 set_error( sock_get_ntstatus( errno
));
1777 /* accept a socket (creates a new fd) */
1778 static struct sock
*accept_socket( struct sock
*sock
)
1780 struct sock
*acceptsock
;
1783 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1785 if ( sock
->deferred
)
1787 acceptsock
= sock
->deferred
;
1788 sock
->deferred
= NULL
;
1792 union unix_sockaddr unix_addr
;
1795 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1796 if (!(acceptsock
= create_socket()))
1802 /* newly created socket gets the same properties of the listening socket */
1803 acceptsock
->state
= SOCK_CONNECTED
;
1804 acceptsock
->bound
= 1;
1805 acceptsock
->nonblocking
= sock
->nonblocking
;
1806 acceptsock
->mask
= sock
->mask
;
1807 acceptsock
->proto
= sock
->proto
;
1808 acceptsock
->type
= sock
->type
;
1809 acceptsock
->family
= sock
->family
;
1810 acceptsock
->window
= sock
->window
;
1811 acceptsock
->message
= sock
->message
;
1812 acceptsock
->connect_time
= current_time
;
1813 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
1814 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1815 get_fd_options( sock
->fd
) )))
1817 release_object( acceptsock
);
1820 unix_len
= sizeof(unix_addr
);
1821 if (!getsockname( acceptfd
, &unix_addr
.addr
, &unix_len
))
1822 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1825 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1826 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1827 sock_reselect( sock
);
1831 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
1833 union unix_sockaddr unix_addr
;
1838 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
1840 if ( sock
->deferred
)
1842 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
1843 get_fd_options( acceptsock
->fd
) );
1847 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
1849 release_object( sock
->deferred
);
1850 sock
->deferred
= NULL
;
1854 if ((acceptfd
= accept_new_fd( sock
)) == -1)
1857 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1858 get_fd_options( acceptsock
->fd
) )))
1862 acceptsock
->state
= SOCK_CONNECTED
;
1863 acceptsock
->bound
= 1;
1864 acceptsock
->pending_events
= 0;
1865 acceptsock
->reported_events
= 0;
1866 acceptsock
->proto
= sock
->proto
;
1867 acceptsock
->type
= sock
->type
;
1868 acceptsock
->family
= sock
->family
;
1869 acceptsock
->wparam
= 0;
1870 acceptsock
->deferred
= NULL
;
1871 acceptsock
->connect_time
= current_time
;
1872 fd_copy_completion( acceptsock
->fd
, newfd
);
1873 release_object( acceptsock
->fd
);
1874 acceptsock
->fd
= newfd
;
1876 unix_len
= sizeof(unix_addr
);
1877 if (!getsockname( get_unix_fd( newfd
), &unix_addr
.addr
, &unix_len
))
1878 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1881 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1882 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1883 sock_reselect( sock
);
1890 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
1892 static const int enable
= 1;
1895 if (!(index
= if_nametoindex( name
)))
1898 if (setsockopt( fd
, IPPROTO_IP
, IP_BOUND_IF
, &index
, sizeof(index
) ))
1901 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
1904 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER) && defined(SO_BINDTODEVICE)
1906 struct interface_filter
1908 struct sock_filter iface_memaddr
;
1909 struct sock_filter iface_rule
;
1910 struct sock_filter ip_memaddr
;
1911 struct sock_filter ip_rule
;
1912 struct sock_filter return_keep
;
1913 struct sock_filter return_dump
;
1915 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
1916 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1917 /sizeof(struct sock_filter)
1918 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
1919 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1920 /sizeof(struct sock_filter)
1921 # define FILTER_JUMP_NEXT() (u_char)(0)
1922 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
1923 static struct interface_filter generic_interface_filter
=
1925 /* This filter rule allows incoming packets on the specified interface, which works for all
1926 * remotely generated packets and for locally generated broadcast packets. */
1927 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
1928 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
1929 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
1930 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
1931 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
1932 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
1933 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
1934 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
1937 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
1939 struct interface_filter specific_interface_filter
;
1940 struct sock_fprog filter_prog
;
1941 static const int enable
= 1;
1945 if (!setsockopt( fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen( name
) + 1 ))
1948 /* SO_BINDTODEVICE requires NET_CAP_RAW until Linux 5.7. */
1950 fprintf( stderr
, "setsockopt SO_BINDTODEVICE fd %d, name %s failed: %s, falling back to SO_REUSE_ADDR\n",
1951 fd
, name
, strerror( errno
));
1953 if (!(index
= if_nametoindex( name
)))
1956 ifindex
= htonl( index
);
1957 if (setsockopt( fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
) ) < 0)
1960 specific_interface_filter
= generic_interface_filter
;
1961 specific_interface_filter
.iface_rule
.k
= index
;
1962 specific_interface_filter
.ip_rule
.k
= htonl( bind_addr
);
1963 filter_prog
.len
= sizeof(generic_interface_filter
) / sizeof(struct sock_filter
);
1964 filter_prog
.filter
= (struct sock_filter
*)&specific_interface_filter
;
1965 if (setsockopt( fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
) ))
1968 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
1973 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
1979 #endif /* LINUX_BOUND_IF */
1981 /* Take bind() calls on any name corresponding to a local network adapter and
1982 * restrict the given socket to operating only on the specified interface. This
1983 * restriction consists of two components:
1984 * 1) An outgoing packet restriction suggesting the egress interface for all
1986 * 2) An incoming packet restriction dropping packets not meant for the
1988 * If the function succeeds in placing these restrictions, then the name for the
1989 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
1990 * receipt of broadcast packets on the socket. This behavior is only relevant to
1991 * UDP sockets and is needed for applications that expect to be able to receive
1992 * broadcast packets on a socket that is bound to a specific network interface.
1994 static int bind_to_interface( struct sock
*sock
, const struct sockaddr_in
*addr
)
1996 in_addr_t bind_addr
= addr
->sin_addr
.s_addr
;
1997 struct ifaddrs
*ifaddrs
, *ifaddr
;
1998 int fd
= get_unix_fd( sock
->fd
);
2001 if (bind_addr
== htonl( INADDR_ANY
) || bind_addr
== htonl( INADDR_LOOPBACK
))
2003 if (sock
->type
!= WS_SOCK_DGRAM
)
2006 if (getifaddrs( &ifaddrs
) < 0) return 0;
2008 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
2010 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET
2011 && ((struct sockaddr_in
*)ifaddr
->ifa_addr
)->sin_addr
.s_addr
== bind_addr
)
2013 if ((err
= bind_to_iface_name( fd
, bind_addr
, ifaddr
->ifa_name
)) < 0)
2016 fprintf( stderr
, "failed to bind to interface: %s\n", strerror( errno
) );
2021 freeifaddrs( ifaddrs
);
2025 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2026 static unsigned int get_ipv6_interface_index( const struct in6_addr
*addr
)
2028 struct ifaddrs
*ifaddrs
, *ifaddr
;
2030 if (getifaddrs( &ifaddrs
) < 0) return 0;
2032 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
2034 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET6
2035 && !memcmp( &((struct sockaddr_in6
*)ifaddr
->ifa_addr
)->sin6_addr
, addr
, sizeof(*addr
) ))
2037 unsigned int index
= if_nametoindex( ifaddr
->ifa_name
);
2042 fprintf( stderr
, "Unable to look up interface index for %s: %s\n",
2043 ifaddr
->ifa_name
, strerror( errno
) );
2047 freeifaddrs( ifaddrs
);
2052 freeifaddrs( ifaddrs
);
2057 /* return an errno value mapped to a WSA error */
2058 static unsigned int sock_get_error( int err
)
2062 case EINTR
: return WSAEINTR
;
2063 case EBADF
: return WSAEBADF
;
2065 case EACCES
: return WSAEACCES
;
2066 case EFAULT
: return WSAEFAULT
;
2067 case EINVAL
: return WSAEINVAL
;
2068 case EMFILE
: return WSAEMFILE
;
2070 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
2071 case EALREADY
: return WSAEALREADY
;
2072 case ENOTSOCK
: return WSAENOTSOCK
;
2073 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
2074 case EMSGSIZE
: return WSAEMSGSIZE
;
2075 case EPROTOTYPE
: return WSAEPROTOTYPE
;
2076 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
2077 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
2078 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
2079 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
2080 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
2081 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
2082 case EADDRINUSE
: return WSAEADDRINUSE
;
2083 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
2084 case ENETDOWN
: return WSAENETDOWN
;
2085 case ENETUNREACH
: return WSAENETUNREACH
;
2086 case ENETRESET
: return WSAENETRESET
;
2087 case ECONNABORTED
: return WSAECONNABORTED
;
2089 case ECONNRESET
: return WSAECONNRESET
;
2090 case ENOBUFS
: return WSAENOBUFS
;
2091 case EISCONN
: return WSAEISCONN
;
2092 case ENOTCONN
: return WSAENOTCONN
;
2093 case ESHUTDOWN
: return WSAESHUTDOWN
;
2094 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
2095 case ETIMEDOUT
: return WSAETIMEDOUT
;
2096 case ECONNREFUSED
: return WSAECONNREFUSED
;
2097 case ELOOP
: return WSAELOOP
;
2098 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
2099 case EHOSTDOWN
: return WSAEHOSTDOWN
;
2100 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
2101 case ENOTEMPTY
: return WSAENOTEMPTY
;
2103 case EPROCLIM
: return WSAEPROCLIM
;
2106 case EUSERS
: return WSAEUSERS
;
2109 case EDQUOT
: return WSAEDQUOT
;
2112 case ESTALE
: return WSAESTALE
;
2115 case EREMOTE
: return WSAEREMOTE
;
2121 perror("wineserver: sock_get_error() can't map error");
2126 static int sock_get_ntstatus( int err
)
2130 case EBADF
: return STATUS_INVALID_HANDLE
;
2131 case EBUSY
: return STATUS_DEVICE_BUSY
;
2133 case EACCES
: return STATUS_ACCESS_DENIED
;
2134 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
2135 case EINVAL
: return STATUS_INVALID_PARAMETER
;
2137 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
2139 case EWOULDBLOCK
: return STATUS_DEVICE_NOT_READY
;
2140 case EALREADY
: return STATUS_NETWORK_BUSY
;
2141 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
2142 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
2143 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
2144 case EPROTONOSUPPORT
:
2145 case ESOCKTNOSUPPORT
:
2148 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
2149 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
2150 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
2151 case EADDRINUSE
: return STATUS_SHARING_VIOLATION
;
2152 /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
2153 * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
2155 case EADDRNOTAVAIL
: return STATUS_INVALID_ADDRESS_COMPONENT
;
2156 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
2157 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
2158 case ENOTCONN
: return STATUS_INVALID_CONNECTION
;
2159 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
2160 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
2161 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
2162 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
2164 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
2165 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
2166 case EISCONN
: return STATUS_CONNECTION_ACTIVE
;
2168 case 0: return STATUS_SUCCESS
;
2171 perror("wineserver: sock_get_ntstatus() can't map error");
2172 return STATUS_UNSUCCESSFUL
;
2176 static struct accept_req
*alloc_accept_req( struct sock
*sock
, struct sock
*acceptsock
, struct async
*async
,
2177 const struct afd_accept_into_params
*params
)
2179 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
2183 req
->async
= (struct async
*)grab_object( async
);
2184 req
->iosb
= async_get_iosb( async
);
2185 req
->sock
= (struct sock
*)grab_object( sock
);
2186 req
->acceptsock
= acceptsock
;
2187 if (acceptsock
) grab_object( acceptsock
);
2193 req
->recv_len
= params
->recv_len
;
2194 req
->local_len
= params
->local_len
;
2200 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2202 struct sock
*sock
= get_fd_user( fd
);
2205 assert( sock
->obj
.ops
== &sock_ops
);
2207 if (code
!= IOCTL_AFD_WINE_CREATE
&& code
!= IOCTL_AFD_POLL
&& (unix_fd
= get_unix_fd( fd
)) < 0)
2212 case IOCTL_AFD_WINE_CREATE
:
2214 const struct afd_create_params
*params
= get_req_data();
2216 if (get_req_data_size() != sizeof(*params
))
2218 set_error( STATUS_INVALID_PARAMETER
);
2221 init_socket( sock
, params
->family
, params
->type
, params
->protocol
);
2225 case IOCTL_AFD_WINE_ACCEPT
:
2227 struct sock
*acceptsock
;
2228 obj_handle_t handle
;
2230 if (get_reply_max_size() != sizeof(handle
))
2232 set_error( STATUS_BUFFER_TOO_SMALL
);
2236 if (!(acceptsock
= accept_socket( sock
)))
2238 struct accept_req
*req
;
2240 if (sock
->nonblocking
) return;
2241 if (get_error() != STATUS_DEVICE_NOT_READY
) return;
2243 if (!(req
= alloc_accept_req( sock
, NULL
, async
, NULL
))) return;
2244 list_add_tail( &sock
->accept_list
, &req
->entry
);
2246 async_set_completion_callback( async
, free_accept_req
, req
);
2247 queue_async( &sock
->accept_q
, async
);
2248 sock_reselect( sock
);
2249 set_error( STATUS_PENDING
);
2252 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
2253 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
2254 acceptsock
->wparam
= handle
;
2255 sock_reselect( acceptsock
);
2256 release_object( acceptsock
);
2257 set_reply_data( &handle
, sizeof(handle
) );
2261 case IOCTL_AFD_WINE_ACCEPT_INTO
:
2263 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
2264 const struct afd_accept_into_params
*params
= get_req_data();
2265 struct sock
*acceptsock
;
2266 unsigned int remote_len
;
2267 struct accept_req
*req
;
2269 if (get_req_data_size() != sizeof(*params
) ||
2270 get_reply_max_size() < params
->recv_len
||
2271 get_reply_max_size() - params
->recv_len
< params
->local_len
)
2273 set_error( STATUS_BUFFER_TOO_SMALL
);
2277 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
2278 if (remote_len
< sizeof(int))
2280 set_error( STATUS_INVALID_PARAMETER
);
2284 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
2287 if (acceptsock
->accept_recv_req
)
2289 release_object( acceptsock
);
2290 set_error( STATUS_INVALID_PARAMETER
);
2294 if (!(req
= alloc_accept_req( sock
, acceptsock
, async
, params
)))
2296 release_object( acceptsock
);
2299 list_add_tail( &sock
->accept_list
, &req
->entry
);
2300 acceptsock
->accept_recv_req
= req
;
2301 release_object( acceptsock
);
2303 acceptsock
->wparam
= params
->accept_handle
;
2304 async_set_completion_callback( async
, free_accept_req
, req
);
2305 queue_async( &sock
->accept_q
, async
);
2306 sock_reselect( sock
);
2307 set_error( STATUS_PENDING
);
2311 case IOCTL_AFD_LISTEN
:
2313 const struct afd_listen_params
*params
= get_req_data();
2315 if (get_req_data_size() < sizeof(*params
))
2317 set_error( STATUS_INVALID_PARAMETER
);
2323 set_error( STATUS_INVALID_PARAMETER
);
2327 if (listen( unix_fd
, params
->backlog
) < 0)
2329 set_error( sock_get_ntstatus( errno
) );
2333 sock
->state
= SOCK_LISTENING
;
2335 /* a listening socket can no longer be accepted into */
2336 allow_fd_caching( sock
->fd
);
2338 /* we may already be selecting for AFD_POLL_ACCEPT */
2339 sock_reselect( sock
);
2343 case IOCTL_AFD_WINE_CONNECT
:
2345 const struct afd_connect_params
*params
= get_req_data();
2346 const struct WS_sockaddr
*addr
;
2347 union unix_sockaddr unix_addr
;
2348 struct connect_req
*req
;
2352 if (get_req_data_size() < sizeof(*params
) ||
2353 get_req_data_size() - sizeof(*params
) < params
->addr_len
)
2355 set_error( STATUS_BUFFER_TOO_SMALL
);
2358 send_len
= get_req_data_size() - sizeof(*params
) - params
->addr_len
;
2359 addr
= (const struct WS_sockaddr
*)(params
+ 1);
2361 if (!params
->synchronous
&& !sock
->bound
)
2363 set_error( STATUS_INVALID_PARAMETER
);
2367 if (sock
->accept_recv_req
)
2369 set_error( STATUS_INVALID_PARAMETER
);
2373 if (sock
->connect_req
)
2375 set_error( STATUS_INVALID_PARAMETER
);
2379 switch (sock
->state
)
2381 case SOCK_LISTENING
:
2382 set_error( STATUS_INVALID_PARAMETER
);
2385 case SOCK_CONNECTING
:
2386 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2387 * but there's no status code that maps to WSAEALREADY... */
2388 set_error( params
->synchronous
? STATUS_ADDRESS_ALREADY_ASSOCIATED
: STATUS_INVALID_PARAMETER
);
2391 case SOCK_CONNECTED
:
2392 set_error( STATUS_CONNECTION_ACTIVE
);
2395 case SOCK_UNCONNECTED
:
2396 case SOCK_CONNECTIONLESS
:
2400 unix_len
= sockaddr_to_unix( addr
, params
->addr_len
, &unix_addr
);
2403 set_error( STATUS_INVALID_ADDRESS
);
2406 if (unix_addr
.addr
.sa_family
== AF_INET
&& !memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 ))
2407 unix_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_LOOPBACK
);
2409 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2410 if (ret
< 0 && errno
!= EINPROGRESS
)
2412 set_error( sock_get_ntstatus( errno
) );
2416 /* a connected or connecting socket can no longer be accepted into */
2417 allow_fd_caching( sock
->fd
);
2419 unix_len
= sizeof(unix_addr
);
2420 if (!getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
))
2421 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2426 if (sock
->type
!= WS_SOCK_DGRAM
)
2428 sock
->state
= SOCK_CONNECTED
;
2429 sock
->connect_time
= current_time
;
2432 if (!send_len
) return;
2435 if (sock
->type
!= WS_SOCK_DGRAM
)
2436 sock
->state
= SOCK_CONNECTING
;
2438 if (params
->synchronous
&& sock
->nonblocking
)
2440 sock_reselect( sock
);
2441 set_error( STATUS_DEVICE_NOT_READY
);
2445 if (!(req
= mem_alloc( sizeof(*req
) )))
2448 req
->async
= (struct async
*)grab_object( async
);
2449 req
->iosb
= async_get_iosb( async
);
2450 req
->sock
= (struct sock
*)grab_object( sock
);
2451 req
->addr_len
= params
->addr_len
;
2452 req
->send_len
= send_len
;
2453 req
->send_cursor
= 0;
2455 async_set_completion_callback( async
, free_connect_req
, req
);
2456 sock
->connect_req
= req
;
2457 queue_async( &sock
->connect_q
, async
);
2458 sock_reselect( sock
);
2459 set_error( STATUS_PENDING
);
2463 case IOCTL_AFD_WINE_SHUTDOWN
:
2467 if (get_req_data_size() < sizeof(int))
2469 set_error( STATUS_BUFFER_TOO_SMALL
);
2472 how
= *(int *)get_req_data();
2476 set_error( STATUS_INVALID_PARAMETER
);
2480 if (sock
->state
!= SOCK_CONNECTED
&& sock
->state
!= SOCK_CONNECTIONLESS
)
2482 set_error( STATUS_INVALID_CONNECTION
);
2488 sock
->rd_shutdown
= 1;
2490 if (how
!= SD_RECEIVE
)
2492 sock
->wr_shutdown
= 1;
2493 if (list_empty( &sock
->write_q
.queue
))
2494 shutdown( unix_fd
, SHUT_WR
);
2496 sock
->wr_shutdown_pending
= 1;
2501 if (sock
->event
) release_object( sock
->event
);
2505 sock
->nonblocking
= 1;
2508 sock_reselect( sock
);
2512 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE
:
2516 if (get_req_data_size() < sizeof(int))
2518 set_error( STATUS_BUFFER_TOO_SMALL
);
2521 force_async
= *(int *)get_req_data();
2523 if (sock
->nonblocking
&& !force_async
)
2525 set_error( STATUS_DEVICE_NOT_READY
);
2528 if (!sock_get_ifchange( sock
)) return;
2529 queue_async( &sock
->ifchange_q
, async
);
2530 set_error( STATUS_PENDING
);
2534 case IOCTL_AFD_WINE_FIONBIO
:
2535 if (get_req_data_size() < sizeof(int))
2537 set_error( STATUS_BUFFER_TOO_SMALL
);
2540 if (*(int *)get_req_data())
2542 sock
->nonblocking
= 1;
2548 set_error( STATUS_INVALID_PARAMETER
);
2551 sock
->nonblocking
= 0;
2555 case IOCTL_AFD_GET_EVENTS
:
2557 struct afd_get_events_params params
= {0};
2560 if (get_reply_max_size() < sizeof(params
))
2562 set_error( STATUS_INVALID_PARAMETER
);
2566 params
.flags
= sock
->pending_events
& sock
->mask
;
2567 for (i
= 0; i
< ARRAY_SIZE( params
.status
); ++i
)
2568 params
.status
[i
] = sock_get_ntstatus( sock
->errors
[i
] );
2570 sock
->pending_events
&= ~sock
->mask
;
2571 sock_reselect( sock
);
2573 set_reply_data( ¶ms
, sizeof(params
) );
2577 case IOCTL_AFD_EVENT_SELECT
:
2579 struct event
*event
= NULL
;
2580 obj_handle_t event_handle
;
2583 set_async_pending( async
);
2585 if (is_machine_64bit( current
->process
->machine
))
2587 const struct afd_event_select_params_64
*params
= get_req_data();
2589 if (get_req_data_size() < sizeof(*params
))
2591 set_error( STATUS_INVALID_PARAMETER
);
2595 event_handle
= params
->event
;
2596 mask
= params
->mask
;
2600 const struct afd_event_select_params_32
*params
= get_req_data();
2602 if (get_req_data_size() < sizeof(*params
))
2604 set_error( STATUS_INVALID_PARAMETER
);
2608 event_handle
= params
->event
;
2609 mask
= params
->mask
;
2612 if ((event_handle
|| mask
) &&
2613 !(event
= get_event_obj( current
->process
, event_handle
, EVENT_MODIFY_STATE
)))
2615 set_error( STATUS_INVALID_PARAMETER
);
2619 if (sock
->event
) release_object( sock
->event
);
2620 sock
->event
= event
;
2625 sock
->nonblocking
= 1;
2627 sock_reselect( sock
);
2628 /* Explicitly wake the socket up if the mask causes it to become
2629 * signaled. Note that reselecting isn't enough, since we might already
2630 * have had events recorded in sock->reported_events and we don't want
2631 * to select for them again. */
2632 sock_wake_up( sock
);
2637 case IOCTL_AFD_WINE_MESSAGE_SELECT
:
2639 const struct afd_message_select_params
*params
= get_req_data();
2641 if (get_req_data_size() < sizeof(params
))
2643 set_error( STATUS_BUFFER_TOO_SMALL
);
2647 if (sock
->event
) release_object( sock
->event
);
2651 sock
->pending_events
= 0;
2652 sock
->reported_events
= 0;
2655 sock
->mask
= params
->mask
;
2656 sock
->window
= params
->window
;
2657 sock
->message
= params
->message
;
2658 sock
->wparam
= params
->handle
;
2659 sock
->nonblocking
= 1;
2661 sock_reselect( sock
);
2666 case IOCTL_AFD_BIND
:
2668 const struct afd_bind_params
*params
= get_req_data();
2669 union unix_sockaddr unix_addr
, bind_addr
;
2670 data_size_t in_size
;
2673 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2675 if (get_req_data_size() < get_reply_max_size())
2677 set_error( STATUS_BUFFER_TOO_SMALL
);
2680 in_size
= get_req_data_size() - get_reply_max_size();
2681 if (in_size
< offsetof(struct afd_bind_params
, addr
.sa_data
)
2682 || get_reply_max_size() < in_size
- sizeof(int))
2684 set_error( STATUS_INVALID_PARAMETER
);
2690 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED
);
2694 unix_len
= sockaddr_to_unix( ¶ms
->addr
, in_size
- sizeof(int), &unix_addr
);
2697 set_error( STATUS_INVALID_ADDRESS
);
2700 bind_addr
= unix_addr
;
2702 if (unix_addr
.addr
.sa_family
== AF_INET
)
2704 if (!memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 )
2705 || bind_to_interface( sock
, &unix_addr
.in
))
2706 bind_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
2708 else if (unix_addr
.addr
.sa_family
== AF_INET6
)
2710 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2711 /* Windows allows specifying zero to use the default scope. Linux
2712 * interprets it as an interface index and requires that it be
2714 if (!unix_addr
.in6
.sin6_scope_id
)
2715 bind_addr
.in6
.sin6_scope_id
= get_ipv6_interface_index( &unix_addr
.in6
.sin6_addr
);
2719 set_async_pending( async
);
2721 if (bind( unix_fd
, &bind_addr
.addr
, unix_len
) < 0)
2723 if (errno
== EADDRINUSE
)
2726 socklen_t len
= sizeof(reuse
);
2728 if (!getsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&reuse
, &len
) && reuse
)
2732 set_error( sock_get_ntstatus( errno
) );
2738 unix_len
= sizeof(bind_addr
);
2739 if (!getsockname( unix_fd
, &bind_addr
.addr
, &unix_len
))
2741 /* store the interface or magic loopback address instead of the
2742 * actual unix address */
2743 if (bind_addr
.addr
.sa_family
== AF_INET
)
2744 bind_addr
.in
.sin_addr
= unix_addr
.in
.sin_addr
;
2745 sock
->addr_len
= sockaddr_from_unix( &bind_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2748 if (get_reply_max_size() >= sock
->addr_len
)
2749 set_reply_data( &sock
->addr
, sock
->addr_len
);
2753 case IOCTL_AFD_GETSOCKNAME
:
2756 set_error( STATUS_INVALID_PARAMETER
);
2760 if (get_reply_max_size() < sock
->addr_len
)
2762 set_error( STATUS_BUFFER_TOO_SMALL
);
2766 set_reply_data( &sock
->addr
, sock
->addr_len
);
2769 case IOCTL_AFD_WINE_DEFER
:
2771 const obj_handle_t
*handle
= get_req_data();
2772 struct sock
*acceptsock
;
2774 if (get_req_data_size() < sizeof(*handle
))
2776 set_error( STATUS_BUFFER_TOO_SMALL
);
2780 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, *handle
, 0, &sock_ops
);
2781 if (!acceptsock
) return;
2783 sock
->deferred
= acceptsock
;
2787 case IOCTL_AFD_WINE_GET_INFO
:
2789 struct afd_get_info_params params
;
2791 if (get_reply_max_size() < sizeof(params
))
2793 set_error( STATUS_BUFFER_TOO_SMALL
);
2797 params
.family
= sock
->family
;
2798 params
.type
= sock
->type
;
2799 params
.protocol
= sock
->proto
;
2800 set_reply_data( ¶ms
, sizeof(params
) );
2804 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN
:
2806 int listening
= (sock
->state
== SOCK_LISTENING
);
2808 if (get_reply_max_size() < sizeof(listening
))
2810 set_error( STATUS_BUFFER_TOO_SMALL
);
2814 set_reply_data( &listening
, sizeof(listening
) );
2818 case IOCTL_AFD_WINE_GET_SO_ERROR
:
2823 if (get_reply_max_size() < sizeof(error
))
2825 set_error( STATUS_BUFFER_TOO_SMALL
);
2829 error
= sock_error( sock
);
2832 for (i
= 0; i
< ARRAY_SIZE( sock
->errors
); ++i
)
2834 if (sock
->errors
[i
])
2836 error
= sock
->errors
[i
];
2842 error
= sock_get_error( error
);
2843 set_reply_data( &error
, sizeof(error
) );
2847 case IOCTL_AFD_WINE_GET_SO_RCVBUF
:
2849 int rcvbuf
= sock
->rcvbuf
;
2851 if (get_reply_max_size() < sizeof(rcvbuf
))
2853 set_error( STATUS_BUFFER_TOO_SMALL
);
2857 set_reply_data( &rcvbuf
, sizeof(rcvbuf
) );
2861 case IOCTL_AFD_WINE_SET_SO_RCVBUF
:
2865 if (get_req_data_size() < sizeof(rcvbuf
))
2867 set_error( STATUS_BUFFER_TOO_SMALL
);
2870 rcvbuf
= *(DWORD
*)get_req_data();
2872 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_RCVBUF
, (char *)&rcvbuf
, sizeof(rcvbuf
) ))
2873 sock
->rcvbuf
= rcvbuf
;
2875 set_error( sock_get_ntstatus( errno
) );
2879 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO
:
2881 DWORD rcvtimeo
= sock
->rcvtimeo
;
2883 if (get_reply_max_size() < sizeof(rcvtimeo
))
2885 set_error( STATUS_BUFFER_TOO_SMALL
);
2889 set_reply_data( &rcvtimeo
, sizeof(rcvtimeo
) );
2893 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO
:
2897 if (get_req_data_size() < sizeof(rcvtimeo
))
2899 set_error( STATUS_BUFFER_TOO_SMALL
);
2902 rcvtimeo
= *(DWORD
*)get_req_data();
2904 sock
->rcvtimeo
= rcvtimeo
;
2908 case IOCTL_AFD_WINE_GET_SO_SNDBUF
:
2910 int sndbuf
= sock
->sndbuf
;
2912 if (get_reply_max_size() < sizeof(sndbuf
))
2914 set_error( STATUS_BUFFER_TOO_SMALL
);
2918 set_reply_data( &sndbuf
, sizeof(sndbuf
) );
2922 case IOCTL_AFD_WINE_SET_SO_SNDBUF
:
2926 if (get_req_data_size() < sizeof(sndbuf
))
2928 set_error( STATUS_BUFFER_TOO_SMALL
);
2931 sndbuf
= *(DWORD
*)get_req_data();
2936 /* setsockopt fails if a zero value is passed */
2937 sock
->sndbuf
= sndbuf
;
2942 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_SNDBUF
, (char *)&sndbuf
, sizeof(sndbuf
) ))
2943 sock
->sndbuf
= sndbuf
;
2945 set_error( sock_get_ntstatus( errno
) );
2949 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO
:
2951 DWORD sndtimeo
= sock
->sndtimeo
;
2953 if (get_reply_max_size() < sizeof(sndtimeo
))
2955 set_error( STATUS_BUFFER_TOO_SMALL
);
2959 set_reply_data( &sndtimeo
, sizeof(sndtimeo
) );
2963 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO
:
2967 if (get_req_data_size() < sizeof(sndtimeo
))
2969 set_error( STATUS_BUFFER_TOO_SMALL
);
2972 sndtimeo
= *(DWORD
*)get_req_data();
2974 sock
->sndtimeo
= sndtimeo
;
2978 case IOCTL_AFD_WINE_GET_SO_CONNECT_TIME
:
2982 if (get_reply_max_size() < sizeof(time
))
2984 set_error( STATUS_BUFFER_TOO_SMALL
);
2988 if (sock
->state
== SOCK_CONNECTED
)
2989 time
= (current_time
- sock
->connect_time
) / 10000000;
2991 set_reply_data( &time
, sizeof(time
) );
2995 case IOCTL_AFD_POLL
:
2997 if (get_reply_max_size() < get_req_data_size())
2999 set_error( STATUS_INVALID_PARAMETER
);
3003 if (is_machine_64bit( current
->process
->machine
))
3005 const struct afd_poll_params_64
*params
= get_req_data();
3007 if (get_req_data_size() < sizeof(struct afd_poll_params_64
) ||
3008 get_req_data_size() < offsetof( struct afd_poll_params_64
, sockets
[params
->count
] ))
3010 set_error( STATUS_INVALID_PARAMETER
);
3014 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, params
->sockets
);
3018 const struct afd_poll_params_32
*params
= get_req_data();
3019 struct afd_poll_socket_64
*sockets
;
3022 if (get_req_data_size() < sizeof(struct afd_poll_params_32
) ||
3023 get_req_data_size() < offsetof( struct afd_poll_params_32
, sockets
[params
->count
] ))
3025 set_error( STATUS_INVALID_PARAMETER
);
3029 if (!(sockets
= mem_alloc( params
->count
* sizeof(*sockets
) ))) return;
3030 for (i
= 0; i
< params
->count
; ++i
)
3032 sockets
[i
].socket
= params
->sockets
[i
].socket
;
3033 sockets
[i
].flags
= params
->sockets
[i
].flags
;
3034 sockets
[i
].status
= params
->sockets
[i
].status
;
3037 poll_socket( sock
, async
, params
->exclusive
, params
->timeout
, params
->count
, sockets
);
3045 set_error( STATUS_NOT_SUPPORTED
);
3050 static void handle_exclusive_poll(struct poll_req
*req
)
3054 for (i
= 0; i
< req
->count
; ++i
)
3056 struct sock
*sock
= req
->sockets
[i
].sock
;
3057 struct poll_req
*main_poll
= sock
->main_poll
;
3059 if (main_poll
&& main_poll
->exclusive
&& req
->exclusive
)
3061 complete_async_poll( main_poll
, STATUS_SUCCESS
);
3066 sock
->main_poll
= req
;
3070 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
3071 unsigned int count
, const struct afd_poll_socket_64
*sockets
)
3073 BOOL signaled
= FALSE
;
3074 struct poll_req
*req
;
3079 set_error( STATUS_INVALID_PARAMETER
);
3083 if (!(req
= mem_alloc( offsetof( struct poll_req
, sockets
[count
] ) )))
3086 req
->timeout
= NULL
;
3088 if (timeout
&& timeout
!= TIMEOUT_INFINITE
&&
3089 !(req
->timeout
= add_timeout_user( timeout
, async_poll_timeout
, req
)))
3094 req
->orig_timeout
= timeout
;
3096 for (i
= 0; i
< count
; ++i
)
3098 req
->sockets
[i
].sock
= (struct sock
*)get_handle_obj( current
->process
, sockets
[i
].socket
, 0, &sock_ops
);
3099 if (!req
->sockets
[i
].sock
)
3101 for (j
= 0; j
< i
; ++j
) release_object( req
->sockets
[j
].sock
);
3102 if (req
->timeout
) remove_timeout_user( req
->timeout
);
3106 req
->sockets
[i
].handle
= sockets
[i
].socket
;
3107 req
->sockets
[i
].mask
= sockets
[i
].flags
;
3108 req
->sockets
[i
].flags
= 0;
3111 req
->exclusive
= exclusive
;
3113 req
->async
= (struct async
*)grab_object( async
);
3114 req
->iosb
= async_get_iosb( async
);
3116 handle_exclusive_poll(req
);
3118 list_add_tail( &poll_list
, &req
->entry
);
3119 async_set_completion_callback( async
, free_poll_req
, req
);
3120 queue_async( &poll_sock
->poll_q
, async
);
3122 for (i
= 0; i
< count
; ++i
)
3124 struct sock
*sock
= req
->sockets
[i
].sock
;
3125 int mask
= req
->sockets
[i
].mask
;
3126 struct pollfd pollfd
;
3128 pollfd
.fd
= get_unix_fd( sock
->fd
);
3129 pollfd
.events
= poll_flags_from_afd( sock
, mask
);
3130 if (pollfd
.events
>= 0 && poll( &pollfd
, 1, 0 ) >= 0)
3131 sock_poll_event( sock
->fd
, pollfd
.revents
);
3133 /* FIXME: do other error conditions deserve a similar treatment? */
3134 if (sock
->state
!= SOCK_CONNECTING
&& sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] && (mask
& AFD_POLL_CONNECT_ERR
))
3136 req
->sockets
[i
].flags
|= AFD_POLL_CONNECT_ERR
;
3137 req
->sockets
[i
].status
= sock_get_ntstatus( sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] );
3140 if (req
->sockets
[i
].flags
)
3144 if (!timeout
|| signaled
)
3145 complete_async_poll( req
, STATUS_SUCCESS
);
3149 for (i
= 0; i
< req
->count
; ++i
)
3150 sock_reselect( req
->sockets
[i
].sock
);
3151 set_error( STATUS_PENDING
);
3154 #ifdef HAVE_LINUX_RTNETLINK_H
3156 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
3157 static struct object
*ifchange_object
;
3159 static void ifchange_dump( struct object
*obj
, int verbose
);
3160 static struct fd
*ifchange_get_fd( struct object
*obj
);
3161 static void ifchange_destroy( struct object
*obj
);
3163 static int ifchange_get_poll_events( struct fd
*fd
);
3164 static void ifchange_poll_event( struct fd
*fd
, int event
);
3168 struct object obj
; /* object header */
3169 struct fd
*fd
; /* interface change file descriptor */
3170 struct list sockets
; /* list of sockets to send interface change notifications */
3173 static const struct object_ops ifchange_ops
=
3175 sizeof(struct ifchange
), /* size */
3176 &no_type
, /* type */
3177 ifchange_dump
, /* dump */
3178 no_add_queue
, /* add_queue */
3179 NULL
, /* remove_queue */
3180 NULL
, /* signaled */
3181 no_satisfied
, /* satisfied */
3182 no_signal
, /* signal */
3183 ifchange_get_fd
, /* get_fd */
3184 default_map_access
, /* map_access */
3185 default_get_sd
, /* get_sd */
3186 default_set_sd
, /* set_sd */
3187 no_get_full_name
, /* get_full_name */
3188 no_lookup_name
, /* lookup_name */
3189 no_link_name
, /* link_name */
3190 NULL
, /* unlink_name */
3191 no_open_file
, /* open_file */
3192 no_kernel_obj_list
, /* get_kernel_obj_list */
3193 no_close_handle
, /* close_handle */
3194 ifchange_destroy
/* destroy */
3197 static const struct fd_ops ifchange_fd_ops
=
3199 ifchange_get_poll_events
, /* get_poll_events */
3200 ifchange_poll_event
, /* poll_event */
3201 NULL
, /* get_fd_type */
3202 no_fd_read
, /* read */
3203 no_fd_write
, /* write */
3204 no_fd_flush
, /* flush */
3205 no_fd_get_file_info
, /* get_file_info */
3206 no_fd_get_volume_info
, /* get_volume_info */
3207 no_fd_ioctl
, /* ioctl */
3208 NULL
, /* cancel_async */
3209 NULL
, /* queue_async */
3210 NULL
/* reselect_async */
3213 static void ifchange_dump( struct object
*obj
, int verbose
)
3215 assert( obj
->ops
== &ifchange_ops
);
3216 fprintf( stderr
, "Interface change\n" );
3219 static struct fd
*ifchange_get_fd( struct object
*obj
)
3221 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3222 return (struct fd
*)grab_object( ifchange
->fd
);
3225 static void ifchange_destroy( struct object
*obj
)
3227 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3228 assert( obj
->ops
== &ifchange_ops
);
3230 release_object( ifchange
->fd
);
3232 /* reset the global ifchange object so that it will be recreated if it is needed again */
3233 assert( obj
== ifchange_object
);
3234 ifchange_object
= NULL
;
3237 static int ifchange_get_poll_events( struct fd
*fd
)
3242 /* wake up all the sockets waiting for a change notification event */
3243 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
3245 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3246 struct list
*ptr
, *next
;
3247 assert( obj
->ops
== &ifchange_ops
);
3248 assert( obj
== ifchange_object
);
3250 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
3252 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
3254 assert( sock
->ifchange_obj
);
3255 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
3256 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
3260 static void ifchange_poll_event( struct fd
*fd
, int event
)
3262 struct object
*ifchange
= get_fd_user( fd
);
3263 unsigned int status
= STATUS_PENDING
;
3264 char buffer
[PIPE_BUF
];
3267 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
3270 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
3271 return; /* retry when poll() says the socket is ready */
3272 status
= sock_get_ntstatus( errno
);
3276 struct nlmsghdr
*nlh
;
3278 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
3280 if (nlh
->nlmsg_type
== NLMSG_DONE
)
3282 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
3283 status
= STATUS_SUCCESS
;
3286 else status
= STATUS_CANCELLED
;
3288 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
3293 /* we only need one of these interface notification objects, all of the sockets dependent upon
3294 * it will wake up when a notification event occurs */
3295 static struct object
*get_ifchange( void )
3297 #ifdef HAVE_LINUX_RTNETLINK_H
3298 struct ifchange
*ifchange
;
3299 struct sockaddr_nl addr
;
3302 if (ifchange_object
)
3304 /* increment the refcount for each socket that uses the ifchange object */
3305 return grab_object( ifchange_object
);
3308 /* create the socket we need for processing interface change notifications */
3309 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
3312 set_error( sock_get_ntstatus( errno
));
3315 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
3316 memset( &addr
, 0, sizeof(addr
) );
3317 addr
.nl_family
= AF_NETLINK
;
3318 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
3319 /* bind the socket to the special netlink kernel interface */
3320 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
3323 set_error( sock_get_ntstatus( errno
));
3326 if (!(ifchange
= alloc_object( &ifchange_ops
)))
3329 set_error( STATUS_NO_MEMORY
);
3332 list_init( &ifchange
->sockets
);
3333 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
3335 release_object( ifchange
);
3336 set_error( STATUS_NO_MEMORY
);
3339 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
3341 /* the ifchange object is now successfully configured */
3342 ifchange_object
= &ifchange
->obj
;
3343 return &ifchange
->obj
;
3345 set_error( STATUS_NOT_SUPPORTED
);
3350 /* add the socket to the interface change notification list */
3351 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
3353 #ifdef HAVE_LINUX_RTNETLINK_H
3354 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3356 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
3360 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3361 static struct object
*sock_get_ifchange( struct sock
*sock
)
3363 struct object
*ifchange
;
3365 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
3366 return sock
->ifchange_obj
;
3368 if (!(ifchange
= get_ifchange()))
3371 /* add the socket to the ifchange notification list */
3372 ifchange_add_sock( ifchange
, sock
);
3373 sock
->ifchange_obj
= ifchange
;
3377 /* destroy an existing ifchange queue for a specific socket */
3378 static void sock_release_ifchange( struct sock
*sock
)
3380 if (sock
->ifchange_obj
)
3382 list_remove( &sock
->ifchange_entry
);
3383 release_object( sock
->ifchange_obj
);
3384 sock
->ifchange_obj
= NULL
;
3388 static void socket_device_dump( struct object
*obj
, int verbose
);
3389 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3390 unsigned int attr
, struct object
*root
);
3391 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3392 unsigned int sharing
, unsigned int options
);
3394 static const struct object_ops socket_device_ops
=
3396 sizeof(struct object
), /* size */
3397 &device_type
, /* type */
3398 socket_device_dump
, /* dump */
3399 no_add_queue
, /* add_queue */
3400 NULL
, /* remove_queue */
3401 NULL
, /* signaled */
3402 no_satisfied
, /* satisfied */
3403 no_signal
, /* signal */
3404 no_get_fd
, /* get_fd */
3405 default_map_access
, /* map_access */
3406 default_get_sd
, /* get_sd */
3407 default_set_sd
, /* set_sd */
3408 default_get_full_name
, /* get_full_name */
3409 socket_device_lookup_name
, /* lookup_name */
3410 directory_link_name
, /* link_name */
3411 default_unlink_name
, /* unlink_name */
3412 socket_device_open_file
, /* open_file */
3413 no_kernel_obj_list
, /* get_kernel_obj_list */
3414 no_close_handle
, /* close_handle */
3415 no_destroy
/* destroy */
3418 static void socket_device_dump( struct object
*obj
, int verbose
)
3420 fputs( "Socket device\n", stderr
);
3423 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3424 unsigned int attr
, struct object
*root
)
3426 if (name
) name
->len
= 0;
3430 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3431 unsigned int sharing
, unsigned int options
)
3435 if (!(sock
= create_socket())) return NULL
;
3436 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
3438 release_object( sock
);
3444 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
3445 unsigned int attr
, const struct security_descriptor
*sd
)
3447 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
3450 DECL_HANDLER(recv_socket
)
3452 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3453 unsigned int status
= STATUS_PENDING
;
3454 timeout_t timeout
= 0;
3455 struct async
*async
;
3461 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3462 timeout
= (timeout_t
)sock
->rcvtimeo
* -10000;
3464 if (sock
->rd_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3465 else if (!async_queued( &sock
->read_q
))
3467 /* If read_q is not empty, we cannot really tell if the already queued
3468 * asyncs will not consume all available data; if there's no data
3469 * available, the current request won't be immediately satiable.
3471 struct pollfd pollfd
;
3472 pollfd
.fd
= get_unix_fd( sock
->fd
);
3473 pollfd
.events
= req
->oob
? POLLPRI
: POLLIN
;
3475 if (poll(&pollfd
, 1, 0) >= 0 && pollfd
.revents
)
3477 /* Give the client opportunity to complete synchronously.
3478 * If it turns out that the I/O request is not actually immediately satiable,
3479 * the client may then choose to re-queue the async (with STATUS_PENDING). */
3480 status
= STATUS_ALERTED
;
3484 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3485 status
= STATUS_DEVICE_NOT_READY
;
3487 sock
->pending_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3488 sock
->reported_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3490 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3492 set_error( status
);
3495 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3497 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3498 queue_async( &sock
->read_q
, async
);
3500 /* always reselect; we changed reported_events above */
3501 sock_reselect( sock
);
3503 reply
->wait
= async_handoff( async
, NULL
, 0 );
3504 reply
->options
= get_fd_options( fd
);
3505 reply
->nonblocking
= sock
->nonblocking
;
3506 release_object( async
);
3508 release_object( sock
);
3511 static void send_socket_completion_callback( void *private )
3513 struct send_req
*send_req
= private;
3514 struct iosb
*iosb
= send_req
->iosb
;
3515 struct sock
*sock
= send_req
->sock
;
3517 if (iosb
->status
!= STATUS_SUCCESS
)
3519 /* send() calls only clear and reselect events if unsuccessful. */
3520 sock
->pending_events
&= ~AFD_POLL_WRITE
;
3521 sock
->reported_events
&= ~AFD_POLL_WRITE
;
3522 sock_reselect( sock
);
3525 release_object( iosb
);
3526 release_object( sock
);
3530 DECL_HANDLER(send_socket
)
3532 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3533 unsigned int status
= STATUS_PENDING
;
3534 timeout_t timeout
= 0;
3535 struct async
*async
;
3542 if (sock
->type
== WS_SOCK_DGRAM
&& !sock
->bound
)
3544 union unix_sockaddr unix_addr
;
3546 int unix_fd
= get_unix_fd( fd
);
3548 unix_len
= get_unix_sockaddr_any( &unix_addr
, sock
->family
);
3549 if (bind( unix_fd
, &unix_addr
.addr
, unix_len
) < 0)
3552 if (getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
) >= 0)
3554 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
3557 else if (!bind_errno
) bind_errno
= errno
;
3560 if (!req
->force_async
&& !sock
->nonblocking
&& is_fd_overlapped( fd
))
3561 timeout
= (timeout_t
)sock
->sndtimeo
* -10000;
3563 if (bind_errno
) status
= sock_get_ntstatus( bind_errno
);
3564 else if (sock
->wr_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3565 else if (!async_queued( &sock
->write_q
))
3567 /* If write_q is not empty, we cannot really tell if the already queued
3568 * asyncs will not consume all available space; if there's no space
3569 * available, the current request won't be immediately satiable.
3571 struct pollfd pollfd
;
3572 pollfd
.fd
= get_unix_fd( sock
->fd
);
3573 pollfd
.events
= POLLOUT
;
3575 if (poll(&pollfd
, 1, 0) >= 0 && pollfd
.revents
)
3577 /* Give the client opportunity to complete synchronously.
3578 * If it turns out that the I/O request is not actually immediately satiable,
3579 * the client may then choose to re-queue the async (with STATUS_PENDING). */
3580 status
= STATUS_ALERTED
;
3584 if (status
== STATUS_PENDING
&& !req
->force_async
&& sock
->nonblocking
)
3585 status
= STATUS_DEVICE_NOT_READY
;
3587 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3589 struct send_req
*send_req
;
3590 struct iosb
*iosb
= async_get_iosb( async
);
3592 if ((send_req
= mem_alloc( sizeof(*send_req
) )))
3594 send_req
->iosb
= (struct iosb
*)grab_object( iosb
);
3595 send_req
->sock
= (struct sock
*)grab_object( sock
);
3596 async_set_completion_callback( async
, send_socket_completion_callback
, send_req
);
3598 else if (status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
)
3599 status
= STATUS_NO_MEMORY
;
3601 release_object( iosb
);
3603 set_error( status
);
3606 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3608 if (status
== STATUS_PENDING
|| status
== STATUS_ALERTED
)
3610 queue_async( &sock
->write_q
, async
);
3611 sock_reselect( sock
);
3614 reply
->wait
= async_handoff( async
, NULL
, 0 );
3615 reply
->options
= get_fd_options( fd
);
3616 reply
->nonblocking
= sock
->nonblocking
;
3617 release_object( async
);
3619 release_object( sock
);
3622 DECL_HANDLER(socket_send_icmp_id
)
3624 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, 0, &sock_ops
);
3628 if (sock
->icmp_fixup_data_len
== MAX_ICMP_HISTORY_LENGTH
)
3630 memmove( sock
->icmp_fixup_data
, sock
->icmp_fixup_data
+ 1,
3631 sizeof(*sock
->icmp_fixup_data
) * (MAX_ICMP_HISTORY_LENGTH
- 1) );
3632 --sock
->icmp_fixup_data_len
;
3635 sock
->icmp_fixup_data
[sock
->icmp_fixup_data_len
].icmp_id
= req
->icmp_id
;
3636 sock
->icmp_fixup_data
[sock
->icmp_fixup_data_len
].icmp_seq
= req
->icmp_seq
;
3637 ++sock
->icmp_fixup_data_len
;
3639 release_object( sock
);
3642 DECL_HANDLER(socket_get_icmp_id
)
3644 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, 0, &sock_ops
);
3649 for (i
= 0; i
< sock
->icmp_fixup_data_len
; ++i
)
3651 if (sock
->icmp_fixup_data
[i
].icmp_seq
== req
->icmp_seq
)
3653 reply
->icmp_id
= sock
->icmp_fixup_data
[i
].icmp_id
;
3654 --sock
->icmp_fixup_data_len
;
3655 memmove( &sock
->icmp_fixup_data
[i
], &sock
->icmp_fixup_data
[i
+ 1],
3656 (sock
->icmp_fixup_data_len
- i
) * sizeof(*sock
->icmp_fixup_data
) );
3657 release_object( sock
);
3662 set_error( STATUS_NOT_FOUND
);
3663 release_object( sock
);