wined3d: Record sampler usage for WINED3DSIH_LD_RAW.
[wine.git] / server / sock.c
blobf70a85de1a5fb2b4c9d8149250e071e3b9f2eb3b
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 );
136 static int sock_get_ntstatus( int err );
137 static int sock_get_error( int err );
138 static void sock_set_error(void);
140 static const struct object_ops sock_ops =
142 sizeof(struct sock), /* size */
143 sock_dump, /* dump */
144 no_get_type, /* get_type */
145 add_queue, /* add_queue */
146 remove_queue, /* remove_queue */
147 sock_signaled, /* signaled */
148 no_satisfied, /* satisfied */
149 no_signal, /* signal */
150 sock_get_fd, /* get_fd */
151 default_fd_map_access, /* map_access */
152 default_get_sd, /* get_sd */
153 default_set_sd, /* set_sd */
154 no_lookup_name, /* lookup_name */
155 no_link_name, /* link_name */
156 NULL, /* unlink_name */
157 no_open_file, /* open_file */
158 fd_close_handle, /* close_handle */
159 sock_destroy /* destroy */
162 static const struct fd_ops sock_fd_ops =
164 sock_get_poll_events, /* get_poll_events */
165 sock_poll_event, /* poll_event */
166 sock_get_fd_type, /* get_fd_type */
167 no_fd_read, /* read */
168 no_fd_write, /* write */
169 no_fd_flush, /* flush */
170 sock_ioctl, /* ioctl */
171 sock_queue_async, /* queue_async */
172 sock_reselect_async /* reselect_async */
176 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
177 * we post messages if there are multiple events. Used to send
178 * messages. The problem is if there is both a FD_CONNECT event and,
179 * say, an FD_READ event available on the same socket, we want to
180 * notify the app of the connect event first. Otherwise it may
181 * discard the read event because it thinks it hasn't connected yet.
183 static const int event_bitorder[FD_MAX_EVENTS] =
185 FD_CONNECT_BIT,
186 FD_ACCEPT_BIT,
187 FD_OOB_BIT,
188 FD_WRITE_BIT,
189 FD_READ_BIT,
190 FD_CLOSE_BIT,
191 6, 7, 8, 9 /* leftovers */
194 /* Flags that make sense only for SOCK_STREAM sockets */
195 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
197 typedef enum {
198 SOCK_SHUTDOWN_ERROR = -1,
199 SOCK_SHUTDOWN_EOF = 0,
200 SOCK_SHUTDOWN_POLLHUP = 1
201 } sock_shutdown_t;
203 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
205 static sock_shutdown_t sock_check_pollhup(void)
207 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
208 int fd[2], n;
209 struct pollfd pfd;
210 char dummy;
212 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
213 if ( shutdown( fd[0], 1 ) ) goto out;
215 pfd.fd = fd[1];
216 pfd.events = POLLIN;
217 pfd.revents = 0;
219 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
220 n = poll( &pfd, 1, 1 );
221 if ( n != 1 ) goto out; /* error or timeout */
222 if ( pfd.revents & POLLHUP )
223 ret = SOCK_SHUTDOWN_POLLHUP;
224 else if ( pfd.revents & POLLIN &&
225 read( fd[1], &dummy, 1 ) == 0 )
226 ret = SOCK_SHUTDOWN_EOF;
228 out:
229 close( fd[0] );
230 close( fd[1] );
231 return ret;
234 void sock_init(void)
236 sock_shutdown_type = sock_check_pollhup();
238 switch ( sock_shutdown_type )
240 case SOCK_SHUTDOWN_EOF:
241 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
242 break;
243 case SOCK_SHUTDOWN_POLLHUP:
244 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
245 break;
246 default:
247 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
248 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
252 static int sock_reselect( struct sock *sock )
254 int ev = sock_get_poll_events( sock->fd );
256 if (debug_level)
257 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
259 if (!sock->polling) /* FIXME: should find a better way to do this */
261 /* previously unconnected socket, is this reselect supposed to connect it? */
262 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
263 /* ok, it is, attach it to the wineserver's main poll loop */
264 sock->polling = 1;
265 allow_fd_caching( sock->fd );
267 /* update condition mask */
268 set_fd_events( sock->fd, ev );
269 return ev;
272 /* wake anybody waiting on the socket event or send the associated message */
273 static void sock_wake_up( struct sock *sock )
275 unsigned int events = sock->pmask & sock->mask;
276 int i;
278 if ( !events ) return;
280 if (sock->event)
282 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
283 set_event( sock->event );
285 if (sock->window)
287 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
288 for (i = 0; i < FD_MAX_EVENTS; i++)
290 int event = event_bitorder[i];
291 if (sock->pmask & (1 << event))
293 lparam_t lparam = (1 << event) | (sock_get_error(sock->errors[event]) << 16);
294 post_message( sock->window, sock->message, sock->wparam, lparam );
297 sock->pmask = 0;
298 sock_reselect( sock );
302 static inline int sock_error( struct fd *fd )
304 unsigned int optval = 0;
305 socklen_t optlen = sizeof(optval);
307 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
308 return optval;
311 static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
313 if ( sock->flags & WSA_FLAG_OVERLAPPED )
315 if ( event & (POLLIN|POLLPRI) && async_waiting( sock->read_q ) )
317 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
318 async_wake_up( sock->read_q, STATUS_ALERTED );
319 event &= ~(POLLIN|POLLPRI);
321 if ( event & POLLOUT && async_waiting( sock->write_q ) )
323 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
324 async_wake_up( sock->write_q, STATUS_ALERTED );
325 event &= ~POLLOUT;
327 if ( event & (POLLERR|POLLHUP) )
329 int status = sock_get_ntstatus( error );
331 if ( !(sock->state & FD_READ) )
332 async_wake_up( sock->read_q, status );
333 if ( !(sock->state & FD_WRITE) )
334 async_wake_up( sock->write_q, status );
337 return event;
340 static void sock_dispatch_events( struct sock *sock, int prevstate, int event, int error )
342 if (prevstate & FD_CONNECT)
344 sock->pmask |= FD_CONNECT;
345 sock->hmask |= FD_CONNECT;
346 sock->errors[FD_CONNECT_BIT] = error;
347 goto end;
349 if (prevstate & FD_WINE_LISTENING)
351 sock->pmask |= FD_ACCEPT;
352 sock->hmask |= FD_ACCEPT;
353 sock->errors[FD_ACCEPT_BIT] = error;
354 goto end;
357 if (event & POLLIN)
359 sock->pmask |= FD_READ;
360 sock->hmask |= FD_READ;
361 sock->errors[FD_READ_BIT] = 0;
364 if (event & POLLOUT)
366 sock->pmask |= FD_WRITE;
367 sock->hmask |= FD_WRITE;
368 sock->errors[FD_WRITE_BIT] = 0;
371 if (event & POLLPRI)
373 sock->pmask |= FD_OOB;
374 sock->hmask |= FD_OOB;
375 sock->errors[FD_OOB_BIT] = 0;
378 if (event & (POLLERR|POLLHUP))
380 sock->pmask |= FD_CLOSE;
381 sock->hmask |= FD_CLOSE;
382 sock->errors[FD_CLOSE_BIT] = error;
384 end:
385 sock_wake_up( sock );
388 static void sock_poll_event( struct fd *fd, int event )
390 struct sock *sock = get_fd_user( fd );
391 int hangup_seen = 0;
392 int prevstate = sock->state;
393 int error = 0;
395 assert( sock->obj.ops == &sock_ops );
396 if (debug_level)
397 fprintf(stderr, "socket %p select event: %x\n", sock, event);
399 /* we may change event later, remove from loop here */
400 if (event & (POLLERR|POLLHUP)) set_fd_events( sock->fd, -1 );
402 if (sock->state & FD_CONNECT)
404 if (event & (POLLERR|POLLHUP))
406 /* we didn't get connected? */
407 sock->state &= ~FD_CONNECT;
408 event &= ~POLLOUT;
409 error = sock_error( fd );
411 else if (event & POLLOUT)
413 /* we got connected */
414 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
415 sock->state &= ~FD_CONNECT;
416 sock->connect_time = current_time;
419 else if (sock->state & FD_WINE_LISTENING)
421 /* listening */
422 if (event & (POLLERR|POLLHUP))
423 error = sock_error( fd );
425 else
427 /* normal data flow */
428 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
430 char dummy;
431 int nr;
433 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
434 * has been closed, so we need to check for it explicitly here */
435 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
436 if ( nr == 0 )
438 hangup_seen = 1;
439 event &= ~POLLIN;
441 else if ( nr < 0 )
443 event &= ~POLLIN;
444 /* EAGAIN can happen if an async recv() falls between the server's poll()
445 call and the invocation of this routine */
446 if ( errno != EAGAIN )
448 error = errno;
449 event |= POLLERR;
450 if ( debug_level )
451 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
456 if ( (hangup_seen || event & (POLLHUP|POLLERR)) && (sock->state & (FD_READ|FD_WRITE)) )
458 error = error ? error : sock_error( fd );
459 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
460 sock->state &= ~FD_WRITE;
461 sock->state &= ~FD_READ;
463 if (debug_level)
464 fprintf(stderr, "socket %p aborted by error %d, event: %x\n", sock, error, event);
467 if (hangup_seen)
468 event |= POLLHUP;
471 event = sock_dispatch_asyncs( sock, event, error );
472 sock_dispatch_events( sock, prevstate, event, error );
474 /* if anyone is stupid enough to wait on the socket object itself,
475 * maybe we should wake them up too, just in case? */
476 wake_up( &sock->obj, 0 );
478 sock_reselect( sock );
481 static void sock_dump( struct object *obj, int verbose )
483 struct sock *sock = (struct sock *)obj;
484 assert( obj->ops == &sock_ops );
485 fprintf( stderr, "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
486 sock->fd, sock->state,
487 sock->mask, sock->pmask, sock->hmask );
490 static int sock_signaled( struct object *obj, struct wait_queue_entry *entry )
492 struct sock *sock = (struct sock *)obj;
493 assert( obj->ops == &sock_ops );
495 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
498 static int sock_get_poll_events( struct fd *fd )
500 struct sock *sock = get_fd_user( fd );
501 unsigned int mask = sock->mask & ~sock->hmask;
502 unsigned int smask = sock->state & mask;
503 int ev = 0;
505 assert( sock->obj.ops == &sock_ops );
507 if (sock->state & FD_CONNECT)
508 /* connecting, wait for writable */
509 return POLLOUT;
511 if ( async_queued( sock->read_q ) )
513 if ( async_waiting( sock->read_q ) ) ev |= POLLIN | POLLPRI;
515 else if (smask & FD_READ || (sock->state & FD_WINE_LISTENING && mask & FD_ACCEPT))
516 ev |= POLLIN | POLLPRI;
517 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
518 else if ( sock->type == SOCK_STREAM && sock->state & FD_READ && mask & FD_CLOSE &&
519 !(sock->hmask & FD_READ) )
520 ev |= POLLIN;
522 if ( async_queued( sock->write_q ) )
524 if ( async_waiting( sock->write_q ) ) ev |= POLLOUT;
526 else if (smask & FD_WRITE)
527 ev |= POLLOUT;
529 return ev;
532 static enum server_fd_type sock_get_fd_type( struct fd *fd )
534 return FD_TYPE_SOCKET;
537 obj_handle_t sock_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data, int blocking )
539 struct sock *sock = get_fd_user( fd );
540 obj_handle_t wait_handle = 0;
541 struct async_queue *ifchange_q;
542 struct async *async;
544 assert( sock->obj.ops == &sock_ops );
546 switch(code)
548 case WS_SIO_ADDRESS_LIST_CHANGE:
549 if ((sock->state & FD_WINE_NONBLOCKING) && blocking)
551 set_error( STATUS_CANT_WAIT );
552 return 0;
554 if (!(ifchange_q = sock_get_ifchange_q( sock ))) return 0;
555 if (!(async = create_async( current, ifchange_q, async_data, NULL ))) return 0;
556 if (blocking) wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 );
557 release_object( async );
558 set_error( STATUS_PENDING );
559 return wait_handle;
560 default:
561 set_error( STATUS_NOT_SUPPORTED );
562 return 0;
566 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
568 struct sock *sock = get_fd_user( fd );
569 struct async *async;
570 struct async_queue *queue;
572 assert( sock->obj.ops == &sock_ops );
574 switch (type)
576 case ASYNC_TYPE_READ:
577 if (!sock->read_q && !(sock->read_q = create_async_queue( sock->fd ))) return;
578 queue = sock->read_q;
579 break;
580 case ASYNC_TYPE_WRITE:
581 if (!sock->write_q && !(sock->write_q = create_async_queue( sock->fd ))) return;
582 queue = sock->write_q;
583 break;
584 default:
585 set_error( STATUS_INVALID_PARAMETER );
586 return;
589 if ( ( !( sock->state & (FD_READ|FD_CONNECT|FD_WINE_LISTENING) ) && type == ASYNC_TYPE_READ ) ||
590 ( !( sock->state & (FD_WRITE|FD_CONNECT) ) && type == ASYNC_TYPE_WRITE ) )
592 set_error( STATUS_PIPE_DISCONNECTED );
593 return;
596 if (!(async = create_async( current, queue, data, NULL ))) return;
597 release_object( async );
599 sock_reselect( sock );
601 set_error( STATUS_PENDING );
604 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
606 struct sock *sock = get_fd_user( fd );
607 /* ignore reselect on ifchange queue */
608 if (sock->ifchange_q != queue)
609 sock_reselect( sock );
612 static struct fd *sock_get_fd( struct object *obj )
614 struct sock *sock = (struct sock *)obj;
615 return (struct fd *)grab_object( sock->fd );
618 static void sock_destroy( struct object *obj )
620 struct sock *sock = (struct sock *)obj;
621 assert( obj->ops == &sock_ops );
623 /* FIXME: special socket shutdown stuff? */
625 if ( sock->deferred )
626 release_object( sock->deferred );
628 free_async_queue( sock->read_q );
629 free_async_queue( sock->write_q );
630 async_wake_up( sock->ifchange_q, STATUS_CANCELLED );
631 sock_destroy_ifchange_q( sock );
632 if (sock->event) release_object( sock->event );
633 if (sock->fd)
635 /* shut the socket down to force pending poll() calls in the client to return */
636 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
637 release_object( sock->fd );
641 static void init_sock(struct sock *sock)
643 sock->state = 0;
644 sock->mask = 0;
645 sock->hmask = 0;
646 sock->pmask = 0;
647 sock->polling = 0;
648 sock->flags = 0;
649 sock->type = 0;
650 sock->family = 0;
651 sock->event = NULL;
652 sock->window = 0;
653 sock->message = 0;
654 sock->wparam = 0;
655 sock->connect_time = 0;
656 sock->deferred = NULL;
657 sock->read_q = NULL;
658 sock->write_q = NULL;
659 sock->ifchange_q = NULL;
660 sock->ifchange_obj = NULL;
661 memset( sock->errors, 0, sizeof(sock->errors) );
664 /* create a new and unconnected socket */
665 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
667 struct sock *sock;
668 int sockfd;
670 sockfd = socket( family, type, protocol );
671 if (debug_level)
672 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
673 if (sockfd == -1)
675 sock_set_error();
676 return NULL;
678 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
679 if (!(sock = alloc_object( &sock_ops )))
681 close( sockfd );
682 return NULL;
684 init_sock( sock );
685 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
686 sock->flags = flags;
687 sock->proto = protocol;
688 sock->type = type;
689 sock->family = family;
691 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
692 (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
694 release_object( sock );
695 return NULL;
697 sock_reselect( sock );
698 clear_error();
699 return &sock->obj;
702 /* accepts a socket and inits it */
703 static int accept_new_fd( struct sock *sock )
706 /* Try to accept(2). We can't be safe that this an already connected socket
707 * or that accept() is allowed on it. In those cases we will get -1/errno
708 * return.
710 int acceptfd;
711 struct sockaddr saddr;
712 socklen_t slen = sizeof(saddr);
713 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
714 if (acceptfd == -1)
716 sock_set_error();
717 return acceptfd;
720 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
721 return acceptfd;
724 /* accept a socket (creates a new fd) */
725 static struct sock *accept_socket( obj_handle_t handle )
727 struct sock *acceptsock;
728 struct sock *sock;
729 int acceptfd;
731 sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
732 if (!sock)
733 return NULL;
735 if ( sock->deferred )
737 acceptsock = sock->deferred;
738 sock->deferred = NULL;
740 else
742 if ((acceptfd = accept_new_fd( sock )) == -1)
744 release_object( sock );
745 return NULL;
747 if (!(acceptsock = alloc_object( &sock_ops )))
749 close( acceptfd );
750 release_object( sock );
751 return NULL;
754 init_sock( acceptsock );
755 /* newly created socket gets the same properties of the listening socket */
756 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
757 if (sock->state & FD_WINE_NONBLOCKING)
758 acceptsock->state |= FD_WINE_NONBLOCKING;
759 acceptsock->mask = sock->mask;
760 acceptsock->proto = sock->proto;
761 acceptsock->type = sock->type;
762 acceptsock->family = sock->family;
763 acceptsock->window = sock->window;
764 acceptsock->message = sock->message;
765 acceptsock->connect_time = current_time;
766 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
767 acceptsock->flags = sock->flags;
768 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
769 get_fd_options( sock->fd ) )))
771 release_object( acceptsock );
772 release_object( sock );
773 return NULL;
776 clear_error();
777 sock->pmask &= ~FD_ACCEPT;
778 sock->hmask &= ~FD_ACCEPT;
779 sock_reselect( sock );
780 release_object( sock );
781 return acceptsock;
784 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
786 int acceptfd;
787 struct fd *newfd;
788 if ( sock->deferred )
790 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
791 get_fd_options( acceptsock->fd ) );
792 if ( !newfd )
793 return FALSE;
795 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
797 release_object( sock->deferred );
798 sock->deferred = NULL;
800 else
802 if ((acceptfd = accept_new_fd( sock )) == -1)
803 return FALSE;
805 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
806 get_fd_options( acceptsock->fd ) )))
807 return FALSE;
810 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
811 acceptsock->hmask = 0;
812 acceptsock->pmask = 0;
813 acceptsock->polling = 0;
814 acceptsock->proto = sock->proto;
815 acceptsock->type = sock->type;
816 acceptsock->family = sock->family;
817 acceptsock->wparam = 0;
818 acceptsock->deferred = NULL;
819 acceptsock->connect_time = current_time;
820 fd_copy_completion( acceptsock->fd, newfd );
821 release_object( acceptsock->fd );
822 acceptsock->fd = newfd;
824 clear_error();
825 sock->pmask &= ~FD_ACCEPT;
826 sock->hmask &= ~FD_ACCEPT;
827 sock_reselect( sock );
829 return TRUE;
832 /* return an errno value mapped to a WSA error */
833 static int sock_get_error( int err )
835 switch (err)
837 case EINTR: return WSAEINTR;
838 case EBADF: return WSAEBADF;
839 case EPERM:
840 case EACCES: return WSAEACCES;
841 case EFAULT: return WSAEFAULT;
842 case EINVAL: return WSAEINVAL;
843 case EMFILE: return WSAEMFILE;
844 case EWOULDBLOCK: return WSAEWOULDBLOCK;
845 case EINPROGRESS: return WSAEINPROGRESS;
846 case EALREADY: return WSAEALREADY;
847 case ENOTSOCK: return WSAENOTSOCK;
848 case EDESTADDRREQ: return WSAEDESTADDRREQ;
849 case EMSGSIZE: return WSAEMSGSIZE;
850 case EPROTOTYPE: return WSAEPROTOTYPE;
851 case ENOPROTOOPT: return WSAENOPROTOOPT;
852 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
853 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
854 case EOPNOTSUPP: return WSAEOPNOTSUPP;
855 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
856 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
857 case EADDRINUSE: return WSAEADDRINUSE;
858 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
859 case ENETDOWN: return WSAENETDOWN;
860 case ENETUNREACH: return WSAENETUNREACH;
861 case ENETRESET: return WSAENETRESET;
862 case ECONNABORTED: return WSAECONNABORTED;
863 case EPIPE:
864 case ECONNRESET: return WSAECONNRESET;
865 case ENOBUFS: return WSAENOBUFS;
866 case EISCONN: return WSAEISCONN;
867 case ENOTCONN: return WSAENOTCONN;
868 case ESHUTDOWN: return WSAESHUTDOWN;
869 case ETOOMANYREFS: return WSAETOOMANYREFS;
870 case ETIMEDOUT: return WSAETIMEDOUT;
871 case ECONNREFUSED: return WSAECONNREFUSED;
872 case ELOOP: return WSAELOOP;
873 case ENAMETOOLONG: return WSAENAMETOOLONG;
874 case EHOSTDOWN: return WSAEHOSTDOWN;
875 case EHOSTUNREACH: return WSAEHOSTUNREACH;
876 case ENOTEMPTY: return WSAENOTEMPTY;
877 #ifdef EPROCLIM
878 case EPROCLIM: return WSAEPROCLIM;
879 #endif
880 #ifdef EUSERS
881 case EUSERS: return WSAEUSERS;
882 #endif
883 #ifdef EDQUOT
884 case EDQUOT: return WSAEDQUOT;
885 #endif
886 #ifdef ESTALE
887 case ESTALE: return WSAESTALE;
888 #endif
889 #ifdef EREMOTE
890 case EREMOTE: return WSAEREMOTE;
891 #endif
893 case 0: return 0;
894 default:
895 errno = err;
896 perror("wineserver: sock_get_error() can't map error");
897 return WSAEFAULT;
901 static int sock_get_ntstatus( int err )
903 switch ( err )
905 case EBADF: return STATUS_INVALID_HANDLE;
906 case EBUSY: return STATUS_DEVICE_BUSY;
907 case EPERM:
908 case EACCES: return STATUS_ACCESS_DENIED;
909 case EFAULT: return STATUS_NO_MEMORY;
910 case EINVAL: return STATUS_INVALID_PARAMETER;
911 case ENFILE:
912 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
913 case EWOULDBLOCK: return STATUS_CANT_WAIT;
914 case EINPROGRESS: return STATUS_PENDING;
915 case EALREADY: return STATUS_NETWORK_BUSY;
916 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
917 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
918 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
919 case EPROTONOSUPPORT:
920 case ESOCKTNOSUPPORT:
921 case EPFNOSUPPORT:
922 case EAFNOSUPPORT:
923 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
924 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
925 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
926 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
927 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
928 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
929 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
930 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
931 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
932 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
933 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
934 case ENETDOWN: return STATUS_NETWORK_BUSY;
935 case EPIPE:
936 case ECONNRESET: return STATUS_CONNECTION_RESET;
937 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
939 case 0: return STATUS_SUCCESS;
940 default:
941 errno = err;
942 perror("wineserver: sock_get_ntstatus() can't map error");
943 return STATUS_UNSUCCESSFUL;
947 /* set the last error depending on errno */
948 static void sock_set_error(void)
950 set_error( sock_get_ntstatus( errno ) );
953 #ifdef HAVE_LINUX_RTNETLINK_H
955 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
956 static struct object *ifchange_object;
958 static void ifchange_dump( struct object *obj, int verbose );
959 static struct fd *ifchange_get_fd( struct object *obj );
960 static void ifchange_destroy( struct object *obj );
962 static int ifchange_get_poll_events( struct fd *fd );
963 static void ifchange_poll_event( struct fd *fd, int event );
965 struct ifchange
967 struct object obj; /* object header */
968 struct fd *fd; /* interface change file descriptor */
969 struct list sockets; /* list of sockets to send interface change notifications */
972 static const struct object_ops ifchange_ops =
974 sizeof(struct ifchange), /* size */
975 ifchange_dump, /* dump */
976 no_get_type, /* get_type */
977 add_queue, /* add_queue */
978 NULL, /* remove_queue */
979 NULL, /* signaled */
980 no_satisfied, /* satisfied */
981 no_signal, /* signal */
982 ifchange_get_fd, /* get_fd */
983 default_fd_map_access, /* map_access */
984 default_get_sd, /* get_sd */
985 default_set_sd, /* set_sd */
986 no_lookup_name, /* lookup_name */
987 no_link_name, /* link_name */
988 NULL, /* unlink_name */
989 no_open_file, /* open_file */
990 no_close_handle, /* close_handle */
991 ifchange_destroy /* destroy */
994 static const struct fd_ops ifchange_fd_ops =
996 ifchange_get_poll_events, /* get_poll_events */
997 ifchange_poll_event, /* poll_event */
998 NULL, /* get_fd_type */
999 no_fd_read, /* read */
1000 no_fd_write, /* write */
1001 no_fd_flush, /* flush */
1002 no_fd_ioctl, /* ioctl */
1003 NULL, /* queue_async */
1004 NULL /* reselect_async */
1007 static void ifchange_dump( struct object *obj, int verbose )
1009 assert( obj->ops == &ifchange_ops );
1010 fprintf( stderr, "Interface change\n" );
1013 static struct fd *ifchange_get_fd( struct object *obj )
1015 struct ifchange *ifchange = (struct ifchange *)obj;
1016 return (struct fd *)grab_object( ifchange->fd );
1019 static void ifchange_destroy( struct object *obj )
1021 struct ifchange *ifchange = (struct ifchange *)obj;
1022 assert( obj->ops == &ifchange_ops );
1024 release_object( ifchange->fd );
1026 /* reset the global ifchange object so that it will be recreated if it is needed again */
1027 assert( obj == ifchange_object );
1028 ifchange_object = NULL;
1031 static int ifchange_get_poll_events( struct fd *fd )
1033 return POLLIN;
1036 /* wake up all the sockets waiting for a change notification event */
1037 static void ifchange_wake_up( struct object *obj, unsigned int status )
1039 struct ifchange *ifchange = (struct ifchange *)obj;
1040 struct list *ptr, *next;
1041 assert( obj->ops == &ifchange_ops );
1042 assert( obj == ifchange_object );
1044 LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
1046 struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
1048 assert( sock->ifchange_q );
1049 async_wake_up( sock->ifchange_q, status ); /* issue ifchange notification for the socket */
1050 sock_destroy_ifchange_q( sock ); /* remove socket from list and decrement ifchange refcount */
1054 static void ifchange_poll_event( struct fd *fd, int event )
1056 struct object *ifchange = get_fd_user( fd );
1057 unsigned int status = STATUS_PENDING;
1058 char buffer[PIPE_BUF];
1059 int r;
1061 r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
1062 if (r < 0)
1064 if (errno == EWOULDBLOCK || (EWOULDBLOCK != EAGAIN && errno == EAGAIN))
1065 return; /* retry when poll() says the socket is ready */
1066 status = sock_get_ntstatus( errno );
1068 else if (r > 0)
1070 struct nlmsghdr *nlh;
1072 for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
1074 if (nlh->nlmsg_type == NLMSG_DONE)
1075 break;
1076 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
1077 status = STATUS_SUCCESS;
1080 else status = STATUS_CANCELLED;
1082 if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
1085 #endif
1087 /* we only need one of these interface notification objects, all of the sockets dependent upon
1088 * it will wake up when a notification event occurs */
1089 static struct object *get_ifchange( void )
1091 #ifdef HAVE_LINUX_RTNETLINK_H
1092 struct ifchange *ifchange;
1093 struct sockaddr_nl addr;
1094 int unix_fd;
1096 if (ifchange_object)
1098 /* increment the refcount for each socket that uses the ifchange object */
1099 return grab_object( ifchange_object );
1102 /* create the socket we need for processing interface change notifications */
1103 unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
1104 if (unix_fd == -1)
1106 sock_set_error();
1107 return NULL;
1109 fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
1110 memset( &addr, 0, sizeof(addr) );
1111 addr.nl_family = AF_NETLINK;
1112 addr.nl_groups = RTMGRP_IPV4_IFADDR;
1113 /* bind the socket to the special netlink kernel interface */
1114 if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
1116 close( unix_fd );
1117 sock_set_error();
1118 return NULL;
1120 if (!(ifchange = alloc_object( &ifchange_ops )))
1122 close( unix_fd );
1123 set_error( STATUS_NO_MEMORY );
1124 return NULL;
1126 list_init( &ifchange->sockets );
1127 if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
1129 release_object( ifchange );
1130 set_error( STATUS_NO_MEMORY );
1131 return NULL;
1133 set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
1135 /* the ifchange object is now successfully configured */
1136 ifchange_object = &ifchange->obj;
1137 return &ifchange->obj;
1138 #else
1139 set_error( STATUS_NOT_SUPPORTED );
1140 return NULL;
1141 #endif
1144 /* add the socket to the interface change notification list */
1145 static void ifchange_add_sock( struct object *obj, struct sock *sock )
1147 #ifdef HAVE_LINUX_RTNETLINK_H
1148 struct ifchange *ifchange = (struct ifchange *)obj;
1150 list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
1151 #endif
1154 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1155 static struct async_queue *sock_get_ifchange_q( struct sock *sock )
1157 struct object *ifchange;
1159 if (sock->ifchange_q) /* reuse existing ifchange_q for this socket */
1160 return sock->ifchange_q;
1162 if (!(ifchange = get_ifchange()))
1163 return NULL;
1165 /* create the ifchange notification queue */
1166 sock->ifchange_q = create_async_queue( sock->fd );
1167 if (!sock->ifchange_q)
1169 release_object( ifchange );
1170 set_error( STATUS_NO_MEMORY );
1171 return NULL;
1174 /* add the socket to the ifchange notification list */
1175 ifchange_add_sock( ifchange, sock );
1176 sock->ifchange_obj = ifchange;
1177 return sock->ifchange_q;
1180 /* destroy an existing ifchange queue for a specific socket */
1181 static void sock_destroy_ifchange_q( struct sock *sock )
1183 if (sock->ifchange_q)
1185 list_remove( &sock->ifchange_entry );
1186 free_async_queue( sock->ifchange_q );
1187 sock->ifchange_q = NULL;
1188 release_object( sock->ifchange_obj );
1192 /* create a socket */
1193 DECL_HANDLER(create_socket)
1195 struct object *obj;
1197 reply->handle = 0;
1198 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
1200 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1201 release_object( obj );
1205 /* accept a socket */
1206 DECL_HANDLER(accept_socket)
1208 struct sock *sock;
1210 reply->handle = 0;
1211 if ((sock = accept_socket( req->lhandle )) != NULL)
1213 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
1214 sock->wparam = reply->handle; /* wparam for message is the socket handle */
1215 sock_reselect( sock );
1216 release_object( &sock->obj );
1220 /* accept a socket into an initialized socket */
1221 DECL_HANDLER(accept_into_socket)
1223 struct sock *sock, *acceptsock;
1224 const int all_attributes = FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|FILE_READ_DATA;
1226 if (!(sock = (struct sock *)get_handle_obj( current->process, req->lhandle,
1227 all_attributes, &sock_ops)))
1228 return;
1230 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, req->ahandle,
1231 all_attributes, &sock_ops)))
1233 release_object( sock );
1234 return;
1237 if (accept_into_socket( sock, acceptsock ))
1239 acceptsock->wparam = req->ahandle; /* wparam for message is the socket handle */
1240 sock_reselect( acceptsock );
1242 release_object( acceptsock );
1243 release_object( sock );
1246 /* set socket event parameters */
1247 DECL_HANDLER(set_socket_event)
1249 struct sock *sock;
1250 struct event *old_event;
1252 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1253 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
1254 old_event = sock->event;
1255 sock->mask = req->mask;
1256 sock->hmask &= ~req->mask; /* re-enable held events */
1257 sock->event = NULL;
1258 sock->window = req->window;
1259 sock->message = req->msg;
1260 sock->wparam = req->handle; /* wparam is the socket handle */
1261 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
1263 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
1265 sock_reselect( sock );
1267 sock->state |= FD_WINE_NONBLOCKING;
1269 /* if a network event is pending, signal the event object
1270 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1271 before a WSAEventSelect() was done on it.
1272 (when dealing with Asynchronous socket) */
1273 sock_wake_up( sock );
1275 if (old_event) release_object( old_event ); /* we're through with it */
1276 release_object( &sock->obj );
1279 /* get socket event parameters */
1280 DECL_HANDLER(get_socket_event)
1282 struct sock *sock;
1283 int i;
1284 int errors[FD_MAX_EVENTS];
1286 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1287 if (!sock)
1289 reply->mask = 0;
1290 reply->pmask = 0;
1291 reply->state = 0;
1292 return;
1294 reply->mask = sock->mask;
1295 reply->pmask = sock->pmask;
1296 reply->state = sock->state;
1297 for (i = 0; i < FD_MAX_EVENTS; i++)
1298 errors[i] = sock_get_ntstatus(sock->errors[i]);
1300 set_reply_data( errors, min( get_reply_max_size(), sizeof(errors) ));
1302 if (req->service)
1304 if (req->c_event)
1306 struct event *cevent = get_event_obj( current->process, req->c_event,
1307 EVENT_MODIFY_STATE );
1308 if (cevent)
1310 reset_event( cevent );
1311 release_object( cevent );
1314 sock->pmask = 0;
1315 sock_reselect( sock );
1317 release_object( &sock->obj );
1320 /* re-enable pending socket events */
1321 DECL_HANDLER(enable_socket_event)
1323 struct sock *sock;
1325 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
1326 FILE_WRITE_ATTRIBUTES, &sock_ops)))
1327 return;
1329 /* for event-based notification, windows erases stale events */
1330 sock->pmask &= ~req->mask;
1332 sock->hmask &= ~req->mask;
1333 sock->state |= req->sstate;
1334 sock->state &= ~req->cstate;
1335 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
1337 sock_reselect( sock );
1339 release_object( &sock->obj );
1342 DECL_HANDLER(set_socket_deferred)
1344 struct sock *sock, *acceptsock;
1346 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
1347 if ( !sock )
1348 return;
1350 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
1351 if ( !acceptsock )
1353 release_object( sock );
1354 return;
1356 sock->deferred = acceptsock;
1357 release_object( sock );
1360 DECL_HANDLER(get_socket_info)
1362 struct sock *sock;
1364 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1365 if (!sock) return;
1367 reply->family = sock->family;
1368 reply->type = sock->type;
1369 reply->protocol = sock->proto;
1371 release_object( &sock->obj );