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>
22 #include <sys/types.h>
23 #ifdef HAVE_SYS_SOCKET_H
24 # include <sys/socket.h>
26 #include <sys/ioctl.h>
27 #ifdef HAVE_SYS_FILIO_H
28 # include <sys/filio.h>
40 /* To avoid conflicts with the Unix socket headers. Plus we only need a few
48 struct object obj; /* object header */
49 unsigned int state; /* status bits */
50 unsigned int mask; /* event mask */
51 unsigned int hmask; /* held (blocked) events */
52 unsigned int pmask; /* pending events */
53 struct event *event; /* event object */
54 int errors[FD_MAX_EVENTS]; /* event errors */
57 static void sock_dump( struct object *obj, int verbose );
58 static int sock_signaled( struct object *obj, struct thread *thread );
59 static int sock_get_poll_events( struct object *obj );
60 static void sock_poll_event( struct object *obj, int event );
61 static int sock_get_fd( struct object *obj );
62 static int sock_get_info( struct object *obj, struct get_file_info_reply *reply );
63 static void sock_destroy( struct object *obj );
64 static int sock_get_error( int err );
65 static void sock_set_error(void);
67 static const struct object_ops sock_ops =
69 sizeof(struct sock), /* size */
71 add_queue, /* add_queue */
72 remove_queue, /* remove_queue */
73 sock_signaled, /* signaled */
74 no_satisfied, /* satisfied */
75 sock_get_poll_events, /* get_poll_events */
76 sock_poll_event, /* poll_event */
77 sock_get_fd, /* get_fd */
79 sock_get_info, /* get_file_info */
80 NULL, /* queue_async */
81 sock_destroy /* destroy */
84 static void sock_reselect( struct sock *sock )
86 int ev = sock_get_poll_events( &sock->obj );
90 fprintf(stderr,"sock_reselect(%d): new mask %x\n", sock->obj.fd, ev);
92 if (sock->obj.select == -1) {
93 /* previously unconnected socket, is this reselect supposed to connect it? */
94 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return;
95 /* ok, it is, attach it to the wineserver's main poll loop */
96 add_select_user( &sock->obj );
98 /* update condition mask */
99 set_select_events( &sock->obj, ev );
101 /* check whether condition is satisfied already */
102 pfd.fd = sock->obj.fd;
107 sock_poll_event( &sock->obj, pfd.revents);
110 inline static int sock_error(int s)
112 unsigned int optval = 0, optlen;
114 optlen = sizeof(optval);
115 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
116 return optval ? sock_get_error(optval) : 0;
119 static void sock_poll_event( struct object *obj, int event )
121 struct sock *sock = (struct sock *)obj;
123 assert( sock->obj.ops == &sock_ops );
125 fprintf(stderr, "socket %d select event: %x\n", sock->obj.fd, event);
126 if (sock->state & FD_CONNECT)
131 /* we got connected */
132 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
133 sock->state &= ~FD_CONNECT;
134 sock->pmask |= FD_CONNECT;
135 sock->errors[FD_CONNECT_BIT] = 0;
137 fprintf(stderr, "socket %d connection success\n", sock->obj.fd);
139 else if (event & (POLLERR|POLLHUP))
141 /* we didn't get connected? */
142 sock->state &= ~FD_CONNECT;
143 sock->pmask |= FD_CONNECT;
144 sock->errors[FD_CONNECT_BIT] = sock_error( sock->obj.fd );
146 fprintf(stderr, "socket %d connection failure\n", sock->obj.fd);
149 if (sock->state & FD_WINE_LISTENING)
154 /* incoming connection */
155 sock->pmask |= FD_ACCEPT;
156 sock->errors[FD_ACCEPT_BIT] = 0;
157 sock->hmask |= FD_ACCEPT;
159 else if (event & (POLLERR|POLLHUP))
161 /* failed incoming connection? */
162 sock->pmask |= FD_ACCEPT;
163 sock->errors[FD_ACCEPT_BIT] = sock_error( sock->obj.fd );
164 sock->hmask |= FD_ACCEPT;
168 /* normal data flow */
173 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
174 * has been closed, so we need to check for it explicitly here */
175 if (!recv( sock->obj.fd, &dummy, 1, MSG_PEEK )) event = POLLHUP;
179 sock->pmask |= FD_READ;
180 sock->hmask |= FD_READ;
181 sock->errors[FD_READ_BIT] = 0;
183 fprintf(stderr, "socket %d is readable\n", sock->obj.fd );
188 sock->pmask |= FD_WRITE;
189 sock->hmask |= FD_WRITE;
190 sock->errors[FD_WRITE_BIT] = 0;
192 fprintf(stderr, "socket %d is writable\n", sock->obj.fd);
196 sock->pmask |= FD_OOB;
197 sock->hmask |= FD_OOB;
198 sock->errors[FD_OOB_BIT] = 0;
200 fprintf(stderr, "socket %d got OOB data\n", sock->obj.fd);
202 if (((event & POLLERR) || ((event & (POLLIN|POLLHUP)) == POLLHUP))
203 && (sock->state & (FD_READ|FD_WRITE))) {
205 sock->errors[FD_CLOSE_BIT] = sock_error( sock->obj.fd );
206 sock->state &= ~(FD_WINE_CONNECTED|FD_READ|FD_WRITE);
207 sock->pmask |= FD_CLOSE;
209 fprintf(stderr, "socket %d aborted by error %d\n",
210 sock->obj.fd, sock->errors[FD_CLOSE_BIT]);
214 if (event & (POLLERR|POLLHUP))
215 set_select_events( &sock->obj, -1 );
217 sock_reselect( sock );
218 /* wake up anyone waiting for whatever just happened */
219 emask = sock->pmask & sock->mask;
220 if (debug_level && emask)
221 fprintf(stderr, "socket %d pending events: %x\n", sock->obj.fd, emask);
222 if (emask && sock->event) {
223 if (debug_level) fprintf(stderr, "signalling event ptr %p\n", sock->event);
224 set_event(sock->event);
227 /* if anyone is stupid enough to wait on the socket object itself,
228 * maybe we should wake them up too, just in case? */
229 wake_up( &sock->obj, 0 );
232 static void sock_dump( struct object *obj, int verbose )
234 struct sock *sock = (struct sock *)obj;
235 assert( obj->ops == &sock_ops );
236 printf( "Socket fd=%d, state=%x, mask=%x, pending=%x, held=%x\n",
237 sock->obj.fd, sock->state,
238 sock->mask, sock->pmask, sock->hmask );
241 static int sock_signaled( struct object *obj, struct thread *thread )
243 struct sock *sock = (struct sock *)obj;
244 assert( obj->ops == &sock_ops );
246 return check_select_events( sock->obj.fd, sock_get_poll_events( &sock->obj ) );
249 static int sock_get_poll_events( struct object *obj )
251 struct sock *sock = (struct sock *)obj;
252 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
255 assert( obj->ops == &sock_ops );
257 if (sock->state & FD_CONNECT)
258 /* connecting, wait for writable */
260 if (sock->state & FD_WINE_LISTENING)
261 /* listening, wait for readable */
262 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
264 if (mask & FD_READ) ev |= POLLIN | POLLPRI;
265 if (mask & FD_WRITE) ev |= POLLOUT;
269 static int sock_get_fd( struct object *obj )
271 struct sock *sock = (struct sock *)obj;
272 assert( obj->ops == &sock_ops );
276 static int sock_get_info( struct object *obj, struct get_file_info_reply *reply )
280 reply->type = FILE_TYPE_PIPE;
282 reply->access_time = 0;
283 reply->write_time = 0;
284 reply->size_high = 0;
287 reply->index_high = 0;
288 reply->index_low = 0;
291 return FD_TYPE_DEFAULT;
294 static void sock_destroy( struct object *obj )
296 struct sock *sock = (struct sock *)obj;
297 assert( obj->ops == &sock_ops );
299 /* FIXME: special socket shutdown stuff? */
302 /* if the service thread was waiting for the event object,
303 * we should now signal it, to let the service thread
304 * object detect that it is now orphaned... */
305 if (sock->mask & FD_WINE_SERVEVENT)
306 set_event( sock->event );
307 /* we're through with it */
308 release_object( sock->event );
312 /* create a new and unconnected socket */
313 static struct object *create_socket( int family, int type, int protocol )
318 sockfd = socket( family, type, protocol );
320 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
325 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
326 if (!(sock = alloc_object( &sock_ops, -1 ))) return NULL;
327 sock->obj.fd = sockfd;
328 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
333 sock_reselect( sock );
338 /* accept a socket (creates a new fd) */
339 static struct object *accept_socket( handle_t handle )
341 struct sock *acceptsock;
344 struct sockaddr saddr;
347 sock=(struct sock*)get_handle_obj(current->process,handle,
348 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
351 /* Try to accept(2). We can't be safe that this an already connected socket
352 * or that accept() is allowed on it. In those cases we will get -1/errno
355 slen = sizeof(saddr);
356 acceptfd = accept(sock->obj.fd,&saddr,&slen);
359 release_object( sock );
362 if (!(acceptsock = alloc_object( &sock_ops, -1 )))
364 release_object( sock );
368 /* newly created socket gets the same properties of the listening socket */
369 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
370 acceptsock->obj.fd = acceptfd;
371 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
372 if (sock->state & FD_WINE_NONBLOCKING)
373 acceptsock->state |= FD_WINE_NONBLOCKING;
374 acceptsock->mask = sock->mask;
375 acceptsock->hmask = 0;
376 acceptsock->pmask = 0;
377 acceptsock->event = NULL;
378 if (sock->event && !(sock->mask & FD_WINE_SERVEVENT))
379 acceptsock->event = (struct event *)grab_object( sock->event );
381 sock_reselect( acceptsock );
383 sock->pmask &= ~FD_ACCEPT;
384 sock->hmask &= ~FD_ACCEPT;
385 sock_reselect( sock );
386 release_object( sock );
387 return &acceptsock->obj;
390 /* set the last error depending on errno */
391 static int sock_get_error( int err )
395 case EINTR: return WSAEINTR; break;
396 case EBADF: return WSAEBADF; break;
398 case EACCES: return WSAEACCES; break;
399 case EFAULT: return WSAEFAULT; break;
400 case EINVAL: return WSAEINVAL; break;
401 case EMFILE: return WSAEMFILE; break;
402 case EWOULDBLOCK: return WSAEWOULDBLOCK; break;
403 case EINPROGRESS: return WSAEINPROGRESS; break;
404 case EALREADY: return WSAEALREADY; break;
405 case ENOTSOCK: return WSAENOTSOCK; break;
406 case EDESTADDRREQ: return WSAEDESTADDRREQ; break;
407 case EMSGSIZE: return WSAEMSGSIZE; break;
408 case EPROTOTYPE: return WSAEPROTOTYPE; break;
409 case ENOPROTOOPT: return WSAENOPROTOOPT; break;
410 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT; break;
411 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT; break;
412 case EOPNOTSUPP: return WSAEOPNOTSUPP; break;
413 case EPFNOSUPPORT: return WSAEPFNOSUPPORT; break;
414 case EAFNOSUPPORT: return WSAEAFNOSUPPORT; break;
415 case EADDRINUSE: return WSAEADDRINUSE; break;
416 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL; break;
417 case ENETDOWN: return WSAENETDOWN; break;
418 case ENETUNREACH: return WSAENETUNREACH; break;
419 case ENETRESET: return WSAENETRESET; break;
420 case ECONNABORTED: return WSAECONNABORTED; break;
422 case ECONNRESET: return WSAECONNRESET; break;
423 case ENOBUFS: return WSAENOBUFS; break;
424 case EISCONN: return WSAEISCONN; break;
425 case ENOTCONN: return WSAENOTCONN; break;
426 case ESHUTDOWN: return WSAESHUTDOWN; break;
427 case ETOOMANYREFS: return WSAETOOMANYREFS; break;
428 case ETIMEDOUT: return WSAETIMEDOUT; break;
429 case ECONNREFUSED: return WSAECONNREFUSED; break;
430 case ELOOP: return WSAELOOP; break;
431 case ENAMETOOLONG: return WSAENAMETOOLONG; break;
432 case EHOSTDOWN: return WSAEHOSTDOWN; break;
433 case EHOSTUNREACH: return WSAEHOSTUNREACH; break;
434 case ENOTEMPTY: return WSAENOTEMPTY; break;
436 case EPROCLIM: return WSAEPROCLIM; break;
439 case EUSERS: return WSAEUSERS; break;
442 case EDQUOT: return WSAEDQUOT; break;
445 case ESTALE: return WSAESTALE; break;
448 case EREMOTE: return WSAEREMOTE; break;
450 default: errno=err; perror("sock_set_error"); return ERROR_UNKNOWN; break;
454 /* set the last error depending on errno */
455 static void sock_set_error(void)
457 set_error( sock_get_error( errno ) );
460 /* create a socket */
461 DECL_HANDLER(create_socket)
466 if ((obj = create_socket( req->family, req->type, req->protocol )) != NULL)
468 reply->handle = alloc_handle( current->process, obj, req->access, req->inherit );
469 release_object( obj );
473 /* accept a socket */
474 DECL_HANDLER(accept_socket)
479 if ((obj = accept_socket( req->lhandle )) != NULL)
481 reply->handle = alloc_handle( current->process, obj, req->access, req->inherit );
482 release_object( obj );
486 /* set socket event parameters */
487 DECL_HANDLER(set_socket_event)
490 struct event *oevent;
493 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
496 oevent = sock->event;
498 sock->mask = req->mask;
499 sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
500 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
501 sock_reselect( sock );
503 sock->state |= FD_WINE_NONBLOCKING;
505 /* if a network event is pending, signal the event object
506 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
507 before a WSAEventSelect() was done on it.
508 (when dealing with Asynchronous socket) */
509 if (sock->pmask & sock->mask)
510 set_event(sock->event);
514 if ((oevent != sock->event) && (omask & FD_WINE_SERVEVENT))
515 /* if the service thread was waiting for the old event object,
516 * we should now signal it, to let the service thread
517 * object detect that it is now orphaned... */
519 /* we're through with it */
520 release_object( oevent );
522 release_object( &sock->obj );
525 /* get socket event parameters */
526 DECL_HANDLER(get_socket_event)
530 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
536 set_error( WSAENOTSOCK );
539 reply->mask = sock->mask;
540 reply->pmask = sock->pmask;
541 reply->state = sock->state;
542 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
546 handle_t s_event = req->s_event;
549 struct event *sevent = get_event_obj(current->process, req->s_event, 0);
550 if (sevent == sock->event) s_event = 0;
551 release_object( sevent );
557 struct event *cevent = get_event_obj(current->process, req->c_event, EVENT_MODIFY_STATE);
558 reset_event( cevent );
559 release_object( cevent );
562 sock_reselect( sock );
564 else set_error(WSAEINVAL);
566 release_object( &sock->obj );
569 /* re-enable pending socket events */
570 DECL_HANDLER(enable_socket_event)
574 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
577 sock->pmask &= ~req->mask; /* is this safe? */
578 sock->hmask &= ~req->mask;
579 sock->state |= req->sstate;
580 sock->state &= ~req->cstate;
581 sock_reselect( sock );
583 /* service trigger */
584 if (req->mask & FD_WINE_SERVEVENT)
586 sock->pmask |= FD_WINE_SERVEVENT;
588 if (debug_level) fprintf(stderr, "signalling service event ptr %p\n", sock->event);
589 set_event(sock->event);
593 release_object( &sock->obj );