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 #ifdef HAVE_SYS_SOCKET_H
46 # include <sys/socket.h>
48 #ifdef HAVE_SYS_IOCTL_H
49 #include <sys/ioctl.h>
51 #ifdef HAVE_SYS_FILIO_H
52 # include <sys/filio.h>
57 #ifdef HAVE_LINUX_FILTER_H
58 # include <linux/filter.h>
60 #ifdef HAVE_LINUX_RTNETLINK_H
61 # include <linux/rtnetlink.h>
64 #ifdef HAVE_NETIPX_IPX_H
65 # include <netipx/ipx.h>
66 #elif defined(HAVE_LINUX_IPX_H)
67 # ifdef HAVE_ASM_TYPES_H
68 # include <asm/types.h>
70 # ifdef HAVE_LINUX_TYPES_H
71 # include <linux/types.h>
73 # include <linux/ipx.h>
75 #if defined(SOL_IPX) || defined(SO_DEFAULT_HEADERS)
79 #ifdef HAVE_LINUX_IRDA_H
80 # ifdef HAVE_LINUX_TYPES_H
81 # include <linux/types.h>
83 # include <linux/irda.h>
88 #define WIN32_NO_STATUS
106 #if defined(linux) && !defined(IP_UNICAST_IF)
107 #define IP_UNICAST_IF 50
110 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
114 struct WS_sockaddr addr
;
115 struct WS_sockaddr_in in
;
116 struct WS_sockaddr_in6 in6
;
117 struct WS_sockaddr_ipx ipx
;
121 static struct list poll_list
= LIST_INIT( poll_list
);
128 struct timeout_user
*timeout
;
131 struct poll_socket_output
*output
;
144 struct sock
*sock
, *acceptsock
;
146 unsigned int recv_len
, local_len
;
154 unsigned int addr_len
, send_len
, send_cursor
;
157 enum connection_state
168 struct object obj
; /* object header */
169 struct fd
*fd
; /* socket file descriptor */
170 enum connection_state state
; /* connection state */
171 unsigned int mask
; /* event mask */
172 /* pending AFD_POLL_* events which have not yet been reported to the application */
173 unsigned int pending_events
;
174 /* AFD_POLL_* events which have already been reported and should not be
175 * selected for again until reset by a relevant call.
177 * For example, if AFD_POLL_READ is set here and not in pending_events, it
178 * has already been reported and consumed, and we should not report it
179 * again, even if POLLIN is signaled, until it is reset by e.g recv().
181 * If an event has been signaled and not consumed yet, it will be set in
182 * both pending_events and reported_events (as we should only ever report
183 * any event once until it is reset.) */
184 unsigned int reported_events
;
185 unsigned int flags
; /* socket flags */
186 unsigned short proto
; /* socket protocol */
187 unsigned short type
; /* socket type */
188 unsigned short family
; /* socket family */
189 struct event
*event
; /* event object */
190 user_handle_t window
; /* window to send the message to */
191 unsigned int message
; /* message to send */
192 obj_handle_t wparam
; /* message wparam (socket handle) */
193 int errors
[AFD_POLL_BIT_COUNT
]; /* event errors */
194 timeout_t connect_time
;/* time the socket was connected */
195 struct sock
*deferred
; /* socket that waits for a deferred accept */
196 struct async_queue read_q
; /* queue for asynchronous reads */
197 struct async_queue write_q
; /* queue for asynchronous writes */
198 struct async_queue ifchange_q
; /* queue for interface change notifications */
199 struct async_queue accept_q
; /* queue for asynchronous accepts */
200 struct async_queue connect_q
; /* queue for asynchronous connects */
201 struct async_queue poll_q
; /* queue for asynchronous polls */
202 struct object
*ifchange_obj
; /* the interface change notification object */
203 struct list ifchange_entry
; /* entry in ifchange notification list */
204 struct list accept_list
; /* list of pending accept requests */
205 struct accept_req
*accept_recv_req
; /* pending accept-into request which will recv on this socket */
206 struct connect_req
*connect_req
; /* pending connection request */
207 struct poll_req
*main_poll
; /* main poll */
208 union win_sockaddr addr
; /* socket name */
209 int addr_len
; /* socket name length */
210 unsigned int rcvbuf
; /* advisory recv buffer size */
211 unsigned int sndbuf
; /* advisory send buffer size */
212 unsigned int rcvtimeo
; /* receive timeout in ms */
213 unsigned int sndtimeo
; /* send timeout in ms */
214 unsigned int rd_shutdown
: 1; /* is the read end shut down? */
215 unsigned int wr_shutdown
: 1; /* is the write end shut down? */
216 unsigned int wr_shutdown_pending
: 1; /* is a write shutdown pending? */
217 unsigned int hangup
: 1; /* has the read end received a hangup? */
218 unsigned int aborted
: 1; /* did we get a POLLERR or irregular POLLHUP? */
219 unsigned int nonblocking
: 1; /* is the socket nonblocking? */
220 unsigned int bound
: 1; /* is the socket bound? */
223 static void sock_dump( struct object
*obj
, int verbose
);
224 static struct fd
*sock_get_fd( struct object
*obj
);
225 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
226 static void sock_destroy( struct object
*obj
);
227 static struct object
*sock_get_ifchange( struct sock
*sock
);
228 static void sock_release_ifchange( struct sock
*sock
);
230 static int sock_get_poll_events( struct fd
*fd
);
231 static void sock_poll_event( struct fd
*fd
, int event
);
232 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
233 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
234 static void sock_cancel_async( struct fd
*fd
, struct async
*async
);
235 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
);
236 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
238 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
);
239 static struct sock
*accept_socket( struct sock
*sock
);
240 static int sock_get_ntstatus( int err
);
241 static unsigned int sock_get_error( int err
);
243 static const struct object_ops sock_ops
=
245 sizeof(struct sock
), /* size */
246 &file_type
, /* type */
247 sock_dump
, /* dump */
248 add_queue
, /* add_queue */
249 remove_queue
, /* remove_queue */
250 default_fd_signaled
, /* signaled */
251 no_satisfied
, /* satisfied */
252 no_signal
, /* signal */
253 sock_get_fd
, /* get_fd */
254 default_map_access
, /* map_access */
255 default_get_sd
, /* get_sd */
256 default_set_sd
, /* set_sd */
257 no_get_full_name
, /* get_full_name */
258 no_lookup_name
, /* lookup_name */
259 no_link_name
, /* link_name */
260 NULL
, /* unlink_name */
261 no_open_file
, /* open_file */
262 no_kernel_obj_list
, /* get_kernel_obj_list */
263 sock_close_handle
, /* close_handle */
264 sock_destroy
/* destroy */
267 static const struct fd_ops sock_fd_ops
=
269 sock_get_poll_events
, /* get_poll_events */
270 sock_poll_event
, /* poll_event */
271 sock_get_fd_type
, /* get_fd_type */
272 no_fd_read
, /* read */
273 no_fd_write
, /* write */
274 no_fd_flush
, /* flush */
275 default_fd_get_file_info
, /* get_file_info */
276 no_fd_get_volume_info
, /* get_volume_info */
277 sock_ioctl
, /* ioctl */
278 sock_cancel_async
, /* cancel_async */
279 sock_queue_async
, /* queue_async */
280 sock_reselect_async
/* reselect_async */
285 struct sockaddr addr
;
286 struct sockaddr_in in
;
287 struct sockaddr_in6 in6
;
289 struct sockaddr_ipx ipx
;
292 struct sockaddr_irda irda
;
296 static int sockaddr_from_unix( const union unix_sockaddr
*uaddr
, struct WS_sockaddr
*wsaddr
, socklen_t wsaddrlen
)
298 memset( wsaddr
, 0, wsaddrlen
);
300 switch (uaddr
->addr
.sa_family
)
304 struct WS_sockaddr_in win
= {0};
306 if (wsaddrlen
< sizeof(win
)) return -1;
307 win
.sin_family
= WS_AF_INET
;
308 win
.sin_port
= uaddr
->in
.sin_port
;
309 memcpy( &win
.sin_addr
, &uaddr
->in
.sin_addr
, sizeof(win
.sin_addr
) );
310 memcpy( wsaddr
, &win
, sizeof(win
) );
316 struct WS_sockaddr_in6 win
= {0};
318 if (wsaddrlen
< sizeof(win
)) return -1;
319 win
.sin6_family
= WS_AF_INET6
;
320 win
.sin6_port
= uaddr
->in6
.sin6_port
;
321 win
.sin6_flowinfo
= uaddr
->in6
.sin6_flowinfo
;
322 memcpy( &win
.sin6_addr
, &uaddr
->in6
.sin6_addr
, sizeof(win
.sin6_addr
) );
323 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
324 win
.sin6_scope_id
= uaddr
->in6
.sin6_scope_id
;
326 memcpy( wsaddr
, &win
, sizeof(win
) );
333 struct WS_sockaddr_ipx win
= {0};
335 if (wsaddrlen
< sizeof(win
)) return -1;
336 win
.sa_family
= WS_AF_IPX
;
337 memcpy( win
.sa_netnum
, &uaddr
->ipx
.sipx_network
, sizeof(win
.sa_netnum
) );
338 memcpy( win
.sa_nodenum
, &uaddr
->ipx
.sipx_node
, sizeof(win
.sa_nodenum
) );
339 win
.sa_socket
= uaddr
->ipx
.sipx_port
;
340 memcpy( wsaddr
, &win
, sizeof(win
) );
350 if (wsaddrlen
< sizeof(win
)) return -1;
351 win
.irdaAddressFamily
= WS_AF_IRDA
;
352 memcpy( win
.irdaDeviceID
, &uaddr
->irda
.sir_addr
, sizeof(win
.irdaDeviceID
) );
353 if (uaddr
->irda
.sir_lsap_sel
!= LSAP_ANY
)
354 snprintf( win
.irdaServiceName
, sizeof(win
.irdaServiceName
), "LSAP-SEL%u", uaddr
->irda
.sir_lsap_sel
);
356 memcpy( win
.irdaServiceName
, uaddr
->irda
.sir_name
, sizeof(win
.irdaServiceName
) );
357 memcpy( wsaddr
, &win
, sizeof(win
) );
371 static socklen_t
sockaddr_to_unix( const struct WS_sockaddr
*wsaddr
, int wsaddrlen
, union unix_sockaddr
*uaddr
)
373 memset( uaddr
, 0, sizeof(*uaddr
) );
375 switch (wsaddr
->sa_family
)
379 struct WS_sockaddr_in win
= {0};
381 if (wsaddrlen
< sizeof(win
)) return 0;
382 memcpy( &win
, wsaddr
, sizeof(win
) );
383 uaddr
->in
.sin_family
= AF_INET
;
384 uaddr
->in
.sin_port
= win
.sin_port
;
385 memcpy( &uaddr
->in
.sin_addr
, &win
.sin_addr
, sizeof(win
.sin_addr
) );
386 return sizeof(uaddr
->in
);
391 struct WS_sockaddr_in6 win
= {0};
393 if (wsaddrlen
< sizeof(win
)) return 0;
394 memcpy( &win
, wsaddr
, sizeof(win
) );
395 uaddr
->in6
.sin6_family
= AF_INET6
;
396 uaddr
->in6
.sin6_port
= win
.sin6_port
;
397 uaddr
->in6
.sin6_flowinfo
= win
.sin6_flowinfo
;
398 memcpy( &uaddr
->in6
.sin6_addr
, &win
.sin6_addr
, sizeof(win
.sin6_addr
) );
399 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
400 uaddr
->in6
.sin6_scope_id
= win
.sin6_scope_id
;
402 return sizeof(uaddr
->in6
);
408 struct WS_sockaddr_ipx win
= {0};
410 if (wsaddrlen
< sizeof(win
)) return 0;
411 memcpy( &win
, wsaddr
, sizeof(win
) );
412 uaddr
->ipx
.sipx_family
= AF_IPX
;
413 memcpy( &uaddr
->ipx
.sipx_network
, win
.sa_netnum
, sizeof(win
.sa_netnum
) );
414 memcpy( &uaddr
->ipx
.sipx_node
, win
.sa_nodenum
, sizeof(win
.sa_nodenum
) );
415 uaddr
->ipx
.sipx_port
= win
.sa_socket
;
416 return sizeof(uaddr
->ipx
);
423 SOCKADDR_IRDA win
= {0};
424 unsigned int lsap_sel
;
426 if (wsaddrlen
< sizeof(win
)) return 0;
427 memcpy( &win
, wsaddr
, sizeof(win
) );
428 uaddr
->irda
.sir_family
= AF_IRDA
;
429 if (sscanf( win
.irdaServiceName
, "LSAP-SEL%u", &lsap_sel
) == 1)
430 uaddr
->irda
.sir_lsap_sel
= lsap_sel
;
433 uaddr
->irda
.sir_lsap_sel
= LSAP_ANY
;
434 memcpy( uaddr
->irda
.sir_name
, win
.irdaServiceName
, sizeof(win
.irdaServiceName
) );
436 memcpy( &uaddr
->irda
.sir_addr
, win
.irdaDeviceID
, sizeof(win
.irdaDeviceID
) );
437 return sizeof(uaddr
->irda
);
444 default: /* likely an ipv4 address */
445 case sizeof(struct WS_sockaddr_in
):
446 return sizeof(uaddr
->in
);
449 case sizeof(struct WS_sockaddr_ipx
):
450 return sizeof(uaddr
->ipx
);
454 case sizeof(SOCKADDR_IRDA
):
455 return sizeof(uaddr
->irda
);
458 case sizeof(struct WS_sockaddr_in6
):
459 return sizeof(uaddr
->in6
);
467 /* some events are generated at the same time but must be sent in a particular
468 * order (e.g. CONNECT must be sent before READ) */
469 static const enum afd_poll_bit event_bitorder
[] =
471 AFD_POLL_BIT_CONNECT
,
472 AFD_POLL_BIT_CONNECT_ERR
,
483 SOCK_SHUTDOWN_ERROR
= -1,
484 SOCK_SHUTDOWN_EOF
= 0,
485 SOCK_SHUTDOWN_POLLHUP
= 1
488 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
490 static sock_shutdown_t
sock_check_pollhup(void)
492 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
497 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
498 if ( shutdown( fd
[0], 1 ) ) goto out
;
504 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
505 n
= poll( &pfd
, 1, 1 );
506 if ( n
!= 1 ) goto out
; /* error or timeout */
507 if ( pfd
.revents
& POLLHUP
)
508 ret
= SOCK_SHUTDOWN_POLLHUP
;
509 else if ( pfd
.revents
& POLLIN
&&
510 read( fd
[1], &dummy
, 1 ) == 0 )
511 ret
= SOCK_SHUTDOWN_EOF
;
521 sock_shutdown_type
= sock_check_pollhup();
523 switch ( sock_shutdown_type
)
525 case SOCK_SHUTDOWN_EOF
:
526 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
528 case SOCK_SHUTDOWN_POLLHUP
:
529 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
532 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
533 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
537 static int sock_reselect( struct sock
*sock
)
539 int ev
= sock_get_poll_events( sock
->fd
);
542 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
544 set_fd_events( sock
->fd
, ev
);
548 static unsigned int afd_poll_flag_to_win32( unsigned int flags
)
550 static const unsigned int map
[] =
554 FD_WRITE
, /* WRITE */
556 FD_CLOSE
, /* RESET */
558 FD_CONNECT
, /* CONNECT */
559 FD_ACCEPT
, /* ACCEPT */
560 FD_CONNECT
, /* CONNECT_ERR */
563 unsigned int i
, ret
= 0;
565 for (i
= 0; i
< ARRAY_SIZE(map
); ++i
)
567 if (flags
& (1 << i
)) ret
|= map
[i
];
573 /* wake anybody waiting on the socket event or send the associated message */
574 static void sock_wake_up( struct sock
*sock
)
576 unsigned int events
= sock
->pending_events
& sock
->mask
;
581 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
583 set_event( sock
->event
);
587 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
588 for (i
= 0; i
< ARRAY_SIZE(event_bitorder
); i
++)
590 enum afd_poll_bit event
= event_bitorder
[i
];
591 if (events
& (1 << event
))
593 lparam_t lparam
= afd_poll_flag_to_win32(1 << event
) | (sock_get_error( sock
->errors
[event
] ) << 16);
594 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
597 sock
->pending_events
= 0;
598 sock_reselect( sock
);
602 static inline int sock_error( struct fd
*fd
)
604 unsigned int optval
= 0;
605 socklen_t optlen
= sizeof(optval
);
607 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
611 static void free_accept_req( void *private )
613 struct accept_req
*req
= private;
614 list_remove( &req
->entry
);
617 req
->acceptsock
->accept_recv_req
= NULL
;
618 release_object( req
->acceptsock
);
620 release_object( req
->async
);
621 release_object( req
->iosb
);
622 release_object( req
->sock
);
626 static void fill_accept_output( struct accept_req
*req
)
628 const data_size_t out_size
= req
->iosb
->out_size
;
629 struct async
*async
= req
->async
;
630 union unix_sockaddr unix_addr
;
631 struct WS_sockaddr
*win_addr
;
632 unsigned int remote_len
;
638 if (!(out_data
= mem_alloc( out_size
)))
640 async_terminate( async
, get_error() );
644 fd
= get_unix_fd( req
->acceptsock
->fd
);
646 if (req
->recv_len
&& (size
= recv( fd
, out_data
, req
->recv_len
, 0 )) < 0)
648 if (!req
->accepted
&& errno
== EWOULDBLOCK
)
651 sock_reselect( req
->acceptsock
);
655 async_terminate( async
, sock_get_ntstatus( errno
) );
662 if (req
->local_len
< sizeof(int))
664 async_terminate( async
, STATUS_BUFFER_TOO_SMALL
);
669 unix_len
= sizeof(unix_addr
);
670 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ sizeof(int));
671 if (getsockname( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
672 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, req
->local_len
- sizeof(int) )) < 0)
674 async_terminate( async
, sock_get_ntstatus( errno
) );
678 memcpy( out_data
+ req
->recv_len
, &win_len
, sizeof(int) );
681 unix_len
= sizeof(unix_addr
);
682 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ req
->local_len
+ sizeof(int));
683 remote_len
= out_size
- req
->recv_len
- req
->local_len
;
684 if (getpeername( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
685 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, remote_len
- sizeof(int) )) < 0)
687 async_terminate( async
, sock_get_ntstatus( errno
) );
691 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
693 async_request_complete( req
->async
, STATUS_SUCCESS
, size
, out_size
, out_data
);
696 static void complete_async_accept( struct sock
*sock
, struct accept_req
*req
)
698 struct sock
*acceptsock
= req
->acceptsock
;
699 struct async
*async
= req
->async
;
701 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
705 if (!accept_into_socket( sock
, acceptsock
))
707 async_terminate( async
, get_error() );
710 fill_accept_output( req
);
716 if (!(acceptsock
= accept_socket( sock
)))
718 async_terminate( async
, get_error() );
721 handle
= alloc_handle_no_access_check( async_get_thread( async
)->process
, &acceptsock
->obj
,
722 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
723 acceptsock
->wparam
= handle
;
724 release_object( acceptsock
);
727 async_terminate( async
, get_error() );
731 async_request_complete_alloc( req
->async
, STATUS_SUCCESS
, 0, sizeof(handle
), &handle
);
735 static void complete_async_accept_recv( struct accept_req
*req
)
737 if (debug_level
) fprintf( stderr
, "completing accept recv request for socket %p\n", req
->acceptsock
);
739 assert( req
->recv_len
);
741 fill_accept_output( req
);
744 static void free_connect_req( void *private )
746 struct connect_req
*req
= private;
748 req
->sock
->connect_req
= NULL
;
749 release_object( req
->async
);
750 release_object( req
->iosb
);
751 release_object( req
->sock
);
755 static void complete_async_connect( struct sock
*sock
)
757 struct connect_req
*req
= sock
->connect_req
;
758 const char *in_buffer
;
762 if (debug_level
) fprintf( stderr
, "completing connect request for socket %p\n", sock
);
764 sock
->state
= SOCK_CONNECTED
;
768 async_terminate( req
->async
, STATUS_SUCCESS
);
772 in_buffer
= (const char *)req
->iosb
->in_data
+ sizeof(struct afd_connect_params
) + req
->addr_len
;
773 len
= req
->send_len
- req
->send_cursor
;
775 ret
= send( get_unix_fd( sock
->fd
), in_buffer
+ req
->send_cursor
, len
, 0 );
776 if (ret
< 0 && errno
!= EWOULDBLOCK
)
777 async_terminate( req
->async
, sock_get_ntstatus( errno
) );
779 async_request_complete( req
->async
, STATUS_SUCCESS
, req
->send_len
, 0, NULL
);
781 req
->send_cursor
+= ret
;
784 static void free_poll_req( void *private )
786 struct poll_req
*req
= private;
789 if (req
->timeout
) remove_timeout_user( req
->timeout
);
791 for (i
= 0; i
< req
->count
; ++i
)
792 release_object( req
->sockets
[i
].sock
);
793 release_object( req
->async
);
794 release_object( req
->iosb
);
795 list_remove( &req
->entry
);
800 static int is_oobinline( struct sock
*sock
)
803 socklen_t len
= sizeof(oobinline
);
804 return !getsockopt( get_unix_fd( sock
->fd
), SOL_SOCKET
, SO_OOBINLINE
, (char *)&oobinline
, &len
) && oobinline
;
807 static int get_poll_flags( struct sock
*sock
, int event
)
811 /* A connection-mode socket which has never been connected does not return
812 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
813 if (sock
->state
== SOCK_UNCONNECTED
)
814 event
&= ~(POLLOUT
| POLLHUP
);
818 if (sock
->state
== SOCK_LISTENING
)
819 flags
|= AFD_POLL_ACCEPT
;
821 flags
|= AFD_POLL_READ
;
824 flags
|= is_oobinline( sock
) ? AFD_POLL_READ
: AFD_POLL_OOB
;
826 flags
|= AFD_POLL_WRITE
;
827 if (sock
->state
== SOCK_CONNECTED
)
828 flags
|= AFD_POLL_CONNECT
;
830 flags
|= AFD_POLL_HUP
;
832 flags
|= AFD_POLL_CONNECT_ERR
;
837 static void complete_async_poll( struct poll_req
*req
, unsigned int status
)
839 struct poll_socket_output
*output
= req
->output
;
842 for (i
= 0; i
< req
->count
; ++i
)
844 struct sock
*sock
= req
->sockets
[i
].sock
;
846 if (sock
->main_poll
== req
)
847 sock
->main_poll
= NULL
;
850 /* pass 0 as result; client will set actual result size */
852 async_request_complete( req
->async
, status
, 0, req
->count
* sizeof(*output
), output
);
855 static void complete_async_polls( struct sock
*sock
, int event
, int error
)
857 int flags
= get_poll_flags( sock
, event
);
858 struct poll_req
*req
, *next
;
860 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &poll_list
, struct poll_req
, entry
)
864 if (req
->iosb
->status
!= STATUS_PENDING
) continue;
866 for (i
= 0; i
< req
->count
; ++i
)
868 if (req
->sockets
[i
].sock
!= sock
) continue;
869 if (!(req
->sockets
[i
].flags
& flags
)) continue;
872 fprintf( stderr
, "completing poll for socket %p, wanted %#x got %#x\n",
873 sock
, req
->sockets
[i
].flags
, flags
);
875 req
->output
[i
].flags
= req
->sockets
[i
].flags
& flags
;
876 req
->output
[i
].status
= sock_get_ntstatus( error
);
878 complete_async_poll( req
, STATUS_SUCCESS
);
884 static void async_poll_timeout( void *private )
886 struct poll_req
*req
= private;
890 if (req
->iosb
->status
!= STATUS_PENDING
) return;
892 complete_async_poll( req
, STATUS_TIMEOUT
);
895 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
897 if (event
& (POLLIN
| POLLPRI
))
899 struct accept_req
*req
;
901 LIST_FOR_EACH_ENTRY( req
, &sock
->accept_list
, struct accept_req
, entry
)
903 if (req
->iosb
->status
== STATUS_PENDING
&& !req
->accepted
)
905 complete_async_accept( sock
, req
);
910 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
911 complete_async_accept_recv( sock
->accept_recv_req
);
914 if ((event
& POLLOUT
) && sock
->connect_req
&& sock
->connect_req
->iosb
->status
== STATUS_PENDING
)
915 complete_async_connect( sock
);
917 if (event
& (POLLIN
| POLLPRI
) && async_waiting( &sock
->read_q
))
919 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
920 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
921 event
&= ~(POLLIN
| POLLPRI
);
924 if (event
& POLLOUT
&& async_waiting( &sock
->write_q
))
926 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
927 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
931 if (event
& (POLLERR
| POLLHUP
))
933 int status
= sock_get_ntstatus( error
);
934 struct accept_req
*req
, *next
;
936 if (sock
->rd_shutdown
|| sock
->hangup
)
937 async_wake_up( &sock
->read_q
, status
);
938 if (sock
->wr_shutdown
)
939 async_wake_up( &sock
->write_q
, status
);
941 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
943 if (req
->iosb
->status
== STATUS_PENDING
)
944 async_terminate( req
->async
, status
);
947 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
948 async_terminate( sock
->accept_recv_req
->async
, status
);
950 if (sock
->connect_req
)
951 async_terminate( sock
->connect_req
->async
, status
);
957 static void post_socket_event( struct sock
*sock
, enum afd_poll_bit event_bit
, int error
)
959 unsigned int event
= (1 << event_bit
);
961 if (!(sock
->reported_events
& event
))
963 sock
->pending_events
|= event
;
964 sock
->reported_events
|= event
;
965 sock
->errors
[event_bit
] = error
;
969 static void sock_dispatch_events( struct sock
*sock
, enum connection_state prevstate
, int event
, int error
)
973 case SOCK_UNCONNECTED
:
976 case SOCK_CONNECTING
:
979 post_socket_event( sock
, AFD_POLL_BIT_CONNECT
, 0 );
980 sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] = 0;
982 if (event
& (POLLERR
| POLLHUP
))
983 post_socket_event( sock
, AFD_POLL_BIT_CONNECT_ERR
, error
);
987 if (event
& (POLLIN
| POLLERR
| POLLHUP
))
988 post_socket_event( sock
, AFD_POLL_BIT_ACCEPT
, error
);
992 case SOCK_CONNECTIONLESS
:
994 post_socket_event( sock
, AFD_POLL_BIT_READ
, 0 );
997 post_socket_event( sock
, AFD_POLL_BIT_WRITE
, 0 );
1000 post_socket_event( sock
, AFD_POLL_BIT_OOB
, 0 );
1002 if (event
& (POLLERR
| POLLHUP
))
1003 post_socket_event( sock
, AFD_POLL_BIT_HUP
, error
);
1007 sock_wake_up( sock
);
1010 static void sock_poll_event( struct fd
*fd
, int event
)
1012 struct sock
*sock
= get_fd_user( fd
);
1013 int hangup_seen
= 0;
1014 enum connection_state prevstate
= sock
->state
;
1017 assert( sock
->obj
.ops
== &sock_ops
);
1019 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
1021 /* we may change event later, remove from loop here */
1022 if (event
& (POLLERR
|POLLHUP
)) set_fd_events( sock
->fd
, -1 );
1024 switch (sock
->state
)
1026 case SOCK_UNCONNECTED
:
1029 case SOCK_CONNECTING
:
1030 if (event
& (POLLERR
|POLLHUP
))
1032 sock
->state
= SOCK_UNCONNECTED
;
1034 error
= sock_error( fd
);
1036 else if (event
& POLLOUT
)
1038 sock
->state
= SOCK_CONNECTED
;
1039 sock
->connect_time
= current_time
;
1043 case SOCK_LISTENING
:
1044 if (event
& (POLLERR
|POLLHUP
))
1045 error
= sock_error( fd
);
1048 case SOCK_CONNECTED
:
1049 case SOCK_CONNECTIONLESS
:
1050 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
1055 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
1056 * has been closed, so we need to check for it explicitly here */
1057 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
1066 /* EAGAIN can happen if an async recv() falls between the server's poll()
1067 call and the invocation of this routine */
1068 if ( errno
!= EAGAIN
)
1073 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
1078 if (hangup_seen
|| (sock_shutdown_type
== SOCK_SHUTDOWN_POLLHUP
&& (event
& POLLHUP
)))
1082 else if (event
& (POLLHUP
| POLLERR
))
1087 fprintf( stderr
, "socket %p aborted by error %d, event %#x\n", sock
, error
, event
);
1095 complete_async_polls( sock
, event
, error
);
1097 event
= sock_dispatch_asyncs( sock
, event
, error
);
1098 sock_dispatch_events( sock
, prevstate
, event
, error
);
1100 sock_reselect( sock
);
1103 static void sock_dump( struct object
*obj
, int verbose
)
1105 struct sock
*sock
= (struct sock
*)obj
;
1106 assert( obj
->ops
== &sock_ops
);
1107 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
1108 sock
->fd
, sock
->state
,
1109 sock
->mask
, sock
->pending_events
, sock
->reported_events
);
1112 static int poll_flags_from_afd( struct sock
*sock
, int flags
)
1116 /* A connection-mode socket which has never been connected does
1117 * not return write or hangup events, but Linux returns
1118 * POLLOUT | POLLHUP. */
1119 if (sock
->state
== SOCK_UNCONNECTED
)
1122 if (flags
& (AFD_POLL_READ
| AFD_POLL_ACCEPT
))
1124 if ((flags
& AFD_POLL_HUP
) && sock
->type
== WS_SOCK_STREAM
)
1126 if (flags
& AFD_POLL_OOB
)
1127 ev
|= is_oobinline( sock
) ? POLLIN
: POLLPRI
;
1128 if (flags
& AFD_POLL_WRITE
)
1134 static int sock_get_poll_events( struct fd
*fd
)
1136 struct sock
*sock
= get_fd_user( fd
);
1137 unsigned int mask
= sock
->mask
& ~sock
->reported_events
;
1138 struct poll_req
*req
;
1141 assert( sock
->obj
.ops
== &sock_ops
);
1143 if (!sock
->type
) /* not initialized yet */
1146 switch (sock
->state
)
1148 case SOCK_UNCONNECTED
:
1149 /* A connection-mode Windows socket which has never been connected does
1150 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1151 * need to return -1 here, to prevent the socket from being polled on at
1155 case SOCK_CONNECTING
:
1158 case SOCK_LISTENING
:
1159 if (!list_empty( &sock
->accept_list
) || (mask
& AFD_POLL_ACCEPT
))
1163 case SOCK_CONNECTED
:
1164 case SOCK_CONNECTIONLESS
:
1165 if (sock
->hangup
&& sock
->wr_shutdown
&& !sock
->wr_shutdown_pending
)
1167 /* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
1168 * if both the socket and its peer are SHUT_WR.
1170 * We don't use SHUT_RD, so we can only encounter this in the latter
1171 * case. In that case there can't be any pending read requests (they
1172 * would have already been completed with a length of zero), the
1173 * above condition ensures that we don't have any pending write
1174 * requests, and nothing that can change about the socket state that
1175 * would complete a pending poll request. */
1182 if (sock
->accept_recv_req
)
1186 else if (async_queued( &sock
->read_q
))
1188 if (async_waiting( &sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
1192 /* Don't ask for POLLIN if we got a hangup. We won't receive more
1193 * data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
1196 if (mask
& AFD_POLL_READ
)
1198 if (mask
& AFD_POLL_OOB
)
1202 /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
1203 if (sock
->state
== SOCK_CONNECTED
&& (mask
& AFD_POLL_HUP
) && !(sock
->reported_events
& AFD_POLL_READ
))
1207 if (async_queued( &sock
->write_q
))
1209 if (async_waiting( &sock
->write_q
)) ev
|= POLLOUT
;
1211 else if (!sock
->wr_shutdown
&& (mask
& AFD_POLL_WRITE
))
1219 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1223 for (i
= 0; i
< req
->count
; ++i
)
1225 if (req
->sockets
[i
].sock
!= sock
) continue;
1227 ev
|= poll_flags_from_afd( sock
, req
->sockets
[i
].flags
);
1234 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
1236 return FD_TYPE_SOCKET
;
1239 static void sock_cancel_async( struct fd
*fd
, struct async
*async
)
1241 struct poll_req
*req
;
1243 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1247 if (req
->async
!= async
)
1250 for (i
= 0; i
< req
->count
; i
++)
1252 struct sock
*sock
= req
->sockets
[i
].sock
;
1254 if (sock
->main_poll
== req
)
1255 sock
->main_poll
= NULL
;
1259 async_terminate( async
, STATUS_CANCELLED
);
1262 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
1264 struct sock
*sock
= get_fd_user( fd
);
1265 struct async_queue
*queue
;
1267 assert( sock
->obj
.ops
== &sock_ops
);
1271 case ASYNC_TYPE_READ
:
1272 if (sock
->rd_shutdown
)
1274 set_error( STATUS_PIPE_DISCONNECTED
);
1277 queue
= &sock
->read_q
;
1280 case ASYNC_TYPE_WRITE
:
1281 if (sock
->wr_shutdown
)
1283 set_error( STATUS_PIPE_DISCONNECTED
);
1286 queue
= &sock
->write_q
;
1290 set_error( STATUS_INVALID_PARAMETER
);
1294 if (sock
->state
!= SOCK_CONNECTED
)
1296 set_error( STATUS_PIPE_DISCONNECTED
);
1300 queue_async( queue
, async
);
1301 sock_reselect( sock
);
1303 set_error( STATUS_PENDING
);
1306 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
1308 struct sock
*sock
= get_fd_user( fd
);
1310 if (sock
->wr_shutdown_pending
&& list_empty( &sock
->write_q
.queue
))
1312 shutdown( get_unix_fd( sock
->fd
), SHUT_WR
);
1313 sock
->wr_shutdown_pending
= 0;
1316 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1317 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1319 if (queue
!= &sock
->ifchange_q
&& sock
->type
)
1320 sock_reselect( sock
);
1323 static struct fd
*sock_get_fd( struct object
*obj
)
1325 struct sock
*sock
= (struct sock
*)obj
;
1326 return (struct fd
*)grab_object( sock
->fd
);
1329 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
1331 struct sock
*sock
= (struct sock
*)obj
;
1333 if (sock
->obj
.handle_count
== 1) /* last handle */
1335 struct accept_req
*accept_req
, *accept_next
;
1336 struct poll_req
*poll_req
, *poll_next
;
1338 if (sock
->accept_recv_req
)
1339 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
1341 LIST_FOR_EACH_ENTRY_SAFE( accept_req
, accept_next
, &sock
->accept_list
, struct accept_req
, entry
)
1342 async_terminate( accept_req
->async
, STATUS_CANCELLED
);
1344 if (sock
->connect_req
)
1345 async_terminate( sock
->connect_req
->async
, STATUS_CANCELLED
);
1347 LIST_FOR_EACH_ENTRY_SAFE( poll_req
, poll_next
, &poll_list
, struct poll_req
, entry
)
1349 struct iosb
*iosb
= poll_req
->iosb
;
1350 BOOL signaled
= FALSE
;
1353 if (iosb
->status
!= STATUS_PENDING
) continue;
1355 for (i
= 0; i
< poll_req
->count
; ++i
)
1357 if (poll_req
->sockets
[i
].sock
== sock
)
1360 poll_req
->output
[i
].flags
= AFD_POLL_CLOSE
;
1361 poll_req
->output
[i
].status
= 0;
1365 if (signaled
) complete_async_poll( poll_req
, STATUS_SUCCESS
);
1372 static void sock_destroy( struct object
*obj
)
1374 struct sock
*sock
= (struct sock
*)obj
;
1376 assert( obj
->ops
== &sock_ops
);
1378 /* FIXME: special socket shutdown stuff? */
1380 if ( sock
->deferred
)
1381 release_object( sock
->deferred
);
1383 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
1384 sock_release_ifchange( sock
);
1385 free_async_queue( &sock
->read_q
);
1386 free_async_queue( &sock
->write_q
);
1387 free_async_queue( &sock
->ifchange_q
);
1388 free_async_queue( &sock
->accept_q
);
1389 free_async_queue( &sock
->connect_q
);
1390 free_async_queue( &sock
->poll_q
);
1391 if (sock
->event
) release_object( sock
->event
);
1394 /* shut the socket down to force pending poll() calls in the client to return */
1395 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
1396 release_object( sock
->fd
);
1400 static struct sock
*create_socket(void)
1404 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
1406 sock
->state
= SOCK_UNCONNECTED
;
1408 sock
->pending_events
= 0;
1409 sock
->reported_events
= 0;
1418 sock
->connect_time
= 0;
1419 sock
->deferred
= NULL
;
1420 sock
->ifchange_obj
= NULL
;
1421 sock
->accept_recv_req
= NULL
;
1422 sock
->connect_req
= NULL
;
1423 sock
->main_poll
= NULL
;
1424 memset( &sock
->addr
, 0, sizeof(sock
->addr
) );
1426 sock
->rd_shutdown
= 0;
1427 sock
->wr_shutdown
= 0;
1428 sock
->wr_shutdown_pending
= 0;
1431 sock
->nonblocking
= 0;
1437 init_async_queue( &sock
->read_q
);
1438 init_async_queue( &sock
->write_q
);
1439 init_async_queue( &sock
->ifchange_q
);
1440 init_async_queue( &sock
->accept_q
);
1441 init_async_queue( &sock
->connect_q
);
1442 init_async_queue( &sock
->poll_q
);
1443 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
1444 list_init( &sock
->accept_list
);
1448 static int get_unix_family( int family
)
1452 case WS_AF_INET
: return AF_INET
;
1453 case WS_AF_INET6
: return AF_INET6
;
1455 case WS_AF_IPX
: return AF_IPX
;
1458 case WS_AF_IRDA
: return AF_IRDA
;
1460 case WS_AF_UNSPEC
: return AF_UNSPEC
;
1465 static int get_unix_type( int type
)
1469 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
1470 case WS_SOCK_RAW
: return SOCK_RAW
;
1471 case WS_SOCK_STREAM
: return SOCK_STREAM
;
1476 static int get_unix_protocol( int protocol
)
1478 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1483 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
1484 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1485 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1486 case WS_IPPROTO_IPV4
: return IPPROTO_IPIP
;
1487 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1488 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1489 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1490 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1495 static void set_dont_fragment( int fd
, int level
, int value
)
1499 if (level
== IPPROTO_IP
)
1502 optname
= IP_DONTFRAG
;
1503 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1504 optname
= IP_MTU_DISCOVER
;
1505 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1512 #ifdef IPV6_DONTFRAG
1513 optname
= IPV6_DONTFRAG
;
1514 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1515 optname
= IPV6_MTU_DISCOVER
;
1516 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1522 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1525 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
, unsigned int flags
)
1527 unsigned int options
= 0;
1528 int sockfd
, unix_type
, unix_family
, unix_protocol
, value
;
1531 unix_family
= get_unix_family( family
);
1532 unix_type
= get_unix_type( type
);
1533 unix_protocol
= get_unix_protocol( protocol
);
1535 if (unix_protocol
< 0)
1537 if (type
&& unix_type
< 0)
1538 set_win32_error( WSAESOCKTNOSUPPORT
);
1540 set_win32_error( WSAEPROTONOSUPPORT
);
1543 if (unix_family
< 0)
1545 if (family
>= 0 && unix_type
< 0)
1546 set_win32_error( WSAESOCKTNOSUPPORT
);
1548 set_win32_error( WSAEAFNOSUPPORT
);
1552 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1555 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1556 else set_win32_error( sock_get_error( errno
));
1559 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1561 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1564 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1567 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1570 /* Should we retrieve val using a getsockopt call and then
1571 * set the modified one? */
1572 val
.ipx_pt
= ipx_type
;
1573 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1578 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1580 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1581 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1582 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1583 else if (unix_type
== SOCK_STREAM
)
1584 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1588 if (unix_family
== AF_INET6
)
1590 static const int enable
= 1;
1591 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1595 len
= sizeof(value
);
1596 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_RCVBUF
, &value
, &len
))
1597 sock
->rcvbuf
= value
;
1599 len
= sizeof(value
);
1600 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_SNDBUF
, &value
, &len
))
1601 sock
->sndbuf
= value
;
1603 sock
->state
= (type
== WS_SOCK_STREAM
? SOCK_UNCONNECTED
: SOCK_CONNECTIONLESS
);
1604 sock
->flags
= flags
;
1605 sock
->proto
= protocol
;
1607 sock
->family
= family
;
1611 options
= get_fd_options( sock
->fd
);
1612 release_object( sock
->fd
);
1615 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1620 /* We can't immediately allow caching for a connection-mode socket, since it
1621 * might be accepted into (changing the underlying fd object.) */
1622 if (sock
->type
!= WS_SOCK_STREAM
) allow_fd_caching( sock
->fd
);
1627 /* accepts a socket and inits it */
1628 static int accept_new_fd( struct sock
*sock
)
1631 /* Try to accept(2). We can't be safe that this an already connected socket
1632 * or that accept() is allowed on it. In those cases we will get -1/errno
1635 struct sockaddr saddr
;
1636 socklen_t slen
= sizeof(saddr
);
1637 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1639 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1641 set_error( sock_get_ntstatus( errno
));
1645 /* accept a socket (creates a new fd) */
1646 static struct sock
*accept_socket( struct sock
*sock
)
1648 struct sock
*acceptsock
;
1651 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1653 if ( sock
->deferred
)
1655 acceptsock
= sock
->deferred
;
1656 sock
->deferred
= NULL
;
1660 union unix_sockaddr unix_addr
;
1663 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1664 if (!(acceptsock
= create_socket()))
1670 /* newly created socket gets the same properties of the listening socket */
1671 acceptsock
->state
= SOCK_CONNECTED
;
1672 acceptsock
->bound
= 1;
1673 acceptsock
->nonblocking
= sock
->nonblocking
;
1674 acceptsock
->mask
= sock
->mask
;
1675 acceptsock
->proto
= sock
->proto
;
1676 acceptsock
->type
= sock
->type
;
1677 acceptsock
->family
= sock
->family
;
1678 acceptsock
->window
= sock
->window
;
1679 acceptsock
->message
= sock
->message
;
1680 acceptsock
->connect_time
= current_time
;
1681 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
1682 acceptsock
->flags
= sock
->flags
;
1683 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1684 get_fd_options( sock
->fd
) )))
1686 release_object( acceptsock
);
1689 unix_len
= sizeof(unix_addr
);
1690 if (!getsockname( acceptfd
, &unix_addr
.addr
, &unix_len
))
1691 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1694 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1695 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1696 sock_reselect( sock
);
1700 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
1702 union unix_sockaddr unix_addr
;
1707 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
1709 if ( sock
->deferred
)
1711 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
1712 get_fd_options( acceptsock
->fd
) );
1716 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
1718 release_object( sock
->deferred
);
1719 sock
->deferred
= NULL
;
1723 if ((acceptfd
= accept_new_fd( sock
)) == -1)
1726 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1727 get_fd_options( acceptsock
->fd
) )))
1731 acceptsock
->state
= SOCK_CONNECTED
;
1732 acceptsock
->pending_events
= 0;
1733 acceptsock
->reported_events
= 0;
1734 acceptsock
->proto
= sock
->proto
;
1735 acceptsock
->type
= sock
->type
;
1736 acceptsock
->family
= sock
->family
;
1737 acceptsock
->wparam
= 0;
1738 acceptsock
->deferred
= NULL
;
1739 acceptsock
->connect_time
= current_time
;
1740 fd_copy_completion( acceptsock
->fd
, newfd
);
1741 release_object( acceptsock
->fd
);
1742 acceptsock
->fd
= newfd
;
1744 unix_len
= sizeof(unix_addr
);
1745 if (!getsockname( get_unix_fd( newfd
), &unix_addr
.addr
, &unix_len
))
1746 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1749 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1750 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1751 sock_reselect( sock
);
1758 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
1760 static const int enable
= 1;
1763 if (!(index
= if_nametoindex( name
)))
1766 if (setsockopt( fd
, IPPROTO_IP
, IP_BOUND_IF
, &index
, sizeof(index
) ))
1769 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
1772 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER) && defined(SO_BINDTODEVICE)
1774 struct interface_filter
1776 struct sock_filter iface_memaddr
;
1777 struct sock_filter iface_rule
;
1778 struct sock_filter ip_memaddr
;
1779 struct sock_filter ip_rule
;
1780 struct sock_filter return_keep
;
1781 struct sock_filter return_dump
;
1783 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
1784 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1785 /sizeof(struct sock_filter)
1786 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
1787 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1788 /sizeof(struct sock_filter)
1789 # define FILTER_JUMP_NEXT() (u_char)(0)
1790 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
1791 static struct interface_filter generic_interface_filter
=
1793 /* This filter rule allows incoming packets on the specified interface, which works for all
1794 * remotely generated packets and for locally generated broadcast packets. */
1795 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
1796 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
1797 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
1798 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
1799 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
1800 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
1801 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
1802 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
1805 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
1807 struct interface_filter specific_interface_filter
;
1808 struct sock_fprog filter_prog
;
1809 static const int enable
= 1;
1813 if (!setsockopt( fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen( name
) + 1 ))
1816 /* SO_BINDTODEVICE requires NET_CAP_RAW until Linux 5.7. */
1818 fprintf( stderr
, "setsockopt SO_BINDTODEVICE fd %d, name %s failed: %s, falling back to SO_REUSE_ADDR\n",
1819 fd
, name
, strerror( errno
));
1821 if (!(index
= if_nametoindex( name
)))
1824 ifindex
= htonl( index
);
1825 if (setsockopt( fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
) ) < 0)
1828 specific_interface_filter
= generic_interface_filter
;
1829 specific_interface_filter
.iface_rule
.k
= index
;
1830 specific_interface_filter
.ip_rule
.k
= htonl( bind_addr
);
1831 filter_prog
.len
= sizeof(generic_interface_filter
) / sizeof(struct sock_filter
);
1832 filter_prog
.filter
= (struct sock_filter
*)&specific_interface_filter
;
1833 if (setsockopt( fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
) ))
1836 return setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) );
1841 static int bind_to_iface_name( int fd
, in_addr_t bind_addr
, const char *name
)
1847 #endif /* LINUX_BOUND_IF */
1849 /* Take bind() calls on any name corresponding to a local network adapter and
1850 * restrict the given socket to operating only on the specified interface. This
1851 * restriction consists of two components:
1852 * 1) An outgoing packet restriction suggesting the egress interface for all
1854 * 2) An incoming packet restriction dropping packets not meant for the
1856 * If the function succeeds in placing these restrictions, then the name for the
1857 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
1858 * receipt of broadcast packets on the socket. This behavior is only relevant to
1859 * UDP sockets and is needed for applications that expect to be able to receive
1860 * broadcast packets on a socket that is bound to a specific network interface.
1862 static int bind_to_interface( struct sock
*sock
, const struct sockaddr_in
*addr
)
1864 in_addr_t bind_addr
= addr
->sin_addr
.s_addr
;
1865 struct ifaddrs
*ifaddrs
, *ifaddr
;
1866 int fd
= get_unix_fd( sock
->fd
);
1869 if (bind_addr
== htonl( INADDR_ANY
) || bind_addr
== htonl( INADDR_LOOPBACK
))
1871 if (sock
->type
!= WS_SOCK_DGRAM
)
1874 if (getifaddrs( &ifaddrs
) < 0) return 0;
1876 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
1878 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET
1879 && ((struct sockaddr_in
*)ifaddr
->ifa_addr
)->sin_addr
.s_addr
== bind_addr
)
1881 if ((err
= bind_to_iface_name( fd
, bind_addr
, ifaddr
->ifa_name
)) < 0)
1884 fprintf( stderr
, "failed to bind to interface: %s\n", strerror( errno
) );
1889 freeifaddrs( ifaddrs
);
1893 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1894 static unsigned int get_ipv6_interface_index( const struct in6_addr
*addr
)
1896 struct ifaddrs
*ifaddrs
, *ifaddr
;
1898 if (getifaddrs( &ifaddrs
) < 0) return 0;
1900 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
1902 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET6
1903 && !memcmp( &((struct sockaddr_in6
*)ifaddr
->ifa_addr
)->sin6_addr
, addr
, sizeof(*addr
) ))
1905 unsigned int index
= if_nametoindex( ifaddr
->ifa_name
);
1910 fprintf( stderr
, "Unable to look up interface index for %s: %s\n",
1911 ifaddr
->ifa_name
, strerror( errno
) );
1915 freeifaddrs( ifaddrs
);
1920 freeifaddrs( ifaddrs
);
1925 /* return an errno value mapped to a WSA error */
1926 static unsigned int sock_get_error( int err
)
1930 case EINTR
: return WSAEINTR
;
1931 case EBADF
: return WSAEBADF
;
1933 case EACCES
: return WSAEACCES
;
1934 case EFAULT
: return WSAEFAULT
;
1935 case EINVAL
: return WSAEINVAL
;
1936 case EMFILE
: return WSAEMFILE
;
1938 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
1939 case EALREADY
: return WSAEALREADY
;
1940 case ENOTSOCK
: return WSAENOTSOCK
;
1941 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
1942 case EMSGSIZE
: return WSAEMSGSIZE
;
1943 case EPROTOTYPE
: return WSAEPROTOTYPE
;
1944 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
1945 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
1946 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
1947 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
1948 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
1949 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
1950 case EADDRINUSE
: return WSAEADDRINUSE
;
1951 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
1952 case ENETDOWN
: return WSAENETDOWN
;
1953 case ENETUNREACH
: return WSAENETUNREACH
;
1954 case ENETRESET
: return WSAENETRESET
;
1955 case ECONNABORTED
: return WSAECONNABORTED
;
1957 case ECONNRESET
: return WSAECONNRESET
;
1958 case ENOBUFS
: return WSAENOBUFS
;
1959 case EISCONN
: return WSAEISCONN
;
1960 case ENOTCONN
: return WSAENOTCONN
;
1961 case ESHUTDOWN
: return WSAESHUTDOWN
;
1962 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
1963 case ETIMEDOUT
: return WSAETIMEDOUT
;
1964 case ECONNREFUSED
: return WSAECONNREFUSED
;
1965 case ELOOP
: return WSAELOOP
;
1966 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
1967 case EHOSTDOWN
: return WSAEHOSTDOWN
;
1968 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
1969 case ENOTEMPTY
: return WSAENOTEMPTY
;
1971 case EPROCLIM
: return WSAEPROCLIM
;
1974 case EUSERS
: return WSAEUSERS
;
1977 case EDQUOT
: return WSAEDQUOT
;
1980 case ESTALE
: return WSAESTALE
;
1983 case EREMOTE
: return WSAEREMOTE
;
1989 perror("wineserver: sock_get_error() can't map error");
1994 static int sock_get_ntstatus( int err
)
1998 case EBADF
: return STATUS_INVALID_HANDLE
;
1999 case EBUSY
: return STATUS_DEVICE_BUSY
;
2001 case EACCES
: return STATUS_ACCESS_DENIED
;
2002 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
2003 case EINVAL
: return STATUS_INVALID_PARAMETER
;
2005 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
2007 case EWOULDBLOCK
: return STATUS_DEVICE_NOT_READY
;
2008 case EALREADY
: return STATUS_NETWORK_BUSY
;
2009 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
2010 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
2011 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
2012 case EPROTONOSUPPORT
:
2013 case ESOCKTNOSUPPORT
:
2016 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
2017 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
2018 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
2019 case EADDRINUSE
: return STATUS_SHARING_VIOLATION
;
2020 /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
2021 * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
2023 case EADDRNOTAVAIL
: return STATUS_INVALID_ADDRESS_COMPONENT
;
2024 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
2025 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
2026 case ENOTCONN
: return STATUS_INVALID_CONNECTION
;
2027 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
2028 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
2029 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
2030 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
2032 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
2033 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
2034 case EISCONN
: return STATUS_CONNECTION_ACTIVE
;
2036 case 0: return STATUS_SUCCESS
;
2039 perror("wineserver: sock_get_ntstatus() can't map error");
2040 return STATUS_UNSUCCESSFUL
;
2044 static struct accept_req
*alloc_accept_req( struct sock
*sock
, struct sock
*acceptsock
, struct async
*async
,
2045 const struct afd_accept_into_params
*params
)
2047 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
2051 req
->async
= (struct async
*)grab_object( async
);
2052 req
->iosb
= async_get_iosb( async
);
2053 req
->sock
= (struct sock
*)grab_object( sock
);
2054 req
->acceptsock
= acceptsock
;
2055 if (acceptsock
) grab_object( acceptsock
);
2061 req
->recv_len
= params
->recv_len
;
2062 req
->local_len
= params
->local_len
;
2068 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2070 struct sock
*sock
= get_fd_user( fd
);
2073 assert( sock
->obj
.ops
== &sock_ops
);
2075 if (code
!= IOCTL_AFD_WINE_CREATE
&& (unix_fd
= get_unix_fd( fd
)) < 0) return;
2079 case IOCTL_AFD_WINE_CREATE
:
2081 const struct afd_create_params
*params
= get_req_data();
2083 if (get_req_data_size() != sizeof(*params
))
2085 set_error( STATUS_INVALID_PARAMETER
);
2088 init_socket( sock
, params
->family
, params
->type
, params
->protocol
, params
->flags
);
2092 case IOCTL_AFD_WINE_ACCEPT
:
2094 struct sock
*acceptsock
;
2095 obj_handle_t handle
;
2097 if (get_reply_max_size() != sizeof(handle
))
2099 set_error( STATUS_BUFFER_TOO_SMALL
);
2103 if (!(acceptsock
= accept_socket( sock
)))
2105 struct accept_req
*req
;
2107 if (sock
->nonblocking
) return;
2108 if (get_error() != STATUS_DEVICE_NOT_READY
) return;
2110 if (!(req
= alloc_accept_req( sock
, NULL
, async
, NULL
))) return;
2111 list_add_tail( &sock
->accept_list
, &req
->entry
);
2113 async_set_completion_callback( async
, free_accept_req
, req
);
2114 queue_async( &sock
->accept_q
, async
);
2115 sock_reselect( sock
);
2116 set_error( STATUS_PENDING
);
2119 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
2120 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
2121 acceptsock
->wparam
= handle
;
2122 release_object( acceptsock
);
2123 set_reply_data( &handle
, sizeof(handle
) );
2127 case IOCTL_AFD_WINE_ACCEPT_INTO
:
2129 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
2130 const struct afd_accept_into_params
*params
= get_req_data();
2131 struct sock
*acceptsock
;
2132 unsigned int remote_len
;
2133 struct accept_req
*req
;
2135 if (get_req_data_size() != sizeof(*params
) ||
2136 get_reply_max_size() < params
->recv_len
||
2137 get_reply_max_size() - params
->recv_len
< params
->local_len
)
2139 set_error( STATUS_BUFFER_TOO_SMALL
);
2143 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
2144 if (remote_len
< sizeof(int))
2146 set_error( STATUS_INVALID_PARAMETER
);
2150 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
2153 if (acceptsock
->accept_recv_req
)
2155 release_object( acceptsock
);
2156 set_error( STATUS_INVALID_PARAMETER
);
2160 if (!(req
= alloc_accept_req( sock
, acceptsock
, async
, params
)))
2162 release_object( acceptsock
);
2165 list_add_tail( &sock
->accept_list
, &req
->entry
);
2166 acceptsock
->accept_recv_req
= req
;
2167 release_object( acceptsock
);
2169 acceptsock
->wparam
= params
->accept_handle
;
2170 async_set_completion_callback( async
, free_accept_req
, req
);
2171 queue_async( &sock
->accept_q
, async
);
2172 sock_reselect( sock
);
2173 set_error( STATUS_PENDING
);
2177 case IOCTL_AFD_LISTEN
:
2179 const struct afd_listen_params
*params
= get_req_data();
2181 if (get_req_data_size() < sizeof(*params
))
2183 set_error( STATUS_INVALID_PARAMETER
);
2189 set_error( STATUS_INVALID_PARAMETER
);
2193 if (listen( unix_fd
, params
->backlog
) < 0)
2195 set_error( sock_get_ntstatus( errno
) );
2199 sock
->state
= SOCK_LISTENING
;
2201 /* a listening socket can no longer be accepted into */
2202 allow_fd_caching( sock
->fd
);
2204 /* we may already be selecting for AFD_POLL_ACCEPT */
2205 sock_reselect( sock
);
2209 case IOCTL_AFD_WINE_CONNECT
:
2211 const struct afd_connect_params
*params
= get_req_data();
2212 const struct WS_sockaddr
*addr
;
2213 union unix_sockaddr unix_addr
;
2214 struct connect_req
*req
;
2218 if (get_req_data_size() < sizeof(*params
) ||
2219 get_req_data_size() - sizeof(*params
) < params
->addr_len
)
2221 set_error( STATUS_BUFFER_TOO_SMALL
);
2224 send_len
= get_req_data_size() - sizeof(*params
) - params
->addr_len
;
2225 addr
= (const struct WS_sockaddr
*)(params
+ 1);
2227 if (!params
->synchronous
&& !sock
->bound
)
2229 set_error( STATUS_INVALID_PARAMETER
);
2233 if (sock
->accept_recv_req
)
2235 set_error( STATUS_INVALID_PARAMETER
);
2239 if (sock
->connect_req
)
2241 set_error( STATUS_INVALID_PARAMETER
);
2245 switch (sock
->state
)
2247 case SOCK_LISTENING
:
2248 set_error( STATUS_INVALID_PARAMETER
);
2251 case SOCK_CONNECTING
:
2252 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2253 * but there's no status code that maps to WSAEALREADY... */
2254 set_error( params
->synchronous
? STATUS_ADDRESS_ALREADY_ASSOCIATED
: STATUS_INVALID_PARAMETER
);
2257 case SOCK_CONNECTED
:
2258 set_error( STATUS_CONNECTION_ACTIVE
);
2261 case SOCK_UNCONNECTED
:
2262 case SOCK_CONNECTIONLESS
:
2266 unix_len
= sockaddr_to_unix( addr
, params
->addr_len
, &unix_addr
);
2269 set_error( STATUS_INVALID_ADDRESS
);
2272 if (unix_addr
.addr
.sa_family
== AF_INET
&& !memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 ))
2273 unix_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_LOOPBACK
);
2275 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2276 if (ret
< 0 && errno
!= EINPROGRESS
)
2278 set_error( sock_get_ntstatus( errno
) );
2282 /* a connected or connecting socket can no longer be accepted into */
2283 allow_fd_caching( sock
->fd
);
2285 unix_len
= sizeof(unix_addr
);
2286 if (!sock
->bound
&& !getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
))
2287 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2292 sock
->state
= SOCK_CONNECTED
;
2294 if (!send_len
) return;
2297 sock
->state
= SOCK_CONNECTING
;
2299 if (params
->synchronous
&& sock
->nonblocking
)
2301 sock_reselect( sock
);
2302 set_error( STATUS_DEVICE_NOT_READY
);
2306 if (!(req
= mem_alloc( sizeof(*req
) )))
2309 req
->async
= (struct async
*)grab_object( async
);
2310 req
->iosb
= async_get_iosb( async
);
2311 req
->sock
= (struct sock
*)grab_object( sock
);
2312 req
->addr_len
= params
->addr_len
;
2313 req
->send_len
= send_len
;
2314 req
->send_cursor
= 0;
2316 async_set_completion_callback( async
, free_connect_req
, req
);
2317 sock
->connect_req
= req
;
2318 queue_async( &sock
->connect_q
, async
);
2319 sock_reselect( sock
);
2320 set_error( STATUS_PENDING
);
2324 case IOCTL_AFD_WINE_SHUTDOWN
:
2328 if (get_req_data_size() < sizeof(int))
2330 set_error( STATUS_BUFFER_TOO_SMALL
);
2333 how
= *(int *)get_req_data();
2337 set_error( STATUS_INVALID_PARAMETER
);
2341 if (sock
->state
!= SOCK_CONNECTED
&& sock
->state
!= SOCK_CONNECTIONLESS
)
2343 set_error( STATUS_INVALID_CONNECTION
);
2349 sock
->rd_shutdown
= 1;
2351 if (how
!= SD_RECEIVE
)
2353 sock
->wr_shutdown
= 1;
2354 if (list_empty( &sock
->write_q
.queue
))
2355 shutdown( unix_fd
, SHUT_WR
);
2357 sock
->wr_shutdown_pending
= 1;
2362 if (sock
->event
) release_object( sock
->event
);
2366 sock
->nonblocking
= 1;
2369 sock_reselect( sock
);
2373 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE
:
2377 if (get_req_data_size() < sizeof(int))
2379 set_error( STATUS_BUFFER_TOO_SMALL
);
2382 force_async
= *(int *)get_req_data();
2384 if (sock
->nonblocking
&& !force_async
)
2386 set_error( STATUS_DEVICE_NOT_READY
);
2389 if (!sock_get_ifchange( sock
)) return;
2390 queue_async( &sock
->ifchange_q
, async
);
2391 set_error( STATUS_PENDING
);
2395 case IOCTL_AFD_WINE_FIONBIO
:
2396 if (get_req_data_size() < sizeof(int))
2398 set_error( STATUS_BUFFER_TOO_SMALL
);
2401 if (*(int *)get_req_data())
2403 sock
->nonblocking
= 1;
2409 set_error( STATUS_INVALID_PARAMETER
);
2412 sock
->nonblocking
= 0;
2416 case IOCTL_AFD_GET_EVENTS
:
2418 struct afd_get_events_params params
= {0};
2421 if (get_reply_max_size() < sizeof(params
))
2423 set_error( STATUS_INVALID_PARAMETER
);
2427 params
.flags
= sock
->pending_events
& sock
->mask
;
2428 for (i
= 0; i
< ARRAY_SIZE( params
.status
); ++i
)
2429 params
.status
[i
] = sock_get_ntstatus( sock
->errors
[i
] );
2431 sock
->pending_events
= 0;
2432 sock_reselect( sock
);
2434 set_reply_data( ¶ms
, sizeof(params
) );
2438 case IOCTL_AFD_EVENT_SELECT
:
2440 struct event
*event
= NULL
;
2441 obj_handle_t event_handle
;
2444 set_async_pending( async
);
2446 if (is_machine_64bit( current
->process
->machine
))
2448 const struct afd_event_select_params_64
*params
= get_req_data();
2450 if (get_req_data_size() < sizeof(*params
))
2452 set_error( STATUS_INVALID_PARAMETER
);
2456 event_handle
= params
->event
;
2457 mask
= params
->mask
;
2461 const struct afd_event_select_params_32
*params
= get_req_data();
2463 if (get_req_data_size() < sizeof(*params
))
2465 set_error( STATUS_INVALID_PARAMETER
);
2469 event_handle
= params
->event
;
2470 mask
= params
->mask
;
2473 if ((event_handle
|| mask
) &&
2474 !(event
= get_event_obj( current
->process
, event_handle
, EVENT_MODIFY_STATE
)))
2476 set_error( STATUS_INVALID_PARAMETER
);
2480 if (sock
->event
) release_object( sock
->event
);
2481 sock
->event
= event
;
2486 sock
->nonblocking
= 1;
2488 sock_reselect( sock
);
2493 case IOCTL_AFD_WINE_MESSAGE_SELECT
:
2495 const struct afd_message_select_params
*params
= get_req_data();
2497 if (get_req_data_size() < sizeof(params
))
2499 set_error( STATUS_BUFFER_TOO_SMALL
);
2503 if (sock
->event
) release_object( sock
->event
);
2507 sock
->pending_events
= 0;
2508 sock
->reported_events
= 0;
2511 sock
->mask
= params
->mask
;
2512 sock
->window
= params
->window
;
2513 sock
->message
= params
->message
;
2514 sock
->wparam
= params
->handle
;
2515 sock
->nonblocking
= 1;
2517 sock_reselect( sock
);
2522 case IOCTL_AFD_BIND
:
2524 const struct afd_bind_params
*params
= get_req_data();
2525 union unix_sockaddr unix_addr
, bind_addr
;
2526 data_size_t in_size
;
2529 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2531 if (get_req_data_size() < get_reply_max_size())
2533 set_error( STATUS_BUFFER_TOO_SMALL
);
2536 in_size
= get_req_data_size() - get_reply_max_size();
2537 if (in_size
< offsetof(struct afd_bind_params
, addr
.sa_data
)
2538 || get_reply_max_size() < in_size
- sizeof(int))
2540 set_error( STATUS_INVALID_PARAMETER
);
2546 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED
);
2550 unix_len
= sockaddr_to_unix( ¶ms
->addr
, in_size
- sizeof(int), &unix_addr
);
2553 set_error( STATUS_INVALID_ADDRESS
);
2556 bind_addr
= unix_addr
;
2558 if (unix_addr
.addr
.sa_family
== AF_INET
)
2560 if (!memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 )
2561 || bind_to_interface( sock
, &unix_addr
.in
))
2562 bind_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
2564 else if (unix_addr
.addr
.sa_family
== AF_INET6
)
2566 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2567 /* Windows allows specifying zero to use the default scope. Linux
2568 * interprets it as an interface index and requires that it be
2570 if (!unix_addr
.in6
.sin6_scope_id
)
2571 bind_addr
.in6
.sin6_scope_id
= get_ipv6_interface_index( &unix_addr
.in6
.sin6_addr
);
2575 set_async_pending( async
);
2577 if (bind( unix_fd
, &bind_addr
.addr
, unix_len
) < 0)
2579 if (errno
== EADDRINUSE
)
2582 socklen_t len
= sizeof(reuse
);
2584 if (!getsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&reuse
, &len
) && reuse
)
2588 set_error( sock_get_ntstatus( errno
) );
2594 unix_len
= sizeof(bind_addr
);
2595 if (!getsockname( unix_fd
, &bind_addr
.addr
, &unix_len
))
2597 /* store the interface or magic loopback address instead of the
2598 * actual unix address */
2599 if (bind_addr
.addr
.sa_family
== AF_INET
)
2600 bind_addr
.in
.sin_addr
= unix_addr
.in
.sin_addr
;
2601 sock
->addr_len
= sockaddr_from_unix( &bind_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2604 if (get_reply_max_size() >= sock
->addr_len
)
2605 set_reply_data( &sock
->addr
, sock
->addr_len
);
2609 case IOCTL_AFD_GETSOCKNAME
:
2612 set_error( STATUS_INVALID_PARAMETER
);
2616 if (get_reply_max_size() < sock
->addr_len
)
2618 set_error( STATUS_BUFFER_TOO_SMALL
);
2622 set_reply_data( &sock
->addr
, sock
->addr_len
);
2625 case IOCTL_AFD_WINE_DEFER
:
2627 const obj_handle_t
*handle
= get_req_data();
2628 struct sock
*acceptsock
;
2630 if (get_req_data_size() < sizeof(*handle
))
2632 set_error( STATUS_BUFFER_TOO_SMALL
);
2636 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, *handle
, 0, &sock_ops
);
2637 if (!acceptsock
) return;
2639 sock
->deferred
= acceptsock
;
2643 case IOCTL_AFD_WINE_GET_INFO
:
2645 struct afd_get_info_params params
;
2647 if (get_reply_max_size() < sizeof(params
))
2649 set_error( STATUS_BUFFER_TOO_SMALL
);
2653 params
.family
= sock
->family
;
2654 params
.type
= sock
->type
;
2655 params
.protocol
= sock
->proto
;
2656 set_reply_data( ¶ms
, sizeof(params
) );
2660 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN
:
2662 int listening
= (sock
->state
== SOCK_LISTENING
);
2664 if (get_reply_max_size() < sizeof(listening
))
2666 set_error( STATUS_BUFFER_TOO_SMALL
);
2670 set_reply_data( &listening
, sizeof(listening
) );
2674 case IOCTL_AFD_WINE_GET_SO_ERROR
:
2677 socklen_t len
= sizeof(error
);
2680 if (get_reply_max_size() < sizeof(error
))
2682 set_error( STATUS_BUFFER_TOO_SMALL
);
2686 if (getsockopt( unix_fd
, SOL_SOCKET
, SO_ERROR
, (char *)&error
, &len
) < 0)
2688 set_error( sock_get_ntstatus( errno
) );
2694 for (i
= 0; i
< ARRAY_SIZE( sock
->errors
); ++i
)
2696 if (sock
->errors
[i
])
2698 error
= sock_get_error( sock
->errors
[i
] );
2704 set_reply_data( &error
, sizeof(error
) );
2708 case IOCTL_AFD_WINE_GET_SO_RCVBUF
:
2710 int rcvbuf
= sock
->rcvbuf
;
2712 if (get_reply_max_size() < sizeof(rcvbuf
))
2714 set_error( STATUS_BUFFER_TOO_SMALL
);
2718 set_reply_data( &rcvbuf
, sizeof(rcvbuf
) );
2722 case IOCTL_AFD_WINE_SET_SO_RCVBUF
:
2726 if (get_req_data_size() < sizeof(rcvbuf
))
2728 set_error( STATUS_BUFFER_TOO_SMALL
);
2731 rcvbuf
= *(DWORD
*)get_req_data();
2733 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_RCVBUF
, (char *)&rcvbuf
, sizeof(rcvbuf
) ))
2734 sock
->rcvbuf
= rcvbuf
;
2736 set_error( sock_get_ntstatus( errno
) );
2740 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO
:
2742 DWORD rcvtimeo
= sock
->rcvtimeo
;
2744 if (get_reply_max_size() < sizeof(rcvtimeo
))
2746 set_error( STATUS_BUFFER_TOO_SMALL
);
2750 set_reply_data( &rcvtimeo
, sizeof(rcvtimeo
) );
2754 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO
:
2758 if (get_req_data_size() < sizeof(rcvtimeo
))
2760 set_error( STATUS_BUFFER_TOO_SMALL
);
2763 rcvtimeo
= *(DWORD
*)get_req_data();
2765 sock
->rcvtimeo
= rcvtimeo
;
2769 case IOCTL_AFD_WINE_GET_SO_SNDBUF
:
2771 int sndbuf
= sock
->sndbuf
;
2773 if (get_reply_max_size() < sizeof(sndbuf
))
2775 set_error( STATUS_BUFFER_TOO_SMALL
);
2779 set_reply_data( &sndbuf
, sizeof(sndbuf
) );
2783 case IOCTL_AFD_WINE_SET_SO_SNDBUF
:
2787 if (get_req_data_size() < sizeof(sndbuf
))
2789 set_error( STATUS_BUFFER_TOO_SMALL
);
2792 sndbuf
= *(DWORD
*)get_req_data();
2797 /* setsockopt fails if a zero value is passed */
2798 sock
->sndbuf
= sndbuf
;
2803 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_SNDBUF
, (char *)&sndbuf
, sizeof(sndbuf
) ))
2804 sock
->sndbuf
= sndbuf
;
2806 set_error( sock_get_ntstatus( errno
) );
2810 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO
:
2812 DWORD sndtimeo
= sock
->sndtimeo
;
2814 if (get_reply_max_size() < sizeof(sndtimeo
))
2816 set_error( STATUS_BUFFER_TOO_SMALL
);
2820 set_reply_data( &sndtimeo
, sizeof(sndtimeo
) );
2824 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO
:
2828 if (get_req_data_size() < sizeof(sndtimeo
))
2830 set_error( STATUS_BUFFER_TOO_SMALL
);
2833 sndtimeo
= *(DWORD
*)get_req_data();
2835 sock
->sndtimeo
= sndtimeo
;
2839 case IOCTL_AFD_WINE_GET_SO_CONNECT_TIME
:
2843 if (get_reply_max_size() < sizeof(time
))
2845 set_error( STATUS_BUFFER_TOO_SMALL
);
2849 if (sock
->state
== SOCK_CONNECTED
)
2850 time
= (current_time
- sock
->connect_time
) / 10000000;
2852 set_reply_data( &time
, sizeof(time
) );
2857 set_error( STATUS_NOT_SUPPORTED
);
2862 static int poll_single_socket( struct sock
*sock
, int mask
)
2864 struct pollfd pollfd
;
2866 pollfd
.fd
= get_unix_fd( sock
->fd
);
2867 pollfd
.events
= poll_flags_from_afd( sock
, mask
);
2868 if (pollfd
.events
< 0 || poll( &pollfd
, 1, 0 ) < 0)
2871 if ((mask
& AFD_POLL_HUP
) && (pollfd
.revents
& POLLIN
) && sock
->type
== WS_SOCK_STREAM
)
2875 if (!recv( get_unix_fd( sock
->fd
), &dummy
, 1, MSG_PEEK
))
2877 pollfd
.revents
&= ~POLLIN
;
2878 pollfd
.revents
|= POLLHUP
;
2882 return get_poll_flags( sock
, pollfd
.revents
) & mask
;
2885 static void handle_exclusive_poll(struct poll_req
*req
)
2889 for (i
= 0; i
< req
->count
; ++i
)
2891 struct sock
*sock
= req
->sockets
[i
].sock
;
2892 struct poll_req
*main_poll
= sock
->main_poll
;
2894 if (main_poll
&& main_poll
->exclusive
&& req
->exclusive
)
2896 complete_async_poll( main_poll
, STATUS_SUCCESS
);
2901 sock
->main_poll
= req
;
2905 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
2906 unsigned int count
, const struct poll_socket_input
*input
)
2908 struct poll_socket_output
*output
;
2909 BOOL signaled
= FALSE
;
2910 struct poll_req
*req
;
2913 if (!(output
= mem_alloc( count
* sizeof(*output
) )))
2915 memset( output
, 0, count
* sizeof(*output
) );
2917 if (!(req
= mem_alloc( offsetof( struct poll_req
, sockets
[count
] ) )))
2923 req
->timeout
= NULL
;
2924 if (timeout
&& timeout
!= TIMEOUT_INFINITE
&&
2925 !(req
->timeout
= add_timeout_user( timeout
, async_poll_timeout
, req
)))
2932 for (i
= 0; i
< count
; ++i
)
2934 req
->sockets
[i
].sock
= (struct sock
*)get_handle_obj( current
->process
, input
[i
].socket
, 0, &sock_ops
);
2935 if (!req
->sockets
[i
].sock
)
2937 for (j
= 0; j
< i
; ++j
) release_object( req
->sockets
[i
].sock
);
2938 if (req
->timeout
) remove_timeout_user( req
->timeout
);
2943 req
->sockets
[i
].flags
= input
[i
].flags
;
2946 req
->exclusive
= exclusive
;
2948 req
->async
= (struct async
*)grab_object( async
);
2949 req
->iosb
= async_get_iosb( async
);
2950 req
->output
= output
;
2952 handle_exclusive_poll(req
);
2954 list_add_tail( &poll_list
, &req
->entry
);
2955 async_set_completion_callback( async
, free_poll_req
, req
);
2956 queue_async( &poll_sock
->poll_q
, async
);
2958 for (i
= 0; i
< count
; ++i
)
2960 struct sock
*sock
= req
->sockets
[i
].sock
;
2961 int mask
= req
->sockets
[i
].flags
;
2962 int flags
= poll_single_socket( sock
, mask
);
2967 output
[i
].flags
= flags
;
2968 output
[i
].status
= sock_get_ntstatus( sock_error( sock
->fd
) );
2971 /* FIXME: do other error conditions deserve a similar treatment? */
2972 if (sock
->state
!= SOCK_CONNECTING
&& sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] && (mask
& AFD_POLL_CONNECT_ERR
))
2975 output
[i
].flags
|= AFD_POLL_CONNECT_ERR
;
2976 output
[i
].status
= sock_get_ntstatus( sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] );
2980 if (!timeout
|| signaled
)
2981 complete_async_poll( req
, STATUS_SUCCESS
);
2983 for (i
= 0; i
< req
->count
; ++i
)
2984 sock_reselect( req
->sockets
[i
].sock
);
2985 set_error( STATUS_PENDING
);
2988 #ifdef HAVE_LINUX_RTNETLINK_H
2990 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
2991 static struct object
*ifchange_object
;
2993 static void ifchange_dump( struct object
*obj
, int verbose
);
2994 static struct fd
*ifchange_get_fd( struct object
*obj
);
2995 static void ifchange_destroy( struct object
*obj
);
2997 static int ifchange_get_poll_events( struct fd
*fd
);
2998 static void ifchange_poll_event( struct fd
*fd
, int event
);
3002 struct object obj
; /* object header */
3003 struct fd
*fd
; /* interface change file descriptor */
3004 struct list sockets
; /* list of sockets to send interface change notifications */
3007 static const struct object_ops ifchange_ops
=
3009 sizeof(struct ifchange
), /* size */
3010 &no_type
, /* type */
3011 ifchange_dump
, /* dump */
3012 no_add_queue
, /* add_queue */
3013 NULL
, /* remove_queue */
3014 NULL
, /* signaled */
3015 no_satisfied
, /* satisfied */
3016 no_signal
, /* signal */
3017 ifchange_get_fd
, /* get_fd */
3018 default_map_access
, /* map_access */
3019 default_get_sd
, /* get_sd */
3020 default_set_sd
, /* set_sd */
3021 no_get_full_name
, /* get_full_name */
3022 no_lookup_name
, /* lookup_name */
3023 no_link_name
, /* link_name */
3024 NULL
, /* unlink_name */
3025 no_open_file
, /* open_file */
3026 no_kernel_obj_list
, /* get_kernel_obj_list */
3027 no_close_handle
, /* close_handle */
3028 ifchange_destroy
/* destroy */
3031 static const struct fd_ops ifchange_fd_ops
=
3033 ifchange_get_poll_events
, /* get_poll_events */
3034 ifchange_poll_event
, /* poll_event */
3035 NULL
, /* get_fd_type */
3036 no_fd_read
, /* read */
3037 no_fd_write
, /* write */
3038 no_fd_flush
, /* flush */
3039 no_fd_get_file_info
, /* get_file_info */
3040 no_fd_get_volume_info
, /* get_volume_info */
3041 no_fd_ioctl
, /* ioctl */
3042 NULL
, /* cancel_async */
3043 NULL
, /* queue_async */
3044 NULL
/* reselect_async */
3047 static void ifchange_dump( struct object
*obj
, int verbose
)
3049 assert( obj
->ops
== &ifchange_ops
);
3050 fprintf( stderr
, "Interface change\n" );
3053 static struct fd
*ifchange_get_fd( struct object
*obj
)
3055 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3056 return (struct fd
*)grab_object( ifchange
->fd
);
3059 static void ifchange_destroy( struct object
*obj
)
3061 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3062 assert( obj
->ops
== &ifchange_ops
);
3064 release_object( ifchange
->fd
);
3066 /* reset the global ifchange object so that it will be recreated if it is needed again */
3067 assert( obj
== ifchange_object
);
3068 ifchange_object
= NULL
;
3071 static int ifchange_get_poll_events( struct fd
*fd
)
3076 /* wake up all the sockets waiting for a change notification event */
3077 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
3079 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3080 struct list
*ptr
, *next
;
3081 assert( obj
->ops
== &ifchange_ops
);
3082 assert( obj
== ifchange_object
);
3084 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
3086 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
3088 assert( sock
->ifchange_obj
);
3089 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
3090 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
3094 static void ifchange_poll_event( struct fd
*fd
, int event
)
3096 struct object
*ifchange
= get_fd_user( fd
);
3097 unsigned int status
= STATUS_PENDING
;
3098 char buffer
[PIPE_BUF
];
3101 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
3104 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
3105 return; /* retry when poll() says the socket is ready */
3106 status
= sock_get_ntstatus( errno
);
3110 struct nlmsghdr
*nlh
;
3112 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
3114 if (nlh
->nlmsg_type
== NLMSG_DONE
)
3116 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
3117 status
= STATUS_SUCCESS
;
3120 else status
= STATUS_CANCELLED
;
3122 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
3127 /* we only need one of these interface notification objects, all of the sockets dependent upon
3128 * it will wake up when a notification event occurs */
3129 static struct object
*get_ifchange( void )
3131 #ifdef HAVE_LINUX_RTNETLINK_H
3132 struct ifchange
*ifchange
;
3133 struct sockaddr_nl addr
;
3136 if (ifchange_object
)
3138 /* increment the refcount for each socket that uses the ifchange object */
3139 return grab_object( ifchange_object
);
3142 /* create the socket we need for processing interface change notifications */
3143 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
3146 set_error( sock_get_ntstatus( errno
));
3149 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
3150 memset( &addr
, 0, sizeof(addr
) );
3151 addr
.nl_family
= AF_NETLINK
;
3152 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
3153 /* bind the socket to the special netlink kernel interface */
3154 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
3157 set_error( sock_get_ntstatus( errno
));
3160 if (!(ifchange
= alloc_object( &ifchange_ops
)))
3163 set_error( STATUS_NO_MEMORY
);
3166 list_init( &ifchange
->sockets
);
3167 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
3169 release_object( ifchange
);
3170 set_error( STATUS_NO_MEMORY
);
3173 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
3175 /* the ifchange object is now successfully configured */
3176 ifchange_object
= &ifchange
->obj
;
3177 return &ifchange
->obj
;
3179 set_error( STATUS_NOT_SUPPORTED
);
3184 /* add the socket to the interface change notification list */
3185 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
3187 #ifdef HAVE_LINUX_RTNETLINK_H
3188 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3190 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
3194 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3195 static struct object
*sock_get_ifchange( struct sock
*sock
)
3197 struct object
*ifchange
;
3199 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
3200 return sock
->ifchange_obj
;
3202 if (!(ifchange
= get_ifchange()))
3205 /* add the socket to the ifchange notification list */
3206 ifchange_add_sock( ifchange
, sock
);
3207 sock
->ifchange_obj
= ifchange
;
3211 /* destroy an existing ifchange queue for a specific socket */
3212 static void sock_release_ifchange( struct sock
*sock
)
3214 if (sock
->ifchange_obj
)
3216 list_remove( &sock
->ifchange_entry
);
3217 release_object( sock
->ifchange_obj
);
3218 sock
->ifchange_obj
= NULL
;
3222 static void socket_device_dump( struct object
*obj
, int verbose
);
3223 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3224 unsigned int attr
, struct object
*root
);
3225 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3226 unsigned int sharing
, unsigned int options
);
3228 static const struct object_ops socket_device_ops
=
3230 sizeof(struct object
), /* size */
3231 &device_type
, /* type */
3232 socket_device_dump
, /* dump */
3233 no_add_queue
, /* add_queue */
3234 NULL
, /* remove_queue */
3235 NULL
, /* signaled */
3236 no_satisfied
, /* satisfied */
3237 no_signal
, /* signal */
3238 no_get_fd
, /* get_fd */
3239 default_map_access
, /* map_access */
3240 default_get_sd
, /* get_sd */
3241 default_set_sd
, /* set_sd */
3242 default_get_full_name
, /* get_full_name */
3243 socket_device_lookup_name
, /* lookup_name */
3244 directory_link_name
, /* link_name */
3245 default_unlink_name
, /* unlink_name */
3246 socket_device_open_file
, /* open_file */
3247 no_kernel_obj_list
, /* get_kernel_obj_list */
3248 no_close_handle
, /* close_handle */
3249 no_destroy
/* destroy */
3252 static void socket_device_dump( struct object
*obj
, int verbose
)
3254 fputs( "Socket device\n", stderr
);
3257 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3258 unsigned int attr
, struct object
*root
)
3260 if (name
) name
->len
= 0;
3264 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3265 unsigned int sharing
, unsigned int options
)
3269 if (!(sock
= create_socket())) return NULL
;
3270 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
3272 release_object( sock
);
3278 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
3279 unsigned int attr
, const struct security_descriptor
*sd
)
3281 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
3284 DECL_HANDLER(recv_socket
)
3286 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3287 unsigned int status
= req
->status
;
3288 timeout_t timeout
= 0;
3289 struct async
*async
;
3295 /* recv() returned EWOULDBLOCK, i.e. no data available yet */
3296 if (status
== STATUS_DEVICE_NOT_READY
&& !sock
->nonblocking
)
3298 /* Set a timeout on the async if necessary.
3300 * We want to do this *only* if the client gave us STATUS_DEVICE_NOT_READY.
3301 * If the client gave us STATUS_PENDING, it expects the async to always
3302 * block (it was triggered by WSARecv*() with a valid OVERLAPPED
3303 * structure) and for the timeout not to be respected. */
3304 if (is_fd_overlapped( fd
))
3305 timeout
= (timeout_t
)sock
->rcvtimeo
* -10000;
3307 status
= STATUS_PENDING
;
3310 if ((status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
) && sock
->rd_shutdown
)
3311 status
= STATUS_PIPE_DISCONNECTED
;
3313 sock
->pending_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3314 sock
->reported_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3316 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3318 if (status
== STATUS_SUCCESS
)
3320 struct iosb
*iosb
= async_get_iosb( async
);
3321 iosb
->result
= req
->total
;
3322 release_object( iosb
);
3324 set_error( status
);
3327 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3329 if (status
== STATUS_PENDING
)
3330 queue_async( &sock
->read_q
, async
);
3332 /* always reselect; we changed reported_events above */
3333 sock_reselect( sock
);
3335 reply
->wait
= async_handoff( async
, NULL
, 0 );
3336 reply
->options
= get_fd_options( fd
);
3337 release_object( async
);
3339 release_object( sock
);
3342 DECL_HANDLER(poll_socket
)
3344 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3345 const struct poll_socket_input
*input
= get_req_data();
3346 struct async
*async
;
3351 count
= get_req_data_size() / sizeof(*input
);
3353 if ((async
= create_request_async( sock
->fd
, get_fd_comp_flags( sock
->fd
), &req
->async
)))
3355 poll_socket( sock
, async
, req
->exclusive
, req
->timeout
, count
, input
);
3356 reply
->wait
= async_handoff( async
, NULL
, 0 );
3357 reply
->options
= get_fd_options( sock
->fd
);
3358 release_object( async
);
3361 release_object( sock
);
3364 DECL_HANDLER(send_socket
)
3366 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3367 unsigned int status
= req
->status
;
3368 timeout_t timeout
= 0;
3369 struct async
*async
;
3375 if (sock
->type
== WS_SOCK_DGRAM
)
3377 /* sendto() and sendmsg() implicitly binds a socket */
3378 union unix_sockaddr unix_addr
;
3379 socklen_t unix_len
= sizeof(unix_addr
);
3381 if (!sock
->bound
&& !getsockname( get_unix_fd( fd
), &unix_addr
.addr
, &unix_len
))
3382 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
3386 if (status
!= STATUS_SUCCESS
)
3388 /* send() calls only clear and reselect events if unsuccessful. */
3389 sock
->pending_events
&= ~AFD_POLL_WRITE
;
3390 sock
->reported_events
&= ~AFD_POLL_WRITE
;
3393 /* If we had a short write and the socket is nonblocking (and the client is
3394 * not trying to force the operation to be asynchronous), return success.
3395 * Windows actually refuses to send any data in this case, and returns
3396 * EWOULDBLOCK, but we have no way of doing that. */
3397 if (status
== STATUS_DEVICE_NOT_READY
&& req
->total
&& sock
->nonblocking
)
3398 status
= STATUS_SUCCESS
;
3400 /* send() returned EWOULDBLOCK or a short write, i.e. cannot send all data yet */
3401 if (status
== STATUS_DEVICE_NOT_READY
&& !sock
->nonblocking
)
3403 /* Set a timeout on the async if necessary.
3405 * We want to do this *only* if the client gave us STATUS_DEVICE_NOT_READY.
3406 * If the client gave us STATUS_PENDING, it expects the async to always
3407 * block (it was triggered by WSASend*() with a valid OVERLAPPED
3408 * structure) and for the timeout not to be respected. */
3409 if (is_fd_overlapped( fd
))
3410 timeout
= (timeout_t
)sock
->sndtimeo
* -10000;
3412 status
= STATUS_PENDING
;
3415 if ((status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
) && sock
->wr_shutdown
)
3416 status
= STATUS_PIPE_DISCONNECTED
;
3418 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3420 if (status
== STATUS_SUCCESS
)
3422 struct iosb
*iosb
= async_get_iosb( async
);
3423 iosb
->result
= req
->total
;
3424 release_object( iosb
);
3426 set_error( status
);
3429 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3431 if (status
== STATUS_PENDING
)
3432 queue_async( &sock
->write_q
, async
);
3434 /* always reselect; we changed reported_events above */
3435 sock_reselect( sock
);
3437 reply
->wait
= async_handoff( async
, NULL
, 0 );
3438 reply
->options
= get_fd_options( fd
);
3439 release_object( async
);
3441 release_object( sock
);