oleaut32/tests: Use a boolean constant for BOOL argument.
[wine.git] / server / sock.c
blob5ffb1fe37c021039ab66c027110f75a7e5870d44
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 #include <sys/time.h>
34 #include <sys/types.h>
35 #ifdef HAVE_SYS_SOCKET_H
36 # include <sys/socket.h>
37 #endif
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
40 #endif
41 #ifdef HAVE_SYS_FILIO_H
42 # include <sys/filio.h>
43 #endif
44 #include <time.h>
45 #include <unistd.h>
47 #include "ntstatus.h"
48 #define WIN32_NO_STATUS
49 #include "windef.h"
50 #include "winternl.h"
51 #include "winerror.h"
53 #include "process.h"
54 #include "file.h"
55 #include "handle.h"
56 #include "thread.h"
57 #include "request.h"
58 #include "user.h"
60 /* From winsock.h */
61 #define FD_MAX_EVENTS 10
62 #define FD_READ_BIT 0
63 #define FD_WRITE_BIT 1
64 #define FD_OOB_BIT 2
65 #define FD_ACCEPT_BIT 3
66 #define FD_CONNECT_BIT 4
67 #define FD_CLOSE_BIT 5
70 * Define flags to be used with the WSAAsyncSelect() call.
72 #define FD_READ 0x00000001
73 #define FD_WRITE 0x00000002
74 #define FD_OOB 0x00000004
75 #define FD_ACCEPT 0x00000008
76 #define FD_CONNECT 0x00000010
77 #define FD_CLOSE 0x00000020
79 /* internal per-socket flags */
80 #define FD_WINE_LISTENING 0x10000000
81 #define FD_WINE_NONBLOCKING 0x20000000
82 #define FD_WINE_CONNECTED 0x40000000
83 #define FD_WINE_RAW 0x80000000
84 #define FD_WINE_INTERNAL 0xFFFF0000
86 /* Constants for WSAIoctl() */
87 #define WSA_FLAG_OVERLAPPED 0x01
89 struct sock
91 struct object obj; /* object header */
92 struct fd *fd; /* socket file descriptor */
93 unsigned int state; /* status bits */
94 unsigned int mask; /* event mask */
95 unsigned int hmask; /* held (blocked) events */
96 unsigned int pmask; /* pending events */
97 unsigned int flags; /* socket flags */
98 int polling; /* is socket being polled? */
99 unsigned short proto; /* socket protocol */
100 unsigned short type; /* socket type */
101 unsigned short family; /* socket family */
102 struct event *event; /* event object */
103 user_handle_t window; /* window to send the message to */
104 unsigned int message; /* message to send */
105 obj_handle_t wparam; /* message wparam (socket handle) */
106 int errors[FD_MAX_EVENTS]; /* event errors */
107 struct sock *deferred; /* socket that waits for a deferred accept */
108 struct async_queue *read_q; /* queue for asynchronous reads */
109 struct async_queue *write_q; /* queue for asynchronous writes */
112 static void sock_dump( struct object *obj, int verbose );
113 static int sock_signaled( struct object *obj, struct wait_queue_entry *entry );
114 static struct fd *sock_get_fd( struct object *obj );
115 static void sock_destroy( struct object *obj );
117 static int sock_get_poll_events( struct fd *fd );
118 static void sock_poll_event( struct fd *fd, int event );
119 static enum server_fd_type sock_get_fd_type( struct fd *fd );
120 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
121 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
122 static void sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb );
124 static int sock_get_ntstatus( int err );
125 static int sock_get_error( int err );
126 static void sock_set_error(void);
128 static const struct object_ops sock_ops =
130 sizeof(struct sock), /* size */
131 sock_dump, /* dump */
132 no_get_type, /* get_type */
133 add_queue, /* add_queue */
134 remove_queue, /* remove_queue */
135 sock_signaled, /* signaled */
136 no_satisfied, /* satisfied */
137 no_signal, /* signal */
138 sock_get_fd, /* get_fd */
139 default_fd_map_access, /* map_access */
140 default_get_sd, /* get_sd */
141 default_set_sd, /* set_sd */
142 no_lookup_name, /* lookup_name */
143 no_open_file, /* open_file */
144 fd_close_handle, /* close_handle */
145 sock_destroy /* destroy */
148 static const struct fd_ops sock_fd_ops =
150 sock_get_poll_events, /* get_poll_events */
151 sock_poll_event, /* poll_event */
152 no_flush, /* flush */
153 sock_get_fd_type, /* get_fd_type */
154 default_fd_ioctl, /* ioctl */
155 sock_queue_async, /* queue_async */
156 sock_reselect_async, /* reselect_async */
157 sock_cancel_async /* cancel_async */
161 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
162 * we post messages if there are multiple events. Used to send
163 * messages. The problem is if there is both a FD_CONNECT event and,
164 * say, an FD_READ event available on the same socket, we want to
165 * notify the app of the connect event first. Otherwise it may
166 * discard the read event because it thinks it hasn't connected yet.
168 static const int event_bitorder[FD_MAX_EVENTS] =
170 FD_CONNECT_BIT,
171 FD_ACCEPT_BIT,
172 FD_OOB_BIT,
173 FD_WRITE_BIT,
174 FD_READ_BIT,
175 FD_CLOSE_BIT,
176 6, 7, 8, 9 /* leftovers */
179 /* Flags that make sense only for SOCK_STREAM sockets */
180 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
182 typedef enum {
183 SOCK_SHUTDOWN_ERROR = -1,
184 SOCK_SHUTDOWN_EOF = 0,
185 SOCK_SHUTDOWN_POLLHUP = 1
186 } sock_shutdown_t;
188 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
190 static sock_shutdown_t sock_check_pollhup(void)
192 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
193 int fd[2], n;
194 struct pollfd pfd;
195 char dummy;
197 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
198 if ( shutdown( fd[0], 1 ) ) goto out;
200 pfd.fd = fd[1];
201 pfd.events = POLLIN;
202 pfd.revents = 0;
204 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
205 n = poll( &pfd, 1, 1 );
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;
250 allow_fd_caching( sock->fd );
252 /* update condition mask */
253 set_fd_events( sock->fd, ev );
254 return ev;
257 /* wake anybody waiting on the socket event or send the associated message */
258 static void sock_wake_up( struct sock *sock )
260 unsigned int events = sock->pmask & sock->mask;
261 int i;
263 if ( !events ) return;
265 if (sock->event)
267 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
268 set_event( sock->event );
270 if (sock->window)
272 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
273 for (i = 0; i < FD_MAX_EVENTS; i++)
275 int event = event_bitorder[i];
276 if (sock->pmask & (1 << event))
278 lparam_t lparam = (1 << event) | (sock_get_error(sock->errors[event]) << 16);
279 post_message( sock->window, sock->message, sock->wparam, lparam );
282 sock->pmask = 0;
283 sock_reselect( sock );
287 static inline int sock_error( struct fd *fd )
289 unsigned int optval = 0;
290 socklen_t optlen = sizeof(optval);
292 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
293 return optval;
296 static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
298 if ( sock->flags & WSA_FLAG_OVERLAPPED )
300 if ( event & (POLLIN|POLLPRI) && async_waiting( sock->read_q ) )
302 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
303 async_wake_up( sock->read_q, STATUS_ALERTED );
304 event &= ~(POLLIN|POLLPRI);
306 if ( event & POLLOUT && async_waiting( sock->write_q ) )
308 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
309 async_wake_up( sock->write_q, STATUS_ALERTED );
310 event &= ~POLLOUT;
312 if ( event & (POLLERR|POLLHUP) )
314 int status = sock_get_ntstatus( error );
316 if ( !(sock->state & FD_READ) )
317 async_wake_up( sock->read_q, status );
318 if ( !(sock->state & FD_WRITE) )
319 async_wake_up( sock->write_q, status );
322 return event;
325 static void sock_dispatch_events( struct sock *sock, int prevstate, int event, int error )
327 if (prevstate & FD_CONNECT)
329 sock->pmask |= FD_CONNECT;
330 sock->hmask |= FD_CONNECT;
331 sock->errors[FD_CONNECT_BIT] = error;
332 goto end;
334 if (prevstate & FD_WINE_LISTENING)
336 sock->pmask |= FD_ACCEPT;
337 sock->hmask |= FD_ACCEPT;
338 sock->errors[FD_ACCEPT_BIT] = error;
339 goto end;
342 if (event & POLLIN)
344 sock->pmask |= FD_READ;
345 sock->hmask |= FD_READ;
346 sock->errors[FD_READ_BIT] = 0;
349 if (event & POLLOUT)
351 sock->pmask |= FD_WRITE;
352 sock->hmask |= FD_WRITE;
353 sock->errors[FD_WRITE_BIT] = 0;
356 if (event & POLLPRI)
358 sock->pmask |= FD_OOB;
359 sock->hmask |= FD_OOB;
360 sock->errors[FD_OOB_BIT] = 0;
363 if (event & (POLLERR|POLLHUP))
365 sock->pmask |= FD_CLOSE;
366 sock->hmask |= FD_CLOSE;
367 sock->errors[FD_CLOSE_BIT] = error;
369 end:
370 sock_wake_up( sock );
373 static void sock_poll_event( struct fd *fd, int event )
375 struct sock *sock = get_fd_user( fd );
376 int hangup_seen = 0;
377 int prevstate = sock->state;
378 int error = 0;
380 assert( sock->obj.ops == &sock_ops );
381 if (debug_level)
382 fprintf(stderr, "socket %p select event: %x\n", sock, event);
384 /* we may change event later, remove from loop here */
385 if (event & (POLLERR|POLLHUP)) set_fd_events( sock->fd, -1 );
387 if (sock->state & FD_CONNECT)
389 if (event & (POLLERR|POLLHUP))
391 /* we didn't get connected? */
392 sock->state &= ~FD_CONNECT;
393 event &= ~POLLOUT;
394 error = sock_error( fd );
396 else if (event & POLLOUT)
398 /* we got connected */
399 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
400 sock->state &= ~FD_CONNECT;
403 else if (sock->state & FD_WINE_LISTENING)
405 /* listening */
406 if (event & (POLLERR|POLLHUP))
407 error = sock_error( fd );
409 else
411 /* normal data flow */
412 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
414 char dummy;
415 int nr;
417 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
418 * has been closed, so we need to check for it explicitly here */
419 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
420 if ( nr == 0 )
422 hangup_seen = 1;
423 event &= ~POLLIN;
425 else if ( nr < 0 )
427 event &= ~POLLIN;
428 /* EAGAIN can happen if an async recv() falls between the server's poll()
429 call and the invocation of this routine */
430 if ( errno != EAGAIN )
432 error = errno;
433 event |= POLLERR;
434 if ( debug_level )
435 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
440 if ( (hangup_seen || event & (POLLHUP|POLLERR)) && (sock->state & (FD_READ|FD_WRITE)) )
442 error = error ? error : sock_error( fd );
443 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
444 sock->state &= ~FD_WRITE;
445 sock->state &= ~FD_READ;
447 if (debug_level)
448 fprintf(stderr, "socket %p aborted by error %d, event: %x\n", sock, error, event);
451 if (hangup_seen)
452 event |= POLLHUP;
455 event = sock_dispatch_asyncs( sock, event, error );
456 sock_dispatch_events( sock, prevstate, event, error );
458 /* if anyone is stupid enough to wait on the socket object itself,
459 * maybe we should wake them up too, just in case? */
460 wake_up( &sock->obj, 0 );
462 sock_reselect( sock );
465 static void sock_dump( struct object *obj, int verbose )
467 struct sock *sock = (struct sock *)obj;
468 assert( obj->ops == &sock_ops );
469 fprintf( stderr, "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
470 sock->fd, sock->state,
471 sock->mask, sock->pmask, sock->hmask );
474 static int sock_signaled( struct object *obj, struct wait_queue_entry *entry )
476 struct sock *sock = (struct sock *)obj;
477 assert( obj->ops == &sock_ops );
479 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
482 static int sock_get_poll_events( struct fd *fd )
484 struct sock *sock = get_fd_user( fd );
485 unsigned int mask = sock->mask & ~sock->hmask;
486 unsigned int smask = sock->state & mask;
487 int ev = 0;
489 assert( sock->obj.ops == &sock_ops );
491 if (sock->state & FD_CONNECT)
492 /* connecting, wait for writable */
493 return POLLOUT;
495 if ( async_queued( sock->read_q ) )
497 if ( async_waiting( sock->read_q ) ) ev |= POLLIN | POLLPRI;
499 else if (smask & FD_READ || (sock->state & FD_WINE_LISTENING && mask & FD_ACCEPT))
500 ev |= POLLIN | POLLPRI;
501 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
502 else if ( sock->type == SOCK_STREAM && sock->state & FD_READ && mask & FD_CLOSE &&
503 !(sock->hmask & FD_READ) )
504 ev |= POLLIN;
506 if ( async_queued( sock->write_q ) )
508 if ( async_waiting( sock->write_q ) ) ev |= POLLOUT;
510 else if (smask & FD_WRITE)
511 ev |= POLLOUT;
513 return ev;
516 static enum server_fd_type sock_get_fd_type( struct fd *fd )
518 return FD_TYPE_SOCKET;
521 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
523 struct sock *sock = get_fd_user( fd );
524 struct async *async;
525 struct async_queue *queue;
527 assert( sock->obj.ops == &sock_ops );
529 switch (type)
531 case ASYNC_TYPE_READ:
532 if (!sock->read_q && !(sock->read_q = create_async_queue( sock->fd ))) return;
533 queue = sock->read_q;
534 break;
535 case ASYNC_TYPE_WRITE:
536 if (!sock->write_q && !(sock->write_q = create_async_queue( sock->fd ))) return;
537 queue = sock->write_q;
538 break;
539 default:
540 set_error( STATUS_INVALID_PARAMETER );
541 return;
544 if ( ( !( sock->state & (FD_READ|FD_CONNECT|FD_WINE_LISTENING) ) && type == ASYNC_TYPE_READ ) ||
545 ( !( sock->state & (FD_WRITE|FD_CONNECT) ) && type == ASYNC_TYPE_WRITE ) )
547 set_error( STATUS_PIPE_DISCONNECTED );
548 return;
551 if (!(async = create_async( current, queue, data ))) return;
552 release_object( async );
554 sock_reselect( sock );
556 set_error( STATUS_PENDING );
559 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
561 struct sock *sock = get_fd_user( fd );
562 sock_reselect( sock );
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 static void init_sock(struct sock *sock)
606 sock->state = 0;
607 sock->mask = 0;
608 sock->hmask = 0;
609 sock->pmask = 0;
610 sock->polling = 0;
611 sock->flags = 0;
612 sock->type = 0;
613 sock->family = 0;
614 sock->event = NULL;
615 sock->window = 0;
616 sock->message = 0;
617 sock->wparam = 0;
618 sock->deferred = NULL;
619 sock->read_q = NULL;
620 sock->write_q = NULL;
621 memset( sock->errors, 0, sizeof(sock->errors) );
624 /* create a new and unconnected socket */
625 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
627 struct sock *sock;
628 int sockfd;
630 sockfd = socket( family, type, protocol );
631 if (debug_level)
632 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
633 if (sockfd == -1)
635 sock_set_error();
636 return NULL;
638 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
639 if (!(sock = alloc_object( &sock_ops )))
641 close( sockfd );
642 return NULL;
644 init_sock( sock );
645 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
646 sock->flags = flags;
647 sock->proto = protocol;
648 sock->type = type;
649 sock->family = family;
651 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
652 (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
654 release_object( sock );
655 return NULL;
657 sock_reselect( sock );
658 clear_error();
659 return &sock->obj;
662 /* accepts a socket and inits it */
663 static int accept_new_fd( struct sock *sock )
666 /* Try to accept(2). We can't be safe that this an already connected socket
667 * or that accept() is allowed on it. In those cases we will get -1/errno
668 * return.
670 int acceptfd;
671 struct sockaddr saddr;
672 socklen_t slen = sizeof(saddr);
673 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
674 if (acceptfd == -1)
676 sock_set_error();
677 return acceptfd;
680 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
681 return acceptfd;
684 /* accept a socket (creates a new fd) */
685 static struct sock *accept_socket( obj_handle_t handle )
687 struct sock *acceptsock;
688 struct sock *sock;
689 int acceptfd;
691 sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
692 if (!sock)
693 return NULL;
695 if ( sock->deferred )
697 acceptsock = sock->deferred;
698 sock->deferred = NULL;
700 else
702 if ((acceptfd = accept_new_fd( sock )) == -1)
704 release_object( sock );
705 return NULL;
707 if (!(acceptsock = alloc_object( &sock_ops )))
709 close( acceptfd );
710 release_object( sock );
711 return NULL;
714 init_sock( acceptsock );
715 /* newly created socket gets the same properties of the listening socket */
716 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
717 if (sock->state & FD_WINE_NONBLOCKING)
718 acceptsock->state |= FD_WINE_NONBLOCKING;
719 acceptsock->mask = sock->mask;
720 acceptsock->proto = sock->proto;
721 acceptsock->type = sock->type;
722 acceptsock->family = sock->family;
723 acceptsock->window = sock->window;
724 acceptsock->message = sock->message;
725 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
726 acceptsock->flags = sock->flags;
727 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
728 get_fd_options( sock->fd ) )))
730 release_object( acceptsock );
731 release_object( sock );
732 return NULL;
735 clear_error();
736 sock->pmask &= ~FD_ACCEPT;
737 sock->hmask &= ~FD_ACCEPT;
738 sock_reselect( sock );
739 release_object( sock );
740 return acceptsock;
743 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
745 int acceptfd;
746 struct fd *newfd;
747 if ( sock->deferred )
749 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
750 get_fd_options( acceptsock->fd ) );
751 if ( !newfd )
752 return FALSE;
754 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
756 release_object( sock->deferred );
757 sock->deferred = NULL;
759 else
761 if ((acceptfd = accept_new_fd( sock )) == -1)
762 return FALSE;
764 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
765 get_fd_options( acceptsock->fd ) )))
766 return FALSE;
769 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
770 acceptsock->hmask = 0;
771 acceptsock->pmask = 0;
772 acceptsock->polling = 0;
773 acceptsock->type = sock->type;
774 acceptsock->family = sock->family;
775 acceptsock->wparam = 0;
776 acceptsock->deferred = NULL;
777 release_object( acceptsock->fd );
778 acceptsock->fd = newfd;
780 clear_error();
781 sock->pmask &= ~FD_ACCEPT;
782 sock->hmask &= ~FD_ACCEPT;
783 sock_reselect( sock );
785 return TRUE;
788 /* return an errno value mapped to a WSA error */
789 static int sock_get_error( int err )
791 switch (err)
793 case EINTR: return WSAEINTR;
794 case EBADF: return WSAEBADF;
795 case EPERM:
796 case EACCES: return WSAEACCES;
797 case EFAULT: return WSAEFAULT;
798 case EINVAL: return WSAEINVAL;
799 case EMFILE: return WSAEMFILE;
800 case EWOULDBLOCK: return WSAEWOULDBLOCK;
801 case EINPROGRESS: return WSAEINPROGRESS;
802 case EALREADY: return WSAEALREADY;
803 case ENOTSOCK: return WSAENOTSOCK;
804 case EDESTADDRREQ: return WSAEDESTADDRREQ;
805 case EMSGSIZE: return WSAEMSGSIZE;
806 case EPROTOTYPE: return WSAEPROTOTYPE;
807 case ENOPROTOOPT: return WSAENOPROTOOPT;
808 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
809 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
810 case EOPNOTSUPP: return WSAEOPNOTSUPP;
811 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
812 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
813 case EADDRINUSE: return WSAEADDRINUSE;
814 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
815 case ENETDOWN: return WSAENETDOWN;
816 case ENETUNREACH: return WSAENETUNREACH;
817 case ENETRESET: return WSAENETRESET;
818 case ECONNABORTED: return WSAECONNABORTED;
819 case EPIPE:
820 case ECONNRESET: return WSAECONNRESET;
821 case ENOBUFS: return WSAENOBUFS;
822 case EISCONN: return WSAEISCONN;
823 case ENOTCONN: return WSAENOTCONN;
824 case ESHUTDOWN: return WSAESHUTDOWN;
825 case ETOOMANYREFS: return WSAETOOMANYREFS;
826 case ETIMEDOUT: return WSAETIMEDOUT;
827 case ECONNREFUSED: return WSAECONNREFUSED;
828 case ELOOP: return WSAELOOP;
829 case ENAMETOOLONG: return WSAENAMETOOLONG;
830 case EHOSTDOWN: return WSAEHOSTDOWN;
831 case EHOSTUNREACH: return WSAEHOSTUNREACH;
832 case ENOTEMPTY: return WSAENOTEMPTY;
833 #ifdef EPROCLIM
834 case EPROCLIM: return WSAEPROCLIM;
835 #endif
836 #ifdef EUSERS
837 case EUSERS: return WSAEUSERS;
838 #endif
839 #ifdef EDQUOT
840 case EDQUOT: return WSAEDQUOT;
841 #endif
842 #ifdef ESTALE
843 case ESTALE: return WSAESTALE;
844 #endif
845 #ifdef EREMOTE
846 case EREMOTE: return WSAEREMOTE;
847 #endif
849 case 0: return 0;
850 default:
851 errno = err;
852 perror("wineserver: sock_get_error() can't map error");
853 return WSAEFAULT;
857 static int sock_get_ntstatus( int err )
859 switch ( err )
861 case EBADF: return STATUS_INVALID_HANDLE;
862 case EBUSY: return STATUS_DEVICE_BUSY;
863 case EPERM:
864 case EACCES: return STATUS_ACCESS_DENIED;
865 case EFAULT: return STATUS_NO_MEMORY;
866 case EINVAL: return STATUS_INVALID_PARAMETER;
867 case ENFILE:
868 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
869 case EWOULDBLOCK: return STATUS_CANT_WAIT;
870 case EINPROGRESS: return STATUS_PENDING;
871 case EALREADY: return STATUS_NETWORK_BUSY;
872 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
873 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
874 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
875 case EPROTONOSUPPORT:
876 case ESOCKTNOSUPPORT:
877 case EPFNOSUPPORT:
878 case EAFNOSUPPORT:
879 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
880 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
881 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
882 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
883 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
884 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
885 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
886 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
887 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
888 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
889 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
890 case ENETDOWN: return STATUS_NETWORK_BUSY;
891 case EPIPE:
892 case ECONNRESET: return STATUS_CONNECTION_RESET;
893 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
895 case 0: return STATUS_SUCCESS;
896 default:
897 errno = err;
898 perror("wineserver: sock_get_ntstatus() can't map error");
899 return STATUS_UNSUCCESSFUL;
903 /* set the last error depending on errno */
904 static void sock_set_error(void)
906 set_error( sock_get_ntstatus( errno ) );
909 /* create a socket */
910 DECL_HANDLER(create_socket)
912 struct object *obj;
914 reply->handle = 0;
915 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
917 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
918 release_object( obj );
922 /* accept a socket */
923 DECL_HANDLER(accept_socket)
925 struct sock *sock;
927 reply->handle = 0;
928 if ((sock = accept_socket( req->lhandle )) != NULL)
930 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
931 sock->wparam = reply->handle; /* wparam for message is the socket handle */
932 sock_reselect( sock );
933 release_object( &sock->obj );
937 /* accept a socket into an initialized socket */
938 DECL_HANDLER(accept_into_socket)
940 struct sock *sock, *acceptsock;
941 const int all_attributes = FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|FILE_READ_DATA;
943 if (!(sock = (struct sock *)get_handle_obj( current->process, req->lhandle,
944 all_attributes, &sock_ops)))
945 return;
947 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, req->ahandle,
948 all_attributes, &sock_ops)))
950 release_object( sock );
951 return;
954 if (accept_into_socket( sock, acceptsock ))
956 acceptsock->wparam = req->ahandle; /* wparam for message is the socket handle */
957 sock_reselect( acceptsock );
959 release_object( acceptsock );
960 release_object( sock );
963 /* set socket event parameters */
964 DECL_HANDLER(set_socket_event)
966 struct sock *sock;
967 struct event *old_event;
969 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
970 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
971 old_event = sock->event;
972 sock->mask = req->mask;
973 sock->hmask &= ~req->mask; /* re-enable held events */
974 sock->event = NULL;
975 sock->window = req->window;
976 sock->message = req->msg;
977 sock->wparam = req->handle; /* wparam is the socket handle */
978 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
980 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
982 sock_reselect( sock );
984 sock->state |= FD_WINE_NONBLOCKING;
986 /* if a network event is pending, signal the event object
987 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
988 before a WSAEventSelect() was done on it.
989 (when dealing with Asynchronous socket) */
990 sock_wake_up( sock );
992 if (old_event) release_object( old_event ); /* we're through with it */
993 release_object( &sock->obj );
996 /* get socket event parameters */
997 DECL_HANDLER(get_socket_event)
999 struct sock *sock;
1000 int i;
1001 int errors[FD_MAX_EVENTS];
1003 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1004 if (!sock)
1006 reply->mask = 0;
1007 reply->pmask = 0;
1008 reply->state = 0;
1009 return;
1011 reply->mask = sock->mask;
1012 reply->pmask = sock->pmask;
1013 reply->state = sock->state;
1014 for (i = 0; i < FD_MAX_EVENTS; i++)
1015 errors[i] = sock_get_ntstatus(sock->errors[i]);
1017 set_reply_data( errors, min( get_reply_max_size(), sizeof(errors) ));
1019 if (req->service)
1021 if (req->c_event)
1023 struct event *cevent = get_event_obj( current->process, req->c_event,
1024 EVENT_MODIFY_STATE );
1025 if (cevent)
1027 reset_event( cevent );
1028 release_object( cevent );
1031 sock->pmask = 0;
1032 sock_reselect( sock );
1034 release_object( &sock->obj );
1037 /* re-enable pending socket events */
1038 DECL_HANDLER(enable_socket_event)
1040 struct sock *sock;
1042 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
1043 FILE_WRITE_ATTRIBUTES, &sock_ops)))
1044 return;
1046 /* for event-based notification, windows erases stale events */
1047 sock->pmask &= ~req->mask;
1049 sock->hmask &= ~req->mask;
1050 sock->state |= req->sstate;
1051 sock->state &= ~req->cstate;
1052 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
1054 sock_reselect( sock );
1056 release_object( &sock->obj );
1059 DECL_HANDLER(set_socket_deferred)
1061 struct sock *sock, *acceptsock;
1063 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
1064 if ( !sock )
1065 return;
1067 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
1068 if ( !acceptsock )
1070 release_object( sock );
1071 return;
1073 sock->deferred = acceptsock;
1074 release_object( sock );
1077 DECL_HANDLER(get_socket_info)
1079 struct sock *sock;
1081 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1082 if (!sock) return;
1084 reply->family = sock->family;
1085 reply->type = sock->type;
1086 reply->protocol = sock->proto;
1088 release_object( &sock->obj );