Fixed a couple of HWND type mismatches.
[wine/multimedia.git] / server / sock.c
blob58ba06c3557fa97c03596b3f0f3b452bd6cda19f
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 & ~FD_WINE_NONBLOCKING)) 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 & FD_CONNECT)
121 /* connecting */
122 if (event & POLLOUT)
124 /* we got connected */
125 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
126 sock->state &= ~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 &= ~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 & FD_WINE_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 char dummy;
166 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
167 * has been closed, so we need to check for it explicitly here */
168 if (!recv( sock->obj.fd, &dummy, 1, MSG_PEEK )) event = POLLHUP;
169 else
171 /* incoming data */
172 sock->pmask |= FD_READ;
173 sock->hmask |= FD_READ;
174 sock->errors[FD_READ_BIT] = 0;
175 if (debug_level)
176 fprintf(stderr, "socket %d is readable\n", sock->obj.fd );
179 if (event & POLLOUT)
181 sock->pmask |= FD_WRITE;
182 sock->hmask |= FD_WRITE;
183 sock->errors[FD_WRITE_BIT] = 0;
184 if (debug_level)
185 fprintf(stderr, "socket %d is writable\n", sock->obj.fd);
187 if (event & POLLPRI)
189 sock->pmask |= FD_OOB;
190 sock->hmask |= FD_OOB;
191 sock->errors[FD_OOB_BIT] = 0;
192 if (debug_level)
193 fprintf(stderr, "socket %d got OOB data\n", sock->obj.fd);
195 if (((event & POLLERR) || ((event & (POLLIN|POLLHUP)) == POLLHUP))
196 && (sock->state & (FD_READ|FD_WRITE))) {
197 /* socket closing */
198 sock->errors[FD_CLOSE_BIT] = sock_error( sock->obj.fd );
199 sock->state &= ~(FD_WINE_CONNECTED|FD_READ|FD_WRITE);
200 sock->pmask |= FD_CLOSE;
201 if (debug_level)
202 fprintf(stderr, "socket %d aborted by error %d\n",
203 sock->obj.fd, sock->errors[FD_CLOSE_BIT]);
207 if (event & (POLLERR|POLLHUP))
208 set_select_events( &sock->obj, -1 );
209 else
210 sock_reselect( sock );
211 /* wake up anyone waiting for whatever just happened */
212 emask = sock->pmask & sock->mask;
213 if (debug_level && emask)
214 fprintf(stderr, "socket %d pending events: %x\n", sock->obj.fd, emask);
215 if (emask && sock->event) {
216 if (debug_level) fprintf(stderr, "signalling event ptr %p\n", sock->event);
217 set_event(sock->event);
220 /* if anyone is stupid enough to wait on the socket object itself,
221 * maybe we should wake them up too, just in case? */
222 wake_up( &sock->obj, 0 );
225 static void sock_dump( struct object *obj, int verbose )
227 struct sock *sock = (struct sock *)obj;
228 assert( obj->ops == &sock_ops );
229 printf( "Socket fd=%d, state=%x, mask=%x, pending=%x, held=%x\n",
230 sock->obj.fd, sock->state,
231 sock->mask, sock->pmask, sock->hmask );
234 static int sock_signaled( struct object *obj, struct thread *thread )
236 struct sock *sock = (struct sock *)obj;
237 assert( obj->ops == &sock_ops );
239 return check_select_events( sock->obj.fd, sock_get_poll_events( &sock->obj ) );
242 static int sock_get_poll_events( struct object *obj )
244 struct sock *sock = (struct sock *)obj;
245 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
246 int ev = 0;
248 assert( obj->ops == &sock_ops );
250 if (sock->state & FD_CONNECT)
251 /* connecting, wait for writable */
252 return POLLOUT;
253 if (sock->state & FD_WINE_LISTENING)
254 /* listening, wait for readable */
255 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
257 if (mask & FD_READ) ev |= POLLIN | POLLPRI;
258 if (mask & FD_WRITE) ev |= POLLOUT;
259 return ev;
262 static int sock_get_fd( struct object *obj )
264 struct sock *sock = (struct sock *)obj;
265 assert( obj->ops == &sock_ops );
266 return sock->obj.fd;
269 static void sock_destroy( struct object *obj )
271 struct sock *sock = (struct sock *)obj;
272 assert( obj->ops == &sock_ops );
274 /* FIXME: special socket shutdown stuff? */
275 if (sock->event)
277 /* if the service thread was waiting for the event object,
278 * we should now signal it, to let the service thread
279 * object detect that it is now orphaned... */
280 if (sock->mask & FD_WINE_SERVEVENT)
281 set_event( sock->event );
282 /* we're through with it */
283 release_object( sock->event );
287 /* create a new and unconnected socket */
288 static struct object *create_socket( int family, int type, int protocol )
290 struct sock *sock;
291 int sockfd;
293 sockfd = socket( family, type, protocol );
294 if (debug_level)
295 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
296 if (sockfd == -1) {
297 sock_set_error();
298 return NULL;
300 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
301 if (!(sock = alloc_object( &sock_ops, -1 ))) return NULL;
302 sock->obj.fd = sockfd;
303 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
304 sock->mask = 0;
305 sock->hmask = 0;
306 sock->pmask = 0;
307 sock->event = NULL;
308 sock_reselect( sock );
309 clear_error();
310 return &sock->obj;
313 /* accept a socket (creates a new fd) */
314 static struct object *accept_socket( handle_t handle )
316 struct sock *acceptsock;
317 struct sock *sock;
318 int acceptfd;
319 struct sockaddr saddr;
320 int slen;
322 sock=(struct sock*)get_handle_obj(current->process,handle,
323 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
324 if (!sock)
325 return NULL;
326 /* Try to accept(2). We can't be safe that this an already connected socket
327 * or that accept() is allowed on it. In those cases we will get -1/errno
328 * return.
330 slen = sizeof(saddr);
331 acceptfd = accept(sock->obj.fd,&saddr,&slen);
332 if (acceptfd==-1) {
333 sock_set_error();
334 release_object( sock );
335 return NULL;
337 if (!(acceptsock = alloc_object( &sock_ops, -1 )))
339 release_object( sock );
340 return NULL;
343 /* newly created socket gets the same properties of the listening socket */
344 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
345 acceptsock->obj.fd = acceptfd;
346 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
347 if (sock->state & FD_WINE_NONBLOCKING)
348 acceptsock->state |= FD_WINE_NONBLOCKING;
349 acceptsock->mask = sock->mask;
350 acceptsock->hmask = 0;
351 acceptsock->pmask = 0;
352 acceptsock->event = NULL;
353 if (sock->event && !(sock->mask & FD_WINE_SERVEVENT))
354 acceptsock->event = (struct event *)grab_object( sock->event );
356 sock_reselect( acceptsock );
357 clear_error();
358 sock->pmask &= ~FD_ACCEPT;
359 sock->hmask &= ~FD_ACCEPT;
360 sock_reselect( sock );
361 release_object( sock );
362 return &acceptsock->obj;
365 /* set the last error depending on errno */
366 static int sock_get_error( int err )
368 switch (err)
370 case EINTR: return WSAEINTR; break;
371 case EBADF: return WSAEBADF; break;
372 case EPERM:
373 case EACCES: return WSAEACCES; break;
374 case EFAULT: return WSAEFAULT; break;
375 case EINVAL: return WSAEINVAL; break;
376 case EMFILE: return WSAEMFILE; break;
377 case EWOULDBLOCK: return WSAEWOULDBLOCK; break;
378 case EINPROGRESS: return WSAEINPROGRESS; break;
379 case EALREADY: return WSAEALREADY; break;
380 case ENOTSOCK: return WSAENOTSOCK; break;
381 case EDESTADDRREQ: return WSAEDESTADDRREQ; break;
382 case EMSGSIZE: return WSAEMSGSIZE; break;
383 case EPROTOTYPE: return WSAEPROTOTYPE; break;
384 case ENOPROTOOPT: return WSAENOPROTOOPT; break;
385 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT; break;
386 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT; break;
387 case EOPNOTSUPP: return WSAEOPNOTSUPP; break;
388 case EPFNOSUPPORT: return WSAEPFNOSUPPORT; break;
389 case EAFNOSUPPORT: return WSAEAFNOSUPPORT; break;
390 case EADDRINUSE: return WSAEADDRINUSE; break;
391 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL; break;
392 case ENETDOWN: return WSAENETDOWN; break;
393 case ENETUNREACH: return WSAENETUNREACH; break;
394 case ENETRESET: return WSAENETRESET; break;
395 case ECONNABORTED: return WSAECONNABORTED; break;
396 case EPIPE:
397 case ECONNRESET: return WSAECONNRESET; break;
398 case ENOBUFS: return WSAENOBUFS; break;
399 case EISCONN: return WSAEISCONN; break;
400 case ENOTCONN: return WSAENOTCONN; break;
401 case ESHUTDOWN: return WSAESHUTDOWN; break;
402 case ETOOMANYREFS: return WSAETOOMANYREFS; break;
403 case ETIMEDOUT: return WSAETIMEDOUT; break;
404 case ECONNREFUSED: return WSAECONNREFUSED; break;
405 case ELOOP: return WSAELOOP; break;
406 case ENAMETOOLONG: return WSAENAMETOOLONG; break;
407 case EHOSTDOWN: return WSAEHOSTDOWN; break;
408 case EHOSTUNREACH: return WSAEHOSTUNREACH; break;
409 case ENOTEMPTY: return WSAENOTEMPTY; break;
410 #ifdef EPROCLIM
411 case EPROCLIM: return WSAEPROCLIM; break;
412 #endif
413 #ifdef EUSERS
414 case EUSERS: return WSAEUSERS; break;
415 #endif
416 #ifdef EDQUOT
417 case EDQUOT: return WSAEDQUOT; break;
418 #endif
419 #ifdef ESTALE
420 case ESTALE: return WSAESTALE; break;
421 #endif
422 #ifdef EREMOTE
423 case EREMOTE: return WSAEREMOTE; break;
424 #endif
425 default: errno=err; perror("sock_set_error"); return ERROR_UNKNOWN; break;
429 /* set the last error depending on errno */
430 static void sock_set_error(void)
432 set_error( sock_get_error( errno ) );
435 /* create a socket */
436 DECL_HANDLER(create_socket)
438 struct object *obj;
440 req->handle = 0;
441 if ((obj = create_socket( req->family, req->type, req->protocol )) != NULL)
443 req->handle = alloc_handle( current->process, obj, req->access, req->inherit );
444 release_object( obj );
448 /* accept a socket */
449 DECL_HANDLER(accept_socket)
451 struct object *obj;
453 req->handle = 0;
454 if ((obj = accept_socket( req->lhandle )) != NULL)
456 req->handle = alloc_handle( current->process, obj, req->access, req->inherit );
457 release_object( obj );
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 |= FD_WINE_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 & FD_WINE_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 & FD_WINE_SERVEVENT)
564 sock->pmask |= FD_WINE_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 );