dinput: Rename 'axe' to 'axis'.
[wine/multimedia.git] / server / sock.c
blob7c96f5fb573a1eba047d51d2404d48d7ef507980
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"
55 #include "process.h"
56 #include "file.h"
57 #include "handle.h"
58 #include "thread.h"
59 #include "request.h"
60 #include "user.h"
62 /* To avoid conflicts with the Unix socket headers. Plus we only need a few
63 * macros anyway.
65 #define USE_WS_PREFIX
66 #include "winsock2.h"
68 struct sock
70 struct object obj; /* object header */
71 struct fd *fd; /* socket file descriptor */
72 unsigned int state; /* status bits */
73 unsigned int mask; /* event mask */
74 unsigned int hmask; /* held (blocked) events */
75 unsigned int pmask; /* pending events */
76 unsigned int flags; /* socket flags */
77 int polling; /* is socket being polled? */
78 unsigned short type; /* socket type */
79 unsigned short family; /* socket family */
80 struct event *event; /* event object */
81 user_handle_t window; /* window to send the message to */
82 unsigned int message; /* message to send */
83 obj_handle_t wparam; /* message wparam (socket handle) */
84 int errors[FD_MAX_EVENTS]; /* event errors */
85 struct sock *deferred; /* socket that waits for a deferred accept */
86 struct list read_q; /* queue for asynchronous reads */
87 struct list write_q; /* queue for asynchronous writes */
90 static void sock_dump( struct object *obj, int verbose );
91 static int sock_signaled( struct object *obj, struct thread *thread );
92 static struct fd *sock_get_fd( struct object *obj );
93 static unsigned int sock_map_access( struct object *obj, unsigned int access );
94 static void sock_destroy( struct object *obj );
96 static int sock_get_poll_events( struct fd *fd );
97 static void sock_poll_event( struct fd *fd, int event );
98 static enum server_fd_type sock_get_info( struct fd *fd, int *flags );
99 static void sock_queue_async( struct fd *fd, void *apc, void *user, void *iosb, int type, int count );
100 static void sock_cancel_async( struct fd *fd );
102 static int sock_get_error( int err );
103 static void sock_set_error(void);
105 static const struct object_ops sock_ops =
107 sizeof(struct sock), /* size */
108 sock_dump, /* dump */
109 add_queue, /* add_queue */
110 remove_queue, /* remove_queue */
111 sock_signaled, /* signaled */
112 no_satisfied, /* satisfied */
113 no_signal, /* signal */
114 sock_get_fd, /* get_fd */
115 sock_map_access, /* map_access */
116 no_lookup_name, /* lookup_name */
117 fd_close_handle, /* close_handle */
118 sock_destroy /* destroy */
121 static const struct fd_ops sock_fd_ops =
123 sock_get_poll_events, /* get_poll_events */
124 sock_poll_event, /* poll_event */
125 no_flush, /* flush */
126 sock_get_info, /* get_file_info */
127 sock_queue_async, /* queue_async */
128 sock_cancel_async /* cancel_async */
132 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
133 * we post messages if there are multiple events. Used to send
134 * messages. The problem is if there is both a FD_CONNECT event and,
135 * say, an FD_READ event available on the same socket, we want to
136 * notify the app of the connect event first. Otherwise it may
137 * discard the read event because it thinks it hasn't connected yet.
139 static const int event_bitorder[FD_MAX_EVENTS] =
141 FD_CONNECT_BIT,
142 FD_ACCEPT_BIT,
143 FD_OOB_BIT,
144 FD_WRITE_BIT,
145 FD_READ_BIT,
146 FD_CLOSE_BIT,
147 6, 7, 8, 9 /* leftovers */
150 /* Flags that make sense only for SOCK_STREAM sockets */
151 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
153 typedef enum {
154 SOCK_SHUTDOWN_ERROR = -1,
155 SOCK_SHUTDOWN_EOF = 0,
156 SOCK_SHUTDOWN_POLLHUP = 1
157 } sock_shutdown_t;
159 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
161 static sock_shutdown_t sock_check_pollhup(void)
163 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
164 int fd[2], n;
165 struct pollfd pfd;
166 char dummy;
168 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) goto out;
169 if ( shutdown( fd[0], 1 ) ) goto out;
171 pfd.fd = fd[1];
172 pfd.events = POLLIN;
173 pfd.revents = 0;
175 n = poll( &pfd, 1, 0 );
176 if ( n != 1 ) goto out; /* error or timeout */
177 if ( pfd.revents & POLLHUP )
178 ret = SOCK_SHUTDOWN_POLLHUP;
179 else if ( pfd.revents & POLLIN &&
180 read( fd[1], &dummy, 1 ) == 0 )
181 ret = SOCK_SHUTDOWN_EOF;
183 out:
184 close( fd[0] );
185 close( fd[1] );
186 return ret;
189 void sock_init(void)
191 sock_shutdown_type = sock_check_pollhup();
193 switch ( sock_shutdown_type )
195 case SOCK_SHUTDOWN_EOF:
196 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
197 break;
198 case SOCK_SHUTDOWN_POLLHUP:
199 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
200 break;
201 default:
202 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
203 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
207 static int sock_reselect( struct sock *sock )
209 int ev = sock_get_poll_events( sock->fd );
211 if (debug_level)
212 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
214 if (!sock->polling) /* FIXME: should find a better way to do this */
216 /* previously unconnected socket, is this reselect supposed to connect it? */
217 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
218 /* ok, it is, attach it to the wineserver's main poll loop */
219 sock->polling = 1;
221 /* update condition mask */
222 set_fd_events( sock->fd, ev );
223 return ev;
226 /* After POLLHUP is received, the socket will no longer be in the main select loop.
227 This function is used to signal pending events nevertheless */
228 static void sock_try_event( struct sock *sock, int event )
230 event = check_fd_events( sock->fd, event );
231 if (event)
233 if ( debug_level ) fprintf( stderr, "sock_try_event: %x\n", event );
234 sock_poll_event( sock->fd, event );
238 /* wake anybody waiting on the socket event or send the associated message */
239 static void sock_wake_up( struct sock *sock, int pollev )
241 unsigned int events = sock->pmask & sock->mask;
242 int i;
243 int async_active = 0;
245 if ( sock->flags & WSA_FLAG_OVERLAPPED )
247 if ( pollev & (POLLIN|POLLPRI) && !list_empty( &sock->read_q ))
249 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
250 async_terminate_head( &sock->read_q, STATUS_ALERTED );
251 async_active = 1;
253 if ( pollev & POLLOUT && !list_empty( &sock->write_q ))
255 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
256 async_terminate_head( &sock->write_q, STATUS_ALERTED );
257 async_active = 1;
261 /* Do not signal events if there are still pending asynchronous IO requests */
262 /* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
263 if ( !events || async_active ) return;
265 if (sock->event)
267 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
268 set_event( sock->event );
270 if (sock->window)
272 if (debug_level) fprintf(stderr, "signalling events %x win %p\n", events, sock->window );
273 for (i = 0; i < FD_MAX_EVENTS; i++)
275 int event = event_bitorder[i];
276 if (sock->pmask & (1 << event))
278 unsigned int lparam = (1 << event) | (sock->errors[event] << 16);
279 post_message( sock->window, sock->message, (unsigned long)sock->wparam, lparam );
282 sock->pmask = 0;
283 sock_reselect( sock );
287 inline static int sock_error( struct fd *fd )
289 unsigned int optval = 0, optlen;
291 optlen = sizeof(optval);
292 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
293 return optval ? sock_get_error(optval) : 0;
296 static void sock_poll_event( struct fd *fd, int event )
298 struct sock *sock = get_fd_user( fd );
299 int hangup_seen = 0;
301 assert( sock->obj.ops == &sock_ops );
302 if (debug_level)
303 fprintf(stderr, "socket %p select event: %x\n", sock, event);
304 if (sock->state & FD_CONNECT)
306 /* connecting */
307 if (event & POLLOUT)
309 /* we got connected */
310 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
311 sock->state &= ~FD_CONNECT;
312 sock->pmask |= FD_CONNECT;
313 sock->errors[FD_CONNECT_BIT] = 0;
314 if (debug_level)
315 fprintf(stderr, "socket %p connection success\n", sock);
317 else if (event & (POLLERR|POLLHUP))
319 /* we didn't get connected? */
320 sock->state &= ~FD_CONNECT;
321 sock->pmask |= FD_CONNECT;
322 sock->errors[FD_CONNECT_BIT] = sock_error( fd );
323 if (debug_level)
324 fprintf(stderr, "socket %p connection failure\n", sock);
327 else if (sock->state & FD_WINE_LISTENING)
329 /* listening */
330 if (event & POLLIN)
332 /* incoming connection */
333 sock->pmask |= FD_ACCEPT;
334 sock->errors[FD_ACCEPT_BIT] = 0;
335 sock->hmask |= FD_ACCEPT;
337 else if (event & (POLLERR|POLLHUP))
339 /* failed incoming connection? */
340 sock->pmask |= FD_ACCEPT;
341 sock->errors[FD_ACCEPT_BIT] = sock_error( fd );
342 sock->hmask |= FD_ACCEPT;
345 else
347 /* normal data flow */
348 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
350 char dummy;
351 int nr;
353 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
354 * has been closed, so we need to check for it explicitly here */
355 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
356 if ( nr > 0 )
358 /* incoming data */
359 sock->pmask |= FD_READ;
360 sock->hmask |= (FD_READ|FD_CLOSE);
361 sock->errors[FD_READ_BIT] = 0;
362 if (debug_level)
363 fprintf(stderr, "socket %p is readable\n", sock );
365 else if ( nr == 0 )
366 hangup_seen = 1;
367 else
369 /* EAGAIN can happen if an async recv() falls between the server's poll()
370 call and the invocation of this routine */
371 if ( errno == EAGAIN )
372 event &= ~POLLIN;
373 else
375 if ( debug_level )
376 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
377 event = POLLERR;
382 else if ( sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP) )
384 hangup_seen = 1;
386 else if ( event & POLLIN ) /* POLLIN for non-stream socket */
388 sock->pmask |= FD_READ;
389 sock->hmask |= (FD_READ|FD_CLOSE);
390 sock->errors[FD_READ_BIT] = 0;
391 if (debug_level)
392 fprintf(stderr, "socket %p is readable\n", sock );
396 if (event & POLLOUT)
398 sock->pmask |= FD_WRITE;
399 sock->hmask |= FD_WRITE;
400 sock->errors[FD_WRITE_BIT] = 0;
401 if (debug_level)
402 fprintf(stderr, "socket %p is writable\n", sock);
404 if (event & POLLPRI)
406 sock->pmask |= FD_OOB;
407 sock->hmask |= FD_OOB;
408 sock->errors[FD_OOB_BIT] = 0;
409 if (debug_level)
410 fprintf(stderr, "socket %p got OOB data\n", sock);
412 /* According to WS2 specs, FD_CLOSE is only delivered when there is
413 no more data to be read (i.e. hangup_seen = 1) */
414 else if ( hangup_seen && (sock->state & (FD_READ|FD_WRITE) ))
416 sock->errors[FD_CLOSE_BIT] = sock_error( fd );
417 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
418 sock->state &= ~FD_WRITE;
419 sock->pmask |= FD_CLOSE;
420 sock->hmask |= FD_CLOSE;
421 if (debug_level)
422 fprintf(stderr, "socket %p aborted by error %d, event: %x - removing from select loop\n",
423 sock, sock->errors[FD_CLOSE_BIT], event);
427 if ( sock->pmask & FD_CLOSE || event & (POLLERR|POLLHUP) )
429 if ( debug_level )
430 fprintf( stderr, "removing socket %p from select loop\n", sock );
431 set_fd_events( sock->fd, -1 );
433 else
434 sock_reselect( sock );
436 /* wake up anyone waiting for whatever just happened */
437 if ( sock->pmask & sock->mask || sock->flags & WSA_FLAG_OVERLAPPED ) sock_wake_up( sock, event );
439 /* if anyone is stupid enough to wait on the socket object itself,
440 * maybe we should wake them up too, just in case? */
441 wake_up( &sock->obj, 0 );
444 static void sock_dump( struct object *obj, int verbose )
446 struct sock *sock = (struct sock *)obj;
447 assert( obj->ops == &sock_ops );
448 printf( "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
449 sock->fd, sock->state,
450 sock->mask, sock->pmask, sock->hmask );
453 static int sock_signaled( struct object *obj, struct thread *thread )
455 struct sock *sock = (struct sock *)obj;
456 assert( obj->ops == &sock_ops );
458 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
461 static unsigned int sock_map_access( struct object *obj, unsigned int access )
463 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
464 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
465 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
466 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
467 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
470 static int sock_get_poll_events( struct fd *fd )
472 struct sock *sock = get_fd_user( fd );
473 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
474 int ev = 0;
476 assert( sock->obj.ops == &sock_ops );
478 if (sock->state & FD_CONNECT)
479 /* connecting, wait for writable */
480 return POLLOUT;
481 if (sock->state & FD_WINE_LISTENING)
482 /* listening, wait for readable */
483 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
485 if (mask & (FD_READ) || (sock->flags & WSA_FLAG_OVERLAPPED && !list_empty( &sock->read_q )))
486 ev |= POLLIN | POLLPRI;
487 if (mask & FD_WRITE || (sock->flags & WSA_FLAG_OVERLAPPED && !list_empty( &sock->write_q )))
488 ev |= POLLOUT;
489 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
490 if ( sock->type == SOCK_STREAM && ( sock->mask & ~sock->hmask & FD_CLOSE) )
491 ev |= POLLIN;
493 return ev;
496 static enum server_fd_type sock_get_info( struct fd *fd, int *flags )
498 struct sock *sock = get_fd_user( fd );
499 assert( sock->obj.ops == &sock_ops );
501 *flags = FD_FLAG_AVAILABLE;
502 if (sock->flags & WSA_FLAG_OVERLAPPED) *flags |= FD_FLAG_OVERLAPPED;
503 if ( sock->type != SOCK_STREAM || sock->state & FD_WINE_CONNECTED )
505 if ( !(sock->state & FD_READ ) ) *flags |= FD_FLAG_RECV_SHUTDOWN;
506 if ( !(sock->state & FD_WRITE ) ) *flags |= FD_FLAG_SEND_SHUTDOWN;
508 return FD_TYPE_SOCKET;
511 static void sock_queue_async( struct fd *fd, void *apc, void *user, void *iosb,
512 int type, int count )
514 struct sock *sock = get_fd_user( fd );
515 struct list *queue;
516 int pollev;
518 assert( sock->obj.ops == &sock_ops );
520 if ( !(sock->flags & WSA_FLAG_OVERLAPPED) )
522 set_error( STATUS_INVALID_HANDLE );
523 return;
526 switch (type)
528 case ASYNC_TYPE_READ:
529 queue = &sock->read_q;
530 sock->hmask &= ~FD_CLOSE;
531 break;
532 case ASYNC_TYPE_WRITE:
533 queue = &sock->write_q;
534 break;
535 default:
536 set_error( STATUS_INVALID_PARAMETER );
537 return;
540 if ( ( !( sock->state & FD_READ ) && type == ASYNC_TYPE_READ ) ||
541 ( !( sock->state & FD_WRITE ) && type == ASYNC_TYPE_WRITE ) )
543 set_error( STATUS_PIPE_DISCONNECTED );
545 else
547 if (!create_async( current, NULL, queue, apc, user, iosb ))
548 return;
551 pollev = sock_reselect( sock );
552 if ( pollev ) sock_try_event( sock, pollev );
555 static void sock_cancel_async( struct fd *fd )
557 struct sock *sock = get_fd_user( fd );
558 assert( sock->obj.ops == &sock_ops );
560 async_terminate_queue( &sock->read_q, STATUS_CANCELLED );
561 async_terminate_queue( &sock->write_q, STATUS_CANCELLED );
564 static struct fd *sock_get_fd( struct object *obj )
566 struct sock *sock = (struct sock *)obj;
567 return (struct fd *)grab_object( sock->fd );
570 static void sock_destroy( struct object *obj )
572 struct sock *sock = (struct sock *)obj;
573 assert( obj->ops == &sock_ops );
575 /* FIXME: special socket shutdown stuff? */
577 if ( sock->deferred )
578 release_object( sock->deferred );
580 if ( sock->flags & WSA_FLAG_OVERLAPPED )
582 async_terminate_queue( &sock->read_q, STATUS_CANCELLED );
583 async_terminate_queue( &sock->write_q, STATUS_CANCELLED );
585 if (sock->event) release_object( sock->event );
586 if (sock->fd)
588 /* shut the socket down to force pending poll() calls in the client to return */
589 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
590 release_object( sock->fd );
594 /* create a new and unconnected socket */
595 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
597 struct sock *sock;
598 int sockfd;
600 sockfd = socket( family, type, protocol );
601 if (debug_level)
602 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
603 if (sockfd == -1)
605 sock_set_error();
606 return NULL;
608 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
609 if (!(sock = alloc_object( &sock_ops )))
611 close( sockfd );
612 return NULL;
614 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
615 sock->mask = 0;
616 sock->hmask = 0;
617 sock->pmask = 0;
618 sock->polling = 0;
619 sock->flags = flags;
620 sock->type = type;
621 sock->family = family;
622 sock->event = NULL;
623 sock->window = 0;
624 sock->message = 0;
625 sock->wparam = 0;
626 sock->deferred = NULL;
627 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj )))
629 release_object( sock );
630 return NULL;
632 list_init( &sock->read_q );
633 list_init( &sock->write_q );
634 sock_reselect( sock );
635 clear_error();
636 return &sock->obj;
639 /* accept a socket (creates a new fd) */
640 static struct sock *accept_socket( obj_handle_t handle )
642 struct sock *acceptsock;
643 struct sock *sock;
644 int acceptfd;
645 struct sockaddr saddr;
647 sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
648 if (!sock)
649 return NULL;
651 if ( sock->deferred )
653 acceptsock = sock->deferred;
654 sock->deferred = NULL;
656 else
659 /* Try to accept(2). We can't be safe that this an already connected socket
660 * or that accept() is allowed on it. In those cases we will get -1/errno
661 * return.
663 unsigned int slen = sizeof(saddr);
664 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
665 if (acceptfd==-1)
667 sock_set_error();
668 release_object( sock );
669 return NULL;
671 if (!(acceptsock = alloc_object( &sock_ops )))
673 close( acceptfd );
674 release_object( sock );
675 return NULL;
678 /* newly created socket gets the same properties of the listening socket */
679 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
680 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
681 if (sock->state & FD_WINE_NONBLOCKING)
682 acceptsock->state |= FD_WINE_NONBLOCKING;
683 acceptsock->mask = sock->mask;
684 acceptsock->hmask = 0;
685 acceptsock->pmask = 0;
686 acceptsock->polling = 0;
687 acceptsock->type = sock->type;
688 acceptsock->family = sock->family;
689 acceptsock->event = NULL;
690 acceptsock->window = sock->window;
691 acceptsock->message = sock->message;
692 acceptsock->wparam = 0;
693 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
694 acceptsock->flags = sock->flags;
695 acceptsock->deferred = NULL;
696 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj )))
698 release_object( acceptsock );
699 release_object( sock );
700 return NULL;
702 list_init( &acceptsock->read_q );
703 list_init( &acceptsock->write_q );
705 clear_error();
706 sock->pmask &= ~FD_ACCEPT;
707 sock->hmask &= ~FD_ACCEPT;
708 sock_reselect( sock );
709 release_object( sock );
710 return acceptsock;
713 /* set the last error depending on errno */
714 static int sock_get_error( int err )
716 switch (err)
718 case EINTR: return WSAEINTR;
719 case EBADF: return WSAEBADF;
720 case EPERM:
721 case EACCES: return WSAEACCES;
722 case EFAULT: return WSAEFAULT;
723 case EINVAL: return WSAEINVAL;
724 case EMFILE: return WSAEMFILE;
725 case EWOULDBLOCK: return WSAEWOULDBLOCK;
726 case EINPROGRESS: return WSAEINPROGRESS;
727 case EALREADY: return WSAEALREADY;
728 case ENOTSOCK: return WSAENOTSOCK;
729 case EDESTADDRREQ: return WSAEDESTADDRREQ;
730 case EMSGSIZE: return WSAEMSGSIZE;
731 case EPROTOTYPE: return WSAEPROTOTYPE;
732 case ENOPROTOOPT: return WSAENOPROTOOPT;
733 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
734 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
735 case EOPNOTSUPP: return WSAEOPNOTSUPP;
736 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
737 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
738 case EADDRINUSE: return WSAEADDRINUSE;
739 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
740 case ENETDOWN: return WSAENETDOWN;
741 case ENETUNREACH: return WSAENETUNREACH;
742 case ENETRESET: return WSAENETRESET;
743 case ECONNABORTED: return WSAECONNABORTED;
744 case EPIPE:
745 case ECONNRESET: return WSAECONNRESET;
746 case ENOBUFS: return WSAENOBUFS;
747 case EISCONN: return WSAEISCONN;
748 case ENOTCONN: return WSAENOTCONN;
749 case ESHUTDOWN: return WSAESHUTDOWN;
750 case ETOOMANYREFS: return WSAETOOMANYREFS;
751 case ETIMEDOUT: return WSAETIMEDOUT;
752 case ECONNREFUSED: return WSAECONNREFUSED;
753 case ELOOP: return WSAELOOP;
754 case ENAMETOOLONG: return WSAENAMETOOLONG;
755 case EHOSTDOWN: return WSAEHOSTDOWN;
756 case EHOSTUNREACH: return WSAEHOSTUNREACH;
757 case ENOTEMPTY: return WSAENOTEMPTY;
758 #ifdef EPROCLIM
759 case EPROCLIM: return WSAEPROCLIM;
760 #endif
761 #ifdef EUSERS
762 case EUSERS: return WSAEUSERS;
763 #endif
764 #ifdef EDQUOT
765 case EDQUOT: return WSAEDQUOT;
766 #endif
767 #ifdef ESTALE
768 case ESTALE: return WSAESTALE;
769 #endif
770 #ifdef EREMOTE
771 case EREMOTE: return WSAEREMOTE;
772 #endif
773 default: errno=err; perror("sock_set_error"); return WSAEFAULT;
777 /* set the last error depending on errno */
778 static void sock_set_error(void)
780 set_error( sock_get_error( errno ) );
783 /* create a socket */
784 DECL_HANDLER(create_socket)
786 struct object *obj;
788 reply->handle = 0;
789 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
791 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
792 release_object( obj );
796 /* accept a socket */
797 DECL_HANDLER(accept_socket)
799 struct sock *sock;
801 reply->handle = 0;
802 if ((sock = accept_socket( req->lhandle )) != NULL)
804 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
805 sock->wparam = reply->handle; /* wparam for message is the socket handle */
806 sock_reselect( sock );
807 release_object( &sock->obj );
811 /* set socket event parameters */
812 DECL_HANDLER(set_socket_event)
814 struct sock *sock;
815 struct event *old_event;
816 int pollev;
818 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
819 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
820 old_event = sock->event;
821 sock->mask = req->mask;
822 sock->hmask &= ~req->mask; /* re-enable held events */
823 sock->event = NULL;
824 sock->window = req->window;
825 sock->message = req->msg;
826 sock->wparam = req->handle; /* wparam is the socket handle */
827 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
829 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
831 pollev = sock_reselect( sock );
832 if ( pollev ) sock_try_event( sock, pollev );
834 if (sock->mask)
835 sock->state |= FD_WINE_NONBLOCKING;
837 /* if a network event is pending, signal the event object
838 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
839 before a WSAEventSelect() was done on it.
840 (when dealing with Asynchronous socket) */
841 if (sock->pmask & sock->mask) sock_wake_up( sock, pollev );
843 if (old_event) release_object( old_event ); /* we're through with it */
844 release_object( &sock->obj );
847 /* get socket event parameters */
848 DECL_HANDLER(get_socket_event)
850 struct sock *sock;
852 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
853 if (!sock)
855 reply->mask = 0;
856 reply->pmask = 0;
857 reply->state = 0;
858 set_error( WSAENOTSOCK );
859 return;
861 reply->mask = sock->mask;
862 reply->pmask = sock->pmask;
863 reply->state = sock->state;
864 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
866 if (req->service)
868 if (req->c_event)
870 struct event *cevent = get_event_obj( current->process, req->c_event,
871 EVENT_MODIFY_STATE );
872 if (cevent)
874 reset_event( cevent );
875 release_object( cevent );
878 sock->pmask = 0;
879 sock_reselect( sock );
881 release_object( &sock->obj );
884 /* re-enable pending socket events */
885 DECL_HANDLER(enable_socket_event)
887 struct sock *sock;
888 int pollev;
890 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
891 FILE_WRITE_ATTRIBUTES, &sock_ops)))
892 return;
894 sock->pmask &= ~req->mask; /* is this safe? */
895 sock->hmask &= ~req->mask;
896 if ( req->mask & FD_READ )
897 sock->hmask &= ~FD_CLOSE;
898 sock->state |= req->sstate;
899 sock->state &= ~req->cstate;
900 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
902 pollev = sock_reselect( sock );
903 if ( pollev ) sock_try_event( sock, pollev );
905 release_object( &sock->obj );
908 DECL_HANDLER(set_socket_deferred)
910 struct sock *sock, *acceptsock;
912 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
913 if ( !sock )
915 set_error( WSAENOTSOCK );
916 return;
918 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
919 if ( !acceptsock )
921 release_object( sock );
922 set_error( WSAENOTSOCK );
923 return;
925 sock->deferred = acceptsock;
926 release_object( sock );