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
64 #define FD_MAX_EVENTS 10
66 #define FD_WRITE_BIT 1
68 #define FD_ACCEPT_BIT 3
69 #define FD_CONNECT_BIT 4
70 #define FD_CLOSE_BIT 5
73 * Define flags to be used with the WSAAsyncSelect() call.
75 #define FD_READ 0x00000001
76 #define FD_WRITE 0x00000002
77 #define FD_OOB 0x00000004
78 #define FD_ACCEPT 0x00000008
79 #define FD_CONNECT 0x00000010
80 #define FD_CLOSE 0x00000020
82 /* internal per-socket flags */
83 #define FD_WINE_LISTENING 0x10000000
84 #define FD_WINE_NONBLOCKING 0x20000000
85 #define FD_WINE_CONNECTED 0x40000000
86 #define FD_WINE_RAW 0x80000000
87 #define FD_WINE_INTERNAL 0xFFFF0000
89 /* Constants for WSAIoctl() */
90 #define WSA_FLAG_OVERLAPPED 0x01
94 struct object obj
; /* object header */
95 struct fd
*fd
; /* socket file descriptor */
96 unsigned int state
; /* status bits */
97 unsigned int mask
; /* event mask */
98 unsigned int hmask
; /* held (blocked) events */
99 unsigned int pmask
; /* pending events */
100 unsigned int flags
; /* socket flags */
101 int polling
; /* is socket being polled? */
102 unsigned short type
; /* socket type */
103 unsigned short family
; /* socket family */
104 struct event
*event
; /* event object */
105 user_handle_t window
; /* window to send the message to */
106 unsigned int message
; /* message to send */
107 obj_handle_t wparam
; /* message wparam (socket handle) */
108 int errors
[FD_MAX_EVENTS
]; /* event errors */
109 struct sock
*deferred
; /* socket that waits for a deferred accept */
110 struct async_queue
*read_q
; /* queue for asynchronous reads */
111 struct async_queue
*write_q
; /* queue for asynchronous writes */
114 static void sock_dump( struct object
*obj
, int verbose
);
115 static int sock_signaled( struct object
*obj
, struct thread
*thread
);
116 static struct fd
*sock_get_fd( struct object
*obj
);
117 static void sock_destroy( struct object
*obj
);
119 static int sock_get_poll_events( struct fd
*fd
);
120 static void sock_poll_event( struct fd
*fd
, int event
);
121 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
122 static void sock_queue_async( struct fd
*fd
, const async_data_t
*data
, int type
, int count
);
123 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
124 static void sock_cancel_async( struct fd
*fd
, struct process
*process
, struct thread
*thread
, client_ptr_t iosb
);
126 static int sock_get_ntstatus( int err
);
127 static int sock_get_error( int err
);
128 static void sock_set_error(void);
130 static const struct object_ops sock_ops
=
132 sizeof(struct sock
), /* size */
133 sock_dump
, /* dump */
134 no_get_type
, /* get_type */
135 add_queue
, /* add_queue */
136 remove_queue
, /* remove_queue */
137 sock_signaled
, /* signaled */
138 no_satisfied
, /* satisfied */
139 no_signal
, /* signal */
140 sock_get_fd
, /* get_fd */
141 default_fd_map_access
, /* map_access */
142 default_get_sd
, /* get_sd */
143 default_set_sd
, /* set_sd */
144 no_lookup_name
, /* lookup_name */
145 no_open_file
, /* open_file */
146 fd_close_handle
, /* close_handle */
147 sock_destroy
/* destroy */
150 static const struct fd_ops sock_fd_ops
=
152 sock_get_poll_events
, /* get_poll_events */
153 sock_poll_event
, /* poll_event */
154 no_flush
, /* flush */
155 sock_get_fd_type
, /* get_file_info */
156 default_fd_ioctl
, /* ioctl */
157 sock_queue_async
, /* queue_async */
158 sock_reselect_async
, /* reselect_async */
159 sock_cancel_async
/* cancel_async */
163 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
164 * we post messages if there are multiple events. Used to send
165 * messages. The problem is if there is both a FD_CONNECT event and,
166 * say, an FD_READ event available on the same socket, we want to
167 * notify the app of the connect event first. Otherwise it may
168 * discard the read event because it thinks it hasn't connected yet.
170 static const int event_bitorder
[FD_MAX_EVENTS
] =
178 6, 7, 8, 9 /* leftovers */
181 /* Flags that make sense only for SOCK_STREAM sockets */
182 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
185 SOCK_SHUTDOWN_ERROR
= -1,
186 SOCK_SHUTDOWN_EOF
= 0,
187 SOCK_SHUTDOWN_POLLHUP
= 1
190 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
192 static sock_shutdown_t
sock_check_pollhup(void)
194 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
199 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) goto out
;
200 if ( shutdown( fd
[0], 1 ) ) goto out
;
206 n
= poll( &pfd
, 1, 0 );
207 if ( n
!= 1 ) goto out
; /* error or timeout */
208 if ( pfd
.revents
& POLLHUP
)
209 ret
= SOCK_SHUTDOWN_POLLHUP
;
210 else if ( pfd
.revents
& POLLIN
&&
211 read( fd
[1], &dummy
, 1 ) == 0 )
212 ret
= SOCK_SHUTDOWN_EOF
;
222 sock_shutdown_type
= sock_check_pollhup();
224 switch ( sock_shutdown_type
)
226 case SOCK_SHUTDOWN_EOF
:
227 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
229 case SOCK_SHUTDOWN_POLLHUP
:
230 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
233 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
234 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
238 static int sock_reselect( struct sock
*sock
)
240 int ev
= sock_get_poll_events( sock
->fd
);
243 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
245 if (!sock
->polling
) /* FIXME: should find a better way to do this */
247 /* previously unconnected socket, is this reselect supposed to connect it? */
248 if (!(sock
->state
& ~FD_WINE_NONBLOCKING
)) return 0;
249 /* ok, it is, attach it to the wineserver's main poll loop */
252 /* update condition mask */
253 set_fd_events( sock
->fd
, ev
);
257 /* wake anybody waiting on the socket event or send the associated message */
258 static void sock_wake_up( struct sock
*sock
, int pollev
)
260 unsigned int events
= sock
->pmask
& sock
->mask
;
262 int async_active
= 0;
264 if ( pollev
& (POLLIN
|POLLPRI
|POLLERR
|POLLHUP
) && async_waiting( sock
->read_q
))
266 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
267 async_wake_up( sock
->read_q
, STATUS_ALERTED
);
270 if ( pollev
& (POLLOUT
|POLLERR
|POLLHUP
) && async_waiting( sock
->write_q
))
272 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
273 async_wake_up( sock
->write_q
, STATUS_ALERTED
);
277 /* Do not signal events if there are still pending asynchronous IO requests */
278 /* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
279 if ( !events
|| async_active
) return;
283 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
284 set_event( sock
->event
);
288 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
289 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
291 int event
= event_bitorder
[i
];
292 if (sock
->pmask
& (1 << event
))
294 lparam_t lparam
= (1 << event
) | (sock_get_error(sock
->errors
[event
]) << 16);
295 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
299 sock_reselect( sock
);
303 static inline int sock_error( struct fd
*fd
)
305 unsigned int optval
= 0, optlen
;
307 optlen
= sizeof(optval
);
308 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
312 static void sock_poll_event( struct fd
*fd
, int event
)
314 struct sock
*sock
= get_fd_user( fd
);
317 assert( sock
->obj
.ops
== &sock_ops
);
319 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
321 /* we may change event later, remove from loop here */
322 if (event
& (POLLERR
|POLLHUP
)) set_fd_events( sock
->fd
, -1 );
324 if (sock
->state
& FD_CONNECT
)
329 /* we got connected */
330 sock
->state
|= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
331 sock
->state
&= ~FD_CONNECT
;
332 sock
->pmask
|= FD_CONNECT
;
333 sock
->errors
[FD_CONNECT_BIT
] = 0;
335 fprintf(stderr
, "socket %p connection success\n", sock
);
337 else if (event
& (POLLERR
|POLLHUP
))
339 /* we didn't get connected? */
340 sock
->state
&= ~FD_CONNECT
;
341 sock
->pmask
|= FD_CONNECT
;
342 sock
->errors
[FD_CONNECT_BIT
] = sock_error( fd
);
344 fprintf(stderr
, "socket %p connection failure\n", sock
);
347 else if (sock
->state
& FD_WINE_LISTENING
)
352 /* incoming connection */
353 sock
->pmask
|= FD_ACCEPT
;
354 sock
->errors
[FD_ACCEPT_BIT
] = 0;
355 sock
->hmask
|= FD_ACCEPT
;
357 else if (event
& (POLLERR
|POLLHUP
))
359 /* failed incoming connection? */
360 sock
->pmask
|= FD_ACCEPT
;
361 sock
->errors
[FD_ACCEPT_BIT
] = sock_error( fd
);
362 sock
->hmask
|= FD_ACCEPT
;
367 /* normal data flow */
368 if ( sock
->type
== SOCK_STREAM
&& ( event
& POLLIN
) )
373 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
374 * has been closed, so we need to check for it explicitly here */
375 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
384 /* EAGAIN can happen if an async recv() falls between the server's poll()
385 call and the invocation of this routine */
386 if ( errno
!= EAGAIN
)
389 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
395 if ( event
& POLLIN
)
397 sock
->pmask
|= FD_READ
;
398 sock
->hmask
|= FD_READ
;
399 sock
->errors
[FD_READ_BIT
] = 0;
401 fprintf(stderr
, "socket %p is readable\n", sock
);
406 sock
->pmask
|= FD_WRITE
;
407 sock
->hmask
|= FD_WRITE
;
408 sock
->errors
[FD_WRITE_BIT
] = 0;
410 fprintf(stderr
, "socket %p is writable\n", sock
);
414 sock
->pmask
|= FD_OOB
;
415 sock
->hmask
|= FD_OOB
;
416 sock
->errors
[FD_OOB_BIT
] = 0;
418 fprintf(stderr
, "socket %p got OOB data\n", sock
);
421 if ( (hangup_seen
|| event
& (POLLHUP
|POLLERR
)) && (sock
->state
& (FD_READ
|FD_WRITE
)) )
423 sock
->errors
[FD_CLOSE_BIT
] = sock_error( fd
);
424 if ( (event
& POLLERR
) || ( sock_shutdown_type
== SOCK_SHUTDOWN_EOF
&& (event
& POLLHUP
) ))
425 sock
->state
&= ~FD_WRITE
;
426 sock
->state
&= ~FD_READ
;
428 sock
->pmask
|= FD_CLOSE
;
429 sock
->hmask
|= FD_CLOSE
;
431 fprintf(stderr
, "socket %p aborted by error %d, event: %x - removing from select loop\n",
432 sock
, sock
->errors
[FD_CLOSE_BIT
], event
);
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
);
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 );
446 sock_reselect( sock
);
449 static void sock_dump( struct object
*obj
, int verbose
)
451 struct sock
*sock
= (struct sock
*)obj
;
452 assert( obj
->ops
== &sock_ops
);
453 printf( "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
454 sock
->fd
, sock
->state
,
455 sock
->mask
, sock
->pmask
, sock
->hmask
);
458 static int sock_signaled( struct object
*obj
, struct thread
*thread
)
460 struct sock
*sock
= (struct sock
*)obj
;
461 assert( obj
->ops
== &sock_ops
);
463 return check_fd_events( sock
->fd
, sock_get_poll_events( sock
->fd
) ) != 0;
466 static int sock_get_poll_events( struct fd
*fd
)
468 struct sock
*sock
= get_fd_user( fd
);
469 unsigned int mask
= sock
->mask
& sock
->state
& ~sock
->hmask
;
472 assert( sock
->obj
.ops
== &sock_ops
);
474 if (sock
->state
& FD_CONNECT
)
475 /* connecting, wait for writable */
477 if (sock
->state
& FD_WINE_LISTENING
)
478 /* listening, wait for readable */
479 return (sock
->hmask
& FD_ACCEPT
) ? 0 : POLLIN
;
481 if (mask
& FD_READ
|| async_waiting( sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
482 if (mask
& FD_WRITE
|| async_waiting( sock
->write_q
)) ev
|= POLLOUT
;
483 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
484 if ( sock
->type
== SOCK_STREAM
&& ( sock
->mask
& ~sock
->hmask
& FD_CLOSE
) &&
485 !(sock
->hmask
& FD_READ
) && sock
->state
& FD_READ
)
491 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
493 return FD_TYPE_SOCKET
;
496 static void sock_queue_async( struct fd
*fd
, const async_data_t
*data
, int type
, int count
)
498 struct sock
*sock
= get_fd_user( fd
);
499 struct async_queue
*queue
;
501 assert( sock
->obj
.ops
== &sock_ops
);
505 case ASYNC_TYPE_READ
:
506 if (!sock
->read_q
&& !(sock
->read_q
= create_async_queue( sock
->fd
))) return;
507 queue
= sock
->read_q
;
509 case ASYNC_TYPE_WRITE
:
510 if (!sock
->write_q
&& !(sock
->write_q
= create_async_queue( sock
->fd
))) return;
511 queue
= sock
->write_q
;
514 set_error( STATUS_INVALID_PARAMETER
);
518 if ( ( !( sock
->state
& FD_READ
) && type
== ASYNC_TYPE_READ
) ||
519 ( !( sock
->state
& FD_WRITE
) && type
== ASYNC_TYPE_WRITE
) )
521 set_error( STATUS_PIPE_DISCONNECTED
);
526 if (!(async
= create_async( current
, queue
, data
))) return;
527 release_object( async
);
528 set_error( STATUS_PENDING
);
531 sock_reselect( sock
);
534 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
536 struct sock
*sock
= get_fd_user( fd
);
537 sock_reselect( sock
);
540 static void sock_cancel_async( struct fd
*fd
, struct process
*process
, struct thread
*thread
, client_ptr_t iosb
)
542 struct sock
*sock
= get_fd_user( fd
);
544 assert( sock
->obj
.ops
== &sock_ops
);
546 n
+= async_wake_up_by( sock
->read_q
, process
, thread
, iosb
, STATUS_CANCELLED
);
547 n
+= async_wake_up_by( sock
->write_q
, process
, thread
, iosb
, STATUS_CANCELLED
);
549 set_error( STATUS_NOT_FOUND
);
552 static struct fd
*sock_get_fd( struct object
*obj
)
554 struct sock
*sock
= (struct sock
*)obj
;
555 return (struct fd
*)grab_object( sock
->fd
);
558 static void sock_destroy( struct object
*obj
)
560 struct sock
*sock
= (struct sock
*)obj
;
561 assert( obj
->ops
== &sock_ops
);
563 /* FIXME: special socket shutdown stuff? */
565 if ( sock
->deferred
)
566 release_object( sock
->deferred
);
568 free_async_queue( sock
->read_q
);
569 free_async_queue( sock
->write_q
);
570 if (sock
->event
) release_object( sock
->event
);
573 /* shut the socket down to force pending poll() calls in the client to return */
574 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
575 release_object( sock
->fd
);
579 /* create a new and unconnected socket */
580 static struct object
*create_socket( int family
, int type
, int protocol
, unsigned int flags
)
585 sockfd
= socket( family
, type
, protocol
);
587 fprintf(stderr
,"socket(%d,%d,%d)=%d\n",family
,type
,protocol
,sockfd
);
593 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
594 if (!(sock
= alloc_object( &sock_ops
)))
599 sock
->state
= (type
!= SOCK_STREAM
) ? (FD_READ
|FD_WRITE
) : 0;
606 sock
->family
= family
;
611 sock
->deferred
= NULL
;
613 sock
->write_q
= NULL
;
614 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
615 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
,
616 (flags
& WSA_FLAG_OVERLAPPED
) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT
)))
618 release_object( sock
);
621 sock_reselect( sock
);
626 /* accept a socket (creates a new fd) */
627 static struct sock
*accept_socket( obj_handle_t handle
)
629 struct sock
*acceptsock
;
632 struct sockaddr saddr
;
634 sock
= (struct sock
*)get_handle_obj( current
->process
, handle
, FILE_READ_DATA
, &sock_ops
);
638 if ( sock
->deferred
)
640 acceptsock
= sock
->deferred
;
641 sock
->deferred
= NULL
;
646 /* Try to accept(2). We can't be safe that this an already connected socket
647 * or that accept() is allowed on it. In those cases we will get -1/errno
650 unsigned int slen
= sizeof(saddr
);
651 acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
655 release_object( sock
);
658 if (!(acceptsock
= alloc_object( &sock_ops
)))
661 release_object( sock
);
665 /* newly created socket gets the same properties of the listening socket */
666 fcntl(acceptfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
667 acceptsock
->state
= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
668 if (sock
->state
& FD_WINE_NONBLOCKING
)
669 acceptsock
->state
|= FD_WINE_NONBLOCKING
;
670 acceptsock
->mask
= sock
->mask
;
671 acceptsock
->hmask
= 0;
672 acceptsock
->pmask
= 0;
673 acceptsock
->polling
= 0;
674 acceptsock
->type
= sock
->type
;
675 acceptsock
->family
= sock
->family
;
676 acceptsock
->event
= NULL
;
677 acceptsock
->window
= sock
->window
;
678 acceptsock
->message
= sock
->message
;
679 acceptsock
->wparam
= 0;
680 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
681 acceptsock
->flags
= sock
->flags
;
682 acceptsock
->deferred
= NULL
;
683 acceptsock
->read_q
= NULL
;
684 acceptsock
->write_q
= NULL
;
685 memset( acceptsock
->errors
, 0, sizeof(acceptsock
->errors
) );
686 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
687 get_fd_options( sock
->fd
) )))
689 release_object( acceptsock
);
690 release_object( sock
);
695 sock
->pmask
&= ~FD_ACCEPT
;
696 sock
->hmask
&= ~FD_ACCEPT
;
697 sock_reselect( sock
);
698 release_object( sock
);
702 /* set the last error depending on errno */
703 static int sock_get_error( int err
)
707 case EINTR
: return WSAEINTR
;
708 case EBADF
: return WSAEBADF
;
710 case EACCES
: return WSAEACCES
;
711 case EFAULT
: return WSAEFAULT
;
712 case EINVAL
: return WSAEINVAL
;
713 case EMFILE
: return WSAEMFILE
;
714 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
715 case EINPROGRESS
: return WSAEINPROGRESS
;
716 case EALREADY
: return WSAEALREADY
;
717 case ENOTSOCK
: return WSAENOTSOCK
;
718 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
719 case EMSGSIZE
: return WSAEMSGSIZE
;
720 case EPROTOTYPE
: return WSAEPROTOTYPE
;
721 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
722 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
723 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
724 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
725 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
726 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
727 case EADDRINUSE
: return WSAEADDRINUSE
;
728 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
729 case ENETDOWN
: return WSAENETDOWN
;
730 case ENETUNREACH
: return WSAENETUNREACH
;
731 case ENETRESET
: return WSAENETRESET
;
732 case ECONNABORTED
: return WSAECONNABORTED
;
734 case ECONNRESET
: return WSAECONNRESET
;
735 case ENOBUFS
: return WSAENOBUFS
;
736 case EISCONN
: return WSAEISCONN
;
737 case ENOTCONN
: return WSAENOTCONN
;
738 case ESHUTDOWN
: return WSAESHUTDOWN
;
739 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
740 case ETIMEDOUT
: return WSAETIMEDOUT
;
741 case ECONNREFUSED
: return WSAECONNREFUSED
;
742 case ELOOP
: return WSAELOOP
;
743 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
744 case EHOSTDOWN
: return WSAEHOSTDOWN
;
745 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
746 case ENOTEMPTY
: return WSAENOTEMPTY
;
748 case EPROCLIM
: return WSAEPROCLIM
;
751 case EUSERS
: return WSAEUSERS
;
754 case EDQUOT
: return WSAEDQUOT
;
757 case ESTALE
: return WSAESTALE
;
760 case EREMOTE
: return WSAEREMOTE
;
766 perror("wineserver: sock_get_error() can't map error");
771 static int sock_get_ntstatus( int err
)
775 case EBADF
: return STATUS_INVALID_HANDLE
;
776 case EBUSY
: return STATUS_DEVICE_BUSY
;
778 case EACCES
: return STATUS_ACCESS_DENIED
;
779 case EFAULT
: return STATUS_NO_MEMORY
;
780 case EINVAL
: return STATUS_INVALID_PARAMETER
;
782 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
783 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
784 case EINPROGRESS
: return STATUS_PENDING
;
785 case EALREADY
: return STATUS_NETWORK_BUSY
;
786 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
787 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
788 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
789 case EPROTONOSUPPORT
:
790 case ESOCKTNOSUPPORT
:
793 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
794 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
795 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
796 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
797 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
798 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
799 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
800 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
801 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
802 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
803 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
805 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
806 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
808 case 0: return STATUS_SUCCESS
;
811 perror("wineserver: sock_get_ntstatus() can't map error");
812 return STATUS_UNSUCCESSFUL
;
816 /* set the last error depending on errno */
817 static void sock_set_error(void)
819 set_error( sock_get_ntstatus( errno
) );
822 /* create a socket */
823 DECL_HANDLER(create_socket
)
828 if ((obj
= create_socket( req
->family
, req
->type
, req
->protocol
, req
->flags
)) != NULL
)
830 reply
->handle
= alloc_handle( current
->process
, obj
, req
->access
, req
->attributes
);
831 release_object( obj
);
835 /* accept a socket */
836 DECL_HANDLER(accept_socket
)
841 if ((sock
= accept_socket( req
->lhandle
)) != NULL
)
843 reply
->handle
= alloc_handle( current
->process
, &sock
->obj
, req
->access
, req
->attributes
);
844 sock
->wparam
= reply
->handle
; /* wparam for message is the socket handle */
845 sock_reselect( sock
);
846 release_object( &sock
->obj
);
850 /* set socket event parameters */
851 DECL_HANDLER(set_socket_event
)
854 struct event
*old_event
;
857 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
858 FILE_WRITE_ATTRIBUTES
, &sock_ops
))) return;
859 old_event
= sock
->event
;
860 sock
->mask
= req
->mask
;
861 sock
->hmask
&= ~req
->mask
; /* re-enable held events */
863 sock
->window
= req
->window
;
864 sock
->message
= req
->msg
;
865 sock
->wparam
= req
->handle
; /* wparam is the socket handle */
866 if (req
->event
) sock
->event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
868 if (debug_level
&& sock
->event
) fprintf(stderr
, "event ptr: %p\n", sock
->event
);
870 pollev
= sock_reselect( sock
);
873 sock
->state
|= FD_WINE_NONBLOCKING
;
875 /* if a network event is pending, signal the event object
876 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
877 before a WSAEventSelect() was done on it.
878 (when dealing with Asynchronous socket) */
879 if (sock
->pmask
& sock
->mask
) sock_wake_up( sock
, pollev
);
881 if (old_event
) release_object( old_event
); /* we're through with it */
882 release_object( &sock
->obj
);
885 /* get socket event parameters */
886 DECL_HANDLER(get_socket_event
)
890 int errors
[FD_MAX_EVENTS
];
892 sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
, &sock_ops
);
900 reply
->mask
= sock
->mask
;
901 reply
->pmask
= sock
->pmask
;
902 reply
->state
= sock
->state
;
903 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
904 errors
[i
] = sock_get_ntstatus(sock
->errors
[i
]);
906 set_reply_data( errors
, min( get_reply_max_size(), sizeof(errors
) ));
912 struct event
*cevent
= get_event_obj( current
->process
, req
->c_event
,
913 EVENT_MODIFY_STATE
);
916 reset_event( cevent
);
917 release_object( cevent
);
921 sock_reselect( sock
);
923 release_object( &sock
->obj
);
926 /* re-enable pending socket events */
927 DECL_HANDLER(enable_socket_event
)
931 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
932 FILE_WRITE_ATTRIBUTES
, &sock_ops
)))
935 sock
->pmask
&= ~req
->mask
; /* is this safe? */
936 sock
->hmask
&= ~req
->mask
;
937 sock
->state
|= req
->sstate
;
938 sock
->state
&= ~req
->cstate
;
939 if ( sock
->type
!= SOCK_STREAM
) sock
->state
&= ~STREAM_FLAG_MASK
;
941 sock_reselect( sock
);
943 release_object( &sock
->obj
);
946 DECL_HANDLER(set_socket_deferred
)
948 struct sock
*sock
, *acceptsock
;
950 sock
=(struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
, &sock_ops
);
954 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, req
->deferred
, 0, &sock_ops
);
957 release_object( sock
);
960 sock
->deferred
= acceptsock
;
961 release_object( sock
);