setupapi/tests: Remove workarounds in testDeviceRegistryPropertyW().
[wine.git] / server / sock.c
blob284ab9ff78a71f9bd845f8785e616bf40b307eaf
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 unsigned 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 unsigned int sock_get_error( int err );
139 static const struct object_ops sock_ops =
141 sizeof(struct sock), /* size */
142 sock_dump, /* dump */
143 no_get_type, /* get_type */
144 add_queue, /* add_queue */
145 remove_queue, /* remove_queue */
146 sock_signaled, /* signaled */
147 no_satisfied, /* satisfied */
148 no_signal, /* signal */
149 sock_get_fd, /* get_fd */
150 default_fd_map_access, /* map_access */
151 default_get_sd, /* get_sd */
152 default_set_sd, /* set_sd */
153 no_lookup_name, /* lookup_name */
154 no_link_name, /* link_name */
155 NULL, /* unlink_name */
156 no_open_file, /* open_file */
157 fd_close_handle, /* close_handle */
158 sock_destroy /* destroy */
161 static const struct fd_ops sock_fd_ops =
163 sock_get_poll_events, /* get_poll_events */
164 sock_poll_event, /* poll_event */
165 sock_get_fd_type, /* get_fd_type */
166 no_fd_read, /* read */
167 no_fd_write, /* write */
168 no_fd_flush, /* flush */
169 default_fd_get_file_info, /* get_file_info */
170 no_fd_get_volume_info, /* get_volume_info */
171 sock_ioctl, /* ioctl */
172 sock_queue_async, /* queue_async */
173 sock_reselect_async /* reselect_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->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] = sock_get_error( 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] = sock_get_error( 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] = sock_get_error( 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 static int sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
540 struct sock *sock = get_fd_user( fd );
542 assert( sock->obj.ops == &sock_ops );
544 switch(code)
546 case WS_SIO_ADDRESS_LIST_CHANGE:
547 if ((sock->state & FD_WINE_NONBLOCKING) && async_is_blocking( async ))
549 set_win32_error( WSAEWOULDBLOCK );
550 return 0;
552 if (!sock_get_ifchange( sock )) return 0;
553 queue_async( &sock->ifchange_q, async );
554 set_error( STATUS_PENDING );
555 return 1;
556 default:
557 set_error( STATUS_NOT_SUPPORTED );
558 return 0;
562 static void sock_queue_async( struct fd *fd, struct async *async, int type, int count )
564 struct sock *sock = get_fd_user( fd );
565 struct async_queue *queue;
567 assert( sock->obj.ops == &sock_ops );
569 switch (type)
571 case ASYNC_TYPE_READ:
572 queue = &sock->read_q;
573 break;
574 case ASYNC_TYPE_WRITE:
575 queue = &sock->write_q;
576 break;
577 default:
578 set_error( STATUS_INVALID_PARAMETER );
579 return;
582 if ( ( !( sock->state & (FD_READ|FD_CONNECT|FD_WINE_LISTENING) ) && type == ASYNC_TYPE_READ ) ||
583 ( !( sock->state & (FD_WRITE|FD_CONNECT) ) && type == ASYNC_TYPE_WRITE ) )
585 set_error( STATUS_PIPE_DISCONNECTED );
586 return;
589 queue_async( queue, async );
590 sock_reselect( sock );
592 set_error( STATUS_PENDING );
595 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
597 struct sock *sock = get_fd_user( fd );
598 /* ignore reselect on ifchange queue */
599 if (&sock->ifchange_q != queue)
600 sock_reselect( sock );
603 static struct fd *sock_get_fd( struct object *obj )
605 struct sock *sock = (struct sock *)obj;
606 return (struct fd *)grab_object( sock->fd );
609 static void sock_destroy( struct object *obj )
611 struct sock *sock = (struct sock *)obj;
612 assert( obj->ops == &sock_ops );
614 /* FIXME: special socket shutdown stuff? */
616 if ( sock->deferred )
617 release_object( sock->deferred );
619 async_wake_up( &sock->ifchange_q, STATUS_CANCELLED );
620 sock_release_ifchange( sock );
621 free_async_queue( &sock->read_q );
622 free_async_queue( &sock->write_q );
623 free_async_queue( &sock->ifchange_q );
624 if (sock->event) release_object( sock->event );
625 if (sock->fd)
627 /* shut the socket down to force pending poll() calls in the client to return */
628 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
629 release_object( sock->fd );
633 static void init_sock(struct sock *sock)
635 sock->state = 0;
636 sock->mask = 0;
637 sock->hmask = 0;
638 sock->pmask = 0;
639 sock->polling = 0;
640 sock->flags = 0;
641 sock->type = 0;
642 sock->family = 0;
643 sock->event = NULL;
644 sock->window = 0;
645 sock->message = 0;
646 sock->wparam = 0;
647 sock->connect_time = 0;
648 sock->deferred = NULL;
649 sock->ifchange_obj = NULL;
650 init_async_queue( &sock->read_q );
651 init_async_queue( &sock->write_q );
652 init_async_queue( &sock->ifchange_q );
653 memset( sock->errors, 0, sizeof(sock->errors) );
656 /* create a new and unconnected socket */
657 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
659 struct sock *sock;
660 int sockfd;
662 sockfd = socket( family, type, protocol );
663 if (sockfd == -1)
665 if (errno == EINVAL) set_win32_error( WSAESOCKTNOSUPPORT );
666 else set_win32_error( sock_get_error( errno ));
667 return NULL;
669 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
670 if (!(sock = alloc_object( &sock_ops )))
672 close( sockfd );
673 return NULL;
675 init_sock( sock );
676 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
677 sock->flags = flags;
678 sock->proto = protocol;
679 sock->type = type;
680 sock->family = family;
682 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
683 (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
685 release_object( sock );
686 return NULL;
688 sock_reselect( sock );
689 clear_error();
690 return &sock->obj;
693 /* accepts a socket and inits it */
694 static int accept_new_fd( struct sock *sock )
697 /* Try to accept(2). We can't be safe that this an already connected socket
698 * or that accept() is allowed on it. In those cases we will get -1/errno
699 * return.
701 struct sockaddr saddr;
702 socklen_t slen = sizeof(saddr);
703 int acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen );
704 if (acceptfd != -1)
705 fcntl( acceptfd, F_SETFL, O_NONBLOCK );
706 else
707 set_win32_error( sock_get_error( errno ));
708 return acceptfd;
711 /* accept a socket (creates a new fd) */
712 static struct sock *accept_socket( obj_handle_t handle )
714 struct sock *acceptsock;
715 struct sock *sock;
716 int acceptfd;
718 sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
719 if (!sock)
720 return NULL;
722 if ( sock->deferred )
724 acceptsock = sock->deferred;
725 sock->deferred = NULL;
727 else
729 if ((acceptfd = accept_new_fd( sock )) == -1)
731 release_object( sock );
732 return NULL;
734 if (!(acceptsock = alloc_object( &sock_ops )))
736 close( acceptfd );
737 release_object( sock );
738 return NULL;
741 init_sock( acceptsock );
742 /* newly created socket gets the same properties of the listening socket */
743 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
744 if (sock->state & FD_WINE_NONBLOCKING)
745 acceptsock->state |= FD_WINE_NONBLOCKING;
746 acceptsock->mask = sock->mask;
747 acceptsock->proto = sock->proto;
748 acceptsock->type = sock->type;
749 acceptsock->family = sock->family;
750 acceptsock->window = sock->window;
751 acceptsock->message = sock->message;
752 acceptsock->connect_time = current_time;
753 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
754 acceptsock->flags = sock->flags;
755 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
756 get_fd_options( sock->fd ) )))
758 release_object( acceptsock );
759 release_object( sock );
760 return NULL;
763 clear_error();
764 sock->pmask &= ~FD_ACCEPT;
765 sock->hmask &= ~FD_ACCEPT;
766 sock_reselect( sock );
767 release_object( sock );
768 return acceptsock;
771 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
773 int acceptfd;
774 struct fd *newfd;
775 if ( sock->deferred )
777 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
778 get_fd_options( acceptsock->fd ) );
779 if ( !newfd )
780 return FALSE;
782 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
784 release_object( sock->deferred );
785 sock->deferred = NULL;
787 else
789 if ((acceptfd = accept_new_fd( sock )) == -1)
790 return FALSE;
792 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
793 get_fd_options( acceptsock->fd ) )))
794 return FALSE;
797 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
798 acceptsock->hmask = 0;
799 acceptsock->pmask = 0;
800 acceptsock->polling = 0;
801 acceptsock->proto = sock->proto;
802 acceptsock->type = sock->type;
803 acceptsock->family = sock->family;
804 acceptsock->wparam = 0;
805 acceptsock->deferred = NULL;
806 acceptsock->connect_time = current_time;
807 fd_copy_completion( acceptsock->fd, newfd );
808 release_object( acceptsock->fd );
809 acceptsock->fd = newfd;
811 clear_error();
812 sock->pmask &= ~FD_ACCEPT;
813 sock->hmask &= ~FD_ACCEPT;
814 sock_reselect( sock );
816 return TRUE;
819 /* return an errno value mapped to a WSA error */
820 static unsigned int sock_get_error( int err )
822 switch (err)
824 case EINTR: return WSAEINTR;
825 case EBADF: return WSAEBADF;
826 case EPERM:
827 case EACCES: return WSAEACCES;
828 case EFAULT: return WSAEFAULT;
829 case EINVAL: return WSAEINVAL;
830 case EMFILE: return WSAEMFILE;
831 case EWOULDBLOCK: return WSAEWOULDBLOCK;
832 case EINPROGRESS: return WSAEINPROGRESS;
833 case EALREADY: return WSAEALREADY;
834 case ENOTSOCK: return WSAENOTSOCK;
835 case EDESTADDRREQ: return WSAEDESTADDRREQ;
836 case EMSGSIZE: return WSAEMSGSIZE;
837 case EPROTOTYPE: return WSAEPROTOTYPE;
838 case ENOPROTOOPT: return WSAENOPROTOOPT;
839 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
840 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
841 case EOPNOTSUPP: return WSAEOPNOTSUPP;
842 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
843 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
844 case EADDRINUSE: return WSAEADDRINUSE;
845 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
846 case ENETDOWN: return WSAENETDOWN;
847 case ENETUNREACH: return WSAENETUNREACH;
848 case ENETRESET: return WSAENETRESET;
849 case ECONNABORTED: return WSAECONNABORTED;
850 case EPIPE:
851 case ECONNRESET: return WSAECONNRESET;
852 case ENOBUFS: return WSAENOBUFS;
853 case EISCONN: return WSAEISCONN;
854 case ENOTCONN: return WSAENOTCONN;
855 case ESHUTDOWN: return WSAESHUTDOWN;
856 case ETOOMANYREFS: return WSAETOOMANYREFS;
857 case ETIMEDOUT: return WSAETIMEDOUT;
858 case ECONNREFUSED: return WSAECONNREFUSED;
859 case ELOOP: return WSAELOOP;
860 case ENAMETOOLONG: return WSAENAMETOOLONG;
861 case EHOSTDOWN: return WSAEHOSTDOWN;
862 case EHOSTUNREACH: return WSAEHOSTUNREACH;
863 case ENOTEMPTY: return WSAENOTEMPTY;
864 #ifdef EPROCLIM
865 case EPROCLIM: return WSAEPROCLIM;
866 #endif
867 #ifdef EUSERS
868 case EUSERS: return WSAEUSERS;
869 #endif
870 #ifdef EDQUOT
871 case EDQUOT: return WSAEDQUOT;
872 #endif
873 #ifdef ESTALE
874 case ESTALE: return WSAESTALE;
875 #endif
876 #ifdef EREMOTE
877 case EREMOTE: return WSAEREMOTE;
878 #endif
880 case 0: return 0;
881 default:
882 errno = err;
883 perror("wineserver: sock_get_error() can't map error");
884 return WSAEFAULT;
888 static int sock_get_ntstatus( int err )
890 switch ( err )
892 case EBADF: return STATUS_INVALID_HANDLE;
893 case EBUSY: return STATUS_DEVICE_BUSY;
894 case EPERM:
895 case EACCES: return STATUS_ACCESS_DENIED;
896 case EFAULT: return STATUS_NO_MEMORY;
897 case EINVAL: return STATUS_INVALID_PARAMETER;
898 case ENFILE:
899 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
900 case EWOULDBLOCK: return STATUS_CANT_WAIT;
901 case EINPROGRESS: return STATUS_PENDING;
902 case EALREADY: return STATUS_NETWORK_BUSY;
903 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
904 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
905 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
906 case EPROTONOSUPPORT:
907 case ESOCKTNOSUPPORT:
908 case EPFNOSUPPORT:
909 case EAFNOSUPPORT:
910 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
911 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
912 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
913 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
914 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
915 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
916 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
917 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
918 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
919 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
920 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
921 case ENETDOWN: return STATUS_NETWORK_BUSY;
922 case EPIPE:
923 case ECONNRESET: return STATUS_CONNECTION_RESET;
924 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
926 case 0: return STATUS_SUCCESS;
927 default:
928 errno = err;
929 perror("wineserver: sock_get_ntstatus() can't map error");
930 return STATUS_UNSUCCESSFUL;
934 #ifdef HAVE_LINUX_RTNETLINK_H
936 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
937 static struct object *ifchange_object;
939 static void ifchange_dump( struct object *obj, int verbose );
940 static struct fd *ifchange_get_fd( struct object *obj );
941 static void ifchange_destroy( struct object *obj );
943 static int ifchange_get_poll_events( struct fd *fd );
944 static void ifchange_poll_event( struct fd *fd, int event );
946 struct ifchange
948 struct object obj; /* object header */
949 struct fd *fd; /* interface change file descriptor */
950 struct list sockets; /* list of sockets to send interface change notifications */
953 static const struct object_ops ifchange_ops =
955 sizeof(struct ifchange), /* size */
956 ifchange_dump, /* dump */
957 no_get_type, /* get_type */
958 add_queue, /* add_queue */
959 NULL, /* remove_queue */
960 NULL, /* signaled */
961 no_satisfied, /* satisfied */
962 no_signal, /* signal */
963 ifchange_get_fd, /* get_fd */
964 default_fd_map_access, /* map_access */
965 default_get_sd, /* get_sd */
966 default_set_sd, /* set_sd */
967 no_lookup_name, /* lookup_name */
968 no_link_name, /* link_name */
969 NULL, /* unlink_name */
970 no_open_file, /* open_file */
971 no_close_handle, /* close_handle */
972 ifchange_destroy /* destroy */
975 static const struct fd_ops ifchange_fd_ops =
977 ifchange_get_poll_events, /* get_poll_events */
978 ifchange_poll_event, /* poll_event */
979 NULL, /* get_fd_type */
980 no_fd_read, /* read */
981 no_fd_write, /* write */
982 no_fd_flush, /* flush */
983 no_fd_get_file_info, /* get_file_info */
984 no_fd_get_volume_info, /* get_volume_info */
985 no_fd_ioctl, /* ioctl */
986 NULL, /* queue_async */
987 NULL /* reselect_async */
990 static void ifchange_dump( struct object *obj, int verbose )
992 assert( obj->ops == &ifchange_ops );
993 fprintf( stderr, "Interface change\n" );
996 static struct fd *ifchange_get_fd( struct object *obj )
998 struct ifchange *ifchange = (struct ifchange *)obj;
999 return (struct fd *)grab_object( ifchange->fd );
1002 static void ifchange_destroy( struct object *obj )
1004 struct ifchange *ifchange = (struct ifchange *)obj;
1005 assert( obj->ops == &ifchange_ops );
1007 release_object( ifchange->fd );
1009 /* reset the global ifchange object so that it will be recreated if it is needed again */
1010 assert( obj == ifchange_object );
1011 ifchange_object = NULL;
1014 static int ifchange_get_poll_events( struct fd *fd )
1016 return POLLIN;
1019 /* wake up all the sockets waiting for a change notification event */
1020 static void ifchange_wake_up( struct object *obj, unsigned int status )
1022 struct ifchange *ifchange = (struct ifchange *)obj;
1023 struct list *ptr, *next;
1024 assert( obj->ops == &ifchange_ops );
1025 assert( obj == ifchange_object );
1027 LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
1029 struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
1031 assert( sock->ifchange_obj );
1032 async_wake_up( &sock->ifchange_q, status ); /* issue ifchange notification for the socket */
1033 sock_release_ifchange( sock ); /* remove socket from list and decrement ifchange refcount */
1037 static void ifchange_poll_event( struct fd *fd, int event )
1039 struct object *ifchange = get_fd_user( fd );
1040 unsigned int status = STATUS_PENDING;
1041 char buffer[PIPE_BUF];
1042 int r;
1044 r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
1045 if (r < 0)
1047 if (errno == EWOULDBLOCK || (EWOULDBLOCK != EAGAIN && errno == EAGAIN))
1048 return; /* retry when poll() says the socket is ready */
1049 status = sock_get_ntstatus( errno );
1051 else if (r > 0)
1053 struct nlmsghdr *nlh;
1055 for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
1057 if (nlh->nlmsg_type == NLMSG_DONE)
1058 break;
1059 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
1060 status = STATUS_SUCCESS;
1063 else status = STATUS_CANCELLED;
1065 if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
1068 #endif
1070 /* we only need one of these interface notification objects, all of the sockets dependent upon
1071 * it will wake up when a notification event occurs */
1072 static struct object *get_ifchange( void )
1074 #ifdef HAVE_LINUX_RTNETLINK_H
1075 struct ifchange *ifchange;
1076 struct sockaddr_nl addr;
1077 int unix_fd;
1079 if (ifchange_object)
1081 /* increment the refcount for each socket that uses the ifchange object */
1082 return grab_object( ifchange_object );
1085 /* create the socket we need for processing interface change notifications */
1086 unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
1087 if (unix_fd == -1)
1089 set_win32_error( sock_get_error( errno ));
1090 return NULL;
1092 fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
1093 memset( &addr, 0, sizeof(addr) );
1094 addr.nl_family = AF_NETLINK;
1095 addr.nl_groups = RTMGRP_IPV4_IFADDR;
1096 /* bind the socket to the special netlink kernel interface */
1097 if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
1099 close( unix_fd );
1100 set_win32_error( sock_get_error( errno ));
1101 return NULL;
1103 if (!(ifchange = alloc_object( &ifchange_ops )))
1105 close( unix_fd );
1106 set_error( STATUS_NO_MEMORY );
1107 return NULL;
1109 list_init( &ifchange->sockets );
1110 if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
1112 release_object( ifchange );
1113 set_error( STATUS_NO_MEMORY );
1114 return NULL;
1116 set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
1118 /* the ifchange object is now successfully configured */
1119 ifchange_object = &ifchange->obj;
1120 return &ifchange->obj;
1121 #else
1122 set_error( STATUS_NOT_SUPPORTED );
1123 return NULL;
1124 #endif
1127 /* add the socket to the interface change notification list */
1128 static void ifchange_add_sock( struct object *obj, struct sock *sock )
1130 #ifdef HAVE_LINUX_RTNETLINK_H
1131 struct ifchange *ifchange = (struct ifchange *)obj;
1133 list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
1134 #endif
1137 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1138 static struct object *sock_get_ifchange( struct sock *sock )
1140 struct object *ifchange;
1142 if (sock->ifchange_obj) /* reuse existing ifchange_obj for this socket */
1143 return sock->ifchange_obj;
1145 if (!(ifchange = get_ifchange()))
1146 return NULL;
1148 /* add the socket to the ifchange notification list */
1149 ifchange_add_sock( ifchange, sock );
1150 sock->ifchange_obj = ifchange;
1151 return ifchange;
1154 /* destroy an existing ifchange queue for a specific socket */
1155 static void sock_release_ifchange( struct sock *sock )
1157 if (sock->ifchange_obj)
1159 list_remove( &sock->ifchange_entry );
1160 release_object( sock->ifchange_obj );
1161 sock->ifchange_obj = NULL;
1165 /* create a socket */
1166 DECL_HANDLER(create_socket)
1168 struct object *obj;
1170 reply->handle = 0;
1171 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
1173 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1174 release_object( obj );
1178 /* accept a socket */
1179 DECL_HANDLER(accept_socket)
1181 struct sock *sock;
1183 reply->handle = 0;
1184 if ((sock = accept_socket( req->lhandle )) != NULL)
1186 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
1187 sock->wparam = reply->handle; /* wparam for message is the socket handle */
1188 sock_reselect( sock );
1189 release_object( &sock->obj );
1193 /* accept a socket into an initialized socket */
1194 DECL_HANDLER(accept_into_socket)
1196 struct sock *sock, *acceptsock;
1197 const int all_attributes = FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|FILE_READ_DATA;
1199 if (!(sock = (struct sock *)get_handle_obj( current->process, req->lhandle,
1200 all_attributes, &sock_ops)))
1201 return;
1203 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, req->ahandle,
1204 all_attributes, &sock_ops)))
1206 release_object( sock );
1207 return;
1210 if (accept_into_socket( sock, acceptsock ))
1212 acceptsock->wparam = req->ahandle; /* wparam for message is the socket handle */
1213 sock_reselect( acceptsock );
1215 release_object( acceptsock );
1216 release_object( sock );
1219 /* set socket event parameters */
1220 DECL_HANDLER(set_socket_event)
1222 struct sock *sock;
1223 struct event *old_event;
1225 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1226 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
1227 old_event = sock->event;
1228 sock->mask = req->mask;
1229 sock->hmask &= ~req->mask; /* re-enable held events */
1230 sock->event = NULL;
1231 sock->window = req->window;
1232 sock->message = req->msg;
1233 sock->wparam = req->handle; /* wparam is the socket handle */
1234 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
1236 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
1238 sock_reselect( sock );
1240 sock->state |= FD_WINE_NONBLOCKING;
1242 /* if a network event is pending, signal the event object
1243 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1244 before a WSAEventSelect() was done on it.
1245 (when dealing with Asynchronous socket) */
1246 sock_wake_up( sock );
1248 if (old_event) release_object( old_event ); /* we're through with it */
1249 release_object( &sock->obj );
1252 /* get socket event parameters */
1253 DECL_HANDLER(get_socket_event)
1255 struct sock *sock;
1257 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1258 FILE_READ_ATTRIBUTES, &sock_ops ))) return;
1259 reply->mask = sock->mask;
1260 reply->pmask = sock->pmask;
1261 reply->state = sock->state;
1262 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
1264 if (req->service)
1266 if (req->c_event)
1268 struct event *cevent = get_event_obj( current->process, req->c_event,
1269 EVENT_MODIFY_STATE );
1270 if (cevent)
1272 reset_event( cevent );
1273 release_object( cevent );
1276 sock->pmask = 0;
1277 sock_reselect( sock );
1279 release_object( &sock->obj );
1282 /* re-enable pending socket events */
1283 DECL_HANDLER(enable_socket_event)
1285 struct sock *sock;
1287 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
1288 FILE_WRITE_ATTRIBUTES, &sock_ops)))
1289 return;
1291 /* for event-based notification, windows erases stale events */
1292 sock->pmask &= ~req->mask;
1294 sock->hmask &= ~req->mask;
1295 sock->state |= req->sstate;
1296 sock->state &= ~req->cstate;
1297 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
1299 sock_reselect( sock );
1301 release_object( &sock->obj );
1304 DECL_HANDLER(set_socket_deferred)
1306 struct sock *sock, *acceptsock;
1308 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
1309 if ( !sock )
1310 return;
1312 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
1313 if ( !acceptsock )
1315 release_object( sock );
1316 return;
1318 sock->deferred = acceptsock;
1319 release_object( sock );
1322 DECL_HANDLER(get_socket_info)
1324 struct sock *sock;
1326 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1327 if (!sock) return;
1329 reply->family = sock->family;
1330 reply->type = sock->type;
1331 reply->protocol = sock->proto;
1333 release_object( &sock->obj );