mmsystem.drv: Always create a thunk for MMIO objects.
[wine/multimedia.git] / server / sock.c
blob9cd0fc2aaacb6b82faf8df0217524627d9401680
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_SYS_ERRNO_H
34 # include <sys/errno.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>
50 #include "ntstatus.h"
51 #define WIN32_NO_STATUS
52 #include "windef.h"
53 #include "winternl.h"
54 #include "winerror.h"
56 #include "process.h"
57 #include "file.h"
58 #include "handle.h"
59 #include "thread.h"
60 #include "request.h"
61 #include "user.h"
63 /* From winsock.h */
64 #define FD_MAX_EVENTS 10
65 #define FD_READ_BIT 0
66 #define FD_WRITE_BIT 1
67 #define FD_OOB_BIT 2
68 #define FD_ACCEPT_BIT 3
69 #define FD_CONNECT_BIT 4
70 #define FD_CLOSE_BIT 5
73 * Define flags to be used with the WSAAsyncSelect() call.
75 #define FD_READ 0x00000001
76 #define FD_WRITE 0x00000002
77 #define FD_OOB 0x00000004
78 #define FD_ACCEPT 0x00000008
79 #define FD_CONNECT 0x00000010
80 #define FD_CLOSE 0x00000020
82 /* internal per-socket flags */
83 #define FD_WINE_LISTENING 0x10000000
84 #define FD_WINE_NONBLOCKING 0x20000000
85 #define FD_WINE_CONNECTED 0x40000000
86 #define FD_WINE_RAW 0x80000000
87 #define FD_WINE_INTERNAL 0xFFFF0000
89 /* Constants for WSAIoctl() */
90 #define WSA_FLAG_OVERLAPPED 0x01
92 struct sock
94 struct object obj; /* object header */
95 struct fd *fd; /* socket file descriptor */
96 unsigned int state; /* status bits */
97 unsigned int mask; /* event mask */
98 unsigned int hmask; /* held (blocked) events */
99 unsigned int pmask; /* pending events */
100 unsigned int flags; /* socket flags */
101 int polling; /* is socket being polled? */
102 unsigned short type; /* socket type */
103 unsigned short family; /* socket family */
104 struct event *event; /* event object */
105 user_handle_t window; /* window to send the message to */
106 unsigned int message; /* message to send */
107 obj_handle_t wparam; /* message wparam (socket handle) */
108 int errors[FD_MAX_EVENTS]; /* event errors */
109 struct sock *deferred; /* socket that waits for a deferred accept */
110 struct async_queue *read_q; /* queue for asynchronous reads */
111 struct async_queue *write_q; /* queue for asynchronous writes */
114 static void sock_dump( struct object *obj, int verbose );
115 static int sock_signaled( struct object *obj, struct thread *thread );
116 static struct fd *sock_get_fd( struct object *obj );
117 static void sock_destroy( struct object *obj );
119 static int sock_get_poll_events( struct fd *fd );
120 static void sock_poll_event( struct fd *fd, int event );
121 static enum server_fd_type sock_get_fd_type( struct fd *fd );
122 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
123 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
124 static void sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb );
126 static int sock_get_ntstatus( int err );
127 static int sock_get_error( int err );
128 static void sock_set_error(void);
130 static const struct object_ops sock_ops =
132 sizeof(struct sock), /* size */
133 sock_dump, /* dump */
134 no_get_type, /* get_type */
135 add_queue, /* add_queue */
136 remove_queue, /* remove_queue */
137 sock_signaled, /* signaled */
138 no_satisfied, /* satisfied */
139 no_signal, /* signal */
140 sock_get_fd, /* get_fd */
141 default_fd_map_access, /* map_access */
142 default_get_sd, /* get_sd */
143 default_set_sd, /* set_sd */
144 no_lookup_name, /* lookup_name */
145 no_open_file, /* open_file */
146 fd_close_handle, /* close_handle */
147 sock_destroy /* destroy */
150 static const struct fd_ops sock_fd_ops =
152 sock_get_poll_events, /* get_poll_events */
153 sock_poll_event, /* poll_event */
154 no_flush, /* flush */
155 sock_get_fd_type, /* get_file_info */
156 default_fd_ioctl, /* ioctl */
157 sock_queue_async, /* queue_async */
158 sock_reselect_async, /* reselect_async */
159 sock_cancel_async /* cancel_async */
163 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
164 * we post messages if there are multiple events. Used to send
165 * messages. The problem is if there is both a FD_CONNECT event and,
166 * say, an FD_READ event available on the same socket, we want to
167 * notify the app of the connect event first. Otherwise it may
168 * discard the read event because it thinks it hasn't connected yet.
170 static const int event_bitorder[FD_MAX_EVENTS] =
172 FD_CONNECT_BIT,
173 FD_ACCEPT_BIT,
174 FD_OOB_BIT,
175 FD_WRITE_BIT,
176 FD_READ_BIT,
177 FD_CLOSE_BIT,
178 6, 7, 8, 9 /* leftovers */
181 /* Flags that make sense only for SOCK_STREAM sockets */
182 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
184 typedef enum {
185 SOCK_SHUTDOWN_ERROR = -1,
186 SOCK_SHUTDOWN_EOF = 0,
187 SOCK_SHUTDOWN_POLLHUP = 1
188 } sock_shutdown_t;
190 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
192 static sock_shutdown_t sock_check_pollhup(void)
194 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
195 int fd[2], n;
196 struct pollfd pfd;
197 char dummy;
199 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) goto out;
200 if ( shutdown( fd[0], 1 ) ) goto out;
202 pfd.fd = fd[1];
203 pfd.events = POLLIN;
204 pfd.revents = 0;
206 n = poll( &pfd, 1, 0 );
207 if ( n != 1 ) goto out; /* error or timeout */
208 if ( pfd.revents & POLLHUP )
209 ret = SOCK_SHUTDOWN_POLLHUP;
210 else if ( pfd.revents & POLLIN &&
211 read( fd[1], &dummy, 1 ) == 0 )
212 ret = SOCK_SHUTDOWN_EOF;
214 out:
215 close( fd[0] );
216 close( fd[1] );
217 return ret;
220 void sock_init(void)
222 sock_shutdown_type = sock_check_pollhup();
224 switch ( sock_shutdown_type )
226 case SOCK_SHUTDOWN_EOF:
227 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
228 break;
229 case SOCK_SHUTDOWN_POLLHUP:
230 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
231 break;
232 default:
233 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
234 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
238 static int sock_reselect( struct sock *sock )
240 int ev = sock_get_poll_events( sock->fd );
242 if (debug_level)
243 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
245 if (!sock->polling) /* FIXME: should find a better way to do this */
247 /* previously unconnected socket, is this reselect supposed to connect it? */
248 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
249 /* ok, it is, attach it to the wineserver's main poll loop */
250 sock->polling = 1;
252 /* update condition mask */
253 set_fd_events( sock->fd, ev );
254 return ev;
257 /* wake anybody waiting on the socket event or send the associated message */
258 static void sock_wake_up( struct sock *sock )
260 unsigned int events = sock->pmask & sock->mask;
261 int i;
263 /* Do not signal events if there are still pending asynchronous IO requests */
264 /* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
265 if ( !events || async_queued( sock->read_q ) || async_queued( sock->write_q ) ) return;
267 if (sock->event)
269 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
270 set_event( sock->event );
272 if (sock->window)
274 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
275 for (i = 0; i < FD_MAX_EVENTS; i++)
277 int event = event_bitorder[i];
278 if (sock->pmask & (1 << event))
280 lparam_t lparam = (1 << event) | (sock_get_error(sock->errors[event]) << 16);
281 post_message( sock->window, sock->message, sock->wparam, lparam );
284 sock->pmask = 0;
285 sock_reselect( sock );
289 static inline int sock_error( struct fd *fd )
291 unsigned int optval = 0, optlen;
293 optlen = sizeof(optval);
294 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
295 return optval;
298 static void sock_dispatch_asyncs( struct sock *sock, int event )
300 if ( sock->flags & WSA_FLAG_OVERLAPPED )
302 if ( event & (POLLIN|POLLPRI|POLLERR|POLLHUP) && async_waiting( sock->read_q ))
304 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
305 async_wake_up( sock->read_q, STATUS_ALERTED );
307 if ( event & (POLLOUT|POLLERR|POLLHUP) && async_waiting( sock->write_q ))
309 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
310 async_wake_up( sock->write_q, STATUS_ALERTED );
315 static void sock_poll_event( struct fd *fd, int event )
317 struct sock *sock = get_fd_user( fd );
318 int hangup_seen = 0;
320 assert( sock->obj.ops == &sock_ops );
321 if (debug_level)
322 fprintf(stderr, "socket %p select event: %x\n", sock, event);
324 /* we may change event later, remove from loop here */
325 if (event & (POLLERR|POLLHUP)) set_fd_events( sock->fd, -1 );
327 if (sock->state & FD_CONNECT)
329 /* connecting */
330 if (event & POLLOUT)
332 /* we got connected */
333 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
334 sock->state &= ~FD_CONNECT;
335 sock->pmask |= FD_CONNECT;
336 sock->errors[FD_CONNECT_BIT] = 0;
337 if (debug_level)
338 fprintf(stderr, "socket %p connection success\n", sock);
340 else if (event & (POLLERR|POLLHUP))
342 /* we didn't get connected? */
343 sock->state &= ~FD_CONNECT;
344 sock->pmask |= FD_CONNECT;
345 sock->errors[FD_CONNECT_BIT] = sock_error( fd );
346 if (debug_level)
347 fprintf(stderr, "socket %p connection failure\n", sock);
350 else if (sock->state & FD_WINE_LISTENING)
352 /* listening */
353 if (event & POLLIN)
355 /* incoming connection */
356 sock->pmask |= FD_ACCEPT;
357 sock->errors[FD_ACCEPT_BIT] = 0;
358 sock->hmask |= FD_ACCEPT;
360 else if (event & (POLLERR|POLLHUP))
362 /* failed incoming connection? */
363 sock->pmask |= FD_ACCEPT;
364 sock->errors[FD_ACCEPT_BIT] = sock_error( fd );
365 sock->hmask |= FD_ACCEPT;
368 else
370 /* normal data flow */
371 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
373 char dummy;
374 int nr;
376 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
377 * has been closed, so we need to check for it explicitly here */
378 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
379 if ( nr == 0 )
381 hangup_seen = 1;
382 event &= ~POLLIN;
384 else if ( nr < 0 )
386 event &= ~POLLIN;
387 /* EAGAIN can happen if an async recv() falls between the server's poll()
388 call and the invocation of this routine */
389 if ( errno != EAGAIN )
391 if ( debug_level )
392 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
393 event |= POLLERR;
398 if ( event & POLLIN )
400 sock->pmask |= FD_READ;
401 sock->hmask |= FD_READ;
402 sock->errors[FD_READ_BIT] = 0;
403 if (debug_level)
404 fprintf(stderr, "socket %p is readable\n", sock );
407 if (event & POLLOUT)
409 sock->pmask |= FD_WRITE;
410 sock->hmask |= FD_WRITE;
411 sock->errors[FD_WRITE_BIT] = 0;
412 if (debug_level)
413 fprintf(stderr, "socket %p is writable\n", sock);
415 if (event & POLLPRI)
417 sock->pmask |= FD_OOB;
418 sock->hmask |= FD_OOB;
419 sock->errors[FD_OOB_BIT] = 0;
420 if (debug_level)
421 fprintf(stderr, "socket %p got OOB data\n", sock);
424 if ( (hangup_seen || event & (POLLHUP|POLLERR)) && (sock->state & (FD_READ|FD_WRITE)) )
426 sock->errors[FD_CLOSE_BIT] = sock_error( fd );
427 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
428 sock->state &= ~FD_WRITE;
429 sock->state &= ~FD_READ;
431 sock->pmask |= FD_CLOSE;
432 sock->hmask |= FD_CLOSE;
433 if (debug_level)
434 fprintf(stderr, "socket %p aborted by error %d, event: %x - removing from select loop\n",
435 sock, sock->errors[FD_CLOSE_BIT], event);
438 if (hangup_seen)
439 event |= POLLHUP;
442 sock_dispatch_asyncs( sock, event );
444 /* wake up anyone waiting for whatever just happened */
445 sock_wake_up( sock );
447 /* if anyone is stupid enough to wait on the socket object itself,
448 * maybe we should wake them up too, just in case? */
449 wake_up( &sock->obj, 0 );
451 sock_reselect( sock );
454 static void sock_dump( struct object *obj, int verbose )
456 struct sock *sock = (struct sock *)obj;
457 assert( obj->ops == &sock_ops );
458 printf( "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
459 sock->fd, sock->state,
460 sock->mask, sock->pmask, sock->hmask );
463 static int sock_signaled( struct object *obj, struct thread *thread )
465 struct sock *sock = (struct sock *)obj;
466 assert( obj->ops == &sock_ops );
468 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
471 static int sock_get_poll_events( struct fd *fd )
473 struct sock *sock = get_fd_user( fd );
474 unsigned int mask = sock->mask & ~sock->hmask;
475 unsigned int smask = sock->state & mask;
476 int ev = 0;
478 assert( sock->obj.ops == &sock_ops );
480 if (sock->state & FD_CONNECT)
481 /* connecting, wait for writable */
482 return POLLOUT;
483 if (sock->state & FD_WINE_LISTENING)
484 /* listening, wait for readable */
485 return (mask & FD_ACCEPT) ? POLLIN : 0;
487 if ( async_queued( sock->read_q ) )
489 if ( async_waiting( sock->read_q ) ) ev |= POLLIN | POLLPRI;
491 else if (smask & FD_READ)
492 ev |= POLLIN | POLLPRI;
493 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
494 else if ( sock->type == SOCK_STREAM && sock->state & FD_READ && mask & FD_CLOSE &&
495 !(sock->hmask & FD_READ) )
496 ev |= POLLIN;
498 if ( async_queued( sock->write_q ) )
500 if ( async_waiting( sock->write_q ) ) ev |= POLLOUT;
502 else if (smask & FD_WRITE)
503 ev |= POLLOUT;
505 return ev;
508 static enum server_fd_type sock_get_fd_type( struct fd *fd )
510 return FD_TYPE_SOCKET;
513 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
515 struct sock *sock = get_fd_user( fd );
516 struct async_queue *queue;
518 assert( sock->obj.ops == &sock_ops );
520 switch (type)
522 case ASYNC_TYPE_READ:
523 if (!sock->read_q && !(sock->read_q = create_async_queue( sock->fd ))) return;
524 queue = sock->read_q;
525 break;
526 case ASYNC_TYPE_WRITE:
527 if (!sock->write_q && !(sock->write_q = create_async_queue( sock->fd ))) return;
528 queue = sock->write_q;
529 break;
530 default:
531 set_error( STATUS_INVALID_PARAMETER );
532 return;
535 if ( ( !( sock->state & FD_READ ) && type == ASYNC_TYPE_READ ) ||
536 ( !( sock->state & FD_WRITE ) && type == ASYNC_TYPE_WRITE ) )
538 set_error( STATUS_PIPE_DISCONNECTED );
540 else
542 struct async *async;
543 if (!(async = create_async( current, queue, data ))) return;
544 release_object( async );
545 set_error( STATUS_PENDING );
548 sock_reselect( sock );
551 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
553 struct sock *sock = get_fd_user( fd );
554 sock_reselect( sock );
557 static void sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb )
559 struct sock *sock = get_fd_user( fd );
560 int n = 0;
561 assert( sock->obj.ops == &sock_ops );
563 n += async_wake_up_by( sock->read_q, process, thread, iosb, STATUS_CANCELLED );
564 n += async_wake_up_by( sock->write_q, process, thread, iosb, STATUS_CANCELLED );
565 if (!n && iosb)
566 set_error( STATUS_NOT_FOUND );
569 static struct fd *sock_get_fd( struct object *obj )
571 struct sock *sock = (struct sock *)obj;
572 return (struct fd *)grab_object( sock->fd );
575 static void sock_destroy( struct object *obj )
577 struct sock *sock = (struct sock *)obj;
578 assert( obj->ops == &sock_ops );
580 /* FIXME: special socket shutdown stuff? */
582 if ( sock->deferred )
583 release_object( sock->deferred );
585 free_async_queue( sock->read_q );
586 free_async_queue( sock->write_q );
587 if (sock->event) release_object( sock->event );
588 if (sock->fd)
590 /* shut the socket down to force pending poll() calls in the client to return */
591 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
592 release_object( sock->fd );
596 /* create a new and unconnected socket */
597 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
599 struct sock *sock;
600 int sockfd;
602 sockfd = socket( family, type, protocol );
603 if (debug_level)
604 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
605 if (sockfd == -1)
607 sock_set_error();
608 return NULL;
610 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
611 if (!(sock = alloc_object( &sock_ops )))
613 close( sockfd );
614 return NULL;
616 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
617 sock->mask = 0;
618 sock->hmask = 0;
619 sock->pmask = 0;
620 sock->polling = 0;
621 sock->flags = flags;
622 sock->type = type;
623 sock->family = family;
624 sock->event = NULL;
625 sock->window = 0;
626 sock->message = 0;
627 sock->wparam = 0;
628 sock->deferred = NULL;
629 sock->read_q = NULL;
630 sock->write_q = NULL;
631 memset( sock->errors, 0, sizeof(sock->errors) );
632 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
633 (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
635 release_object( sock );
636 return NULL;
638 sock_reselect( sock );
639 clear_error();
640 return &sock->obj;
643 /* accept a socket (creates a new fd) */
644 static struct sock *accept_socket( obj_handle_t handle )
646 struct sock *acceptsock;
647 struct sock *sock;
648 int acceptfd;
649 struct sockaddr saddr;
651 sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
652 if (!sock)
653 return NULL;
655 if ( sock->deferred )
657 acceptsock = sock->deferred;
658 sock->deferred = NULL;
660 else
663 /* Try to accept(2). We can't be safe that this an already connected socket
664 * or that accept() is allowed on it. In those cases we will get -1/errno
665 * return.
667 unsigned int slen = sizeof(saddr);
668 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
669 if (acceptfd==-1)
671 sock_set_error();
672 release_object( sock );
673 return NULL;
675 if (!(acceptsock = alloc_object( &sock_ops )))
677 close( acceptfd );
678 release_object( sock );
679 return NULL;
682 /* newly created socket gets the same properties of the listening socket */
683 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
684 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
685 if (sock->state & FD_WINE_NONBLOCKING)
686 acceptsock->state |= FD_WINE_NONBLOCKING;
687 acceptsock->mask = sock->mask;
688 acceptsock->hmask = 0;
689 acceptsock->pmask = 0;
690 acceptsock->polling = 0;
691 acceptsock->type = sock->type;
692 acceptsock->family = sock->family;
693 acceptsock->event = NULL;
694 acceptsock->window = sock->window;
695 acceptsock->message = sock->message;
696 acceptsock->wparam = 0;
697 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
698 acceptsock->flags = sock->flags;
699 acceptsock->deferred = NULL;
700 acceptsock->read_q = NULL;
701 acceptsock->write_q = NULL;
702 memset( acceptsock->errors, 0, sizeof(acceptsock->errors) );
703 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
704 get_fd_options( sock->fd ) )))
706 release_object( acceptsock );
707 release_object( sock );
708 return NULL;
711 clear_error();
712 sock->pmask &= ~FD_ACCEPT;
713 sock->hmask &= ~FD_ACCEPT;
714 sock_reselect( sock );
715 release_object( sock );
716 return acceptsock;
719 /* set the last error depending on errno */
720 static int sock_get_error( int err )
722 switch (err)
724 case EINTR: return WSAEINTR;
725 case EBADF: return WSAEBADF;
726 case EPERM:
727 case EACCES: return WSAEACCES;
728 case EFAULT: return WSAEFAULT;
729 case EINVAL: return WSAEINVAL;
730 case EMFILE: return WSAEMFILE;
731 case EWOULDBLOCK: return WSAEWOULDBLOCK;
732 case EINPROGRESS: return WSAEINPROGRESS;
733 case EALREADY: return WSAEALREADY;
734 case ENOTSOCK: return WSAENOTSOCK;
735 case EDESTADDRREQ: return WSAEDESTADDRREQ;
736 case EMSGSIZE: return WSAEMSGSIZE;
737 case EPROTOTYPE: return WSAEPROTOTYPE;
738 case ENOPROTOOPT: return WSAENOPROTOOPT;
739 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
740 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
741 case EOPNOTSUPP: return WSAEOPNOTSUPP;
742 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
743 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
744 case EADDRINUSE: return WSAEADDRINUSE;
745 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
746 case ENETDOWN: return WSAENETDOWN;
747 case ENETUNREACH: return WSAENETUNREACH;
748 case ENETRESET: return WSAENETRESET;
749 case ECONNABORTED: return WSAECONNABORTED;
750 case EPIPE:
751 case ECONNRESET: return WSAECONNRESET;
752 case ENOBUFS: return WSAENOBUFS;
753 case EISCONN: return WSAEISCONN;
754 case ENOTCONN: return WSAENOTCONN;
755 case ESHUTDOWN: return WSAESHUTDOWN;
756 case ETOOMANYREFS: return WSAETOOMANYREFS;
757 case ETIMEDOUT: return WSAETIMEDOUT;
758 case ECONNREFUSED: return WSAECONNREFUSED;
759 case ELOOP: return WSAELOOP;
760 case ENAMETOOLONG: return WSAENAMETOOLONG;
761 case EHOSTDOWN: return WSAEHOSTDOWN;
762 case EHOSTUNREACH: return WSAEHOSTUNREACH;
763 case ENOTEMPTY: return WSAENOTEMPTY;
764 #ifdef EPROCLIM
765 case EPROCLIM: return WSAEPROCLIM;
766 #endif
767 #ifdef EUSERS
768 case EUSERS: return WSAEUSERS;
769 #endif
770 #ifdef EDQUOT
771 case EDQUOT: return WSAEDQUOT;
772 #endif
773 #ifdef ESTALE
774 case ESTALE: return WSAESTALE;
775 #endif
776 #ifdef EREMOTE
777 case EREMOTE: return WSAEREMOTE;
778 #endif
780 case 0: return 0;
781 default:
782 errno = err;
783 perror("wineserver: sock_get_error() can't map error");
784 return WSAEFAULT;
788 static int sock_get_ntstatus( int err )
790 switch ( err )
792 case EBADF: return STATUS_INVALID_HANDLE;
793 case EBUSY: return STATUS_DEVICE_BUSY;
794 case EPERM:
795 case EACCES: return STATUS_ACCESS_DENIED;
796 case EFAULT: return STATUS_NO_MEMORY;
797 case EINVAL: return STATUS_INVALID_PARAMETER;
798 case ENFILE:
799 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
800 case EWOULDBLOCK: return STATUS_CANT_WAIT;
801 case EINPROGRESS: return STATUS_PENDING;
802 case EALREADY: return STATUS_NETWORK_BUSY;
803 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
804 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
805 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
806 case EPROTONOSUPPORT:
807 case ESOCKTNOSUPPORT:
808 case EPFNOSUPPORT:
809 case EAFNOSUPPORT:
810 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
811 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
812 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
813 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
814 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
815 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
816 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
817 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
818 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
819 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
820 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
821 case ENETDOWN: return STATUS_NETWORK_BUSY;
822 case EPIPE:
823 case ECONNRESET: return STATUS_CONNECTION_RESET;
824 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
826 case 0: return STATUS_SUCCESS;
827 default:
828 errno = err;
829 perror("wineserver: sock_get_ntstatus() can't map error");
830 return STATUS_UNSUCCESSFUL;
834 /* set the last error depending on errno */
835 static void sock_set_error(void)
837 set_error( sock_get_ntstatus( errno ) );
840 /* create a socket */
841 DECL_HANDLER(create_socket)
843 struct object *obj;
845 reply->handle = 0;
846 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
848 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
849 release_object( obj );
853 /* accept a socket */
854 DECL_HANDLER(accept_socket)
856 struct sock *sock;
858 reply->handle = 0;
859 if ((sock = accept_socket( req->lhandle )) != NULL)
861 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
862 sock->wparam = reply->handle; /* wparam for message is the socket handle */
863 sock_reselect( sock );
864 release_object( &sock->obj );
868 /* set socket event parameters */
869 DECL_HANDLER(set_socket_event)
871 struct sock *sock;
872 struct event *old_event;
873 int pollev;
875 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
876 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
877 old_event = sock->event;
878 sock->mask = req->mask;
879 sock->hmask &= ~req->mask; /* re-enable held events */
880 sock->event = NULL;
881 sock->window = req->window;
882 sock->message = req->msg;
883 sock->wparam = req->handle; /* wparam is the socket handle */
884 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
886 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
888 pollev = sock_reselect( sock );
890 if (sock->mask)
891 sock->state |= FD_WINE_NONBLOCKING;
893 /* if a network event is pending, signal the event object
894 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
895 before a WSAEventSelect() was done on it.
896 (when dealing with Asynchronous socket) */
897 sock_wake_up( sock );
899 if (old_event) release_object( old_event ); /* we're through with it */
900 release_object( &sock->obj );
903 /* get socket event parameters */
904 DECL_HANDLER(get_socket_event)
906 struct sock *sock;
907 int i;
908 int errors[FD_MAX_EVENTS];
910 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
911 if (!sock)
913 reply->mask = 0;
914 reply->pmask = 0;
915 reply->state = 0;
916 return;
918 reply->mask = sock->mask;
919 reply->pmask = sock->pmask;
920 reply->state = sock->state;
921 for (i = 0; i < FD_MAX_EVENTS; i++)
922 errors[i] = sock_get_ntstatus(sock->errors[i]);
924 set_reply_data( errors, min( get_reply_max_size(), sizeof(errors) ));
926 if (req->service)
928 if (req->c_event)
930 struct event *cevent = get_event_obj( current->process, req->c_event,
931 EVENT_MODIFY_STATE );
932 if (cevent)
934 reset_event( cevent );
935 release_object( cevent );
938 sock->pmask = 0;
939 sock_reselect( sock );
941 release_object( &sock->obj );
944 /* re-enable pending socket events */
945 DECL_HANDLER(enable_socket_event)
947 struct sock *sock;
949 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
950 FILE_WRITE_ATTRIBUTES, &sock_ops)))
951 return;
953 sock->pmask &= ~req->mask; /* is this safe? */
954 sock->hmask &= ~req->mask;
955 sock->state |= req->sstate;
956 sock->state &= ~req->cstate;
957 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
959 sock_reselect( sock );
961 release_object( &sock->obj );
964 DECL_HANDLER(set_socket_deferred)
966 struct sock *sock, *acceptsock;
968 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
969 if ( !sock )
970 return;
972 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
973 if ( !acceptsock )
975 release_object( sock );
976 return;
978 sock->deferred = acceptsock;
979 release_object( sock );