server: Add read and write fd member functions.
[wine.git] / server / sock.c
blob337006f7d9d197ea38ea426d7f971c74684702f1
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 sock_get_fd_type, /* get_fd_type */
167 no_fd_read, /* read */
168 no_fd_write, /* write */
169 no_fd_flush, /* flush */
170 sock_ioctl, /* ioctl */
171 sock_queue_async, /* queue_async */
172 sock_reselect_async, /* reselect_async */
173 sock_cancel_async /* cancel_async */
177 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
178 * we post messages if there are multiple events. Used to send
179 * messages. The problem is if there is both a FD_CONNECT event and,
180 * say, an FD_READ event available on the same socket, we want to
181 * notify the app of the connect event first. Otherwise it may
182 * discard the read event because it thinks it hasn't connected yet.
184 static const int event_bitorder[FD_MAX_EVENTS] =
186 FD_CONNECT_BIT,
187 FD_ACCEPT_BIT,
188 FD_OOB_BIT,
189 FD_WRITE_BIT,
190 FD_READ_BIT,
191 FD_CLOSE_BIT,
192 6, 7, 8, 9 /* leftovers */
195 /* Flags that make sense only for SOCK_STREAM sockets */
196 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
198 typedef enum {
199 SOCK_SHUTDOWN_ERROR = -1,
200 SOCK_SHUTDOWN_EOF = 0,
201 SOCK_SHUTDOWN_POLLHUP = 1
202 } sock_shutdown_t;
204 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
206 static sock_shutdown_t sock_check_pollhup(void)
208 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
209 int fd[2], n;
210 struct pollfd pfd;
211 char dummy;
213 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
214 if ( shutdown( fd[0], 1 ) ) goto out;
216 pfd.fd = fd[1];
217 pfd.events = POLLIN;
218 pfd.revents = 0;
220 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
221 n = poll( &pfd, 1, 1 );
222 if ( n != 1 ) goto out; /* error or timeout */
223 if ( pfd.revents & POLLHUP )
224 ret = SOCK_SHUTDOWN_POLLHUP;
225 else if ( pfd.revents & POLLIN &&
226 read( fd[1], &dummy, 1 ) == 0 )
227 ret = SOCK_SHUTDOWN_EOF;
229 out:
230 close( fd[0] );
231 close( fd[1] );
232 return ret;
235 void sock_init(void)
237 sock_shutdown_type = sock_check_pollhup();
239 switch ( sock_shutdown_type )
241 case SOCK_SHUTDOWN_EOF:
242 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
243 break;
244 case SOCK_SHUTDOWN_POLLHUP:
245 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
246 break;
247 default:
248 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
249 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
253 static int sock_reselect( struct sock *sock )
255 int ev = sock_get_poll_events( sock->fd );
257 if (debug_level)
258 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
260 if (!sock->polling) /* FIXME: should find a better way to do this */
262 /* previously unconnected socket, is this reselect supposed to connect it? */
263 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
264 /* ok, it is, attach it to the wineserver's main poll loop */
265 sock->polling = 1;
266 allow_fd_caching( sock->fd );
268 /* update condition mask */
269 set_fd_events( sock->fd, ev );
270 return ev;
273 /* wake anybody waiting on the socket event or send the associated message */
274 static void sock_wake_up( struct sock *sock )
276 unsigned int events = sock->pmask & sock->mask;
277 int i;
279 if ( !events ) return;
281 if (sock->event)
283 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
284 set_event( sock->event );
286 if (sock->window)
288 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
289 for (i = 0; i < FD_MAX_EVENTS; i++)
291 int event = event_bitorder[i];
292 if (sock->pmask & (1 << event))
294 lparam_t lparam = (1 << event) | (sock_get_error(sock->errors[event]) << 16);
295 post_message( sock->window, sock->message, sock->wparam, lparam );
298 sock->pmask = 0;
299 sock_reselect( sock );
303 static inline int sock_error( struct fd *fd )
305 unsigned int optval = 0;
306 socklen_t optlen = sizeof(optval);
308 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
309 return optval;
312 static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
314 if ( sock->flags & WSA_FLAG_OVERLAPPED )
316 if ( event & (POLLIN|POLLPRI) && async_waiting( sock->read_q ) )
318 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
319 async_wake_up( sock->read_q, STATUS_ALERTED );
320 event &= ~(POLLIN|POLLPRI);
322 if ( event & POLLOUT && async_waiting( sock->write_q ) )
324 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
325 async_wake_up( sock->write_q, STATUS_ALERTED );
326 event &= ~POLLOUT;
328 if ( event & (POLLERR|POLLHUP) )
330 int status = sock_get_ntstatus( error );
332 if ( !(sock->state & FD_READ) )
333 async_wake_up( sock->read_q, status );
334 if ( !(sock->state & FD_WRITE) )
335 async_wake_up( sock->write_q, status );
338 return event;
341 static void sock_dispatch_events( struct sock *sock, int prevstate, int event, int error )
343 if (prevstate & FD_CONNECT)
345 sock->pmask |= FD_CONNECT;
346 sock->hmask |= FD_CONNECT;
347 sock->errors[FD_CONNECT_BIT] = error;
348 goto end;
350 if (prevstate & FD_WINE_LISTENING)
352 sock->pmask |= FD_ACCEPT;
353 sock->hmask |= FD_ACCEPT;
354 sock->errors[FD_ACCEPT_BIT] = error;
355 goto end;
358 if (event & POLLIN)
360 sock->pmask |= FD_READ;
361 sock->hmask |= FD_READ;
362 sock->errors[FD_READ_BIT] = 0;
365 if (event & POLLOUT)
367 sock->pmask |= FD_WRITE;
368 sock->hmask |= FD_WRITE;
369 sock->errors[FD_WRITE_BIT] = 0;
372 if (event & POLLPRI)
374 sock->pmask |= FD_OOB;
375 sock->hmask |= FD_OOB;
376 sock->errors[FD_OOB_BIT] = 0;
379 if (event & (POLLERR|POLLHUP))
381 sock->pmask |= FD_CLOSE;
382 sock->hmask |= FD_CLOSE;
383 sock->errors[FD_CLOSE_BIT] = error;
385 end:
386 sock_wake_up( sock );
389 static void sock_poll_event( struct fd *fd, int event )
391 struct sock *sock = get_fd_user( fd );
392 int hangup_seen = 0;
393 int prevstate = sock->state;
394 int error = 0;
396 assert( sock->obj.ops == &sock_ops );
397 if (debug_level)
398 fprintf(stderr, "socket %p select event: %x\n", sock, event);
400 /* we may change event later, remove from loop here */
401 if (event & (POLLERR|POLLHUP)) set_fd_events( sock->fd, -1 );
403 if (sock->state & FD_CONNECT)
405 if (event & (POLLERR|POLLHUP))
407 /* we didn't get connected? */
408 sock->state &= ~FD_CONNECT;
409 event &= ~POLLOUT;
410 error = sock_error( fd );
412 else if (event & POLLOUT)
414 /* we got connected */
415 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
416 sock->state &= ~FD_CONNECT;
417 sock->connect_time = current_time;
420 else if (sock->state & FD_WINE_LISTENING)
422 /* listening */
423 if (event & (POLLERR|POLLHUP))
424 error = sock_error( fd );
426 else
428 /* normal data flow */
429 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
431 char dummy;
432 int nr;
434 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
435 * has been closed, so we need to check for it explicitly here */
436 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
437 if ( nr == 0 )
439 hangup_seen = 1;
440 event &= ~POLLIN;
442 else if ( nr < 0 )
444 event &= ~POLLIN;
445 /* EAGAIN can happen if an async recv() falls between the server's poll()
446 call and the invocation of this routine */
447 if ( errno != EAGAIN )
449 error = errno;
450 event |= POLLERR;
451 if ( debug_level )
452 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
457 if ( (hangup_seen || event & (POLLHUP|POLLERR)) && (sock->state & (FD_READ|FD_WRITE)) )
459 error = error ? error : sock_error( fd );
460 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
461 sock->state &= ~FD_WRITE;
462 sock->state &= ~FD_READ;
464 if (debug_level)
465 fprintf(stderr, "socket %p aborted by error %d, event: %x\n", sock, error, event);
468 if (hangup_seen)
469 event |= POLLHUP;
472 event = sock_dispatch_asyncs( sock, event, error );
473 sock_dispatch_events( sock, prevstate, event, error );
475 /* if anyone is stupid enough to wait on the socket object itself,
476 * maybe we should wake them up too, just in case? */
477 wake_up( &sock->obj, 0 );
479 sock_reselect( sock );
482 static void sock_dump( struct object *obj, int verbose )
484 struct sock *sock = (struct sock *)obj;
485 assert( obj->ops == &sock_ops );
486 fprintf( stderr, "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
487 sock->fd, sock->state,
488 sock->mask, sock->pmask, sock->hmask );
491 static int sock_signaled( struct object *obj, struct wait_queue_entry *entry )
493 struct sock *sock = (struct sock *)obj;
494 assert( obj->ops == &sock_ops );
496 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
499 static int sock_get_poll_events( struct fd *fd )
501 struct sock *sock = get_fd_user( fd );
502 unsigned int mask = sock->mask & ~sock->hmask;
503 unsigned int smask = sock->state & mask;
504 int ev = 0;
506 assert( sock->obj.ops == &sock_ops );
508 if (sock->state & FD_CONNECT)
509 /* connecting, wait for writable */
510 return POLLOUT;
512 if ( async_queued( sock->read_q ) )
514 if ( async_waiting( sock->read_q ) ) ev |= POLLIN | POLLPRI;
516 else if (smask & FD_READ || (sock->state & FD_WINE_LISTENING && mask & FD_ACCEPT))
517 ev |= POLLIN | POLLPRI;
518 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
519 else if ( sock->type == SOCK_STREAM && sock->state & FD_READ && mask & FD_CLOSE &&
520 !(sock->hmask & FD_READ) )
521 ev |= POLLIN;
523 if ( async_queued( sock->write_q ) )
525 if ( async_waiting( sock->write_q ) ) ev |= POLLOUT;
527 else if (smask & FD_WRITE)
528 ev |= POLLOUT;
530 return ev;
533 static enum server_fd_type sock_get_fd_type( struct fd *fd )
535 return FD_TYPE_SOCKET;
538 obj_handle_t sock_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data, int blocking )
540 struct sock *sock = get_fd_user( fd );
541 obj_handle_t wait_handle = 0;
542 async_data_t new_data;
544 assert( sock->obj.ops == &sock_ops );
546 switch(code)
548 case WS_SIO_ADDRESS_LIST_CHANGE:
549 if (blocking)
551 if (!(wait_handle = alloc_wait_event( current->process ))) return 0;
552 new_data = *async_data;
553 new_data.event = wait_handle;
554 async_data = &new_data;
556 if (!sock_add_ifchange( sock, async_data ) && wait_handle)
558 close_handle( current->process, wait_handle );
559 return 0;
561 return wait_handle;
562 default:
563 set_error( STATUS_NOT_SUPPORTED );
564 return 0;
568 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
570 struct sock *sock = get_fd_user( fd );
571 struct async *async;
572 struct async_queue *queue;
574 assert( sock->obj.ops == &sock_ops );
576 switch (type)
578 case ASYNC_TYPE_READ:
579 if (!sock->read_q && !(sock->read_q = create_async_queue( sock->fd ))) return;
580 queue = sock->read_q;
581 break;
582 case ASYNC_TYPE_WRITE:
583 if (!sock->write_q && !(sock->write_q = create_async_queue( sock->fd ))) return;
584 queue = sock->write_q;
585 break;
586 default:
587 set_error( STATUS_INVALID_PARAMETER );
588 return;
591 if ( ( !( sock->state & (FD_READ|FD_CONNECT|FD_WINE_LISTENING) ) && type == ASYNC_TYPE_READ ) ||
592 ( !( sock->state & (FD_WRITE|FD_CONNECT) ) && type == ASYNC_TYPE_WRITE ) )
594 set_error( STATUS_PIPE_DISCONNECTED );
595 return;
598 if (!(async = create_async( current, queue, data ))) return;
599 release_object( async );
601 sock_reselect( sock );
603 set_error( STATUS_PENDING );
606 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
608 struct sock *sock = get_fd_user( fd );
609 sock_reselect( sock );
612 static void sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb )
614 struct sock *sock = get_fd_user( fd );
615 int n = 0;
616 assert( sock->obj.ops == &sock_ops );
618 n += async_wake_up_by( sock->read_q, process, thread, iosb, STATUS_CANCELLED );
619 n += async_wake_up_by( sock->write_q, process, thread, iosb, STATUS_CANCELLED );
620 if (!n && iosb)
621 set_error( STATUS_NOT_FOUND );
624 static struct fd *sock_get_fd( struct object *obj )
626 struct sock *sock = (struct sock *)obj;
627 return (struct fd *)grab_object( sock->fd );
630 static void sock_destroy( struct object *obj )
632 struct sock *sock = (struct sock *)obj;
633 assert( obj->ops == &sock_ops );
635 /* FIXME: special socket shutdown stuff? */
637 if ( sock->deferred )
638 release_object( sock->deferred );
640 free_async_queue( sock->read_q );
641 free_async_queue( sock->write_q );
642 sock_destroy_ifchange_q( sock );
643 if (sock->event) release_object( sock->event );
644 if (sock->fd)
646 /* shut the socket down to force pending poll() calls in the client to return */
647 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
648 release_object( sock->fd );
652 static void init_sock(struct sock *sock)
654 sock->state = 0;
655 sock->mask = 0;
656 sock->hmask = 0;
657 sock->pmask = 0;
658 sock->polling = 0;
659 sock->flags = 0;
660 sock->type = 0;
661 sock->family = 0;
662 sock->event = NULL;
663 sock->window = 0;
664 sock->message = 0;
665 sock->wparam = 0;
666 sock->connect_time = 0;
667 sock->deferred = NULL;
668 sock->read_q = NULL;
669 sock->write_q = NULL;
670 sock->ifchange_q = NULL;
671 sock->ifchange_obj = NULL;
672 memset( sock->errors, 0, sizeof(sock->errors) );
675 /* create a new and unconnected socket */
676 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
678 struct sock *sock;
679 int sockfd;
681 sockfd = socket( family, type, protocol );
682 if (debug_level)
683 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
684 if (sockfd == -1)
686 sock_set_error();
687 return NULL;
689 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
690 if (!(sock = alloc_object( &sock_ops )))
692 close( sockfd );
693 return NULL;
695 init_sock( sock );
696 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
697 sock->flags = flags;
698 sock->proto = protocol;
699 sock->type = type;
700 sock->family = family;
702 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
703 (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
705 release_object( sock );
706 return NULL;
708 sock_reselect( sock );
709 clear_error();
710 return &sock->obj;
713 /* accepts a socket and inits it */
714 static int accept_new_fd( struct sock *sock )
717 /* Try to accept(2). We can't be safe that this an already connected socket
718 * or that accept() is allowed on it. In those cases we will get -1/errno
719 * return.
721 int acceptfd;
722 struct sockaddr saddr;
723 socklen_t slen = sizeof(saddr);
724 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
725 if (acceptfd == -1)
727 sock_set_error();
728 return acceptfd;
731 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
732 return acceptfd;
735 /* accept a socket (creates a new fd) */
736 static struct sock *accept_socket( obj_handle_t handle )
738 struct sock *acceptsock;
739 struct sock *sock;
740 int acceptfd;
742 sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
743 if (!sock)
744 return NULL;
746 if ( sock->deferred )
748 acceptsock = sock->deferred;
749 sock->deferred = NULL;
751 else
753 if ((acceptfd = accept_new_fd( sock )) == -1)
755 release_object( sock );
756 return NULL;
758 if (!(acceptsock = alloc_object( &sock_ops )))
760 close( acceptfd );
761 release_object( sock );
762 return NULL;
765 init_sock( acceptsock );
766 /* newly created socket gets the same properties of the listening socket */
767 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
768 if (sock->state & FD_WINE_NONBLOCKING)
769 acceptsock->state |= FD_WINE_NONBLOCKING;
770 acceptsock->mask = sock->mask;
771 acceptsock->proto = sock->proto;
772 acceptsock->type = sock->type;
773 acceptsock->family = sock->family;
774 acceptsock->window = sock->window;
775 acceptsock->message = sock->message;
776 acceptsock->connect_time = current_time;
777 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
778 acceptsock->flags = sock->flags;
779 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
780 get_fd_options( sock->fd ) )))
782 release_object( acceptsock );
783 release_object( sock );
784 return NULL;
787 clear_error();
788 sock->pmask &= ~FD_ACCEPT;
789 sock->hmask &= ~FD_ACCEPT;
790 sock_reselect( sock );
791 release_object( sock );
792 return acceptsock;
795 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
797 int acceptfd;
798 struct fd *newfd;
799 if ( sock->deferred )
801 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
802 get_fd_options( acceptsock->fd ) );
803 if ( !newfd )
804 return FALSE;
806 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
808 release_object( sock->deferred );
809 sock->deferred = NULL;
811 else
813 if ((acceptfd = accept_new_fd( sock )) == -1)
814 return FALSE;
816 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
817 get_fd_options( acceptsock->fd ) )))
818 return FALSE;
821 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
822 acceptsock->hmask = 0;
823 acceptsock->pmask = 0;
824 acceptsock->polling = 0;
825 acceptsock->proto = sock->proto;
826 acceptsock->type = sock->type;
827 acceptsock->family = sock->family;
828 acceptsock->wparam = 0;
829 acceptsock->deferred = NULL;
830 acceptsock->connect_time = current_time;
831 fd_copy_completion( acceptsock->fd, newfd );
832 release_object( acceptsock->fd );
833 acceptsock->fd = newfd;
835 clear_error();
836 sock->pmask &= ~FD_ACCEPT;
837 sock->hmask &= ~FD_ACCEPT;
838 sock_reselect( sock );
840 return TRUE;
843 /* return an errno value mapped to a WSA error */
844 static int sock_get_error( int err )
846 switch (err)
848 case EINTR: return WSAEINTR;
849 case EBADF: return WSAEBADF;
850 case EPERM:
851 case EACCES: return WSAEACCES;
852 case EFAULT: return WSAEFAULT;
853 case EINVAL: return WSAEINVAL;
854 case EMFILE: return WSAEMFILE;
855 case EWOULDBLOCK: return WSAEWOULDBLOCK;
856 case EINPROGRESS: return WSAEINPROGRESS;
857 case EALREADY: return WSAEALREADY;
858 case ENOTSOCK: return WSAENOTSOCK;
859 case EDESTADDRREQ: return WSAEDESTADDRREQ;
860 case EMSGSIZE: return WSAEMSGSIZE;
861 case EPROTOTYPE: return WSAEPROTOTYPE;
862 case ENOPROTOOPT: return WSAENOPROTOOPT;
863 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
864 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
865 case EOPNOTSUPP: return WSAEOPNOTSUPP;
866 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
867 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
868 case EADDRINUSE: return WSAEADDRINUSE;
869 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
870 case ENETDOWN: return WSAENETDOWN;
871 case ENETUNREACH: return WSAENETUNREACH;
872 case ENETRESET: return WSAENETRESET;
873 case ECONNABORTED: return WSAECONNABORTED;
874 case EPIPE:
875 case ECONNRESET: return WSAECONNRESET;
876 case ENOBUFS: return WSAENOBUFS;
877 case EISCONN: return WSAEISCONN;
878 case ENOTCONN: return WSAENOTCONN;
879 case ESHUTDOWN: return WSAESHUTDOWN;
880 case ETOOMANYREFS: return WSAETOOMANYREFS;
881 case ETIMEDOUT: return WSAETIMEDOUT;
882 case ECONNREFUSED: return WSAECONNREFUSED;
883 case ELOOP: return WSAELOOP;
884 case ENAMETOOLONG: return WSAENAMETOOLONG;
885 case EHOSTDOWN: return WSAEHOSTDOWN;
886 case EHOSTUNREACH: return WSAEHOSTUNREACH;
887 case ENOTEMPTY: return WSAENOTEMPTY;
888 #ifdef EPROCLIM
889 case EPROCLIM: return WSAEPROCLIM;
890 #endif
891 #ifdef EUSERS
892 case EUSERS: return WSAEUSERS;
893 #endif
894 #ifdef EDQUOT
895 case EDQUOT: return WSAEDQUOT;
896 #endif
897 #ifdef ESTALE
898 case ESTALE: return WSAESTALE;
899 #endif
900 #ifdef EREMOTE
901 case EREMOTE: return WSAEREMOTE;
902 #endif
904 case 0: return 0;
905 default:
906 errno = err;
907 perror("wineserver: sock_get_error() can't map error");
908 return WSAEFAULT;
912 static int sock_get_ntstatus( int err )
914 switch ( err )
916 case EBADF: return STATUS_INVALID_HANDLE;
917 case EBUSY: return STATUS_DEVICE_BUSY;
918 case EPERM:
919 case EACCES: return STATUS_ACCESS_DENIED;
920 case EFAULT: return STATUS_NO_MEMORY;
921 case EINVAL: return STATUS_INVALID_PARAMETER;
922 case ENFILE:
923 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
924 case EWOULDBLOCK: return STATUS_CANT_WAIT;
925 case EINPROGRESS: return STATUS_PENDING;
926 case EALREADY: return STATUS_NETWORK_BUSY;
927 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
928 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
929 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
930 case EPROTONOSUPPORT:
931 case ESOCKTNOSUPPORT:
932 case EPFNOSUPPORT:
933 case EAFNOSUPPORT:
934 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
935 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
936 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
937 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
938 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
939 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
940 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
941 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
942 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
943 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
944 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
945 case ENETDOWN: return STATUS_NETWORK_BUSY;
946 case EPIPE:
947 case ECONNRESET: return STATUS_CONNECTION_RESET;
948 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
950 case 0: return STATUS_SUCCESS;
951 default:
952 errno = err;
953 perror("wineserver: sock_get_ntstatus() can't map error");
954 return STATUS_UNSUCCESSFUL;
958 /* set the last error depending on errno */
959 static void sock_set_error(void)
961 set_error( sock_get_ntstatus( errno ) );
964 /* add interface change notification to a socket */
965 static int sock_add_ifchange( struct sock *sock, const async_data_t *async_data )
967 struct async_queue *ifchange_q;
968 struct async *async;
970 if (!(ifchange_q = sock_get_ifchange_q( sock )))
971 return 0;
973 if (!(async = create_async( current, ifchange_q, async_data )))
975 if (!async_queued( ifchange_q ))
976 sock_destroy_ifchange_q( sock );
978 set_error( STATUS_NO_MEMORY );
979 return 0;
982 release_object( async );
983 set_error( STATUS_PENDING );
984 return 1;
987 #ifdef HAVE_LINUX_RTNETLINK_H
989 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
990 static struct object *ifchange_object;
992 static void ifchange_dump( struct object *obj, int verbose );
993 static struct fd *ifchange_get_fd( struct object *obj );
994 static void ifchange_destroy( struct object *obj );
996 static int ifchange_get_poll_events( struct fd *fd );
997 static void ifchange_poll_event( struct fd *fd, int event );
998 static void ifchange_reselect_async( struct fd *fd, struct async_queue *queue );
1000 struct ifchange
1002 struct object obj; /* object header */
1003 struct fd *fd; /* interface change file descriptor */
1004 struct list sockets; /* list of sockets to send interface change notifications */
1007 static const struct object_ops ifchange_ops =
1009 sizeof(struct ifchange), /* size */
1010 ifchange_dump, /* dump */
1011 no_get_type, /* get_type */
1012 add_queue, /* add_queue */
1013 NULL, /* remove_queue */
1014 NULL, /* signaled */
1015 no_satisfied, /* satisfied */
1016 no_signal, /* signal */
1017 ifchange_get_fd, /* get_fd */
1018 default_fd_map_access, /* map_access */
1019 default_get_sd, /* get_sd */
1020 default_set_sd, /* set_sd */
1021 no_lookup_name, /* lookup_name */
1022 no_open_file, /* open_file */
1023 no_close_handle, /* close_handle */
1024 ifchange_destroy /* destroy */
1027 static const struct fd_ops ifchange_fd_ops =
1029 ifchange_get_poll_events, /* get_poll_events */
1030 ifchange_poll_event, /* poll_event */
1031 NULL, /* get_fd_type */
1032 no_fd_read, /* read */
1033 no_fd_write, /* write */
1034 no_fd_flush, /* flush */
1035 no_fd_ioctl, /* ioctl */
1036 NULL, /* queue_async */
1037 ifchange_reselect_async, /* reselect_async */
1038 NULL /* cancel_async */
1041 static void ifchange_dump( struct object *obj, int verbose )
1043 assert( obj->ops == &ifchange_ops );
1044 fprintf( stderr, "Interface change\n" );
1047 static struct fd *ifchange_get_fd( struct object *obj )
1049 struct ifchange *ifchange = (struct ifchange *)obj;
1050 return (struct fd *)grab_object( ifchange->fd );
1053 static void ifchange_destroy( struct object *obj )
1055 struct ifchange *ifchange = (struct ifchange *)obj;
1056 assert( obj->ops == &ifchange_ops );
1058 release_object( ifchange->fd );
1060 /* reset the global ifchange object so that it will be recreated if it is needed again */
1061 assert( obj == ifchange_object );
1062 ifchange_object = NULL;
1065 static int ifchange_get_poll_events( struct fd *fd )
1067 return POLLIN;
1070 /* wake up all the sockets waiting for a change notification event */
1071 static void ifchange_wake_up( struct object *obj, unsigned int status )
1073 struct ifchange *ifchange = (struct ifchange *)obj;
1074 struct list *ptr, *next;
1075 assert( obj->ops == &ifchange_ops );
1076 assert( obj == ifchange_object );
1078 LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
1080 struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
1082 assert( sock->ifchange_q );
1083 async_wake_up( sock->ifchange_q, status ); /* issue ifchange notification for the socket */
1084 sock_destroy_ifchange_q( sock ); /* remove socket from list and decrement ifchange refcount */
1088 static void ifchange_poll_event( struct fd *fd, int event )
1090 struct object *ifchange = get_fd_user( fd );
1091 unsigned int status = STATUS_PENDING;
1092 char buffer[PIPE_BUF];
1093 int r;
1095 r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
1096 if (r < 0)
1098 if (errno == EWOULDBLOCK || errno == EAGAIN)
1099 return; /* retry when poll() says the socket is ready */
1100 status = sock_get_ntstatus( errno );
1102 else if (r > 0)
1104 struct nlmsghdr *nlh;
1106 for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
1108 if (nlh->nlmsg_type == NLMSG_DONE)
1109 break;
1110 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
1111 status = STATUS_SUCCESS;
1114 else status = STATUS_CANCELLED;
1116 if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
1119 static void ifchange_reselect_async( struct fd *fd, struct async_queue *queue )
1121 /* do nothing, this object is about to disappear */
1124 #endif
1126 /* we only need one of these interface notification objects, all of the sockets dependent upon
1127 * it will wake up when a notification event occurs */
1128 static struct object *get_ifchange( void )
1130 #ifdef HAVE_LINUX_RTNETLINK_H
1131 struct ifchange *ifchange;
1132 struct sockaddr_nl addr;
1133 int unix_fd;
1135 if (ifchange_object)
1137 /* increment the refcount for each socket that uses the ifchange object */
1138 return grab_object( ifchange_object );
1141 /* create the socket we need for processing interface change notifications */
1142 unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
1143 if (unix_fd == -1)
1145 sock_set_error();
1146 return NULL;
1148 fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
1149 memset( &addr, 0, sizeof(addr) );
1150 addr.nl_family = AF_NETLINK;
1151 addr.nl_groups = RTMGRP_IPV4_IFADDR;
1152 /* bind the socket to the special netlink kernel interface */
1153 if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
1155 close( unix_fd );
1156 sock_set_error();
1157 return NULL;
1159 if (!(ifchange = alloc_object( &ifchange_ops )))
1161 close( unix_fd );
1162 set_error( STATUS_NO_MEMORY );
1163 return NULL;
1165 list_init( &ifchange->sockets );
1166 if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
1168 release_object( ifchange );
1169 set_error( STATUS_NO_MEMORY );
1170 return NULL;
1172 set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
1174 /* the ifchange object is now successfully configured */
1175 ifchange_object = &ifchange->obj;
1176 return &ifchange->obj;
1177 #else
1178 set_error( STATUS_NOT_SUPPORTED );
1179 return NULL;
1180 #endif
1183 /* add the socket to the interface change notification list */
1184 static void ifchange_add_sock( struct object *obj, struct sock *sock )
1186 #ifdef HAVE_LINUX_RTNETLINK_H
1187 struct ifchange *ifchange = (struct ifchange *)obj;
1189 list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
1190 #endif
1193 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1194 static struct async_queue *sock_get_ifchange_q( struct sock *sock )
1196 struct object *ifchange;
1197 struct fd *fd;
1199 if (sock->ifchange_q) /* reuse existing ifchange_q for this socket */
1200 return sock->ifchange_q;
1202 if (!(ifchange = get_ifchange()))
1203 return NULL;
1205 /* create the ifchange notification queue */
1206 fd = ifchange->ops->get_fd( ifchange );
1207 sock->ifchange_q = create_async_queue( fd );
1208 release_object( fd );
1209 if (!sock->ifchange_q)
1211 release_object( ifchange );
1212 set_error( STATUS_NO_MEMORY );
1213 return NULL;
1216 /* add the socket to the ifchange notification list */
1217 ifchange_add_sock( ifchange, sock );
1218 sock->ifchange_obj = ifchange;
1219 return sock->ifchange_q;
1222 /* destroy an existing ifchange queue for a specific socket */
1223 static void sock_destroy_ifchange_q( struct sock *sock )
1225 if (sock->ifchange_q)
1227 list_remove( &sock->ifchange_entry );
1228 free_async_queue( sock->ifchange_q );
1229 sock->ifchange_q = NULL;
1230 release_object( sock->ifchange_obj );
1234 /* create a socket */
1235 DECL_HANDLER(create_socket)
1237 struct object *obj;
1239 reply->handle = 0;
1240 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
1242 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1243 release_object( obj );
1247 /* accept a socket */
1248 DECL_HANDLER(accept_socket)
1250 struct sock *sock;
1252 reply->handle = 0;
1253 if ((sock = accept_socket( req->lhandle )) != NULL)
1255 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
1256 sock->wparam = reply->handle; /* wparam for message is the socket handle */
1257 sock_reselect( sock );
1258 release_object( &sock->obj );
1262 /* accept a socket into an initialized socket */
1263 DECL_HANDLER(accept_into_socket)
1265 struct sock *sock, *acceptsock;
1266 const int all_attributes = FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|FILE_READ_DATA;
1268 if (!(sock = (struct sock *)get_handle_obj( current->process, req->lhandle,
1269 all_attributes, &sock_ops)))
1270 return;
1272 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, req->ahandle,
1273 all_attributes, &sock_ops)))
1275 release_object( sock );
1276 return;
1279 if (accept_into_socket( sock, acceptsock ))
1281 acceptsock->wparam = req->ahandle; /* wparam for message is the socket handle */
1282 sock_reselect( acceptsock );
1284 release_object( acceptsock );
1285 release_object( sock );
1288 /* set socket event parameters */
1289 DECL_HANDLER(set_socket_event)
1291 struct sock *sock;
1292 struct event *old_event;
1294 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1295 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
1296 old_event = sock->event;
1297 sock->mask = req->mask;
1298 sock->hmask &= ~req->mask; /* re-enable held events */
1299 sock->event = NULL;
1300 sock->window = req->window;
1301 sock->message = req->msg;
1302 sock->wparam = req->handle; /* wparam is the socket handle */
1303 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
1305 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
1307 sock_reselect( sock );
1309 sock->state |= FD_WINE_NONBLOCKING;
1311 /* if a network event is pending, signal the event object
1312 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1313 before a WSAEventSelect() was done on it.
1314 (when dealing with Asynchronous socket) */
1315 sock_wake_up( sock );
1317 if (old_event) release_object( old_event ); /* we're through with it */
1318 release_object( &sock->obj );
1321 /* get socket event parameters */
1322 DECL_HANDLER(get_socket_event)
1324 struct sock *sock;
1325 int i;
1326 int errors[FD_MAX_EVENTS];
1328 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1329 if (!sock)
1331 reply->mask = 0;
1332 reply->pmask = 0;
1333 reply->state = 0;
1334 return;
1336 reply->mask = sock->mask;
1337 reply->pmask = sock->pmask;
1338 reply->state = sock->state;
1339 for (i = 0; i < FD_MAX_EVENTS; i++)
1340 errors[i] = sock_get_ntstatus(sock->errors[i]);
1342 set_reply_data( errors, min( get_reply_max_size(), sizeof(errors) ));
1344 if (req->service)
1346 if (req->c_event)
1348 struct event *cevent = get_event_obj( current->process, req->c_event,
1349 EVENT_MODIFY_STATE );
1350 if (cevent)
1352 reset_event( cevent );
1353 release_object( cevent );
1356 sock->pmask = 0;
1357 sock_reselect( sock );
1359 release_object( &sock->obj );
1362 /* re-enable pending socket events */
1363 DECL_HANDLER(enable_socket_event)
1365 struct sock *sock;
1367 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
1368 FILE_WRITE_ATTRIBUTES, &sock_ops)))
1369 return;
1371 /* for event-based notification, windows erases stale events */
1372 sock->pmask &= ~req->mask;
1374 sock->hmask &= ~req->mask;
1375 sock->state |= req->sstate;
1376 sock->state &= ~req->cstate;
1377 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
1379 sock_reselect( sock );
1381 release_object( &sock->obj );
1384 DECL_HANDLER(set_socket_deferred)
1386 struct sock *sock, *acceptsock;
1388 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
1389 if ( !sock )
1390 return;
1392 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
1393 if ( !acceptsock )
1395 release_object( sock );
1396 return;
1398 sock->deferred = acceptsock;
1399 release_object( sock );
1402 DECL_HANDLER(get_socket_info)
1404 struct sock *sock;
1406 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1407 if (!sock) return;
1409 reply->family = sock->family;
1410 reply->type = sock->type;
1411 reply->protocol = sock->proto;
1413 release_object( &sock->obj );