msvcrt: Import asinh implementation from musl.
[wine.git] / server / sock.c
blob3925018c7a5487229aac55bbafbbb0d2b0ff498b
1 /*
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?
24 #include "config.h"
26 #include <assert.h>
27 #include <fcntl.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #ifdef HAVE_NETINET_IN_H
34 # include <netinet/in.h>
35 #endif
36 #ifdef HAVE_POLL_H
37 # include <poll.h>
38 #endif
39 #include <sys/time.h>
40 #include <sys/types.h>
41 #ifdef HAVE_SYS_SOCKET_H
42 # include <sys/socket.h>
43 #endif
44 #ifdef HAVE_SYS_IOCTL_H
45 #include <sys/ioctl.h>
46 #endif
47 #ifdef HAVE_SYS_FILIO_H
48 # include <sys/filio.h>
49 #endif
50 #include <time.h>
51 #include <unistd.h>
52 #include <limits.h>
53 #ifdef HAVE_LINUX_RTNETLINK_H
54 # include <linux/rtnetlink.h>
55 #endif
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>
62 # endif
63 # ifdef HAVE_LINUX_TYPES_H
64 # include <linux/types.h>
65 # endif
66 # include <linux/ipx.h>
67 #endif
68 #if defined(SOL_IPX) || defined(SO_DEFAULT_HEADERS)
69 # define HAS_IPX
70 #endif
72 #ifdef HAVE_LINUX_IRDA_H
73 # ifdef HAVE_LINUX_TYPES_H
74 # include <linux/types.h>
75 # endif
76 # include <linux/irda.h>
77 # define HAS_IRDA
78 #endif
80 #include "ntstatus.h"
81 #define WIN32_NO_STATUS
82 #include "windef.h"
83 #include "winternl.h"
84 #include "winerror.h"
85 #define USE_WS_PREFIX
86 #include "winsock2.h"
87 #include "ws2tcpip.h"
88 #include "wsipx.h"
89 #include "af_irda.h"
90 #include "wine/afd.h"
92 #include "process.h"
93 #include "file.h"
94 #include "handle.h"
95 #include "thread.h"
96 #include "request.h"
97 #include "user.h"
99 static struct list poll_list = LIST_INIT( poll_list );
101 struct poll_req
103 struct list entry;
104 struct async *async;
105 struct iosb *iosb;
106 struct timeout_user *timeout;
107 unsigned int count;
108 struct poll_socket_output *output;
109 struct
111 struct sock *sock;
112 int flags;
113 } sockets[1];
116 struct accept_req
118 struct list entry;
119 struct async *async;
120 struct iosb *iosb;
121 struct sock *sock, *acceptsock;
122 int accepted;
123 unsigned int recv_len, local_len;
126 struct connect_req
128 struct async *async;
129 struct iosb *iosb;
130 struct sock *sock;
131 unsigned int addr_len, send_len, send_cursor;
134 struct sock
136 struct object obj; /* object header */
137 struct fd *fd; /* socket file descriptor */
138 unsigned int state; /* status bits */
139 unsigned int mask; /* event mask */
140 /* pending FD_* events which have not yet been reported to the application */
141 unsigned int pending_events;
142 /* FD_* events which have already been reported and should not be selected
143 * for again until reset by a relevant call.
145 * For example, if FD_READ is set here and not in pending_events, it has
146 * already been reported and consumed, and we should not report it again,
147 * even if POLLIN is signaled, until it is reset by e.g recv().
149 * If an event has been signaled and not consumed yet, it will be set in
150 * both pending_events and reported_events (as we should only ever report
151 * any event once until it is reset.) */
152 unsigned int reported_events;
153 unsigned int flags; /* socket flags */
154 int polling; /* is socket being polled? */
155 int wr_shutdown_pending; /* is a write shutdown pending? */
156 unsigned short proto; /* socket protocol */
157 unsigned short type; /* socket type */
158 unsigned short family; /* socket family */
159 struct event *event; /* event object */
160 user_handle_t window; /* window to send the message to */
161 unsigned int message; /* message to send */
162 obj_handle_t wparam; /* message wparam (socket handle) */
163 unsigned int errors[FD_MAX_EVENTS]; /* event errors */
164 timeout_t connect_time;/* time the socket was connected */
165 struct sock *deferred; /* socket that waits for a deferred accept */
166 struct async_queue read_q; /* queue for asynchronous reads */
167 struct async_queue write_q; /* queue for asynchronous writes */
168 struct async_queue ifchange_q; /* queue for interface change notifications */
169 struct async_queue accept_q; /* queue for asynchronous accepts */
170 struct async_queue connect_q; /* queue for asynchronous connects */
171 struct async_queue poll_q; /* queue for asynchronous polls */
172 struct object *ifchange_obj; /* the interface change notification object */
173 struct list ifchange_entry; /* entry in ifchange notification list */
174 struct list accept_list; /* list of pending accept requests */
175 struct accept_req *accept_recv_req; /* pending accept-into request which will recv on this socket */
176 struct connect_req *connect_req; /* pending connection request */
179 static void sock_dump( struct object *obj, int verbose );
180 static struct fd *sock_get_fd( struct object *obj );
181 static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
182 static void sock_destroy( struct object *obj );
183 static struct object *sock_get_ifchange( struct sock *sock );
184 static void sock_release_ifchange( struct sock *sock );
186 static int sock_get_poll_events( struct fd *fd );
187 static void sock_poll_event( struct fd *fd, int event );
188 static enum server_fd_type sock_get_fd_type( struct fd *fd );
189 static int sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
190 static void sock_queue_async( struct fd *fd, struct async *async, int type, int count );
191 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
193 static int accept_into_socket( struct sock *sock, struct sock *acceptsock );
194 static struct sock *accept_socket( struct sock *sock );
195 static int sock_get_ntstatus( int err );
196 static unsigned int sock_get_error( int err );
198 static const struct object_ops sock_ops =
200 sizeof(struct sock), /* size */
201 &file_type, /* type */
202 sock_dump, /* dump */
203 add_queue, /* add_queue */
204 remove_queue, /* remove_queue */
205 default_fd_signaled, /* signaled */
206 no_satisfied, /* satisfied */
207 no_signal, /* signal */
208 sock_get_fd, /* get_fd */
209 default_map_access, /* map_access */
210 default_get_sd, /* get_sd */
211 default_set_sd, /* set_sd */
212 no_get_full_name, /* get_full_name */
213 no_lookup_name, /* lookup_name */
214 no_link_name, /* link_name */
215 NULL, /* unlink_name */
216 no_open_file, /* open_file */
217 no_kernel_obj_list, /* get_kernel_obj_list */
218 sock_close_handle, /* close_handle */
219 sock_destroy /* destroy */
222 static const struct fd_ops sock_fd_ops =
224 sock_get_poll_events, /* get_poll_events */
225 sock_poll_event, /* poll_event */
226 sock_get_fd_type, /* get_fd_type */
227 no_fd_read, /* read */
228 no_fd_write, /* write */
229 no_fd_flush, /* flush */
230 default_fd_get_file_info, /* get_file_info */
231 no_fd_get_volume_info, /* get_volume_info */
232 sock_ioctl, /* ioctl */
233 sock_queue_async, /* queue_async */
234 sock_reselect_async /* reselect_async */
237 union unix_sockaddr
239 struct sockaddr addr;
240 struct sockaddr_in in;
241 struct sockaddr_in6 in6;
242 #ifdef HAS_IPX
243 struct sockaddr_ipx ipx;
244 #endif
245 #ifdef HAS_IRDA
246 struct sockaddr_irda irda;
247 #endif
250 static int sockaddr_from_unix( const union unix_sockaddr *uaddr, struct WS_sockaddr *wsaddr, socklen_t wsaddrlen )
252 memset( wsaddr, 0, wsaddrlen );
254 switch (uaddr->addr.sa_family)
256 case AF_INET:
258 struct WS_sockaddr_in win = {0};
260 if (wsaddrlen < sizeof(win)) return -1;
261 win.sin_family = WS_AF_INET;
262 win.sin_port = uaddr->in.sin_port;
263 memcpy( &win.sin_addr, &uaddr->in.sin_addr, sizeof(win.sin_addr) );
264 memcpy( wsaddr, &win, sizeof(win) );
265 return sizeof(win);
268 case AF_INET6:
270 struct WS_sockaddr_in6 win = {0};
272 if (wsaddrlen < sizeof(struct WS_sockaddr_in6_old)) return -1;
273 win.sin6_family = WS_AF_INET6;
274 win.sin6_port = uaddr->in6.sin6_port;
275 win.sin6_flowinfo = uaddr->in6.sin6_flowinfo;
276 memcpy( &win.sin6_addr, &uaddr->in6.sin6_addr, sizeof(win.sin6_addr) );
277 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
278 win.sin6_scope_id = uaddr->in6.sin6_scope_id;
279 #endif
280 if (wsaddrlen >= sizeof(struct WS_sockaddr_in6))
282 memcpy( wsaddr, &win, sizeof(struct WS_sockaddr_in6) );
283 return sizeof(struct WS_sockaddr_in6);
285 memcpy( wsaddr, &win, sizeof(struct WS_sockaddr_in6_old) );
286 return sizeof(struct WS_sockaddr_in6_old);
289 #ifdef HAS_IPX
290 case AF_IPX:
292 struct WS_sockaddr_ipx win = {0};
294 if (wsaddrlen < sizeof(win)) return -1;
295 win.sa_family = WS_AF_IPX;
296 memcpy( win.sa_netnum, &uaddr->ipx.sipx_network, sizeof(win.sa_netnum) );
297 memcpy( win.sa_nodenum, &uaddr->ipx.sipx_node, sizeof(win.sa_nodenum) );
298 win.sa_socket = uaddr->ipx.sipx_port;
299 memcpy( wsaddr, &win, sizeof(win) );
300 return sizeof(win);
302 #endif
304 #ifdef HAS_IRDA
305 case AF_IRDA:
307 SOCKADDR_IRDA win;
309 if (wsaddrlen < sizeof(win)) return -1;
310 win.irdaAddressFamily = WS_AF_IRDA;
311 memcpy( win.irdaDeviceID, &uaddr->irda.sir_addr, sizeof(win.irdaDeviceID) );
312 if (uaddr->irda.sir_lsap_sel != LSAP_ANY)
313 snprintf( win.irdaServiceName, sizeof(win.irdaServiceName), "LSAP-SEL%u", uaddr->irda.sir_lsap_sel );
314 else
315 memcpy( win.irdaServiceName, uaddr->irda.sir_name, sizeof(win.irdaServiceName) );
316 memcpy( wsaddr, &win, sizeof(win) );
317 return sizeof(win);
319 #endif
321 case AF_UNSPEC:
322 return 0;
324 default:
325 return -1;
330 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
331 * we post messages if there are multiple events. Used to send
332 * messages. The problem is if there is both a FD_CONNECT event and,
333 * say, an FD_READ event available on the same socket, we want to
334 * notify the app of the connect event first. Otherwise it may
335 * discard the read event because it thinks it hasn't connected yet.
337 static const int event_bitorder[FD_MAX_EVENTS] =
339 FD_CONNECT_BIT,
340 FD_ACCEPT_BIT,
341 FD_OOB_BIT,
342 FD_WRITE_BIT,
343 FD_READ_BIT,
344 FD_CLOSE_BIT,
345 6, 7, 8, 9 /* leftovers */
348 /* Flags that make sense only for SOCK_STREAM sockets */
349 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
351 typedef enum {
352 SOCK_SHUTDOWN_ERROR = -1,
353 SOCK_SHUTDOWN_EOF = 0,
354 SOCK_SHUTDOWN_POLLHUP = 1
355 } sock_shutdown_t;
357 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
359 static sock_shutdown_t sock_check_pollhup(void)
361 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
362 int fd[2], n;
363 struct pollfd pfd;
364 char dummy;
366 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
367 if ( shutdown( fd[0], 1 ) ) goto out;
369 pfd.fd = fd[1];
370 pfd.events = POLLIN;
371 pfd.revents = 0;
373 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
374 n = poll( &pfd, 1, 1 );
375 if ( n != 1 ) goto out; /* error or timeout */
376 if ( pfd.revents & POLLHUP )
377 ret = SOCK_SHUTDOWN_POLLHUP;
378 else if ( pfd.revents & POLLIN &&
379 read( fd[1], &dummy, 1 ) == 0 )
380 ret = SOCK_SHUTDOWN_EOF;
382 out:
383 close( fd[0] );
384 close( fd[1] );
385 return ret;
388 void sock_init(void)
390 sock_shutdown_type = sock_check_pollhup();
392 switch ( sock_shutdown_type )
394 case SOCK_SHUTDOWN_EOF:
395 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
396 break;
397 case SOCK_SHUTDOWN_POLLHUP:
398 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
399 break;
400 default:
401 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
402 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
406 static int sock_reselect( struct sock *sock )
408 int ev = sock_get_poll_events( sock->fd );
410 if (debug_level)
411 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
413 if (!sock->polling) /* FIXME: should find a better way to do this */
415 /* previously unconnected socket, is this reselect supposed to connect it? */
416 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
417 /* ok, it is, attach it to the wineserver's main poll loop */
418 sock->polling = 1;
419 allow_fd_caching( sock->fd );
421 /* update condition mask */
422 set_fd_events( sock->fd, ev );
423 return ev;
426 /* wake anybody waiting on the socket event or send the associated message */
427 static void sock_wake_up( struct sock *sock )
429 unsigned int events = sock->pending_events & sock->mask;
430 int i;
432 if (sock->event)
434 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
435 if (events)
436 set_event( sock->event );
438 if (sock->window)
440 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
441 for (i = 0; i < FD_MAX_EVENTS; i++)
443 int event = event_bitorder[i];
444 if (events & (1 << event))
446 lparam_t lparam = (1 << event) | (sock->errors[event] << 16);
447 post_message( sock->window, sock->message, sock->wparam, lparam );
450 sock->pending_events = 0;
451 sock_reselect( sock );
455 static inline int sock_error( struct fd *fd )
457 unsigned int optval = 0;
458 socklen_t optlen = sizeof(optval);
460 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
461 return optval;
464 static void free_accept_req( void *private )
466 struct accept_req *req = private;
467 list_remove( &req->entry );
468 if (req->acceptsock)
470 req->acceptsock->accept_recv_req = NULL;
471 release_object( req->acceptsock );
473 release_object( req->async );
474 release_object( req->iosb );
475 release_object( req->sock );
476 free( req );
479 static void fill_accept_output( struct accept_req *req )
481 struct iosb *iosb = req->iosb;
482 union unix_sockaddr unix_addr;
483 struct WS_sockaddr *win_addr;
484 unsigned int remote_len;
485 socklen_t unix_len;
486 int fd, size = 0;
487 char *out_data;
488 int win_len;
490 if (!(out_data = mem_alloc( iosb->out_size ))) return;
492 fd = get_unix_fd( req->acceptsock->fd );
494 if (req->recv_len && (size = recv( fd, out_data, req->recv_len, 0 )) < 0)
496 if (!req->accepted && errno == EWOULDBLOCK)
498 req->accepted = 1;
499 sock_reselect( req->acceptsock );
500 set_error( STATUS_PENDING );
501 return;
504 set_error( sock_get_ntstatus( errno ) );
505 free( out_data );
506 return;
509 if (req->local_len)
511 if (req->local_len < sizeof(int))
513 set_error( STATUS_BUFFER_TOO_SMALL );
514 free( out_data );
515 return;
518 unix_len = sizeof(unix_addr);
519 win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + sizeof(int));
520 if (getsockname( fd, &unix_addr.addr, &unix_len ) < 0 ||
521 (win_len = sockaddr_from_unix( &unix_addr, win_addr, req->local_len - sizeof(int) )) < 0)
523 set_error( sock_get_ntstatus( errno ) );
524 free( out_data );
525 return;
527 memcpy( out_data + req->recv_len, &win_len, sizeof(int) );
530 unix_len = sizeof(unix_addr);
531 win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + req->local_len + sizeof(int));
532 remote_len = iosb->out_size - req->recv_len - req->local_len;
533 if (getpeername( fd, &unix_addr.addr, &unix_len ) < 0 ||
534 (win_len = sockaddr_from_unix( &unix_addr, win_addr, remote_len - sizeof(int) )) < 0)
536 set_error( sock_get_ntstatus( errno ) );
537 free( out_data );
538 return;
540 memcpy( out_data + req->recv_len + req->local_len, &win_len, sizeof(int) );
542 iosb->status = STATUS_SUCCESS;
543 iosb->result = size;
544 iosb->out_data = out_data;
545 set_error( STATUS_ALERTED );
548 static void complete_async_accept( struct sock *sock, struct accept_req *req )
550 struct sock *acceptsock = req->acceptsock;
551 struct async *async = req->async;
553 if (debug_level) fprintf( stderr, "completing accept request for socket %p\n", sock );
555 if (acceptsock)
557 if (!accept_into_socket( sock, acceptsock )) return;
558 fill_accept_output( req );
560 else
562 struct iosb *iosb = req->iosb;
563 obj_handle_t handle;
565 if (!(acceptsock = accept_socket( sock ))) return;
566 handle = alloc_handle_no_access_check( async_get_thread( async )->process, &acceptsock->obj,
567 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
568 acceptsock->wparam = handle;
569 release_object( acceptsock );
570 if (!handle) return;
572 if (!(iosb->out_data = malloc( sizeof(handle) ))) return;
574 iosb->status = STATUS_SUCCESS;
575 iosb->out_size = sizeof(handle);
576 memcpy( iosb->out_data, &handle, sizeof(handle) );
577 set_error( STATUS_ALERTED );
581 static void complete_async_accept_recv( struct accept_req *req )
583 if (debug_level) fprintf( stderr, "completing accept recv request for socket %p\n", req->acceptsock );
585 assert( req->recv_len );
587 fill_accept_output( req );
590 static void free_connect_req( void *private )
592 struct connect_req *req = private;
594 req->sock->connect_req = NULL;
595 release_object( req->async );
596 release_object( req->iosb );
597 release_object( req->sock );
598 free( req );
601 static void complete_async_connect( struct sock *sock )
603 struct connect_req *req = sock->connect_req;
604 const char *in_buffer;
605 struct iosb *iosb;
606 size_t len;
607 int ret;
609 if (debug_level) fprintf( stderr, "completing connect request for socket %p\n", sock );
611 sock->pending_events &= ~(FD_CONNECT | FD_READ | FD_WRITE);
612 sock->reported_events &= ~(FD_CONNECT | FD_READ | FD_WRITE);
613 sock->state |= FD_WINE_CONNECTED;
614 sock->state &= ~(FD_CONNECT | FD_WINE_LISTENING);
616 if (!req->send_len)
618 set_error( STATUS_SUCCESS );
619 return;
622 iosb = req->iosb;
623 in_buffer = (const char *)iosb->in_data + sizeof(struct afd_connect_params) + req->addr_len;
624 len = req->send_len - req->send_cursor;
626 ret = send( get_unix_fd( sock->fd ), in_buffer + req->send_cursor, len, 0 );
627 if (ret < 0 && errno != EWOULDBLOCK)
628 set_error( sock_get_ntstatus( errno ) );
629 else if (ret == len)
631 iosb->result = req->send_len;
632 iosb->status = STATUS_SUCCESS;
633 set_error( STATUS_ALERTED );
635 else
637 req->send_cursor += ret;
638 set_error( STATUS_PENDING );
642 static void free_poll_req( void *private )
644 struct poll_req *req = private;
645 unsigned int i;
647 if (req->timeout) remove_timeout_user( req->timeout );
649 for (i = 0; i < req->count; ++i)
650 release_object( req->sockets[i].sock );
651 release_object( req->async );
652 release_object( req->iosb );
653 list_remove( &req->entry );
654 free( req );
657 static int is_oobinline( struct sock *sock )
659 int oobinline;
660 socklen_t len = sizeof(oobinline);
661 return !getsockopt( get_unix_fd( sock->fd ), SOL_SOCKET, SO_OOBINLINE, (char *)&oobinline, &len ) && oobinline;
664 static int get_poll_flags( struct sock *sock, int event )
666 int flags = 0;
668 /* A connection-mode socket which has never been connected does not return
669 * write or hangup events, but Linux reports POLLOUT | POLLHUP. */
670 if (sock->type == WS_SOCK_STREAM && !(sock->state & (FD_CONNECT | FD_WINE_CONNECTED | FD_WINE_LISTENING)))
671 event &= ~(POLLOUT | POLLHUP);
673 if (event & POLLIN)
675 if (sock->state & FD_WINE_LISTENING)
676 flags |= AFD_POLL_ACCEPT;
677 else
678 flags |= AFD_POLL_READ;
680 if (event & POLLPRI)
681 flags |= is_oobinline( sock ) ? AFD_POLL_READ : AFD_POLL_OOB;
682 if (event & POLLOUT)
683 flags |= AFD_POLL_WRITE;
684 if (sock->state & FD_WINE_CONNECTED)
685 flags |= AFD_POLL_CONNECT;
686 if (event & POLLHUP)
687 flags |= AFD_POLL_HUP;
688 if (event & POLLERR)
689 flags |= AFD_POLL_CONNECT_ERR;
691 return flags;
694 static void complete_async_polls( struct sock *sock, int event, int error )
696 int flags = get_poll_flags( sock, event );
697 struct poll_req *req, *next;
699 LIST_FOR_EACH_ENTRY_SAFE( req, next, &poll_list, struct poll_req, entry )
701 struct iosb *iosb = req->iosb;
702 unsigned int i;
704 if (iosb->status != STATUS_PENDING) continue;
706 for (i = 0; i < req->count; ++i)
708 if (req->sockets[i].sock != sock) continue;
709 if (!(req->sockets[i].flags & flags)) continue;
711 if (debug_level)
712 fprintf( stderr, "completing poll for socket %p, wanted %#x got %#x\n",
713 sock, req->sockets[i].flags, flags );
715 req->output[i].flags = req->sockets[i].flags & flags;
716 req->output[i].status = sock_get_ntstatus( error );
718 iosb->status = STATUS_SUCCESS;
719 iosb->out_data = req->output;
720 iosb->out_size = req->count * sizeof(*req->output);
721 async_terminate( req->async, STATUS_ALERTED );
722 break;
727 static void async_poll_timeout( void *private )
729 struct poll_req *req = private;
730 struct iosb *iosb = req->iosb;
732 req->timeout = NULL;
734 if (iosb->status != STATUS_PENDING) return;
736 iosb->status = STATUS_TIMEOUT;
737 iosb->out_data = req->output;
738 iosb->out_size = req->count * sizeof(*req->output);
739 async_terminate( req->async, STATUS_ALERTED );
742 static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
744 if (event & (POLLIN | POLLPRI))
746 struct accept_req *req;
748 LIST_FOR_EACH_ENTRY( req, &sock->accept_list, struct accept_req, entry )
750 if (req->iosb->status == STATUS_PENDING && !req->accepted)
752 complete_async_accept( sock, req );
753 if (get_error() != STATUS_PENDING)
754 async_terminate( req->async, get_error() );
755 break;
759 if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
761 complete_async_accept_recv( sock->accept_recv_req );
762 if (get_error() != STATUS_PENDING)
763 async_terminate( sock->accept_recv_req->async, get_error() );
767 if ((event & POLLOUT) && sock->connect_req && sock->connect_req->iosb->status == STATUS_PENDING)
769 complete_async_connect( sock );
770 if (get_error() != STATUS_PENDING)
771 async_terminate( sock->connect_req->async, get_error() );
774 if (event & (POLLIN | POLLPRI) && async_waiting( &sock->read_q ))
776 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
777 async_wake_up( &sock->read_q, STATUS_ALERTED );
778 event &= ~(POLLIN | POLLPRI);
781 if (is_fd_overlapped( sock->fd ))
783 if (event & POLLOUT && async_waiting( &sock->write_q ))
785 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
786 async_wake_up( &sock->write_q, STATUS_ALERTED );
787 event &= ~POLLOUT;
791 if (event & (POLLERR | POLLHUP))
793 int status = sock_get_ntstatus( error );
794 struct accept_req *req, *next;
796 if (!(sock->state & FD_READ))
797 async_wake_up( &sock->read_q, status );
798 if (!(sock->state & FD_WRITE))
799 async_wake_up( &sock->write_q, status );
801 LIST_FOR_EACH_ENTRY_SAFE( req, next, &sock->accept_list, struct accept_req, entry )
803 if (req->iosb->status == STATUS_PENDING)
804 async_terminate( req->async, status );
807 if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
808 async_terminate( sock->accept_recv_req->async, status );
810 if (sock->connect_req)
811 async_terminate( sock->connect_req->async, status );
814 return event;
817 static void post_socket_event( struct sock *sock, unsigned int event_bit, unsigned int error )
819 unsigned int event = (1 << event_bit);
821 if (!(sock->reported_events & event))
823 sock->pending_events |= event;
824 sock->reported_events |= event;
825 sock->errors[event_bit] = error;
829 static void sock_dispatch_events( struct sock *sock, int prevstate, int event, int error )
831 if (prevstate & FD_CONNECT)
833 post_socket_event( sock, FD_CONNECT_BIT, sock_get_error( error ) );
834 goto end;
836 if (prevstate & FD_WINE_LISTENING)
838 post_socket_event( sock, FD_ACCEPT_BIT, sock_get_error( error ) );
839 goto end;
842 if (event & POLLIN)
843 post_socket_event( sock, FD_READ_BIT, 0 );
845 if (event & POLLOUT)
846 post_socket_event( sock, FD_WRITE_BIT, 0 );
848 if (event & POLLPRI)
849 post_socket_event( sock, FD_OOB_BIT, 0 );
851 if (event & (POLLERR|POLLHUP))
852 post_socket_event( sock, FD_CLOSE_BIT, sock_get_error( error ) );
854 end:
855 sock_wake_up( sock );
858 static void sock_poll_event( struct fd *fd, int event )
860 struct sock *sock = get_fd_user( fd );
861 int hangup_seen = 0;
862 int prevstate = sock->state;
863 int error = 0;
865 assert( sock->obj.ops == &sock_ops );
866 if (debug_level)
867 fprintf(stderr, "socket %p select event: %x\n", sock, event);
869 /* we may change event later, remove from loop here */
870 if (event & (POLLERR|POLLHUP)) set_fd_events( sock->fd, -1 );
872 if (sock->state & FD_CONNECT)
874 if (event & (POLLERR|POLLHUP))
876 /* we didn't get connected? */
877 sock->state &= ~FD_CONNECT;
878 event &= ~POLLOUT;
879 error = sock_error( fd );
881 else if (event & POLLOUT)
883 /* we got connected */
884 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
885 sock->state &= ~FD_CONNECT;
886 sock->connect_time = current_time;
889 else if (sock->state & FD_WINE_LISTENING)
891 /* listening */
892 if (event & (POLLERR|POLLHUP))
893 error = sock_error( fd );
895 else
897 /* normal data flow */
898 if (sock->type == WS_SOCK_STREAM && (event & POLLIN))
900 char dummy;
901 int nr;
903 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
904 * has been closed, so we need to check for it explicitly here */
905 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
906 if ( nr == 0 )
908 hangup_seen = 1;
909 event &= ~POLLIN;
911 else if ( nr < 0 )
913 event &= ~POLLIN;
914 /* EAGAIN can happen if an async recv() falls between the server's poll()
915 call and the invocation of this routine */
916 if ( errno != EAGAIN )
918 error = errno;
919 event |= POLLERR;
920 if ( debug_level )
921 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
926 if ( (hangup_seen || event & (POLLHUP|POLLERR)) && (sock->state & (FD_READ|FD_WRITE)) )
928 error = error ? error : sock_error( fd );
929 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
930 sock->state &= ~FD_WRITE;
931 sock->state &= ~FD_READ;
933 if (debug_level)
934 fprintf(stderr, "socket %p aborted by error %d, event: %x\n", sock, error, event);
937 if (hangup_seen)
938 event |= POLLHUP;
941 complete_async_polls( sock, event, error );
943 event = sock_dispatch_asyncs( sock, event, error );
944 sock_dispatch_events( sock, prevstate, event, error );
946 sock_reselect( sock );
949 static void sock_dump( struct object *obj, int verbose )
951 struct sock *sock = (struct sock *)obj;
952 assert( obj->ops == &sock_ops );
953 fprintf( stderr, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
954 sock->fd, sock->state,
955 sock->mask, sock->pending_events, sock->reported_events );
958 static int poll_flags_from_afd( struct sock *sock, int flags )
960 int ev = 0;
962 /* A connection-mode socket which has never been connected does
963 * not return write or hangup events, but Linux returns
964 * POLLOUT | POLLHUP. */
965 if (sock->type == WS_SOCK_STREAM && !(sock->state & (FD_CONNECT | FD_WINE_CONNECTED | FD_WINE_LISTENING)))
966 return -1;
968 if (flags & (AFD_POLL_READ | AFD_POLL_ACCEPT))
969 ev |= POLLIN;
970 if ((flags & AFD_POLL_HUP) && sock->type == WS_SOCK_STREAM)
971 ev |= POLLIN;
972 if (flags & AFD_POLL_OOB)
973 ev |= is_oobinline( sock ) ? POLLIN : POLLPRI;
974 if (flags & AFD_POLL_WRITE)
975 ev |= POLLOUT;
977 return ev;
980 static int sock_get_poll_events( struct fd *fd )
982 struct sock *sock = get_fd_user( fd );
983 unsigned int mask = sock->mask & ~sock->reported_events;
984 unsigned int smask = sock->state & mask;
985 struct poll_req *req;
986 int ev = 0;
988 assert( sock->obj.ops == &sock_ops );
990 if (sock->state & FD_CONNECT)
991 /* connecting, wait for writable */
992 return POLLOUT;
994 if (!list_empty( &sock->accept_list ) || sock->accept_recv_req )
996 ev |= POLLIN | POLLPRI;
998 else if (async_queued( &sock->read_q ))
1000 if (async_waiting( &sock->read_q )) ev |= POLLIN | POLLPRI;
1002 else if (smask & FD_READ || (sock->state & FD_WINE_LISTENING && mask & FD_ACCEPT))
1003 ev |= POLLIN | POLLPRI;
1004 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
1005 else if (sock->type == WS_SOCK_STREAM && (mask & FD_CLOSE) && !(sock->reported_events & FD_READ))
1006 ev |= POLLIN;
1008 if (async_queued( &sock->write_q ))
1010 if (async_waiting( &sock->write_q )) ev |= POLLOUT;
1012 else if (smask & FD_WRITE)
1013 ev |= POLLOUT;
1015 LIST_FOR_EACH_ENTRY( req, &poll_list, struct poll_req, entry )
1017 unsigned int i;
1019 for (i = 0; i < req->count; ++i)
1021 if (req->sockets[i].sock != sock) continue;
1023 ev |= poll_flags_from_afd( sock, req->sockets[i].flags );
1027 return ev;
1030 static enum server_fd_type sock_get_fd_type( struct fd *fd )
1032 return FD_TYPE_SOCKET;
1035 static void sock_queue_async( struct fd *fd, struct async *async, int type, int count )
1037 struct sock *sock = get_fd_user( fd );
1038 struct async_queue *queue;
1040 assert( sock->obj.ops == &sock_ops );
1042 switch (type)
1044 case ASYNC_TYPE_READ:
1045 queue = &sock->read_q;
1046 break;
1047 case ASYNC_TYPE_WRITE:
1048 queue = &sock->write_q;
1049 break;
1050 default:
1051 set_error( STATUS_INVALID_PARAMETER );
1052 return;
1055 if ( ( !( sock->state & (FD_READ|FD_CONNECT|FD_WINE_LISTENING) ) && type == ASYNC_TYPE_READ ) ||
1056 ( !( sock->state & (FD_WRITE|FD_CONNECT) ) && type == ASYNC_TYPE_WRITE ) )
1058 set_error( STATUS_PIPE_DISCONNECTED );
1059 return;
1062 queue_async( queue, async );
1063 sock_reselect( sock );
1065 set_error( STATUS_PENDING );
1068 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
1070 struct sock *sock = get_fd_user( fd );
1072 if (sock->wr_shutdown_pending && list_empty( &sock->write_q.queue ))
1073 shutdown( get_unix_fd( sock->fd ), SHUT_WR );
1075 /* Don't reselect the ifchange queue; we always ask for POLLIN.
1076 * Don't reselect an uninitialized socket; we can't call set_fd_events() on
1077 * a pseudo-fd. */
1078 if (queue != &sock->ifchange_q && sock->type)
1079 sock_reselect( sock );
1082 static struct fd *sock_get_fd( struct object *obj )
1084 struct sock *sock = (struct sock *)obj;
1085 return (struct fd *)grab_object( sock->fd );
1088 static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
1090 struct sock *sock = (struct sock *)obj;
1092 if (sock->obj.handle_count == 1) /* last handle */
1094 struct accept_req *accept_req, *accept_next;
1095 struct poll_req *poll_req, *poll_next;
1097 if (sock->accept_recv_req)
1098 async_terminate( sock->accept_recv_req->async, STATUS_CANCELLED );
1100 LIST_FOR_EACH_ENTRY_SAFE( accept_req, accept_next, &sock->accept_list, struct accept_req, entry )
1101 async_terminate( accept_req->async, STATUS_CANCELLED );
1103 if (sock->connect_req)
1104 async_terminate( sock->connect_req->async, STATUS_CANCELLED );
1106 LIST_FOR_EACH_ENTRY_SAFE( poll_req, poll_next, &poll_list, struct poll_req, entry )
1108 struct iosb *iosb = poll_req->iosb;
1109 unsigned int i;
1111 if (iosb->status != STATUS_PENDING) continue;
1113 for (i = 0; i < poll_req->count; ++i)
1115 if (poll_req->sockets[i].sock == sock)
1117 iosb->status = STATUS_SUCCESS;
1118 poll_req->output[i].flags = AFD_POLL_CLOSE;
1119 poll_req->output[i].status = 0;
1123 if (iosb->status != STATUS_PENDING)
1125 iosb->out_data = poll_req->output;
1126 iosb->out_size = poll_req->count * sizeof(*poll_req->output);
1127 async_terminate( poll_req->async, STATUS_ALERTED );
1132 return 1;
1135 static void sock_destroy( struct object *obj )
1137 struct sock *sock = (struct sock *)obj;
1139 assert( obj->ops == &sock_ops );
1141 /* FIXME: special socket shutdown stuff? */
1143 if ( sock->deferred )
1144 release_object( sock->deferred );
1146 async_wake_up( &sock->ifchange_q, STATUS_CANCELLED );
1147 sock_release_ifchange( sock );
1148 free_async_queue( &sock->read_q );
1149 free_async_queue( &sock->write_q );
1150 free_async_queue( &sock->ifchange_q );
1151 free_async_queue( &sock->accept_q );
1152 free_async_queue( &sock->connect_q );
1153 free_async_queue( &sock->poll_q );
1154 if (sock->event) release_object( sock->event );
1155 if (sock->fd)
1157 /* shut the socket down to force pending poll() calls in the client to return */
1158 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
1159 release_object( sock->fd );
1163 static struct sock *create_socket(void)
1165 struct sock *sock;
1167 if (!(sock = alloc_object( &sock_ops ))) return NULL;
1168 sock->fd = NULL;
1169 sock->state = 0;
1170 sock->mask = 0;
1171 sock->pending_events = 0;
1172 sock->reported_events = 0;
1173 sock->polling = 0;
1174 sock->wr_shutdown_pending = 0;
1175 sock->flags = 0;
1176 sock->proto = 0;
1177 sock->type = 0;
1178 sock->family = 0;
1179 sock->event = NULL;
1180 sock->window = 0;
1181 sock->message = 0;
1182 sock->wparam = 0;
1183 sock->connect_time = 0;
1184 sock->deferred = NULL;
1185 sock->ifchange_obj = NULL;
1186 sock->accept_recv_req = NULL;
1187 sock->connect_req = NULL;
1188 init_async_queue( &sock->read_q );
1189 init_async_queue( &sock->write_q );
1190 init_async_queue( &sock->ifchange_q );
1191 init_async_queue( &sock->accept_q );
1192 init_async_queue( &sock->connect_q );
1193 init_async_queue( &sock->poll_q );
1194 memset( sock->errors, 0, sizeof(sock->errors) );
1195 list_init( &sock->accept_list );
1196 return sock;
1199 static int get_unix_family( int family )
1201 switch (family)
1203 case WS_AF_INET: return AF_INET;
1204 case WS_AF_INET6: return AF_INET6;
1205 #ifdef HAS_IPX
1206 case WS_AF_IPX: return AF_IPX;
1207 #endif
1208 #ifdef AF_IRDA
1209 case WS_AF_IRDA: return AF_IRDA;
1210 #endif
1211 case WS_AF_UNSPEC: return AF_UNSPEC;
1212 default: return -1;
1216 static int get_unix_type( int type )
1218 switch (type)
1220 case WS_SOCK_DGRAM: return SOCK_DGRAM;
1221 case WS_SOCK_RAW: return SOCK_RAW;
1222 case WS_SOCK_STREAM: return SOCK_STREAM;
1223 default: return -1;
1227 static int get_unix_protocol( int protocol )
1229 if (protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
1230 return protocol;
1232 switch (protocol)
1234 case WS_IPPROTO_ICMP: return IPPROTO_ICMP;
1235 case WS_IPPROTO_IGMP: return IPPROTO_IGMP;
1236 case WS_IPPROTO_IP: return IPPROTO_IP;
1237 case WS_IPPROTO_IPV4: return IPPROTO_IPIP;
1238 case WS_IPPROTO_IPV6: return IPPROTO_IPV6;
1239 case WS_IPPROTO_RAW: return IPPROTO_RAW;
1240 case WS_IPPROTO_TCP: return IPPROTO_TCP;
1241 case WS_IPPROTO_UDP: return IPPROTO_UDP;
1242 default: return -1;
1246 static void set_dont_fragment( int fd, int level, int value )
1248 int optname;
1250 if (level == IPPROTO_IP)
1252 #ifdef IP_DONTFRAG
1253 optname = IP_DONTFRAG;
1254 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1255 optname = IP_MTU_DISCOVER;
1256 value = value ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
1257 #else
1258 return;
1259 #endif
1261 else
1263 #ifdef IPV6_DONTFRAG
1264 optname = IPV6_DONTFRAG;
1265 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1266 optname = IPV6_MTU_DISCOVER;
1267 value = value ? IPV6_PMTUDISC_DO : IPV6_PMTUDISC_DONT;
1268 #else
1269 return;
1270 #endif
1273 setsockopt( fd, level, optname, &value, sizeof(value) );
1276 static int init_socket( struct sock *sock, int family, int type, int protocol, unsigned int flags )
1278 unsigned int options = 0;
1279 int sockfd, unix_type, unix_family, unix_protocol;
1281 unix_family = get_unix_family( family );
1282 unix_type = get_unix_type( type );
1283 unix_protocol = get_unix_protocol( protocol );
1285 if (unix_protocol < 0)
1287 if (type && unix_type < 0)
1288 set_win32_error( WSAESOCKTNOSUPPORT );
1289 else
1290 set_win32_error( WSAEPROTONOSUPPORT );
1291 return -1;
1293 if (unix_family < 0)
1295 if (family >= 0 && unix_type < 0)
1296 set_win32_error( WSAESOCKTNOSUPPORT );
1297 else
1298 set_win32_error( WSAEAFNOSUPPORT );
1299 return -1;
1302 sockfd = socket( unix_family, unix_type, unix_protocol );
1303 if (sockfd == -1)
1305 if (errno == EINVAL) set_win32_error( WSAESOCKTNOSUPPORT );
1306 else set_win32_error( sock_get_error( errno ));
1307 return -1;
1309 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
1311 if (family == WS_AF_IPX && protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
1313 #ifdef HAS_IPX
1314 int ipx_type = protocol - WS_NSPROTO_IPX;
1316 #ifdef SOL_IPX
1317 setsockopt( sockfd, SOL_IPX, IPX_TYPE, &ipx_type, sizeof(ipx_type) );
1318 #else
1319 struct ipx val;
1320 /* Should we retrieve val using a getsockopt call and then
1321 * set the modified one? */
1322 val.ipx_pt = ipx_type;
1323 setsockopt( sockfd, 0, SO_DEFAULT_HEADERS, &val, sizeof(val) );
1324 #endif
1325 #endif
1328 if (unix_family == AF_INET || unix_family == AF_INET6)
1330 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1331 if (unix_type == SOCK_DGRAM || unix_type == SOCK_RAW) /* in Linux the global default can be enabled */
1332 set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, FALSE );
1333 else if (unix_type == SOCK_STREAM)
1334 set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, TRUE );
1337 #ifdef IPV6_V6ONLY
1338 if (unix_family == AF_INET6)
1340 static const int enable = 1;
1341 setsockopt( sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &enable, sizeof(enable) );
1343 #endif
1345 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
1346 sock->flags = flags;
1347 sock->proto = protocol;
1348 sock->type = type;
1349 sock->family = family;
1351 if (sock->fd)
1353 options = get_fd_options( sock->fd );
1354 release_object( sock->fd );
1357 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj, options )))
1359 return -1;
1361 sock_reselect( sock );
1362 return 0;
1365 /* accepts a socket and inits it */
1366 static int accept_new_fd( struct sock *sock )
1369 /* Try to accept(2). We can't be safe that this an already connected socket
1370 * or that accept() is allowed on it. In those cases we will get -1/errno
1371 * return.
1373 struct sockaddr saddr;
1374 socklen_t slen = sizeof(saddr);
1375 int acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen );
1376 if (acceptfd != -1)
1377 fcntl( acceptfd, F_SETFL, O_NONBLOCK );
1378 else
1379 set_error( sock_get_ntstatus( errno ));
1380 return acceptfd;
1383 /* accept a socket (creates a new fd) */
1384 static struct sock *accept_socket( struct sock *sock )
1386 struct sock *acceptsock;
1387 int acceptfd;
1389 if (get_unix_fd( sock->fd ) == -1) return NULL;
1391 if ( sock->deferred )
1393 acceptsock = sock->deferred;
1394 sock->deferred = NULL;
1396 else
1398 if ((acceptfd = accept_new_fd( sock )) == -1) return NULL;
1399 if (!(acceptsock = create_socket()))
1401 close( acceptfd );
1402 return NULL;
1405 /* newly created socket gets the same properties of the listening socket */
1406 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
1407 if (sock->state & FD_WINE_NONBLOCKING)
1408 acceptsock->state |= FD_WINE_NONBLOCKING;
1409 acceptsock->mask = sock->mask;
1410 acceptsock->proto = sock->proto;
1411 acceptsock->type = sock->type;
1412 acceptsock->family = sock->family;
1413 acceptsock->window = sock->window;
1414 acceptsock->message = sock->message;
1415 acceptsock->connect_time = current_time;
1416 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
1417 acceptsock->flags = sock->flags;
1418 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
1419 get_fd_options( sock->fd ) )))
1421 release_object( acceptsock );
1422 return NULL;
1425 clear_error();
1426 sock->pending_events &= ~FD_ACCEPT;
1427 sock->reported_events &= ~FD_ACCEPT;
1428 sock_reselect( sock );
1429 return acceptsock;
1432 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
1434 int acceptfd;
1435 struct fd *newfd;
1437 if (get_unix_fd( sock->fd ) == -1) return FALSE;
1439 if ( sock->deferred )
1441 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
1442 get_fd_options( acceptsock->fd ) );
1443 if ( !newfd )
1444 return FALSE;
1446 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
1448 release_object( sock->deferred );
1449 sock->deferred = NULL;
1451 else
1453 if ((acceptfd = accept_new_fd( sock )) == -1)
1454 return FALSE;
1456 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
1457 get_fd_options( acceptsock->fd ) )))
1458 return FALSE;
1461 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
1462 acceptsock->pending_events = 0;
1463 acceptsock->reported_events = 0;
1464 acceptsock->polling = 0;
1465 acceptsock->proto = sock->proto;
1466 acceptsock->type = sock->type;
1467 acceptsock->family = sock->family;
1468 acceptsock->wparam = 0;
1469 acceptsock->deferred = NULL;
1470 acceptsock->connect_time = current_time;
1471 fd_copy_completion( acceptsock->fd, newfd );
1472 release_object( acceptsock->fd );
1473 acceptsock->fd = newfd;
1475 clear_error();
1476 sock->pending_events &= ~FD_ACCEPT;
1477 sock->reported_events &= ~FD_ACCEPT;
1478 sock_reselect( sock );
1480 return TRUE;
1483 /* return an errno value mapped to a WSA error */
1484 static unsigned int sock_get_error( int err )
1486 switch (err)
1488 case EINTR: return WSAEINTR;
1489 case EBADF: return WSAEBADF;
1490 case EPERM:
1491 case EACCES: return WSAEACCES;
1492 case EFAULT: return WSAEFAULT;
1493 case EINVAL: return WSAEINVAL;
1494 case EMFILE: return WSAEMFILE;
1495 case EINPROGRESS:
1496 case EWOULDBLOCK: return WSAEWOULDBLOCK;
1497 case EALREADY: return WSAEALREADY;
1498 case ENOTSOCK: return WSAENOTSOCK;
1499 case EDESTADDRREQ: return WSAEDESTADDRREQ;
1500 case EMSGSIZE: return WSAEMSGSIZE;
1501 case EPROTOTYPE: return WSAEPROTOTYPE;
1502 case ENOPROTOOPT: return WSAENOPROTOOPT;
1503 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
1504 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
1505 case EOPNOTSUPP: return WSAEOPNOTSUPP;
1506 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
1507 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
1508 case EADDRINUSE: return WSAEADDRINUSE;
1509 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
1510 case ENETDOWN: return WSAENETDOWN;
1511 case ENETUNREACH: return WSAENETUNREACH;
1512 case ENETRESET: return WSAENETRESET;
1513 case ECONNABORTED: return WSAECONNABORTED;
1514 case EPIPE:
1515 case ECONNRESET: return WSAECONNRESET;
1516 case ENOBUFS: return WSAENOBUFS;
1517 case EISCONN: return WSAEISCONN;
1518 case ENOTCONN: return WSAENOTCONN;
1519 case ESHUTDOWN: return WSAESHUTDOWN;
1520 case ETOOMANYREFS: return WSAETOOMANYREFS;
1521 case ETIMEDOUT: return WSAETIMEDOUT;
1522 case ECONNREFUSED: return WSAECONNREFUSED;
1523 case ELOOP: return WSAELOOP;
1524 case ENAMETOOLONG: return WSAENAMETOOLONG;
1525 case EHOSTDOWN: return WSAEHOSTDOWN;
1526 case EHOSTUNREACH: return WSAEHOSTUNREACH;
1527 case ENOTEMPTY: return WSAENOTEMPTY;
1528 #ifdef EPROCLIM
1529 case EPROCLIM: return WSAEPROCLIM;
1530 #endif
1531 #ifdef EUSERS
1532 case EUSERS: return WSAEUSERS;
1533 #endif
1534 #ifdef EDQUOT
1535 case EDQUOT: return WSAEDQUOT;
1536 #endif
1537 #ifdef ESTALE
1538 case ESTALE: return WSAESTALE;
1539 #endif
1540 #ifdef EREMOTE
1541 case EREMOTE: return WSAEREMOTE;
1542 #endif
1544 case 0: return 0;
1545 default:
1546 errno = err;
1547 perror("wineserver: sock_get_error() can't map error");
1548 return WSAEFAULT;
1552 static int sock_get_ntstatus( int err )
1554 switch ( err )
1556 case EBADF: return STATUS_INVALID_HANDLE;
1557 case EBUSY: return STATUS_DEVICE_BUSY;
1558 case EPERM:
1559 case EACCES: return STATUS_ACCESS_DENIED;
1560 case EFAULT: return STATUS_ACCESS_VIOLATION;
1561 case EINVAL: return STATUS_INVALID_PARAMETER;
1562 case ENFILE:
1563 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
1564 case EINPROGRESS:
1565 case EWOULDBLOCK: return STATUS_DEVICE_NOT_READY;
1566 case EALREADY: return STATUS_NETWORK_BUSY;
1567 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
1568 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
1569 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
1570 case EPROTONOSUPPORT:
1571 case ESOCKTNOSUPPORT:
1572 case EPFNOSUPPORT:
1573 case EAFNOSUPPORT:
1574 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
1575 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
1576 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
1577 case EADDRINUSE: return STATUS_SHARING_VIOLATION;
1578 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
1579 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
1580 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
1581 case ENOTCONN: return STATUS_INVALID_CONNECTION;
1582 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
1583 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
1584 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
1585 case ENETDOWN: return STATUS_NETWORK_BUSY;
1586 case EPIPE:
1587 case ECONNRESET: return STATUS_CONNECTION_RESET;
1588 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
1589 case EISCONN: return STATUS_CONNECTION_ACTIVE;
1591 case 0: return STATUS_SUCCESS;
1592 default:
1593 errno = err;
1594 perror("wineserver: sock_get_ntstatus() can't map error");
1595 return STATUS_UNSUCCESSFUL;
1599 static struct accept_req *alloc_accept_req( struct sock *sock, struct sock *acceptsock, struct async *async,
1600 const struct afd_accept_into_params *params )
1602 struct accept_req *req = mem_alloc( sizeof(*req) );
1604 if (req)
1606 req->async = (struct async *)grab_object( async );
1607 req->iosb = async_get_iosb( async );
1608 req->sock = (struct sock *)grab_object( sock );
1609 req->acceptsock = acceptsock;
1610 if (acceptsock) grab_object( acceptsock );
1611 req->accepted = 0;
1612 req->recv_len = 0;
1613 req->local_len = 0;
1614 if (params)
1616 req->recv_len = params->recv_len;
1617 req->local_len = params->local_len;
1620 return req;
1623 static int sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1625 struct sock *sock = get_fd_user( fd );
1626 int unix_fd;
1628 assert( sock->obj.ops == &sock_ops );
1630 if (code != IOCTL_AFD_WINE_CREATE && (unix_fd = get_unix_fd( fd )) < 0) return 0;
1632 switch(code)
1634 case IOCTL_AFD_WINE_CREATE:
1636 const struct afd_create_params *params = get_req_data();
1638 if (get_req_data_size() != sizeof(*params))
1640 set_error( STATUS_INVALID_PARAMETER );
1641 return 0;
1643 init_socket( sock, params->family, params->type, params->protocol, params->flags );
1644 return 0;
1647 case IOCTL_AFD_WINE_ACCEPT:
1649 struct sock *acceptsock;
1650 obj_handle_t handle;
1652 if (get_reply_max_size() != sizeof(handle))
1654 set_error( STATUS_BUFFER_TOO_SMALL );
1655 return 0;
1658 if (!(acceptsock = accept_socket( sock )))
1660 struct accept_req *req;
1662 if (sock->state & FD_WINE_NONBLOCKING) return 0;
1663 if (get_error() != STATUS_DEVICE_NOT_READY) return 0;
1665 if (!(req = alloc_accept_req( sock, NULL, async, NULL ))) return 0;
1666 list_add_tail( &sock->accept_list, &req->entry );
1668 async_set_completion_callback( async, free_accept_req, req );
1669 queue_async( &sock->accept_q, async );
1670 sock_reselect( sock );
1671 set_error( STATUS_PENDING );
1672 return 1;
1674 handle = alloc_handle( current->process, &acceptsock->obj,
1675 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
1676 acceptsock->wparam = handle;
1677 release_object( acceptsock );
1678 set_reply_data( &handle, sizeof(handle) );
1679 return 0;
1682 case IOCTL_AFD_WINE_ACCEPT_INTO:
1684 static const int access = FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | FILE_READ_DATA;
1685 const struct afd_accept_into_params *params = get_req_data();
1686 struct sock *acceptsock;
1687 unsigned int remote_len;
1688 struct accept_req *req;
1690 if (get_req_data_size() != sizeof(*params) ||
1691 get_reply_max_size() < params->recv_len ||
1692 get_reply_max_size() - params->recv_len < params->local_len)
1694 set_error( STATUS_BUFFER_TOO_SMALL );
1695 return 0;
1698 remote_len = get_reply_max_size() - params->recv_len - params->local_len;
1699 if (remote_len < sizeof(int))
1701 set_error( STATUS_INVALID_PARAMETER );
1702 return 0;
1705 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, params->accept_handle, access, &sock_ops )))
1706 return 0;
1708 if (acceptsock->accept_recv_req)
1710 release_object( acceptsock );
1711 set_error( STATUS_INVALID_PARAMETER );
1712 return 0;
1715 if (!(req = alloc_accept_req( sock, acceptsock, async, params )))
1717 release_object( acceptsock );
1718 return 0;
1720 list_add_tail( &sock->accept_list, &req->entry );
1721 acceptsock->accept_recv_req = req;
1722 release_object( acceptsock );
1724 acceptsock->wparam = params->accept_handle;
1725 async_set_completion_callback( async, free_accept_req, req );
1726 queue_async( &sock->accept_q, async );
1727 sock_reselect( sock );
1728 set_error( STATUS_PENDING );
1729 return 1;
1732 case IOCTL_AFD_LISTEN:
1734 const struct afd_listen_params *params = get_req_data();
1736 if (get_req_data_size() < sizeof(*params))
1738 set_error( STATUS_INVALID_PARAMETER );
1739 return 0;
1742 if (listen( unix_fd, params->backlog ) < 0)
1744 set_error( sock_get_ntstatus( errno ) );
1745 return 0;
1748 sock->pending_events &= ~FD_ACCEPT;
1749 sock->reported_events &= ~FD_ACCEPT;
1750 sock->state |= FD_WINE_LISTENING;
1751 sock->state &= ~(FD_CONNECT | FD_WINE_CONNECTED);
1753 /* we may already be selecting for FD_ACCEPT */
1754 sock_reselect( sock );
1755 return 0;
1758 case IOCTL_AFD_WINE_CONNECT:
1760 const struct afd_connect_params *params = get_req_data();
1761 const struct sockaddr *addr;
1762 struct connect_req *req;
1763 int send_len, ret;
1765 if (get_req_data_size() < sizeof(*params) ||
1766 get_req_data_size() - sizeof(*params) < params->addr_len)
1768 set_error( STATUS_BUFFER_TOO_SMALL );
1769 return 0;
1771 send_len = get_req_data_size() - sizeof(*params) - params->addr_len;
1772 addr = (const struct sockaddr *)(params + 1);
1774 if (sock->accept_recv_req)
1776 set_error( STATUS_INVALID_PARAMETER );
1777 return 0;
1780 if (sock->connect_req)
1782 set_error( params->synchronous ? STATUS_INVALID_PARAMETER : STATUS_CONNECTION_ACTIVE );
1783 return 0;
1786 ret = connect( unix_fd, addr, params->addr_len );
1787 if (ret < 0 && errno != EINPROGRESS)
1789 set_error( sock_get_ntstatus( errno ) );
1790 return 0;
1793 sock->pending_events &= ~(FD_CONNECT | FD_READ | FD_WRITE);
1794 sock->reported_events &= ~(FD_CONNECT | FD_READ | FD_WRITE);
1796 if (!ret)
1798 sock->state |= FD_WINE_CONNECTED | FD_READ | FD_WRITE;
1799 sock->state &= ~FD_CONNECT;
1801 if (!send_len) return 1;
1804 if (!(req = mem_alloc( sizeof(*req) )))
1805 return 0;
1807 sock->state |= FD_CONNECT;
1809 if (params->synchronous && (sock->state & FD_WINE_NONBLOCKING))
1811 sock_reselect( sock );
1812 set_error( STATUS_DEVICE_NOT_READY );
1813 return 0;
1816 req->async = (struct async *)grab_object( async );
1817 req->iosb = async_get_iosb( async );
1818 req->sock = (struct sock *)grab_object( sock );
1819 req->addr_len = params->addr_len;
1820 req->send_len = send_len;
1821 req->send_cursor = 0;
1823 async_set_completion_callback( async, free_connect_req, req );
1824 sock->connect_req = req;
1825 queue_async( &sock->connect_q, async );
1826 sock_reselect( sock );
1827 set_error( STATUS_PENDING );
1828 return 1;
1831 case IOCTL_AFD_WINE_SHUTDOWN:
1833 unsigned int how;
1835 if (get_req_data_size() < sizeof(int))
1837 set_error( STATUS_BUFFER_TOO_SMALL );
1838 return 0;
1840 how = *(int *)get_req_data();
1842 if (how > SD_BOTH)
1844 set_error( STATUS_INVALID_PARAMETER );
1845 return 0;
1848 if (sock->type == WS_SOCK_STREAM && !(sock->state & FD_WINE_CONNECTED))
1850 set_error( STATUS_INVALID_CONNECTION );
1851 return 0;
1854 if (how != SD_SEND)
1856 sock->state &= ~FD_READ;
1858 if (how != SD_RECEIVE)
1860 sock->state &= ~FD_WRITE;
1861 if (list_empty( &sock->write_q.queue ))
1862 shutdown( unix_fd, SHUT_WR );
1863 else
1864 sock->wr_shutdown_pending = 1;
1867 if (how == SD_BOTH)
1869 if (sock->event) release_object( sock->event );
1870 sock->event = NULL;
1871 sock->window = 0;
1872 sock->mask = 0;
1873 sock->state |= FD_WINE_NONBLOCKING;
1876 sock_reselect( sock );
1877 return 1;
1880 case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE:
1881 if ((sock->state & FD_WINE_NONBLOCKING) && async_is_blocking( async ))
1883 set_error( STATUS_DEVICE_NOT_READY );
1884 return 0;
1886 if (!sock_get_ifchange( sock )) return 0;
1887 queue_async( &sock->ifchange_q, async );
1888 set_error( STATUS_PENDING );
1889 return 1;
1891 default:
1892 set_error( STATUS_NOT_SUPPORTED );
1893 return 0;
1897 static int poll_socket( struct sock *poll_sock, struct async *async, timeout_t timeout,
1898 unsigned int count, const struct poll_socket_input *input )
1900 struct poll_socket_output *output;
1901 struct poll_req *req;
1902 unsigned int i, j;
1904 if (!(output = mem_alloc( count * sizeof(*output) )))
1905 return 0;
1906 memset( output, 0, count * sizeof(*output) );
1908 if (!(req = mem_alloc( offsetof( struct poll_req, sockets[count] ) )))
1910 free( output );
1911 return 0;
1914 req->timeout = NULL;
1915 if (timeout && timeout != TIMEOUT_INFINITE &&
1916 !(req->timeout = add_timeout_user( timeout, async_poll_timeout, req )))
1918 free( req );
1919 free( output );
1920 return 0;
1923 for (i = 0; i < count; ++i)
1925 req->sockets[i].sock = (struct sock *)get_handle_obj( current->process, input[i].socket, 0, &sock_ops );
1926 if (!req->sockets[i].sock)
1928 for (j = 0; j < i; ++j) release_object( req->sockets[i].sock );
1929 if (req->timeout) remove_timeout_user( req->timeout );
1930 free( req );
1931 free( output );
1932 return 0;
1934 req->sockets[i].flags = input[i].flags;
1937 req->count = count;
1938 req->async = (struct async *)grab_object( async );
1939 req->iosb = async_get_iosb( async );
1940 req->output = output;
1942 list_add_tail( &poll_list, &req->entry );
1943 async_set_completion_callback( async, free_poll_req, req );
1944 queue_async( &poll_sock->poll_q, async );
1946 if (!timeout) req->iosb->status = STATUS_SUCCESS;
1948 for (i = 0; i < count; ++i)
1950 struct sock *sock = req->sockets[i].sock;
1951 struct pollfd pollfd;
1952 int flags;
1954 pollfd.fd = get_unix_fd( sock->fd );
1955 pollfd.events = poll_flags_from_afd( sock, req->sockets[i].flags );
1956 if (pollfd.events < 0 || poll( &pollfd, 1, 0 ) < 0) continue;
1958 if ((req->sockets[i].flags & AFD_POLL_HUP) && (pollfd.revents & POLLIN) &&
1959 sock->type == WS_SOCK_STREAM)
1961 char dummy;
1963 if (!recv( get_unix_fd( sock->fd ), &dummy, 1, MSG_PEEK ))
1965 pollfd.revents &= ~POLLIN;
1966 pollfd.revents |= POLLHUP;
1970 flags = get_poll_flags( sock, pollfd.revents ) & req->sockets[i].flags;
1971 if (flags)
1973 req->iosb->status = STATUS_SUCCESS;
1974 output[i].flags = flags;
1975 output[i].status = sock_get_ntstatus( sock_error( sock->fd ) );
1979 if (req->iosb->status != STATUS_PENDING)
1981 req->iosb->out_data = output;
1982 req->iosb->out_size = count * sizeof(*output);
1983 async_terminate( req->async, STATUS_ALERTED );
1986 for (i = 0; i < req->count; ++i)
1987 sock_reselect( req->sockets[i].sock );
1988 set_error( STATUS_PENDING );
1989 return 1;
1992 #ifdef HAVE_LINUX_RTNETLINK_H
1994 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
1995 static struct object *ifchange_object;
1997 static void ifchange_dump( struct object *obj, int verbose );
1998 static struct fd *ifchange_get_fd( struct object *obj );
1999 static void ifchange_destroy( struct object *obj );
2001 static int ifchange_get_poll_events( struct fd *fd );
2002 static void ifchange_poll_event( struct fd *fd, int event );
2004 struct ifchange
2006 struct object obj; /* object header */
2007 struct fd *fd; /* interface change file descriptor */
2008 struct list sockets; /* list of sockets to send interface change notifications */
2011 static const struct object_ops ifchange_ops =
2013 sizeof(struct ifchange), /* size */
2014 &no_type, /* type */
2015 ifchange_dump, /* dump */
2016 no_add_queue, /* add_queue */
2017 NULL, /* remove_queue */
2018 NULL, /* signaled */
2019 no_satisfied, /* satisfied */
2020 no_signal, /* signal */
2021 ifchange_get_fd, /* get_fd */
2022 default_map_access, /* map_access */
2023 default_get_sd, /* get_sd */
2024 default_set_sd, /* set_sd */
2025 no_get_full_name, /* get_full_name */
2026 no_lookup_name, /* lookup_name */
2027 no_link_name, /* link_name */
2028 NULL, /* unlink_name */
2029 no_open_file, /* open_file */
2030 no_kernel_obj_list, /* get_kernel_obj_list */
2031 no_close_handle, /* close_handle */
2032 ifchange_destroy /* destroy */
2035 static const struct fd_ops ifchange_fd_ops =
2037 ifchange_get_poll_events, /* get_poll_events */
2038 ifchange_poll_event, /* poll_event */
2039 NULL, /* get_fd_type */
2040 no_fd_read, /* read */
2041 no_fd_write, /* write */
2042 no_fd_flush, /* flush */
2043 no_fd_get_file_info, /* get_file_info */
2044 no_fd_get_volume_info, /* get_volume_info */
2045 no_fd_ioctl, /* ioctl */
2046 NULL, /* queue_async */
2047 NULL /* reselect_async */
2050 static void ifchange_dump( struct object *obj, int verbose )
2052 assert( obj->ops == &ifchange_ops );
2053 fprintf( stderr, "Interface change\n" );
2056 static struct fd *ifchange_get_fd( struct object *obj )
2058 struct ifchange *ifchange = (struct ifchange *)obj;
2059 return (struct fd *)grab_object( ifchange->fd );
2062 static void ifchange_destroy( struct object *obj )
2064 struct ifchange *ifchange = (struct ifchange *)obj;
2065 assert( obj->ops == &ifchange_ops );
2067 release_object( ifchange->fd );
2069 /* reset the global ifchange object so that it will be recreated if it is needed again */
2070 assert( obj == ifchange_object );
2071 ifchange_object = NULL;
2074 static int ifchange_get_poll_events( struct fd *fd )
2076 return POLLIN;
2079 /* wake up all the sockets waiting for a change notification event */
2080 static void ifchange_wake_up( struct object *obj, unsigned int status )
2082 struct ifchange *ifchange = (struct ifchange *)obj;
2083 struct list *ptr, *next;
2084 assert( obj->ops == &ifchange_ops );
2085 assert( obj == ifchange_object );
2087 LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
2089 struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
2091 assert( sock->ifchange_obj );
2092 async_wake_up( &sock->ifchange_q, status ); /* issue ifchange notification for the socket */
2093 sock_release_ifchange( sock ); /* remove socket from list and decrement ifchange refcount */
2097 static void ifchange_poll_event( struct fd *fd, int event )
2099 struct object *ifchange = get_fd_user( fd );
2100 unsigned int status = STATUS_PENDING;
2101 char buffer[PIPE_BUF];
2102 int r;
2104 r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
2105 if (r < 0)
2107 if (errno == EWOULDBLOCK || (EWOULDBLOCK != EAGAIN && errno == EAGAIN))
2108 return; /* retry when poll() says the socket is ready */
2109 status = sock_get_ntstatus( errno );
2111 else if (r > 0)
2113 struct nlmsghdr *nlh;
2115 for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
2117 if (nlh->nlmsg_type == NLMSG_DONE)
2118 break;
2119 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
2120 status = STATUS_SUCCESS;
2123 else status = STATUS_CANCELLED;
2125 if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
2128 #endif
2130 /* we only need one of these interface notification objects, all of the sockets dependent upon
2131 * it will wake up when a notification event occurs */
2132 static struct object *get_ifchange( void )
2134 #ifdef HAVE_LINUX_RTNETLINK_H
2135 struct ifchange *ifchange;
2136 struct sockaddr_nl addr;
2137 int unix_fd;
2139 if (ifchange_object)
2141 /* increment the refcount for each socket that uses the ifchange object */
2142 return grab_object( ifchange_object );
2145 /* create the socket we need for processing interface change notifications */
2146 unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
2147 if (unix_fd == -1)
2149 set_error( sock_get_ntstatus( errno ));
2150 return NULL;
2152 fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
2153 memset( &addr, 0, sizeof(addr) );
2154 addr.nl_family = AF_NETLINK;
2155 addr.nl_groups = RTMGRP_IPV4_IFADDR;
2156 /* bind the socket to the special netlink kernel interface */
2157 if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
2159 close( unix_fd );
2160 set_error( sock_get_ntstatus( errno ));
2161 return NULL;
2163 if (!(ifchange = alloc_object( &ifchange_ops )))
2165 close( unix_fd );
2166 set_error( STATUS_NO_MEMORY );
2167 return NULL;
2169 list_init( &ifchange->sockets );
2170 if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
2172 release_object( ifchange );
2173 set_error( STATUS_NO_MEMORY );
2174 return NULL;
2176 set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
2178 /* the ifchange object is now successfully configured */
2179 ifchange_object = &ifchange->obj;
2180 return &ifchange->obj;
2181 #else
2182 set_error( STATUS_NOT_SUPPORTED );
2183 return NULL;
2184 #endif
2187 /* add the socket to the interface change notification list */
2188 static void ifchange_add_sock( struct object *obj, struct sock *sock )
2190 #ifdef HAVE_LINUX_RTNETLINK_H
2191 struct ifchange *ifchange = (struct ifchange *)obj;
2193 list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
2194 #endif
2197 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
2198 static struct object *sock_get_ifchange( struct sock *sock )
2200 struct object *ifchange;
2202 if (sock->ifchange_obj) /* reuse existing ifchange_obj for this socket */
2203 return sock->ifchange_obj;
2205 if (!(ifchange = get_ifchange()))
2206 return NULL;
2208 /* add the socket to the ifchange notification list */
2209 ifchange_add_sock( ifchange, sock );
2210 sock->ifchange_obj = ifchange;
2211 return ifchange;
2214 /* destroy an existing ifchange queue for a specific socket */
2215 static void sock_release_ifchange( struct sock *sock )
2217 if (sock->ifchange_obj)
2219 list_remove( &sock->ifchange_entry );
2220 release_object( sock->ifchange_obj );
2221 sock->ifchange_obj = NULL;
2225 static void socket_device_dump( struct object *obj, int verbose );
2226 static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
2227 unsigned int attr, struct object *root );
2228 static struct object *socket_device_open_file( struct object *obj, unsigned int access,
2229 unsigned int sharing, unsigned int options );
2231 static const struct object_ops socket_device_ops =
2233 sizeof(struct object), /* size */
2234 &device_type, /* type */
2235 socket_device_dump, /* dump */
2236 no_add_queue, /* add_queue */
2237 NULL, /* remove_queue */
2238 NULL, /* signaled */
2239 no_satisfied, /* satisfied */
2240 no_signal, /* signal */
2241 no_get_fd, /* get_fd */
2242 default_map_access, /* map_access */
2243 default_get_sd, /* get_sd */
2244 default_set_sd, /* set_sd */
2245 default_get_full_name, /* get_full_name */
2246 socket_device_lookup_name, /* lookup_name */
2247 directory_link_name, /* link_name */
2248 default_unlink_name, /* unlink_name */
2249 socket_device_open_file, /* open_file */
2250 no_kernel_obj_list, /* get_kernel_obj_list */
2251 no_close_handle, /* close_handle */
2252 no_destroy /* destroy */
2255 static void socket_device_dump( struct object *obj, int verbose )
2257 fputs( "Socket device\n", stderr );
2260 static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
2261 unsigned int attr, struct object *root )
2263 if (name) name->len = 0;
2264 return NULL;
2267 static struct object *socket_device_open_file( struct object *obj, unsigned int access,
2268 unsigned int sharing, unsigned int options )
2270 struct sock *sock;
2272 if (!(sock = create_socket())) return NULL;
2273 if (!(sock->fd = alloc_pseudo_fd( &sock_fd_ops, &sock->obj, options )))
2275 release_object( sock );
2276 return NULL;
2278 return &sock->obj;
2281 struct object *create_socket_device( struct object *root, const struct unicode_str *name,
2282 unsigned int attr, const struct security_descriptor *sd )
2284 return create_named_object( root, &socket_device_ops, name, attr, sd );
2287 /* set socket event parameters */
2288 DECL_HANDLER(set_socket_event)
2290 struct sock *sock;
2291 struct event *old_event;
2293 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
2294 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
2295 if (get_unix_fd( sock->fd ) == -1) return;
2296 old_event = sock->event;
2297 sock->mask = req->mask;
2298 if (req->window)
2300 sock->pending_events &= ~req->mask;
2301 sock->reported_events &= ~req->mask;
2303 sock->event = NULL;
2304 sock->window = req->window;
2305 sock->message = req->msg;
2306 sock->wparam = req->handle; /* wparam is the socket handle */
2307 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
2309 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
2311 sock_reselect( sock );
2313 sock->state |= FD_WINE_NONBLOCKING;
2315 /* if a network event is pending, signal the event object
2316 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
2317 before a WSAEventSelect() was done on it.
2318 (when dealing with Asynchronous socket) */
2319 sock_wake_up( sock );
2321 if (old_event) release_object( old_event ); /* we're through with it */
2322 release_object( &sock->obj );
2325 /* get socket event parameters */
2326 DECL_HANDLER(get_socket_event)
2328 struct sock *sock;
2330 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
2331 FILE_READ_ATTRIBUTES, &sock_ops ))) return;
2332 if (get_unix_fd( sock->fd ) == -1) return;
2333 reply->mask = sock->mask;
2334 reply->pmask = sock->pending_events;
2335 reply->state = sock->state;
2336 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
2338 if (req->service)
2340 if (req->c_event)
2342 struct event *cevent = get_event_obj( current->process, req->c_event,
2343 EVENT_MODIFY_STATE );
2344 if (cevent)
2346 reset_event( cevent );
2347 release_object( cevent );
2350 sock->pending_events = 0;
2351 sock_reselect( sock );
2353 release_object( &sock->obj );
2356 /* re-enable pending socket events */
2357 DECL_HANDLER(enable_socket_event)
2359 struct sock *sock;
2361 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
2362 FILE_WRITE_ATTRIBUTES, &sock_ops)))
2363 return;
2365 if (get_unix_fd( sock->fd ) == -1) return;
2367 /* for event-based notification, windows erases stale events */
2368 sock->pending_events &= ~req->mask;
2370 sock->reported_events &= ~req->mask;
2371 sock->state |= req->sstate;
2372 sock->state &= ~req->cstate;
2373 if (sock->type != WS_SOCK_STREAM) sock->state &= ~STREAM_FLAG_MASK;
2375 sock_reselect( sock );
2377 release_object( &sock->obj );
2380 DECL_HANDLER(set_socket_deferred)
2382 struct sock *sock, *acceptsock;
2384 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
2385 if ( !sock )
2386 return;
2388 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
2389 if ( !acceptsock )
2391 release_object( sock );
2392 return;
2394 sock->deferred = acceptsock;
2395 release_object( sock );
2398 DECL_HANDLER(get_socket_info)
2400 struct sock *sock;
2402 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
2403 if (!sock) return;
2405 if (get_unix_fd( sock->fd ) == -1) return;
2407 reply->family = sock->family;
2408 reply->type = sock->type;
2409 reply->protocol = sock->proto;
2411 release_object( &sock->obj );
2414 DECL_HANDLER(recv_socket)
2416 struct sock *sock = (struct sock *)get_handle_obj( current->process, req->async.handle, 0, &sock_ops );
2417 unsigned int status = req->status;
2418 timeout_t timeout = 0;
2419 struct async *async;
2420 struct fd *fd;
2422 if (!sock) return;
2423 fd = sock->fd;
2425 /* recv() returned EWOULDBLOCK, i.e. no data available yet */
2426 if (status == STATUS_DEVICE_NOT_READY && !(sock->state & FD_WINE_NONBLOCKING))
2428 #ifdef SO_RCVTIMEO
2429 struct timeval tv;
2430 socklen_t len = sizeof(tv);
2432 /* Set a timeout on the async if necessary.
2434 * We want to do this *only* if the client gave us STATUS_DEVICE_NOT_READY.
2435 * If the client gave us STATUS_PENDING, it expects the async to always
2436 * block (it was triggered by WSARecv*() with a valid OVERLAPPED
2437 * structure) and for the timeout not to be respected. */
2438 if (is_fd_overlapped( fd ) && !getsockopt( get_unix_fd( fd ), SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, &len ))
2439 timeout = tv.tv_sec * -10000000 + tv.tv_usec * -10;
2440 #endif
2442 status = STATUS_PENDING;
2445 /* are we shut down? */
2446 if (status == STATUS_PENDING && !(sock->state & FD_READ)) status = STATUS_PIPE_DISCONNECTED;
2448 sock->pending_events &= ~FD_READ;
2449 sock->reported_events &= ~FD_READ;
2451 if ((async = create_request_async( fd, get_fd_comp_flags( fd ), &req->async )))
2453 int success = 0;
2455 if (status == STATUS_SUCCESS)
2457 struct iosb *iosb = async_get_iosb( async );
2458 iosb->result = req->total;
2459 release_object( iosb );
2460 success = 1;
2462 else if (status == STATUS_PENDING)
2464 success = 1;
2466 set_error( status );
2468 if (timeout)
2469 async_set_timeout( async, timeout, STATUS_IO_TIMEOUT );
2471 if (status == STATUS_PENDING)
2472 queue_async( &sock->read_q, async );
2474 /* always reselect; we changed reported_events above */
2475 sock_reselect( sock );
2477 reply->wait = async_handoff( async, success, NULL, 0 );
2478 reply->options = get_fd_options( fd );
2479 release_object( async );
2481 release_object( sock );
2484 DECL_HANDLER(poll_socket)
2486 struct sock *sock = (struct sock *)get_handle_obj( current->process, req->async.handle, 0, &sock_ops );
2487 const struct poll_socket_input *input = get_req_data();
2488 struct async *async;
2489 unsigned int count;
2491 if (!sock) return;
2493 count = get_req_data_size() / sizeof(*input);
2495 if ((async = create_request_async( sock->fd, get_fd_comp_flags( sock->fd ), &req->async )))
2497 reply->wait = async_handoff( async, poll_socket( sock, async, req->timeout, count, input ), NULL, 0 );
2498 reply->options = get_fd_options( sock->fd );
2499 release_object( async );
2502 release_object( sock );