2 * Server-side socket management
4 * Copyright (C) 1999 Marcus Meissner, Ove Kåven
6 * FIXME: we use read|write access in all cases. Shouldn't we depend that
7 * on the access of the current handle?
18 #ifdef HAVE_SYS_ERRNO_H
19 # include <sys/errno.h>
23 #include <sys/types.h>
24 #ifdef HAVE_SYS_SOCKET_H
25 # include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #ifdef HAVE_SYS_FILIO_H
29 # include <sys/filio.h>
44 struct object obj; /* object header */
45 struct select_user select; /* select user */
46 unsigned int state; /* status bits */
47 unsigned int mask; /* event mask */
48 unsigned int hmask; /* held (blocked) events */
49 unsigned int pmask; /* pending events */
50 struct event *event; /* event object */
51 int errors[FD_MAX_EVENTS]; /* event errors */
54 static void sock_dump( struct object *obj, int verbose );
55 static int sock_add_queue( struct object *obj, struct wait_queue_entry *entry );
56 static void sock_remove_queue( struct object *obj, struct wait_queue_entry *entry );
57 static int sock_signaled( struct object *obj, struct thread *thread );
58 static int sock_get_fd( struct object *obj );
59 static void sock_destroy( struct object *obj );
60 static void sock_set_error(void);
62 static const struct object_ops sock_ops =
77 static int sock_event( struct sock *sock )
79 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
80 int ev = EXCEPT_EVENT;
82 if (sock->state & WS_FD_CONNECT)
83 /* connecting, wait for writable */
84 return WRITE_EVENT | EXCEPT_EVENT;
85 if (sock->state & WS_FD_LISTENING)
86 /* listening, wait for readable */
87 return ((sock->hmask & FD_ACCEPT) ? 0 : READ_EVENT) | EXCEPT_EVENT;
89 if (mask & FD_READ) ev |= READ_EVENT;
90 if (mask & FD_WRITE) ev |= WRITE_EVENT;
94 static void sock_reselect( struct sock *sock )
96 int ev = sock_event( sock );
98 fprintf(stderr,"sock_reselect(%d): new mask %x\n", sock->select.fd, ev);
99 set_select_events( &sock->select, ev );
102 inline static int sock_error(int s)
104 unsigned int optval, optlen;
106 optlen = sizeof(optval);
107 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
111 static void sock_select_event( int event, void *private )
113 struct sock *sock = (struct sock *)private;
115 assert( sock->obj.ops == &sock_ops );
117 fprintf(stderr, "socket %d select event: %x\n", sock->select.fd, event);
118 if (sock->state & WS_FD_CONNECT)
121 if (event & WRITE_EVENT)
123 /* we got connected */
124 sock->state |= WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE;
125 sock->state &= ~WS_FD_CONNECT;
126 sock->pmask |= FD_CONNECT;
127 sock->errors[FD_CONNECT_BIT] = 0;
129 fprintf(stderr, "socket %d connection success\n", sock->select.fd);
131 else if (event & EXCEPT_EVENT)
133 /* we didn't get connected? */
134 sock->state &= ~WS_FD_CONNECT;
135 sock->pmask |= FD_CONNECT;
136 sock->errors[FD_CONNECT_BIT] = sock_error( sock->select.fd );
138 fprintf(stderr, "socket %d connection failure\n", sock->select.fd);
141 if (sock->state & WS_FD_LISTENING)
144 if (event & READ_EVENT)
146 /* incoming connection */
147 sock->pmask |= FD_ACCEPT;
148 sock->errors[FD_ACCEPT_BIT] = 0;
149 sock->hmask |= FD_ACCEPT;
151 else if (event & EXCEPT_EVENT)
153 /* failed incoming connection? */
154 sock->pmask |= FD_ACCEPT;
155 sock->errors[FD_ACCEPT_BIT] = sock_error( sock->select.fd );
156 sock->hmask |= FD_ACCEPT;
160 /* normal data flow */
161 if (event & READ_EVENT)
163 /* make sure there's data here */
165 ioctl(sock->select.fd, FIONREAD, (char*)&bytes);
169 sock->pmask |= FD_READ;
170 sock->hmask |= FD_READ;
171 sock->errors[FD_READ_BIT] = 0;
173 fprintf(stderr, "socket %d has %d bytes\n", sock->select.fd, bytes);
177 /* 0 bytes readable == socket closed cleanly */
178 sock->state &= ~(WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE);
179 sock->pmask |= FD_CLOSE;
180 sock->errors[FD_CLOSE_BIT] = 0;
182 fprintf(stderr, "socket %d is closing\n", sock->select.fd);
185 if (event & WRITE_EVENT)
187 sock->pmask |= FD_WRITE;
188 sock->hmask |= FD_WRITE;
189 sock->errors[FD_WRITE_BIT] = 0;
191 fprintf(stderr, "socket %d is writable\n", sock->select.fd);
193 if (event & EXCEPT_EVENT)
195 sock->errors[FD_CLOSE_BIT] = sock_error( sock->select.fd );
196 if (sock->errors[FD_CLOSE_BIT])
198 /* we got an error, socket closing? */
199 sock->state &= ~(WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE);
200 sock->pmask |= FD_CLOSE;
202 fprintf(stderr, "socket %d aborted by error %d\n", sock->select.fd, sock->errors[FD_CLOSE_BIT]);
206 /* no error, OOB data? */
207 sock->pmask |= FD_OOB;
208 sock->hmask |= FD_OOB;
210 fprintf(stderr, "socket %d got OOB data\n", sock->select.fd);
215 sock_reselect( sock );
216 /* wake up anyone waiting for whatever just happened */
217 emask = sock->pmask & sock->mask;
218 if (debug_level && emask)
219 fprintf(stderr, "socket %d pending events: %x\n", sock->select.fd, emask);
220 if (emask && sock->event) {
221 if (debug_level) fprintf(stderr, "signalling event ptr %p\n", sock->event);
222 set_event(sock->event);
225 /* if anyone is stupid enough to wait on the socket object itself,
226 * maybe we should wake them up too, just in case? */
227 wake_up( &sock->obj, 0 );
230 static void sock_dump( struct object *obj, int verbose )
232 struct sock *sock = (struct sock *)obj;
233 assert( obj->ops == &sock_ops );
234 printf( "Socket fd=%d, state=%x, mask=%x, pending=%x, held=%x\n",
235 sock->select.fd, sock->state,
236 sock->mask, sock->pmask, sock->hmask );
239 static int sock_add_queue( struct object *obj, struct wait_queue_entry *entry )
241 /* struct sock *sock = (struct sock *)obj; */
242 assert( obj->ops == &sock_ops );
244 add_queue( obj, entry );
248 static void sock_remove_queue( struct object *obj, struct wait_queue_entry *entry )
250 /* struct sock *sock = (struct sock *)grab_object(obj); */
251 assert( obj->ops == &sock_ops );
253 remove_queue( obj, entry );
254 /* release_object( obj ); */
257 static int sock_signaled( struct object *obj, struct thread *thread )
259 struct sock *sock = (struct sock *)obj;
260 assert( obj->ops == &sock_ops );
262 return check_select_events( &sock->select, sock_event( sock ) );
265 static int sock_get_fd( struct object *obj )
267 struct sock *sock = (struct sock *)obj;
269 assert( obj->ops == &sock_ops );
270 fd = dup( sock->select.fd );
276 static void sock_destroy( struct object *obj )
278 struct sock *sock = (struct sock *)obj;
279 assert( obj->ops == &sock_ops );
281 unregister_select_user( &sock->select );
282 /* FIXME: special socket shutdown stuff? */
283 close( sock->select.fd );
286 /* if the service thread was waiting for the event object,
287 * we should now signal it, to let the service thread
288 * object detect that it is now orphaned... */
289 if (sock->mask & WS_FD_SERVEVENT)
290 set_event( sock->event );
291 /* we're through with it */
292 release_object( sock->event );
296 /* create a new and unconnected socket */
297 static struct object *create_socket( int family, int type, int protocol )
302 sockfd = socket( family, type, protocol );
304 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
309 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
310 if (!(sock = alloc_object( &sock_ops ))) {
314 sock->select.fd = sockfd;
315 sock->select.func = sock_select_event;
316 sock->select.private = sock;
317 sock->state = (type!=SOCK_STREAM) ? WS_FD_READ|WS_FD_WRITE : 0;
322 register_select_user( &sock->select );
323 sock_reselect( sock );
328 /* accept a socket (creates a new fd) */
329 static struct object *accept_socket( int handle )
331 struct sock *acceptsock;
334 struct sockaddr saddr;
337 sock=(struct sock*)get_handle_obj(current->process,handle,
338 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
341 /* Try to accept(2). We can't be safe that this an already connected socket
342 * or that accept() is allowed on it. In those cases we will get -1/errno
345 slen = sizeof(saddr);
346 acceptfd = accept(sock->select.fd,&saddr,&slen);
349 release_object( sock );
352 if (!(acceptsock = alloc_object( &sock_ops )))
355 release_object( sock );
359 acceptsock->select.fd = acceptfd;
360 acceptsock->select.func = sock_select_event;
361 acceptsock->select.private = acceptsock;
362 acceptsock->state = WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE;
363 acceptsock->mask = sock->mask;
364 acceptsock->hmask = 0;
365 acceptsock->pmask = 0;
367 acceptsock->event = (struct event *)grab_object( sock->event );
369 acceptsock->event = NULL;
371 register_select_user( &acceptsock->select );
372 sock_reselect( acceptsock );
374 sock->pmask &= ~FD_ACCEPT;
375 sock->hmask &= ~FD_ACCEPT;
376 release_object( sock );
377 return &acceptsock->obj;
380 /* set the last error depending on errno */
381 static void sock_set_error(void)
385 case EINTR: set_error(WSAEINTR);break;
386 case EBADF: set_error(WSAEBADF);break;
388 case EACCES: set_error(WSAEACCES);break;
389 case EFAULT: set_error(WSAEFAULT);break;
390 case EINVAL: set_error(WSAEINVAL);break;
391 case EMFILE: set_error(WSAEMFILE);break;
392 case EWOULDBLOCK: set_error(WSAEWOULDBLOCK);break;
393 case EINPROGRESS: set_error(WSAEINPROGRESS);break;
394 case EALREADY: set_error(WSAEALREADY);break;
395 case ENOTSOCK: set_error(WSAENOTSOCK);break;
396 case EDESTADDRREQ: set_error(WSAEDESTADDRREQ);break;
397 case EMSGSIZE: set_error(WSAEMSGSIZE);break;
398 case EPROTOTYPE: set_error(WSAEPROTOTYPE);break;
399 case ENOPROTOOPT: set_error(WSAENOPROTOOPT);break;
400 case EPROTONOSUPPORT: set_error(WSAEPROTONOSUPPORT);break;
401 case ESOCKTNOSUPPORT: set_error(WSAESOCKTNOSUPPORT);break;
402 case EOPNOTSUPP: set_error(WSAEOPNOTSUPP);break;
403 case EPFNOSUPPORT: set_error(WSAEPFNOSUPPORT);break;
404 case EAFNOSUPPORT: set_error(WSAEAFNOSUPPORT);break;
405 case EADDRINUSE: set_error(WSAEADDRINUSE);break;
406 case EADDRNOTAVAIL: set_error(WSAEADDRNOTAVAIL);break;
407 case ENETDOWN: set_error(WSAENETDOWN);break;
408 case ENETUNREACH: set_error(WSAENETUNREACH);break;
409 case ENETRESET: set_error(WSAENETRESET);break;
410 case ECONNABORTED: set_error(WSAECONNABORTED);break;
412 case ECONNRESET: set_error(WSAECONNRESET);break;
413 case ENOBUFS: set_error(WSAENOBUFS);break;
414 case EISCONN: set_error(WSAEISCONN);break;
415 case ENOTCONN: set_error(WSAENOTCONN);break;
416 case ESHUTDOWN: set_error(WSAESHUTDOWN);break;
417 case ETOOMANYREFS: set_error(WSAETOOMANYREFS);break;
418 case ETIMEDOUT: set_error(WSAETIMEDOUT);break;
419 case ECONNREFUSED: set_error(WSAECONNREFUSED);break;
420 case ELOOP: set_error(WSAELOOP);break;
421 case ENAMETOOLONG: set_error(WSAENAMETOOLONG);break;
422 case EHOSTDOWN: set_error(WSAEHOSTDOWN);break;
423 case EHOSTUNREACH: set_error(WSAEHOSTUNREACH);break;
424 case ENOTEMPTY: set_error(WSAENOTEMPTY);break;
426 case EPROCLIM: set_error(WSAEPROCLIM);break;
429 case EUSERS: set_error(WSAEUSERS);break;
432 case EDQUOT: set_error(WSAEDQUOT);break;
435 case ESTALE: set_error(WSAESTALE);break;
438 case EREMOTE: set_error(WSAEREMOTE);break;
440 default: perror("sock_set_error"); set_error( ERROR_UNKNOWN ); break;
444 /* create a socket */
445 DECL_HANDLER(create_socket)
450 if ((obj = create_socket( req->family, req->type, req->protocol )) != NULL)
452 s = alloc_handle( current->process, obj, req->access, req->inherit );
453 release_object( obj );
458 /* accept a socket */
459 DECL_HANDLER(accept_socket)
464 if ((obj = accept_socket( req->lhandle )) != NULL)
466 s = alloc_handle( current->process, obj, req->access, req->inherit );
467 release_object( obj );
472 /* set socket event parameters */
473 DECL_HANDLER(set_socket_event)
476 struct event *oevent;
479 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
482 oevent = sock->event;
484 sock->mask = req->mask;
485 sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
486 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
487 sock_reselect( sock );
489 sock->state |= WS_FD_NONBLOCKING;
492 if ((oevent != sock->event) && (omask & WS_FD_SERVEVENT))
493 /* if the service thread was waiting for the old event object,
494 * we should now signal it, to let the service thread
495 * object detect that it is now orphaned... */
497 /* we're through with it */
498 release_object( oevent );
500 release_object( &sock->obj );
503 /* get socket event parameters */
504 DECL_HANDLER(get_socket_event)
508 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
514 set_error(WSAENOTSOCK);
517 req->mask = sock->mask;
518 req->pmask = sock->pmask;
519 req->state = sock->state;
520 memcpy(req->errors, sock->errors, sizeof(sock->errors));
526 struct event *sevent = get_event_obj(current->process, req->s_event, 0);
527 if (sevent == sock->event)
529 release_object( sevent );
534 sock_reselect( sock );
536 else set_error(WSAEINVAL);
538 release_object( &sock->obj );
541 /* re-enable pending socket events */
542 DECL_HANDLER(enable_socket_event)
546 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
549 sock->pmask &= ~req->mask; /* is this safe? */
550 sock->hmask &= ~req->mask;
551 sock->state |= req->sstate;
552 sock->state &= ~req->cstate;
553 sock_reselect( sock );
554 release_object( &sock->obj );