Clean-up async IO internal functions.
[wine/multimedia.git] / server / sock.c
blobf3f27fc6cd732c854c5c11965e202bf08bf77cc5
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., 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?
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 "windef.h"
51 #include "winbase.h"
53 #include "process.h"
54 #include "file.h"
55 #include "handle.h"
56 #include "thread.h"
57 #include "request.h"
58 #include "user.h"
60 /* To avoid conflicts with the Unix socket headers. Plus we only need a few
61 * macros anyway.
63 #define USE_WS_PREFIX
64 #include "winsock2.h"
66 struct sock
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 sock_get_fd, /* get_fd */
111 sock_destroy /* destroy */
114 static const struct fd_ops sock_fd_ops =
116 sock_get_poll_events, /* get_poll_events */
117 sock_poll_event, /* poll_event */
118 no_flush, /* flush */
119 sock_get_info, /* get_file_info */
120 sock_queue_async, /* queue_async */
121 sock_cancel_async /* cancel_async */
125 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
126 * we post messages if there are multiple events. Used to send
127 * messages. The problem is if there is both a FD_CONNECT event and,
128 * say, an FD_READ event available on the same socket, we want to
129 * notify the app of the connect event first. Otherwise it may
130 * discard the read event because it thinks it hasn't connected yet.
132 static const int event_bitorder[FD_MAX_EVENTS] =
134 FD_CONNECT_BIT,
135 FD_ACCEPT_BIT,
136 FD_OOB_BIT,
137 FD_WRITE_BIT,
138 FD_READ_BIT,
139 FD_CLOSE_BIT,
140 6, 7, 8, 9 /* leftovers */
143 /* Flags that make sense only for SOCK_STREAM sockets */
144 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
146 typedef enum {
147 SOCK_SHUTDOWN_ERROR = -1,
148 SOCK_SHUTDOWN_EOF = 0,
149 SOCK_SHUTDOWN_POLLHUP = 1
150 } sock_shutdown_t;
152 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
154 static sock_shutdown_t sock_check_pollhup(void)
156 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
157 int fd[2], n;
158 struct pollfd pfd;
159 char dummy;
161 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) goto out;
162 if ( shutdown( fd[0], 1 ) ) goto out;
164 pfd.fd = fd[1];
165 pfd.events = POLLIN;
166 pfd.revents = 0;
168 n = poll( &pfd, 1, 0 );
169 if ( n != 1 ) goto out; /* error or timeout */
170 if ( pfd.revents & POLLHUP )
171 ret = SOCK_SHUTDOWN_POLLHUP;
172 else if ( pfd.revents & POLLIN &&
173 read( fd[1], &dummy, 1 ) == 0 )
174 ret = SOCK_SHUTDOWN_EOF;
176 out:
177 close( fd[0] );
178 close( fd[1] );
179 return ret;
182 void sock_init(void)
184 sock_shutdown_type = sock_check_pollhup();
186 switch ( sock_shutdown_type )
188 case SOCK_SHUTDOWN_EOF:
189 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
190 break;
191 case SOCK_SHUTDOWN_POLLHUP:
192 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
193 break;
194 default:
195 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
196 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
200 static int sock_reselect( struct sock *sock )
202 int ev = sock_get_poll_events( sock->fd );
204 if (debug_level)
205 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
207 if (!sock->polling) /* FIXME: should find a better way to do this */
209 /* previously unconnected socket, is this reselect supposed to connect it? */
210 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
211 /* ok, it is, attach it to the wineserver's main poll loop */
212 sock->polling = 1;
214 /* update condition mask */
215 set_fd_events( sock->fd, ev );
216 return ev;
219 /* After POLLHUP is received, the socket will no longer be in the main select loop.
220 This function is used to signal pending events nevertheless */
221 static void sock_try_event( struct sock *sock, int event )
223 event = check_fd_events( sock->fd, event );
224 if (event)
226 if ( debug_level ) fprintf( stderr, "sock_try_event: %x\n", event );
227 sock_poll_event( sock->fd, event );
231 /* wake anybody waiting on the socket event or send the associated message */
232 static void sock_wake_up( struct sock *sock, int pollev )
234 unsigned int events = sock->pmask & sock->mask;
235 int i;
236 int async_active = 0;
238 if ( sock->flags & WSA_FLAG_OVERLAPPED )
240 if ( pollev & (POLLIN|POLLPRI) && !list_empty( &sock->read_q ))
242 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
243 async_terminate_head( &sock->read_q, STATUS_ALERTED );
244 async_active = 1;
246 if ( pollev & POLLOUT && !list_empty( &sock->write_q ))
248 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
249 async_terminate_head( &sock->write_q, STATUS_ALERTED );
250 async_active = 1;
254 /* Do not signal events if there are still pending asynchronous IO requests */
255 /* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
256 if ( !events || async_active ) return;
258 if (sock->event)
260 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
261 set_event( sock->event );
263 if (sock->window)
265 if (debug_level) fprintf(stderr, "signalling events %x win %p\n", events, sock->window );
266 for (i = 0; i < FD_MAX_EVENTS; i++)
268 int event = event_bitorder[i];
269 if (sock->pmask & (1 << event))
271 unsigned int lparam = (1 << event) | (sock->errors[event] << 16);
272 post_message( sock->window, sock->message, (unsigned int)sock->wparam, lparam );
275 sock->pmask = 0;
276 sock_reselect( sock );
280 inline static int sock_error( struct fd *fd )
282 unsigned int optval = 0, optlen;
284 optlen = sizeof(optval);
285 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
286 return optval ? sock_get_error(optval) : 0;
289 static void sock_poll_event( struct fd *fd, int event )
291 struct sock *sock = get_fd_user( fd );
292 int hangup_seen = 0;
294 assert( sock->obj.ops == &sock_ops );
295 if (debug_level)
296 fprintf(stderr, "socket %p select event: %x\n", sock, event);
297 if (sock->state & FD_CONNECT)
299 /* connecting */
300 if (event & POLLOUT)
302 /* we got connected */
303 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
304 sock->state &= ~FD_CONNECT;
305 sock->pmask |= FD_CONNECT;
306 sock->errors[FD_CONNECT_BIT] = 0;
307 if (debug_level)
308 fprintf(stderr, "socket %p connection success\n", sock);
310 else if (event & (POLLERR|POLLHUP))
312 /* we didn't get connected? */
313 sock->state &= ~FD_CONNECT;
314 sock->pmask |= FD_CONNECT;
315 sock->errors[FD_CONNECT_BIT] = sock_error( fd );
316 if (debug_level)
317 fprintf(stderr, "socket %p connection failure\n", sock);
320 else if (sock->state & FD_WINE_LISTENING)
322 /* listening */
323 if (event & POLLIN)
325 /* incoming connection */
326 sock->pmask |= FD_ACCEPT;
327 sock->errors[FD_ACCEPT_BIT] = 0;
328 sock->hmask |= FD_ACCEPT;
330 else if (event & (POLLERR|POLLHUP))
332 /* failed incoming connection? */
333 sock->pmask |= FD_ACCEPT;
334 sock->errors[FD_ACCEPT_BIT] = sock_error( fd );
335 sock->hmask |= FD_ACCEPT;
338 else
340 /* normal data flow */
341 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
343 char dummy;
344 int nr;
346 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
347 * has been closed, so we need to check for it explicitly here */
348 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
349 if ( nr > 0 )
351 /* incoming data */
352 sock->pmask |= FD_READ;
353 sock->hmask |= (FD_READ|FD_CLOSE);
354 sock->errors[FD_READ_BIT] = 0;
355 if (debug_level)
356 fprintf(stderr, "socket %p is readable\n", sock );
358 else if ( nr == 0 )
359 hangup_seen = 1;
360 else
362 /* EAGAIN can happen if an async recv() falls between the server's poll()
363 call and the invocation of this routine */
364 if ( errno == EAGAIN )
365 event &= ~POLLIN;
366 else
368 if ( debug_level )
369 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
370 event = POLLERR;
375 else if ( sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP) )
377 hangup_seen = 1;
379 else if ( event & POLLIN ) /* POLLIN for non-stream socket */
381 sock->pmask |= FD_READ;
382 sock->hmask |= (FD_READ|FD_CLOSE);
383 sock->errors[FD_READ_BIT] = 0;
384 if (debug_level)
385 fprintf(stderr, "socket %p is readable\n", sock );
389 if (event & POLLOUT)
391 sock->pmask |= FD_WRITE;
392 sock->hmask |= FD_WRITE;
393 sock->errors[FD_WRITE_BIT] = 0;
394 if (debug_level)
395 fprintf(stderr, "socket %p is writable\n", sock);
397 if (event & POLLPRI)
399 sock->pmask |= FD_OOB;
400 sock->hmask |= FD_OOB;
401 sock->errors[FD_OOB_BIT] = 0;
402 if (debug_level)
403 fprintf(stderr, "socket %p got OOB data\n", sock);
405 /* According to WS2 specs, FD_CLOSE is only delivered when there is
406 no more data to be read (i.e. hangup_seen = 1) */
407 else if ( hangup_seen && (sock->state & (FD_READ|FD_WRITE) ))
409 sock->errors[FD_CLOSE_BIT] = sock_error( fd );
410 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
411 sock->state &= ~FD_WRITE;
412 sock->pmask |= FD_CLOSE;
413 sock->hmask |= FD_CLOSE;
414 if (debug_level)
415 fprintf(stderr, "socket %p aborted by error %d, event: %x - removing from select loop\n",
416 sock, sock->errors[FD_CLOSE_BIT], event);
420 if ( sock->pmask & FD_CLOSE || event & (POLLERR|POLLHUP) )
422 if ( debug_level )
423 fprintf( stderr, "removing socket %p from select loop\n", sock );
424 set_fd_events( sock->fd, -1 );
426 else
427 sock_reselect( sock );
429 /* wake up anyone waiting for whatever just happened */
430 if ( sock->pmask & sock->mask || sock->flags & WSA_FLAG_OVERLAPPED ) sock_wake_up( sock, event );
432 /* if anyone is stupid enough to wait on the socket object itself,
433 * maybe we should wake them up too, just in case? */
434 wake_up( &sock->obj, 0 );
437 static void sock_dump( struct object *obj, int verbose )
439 struct sock *sock = (struct sock *)obj;
440 assert( obj->ops == &sock_ops );
441 printf( "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
442 sock->fd, sock->state,
443 sock->mask, sock->pmask, sock->hmask );
446 static int sock_signaled( struct object *obj, struct thread *thread )
448 struct sock *sock = (struct sock *)obj;
449 assert( obj->ops == &sock_ops );
451 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
454 static int sock_get_poll_events( struct fd *fd )
456 struct sock *sock = get_fd_user( fd );
457 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
458 int ev = 0;
460 assert( sock->obj.ops == &sock_ops );
462 if (sock->state & FD_CONNECT)
463 /* connecting, wait for writable */
464 return POLLOUT;
465 if (sock->state & FD_WINE_LISTENING)
466 /* listening, wait for readable */
467 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
469 if (mask & (FD_READ) || (sock->flags & WSA_FLAG_OVERLAPPED && !list_empty( &sock->read_q )))
470 ev |= POLLIN | POLLPRI;
471 if (mask & FD_WRITE || (sock->flags & WSA_FLAG_OVERLAPPED && !list_empty( &sock->write_q )))
472 ev |= POLLOUT;
473 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
474 if ( sock->type == SOCK_STREAM && ( sock->mask & ~sock->hmask & FD_CLOSE) )
475 ev |= POLLIN;
477 return ev;
480 static int sock_get_info( struct fd *fd )
482 int flags = FD_FLAG_AVAILABLE;
483 struct sock *sock = get_fd_user( fd );
484 assert( sock->obj.ops == &sock_ops );
486 if (sock->flags & WSA_FLAG_OVERLAPPED) flags |= FD_FLAG_OVERLAPPED;
487 if ( sock->type != SOCK_STREAM || sock->state & FD_WINE_CONNECTED )
489 if ( !(sock->state & FD_READ ) ) flags |= FD_FLAG_RECV_SHUTDOWN;
490 if ( !(sock->state & FD_WRITE ) ) flags |= FD_FLAG_SEND_SHUTDOWN;
492 return flags;
495 static void sock_queue_async( struct fd *fd, void *apc, void *user, void *iosb,
496 int type, int count )
498 struct sock *sock = get_fd_user( fd );
499 struct list *queue;
500 int pollev;
502 assert( sock->obj.ops == &sock_ops );
504 if ( !(sock->flags & WSA_FLAG_OVERLAPPED) )
506 set_error( STATUS_INVALID_HANDLE );
507 return;
510 switch (type)
512 case ASYNC_TYPE_READ:
513 queue = &sock->read_q;
514 sock->hmask &= ~FD_CLOSE;
515 break;
516 case ASYNC_TYPE_WRITE:
517 queue = &sock->write_q;
518 break;
519 default:
520 set_error( STATUS_INVALID_PARAMETER );
521 return;
524 if ( ( !( sock->state & FD_READ ) && type == ASYNC_TYPE_READ ) ||
525 ( !( sock->state & FD_WRITE ) && type == ASYNC_TYPE_WRITE ) )
527 set_error( STATUS_PIPE_DISCONNECTED );
529 else
531 if (!create_async( current, 0, queue, apc, user, iosb ))
532 return;
535 pollev = sock_reselect( sock );
536 if ( pollev ) sock_try_event( sock, pollev );
539 static void sock_cancel_async( struct fd *fd )
541 struct sock *sock = get_fd_user( fd );
542 assert( sock->obj.ops == &sock_ops );
544 async_terminate_queue( &sock->read_q, STATUS_CANCELLED );
545 async_terminate_queue( &sock->write_q, STATUS_CANCELLED );
548 static struct fd *sock_get_fd( struct object *obj )
550 struct sock *sock = (struct sock *)obj;
551 return (struct fd *)grab_object( sock->fd );
554 static void sock_destroy( struct object *obj )
556 struct sock *sock = (struct sock *)obj;
557 assert( obj->ops == &sock_ops );
559 /* FIXME: special socket shutdown stuff? */
561 if ( sock->deferred )
562 release_object( sock->deferred );
564 if ( sock->flags & WSA_FLAG_OVERLAPPED )
566 async_terminate_queue( &sock->read_q, STATUS_CANCELLED );
567 async_terminate_queue( &sock->write_q, STATUS_CANCELLED );
569 if (sock->event) release_object( sock->event );
570 if (sock->fd) release_object( sock->fd );
573 /* create a new and unconnected socket */
574 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
576 struct sock *sock;
577 int sockfd;
579 sockfd = socket( family, type, protocol );
580 if (debug_level)
581 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
582 if (sockfd == -1)
584 sock_set_error();
585 return NULL;
587 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
588 if (!(sock = alloc_object( &sock_ops )))
590 close( sockfd );
591 return NULL;
593 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
594 sock->mask = 0;
595 sock->hmask = 0;
596 sock->pmask = 0;
597 sock->polling = 0;
598 sock->flags = flags;
599 sock->type = type;
600 sock->family = family;
601 sock->event = NULL;
602 sock->window = 0;
603 sock->message = 0;
604 sock->wparam = 0;
605 sock->deferred = NULL;
606 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj )))
608 release_object( sock );
609 return NULL;
611 list_init( &sock->read_q );
612 list_init( &sock->write_q );
613 sock_reselect( sock );
614 clear_error();
615 return &sock->obj;
618 /* accept a socket (creates a new fd) */
619 static struct sock *accept_socket( obj_handle_t handle )
621 struct sock *acceptsock;
622 struct sock *sock;
623 int acceptfd;
624 struct sockaddr saddr;
625 int slen;
627 sock=(struct sock*)get_handle_obj(current->process,handle,
628 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
629 if (!sock)
630 return NULL;
632 if ( sock->deferred )
634 acceptsock = sock->deferred;
635 sock->deferred = NULL;
637 else
640 /* Try to accept(2). We can't be safe that this an already connected socket
641 * or that accept() is allowed on it. In those cases we will get -1/errno
642 * return.
644 slen = sizeof(saddr);
645 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
646 if (acceptfd==-1)
648 sock_set_error();
649 release_object( sock );
650 return NULL;
652 if (!(acceptsock = alloc_object( &sock_ops )))
654 close( acceptfd );
655 release_object( sock );
656 return NULL;
659 /* newly created socket gets the same properties of the listening socket */
660 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
661 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
662 if (sock->state & FD_WINE_NONBLOCKING)
663 acceptsock->state |= FD_WINE_NONBLOCKING;
664 acceptsock->mask = sock->mask;
665 acceptsock->hmask = 0;
666 acceptsock->pmask = 0;
667 acceptsock->polling = 0;
668 acceptsock->type = sock->type;
669 acceptsock->family = sock->family;
670 acceptsock->event = NULL;
671 acceptsock->window = sock->window;
672 acceptsock->message = sock->message;
673 acceptsock->wparam = 0;
674 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
675 acceptsock->flags = sock->flags;
676 acceptsock->deferred = 0;
677 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj )))
679 release_object( acceptsock );
680 release_object( sock );
681 return NULL;
683 list_init( &acceptsock->read_q );
684 list_init( &acceptsock->write_q );
686 clear_error();
687 sock->pmask &= ~FD_ACCEPT;
688 sock->hmask &= ~FD_ACCEPT;
689 sock_reselect( sock );
690 release_object( sock );
691 return acceptsock;
694 /* set the last error depending on errno */
695 static int sock_get_error( int err )
697 switch (err)
699 case EINTR: return WSAEINTR;
700 case EBADF: return WSAEBADF;
701 case EPERM:
702 case EACCES: return WSAEACCES;
703 case EFAULT: return WSAEFAULT;
704 case EINVAL: return WSAEINVAL;
705 case EMFILE: return WSAEMFILE;
706 case EWOULDBLOCK: return WSAEWOULDBLOCK;
707 case EINPROGRESS: return WSAEINPROGRESS;
708 case EALREADY: return WSAEALREADY;
709 case ENOTSOCK: return WSAENOTSOCK;
710 case EDESTADDRREQ: return WSAEDESTADDRREQ;
711 case EMSGSIZE: return WSAEMSGSIZE;
712 case EPROTOTYPE: return WSAEPROTOTYPE;
713 case ENOPROTOOPT: return WSAENOPROTOOPT;
714 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
715 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
716 case EOPNOTSUPP: return WSAEOPNOTSUPP;
717 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
718 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
719 case EADDRINUSE: return WSAEADDRINUSE;
720 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
721 case ENETDOWN: return WSAENETDOWN;
722 case ENETUNREACH: return WSAENETUNREACH;
723 case ENETRESET: return WSAENETRESET;
724 case ECONNABORTED: return WSAECONNABORTED;
725 case EPIPE:
726 case ECONNRESET: return WSAECONNRESET;
727 case ENOBUFS: return WSAENOBUFS;
728 case EISCONN: return WSAEISCONN;
729 case ENOTCONN: return WSAENOTCONN;
730 case ESHUTDOWN: return WSAESHUTDOWN;
731 case ETOOMANYREFS: return WSAETOOMANYREFS;
732 case ETIMEDOUT: return WSAETIMEDOUT;
733 case ECONNREFUSED: return WSAECONNREFUSED;
734 case ELOOP: return WSAELOOP;
735 case ENAMETOOLONG: return WSAENAMETOOLONG;
736 case EHOSTDOWN: return WSAEHOSTDOWN;
737 case EHOSTUNREACH: return WSAEHOSTUNREACH;
738 case ENOTEMPTY: return WSAENOTEMPTY;
739 #ifdef EPROCLIM
740 case EPROCLIM: return WSAEPROCLIM;
741 #endif
742 #ifdef EUSERS
743 case EUSERS: return WSAEUSERS;
744 #endif
745 #ifdef EDQUOT
746 case EDQUOT: return WSAEDQUOT;
747 #endif
748 #ifdef ESTALE
749 case ESTALE: return WSAESTALE;
750 #endif
751 #ifdef EREMOTE
752 case EREMOTE: return WSAEREMOTE;
753 #endif
754 default: errno=err; perror("sock_set_error"); return WSAEFAULT;
758 /* set the last error depending on errno */
759 static void sock_set_error(void)
761 set_error( sock_get_error( errno ) );
764 /* create a socket */
765 DECL_HANDLER(create_socket)
767 struct object *obj;
769 reply->handle = 0;
770 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
772 reply->handle = alloc_handle( current->process, obj, req->access, req->inherit );
773 release_object( obj );
777 /* accept a socket */
778 DECL_HANDLER(accept_socket)
780 struct sock *sock;
782 reply->handle = 0;
783 if ((sock = accept_socket( req->lhandle )) != NULL)
785 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->inherit );
786 sock->wparam = reply->handle; /* wparam for message is the socket handle */
787 sock_reselect( sock );
788 release_object( &sock->obj );
792 /* set socket event parameters */
793 DECL_HANDLER(set_socket_event)
795 struct sock *sock;
796 struct event *old_event;
797 int pollev;
799 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
800 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &sock_ops)))
801 return;
802 old_event = sock->event;
803 sock->mask = req->mask;
804 sock->event = NULL;
805 sock->window = req->window;
806 sock->message = req->msg;
807 sock->wparam = req->handle; /* wparam is the socket handle */
808 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
810 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
812 pollev = sock_reselect( sock );
813 if ( pollev ) sock_try_event( sock, pollev );
815 if (sock->mask)
816 sock->state |= FD_WINE_NONBLOCKING;
818 /* if a network event is pending, signal the event object
819 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
820 before a WSAEventSelect() was done on it.
821 (when dealing with Asynchronous socket) */
822 if (sock->pmask & sock->mask) sock_wake_up( sock, pollev );
824 if (old_event) release_object( old_event ); /* we're through with it */
825 release_object( &sock->obj );
828 /* get socket event parameters */
829 DECL_HANDLER(get_socket_event)
831 struct sock *sock;
833 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
834 if (!sock)
836 reply->mask = 0;
837 reply->pmask = 0;
838 reply->state = 0;
839 set_error( WSAENOTSOCK );
840 return;
842 reply->mask = sock->mask;
843 reply->pmask = sock->pmask;
844 reply->state = sock->state;
845 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
847 if (req->service)
849 if (req->c_event)
851 struct event *cevent = get_event_obj( current->process, req->c_event,
852 EVENT_MODIFY_STATE );
853 if (cevent)
855 reset_event( cevent );
856 release_object( cevent );
859 sock->pmask = 0;
860 sock_reselect( sock );
862 release_object( &sock->obj );
865 /* re-enable pending socket events */
866 DECL_HANDLER(enable_socket_event)
868 struct sock *sock;
869 int pollev;
871 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
872 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &sock_ops)))
873 return;
875 sock->pmask &= ~req->mask; /* is this safe? */
876 sock->hmask &= ~req->mask;
877 if ( req->mask & FD_READ )
878 sock->hmask &= ~FD_CLOSE;
879 sock->state |= req->sstate;
880 sock->state &= ~req->cstate;
881 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
883 pollev = sock_reselect( sock );
884 if ( pollev ) sock_try_event( sock, pollev );
886 release_object( &sock->obj );
889 DECL_HANDLER(set_socket_deferred)
891 struct sock *sock, *acceptsock;
893 sock=(struct sock*)get_handle_obj( current->process,req->handle,
894 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops );
895 if ( !sock )
897 set_error( WSAENOTSOCK );
898 return;
900 acceptsock = (struct sock*)get_handle_obj( current->process,req->deferred,
901 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops );
902 if ( !acceptsock )
904 release_object( sock );
905 set_error( WSAENOTSOCK );
906 return;
908 sock->deferred = acceptsock;
909 release_object( sock );