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
;
132 struct poll_socket_output
*output
;
145 struct sock
*sock
, *acceptsock
;
147 unsigned int recv_len
, local_len
;
155 unsigned int addr_len
, send_len
, send_cursor
;
158 enum connection_state
169 struct object obj
; /* object header */
170 struct fd
*fd
; /* socket file descriptor */
171 enum connection_state state
; /* connection state */
172 unsigned int mask
; /* event mask */
173 /* pending AFD_POLL_* events which have not yet been reported to the application */
174 unsigned int pending_events
;
175 /* AFD_POLL_* events which have already been reported and should not be
176 * selected for again until reset by a relevant call.
178 * For example, if AFD_POLL_READ is set here and not in pending_events, it
179 * has already been reported and consumed, and we should not report it
180 * again, even if POLLIN is signaled, until it is reset by e.g recv().
182 * If an event has been signaled and not consumed yet, it will be set in
183 * both pending_events and reported_events (as we should only ever report
184 * any event once until it is reset.) */
185 unsigned int reported_events
;
186 unsigned int flags
; /* socket flags */
187 unsigned short proto
; /* socket protocol */
188 unsigned short type
; /* socket type */
189 unsigned short family
; /* socket family */
190 struct event
*event
; /* event object */
191 user_handle_t window
; /* window to send the message to */
192 unsigned int message
; /* message to send */
193 obj_handle_t wparam
; /* message wparam (socket handle) */
194 int errors
[AFD_POLL_BIT_COUNT
]; /* event errors */
195 timeout_t connect_time
;/* time the socket was connected */
196 struct sock
*deferred
; /* socket that waits for a deferred accept */
197 struct async_queue read_q
; /* queue for asynchronous reads */
198 struct async_queue write_q
; /* queue for asynchronous writes */
199 struct async_queue ifchange_q
; /* queue for interface change notifications */
200 struct async_queue accept_q
; /* queue for asynchronous accepts */
201 struct async_queue connect_q
; /* queue for asynchronous connects */
202 struct async_queue poll_q
; /* queue for asynchronous polls */
203 struct object
*ifchange_obj
; /* the interface change notification object */
204 struct list ifchange_entry
; /* entry in ifchange notification list */
205 struct list accept_list
; /* list of pending accept requests */
206 struct accept_req
*accept_recv_req
; /* pending accept-into request which will recv on this socket */
207 struct connect_req
*connect_req
; /* pending connection request */
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 int sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
234 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
);
235 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
237 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
);
238 static struct sock
*accept_socket( struct sock
*sock
);
239 static int sock_get_ntstatus( int err
);
240 static unsigned int sock_get_error( int err
);
242 static const struct object_ops sock_ops
=
244 sizeof(struct sock
), /* size */
245 &file_type
, /* type */
246 sock_dump
, /* dump */
247 add_queue
, /* add_queue */
248 remove_queue
, /* remove_queue */
249 default_fd_signaled
, /* signaled */
250 no_satisfied
, /* satisfied */
251 no_signal
, /* signal */
252 sock_get_fd
, /* get_fd */
253 default_map_access
, /* map_access */
254 default_get_sd
, /* get_sd */
255 default_set_sd
, /* set_sd */
256 no_get_full_name
, /* get_full_name */
257 no_lookup_name
, /* lookup_name */
258 no_link_name
, /* link_name */
259 NULL
, /* unlink_name */
260 no_open_file
, /* open_file */
261 no_kernel_obj_list
, /* get_kernel_obj_list */
262 sock_close_handle
, /* close_handle */
263 sock_destroy
/* destroy */
266 static const struct fd_ops sock_fd_ops
=
268 sock_get_poll_events
, /* get_poll_events */
269 sock_poll_event
, /* poll_event */
270 sock_get_fd_type
, /* get_fd_type */
271 no_fd_read
, /* read */
272 no_fd_write
, /* write */
273 no_fd_flush
, /* flush */
274 default_fd_get_file_info
, /* get_file_info */
275 no_fd_get_volume_info
, /* get_volume_info */
276 sock_ioctl
, /* ioctl */
277 sock_queue_async
, /* queue_async */
278 sock_reselect_async
/* reselect_async */
283 struct sockaddr addr
;
284 struct sockaddr_in in
;
285 struct sockaddr_in6 in6
;
287 struct sockaddr_ipx ipx
;
290 struct sockaddr_irda irda
;
294 static int sockaddr_from_unix( const union unix_sockaddr
*uaddr
, struct WS_sockaddr
*wsaddr
, socklen_t wsaddrlen
)
296 memset( wsaddr
, 0, wsaddrlen
);
298 switch (uaddr
->addr
.sa_family
)
302 struct WS_sockaddr_in win
= {0};
304 if (wsaddrlen
< sizeof(win
)) return -1;
305 win
.sin_family
= WS_AF_INET
;
306 win
.sin_port
= uaddr
->in
.sin_port
;
307 memcpy( &win
.sin_addr
, &uaddr
->in
.sin_addr
, sizeof(win
.sin_addr
) );
308 memcpy( wsaddr
, &win
, sizeof(win
) );
314 struct WS_sockaddr_in6 win
= {0};
316 if (wsaddrlen
< sizeof(win
)) return -1;
317 win
.sin6_family
= WS_AF_INET6
;
318 win
.sin6_port
= uaddr
->in6
.sin6_port
;
319 win
.sin6_flowinfo
= uaddr
->in6
.sin6_flowinfo
;
320 memcpy( &win
.sin6_addr
, &uaddr
->in6
.sin6_addr
, sizeof(win
.sin6_addr
) );
321 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
322 win
.sin6_scope_id
= uaddr
->in6
.sin6_scope_id
;
324 memcpy( wsaddr
, &win
, sizeof(win
) );
331 struct WS_sockaddr_ipx win
= {0};
333 if (wsaddrlen
< sizeof(win
)) return -1;
334 win
.sa_family
= WS_AF_IPX
;
335 memcpy( win
.sa_netnum
, &uaddr
->ipx
.sipx_network
, sizeof(win
.sa_netnum
) );
336 memcpy( win
.sa_nodenum
, &uaddr
->ipx
.sipx_node
, sizeof(win
.sa_nodenum
) );
337 win
.sa_socket
= uaddr
->ipx
.sipx_port
;
338 memcpy( wsaddr
, &win
, sizeof(win
) );
348 if (wsaddrlen
< sizeof(win
)) return -1;
349 win
.irdaAddressFamily
= WS_AF_IRDA
;
350 memcpy( win
.irdaDeviceID
, &uaddr
->irda
.sir_addr
, sizeof(win
.irdaDeviceID
) );
351 if (uaddr
->irda
.sir_lsap_sel
!= LSAP_ANY
)
352 snprintf( win
.irdaServiceName
, sizeof(win
.irdaServiceName
), "LSAP-SEL%u", uaddr
->irda
.sir_lsap_sel
);
354 memcpy( win
.irdaServiceName
, uaddr
->irda
.sir_name
, sizeof(win
.irdaServiceName
) );
355 memcpy( wsaddr
, &win
, sizeof(win
) );
369 static socklen_t
sockaddr_to_unix( const struct WS_sockaddr
*wsaddr
, int wsaddrlen
, union unix_sockaddr
*uaddr
)
371 memset( uaddr
, 0, sizeof(*uaddr
) );
373 switch (wsaddr
->sa_family
)
377 struct WS_sockaddr_in win
= {0};
379 if (wsaddrlen
< sizeof(win
)) return 0;
380 memcpy( &win
, wsaddr
, sizeof(win
) );
381 uaddr
->in
.sin_family
= AF_INET
;
382 uaddr
->in
.sin_port
= win
.sin_port
;
383 memcpy( &uaddr
->in
.sin_addr
, &win
.sin_addr
, sizeof(win
.sin_addr
) );
384 return sizeof(uaddr
->in
);
389 struct WS_sockaddr_in6 win
= {0};
391 if (wsaddrlen
< sizeof(win
)) return 0;
392 memcpy( &win
, wsaddr
, sizeof(win
) );
393 uaddr
->in6
.sin6_family
= AF_INET6
;
394 uaddr
->in6
.sin6_port
= win
.sin6_port
;
395 uaddr
->in6
.sin6_flowinfo
= win
.sin6_flowinfo
;
396 memcpy( &uaddr
->in6
.sin6_addr
, &win
.sin6_addr
, sizeof(win
.sin6_addr
) );
397 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
398 uaddr
->in6
.sin6_scope_id
= win
.sin6_scope_id
;
400 return sizeof(uaddr
->in6
);
406 struct WS_sockaddr_ipx win
= {0};
408 if (wsaddrlen
< sizeof(win
)) return 0;
409 memcpy( &win
, wsaddr
, sizeof(win
) );
410 uaddr
->ipx
.sipx_family
= AF_IPX
;
411 memcpy( &uaddr
->ipx
.sipx_network
, win
.sa_netnum
, sizeof(win
.sa_netnum
) );
412 memcpy( &uaddr
->ipx
.sipx_node
, win
.sa_nodenum
, sizeof(win
.sa_nodenum
) );
413 uaddr
->ipx
.sipx_port
= win
.sa_socket
;
414 return sizeof(uaddr
->ipx
);
421 SOCKADDR_IRDA win
= {0};
422 unsigned int lsap_sel
;
424 if (wsaddrlen
< sizeof(win
)) return 0;
425 memcpy( &win
, wsaddr
, sizeof(win
) );
426 uaddr
->irda
.sir_family
= AF_IRDA
;
427 if (sscanf( win
.irdaServiceName
, "LSAP-SEL%u", &lsap_sel
) == 1)
428 uaddr
->irda
.sir_lsap_sel
= lsap_sel
;
431 uaddr
->irda
.sir_lsap_sel
= LSAP_ANY
;
432 memcpy( uaddr
->irda
.sir_name
, win
.irdaServiceName
, sizeof(win
.irdaServiceName
) );
434 memcpy( &uaddr
->irda
.sir_addr
, win
.irdaDeviceID
, sizeof(win
.irdaDeviceID
) );
435 return sizeof(uaddr
->irda
);
442 default: /* likely an ipv4 address */
443 case sizeof(struct WS_sockaddr_in
):
444 return sizeof(uaddr
->in
);
447 case sizeof(struct WS_sockaddr_ipx
):
448 return sizeof(uaddr
->ipx
);
452 case sizeof(SOCKADDR_IRDA
):
453 return sizeof(uaddr
->irda
);
456 case sizeof(struct WS_sockaddr_in6
):
457 return sizeof(uaddr
->in6
);
465 /* some events are generated at the same time but must be sent in a particular
466 * order (e.g. CONNECT must be sent before READ) */
467 static const enum afd_poll_bit event_bitorder
[] =
469 AFD_POLL_BIT_CONNECT
,
470 AFD_POLL_BIT_CONNECT_ERR
,
481 SOCK_SHUTDOWN_ERROR
= -1,
482 SOCK_SHUTDOWN_EOF
= 0,
483 SOCK_SHUTDOWN_POLLHUP
= 1
486 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
488 static sock_shutdown_t
sock_check_pollhup(void)
490 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
495 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
496 if ( shutdown( fd
[0], 1 ) ) goto out
;
502 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
503 n
= poll( &pfd
, 1, 1 );
504 if ( n
!= 1 ) goto out
; /* error or timeout */
505 if ( pfd
.revents
& POLLHUP
)
506 ret
= SOCK_SHUTDOWN_POLLHUP
;
507 else if ( pfd
.revents
& POLLIN
&&
508 read( fd
[1], &dummy
, 1 ) == 0 )
509 ret
= SOCK_SHUTDOWN_EOF
;
519 sock_shutdown_type
= sock_check_pollhup();
521 switch ( sock_shutdown_type
)
523 case SOCK_SHUTDOWN_EOF
:
524 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
526 case SOCK_SHUTDOWN_POLLHUP
:
527 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
530 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
531 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
535 static int sock_reselect( struct sock
*sock
)
537 int ev
= sock_get_poll_events( sock
->fd
);
540 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
542 set_fd_events( sock
->fd
, ev
);
546 static unsigned int afd_poll_flag_to_win32( unsigned int flags
)
548 static const unsigned int map
[] =
552 FD_WRITE
, /* WRITE */
554 FD_CLOSE
, /* RESET */
556 FD_CONNECT
, /* CONNECT */
557 FD_ACCEPT
, /* ACCEPT */
558 FD_CONNECT
, /* CONNECT_ERR */
561 unsigned int i
, ret
= 0;
563 for (i
= 0; i
< ARRAY_SIZE(map
); ++i
)
565 if (flags
& (1 << i
)) ret
|= map
[i
];
571 /* wake anybody waiting on the socket event or send the associated message */
572 static void sock_wake_up( struct sock
*sock
)
574 unsigned int events
= sock
->pending_events
& sock
->mask
;
579 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
581 set_event( sock
->event
);
585 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
586 for (i
= 0; i
< ARRAY_SIZE(event_bitorder
); i
++)
588 enum afd_poll_bit event
= event_bitorder
[i
];
589 if (events
& (1 << event
))
591 lparam_t lparam
= afd_poll_flag_to_win32(1 << event
) | (sock_get_error( sock
->errors
[event
] ) << 16);
592 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
595 sock
->pending_events
= 0;
596 sock_reselect( sock
);
600 static inline int sock_error( struct fd
*fd
)
602 unsigned int optval
= 0;
603 socklen_t optlen
= sizeof(optval
);
605 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
609 static void free_accept_req( void *private )
611 struct accept_req
*req
= private;
612 list_remove( &req
->entry
);
615 req
->acceptsock
->accept_recv_req
= NULL
;
616 release_object( req
->acceptsock
);
618 release_object( req
->async
);
619 release_object( req
->iosb
);
620 release_object( req
->sock
);
624 static void fill_accept_output( struct accept_req
*req
)
626 struct iosb
*iosb
= req
->iosb
;
627 union unix_sockaddr unix_addr
;
628 struct WS_sockaddr
*win_addr
;
629 unsigned int remote_len
;
635 if (!(out_data
= mem_alloc( iosb
->out_size
))) return;
637 fd
= get_unix_fd( req
->acceptsock
->fd
);
639 if (req
->recv_len
&& (size
= recv( fd
, out_data
, req
->recv_len
, 0 )) < 0)
641 if (!req
->accepted
&& errno
== EWOULDBLOCK
)
644 sock_reselect( req
->acceptsock
);
645 set_error( STATUS_PENDING
);
649 set_error( sock_get_ntstatus( errno
) );
656 if (req
->local_len
< sizeof(int))
658 set_error( STATUS_BUFFER_TOO_SMALL
);
663 unix_len
= sizeof(unix_addr
);
664 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ sizeof(int));
665 if (getsockname( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
666 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, req
->local_len
- sizeof(int) )) < 0)
668 set_error( sock_get_ntstatus( errno
) );
672 memcpy( out_data
+ req
->recv_len
, &win_len
, sizeof(int) );
675 unix_len
= sizeof(unix_addr
);
676 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ req
->local_len
+ sizeof(int));
677 remote_len
= iosb
->out_size
- req
->recv_len
- req
->local_len
;
678 if (getpeername( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
679 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, remote_len
- sizeof(int) )) < 0)
681 set_error( sock_get_ntstatus( errno
) );
685 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
687 iosb
->status
= STATUS_SUCCESS
;
689 iosb
->out_data
= out_data
;
690 set_error( STATUS_ALERTED
);
693 static void complete_async_accept( struct sock
*sock
, struct accept_req
*req
)
695 struct sock
*acceptsock
= req
->acceptsock
;
696 struct async
*async
= req
->async
;
698 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
702 if (!accept_into_socket( sock
, acceptsock
)) return;
703 fill_accept_output( req
);
707 struct iosb
*iosb
= req
->iosb
;
710 if (!(acceptsock
= accept_socket( sock
))) return;
711 handle
= alloc_handle_no_access_check( async_get_thread( async
)->process
, &acceptsock
->obj
,
712 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
713 acceptsock
->wparam
= handle
;
714 release_object( acceptsock
);
717 if (!(iosb
->out_data
= malloc( sizeof(handle
) ))) return;
719 iosb
->status
= STATUS_SUCCESS
;
720 iosb
->out_size
= sizeof(handle
);
721 memcpy( iosb
->out_data
, &handle
, sizeof(handle
) );
722 set_error( STATUS_ALERTED
);
726 static void complete_async_accept_recv( struct accept_req
*req
)
728 if (debug_level
) fprintf( stderr
, "completing accept recv request for socket %p\n", req
->acceptsock
);
730 assert( req
->recv_len
);
732 fill_accept_output( req
);
735 static void free_connect_req( void *private )
737 struct connect_req
*req
= private;
739 req
->sock
->connect_req
= NULL
;
740 release_object( req
->async
);
741 release_object( req
->iosb
);
742 release_object( req
->sock
);
746 static void complete_async_connect( struct sock
*sock
)
748 struct connect_req
*req
= sock
->connect_req
;
749 const char *in_buffer
;
754 if (debug_level
) fprintf( stderr
, "completing connect request for socket %p\n", sock
);
756 sock
->state
= SOCK_CONNECTED
;
760 set_error( STATUS_SUCCESS
);
765 in_buffer
= (const char *)iosb
->in_data
+ sizeof(struct afd_connect_params
) + req
->addr_len
;
766 len
= req
->send_len
- req
->send_cursor
;
768 ret
= send( get_unix_fd( sock
->fd
), in_buffer
+ req
->send_cursor
, len
, 0 );
769 if (ret
< 0 && errno
!= EWOULDBLOCK
)
770 set_error( sock_get_ntstatus( errno
) );
773 iosb
->result
= req
->send_len
;
774 iosb
->status
= STATUS_SUCCESS
;
775 set_error( STATUS_ALERTED
);
779 req
->send_cursor
+= ret
;
780 set_error( STATUS_PENDING
);
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
);
799 static int is_oobinline( struct sock
*sock
)
802 socklen_t len
= sizeof(oobinline
);
803 return !getsockopt( get_unix_fd( sock
->fd
), SOL_SOCKET
, SO_OOBINLINE
, (char *)&oobinline
, &len
) && oobinline
;
806 static int get_poll_flags( struct sock
*sock
, int event
)
810 /* A connection-mode socket which has never been connected does not return
811 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
812 if (sock
->state
== SOCK_UNCONNECTED
)
813 event
&= ~(POLLOUT
| POLLHUP
);
817 if (sock
->state
== SOCK_LISTENING
)
818 flags
|= AFD_POLL_ACCEPT
;
820 flags
|= AFD_POLL_READ
;
823 flags
|= is_oobinline( sock
) ? AFD_POLL_READ
: AFD_POLL_OOB
;
825 flags
|= AFD_POLL_WRITE
;
826 if (sock
->state
== SOCK_CONNECTED
)
827 flags
|= AFD_POLL_CONNECT
;
829 flags
|= AFD_POLL_HUP
;
831 flags
|= AFD_POLL_CONNECT_ERR
;
836 static void complete_async_polls( struct sock
*sock
, int event
, int error
)
838 int flags
= get_poll_flags( sock
, event
);
839 struct poll_req
*req
, *next
;
841 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &poll_list
, struct poll_req
, entry
)
843 struct iosb
*iosb
= req
->iosb
;
846 if (iosb
->status
!= STATUS_PENDING
) continue;
848 for (i
= 0; i
< req
->count
; ++i
)
850 if (req
->sockets
[i
].sock
!= sock
) continue;
851 if (!(req
->sockets
[i
].flags
& flags
)) continue;
854 fprintf( stderr
, "completing poll for socket %p, wanted %#x got %#x\n",
855 sock
, req
->sockets
[i
].flags
, flags
);
857 req
->output
[i
].flags
= req
->sockets
[i
].flags
& flags
;
858 req
->output
[i
].status
= sock_get_ntstatus( error
);
860 iosb
->status
= STATUS_SUCCESS
;
861 iosb
->out_data
= req
->output
;
862 iosb
->out_size
= req
->count
* sizeof(*req
->output
);
863 async_terminate( req
->async
, STATUS_ALERTED
);
869 static void async_poll_timeout( void *private )
871 struct poll_req
*req
= private;
872 struct iosb
*iosb
= req
->iosb
;
876 if (iosb
->status
!= STATUS_PENDING
) return;
878 iosb
->status
= STATUS_TIMEOUT
;
879 iosb
->out_data
= req
->output
;
880 iosb
->out_size
= req
->count
* sizeof(*req
->output
);
881 async_terminate( req
->async
, STATUS_ALERTED
);
884 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
886 if (event
& (POLLIN
| POLLPRI
))
888 struct accept_req
*req
;
890 LIST_FOR_EACH_ENTRY( req
, &sock
->accept_list
, struct accept_req
, entry
)
892 if (req
->iosb
->status
== STATUS_PENDING
&& !req
->accepted
)
894 complete_async_accept( sock
, req
);
895 if (get_error() != STATUS_PENDING
)
896 async_terminate( req
->async
, get_error() );
901 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
903 complete_async_accept_recv( sock
->accept_recv_req
);
904 if (get_error() != STATUS_PENDING
)
905 async_terminate( sock
->accept_recv_req
->async
, get_error() );
909 if ((event
& POLLOUT
) && sock
->connect_req
&& sock
->connect_req
->iosb
->status
== STATUS_PENDING
)
911 complete_async_connect( sock
);
912 if (get_error() != STATUS_PENDING
)
913 async_terminate( sock
->connect_req
->async
, get_error() );
916 if (event
& (POLLIN
| POLLPRI
) && async_waiting( &sock
->read_q
))
918 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
919 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
920 event
&= ~(POLLIN
| POLLPRI
);
923 if (event
& POLLOUT
&& async_waiting( &sock
->write_q
))
925 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
926 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
930 if (event
& (POLLERR
| POLLHUP
))
932 int status
= sock_get_ntstatus( error
);
933 struct accept_req
*req
, *next
;
935 if (sock
->rd_shutdown
|| sock
->hangup
)
936 async_wake_up( &sock
->read_q
, status
);
937 if (sock
->wr_shutdown
)
938 async_wake_up( &sock
->write_q
, status
);
940 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
942 if (req
->iosb
->status
== STATUS_PENDING
)
943 async_terminate( req
->async
, status
);
946 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
947 async_terminate( sock
->accept_recv_req
->async
, status
);
949 if (sock
->connect_req
)
950 async_terminate( sock
->connect_req
->async
, status
);
956 static void post_socket_event( struct sock
*sock
, enum afd_poll_bit event_bit
, int error
)
958 unsigned int event
= (1 << event_bit
);
960 if (!(sock
->reported_events
& event
))
962 sock
->pending_events
|= event
;
963 sock
->reported_events
|= event
;
964 sock
->errors
[event_bit
] = error
;
968 static void sock_dispatch_events( struct sock
*sock
, enum connection_state prevstate
, int event
, int error
)
972 case SOCK_UNCONNECTED
:
975 case SOCK_CONNECTING
:
978 post_socket_event( sock
, AFD_POLL_BIT_CONNECT
, 0 );
979 sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] = 0;
981 if (event
& (POLLERR
| POLLHUP
))
982 post_socket_event( sock
, AFD_POLL_BIT_CONNECT_ERR
, error
);
986 if (event
& (POLLIN
| POLLERR
| POLLHUP
))
987 post_socket_event( sock
, AFD_POLL_BIT_ACCEPT
, error
);
991 case SOCK_CONNECTIONLESS
:
993 post_socket_event( sock
, AFD_POLL_BIT_READ
, 0 );
996 post_socket_event( sock
, AFD_POLL_BIT_WRITE
, 0 );
999 post_socket_event( sock
, AFD_POLL_BIT_OOB
, 0 );
1001 if (event
& (POLLERR
| POLLHUP
))
1002 post_socket_event( sock
, AFD_POLL_BIT_HUP
, error
);
1006 sock_wake_up( sock
);
1009 static void sock_poll_event( struct fd
*fd
, int event
)
1011 struct sock
*sock
= get_fd_user( fd
);
1012 int hangup_seen
= 0;
1013 enum connection_state prevstate
= sock
->state
;
1016 assert( sock
->obj
.ops
== &sock_ops
);
1018 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
1020 /* we may change event later, remove from loop here */
1021 if (event
& (POLLERR
|POLLHUP
)) set_fd_events( sock
->fd
, -1 );
1023 switch (sock
->state
)
1025 case SOCK_UNCONNECTED
:
1028 case SOCK_CONNECTING
:
1029 if (event
& (POLLERR
|POLLHUP
))
1031 sock
->state
= SOCK_UNCONNECTED
;
1033 error
= sock_error( fd
);
1035 else if (event
& POLLOUT
)
1037 sock
->state
= SOCK_CONNECTED
;
1038 sock
->connect_time
= current_time
;
1042 case SOCK_LISTENING
:
1043 if (event
& (POLLERR
|POLLHUP
))
1044 error
= sock_error( fd
);
1047 case SOCK_CONNECTED
:
1048 case SOCK_CONNECTIONLESS
:
1049 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
1054 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
1055 * has been closed, so we need to check for it explicitly here */
1056 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
1065 /* EAGAIN can happen if an async recv() falls between the server's poll()
1066 call and the invocation of this routine */
1067 if ( errno
!= EAGAIN
)
1072 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
1077 if (hangup_seen
|| (sock_shutdown_type
== SOCK_SHUTDOWN_POLLHUP
&& (event
& POLLHUP
)))
1081 else if (event
& (POLLHUP
| POLLERR
))
1086 fprintf( stderr
, "socket %p aborted by error %d, event %#x\n", sock
, error
, event
);
1094 complete_async_polls( sock
, event
, error
);
1096 event
= sock_dispatch_asyncs( sock
, event
, error
);
1097 sock_dispatch_events( sock
, prevstate
, event
, error
);
1099 sock_reselect( sock
);
1102 static void sock_dump( struct object
*obj
, int verbose
)
1104 struct sock
*sock
= (struct sock
*)obj
;
1105 assert( obj
->ops
== &sock_ops
);
1106 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
1107 sock
->fd
, sock
->state
,
1108 sock
->mask
, sock
->pending_events
, sock
->reported_events
);
1111 static int poll_flags_from_afd( struct sock
*sock
, int flags
)
1115 /* A connection-mode socket which has never been connected does
1116 * not return write or hangup events, but Linux returns
1117 * POLLOUT | POLLHUP. */
1118 if (sock
->state
== SOCK_UNCONNECTED
)
1121 if (flags
& (AFD_POLL_READ
| AFD_POLL_ACCEPT
))
1123 if ((flags
& AFD_POLL_HUP
) && sock
->type
== WS_SOCK_STREAM
)
1125 if (flags
& AFD_POLL_OOB
)
1126 ev
|= is_oobinline( sock
) ? POLLIN
: POLLPRI
;
1127 if (flags
& AFD_POLL_WRITE
)
1133 static int sock_get_poll_events( struct fd
*fd
)
1135 struct sock
*sock
= get_fd_user( fd
);
1136 unsigned int mask
= sock
->mask
& ~sock
->reported_events
;
1137 struct poll_req
*req
;
1140 assert( sock
->obj
.ops
== &sock_ops
);
1142 if (!sock
->type
) /* not initialized yet */
1145 switch (sock
->state
)
1147 case SOCK_UNCONNECTED
:
1148 /* A connection-mode Windows socket which has never been connected does
1149 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1150 * need to return -1 here, to prevent the socket from being polled on at
1154 case SOCK_CONNECTING
:
1157 case SOCK_LISTENING
:
1158 if (!list_empty( &sock
->accept_list
) || (mask
& AFD_POLL_ACCEPT
))
1162 case SOCK_CONNECTED
:
1163 case SOCK_CONNECTIONLESS
:
1164 if (sock
->hangup
&& sock
->wr_shutdown
&& !sock
->wr_shutdown_pending
)
1166 /* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
1167 * if both the socket and its peer are SHUT_WR.
1169 * We don't use SHUT_RD, so we can only encounter this in the latter
1170 * case. In that case there can't be any pending read requests (they
1171 * would have already been completed with a length of zero), the
1172 * above condition ensures that we don't have any pending write
1173 * requests, and nothing that can change about the socket state that
1174 * would complete a pending poll request. */
1181 if (sock
->accept_recv_req
)
1185 else if (async_queued( &sock
->read_q
))
1187 if (async_waiting( &sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
1191 /* Don't ask for POLLIN if we got a hangup. We won't receive more
1192 * data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
1195 if (mask
& AFD_POLL_READ
)
1197 if (mask
& AFD_POLL_OOB
)
1201 /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
1202 if (sock
->state
== SOCK_CONNECTED
&& (mask
& AFD_POLL_HUP
) && !(sock
->reported_events
& AFD_POLL_READ
))
1206 if (async_queued( &sock
->write_q
))
1208 if (async_waiting( &sock
->write_q
)) ev
|= POLLOUT
;
1210 else if (!sock
->wr_shutdown
&& (mask
& AFD_POLL_WRITE
))
1218 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1222 for (i
= 0; i
< req
->count
; ++i
)
1224 if (req
->sockets
[i
].sock
!= sock
) continue;
1226 ev
|= poll_flags_from_afd( sock
, req
->sockets
[i
].flags
);
1233 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
1235 return FD_TYPE_SOCKET
;
1238 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
1240 struct sock
*sock
= get_fd_user( fd
);
1241 struct async_queue
*queue
;
1243 assert( sock
->obj
.ops
== &sock_ops
);
1247 case ASYNC_TYPE_READ
:
1248 if (sock
->rd_shutdown
)
1250 set_error( STATUS_PIPE_DISCONNECTED
);
1253 queue
= &sock
->read_q
;
1256 case ASYNC_TYPE_WRITE
:
1257 if (sock
->wr_shutdown
)
1259 set_error( STATUS_PIPE_DISCONNECTED
);
1262 queue
= &sock
->write_q
;
1266 set_error( STATUS_INVALID_PARAMETER
);
1270 if (sock
->state
!= SOCK_CONNECTED
)
1272 set_error( STATUS_PIPE_DISCONNECTED
);
1276 queue_async( queue
, async
);
1277 sock_reselect( sock
);
1279 set_error( STATUS_PENDING
);
1282 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
1284 struct sock
*sock
= get_fd_user( fd
);
1286 if (sock
->wr_shutdown_pending
&& list_empty( &sock
->write_q
.queue
))
1288 shutdown( get_unix_fd( sock
->fd
), SHUT_WR
);
1289 sock
->wr_shutdown_pending
= 0;
1292 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1293 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1295 if (queue
!= &sock
->ifchange_q
&& sock
->type
)
1296 sock_reselect( sock
);
1299 static struct fd
*sock_get_fd( struct object
*obj
)
1301 struct sock
*sock
= (struct sock
*)obj
;
1302 return (struct fd
*)grab_object( sock
->fd
);
1305 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
1307 struct sock
*sock
= (struct sock
*)obj
;
1309 if (sock
->obj
.handle_count
== 1) /* last handle */
1311 struct accept_req
*accept_req
, *accept_next
;
1312 struct poll_req
*poll_req
, *poll_next
;
1314 if (sock
->accept_recv_req
)
1315 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
1317 LIST_FOR_EACH_ENTRY_SAFE( accept_req
, accept_next
, &sock
->accept_list
, struct accept_req
, entry
)
1318 async_terminate( accept_req
->async
, STATUS_CANCELLED
);
1320 if (sock
->connect_req
)
1321 async_terminate( sock
->connect_req
->async
, STATUS_CANCELLED
);
1323 LIST_FOR_EACH_ENTRY_SAFE( poll_req
, poll_next
, &poll_list
, struct poll_req
, entry
)
1325 struct iosb
*iosb
= poll_req
->iosb
;
1328 if (iosb
->status
!= STATUS_PENDING
) continue;
1330 for (i
= 0; i
< poll_req
->count
; ++i
)
1332 if (poll_req
->sockets
[i
].sock
== sock
)
1334 iosb
->status
= STATUS_SUCCESS
;
1335 poll_req
->output
[i
].flags
= AFD_POLL_CLOSE
;
1336 poll_req
->output
[i
].status
= 0;
1340 if (iosb
->status
!= STATUS_PENDING
)
1342 iosb
->out_data
= poll_req
->output
;
1343 iosb
->out_size
= poll_req
->count
* sizeof(*poll_req
->output
);
1344 async_terminate( poll_req
->async
, STATUS_ALERTED
);
1352 static void sock_destroy( struct object
*obj
)
1354 struct sock
*sock
= (struct sock
*)obj
;
1356 assert( obj
->ops
== &sock_ops
);
1358 /* FIXME: special socket shutdown stuff? */
1360 if ( sock
->deferred
)
1361 release_object( sock
->deferred
);
1363 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
1364 sock_release_ifchange( sock
);
1365 free_async_queue( &sock
->read_q
);
1366 free_async_queue( &sock
->write_q
);
1367 free_async_queue( &sock
->ifchange_q
);
1368 free_async_queue( &sock
->accept_q
);
1369 free_async_queue( &sock
->connect_q
);
1370 free_async_queue( &sock
->poll_q
);
1371 if (sock
->event
) release_object( sock
->event
);
1374 /* shut the socket down to force pending poll() calls in the client to return */
1375 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
1376 release_object( sock
->fd
);
1380 static struct sock
*create_socket(void)
1384 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
1386 sock
->state
= SOCK_UNCONNECTED
;
1388 sock
->pending_events
= 0;
1389 sock
->reported_events
= 0;
1398 sock
->connect_time
= 0;
1399 sock
->deferred
= NULL
;
1400 sock
->ifchange_obj
= NULL
;
1401 sock
->accept_recv_req
= NULL
;
1402 sock
->connect_req
= NULL
;
1403 memset( &sock
->addr
, 0, sizeof(sock
->addr
) );
1405 sock
->rd_shutdown
= 0;
1406 sock
->wr_shutdown
= 0;
1407 sock
->wr_shutdown_pending
= 0;
1410 sock
->nonblocking
= 0;
1416 init_async_queue( &sock
->read_q
);
1417 init_async_queue( &sock
->write_q
);
1418 init_async_queue( &sock
->ifchange_q
);
1419 init_async_queue( &sock
->accept_q
);
1420 init_async_queue( &sock
->connect_q
);
1421 init_async_queue( &sock
->poll_q
);
1422 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
1423 list_init( &sock
->accept_list
);
1427 static int get_unix_family( int family
)
1431 case WS_AF_INET
: return AF_INET
;
1432 case WS_AF_INET6
: return AF_INET6
;
1434 case WS_AF_IPX
: return AF_IPX
;
1437 case WS_AF_IRDA
: return AF_IRDA
;
1439 case WS_AF_UNSPEC
: return AF_UNSPEC
;
1444 static int get_unix_type( int type
)
1448 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
1449 case WS_SOCK_RAW
: return SOCK_RAW
;
1450 case WS_SOCK_STREAM
: return SOCK_STREAM
;
1455 static int get_unix_protocol( int protocol
)
1457 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1462 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
1463 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1464 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1465 case WS_IPPROTO_IPV4
: return IPPROTO_IPIP
;
1466 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1467 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1468 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1469 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1474 static void set_dont_fragment( int fd
, int level
, int value
)
1478 if (level
== IPPROTO_IP
)
1481 optname
= IP_DONTFRAG
;
1482 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1483 optname
= IP_MTU_DISCOVER
;
1484 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1491 #ifdef IPV6_DONTFRAG
1492 optname
= IPV6_DONTFRAG
;
1493 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1494 optname
= IPV6_MTU_DISCOVER
;
1495 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1501 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1504 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
, unsigned int flags
)
1506 unsigned int options
= 0;
1507 int sockfd
, unix_type
, unix_family
, unix_protocol
, value
;
1510 unix_family
= get_unix_family( family
);
1511 unix_type
= get_unix_type( type
);
1512 unix_protocol
= get_unix_protocol( protocol
);
1514 if (unix_protocol
< 0)
1516 if (type
&& unix_type
< 0)
1517 set_win32_error( WSAESOCKTNOSUPPORT
);
1519 set_win32_error( WSAEPROTONOSUPPORT
);
1522 if (unix_family
< 0)
1524 if (family
>= 0 && unix_type
< 0)
1525 set_win32_error( WSAESOCKTNOSUPPORT
);
1527 set_win32_error( WSAEAFNOSUPPORT
);
1531 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1534 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1535 else set_win32_error( sock_get_error( errno
));
1538 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1540 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1543 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1546 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1549 /* Should we retrieve val using a getsockopt call and then
1550 * set the modified one? */
1551 val
.ipx_pt
= ipx_type
;
1552 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1557 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1559 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1560 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1561 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1562 else if (unix_type
== SOCK_STREAM
)
1563 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1567 if (unix_family
== AF_INET6
)
1569 static const int enable
= 1;
1570 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1574 len
= sizeof(value
);
1575 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_RCVBUF
, &value
, &len
))
1576 sock
->rcvbuf
= value
;
1578 len
= sizeof(value
);
1579 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_SNDBUF
, &value
, &len
))
1580 sock
->sndbuf
= value
;
1582 sock
->state
= (type
== WS_SOCK_STREAM
? SOCK_UNCONNECTED
: SOCK_CONNECTIONLESS
);
1583 sock
->flags
= flags
;
1584 sock
->proto
= protocol
;
1586 sock
->family
= family
;
1590 options
= get_fd_options( sock
->fd
);
1591 release_object( sock
->fd
);
1594 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1599 /* We can't immediately allow caching for a connection-mode socket, since it
1600 * might be accepted into (changing the underlying fd object.) */
1601 if (sock
->type
!= WS_SOCK_STREAM
) allow_fd_caching( sock
->fd
);
1606 /* accepts a socket and inits it */
1607 static int accept_new_fd( struct sock
*sock
)
1610 /* Try to accept(2). We can't be safe that this an already connected socket
1611 * or that accept() is allowed on it. In those cases we will get -1/errno
1614 struct sockaddr saddr
;
1615 socklen_t slen
= sizeof(saddr
);
1616 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1618 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1620 set_error( sock_get_ntstatus( errno
));
1624 /* accept a socket (creates a new fd) */
1625 static struct sock
*accept_socket( struct sock
*sock
)
1627 struct sock
*acceptsock
;
1630 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1632 if ( sock
->deferred
)
1634 acceptsock
= sock
->deferred
;
1635 sock
->deferred
= NULL
;
1639 union unix_sockaddr unix_addr
;
1642 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1643 if (!(acceptsock
= create_socket()))
1649 /* newly created socket gets the same properties of the listening socket */
1650 acceptsock
->state
= SOCK_CONNECTED
;
1651 acceptsock
->bound
= 1;
1652 acceptsock
->nonblocking
= sock
->nonblocking
;
1653 acceptsock
->mask
= sock
->mask
;
1654 acceptsock
->proto
= sock
->proto
;
1655 acceptsock
->type
= sock
->type
;
1656 acceptsock
->family
= sock
->family
;
1657 acceptsock
->window
= sock
->window
;
1658 acceptsock
->message
= sock
->message
;
1659 acceptsock
->connect_time
= current_time
;
1660 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
1661 acceptsock
->flags
= sock
->flags
;
1662 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1663 get_fd_options( sock
->fd
) )))
1665 release_object( acceptsock
);
1668 unix_len
= sizeof(unix_addr
);
1669 if (!getsockname( acceptfd
, &unix_addr
.addr
, &unix_len
))
1670 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1673 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1674 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1675 sock_reselect( sock
);
1679 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
1681 union unix_sockaddr unix_addr
;
1686 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
1688 if ( sock
->deferred
)
1690 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
1691 get_fd_options( acceptsock
->fd
) );
1695 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
1697 release_object( sock
->deferred
);
1698 sock
->deferred
= NULL
;
1702 if ((acceptfd
= accept_new_fd( sock
)) == -1)
1705 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1706 get_fd_options( acceptsock
->fd
) )))
1710 acceptsock
->state
= SOCK_CONNECTED
;
1711 acceptsock
->pending_events
= 0;
1712 acceptsock
->reported_events
= 0;
1713 acceptsock
->proto
= sock
->proto
;
1714 acceptsock
->type
= sock
->type
;
1715 acceptsock
->family
= sock
->family
;
1716 acceptsock
->wparam
= 0;
1717 acceptsock
->deferred
= NULL
;
1718 acceptsock
->connect_time
= current_time
;
1719 fd_copy_completion( acceptsock
->fd
, newfd
);
1720 release_object( acceptsock
->fd
);
1721 acceptsock
->fd
= newfd
;
1723 unix_len
= sizeof(unix_addr
);
1724 if (!getsockname( get_unix_fd( newfd
), &unix_addr
.addr
, &unix_len
))
1725 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1728 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1729 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1730 sock_reselect( sock
);
1737 static int bind_to_index( int fd
, in_addr_t bind_addr
, unsigned int index
)
1739 return setsockopt( fd
, IPPROTO_IP
, IP_BOUND_IF
, &index
, sizeof(index
) );
1742 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER)
1744 struct interface_filter
1746 struct sock_filter iface_memaddr
;
1747 struct sock_filter iface_rule
;
1748 struct sock_filter ip_memaddr
;
1749 struct sock_filter ip_rule
;
1750 struct sock_filter return_keep
;
1751 struct sock_filter return_dump
;
1753 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
1754 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1755 /sizeof(struct sock_filter)
1756 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
1757 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1758 /sizeof(struct sock_filter)
1759 # define FILTER_JUMP_NEXT() (u_char)(0)
1760 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
1761 static struct interface_filter generic_interface_filter
=
1763 /* This filter rule allows incoming packets on the specified interface, which works for all
1764 * remotely generated packets and for locally generated broadcast packets. */
1765 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
1766 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
1767 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
1768 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
1769 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
1770 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
1771 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
1772 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
1775 static int bind_to_index( int fd
, in_addr_t bind_addr
, unsigned int index
)
1777 in_addr_t ifindex
= htonl( index
);
1778 struct interface_filter specific_interface_filter
;
1779 struct sock_fprog filter_prog
;
1782 if ((ret
= setsockopt( fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
) )) < 0)
1785 specific_interface_filter
= generic_interface_filter
;
1786 specific_interface_filter
.iface_rule
.k
= index
;
1787 specific_interface_filter
.ip_rule
.k
= htonl( bind_addr
);
1788 filter_prog
.len
= sizeof(generic_interface_filter
) / sizeof(struct sock_filter
);
1789 filter_prog
.filter
= (struct sock_filter
*)&specific_interface_filter
;
1790 return setsockopt( fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
) );
1795 static int bind_to_index( int fd
, in_addr_t bind_addr
, unsigned int index
)
1801 #endif /* LINUX_BOUND_IF */
1803 /* Take bind() calls on any name corresponding to a local network adapter and
1804 * restrict the given socket to operating only on the specified interface. This
1805 * restriction consists of two components:
1806 * 1) An outgoing packet restriction suggesting the egress interface for all
1808 * 2) An incoming packet restriction dropping packets not meant for the
1810 * If the function succeeds in placing these restrictions, then the name for the
1811 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
1812 * receipt of broadcast packets on the socket. This behavior is only relevant to
1813 * UDP sockets and is needed for applications that expect to be able to receive
1814 * broadcast packets on a socket that is bound to a specific network interface.
1816 static int bind_to_interface( struct sock
*sock
, const struct sockaddr_in
*addr
)
1818 in_addr_t bind_addr
= addr
->sin_addr
.s_addr
;
1819 struct ifaddrs
*ifaddrs
, *ifaddr
;
1820 int fd
= get_unix_fd( sock
->fd
);
1821 static const int enable
= 1;
1824 if (bind_addr
== htonl( INADDR_ANY
) || bind_addr
== htonl( INADDR_LOOPBACK
))
1826 if (sock
->type
!= WS_SOCK_DGRAM
)
1829 if (getifaddrs( &ifaddrs
) < 0) return 0;
1831 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
1833 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET
1834 && ((struct sockaddr_in
*)ifaddr
->ifa_addr
)->sin_addr
.s_addr
== bind_addr
)
1836 index
= if_nametoindex( ifaddr
->ifa_name
);
1840 fprintf( stderr
, "Unable to look up interface index for %s: %s\n",
1841 ifaddr
->ifa_name
, strerror( errno
) );
1845 freeifaddrs( ifaddrs
);
1847 if (bind_to_index( fd
, bind_addr
, index
) < 0)
1850 fprintf( stderr
, "failed to bind to interface: %s\n", strerror( errno
) );
1854 if (setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) ) < 0)
1857 fprintf( stderr
, "failed to reuse address: %s\n", strerror( errno
) );
1864 freeifaddrs( ifaddrs
);
1868 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1869 static unsigned int get_ipv6_interface_index( const struct in6_addr
*addr
)
1871 struct ifaddrs
*ifaddrs
, *ifaddr
;
1873 if (getifaddrs( &ifaddrs
) < 0) return 0;
1875 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
1877 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET6
1878 && !memcmp( &((struct sockaddr_in6
*)ifaddr
->ifa_addr
)->sin6_addr
, addr
, sizeof(*addr
) ))
1880 unsigned int index
= if_nametoindex( ifaddr
->ifa_name
);
1885 fprintf( stderr
, "Unable to look up interface index for %s: %s\n",
1886 ifaddr
->ifa_name
, strerror( errno
) );
1890 freeifaddrs( ifaddrs
);
1895 freeifaddrs( ifaddrs
);
1900 /* return an errno value mapped to a WSA error */
1901 static unsigned int sock_get_error( int err
)
1905 case EINTR
: return WSAEINTR
;
1906 case EBADF
: return WSAEBADF
;
1908 case EACCES
: return WSAEACCES
;
1909 case EFAULT
: return WSAEFAULT
;
1910 case EINVAL
: return WSAEINVAL
;
1911 case EMFILE
: return WSAEMFILE
;
1913 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
1914 case EALREADY
: return WSAEALREADY
;
1915 case ENOTSOCK
: return WSAENOTSOCK
;
1916 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
1917 case EMSGSIZE
: return WSAEMSGSIZE
;
1918 case EPROTOTYPE
: return WSAEPROTOTYPE
;
1919 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
1920 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
1921 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
1922 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
1923 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
1924 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
1925 case EADDRINUSE
: return WSAEADDRINUSE
;
1926 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
1927 case ENETDOWN
: return WSAENETDOWN
;
1928 case ENETUNREACH
: return WSAENETUNREACH
;
1929 case ENETRESET
: return WSAENETRESET
;
1930 case ECONNABORTED
: return WSAECONNABORTED
;
1932 case ECONNRESET
: return WSAECONNRESET
;
1933 case ENOBUFS
: return WSAENOBUFS
;
1934 case EISCONN
: return WSAEISCONN
;
1935 case ENOTCONN
: return WSAENOTCONN
;
1936 case ESHUTDOWN
: return WSAESHUTDOWN
;
1937 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
1938 case ETIMEDOUT
: return WSAETIMEDOUT
;
1939 case ECONNREFUSED
: return WSAECONNREFUSED
;
1940 case ELOOP
: return WSAELOOP
;
1941 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
1942 case EHOSTDOWN
: return WSAEHOSTDOWN
;
1943 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
1944 case ENOTEMPTY
: return WSAENOTEMPTY
;
1946 case EPROCLIM
: return WSAEPROCLIM
;
1949 case EUSERS
: return WSAEUSERS
;
1952 case EDQUOT
: return WSAEDQUOT
;
1955 case ESTALE
: return WSAESTALE
;
1958 case EREMOTE
: return WSAEREMOTE
;
1964 perror("wineserver: sock_get_error() can't map error");
1969 static int sock_get_ntstatus( int err
)
1973 case EBADF
: return STATUS_INVALID_HANDLE
;
1974 case EBUSY
: return STATUS_DEVICE_BUSY
;
1976 case EACCES
: return STATUS_ACCESS_DENIED
;
1977 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
1978 case EINVAL
: return STATUS_INVALID_PARAMETER
;
1980 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
1982 case EWOULDBLOCK
: return STATUS_DEVICE_NOT_READY
;
1983 case EALREADY
: return STATUS_NETWORK_BUSY
;
1984 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
1985 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
1986 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
1987 case EPROTONOSUPPORT
:
1988 case ESOCKTNOSUPPORT
:
1991 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
1992 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
1993 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
1994 case EADDRINUSE
: return STATUS_SHARING_VIOLATION
;
1995 /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
1996 * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
1998 case EADDRNOTAVAIL
: return STATUS_INVALID_ADDRESS_COMPONENT
;
1999 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
2000 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
2001 case ENOTCONN
: return STATUS_INVALID_CONNECTION
;
2002 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
2003 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
2004 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
2005 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
2007 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
2008 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
2009 case EISCONN
: return STATUS_CONNECTION_ACTIVE
;
2011 case 0: return STATUS_SUCCESS
;
2014 perror("wineserver: sock_get_ntstatus() can't map error");
2015 return STATUS_UNSUCCESSFUL
;
2019 static struct accept_req
*alloc_accept_req( struct sock
*sock
, struct sock
*acceptsock
, struct async
*async
,
2020 const struct afd_accept_into_params
*params
)
2022 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
2026 req
->async
= (struct async
*)grab_object( async
);
2027 req
->iosb
= async_get_iosb( async
);
2028 req
->sock
= (struct sock
*)grab_object( sock
);
2029 req
->acceptsock
= acceptsock
;
2030 if (acceptsock
) grab_object( acceptsock
);
2036 req
->recv_len
= params
->recv_len
;
2037 req
->local_len
= params
->local_len
;
2043 static int sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2045 struct sock
*sock
= get_fd_user( fd
);
2048 assert( sock
->obj
.ops
== &sock_ops
);
2050 if (code
!= IOCTL_AFD_WINE_CREATE
&& (unix_fd
= get_unix_fd( fd
)) < 0) return 0;
2054 case IOCTL_AFD_WINE_CREATE
:
2056 const struct afd_create_params
*params
= get_req_data();
2058 if (get_req_data_size() != sizeof(*params
))
2060 set_error( STATUS_INVALID_PARAMETER
);
2063 init_socket( sock
, params
->family
, params
->type
, params
->protocol
, params
->flags
);
2067 case IOCTL_AFD_WINE_ACCEPT
:
2069 struct sock
*acceptsock
;
2070 obj_handle_t handle
;
2072 if (get_reply_max_size() != sizeof(handle
))
2074 set_error( STATUS_BUFFER_TOO_SMALL
);
2078 if (!(acceptsock
= accept_socket( sock
)))
2080 struct accept_req
*req
;
2082 if (sock
->nonblocking
) return 0;
2083 if (get_error() != STATUS_DEVICE_NOT_READY
) return 0;
2085 if (!(req
= alloc_accept_req( sock
, NULL
, async
, NULL
))) return 0;
2086 list_add_tail( &sock
->accept_list
, &req
->entry
);
2088 async_set_completion_callback( async
, free_accept_req
, req
);
2089 queue_async( &sock
->accept_q
, async
);
2090 sock_reselect( sock
);
2091 set_error( STATUS_PENDING
);
2094 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
2095 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
2096 acceptsock
->wparam
= handle
;
2097 release_object( acceptsock
);
2098 set_reply_data( &handle
, sizeof(handle
) );
2102 case IOCTL_AFD_WINE_ACCEPT_INTO
:
2104 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
2105 const struct afd_accept_into_params
*params
= get_req_data();
2106 struct sock
*acceptsock
;
2107 unsigned int remote_len
;
2108 struct accept_req
*req
;
2110 if (get_req_data_size() != sizeof(*params
) ||
2111 get_reply_max_size() < params
->recv_len
||
2112 get_reply_max_size() - params
->recv_len
< params
->local_len
)
2114 set_error( STATUS_BUFFER_TOO_SMALL
);
2118 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
2119 if (remote_len
< sizeof(int))
2121 set_error( STATUS_INVALID_PARAMETER
);
2125 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
2128 if (acceptsock
->accept_recv_req
)
2130 release_object( acceptsock
);
2131 set_error( STATUS_INVALID_PARAMETER
);
2135 if (!(req
= alloc_accept_req( sock
, acceptsock
, async
, params
)))
2137 release_object( acceptsock
);
2140 list_add_tail( &sock
->accept_list
, &req
->entry
);
2141 acceptsock
->accept_recv_req
= req
;
2142 release_object( acceptsock
);
2144 acceptsock
->wparam
= params
->accept_handle
;
2145 async_set_completion_callback( async
, free_accept_req
, req
);
2146 queue_async( &sock
->accept_q
, async
);
2147 sock_reselect( sock
);
2148 set_error( STATUS_PENDING
);
2152 case IOCTL_AFD_LISTEN
:
2154 const struct afd_listen_params
*params
= get_req_data();
2156 if (get_req_data_size() < sizeof(*params
))
2158 set_error( STATUS_INVALID_PARAMETER
);
2164 set_error( STATUS_INVALID_PARAMETER
);
2168 if (listen( unix_fd
, params
->backlog
) < 0)
2170 set_error( sock_get_ntstatus( errno
) );
2174 sock
->state
= SOCK_LISTENING
;
2176 /* a listening socket can no longer be accepted into */
2177 allow_fd_caching( sock
->fd
);
2179 /* we may already be selecting for AFD_POLL_ACCEPT */
2180 sock_reselect( sock
);
2184 case IOCTL_AFD_WINE_CONNECT
:
2186 const struct afd_connect_params
*params
= get_req_data();
2187 const struct WS_sockaddr
*addr
;
2188 union unix_sockaddr unix_addr
;
2189 struct connect_req
*req
;
2193 if (get_req_data_size() < sizeof(*params
) ||
2194 get_req_data_size() - sizeof(*params
) < params
->addr_len
)
2196 set_error( STATUS_BUFFER_TOO_SMALL
);
2199 send_len
= get_req_data_size() - sizeof(*params
) - params
->addr_len
;
2200 addr
= (const struct WS_sockaddr
*)(params
+ 1);
2202 if (!params
->synchronous
&& !sock
->bound
)
2204 set_error( STATUS_INVALID_PARAMETER
);
2208 if (sock
->accept_recv_req
)
2210 set_error( STATUS_INVALID_PARAMETER
);
2214 if (sock
->connect_req
)
2216 set_error( STATUS_INVALID_PARAMETER
);
2220 switch (sock
->state
)
2222 case SOCK_LISTENING
:
2223 set_error( STATUS_INVALID_PARAMETER
);
2226 case SOCK_CONNECTING
:
2227 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2228 * but there's no status code that maps to WSAEALREADY... */
2229 set_error( params
->synchronous
? STATUS_ADDRESS_ALREADY_ASSOCIATED
: STATUS_INVALID_PARAMETER
);
2232 case SOCK_CONNECTED
:
2233 set_error( STATUS_CONNECTION_ACTIVE
);
2236 case SOCK_UNCONNECTED
:
2237 case SOCK_CONNECTIONLESS
:
2241 unix_len
= sockaddr_to_unix( addr
, params
->addr_len
, &unix_addr
);
2244 set_error( STATUS_INVALID_ADDRESS
);
2247 if (unix_addr
.addr
.sa_family
== AF_INET
&& !memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 ))
2248 unix_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_LOOPBACK
);
2250 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2251 if (ret
< 0 && errno
!= EINPROGRESS
)
2253 set_error( sock_get_ntstatus( errno
) );
2257 /* a connected or connecting socket can no longer be accepted into */
2258 allow_fd_caching( sock
->fd
);
2260 unix_len
= sizeof(unix_addr
);
2261 if (!sock
->bound
&& !getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
))
2262 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2267 sock
->state
= SOCK_CONNECTED
;
2269 if (!send_len
) return 1;
2272 if (!(req
= mem_alloc( sizeof(*req
) )))
2275 sock
->state
= SOCK_CONNECTING
;
2277 if (params
->synchronous
&& sock
->nonblocking
)
2279 sock_reselect( sock
);
2280 set_error( STATUS_DEVICE_NOT_READY
);
2284 req
->async
= (struct async
*)grab_object( async
);
2285 req
->iosb
= async_get_iosb( async
);
2286 req
->sock
= (struct sock
*)grab_object( sock
);
2287 req
->addr_len
= params
->addr_len
;
2288 req
->send_len
= send_len
;
2289 req
->send_cursor
= 0;
2291 async_set_completion_callback( async
, free_connect_req
, req
);
2292 sock
->connect_req
= req
;
2293 queue_async( &sock
->connect_q
, async
);
2294 sock_reselect( sock
);
2295 set_error( STATUS_PENDING
);
2299 case IOCTL_AFD_WINE_SHUTDOWN
:
2303 if (get_req_data_size() < sizeof(int))
2305 set_error( STATUS_BUFFER_TOO_SMALL
);
2308 how
= *(int *)get_req_data();
2312 set_error( STATUS_INVALID_PARAMETER
);
2316 if (sock
->state
!= SOCK_CONNECTED
&& sock
->state
!= SOCK_CONNECTIONLESS
)
2318 set_error( STATUS_INVALID_CONNECTION
);
2324 sock
->rd_shutdown
= 1;
2326 if (how
!= SD_RECEIVE
)
2328 sock
->wr_shutdown
= 1;
2329 if (list_empty( &sock
->write_q
.queue
))
2330 shutdown( unix_fd
, SHUT_WR
);
2332 sock
->wr_shutdown_pending
= 1;
2337 if (sock
->event
) release_object( sock
->event
);
2341 sock
->nonblocking
= 1;
2344 sock_reselect( sock
);
2348 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE
:
2352 if (get_req_data_size() < sizeof(int))
2354 set_error( STATUS_BUFFER_TOO_SMALL
);
2357 force_async
= *(int *)get_req_data();
2359 if (sock
->nonblocking
&& !force_async
)
2361 set_error( STATUS_DEVICE_NOT_READY
);
2364 if (!sock_get_ifchange( sock
)) return 0;
2365 queue_async( &sock
->ifchange_q
, async
);
2366 set_error( STATUS_PENDING
);
2370 case IOCTL_AFD_WINE_FIONBIO
:
2371 if (get_req_data_size() < sizeof(int))
2373 set_error( STATUS_BUFFER_TOO_SMALL
);
2376 if (*(int *)get_req_data())
2378 sock
->nonblocking
= 1;
2384 set_error( STATUS_INVALID_PARAMETER
);
2387 sock
->nonblocking
= 0;
2391 case IOCTL_AFD_GET_EVENTS
:
2393 struct afd_get_events_params params
= {0};
2396 if (get_reply_max_size() < sizeof(params
))
2398 set_error( STATUS_INVALID_PARAMETER
);
2402 params
.flags
= sock
->pending_events
& sock
->mask
;
2403 for (i
= 0; i
< ARRAY_SIZE( params
.status
); ++i
)
2404 params
.status
[i
] = sock_get_ntstatus( sock
->errors
[i
] );
2406 sock
->pending_events
= 0;
2407 sock_reselect( sock
);
2409 set_reply_data( ¶ms
, sizeof(params
) );
2413 case IOCTL_AFD_EVENT_SELECT
:
2415 struct event
*event
= NULL
;
2416 obj_handle_t event_handle
;
2419 if (is_machine_64bit( current
->process
->machine
))
2421 const struct afd_event_select_params_64
*params
= get_req_data();
2423 if (get_req_data_size() < sizeof(*params
))
2425 set_error( STATUS_INVALID_PARAMETER
);
2429 event_handle
= params
->event
;
2430 mask
= params
->mask
;
2434 const struct afd_event_select_params_32
*params
= get_req_data();
2436 if (get_req_data_size() < sizeof(*params
))
2438 set_error( STATUS_INVALID_PARAMETER
);
2442 event_handle
= params
->event
;
2443 mask
= params
->mask
;
2446 if ((event_handle
|| mask
) &&
2447 !(event
= get_event_obj( current
->process
, event_handle
, EVENT_MODIFY_STATE
)))
2449 set_error( STATUS_INVALID_PARAMETER
);
2453 if (sock
->event
) release_object( sock
->event
);
2454 sock
->event
= event
;
2459 sock
->nonblocking
= 1;
2461 sock_reselect( sock
);
2466 case IOCTL_AFD_WINE_MESSAGE_SELECT
:
2468 const struct afd_message_select_params
*params
= get_req_data();
2470 if (get_req_data_size() < sizeof(params
))
2472 set_error( STATUS_BUFFER_TOO_SMALL
);
2476 if (sock
->event
) release_object( sock
->event
);
2480 sock
->pending_events
= 0;
2481 sock
->reported_events
= 0;
2484 sock
->mask
= params
->mask
;
2485 sock
->window
= params
->window
;
2486 sock
->message
= params
->message
;
2487 sock
->wparam
= params
->handle
;
2488 sock
->nonblocking
= 1;
2490 sock_reselect( sock
);
2495 case IOCTL_AFD_BIND
:
2497 const struct afd_bind_params
*params
= get_req_data();
2498 union unix_sockaddr unix_addr
, bind_addr
;
2499 data_size_t in_size
;
2502 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2504 if (get_req_data_size() < get_reply_max_size())
2506 set_error( STATUS_BUFFER_TOO_SMALL
);
2509 in_size
= get_req_data_size() - get_reply_max_size();
2510 if (in_size
< offsetof(struct afd_bind_params
, addr
.sa_data
)
2511 || get_reply_max_size() < in_size
- sizeof(int))
2513 set_error( STATUS_INVALID_PARAMETER
);
2519 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED
);
2523 unix_len
= sockaddr_to_unix( ¶ms
->addr
, in_size
- sizeof(int), &unix_addr
);
2526 set_error( STATUS_INVALID_ADDRESS
);
2529 bind_addr
= unix_addr
;
2531 if (unix_addr
.addr
.sa_family
== AF_INET
)
2533 if (!memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 )
2534 || bind_to_interface( sock
, &unix_addr
.in
))
2535 bind_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
2537 else if (unix_addr
.addr
.sa_family
== AF_INET6
)
2539 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2540 /* Windows allows specifying zero to use the default scope. Linux
2541 * interprets it as an interface index and requires that it be
2543 if (!unix_addr
.in6
.sin6_scope_id
)
2544 bind_addr
.in6
.sin6_scope_id
= get_ipv6_interface_index( &unix_addr
.in6
.sin6_addr
);
2548 if (bind( unix_fd
, &bind_addr
.addr
, unix_len
) < 0)
2550 if (errno
== EADDRINUSE
)
2553 socklen_t len
= sizeof(reuse
);
2555 if (!getsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&reuse
, &len
) && reuse
)
2559 set_error( sock_get_ntstatus( errno
) );
2565 unix_len
= sizeof(bind_addr
);
2566 if (!getsockname( unix_fd
, &bind_addr
.addr
, &unix_len
))
2568 /* store the interface or magic loopback address instead of the
2569 * actual unix address */
2570 if (bind_addr
.addr
.sa_family
== AF_INET
)
2571 bind_addr
.in
.sin_addr
= unix_addr
.in
.sin_addr
;
2572 sock
->addr_len
= sockaddr_from_unix( &bind_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2575 if (get_reply_max_size() >= sock
->addr_len
)
2576 set_reply_data( &sock
->addr
, sock
->addr_len
);
2580 case IOCTL_AFD_GETSOCKNAME
:
2583 set_error( STATUS_INVALID_PARAMETER
);
2587 if (get_reply_max_size() < sock
->addr_len
)
2589 set_error( STATUS_BUFFER_TOO_SMALL
);
2593 set_reply_data( &sock
->addr
, sock
->addr_len
);
2596 case IOCTL_AFD_WINE_DEFER
:
2598 const obj_handle_t
*handle
= get_req_data();
2599 struct sock
*acceptsock
;
2601 if (get_req_data_size() < sizeof(*handle
))
2603 set_error( STATUS_BUFFER_TOO_SMALL
);
2607 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, *handle
, 0, &sock_ops
);
2608 if (!acceptsock
) return 0;
2610 sock
->deferred
= acceptsock
;
2614 case IOCTL_AFD_WINE_GET_INFO
:
2616 struct afd_get_info_params params
;
2618 if (get_reply_max_size() < sizeof(params
))
2620 set_error( STATUS_BUFFER_TOO_SMALL
);
2624 params
.family
= sock
->family
;
2625 params
.type
= sock
->type
;
2626 params
.protocol
= sock
->proto
;
2627 set_reply_data( ¶ms
, sizeof(params
) );
2631 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN
:
2633 int listening
= (sock
->state
== SOCK_LISTENING
);
2635 if (get_reply_max_size() < sizeof(listening
))
2637 set_error( STATUS_BUFFER_TOO_SMALL
);
2641 set_reply_data( &listening
, sizeof(listening
) );
2645 case IOCTL_AFD_WINE_GET_SO_ERROR
:
2648 socklen_t len
= sizeof(error
);
2651 if (get_reply_max_size() < sizeof(error
))
2653 set_error( STATUS_BUFFER_TOO_SMALL
);
2657 if (getsockopt( unix_fd
, SOL_SOCKET
, SO_ERROR
, (char *)&error
, &len
) < 0)
2659 set_error( sock_get_ntstatus( errno
) );
2665 for (i
= 0; i
< ARRAY_SIZE( sock
->errors
); ++i
)
2667 if (sock
->errors
[i
])
2669 error
= sock_get_error( sock
->errors
[i
] );
2675 set_reply_data( &error
, sizeof(error
) );
2679 case IOCTL_AFD_WINE_GET_SO_RCVBUF
:
2681 int rcvbuf
= sock
->rcvbuf
;
2683 if (get_reply_max_size() < sizeof(rcvbuf
))
2685 set_error( STATUS_BUFFER_TOO_SMALL
);
2689 set_reply_data( &rcvbuf
, sizeof(rcvbuf
) );
2693 case IOCTL_AFD_WINE_SET_SO_RCVBUF
:
2697 if (get_req_data_size() < sizeof(rcvbuf
))
2699 set_error( STATUS_BUFFER_TOO_SMALL
);
2702 rcvbuf
= *(DWORD
*)get_req_data();
2704 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_RCVBUF
, (char *)&rcvbuf
, sizeof(rcvbuf
) ))
2705 sock
->rcvbuf
= rcvbuf
;
2707 set_error( sock_get_ntstatus( errno
) );
2711 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO
:
2713 DWORD rcvtimeo
= sock
->rcvtimeo
;
2715 if (get_reply_max_size() < sizeof(rcvtimeo
))
2717 set_error( STATUS_BUFFER_TOO_SMALL
);
2721 set_reply_data( &rcvtimeo
, sizeof(rcvtimeo
) );
2725 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO
:
2729 if (get_req_data_size() < sizeof(rcvtimeo
))
2731 set_error( STATUS_BUFFER_TOO_SMALL
);
2734 rcvtimeo
= *(DWORD
*)get_req_data();
2736 sock
->rcvtimeo
= rcvtimeo
;
2740 case IOCTL_AFD_WINE_GET_SO_SNDBUF
:
2742 int sndbuf
= sock
->sndbuf
;
2744 if (get_reply_max_size() < sizeof(sndbuf
))
2746 set_error( STATUS_BUFFER_TOO_SMALL
);
2750 set_reply_data( &sndbuf
, sizeof(sndbuf
) );
2754 case IOCTL_AFD_WINE_SET_SO_SNDBUF
:
2758 if (get_req_data_size() < sizeof(sndbuf
))
2760 set_error( STATUS_BUFFER_TOO_SMALL
);
2763 sndbuf
= *(DWORD
*)get_req_data();
2768 /* setsockopt fails if a zero value is passed */
2769 sock
->sndbuf
= sndbuf
;
2774 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_SNDBUF
, (char *)&sndbuf
, sizeof(sndbuf
) ))
2775 sock
->sndbuf
= sndbuf
;
2777 set_error( sock_get_ntstatus( errno
) );
2781 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO
:
2783 DWORD sndtimeo
= sock
->sndtimeo
;
2785 if (get_reply_max_size() < sizeof(sndtimeo
))
2787 set_error( STATUS_BUFFER_TOO_SMALL
);
2791 set_reply_data( &sndtimeo
, sizeof(sndtimeo
) );
2795 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO
:
2799 if (get_req_data_size() < sizeof(sndtimeo
))
2801 set_error( STATUS_BUFFER_TOO_SMALL
);
2804 sndtimeo
= *(DWORD
*)get_req_data();
2806 sock
->sndtimeo
= sndtimeo
;
2811 set_error( STATUS_NOT_SUPPORTED
);
2816 static int poll_single_socket( struct sock
*sock
, int mask
)
2818 struct pollfd pollfd
;
2820 pollfd
.fd
= get_unix_fd( sock
->fd
);
2821 pollfd
.events
= poll_flags_from_afd( sock
, mask
);
2822 if (pollfd
.events
< 0 || poll( &pollfd
, 1, 0 ) < 0)
2825 if ((mask
& AFD_POLL_HUP
) && (pollfd
.revents
& POLLIN
) && sock
->type
== WS_SOCK_STREAM
)
2829 if (!recv( get_unix_fd( sock
->fd
), &dummy
, 1, MSG_PEEK
))
2831 pollfd
.revents
&= ~POLLIN
;
2832 pollfd
.revents
|= POLLHUP
;
2836 return get_poll_flags( sock
, pollfd
.revents
) & mask
;
2839 static int poll_socket( struct sock
*poll_sock
, struct async
*async
, timeout_t timeout
,
2840 unsigned int count
, const struct poll_socket_input
*input
)
2842 struct poll_socket_output
*output
;
2843 struct poll_req
*req
;
2846 if (!(output
= mem_alloc( count
* sizeof(*output
) )))
2848 memset( output
, 0, count
* sizeof(*output
) );
2850 if (!(req
= mem_alloc( offsetof( struct poll_req
, sockets
[count
] ) )))
2856 req
->timeout
= NULL
;
2857 if (timeout
&& timeout
!= TIMEOUT_INFINITE
&&
2858 !(req
->timeout
= add_timeout_user( timeout
, async_poll_timeout
, req
)))
2865 for (i
= 0; i
< count
; ++i
)
2867 req
->sockets
[i
].sock
= (struct sock
*)get_handle_obj( current
->process
, input
[i
].socket
, 0, &sock_ops
);
2868 if (!req
->sockets
[i
].sock
)
2870 for (j
= 0; j
< i
; ++j
) release_object( req
->sockets
[i
].sock
);
2871 if (req
->timeout
) remove_timeout_user( req
->timeout
);
2876 req
->sockets
[i
].flags
= input
[i
].flags
;
2880 req
->async
= (struct async
*)grab_object( async
);
2881 req
->iosb
= async_get_iosb( async
);
2882 req
->output
= output
;
2884 list_add_tail( &poll_list
, &req
->entry
);
2885 async_set_completion_callback( async
, free_poll_req
, req
);
2886 queue_async( &poll_sock
->poll_q
, async
);
2888 if (!timeout
) req
->iosb
->status
= STATUS_SUCCESS
;
2890 for (i
= 0; i
< count
; ++i
)
2892 struct sock
*sock
= req
->sockets
[i
].sock
;
2893 int mask
= req
->sockets
[i
].flags
;
2894 int flags
= poll_single_socket( sock
, mask
);
2898 req
->iosb
->status
= STATUS_SUCCESS
;
2899 output
[i
].flags
= flags
;
2900 output
[i
].status
= sock_get_ntstatus( sock_error( sock
->fd
) );
2903 /* FIXME: do other error conditions deserve a similar treatment? */
2904 if (sock
->state
!= SOCK_CONNECTING
&& sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] && (mask
& AFD_POLL_CONNECT_ERR
))
2906 req
->iosb
->status
= STATUS_SUCCESS
;
2907 output
[i
].flags
|= AFD_POLL_CONNECT_ERR
;
2908 output
[i
].status
= sock_get_ntstatus( sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] );
2912 if (req
->iosb
->status
!= STATUS_PENDING
)
2914 req
->iosb
->out_data
= output
;
2915 req
->iosb
->out_size
= count
* sizeof(*output
);
2916 async_terminate( req
->async
, STATUS_ALERTED
);
2919 for (i
= 0; i
< req
->count
; ++i
)
2920 sock_reselect( req
->sockets
[i
].sock
);
2921 set_error( STATUS_PENDING
);
2925 #ifdef HAVE_LINUX_RTNETLINK_H
2927 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
2928 static struct object
*ifchange_object
;
2930 static void ifchange_dump( struct object
*obj
, int verbose
);
2931 static struct fd
*ifchange_get_fd( struct object
*obj
);
2932 static void ifchange_destroy( struct object
*obj
);
2934 static int ifchange_get_poll_events( struct fd
*fd
);
2935 static void ifchange_poll_event( struct fd
*fd
, int event
);
2939 struct object obj
; /* object header */
2940 struct fd
*fd
; /* interface change file descriptor */
2941 struct list sockets
; /* list of sockets to send interface change notifications */
2944 static const struct object_ops ifchange_ops
=
2946 sizeof(struct ifchange
), /* size */
2947 &no_type
, /* type */
2948 ifchange_dump
, /* dump */
2949 no_add_queue
, /* add_queue */
2950 NULL
, /* remove_queue */
2951 NULL
, /* signaled */
2952 no_satisfied
, /* satisfied */
2953 no_signal
, /* signal */
2954 ifchange_get_fd
, /* get_fd */
2955 default_map_access
, /* map_access */
2956 default_get_sd
, /* get_sd */
2957 default_set_sd
, /* set_sd */
2958 no_get_full_name
, /* get_full_name */
2959 no_lookup_name
, /* lookup_name */
2960 no_link_name
, /* link_name */
2961 NULL
, /* unlink_name */
2962 no_open_file
, /* open_file */
2963 no_kernel_obj_list
, /* get_kernel_obj_list */
2964 no_close_handle
, /* close_handle */
2965 ifchange_destroy
/* destroy */
2968 static const struct fd_ops ifchange_fd_ops
=
2970 ifchange_get_poll_events
, /* get_poll_events */
2971 ifchange_poll_event
, /* poll_event */
2972 NULL
, /* get_fd_type */
2973 no_fd_read
, /* read */
2974 no_fd_write
, /* write */
2975 no_fd_flush
, /* flush */
2976 no_fd_get_file_info
, /* get_file_info */
2977 no_fd_get_volume_info
, /* get_volume_info */
2978 no_fd_ioctl
, /* ioctl */
2979 NULL
, /* queue_async */
2980 NULL
/* reselect_async */
2983 static void ifchange_dump( struct object
*obj
, int verbose
)
2985 assert( obj
->ops
== &ifchange_ops
);
2986 fprintf( stderr
, "Interface change\n" );
2989 static struct fd
*ifchange_get_fd( struct object
*obj
)
2991 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
2992 return (struct fd
*)grab_object( ifchange
->fd
);
2995 static void ifchange_destroy( struct object
*obj
)
2997 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
2998 assert( obj
->ops
== &ifchange_ops
);
3000 release_object( ifchange
->fd
);
3002 /* reset the global ifchange object so that it will be recreated if it is needed again */
3003 assert( obj
== ifchange_object
);
3004 ifchange_object
= NULL
;
3007 static int ifchange_get_poll_events( struct fd
*fd
)
3012 /* wake up all the sockets waiting for a change notification event */
3013 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
3015 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3016 struct list
*ptr
, *next
;
3017 assert( obj
->ops
== &ifchange_ops
);
3018 assert( obj
== ifchange_object
);
3020 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
3022 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
3024 assert( sock
->ifchange_obj
);
3025 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
3026 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
3030 static void ifchange_poll_event( struct fd
*fd
, int event
)
3032 struct object
*ifchange
= get_fd_user( fd
);
3033 unsigned int status
= STATUS_PENDING
;
3034 char buffer
[PIPE_BUF
];
3037 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
3040 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
3041 return; /* retry when poll() says the socket is ready */
3042 status
= sock_get_ntstatus( errno
);
3046 struct nlmsghdr
*nlh
;
3048 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
3050 if (nlh
->nlmsg_type
== NLMSG_DONE
)
3052 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
3053 status
= STATUS_SUCCESS
;
3056 else status
= STATUS_CANCELLED
;
3058 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
3063 /* we only need one of these interface notification objects, all of the sockets dependent upon
3064 * it will wake up when a notification event occurs */
3065 static struct object
*get_ifchange( void )
3067 #ifdef HAVE_LINUX_RTNETLINK_H
3068 struct ifchange
*ifchange
;
3069 struct sockaddr_nl addr
;
3072 if (ifchange_object
)
3074 /* increment the refcount for each socket that uses the ifchange object */
3075 return grab_object( ifchange_object
);
3078 /* create the socket we need for processing interface change notifications */
3079 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
3082 set_error( sock_get_ntstatus( errno
));
3085 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
3086 memset( &addr
, 0, sizeof(addr
) );
3087 addr
.nl_family
= AF_NETLINK
;
3088 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
3089 /* bind the socket to the special netlink kernel interface */
3090 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
3093 set_error( sock_get_ntstatus( errno
));
3096 if (!(ifchange
= alloc_object( &ifchange_ops
)))
3099 set_error( STATUS_NO_MEMORY
);
3102 list_init( &ifchange
->sockets
);
3103 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
3105 release_object( ifchange
);
3106 set_error( STATUS_NO_MEMORY
);
3109 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
3111 /* the ifchange object is now successfully configured */
3112 ifchange_object
= &ifchange
->obj
;
3113 return &ifchange
->obj
;
3115 set_error( STATUS_NOT_SUPPORTED
);
3120 /* add the socket to the interface change notification list */
3121 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
3123 #ifdef HAVE_LINUX_RTNETLINK_H
3124 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3126 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
3130 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3131 static struct object
*sock_get_ifchange( struct sock
*sock
)
3133 struct object
*ifchange
;
3135 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
3136 return sock
->ifchange_obj
;
3138 if (!(ifchange
= get_ifchange()))
3141 /* add the socket to the ifchange notification list */
3142 ifchange_add_sock( ifchange
, sock
);
3143 sock
->ifchange_obj
= ifchange
;
3147 /* destroy an existing ifchange queue for a specific socket */
3148 static void sock_release_ifchange( struct sock
*sock
)
3150 if (sock
->ifchange_obj
)
3152 list_remove( &sock
->ifchange_entry
);
3153 release_object( sock
->ifchange_obj
);
3154 sock
->ifchange_obj
= NULL
;
3158 static void socket_device_dump( struct object
*obj
, int verbose
);
3159 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3160 unsigned int attr
, struct object
*root
);
3161 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3162 unsigned int sharing
, unsigned int options
);
3164 static const struct object_ops socket_device_ops
=
3166 sizeof(struct object
), /* size */
3167 &device_type
, /* type */
3168 socket_device_dump
, /* dump */
3169 no_add_queue
, /* add_queue */
3170 NULL
, /* remove_queue */
3171 NULL
, /* signaled */
3172 no_satisfied
, /* satisfied */
3173 no_signal
, /* signal */
3174 no_get_fd
, /* get_fd */
3175 default_map_access
, /* map_access */
3176 default_get_sd
, /* get_sd */
3177 default_set_sd
, /* set_sd */
3178 default_get_full_name
, /* get_full_name */
3179 socket_device_lookup_name
, /* lookup_name */
3180 directory_link_name
, /* link_name */
3181 default_unlink_name
, /* unlink_name */
3182 socket_device_open_file
, /* open_file */
3183 no_kernel_obj_list
, /* get_kernel_obj_list */
3184 no_close_handle
, /* close_handle */
3185 no_destroy
/* destroy */
3188 static void socket_device_dump( struct object
*obj
, int verbose
)
3190 fputs( "Socket device\n", stderr
);
3193 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3194 unsigned int attr
, struct object
*root
)
3196 if (name
) name
->len
= 0;
3200 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3201 unsigned int sharing
, unsigned int options
)
3205 if (!(sock
= create_socket())) return NULL
;
3206 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
3208 release_object( sock
);
3214 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
3215 unsigned int attr
, const struct security_descriptor
*sd
)
3217 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
3220 DECL_HANDLER(recv_socket
)
3222 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3223 unsigned int status
= req
->status
;
3224 timeout_t timeout
= 0;
3225 struct async
*async
;
3231 /* recv() returned EWOULDBLOCK, i.e. no data available yet */
3232 if (status
== STATUS_DEVICE_NOT_READY
&& !sock
->nonblocking
)
3234 /* Set a timeout on the async if necessary.
3236 * We want to do this *only* if the client gave us STATUS_DEVICE_NOT_READY.
3237 * If the client gave us STATUS_PENDING, it expects the async to always
3238 * block (it was triggered by WSARecv*() with a valid OVERLAPPED
3239 * structure) and for the timeout not to be respected. */
3240 if (is_fd_overlapped( fd
))
3241 timeout
= (timeout_t
)sock
->rcvtimeo
* -10000;
3243 status
= STATUS_PENDING
;
3246 if ((status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
) && sock
->rd_shutdown
)
3247 status
= STATUS_PIPE_DISCONNECTED
;
3249 sock
->pending_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3250 sock
->reported_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3252 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3256 if (status
== STATUS_SUCCESS
)
3258 struct iosb
*iosb
= async_get_iosb( async
);
3259 iosb
->result
= req
->total
;
3260 release_object( iosb
);
3263 else if (status
== STATUS_PENDING
)
3267 set_error( status
);
3270 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3272 if (status
== STATUS_PENDING
)
3273 queue_async( &sock
->read_q
, async
);
3275 /* always reselect; we changed reported_events above */
3276 sock_reselect( sock
);
3278 reply
->wait
= async_handoff( async
, success
, NULL
, 0 );
3279 reply
->options
= get_fd_options( fd
);
3280 release_object( async
);
3282 release_object( sock
);
3285 DECL_HANDLER(poll_socket
)
3287 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3288 const struct poll_socket_input
*input
= get_req_data();
3289 struct async
*async
;
3294 count
= get_req_data_size() / sizeof(*input
);
3296 if ((async
= create_request_async( sock
->fd
, get_fd_comp_flags( sock
->fd
), &req
->async
)))
3298 reply
->wait
= async_handoff( async
, poll_socket( sock
, async
, req
->timeout
, count
, input
), NULL
, 0 );
3299 reply
->options
= get_fd_options( sock
->fd
);
3300 release_object( async
);
3303 release_object( sock
);
3306 DECL_HANDLER(send_socket
)
3308 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3309 unsigned int status
= req
->status
;
3310 timeout_t timeout
= 0;
3311 struct async
*async
;
3317 if (sock
->type
== WS_SOCK_DGRAM
)
3319 /* sendto() and sendmsg() implicitly binds a socket */
3320 union unix_sockaddr unix_addr
;
3321 socklen_t unix_len
= sizeof(unix_addr
);
3323 if (!sock
->bound
&& !getsockname( get_unix_fd( fd
), &unix_addr
.addr
, &unix_len
))
3324 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
3328 if (status
!= STATUS_SUCCESS
)
3330 /* send() calls only clear and reselect events if unsuccessful. */
3331 sock
->pending_events
&= ~AFD_POLL_WRITE
;
3332 sock
->reported_events
&= ~AFD_POLL_WRITE
;
3335 /* If we had a short write and the socket is nonblocking (and the client is
3336 * not trying to force the operation to be asynchronous), return success.
3337 * Windows actually refuses to send any data in this case, and returns
3338 * EWOULDBLOCK, but we have no way of doing that. */
3339 if (status
== STATUS_DEVICE_NOT_READY
&& req
->total
&& sock
->nonblocking
)
3340 status
= STATUS_SUCCESS
;
3342 /* send() returned EWOULDBLOCK or a short write, i.e. cannot send all data yet */
3343 if (status
== STATUS_DEVICE_NOT_READY
&& !sock
->nonblocking
)
3345 /* Set a timeout on the async if necessary.
3347 * We want to do this *only* if the client gave us STATUS_DEVICE_NOT_READY.
3348 * If the client gave us STATUS_PENDING, it expects the async to always
3349 * block (it was triggered by WSASend*() with a valid OVERLAPPED
3350 * structure) and for the timeout not to be respected. */
3351 if (is_fd_overlapped( fd
))
3352 timeout
= (timeout_t
)sock
->sndtimeo
* -10000;
3354 status
= STATUS_PENDING
;
3357 if ((status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
) && sock
->wr_shutdown
)
3358 status
= STATUS_PIPE_DISCONNECTED
;
3360 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3364 if (status
== STATUS_SUCCESS
)
3366 struct iosb
*iosb
= async_get_iosb( async
);
3367 iosb
->result
= req
->total
;
3368 release_object( iosb
);
3371 else if (status
== STATUS_PENDING
)
3375 set_error( status
);
3378 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3380 if (status
== STATUS_PENDING
)
3381 queue_async( &sock
->write_q
, async
);
3383 /* always reselect; we changed reported_events above */
3384 sock_reselect( sock
);
3386 reply
->wait
= async_handoff( async
, success
, NULL
, 0 );
3387 reply
->options
= get_fd_options( fd
);
3388 release_object( async
);
3390 release_object( sock
);