wined3d: Fix / update reported driver model version.
[wine/zf.git] / server / sock.c
blob1a53ce4b091dd2d89c944900afd3e5a69d45a84f
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 no_kernel_obj_list, /* get_kernel_obj_list */
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 default_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->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] = sock_get_error( 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] = sock_get_error( 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] = sock_get_error( 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_win32_error( WSAEWOULDBLOCK );
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 (sockfd == -1)
666 if (errno == EINVAL) set_win32_error( WSAESOCKTNOSUPPORT );
667 else set_win32_error( sock_get_error( errno ));
668 return NULL;
670 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
671 if (!(sock = alloc_object( &sock_ops )))
673 close( sockfd );
674 return NULL;
676 init_sock( sock );
677 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
678 sock->flags = flags;
679 sock->proto = protocol;
680 sock->type = type;
681 sock->family = family;
683 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
684 (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
686 release_object( sock );
687 return NULL;
689 sock_reselect( sock );
690 clear_error();
691 return &sock->obj;
694 /* accepts a socket and inits it */
695 static int accept_new_fd( struct sock *sock )
698 /* Try to accept(2). We can't be safe that this an already connected socket
699 * or that accept() is allowed on it. In those cases we will get -1/errno
700 * return.
702 struct sockaddr saddr;
703 socklen_t slen = sizeof(saddr);
704 int acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen );
705 if (acceptfd != -1)
706 fcntl( acceptfd, F_SETFL, O_NONBLOCK );
707 else
708 set_win32_error( sock_get_error( errno ));
709 return acceptfd;
712 /* accept a socket (creates a new fd) */
713 static struct sock *accept_socket( obj_handle_t handle )
715 struct sock *acceptsock;
716 struct sock *sock;
717 int acceptfd;
719 sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
720 if (!sock)
721 return NULL;
723 if ( sock->deferred )
725 acceptsock = sock->deferred;
726 sock->deferred = NULL;
728 else
730 if ((acceptfd = accept_new_fd( sock )) == -1)
732 release_object( sock );
733 return NULL;
735 if (!(acceptsock = alloc_object( &sock_ops )))
737 close( acceptfd );
738 release_object( sock );
739 return NULL;
742 init_sock( acceptsock );
743 /* newly created socket gets the same properties of the listening socket */
744 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
745 if (sock->state & FD_WINE_NONBLOCKING)
746 acceptsock->state |= FD_WINE_NONBLOCKING;
747 acceptsock->mask = sock->mask;
748 acceptsock->proto = sock->proto;
749 acceptsock->type = sock->type;
750 acceptsock->family = sock->family;
751 acceptsock->window = sock->window;
752 acceptsock->message = sock->message;
753 acceptsock->connect_time = current_time;
754 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
755 acceptsock->flags = sock->flags;
756 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
757 get_fd_options( sock->fd ) )))
759 release_object( acceptsock );
760 release_object( sock );
761 return NULL;
764 clear_error();
765 sock->pmask &= ~FD_ACCEPT;
766 sock->hmask &= ~FD_ACCEPT;
767 sock_reselect( sock );
768 release_object( sock );
769 return acceptsock;
772 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
774 int acceptfd;
775 struct fd *newfd;
776 if ( sock->deferred )
778 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
779 get_fd_options( acceptsock->fd ) );
780 if ( !newfd )
781 return FALSE;
783 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
785 release_object( sock->deferred );
786 sock->deferred = NULL;
788 else
790 if ((acceptfd = accept_new_fd( sock )) == -1)
791 return FALSE;
793 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
794 get_fd_options( acceptsock->fd ) )))
795 return FALSE;
798 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
799 acceptsock->hmask = 0;
800 acceptsock->pmask = 0;
801 acceptsock->polling = 0;
802 acceptsock->proto = sock->proto;
803 acceptsock->type = sock->type;
804 acceptsock->family = sock->family;
805 acceptsock->wparam = 0;
806 acceptsock->deferred = NULL;
807 acceptsock->connect_time = current_time;
808 fd_copy_completion( acceptsock->fd, newfd );
809 release_object( acceptsock->fd );
810 acceptsock->fd = newfd;
812 clear_error();
813 sock->pmask &= ~FD_ACCEPT;
814 sock->hmask &= ~FD_ACCEPT;
815 sock_reselect( sock );
817 return TRUE;
820 /* return an errno value mapped to a WSA error */
821 static unsigned int sock_get_error( int err )
823 switch (err)
825 case EINTR: return WSAEINTR;
826 case EBADF: return WSAEBADF;
827 case EPERM:
828 case EACCES: return WSAEACCES;
829 case EFAULT: return WSAEFAULT;
830 case EINVAL: return WSAEINVAL;
831 case EMFILE: return WSAEMFILE;
832 case EWOULDBLOCK: return WSAEWOULDBLOCK;
833 case EINPROGRESS: return WSAEINPROGRESS;
834 case EALREADY: return WSAEALREADY;
835 case ENOTSOCK: return WSAENOTSOCK;
836 case EDESTADDRREQ: return WSAEDESTADDRREQ;
837 case EMSGSIZE: return WSAEMSGSIZE;
838 case EPROTOTYPE: return WSAEPROTOTYPE;
839 case ENOPROTOOPT: return WSAENOPROTOOPT;
840 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
841 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
842 case EOPNOTSUPP: return WSAEOPNOTSUPP;
843 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
844 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
845 case EADDRINUSE: return WSAEADDRINUSE;
846 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
847 case ENETDOWN: return WSAENETDOWN;
848 case ENETUNREACH: return WSAENETUNREACH;
849 case ENETRESET: return WSAENETRESET;
850 case ECONNABORTED: return WSAECONNABORTED;
851 case EPIPE:
852 case ECONNRESET: return WSAECONNRESET;
853 case ENOBUFS: return WSAENOBUFS;
854 case EISCONN: return WSAEISCONN;
855 case ENOTCONN: return WSAENOTCONN;
856 case ESHUTDOWN: return WSAESHUTDOWN;
857 case ETOOMANYREFS: return WSAETOOMANYREFS;
858 case ETIMEDOUT: return WSAETIMEDOUT;
859 case ECONNREFUSED: return WSAECONNREFUSED;
860 case ELOOP: return WSAELOOP;
861 case ENAMETOOLONG: return WSAENAMETOOLONG;
862 case EHOSTDOWN: return WSAEHOSTDOWN;
863 case EHOSTUNREACH: return WSAEHOSTUNREACH;
864 case ENOTEMPTY: return WSAENOTEMPTY;
865 #ifdef EPROCLIM
866 case EPROCLIM: return WSAEPROCLIM;
867 #endif
868 #ifdef EUSERS
869 case EUSERS: return WSAEUSERS;
870 #endif
871 #ifdef EDQUOT
872 case EDQUOT: return WSAEDQUOT;
873 #endif
874 #ifdef ESTALE
875 case ESTALE: return WSAESTALE;
876 #endif
877 #ifdef EREMOTE
878 case EREMOTE: return WSAEREMOTE;
879 #endif
881 case 0: return 0;
882 default:
883 errno = err;
884 perror("wineserver: sock_get_error() can't map error");
885 return WSAEFAULT;
889 static int sock_get_ntstatus( int err )
891 switch ( err )
893 case EBADF: return STATUS_INVALID_HANDLE;
894 case EBUSY: return STATUS_DEVICE_BUSY;
895 case EPERM:
896 case EACCES: return STATUS_ACCESS_DENIED;
897 case EFAULT: return STATUS_NO_MEMORY;
898 case EINVAL: return STATUS_INVALID_PARAMETER;
899 case ENFILE:
900 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
901 case EWOULDBLOCK: return STATUS_CANT_WAIT;
902 case EINPROGRESS: return STATUS_PENDING;
903 case EALREADY: return STATUS_NETWORK_BUSY;
904 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
905 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
906 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
907 case EPROTONOSUPPORT:
908 case ESOCKTNOSUPPORT:
909 case EPFNOSUPPORT:
910 case EAFNOSUPPORT:
911 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
912 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
913 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
914 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
915 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
916 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
917 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
918 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
919 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
920 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
921 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
922 case ENETDOWN: return STATUS_NETWORK_BUSY;
923 case EPIPE:
924 case ECONNRESET: return STATUS_CONNECTION_RESET;
925 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
927 case 0: return STATUS_SUCCESS;
928 default:
929 errno = err;
930 perror("wineserver: sock_get_ntstatus() can't map error");
931 return STATUS_UNSUCCESSFUL;
935 #ifdef HAVE_LINUX_RTNETLINK_H
937 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
938 static struct object *ifchange_object;
940 static void ifchange_dump( struct object *obj, int verbose );
941 static struct fd *ifchange_get_fd( struct object *obj );
942 static void ifchange_destroy( struct object *obj );
944 static int ifchange_get_poll_events( struct fd *fd );
945 static void ifchange_poll_event( struct fd *fd, int event );
947 struct ifchange
949 struct object obj; /* object header */
950 struct fd *fd; /* interface change file descriptor */
951 struct list sockets; /* list of sockets to send interface change notifications */
954 static const struct object_ops ifchange_ops =
956 sizeof(struct ifchange), /* size */
957 ifchange_dump, /* dump */
958 no_get_type, /* get_type */
959 add_queue, /* add_queue */
960 NULL, /* remove_queue */
961 NULL, /* signaled */
962 no_satisfied, /* satisfied */
963 no_signal, /* signal */
964 ifchange_get_fd, /* get_fd */
965 default_fd_map_access, /* map_access */
966 default_get_sd, /* get_sd */
967 default_set_sd, /* set_sd */
968 no_lookup_name, /* lookup_name */
969 no_link_name, /* link_name */
970 NULL, /* unlink_name */
971 no_open_file, /* open_file */
972 no_kernel_obj_list, /* get_kernel_obj_list */
973 no_close_handle, /* close_handle */
974 ifchange_destroy /* destroy */
977 static const struct fd_ops ifchange_fd_ops =
979 ifchange_get_poll_events, /* get_poll_events */
980 ifchange_poll_event, /* poll_event */
981 NULL, /* get_fd_type */
982 no_fd_read, /* read */
983 no_fd_write, /* write */
984 no_fd_flush, /* flush */
985 no_fd_get_file_info, /* get_file_info */
986 no_fd_get_volume_info, /* get_volume_info */
987 no_fd_ioctl, /* ioctl */
988 NULL, /* queue_async */
989 NULL /* reselect_async */
992 static void ifchange_dump( struct object *obj, int verbose )
994 assert( obj->ops == &ifchange_ops );
995 fprintf( stderr, "Interface change\n" );
998 static struct fd *ifchange_get_fd( struct object *obj )
1000 struct ifchange *ifchange = (struct ifchange *)obj;
1001 return (struct fd *)grab_object( ifchange->fd );
1004 static void ifchange_destroy( struct object *obj )
1006 struct ifchange *ifchange = (struct ifchange *)obj;
1007 assert( obj->ops == &ifchange_ops );
1009 release_object( ifchange->fd );
1011 /* reset the global ifchange object so that it will be recreated if it is needed again */
1012 assert( obj == ifchange_object );
1013 ifchange_object = NULL;
1016 static int ifchange_get_poll_events( struct fd *fd )
1018 return POLLIN;
1021 /* wake up all the sockets waiting for a change notification event */
1022 static void ifchange_wake_up( struct object *obj, unsigned int status )
1024 struct ifchange *ifchange = (struct ifchange *)obj;
1025 struct list *ptr, *next;
1026 assert( obj->ops == &ifchange_ops );
1027 assert( obj == ifchange_object );
1029 LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
1031 struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
1033 assert( sock->ifchange_obj );
1034 async_wake_up( &sock->ifchange_q, status ); /* issue ifchange notification for the socket */
1035 sock_release_ifchange( sock ); /* remove socket from list and decrement ifchange refcount */
1039 static void ifchange_poll_event( struct fd *fd, int event )
1041 struct object *ifchange = get_fd_user( fd );
1042 unsigned int status = STATUS_PENDING;
1043 char buffer[PIPE_BUF];
1044 int r;
1046 r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
1047 if (r < 0)
1049 if (errno == EWOULDBLOCK || (EWOULDBLOCK != EAGAIN && errno == EAGAIN))
1050 return; /* retry when poll() says the socket is ready */
1051 status = sock_get_ntstatus( errno );
1053 else if (r > 0)
1055 struct nlmsghdr *nlh;
1057 for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
1059 if (nlh->nlmsg_type == NLMSG_DONE)
1060 break;
1061 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
1062 status = STATUS_SUCCESS;
1065 else status = STATUS_CANCELLED;
1067 if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
1070 #endif
1072 /* we only need one of these interface notification objects, all of the sockets dependent upon
1073 * it will wake up when a notification event occurs */
1074 static struct object *get_ifchange( void )
1076 #ifdef HAVE_LINUX_RTNETLINK_H
1077 struct ifchange *ifchange;
1078 struct sockaddr_nl addr;
1079 int unix_fd;
1081 if (ifchange_object)
1083 /* increment the refcount for each socket that uses the ifchange object */
1084 return grab_object( ifchange_object );
1087 /* create the socket we need for processing interface change notifications */
1088 unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
1089 if (unix_fd == -1)
1091 set_win32_error( sock_get_error( errno ));
1092 return NULL;
1094 fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
1095 memset( &addr, 0, sizeof(addr) );
1096 addr.nl_family = AF_NETLINK;
1097 addr.nl_groups = RTMGRP_IPV4_IFADDR;
1098 /* bind the socket to the special netlink kernel interface */
1099 if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
1101 close( unix_fd );
1102 set_win32_error( sock_get_error( errno ));
1103 return NULL;
1105 if (!(ifchange = alloc_object( &ifchange_ops )))
1107 close( unix_fd );
1108 set_error( STATUS_NO_MEMORY );
1109 return NULL;
1111 list_init( &ifchange->sockets );
1112 if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
1114 release_object( ifchange );
1115 set_error( STATUS_NO_MEMORY );
1116 return NULL;
1118 set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
1120 /* the ifchange object is now successfully configured */
1121 ifchange_object = &ifchange->obj;
1122 return &ifchange->obj;
1123 #else
1124 set_error( STATUS_NOT_SUPPORTED );
1125 return NULL;
1126 #endif
1129 /* add the socket to the interface change notification list */
1130 static void ifchange_add_sock( struct object *obj, struct sock *sock )
1132 #ifdef HAVE_LINUX_RTNETLINK_H
1133 struct ifchange *ifchange = (struct ifchange *)obj;
1135 list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
1136 #endif
1139 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1140 static struct object *sock_get_ifchange( struct sock *sock )
1142 struct object *ifchange;
1144 if (sock->ifchange_obj) /* reuse existing ifchange_obj for this socket */
1145 return sock->ifchange_obj;
1147 if (!(ifchange = get_ifchange()))
1148 return NULL;
1150 /* add the socket to the ifchange notification list */
1151 ifchange_add_sock( ifchange, sock );
1152 sock->ifchange_obj = ifchange;
1153 return ifchange;
1156 /* destroy an existing ifchange queue for a specific socket */
1157 static void sock_release_ifchange( struct sock *sock )
1159 if (sock->ifchange_obj)
1161 list_remove( &sock->ifchange_entry );
1162 release_object( sock->ifchange_obj );
1163 sock->ifchange_obj = NULL;
1167 /* create a socket */
1168 DECL_HANDLER(create_socket)
1170 struct object *obj;
1172 reply->handle = 0;
1173 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
1175 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1176 release_object( obj );
1180 /* accept a socket */
1181 DECL_HANDLER(accept_socket)
1183 struct sock *sock;
1185 reply->handle = 0;
1186 if ((sock = accept_socket( req->lhandle )) != NULL)
1188 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
1189 sock->wparam = reply->handle; /* wparam for message is the socket handle */
1190 sock_reselect( sock );
1191 release_object( &sock->obj );
1195 /* accept a socket into an initialized socket */
1196 DECL_HANDLER(accept_into_socket)
1198 struct sock *sock, *acceptsock;
1199 const int all_attributes = FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|FILE_READ_DATA;
1201 if (!(sock = (struct sock *)get_handle_obj( current->process, req->lhandle,
1202 all_attributes, &sock_ops)))
1203 return;
1205 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, req->ahandle,
1206 all_attributes, &sock_ops)))
1208 release_object( sock );
1209 return;
1212 if (accept_into_socket( sock, acceptsock ))
1214 acceptsock->wparam = req->ahandle; /* wparam for message is the socket handle */
1215 sock_reselect( acceptsock );
1217 release_object( acceptsock );
1218 release_object( sock );
1221 /* set socket event parameters */
1222 DECL_HANDLER(set_socket_event)
1224 struct sock *sock;
1225 struct event *old_event;
1227 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1228 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
1229 old_event = sock->event;
1230 sock->mask = req->mask;
1231 sock->hmask &= ~req->mask; /* re-enable held events */
1232 sock->event = NULL;
1233 sock->window = req->window;
1234 sock->message = req->msg;
1235 sock->wparam = req->handle; /* wparam is the socket handle */
1236 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
1238 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
1240 sock_reselect( sock );
1242 sock->state |= FD_WINE_NONBLOCKING;
1244 /* if a network event is pending, signal the event object
1245 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1246 before a WSAEventSelect() was done on it.
1247 (when dealing with Asynchronous socket) */
1248 sock_wake_up( sock );
1250 if (old_event) release_object( old_event ); /* we're through with it */
1251 release_object( &sock->obj );
1254 /* get socket event parameters */
1255 DECL_HANDLER(get_socket_event)
1257 struct sock *sock;
1259 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1260 FILE_READ_ATTRIBUTES, &sock_ops ))) return;
1261 reply->mask = sock->mask;
1262 reply->pmask = sock->pmask;
1263 reply->state = sock->state;
1264 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
1266 if (req->service)
1268 if (req->c_event)
1270 struct event *cevent = get_event_obj( current->process, req->c_event,
1271 EVENT_MODIFY_STATE );
1272 if (cevent)
1274 reset_event( cevent );
1275 release_object( cevent );
1278 sock->pmask = 0;
1279 sock_reselect( sock );
1281 release_object( &sock->obj );
1284 /* re-enable pending socket events */
1285 DECL_HANDLER(enable_socket_event)
1287 struct sock *sock;
1289 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
1290 FILE_WRITE_ATTRIBUTES, &sock_ops)))
1291 return;
1293 /* for event-based notification, windows erases stale events */
1294 sock->pmask &= ~req->mask;
1296 sock->hmask &= ~req->mask;
1297 sock->state |= req->sstate;
1298 sock->state &= ~req->cstate;
1299 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
1301 sock_reselect( sock );
1303 release_object( &sock->obj );
1306 DECL_HANDLER(set_socket_deferred)
1308 struct sock *sock, *acceptsock;
1310 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
1311 if ( !sock )
1312 return;
1314 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
1315 if ( !acceptsock )
1317 release_object( sock );
1318 return;
1320 sock->deferred = acceptsock;
1321 release_object( sock );
1324 DECL_HANDLER(get_socket_info)
1326 struct sock *sock;
1328 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1329 if (!sock) return;
1331 reply->family = sock->family;
1332 reply->type = sock->type;
1333 reply->protocol = sock->proto;
1335 release_object( &sock->obj );