po: Update Lithuanian translation.
[wine.git] / server / sock.c
blobdc10d2a415ffab1bc4805104df299596e1ff8933
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 async_queue *sock_get_ifchange_q( struct sock *sock );
127 static void sock_destroy_ifchange_q( 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 obj_handle_t sock_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async, int blocking );
133 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
134 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
135 static int sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb );
137 static int sock_get_ntstatus( int err );
138 static int sock_get_error( int err );
139 static void sock_set_error(void);
141 static const struct object_ops sock_ops =
143 sizeof(struct sock), /* size */
144 sock_dump, /* dump */
145 no_get_type, /* get_type */
146 add_queue, /* add_queue */
147 remove_queue, /* remove_queue */
148 sock_signaled, /* signaled */
149 no_satisfied, /* satisfied */
150 no_signal, /* signal */
151 sock_get_fd, /* get_fd */
152 default_fd_map_access, /* map_access */
153 default_get_sd, /* get_sd */
154 default_set_sd, /* set_sd */
155 no_lookup_name, /* lookup_name */
156 no_link_name, /* link_name */
157 NULL, /* unlink_name */
158 no_open_file, /* open_file */
159 fd_close_handle, /* close_handle */
160 sock_destroy /* destroy */
163 static const struct fd_ops sock_fd_ops =
165 sock_get_poll_events, /* get_poll_events */
166 sock_poll_event, /* poll_event */
167 sock_get_fd_type, /* get_fd_type */
168 no_fd_read, /* read */
169 no_fd_write, /* write */
170 no_fd_flush, /* flush */
171 sock_ioctl, /* ioctl */
172 sock_queue_async, /* queue_async */
173 sock_reselect_async, /* reselect_async */
174 sock_cancel_async /* cancel_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 obj_handle_t sock_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data, int blocking )
541 struct sock *sock = get_fd_user( fd );
542 obj_handle_t wait_handle = 0;
543 struct async_queue *ifchange_q;
544 struct async *async;
546 assert( sock->obj.ops == &sock_ops );
548 switch(code)
550 case WS_SIO_ADDRESS_LIST_CHANGE:
551 if ((sock->state & FD_WINE_NONBLOCKING) && !blocking)
553 set_error( STATUS_CANT_WAIT );
554 return 0;
556 if (!(ifchange_q = sock_get_ifchange_q( sock ))) return 0;
557 if (!(async = create_async( current, ifchange_q, async_data ))) return 0;
558 if (blocking) wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 );
559 release_object( async );
560 set_error( STATUS_PENDING );
561 return wait_handle;
562 default:
563 set_error( STATUS_NOT_SUPPORTED );
564 return 0;
568 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
570 struct sock *sock = get_fd_user( fd );
571 struct async *async;
572 struct async_queue *queue;
574 assert( sock->obj.ops == &sock_ops );
576 switch (type)
578 case ASYNC_TYPE_READ:
579 if (!sock->read_q && !(sock->read_q = create_async_queue( sock->fd ))) return;
580 queue = sock->read_q;
581 break;
582 case ASYNC_TYPE_WRITE:
583 if (!sock->write_q && !(sock->write_q = create_async_queue( sock->fd ))) return;
584 queue = sock->write_q;
585 break;
586 default:
587 set_error( STATUS_INVALID_PARAMETER );
588 return;
591 if ( ( !( sock->state & (FD_READ|FD_CONNECT|FD_WINE_LISTENING) ) && type == ASYNC_TYPE_READ ) ||
592 ( !( sock->state & (FD_WRITE|FD_CONNECT) ) && type == ASYNC_TYPE_WRITE ) )
594 set_error( STATUS_PIPE_DISCONNECTED );
595 return;
598 if (!(async = create_async( current, queue, data ))) return;
599 release_object( async );
601 sock_reselect( sock );
603 set_error( STATUS_PENDING );
606 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
608 struct sock *sock = get_fd_user( fd );
609 sock_reselect( sock );
612 static int sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb )
614 struct sock *sock = get_fd_user( fd );
615 int n = 0;
617 assert( sock->obj.ops == &sock_ops );
619 n += async_wake_up_by( sock->read_q, process, thread, iosb, STATUS_CANCELLED );
620 n += async_wake_up_by( sock->write_q, process, thread, iosb, STATUS_CANCELLED );
621 n += async_wake_up_by( sock->ifchange_q, process, thread, iosb, STATUS_CANCELLED );
622 return n;
625 static struct fd *sock_get_fd( struct object *obj )
627 struct sock *sock = (struct sock *)obj;
628 return (struct fd *)grab_object( sock->fd );
631 static void sock_destroy( struct object *obj )
633 struct sock *sock = (struct sock *)obj;
634 assert( obj->ops == &sock_ops );
636 /* FIXME: special socket shutdown stuff? */
638 if ( sock->deferred )
639 release_object( sock->deferred );
641 free_async_queue( sock->read_q );
642 free_async_queue( sock->write_q );
643 sock_destroy_ifchange_q( sock );
644 if (sock->event) release_object( sock->event );
645 if (sock->fd)
647 /* shut the socket down to force pending poll() calls in the client to return */
648 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
649 release_object( sock->fd );
653 static void init_sock(struct sock *sock)
655 sock->state = 0;
656 sock->mask = 0;
657 sock->hmask = 0;
658 sock->pmask = 0;
659 sock->polling = 0;
660 sock->flags = 0;
661 sock->type = 0;
662 sock->family = 0;
663 sock->event = NULL;
664 sock->window = 0;
665 sock->message = 0;
666 sock->wparam = 0;
667 sock->connect_time = 0;
668 sock->deferred = NULL;
669 sock->read_q = NULL;
670 sock->write_q = NULL;
671 sock->ifchange_q = NULL;
672 sock->ifchange_obj = NULL;
673 memset( sock->errors, 0, sizeof(sock->errors) );
676 /* create a new and unconnected socket */
677 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
679 struct sock *sock;
680 int sockfd;
682 sockfd = socket( family, type, protocol );
683 if (debug_level)
684 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
685 if (sockfd == -1)
687 sock_set_error();
688 return NULL;
690 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
691 if (!(sock = alloc_object( &sock_ops )))
693 close( sockfd );
694 return NULL;
696 init_sock( sock );
697 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
698 sock->flags = flags;
699 sock->proto = protocol;
700 sock->type = type;
701 sock->family = family;
703 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
704 (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
706 release_object( sock );
707 return NULL;
709 sock_reselect( sock );
710 clear_error();
711 return &sock->obj;
714 /* accepts a socket and inits it */
715 static int accept_new_fd( struct sock *sock )
718 /* Try to accept(2). We can't be safe that this an already connected socket
719 * or that accept() is allowed on it. In those cases we will get -1/errno
720 * return.
722 int acceptfd;
723 struct sockaddr saddr;
724 socklen_t slen = sizeof(saddr);
725 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
726 if (acceptfd == -1)
728 sock_set_error();
729 return acceptfd;
732 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
733 return acceptfd;
736 /* accept a socket (creates a new fd) */
737 static struct sock *accept_socket( obj_handle_t handle )
739 struct sock *acceptsock;
740 struct sock *sock;
741 int acceptfd;
743 sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
744 if (!sock)
745 return NULL;
747 if ( sock->deferred )
749 acceptsock = sock->deferred;
750 sock->deferred = NULL;
752 else
754 if ((acceptfd = accept_new_fd( sock )) == -1)
756 release_object( sock );
757 return NULL;
759 if (!(acceptsock = alloc_object( &sock_ops )))
761 close( acceptfd );
762 release_object( sock );
763 return NULL;
766 init_sock( acceptsock );
767 /* newly created socket gets the same properties of the listening socket */
768 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
769 if (sock->state & FD_WINE_NONBLOCKING)
770 acceptsock->state |= FD_WINE_NONBLOCKING;
771 acceptsock->mask = sock->mask;
772 acceptsock->proto = sock->proto;
773 acceptsock->type = sock->type;
774 acceptsock->family = sock->family;
775 acceptsock->window = sock->window;
776 acceptsock->message = sock->message;
777 acceptsock->connect_time = current_time;
778 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
779 acceptsock->flags = sock->flags;
780 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
781 get_fd_options( sock->fd ) )))
783 release_object( acceptsock );
784 release_object( sock );
785 return NULL;
788 clear_error();
789 sock->pmask &= ~FD_ACCEPT;
790 sock->hmask &= ~FD_ACCEPT;
791 sock_reselect( sock );
792 release_object( sock );
793 return acceptsock;
796 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
798 int acceptfd;
799 struct fd *newfd;
800 if ( sock->deferred )
802 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
803 get_fd_options( acceptsock->fd ) );
804 if ( !newfd )
805 return FALSE;
807 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
809 release_object( sock->deferred );
810 sock->deferred = NULL;
812 else
814 if ((acceptfd = accept_new_fd( sock )) == -1)
815 return FALSE;
817 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
818 get_fd_options( acceptsock->fd ) )))
819 return FALSE;
822 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
823 acceptsock->hmask = 0;
824 acceptsock->pmask = 0;
825 acceptsock->polling = 0;
826 acceptsock->proto = sock->proto;
827 acceptsock->type = sock->type;
828 acceptsock->family = sock->family;
829 acceptsock->wparam = 0;
830 acceptsock->deferred = NULL;
831 acceptsock->connect_time = current_time;
832 fd_copy_completion( acceptsock->fd, newfd );
833 release_object( acceptsock->fd );
834 acceptsock->fd = newfd;
836 clear_error();
837 sock->pmask &= ~FD_ACCEPT;
838 sock->hmask &= ~FD_ACCEPT;
839 sock_reselect( sock );
841 return TRUE;
844 /* return an errno value mapped to a WSA error */
845 static int sock_get_error( int err )
847 switch (err)
849 case EINTR: return WSAEINTR;
850 case EBADF: return WSAEBADF;
851 case EPERM:
852 case EACCES: return WSAEACCES;
853 case EFAULT: return WSAEFAULT;
854 case EINVAL: return WSAEINVAL;
855 case EMFILE: return WSAEMFILE;
856 case EWOULDBLOCK: return WSAEWOULDBLOCK;
857 case EINPROGRESS: return WSAEINPROGRESS;
858 case EALREADY: return WSAEALREADY;
859 case ENOTSOCK: return WSAENOTSOCK;
860 case EDESTADDRREQ: return WSAEDESTADDRREQ;
861 case EMSGSIZE: return WSAEMSGSIZE;
862 case EPROTOTYPE: return WSAEPROTOTYPE;
863 case ENOPROTOOPT: return WSAENOPROTOOPT;
864 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
865 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
866 case EOPNOTSUPP: return WSAEOPNOTSUPP;
867 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
868 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
869 case EADDRINUSE: return WSAEADDRINUSE;
870 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
871 case ENETDOWN: return WSAENETDOWN;
872 case ENETUNREACH: return WSAENETUNREACH;
873 case ENETRESET: return WSAENETRESET;
874 case ECONNABORTED: return WSAECONNABORTED;
875 case EPIPE:
876 case ECONNRESET: return WSAECONNRESET;
877 case ENOBUFS: return WSAENOBUFS;
878 case EISCONN: return WSAEISCONN;
879 case ENOTCONN: return WSAENOTCONN;
880 case ESHUTDOWN: return WSAESHUTDOWN;
881 case ETOOMANYREFS: return WSAETOOMANYREFS;
882 case ETIMEDOUT: return WSAETIMEDOUT;
883 case ECONNREFUSED: return WSAECONNREFUSED;
884 case ELOOP: return WSAELOOP;
885 case ENAMETOOLONG: return WSAENAMETOOLONG;
886 case EHOSTDOWN: return WSAEHOSTDOWN;
887 case EHOSTUNREACH: return WSAEHOSTUNREACH;
888 case ENOTEMPTY: return WSAENOTEMPTY;
889 #ifdef EPROCLIM
890 case EPROCLIM: return WSAEPROCLIM;
891 #endif
892 #ifdef EUSERS
893 case EUSERS: return WSAEUSERS;
894 #endif
895 #ifdef EDQUOT
896 case EDQUOT: return WSAEDQUOT;
897 #endif
898 #ifdef ESTALE
899 case ESTALE: return WSAESTALE;
900 #endif
901 #ifdef EREMOTE
902 case EREMOTE: return WSAEREMOTE;
903 #endif
905 case 0: return 0;
906 default:
907 errno = err;
908 perror("wineserver: sock_get_error() can't map error");
909 return WSAEFAULT;
913 static int sock_get_ntstatus( int err )
915 switch ( err )
917 case EBADF: return STATUS_INVALID_HANDLE;
918 case EBUSY: return STATUS_DEVICE_BUSY;
919 case EPERM:
920 case EACCES: return STATUS_ACCESS_DENIED;
921 case EFAULT: return STATUS_NO_MEMORY;
922 case EINVAL: return STATUS_INVALID_PARAMETER;
923 case ENFILE:
924 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
925 case EWOULDBLOCK: return STATUS_CANT_WAIT;
926 case EINPROGRESS: return STATUS_PENDING;
927 case EALREADY: return STATUS_NETWORK_BUSY;
928 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
929 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
930 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
931 case EPROTONOSUPPORT:
932 case ESOCKTNOSUPPORT:
933 case EPFNOSUPPORT:
934 case EAFNOSUPPORT:
935 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
936 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
937 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
938 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
939 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
940 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
941 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
942 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
943 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
944 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
945 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
946 case ENETDOWN: return STATUS_NETWORK_BUSY;
947 case EPIPE:
948 case ECONNRESET: return STATUS_CONNECTION_RESET;
949 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
951 case 0: return STATUS_SUCCESS;
952 default:
953 errno = err;
954 perror("wineserver: sock_get_ntstatus() can't map error");
955 return STATUS_UNSUCCESSFUL;
959 /* set the last error depending on errno */
960 static void sock_set_error(void)
962 set_error( sock_get_ntstatus( errno ) );
965 #ifdef HAVE_LINUX_RTNETLINK_H
967 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
968 static struct object *ifchange_object;
970 static void ifchange_dump( struct object *obj, int verbose );
971 static struct fd *ifchange_get_fd( struct object *obj );
972 static void ifchange_destroy( struct object *obj );
974 static int ifchange_get_poll_events( struct fd *fd );
975 static void ifchange_poll_event( struct fd *fd, int event );
976 static void ifchange_reselect_async( struct fd *fd, struct async_queue *queue );
978 struct ifchange
980 struct object obj; /* object header */
981 struct fd *fd; /* interface change file descriptor */
982 struct list sockets; /* list of sockets to send interface change notifications */
985 static const struct object_ops ifchange_ops =
987 sizeof(struct ifchange), /* size */
988 ifchange_dump, /* dump */
989 no_get_type, /* get_type */
990 add_queue, /* add_queue */
991 NULL, /* remove_queue */
992 NULL, /* signaled */
993 no_satisfied, /* satisfied */
994 no_signal, /* signal */
995 ifchange_get_fd, /* get_fd */
996 default_fd_map_access, /* map_access */
997 default_get_sd, /* get_sd */
998 default_set_sd, /* set_sd */
999 no_lookup_name, /* lookup_name */
1000 no_link_name, /* link_name */
1001 NULL, /* unlink_name */
1002 no_open_file, /* open_file */
1003 no_close_handle, /* close_handle */
1004 ifchange_destroy /* destroy */
1007 static const struct fd_ops ifchange_fd_ops =
1009 ifchange_get_poll_events, /* get_poll_events */
1010 ifchange_poll_event, /* poll_event */
1011 NULL, /* get_fd_type */
1012 no_fd_read, /* read */
1013 no_fd_write, /* write */
1014 no_fd_flush, /* flush */
1015 no_fd_ioctl, /* ioctl */
1016 NULL, /* queue_async */
1017 ifchange_reselect_async, /* reselect_async */
1018 NULL /* cancel_async */
1021 static void ifchange_dump( struct object *obj, int verbose )
1023 assert( obj->ops == &ifchange_ops );
1024 fprintf( stderr, "Interface change\n" );
1027 static struct fd *ifchange_get_fd( struct object *obj )
1029 struct ifchange *ifchange = (struct ifchange *)obj;
1030 return (struct fd *)grab_object( ifchange->fd );
1033 static void ifchange_destroy( struct object *obj )
1035 struct ifchange *ifchange = (struct ifchange *)obj;
1036 assert( obj->ops == &ifchange_ops );
1038 release_object( ifchange->fd );
1040 /* reset the global ifchange object so that it will be recreated if it is needed again */
1041 assert( obj == ifchange_object );
1042 ifchange_object = NULL;
1045 static int ifchange_get_poll_events( struct fd *fd )
1047 return POLLIN;
1050 /* wake up all the sockets waiting for a change notification event */
1051 static void ifchange_wake_up( struct object *obj, unsigned int status )
1053 struct ifchange *ifchange = (struct ifchange *)obj;
1054 struct list *ptr, *next;
1055 assert( obj->ops == &ifchange_ops );
1056 assert( obj == ifchange_object );
1058 LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
1060 struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
1062 assert( sock->ifchange_q );
1063 async_wake_up( sock->ifchange_q, status ); /* issue ifchange notification for the socket */
1064 sock_destroy_ifchange_q( sock ); /* remove socket from list and decrement ifchange refcount */
1068 static void ifchange_poll_event( struct fd *fd, int event )
1070 struct object *ifchange = get_fd_user( fd );
1071 unsigned int status = STATUS_PENDING;
1072 char buffer[PIPE_BUF];
1073 int r;
1075 r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
1076 if (r < 0)
1078 if (errno == EWOULDBLOCK || errno == EAGAIN)
1079 return; /* retry when poll() says the socket is ready */
1080 status = sock_get_ntstatus( errno );
1082 else if (r > 0)
1084 struct nlmsghdr *nlh;
1086 for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
1088 if (nlh->nlmsg_type == NLMSG_DONE)
1089 break;
1090 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
1091 status = STATUS_SUCCESS;
1094 else status = STATUS_CANCELLED;
1096 if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
1099 static void ifchange_reselect_async( struct fd *fd, struct async_queue *queue )
1101 /* do nothing, this object is about to disappear */
1104 #endif
1106 /* we only need one of these interface notification objects, all of the sockets dependent upon
1107 * it will wake up when a notification event occurs */
1108 static struct object *get_ifchange( void )
1110 #ifdef HAVE_LINUX_RTNETLINK_H
1111 struct ifchange *ifchange;
1112 struct sockaddr_nl addr;
1113 int unix_fd;
1115 if (ifchange_object)
1117 /* increment the refcount for each socket that uses the ifchange object */
1118 return grab_object( ifchange_object );
1121 /* create the socket we need for processing interface change notifications */
1122 unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
1123 if (unix_fd == -1)
1125 sock_set_error();
1126 return NULL;
1128 fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
1129 memset( &addr, 0, sizeof(addr) );
1130 addr.nl_family = AF_NETLINK;
1131 addr.nl_groups = RTMGRP_IPV4_IFADDR;
1132 /* bind the socket to the special netlink kernel interface */
1133 if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
1135 close( unix_fd );
1136 sock_set_error();
1137 return NULL;
1139 if (!(ifchange = alloc_object( &ifchange_ops )))
1141 close( unix_fd );
1142 set_error( STATUS_NO_MEMORY );
1143 return NULL;
1145 list_init( &ifchange->sockets );
1146 if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
1148 release_object( ifchange );
1149 set_error( STATUS_NO_MEMORY );
1150 return NULL;
1152 set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
1154 /* the ifchange object is now successfully configured */
1155 ifchange_object = &ifchange->obj;
1156 return &ifchange->obj;
1157 #else
1158 set_error( STATUS_NOT_SUPPORTED );
1159 return NULL;
1160 #endif
1163 /* add the socket to the interface change notification list */
1164 static void ifchange_add_sock( struct object *obj, struct sock *sock )
1166 #ifdef HAVE_LINUX_RTNETLINK_H
1167 struct ifchange *ifchange = (struct ifchange *)obj;
1169 list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
1170 #endif
1173 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1174 static struct async_queue *sock_get_ifchange_q( struct sock *sock )
1176 struct object *ifchange;
1177 struct fd *fd;
1179 if (sock->ifchange_q) /* reuse existing ifchange_q for this socket */
1180 return sock->ifchange_q;
1182 if (!(ifchange = get_ifchange()))
1183 return NULL;
1185 /* create the ifchange notification queue */
1186 fd = get_obj_fd( ifchange );
1187 sock->ifchange_q = create_async_queue( fd );
1188 release_object( fd );
1189 if (!sock->ifchange_q)
1191 release_object( ifchange );
1192 set_error( STATUS_NO_MEMORY );
1193 return NULL;
1196 /* add the socket to the ifchange notification list */
1197 ifchange_add_sock( ifchange, sock );
1198 sock->ifchange_obj = ifchange;
1199 return sock->ifchange_q;
1202 /* destroy an existing ifchange queue for a specific socket */
1203 static void sock_destroy_ifchange_q( struct sock *sock )
1205 if (sock->ifchange_q)
1207 list_remove( &sock->ifchange_entry );
1208 free_async_queue( sock->ifchange_q );
1209 sock->ifchange_q = NULL;
1210 release_object( sock->ifchange_obj );
1214 /* create a socket */
1215 DECL_HANDLER(create_socket)
1217 struct object *obj;
1219 reply->handle = 0;
1220 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
1222 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1223 release_object( obj );
1227 /* accept a socket */
1228 DECL_HANDLER(accept_socket)
1230 struct sock *sock;
1232 reply->handle = 0;
1233 if ((sock = accept_socket( req->lhandle )) != NULL)
1235 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
1236 sock->wparam = reply->handle; /* wparam for message is the socket handle */
1237 sock_reselect( sock );
1238 release_object( &sock->obj );
1242 /* accept a socket into an initialized socket */
1243 DECL_HANDLER(accept_into_socket)
1245 struct sock *sock, *acceptsock;
1246 const int all_attributes = FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|FILE_READ_DATA;
1248 if (!(sock = (struct sock *)get_handle_obj( current->process, req->lhandle,
1249 all_attributes, &sock_ops)))
1250 return;
1252 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, req->ahandle,
1253 all_attributes, &sock_ops)))
1255 release_object( sock );
1256 return;
1259 if (accept_into_socket( sock, acceptsock ))
1261 acceptsock->wparam = req->ahandle; /* wparam for message is the socket handle */
1262 sock_reselect( acceptsock );
1264 release_object( acceptsock );
1265 release_object( sock );
1268 /* set socket event parameters */
1269 DECL_HANDLER(set_socket_event)
1271 struct sock *sock;
1272 struct event *old_event;
1274 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1275 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
1276 old_event = sock->event;
1277 sock->mask = req->mask;
1278 sock->hmask &= ~req->mask; /* re-enable held events */
1279 sock->event = NULL;
1280 sock->window = req->window;
1281 sock->message = req->msg;
1282 sock->wparam = req->handle; /* wparam is the socket handle */
1283 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
1285 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
1287 sock_reselect( sock );
1289 sock->state |= FD_WINE_NONBLOCKING;
1291 /* if a network event is pending, signal the event object
1292 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1293 before a WSAEventSelect() was done on it.
1294 (when dealing with Asynchronous socket) */
1295 sock_wake_up( sock );
1297 if (old_event) release_object( old_event ); /* we're through with it */
1298 release_object( &sock->obj );
1301 /* get socket event parameters */
1302 DECL_HANDLER(get_socket_event)
1304 struct sock *sock;
1305 int i;
1306 int errors[FD_MAX_EVENTS];
1308 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1309 if (!sock)
1311 reply->mask = 0;
1312 reply->pmask = 0;
1313 reply->state = 0;
1314 return;
1316 reply->mask = sock->mask;
1317 reply->pmask = sock->pmask;
1318 reply->state = sock->state;
1319 for (i = 0; i < FD_MAX_EVENTS; i++)
1320 errors[i] = sock_get_ntstatus(sock->errors[i]);
1322 set_reply_data( errors, min( get_reply_max_size(), sizeof(errors) ));
1324 if (req->service)
1326 if (req->c_event)
1328 struct event *cevent = get_event_obj( current->process, req->c_event,
1329 EVENT_MODIFY_STATE );
1330 if (cevent)
1332 reset_event( cevent );
1333 release_object( cevent );
1336 sock->pmask = 0;
1337 sock_reselect( sock );
1339 release_object( &sock->obj );
1342 /* re-enable pending socket events */
1343 DECL_HANDLER(enable_socket_event)
1345 struct sock *sock;
1347 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
1348 FILE_WRITE_ATTRIBUTES, &sock_ops)))
1349 return;
1351 /* for event-based notification, windows erases stale events */
1352 sock->pmask &= ~req->mask;
1354 sock->hmask &= ~req->mask;
1355 sock->state |= req->sstate;
1356 sock->state &= ~req->cstate;
1357 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
1359 sock_reselect( sock );
1361 release_object( &sock->obj );
1364 DECL_HANDLER(set_socket_deferred)
1366 struct sock *sock, *acceptsock;
1368 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
1369 if ( !sock )
1370 return;
1372 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
1373 if ( !acceptsock )
1375 release_object( sock );
1376 return;
1378 sock->deferred = acceptsock;
1379 release_object( sock );
1382 DECL_HANDLER(get_socket_info)
1384 struct sock *sock;
1386 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1387 if (!sock) return;
1389 reply->family = sock->family;
1390 reply->type = sock->type;
1391 reply->protocol = sock->proto;
1393 release_object( &sock->obj );