wordpad: Move printing functions to a separate file.
[wine.git] / server / sock.c
blob66ab0d116f95fd6afacf098971d8fe5ed648b0b6
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 async_queue *read_q; /* queue for asynchronous reads */
87 struct async_queue *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 enum server_fd_type sock_get_fd_type( struct fd *fd );
98 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
99 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
100 static void sock_cancel_async( struct fd *fd );
102 static int sock_get_error( int err );
103 static void sock_set_error(void);
105 static const struct object_ops sock_ops =
107 sizeof(struct sock), /* size */
108 sock_dump, /* dump */
109 add_queue, /* add_queue */
110 remove_queue, /* remove_queue */
111 sock_signaled, /* signaled */
112 no_satisfied, /* satisfied */
113 no_signal, /* signal */
114 sock_get_fd, /* get_fd */
115 default_fd_map_access, /* map_access */
116 default_get_sd, /* get_sd */
117 default_set_sd, /* set_sd */
118 no_lookup_name, /* lookup_name */
119 no_open_file, /* open_file */
120 fd_close_handle, /* close_handle */
121 sock_destroy /* destroy */
124 static const struct fd_ops sock_fd_ops =
126 sock_get_poll_events, /* get_poll_events */
127 sock_poll_event, /* poll_event */
128 no_flush, /* flush */
129 sock_get_fd_type, /* get_file_info */
130 default_fd_ioctl, /* ioctl */
131 sock_queue_async, /* queue_async */
132 sock_reselect_async, /* reselect_async */
133 sock_cancel_async /* cancel_async */
137 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
138 * we post messages if there are multiple events. Used to send
139 * messages. The problem is if there is both a FD_CONNECT event and,
140 * say, an FD_READ event available on the same socket, we want to
141 * notify the app of the connect event first. Otherwise it may
142 * discard the read event because it thinks it hasn't connected yet.
144 static const int event_bitorder[FD_MAX_EVENTS] =
146 FD_CONNECT_BIT,
147 FD_ACCEPT_BIT,
148 FD_OOB_BIT,
149 FD_WRITE_BIT,
150 FD_READ_BIT,
151 FD_CLOSE_BIT,
152 6, 7, 8, 9 /* leftovers */
155 /* Flags that make sense only for SOCK_STREAM sockets */
156 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
158 typedef enum {
159 SOCK_SHUTDOWN_ERROR = -1,
160 SOCK_SHUTDOWN_EOF = 0,
161 SOCK_SHUTDOWN_POLLHUP = 1
162 } sock_shutdown_t;
164 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
166 static sock_shutdown_t sock_check_pollhup(void)
168 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
169 int fd[2], n;
170 struct pollfd pfd;
171 char dummy;
173 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) goto out;
174 if ( shutdown( fd[0], 1 ) ) goto out;
176 pfd.fd = fd[1];
177 pfd.events = POLLIN;
178 pfd.revents = 0;
180 n = poll( &pfd, 1, 0 );
181 if ( n != 1 ) goto out; /* error or timeout */
182 if ( pfd.revents & POLLHUP )
183 ret = SOCK_SHUTDOWN_POLLHUP;
184 else if ( pfd.revents & POLLIN &&
185 read( fd[1], &dummy, 1 ) == 0 )
186 ret = SOCK_SHUTDOWN_EOF;
188 out:
189 close( fd[0] );
190 close( fd[1] );
191 return ret;
194 void sock_init(void)
196 sock_shutdown_type = sock_check_pollhup();
198 switch ( sock_shutdown_type )
200 case SOCK_SHUTDOWN_EOF:
201 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
202 break;
203 case SOCK_SHUTDOWN_POLLHUP:
204 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
205 break;
206 default:
207 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
208 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
212 static int sock_reselect( struct sock *sock )
214 int ev = sock_get_poll_events( sock->fd );
216 if (debug_level)
217 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
219 if (!sock->polling) /* FIXME: should find a better way to do this */
221 /* previously unconnected socket, is this reselect supposed to connect it? */
222 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
223 /* ok, it is, attach it to the wineserver's main poll loop */
224 sock->polling = 1;
226 /* update condition mask */
227 set_fd_events( sock->fd, ev );
228 return ev;
231 /* After POLLHUP is received, the socket will no longer be in the main select loop.
232 This function is used to signal pending events nevertheless */
233 static void sock_try_event( struct sock *sock, int event )
235 event = check_fd_events( sock->fd, event );
236 if (event)
238 if ( debug_level ) fprintf( stderr, "sock_try_event: %x\n", event );
239 sock_poll_event( sock->fd, event );
243 /* wake anybody waiting on the socket event or send the associated message */
244 static void sock_wake_up( struct sock *sock, int pollev )
246 unsigned int events = sock->pmask & sock->mask;
247 int i;
248 int async_active = 0;
250 if ( pollev & (POLLIN|POLLPRI) && async_waiting( sock->read_q ))
252 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
253 async_wake_up( sock->read_q, STATUS_ALERTED );
254 async_active = 1;
256 if ( pollev & POLLOUT && async_waiting( sock->write_q ))
258 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
259 async_wake_up( sock->write_q, STATUS_ALERTED );
260 async_active = 1;
263 /* Do not signal events if there are still pending asynchronous IO requests */
264 /* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
265 if ( !events || async_active ) return;
267 if (sock->event)
269 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
270 set_event( sock->event );
272 if (sock->window)
274 if (debug_level) fprintf(stderr, "signalling events %x win %p\n", events, sock->window );
275 for (i = 0; i < FD_MAX_EVENTS; i++)
277 int event = event_bitorder[i];
278 if (sock->pmask & (1 << event))
280 unsigned int lparam = (1 << event) | (sock->errors[event] << 16);
281 post_message( sock->window, sock->message, (unsigned long)sock->wparam, lparam );
284 sock->pmask = 0;
285 sock_reselect( sock );
289 static inline int sock_error( struct fd *fd )
291 unsigned int optval = 0, optlen;
293 optlen = sizeof(optval);
294 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
295 return optval ? sock_get_error(optval) : 0;
298 static void sock_poll_event( struct fd *fd, int event )
300 struct sock *sock = get_fd_user( fd );
301 int hangup_seen = 0;
303 assert( sock->obj.ops == &sock_ops );
304 if (debug_level)
305 fprintf(stderr, "socket %p select event: %x\n", sock, event);
306 if (sock->state & FD_CONNECT)
308 /* connecting */
309 if (event & POLLOUT)
311 /* we got connected */
312 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
313 sock->state &= ~FD_CONNECT;
314 sock->pmask |= FD_CONNECT;
315 sock->errors[FD_CONNECT_BIT] = 0;
316 if (debug_level)
317 fprintf(stderr, "socket %p connection success\n", sock);
319 else if (event & (POLLERR|POLLHUP))
321 /* we didn't get connected? */
322 sock->state &= ~FD_CONNECT;
323 sock->pmask |= FD_CONNECT;
324 sock->errors[FD_CONNECT_BIT] = sock_error( fd );
325 if (debug_level)
326 fprintf(stderr, "socket %p connection failure\n", sock);
329 else if (sock->state & FD_WINE_LISTENING)
331 /* listening */
332 if (event & POLLIN)
334 /* incoming connection */
335 sock->pmask |= FD_ACCEPT;
336 sock->errors[FD_ACCEPT_BIT] = 0;
337 sock->hmask |= FD_ACCEPT;
339 else if (event & (POLLERR|POLLHUP))
341 /* failed incoming connection? */
342 sock->pmask |= FD_ACCEPT;
343 sock->errors[FD_ACCEPT_BIT] = sock_error( fd );
344 sock->hmask |= FD_ACCEPT;
347 else
349 /* normal data flow */
350 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
352 char dummy;
353 int nr;
355 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
356 * has been closed, so we need to check for it explicitly here */
357 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
358 if ( nr > 0 )
360 /* incoming data */
361 sock->pmask |= FD_READ;
362 sock->hmask |= (FD_READ|FD_CLOSE);
363 sock->errors[FD_READ_BIT] = 0;
364 if (debug_level)
365 fprintf(stderr, "socket %p is readable\n", sock );
367 else if ( nr == 0 )
368 hangup_seen = 1;
369 else
371 /* EAGAIN can happen if an async recv() falls between the server's poll()
372 call and the invocation of this routine */
373 if ( errno == EAGAIN )
374 event &= ~POLLIN;
375 else
377 if ( debug_level )
378 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
379 event = POLLERR;
384 else if ( sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP) )
386 hangup_seen = 1;
388 else if ( event & POLLIN ) /* POLLIN for non-stream socket */
390 sock->pmask |= FD_READ;
391 sock->hmask |= (FD_READ|FD_CLOSE);
392 sock->errors[FD_READ_BIT] = 0;
393 if (debug_level)
394 fprintf(stderr, "socket %p is readable\n", sock );
398 if (event & POLLOUT)
400 sock->pmask |= FD_WRITE;
401 sock->hmask |= FD_WRITE;
402 sock->errors[FD_WRITE_BIT] = 0;
403 if (debug_level)
404 fprintf(stderr, "socket %p is writable\n", sock);
406 if (event & POLLPRI)
408 sock->pmask |= FD_OOB;
409 sock->hmask |= FD_OOB;
410 sock->errors[FD_OOB_BIT] = 0;
411 if (debug_level)
412 fprintf(stderr, "socket %p got OOB data\n", sock);
414 /* According to WS2 specs, FD_CLOSE is only delivered when there is
415 no more data to be read (i.e. hangup_seen = 1) */
416 else if ( hangup_seen && (sock->state & (FD_READ|FD_WRITE) ))
418 sock->errors[FD_CLOSE_BIT] = sock_error( fd );
419 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
420 sock->state &= ~FD_WRITE;
421 sock->pmask |= FD_CLOSE;
422 sock->hmask |= FD_CLOSE;
423 if (debug_level)
424 fprintf(stderr, "socket %p aborted by error %d, event: %x - removing from select loop\n",
425 sock, sock->errors[FD_CLOSE_BIT], event);
429 if ( sock->pmask & FD_CLOSE || event & (POLLERR|POLLHUP) )
431 if ( debug_level )
432 fprintf( stderr, "removing socket %p from select loop\n", sock );
433 set_fd_events( sock->fd, -1 );
435 else
436 sock_reselect( sock );
438 /* wake up anyone waiting for whatever just happened */
439 if ( sock->pmask & sock->mask || sock->flags & WSA_FLAG_OVERLAPPED ) sock_wake_up( sock, event );
441 /* if anyone is stupid enough to wait on the socket object itself,
442 * maybe we should wake them up too, just in case? */
443 wake_up( &sock->obj, 0 );
446 static void sock_dump( struct object *obj, int verbose )
448 struct sock *sock = (struct sock *)obj;
449 assert( obj->ops == &sock_ops );
450 printf( "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
451 sock->fd, sock->state,
452 sock->mask, sock->pmask, sock->hmask );
455 static int sock_signaled( struct object *obj, struct thread *thread )
457 struct sock *sock = (struct sock *)obj;
458 assert( obj->ops == &sock_ops );
460 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
463 static int sock_get_poll_events( struct fd *fd )
465 struct sock *sock = get_fd_user( fd );
466 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
467 int ev = 0;
469 assert( sock->obj.ops == &sock_ops );
471 if (sock->state & FD_CONNECT)
472 /* connecting, wait for writable */
473 return POLLOUT;
474 if (sock->state & FD_WINE_LISTENING)
475 /* listening, wait for readable */
476 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
478 if (mask & FD_READ || async_waiting( sock->read_q )) ev |= POLLIN | POLLPRI;
479 if (mask & FD_WRITE || async_waiting( sock->write_q )) ev |= POLLOUT;
480 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
481 if ( sock->type == SOCK_STREAM && ( sock->mask & ~sock->hmask & FD_CLOSE) )
482 ev |= POLLIN;
484 return ev;
487 static enum server_fd_type sock_get_fd_type( struct fd *fd )
489 return FD_TYPE_SOCKET;
492 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
494 struct sock *sock = get_fd_user( fd );
495 struct async_queue *queue;
496 int pollev;
498 assert( sock->obj.ops == &sock_ops );
500 switch (type)
502 case ASYNC_TYPE_READ:
503 if (!sock->read_q && !(sock->read_q = create_async_queue( sock->fd ))) return;
504 queue = sock->read_q;
505 sock->hmask &= ~FD_CLOSE;
506 break;
507 case ASYNC_TYPE_WRITE:
508 if (!sock->write_q && !(sock->write_q = create_async_queue( sock->fd ))) return;
509 queue = sock->write_q;
510 break;
511 default:
512 set_error( STATUS_INVALID_PARAMETER );
513 return;
516 if ( ( !( sock->state & FD_READ ) && type == ASYNC_TYPE_READ ) ||
517 ( !( sock->state & FD_WRITE ) && type == ASYNC_TYPE_WRITE ) )
519 set_error( STATUS_PIPE_DISCONNECTED );
521 else
523 struct async *async;
524 if (!(async = create_async( current, queue, data ))) return;
525 release_object( async );
526 set_error( STATUS_PENDING );
529 pollev = sock_reselect( sock );
530 if ( pollev ) sock_try_event( sock, pollev );
533 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
535 struct sock *sock = get_fd_user( fd );
536 int events = sock_reselect( sock );
537 if (events) sock_try_event( sock, events );
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_wake_up( sock->read_q, STATUS_CANCELLED );
546 async_wake_up( 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 free_async_queue( sock->read_q );
566 free_async_queue( sock->write_q );
567 if (sock->event) release_object( sock->event );
568 if (sock->fd)
570 /* shut the socket down to force pending poll() calls in the client to return */
571 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
572 release_object( sock->fd );
576 /* create a new and unconnected socket */
577 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
579 struct sock *sock;
580 int sockfd;
582 sockfd = socket( family, type, protocol );
583 if (debug_level)
584 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
585 if (sockfd == -1)
587 sock_set_error();
588 return NULL;
590 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
591 if (!(sock = alloc_object( &sock_ops )))
593 close( sockfd );
594 return NULL;
596 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
597 sock->mask = 0;
598 sock->hmask = 0;
599 sock->pmask = 0;
600 sock->polling = 0;
601 sock->flags = flags;
602 sock->type = type;
603 sock->family = family;
604 sock->event = NULL;
605 sock->window = 0;
606 sock->message = 0;
607 sock->wparam = 0;
608 sock->deferred = NULL;
609 sock->read_q = NULL;
610 sock->write_q = NULL;
611 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
612 (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
614 release_object( sock );
615 return NULL;
617 sock_reselect( sock );
618 clear_error();
619 return &sock->obj;
622 /* accept a socket (creates a new fd) */
623 static struct sock *accept_socket( obj_handle_t handle )
625 struct sock *acceptsock;
626 struct sock *sock;
627 int acceptfd;
628 struct sockaddr saddr;
630 sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
631 if (!sock)
632 return NULL;
634 if ( sock->deferred )
636 acceptsock = sock->deferred;
637 sock->deferred = NULL;
639 else
642 /* Try to accept(2). We can't be safe that this an already connected socket
643 * or that accept() is allowed on it. In those cases we will get -1/errno
644 * return.
646 unsigned int slen = sizeof(saddr);
647 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
648 if (acceptfd==-1)
650 sock_set_error();
651 release_object( sock );
652 return NULL;
654 if (!(acceptsock = alloc_object( &sock_ops )))
656 close( acceptfd );
657 release_object( sock );
658 return NULL;
661 /* newly created socket gets the same properties of the listening socket */
662 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
663 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
664 if (sock->state & FD_WINE_NONBLOCKING)
665 acceptsock->state |= FD_WINE_NONBLOCKING;
666 acceptsock->mask = sock->mask;
667 acceptsock->hmask = 0;
668 acceptsock->pmask = 0;
669 acceptsock->polling = 0;
670 acceptsock->type = sock->type;
671 acceptsock->family = sock->family;
672 acceptsock->event = NULL;
673 acceptsock->window = sock->window;
674 acceptsock->message = sock->message;
675 acceptsock->wparam = 0;
676 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
677 acceptsock->flags = sock->flags;
678 acceptsock->deferred = NULL;
679 acceptsock->read_q = NULL;
680 acceptsock->write_q = NULL;
681 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
682 get_fd_options( sock->fd ) )))
684 release_object( acceptsock );
685 release_object( sock );
686 return NULL;
689 clear_error();
690 sock->pmask &= ~FD_ACCEPT;
691 sock->hmask &= ~FD_ACCEPT;
692 sock_reselect( sock );
693 release_object( sock );
694 return acceptsock;
697 /* set the last error depending on errno */
698 static int sock_get_error( int err )
700 switch (err)
702 case EINTR: return WSAEINTR;
703 case EBADF: return WSAEBADF;
704 case EPERM:
705 case EACCES: return WSAEACCES;
706 case EFAULT: return WSAEFAULT;
707 case EINVAL: return WSAEINVAL;
708 case EMFILE: return WSAEMFILE;
709 case EWOULDBLOCK: return WSAEWOULDBLOCK;
710 case EINPROGRESS: return WSAEINPROGRESS;
711 case EALREADY: return WSAEALREADY;
712 case ENOTSOCK: return WSAENOTSOCK;
713 case EDESTADDRREQ: return WSAEDESTADDRREQ;
714 case EMSGSIZE: return WSAEMSGSIZE;
715 case EPROTOTYPE: return WSAEPROTOTYPE;
716 case ENOPROTOOPT: return WSAENOPROTOOPT;
717 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
718 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
719 case EOPNOTSUPP: return WSAEOPNOTSUPP;
720 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
721 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
722 case EADDRINUSE: return WSAEADDRINUSE;
723 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
724 case ENETDOWN: return WSAENETDOWN;
725 case ENETUNREACH: return WSAENETUNREACH;
726 case ENETRESET: return WSAENETRESET;
727 case ECONNABORTED: return WSAECONNABORTED;
728 case EPIPE:
729 case ECONNRESET: return WSAECONNRESET;
730 case ENOBUFS: return WSAENOBUFS;
731 case EISCONN: return WSAEISCONN;
732 case ENOTCONN: return WSAENOTCONN;
733 case ESHUTDOWN: return WSAESHUTDOWN;
734 case ETOOMANYREFS: return WSAETOOMANYREFS;
735 case ETIMEDOUT: return WSAETIMEDOUT;
736 case ECONNREFUSED: return WSAECONNREFUSED;
737 case ELOOP: return WSAELOOP;
738 case ENAMETOOLONG: return WSAENAMETOOLONG;
739 case EHOSTDOWN: return WSAEHOSTDOWN;
740 case EHOSTUNREACH: return WSAEHOSTUNREACH;
741 case ENOTEMPTY: return WSAENOTEMPTY;
742 #ifdef EPROCLIM
743 case EPROCLIM: return WSAEPROCLIM;
744 #endif
745 #ifdef EUSERS
746 case EUSERS: return WSAEUSERS;
747 #endif
748 #ifdef EDQUOT
749 case EDQUOT: return WSAEDQUOT;
750 #endif
751 #ifdef ESTALE
752 case ESTALE: return WSAESTALE;
753 #endif
754 #ifdef EREMOTE
755 case EREMOTE: return WSAEREMOTE;
756 #endif
757 default:
758 errno = err;
759 perror("wineserver: sock_get_error() can't map error");
760 return WSAEFAULT;
764 /* set the last error depending on errno */
765 static void sock_set_error(void)
767 set_error( sock_get_error( errno ) );
770 /* create a socket */
771 DECL_HANDLER(create_socket)
773 struct object *obj;
775 reply->handle = 0;
776 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
778 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
779 release_object( obj );
783 /* accept a socket */
784 DECL_HANDLER(accept_socket)
786 struct sock *sock;
788 reply->handle = 0;
789 if ((sock = accept_socket( req->lhandle )) != NULL)
791 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
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 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
807 old_event = sock->event;
808 sock->mask = req->mask;
809 sock->hmask &= ~req->mask; /* re-enable held events */
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, FILE_READ_ATTRIBUTES, &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 FILE_WRITE_ATTRIBUTES, &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, FILE_WRITE_ATTRIBUTES, &sock_ops );
900 if ( !sock )
902 set_error( WSAENOTSOCK );
903 return;
905 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
906 if ( !acceptsock )
908 release_object( sock );
909 set_error( WSAENOTSOCK );
910 return;
912 sock->deferred = acceptsock;
913 release_object( sock );