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 async_queue read_q; /* Queue for asynchronous reads */
76 struct async_queue write_q; /* Queue for asynchronous writes */
79 static void sock_dump( struct object *obj, int verbose );
80 static int sock_signaled( struct object *obj, struct thread *thread );
81 static int sock_get_poll_events( struct object *obj );
82 static void sock_poll_event( struct object *obj, int event );
83 static int sock_get_fd( struct object *obj );
84 static int sock_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
85 static void sock_destroy( struct object *obj );
86 static int sock_get_error( int err );
87 static void sock_set_error(void);
89 static const struct object_ops sock_ops =
91 sizeof(struct sock), /* size */
93 add_queue, /* add_queue */
94 remove_queue, /* remove_queue */
95 sock_signaled, /* signaled */
96 no_satisfied, /* satisfied */
97 sock_get_poll_events, /* get_poll_events */
98 sock_poll_event, /* poll_event */
99 sock_get_fd, /* get_fd */
100 no_flush, /* flush */
101 sock_get_info, /* get_file_info */
102 NULL, /* queue_async */
103 sock_destroy /* destroy */
107 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
108 * we post messages if there are multiple events. Used to send
109 * messages. The problem is if there is both a FD_CONNECT event and,
110 * say, an FD_READ event available on the same socket, we want to
111 * notify the app of the connect event first. Otherwise it may
112 * discard the read event because it thinks it hasn't connected yet.
114 static const int event_bitorder[FD_MAX_EVENTS] =
122 6, 7, 8, 9 /* leftovers */
126 static void sock_reselect( struct sock *sock )
128 int ev = sock_get_poll_events( &sock->obj );
131 fprintf(stderr,"sock_reselect(%d): new mask %x\n", sock->obj.fd, ev);
133 if (sock->obj.select == -1) {
134 /* previously unconnected socket, is this reselect supposed to connect it? */
135 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return;
136 /* ok, it is, attach it to the wineserver's main poll loop */
137 add_select_user( &sock->obj );
139 /* update condition mask */
140 set_select_events( &sock->obj, ev );
143 /* wake anybody waiting on the socket event or send the associated message */
144 static void sock_wake_up( struct sock *sock )
146 unsigned int events = sock->pmask & sock->mask;
153 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
154 set_event( sock->event );
158 if (debug_level) fprintf(stderr, "signalling events %x win %x\n", events, sock->window );
159 for (i = 0; i < FD_MAX_EVENTS; i++)
161 int event = event_bitorder[i];
162 if (sock->pmask & (1 << event))
164 unsigned int lparam = (1 << event) | (sock->errors[event] << 16);
165 post_message( sock->window, sock->message, sock->wparam, lparam );
169 sock_reselect( sock );
173 inline static int sock_error(int s)
175 unsigned int optval = 0, optlen;
177 optlen = sizeof(optval);
178 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
179 return optval ? sock_get_error(optval) : 0;
182 static void sock_poll_event( struct object *obj, int event )
184 struct sock *sock = (struct sock *)obj;
186 assert( sock->obj.ops == &sock_ops );
188 fprintf(stderr, "socket %d select event: %x\n", sock->obj.fd, event);
189 if (sock->state & FD_CONNECT)
194 /* we got connected */
195 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
196 sock->state &= ~FD_CONNECT;
197 sock->pmask |= FD_CONNECT;
198 sock->errors[FD_CONNECT_BIT] = 0;
200 fprintf(stderr, "socket %d connection success\n", sock->obj.fd);
202 else if (event & (POLLERR|POLLHUP))
204 /* we didn't get connected? */
205 sock->state &= ~FD_CONNECT;
206 sock->pmask |= FD_CONNECT;
207 sock->errors[FD_CONNECT_BIT] = sock_error( sock->obj.fd );
209 fprintf(stderr, "socket %d connection failure\n", sock->obj.fd);
212 if (sock->state & FD_WINE_LISTENING)
217 /* incoming connection */
218 sock->pmask |= FD_ACCEPT;
219 sock->errors[FD_ACCEPT_BIT] = 0;
220 sock->hmask |= FD_ACCEPT;
222 else if (event & (POLLERR|POLLHUP))
224 /* failed incoming connection? */
225 sock->pmask |= FD_ACCEPT;
226 sock->errors[FD_ACCEPT_BIT] = sock_error( sock->obj.fd );
227 sock->hmask |= FD_ACCEPT;
231 /* normal data flow */
236 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
237 * has been closed, so we need to check for it explicitly here */
238 if (!recv( sock->obj.fd, &dummy, 1, MSG_PEEK )) event = POLLHUP;
242 sock->pmask |= FD_READ;
243 sock->hmask |= FD_READ;
244 sock->errors[FD_READ_BIT] = 0;
246 fprintf(stderr, "socket %d is readable\n", sock->obj.fd );
251 sock->pmask |= FD_WRITE;
252 sock->hmask |= FD_WRITE;
253 sock->errors[FD_WRITE_BIT] = 0;
255 fprintf(stderr, "socket %d is writable\n", sock->obj.fd);
259 sock->pmask |= FD_OOB;
260 sock->hmask |= FD_OOB;
261 sock->errors[FD_OOB_BIT] = 0;
263 fprintf(stderr, "socket %d got OOB data\n", sock->obj.fd);
265 if (((event & POLLERR) || ((event & (POLLIN|POLLHUP)) == POLLHUP))
266 && (sock->state & (FD_READ|FD_WRITE))) {
268 sock->errors[FD_CLOSE_BIT] = sock_error( sock->obj.fd );
269 sock->state &= ~(FD_WINE_CONNECTED|FD_READ|FD_WRITE);
270 sock->pmask |= FD_CLOSE;
272 fprintf(stderr, "socket %d aborted by error %d\n",
273 sock->obj.fd, sock->errors[FD_CLOSE_BIT]);
277 if (event & (POLLERR|POLLHUP))
278 set_select_events( &sock->obj, -1 );
280 sock_reselect( sock );
282 /* wake up anyone waiting for whatever just happened */
283 if (sock->pmask & sock->mask) sock_wake_up( sock );
285 /* if anyone is stupid enough to wait on the socket object itself,
286 * maybe we should wake them up too, just in case? */
287 wake_up( &sock->obj, 0 );
290 static void sock_dump( struct object *obj, int verbose )
292 struct sock *sock = (struct sock *)obj;
293 assert( obj->ops == &sock_ops );
294 printf( "Socket fd=%d, state=%x, mask=%x, pending=%x, held=%x\n",
295 sock->obj.fd, sock->state,
296 sock->mask, sock->pmask, sock->hmask );
299 static int sock_signaled( struct object *obj, struct thread *thread )
301 struct sock *sock = (struct sock *)obj;
302 assert( obj->ops == &sock_ops );
304 return check_select_events( sock->obj.fd, sock_get_poll_events( &sock->obj ) );
307 static int sock_get_poll_events( struct object *obj )
309 struct sock *sock = (struct sock *)obj;
310 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
313 assert( obj->ops == &sock_ops );
315 if (sock->state & FD_CONNECT)
316 /* connecting, wait for writable */
318 if (sock->state & FD_WINE_LISTENING)
319 /* listening, wait for readable */
320 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
322 if (mask & FD_READ) ev |= POLLIN | POLLPRI;
323 if (mask & FD_WRITE) ev |= POLLOUT;
327 static int sock_get_fd( struct object *obj )
329 struct sock *sock = (struct sock *)obj;
330 assert( obj->ops == &sock_ops );
334 static int sock_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
336 struct sock *sock = (struct sock*) obj;
337 assert ( obj->ops == &sock_ops );
341 reply->type = FILE_TYPE_PIPE;
343 reply->access_time = 0;
344 reply->write_time = 0;
345 reply->size_high = 0;
348 reply->index_high = 0;
349 reply->index_low = 0;
353 if (sock->flags & WSA_FLAG_OVERLAPPED) *flags |= FD_FLAG_OVERLAPPED;
354 return FD_TYPE_DEFAULT;
357 static void sock_destroy( struct object *obj )
359 struct sock *sock = (struct sock *)obj;
360 assert( obj->ops == &sock_ops );
362 /* FIXME: special socket shutdown stuff? */
364 if ( sock->flags & WSA_FLAG_OVERLAPPED )
366 destroy_async_queue ( &sock->read_q );
367 destroy_async_queue ( &sock->write_q );
369 if (sock->event) release_object( sock->event );
372 /* create a new and unconnected socket */
373 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
378 sockfd = socket( family, type, protocol );
380 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
385 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
386 if (!(sock = alloc_object( &sock_ops, -1 ))) return NULL;
387 sock->obj.fd = sockfd;
388 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
397 sock_reselect( sock );
399 if (sock->flags & WSA_FLAG_OVERLAPPED)
401 init_async_queue (&sock->read_q);
402 init_async_queue (&sock->write_q);
407 /* accept a socket (creates a new fd) */
408 static struct sock *accept_socket( handle_t handle )
410 struct sock *acceptsock;
413 struct sockaddr saddr;
416 sock=(struct sock*)get_handle_obj(current->process,handle,
417 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
420 /* Try to accept(2). We can't be safe that this an already connected socket
421 * or that accept() is allowed on it. In those cases we will get -1/errno
424 slen = sizeof(saddr);
425 acceptfd = accept(sock->obj.fd,&saddr,&slen);
428 release_object( sock );
431 if (!(acceptsock = alloc_object( &sock_ops, -1 )))
433 release_object( sock );
437 /* newly created socket gets the same properties of the listening socket */
438 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
439 acceptsock->obj.fd = acceptfd;
440 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
441 if (sock->state & FD_WINE_NONBLOCKING)
442 acceptsock->state |= FD_WINE_NONBLOCKING;
443 acceptsock->mask = sock->mask;
444 acceptsock->hmask = 0;
445 acceptsock->pmask = 0;
446 acceptsock->event = NULL;
447 acceptsock->window = sock->window;
448 acceptsock->message = sock->message;
449 acceptsock->wparam = 0;
450 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
451 acceptsock->flags = sock->flags;
452 if ( acceptsock->flags & WSA_FLAG_OVERLAPPED )
454 init_async_queue ( &acceptsock->read_q );
455 init_async_queue ( &acceptsock->write_q );
459 sock->pmask &= ~FD_ACCEPT;
460 sock->hmask &= ~FD_ACCEPT;
461 sock_reselect( sock );
462 release_object( sock );
466 /* set the last error depending on errno */
467 static int sock_get_error( int err )
471 case EINTR: return WSAEINTR; break;
472 case EBADF: return WSAEBADF; break;
474 case EACCES: return WSAEACCES; break;
475 case EFAULT: return WSAEFAULT; break;
476 case EINVAL: return WSAEINVAL; break;
477 case EMFILE: return WSAEMFILE; break;
478 case EWOULDBLOCK: return WSAEWOULDBLOCK; break;
479 case EINPROGRESS: return WSAEINPROGRESS; break;
480 case EALREADY: return WSAEALREADY; break;
481 case ENOTSOCK: return WSAENOTSOCK; break;
482 case EDESTADDRREQ: return WSAEDESTADDRREQ; break;
483 case EMSGSIZE: return WSAEMSGSIZE; break;
484 case EPROTOTYPE: return WSAEPROTOTYPE; break;
485 case ENOPROTOOPT: return WSAENOPROTOOPT; break;
486 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT; break;
487 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT; break;
488 case EOPNOTSUPP: return WSAEOPNOTSUPP; break;
489 case EPFNOSUPPORT: return WSAEPFNOSUPPORT; break;
490 case EAFNOSUPPORT: return WSAEAFNOSUPPORT; break;
491 case EADDRINUSE: return WSAEADDRINUSE; break;
492 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL; break;
493 case ENETDOWN: return WSAENETDOWN; break;
494 case ENETUNREACH: return WSAENETUNREACH; break;
495 case ENETRESET: return WSAENETRESET; break;
496 case ECONNABORTED: return WSAECONNABORTED; break;
498 case ECONNRESET: return WSAECONNRESET; break;
499 case ENOBUFS: return WSAENOBUFS; break;
500 case EISCONN: return WSAEISCONN; break;
501 case ENOTCONN: return WSAENOTCONN; break;
502 case ESHUTDOWN: return WSAESHUTDOWN; break;
503 case ETOOMANYREFS: return WSAETOOMANYREFS; break;
504 case ETIMEDOUT: return WSAETIMEDOUT; break;
505 case ECONNREFUSED: return WSAECONNREFUSED; break;
506 case ELOOP: return WSAELOOP; break;
507 case ENAMETOOLONG: return WSAENAMETOOLONG; break;
508 case EHOSTDOWN: return WSAEHOSTDOWN; break;
509 case EHOSTUNREACH: return WSAEHOSTUNREACH; break;
510 case ENOTEMPTY: return WSAENOTEMPTY; break;
512 case EPROCLIM: return WSAEPROCLIM; break;
515 case EUSERS: return WSAEUSERS; break;
518 case EDQUOT: return WSAEDQUOT; break;
521 case ESTALE: return WSAESTALE; break;
524 case EREMOTE: return WSAEREMOTE; break;
526 default: errno=err; perror("sock_set_error"); return ERROR_UNKNOWN; break;
530 /* set the last error depending on errno */
531 static void sock_set_error(void)
533 set_error( sock_get_error( errno ) );
536 /* create a socket */
537 DECL_HANDLER(create_socket)
542 if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
544 reply->handle = alloc_handle( current->process, obj, req->access, req->inherit );
545 release_object( obj );
549 /* accept a socket */
550 DECL_HANDLER(accept_socket)
555 if ((sock = accept_socket( req->lhandle )) != NULL)
557 reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->inherit );
558 sock->wparam = reply->handle; /* wparam for message is the socket handle */
559 sock_reselect( sock );
560 release_object( &sock->obj );
564 /* set socket event parameters */
565 DECL_HANDLER(set_socket_event)
568 struct event *old_event;
570 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
571 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &sock_ops)))
573 old_event = sock->event;
574 sock->mask = req->mask;
576 sock->window = req->window;
577 sock->message = req->msg;
578 sock->wparam = req->handle; /* wparam is the socket handle */
579 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
581 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
582 sock_reselect( sock );
584 sock->state |= FD_WINE_NONBLOCKING;
586 /* if a network event is pending, signal the event object
587 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
588 before a WSAEventSelect() was done on it.
589 (when dealing with Asynchronous socket) */
590 if (sock->pmask & sock->mask) sock_wake_up( sock );
592 if (old_event) release_object( old_event ); /* we're through with it */
593 release_object( &sock->obj );
596 /* get socket event parameters */
597 DECL_HANDLER(get_socket_event)
601 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
607 set_error( WSAENOTSOCK );
610 reply->mask = sock->mask;
611 reply->pmask = sock->pmask;
612 reply->state = sock->state;
613 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
619 struct event *cevent = get_event_obj( current->process, req->c_event,
620 EVENT_MODIFY_STATE );
623 reset_event( cevent );
624 release_object( cevent );
628 sock_reselect( sock );
630 release_object( &sock->obj );
633 /* re-enable pending socket events */
634 DECL_HANDLER(enable_socket_event)
638 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
639 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, &sock_ops)))
642 sock->pmask &= ~req->mask; /* is this safe? */
643 sock->hmask &= ~req->mask;
644 sock->state |= req->sstate;
645 sock->state &= ~req->cstate;
646 sock_reselect( sock );
647 release_object( &sock->obj );