~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/server/sock.c

Version: ~ [ wine-1.5.31 ] ~ [ wine-1.5.30 ] ~ [ wine-1.5.29 ] ~ [ wine-1.5.28 ] ~ [ wine-1.5.27 ] ~ [ wine-1.5.26 ] ~ [ wine-1.5.25 ] ~ [ wine-1.5.24 ] ~ [ wine-1.5.23 ] ~ [ wine-1.5.22 ] ~ [ wine-1.5.21 ] ~ [ wine-1.5.20 ] ~ [ wine-1.5.19 ] ~ [ wine-1.5.18 ] ~ [ wine-1.5.17 ] ~ [ wine-1.5.16 ] ~ [ wine-1.5.15 ] ~ [ wine-1.5.14 ] ~ [ wine-1.5.13 ] ~ [ wine-1.5.12 ] ~ [ wine-1.5.11 ] ~ [ wine-1.5.10 ] ~ [ wine-1.5.9 ] ~ [ wine-1.5.8 ] ~ [ wine-1.5.7 ] ~ [ wine-1.4.1 ] ~ [ wine-1.5.6 ] ~ [ wine-1.5.5 ] ~ [ wine-1.5.4 ] ~ [ wine-1.5.3 ] ~ [ wine-1.5.2 ] ~ [ wine-1.5.1 ] ~ [ wine-1.5.0 ] ~ [ wine-1.4 ] ~ [ wine-1.4-rc6 ] ~ [ wine-1.4-rc5 ] ~ [ wine-1.4-rc4 ] ~ [ wine-1.4-rc3 ] ~ [ wine-1.4-rc2 ] ~ [ wine-1.4-rc1 ] ~ [ wine-1.3.37 ] ~ [ wine-1.3.36 ] ~ [ wine-1.3.35 ] ~ [ wine-1.3.34 ] ~ [ wine-1.3.33 ] ~ [ wine-1.3.32 ] ~ [ wine-1.3.31 ] ~ [ wine-1.3.30 ] ~ [ wine-1.3.29 ] ~ [ wine-1.3.28 ] ~ [ wine-1.3.27 ] ~ [ wine-1.3.26 ] ~ [ wine-1.3.25 ] ~ [ wine-1.3.24 ] ~ [ wine-1.3.23 ] ~ [ wine-1.3.22 ] ~ [ wine-1.3.21 ] ~ [ wine-1.3.20 ] ~ [ wine-1.3.19 ] ~ [ wine-1.3.18 ] ~ [ wine-1.2.3 ] ~ [ wine-1.3.17 ] ~ [ wine-1.3.16 ] ~ [ wine-1.3.15 ] ~ [ wine-1.3.14 ] ~ [ wine-1.3.13 ] ~ [ wine-1.3.12 ] ~ [ wine-1.3.11 ] ~ [ wine-1.3.10 ] ~ [ wine-1.3.9 ] ~ [ wine-1.2.2 ] ~ [ wine-1.3.8 ] ~ [ wine-1.3.7 ] ~ [ wine-1.3.6 ] ~ [ wine-1.3.5 ] ~ [ wine-1.2.1 ] ~ [ wine-1.3.4 ] ~ [ wine-1.3.3 ] ~ [ wine-1.3.2 ] ~ [ wine-1.3.1 ] ~ [ wine-1.3.0 ] ~ [ wine-1.2 ] ~ [ wine-1.2-rc7 ] ~ [ wine-1.2-rc6 ] ~ [ wine-1.2-rc5 ] ~ [ wine-1.2-rc4 ] ~ [ wine-1.2-rc3 ] ~ [ wine-1.2-rc2 ] ~ [ wine-1.2-rc1 ] ~ [ wine-1.1.44 ] ~ [ wine-1.1.43 ] ~ [ wine-1.1.42 ] ~ [ wine-1.1.41 ] ~ [ wine-1.1.40 ] ~ [ wine-1.1.39 ] ~ [ wine-1.1.38 ] ~ [ wine-1.1.37 ] ~ [ wine-1.1.36 ] ~ [ wine-1.1.35 ] ~ [ wine-1.1.34 ] ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  1 /*
  2  * Server-side socket management
  3  *
  4  * Copyright (C) 1999 Marcus Meissner, Ove Kåven
  5  *
  6  * This library is free software; you can redistribute it and/or
  7  * modify it under the terms of the GNU Lesser General Public
  8  * License as published by the Free Software Foundation; either
  9  * version 2.1 of the License, or (at your option) any later version.
 10  *
 11  * This library is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14  * Lesser General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU Lesser General Public
 17  * License along with this library; if not, write to the Free Software
 18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 19  *
 20  * FIXME: we use read|write access in all cases. Shouldn't we depend that
 21  * on the access of the current handle?
 22  */
 23 
 24 #include "config.h"
 25 
 26 #include <assert.h>
 27 #include <fcntl.h>
 28 #include <stdarg.h>
 29 #include <stdio.h>
 30 #include <string.h>
 31 #include <stdlib.h>
 32 #include <errno.h>
 33 #ifdef HAVE_SYS_ERRNO_H
 34 # include <sys/errno.h>
 35 #endif
 36 #include <sys/time.h>
 37 #include <sys/types.h>
 38 #ifdef HAVE_SYS_SOCKET_H
 39 # include <sys/socket.h>
 40 #endif
 41 #ifdef HAVE_SYS_IOCTL_H
 42 #include <sys/ioctl.h>
 43 #endif
 44 #ifdef HAVE_SYS_FILIO_H
 45 # include <sys/filio.h>
 46 #endif
 47 #include <time.h>
 48 #include <unistd.h>
 49 
 50 #include "ntstatus.h"
 51 #define WIN32_NO_STATUS
 52 #include "windef.h"
 53 #include "winternl.h"
 54 
 55 #include "process.h"
 56 #include "file.h"
 57 #include "handle.h"
 58 #include "thread.h"
 59 #include "request.h"
 60 #include "user.h"
 61 
 62 /* To avoid conflicts with the Unix socket headers. Plus we only need a few
 63  * macros anyway.
 64  */
 65 #define USE_WS_PREFIX
 66 #include "winsock2.h"
 67 
 68 struct sock
 69 {
 70     struct object       obj;         /* object header */
 71     struct fd          *fd;          /* socket file descriptor */
 72     unsigned int        state;       /* status bits */
 73     unsigned int        mask;        /* event mask */
 74     unsigned int        hmask;       /* held (blocked) events */
 75     unsigned int        pmask;       /* pending events */
 76     unsigned int        flags;       /* socket flags */
 77     int                 polling;     /* is socket being polled? */
 78     unsigned short      type;        /* socket type */
 79     unsigned short      family;      /* socket family */
 80     struct event       *event;       /* event object */
 81     user_handle_t       window;      /* window to send the message to */
 82     unsigned int        message;     /* message to send */
 83     obj_handle_t        wparam;      /* message wparam (socket handle) */
 84     int                 errors[FD_MAX_EVENTS]; /* event errors */
 85     struct sock        *deferred;    /* socket that waits for a deferred accept */
 86     struct async_queue *read_q;      /* queue for asynchronous reads */
 87     struct async_queue *write_q;     /* queue for asynchronous writes */
 88 };
 89 
 90 static void sock_dump( struct object *obj, int verbose );
 91 static int sock_signaled( struct object *obj, struct thread *thread );
 92 static struct fd *sock_get_fd( struct object *obj );
 93 static void sock_destroy( struct object *obj );
 94 
 95 static int sock_get_poll_events( struct fd *fd );
 96 static void sock_poll_event( struct fd *fd, int event );
 97 static enum server_fd_type sock_get_fd_type( struct fd *fd );
 98 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
 99 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
100 static void sock_cancel_async( struct fd *fd );
101 
102 static int sock_get_error( int err );
103 static void sock_set_error(void);
104 
105 static const struct object_ops sock_ops =
106 {
107     sizeof(struct sock),          /* size */
108     sock_dump,                    /* dump */
109     no_get_type,                  /* get_type */
110     add_queue,                    /* add_queue */
111     remove_queue,                 /* remove_queue */
112     sock_signaled,                /* signaled */
113     no_satisfied,                 /* satisfied */
114     no_signal,                    /* signal */
115     sock_get_fd,                  /* get_fd */
116     default_fd_map_access,        /* map_access */
117     default_get_sd,               /* get_sd */
118     default_set_sd,               /* set_sd */
119     no_lookup_name,               /* lookup_name */
120     no_open_file,                 /* open_file */
121     fd_close_handle,              /* close_handle */
122     sock_destroy                  /* destroy */
123 };
124 
125 static const struct fd_ops sock_fd_ops =
126 {
127     sock_get_poll_events,         /* get_poll_events */
128     sock_poll_event,              /* poll_event */
129     no_flush,                     /* flush */
130     sock_get_fd_type,             /* get_file_info */
131     default_fd_ioctl,             /* ioctl */
132     sock_queue_async,             /* queue_async */
133     sock_reselect_async,          /* reselect_async */
134     sock_cancel_async             /* cancel_async */
135 };
136 
137 
138 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
139  * we post messages if there are multiple events.  Used to send
140  * messages.  The problem is if there is both a FD_CONNECT event and,
141  * say, an FD_READ event available on the same socket, we want to
142  * notify the app of the connect event first.  Otherwise it may
143  * discard the read event because it thinks it hasn't connected yet.
144  */
145 static const int event_bitorder[FD_MAX_EVENTS] =
146 {
147     FD_CONNECT_BIT,
148     FD_ACCEPT_BIT,
149     FD_OOB_BIT,
150     FD_WRITE_BIT,
151     FD_READ_BIT,
152     FD_CLOSE_BIT,
153     6, 7, 8, 9  /* leftovers */
154 };
155 
156 /* Flags that make sense only for SOCK_STREAM sockets */
157 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
158 
159 typedef enum {
160     SOCK_SHUTDOWN_ERROR = -1,
161     SOCK_SHUTDOWN_EOF = 0,
162     SOCK_SHUTDOWN_POLLHUP = 1
163 } sock_shutdown_t;
164 
165 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
166 
167 static sock_shutdown_t sock_check_pollhup(void)
168 {
169     sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
170     int fd[2], n;
171     struct pollfd pfd;
172     char dummy;
173 
174     if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) goto out;
175     if ( shutdown( fd[0], 1 ) ) goto out;
176 
177     pfd.fd = fd[1];
178     pfd.events = POLLIN;
179     pfd.revents = 0;
180 
181     n = poll( &pfd, 1, 0 );
182     if ( n != 1 ) goto out; /* error or timeout */
183     if ( pfd.revents & POLLHUP )
184         ret = SOCK_SHUTDOWN_POLLHUP;
185     else if ( pfd.revents & POLLIN &&
186               read( fd[1], &dummy, 1 ) == 0 )
187         ret = SOCK_SHUTDOWN_EOF;
188 
189 out:
190     close( fd[0] );
191     close( fd[1] );
192     return ret;
193 }
194 
195 void sock_init(void)
196 {
197     sock_shutdown_type = sock_check_pollhup();
198 
199     switch ( sock_shutdown_type )
200     {
201     case SOCK_SHUTDOWN_EOF:
202         if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
203         break;
204     case SOCK_SHUTDOWN_POLLHUP:
205         if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
206         break;
207     default:
208         fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
209         sock_shutdown_type = SOCK_SHUTDOWN_EOF;
210     }
211 }
212 
213 static int sock_reselect( struct sock *sock )
214 {
215     int ev = sock_get_poll_events( sock->fd );
216 
217     if (debug_level)
218         fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
219 
220     if (!sock->polling)  /* FIXME: should find a better way to do this */
221     {
222         /* previously unconnected socket, is this reselect supposed to connect it? */
223         if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
224         /* ok, it is, attach it to the wineserver's main poll loop */
225         sock->polling = 1;
226     }
227     /* update condition mask */
228     set_fd_events( sock->fd, ev );
229     return ev;
230 }
231 
232 /* After POLLHUP is received, the socket will no longer be in the main select loop.
233    This function is used to signal pending events nevertheless */
234 static void sock_try_event( struct sock *sock, int event )
235 {
236     event = check_fd_events( sock->fd, event );
237     if (event)
238     {
239         if ( debug_level ) fprintf( stderr, "sock_try_event: %x\n", event );
240         sock_poll_event( sock->fd, event );
241     }
242 }
243 
244 /* wake anybody waiting on the socket event or send the associated message */
245 static void sock_wake_up( struct sock *sock, int pollev )
246 {
247     unsigned int events = sock->pmask & sock->mask;
248     int i;
249     int async_active = 0;
250 
251     if ( pollev & (POLLIN|POLLPRI) && async_waiting( sock->read_q ))
252     {
253         if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
254         async_wake_up( sock->read_q, STATUS_ALERTED );
255         async_active = 1;
256     }
257     if ( pollev & POLLOUT && async_waiting( sock->write_q ))
258     {
259         if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
260         async_wake_up( sock->write_q, STATUS_ALERTED );
261         async_active = 1;
262     }
263 
264     /* Do not signal events if there are still pending asynchronous IO requests */
265     /* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
266     if ( !events || async_active ) return;
267 
268     if (sock->event)
269     {
270         if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
271         set_event( sock->event );
272     }
273     if (sock->window)
274     {
275         if (debug_level) fprintf(stderr, "signalling events %x win %p\n", events, sock->window );
276         for (i = 0; i < FD_MAX_EVENTS; i++)
277         {
278             int event = event_bitorder[i];
279             if (sock->pmask & (1 << event))
280             {
281                 unsigned int lparam = (1 << event) | (sock->errors[event] << 16);
282                 post_message( sock->window, sock->message, (unsigned long)sock->wparam, lparam );
283             }
284         }
285         sock->pmask = 0;
286         sock_reselect( sock );
287     }
288 }
289 
290 static inline int sock_error( struct fd *fd )
291 {
292     unsigned int optval = 0, optlen;
293 
294     optlen = sizeof(optval);
295     getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
296     return optval ? sock_get_error(optval) : 0;
297 }
298 
299 static void sock_poll_event( struct fd *fd, int event )
300 {
301     struct sock *sock = get_fd_user( fd );
302     int hangup_seen = 0;
303 
304     assert( sock->obj.ops == &sock_ops );
305     if (debug_level)
306         fprintf(stderr, "socket %p select event: %x\n", sock, event);
307     if (sock->state & FD_CONNECT)
308     {
309         /* connecting */
310         if (event & POLLOUT)
311         {
312             /* we got connected */
313             sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
314             sock->state &= ~FD_CONNECT;
315             sock->pmask |= FD_CONNECT;
316             sock->errors[FD_CONNECT_BIT] = 0;
317             if (debug_level)
318                 fprintf(stderr, "socket %p connection success\n", sock);
319         }
320         else if (event & (POLLERR|POLLHUP))
321         {
322             /* we didn't get connected? */
323             sock->state &= ~FD_CONNECT;
324             sock->pmask |= FD_CONNECT;
325             sock->errors[FD_CONNECT_BIT] = sock_error( fd );
326             if (debug_level)
327                 fprintf(stderr, "socket %p connection failure\n", sock);
328         }
329     }
330     else if (sock->state & FD_WINE_LISTENING)
331     {
332         /* listening */
333         if (event & POLLIN)
334         {
335             /* incoming connection */
336             sock->pmask |= FD_ACCEPT;
337             sock->errors[FD_ACCEPT_BIT] = 0;
338             sock->hmask |= FD_ACCEPT;
339         }
340         else if (event & (POLLERR|POLLHUP))
341         {
342             /* failed incoming connection? */
343             sock->pmask |= FD_ACCEPT;
344             sock->errors[FD_ACCEPT_BIT] = sock_error( fd );
345             sock->hmask |= FD_ACCEPT;
346         }
347     }
348     else
349     {
350         /* normal data flow */
351         if ( sock->type == SOCK_STREAM && ( event & POLLIN ) )
352         {
353             char dummy;
354             int nr;
355 
356             /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
357              * has been closed, so we need to check for it explicitly here */
358             nr  = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
359             if ( nr > 0 )
360             {
361                 /* incoming data */
362                 sock->pmask |= FD_READ;
363                 sock->hmask |= (FD_READ|FD_CLOSE);
364                 sock->errors[FD_READ_BIT] = 0;
365                 if (debug_level)
366                     fprintf(stderr, "socket %p is readable\n", sock );
367             }
368             else if ( nr == 0 )
369                 hangup_seen = 1;
370             else
371             {
372                 /* EAGAIN can happen if an async recv() falls between the server's poll()
373                    call and the invocation of this routine */
374                 if ( errno == EAGAIN )
375                     event &= ~POLLIN;
376                 else
377                 {
378                     if ( debug_level )
379                         fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
380                     event = POLLERR;
381                 }
382             }
383 
384         }
385         else if ( sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP) )
386         {
387             hangup_seen = 1;
388         }
389         else if ( event & POLLIN ) /* POLLIN for non-stream socket */
390         {
391             sock->pmask |= FD_READ;
392             sock->hmask |= (FD_READ|FD_CLOSE);
393             sock->errors[FD_READ_BIT] = 0;
394             if (debug_level)
395                 fprintf(stderr, "socket %p is readable\n", sock );
396 
397         }
398 
399         if (event & POLLOUT)
400         {
401             sock->pmask |= FD_WRITE;
402             sock->hmask |= FD_WRITE;
403             sock->errors[FD_WRITE_BIT] = 0;
404             if (debug_level)
405                 fprintf(stderr, "socket %p is writable\n", sock);
406         }
407         if (event & POLLPRI)
408         {
409             sock->pmask |= FD_OOB;
410             sock->hmask |= FD_OOB;
411             sock->errors[FD_OOB_BIT] = 0;
412             if (debug_level)
413                 fprintf(stderr, "socket %p got OOB data\n", sock);
414         }
415         /* According to WS2 specs, FD_CLOSE is only delivered when there is
416            no more data to be read (i.e. hangup_seen = 1) */
417         else if ( hangup_seen && (sock->state & (FD_READ|FD_WRITE) ))
418         {
419             sock->errors[FD_CLOSE_BIT] = sock_error( fd );
420             if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
421                 sock->state &= ~FD_WRITE;
422             sock->pmask |= FD_CLOSE;
423             sock->hmask |= FD_CLOSE;
424             if (debug_level)
425                 fprintf(stderr, "socket %p aborted by error %d, event: %x - removing from select loop\n",
426                         sock, sock->errors[FD_CLOSE_BIT], event);
427         }
428     }
429 
430     if ( sock->pmask & FD_CLOSE || event & (POLLERR|POLLHUP) )
431     {
432         if ( debug_level )
433             fprintf( stderr, "removing socket %p from select loop\n", sock );
434         set_fd_events( sock->fd, -1 );
435     }
436     else
437         sock_reselect( sock );
438 
439     /* wake up anyone waiting for whatever just happened */
440     if ( sock->pmask & sock->mask || sock->flags & WSA_FLAG_OVERLAPPED ) sock_wake_up( sock, event );
441 
442     /* if anyone is stupid enough to wait on the socket object itself,
443      * maybe we should wake them up too, just in case? */
444     wake_up( &sock->obj, 0 );
445 }
446 
447 static void sock_dump( struct object *obj, int verbose )
448 {
449     struct sock *sock = (struct sock *)obj;
450     assert( obj->ops == &sock_ops );
451     printf( "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
452             sock->fd, sock->state,
453             sock->mask, sock->pmask, sock->hmask );
454 }
455 
456 static int sock_signaled( struct object *obj, struct thread *thread )
457 {
458     struct sock *sock = (struct sock *)obj;
459     assert( obj->ops == &sock_ops );
460 
461     return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
462 }
463 
464 static int sock_get_poll_events( struct fd *fd )
465 {
466     struct sock *sock = get_fd_user( fd );
467     unsigned int mask = sock->mask & sock->state & ~sock->hmask;
468     int ev = 0;
469 
470     assert( sock->obj.ops == &sock_ops );
471 
472     if (sock->state & FD_CONNECT)
473         /* connecting, wait for writable */
474         return POLLOUT;
475     if (sock->state & FD_WINE_LISTENING)
476         /* listening, wait for readable */
477         return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
478 
479     if (mask & FD_READ  || async_waiting( sock->read_q )) ev |= POLLIN | POLLPRI;
480     if (mask & FD_WRITE || async_waiting( sock->write_q )) ev |= POLLOUT;
481     /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
482     if ( sock->type == SOCK_STREAM && ( sock->mask & ~sock->hmask & FD_CLOSE) )
483         ev |= POLLIN;
484 
485     return ev;
486 }
487 
488 static enum server_fd_type sock_get_fd_type( struct fd *fd )
489 {
490     return FD_TYPE_SOCKET;
491 }
492 
493 static void sock_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
494 {
495     struct sock *sock = get_fd_user( fd );
496     struct async_queue *queue;
497     int pollev;
498 
499     assert( sock->obj.ops == &sock_ops );
500 
501     switch (type)
502     {
503     case ASYNC_TYPE_READ:
504         if (!sock->read_q && !(sock->read_q = create_async_queue( sock->fd ))) return;
505         queue = sock->read_q;
506         sock->hmask &= ~FD_CLOSE;
507         break;
508     case ASYNC_TYPE_WRITE:
509         if (!sock->write_q && !(sock->write_q = create_async_queue( sock->fd ))) return;
510         queue = sock->write_q;
511         break;
512     default:
513         set_error( STATUS_INVALID_PARAMETER );
514         return;
515     }
516 
517     if ( ( !( sock->state & FD_READ ) && type == ASYNC_TYPE_READ  ) ||
518          ( !( sock->state & FD_WRITE ) && type == ASYNC_TYPE_WRITE ) )
519     {
520         set_error( STATUS_PIPE_DISCONNECTED );
521     }
522     else
523     {
524         struct async *async;
525         if (!(async = create_async( current, queue, data ))) return;
526         release_object( async );
527         set_error( STATUS_PENDING );
528     }
529 
530     pollev = sock_reselect( sock );
531     if ( pollev ) sock_try_event( sock, pollev );
532 }
533 
534 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
535 {
536     struct sock *sock = get_fd_user( fd );
537     int events = sock_reselect( sock );
538     if (events) sock_try_event( sock, events );
539 }
540 
541 static void sock_cancel_async( struct fd *fd )
542 {
543     struct sock *sock = get_fd_user( fd );
544     assert( sock->obj.ops == &sock_ops );
545 
546     async_wake_up( sock->read_q, STATUS_CANCELLED );
547     async_wake_up( sock->write_q, STATUS_CANCELLED );
548 }
549 
550 static struct fd *sock_get_fd( struct object *obj )
551 {
552     struct sock *sock = (struct sock *)obj;
553     return (struct fd *)grab_object( sock->fd );
554 }
555 
556 static void sock_destroy( struct object *obj )
557 {
558     struct sock *sock = (struct sock *)obj;
559     assert( obj->ops == &sock_ops );
560 
561     /* FIXME: special socket shutdown stuff? */
562 
563     if ( sock->deferred )
564         release_object( sock->deferred );
565 
566     free_async_queue( sock->read_q );
567     free_async_queue( sock->write_q );
568     if (sock->event) release_object( sock->event );
569     if (sock->fd)
570     {
571         /* shut the socket down to force pending poll() calls in the client to return */
572         shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
573         release_object( sock->fd );
574     }
575 }
576 
577 /* create a new and unconnected socket */
578 static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
579 {
580     struct sock *sock;
581     int sockfd;
582 
583     sockfd = socket( family, type, protocol );
584     if (debug_level)
585         fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
586     if (sockfd == -1)
587     {
588         sock_set_error();
589         return NULL;
590     }
591     fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
592     if (!(sock = alloc_object( &sock_ops )))
593     {
594         close( sockfd );
595         return NULL;
596     }
597     sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
598     sock->mask    = 0;
599     sock->hmask   = 0;
600     sock->pmask   = 0;
601     sock->polling = 0;
602     sock->flags   = flags;
603     sock->type    = type;
604     sock->family  = family;
605     sock->event   = NULL;
606     sock->window  = 0;
607     sock->message = 0;
608     sock->wparam  = 0;
609     sock->deferred = NULL;
610     sock->read_q  = NULL;
611     sock->write_q = NULL;
612     if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj,
613                             (flags & WSA_FLAG_OVERLAPPED) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT )))
614     {
615         release_object( sock );
616         return NULL;
617     }
618     sock_reselect( sock );
619     clear_error();
620     return &sock->obj;
621 }
622 
623 /* accept a socket (creates a new fd) */
624 static struct sock *accept_socket( obj_handle_t handle )
625 {
626     struct sock *acceptsock;
627     struct sock *sock;
628     int acceptfd;
629     struct sockaddr     saddr;
630 
631     sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
632     if (!sock)
633         return NULL;
634 
635     if ( sock->deferred )
636     {
637         acceptsock = sock->deferred;
638         sock->deferred = NULL;
639     }
640     else
641     {
642 
643         /* Try to accept(2). We can't be safe that this an already connected socket
644          * or that accept() is allowed on it. In those cases we will get -1/errno
645          * return.
646          */
647         unsigned int slen = sizeof(saddr);
648         acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
649         if (acceptfd==-1)
650         {
651             sock_set_error();
652             release_object( sock );
653             return NULL;
654         }
655         if (!(acceptsock = alloc_object( &sock_ops )))
656         {
657             close( acceptfd );
658             release_object( sock );
659             return NULL;
660         }
661 
662         /* newly created socket gets the same properties of the listening socket */
663         fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
664         acceptsock->state  = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
665         if (sock->state & FD_WINE_NONBLOCKING)
666             acceptsock->state |= FD_WINE_NONBLOCKING;
667         acceptsock->mask    = sock->mask;
668         acceptsock->hmask   = 0;
669         acceptsock->pmask   = 0;
670         acceptsock->polling = 0;
671         acceptsock->type    = sock->type;
672         acceptsock->family  = sock->family;
673         acceptsock->event   = NULL;
674         acceptsock->window  = sock->window;
675         acceptsock->message = sock->message;
676         acceptsock->wparam  = 0;
677         if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
678         acceptsock->flags = sock->flags;
679         acceptsock->deferred = NULL;
680         acceptsock->read_q  = NULL;
681         acceptsock->write_q = NULL;
682         if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
683                                                     get_fd_options( sock->fd ) )))
684         {
685             release_object( acceptsock );
686             release_object( sock );
687             return NULL;
688         }
689     }
690     clear_error();
691     sock->pmask &= ~FD_ACCEPT;
692     sock->hmask &= ~FD_ACCEPT;
693     sock_reselect( sock );
694     release_object( sock );
695     return acceptsock;
696 }
697 
698 /* set the last error depending on errno */
699 static int sock_get_error( int err )
700 {
701     switch (err)
702     {
703         case EINTR:             return WSAEINTR;
704         case EBADF:             return WSAEBADF;
705         case EPERM:
706         case EACCES:            return WSAEACCES;
707         case EFAULT:            return WSAEFAULT;
708         case EINVAL:            return WSAEINVAL;
709         case EMFILE:            return WSAEMFILE;
710         case EWOULDBLOCK:       return WSAEWOULDBLOCK;
711         case EINPROGRESS:       return WSAEINPROGRESS;
712         case EALREADY:          return WSAEALREADY;
713         case ENOTSOCK:          return WSAENOTSOCK;
714         case EDESTADDRREQ:      return WSAEDESTADDRREQ;
715         case EMSGSIZE:          return WSAEMSGSIZE;
716         case EPROTOTYPE:        return WSAEPROTOTYPE;
717         case ENOPROTOOPT:       return WSAENOPROTOOPT;
718         case EPROTONOSUPPORT:   return WSAEPROTONOSUPPORT;
719         case ESOCKTNOSUPPORT:   return WSAESOCKTNOSUPPORT;
720         case EOPNOTSUPP:        return WSAEOPNOTSUPP;
721         case EPFNOSUPPORT:      return WSAEPFNOSUPPORT;
722         case EAFNOSUPPORT:      return WSAEAFNOSUPPORT;
723         case EADDRINUSE:        return WSAEADDRINUSE;
724         case EADDRNOTAVAIL:     return WSAEADDRNOTAVAIL;
725         case ENETDOWN:          return WSAENETDOWN;
726         case ENETUNREACH:       return WSAENETUNREACH;
727         case ENETRESET:         return WSAENETRESET;
728         case ECONNABORTED:      return WSAECONNABORTED;
729         case EPIPE:
730         case ECONNRESET:        return WSAECONNRESET;
731         case ENOBUFS:           return WSAENOBUFS;
732         case EISCONN:           return WSAEISCONN;
733         case ENOTCONN:          return WSAENOTCONN;
734         case ESHUTDOWN:         return WSAESHUTDOWN;
735         case ETOOMANYREFS:      return WSAETOOMANYREFS;
736         case ETIMEDOUT:         return WSAETIMEDOUT;
737         case ECONNREFUSED:      return WSAECONNREFUSED;
738         case ELOOP:             return WSAELOOP;
739         case ENAMETOOLONG:      return WSAENAMETOOLONG;
740         case EHOSTDOWN:         return WSAEHOSTDOWN;
741         case EHOSTUNREACH:      return WSAEHOSTUNREACH;
742         case ENOTEMPTY:         return WSAENOTEMPTY;
743 #ifdef EPROCLIM
744         case EPROCLIM:          return WSAEPROCLIM;
745 #endif
746 #ifdef EUSERS
747         case EUSERS:            return WSAEUSERS;
748 #endif
749 #ifdef EDQUOT
750         case EDQUOT:            return WSAEDQUOT;
751 #endif
752 #ifdef ESTALE
753         case ESTALE:            return WSAESTALE;
754 #endif
755 #ifdef EREMOTE
756         case EREMOTE:           return WSAEREMOTE;
757 #endif
758         default:
759             errno = err;
760             perror("wineserver: sock_get_error() can't map error");
761             return WSAEFAULT;
762     }
763 }
764 
765 /* set the last error depending on errno */
766 static void sock_set_error(void)
767 {
768     set_error( sock_get_error( errno ) );
769 }
770 
771 /* create a socket */
772 DECL_HANDLER(create_socket)
773 {
774     struct object *obj;
775 
776     reply->handle = 0;
777     if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
778     {
779         reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
780         release_object( obj );
781     }
782 }
783 
784 /* accept a socket */
785 DECL_HANDLER(accept_socket)
786 {
787     struct sock *sock;
788 
789     reply->handle = 0;
790     if ((sock = accept_socket( req->lhandle )) != NULL)
791     {
792         reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
793         sock->wparam = reply->handle;  /* wparam for message is the socket handle */
794         sock_reselect( sock );
795         release_object( &sock->obj );
796     }
797 }
798 
799 /* set socket event parameters */
800 DECL_HANDLER(set_socket_event)
801 {
802     struct sock *sock;
803     struct event *old_event;
804     int pollev;
805 
806     if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
807                                                 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
808     old_event = sock->event;
809     sock->mask    = req->mask;
810     sock->hmask   &= ~req->mask; /* re-enable held events */
811     sock->event   = NULL;
812     sock->window  = req->window;
813     sock->message = req->msg;
814     sock->wparam  = req->handle;  /* wparam is the socket handle */
815     if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
816 
817     if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
818 
819     pollev = sock_reselect( sock );
820     if ( pollev ) sock_try_event( sock, pollev );
821 
822     if (sock->mask)
823         sock->state |= FD_WINE_NONBLOCKING;
824 
825     /* if a network event is pending, signal the event object
826        it is possible that FD_CONNECT or FD_ACCEPT network events has happened
827        before a WSAEventSelect() was done on it.
828        (when dealing with Asynchronous socket)  */
829     if (sock->pmask & sock->mask) sock_wake_up( sock, pollev );
830 
831     if (old_event) release_object( old_event ); /* we're through with it */
832     release_object( &sock->obj );
833 }
834 
835 /* get socket event parameters */
836 DECL_HANDLER(get_socket_event)
837 {
838     struct sock *sock;
839 
840     sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
841     if (!sock)
842     {
843         reply->mask  = 0;
844         reply->pmask = 0;
845         reply->state = 0;
846         set_error( WSAENOTSOCK );
847         return;
848     }
849     reply->mask  = sock->mask;
850     reply->pmask = sock->pmask;
851     reply->state = sock->state;
852     set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
853 
854     if (req->service)
855     {
856         if (req->c_event)
857         {
858             struct event *cevent = get_event_obj( current->process, req->c_event,
859                                                   EVENT_MODIFY_STATE );
860             if (cevent)
861             {
862                 reset_event( cevent );
863                 release_object( cevent );
864             }
865         }
866         sock->pmask = 0;
867         sock_reselect( sock );
868     }
869     release_object( &sock->obj );
870 }
871 
872 /* re-enable pending socket events */
873 DECL_HANDLER(enable_socket_event)
874 {
875     struct sock *sock;
876     int pollev;
877 
878     if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
879                                                FILE_WRITE_ATTRIBUTES, &sock_ops)))
880         return;
881 
882     sock->pmask &= ~req->mask; /* is this safe? */
883     sock->hmask &= ~req->mask;
884     if ( req->mask & FD_READ )
885         sock->hmask &= ~FD_CLOSE;
886     sock->state |= req->sstate;
887     sock->state &= ~req->cstate;
888     if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
889 
890     pollev = sock_reselect( sock );
891     if ( pollev ) sock_try_event( sock, pollev );
892 
893     release_object( &sock->obj );
894 }
895 
896 DECL_HANDLER(set_socket_deferred)
897 {
898     struct sock *sock, *acceptsock;
899 
900     sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
901     if ( !sock )
902     {
903         set_error( WSAENOTSOCK );
904         return;
905     }
906     acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
907     if ( !acceptsock )
908     {
909         release_object( sock );
910         set_error( WSAENOTSOCK );
911         return;
912     }
913     sock->deferred = acceptsock;
914     release_object( sock );
915 }
916 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.