pop 5dd396018caae1ce9bdc685317d9d97425e08481
[wine/hacks.git] / server / sock.c
blob75e6232ca0fe0be1d4a7dcce4997b5c66e803ae0
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_ACCEPTING 0x08000000
84 #define FD_WINE_LISTENING 0x10000000
85 #define FD_WINE_NONBLOCKING 0x20000000
86 #define FD_WINE_CONNECTED 0x40000000
87 #define FD_WINE_RAW 0x80000000
88 #define FD_WINE_INTERNAL 0xFFFF0000
90 /* Constants for WSAIoctl() */
91 #define WSA_FLAG_OVERLAPPED 0x01
93 struct sock
95 struct object obj; /* object header */
96 struct fd *fd; /* socket file descriptor */
97 unsigned int state; /* status bits */
98 unsigned int mask; /* event mask */
99 unsigned int hmask; /* held (blocked) events */
100 unsigned int pmask; /* pending events */
101 unsigned int flags; /* socket flags */
102 int polling; /* is socket being polled? */
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 *async; /* pending accept to this socket */
112 struct list accentry; /* entry in the list below for the request */
113 struct list paccepts; /* pending accepts on this socket */
114 struct async_queue *read_q; /* queue for asynchronous reads */
115 struct async_queue *write_q; /* queue for asynchronous writes */
118 static void sock_dump( struct object *obj, int verbose );
119 static int sock_signaled( struct object *obj, struct thread *thread );
120 static struct fd *sock_get_fd( struct object *obj );
121 static void sock_destroy( struct object *obj );
123 static int sock_get_poll_events( struct fd *fd );
124 static void sock_poll_event( struct fd *fd, int event );
125 static enum server_fd_type sock_get_fd_type( struct fd *fd );
126 static int sock_removable( struct fd *fd );
127 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
128 static void sock_async_event( struct fd *fd, struct async_queue *queue, struct async *async, int status, int finished );
129 static int sock_async_terminated( struct fd *fd, struct async_queue *queue, struct async *async, int status );
130 static void sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb );
132 static int sock_get_ntstatus( int err );
133 static int sock_get_error( int err );
134 static void sock_set_error(void);
135 static int accept_into_socket( struct sock *sock, struct sock *acceptsock );
137 static const struct object_ops sock_ops =
139 sizeof(struct sock), /* size */
140 sock_dump, /* dump */
141 no_get_type, /* get_type */
142 add_queue, /* add_queue */
143 remove_queue, /* remove_queue */
144 sock_signaled, /* signaled */
145 no_satisfied, /* satisfied */
146 no_signal, /* signal */
147 sock_get_fd, /* get_fd */
148 default_fd_map_access, /* map_access */
149 default_get_sd, /* get_sd */
150 default_set_sd, /* set_sd */
151 no_lookup_name, /* lookup_name */
152 no_open_file, /* open_file */
153 fd_close_handle, /* close_handle */
154 sock_destroy /* destroy */
157 static const struct fd_ops sock_fd_ops =
159 sock_get_poll_events, /* get_poll_events */
160 sock_poll_event, /* poll_event */
161 no_flush, /* flush */
162 sock_get_fd_type, /* get_file_info */
163 sock_removable, /* removable */
164 default_fd_ioctl, /* ioctl */
165 sock_queue_async, /* queue_async */
166 sock_async_event, /* async_event */
167 sock_async_terminated, /* async_terminated */
168 sock_cancel_async /* cancel_async */
172 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
173 * we post messages if there are multiple events. Used to send
174 * messages. The problem is if there is both a FD_CONNECT event and,
175 * say, an FD_READ event available on the same socket, we want to
176 * notify the app of the connect event first. Otherwise it may
177 * discard the read event because it thinks it hasn't connected yet.
179 static const int event_bitorder[FD_MAX_EVENTS] =
181 FD_CONNECT_BIT,
182 FD_ACCEPT_BIT,
183 FD_OOB_BIT,
184 FD_WRITE_BIT,
185 FD_READ_BIT,
186 FD_CLOSE_BIT,
187 6, 7, 8, 9 /* leftovers */
190 /* Flags that make sense only for SOCK_STREAM sockets */
191 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
193 typedef enum {
194 SOCK_SHUTDOWN_ERROR = -1,
195 SOCK_SHUTDOWN_EOF = 0,
196 SOCK_SHUTDOWN_POLLHUP = 1
197 } sock_shutdown_t;
199 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
201 static sock_shutdown_t sock_check_pollhup(void)
203 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
204 int fd[2], n;
205 struct pollfd pfd;
206 char dummy;
208 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) goto out;
209 if ( shutdown( fd[0], 1 ) ) goto out;
211 pfd.fd = fd[1];
212 pfd.events = POLLIN;
213 pfd.revents = 0;
215 n = poll( &pfd, 1, 0 );
216 if ( n != 1 ) goto out; /* error or timeout */
217 if ( pfd.revents & POLLHUP )
218 ret = SOCK_SHUTDOWN_POLLHUP;
219 else if ( pfd.revents & POLLIN &&
220 read( fd[1], &dummy, 1 ) == 0 )
221 ret = SOCK_SHUTDOWN_EOF;
223 out:
224 close( fd[0] );
225 close( fd[1] );
226 return ret;
229 void sock_init(void)
231 sock_shutdown_type = sock_check_pollhup();
233 switch ( sock_shutdown_type )
235 case SOCK_SHUTDOWN_EOF:
236 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
237 break;
238 case SOCK_SHUTDOWN_POLLHUP:
239 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
240 break;
241 default:
242 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
243 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
247 static int sock_reselect( struct sock *sock )
249 int ev = sock_get_poll_events( sock->fd );
251 if (debug_level)
252 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
254 if (!sock->polling) /* FIXME: should find a better way to do this */
256 /* previously unconnected socket, is this reselect supposed to connect it? */
257 if (!(sock->state & ~(FD_WINE_NONBLOCKING|FD_WINE_ACCEPTING))) return 0;
258 /* ok, it is, attach it to the wineserver's main poll loop */
259 sock->polling = 1;
261 /* update condition mask */
262 set_fd_events( sock->fd, ev );
263 return ev;
266 /* wake anybody waiting on the socket event or send the associated message */
267 static void sock_wake_up( struct sock *sock, int pollev )
269 unsigned int events = sock->pmask & sock->mask;
270 int i;
271 int async_active = 0;
273 if ( pollev & (POLLIN|POLLPRI|POLLERR|POLLHUP) && async_waiting( sock->read_q ))
275 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
276 async_wake_up( sock->read_q, STATUS_ALERTED );
277 async_active = 1;
279 if ( pollev & (POLLOUT|POLLERR|POLLHUP) && async_waiting( sock->write_q ))
281 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
282 async_wake_up( sock->write_q, STATUS_ALERTED );
283 async_active = 1;
286 /* Do not signal events if there are still pending asynchronous IO requests */
287 /* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
288 if ( !events || async_active ) return;
290 if (sock->event)
292 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
293 set_event( sock->event );
295 if (sock->window)
297 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
298 for (i = 0; i < FD_MAX_EVENTS; i++)
300 int event = event_bitorder[i];
301 if (sock->pmask & (1 << event))
303 lparam_t lparam = (1 << event) | (sock_get_error(sock->errors[event]) << 16);
304 post_message( sock->window, sock->message, sock->wparam, lparam );
307 sock->pmask = 0;
308 sock_reselect( sock );
312 static inline int sock_error( struct fd *fd )
314 unsigned int optval = 0, optlen;
316 optlen = sizeof(optval);
317 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
318 return optval;
321 static inline void sock_free_accept_async( struct sock *acceptsock )
323 list_remove( &acceptsock->accentry );
324 acceptsock->state &= ~FD_WINE_ACCEPTING;
325 acceptsock->async = NULL;
328 static void sock_poll_event( struct fd *fd, int event )
330 struct sock *sock = get_fd_user( fd );
331 int hangup_seen = 0;
333 assert( sock->obj.ops == &sock_ops );
334 if (debug_level)
335 fprintf(stderr, "socket %p select event: %x\n", sock, event);
337 /* we may change event later, remove from loop here */
338 if (event & (POLLERR|POLLHUP)) set_fd_events( sock->fd, -1 );
340 if (sock->state & FD_CONNECT)
342 /* connecting */
343 if (event & POLLOUT)
345 /* we got connected */
346 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
347 sock->state &= ~FD_CONNECT;
348 sock->pmask |= FD_CONNECT;
349 sock->errors[FD_CONNECT_BIT] = 0;
350 if (debug_level)
351 fprintf(stderr, "socket %p connection success\n", sock);
353 else if (event & (POLLERR|POLLHUP))
355 /* we didn't get connected? */
356 sock->state &= ~FD_CONNECT;
357 sock->pmask |= FD_CONNECT;
358 sock->errors[FD_CONNECT_BIT] = sock_error( fd );
359 if (debug_level)
360 fprintf(stderr, "socket %p connection failure\n", sock);
363 else if (sock->state & FD_WINE_LISTENING)
365 /* listening */
366 if (event & POLLIN)
368 /* incoming connection */
369 sock->pmask |= FD_ACCEPT;
370 sock->errors[FD_ACCEPT_BIT] = 0;
371 sock->hmask |= FD_ACCEPT;
373 else if (event & (POLLERR|POLLHUP))
375 /* failed incoming connection? */
376 sock->pmask |= FD_ACCEPT;
377 sock->errors[FD_ACCEPT_BIT] = sock_error( fd );
378 sock->hmask |= FD_ACCEPT;
381 else
383 /* normal data flow */
384 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
386 char dummy;
387 int nr;
389 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
390 * has been closed, so we need to check for it explicitly here */
391 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
392 if ( nr == 0 )
394 hangup_seen = 1;
395 event &= ~POLLIN;
397 else if ( nr < 0 )
399 event &= ~POLLIN;
400 /* EAGAIN can happen if an async recv() falls between the server's poll()
401 call and the invocation of this routine */
402 if ( errno != EAGAIN )
404 if ( debug_level )
405 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
406 event |= POLLERR;
411 if ( event & POLLIN )
413 sock->pmask |= FD_READ;
414 sock->hmask |= FD_READ;
415 sock->errors[FD_READ_BIT] = 0;
416 if (debug_level)
417 fprintf(stderr, "socket %p is readable\n", sock );
420 if (event & POLLOUT)
422 sock->pmask |= FD_WRITE;
423 sock->hmask |= FD_WRITE;
424 sock->errors[FD_WRITE_BIT] = 0;
425 if (debug_level)
426 fprintf(stderr, "socket %p is writable\n", sock);
428 if (event & POLLPRI)
430 sock->pmask |= FD_OOB;
431 sock->hmask |= FD_OOB;
432 sock->errors[FD_OOB_BIT] = 0;
433 if (debug_level)
434 fprintf(stderr, "socket %p got OOB data\n", sock);
437 if ( (hangup_seen || event & (POLLHUP|POLLERR)) && (sock->state & (FD_READ|FD_WRITE)) )
439 sock->errors[FD_CLOSE_BIT] = sock_error( fd );
440 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
441 sock->state &= ~FD_WRITE;
442 sock->state &= ~FD_READ;
444 sock->pmask |= FD_CLOSE;
445 sock->hmask |= FD_CLOSE;
446 if (debug_level)
447 fprintf(stderr, "socket %p aborted by error %d, event: %x - removing from select loop\n",
448 sock, sock->errors[FD_CLOSE_BIT], event);
451 if (hangup_seen)
452 event |= POLLHUP;
455 /* wake up anyone waiting for whatever just happened */
456 if ( sock->pmask & sock->mask || sock->flags & WSA_FLAG_OVERLAPPED ) sock_wake_up( sock, event );
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 printf( "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 thread *thread )
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->state & ~sock->hmask;
486 int ev = 0;
488 assert( sock->obj.ops == &sock_ops );
490 if (sock->state & FD_CONNECT)
491 /* connecting, wait for writable */
492 return POLLOUT;
493 if (sock->state & FD_WINE_LISTENING)
494 /* listening, wait for readable */
495 return (!(sock->hmask & FD_ACCEPT) || async_waiting( sock->read_q )) ? POLLIN : 0;
497 if (mask & FD_READ || async_waiting( sock->read_q )) ev |= POLLIN | POLLPRI;
498 if (mask & FD_WRITE || async_waiting( sock->write_q )) ev |= POLLOUT;
499 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
500 if ( sock->type == SOCK_STREAM && ( sock->mask & ~sock->hmask & FD_CLOSE) &&
501 !(sock->hmask & FD_READ) && sock->state & FD_READ )
502 ev |= POLLIN;
504 return ev;
507 static enum server_fd_type sock_get_fd_type( struct fd *fd )
509 return FD_TYPE_SOCKET;
512 static int sock_removable( struct fd *fd )
514 struct sock *sock = get_fd_user( fd );
515 assert( sock->obj.ops == &sock_ops );
517 /* Don't cache SOCK_STREAM sockets until connected (needed for AcceptEx) */
518 return sock->type == SOCK_STREAM && !sock->polling;
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_queue *queue;
526 assert( sock->obj.ops == &sock_ops );
528 switch (type)
530 case ASYNC_TYPE_READ:
531 if (!sock->read_q && !(sock->read_q = create_async_queue( sock->fd ))) return;
532 queue = sock->read_q;
533 break;
534 case ASYNC_TYPE_WRITE:
535 if (!sock->write_q && !(sock->write_q = create_async_queue( sock->fd ))) return;
536 queue = sock->write_q;
537 break;
538 default:
539 set_error( STATUS_INVALID_PARAMETER );
540 return;
543 if ( ( !( sock->state & FD_READ ) && type == ASYNC_TYPE_READ ) ||
544 ( !( sock->state & FD_WRITE ) && type == ASYNC_TYPE_WRITE ) )
546 set_error( STATUS_PIPE_DISCONNECTED );
548 else
550 struct async *async;
551 if (!(async = create_async( current, queue, data ))) return;
552 release_object( async );
553 set_error( STATUS_PENDING );
556 sock_reselect( sock );
559 static void sock_async_event( struct fd *fd, struct async_queue *queue, struct async *async, int status, int finished )
561 struct sock *sock = get_fd_user( fd );
562 struct sock *acceptsock, *next;
563 assert( sock->obj.ops == &sock_ops );
565 if ( finished )
567 /* Clear pending accepts */
568 LIST_FOR_EACH_ENTRY_SAFE( acceptsock, next, &sock->paccepts, struct sock, accentry )
570 if ( acceptsock->async == async )
572 sock_free_accept_async( acceptsock );
573 break;
578 sock_reselect( sock );
581 static int sock_async_terminated( struct fd *fd, struct async_queue *queue, struct async *async, int status)
583 struct sock *sock = get_fd_user( fd );
584 struct sock *acceptsock;
585 assert( sock->obj.ops == &sock_ops );
587 if (status == STATUS_ALERTED)
589 LIST_FOR_EACH_ENTRY( acceptsock, &sock->paccepts, struct sock, accentry )
591 if ( acceptsock->async == async )
593 status = accept_into_socket( sock, acceptsock );
595 if (status != WSAEWOULDBLOCK)
596 sock_free_accept_async( acceptsock );
598 if (status == STATUS_SUCCESS)
600 sock_reselect( acceptsock );
601 status = STATUS_ALERTED;
603 else if (status == WSAEWOULDBLOCK)
604 status = STATUS_PENDING;
605 break;
610 return status;
613 static void sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb )
615 struct sock *sock = get_fd_user( fd );
616 int n = 0;
617 assert( sock->obj.ops == &sock_ops );
619 n += async_wake_up_by( sock->read_q, process, thread, iosb, STATUS_CANCELLED );
620 n += async_wake_up_by( sock->write_q, process, thread, iosb, STATUS_CANCELLED );
621 if (!n && iosb)
622 set_error( STATUS_NOT_FOUND );
625 static struct fd *sock_get_fd( struct object *obj )
627 struct sock *sock = (struct sock *)obj;
628 return (struct fd *)grab_object( sock->fd );
631 static void sock_destroy( struct object *obj )
633 struct sock *sock = (struct sock *)obj;
634 struct sock *acceptsock, *next;
635 assert( obj->ops == &sock_ops );
637 /* FIXME: special socket shutdown stuff? */
639 if ( sock->deferred )
640 release_object( sock->deferred );
641 if ( sock->async )
643 async_terminate( sock->async, STATUS_CANCELLED );
644 sock_free_accept_async( sock );
646 LIST_FOR_EACH_ENTRY_SAFE( acceptsock, next, &sock->paccepts, struct sock, accentry )
648 /* No need to cancel, freeing queues does it */
649 sock_free_accept_async( acceptsock );
652 free_async_queue( sock->read_q );
653 free_async_queue( sock->write_q );
654 if (sock->event) release_object( sock->event );
655 if (sock->fd)
657 /* shut the socket down to force pending poll() calls in the client to return */
658 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
659 release_object( sock->fd );
663 static struct sock *alloc_sock(void)
665 struct sock *sock;
666 if (!(sock = alloc_object( &sock_ops )))
667 return NULL;
669 sock->state = 0;
670 sock->mask = 0;
671 sock->hmask = 0;
672 sock->pmask = 0;
673 sock->polling = 0;
674 sock->flags = 0;
675 sock->type = 0;
676 sock->family = 0;
677 sock->event = NULL;
678 sock->window = 0;
679 sock->message = 0;
680 sock->wparam = 0;
681 sock->deferred = NULL;
682 sock->async = NULL;
683 list_init( &sock->paccepts );
684 sock->read_q = NULL;
685 sock->write_q = NULL;
687 return sock;
690 /* create a new and unconnected socket */
691 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
693 struct sock *sock;
694 int sockfd;
696 sockfd = socket( family, type, protocol );
697 if (debug_level)
698 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
699 if (sockfd == -1)
701 sock_set_error();
702 return NULL;
704 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
705 if (!(sock = alloc_sock()))
707 close( sockfd );
708 return NULL;
710 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
711 sock->flags = flags;
712 sock->type = type;
713 sock->family = family;
714 memset( sock->errors, 0, sizeof(sock->errors) );
715 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
716 (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
718 release_object( sock );
719 return NULL;
721 sock_reselect( sock );
722 clear_error();
723 return &sock->obj;
726 /* accepts a socket and inits it */
727 static int accept_new_fd( struct sock *sock )
730 /* Try to accept(2). We can't be safe that this an already connected socket
731 * or that accept() is allowed on it. In those cases we will get -1/errno
732 * return.
734 int acceptfd;
735 struct sockaddr saddr;
736 unsigned int slen = sizeof(saddr);
737 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
738 if (acceptfd == -1)
739 return acceptfd;
741 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
742 return acceptfd;
745 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
747 int acceptfd;
748 struct fd *oldfd;
749 if ( sock->deferred )
751 acceptfd = dup( get_unix_fd(sock->deferred->fd) );
752 if ( acceptfd == -1 )
753 return sock_get_error(errno);
755 /* Make sure we destroy fd first, so shutdown() isn't called */
756 oldfd = sock->deferred->fd;
757 sock->deferred->fd = NULL;
758 release_object( sock->deferred );
759 sock->deferred = NULL;
760 release_object( oldfd );
762 else
764 if ((acceptfd = accept_new_fd( sock )) == -1)
765 return sock_get_error(errno);
768 /* FIXME: need to copy sockopts from the old fd */
769 if ( dup2( acceptfd, get_unix_fd(acceptsock->fd) ) == -1 )
771 close(acceptfd);
772 return sock_get_error(errno);
774 close(acceptfd);
776 /* FIXME: Move these into SO_UPDATE_ACCEPT_CONTEXT + validation */
777 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
778 acceptsock->mask = sock->mask;
779 acceptsock->hmask = 0;
780 acceptsock->pmask = 0;
781 acceptsock->polling = 0;
782 acceptsock->type = sock->type;
783 acceptsock->family = sock->family;
784 acceptsock->window = sock->window;
785 acceptsock->message = sock->message;
786 acceptsock->wparam = 0;
787 if (acceptsock->event) release_object(acceptsock->event);
788 acceptsock->event = NULL;
789 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
790 acceptsock->deferred = NULL;
792 sock->pmask &= ~FD_ACCEPT;
793 sock->hmask &= ~FD_ACCEPT;
794 sock_reselect( sock );
796 return STATUS_SUCCESS;
799 /* accept a socket (creates a new fd) */
800 static struct sock *accept_socket( struct sock *sock )
802 struct sock *acceptsock;
803 int acceptfd;
805 if ( sock->deferred )
807 acceptsock = sock->deferred;
808 sock->deferred = NULL;
810 else
812 if ((acceptfd = accept_new_fd( sock )) == -1)
814 sock_set_error();
815 return NULL;
818 if (!(acceptsock = alloc_sock()))
820 close( acceptfd );
821 return NULL;
824 /* newly created socket gets the same properties of the listening socket */
825 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
826 if (sock->state & FD_WINE_NONBLOCKING)
827 acceptsock->state |= FD_WINE_NONBLOCKING;
828 acceptsock->mask = sock->mask;
829 acceptsock->type = sock->type;
830 acceptsock->family = sock->family;
831 acceptsock->window = sock->window;
832 acceptsock->message = sock->message;
833 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
834 acceptsock->flags = sock->flags;
835 memset( acceptsock->errors, 0, sizeof(acceptsock->errors) );
836 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
837 get_fd_options( sock->fd ) )))
839 release_object( acceptsock );
840 return NULL;
843 clear_error();
844 sock->pmask &= ~FD_ACCEPT;
845 sock->hmask &= ~FD_ACCEPT;
846 sock_reselect( sock );
847 return acceptsock;
850 /* set the last error depending on errno */
851 static int sock_get_error( int err )
853 switch (err)
855 case EINTR: return WSAEINTR;
856 case EBADF: return WSAEBADF;
857 case EPERM:
858 case EACCES: return WSAEACCES;
859 case EFAULT: return WSAEFAULT;
860 case EINVAL: return WSAEINVAL;
861 case EMFILE: return WSAEMFILE;
862 case EWOULDBLOCK: return WSAEWOULDBLOCK;
863 case EINPROGRESS: return WSAEINPROGRESS;
864 case EALREADY: return WSAEALREADY;
865 case ENOTSOCK: return WSAENOTSOCK;
866 case EDESTADDRREQ: return WSAEDESTADDRREQ;
867 case EMSGSIZE: return WSAEMSGSIZE;
868 case EPROTOTYPE: return WSAEPROTOTYPE;
869 case ENOPROTOOPT: return WSAENOPROTOOPT;
870 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
871 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
872 case EOPNOTSUPP: return WSAEOPNOTSUPP;
873 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
874 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
875 case EADDRINUSE: return WSAEADDRINUSE;
876 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
877 case ENETDOWN: return WSAENETDOWN;
878 case ENETUNREACH: return WSAENETUNREACH;
879 case ENETRESET: return WSAENETRESET;
880 case ECONNABORTED: return WSAECONNABORTED;
881 case EPIPE:
882 case ECONNRESET: return WSAECONNRESET;
883 case ENOBUFS: return WSAENOBUFS;
884 case EISCONN: return WSAEISCONN;
885 case ENOTCONN: return WSAENOTCONN;
886 case ESHUTDOWN: return WSAESHUTDOWN;
887 case ETOOMANYREFS: return WSAETOOMANYREFS;
888 case ETIMEDOUT: return WSAETIMEDOUT;
889 case ECONNREFUSED: return WSAECONNREFUSED;
890 case ELOOP: return WSAELOOP;
891 case ENAMETOOLONG: return WSAENAMETOOLONG;
892 case EHOSTDOWN: return WSAEHOSTDOWN;
893 case EHOSTUNREACH: return WSAEHOSTUNREACH;
894 case ENOTEMPTY: return WSAENOTEMPTY;
895 #ifdef EPROCLIM
896 case EPROCLIM: return WSAEPROCLIM;
897 #endif
898 #ifdef EUSERS
899 case EUSERS: return WSAEUSERS;
900 #endif
901 #ifdef EDQUOT
902 case EDQUOT: return WSAEDQUOT;
903 #endif
904 #ifdef ESTALE
905 case ESTALE: return WSAESTALE;
906 #endif
907 #ifdef EREMOTE
908 case EREMOTE: return WSAEREMOTE;
909 #endif
911 case 0: return 0;
912 default:
913 errno = err;
914 perror("wineserver: sock_get_error() can't map error");
915 return WSAEFAULT;
919 static int sock_get_ntstatus( int err )
921 switch ( err )
923 case EBADF: return STATUS_INVALID_HANDLE;
924 case EBUSY: return STATUS_DEVICE_BUSY;
925 case EPERM:
926 case EACCES: return STATUS_ACCESS_DENIED;
927 case EFAULT: return STATUS_NO_MEMORY;
928 case EINVAL: return STATUS_INVALID_PARAMETER;
929 case ENFILE:
930 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
931 case EWOULDBLOCK: return STATUS_CANT_WAIT;
932 case EINPROGRESS: return STATUS_PENDING;
933 case EALREADY: return STATUS_NETWORK_BUSY;
934 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
935 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
936 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
937 case EPROTONOSUPPORT:
938 case ESOCKTNOSUPPORT:
939 case EPFNOSUPPORT:
940 case EAFNOSUPPORT:
941 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
942 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
943 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
944 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
945 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
946 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
947 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
948 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
949 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
950 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
951 case ENETDOWN: return STATUS_NETWORK_BUSY;
952 case EPIPE:
953 case ECONNRESET: return STATUS_CONNECTION_RESET;
954 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
956 case 0: return STATUS_SUCCESS;
957 default:
958 errno = err;
959 perror("wineserver: sock_get_ntstatus() can't map error");
960 return STATUS_UNSUCCESSFUL;
964 /* set the last error depending on errno */
965 static void sock_set_error(void)
967 set_error( sock_get_ntstatus( errno ) );
970 /* create a socket */
971 DECL_HANDLER(create_socket)
973 struct object *obj;
975 reply->handle = 0;
976 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
978 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
979 release_object( obj );
983 /* accept a socket */
984 DECL_HANDLER(accept_socket)
986 struct sock *sock, *acceptsock;
988 reply->handle = 0;
989 if (!(sock = (struct sock *)get_handle_obj( current->process, req->lhandle,
990 FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|FILE_READ_DATA, &sock_ops)))
991 return;
993 if ((acceptsock = accept_socket( sock )) != NULL)
995 reply->handle = alloc_handle( current->process, &acceptsock->obj, req->access, req->attributes );
996 acceptsock->wparam = reply->handle; /* wparam for message is the socket handle */
997 sock_reselect( acceptsock );
999 release_object( acceptsock );
1001 release_object( sock );
1004 /* set socket event parameters */
1005 DECL_HANDLER(set_socket_event)
1007 struct sock *sock;
1008 struct event *old_event;
1009 int pollev;
1011 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1012 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
1013 old_event = sock->event;
1014 sock->mask = req->mask;
1015 sock->hmask &= ~req->mask; /* re-enable held events */
1016 sock->event = NULL;
1017 sock->window = req->window;
1018 sock->message = req->msg;
1019 sock->wparam = req->handle; /* wparam is the socket handle */
1020 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
1022 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
1024 pollev = sock_reselect( sock );
1026 if (sock->mask)
1027 sock->state |= FD_WINE_NONBLOCKING;
1029 /* if a network event is pending, signal the event object
1030 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1031 before a WSAEventSelect() was done on it.
1032 (when dealing with Asynchronous socket) */
1033 if (sock->pmask & sock->mask) sock_wake_up( sock, pollev );
1035 if (old_event) release_object( old_event ); /* we're through with it */
1036 release_object( &sock->obj );
1039 /* get socket event parameters */
1040 DECL_HANDLER(get_socket_event)
1042 struct sock *sock;
1043 int i;
1044 int errors[FD_MAX_EVENTS];
1046 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1047 if (!sock)
1049 reply->mask = 0;
1050 reply->pmask = 0;
1051 reply->state = 0;
1052 return;
1054 reply->mask = sock->mask;
1055 reply->pmask = sock->pmask;
1056 reply->state = sock->state;
1057 for (i = 0; i < FD_MAX_EVENTS; i++)
1058 errors[i] = sock_get_ntstatus(sock->errors[i]);
1060 set_reply_data( errors, min( get_reply_max_size(), sizeof(errors) ));
1062 if (req->service)
1064 if (req->c_event)
1066 struct event *cevent = get_event_obj( current->process, req->c_event,
1067 EVENT_MODIFY_STATE );
1068 if (cevent)
1070 reset_event( cevent );
1071 release_object( cevent );
1074 sock->pmask = 0;
1075 sock_reselect( sock );
1077 release_object( &sock->obj );
1080 /* re-enable pending socket events */
1081 DECL_HANDLER(enable_socket_event)
1083 struct sock *sock;
1085 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
1086 FILE_WRITE_ATTRIBUTES, &sock_ops)))
1087 return;
1089 sock->pmask &= ~req->mask; /* is this safe? */
1090 sock->hmask &= ~req->mask;
1091 sock->state |= req->sstate;
1092 sock->state &= ~req->cstate;
1093 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
1095 sock_reselect( sock );
1097 release_object( &sock->obj );
1100 DECL_HANDLER(set_socket_deferred)
1102 struct sock *sock, *acceptsock;
1104 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
1105 if ( !sock )
1106 return;
1108 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
1109 if ( !acceptsock )
1111 release_object( sock );
1112 return;
1114 sock->deferred = acceptsock;
1115 release_object( sock );
1118 DECL_HANDLER(register_accept_async)
1120 struct sock *sock, *acceptsock;
1122 sock = (struct sock *)get_handle_obj( current->process, req->data.handle,
1123 FILE_READ_ATTRIBUTES, &sock_ops );
1124 if ( !sock )
1126 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1127 return;
1129 if ( !(sock->state & FD_WINE_LISTENING) )
1131 release_object( sock );
1132 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1133 return;
1136 acceptsock = (struct sock *)get_handle_obj( current->process, req->ahandle,
1137 FILE_WRITE_ATTRIBUTES, &sock_ops );
1138 if ( !acceptsock )
1140 release_object( sock );
1141 set_error( STATUS_INVALID_PARAMETER );
1142 return;
1144 if ( acceptsock->state & ~FD_WINE_NONBLOCKING )
1146 release_object( acceptsock );
1147 release_object( sock );
1148 set_error( STATUS_INVALID_PARAMETER );
1149 return;
1152 if (!sock->read_q && !(sock->read_q = create_async_queue( sock->fd ))) return;
1154 acceptsock->async = create_async( current, sock->read_q, &req->data );
1155 if ( !acceptsock->async )
1157 release_object( acceptsock );
1158 release_object( sock );
1159 return;
1162 list_add_tail( &sock->paccepts, &acceptsock->accentry );
1163 acceptsock->state |= FD_WINE_ACCEPTING;
1164 release_object( acceptsock->async );
1166 sock_reselect( sock );
1168 set_error( STATUS_PENDING );
1170 release_object( acceptsock );
1171 release_object( sock );