Added a few forwards.
[wine/multimedia.git] / server / sock.c
blob72cea8964c0c41af55110801bc8fe26f3cb9d98b
1 /*
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?
8 */
10 #include "config.h"
12 #include <assert.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <errno.h>
18 #ifdef HAVE_SYS_ERRNO_H
19 # include <sys/errno.h>
20 #endif
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 #include <sys/types.h>
24 #ifdef HAVE_SYS_SOCKET_H
25 # include <sys/socket.h>
26 #endif
27 #include <sys/ioctl.h>
28 #ifdef HAVE_SYS_FILIO_H
29 # include <sys/filio.h>
30 #endif
31 #include <time.h>
32 #include <unistd.h>
34 #include "winerror.h"
35 #include "winbase.h"
36 #include "winsock2.h"
37 #include "process.h"
38 #include "handle.h"
39 #include "thread.h"
40 #include "request.h"
42 struct sock
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 */
65 sock_dump, /* dump */
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 */
74 no_flush, /* flush */
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 );
82 struct pollfd pfd;
84 if (debug_level)
85 fprintf(stderr,"sock_reselect(%d): new mask %x\n", sock->obj.fd, ev);
87 if (sock->obj.select == -1) {
88 /* previously unconnected socket, is this reselect supposed to connect it? */
89 if (!sock->state) return;
90 /* ok, it is, attach it to the wineserver's main poll loop */
91 add_select_user( &sock->obj );
93 /* update condition mask */
94 set_select_events( &sock->obj, ev );
96 /* check whether condition is satisfied already */
97 pfd.fd = sock->obj.fd;
98 pfd.events = ev;
99 pfd.revents = 0;
100 poll( &pfd, 1, 0 );
101 if (pfd.revents)
102 sock_poll_event( &sock->obj, pfd.revents);
105 inline static int sock_error(int s)
107 unsigned int optval = 0, optlen;
109 optlen = sizeof(optval);
110 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
111 return optval ? sock_get_error(optval) : 0;
114 static void sock_poll_event( struct object *obj, int event )
116 struct sock *sock = (struct sock *)obj;
117 unsigned int emask;
118 assert( sock->obj.ops == &sock_ops );
119 if (debug_level)
120 fprintf(stderr, "socket %d select event: %x\n", sock->obj.fd, event);
121 if (sock->state & WS_FD_CONNECT)
123 /* connecting */
124 if (event & POLLOUT)
126 /* we got connected */
127 sock->state |= WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE;
128 sock->state &= ~WS_FD_CONNECT;
129 sock->pmask |= FD_CONNECT;
130 sock->errors[FD_CONNECT_BIT] = 0;
131 if (debug_level)
132 fprintf(stderr, "socket %d connection success\n", sock->obj.fd);
134 else if (event & (POLLERR|POLLHUP))
136 /* we didn't get connected? */
137 sock->state &= ~WS_FD_CONNECT;
138 sock->pmask |= FD_CONNECT;
139 sock->errors[FD_CONNECT_BIT] = sock_error( sock->obj.fd );
140 if (debug_level)
141 fprintf(stderr, "socket %d connection failure\n", sock->obj.fd);
143 } else
144 if (sock->state & WS_FD_LISTENING)
146 /* listening */
147 if (event & POLLIN)
149 /* incoming connection */
150 sock->pmask |= FD_ACCEPT;
151 sock->errors[FD_ACCEPT_BIT] = 0;
152 sock->hmask |= FD_ACCEPT;
154 else if (event & (POLLERR|POLLHUP))
156 /* failed incoming connection? */
157 sock->pmask |= FD_ACCEPT;
158 sock->errors[FD_ACCEPT_BIT] = sock_error( sock->obj.fd );
159 sock->hmask |= FD_ACCEPT;
161 } else
163 /* normal data flow */
164 if (event & POLLIN)
166 /* incoming data */
167 sock->pmask |= FD_READ;
168 sock->hmask |= FD_READ;
169 sock->errors[FD_READ_BIT] = 0;
170 if (debug_level)
171 fprintf(stderr, "socket %d is readable\n", sock->obj.fd );
173 if (event & POLLOUT)
175 sock->pmask |= FD_WRITE;
176 sock->hmask |= FD_WRITE;
177 sock->errors[FD_WRITE_BIT] = 0;
178 if (debug_level)
179 fprintf(stderr, "socket %d is writable\n", sock->obj.fd);
181 if (event & POLLPRI)
183 sock->pmask |= FD_OOB;
184 sock->hmask |= FD_OOB;
185 sock->errors[FD_OOB_BIT] = 0;
186 if (debug_level)
187 fprintf(stderr, "socket %d got OOB data\n", sock->obj.fd);
189 if (((event & POLLERR) || ((event & (POLLIN|POLLHUP)) == POLLHUP))
190 && (sock->state & (WS_FD_READ|WS_FD_WRITE))) {
191 /* socket closing */
192 sock->errors[FD_CLOSE_BIT] = sock_error( sock->obj.fd );
193 sock->state &= ~(WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE);
194 sock->pmask |= FD_CLOSE;
195 if (debug_level)
196 fprintf(stderr, "socket %d aborted by error %d\n",
197 sock->obj.fd, sock->errors[FD_CLOSE_BIT]);
201 if (event & (POLLERR|POLLHUP))
202 set_select_events( &sock->obj, -1 );
203 else
204 sock_reselect( sock );
205 /* wake up anyone waiting for whatever just happened */
206 emask = sock->pmask & sock->mask;
207 if (debug_level && emask)
208 fprintf(stderr, "socket %d pending events: %x\n", sock->obj.fd, emask);
209 if (emask && sock->event) {
210 if (debug_level) fprintf(stderr, "signalling event ptr %p\n", sock->event);
211 set_event(sock->event);
214 /* if anyone is stupid enough to wait on the socket object itself,
215 * maybe we should wake them up too, just in case? */
216 wake_up( &sock->obj, 0 );
219 static void sock_dump( struct object *obj, int verbose )
221 struct sock *sock = (struct sock *)obj;
222 assert( obj->ops == &sock_ops );
223 printf( "Socket fd=%d, state=%x, mask=%x, pending=%x, held=%x\n",
224 sock->obj.fd, sock->state,
225 sock->mask, sock->pmask, sock->hmask );
228 static int sock_signaled( struct object *obj, struct thread *thread )
230 struct sock *sock = (struct sock *)obj;
231 assert( obj->ops == &sock_ops );
233 return check_select_events( sock->obj.fd, sock_get_poll_events( &sock->obj ) );
236 static int sock_get_poll_events( struct object *obj )
238 struct sock *sock = (struct sock *)obj;
239 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
240 int ev = 0;
242 assert( obj->ops == &sock_ops );
244 if (sock->state & WS_FD_CONNECT)
245 /* connecting, wait for writable */
246 return POLLOUT;
247 if (sock->state & WS_FD_LISTENING)
248 /* listening, wait for readable */
249 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
251 if (mask & FD_READ) ev |= POLLIN | POLLPRI;
252 if (mask & FD_WRITE) ev |= POLLOUT;
253 return ev;
256 static int sock_get_fd( struct object *obj )
258 struct sock *sock = (struct sock *)obj;
259 int fd;
260 assert( obj->ops == &sock_ops );
261 fd = dup( sock->obj.fd );
262 if (fd==-1)
263 sock_set_error();
264 return fd;
267 static void sock_destroy( struct object *obj )
269 struct sock *sock = (struct sock *)obj;
270 assert( obj->ops == &sock_ops );
272 /* FIXME: special socket shutdown stuff? */
273 if (sock->event)
275 /* if the service thread was waiting for the event object,
276 * we should now signal it, to let the service thread
277 * object detect that it is now orphaned... */
278 if (sock->mask & WS_FD_SERVEVENT)
279 set_event( sock->event );
280 /* we're through with it */
281 release_object( sock->event );
285 /* create a new and unconnected socket */
286 static struct object *create_socket( int family, int type, int protocol )
288 struct sock *sock;
289 int sockfd;
291 sockfd = socket( family, type, protocol );
292 if (debug_level)
293 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
294 if (sockfd == -1) {
295 sock_set_error();
296 return NULL;
298 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
299 if (!(sock = alloc_object( &sock_ops, -1 ))) return NULL;
300 sock->obj.fd = sockfd;
301 sock->state = (type != SOCK_STREAM) ? (WS_FD_READ|WS_FD_WRITE) : 0;
302 sock->mask = 0;
303 sock->hmask = 0;
304 sock->pmask = 0;
305 sock->event = NULL;
306 sock_reselect( sock );
307 clear_error();
308 return &sock->obj;
311 /* accept a socket (creates a new fd) */
312 static struct object *accept_socket( int handle )
314 struct sock *acceptsock;
315 struct sock *sock;
316 int acceptfd;
317 struct sockaddr saddr;
318 int slen;
320 sock=(struct sock*)get_handle_obj(current->process,handle,
321 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
322 if (!sock)
323 return NULL;
324 /* Try to accept(2). We can't be safe that this an already connected socket
325 * or that accept() is allowed on it. In those cases we will get -1/errno
326 * return.
328 slen = sizeof(saddr);
329 acceptfd = accept(sock->obj.fd,&saddr,&slen);
330 if (acceptfd==-1) {
331 sock_set_error();
332 release_object( sock );
333 return NULL;
335 if (!(acceptsock = alloc_object( &sock_ops, -1 )))
337 release_object( sock );
338 return NULL;
341 /* newly created socket gets the same properties of the listening socket */
342 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
343 acceptsock->obj.fd = acceptfd;
344 acceptsock->state = WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE;
345 if (sock->state & WS_FD_NONBLOCKING)
346 acceptsock->state |= WS_FD_NONBLOCKING;
347 acceptsock->mask = sock->mask;
348 acceptsock->hmask = 0;
349 acceptsock->pmask = 0;
350 acceptsock->event = NULL;
351 if (sock->event && !(sock->mask & WS_FD_SERVEVENT))
352 acceptsock->event = (struct event *)grab_object( sock->event );
354 sock_reselect( acceptsock );
355 clear_error();
356 sock->pmask &= ~FD_ACCEPT;
357 sock->hmask &= ~FD_ACCEPT;
358 sock_reselect( sock );
359 release_object( sock );
360 return &acceptsock->obj;
363 /* set the last error depending on errno */
364 static int sock_get_error( int err )
366 switch (err)
368 case EINTR: return WSAEINTR; break;
369 case EBADF: return WSAEBADF; break;
370 case EPERM:
371 case EACCES: return WSAEACCES; break;
372 case EFAULT: return WSAEFAULT; break;
373 case EINVAL: return WSAEINVAL; break;
374 case EMFILE: return WSAEMFILE; break;
375 case EWOULDBLOCK: return WSAEWOULDBLOCK; break;
376 case EINPROGRESS: return WSAEINPROGRESS; break;
377 case EALREADY: return WSAEALREADY; break;
378 case ENOTSOCK: return WSAENOTSOCK; break;
379 case EDESTADDRREQ: return WSAEDESTADDRREQ; break;
380 case EMSGSIZE: return WSAEMSGSIZE; break;
381 case EPROTOTYPE: return WSAEPROTOTYPE; break;
382 case ENOPROTOOPT: return WSAENOPROTOOPT; break;
383 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT; break;
384 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT; break;
385 case EOPNOTSUPP: return WSAEOPNOTSUPP; break;
386 case EPFNOSUPPORT: return WSAEPFNOSUPPORT; break;
387 case EAFNOSUPPORT: return WSAEAFNOSUPPORT; break;
388 case EADDRINUSE: return WSAEADDRINUSE; break;
389 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL; break;
390 case ENETDOWN: return WSAENETDOWN; break;
391 case ENETUNREACH: return WSAENETUNREACH; break;
392 case ENETRESET: return WSAENETRESET; break;
393 case ECONNABORTED: return WSAECONNABORTED; break;
394 case EPIPE:
395 case ECONNRESET: return WSAECONNRESET; break;
396 case ENOBUFS: return WSAENOBUFS; break;
397 case EISCONN: return WSAEISCONN; break;
398 case ENOTCONN: return WSAENOTCONN; break;
399 case ESHUTDOWN: return WSAESHUTDOWN; break;
400 case ETOOMANYREFS: return WSAETOOMANYREFS; break;
401 case ETIMEDOUT: return WSAETIMEDOUT; break;
402 case ECONNREFUSED: return WSAECONNREFUSED; break;
403 case ELOOP: return WSAELOOP; break;
404 case ENAMETOOLONG: return WSAENAMETOOLONG; break;
405 case EHOSTDOWN: return WSAEHOSTDOWN; break;
406 case EHOSTUNREACH: return WSAEHOSTUNREACH; break;
407 case ENOTEMPTY: return WSAENOTEMPTY; break;
408 #ifdef EPROCLIM
409 case EPROCLIM: return WSAEPROCLIM; break;
410 #endif
411 #ifdef EUSERS
412 case EUSERS: return WSAEUSERS; break;
413 #endif
414 #ifdef EDQUOT
415 case EDQUOT: return WSAEDQUOT; break;
416 #endif
417 #ifdef ESTALE
418 case ESTALE: return WSAESTALE; break;
419 #endif
420 #ifdef EREMOTE
421 case EREMOTE: return WSAEREMOTE; break;
422 #endif
423 default: errno=err; perror("sock_set_error"); return ERROR_UNKNOWN; break;
427 /* set the last error depending on errno */
428 static void sock_set_error(void)
430 set_error( sock_get_error( errno ) );
433 /* create a socket */
434 DECL_HANDLER(create_socket)
436 struct object *obj;
437 int s = -1;
439 if ((obj = create_socket( req->family, req->type, req->protocol )) != NULL)
441 s = alloc_handle( current->process, obj, req->access, req->inherit );
442 release_object( obj );
444 req->handle = s;
447 /* accept a socket */
448 DECL_HANDLER(accept_socket)
450 struct object *obj;
451 int s = -1;
453 if ((obj = accept_socket( req->lhandle )) != NULL)
455 s = alloc_handle( current->process, obj, req->access, req->inherit );
456 release_object( obj );
458 req->handle = s;
461 /* set socket event parameters */
462 DECL_HANDLER(set_socket_event)
464 struct sock *sock;
465 struct event *oevent;
466 unsigned int omask;
468 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
469 if (!sock)
470 return;
471 oevent = sock->event;
472 omask = sock->mask;
473 sock->mask = req->mask;
474 sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
475 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
476 sock_reselect( sock );
477 if (sock->mask)
478 sock->state |= WS_FD_NONBLOCKING;
480 /* if a network event is pending, signal the event object
481 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
482 before a WSAEventSelect() was done on it.
483 (when dealing with Asynchronous socket) */
484 if (sock->pmask & sock->mask)
485 set_event(sock->event);
487 if (oevent)
489 if ((oevent != sock->event) && (omask & WS_FD_SERVEVENT))
490 /* if the service thread was waiting for the old event object,
491 * we should now signal it, to let the service thread
492 * object detect that it is now orphaned... */
493 set_event( oevent );
494 /* we're through with it */
495 release_object( oevent );
497 release_object( &sock->obj );
500 /* get socket event parameters */
501 DECL_HANDLER(get_socket_event)
503 struct sock *sock;
504 size_t size;
506 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
507 if (!sock)
509 req->mask = 0;
510 req->pmask = 0;
511 req->state = 0;
512 set_error(WSAENOTSOCK);
513 return;
515 req->mask = sock->mask;
516 req->pmask = sock->pmask;
517 req->state = sock->state;
518 size = min( get_req_data_size(req), sizeof(sock->errors) );
519 memcpy( get_req_data(req), sock->errors, size );
520 set_req_data_size( req, size );
522 if (req->service)
524 if (req->s_event)
526 struct event *sevent = get_event_obj(current->process, req->s_event, 0);
527 if (sevent == sock->event)
528 req->s_event = 0;
529 release_object( sevent );
531 if (!req->s_event)
533 if (req->c_event)
535 struct event *cevent = get_event_obj(current->process, req->c_event, EVENT_MODIFY_STATE);
536 reset_event( cevent );
537 release_object( cevent );
539 sock->pmask = 0;
540 sock_reselect( sock );
542 else set_error(WSAEINVAL);
544 release_object( &sock->obj );
547 /* re-enable pending socket events */
548 DECL_HANDLER(enable_socket_event)
550 struct sock *sock;
552 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
553 if (!sock)
554 return;
555 sock->pmask &= ~req->mask; /* is this safe? */
556 sock->hmask &= ~req->mask;
557 sock->state |= req->sstate;
558 sock->state &= ~req->cstate;
559 sock_reselect( sock );
561 /* service trigger */
562 if (req->mask & WS_FD_SERVEVENT)
564 sock->pmask |= WS_FD_SERVEVENT;
565 if (sock->event) {
566 if (debug_level) fprintf(stderr, "signalling service event ptr %p\n", sock->event);
567 set_event(sock->event);
571 release_object( &sock->obj );