Extended __wine_enter_vm86 to handle pending interrupts.
[wine/multimedia.git] / server / sock.c
blobb5c77a8e3f0a284fa57804b01d2ee2df033bafee
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/time.h>
22 #include <sys/types.h>
23 #ifdef HAVE_SYS_SOCKET_H
24 # include <sys/socket.h>
25 #endif
26 #include <sys/ioctl.h>
27 #ifdef HAVE_SYS_FILIO_H
28 # include <sys/filio.h>
29 #endif
30 #include <time.h>
31 #include <unistd.h>
33 #include "winerror.h"
34 #include "winbase.h"
35 #include "winsock2.h"
36 #include "process.h"
37 #include "handle.h"
38 #include "thread.h"
39 #include "request.h"
41 struct sock
43 struct object obj; /* object header */
44 unsigned int state; /* status bits */
45 unsigned int mask; /* event mask */
46 unsigned int hmask; /* held (blocked) events */
47 unsigned int pmask; /* pending events */
48 struct event *event; /* event object */
49 int errors[FD_MAX_EVENTS]; /* event errors */
52 static void sock_dump( struct object *obj, int verbose );
53 static int sock_signaled( struct object *obj, struct thread *thread );
54 static int sock_get_poll_events( struct object *obj );
55 static void sock_poll_event( struct object *obj, int event );
56 static int sock_get_fd( struct object *obj );
57 static void sock_destroy( struct object *obj );
58 static int sock_get_error( int err );
59 static void sock_set_error(void);
61 static const struct object_ops sock_ops =
63 sizeof(struct sock), /* size */
64 sock_dump, /* dump */
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_fd */
72 no_flush, /* flush */
73 no_get_file_info, /* get_file_info */
74 sock_destroy /* destroy */
77 static void sock_reselect( struct sock *sock )
79 int ev = sock_get_poll_events( &sock->obj );
80 struct pollfd pfd;
82 if (debug_level)
83 fprintf(stderr,"sock_reselect(%d): new mask %x\n", sock->obj.fd, ev);
85 if (sock->obj.select == -1) {
86 /* previously unconnected socket, is this reselect supposed to connect it? */
87 if (!sock->state) return;
88 /* ok, it is, attach it to the wineserver's main poll loop */
89 add_select_user( &sock->obj );
91 /* update condition mask */
92 set_select_events( &sock->obj, ev );
94 /* check whether condition is satisfied already */
95 pfd.fd = sock->obj.fd;
96 pfd.events = ev;
97 pfd.revents = 0;
98 poll( &pfd, 1, 0 );
99 if (pfd.revents)
100 sock_poll_event( &sock->obj, pfd.revents);
103 inline static int sock_error(int s)
105 unsigned int optval = 0, optlen;
107 optlen = sizeof(optval);
108 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
109 return optval ? sock_get_error(optval) : 0;
112 static void sock_poll_event( struct object *obj, int event )
114 struct sock *sock = (struct sock *)obj;
115 unsigned int emask;
116 assert( sock->obj.ops == &sock_ops );
117 if (debug_level)
118 fprintf(stderr, "socket %d select event: %x\n", sock->obj.fd, event);
119 if (sock->state & WS_FD_CONNECT)
121 /* connecting */
122 if (event & POLLOUT)
124 /* we got connected */
125 sock->state |= WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE;
126 sock->state &= ~WS_FD_CONNECT;
127 sock->pmask |= FD_CONNECT;
128 sock->errors[FD_CONNECT_BIT] = 0;
129 if (debug_level)
130 fprintf(stderr, "socket %d connection success\n", sock->obj.fd);
132 else if (event & (POLLERR|POLLHUP))
134 /* we didn't get connected? */
135 sock->state &= ~WS_FD_CONNECT;
136 sock->pmask |= FD_CONNECT;
137 sock->errors[FD_CONNECT_BIT] = sock_error( sock->obj.fd );
138 if (debug_level)
139 fprintf(stderr, "socket %d connection failure\n", sock->obj.fd);
141 } else
142 if (sock->state & WS_FD_LISTENING)
144 /* listening */
145 if (event & POLLIN)
147 /* incoming connection */
148 sock->pmask |= FD_ACCEPT;
149 sock->errors[FD_ACCEPT_BIT] = 0;
150 sock->hmask |= FD_ACCEPT;
152 else if (event & (POLLERR|POLLHUP))
154 /* failed incoming connection? */
155 sock->pmask |= FD_ACCEPT;
156 sock->errors[FD_ACCEPT_BIT] = sock_error( sock->obj.fd );
157 sock->hmask |= FD_ACCEPT;
159 } else
161 /* normal data flow */
162 if (event & POLLIN)
164 /* incoming data */
165 sock->pmask |= FD_READ;
166 sock->hmask |= FD_READ;
167 sock->errors[FD_READ_BIT] = 0;
168 if (debug_level)
169 fprintf(stderr, "socket %d is readable\n", sock->obj.fd );
171 if (event & POLLOUT)
173 sock->pmask |= FD_WRITE;
174 sock->hmask |= FD_WRITE;
175 sock->errors[FD_WRITE_BIT] = 0;
176 if (debug_level)
177 fprintf(stderr, "socket %d is writable\n", sock->obj.fd);
179 if (event & POLLPRI)
181 sock->pmask |= FD_OOB;
182 sock->hmask |= FD_OOB;
183 sock->errors[FD_OOB_BIT] = 0;
184 if (debug_level)
185 fprintf(stderr, "socket %d got OOB data\n", sock->obj.fd);
187 if (((event & POLLERR) || ((event & (POLLIN|POLLHUP)) == POLLHUP))
188 && (sock->state & (WS_FD_READ|WS_FD_WRITE))) {
189 /* socket closing */
190 sock->errors[FD_CLOSE_BIT] = sock_error( sock->obj.fd );
191 sock->state &= ~(WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE);
192 sock->pmask |= FD_CLOSE;
193 if (debug_level)
194 fprintf(stderr, "socket %d aborted by error %d\n",
195 sock->obj.fd, sock->errors[FD_CLOSE_BIT]);
199 if (event & (POLLERR|POLLHUP))
200 set_select_events( &sock->obj, -1 );
201 else
202 sock_reselect( sock );
203 /* wake up anyone waiting for whatever just happened */
204 emask = sock->pmask & sock->mask;
205 if (debug_level && emask)
206 fprintf(stderr, "socket %d pending events: %x\n", sock->obj.fd, emask);
207 if (emask && sock->event) {
208 if (debug_level) fprintf(stderr, "signalling event ptr %p\n", sock->event);
209 set_event(sock->event);
212 /* if anyone is stupid enough to wait on the socket object itself,
213 * maybe we should wake them up too, just in case? */
214 wake_up( &sock->obj, 0 );
217 static void sock_dump( struct object *obj, int verbose )
219 struct sock *sock = (struct sock *)obj;
220 assert( obj->ops == &sock_ops );
221 printf( "Socket fd=%d, state=%x, mask=%x, pending=%x, held=%x\n",
222 sock->obj.fd, sock->state,
223 sock->mask, sock->pmask, sock->hmask );
226 static int sock_signaled( struct object *obj, struct thread *thread )
228 struct sock *sock = (struct sock *)obj;
229 assert( obj->ops == &sock_ops );
231 return check_select_events( sock->obj.fd, sock_get_poll_events( &sock->obj ) );
234 static int sock_get_poll_events( struct object *obj )
236 struct sock *sock = (struct sock *)obj;
237 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
238 int ev = 0;
240 assert( obj->ops == &sock_ops );
242 if (sock->state & WS_FD_CONNECT)
243 /* connecting, wait for writable */
244 return POLLOUT;
245 if (sock->state & WS_FD_LISTENING)
246 /* listening, wait for readable */
247 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
249 if (mask & FD_READ) ev |= POLLIN | POLLPRI;
250 if (mask & FD_WRITE) ev |= POLLOUT;
251 return ev;
254 static int sock_get_fd( struct object *obj )
256 struct sock *sock = (struct sock *)obj;
257 assert( obj->ops == &sock_ops );
258 return sock->obj.fd;
261 static void sock_destroy( struct object *obj )
263 struct sock *sock = (struct sock *)obj;
264 assert( obj->ops == &sock_ops );
266 /* FIXME: special socket shutdown stuff? */
267 if (sock->event)
269 /* if the service thread was waiting for the event object,
270 * we should now signal it, to let the service thread
271 * object detect that it is now orphaned... */
272 if (sock->mask & WS_FD_SERVEVENT)
273 set_event( sock->event );
274 /* we're through with it */
275 release_object( sock->event );
279 /* create a new and unconnected socket */
280 static struct object *create_socket( int family, int type, int protocol )
282 struct sock *sock;
283 int sockfd;
285 sockfd = socket( family, type, protocol );
286 if (debug_level)
287 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
288 if (sockfd == -1) {
289 sock_set_error();
290 return NULL;
292 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
293 if (!(sock = alloc_object( &sock_ops, -1 ))) return NULL;
294 sock->obj.fd = sockfd;
295 sock->state = (type != SOCK_STREAM) ? (WS_FD_READ|WS_FD_WRITE) : 0;
296 sock->mask = 0;
297 sock->hmask = 0;
298 sock->pmask = 0;
299 sock->event = NULL;
300 sock_reselect( sock );
301 clear_error();
302 return &sock->obj;
305 /* accept a socket (creates a new fd) */
306 static struct object *accept_socket( handle_t handle )
308 struct sock *acceptsock;
309 struct sock *sock;
310 int acceptfd;
311 struct sockaddr saddr;
312 int slen;
314 sock=(struct sock*)get_handle_obj(current->process,handle,
315 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
316 if (!sock)
317 return NULL;
318 /* Try to accept(2). We can't be safe that this an already connected socket
319 * or that accept() is allowed on it. In those cases we will get -1/errno
320 * return.
322 slen = sizeof(saddr);
323 acceptfd = accept(sock->obj.fd,&saddr,&slen);
324 if (acceptfd==-1) {
325 sock_set_error();
326 release_object( sock );
327 return NULL;
329 if (!(acceptsock = alloc_object( &sock_ops, -1 )))
331 release_object( sock );
332 return NULL;
335 /* newly created socket gets the same properties of the listening socket */
336 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
337 acceptsock->obj.fd = acceptfd;
338 acceptsock->state = WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE;
339 if (sock->state & WS_FD_NONBLOCKING)
340 acceptsock->state |= WS_FD_NONBLOCKING;
341 acceptsock->mask = sock->mask;
342 acceptsock->hmask = 0;
343 acceptsock->pmask = 0;
344 acceptsock->event = NULL;
345 if (sock->event && !(sock->mask & WS_FD_SERVEVENT))
346 acceptsock->event = (struct event *)grab_object( sock->event );
348 sock_reselect( acceptsock );
349 clear_error();
350 sock->pmask &= ~FD_ACCEPT;
351 sock->hmask &= ~FD_ACCEPT;
352 sock_reselect( sock );
353 release_object( sock );
354 return &acceptsock->obj;
357 /* set the last error depending on errno */
358 static int sock_get_error( int err )
360 switch (err)
362 case EINTR: return WSAEINTR; break;
363 case EBADF: return WSAEBADF; break;
364 case EPERM:
365 case EACCES: return WSAEACCES; break;
366 case EFAULT: return WSAEFAULT; break;
367 case EINVAL: return WSAEINVAL; break;
368 case EMFILE: return WSAEMFILE; break;
369 case EWOULDBLOCK: return WSAEWOULDBLOCK; break;
370 case EINPROGRESS: return WSAEINPROGRESS; break;
371 case EALREADY: return WSAEALREADY; break;
372 case ENOTSOCK: return WSAENOTSOCK; break;
373 case EDESTADDRREQ: return WSAEDESTADDRREQ; break;
374 case EMSGSIZE: return WSAEMSGSIZE; break;
375 case EPROTOTYPE: return WSAEPROTOTYPE; break;
376 case ENOPROTOOPT: return WSAENOPROTOOPT; break;
377 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT; break;
378 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT; break;
379 case EOPNOTSUPP: return WSAEOPNOTSUPP; break;
380 case EPFNOSUPPORT: return WSAEPFNOSUPPORT; break;
381 case EAFNOSUPPORT: return WSAEAFNOSUPPORT; break;
382 case EADDRINUSE: return WSAEADDRINUSE; break;
383 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL; break;
384 case ENETDOWN: return WSAENETDOWN; break;
385 case ENETUNREACH: return WSAENETUNREACH; break;
386 case ENETRESET: return WSAENETRESET; break;
387 case ECONNABORTED: return WSAECONNABORTED; break;
388 case EPIPE:
389 case ECONNRESET: return WSAECONNRESET; break;
390 case ENOBUFS: return WSAENOBUFS; break;
391 case EISCONN: return WSAEISCONN; break;
392 case ENOTCONN: return WSAENOTCONN; break;
393 case ESHUTDOWN: return WSAESHUTDOWN; break;
394 case ETOOMANYREFS: return WSAETOOMANYREFS; break;
395 case ETIMEDOUT: return WSAETIMEDOUT; break;
396 case ECONNREFUSED: return WSAECONNREFUSED; break;
397 case ELOOP: return WSAELOOP; break;
398 case ENAMETOOLONG: return WSAENAMETOOLONG; break;
399 case EHOSTDOWN: return WSAEHOSTDOWN; break;
400 case EHOSTUNREACH: return WSAEHOSTUNREACH; break;
401 case ENOTEMPTY: return WSAENOTEMPTY; break;
402 #ifdef EPROCLIM
403 case EPROCLIM: return WSAEPROCLIM; break;
404 #endif
405 #ifdef EUSERS
406 case EUSERS: return WSAEUSERS; break;
407 #endif
408 #ifdef EDQUOT
409 case EDQUOT: return WSAEDQUOT; break;
410 #endif
411 #ifdef ESTALE
412 case ESTALE: return WSAESTALE; break;
413 #endif
414 #ifdef EREMOTE
415 case EREMOTE: return WSAEREMOTE; break;
416 #endif
417 default: errno=err; perror("sock_set_error"); return ERROR_UNKNOWN; break;
421 /* set the last error depending on errno */
422 static void sock_set_error(void)
424 set_error( sock_get_error( errno ) );
427 /* create a socket */
428 DECL_HANDLER(create_socket)
430 struct object *obj;
432 req->handle = 0;
433 if ((obj = create_socket( req->family, req->type, req->protocol )) != NULL)
435 req->handle = alloc_handle( current->process, obj, req->access, req->inherit );
436 release_object( obj );
440 /* accept a socket */
441 DECL_HANDLER(accept_socket)
443 struct object *obj;
445 req->handle = 0;
446 if ((obj = accept_socket( req->lhandle )) != NULL)
448 req->handle = alloc_handle( current->process, obj, req->access, req->inherit );
449 release_object( obj );
453 /* set socket event parameters */
454 DECL_HANDLER(set_socket_event)
456 struct sock *sock;
457 struct event *oevent;
458 unsigned int omask;
460 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
461 if (!sock)
462 return;
463 oevent = sock->event;
464 omask = sock->mask;
465 sock->mask = req->mask;
466 sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
467 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
468 sock_reselect( sock );
469 if (sock->mask)
470 sock->state |= WS_FD_NONBLOCKING;
472 /* if a network event is pending, signal the event object
473 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
474 before a WSAEventSelect() was done on it.
475 (when dealing with Asynchronous socket) */
476 if (sock->pmask & sock->mask)
477 set_event(sock->event);
479 if (oevent)
481 if ((oevent != sock->event) && (omask & WS_FD_SERVEVENT))
482 /* if the service thread was waiting for the old event object,
483 * we should now signal it, to let the service thread
484 * object detect that it is now orphaned... */
485 set_event( oevent );
486 /* we're through with it */
487 release_object( oevent );
489 release_object( &sock->obj );
492 /* get socket event parameters */
493 DECL_HANDLER(get_socket_event)
495 struct sock *sock;
496 size_t size;
498 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
499 if (!sock)
501 req->mask = 0;
502 req->pmask = 0;
503 req->state = 0;
504 set_error(WSAENOTSOCK);
505 return;
507 req->mask = sock->mask;
508 req->pmask = sock->pmask;
509 req->state = sock->state;
510 size = min( get_req_data_size(req), sizeof(sock->errors) );
511 memcpy( get_req_data(req), sock->errors, size );
512 set_req_data_size( req, size );
514 if (req->service)
516 if (req->s_event)
518 struct event *sevent = get_event_obj(current->process, req->s_event, 0);
519 if (sevent == sock->event)
520 req->s_event = 0;
521 release_object( sevent );
523 if (!req->s_event)
525 if (req->c_event)
527 struct event *cevent = get_event_obj(current->process, req->c_event, EVENT_MODIFY_STATE);
528 reset_event( cevent );
529 release_object( cevent );
531 sock->pmask = 0;
532 sock_reselect( sock );
534 else set_error(WSAEINVAL);
536 release_object( &sock->obj );
539 /* re-enable pending socket events */
540 DECL_HANDLER(enable_socket_event)
542 struct sock *sock;
544 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
545 if (!sock)
546 return;
547 sock->pmask &= ~req->mask; /* is this safe? */
548 sock->hmask &= ~req->mask;
549 sock->state |= req->sstate;
550 sock->state &= ~req->cstate;
551 sock_reselect( sock );
553 /* service trigger */
554 if (req->mask & WS_FD_SERVEVENT)
556 sock->pmask |= WS_FD_SERVEVENT;
557 if (sock->event) {
558 if (debug_level) fprintf(stderr, "signalling service event ptr %p\n", sock->event);
559 set_event(sock->event);
563 release_object( &sock->obj );