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
104 struct sock
*sock
, *acceptsock
;
106 unsigned int recv_len
, local_len
;
114 unsigned int addr_len
, send_len
, send_cursor
;
119 struct object obj
; /* object header */
120 struct fd
*fd
; /* socket file descriptor */
121 unsigned int state
; /* status bits */
122 unsigned int mask
; /* event mask */
123 /* pending FD_* events which have not yet been reported to the application */
124 unsigned int pending_events
;
125 /* FD_* events which have already been reported and should not be selected
126 * for again until reset by a relevant call.
128 * For example, if FD_READ is set here and not in pending_events, it has
129 * already been reported and consumed, and we should not report it again,
130 * even if POLLIN is signaled, until it is reset by e.g recv().
132 * If an event has been signaled and not consumed yet, it will be set in
133 * both pending_events and reported_events (as we should only ever report
134 * any event once until it is reset.) */
135 unsigned int reported_events
;
136 unsigned int flags
; /* socket flags */
137 int polling
; /* is socket being polled? */
138 int wr_shutdown_pending
; /* is a write shutdown pending? */
139 unsigned short proto
; /* socket protocol */
140 unsigned short type
; /* socket type */
141 unsigned short family
; /* socket family */
142 struct event
*event
; /* event object */
143 user_handle_t window
; /* window to send the message to */
144 unsigned int message
; /* message to send */
145 obj_handle_t wparam
; /* message wparam (socket handle) */
146 unsigned int errors
[FD_MAX_EVENTS
]; /* event errors */
147 timeout_t connect_time
;/* time the socket was connected */
148 struct sock
*deferred
; /* socket that waits for a deferred accept */
149 struct async_queue read_q
; /* queue for asynchronous reads */
150 struct async_queue write_q
; /* queue for asynchronous writes */
151 struct async_queue ifchange_q
; /* queue for interface change notifications */
152 struct async_queue accept_q
; /* queue for asynchronous accepts */
153 struct async_queue connect_q
; /* queue for asynchronous connects */
154 struct object
*ifchange_obj
; /* the interface change notification object */
155 struct list ifchange_entry
; /* entry in ifchange notification list */
156 struct list accept_list
; /* list of pending accept requests */
157 struct accept_req
*accept_recv_req
; /* pending accept-into request which will recv on this socket */
158 struct connect_req
*connect_req
; /* pending connection request */
161 static void sock_dump( struct object
*obj
, int verbose
);
162 static struct fd
*sock_get_fd( struct object
*obj
);
163 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
164 static void sock_destroy( struct object
*obj
);
165 static struct object
*sock_get_ifchange( struct sock
*sock
);
166 static void sock_release_ifchange( struct sock
*sock
);
168 static int sock_get_poll_events( struct fd
*fd
);
169 static void sock_poll_event( struct fd
*fd
, int event
);
170 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
171 static int sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
172 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
);
173 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
175 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
);
176 static struct sock
*accept_socket( struct sock
*sock
);
177 static int sock_get_ntstatus( int err
);
178 static unsigned int sock_get_error( int err
);
180 static const struct object_ops sock_ops
=
182 sizeof(struct sock
), /* size */
183 &file_type
, /* type */
184 sock_dump
, /* dump */
185 add_queue
, /* add_queue */
186 remove_queue
, /* remove_queue */
187 default_fd_signaled
, /* signaled */
188 no_satisfied
, /* satisfied */
189 no_signal
, /* signal */
190 sock_get_fd
, /* get_fd */
191 default_map_access
, /* map_access */
192 default_get_sd
, /* get_sd */
193 default_set_sd
, /* set_sd */
194 no_get_full_name
, /* get_full_name */
195 no_lookup_name
, /* lookup_name */
196 no_link_name
, /* link_name */
197 NULL
, /* unlink_name */
198 no_open_file
, /* open_file */
199 no_kernel_obj_list
, /* get_kernel_obj_list */
200 sock_close_handle
, /* close_handle */
201 sock_destroy
/* destroy */
204 static const struct fd_ops sock_fd_ops
=
206 sock_get_poll_events
, /* get_poll_events */
207 sock_poll_event
, /* poll_event */
208 sock_get_fd_type
, /* get_fd_type */
209 no_fd_read
, /* read */
210 no_fd_write
, /* write */
211 no_fd_flush
, /* flush */
212 default_fd_get_file_info
, /* get_file_info */
213 no_fd_get_volume_info
, /* get_volume_info */
214 sock_ioctl
, /* ioctl */
215 sock_queue_async
, /* queue_async */
216 sock_reselect_async
/* reselect_async */
221 struct sockaddr addr
;
222 struct sockaddr_in in
;
223 struct sockaddr_in6 in6
;
225 struct sockaddr_ipx ipx
;
228 struct sockaddr_irda irda
;
232 static int sockaddr_from_unix( const union unix_sockaddr
*uaddr
, struct WS_sockaddr
*wsaddr
, socklen_t wsaddrlen
)
234 memset( wsaddr
, 0, wsaddrlen
);
236 switch (uaddr
->addr
.sa_family
)
240 struct WS_sockaddr_in win
= {0};
242 if (wsaddrlen
< sizeof(win
)) return -1;
243 win
.sin_family
= WS_AF_INET
;
244 win
.sin_port
= uaddr
->in
.sin_port
;
245 memcpy( &win
.sin_addr
, &uaddr
->in
.sin_addr
, sizeof(win
.sin_addr
) );
246 memcpy( wsaddr
, &win
, sizeof(win
) );
252 struct WS_sockaddr_in6 win
= {0};
254 if (wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
)) return -1;
255 win
.sin6_family
= WS_AF_INET6
;
256 win
.sin6_port
= uaddr
->in6
.sin6_port
;
257 win
.sin6_flowinfo
= uaddr
->in6
.sin6_flowinfo
;
258 memcpy( &win
.sin6_addr
, &uaddr
->in6
.sin6_addr
, sizeof(win
.sin6_addr
) );
259 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
260 win
.sin6_scope_id
= uaddr
->in6
.sin6_scope_id
;
262 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
))
264 memcpy( wsaddr
, &win
, sizeof(struct WS_sockaddr_in6
) );
265 return sizeof(struct WS_sockaddr_in6
);
267 memcpy( wsaddr
, &win
, sizeof(struct WS_sockaddr_in6_old
) );
268 return sizeof(struct WS_sockaddr_in6_old
);
274 struct WS_sockaddr_ipx win
= {0};
276 if (wsaddrlen
< sizeof(win
)) return -1;
277 win
.sa_family
= WS_AF_IPX
;
278 memcpy( win
.sa_netnum
, &uaddr
->ipx
.sipx_network
, sizeof(win
.sa_netnum
) );
279 memcpy( win
.sa_nodenum
, &uaddr
->ipx
.sipx_node
, sizeof(win
.sa_nodenum
) );
280 win
.sa_socket
= uaddr
->ipx
.sipx_port
;
281 memcpy( wsaddr
, &win
, sizeof(win
) );
291 if (wsaddrlen
< sizeof(win
)) return -1;
292 win
.irdaAddressFamily
= WS_AF_IRDA
;
293 memcpy( win
.irdaDeviceID
, &uaddr
->irda
.sir_addr
, sizeof(win
.irdaDeviceID
) );
294 if (uaddr
->irda
.sir_lsap_sel
!= LSAP_ANY
)
295 snprintf( win
.irdaServiceName
, sizeof(win
.irdaServiceName
), "LSAP-SEL%u", uaddr
->irda
.sir_lsap_sel
);
297 memcpy( win
.irdaServiceName
, uaddr
->irda
.sir_name
, sizeof(win
.irdaServiceName
) );
298 memcpy( wsaddr
, &win
, sizeof(win
) );
312 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
313 * we post messages if there are multiple events. Used to send
314 * messages. The problem is if there is both a FD_CONNECT event and,
315 * say, an FD_READ event available on the same socket, we want to
316 * notify the app of the connect event first. Otherwise it may
317 * discard the read event because it thinks it hasn't connected yet.
319 static const int event_bitorder
[FD_MAX_EVENTS
] =
327 6, 7, 8, 9 /* leftovers */
330 /* Flags that make sense only for SOCK_STREAM sockets */
331 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
334 SOCK_SHUTDOWN_ERROR
= -1,
335 SOCK_SHUTDOWN_EOF
= 0,
336 SOCK_SHUTDOWN_POLLHUP
= 1
339 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
341 static sock_shutdown_t
sock_check_pollhup(void)
343 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
348 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
349 if ( shutdown( fd
[0], 1 ) ) goto out
;
355 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
356 n
= poll( &pfd
, 1, 1 );
357 if ( n
!= 1 ) goto out
; /* error or timeout */
358 if ( pfd
.revents
& POLLHUP
)
359 ret
= SOCK_SHUTDOWN_POLLHUP
;
360 else if ( pfd
.revents
& POLLIN
&&
361 read( fd
[1], &dummy
, 1 ) == 0 )
362 ret
= SOCK_SHUTDOWN_EOF
;
372 sock_shutdown_type
= sock_check_pollhup();
374 switch ( sock_shutdown_type
)
376 case SOCK_SHUTDOWN_EOF
:
377 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
379 case SOCK_SHUTDOWN_POLLHUP
:
380 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
383 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
384 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
388 static int sock_reselect( struct sock
*sock
)
390 int ev
= sock_get_poll_events( sock
->fd
);
393 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
395 if (!sock
->polling
) /* FIXME: should find a better way to do this */
397 /* previously unconnected socket, is this reselect supposed to connect it? */
398 if (!(sock
->state
& ~FD_WINE_NONBLOCKING
)) return 0;
399 /* ok, it is, attach it to the wineserver's main poll loop */
401 allow_fd_caching( sock
->fd
);
403 /* update condition mask */
404 set_fd_events( sock
->fd
, ev
);
408 /* wake anybody waiting on the socket event or send the associated message */
409 static void sock_wake_up( struct sock
*sock
)
411 unsigned int events
= sock
->pending_events
& sock
->mask
;
416 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
418 set_event( sock
->event
);
422 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
423 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
425 int event
= event_bitorder
[i
];
426 if (events
& (1 << event
))
428 lparam_t lparam
= (1 << event
) | (sock
->errors
[event
] << 16);
429 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
432 sock
->pending_events
= 0;
433 sock_reselect( sock
);
437 static inline int sock_error( struct fd
*fd
)
439 unsigned int optval
= 0;
440 socklen_t optlen
= sizeof(optval
);
442 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
446 static void free_accept_req( void *private )
448 struct accept_req
*req
= private;
449 list_remove( &req
->entry
);
452 req
->acceptsock
->accept_recv_req
= NULL
;
453 release_object( req
->acceptsock
);
455 release_object( req
->async
);
456 release_object( req
->iosb
);
457 release_object( req
->sock
);
461 static void fill_accept_output( struct accept_req
*req
)
463 struct iosb
*iosb
= req
->iosb
;
464 union unix_sockaddr unix_addr
;
465 struct WS_sockaddr
*win_addr
;
466 unsigned int remote_len
;
472 if (!(out_data
= mem_alloc( iosb
->out_size
))) return;
474 fd
= get_unix_fd( req
->acceptsock
->fd
);
476 if (req
->recv_len
&& (size
= recv( fd
, out_data
, req
->recv_len
, 0 )) < 0)
478 if (!req
->accepted
&& errno
== EWOULDBLOCK
)
481 sock_reselect( req
->acceptsock
);
482 set_error( STATUS_PENDING
);
486 set_error( sock_get_ntstatus( errno
) );
493 if (req
->local_len
< sizeof(int))
495 set_error( STATUS_BUFFER_TOO_SMALL
);
500 unix_len
= sizeof(unix_addr
);
501 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ sizeof(int));
502 if (getsockname( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
503 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, req
->local_len
- sizeof(int) )) < 0)
505 set_error( sock_get_ntstatus( errno
) );
509 memcpy( out_data
+ req
->recv_len
, &win_len
, sizeof(int) );
512 unix_len
= sizeof(unix_addr
);
513 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ req
->local_len
+ sizeof(int));
514 remote_len
= iosb
->out_size
- req
->recv_len
- req
->local_len
;
515 if (getpeername( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
516 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, remote_len
- sizeof(int) )) < 0)
518 set_error( sock_get_ntstatus( errno
) );
522 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
524 iosb
->status
= STATUS_SUCCESS
;
526 iosb
->out_data
= out_data
;
527 set_error( STATUS_ALERTED
);
530 static void complete_async_accept( struct sock
*sock
, struct accept_req
*req
)
532 struct sock
*acceptsock
= req
->acceptsock
;
533 struct async
*async
= req
->async
;
535 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
539 if (!accept_into_socket( sock
, acceptsock
)) return;
540 fill_accept_output( req
);
544 struct iosb
*iosb
= req
->iosb
;
547 if (!(acceptsock
= accept_socket( sock
))) return;
548 handle
= alloc_handle_no_access_check( async_get_thread( async
)->process
, &acceptsock
->obj
,
549 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
550 acceptsock
->wparam
= handle
;
551 release_object( acceptsock
);
554 if (!(iosb
->out_data
= malloc( sizeof(handle
) ))) return;
556 iosb
->status
= STATUS_SUCCESS
;
557 iosb
->out_size
= sizeof(handle
);
558 memcpy( iosb
->out_data
, &handle
, sizeof(handle
) );
559 set_error( STATUS_ALERTED
);
563 static void complete_async_accept_recv( struct accept_req
*req
)
565 if (debug_level
) fprintf( stderr
, "completing accept recv request for socket %p\n", req
->acceptsock
);
567 assert( req
->recv_len
);
569 fill_accept_output( req
);
572 static void free_connect_req( void *private )
574 struct connect_req
*req
= private;
576 req
->sock
->connect_req
= NULL
;
577 release_object( req
->async
);
578 release_object( req
->iosb
);
579 release_object( req
->sock
);
583 static void complete_async_connect( struct sock
*sock
)
585 struct connect_req
*req
= sock
->connect_req
;
586 const char *in_buffer
;
591 if (debug_level
) fprintf( stderr
, "completing connect request for socket %p\n", sock
);
593 sock
->pending_events
&= ~(FD_CONNECT
| FD_READ
| FD_WRITE
);
594 sock
->reported_events
&= ~(FD_CONNECT
| FD_READ
| FD_WRITE
);
595 sock
->state
|= FD_WINE_CONNECTED
;
596 sock
->state
&= ~(FD_CONNECT
| FD_WINE_LISTENING
);
600 set_error( STATUS_SUCCESS
);
605 in_buffer
= (const char *)iosb
->in_data
+ sizeof(struct afd_connect_params
) + req
->addr_len
;
606 len
= req
->send_len
- req
->send_cursor
;
608 ret
= send( get_unix_fd( sock
->fd
), in_buffer
+ req
->send_cursor
, len
, 0 );
609 if (ret
< 0 && errno
!= EWOULDBLOCK
)
610 set_error( sock_get_ntstatus( errno
) );
613 iosb
->result
= req
->send_len
;
614 iosb
->status
= STATUS_SUCCESS
;
615 set_error( STATUS_ALERTED
);
619 req
->send_cursor
+= ret
;
620 set_error( STATUS_PENDING
);
624 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
626 if (event
& (POLLIN
| POLLPRI
))
628 struct accept_req
*req
;
630 LIST_FOR_EACH_ENTRY( req
, &sock
->accept_list
, struct accept_req
, entry
)
632 if (req
->iosb
->status
== STATUS_PENDING
&& !req
->accepted
)
634 complete_async_accept( sock
, req
);
635 if (get_error() != STATUS_PENDING
)
636 async_terminate( req
->async
, get_error() );
641 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
643 complete_async_accept_recv( sock
->accept_recv_req
);
644 if (get_error() != STATUS_PENDING
)
645 async_terminate( sock
->accept_recv_req
->async
, get_error() );
649 if ((event
& POLLOUT
) && sock
->connect_req
&& sock
->connect_req
->iosb
->status
== STATUS_PENDING
)
651 complete_async_connect( sock
);
652 if (get_error() != STATUS_PENDING
)
653 async_terminate( sock
->connect_req
->async
, get_error() );
656 if (is_fd_overlapped( sock
->fd
))
658 if (event
& (POLLIN
|POLLPRI
) && async_waiting( &sock
->read_q
))
660 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
661 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
662 event
&= ~(POLLIN
|POLLPRI
);
664 if (event
& POLLOUT
&& async_waiting( &sock
->write_q
))
666 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
667 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
672 if (event
& (POLLERR
| POLLHUP
))
674 int status
= sock_get_ntstatus( error
);
675 struct accept_req
*req
, *next
;
677 if (!(sock
->state
& FD_READ
))
678 async_wake_up( &sock
->read_q
, status
);
679 if (!(sock
->state
& FD_WRITE
))
680 async_wake_up( &sock
->write_q
, status
);
682 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
684 if (req
->iosb
->status
== STATUS_PENDING
)
685 async_terminate( req
->async
, status
);
688 if (sock
->accept_recv_req
&& sock
->accept_recv_req
->iosb
->status
== STATUS_PENDING
)
689 async_terminate( sock
->accept_recv_req
->async
, status
);
691 if (sock
->connect_req
)
692 async_terminate( sock
->connect_req
->async
, status
);
698 static void post_socket_event( struct sock
*sock
, unsigned int event_bit
, unsigned int error
)
700 unsigned int event
= (1 << event_bit
);
702 if (!(sock
->reported_events
& event
))
704 sock
->pending_events
|= event
;
705 sock
->reported_events
|= event
;
706 sock
->errors
[event_bit
] = error
;
710 static void sock_dispatch_events( struct sock
*sock
, int prevstate
, int event
, int error
)
712 if (prevstate
& FD_CONNECT
)
714 post_socket_event( sock
, FD_CONNECT_BIT
, sock_get_error( error
) );
717 if (prevstate
& FD_WINE_LISTENING
)
719 post_socket_event( sock
, FD_ACCEPT_BIT
, sock_get_error( error
) );
724 post_socket_event( sock
, FD_READ_BIT
, 0 );
727 post_socket_event( sock
, FD_WRITE_BIT
, 0 );
730 post_socket_event( sock
, FD_OOB_BIT
, 0 );
732 if (event
& (POLLERR
|POLLHUP
))
733 post_socket_event( sock
, FD_CLOSE_BIT
, sock_get_error( error
) );
736 sock_wake_up( sock
);
739 static void sock_poll_event( struct fd
*fd
, int event
)
741 struct sock
*sock
= get_fd_user( fd
);
743 int prevstate
= sock
->state
;
746 assert( sock
->obj
.ops
== &sock_ops
);
748 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
750 /* we may change event later, remove from loop here */
751 if (event
& (POLLERR
|POLLHUP
)) set_fd_events( sock
->fd
, -1 );
753 if (sock
->state
& FD_CONNECT
)
755 if (event
& (POLLERR
|POLLHUP
))
757 /* we didn't get connected? */
758 sock
->state
&= ~FD_CONNECT
;
760 error
= sock_error( fd
);
762 else if (event
& POLLOUT
)
764 /* we got connected */
765 sock
->state
|= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
766 sock
->state
&= ~FD_CONNECT
;
767 sock
->connect_time
= current_time
;
770 else if (sock
->state
& FD_WINE_LISTENING
)
773 if (event
& (POLLERR
|POLLHUP
))
774 error
= sock_error( fd
);
778 /* normal data flow */
779 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
784 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
785 * has been closed, so we need to check for it explicitly here */
786 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
795 /* EAGAIN can happen if an async recv() falls between the server's poll()
796 call and the invocation of this routine */
797 if ( errno
!= EAGAIN
)
802 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
807 if ( (hangup_seen
|| event
& (POLLHUP
|POLLERR
)) && (sock
->state
& (FD_READ
|FD_WRITE
)) )
809 error
= error
? error
: sock_error( fd
);
810 if ( (event
& POLLERR
) || ( sock_shutdown_type
== SOCK_SHUTDOWN_EOF
&& (event
& POLLHUP
) ))
811 sock
->state
&= ~FD_WRITE
;
812 sock
->state
&= ~FD_READ
;
815 fprintf(stderr
, "socket %p aborted by error %d, event: %x\n", sock
, error
, event
);
822 event
= sock_dispatch_asyncs( sock
, event
, error
);
823 sock_dispatch_events( sock
, prevstate
, event
, error
);
825 sock_reselect( sock
);
828 static void sock_dump( struct object
*obj
, int verbose
)
830 struct sock
*sock
= (struct sock
*)obj
;
831 assert( obj
->ops
== &sock_ops
);
832 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
833 sock
->fd
, sock
->state
,
834 sock
->mask
, sock
->pending_events
, sock
->reported_events
);
837 static int sock_get_poll_events( struct fd
*fd
)
839 struct sock
*sock
= get_fd_user( fd
);
840 unsigned int mask
= sock
->mask
& ~sock
->reported_events
;
841 unsigned int smask
= sock
->state
& mask
;
844 assert( sock
->obj
.ops
== &sock_ops
);
846 if (sock
->state
& FD_CONNECT
)
847 /* connecting, wait for writable */
850 if (!list_empty( &sock
->accept_list
) || sock
->accept_recv_req
)
852 ev
|= POLLIN
| POLLPRI
;
854 else if (async_queued( &sock
->read_q
))
856 if (async_waiting( &sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
858 else if (smask
& FD_READ
|| (sock
->state
& FD_WINE_LISTENING
&& mask
& FD_ACCEPT
))
859 ev
|= POLLIN
| POLLPRI
;
860 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
861 else if (sock
->type
== WS_SOCK_STREAM
&& (mask
& FD_CLOSE
) && !(sock
->reported_events
& FD_READ
))
864 if (async_queued( &sock
->write_q
))
866 if (async_waiting( &sock
->write_q
)) ev
|= POLLOUT
;
868 else if (smask
& FD_WRITE
)
874 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
876 return FD_TYPE_SOCKET
;
879 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
881 struct sock
*sock
= get_fd_user( fd
);
882 struct async_queue
*queue
;
884 assert( sock
->obj
.ops
== &sock_ops
);
888 case ASYNC_TYPE_READ
:
889 queue
= &sock
->read_q
;
891 case ASYNC_TYPE_WRITE
:
892 queue
= &sock
->write_q
;
895 set_error( STATUS_INVALID_PARAMETER
);
899 if ( ( !( sock
->state
& (FD_READ
|FD_CONNECT
|FD_WINE_LISTENING
) ) && type
== ASYNC_TYPE_READ
) ||
900 ( !( sock
->state
& (FD_WRITE
|FD_CONNECT
) ) && type
== ASYNC_TYPE_WRITE
) )
902 set_error( STATUS_PIPE_DISCONNECTED
);
906 queue_async( queue
, async
);
907 sock_reselect( sock
);
909 set_error( STATUS_PENDING
);
912 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
914 struct sock
*sock
= get_fd_user( fd
);
916 if (sock
->wr_shutdown_pending
&& list_empty( &sock
->write_q
.queue
))
917 shutdown( get_unix_fd( sock
->fd
), SHUT_WR
);
919 /* Don't reselect the ifchange queue; we always ask for POLLIN.
920 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
922 if (queue
!= &sock
->ifchange_q
&& sock
->type
)
923 sock_reselect( sock
);
926 static struct fd
*sock_get_fd( struct object
*obj
)
928 struct sock
*sock
= (struct sock
*)obj
;
929 return (struct fd
*)grab_object( sock
->fd
);
932 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
934 struct sock
*sock
= (struct sock
*)obj
;
935 struct accept_req
*req
, *next
;
937 if (sock
->obj
.handle_count
== 1) /* last handle */
939 if (sock
->accept_recv_req
)
940 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
942 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
943 async_terminate( req
->async
, STATUS_CANCELLED
);
945 if (sock
->connect_req
)
946 async_terminate( sock
->connect_req
->async
, STATUS_CANCELLED
);
952 static void sock_destroy( struct object
*obj
)
954 struct sock
*sock
= (struct sock
*)obj
;
956 assert( obj
->ops
== &sock_ops
);
958 /* FIXME: special socket shutdown stuff? */
960 if ( sock
->deferred
)
961 release_object( sock
->deferred
);
963 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
964 sock_release_ifchange( sock
);
965 free_async_queue( &sock
->read_q
);
966 free_async_queue( &sock
->write_q
);
967 free_async_queue( &sock
->ifchange_q
);
968 free_async_queue( &sock
->accept_q
);
969 free_async_queue( &sock
->connect_q
);
970 if (sock
->event
) release_object( sock
->event
);
973 /* shut the socket down to force pending poll() calls in the client to return */
974 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
975 release_object( sock
->fd
);
979 static struct sock
*create_socket(void)
983 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
987 sock
->pending_events
= 0;
988 sock
->reported_events
= 0;
990 sock
->wr_shutdown_pending
= 0;
999 sock
->connect_time
= 0;
1000 sock
->deferred
= NULL
;
1001 sock
->ifchange_obj
= NULL
;
1002 sock
->accept_recv_req
= NULL
;
1003 sock
->connect_req
= NULL
;
1004 init_async_queue( &sock
->read_q
);
1005 init_async_queue( &sock
->write_q
);
1006 init_async_queue( &sock
->ifchange_q
);
1007 init_async_queue( &sock
->accept_q
);
1008 init_async_queue( &sock
->connect_q
);
1009 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
1010 list_init( &sock
->accept_list
);
1014 static int get_unix_family( int family
)
1018 case WS_AF_INET
: return AF_INET
;
1019 case WS_AF_INET6
: return AF_INET6
;
1021 case WS_AF_IPX
: return AF_IPX
;
1024 case WS_AF_IRDA
: return AF_IRDA
;
1026 case WS_AF_UNSPEC
: return AF_UNSPEC
;
1031 static int get_unix_type( int type
)
1035 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
1036 case WS_SOCK_RAW
: return SOCK_RAW
;
1037 case WS_SOCK_STREAM
: return SOCK_STREAM
;
1042 static int get_unix_protocol( int protocol
)
1044 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1049 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
1050 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1051 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1052 case WS_IPPROTO_IPV4
: return IPPROTO_IPIP
;
1053 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1054 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1055 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1056 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1061 static void set_dont_fragment( int fd
, int level
, int value
)
1065 if (level
== IPPROTO_IP
)
1068 optname
= IP_DONTFRAG
;
1069 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1070 optname
= IP_MTU_DISCOVER
;
1071 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1078 #ifdef IPV6_DONTFRAG
1079 optname
= IPV6_DONTFRAG
;
1080 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1081 optname
= IPV6_MTU_DISCOVER
;
1082 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1088 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1091 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
, unsigned int flags
)
1093 unsigned int options
= 0;
1094 int sockfd
, unix_type
, unix_family
, unix_protocol
;
1096 unix_family
= get_unix_family( family
);
1097 unix_type
= get_unix_type( type
);
1098 unix_protocol
= get_unix_protocol( protocol
);
1100 if (unix_protocol
< 0)
1102 if (type
&& unix_type
< 0)
1103 set_win32_error( WSAESOCKTNOSUPPORT
);
1105 set_win32_error( WSAEPROTONOSUPPORT
);
1108 if (unix_family
< 0)
1110 if (family
>= 0 && unix_type
< 0)
1111 set_win32_error( WSAESOCKTNOSUPPORT
);
1113 set_win32_error( WSAEAFNOSUPPORT
);
1117 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1120 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1121 else set_win32_error( sock_get_error( errno
));
1124 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1126 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1129 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1132 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1135 /* Should we retrieve val using a getsockopt call and then
1136 * set the modified one? */
1137 val
.ipx_pt
= ipx_type
;
1138 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1143 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1145 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1146 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1147 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1148 else if (unix_type
== SOCK_STREAM
)
1149 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1153 if (unix_family
== AF_INET6
)
1155 static const int enable
= 1;
1156 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1160 sock
->state
= (type
!= SOCK_STREAM
) ? (FD_READ
|FD_WRITE
) : 0;
1161 sock
->flags
= flags
;
1162 sock
->proto
= protocol
;
1164 sock
->family
= family
;
1168 options
= get_fd_options( sock
->fd
);
1169 release_object( sock
->fd
);
1172 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1176 sock_reselect( sock
);
1180 /* accepts a socket and inits it */
1181 static int accept_new_fd( struct sock
*sock
)
1184 /* Try to accept(2). We can't be safe that this an already connected socket
1185 * or that accept() is allowed on it. In those cases we will get -1/errno
1188 struct sockaddr saddr
;
1189 socklen_t slen
= sizeof(saddr
);
1190 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1192 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1194 set_error( sock_get_ntstatus( errno
));
1198 /* accept a socket (creates a new fd) */
1199 static struct sock
*accept_socket( struct sock
*sock
)
1201 struct sock
*acceptsock
;
1204 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1206 if ( sock
->deferred
)
1208 acceptsock
= sock
->deferred
;
1209 sock
->deferred
= NULL
;
1213 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1214 if (!(acceptsock
= create_socket()))
1220 /* newly created socket gets the same properties of the listening socket */
1221 acceptsock
->state
= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
1222 if (sock
->state
& FD_WINE_NONBLOCKING
)
1223 acceptsock
->state
|= FD_WINE_NONBLOCKING
;
1224 acceptsock
->mask
= sock
->mask
;
1225 acceptsock
->proto
= sock
->proto
;
1226 acceptsock
->type
= sock
->type
;
1227 acceptsock
->family
= sock
->family
;
1228 acceptsock
->window
= sock
->window
;
1229 acceptsock
->message
= sock
->message
;
1230 acceptsock
->connect_time
= current_time
;
1231 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
1232 acceptsock
->flags
= sock
->flags
;
1233 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1234 get_fd_options( sock
->fd
) )))
1236 release_object( acceptsock
);
1241 sock
->pending_events
&= ~FD_ACCEPT
;
1242 sock
->reported_events
&= ~FD_ACCEPT
;
1243 sock_reselect( sock
);
1247 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
1252 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
1254 if ( sock
->deferred
)
1256 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
1257 get_fd_options( acceptsock
->fd
) );
1261 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
1263 release_object( sock
->deferred
);
1264 sock
->deferred
= NULL
;
1268 if ((acceptfd
= accept_new_fd( sock
)) == -1)
1271 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1272 get_fd_options( acceptsock
->fd
) )))
1276 acceptsock
->state
|= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
1277 acceptsock
->pending_events
= 0;
1278 acceptsock
->reported_events
= 0;
1279 acceptsock
->polling
= 0;
1280 acceptsock
->proto
= sock
->proto
;
1281 acceptsock
->type
= sock
->type
;
1282 acceptsock
->family
= sock
->family
;
1283 acceptsock
->wparam
= 0;
1284 acceptsock
->deferred
= NULL
;
1285 acceptsock
->connect_time
= current_time
;
1286 fd_copy_completion( acceptsock
->fd
, newfd
);
1287 release_object( acceptsock
->fd
);
1288 acceptsock
->fd
= newfd
;
1291 sock
->pending_events
&= ~FD_ACCEPT
;
1292 sock
->reported_events
&= ~FD_ACCEPT
;
1293 sock_reselect( sock
);
1298 /* return an errno value mapped to a WSA error */
1299 static unsigned int sock_get_error( int err
)
1303 case EINTR
: return WSAEINTR
;
1304 case EBADF
: return WSAEBADF
;
1306 case EACCES
: return WSAEACCES
;
1307 case EFAULT
: return WSAEFAULT
;
1308 case EINVAL
: return WSAEINVAL
;
1309 case EMFILE
: return WSAEMFILE
;
1311 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
1312 case EALREADY
: return WSAEALREADY
;
1313 case ENOTSOCK
: return WSAENOTSOCK
;
1314 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
1315 case EMSGSIZE
: return WSAEMSGSIZE
;
1316 case EPROTOTYPE
: return WSAEPROTOTYPE
;
1317 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
1318 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
1319 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
1320 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
1321 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
1322 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
1323 case EADDRINUSE
: return WSAEADDRINUSE
;
1324 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
1325 case ENETDOWN
: return WSAENETDOWN
;
1326 case ENETUNREACH
: return WSAENETUNREACH
;
1327 case ENETRESET
: return WSAENETRESET
;
1328 case ECONNABORTED
: return WSAECONNABORTED
;
1330 case ECONNRESET
: return WSAECONNRESET
;
1331 case ENOBUFS
: return WSAENOBUFS
;
1332 case EISCONN
: return WSAEISCONN
;
1333 case ENOTCONN
: return WSAENOTCONN
;
1334 case ESHUTDOWN
: return WSAESHUTDOWN
;
1335 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
1336 case ETIMEDOUT
: return WSAETIMEDOUT
;
1337 case ECONNREFUSED
: return WSAECONNREFUSED
;
1338 case ELOOP
: return WSAELOOP
;
1339 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
1340 case EHOSTDOWN
: return WSAEHOSTDOWN
;
1341 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
1342 case ENOTEMPTY
: return WSAENOTEMPTY
;
1344 case EPROCLIM
: return WSAEPROCLIM
;
1347 case EUSERS
: return WSAEUSERS
;
1350 case EDQUOT
: return WSAEDQUOT
;
1353 case ESTALE
: return WSAESTALE
;
1356 case EREMOTE
: return WSAEREMOTE
;
1362 perror("wineserver: sock_get_error() can't map error");
1367 static int sock_get_ntstatus( int err
)
1371 case EBADF
: return STATUS_INVALID_HANDLE
;
1372 case EBUSY
: return STATUS_DEVICE_BUSY
;
1374 case EACCES
: return STATUS_ACCESS_DENIED
;
1375 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
1376 case EINVAL
: return STATUS_INVALID_PARAMETER
;
1378 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
1380 case EWOULDBLOCK
: return STATUS_DEVICE_NOT_READY
;
1381 case EALREADY
: return STATUS_NETWORK_BUSY
;
1382 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
1383 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
1384 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
1385 case EPROTONOSUPPORT
:
1386 case ESOCKTNOSUPPORT
:
1389 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
1390 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
1391 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
1392 case EADDRINUSE
: return STATUS_SHARING_VIOLATION
;
1393 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
1394 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
1395 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
1396 case ENOTCONN
: return STATUS_INVALID_CONNECTION
;
1397 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
1398 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
1399 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
1400 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
1402 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
1403 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
1404 case EISCONN
: return STATUS_CONNECTION_ACTIVE
;
1406 case 0: return STATUS_SUCCESS
;
1409 perror("wineserver: sock_get_ntstatus() can't map error");
1410 return STATUS_UNSUCCESSFUL
;
1414 static struct accept_req
*alloc_accept_req( struct sock
*sock
, struct sock
*acceptsock
, struct async
*async
,
1415 const struct afd_accept_into_params
*params
)
1417 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
1421 req
->async
= (struct async
*)grab_object( async
);
1422 req
->iosb
= async_get_iosb( async
);
1423 req
->sock
= (struct sock
*)grab_object( sock
);
1424 req
->acceptsock
= acceptsock
;
1425 if (acceptsock
) grab_object( acceptsock
);
1431 req
->recv_len
= params
->recv_len
;
1432 req
->local_len
= params
->local_len
;
1438 static int sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
1440 struct sock
*sock
= get_fd_user( fd
);
1443 assert( sock
->obj
.ops
== &sock_ops
);
1445 if (code
!= IOCTL_AFD_WINE_CREATE
&& (unix_fd
= get_unix_fd( fd
)) < 0) return 0;
1449 case IOCTL_AFD_WINE_CREATE
:
1451 const struct afd_create_params
*params
= get_req_data();
1453 if (get_req_data_size() != sizeof(*params
))
1455 set_error( STATUS_INVALID_PARAMETER
);
1458 init_socket( sock
, params
->family
, params
->type
, params
->protocol
, params
->flags
);
1462 case IOCTL_AFD_WINE_ACCEPT
:
1464 struct sock
*acceptsock
;
1465 obj_handle_t handle
;
1467 if (get_reply_max_size() != sizeof(handle
))
1469 set_error( STATUS_BUFFER_TOO_SMALL
);
1473 if (!(acceptsock
= accept_socket( sock
)))
1475 struct accept_req
*req
;
1477 if (sock
->state
& FD_WINE_NONBLOCKING
) return 0;
1478 if (get_error() != STATUS_DEVICE_NOT_READY
) return 0;
1480 if (!(req
= alloc_accept_req( sock
, NULL
, async
, NULL
))) return 0;
1481 list_add_tail( &sock
->accept_list
, &req
->entry
);
1483 async_set_completion_callback( async
, free_accept_req
, req
);
1484 queue_async( &sock
->accept_q
, async
);
1485 sock_reselect( sock
);
1486 set_error( STATUS_PENDING
);
1489 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
1490 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
1491 acceptsock
->wparam
= handle
;
1492 release_object( acceptsock
);
1493 set_reply_data( &handle
, sizeof(handle
) );
1497 case IOCTL_AFD_WINE_ACCEPT_INTO
:
1499 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
1500 const struct afd_accept_into_params
*params
= get_req_data();
1501 struct sock
*acceptsock
;
1502 unsigned int remote_len
;
1503 struct accept_req
*req
;
1505 if (get_req_data_size() != sizeof(*params
) ||
1506 get_reply_max_size() < params
->recv_len
||
1507 get_reply_max_size() - params
->recv_len
< params
->local_len
)
1509 set_error( STATUS_BUFFER_TOO_SMALL
);
1513 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
1514 if (remote_len
< sizeof(int))
1516 set_error( STATUS_INVALID_PARAMETER
);
1520 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
1523 if (acceptsock
->accept_recv_req
)
1525 release_object( acceptsock
);
1526 set_error( STATUS_INVALID_PARAMETER
);
1530 if (!(req
= alloc_accept_req( sock
, acceptsock
, async
, params
)))
1532 release_object( acceptsock
);
1535 list_add_tail( &sock
->accept_list
, &req
->entry
);
1536 acceptsock
->accept_recv_req
= req
;
1537 release_object( acceptsock
);
1539 acceptsock
->wparam
= params
->accept_handle
;
1540 async_set_completion_callback( async
, free_accept_req
, req
);
1541 queue_async( &sock
->accept_q
, async
);
1542 sock_reselect( sock
);
1543 set_error( STATUS_PENDING
);
1547 case IOCTL_AFD_LISTEN
:
1549 const struct afd_listen_params
*params
= get_req_data();
1551 if (get_req_data_size() < sizeof(*params
))
1553 set_error( STATUS_INVALID_PARAMETER
);
1557 if (listen( unix_fd
, params
->backlog
) < 0)
1559 set_error( sock_get_ntstatus( errno
) );
1563 sock
->pending_events
&= ~FD_ACCEPT
;
1564 sock
->reported_events
&= ~FD_ACCEPT
;
1565 sock
->state
|= FD_WINE_LISTENING
;
1566 sock
->state
&= ~(FD_CONNECT
| FD_WINE_CONNECTED
);
1568 /* we may already be selecting for FD_ACCEPT */
1569 sock_reselect( sock
);
1573 case IOCTL_AFD_WINE_CONNECT
:
1575 const struct afd_connect_params
*params
= get_req_data();
1576 const struct sockaddr
*addr
;
1577 struct connect_req
*req
;
1580 if (get_req_data_size() < sizeof(*params
) ||
1581 get_req_data_size() - sizeof(*params
) < params
->addr_len
)
1583 set_error( STATUS_BUFFER_TOO_SMALL
);
1586 send_len
= get_req_data_size() - sizeof(*params
) - params
->addr_len
;
1587 addr
= (const struct sockaddr
*)(params
+ 1);
1589 if (sock
->accept_recv_req
)
1591 set_error( STATUS_INVALID_PARAMETER
);
1595 if (sock
->connect_req
)
1597 set_error( params
->synchronous
? STATUS_INVALID_PARAMETER
: STATUS_CONNECTION_ACTIVE
);
1601 ret
= connect( unix_fd
, addr
, params
->addr_len
);
1602 if (ret
< 0 && errno
!= EINPROGRESS
)
1604 set_error( sock_get_ntstatus( errno
) );
1608 sock
->pending_events
&= ~(FD_CONNECT
| FD_READ
| FD_WRITE
);
1609 sock
->reported_events
&= ~(FD_CONNECT
| FD_READ
| FD_WRITE
);
1613 sock
->state
|= FD_WINE_CONNECTED
| FD_READ
| FD_WRITE
;
1614 sock
->state
&= ~FD_CONNECT
;
1616 if (!send_len
) return 1;
1619 if (!(req
= mem_alloc( sizeof(*req
) )))
1622 sock
->state
|= FD_CONNECT
;
1624 if (params
->synchronous
&& (sock
->state
& FD_WINE_NONBLOCKING
))
1626 sock_reselect( sock
);
1627 set_error( STATUS_DEVICE_NOT_READY
);
1631 req
->async
= (struct async
*)grab_object( async
);
1632 req
->iosb
= async_get_iosb( async
);
1633 req
->sock
= (struct sock
*)grab_object( sock
);
1634 req
->addr_len
= params
->addr_len
;
1635 req
->send_len
= send_len
;
1636 req
->send_cursor
= 0;
1638 async_set_completion_callback( async
, free_connect_req
, req
);
1639 sock
->connect_req
= req
;
1640 queue_async( &sock
->connect_q
, async
);
1641 sock_reselect( sock
);
1642 set_error( STATUS_PENDING
);
1646 case IOCTL_AFD_WINE_SHUTDOWN
:
1650 if (get_req_data_size() < sizeof(int))
1652 set_error( STATUS_BUFFER_TOO_SMALL
);
1655 how
= *(int *)get_req_data();
1659 set_error( STATUS_INVALID_PARAMETER
);
1663 if (sock
->type
== WS_SOCK_STREAM
&& !(sock
->state
& FD_WINE_CONNECTED
))
1665 set_error( STATUS_INVALID_CONNECTION
);
1671 sock
->state
&= ~FD_READ
;
1673 if (how
!= SD_RECEIVE
)
1675 sock
->state
&= ~FD_WRITE
;
1676 if (list_empty( &sock
->write_q
.queue
))
1677 shutdown( unix_fd
, SHUT_WR
);
1679 sock
->wr_shutdown_pending
= 1;
1684 if (sock
->event
) release_object( sock
->event
);
1688 sock
->state
|= FD_WINE_NONBLOCKING
;
1691 sock_reselect( sock
);
1695 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE
:
1696 if ((sock
->state
& FD_WINE_NONBLOCKING
) && async_is_blocking( async
))
1698 set_error( STATUS_DEVICE_NOT_READY
);
1701 if (!sock_get_ifchange( sock
)) return 0;
1702 queue_async( &sock
->ifchange_q
, async
);
1703 set_error( STATUS_PENDING
);
1707 set_error( STATUS_NOT_SUPPORTED
);
1712 #ifdef HAVE_LINUX_RTNETLINK_H
1714 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
1715 static struct object
*ifchange_object
;
1717 static void ifchange_dump( struct object
*obj
, int verbose
);
1718 static struct fd
*ifchange_get_fd( struct object
*obj
);
1719 static void ifchange_destroy( struct object
*obj
);
1721 static int ifchange_get_poll_events( struct fd
*fd
);
1722 static void ifchange_poll_event( struct fd
*fd
, int event
);
1726 struct object obj
; /* object header */
1727 struct fd
*fd
; /* interface change file descriptor */
1728 struct list sockets
; /* list of sockets to send interface change notifications */
1731 static const struct object_ops ifchange_ops
=
1733 sizeof(struct ifchange
), /* size */
1734 &no_type
, /* type */
1735 ifchange_dump
, /* dump */
1736 no_add_queue
, /* add_queue */
1737 NULL
, /* remove_queue */
1738 NULL
, /* signaled */
1739 no_satisfied
, /* satisfied */
1740 no_signal
, /* signal */
1741 ifchange_get_fd
, /* get_fd */
1742 default_map_access
, /* map_access */
1743 default_get_sd
, /* get_sd */
1744 default_set_sd
, /* set_sd */
1745 no_get_full_name
, /* get_full_name */
1746 no_lookup_name
, /* lookup_name */
1747 no_link_name
, /* link_name */
1748 NULL
, /* unlink_name */
1749 no_open_file
, /* open_file */
1750 no_kernel_obj_list
, /* get_kernel_obj_list */
1751 no_close_handle
, /* close_handle */
1752 ifchange_destroy
/* destroy */
1755 static const struct fd_ops ifchange_fd_ops
=
1757 ifchange_get_poll_events
, /* get_poll_events */
1758 ifchange_poll_event
, /* poll_event */
1759 NULL
, /* get_fd_type */
1760 no_fd_read
, /* read */
1761 no_fd_write
, /* write */
1762 no_fd_flush
, /* flush */
1763 no_fd_get_file_info
, /* get_file_info */
1764 no_fd_get_volume_info
, /* get_volume_info */
1765 no_fd_ioctl
, /* ioctl */
1766 NULL
, /* queue_async */
1767 NULL
/* reselect_async */
1770 static void ifchange_dump( struct object
*obj
, int verbose
)
1772 assert( obj
->ops
== &ifchange_ops
);
1773 fprintf( stderr
, "Interface change\n" );
1776 static struct fd
*ifchange_get_fd( struct object
*obj
)
1778 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1779 return (struct fd
*)grab_object( ifchange
->fd
);
1782 static void ifchange_destroy( struct object
*obj
)
1784 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1785 assert( obj
->ops
== &ifchange_ops
);
1787 release_object( ifchange
->fd
);
1789 /* reset the global ifchange object so that it will be recreated if it is needed again */
1790 assert( obj
== ifchange_object
);
1791 ifchange_object
= NULL
;
1794 static int ifchange_get_poll_events( struct fd
*fd
)
1799 /* wake up all the sockets waiting for a change notification event */
1800 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
1802 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1803 struct list
*ptr
, *next
;
1804 assert( obj
->ops
== &ifchange_ops
);
1805 assert( obj
== ifchange_object
);
1807 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
1809 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
1811 assert( sock
->ifchange_obj
);
1812 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
1813 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
1817 static void ifchange_poll_event( struct fd
*fd
, int event
)
1819 struct object
*ifchange
= get_fd_user( fd
);
1820 unsigned int status
= STATUS_PENDING
;
1821 char buffer
[PIPE_BUF
];
1824 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
1827 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
1828 return; /* retry when poll() says the socket is ready */
1829 status
= sock_get_ntstatus( errno
);
1833 struct nlmsghdr
*nlh
;
1835 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
1837 if (nlh
->nlmsg_type
== NLMSG_DONE
)
1839 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
1840 status
= STATUS_SUCCESS
;
1843 else status
= STATUS_CANCELLED
;
1845 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
1850 /* we only need one of these interface notification objects, all of the sockets dependent upon
1851 * it will wake up when a notification event occurs */
1852 static struct object
*get_ifchange( void )
1854 #ifdef HAVE_LINUX_RTNETLINK_H
1855 struct ifchange
*ifchange
;
1856 struct sockaddr_nl addr
;
1859 if (ifchange_object
)
1861 /* increment the refcount for each socket that uses the ifchange object */
1862 return grab_object( ifchange_object
);
1865 /* create the socket we need for processing interface change notifications */
1866 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1869 set_error( sock_get_ntstatus( errno
));
1872 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1873 memset( &addr
, 0, sizeof(addr
) );
1874 addr
.nl_family
= AF_NETLINK
;
1875 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
1876 /* bind the socket to the special netlink kernel interface */
1877 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
1880 set_error( sock_get_ntstatus( errno
));
1883 if (!(ifchange
= alloc_object( &ifchange_ops
)))
1886 set_error( STATUS_NO_MEMORY
);
1889 list_init( &ifchange
->sockets
);
1890 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
1892 release_object( ifchange
);
1893 set_error( STATUS_NO_MEMORY
);
1896 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
1898 /* the ifchange object is now successfully configured */
1899 ifchange_object
= &ifchange
->obj
;
1900 return &ifchange
->obj
;
1902 set_error( STATUS_NOT_SUPPORTED
);
1907 /* add the socket to the interface change notification list */
1908 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
1910 #ifdef HAVE_LINUX_RTNETLINK_H
1911 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1913 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
1917 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1918 static struct object
*sock_get_ifchange( struct sock
*sock
)
1920 struct object
*ifchange
;
1922 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
1923 return sock
->ifchange_obj
;
1925 if (!(ifchange
= get_ifchange()))
1928 /* add the socket to the ifchange notification list */
1929 ifchange_add_sock( ifchange
, sock
);
1930 sock
->ifchange_obj
= ifchange
;
1934 /* destroy an existing ifchange queue for a specific socket */
1935 static void sock_release_ifchange( struct sock
*sock
)
1937 if (sock
->ifchange_obj
)
1939 list_remove( &sock
->ifchange_entry
);
1940 release_object( sock
->ifchange_obj
);
1941 sock
->ifchange_obj
= NULL
;
1945 static void socket_device_dump( struct object
*obj
, int verbose
);
1946 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
1947 unsigned int attr
, struct object
*root
);
1948 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
1949 unsigned int sharing
, unsigned int options
);
1951 static const struct object_ops socket_device_ops
=
1953 sizeof(struct object
), /* size */
1954 &device_type
, /* type */
1955 socket_device_dump
, /* dump */
1956 no_add_queue
, /* add_queue */
1957 NULL
, /* remove_queue */
1958 NULL
, /* signaled */
1959 no_satisfied
, /* satisfied */
1960 no_signal
, /* signal */
1961 no_get_fd
, /* get_fd */
1962 default_map_access
, /* map_access */
1963 default_get_sd
, /* get_sd */
1964 default_set_sd
, /* set_sd */
1965 default_get_full_name
, /* get_full_name */
1966 socket_device_lookup_name
, /* lookup_name */
1967 directory_link_name
, /* link_name */
1968 default_unlink_name
, /* unlink_name */
1969 socket_device_open_file
, /* open_file */
1970 no_kernel_obj_list
, /* get_kernel_obj_list */
1971 no_close_handle
, /* close_handle */
1972 no_destroy
/* destroy */
1975 static void socket_device_dump( struct object
*obj
, int verbose
)
1977 fputs( "Socket device\n", stderr
);
1980 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
1981 unsigned int attr
, struct object
*root
)
1986 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
1987 unsigned int sharing
, unsigned int options
)
1991 if (!(sock
= create_socket())) return NULL
;
1992 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
1994 release_object( sock
);
2000 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
2001 unsigned int attr
, const struct security_descriptor
*sd
)
2003 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
2006 /* set socket event parameters */
2007 DECL_HANDLER(set_socket_event
)
2010 struct event
*old_event
;
2012 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
2013 FILE_WRITE_ATTRIBUTES
, &sock_ops
))) return;
2014 if (get_unix_fd( sock
->fd
) == -1) return;
2015 old_event
= sock
->event
;
2016 sock
->mask
= req
->mask
;
2019 sock
->pending_events
&= ~req
->mask
;
2020 sock
->reported_events
&= ~req
->mask
;
2023 sock
->window
= req
->window
;
2024 sock
->message
= req
->msg
;
2025 sock
->wparam
= req
->handle
; /* wparam is the socket handle */
2026 if (req
->event
) sock
->event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
2028 if (debug_level
&& sock
->event
) fprintf(stderr
, "event ptr: %p\n", sock
->event
);
2030 sock_reselect( sock
);
2032 sock
->state
|= FD_WINE_NONBLOCKING
;
2034 /* if a network event is pending, signal the event object
2035 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
2036 before a WSAEventSelect() was done on it.
2037 (when dealing with Asynchronous socket) */
2038 sock_wake_up( sock
);
2040 if (old_event
) release_object( old_event
); /* we're through with it */
2041 release_object( &sock
->obj
);
2044 /* get socket event parameters */
2045 DECL_HANDLER(get_socket_event
)
2049 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
2050 FILE_READ_ATTRIBUTES
, &sock_ops
))) return;
2051 if (get_unix_fd( sock
->fd
) == -1) return;
2052 reply
->mask
= sock
->mask
;
2053 reply
->pmask
= sock
->pending_events
;
2054 reply
->state
= sock
->state
;
2055 set_reply_data( sock
->errors
, min( get_reply_max_size(), sizeof(sock
->errors
) ));
2061 struct event
*cevent
= get_event_obj( current
->process
, req
->c_event
,
2062 EVENT_MODIFY_STATE
);
2065 reset_event( cevent
);
2066 release_object( cevent
);
2069 sock
->pending_events
= 0;
2070 sock_reselect( sock
);
2072 release_object( &sock
->obj
);
2075 /* re-enable pending socket events */
2076 DECL_HANDLER(enable_socket_event
)
2080 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
2081 FILE_WRITE_ATTRIBUTES
, &sock_ops
)))
2084 if (get_unix_fd( sock
->fd
) == -1) return;
2086 /* for event-based notification, windows erases stale events */
2087 sock
->pending_events
&= ~req
->mask
;
2089 sock
->reported_events
&= ~req
->mask
;
2090 sock
->state
|= req
->sstate
;
2091 sock
->state
&= ~req
->cstate
;
2092 if (sock
->type
!= WS_SOCK_STREAM
) sock
->state
&= ~STREAM_FLAG_MASK
;
2094 sock_reselect( sock
);
2096 release_object( &sock
->obj
);
2099 DECL_HANDLER(set_socket_deferred
)
2101 struct sock
*sock
, *acceptsock
;
2103 sock
=(struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
, &sock_ops
);
2107 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, req
->deferred
, 0, &sock_ops
);
2110 release_object( sock
);
2113 sock
->deferred
= acceptsock
;
2114 release_object( sock
);
2117 DECL_HANDLER(get_socket_info
)
2121 sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
, &sock_ops
);
2124 if (get_unix_fd( sock
->fd
) == -1) return;
2126 reply
->family
= sock
->family
;
2127 reply
->type
= sock
->type
;
2128 reply
->protocol
= sock
->proto
;
2130 release_object( &sock
->obj
);