Move the name directive from the .spec file to the Makefile.
[wine/gsoc_dplay.git] / server / sock.c
blobfb726118e8a3a80847a2324d7be23a339efa393e
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 #include <sys/ioctl.h>
41 #ifdef HAVE_SYS_FILIO_H
42 # include <sys/filio.h>
43 #endif
44 #include <time.h>
45 #include <unistd.h>
47 #include "winerror.h"
48 #include "winbase.h"
49 #include "process.h"
50 #include "handle.h"
51 #include "thread.h"
52 #include "request.h"
53 #include "user.h"
54 #include "async.h"
56 /* To avoid conflicts with the Unix socket headers. Plus we only need a few
57 * macros anyway.
59 #define USE_WS_PREFIX
60 #include "winsock2.h"
62 struct sock
64 struct object obj; /* object header */
65 unsigned int state; /* status bits */
66 unsigned int mask; /* event mask */
67 unsigned int hmask; /* held (blocked) events */
68 unsigned int pmask; /* pending events */
69 unsigned int flags; /* socket flags */
70 unsigned short type; /* socket type */
71 unsigned short family; /* socket family */
72 struct event *event; /* event object */
73 user_handle_t window; /* window to send the message to */
74 unsigned int message; /* message to send */
75 obj_handle_t wparam; /* message wparam (socket handle) */
76 int errors[FD_MAX_EVENTS]; /* event errors */
77 struct sock* deferred; /* socket that waits for a deferred accept */
78 struct async_queue read_q; /* Queue for asynchronous reads */
79 struct async_queue write_q; /* Queue for asynchronous writes */
82 static void sock_dump( struct object *obj, int verbose );
83 static int sock_signaled( struct object *obj, struct thread *thread );
84 static int sock_get_poll_events( struct object *obj );
85 static void sock_poll_event( struct object *obj, int event );
86 static int sock_get_fd( struct object *obj );
87 static int sock_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
88 static void sock_destroy( struct object *obj );
89 static int sock_get_error( int err );
90 static void sock_set_error(void);
91 static void sock_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count);
93 static const struct object_ops sock_ops =
95 sizeof(struct sock), /* size */
96 sock_dump, /* dump */
97 add_queue, /* add_queue */
98 remove_queue, /* remove_queue */
99 sock_signaled, /* signaled */
100 no_satisfied, /* satisfied */
101 sock_get_poll_events, /* get_poll_events */
102 sock_poll_event, /* poll_event */
103 sock_get_fd, /* get_fd */
104 no_flush, /* flush */
105 sock_get_info, /* get_file_info */
106 sock_queue_async, /* queue_async */
107 sock_destroy /* destroy */
111 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
112 * we post messages if there are multiple events. Used to send
113 * messages. The problem is if there is both a FD_CONNECT event and,
114 * say, an FD_READ event available on the same socket, we want to
115 * notify the app of the connect event first. Otherwise it may
116 * discard the read event because it thinks it hasn't connected yet.
118 static const int event_bitorder[FD_MAX_EVENTS] =
120 FD_CONNECT_BIT,
121 FD_ACCEPT_BIT,
122 FD_OOB_BIT,
123 FD_WRITE_BIT,
124 FD_READ_BIT,
125 FD_CLOSE_BIT,
126 6, 7, 8, 9 /* leftovers */
129 /* Flags that make sense only for SOCK_STREAM sockets */
130 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
132 typedef enum {
133 SOCK_SHUTDOWN_ERROR = -1,
134 SOCK_SHUTDOWN_EOF = 0,
135 SOCK_SHUTDOWN_POLLHUP = 1
136 } sock_shutdown_t;
138 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
140 static sock_shutdown_t sock_check_pollhup (void)
142 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
143 int fd[2], n;
144 struct pollfd pfd;
145 char dummy;
147 if ( socketpair ( AF_UNIX, SOCK_STREAM, 0, fd ) ) goto out;
148 if ( shutdown ( fd[0], 1 ) ) goto out;
150 pfd.fd = fd[1];
151 pfd.events = POLLIN;
152 pfd.revents = 0;
154 n = poll ( &pfd, 1, 0 );
155 if ( n != 1 ) goto out; /* error or timeout */
156 if ( pfd.revents & POLLHUP )
157 ret = SOCK_SHUTDOWN_POLLHUP;
158 else if ( pfd.revents & POLLIN &&
159 read ( fd[1], &dummy, 1 ) == 0 )
160 ret = SOCK_SHUTDOWN_EOF;
162 out:
163 close ( fd[0] );
164 close ( fd[1] );
165 return ret;
168 void sock_init(void)
170 sock_shutdown_type = sock_check_pollhup ();
172 switch ( sock_shutdown_type )
174 case SOCK_SHUTDOWN_EOF:
175 if (debug_level) fprintf ( stderr, "sock_init: shutdown() causes EOF\n" );
176 break;
177 case SOCK_SHUTDOWN_POLLHUP:
178 if (debug_level) fprintf ( stderr, "sock_init: shutdown() causes POLLHUP\n" );
179 break;
180 default:
181 fprintf ( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
182 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
186 static int sock_reselect( struct sock *sock )
188 int ev = sock_get_poll_events( &sock->obj );
190 if (debug_level)
191 fprintf(stderr,"sock_reselect(%d): new mask %x\n", sock->obj.fd, ev);
193 if (sock->obj.select == -1) {
194 /* previously unconnected socket, is this reselect supposed to connect it? */
195 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
196 /* ok, it is, attach it to the wineserver's main poll loop */
197 add_select_user( &sock->obj );
199 /* update condition mask */
200 set_select_events( &sock->obj, ev );
201 return ev;
204 /* After POLLHUP is received, the socket will no longer be in the main select loop.
205 This function is used to signal pending events nevertheless */
206 static void sock_try_event ( struct sock *sock, int event )
208 struct pollfd pfd;
210 pfd.fd = sock->obj.fd;
211 pfd.events = event;
212 pfd.revents = 0;
213 poll (&pfd, 1, 0);
215 if ( pfd.revents )
217 if ( debug_level ) fprintf ( stderr, "sock_try_event: %x\n", pfd.revents );
218 sock_poll_event ( &sock->obj, pfd.revents );
222 /* wake anybody waiting on the socket event or send the associated message */
223 static void sock_wake_up( struct sock *sock, int pollev )
225 unsigned int events = sock->pmask & sock->mask;
226 int i;
227 int async_active = 0;
229 if ( sock->flags & FD_FLAG_OVERLAPPED )
231 if( pollev & (POLLIN|POLLPRI) && IS_READY( sock->read_q ) )
233 if (debug_level) fprintf ( stderr, "activating read queue for socket %p\n", sock );
234 async_notify( sock->read_q.head, STATUS_ALERTED );
235 async_active = 1;
237 if( pollev & POLLOUT && IS_READY( sock->write_q ) )
239 if (debug_level) fprintf ( stderr, "activating write queue for socket %p\n", sock );
240 async_notify( sock->write_q.head, STATUS_ALERTED );
241 async_active = 1;
245 /* Do not signal events if there are still pending asynchronous IO requests */
246 /* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
247 if ( !events || async_active ) return;
249 if (sock->event)
251 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
252 set_event( sock->event );
254 if (sock->window)
256 if (debug_level) fprintf(stderr, "signalling events %x win %x\n", events, sock->window );
257 for (i = 0; i < FD_MAX_EVENTS; i++)
259 int event = event_bitorder[i];
260 if (sock->pmask & (1 << event))
262 unsigned int lparam = (1 << event) | (sock->errors[event] << 16);
263 post_message( sock->window, sock->message, (unsigned int)sock->wparam, lparam );
266 sock->pmask = 0;
267 sock_reselect( sock );
271 inline static int sock_error(int s)
273 unsigned int optval = 0, optlen;
275 optlen = sizeof(optval);
276 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
277 return optval ? sock_get_error(optval) : 0;
280 static void sock_poll_event( struct object *obj, int event )
282 struct sock *sock = (struct sock *)obj;
283 int hangup_seen = 0;
285 assert( sock->obj.ops == &sock_ops );
286 if (debug_level)
287 fprintf(stderr, "socket %d select event: %x\n", sock->obj.fd, event);
288 if (sock->state & FD_CONNECT)
290 /* connecting */
291 if (event & POLLOUT)
293 /* we got connected */
294 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
295 sock->state &= ~FD_CONNECT;
296 sock->pmask |= FD_CONNECT;
297 sock->errors[FD_CONNECT_BIT] = 0;
298 if (debug_level)
299 fprintf(stderr, "socket %d connection success\n", sock->obj.fd);
301 else if (event & (POLLERR|POLLHUP))
303 /* we didn't get connected? */
304 sock->state &= ~FD_CONNECT;
305 sock->pmask |= FD_CONNECT;
306 sock->errors[FD_CONNECT_BIT] = sock_error( sock->obj.fd );
307 if (debug_level)
308 fprintf(stderr, "socket %d connection failure\n", sock->obj.fd);
310 } else
311 if (sock->state & FD_WINE_LISTENING)
313 /* listening */
314 if (event & POLLIN)
316 /* incoming connection */
317 sock->pmask |= FD_ACCEPT;
318 sock->errors[FD_ACCEPT_BIT] = 0;
319 sock->hmask |= FD_ACCEPT;
321 else if (event & (POLLERR|POLLHUP))
323 /* failed incoming connection? */
324 sock->pmask |= FD_ACCEPT;
325 sock->errors[FD_ACCEPT_BIT] = sock_error( sock->obj.fd );
326 sock->hmask |= FD_ACCEPT;
328 } else
330 /* normal data flow */
331 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
333 char dummy;
334 int nr;
336 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
337 * has been closed, so we need to check for it explicitly here */
338 nr = recv( sock->obj.fd, &dummy, 1, MSG_PEEK );
339 if ( nr > 0 )
341 /* incoming data */
342 sock->pmask |= FD_READ;
343 sock->hmask |= (FD_READ|FD_CLOSE);
344 sock->errors[FD_READ_BIT] = 0;
345 if (debug_level)
346 fprintf(stderr, "socket %d is readable\n", sock->obj.fd );
348 else if ( nr == 0 )
349 hangup_seen = 1;
350 else
352 /* EAGAIN can happen if an async recv() falls between the server's poll()
353 call and the invocation of this routine */
354 if ( errno == EAGAIN )
355 event &= ~POLLIN;
356 else
358 if ( debug_level )
359 fprintf ( stderr, "recv error on socket %d: %d\n", sock->obj.fd, errno );
360 event = POLLERR;
365 else if ( sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP) )
367 hangup_seen = 1;
369 else if ( event & POLLIN ) /* POLLIN for non-stream socket */
371 sock->pmask |= FD_READ;
372 sock->hmask |= (FD_READ|FD_CLOSE);
373 sock->errors[FD_READ_BIT] = 0;
374 if (debug_level)
375 fprintf(stderr, "socket %d is readable\n", sock->obj.fd );
379 if (event & POLLOUT)
381 sock->pmask |= FD_WRITE;
382 sock->hmask |= FD_WRITE;
383 sock->errors[FD_WRITE_BIT] = 0;
384 if (debug_level)
385 fprintf(stderr, "socket %d is writable\n", sock->obj.fd);
387 if (event & POLLPRI)
389 sock->pmask |= FD_OOB;
390 sock->hmask |= FD_OOB;
391 sock->errors[FD_OOB_BIT] = 0;
392 if (debug_level)
393 fprintf(stderr, "socket %d got OOB data\n", sock->obj.fd);
395 /* According to WS2 specs, FD_CLOSE is only delivered when there is
396 no more data to be read (i.e. hangup_seen = 1) */
397 else if ( hangup_seen && (sock->state & (FD_READ|FD_WRITE) ))
399 sock->errors[FD_CLOSE_BIT] = sock_error( sock->obj.fd );
400 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
401 sock->state &= ~(FD_WINE_CONNECTED|FD_WRITE);
402 sock->pmask |= FD_CLOSE;
403 sock->hmask |= FD_CLOSE;
404 if (debug_level)
405 fprintf(stderr, "socket %d aborted by error %d, event: %x - removing from select loop\n",
406 sock->obj.fd, sock->errors[FD_CLOSE_BIT], event);
410 if ( sock->pmask & FD_CLOSE || event & (POLLERR|POLLHUP) )
412 if ( debug_level )
413 fprintf ( stderr, "removing socket %d from select loop\n", sock->obj.fd );
414 set_select_events( &sock->obj, -1 );
416 else
417 sock_reselect( sock );
419 /* wake up anyone waiting for whatever just happened */
420 if ( sock->pmask & sock->mask || sock->flags & FD_FLAG_OVERLAPPED ) sock_wake_up( sock, event );
422 /* if anyone is stupid enough to wait on the socket object itself,
423 * maybe we should wake them up too, just in case? */
424 wake_up( &sock->obj, 0 );
427 static void sock_dump( struct object *obj, int verbose )
429 struct sock *sock = (struct sock *)obj;
430 assert( obj->ops == &sock_ops );
431 printf( "Socket fd=%d, state=%x, mask=%x, pending=%x, held=%x\n",
432 sock->obj.fd, sock->state,
433 sock->mask, sock->pmask, sock->hmask );
436 static int sock_signaled( struct object *obj, struct thread *thread )
438 struct sock *sock = (struct sock *)obj;
439 assert( obj->ops == &sock_ops );
441 return check_select_events( sock->obj.fd, sock_get_poll_events( &sock->obj ) );
444 static int sock_get_poll_events( struct object *obj )
446 struct sock *sock = (struct sock *)obj;
447 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
448 int ev = 0;
450 assert( obj->ops == &sock_ops );
452 if (sock->state & FD_CONNECT)
453 /* connecting, wait for writable */
454 return POLLOUT;
455 if (sock->state & FD_WINE_LISTENING)
456 /* listening, wait for readable */
457 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
459 if (mask & (FD_READ) || (sock->flags & WSA_FLAG_OVERLAPPED && IS_READY (sock->read_q)))
460 ev |= POLLIN | POLLPRI;
461 if (mask & FD_WRITE || (sock->flags & WSA_FLAG_OVERLAPPED && IS_READY (sock->write_q)))
462 ev |= POLLOUT;
463 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
464 if ( sock->type == SOCK_STREAM && ( sock->mask & ~sock->hmask & FD_CLOSE) )
465 ev |= POLLIN;
467 return ev;
470 static int sock_get_fd( struct object *obj )
472 struct sock *sock = (struct sock *)obj;
473 assert( obj->ops == &sock_ops );
474 return sock->obj.fd;
477 static int sock_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
479 struct sock *sock = (struct sock*) obj;
480 assert ( obj->ops == &sock_ops );
482 if (reply)
484 reply->type = FILE_TYPE_PIPE;
485 reply->attr = 0;
486 reply->access_time = 0;
487 reply->write_time = 0;
488 reply->size_high = 0;
489 reply->size_low = 0;
490 reply->links = 0;
491 reply->index_high = 0;
492 reply->index_low = 0;
493 reply->serial = 0;
495 *flags = 0;
496 if (sock->flags & WSA_FLAG_OVERLAPPED) *flags |= FD_FLAG_OVERLAPPED;
497 if ( !(sock->state & FD_READ ) ) *flags |= FD_FLAG_RECV_SHUTDOWN;
498 if ( !(sock->state & FD_WRITE ) ) *flags |= FD_FLAG_SEND_SHUTDOWN;
499 return FD_TYPE_SOCKET;
502 static void sock_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count)
504 struct sock *sock = (struct sock *)obj;
505 struct async_queue *q;
506 struct async *async;
507 int pollev;
509 assert( obj->ops == &sock_ops );
511 if ( !(sock->flags & WSA_FLAG_OVERLAPPED) )
513 set_error ( STATUS_INVALID_HANDLE );
514 return;
517 switch( type )
519 case ASYNC_TYPE_READ:
520 q = &sock->read_q;
521 sock->hmask &= ~FD_CLOSE;
522 break;
523 case ASYNC_TYPE_WRITE:
524 q = &sock->write_q;
525 break;
526 default:
527 set_error( STATUS_INVALID_PARAMETER );
528 return;
531 async = find_async ( q, current, ptr );
533 if ( status == STATUS_PENDING )
535 if ( ( !( sock->state & FD_READ ) && type == ASYNC_TYPE_READ ) ||
536 ( !( sock->state & FD_WRITE ) && type == ASYNC_TYPE_WRITE ) )
538 set_error ( STATUS_PIPE_DISCONNECTED );
539 if ( async ) destroy_async ( async );
541 else
543 if ( !async )
544 async = create_async ( obj, current, ptr );
545 if ( !async )
546 return;
548 async->status = STATUS_PENDING;
549 if ( !async->q )
550 async_insert ( q, async );
553 else if ( async ) destroy_async ( async );
554 else set_error ( STATUS_INVALID_PARAMETER );
556 pollev = sock_reselect ( sock );
557 if ( pollev ) sock_try_event ( sock, pollev );
560 static void sock_destroy( struct object *obj )
562 struct sock *sock = (struct sock *)obj;
563 assert( obj->ops == &sock_ops );
565 /* FIXME: special socket shutdown stuff? */
567 if ( sock->deferred )
568 release_object ( sock->deferred );
570 if ( sock->flags & WSA_FLAG_OVERLAPPED )
572 destroy_async_queue ( &sock->read_q );
573 destroy_async_queue ( &sock->write_q );
575 if (sock->event) release_object( sock->event );
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) {
588 sock_set_error();
589 return NULL;
591 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
592 if (!(sock = alloc_object( &sock_ops, -1 ))) return NULL;
593 sock->obj.fd = sockfd;
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->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->flags & WSA_FLAG_OVERLAPPED)
608 init_async_queue (&sock->read_q);
609 init_async_queue (&sock->write_q);
611 sock_reselect( sock );
612 clear_error();
613 return &sock->obj;
616 /* accept a socket (creates a new fd) */
617 static struct sock *accept_socket( obj_handle_t handle )
619 struct sock *acceptsock;
620 struct sock *sock;
621 int acceptfd;
622 struct sockaddr saddr;
623 int slen;
625 sock=(struct sock*)get_handle_obj(current->process,handle,
626 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
627 if (!sock)
628 return NULL;
630 if ( sock->deferred ) {
631 acceptsock = sock->deferred;
632 sock->deferred = NULL;
633 } else {
635 /* Try to accept(2). We can't be safe that this an already connected socket
636 * or that accept() is allowed on it. In those cases we will get -1/errno
637 * return.
639 slen = sizeof(saddr);
640 acceptfd = accept(sock->obj.fd,&saddr,&slen);
641 if (acceptfd==-1) {
642 sock_set_error();
643 release_object( sock );
644 return NULL;
646 if (!(acceptsock = alloc_object( &sock_ops, -1 )))
648 release_object( sock );
649 return NULL;
652 /* newly created socket gets the same properties of the listening socket */
653 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
654 acceptsock->obj.fd = acceptfd;
655 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
656 if (sock->state & FD_WINE_NONBLOCKING)
657 acceptsock->state |= FD_WINE_NONBLOCKING;
658 acceptsock->mask = sock->mask;
659 acceptsock->hmask = 0;
660 acceptsock->pmask = 0;
661 acceptsock->type = sock->type;
662 acceptsock->family = sock->family;
663 acceptsock->event = NULL;
664 acceptsock->window = sock->window;
665 acceptsock->message = sock->message;
666 acceptsock->wparam = 0;
667 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
668 acceptsock->flags = sock->flags;
669 acceptsock->deferred = 0;
670 if ( acceptsock->flags & WSA_FLAG_OVERLAPPED )
672 init_async_queue ( &acceptsock->read_q );
673 init_async_queue ( &acceptsock->write_q );
676 clear_error();
677 sock->pmask &= ~FD_ACCEPT;
678 sock->hmask &= ~FD_ACCEPT;
679 sock_reselect( sock );
680 release_object( sock );
681 return acceptsock;
684 /* set the last error depending on errno */
685 static int sock_get_error( int err )
687 switch (err)
689 case EINTR: return WSAEINTR; break;
690 case EBADF: return WSAEBADF; break;
691 case EPERM:
692 case EACCES: return WSAEACCES; break;
693 case EFAULT: return WSAEFAULT; break;
694 case EINVAL: return WSAEINVAL; break;
695 case EMFILE: return WSAEMFILE; break;
696 case EWOULDBLOCK: return WSAEWOULDBLOCK; break;
697 case EINPROGRESS: return WSAEINPROGRESS; break;
698 case EALREADY: return WSAEALREADY; break;
699 case ENOTSOCK: return WSAENOTSOCK; break;
700 case EDESTADDRREQ: return WSAEDESTADDRREQ; break;
701 case EMSGSIZE: return WSAEMSGSIZE; break;
702 case EPROTOTYPE: return WSAEPROTOTYPE; break;
703 case ENOPROTOOPT: return WSAENOPROTOOPT; break;
704 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT; break;
705 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT; break;
706 case EOPNOTSUPP: return WSAEOPNOTSUPP; break;
707 case EPFNOSUPPORT: return WSAEPFNOSUPPORT; break;
708 case EAFNOSUPPORT: return WSAEAFNOSUPPORT; break;
709 case EADDRINUSE: return WSAEADDRINUSE; break;
710 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL; break;
711 case ENETDOWN: return WSAENETDOWN; break;
712 case ENETUNREACH: return WSAENETUNREACH; break;
713 case ENETRESET: return WSAENETRESET; break;
714 case ECONNABORTED: return WSAECONNABORTED; break;
715 case EPIPE:
716 case ECONNRESET: return WSAECONNRESET; break;
717 case ENOBUFS: return WSAENOBUFS; break;
718 case EISCONN: return WSAEISCONN; break;
719 case ENOTCONN: return WSAENOTCONN; break;
720 case ESHUTDOWN: return WSAESHUTDOWN; break;
721 case ETOOMANYREFS: return WSAETOOMANYREFS; break;
722 case ETIMEDOUT: return WSAETIMEDOUT; break;
723 case ECONNREFUSED: return WSAECONNREFUSED; break;
724 case ELOOP: return WSAELOOP; break;
725 case ENAMETOOLONG: return WSAENAMETOOLONG; break;
726 case EHOSTDOWN: return WSAEHOSTDOWN; break;
727 case EHOSTUNREACH: return WSAEHOSTUNREACH; break;
728 case ENOTEMPTY: return WSAENOTEMPTY; break;
729 #ifdef EPROCLIM
730 case EPROCLIM: return WSAEPROCLIM; break;
731 #endif
732 #ifdef EUSERS
733 case EUSERS: return WSAEUSERS; break;
734 #endif
735 #ifdef EDQUOT
736 case EDQUOT: return WSAEDQUOT; break;
737 #endif
738 #ifdef ESTALE
739 case ESTALE: return WSAESTALE; break;
740 #endif
741 #ifdef EREMOTE
742 case EREMOTE: return WSAEREMOTE; break;
743 #endif
744 default: errno=err; perror("sock_set_error"); return ERROR_UNKNOWN; break;
748 /* set the last error depending on errno */
749 static void sock_set_error(void)
751 set_error( sock_get_error( errno ) );
754 /* create a socket */
755 DECL_HANDLER(create_socket)
757 struct object *obj;
759 reply->handle = 0;
760 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
762 reply->handle = alloc_handle( current->process, obj, req->access, req->inherit );
763 release_object( obj );
767 /* accept a socket */
768 DECL_HANDLER(accept_socket)
770 struct sock *sock;
772 reply->handle = 0;
773 if ((sock = accept_socket( req->lhandle )) != NULL)
775 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->inherit );
776 sock->wparam = reply->handle; /* wparam for message is the socket handle */
777 sock_reselect( sock );
778 release_object( &sock->obj );
782 /* set socket event parameters */
783 DECL_HANDLER(set_socket_event)
785 struct sock *sock;
786 struct event *old_event;
787 int pollev;
789 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
790 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &sock_ops)))
791 return;
792 old_event = sock->event;
793 sock->mask = req->mask;
794 sock->event = NULL;
795 sock->window = req->window;
796 sock->message = req->msg;
797 sock->wparam = req->handle; /* wparam is the socket handle */
798 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
800 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
802 pollev = sock_reselect( sock );
803 if ( pollev ) sock_try_event ( sock, pollev );
805 if (sock->mask)
806 sock->state |= FD_WINE_NONBLOCKING;
808 /* if a network event is pending, signal the event object
809 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
810 before a WSAEventSelect() was done on it.
811 (when dealing with Asynchronous socket) */
812 if (sock->pmask & sock->mask) sock_wake_up( sock, pollev );
814 if (old_event) release_object( old_event ); /* we're through with it */
815 release_object( &sock->obj );
818 /* get socket event parameters */
819 DECL_HANDLER(get_socket_event)
821 struct sock *sock;
823 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
824 if (!sock)
826 reply->mask = 0;
827 reply->pmask = 0;
828 reply->state = 0;
829 set_error( WSAENOTSOCK );
830 return;
832 reply->mask = sock->mask;
833 reply->pmask = sock->pmask;
834 reply->state = sock->state;
835 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
837 if (req->service)
839 if (req->c_event)
841 struct event *cevent = get_event_obj( current->process, req->c_event,
842 EVENT_MODIFY_STATE );
843 if (cevent)
845 reset_event( cevent );
846 release_object( cevent );
849 sock->pmask = 0;
850 sock_reselect( sock );
852 release_object( &sock->obj );
855 /* re-enable pending socket events */
856 DECL_HANDLER(enable_socket_event)
858 struct sock *sock;
859 int pollev;
861 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
862 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &sock_ops)))
863 return;
865 sock->pmask &= ~req->mask; /* is this safe? */
866 sock->hmask &= ~req->mask;
867 if ( req->mask & FD_READ )
868 sock->hmask &= ~FD_CLOSE;
869 sock->state |= req->sstate;
870 sock->state &= ~req->cstate;
871 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
873 pollev = sock_reselect( sock );
874 if ( pollev ) sock_try_event ( sock, pollev );
876 release_object( &sock->obj );
879 DECL_HANDLER(set_socket_deferred)
881 struct sock *sock, *acceptsock;
883 sock=(struct sock*)get_handle_obj( current->process,req->handle,
884 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops );
885 if ( !sock )
887 set_error ( WSAENOTSOCK );
888 return;
890 acceptsock = (struct sock*)get_handle_obj( current->process,req->deferred,
891 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops );
892 if ( !acceptsock )
894 release_object ( sock );
895 set_error ( WSAENOTSOCK );
896 return;
898 sock->deferred = acceptsock;
899 release_object ( sock );