msvcrt: _Gettnames() should respect user overrides.
[wine.git] / server / sock.c
blob84f54f6f0592d15b2e87413b60bd55202c45729a
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_signaled( struct object *obj, struct wait_queue_entry *entry );
124 static struct fd *sock_get_fd( struct object *obj );
125 static void sock_destroy( struct object *obj );
126 static struct object *sock_get_ifchange( struct sock *sock );
127 static void sock_release_ifchange( struct sock *sock );
129 static int sock_get_poll_events( struct fd *fd );
130 static void sock_poll_event( struct fd *fd, int event );
131 static enum server_fd_type sock_get_fd_type( struct fd *fd );
132 static int sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
133 static void sock_queue_async( struct fd *fd, struct async *async, int type, int count );
134 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
136 static int sock_get_ntstatus( int err );
137 static int sock_get_error( int err );
138 static void sock_set_error(void);
140 static const struct object_ops sock_ops =
142 sizeof(struct sock), /* size */
143 sock_dump, /* dump */
144 no_get_type, /* get_type */
145 add_queue, /* add_queue */
146 remove_queue, /* remove_queue */
147 sock_signaled, /* signaled */
148 no_satisfied, /* satisfied */
149 no_signal, /* signal */
150 sock_get_fd, /* get_fd */
151 default_fd_map_access, /* map_access */
152 default_get_sd, /* get_sd */
153 default_set_sd, /* set_sd */
154 no_lookup_name, /* lookup_name */
155 no_link_name, /* link_name */
156 NULL, /* unlink_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 no_fd_get_file_info, /* get_file_info */
171 no_fd_get_volume_info, /* get_volume_info */
172 sock_ioctl, /* ioctl */
173 sock_queue_async, /* queue_async */
174 sock_reselect_async /* reselect_async */
178 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
179 * we post messages if there are multiple events. Used to send
180 * messages. The problem is if there is both a FD_CONNECT event and,
181 * say, an FD_READ event available on the same socket, we want to
182 * notify the app of the connect event first. Otherwise it may
183 * discard the read event because it thinks it hasn't connected yet.
185 static const int event_bitorder[FD_MAX_EVENTS] =
187 FD_CONNECT_BIT,
188 FD_ACCEPT_BIT,
189 FD_OOB_BIT,
190 FD_WRITE_BIT,
191 FD_READ_BIT,
192 FD_CLOSE_BIT,
193 6, 7, 8, 9 /* leftovers */
196 /* Flags that make sense only for SOCK_STREAM sockets */
197 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
199 typedef enum {
200 SOCK_SHUTDOWN_ERROR = -1,
201 SOCK_SHUTDOWN_EOF = 0,
202 SOCK_SHUTDOWN_POLLHUP = 1
203 } sock_shutdown_t;
205 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
207 static sock_shutdown_t sock_check_pollhup(void)
209 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
210 int fd[2], n;
211 struct pollfd pfd;
212 char dummy;
214 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
215 if ( shutdown( fd[0], 1 ) ) goto out;
217 pfd.fd = fd[1];
218 pfd.events = POLLIN;
219 pfd.revents = 0;
221 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
222 n = poll( &pfd, 1, 1 );
223 if ( n != 1 ) goto out; /* error or timeout */
224 if ( pfd.revents & POLLHUP )
225 ret = SOCK_SHUTDOWN_POLLHUP;
226 else if ( pfd.revents & POLLIN &&
227 read( fd[1], &dummy, 1 ) == 0 )
228 ret = SOCK_SHUTDOWN_EOF;
230 out:
231 close( fd[0] );
232 close( fd[1] );
233 return ret;
236 void sock_init(void)
238 sock_shutdown_type = sock_check_pollhup();
240 switch ( sock_shutdown_type )
242 case SOCK_SHUTDOWN_EOF:
243 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
244 break;
245 case SOCK_SHUTDOWN_POLLHUP:
246 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
247 break;
248 default:
249 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
250 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
254 static int sock_reselect( struct sock *sock )
256 int ev = sock_get_poll_events( sock->fd );
258 if (debug_level)
259 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
261 if (!sock->polling) /* FIXME: should find a better way to do this */
263 /* previously unconnected socket, is this reselect supposed to connect it? */
264 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
265 /* ok, it is, attach it to the wineserver's main poll loop */
266 sock->polling = 1;
267 allow_fd_caching( sock->fd );
269 /* update condition mask */
270 set_fd_events( sock->fd, ev );
271 return ev;
274 /* wake anybody waiting on the socket event or send the associated message */
275 static void sock_wake_up( struct sock *sock )
277 unsigned int events = sock->pmask & sock->mask;
278 int i;
280 if ( !events ) return;
282 if (sock->event)
284 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
285 set_event( sock->event );
287 if (sock->window)
289 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
290 for (i = 0; i < FD_MAX_EVENTS; i++)
292 int event = event_bitorder[i];
293 if (sock->pmask & (1 << event))
295 lparam_t lparam = (1 << event) | (sock_get_error(sock->errors[event]) << 16);
296 post_message( sock->window, sock->message, sock->wparam, lparam );
299 sock->pmask = 0;
300 sock_reselect( sock );
304 static inline int sock_error( struct fd *fd )
306 unsigned int optval = 0;
307 socklen_t optlen = sizeof(optval);
309 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
310 return optval;
313 static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
315 if ( sock->flags & WSA_FLAG_OVERLAPPED )
317 if (event & (POLLIN|POLLPRI) && async_waiting( &sock->read_q ))
319 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
320 async_wake_up( &sock->read_q, STATUS_ALERTED );
321 event &= ~(POLLIN|POLLPRI);
323 if (event & POLLOUT && async_waiting( &sock->write_q ))
325 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
326 async_wake_up( &sock->write_q, STATUS_ALERTED );
327 event &= ~POLLOUT;
329 if ( event & (POLLERR|POLLHUP) )
331 int status = sock_get_ntstatus( error );
333 if ( !(sock->state & FD_READ) )
334 async_wake_up( &sock->read_q, status );
335 if ( !(sock->state & FD_WRITE) )
336 async_wake_up( &sock->write_q, status );
339 return event;
342 static void sock_dispatch_events( struct sock *sock, int prevstate, int event, int error )
344 if (prevstate & FD_CONNECT)
346 sock->pmask |= FD_CONNECT;
347 sock->hmask |= FD_CONNECT;
348 sock->errors[FD_CONNECT_BIT] = error;
349 goto end;
351 if (prevstate & FD_WINE_LISTENING)
353 sock->pmask |= FD_ACCEPT;
354 sock->hmask |= FD_ACCEPT;
355 sock->errors[FD_ACCEPT_BIT] = error;
356 goto end;
359 if (event & POLLIN)
361 sock->pmask |= FD_READ;
362 sock->hmask |= FD_READ;
363 sock->errors[FD_READ_BIT] = 0;
366 if (event & POLLOUT)
368 sock->pmask |= FD_WRITE;
369 sock->hmask |= FD_WRITE;
370 sock->errors[FD_WRITE_BIT] = 0;
373 if (event & POLLPRI)
375 sock->pmask |= FD_OOB;
376 sock->hmask |= FD_OOB;
377 sock->errors[FD_OOB_BIT] = 0;
380 if (event & (POLLERR|POLLHUP))
382 sock->pmask |= FD_CLOSE;
383 sock->hmask |= FD_CLOSE;
384 sock->errors[FD_CLOSE_BIT] = error;
386 end:
387 sock_wake_up( sock );
390 static void sock_poll_event( struct fd *fd, int event )
392 struct sock *sock = get_fd_user( fd );
393 int hangup_seen = 0;
394 int prevstate = sock->state;
395 int error = 0;
397 assert( sock->obj.ops == &sock_ops );
398 if (debug_level)
399 fprintf(stderr, "socket %p select event: %x\n", sock, event);
401 /* we may change event later, remove from loop here */
402 if (event & (POLLERR|POLLHUP)) set_fd_events( sock->fd, -1 );
404 if (sock->state & FD_CONNECT)
406 if (event & (POLLERR|POLLHUP))
408 /* we didn't get connected? */
409 sock->state &= ~FD_CONNECT;
410 event &= ~POLLOUT;
411 error = sock_error( fd );
413 else if (event & POLLOUT)
415 /* we got connected */
416 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
417 sock->state &= ~FD_CONNECT;
418 sock->connect_time = current_time;
421 else if (sock->state & FD_WINE_LISTENING)
423 /* listening */
424 if (event & (POLLERR|POLLHUP))
425 error = sock_error( fd );
427 else
429 /* normal data flow */
430 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
432 char dummy;
433 int nr;
435 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
436 * has been closed, so we need to check for it explicitly here */
437 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
438 if ( nr == 0 )
440 hangup_seen = 1;
441 event &= ~POLLIN;
443 else if ( nr < 0 )
445 event &= ~POLLIN;
446 /* EAGAIN can happen if an async recv() falls between the server's poll()
447 call and the invocation of this routine */
448 if ( errno != EAGAIN )
450 error = errno;
451 event |= POLLERR;
452 if ( debug_level )
453 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
458 if ( (hangup_seen || event & (POLLHUP|POLLERR)) && (sock->state & (FD_READ|FD_WRITE)) )
460 error = error ? error : sock_error( fd );
461 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
462 sock->state &= ~FD_WRITE;
463 sock->state &= ~FD_READ;
465 if (debug_level)
466 fprintf(stderr, "socket %p aborted by error %d, event: %x\n", sock, error, event);
469 if (hangup_seen)
470 event |= POLLHUP;
473 event = sock_dispatch_asyncs( sock, event, error );
474 sock_dispatch_events( sock, prevstate, event, error );
476 /* if anyone is stupid enough to wait on the socket object itself,
477 * maybe we should wake them up too, just in case? */
478 wake_up( &sock->obj, 0 );
480 sock_reselect( sock );
483 static void sock_dump( struct object *obj, int verbose )
485 struct sock *sock = (struct sock *)obj;
486 assert( obj->ops == &sock_ops );
487 fprintf( stderr, "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
488 sock->fd, sock->state,
489 sock->mask, sock->pmask, sock->hmask );
492 static int sock_signaled( struct object *obj, struct wait_queue_entry *entry )
494 struct sock *sock = (struct sock *)obj;
495 assert( obj->ops == &sock_ops );
497 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
500 static int sock_get_poll_events( struct fd *fd )
502 struct sock *sock = get_fd_user( fd );
503 unsigned int mask = sock->mask & ~sock->hmask;
504 unsigned int smask = sock->state & mask;
505 int ev = 0;
507 assert( sock->obj.ops == &sock_ops );
509 if (sock->state & FD_CONNECT)
510 /* connecting, wait for writable */
511 return POLLOUT;
513 if (async_queued( &sock->read_q ))
515 if (async_waiting( &sock->read_q )) ev |= POLLIN | POLLPRI;
517 else if (smask & FD_READ || (sock->state & FD_WINE_LISTENING && mask & FD_ACCEPT))
518 ev |= POLLIN | POLLPRI;
519 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
520 else if ( sock->type == SOCK_STREAM && sock->state & FD_READ && mask & FD_CLOSE &&
521 !(sock->hmask & FD_READ) )
522 ev |= POLLIN;
524 if (async_queued( &sock->write_q ))
526 if (async_waiting( &sock->write_q )) ev |= POLLOUT;
528 else if (smask & FD_WRITE)
529 ev |= POLLOUT;
531 return ev;
534 static enum server_fd_type sock_get_fd_type( struct fd *fd )
536 return FD_TYPE_SOCKET;
539 static int sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
541 struct sock *sock = get_fd_user( fd );
543 assert( sock->obj.ops == &sock_ops );
545 switch(code)
547 case WS_SIO_ADDRESS_LIST_CHANGE:
548 if ((sock->state & FD_WINE_NONBLOCKING) && async_is_blocking( async ))
550 set_error( STATUS_CANT_WAIT );
551 return 0;
553 if (!sock_get_ifchange( sock )) return 0;
554 queue_async( &sock->ifchange_q, async );
555 set_error( STATUS_PENDING );
556 return 1;
557 default:
558 set_error( STATUS_NOT_SUPPORTED );
559 return 0;
563 static void sock_queue_async( struct fd *fd, struct async *async, int type, int count )
565 struct sock *sock = get_fd_user( fd );
566 struct async_queue *queue;
568 assert( sock->obj.ops == &sock_ops );
570 switch (type)
572 case ASYNC_TYPE_READ:
573 queue = &sock->read_q;
574 break;
575 case ASYNC_TYPE_WRITE:
576 queue = &sock->write_q;
577 break;
578 default:
579 set_error( STATUS_INVALID_PARAMETER );
580 return;
583 if ( ( !( sock->state & (FD_READ|FD_CONNECT|FD_WINE_LISTENING) ) && type == ASYNC_TYPE_READ ) ||
584 ( !( sock->state & (FD_WRITE|FD_CONNECT) ) && type == ASYNC_TYPE_WRITE ) )
586 set_error( STATUS_PIPE_DISCONNECTED );
587 return;
590 queue_async( queue, async );
591 sock_reselect( sock );
593 set_error( STATUS_PENDING );
596 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
598 struct sock *sock = get_fd_user( fd );
599 /* ignore reselect on ifchange queue */
600 if (&sock->ifchange_q != queue)
601 sock_reselect( sock );
604 static struct fd *sock_get_fd( struct object *obj )
606 struct sock *sock = (struct sock *)obj;
607 return (struct fd *)grab_object( sock->fd );
610 static void sock_destroy( struct object *obj )
612 struct sock *sock = (struct sock *)obj;
613 assert( obj->ops == &sock_ops );
615 /* FIXME: special socket shutdown stuff? */
617 if ( sock->deferred )
618 release_object( sock->deferred );
620 async_wake_up( &sock->ifchange_q, STATUS_CANCELLED );
621 sock_release_ifchange( sock );
622 free_async_queue( &sock->read_q );
623 free_async_queue( &sock->write_q );
624 free_async_queue( &sock->ifchange_q );
625 if (sock->event) release_object( sock->event );
626 if (sock->fd)
628 /* shut the socket down to force pending poll() calls in the client to return */
629 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
630 release_object( sock->fd );
634 static void init_sock(struct sock *sock)
636 sock->state = 0;
637 sock->mask = 0;
638 sock->hmask = 0;
639 sock->pmask = 0;
640 sock->polling = 0;
641 sock->flags = 0;
642 sock->type = 0;
643 sock->family = 0;
644 sock->event = NULL;
645 sock->window = 0;
646 sock->message = 0;
647 sock->wparam = 0;
648 sock->connect_time = 0;
649 sock->deferred = NULL;
650 sock->ifchange_obj = NULL;
651 init_async_queue( &sock->read_q );
652 init_async_queue( &sock->write_q );
653 init_async_queue( &sock->ifchange_q );
654 memset( sock->errors, 0, sizeof(sock->errors) );
657 /* create a new and unconnected socket */
658 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
660 struct sock *sock;
661 int sockfd;
663 sockfd = socket( family, type, protocol );
664 if (debug_level)
665 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
666 if (sockfd == -1)
668 sock_set_error();
669 return NULL;
671 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
672 if (!(sock = alloc_object( &sock_ops )))
674 close( sockfd );
675 return NULL;
677 init_sock( sock );
678 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
679 sock->flags = flags;
680 sock->proto = protocol;
681 sock->type = type;
682 sock->family = family;
684 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
685 (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
687 release_object( sock );
688 return NULL;
690 sock_reselect( sock );
691 clear_error();
692 return &sock->obj;
695 /* accepts a socket and inits it */
696 static int accept_new_fd( struct sock *sock )
699 /* Try to accept(2). We can't be safe that this an already connected socket
700 * or that accept() is allowed on it. In those cases we will get -1/errno
701 * return.
703 int acceptfd;
704 struct sockaddr saddr;
705 socklen_t slen = sizeof(saddr);
706 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
707 if (acceptfd == -1)
709 sock_set_error();
710 return acceptfd;
713 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
714 return acceptfd;
717 /* accept a socket (creates a new fd) */
718 static struct sock *accept_socket( obj_handle_t handle )
720 struct sock *acceptsock;
721 struct sock *sock;
722 int acceptfd;
724 sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
725 if (!sock)
726 return NULL;
728 if ( sock->deferred )
730 acceptsock = sock->deferred;
731 sock->deferred = NULL;
733 else
735 if ((acceptfd = accept_new_fd( sock )) == -1)
737 release_object( sock );
738 return NULL;
740 if (!(acceptsock = alloc_object( &sock_ops )))
742 close( acceptfd );
743 release_object( sock );
744 return NULL;
747 init_sock( acceptsock );
748 /* newly created socket gets the same properties of the listening socket */
749 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
750 if (sock->state & FD_WINE_NONBLOCKING)
751 acceptsock->state |= FD_WINE_NONBLOCKING;
752 acceptsock->mask = sock->mask;
753 acceptsock->proto = sock->proto;
754 acceptsock->type = sock->type;
755 acceptsock->family = sock->family;
756 acceptsock->window = sock->window;
757 acceptsock->message = sock->message;
758 acceptsock->connect_time = current_time;
759 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
760 acceptsock->flags = sock->flags;
761 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
762 get_fd_options( sock->fd ) )))
764 release_object( acceptsock );
765 release_object( sock );
766 return NULL;
769 clear_error();
770 sock->pmask &= ~FD_ACCEPT;
771 sock->hmask &= ~FD_ACCEPT;
772 sock_reselect( sock );
773 release_object( sock );
774 return acceptsock;
777 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
779 int acceptfd;
780 struct fd *newfd;
781 if ( sock->deferred )
783 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
784 get_fd_options( acceptsock->fd ) );
785 if ( !newfd )
786 return FALSE;
788 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
790 release_object( sock->deferred );
791 sock->deferred = NULL;
793 else
795 if ((acceptfd = accept_new_fd( sock )) == -1)
796 return FALSE;
798 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
799 get_fd_options( acceptsock->fd ) )))
800 return FALSE;
803 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
804 acceptsock->hmask = 0;
805 acceptsock->pmask = 0;
806 acceptsock->polling = 0;
807 acceptsock->proto = sock->proto;
808 acceptsock->type = sock->type;
809 acceptsock->family = sock->family;
810 acceptsock->wparam = 0;
811 acceptsock->deferred = NULL;
812 acceptsock->connect_time = current_time;
813 fd_copy_completion( acceptsock->fd, newfd );
814 release_object( acceptsock->fd );
815 acceptsock->fd = newfd;
817 clear_error();
818 sock->pmask &= ~FD_ACCEPT;
819 sock->hmask &= ~FD_ACCEPT;
820 sock_reselect( sock );
822 return TRUE;
825 /* return an errno value mapped to a WSA error */
826 static int sock_get_error( int err )
828 switch (err)
830 case EINTR: return WSAEINTR;
831 case EBADF: return WSAEBADF;
832 case EPERM:
833 case EACCES: return WSAEACCES;
834 case EFAULT: return WSAEFAULT;
835 case EINVAL: return WSAEINVAL;
836 case EMFILE: return WSAEMFILE;
837 case EWOULDBLOCK: return WSAEWOULDBLOCK;
838 case EINPROGRESS: return WSAEINPROGRESS;
839 case EALREADY: return WSAEALREADY;
840 case ENOTSOCK: return WSAENOTSOCK;
841 case EDESTADDRREQ: return WSAEDESTADDRREQ;
842 case EMSGSIZE: return WSAEMSGSIZE;
843 case EPROTOTYPE: return WSAEPROTOTYPE;
844 case ENOPROTOOPT: return WSAENOPROTOOPT;
845 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
846 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
847 case EOPNOTSUPP: return WSAEOPNOTSUPP;
848 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
849 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
850 case EADDRINUSE: return WSAEADDRINUSE;
851 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
852 case ENETDOWN: return WSAENETDOWN;
853 case ENETUNREACH: return WSAENETUNREACH;
854 case ENETRESET: return WSAENETRESET;
855 case ECONNABORTED: return WSAECONNABORTED;
856 case EPIPE:
857 case ECONNRESET: return WSAECONNRESET;
858 case ENOBUFS: return WSAENOBUFS;
859 case EISCONN: return WSAEISCONN;
860 case ENOTCONN: return WSAENOTCONN;
861 case ESHUTDOWN: return WSAESHUTDOWN;
862 case ETOOMANYREFS: return WSAETOOMANYREFS;
863 case ETIMEDOUT: return WSAETIMEDOUT;
864 case ECONNREFUSED: return WSAECONNREFUSED;
865 case ELOOP: return WSAELOOP;
866 case ENAMETOOLONG: return WSAENAMETOOLONG;
867 case EHOSTDOWN: return WSAEHOSTDOWN;
868 case EHOSTUNREACH: return WSAEHOSTUNREACH;
869 case ENOTEMPTY: return WSAENOTEMPTY;
870 #ifdef EPROCLIM
871 case EPROCLIM: return WSAEPROCLIM;
872 #endif
873 #ifdef EUSERS
874 case EUSERS: return WSAEUSERS;
875 #endif
876 #ifdef EDQUOT
877 case EDQUOT: return WSAEDQUOT;
878 #endif
879 #ifdef ESTALE
880 case ESTALE: return WSAESTALE;
881 #endif
882 #ifdef EREMOTE
883 case EREMOTE: return WSAEREMOTE;
884 #endif
886 case 0: return 0;
887 default:
888 errno = err;
889 perror("wineserver: sock_get_error() can't map error");
890 return WSAEFAULT;
894 static int sock_get_ntstatus( int err )
896 switch ( err )
898 case EBADF: return STATUS_INVALID_HANDLE;
899 case EBUSY: return STATUS_DEVICE_BUSY;
900 case EPERM:
901 case EACCES: return STATUS_ACCESS_DENIED;
902 case EFAULT: return STATUS_NO_MEMORY;
903 case EINVAL: return STATUS_INVALID_PARAMETER;
904 case ENFILE:
905 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
906 case EWOULDBLOCK: return STATUS_CANT_WAIT;
907 case EINPROGRESS: return STATUS_PENDING;
908 case EALREADY: return STATUS_NETWORK_BUSY;
909 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
910 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
911 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
912 case EPROTONOSUPPORT:
913 case ESOCKTNOSUPPORT:
914 case EPFNOSUPPORT:
915 case EAFNOSUPPORT:
916 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
917 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
918 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
919 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
920 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
921 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
922 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
923 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
924 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
925 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
926 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
927 case ENETDOWN: return STATUS_NETWORK_BUSY;
928 case EPIPE:
929 case ECONNRESET: return STATUS_CONNECTION_RESET;
930 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
932 case 0: return STATUS_SUCCESS;
933 default:
934 errno = err;
935 perror("wineserver: sock_get_ntstatus() can't map error");
936 return STATUS_UNSUCCESSFUL;
940 /* set the last error depending on errno */
941 static void sock_set_error(void)
943 set_error( sock_get_ntstatus( errno ) );
946 #ifdef HAVE_LINUX_RTNETLINK_H
948 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
949 static struct object *ifchange_object;
951 static void ifchange_dump( struct object *obj, int verbose );
952 static struct fd *ifchange_get_fd( struct object *obj );
953 static void ifchange_destroy( struct object *obj );
955 static int ifchange_get_poll_events( struct fd *fd );
956 static void ifchange_poll_event( struct fd *fd, int event );
958 struct ifchange
960 struct object obj; /* object header */
961 struct fd *fd; /* interface change file descriptor */
962 struct list sockets; /* list of sockets to send interface change notifications */
965 static const struct object_ops ifchange_ops =
967 sizeof(struct ifchange), /* size */
968 ifchange_dump, /* dump */
969 no_get_type, /* get_type */
970 add_queue, /* add_queue */
971 NULL, /* remove_queue */
972 NULL, /* signaled */
973 no_satisfied, /* satisfied */
974 no_signal, /* signal */
975 ifchange_get_fd, /* get_fd */
976 default_fd_map_access, /* map_access */
977 default_get_sd, /* get_sd */
978 default_set_sd, /* set_sd */
979 no_lookup_name, /* lookup_name */
980 no_link_name, /* link_name */
981 NULL, /* unlink_name */
982 no_open_file, /* open_file */
983 no_close_handle, /* close_handle */
984 ifchange_destroy /* destroy */
987 static const struct fd_ops ifchange_fd_ops =
989 ifchange_get_poll_events, /* get_poll_events */
990 ifchange_poll_event, /* poll_event */
991 NULL, /* get_fd_type */
992 no_fd_read, /* read */
993 no_fd_write, /* write */
994 no_fd_flush, /* flush */
995 no_fd_get_file_info, /* get_file_info */
996 no_fd_get_volume_info, /* get_volume_info */
997 no_fd_ioctl, /* ioctl */
998 NULL, /* queue_async */
999 NULL /* reselect_async */
1002 static void ifchange_dump( struct object *obj, int verbose )
1004 assert( obj->ops == &ifchange_ops );
1005 fprintf( stderr, "Interface change\n" );
1008 static struct fd *ifchange_get_fd( struct object *obj )
1010 struct ifchange *ifchange = (struct ifchange *)obj;
1011 return (struct fd *)grab_object( ifchange->fd );
1014 static void ifchange_destroy( struct object *obj )
1016 struct ifchange *ifchange = (struct ifchange *)obj;
1017 assert( obj->ops == &ifchange_ops );
1019 release_object( ifchange->fd );
1021 /* reset the global ifchange object so that it will be recreated if it is needed again */
1022 assert( obj == ifchange_object );
1023 ifchange_object = NULL;
1026 static int ifchange_get_poll_events( struct fd *fd )
1028 return POLLIN;
1031 /* wake up all the sockets waiting for a change notification event */
1032 static void ifchange_wake_up( struct object *obj, unsigned int status )
1034 struct ifchange *ifchange = (struct ifchange *)obj;
1035 struct list *ptr, *next;
1036 assert( obj->ops == &ifchange_ops );
1037 assert( obj == ifchange_object );
1039 LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
1041 struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
1043 assert( sock->ifchange_obj );
1044 async_wake_up( &sock->ifchange_q, status ); /* issue ifchange notification for the socket */
1045 sock_release_ifchange( sock ); /* remove socket from list and decrement ifchange refcount */
1049 static void ifchange_poll_event( struct fd *fd, int event )
1051 struct object *ifchange = get_fd_user( fd );
1052 unsigned int status = STATUS_PENDING;
1053 char buffer[PIPE_BUF];
1054 int r;
1056 r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
1057 if (r < 0)
1059 if (errno == EWOULDBLOCK || (EWOULDBLOCK != EAGAIN && errno == EAGAIN))
1060 return; /* retry when poll() says the socket is ready */
1061 status = sock_get_ntstatus( errno );
1063 else if (r > 0)
1065 struct nlmsghdr *nlh;
1067 for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
1069 if (nlh->nlmsg_type == NLMSG_DONE)
1070 break;
1071 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
1072 status = STATUS_SUCCESS;
1075 else status = STATUS_CANCELLED;
1077 if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
1080 #endif
1082 /* we only need one of these interface notification objects, all of the sockets dependent upon
1083 * it will wake up when a notification event occurs */
1084 static struct object *get_ifchange( void )
1086 #ifdef HAVE_LINUX_RTNETLINK_H
1087 struct ifchange *ifchange;
1088 struct sockaddr_nl addr;
1089 int unix_fd;
1091 if (ifchange_object)
1093 /* increment the refcount for each socket that uses the ifchange object */
1094 return grab_object( ifchange_object );
1097 /* create the socket we need for processing interface change notifications */
1098 unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
1099 if (unix_fd == -1)
1101 sock_set_error();
1102 return NULL;
1104 fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
1105 memset( &addr, 0, sizeof(addr) );
1106 addr.nl_family = AF_NETLINK;
1107 addr.nl_groups = RTMGRP_IPV4_IFADDR;
1108 /* bind the socket to the special netlink kernel interface */
1109 if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
1111 close( unix_fd );
1112 sock_set_error();
1113 return NULL;
1115 if (!(ifchange = alloc_object( &ifchange_ops )))
1117 close( unix_fd );
1118 set_error( STATUS_NO_MEMORY );
1119 return NULL;
1121 list_init( &ifchange->sockets );
1122 if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
1124 release_object( ifchange );
1125 set_error( STATUS_NO_MEMORY );
1126 return NULL;
1128 set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
1130 /* the ifchange object is now successfully configured */
1131 ifchange_object = &ifchange->obj;
1132 return &ifchange->obj;
1133 #else
1134 set_error( STATUS_NOT_SUPPORTED );
1135 return NULL;
1136 #endif
1139 /* add the socket to the interface change notification list */
1140 static void ifchange_add_sock( struct object *obj, struct sock *sock )
1142 #ifdef HAVE_LINUX_RTNETLINK_H
1143 struct ifchange *ifchange = (struct ifchange *)obj;
1145 list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
1146 #endif
1149 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1150 static struct object *sock_get_ifchange( struct sock *sock )
1152 struct object *ifchange;
1154 if (sock->ifchange_obj) /* reuse existing ifchange_obj for this socket */
1155 return sock->ifchange_obj;
1157 if (!(ifchange = get_ifchange()))
1158 return NULL;
1160 /* add the socket to the ifchange notification list */
1161 ifchange_add_sock( ifchange, sock );
1162 sock->ifchange_obj = ifchange;
1163 return ifchange;
1166 /* destroy an existing ifchange queue for a specific socket */
1167 static void sock_release_ifchange( struct sock *sock )
1169 if (sock->ifchange_obj)
1171 list_remove( &sock->ifchange_entry );
1172 release_object( sock->ifchange_obj );
1173 sock->ifchange_obj = NULL;
1177 /* create a socket */
1178 DECL_HANDLER(create_socket)
1180 struct object *obj;
1182 reply->handle = 0;
1183 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
1185 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1186 release_object( obj );
1190 /* accept a socket */
1191 DECL_HANDLER(accept_socket)
1193 struct sock *sock;
1195 reply->handle = 0;
1196 if ((sock = accept_socket( req->lhandle )) != NULL)
1198 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
1199 sock->wparam = reply->handle; /* wparam for message is the socket handle */
1200 sock_reselect( sock );
1201 release_object( &sock->obj );
1205 /* accept a socket into an initialized socket */
1206 DECL_HANDLER(accept_into_socket)
1208 struct sock *sock, *acceptsock;
1209 const int all_attributes = FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|FILE_READ_DATA;
1211 if (!(sock = (struct sock *)get_handle_obj( current->process, req->lhandle,
1212 all_attributes, &sock_ops)))
1213 return;
1215 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, req->ahandle,
1216 all_attributes, &sock_ops)))
1218 release_object( sock );
1219 return;
1222 if (accept_into_socket( sock, acceptsock ))
1224 acceptsock->wparam = req->ahandle; /* wparam for message is the socket handle */
1225 sock_reselect( acceptsock );
1227 release_object( acceptsock );
1228 release_object( sock );
1231 /* set socket event parameters */
1232 DECL_HANDLER(set_socket_event)
1234 struct sock *sock;
1235 struct event *old_event;
1237 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1238 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
1239 old_event = sock->event;
1240 sock->mask = req->mask;
1241 sock->hmask &= ~req->mask; /* re-enable held events */
1242 sock->event = NULL;
1243 sock->window = req->window;
1244 sock->message = req->msg;
1245 sock->wparam = req->handle; /* wparam is the socket handle */
1246 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
1248 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
1250 sock_reselect( sock );
1252 sock->state |= FD_WINE_NONBLOCKING;
1254 /* if a network event is pending, signal the event object
1255 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1256 before a WSAEventSelect() was done on it.
1257 (when dealing with Asynchronous socket) */
1258 sock_wake_up( sock );
1260 if (old_event) release_object( old_event ); /* we're through with it */
1261 release_object( &sock->obj );
1264 /* get socket event parameters */
1265 DECL_HANDLER(get_socket_event)
1267 struct sock *sock;
1268 int i;
1269 int errors[FD_MAX_EVENTS];
1271 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1272 if (!sock)
1274 reply->mask = 0;
1275 reply->pmask = 0;
1276 reply->state = 0;
1277 return;
1279 reply->mask = sock->mask;
1280 reply->pmask = sock->pmask;
1281 reply->state = sock->state;
1282 for (i = 0; i < FD_MAX_EVENTS; i++)
1283 errors[i] = sock_get_ntstatus(sock->errors[i]);
1285 set_reply_data( errors, min( get_reply_max_size(), sizeof(errors) ));
1287 if (req->service)
1289 if (req->c_event)
1291 struct event *cevent = get_event_obj( current->process, req->c_event,
1292 EVENT_MODIFY_STATE );
1293 if (cevent)
1295 reset_event( cevent );
1296 release_object( cevent );
1299 sock->pmask = 0;
1300 sock_reselect( sock );
1302 release_object( &sock->obj );
1305 /* re-enable pending socket events */
1306 DECL_HANDLER(enable_socket_event)
1308 struct sock *sock;
1310 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
1311 FILE_WRITE_ATTRIBUTES, &sock_ops)))
1312 return;
1314 /* for event-based notification, windows erases stale events */
1315 sock->pmask &= ~req->mask;
1317 sock->hmask &= ~req->mask;
1318 sock->state |= req->sstate;
1319 sock->state &= ~req->cstate;
1320 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
1322 sock_reselect( sock );
1324 release_object( &sock->obj );
1327 DECL_HANDLER(set_socket_deferred)
1329 struct sock *sock, *acceptsock;
1331 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
1332 if ( !sock )
1333 return;
1335 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
1336 if ( !acceptsock )
1338 release_object( sock );
1339 return;
1341 sock->deferred = acceptsock;
1342 release_object( sock );
1345 DECL_HANDLER(get_socket_info)
1347 struct sock *sock;
1349 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1350 if (!sock) return;
1352 reply->family = sock->family;
1353 reply->type = sock->type;
1354 reply->protocol = sock->proto;
1356 release_object( &sock->obj );