Fix remaining signed/unsigned mismatches.
[wine.git] / server / sock.c
blobc5f41e4f0d5ae3a35a4ab56e3357d54337a5f61a
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_fd */
73 no_flush, /* flush */
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 );
81 struct pollfd pfd;
83 if (debug_level)
84 fprintf(stderr,"sock_reselect(%d): new mask %x\n", sock->obj.fd, ev);
86 if (sock->obj.select == -1) {
87 /* previously unconnected socket, is this reselect supposed to connect it? */
88 if (!sock->state) return;
89 /* ok, it is, attach it to the wineserver's main poll loop */
90 add_select_user( &sock->obj );
92 /* update condition mask */
93 set_select_events( &sock->obj, ev );
95 /* check whether condition is satisfied already */
96 pfd.fd = sock->obj.fd;
97 pfd.events = ev;
98 pfd.revents = 0;
99 poll( &pfd, 1, 0 );
100 if (pfd.revents)
101 sock_poll_event( &sock->obj, pfd.revents);
104 inline static int sock_error(int s)
106 unsigned int optval = 0, optlen;
108 optlen = sizeof(optval);
109 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
110 return optval ? sock_get_error(optval) : 0;
113 static void sock_poll_event( struct object *obj, int event )
115 struct sock *sock = (struct sock *)obj;
116 unsigned int emask;
117 assert( sock->obj.ops == &sock_ops );
118 if (debug_level)
119 fprintf(stderr, "socket %d select event: %x\n", sock->obj.fd, event);
120 if (sock->state & WS_FD_CONNECT)
122 /* connecting */
123 if (event & POLLOUT)
125 /* we got connected */
126 sock->state |= WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE;
127 sock->state &= ~WS_FD_CONNECT;
128 sock->pmask |= FD_CONNECT;
129 sock->errors[FD_CONNECT_BIT] = 0;
130 if (debug_level)
131 fprintf(stderr, "socket %d connection success\n", sock->obj.fd);
133 else if (event & (POLLERR|POLLHUP))
135 /* we didn't get connected? */
136 sock->state &= ~WS_FD_CONNECT;
137 sock->pmask |= FD_CONNECT;
138 sock->errors[FD_CONNECT_BIT] = sock_error( sock->obj.fd );
139 if (debug_level)
140 fprintf(stderr, "socket %d connection failure\n", sock->obj.fd);
142 } else
143 if (sock->state & WS_FD_LISTENING)
145 /* listening */
146 if (event & POLLIN)
148 /* incoming connection */
149 sock->pmask |= FD_ACCEPT;
150 sock->errors[FD_ACCEPT_BIT] = 0;
151 sock->hmask |= FD_ACCEPT;
153 else if (event & (POLLERR|POLLHUP))
155 /* failed incoming connection? */
156 sock->pmask |= FD_ACCEPT;
157 sock->errors[FD_ACCEPT_BIT] = sock_error( sock->obj.fd );
158 sock->hmask |= FD_ACCEPT;
160 } else
162 /* normal data flow */
163 if (event & POLLIN)
165 /* incoming data */
166 sock->pmask |= FD_READ;
167 sock->hmask |= FD_READ;
168 sock->errors[FD_READ_BIT] = 0;
169 if (debug_level)
170 fprintf(stderr, "socket %d is readable\n", sock->obj.fd );
172 if (event & POLLOUT)
174 sock->pmask |= FD_WRITE;
175 sock->hmask |= FD_WRITE;
176 sock->errors[FD_WRITE_BIT] = 0;
177 if (debug_level)
178 fprintf(stderr, "socket %d is writable\n", sock->obj.fd);
180 if (event & POLLPRI)
182 sock->pmask |= FD_OOB;
183 sock->hmask |= FD_OOB;
184 sock->errors[FD_OOB_BIT] = 0;
185 if (debug_level)
186 fprintf(stderr, "socket %d got OOB data\n", sock->obj.fd);
188 if (((event & POLLERR) || ((event & (POLLIN|POLLHUP)) == POLLHUP))
189 && (sock->state & (WS_FD_READ|WS_FD_WRITE))) {
190 /* socket closing */
191 sock->errors[FD_CLOSE_BIT] = sock_error( sock->obj.fd );
192 sock->state &= ~(WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE);
193 sock->pmask |= FD_CLOSE;
194 if (debug_level)
195 fprintf(stderr, "socket %d aborted by error %d\n",
196 sock->obj.fd, sock->errors[FD_CLOSE_BIT]);
200 if (event & (POLLERR|POLLHUP))
201 set_select_events( &sock->obj, -1 );
202 else
203 sock_reselect( sock );
204 /* wake up anyone waiting for whatever just happened */
205 emask = sock->pmask & sock->mask;
206 if (debug_level && emask)
207 fprintf(stderr, "socket %d pending events: %x\n", sock->obj.fd, emask);
208 if (emask && sock->event) {
209 if (debug_level) fprintf(stderr, "signalling event ptr %p\n", sock->event);
210 set_event(sock->event);
213 /* if anyone is stupid enough to wait on the socket object itself,
214 * maybe we should wake them up too, just in case? */
215 wake_up( &sock->obj, 0 );
218 static void sock_dump( struct object *obj, int verbose )
220 struct sock *sock = (struct sock *)obj;
221 assert( obj->ops == &sock_ops );
222 printf( "Socket fd=%d, state=%x, mask=%x, pending=%x, held=%x\n",
223 sock->obj.fd, sock->state,
224 sock->mask, sock->pmask, sock->hmask );
227 static int sock_signaled( struct object *obj, struct thread *thread )
229 struct sock *sock = (struct sock *)obj;
230 assert( obj->ops == &sock_ops );
232 return check_select_events( sock->obj.fd, sock_get_poll_events( &sock->obj ) );
235 static int sock_get_poll_events( struct object *obj )
237 struct sock *sock = (struct sock *)obj;
238 unsigned int mask = sock->mask & sock->state & ~sock->hmask;
239 int ev = 0;
241 assert( obj->ops == &sock_ops );
243 if (sock->state & WS_FD_CONNECT)
244 /* connecting, wait for writable */
245 return POLLOUT;
246 if (sock->state & WS_FD_LISTENING)
247 /* listening, wait for readable */
248 return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
250 if (mask & FD_READ) ev |= POLLIN | POLLPRI;
251 if (mask & FD_WRITE) ev |= POLLOUT;
252 return ev;
255 static int sock_get_fd( struct object *obj )
257 struct sock *sock = (struct sock *)obj;
258 int fd;
259 assert( obj->ops == &sock_ops );
260 fd = dup( sock->obj.fd );
261 if (fd==-1)
262 sock_set_error();
263 return fd;
266 static void sock_destroy( struct object *obj )
268 struct sock *sock = (struct sock *)obj;
269 assert( obj->ops == &sock_ops );
271 /* FIXME: special socket shutdown stuff? */
272 if (sock->event)
274 /* if the service thread was waiting for the event object,
275 * we should now signal it, to let the service thread
276 * object detect that it is now orphaned... */
277 if (sock->mask & WS_FD_SERVEVENT)
278 set_event( sock->event );
279 /* we're through with it */
280 release_object( sock->event );
284 /* create a new and unconnected socket */
285 static struct object *create_socket( int family, int type, int protocol )
287 struct sock *sock;
288 int sockfd;
290 sockfd = socket( family, type, protocol );
291 if (debug_level)
292 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
293 if (sockfd == -1) {
294 sock_set_error();
295 return NULL;
297 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
298 if (!(sock = alloc_object( &sock_ops, -1 ))) return NULL;
299 sock->obj.fd = sockfd;
300 sock->state = (type != SOCK_STREAM) ? (WS_FD_READ|WS_FD_WRITE) : 0;
301 sock->mask = 0;
302 sock->hmask = 0;
303 sock->pmask = 0;
304 sock->event = NULL;
305 sock_reselect( sock );
306 clear_error();
307 return &sock->obj;
310 /* accept a socket (creates a new fd) */
311 static struct object *accept_socket( int handle )
313 struct sock *acceptsock;
314 struct sock *sock;
315 int acceptfd;
316 struct sockaddr saddr;
317 int slen;
319 sock=(struct sock*)get_handle_obj(current->process,handle,
320 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
321 if (!sock)
322 return NULL;
323 /* Try to accept(2). We can't be safe that this an already connected socket
324 * or that accept() is allowed on it. In those cases we will get -1/errno
325 * return.
327 slen = sizeof(saddr);
328 acceptfd = accept(sock->obj.fd,&saddr,&slen);
329 if (acceptfd==-1) {
330 sock_set_error();
331 release_object( sock );
332 return NULL;
334 if (!(acceptsock = alloc_object( &sock_ops, -1 )))
336 release_object( sock );
337 return NULL;
340 /* newly created socket gets the same properties of the listening socket */
341 fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
342 acceptsock->obj.fd = acceptfd;
343 acceptsock->state = WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE;
344 if (sock->state & WS_FD_NONBLOCKING)
345 acceptsock->state |= WS_FD_NONBLOCKING;
346 acceptsock->mask = sock->mask;
347 acceptsock->hmask = 0;
348 acceptsock->pmask = 0;
349 acceptsock->event = NULL;
350 if (sock->event && !(sock->mask & WS_FD_SERVEVENT))
351 acceptsock->event = (struct event *)grab_object( sock->event );
353 sock_reselect( acceptsock );
354 clear_error();
355 sock->pmask &= ~FD_ACCEPT;
356 sock->hmask &= ~FD_ACCEPT;
357 sock_reselect( sock );
358 release_object( sock );
359 return &acceptsock->obj;
362 /* set the last error depending on errno */
363 static int sock_get_error( int err )
365 switch (err)
367 case EINTR: return WSAEINTR; break;
368 case EBADF: return WSAEBADF; break;
369 case EPERM:
370 case EACCES: return WSAEACCES; break;
371 case EFAULT: return WSAEFAULT; break;
372 case EINVAL: return WSAEINVAL; break;
373 case EMFILE: return WSAEMFILE; break;
374 case EWOULDBLOCK: return WSAEWOULDBLOCK; break;
375 case EINPROGRESS: return WSAEINPROGRESS; break;
376 case EALREADY: return WSAEALREADY; break;
377 case ENOTSOCK: return WSAENOTSOCK; break;
378 case EDESTADDRREQ: return WSAEDESTADDRREQ; break;
379 case EMSGSIZE: return WSAEMSGSIZE; break;
380 case EPROTOTYPE: return WSAEPROTOTYPE; break;
381 case ENOPROTOOPT: return WSAENOPROTOOPT; break;
382 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT; break;
383 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT; break;
384 case EOPNOTSUPP: return WSAEOPNOTSUPP; break;
385 case EPFNOSUPPORT: return WSAEPFNOSUPPORT; break;
386 case EAFNOSUPPORT: return WSAEAFNOSUPPORT; break;
387 case EADDRINUSE: return WSAEADDRINUSE; break;
388 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL; break;
389 case ENETDOWN: return WSAENETDOWN; break;
390 case ENETUNREACH: return WSAENETUNREACH; break;
391 case ENETRESET: return WSAENETRESET; break;
392 case ECONNABORTED: return WSAECONNABORTED; break;
393 case EPIPE:
394 case ECONNRESET: return WSAECONNRESET; break;
395 case ENOBUFS: return WSAENOBUFS; break;
396 case EISCONN: return WSAEISCONN; break;
397 case ENOTCONN: return WSAENOTCONN; break;
398 case ESHUTDOWN: return WSAESHUTDOWN; break;
399 case ETOOMANYREFS: return WSAETOOMANYREFS; break;
400 case ETIMEDOUT: return WSAETIMEDOUT; break;
401 case ECONNREFUSED: return WSAECONNREFUSED; break;
402 case ELOOP: return WSAELOOP; break;
403 case ENAMETOOLONG: return WSAENAMETOOLONG; break;
404 case EHOSTDOWN: return WSAEHOSTDOWN; break;
405 case EHOSTUNREACH: return WSAEHOSTUNREACH; break;
406 case ENOTEMPTY: return WSAENOTEMPTY; break;
407 #ifdef EPROCLIM
408 case EPROCLIM: return WSAEPROCLIM; break;
409 #endif
410 #ifdef EUSERS
411 case EUSERS: return WSAEUSERS; break;
412 #endif
413 #ifdef EDQUOT
414 case EDQUOT: return WSAEDQUOT; break;
415 #endif
416 #ifdef ESTALE
417 case ESTALE: return WSAESTALE; break;
418 #endif
419 #ifdef EREMOTE
420 case EREMOTE: return WSAEREMOTE; break;
421 #endif
422 default: errno=err; perror("sock_set_error"); return ERROR_UNKNOWN; break;
426 /* set the last error depending on errno */
427 static void sock_set_error(void)
429 set_error( sock_get_error( errno ) );
432 /* create a socket */
433 DECL_HANDLER(create_socket)
435 struct object *obj;
436 int s = -1;
438 if ((obj = create_socket( req->family, req->type, req->protocol )) != NULL)
440 s = alloc_handle( current->process, obj, req->access, req->inherit );
441 release_object( obj );
443 req->handle = s;
446 /* accept a socket */
447 DECL_HANDLER(accept_socket)
449 struct object *obj;
450 int s = -1;
452 if ((obj = accept_socket( req->lhandle )) != NULL)
454 s = alloc_handle( current->process, obj, req->access, req->inherit );
455 release_object( obj );
457 req->handle = s;
460 /* set socket event parameters */
461 DECL_HANDLER(set_socket_event)
463 struct sock *sock;
464 struct event *oevent;
465 unsigned int omask;
467 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
468 if (!sock)
469 return;
470 oevent = sock->event;
471 omask = sock->mask;
472 sock->mask = req->mask;
473 sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
474 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
475 sock_reselect( sock );
476 if (sock->mask)
477 sock->state |= WS_FD_NONBLOCKING;
479 /* if a network event is pending, signal the event object
480 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
481 before a WSAEventSelect() was done on it.
482 (when dealing with Asynchronous socket) */
483 if (sock->pmask & sock->mask)
484 set_event(sock->event);
486 if (oevent)
488 if ((oevent != sock->event) && (omask & WS_FD_SERVEVENT))
489 /* if the service thread was waiting for the old event object,
490 * we should now signal it, to let the service thread
491 * object detect that it is now orphaned... */
492 set_event( oevent );
493 /* we're through with it */
494 release_object( oevent );
496 release_object( &sock->obj );
499 /* get socket event parameters */
500 DECL_HANDLER(get_socket_event)
502 struct sock *sock;
503 size_t size;
505 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
506 if (!sock)
508 req->mask = 0;
509 req->pmask = 0;
510 req->state = 0;
511 set_error(WSAENOTSOCK);
512 return;
514 req->mask = sock->mask;
515 req->pmask = sock->pmask;
516 req->state = sock->state;
517 size = min( get_req_data_size(req), sizeof(sock->errors) );
518 memcpy( get_req_data(req), sock->errors, size );
519 set_req_data_size( req, size );
521 if (req->service)
523 if (req->s_event)
525 struct event *sevent = get_event_obj(current->process, req->s_event, 0);
526 if (sevent == sock->event)
527 req->s_event = 0;
528 release_object( sevent );
530 if (!req->s_event)
532 if (req->c_event)
534 struct event *cevent = get_event_obj(current->process, req->c_event, EVENT_MODIFY_STATE);
535 reset_event( cevent );
536 release_object( cevent );
538 sock->pmask = 0;
539 sock_reselect( sock );
541 else set_error(WSAEINVAL);
543 release_object( &sock->obj );
546 /* re-enable pending socket events */
547 DECL_HANDLER(enable_socket_event)
549 struct sock *sock;
551 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
552 if (!sock)
553 return;
554 sock->pmask &= ~req->mask; /* is this safe? */
555 sock->hmask &= ~req->mask;
556 sock->state |= req->sstate;
557 sock->state &= ~req->cstate;
558 sock_reselect( sock );
560 /* service trigger */
561 if (req->mask & WS_FD_SERVEVENT)
563 sock->pmask |= WS_FD_SERVEVENT;
564 if (sock->event) {
565 if (debug_level) fprintf(stderr, "signalling service event ptr %p\n", sock->event);
566 set_event(sock->event);
570 release_object( &sock->obj );