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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * FIXME: we use read|write access in all cases. Shouldn't we depend that
21 * on the access of the current handle?
32 #ifdef HAVE_SYS_ERRNO_H
33 # include <sys/errno.h>
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_SOCKET_H
38 # include <sys/socket.h>
40 #include <sys/ioctl.h>
41 #ifdef HAVE_SYS_FILIO_H
42 # include <sys/filio.h>
56 /* To avoid conflicts with the Unix socket headers. Plus we only need a few
64 struct object obj; /* object header */
65 unsigned int state; /* status bits */
66 unsigned int mask; /* event mask */
67 unsigned int hmask; /* held (blocked) events */
68 unsigned int pmask; /* pending events */
69 unsigned int flags; /* socket flags */
70 struct event *event; /* event object */
71 user_handle_t window; /* window to send the message to */
72 unsigned int message; /* message to send */
73 unsigned int wparam; /* message wparam (socket handle) */
74 int errors[FD_MAX_EVENTS]; /* event errors */
75 struct sock* deferred; /* socket that waits for a deferred accept */
76 struct async_queue read_q; /* Queue for asynchronous reads */
77 struct async_queue write_q; /* Queue for asynchronous writes */
80 static void sock_dump( struct object *obj, int verbose );
81 static int sock_signaled( struct object *obj, struct thread *thread );
82 static int sock_get_poll_events( struct object *obj );
83 static void sock_poll_event( struct object *obj, int event );
84 static int sock_get_fd( struct object *obj );
85 static int sock_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
86 static void sock_destroy( struct object *obj );
87 static int sock_get_error( int err );
88 static void sock_set_error(void);
89 static void sock_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count);
91 static const struct object_ops sock_ops =
93 sizeof(struct sock), /* size */
95 add_queue, /* add_queue */
96 remove_queue, /* remove_queue */
97 sock_signaled, /* signaled */
98 no_satisfied, /* satisfied */
99 sock_get_poll_events, /* get_poll_events */
100 sock_poll_event, /* poll_event */
101 sock_get_fd, /* get_fd */
102 no_flush, /* flush */
103 sock_get_info, /* get_file_info */
104 sock_queue_async, /* queue_async */
105 sock_destroy /* destroy */
109 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
110 * we post messages if there are multiple events. Used to send
111 * messages. The problem is if there is both a FD_CONNECT event and,
112 * say, an FD_READ event available on the same socket, we want to
113 * notify the app of the connect event first. Otherwise it may
114 * discard the read event because it thinks it hasn't connected yet.
116 static const int event_bitorder[FD_MAX_EVENTS] =
124 6, 7, 8, 9 /* leftovers */
128 static int sock_reselect( struct sock *sock )
130 int ev = sock_get_poll_events( &sock->obj );
133 fprintf(stderr,"sock_reselect(%d): new mask %x\n", sock->obj.fd, ev);
135 if (sock->obj.select == -1) {
136 /* previously unconnected socket, is this reselect supposed to connect it? */
137 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
138 /* ok, it is, attach it to the wineserver's main poll loop */
139 add_select_user( &sock->obj );
141 /* update condition mask */
142 set_select_events( &sock->obj, ev );
146 /* After POLLHUP is received, the socket will no longer be in the main select loop.
147 This function is used to signal pending events nevertheless */
148 static void sock_try_event ( struct sock *sock, int event )
152 pfd.fd = sock->obj.fd;
159 if ( debug_level ) fprintf ( stderr, "sock_try_event: %x\n", pfd.revents );
160 sock_poll_event ( &sock->obj, pfd.revents );
164 /* wake anybody waiting on the socket event or send the associated message */
165 static void sock_wake_up( struct sock *sock, int pollev )
167 unsigned int events = sock->pmask & sock->mask;
169 int async_active = 0;
171 if ( sock->flags & FD_FLAG_OVERLAPPED )
173 if( pollev & (POLLIN|POLLPRI) && IS_READY( sock->read_q ) )
175 if (debug_level) fprintf ( stderr, "activating read queue for socket %p\n", sock );
176 async_notify( sock->read_q.head, STATUS_ALERTED );
179 if( pollev & POLLOUT && IS_READY( sock->write_q ) )
181 if (debug_level) fprintf ( stderr, "activating write queue for socket %p\n", sock );
182 async_notify( sock->write_q.head, STATUS_ALERTED );
187 /* Do not signal events if there are still pending asynchronous IO requests */
188 /* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
189 if ( !events || async_active ) return;
191 if (events & FD_CLOSE) sock->hmask |= FD_CLOSE;
195 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
196 set_event( sock->event );
200 if (debug_level) fprintf(stderr, "signalling events %x win %x\n", events, sock->window );
201 for (i = 0; i < FD_MAX_EVENTS; i++)
203 int event = event_bitorder[i];
204 if (sock->pmask & (1 << event))
206 unsigned int lparam = (1 << event) | (sock->errors[event] << 16);
207 post_message( sock->window, sock->message, sock->wparam, lparam );
211 sock_reselect( sock );
215 inline static int sock_error(int s)
217 unsigned int optval = 0, optlen;
219 optlen = sizeof(optval);
220 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
221 return optval ? sock_get_error(optval) : 0;
224 static void sock_poll_event( struct object *obj, int event )
226 struct sock *sock = (struct sock *)obj;
229 assert( sock->obj.ops == &sock_ops );
231 fprintf(stderr, "socket %d select event: %x\n", sock->obj.fd, event);
232 if (sock->state & FD_CONNECT)
237 /* we got connected */
238 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
239 sock->state &= ~FD_CONNECT;
240 sock->pmask |= FD_CONNECT;
241 sock->errors[FD_CONNECT_BIT] = 0;
243 fprintf(stderr, "socket %d connection success\n", sock->obj.fd);
245 else if (event & (POLLERR|POLLHUP))
247 /* we didn't get connected? */
248 sock->state &= ~FD_CONNECT;
249 sock->pmask |= FD_CONNECT;
250 sock->errors[FD_CONNECT_BIT] = sock_error( sock->obj.fd );
252 fprintf(stderr, "socket %d connection failure\n", sock->obj.fd);
253 set_select_events( &sock->obj, -1 );
256 if (sock->state & FD_WINE_LISTENING)
261 /* incoming connection */
262 sock->pmask |= FD_ACCEPT;
263 sock->errors[FD_ACCEPT_BIT] = 0;
264 sock->hmask |= FD_ACCEPT;
266 else if (event & (POLLERR|POLLHUP))
268 /* failed incoming connection? */
269 sock->pmask |= FD_ACCEPT;
270 sock->errors[FD_ACCEPT_BIT] = sock_error( sock->obj.fd );
271 sock->hmask |= FD_ACCEPT;
272 set_select_events( &sock->obj, -1 );
276 /* normal data flow */
282 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
283 * has been closed, so we need to check for it explicitly here */
284 nr = recv( sock->obj.fd, &dummy, 1, MSG_PEEK );
288 sock->pmask |= FD_READ;
289 sock->hmask |= FD_READ;
290 sock->errors[FD_READ_BIT] = 0;
292 fprintf(stderr, "socket %d is readable\n", sock->obj.fd );
298 /* EAGAIN can happen if an async recv() falls between the server's poll()
299 call and the invocation of this routine */
300 if ( errno == EAGAIN )
305 fprintf ( stderr, "recv error on socket %d: %d\n", sock->obj.fd, errno );
313 sock->pmask |= FD_WRITE;
314 sock->hmask |= FD_WRITE;
315 sock->errors[FD_WRITE_BIT] = 0;
317 fprintf(stderr, "socket %d is writable\n", sock->obj.fd);
321 sock->pmask |= FD_OOB;
322 sock->hmask |= FD_OOB;
323 sock->errors[FD_OOB_BIT] = 0;
325 fprintf(stderr, "socket %d got OOB data\n", sock->obj.fd);
327 /* According to WS2 specs, FD_CLOSE is only delivered when there is
328 no more data to be read (i.e. empty_recv = 1) */
329 else if ( empty_recv && (sock->state & (FD_READ|FD_WRITE) ))
331 sock->errors[FD_CLOSE_BIT] = sock_error( sock->obj.fd );
332 if ( event & ( POLLERR|POLLHUP ) )
333 sock->state &= ~(FD_WINE_CONNECTED|FD_WRITE);
334 sock->pmask |= FD_CLOSE;
336 fprintf(stderr, "socket %d aborted by error %d, event: %x - removing from select loop\n",
337 sock->obj.fd, sock->errors[FD_CLOSE_BIT], event);
338 set_select_events( &sock->obj, -1 );
342 sock_reselect( sock );
344 /* wake up anyone waiting for whatever just happened */
345 if ( sock->pmask & sock->mask || sock->flags & FD_FLAG_OVERLAPPED ) sock_wake_up( sock, event );
347 /* if anyone is stupid enough to wait on the socket object itself,
348 * maybe we should wake them up too, just in case? */
349 wake_up( &sock->obj, 0 );
352 static void sock_dump( struct object *obj, int verbose )
354 struct sock *sock = (struct sock *)obj;
355 assert( obj->ops == &sock_ops );
356 printf( "Socket fd=%d, state=%x, mask=%x, pending=%x, held=%x\n",
357 sock->obj.fd, sock->state,
358 sock->mask, sock->pmask, sock->hmask );
361 static int sock_signaled( struct object *obj, struct thread *thread )
363 struct sock *sock = (struct sock *)obj;
364 assert( obj->ops == &sock_ops );
366 return check_select_events( sock->obj.fd, sock_get_poll_events( &sock->obj ) );
369 static int sock_get_poll_events( struct object *obj )
371 struct sock *sock = (struct sock *)obj;
372 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
375 assert( obj->ops == &sock_ops );
377 if (sock->state & FD_CONNECT)
378 /* connecting, wait for writable */
380 if (sock->state & FD_WINE_LISTENING)
381 /* listening, wait for readable */
382 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
384 if (mask & (FD_READ) || (sock->flags & WSA_FLAG_OVERLAPPED && IS_READY (sock->read_q)))
385 ev |= POLLIN | POLLPRI;
386 if (mask & FD_WRITE || (sock->flags & WSA_FLAG_OVERLAPPED && IS_READY (sock->write_q)))
388 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication. */
389 if (sock->mask & ~sock->hmask & FD_CLOSE && !(sock->hmask & FD_READ) )
395 static int sock_get_fd( struct object *obj )
397 struct sock *sock = (struct sock *)obj;
398 assert( obj->ops == &sock_ops );
402 static int sock_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
404 struct sock *sock = (struct sock*) obj;
405 assert ( obj->ops == &sock_ops );
409 reply->type = FILE_TYPE_PIPE;
411 reply->access_time = 0;
412 reply->write_time = 0;
413 reply->size_high = 0;
416 reply->index_high = 0;
417 reply->index_low = 0;
421 if (sock->flags & WSA_FLAG_OVERLAPPED) *flags |= FD_FLAG_OVERLAPPED;
422 if ( !(sock->state & FD_READ ) ) *flags |= FD_FLAG_RECV_SHUTDOWN;
423 if ( !(sock->state & FD_WRITE ) ) *flags |= FD_FLAG_SEND_SHUTDOWN;
424 return FD_TYPE_SOCKET;
427 static void sock_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count)
429 struct sock *sock = (struct sock *)obj;
430 struct async_queue *q;
434 assert( obj->ops == &sock_ops );
436 if ( !(sock->flags & WSA_FLAG_OVERLAPPED) )
438 set_error ( STATUS_INVALID_HANDLE );
444 case ASYNC_TYPE_READ:
447 case ASYNC_TYPE_WRITE:
451 set_error( STATUS_INVALID_PARAMETER );
455 async = find_async ( q, current, ptr );
457 if ( status == STATUS_PENDING )
459 if ( ( !( sock->state & FD_READ ) && type == ASYNC_TYPE_READ ) ||
460 ( !( sock->state & FD_WRITE ) && type == ASYNC_TYPE_WRITE ) )
462 set_error ( STATUS_PIPE_DISCONNECTED );
463 if ( async ) destroy_async ( async );
468 async = create_async ( obj, current, ptr );
472 async->status = STATUS_PENDING;
474 async_insert ( q, async );
477 else if ( async ) destroy_async ( async );
478 else set_error ( STATUS_INVALID_PARAMETER );
480 pollev = sock_reselect ( sock );
481 if ( pollev ) sock_try_event ( sock, pollev );
484 static void sock_destroy( struct object *obj )
486 struct sock *sock = (struct sock *)obj;
487 assert( obj->ops == &sock_ops );
489 /* FIXME: special socket shutdown stuff? */
491 if ( sock->deferred )
492 release_object ( sock->deferred );
494 if ( sock->flags & WSA_FLAG_OVERLAPPED )
496 destroy_async_queue ( &sock->read_q );
497 destroy_async_queue ( &sock->write_q );
499 if (sock->event) release_object( sock->event );
502 /* create a new and unconnected socket */
503 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
508 sockfd = socket( family, type, protocol );
510 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
515 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
516 if (!(sock = alloc_object( &sock_ops, -1 ))) return NULL;
517 sock->obj.fd = sockfd;
518 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
527 sock->deferred = NULL;
528 if (sock->flags & WSA_FLAG_OVERLAPPED)
530 init_async_queue (&sock->read_q);
531 init_async_queue (&sock->write_q);
533 sock_reselect( sock );
538 /* accept a socket (creates a new fd) */
539 static struct sock *accept_socket( handle_t handle )
541 struct sock *acceptsock;
544 struct sockaddr saddr;
547 sock=(struct sock*)get_handle_obj(current->process,handle,
548 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
552 if ( sock->deferred ) {
553 acceptsock = sock->deferred;
554 sock->deferred = NULL;
557 /* Try to accept(2). We can't be safe that this an already connected socket
558 * or that accept() is allowed on it. In those cases we will get -1/errno
561 slen = sizeof(saddr);
562 acceptfd = accept(sock->obj.fd,&saddr,&slen);
565 release_object( sock );
568 if (!(acceptsock = alloc_object( &sock_ops, -1 )))
570 release_object( sock );
574 /* newly created socket gets the same properties of the listening socket */
575 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
576 acceptsock->obj.fd = acceptfd;
577 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
578 if (sock->state & FD_WINE_NONBLOCKING)
579 acceptsock->state |= FD_WINE_NONBLOCKING;
580 acceptsock->mask = sock->mask;
581 acceptsock->hmask = 0;
582 acceptsock->pmask = 0;
583 acceptsock->event = NULL;
584 acceptsock->window = sock->window;
585 acceptsock->message = sock->message;
586 acceptsock->wparam = 0;
587 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
588 acceptsock->flags = sock->flags;
589 acceptsock->deferred = 0;
590 if ( acceptsock->flags & WSA_FLAG_OVERLAPPED )
592 init_async_queue ( &acceptsock->read_q );
593 init_async_queue ( &acceptsock->write_q );
597 sock->pmask &= ~FD_ACCEPT;
598 sock->hmask &= ~FD_ACCEPT;
599 sock_reselect( sock );
600 release_object( sock );
604 /* set the last error depending on errno */
605 static int sock_get_error( int err )
609 case EINTR: return WSAEINTR; break;
610 case EBADF: return WSAEBADF; break;
612 case EACCES: return WSAEACCES; break;
613 case EFAULT: return WSAEFAULT; break;
614 case EINVAL: return WSAEINVAL; break;
615 case EMFILE: return WSAEMFILE; break;
616 case EWOULDBLOCK: return WSAEWOULDBLOCK; break;
617 case EINPROGRESS: return WSAEINPROGRESS; break;
618 case EALREADY: return WSAEALREADY; break;
619 case ENOTSOCK: return WSAENOTSOCK; break;
620 case EDESTADDRREQ: return WSAEDESTADDRREQ; break;
621 case EMSGSIZE: return WSAEMSGSIZE; break;
622 case EPROTOTYPE: return WSAEPROTOTYPE; break;
623 case ENOPROTOOPT: return WSAENOPROTOOPT; break;
624 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT; break;
625 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT; break;
626 case EOPNOTSUPP: return WSAEOPNOTSUPP; break;
627 case EPFNOSUPPORT: return WSAEPFNOSUPPORT; break;
628 case EAFNOSUPPORT: return WSAEAFNOSUPPORT; break;
629 case EADDRINUSE: return WSAEADDRINUSE; break;
630 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL; break;
631 case ENETDOWN: return WSAENETDOWN; break;
632 case ENETUNREACH: return WSAENETUNREACH; break;
633 case ENETRESET: return WSAENETRESET; break;
634 case ECONNABORTED: return WSAECONNABORTED; break;
636 case ECONNRESET: return WSAECONNRESET; break;
637 case ENOBUFS: return WSAENOBUFS; break;
638 case EISCONN: return WSAEISCONN; break;
639 case ENOTCONN: return WSAENOTCONN; break;
640 case ESHUTDOWN: return WSAESHUTDOWN; break;
641 case ETOOMANYREFS: return WSAETOOMANYREFS; break;
642 case ETIMEDOUT: return WSAETIMEDOUT; break;
643 case ECONNREFUSED: return WSAECONNREFUSED; break;
644 case ELOOP: return WSAELOOP; break;
645 case ENAMETOOLONG: return WSAENAMETOOLONG; break;
646 case EHOSTDOWN: return WSAEHOSTDOWN; break;
647 case EHOSTUNREACH: return WSAEHOSTUNREACH; break;
648 case ENOTEMPTY: return WSAENOTEMPTY; break;
650 case EPROCLIM: return WSAEPROCLIM; break;
653 case EUSERS: return WSAEUSERS; break;
656 case EDQUOT: return WSAEDQUOT; break;
659 case ESTALE: return WSAESTALE; break;
662 case EREMOTE: return WSAEREMOTE; break;
664 default: errno=err; perror("sock_set_error"); return ERROR_UNKNOWN; break;
668 /* set the last error depending on errno */
669 static void sock_set_error(void)
671 set_error( sock_get_error( errno ) );
674 /* create a socket */
675 DECL_HANDLER(create_socket)
680 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
682 reply->handle = alloc_handle( current->process, obj, req->access, req->inherit );
683 release_object( obj );
687 /* accept a socket */
688 DECL_HANDLER(accept_socket)
693 if ((sock = accept_socket( req->lhandle )) != NULL)
695 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->inherit );
696 sock->wparam = reply->handle; /* wparam for message is the socket handle */
697 sock_reselect( sock );
698 release_object( &sock->obj );
702 /* set socket event parameters */
703 DECL_HANDLER(set_socket_event)
706 struct event *old_event;
709 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
710 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &sock_ops)))
712 old_event = sock->event;
713 sock->mask = req->mask;
715 sock->window = req->window;
716 sock->message = req->msg;
717 sock->wparam = req->handle; /* wparam is the socket handle */
718 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
720 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
722 pollev = sock_reselect( sock );
723 if ( pollev ) sock_try_event ( sock, pollev );
726 sock->state |= FD_WINE_NONBLOCKING;
728 /* if a network event is pending, signal the event object
729 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
730 before a WSAEventSelect() was done on it.
731 (when dealing with Asynchronous socket) */
732 if (sock->pmask & sock->mask) sock_wake_up( sock, pollev );
734 if (old_event) release_object( old_event ); /* we're through with it */
735 release_object( &sock->obj );
738 /* get socket event parameters */
739 DECL_HANDLER(get_socket_event)
743 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
749 set_error( WSAENOTSOCK );
752 reply->mask = sock->mask;
753 reply->pmask = sock->pmask;
754 reply->state = sock->state;
755 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
761 struct event *cevent = get_event_obj( current->process, req->c_event,
762 EVENT_MODIFY_STATE );
765 reset_event( cevent );
766 release_object( cevent );
770 sock_reselect( sock );
772 release_object( &sock->obj );
775 /* re-enable pending socket events */
776 DECL_HANDLER(enable_socket_event)
781 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
782 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &sock_ops)))
785 sock->pmask &= ~req->mask; /* is this safe? */
786 sock->hmask &= ~req->mask;
787 sock->state |= req->sstate;
788 sock->state &= ~req->cstate;
790 pollev = sock_reselect( sock );
791 if ( pollev ) sock_try_event ( sock, pollev );
793 release_object( &sock->obj );
796 DECL_HANDLER(set_socket_deferred)
798 struct sock *sock, *acceptsock;
800 sock=(struct sock*)get_handle_obj( current->process,req->handle,
801 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops );
804 set_error ( WSAENOTSOCK );
807 acceptsock = (struct sock*)get_handle_obj( current->process,req->deferred,
808 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops );
811 release_object ( sock );
812 set_error ( WSAENOTSOCK );
815 sock->deferred = acceptsock;
816 release_object ( sock );