libwine: Use the best fit data for codepage 1361.
[wine.git] / server / sock.c
blob5cb6d319ce19101f904a2b0d17afed4be00863a5
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 type; /* socket type */
100 unsigned short family; /* socket family */
101 struct event *event; /* event object */
102 user_handle_t window; /* window to send the message to */
103 unsigned int message; /* message to send */
104 obj_handle_t wparam; /* message wparam (socket handle) */
105 int errors[FD_MAX_EVENTS]; /* event errors */
106 struct sock *deferred; /* socket that waits for a deferred accept */
107 struct async_queue *read_q; /* queue for asynchronous reads */
108 struct async_queue *write_q; /* queue for asynchronous writes */
111 static void sock_dump( struct object *obj, int verbose );
112 static int sock_signaled( struct object *obj, struct thread *thread );
113 static struct fd *sock_get_fd( struct object *obj );
114 static void sock_destroy( struct object *obj );
116 static int sock_get_poll_events( struct fd *fd );
117 static void sock_poll_event( struct fd *fd, int event );
118 static enum server_fd_type sock_get_fd_type( struct fd *fd );
119 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
120 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
121 static void sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb );
123 static int sock_get_ntstatus( int err );
124 static int sock_get_error( int err );
125 static void sock_set_error(void);
127 static const struct object_ops sock_ops =
129 sizeof(struct sock), /* size */
130 sock_dump, /* dump */
131 no_get_type, /* get_type */
132 add_queue, /* add_queue */
133 remove_queue, /* remove_queue */
134 sock_signaled, /* signaled */
135 no_satisfied, /* satisfied */
136 no_signal, /* signal */
137 sock_get_fd, /* get_fd */
138 default_fd_map_access, /* map_access */
139 default_get_sd, /* get_sd */
140 default_set_sd, /* set_sd */
141 no_lookup_name, /* lookup_name */
142 no_open_file, /* open_file */
143 fd_close_handle, /* close_handle */
144 sock_destroy /* destroy */
147 static const struct fd_ops sock_fd_ops =
149 sock_get_poll_events, /* get_poll_events */
150 sock_poll_event, /* poll_event */
151 no_flush, /* flush */
152 sock_get_fd_type, /* get_fd_type */
153 default_fd_ioctl, /* ioctl */
154 sock_queue_async, /* queue_async */
155 sock_reselect_async, /* reselect_async */
156 sock_cancel_async /* cancel_async */
160 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
161 * we post messages if there are multiple events. Used to send
162 * messages. The problem is if there is both a FD_CONNECT event and,
163 * say, an FD_READ event available on the same socket, we want to
164 * notify the app of the connect event first. Otherwise it may
165 * discard the read event because it thinks it hasn't connected yet.
167 static const int event_bitorder[FD_MAX_EVENTS] =
169 FD_CONNECT_BIT,
170 FD_ACCEPT_BIT,
171 FD_OOB_BIT,
172 FD_WRITE_BIT,
173 FD_READ_BIT,
174 FD_CLOSE_BIT,
175 6, 7, 8, 9 /* leftovers */
178 /* Flags that make sense only for SOCK_STREAM sockets */
179 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
181 typedef enum {
182 SOCK_SHUTDOWN_ERROR = -1,
183 SOCK_SHUTDOWN_EOF = 0,
184 SOCK_SHUTDOWN_POLLHUP = 1
185 } sock_shutdown_t;
187 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
189 static sock_shutdown_t sock_check_pollhup(void)
191 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
192 int fd[2], n;
193 struct pollfd pfd;
194 char dummy;
196 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) goto out;
197 if ( shutdown( fd[0], 1 ) ) goto out;
199 pfd.fd = fd[1];
200 pfd.events = POLLIN;
201 pfd.revents = 0;
203 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
204 n = poll( &pfd, 1, 1 );
205 if ( n != 1 ) goto out; /* error or timeout */
206 if ( pfd.revents & POLLHUP )
207 ret = SOCK_SHUTDOWN_POLLHUP;
208 else if ( pfd.revents & POLLIN &&
209 read( fd[1], &dummy, 1 ) == 0 )
210 ret = SOCK_SHUTDOWN_EOF;
212 out:
213 close( fd[0] );
214 close( fd[1] );
215 return ret;
218 void sock_init(void)
220 sock_shutdown_type = sock_check_pollhup();
222 switch ( sock_shutdown_type )
224 case SOCK_SHUTDOWN_EOF:
225 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
226 break;
227 case SOCK_SHUTDOWN_POLLHUP:
228 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
229 break;
230 default:
231 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
232 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
236 static int sock_reselect( struct sock *sock )
238 int ev = sock_get_poll_events( sock->fd );
240 if (debug_level)
241 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
243 if (!sock->polling) /* FIXME: should find a better way to do this */
245 /* previously unconnected socket, is this reselect supposed to connect it? */
246 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
247 /* ok, it is, attach it to the wineserver's main poll loop */
248 sock->polling = 1;
249 allow_fd_caching( sock->fd );
251 /* update condition mask */
252 set_fd_events( sock->fd, ev );
253 return ev;
256 /* wake anybody waiting on the socket event or send the associated message */
257 static void sock_wake_up( struct sock *sock )
259 unsigned int events = sock->pmask & sock->mask;
260 int i;
262 if ( !events ) return;
264 if (sock->event)
266 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
267 set_event( sock->event );
269 if (sock->window)
271 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
272 for (i = 0; i < FD_MAX_EVENTS; i++)
274 int event = event_bitorder[i];
275 if (sock->pmask & (1 << event))
277 lparam_t lparam = (1 << event) | (sock_get_error(sock->errors[event]) << 16);
278 post_message( sock->window, sock->message, sock->wparam, lparam );
281 sock->pmask = 0;
282 sock_reselect( sock );
286 static inline int sock_error( struct fd *fd )
288 unsigned int optval = 0;
289 socklen_t optlen = sizeof(optval);
291 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
292 return optval;
295 static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
297 if ( sock->flags & WSA_FLAG_OVERLAPPED )
299 if ( event & (POLLIN|POLLPRI) && async_waiting( sock->read_q ) )
301 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
302 async_wake_up( sock->read_q, STATUS_ALERTED );
303 event &= ~(POLLIN|POLLPRI);
305 if ( event & POLLOUT && async_waiting( sock->write_q ) )
307 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
308 async_wake_up( sock->write_q, STATUS_ALERTED );
309 event &= ~POLLOUT;
311 if ( event & (POLLERR|POLLHUP) )
313 int status = sock_get_ntstatus( error );
315 if ( !(sock->state & FD_READ) )
316 async_wake_up( sock->read_q, status );
317 if ( !(sock->state & FD_WRITE) )
318 async_wake_up( sock->write_q, status );
321 return event;
324 static void sock_dispatch_events( struct sock *sock, int prevstate, int event, int error )
326 if (prevstate & FD_CONNECT)
328 sock->pmask |= FD_CONNECT;
329 sock->hmask |= FD_CONNECT;
330 sock->errors[FD_CONNECT_BIT] = error;
331 goto end;
333 if (prevstate & FD_WINE_LISTENING)
335 sock->pmask |= FD_ACCEPT;
336 sock->hmask |= FD_ACCEPT;
337 sock->errors[FD_ACCEPT_BIT] = error;
338 goto end;
341 if (event & POLLIN)
343 sock->pmask |= FD_READ;
344 sock->hmask |= FD_READ;
345 sock->errors[FD_READ_BIT] = 0;
348 if (event & POLLOUT)
350 sock->pmask |= FD_WRITE;
351 sock->hmask |= FD_WRITE;
352 sock->errors[FD_WRITE_BIT] = 0;
355 if (event & POLLPRI)
357 sock->pmask |= FD_OOB;
358 sock->hmask |= FD_OOB;
359 sock->errors[FD_OOB_BIT] = 0;
362 if (event & (POLLERR|POLLHUP))
364 sock->pmask |= FD_CLOSE;
365 sock->hmask |= FD_CLOSE;
366 sock->errors[FD_CLOSE_BIT] = error;
368 end:
369 sock_wake_up( sock );
372 static void sock_poll_event( struct fd *fd, int event )
374 struct sock *sock = get_fd_user( fd );
375 int hangup_seen = 0;
376 int prevstate = sock->state;
377 int error = 0;
379 assert( sock->obj.ops == &sock_ops );
380 if (debug_level)
381 fprintf(stderr, "socket %p select event: %x\n", sock, event);
383 /* we may change event later, remove from loop here */
384 if (event & (POLLERR|POLLHUP)) set_fd_events( sock->fd, -1 );
386 if (sock->state & FD_CONNECT)
388 if (event & (POLLERR|POLLHUP))
390 /* we didn't get connected? */
391 sock->state &= ~FD_CONNECT;
392 event &= ~POLLOUT;
393 error = sock_error( fd );
395 else if (event & POLLOUT)
397 /* we got connected */
398 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
399 sock->state &= ~FD_CONNECT;
402 else if (sock->state & FD_WINE_LISTENING)
404 /* listening */
405 if (event & (POLLERR|POLLHUP))
406 error = sock_error( fd );
408 else
410 /* normal data flow */
411 if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
413 char dummy;
414 int nr;
416 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
417 * has been closed, so we need to check for it explicitly here */
418 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
419 if ( nr == 0 )
421 hangup_seen = 1;
422 event &= ~POLLIN;
424 else if ( nr < 0 )
426 event &= ~POLLIN;
427 /* EAGAIN can happen if an async recv() falls between the server's poll()
428 call and the invocation of this routine */
429 if ( errno != EAGAIN )
431 error = errno;
432 event |= POLLERR;
433 if ( debug_level )
434 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
439 if ( (hangup_seen || event & (POLLHUP|POLLERR)) && (sock->state & (FD_READ|FD_WRITE)) )
441 error = error ? error : sock_error( fd );
442 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
443 sock->state &= ~FD_WRITE;
444 sock->state &= ~FD_READ;
446 if (debug_level)
447 fprintf(stderr, "socket %p aborted by error %d, event: %x\n", sock, error, event);
450 if (hangup_seen)
451 event |= POLLHUP;
454 event = sock_dispatch_asyncs( sock, event, error );
455 sock_dispatch_events( sock, prevstate, event, error );
457 /* if anyone is stupid enough to wait on the socket object itself,
458 * maybe we should wake them up too, just in case? */
459 wake_up( &sock->obj, 0 );
461 sock_reselect( sock );
464 static void sock_dump( struct object *obj, int verbose )
466 struct sock *sock = (struct sock *)obj;
467 assert( obj->ops == &sock_ops );
468 printf( "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
469 sock->fd, sock->state,
470 sock->mask, sock->pmask, sock->hmask );
473 static int sock_signaled( struct object *obj, struct thread *thread )
475 struct sock *sock = (struct sock *)obj;
476 assert( obj->ops == &sock_ops );
478 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
481 static int sock_get_poll_events( struct fd *fd )
483 struct sock *sock = get_fd_user( fd );
484 unsigned int mask = sock->mask & ~sock->hmask;
485 unsigned int smask = sock->state & mask;
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;
494 if ( async_queued( sock->read_q ) )
496 if ( async_waiting( sock->read_q ) ) ev |= POLLIN | POLLPRI;
498 else if (smask & FD_READ || (sock->state & FD_WINE_LISTENING && mask & FD_ACCEPT))
499 ev |= POLLIN | POLLPRI;
500 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
501 else if ( sock->type == SOCK_STREAM && sock->state & FD_READ && mask & FD_CLOSE &&
502 !(sock->hmask & FD_READ) )
503 ev |= POLLIN;
505 if ( async_queued( sock->write_q ) )
507 if ( async_waiting( sock->write_q ) ) ev |= POLLOUT;
509 else if (smask & FD_WRITE)
510 ev |= POLLOUT;
512 return ev;
515 static enum server_fd_type sock_get_fd_type( struct fd *fd )
517 return FD_TYPE_SOCKET;
520 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
522 struct sock *sock = get_fd_user( fd );
523 struct async *async;
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|FD_CONNECT|FD_WINE_LISTENING) ) && type == ASYNC_TYPE_READ ) ||
544 ( !( sock->state & (FD_WRITE|FD_CONNECT) ) && type == ASYNC_TYPE_WRITE ) )
546 set_error( STATUS_PIPE_DISCONNECTED );
547 return;
550 if (!(async = create_async( current, queue, data ))) return;
551 release_object( async );
553 sock_reselect( sock );
555 set_error( STATUS_PENDING );
558 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
560 struct sock *sock = get_fd_user( fd );
561 sock_reselect( sock );
564 static void sock_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb )
566 struct sock *sock = get_fd_user( fd );
567 int n = 0;
568 assert( sock->obj.ops == &sock_ops );
570 n += async_wake_up_by( sock->read_q, process, thread, iosb, STATUS_CANCELLED );
571 n += async_wake_up_by( sock->write_q, process, thread, iosb, STATUS_CANCELLED );
572 if (!n && iosb)
573 set_error( STATUS_NOT_FOUND );
576 static struct fd *sock_get_fd( struct object *obj )
578 struct sock *sock = (struct sock *)obj;
579 return (struct fd *)grab_object( sock->fd );
582 static void sock_destroy( struct object *obj )
584 struct sock *sock = (struct sock *)obj;
585 assert( obj->ops == &sock_ops );
587 /* FIXME: special socket shutdown stuff? */
589 if ( sock->deferred )
590 release_object( sock->deferred );
592 free_async_queue( sock->read_q );
593 free_async_queue( sock->write_q );
594 if (sock->event) release_object( sock->event );
595 if (sock->fd)
597 /* shut the socket down to force pending poll() calls in the client to return */
598 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
599 release_object( sock->fd );
603 static void init_sock(struct sock *sock)
605 sock->state = 0;
606 sock->mask = 0;
607 sock->hmask = 0;
608 sock->pmask = 0;
609 sock->polling = 0;
610 sock->flags = 0;
611 sock->type = 0;
612 sock->family = 0;
613 sock->event = NULL;
614 sock->window = 0;
615 sock->message = 0;
616 sock->wparam = 0;
617 sock->deferred = NULL;
618 sock->read_q = NULL;
619 sock->write_q = NULL;
620 memset( sock->errors, 0, sizeof(sock->errors) );
623 /* create a new and unconnected socket */
624 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
626 struct sock *sock;
627 int sockfd;
629 sockfd = socket( family, type, protocol );
630 if (debug_level)
631 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
632 if (sockfd == -1)
634 sock_set_error();
635 return NULL;
637 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
638 if (!(sock = alloc_object( &sock_ops )))
640 close( sockfd );
641 return NULL;
643 init_sock( sock );
644 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
645 sock->flags = flags;
646 sock->type = type;
647 sock->family = family;
649 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
650 (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
652 release_object( sock );
653 return NULL;
655 sock_reselect( sock );
656 clear_error();
657 return &sock->obj;
660 /* accepts a socket and inits it */
661 static int accept_new_fd( struct sock *sock )
664 /* Try to accept(2). We can't be safe that this an already connected socket
665 * or that accept() is allowed on it. In those cases we will get -1/errno
666 * return.
668 int acceptfd;
669 struct sockaddr saddr;
670 socklen_t slen = sizeof(saddr);
671 acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
672 if (acceptfd == -1)
674 sock_set_error();
675 return acceptfd;
678 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
679 return acceptfd;
682 /* accept a socket (creates a new fd) */
683 static struct sock *accept_socket( obj_handle_t handle )
685 struct sock *acceptsock;
686 struct sock *sock;
687 int acceptfd;
689 sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
690 if (!sock)
691 return NULL;
693 if ( sock->deferred )
695 acceptsock = sock->deferred;
696 sock->deferred = NULL;
698 else
700 if ((acceptfd = accept_new_fd( sock )) == -1)
702 release_object( sock );
703 return NULL;
705 if (!(acceptsock = alloc_object( &sock_ops )))
707 close( acceptfd );
708 release_object( sock );
709 return NULL;
712 init_sock( acceptsock );
713 /* newly created socket gets the same properties of the listening socket */
714 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
715 if (sock->state & FD_WINE_NONBLOCKING)
716 acceptsock->state |= FD_WINE_NONBLOCKING;
717 acceptsock->mask = sock->mask;
718 acceptsock->type = sock->type;
719 acceptsock->family = sock->family;
720 acceptsock->window = sock->window;
721 acceptsock->message = sock->message;
722 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
723 acceptsock->flags = sock->flags;
724 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
725 get_fd_options( sock->fd ) )))
727 release_object( acceptsock );
728 release_object( sock );
729 return NULL;
732 clear_error();
733 sock->pmask &= ~FD_ACCEPT;
734 sock->hmask &= ~FD_ACCEPT;
735 sock_reselect( sock );
736 release_object( sock );
737 return acceptsock;
740 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
742 int acceptfd;
743 struct fd *newfd;
744 if ( sock->deferred )
746 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
747 get_fd_options( acceptsock->fd ) );
748 if ( !newfd )
749 return FALSE;
751 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
753 release_object( sock->deferred );
754 sock->deferred = NULL;
756 else
758 if ((acceptfd = accept_new_fd( sock )) == -1)
759 return FALSE;
761 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
762 get_fd_options( acceptsock->fd ) )))
763 return FALSE;
766 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
767 acceptsock->hmask = 0;
768 acceptsock->pmask = 0;
769 acceptsock->polling = 0;
770 acceptsock->type = sock->type;
771 acceptsock->family = sock->family;
772 acceptsock->wparam = 0;
773 acceptsock->deferred = NULL;
774 release_object( acceptsock->fd );
775 acceptsock->fd = newfd;
777 clear_error();
778 sock->pmask &= ~FD_ACCEPT;
779 sock->hmask &= ~FD_ACCEPT;
780 sock_reselect( sock );
782 return TRUE;
785 /* set the last error depending on errno */
786 static int sock_get_error( int err )
788 switch (err)
790 case EINTR: return WSAEINTR;
791 case EBADF: return WSAEBADF;
792 case EPERM:
793 case EACCES: return WSAEACCES;
794 case EFAULT: return WSAEFAULT;
795 case EINVAL: return WSAEINVAL;
796 case EMFILE: return WSAEMFILE;
797 case EWOULDBLOCK: return WSAEWOULDBLOCK;
798 case EINPROGRESS: return WSAEINPROGRESS;
799 case EALREADY: return WSAEALREADY;
800 case ENOTSOCK: return WSAENOTSOCK;
801 case EDESTADDRREQ: return WSAEDESTADDRREQ;
802 case EMSGSIZE: return WSAEMSGSIZE;
803 case EPROTOTYPE: return WSAEPROTOTYPE;
804 case ENOPROTOOPT: return WSAENOPROTOOPT;
805 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
806 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
807 case EOPNOTSUPP: return WSAEOPNOTSUPP;
808 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
809 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
810 case EADDRINUSE: return WSAEADDRINUSE;
811 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
812 case ENETDOWN: return WSAENETDOWN;
813 case ENETUNREACH: return WSAENETUNREACH;
814 case ENETRESET: return WSAENETRESET;
815 case ECONNABORTED: return WSAECONNABORTED;
816 case EPIPE:
817 case ECONNRESET: return WSAECONNRESET;
818 case ENOBUFS: return WSAENOBUFS;
819 case EISCONN: return WSAEISCONN;
820 case ENOTCONN: return WSAENOTCONN;
821 case ESHUTDOWN: return WSAESHUTDOWN;
822 case ETOOMANYREFS: return WSAETOOMANYREFS;
823 case ETIMEDOUT: return WSAETIMEDOUT;
824 case ECONNREFUSED: return WSAECONNREFUSED;
825 case ELOOP: return WSAELOOP;
826 case ENAMETOOLONG: return WSAENAMETOOLONG;
827 case EHOSTDOWN: return WSAEHOSTDOWN;
828 case EHOSTUNREACH: return WSAEHOSTUNREACH;
829 case ENOTEMPTY: return WSAENOTEMPTY;
830 #ifdef EPROCLIM
831 case EPROCLIM: return WSAEPROCLIM;
832 #endif
833 #ifdef EUSERS
834 case EUSERS: return WSAEUSERS;
835 #endif
836 #ifdef EDQUOT
837 case EDQUOT: return WSAEDQUOT;
838 #endif
839 #ifdef ESTALE
840 case ESTALE: return WSAESTALE;
841 #endif
842 #ifdef EREMOTE
843 case EREMOTE: return WSAEREMOTE;
844 #endif
846 case 0: return 0;
847 default:
848 errno = err;
849 perror("wineserver: sock_get_error() can't map error");
850 return WSAEFAULT;
854 static int sock_get_ntstatus( int err )
856 switch ( err )
858 case EBADF: return STATUS_INVALID_HANDLE;
859 case EBUSY: return STATUS_DEVICE_BUSY;
860 case EPERM:
861 case EACCES: return STATUS_ACCESS_DENIED;
862 case EFAULT: return STATUS_NO_MEMORY;
863 case EINVAL: return STATUS_INVALID_PARAMETER;
864 case ENFILE:
865 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
866 case EWOULDBLOCK: return STATUS_CANT_WAIT;
867 case EINPROGRESS: return STATUS_PENDING;
868 case EALREADY: return STATUS_NETWORK_BUSY;
869 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
870 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
871 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
872 case EPROTONOSUPPORT:
873 case ESOCKTNOSUPPORT:
874 case EPFNOSUPPORT:
875 case EAFNOSUPPORT:
876 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
877 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
878 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
879 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
880 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
881 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
882 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
883 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
884 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
885 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
886 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
887 case ENETDOWN: return STATUS_NETWORK_BUSY;
888 case EPIPE:
889 case ECONNRESET: return STATUS_CONNECTION_RESET;
890 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
892 case 0: return STATUS_SUCCESS;
893 default:
894 errno = err;
895 perror("wineserver: sock_get_ntstatus() can't map error");
896 return STATUS_UNSUCCESSFUL;
900 /* set the last error depending on errno */
901 static void sock_set_error(void)
903 set_error( sock_get_ntstatus( errno ) );
906 /* create a socket */
907 DECL_HANDLER(create_socket)
909 struct object *obj;
911 reply->handle = 0;
912 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
914 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
915 release_object( obj );
919 /* accept a socket */
920 DECL_HANDLER(accept_socket)
922 struct sock *sock;
924 reply->handle = 0;
925 if ((sock = accept_socket( req->lhandle )) != NULL)
927 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
928 sock->wparam = reply->handle; /* wparam for message is the socket handle */
929 sock_reselect( sock );
930 release_object( &sock->obj );
934 /* accept a socket into an initialized socket */
935 DECL_HANDLER(accept_into_socket)
937 struct sock *sock, *acceptsock;
938 const int all_attributes = FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|FILE_READ_DATA;
940 if (!(sock = (struct sock *)get_handle_obj( current->process, req->lhandle,
941 all_attributes, &sock_ops)))
942 return;
944 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, req->ahandle,
945 all_attributes, &sock_ops)))
947 release_object( sock );
948 return;
951 if (accept_into_socket( sock, acceptsock ))
953 acceptsock->wparam = req->ahandle; /* wparam for message is the socket handle */
954 sock_reselect( acceptsock );
956 release_object( acceptsock );
957 release_object( sock );
960 /* set socket event parameters */
961 DECL_HANDLER(set_socket_event)
963 struct sock *sock;
964 struct event *old_event;
966 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
967 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
968 old_event = sock->event;
969 sock->mask = req->mask;
970 sock->hmask &= ~req->mask; /* re-enable held events */
971 sock->event = NULL;
972 sock->window = req->window;
973 sock->message = req->msg;
974 sock->wparam = req->handle; /* wparam is the socket handle */
975 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
977 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
979 sock_reselect( sock );
981 sock->state |= FD_WINE_NONBLOCKING;
983 /* if a network event is pending, signal the event object
984 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
985 before a WSAEventSelect() was done on it.
986 (when dealing with Asynchronous socket) */
987 sock_wake_up( sock );
989 if (old_event) release_object( old_event ); /* we're through with it */
990 release_object( &sock->obj );
993 /* get socket event parameters */
994 DECL_HANDLER(get_socket_event)
996 struct sock *sock;
997 int i;
998 int errors[FD_MAX_EVENTS];
1000 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1001 if (!sock)
1003 reply->mask = 0;
1004 reply->pmask = 0;
1005 reply->state = 0;
1006 return;
1008 reply->mask = sock->mask;
1009 reply->pmask = sock->pmask;
1010 reply->state = sock->state;
1011 for (i = 0; i < FD_MAX_EVENTS; i++)
1012 errors[i] = sock_get_ntstatus(sock->errors[i]);
1014 set_reply_data( errors, min( get_reply_max_size(), sizeof(errors) ));
1016 if (req->service)
1018 if (req->c_event)
1020 struct event *cevent = get_event_obj( current->process, req->c_event,
1021 EVENT_MODIFY_STATE );
1022 if (cevent)
1024 reset_event( cevent );
1025 release_object( cevent );
1028 sock->pmask = 0;
1029 sock_reselect( sock );
1031 release_object( &sock->obj );
1034 /* re-enable pending socket events */
1035 DECL_HANDLER(enable_socket_event)
1037 struct sock *sock;
1039 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
1040 FILE_WRITE_ATTRIBUTES, &sock_ops)))
1041 return;
1043 /* for event-based notification, windows erases stale events */
1044 sock->pmask &= ~req->mask;
1046 sock->hmask &= ~req->mask;
1047 sock->state |= req->sstate;
1048 sock->state &= ~req->cstate;
1049 if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
1051 sock_reselect( sock );
1053 release_object( &sock->obj );
1056 DECL_HANDLER(set_socket_deferred)
1058 struct sock *sock, *acceptsock;
1060 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
1061 if ( !sock )
1062 return;
1064 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
1065 if ( !acceptsock )
1067 release_object( sock );
1068 return;
1070 sock->deferred = acceptsock;
1071 release_object( sock );