Desktop mode should only disable mode switching, not gamma ramp
[wine/multimedia.git] / server / sock.c
blob6161d23bd030685940835a99ddb1669c7451d0f3
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 <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #ifdef HAVE_SYS_ERRNO_H
33 # include <sys/errno.h>
34 #endif
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_SOCKET_H
38 # include <sys/socket.h>
39 #endif
40 #ifdef HAVE_SYS_IOCTL_H
41 #include <sys/ioctl.h>
42 #endif
43 #ifdef HAVE_SYS_FILIO_H
44 # include <sys/filio.h>
45 #endif
46 #include <time.h>
47 #include <unistd.h>
49 #include "winbase.h"
51 #include "process.h"
52 #include "file.h"
53 #include "handle.h"
54 #include "thread.h"
55 #include "request.h"
56 #include "user.h"
57 #include "async.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 async_queue read_q; /* Queue for asynchronous reads */
84 struct async_queue 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, struct get_file_info_reply *reply, int *flags );
95 static void sock_queue_async( struct fd *fd, void *ptr, unsigned int status, int type, int count );
97 static int sock_get_error( int err );
98 static void sock_set_error(void);
100 static const struct object_ops sock_ops =
102 sizeof(struct sock), /* size */
103 sock_dump, /* dump */
104 add_queue, /* add_queue */
105 remove_queue, /* remove_queue */
106 sock_signaled, /* signaled */
107 no_satisfied, /* satisfied */
108 sock_get_fd, /* get_fd */
109 sock_destroy /* destroy */
112 static const struct fd_ops sock_fd_ops =
114 sock_get_poll_events, /* get_poll_events */
115 sock_poll_event, /* poll_event */
116 no_flush, /* flush */
117 sock_get_info, /* get_file_info */
118 sock_queue_async /* queue_async */
122 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
123 * we post messages if there are multiple events. Used to send
124 * messages. The problem is if there is both a FD_CONNECT event and,
125 * say, an FD_READ event available on the same socket, we want to
126 * notify the app of the connect event first. Otherwise it may
127 * discard the read event because it thinks it hasn't connected yet.
129 static const int event_bitorder[FD_MAX_EVENTS] =
131 FD_CONNECT_BIT,
132 FD_ACCEPT_BIT,
133 FD_OOB_BIT,
134 FD_WRITE_BIT,
135 FD_READ_BIT,
136 FD_CLOSE_BIT,
137 6, 7, 8, 9 /* leftovers */
140 /* Flags that make sense only for SOCK_STREAM sockets */
141 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
143 typedef enum {
144 SOCK_SHUTDOWN_ERROR = -1,
145 SOCK_SHUTDOWN_EOF = 0,
146 SOCK_SHUTDOWN_POLLHUP = 1
147 } sock_shutdown_t;
149 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
151 static sock_shutdown_t sock_check_pollhup (void)
153 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
154 int fd[2], n;
155 struct pollfd pfd;
156 char dummy;
158 if ( socketpair ( AF_UNIX, SOCK_STREAM, 0, fd ) ) goto out;
159 if ( shutdown ( fd[0], 1 ) ) goto out;
161 pfd.fd = fd[1];
162 pfd.events = POLLIN;
163 pfd.revents = 0;
165 n = poll ( &pfd, 1, 0 );
166 if ( n != 1 ) goto out; /* error or timeout */
167 if ( pfd.revents & POLLHUP )
168 ret = SOCK_SHUTDOWN_POLLHUP;
169 else if ( pfd.revents & POLLIN &&
170 read ( fd[1], &dummy, 1 ) == 0 )
171 ret = SOCK_SHUTDOWN_EOF;
173 out:
174 close ( fd[0] );
175 close ( fd[1] );
176 return ret;
179 void sock_init(void)
181 sock_shutdown_type = sock_check_pollhup ();
183 switch ( sock_shutdown_type )
185 case SOCK_SHUTDOWN_EOF:
186 if (debug_level) fprintf ( stderr, "sock_init: shutdown() causes EOF\n" );
187 break;
188 case SOCK_SHUTDOWN_POLLHUP:
189 if (debug_level) fprintf ( stderr, "sock_init: shutdown() causes POLLHUP\n" );
190 break;
191 default:
192 fprintf ( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
193 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
197 static int sock_reselect( struct sock *sock )
199 int ev = sock_get_poll_events( sock->fd );
201 if (debug_level)
202 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
204 if (!sock->polling) /* FIXME: should find a better way to do this */
206 /* previously unconnected socket, is this reselect supposed to connect it? */
207 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
208 /* ok, it is, attach it to the wineserver's main poll loop */
209 sock->polling = 1;
211 /* update condition mask */
212 set_fd_events( sock->fd, ev );
213 return ev;
216 /* After POLLHUP is received, the socket will no longer be in the main select loop.
217 This function is used to signal pending events nevertheless */
218 static void sock_try_event ( struct sock *sock, int event )
220 event = check_fd_events( sock->fd, event );
221 if (event)
223 if ( debug_level ) fprintf ( stderr, "sock_try_event: %x\n", event );
224 sock_poll_event ( sock->fd, event );
228 /* wake anybody waiting on the socket event or send the associated message */
229 static void sock_wake_up( struct sock *sock, int pollev )
231 unsigned int events = sock->pmask & sock->mask;
232 int i;
233 int async_active = 0;
235 if ( sock->flags & FD_FLAG_OVERLAPPED )
237 if( pollev & (POLLIN|POLLPRI) && IS_READY( sock->read_q ) )
239 if (debug_level) fprintf ( stderr, "activating read queue for socket %p\n", sock );
240 async_notify( sock->read_q.head, STATUS_ALERTED );
241 async_active = 1;
243 if( pollev & POLLOUT && IS_READY( sock->write_q ) )
245 if (debug_level) fprintf ( stderr, "activating write queue for socket %p\n", sock );
246 async_notify( sock->write_q.head, STATUS_ALERTED );
247 async_active = 1;
251 /* Do not signal events if there are still pending asynchronous IO requests */
252 /* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
253 if ( !events || async_active ) return;
255 if (sock->event)
257 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
258 set_event( sock->event );
260 if (sock->window)
262 if (debug_level) fprintf(stderr, "signalling events %x win %p\n", events, sock->window );
263 for (i = 0; i < FD_MAX_EVENTS; i++)
265 int event = event_bitorder[i];
266 if (sock->pmask & (1 << event))
268 unsigned int lparam = (1 << event) | (sock->errors[event] << 16);
269 post_message( sock->window, sock->message, (unsigned int)sock->wparam, lparam );
272 sock->pmask = 0;
273 sock_reselect( sock );
277 inline static int sock_error( struct fd *fd )
279 unsigned int optval = 0, optlen;
281 optlen = sizeof(optval);
282 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
283 return optval ? sock_get_error(optval) : 0;
286 static void sock_poll_event( struct fd *fd, int event )
288 struct sock *sock = get_fd_user( fd );
289 int hangup_seen = 0;
291 assert( sock->obj.ops == &sock_ops );
292 if (debug_level)
293 fprintf(stderr, "socket %p select event: %x\n", sock, event);
294 if (sock->state & FD_CONNECT)
296 /* connecting */
297 if (event & POLLOUT)
299 /* we got connected */
300 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
301 sock->state &= ~FD_CONNECT;
302 sock->pmask |= FD_CONNECT;
303 sock->errors[FD_CONNECT_BIT] = 0;
304 if (debug_level)
305 fprintf(stderr, "socket %p connection success\n", sock);
307 else if (event & (POLLERR|POLLHUP))
309 /* we didn't get connected? */
310 sock->state &= ~FD_CONNECT;
311 sock->pmask |= FD_CONNECT;
312 sock->errors[FD_CONNECT_BIT] = sock_error( fd );
313 if (debug_level)
314 fprintf(stderr, "socket %p connection failure\n", sock);
316 } else
317 if (sock->state & FD_WINE_LISTENING)
319 /* listening */
320 if (event & POLLIN)
322 /* incoming connection */
323 sock->pmask |= FD_ACCEPT;
324 sock->errors[FD_ACCEPT_BIT] = 0;
325 sock->hmask |= FD_ACCEPT;
327 else if (event & (POLLERR|POLLHUP))
329 /* failed incoming connection? */
330 sock->pmask |= FD_ACCEPT;
331 sock->errors[FD_ACCEPT_BIT] = sock_error( fd );
332 sock->hmask |= FD_ACCEPT;
334 } else
336 /* normal data flow */
337 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
339 char dummy;
340 int nr;
342 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
343 * has been closed, so we need to check for it explicitly here */
344 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
345 if ( nr > 0 )
347 /* incoming data */
348 sock->pmask |= FD_READ;
349 sock->hmask |= (FD_READ|FD_CLOSE);
350 sock->errors[FD_READ_BIT] = 0;
351 if (debug_level)
352 fprintf(stderr, "socket %p is readable\n", sock );
354 else if ( nr == 0 )
355 hangup_seen = 1;
356 else
358 /* EAGAIN can happen if an async recv() falls between the server's poll()
359 call and the invocation of this routine */
360 if ( errno == EAGAIN )
361 event &= ~POLLIN;
362 else
364 if ( debug_level )
365 fprintf ( stderr, "recv error on socket %p: %d\n", sock, errno );
366 event = POLLERR;
371 else if ( sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP) )
373 hangup_seen = 1;
375 else if ( event & POLLIN ) /* POLLIN for non-stream socket */
377 sock->pmask |= FD_READ;
378 sock->hmask |= (FD_READ|FD_CLOSE);
379 sock->errors[FD_READ_BIT] = 0;
380 if (debug_level)
381 fprintf(stderr, "socket %p is readable\n", sock );
385 if (event & POLLOUT)
387 sock->pmask |= FD_WRITE;
388 sock->hmask |= FD_WRITE;
389 sock->errors[FD_WRITE_BIT] = 0;
390 if (debug_level)
391 fprintf(stderr, "socket %p is writable\n", sock);
393 if (event & POLLPRI)
395 sock->pmask |= FD_OOB;
396 sock->hmask |= FD_OOB;
397 sock->errors[FD_OOB_BIT] = 0;
398 if (debug_level)
399 fprintf(stderr, "socket %p got OOB data\n", sock);
401 /* According to WS2 specs, FD_CLOSE is only delivered when there is
402 no more data to be read (i.e. hangup_seen = 1) */
403 else if ( hangup_seen && (sock->state & (FD_READ|FD_WRITE) ))
405 sock->errors[FD_CLOSE_BIT] = sock_error( fd );
406 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
407 sock->state &= ~FD_WRITE;
408 sock->pmask |= FD_CLOSE;
409 sock->hmask |= FD_CLOSE;
410 if (debug_level)
411 fprintf(stderr, "socket %p aborted by error %d, event: %x - removing from select loop\n",
412 sock, sock->errors[FD_CLOSE_BIT], event);
416 if ( sock->pmask & FD_CLOSE || event & (POLLERR|POLLHUP) )
418 if ( debug_level )
419 fprintf ( stderr, "removing socket %p from select loop\n", sock );
420 set_fd_events( sock->fd, -1 );
422 else
423 sock_reselect( sock );
425 /* wake up anyone waiting for whatever just happened */
426 if ( sock->pmask & sock->mask || sock->flags & FD_FLAG_OVERLAPPED ) sock_wake_up( sock, event );
428 /* if anyone is stupid enough to wait on the socket object itself,
429 * maybe we should wake them up too, just in case? */
430 wake_up( &sock->obj, 0 );
433 static void sock_dump( struct object *obj, int verbose )
435 struct sock *sock = (struct sock *)obj;
436 assert( obj->ops == &sock_ops );
437 printf( "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
438 sock->fd, sock->state,
439 sock->mask, sock->pmask, sock->hmask );
442 static int sock_signaled( struct object *obj, struct thread *thread )
444 struct sock *sock = (struct sock *)obj;
445 assert( obj->ops == &sock_ops );
447 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
450 static int sock_get_poll_events( struct fd *fd )
452 struct sock *sock = get_fd_user( fd );
453 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
454 int ev = 0;
456 assert( sock->obj.ops == &sock_ops );
458 if (sock->state & FD_CONNECT)
459 /* connecting, wait for writable */
460 return POLLOUT;
461 if (sock->state & FD_WINE_LISTENING)
462 /* listening, wait for readable */
463 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
465 if (mask & (FD_READ) || (sock->flags & WSA_FLAG_OVERLAPPED && IS_READY (sock->read_q)))
466 ev |= POLLIN | POLLPRI;
467 if (mask & FD_WRITE || (sock->flags & WSA_FLAG_OVERLAPPED && IS_READY (sock->write_q)))
468 ev |= POLLOUT;
469 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
470 if ( sock->type == SOCK_STREAM && ( sock->mask & ~sock->hmask & FD_CLOSE) )
471 ev |= POLLIN;
473 return ev;
476 static int sock_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags )
478 struct sock *sock = get_fd_user( fd );
479 assert ( sock->obj.ops == &sock_ops );
481 if (reply)
483 reply->type = FILE_TYPE_PIPE;
484 reply->attr = 0;
485 reply->access_time = 0;
486 reply->write_time = 0;
487 reply->size_high = 0;
488 reply->size_low = 0;
489 reply->links = 0;
490 reply->index_high = 0;
491 reply->index_low = 0;
492 reply->serial = 0;
494 *flags = 0;
495 if (sock->flags & WSA_FLAG_OVERLAPPED) *flags |= FD_FLAG_OVERLAPPED;
496 if ( sock->type != SOCK_STREAM || sock->state & FD_WINE_CONNECTED )
498 if ( !(sock->state & FD_READ ) ) *flags |= FD_FLAG_RECV_SHUTDOWN;
499 if ( !(sock->state & FD_WRITE ) ) *flags |= FD_FLAG_SEND_SHUTDOWN;
501 return FD_TYPE_SOCKET;
504 static void sock_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count)
506 struct sock *sock = get_fd_user( fd );
507 struct async_queue *q;
508 struct async *async;
509 int pollev;
511 assert( sock->obj.ops == &sock_ops );
513 if ( !(sock->flags & WSA_FLAG_OVERLAPPED) )
515 set_error ( STATUS_INVALID_HANDLE );
516 return;
519 switch( type )
521 case ASYNC_TYPE_READ:
522 q = &sock->read_q;
523 sock->hmask &= ~FD_CLOSE;
524 break;
525 case ASYNC_TYPE_WRITE:
526 q = &sock->write_q;
527 break;
528 default:
529 set_error( STATUS_INVALID_PARAMETER );
530 return;
533 async = find_async ( q, current, ptr );
535 if ( status == STATUS_PENDING )
537 if ( ( !( sock->state & FD_READ ) && type == ASYNC_TYPE_READ ) ||
538 ( !( sock->state & FD_WRITE ) && type == ASYNC_TYPE_WRITE ) )
540 set_error ( STATUS_PIPE_DISCONNECTED );
541 if ( async ) destroy_async ( async );
543 else
545 if ( !async )
546 async = create_async ( &sock->obj, current, ptr );
547 if ( !async )
548 return;
550 async->status = STATUS_PENDING;
551 if ( !async->q )
552 async_insert ( q, async );
555 else if ( async ) destroy_async ( async );
556 else set_error ( STATUS_INVALID_PARAMETER );
558 pollev = sock_reselect ( sock );
559 if ( pollev ) sock_try_event ( sock, pollev );
562 static struct fd *sock_get_fd( struct object *obj )
564 struct sock *sock = (struct sock *)obj;
565 return (struct fd *)grab_object( sock->fd );
568 static void sock_destroy( struct object *obj )
570 struct sock *sock = (struct sock *)obj;
571 assert( obj->ops == &sock_ops );
573 /* FIXME: special socket shutdown stuff? */
575 if ( sock->deferred )
576 release_object ( sock->deferred );
578 if ( sock->flags & WSA_FLAG_OVERLAPPED )
580 destroy_async_queue ( &sock->read_q );
581 destroy_async_queue ( &sock->write_q );
583 if (sock->event) release_object( sock->event );
584 if (sock->fd) release_object( sock->fd );
587 /* create a new and unconnected socket */
588 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
590 struct sock *sock;
591 int sockfd;
593 sockfd = socket( family, type, protocol );
594 if (debug_level)
595 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
596 if (sockfd == -1) {
597 sock_set_error();
598 return NULL;
600 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
601 if (!(sock = alloc_object( &sock_ops )))
603 close( sockfd );
604 return NULL;
606 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
607 sock->mask = 0;
608 sock->hmask = 0;
609 sock->pmask = 0;
610 sock->polling = 0;
611 sock->flags = flags;
612 sock->type = type;
613 sock->family = family;
614 sock->event = NULL;
615 sock->window = 0;
616 sock->message = 0;
617 sock->wparam = 0;
618 sock->deferred = NULL;
619 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj )))
621 release_object( sock );
622 return NULL;
624 if (sock->flags & WSA_FLAG_OVERLAPPED)
626 init_async_queue (&sock->read_q);
627 init_async_queue (&sock->write_q);
629 sock_reselect( sock );
630 clear_error();
631 return &sock->obj;
634 /* accept a socket (creates a new fd) */
635 static struct sock *accept_socket( obj_handle_t handle )
637 struct sock *acceptsock;
638 struct sock *sock;
639 int acceptfd;
640 struct sockaddr saddr;
641 int slen;
643 sock=(struct sock*)get_handle_obj(current->process,handle,
644 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
645 if (!sock)
646 return NULL;
648 if ( sock->deferred ) {
649 acceptsock = sock->deferred;
650 sock->deferred = NULL;
651 } else {
653 /* Try to accept(2). We can't be safe that this an already connected socket
654 * or that accept() is allowed on it. In those cases we will get -1/errno
655 * return.
657 slen = sizeof(saddr);
658 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
659 if (acceptfd==-1) {
660 sock_set_error();
661 release_object( sock );
662 return NULL;
664 if (!(acceptsock = alloc_object( &sock_ops )))
666 close( acceptfd );
667 release_object( sock );
668 return NULL;
671 /* newly created socket gets the same properties of the listening socket */
672 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
673 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
674 if (sock->state & FD_WINE_NONBLOCKING)
675 acceptsock->state |= FD_WINE_NONBLOCKING;
676 acceptsock->mask = sock->mask;
677 acceptsock->hmask = 0;
678 acceptsock->pmask = 0;
679 acceptsock->polling = 0;
680 acceptsock->type = sock->type;
681 acceptsock->family = sock->family;
682 acceptsock->event = NULL;
683 acceptsock->window = sock->window;
684 acceptsock->message = sock->message;
685 acceptsock->wparam = 0;
686 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
687 acceptsock->flags = sock->flags;
688 acceptsock->deferred = 0;
689 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj )))
691 release_object( acceptsock );
692 release_object( sock );
693 return NULL;
695 if ( acceptsock->flags & WSA_FLAG_OVERLAPPED )
697 init_async_queue ( &acceptsock->read_q );
698 init_async_queue ( &acceptsock->write_q );
701 clear_error();
702 sock->pmask &= ~FD_ACCEPT;
703 sock->hmask &= ~FD_ACCEPT;
704 sock_reselect( sock );
705 release_object( sock );
706 return acceptsock;
709 /* set the last error depending on errno */
710 static int sock_get_error( int err )
712 switch (err)
714 case EINTR: return WSAEINTR; break;
715 case EBADF: return WSAEBADF; break;
716 case EPERM:
717 case EACCES: return WSAEACCES; break;
718 case EFAULT: return WSAEFAULT; break;
719 case EINVAL: return WSAEINVAL; break;
720 case EMFILE: return WSAEMFILE; break;
721 case EWOULDBLOCK: return WSAEWOULDBLOCK; break;
722 case EINPROGRESS: return WSAEINPROGRESS; break;
723 case EALREADY: return WSAEALREADY; break;
724 case ENOTSOCK: return WSAENOTSOCK; break;
725 case EDESTADDRREQ: return WSAEDESTADDRREQ; break;
726 case EMSGSIZE: return WSAEMSGSIZE; break;
727 case EPROTOTYPE: return WSAEPROTOTYPE; break;
728 case ENOPROTOOPT: return WSAENOPROTOOPT; break;
729 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT; break;
730 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT; break;
731 case EOPNOTSUPP: return WSAEOPNOTSUPP; break;
732 case EPFNOSUPPORT: return WSAEPFNOSUPPORT; break;
733 case EAFNOSUPPORT: return WSAEAFNOSUPPORT; break;
734 case EADDRINUSE: return WSAEADDRINUSE; break;
735 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL; break;
736 case ENETDOWN: return WSAENETDOWN; break;
737 case ENETUNREACH: return WSAENETUNREACH; break;
738 case ENETRESET: return WSAENETRESET; break;
739 case ECONNABORTED: return WSAECONNABORTED; break;
740 case EPIPE:
741 case ECONNRESET: return WSAECONNRESET; break;
742 case ENOBUFS: return WSAENOBUFS; break;
743 case EISCONN: return WSAEISCONN; break;
744 case ENOTCONN: return WSAENOTCONN; break;
745 case ESHUTDOWN: return WSAESHUTDOWN; break;
746 case ETOOMANYREFS: return WSAETOOMANYREFS; break;
747 case ETIMEDOUT: return WSAETIMEDOUT; break;
748 case ECONNREFUSED: return WSAECONNREFUSED; break;
749 case ELOOP: return WSAELOOP; break;
750 case ENAMETOOLONG: return WSAENAMETOOLONG; break;
751 case EHOSTDOWN: return WSAEHOSTDOWN; break;
752 case EHOSTUNREACH: return WSAEHOSTUNREACH; break;
753 case ENOTEMPTY: return WSAENOTEMPTY; break;
754 #ifdef EPROCLIM
755 case EPROCLIM: return WSAEPROCLIM; break;
756 #endif
757 #ifdef EUSERS
758 case EUSERS: return WSAEUSERS; break;
759 #endif
760 #ifdef EDQUOT
761 case EDQUOT: return WSAEDQUOT; break;
762 #endif
763 #ifdef ESTALE
764 case ESTALE: return WSAESTALE; break;
765 #endif
766 #ifdef EREMOTE
767 case EREMOTE: return WSAEREMOTE; break;
768 #endif
769 default: errno=err; perror("sock_set_error"); return ERROR_UNKNOWN; break;
773 /* set the last error depending on errno */
774 static void sock_set_error(void)
776 set_error( sock_get_error( errno ) );
779 /* create a socket */
780 DECL_HANDLER(create_socket)
782 struct object *obj;
784 reply->handle = 0;
785 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
787 reply->handle = alloc_handle( current->process, obj, req->access, req->inherit );
788 release_object( obj );
792 /* accept a socket */
793 DECL_HANDLER(accept_socket)
795 struct sock *sock;
797 reply->handle = 0;
798 if ((sock = accept_socket( req->lhandle )) != NULL)
800 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->inherit );
801 sock->wparam = reply->handle; /* wparam for message is the socket handle */
802 sock_reselect( sock );
803 release_object( &sock->obj );
807 /* set socket event parameters */
808 DECL_HANDLER(set_socket_event)
810 struct sock *sock;
811 struct event *old_event;
812 int pollev;
814 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
815 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &sock_ops)))
816 return;
817 old_event = sock->event;
818 sock->mask = req->mask;
819 sock->event = NULL;
820 sock->window = req->window;
821 sock->message = req->msg;
822 sock->wparam = req->handle; /* wparam is the socket handle */
823 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
825 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
827 pollev = sock_reselect( sock );
828 if ( pollev ) sock_try_event ( sock, pollev );
830 if (sock->mask)
831 sock->state |= FD_WINE_NONBLOCKING;
833 /* if a network event is pending, signal the event object
834 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
835 before a WSAEventSelect() was done on it.
836 (when dealing with Asynchronous socket) */
837 if (sock->pmask & sock->mask) sock_wake_up( sock, pollev );
839 if (old_event) release_object( old_event ); /* we're through with it */
840 release_object( &sock->obj );
843 /* get socket event parameters */
844 DECL_HANDLER(get_socket_event)
846 struct sock *sock;
848 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
849 if (!sock)
851 reply->mask = 0;
852 reply->pmask = 0;
853 reply->state = 0;
854 set_error( WSAENOTSOCK );
855 return;
857 reply->mask = sock->mask;
858 reply->pmask = sock->pmask;
859 reply->state = sock->state;
860 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
862 if (req->service)
864 if (req->c_event)
866 struct event *cevent = get_event_obj( current->process, req->c_event,
867 EVENT_MODIFY_STATE );
868 if (cevent)
870 reset_event( cevent );
871 release_object( cevent );
874 sock->pmask = 0;
875 sock_reselect( sock );
877 release_object( &sock->obj );
880 /* re-enable pending socket events */
881 DECL_HANDLER(enable_socket_event)
883 struct sock *sock;
884 int pollev;
886 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
887 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &sock_ops)))
888 return;
890 sock->pmask &= ~req->mask; /* is this safe? */
891 sock->hmask &= ~req->mask;
892 if ( req->mask & FD_READ )
893 sock->hmask &= ~FD_CLOSE;
894 sock->state |= req->sstate;
895 sock->state &= ~req->cstate;
896 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
898 pollev = sock_reselect( sock );
899 if ( pollev ) sock_try_event ( sock, pollev );
901 release_object( &sock->obj );
904 DECL_HANDLER(set_socket_deferred)
906 struct sock *sock, *acceptsock;
908 sock=(struct sock*)get_handle_obj( current->process,req->handle,
909 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops );
910 if ( !sock )
912 set_error ( WSAENOTSOCK );
913 return;
915 acceptsock = (struct sock*)get_handle_obj( current->process,req->deferred,
916 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops );
917 if ( !acceptsock )
919 release_object ( sock );
920 set_error ( WSAENOTSOCK );
921 return;
923 sock->deferred = acceptsock;
924 release_object ( sock );