server: Use attributes instead of inherit flag in console requests.
[wine/multimedia.git] / server / sock.c
blob6e2acbb17014fcec7a88a0ec514677782992f555
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 "ntstatus.h"
51 #define WIN32_NO_STATUS
52 #include "windef.h"
53 #include "winternl.h"
55 #include "process.h"
56 #include "file.h"
57 #include "handle.h"
58 #include "thread.h"
59 #include "request.h"
60 #include "user.h"
62 /* To avoid conflicts with the Unix socket headers. Plus we only need a few
63 * macros anyway.
65 #define USE_WS_PREFIX
66 #include "winsock2.h"
68 struct sock
70 struct object obj; /* object header */
71 struct fd *fd; /* socket file descriptor */
72 unsigned int state; /* status bits */
73 unsigned int mask; /* event mask */
74 unsigned int hmask; /* held (blocked) events */
75 unsigned int pmask; /* pending events */
76 unsigned int flags; /* socket flags */
77 int polling; /* is socket being polled? */
78 unsigned short type; /* socket type */
79 unsigned short family; /* socket family */
80 struct event *event; /* event object */
81 user_handle_t window; /* window to send the message to */
82 unsigned int message; /* message to send */
83 obj_handle_t wparam; /* message wparam (socket handle) */
84 int errors[FD_MAX_EVENTS]; /* event errors */
85 struct sock *deferred; /* socket that waits for a deferred accept */
86 struct list read_q; /* queue for asynchronous reads */
87 struct list write_q; /* queue for asynchronous writes */
90 static void sock_dump( struct object *obj, int verbose );
91 static int sock_signaled( struct object *obj, struct thread *thread );
92 static struct fd *sock_get_fd( struct object *obj );
93 static void sock_destroy( struct object *obj );
95 static int sock_get_poll_events( struct fd *fd );
96 static void sock_poll_event( struct fd *fd, int event );
97 static int sock_get_info( struct fd *fd );
98 static void sock_queue_async( struct fd *fd, void *apc, void *user, void *iosb, int type, int count );
99 static void sock_cancel_async( struct fd *fd );
101 static int sock_get_error( int err );
102 static void sock_set_error(void);
104 static const struct object_ops sock_ops =
106 sizeof(struct sock), /* size */
107 sock_dump, /* dump */
108 add_queue, /* add_queue */
109 remove_queue, /* remove_queue */
110 sock_signaled, /* signaled */
111 no_satisfied, /* satisfied */
112 no_signal, /* signal */
113 sock_get_fd, /* get_fd */
114 no_lookup_name, /* lookup_name */
115 no_close_handle, /* close_handle */
116 sock_destroy /* destroy */
119 static const struct fd_ops sock_fd_ops =
121 sock_get_poll_events, /* get_poll_events */
122 sock_poll_event, /* poll_event */
123 no_flush, /* flush */
124 sock_get_info, /* get_file_info */
125 sock_queue_async, /* queue_async */
126 sock_cancel_async /* cancel_async */
130 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
131 * we post messages if there are multiple events. Used to send
132 * messages. The problem is if there is both a FD_CONNECT event and,
133 * say, an FD_READ event available on the same socket, we want to
134 * notify the app of the connect event first. Otherwise it may
135 * discard the read event because it thinks it hasn't connected yet.
137 static const int event_bitorder[FD_MAX_EVENTS] =
139 FD_CONNECT_BIT,
140 FD_ACCEPT_BIT,
141 FD_OOB_BIT,
142 FD_WRITE_BIT,
143 FD_READ_BIT,
144 FD_CLOSE_BIT,
145 6, 7, 8, 9 /* leftovers */
148 /* Flags that make sense only for SOCK_STREAM sockets */
149 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
151 typedef enum {
152 SOCK_SHUTDOWN_ERROR = -1,
153 SOCK_SHUTDOWN_EOF = 0,
154 SOCK_SHUTDOWN_POLLHUP = 1
155 } sock_shutdown_t;
157 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
159 static sock_shutdown_t sock_check_pollhup(void)
161 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
162 int fd[2], n;
163 struct pollfd pfd;
164 char dummy;
166 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) goto out;
167 if ( shutdown( fd[0], 1 ) ) goto out;
169 pfd.fd = fd[1];
170 pfd.events = POLLIN;
171 pfd.revents = 0;
173 n = poll( &pfd, 1, 0 );
174 if ( n != 1 ) goto out; /* error or timeout */
175 if ( pfd.revents & POLLHUP )
176 ret = SOCK_SHUTDOWN_POLLHUP;
177 else if ( pfd.revents & POLLIN &&
178 read( fd[1], &dummy, 1 ) == 0 )
179 ret = SOCK_SHUTDOWN_EOF;
181 out:
182 close( fd[0] );
183 close( fd[1] );
184 return ret;
187 void sock_init(void)
189 sock_shutdown_type = sock_check_pollhup();
191 switch ( sock_shutdown_type )
193 case SOCK_SHUTDOWN_EOF:
194 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
195 break;
196 case SOCK_SHUTDOWN_POLLHUP:
197 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
198 break;
199 default:
200 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
201 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
205 static int sock_reselect( struct sock *sock )
207 int ev = sock_get_poll_events( sock->fd );
209 if (debug_level)
210 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
212 if (!sock->polling) /* FIXME: should find a better way to do this */
214 /* previously unconnected socket, is this reselect supposed to connect it? */
215 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
216 /* ok, it is, attach it to the wineserver's main poll loop */
217 sock->polling = 1;
219 /* update condition mask */
220 set_fd_events( sock->fd, ev );
221 return ev;
224 /* After POLLHUP is received, the socket will no longer be in the main select loop.
225 This function is used to signal pending events nevertheless */
226 static void sock_try_event( struct sock *sock, int event )
228 event = check_fd_events( sock->fd, event );
229 if (event)
231 if ( debug_level ) fprintf( stderr, "sock_try_event: %x\n", event );
232 sock_poll_event( sock->fd, event );
236 /* wake anybody waiting on the socket event or send the associated message */
237 static void sock_wake_up( struct sock *sock, int pollev )
239 unsigned int events = sock->pmask & sock->mask;
240 int i;
241 int async_active = 0;
243 if ( sock->flags & WSA_FLAG_OVERLAPPED )
245 if ( pollev & (POLLIN|POLLPRI) && !list_empty( &sock->read_q ))
247 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
248 async_terminate_head( &sock->read_q, STATUS_ALERTED );
249 async_active = 1;
251 if ( pollev & POLLOUT && !list_empty( &sock->write_q ))
253 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
254 async_terminate_head( &sock->write_q, STATUS_ALERTED );
255 async_active = 1;
259 /* Do not signal events if there are still pending asynchronous IO requests */
260 /* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
261 if ( !events || async_active ) return;
263 if (sock->event)
265 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
266 set_event( sock->event );
268 if (sock->window)
270 if (debug_level) fprintf(stderr, "signalling events %x win %p\n", events, sock->window );
271 for (i = 0; i < FD_MAX_EVENTS; i++)
273 int event = event_bitorder[i];
274 if (sock->pmask & (1 << event))
276 unsigned int lparam = (1 << event) | (sock->errors[event] << 16);
277 post_message( sock->window, sock->message, (unsigned int)sock->wparam, lparam );
280 sock->pmask = 0;
281 sock_reselect( sock );
285 inline static int sock_error( struct fd *fd )
287 unsigned int optval = 0, optlen;
289 optlen = sizeof(optval);
290 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
291 return optval ? sock_get_error(optval) : 0;
294 static void sock_poll_event( struct fd *fd, int event )
296 struct sock *sock = get_fd_user( fd );
297 int hangup_seen = 0;
299 assert( sock->obj.ops == &sock_ops );
300 if (debug_level)
301 fprintf(stderr, "socket %p select event: %x\n", sock, event);
302 if (sock->state & FD_CONNECT)
304 /* connecting */
305 if (event & POLLOUT)
307 /* we got connected */
308 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
309 sock->state &= ~FD_CONNECT;
310 sock->pmask |= FD_CONNECT;
311 sock->errors[FD_CONNECT_BIT] = 0;
312 if (debug_level)
313 fprintf(stderr, "socket %p connection success\n", sock);
315 else if (event & (POLLERR|POLLHUP))
317 /* we didn't get connected? */
318 sock->state &= ~FD_CONNECT;
319 sock->pmask |= FD_CONNECT;
320 sock->errors[FD_CONNECT_BIT] = sock_error( fd );
321 if (debug_level)
322 fprintf(stderr, "socket %p connection failure\n", sock);
325 else if (sock->state & FD_WINE_LISTENING)
327 /* listening */
328 if (event & POLLIN)
330 /* incoming connection */
331 sock->pmask |= FD_ACCEPT;
332 sock->errors[FD_ACCEPT_BIT] = 0;
333 sock->hmask |= FD_ACCEPT;
335 else if (event & (POLLERR|POLLHUP))
337 /* failed incoming connection? */
338 sock->pmask |= FD_ACCEPT;
339 sock->errors[FD_ACCEPT_BIT] = sock_error( fd );
340 sock->hmask |= FD_ACCEPT;
343 else
345 /* normal data flow */
346 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
348 char dummy;
349 int nr;
351 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
352 * has been closed, so we need to check for it explicitly here */
353 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
354 if ( nr > 0 )
356 /* incoming data */
357 sock->pmask |= FD_READ;
358 sock->hmask |= (FD_READ|FD_CLOSE);
359 sock->errors[FD_READ_BIT] = 0;
360 if (debug_level)
361 fprintf(stderr, "socket %p is readable\n", sock );
363 else if ( nr == 0 )
364 hangup_seen = 1;
365 else
367 /* EAGAIN can happen if an async recv() falls between the server's poll()
368 call and the invocation of this routine */
369 if ( errno == EAGAIN )
370 event &= ~POLLIN;
371 else
373 if ( debug_level )
374 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
375 event = POLLERR;
380 else if ( sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP) )
382 hangup_seen = 1;
384 else if ( event & POLLIN ) /* POLLIN for non-stream socket */
386 sock->pmask |= FD_READ;
387 sock->hmask |= (FD_READ|FD_CLOSE);
388 sock->errors[FD_READ_BIT] = 0;
389 if (debug_level)
390 fprintf(stderr, "socket %p is readable\n", sock );
394 if (event & POLLOUT)
396 sock->pmask |= FD_WRITE;
397 sock->hmask |= FD_WRITE;
398 sock->errors[FD_WRITE_BIT] = 0;
399 if (debug_level)
400 fprintf(stderr, "socket %p is writable\n", sock);
402 if (event & POLLPRI)
404 sock->pmask |= FD_OOB;
405 sock->hmask |= FD_OOB;
406 sock->errors[FD_OOB_BIT] = 0;
407 if (debug_level)
408 fprintf(stderr, "socket %p got OOB data\n", sock);
410 /* According to WS2 specs, FD_CLOSE is only delivered when there is
411 no more data to be read (i.e. hangup_seen = 1) */
412 else if ( hangup_seen && (sock->state & (FD_READ|FD_WRITE) ))
414 sock->errors[FD_CLOSE_BIT] = sock_error( fd );
415 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
416 sock->state &= ~FD_WRITE;
417 sock->pmask |= FD_CLOSE;
418 sock->hmask |= FD_CLOSE;
419 if (debug_level)
420 fprintf(stderr, "socket %p aborted by error %d, event: %x - removing from select loop\n",
421 sock, sock->errors[FD_CLOSE_BIT], event);
425 if ( sock->pmask & FD_CLOSE || event & (POLLERR|POLLHUP) )
427 if ( debug_level )
428 fprintf( stderr, "removing socket %p from select loop\n", sock );
429 set_fd_events( sock->fd, -1 );
431 else
432 sock_reselect( sock );
434 /* wake up anyone waiting for whatever just happened */
435 if ( sock->pmask & sock->mask || sock->flags & WSA_FLAG_OVERLAPPED ) sock_wake_up( sock, event );
437 /* if anyone is stupid enough to wait on the socket object itself,
438 * maybe we should wake them up too, just in case? */
439 wake_up( &sock->obj, 0 );
442 static void sock_dump( struct object *obj, int verbose )
444 struct sock *sock = (struct sock *)obj;
445 assert( obj->ops == &sock_ops );
446 printf( "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
447 sock->fd, sock->state,
448 sock->mask, sock->pmask, sock->hmask );
451 static int sock_signaled( struct object *obj, struct thread *thread )
453 struct sock *sock = (struct sock *)obj;
454 assert( obj->ops == &sock_ops );
456 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
459 static int sock_get_poll_events( struct fd *fd )
461 struct sock *sock = get_fd_user( fd );
462 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
463 int ev = 0;
465 assert( sock->obj.ops == &sock_ops );
467 if (sock->state & FD_CONNECT)
468 /* connecting, wait for writable */
469 return POLLOUT;
470 if (sock->state & FD_WINE_LISTENING)
471 /* listening, wait for readable */
472 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
474 if (mask & (FD_READ) || (sock->flags & WSA_FLAG_OVERLAPPED && !list_empty( &sock->read_q )))
475 ev |= POLLIN | POLLPRI;
476 if (mask & FD_WRITE || (sock->flags & WSA_FLAG_OVERLAPPED && !list_empty( &sock->write_q )))
477 ev |= POLLOUT;
478 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
479 if ( sock->type == SOCK_STREAM && ( sock->mask & ~sock->hmask & FD_CLOSE) )
480 ev |= POLLIN;
482 return ev;
485 static int sock_get_info( struct fd *fd )
487 int flags = FD_FLAG_AVAILABLE;
488 struct sock *sock = get_fd_user( fd );
489 assert( sock->obj.ops == &sock_ops );
491 if (sock->flags & WSA_FLAG_OVERLAPPED) flags |= FD_FLAG_OVERLAPPED;
492 if ( sock->type != SOCK_STREAM || sock->state & FD_WINE_CONNECTED )
494 if ( !(sock->state & FD_READ ) ) flags |= FD_FLAG_RECV_SHUTDOWN;
495 if ( !(sock->state & FD_WRITE ) ) flags |= FD_FLAG_SEND_SHUTDOWN;
497 return flags;
500 static void sock_queue_async( struct fd *fd, void *apc, void *user, void *iosb,
501 int type, int count )
503 struct sock *sock = get_fd_user( fd );
504 struct list *queue;
505 int pollev;
507 assert( sock->obj.ops == &sock_ops );
509 if ( !(sock->flags & WSA_FLAG_OVERLAPPED) )
511 set_error( STATUS_INVALID_HANDLE );
512 return;
515 switch (type)
517 case ASYNC_TYPE_READ:
518 queue = &sock->read_q;
519 sock->hmask &= ~FD_CLOSE;
520 break;
521 case ASYNC_TYPE_WRITE:
522 queue = &sock->write_q;
523 break;
524 default:
525 set_error( STATUS_INVALID_PARAMETER );
526 return;
529 if ( ( !( sock->state & FD_READ ) && type == ASYNC_TYPE_READ ) ||
530 ( !( sock->state & FD_WRITE ) && type == ASYNC_TYPE_WRITE ) )
532 set_error( STATUS_PIPE_DISCONNECTED );
534 else
536 if (!create_async( current, NULL, queue, apc, user, iosb ))
537 return;
540 pollev = sock_reselect( sock );
541 if ( pollev ) sock_try_event( sock, pollev );
544 static void sock_cancel_async( struct fd *fd )
546 struct sock *sock = get_fd_user( fd );
547 assert( sock->obj.ops == &sock_ops );
549 async_terminate_queue( &sock->read_q, STATUS_CANCELLED );
550 async_terminate_queue( &sock->write_q, STATUS_CANCELLED );
553 static struct fd *sock_get_fd( struct object *obj )
555 struct sock *sock = (struct sock *)obj;
556 return (struct fd *)grab_object( sock->fd );
559 static void sock_destroy( struct object *obj )
561 struct sock *sock = (struct sock *)obj;
562 assert( obj->ops == &sock_ops );
564 /* FIXME: special socket shutdown stuff? */
566 if ( sock->deferred )
567 release_object( sock->deferred );
569 if ( sock->flags & WSA_FLAG_OVERLAPPED )
571 async_terminate_queue( &sock->read_q, STATUS_CANCELLED );
572 async_terminate_queue( &sock->write_q, STATUS_CANCELLED );
574 if (sock->event) release_object( sock->event );
575 if (sock->fd) release_object( sock->fd );
578 /* create a new and unconnected socket */
579 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
581 struct sock *sock;
582 int sockfd;
584 sockfd = socket( family, type, protocol );
585 if (debug_level)
586 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
587 if (sockfd == -1)
589 sock_set_error();
590 return NULL;
592 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
593 if (!(sock = alloc_object( &sock_ops )))
595 close( sockfd );
596 return NULL;
598 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
599 sock->mask = 0;
600 sock->hmask = 0;
601 sock->pmask = 0;
602 sock->polling = 0;
603 sock->flags = flags;
604 sock->type = type;
605 sock->family = family;
606 sock->event = NULL;
607 sock->window = 0;
608 sock->message = 0;
609 sock->wparam = 0;
610 sock->deferred = NULL;
611 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj )))
613 release_object( sock );
614 return NULL;
616 list_init( &sock->read_q );
617 list_init( &sock->write_q );
618 sock_reselect( sock );
619 clear_error();
620 return &sock->obj;
623 /* accept a socket (creates a new fd) */
624 static struct sock *accept_socket( obj_handle_t handle )
626 struct sock *acceptsock;
627 struct sock *sock;
628 int acceptfd;
629 struct sockaddr saddr;
631 sock=(struct sock*)get_handle_obj(current->process,handle,
632 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
633 if (!sock)
634 return NULL;
636 if ( sock->deferred )
638 acceptsock = sock->deferred;
639 sock->deferred = NULL;
641 else
644 /* Try to accept(2). We can't be safe that this an already connected socket
645 * or that accept() is allowed on it. In those cases we will get -1/errno
646 * return.
648 unsigned int slen = sizeof(saddr);
649 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
650 if (acceptfd==-1)
652 sock_set_error();
653 release_object( sock );
654 return NULL;
656 if (!(acceptsock = alloc_object( &sock_ops )))
658 close( acceptfd );
659 release_object( sock );
660 return NULL;
663 /* newly created socket gets the same properties of the listening socket */
664 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
665 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
666 if (sock->state & FD_WINE_NONBLOCKING)
667 acceptsock->state |= FD_WINE_NONBLOCKING;
668 acceptsock->mask = sock->mask;
669 acceptsock->hmask = 0;
670 acceptsock->pmask = 0;
671 acceptsock->polling = 0;
672 acceptsock->type = sock->type;
673 acceptsock->family = sock->family;
674 acceptsock->event = NULL;
675 acceptsock->window = sock->window;
676 acceptsock->message = sock->message;
677 acceptsock->wparam = 0;
678 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
679 acceptsock->flags = sock->flags;
680 acceptsock->deferred = NULL;
681 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj )))
683 release_object( acceptsock );
684 release_object( sock );
685 return NULL;
687 list_init( &acceptsock->read_q );
688 list_init( &acceptsock->write_q );
690 clear_error();
691 sock->pmask &= ~FD_ACCEPT;
692 sock->hmask &= ~FD_ACCEPT;
693 sock_reselect( sock );
694 release_object( sock );
695 return acceptsock;
698 /* set the last error depending on errno */
699 static int sock_get_error( int err )
701 switch (err)
703 case EINTR: return WSAEINTR;
704 case EBADF: return WSAEBADF;
705 case EPERM:
706 case EACCES: return WSAEACCES;
707 case EFAULT: return WSAEFAULT;
708 case EINVAL: return WSAEINVAL;
709 case EMFILE: return WSAEMFILE;
710 case EWOULDBLOCK: return WSAEWOULDBLOCK;
711 case EINPROGRESS: return WSAEINPROGRESS;
712 case EALREADY: return WSAEALREADY;
713 case ENOTSOCK: return WSAENOTSOCK;
714 case EDESTADDRREQ: return WSAEDESTADDRREQ;
715 case EMSGSIZE: return WSAEMSGSIZE;
716 case EPROTOTYPE: return WSAEPROTOTYPE;
717 case ENOPROTOOPT: return WSAENOPROTOOPT;
718 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
719 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
720 case EOPNOTSUPP: return WSAEOPNOTSUPP;
721 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
722 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
723 case EADDRINUSE: return WSAEADDRINUSE;
724 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
725 case ENETDOWN: return WSAENETDOWN;
726 case ENETUNREACH: return WSAENETUNREACH;
727 case ENETRESET: return WSAENETRESET;
728 case ECONNABORTED: return WSAECONNABORTED;
729 case EPIPE:
730 case ECONNRESET: return WSAECONNRESET;
731 case ENOBUFS: return WSAENOBUFS;
732 case EISCONN: return WSAEISCONN;
733 case ENOTCONN: return WSAENOTCONN;
734 case ESHUTDOWN: return WSAESHUTDOWN;
735 case ETOOMANYREFS: return WSAETOOMANYREFS;
736 case ETIMEDOUT: return WSAETIMEDOUT;
737 case ECONNREFUSED: return WSAECONNREFUSED;
738 case ELOOP: return WSAELOOP;
739 case ENAMETOOLONG: return WSAENAMETOOLONG;
740 case EHOSTDOWN: return WSAEHOSTDOWN;
741 case EHOSTUNREACH: return WSAEHOSTUNREACH;
742 case ENOTEMPTY: return WSAENOTEMPTY;
743 #ifdef EPROCLIM
744 case EPROCLIM: return WSAEPROCLIM;
745 #endif
746 #ifdef EUSERS
747 case EUSERS: return WSAEUSERS;
748 #endif
749 #ifdef EDQUOT
750 case EDQUOT: return WSAEDQUOT;
751 #endif
752 #ifdef ESTALE
753 case ESTALE: return WSAESTALE;
754 #endif
755 #ifdef EREMOTE
756 case EREMOTE: return WSAEREMOTE;
757 #endif
758 default: errno=err; perror("sock_set_error"); return WSAEFAULT;
762 /* set the last error depending on errno */
763 static void sock_set_error(void)
765 set_error( sock_get_error( errno ) );
768 /* create a socket */
769 DECL_HANDLER(create_socket)
771 struct object *obj;
773 reply->handle = 0;
774 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
776 reply->handle = alloc_handle( current->process, obj, req->access,
777 req->attributes & OBJ_INHERIT );
778 release_object( obj );
782 /* accept a socket */
783 DECL_HANDLER(accept_socket)
785 struct sock *sock;
787 reply->handle = 0;
788 if ((sock = accept_socket( req->lhandle )) != NULL)
790 reply->handle = alloc_handle( current->process, &sock->obj, req->access,
791 req->attributes & OBJ_INHERIT );
792 sock->wparam = reply->handle; /* wparam for message is the socket handle */
793 sock_reselect( sock );
794 release_object( &sock->obj );
798 /* set socket event parameters */
799 DECL_HANDLER(set_socket_event)
801 struct sock *sock;
802 struct event *old_event;
803 int pollev;
805 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
806 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &sock_ops)))
807 return;
808 old_event = sock->event;
809 sock->mask = req->mask;
810 sock->event = NULL;
811 sock->window = req->window;
812 sock->message = req->msg;
813 sock->wparam = req->handle; /* wparam is the socket handle */
814 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
816 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
818 pollev = sock_reselect( sock );
819 if ( pollev ) sock_try_event( sock, pollev );
821 if (sock->mask)
822 sock->state |= FD_WINE_NONBLOCKING;
824 /* if a network event is pending, signal the event object
825 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
826 before a WSAEventSelect() was done on it.
827 (when dealing with Asynchronous socket) */
828 if (sock->pmask & sock->mask) sock_wake_up( sock, pollev );
830 if (old_event) release_object( old_event ); /* we're through with it */
831 release_object( &sock->obj );
834 /* get socket event parameters */
835 DECL_HANDLER(get_socket_event)
837 struct sock *sock;
839 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
840 if (!sock)
842 reply->mask = 0;
843 reply->pmask = 0;
844 reply->state = 0;
845 set_error( WSAENOTSOCK );
846 return;
848 reply->mask = sock->mask;
849 reply->pmask = sock->pmask;
850 reply->state = sock->state;
851 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
853 if (req->service)
855 if (req->c_event)
857 struct event *cevent = get_event_obj( current->process, req->c_event,
858 EVENT_MODIFY_STATE );
859 if (cevent)
861 reset_event( cevent );
862 release_object( cevent );
865 sock->pmask = 0;
866 sock_reselect( sock );
868 release_object( &sock->obj );
871 /* re-enable pending socket events */
872 DECL_HANDLER(enable_socket_event)
874 struct sock *sock;
875 int pollev;
877 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
878 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &sock_ops)))
879 return;
881 sock->pmask &= ~req->mask; /* is this safe? */
882 sock->hmask &= ~req->mask;
883 if ( req->mask & FD_READ )
884 sock->hmask &= ~FD_CLOSE;
885 sock->state |= req->sstate;
886 sock->state &= ~req->cstate;
887 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
889 pollev = sock_reselect( sock );
890 if ( pollev ) sock_try_event( sock, pollev );
892 release_object( &sock->obj );
895 DECL_HANDLER(set_socket_deferred)
897 struct sock *sock, *acceptsock;
899 sock=(struct sock*)get_handle_obj( current->process,req->handle,
900 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops );
901 if ( !sock )
903 set_error( WSAENOTSOCK );
904 return;
906 acceptsock = (struct sock*)get_handle_obj( current->process,req->deferred,
907 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops );
908 if ( !acceptsock )
910 release_object( sock );
911 set_error( WSAENOTSOCK );
912 return;
914 sock->deferred = acceptsock;
915 release_object( sock );