Running object table are not using 0 as a valid index anymore.
[wine.git] / server / sock.c
blob531231b2cfaef5b81ba5420073ca9814149fc16d
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 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_read_fd */
72 sock_get_fd, /* get_write_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 if (debug_level)
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);
92 return optval;
95 static void sock_poll_event( struct object *obj, int event )
97 struct sock *sock = (struct sock *)obj;
98 unsigned int emask;
99 assert( sock->obj.ops == &sock_ops );
100 if (debug_level)
101 fprintf(stderr, "socket %d select event: %x\n", sock->obj.fd, event);
102 if (sock->state & WS_FD_CONNECT)
104 /* connecting */
105 if (event & POLLOUT)
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;
112 if (debug_level)
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 );
121 if (debug_level)
122 fprintf(stderr, "socket %d connection failure\n", sock->obj.fd);
124 } else
125 if (sock->state & WS_FD_LISTENING)
127 /* listening */
128 if (event & POLLIN)
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;
142 } else
144 /* normal data flow */
145 if (event & POLLIN)
147 /* incoming data */
148 sock->pmask |= FD_READ;
149 sock->hmask |= FD_READ;
150 sock->errors[FD_READ_BIT] = 0;
151 if (debug_level)
152 fprintf(stderr, "socket %d is readable\n", sock->obj.fd );
154 if (event & POLLOUT)
156 sock->pmask |= FD_WRITE;
157 sock->hmask |= FD_WRITE;
158 sock->errors[FD_WRITE_BIT] = 0;
159 if (debug_level)
160 fprintf(stderr, "socket %d is writable\n", sock->obj.fd);
162 if (event & POLLPRI)
164 sock->pmask |= FD_OOB;
165 sock->hmask |= FD_OOB;
166 if (debug_level)
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;
175 if (debug_level)
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;
217 int ev = 0;
219 assert( obj->ops == &sock_ops );
221 if (sock->state & WS_FD_CONNECT)
222 /* connecting, wait for writable */
223 return POLLOUT;
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;
230 return ev;
233 static int sock_get_fd( struct object *obj )
235 struct sock *sock = (struct sock *)obj;
236 int fd;
237 assert( obj->ops == &sock_ops );
238 fd = dup( sock->obj.fd );
239 if (fd==-1)
240 sock_set_error();
241 return 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? */
250 if (sock->event)
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 )
265 struct sock *sock;
266 int sockfd;
268 sockfd = socket( family, type, protocol );
269 if (debug_level)
270 fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
271 if (sockfd == -1) {
272 sock_set_error();
273 return NULL;
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;
278 sock->mask = 0;
279 sock->hmask = 0;
280 sock->pmask = 0;
281 sock->event = NULL;
282 sock_reselect( sock );
283 clear_error();
284 return &sock->obj;
287 /* accept a socket (creates a new fd) */
288 static struct object *accept_socket( int handle )
290 struct sock *acceptsock;
291 struct sock *sock;
292 int acceptfd;
293 struct sockaddr saddr;
294 int slen;
296 sock=(struct sock*)get_handle_obj(current->process,handle,
297 GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
298 if (!sock)
299 return NULL;
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
302 * return.
304 slen = sizeof(saddr);
305 acceptfd = accept(sock->obj.fd,&saddr,&slen);
306 if (acceptfd==-1) {
307 sock_set_error();
308 release_object( sock );
309 return NULL;
311 if (!(acceptsock = alloc_object( &sock_ops, acceptfd )))
313 release_object( sock );
314 return NULL;
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 );
325 clear_error();
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)
335 switch (errno)
337 case EINTR: set_error(WSAEINTR);break;
338 case EBADF: set_error(WSAEBADF);break;
339 case EPERM:
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;
363 case EPIPE:
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;
377 #ifdef EPROCLIM
378 case EPROCLIM: set_error(WSAEPROCLIM);break;
379 #endif
380 #ifdef EUSERS
381 case EUSERS: set_error(WSAEUSERS);break;
382 #endif
383 #ifdef EDQUOT
384 case EDQUOT: set_error(WSAEDQUOT);break;
385 #endif
386 #ifdef ESTALE
387 case ESTALE: set_error(WSAESTALE);break;
388 #endif
389 #ifdef EREMOTE
390 case EREMOTE: set_error(WSAEREMOTE);break;
391 #endif
392 default: perror("sock_set_error"); set_error( ERROR_UNKNOWN ); break;
396 /* create a socket */
397 DECL_HANDLER(create_socket)
399 struct object *obj;
400 int s = -1;
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 );
407 req->handle = s;
410 /* accept a socket */
411 DECL_HANDLER(accept_socket)
413 struct object *obj;
414 int s = -1;
416 if ((obj = accept_socket( req->lhandle )) != NULL)
418 s = alloc_handle( current->process, obj, req->access, req->inherit );
419 release_object( obj );
421 req->handle = s;
424 /* set socket event parameters */
425 DECL_HANDLER(set_socket_event)
427 struct sock *sock;
428 struct event *oevent;
429 unsigned int omask;
431 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
432 if (!sock)
433 return;
434 oevent = sock->event;
435 omask = sock->mask;
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 );
440 if (sock->mask)
441 sock->state |= WS_FD_NONBLOCKING;
442 if (oevent)
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... */
448 set_event( oevent );
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)
458 struct sock *sock;
460 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
461 if (!sock)
463 req->mask = 0;
464 req->pmask = 0;
465 req->state = 0;
466 set_error(WSAENOTSOCK);
467 return;
469 req->mask = sock->mask;
470 req->pmask = sock->pmask;
471 req->state = sock->state;
472 memcpy(req->errors, sock->errors, sizeof(sock->errors));
473 clear_error();
474 if (req->service)
476 if (req->s_event)
478 struct event *sevent = get_event_obj(current->process, req->s_event, 0);
479 if (sevent == sock->event)
480 req->s_event = 0;
481 release_object( sevent );
483 if (!req->s_event)
485 sock->pmask = 0;
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)
496 struct sock *sock;
498 sock=(struct sock*)get_handle_obj(current->process,req->handle,GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,&sock_ops);
499 if (!sock)
500 return;
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 );