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 void sock_set_error(void);
61 static const struct object_ops sock_ops
=
63 sizeof(struct sock
), /* size */
65 add_queue
, /* add_queue */
66 remove_queue
, /* remove_queue */
67 sock_signaled
, /* signaled */
68 no_satisfied
, /* satisfied */
69 sock_get_poll_events
, /* get_poll_events */
70 sock_poll_event
, /* poll_event */
71 sock_get_fd
, /* get_read_fd */
72 sock_get_fd
, /* get_write_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
);
82 fprintf(stderr
,"sock_reselect(%d): new mask %x\n", sock
->obj
.fd
, ev
);
83 set_select_events( &sock
->obj
, ev
);
86 inline static int sock_error(int s
)
88 unsigned int optval
, optlen
;
90 optlen
= sizeof(optval
);
91 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
95 static void sock_poll_event( struct object
*obj
, int event
)
97 struct sock
*sock
= (struct sock
*)obj
;
99 assert( sock
->obj
.ops
== &sock_ops
);
101 fprintf(stderr
, "socket %d select event: %x\n", sock
->obj
.fd
, event
);
102 if (sock
->state
& WS_FD_CONNECT
)
107 /* we got connected */
108 sock
->state
|= WS_FD_CONNECTED
|WS_FD_READ
|WS_FD_WRITE
;
109 sock
->state
&= ~WS_FD_CONNECT
;
110 sock
->pmask
|= FD_CONNECT
;
111 sock
->errors
[FD_CONNECT_BIT
] = 0;
113 fprintf(stderr
, "socket %d connection success\n", sock
->obj
.fd
);
115 else if (event
& (POLLERR
|POLLHUP
))
117 /* we didn't get connected? */
118 sock
->state
&= ~WS_FD_CONNECT
;
119 sock
->pmask
|= FD_CONNECT
;
120 sock
->errors
[FD_CONNECT_BIT
] = sock_error( sock
->obj
.fd
);
122 fprintf(stderr
, "socket %d connection failure\n", sock
->obj
.fd
);
125 if (sock
->state
& WS_FD_LISTENING
)
130 /* incoming connection */
131 sock
->pmask
|= FD_ACCEPT
;
132 sock
->errors
[FD_ACCEPT_BIT
] = 0;
133 sock
->hmask
|= FD_ACCEPT
;
135 else if (event
& (POLLERR
|POLLHUP
))
137 /* failed incoming connection? */
138 sock
->pmask
|= FD_ACCEPT
;
139 sock
->errors
[FD_ACCEPT_BIT
] = sock_error( sock
->obj
.fd
);
140 sock
->hmask
|= FD_ACCEPT
;
144 /* normal data flow */
148 sock
->pmask
|= FD_READ
;
149 sock
->hmask
|= FD_READ
;
150 sock
->errors
[FD_READ_BIT
] = 0;
152 fprintf(stderr
, "socket %d is readable\n", sock
->obj
.fd
);
156 sock
->pmask
|= FD_WRITE
;
157 sock
->hmask
|= FD_WRITE
;
158 sock
->errors
[FD_WRITE_BIT
] = 0;
160 fprintf(stderr
, "socket %d is writable\n", sock
->obj
.fd
);
164 sock
->pmask
|= FD_OOB
;
165 sock
->hmask
|= FD_OOB
;
167 fprintf(stderr
, "socket %d got OOB data\n", sock
->obj
.fd
);
169 if (event
& (POLLERR
|POLLHUP
))
171 sock
->errors
[FD_CLOSE_BIT
] = sock_error( sock
->obj
.fd
);
172 /* we got an error, socket closing? */
173 sock
->state
&= ~(WS_FD_CONNECTED
|WS_FD_READ
|WS_FD_WRITE
);
174 sock
->pmask
|= FD_CLOSE
;
176 fprintf(stderr
, "socket %d aborted by error %d\n",
177 sock
->obj
.fd
, sock
->errors
[FD_CLOSE_BIT
]);
181 sock_reselect( sock
);
182 /* wake up anyone waiting for whatever just happened */
183 emask
= sock
->pmask
& sock
->mask
;
184 if (debug_level
&& emask
)
185 fprintf(stderr
, "socket %d pending events: %x\n", sock
->obj
.fd
, emask
);
186 if (emask
&& sock
->event
) {
187 if (debug_level
) fprintf(stderr
, "signalling event ptr %p\n", sock
->event
);
188 set_event(sock
->event
);
191 /* if anyone is stupid enough to wait on the socket object itself,
192 * maybe we should wake them up too, just in case? */
193 wake_up( &sock
->obj
, 0 );
196 static void sock_dump( struct object
*obj
, int verbose
)
198 struct sock
*sock
= (struct sock
*)obj
;
199 assert( obj
->ops
== &sock_ops
);
200 printf( "Socket fd=%d, state=%x, mask=%x, pending=%x, held=%x\n",
201 sock
->obj
.fd
, sock
->state
,
202 sock
->mask
, sock
->pmask
, sock
->hmask
);
205 static int sock_signaled( struct object
*obj
, struct thread
*thread
)
207 struct sock
*sock
= (struct sock
*)obj
;
208 assert( obj
->ops
== &sock_ops
);
210 return check_select_events( sock
->obj
.fd
, sock_get_poll_events( &sock
->obj
) );
213 static int sock_get_poll_events( struct object
*obj
)
215 struct sock
*sock
= (struct sock
*)obj
;
216 unsigned int mask
= sock
->mask
& sock
->state
& ~sock
->hmask
;
219 assert( obj
->ops
== &sock_ops
);
221 if (sock
->state
& WS_FD_CONNECT
)
222 /* connecting, wait for writable */
224 if (sock
->state
& WS_FD_LISTENING
)
225 /* listening, wait for readable */
226 return (sock
->hmask
& FD_ACCEPT
) ? 0 : POLLIN
;
228 if (mask
& FD_READ
) ev
|= POLLIN
| POLLPRI
;
229 if (mask
& FD_WRITE
) ev
|= POLLOUT
;
233 static int sock_get_fd( struct object
*obj
)
235 struct sock
*sock
= (struct sock
*)obj
;
237 assert( obj
->ops
== &sock_ops
);
238 fd
= dup( sock
->obj
.fd
);
244 static void sock_destroy( struct object
*obj
)
246 struct sock
*sock
= (struct sock
*)obj
;
247 assert( obj
->ops
== &sock_ops
);
249 /* FIXME: special socket shutdown stuff? */
252 /* if the service thread was waiting for the event object,
253 * we should now signal it, to let the service thread
254 * object detect that it is now orphaned... */
255 if (sock
->mask
& WS_FD_SERVEVENT
)
256 set_event( sock
->event
);
257 /* we're through with it */
258 release_object( sock
->event
);
262 /* create a new and unconnected socket */
263 static struct object
*create_socket( int family
, int type
, int protocol
)
268 sockfd
= socket( family
, type
, protocol
);
270 fprintf(stderr
,"socket(%d,%d,%d)=%d\n",family
,type
,protocol
,sockfd
);
275 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
276 if (!(sock
= alloc_object( &sock_ops
, sockfd
))) return NULL
;
277 sock
->state
= (type
!=SOCK_STREAM
) ? WS_FD_READ
|WS_FD_WRITE
: 0;
282 sock_reselect( sock
);
287 /* accept a socket (creates a new fd) */
288 static struct object
*accept_socket( int handle
)
290 struct sock
*acceptsock
;
293 struct sockaddr saddr
;
296 sock
=(struct sock
*)get_handle_obj(current
->process
,handle
,
297 GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,&sock_ops
);
300 /* Try to accept(2). We can't be safe that this an already connected socket
301 * or that accept() is allowed on it. In those cases we will get -1/errno
304 slen
= sizeof(saddr
);
305 acceptfd
= accept(sock
->obj
.fd
,&saddr
,&slen
);
308 release_object( sock
);
311 if (!(acceptsock
= alloc_object( &sock_ops
, acceptfd
)))
313 release_object( sock
);
317 acceptsock
->state
= WS_FD_CONNECTED
|WS_FD_READ
|WS_FD_WRITE
;
318 acceptsock
->mask
= sock
->mask
;
319 acceptsock
->hmask
= 0;
320 acceptsock
->pmask
= 0;
321 acceptsock
->event
= NULL
;
322 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
324 sock_reselect( acceptsock
);
326 sock
->pmask
&= ~FD_ACCEPT
;
327 sock
->hmask
&= ~FD_ACCEPT
;
328 release_object( sock
);
329 return &acceptsock
->obj
;
332 /* set the last error depending on errno */
333 static void sock_set_error(void)
337 case EINTR
: set_error(WSAEINTR
);break;
338 case EBADF
: set_error(WSAEBADF
);break;
340 case EACCES
: set_error(WSAEACCES
);break;
341 case EFAULT
: set_error(WSAEFAULT
);break;
342 case EINVAL
: set_error(WSAEINVAL
);break;
343 case EMFILE
: set_error(WSAEMFILE
);break;
344 case EWOULDBLOCK
: set_error(WSAEWOULDBLOCK
);break;
345 case EINPROGRESS
: set_error(WSAEINPROGRESS
);break;
346 case EALREADY
: set_error(WSAEALREADY
);break;
347 case ENOTSOCK
: set_error(WSAENOTSOCK
);break;
348 case EDESTADDRREQ
: set_error(WSAEDESTADDRREQ
);break;
349 case EMSGSIZE
: set_error(WSAEMSGSIZE
);break;
350 case EPROTOTYPE
: set_error(WSAEPROTOTYPE
);break;
351 case ENOPROTOOPT
: set_error(WSAENOPROTOOPT
);break;
352 case EPROTONOSUPPORT
: set_error(WSAEPROTONOSUPPORT
);break;
353 case ESOCKTNOSUPPORT
: set_error(WSAESOCKTNOSUPPORT
);break;
354 case EOPNOTSUPP
: set_error(WSAEOPNOTSUPP
);break;
355 case EPFNOSUPPORT
: set_error(WSAEPFNOSUPPORT
);break;
356 case EAFNOSUPPORT
: set_error(WSAEAFNOSUPPORT
);break;
357 case EADDRINUSE
: set_error(WSAEADDRINUSE
);break;
358 case EADDRNOTAVAIL
: set_error(WSAEADDRNOTAVAIL
);break;
359 case ENETDOWN
: set_error(WSAENETDOWN
);break;
360 case ENETUNREACH
: set_error(WSAENETUNREACH
);break;
361 case ENETRESET
: set_error(WSAENETRESET
);break;
362 case ECONNABORTED
: set_error(WSAECONNABORTED
);break;
364 case ECONNRESET
: set_error(WSAECONNRESET
);break;
365 case ENOBUFS
: set_error(WSAENOBUFS
);break;
366 case EISCONN
: set_error(WSAEISCONN
);break;
367 case ENOTCONN
: set_error(WSAENOTCONN
);break;
368 case ESHUTDOWN
: set_error(WSAESHUTDOWN
);break;
369 case ETOOMANYREFS
: set_error(WSAETOOMANYREFS
);break;
370 case ETIMEDOUT
: set_error(WSAETIMEDOUT
);break;
371 case ECONNREFUSED
: set_error(WSAECONNREFUSED
);break;
372 case ELOOP
: set_error(WSAELOOP
);break;
373 case ENAMETOOLONG
: set_error(WSAENAMETOOLONG
);break;
374 case EHOSTDOWN
: set_error(WSAEHOSTDOWN
);break;
375 case EHOSTUNREACH
: set_error(WSAEHOSTUNREACH
);break;
376 case ENOTEMPTY
: set_error(WSAENOTEMPTY
);break;
378 case EPROCLIM
: set_error(WSAEPROCLIM
);break;
381 case EUSERS
: set_error(WSAEUSERS
);break;
384 case EDQUOT
: set_error(WSAEDQUOT
);break;
387 case ESTALE
: set_error(WSAESTALE
);break;
390 case EREMOTE
: set_error(WSAEREMOTE
);break;
392 default: perror("sock_set_error"); set_error( ERROR_UNKNOWN
); break;
396 /* create a socket */
397 DECL_HANDLER(create_socket
)
402 if ((obj
= create_socket( req
->family
, req
->type
, req
->protocol
)) != NULL
)
404 s
= alloc_handle( current
->process
, obj
, req
->access
, req
->inherit
);
405 release_object( obj
);
410 /* accept a socket */
411 DECL_HANDLER(accept_socket
)
416 if ((obj
= accept_socket( req
->lhandle
)) != NULL
)
418 s
= alloc_handle( current
->process
, obj
, req
->access
, req
->inherit
);
419 release_object( obj
);
424 /* set socket event parameters */
425 DECL_HANDLER(set_socket_event
)
428 struct event
*oevent
;
431 sock
=(struct sock
*)get_handle_obj(current
->process
,req
->handle
,GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,&sock_ops
);
434 oevent
= sock
->event
;
436 sock
->mask
= req
->mask
;
437 sock
->event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
438 if (debug_level
&& sock
->event
) fprintf(stderr
, "event ptr: %p\n", sock
->event
);
439 sock_reselect( sock
);
441 sock
->state
|= WS_FD_NONBLOCKING
;
444 if ((oevent
!= sock
->event
) && (omask
& WS_FD_SERVEVENT
))
445 /* if the service thread was waiting for the old event object,
446 * we should now signal it, to let the service thread
447 * object detect that it is now orphaned... */
449 /* we're through with it */
450 release_object( oevent
);
452 release_object( &sock
->obj
);
455 /* get socket event parameters */
456 DECL_HANDLER(get_socket_event
)
460 sock
=(struct sock
*)get_handle_obj(current
->process
,req
->handle
,GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,&sock_ops
);
466 set_error(WSAENOTSOCK
);
469 req
->mask
= sock
->mask
;
470 req
->pmask
= sock
->pmask
;
471 req
->state
= sock
->state
;
472 memcpy(req
->errors
, sock
->errors
, sizeof(sock
->errors
));
478 struct event
*sevent
= get_event_obj(current
->process
, req
->s_event
, 0);
479 if (sevent
== sock
->event
)
481 release_object( sevent
);
486 sock_reselect( sock
);
488 else set_error(WSAEINVAL
);
490 release_object( &sock
->obj
);
493 /* re-enable pending socket events */
494 DECL_HANDLER(enable_socket_event
)
498 sock
=(struct sock
*)get_handle_obj(current
->process
,req
->handle
,GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,&sock_ops
);
501 sock
->pmask
&= ~req
->mask
; /* is this safe? */
502 sock
->hmask
&= ~req
->mask
;
503 sock
->state
|= req
->sstate
;
504 sock
->state
&= ~req
->cstate
;
505 sock_reselect( sock
);
506 release_object( &sock
->obj
);