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>
46 #include <sys/types.h>
47 #ifdef HAVE_SYS_SOCKET_H
48 # include <sys/socket.h>
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
53 #ifdef HAVE_SYS_FILIO_H
54 # include <sys/filio.h>
59 #ifdef HAVE_LINUX_FILTER_H
60 # include <linux/filter.h>
62 #ifdef HAVE_LINUX_RTNETLINK_H
63 # include <linux/rtnetlink.h>
66 #ifdef HAVE_NETIPX_IPX_H
67 # include <netipx/ipx.h>
68 #elif defined(HAVE_LINUX_IPX_H)
69 # ifdef HAVE_ASM_TYPES_H
70 # include <asm/types.h>
72 # ifdef HAVE_LINUX_TYPES_H
73 # include <linux/types.h>
75 # include <linux/ipx.h>
77 #if defined(SOL_IPX) || defined(SO_DEFAULT_HEADERS)
81 #ifdef HAVE_LINUX_IRDA_H
82 # ifdef HAVE_LINUX_TYPES_H
83 # include <linux/types.h>
85 # include <linux/irda.h>
90 #define WIN32_NO_STATUS
108 #if defined(linux) && !defined(IP_UNICAST_IF)
109 #define IP_UNICAST_IF 50
112 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
116 struct WS_sockaddr addr
;
117 struct WS_sockaddr_in in
;
118 struct WS_sockaddr_in6 in6
;
119 struct WS_sockaddr_ipx ipx
;
123 static struct list poll_list
= LIST_INIT( poll_list
);
130 struct timeout_user
*timeout
;
133 struct poll_socket_output
*output
;
146 struct sock
*sock
, *acceptsock
;
148 unsigned int recv_len
, local_len
;
156 unsigned int addr_len
, send_len
, send_cursor
;
159 enum connection_state
170 struct object obj
; /* object header */
171 struct fd
*fd
; /* socket file descriptor */
172 enum connection_state state
; /* connection state */
173 unsigned int mask
; /* event mask */
174 /* pending AFD_POLL_* events which have not yet been reported to the application */
175 unsigned int pending_events
;
176 /* AFD_POLL_* events which have already been reported and should not be
177 * selected for again until reset by a relevant call.
179 * For example, if AFD_POLL_READ is set here and not in pending_events, it
180 * has already been reported and consumed, and we should not report it
181 * again, even if POLLIN is signaled, until it is reset by e.g recv().
183 * If an event has been signaled and not consumed yet, it will be set in
184 * both pending_events and reported_events (as we should only ever report
185 * any event once until it is reset.) */
186 unsigned int reported_events
;
187 unsigned int flags
; /* socket flags */
188 unsigned short proto
; /* socket protocol */
189 unsigned short type
; /* socket type */
190 unsigned short family
; /* socket family */
191 struct event
*event
; /* event object */
192 user_handle_t window
; /* window to send the message to */
193 unsigned int message
; /* message to send */
194 obj_handle_t wparam
; /* message wparam (socket handle) */
195 int errors
[AFD_POLL_BIT_COUNT
]; /* event errors */
196 timeout_t connect_time
;/* time the socket was connected */
197 struct sock
*deferred
; /* socket that waits for a deferred accept */
198 struct async_queue read_q
; /* queue for asynchronous reads */
199 struct async_queue write_q
; /* queue for asynchronous writes */
200 struct async_queue ifchange_q
; /* queue for interface change notifications */
201 struct async_queue accept_q
; /* queue for asynchronous accepts */
202 struct async_queue connect_q
; /* queue for asynchronous connects */
203 struct async_queue poll_q
; /* queue for asynchronous polls */
204 struct object
*ifchange_obj
; /* the interface change notification object */
205 struct list ifchange_entry
; /* entry in ifchange notification list */
206 struct list accept_list
; /* list of pending accept requests */
207 struct accept_req
*accept_recv_req
; /* pending accept-into request which will recv on this socket */
208 struct connect_req
*connect_req
; /* pending connection request */
209 struct poll_req
*main_poll
; /* main poll */
210 union win_sockaddr addr
; /* socket name */
211 int addr_len
; /* socket name length */
212 unsigned int rcvbuf
; /* advisory recv buffer size */
213 unsigned int sndbuf
; /* advisory send buffer size */
214 unsigned int rcvtimeo
; /* receive timeout in ms */
215 unsigned int sndtimeo
; /* send timeout in ms */
216 unsigned int rd_shutdown
: 1; /* is the read end shut down? */
217 unsigned int wr_shutdown
: 1; /* is the write end shut down? */
218 unsigned int wr_shutdown_pending
: 1; /* is a write shutdown pending? */
219 unsigned int hangup
: 1; /* has the read end received a hangup? */
220 unsigned int aborted
: 1; /* did we get a POLLERR or irregular POLLHUP? */
221 unsigned int nonblocking
: 1; /* is the socket nonblocking? */
222 unsigned int bound
: 1; /* is the socket bound? */
225 static void sock_dump( struct object
*obj
, int verbose
);
226 static struct fd
*sock_get_fd( struct object
*obj
);
227 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
228 static void sock_destroy( struct object
*obj
);
229 static struct object
*sock_get_ifchange( struct sock
*sock
);
230 static void sock_release_ifchange( struct sock
*sock
);
232 static int sock_get_poll_events( struct fd
*fd
);
233 static void sock_poll_event( struct fd
*fd
, int event
);
234 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
235 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
236 static void sock_cancel_async( struct fd
*fd
, struct async
*async
);
237 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
);
238 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
240 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
);
241 static struct sock
*accept_socket( struct sock
*sock
);
242 static int sock_get_ntstatus( int err
);
243 static unsigned int sock_get_error( int err
);
245 static const struct object_ops sock_ops
=
247 sizeof(struct sock
), /* size */
248 &file_type
, /* type */
249 sock_dump
, /* dump */
250 add_queue
, /* add_queue */
251 remove_queue
, /* remove_queue */
252 default_fd_signaled
, /* signaled */
253 no_satisfied
, /* satisfied */
254 no_signal
, /* signal */
255 sock_get_fd
, /* get_fd */
256 default_map_access
, /* map_access */
257 default_get_sd
, /* get_sd */
258 default_set_sd
, /* set_sd */
259 no_get_full_name
, /* get_full_name */
260 no_lookup_name
, /* lookup_name */
261 no_link_name
, /* link_name */
262 NULL
, /* unlink_name */
263 no_open_file
, /* open_file */
264 no_kernel_obj_list
, /* get_kernel_obj_list */
265 sock_close_handle
, /* close_handle */
266 sock_destroy
/* destroy */
269 static const struct fd_ops sock_fd_ops
=
271 sock_get_poll_events
, /* get_poll_events */
272 sock_poll_event
, /* poll_event */
273 sock_get_fd_type
, /* get_fd_type */
274 no_fd_read
, /* read */
275 no_fd_write
, /* write */
276 no_fd_flush
, /* flush */
277 default_fd_get_file_info
, /* get_file_info */
278 no_fd_get_volume_info
, /* get_volume_info */
279 sock_ioctl
, /* ioctl */
280 sock_cancel_async
, /* cancel_async */
281 sock_queue_async
, /* queue_async */
282 sock_reselect_async
/* reselect_async */
287 struct sockaddr addr
;
288 struct sockaddr_in in
;
289 struct sockaddr_in6 in6
;
291 struct sockaddr_ipx ipx
;
294 struct sockaddr_irda irda
;
298 static int sockaddr_from_unix( const union unix_sockaddr
*uaddr
, struct WS_sockaddr
*wsaddr
, socklen_t wsaddrlen
)
300 memset( wsaddr
, 0, wsaddrlen
);
302 switch (uaddr
->addr
.sa_family
)
306 struct WS_sockaddr_in win
= {0};
308 if (wsaddrlen
< sizeof(win
)) return -1;
309 win
.sin_family
= WS_AF_INET
;
310 win
.sin_port
= uaddr
->in
.sin_port
;
311 memcpy( &win
.sin_addr
, &uaddr
->in
.sin_addr
, sizeof(win
.sin_addr
) );
312 memcpy( wsaddr
, &win
, sizeof(win
) );
318 struct WS_sockaddr_in6 win
= {0};
320 if (wsaddrlen
< sizeof(win
)) return -1;
321 win
.sin6_family
= WS_AF_INET6
;
322 win
.sin6_port
= uaddr
->in6
.sin6_port
;
323 win
.sin6_flowinfo
= uaddr
->in6
.sin6_flowinfo
;
324 memcpy( &win
.sin6_addr
, &uaddr
->in6
.sin6_addr
, sizeof(win
.sin6_addr
) );
325 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
326 win
.sin6_scope_id
= uaddr
->in6
.sin6_scope_id
;
328 memcpy( wsaddr
, &win
, sizeof(win
) );
335 struct WS_sockaddr_ipx win
= {0};
337 if (wsaddrlen
< sizeof(win
)) return -1;
338 win
.sa_family
= WS_AF_IPX
;
339 memcpy( win
.sa_netnum
, &uaddr
->ipx
.sipx_network
, sizeof(win
.sa_netnum
) );
340 memcpy( win
.sa_nodenum
, &uaddr
->ipx
.sipx_node
, sizeof(win
.sa_nodenum
) );
341 win
.sa_socket
= uaddr
->ipx
.sipx_port
;
342 memcpy( wsaddr
, &win
, sizeof(win
) );
352 if (wsaddrlen
< sizeof(win
)) return -1;
353 win
.irdaAddressFamily
= WS_AF_IRDA
;
354 memcpy( win
.irdaDeviceID
, &uaddr
->irda
.sir_addr
, sizeof(win
.irdaDeviceID
) );
355 if (uaddr
->irda
.sir_lsap_sel
!= LSAP_ANY
)
356 snprintf( win
.irdaServiceName
, sizeof(win
.irdaServiceName
), "LSAP-SEL%u", uaddr
->irda
.sir_lsap_sel
);
358 memcpy( win
.irdaServiceName
, uaddr
->irda
.sir_name
, sizeof(win
.irdaServiceName
) );
359 memcpy( wsaddr
, &win
, sizeof(win
) );
373 static socklen_t
sockaddr_to_unix( const struct WS_sockaddr
*wsaddr
, int wsaddrlen
, union unix_sockaddr
*uaddr
)
375 memset( uaddr
, 0, sizeof(*uaddr
) );
377 switch (wsaddr
->sa_family
)
381 struct WS_sockaddr_in win
= {0};
383 if (wsaddrlen
< sizeof(win
)) return 0;
384 memcpy( &win
, wsaddr
, sizeof(win
) );
385 uaddr
->in
.sin_family
= AF_INET
;
386 uaddr
->in
.sin_port
= win
.sin_port
;
387 memcpy( &uaddr
->in
.sin_addr
, &win
.sin_addr
, sizeof(win
.sin_addr
) );
388 return sizeof(uaddr
->in
);
393 struct WS_sockaddr_in6 win
= {0};
395 if (wsaddrlen
< sizeof(win
)) return 0;
396 memcpy( &win
, wsaddr
, sizeof(win
) );
397 uaddr
->in6
.sin6_family
= AF_INET6
;
398 uaddr
->in6
.sin6_port
= win
.sin6_port
;
399 uaddr
->in6
.sin6_flowinfo
= win
.sin6_flowinfo
;
400 memcpy( &uaddr
->in6
.sin6_addr
, &win
.sin6_addr
, sizeof(win
.sin6_addr
) );
401 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
402 uaddr
->in6
.sin6_scope_id
= win
.sin6_scope_id
;
404 return sizeof(uaddr
->in6
);
410 struct WS_sockaddr_ipx win
= {0};
412 if (wsaddrlen
< sizeof(win
)) return 0;
413 memcpy( &win
, wsaddr
, sizeof(win
) );
414 uaddr
->ipx
.sipx_family
= AF_IPX
;
415 memcpy( &uaddr
->ipx
.sipx_network
, win
.sa_netnum
, sizeof(win
.sa_netnum
) );
416 memcpy( &uaddr
->ipx
.sipx_node
, win
.sa_nodenum
, sizeof(win
.sa_nodenum
) );
417 uaddr
->ipx
.sipx_port
= win
.sa_socket
;
418 return sizeof(uaddr
->ipx
);
425 SOCKADDR_IRDA win
= {0};
426 unsigned int lsap_sel
;
428 if (wsaddrlen
< sizeof(win
)) return 0;
429 memcpy( &win
, wsaddr
, sizeof(win
) );
430 uaddr
->irda
.sir_family
= AF_IRDA
;
431 if (sscanf( win
.irdaServiceName
, "LSAP-SEL%u", &lsap_sel
) == 1)
432 uaddr
->irda
.sir_lsap_sel
= lsap_sel
;
435 uaddr
->irda
.sir_lsap_sel
= LSAP_ANY
;
436 memcpy( uaddr
->irda
.sir_name
, win
.irdaServiceName
, sizeof(win
.irdaServiceName
) );
438 memcpy( &uaddr
->irda
.sir_addr
, win
.irdaDeviceID
, sizeof(win
.irdaDeviceID
) );
439 return sizeof(uaddr
->irda
);
446 default: /* likely an ipv4 address */
447 case sizeof(struct WS_sockaddr_in
):
448 return sizeof(uaddr
->in
);
451 case sizeof(struct WS_sockaddr_ipx
):
452 return sizeof(uaddr
->ipx
);
456 case sizeof(SOCKADDR_IRDA
):
457 return sizeof(uaddr
->irda
);
460 case sizeof(struct WS_sockaddr_in6
):
461 return sizeof(uaddr
->in6
);
469 /* some events are generated at the same time but must be sent in a particular
470 * order (e.g. CONNECT must be sent before READ) */
471 static const enum afd_poll_bit event_bitorder
[] =
473 AFD_POLL_BIT_CONNECT
,
474 AFD_POLL_BIT_CONNECT_ERR
,
485 SOCK_SHUTDOWN_ERROR
= -1,
486 SOCK_SHUTDOWN_EOF
= 0,
487 SOCK_SHUTDOWN_POLLHUP
= 1
490 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
492 static sock_shutdown_t
sock_check_pollhup(void)
494 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
499 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
500 if ( shutdown( fd
[0], 1 ) ) goto out
;
506 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
507 n
= poll( &pfd
, 1, 1 );
508 if ( n
!= 1 ) goto out
; /* error or timeout */
509 if ( pfd
.revents
& POLLHUP
)
510 ret
= SOCK_SHUTDOWN_POLLHUP
;
511 else if ( pfd
.revents
& POLLIN
&&
512 read( fd
[1], &dummy
, 1 ) == 0 )
513 ret
= SOCK_SHUTDOWN_EOF
;
523 sock_shutdown_type
= sock_check_pollhup();
525 switch ( sock_shutdown_type
)
527 case SOCK_SHUTDOWN_EOF
:
528 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
530 case SOCK_SHUTDOWN_POLLHUP
:
531 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
534 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
535 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
539 static int sock_reselect( struct sock
*sock
)
541 int ev
= sock_get_poll_events( sock
->fd
);
544 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
546 set_fd_events( sock
->fd
, ev
);
550 static unsigned int afd_poll_flag_to_win32( unsigned int flags
)
552 static const unsigned int map
[] =
556 FD_WRITE
, /* WRITE */
558 FD_CLOSE
, /* RESET */
560 FD_CONNECT
, /* CONNECT */
561 FD_ACCEPT
, /* ACCEPT */
562 FD_CONNECT
, /* CONNECT_ERR */
565 unsigned int i
, ret
= 0;
567 for (i
= 0; i
< ARRAY_SIZE(map
); ++i
)
569 if (flags
& (1 << i
)) ret
|= map
[i
];
575 /* wake anybody waiting on the socket event or send the associated message */
576 static void sock_wake_up( struct sock
*sock
)
578 unsigned int events
= sock
->pending_events
& sock
->mask
;
583 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
585 set_event( sock
->event
);
589 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
590 for (i
= 0; i
< ARRAY_SIZE(event_bitorder
); i
++)
592 enum afd_poll_bit event
= event_bitorder
[i
];
593 if (events
& (1 << event
))
595 lparam_t lparam
= afd_poll_flag_to_win32(1 << event
) | (sock_get_error( sock
->errors
[event
] ) << 16);
596 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
599 sock
->pending_events
= 0;
600 sock_reselect( sock
);
604 static inline int sock_error( struct fd
*fd
)
606 unsigned int optval
= 0;
607 socklen_t optlen
= sizeof(optval
);
609 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
613 static void free_accept_req( void *private )
615 struct accept_req
*req
= private;
616 list_remove( &req
->entry
);
619 req
->acceptsock
->accept_recv_req
= NULL
;
620 release_object( req
->acceptsock
);
622 release_object( req
->async
);
623 release_object( req
->iosb
);
624 release_object( req
->sock
);
628 static void fill_accept_output( struct accept_req
*req
)
630 const data_size_t out_size
= req
->iosb
->out_size
;
631 struct async
*async
= req
->async
;
632 union unix_sockaddr unix_addr
;
633 struct WS_sockaddr
*win_addr
;
634 unsigned int remote_len
;
640 if (!(out_data
= mem_alloc( out_size
)))
642 async_terminate( async
, get_error() );
646 fd
= get_unix_fd( req
->acceptsock
->fd
);
648 if (req
->recv_len
&& (size
= recv( fd
, out_data
, req
->recv_len
, 0 )) < 0)
650 if (!req
->accepted
&& errno
== EWOULDBLOCK
)
653 sock_reselect( req
->acceptsock
);
657 async_terminate( async
, sock_get_ntstatus( errno
) );
664 if (req
->local_len
< sizeof(int))
666 async_terminate( async
, STATUS_BUFFER_TOO_SMALL
);
671 unix_len
= sizeof(unix_addr
);
672 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ sizeof(int));
673 if (getsockname( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
674 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, req
->local_len
- sizeof(int) )) < 0)
676 async_terminate( async
, sock_get_ntstatus( errno
) );
680 memcpy( out_data
+ req
->recv_len
, &win_len
, sizeof(int) );
683 unix_len
= sizeof(unix_addr
);
684 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ req
->local_len
+ sizeof(int));
685 remote_len
= out_size
- req
->recv_len
- req
->local_len
;
686 if (getpeername( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
687 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, remote_len
- sizeof(int) )) < 0)
689 async_terminate( async
, sock_get_ntstatus( errno
) );
693 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
695 async_request_complete( req
->async
, STATUS_SUCCESS
, size
, out_size
, out_data
);
698 static void complete_async_accept( struct sock
*sock
, struct accept_req
*req
)
700 struct sock
*acceptsock
= req
->acceptsock
;
701 struct async
*async
= req
->async
;
703 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
707 if (!accept_into_socket( sock
, acceptsock
))
709 async_terminate( async
, get_error() );
712 fill_accept_output( req
);
718 if (!(acceptsock
= accept_socket( sock
)))
720 async_terminate( async
, get_error() );
723 handle
= alloc_handle_no_access_check( async_get_thread( async
)->process
, &acceptsock
->obj
,
724 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
725 acceptsock
->wparam
= handle
;
726 release_object( acceptsock
);
729 async_terminate( async
, get_error() );
733 async_request_complete_alloc( req
->async
, STATUS_SUCCESS
, 0, sizeof(handle
), &handle
);
737 static void complete_async_accept_recv( struct accept_req
*req
)
739 if (debug_level
) fprintf( stderr
, "completing accept recv request for socket %p\n", req
->acceptsock
);
741 assert( req
->recv_len
);
743 fill_accept_output( req
);
746 static void free_connect_req( void *private )
748 struct connect_req
*req
= private;
750 req
->sock
->connect_req
= NULL
;
751 release_object( req
->async
);
752 release_object( req
->iosb
);
753 release_object( req
->sock
);
757 static void complete_async_connect( struct sock
*sock
)
759 struct connect_req
*req
= sock
->connect_req
;
760 const char *in_buffer
;
764 if (debug_level
) fprintf( stderr
, "completing connect request for socket %p\n", sock
);
766 sock
->state
= SOCK_CONNECTED
;
770 async_terminate( req
->async
, STATUS_SUCCESS
);
774 in_buffer
= (const char *)req
->iosb
->in_data
+ sizeof(struct afd_connect_params
) + req
->addr_len
;
775 len
= req
->send_len
- req
->send_cursor
;
777 ret
= send( get_unix_fd( sock
->fd
), in_buffer
+ req
->send_cursor
, len
, 0 );
778 if (ret
< 0 && errno
!= EWOULDBLOCK
)
779 async_terminate( req
->async
, sock_get_ntstatus( errno
) );
781 async_request_complete( req
->async
, STATUS_SUCCESS
, req
->send_len
, 0, NULL
);
783 req
->send_cursor
+= ret
;
786 static void free_poll_req( void *private )
788 struct poll_req
*req
= private;
791 if (req
->timeout
) remove_timeout_user( req
->timeout
);
793 for (i
= 0; i
< req
->count
; ++i
)
794 release_object( req
->sockets
[i
].sock
);
795 release_object( req
->async
);
796 release_object( req
->iosb
);
797 list_remove( &req
->entry
);
802 static int is_oobinline( struct sock
*sock
)
805 socklen_t len
= sizeof(oobinline
);
806 return !getsockopt( get_unix_fd( sock
->fd
), SOL_SOCKET
, SO_OOBINLINE
, (char *)&oobinline
, &len
) && oobinline
;
809 static int get_poll_flags( struct sock
*sock
, int event
)
813 /* A connection-mode socket which has never been connected does not return
814 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
815 if (sock
->state
== SOCK_UNCONNECTED
)
816 event
&= ~(POLLOUT
| POLLHUP
);
820 if (sock
->state
== SOCK_LISTENING
)
821 flags
|= AFD_POLL_ACCEPT
;
823 flags
|= AFD_POLL_READ
;
826 flags
|= is_oobinline( sock
) ? AFD_POLL_READ
: AFD_POLL_OOB
;
828 flags
|= AFD_POLL_WRITE
;
829 if (sock
->state
== SOCK_CONNECTED
)
830 flags
|= AFD_POLL_CONNECT
;
832 flags
|= AFD_POLL_HUP
;
834 flags
|= AFD_POLL_CONNECT_ERR
;
839 static void complete_async_poll( struct poll_req
*req
, unsigned int status
)
841 struct poll_socket_output
*output
= req
->output
;
844 for (i
= 0; i
< req
->count
; ++i
)
846 struct sock
*sock
= req
->sockets
[i
].sock
;
848 if (sock
->main_poll
== req
)
849 sock
->main_poll
= NULL
;
852 /* pass 0 as result; client will set actual result size */
854 async_request_complete( req
->async
, status
, 0, req
->count
* sizeof(*output
), output
);
857 static void complete_async_polls( struct sock
*sock
, int event
, int error
)
859 int flags
= get_poll_flags( sock
, event
);
860 struct poll_req
*req
, *next
;
862 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &poll_list
, struct poll_req
, entry
)
866 if (req
->iosb
->status
!= STATUS_PENDING
) continue;
868 for (i
= 0; i
< req
->count
; ++i
)
870 if (req
->sockets
[i
].sock
!= sock
) continue;
871 if (!(req
->sockets
[i
].flags
& flags
)) continue;
874 fprintf( stderr
, "completing poll for socket %p, wanted %#x got %#x\n",
875 sock
, req
->sockets
[i
].flags
, flags
);
877 req
->output
[i
].flags
= req
->sockets
[i
].flags
& flags
;
878 req
->output
[i
].status
= sock_get_ntstatus( error
);
880 complete_async_poll( req
, STATUS_SUCCESS
);
886 static void async_poll_timeout( void *private )
888 struct poll_req
*req
= private;
892 if (req
->iosb
->status
!= STATUS_PENDING
) return;
894 complete_async_poll( req
, STATUS_TIMEOUT
);
897 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
899 if (event
& (POLLIN
| POLLPRI
))
901 struct accept_req
*req
;
903 LIST_FOR_EACH_ENTRY( req
, &sock
->accept_list
, struct accept_req
, entry
)
905 if (req
->iosb
->status
== STATUS_PENDING
&& !req
->accepted
)
907 complete_async_accept( sock
, req
);
912 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
913 complete_async_accept_recv( sock
->accept_recv_req
);
916 if ((event
& POLLOUT
) && sock
->connect_req
&& sock
->connect_req
->iosb
->status
== STATUS_PENDING
)
917 complete_async_connect( sock
);
919 if (event
& (POLLIN
| POLLPRI
) && async_waiting( &sock
->read_q
))
921 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
922 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
923 event
&= ~(POLLIN
| POLLPRI
);
926 if (event
& POLLOUT
&& async_waiting( &sock
->write_q
))
928 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
929 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
933 if (event
& (POLLERR
| POLLHUP
))
935 int status
= sock_get_ntstatus( error
);
936 struct accept_req
*req
, *next
;
938 if (sock
->rd_shutdown
|| sock
->hangup
)
939 async_wake_up( &sock
->read_q
, status
);
940 if (sock
->wr_shutdown
)
941 async_wake_up( &sock
->write_q
, status
);
943 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
945 if (req
->iosb
->status
== STATUS_PENDING
)
946 async_terminate( req
->async
, status
);
949 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
950 async_terminate( sock
->accept_recv_req
->async
, status
);
952 if (sock
->connect_req
)
953 async_terminate( sock
->connect_req
->async
, status
);
959 static void post_socket_event( struct sock
*sock
, enum afd_poll_bit event_bit
, int error
)
961 unsigned int event
= (1 << event_bit
);
963 if (!(sock
->reported_events
& event
))
965 sock
->pending_events
|= event
;
966 sock
->reported_events
|= event
;
967 sock
->errors
[event_bit
] = error
;
971 static void sock_dispatch_events( struct sock
*sock
, enum connection_state prevstate
, int event
, int error
)
975 case SOCK_UNCONNECTED
:
978 case SOCK_CONNECTING
:
981 post_socket_event( sock
, AFD_POLL_BIT_CONNECT
, 0 );
982 sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] = 0;
984 if (event
& (POLLERR
| POLLHUP
))
985 post_socket_event( sock
, AFD_POLL_BIT_CONNECT_ERR
, error
);
989 if (event
& (POLLIN
| POLLERR
| POLLHUP
))
990 post_socket_event( sock
, AFD_POLL_BIT_ACCEPT
, error
);
994 case SOCK_CONNECTIONLESS
:
996 post_socket_event( sock
, AFD_POLL_BIT_READ
, 0 );
999 post_socket_event( sock
, AFD_POLL_BIT_WRITE
, 0 );
1001 if (event
& POLLPRI
)
1002 post_socket_event( sock
, AFD_POLL_BIT_OOB
, 0 );
1004 if (event
& (POLLERR
| POLLHUP
))
1005 post_socket_event( sock
, AFD_POLL_BIT_HUP
, error
);
1009 sock_wake_up( sock
);
1012 static void sock_poll_event( struct fd
*fd
, int event
)
1014 struct sock
*sock
= get_fd_user( fd
);
1015 int hangup_seen
= 0;
1016 enum connection_state prevstate
= sock
->state
;
1019 assert( sock
->obj
.ops
== &sock_ops
);
1021 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
1023 /* we may change event later, remove from loop here */
1024 if (event
& (POLLERR
|POLLHUP
)) set_fd_events( sock
->fd
, -1 );
1026 switch (sock
->state
)
1028 case SOCK_UNCONNECTED
:
1031 case SOCK_CONNECTING
:
1032 if (event
& (POLLERR
|POLLHUP
))
1034 sock
->state
= SOCK_UNCONNECTED
;
1036 error
= sock_error( fd
);
1038 else if (event
& POLLOUT
)
1040 sock
->state
= SOCK_CONNECTED
;
1041 sock
->connect_time
= current_time
;
1045 case SOCK_LISTENING
:
1046 if (event
& (POLLERR
|POLLHUP
))
1047 error
= sock_error( fd
);
1050 case SOCK_CONNECTED
:
1051 case SOCK_CONNECTIONLESS
:
1052 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
1057 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
1058 * has been closed, so we need to check for it explicitly here */
1059 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
1068 /* EAGAIN can happen if an async recv() falls between the server's poll()
1069 call and the invocation of this routine */
1070 if ( errno
!= EAGAIN
)
1075 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
1080 if (hangup_seen
|| (sock_shutdown_type
== SOCK_SHUTDOWN_POLLHUP
&& (event
& POLLHUP
)))
1084 else if (event
& (POLLHUP
| POLLERR
))
1089 fprintf( stderr
, "socket %p aborted by error %d, event %#x\n", sock
, error
, event
);
1097 complete_async_polls( sock
, event
, error
);
1099 event
= sock_dispatch_asyncs( sock
, event
, error
);
1100 sock_dispatch_events( sock
, prevstate
, event
, error
);
1102 sock_reselect( sock
);
1105 static void sock_dump( struct object
*obj
, int verbose
)
1107 struct sock
*sock
= (struct sock
*)obj
;
1108 assert( obj
->ops
== &sock_ops
);
1109 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
1110 sock
->fd
, sock
->state
,
1111 sock
->mask
, sock
->pending_events
, sock
->reported_events
);
1114 static int poll_flags_from_afd( struct sock
*sock
, int flags
)
1118 /* A connection-mode socket which has never been connected does
1119 * not return write or hangup events, but Linux returns
1120 * POLLOUT | POLLHUP. */
1121 if (sock
->state
== SOCK_UNCONNECTED
)
1124 if (flags
& (AFD_POLL_READ
| AFD_POLL_ACCEPT
))
1126 if ((flags
& AFD_POLL_HUP
) && sock
->type
== WS_SOCK_STREAM
)
1128 if (flags
& AFD_POLL_OOB
)
1129 ev
|= is_oobinline( sock
) ? POLLIN
: POLLPRI
;
1130 if (flags
& AFD_POLL_WRITE
)
1136 static int sock_get_poll_events( struct fd
*fd
)
1138 struct sock
*sock
= get_fd_user( fd
);
1139 unsigned int mask
= sock
->mask
& ~sock
->reported_events
;
1140 struct poll_req
*req
;
1143 assert( sock
->obj
.ops
== &sock_ops
);
1145 if (!sock
->type
) /* not initialized yet */
1148 switch (sock
->state
)
1150 case SOCK_UNCONNECTED
:
1151 /* A connection-mode Windows socket which has never been connected does
1152 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1153 * need to return -1 here, to prevent the socket from being polled on at
1157 case SOCK_CONNECTING
:
1160 case SOCK_LISTENING
:
1161 if (!list_empty( &sock
->accept_list
) || (mask
& AFD_POLL_ACCEPT
))
1165 case SOCK_CONNECTED
:
1166 case SOCK_CONNECTIONLESS
:
1167 if (sock
->hangup
&& sock
->wr_shutdown
&& !sock
->wr_shutdown_pending
)
1169 /* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
1170 * if both the socket and its peer are SHUT_WR.
1172 * We don't use SHUT_RD, so we can only encounter this in the latter
1173 * case. In that case there can't be any pending read requests (they
1174 * would have already been completed with a length of zero), the
1175 * above condition ensures that we don't have any pending write
1176 * requests, and nothing that can change about the socket state that
1177 * would complete a pending poll request. */
1184 if (sock
->accept_recv_req
)
1188 else if (async_queued( &sock
->read_q
))
1190 if (async_waiting( &sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
1194 /* Don't ask for POLLIN if we got a hangup. We won't receive more
1195 * data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
1198 if (mask
& AFD_POLL_READ
)
1200 if (mask
& AFD_POLL_OOB
)
1204 /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
1205 if (sock
->state
== SOCK_CONNECTED
&& (mask
& AFD_POLL_HUP
) && !(sock
->reported_events
& AFD_POLL_READ
))
1209 if (async_queued( &sock
->write_q
))
1211 if (async_waiting( &sock
->write_q
)) ev
|= POLLOUT
;
1213 else if (!sock
->wr_shutdown
&& (mask
& AFD_POLL_WRITE
))
1221 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1225 for (i
= 0; i
< req
->count
; ++i
)
1227 if (req
->sockets
[i
].sock
!= sock
) continue;
1229 ev
|= poll_flags_from_afd( sock
, req
->sockets
[i
].flags
);
1236 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
1238 return FD_TYPE_SOCKET
;
1241 static void sock_cancel_async( struct fd
*fd
, struct async
*async
)
1243 struct poll_req
*req
;
1245 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1249 if (req
->async
!= async
)
1252 for (i
= 0; i
< req
->count
; i
++)
1254 struct sock
*sock
= req
->sockets
[i
].sock
;
1256 if (sock
->main_poll
== req
)
1257 sock
->main_poll
= NULL
;
1261 async_terminate( async
, STATUS_CANCELLED
);
1264 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
1266 struct sock
*sock
= get_fd_user( fd
);
1267 struct async_queue
*queue
;
1269 assert( sock
->obj
.ops
== &sock_ops
);
1273 case ASYNC_TYPE_READ
:
1274 if (sock
->rd_shutdown
)
1276 set_error( STATUS_PIPE_DISCONNECTED
);
1279 queue
= &sock
->read_q
;
1282 case ASYNC_TYPE_WRITE
:
1283 if (sock
->wr_shutdown
)
1285 set_error( STATUS_PIPE_DISCONNECTED
);
1288 queue
= &sock
->write_q
;
1292 set_error( STATUS_INVALID_PARAMETER
);
1296 if (sock
->state
!= SOCK_CONNECTED
)
1298 set_error( STATUS_PIPE_DISCONNECTED
);
1302 queue_async( queue
, async
);
1303 sock_reselect( sock
);
1305 set_error( STATUS_PENDING
);
1308 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
1310 struct sock
*sock
= get_fd_user( fd
);
1312 if (sock
->wr_shutdown_pending
&& list_empty( &sock
->write_q
.queue
))
1314 shutdown( get_unix_fd( sock
->fd
), SHUT_WR
);
1315 sock
->wr_shutdown_pending
= 0;
1318 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1319 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1321 if (queue
!= &sock
->ifchange_q
&& sock
->type
)
1322 sock_reselect( sock
);
1325 static struct fd
*sock_get_fd( struct object
*obj
)
1327 struct sock
*sock
= (struct sock
*)obj
;
1328 return (struct fd
*)grab_object( sock
->fd
);
1331 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
1333 struct sock
*sock
= (struct sock
*)obj
;
1335 if (sock
->obj
.handle_count
== 1) /* last handle */
1337 struct accept_req
*accept_req
, *accept_next
;
1338 struct poll_req
*poll_req
, *poll_next
;
1340 if (sock
->accept_recv_req
)
1341 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
1343 LIST_FOR_EACH_ENTRY_SAFE( accept_req
, accept_next
, &sock
->accept_list
, struct accept_req
, entry
)
1344 async_terminate( accept_req
->async
, STATUS_CANCELLED
);
1346 if (sock
->connect_req
)
1347 async_terminate( sock
->connect_req
->async
, STATUS_CANCELLED
);
1349 LIST_FOR_EACH_ENTRY_SAFE( poll_req
, poll_next
, &poll_list
, struct poll_req
, entry
)
1351 struct iosb
*iosb
= poll_req
->iosb
;
1352 BOOL signaled
= FALSE
;
1355 if (iosb
->status
!= STATUS_PENDING
) continue;
1357 for (i
= 0; i
< poll_req
->count
; ++i
)
1359 if (poll_req
->sockets
[i
].sock
== sock
)
1362 poll_req
->output
[i
].flags
= AFD_POLL_CLOSE
;
1363 poll_req
->output
[i
].status
= 0;
1367 if (signaled
) complete_async_poll( poll_req
, STATUS_SUCCESS
);
1374 static void sock_destroy( struct object
*obj
)
1376 struct sock
*sock
= (struct sock
*)obj
;
1378 assert( obj
->ops
== &sock_ops
);
1380 /* FIXME: special socket shutdown stuff? */
1382 if ( sock
->deferred
)
1383 release_object( sock
->deferred
);
1385 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
1386 sock_release_ifchange( sock
);
1387 free_async_queue( &sock
->read_q
);
1388 free_async_queue( &sock
->write_q
);
1389 free_async_queue( &sock
->ifchange_q
);
1390 free_async_queue( &sock
->accept_q
);
1391 free_async_queue( &sock
->connect_q
);
1392 free_async_queue( &sock
->poll_q
);
1393 if (sock
->event
) release_object( sock
->event
);
1396 /* shut the socket down to force pending poll() calls in the client to return */
1397 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
1398 release_object( sock
->fd
);
1402 static struct sock
*create_socket(void)
1406 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
1408 sock
->state
= SOCK_UNCONNECTED
;
1410 sock
->pending_events
= 0;
1411 sock
->reported_events
= 0;
1420 sock
->connect_time
= 0;
1421 sock
->deferred
= NULL
;
1422 sock
->ifchange_obj
= NULL
;
1423 sock
->accept_recv_req
= NULL
;
1424 sock
->connect_req
= NULL
;
1425 sock
->main_poll
= NULL
;
1426 memset( &sock
->addr
, 0, sizeof(sock
->addr
) );
1428 sock
->rd_shutdown
= 0;
1429 sock
->wr_shutdown
= 0;
1430 sock
->wr_shutdown_pending
= 0;
1433 sock
->nonblocking
= 0;
1439 init_async_queue( &sock
->read_q
);
1440 init_async_queue( &sock
->write_q
);
1441 init_async_queue( &sock
->ifchange_q
);
1442 init_async_queue( &sock
->accept_q
);
1443 init_async_queue( &sock
->connect_q
);
1444 init_async_queue( &sock
->poll_q
);
1445 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
1446 list_init( &sock
->accept_list
);
1450 static int get_unix_family( int family
)
1454 case WS_AF_INET
: return AF_INET
;
1455 case WS_AF_INET6
: return AF_INET6
;
1457 case WS_AF_IPX
: return AF_IPX
;
1460 case WS_AF_IRDA
: return AF_IRDA
;
1462 case WS_AF_UNSPEC
: return AF_UNSPEC
;
1467 static int get_unix_type( int type
)
1471 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
1472 case WS_SOCK_RAW
: return SOCK_RAW
;
1473 case WS_SOCK_STREAM
: return SOCK_STREAM
;
1478 static int get_unix_protocol( int protocol
)
1480 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1485 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
1486 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1487 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1488 case WS_IPPROTO_IPV4
: return IPPROTO_IPIP
;
1489 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1490 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1491 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1492 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1497 static void set_dont_fragment( int fd
, int level
, int value
)
1501 if (level
== IPPROTO_IP
)
1504 optname
= IP_DONTFRAG
;
1505 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1506 optname
= IP_MTU_DISCOVER
;
1507 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1514 #ifdef IPV6_DONTFRAG
1515 optname
= IPV6_DONTFRAG
;
1516 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1517 optname
= IPV6_MTU_DISCOVER
;
1518 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1524 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1527 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
, unsigned int flags
)
1529 unsigned int options
= 0;
1530 int sockfd
, unix_type
, unix_family
, unix_protocol
, value
;
1533 unix_family
= get_unix_family( family
);
1534 unix_type
= get_unix_type( type
);
1535 unix_protocol
= get_unix_protocol( protocol
);
1537 if (unix_protocol
< 0)
1539 if (type
&& unix_type
< 0)
1540 set_win32_error( WSAESOCKTNOSUPPORT
);
1542 set_win32_error( WSAEPROTONOSUPPORT
);
1545 if (unix_family
< 0)
1547 if (family
>= 0 && unix_type
< 0)
1548 set_win32_error( WSAESOCKTNOSUPPORT
);
1550 set_win32_error( WSAEAFNOSUPPORT
);
1554 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1557 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1558 else set_win32_error( sock_get_error( errno
));
1561 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1563 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1566 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1569 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1572 /* Should we retrieve val using a getsockopt call and then
1573 * set the modified one? */
1574 val
.ipx_pt
= ipx_type
;
1575 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1580 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1582 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1583 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1584 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1585 else if (unix_type
== SOCK_STREAM
)
1586 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1590 if (unix_family
== AF_INET6
)
1592 static const int enable
= 1;
1593 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1597 len
= sizeof(value
);
1598 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_RCVBUF
, &value
, &len
))
1599 sock
->rcvbuf
= value
;
1601 len
= sizeof(value
);
1602 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_SNDBUF
, &value
, &len
))
1603 sock
->sndbuf
= value
;
1605 sock
->state
= (type
== WS_SOCK_STREAM
? SOCK_UNCONNECTED
: SOCK_CONNECTIONLESS
);
1606 sock
->flags
= flags
;
1607 sock
->proto
= protocol
;
1609 sock
->family
= family
;
1613 options
= get_fd_options( sock
->fd
);
1614 release_object( sock
->fd
);
1617 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1622 /* We can't immediately allow caching for a connection-mode socket, since it
1623 * might be accepted into (changing the underlying fd object.) */
1624 if (sock
->type
!= WS_SOCK_STREAM
) allow_fd_caching( sock
->fd
);
1629 /* accepts a socket and inits it */
1630 static int accept_new_fd( struct sock
*sock
)
1633 /* Try to accept(2). We can't be safe that this an already connected socket
1634 * or that accept() is allowed on it. In those cases we will get -1/errno
1637 struct sockaddr saddr
;
1638 socklen_t slen
= sizeof(saddr
);
1639 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1641 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1643 set_error( sock_get_ntstatus( errno
));
1647 /* accept a socket (creates a new fd) */
1648 static struct sock
*accept_socket( struct sock
*sock
)
1650 struct sock
*acceptsock
;
1653 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1655 if ( sock
->deferred
)
1657 acceptsock
= sock
->deferred
;
1658 sock
->deferred
= NULL
;
1662 union unix_sockaddr unix_addr
;
1665 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1666 if (!(acceptsock
= create_socket()))
1672 /* newly created socket gets the same properties of the listening socket */
1673 acceptsock
->state
= SOCK_CONNECTED
;
1674 acceptsock
->bound
= 1;
1675 acceptsock
->nonblocking
= sock
->nonblocking
;
1676 acceptsock
->mask
= sock
->mask
;
1677 acceptsock
->proto
= sock
->proto
;
1678 acceptsock
->type
= sock
->type
;
1679 acceptsock
->family
= sock
->family
;
1680 acceptsock
->window
= sock
->window
;
1681 acceptsock
->message
= sock
->message
;
1682 acceptsock
->connect_time
= current_time
;
1683 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
1684 acceptsock
->flags
= sock
->flags
;
1685 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1686 get_fd_options( sock
->fd
) )))
1688 release_object( acceptsock
);
1691 unix_len
= sizeof(unix_addr
);
1692 if (!getsockname( acceptfd
, &unix_addr
.addr
, &unix_len
))
1693 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1696 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1697 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1698 sock_reselect( sock
);
1702 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
1704 union unix_sockaddr unix_addr
;
1709 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
1711 if ( sock
->deferred
)
1713 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
1714 get_fd_options( acceptsock
->fd
) );
1718 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
1720 release_object( sock
->deferred
);
1721 sock
->deferred
= NULL
;
1725 if ((acceptfd
= accept_new_fd( sock
)) == -1)
1728 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1729 get_fd_options( acceptsock
->fd
) )))
1733 acceptsock
->state
= SOCK_CONNECTED
;
1734 acceptsock
->pending_events
= 0;
1735 acceptsock
->reported_events
= 0;
1736 acceptsock
->proto
= sock
->proto
;
1737 acceptsock
->type
= sock
->type
;
1738 acceptsock
->family
= sock
->family
;
1739 acceptsock
->wparam
= 0;
1740 acceptsock
->deferred
= NULL
;
1741 acceptsock
->connect_time
= current_time
;
1742 fd_copy_completion( acceptsock
->fd
, newfd
);
1743 release_object( acceptsock
->fd
);
1744 acceptsock
->fd
= newfd
;
1746 unix_len
= sizeof(unix_addr
);
1747 if (!getsockname( get_unix_fd( newfd
), &unix_addr
.addr
, &unix_len
))
1748 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1751 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1752 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1753 sock_reselect( sock
);
1760 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
1762 static const int enable
= 1;
1765 if (!(index
= if_nametoindex( name
)))
1768 if (setsockopt( fd
, IPPROTO_IP
, IP_BOUND_IF
, &index
, sizeof(index
) ))
1771 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
1774 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER) && defined(SO_BINDTODEVICE)
1776 struct interface_filter
1778 struct sock_filter iface_memaddr
;
1779 struct sock_filter iface_rule
;
1780 struct sock_filter ip_memaddr
;
1781 struct sock_filter ip_rule
;
1782 struct sock_filter return_keep
;
1783 struct sock_filter return_dump
;
1785 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
1786 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1787 /sizeof(struct sock_filter)
1788 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
1789 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1790 /sizeof(struct sock_filter)
1791 # define FILTER_JUMP_NEXT() (u_char)(0)
1792 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
1793 static struct interface_filter generic_interface_filter
=
1795 /* This filter rule allows incoming packets on the specified interface, which works for all
1796 * remotely generated packets and for locally generated broadcast packets. */
1797 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
1798 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
1799 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
1800 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
1801 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
1802 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
1803 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
1804 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
1807 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
1809 struct interface_filter specific_interface_filter
;
1810 struct sock_fprog filter_prog
;
1811 static const int enable
= 1;
1815 if (!setsockopt( fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen( name
) + 1 ))
1818 /* SO_BINDTODEVICE requires NET_CAP_RAW until Linux 5.7. */
1820 fprintf( stderr
, "setsockopt SO_BINDTODEVICE fd %d, name %s failed: %s, falling back to SO_REUSE_ADDR\n",
1821 fd
, name
, strerror( errno
));
1823 if (!(index
= if_nametoindex( name
)))
1826 ifindex
= htonl( index
);
1827 if (setsockopt( fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
) ) < 0)
1830 specific_interface_filter
= generic_interface_filter
;
1831 specific_interface_filter
.iface_rule
.k
= index
;
1832 specific_interface_filter
.ip_rule
.k
= htonl( bind_addr
);
1833 filter_prog
.len
= sizeof(generic_interface_filter
) / sizeof(struct sock_filter
);
1834 filter_prog
.filter
= (struct sock_filter
*)&specific_interface_filter
;
1835 if (setsockopt( fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
) ))
1838 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
1843 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
1849 #endif /* LINUX_BOUND_IF */
1851 /* Take bind() calls on any name corresponding to a local network adapter and
1852 * restrict the given socket to operating only on the specified interface. This
1853 * restriction consists of two components:
1854 * 1) An outgoing packet restriction suggesting the egress interface for all
1856 * 2) An incoming packet restriction dropping packets not meant for the
1858 * If the function succeeds in placing these restrictions, then the name for the
1859 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
1860 * receipt of broadcast packets on the socket. This behavior is only relevant to
1861 * UDP sockets and is needed for applications that expect to be able to receive
1862 * broadcast packets on a socket that is bound to a specific network interface.
1864 static int bind_to_interface( struct sock
*sock
, const struct sockaddr_in
*addr
)
1866 in_addr_t bind_addr
= addr
->sin_addr
.s_addr
;
1867 struct ifaddrs
*ifaddrs
, *ifaddr
;
1868 int fd
= get_unix_fd( sock
->fd
);
1871 if (bind_addr
== htonl( INADDR_ANY
) || bind_addr
== htonl( INADDR_LOOPBACK
))
1873 if (sock
->type
!= WS_SOCK_DGRAM
)
1876 if (getifaddrs( &ifaddrs
) < 0) return 0;
1878 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
1880 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET
1881 && ((struct sockaddr_in
*)ifaddr
->ifa_addr
)->sin_addr
.s_addr
== bind_addr
)
1883 if ((err
= bind_to_iface_name( fd
, bind_addr
, ifaddr
->ifa_name
)) < 0)
1886 fprintf( stderr
, "failed to bind to interface: %s\n", strerror( errno
) );
1891 freeifaddrs( ifaddrs
);
1895 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1896 static unsigned int get_ipv6_interface_index( const struct in6_addr
*addr
)
1898 struct ifaddrs
*ifaddrs
, *ifaddr
;
1900 if (getifaddrs( &ifaddrs
) < 0) return 0;
1902 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
1904 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET6
1905 && !memcmp( &((struct sockaddr_in6
*)ifaddr
->ifa_addr
)->sin6_addr
, addr
, sizeof(*addr
) ))
1907 unsigned int index
= if_nametoindex( ifaddr
->ifa_name
);
1912 fprintf( stderr
, "Unable to look up interface index for %s: %s\n",
1913 ifaddr
->ifa_name
, strerror( errno
) );
1917 freeifaddrs( ifaddrs
);
1922 freeifaddrs( ifaddrs
);
1927 /* return an errno value mapped to a WSA error */
1928 static unsigned int sock_get_error( int err
)
1932 case EINTR
: return WSAEINTR
;
1933 case EBADF
: return WSAEBADF
;
1935 case EACCES
: return WSAEACCES
;
1936 case EFAULT
: return WSAEFAULT
;
1937 case EINVAL
: return WSAEINVAL
;
1938 case EMFILE
: return WSAEMFILE
;
1940 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
1941 case EALREADY
: return WSAEALREADY
;
1942 case ENOTSOCK
: return WSAENOTSOCK
;
1943 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
1944 case EMSGSIZE
: return WSAEMSGSIZE
;
1945 case EPROTOTYPE
: return WSAEPROTOTYPE
;
1946 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
1947 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
1948 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
1949 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
1950 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
1951 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
1952 case EADDRINUSE
: return WSAEADDRINUSE
;
1953 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
1954 case ENETDOWN
: return WSAENETDOWN
;
1955 case ENETUNREACH
: return WSAENETUNREACH
;
1956 case ENETRESET
: return WSAENETRESET
;
1957 case ECONNABORTED
: return WSAECONNABORTED
;
1959 case ECONNRESET
: return WSAECONNRESET
;
1960 case ENOBUFS
: return WSAENOBUFS
;
1961 case EISCONN
: return WSAEISCONN
;
1962 case ENOTCONN
: return WSAENOTCONN
;
1963 case ESHUTDOWN
: return WSAESHUTDOWN
;
1964 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
1965 case ETIMEDOUT
: return WSAETIMEDOUT
;
1966 case ECONNREFUSED
: return WSAECONNREFUSED
;
1967 case ELOOP
: return WSAELOOP
;
1968 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
1969 case EHOSTDOWN
: return WSAEHOSTDOWN
;
1970 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
1971 case ENOTEMPTY
: return WSAENOTEMPTY
;
1973 case EPROCLIM
: return WSAEPROCLIM
;
1976 case EUSERS
: return WSAEUSERS
;
1979 case EDQUOT
: return WSAEDQUOT
;
1982 case ESTALE
: return WSAESTALE
;
1985 case EREMOTE
: return WSAEREMOTE
;
1991 perror("wineserver: sock_get_error() can't map error");
1996 static int sock_get_ntstatus( int err
)
2000 case EBADF
: return STATUS_INVALID_HANDLE
;
2001 case EBUSY
: return STATUS_DEVICE_BUSY
;
2003 case EACCES
: return STATUS_ACCESS_DENIED
;
2004 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
2005 case EINVAL
: return STATUS_INVALID_PARAMETER
;
2007 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
2009 case EWOULDBLOCK
: return STATUS_DEVICE_NOT_READY
;
2010 case EALREADY
: return STATUS_NETWORK_BUSY
;
2011 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
2012 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
2013 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
2014 case EPROTONOSUPPORT
:
2015 case ESOCKTNOSUPPORT
:
2018 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
2019 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
2020 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
2021 case EADDRINUSE
: return STATUS_SHARING_VIOLATION
;
2022 /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
2023 * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
2025 case EADDRNOTAVAIL
: return STATUS_INVALID_ADDRESS_COMPONENT
;
2026 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
2027 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
2028 case ENOTCONN
: return STATUS_INVALID_CONNECTION
;
2029 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
2030 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
2031 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
2032 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
2034 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
2035 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
2036 case EISCONN
: return STATUS_CONNECTION_ACTIVE
;
2038 case 0: return STATUS_SUCCESS
;
2041 perror("wineserver: sock_get_ntstatus() can't map error");
2042 return STATUS_UNSUCCESSFUL
;
2046 static struct accept_req
*alloc_accept_req( struct sock
*sock
, struct sock
*acceptsock
, struct async
*async
,
2047 const struct afd_accept_into_params
*params
)
2049 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
2053 req
->async
= (struct async
*)grab_object( async
);
2054 req
->iosb
= async_get_iosb( async
);
2055 req
->sock
= (struct sock
*)grab_object( sock
);
2056 req
->acceptsock
= acceptsock
;
2057 if (acceptsock
) grab_object( acceptsock
);
2063 req
->recv_len
= params
->recv_len
;
2064 req
->local_len
= params
->local_len
;
2070 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2072 struct sock
*sock
= get_fd_user( fd
);
2075 assert( sock
->obj
.ops
== &sock_ops
);
2077 if (code
!= IOCTL_AFD_WINE_CREATE
&& (unix_fd
= get_unix_fd( fd
)) < 0) return;
2081 case IOCTL_AFD_WINE_CREATE
:
2083 const struct afd_create_params
*params
= get_req_data();
2085 if (get_req_data_size() != sizeof(*params
))
2087 set_error( STATUS_INVALID_PARAMETER
);
2090 init_socket( sock
, params
->family
, params
->type
, params
->protocol
, params
->flags
);
2094 case IOCTL_AFD_WINE_ACCEPT
:
2096 struct sock
*acceptsock
;
2097 obj_handle_t handle
;
2099 if (get_reply_max_size() != sizeof(handle
))
2101 set_error( STATUS_BUFFER_TOO_SMALL
);
2105 if (!(acceptsock
= accept_socket( sock
)))
2107 struct accept_req
*req
;
2109 if (sock
->nonblocking
) return;
2110 if (get_error() != STATUS_DEVICE_NOT_READY
) return;
2112 if (!(req
= alloc_accept_req( sock
, NULL
, async
, NULL
))) return;
2113 list_add_tail( &sock
->accept_list
, &req
->entry
);
2115 async_set_completion_callback( async
, free_accept_req
, req
);
2116 queue_async( &sock
->accept_q
, async
);
2117 sock_reselect( sock
);
2118 set_error( STATUS_PENDING
);
2121 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
2122 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
2123 acceptsock
->wparam
= handle
;
2124 release_object( acceptsock
);
2125 set_reply_data( &handle
, sizeof(handle
) );
2129 case IOCTL_AFD_WINE_ACCEPT_INTO
:
2131 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
2132 const struct afd_accept_into_params
*params
= get_req_data();
2133 struct sock
*acceptsock
;
2134 unsigned int remote_len
;
2135 struct accept_req
*req
;
2137 if (get_req_data_size() != sizeof(*params
) ||
2138 get_reply_max_size() < params
->recv_len
||
2139 get_reply_max_size() - params
->recv_len
< params
->local_len
)
2141 set_error( STATUS_BUFFER_TOO_SMALL
);
2145 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
2146 if (remote_len
< sizeof(int))
2148 set_error( STATUS_INVALID_PARAMETER
);
2152 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
2155 if (acceptsock
->accept_recv_req
)
2157 release_object( acceptsock
);
2158 set_error( STATUS_INVALID_PARAMETER
);
2162 if (!(req
= alloc_accept_req( sock
, acceptsock
, async
, params
)))
2164 release_object( acceptsock
);
2167 list_add_tail( &sock
->accept_list
, &req
->entry
);
2168 acceptsock
->accept_recv_req
= req
;
2169 release_object( acceptsock
);
2171 acceptsock
->wparam
= params
->accept_handle
;
2172 async_set_completion_callback( async
, free_accept_req
, req
);
2173 queue_async( &sock
->accept_q
, async
);
2174 sock_reselect( sock
);
2175 set_error( STATUS_PENDING
);
2179 case IOCTL_AFD_LISTEN
:
2181 const struct afd_listen_params
*params
= get_req_data();
2183 if (get_req_data_size() < sizeof(*params
))
2185 set_error( STATUS_INVALID_PARAMETER
);
2191 set_error( STATUS_INVALID_PARAMETER
);
2195 if (listen( unix_fd
, params
->backlog
) < 0)
2197 set_error( sock_get_ntstatus( errno
) );
2201 sock
->state
= SOCK_LISTENING
;
2203 /* a listening socket can no longer be accepted into */
2204 allow_fd_caching( sock
->fd
);
2206 /* we may already be selecting for AFD_POLL_ACCEPT */
2207 sock_reselect( sock
);
2211 case IOCTL_AFD_WINE_CONNECT
:
2213 const struct afd_connect_params
*params
= get_req_data();
2214 const struct WS_sockaddr
*addr
;
2215 union unix_sockaddr unix_addr
;
2216 struct connect_req
*req
;
2220 if (get_req_data_size() < sizeof(*params
) ||
2221 get_req_data_size() - sizeof(*params
) < params
->addr_len
)
2223 set_error( STATUS_BUFFER_TOO_SMALL
);
2226 send_len
= get_req_data_size() - sizeof(*params
) - params
->addr_len
;
2227 addr
= (const struct WS_sockaddr
*)(params
+ 1);
2229 if (!params
->synchronous
&& !sock
->bound
)
2231 set_error( STATUS_INVALID_PARAMETER
);
2235 if (sock
->accept_recv_req
)
2237 set_error( STATUS_INVALID_PARAMETER
);
2241 if (sock
->connect_req
)
2243 set_error( STATUS_INVALID_PARAMETER
);
2247 switch (sock
->state
)
2249 case SOCK_LISTENING
:
2250 set_error( STATUS_INVALID_PARAMETER
);
2253 case SOCK_CONNECTING
:
2254 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2255 * but there's no status code that maps to WSAEALREADY... */
2256 set_error( params
->synchronous
? STATUS_ADDRESS_ALREADY_ASSOCIATED
: STATUS_INVALID_PARAMETER
);
2259 case SOCK_CONNECTED
:
2260 set_error( STATUS_CONNECTION_ACTIVE
);
2263 case SOCK_UNCONNECTED
:
2264 case SOCK_CONNECTIONLESS
:
2268 unix_len
= sockaddr_to_unix( addr
, params
->addr_len
, &unix_addr
);
2271 set_error( STATUS_INVALID_ADDRESS
);
2274 if (unix_addr
.addr
.sa_family
== AF_INET
&& !memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 ))
2275 unix_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_LOOPBACK
);
2277 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2278 if (ret
< 0 && errno
!= EINPROGRESS
)
2280 set_error( sock_get_ntstatus( errno
) );
2284 /* a connected or connecting socket can no longer be accepted into */
2285 allow_fd_caching( sock
->fd
);
2287 unix_len
= sizeof(unix_addr
);
2288 if (!sock
->bound
&& !getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
))
2289 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2294 sock
->state
= SOCK_CONNECTED
;
2296 if (!send_len
) return;
2299 sock
->state
= SOCK_CONNECTING
;
2301 if (params
->synchronous
&& sock
->nonblocking
)
2303 sock_reselect( sock
);
2304 set_error( STATUS_DEVICE_NOT_READY
);
2308 if (!(req
= mem_alloc( sizeof(*req
) )))
2311 req
->async
= (struct async
*)grab_object( async
);
2312 req
->iosb
= async_get_iosb( async
);
2313 req
->sock
= (struct sock
*)grab_object( sock
);
2314 req
->addr_len
= params
->addr_len
;
2315 req
->send_len
= send_len
;
2316 req
->send_cursor
= 0;
2318 async_set_completion_callback( async
, free_connect_req
, req
);
2319 sock
->connect_req
= req
;
2320 queue_async( &sock
->connect_q
, async
);
2321 sock_reselect( sock
);
2322 set_error( STATUS_PENDING
);
2326 case IOCTL_AFD_WINE_SHUTDOWN
:
2330 if (get_req_data_size() < sizeof(int))
2332 set_error( STATUS_BUFFER_TOO_SMALL
);
2335 how
= *(int *)get_req_data();
2339 set_error( STATUS_INVALID_PARAMETER
);
2343 if (sock
->state
!= SOCK_CONNECTED
&& sock
->state
!= SOCK_CONNECTIONLESS
)
2345 set_error( STATUS_INVALID_CONNECTION
);
2351 sock
->rd_shutdown
= 1;
2353 if (how
!= SD_RECEIVE
)
2355 sock
->wr_shutdown
= 1;
2356 if (list_empty( &sock
->write_q
.queue
))
2357 shutdown( unix_fd
, SHUT_WR
);
2359 sock
->wr_shutdown_pending
= 1;
2364 if (sock
->event
) release_object( sock
->event
);
2368 sock
->nonblocking
= 1;
2371 sock_reselect( sock
);
2375 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE
:
2379 if (get_req_data_size() < sizeof(int))
2381 set_error( STATUS_BUFFER_TOO_SMALL
);
2384 force_async
= *(int *)get_req_data();
2386 if (sock
->nonblocking
&& !force_async
)
2388 set_error( STATUS_DEVICE_NOT_READY
);
2391 if (!sock_get_ifchange( sock
)) return;
2392 queue_async( &sock
->ifchange_q
, async
);
2393 set_error( STATUS_PENDING
);
2397 case IOCTL_AFD_WINE_FIONBIO
:
2398 if (get_req_data_size() < sizeof(int))
2400 set_error( STATUS_BUFFER_TOO_SMALL
);
2403 if (*(int *)get_req_data())
2405 sock
->nonblocking
= 1;
2411 set_error( STATUS_INVALID_PARAMETER
);
2414 sock
->nonblocking
= 0;
2418 case IOCTL_AFD_GET_EVENTS
:
2420 struct afd_get_events_params params
= {0};
2423 if (get_reply_max_size() < sizeof(params
))
2425 set_error( STATUS_INVALID_PARAMETER
);
2429 params
.flags
= sock
->pending_events
& sock
->mask
;
2430 for (i
= 0; i
< ARRAY_SIZE( params
.status
); ++i
)
2431 params
.status
[i
] = sock_get_ntstatus( sock
->errors
[i
] );
2433 sock
->pending_events
= 0;
2434 sock_reselect( sock
);
2436 set_reply_data( ¶ms
, sizeof(params
) );
2440 case IOCTL_AFD_EVENT_SELECT
:
2442 struct event
*event
= NULL
;
2443 obj_handle_t event_handle
;
2446 set_async_pending( async
);
2448 if (is_machine_64bit( current
->process
->machine
))
2450 const struct afd_event_select_params_64
*params
= get_req_data();
2452 if (get_req_data_size() < sizeof(*params
))
2454 set_error( STATUS_INVALID_PARAMETER
);
2458 event_handle
= params
->event
;
2459 mask
= params
->mask
;
2463 const struct afd_event_select_params_32
*params
= get_req_data();
2465 if (get_req_data_size() < sizeof(*params
))
2467 set_error( STATUS_INVALID_PARAMETER
);
2471 event_handle
= params
->event
;
2472 mask
= params
->mask
;
2475 if ((event_handle
|| mask
) &&
2476 !(event
= get_event_obj( current
->process
, event_handle
, EVENT_MODIFY_STATE
)))
2478 set_error( STATUS_INVALID_PARAMETER
);
2482 if (sock
->event
) release_object( sock
->event
);
2483 sock
->event
= event
;
2488 sock
->nonblocking
= 1;
2490 sock_reselect( sock
);
2495 case IOCTL_AFD_WINE_MESSAGE_SELECT
:
2497 const struct afd_message_select_params
*params
= get_req_data();
2499 if (get_req_data_size() < sizeof(params
))
2501 set_error( STATUS_BUFFER_TOO_SMALL
);
2505 if (sock
->event
) release_object( sock
->event
);
2509 sock
->pending_events
= 0;
2510 sock
->reported_events
= 0;
2513 sock
->mask
= params
->mask
;
2514 sock
->window
= params
->window
;
2515 sock
->message
= params
->message
;
2516 sock
->wparam
= params
->handle
;
2517 sock
->nonblocking
= 1;
2519 sock_reselect( sock
);
2524 case IOCTL_AFD_BIND
:
2526 const struct afd_bind_params
*params
= get_req_data();
2527 union unix_sockaddr unix_addr
, bind_addr
;
2528 data_size_t in_size
;
2531 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2533 if (get_req_data_size() < get_reply_max_size())
2535 set_error( STATUS_BUFFER_TOO_SMALL
);
2538 in_size
= get_req_data_size() - get_reply_max_size();
2539 if (in_size
< offsetof(struct afd_bind_params
, addr
.sa_data
)
2540 || get_reply_max_size() < in_size
- sizeof(int))
2542 set_error( STATUS_INVALID_PARAMETER
);
2548 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED
);
2552 unix_len
= sockaddr_to_unix( ¶ms
->addr
, in_size
- sizeof(int), &unix_addr
);
2555 set_error( STATUS_INVALID_ADDRESS
);
2558 bind_addr
= unix_addr
;
2560 if (unix_addr
.addr
.sa_family
== AF_INET
)
2562 if (!memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 )
2563 || bind_to_interface( sock
, &unix_addr
.in
))
2564 bind_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
2566 else if (unix_addr
.addr
.sa_family
== AF_INET6
)
2568 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2569 /* Windows allows specifying zero to use the default scope. Linux
2570 * interprets it as an interface index and requires that it be
2572 if (!unix_addr
.in6
.sin6_scope_id
)
2573 bind_addr
.in6
.sin6_scope_id
= get_ipv6_interface_index( &unix_addr
.in6
.sin6_addr
);
2577 set_async_pending( async
);
2579 if (bind( unix_fd
, &bind_addr
.addr
, unix_len
) < 0)
2581 if (errno
== EADDRINUSE
)
2584 socklen_t len
= sizeof(reuse
);
2586 if (!getsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&reuse
, &len
) && reuse
)
2590 set_error( sock_get_ntstatus( errno
) );
2596 unix_len
= sizeof(bind_addr
);
2597 if (!getsockname( unix_fd
, &bind_addr
.addr
, &unix_len
))
2599 /* store the interface or magic loopback address instead of the
2600 * actual unix address */
2601 if (bind_addr
.addr
.sa_family
== AF_INET
)
2602 bind_addr
.in
.sin_addr
= unix_addr
.in
.sin_addr
;
2603 sock
->addr_len
= sockaddr_from_unix( &bind_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2606 if (get_reply_max_size() >= sock
->addr_len
)
2607 set_reply_data( &sock
->addr
, sock
->addr_len
);
2611 case IOCTL_AFD_GETSOCKNAME
:
2614 set_error( STATUS_INVALID_PARAMETER
);
2618 if (get_reply_max_size() < sock
->addr_len
)
2620 set_error( STATUS_BUFFER_TOO_SMALL
);
2624 set_reply_data( &sock
->addr
, sock
->addr_len
);
2627 case IOCTL_AFD_WINE_DEFER
:
2629 const obj_handle_t
*handle
= get_req_data();
2630 struct sock
*acceptsock
;
2632 if (get_req_data_size() < sizeof(*handle
))
2634 set_error( STATUS_BUFFER_TOO_SMALL
);
2638 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, *handle
, 0, &sock_ops
);
2639 if (!acceptsock
) return;
2641 sock
->deferred
= acceptsock
;
2645 case IOCTL_AFD_WINE_GET_INFO
:
2647 struct afd_get_info_params params
;
2649 if (get_reply_max_size() < sizeof(params
))
2651 set_error( STATUS_BUFFER_TOO_SMALL
);
2655 params
.family
= sock
->family
;
2656 params
.type
= sock
->type
;
2657 params
.protocol
= sock
->proto
;
2658 set_reply_data( ¶ms
, sizeof(params
) );
2662 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN
:
2664 int listening
= (sock
->state
== SOCK_LISTENING
);
2666 if (get_reply_max_size() < sizeof(listening
))
2668 set_error( STATUS_BUFFER_TOO_SMALL
);
2672 set_reply_data( &listening
, sizeof(listening
) );
2676 case IOCTL_AFD_WINE_GET_SO_ERROR
:
2679 socklen_t len
= sizeof(error
);
2682 if (get_reply_max_size() < sizeof(error
))
2684 set_error( STATUS_BUFFER_TOO_SMALL
);
2688 if (getsockopt( unix_fd
, SOL_SOCKET
, SO_ERROR
, (char *)&error
, &len
) < 0)
2690 set_error( sock_get_ntstatus( errno
) );
2696 for (i
= 0; i
< ARRAY_SIZE( sock
->errors
); ++i
)
2698 if (sock
->errors
[i
])
2700 error
= sock_get_error( sock
->errors
[i
] );
2706 set_reply_data( &error
, sizeof(error
) );
2710 case IOCTL_AFD_WINE_GET_SO_RCVBUF
:
2712 int rcvbuf
= sock
->rcvbuf
;
2714 if (get_reply_max_size() < sizeof(rcvbuf
))
2716 set_error( STATUS_BUFFER_TOO_SMALL
);
2720 set_reply_data( &rcvbuf
, sizeof(rcvbuf
) );
2724 case IOCTL_AFD_WINE_SET_SO_RCVBUF
:
2728 if (get_req_data_size() < sizeof(rcvbuf
))
2730 set_error( STATUS_BUFFER_TOO_SMALL
);
2733 rcvbuf
= *(DWORD
*)get_req_data();
2735 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_RCVBUF
, (char *)&rcvbuf
, sizeof(rcvbuf
) ))
2736 sock
->rcvbuf
= rcvbuf
;
2738 set_error( sock_get_ntstatus( errno
) );
2742 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO
:
2744 DWORD rcvtimeo
= sock
->rcvtimeo
;
2746 if (get_reply_max_size() < sizeof(rcvtimeo
))
2748 set_error( STATUS_BUFFER_TOO_SMALL
);
2752 set_reply_data( &rcvtimeo
, sizeof(rcvtimeo
) );
2756 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO
:
2760 if (get_req_data_size() < sizeof(rcvtimeo
))
2762 set_error( STATUS_BUFFER_TOO_SMALL
);
2765 rcvtimeo
= *(DWORD
*)get_req_data();
2767 sock
->rcvtimeo
= rcvtimeo
;
2771 case IOCTL_AFD_WINE_GET_SO_SNDBUF
:
2773 int sndbuf
= sock
->sndbuf
;
2775 if (get_reply_max_size() < sizeof(sndbuf
))
2777 set_error( STATUS_BUFFER_TOO_SMALL
);
2781 set_reply_data( &sndbuf
, sizeof(sndbuf
) );
2785 case IOCTL_AFD_WINE_SET_SO_SNDBUF
:
2789 if (get_req_data_size() < sizeof(sndbuf
))
2791 set_error( STATUS_BUFFER_TOO_SMALL
);
2794 sndbuf
= *(DWORD
*)get_req_data();
2799 /* setsockopt fails if a zero value is passed */
2800 sock
->sndbuf
= sndbuf
;
2805 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_SNDBUF
, (char *)&sndbuf
, sizeof(sndbuf
) ))
2806 sock
->sndbuf
= sndbuf
;
2808 set_error( sock_get_ntstatus( errno
) );
2812 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO
:
2814 DWORD sndtimeo
= sock
->sndtimeo
;
2816 if (get_reply_max_size() < sizeof(sndtimeo
))
2818 set_error( STATUS_BUFFER_TOO_SMALL
);
2822 set_reply_data( &sndtimeo
, sizeof(sndtimeo
) );
2826 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO
:
2830 if (get_req_data_size() < sizeof(sndtimeo
))
2832 set_error( STATUS_BUFFER_TOO_SMALL
);
2835 sndtimeo
= *(DWORD
*)get_req_data();
2837 sock
->sndtimeo
= sndtimeo
;
2841 case IOCTL_AFD_WINE_GET_SO_CONNECT_TIME
:
2845 if (get_reply_max_size() < sizeof(time
))
2847 set_error( STATUS_BUFFER_TOO_SMALL
);
2851 if (sock
->state
== SOCK_CONNECTED
)
2852 time
= (current_time
- sock
->connect_time
) / 10000000;
2854 set_reply_data( &time
, sizeof(time
) );
2859 set_error( STATUS_NOT_SUPPORTED
);
2864 static int poll_single_socket( struct sock
*sock
, int mask
)
2866 struct pollfd pollfd
;
2868 pollfd
.fd
= get_unix_fd( sock
->fd
);
2869 pollfd
.events
= poll_flags_from_afd( sock
, mask
);
2870 if (pollfd
.events
< 0 || poll( &pollfd
, 1, 0 ) < 0)
2873 if ((mask
& AFD_POLL_HUP
) && (pollfd
.revents
& POLLIN
) && sock
->type
== WS_SOCK_STREAM
)
2877 if (!recv( get_unix_fd( sock
->fd
), &dummy
, 1, MSG_PEEK
))
2879 pollfd
.revents
&= ~POLLIN
;
2880 pollfd
.revents
|= POLLHUP
;
2884 return get_poll_flags( sock
, pollfd
.revents
) & mask
;
2887 static void handle_exclusive_poll(struct poll_req
*req
)
2891 for (i
= 0; i
< req
->count
; ++i
)
2893 struct sock
*sock
= req
->sockets
[i
].sock
;
2894 struct poll_req
*main_poll
= sock
->main_poll
;
2896 if (main_poll
&& main_poll
->exclusive
&& req
->exclusive
)
2898 complete_async_poll( main_poll
, STATUS_SUCCESS
);
2903 sock
->main_poll
= req
;
2907 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
2908 unsigned int count
, const struct poll_socket_input
*input
)
2910 struct poll_socket_output
*output
;
2911 BOOL signaled
= FALSE
;
2912 struct poll_req
*req
;
2915 if (!(output
= mem_alloc( count
* sizeof(*output
) )))
2917 memset( output
, 0, count
* sizeof(*output
) );
2919 if (!(req
= mem_alloc( offsetof( struct poll_req
, sockets
[count
] ) )))
2925 req
->timeout
= NULL
;
2926 if (timeout
&& timeout
!= TIMEOUT_INFINITE
&&
2927 !(req
->timeout
= add_timeout_user( timeout
, async_poll_timeout
, req
)))
2934 for (i
= 0; i
< count
; ++i
)
2936 req
->sockets
[i
].sock
= (struct sock
*)get_handle_obj( current
->process
, input
[i
].socket
, 0, &sock_ops
);
2937 if (!req
->sockets
[i
].sock
)
2939 for (j
= 0; j
< i
; ++j
) release_object( req
->sockets
[i
].sock
);
2940 if (req
->timeout
) remove_timeout_user( req
->timeout
);
2945 req
->sockets
[i
].flags
= input
[i
].flags
;
2948 req
->exclusive
= exclusive
;
2950 req
->async
= (struct async
*)grab_object( async
);
2951 req
->iosb
= async_get_iosb( async
);
2952 req
->output
= output
;
2954 handle_exclusive_poll(req
);
2956 list_add_tail( &poll_list
, &req
->entry
);
2957 async_set_completion_callback( async
, free_poll_req
, req
);
2958 queue_async( &poll_sock
->poll_q
, async
);
2960 for (i
= 0; i
< count
; ++i
)
2962 struct sock
*sock
= req
->sockets
[i
].sock
;
2963 int mask
= req
->sockets
[i
].flags
;
2964 int flags
= poll_single_socket( sock
, mask
);
2969 output
[i
].flags
= flags
;
2970 output
[i
].status
= sock_get_ntstatus( sock_error( sock
->fd
) );
2973 /* FIXME: do other error conditions deserve a similar treatment? */
2974 if (sock
->state
!= SOCK_CONNECTING
&& sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] && (mask
& AFD_POLL_CONNECT_ERR
))
2977 output
[i
].flags
|= AFD_POLL_CONNECT_ERR
;
2978 output
[i
].status
= sock_get_ntstatus( sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] );
2982 if (!timeout
|| signaled
)
2983 complete_async_poll( req
, STATUS_SUCCESS
);
2985 for (i
= 0; i
< req
->count
; ++i
)
2986 sock_reselect( req
->sockets
[i
].sock
);
2987 set_error( STATUS_PENDING
);
2990 #ifdef HAVE_LINUX_RTNETLINK_H
2992 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
2993 static struct object
*ifchange_object
;
2995 static void ifchange_dump( struct object
*obj
, int verbose
);
2996 static struct fd
*ifchange_get_fd( struct object
*obj
);
2997 static void ifchange_destroy( struct object
*obj
);
2999 static int ifchange_get_poll_events( struct fd
*fd
);
3000 static void ifchange_poll_event( struct fd
*fd
, int event
);
3004 struct object obj
; /* object header */
3005 struct fd
*fd
; /* interface change file descriptor */
3006 struct list sockets
; /* list of sockets to send interface change notifications */
3009 static const struct object_ops ifchange_ops
=
3011 sizeof(struct ifchange
), /* size */
3012 &no_type
, /* type */
3013 ifchange_dump
, /* dump */
3014 no_add_queue
, /* add_queue */
3015 NULL
, /* remove_queue */
3016 NULL
, /* signaled */
3017 no_satisfied
, /* satisfied */
3018 no_signal
, /* signal */
3019 ifchange_get_fd
, /* get_fd */
3020 default_map_access
, /* map_access */
3021 default_get_sd
, /* get_sd */
3022 default_set_sd
, /* set_sd */
3023 no_get_full_name
, /* get_full_name */
3024 no_lookup_name
, /* lookup_name */
3025 no_link_name
, /* link_name */
3026 NULL
, /* unlink_name */
3027 no_open_file
, /* open_file */
3028 no_kernel_obj_list
, /* get_kernel_obj_list */
3029 no_close_handle
, /* close_handle */
3030 ifchange_destroy
/* destroy */
3033 static const struct fd_ops ifchange_fd_ops
=
3035 ifchange_get_poll_events
, /* get_poll_events */
3036 ifchange_poll_event
, /* poll_event */
3037 NULL
, /* get_fd_type */
3038 no_fd_read
, /* read */
3039 no_fd_write
, /* write */
3040 no_fd_flush
, /* flush */
3041 no_fd_get_file_info
, /* get_file_info */
3042 no_fd_get_volume_info
, /* get_volume_info */
3043 no_fd_ioctl
, /* ioctl */
3044 NULL
, /* cancel_async */
3045 NULL
, /* queue_async */
3046 NULL
/* reselect_async */
3049 static void ifchange_dump( struct object
*obj
, int verbose
)
3051 assert( obj
->ops
== &ifchange_ops
);
3052 fprintf( stderr
, "Interface change\n" );
3055 static struct fd
*ifchange_get_fd( struct object
*obj
)
3057 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3058 return (struct fd
*)grab_object( ifchange
->fd
);
3061 static void ifchange_destroy( struct object
*obj
)
3063 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3064 assert( obj
->ops
== &ifchange_ops
);
3066 release_object( ifchange
->fd
);
3068 /* reset the global ifchange object so that it will be recreated if it is needed again */
3069 assert( obj
== ifchange_object
);
3070 ifchange_object
= NULL
;
3073 static int ifchange_get_poll_events( struct fd
*fd
)
3078 /* wake up all the sockets waiting for a change notification event */
3079 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
3081 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3082 struct list
*ptr
, *next
;
3083 assert( obj
->ops
== &ifchange_ops
);
3084 assert( obj
== ifchange_object
);
3086 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
3088 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
3090 assert( sock
->ifchange_obj
);
3091 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
3092 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
3096 static void ifchange_poll_event( struct fd
*fd
, int event
)
3098 struct object
*ifchange
= get_fd_user( fd
);
3099 unsigned int status
= STATUS_PENDING
;
3100 char buffer
[PIPE_BUF
];
3103 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
3106 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
3107 return; /* retry when poll() says the socket is ready */
3108 status
= sock_get_ntstatus( errno
);
3112 struct nlmsghdr
*nlh
;
3114 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
3116 if (nlh
->nlmsg_type
== NLMSG_DONE
)
3118 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
3119 status
= STATUS_SUCCESS
;
3122 else status
= STATUS_CANCELLED
;
3124 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
3129 /* we only need one of these interface notification objects, all of the sockets dependent upon
3130 * it will wake up when a notification event occurs */
3131 static struct object
*get_ifchange( void )
3133 #ifdef HAVE_LINUX_RTNETLINK_H
3134 struct ifchange
*ifchange
;
3135 struct sockaddr_nl addr
;
3138 if (ifchange_object
)
3140 /* increment the refcount for each socket that uses the ifchange object */
3141 return grab_object( ifchange_object
);
3144 /* create the socket we need for processing interface change notifications */
3145 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
3148 set_error( sock_get_ntstatus( errno
));
3151 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
3152 memset( &addr
, 0, sizeof(addr
) );
3153 addr
.nl_family
= AF_NETLINK
;
3154 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
3155 /* bind the socket to the special netlink kernel interface */
3156 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
3159 set_error( sock_get_ntstatus( errno
));
3162 if (!(ifchange
= alloc_object( &ifchange_ops
)))
3165 set_error( STATUS_NO_MEMORY
);
3168 list_init( &ifchange
->sockets
);
3169 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
3171 release_object( ifchange
);
3172 set_error( STATUS_NO_MEMORY
);
3175 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
3177 /* the ifchange object is now successfully configured */
3178 ifchange_object
= &ifchange
->obj
;
3179 return &ifchange
->obj
;
3181 set_error( STATUS_NOT_SUPPORTED
);
3186 /* add the socket to the interface change notification list */
3187 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
3189 #ifdef HAVE_LINUX_RTNETLINK_H
3190 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3192 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
3196 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3197 static struct object
*sock_get_ifchange( struct sock
*sock
)
3199 struct object
*ifchange
;
3201 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
3202 return sock
->ifchange_obj
;
3204 if (!(ifchange
= get_ifchange()))
3207 /* add the socket to the ifchange notification list */
3208 ifchange_add_sock( ifchange
, sock
);
3209 sock
->ifchange_obj
= ifchange
;
3213 /* destroy an existing ifchange queue for a specific socket */
3214 static void sock_release_ifchange( struct sock
*sock
)
3216 if (sock
->ifchange_obj
)
3218 list_remove( &sock
->ifchange_entry
);
3219 release_object( sock
->ifchange_obj
);
3220 sock
->ifchange_obj
= NULL
;
3224 static void socket_device_dump( struct object
*obj
, int verbose
);
3225 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3226 unsigned int attr
, struct object
*root
);
3227 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3228 unsigned int sharing
, unsigned int options
);
3230 static const struct object_ops socket_device_ops
=
3232 sizeof(struct object
), /* size */
3233 &device_type
, /* type */
3234 socket_device_dump
, /* dump */
3235 no_add_queue
, /* add_queue */
3236 NULL
, /* remove_queue */
3237 NULL
, /* signaled */
3238 no_satisfied
, /* satisfied */
3239 no_signal
, /* signal */
3240 no_get_fd
, /* get_fd */
3241 default_map_access
, /* map_access */
3242 default_get_sd
, /* get_sd */
3243 default_set_sd
, /* set_sd */
3244 default_get_full_name
, /* get_full_name */
3245 socket_device_lookup_name
, /* lookup_name */
3246 directory_link_name
, /* link_name */
3247 default_unlink_name
, /* unlink_name */
3248 socket_device_open_file
, /* open_file */
3249 no_kernel_obj_list
, /* get_kernel_obj_list */
3250 no_close_handle
, /* close_handle */
3251 no_destroy
/* destroy */
3254 static void socket_device_dump( struct object
*obj
, int verbose
)
3256 fputs( "Socket device\n", stderr
);
3259 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3260 unsigned int attr
, struct object
*root
)
3262 if (name
) name
->len
= 0;
3266 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3267 unsigned int sharing
, unsigned int options
)
3271 if (!(sock
= create_socket())) return NULL
;
3272 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
3274 release_object( sock
);
3280 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
3281 unsigned int attr
, const struct security_descriptor
*sd
)
3283 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
3286 DECL_HANDLER(recv_socket
)
3288 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3289 unsigned int status
= req
->status
;
3290 timeout_t timeout
= 0;
3291 struct async
*async
;
3297 /* recv() returned EWOULDBLOCK, i.e. no data available yet */
3298 if (status
== STATUS_DEVICE_NOT_READY
&& !sock
->nonblocking
)
3300 /* Set a timeout on the async if necessary.
3302 * We want to do this *only* if the client gave us STATUS_DEVICE_NOT_READY.
3303 * If the client gave us STATUS_PENDING, it expects the async to always
3304 * block (it was triggered by WSARecv*() with a valid OVERLAPPED
3305 * structure) and for the timeout not to be respected. */
3306 if (is_fd_overlapped( fd
))
3307 timeout
= (timeout_t
)sock
->rcvtimeo
* -10000;
3309 status
= STATUS_PENDING
;
3312 if ((status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
) && sock
->rd_shutdown
)
3313 status
= STATUS_PIPE_DISCONNECTED
;
3315 sock
->pending_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3316 sock
->reported_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3318 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3320 if (status
== STATUS_SUCCESS
)
3322 struct iosb
*iosb
= async_get_iosb( async
);
3323 iosb
->result
= req
->total
;
3324 release_object( iosb
);
3326 set_error( status
);
3329 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3331 if (status
== STATUS_PENDING
)
3332 queue_async( &sock
->read_q
, async
);
3334 /* always reselect; we changed reported_events above */
3335 sock_reselect( sock
);
3337 reply
->wait
= async_handoff( async
, NULL
, 0 );
3338 reply
->options
= get_fd_options( fd
);
3339 release_object( async
);
3341 release_object( sock
);
3344 DECL_HANDLER(poll_socket
)
3346 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3347 const struct poll_socket_input
*input
= get_req_data();
3348 struct async
*async
;
3353 count
= get_req_data_size() / sizeof(*input
);
3355 if ((async
= create_request_async( sock
->fd
, get_fd_comp_flags( sock
->fd
), &req
->async
)))
3357 poll_socket( sock
, async
, req
->exclusive
, req
->timeout
, count
, input
);
3358 reply
->wait
= async_handoff( async
, NULL
, 0 );
3359 reply
->options
= get_fd_options( sock
->fd
);
3360 release_object( async
);
3363 release_object( sock
);
3366 DECL_HANDLER(send_socket
)
3368 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3369 unsigned int status
= req
->status
;
3370 timeout_t timeout
= 0;
3371 struct async
*async
;
3377 if (sock
->type
== WS_SOCK_DGRAM
)
3379 /* sendto() and sendmsg() implicitly binds a socket */
3380 union unix_sockaddr unix_addr
;
3381 socklen_t unix_len
= sizeof(unix_addr
);
3383 if (!sock
->bound
&& !getsockname( get_unix_fd( fd
), &unix_addr
.addr
, &unix_len
))
3384 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
3388 if (status
!= STATUS_SUCCESS
)
3390 /* send() calls only clear and reselect events if unsuccessful. */
3391 sock
->pending_events
&= ~AFD_POLL_WRITE
;
3392 sock
->reported_events
&= ~AFD_POLL_WRITE
;
3395 /* If we had a short write and the socket is nonblocking (and the client is
3396 * not trying to force the operation to be asynchronous), return success.
3397 * Windows actually refuses to send any data in this case, and returns
3398 * EWOULDBLOCK, but we have no way of doing that. */
3399 if (status
== STATUS_DEVICE_NOT_READY
&& req
->total
&& sock
->nonblocking
)
3400 status
= STATUS_SUCCESS
;
3402 /* send() returned EWOULDBLOCK or a short write, i.e. cannot send all data yet */
3403 if (status
== STATUS_DEVICE_NOT_READY
&& !sock
->nonblocking
)
3405 /* Set a timeout on the async if necessary.
3407 * We want to do this *only* if the client gave us STATUS_DEVICE_NOT_READY.
3408 * If the client gave us STATUS_PENDING, it expects the async to always
3409 * block (it was triggered by WSASend*() with a valid OVERLAPPED
3410 * structure) and for the timeout not to be respected. */
3411 if (is_fd_overlapped( fd
))
3412 timeout
= (timeout_t
)sock
->sndtimeo
* -10000;
3414 status
= STATUS_PENDING
;
3417 if ((status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
) && sock
->wr_shutdown
)
3418 status
= STATUS_PIPE_DISCONNECTED
;
3420 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3422 if (status
== STATUS_SUCCESS
)
3424 struct iosb
*iosb
= async_get_iosb( async
);
3425 iosb
->result
= req
->total
;
3426 release_object( iosb
);
3428 set_error( status
);
3431 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3433 if (status
== STATUS_PENDING
)
3434 queue_async( &sock
->write_q
, async
);
3436 /* always reselect; we changed reported_events above */
3437 sock_reselect( sock
);
3439 reply
->wait
= async_handoff( async
, NULL
, 0 );
3440 reply
->options
= get_fd_options( fd
);
3441 release_object( async
);
3443 release_object( sock
);