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 nonblocking
: 1; /* is the socket nonblocking? */
218 unsigned int bound
: 1; /* is the socket bound? */
221 static void sock_dump( struct object
*obj
, int verbose
);
222 static struct fd
*sock_get_fd( struct object
*obj
);
223 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
224 static void sock_destroy( struct object
*obj
);
225 static struct object
*sock_get_ifchange( struct sock
*sock
);
226 static void sock_release_ifchange( struct sock
*sock
);
228 static int sock_get_poll_events( struct fd
*fd
);
229 static void sock_poll_event( struct fd
*fd
, int event
);
230 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
231 static int sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
232 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
);
233 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
235 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
);
236 static struct sock
*accept_socket( struct sock
*sock
);
237 static int sock_get_ntstatus( int err
);
238 static unsigned int sock_get_error( int err
);
240 static const struct object_ops sock_ops
=
242 sizeof(struct sock
), /* size */
243 &file_type
, /* type */
244 sock_dump
, /* dump */
245 add_queue
, /* add_queue */
246 remove_queue
, /* remove_queue */
247 default_fd_signaled
, /* signaled */
248 no_satisfied
, /* satisfied */
249 no_signal
, /* signal */
250 sock_get_fd
, /* get_fd */
251 default_map_access
, /* map_access */
252 default_get_sd
, /* get_sd */
253 default_set_sd
, /* set_sd */
254 no_get_full_name
, /* get_full_name */
255 no_lookup_name
, /* lookup_name */
256 no_link_name
, /* link_name */
257 NULL
, /* unlink_name */
258 no_open_file
, /* open_file */
259 no_kernel_obj_list
, /* get_kernel_obj_list */
260 sock_close_handle
, /* close_handle */
261 sock_destroy
/* destroy */
264 static const struct fd_ops sock_fd_ops
=
266 sock_get_poll_events
, /* get_poll_events */
267 sock_poll_event
, /* poll_event */
268 sock_get_fd_type
, /* get_fd_type */
269 no_fd_read
, /* read */
270 no_fd_write
, /* write */
271 no_fd_flush
, /* flush */
272 default_fd_get_file_info
, /* get_file_info */
273 no_fd_get_volume_info
, /* get_volume_info */
274 sock_ioctl
, /* ioctl */
275 sock_queue_async
, /* queue_async */
276 sock_reselect_async
/* reselect_async */
281 struct sockaddr addr
;
282 struct sockaddr_in in
;
283 struct sockaddr_in6 in6
;
285 struct sockaddr_ipx ipx
;
288 struct sockaddr_irda irda
;
292 static int sockaddr_from_unix( const union unix_sockaddr
*uaddr
, struct WS_sockaddr
*wsaddr
, socklen_t wsaddrlen
)
294 memset( wsaddr
, 0, wsaddrlen
);
296 switch (uaddr
->addr
.sa_family
)
300 struct WS_sockaddr_in win
= {0};
302 if (wsaddrlen
< sizeof(win
)) return -1;
303 win
.sin_family
= WS_AF_INET
;
304 win
.sin_port
= uaddr
->in
.sin_port
;
305 memcpy( &win
.sin_addr
, &uaddr
->in
.sin_addr
, sizeof(win
.sin_addr
) );
306 memcpy( wsaddr
, &win
, sizeof(win
) );
312 struct WS_sockaddr_in6 win
= {0};
314 if (wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
)) return -1;
315 win
.sin6_family
= WS_AF_INET6
;
316 win
.sin6_port
= uaddr
->in6
.sin6_port
;
317 win
.sin6_flowinfo
= uaddr
->in6
.sin6_flowinfo
;
318 memcpy( &win
.sin6_addr
, &uaddr
->in6
.sin6_addr
, sizeof(win
.sin6_addr
) );
319 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
320 win
.sin6_scope_id
= uaddr
->in6
.sin6_scope_id
;
322 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
))
324 memcpy( wsaddr
, &win
, sizeof(struct WS_sockaddr_in6
) );
325 return sizeof(struct WS_sockaddr_in6
);
327 memcpy( wsaddr
, &win
, sizeof(struct WS_sockaddr_in6_old
) );
328 return sizeof(struct WS_sockaddr_in6_old
);
334 struct WS_sockaddr_ipx win
= {0};
336 if (wsaddrlen
< sizeof(win
)) return -1;
337 win
.sa_family
= WS_AF_IPX
;
338 memcpy( win
.sa_netnum
, &uaddr
->ipx
.sipx_network
, sizeof(win
.sa_netnum
) );
339 memcpy( win
.sa_nodenum
, &uaddr
->ipx
.sipx_node
, sizeof(win
.sa_nodenum
) );
340 win
.sa_socket
= uaddr
->ipx
.sipx_port
;
341 memcpy( wsaddr
, &win
, sizeof(win
) );
351 if (wsaddrlen
< sizeof(win
)) return -1;
352 win
.irdaAddressFamily
= WS_AF_IRDA
;
353 memcpy( win
.irdaDeviceID
, &uaddr
->irda
.sir_addr
, sizeof(win
.irdaDeviceID
) );
354 if (uaddr
->irda
.sir_lsap_sel
!= LSAP_ANY
)
355 snprintf( win
.irdaServiceName
, sizeof(win
.irdaServiceName
), "LSAP-SEL%u", uaddr
->irda
.sir_lsap_sel
);
357 memcpy( win
.irdaServiceName
, uaddr
->irda
.sir_name
, sizeof(win
.irdaServiceName
) );
358 memcpy( wsaddr
, &win
, sizeof(win
) );
372 static socklen_t
sockaddr_to_unix( const struct WS_sockaddr
*wsaddr
, int wsaddrlen
, union unix_sockaddr
*uaddr
)
374 memset( uaddr
, 0, sizeof(*uaddr
) );
376 switch (wsaddr
->sa_family
)
380 struct WS_sockaddr_in win
= {0};
382 if (wsaddrlen
< sizeof(win
)) return 0;
383 memcpy( &win
, wsaddr
, sizeof(win
) );
384 uaddr
->in
.sin_family
= AF_INET
;
385 uaddr
->in
.sin_port
= win
.sin_port
;
386 memcpy( &uaddr
->in
.sin_addr
, &win
.sin_addr
, sizeof(win
.sin_addr
) );
387 return sizeof(uaddr
->in
);
392 struct WS_sockaddr_in6 win
= {0};
394 if (wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
)) return 0;
395 if (wsaddrlen
< sizeof(struct WS_sockaddr_in6
))
396 memcpy( &win
, wsaddr
, sizeof(struct WS_sockaddr_in6_old
) );
398 memcpy( &win
, wsaddr
, sizeof(struct WS_sockaddr_in6
) );
400 uaddr
->in6
.sin6_family
= AF_INET6
;
401 uaddr
->in6
.sin6_port
= win
.sin6_port
;
402 uaddr
->in6
.sin6_flowinfo
= win
.sin6_flowinfo
;
403 memcpy( &uaddr
->in6
.sin6_addr
, &win
.sin6_addr
, sizeof(win
.sin6_addr
) );
404 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
405 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
))
406 uaddr
->in6
.sin6_scope_id
= win
.sin6_scope_id
;
408 return sizeof(uaddr
->in6
);
414 struct WS_sockaddr_ipx win
= {0};
416 if (wsaddrlen
< sizeof(win
)) return 0;
417 memcpy( &win
, wsaddr
, sizeof(win
) );
418 uaddr
->ipx
.sipx_family
= AF_IPX
;
419 memcpy( &uaddr
->ipx
.sipx_network
, win
.sa_netnum
, sizeof(win
.sa_netnum
) );
420 memcpy( &uaddr
->ipx
.sipx_node
, win
.sa_nodenum
, sizeof(win
.sa_nodenum
) );
421 uaddr
->ipx
.sipx_port
= win
.sa_socket
;
422 return sizeof(uaddr
->ipx
);
429 SOCKADDR_IRDA win
= {0};
430 unsigned int lsap_sel
;
432 if (wsaddrlen
< sizeof(win
)) return 0;
433 memcpy( &win
, wsaddr
, sizeof(win
) );
434 uaddr
->irda
.sir_family
= AF_IRDA
;
435 if (sscanf( win
.irdaServiceName
, "LSAP-SEL%u", &lsap_sel
) == 1)
436 uaddr
->irda
.sir_lsap_sel
= lsap_sel
;
439 uaddr
->irda
.sir_lsap_sel
= LSAP_ANY
;
440 memcpy( uaddr
->irda
.sir_name
, win
.irdaServiceName
, sizeof(win
.irdaServiceName
) );
442 memcpy( &uaddr
->irda
.sir_addr
, win
.irdaDeviceID
, sizeof(win
.irdaDeviceID
) );
443 return sizeof(uaddr
->irda
);
450 default: /* likely an ipv4 address */
451 case sizeof(struct WS_sockaddr_in
):
452 return sizeof(uaddr
->in
);
455 case sizeof(struct WS_sockaddr_ipx
):
456 return sizeof(uaddr
->ipx
);
460 case sizeof(SOCKADDR_IRDA
):
461 return sizeof(uaddr
->irda
);
464 case sizeof(struct WS_sockaddr_in6
):
465 case sizeof(struct WS_sockaddr_in6_old
):
466 return sizeof(uaddr
->in6
);
474 /* some events are generated at the same time but must be sent in a particular
475 * order (e.g. CONNECT must be sent before READ) */
476 static const enum afd_poll_bit event_bitorder
[] =
478 AFD_POLL_BIT_CONNECT
,
479 AFD_POLL_BIT_CONNECT_ERR
,
490 SOCK_SHUTDOWN_ERROR
= -1,
491 SOCK_SHUTDOWN_EOF
= 0,
492 SOCK_SHUTDOWN_POLLHUP
= 1
495 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
497 static sock_shutdown_t
sock_check_pollhup(void)
499 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
504 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
505 if ( shutdown( fd
[0], 1 ) ) goto out
;
511 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
512 n
= poll( &pfd
, 1, 1 );
513 if ( n
!= 1 ) goto out
; /* error or timeout */
514 if ( pfd
.revents
& POLLHUP
)
515 ret
= SOCK_SHUTDOWN_POLLHUP
;
516 else if ( pfd
.revents
& POLLIN
&&
517 read( fd
[1], &dummy
, 1 ) == 0 )
518 ret
= SOCK_SHUTDOWN_EOF
;
528 sock_shutdown_type
= sock_check_pollhup();
530 switch ( sock_shutdown_type
)
532 case SOCK_SHUTDOWN_EOF
:
533 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
535 case SOCK_SHUTDOWN_POLLHUP
:
536 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
539 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
540 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
544 static int sock_reselect( struct sock
*sock
)
546 int ev
= sock_get_poll_events( sock
->fd
);
549 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
551 set_fd_events( sock
->fd
, ev
);
555 static unsigned int afd_poll_flag_to_win32( unsigned int flags
)
557 static const unsigned int map
[] =
561 FD_WRITE
, /* WRITE */
563 FD_CLOSE
, /* RESET */
565 FD_CONNECT
, /* CONNECT */
566 FD_ACCEPT
, /* ACCEPT */
567 FD_CONNECT
, /* CONNECT_ERR */
570 unsigned int i
, ret
= 0;
572 for (i
= 0; i
< ARRAY_SIZE(map
); ++i
)
574 if (flags
& (1 << i
)) ret
|= map
[i
];
580 /* wake anybody waiting on the socket event or send the associated message */
581 static void sock_wake_up( struct sock
*sock
)
583 unsigned int events
= sock
->pending_events
& sock
->mask
;
588 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
590 set_event( sock
->event
);
594 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
595 for (i
= 0; i
< ARRAY_SIZE(event_bitorder
); i
++)
597 enum afd_poll_bit event
= event_bitorder
[i
];
598 if (events
& (1 << event
))
600 lparam_t lparam
= afd_poll_flag_to_win32(1 << event
) | (sock_get_error( sock
->errors
[event
] ) << 16);
601 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
604 sock
->pending_events
= 0;
605 sock_reselect( sock
);
609 static inline int sock_error( struct fd
*fd
)
611 unsigned int optval
= 0;
612 socklen_t optlen
= sizeof(optval
);
614 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
618 static void free_accept_req( void *private )
620 struct accept_req
*req
= private;
621 list_remove( &req
->entry
);
624 req
->acceptsock
->accept_recv_req
= NULL
;
625 release_object( req
->acceptsock
);
627 release_object( req
->async
);
628 release_object( req
->iosb
);
629 release_object( req
->sock
);
633 static void fill_accept_output( struct accept_req
*req
)
635 struct iosb
*iosb
= req
->iosb
;
636 union unix_sockaddr unix_addr
;
637 struct WS_sockaddr
*win_addr
;
638 unsigned int remote_len
;
644 if (!(out_data
= mem_alloc( iosb
->out_size
))) return;
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
);
654 set_error( STATUS_PENDING
);
658 set_error( sock_get_ntstatus( errno
) );
665 if (req
->local_len
< sizeof(int))
667 set_error( STATUS_BUFFER_TOO_SMALL
);
672 unix_len
= sizeof(unix_addr
);
673 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ sizeof(int));
674 if (getsockname( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
675 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, req
->local_len
- sizeof(int) )) < 0)
677 set_error( sock_get_ntstatus( errno
) );
681 memcpy( out_data
+ req
->recv_len
, &win_len
, sizeof(int) );
684 unix_len
= sizeof(unix_addr
);
685 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ req
->local_len
+ sizeof(int));
686 remote_len
= iosb
->out_size
- req
->recv_len
- req
->local_len
;
687 if (getpeername( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
688 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, remote_len
- sizeof(int) )) < 0)
690 set_error( sock_get_ntstatus( errno
) );
694 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
696 iosb
->status
= STATUS_SUCCESS
;
698 iosb
->out_data
= out_data
;
699 set_error( STATUS_ALERTED
);
702 static void complete_async_accept( struct sock
*sock
, struct accept_req
*req
)
704 struct sock
*acceptsock
= req
->acceptsock
;
705 struct async
*async
= req
->async
;
707 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
711 if (!accept_into_socket( sock
, acceptsock
)) return;
712 fill_accept_output( req
);
716 struct iosb
*iosb
= req
->iosb
;
719 if (!(acceptsock
= accept_socket( sock
))) return;
720 handle
= alloc_handle_no_access_check( async_get_thread( async
)->process
, &acceptsock
->obj
,
721 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
722 acceptsock
->wparam
= handle
;
723 release_object( acceptsock
);
726 if (!(iosb
->out_data
= malloc( sizeof(handle
) ))) return;
728 iosb
->status
= STATUS_SUCCESS
;
729 iosb
->out_size
= sizeof(handle
);
730 memcpy( iosb
->out_data
, &handle
, sizeof(handle
) );
731 set_error( STATUS_ALERTED
);
735 static void complete_async_accept_recv( struct accept_req
*req
)
737 if (debug_level
) fprintf( stderr
, "completing accept recv request for socket %p\n", req
->acceptsock
);
739 assert( req
->recv_len
);
741 fill_accept_output( req
);
744 static void free_connect_req( void *private )
746 struct connect_req
*req
= private;
748 req
->sock
->connect_req
= NULL
;
749 release_object( req
->async
);
750 release_object( req
->iosb
);
751 release_object( req
->sock
);
755 static void complete_async_connect( struct sock
*sock
)
757 struct connect_req
*req
= sock
->connect_req
;
758 const char *in_buffer
;
763 if (debug_level
) fprintf( stderr
, "completing connect request for socket %p\n", sock
);
765 sock
->state
= SOCK_CONNECTED
;
769 set_error( STATUS_SUCCESS
);
774 in_buffer
= (const char *)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 set_error( sock_get_ntstatus( errno
) );
782 iosb
->result
= req
->send_len
;
783 iosb
->status
= STATUS_SUCCESS
;
784 set_error( STATUS_ALERTED
);
788 req
->send_cursor
+= ret
;
789 set_error( STATUS_PENDING
);
793 static void free_poll_req( void *private )
795 struct poll_req
*req
= private;
798 if (req
->timeout
) remove_timeout_user( req
->timeout
);
800 for (i
= 0; i
< req
->count
; ++i
)
801 release_object( req
->sockets
[i
].sock
);
802 release_object( req
->async
);
803 release_object( req
->iosb
);
804 list_remove( &req
->entry
);
808 static int is_oobinline( struct sock
*sock
)
811 socklen_t len
= sizeof(oobinline
);
812 return !getsockopt( get_unix_fd( sock
->fd
), SOL_SOCKET
, SO_OOBINLINE
, (char *)&oobinline
, &len
) && oobinline
;
815 static int get_poll_flags( struct sock
*sock
, int event
)
819 /* A connection-mode socket which has never been connected does not return
820 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
821 if (sock
->state
== SOCK_UNCONNECTED
)
822 event
&= ~(POLLOUT
| POLLHUP
);
826 if (sock
->state
== SOCK_LISTENING
)
827 flags
|= AFD_POLL_ACCEPT
;
829 flags
|= AFD_POLL_READ
;
832 flags
|= is_oobinline( sock
) ? AFD_POLL_READ
: AFD_POLL_OOB
;
834 flags
|= AFD_POLL_WRITE
;
835 if (sock
->state
== SOCK_CONNECTED
)
836 flags
|= AFD_POLL_CONNECT
;
838 flags
|= AFD_POLL_HUP
;
840 flags
|= AFD_POLL_CONNECT_ERR
;
845 static void complete_async_polls( struct sock
*sock
, int event
, int error
)
847 int flags
= get_poll_flags( sock
, event
);
848 struct poll_req
*req
, *next
;
850 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &poll_list
, struct poll_req
, entry
)
852 struct iosb
*iosb
= req
->iosb
;
855 if (iosb
->status
!= STATUS_PENDING
) continue;
857 for (i
= 0; i
< req
->count
; ++i
)
859 if (req
->sockets
[i
].sock
!= sock
) continue;
860 if (!(req
->sockets
[i
].flags
& flags
)) continue;
863 fprintf( stderr
, "completing poll for socket %p, wanted %#x got %#x\n",
864 sock
, req
->sockets
[i
].flags
, flags
);
866 req
->output
[i
].flags
= req
->sockets
[i
].flags
& flags
;
867 req
->output
[i
].status
= sock_get_ntstatus( error
);
869 iosb
->status
= STATUS_SUCCESS
;
870 iosb
->out_data
= req
->output
;
871 iosb
->out_size
= req
->count
* sizeof(*req
->output
);
872 async_terminate( req
->async
, STATUS_ALERTED
);
878 static void async_poll_timeout( void *private )
880 struct poll_req
*req
= private;
881 struct iosb
*iosb
= req
->iosb
;
885 if (iosb
->status
!= STATUS_PENDING
) return;
887 iosb
->status
= STATUS_TIMEOUT
;
888 iosb
->out_data
= req
->output
;
889 iosb
->out_size
= req
->count
* sizeof(*req
->output
);
890 async_terminate( req
->async
, STATUS_ALERTED
);
893 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
895 if (event
& (POLLIN
| POLLPRI
))
897 struct accept_req
*req
;
899 LIST_FOR_EACH_ENTRY( req
, &sock
->accept_list
, struct accept_req
, entry
)
901 if (req
->iosb
->status
== STATUS_PENDING
&& !req
->accepted
)
903 complete_async_accept( sock
, req
);
904 if (get_error() != STATUS_PENDING
)
905 async_terminate( req
->async
, get_error() );
910 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
912 complete_async_accept_recv( sock
->accept_recv_req
);
913 if (get_error() != STATUS_PENDING
)
914 async_terminate( sock
->accept_recv_req
->async
, get_error() );
918 if ((event
& POLLOUT
) && sock
->connect_req
&& sock
->connect_req
->iosb
->status
== STATUS_PENDING
)
920 complete_async_connect( sock
);
921 if (get_error() != STATUS_PENDING
)
922 async_terminate( sock
->connect_req
->async
, get_error() );
925 if (event
& (POLLIN
| POLLPRI
) && async_waiting( &sock
->read_q
))
927 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
928 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
929 event
&= ~(POLLIN
| POLLPRI
);
932 if (event
& POLLOUT
&& async_waiting( &sock
->write_q
))
934 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
935 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
939 if (event
& (POLLERR
| POLLHUP
))
941 int status
= sock_get_ntstatus( error
);
942 struct accept_req
*req
, *next
;
944 if (sock
->rd_shutdown
)
945 async_wake_up( &sock
->read_q
, status
);
946 if (sock
->wr_shutdown
)
947 async_wake_up( &sock
->write_q
, status
);
949 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
951 if (req
->iosb
->status
== STATUS_PENDING
)
952 async_terminate( req
->async
, status
);
955 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
956 async_terminate( sock
->accept_recv_req
->async
, status
);
958 if (sock
->connect_req
)
959 async_terminate( sock
->connect_req
->async
, status
);
965 static void post_socket_event( struct sock
*sock
, enum afd_poll_bit event_bit
, int error
)
967 unsigned int event
= (1 << event_bit
);
969 if (!(sock
->reported_events
& event
))
971 sock
->pending_events
|= event
;
972 sock
->reported_events
|= event
;
973 sock
->errors
[event_bit
] = error
;
977 static void sock_dispatch_events( struct sock
*sock
, enum connection_state prevstate
, int event
, int error
)
981 case SOCK_UNCONNECTED
:
984 case SOCK_CONNECTING
:
986 post_socket_event( sock
, AFD_POLL_BIT_CONNECT
, 0 );
987 if (event
& (POLLERR
| POLLHUP
))
988 post_socket_event( sock
, AFD_POLL_BIT_CONNECT_ERR
, error
);
992 if (event
& (POLLIN
| POLLERR
| POLLHUP
))
993 post_socket_event( sock
, AFD_POLL_BIT_ACCEPT
, error
);
997 case SOCK_CONNECTIONLESS
:
999 post_socket_event( sock
, AFD_POLL_BIT_READ
, 0 );
1001 if (event
& POLLOUT
)
1002 post_socket_event( sock
, AFD_POLL_BIT_WRITE
, 0 );
1004 if (event
& POLLPRI
)
1005 post_socket_event( sock
, AFD_POLL_BIT_OOB
, 0 );
1007 if (event
& (POLLERR
| POLLHUP
))
1008 post_socket_event( sock
, AFD_POLL_BIT_HUP
, error
);
1012 sock_wake_up( sock
);
1015 static void sock_poll_event( struct fd
*fd
, int event
)
1017 struct sock
*sock
= get_fd_user( fd
);
1018 int hangup_seen
= 0;
1019 enum connection_state prevstate
= sock
->state
;
1022 assert( sock
->obj
.ops
== &sock_ops
);
1024 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
1026 /* we may change event later, remove from loop here */
1027 if (event
& (POLLERR
|POLLHUP
)) set_fd_events( sock
->fd
, -1 );
1029 switch (sock
->state
)
1031 case SOCK_UNCONNECTED
:
1034 case SOCK_CONNECTING
:
1035 if (event
& (POLLERR
|POLLHUP
))
1037 sock
->state
= SOCK_UNCONNECTED
;
1039 error
= sock_error( fd
);
1041 else if (event
& POLLOUT
)
1043 sock
->state
= SOCK_CONNECTED
;
1044 sock
->connect_time
= current_time
;
1048 case SOCK_LISTENING
:
1049 if (event
& (POLLERR
|POLLHUP
))
1050 error
= sock_error( fd
);
1053 case SOCK_CONNECTED
:
1054 case SOCK_CONNECTIONLESS
:
1055 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
1060 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
1061 * has been closed, so we need to check for it explicitly here */
1062 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
1071 /* EAGAIN can happen if an async recv() falls between the server's poll()
1072 call and the invocation of this routine */
1073 if ( errno
!= EAGAIN
)
1078 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
1083 if ((hangup_seen
|| event
& (POLLHUP
| POLLERR
)) && (!sock
->rd_shutdown
|| !sock
->wr_shutdown
))
1085 error
= error
? error
: sock_error( fd
);
1086 if ( (event
& POLLERR
) || ( sock_shutdown_type
== SOCK_SHUTDOWN_EOF
&& (event
& POLLHUP
) ))
1087 sock
->wr_shutdown
= 1;
1088 sock
->rd_shutdown
= 1;
1091 fprintf(stderr
, "socket %p aborted by error %d, event: %x\n", sock
, error
, event
);
1099 complete_async_polls( sock
, event
, error
);
1101 event
= sock_dispatch_asyncs( sock
, event
, error
);
1102 sock_dispatch_events( sock
, prevstate
, event
, error
);
1104 sock_reselect( sock
);
1107 static void sock_dump( struct object
*obj
, int verbose
)
1109 struct sock
*sock
= (struct sock
*)obj
;
1110 assert( obj
->ops
== &sock_ops
);
1111 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
1112 sock
->fd
, sock
->state
,
1113 sock
->mask
, sock
->pending_events
, sock
->reported_events
);
1116 static int poll_flags_from_afd( struct sock
*sock
, int flags
)
1120 /* A connection-mode socket which has never been connected does
1121 * not return write or hangup events, but Linux returns
1122 * POLLOUT | POLLHUP. */
1123 if (sock
->state
== SOCK_UNCONNECTED
)
1126 if (flags
& (AFD_POLL_READ
| AFD_POLL_ACCEPT
))
1128 if ((flags
& AFD_POLL_HUP
) && sock
->type
== WS_SOCK_STREAM
)
1130 if (flags
& AFD_POLL_OOB
)
1131 ev
|= is_oobinline( sock
) ? POLLIN
: POLLPRI
;
1132 if (flags
& AFD_POLL_WRITE
)
1138 static int sock_get_poll_events( struct fd
*fd
)
1140 struct sock
*sock
= get_fd_user( fd
);
1141 unsigned int mask
= sock
->mask
& ~sock
->reported_events
;
1142 struct poll_req
*req
;
1145 assert( sock
->obj
.ops
== &sock_ops
);
1147 if (!sock
->type
) /* not initialized yet */
1150 switch (sock
->state
)
1152 case SOCK_UNCONNECTED
:
1153 /* A connection-mode Windows socket which has never been connected does
1154 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1155 * need to return -1 here, to prevent the socket from being polled on at
1159 case SOCK_CONNECTING
:
1162 case SOCK_LISTENING
:
1163 if (!list_empty( &sock
->accept_list
) || (mask
& AFD_POLL_ACCEPT
))
1167 case SOCK_CONNECTED
:
1168 case SOCK_CONNECTIONLESS
:
1169 if (sock
->accept_recv_req
)
1173 else if (async_queued( &sock
->read_q
))
1175 if (async_waiting( &sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
1179 if (!sock
->rd_shutdown
)
1181 if (mask
& AFD_POLL_READ
)
1183 if (mask
& AFD_POLL_OOB
)
1187 /* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
1188 if (sock
->state
== SOCK_CONNECTED
&& (mask
& AFD_POLL_HUP
) && !(sock
->reported_events
& AFD_POLL_READ
))
1192 if (async_queued( &sock
->write_q
))
1194 if (async_waiting( &sock
->write_q
)) ev
|= POLLOUT
;
1196 else if (!sock
->wr_shutdown
&& (mask
& AFD_POLL_WRITE
))
1204 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1208 for (i
= 0; i
< req
->count
; ++i
)
1210 if (req
->sockets
[i
].sock
!= sock
) continue;
1212 ev
|= poll_flags_from_afd( sock
, req
->sockets
[i
].flags
);
1219 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
1221 return FD_TYPE_SOCKET
;
1224 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
1226 struct sock
*sock
= get_fd_user( fd
);
1227 struct async_queue
*queue
;
1229 assert( sock
->obj
.ops
== &sock_ops
);
1233 case ASYNC_TYPE_READ
:
1234 if (sock
->rd_shutdown
)
1236 set_error( STATUS_PIPE_DISCONNECTED
);
1239 queue
= &sock
->read_q
;
1242 case ASYNC_TYPE_WRITE
:
1243 if (sock
->wr_shutdown
)
1245 set_error( STATUS_PIPE_DISCONNECTED
);
1248 queue
= &sock
->write_q
;
1252 set_error( STATUS_INVALID_PARAMETER
);
1256 if (sock
->state
!= SOCK_CONNECTED
)
1258 set_error( STATUS_PIPE_DISCONNECTED
);
1262 queue_async( queue
, async
);
1263 sock_reselect( sock
);
1265 set_error( STATUS_PENDING
);
1268 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
1270 struct sock
*sock
= get_fd_user( fd
);
1272 if (sock
->wr_shutdown_pending
&& list_empty( &sock
->write_q
.queue
))
1273 shutdown( get_unix_fd( sock
->fd
), SHUT_WR
);
1275 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1276 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1278 if (queue
!= &sock
->ifchange_q
&& sock
->type
)
1279 sock_reselect( sock
);
1282 static struct fd
*sock_get_fd( struct object
*obj
)
1284 struct sock
*sock
= (struct sock
*)obj
;
1285 return (struct fd
*)grab_object( sock
->fd
);
1288 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
1290 struct sock
*sock
= (struct sock
*)obj
;
1292 if (sock
->obj
.handle_count
== 1) /* last handle */
1294 struct accept_req
*accept_req
, *accept_next
;
1295 struct poll_req
*poll_req
, *poll_next
;
1297 if (sock
->accept_recv_req
)
1298 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
1300 LIST_FOR_EACH_ENTRY_SAFE( accept_req
, accept_next
, &sock
->accept_list
, struct accept_req
, entry
)
1301 async_terminate( accept_req
->async
, STATUS_CANCELLED
);
1303 if (sock
->connect_req
)
1304 async_terminate( sock
->connect_req
->async
, STATUS_CANCELLED
);
1306 LIST_FOR_EACH_ENTRY_SAFE( poll_req
, poll_next
, &poll_list
, struct poll_req
, entry
)
1308 struct iosb
*iosb
= poll_req
->iosb
;
1311 if (iosb
->status
!= STATUS_PENDING
) continue;
1313 for (i
= 0; i
< poll_req
->count
; ++i
)
1315 if (poll_req
->sockets
[i
].sock
== sock
)
1317 iosb
->status
= STATUS_SUCCESS
;
1318 poll_req
->output
[i
].flags
= AFD_POLL_CLOSE
;
1319 poll_req
->output
[i
].status
= 0;
1323 if (iosb
->status
!= STATUS_PENDING
)
1325 iosb
->out_data
= poll_req
->output
;
1326 iosb
->out_size
= poll_req
->count
* sizeof(*poll_req
->output
);
1327 async_terminate( poll_req
->async
, STATUS_ALERTED
);
1335 static void sock_destroy( struct object
*obj
)
1337 struct sock
*sock
= (struct sock
*)obj
;
1339 assert( obj
->ops
== &sock_ops
);
1341 /* FIXME: special socket shutdown stuff? */
1343 if ( sock
->deferred
)
1344 release_object( sock
->deferred
);
1346 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
1347 sock_release_ifchange( sock
);
1348 free_async_queue( &sock
->read_q
);
1349 free_async_queue( &sock
->write_q
);
1350 free_async_queue( &sock
->ifchange_q
);
1351 free_async_queue( &sock
->accept_q
);
1352 free_async_queue( &sock
->connect_q
);
1353 free_async_queue( &sock
->poll_q
);
1354 if (sock
->event
) release_object( sock
->event
);
1357 /* shut the socket down to force pending poll() calls in the client to return */
1358 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
1359 release_object( sock
->fd
);
1363 static struct sock
*create_socket(void)
1367 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
1369 sock
->state
= SOCK_UNCONNECTED
;
1371 sock
->pending_events
= 0;
1372 sock
->reported_events
= 0;
1381 sock
->connect_time
= 0;
1382 sock
->deferred
= NULL
;
1383 sock
->ifchange_obj
= NULL
;
1384 sock
->accept_recv_req
= NULL
;
1385 sock
->connect_req
= NULL
;
1386 memset( &sock
->addr
, 0, sizeof(sock
->addr
) );
1388 sock
->rd_shutdown
= 0;
1389 sock
->wr_shutdown
= 0;
1390 sock
->wr_shutdown_pending
= 0;
1391 sock
->nonblocking
= 0;
1397 init_async_queue( &sock
->read_q
);
1398 init_async_queue( &sock
->write_q
);
1399 init_async_queue( &sock
->ifchange_q
);
1400 init_async_queue( &sock
->accept_q
);
1401 init_async_queue( &sock
->connect_q
);
1402 init_async_queue( &sock
->poll_q
);
1403 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
1404 list_init( &sock
->accept_list
);
1408 static int get_unix_family( int family
)
1412 case WS_AF_INET
: return AF_INET
;
1413 case WS_AF_INET6
: return AF_INET6
;
1415 case WS_AF_IPX
: return AF_IPX
;
1418 case WS_AF_IRDA
: return AF_IRDA
;
1420 case WS_AF_UNSPEC
: return AF_UNSPEC
;
1425 static int get_unix_type( int type
)
1429 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
1430 case WS_SOCK_RAW
: return SOCK_RAW
;
1431 case WS_SOCK_STREAM
: return SOCK_STREAM
;
1436 static int get_unix_protocol( int protocol
)
1438 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1443 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
1444 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1445 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1446 case WS_IPPROTO_IPV4
: return IPPROTO_IPIP
;
1447 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1448 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1449 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1450 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1455 static void set_dont_fragment( int fd
, int level
, int value
)
1459 if (level
== IPPROTO_IP
)
1462 optname
= IP_DONTFRAG
;
1463 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1464 optname
= IP_MTU_DISCOVER
;
1465 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1472 #ifdef IPV6_DONTFRAG
1473 optname
= IPV6_DONTFRAG
;
1474 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1475 optname
= IPV6_MTU_DISCOVER
;
1476 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1482 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1485 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
, unsigned int flags
)
1487 unsigned int options
= 0;
1488 int sockfd
, unix_type
, unix_family
, unix_protocol
, value
;
1491 unix_family
= get_unix_family( family
);
1492 unix_type
= get_unix_type( type
);
1493 unix_protocol
= get_unix_protocol( protocol
);
1495 if (unix_protocol
< 0)
1497 if (type
&& unix_type
< 0)
1498 set_win32_error( WSAESOCKTNOSUPPORT
);
1500 set_win32_error( WSAEPROTONOSUPPORT
);
1503 if (unix_family
< 0)
1505 if (family
>= 0 && unix_type
< 0)
1506 set_win32_error( WSAESOCKTNOSUPPORT
);
1508 set_win32_error( WSAEAFNOSUPPORT
);
1512 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1515 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1516 else set_win32_error( sock_get_error( errno
));
1519 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1521 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1524 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1527 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1530 /* Should we retrieve val using a getsockopt call and then
1531 * set the modified one? */
1532 val
.ipx_pt
= ipx_type
;
1533 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1538 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1540 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1541 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1542 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1543 else if (unix_type
== SOCK_STREAM
)
1544 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1548 if (unix_family
== AF_INET6
)
1550 static const int enable
= 1;
1551 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1555 len
= sizeof(value
);
1556 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_RCVBUF
, &value
, &len
))
1557 sock
->rcvbuf
= value
;
1559 len
= sizeof(value
);
1560 if (!getsockopt( sockfd
, SOL_SOCKET
, SO_SNDBUF
, &value
, &len
))
1561 sock
->sndbuf
= value
;
1563 sock
->state
= (type
== WS_SOCK_STREAM
? SOCK_UNCONNECTED
: SOCK_CONNECTIONLESS
);
1564 sock
->flags
= flags
;
1565 sock
->proto
= protocol
;
1567 sock
->family
= family
;
1571 options
= get_fd_options( sock
->fd
);
1572 release_object( sock
->fd
);
1575 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1580 /* We can't immediately allow caching for a connection-mode socket, since it
1581 * might be accepted into (changing the underlying fd object.) */
1582 if (sock
->type
!= WS_SOCK_STREAM
) allow_fd_caching( sock
->fd
);
1587 /* accepts a socket and inits it */
1588 static int accept_new_fd( struct sock
*sock
)
1591 /* Try to accept(2). We can't be safe that this an already connected socket
1592 * or that accept() is allowed on it. In those cases we will get -1/errno
1595 struct sockaddr saddr
;
1596 socklen_t slen
= sizeof(saddr
);
1597 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1599 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1601 set_error( sock_get_ntstatus( errno
));
1605 /* accept a socket (creates a new fd) */
1606 static struct sock
*accept_socket( struct sock
*sock
)
1608 struct sock
*acceptsock
;
1611 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1613 if ( sock
->deferred
)
1615 acceptsock
= sock
->deferred
;
1616 sock
->deferred
= NULL
;
1620 union unix_sockaddr unix_addr
;
1623 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1624 if (!(acceptsock
= create_socket()))
1630 /* newly created socket gets the same properties of the listening socket */
1631 acceptsock
->state
= SOCK_CONNECTED
;
1632 acceptsock
->bound
= 1;
1633 acceptsock
->nonblocking
= sock
->nonblocking
;
1634 acceptsock
->mask
= sock
->mask
;
1635 acceptsock
->proto
= sock
->proto
;
1636 acceptsock
->type
= sock
->type
;
1637 acceptsock
->family
= sock
->family
;
1638 acceptsock
->window
= sock
->window
;
1639 acceptsock
->message
= sock
->message
;
1640 acceptsock
->connect_time
= current_time
;
1641 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
1642 acceptsock
->flags
= sock
->flags
;
1643 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1644 get_fd_options( sock
->fd
) )))
1646 release_object( acceptsock
);
1649 unix_len
= sizeof(unix_addr
);
1650 if (!getsockname( acceptfd
, &unix_addr
.addr
, &unix_len
))
1651 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1654 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1655 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1656 sock_reselect( sock
);
1660 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
1662 union unix_sockaddr unix_addr
;
1667 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
1669 if ( sock
->deferred
)
1671 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
1672 get_fd_options( acceptsock
->fd
) );
1676 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
1678 release_object( sock
->deferred
);
1679 sock
->deferred
= NULL
;
1683 if ((acceptfd
= accept_new_fd( sock
)) == -1)
1686 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1687 get_fd_options( acceptsock
->fd
) )))
1691 acceptsock
->state
= SOCK_CONNECTED
;
1692 acceptsock
->pending_events
= 0;
1693 acceptsock
->reported_events
= 0;
1694 acceptsock
->proto
= sock
->proto
;
1695 acceptsock
->type
= sock
->type
;
1696 acceptsock
->family
= sock
->family
;
1697 acceptsock
->wparam
= 0;
1698 acceptsock
->deferred
= NULL
;
1699 acceptsock
->connect_time
= current_time
;
1700 fd_copy_completion( acceptsock
->fd
, newfd
);
1701 release_object( acceptsock
->fd
);
1702 acceptsock
->fd
= newfd
;
1704 unix_len
= sizeof(unix_addr
);
1705 if (!getsockname( get_unix_fd( newfd
), &unix_addr
.addr
, &unix_len
))
1706 acceptsock
->addr_len
= sockaddr_from_unix( &unix_addr
, &acceptsock
->addr
.addr
, sizeof(acceptsock
->addr
) );
1709 sock
->pending_events
&= ~AFD_POLL_ACCEPT
;
1710 sock
->reported_events
&= ~AFD_POLL_ACCEPT
;
1711 sock_reselect( sock
);
1718 static int bind_to_index( int fd
, in_addr_t bind_addr
, unsigned int index
)
1720 return setsockopt( fd
, IPPROTO_IP
, IP_BOUND_IF
, &index
, sizeof(index
) );
1723 #elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER)
1725 struct interface_filter
1727 struct sock_filter iface_memaddr
;
1728 struct sock_filter iface_rule
;
1729 struct sock_filter ip_memaddr
;
1730 struct sock_filter ip_rule
;
1731 struct sock_filter return_keep
;
1732 struct sock_filter return_dump
;
1734 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
1735 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1736 /sizeof(struct sock_filter)
1737 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
1738 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
1739 /sizeof(struct sock_filter)
1740 # define FILTER_JUMP_NEXT() (u_char)(0)
1741 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
1742 static struct interface_filter generic_interface_filter
=
1744 /* This filter rule allows incoming packets on the specified interface, which works for all
1745 * remotely generated packets and for locally generated broadcast packets. */
1746 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
1747 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
1748 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
1749 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
1750 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
1751 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
1752 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
1753 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
1756 static int bind_to_index( int fd
, in_addr_t bind_addr
, unsigned int index
)
1758 in_addr_t ifindex
= htonl( index
);
1759 struct interface_filter specific_interface_filter
;
1760 struct sock_fprog filter_prog
;
1763 if ((ret
= setsockopt( fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
) )) < 0)
1766 specific_interface_filter
= generic_interface_filter
;
1767 specific_interface_filter
.iface_rule
.k
= index
;
1768 specific_interface_filter
.ip_rule
.k
= htonl( bind_addr
);
1769 filter_prog
.len
= sizeof(generic_interface_filter
) / sizeof(struct sock_filter
);
1770 filter_prog
.filter
= (struct sock_filter
*)&specific_interface_filter
;
1771 return setsockopt( fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
) );
1776 static int bind_to_index( int fd
, in_addr_t bind_addr
, unsigned int index
)
1782 #endif /* LINUX_BOUND_IF */
1784 /* Take bind() calls on any name corresponding to a local network adapter and
1785 * restrict the given socket to operating only on the specified interface. This
1786 * restriction consists of two components:
1787 * 1) An outgoing packet restriction suggesting the egress interface for all
1789 * 2) An incoming packet restriction dropping packets not meant for the
1791 * If the function succeeds in placing these restrictions, then the name for the
1792 * bind() may safely be changed to INADDR_ANY, permitting the transmission and
1793 * receipt of broadcast packets on the socket. This behavior is only relevant to
1794 * UDP sockets and is needed for applications that expect to be able to receive
1795 * broadcast packets on a socket that is bound to a specific network interface.
1797 static int bind_to_interface( struct sock
*sock
, const struct sockaddr_in
*addr
)
1799 in_addr_t bind_addr
= addr
->sin_addr
.s_addr
;
1800 struct ifaddrs
*ifaddrs
, *ifaddr
;
1801 int fd
= get_unix_fd( sock
->fd
);
1802 static const int enable
= 1;
1805 if (bind_addr
== htonl( INADDR_ANY
) || bind_addr
== htonl( INADDR_LOOPBACK
))
1807 if (sock
->type
!= WS_SOCK_DGRAM
)
1810 if (getifaddrs( &ifaddrs
) < 0) return 0;
1812 for (ifaddr
= ifaddrs
; ifaddr
!= NULL
; ifaddr
= ifaddr
->ifa_next
)
1814 if (ifaddr
->ifa_addr
&& ifaddr
->ifa_addr
->sa_family
== AF_INET
1815 && ((struct sockaddr_in
*)ifaddr
->ifa_addr
)->sin_addr
.s_addr
== bind_addr
)
1817 index
= if_nametoindex( ifaddr
->ifa_name
);
1821 fprintf( stderr
, "Unable to look up interface index for %s: %s\n",
1822 ifaddr
->ifa_name
, strerror( errno
) );
1826 freeifaddrs( ifaddrs
);
1828 if (bind_to_index( fd
, bind_addr
, index
) < 0)
1831 fprintf( stderr
, "failed to bind to interface: %s\n", strerror( errno
) );
1835 if (setsockopt( fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
) ) < 0)
1838 fprintf( stderr
, "failed to reuse address: %s\n", strerror( errno
) );
1847 /* return an errno value mapped to a WSA error */
1848 static unsigned int sock_get_error( int err
)
1852 case EINTR
: return WSAEINTR
;
1853 case EBADF
: return WSAEBADF
;
1855 case EACCES
: return WSAEACCES
;
1856 case EFAULT
: return WSAEFAULT
;
1857 case EINVAL
: return WSAEINVAL
;
1858 case EMFILE
: return WSAEMFILE
;
1860 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
1861 case EALREADY
: return WSAEALREADY
;
1862 case ENOTSOCK
: return WSAENOTSOCK
;
1863 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
1864 case EMSGSIZE
: return WSAEMSGSIZE
;
1865 case EPROTOTYPE
: return WSAEPROTOTYPE
;
1866 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
1867 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
1868 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
1869 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
1870 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
1871 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
1872 case EADDRINUSE
: return WSAEADDRINUSE
;
1873 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
1874 case ENETDOWN
: return WSAENETDOWN
;
1875 case ENETUNREACH
: return WSAENETUNREACH
;
1876 case ENETRESET
: return WSAENETRESET
;
1877 case ECONNABORTED
: return WSAECONNABORTED
;
1879 case ECONNRESET
: return WSAECONNRESET
;
1880 case ENOBUFS
: return WSAENOBUFS
;
1881 case EISCONN
: return WSAEISCONN
;
1882 case ENOTCONN
: return WSAENOTCONN
;
1883 case ESHUTDOWN
: return WSAESHUTDOWN
;
1884 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
1885 case ETIMEDOUT
: return WSAETIMEDOUT
;
1886 case ECONNREFUSED
: return WSAECONNREFUSED
;
1887 case ELOOP
: return WSAELOOP
;
1888 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
1889 case EHOSTDOWN
: return WSAEHOSTDOWN
;
1890 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
1891 case ENOTEMPTY
: return WSAENOTEMPTY
;
1893 case EPROCLIM
: return WSAEPROCLIM
;
1896 case EUSERS
: return WSAEUSERS
;
1899 case EDQUOT
: return WSAEDQUOT
;
1902 case ESTALE
: return WSAESTALE
;
1905 case EREMOTE
: return WSAEREMOTE
;
1911 perror("wineserver: sock_get_error() can't map error");
1916 static int sock_get_ntstatus( int err
)
1920 case EBADF
: return STATUS_INVALID_HANDLE
;
1921 case EBUSY
: return STATUS_DEVICE_BUSY
;
1923 case EACCES
: return STATUS_ACCESS_DENIED
;
1924 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
1925 case EINVAL
: return STATUS_INVALID_PARAMETER
;
1927 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
1929 case EWOULDBLOCK
: return STATUS_DEVICE_NOT_READY
;
1930 case EALREADY
: return STATUS_NETWORK_BUSY
;
1931 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
1932 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
1933 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
1934 case EPROTONOSUPPORT
:
1935 case ESOCKTNOSUPPORT
:
1938 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
1939 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
1940 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
1941 case EADDRINUSE
: return STATUS_SHARING_VIOLATION
;
1942 case EADDRNOTAVAIL
: return STATUS_INVALID_ADDRESS_COMPONENT
;
1943 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
1944 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
1945 case ENOTCONN
: return STATUS_INVALID_CONNECTION
;
1946 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
1947 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
1948 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
1949 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
1951 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
1952 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
1953 case EISCONN
: return STATUS_CONNECTION_ACTIVE
;
1955 case 0: return STATUS_SUCCESS
;
1958 perror("wineserver: sock_get_ntstatus() can't map error");
1959 return STATUS_UNSUCCESSFUL
;
1963 static struct accept_req
*alloc_accept_req( struct sock
*sock
, struct sock
*acceptsock
, struct async
*async
,
1964 const struct afd_accept_into_params
*params
)
1966 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
1970 req
->async
= (struct async
*)grab_object( async
);
1971 req
->iosb
= async_get_iosb( async
);
1972 req
->sock
= (struct sock
*)grab_object( sock
);
1973 req
->acceptsock
= acceptsock
;
1974 if (acceptsock
) grab_object( acceptsock
);
1980 req
->recv_len
= params
->recv_len
;
1981 req
->local_len
= params
->local_len
;
1987 static int sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
1989 struct sock
*sock
= get_fd_user( fd
);
1992 assert( sock
->obj
.ops
== &sock_ops
);
1994 if (code
!= IOCTL_AFD_WINE_CREATE
&& (unix_fd
= get_unix_fd( fd
)) < 0) return 0;
1998 case IOCTL_AFD_WINE_CREATE
:
2000 const struct afd_create_params
*params
= get_req_data();
2002 if (get_req_data_size() != sizeof(*params
))
2004 set_error( STATUS_INVALID_PARAMETER
);
2007 init_socket( sock
, params
->family
, params
->type
, params
->protocol
, params
->flags
);
2011 case IOCTL_AFD_WINE_ACCEPT
:
2013 struct sock
*acceptsock
;
2014 obj_handle_t handle
;
2016 if (get_reply_max_size() != sizeof(handle
))
2018 set_error( STATUS_BUFFER_TOO_SMALL
);
2022 if (!(acceptsock
= accept_socket( sock
)))
2024 struct accept_req
*req
;
2026 if (sock
->nonblocking
) return 0;
2027 if (get_error() != STATUS_DEVICE_NOT_READY
) return 0;
2029 if (!(req
= alloc_accept_req( sock
, NULL
, async
, NULL
))) return 0;
2030 list_add_tail( &sock
->accept_list
, &req
->entry
);
2032 async_set_completion_callback( async
, free_accept_req
, req
);
2033 queue_async( &sock
->accept_q
, async
);
2034 sock_reselect( sock
);
2035 set_error( STATUS_PENDING
);
2038 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
2039 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
2040 acceptsock
->wparam
= handle
;
2041 release_object( acceptsock
);
2042 set_reply_data( &handle
, sizeof(handle
) );
2046 case IOCTL_AFD_WINE_ACCEPT_INTO
:
2048 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
2049 const struct afd_accept_into_params
*params
= get_req_data();
2050 struct sock
*acceptsock
;
2051 unsigned int remote_len
;
2052 struct accept_req
*req
;
2054 if (get_req_data_size() != sizeof(*params
) ||
2055 get_reply_max_size() < params
->recv_len
||
2056 get_reply_max_size() - params
->recv_len
< params
->local_len
)
2058 set_error( STATUS_BUFFER_TOO_SMALL
);
2062 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
2063 if (remote_len
< sizeof(int))
2065 set_error( STATUS_INVALID_PARAMETER
);
2069 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
2072 if (acceptsock
->accept_recv_req
)
2074 release_object( acceptsock
);
2075 set_error( STATUS_INVALID_PARAMETER
);
2079 if (!(req
= alloc_accept_req( sock
, acceptsock
, async
, params
)))
2081 release_object( acceptsock
);
2084 list_add_tail( &sock
->accept_list
, &req
->entry
);
2085 acceptsock
->accept_recv_req
= req
;
2086 release_object( acceptsock
);
2088 acceptsock
->wparam
= params
->accept_handle
;
2089 async_set_completion_callback( async
, free_accept_req
, req
);
2090 queue_async( &sock
->accept_q
, async
);
2091 sock_reselect( sock
);
2092 set_error( STATUS_PENDING
);
2096 case IOCTL_AFD_LISTEN
:
2098 const struct afd_listen_params
*params
= get_req_data();
2100 if (get_req_data_size() < sizeof(*params
))
2102 set_error( STATUS_INVALID_PARAMETER
);
2108 set_error( STATUS_INVALID_PARAMETER
);
2112 if (listen( unix_fd
, params
->backlog
) < 0)
2114 set_error( sock_get_ntstatus( errno
) );
2118 sock
->state
= SOCK_LISTENING
;
2120 /* a listening socket can no longer be accepted into */
2121 allow_fd_caching( sock
->fd
);
2123 /* we may already be selecting for AFD_POLL_ACCEPT */
2124 sock_reselect( sock
);
2128 case IOCTL_AFD_WINE_CONNECT
:
2130 const struct afd_connect_params
*params
= get_req_data();
2131 const struct WS_sockaddr
*addr
;
2132 union unix_sockaddr unix_addr
;
2133 struct connect_req
*req
;
2137 if (get_req_data_size() < sizeof(*params
) ||
2138 get_req_data_size() - sizeof(*params
) < params
->addr_len
)
2140 set_error( STATUS_BUFFER_TOO_SMALL
);
2143 send_len
= get_req_data_size() - sizeof(*params
) - params
->addr_len
;
2144 addr
= (const struct WS_sockaddr
*)(params
+ 1);
2146 if (!params
->synchronous
&& !sock
->bound
)
2148 set_error( STATUS_INVALID_PARAMETER
);
2152 if (sock
->accept_recv_req
)
2154 set_error( STATUS_INVALID_PARAMETER
);
2158 if (sock
->connect_req
)
2160 set_error( STATUS_INVALID_PARAMETER
);
2164 if (sock
->state
== SOCK_CONNECTING
)
2166 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
2167 * but there's no status code that maps to WSAEALREADY... */
2168 set_error( params
->synchronous
? STATUS_ADDRESS_ALREADY_ASSOCIATED
: STATUS_INVALID_PARAMETER
);
2172 unix_len
= sockaddr_to_unix( addr
, params
->addr_len
, &unix_addr
);
2175 set_error( STATUS_INVALID_ADDRESS
);
2178 if (unix_addr
.addr
.sa_family
== AF_INET
&& !memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 ))
2179 unix_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_LOOPBACK
);
2181 ret
= connect( unix_fd
, &unix_addr
.addr
, unix_len
);
2182 if (ret
< 0 && errno
!= EINPROGRESS
)
2184 set_error( sock_get_ntstatus( errno
) );
2188 /* a connected or connecting socket can no longer be accepted into */
2189 allow_fd_caching( sock
->fd
);
2191 unix_len
= sizeof(unix_addr
);
2192 if (!sock
->bound
&& !getsockname( unix_fd
, &unix_addr
.addr
, &unix_len
))
2193 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2198 sock
->state
= SOCK_CONNECTED
;
2200 if (!send_len
) return 1;
2203 if (!(req
= mem_alloc( sizeof(*req
) )))
2206 sock
->state
= SOCK_CONNECTING
;
2208 if (params
->synchronous
&& sock
->nonblocking
)
2210 sock_reselect( sock
);
2211 set_error( STATUS_DEVICE_NOT_READY
);
2215 req
->async
= (struct async
*)grab_object( async
);
2216 req
->iosb
= async_get_iosb( async
);
2217 req
->sock
= (struct sock
*)grab_object( sock
);
2218 req
->addr_len
= params
->addr_len
;
2219 req
->send_len
= send_len
;
2220 req
->send_cursor
= 0;
2222 async_set_completion_callback( async
, free_connect_req
, req
);
2223 sock
->connect_req
= req
;
2224 queue_async( &sock
->connect_q
, async
);
2225 sock_reselect( sock
);
2226 set_error( STATUS_PENDING
);
2230 case IOCTL_AFD_WINE_SHUTDOWN
:
2234 if (get_req_data_size() < sizeof(int))
2236 set_error( STATUS_BUFFER_TOO_SMALL
);
2239 how
= *(int *)get_req_data();
2243 set_error( STATUS_INVALID_PARAMETER
);
2247 if (sock
->state
!= SOCK_CONNECTED
&& sock
->state
!= SOCK_CONNECTIONLESS
)
2249 set_error( STATUS_INVALID_CONNECTION
);
2255 sock
->rd_shutdown
= 1;
2257 if (how
!= SD_RECEIVE
)
2259 sock
->wr_shutdown
= 1;
2260 if (list_empty( &sock
->write_q
.queue
))
2261 shutdown( unix_fd
, SHUT_WR
);
2263 sock
->wr_shutdown_pending
= 1;
2268 if (sock
->event
) release_object( sock
->event
);
2272 sock
->nonblocking
= 1;
2275 sock_reselect( sock
);
2279 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE
:
2283 if (get_req_data_size() < sizeof(int))
2285 set_error( STATUS_BUFFER_TOO_SMALL
);
2288 force_async
= *(int *)get_req_data();
2290 if (sock
->nonblocking
&& !force_async
)
2292 set_error( STATUS_DEVICE_NOT_READY
);
2295 if (!sock_get_ifchange( sock
)) return 0;
2296 queue_async( &sock
->ifchange_q
, async
);
2297 set_error( STATUS_PENDING
);
2301 case IOCTL_AFD_WINE_FIONBIO
:
2302 if (get_req_data_size() < sizeof(int))
2304 set_error( STATUS_BUFFER_TOO_SMALL
);
2307 if (*(int *)get_req_data())
2309 sock
->nonblocking
= 1;
2315 set_error( STATUS_INVALID_PARAMETER
);
2318 sock
->nonblocking
= 0;
2322 case IOCTL_AFD_GET_EVENTS
:
2324 struct afd_get_events_params params
= {0};
2327 if (get_reply_max_size() < sizeof(params
))
2329 set_error( STATUS_INVALID_PARAMETER
);
2333 params
.flags
= sock
->pending_events
& sock
->mask
;
2334 for (i
= 0; i
< ARRAY_SIZE( params
.status
); ++i
)
2335 params
.status
[i
] = sock_get_ntstatus( sock
->errors
[i
] );
2337 sock
->pending_events
= 0;
2338 sock_reselect( sock
);
2340 set_reply_data( ¶ms
, sizeof(params
) );
2344 case IOCTL_AFD_EVENT_SELECT
:
2346 struct event
*event
= NULL
;
2347 obj_handle_t event_handle
;
2350 if (is_machine_64bit( current
->process
->machine
))
2352 const struct afd_event_select_params_64
*params
= get_req_data();
2354 if (get_req_data_size() < sizeof(params
))
2356 set_error( STATUS_INVALID_PARAMETER
);
2360 event_handle
= params
->event
;
2361 mask
= params
->mask
;
2365 const struct afd_event_select_params_32
*params
= get_req_data();
2367 if (get_req_data_size() < sizeof(params
))
2369 set_error( STATUS_INVALID_PARAMETER
);
2373 event_handle
= params
->event
;
2374 mask
= params
->mask
;
2377 if ((event_handle
|| mask
) &&
2378 !(event
= get_event_obj( current
->process
, event_handle
, EVENT_MODIFY_STATE
)))
2380 set_error( STATUS_INVALID_PARAMETER
);
2384 if (sock
->event
) release_object( sock
->event
);
2385 sock
->event
= event
;
2390 sock
->nonblocking
= 1;
2392 sock_reselect( sock
);
2397 case IOCTL_AFD_WINE_MESSAGE_SELECT
:
2399 const struct afd_message_select_params
*params
= get_req_data();
2401 if (get_req_data_size() < sizeof(params
))
2403 set_error( STATUS_BUFFER_TOO_SMALL
);
2407 if (sock
->event
) release_object( sock
->event
);
2411 sock
->pending_events
= 0;
2412 sock
->reported_events
= 0;
2415 sock
->mask
= params
->mask
;
2416 sock
->window
= params
->window
;
2417 sock
->message
= params
->message
;
2418 sock
->wparam
= params
->handle
;
2419 sock
->nonblocking
= 1;
2421 sock_reselect( sock
);
2426 case IOCTL_AFD_BIND
:
2428 const struct afd_bind_params
*params
= get_req_data();
2429 union unix_sockaddr unix_addr
, bind_addr
;
2430 data_size_t in_size
;
2433 /* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
2435 if (get_req_data_size() < get_reply_max_size())
2437 set_error( STATUS_BUFFER_TOO_SMALL
);
2440 in_size
= get_req_data_size() - get_reply_max_size();
2441 if (in_size
< offsetof(struct afd_bind_params
, addr
.sa_data
)
2442 || get_reply_max_size() < sizeof(struct WS_sockaddr
))
2444 set_error( STATUS_INVALID_PARAMETER
);
2450 set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED
);
2454 unix_len
= sockaddr_to_unix( ¶ms
->addr
, in_size
- sizeof(int), &unix_addr
);
2457 set_error( STATUS_INVALID_ADDRESS
);
2460 bind_addr
= unix_addr
;
2462 if (unix_addr
.addr
.sa_family
== WS_AF_INET
)
2464 if (!memcmp( &unix_addr
.in
.sin_addr
, magic_loopback_addr
, 4 )
2465 || bind_to_interface( sock
, &unix_addr
.in
))
2466 bind_addr
.in
.sin_addr
.s_addr
= htonl( INADDR_ANY
);
2469 if (bind( unix_fd
, &bind_addr
.addr
, unix_len
) < 0)
2471 if (errno
== EADDRINUSE
)
2474 socklen_t len
= sizeof(reuse
);
2476 if (!getsockopt( unix_fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&reuse
, &len
) && reuse
)
2480 set_error( sock_get_ntstatus( errno
) );
2486 unix_len
= sizeof(bind_addr
);
2487 if (!getsockname( unix_fd
, &bind_addr
.addr
, &unix_len
))
2489 /* store the interface or magic loopback address instead of the
2490 * actual unix address */
2491 if (bind_addr
.addr
.sa_family
== AF_INET
)
2492 bind_addr
.in
.sin_addr
= unix_addr
.in
.sin_addr
;
2493 sock
->addr_len
= sockaddr_from_unix( &bind_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
2496 if (get_reply_max_size() >= sock
->addr_len
)
2497 set_reply_data( &sock
->addr
, sock
->addr_len
);
2501 case IOCTL_AFD_GETSOCKNAME
:
2504 set_error( STATUS_INVALID_PARAMETER
);
2508 if (get_reply_max_size() < sock
->addr_len
)
2510 set_error( STATUS_BUFFER_TOO_SMALL
);
2514 set_reply_data( &sock
->addr
, sock
->addr_len
);
2517 case IOCTL_AFD_WINE_DEFER
:
2519 const obj_handle_t
*handle
= get_req_data();
2520 struct sock
*acceptsock
;
2522 if (get_req_data_size() < sizeof(*handle
))
2524 set_error( STATUS_BUFFER_TOO_SMALL
);
2528 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, *handle
, 0, &sock_ops
);
2529 if (!acceptsock
) return 0;
2531 sock
->deferred
= acceptsock
;
2535 case IOCTL_AFD_WINE_GET_INFO
:
2537 struct afd_get_info_params params
;
2539 if (get_reply_max_size() < sizeof(params
))
2541 set_error( STATUS_BUFFER_TOO_SMALL
);
2545 params
.family
= sock
->family
;
2546 params
.type
= sock
->type
;
2547 params
.protocol
= sock
->proto
;
2548 set_reply_data( ¶ms
, sizeof(params
) );
2552 case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN
:
2554 int listening
= (sock
->state
== SOCK_LISTENING
);
2556 if (get_reply_max_size() < sizeof(listening
))
2558 set_error( STATUS_BUFFER_TOO_SMALL
);
2562 set_reply_data( &listening
, sizeof(listening
) );
2566 case IOCTL_AFD_WINE_GET_SO_ERROR
:
2569 socklen_t len
= sizeof(error
);
2572 if (get_reply_max_size() < sizeof(error
))
2574 set_error( STATUS_BUFFER_TOO_SMALL
);
2578 if (getsockopt( unix_fd
, SOL_SOCKET
, SO_ERROR
, (char *)&error
, &len
) < 0)
2580 set_error( sock_get_ntstatus( errno
) );
2586 for (i
= 0; i
< ARRAY_SIZE( sock
->errors
); ++i
)
2588 if (sock
->errors
[i
])
2590 error
= sock
->errors
[i
];
2596 set_reply_data( &error
, sizeof(error
) );
2600 case IOCTL_AFD_WINE_GET_SO_RCVBUF
:
2602 int rcvbuf
= sock
->rcvbuf
;
2604 if (get_reply_max_size() < sizeof(rcvbuf
))
2606 set_error( STATUS_BUFFER_TOO_SMALL
);
2610 set_reply_data( &rcvbuf
, sizeof(rcvbuf
) );
2614 case IOCTL_AFD_WINE_SET_SO_RCVBUF
:
2618 if (get_req_data_size() < sizeof(rcvbuf
))
2620 set_error( STATUS_BUFFER_TOO_SMALL
);
2623 rcvbuf
= *(DWORD
*)get_req_data();
2625 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_RCVBUF
, (char *)&rcvbuf
, sizeof(rcvbuf
) ))
2626 sock
->rcvbuf
= rcvbuf
;
2628 set_error( sock_get_ntstatus( errno
) );
2632 case IOCTL_AFD_WINE_GET_SO_RCVTIMEO
:
2634 DWORD rcvtimeo
= sock
->rcvtimeo
;
2636 if (get_reply_max_size() < sizeof(rcvtimeo
))
2638 set_error( STATUS_BUFFER_TOO_SMALL
);
2642 set_reply_data( &rcvtimeo
, sizeof(rcvtimeo
) );
2646 case IOCTL_AFD_WINE_SET_SO_RCVTIMEO
:
2650 if (get_req_data_size() < sizeof(rcvtimeo
))
2652 set_error( STATUS_BUFFER_TOO_SMALL
);
2655 rcvtimeo
= *(DWORD
*)get_req_data();
2657 sock
->rcvtimeo
= rcvtimeo
;
2661 case IOCTL_AFD_WINE_GET_SO_SNDBUF
:
2663 int sndbuf
= sock
->sndbuf
;
2665 if (get_reply_max_size() < sizeof(sndbuf
))
2667 set_error( STATUS_BUFFER_TOO_SMALL
);
2671 set_reply_data( &sndbuf
, sizeof(sndbuf
) );
2675 case IOCTL_AFD_WINE_SET_SO_SNDBUF
:
2679 if (get_req_data_size() < sizeof(sndbuf
))
2681 set_error( STATUS_BUFFER_TOO_SMALL
);
2684 sndbuf
= *(DWORD
*)get_req_data();
2689 /* setsockopt fails if a zero value is passed */
2690 sock
->sndbuf
= sndbuf
;
2695 if (!setsockopt( unix_fd
, SOL_SOCKET
, SO_SNDBUF
, (char *)&sndbuf
, sizeof(sndbuf
) ))
2696 sock
->sndbuf
= sndbuf
;
2698 set_error( sock_get_ntstatus( errno
) );
2702 case IOCTL_AFD_WINE_GET_SO_SNDTIMEO
:
2704 DWORD sndtimeo
= sock
->sndtimeo
;
2706 if (get_reply_max_size() < sizeof(sndtimeo
))
2708 set_error( STATUS_BUFFER_TOO_SMALL
);
2712 set_reply_data( &sndtimeo
, sizeof(sndtimeo
) );
2716 case IOCTL_AFD_WINE_SET_SO_SNDTIMEO
:
2720 if (get_req_data_size() < sizeof(sndtimeo
))
2722 set_error( STATUS_BUFFER_TOO_SMALL
);
2725 sndtimeo
= *(DWORD
*)get_req_data();
2727 sock
->sndtimeo
= sndtimeo
;
2732 set_error( STATUS_NOT_SUPPORTED
);
2737 static int poll_socket( struct sock
*poll_sock
, struct async
*async
, timeout_t timeout
,
2738 unsigned int count
, const struct poll_socket_input
*input
)
2740 struct poll_socket_output
*output
;
2741 struct poll_req
*req
;
2744 if (!(output
= mem_alloc( count
* sizeof(*output
) )))
2746 memset( output
, 0, count
* sizeof(*output
) );
2748 if (!(req
= mem_alloc( offsetof( struct poll_req
, sockets
[count
] ) )))
2754 req
->timeout
= NULL
;
2755 if (timeout
&& timeout
!= TIMEOUT_INFINITE
&&
2756 !(req
->timeout
= add_timeout_user( timeout
, async_poll_timeout
, req
)))
2763 for (i
= 0; i
< count
; ++i
)
2765 req
->sockets
[i
].sock
= (struct sock
*)get_handle_obj( current
->process
, input
[i
].socket
, 0, &sock_ops
);
2766 if (!req
->sockets
[i
].sock
)
2768 for (j
= 0; j
< i
; ++j
) release_object( req
->sockets
[i
].sock
);
2769 if (req
->timeout
) remove_timeout_user( req
->timeout
);
2774 req
->sockets
[i
].flags
= input
[i
].flags
;
2778 req
->async
= (struct async
*)grab_object( async
);
2779 req
->iosb
= async_get_iosb( async
);
2780 req
->output
= output
;
2782 list_add_tail( &poll_list
, &req
->entry
);
2783 async_set_completion_callback( async
, free_poll_req
, req
);
2784 queue_async( &poll_sock
->poll_q
, async
);
2786 if (!timeout
) req
->iosb
->status
= STATUS_SUCCESS
;
2788 for (i
= 0; i
< count
; ++i
)
2790 struct sock
*sock
= req
->sockets
[i
].sock
;
2791 struct pollfd pollfd
;
2794 pollfd
.fd
= get_unix_fd( sock
->fd
);
2795 pollfd
.events
= poll_flags_from_afd( sock
, req
->sockets
[i
].flags
);
2796 if (pollfd
.events
< 0 || poll( &pollfd
, 1, 0 ) < 0) continue;
2798 if ((req
->sockets
[i
].flags
& AFD_POLL_HUP
) && (pollfd
.revents
& POLLIN
) &&
2799 sock
->type
== WS_SOCK_STREAM
)
2803 if (!recv( get_unix_fd( sock
->fd
), &dummy
, 1, MSG_PEEK
))
2805 pollfd
.revents
&= ~POLLIN
;
2806 pollfd
.revents
|= POLLHUP
;
2810 flags
= get_poll_flags( sock
, pollfd
.revents
) & req
->sockets
[i
].flags
;
2813 req
->iosb
->status
= STATUS_SUCCESS
;
2814 output
[i
].flags
= flags
;
2815 output
[i
].status
= sock_get_ntstatus( sock_error( sock
->fd
) );
2819 if (req
->iosb
->status
!= STATUS_PENDING
)
2821 req
->iosb
->out_data
= output
;
2822 req
->iosb
->out_size
= count
* sizeof(*output
);
2823 async_terminate( req
->async
, STATUS_ALERTED
);
2826 for (i
= 0; i
< req
->count
; ++i
)
2827 sock_reselect( req
->sockets
[i
].sock
);
2828 set_error( STATUS_PENDING
);
2832 #ifdef HAVE_LINUX_RTNETLINK_H
2834 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
2835 static struct object
*ifchange_object
;
2837 static void ifchange_dump( struct object
*obj
, int verbose
);
2838 static struct fd
*ifchange_get_fd( struct object
*obj
);
2839 static void ifchange_destroy( struct object
*obj
);
2841 static int ifchange_get_poll_events( struct fd
*fd
);
2842 static void ifchange_poll_event( struct fd
*fd
, int event
);
2846 struct object obj
; /* object header */
2847 struct fd
*fd
; /* interface change file descriptor */
2848 struct list sockets
; /* list of sockets to send interface change notifications */
2851 static const struct object_ops ifchange_ops
=
2853 sizeof(struct ifchange
), /* size */
2854 &no_type
, /* type */
2855 ifchange_dump
, /* dump */
2856 no_add_queue
, /* add_queue */
2857 NULL
, /* remove_queue */
2858 NULL
, /* signaled */
2859 no_satisfied
, /* satisfied */
2860 no_signal
, /* signal */
2861 ifchange_get_fd
, /* get_fd */
2862 default_map_access
, /* map_access */
2863 default_get_sd
, /* get_sd */
2864 default_set_sd
, /* set_sd */
2865 no_get_full_name
, /* get_full_name */
2866 no_lookup_name
, /* lookup_name */
2867 no_link_name
, /* link_name */
2868 NULL
, /* unlink_name */
2869 no_open_file
, /* open_file */
2870 no_kernel_obj_list
, /* get_kernel_obj_list */
2871 no_close_handle
, /* close_handle */
2872 ifchange_destroy
/* destroy */
2875 static const struct fd_ops ifchange_fd_ops
=
2877 ifchange_get_poll_events
, /* get_poll_events */
2878 ifchange_poll_event
, /* poll_event */
2879 NULL
, /* get_fd_type */
2880 no_fd_read
, /* read */
2881 no_fd_write
, /* write */
2882 no_fd_flush
, /* flush */
2883 no_fd_get_file_info
, /* get_file_info */
2884 no_fd_get_volume_info
, /* get_volume_info */
2885 no_fd_ioctl
, /* ioctl */
2886 NULL
, /* queue_async */
2887 NULL
/* reselect_async */
2890 static void ifchange_dump( struct object
*obj
, int verbose
)
2892 assert( obj
->ops
== &ifchange_ops
);
2893 fprintf( stderr
, "Interface change\n" );
2896 static struct fd
*ifchange_get_fd( struct object
*obj
)
2898 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
2899 return (struct fd
*)grab_object( ifchange
->fd
);
2902 static void ifchange_destroy( struct object
*obj
)
2904 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
2905 assert( obj
->ops
== &ifchange_ops
);
2907 release_object( ifchange
->fd
);
2909 /* reset the global ifchange object so that it will be recreated if it is needed again */
2910 assert( obj
== ifchange_object
);
2911 ifchange_object
= NULL
;
2914 static int ifchange_get_poll_events( struct fd
*fd
)
2919 /* wake up all the sockets waiting for a change notification event */
2920 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
2922 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
2923 struct list
*ptr
, *next
;
2924 assert( obj
->ops
== &ifchange_ops
);
2925 assert( obj
== ifchange_object
);
2927 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
2929 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
2931 assert( sock
->ifchange_obj
);
2932 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
2933 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
2937 static void ifchange_poll_event( struct fd
*fd
, int event
)
2939 struct object
*ifchange
= get_fd_user( fd
);
2940 unsigned int status
= STATUS_PENDING
;
2941 char buffer
[PIPE_BUF
];
2944 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
2947 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
2948 return; /* retry when poll() says the socket is ready */
2949 status
= sock_get_ntstatus( errno
);
2953 struct nlmsghdr
*nlh
;
2955 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
2957 if (nlh
->nlmsg_type
== NLMSG_DONE
)
2959 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
2960 status
= STATUS_SUCCESS
;
2963 else status
= STATUS_CANCELLED
;
2965 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
2970 /* we only need one of these interface notification objects, all of the sockets dependent upon
2971 * it will wake up when a notification event occurs */
2972 static struct object
*get_ifchange( void )
2974 #ifdef HAVE_LINUX_RTNETLINK_H
2975 struct ifchange
*ifchange
;
2976 struct sockaddr_nl addr
;
2979 if (ifchange_object
)
2981 /* increment the refcount for each socket that uses the ifchange object */
2982 return grab_object( ifchange_object
);
2985 /* create the socket we need for processing interface change notifications */
2986 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
2989 set_error( sock_get_ntstatus( errno
));
2992 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
2993 memset( &addr
, 0, sizeof(addr
) );
2994 addr
.nl_family
= AF_NETLINK
;
2995 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
2996 /* bind the socket to the special netlink kernel interface */
2997 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
3000 set_error( sock_get_ntstatus( errno
));
3003 if (!(ifchange
= alloc_object( &ifchange_ops
)))
3006 set_error( STATUS_NO_MEMORY
);
3009 list_init( &ifchange
->sockets
);
3010 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
3012 release_object( ifchange
);
3013 set_error( STATUS_NO_MEMORY
);
3016 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
3018 /* the ifchange object is now successfully configured */
3019 ifchange_object
= &ifchange
->obj
;
3020 return &ifchange
->obj
;
3022 set_error( STATUS_NOT_SUPPORTED
);
3027 /* add the socket to the interface change notification list */
3028 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
3030 #ifdef HAVE_LINUX_RTNETLINK_H
3031 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
3033 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
3037 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
3038 static struct object
*sock_get_ifchange( struct sock
*sock
)
3040 struct object
*ifchange
;
3042 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
3043 return sock
->ifchange_obj
;
3045 if (!(ifchange
= get_ifchange()))
3048 /* add the socket to the ifchange notification list */
3049 ifchange_add_sock( ifchange
, sock
);
3050 sock
->ifchange_obj
= ifchange
;
3054 /* destroy an existing ifchange queue for a specific socket */
3055 static void sock_release_ifchange( struct sock
*sock
)
3057 if (sock
->ifchange_obj
)
3059 list_remove( &sock
->ifchange_entry
);
3060 release_object( sock
->ifchange_obj
);
3061 sock
->ifchange_obj
= NULL
;
3065 static void socket_device_dump( struct object
*obj
, int verbose
);
3066 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3067 unsigned int attr
, struct object
*root
);
3068 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3069 unsigned int sharing
, unsigned int options
);
3071 static const struct object_ops socket_device_ops
=
3073 sizeof(struct object
), /* size */
3074 &device_type
, /* type */
3075 socket_device_dump
, /* dump */
3076 no_add_queue
, /* add_queue */
3077 NULL
, /* remove_queue */
3078 NULL
, /* signaled */
3079 no_satisfied
, /* satisfied */
3080 no_signal
, /* signal */
3081 no_get_fd
, /* get_fd */
3082 default_map_access
, /* map_access */
3083 default_get_sd
, /* get_sd */
3084 default_set_sd
, /* set_sd */
3085 default_get_full_name
, /* get_full_name */
3086 socket_device_lookup_name
, /* lookup_name */
3087 directory_link_name
, /* link_name */
3088 default_unlink_name
, /* unlink_name */
3089 socket_device_open_file
, /* open_file */
3090 no_kernel_obj_list
, /* get_kernel_obj_list */
3091 no_close_handle
, /* close_handle */
3092 no_destroy
/* destroy */
3095 static void socket_device_dump( struct object
*obj
, int verbose
)
3097 fputs( "Socket device\n", stderr
);
3100 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
3101 unsigned int attr
, struct object
*root
)
3103 if (name
) name
->len
= 0;
3107 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
3108 unsigned int sharing
, unsigned int options
)
3112 if (!(sock
= create_socket())) return NULL
;
3113 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
3115 release_object( sock
);
3121 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
3122 unsigned int attr
, const struct security_descriptor
*sd
)
3124 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
3127 DECL_HANDLER(recv_socket
)
3129 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3130 unsigned int status
= req
->status
;
3131 timeout_t timeout
= 0;
3132 struct async
*async
;
3138 /* recv() returned EWOULDBLOCK, i.e. no data available yet */
3139 if (status
== STATUS_DEVICE_NOT_READY
&& !sock
->nonblocking
)
3141 /* Set a timeout on the async if necessary.
3143 * We want to do this *only* if the client gave us STATUS_DEVICE_NOT_READY.
3144 * If the client gave us STATUS_PENDING, it expects the async to always
3145 * block (it was triggered by WSARecv*() with a valid OVERLAPPED
3146 * structure) and for the timeout not to be respected. */
3147 if (is_fd_overlapped( fd
))
3148 timeout
= (timeout_t
)sock
->rcvtimeo
* -10000;
3150 status
= STATUS_PENDING
;
3153 if (status
== STATUS_PENDING
&& sock
->rd_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3155 sock
->pending_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3156 sock
->reported_events
&= ~(req
->oob
? AFD_POLL_OOB
: AFD_POLL_READ
);
3158 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3162 if (status
== STATUS_SUCCESS
)
3164 struct iosb
*iosb
= async_get_iosb( async
);
3165 iosb
->result
= req
->total
;
3166 release_object( iosb
);
3169 else if (status
== STATUS_PENDING
)
3173 set_error( status
);
3176 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3178 if (status
== STATUS_PENDING
)
3179 queue_async( &sock
->read_q
, async
);
3181 /* always reselect; we changed reported_events above */
3182 sock_reselect( sock
);
3184 reply
->wait
= async_handoff( async
, success
, NULL
, 0 );
3185 reply
->options
= get_fd_options( fd
);
3186 release_object( async
);
3188 release_object( sock
);
3191 DECL_HANDLER(poll_socket
)
3193 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3194 const struct poll_socket_input
*input
= get_req_data();
3195 struct async
*async
;
3200 count
= get_req_data_size() / sizeof(*input
);
3202 if ((async
= create_request_async( sock
->fd
, get_fd_comp_flags( sock
->fd
), &req
->async
)))
3204 reply
->wait
= async_handoff( async
, poll_socket( sock
, async
, req
->timeout
, count
, input
), NULL
, 0 );
3205 reply
->options
= get_fd_options( sock
->fd
);
3206 release_object( async
);
3209 release_object( sock
);
3212 DECL_HANDLER(send_socket
)
3214 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
3215 unsigned int status
= req
->status
;
3216 timeout_t timeout
= 0;
3217 struct async
*async
;
3223 if (sock
->type
== WS_SOCK_DGRAM
)
3225 /* sendto() and sendmsg() implicitly binds a socket */
3226 union unix_sockaddr unix_addr
;
3227 socklen_t unix_len
= sizeof(unix_addr
);
3229 if (!sock
->bound
&& !getsockname( get_unix_fd( fd
), &unix_addr
.addr
, &unix_len
))
3230 sock
->addr_len
= sockaddr_from_unix( &unix_addr
, &sock
->addr
.addr
, sizeof(sock
->addr
) );
3234 if (status
!= STATUS_SUCCESS
)
3236 /* send() calls only clear and reselect events if unsuccessful. */
3237 sock
->pending_events
&= ~AFD_POLL_WRITE
;
3238 sock
->reported_events
&= ~AFD_POLL_WRITE
;
3241 /* If we had a short write and the socket is nonblocking (and the client is
3242 * not trying to force the operation to be asynchronous), return success.
3243 * Windows actually refuses to send any data in this case, and returns
3244 * EWOULDBLOCK, but we have no way of doing that. */
3245 if (status
== STATUS_DEVICE_NOT_READY
&& req
->total
&& sock
->nonblocking
)
3246 status
= STATUS_SUCCESS
;
3248 /* send() returned EWOULDBLOCK or a short write, i.e. cannot send all data yet */
3249 if (status
== STATUS_DEVICE_NOT_READY
&& !sock
->nonblocking
)
3251 /* Set a timeout on the async if necessary.
3253 * We want to do this *only* if the client gave us STATUS_DEVICE_NOT_READY.
3254 * If the client gave us STATUS_PENDING, it expects the async to always
3255 * block (it was triggered by WSASend*() with a valid OVERLAPPED
3256 * structure) and for the timeout not to be respected. */
3257 if (is_fd_overlapped( fd
))
3258 timeout
= (timeout_t
)sock
->sndtimeo
* -10000;
3260 status
= STATUS_PENDING
;
3263 if (status
== STATUS_PENDING
&& sock
->wr_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
3265 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
3269 if (status
== STATUS_SUCCESS
)
3271 struct iosb
*iosb
= async_get_iosb( async
);
3272 iosb
->result
= req
->total
;
3273 release_object( iosb
);
3276 else if (status
== STATUS_PENDING
)
3280 set_error( status
);
3283 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
3285 if (status
== STATUS_PENDING
)
3286 queue_async( &sock
->write_q
, async
);
3288 /* always reselect; we changed reported_events above */
3289 sock_reselect( sock
);
3291 reply
->wait
= async_handoff( async
, success
, NULL
, 0 );
3292 reply
->options
= get_fd_options( fd
);
3293 release_object( async
);
3295 release_object( sock
);