server: Retrieve ioctl data directly from the request.
[wine.git] / server / sock.c
blobad0ec714dfec31d8519a7856dc3e5be3678f728b
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_POLL_H
34 # include <poll.h>
35 #endif
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
40 #endif
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
43 #endif
44 #ifdef HAVE_SYS_FILIO_H
45 # include <sys/filio.h>
46 #endif
47 #include <time.h>
48 #include <unistd.h>
49 #include <limits.h>
50 #ifdef HAVE_LINUX_RTNETLINK_H
51 # include <linux/rtnetlink.h>
52 #endif
54 #include "ntstatus.h"
55 #define WIN32_NO_STATUS
56 #include "windef.h"
57 #include "winternl.h"
58 #include "winerror.h"
59 #define USE_WS_PREFIX
60 #include "winsock2.h"
62 #include "process.h"
63 #include "file.h"
64 #include "handle.h"
65 #include "thread.h"
66 #include "request.h"
67 #include "user.h"
69 /* From winsock.h */
70 #define FD_MAX_EVENTS 10
71 #define FD_READ_BIT 0
72 #define FD_WRITE_BIT 1
73 #define FD_OOB_BIT 2
74 #define FD_ACCEPT_BIT 3
75 #define FD_CONNECT_BIT 4
76 #define FD_CLOSE_BIT 5
79 * Define flags to be used with the WSAAsyncSelect() call.
81 #define FD_READ 0x00000001
82 #define FD_WRITE 0x00000002
83 #define FD_OOB 0x00000004
84 #define FD_ACCEPT 0x00000008
85 #define FD_CONNECT 0x00000010
86 #define FD_CLOSE 0x00000020
88 /* internal per-socket flags */
89 #define FD_WINE_LISTENING 0x10000000
90 #define FD_WINE_NONBLOCKING 0x20000000
91 #define FD_WINE_CONNECTED 0x40000000
92 #define FD_WINE_RAW 0x80000000
93 #define FD_WINE_INTERNAL 0xFFFF0000
95 struct sock
97 struct object obj; /* object header */
98 struct fd *fd; /* socket file descriptor */
99 unsigned int state; /* status bits */
100 unsigned int mask; /* event mask */
101 unsigned int hmask; /* held (blocked) events */
102 unsigned int pmask; /* pending events */
103 unsigned int flags; /* socket flags */
104 int polling; /* is socket being polled? */
105 unsigned short proto; /* socket protocol */
106 unsigned short type; /* socket type */
107 unsigned short family; /* socket family */
108 struct event *event; /* event object */
109 user_handle_t window; /* window to send the message to */
110 unsigned int message; /* message to send */
111 obj_handle_t wparam; /* message wparam (socket handle) */
112 int errors[FD_MAX_EVENTS]; /* event errors */
113 timeout_t connect_time;/* time the socket was connected */
114 struct sock *deferred; /* socket that waits for a deferred accept */
115 struct async_queue *read_q; /* queue for asynchronous reads */
116 struct async_queue *write_q; /* queue for asynchronous writes */
117 struct async_queue *ifchange_q; /* queue for interface change notifications */
118 struct object *ifchange_obj; /* the interface change notification object */
119 struct list ifchange_entry; /* entry in ifchange notification list */
122 static void sock_dump( struct object *obj, int verbose );
123 static int sock_add_ifchange( struct sock *sock, const async_data_t *async_data );
124 static int sock_signaled( struct object *obj, struct wait_queue_entry *entry );
125 static struct fd *sock_get_fd( struct object *obj );
126 static void sock_destroy( struct object *obj );
127 static struct async_queue *sock_get_ifchange_q( struct sock *sock );
128 static void sock_destroy_ifchange_q( struct sock *sock );
130 static int sock_get_poll_events( struct fd *fd );
131 static void sock_poll_event( struct fd *fd, int event );
132 static enum server_fd_type sock_get_fd_type( struct fd *fd );
133 static obj_handle_t sock_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async, int blocking );
134 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
135 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
136 static void sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb );
138 static int sock_get_ntstatus( int err );
139 static int sock_get_error( int err );
140 static void sock_set_error(void);
142 static const struct object_ops sock_ops =
144 sizeof(struct sock), /* size */
145 sock_dump, /* dump */
146 no_get_type, /* get_type */
147 add_queue, /* add_queue */
148 remove_queue, /* remove_queue */
149 sock_signaled, /* signaled */
150 no_satisfied, /* satisfied */
151 no_signal, /* signal */
152 sock_get_fd, /* get_fd */
153 default_fd_map_access, /* map_access */
154 default_get_sd, /* get_sd */
155 default_set_sd, /* set_sd */
156 no_lookup_name, /* lookup_name */
157 no_open_file, /* open_file */
158 fd_close_handle, /* close_handle */
159 sock_destroy /* destroy */
162 static const struct fd_ops sock_fd_ops =
164 sock_get_poll_events, /* get_poll_events */
165 sock_poll_event, /* poll_event */
166 no_flush, /* flush */
167 sock_get_fd_type, /* get_fd_type */
168 sock_ioctl, /* ioctl */
169 sock_queue_async, /* queue_async */
170 sock_reselect_async, /* reselect_async */
171 sock_cancel_async /* cancel_async */
175 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
176 * we post messages if there are multiple events. Used to send
177 * messages. The problem is if there is both a FD_CONNECT event and,
178 * say, an FD_READ event available on the same socket, we want to
179 * notify the app of the connect event first. Otherwise it may
180 * discard the read event because it thinks it hasn't connected yet.
182 static const int event_bitorder[FD_MAX_EVENTS] =
184 FD_CONNECT_BIT,
185 FD_ACCEPT_BIT,
186 FD_OOB_BIT,
187 FD_WRITE_BIT,
188 FD_READ_BIT,
189 FD_CLOSE_BIT,
190 6, 7, 8, 9 /* leftovers */
193 /* Flags that make sense only for SOCK_STREAM sockets */
194 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
196 typedef enum {
197 SOCK_SHUTDOWN_ERROR = -1,
198 SOCK_SHUTDOWN_EOF = 0,
199 SOCK_SHUTDOWN_POLLHUP = 1
200 } sock_shutdown_t;
202 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
204 static sock_shutdown_t sock_check_pollhup(void)
206 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
207 int fd[2], n;
208 struct pollfd pfd;
209 char dummy;
211 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
212 if ( shutdown( fd[0], 1 ) ) goto out;
214 pfd.fd = fd[1];
215 pfd.events = POLLIN;
216 pfd.revents = 0;
218 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
219 n = poll( &pfd, 1, 1 );
220 if ( n != 1 ) goto out; /* error or timeout */
221 if ( pfd.revents & POLLHUP )
222 ret = SOCK_SHUTDOWN_POLLHUP;
223 else if ( pfd.revents & POLLIN &&
224 read( fd[1], &dummy, 1 ) == 0 )
225 ret = SOCK_SHUTDOWN_EOF;
227 out:
228 close( fd[0] );
229 close( fd[1] );
230 return ret;
233 void sock_init(void)
235 sock_shutdown_type = sock_check_pollhup();
237 switch ( sock_shutdown_type )
239 case SOCK_SHUTDOWN_EOF:
240 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
241 break;
242 case SOCK_SHUTDOWN_POLLHUP:
243 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
244 break;
245 default:
246 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
247 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
251 static int sock_reselect( struct sock *sock )
253 int ev = sock_get_poll_events( sock->fd );
255 if (debug_level)
256 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
258 if (!sock->polling) /* FIXME: should find a better way to do this */
260 /* previously unconnected socket, is this reselect supposed to connect it? */
261 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
262 /* ok, it is, attach it to the wineserver's main poll loop */
263 sock->polling = 1;
264 allow_fd_caching( sock->fd );
266 /* update condition mask */
267 set_fd_events( sock->fd, ev );
268 return ev;
271 /* wake anybody waiting on the socket event or send the associated message */
272 static void sock_wake_up( struct sock *sock )
274 unsigned int events = sock->pmask & sock->mask;
275 int i;
277 if ( !events ) return;
279 if (sock->event)
281 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
282 set_event( sock->event );
284 if (sock->window)
286 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
287 for (i = 0; i < FD_MAX_EVENTS; i++)
289 int event = event_bitorder[i];
290 if (sock->pmask & (1 << event))
292 lparam_t lparam = (1 << event) | (sock_get_error(sock->errors[event]) << 16);
293 post_message( sock->window, sock->message, sock->wparam, lparam );
296 sock->pmask = 0;
297 sock_reselect( sock );
301 static inline int sock_error( struct fd *fd )
303 unsigned int optval = 0;
304 socklen_t optlen = sizeof(optval);
306 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
307 return optval;
310 static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
312 if ( sock->flags & WSA_FLAG_OVERLAPPED )
314 if ( event & (POLLIN|POLLPRI) && async_waiting( sock->read_q ) )
316 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
317 async_wake_up( sock->read_q, STATUS_ALERTED );
318 event &= ~(POLLIN|POLLPRI);
320 if ( event & POLLOUT && async_waiting( sock->write_q ) )
322 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
323 async_wake_up( sock->write_q, STATUS_ALERTED );
324 event &= ~POLLOUT;
326 if ( event & (POLLERR|POLLHUP) )
328 int status = sock_get_ntstatus( error );
330 if ( !(sock->state & FD_READ) )
331 async_wake_up( sock->read_q, status );
332 if ( !(sock->state & FD_WRITE) )
333 async_wake_up( sock->write_q, status );
336 return event;
339 static void sock_dispatch_events( struct sock *sock, int prevstate, int event, int error )
341 if (prevstate & FD_CONNECT)
343 sock->pmask |= FD_CONNECT;
344 sock->hmask |= FD_CONNECT;
345 sock->errors[FD_CONNECT_BIT] = error;
346 goto end;
348 if (prevstate & FD_WINE_LISTENING)
350 sock->pmask |= FD_ACCEPT;
351 sock->hmask |= FD_ACCEPT;
352 sock->errors[FD_ACCEPT_BIT] = error;
353 goto end;
356 if (event & POLLIN)
358 sock->pmask |= FD_READ;
359 sock->hmask |= FD_READ;
360 sock->errors[FD_READ_BIT] = 0;
363 if (event & POLLOUT)
365 sock->pmask |= FD_WRITE;
366 sock->hmask |= FD_WRITE;
367 sock->errors[FD_WRITE_BIT] = 0;
370 if (event & POLLPRI)
372 sock->pmask |= FD_OOB;
373 sock->hmask |= FD_OOB;
374 sock->errors[FD_OOB_BIT] = 0;
377 if (event & (POLLERR|POLLHUP))
379 sock->pmask |= FD_CLOSE;
380 sock->hmask |= FD_CLOSE;
381 sock->errors[FD_CLOSE_BIT] = error;
383 end:
384 sock_wake_up( sock );
387 static void sock_poll_event( struct fd *fd, int event )
389 struct sock *sock = get_fd_user( fd );
390 int hangup_seen = 0;
391 int prevstate = sock->state;
392 int error = 0;
394 assert( sock->obj.ops == &sock_ops );
395 if (debug_level)
396 fprintf(stderr, "socket %p select event: %x\n", sock, event);
398 /* we may change event later, remove from loop here */
399 if (event & (POLLERR|POLLHUP)) set_fd_events( sock->fd, -1 );
401 if (sock->state & FD_CONNECT)
403 if (event & (POLLERR|POLLHUP))
405 /* we didn't get connected? */
406 sock->state &= ~FD_CONNECT;
407 event &= ~POLLOUT;
408 error = sock_error( fd );
410 else if (event & POLLOUT)
412 /* we got connected */
413 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
414 sock->state &= ~FD_CONNECT;
415 sock->connect_time = current_time;
418 else if (sock->state & FD_WINE_LISTENING)
420 /* listening */
421 if (event & (POLLERR|POLLHUP))
422 error = sock_error( fd );
424 else
426 /* normal data flow */
427 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
429 char dummy;
430 int nr;
432 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
433 * has been closed, so we need to check for it explicitly here */
434 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
435 if ( nr == 0 )
437 hangup_seen = 1;
438 event &= ~POLLIN;
440 else if ( nr < 0 )
442 event &= ~POLLIN;
443 /* EAGAIN can happen if an async recv() falls between the server's poll()
444 call and the invocation of this routine */
445 if ( errno != EAGAIN )
447 error = errno;
448 event |= POLLERR;
449 if ( debug_level )
450 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
455 if ( (hangup_seen || event & (POLLHUP|POLLERR)) && (sock->state & (FD_READ|FD_WRITE)) )
457 error = error ? error : sock_error( fd );
458 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
459 sock->state &= ~FD_WRITE;
460 sock->state &= ~FD_READ;
462 if (debug_level)
463 fprintf(stderr, "socket %p aborted by error %d, event: %x\n", sock, error, event);
466 if (hangup_seen)
467 event |= POLLHUP;
470 event = sock_dispatch_asyncs( sock, event, error );
471 sock_dispatch_events( sock, prevstate, event, error );
473 /* if anyone is stupid enough to wait on the socket object itself,
474 * maybe we should wake them up too, just in case? */
475 wake_up( &sock->obj, 0 );
477 sock_reselect( sock );
480 static void sock_dump( struct object *obj, int verbose )
482 struct sock *sock = (struct sock *)obj;
483 assert( obj->ops == &sock_ops );
484 fprintf( stderr, "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
485 sock->fd, sock->state,
486 sock->mask, sock->pmask, sock->hmask );
489 static int sock_signaled( struct object *obj, struct wait_queue_entry *entry )
491 struct sock *sock = (struct sock *)obj;
492 assert( obj->ops == &sock_ops );
494 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
497 static int sock_get_poll_events( struct fd *fd )
499 struct sock *sock = get_fd_user( fd );
500 unsigned int mask = sock->mask & ~sock->hmask;
501 unsigned int smask = sock->state & mask;
502 int ev = 0;
504 assert( sock->obj.ops == &sock_ops );
506 if (sock->state & FD_CONNECT)
507 /* connecting, wait for writable */
508 return POLLOUT;
510 if ( async_queued( sock->read_q ) )
512 if ( async_waiting( sock->read_q ) ) ev |= POLLIN | POLLPRI;
514 else if (smask & FD_READ || (sock->state & FD_WINE_LISTENING && mask & FD_ACCEPT))
515 ev |= POLLIN | POLLPRI;
516 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
517 else if ( sock->type == SOCK_STREAM && sock->state & FD_READ && mask & FD_CLOSE &&
518 !(sock->hmask & FD_READ) )
519 ev |= POLLIN;
521 if ( async_queued( sock->write_q ) )
523 if ( async_waiting( sock->write_q ) ) ev |= POLLOUT;
525 else if (smask & FD_WRITE)
526 ev |= POLLOUT;
528 return ev;
531 static enum server_fd_type sock_get_fd_type( struct fd *fd )
533 return FD_TYPE_SOCKET;
536 obj_handle_t sock_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data, int blocking )
538 struct sock *sock = get_fd_user( fd );
539 obj_handle_t wait_handle = 0;
540 async_data_t new_data;
542 assert( sock->obj.ops == &sock_ops );
544 switch(code)
546 case WS_SIO_ADDRESS_LIST_CHANGE:
547 if (blocking)
549 if (!(wait_handle = alloc_wait_event( current->process ))) return 0;
550 new_data = *async_data;
551 new_data.event = wait_handle;
552 async_data = &new_data;
554 if (!sock_add_ifchange( sock, async_data ) && wait_handle)
556 close_handle( current->process, wait_handle );
557 return 0;
559 return wait_handle;
560 default:
561 set_error( STATUS_NOT_SUPPORTED );
562 return 0;
566 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
568 struct sock *sock = get_fd_user( fd );
569 struct async *async;
570 struct async_queue *queue;
572 assert( sock->obj.ops == &sock_ops );
574 switch (type)
576 case ASYNC_TYPE_READ:
577 if (!sock->read_q && !(sock->read_q = create_async_queue( sock->fd ))) return;
578 queue = sock->read_q;
579 break;
580 case ASYNC_TYPE_WRITE:
581 if (!sock->write_q && !(sock->write_q = create_async_queue( sock->fd ))) return;
582 queue = sock->write_q;
583 break;
584 default:
585 set_error( STATUS_INVALID_PARAMETER );
586 return;
589 if ( ( !( sock->state & (FD_READ|FD_CONNECT|FD_WINE_LISTENING) ) && type == ASYNC_TYPE_READ ) ||
590 ( !( sock->state & (FD_WRITE|FD_CONNECT) ) && type == ASYNC_TYPE_WRITE ) )
592 set_error( STATUS_PIPE_DISCONNECTED );
593 return;
596 if (!(async = create_async( current, queue, data ))) return;
597 release_object( async );
599 sock_reselect( sock );
601 set_error( STATUS_PENDING );
604 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
606 struct sock *sock = get_fd_user( fd );
607 sock_reselect( sock );
610 static void sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb )
612 struct sock *sock = get_fd_user( fd );
613 int n = 0;
614 assert( sock->obj.ops == &sock_ops );
616 n += async_wake_up_by( sock->read_q, process, thread, iosb, STATUS_CANCELLED );
617 n += async_wake_up_by( sock->write_q, process, thread, iosb, STATUS_CANCELLED );
618 if (!n && iosb)
619 set_error( STATUS_NOT_FOUND );
622 static struct fd *sock_get_fd( struct object *obj )
624 struct sock *sock = (struct sock *)obj;
625 return (struct fd *)grab_object( sock->fd );
628 static void sock_destroy( struct object *obj )
630 struct sock *sock = (struct sock *)obj;
631 assert( obj->ops == &sock_ops );
633 /* FIXME: special socket shutdown stuff? */
635 if ( sock->deferred )
636 release_object( sock->deferred );
638 free_async_queue( sock->read_q );
639 free_async_queue( sock->write_q );
640 sock_destroy_ifchange_q( sock );
641 if (sock->event) release_object( sock->event );
642 if (sock->fd)
644 /* shut the socket down to force pending poll() calls in the client to return */
645 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
646 release_object( sock->fd );
650 static void init_sock(struct sock *sock)
652 sock->state = 0;
653 sock->mask = 0;
654 sock->hmask = 0;
655 sock->pmask = 0;
656 sock->polling = 0;
657 sock->flags = 0;
658 sock->type = 0;
659 sock->family = 0;
660 sock->event = NULL;
661 sock->window = 0;
662 sock->message = 0;
663 sock->wparam = 0;
664 sock->connect_time = 0;
665 sock->deferred = NULL;
666 sock->read_q = NULL;
667 sock->write_q = NULL;
668 sock->ifchange_q = NULL;
669 sock->ifchange_obj = NULL;
670 memset( sock->errors, 0, sizeof(sock->errors) );
673 /* create a new and unconnected socket */
674 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
676 struct sock *sock;
677 int sockfd;
679 sockfd = socket( family, type, protocol );
680 if (debug_level)
681 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
682 if (sockfd == -1)
684 sock_set_error();
685 return NULL;
687 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
688 if (!(sock = alloc_object( &sock_ops )))
690 close( sockfd );
691 return NULL;
693 init_sock( sock );
694 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
695 sock->flags = flags;
696 sock->proto = protocol;
697 sock->type = type;
698 sock->family = family;
700 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
701 (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
703 release_object( sock );
704 return NULL;
706 sock_reselect( sock );
707 clear_error();
708 return &sock->obj;
711 /* accepts a socket and inits it */
712 static int accept_new_fd( struct sock *sock )
715 /* Try to accept(2). We can't be safe that this an already connected socket
716 * or that accept() is allowed on it. In those cases we will get -1/errno
717 * return.
719 int acceptfd;
720 struct sockaddr saddr;
721 socklen_t slen = sizeof(saddr);
722 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
723 if (acceptfd == -1)
725 sock_set_error();
726 return acceptfd;
729 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
730 return acceptfd;
733 /* accept a socket (creates a new fd) */
734 static struct sock *accept_socket( obj_handle_t handle )
736 struct sock *acceptsock;
737 struct sock *sock;
738 int acceptfd;
740 sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
741 if (!sock)
742 return NULL;
744 if ( sock->deferred )
746 acceptsock = sock->deferred;
747 sock->deferred = NULL;
749 else
751 if ((acceptfd = accept_new_fd( sock )) == -1)
753 release_object( sock );
754 return NULL;
756 if (!(acceptsock = alloc_object( &sock_ops )))
758 close( acceptfd );
759 release_object( sock );
760 return NULL;
763 init_sock( acceptsock );
764 /* newly created socket gets the same properties of the listening socket */
765 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
766 if (sock->state & FD_WINE_NONBLOCKING)
767 acceptsock->state |= FD_WINE_NONBLOCKING;
768 acceptsock->mask = sock->mask;
769 acceptsock->proto = sock->proto;
770 acceptsock->type = sock->type;
771 acceptsock->family = sock->family;
772 acceptsock->window = sock->window;
773 acceptsock->message = sock->message;
774 acceptsock->connect_time = current_time;
775 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
776 acceptsock->flags = sock->flags;
777 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
778 get_fd_options( sock->fd ) )))
780 release_object( acceptsock );
781 release_object( sock );
782 return NULL;
785 clear_error();
786 sock->pmask &= ~FD_ACCEPT;
787 sock->hmask &= ~FD_ACCEPT;
788 sock_reselect( sock );
789 release_object( sock );
790 return acceptsock;
793 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
795 int acceptfd;
796 struct fd *newfd;
797 if ( sock->deferred )
799 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
800 get_fd_options( acceptsock->fd ) );
801 if ( !newfd )
802 return FALSE;
804 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
806 release_object( sock->deferred );
807 sock->deferred = NULL;
809 else
811 if ((acceptfd = accept_new_fd( sock )) == -1)
812 return FALSE;
814 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
815 get_fd_options( acceptsock->fd ) )))
816 return FALSE;
819 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
820 acceptsock->hmask = 0;
821 acceptsock->pmask = 0;
822 acceptsock->polling = 0;
823 acceptsock->proto = sock->proto;
824 acceptsock->type = sock->type;
825 acceptsock->family = sock->family;
826 acceptsock->wparam = 0;
827 acceptsock->deferred = NULL;
828 acceptsock->connect_time = current_time;
829 fd_copy_completion( acceptsock->fd, newfd );
830 release_object( acceptsock->fd );
831 acceptsock->fd = newfd;
833 clear_error();
834 sock->pmask &= ~FD_ACCEPT;
835 sock->hmask &= ~FD_ACCEPT;
836 sock_reselect( sock );
838 return TRUE;
841 /* return an errno value mapped to a WSA error */
842 static int sock_get_error( int err )
844 switch (err)
846 case EINTR: return WSAEINTR;
847 case EBADF: return WSAEBADF;
848 case EPERM:
849 case EACCES: return WSAEACCES;
850 case EFAULT: return WSAEFAULT;
851 case EINVAL: return WSAEINVAL;
852 case EMFILE: return WSAEMFILE;
853 case EWOULDBLOCK: return WSAEWOULDBLOCK;
854 case EINPROGRESS: return WSAEINPROGRESS;
855 case EALREADY: return WSAEALREADY;
856 case ENOTSOCK: return WSAENOTSOCK;
857 case EDESTADDRREQ: return WSAEDESTADDRREQ;
858 case EMSGSIZE: return WSAEMSGSIZE;
859 case EPROTOTYPE: return WSAEPROTOTYPE;
860 case ENOPROTOOPT: return WSAENOPROTOOPT;
861 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
862 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
863 case EOPNOTSUPP: return WSAEOPNOTSUPP;
864 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
865 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
866 case EADDRINUSE: return WSAEADDRINUSE;
867 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
868 case ENETDOWN: return WSAENETDOWN;
869 case ENETUNREACH: return WSAENETUNREACH;
870 case ENETRESET: return WSAENETRESET;
871 case ECONNABORTED: return WSAECONNABORTED;
872 case EPIPE:
873 case ECONNRESET: return WSAECONNRESET;
874 case ENOBUFS: return WSAENOBUFS;
875 case EISCONN: return WSAEISCONN;
876 case ENOTCONN: return WSAENOTCONN;
877 case ESHUTDOWN: return WSAESHUTDOWN;
878 case ETOOMANYREFS: return WSAETOOMANYREFS;
879 case ETIMEDOUT: return WSAETIMEDOUT;
880 case ECONNREFUSED: return WSAECONNREFUSED;
881 case ELOOP: return WSAELOOP;
882 case ENAMETOOLONG: return WSAENAMETOOLONG;
883 case EHOSTDOWN: return WSAEHOSTDOWN;
884 case EHOSTUNREACH: return WSAEHOSTUNREACH;
885 case ENOTEMPTY: return WSAENOTEMPTY;
886 #ifdef EPROCLIM
887 case EPROCLIM: return WSAEPROCLIM;
888 #endif
889 #ifdef EUSERS
890 case EUSERS: return WSAEUSERS;
891 #endif
892 #ifdef EDQUOT
893 case EDQUOT: return WSAEDQUOT;
894 #endif
895 #ifdef ESTALE
896 case ESTALE: return WSAESTALE;
897 #endif
898 #ifdef EREMOTE
899 case EREMOTE: return WSAEREMOTE;
900 #endif
902 case 0: return 0;
903 default:
904 errno = err;
905 perror("wineserver: sock_get_error() can't map error");
906 return WSAEFAULT;
910 static int sock_get_ntstatus( int err )
912 switch ( err )
914 case EBADF: return STATUS_INVALID_HANDLE;
915 case EBUSY: return STATUS_DEVICE_BUSY;
916 case EPERM:
917 case EACCES: return STATUS_ACCESS_DENIED;
918 case EFAULT: return STATUS_NO_MEMORY;
919 case EINVAL: return STATUS_INVALID_PARAMETER;
920 case ENFILE:
921 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
922 case EWOULDBLOCK: return STATUS_CANT_WAIT;
923 case EINPROGRESS: return STATUS_PENDING;
924 case EALREADY: return STATUS_NETWORK_BUSY;
925 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
926 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
927 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
928 case EPROTONOSUPPORT:
929 case ESOCKTNOSUPPORT:
930 case EPFNOSUPPORT:
931 case EAFNOSUPPORT:
932 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
933 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
934 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
935 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
936 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
937 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
938 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
939 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
940 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
941 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
942 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
943 case ENETDOWN: return STATUS_NETWORK_BUSY;
944 case EPIPE:
945 case ECONNRESET: return STATUS_CONNECTION_RESET;
946 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
948 case 0: return STATUS_SUCCESS;
949 default:
950 errno = err;
951 perror("wineserver: sock_get_ntstatus() can't map error");
952 return STATUS_UNSUCCESSFUL;
956 /* set the last error depending on errno */
957 static void sock_set_error(void)
959 set_error( sock_get_ntstatus( errno ) );
962 /* add interface change notification to a socket */
963 static int sock_add_ifchange( struct sock *sock, const async_data_t *async_data )
965 struct async_queue *ifchange_q;
966 struct async *async;
968 if (!(ifchange_q = sock_get_ifchange_q( sock )))
969 return 0;
971 if (!(async = create_async( current, ifchange_q, async_data )))
973 if (!async_queued( ifchange_q ))
974 sock_destroy_ifchange_q( sock );
976 set_error( STATUS_NO_MEMORY );
977 return 0;
980 release_object( async );
981 set_error( STATUS_PENDING );
982 return 1;
985 #ifdef HAVE_LINUX_RTNETLINK_H
987 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
988 static struct object *ifchange_object;
990 static void ifchange_dump( struct object *obj, int verbose );
991 static struct fd *ifchange_get_fd( struct object *obj );
992 static void ifchange_destroy( struct object *obj );
994 static int ifchange_get_poll_events( struct fd *fd );
995 static void ifchange_poll_event( struct fd *fd, int event );
996 static void ifchange_reselect_async( struct fd *fd, struct async_queue *queue );
998 struct ifchange
1000 struct object obj; /* object header */
1001 struct fd *fd; /* interface change file descriptor */
1002 struct list sockets; /* list of sockets to send interface change notifications */
1005 static const struct object_ops ifchange_ops =
1007 sizeof(struct ifchange), /* size */
1008 ifchange_dump, /* dump */
1009 no_get_type, /* get_type */
1010 add_queue, /* add_queue */
1011 NULL, /* remove_queue */
1012 NULL, /* signaled */
1013 no_satisfied, /* satisfied */
1014 no_signal, /* signal */
1015 ifchange_get_fd, /* get_fd */
1016 default_fd_map_access, /* map_access */
1017 default_get_sd, /* get_sd */
1018 default_set_sd, /* set_sd */
1019 no_lookup_name, /* lookup_name */
1020 no_open_file, /* open_file */
1021 no_close_handle, /* close_handle */
1022 ifchange_destroy /* destroy */
1025 static const struct fd_ops ifchange_fd_ops =
1027 ifchange_get_poll_events, /* get_poll_events */
1028 ifchange_poll_event, /* poll_event */
1029 NULL, /* flush */
1030 NULL, /* get_fd_type */
1031 NULL, /* ioctl */
1032 NULL, /* queue_async */
1033 ifchange_reselect_async, /* reselect_async */
1034 NULL /* cancel_async */
1037 static void ifchange_dump( struct object *obj, int verbose )
1039 assert( obj->ops == &ifchange_ops );
1040 fprintf( stderr, "Interface change\n" );
1043 static struct fd *ifchange_get_fd( struct object *obj )
1045 struct ifchange *ifchange = (struct ifchange *)obj;
1046 return (struct fd *)grab_object( ifchange->fd );
1049 static void ifchange_destroy( struct object *obj )
1051 struct ifchange *ifchange = (struct ifchange *)obj;
1052 assert( obj->ops == &ifchange_ops );
1054 release_object( ifchange->fd );
1056 /* reset the global ifchange object so that it will be recreated if it is needed again */
1057 assert( obj == ifchange_object );
1058 ifchange_object = NULL;
1061 static int ifchange_get_poll_events( struct fd *fd )
1063 return POLLIN;
1066 /* wake up all the sockets waiting for a change notification event */
1067 static void ifchange_wake_up( struct object *obj, unsigned int status )
1069 struct ifchange *ifchange = (struct ifchange *)obj;
1070 struct list *ptr, *next;
1071 assert( obj->ops == &ifchange_ops );
1072 assert( obj == ifchange_object );
1074 LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
1076 struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
1078 assert( sock->ifchange_q );
1079 async_wake_up( sock->ifchange_q, status ); /* issue ifchange notification for the socket */
1080 sock_destroy_ifchange_q( sock ); /* remove socket from list and decrement ifchange refcount */
1084 static void ifchange_poll_event( struct fd *fd, int event )
1086 struct object *ifchange = get_fd_user( fd );
1087 unsigned int status = STATUS_PENDING;
1088 char buffer[PIPE_BUF];
1089 int r;
1091 r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
1092 if (r < 0)
1094 if (errno == EWOULDBLOCK || errno == EAGAIN)
1095 return; /* retry when poll() says the socket is ready */
1096 status = sock_get_ntstatus( errno );
1098 else if (r > 0)
1100 struct nlmsghdr *nlh;
1102 for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
1104 if (nlh->nlmsg_type == NLMSG_DONE)
1105 break;
1106 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
1107 status = STATUS_SUCCESS;
1110 else status = STATUS_CANCELLED;
1112 if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
1115 static void ifchange_reselect_async( struct fd *fd, struct async_queue *queue )
1117 /* do nothing, this object is about to disappear */
1120 #endif
1122 /* we only need one of these interface notification objects, all of the sockets dependent upon
1123 * it will wake up when a notification event occurs */
1124 static struct object *get_ifchange( void )
1126 #ifdef HAVE_LINUX_RTNETLINK_H
1127 struct ifchange *ifchange;
1128 struct sockaddr_nl addr;
1129 int unix_fd;
1131 if (ifchange_object)
1133 /* increment the refcount for each socket that uses the ifchange object */
1134 return grab_object( ifchange_object );
1137 /* create the socket we need for processing interface change notifications */
1138 unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
1139 if (unix_fd == -1)
1141 sock_set_error();
1142 return NULL;
1144 fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
1145 memset( &addr, 0, sizeof(addr) );
1146 addr.nl_family = AF_NETLINK;
1147 addr.nl_groups = RTMGRP_IPV4_IFADDR;
1148 /* bind the socket to the special netlink kernel interface */
1149 if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
1151 close( unix_fd );
1152 sock_set_error();
1153 return NULL;
1155 if (!(ifchange = alloc_object( &ifchange_ops )))
1157 close( unix_fd );
1158 set_error( STATUS_NO_MEMORY );
1159 return NULL;
1161 list_init( &ifchange->sockets );
1162 if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
1164 release_object( ifchange );
1165 set_error( STATUS_NO_MEMORY );
1166 return NULL;
1168 set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
1170 /* the ifchange object is now successfully configured */
1171 ifchange_object = &ifchange->obj;
1172 return &ifchange->obj;
1173 #else
1174 set_error( STATUS_NOT_SUPPORTED );
1175 return NULL;
1176 #endif
1179 /* add the socket to the interface change notification list */
1180 static void ifchange_add_sock( struct object *obj, struct sock *sock )
1182 #ifdef HAVE_LINUX_RTNETLINK_H
1183 struct ifchange *ifchange = (struct ifchange *)obj;
1185 list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
1186 #endif
1189 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1190 static struct async_queue *sock_get_ifchange_q( struct sock *sock )
1192 struct object *ifchange;
1193 struct fd *fd;
1195 if (sock->ifchange_q) /* reuse existing ifchange_q for this socket */
1196 return sock->ifchange_q;
1198 if (!(ifchange = get_ifchange()))
1199 return NULL;
1201 /* create the ifchange notification queue */
1202 fd = ifchange->ops->get_fd( ifchange );
1203 sock->ifchange_q = create_async_queue( fd );
1204 release_object( fd );
1205 if (!sock->ifchange_q)
1207 release_object( ifchange );
1208 set_error( STATUS_NO_MEMORY );
1209 return NULL;
1212 /* add the socket to the ifchange notification list */
1213 ifchange_add_sock( ifchange, sock );
1214 sock->ifchange_obj = ifchange;
1215 return sock->ifchange_q;
1218 /* destroy an existing ifchange queue for a specific socket */
1219 static void sock_destroy_ifchange_q( struct sock *sock )
1221 if (sock->ifchange_q)
1223 list_remove( &sock->ifchange_entry );
1224 free_async_queue( sock->ifchange_q );
1225 sock->ifchange_q = NULL;
1226 release_object( sock->ifchange_obj );
1230 /* create a socket */
1231 DECL_HANDLER(create_socket)
1233 struct object *obj;
1235 reply->handle = 0;
1236 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
1238 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1239 release_object( obj );
1243 /* accept a socket */
1244 DECL_HANDLER(accept_socket)
1246 struct sock *sock;
1248 reply->handle = 0;
1249 if ((sock = accept_socket( req->lhandle )) != NULL)
1251 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
1252 sock->wparam = reply->handle; /* wparam for message is the socket handle */
1253 sock_reselect( sock );
1254 release_object( &sock->obj );
1258 /* accept a socket into an initialized socket */
1259 DECL_HANDLER(accept_into_socket)
1261 struct sock *sock, *acceptsock;
1262 const int all_attributes = FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|FILE_READ_DATA;
1264 if (!(sock = (struct sock *)get_handle_obj( current->process, req->lhandle,
1265 all_attributes, &sock_ops)))
1266 return;
1268 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, req->ahandle,
1269 all_attributes, &sock_ops)))
1271 release_object( sock );
1272 return;
1275 if (accept_into_socket( sock, acceptsock ))
1277 acceptsock->wparam = req->ahandle; /* wparam for message is the socket handle */
1278 sock_reselect( acceptsock );
1280 release_object( acceptsock );
1281 release_object( sock );
1284 /* set socket event parameters */
1285 DECL_HANDLER(set_socket_event)
1287 struct sock *sock;
1288 struct event *old_event;
1290 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1291 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
1292 old_event = sock->event;
1293 sock->mask = req->mask;
1294 sock->hmask &= ~req->mask; /* re-enable held events */
1295 sock->event = NULL;
1296 sock->window = req->window;
1297 sock->message = req->msg;
1298 sock->wparam = req->handle; /* wparam is the socket handle */
1299 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
1301 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
1303 sock_reselect( sock );
1305 sock->state |= FD_WINE_NONBLOCKING;
1307 /* if a network event is pending, signal the event object
1308 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1309 before a WSAEventSelect() was done on it.
1310 (when dealing with Asynchronous socket) */
1311 sock_wake_up( sock );
1313 if (old_event) release_object( old_event ); /* we're through with it */
1314 release_object( &sock->obj );
1317 /* get socket event parameters */
1318 DECL_HANDLER(get_socket_event)
1320 struct sock *sock;
1321 int i;
1322 int errors[FD_MAX_EVENTS];
1324 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1325 if (!sock)
1327 reply->mask = 0;
1328 reply->pmask = 0;
1329 reply->state = 0;
1330 return;
1332 reply->mask = sock->mask;
1333 reply->pmask = sock->pmask;
1334 reply->state = sock->state;
1335 for (i = 0; i < FD_MAX_EVENTS; i++)
1336 errors[i] = sock_get_ntstatus(sock->errors[i]);
1338 set_reply_data( errors, min( get_reply_max_size(), sizeof(errors) ));
1340 if (req->service)
1342 if (req->c_event)
1344 struct event *cevent = get_event_obj( current->process, req->c_event,
1345 EVENT_MODIFY_STATE );
1346 if (cevent)
1348 reset_event( cevent );
1349 release_object( cevent );
1352 sock->pmask = 0;
1353 sock_reselect( sock );
1355 release_object( &sock->obj );
1358 /* re-enable pending socket events */
1359 DECL_HANDLER(enable_socket_event)
1361 struct sock *sock;
1363 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
1364 FILE_WRITE_ATTRIBUTES, &sock_ops)))
1365 return;
1367 /* for event-based notification, windows erases stale events */
1368 sock->pmask &= ~req->mask;
1370 sock->hmask &= ~req->mask;
1371 sock->state |= req->sstate;
1372 sock->state &= ~req->cstate;
1373 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
1375 sock_reselect( sock );
1377 release_object( &sock->obj );
1380 DECL_HANDLER(set_socket_deferred)
1382 struct sock *sock, *acceptsock;
1384 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
1385 if ( !sock )
1386 return;
1388 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
1389 if ( !acceptsock )
1391 release_object( sock );
1392 return;
1394 sock->deferred = acceptsock;
1395 release_object( sock );
1398 DECL_HANDLER(get_socket_info)
1400 struct sock *sock;
1402 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1403 if (!sock) return;
1405 reply->family = sock->family;
1406 reply->type = sock->type;
1407 reply->protocol = sock->proto;
1409 release_object( &sock->obj );