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?
33 #ifdef HAVE_NETINET_IN_H
34 # include <netinet/in.h>
40 #include <sys/types.h>
41 #ifdef HAVE_SYS_SOCKET_H
42 # include <sys/socket.h>
44 #ifdef HAVE_SYS_IOCTL_H
45 #include <sys/ioctl.h>
47 #ifdef HAVE_SYS_FILIO_H
48 # include <sys/filio.h>
53 #ifdef HAVE_LINUX_RTNETLINK_H
54 # include <linux/rtnetlink.h>
57 #ifdef HAVE_NETIPX_IPX_H
58 # include <netipx/ipx.h>
59 #elif defined(HAVE_LINUX_IPX_H)
60 # ifdef HAVE_ASM_TYPES_H
61 # include <asm/types.h>
63 # ifdef HAVE_LINUX_TYPES_H
64 # include <linux/types.h>
66 # include <linux/ipx.h>
68 #if defined(SOL_IPX) || defined(SO_DEFAULT_HEADERS)
72 #ifdef HAVE_LINUX_IRDA_H
73 # ifdef HAVE_LINUX_TYPES_H
74 # include <linux/types.h>
76 # include <linux/irda.h>
81 #define WIN32_NO_STATUS
99 static struct list poll_list
= LIST_INIT( poll_list
);
106 struct timeout_user
*timeout
;
108 struct poll_socket_output
*output
;
121 struct sock
*sock
, *acceptsock
;
123 unsigned int recv_len
, local_len
;
131 unsigned int addr_len
, send_len
, send_cursor
;
134 enum connection_state
145 struct object obj
; /* object header */
146 struct fd
*fd
; /* socket file descriptor */
147 enum connection_state state
; /* connection state */
148 unsigned int mask
; /* event mask */
149 /* pending FD_* events which have not yet been reported to the application */
150 unsigned int pending_events
;
151 /* FD_* events which have already been reported and should not be selected
152 * for again until reset by a relevant call.
154 * For example, if FD_READ is set here and not in pending_events, it has
155 * already been reported and consumed, and we should not report it again,
156 * even if POLLIN is signaled, until it is reset by e.g recv().
158 * If an event has been signaled and not consumed yet, it will be set in
159 * both pending_events and reported_events (as we should only ever report
160 * any event once until it is reset.) */
161 unsigned int reported_events
;
162 unsigned int flags
; /* socket flags */
163 unsigned short proto
; /* socket protocol */
164 unsigned short type
; /* socket type */
165 unsigned short family
; /* socket family */
166 struct event
*event
; /* event object */
167 user_handle_t window
; /* window to send the message to */
168 unsigned int message
; /* message to send */
169 obj_handle_t wparam
; /* message wparam (socket handle) */
170 unsigned int errors
[FD_MAX_EVENTS
]; /* event errors */
171 timeout_t connect_time
;/* time the socket was connected */
172 struct sock
*deferred
; /* socket that waits for a deferred accept */
173 struct async_queue read_q
; /* queue for asynchronous reads */
174 struct async_queue write_q
; /* queue for asynchronous writes */
175 struct async_queue ifchange_q
; /* queue for interface change notifications */
176 struct async_queue accept_q
; /* queue for asynchronous accepts */
177 struct async_queue connect_q
; /* queue for asynchronous connects */
178 struct async_queue poll_q
; /* queue for asynchronous polls */
179 struct object
*ifchange_obj
; /* the interface change notification object */
180 struct list ifchange_entry
; /* entry in ifchange notification list */
181 struct list accept_list
; /* list of pending accept requests */
182 struct accept_req
*accept_recv_req
; /* pending accept-into request which will recv on this socket */
183 struct connect_req
*connect_req
; /* pending connection request */
184 unsigned int rd_shutdown
: 1; /* is the read end shut down? */
185 unsigned int wr_shutdown
: 1; /* is the write end shut down? */
186 unsigned int wr_shutdown_pending
: 1; /* is a write shutdown pending? */
187 unsigned int nonblocking
: 1; /* is the socket nonblocking? */
190 static void sock_dump( struct object
*obj
, int verbose
);
191 static struct fd
*sock_get_fd( struct object
*obj
);
192 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
193 static void sock_destroy( struct object
*obj
);
194 static struct object
*sock_get_ifchange( struct sock
*sock
);
195 static void sock_release_ifchange( struct sock
*sock
);
197 static int sock_get_poll_events( struct fd
*fd
);
198 static void sock_poll_event( struct fd
*fd
, int event
);
199 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
200 static int sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
201 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
);
202 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
204 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
);
205 static struct sock
*accept_socket( struct sock
*sock
);
206 static int sock_get_ntstatus( int err
);
207 static unsigned int sock_get_error( int err
);
209 static const struct object_ops sock_ops
=
211 sizeof(struct sock
), /* size */
212 &file_type
, /* type */
213 sock_dump
, /* dump */
214 add_queue
, /* add_queue */
215 remove_queue
, /* remove_queue */
216 default_fd_signaled
, /* signaled */
217 no_satisfied
, /* satisfied */
218 no_signal
, /* signal */
219 sock_get_fd
, /* get_fd */
220 default_map_access
, /* map_access */
221 default_get_sd
, /* get_sd */
222 default_set_sd
, /* set_sd */
223 no_get_full_name
, /* get_full_name */
224 no_lookup_name
, /* lookup_name */
225 no_link_name
, /* link_name */
226 NULL
, /* unlink_name */
227 no_open_file
, /* open_file */
228 no_kernel_obj_list
, /* get_kernel_obj_list */
229 sock_close_handle
, /* close_handle */
230 sock_destroy
/* destroy */
233 static const struct fd_ops sock_fd_ops
=
235 sock_get_poll_events
, /* get_poll_events */
236 sock_poll_event
, /* poll_event */
237 sock_get_fd_type
, /* get_fd_type */
238 no_fd_read
, /* read */
239 no_fd_write
, /* write */
240 no_fd_flush
, /* flush */
241 default_fd_get_file_info
, /* get_file_info */
242 no_fd_get_volume_info
, /* get_volume_info */
243 sock_ioctl
, /* ioctl */
244 sock_queue_async
, /* queue_async */
245 sock_reselect_async
/* reselect_async */
250 struct sockaddr addr
;
251 struct sockaddr_in in
;
252 struct sockaddr_in6 in6
;
254 struct sockaddr_ipx ipx
;
257 struct sockaddr_irda irda
;
261 static int sockaddr_from_unix( const union unix_sockaddr
*uaddr
, struct WS_sockaddr
*wsaddr
, socklen_t wsaddrlen
)
263 memset( wsaddr
, 0, wsaddrlen
);
265 switch (uaddr
->addr
.sa_family
)
269 struct WS_sockaddr_in win
= {0};
271 if (wsaddrlen
< sizeof(win
)) return -1;
272 win
.sin_family
= WS_AF_INET
;
273 win
.sin_port
= uaddr
->in
.sin_port
;
274 memcpy( &win
.sin_addr
, &uaddr
->in
.sin_addr
, sizeof(win
.sin_addr
) );
275 memcpy( wsaddr
, &win
, sizeof(win
) );
281 struct WS_sockaddr_in6 win
= {0};
283 if (wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
)) return -1;
284 win
.sin6_family
= WS_AF_INET6
;
285 win
.sin6_port
= uaddr
->in6
.sin6_port
;
286 win
.sin6_flowinfo
= uaddr
->in6
.sin6_flowinfo
;
287 memcpy( &win
.sin6_addr
, &uaddr
->in6
.sin6_addr
, sizeof(win
.sin6_addr
) );
288 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
289 win
.sin6_scope_id
= uaddr
->in6
.sin6_scope_id
;
291 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
))
293 memcpy( wsaddr
, &win
, sizeof(struct WS_sockaddr_in6
) );
294 return sizeof(struct WS_sockaddr_in6
);
296 memcpy( wsaddr
, &win
, sizeof(struct WS_sockaddr_in6_old
) );
297 return sizeof(struct WS_sockaddr_in6_old
);
303 struct WS_sockaddr_ipx win
= {0};
305 if (wsaddrlen
< sizeof(win
)) return -1;
306 win
.sa_family
= WS_AF_IPX
;
307 memcpy( win
.sa_netnum
, &uaddr
->ipx
.sipx_network
, sizeof(win
.sa_netnum
) );
308 memcpy( win
.sa_nodenum
, &uaddr
->ipx
.sipx_node
, sizeof(win
.sa_nodenum
) );
309 win
.sa_socket
= uaddr
->ipx
.sipx_port
;
310 memcpy( wsaddr
, &win
, sizeof(win
) );
320 if (wsaddrlen
< sizeof(win
)) return -1;
321 win
.irdaAddressFamily
= WS_AF_IRDA
;
322 memcpy( win
.irdaDeviceID
, &uaddr
->irda
.sir_addr
, sizeof(win
.irdaDeviceID
) );
323 if (uaddr
->irda
.sir_lsap_sel
!= LSAP_ANY
)
324 snprintf( win
.irdaServiceName
, sizeof(win
.irdaServiceName
), "LSAP-SEL%u", uaddr
->irda
.sir_lsap_sel
);
326 memcpy( win
.irdaServiceName
, uaddr
->irda
.sir_name
, sizeof(win
.irdaServiceName
) );
327 memcpy( wsaddr
, &win
, sizeof(win
) );
341 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
342 * we post messages if there are multiple events. Used to send
343 * messages. The problem is if there is both a FD_CONNECT event and,
344 * say, an FD_READ event available on the same socket, we want to
345 * notify the app of the connect event first. Otherwise it may
346 * discard the read event because it thinks it hasn't connected yet.
348 static const int event_bitorder
[FD_MAX_EVENTS
] =
356 6, 7, 8, 9 /* leftovers */
359 /* Flags that make sense only for SOCK_STREAM sockets */
360 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
363 SOCK_SHUTDOWN_ERROR
= -1,
364 SOCK_SHUTDOWN_EOF
= 0,
365 SOCK_SHUTDOWN_POLLHUP
= 1
368 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
370 static sock_shutdown_t
sock_check_pollhup(void)
372 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
377 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
378 if ( shutdown( fd
[0], 1 ) ) goto out
;
384 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
385 n
= poll( &pfd
, 1, 1 );
386 if ( n
!= 1 ) goto out
; /* error or timeout */
387 if ( pfd
.revents
& POLLHUP
)
388 ret
= SOCK_SHUTDOWN_POLLHUP
;
389 else if ( pfd
.revents
& POLLIN
&&
390 read( fd
[1], &dummy
, 1 ) == 0 )
391 ret
= SOCK_SHUTDOWN_EOF
;
401 sock_shutdown_type
= sock_check_pollhup();
403 switch ( sock_shutdown_type
)
405 case SOCK_SHUTDOWN_EOF
:
406 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
408 case SOCK_SHUTDOWN_POLLHUP
:
409 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
412 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
413 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
417 static int sock_reselect( struct sock
*sock
)
419 int ev
= sock_get_poll_events( sock
->fd
);
422 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
424 set_fd_events( sock
->fd
, ev
);
428 /* wake anybody waiting on the socket event or send the associated message */
429 static void sock_wake_up( struct sock
*sock
)
431 unsigned int events
= sock
->pending_events
& sock
->mask
;
436 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
438 set_event( sock
->event
);
442 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
443 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
445 int event
= event_bitorder
[i
];
446 if (events
& (1 << event
))
448 lparam_t lparam
= (1 << event
) | (sock
->errors
[event
] << 16);
449 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
452 sock
->pending_events
= 0;
453 sock_reselect( sock
);
457 static inline int sock_error( struct fd
*fd
)
459 unsigned int optval
= 0;
460 socklen_t optlen
= sizeof(optval
);
462 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
466 static void free_accept_req( void *private )
468 struct accept_req
*req
= private;
469 list_remove( &req
->entry
);
472 req
->acceptsock
->accept_recv_req
= NULL
;
473 release_object( req
->acceptsock
);
475 release_object( req
->async
);
476 release_object( req
->iosb
);
477 release_object( req
->sock
);
481 static void fill_accept_output( struct accept_req
*req
)
483 struct iosb
*iosb
= req
->iosb
;
484 union unix_sockaddr unix_addr
;
485 struct WS_sockaddr
*win_addr
;
486 unsigned int remote_len
;
492 if (!(out_data
= mem_alloc( iosb
->out_size
))) return;
494 fd
= get_unix_fd( req
->acceptsock
->fd
);
496 if (req
->recv_len
&& (size
= recv( fd
, out_data
, req
->recv_len
, 0 )) < 0)
498 if (!req
->accepted
&& errno
== EWOULDBLOCK
)
501 sock_reselect( req
->acceptsock
);
502 set_error( STATUS_PENDING
);
506 set_error( sock_get_ntstatus( errno
) );
513 if (req
->local_len
< sizeof(int))
515 set_error( STATUS_BUFFER_TOO_SMALL
);
520 unix_len
= sizeof(unix_addr
);
521 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ sizeof(int));
522 if (getsockname( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
523 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, req
->local_len
- sizeof(int) )) < 0)
525 set_error( sock_get_ntstatus( errno
) );
529 memcpy( out_data
+ req
->recv_len
, &win_len
, sizeof(int) );
532 unix_len
= sizeof(unix_addr
);
533 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ req
->local_len
+ sizeof(int));
534 remote_len
= iosb
->out_size
- req
->recv_len
- req
->local_len
;
535 if (getpeername( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
536 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, remote_len
- sizeof(int) )) < 0)
538 set_error( sock_get_ntstatus( errno
) );
542 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
544 iosb
->status
= STATUS_SUCCESS
;
546 iosb
->out_data
= out_data
;
547 set_error( STATUS_ALERTED
);
550 static void complete_async_accept( struct sock
*sock
, struct accept_req
*req
)
552 struct sock
*acceptsock
= req
->acceptsock
;
553 struct async
*async
= req
->async
;
555 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
559 if (!accept_into_socket( sock
, acceptsock
)) return;
560 fill_accept_output( req
);
564 struct iosb
*iosb
= req
->iosb
;
567 if (!(acceptsock
= accept_socket( sock
))) return;
568 handle
= alloc_handle_no_access_check( async_get_thread( async
)->process
, &acceptsock
->obj
,
569 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
570 acceptsock
->wparam
= handle
;
571 release_object( acceptsock
);
574 if (!(iosb
->out_data
= malloc( sizeof(handle
) ))) return;
576 iosb
->status
= STATUS_SUCCESS
;
577 iosb
->out_size
= sizeof(handle
);
578 memcpy( iosb
->out_data
, &handle
, sizeof(handle
) );
579 set_error( STATUS_ALERTED
);
583 static void complete_async_accept_recv( struct accept_req
*req
)
585 if (debug_level
) fprintf( stderr
, "completing accept recv request for socket %p\n", req
->acceptsock
);
587 assert( req
->recv_len
);
589 fill_accept_output( req
);
592 static void free_connect_req( void *private )
594 struct connect_req
*req
= private;
596 req
->sock
->connect_req
= NULL
;
597 release_object( req
->async
);
598 release_object( req
->iosb
);
599 release_object( req
->sock
);
603 static void complete_async_connect( struct sock
*sock
)
605 struct connect_req
*req
= sock
->connect_req
;
606 const char *in_buffer
;
611 if (debug_level
) fprintf( stderr
, "completing connect request for socket %p\n", sock
);
613 sock
->state
= SOCK_CONNECTED
;
617 set_error( STATUS_SUCCESS
);
622 in_buffer
= (const char *)iosb
->in_data
+ sizeof(struct afd_connect_params
) + req
->addr_len
;
623 len
= req
->send_len
- req
->send_cursor
;
625 ret
= send( get_unix_fd( sock
->fd
), in_buffer
+ req
->send_cursor
, len
, 0 );
626 if (ret
< 0 && errno
!= EWOULDBLOCK
)
627 set_error( sock_get_ntstatus( errno
) );
630 iosb
->result
= req
->send_len
;
631 iosb
->status
= STATUS_SUCCESS
;
632 set_error( STATUS_ALERTED
);
636 req
->send_cursor
+= ret
;
637 set_error( STATUS_PENDING
);
641 static void free_poll_req( void *private )
643 struct poll_req
*req
= private;
646 if (req
->timeout
) remove_timeout_user( req
->timeout
);
648 for (i
= 0; i
< req
->count
; ++i
)
649 release_object( req
->sockets
[i
].sock
);
650 release_object( req
->async
);
651 release_object( req
->iosb
);
652 list_remove( &req
->entry
);
656 static int is_oobinline( struct sock
*sock
)
659 socklen_t len
= sizeof(oobinline
);
660 return !getsockopt( get_unix_fd( sock
->fd
), SOL_SOCKET
, SO_OOBINLINE
, (char *)&oobinline
, &len
) && oobinline
;
663 static int get_poll_flags( struct sock
*sock
, int event
)
667 /* A connection-mode socket which has never been connected does not return
668 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
669 if (sock
->state
== SOCK_UNCONNECTED
)
670 event
&= ~(POLLOUT
| POLLHUP
);
674 if (sock
->state
== SOCK_LISTENING
)
675 flags
|= AFD_POLL_ACCEPT
;
677 flags
|= AFD_POLL_READ
;
680 flags
|= is_oobinline( sock
) ? AFD_POLL_READ
: AFD_POLL_OOB
;
682 flags
|= AFD_POLL_WRITE
;
683 if (sock
->state
== SOCK_CONNECTED
)
684 flags
|= AFD_POLL_CONNECT
;
686 flags
|= AFD_POLL_HUP
;
688 flags
|= AFD_POLL_CONNECT_ERR
;
693 static void complete_async_polls( struct sock
*sock
, int event
, int error
)
695 int flags
= get_poll_flags( sock
, event
);
696 struct poll_req
*req
, *next
;
698 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &poll_list
, struct poll_req
, entry
)
700 struct iosb
*iosb
= req
->iosb
;
703 if (iosb
->status
!= STATUS_PENDING
) continue;
705 for (i
= 0; i
< req
->count
; ++i
)
707 if (req
->sockets
[i
].sock
!= sock
) continue;
708 if (!(req
->sockets
[i
].flags
& flags
)) continue;
711 fprintf( stderr
, "completing poll for socket %p, wanted %#x got %#x\n",
712 sock
, req
->sockets
[i
].flags
, flags
);
714 req
->output
[i
].flags
= req
->sockets
[i
].flags
& flags
;
715 req
->output
[i
].status
= sock_get_ntstatus( error
);
717 iosb
->status
= STATUS_SUCCESS
;
718 iosb
->out_data
= req
->output
;
719 iosb
->out_size
= req
->count
* sizeof(*req
->output
);
720 async_terminate( req
->async
, STATUS_ALERTED
);
726 static void async_poll_timeout( void *private )
728 struct poll_req
*req
= private;
729 struct iosb
*iosb
= req
->iosb
;
733 if (iosb
->status
!= STATUS_PENDING
) return;
735 iosb
->status
= STATUS_TIMEOUT
;
736 iosb
->out_data
= req
->output
;
737 iosb
->out_size
= req
->count
* sizeof(*req
->output
);
738 async_terminate( req
->async
, STATUS_ALERTED
);
741 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
743 if (event
& (POLLIN
| POLLPRI
))
745 struct accept_req
*req
;
747 LIST_FOR_EACH_ENTRY( req
, &sock
->accept_list
, struct accept_req
, entry
)
749 if (req
->iosb
->status
== STATUS_PENDING
&& !req
->accepted
)
751 complete_async_accept( sock
, req
);
752 if (get_error() != STATUS_PENDING
)
753 async_terminate( req
->async
, get_error() );
758 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
760 complete_async_accept_recv( sock
->accept_recv_req
);
761 if (get_error() != STATUS_PENDING
)
762 async_terminate( sock
->accept_recv_req
->async
, get_error() );
766 if ((event
& POLLOUT
) && sock
->connect_req
&& sock
->connect_req
->iosb
->status
== STATUS_PENDING
)
768 complete_async_connect( sock
);
769 if (get_error() != STATUS_PENDING
)
770 async_terminate( sock
->connect_req
->async
, get_error() );
773 if (event
& (POLLIN
| POLLPRI
) && async_waiting( &sock
->read_q
))
775 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
776 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
777 event
&= ~(POLLIN
| POLLPRI
);
780 if (event
& POLLOUT
&& async_waiting( &sock
->write_q
))
782 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
783 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
787 if (event
& (POLLERR
| POLLHUP
))
789 int status
= sock_get_ntstatus( error
);
790 struct accept_req
*req
, *next
;
792 if (sock
->rd_shutdown
)
793 async_wake_up( &sock
->read_q
, status
);
794 if (sock
->wr_shutdown
)
795 async_wake_up( &sock
->write_q
, status
);
797 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
799 if (req
->iosb
->status
== STATUS_PENDING
)
800 async_terminate( req
->async
, status
);
803 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
804 async_terminate( sock
->accept_recv_req
->async
, status
);
806 if (sock
->connect_req
)
807 async_terminate( sock
->connect_req
->async
, status
);
813 static void post_socket_event( struct sock
*sock
, unsigned int event_bit
, unsigned int error
)
815 unsigned int event
= (1 << event_bit
);
817 if (!(sock
->reported_events
& event
))
819 sock
->pending_events
|= event
;
820 sock
->reported_events
|= event
;
821 sock
->errors
[event_bit
] = error
;
825 static void sock_dispatch_events( struct sock
*sock
, enum connection_state prevstate
, int event
, int error
)
829 case SOCK_UNCONNECTED
:
832 case SOCK_CONNECTING
:
833 if (event
& (POLLOUT
| POLLERR
| POLLHUP
))
834 post_socket_event( sock
, FD_CONNECT_BIT
, sock_get_error( error
) );
838 if (event
& (POLLIN
| POLLERR
| POLLHUP
))
839 post_socket_event( sock
, FD_ACCEPT_BIT
, sock_get_error( error
) );
843 case SOCK_CONNECTIONLESS
:
845 post_socket_event( sock
, FD_READ_BIT
, 0 );
848 post_socket_event( sock
, FD_WRITE_BIT
, 0 );
851 post_socket_event( sock
, FD_OOB_BIT
, 0 );
853 if (event
& (POLLERR
| POLLHUP
))
854 post_socket_event( sock
, FD_CLOSE_BIT
, sock_get_error( error
) );
858 sock_wake_up( sock
);
861 static void sock_poll_event( struct fd
*fd
, int event
)
863 struct sock
*sock
= get_fd_user( fd
);
865 enum connection_state prevstate
= sock
->state
;
868 assert( sock
->obj
.ops
== &sock_ops
);
870 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
872 /* we may change event later, remove from loop here */
873 if (event
& (POLLERR
|POLLHUP
)) set_fd_events( sock
->fd
, -1 );
877 case SOCK_UNCONNECTED
:
880 case SOCK_CONNECTING
:
881 if (event
& (POLLERR
|POLLHUP
))
883 sock
->state
= SOCK_UNCONNECTED
;
885 error
= sock_error( fd
);
887 else if (event
& POLLOUT
)
889 sock
->state
= SOCK_CONNECTED
;
890 sock
->connect_time
= current_time
;
895 if (event
& (POLLERR
|POLLHUP
))
896 error
= sock_error( fd
);
900 case SOCK_CONNECTIONLESS
:
901 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
906 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
907 * has been closed, so we need to check for it explicitly here */
908 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
917 /* EAGAIN can happen if an async recv() falls between the server's poll()
918 call and the invocation of this routine */
919 if ( errno
!= EAGAIN
)
924 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
929 if ((hangup_seen
|| event
& (POLLHUP
| POLLERR
)) && (!sock
->rd_shutdown
|| !sock
->wr_shutdown
))
931 error
= error
? error
: sock_error( fd
);
932 if ( (event
& POLLERR
) || ( sock_shutdown_type
== SOCK_SHUTDOWN_EOF
&& (event
& POLLHUP
) ))
933 sock
->wr_shutdown
= 1;
934 sock
->rd_shutdown
= 1;
937 fprintf(stderr
, "socket %p aborted by error %d, event: %x\n", sock
, error
, event
);
945 complete_async_polls( sock
, event
, error
);
947 event
= sock_dispatch_asyncs( sock
, event
, error
);
948 sock_dispatch_events( sock
, prevstate
, event
, error
);
950 sock_reselect( sock
);
953 static void sock_dump( struct object
*obj
, int verbose
)
955 struct sock
*sock
= (struct sock
*)obj
;
956 assert( obj
->ops
== &sock_ops
);
957 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
958 sock
->fd
, sock
->state
,
959 sock
->mask
, sock
->pending_events
, sock
->reported_events
);
962 static int poll_flags_from_afd( struct sock
*sock
, int flags
)
966 /* A connection-mode socket which has never been connected does
967 * not return write or hangup events, but Linux returns
968 * POLLOUT | POLLHUP. */
969 if (sock
->state
== SOCK_UNCONNECTED
)
972 if (flags
& (AFD_POLL_READ
| AFD_POLL_ACCEPT
))
974 if ((flags
& AFD_POLL_HUP
) && sock
->type
== WS_SOCK_STREAM
)
976 if (flags
& AFD_POLL_OOB
)
977 ev
|= is_oobinline( sock
) ? POLLIN
: POLLPRI
;
978 if (flags
& AFD_POLL_WRITE
)
984 static int sock_get_poll_events( struct fd
*fd
)
986 struct sock
*sock
= get_fd_user( fd
);
987 unsigned int mask
= sock
->mask
& ~sock
->reported_events
;
988 struct poll_req
*req
;
991 assert( sock
->obj
.ops
== &sock_ops
);
993 if (!sock
->type
) /* not initialized yet */
998 case SOCK_UNCONNECTED
:
999 /* A connection-mode Windows socket which has never been connected does
1000 * not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
1001 * need to return -1 here, to prevent the socket from being polled on at
1005 case SOCK_CONNECTING
:
1008 case SOCK_LISTENING
:
1009 if (!list_empty( &sock
->accept_list
) || (mask
& FD_ACCEPT
))
1013 case SOCK_CONNECTED
:
1014 case SOCK_CONNECTIONLESS
:
1015 if (sock
->accept_recv_req
)
1019 else if (async_queued( &sock
->read_q
))
1021 if (async_waiting( &sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
1023 else if (!sock
->rd_shutdown
&& (mask
& FD_READ
))
1024 ev
|= POLLIN
| POLLPRI
;
1025 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
1026 else if (sock
->state
== SOCK_CONNECTED
&& (mask
& FD_CLOSE
) && !(sock
->reported_events
& FD_READ
))
1029 if (async_queued( &sock
->write_q
))
1031 if (async_waiting( &sock
->write_q
)) ev
|= POLLOUT
;
1033 else if (!sock
->wr_shutdown
&& (mask
& FD_WRITE
))
1041 LIST_FOR_EACH_ENTRY( req
, &poll_list
, struct poll_req
, entry
)
1045 for (i
= 0; i
< req
->count
; ++i
)
1047 if (req
->sockets
[i
].sock
!= sock
) continue;
1049 ev
|= poll_flags_from_afd( sock
, req
->sockets
[i
].flags
);
1056 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
1058 return FD_TYPE_SOCKET
;
1061 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
1063 struct sock
*sock
= get_fd_user( fd
);
1064 struct async_queue
*queue
;
1066 assert( sock
->obj
.ops
== &sock_ops
);
1070 case ASYNC_TYPE_READ
:
1071 if (sock
->rd_shutdown
)
1073 set_error( STATUS_PIPE_DISCONNECTED
);
1076 queue
= &sock
->read_q
;
1079 case ASYNC_TYPE_WRITE
:
1080 if (sock
->wr_shutdown
)
1082 set_error( STATUS_PIPE_DISCONNECTED
);
1085 queue
= &sock
->write_q
;
1089 set_error( STATUS_INVALID_PARAMETER
);
1093 if (sock
->state
!= SOCK_CONNECTED
)
1095 set_error( STATUS_PIPE_DISCONNECTED
);
1099 queue_async( queue
, async
);
1100 sock_reselect( sock
);
1102 set_error( STATUS_PENDING
);
1105 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
1107 struct sock
*sock
= get_fd_user( fd
);
1109 if (sock
->wr_shutdown_pending
&& list_empty( &sock
->write_q
.queue
))
1110 shutdown( get_unix_fd( sock
->fd
), SHUT_WR
);
1112 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1113 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1115 if (queue
!= &sock
->ifchange_q
&& sock
->type
)
1116 sock_reselect( sock
);
1119 static struct fd
*sock_get_fd( struct object
*obj
)
1121 struct sock
*sock
= (struct sock
*)obj
;
1122 return (struct fd
*)grab_object( sock
->fd
);
1125 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
1127 struct sock
*sock
= (struct sock
*)obj
;
1129 if (sock
->obj
.handle_count
== 1) /* last handle */
1131 struct accept_req
*accept_req
, *accept_next
;
1132 struct poll_req
*poll_req
, *poll_next
;
1134 if (sock
->accept_recv_req
)
1135 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
1137 LIST_FOR_EACH_ENTRY_SAFE( accept_req
, accept_next
, &sock
->accept_list
, struct accept_req
, entry
)
1138 async_terminate( accept_req
->async
, STATUS_CANCELLED
);
1140 if (sock
->connect_req
)
1141 async_terminate( sock
->connect_req
->async
, STATUS_CANCELLED
);
1143 LIST_FOR_EACH_ENTRY_SAFE( poll_req
, poll_next
, &poll_list
, struct poll_req
, entry
)
1145 struct iosb
*iosb
= poll_req
->iosb
;
1148 if (iosb
->status
!= STATUS_PENDING
) continue;
1150 for (i
= 0; i
< poll_req
->count
; ++i
)
1152 if (poll_req
->sockets
[i
].sock
== sock
)
1154 iosb
->status
= STATUS_SUCCESS
;
1155 poll_req
->output
[i
].flags
= AFD_POLL_CLOSE
;
1156 poll_req
->output
[i
].status
= 0;
1160 if (iosb
->status
!= STATUS_PENDING
)
1162 iosb
->out_data
= poll_req
->output
;
1163 iosb
->out_size
= poll_req
->count
* sizeof(*poll_req
->output
);
1164 async_terminate( poll_req
->async
, STATUS_ALERTED
);
1172 static void sock_destroy( struct object
*obj
)
1174 struct sock
*sock
= (struct sock
*)obj
;
1176 assert( obj
->ops
== &sock_ops
);
1178 /* FIXME: special socket shutdown stuff? */
1180 if ( sock
->deferred
)
1181 release_object( sock
->deferred
);
1183 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
1184 sock_release_ifchange( sock
);
1185 free_async_queue( &sock
->read_q
);
1186 free_async_queue( &sock
->write_q
);
1187 free_async_queue( &sock
->ifchange_q
);
1188 free_async_queue( &sock
->accept_q
);
1189 free_async_queue( &sock
->connect_q
);
1190 free_async_queue( &sock
->poll_q
);
1191 if (sock
->event
) release_object( sock
->event
);
1194 /* shut the socket down to force pending poll() calls in the client to return */
1195 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
1196 release_object( sock
->fd
);
1200 static struct sock
*create_socket(void)
1204 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
1206 sock
->state
= SOCK_UNCONNECTED
;
1208 sock
->pending_events
= 0;
1209 sock
->reported_events
= 0;
1218 sock
->connect_time
= 0;
1219 sock
->deferred
= NULL
;
1220 sock
->ifchange_obj
= NULL
;
1221 sock
->accept_recv_req
= NULL
;
1222 sock
->connect_req
= NULL
;
1223 sock
->rd_shutdown
= 0;
1224 sock
->wr_shutdown
= 0;
1225 sock
->wr_shutdown_pending
= 0;
1226 sock
->nonblocking
= 0;
1227 init_async_queue( &sock
->read_q
);
1228 init_async_queue( &sock
->write_q
);
1229 init_async_queue( &sock
->ifchange_q
);
1230 init_async_queue( &sock
->accept_q
);
1231 init_async_queue( &sock
->connect_q
);
1232 init_async_queue( &sock
->poll_q
);
1233 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
1234 list_init( &sock
->accept_list
);
1238 static int get_unix_family( int family
)
1242 case WS_AF_INET
: return AF_INET
;
1243 case WS_AF_INET6
: return AF_INET6
;
1245 case WS_AF_IPX
: return AF_IPX
;
1248 case WS_AF_IRDA
: return AF_IRDA
;
1250 case WS_AF_UNSPEC
: return AF_UNSPEC
;
1255 static int get_unix_type( int type
)
1259 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
1260 case WS_SOCK_RAW
: return SOCK_RAW
;
1261 case WS_SOCK_STREAM
: return SOCK_STREAM
;
1266 static int get_unix_protocol( int protocol
)
1268 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1273 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
1274 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1275 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1276 case WS_IPPROTO_IPV4
: return IPPROTO_IPIP
;
1277 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1278 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1279 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1280 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1285 static void set_dont_fragment( int fd
, int level
, int value
)
1289 if (level
== IPPROTO_IP
)
1292 optname
= IP_DONTFRAG
;
1293 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1294 optname
= IP_MTU_DISCOVER
;
1295 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1302 #ifdef IPV6_DONTFRAG
1303 optname
= IPV6_DONTFRAG
;
1304 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1305 optname
= IPV6_MTU_DISCOVER
;
1306 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1312 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1315 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
, unsigned int flags
)
1317 unsigned int options
= 0;
1318 int sockfd
, unix_type
, unix_family
, unix_protocol
;
1320 unix_family
= get_unix_family( family
);
1321 unix_type
= get_unix_type( type
);
1322 unix_protocol
= get_unix_protocol( protocol
);
1324 if (unix_protocol
< 0)
1326 if (type
&& unix_type
< 0)
1327 set_win32_error( WSAESOCKTNOSUPPORT
);
1329 set_win32_error( WSAEPROTONOSUPPORT
);
1332 if (unix_family
< 0)
1334 if (family
>= 0 && unix_type
< 0)
1335 set_win32_error( WSAESOCKTNOSUPPORT
);
1337 set_win32_error( WSAEAFNOSUPPORT
);
1341 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1344 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1345 else set_win32_error( sock_get_error( errno
));
1348 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1350 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1353 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1356 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1359 /* Should we retrieve val using a getsockopt call and then
1360 * set the modified one? */
1361 val
.ipx_pt
= ipx_type
;
1362 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1367 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1369 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1370 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1371 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1372 else if (unix_type
== SOCK_STREAM
)
1373 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1377 if (unix_family
== AF_INET6
)
1379 static const int enable
= 1;
1380 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1384 sock
->state
= (type
== WS_SOCK_STREAM
? SOCK_UNCONNECTED
: SOCK_CONNECTIONLESS
);
1385 sock
->flags
= flags
;
1386 sock
->proto
= protocol
;
1388 sock
->family
= family
;
1392 options
= get_fd_options( sock
->fd
);
1393 release_object( sock
->fd
);
1396 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1401 /* We can't immediately allow caching for a connection-mode socket, since it
1402 * might be accepted into (changing the underlying fd object.) */
1403 if (sock
->type
!= WS_SOCK_STREAM
) allow_fd_caching( sock
->fd
);
1408 /* accepts a socket and inits it */
1409 static int accept_new_fd( struct sock
*sock
)
1412 /* Try to accept(2). We can't be safe that this an already connected socket
1413 * or that accept() is allowed on it. In those cases we will get -1/errno
1416 struct sockaddr saddr
;
1417 socklen_t slen
= sizeof(saddr
);
1418 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1420 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1422 set_error( sock_get_ntstatus( errno
));
1426 /* accept a socket (creates a new fd) */
1427 static struct sock
*accept_socket( struct sock
*sock
)
1429 struct sock
*acceptsock
;
1432 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1434 if ( sock
->deferred
)
1436 acceptsock
= sock
->deferred
;
1437 sock
->deferred
= NULL
;
1441 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1442 if (!(acceptsock
= create_socket()))
1448 /* newly created socket gets the same properties of the listening socket */
1449 acceptsock
->state
= SOCK_CONNECTED
;
1450 acceptsock
->nonblocking
= sock
->nonblocking
;
1451 acceptsock
->mask
= sock
->mask
;
1452 acceptsock
->proto
= sock
->proto
;
1453 acceptsock
->type
= sock
->type
;
1454 acceptsock
->family
= sock
->family
;
1455 acceptsock
->window
= sock
->window
;
1456 acceptsock
->message
= sock
->message
;
1457 acceptsock
->connect_time
= current_time
;
1458 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
1459 acceptsock
->flags
= sock
->flags
;
1460 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1461 get_fd_options( sock
->fd
) )))
1463 release_object( acceptsock
);
1468 sock
->pending_events
&= ~FD_ACCEPT
;
1469 sock
->reported_events
&= ~FD_ACCEPT
;
1470 sock_reselect( sock
);
1474 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
1479 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
1481 if ( sock
->deferred
)
1483 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
1484 get_fd_options( acceptsock
->fd
) );
1488 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
1490 release_object( sock
->deferred
);
1491 sock
->deferred
= NULL
;
1495 if ((acceptfd
= accept_new_fd( sock
)) == -1)
1498 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1499 get_fd_options( acceptsock
->fd
) )))
1503 acceptsock
->state
= SOCK_CONNECTED
;
1504 acceptsock
->pending_events
= 0;
1505 acceptsock
->reported_events
= 0;
1506 acceptsock
->proto
= sock
->proto
;
1507 acceptsock
->type
= sock
->type
;
1508 acceptsock
->family
= sock
->family
;
1509 acceptsock
->wparam
= 0;
1510 acceptsock
->deferred
= NULL
;
1511 acceptsock
->connect_time
= current_time
;
1512 fd_copy_completion( acceptsock
->fd
, newfd
);
1513 release_object( acceptsock
->fd
);
1514 acceptsock
->fd
= newfd
;
1517 sock
->pending_events
&= ~FD_ACCEPT
;
1518 sock
->reported_events
&= ~FD_ACCEPT
;
1519 sock_reselect( sock
);
1524 /* return an errno value mapped to a WSA error */
1525 static unsigned int sock_get_error( int err
)
1529 case EINTR
: return WSAEINTR
;
1530 case EBADF
: return WSAEBADF
;
1532 case EACCES
: return WSAEACCES
;
1533 case EFAULT
: return WSAEFAULT
;
1534 case EINVAL
: return WSAEINVAL
;
1535 case EMFILE
: return WSAEMFILE
;
1537 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
1538 case EALREADY
: return WSAEALREADY
;
1539 case ENOTSOCK
: return WSAENOTSOCK
;
1540 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
1541 case EMSGSIZE
: return WSAEMSGSIZE
;
1542 case EPROTOTYPE
: return WSAEPROTOTYPE
;
1543 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
1544 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
1545 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
1546 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
1547 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
1548 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
1549 case EADDRINUSE
: return WSAEADDRINUSE
;
1550 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
1551 case ENETDOWN
: return WSAENETDOWN
;
1552 case ENETUNREACH
: return WSAENETUNREACH
;
1553 case ENETRESET
: return WSAENETRESET
;
1554 case ECONNABORTED
: return WSAECONNABORTED
;
1556 case ECONNRESET
: return WSAECONNRESET
;
1557 case ENOBUFS
: return WSAENOBUFS
;
1558 case EISCONN
: return WSAEISCONN
;
1559 case ENOTCONN
: return WSAENOTCONN
;
1560 case ESHUTDOWN
: return WSAESHUTDOWN
;
1561 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
1562 case ETIMEDOUT
: return WSAETIMEDOUT
;
1563 case ECONNREFUSED
: return WSAECONNREFUSED
;
1564 case ELOOP
: return WSAELOOP
;
1565 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
1566 case EHOSTDOWN
: return WSAEHOSTDOWN
;
1567 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
1568 case ENOTEMPTY
: return WSAENOTEMPTY
;
1570 case EPROCLIM
: return WSAEPROCLIM
;
1573 case EUSERS
: return WSAEUSERS
;
1576 case EDQUOT
: return WSAEDQUOT
;
1579 case ESTALE
: return WSAESTALE
;
1582 case EREMOTE
: return WSAEREMOTE
;
1588 perror("wineserver: sock_get_error() can't map error");
1593 static int sock_get_ntstatus( int err
)
1597 case EBADF
: return STATUS_INVALID_HANDLE
;
1598 case EBUSY
: return STATUS_DEVICE_BUSY
;
1600 case EACCES
: return STATUS_ACCESS_DENIED
;
1601 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
1602 case EINVAL
: return STATUS_INVALID_PARAMETER
;
1604 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
1606 case EWOULDBLOCK
: return STATUS_DEVICE_NOT_READY
;
1607 case EALREADY
: return STATUS_NETWORK_BUSY
;
1608 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
1609 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
1610 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
1611 case EPROTONOSUPPORT
:
1612 case ESOCKTNOSUPPORT
:
1615 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
1616 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
1617 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
1618 case EADDRINUSE
: return STATUS_SHARING_VIOLATION
;
1619 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
1620 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
1621 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
1622 case ENOTCONN
: return STATUS_INVALID_CONNECTION
;
1623 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
1624 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
1625 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
1626 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
1628 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
1629 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
1630 case EISCONN
: return STATUS_CONNECTION_ACTIVE
;
1632 case 0: return STATUS_SUCCESS
;
1635 perror("wineserver: sock_get_ntstatus() can't map error");
1636 return STATUS_UNSUCCESSFUL
;
1640 static struct accept_req
*alloc_accept_req( struct sock
*sock
, struct sock
*acceptsock
, struct async
*async
,
1641 const struct afd_accept_into_params
*params
)
1643 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
1647 req
->async
= (struct async
*)grab_object( async
);
1648 req
->iosb
= async_get_iosb( async
);
1649 req
->sock
= (struct sock
*)grab_object( sock
);
1650 req
->acceptsock
= acceptsock
;
1651 if (acceptsock
) grab_object( acceptsock
);
1657 req
->recv_len
= params
->recv_len
;
1658 req
->local_len
= params
->local_len
;
1664 static int sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
1666 struct sock
*sock
= get_fd_user( fd
);
1669 assert( sock
->obj
.ops
== &sock_ops
);
1671 if (code
!= IOCTL_AFD_WINE_CREATE
&& (unix_fd
= get_unix_fd( fd
)) < 0) return 0;
1675 case IOCTL_AFD_WINE_CREATE
:
1677 const struct afd_create_params
*params
= get_req_data();
1679 if (get_req_data_size() != sizeof(*params
))
1681 set_error( STATUS_INVALID_PARAMETER
);
1684 init_socket( sock
, params
->family
, params
->type
, params
->protocol
, params
->flags
);
1688 case IOCTL_AFD_WINE_ACCEPT
:
1690 struct sock
*acceptsock
;
1691 obj_handle_t handle
;
1693 if (get_reply_max_size() != sizeof(handle
))
1695 set_error( STATUS_BUFFER_TOO_SMALL
);
1699 if (!(acceptsock
= accept_socket( sock
)))
1701 struct accept_req
*req
;
1703 if (sock
->nonblocking
) return 0;
1704 if (get_error() != STATUS_DEVICE_NOT_READY
) return 0;
1706 if (!(req
= alloc_accept_req( sock
, NULL
, async
, NULL
))) return 0;
1707 list_add_tail( &sock
->accept_list
, &req
->entry
);
1709 async_set_completion_callback( async
, free_accept_req
, req
);
1710 queue_async( &sock
->accept_q
, async
);
1711 sock_reselect( sock
);
1712 set_error( STATUS_PENDING
);
1715 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
1716 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
1717 acceptsock
->wparam
= handle
;
1718 release_object( acceptsock
);
1719 set_reply_data( &handle
, sizeof(handle
) );
1723 case IOCTL_AFD_WINE_ACCEPT_INTO
:
1725 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
1726 const struct afd_accept_into_params
*params
= get_req_data();
1727 struct sock
*acceptsock
;
1728 unsigned int remote_len
;
1729 struct accept_req
*req
;
1731 if (get_req_data_size() != sizeof(*params
) ||
1732 get_reply_max_size() < params
->recv_len
||
1733 get_reply_max_size() - params
->recv_len
< params
->local_len
)
1735 set_error( STATUS_BUFFER_TOO_SMALL
);
1739 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
1740 if (remote_len
< sizeof(int))
1742 set_error( STATUS_INVALID_PARAMETER
);
1746 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
1749 if (acceptsock
->accept_recv_req
)
1751 release_object( acceptsock
);
1752 set_error( STATUS_INVALID_PARAMETER
);
1756 if (!(req
= alloc_accept_req( sock
, acceptsock
, async
, params
)))
1758 release_object( acceptsock
);
1761 list_add_tail( &sock
->accept_list
, &req
->entry
);
1762 acceptsock
->accept_recv_req
= req
;
1763 release_object( acceptsock
);
1765 acceptsock
->wparam
= params
->accept_handle
;
1766 async_set_completion_callback( async
, free_accept_req
, req
);
1767 queue_async( &sock
->accept_q
, async
);
1768 sock_reselect( sock
);
1769 set_error( STATUS_PENDING
);
1773 case IOCTL_AFD_LISTEN
:
1775 const struct afd_listen_params
*params
= get_req_data();
1777 if (get_req_data_size() < sizeof(*params
))
1779 set_error( STATUS_INVALID_PARAMETER
);
1783 if (listen( unix_fd
, params
->backlog
) < 0)
1785 set_error( sock_get_ntstatus( errno
) );
1789 sock
->state
= SOCK_LISTENING
;
1791 /* a listening socket can no longer be accepted into */
1792 allow_fd_caching( sock
->fd
);
1794 /* we may already be selecting for FD_ACCEPT */
1795 sock_reselect( sock
);
1799 case IOCTL_AFD_WINE_CONNECT
:
1801 const struct afd_connect_params
*params
= get_req_data();
1802 const struct sockaddr
*addr
;
1803 struct connect_req
*req
;
1806 if (get_req_data_size() < sizeof(*params
) ||
1807 get_req_data_size() - sizeof(*params
) < params
->addr_len
)
1809 set_error( STATUS_BUFFER_TOO_SMALL
);
1812 send_len
= get_req_data_size() - sizeof(*params
) - params
->addr_len
;
1813 addr
= (const struct sockaddr
*)(params
+ 1);
1815 if (sock
->accept_recv_req
)
1817 set_error( STATUS_INVALID_PARAMETER
);
1821 if (sock
->connect_req
)
1823 set_error( STATUS_INVALID_PARAMETER
);
1827 if (sock
->state
== SOCK_CONNECTING
)
1829 /* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
1830 * but there's no status code that maps to WSAEALREADY... */
1831 set_error( params
->synchronous
? STATUS_ADDRESS_ALREADY_ASSOCIATED
: STATUS_INVALID_PARAMETER
);
1835 ret
= connect( unix_fd
, addr
, params
->addr_len
);
1836 if (ret
< 0 && errno
!= EINPROGRESS
)
1838 set_error( sock_get_ntstatus( errno
) );
1842 /* a connected or connecting socket can no longer be accepted into */
1843 allow_fd_caching( sock
->fd
);
1847 sock
->state
= SOCK_CONNECTED
;
1849 if (!send_len
) return 1;
1852 if (!(req
= mem_alloc( sizeof(*req
) )))
1855 sock
->state
= SOCK_CONNECTING
;
1857 if (params
->synchronous
&& sock
->nonblocking
)
1859 sock_reselect( sock
);
1860 set_error( STATUS_DEVICE_NOT_READY
);
1864 req
->async
= (struct async
*)grab_object( async
);
1865 req
->iosb
= async_get_iosb( async
);
1866 req
->sock
= (struct sock
*)grab_object( sock
);
1867 req
->addr_len
= params
->addr_len
;
1868 req
->send_len
= send_len
;
1869 req
->send_cursor
= 0;
1871 async_set_completion_callback( async
, free_connect_req
, req
);
1872 sock
->connect_req
= req
;
1873 queue_async( &sock
->connect_q
, async
);
1874 sock_reselect( sock
);
1875 set_error( STATUS_PENDING
);
1879 case IOCTL_AFD_WINE_SHUTDOWN
:
1883 if (get_req_data_size() < sizeof(int))
1885 set_error( STATUS_BUFFER_TOO_SMALL
);
1888 how
= *(int *)get_req_data();
1892 set_error( STATUS_INVALID_PARAMETER
);
1896 if (sock
->state
!= SOCK_CONNECTED
&& sock
->state
!= SOCK_CONNECTIONLESS
)
1898 set_error( STATUS_INVALID_CONNECTION
);
1904 sock
->rd_shutdown
= 1;
1906 if (how
!= SD_RECEIVE
)
1908 sock
->wr_shutdown
= 1;
1909 if (list_empty( &sock
->write_q
.queue
))
1910 shutdown( unix_fd
, SHUT_WR
);
1912 sock
->wr_shutdown_pending
= 1;
1917 if (sock
->event
) release_object( sock
->event
);
1921 sock
->nonblocking
= 1;
1924 sock_reselect( sock
);
1928 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE
:
1932 if (get_req_data_size() < sizeof(int))
1934 set_error( STATUS_BUFFER_TOO_SMALL
);
1937 force_async
= *(int *)get_req_data();
1939 if (sock
->nonblocking
&& !force_async
)
1941 set_error( STATUS_DEVICE_NOT_READY
);
1944 if (!sock_get_ifchange( sock
)) return 0;
1945 queue_async( &sock
->ifchange_q
, async
);
1946 set_error( STATUS_PENDING
);
1950 case IOCTL_AFD_WINE_FIONBIO
:
1951 if (get_req_data_size() < sizeof(int))
1953 set_error( STATUS_BUFFER_TOO_SMALL
);
1956 if (*(int *)get_req_data())
1958 sock
->nonblocking
= 1;
1964 set_error( STATUS_INVALID_PARAMETER
);
1967 sock
->nonblocking
= 0;
1972 set_error( STATUS_NOT_SUPPORTED
);
1977 static int poll_socket( struct sock
*poll_sock
, struct async
*async
, timeout_t timeout
,
1978 unsigned int count
, const struct poll_socket_input
*input
)
1980 struct poll_socket_output
*output
;
1981 struct poll_req
*req
;
1984 if (!(output
= mem_alloc( count
* sizeof(*output
) )))
1986 memset( output
, 0, count
* sizeof(*output
) );
1988 if (!(req
= mem_alloc( offsetof( struct poll_req
, sockets
[count
] ) )))
1994 req
->timeout
= NULL
;
1995 if (timeout
&& timeout
!= TIMEOUT_INFINITE
&&
1996 !(req
->timeout
= add_timeout_user( timeout
, async_poll_timeout
, req
)))
2003 for (i
= 0; i
< count
; ++i
)
2005 req
->sockets
[i
].sock
= (struct sock
*)get_handle_obj( current
->process
, input
[i
].socket
, 0, &sock_ops
);
2006 if (!req
->sockets
[i
].sock
)
2008 for (j
= 0; j
< i
; ++j
) release_object( req
->sockets
[i
].sock
);
2009 if (req
->timeout
) remove_timeout_user( req
->timeout
);
2014 req
->sockets
[i
].flags
= input
[i
].flags
;
2018 req
->async
= (struct async
*)grab_object( async
);
2019 req
->iosb
= async_get_iosb( async
);
2020 req
->output
= output
;
2022 list_add_tail( &poll_list
, &req
->entry
);
2023 async_set_completion_callback( async
, free_poll_req
, req
);
2024 queue_async( &poll_sock
->poll_q
, async
);
2026 if (!timeout
) req
->iosb
->status
= STATUS_SUCCESS
;
2028 for (i
= 0; i
< count
; ++i
)
2030 struct sock
*sock
= req
->sockets
[i
].sock
;
2031 struct pollfd pollfd
;
2034 pollfd
.fd
= get_unix_fd( sock
->fd
);
2035 pollfd
.events
= poll_flags_from_afd( sock
, req
->sockets
[i
].flags
);
2036 if (pollfd
.events
< 0 || poll( &pollfd
, 1, 0 ) < 0) continue;
2038 if ((req
->sockets
[i
].flags
& AFD_POLL_HUP
) && (pollfd
.revents
& POLLIN
) &&
2039 sock
->type
== WS_SOCK_STREAM
)
2043 if (!recv( get_unix_fd( sock
->fd
), &dummy
, 1, MSG_PEEK
))
2045 pollfd
.revents
&= ~POLLIN
;
2046 pollfd
.revents
|= POLLHUP
;
2050 flags
= get_poll_flags( sock
, pollfd
.revents
) & req
->sockets
[i
].flags
;
2053 req
->iosb
->status
= STATUS_SUCCESS
;
2054 output
[i
].flags
= flags
;
2055 output
[i
].status
= sock_get_ntstatus( sock_error( sock
->fd
) );
2059 if (req
->iosb
->status
!= STATUS_PENDING
)
2061 req
->iosb
->out_data
= output
;
2062 req
->iosb
->out_size
= count
* sizeof(*output
);
2063 async_terminate( req
->async
, STATUS_ALERTED
);
2066 for (i
= 0; i
< req
->count
; ++i
)
2067 sock_reselect( req
->sockets
[i
].sock
);
2068 set_error( STATUS_PENDING
);
2072 #ifdef HAVE_LINUX_RTNETLINK_H
2074 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
2075 static struct object
*ifchange_object
;
2077 static void ifchange_dump( struct object
*obj
, int verbose
);
2078 static struct fd
*ifchange_get_fd( struct object
*obj
);
2079 static void ifchange_destroy( struct object
*obj
);
2081 static int ifchange_get_poll_events( struct fd
*fd
);
2082 static void ifchange_poll_event( struct fd
*fd
, int event
);
2086 struct object obj
; /* object header */
2087 struct fd
*fd
; /* interface change file descriptor */
2088 struct list sockets
; /* list of sockets to send interface change notifications */
2091 static const struct object_ops ifchange_ops
=
2093 sizeof(struct ifchange
), /* size */
2094 &no_type
, /* type */
2095 ifchange_dump
, /* dump */
2096 no_add_queue
, /* add_queue */
2097 NULL
, /* remove_queue */
2098 NULL
, /* signaled */
2099 no_satisfied
, /* satisfied */
2100 no_signal
, /* signal */
2101 ifchange_get_fd
, /* get_fd */
2102 default_map_access
, /* map_access */
2103 default_get_sd
, /* get_sd */
2104 default_set_sd
, /* set_sd */
2105 no_get_full_name
, /* get_full_name */
2106 no_lookup_name
, /* lookup_name */
2107 no_link_name
, /* link_name */
2108 NULL
, /* unlink_name */
2109 no_open_file
, /* open_file */
2110 no_kernel_obj_list
, /* get_kernel_obj_list */
2111 no_close_handle
, /* close_handle */
2112 ifchange_destroy
/* destroy */
2115 static const struct fd_ops ifchange_fd_ops
=
2117 ifchange_get_poll_events
, /* get_poll_events */
2118 ifchange_poll_event
, /* poll_event */
2119 NULL
, /* get_fd_type */
2120 no_fd_read
, /* read */
2121 no_fd_write
, /* write */
2122 no_fd_flush
, /* flush */
2123 no_fd_get_file_info
, /* get_file_info */
2124 no_fd_get_volume_info
, /* get_volume_info */
2125 no_fd_ioctl
, /* ioctl */
2126 NULL
, /* queue_async */
2127 NULL
/* reselect_async */
2130 static void ifchange_dump( struct object
*obj
, int verbose
)
2132 assert( obj
->ops
== &ifchange_ops
);
2133 fprintf( stderr
, "Interface change\n" );
2136 static struct fd
*ifchange_get_fd( struct object
*obj
)
2138 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
2139 return (struct fd
*)grab_object( ifchange
->fd
);
2142 static void ifchange_destroy( struct object
*obj
)
2144 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
2145 assert( obj
->ops
== &ifchange_ops
);
2147 release_object( ifchange
->fd
);
2149 /* reset the global ifchange object so that it will be recreated if it is needed again */
2150 assert( obj
== ifchange_object
);
2151 ifchange_object
= NULL
;
2154 static int ifchange_get_poll_events( struct fd
*fd
)
2159 /* wake up all the sockets waiting for a change notification event */
2160 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
2162 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
2163 struct list
*ptr
, *next
;
2164 assert( obj
->ops
== &ifchange_ops
);
2165 assert( obj
== ifchange_object
);
2167 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
2169 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
2171 assert( sock
->ifchange_obj
);
2172 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
2173 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
2177 static void ifchange_poll_event( struct fd
*fd
, int event
)
2179 struct object
*ifchange
= get_fd_user( fd
);
2180 unsigned int status
= STATUS_PENDING
;
2181 char buffer
[PIPE_BUF
];
2184 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
2187 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
2188 return; /* retry when poll() says the socket is ready */
2189 status
= sock_get_ntstatus( errno
);
2193 struct nlmsghdr
*nlh
;
2195 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
2197 if (nlh
->nlmsg_type
== NLMSG_DONE
)
2199 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
2200 status
= STATUS_SUCCESS
;
2203 else status
= STATUS_CANCELLED
;
2205 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
2210 /* we only need one of these interface notification objects, all of the sockets dependent upon
2211 * it will wake up when a notification event occurs */
2212 static struct object
*get_ifchange( void )
2214 #ifdef HAVE_LINUX_RTNETLINK_H
2215 struct ifchange
*ifchange
;
2216 struct sockaddr_nl addr
;
2219 if (ifchange_object
)
2221 /* increment the refcount for each socket that uses the ifchange object */
2222 return grab_object( ifchange_object
);
2225 /* create the socket we need for processing interface change notifications */
2226 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
2229 set_error( sock_get_ntstatus( errno
));
2232 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
2233 memset( &addr
, 0, sizeof(addr
) );
2234 addr
.nl_family
= AF_NETLINK
;
2235 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
2236 /* bind the socket to the special netlink kernel interface */
2237 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
2240 set_error( sock_get_ntstatus( errno
));
2243 if (!(ifchange
= alloc_object( &ifchange_ops
)))
2246 set_error( STATUS_NO_MEMORY
);
2249 list_init( &ifchange
->sockets
);
2250 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
2252 release_object( ifchange
);
2253 set_error( STATUS_NO_MEMORY
);
2256 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
2258 /* the ifchange object is now successfully configured */
2259 ifchange_object
= &ifchange
->obj
;
2260 return &ifchange
->obj
;
2262 set_error( STATUS_NOT_SUPPORTED
);
2267 /* add the socket to the interface change notification list */
2268 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
2270 #ifdef HAVE_LINUX_RTNETLINK_H
2271 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
2273 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
2277 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
2278 static struct object
*sock_get_ifchange( struct sock
*sock
)
2280 struct object
*ifchange
;
2282 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
2283 return sock
->ifchange_obj
;
2285 if (!(ifchange
= get_ifchange()))
2288 /* add the socket to the ifchange notification list */
2289 ifchange_add_sock( ifchange
, sock
);
2290 sock
->ifchange_obj
= ifchange
;
2294 /* destroy an existing ifchange queue for a specific socket */
2295 static void sock_release_ifchange( struct sock
*sock
)
2297 if (sock
->ifchange_obj
)
2299 list_remove( &sock
->ifchange_entry
);
2300 release_object( sock
->ifchange_obj
);
2301 sock
->ifchange_obj
= NULL
;
2305 static void socket_device_dump( struct object
*obj
, int verbose
);
2306 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
2307 unsigned int attr
, struct object
*root
);
2308 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
2309 unsigned int sharing
, unsigned int options
);
2311 static const struct object_ops socket_device_ops
=
2313 sizeof(struct object
), /* size */
2314 &device_type
, /* type */
2315 socket_device_dump
, /* dump */
2316 no_add_queue
, /* add_queue */
2317 NULL
, /* remove_queue */
2318 NULL
, /* signaled */
2319 no_satisfied
, /* satisfied */
2320 no_signal
, /* signal */
2321 no_get_fd
, /* get_fd */
2322 default_map_access
, /* map_access */
2323 default_get_sd
, /* get_sd */
2324 default_set_sd
, /* set_sd */
2325 default_get_full_name
, /* get_full_name */
2326 socket_device_lookup_name
, /* lookup_name */
2327 directory_link_name
, /* link_name */
2328 default_unlink_name
, /* unlink_name */
2329 socket_device_open_file
, /* open_file */
2330 no_kernel_obj_list
, /* get_kernel_obj_list */
2331 no_close_handle
, /* close_handle */
2332 no_destroy
/* destroy */
2335 static void socket_device_dump( struct object
*obj
, int verbose
)
2337 fputs( "Socket device\n", stderr
);
2340 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
2341 unsigned int attr
, struct object
*root
)
2343 if (name
) name
->len
= 0;
2347 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
2348 unsigned int sharing
, unsigned int options
)
2352 if (!(sock
= create_socket())) return NULL
;
2353 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
2355 release_object( sock
);
2361 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
2362 unsigned int attr
, const struct security_descriptor
*sd
)
2364 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
2367 /* set socket event parameters */
2368 DECL_HANDLER(set_socket_event
)
2371 struct event
*old_event
;
2373 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
2374 FILE_WRITE_ATTRIBUTES
, &sock_ops
))) return;
2375 if (get_unix_fd( sock
->fd
) == -1) return;
2376 old_event
= sock
->event
;
2377 sock
->mask
= req
->mask
;
2380 sock
->pending_events
&= ~req
->mask
;
2381 sock
->reported_events
&= ~req
->mask
;
2384 sock
->window
= req
->window
;
2385 sock
->message
= req
->msg
;
2386 sock
->wparam
= req
->handle
; /* wparam is the socket handle */
2387 if (req
->event
) sock
->event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
2389 if (debug_level
&& sock
->event
) fprintf(stderr
, "event ptr: %p\n", sock
->event
);
2391 sock_reselect( sock
);
2393 sock
->nonblocking
= 1;
2395 /* if a network event is pending, signal the event object
2396 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
2397 before a WSAEventSelect() was done on it.
2398 (when dealing with Asynchronous socket) */
2399 sock_wake_up( sock
);
2401 if (old_event
) release_object( old_event
); /* we're through with it */
2402 release_object( &sock
->obj
);
2405 /* get socket event parameters */
2406 DECL_HANDLER(get_socket_event
)
2410 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
2411 FILE_READ_ATTRIBUTES
, &sock_ops
))) return;
2412 if (get_unix_fd( sock
->fd
) == -1) return;
2413 reply
->mask
= sock
->mask
;
2414 reply
->pmask
= sock
->pending_events
;
2415 set_reply_data( sock
->errors
, min( get_reply_max_size(), sizeof(sock
->errors
) ));
2421 struct event
*cevent
= get_event_obj( current
->process
, req
->c_event
,
2422 EVENT_MODIFY_STATE
);
2425 reset_event( cevent
);
2426 release_object( cevent
);
2429 sock
->pending_events
= 0;
2430 sock_reselect( sock
);
2432 release_object( &sock
->obj
);
2435 DECL_HANDLER(set_socket_deferred
)
2437 struct sock
*sock
, *acceptsock
;
2439 sock
=(struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
, &sock_ops
);
2443 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, req
->deferred
, 0, &sock_ops
);
2446 release_object( sock
);
2449 sock
->deferred
= acceptsock
;
2450 release_object( sock
);
2453 DECL_HANDLER(get_socket_info
)
2457 sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
, &sock_ops
);
2460 if (get_unix_fd( sock
->fd
) == -1) return;
2462 reply
->family
= sock
->family
;
2463 reply
->type
= sock
->type
;
2464 reply
->protocol
= sock
->proto
;
2466 release_object( &sock
->obj
);
2469 DECL_HANDLER(recv_socket
)
2471 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
2472 unsigned int status
= req
->status
;
2473 timeout_t timeout
= 0;
2474 struct async
*async
;
2480 /* recv() returned EWOULDBLOCK, i.e. no data available yet */
2481 if (status
== STATUS_DEVICE_NOT_READY
&& !sock
->nonblocking
)
2485 socklen_t len
= sizeof(tv
);
2487 /* Set a timeout on the async if necessary.
2489 * We want to do this *only* if the client gave us STATUS_DEVICE_NOT_READY.
2490 * If the client gave us STATUS_PENDING, it expects the async to always
2491 * block (it was triggered by WSARecv*() with a valid OVERLAPPED
2492 * structure) and for the timeout not to be respected. */
2493 if (is_fd_overlapped( fd
) && !getsockopt( get_unix_fd( fd
), SOL_SOCKET
, SO_RCVTIMEO
, (char *)&tv
, &len
))
2494 timeout
= tv
.tv_sec
* -10000000 + tv
.tv_usec
* -10;
2497 status
= STATUS_PENDING
;
2500 if (status
== STATUS_PENDING
&& sock
->rd_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
2502 sock
->pending_events
&= ~(req
->oob
? FD_OOB
: FD_READ
);
2503 sock
->reported_events
&= ~(req
->oob
? FD_OOB
: FD_READ
);
2505 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
2509 if (status
== STATUS_SUCCESS
)
2511 struct iosb
*iosb
= async_get_iosb( async
);
2512 iosb
->result
= req
->total
;
2513 release_object( iosb
);
2516 else if (status
== STATUS_PENDING
)
2520 set_error( status
);
2523 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
2525 if (status
== STATUS_PENDING
)
2526 queue_async( &sock
->read_q
, async
);
2528 /* always reselect; we changed reported_events above */
2529 sock_reselect( sock
);
2531 reply
->wait
= async_handoff( async
, success
, NULL
, 0 );
2532 reply
->options
= get_fd_options( fd
);
2533 release_object( async
);
2535 release_object( sock
);
2538 DECL_HANDLER(poll_socket
)
2540 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
2541 const struct poll_socket_input
*input
= get_req_data();
2542 struct async
*async
;
2547 count
= get_req_data_size() / sizeof(*input
);
2549 if ((async
= create_request_async( sock
->fd
, get_fd_comp_flags( sock
->fd
), &req
->async
)))
2551 reply
->wait
= async_handoff( async
, poll_socket( sock
, async
, req
->timeout
, count
, input
), NULL
, 0 );
2552 reply
->options
= get_fd_options( sock
->fd
);
2553 release_object( async
);
2556 release_object( sock
);
2559 DECL_HANDLER(send_socket
)
2561 struct sock
*sock
= (struct sock
*)get_handle_obj( current
->process
, req
->async
.handle
, 0, &sock_ops
);
2562 unsigned int status
= req
->status
;
2563 timeout_t timeout
= 0;
2564 struct async
*async
;
2570 if (status
!= STATUS_SUCCESS
)
2572 /* send() calls only clear and reselect events if unsuccessful. */
2573 sock
->pending_events
&= ~FD_WRITE
;
2574 sock
->reported_events
&= ~FD_WRITE
;
2577 /* If we had a short write and the socket is nonblocking (and the client is
2578 * not trying to force the operation to be asynchronous), return success.
2579 * Windows actually refuses to send any data in this case, and returns
2580 * EWOULDBLOCK, but we have no way of doing that. */
2581 if (status
== STATUS_DEVICE_NOT_READY
&& req
->total
&& sock
->nonblocking
)
2582 status
= STATUS_SUCCESS
;
2584 /* send() returned EWOULDBLOCK or a short write, i.e. cannot send all data yet */
2585 if (status
== STATUS_DEVICE_NOT_READY
&& !sock
->nonblocking
)
2589 socklen_t len
= sizeof(tv
);
2591 /* Set a timeout on the async if necessary.
2593 * We want to do this *only* if the client gave us STATUS_DEVICE_NOT_READY.
2594 * If the client gave us STATUS_PENDING, it expects the async to always
2595 * block (it was triggered by WSASend*() with a valid OVERLAPPED
2596 * structure) and for the timeout not to be respected. */
2597 if (is_fd_overlapped( fd
) && !getsockopt( get_unix_fd( fd
), SOL_SOCKET
, SO_SNDTIMEO
, (char *)&tv
, &len
))
2598 timeout
= tv
.tv_sec
* -10000000 + tv
.tv_usec
* -10;
2601 status
= STATUS_PENDING
;
2604 if (status
== STATUS_PENDING
&& sock
->wr_shutdown
) status
= STATUS_PIPE_DISCONNECTED
;
2606 if ((async
= create_request_async( fd
, get_fd_comp_flags( fd
), &req
->async
)))
2610 if (status
== STATUS_SUCCESS
)
2612 struct iosb
*iosb
= async_get_iosb( async
);
2613 iosb
->result
= req
->total
;
2614 release_object( iosb
);
2617 else if (status
== STATUS_PENDING
)
2621 set_error( status
);
2624 async_set_timeout( async
, timeout
, STATUS_IO_TIMEOUT
);
2626 if (status
== STATUS_PENDING
)
2627 queue_async( &sock
->write_q
, async
);
2629 /* always reselect; we changed reported_events above */
2630 sock_reselect( sock
);
2632 reply
->wait
= async_handoff( async
, success
, NULL
, 0 );
2633 reply
->options
= get_fd_options( fd
);
2634 release_object( async
);
2636 release_object( sock
);