2 * Server-side socket management
4 * Copyright (C) 1999 Marcus Meissner, Ove Kåven
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.
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.
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
20 * FIXME: we use read|write access in all cases. Shouldn't we depend that
21 * on the access of the current handle?
33 #ifdef HAVE_SYS_ERRNO_H
34 # include <sys/errno.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
44 #ifdef HAVE_SYS_FILIO_H
45 # include <sys/filio.h>
51 #define WIN32_NO_STATUS
62 /* To avoid conflicts with the Unix socket headers. Plus we only need a few
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 */
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
);
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
);
102 static int sock_get_error( int err
);
103 static void sock_set_error(void);
105 static const struct object_ops sock_ops
=
107 sizeof(struct sock
), /* size */
108 sock_dump
, /* dump */
109 add_queue
, /* add_queue */
110 remove_queue
, /* remove_queue */
111 sock_signaled
, /* signaled */
112 no_satisfied
, /* satisfied */
113 no_signal
, /* signal */
114 sock_get_fd
, /* get_fd */
115 default_fd_map_access
, /* map_access */
116 no_lookup_name
, /* lookup_name */
117 no_open_file
, /* open_file */
118 fd_close_handle
, /* close_handle */
119 sock_destroy
/* destroy */
122 static const struct fd_ops sock_fd_ops
=
124 sock_get_poll_events
, /* get_poll_events */
125 sock_poll_event
, /* poll_event */
126 no_flush
, /* flush */
127 sock_get_fd_type
, /* get_file_info */
128 default_fd_ioctl
, /* ioctl */
129 sock_queue_async
, /* queue_async */
130 sock_reselect_async
, /* reselect_async */
131 sock_cancel_async
/* cancel_async */
135 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
136 * we post messages if there are multiple events. Used to send
137 * messages. The problem is if there is both a FD_CONNECT event and,
138 * say, an FD_READ event available on the same socket, we want to
139 * notify the app of the connect event first. Otherwise it may
140 * discard the read event because it thinks it hasn't connected yet.
142 static const int event_bitorder
[FD_MAX_EVENTS
] =
150 6, 7, 8, 9 /* leftovers */
153 /* Flags that make sense only for SOCK_STREAM sockets */
154 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
157 SOCK_SHUTDOWN_ERROR
= -1,
158 SOCK_SHUTDOWN_EOF
= 0,
159 SOCK_SHUTDOWN_POLLHUP
= 1
162 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
164 static sock_shutdown_t
sock_check_pollhup(void)
166 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
171 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) goto out
;
172 if ( shutdown( fd
[0], 1 ) ) goto out
;
178 n
= poll( &pfd
, 1, 0 );
179 if ( n
!= 1 ) goto out
; /* error or timeout */
180 if ( pfd
.revents
& POLLHUP
)
181 ret
= SOCK_SHUTDOWN_POLLHUP
;
182 else if ( pfd
.revents
& POLLIN
&&
183 read( fd
[1], &dummy
, 1 ) == 0 )
184 ret
= SOCK_SHUTDOWN_EOF
;
194 sock_shutdown_type
= sock_check_pollhup();
196 switch ( sock_shutdown_type
)
198 case SOCK_SHUTDOWN_EOF
:
199 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
201 case SOCK_SHUTDOWN_POLLHUP
:
202 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
205 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
206 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
210 static int sock_reselect( struct sock
*sock
)
212 int ev
= sock_get_poll_events( sock
->fd
);
215 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
217 if (!sock
->polling
) /* FIXME: should find a better way to do this */
219 /* previously unconnected socket, is this reselect supposed to connect it? */
220 if (!(sock
->state
& ~FD_WINE_NONBLOCKING
)) return 0;
221 /* ok, it is, attach it to the wineserver's main poll loop */
224 /* update condition mask */
225 set_fd_events( sock
->fd
, ev
);
229 /* After POLLHUP is received, the socket will no longer be in the main select loop.
230 This function is used to signal pending events nevertheless */
231 static void sock_try_event( struct sock
*sock
, int event
)
233 event
= check_fd_events( sock
->fd
, event
);
236 if ( debug_level
) fprintf( stderr
, "sock_try_event: %x\n", event
);
237 sock_poll_event( sock
->fd
, event
);
241 /* wake anybody waiting on the socket event or send the associated message */
242 static void sock_wake_up( struct sock
*sock
, int pollev
)
244 unsigned int events
= sock
->pmask
& sock
->mask
;
246 int async_active
= 0;
248 if ( pollev
& (POLLIN
|POLLPRI
) && async_waiting( sock
->read_q
))
250 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
251 async_wake_up( sock
->read_q
, STATUS_ALERTED
);
254 if ( pollev
& POLLOUT
&& async_waiting( sock
->write_q
))
256 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
257 async_wake_up( sock
->write_q
, STATUS_ALERTED
);
261 /* Do not signal events if there are still pending asynchronous IO requests */
262 /* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
263 if ( !events
|| async_active
) return;
267 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
268 set_event( sock
->event
);
272 if (debug_level
) fprintf(stderr
, "signalling events %x win %p\n", events
, sock
->window
);
273 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
275 int event
= event_bitorder
[i
];
276 if (sock
->pmask
& (1 << event
))
278 unsigned int lparam
= (1 << event
) | (sock
->errors
[event
] << 16);
279 post_message( sock
->window
, sock
->message
, (unsigned long)sock
->wparam
, lparam
);
283 sock_reselect( sock
);
287 static inline int sock_error( struct fd
*fd
)
289 unsigned int optval
= 0, optlen
;
291 optlen
= sizeof(optval
);
292 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
293 return optval
? sock_get_error(optval
) : 0;
296 static void sock_poll_event( struct fd
*fd
, int event
)
298 struct sock
*sock
= get_fd_user( fd
);
301 assert( sock
->obj
.ops
== &sock_ops
);
303 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
304 if (sock
->state
& FD_CONNECT
)
309 /* we got connected */
310 sock
->state
|= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
311 sock
->state
&= ~FD_CONNECT
;
312 sock
->pmask
|= FD_CONNECT
;
313 sock
->errors
[FD_CONNECT_BIT
] = 0;
315 fprintf(stderr
, "socket %p connection success\n", sock
);
317 else if (event
& (POLLERR
|POLLHUP
))
319 /* we didn't get connected? */
320 sock
->state
&= ~FD_CONNECT
;
321 sock
->pmask
|= FD_CONNECT
;
322 sock
->errors
[FD_CONNECT_BIT
] = sock_error( fd
);
324 fprintf(stderr
, "socket %p connection failure\n", sock
);
327 else if (sock
->state
& FD_WINE_LISTENING
)
332 /* incoming connection */
333 sock
->pmask
|= FD_ACCEPT
;
334 sock
->errors
[FD_ACCEPT_BIT
] = 0;
335 sock
->hmask
|= FD_ACCEPT
;
337 else if (event
& (POLLERR
|POLLHUP
))
339 /* failed incoming connection? */
340 sock
->pmask
|= FD_ACCEPT
;
341 sock
->errors
[FD_ACCEPT_BIT
] = sock_error( fd
);
342 sock
->hmask
|= FD_ACCEPT
;
347 /* normal data flow */
348 if ( sock
->type
== SOCK_STREAM
&& ( event
& POLLIN
) )
353 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
354 * has been closed, so we need to check for it explicitly here */
355 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
359 sock
->pmask
|= FD_READ
;
360 sock
->hmask
|= (FD_READ
|FD_CLOSE
);
361 sock
->errors
[FD_READ_BIT
] = 0;
363 fprintf(stderr
, "socket %p is readable\n", sock
);
369 /* EAGAIN can happen if an async recv() falls between the server's poll()
370 call and the invocation of this routine */
371 if ( errno
== EAGAIN
)
376 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
382 else if ( sock_shutdown_type
== SOCK_SHUTDOWN_POLLHUP
&& (event
& POLLHUP
) )
386 else if ( event
& POLLIN
) /* POLLIN for non-stream socket */
388 sock
->pmask
|= FD_READ
;
389 sock
->hmask
|= (FD_READ
|FD_CLOSE
);
390 sock
->errors
[FD_READ_BIT
] = 0;
392 fprintf(stderr
, "socket %p is readable\n", sock
);
398 sock
->pmask
|= FD_WRITE
;
399 sock
->hmask
|= FD_WRITE
;
400 sock
->errors
[FD_WRITE_BIT
] = 0;
402 fprintf(stderr
, "socket %p is writable\n", sock
);
406 sock
->pmask
|= FD_OOB
;
407 sock
->hmask
|= FD_OOB
;
408 sock
->errors
[FD_OOB_BIT
] = 0;
410 fprintf(stderr
, "socket %p got OOB data\n", sock
);
412 /* According to WS2 specs, FD_CLOSE is only delivered when there is
413 no more data to be read (i.e. hangup_seen = 1) */
414 else if ( hangup_seen
&& (sock
->state
& (FD_READ
|FD_WRITE
) ))
416 sock
->errors
[FD_CLOSE_BIT
] = sock_error( fd
);
417 if ( (event
& POLLERR
) || ( sock_shutdown_type
== SOCK_SHUTDOWN_EOF
&& (event
& POLLHUP
) ))
418 sock
->state
&= ~FD_WRITE
;
419 sock
->pmask
|= FD_CLOSE
;
420 sock
->hmask
|= FD_CLOSE
;
422 fprintf(stderr
, "socket %p aborted by error %d, event: %x - removing from select loop\n",
423 sock
, sock
->errors
[FD_CLOSE_BIT
], event
);
427 if ( sock
->pmask
& FD_CLOSE
|| event
& (POLLERR
|POLLHUP
) )
430 fprintf( stderr
, "removing socket %p from select loop\n", sock
);
431 set_fd_events( sock
->fd
, -1 );
434 sock_reselect( sock
);
436 /* wake up anyone waiting for whatever just happened */
437 if ( sock
->pmask
& sock
->mask
|| sock
->flags
& WSA_FLAG_OVERLAPPED
) sock_wake_up( sock
, event
);
439 /* if anyone is stupid enough to wait on the socket object itself,
440 * maybe we should wake them up too, just in case? */
441 wake_up( &sock
->obj
, 0 );
444 static void sock_dump( struct object
*obj
, int verbose
)
446 struct sock
*sock
= (struct sock
*)obj
;
447 assert( obj
->ops
== &sock_ops
);
448 printf( "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
449 sock
->fd
, sock
->state
,
450 sock
->mask
, sock
->pmask
, sock
->hmask
);
453 static int sock_signaled( struct object
*obj
, struct thread
*thread
)
455 struct sock
*sock
= (struct sock
*)obj
;
456 assert( obj
->ops
== &sock_ops
);
458 return check_fd_events( sock
->fd
, sock_get_poll_events( sock
->fd
) ) != 0;
461 static int sock_get_poll_events( struct fd
*fd
)
463 struct sock
*sock
= get_fd_user( fd
);
464 unsigned int mask
= sock
->mask
& sock
->state
& ~sock
->hmask
;
467 assert( sock
->obj
.ops
== &sock_ops
);
469 if (sock
->state
& FD_CONNECT
)
470 /* connecting, wait for writable */
472 if (sock
->state
& FD_WINE_LISTENING
)
473 /* listening, wait for readable */
474 return (sock
->hmask
& FD_ACCEPT
) ? 0 : POLLIN
;
476 if (mask
& FD_READ
|| async_waiting( sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
477 if (mask
& FD_WRITE
|| async_waiting( sock
->write_q
)) ev
|= POLLOUT
;
478 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
479 if ( sock
->type
== SOCK_STREAM
&& ( sock
->mask
& ~sock
->hmask
& FD_CLOSE
) )
485 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
487 return FD_TYPE_SOCKET
;
490 static void sock_queue_async( struct fd
*fd
, const async_data_t
*data
, int type
, int count
)
492 struct sock
*sock
= get_fd_user( fd
);
493 struct async_queue
*queue
;
496 assert( sock
->obj
.ops
== &sock_ops
);
500 case ASYNC_TYPE_READ
:
501 if (!sock
->read_q
&& !(sock
->read_q
= create_async_queue( sock
->fd
))) return;
502 queue
= sock
->read_q
;
503 sock
->hmask
&= ~FD_CLOSE
;
505 case ASYNC_TYPE_WRITE
:
506 if (!sock
->write_q
&& !(sock
->write_q
= create_async_queue( sock
->fd
))) return;
507 queue
= sock
->write_q
;
510 set_error( STATUS_INVALID_PARAMETER
);
514 if ( ( !( sock
->state
& FD_READ
) && type
== ASYNC_TYPE_READ
) ||
515 ( !( sock
->state
& FD_WRITE
) && type
== ASYNC_TYPE_WRITE
) )
517 set_error( STATUS_PIPE_DISCONNECTED
);
522 if (!(async
= create_async( current
, queue
, data
))) return;
523 release_object( async
);
524 set_error( STATUS_PENDING
);
527 pollev
= sock_reselect( sock
);
528 if ( pollev
) sock_try_event( sock
, pollev
);
531 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
533 struct sock
*sock
= get_fd_user( fd
);
534 int events
= sock_reselect( sock
);
535 if (events
) sock_try_event( sock
, events
);
538 static void sock_cancel_async( struct fd
*fd
)
540 struct sock
*sock
= get_fd_user( fd
);
541 assert( sock
->obj
.ops
== &sock_ops
);
543 async_wake_up( sock
->read_q
, STATUS_CANCELLED
);
544 async_wake_up( sock
->write_q
, STATUS_CANCELLED
);
547 static struct fd
*sock_get_fd( struct object
*obj
)
549 struct sock
*sock
= (struct sock
*)obj
;
550 return (struct fd
*)grab_object( sock
->fd
);
553 static void sock_destroy( struct object
*obj
)
555 struct sock
*sock
= (struct sock
*)obj
;
556 assert( obj
->ops
== &sock_ops
);
558 /* FIXME: special socket shutdown stuff? */
560 if ( sock
->deferred
)
561 release_object( sock
->deferred
);
563 free_async_queue( sock
->read_q
);
564 free_async_queue( sock
->write_q
);
565 if (sock
->event
) release_object( sock
->event
);
568 /* shut the socket down to force pending poll() calls in the client to return */
569 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
570 release_object( sock
->fd
);
574 /* create a new and unconnected socket */
575 static struct object
*create_socket( int family
, int type
, int protocol
, unsigned int flags
)
580 sockfd
= socket( family
, type
, protocol
);
582 fprintf(stderr
,"socket(%d,%d,%d)=%d\n",family
,type
,protocol
,sockfd
);
588 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
589 if (!(sock
= alloc_object( &sock_ops
)))
594 sock
->state
= (type
!= SOCK_STREAM
) ? (FD_READ
|FD_WRITE
) : 0;
601 sock
->family
= family
;
606 sock
->deferred
= NULL
;
608 sock
->write_q
= NULL
;
609 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
,
610 (flags
& WSA_FLAG_OVERLAPPED
) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT
)))
612 release_object( sock
);
615 sock_reselect( sock
);
620 /* accept a socket (creates a new fd) */
621 static struct sock
*accept_socket( obj_handle_t handle
)
623 struct sock
*acceptsock
;
626 struct sockaddr saddr
;
628 sock
= (struct sock
*)get_handle_obj( current
->process
, handle
, FILE_READ_DATA
, &sock_ops
);
632 if ( sock
->deferred
)
634 acceptsock
= sock
->deferred
;
635 sock
->deferred
= NULL
;
640 /* Try to accept(2). We can't be safe that this an already connected socket
641 * or that accept() is allowed on it. In those cases we will get -1/errno
644 unsigned int slen
= sizeof(saddr
);
645 acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
649 release_object( sock
);
652 if (!(acceptsock
= alloc_object( &sock_ops
)))
655 release_object( sock
);
659 /* newly created socket gets the same properties of the listening socket */
660 fcntl(acceptfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
661 acceptsock
->state
= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
662 if (sock
->state
& FD_WINE_NONBLOCKING
)
663 acceptsock
->state
|= FD_WINE_NONBLOCKING
;
664 acceptsock
->mask
= sock
->mask
;
665 acceptsock
->hmask
= 0;
666 acceptsock
->pmask
= 0;
667 acceptsock
->polling
= 0;
668 acceptsock
->type
= sock
->type
;
669 acceptsock
->family
= sock
->family
;
670 acceptsock
->event
= NULL
;
671 acceptsock
->window
= sock
->window
;
672 acceptsock
->message
= sock
->message
;
673 acceptsock
->wparam
= 0;
674 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
675 acceptsock
->flags
= sock
->flags
;
676 acceptsock
->deferred
= NULL
;
677 acceptsock
->read_q
= NULL
;
678 acceptsock
->write_q
= NULL
;
679 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
680 get_fd_options( sock
->fd
) )))
682 release_object( acceptsock
);
683 release_object( sock
);
688 sock
->pmask
&= ~FD_ACCEPT
;
689 sock
->hmask
&= ~FD_ACCEPT
;
690 sock_reselect( sock
);
691 release_object( sock
);
695 /* set the last error depending on errno */
696 static int sock_get_error( int err
)
700 case EINTR
: return WSAEINTR
;
701 case EBADF
: return WSAEBADF
;
703 case EACCES
: return WSAEACCES
;
704 case EFAULT
: return WSAEFAULT
;
705 case EINVAL
: return WSAEINVAL
;
706 case EMFILE
: return WSAEMFILE
;
707 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
708 case EINPROGRESS
: return WSAEINPROGRESS
;
709 case EALREADY
: return WSAEALREADY
;
710 case ENOTSOCK
: return WSAENOTSOCK
;
711 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
712 case EMSGSIZE
: return WSAEMSGSIZE
;
713 case EPROTOTYPE
: return WSAEPROTOTYPE
;
714 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
715 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
716 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
717 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
718 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
719 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
720 case EADDRINUSE
: return WSAEADDRINUSE
;
721 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
722 case ENETDOWN
: return WSAENETDOWN
;
723 case ENETUNREACH
: return WSAENETUNREACH
;
724 case ENETRESET
: return WSAENETRESET
;
725 case ECONNABORTED
: return WSAECONNABORTED
;
727 case ECONNRESET
: return WSAECONNRESET
;
728 case ENOBUFS
: return WSAENOBUFS
;
729 case EISCONN
: return WSAEISCONN
;
730 case ENOTCONN
: return WSAENOTCONN
;
731 case ESHUTDOWN
: return WSAESHUTDOWN
;
732 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
733 case ETIMEDOUT
: return WSAETIMEDOUT
;
734 case ECONNREFUSED
: return WSAECONNREFUSED
;
735 case ELOOP
: return WSAELOOP
;
736 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
737 case EHOSTDOWN
: return WSAEHOSTDOWN
;
738 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
739 case ENOTEMPTY
: return WSAENOTEMPTY
;
741 case EPROCLIM
: return WSAEPROCLIM
;
744 case EUSERS
: return WSAEUSERS
;
747 case EDQUOT
: return WSAEDQUOT
;
750 case ESTALE
: return WSAESTALE
;
753 case EREMOTE
: return WSAEREMOTE
;
757 perror("wineserver: sock_get_error() can't map error");
762 /* set the last error depending on errno */
763 static void sock_set_error(void)
765 set_error( sock_get_error( errno
) );
768 /* create a socket */
769 DECL_HANDLER(create_socket
)
774 if ((obj
= create_socket( req
->family
, req
->type
, req
->protocol
, req
->flags
)) != NULL
)
776 reply
->handle
= alloc_handle( current
->process
, obj
, req
->access
, req
->attributes
);
777 release_object( obj
);
781 /* accept a socket */
782 DECL_HANDLER(accept_socket
)
787 if ((sock
= accept_socket( req
->lhandle
)) != NULL
)
789 reply
->handle
= alloc_handle( current
->process
, &sock
->obj
, req
->access
, req
->attributes
);
790 sock
->wparam
= reply
->handle
; /* wparam for message is the socket handle */
791 sock_reselect( sock
);
792 release_object( &sock
->obj
);
796 /* set socket event parameters */
797 DECL_HANDLER(set_socket_event
)
800 struct event
*old_event
;
803 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
804 FILE_WRITE_ATTRIBUTES
, &sock_ops
))) return;
805 old_event
= sock
->event
;
806 sock
->mask
= req
->mask
;
807 sock
->hmask
&= ~req
->mask
; /* re-enable held events */
809 sock
->window
= req
->window
;
810 sock
->message
= req
->msg
;
811 sock
->wparam
= req
->handle
; /* wparam is the socket handle */
812 if (req
->event
) sock
->event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
814 if (debug_level
&& sock
->event
) fprintf(stderr
, "event ptr: %p\n", sock
->event
);
816 pollev
= sock_reselect( sock
);
817 if ( pollev
) sock_try_event( sock
, pollev
);
820 sock
->state
|= FD_WINE_NONBLOCKING
;
822 /* if a network event is pending, signal the event object
823 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
824 before a WSAEventSelect() was done on it.
825 (when dealing with Asynchronous socket) */
826 if (sock
->pmask
& sock
->mask
) sock_wake_up( sock
, pollev
);
828 if (old_event
) release_object( old_event
); /* we're through with it */
829 release_object( &sock
->obj
);
832 /* get socket event parameters */
833 DECL_HANDLER(get_socket_event
)
837 sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
, &sock_ops
);
843 set_error( WSAENOTSOCK
);
846 reply
->mask
= sock
->mask
;
847 reply
->pmask
= sock
->pmask
;
848 reply
->state
= sock
->state
;
849 set_reply_data( sock
->errors
, min( get_reply_max_size(), sizeof(sock
->errors
) ));
855 struct event
*cevent
= get_event_obj( current
->process
, req
->c_event
,
856 EVENT_MODIFY_STATE
);
859 reset_event( cevent
);
860 release_object( cevent
);
864 sock_reselect( sock
);
866 release_object( &sock
->obj
);
869 /* re-enable pending socket events */
870 DECL_HANDLER(enable_socket_event
)
875 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
876 FILE_WRITE_ATTRIBUTES
, &sock_ops
)))
879 sock
->pmask
&= ~req
->mask
; /* is this safe? */
880 sock
->hmask
&= ~req
->mask
;
881 if ( req
->mask
& FD_READ
)
882 sock
->hmask
&= ~FD_CLOSE
;
883 sock
->state
|= req
->sstate
;
884 sock
->state
&= ~req
->cstate
;
885 if ( sock
->type
!= SOCK_STREAM
) sock
->state
&= ~STREAM_FLAG_MASK
;
887 pollev
= sock_reselect( sock
);
888 if ( pollev
) sock_try_event( sock
, pollev
);
890 release_object( &sock
->obj
);
893 DECL_HANDLER(set_socket_deferred
)
895 struct sock
*sock
, *acceptsock
;
897 sock
=(struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
, &sock_ops
);
900 set_error( WSAENOTSOCK
);
903 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, req
->deferred
, 0, &sock_ops
);
906 release_object( sock
);
907 set_error( WSAENOTSOCK
);
910 sock
->deferred
= acceptsock
;
911 release_object( sock
);