Define RPC_FC_RANGE.
[wine.git] / server / sock.c
blob0a9b4225d2dd57407e661587636f10627c165c1e
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"
52 #include "process.h"
53 #include "file.h"
54 #include "handle.h"
55 #include "thread.h"
56 #include "request.h"
57 #include "user.h"
59 /* To avoid conflicts with the Unix socket headers. Plus we only need a few
60 * macros anyway.
62 #define USE_WS_PREFIX
63 #include "winsock2.h"
65 struct sock
67 struct object obj; /* object header */
68 struct fd *fd; /* socket file descriptor */
69 unsigned int state; /* status bits */
70 unsigned int mask; /* event mask */
71 unsigned int hmask; /* held (blocked) events */
72 unsigned int pmask; /* pending events */
73 unsigned int flags; /* socket flags */
74 int polling; /* is socket being polled? */
75 unsigned short type; /* socket type */
76 unsigned short family; /* socket family */
77 struct event *event; /* event object */
78 user_handle_t window; /* window to send the message to */
79 unsigned int message; /* message to send */
80 obj_handle_t wparam; /* message wparam (socket handle) */
81 int errors[FD_MAX_EVENTS]; /* event errors */
82 struct sock *deferred; /* socket that waits for a deferred accept */
83 struct list read_q; /* queue for asynchronous reads */
84 struct list write_q; /* queue for asynchronous writes */
87 static void sock_dump( struct object *obj, int verbose );
88 static int sock_signaled( struct object *obj, struct thread *thread );
89 static struct fd *sock_get_fd( struct object *obj );
90 static void sock_destroy( struct object *obj );
92 static int sock_get_poll_events( struct fd *fd );
93 static void sock_poll_event( struct fd *fd, int event );
94 static int sock_get_info( struct fd *fd );
95 static void sock_queue_async( struct fd *fd, void *apc, void *user, void *iosb, int type, int count );
96 static void sock_cancel_async( struct fd *fd );
98 static int sock_get_error( int err );
99 static void sock_set_error(void);
101 static const struct object_ops sock_ops =
103 sizeof(struct sock), /* size */
104 sock_dump, /* dump */
105 add_queue, /* add_queue */
106 remove_queue, /* remove_queue */
107 sock_signaled, /* signaled */
108 no_satisfied, /* satisfied */
109 no_signal, /* signal */
110 sock_get_fd, /* get_fd */
111 no_close_handle, /* close_handle */
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] =
135 FD_CONNECT_BIT,
136 FD_ACCEPT_BIT,
137 FD_OOB_BIT,
138 FD_WRITE_BIT,
139 FD_READ_BIT,
140 FD_CLOSE_BIT,
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))
147 typedef enum {
148 SOCK_SHUTDOWN_ERROR = -1,
149 SOCK_SHUTDOWN_EOF = 0,
150 SOCK_SHUTDOWN_POLLHUP = 1
151 } sock_shutdown_t;
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;
158 int fd[2], n;
159 struct pollfd pfd;
160 char dummy;
162 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) goto out;
163 if ( shutdown( fd[0], 1 ) ) goto out;
165 pfd.fd = fd[1];
166 pfd.events = POLLIN;
167 pfd.revents = 0;
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;
177 out:
178 close( fd[0] );
179 close( fd[1] );
180 return ret;
183 void sock_init(void)
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" );
191 break;
192 case SOCK_SHUTDOWN_POLLHUP:
193 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
194 break;
195 default:
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 );
205 if (debug_level)
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 */
213 sock->polling = 1;
215 /* update condition mask */
216 set_fd_events( sock->fd, ev );
217 return 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 );
225 if (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;
236 int i;
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 );
245 async_active = 1;
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 );
251 async_active = 1;
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;
259 if (sock->event)
261 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
262 set_event( sock->event );
264 if (sock->window)
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 );
276 sock->pmask = 0;
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 );
293 int hangup_seen = 0;
295 assert( sock->obj.ops == &sock_ops );
296 if (debug_level)
297 fprintf(stderr, "socket %p select event: %x\n", sock, event);
298 if (sock->state & FD_CONNECT)
300 /* connecting */
301 if (event & POLLOUT)
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;
308 if (debug_level)
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 );
317 if (debug_level)
318 fprintf(stderr, "socket %p connection failure\n", sock);
321 else if (sock->state & FD_WINE_LISTENING)
323 /* listening */
324 if (event & POLLIN)
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;
339 else
341 /* normal data flow */
342 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
344 char dummy;
345 int nr;
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 );
350 if ( nr > 0 )
352 /* incoming data */
353 sock->pmask |= FD_READ;
354 sock->hmask |= (FD_READ|FD_CLOSE);
355 sock->errors[FD_READ_BIT] = 0;
356 if (debug_level)
357 fprintf(stderr, "socket %p is readable\n", sock );
359 else if ( nr == 0 )
360 hangup_seen = 1;
361 else
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 )
366 event &= ~POLLIN;
367 else
369 if ( debug_level )
370 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
371 event = POLLERR;
376 else if ( sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP) )
378 hangup_seen = 1;
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;
385 if (debug_level)
386 fprintf(stderr, "socket %p is readable\n", sock );
390 if (event & POLLOUT)
392 sock->pmask |= FD_WRITE;
393 sock->hmask |= FD_WRITE;
394 sock->errors[FD_WRITE_BIT] = 0;
395 if (debug_level)
396 fprintf(stderr, "socket %p is writable\n", sock);
398 if (event & POLLPRI)
400 sock->pmask |= FD_OOB;
401 sock->hmask |= FD_OOB;
402 sock->errors[FD_OOB_BIT] = 0;
403 if (debug_level)
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;
415 if (debug_level)
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) )
423 if ( debug_level )
424 fprintf( stderr, "removing socket %p from select loop\n", sock );
425 set_fd_events( sock->fd, -1 );
427 else
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;
459 int ev = 0;
461 assert( sock->obj.ops == &sock_ops );
463 if (sock->state & FD_CONNECT)
464 /* connecting, wait for writable */
465 return POLLOUT;
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 )))
473 ev |= POLLOUT;
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) )
476 ev |= POLLIN;
478 return ev;
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;
493 return flags;
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 );
500 struct list *queue;
501 int pollev;
503 assert( sock->obj.ops == &sock_ops );
505 if ( !(sock->flags & WSA_FLAG_OVERLAPPED) )
507 set_error( STATUS_INVALID_HANDLE );
508 return;
511 switch (type)
513 case ASYNC_TYPE_READ:
514 queue = &sock->read_q;
515 sock->hmask &= ~FD_CLOSE;
516 break;
517 case ASYNC_TYPE_WRITE:
518 queue = &sock->write_q;
519 break;
520 default:
521 set_error( STATUS_INVALID_PARAMETER );
522 return;
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 );
530 else
532 if (!create_async( current, NULL, queue, apc, user, iosb ))
533 return;
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 )
577 struct sock *sock;
578 int sockfd;
580 sockfd = socket( family, type, protocol );
581 if (debug_level)
582 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
583 if (sockfd == -1)
585 sock_set_error();
586 return NULL;
588 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
589 if (!(sock = alloc_object( &sock_ops )))
591 close( sockfd );
592 return NULL;
594 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
595 sock->mask = 0;
596 sock->hmask = 0;
597 sock->pmask = 0;
598 sock->polling = 0;
599 sock->flags = flags;
600 sock->type = type;
601 sock->family = family;
602 sock->event = NULL;
603 sock->window = 0;
604 sock->message = 0;
605 sock->wparam = 0;
606 sock->deferred = NULL;
607 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj )))
609 release_object( sock );
610 return NULL;
612 list_init( &sock->read_q );
613 list_init( &sock->write_q );
614 sock_reselect( sock );
615 clear_error();
616 return &sock->obj;
619 /* accept a socket (creates a new fd) */
620 static struct sock *accept_socket( obj_handle_t handle )
622 struct sock *acceptsock;
623 struct sock *sock;
624 int acceptfd;
625 struct sockaddr saddr;
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 unsigned int 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 = NULL;
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 );