server: Add a helper function to change a queue thread input.
[wine/multimedia.git] / server / sock.c
blob5eaed6b79dac24b75c1fc819e7e022fa7d31c9eb
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"
54 #include "winerror.h"
56 #include "process.h"
57 #include "file.h"
58 #include "handle.h"
59 #include "thread.h"
60 #include "request.h"
61 #include "user.h"
63 /* From winsock.h */
64 #define FD_MAX_EVENTS 10
65 #define FD_READ_BIT 0
66 #define FD_WRITE_BIT 1
67 #define FD_OOB_BIT 2
68 #define FD_ACCEPT_BIT 3
69 #define FD_CONNECT_BIT 4
70 #define FD_CLOSE_BIT 5
73 * Define flags to be used with the WSAAsyncSelect() call.
75 #define FD_READ 0x00000001
76 #define FD_WRITE 0x00000002
77 #define FD_OOB 0x00000004
78 #define FD_ACCEPT 0x00000008
79 #define FD_CONNECT 0x00000010
80 #define FD_CLOSE 0x00000020
82 /* internal per-socket flags */
83 #define FD_WINE_LISTENING 0x10000000
84 #define FD_WINE_NONBLOCKING 0x20000000
85 #define FD_WINE_CONNECTED 0x40000000
86 #define FD_WINE_RAW 0x80000000
87 #define FD_WINE_INTERNAL 0xFFFF0000
89 /* Constants for WSAIoctl() */
90 #define WSA_FLAG_OVERLAPPED 0x01
92 struct sock
94 struct object obj; /* object header */
95 struct fd *fd; /* socket file descriptor */
96 unsigned int state; /* status bits */
97 unsigned int mask; /* event mask */
98 unsigned int hmask; /* held (blocked) events */
99 unsigned int pmask; /* pending events */
100 unsigned int flags; /* socket flags */
101 int polling; /* is socket being polled? */
102 unsigned short type; /* socket type */
103 unsigned short family; /* socket family */
104 struct event *event; /* event object */
105 user_handle_t window; /* window to send the message to */
106 unsigned int message; /* message to send */
107 obj_handle_t wparam; /* message wparam (socket handle) */
108 int errors[FD_MAX_EVENTS]; /* event errors */
109 struct sock *deferred; /* socket that waits for a deferred accept */
110 struct async_queue *read_q; /* queue for asynchronous reads */
111 struct async_queue *write_q; /* queue for asynchronous writes */
114 static void sock_dump( struct object *obj, int verbose );
115 static int sock_signaled( struct object *obj, struct thread *thread );
116 static struct fd *sock_get_fd( struct object *obj );
117 static void sock_destroy( struct object *obj );
119 static int sock_get_poll_events( struct fd *fd );
120 static void sock_poll_event( struct fd *fd, int event );
121 static enum server_fd_type sock_get_fd_type( struct fd *fd );
122 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
123 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
124 static void sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb );
126 static int sock_get_error( int err );
127 static void sock_set_error(void);
129 static const struct object_ops sock_ops =
131 sizeof(struct sock), /* size */
132 sock_dump, /* dump */
133 no_get_type, /* get_type */
134 add_queue, /* add_queue */
135 remove_queue, /* remove_queue */
136 sock_signaled, /* signaled */
137 no_satisfied, /* satisfied */
138 no_signal, /* signal */
139 sock_get_fd, /* get_fd */
140 default_fd_map_access, /* map_access */
141 default_get_sd, /* get_sd */
142 default_set_sd, /* set_sd */
143 no_lookup_name, /* lookup_name */
144 no_open_file, /* open_file */
145 fd_close_handle, /* close_handle */
146 sock_destroy /* destroy */
149 static const struct fd_ops sock_fd_ops =
151 sock_get_poll_events, /* get_poll_events */
152 sock_poll_event, /* poll_event */
153 no_flush, /* flush */
154 sock_get_fd_type, /* get_file_info */
155 default_fd_ioctl, /* ioctl */
156 sock_queue_async, /* queue_async */
157 sock_reselect_async, /* reselect_async */
158 sock_cancel_async /* cancel_async */
162 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
163 * we post messages if there are multiple events. Used to send
164 * messages. The problem is if there is both a FD_CONNECT event and,
165 * say, an FD_READ event available on the same socket, we want to
166 * notify the app of the connect event first. Otherwise it may
167 * discard the read event because it thinks it hasn't connected yet.
169 static const int event_bitorder[FD_MAX_EVENTS] =
171 FD_CONNECT_BIT,
172 FD_ACCEPT_BIT,
173 FD_OOB_BIT,
174 FD_WRITE_BIT,
175 FD_READ_BIT,
176 FD_CLOSE_BIT,
177 6, 7, 8, 9 /* leftovers */
180 /* Flags that make sense only for SOCK_STREAM sockets */
181 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
183 typedef enum {
184 SOCK_SHUTDOWN_ERROR = -1,
185 SOCK_SHUTDOWN_EOF = 0,
186 SOCK_SHUTDOWN_POLLHUP = 1
187 } sock_shutdown_t;
189 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
191 static sock_shutdown_t sock_check_pollhup(void)
193 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
194 int fd[2], n;
195 struct pollfd pfd;
196 char dummy;
198 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) goto out;
199 if ( shutdown( fd[0], 1 ) ) goto out;
201 pfd.fd = fd[1];
202 pfd.events = POLLIN;
203 pfd.revents = 0;
205 n = poll( &pfd, 1, 0 );
206 if ( n != 1 ) goto out; /* error or timeout */
207 if ( pfd.revents & POLLHUP )
208 ret = SOCK_SHUTDOWN_POLLHUP;
209 else if ( pfd.revents & POLLIN &&
210 read( fd[1], &dummy, 1 ) == 0 )
211 ret = SOCK_SHUTDOWN_EOF;
213 out:
214 close( fd[0] );
215 close( fd[1] );
216 return ret;
219 void sock_init(void)
221 sock_shutdown_type = sock_check_pollhup();
223 switch ( sock_shutdown_type )
225 case SOCK_SHUTDOWN_EOF:
226 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
227 break;
228 case SOCK_SHUTDOWN_POLLHUP:
229 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
230 break;
231 default:
232 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
233 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
237 static int sock_reselect( struct sock *sock )
239 int ev = sock_get_poll_events( sock->fd );
241 if (debug_level)
242 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
244 if (!sock->polling) /* FIXME: should find a better way to do this */
246 /* previously unconnected socket, is this reselect supposed to connect it? */
247 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
248 /* ok, it is, attach it to the wineserver's main poll loop */
249 sock->polling = 1;
251 /* update condition mask */
252 set_fd_events( sock->fd, ev );
253 return ev;
256 /* After POLLHUP is received, the socket will no longer be in the main select loop.
257 This function is used to signal pending events nevertheless */
258 static void sock_try_event( struct sock *sock, int event )
260 event = check_fd_events( sock->fd, event );
261 if (event)
263 if ( debug_level ) fprintf( stderr, "sock_try_event: %x\n", event );
264 sock_poll_event( sock->fd, event );
268 /* wake anybody waiting on the socket event or send the associated message */
269 static void sock_wake_up( struct sock *sock, int pollev )
271 unsigned int events = sock->pmask & sock->mask;
272 int i;
273 int async_active = 0;
275 if ( pollev & (POLLIN|POLLPRI|POLLERR|POLLHUP) && async_waiting( sock->read_q ))
277 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
278 async_wake_up( sock->read_q, STATUS_ALERTED );
279 async_active = 1;
281 if ( pollev & (POLLOUT|POLLERR|POLLHUP) && async_waiting( sock->write_q ))
283 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
284 async_wake_up( sock->write_q, STATUS_ALERTED );
285 async_active = 1;
288 /* Do not signal events if there are still pending asynchronous IO requests */
289 /* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
290 if ( !events || async_active ) return;
292 if (sock->event)
294 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
295 set_event( sock->event );
297 if (sock->window)
299 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
300 for (i = 0; i < FD_MAX_EVENTS; i++)
302 int event = event_bitorder[i];
303 if (sock->pmask & (1 << event))
305 lparam_t lparam = (1 << event) | (sock->errors[event] << 16);
306 post_message( sock->window, sock->message, sock->wparam, lparam );
309 sock->pmask = 0;
310 sock_reselect( sock );
314 static inline int sock_error( struct fd *fd )
316 unsigned int optval = 0, optlen;
318 optlen = sizeof(optval);
319 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
320 return optval ? sock_get_error(optval) : 0;
323 static void sock_poll_event( struct fd *fd, int event )
325 struct sock *sock = get_fd_user( fd );
326 int hangup_seen = 0;
328 assert( sock->obj.ops == &sock_ops );
329 if (debug_level)
330 fprintf(stderr, "socket %p select event: %x\n", sock, event);
331 if (sock->state & FD_CONNECT)
333 /* connecting */
334 if (event & POLLOUT)
336 /* we got connected */
337 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
338 sock->state &= ~FD_CONNECT;
339 sock->pmask |= FD_CONNECT;
340 sock->errors[FD_CONNECT_BIT] = 0;
341 if (debug_level)
342 fprintf(stderr, "socket %p connection success\n", sock);
344 else if (event & (POLLERR|POLLHUP))
346 /* we didn't get connected? */
347 sock->state &= ~FD_CONNECT;
348 sock->pmask |= FD_CONNECT;
349 sock->errors[FD_CONNECT_BIT] = sock_error( fd );
350 if (debug_level)
351 fprintf(stderr, "socket %p connection failure\n", sock);
354 else if (sock->state & FD_WINE_LISTENING)
356 /* listening */
357 if (event & POLLIN)
359 /* incoming connection */
360 sock->pmask |= FD_ACCEPT;
361 sock->errors[FD_ACCEPT_BIT] = 0;
362 sock->hmask |= FD_ACCEPT;
364 else if (event & (POLLERR|POLLHUP))
366 /* failed incoming connection? */
367 sock->pmask |= FD_ACCEPT;
368 sock->errors[FD_ACCEPT_BIT] = sock_error( fd );
369 sock->hmask |= FD_ACCEPT;
372 else
374 /* normal data flow */
375 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
377 char dummy;
378 int nr;
380 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
381 * has been closed, so we need to check for it explicitly here */
382 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
383 if ( nr > 0 )
385 /* incoming data */
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 );
392 else if ( nr == 0 )
393 hangup_seen = 1;
394 else
396 /* EAGAIN can happen if an async recv() falls between the server's poll()
397 call and the invocation of this routine */
398 if ( errno == EAGAIN )
399 event &= ~POLLIN;
400 else
402 if ( debug_level )
403 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
404 event = POLLERR;
409 else if ( sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP) )
411 hangup_seen = 1;
413 else if ( event & POLLIN ) /* POLLIN for non-stream socket */
415 sock->pmask |= FD_READ;
416 sock->hmask |= (FD_READ|FD_CLOSE);
417 sock->errors[FD_READ_BIT] = 0;
418 if (debug_level)
419 fprintf(stderr, "socket %p is readable\n", sock );
423 if (event & POLLOUT)
425 sock->pmask |= FD_WRITE;
426 sock->hmask |= FD_WRITE;
427 sock->errors[FD_WRITE_BIT] = 0;
428 if (debug_level)
429 fprintf(stderr, "socket %p is writable\n", sock);
431 if (event & POLLPRI)
433 sock->pmask |= FD_OOB;
434 sock->hmask |= FD_OOB;
435 sock->errors[FD_OOB_BIT] = 0;
436 if (debug_level)
437 fprintf(stderr, "socket %p got OOB data\n", sock);
439 /* According to WS2 specs, FD_CLOSE is only delivered when there is
440 no more data to be read (i.e. hangup_seen = 1) */
441 else if ( hangup_seen && (sock->state & (FD_READ|FD_WRITE) ))
443 sock->errors[FD_CLOSE_BIT] = sock_error( fd );
444 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
445 sock->state &= ~FD_WRITE;
446 sock->pmask |= FD_CLOSE;
447 sock->hmask |= FD_CLOSE;
448 if (debug_level)
449 fprintf(stderr, "socket %p aborted by error %d, event: %x - removing from select loop\n",
450 sock, sock->errors[FD_CLOSE_BIT], event);
454 if ( sock->pmask & FD_CLOSE || event & (POLLERR|POLLHUP) )
456 if ( debug_level )
457 fprintf( stderr, "removing socket %p from select loop\n", sock );
458 set_fd_events( sock->fd, -1 );
460 else
461 sock_reselect( sock );
463 /* wake up anyone waiting for whatever just happened */
464 if ( sock->pmask & sock->mask || sock->flags & WSA_FLAG_OVERLAPPED ) sock_wake_up( sock, event );
466 /* if anyone is stupid enough to wait on the socket object itself,
467 * maybe we should wake them up too, just in case? */
468 wake_up( &sock->obj, 0 );
471 static void sock_dump( struct object *obj, int verbose )
473 struct sock *sock = (struct sock *)obj;
474 assert( obj->ops == &sock_ops );
475 printf( "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
476 sock->fd, sock->state,
477 sock->mask, sock->pmask, sock->hmask );
480 static int sock_signaled( struct object *obj, struct thread *thread )
482 struct sock *sock = (struct sock *)obj;
483 assert( obj->ops == &sock_ops );
485 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
488 static int sock_get_poll_events( struct fd *fd )
490 struct sock *sock = get_fd_user( fd );
491 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
492 int ev = 0;
494 assert( sock->obj.ops == &sock_ops );
496 if (sock->state & FD_CONNECT)
497 /* connecting, wait for writable */
498 return POLLOUT;
499 if (sock->state & FD_WINE_LISTENING)
500 /* listening, wait for readable */
501 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
503 if (mask & FD_READ || async_waiting( sock->read_q )) ev |= POLLIN | POLLPRI;
504 if (mask & FD_WRITE || async_waiting( sock->write_q )) ev |= POLLOUT;
505 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
506 if ( sock->type == SOCK_STREAM && ( sock->mask & ~sock->hmask & FD_CLOSE) )
507 ev |= POLLIN;
509 return ev;
512 static enum server_fd_type sock_get_fd_type( struct fd *fd )
514 return FD_TYPE_SOCKET;
517 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
519 struct sock *sock = get_fd_user( fd );
520 struct async_queue *queue;
521 int pollev;
523 assert( sock->obj.ops == &sock_ops );
525 switch (type)
527 case ASYNC_TYPE_READ:
528 if (!sock->read_q && !(sock->read_q = create_async_queue( sock->fd ))) return;
529 queue = sock->read_q;
530 sock->hmask &= ~FD_CLOSE;
531 break;
532 case ASYNC_TYPE_WRITE:
533 if (!sock->write_q && !(sock->write_q = create_async_queue( sock->fd ))) return;
534 queue = sock->write_q;
535 break;
536 default:
537 set_error( STATUS_INVALID_PARAMETER );
538 return;
541 if ( ( !( sock->state & FD_READ ) && type == ASYNC_TYPE_READ ) ||
542 ( !( sock->state & FD_WRITE ) && type == ASYNC_TYPE_WRITE ) )
544 set_error( STATUS_PIPE_DISCONNECTED );
546 else
548 struct async *async;
549 if (!(async = create_async( current, queue, data ))) return;
550 release_object( async );
551 set_error( STATUS_PENDING );
554 pollev = sock_reselect( sock );
555 if ( pollev ) sock_try_event( sock, pollev );
558 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
560 struct sock *sock = get_fd_user( fd );
561 int events = sock_reselect( sock );
562 if (events) sock_try_event( sock, events );
565 static void sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb )
567 struct sock *sock = get_fd_user( fd );
568 int n = 0;
569 assert( sock->obj.ops == &sock_ops );
571 n += async_wake_up_by( sock->read_q, process, thread, iosb, STATUS_CANCELLED );
572 n += async_wake_up_by( sock->write_q, process, thread, iosb, STATUS_CANCELLED );
573 if (!n && iosb)
574 set_error( STATUS_NOT_FOUND );
577 static struct fd *sock_get_fd( struct object *obj )
579 struct sock *sock = (struct sock *)obj;
580 return (struct fd *)grab_object( sock->fd );
583 static void sock_destroy( struct object *obj )
585 struct sock *sock = (struct sock *)obj;
586 assert( obj->ops == &sock_ops );
588 /* FIXME: special socket shutdown stuff? */
590 if ( sock->deferred )
591 release_object( sock->deferred );
593 free_async_queue( sock->read_q );
594 free_async_queue( sock->write_q );
595 if (sock->event) release_object( sock->event );
596 if (sock->fd)
598 /* shut the socket down to force pending poll() calls in the client to return */
599 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
600 release_object( sock->fd );
604 /* create a new and unconnected socket */
605 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
607 struct sock *sock;
608 int sockfd;
610 sockfd = socket( family, type, protocol );
611 if (debug_level)
612 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
613 if (sockfd == -1)
615 sock_set_error();
616 return NULL;
618 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
619 if (!(sock = alloc_object( &sock_ops )))
621 close( sockfd );
622 return NULL;
624 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
625 sock->mask = 0;
626 sock->hmask = 0;
627 sock->pmask = 0;
628 sock->polling = 0;
629 sock->flags = flags;
630 sock->type = type;
631 sock->family = family;
632 sock->event = NULL;
633 sock->window = 0;
634 sock->message = 0;
635 sock->wparam = 0;
636 sock->deferred = NULL;
637 sock->read_q = NULL;
638 sock->write_q = NULL;
639 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
640 (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
642 release_object( sock );
643 return NULL;
645 sock_reselect( sock );
646 clear_error();
647 return &sock->obj;
650 /* accept a socket (creates a new fd) */
651 static struct sock *accept_socket( obj_handle_t handle )
653 struct sock *acceptsock;
654 struct sock *sock;
655 int acceptfd;
656 struct sockaddr saddr;
658 sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
659 if (!sock)
660 return NULL;
662 if ( sock->deferred )
664 acceptsock = sock->deferred;
665 sock->deferred = NULL;
667 else
670 /* Try to accept(2). We can't be safe that this an already connected socket
671 * or that accept() is allowed on it. In those cases we will get -1/errno
672 * return.
674 unsigned int slen = sizeof(saddr);
675 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
676 if (acceptfd==-1)
678 sock_set_error();
679 release_object( sock );
680 return NULL;
682 if (!(acceptsock = alloc_object( &sock_ops )))
684 close( acceptfd );
685 release_object( sock );
686 return NULL;
689 /* newly created socket gets the same properties of the listening socket */
690 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
691 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
692 if (sock->state & FD_WINE_NONBLOCKING)
693 acceptsock->state |= FD_WINE_NONBLOCKING;
694 acceptsock->mask = sock->mask;
695 acceptsock->hmask = 0;
696 acceptsock->pmask = 0;
697 acceptsock->polling = 0;
698 acceptsock->type = sock->type;
699 acceptsock->family = sock->family;
700 acceptsock->event = NULL;
701 acceptsock->window = sock->window;
702 acceptsock->message = sock->message;
703 acceptsock->wparam = 0;
704 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
705 acceptsock->flags = sock->flags;
706 acceptsock->deferred = NULL;
707 acceptsock->read_q = NULL;
708 acceptsock->write_q = NULL;
709 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
710 get_fd_options( sock->fd ) )))
712 release_object( acceptsock );
713 release_object( sock );
714 return NULL;
717 clear_error();
718 sock->pmask &= ~FD_ACCEPT;
719 sock->hmask &= ~FD_ACCEPT;
720 sock_reselect( sock );
721 release_object( sock );
722 return acceptsock;
725 /* set the last error depending on errno */
726 static int sock_get_error( int err )
728 switch (err)
730 case EINTR: return WSAEINTR;
731 case EBADF: return WSAEBADF;
732 case EPERM:
733 case EACCES: return WSAEACCES;
734 case EFAULT: return WSAEFAULT;
735 case EINVAL: return WSAEINVAL;
736 case EMFILE: return WSAEMFILE;
737 case EWOULDBLOCK: return WSAEWOULDBLOCK;
738 case EINPROGRESS: return WSAEINPROGRESS;
739 case EALREADY: return WSAEALREADY;
740 case ENOTSOCK: return WSAENOTSOCK;
741 case EDESTADDRREQ: return WSAEDESTADDRREQ;
742 case EMSGSIZE: return WSAEMSGSIZE;
743 case EPROTOTYPE: return WSAEPROTOTYPE;
744 case ENOPROTOOPT: return WSAENOPROTOOPT;
745 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
746 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
747 case EOPNOTSUPP: return WSAEOPNOTSUPP;
748 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
749 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
750 case EADDRINUSE: return WSAEADDRINUSE;
751 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
752 case ENETDOWN: return WSAENETDOWN;
753 case ENETUNREACH: return WSAENETUNREACH;
754 case ENETRESET: return WSAENETRESET;
755 case ECONNABORTED: return WSAECONNABORTED;
756 case EPIPE:
757 case ECONNRESET: return WSAECONNRESET;
758 case ENOBUFS: return WSAENOBUFS;
759 case EISCONN: return WSAEISCONN;
760 case ENOTCONN: return WSAENOTCONN;
761 case ESHUTDOWN: return WSAESHUTDOWN;
762 case ETOOMANYREFS: return WSAETOOMANYREFS;
763 case ETIMEDOUT: return WSAETIMEDOUT;
764 case ECONNREFUSED: return WSAECONNREFUSED;
765 case ELOOP: return WSAELOOP;
766 case ENAMETOOLONG: return WSAENAMETOOLONG;
767 case EHOSTDOWN: return WSAEHOSTDOWN;
768 case EHOSTUNREACH: return WSAEHOSTUNREACH;
769 case ENOTEMPTY: return WSAENOTEMPTY;
770 #ifdef EPROCLIM
771 case EPROCLIM: return WSAEPROCLIM;
772 #endif
773 #ifdef EUSERS
774 case EUSERS: return WSAEUSERS;
775 #endif
776 #ifdef EDQUOT
777 case EDQUOT: return WSAEDQUOT;
778 #endif
779 #ifdef ESTALE
780 case ESTALE: return WSAESTALE;
781 #endif
782 #ifdef EREMOTE
783 case EREMOTE: return WSAEREMOTE;
784 #endif
785 default:
786 errno = err;
787 perror("wineserver: sock_get_error() can't map error");
788 return WSAEFAULT;
792 /* set the last error depending on errno */
793 static void sock_set_error(void)
795 set_error( sock_get_error( errno ) );
798 /* create a socket */
799 DECL_HANDLER(create_socket)
801 struct object *obj;
803 reply->handle = 0;
804 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
806 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
807 release_object( obj );
811 /* accept a socket */
812 DECL_HANDLER(accept_socket)
814 struct sock *sock;
816 reply->handle = 0;
817 if ((sock = accept_socket( req->lhandle )) != NULL)
819 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
820 sock->wparam = reply->handle; /* wparam for message is the socket handle */
821 sock_reselect( sock );
822 release_object( &sock->obj );
826 /* set socket event parameters */
827 DECL_HANDLER(set_socket_event)
829 struct sock *sock;
830 struct event *old_event;
831 int pollev;
833 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
834 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
835 old_event = sock->event;
836 sock->mask = req->mask;
837 sock->hmask &= ~req->mask; /* re-enable held events */
838 sock->event = NULL;
839 sock->window = req->window;
840 sock->message = req->msg;
841 sock->wparam = req->handle; /* wparam is the socket handle */
842 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
844 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
846 pollev = sock_reselect( sock );
847 if ( pollev ) sock_try_event( sock, pollev );
849 if (sock->mask)
850 sock->state |= FD_WINE_NONBLOCKING;
852 /* if a network event is pending, signal the event object
853 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
854 before a WSAEventSelect() was done on it.
855 (when dealing with Asynchronous socket) */
856 if (sock->pmask & sock->mask) sock_wake_up( sock, pollev );
858 if (old_event) release_object( old_event ); /* we're through with it */
859 release_object( &sock->obj );
862 /* get socket event parameters */
863 DECL_HANDLER(get_socket_event)
865 struct sock *sock;
867 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
868 if (!sock)
870 reply->mask = 0;
871 reply->pmask = 0;
872 reply->state = 0;
873 set_error( WSAENOTSOCK );
874 return;
876 reply->mask = sock->mask;
877 reply->pmask = sock->pmask;
878 reply->state = sock->state;
879 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
881 if (req->service)
883 if (req->c_event)
885 struct event *cevent = get_event_obj( current->process, req->c_event,
886 EVENT_MODIFY_STATE );
887 if (cevent)
889 reset_event( cevent );
890 release_object( cevent );
893 sock->pmask = 0;
894 sock_reselect( sock );
896 release_object( &sock->obj );
899 /* re-enable pending socket events */
900 DECL_HANDLER(enable_socket_event)
902 struct sock *sock;
903 int pollev;
905 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
906 FILE_WRITE_ATTRIBUTES, &sock_ops)))
907 return;
909 sock->pmask &= ~req->mask; /* is this safe? */
910 sock->hmask &= ~req->mask;
911 if ( req->mask & FD_READ )
912 sock->hmask &= ~FD_CLOSE;
913 sock->state |= req->sstate;
914 sock->state &= ~req->cstate;
915 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
917 pollev = sock_reselect( sock );
918 if ( pollev ) sock_try_event( sock, pollev );
920 release_object( &sock->obj );
923 DECL_HANDLER(set_socket_deferred)
925 struct sock *sock, *acceptsock;
927 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
928 if ( !sock )
930 set_error( WSAENOTSOCK );
931 return;
933 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
934 if ( !acceptsock )
936 release_object( sock );
937 set_error( WSAENOTSOCK );
938 return;
940 sock->deferred = acceptsock;
941 release_object( sock );