wined3d: Add ARB_shader_image_load_store extension.
[wine.git] / server / sock.c
blob0de6f68025cc7524d7fafdbeca083d5839d3dbb8
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 /* ignore reselect on ifchange queue */
610 if (sock->ifchange_q != queue)
611 sock_reselect( sock );
614 static int sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb )
616 struct sock *sock = get_fd_user( fd );
617 int n = 0;
619 assert( sock->obj.ops == &sock_ops );
621 n += async_wake_up_by( sock->read_q, process, thread, iosb, STATUS_CANCELLED );
622 n += async_wake_up_by( sock->write_q, process, thread, iosb, STATUS_CANCELLED );
623 n += async_wake_up_by( sock->ifchange_q, process, thread, iosb, STATUS_CANCELLED );
624 return n;
627 static struct fd *sock_get_fd( struct object *obj )
629 struct sock *sock = (struct sock *)obj;
630 return (struct fd *)grab_object( sock->fd );
633 static void sock_destroy( struct object *obj )
635 struct sock *sock = (struct sock *)obj;
636 assert( obj->ops == &sock_ops );
638 /* FIXME: special socket shutdown stuff? */
640 if ( sock->deferred )
641 release_object( sock->deferred );
643 free_async_queue( sock->read_q );
644 free_async_queue( sock->write_q );
645 async_wake_up( sock->ifchange_q, STATUS_CANCELLED );
646 sock_destroy_ifchange_q( sock );
647 if (sock->event) release_object( sock->event );
648 if (sock->fd)
650 /* shut the socket down to force pending poll() calls in the client to return */
651 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
652 release_object( sock->fd );
656 static void init_sock(struct sock *sock)
658 sock->state = 0;
659 sock->mask = 0;
660 sock->hmask = 0;
661 sock->pmask = 0;
662 sock->polling = 0;
663 sock->flags = 0;
664 sock->type = 0;
665 sock->family = 0;
666 sock->event = NULL;
667 sock->window = 0;
668 sock->message = 0;
669 sock->wparam = 0;
670 sock->connect_time = 0;
671 sock->deferred = NULL;
672 sock->read_q = NULL;
673 sock->write_q = NULL;
674 sock->ifchange_q = NULL;
675 sock->ifchange_obj = NULL;
676 memset( sock->errors, 0, sizeof(sock->errors) );
679 /* create a new and unconnected socket */
680 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
682 struct sock *sock;
683 int sockfd;
685 sockfd = socket( family, type, protocol );
686 if (debug_level)
687 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
688 if (sockfd == -1)
690 sock_set_error();
691 return NULL;
693 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
694 if (!(sock = alloc_object( &sock_ops )))
696 close( sockfd );
697 return NULL;
699 init_sock( sock );
700 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
701 sock->flags = flags;
702 sock->proto = protocol;
703 sock->type = type;
704 sock->family = family;
706 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
707 (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
709 release_object( sock );
710 return NULL;
712 sock_reselect( sock );
713 clear_error();
714 return &sock->obj;
717 /* accepts a socket and inits it */
718 static int accept_new_fd( struct sock *sock )
721 /* Try to accept(2). We can't be safe that this an already connected socket
722 * or that accept() is allowed on it. In those cases we will get -1/errno
723 * return.
725 int acceptfd;
726 struct sockaddr saddr;
727 socklen_t slen = sizeof(saddr);
728 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
729 if (acceptfd == -1)
731 sock_set_error();
732 return acceptfd;
735 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
736 return acceptfd;
739 /* accept a socket (creates a new fd) */
740 static struct sock *accept_socket( obj_handle_t handle )
742 struct sock *acceptsock;
743 struct sock *sock;
744 int acceptfd;
746 sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
747 if (!sock)
748 return NULL;
750 if ( sock->deferred )
752 acceptsock = sock->deferred;
753 sock->deferred = NULL;
755 else
757 if ((acceptfd = accept_new_fd( sock )) == -1)
759 release_object( sock );
760 return NULL;
762 if (!(acceptsock = alloc_object( &sock_ops )))
764 close( acceptfd );
765 release_object( sock );
766 return NULL;
769 init_sock( acceptsock );
770 /* newly created socket gets the same properties of the listening socket */
771 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
772 if (sock->state & FD_WINE_NONBLOCKING)
773 acceptsock->state |= FD_WINE_NONBLOCKING;
774 acceptsock->mask = sock->mask;
775 acceptsock->proto = sock->proto;
776 acceptsock->type = sock->type;
777 acceptsock->family = sock->family;
778 acceptsock->window = sock->window;
779 acceptsock->message = sock->message;
780 acceptsock->connect_time = current_time;
781 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
782 acceptsock->flags = sock->flags;
783 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
784 get_fd_options( sock->fd ) )))
786 release_object( acceptsock );
787 release_object( sock );
788 return NULL;
791 clear_error();
792 sock->pmask &= ~FD_ACCEPT;
793 sock->hmask &= ~FD_ACCEPT;
794 sock_reselect( sock );
795 release_object( sock );
796 return acceptsock;
799 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
801 int acceptfd;
802 struct fd *newfd;
803 if ( sock->deferred )
805 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
806 get_fd_options( acceptsock->fd ) );
807 if ( !newfd )
808 return FALSE;
810 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
812 release_object( sock->deferred );
813 sock->deferred = NULL;
815 else
817 if ((acceptfd = accept_new_fd( sock )) == -1)
818 return FALSE;
820 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
821 get_fd_options( acceptsock->fd ) )))
822 return FALSE;
825 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
826 acceptsock->hmask = 0;
827 acceptsock->pmask = 0;
828 acceptsock->polling = 0;
829 acceptsock->proto = sock->proto;
830 acceptsock->type = sock->type;
831 acceptsock->family = sock->family;
832 acceptsock->wparam = 0;
833 acceptsock->deferred = NULL;
834 acceptsock->connect_time = current_time;
835 fd_copy_completion( acceptsock->fd, newfd );
836 release_object( acceptsock->fd );
837 acceptsock->fd = newfd;
839 clear_error();
840 sock->pmask &= ~FD_ACCEPT;
841 sock->hmask &= ~FD_ACCEPT;
842 sock_reselect( sock );
844 return TRUE;
847 /* return an errno value mapped to a WSA error */
848 static int sock_get_error( int err )
850 switch (err)
852 case EINTR: return WSAEINTR;
853 case EBADF: return WSAEBADF;
854 case EPERM:
855 case EACCES: return WSAEACCES;
856 case EFAULT: return WSAEFAULT;
857 case EINVAL: return WSAEINVAL;
858 case EMFILE: return WSAEMFILE;
859 case EWOULDBLOCK: return WSAEWOULDBLOCK;
860 case EINPROGRESS: return WSAEINPROGRESS;
861 case EALREADY: return WSAEALREADY;
862 case ENOTSOCK: return WSAENOTSOCK;
863 case EDESTADDRREQ: return WSAEDESTADDRREQ;
864 case EMSGSIZE: return WSAEMSGSIZE;
865 case EPROTOTYPE: return WSAEPROTOTYPE;
866 case ENOPROTOOPT: return WSAENOPROTOOPT;
867 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
868 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
869 case EOPNOTSUPP: return WSAEOPNOTSUPP;
870 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
871 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
872 case EADDRINUSE: return WSAEADDRINUSE;
873 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
874 case ENETDOWN: return WSAENETDOWN;
875 case ENETUNREACH: return WSAENETUNREACH;
876 case ENETRESET: return WSAENETRESET;
877 case ECONNABORTED: return WSAECONNABORTED;
878 case EPIPE:
879 case ECONNRESET: return WSAECONNRESET;
880 case ENOBUFS: return WSAENOBUFS;
881 case EISCONN: return WSAEISCONN;
882 case ENOTCONN: return WSAENOTCONN;
883 case ESHUTDOWN: return WSAESHUTDOWN;
884 case ETOOMANYREFS: return WSAETOOMANYREFS;
885 case ETIMEDOUT: return WSAETIMEDOUT;
886 case ECONNREFUSED: return WSAECONNREFUSED;
887 case ELOOP: return WSAELOOP;
888 case ENAMETOOLONG: return WSAENAMETOOLONG;
889 case EHOSTDOWN: return WSAEHOSTDOWN;
890 case EHOSTUNREACH: return WSAEHOSTUNREACH;
891 case ENOTEMPTY: return WSAENOTEMPTY;
892 #ifdef EPROCLIM
893 case EPROCLIM: return WSAEPROCLIM;
894 #endif
895 #ifdef EUSERS
896 case EUSERS: return WSAEUSERS;
897 #endif
898 #ifdef EDQUOT
899 case EDQUOT: return WSAEDQUOT;
900 #endif
901 #ifdef ESTALE
902 case ESTALE: return WSAESTALE;
903 #endif
904 #ifdef EREMOTE
905 case EREMOTE: return WSAEREMOTE;
906 #endif
908 case 0: return 0;
909 default:
910 errno = err;
911 perror("wineserver: sock_get_error() can't map error");
912 return WSAEFAULT;
916 static int sock_get_ntstatus( int err )
918 switch ( err )
920 case EBADF: return STATUS_INVALID_HANDLE;
921 case EBUSY: return STATUS_DEVICE_BUSY;
922 case EPERM:
923 case EACCES: return STATUS_ACCESS_DENIED;
924 case EFAULT: return STATUS_NO_MEMORY;
925 case EINVAL: return STATUS_INVALID_PARAMETER;
926 case ENFILE:
927 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
928 case EWOULDBLOCK: return STATUS_CANT_WAIT;
929 case EINPROGRESS: return STATUS_PENDING;
930 case EALREADY: return STATUS_NETWORK_BUSY;
931 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
932 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
933 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
934 case EPROTONOSUPPORT:
935 case ESOCKTNOSUPPORT:
936 case EPFNOSUPPORT:
937 case EAFNOSUPPORT:
938 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
939 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
940 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
941 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
942 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
943 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
944 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
945 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
946 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
947 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
948 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
949 case ENETDOWN: return STATUS_NETWORK_BUSY;
950 case EPIPE:
951 case ECONNRESET: return STATUS_CONNECTION_RESET;
952 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
954 case 0: return STATUS_SUCCESS;
955 default:
956 errno = err;
957 perror("wineserver: sock_get_ntstatus() can't map error");
958 return STATUS_UNSUCCESSFUL;
962 /* set the last error depending on errno */
963 static void sock_set_error(void)
965 set_error( sock_get_ntstatus( errno ) );
968 #ifdef HAVE_LINUX_RTNETLINK_H
970 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
971 static struct object *ifchange_object;
973 static void ifchange_dump( struct object *obj, int verbose );
974 static struct fd *ifchange_get_fd( struct object *obj );
975 static void ifchange_destroy( struct object *obj );
977 static int ifchange_get_poll_events( struct fd *fd );
978 static void ifchange_poll_event( struct fd *fd, int event );
980 struct ifchange
982 struct object obj; /* object header */
983 struct fd *fd; /* interface change file descriptor */
984 struct list sockets; /* list of sockets to send interface change notifications */
987 static const struct object_ops ifchange_ops =
989 sizeof(struct ifchange), /* size */
990 ifchange_dump, /* dump */
991 no_get_type, /* get_type */
992 add_queue, /* add_queue */
993 NULL, /* remove_queue */
994 NULL, /* signaled */
995 no_satisfied, /* satisfied */
996 no_signal, /* signal */
997 ifchange_get_fd, /* get_fd */
998 default_fd_map_access, /* map_access */
999 default_get_sd, /* get_sd */
1000 default_set_sd, /* set_sd */
1001 no_lookup_name, /* lookup_name */
1002 no_link_name, /* link_name */
1003 NULL, /* unlink_name */
1004 no_open_file, /* open_file */
1005 no_close_handle, /* close_handle */
1006 ifchange_destroy /* destroy */
1009 static const struct fd_ops ifchange_fd_ops =
1011 ifchange_get_poll_events, /* get_poll_events */
1012 ifchange_poll_event, /* poll_event */
1013 NULL, /* get_fd_type */
1014 no_fd_read, /* read */
1015 no_fd_write, /* write */
1016 no_fd_flush, /* flush */
1017 no_fd_ioctl, /* ioctl */
1018 NULL, /* queue_async */
1019 NULL, /* reselect_async */
1020 NULL /* cancel_async */
1023 static void ifchange_dump( struct object *obj, int verbose )
1025 assert( obj->ops == &ifchange_ops );
1026 fprintf( stderr, "Interface change\n" );
1029 static struct fd *ifchange_get_fd( struct object *obj )
1031 struct ifchange *ifchange = (struct ifchange *)obj;
1032 return (struct fd *)grab_object( ifchange->fd );
1035 static void ifchange_destroy( struct object *obj )
1037 struct ifchange *ifchange = (struct ifchange *)obj;
1038 assert( obj->ops == &ifchange_ops );
1040 release_object( ifchange->fd );
1042 /* reset the global ifchange object so that it will be recreated if it is needed again */
1043 assert( obj == ifchange_object );
1044 ifchange_object = NULL;
1047 static int ifchange_get_poll_events( struct fd *fd )
1049 return POLLIN;
1052 /* wake up all the sockets waiting for a change notification event */
1053 static void ifchange_wake_up( struct object *obj, unsigned int status )
1055 struct ifchange *ifchange = (struct ifchange *)obj;
1056 struct list *ptr, *next;
1057 assert( obj->ops == &ifchange_ops );
1058 assert( obj == ifchange_object );
1060 LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
1062 struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
1064 assert( sock->ifchange_q );
1065 async_wake_up( sock->ifchange_q, status ); /* issue ifchange notification for the socket */
1066 sock_destroy_ifchange_q( sock ); /* remove socket from list and decrement ifchange refcount */
1070 static void ifchange_poll_event( struct fd *fd, int event )
1072 struct object *ifchange = get_fd_user( fd );
1073 unsigned int status = STATUS_PENDING;
1074 char buffer[PIPE_BUF];
1075 int r;
1077 r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
1078 if (r < 0)
1080 if (errno == EWOULDBLOCK || (EWOULDBLOCK != EAGAIN && errno == EAGAIN))
1081 return; /* retry when poll() says the socket is ready */
1082 status = sock_get_ntstatus( errno );
1084 else if (r > 0)
1086 struct nlmsghdr *nlh;
1088 for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
1090 if (nlh->nlmsg_type == NLMSG_DONE)
1091 break;
1092 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
1093 status = STATUS_SUCCESS;
1096 else status = STATUS_CANCELLED;
1098 if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
1101 #endif
1103 /* we only need one of these interface notification objects, all of the sockets dependent upon
1104 * it will wake up when a notification event occurs */
1105 static struct object *get_ifchange( void )
1107 #ifdef HAVE_LINUX_RTNETLINK_H
1108 struct ifchange *ifchange;
1109 struct sockaddr_nl addr;
1110 int unix_fd;
1112 if (ifchange_object)
1114 /* increment the refcount for each socket that uses the ifchange object */
1115 return grab_object( ifchange_object );
1118 /* create the socket we need for processing interface change notifications */
1119 unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
1120 if (unix_fd == -1)
1122 sock_set_error();
1123 return NULL;
1125 fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
1126 memset( &addr, 0, sizeof(addr) );
1127 addr.nl_family = AF_NETLINK;
1128 addr.nl_groups = RTMGRP_IPV4_IFADDR;
1129 /* bind the socket to the special netlink kernel interface */
1130 if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
1132 close( unix_fd );
1133 sock_set_error();
1134 return NULL;
1136 if (!(ifchange = alloc_object( &ifchange_ops )))
1138 close( unix_fd );
1139 set_error( STATUS_NO_MEMORY );
1140 return NULL;
1142 list_init( &ifchange->sockets );
1143 if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
1145 release_object( ifchange );
1146 set_error( STATUS_NO_MEMORY );
1147 return NULL;
1149 set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
1151 /* the ifchange object is now successfully configured */
1152 ifchange_object = &ifchange->obj;
1153 return &ifchange->obj;
1154 #else
1155 set_error( STATUS_NOT_SUPPORTED );
1156 return NULL;
1157 #endif
1160 /* add the socket to the interface change notification list */
1161 static void ifchange_add_sock( struct object *obj, struct sock *sock )
1163 #ifdef HAVE_LINUX_RTNETLINK_H
1164 struct ifchange *ifchange = (struct ifchange *)obj;
1166 list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
1167 #endif
1170 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1171 static struct async_queue *sock_get_ifchange_q( struct sock *sock )
1173 struct object *ifchange;
1175 if (sock->ifchange_q) /* reuse existing ifchange_q for this socket */
1176 return sock->ifchange_q;
1178 if (!(ifchange = get_ifchange()))
1179 return NULL;
1181 /* create the ifchange notification queue */
1182 sock->ifchange_q = create_async_queue( sock->fd );
1183 if (!sock->ifchange_q)
1185 release_object( ifchange );
1186 set_error( STATUS_NO_MEMORY );
1187 return NULL;
1190 /* add the socket to the ifchange notification list */
1191 ifchange_add_sock( ifchange, sock );
1192 sock->ifchange_obj = ifchange;
1193 return sock->ifchange_q;
1196 /* destroy an existing ifchange queue for a specific socket */
1197 static void sock_destroy_ifchange_q( struct sock *sock )
1199 if (sock->ifchange_q)
1201 list_remove( &sock->ifchange_entry );
1202 free_async_queue( sock->ifchange_q );
1203 sock->ifchange_q = NULL;
1204 release_object( sock->ifchange_obj );
1208 /* create a socket */
1209 DECL_HANDLER(create_socket)
1211 struct object *obj;
1213 reply->handle = 0;
1214 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
1216 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1217 release_object( obj );
1221 /* accept a socket */
1222 DECL_HANDLER(accept_socket)
1224 struct sock *sock;
1226 reply->handle = 0;
1227 if ((sock = accept_socket( req->lhandle )) != NULL)
1229 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
1230 sock->wparam = reply->handle; /* wparam for message is the socket handle */
1231 sock_reselect( sock );
1232 release_object( &sock->obj );
1236 /* accept a socket into an initialized socket */
1237 DECL_HANDLER(accept_into_socket)
1239 struct sock *sock, *acceptsock;
1240 const int all_attributes = FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|FILE_READ_DATA;
1242 if (!(sock = (struct sock *)get_handle_obj( current->process, req->lhandle,
1243 all_attributes, &sock_ops)))
1244 return;
1246 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, req->ahandle,
1247 all_attributes, &sock_ops)))
1249 release_object( sock );
1250 return;
1253 if (accept_into_socket( sock, acceptsock ))
1255 acceptsock->wparam = req->ahandle; /* wparam for message is the socket handle */
1256 sock_reselect( acceptsock );
1258 release_object( acceptsock );
1259 release_object( sock );
1262 /* set socket event parameters */
1263 DECL_HANDLER(set_socket_event)
1265 struct sock *sock;
1266 struct event *old_event;
1268 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1269 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
1270 old_event = sock->event;
1271 sock->mask = req->mask;
1272 sock->hmask &= ~req->mask; /* re-enable held events */
1273 sock->event = NULL;
1274 sock->window = req->window;
1275 sock->message = req->msg;
1276 sock->wparam = req->handle; /* wparam is the socket handle */
1277 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
1279 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
1281 sock_reselect( sock );
1283 sock->state |= FD_WINE_NONBLOCKING;
1285 /* if a network event is pending, signal the event object
1286 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1287 before a WSAEventSelect() was done on it.
1288 (when dealing with Asynchronous socket) */
1289 sock_wake_up( sock );
1291 if (old_event) release_object( old_event ); /* we're through with it */
1292 release_object( &sock->obj );
1295 /* get socket event parameters */
1296 DECL_HANDLER(get_socket_event)
1298 struct sock *sock;
1299 int i;
1300 int errors[FD_MAX_EVENTS];
1302 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1303 if (!sock)
1305 reply->mask = 0;
1306 reply->pmask = 0;
1307 reply->state = 0;
1308 return;
1310 reply->mask = sock->mask;
1311 reply->pmask = sock->pmask;
1312 reply->state = sock->state;
1313 for (i = 0; i < FD_MAX_EVENTS; i++)
1314 errors[i] = sock_get_ntstatus(sock->errors[i]);
1316 set_reply_data( errors, min( get_reply_max_size(), sizeof(errors) ));
1318 if (req->service)
1320 if (req->c_event)
1322 struct event *cevent = get_event_obj( current->process, req->c_event,
1323 EVENT_MODIFY_STATE );
1324 if (cevent)
1326 reset_event( cevent );
1327 release_object( cevent );
1330 sock->pmask = 0;
1331 sock_reselect( sock );
1333 release_object( &sock->obj );
1336 /* re-enable pending socket events */
1337 DECL_HANDLER(enable_socket_event)
1339 struct sock *sock;
1341 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
1342 FILE_WRITE_ATTRIBUTES, &sock_ops)))
1343 return;
1345 /* for event-based notification, windows erases stale events */
1346 sock->pmask &= ~req->mask;
1348 sock->hmask &= ~req->mask;
1349 sock->state |= req->sstate;
1350 sock->state &= ~req->cstate;
1351 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
1353 sock_reselect( sock );
1355 release_object( &sock->obj );
1358 DECL_HANDLER(set_socket_deferred)
1360 struct sock *sock, *acceptsock;
1362 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
1363 if ( !sock )
1364 return;
1366 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
1367 if ( !acceptsock )
1369 release_object( sock );
1370 return;
1372 sock->deferred = acceptsock;
1373 release_object( sock );
1376 DECL_HANDLER(get_socket_info)
1378 struct sock *sock;
1380 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1381 if (!sock) return;
1383 reply->family = sock->family;
1384 reply->type = sock->type;
1385 reply->protocol = sock->proto;
1387 release_object( &sock->obj );