comctl32: Don't use the RECT from the custom draw structure when referring to the...
[wine/multimedia.git] / server / sock.c
blob4adad0f68ea464bbfa07323e462e84541a697950
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_POLL_H
34 # include <poll.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 proto; /* socket protocol */
103 unsigned short type; /* socket type */
104 unsigned short family; /* socket family */
105 struct event *event; /* event object */
106 user_handle_t window; /* window to send the message to */
107 unsigned int message; /* message to send */
108 obj_handle_t wparam; /* message wparam (socket handle) */
109 int errors[FD_MAX_EVENTS]; /* event errors */
110 struct sock *deferred; /* socket that waits for a deferred accept */
111 struct async_queue *read_q; /* queue for asynchronous reads */
112 struct async_queue *write_q; /* queue for asynchronous writes */
115 static void sock_dump( struct object *obj, int verbose );
116 static int sock_signaled( struct object *obj, struct wait_queue_entry *entry );
117 static struct fd *sock_get_fd( struct object *obj );
118 static void sock_destroy( struct object *obj );
120 static int sock_get_poll_events( struct fd *fd );
121 static void sock_poll_event( struct fd *fd, int event );
122 static enum server_fd_type sock_get_fd_type( struct fd *fd );
123 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
124 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
125 static void sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb );
127 static int sock_get_ntstatus( int err );
128 static int sock_get_error( int err );
129 static void sock_set_error(void);
131 static const struct object_ops sock_ops =
133 sizeof(struct sock), /* size */
134 sock_dump, /* dump */
135 no_get_type, /* get_type */
136 add_queue, /* add_queue */
137 remove_queue, /* remove_queue */
138 sock_signaled, /* signaled */
139 no_satisfied, /* satisfied */
140 no_signal, /* signal */
141 sock_get_fd, /* get_fd */
142 default_fd_map_access, /* map_access */
143 default_get_sd, /* get_sd */
144 default_set_sd, /* set_sd */
145 no_lookup_name, /* lookup_name */
146 no_open_file, /* open_file */
147 fd_close_handle, /* close_handle */
148 sock_destroy /* destroy */
151 static const struct fd_ops sock_fd_ops =
153 sock_get_poll_events, /* get_poll_events */
154 sock_poll_event, /* poll_event */
155 no_flush, /* flush */
156 sock_get_fd_type, /* get_fd_type */
157 default_fd_ioctl, /* ioctl */
158 sock_queue_async, /* queue_async */
159 sock_reselect_async, /* reselect_async */
160 sock_cancel_async /* cancel_async */
164 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
165 * we post messages if there are multiple events. Used to send
166 * messages. The problem is if there is both a FD_CONNECT event and,
167 * say, an FD_READ event available on the same socket, we want to
168 * notify the app of the connect event first. Otherwise it may
169 * discard the read event because it thinks it hasn't connected yet.
171 static const int event_bitorder[FD_MAX_EVENTS] =
173 FD_CONNECT_BIT,
174 FD_ACCEPT_BIT,
175 FD_OOB_BIT,
176 FD_WRITE_BIT,
177 FD_READ_BIT,
178 FD_CLOSE_BIT,
179 6, 7, 8, 9 /* leftovers */
182 /* Flags that make sense only for SOCK_STREAM sockets */
183 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
185 typedef enum {
186 SOCK_SHUTDOWN_ERROR = -1,
187 SOCK_SHUTDOWN_EOF = 0,
188 SOCK_SHUTDOWN_POLLHUP = 1
189 } sock_shutdown_t;
191 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
193 static sock_shutdown_t sock_check_pollhup(void)
195 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
196 int fd[2], n;
197 struct pollfd pfd;
198 char dummy;
200 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
201 if ( shutdown( fd[0], 1 ) ) goto out;
203 pfd.fd = fd[1];
204 pfd.events = POLLIN;
205 pfd.revents = 0;
207 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
208 n = poll( &pfd, 1, 1 );
209 if ( n != 1 ) goto out; /* error or timeout */
210 if ( pfd.revents & POLLHUP )
211 ret = SOCK_SHUTDOWN_POLLHUP;
212 else if ( pfd.revents & POLLIN &&
213 read( fd[1], &dummy, 1 ) == 0 )
214 ret = SOCK_SHUTDOWN_EOF;
216 out:
217 close( fd[0] );
218 close( fd[1] );
219 return ret;
222 void sock_init(void)
224 sock_shutdown_type = sock_check_pollhup();
226 switch ( sock_shutdown_type )
228 case SOCK_SHUTDOWN_EOF:
229 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
230 break;
231 case SOCK_SHUTDOWN_POLLHUP:
232 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
233 break;
234 default:
235 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
236 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
240 static int sock_reselect( struct sock *sock )
242 int ev = sock_get_poll_events( sock->fd );
244 if (debug_level)
245 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
247 if (!sock->polling) /* FIXME: should find a better way to do this */
249 /* previously unconnected socket, is this reselect supposed to connect it? */
250 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
251 /* ok, it is, attach it to the wineserver's main poll loop */
252 sock->polling = 1;
253 allow_fd_caching( sock->fd );
255 /* update condition mask */
256 set_fd_events( sock->fd, ev );
257 return ev;
260 /* wake anybody waiting on the socket event or send the associated message */
261 static void sock_wake_up( struct sock *sock )
263 unsigned int events = sock->pmask & sock->mask;
264 int i;
266 if ( !events ) return;
268 if (sock->event)
270 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
271 set_event( sock->event );
273 if (sock->window)
275 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
276 for (i = 0; i < FD_MAX_EVENTS; i++)
278 int event = event_bitorder[i];
279 if (sock->pmask & (1 << event))
281 lparam_t lparam = (1 << event) | (sock_get_error(sock->errors[event]) << 16);
282 post_message( sock->window, sock->message, sock->wparam, lparam );
285 sock->pmask = 0;
286 sock_reselect( sock );
290 static inline int sock_error( struct fd *fd )
292 unsigned int optval = 0;
293 socklen_t optlen = sizeof(optval);
295 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
296 return optval;
299 static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
301 if ( sock->flags & WSA_FLAG_OVERLAPPED )
303 if ( event & (POLLIN|POLLPRI) && async_waiting( sock->read_q ) )
305 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
306 async_wake_up( sock->read_q, STATUS_ALERTED );
307 event &= ~(POLLIN|POLLPRI);
309 if ( event & POLLOUT && async_waiting( sock->write_q ) )
311 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
312 async_wake_up( sock->write_q, STATUS_ALERTED );
313 event &= ~POLLOUT;
315 if ( event & (POLLERR|POLLHUP) )
317 int status = sock_get_ntstatus( error );
319 if ( !(sock->state & FD_READ) )
320 async_wake_up( sock->read_q, status );
321 if ( !(sock->state & FD_WRITE) )
322 async_wake_up( sock->write_q, status );
325 return event;
328 static void sock_dispatch_events( struct sock *sock, int prevstate, int event, int error )
330 if (prevstate & FD_CONNECT)
332 sock->pmask |= FD_CONNECT;
333 sock->hmask |= FD_CONNECT;
334 sock->errors[FD_CONNECT_BIT] = error;
335 goto end;
337 if (prevstate & FD_WINE_LISTENING)
339 sock->pmask |= FD_ACCEPT;
340 sock->hmask |= FD_ACCEPT;
341 sock->errors[FD_ACCEPT_BIT] = error;
342 goto end;
345 if (event & POLLIN)
347 sock->pmask |= FD_READ;
348 sock->hmask |= FD_READ;
349 sock->errors[FD_READ_BIT] = 0;
352 if (event & POLLOUT)
354 sock->pmask |= FD_WRITE;
355 sock->hmask |= FD_WRITE;
356 sock->errors[FD_WRITE_BIT] = 0;
359 if (event & POLLPRI)
361 sock->pmask |= FD_OOB;
362 sock->hmask |= FD_OOB;
363 sock->errors[FD_OOB_BIT] = 0;
366 if (event & (POLLERR|POLLHUP))
368 sock->pmask |= FD_CLOSE;
369 sock->hmask |= FD_CLOSE;
370 sock->errors[FD_CLOSE_BIT] = error;
372 end:
373 sock_wake_up( sock );
376 static void sock_poll_event( struct fd *fd, int event )
378 struct sock *sock = get_fd_user( fd );
379 int hangup_seen = 0;
380 int prevstate = sock->state;
381 int error = 0;
383 assert( sock->obj.ops == &sock_ops );
384 if (debug_level)
385 fprintf(stderr, "socket %p select event: %x\n", sock, event);
387 /* we may change event later, remove from loop here */
388 if (event & (POLLERR|POLLHUP)) set_fd_events( sock->fd, -1 );
390 if (sock->state & FD_CONNECT)
392 if (event & (POLLERR|POLLHUP))
394 /* we didn't get connected? */
395 sock->state &= ~FD_CONNECT;
396 event &= ~POLLOUT;
397 error = sock_error( fd );
399 else if (event & POLLOUT)
401 /* we got connected */
402 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
403 sock->state &= ~FD_CONNECT;
406 else if (sock->state & FD_WINE_LISTENING)
408 /* listening */
409 if (event & (POLLERR|POLLHUP))
410 error = sock_error( fd );
412 else
414 /* normal data flow */
415 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
417 char dummy;
418 int nr;
420 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
421 * has been closed, so we need to check for it explicitly here */
422 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
423 if ( nr == 0 )
425 hangup_seen = 1;
426 event &= ~POLLIN;
428 else if ( nr < 0 )
430 event &= ~POLLIN;
431 /* EAGAIN can happen if an async recv() falls between the server's poll()
432 call and the invocation of this routine */
433 if ( errno != EAGAIN )
435 error = errno;
436 event |= POLLERR;
437 if ( debug_level )
438 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
443 if ( (hangup_seen || event & (POLLHUP|POLLERR)) && (sock->state & (FD_READ|FD_WRITE)) )
445 error = error ? error : sock_error( fd );
446 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
447 sock->state &= ~FD_WRITE;
448 sock->state &= ~FD_READ;
450 if (debug_level)
451 fprintf(stderr, "socket %p aborted by error %d, event: %x\n", sock, error, event);
454 if (hangup_seen)
455 event |= POLLHUP;
458 event = sock_dispatch_asyncs( sock, event, error );
459 sock_dispatch_events( sock, prevstate, event, error );
461 /* if anyone is stupid enough to wait on the socket object itself,
462 * maybe we should wake them up too, just in case? */
463 wake_up( &sock->obj, 0 );
465 sock_reselect( sock );
468 static void sock_dump( struct object *obj, int verbose )
470 struct sock *sock = (struct sock *)obj;
471 assert( obj->ops == &sock_ops );
472 fprintf( stderr, "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
473 sock->fd, sock->state,
474 sock->mask, sock->pmask, sock->hmask );
477 static int sock_signaled( struct object *obj, struct wait_queue_entry *entry )
479 struct sock *sock = (struct sock *)obj;
480 assert( obj->ops == &sock_ops );
482 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
485 static int sock_get_poll_events( struct fd *fd )
487 struct sock *sock = get_fd_user( fd );
488 unsigned int mask = sock->mask & ~sock->hmask;
489 unsigned int smask = sock->state & mask;
490 int ev = 0;
492 assert( sock->obj.ops == &sock_ops );
494 if (sock->state & FD_CONNECT)
495 /* connecting, wait for writable */
496 return POLLOUT;
498 if ( async_queued( sock->read_q ) )
500 if ( async_waiting( sock->read_q ) ) ev |= POLLIN | POLLPRI;
502 else if (smask & FD_READ || (sock->state & FD_WINE_LISTENING && mask & FD_ACCEPT))
503 ev |= POLLIN | POLLPRI;
504 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
505 else if ( sock->type == SOCK_STREAM && sock->state & FD_READ && mask & FD_CLOSE &&
506 !(sock->hmask & FD_READ) )
507 ev |= POLLIN;
509 if ( async_queued( sock->write_q ) )
511 if ( async_waiting( sock->write_q ) ) ev |= POLLOUT;
513 else if (smask & FD_WRITE)
514 ev |= POLLOUT;
516 return ev;
519 static enum server_fd_type sock_get_fd_type( struct fd *fd )
521 return FD_TYPE_SOCKET;
524 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
526 struct sock *sock = get_fd_user( fd );
527 struct async *async;
528 struct async_queue *queue;
530 assert( sock->obj.ops == &sock_ops );
532 switch (type)
534 case ASYNC_TYPE_READ:
535 if (!sock->read_q && !(sock->read_q = create_async_queue( sock->fd ))) return;
536 queue = sock->read_q;
537 break;
538 case ASYNC_TYPE_WRITE:
539 if (!sock->write_q && !(sock->write_q = create_async_queue( sock->fd ))) return;
540 queue = sock->write_q;
541 break;
542 default:
543 set_error( STATUS_INVALID_PARAMETER );
544 return;
547 if ( ( !( sock->state & (FD_READ|FD_CONNECT|FD_WINE_LISTENING) ) && type == ASYNC_TYPE_READ ) ||
548 ( !( sock->state & (FD_WRITE|FD_CONNECT) ) && type == ASYNC_TYPE_WRITE ) )
550 set_error( STATUS_PIPE_DISCONNECTED );
551 return;
554 if (!(async = create_async( current, queue, data ))) return;
555 release_object( async );
557 sock_reselect( sock );
559 set_error( STATUS_PENDING );
562 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
564 struct sock *sock = get_fd_user( fd );
565 sock_reselect( sock );
568 static void sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb )
570 struct sock *sock = get_fd_user( fd );
571 int n = 0;
572 assert( sock->obj.ops == &sock_ops );
574 n += async_wake_up_by( sock->read_q, process, thread, iosb, STATUS_CANCELLED );
575 n += async_wake_up_by( sock->write_q, process, thread, iosb, STATUS_CANCELLED );
576 if (!n && iosb)
577 set_error( STATUS_NOT_FOUND );
580 static struct fd *sock_get_fd( struct object *obj )
582 struct sock *sock = (struct sock *)obj;
583 return (struct fd *)grab_object( sock->fd );
586 static void sock_destroy( struct object *obj )
588 struct sock *sock = (struct sock *)obj;
589 assert( obj->ops == &sock_ops );
591 /* FIXME: special socket shutdown stuff? */
593 if ( sock->deferred )
594 release_object( sock->deferred );
596 free_async_queue( sock->read_q );
597 free_async_queue( sock->write_q );
598 if (sock->event) release_object( sock->event );
599 if (sock->fd)
601 /* shut the socket down to force pending poll() calls in the client to return */
602 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
603 release_object( sock->fd );
607 static void init_sock(struct sock *sock)
609 sock->state = 0;
610 sock->mask = 0;
611 sock->hmask = 0;
612 sock->pmask = 0;
613 sock->polling = 0;
614 sock->flags = 0;
615 sock->type = 0;
616 sock->family = 0;
617 sock->event = NULL;
618 sock->window = 0;
619 sock->message = 0;
620 sock->wparam = 0;
621 sock->deferred = NULL;
622 sock->read_q = NULL;
623 sock->write_q = NULL;
624 memset( sock->errors, 0, sizeof(sock->errors) );
627 /* create a new and unconnected socket */
628 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
630 struct sock *sock;
631 int sockfd;
633 sockfd = socket( family, type, protocol );
634 if (debug_level)
635 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
636 if (sockfd == -1)
638 sock_set_error();
639 return NULL;
641 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
642 if (!(sock = alloc_object( &sock_ops )))
644 close( sockfd );
645 return NULL;
647 init_sock( sock );
648 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
649 sock->flags = flags;
650 sock->proto = protocol;
651 sock->type = type;
652 sock->family = family;
654 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
655 (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
657 release_object( sock );
658 return NULL;
660 sock_reselect( sock );
661 clear_error();
662 return &sock->obj;
665 /* accepts a socket and inits it */
666 static int accept_new_fd( struct sock *sock )
669 /* Try to accept(2). We can't be safe that this an already connected socket
670 * or that accept() is allowed on it. In those cases we will get -1/errno
671 * return.
673 int acceptfd;
674 struct sockaddr saddr;
675 socklen_t slen = sizeof(saddr);
676 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
677 if (acceptfd == -1)
679 sock_set_error();
680 return acceptfd;
683 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
684 return acceptfd;
687 /* accept a socket (creates a new fd) */
688 static struct sock *accept_socket( obj_handle_t handle )
690 struct sock *acceptsock;
691 struct sock *sock;
692 int acceptfd;
694 sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
695 if (!sock)
696 return NULL;
698 if ( sock->deferred )
700 acceptsock = sock->deferred;
701 sock->deferred = NULL;
703 else
705 if ((acceptfd = accept_new_fd( sock )) == -1)
707 release_object( sock );
708 return NULL;
710 if (!(acceptsock = alloc_object( &sock_ops )))
712 close( acceptfd );
713 release_object( sock );
714 return NULL;
717 init_sock( acceptsock );
718 /* newly created socket gets the same properties of the listening socket */
719 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
720 if (sock->state & FD_WINE_NONBLOCKING)
721 acceptsock->state |= FD_WINE_NONBLOCKING;
722 acceptsock->mask = sock->mask;
723 acceptsock->proto = sock->proto;
724 acceptsock->type = sock->type;
725 acceptsock->family = sock->family;
726 acceptsock->window = sock->window;
727 acceptsock->message = sock->message;
728 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
729 acceptsock->flags = sock->flags;
730 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
731 get_fd_options( sock->fd ) )))
733 release_object( acceptsock );
734 release_object( sock );
735 return NULL;
738 clear_error();
739 sock->pmask &= ~FD_ACCEPT;
740 sock->hmask &= ~FD_ACCEPT;
741 sock_reselect( sock );
742 release_object( sock );
743 return acceptsock;
746 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
748 int acceptfd;
749 struct fd *newfd;
750 if ( sock->deferred )
752 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
753 get_fd_options( acceptsock->fd ) );
754 if ( !newfd )
755 return FALSE;
757 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
759 release_object( sock->deferred );
760 sock->deferred = NULL;
762 else
764 if ((acceptfd = accept_new_fd( sock )) == -1)
765 return FALSE;
767 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
768 get_fd_options( acceptsock->fd ) )))
769 return FALSE;
772 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
773 acceptsock->hmask = 0;
774 acceptsock->pmask = 0;
775 acceptsock->polling = 0;
776 acceptsock->type = sock->type;
777 acceptsock->family = sock->family;
778 acceptsock->wparam = 0;
779 acceptsock->deferred = NULL;
780 release_object( acceptsock->fd );
781 acceptsock->fd = newfd;
783 clear_error();
784 sock->pmask &= ~FD_ACCEPT;
785 sock->hmask &= ~FD_ACCEPT;
786 sock_reselect( sock );
788 return TRUE;
791 /* return an errno value mapped to a WSA error */
792 static int sock_get_error( int err )
794 switch (err)
796 case EINTR: return WSAEINTR;
797 case EBADF: return WSAEBADF;
798 case EPERM:
799 case EACCES: return WSAEACCES;
800 case EFAULT: return WSAEFAULT;
801 case EINVAL: return WSAEINVAL;
802 case EMFILE: return WSAEMFILE;
803 case EWOULDBLOCK: return WSAEWOULDBLOCK;
804 case EINPROGRESS: return WSAEINPROGRESS;
805 case EALREADY: return WSAEALREADY;
806 case ENOTSOCK: return WSAENOTSOCK;
807 case EDESTADDRREQ: return WSAEDESTADDRREQ;
808 case EMSGSIZE: return WSAEMSGSIZE;
809 case EPROTOTYPE: return WSAEPROTOTYPE;
810 case ENOPROTOOPT: return WSAENOPROTOOPT;
811 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
812 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
813 case EOPNOTSUPP: return WSAEOPNOTSUPP;
814 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
815 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
816 case EADDRINUSE: return WSAEADDRINUSE;
817 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
818 case ENETDOWN: return WSAENETDOWN;
819 case ENETUNREACH: return WSAENETUNREACH;
820 case ENETRESET: return WSAENETRESET;
821 case ECONNABORTED: return WSAECONNABORTED;
822 case EPIPE:
823 case ECONNRESET: return WSAECONNRESET;
824 case ENOBUFS: return WSAENOBUFS;
825 case EISCONN: return WSAEISCONN;
826 case ENOTCONN: return WSAENOTCONN;
827 case ESHUTDOWN: return WSAESHUTDOWN;
828 case ETOOMANYREFS: return WSAETOOMANYREFS;
829 case ETIMEDOUT: return WSAETIMEDOUT;
830 case ECONNREFUSED: return WSAECONNREFUSED;
831 case ELOOP: return WSAELOOP;
832 case ENAMETOOLONG: return WSAENAMETOOLONG;
833 case EHOSTDOWN: return WSAEHOSTDOWN;
834 case EHOSTUNREACH: return WSAEHOSTUNREACH;
835 case ENOTEMPTY: return WSAENOTEMPTY;
836 #ifdef EPROCLIM
837 case EPROCLIM: return WSAEPROCLIM;
838 #endif
839 #ifdef EUSERS
840 case EUSERS: return WSAEUSERS;
841 #endif
842 #ifdef EDQUOT
843 case EDQUOT: return WSAEDQUOT;
844 #endif
845 #ifdef ESTALE
846 case ESTALE: return WSAESTALE;
847 #endif
848 #ifdef EREMOTE
849 case EREMOTE: return WSAEREMOTE;
850 #endif
852 case 0: return 0;
853 default:
854 errno = err;
855 perror("wineserver: sock_get_error() can't map error");
856 return WSAEFAULT;
860 static int sock_get_ntstatus( int err )
862 switch ( err )
864 case EBADF: return STATUS_INVALID_HANDLE;
865 case EBUSY: return STATUS_DEVICE_BUSY;
866 case EPERM:
867 case EACCES: return STATUS_ACCESS_DENIED;
868 case EFAULT: return STATUS_NO_MEMORY;
869 case EINVAL: return STATUS_INVALID_PARAMETER;
870 case ENFILE:
871 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
872 case EWOULDBLOCK: return STATUS_CANT_WAIT;
873 case EINPROGRESS: return STATUS_PENDING;
874 case EALREADY: return STATUS_NETWORK_BUSY;
875 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
876 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
877 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
878 case EPROTONOSUPPORT:
879 case ESOCKTNOSUPPORT:
880 case EPFNOSUPPORT:
881 case EAFNOSUPPORT:
882 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
883 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
884 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
885 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
886 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
887 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
888 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
889 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
890 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
891 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
892 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
893 case ENETDOWN: return STATUS_NETWORK_BUSY;
894 case EPIPE:
895 case ECONNRESET: return STATUS_CONNECTION_RESET;
896 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
898 case 0: return STATUS_SUCCESS;
899 default:
900 errno = err;
901 perror("wineserver: sock_get_ntstatus() can't map error");
902 return STATUS_UNSUCCESSFUL;
906 /* set the last error depending on errno */
907 static void sock_set_error(void)
909 set_error( sock_get_ntstatus( errno ) );
912 /* create a socket */
913 DECL_HANDLER(create_socket)
915 struct object *obj;
917 reply->handle = 0;
918 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
920 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
921 release_object( obj );
925 /* accept a socket */
926 DECL_HANDLER(accept_socket)
928 struct sock *sock;
930 reply->handle = 0;
931 if ((sock = accept_socket( req->lhandle )) != NULL)
933 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
934 sock->wparam = reply->handle; /* wparam for message is the socket handle */
935 sock_reselect( sock );
936 release_object( &sock->obj );
940 /* accept a socket into an initialized socket */
941 DECL_HANDLER(accept_into_socket)
943 struct sock *sock, *acceptsock;
944 const int all_attributes = FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|FILE_READ_DATA;
946 if (!(sock = (struct sock *)get_handle_obj( current->process, req->lhandle,
947 all_attributes, &sock_ops)))
948 return;
950 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, req->ahandle,
951 all_attributes, &sock_ops)))
953 release_object( sock );
954 return;
957 if (accept_into_socket( sock, acceptsock ))
959 acceptsock->wparam = req->ahandle; /* wparam for message is the socket handle */
960 sock_reselect( acceptsock );
962 release_object( acceptsock );
963 release_object( sock );
966 /* set socket event parameters */
967 DECL_HANDLER(set_socket_event)
969 struct sock *sock;
970 struct event *old_event;
972 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
973 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
974 old_event = sock->event;
975 sock->mask = req->mask;
976 sock->hmask &= ~req->mask; /* re-enable held events */
977 sock->event = NULL;
978 sock->window = req->window;
979 sock->message = req->msg;
980 sock->wparam = req->handle; /* wparam is the socket handle */
981 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
983 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
985 sock_reselect( sock );
987 sock->state |= FD_WINE_NONBLOCKING;
989 /* if a network event is pending, signal the event object
990 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
991 before a WSAEventSelect() was done on it.
992 (when dealing with Asynchronous socket) */
993 sock_wake_up( sock );
995 if (old_event) release_object( old_event ); /* we're through with it */
996 release_object( &sock->obj );
999 /* get socket event parameters */
1000 DECL_HANDLER(get_socket_event)
1002 struct sock *sock;
1003 int i;
1004 int errors[FD_MAX_EVENTS];
1006 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1007 if (!sock)
1009 reply->mask = 0;
1010 reply->pmask = 0;
1011 reply->state = 0;
1012 return;
1014 reply->mask = sock->mask;
1015 reply->pmask = sock->pmask;
1016 reply->state = sock->state;
1017 for (i = 0; i < FD_MAX_EVENTS; i++)
1018 errors[i] = sock_get_ntstatus(sock->errors[i]);
1020 set_reply_data( errors, min( get_reply_max_size(), sizeof(errors) ));
1022 if (req->service)
1024 if (req->c_event)
1026 struct event *cevent = get_event_obj( current->process, req->c_event,
1027 EVENT_MODIFY_STATE );
1028 if (cevent)
1030 reset_event( cevent );
1031 release_object( cevent );
1034 sock->pmask = 0;
1035 sock_reselect( sock );
1037 release_object( &sock->obj );
1040 /* re-enable pending socket events */
1041 DECL_HANDLER(enable_socket_event)
1043 struct sock *sock;
1045 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
1046 FILE_WRITE_ATTRIBUTES, &sock_ops)))
1047 return;
1049 /* for event-based notification, windows erases stale events */
1050 sock->pmask &= ~req->mask;
1052 sock->hmask &= ~req->mask;
1053 sock->state |= req->sstate;
1054 sock->state &= ~req->cstate;
1055 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
1057 sock_reselect( sock );
1059 release_object( &sock->obj );
1062 DECL_HANDLER(set_socket_deferred)
1064 struct sock *sock, *acceptsock;
1066 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
1067 if ( !sock )
1068 return;
1070 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
1071 if ( !acceptsock )
1073 release_object( sock );
1074 return;
1076 sock->deferred = acceptsock;
1077 release_object( sock );
1080 DECL_HANDLER(get_socket_info)
1082 struct sock *sock;
1084 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1085 if (!sock) return;
1087 reply->family = sock->family;
1088 reply->type = sock->type;
1089 reply->protocol = sock->proto;
1091 release_object( &sock->obj );