2 * Server-side socket management
4 * Copyright (C) 1999 Marcus Meissner, Ove Kåven
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * FIXME: we use read|write access in all cases. Shouldn't we depend that
21 * on the access of the current handle?
39 #ifdef HAVE_NETINET_IN_H
40 # include <netinet/in.h>
46 #include <sys/types.h>
47 #ifdef HAVE_SYS_SOCKET_H
48 # include <sys/socket.h>
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
53 #ifdef HAVE_SYS_FILIO_H
54 # include <sys/filio.h>
59 #ifdef HAVE_LINUX_FILTER_H
60 # include <linux/filter.h>
62 #ifdef HAVE_LINUX_RTNETLINK_H
63 # include <linux/rtnetlink.h>
66 #ifdef HAVE_NETIPX_IPX_H
67 # include <netipx/ipx.h>
68 #elif defined(HAVE_LINUX_IPX_H)
69 # ifdef HAVE_ASM_TYPES_H
70 # include <asm/types.h>
72 # ifdef HAVE_LINUX_TYPES_H
73 # include <linux/types.h>
75 # include <linux/ipx.h>
77 #if defined(SOL_IPX) || defined(SO_DEFAULT_HEADERS)
81 #ifdef HAVE_LINUX_IRDA_H
82 # ifdef HAVE_LINUX_TYPES_H
83 # include <linux/types.h>
85 # include <linux/irda.h>
90 #define WIN32_NO_STATUS
108 #if defined(linux) && !defined(IP_UNICAST_IF)
109 #define IP_UNICAST_IF 50
112 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
116 struct WS_sockaddr addr
;
117 struct WS_sockaddr_in in
;
118 struct WS_sockaddr_in6 in6
;
119 struct WS_sockaddr_ipx ipx
;
123 static struct list poll_list
= LIST_INIT( poll_list
);
130 struct timeout_user
*timeout
;
133 struct poll_socket_output
*output
;
146 struct sock
*sock
, *acceptsock
;
148 unsigned int recv_len
, local_len
;
156 unsigned int addr_len
, send_len
, send_cursor
;
159 enum connection_state
170 struct object obj
; /* object header */
171 struct fd
*fd
; /* socket file descriptor */
172 enum connection_state state
; /* connection state */
173 unsigned int mask
; /* event mask */
174 /* pending AFD_POLL_* events which have not yet been reported to the application */
175 unsigned int pending_events
;
176 /* AFD_POLL_* events which have already been reported and should not be
177 * selected for again until reset by a relevant call.
179 * For example, if AFD_POLL_READ is set here and not in pending_events, it
180 * has already been reported and consumed, and we should not report it
181 * again, even if POLLIN is signaled, until it is reset by e.g recv().
183 * If an event has been signaled and not consumed yet, it will be set in
184 * both pending_events and reported_events (as we should only ever report
185 * any event once until it is reset.) */
186 unsigned int reported_events
;
187 unsigned int flags
; /* socket flags */
188 unsigned short proto
; /* socket protocol */
189 unsigned short type
; /* socket type */
190 unsigned short family
; /* socket family */
191 struct event
*event
; /* event object */
192 user_handle_t window
; /* window to send the message to */
193 unsigned int message
; /* message to send */
194 obj_handle_t wparam
; /* message wparam (socket handle) */
195 int errors
[AFD_POLL_BIT_COUNT
]; /* event errors */
196 timeout_t connect_time
;/* time the socket was connected */
197 struct sock
*deferred
; /* socket that waits for a deferred accept */
198 struct async_queue read_q
; /* queue for asynchronous reads */
199 struct async_queue write_q
; /* queue for asynchronous writes */
200 struct async_queue ifchange_q
; /* queue for interface change notifications */
201 struct async_queue accept_q
; /* queue for asynchronous accepts */
202 struct async_queue connect_q
; /* queue for asynchronous connects */
203 struct async_queue poll_q
; /* queue for asynchronous polls */
204 struct object
*ifchange_obj
; /* the interface change notification object */
205 struct list ifchange_entry
; /* entry in ifchange notification list */
206 struct list accept_list
; /* list of pending accept requests */
207 struct accept_req
*accept_recv_req
; /* pending accept-into request which will recv on this socket */
208 struct connect_req
*connect_req
; /* pending connection request */
209 struct poll_req
*main_poll
; /* main poll */
210 union win_sockaddr addr
; /* socket name */
211 int addr_len
; /* socket name length */
212 unsigned int rcvbuf
; /* advisory recv buffer size */
213 unsigned int sndbuf
; /* advisory send buffer size */
214 unsigned int rcvtimeo
; /* receive timeout in ms */
215 unsigned int sndtimeo
; /* send timeout in ms */
216 unsigned int rd_shutdown
: 1; /* is the read end shut down? */
217 unsigned int wr_shutdown
: 1; /* is the write end shut down? */
218 unsigned int wr_shutdown_pending
: 1; /* is a write shutdown pending? */
219 unsigned int hangup
: 1; /* has the read end received a hangup? */
220 unsigned int aborted
: 1; /* did we get a POLLERR or irregular POLLHUP? */
221 unsigned int nonblocking
: 1; /* is the socket nonblocking? */
222 unsigned int bound
: 1; /* is the socket bound? */
225 static void sock_dump( struct object
*obj
, int verbose
);
226 static struct fd
*sock_get_fd( struct object
*obj
);
227 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
228 static void sock_destroy( struct object
*obj
);
229 static struct object
*sock_get_ifchange( struct sock
*sock
);
230 static void sock_release_ifchange( struct sock
*sock
);
232 static int sock_get_poll_events( struct fd
*fd
);
233 static void sock_poll_event( struct fd
*fd
, int event
);
234 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
235 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
236 static void sock_cancel_async( struct fd
*fd
, struct async
*async
);
237 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
);
238 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
240 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
);
241 static struct sock
*accept_socket( struct sock
*sock
);
242 static int sock_get_ntstatus( int err
);
243 static unsigned int sock_get_error( int err
);
245 static const struct object_ops sock_ops
=
247 sizeof(struct sock
), /* size */
248 &file_type
, /* type */
249 sock_dump
, /* dump */
250 add_queue
, /* add_queue */
251 remove_queue
, /* remove_queue */
252 default_fd_signaled
, /* signaled */
253 no_satisfied
, /* satisfied */
254 no_signal
, /* signal */
255 sock_get_fd
, /* get_fd */
256 default_map_access
, /* map_access */
257 default_get_sd
, /* get_sd */
258 default_set_sd
, /* set_sd */
259 no_get_full_name
, /* get_full_name */
260 no_lookup_name
, /* lookup_name */
261 no_link_name
, /* link_name */
262 NULL
, /* unlink_name */
263 no_open_file
, /* open_file */
264 no_kernel_obj_list
, /* get_kernel_obj_list */
265 sock_close_handle
, /* close_handle */
266 sock_destroy
/* destroy */
269 static const struct fd_ops sock_fd_ops
=
271 sock_get_poll_events
, /* get_poll_events */
272 sock_poll_event
, /* poll_event */
273 sock_get_fd_type
, /* get_fd_type */
274 no_fd_read
, /* read */
275 no_fd_write
, /* write */
276 no_fd_flush
, /* flush */
277 default_fd_get_file_info
, /* get_file_info */
278 no_fd_get_volume_info
, /* get_volume_info */
279 sock_ioctl
, /* ioctl */
280 sock_cancel_async
, /* cancel_async */
281 sock_queue_async
, /* queue_async */
282 sock_reselect_async
/* reselect_async */
287 struct sockaddr addr
;
288 struct sockaddr_in in
;
289 struct sockaddr_in6 in6
;
291 struct sockaddr_ipx ipx
;
294 struct sockaddr_irda irda
;
298 static int sockaddr_from_unix( const union unix_sockaddr
*uaddr
, struct WS_sockaddr
*wsaddr
, socklen_t wsaddrlen
)
300 memset( wsaddr
, 0, wsaddrlen
);
302 switch (uaddr
->addr
.sa_family
)
306 struct WS_sockaddr_in win
= {0};
308 if (wsaddrlen
< sizeof(win
)) return -1;
309 win
.sin_family
= WS_AF_INET
;
310 win
.sin_port
= uaddr
->in
.sin_port
;
311 memcpy( &win
.sin_addr
, &uaddr
->in
.sin_addr
, sizeof(win
.sin_addr
) );
312 memcpy( wsaddr
, &win
, sizeof(win
) );
318 struct WS_sockaddr_in6 win
= {0};
320 if (wsaddrlen
< sizeof(win
)) return -1;
321 win
.sin6_family
= WS_AF_INET6
;
322 win
.sin6_port
= uaddr
->in6
.sin6_port
;
323 win
.sin6_flowinfo
= uaddr
->in6
.sin6_flowinfo
;
324 memcpy( &win
.sin6_addr
, &uaddr
->in6
.sin6_addr
, sizeof(win
.sin6_addr
) );
325 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
326 win
.sin6_scope_id
= uaddr
->in6
.sin6_scope_id
;
328 memcpy( wsaddr
, &win
, sizeof(win
) );
335 struct WS_sockaddr_ipx win
= {0};
337 if (wsaddrlen
< sizeof(win
)) return -1;
338 win
.sa_family
= WS_AF_IPX
;
339 memcpy( win
.sa_netnum
, &uaddr
->ipx
.sipx_network
, sizeof(win
.sa_netnum
) );
340 memcpy( win
.sa_nodenum
, &uaddr
->ipx
.sipx_node
, sizeof(win
.sa_nodenum
) );
341 win
.sa_socket
= uaddr
->ipx
.sipx_port
;
342 memcpy( wsaddr
, &win
, sizeof(win
) );
352 if (wsaddrlen
< sizeof(win
)) return -1;
353 win
.irdaAddressFamily
= WS_AF_IRDA
;
354 memcpy( win
.irdaDeviceID
, &uaddr
->irda
.sir_addr
, sizeof(win
.irdaDeviceID
) );
355 if (uaddr
->irda
.sir_lsap_sel
!= LSAP_ANY
)
356 snprintf( win
.irdaServiceName
, sizeof(win
.irdaServiceName
), "LSAP-SEL%u", uaddr
->irda
.sir_lsap_sel
);
358 memcpy( win
.irdaServiceName
, uaddr
->irda
.sir_name
, sizeof(win
.irdaServiceName
) );
359 memcpy( wsaddr
, &win
, sizeof(win
) );
373 static socklen_t
sockaddr_to_unix( const struct WS_sockaddr
*wsaddr
, int wsaddrlen
, union unix_sockaddr
*uaddr
)
375 memset( uaddr
, 0, sizeof(*uaddr
) );
377 switch (wsaddr
->sa_family
)
381 struct WS_sockaddr_in win
= {0};
383 if (wsaddrlen
< sizeof(win
)) return 0;
384 memcpy( &win
, wsaddr
, sizeof(win
) );
385 uaddr
->in
.sin_family
= AF_INET
;
386 uaddr
->in
.sin_port
= win
.sin_port
;
387 memcpy( &uaddr
->in
.sin_addr
, &win
.sin_addr
, sizeof(win
.sin_addr
) );
388 return sizeof(uaddr
->in
);
393 struct WS_sockaddr_in6 win
= {0};
395 if (wsaddrlen
< sizeof(win
)) return 0;
396 memcpy( &win
, wsaddr
, sizeof(win
) );
397 uaddr
->in6
.sin6_family
= AF_INET6
;
398 uaddr
->in6
.sin6_port
= win
.sin6_port
;
399 uaddr
->in6
.sin6_flowinfo
= win
.sin6_flowinfo
;
400 memcpy( &uaddr
->in6
.sin6_addr
, &win
.sin6_addr
, sizeof(win
.sin6_addr
) );
401 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
402 uaddr
->in6
.sin6_scope_id
= win
.sin6_scope_id
;
404 return sizeof(uaddr
->in6
);
410 struct WS_sockaddr_ipx win
= {0};
412 if (wsaddrlen
< sizeof(win
)) return 0;
413 memcpy( &win
, wsaddr
, sizeof(win
) );
414 uaddr
->ipx
.sipx_family
= AF_IPX
;
415 memcpy( &uaddr
->ipx
.sipx_network
, win
.sa_netnum
, sizeof(win
.sa_netnum
) );
416 memcpy( &uaddr
->ipx
.sipx_node
, win
.sa_nodenum
, sizeof(win
.sa_nodenum
) );
417 uaddr
->ipx
.sipx_port
= win
.sa_socket
;
418 return sizeof(uaddr
->ipx
);
425 SOCKADDR_IRDA win
= {0};
426 unsigned int lsap_sel
;
428 if (wsaddrlen
< sizeof(win
)) return 0;
429 memcpy( &win
, wsaddr
, sizeof(win
) );
430 uaddr
->irda
.sir_family
= AF_IRDA
;
431 if (sscanf( win
.irdaServiceName
, "LSAP-SEL%u", &lsap_sel
) == 1)
432 uaddr
->irda
.sir_lsap_sel
= lsap_sel
;
435 uaddr
->irda
.sir_lsap_sel
= LSAP_ANY
;
436 memcpy( uaddr
->irda
.sir_name
, win
.irdaServiceName
, sizeof(win
.irdaServiceName
) );
438 memcpy( &uaddr
->irda
.sir_addr
, win
.irdaDeviceID
, sizeof(win
.irdaDeviceID
) );
439 return sizeof(uaddr
->irda
);
446 default: /* likely an ipv4 address */
447 case sizeof(struct WS_sockaddr_in
):
448 return sizeof(uaddr
->in
);
451 case sizeof(struct WS_sockaddr_ipx
):
452 return sizeof(uaddr
->ipx
);
456 case sizeof(SOCKADDR_IRDA
):
457 return sizeof(uaddr
->irda
);
460 case sizeof(struct WS_sockaddr_in6
):
461 return sizeof(uaddr
->in6
);
469 /* some events are generated at the same time but must be sent in a particular
470 * order (e.g. CONNECT must be sent before READ) */
471 static const enum afd_poll_bit event_bitorder
[] =
473 AFD_POLL_BIT_CONNECT
,
474 AFD_POLL_BIT_CONNECT_ERR
,
485 SOCK_SHUTDOWN_ERROR
= -1,
486 SOCK_SHUTDOWN_EOF
= 0,
487 SOCK_SHUTDOWN_POLLHUP
= 1
490 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
492 static sock_shutdown_t
sock_check_pollhup(void)
494 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
499 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
500 if ( shutdown( fd
[0], 1 ) ) goto out
;
506 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
507 n
= poll( &pfd
, 1, 1 );
508 if ( n
!= 1 ) goto out
; /* error or timeout */
509 if ( pfd
.revents
& POLLHUP
)
510 ret
= SOCK_SHUTDOWN_POLLHUP
;
511 else if ( pfd
.revents
& POLLIN
&&
512 read( fd
[1], &dummy
, 1 ) == 0 )
513 ret
= SOCK_SHUTDOWN_EOF
;
523 sock_shutdown_type
= sock_check_pollhup();
525 switch ( sock_shutdown_type
)
527 case SOCK_SHUTDOWN_EOF
:
528 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
530 case SOCK_SHUTDOWN_POLLHUP
:
531 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
534 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
535 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
539 static int sock_reselect( struct sock
*sock
)
541 int ev
= sock_get_poll_events( sock
->fd
);
544 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
546 set_fd_events( sock
->fd
, ev
);
550 static unsigned int afd_poll_flag_to_win32( unsigned int flags
)
552 static const unsigned int map
[] =
556 FD_WRITE
, /* WRITE */
558 FD_CLOSE
, /* RESET */
560 FD_CONNECT
, /* CONNECT */
561 FD_ACCEPT
, /* ACCEPT */
562 FD_CONNECT
, /* CONNECT_ERR */
565 unsigned int i
, ret
= 0;
567 for (i
= 0; i
< ARRAY_SIZE(map
); ++i
)
569 if (flags
& (1 << i
)) ret
|= map
[i
];
575 /* wake anybody waiting on the socket event or send the associated message */
576 static void sock_wake_up( struct sock
*sock
)
578 unsigned int events
= sock
->pending_events
& sock
->mask
;
583 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
585 set_event( sock
->event
);
589 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
590 for (i
= 0; i
< ARRAY_SIZE(event_bitorder
); i
++)
592 enum afd_poll_bit event
= event_bitorder
[i
];
593 if (events
& (1 << event
))
595 lparam_t lparam
= afd_poll_flag_to_win32(1 << event
) | (sock_get_error( sock
->errors
[event
] ) << 16);
596 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
599 sock
->pending_events
= 0;
600 sock_reselect( sock
);
604 static inline int sock_error( struct fd
*fd
)
606 unsigned int optval
= 0;
607 socklen_t optlen
= sizeof(optval
);
609 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
613 static void free_accept_req( void *private )
615 struct accept_req
*req
= private;
616 list_remove( &req
->entry
);
619 req
->acceptsock
->accept_recv_req
= NULL
;
620 release_object( req
->acceptsock
);
622 release_object( req
->async
);
623 release_object( req
->iosb
);
624 release_object( req
->sock
);
628 static void fill_accept_output( struct accept_req
*req
)
630 const data_size_t out_size
= req
->iosb
->out_size
;
631 struct async
*async
= req
->async
;
632 union unix_sockaddr unix_addr
;
633 struct WS_sockaddr
*win_addr
;
634 unsigned int remote_len
;
640 if (!(out_data
= mem_alloc( out_size
)))
642 async_terminate( async
, get_error() );
646 fd
= get_unix_fd( req
->acceptsock
->fd
);
648 if (req
->recv_len
&& (size
= recv( fd
, out_data
, req
->recv_len
, 0 )) < 0)
650 if (!req
->accepted
&& errno
== EWOULDBLOCK
)
653 sock_reselect( req
->acceptsock
);
657 async_terminate( async
, sock_get_ntstatus( errno
) );
664 if (req
->local_len
< sizeof(int))
666 async_terminate( async
, STATUS_BUFFER_TOO_SMALL
);
671 unix_len
= sizeof(unix_addr
);
672 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ sizeof(int));
673 if (getsockname( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
674 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, req
->local_len
- sizeof(int) )) < 0)
676 async_terminate( async
, sock_get_ntstatus( errno
) );
680 memcpy( out_data
+ req
->recv_len
, &win_len
, sizeof(int) );
683 unix_len
= sizeof(unix_addr
);
684 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ req
->local_len
+ sizeof(int));
685 remote_len
= out_size
- req
->recv_len
- req
->local_len
;
686 if (getpeername( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
687 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, remote_len
- sizeof(int) )) < 0)
689 async_terminate( async
, sock_get_ntstatus( errno
) );
693 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
695 async_request_complete( req
->async
, STATUS_SUCCESS
, size
, out_size
, out_data
);
698 static void complete_async_accept( struct sock
*sock
, struct accept_req
*req
)
700 struct sock
*acceptsock
= req
->acceptsock
;
701 struct async
*async
= req
->async
;
703 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
707 if (!accept_into_socket( sock
, acceptsock
))
709 async_terminate( async
, get_error() );
712 fill_accept_output( req
);
718 if (!(acceptsock
= accept_socket( sock
)))
720 async_terminate( async
, get_error() );
723 handle
= alloc_handle_no_access_check( async_get_thread( async
)->process
, &acceptsock
->obj
,
724 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
725 acceptsock
->wparam
= handle
;
726 release_object( acceptsock
);
729 async_terminate( async
, get_error() );
733 async_request_complete_alloc( req
->async
, STATUS_SUCCESS
, 0, sizeof(handle
), &handle
);
737 static void complete_async_accept_recv( struct accept_req
*req
)
739 if (debug_level
) fprintf( stderr
, "completing accept recv request for socket %p\n", req
->acceptsock
);
741 assert( req
->recv_len
);
743 fill_accept_output( req
);
746 static void free_connect_req( void *private )
748 struct connect_req
*req
= private;
750 req
->sock
->connect_req
= NULL
;
751 release_object( req
->async
);
752 release_object( req
->iosb
);
753 release_object( req
->sock
);
757 static void complete_async_connect( struct sock
*sock
)
759 struct connect_req
*req
= sock
->connect_req
;
760 const char *in_buffer
;
764 if (debug_level
) fprintf( stderr
, "completing connect request for socket %p\n", sock
);
766 sock
->state
= SOCK_CONNECTED
;
770 async_terminate( req
->async
, STATUS_SUCCESS
);
774 in_buffer
= (const char *)req
->iosb
->in_data
+ sizeof(struct afd_connect_params
) + req
->addr_len
;
775 len
= req
->send_len
- req
->send_cursor
;
777 ret
= send( get_unix_fd( sock
->fd
), in_buffer
+ req
->send_cursor
, len
, 0 );
778 if (ret
< 0 && errno
!= EWOULDBLOCK
)
779 async_terminate( req
->async
, sock_get_ntstatus( errno
) );
781 async_request_complete( req
->async
, STATUS_SUCCESS
, req
->send_len
, 0, NULL
);
783 req
->send_cursor
+= ret
;
786 static void free_poll_req( void *private )
788 struct poll_req
*req
= private;
791 if (req
->timeout
) remove_timeout_user( req
->timeout
);
793 for (i
= 0; i
< req
->count
; ++i
)
794 release_object( req
->sockets
[i
].sock
);
795 release_object( req
->async
);
796 release_object( req
->iosb
);
797 list_remove( &req
->entry
);
802 static int is_oobinline( struct sock
*sock
)
805 socklen_t len
= sizeof(oobinline
);
806 return !getsockopt( get_unix_fd( sock
->fd
), SOL_SOCKET
, SO_OOBINLINE
, (char *)&oobinline
, &len
) && oobinline
;
809 static int get_poll_flags( struct sock
*sock
, int event
)
813 /* A connection-mode socket which has never been connected does not return
814 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
815 if (sock
->state
== SOCK_UNCONNECTED
)
816 event
&= ~(POLLOUT
| POLLHUP
);
820 if (sock
->state
== SOCK_LISTENING
)
821 flags
|= AFD_POLL_ACCEPT
;
823 flags
|= AFD_POLL_READ
;
826 flags
|= is_oobinline( sock
) ? AFD_POLL_READ
: AFD_POLL_OOB
;
828 flags
|= AFD_POLL_WRITE
;
829 if (sock
->state
== SOCK_CONNECTED
)
830 flags
|= AFD_POLL_CONNECT
;
832 flags
|= AFD_POLL_HUP
;
834 flags
|= AFD_POLL_CONNECT_ERR
;
839 static void complete_async_poll( struct poll_req
*req
, unsigned int status
)
843 for (i
= 0; i
< req
->count
; ++i
)
845 struct sock
*sock
= req
->sockets
[i
].sock
;
847 if (sock
->main_poll
== req
)
848 sock
->main_poll
= NULL
;
851 /* pass 0 as result; client will set actual result size */
852 async_request_complete( req
->async
, status
, 0, req
->count
* sizeof(*req
->output
), req
->output
);
856 static void complete_async_polls( struct sock
*sock
, int event
, int error
)
858 int flags
= get_poll_flags( sock
, event
);
859 struct poll_req
*req
, *next
;
861 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &poll_list
, struct poll_req
, entry
)
865 if (req
->iosb
->status
!= STATUS_PENDING
) continue;
867 for (i
= 0; i
< req
->count
; ++i
)
869 if (req
->sockets
[i
].sock
!= sock
) continue;
870 if (!(req
->sockets
[i
].flags
& flags
)) continue;
873 fprintf( stderr
, "completing poll for socket %p, wanted %#x got %#x\n",
874 sock
, req
->sockets
[i
].flags
, flags
);
876 req
->output
[i
].flags
= req
->sockets
[i
].flags
& flags
;
877 req
->output
[i
].status
= sock_get_ntstatus( error
);
879 complete_async_poll( req
, STATUS_SUCCESS
);
885 static void async_poll_timeout( void *private )
887 struct poll_req
*req
= private;
891 if (req
->iosb
->status
!= STATUS_PENDING
) return;
893 complete_async_poll( req
, STATUS_TIMEOUT
);
896 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
898 if (event
& (POLLIN
| POLLPRI
))
900 struct accept_req
*req
;
902 LIST_FOR_EACH_ENTRY( req
, &sock
->accept_list
, struct accept_req
, entry
)
904 if (req
->iosb
->status
== STATUS_PENDING
&& !req
->accepted
)
906 complete_async_accept( sock
, req
);
911 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
912 complete_async_accept_recv( sock
->accept_recv_req
);
915 if ((event
& POLLOUT
) && sock
->connect_req
&& sock
->connect_req
->iosb
->status
== STATUS_PENDING
)
916 complete_async_connect( sock
);
918 if (event
& (POLLIN
| POLLPRI
) && async_waiting( &sock
->read_q
))
920 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
921 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
922 event
&= ~(POLLIN
| POLLPRI
);
925 if (event
& POLLOUT
&& async_waiting( &sock
->write_q
))
927 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
928 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
932 if (event
& (POLLERR
| POLLHUP
))
934 int status
= sock_get_ntstatus( error
);
935 struct accept_req
*req
, *next
;
937 if (sock
->rd_shutdown
|| sock
->hangup
)
938 async_wake_up( &sock
->read_q
, status
);
939 if (sock
->wr_shutdown
)
940 async_wake_up( &sock
->write_q
, status
);
942 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
944 if (req
->iosb
->status
== STATUS_PENDING
)
945 async_terminate( req
->async
, status
);
948 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
949 async_terminate( sock
->accept_recv_req
->async
, status
);
951 if (sock
->connect_req
)
952 async_terminate( sock
->connect_req
->async
, status
);
958 static void post_socket_event( struct sock
*sock
, enum afd_poll_bit event_bit
, int error
)
960 unsigned int event
= (1 << event_bit
);
962 if (!(sock
->reported_events
& event
))
964 sock
->pending_events
|= event
;
965 sock
->reported_events
|= event
;
966 sock
->errors
[event_bit
] = error
;
970 static void sock_dispatch_events( struct sock
*sock
, enum connection_state prevstate
, int event
, int error
)
974 case SOCK_UNCONNECTED
:
977 case SOCK_CONNECTING
:
980 post_socket_event( sock
, AFD_POLL_BIT_CONNECT
, 0 );
981 sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] = 0;
983 if (event
& (POLLERR
| POLLHUP
))
984 post_socket_event( sock
, AFD_POLL_BIT_CONNECT_ERR
, error
);
988 if (event
& (POLLIN
| POLLERR
| POLLHUP
))
989 post_socket_event( sock
, AFD_POLL_BIT_ACCEPT
, error
);
993 case SOCK_CONNECTIONLESS
:
995 post_socket_event( sock
, AFD_POLL_BIT_READ
, 0 );
998 post_socket_event( sock
, AFD_POLL_BIT_WRITE
, 0 );
1000 if (event
& POLLPRI
)
1001 post_socket_event( sock
, AFD_POLL_BIT_OOB
, 0 );
1003 if (event
& (POLLERR
| POLLHUP
))
1004 post_socket_event( sock
, AFD_POLL_BIT_HUP
, error
);
1008 sock_wake_up( sock
);
1011 static void sock_poll_event( struct fd
*fd
, int event
)
1013 struct sock
*sock
= get_fd_user( fd
);
1014 int hangup_seen
= 0;
1015 enum connection_state prevstate
= sock
->state
;
1018 assert( sock
->obj
.ops
== &sock_ops
);
1020 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
1022 /* we may change event later, remove from loop here */
1023 if (event
& (POLLERR
|POLLHUP
)) set_fd_events( sock
->fd
, -1 );
1025 switch (sock
->state
)
1027 case SOCK_UNCONNECTED
:
1030 case SOCK_CONNECTING
:
1031 if (event
& (POLLERR
|POLLHUP
))
1033 sock
->state
= SOCK_UNCONNECTED
;
1035 error
= sock_error( fd
);
1037 else if (event
& POLLOUT
)
1039 sock
->state
= SOCK_CONNECTED
;
1040 sock
->connect_time
= current_time
;
1044 case SOCK_LISTENING
:
1045 if (event
& (POLLERR
|POLLHUP
))
1046 error
= sock_error( fd
);
1049 case SOCK_CONNECTED
:
1050 case SOCK_CONNECTIONLESS
:
1051 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
1056 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
1057 * has been closed, so we need to check for it explicitly here */
1058 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
1067 /* EAGAIN can happen if an async recv() falls between the server's poll()
1068 call and the invocation of this routine */
1069 if ( errno
!= EAGAIN
)
1074 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
1079 if (hangup_seen
|| (sock_shutdown_type
== SOCK_SHUTDOWN_POLLHUP
&& (event
& POLLHUP
)))
1083 else if (event
& (POLLHUP
| POLLERR
))
1088 fprintf( stderr
, "socket %p aborted by error %d, event %#x\n", sock
, error
, event
);
1096 complete_async_polls( sock
, event
, error
);
1098 event
= sock_dispatch_asyncs( sock
, event
, error
);
1099 sock_dispatch_events( sock
, prevstate
, event
, error
);
1101 sock_reselect( sock
);
1104 static void sock_dump( struct object
*obj
, int verbose
)
1106 struct sock
*sock
= (struct sock
*)obj
;
1107 assert( obj
->ops
== &sock_ops
);
1108 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
1109 sock
->fd
, sock
->state
,
1110 sock
->mask
, sock
->pending_events
, sock
->reported_events
);
1113 static int poll_flags_from_afd( struct sock
*sock
, int flags
)
1117 /* A connection-mode socket which has never been connected does
1118 * not return write or hangup events, but Linux returns
1119 * POLLOUT | POLLHUP. */
1120 if (sock
->state
== SOCK_UNCONNECTED
)
1123 if (flags
& (AFD_POLL_READ
| AFD_POLL_ACCEPT
))
1125 if ((flags
& AFD_POLL_HUP
) && sock
->type
== WS_SOCK_STREAM
)
1127 if (flags
& AFD_POLL_OOB
)
1128 ev
|= is_oobinline( sock
) ? POLLIN
: POLLPRI
;
1129 if (flags
& AFD_POLL_WRITE
)
1135 static int sock_get_poll_events( struct fd
*fd
)
1137 struct sock
*sock
= get_fd_user( fd
);
1138 unsigned int mask
= sock
->mask
& ~sock
->reported_events
;
1139 struct poll_req
*req
;
1142 assert( sock
->obj
.ops
== &sock_ops
);
1144 if (!sock
->type
) /* not initialized yet */
1147 switch (sock
->state
)
1149 case SOCK_UNCONNECTED
:
1150 /* A connection-mode Windows socket which has never been connected does
1151 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1152 * need to return -1 here, to prevent the socket from being polled on at
1156 case SOCK_CONNECTING
:
1159 case SOCK_LISTENING
:
1160 if (!list_empty( &sock
->accept_list
) || (mask
& AFD_POLL_ACCEPT
))
1164 case SOCK_CONNECTED
:
1165 case SOCK_CONNECTIONLESS
:
1166 if (sock
->hangup
&& sock
->wr_shutdown
&& !sock
->wr_shutdown_pending
)
1168 /* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
1169 * if both the socket and its peer are SHUT_WR.
1171 * We don't use SHUT_RD, so we can only encounter this in the latter
1172 * case. In that case there can't be any pending read requests (they
1173 * would have already been completed with a length of zero), the
1174 * above condition ensures that we don't have any pending write
1175 * requests, and nothing that can change about the socket state that
1176 * would complete a pending poll request. */
1183 if (sock
->accept_recv_req
)
1187 else if (async_queued( &sock
->read_q
))
1189 if (async_waiting( &sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
1193 /* Don't ask for POLLIN if we got a hangup. We won't receive more
1194 * data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
1197 if (mask
& AFD_POLL_READ
)
1199 if (mask
& AFD_POLL_OOB
)
1203 /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
1204 if (sock
->state
== SOCK_CONNECTED
&& (mask
& AFD_POLL_HUP
) && !(sock
->reported_events
& AFD_POLL_READ
))
1208 if (async_queued( &sock
->write_q
))
1210 if (async_waiting( &sock
->write_q
)) ev
|= POLLOUT
;
1212 else if (!sock
->wr_shutdown
&& (mask
& AFD_POLL_WRITE
))
1220 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1224 for (i
= 0; i
< req
->count
; ++i
)
1226 if (req
->sockets
[i
].sock
!= sock
) continue;
1228 ev
|= poll_flags_from_afd( sock
, req
->sockets
[i
].flags
);
1235 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
1237 return FD_TYPE_SOCKET
;
1240 static void sock_cancel_async( struct fd
*fd
, struct async
*async
)
1242 struct poll_req
*req
;
1244 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1248 if (req
->async
!= async
)
1251 for (i
= 0; i
< req
->count
; i
++)
1253 struct sock
*sock
= req
->sockets
[i
].sock
;
1255 if (sock
->main_poll
== req
)
1256 sock
->main_poll
= NULL
;
1260 async_terminate( async
, STATUS_CANCELLED
);
1263 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
1265 struct sock
*sock
= get_fd_user( fd
);
1266 struct async_queue
*queue
;
1268 assert( sock
->obj
.ops
== &sock_ops
);
1272 case ASYNC_TYPE_READ
:
1273 if (sock
->rd_shutdown
)
1275 set_error( STATUS_PIPE_DISCONNECTED
);
1278 queue
= &sock
->read_q
;
1281 case ASYNC_TYPE_WRITE
:
1282 if (sock
->wr_shutdown
)
1284 set_error( STATUS_PIPE_DISCONNECTED
);
1287 queue
= &sock
->write_q
;
1291 set_error( STATUS_INVALID_PARAMETER
);
1295 if (sock
->state
!= SOCK_CONNECTED
)
1297 set_error( STATUS_PIPE_DISCONNECTED
);
1301 queue_async( queue
, async
);
1302 sock_reselect( sock
);
1304 set_error( STATUS_PENDING
);
1307 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
1309 struct sock
*sock
= get_fd_user( fd
);
1311 if (sock
->wr_shutdown_pending
&& list_empty( &sock
->write_q
.queue
))
1313 shutdown( get_unix_fd( sock
->fd
), SHUT_WR
);
1314 sock
->wr_shutdown_pending
= 0;
1317 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1318 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1320 if (queue
!= &sock
->ifchange_q
&& sock
->type
)
1321 sock_reselect( sock
);
1324 static struct fd
*sock_get_fd( struct object
*obj
)
1326 struct sock
*sock
= (struct sock
*)obj
;
1327 return (struct fd
*)grab_object( sock
->fd
);
1330 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
1332 struct sock
*sock
= (struct sock
*)obj
;
1334 if (sock
->obj
.handle_count
== 1) /* last handle */
1336 struct accept_req
*accept_req
, *accept_next
;
1337 struct poll_req
*poll_req
, *poll_next
;
1339 if (sock
->accept_recv_req
)
1340 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
1342 LIST_FOR_EACH_ENTRY_SAFE( accept_req
, accept_next
, &sock
->accept_list
, struct accept_req
, entry
)
1343 async_terminate( accept_req
->async
, STATUS_CANCELLED
);
1345 if (sock
->connect_req
)
1346 async_terminate( sock
->connect_req
->async
, STATUS_CANCELLED
);
1348 LIST_FOR_EACH_ENTRY_SAFE( poll_req
, poll_next
, &poll_list
, struct poll_req
, entry
)
1350 struct iosb
*iosb
= poll_req
->iosb
;
1351 BOOL signaled
= FALSE
;
1354 if (iosb
->status
!= STATUS_PENDING
) continue;
1356 for (i
= 0; i
< poll_req
->count
; ++i
)
1358 if (poll_req
->sockets
[i
].sock
== sock
)
1361 poll_req
->output
[i
].flags
= AFD_POLL_CLOSE
;
1362 poll_req
->output
[i
].status
= 0;
1366 if (signaled
) complete_async_poll( poll_req
, STATUS_SUCCESS
);
1373 static void sock_destroy( struct object
*obj
)
1375 struct sock
*sock
= (struct sock
*)obj
;
1377 assert( obj
->ops
== &sock_ops
);
1379 /* FIXME: special socket shutdown stuff? */
1381 if ( sock
->deferred
)
1382 release_object( sock
->deferred
);
1384 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
1385 sock_release_ifchange( sock
);
1386 free_async_queue( &sock
->read_q
);
1387 free_async_queue( &sock
->write_q
);
1388 free_async_queue( &sock
->ifchange_q
);
1389 free_async_queue( &sock
->accept_q
);
1390 free_async_queue( &sock
->connect_q
);
1391 free_async_queue( &sock
->poll_q
);
1392 if (sock
->event
) release_object( sock
->event
);
1395 /* shut the socket down to force pending poll() calls in the client to return */
1396 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
1397 release_object( sock
->fd
);
1401 static struct sock
*create_socket(void)
1405 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
1407 sock
->state
= SOCK_UNCONNECTED
;
1409 sock
->pending_events
= 0;
1410 sock
->reported_events
= 0;
1419 sock
->connect_time
= 0;
1420 sock
->deferred
= NULL
;
1421 sock
->ifchange_obj
= NULL
;
1422 sock
->accept_recv_req
= NULL
;
1423 sock
->connect_req
= NULL
;
1424 sock
->main_poll
= NULL
;
1425 memset( &sock
->addr
, 0, sizeof(sock
->addr
) );
1427 sock
->rd_shutdown
= 0;
1428 sock
->wr_shutdown
= 0;
1429 sock
->wr_shutdown_pending
= 0;
1432 sock
->nonblocking
= 0;
1438 init_async_queue( &sock
->read_q
);
1439 init_async_queue( &sock
->write_q
);
1440 init_async_queue( &sock
->ifchange_q
);
1441 init_async_queue( &sock
->accept_q
);
1442 init_async_queue( &sock
->connect_q
);
1443 init_async_queue( &sock
->poll_q
);
1444 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
1445 list_init( &sock
->accept_list
);
1449 static int get_unix_family( int family
)
1453 case WS_AF_INET
: return AF_INET
;
1454 case WS_AF_INET6
: return AF_INET6
;
1456 case WS_AF_IPX
: return AF_IPX
;
1459 case WS_AF_IRDA
: return AF_IRDA
;
1461 case WS_AF_UNSPEC
: return AF_UNSPEC
;
1466 static int get_unix_type( int type
)
1470 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
1471 case WS_SOCK_RAW
: return SOCK_RAW
;
1472 case WS_SOCK_STREAM
: return SOCK_STREAM
;
1477 static int get_unix_protocol( int protocol
)
1479 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1484 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
1485 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1486 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1487 case WS_IPPROTO_IPV4
: return IPPROTO_IPIP
;
1488 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1489 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1490 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1491 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1496 static void set_dont_fragment( int fd
, int level
, int value
)
1500 if (level
== IPPROTO_IP
)
1503 optname
= IP_DONTFRAG
;
1504 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1505 optname
= IP_MTU_DISCOVER
;
1506 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1513 #ifdef IPV6_DONTFRAG
1514 optname
= IPV6_DONTFRAG
;
1515 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1516 optname
= IPV6_MTU_DISCOVER
;
1517 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1523 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1526 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
, unsigned int flags
)
1528 unsigned int options
= 0;
1529 int sockfd
, unix_type
, unix_family
, unix_protocol
, value
;
1532 unix_family
= get_unix_family( family
);
1533 unix_type
= get_unix_type( type
);
1534 unix_protocol
= get_unix_protocol( protocol
);
1536 if (unix_protocol
< 0)
1538 if (type
&& unix_type
< 0)
1539 set_win32_error( WSAESOCKTNOSUPPORT
);
1541 set_win32_error( WSAEPROTONOSUPPORT
);
1544 if (unix_family
< 0)
1546 if (family
>= 0 && unix_type
< 0)
1547 set_win32_error( WSAESOCKTNOSUPPORT
);
1549 set_win32_error( WSAEAFNOSUPPORT
);
1553 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1556 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1557 else set_win32_error( sock_get_error( errno
));
1560 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1562 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1565 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1568 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1571 /* Should we retrieve val using a getsockopt call and then
1572 * set the modified one? */
1573 val
.ipx_pt
= ipx_type
;
1574 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1579 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1581 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1582 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1583 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1584 else if (unix_type
== SOCK_STREAM
)
1585 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1589 if (unix_family
== AF_INET6
)
1591 static const int enable
= 1;
1592 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1596 len
= sizeof(value
);
1597 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_RCVBUF
, &value
, &len
))
1598 sock
->rcvbuf
= value
;
1600 len
= sizeof(value
);
1601 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_SNDBUF
, &value
, &len
))
1602 sock
->sndbuf
= value
;
1604 sock
->state
= (type
== WS_SOCK_STREAM
? SOCK_UNCONNECTED
: SOCK_CONNECTIONLESS
);
1605 sock
->flags
= flags
;
1606 sock
->proto
= protocol
;
1608 sock
->family
= family
;
1612 options
= get_fd_options( sock
->fd
);
1613 release_object( sock
->fd
);
1616 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1621 /* We can't immediately allow caching for a connection-mode socket, since it
1622 * might be accepted into (changing the underlying fd object.) */
1623 if (sock
->type
!= WS_SOCK_STREAM
) allow_fd_caching( sock
->fd
);
1628 /* accepts a socket and inits it */
1629 static int accept_new_fd( struct sock
*sock
)
1632 /* Try to accept(2). We can't be safe that this an already connected socket
1633 * or that accept() is allowed on it. In those cases we will get -1/errno
1636 struct sockaddr saddr
;
1637 socklen_t slen
= sizeof(saddr
);
1638 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1640 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1642 set_error( sock_get_ntstatus( errno
));
1646 /* accept a socket (creates a new fd) */
1647 static struct sock
*accept_socket( struct sock
*sock
)
1649 struct sock
*acceptsock
;
1652 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1654 if ( sock
->deferred
)
1656 acceptsock
= sock
->deferred
;
1657 sock
->deferred
= NULL
;
1661 union unix_sockaddr unix_addr
;
1664 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1665 if (!(acceptsock
= create_socket()))
1671 /* newly created socket gets the same properties of the listening socket */
1672 acceptsock
->state
= SOCK_CONNECTED
;
1673 acceptsock
->bound
= 1;
1674 acceptsock
->nonblocking
= sock
->nonblocking
;
1675 acceptsock
->mask
= sock
->mask
;
1676 acceptsock
->proto
= sock
->proto
;
1677 acceptsock
->type
= sock
->type
;
1678 acceptsock
->family
= sock
->family
;
1679 acceptsock
->window
= sock
->window
;
1680 acceptsock
->message
= sock
->message
;
1681 acceptsock
->connect_time
= current_time
;
1682 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
1683 acceptsock
->flags
= sock
->flags
;
1684 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1685 get_fd_options( sock
->fd
) )))
1687 release_object( acceptsock
);
1690 unix_len
= sizeof(unix_addr
);
1691 if (!getsockname( acceptfd
, &unix_addr
.addr
, &unix_len
))
1692 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1695 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1696 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1697 sock_reselect( sock
);
1701 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
1703 union unix_sockaddr unix_addr
;
1708 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
1710 if ( sock
->deferred
)
1712 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
1713 get_fd_options( acceptsock
->fd
) );
1717 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
1719 release_object( sock
->deferred
);
1720 sock
->deferred
= NULL
;
1724 if ((acceptfd
= accept_new_fd( sock
)) == -1)
1727 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1728 get_fd_options( acceptsock
->fd
) )))
1732 acceptsock
->state
= SOCK_CONNECTED
;
1733 acceptsock
->pending_events
= 0;
1734 acceptsock
->reported_events
= 0;
1735 acceptsock
->proto
= sock
->proto
;
1736 acceptsock
->type
= sock
->type
;
1737 acceptsock
->family
= sock
->family
;
1738 acceptsock
->wparam
= 0;
1739 acceptsock
->deferred
= NULL
;
1740 acceptsock
->connect_time
= current_time
;
1741 fd_copy_completion( acceptsock
->fd
, newfd
);
1742 release_object( acceptsock
->fd
);
1743 acceptsock
->fd
= newfd
;
1745 unix_len
= sizeof(unix_addr
);
1746 if (!getsockname( get_unix_fd( newfd
), &unix_addr
.addr
, &unix_len
))
1747 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1750 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1751 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1752 sock_reselect( sock
);
1759 static int bind_to_index( int fd
, in_addr_t bind_addr
, unsigned int index
)
1761 return setsockopt( fd
, IPPROTO_IP
, IP_BOUND_IF
, &index
, sizeof(index
) );
1764 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER)
1766 struct interface_filter
1768 struct sock_filter iface_memaddr
;
1769 struct sock_filter iface_rule
;
1770 struct sock_filter ip_memaddr
;
1771 struct sock_filter ip_rule
;
1772 struct sock_filter return_keep
;
1773 struct sock_filter return_dump
;
1775 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
1776 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1777 /sizeof(struct sock_filter)
1778 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
1779 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1780 /sizeof(struct sock_filter)
1781 # define FILTER_JUMP_NEXT() (u_char)(0)
1782 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
1783 static struct interface_filter generic_interface_filter
=
1785 /* This filter rule allows incoming packets on the specified interface, which works for all
1786 * remotely generated packets and for locally generated broadcast packets. */
1787 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
1788 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
1789 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
1790 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
1791 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
1792 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
1793 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
1794 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
1797 static int bind_to_index( int fd
, in_addr_t bind_addr
, unsigned int index
)
1799 in_addr_t ifindex
= htonl( index
);
1800 struct interface_filter specific_interface_filter
;
1801 struct sock_fprog filter_prog
;
1804 if ((ret
= setsockopt( fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
) )) < 0)
1807 specific_interface_filter
= generic_interface_filter
;
1808 specific_interface_filter
.iface_rule
.k
= index
;
1809 specific_interface_filter
.ip_rule
.k
= htonl( bind_addr
);
1810 filter_prog
.len
= sizeof(generic_interface_filter
) / sizeof(struct sock_filter
);
1811 filter_prog
.filter
= (struct sock_filter
*)&specific_interface_filter
;
1812 return setsockopt( fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
) );
1817 static int bind_to_index( int fd
, in_addr_t bind_addr
, unsigned int index
)
1823 #endif /* LINUX_BOUND_IF */
1825 /* Take bind() calls on any name corresponding to a local network adapter and
1826 * restrict the given socket to operating only on the specified interface. This
1827 * restriction consists of two components:
1828 * 1) An outgoing packet restriction suggesting the egress interface for all
1830 * 2) An incoming packet restriction dropping packets not meant for the
1832 * If the function succeeds in placing these restrictions, then the name for the
1833 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
1834 * receipt of broadcast packets on the socket. This behavior is only relevant to
1835 * UDP sockets and is needed for applications that expect to be able to receive
1836 * broadcast packets on a socket that is bound to a specific network interface.
1838 static int bind_to_interface( struct sock
*sock
, const struct sockaddr_in
*addr
)
1840 in_addr_t bind_addr
= addr
->sin_addr
.s_addr
;
1841 struct ifaddrs
*ifaddrs
, *ifaddr
;
1842 int fd
= get_unix_fd( sock
->fd
);
1843 static const int enable
= 1;
1846 if (bind_addr
== htonl( INADDR_ANY
) || bind_addr
== htonl( INADDR_LOOPBACK
))
1848 if (sock
->type
!= WS_SOCK_DGRAM
)
1851 if (getifaddrs( &ifaddrs
) < 0) return 0;
1853 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
1855 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET
1856 && ((struct sockaddr_in
*)ifaddr
->ifa_addr
)->sin_addr
.s_addr
== bind_addr
)
1858 index
= if_nametoindex( ifaddr
->ifa_name
);
1862 fprintf( stderr
, "Unable to look up interface index for %s: %s\n",
1863 ifaddr
->ifa_name
, strerror( errno
) );
1867 freeifaddrs( ifaddrs
);
1869 if (bind_to_index( fd
, bind_addr
, index
) < 0)
1872 fprintf( stderr
, "failed to bind to interface: %s\n", strerror( errno
) );
1876 if (setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) ) < 0)
1879 fprintf( stderr
, "failed to reuse address: %s\n", strerror( errno
) );
1886 freeifaddrs( ifaddrs
);
1890 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1891 static unsigned int get_ipv6_interface_index( const struct in6_addr
*addr
)
1893 struct ifaddrs
*ifaddrs
, *ifaddr
;
1895 if (getifaddrs( &ifaddrs
) < 0) return 0;
1897 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
1899 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET6
1900 && !memcmp( &((struct sockaddr_in6
*)ifaddr
->ifa_addr
)->sin6_addr
, addr
, sizeof(*addr
) ))
1902 unsigned int index
= if_nametoindex( ifaddr
->ifa_name
);
1907 fprintf( stderr
, "Unable to look up interface index for %s: %s\n",
1908 ifaddr
->ifa_name
, strerror( errno
) );
1912 freeifaddrs( ifaddrs
);
1917 freeifaddrs( ifaddrs
);
1922 /* return an errno value mapped to a WSA error */
1923 static unsigned int sock_get_error( int err
)
1927 case EINTR
: return WSAEINTR
;
1928 case EBADF
: return WSAEBADF
;
1930 case EACCES
: return WSAEACCES
;
1931 case EFAULT
: return WSAEFAULT
;
1932 case EINVAL
: return WSAEINVAL
;
1933 case EMFILE
: return WSAEMFILE
;
1935 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
1936 case EALREADY
: return WSAEALREADY
;
1937 case ENOTSOCK
: return WSAENOTSOCK
;
1938 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
1939 case EMSGSIZE
: return WSAEMSGSIZE
;
1940 case EPROTOTYPE
: return WSAEPROTOTYPE
;
1941 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
1942 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
1943 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
1944 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
1945 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
1946 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
1947 case EADDRINUSE
: return WSAEADDRINUSE
;
1948 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
1949 case ENETDOWN
: return WSAENETDOWN
;
1950 case ENETUNREACH
: return WSAENETUNREACH
;
1951 case ENETRESET
: return WSAENETRESET
;
1952 case ECONNABORTED
: return WSAECONNABORTED
;
1954 case ECONNRESET
: return WSAECONNRESET
;
1955 case ENOBUFS
: return WSAENOBUFS
;
1956 case EISCONN
: return WSAEISCONN
;
1957 case ENOTCONN
: return WSAENOTCONN
;
1958 case ESHUTDOWN
: return WSAESHUTDOWN
;
1959 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
1960 case ETIMEDOUT
: return WSAETIMEDOUT
;
1961 case ECONNREFUSED
: return WSAECONNREFUSED
;
1962 case ELOOP
: return WSAELOOP
;
1963 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
1964 case EHOSTDOWN
: return WSAEHOSTDOWN
;
1965 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
1966 case ENOTEMPTY
: return WSAENOTEMPTY
;
1968 case EPROCLIM
: return WSAEPROCLIM
;
1971 case EUSERS
: return WSAEUSERS
;
1974 case EDQUOT
: return WSAEDQUOT
;
1977 case ESTALE
: return WSAESTALE
;
1980 case EREMOTE
: return WSAEREMOTE
;
1986 perror("wineserver: sock_get_error() can't map error");
1991 static int sock_get_ntstatus( int err
)
1995 case EBADF
: return STATUS_INVALID_HANDLE
;
1996 case EBUSY
: return STATUS_DEVICE_BUSY
;
1998 case EACCES
: return STATUS_ACCESS_DENIED
;
1999 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
2000 case EINVAL
: return STATUS_INVALID_PARAMETER
;
2002 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
2004 case EWOULDBLOCK
: return STATUS_DEVICE_NOT_READY
;
2005 case EALREADY
: return STATUS_NETWORK_BUSY
;
2006 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
2007 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
2008 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
2009 case EPROTONOSUPPORT
:
2010 case ESOCKTNOSUPPORT
:
2013 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
2014 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
2015 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
2016 case EADDRINUSE
: return STATUS_SHARING_VIOLATION
;
2017 /* Linux returns ENODEV when specifying an invalid sin6_scope_id;
2018 * Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
2020 case EADDRNOTAVAIL
: return STATUS_INVALID_ADDRESS_COMPONENT
;
2021 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
2022 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
2023 case ENOTCONN
: return STATUS_INVALID_CONNECTION
;
2024 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
2025 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
2026 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
2027 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
2029 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
2030 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
2031 case EISCONN
: return STATUS_CONNECTION_ACTIVE
;
2033 case 0: return STATUS_SUCCESS
;
2036 perror("wineserver: sock_get_ntstatus() can't map error");
2037 return STATUS_UNSUCCESSFUL
;
2041 static struct accept_req
*alloc_accept_req( struct sock
*sock
, struct sock
*acceptsock
, struct async
*async
,
2042 const struct afd_accept_into_params
*params
)
2044 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
2048 req
->async
= (struct async
*)grab_object( async
);
2049 req
->iosb
= async_get_iosb( async
);
2050 req
->sock
= (struct sock
*)grab_object( sock
);
2051 req
->acceptsock
= acceptsock
;
2052 if (acceptsock
) grab_object( acceptsock
);
2058 req
->recv_len
= params
->recv_len
;
2059 req
->local_len
= params
->local_len
;
2065 static void sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2067 struct sock
*sock
= get_fd_user( fd
);
2070 assert( sock
->obj
.ops
== &sock_ops
);
2072 if (code
!= IOCTL_AFD_WINE_CREATE
&& (unix_fd
= get_unix_fd( fd
)) < 0) return;
2076 case IOCTL_AFD_WINE_CREATE
:
2078 const struct afd_create_params
*params
= get_req_data();
2080 if (get_req_data_size() != sizeof(*params
))
2082 set_error( STATUS_INVALID_PARAMETER
);
2085 init_socket( sock
, params
->family
, params
->type
, params
->protocol
, params
->flags
);
2089 case IOCTL_AFD_WINE_ACCEPT
:
2091 struct sock
*acceptsock
;
2092 obj_handle_t handle
;
2094 if (get_reply_max_size() != sizeof(handle
))
2096 set_error( STATUS_BUFFER_TOO_SMALL
);
2100 if (!(acceptsock
= accept_socket( sock
)))
2102 struct accept_req
*req
;
2104 if (sock
->nonblocking
) return;
2105 if (get_error() != STATUS_DEVICE_NOT_READY
) return;
2107 if (!(req
= alloc_accept_req( sock
, NULL
, async
, NULL
))) return;
2108 list_add_tail( &sock
->accept_list
, &req
->entry
);
2110 async_set_completion_callback( async
, free_accept_req
, req
);
2111 queue_async( &sock
->accept_q
, async
);
2112 sock_reselect( sock
);
2113 set_error( STATUS_PENDING
);
2116 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
2117 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
2118 acceptsock
->wparam
= handle
;
2119 release_object( acceptsock
);
2120 set_reply_data( &handle
, sizeof(handle
) );
2124 case IOCTL_AFD_WINE_ACCEPT_INTO
:
2126 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
2127 const struct afd_accept_into_params
*params
= get_req_data();
2128 struct sock
*acceptsock
;
2129 unsigned int remote_len
;
2130 struct accept_req
*req
;
2132 if (get_req_data_size() != sizeof(*params
) ||
2133 get_reply_max_size() < params
->recv_len
||
2134 get_reply_max_size() - params
->recv_len
< params
->local_len
)
2136 set_error( STATUS_BUFFER_TOO_SMALL
);
2140 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
2141 if (remote_len
< sizeof(int))
2143 set_error( STATUS_INVALID_PARAMETER
);
2147 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
2150 if (acceptsock
->accept_recv_req
)
2152 release_object( acceptsock
);
2153 set_error( STATUS_INVALID_PARAMETER
);
2157 if (!(req
= alloc_accept_req( sock
, acceptsock
, async
, params
)))
2159 release_object( acceptsock
);
2162 list_add_tail( &sock
->accept_list
, &req
->entry
);
2163 acceptsock
->accept_recv_req
= req
;
2164 release_object( acceptsock
);
2166 acceptsock
->wparam
= params
->accept_handle
;
2167 async_set_completion_callback( async
, free_accept_req
, req
);
2168 queue_async( &sock
->accept_q
, async
);
2169 sock_reselect( sock
);
2170 set_error( STATUS_PENDING
);
2174 case IOCTL_AFD_LISTEN
:
2176 const struct afd_listen_params
*params
= get_req_data();
2178 if (get_req_data_size() < sizeof(*params
))
2180 set_error( STATUS_INVALID_PARAMETER
);
2186 set_error( STATUS_INVALID_PARAMETER
);
2190 if (listen( unix_fd
, params
->backlog
) < 0)
2192 set_error( sock_get_ntstatus( errno
) );
2196 sock
->state
= SOCK_LISTENING
;
2198 /* a listening socket can no longer be accepted into */
2199 allow_fd_caching( sock
->fd
);
2201 /* we may already be selecting for AFD_POLL_ACCEPT */
2202 sock_reselect( sock
);
2206 case IOCTL_AFD_WINE_CONNECT
:
2208 const struct afd_connect_params
*params
= get_req_data();
2209 const struct WS_sockaddr
*addr
;
2210 union unix_sockaddr unix_addr
;
2211 struct connect_req
*req
;
2215 if (get_req_data_size() < sizeof(*params
) ||
2216 get_req_data_size() - sizeof(*params
) < params
->addr_len
)
2218 set_error( STATUS_BUFFER_TOO_SMALL
);
2221 send_len
= get_req_data_size() - sizeof(*params
) - params
->addr_len
;
2222 addr
= (const struct WS_sockaddr
*)(params
+ 1);
2224 if (!params
->synchronous
&& !sock
->bound
)
2226 set_error( STATUS_INVALID_PARAMETER
);
2230 if (sock
->accept_recv_req
)
2232 set_error( STATUS_INVALID_PARAMETER
);
2236 if (sock
->connect_req
)
2238 set_error( STATUS_INVALID_PARAMETER
);
2242 switch (sock
->state
)
2244 case SOCK_LISTENING
:
2245 set_error( STATUS_INVALID_PARAMETER
);
2248 case SOCK_CONNECTING
:
2249 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2250 * but there's no status code that maps to WSAEALREADY... */
2251 set_error( params
->synchronous
? STATUS_ADDRESS_ALREADY_ASSOCIATED
: STATUS_INVALID_PARAMETER
);
2254 case SOCK_CONNECTED
:
2255 set_error( STATUS_CONNECTION_ACTIVE
);
2258 case SOCK_UNCONNECTED
:
2259 case SOCK_CONNECTIONLESS
:
2263 unix_len
= sockaddr_to_unix( addr
, params
->addr_len
, &unix_addr
);
2266 set_error( STATUS_INVALID_ADDRESS
);
2269 if (unix_addr
.addr
.sa_family
== AF_INET
&& !memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 ))
2270 unix_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_LOOPBACK
);
2272 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2273 if (ret
< 0 && errno
!= EINPROGRESS
)
2275 set_error( sock_get_ntstatus( errno
) );
2279 /* a connected or connecting socket can no longer be accepted into */
2280 allow_fd_caching( sock
->fd
);
2282 unix_len
= sizeof(unix_addr
);
2283 if (!sock
->bound
&& !getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
))
2284 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2289 sock
->state
= SOCK_CONNECTED
;
2291 if (!send_len
) return;
2294 sock
->state
= SOCK_CONNECTING
;
2296 if (params
->synchronous
&& sock
->nonblocking
)
2298 sock_reselect( sock
);
2299 set_error( STATUS_DEVICE_NOT_READY
);
2303 if (!(req
= mem_alloc( sizeof(*req
) )))
2306 req
->async
= (struct async
*)grab_object( async
);
2307 req
->iosb
= async_get_iosb( async
);
2308 req
->sock
= (struct sock
*)grab_object( sock
);
2309 req
->addr_len
= params
->addr_len
;
2310 req
->send_len
= send_len
;
2311 req
->send_cursor
= 0;
2313 async_set_completion_callback( async
, free_connect_req
, req
);
2314 sock
->connect_req
= req
;
2315 queue_async( &sock
->connect_q
, async
);
2316 sock_reselect( sock
);
2317 set_error( STATUS_PENDING
);
2321 case IOCTL_AFD_WINE_SHUTDOWN
:
2325 if (get_req_data_size() < sizeof(int))
2327 set_error( STATUS_BUFFER_TOO_SMALL
);
2330 how
= *(int *)get_req_data();
2334 set_error( STATUS_INVALID_PARAMETER
);
2338 if (sock
->state
!= SOCK_CONNECTED
&& sock
->state
!= SOCK_CONNECTIONLESS
)
2340 set_error( STATUS_INVALID_CONNECTION
);
2346 sock
->rd_shutdown
= 1;
2348 if (how
!= SD_RECEIVE
)
2350 sock
->wr_shutdown
= 1;
2351 if (list_empty( &sock
->write_q
.queue
))
2352 shutdown( unix_fd
, SHUT_WR
);
2354 sock
->wr_shutdown_pending
= 1;
2359 if (sock
->event
) release_object( sock
->event
);
2363 sock
->nonblocking
= 1;
2366 sock_reselect( sock
);
2370 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE
:
2374 if (get_req_data_size() < sizeof(int))
2376 set_error( STATUS_BUFFER_TOO_SMALL
);
2379 force_async
= *(int *)get_req_data();
2381 if (sock
->nonblocking
&& !force_async
)
2383 set_error( STATUS_DEVICE_NOT_READY
);
2386 if (!sock_get_ifchange( sock
)) return;
2387 queue_async( &sock
->ifchange_q
, async
);
2388 set_error( STATUS_PENDING
);
2392 case IOCTL_AFD_WINE_FIONBIO
:
2393 if (get_req_data_size() < sizeof(int))
2395 set_error( STATUS_BUFFER_TOO_SMALL
);
2398 if (*(int *)get_req_data())
2400 sock
->nonblocking
= 1;
2406 set_error( STATUS_INVALID_PARAMETER
);
2409 sock
->nonblocking
= 0;
2413 case IOCTL_AFD_GET_EVENTS
:
2415 struct afd_get_events_params params
= {0};
2418 if (get_reply_max_size() < sizeof(params
))
2420 set_error( STATUS_INVALID_PARAMETER
);
2424 params
.flags
= sock
->pending_events
& sock
->mask
;
2425 for (i
= 0; i
< ARRAY_SIZE( params
.status
); ++i
)
2426 params
.status
[i
] = sock_get_ntstatus( sock
->errors
[i
] );
2428 sock
->pending_events
= 0;
2429 sock_reselect( sock
);
2431 set_reply_data( ¶ms
, sizeof(params
) );
2435 case IOCTL_AFD_EVENT_SELECT
:
2437 struct event
*event
= NULL
;
2438 obj_handle_t event_handle
;
2441 set_async_pending( async
);
2443 if (is_machine_64bit( current
->process
->machine
))
2445 const struct afd_event_select_params_64
*params
= get_req_data();
2447 if (get_req_data_size() < sizeof(*params
))
2449 set_error( STATUS_INVALID_PARAMETER
);
2453 event_handle
= params
->event
;
2454 mask
= params
->mask
;
2458 const struct afd_event_select_params_32
*params
= get_req_data();
2460 if (get_req_data_size() < sizeof(*params
))
2462 set_error( STATUS_INVALID_PARAMETER
);
2466 event_handle
= params
->event
;
2467 mask
= params
->mask
;
2470 if ((event_handle
|| mask
) &&
2471 !(event
= get_event_obj( current
->process
, event_handle
, EVENT_MODIFY_STATE
)))
2473 set_error( STATUS_INVALID_PARAMETER
);
2477 if (sock
->event
) release_object( sock
->event
);
2478 sock
->event
= event
;
2483 sock
->nonblocking
= 1;
2485 sock_reselect( sock
);
2490 case IOCTL_AFD_WINE_MESSAGE_SELECT
:
2492 const struct afd_message_select_params
*params
= get_req_data();
2494 if (get_req_data_size() < sizeof(params
))
2496 set_error( STATUS_BUFFER_TOO_SMALL
);
2500 if (sock
->event
) release_object( sock
->event
);
2504 sock
->pending_events
= 0;
2505 sock
->reported_events
= 0;
2508 sock
->mask
= params
->mask
;
2509 sock
->window
= params
->window
;
2510 sock
->message
= params
->message
;
2511 sock
->wparam
= params
->handle
;
2512 sock
->nonblocking
= 1;
2514 sock_reselect( sock
);
2519 case IOCTL_AFD_BIND
:
2521 const struct afd_bind_params
*params
= get_req_data();
2522 union unix_sockaddr unix_addr
, bind_addr
;
2523 data_size_t in_size
;
2526 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2528 if (get_req_data_size() < get_reply_max_size())
2530 set_error( STATUS_BUFFER_TOO_SMALL
);
2533 in_size
= get_req_data_size() - get_reply_max_size();
2534 if (in_size
< offsetof(struct afd_bind_params
, addr
.sa_data
)
2535 || get_reply_max_size() < in_size
- sizeof(int))
2537 set_error( STATUS_INVALID_PARAMETER
);
2543 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED
);
2547 unix_len
= sockaddr_to_unix( ¶ms
->addr
, in_size
- sizeof(int), &unix_addr
);
2550 set_error( STATUS_INVALID_ADDRESS
);
2553 bind_addr
= unix_addr
;
2555 if (unix_addr
.addr
.sa_family
== AF_INET
)
2557 if (!memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 )
2558 || bind_to_interface( sock
, &unix_addr
.in
))
2559 bind_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
2561 else if (unix_addr
.addr
.sa_family
== AF_INET6
)
2563 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2564 /* Windows allows specifying zero to use the default scope. Linux
2565 * interprets it as an interface index and requires that it be
2567 if (!unix_addr
.in6
.sin6_scope_id
)
2568 bind_addr
.in6
.sin6_scope_id
= get_ipv6_interface_index( &unix_addr
.in6
.sin6_addr
);
2572 set_async_pending( async
);
2574 if (bind( unix_fd
, &bind_addr
.addr
, unix_len
) < 0)
2576 if (errno
== EADDRINUSE
)
2579 socklen_t len
= sizeof(reuse
);
2581 if (!getsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&reuse
, &len
) && reuse
)
2585 set_error( sock_get_ntstatus( errno
) );
2591 unix_len
= sizeof(bind_addr
);
2592 if (!getsockname( unix_fd
, &bind_addr
.addr
, &unix_len
))
2594 /* store the interface or magic loopback address instead of the
2595 * actual unix address */
2596 if (bind_addr
.addr
.sa_family
== AF_INET
)
2597 bind_addr
.in
.sin_addr
= unix_addr
.in
.sin_addr
;
2598 sock
->addr_len
= sockaddr_from_unix( &bind_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2601 if (get_reply_max_size() >= sock
->addr_len
)
2602 set_reply_data( &sock
->addr
, sock
->addr_len
);
2606 case IOCTL_AFD_GETSOCKNAME
:
2609 set_error( STATUS_INVALID_PARAMETER
);
2613 if (get_reply_max_size() < sock
->addr_len
)
2615 set_error( STATUS_BUFFER_TOO_SMALL
);
2619 set_reply_data( &sock
->addr
, sock
->addr_len
);
2622 case IOCTL_AFD_WINE_DEFER
:
2624 const obj_handle_t
*handle
= get_req_data();
2625 struct sock
*acceptsock
;
2627 if (get_req_data_size() < sizeof(*handle
))
2629 set_error( STATUS_BUFFER_TOO_SMALL
);
2633 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, *handle
, 0, &sock_ops
);
2634 if (!acceptsock
) return;
2636 sock
->deferred
= acceptsock
;
2640 case IOCTL_AFD_WINE_GET_INFO
:
2642 struct afd_get_info_params params
;
2644 if (get_reply_max_size() < sizeof(params
))
2646 set_error( STATUS_BUFFER_TOO_SMALL
);
2650 params
.family
= sock
->family
;
2651 params
.type
= sock
->type
;
2652 params
.protocol
= sock
->proto
;
2653 set_reply_data( ¶ms
, sizeof(params
) );
2657 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN
:
2659 int listening
= (sock
->state
== SOCK_LISTENING
);
2661 if (get_reply_max_size() < sizeof(listening
))
2663 set_error( STATUS_BUFFER_TOO_SMALL
);
2667 set_reply_data( &listening
, sizeof(listening
) );
2671 case IOCTL_AFD_WINE_GET_SO_ERROR
:
2674 socklen_t len
= sizeof(error
);
2677 if (get_reply_max_size() < sizeof(error
))
2679 set_error( STATUS_BUFFER_TOO_SMALL
);
2683 if (getsockopt( unix_fd
, SOL_SOCKET
, SO_ERROR
, (char *)&error
, &len
) < 0)
2685 set_error( sock_get_ntstatus( errno
) );
2691 for (i
= 0; i
< ARRAY_SIZE( sock
->errors
); ++i
)
2693 if (sock
->errors
[i
])
2695 error
= sock_get_error( sock
->errors
[i
] );
2701 set_reply_data( &error
, sizeof(error
) );
2705 case IOCTL_AFD_WINE_GET_SO_RCVBUF
:
2707 int rcvbuf
= sock
->rcvbuf
;
2709 if (get_reply_max_size() < sizeof(rcvbuf
))
2711 set_error( STATUS_BUFFER_TOO_SMALL
);
2715 set_reply_data( &rcvbuf
, sizeof(rcvbuf
) );
2719 case IOCTL_AFD_WINE_SET_SO_RCVBUF
:
2723 if (get_req_data_size() < sizeof(rcvbuf
))
2725 set_error( STATUS_BUFFER_TOO_SMALL
);
2728 rcvbuf
= *(DWORD
*)get_req_data();
2730 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_RCVBUF
, (char *)&rcvbuf
, sizeof(rcvbuf
) ))
2731 sock
->rcvbuf
= rcvbuf
;
2733 set_error( sock_get_ntstatus( errno
) );
2737 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO
:
2739 DWORD rcvtimeo
= sock
->rcvtimeo
;
2741 if (get_reply_max_size() < sizeof(rcvtimeo
))
2743 set_error( STATUS_BUFFER_TOO_SMALL
);
2747 set_reply_data( &rcvtimeo
, sizeof(rcvtimeo
) );
2751 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO
:
2755 if (get_req_data_size() < sizeof(rcvtimeo
))
2757 set_error( STATUS_BUFFER_TOO_SMALL
);
2760 rcvtimeo
= *(DWORD
*)get_req_data();
2762 sock
->rcvtimeo
= rcvtimeo
;
2766 case IOCTL_AFD_WINE_GET_SO_SNDBUF
:
2768 int sndbuf
= sock
->sndbuf
;
2770 if (get_reply_max_size() < sizeof(sndbuf
))
2772 set_error( STATUS_BUFFER_TOO_SMALL
);
2776 set_reply_data( &sndbuf
, sizeof(sndbuf
) );
2780 case IOCTL_AFD_WINE_SET_SO_SNDBUF
:
2784 if (get_req_data_size() < sizeof(sndbuf
))
2786 set_error( STATUS_BUFFER_TOO_SMALL
);
2789 sndbuf
= *(DWORD
*)get_req_data();
2794 /* setsockopt fails if a zero value is passed */
2795 sock
->sndbuf
= sndbuf
;
2800 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_SNDBUF
, (char *)&sndbuf
, sizeof(sndbuf
) ))
2801 sock
->sndbuf
= sndbuf
;
2803 set_error( sock_get_ntstatus( errno
) );
2807 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO
:
2809 DWORD sndtimeo
= sock
->sndtimeo
;
2811 if (get_reply_max_size() < sizeof(sndtimeo
))
2813 set_error( STATUS_BUFFER_TOO_SMALL
);
2817 set_reply_data( &sndtimeo
, sizeof(sndtimeo
) );
2821 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO
:
2825 if (get_req_data_size() < sizeof(sndtimeo
))
2827 set_error( STATUS_BUFFER_TOO_SMALL
);
2830 sndtimeo
= *(DWORD
*)get_req_data();
2832 sock
->sndtimeo
= sndtimeo
;
2836 case IOCTL_AFD_WINE_GET_SO_CONNECT_TIME
:
2840 if (get_reply_max_size() < sizeof(time
))
2842 set_error( STATUS_BUFFER_TOO_SMALL
);
2846 if (sock
->state
== SOCK_CONNECTED
)
2847 time
= (current_time
- sock
->connect_time
) / 10000000;
2849 set_reply_data( &time
, sizeof(time
) );
2854 set_error( STATUS_NOT_SUPPORTED
);
2859 static int poll_single_socket( struct sock
*sock
, int mask
)
2861 struct pollfd pollfd
;
2863 pollfd
.fd
= get_unix_fd( sock
->fd
);
2864 pollfd
.events
= poll_flags_from_afd( sock
, mask
);
2865 if (pollfd
.events
< 0 || poll( &pollfd
, 1, 0 ) < 0)
2868 if ((mask
& AFD_POLL_HUP
) && (pollfd
.revents
& POLLIN
) && sock
->type
== WS_SOCK_STREAM
)
2872 if (!recv( get_unix_fd( sock
->fd
), &dummy
, 1, MSG_PEEK
))
2874 pollfd
.revents
&= ~POLLIN
;
2875 pollfd
.revents
|= POLLHUP
;
2879 return get_poll_flags( sock
, pollfd
.revents
) & mask
;
2882 static void handle_exclusive_poll(struct poll_req
*req
)
2886 for (i
= 0; i
< req
->count
; ++i
)
2888 struct sock
*sock
= req
->sockets
[i
].sock
;
2889 struct poll_req
*main_poll
= sock
->main_poll
;
2891 if (main_poll
&& main_poll
->exclusive
&& req
->exclusive
)
2893 complete_async_poll( main_poll
, STATUS_SUCCESS
);
2898 sock
->main_poll
= req
;
2902 static void poll_socket( struct sock
*poll_sock
, struct async
*async
, int exclusive
, timeout_t timeout
,
2903 unsigned int count
, const struct poll_socket_input
*input
)
2905 struct poll_socket_output
*output
;
2906 BOOL signaled
= FALSE
;
2907 struct poll_req
*req
;
2910 if (!(output
= mem_alloc( count
* sizeof(*output
) )))
2912 memset( output
, 0, count
* sizeof(*output
) );
2914 if (!(req
= mem_alloc( offsetof( struct poll_req
, sockets
[count
] ) )))
2920 req
->timeout
= NULL
;
2921 if (timeout
&& timeout
!= TIMEOUT_INFINITE
&&
2922 !(req
->timeout
= add_timeout_user( timeout
, async_poll_timeout
, req
)))
2929 for (i
= 0; i
< count
; ++i
)
2931 req
->sockets
[i
].sock
= (struct sock
*)get_handle_obj( current
->process
, input
[i
].socket
, 0, &sock_ops
);
2932 if (!req
->sockets
[i
].sock
)
2934 for (j
= 0; j
< i
; ++j
) release_object( req
->sockets
[i
].sock
);
2935 if (req
->timeout
) remove_timeout_user( req
->timeout
);
2940 req
->sockets
[i
].flags
= input
[i
].flags
;
2943 req
->exclusive
= exclusive
;
2945 req
->async
= (struct async
*)grab_object( async
);
2946 req
->iosb
= async_get_iosb( async
);
2947 req
->output
= output
;
2949 handle_exclusive_poll(req
);
2951 list_add_tail( &poll_list
, &req
->entry
);
2952 async_set_completion_callback( async
, free_poll_req
, req
);
2953 queue_async( &poll_sock
->poll_q
, async
);
2955 for (i
= 0; i
< count
; ++i
)
2957 struct sock
*sock
= req
->sockets
[i
].sock
;
2958 int mask
= req
->sockets
[i
].flags
;
2959 int flags
= poll_single_socket( sock
, mask
);
2964 output
[i
].flags
= flags
;
2965 output
[i
].status
= sock_get_ntstatus( sock_error( sock
->fd
) );
2968 /* FIXME: do other error conditions deserve a similar treatment? */
2969 if (sock
->state
!= SOCK_CONNECTING
&& sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] && (mask
& AFD_POLL_CONNECT_ERR
))
2972 output
[i
].flags
|= AFD_POLL_CONNECT_ERR
;
2973 output
[i
].status
= sock_get_ntstatus( sock
->errors
[AFD_POLL_BIT_CONNECT_ERR
] );
2977 if (!timeout
|| signaled
)
2978 complete_async_poll( req
, STATUS_SUCCESS
);
2980 for (i
= 0; i
< req
->count
; ++i
)
2981 sock_reselect( req
->sockets
[i
].sock
);
2982 set_error( STATUS_PENDING
);
2985 #ifdef HAVE_LINUX_RTNETLINK_H
2987 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
2988 static struct object
*ifchange_object
;
2990 static void ifchange_dump( struct object
*obj
, int verbose
);
2991 static struct fd
*ifchange_get_fd( struct object
*obj
);
2992 static void ifchange_destroy( struct object
*obj
);
2994 static int ifchange_get_poll_events( struct fd
*fd
);
2995 static void ifchange_poll_event( struct fd
*fd
, int event
);
2999 struct object obj
; /* object header */
3000 struct fd
*fd
; /* interface change file descriptor */
3001 struct list sockets
; /* list of sockets to send interface change notifications */
3004 static const struct object_ops ifchange_ops
=
3006 sizeof(struct ifchange
), /* size */
3007 &no_type
, /* type */
3008 ifchange_dump
, /* dump */
3009 no_add_queue
, /* add_queue */
3010 NULL
, /* remove_queue */
3011 NULL
, /* signaled */
3012 no_satisfied
, /* satisfied */
3013 no_signal
, /* signal */
3014 ifchange_get_fd
, /* get_fd */
3015 default_map_access
, /* map_access */
3016 default_get_sd
, /* get_sd */
3017 default_set_sd
, /* set_sd */
3018 no_get_full_name
, /* get_full_name */
3019 no_lookup_name
, /* lookup_name */
3020 no_link_name
, /* link_name */
3021 NULL
, /* unlink_name */
3022 no_open_file
, /* open_file */
3023 no_kernel_obj_list
, /* get_kernel_obj_list */
3024 no_close_handle
, /* close_handle */
3025 ifchange_destroy
/* destroy */
3028 static const struct fd_ops ifchange_fd_ops
=
3030 ifchange_get_poll_events
, /* get_poll_events */
3031 ifchange_poll_event
, /* poll_event */
3032 NULL
, /* get_fd_type */
3033 no_fd_read
, /* read */
3034 no_fd_write
, /* write */
3035 no_fd_flush
, /* flush */
3036 no_fd_get_file_info
, /* get_file_info */
3037 no_fd_get_volume_info
, /* get_volume_info */
3038 no_fd_ioctl
, /* ioctl */
3039 NULL
, /* cancel_async */
3040 NULL
, /* queue_async */
3041 NULL
/* reselect_async */
3044 static void ifchange_dump( struct object
*obj
, int verbose
)
3046 assert( obj
->ops
== &ifchange_ops
);
3047 fprintf( stderr
, "Interface change\n" );
3050 static struct fd
*ifchange_get_fd( struct object
*obj
)
3052 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3053 return (struct fd
*)grab_object( ifchange
->fd
);
3056 static void ifchange_destroy( struct object
*obj
)
3058 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3059 assert( obj
->ops
== &ifchange_ops
);
3061 release_object( ifchange
->fd
);
3063 /* reset the global ifchange object so that it will be recreated if it is needed again */
3064 assert( obj
== ifchange_object
);
3065 ifchange_object
= NULL
;
3068 static int ifchange_get_poll_events( struct fd
*fd
)
3073 /* wake up all the sockets waiting for a change notification event */
3074 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
3076 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3077 struct list
*ptr
, *next
;
3078 assert( obj
->ops
== &ifchange_ops
);
3079 assert( obj
== ifchange_object
);
3081 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
3083 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
3085 assert( sock
->ifchange_obj
);
3086 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
3087 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
3091 static void ifchange_poll_event( struct fd
*fd
, int event
)
3093 struct object
*ifchange
= get_fd_user( fd
);
3094 unsigned int status
= STATUS_PENDING
;
3095 char buffer
[PIPE_BUF
];
3098 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
3101 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
3102 return; /* retry when poll() says the socket is ready */
3103 status
= sock_get_ntstatus( errno
);
3107 struct nlmsghdr
*nlh
;
3109 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
3111 if (nlh
->nlmsg_type
== NLMSG_DONE
)
3113 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
3114 status
= STATUS_SUCCESS
;
3117 else status
= STATUS_CANCELLED
;
3119 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
3124 /* we only need one of these interface notification objects, all of the sockets dependent upon
3125 * it will wake up when a notification event occurs */
3126 static struct object
*get_ifchange( void )
3128 #ifdef HAVE_LINUX_RTNETLINK_H
3129 struct ifchange
*ifchange
;
3130 struct sockaddr_nl addr
;
3133 if (ifchange_object
)
3135 /* increment the refcount for each socket that uses the ifchange object */
3136 return grab_object( ifchange_object
);
3139 /* create the socket we need for processing interface change notifications */
3140 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
3143 set_error( sock_get_ntstatus( errno
));
3146 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
3147 memset( &addr
, 0, sizeof(addr
) );
3148 addr
.nl_family
= AF_NETLINK
;
3149 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
3150 /* bind the socket to the special netlink kernel interface */
3151 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
3154 set_error( sock_get_ntstatus( errno
));
3157 if (!(ifchange
= alloc_object( &ifchange_ops
)))
3160 set_error( STATUS_NO_MEMORY
);
3163 list_init( &ifchange
->sockets
);
3164 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
3166 release_object( ifchange
);
3167 set_error( STATUS_NO_MEMORY
);
3170 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
3172 /* the ifchange object is now successfully configured */
3173 ifchange_object
= &ifchange
->obj
;
3174 return &ifchange
->obj
;
3176 set_error( STATUS_NOT_SUPPORTED
);
3181 /* add the socket to the interface change notification list */
3182 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
3184 #ifdef HAVE_LINUX_RTNETLINK_H
3185 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3187 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
3191 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3192 static struct object
*sock_get_ifchange( struct sock
*sock
)
3194 struct object
*ifchange
;
3196 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
3197 return sock
->ifchange_obj
;
3199 if (!(ifchange
= get_ifchange()))
3202 /* add the socket to the ifchange notification list */
3203 ifchange_add_sock( ifchange
, sock
);
3204 sock
->ifchange_obj
= ifchange
;
3208 /* destroy an existing ifchange queue for a specific socket */
3209 static void sock_release_ifchange( struct sock
*sock
)
3211 if (sock
->ifchange_obj
)
3213 list_remove( &sock
->ifchange_entry
);
3214 release_object( sock
->ifchange_obj
);
3215 sock
->ifchange_obj
= NULL
;
3219 static void socket_device_dump( struct object
*obj
, int verbose
);
3220 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3221 unsigned int attr
, struct object
*root
);
3222 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3223 unsigned int sharing
, unsigned int options
);
3225 static const struct object_ops socket_device_ops
=
3227 sizeof(struct object
), /* size */
3228 &device_type
, /* type */
3229 socket_device_dump
, /* dump */
3230 no_add_queue
, /* add_queue */
3231 NULL
, /* remove_queue */
3232 NULL
, /* signaled */
3233 no_satisfied
, /* satisfied */
3234 no_signal
, /* signal */
3235 no_get_fd
, /* get_fd */
3236 default_map_access
, /* map_access */
3237 default_get_sd
, /* get_sd */
3238 default_set_sd
, /* set_sd */
3239 default_get_full_name
, /* get_full_name */
3240 socket_device_lookup_name
, /* lookup_name */
3241 directory_link_name
, /* link_name */
3242 default_unlink_name
, /* unlink_name */
3243 socket_device_open_file
, /* open_file */
3244 no_kernel_obj_list
, /* get_kernel_obj_list */
3245 no_close_handle
, /* close_handle */
3246 no_destroy
/* destroy */
3249 static void socket_device_dump( struct object
*obj
, int verbose
)
3251 fputs( "Socket device\n", stderr
);
3254 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3255 unsigned int attr
, struct object
*root
)
3257 if (name
) name
->len
= 0;
3261 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3262 unsigned int sharing
, unsigned int options
)
3266 if (!(sock
= create_socket())) return NULL
;
3267 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
3269 release_object( sock
);
3275 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
3276 unsigned int attr
, const struct security_descriptor
*sd
)
3278 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
3281 DECL_HANDLER(recv_socket
)
3283 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3284 unsigned int status
= req
->status
;
3285 timeout_t timeout
= 0;
3286 struct async
*async
;
3292 /* recv() returned EWOULDBLOCK, i.e. no data available yet */
3293 if (status
== STATUS_DEVICE_NOT_READY
&& !sock
->nonblocking
)
3295 /* Set a timeout on the async if necessary.
3297 * We want to do this *only* if the client gave us STATUS_DEVICE_NOT_READY.
3298 * If the client gave us STATUS_PENDING, it expects the async to always
3299 * block (it was triggered by WSARecv*() with a valid OVERLAPPED
3300 * structure) and for the timeout not to be respected. */
3301 if (is_fd_overlapped( fd
))
3302 timeout
= (timeout_t
)sock
->rcvtimeo
* -10000;
3304 status
= STATUS_PENDING
;
3307 if ((status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
) && sock
->rd_shutdown
)
3308 status
= STATUS_PIPE_DISCONNECTED
;
3310 sock
->pending_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3311 sock
->reported_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3313 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3315 if (status
== STATUS_SUCCESS
)
3317 struct iosb
*iosb
= async_get_iosb( async
);
3318 iosb
->result
= req
->total
;
3319 release_object( iosb
);
3321 set_error( status
);
3324 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3326 if (status
== STATUS_PENDING
)
3327 queue_async( &sock
->read_q
, async
);
3329 /* always reselect; we changed reported_events above */
3330 sock_reselect( sock
);
3332 reply
->wait
= async_handoff( async
, NULL
, 0 );
3333 reply
->options
= get_fd_options( fd
);
3334 release_object( async
);
3336 release_object( sock
);
3339 DECL_HANDLER(poll_socket
)
3341 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3342 const struct poll_socket_input
*input
= get_req_data();
3343 struct async
*async
;
3348 count
= get_req_data_size() / sizeof(*input
);
3350 if ((async
= create_request_async( sock
->fd
, get_fd_comp_flags( sock
->fd
), &req
->async
)))
3352 poll_socket( sock
, async
, req
->exclusive
, req
->timeout
, count
, input
);
3353 reply
->wait
= async_handoff( async
, NULL
, 0 );
3354 reply
->options
= get_fd_options( sock
->fd
);
3355 release_object( async
);
3358 release_object( sock
);
3361 DECL_HANDLER(send_socket
)
3363 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3364 unsigned int status
= req
->status
;
3365 timeout_t timeout
= 0;
3366 struct async
*async
;
3372 if (sock
->type
== WS_SOCK_DGRAM
)
3374 /* sendto() and sendmsg() implicitly binds a socket */
3375 union unix_sockaddr unix_addr
;
3376 socklen_t unix_len
= sizeof(unix_addr
);
3378 if (!sock
->bound
&& !getsockname( get_unix_fd( fd
), &unix_addr
.addr
, &unix_len
))
3379 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
3383 if (status
!= STATUS_SUCCESS
)
3385 /* send() calls only clear and reselect events if unsuccessful. */
3386 sock
->pending_events
&= ~AFD_POLL_WRITE
;
3387 sock
->reported_events
&= ~AFD_POLL_WRITE
;
3390 /* If we had a short write and the socket is nonblocking (and the client is
3391 * not trying to force the operation to be asynchronous), return success.
3392 * Windows actually refuses to send any data in this case, and returns
3393 * EWOULDBLOCK, but we have no way of doing that. */
3394 if (status
== STATUS_DEVICE_NOT_READY
&& req
->total
&& sock
->nonblocking
)
3395 status
= STATUS_SUCCESS
;
3397 /* send() returned EWOULDBLOCK or a short write, i.e. cannot send all data yet */
3398 if (status
== STATUS_DEVICE_NOT_READY
&& !sock
->nonblocking
)
3400 /* Set a timeout on the async if necessary.
3402 * We want to do this *only* if the client gave us STATUS_DEVICE_NOT_READY.
3403 * If the client gave us STATUS_PENDING, it expects the async to always
3404 * block (it was triggered by WSASend*() with a valid OVERLAPPED
3405 * structure) and for the timeout not to be respected. */
3406 if (is_fd_overlapped( fd
))
3407 timeout
= (timeout_t
)sock
->sndtimeo
* -10000;
3409 status
= STATUS_PENDING
;
3412 if ((status
== STATUS_PENDING
|| status
== STATUS_DEVICE_NOT_READY
) && sock
->wr_shutdown
)
3413 status
= STATUS_PIPE_DISCONNECTED
;
3415 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3417 if (status
== STATUS_SUCCESS
)
3419 struct iosb
*iosb
= async_get_iosb( async
);
3420 iosb
->result
= req
->total
;
3421 release_object( iosb
);
3423 set_error( status
);
3426 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3428 if (status
== STATUS_PENDING
)
3429 queue_async( &sock
->write_q
, async
);
3431 /* always reselect; we changed reported_events above */
3432 sock_reselect( sock
);
3434 reply
->wait
= async_handoff( async
, NULL
, 0 );
3435 reply
->options
= get_fd_options( fd
);
3436 release_object( async
);
3438 release_object( sock
);