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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * FIXME: we use read|write access in all cases. Shouldn't we depend that
21 * on the access of the current handle?
33 #ifdef HAVE_SYS_ERRNO_H
34 # include <sys/errno.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
44 #ifdef HAVE_SYS_FILIO_H
45 # include <sys/filio.h>
60 /* To avoid conflicts with the Unix socket headers. Plus we only need a few
68 struct object obj; /* object header */
69 struct fd *fd; /* socket file descriptor */
70 unsigned int state; /* status bits */
71 unsigned int mask; /* event mask */
72 unsigned int hmask; /* held (blocked) events */
73 unsigned int pmask; /* pending events */
74 unsigned int flags; /* socket flags */
75 int polling; /* is socket being polled? */
76 unsigned short type; /* socket type */
77 unsigned short family; /* socket family */
78 struct event *event; /* event object */
79 user_handle_t window; /* window to send the message to */
80 unsigned int message; /* message to send */
81 obj_handle_t wparam; /* message wparam (socket handle) */
82 int errors[FD_MAX_EVENTS]; /* event errors */
83 struct sock *deferred; /* socket that waits for a deferred accept */
84 struct list read_q; /* queue for asynchronous reads */
85 struct list write_q; /* queue for asynchronous writes */
88 static void sock_dump( struct object *obj, int verbose );
89 static int sock_signaled( struct object *obj, struct thread *thread );
90 static struct fd *sock_get_fd( struct object *obj );
91 static void sock_destroy( struct object *obj );
93 static int sock_get_poll_events( struct fd *fd );
94 static void sock_poll_event( struct fd *fd, int event );
95 static int sock_get_info( struct fd *fd );
96 static void sock_queue_async( struct fd *fd, void *apc, void *user, void *iosb, int type, int count );
97 static void sock_cancel_async( struct fd *fd );
99 static int sock_get_error( int err );
100 static void sock_set_error(void);
102 static const struct object_ops sock_ops =
104 sizeof(struct sock), /* size */
105 sock_dump, /* dump */
106 add_queue, /* add_queue */
107 remove_queue, /* remove_queue */
108 sock_signaled, /* signaled */
109 no_satisfied, /* satisfied */
110 no_signal, /* signal */
111 sock_get_fd, /* get_fd */
112 sock_destroy /* destroy */
115 static const struct fd_ops sock_fd_ops =
117 sock_get_poll_events, /* get_poll_events */
118 sock_poll_event, /* poll_event */
119 no_flush, /* flush */
120 sock_get_info, /* get_file_info */
121 sock_queue_async, /* queue_async */
122 sock_cancel_async /* cancel_async */
126 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
127 * we post messages if there are multiple events. Used to send
128 * messages. The problem is if there is both a FD_CONNECT event and,
129 * say, an FD_READ event available on the same socket, we want to
130 * notify the app of the connect event first. Otherwise it may
131 * discard the read event because it thinks it hasn't connected yet.
133 static const int event_bitorder[FD_MAX_EVENTS] =
141 6, 7, 8, 9 /* leftovers */
144 /* Flags that make sense only for SOCK_STREAM sockets */
145 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
148 SOCK_SHUTDOWN_ERROR = -1,
149 SOCK_SHUTDOWN_EOF = 0,
150 SOCK_SHUTDOWN_POLLHUP = 1
153 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
155 static sock_shutdown_t sock_check_pollhup(void)
157 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
162 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) goto out;
163 if ( shutdown( fd[0], 1 ) ) goto out;
169 n = poll( &pfd, 1, 0 );
170 if ( n != 1 ) goto out; /* error or timeout */
171 if ( pfd.revents & POLLHUP )
172 ret = SOCK_SHUTDOWN_POLLHUP;
173 else if ( pfd.revents & POLLIN &&
174 read( fd[1], &dummy, 1 ) == 0 )
175 ret = SOCK_SHUTDOWN_EOF;
185 sock_shutdown_type = sock_check_pollhup();
187 switch ( sock_shutdown_type )
189 case SOCK_SHUTDOWN_EOF:
190 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
192 case SOCK_SHUTDOWN_POLLHUP:
193 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
196 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
197 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
201 static int sock_reselect( struct sock *sock )
203 int ev = sock_get_poll_events( sock->fd );
206 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
208 if (!sock->polling) /* FIXME: should find a better way to do this */
210 /* previously unconnected socket, is this reselect supposed to connect it? */
211 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
212 /* ok, it is, attach it to the wineserver's main poll loop */
215 /* update condition mask */
216 set_fd_events( sock->fd, ev );
220 /* After POLLHUP is received, the socket will no longer be in the main select loop.
221 This function is used to signal pending events nevertheless */
222 static void sock_try_event( struct sock *sock, int event )
224 event = check_fd_events( sock->fd, event );
227 if ( debug_level ) fprintf( stderr, "sock_try_event: %x\n", event );
228 sock_poll_event( sock->fd, event );
232 /* wake anybody waiting on the socket event or send the associated message */
233 static void sock_wake_up( struct sock *sock, int pollev )
235 unsigned int events = sock->pmask & sock->mask;
237 int async_active = 0;
239 if ( sock->flags & WSA_FLAG_OVERLAPPED )
241 if ( pollev & (POLLIN|POLLPRI) && !list_empty( &sock->read_q ))
243 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
244 async_terminate_head( &sock->read_q, STATUS_ALERTED );
247 if ( pollev & POLLOUT && !list_empty( &sock->write_q ))
249 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
250 async_terminate_head( &sock->write_q, STATUS_ALERTED );
255 /* Do not signal events if there are still pending asynchronous IO requests */
256 /* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
257 if ( !events || async_active ) return;
261 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
262 set_event( sock->event );
266 if (debug_level) fprintf(stderr, "signalling events %x win %p\n", events, sock->window );
267 for (i = 0; i < FD_MAX_EVENTS; i++)
269 int event = event_bitorder[i];
270 if (sock->pmask & (1 << event))
272 unsigned int lparam = (1 << event) | (sock->errors[event] << 16);
273 post_message( sock->window, sock->message, (unsigned int)sock->wparam, lparam );
277 sock_reselect( sock );
281 inline static int sock_error( struct fd *fd )
283 unsigned int optval = 0, optlen;
285 optlen = sizeof(optval);
286 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
287 return optval ? sock_get_error(optval) : 0;
290 static void sock_poll_event( struct fd *fd, int event )
292 struct sock *sock = get_fd_user( fd );
295 assert( sock->obj.ops == &sock_ops );
297 fprintf(stderr, "socket %p select event: %x\n", sock, event);
298 if (sock->state & FD_CONNECT)
303 /* we got connected */
304 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
305 sock->state &= ~FD_CONNECT;
306 sock->pmask |= FD_CONNECT;
307 sock->errors[FD_CONNECT_BIT] = 0;
309 fprintf(stderr, "socket %p connection success\n", sock);
311 else if (event & (POLLERR|POLLHUP))
313 /* we didn't get connected? */
314 sock->state &= ~FD_CONNECT;
315 sock->pmask |= FD_CONNECT;
316 sock->errors[FD_CONNECT_BIT] = sock_error( fd );
318 fprintf(stderr, "socket %p connection failure\n", sock);
321 else if (sock->state & FD_WINE_LISTENING)
326 /* incoming connection */
327 sock->pmask |= FD_ACCEPT;
328 sock->errors[FD_ACCEPT_BIT] = 0;
329 sock->hmask |= FD_ACCEPT;
331 else if (event & (POLLERR|POLLHUP))
333 /* failed incoming connection? */
334 sock->pmask |= FD_ACCEPT;
335 sock->errors[FD_ACCEPT_BIT] = sock_error( fd );
336 sock->hmask |= FD_ACCEPT;
341 /* normal data flow */
342 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
347 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
348 * has been closed, so we need to check for it explicitly here */
349 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
353 sock->pmask |= FD_READ;
354 sock->hmask |= (FD_READ|FD_CLOSE);
355 sock->errors[FD_READ_BIT] = 0;
357 fprintf(stderr, "socket %p is readable\n", sock );
363 /* EAGAIN can happen if an async recv() falls between the server's poll()
364 call and the invocation of this routine */
365 if ( errno == EAGAIN )
370 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
376 else if ( sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP) )
380 else if ( event & POLLIN ) /* POLLIN for non-stream socket */
382 sock->pmask |= FD_READ;
383 sock->hmask |= (FD_READ|FD_CLOSE);
384 sock->errors[FD_READ_BIT] = 0;
386 fprintf(stderr, "socket %p is readable\n", sock );
392 sock->pmask |= FD_WRITE;
393 sock->hmask |= FD_WRITE;
394 sock->errors[FD_WRITE_BIT] = 0;
396 fprintf(stderr, "socket %p is writable\n", sock);
400 sock->pmask |= FD_OOB;
401 sock->hmask |= FD_OOB;
402 sock->errors[FD_OOB_BIT] = 0;
404 fprintf(stderr, "socket %p got OOB data\n", sock);
406 /* According to WS2 specs, FD_CLOSE is only delivered when there is
407 no more data to be read (i.e. hangup_seen = 1) */
408 else if ( hangup_seen && (sock->state & (FD_READ|FD_WRITE) ))
410 sock->errors[FD_CLOSE_BIT] = sock_error( fd );
411 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
412 sock->state &= ~FD_WRITE;
413 sock->pmask |= FD_CLOSE;
414 sock->hmask |= FD_CLOSE;
416 fprintf(stderr, "socket %p aborted by error %d, event: %x - removing from select loop\n",
417 sock, sock->errors[FD_CLOSE_BIT], event);
421 if ( sock->pmask & FD_CLOSE || event & (POLLERR|POLLHUP) )
424 fprintf( stderr, "removing socket %p from select loop\n", sock );
425 set_fd_events( sock->fd, -1 );
428 sock_reselect( sock );
430 /* wake up anyone waiting for whatever just happened */
431 if ( sock->pmask & sock->mask || sock->flags & WSA_FLAG_OVERLAPPED ) sock_wake_up( sock, event );
433 /* if anyone is stupid enough to wait on the socket object itself,
434 * maybe we should wake them up too, just in case? */
435 wake_up( &sock->obj, 0 );
438 static void sock_dump( struct object *obj, int verbose )
440 struct sock *sock = (struct sock *)obj;
441 assert( obj->ops == &sock_ops );
442 printf( "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
443 sock->fd, sock->state,
444 sock->mask, sock->pmask, sock->hmask );
447 static int sock_signaled( struct object *obj, struct thread *thread )
449 struct sock *sock = (struct sock *)obj;
450 assert( obj->ops == &sock_ops );
452 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
455 static int sock_get_poll_events( struct fd *fd )
457 struct sock *sock = get_fd_user( fd );
458 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
461 assert( sock->obj.ops == &sock_ops );
463 if (sock->state & FD_CONNECT)
464 /* connecting, wait for writable */
466 if (sock->state & FD_WINE_LISTENING)
467 /* listening, wait for readable */
468 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
470 if (mask & (FD_READ) || (sock->flags & WSA_FLAG_OVERLAPPED && !list_empty( &sock->read_q )))
471 ev |= POLLIN | POLLPRI;
472 if (mask & FD_WRITE || (sock->flags & WSA_FLAG_OVERLAPPED && !list_empty( &sock->write_q )))
474 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
475 if ( sock->type == SOCK_STREAM && ( sock->mask & ~sock->hmask & FD_CLOSE) )
481 static int sock_get_info( struct fd *fd )
483 int flags = FD_FLAG_AVAILABLE;
484 struct sock *sock = get_fd_user( fd );
485 assert( sock->obj.ops == &sock_ops );
487 if (sock->flags & WSA_FLAG_OVERLAPPED) flags |= FD_FLAG_OVERLAPPED;
488 if ( sock->type != SOCK_STREAM || sock->state & FD_WINE_CONNECTED )
490 if ( !(sock->state & FD_READ ) ) flags |= FD_FLAG_RECV_SHUTDOWN;
491 if ( !(sock->state & FD_WRITE ) ) flags |= FD_FLAG_SEND_SHUTDOWN;
496 static void sock_queue_async( struct fd *fd, void *apc, void *user, void *iosb,
497 int type, int count )
499 struct sock *sock = get_fd_user( fd );
503 assert( sock->obj.ops == &sock_ops );
505 if ( !(sock->flags & WSA_FLAG_OVERLAPPED) )
507 set_error( STATUS_INVALID_HANDLE );
513 case ASYNC_TYPE_READ:
514 queue = &sock->read_q;
515 sock->hmask &= ~FD_CLOSE;
517 case ASYNC_TYPE_WRITE:
518 queue = &sock->write_q;
521 set_error( STATUS_INVALID_PARAMETER );
525 if ( ( !( sock->state & FD_READ ) && type == ASYNC_TYPE_READ ) ||
526 ( !( sock->state & FD_WRITE ) && type == ASYNC_TYPE_WRITE ) )
528 set_error( STATUS_PIPE_DISCONNECTED );
532 if (!create_async( current, NULL, queue, apc, user, iosb ))
536 pollev = sock_reselect( sock );
537 if ( pollev ) sock_try_event( sock, pollev );
540 static void sock_cancel_async( struct fd *fd )
542 struct sock *sock = get_fd_user( fd );
543 assert( sock->obj.ops == &sock_ops );
545 async_terminate_queue( &sock->read_q, STATUS_CANCELLED );
546 async_terminate_queue( &sock->write_q, STATUS_CANCELLED );
549 static struct fd *sock_get_fd( struct object *obj )
551 struct sock *sock = (struct sock *)obj;
552 return (struct fd *)grab_object( sock->fd );
555 static void sock_destroy( struct object *obj )
557 struct sock *sock = (struct sock *)obj;
558 assert( obj->ops == &sock_ops );
560 /* FIXME: special socket shutdown stuff? */
562 if ( sock->deferred )
563 release_object( sock->deferred );
565 if ( sock->flags & WSA_FLAG_OVERLAPPED )
567 async_terminate_queue( &sock->read_q, STATUS_CANCELLED );
568 async_terminate_queue( &sock->write_q, STATUS_CANCELLED );
570 if (sock->event) release_object( sock->event );
571 if (sock->fd) release_object( sock->fd );
574 /* create a new and unconnected socket */
575 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
580 sockfd = socket( family, type, protocol );
582 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
588 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
589 if (!(sock = alloc_object( &sock_ops )))
594 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
601 sock->family = family;
606 sock->deferred = NULL;
607 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj )))
609 release_object( sock );
612 list_init( &sock->read_q );
613 list_init( &sock->write_q );
614 sock_reselect( sock );
619 /* accept a socket (creates a new fd) */
620 static struct sock *accept_socket( obj_handle_t handle )
622 struct sock *acceptsock;
625 struct sockaddr saddr;
628 sock=(struct sock*)get_handle_obj(current->process,handle,
629 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
633 if ( sock->deferred )
635 acceptsock = sock->deferred;
636 sock->deferred = NULL;
641 /* Try to accept(2). We can't be safe that this an already connected socket
642 * or that accept() is allowed on it. In those cases we will get -1/errno
645 slen = sizeof(saddr);
646 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
650 release_object( sock );
653 if (!(acceptsock = alloc_object( &sock_ops )))
656 release_object( sock );
660 /* newly created socket gets the same properties of the listening socket */
661 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
662 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
663 if (sock->state & FD_WINE_NONBLOCKING)
664 acceptsock->state |= FD_WINE_NONBLOCKING;
665 acceptsock->mask = sock->mask;
666 acceptsock->hmask = 0;
667 acceptsock->pmask = 0;
668 acceptsock->polling = 0;
669 acceptsock->type = sock->type;
670 acceptsock->family = sock->family;
671 acceptsock->event = NULL;
672 acceptsock->window = sock->window;
673 acceptsock->message = sock->message;
674 acceptsock->wparam = 0;
675 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
676 acceptsock->flags = sock->flags;
677 acceptsock->deferred = NULL;
678 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj )))
680 release_object( acceptsock );
681 release_object( sock );
684 list_init( &acceptsock->read_q );
685 list_init( &acceptsock->write_q );
688 sock->pmask &= ~FD_ACCEPT;
689 sock->hmask &= ~FD_ACCEPT;
690 sock_reselect( sock );
691 release_object( sock );
695 /* set the last error depending on errno */
696 static int sock_get_error( int err )
700 case EINTR: return WSAEINTR;
701 case EBADF: return WSAEBADF;
703 case EACCES: return WSAEACCES;
704 case EFAULT: return WSAEFAULT;
705 case EINVAL: return WSAEINVAL;
706 case EMFILE: return WSAEMFILE;
707 case EWOULDBLOCK: return WSAEWOULDBLOCK;
708 case EINPROGRESS: return WSAEINPROGRESS;
709 case EALREADY: return WSAEALREADY;
710 case ENOTSOCK: return WSAENOTSOCK;
711 case EDESTADDRREQ: return WSAEDESTADDRREQ;
712 case EMSGSIZE: return WSAEMSGSIZE;
713 case EPROTOTYPE: return WSAEPROTOTYPE;
714 case ENOPROTOOPT: return WSAENOPROTOOPT;
715 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
716 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
717 case EOPNOTSUPP: return WSAEOPNOTSUPP;
718 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
719 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
720 case EADDRINUSE: return WSAEADDRINUSE;
721 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
722 case ENETDOWN: return WSAENETDOWN;
723 case ENETUNREACH: return WSAENETUNREACH;
724 case ENETRESET: return WSAENETRESET;
725 case ECONNABORTED: return WSAECONNABORTED;
727 case ECONNRESET: return WSAECONNRESET;
728 case ENOBUFS: return WSAENOBUFS;
729 case EISCONN: return WSAEISCONN;
730 case ENOTCONN: return WSAENOTCONN;
731 case ESHUTDOWN: return WSAESHUTDOWN;
732 case ETOOMANYREFS: return WSAETOOMANYREFS;
733 case ETIMEDOUT: return WSAETIMEDOUT;
734 case ECONNREFUSED: return WSAECONNREFUSED;
735 case ELOOP: return WSAELOOP;
736 case ENAMETOOLONG: return WSAENAMETOOLONG;
737 case EHOSTDOWN: return WSAEHOSTDOWN;
738 case EHOSTUNREACH: return WSAEHOSTUNREACH;
739 case ENOTEMPTY: return WSAENOTEMPTY;
741 case EPROCLIM: return WSAEPROCLIM;
744 case EUSERS: return WSAEUSERS;
747 case EDQUOT: return WSAEDQUOT;
750 case ESTALE: return WSAESTALE;
753 case EREMOTE: return WSAEREMOTE;
755 default: errno=err; perror("sock_set_error"); return WSAEFAULT;
759 /* set the last error depending on errno */
760 static void sock_set_error(void)
762 set_error( sock_get_error( errno ) );
765 /* create a socket */
766 DECL_HANDLER(create_socket)
771 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
773 reply->handle = alloc_handle( current->process, obj, req->access, req->inherit );
774 release_object( obj );
778 /* accept a socket */
779 DECL_HANDLER(accept_socket)
784 if ((sock = accept_socket( req->lhandle )) != NULL)
786 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->inherit );
787 sock->wparam = reply->handle; /* wparam for message is the socket handle */
788 sock_reselect( sock );
789 release_object( &sock->obj );
793 /* set socket event parameters */
794 DECL_HANDLER(set_socket_event)
797 struct event *old_event;
800 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
801 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &sock_ops)))
803 old_event = sock->event;
804 sock->mask = req->mask;
806 sock->window = req->window;
807 sock->message = req->msg;
808 sock->wparam = req->handle; /* wparam is the socket handle */
809 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
811 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
813 pollev = sock_reselect( sock );
814 if ( pollev ) sock_try_event( sock, pollev );
817 sock->state |= FD_WINE_NONBLOCKING;
819 /* if a network event is pending, signal the event object
820 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
821 before a WSAEventSelect() was done on it.
822 (when dealing with Asynchronous socket) */
823 if (sock->pmask & sock->mask) sock_wake_up( sock, pollev );
825 if (old_event) release_object( old_event ); /* we're through with it */
826 release_object( &sock->obj );
829 /* get socket event parameters */
830 DECL_HANDLER(get_socket_event)
834 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
840 set_error( WSAENOTSOCK );
843 reply->mask = sock->mask;
844 reply->pmask = sock->pmask;
845 reply->state = sock->state;
846 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
852 struct event *cevent = get_event_obj( current->process, req->c_event,
853 EVENT_MODIFY_STATE );
856 reset_event( cevent );
857 release_object( cevent );
861 sock_reselect( sock );
863 release_object( &sock->obj );
866 /* re-enable pending socket events */
867 DECL_HANDLER(enable_socket_event)
872 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
873 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &sock_ops)))
876 sock->pmask &= ~req->mask; /* is this safe? */
877 sock->hmask &= ~req->mask;
878 if ( req->mask & FD_READ )
879 sock->hmask &= ~FD_CLOSE;
880 sock->state |= req->sstate;
881 sock->state &= ~req->cstate;
882 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
884 pollev = sock_reselect( sock );
885 if ( pollev ) sock_try_event( sock, pollev );
887 release_object( &sock->obj );
890 DECL_HANDLER(set_socket_deferred)
892 struct sock *sock, *acceptsock;
894 sock=(struct sock*)get_handle_obj( current->process,req->handle,
895 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops );
898 set_error( WSAENOTSOCK );
901 acceptsock = (struct sock*)get_handle_obj( current->process,req->deferred,
902 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops );
905 release_object( sock );
906 set_error( WSAENOTSOCK );
909 sock->deferred = acceptsock;
910 release_object( sock );