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_read_fd */
73 sock_get_fd
, /* get_write_fd */
75 no_get_file_info
, /* get_file_info */
76 sock_destroy
/* destroy */
79 static void sock_reselect( struct sock
*sock
)
81 int ev
= sock_get_poll_events( &sock
->obj
);
85 fprintf(stderr
,"sock_reselect(%d): new mask %x\n", sock
->obj
.fd
, ev
);
86 set_select_events( &sock
->obj
, ev
);
88 /* check whether condition is satisfied already */
89 pfd
.fd
= sock
->obj
.fd
;
94 sock_poll_event( &sock
->obj
, pfd
.revents
);
97 inline static int sock_error(int s
)
99 unsigned int optval
= 0, optlen
;
101 optlen
= sizeof(optval
);
102 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
103 return optval
? sock_get_error(optval
) : 0;
106 static void sock_poll_event( struct object
*obj
, int event
)
108 struct sock
*sock
= (struct sock
*)obj
;
110 assert( sock
->obj
.ops
== &sock_ops
);
112 fprintf(stderr
, "socket %d select event: %x\n", sock
->obj
.fd
, event
);
113 if (sock
->state
& WS_FD_CONNECT
)
118 /* we got connected */
119 sock
->state
|= WS_FD_CONNECTED
|WS_FD_READ
|WS_FD_WRITE
;
120 sock
->state
&= ~WS_FD_CONNECT
;
121 sock
->pmask
|= FD_CONNECT
;
122 sock
->errors
[FD_CONNECT_BIT
] = 0;
124 fprintf(stderr
, "socket %d connection success\n", sock
->obj
.fd
);
126 else if (event
& (POLLERR
|POLLHUP
))
128 /* we didn't get connected? */
129 sock
->state
&= ~WS_FD_CONNECT
;
130 sock
->pmask
|= FD_CONNECT
;
131 sock
->errors
[FD_CONNECT_BIT
] = sock_error( sock
->obj
.fd
);
133 fprintf(stderr
, "socket %d connection failure\n", sock
->obj
.fd
);
136 if (sock
->state
& WS_FD_LISTENING
)
141 /* incoming connection */
142 sock
->pmask
|= FD_ACCEPT
;
143 sock
->errors
[FD_ACCEPT_BIT
] = 0;
144 sock
->hmask
|= FD_ACCEPT
;
146 else if (event
& (POLLERR
|POLLHUP
))
148 /* failed incoming connection? */
149 sock
->pmask
|= FD_ACCEPT
;
150 sock
->errors
[FD_ACCEPT_BIT
] = sock_error( sock
->obj
.fd
);
151 sock
->hmask
|= FD_ACCEPT
;
155 /* normal data flow */
159 sock
->pmask
|= FD_READ
;
160 sock
->hmask
|= FD_READ
;
161 sock
->errors
[FD_READ_BIT
] = 0;
163 fprintf(stderr
, "socket %d is readable\n", sock
->obj
.fd
);
167 sock
->pmask
|= FD_WRITE
;
168 sock
->hmask
|= FD_WRITE
;
169 sock
->errors
[FD_WRITE_BIT
] = 0;
171 fprintf(stderr
, "socket %d is writable\n", sock
->obj
.fd
);
175 sock
->pmask
|= FD_OOB
;
176 sock
->hmask
|= FD_OOB
;
177 sock
->errors
[FD_OOB_BIT
] = 0;
179 fprintf(stderr
, "socket %d got OOB data\n", sock
->obj
.fd
);
181 if (((event
& POLLERR
) || ((event
& (POLLIN
|POLLHUP
)) == POLLHUP
))
182 && (sock
->state
& (WS_FD_READ
|WS_FD_WRITE
))) {
184 sock
->errors
[FD_CLOSE_BIT
] = sock_error( sock
->obj
.fd
);
185 sock
->state
&= ~(WS_FD_CONNECTED
|WS_FD_READ
|WS_FD_WRITE
);
186 sock
->pmask
|= FD_CLOSE
;
188 fprintf(stderr
, "socket %d aborted by error %d\n",
189 sock
->obj
.fd
, sock
->errors
[FD_CLOSE_BIT
]);
193 if (event
& (POLLERR
|POLLHUP
))
194 set_select_events( &sock
->obj
, -1 );
196 sock_reselect( sock
);
197 /* wake up anyone waiting for whatever just happened */
198 emask
= sock
->pmask
& sock
->mask
;
199 if (debug_level
&& emask
)
200 fprintf(stderr
, "socket %d pending events: %x\n", sock
->obj
.fd
, emask
);
201 if (emask
&& sock
->event
) {
202 if (debug_level
) fprintf(stderr
, "signalling event ptr %p\n", sock
->event
);
203 set_event(sock
->event
);
206 /* if anyone is stupid enough to wait on the socket object itself,
207 * maybe we should wake them up too, just in case? */
208 wake_up( &sock
->obj
, 0 );
211 static void sock_dump( struct object
*obj
, int verbose
)
213 struct sock
*sock
= (struct sock
*)obj
;
214 assert( obj
->ops
== &sock_ops
);
215 printf( "Socket fd=%d, state=%x, mask=%x, pending=%x, held=%x\n",
216 sock
->obj
.fd
, sock
->state
,
217 sock
->mask
, sock
->pmask
, sock
->hmask
);
220 static int sock_signaled( struct object
*obj
, struct thread
*thread
)
222 struct sock
*sock
= (struct sock
*)obj
;
223 assert( obj
->ops
== &sock_ops
);
225 return check_select_events( sock
->obj
.fd
, sock_get_poll_events( &sock
->obj
) );
228 static int sock_get_poll_events( struct object
*obj
)
230 struct sock
*sock
= (struct sock
*)obj
;
231 unsigned int mask
= sock
->mask
& sock
->state
& ~sock
->hmask
;
234 assert( obj
->ops
== &sock_ops
);
236 if (sock
->state
& WS_FD_CONNECT
)
237 /* connecting, wait for writable */
239 if (sock
->state
& WS_FD_LISTENING
)
240 /* listening, wait for readable */
241 return (sock
->hmask
& FD_ACCEPT
) ? 0 : POLLIN
;
243 if (mask
& FD_READ
) ev
|= POLLIN
| POLLPRI
;
244 if (mask
& FD_WRITE
) ev
|= POLLOUT
;
248 static int sock_get_fd( struct object
*obj
)
250 struct sock
*sock
= (struct sock
*)obj
;
252 assert( obj
->ops
== &sock_ops
);
253 fd
= dup( sock
->obj
.fd
);
259 static void sock_destroy( struct object
*obj
)
261 struct sock
*sock
= (struct sock
*)obj
;
262 assert( obj
->ops
== &sock_ops
);
264 /* FIXME: special socket shutdown stuff? */
267 /* if the service thread was waiting for the event object,
268 * we should now signal it, to let the service thread
269 * object detect that it is now orphaned... */
270 if (sock
->mask
& WS_FD_SERVEVENT
)
271 set_event( sock
->event
);
272 /* we're through with it */
273 release_object( sock
->event
);
277 /* create a new and unconnected socket */
278 static struct object
*create_socket( int family
, int type
, int protocol
)
283 sockfd
= socket( family
, type
, protocol
);
285 fprintf(stderr
,"socket(%d,%d,%d)=%d\n",family
,type
,protocol
,sockfd
);
290 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
291 if (!(sock
= alloc_object( &sock_ops
, sockfd
))) return NULL
;
292 sock
->state
= (type
!=SOCK_STREAM
) ? WS_FD_READ
|WS_FD_WRITE
: 0;
297 sock_reselect( sock
);
302 /* accept a socket (creates a new fd) */
303 static struct object
*accept_socket( int handle
)
305 struct sock
*acceptsock
;
308 struct sockaddr saddr
;
311 sock
=(struct sock
*)get_handle_obj(current
->process
,handle
,
312 GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,&sock_ops
);
315 /* Try to accept(2). We can't be safe that this an already connected socket
316 * or that accept() is allowed on it. In those cases we will get -1/errno
319 slen
= sizeof(saddr
);
320 acceptfd
= accept(sock
->obj
.fd
,&saddr
,&slen
);
323 release_object( sock
);
326 if (!(acceptsock
= alloc_object( &sock_ops
, acceptfd
)))
328 release_object( sock
);
332 acceptsock
->state
= WS_FD_CONNECTED
|WS_FD_READ
|WS_FD_WRITE
;
333 acceptsock
->mask
= sock
->mask
;
334 acceptsock
->hmask
= 0;
335 acceptsock
->pmask
= 0;
336 acceptsock
->event
= NULL
;
337 if (sock
->event
&& !(sock
->mask
& WS_FD_SERVEVENT
))
338 acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
340 sock_reselect( acceptsock
);
342 sock
->pmask
&= ~FD_ACCEPT
;
343 sock
->hmask
&= ~FD_ACCEPT
;
344 release_object( sock
);
345 return &acceptsock
->obj
;
348 /* set the last error depending on errno */
349 static int sock_get_error( int err
)
353 case EINTR
: return WSAEINTR
; break;
354 case EBADF
: return WSAEBADF
; break;
356 case EACCES
: return WSAEACCES
; break;
357 case EFAULT
: return WSAEFAULT
; break;
358 case EINVAL
: return WSAEINVAL
; break;
359 case EMFILE
: return WSAEMFILE
; break;
360 case EWOULDBLOCK
: return WSAEWOULDBLOCK
; break;
361 case EINPROGRESS
: return WSAEINPROGRESS
; break;
362 case EALREADY
: return WSAEALREADY
; break;
363 case ENOTSOCK
: return WSAENOTSOCK
; break;
364 case EDESTADDRREQ
: return WSAEDESTADDRREQ
; break;
365 case EMSGSIZE
: return WSAEMSGSIZE
; break;
366 case EPROTOTYPE
: return WSAEPROTOTYPE
; break;
367 case ENOPROTOOPT
: return WSAENOPROTOOPT
; break;
368 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
; break;
369 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
; break;
370 case EOPNOTSUPP
: return WSAEOPNOTSUPP
; break;
371 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
; break;
372 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
; break;
373 case EADDRINUSE
: return WSAEADDRINUSE
; break;
374 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
; break;
375 case ENETDOWN
: return WSAENETDOWN
; break;
376 case ENETUNREACH
: return WSAENETUNREACH
; break;
377 case ENETRESET
: return WSAENETRESET
; break;
378 case ECONNABORTED
: return WSAECONNABORTED
; break;
380 case ECONNRESET
: return WSAECONNRESET
; break;
381 case ENOBUFS
: return WSAENOBUFS
; break;
382 case EISCONN
: return WSAEISCONN
; break;
383 case ENOTCONN
: return WSAENOTCONN
; break;
384 case ESHUTDOWN
: return WSAESHUTDOWN
; break;
385 case ETOOMANYREFS
: return WSAETOOMANYREFS
; break;
386 case ETIMEDOUT
: return WSAETIMEDOUT
; break;
387 case ECONNREFUSED
: return WSAECONNREFUSED
; break;
388 case ELOOP
: return WSAELOOP
; break;
389 case ENAMETOOLONG
: return WSAENAMETOOLONG
; break;
390 case EHOSTDOWN
: return WSAEHOSTDOWN
; break;
391 case EHOSTUNREACH
: return WSAEHOSTUNREACH
; break;
392 case ENOTEMPTY
: return WSAENOTEMPTY
; break;
394 case EPROCLIM
: return WSAEPROCLIM
; break;
397 case EUSERS
: return WSAEUSERS
; break;
400 case EDQUOT
: return WSAEDQUOT
; break;
403 case ESTALE
: return WSAESTALE
; break;
406 case EREMOTE
: return WSAEREMOTE
; break;
408 default: errno
=err
; perror("sock_set_error"); return ERROR_UNKNOWN
; break;
412 /* set the last error depending on errno */
413 static void sock_set_error(void)
415 set_error( sock_get_error( errno
) );
418 /* create a socket */
419 DECL_HANDLER(create_socket
)
424 if ((obj
= create_socket( req
->family
, req
->type
, req
->protocol
)) != NULL
)
426 s
= alloc_handle( current
->process
, obj
, req
->access
, req
->inherit
);
427 release_object( obj
);
432 /* accept a socket */
433 DECL_HANDLER(accept_socket
)
438 if ((obj
= accept_socket( req
->lhandle
)) != NULL
)
440 s
= alloc_handle( current
->process
, obj
, req
->access
, req
->inherit
);
441 release_object( obj
);
446 /* set socket event parameters */
447 DECL_HANDLER(set_socket_event
)
450 struct event
*oevent
;
453 sock
=(struct sock
*)get_handle_obj(current
->process
,req
->handle
,GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,&sock_ops
);
456 oevent
= sock
->event
;
458 sock
->mask
= req
->mask
;
459 sock
->event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
460 if (debug_level
&& sock
->event
) fprintf(stderr
, "event ptr: %p\n", sock
->event
);
461 sock_reselect( sock
);
463 sock
->state
|= WS_FD_NONBLOCKING
;
466 if ((oevent
!= sock
->event
) && (omask
& WS_FD_SERVEVENT
))
467 /* if the service thread was waiting for the old event object,
468 * we should now signal it, to let the service thread
469 * object detect that it is now orphaned... */
471 /* we're through with it */
472 release_object( oevent
);
474 release_object( &sock
->obj
);
477 /* get socket event parameters */
478 DECL_HANDLER(get_socket_event
)
482 sock
=(struct sock
*)get_handle_obj(current
->process
,req
->handle
,GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,&sock_ops
);
488 set_error(WSAENOTSOCK
);
491 req
->mask
= sock
->mask
;
492 req
->pmask
= sock
->pmask
;
493 req
->state
= sock
->state
;
494 memcpy(req
->errors
, sock
->errors
, sizeof(sock
->errors
));
500 struct event
*sevent
= get_event_obj(current
->process
, req
->s_event
, 0);
501 if (sevent
== sock
->event
)
503 release_object( sevent
);
509 struct event
*cevent
= get_event_obj(current
->process
, req
->c_event
, EVENT_MODIFY_STATE
);
510 reset_event( cevent
);
511 release_object( cevent
);
514 sock_reselect( sock
);
516 else set_error(WSAEINVAL
);
518 release_object( &sock
->obj
);
521 /* re-enable pending socket events */
522 DECL_HANDLER(enable_socket_event
)
526 sock
=(struct sock
*)get_handle_obj(current
->process
,req
->handle
,GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,&sock_ops
);
529 sock
->pmask
&= ~req
->mask
; /* is this safe? */
530 sock
->hmask
&= ~req
->mask
;
531 sock
->state
|= req
->sstate
;
532 sock
->state
&= ~req
->cstate
;
533 sock_reselect( sock
);
535 /* service trigger */
536 if (req
->mask
& WS_FD_SERVEVENT
)
538 sock
->pmask
|= WS_FD_SERVEVENT
;
540 if (debug_level
) fprintf(stderr
, "signalling service event ptr %p\n", sock
->event
);
541 set_event(sock
->event
);
545 release_object( &sock
->obj
);