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 unsigned int state; /* status bits */
46 unsigned int mask; /* event mask */
47 unsigned int hmask; /* held (blocked) events */
48 unsigned int pmask; /* pending events */
49 struct event *event; /* event object */
50 int errors[FD_MAX_EVENTS]; /* event errors */
53 static void sock_dump( struct object *obj, int verbose );
54 static int sock_signaled( struct object *obj, struct thread *thread );
55 static int sock_get_poll_events( struct object *obj );
56 static void sock_poll_event( struct object *obj, int event );
57 static int sock_get_fd( struct object *obj );
58 static void sock_destroy( struct object *obj );
59 static int sock_get_error( int err );
60 static void sock_set_error(void);
62 static const struct object_ops sock_ops =
64 sizeof(struct sock), /* size */
66 add_queue, /* add_queue */
67 remove_queue, /* remove_queue */
68 sock_signaled, /* signaled */
69 no_satisfied, /* satisfied */
70 sock_get_poll_events, /* get_poll_events */
71 sock_poll_event, /* poll_event */
72 sock_get_fd, /* get_fd */
74 no_get_file_info, /* get_file_info */
75 sock_destroy /* destroy */
78 static void sock_reselect( struct sock *sock )
80 int ev = sock_get_poll_events( &sock->obj );
84 fprintf(stderr,"sock_reselect(%d): new mask %x\n", sock->obj.fd, ev);
86 if (sock->obj.select == -1) {
87 /* previously unconnected socket, is this reselect supposed to connect it? */
88 if (!sock->state) return;
89 /* ok, it is, attach it to the wineserver's main poll loop */
90 add_select_user( &sock->obj );
92 /* update condition mask */
93 set_select_events( &sock->obj, ev );
95 /* check whether condition is satisfied already */
96 pfd.fd = sock->obj.fd;
101 sock_poll_event( &sock->obj, pfd.revents);
104 inline static int sock_error(int s)
106 unsigned int optval = 0, optlen;
108 optlen = sizeof(optval);
109 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
110 return optval ? sock_get_error(optval) : 0;
113 static void sock_poll_event( struct object *obj, int event )
115 struct sock *sock = (struct sock *)obj;
117 assert( sock->obj.ops == &sock_ops );
119 fprintf(stderr, "socket %d select event: %x\n", sock->obj.fd, event);
120 if (sock->state & WS_FD_CONNECT)
125 /* we got connected */
126 sock->state |= WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE;
127 sock->state &= ~WS_FD_CONNECT;
128 sock->pmask |= FD_CONNECT;
129 sock->errors[FD_CONNECT_BIT] = 0;
131 fprintf(stderr, "socket %d connection success\n", sock->obj.fd);
133 else if (event & (POLLERR|POLLHUP))
135 /* we didn't get connected? */
136 sock->state &= ~WS_FD_CONNECT;
137 sock->pmask |= FD_CONNECT;
138 sock->errors[FD_CONNECT_BIT] = sock_error( sock->obj.fd );
140 fprintf(stderr, "socket %d connection failure\n", sock->obj.fd);
143 if (sock->state & WS_FD_LISTENING)
148 /* incoming connection */
149 sock->pmask |= FD_ACCEPT;
150 sock->errors[FD_ACCEPT_BIT] = 0;
151 sock->hmask |= FD_ACCEPT;
153 else if (event & (POLLERR|POLLHUP))
155 /* failed incoming connection? */
156 sock->pmask |= FD_ACCEPT;
157 sock->errors[FD_ACCEPT_BIT] = sock_error( sock->obj.fd );
158 sock->hmask |= FD_ACCEPT;
162 /* normal data flow */
166 sock->pmask |= FD_READ;
167 sock->hmask |= FD_READ;
168 sock->errors[FD_READ_BIT] = 0;
170 fprintf(stderr, "socket %d is readable\n", sock->obj.fd );
174 sock->pmask |= FD_WRITE;
175 sock->hmask |= FD_WRITE;
176 sock->errors[FD_WRITE_BIT] = 0;
178 fprintf(stderr, "socket %d is writable\n", sock->obj.fd);
182 sock->pmask |= FD_OOB;
183 sock->hmask |= FD_OOB;
184 sock->errors[FD_OOB_BIT] = 0;
186 fprintf(stderr, "socket %d got OOB data\n", sock->obj.fd);
188 if (((event & POLLERR) || ((event & (POLLIN|POLLHUP)) == POLLHUP))
189 && (sock->state & (WS_FD_READ|WS_FD_WRITE))) {
191 sock->errors[FD_CLOSE_BIT] = sock_error( sock->obj.fd );
192 sock->state &= ~(WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE);
193 sock->pmask |= FD_CLOSE;
195 fprintf(stderr, "socket %d aborted by error %d\n",
196 sock->obj.fd, sock->errors[FD_CLOSE_BIT]);
200 if (event & (POLLERR|POLLHUP))
201 set_select_events( &sock->obj, -1 );
203 sock_reselect( sock );
204 /* wake up anyone waiting for whatever just happened */
205 emask = sock->pmask & sock->mask;
206 if (debug_level && emask)
207 fprintf(stderr, "socket %d pending events: %x\n", sock->obj.fd, emask);
208 if (emask && sock->event) {
209 if (debug_level) fprintf(stderr, "signalling event ptr %p\n", sock->event);
210 set_event(sock->event);
213 /* if anyone is stupid enough to wait on the socket object itself,
214 * maybe we should wake them up too, just in case? */
215 wake_up( &sock->obj, 0 );
218 static void sock_dump( struct object *obj, int verbose )
220 struct sock *sock = (struct sock *)obj;
221 assert( obj->ops == &sock_ops );
222 printf( "Socket fd=%d, state=%x, mask=%x, pending=%x, held=%x\n",
223 sock->obj.fd, sock->state,
224 sock->mask, sock->pmask, sock->hmask );
227 static int sock_signaled( struct object *obj, struct thread *thread )
229 struct sock *sock = (struct sock *)obj;
230 assert( obj->ops == &sock_ops );
232 return check_select_events( sock->obj.fd, sock_get_poll_events( &sock->obj ) );
235 static int sock_get_poll_events( struct object *obj )
237 struct sock *sock = (struct sock *)obj;
238 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
241 assert( obj->ops == &sock_ops );
243 if (sock->state & WS_FD_CONNECT)
244 /* connecting, wait for writable */
246 if (sock->state & WS_FD_LISTENING)
247 /* listening, wait for readable */
248 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
250 if (mask & FD_READ) ev |= POLLIN | POLLPRI;
251 if (mask & FD_WRITE) ev |= POLLOUT;
255 static int sock_get_fd( struct object *obj )
257 struct sock *sock = (struct sock *)obj;
258 assert( obj->ops == &sock_ops );
262 static void sock_destroy( struct object *obj )
264 struct sock *sock = (struct sock *)obj;
265 assert( obj->ops == &sock_ops );
267 /* FIXME: special socket shutdown stuff? */
270 /* if the service thread was waiting for the event object,
271 * we should now signal it, to let the service thread
272 * object detect that it is now orphaned... */
273 if (sock->mask & WS_FD_SERVEVENT)
274 set_event( sock->event );
275 /* we're through with it */
276 release_object( sock->event );
280 /* create a new and unconnected socket */
281 static struct object *create_socket( int family, int type, int protocol )
286 sockfd = socket( family, type, protocol );
288 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
293 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
294 if (!(sock = alloc_object( &sock_ops, -1 ))) return NULL;
295 sock->obj.fd = sockfd;
296 sock->state = (type != SOCK_STREAM) ? (WS_FD_READ|WS_FD_WRITE) : 0;
301 sock_reselect( sock );
306 /* accept a socket (creates a new fd) */
307 static struct object *accept_socket( handle_t handle )
309 struct sock *acceptsock;
312 struct sockaddr saddr;
315 sock=(struct sock*)get_handle_obj(current->process,handle,
316 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
319 /* Try to accept(2). We can't be safe that this an already connected socket
320 * or that accept() is allowed on it. In those cases we will get -1/errno
323 slen = sizeof(saddr);
324 acceptfd = accept(sock->obj.fd,&saddr,&slen);
327 release_object( sock );
330 if (!(acceptsock = alloc_object( &sock_ops, -1 )))
332 release_object( sock );
336 /* newly created socket gets the same properties of the listening socket */
337 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
338 acceptsock->obj.fd = acceptfd;
339 acceptsock->state = WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE;
340 if (sock->state & WS_FD_NONBLOCKING)
341 acceptsock->state |= WS_FD_NONBLOCKING;
342 acceptsock->mask = sock->mask;
343 acceptsock->hmask = 0;
344 acceptsock->pmask = 0;
345 acceptsock->event = NULL;
346 if (sock->event && !(sock->mask & WS_FD_SERVEVENT))
347 acceptsock->event = (struct event *)grab_object( sock->event );
349 sock_reselect( acceptsock );
351 sock->pmask &= ~FD_ACCEPT;
352 sock->hmask &= ~FD_ACCEPT;
353 sock_reselect( sock );
354 release_object( sock );
355 return &acceptsock->obj;
358 /* set the last error depending on errno */
359 static int sock_get_error( int err )
363 case EINTR: return WSAEINTR; break;
364 case EBADF: return WSAEBADF; break;
366 case EACCES: return WSAEACCES; break;
367 case EFAULT: return WSAEFAULT; break;
368 case EINVAL: return WSAEINVAL; break;
369 case EMFILE: return WSAEMFILE; break;
370 case EWOULDBLOCK: return WSAEWOULDBLOCK; break;
371 case EINPROGRESS: return WSAEINPROGRESS; break;
372 case EALREADY: return WSAEALREADY; break;
373 case ENOTSOCK: return WSAENOTSOCK; break;
374 case EDESTADDRREQ: return WSAEDESTADDRREQ; break;
375 case EMSGSIZE: return WSAEMSGSIZE; break;
376 case EPROTOTYPE: return WSAEPROTOTYPE; break;
377 case ENOPROTOOPT: return WSAENOPROTOOPT; break;
378 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT; break;
379 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT; break;
380 case EOPNOTSUPP: return WSAEOPNOTSUPP; break;
381 case EPFNOSUPPORT: return WSAEPFNOSUPPORT; break;
382 case EAFNOSUPPORT: return WSAEAFNOSUPPORT; break;
383 case EADDRINUSE: return WSAEADDRINUSE; break;
384 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL; break;
385 case ENETDOWN: return WSAENETDOWN; break;
386 case ENETUNREACH: return WSAENETUNREACH; break;
387 case ENETRESET: return WSAENETRESET; break;
388 case ECONNABORTED: return WSAECONNABORTED; break;
390 case ECONNRESET: return WSAECONNRESET; break;
391 case ENOBUFS: return WSAENOBUFS; break;
392 case EISCONN: return WSAEISCONN; break;
393 case ENOTCONN: return WSAENOTCONN; break;
394 case ESHUTDOWN: return WSAESHUTDOWN; break;
395 case ETOOMANYREFS: return WSAETOOMANYREFS; break;
396 case ETIMEDOUT: return WSAETIMEDOUT; break;
397 case ECONNREFUSED: return WSAECONNREFUSED; break;
398 case ELOOP: return WSAELOOP; break;
399 case ENAMETOOLONG: return WSAENAMETOOLONG; break;
400 case EHOSTDOWN: return WSAEHOSTDOWN; break;
401 case EHOSTUNREACH: return WSAEHOSTUNREACH; break;
402 case ENOTEMPTY: return WSAENOTEMPTY; break;
404 case EPROCLIM: return WSAEPROCLIM; break;
407 case EUSERS: return WSAEUSERS; break;
410 case EDQUOT: return WSAEDQUOT; break;
413 case ESTALE: return WSAESTALE; break;
416 case EREMOTE: return WSAEREMOTE; break;
418 default: errno=err; perror("sock_set_error"); return ERROR_UNKNOWN; break;
422 /* set the last error depending on errno */
423 static void sock_set_error(void)
425 set_error( sock_get_error( errno ) );
428 /* create a socket */
429 DECL_HANDLER(create_socket)
434 if ((obj = create_socket( req->family, req->type, req->protocol )) != NULL)
436 req->handle = alloc_handle( current->process, obj, req->access, req->inherit );
437 release_object( obj );
441 /* accept a socket */
442 DECL_HANDLER(accept_socket)
447 if ((obj = accept_socket( req->lhandle )) != NULL)
449 req->handle = alloc_handle( current->process, obj, req->access, req->inherit );
450 release_object( obj );
454 /* set socket event parameters */
455 DECL_HANDLER(set_socket_event)
458 struct event *oevent;
461 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
464 oevent = sock->event;
466 sock->mask = req->mask;
467 sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
468 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
469 sock_reselect( sock );
471 sock->state |= WS_FD_NONBLOCKING;
473 /* if a network event is pending, signal the event object
474 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
475 before a WSAEventSelect() was done on it.
476 (when dealing with Asynchronous socket) */
477 if (sock->pmask & sock->mask)
478 set_event(sock->event);
482 if ((oevent != sock->event) && (omask & WS_FD_SERVEVENT))
483 /* if the service thread was waiting for the old event object,
484 * we should now signal it, to let the service thread
485 * object detect that it is now orphaned... */
487 /* we're through with it */
488 release_object( oevent );
490 release_object( &sock->obj );
493 /* get socket event parameters */
494 DECL_HANDLER(get_socket_event)
499 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
505 set_error(WSAENOTSOCK);
508 req->mask = sock->mask;
509 req->pmask = sock->pmask;
510 req->state = sock->state;
511 size = min( get_req_data_size(req), sizeof(sock->errors) );
512 memcpy( get_req_data(req), sock->errors, size );
513 set_req_data_size( req, size );
519 struct event *sevent = get_event_obj(current->process, req->s_event, 0);
520 if (sevent == sock->event)
522 release_object( sevent );
528 struct event *cevent = get_event_obj(current->process, req->c_event, EVENT_MODIFY_STATE);
529 reset_event( cevent );
530 release_object( cevent );
533 sock_reselect( sock );
535 else set_error(WSAEINVAL);
537 release_object( &sock->obj );
540 /* re-enable pending socket events */
541 DECL_HANDLER(enable_socket_event)
545 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
548 sock->pmask &= ~req->mask; /* is this safe? */
549 sock->hmask &= ~req->mask;
550 sock->state |= req->sstate;
551 sock->state &= ~req->cstate;
552 sock_reselect( sock );
554 /* service trigger */
555 if (req->mask & WS_FD_SERVEVENT)
557 sock->pmask |= WS_FD_SERVEVENT;
559 if (debug_level) fprintf(stderr, "signalling service event ptr %p\n", sock->event);
560 set_event(sock->event);
564 release_object( &sock->obj );