mf/session: Handle shutdown state on GetService().
[wine/zf.git] / server / sock.c
blob00d5b0b904404704de4acf1d64da994411d42383
1 /*
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?
24 #include "config.h"
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_NETINET_IN_H
34 # include <netinet/in.h>
35 #endif
36 #ifdef HAVE_POLL_H
37 # include <poll.h>
38 #endif
39 #include <sys/time.h>
40 #include <sys/types.h>
41 #ifdef HAVE_SYS_SOCKET_H
42 # include <sys/socket.h>
43 #endif
44 #ifdef HAVE_SYS_IOCTL_H
45 #include <sys/ioctl.h>
46 #endif
47 #ifdef HAVE_SYS_FILIO_H
48 # include <sys/filio.h>
49 #endif
50 #include <time.h>
51 #include <unistd.h>
52 #include <limits.h>
53 #ifdef HAVE_LINUX_RTNETLINK_H
54 # include <linux/rtnetlink.h>
55 #endif
57 #ifdef HAVE_NETIPX_IPX_H
58 # include <netipx/ipx.h>
59 #elif defined(HAVE_LINUX_IPX_H)
60 # ifdef HAVE_ASM_TYPES_H
61 # include <asm/types.h>
62 # endif
63 # ifdef HAVE_LINUX_TYPES_H
64 # include <linux/types.h>
65 # endif
66 # include <linux/ipx.h>
67 #endif
68 #if defined(SOL_IPX) || defined(SO_DEFAULT_HEADERS)
69 # define HAS_IPX
70 #endif
72 #ifdef HAVE_LINUX_IRDA_H
73 # ifdef HAVE_LINUX_TYPES_H
74 # include <linux/types.h>
75 # endif
76 # include <linux/irda.h>
77 # define HAS_IRDA
78 #endif
80 #include "ntstatus.h"
81 #define WIN32_NO_STATUS
82 #include "windef.h"
83 #include "winternl.h"
84 #include "winerror.h"
85 #define USE_WS_PREFIX
86 #include "winsock2.h"
87 #include "ws2tcpip.h"
88 #include "wsipx.h"
89 #include "af_irda.h"
90 #include "wine/afd.h"
92 #include "process.h"
93 #include "file.h"
94 #include "handle.h"
95 #include "thread.h"
96 #include "request.h"
97 #include "user.h"
99 /* From winsock.h */
100 #define FD_MAX_EVENTS 10
101 #define FD_READ_BIT 0
102 #define FD_WRITE_BIT 1
103 #define FD_OOB_BIT 2
104 #define FD_ACCEPT_BIT 3
105 #define FD_CONNECT_BIT 4
106 #define FD_CLOSE_BIT 5
109 * Define flags to be used with the WSAAsyncSelect() call.
111 #define FD_READ 0x00000001
112 #define FD_WRITE 0x00000002
113 #define FD_OOB 0x00000004
114 #define FD_ACCEPT 0x00000008
115 #define FD_CONNECT 0x00000010
116 #define FD_CLOSE 0x00000020
118 /* internal per-socket flags */
119 #define FD_WINE_LISTENING 0x10000000
120 #define FD_WINE_NONBLOCKING 0x20000000
121 #define FD_WINE_CONNECTED 0x40000000
122 #define FD_WINE_RAW 0x80000000
123 #define FD_WINE_INTERNAL 0xFFFF0000
125 struct accept_req
127 struct list entry;
128 struct async *async;
129 struct iosb *iosb;
130 struct sock *sock, *acceptsock;
131 int accepted;
132 unsigned int recv_len, local_len;
135 struct sock
137 struct object obj; /* object header */
138 struct fd *fd; /* socket file descriptor */
139 unsigned int state; /* status bits */
140 unsigned int mask; /* event mask */
141 unsigned int hmask; /* held (blocked) events */
142 unsigned int pmask; /* pending events */
143 unsigned int flags; /* socket flags */
144 int polling; /* is socket being polled? */
145 unsigned short proto; /* socket protocol */
146 unsigned short type; /* socket type */
147 unsigned short family; /* socket family */
148 struct event *event; /* event object */
149 user_handle_t window; /* window to send the message to */
150 unsigned int message; /* message to send */
151 obj_handle_t wparam; /* message wparam (socket handle) */
152 unsigned int errors[FD_MAX_EVENTS]; /* event errors */
153 timeout_t connect_time;/* time the socket was connected */
154 struct sock *deferred; /* socket that waits for a deferred accept */
155 struct async_queue read_q; /* queue for asynchronous reads */
156 struct async_queue write_q; /* queue for asynchronous writes */
157 struct async_queue ifchange_q; /* queue for interface change notifications */
158 struct async_queue accept_q; /* queue for asynchronous accepts */
159 struct object *ifchange_obj; /* the interface change notification object */
160 struct list ifchange_entry; /* entry in ifchange notification list */
161 struct list accept_list; /* list of pending accept requests */
162 struct accept_req *accept_recv_req; /* pending accept-into request which will recv on this socket */
165 static void sock_dump( struct object *obj, int verbose );
166 static struct fd *sock_get_fd( struct object *obj );
167 static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
168 static void sock_destroy( struct object *obj );
169 static struct object *sock_get_ifchange( struct sock *sock );
170 static void sock_release_ifchange( struct sock *sock );
172 static int sock_get_poll_events( struct fd *fd );
173 static void sock_poll_event( struct fd *fd, int event );
174 static enum server_fd_type sock_get_fd_type( struct fd *fd );
175 static int sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
176 static void sock_queue_async( struct fd *fd, struct async *async, int type, int count );
177 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
179 static int accept_into_socket( struct sock *sock, struct sock *acceptsock );
180 static struct sock *accept_socket( struct sock *sock );
181 static int sock_get_ntstatus( int err );
182 static unsigned int sock_get_error( int err );
184 static const struct object_ops sock_ops =
186 sizeof(struct sock), /* size */
187 &file_type, /* type */
188 sock_dump, /* dump */
189 add_queue, /* add_queue */
190 remove_queue, /* remove_queue */
191 default_fd_signaled, /* signaled */
192 no_satisfied, /* satisfied */
193 no_signal, /* signal */
194 sock_get_fd, /* get_fd */
195 default_map_access, /* map_access */
196 default_get_sd, /* get_sd */
197 default_set_sd, /* set_sd */
198 no_get_full_name, /* get_full_name */
199 no_lookup_name, /* lookup_name */
200 no_link_name, /* link_name */
201 NULL, /* unlink_name */
202 no_open_file, /* open_file */
203 no_kernel_obj_list, /* get_kernel_obj_list */
204 sock_close_handle, /* close_handle */
205 sock_destroy /* destroy */
208 static const struct fd_ops sock_fd_ops =
210 sock_get_poll_events, /* get_poll_events */
211 sock_poll_event, /* poll_event */
212 sock_get_fd_type, /* get_fd_type */
213 no_fd_read, /* read */
214 no_fd_write, /* write */
215 no_fd_flush, /* flush */
216 default_fd_get_file_info, /* get_file_info */
217 no_fd_get_volume_info, /* get_volume_info */
218 sock_ioctl, /* ioctl */
219 sock_queue_async, /* queue_async */
220 sock_reselect_async /* reselect_async */
223 union unix_sockaddr
225 struct sockaddr addr;
226 struct sockaddr_in in;
227 struct sockaddr_in6 in6;
228 #ifdef HAS_IPX
229 struct sockaddr_ipx ipx;
230 #endif
231 #ifdef HAS_IRDA
232 struct sockaddr_irda irda;
233 #endif
236 static int sockaddr_from_unix( const union unix_sockaddr *uaddr, struct WS_sockaddr *wsaddr, socklen_t wsaddrlen )
238 memset( wsaddr, 0, wsaddrlen );
240 switch (uaddr->addr.sa_family)
242 case AF_INET:
244 struct WS_sockaddr_in win = {0};
246 if (wsaddrlen < sizeof(win)) return -1;
247 win.sin_family = WS_AF_INET;
248 win.sin_port = uaddr->in.sin_port;
249 memcpy( &win.sin_addr, &uaddr->in.sin_addr, sizeof(win.sin_addr) );
250 memcpy( wsaddr, &win, sizeof(win) );
251 return sizeof(win);
254 case AF_INET6:
256 struct WS_sockaddr_in6 win = {0};
258 if (wsaddrlen < sizeof(struct WS_sockaddr_in6_old)) return -1;
259 win.sin6_family = WS_AF_INET6;
260 win.sin6_port = uaddr->in6.sin6_port;
261 win.sin6_flowinfo = uaddr->in6.sin6_flowinfo;
262 memcpy( &win.sin6_addr, &uaddr->in6.sin6_addr, sizeof(win.sin6_addr) );
263 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
264 win.sin6_scope_id = uaddr->in6.sin6_scope_id;
265 #endif
266 if (wsaddrlen >= sizeof(struct WS_sockaddr_in6))
268 memcpy( wsaddr, &win, sizeof(struct WS_sockaddr_in6) );
269 return sizeof(struct WS_sockaddr_in6);
271 memcpy( wsaddr, &win, sizeof(struct WS_sockaddr_in6_old) );
272 return sizeof(struct WS_sockaddr_in6_old);
275 #ifdef HAS_IPX
276 case AF_IPX:
278 struct WS_sockaddr_ipx win = {0};
280 if (wsaddrlen < sizeof(win)) return -1;
281 win.sa_family = WS_AF_IPX;
282 memcpy( win.sa_netnum, &uaddr->ipx.sipx_network, sizeof(win.sa_netnum) );
283 memcpy( win.sa_nodenum, &uaddr->ipx.sipx_node, sizeof(win.sa_nodenum) );
284 win.sa_socket = uaddr->ipx.sipx_port;
285 memcpy( wsaddr, &win, sizeof(win) );
286 return sizeof(win);
288 #endif
290 #ifdef HAS_IRDA
291 case AF_IRDA:
293 SOCKADDR_IRDA win;
295 if (wsaddrlen < sizeof(win)) return -1;
296 win.irdaAddressFamily = WS_AF_IRDA;
297 memcpy( win.irdaDeviceID, &uaddr->irda.sir_addr, sizeof(win.irdaDeviceID) );
298 if (uaddr->irda.sir_lsap_sel != LSAP_ANY)
299 snprintf( win.irdaServiceName, sizeof(win.irdaServiceName), "LSAP-SEL%u", uaddr->irda.sir_lsap_sel );
300 else
301 memcpy( win.irdaServiceName, uaddr->irda.sir_name, sizeof(win.irdaServiceName) );
302 memcpy( wsaddr, &win, sizeof(win) );
303 return sizeof(win);
305 #endif
307 case AF_UNSPEC:
308 return 0;
310 default:
311 return -1;
316 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
317 * we post messages if there are multiple events. Used to send
318 * messages. The problem is if there is both a FD_CONNECT event and,
319 * say, an FD_READ event available on the same socket, we want to
320 * notify the app of the connect event first. Otherwise it may
321 * discard the read event because it thinks it hasn't connected yet.
323 static const int event_bitorder[FD_MAX_EVENTS] =
325 FD_CONNECT_BIT,
326 FD_ACCEPT_BIT,
327 FD_OOB_BIT,
328 FD_WRITE_BIT,
329 FD_READ_BIT,
330 FD_CLOSE_BIT,
331 6, 7, 8, 9 /* leftovers */
334 /* Flags that make sense only for SOCK_STREAM sockets */
335 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
337 typedef enum {
338 SOCK_SHUTDOWN_ERROR = -1,
339 SOCK_SHUTDOWN_EOF = 0,
340 SOCK_SHUTDOWN_POLLHUP = 1
341 } sock_shutdown_t;
343 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
345 static sock_shutdown_t sock_check_pollhup(void)
347 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
348 int fd[2], n;
349 struct pollfd pfd;
350 char dummy;
352 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
353 if ( shutdown( fd[0], 1 ) ) goto out;
355 pfd.fd = fd[1];
356 pfd.events = POLLIN;
357 pfd.revents = 0;
359 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
360 n = poll( &pfd, 1, 1 );
361 if ( n != 1 ) goto out; /* error or timeout */
362 if ( pfd.revents & POLLHUP )
363 ret = SOCK_SHUTDOWN_POLLHUP;
364 else if ( pfd.revents & POLLIN &&
365 read( fd[1], &dummy, 1 ) == 0 )
366 ret = SOCK_SHUTDOWN_EOF;
368 out:
369 close( fd[0] );
370 close( fd[1] );
371 return ret;
374 void sock_init(void)
376 sock_shutdown_type = sock_check_pollhup();
378 switch ( sock_shutdown_type )
380 case SOCK_SHUTDOWN_EOF:
381 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
382 break;
383 case SOCK_SHUTDOWN_POLLHUP:
384 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
385 break;
386 default:
387 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
388 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
392 static int sock_reselect( struct sock *sock )
394 int ev = sock_get_poll_events( sock->fd );
396 if (debug_level)
397 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
399 if (!sock->polling) /* FIXME: should find a better way to do this */
401 /* previously unconnected socket, is this reselect supposed to connect it? */
402 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
403 /* ok, it is, attach it to the wineserver's main poll loop */
404 sock->polling = 1;
405 allow_fd_caching( sock->fd );
407 /* update condition mask */
408 set_fd_events( sock->fd, ev );
409 return ev;
412 /* wake anybody waiting on the socket event or send the associated message */
413 static void sock_wake_up( struct sock *sock )
415 unsigned int events = sock->pmask & sock->mask;
416 int i;
418 if ( !events ) return;
420 if (sock->event)
422 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
423 set_event( sock->event );
425 if (sock->window)
427 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
428 for (i = 0; i < FD_MAX_EVENTS; i++)
430 int event = event_bitorder[i];
431 if (sock->pmask & (1 << event))
433 lparam_t lparam = (1 << event) | (sock->errors[event] << 16);
434 post_message( sock->window, sock->message, sock->wparam, lparam );
437 sock->pmask = 0;
438 sock_reselect( sock );
442 static inline int sock_error( struct fd *fd )
444 unsigned int optval = 0;
445 socklen_t optlen = sizeof(optval);
447 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
448 return optval;
451 static void free_accept_req( void *private )
453 struct accept_req *req = private;
454 list_remove( &req->entry );
455 if (req->acceptsock)
457 req->acceptsock->accept_recv_req = NULL;
458 release_object( req->acceptsock );
460 release_object( req->async );
461 release_object( req->iosb );
462 release_object( req->sock );
463 free( req );
466 static void fill_accept_output( struct accept_req *req )
468 struct iosb *iosb = req->iosb;
469 union unix_sockaddr unix_addr;
470 struct WS_sockaddr *win_addr;
471 unsigned int remote_len;
472 socklen_t unix_len;
473 int fd, size = 0;
474 char *out_data;
475 int win_len;
477 if (!(out_data = mem_alloc( iosb->out_size ))) return;
479 fd = get_unix_fd( req->acceptsock->fd );
481 if (req->recv_len && (size = recv( fd, out_data, req->recv_len, 0 )) < 0)
483 if (!req->accepted && errno == EWOULDBLOCK)
485 req->accepted = 1;
486 sock_reselect( req->acceptsock );
487 set_error( STATUS_PENDING );
488 return;
491 set_win32_error( sock_get_error( errno ) );
492 free( out_data );
493 return;
496 if (req->local_len)
498 if (req->local_len < sizeof(int))
500 set_error( STATUS_BUFFER_TOO_SMALL );
501 free( out_data );
502 return;
505 unix_len = sizeof(unix_addr);
506 win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + sizeof(int));
507 if (getsockname( fd, &unix_addr.addr, &unix_len ) < 0 ||
508 (win_len = sockaddr_from_unix( &unix_addr, win_addr, req->local_len - sizeof(int) )) < 0)
510 set_win32_error( sock_get_error( errno ) );
511 free( out_data );
512 return;
514 memcpy( out_data + req->recv_len, &win_len, sizeof(int) );
517 unix_len = sizeof(unix_addr);
518 win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + req->local_len + sizeof(int));
519 remote_len = iosb->out_size - req->recv_len - req->local_len;
520 if (getpeername( fd, &unix_addr.addr, &unix_len ) < 0 ||
521 (win_len = sockaddr_from_unix( &unix_addr, win_addr, remote_len - sizeof(int) )) < 0)
523 set_win32_error( sock_get_error( errno ) );
524 free( out_data );
525 return;
527 memcpy( out_data + req->recv_len + req->local_len, &win_len, sizeof(int) );
529 iosb->status = STATUS_SUCCESS;
530 iosb->result = size;
531 iosb->out_data = out_data;
532 set_error( STATUS_ALERTED );
535 static void complete_async_accept( struct sock *sock, struct accept_req *req )
537 struct sock *acceptsock = req->acceptsock;
538 struct async *async = req->async;
540 if (debug_level) fprintf( stderr, "completing accept request for socket %p\n", sock );
542 if (acceptsock)
544 if (!accept_into_socket( sock, acceptsock )) return;
545 fill_accept_output( req );
547 else
549 struct iosb *iosb = req->iosb;
550 obj_handle_t handle;
552 if (!(acceptsock = accept_socket( sock ))) return;
553 handle = alloc_handle_no_access_check( async_get_thread( async )->process, &acceptsock->obj,
554 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
555 acceptsock->wparam = handle;
556 release_object( acceptsock );
557 if (!handle) return;
559 if (!(iosb->out_data = malloc( sizeof(handle) ))) return;
561 iosb->status = STATUS_SUCCESS;
562 iosb->out_size = sizeof(handle);
563 memcpy( iosb->out_data, &handle, sizeof(handle) );
564 set_error( STATUS_ALERTED );
568 static void complete_async_accept_recv( struct accept_req *req )
570 if (debug_level) fprintf( stderr, "completing accept recv request for socket %p\n", req->acceptsock );
572 assert( req->recv_len );
574 fill_accept_output( req );
577 static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
579 if (event & (POLLIN | POLLPRI))
581 struct accept_req *req;
583 LIST_FOR_EACH_ENTRY( req, &sock->accept_list, struct accept_req, entry )
585 if (req->iosb->status == STATUS_PENDING && !req->accepted)
587 complete_async_accept( sock, req );
588 if (get_error() != STATUS_PENDING)
589 async_terminate( req->async, get_error() );
590 break;
594 if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
596 complete_async_accept_recv( sock->accept_recv_req );
597 if (get_error() != STATUS_PENDING)
598 async_terminate( sock->accept_recv_req->async, get_error() );
602 if (is_fd_overlapped( sock->fd ))
604 if (event & (POLLIN|POLLPRI) && async_waiting( &sock->read_q ))
606 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
607 async_wake_up( &sock->read_q, STATUS_ALERTED );
608 event &= ~(POLLIN|POLLPRI);
610 if (event & POLLOUT && async_waiting( &sock->write_q ))
612 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
613 async_wake_up( &sock->write_q, STATUS_ALERTED );
614 event &= ~POLLOUT;
618 if (event & (POLLERR | POLLHUP))
620 int status = sock_get_ntstatus( error );
621 struct accept_req *req, *next;
623 if (!(sock->state & FD_READ))
624 async_wake_up( &sock->read_q, status );
625 if (!(sock->state & FD_WRITE))
626 async_wake_up( &sock->write_q, status );
628 LIST_FOR_EACH_ENTRY_SAFE( req, next, &sock->accept_list, struct accept_req, entry )
630 if (req->iosb->status == STATUS_PENDING)
631 async_terminate( req->async, status );
634 if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
635 async_terminate( sock->accept_recv_req->async, status );
638 return event;
641 static void sock_dispatch_events( struct sock *sock, int prevstate, int event, int error )
643 if (prevstate & FD_CONNECT)
645 sock->pmask |= FD_CONNECT;
646 sock->hmask |= FD_CONNECT;
647 sock->errors[FD_CONNECT_BIT] = sock_get_error( error );
648 goto end;
650 if (prevstate & FD_WINE_LISTENING)
652 sock->pmask |= FD_ACCEPT;
653 sock->hmask |= FD_ACCEPT;
654 sock->errors[FD_ACCEPT_BIT] = sock_get_error( error );
655 goto end;
658 if (event & POLLIN)
660 sock->pmask |= FD_READ;
661 sock->hmask |= FD_READ;
662 sock->errors[FD_READ_BIT] = 0;
665 if (event & POLLOUT)
667 sock->pmask |= FD_WRITE;
668 sock->hmask |= FD_WRITE;
669 sock->errors[FD_WRITE_BIT] = 0;
672 if (event & POLLPRI)
674 sock->pmask |= FD_OOB;
675 sock->hmask |= FD_OOB;
676 sock->errors[FD_OOB_BIT] = 0;
679 if (event & (POLLERR|POLLHUP))
681 sock->pmask |= FD_CLOSE;
682 sock->hmask |= FD_CLOSE;
683 sock->errors[FD_CLOSE_BIT] = sock_get_error( error );
685 end:
686 sock_wake_up( sock );
689 static void sock_poll_event( struct fd *fd, int event )
691 struct sock *sock = get_fd_user( fd );
692 int hangup_seen = 0;
693 int prevstate = sock->state;
694 int error = 0;
696 assert( sock->obj.ops == &sock_ops );
697 if (debug_level)
698 fprintf(stderr, "socket %p select event: %x\n", sock, event);
700 /* we may change event later, remove from loop here */
701 if (event & (POLLERR|POLLHUP)) set_fd_events( sock->fd, -1 );
703 if (sock->state & FD_CONNECT)
705 if (event & (POLLERR|POLLHUP))
707 /* we didn't get connected? */
708 sock->state &= ~FD_CONNECT;
709 event &= ~POLLOUT;
710 error = sock_error( fd );
712 else if (event & POLLOUT)
714 /* we got connected */
715 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
716 sock->state &= ~FD_CONNECT;
717 sock->connect_time = current_time;
720 else if (sock->state & FD_WINE_LISTENING)
722 /* listening */
723 if (event & (POLLERR|POLLHUP))
724 error = sock_error( fd );
726 else
728 /* normal data flow */
729 if (sock->type == WS_SOCK_STREAM && (event & POLLIN))
731 char dummy;
732 int nr;
734 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
735 * has been closed, so we need to check for it explicitly here */
736 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
737 if ( nr == 0 )
739 hangup_seen = 1;
740 event &= ~POLLIN;
742 else if ( nr < 0 )
744 event &= ~POLLIN;
745 /* EAGAIN can happen if an async recv() falls between the server's poll()
746 call and the invocation of this routine */
747 if ( errno != EAGAIN )
749 error = errno;
750 event |= POLLERR;
751 if ( debug_level )
752 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
757 if ( (hangup_seen || event & (POLLHUP|POLLERR)) && (sock->state & (FD_READ|FD_WRITE)) )
759 error = error ? error : sock_error( fd );
760 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
761 sock->state &= ~FD_WRITE;
762 sock->state &= ~FD_READ;
764 if (debug_level)
765 fprintf(stderr, "socket %p aborted by error %d, event: %x\n", sock, error, event);
768 if (hangup_seen)
769 event |= POLLHUP;
772 event = sock_dispatch_asyncs( sock, event, error );
773 sock_dispatch_events( sock, prevstate, event, error );
775 sock_reselect( sock );
778 static void sock_dump( struct object *obj, int verbose )
780 struct sock *sock = (struct sock *)obj;
781 assert( obj->ops == &sock_ops );
782 fprintf( stderr, "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
783 sock->fd, sock->state,
784 sock->mask, sock->pmask, sock->hmask );
787 static int sock_get_poll_events( struct fd *fd )
789 struct sock *sock = get_fd_user( fd );
790 unsigned int mask = sock->mask & ~sock->hmask;
791 unsigned int smask = sock->state & mask;
792 int ev = 0;
794 assert( sock->obj.ops == &sock_ops );
796 if (sock->state & FD_CONNECT)
797 /* connecting, wait for writable */
798 return POLLOUT;
800 if (!list_empty( &sock->accept_list ) || sock->accept_recv_req )
802 ev |= POLLIN | POLLPRI;
804 else if (async_queued( &sock->read_q ))
806 if (async_waiting( &sock->read_q )) ev |= POLLIN | POLLPRI;
808 else if (smask & FD_READ || (sock->state & FD_WINE_LISTENING && mask & FD_ACCEPT))
809 ev |= POLLIN | POLLPRI;
810 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
811 else if (sock->type == WS_SOCK_STREAM && (sock->state & FD_READ) && (mask & FD_CLOSE) &&
812 !(sock->hmask & FD_READ))
813 ev |= POLLIN;
815 if (async_queued( &sock->write_q ))
817 if (async_waiting( &sock->write_q )) ev |= POLLOUT;
819 else if (smask & FD_WRITE)
820 ev |= POLLOUT;
822 return ev;
825 static enum server_fd_type sock_get_fd_type( struct fd *fd )
827 return FD_TYPE_SOCKET;
830 static void sock_queue_async( struct fd *fd, struct async *async, int type, int count )
832 struct sock *sock = get_fd_user( fd );
833 struct async_queue *queue;
835 assert( sock->obj.ops == &sock_ops );
837 switch (type)
839 case ASYNC_TYPE_READ:
840 queue = &sock->read_q;
841 break;
842 case ASYNC_TYPE_WRITE:
843 queue = &sock->write_q;
844 break;
845 default:
846 set_error( STATUS_INVALID_PARAMETER );
847 return;
850 if ( ( !( sock->state & (FD_READ|FD_CONNECT|FD_WINE_LISTENING) ) && type == ASYNC_TYPE_READ ) ||
851 ( !( sock->state & (FD_WRITE|FD_CONNECT) ) && type == ASYNC_TYPE_WRITE ) )
853 set_error( STATUS_PIPE_DISCONNECTED );
854 return;
857 queue_async( queue, async );
858 sock_reselect( sock );
860 set_error( STATUS_PENDING );
863 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
865 struct sock *sock = get_fd_user( fd );
867 /* ignore reselect on ifchange queue */
868 if (&sock->ifchange_q != queue)
869 sock_reselect( sock );
872 static struct fd *sock_get_fd( struct object *obj )
874 struct sock *sock = (struct sock *)obj;
875 return (struct fd *)grab_object( sock->fd );
878 static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
880 struct sock *sock = (struct sock *)obj;
881 struct accept_req *req, *next;
883 if (sock->obj.handle_count == 1) /* last handle */
885 if (sock->accept_recv_req)
886 async_terminate( sock->accept_recv_req->async, STATUS_CANCELLED );
888 LIST_FOR_EACH_ENTRY_SAFE( req, next, &sock->accept_list, struct accept_req, entry )
889 async_terminate( req->async, STATUS_CANCELLED );
892 return 1;
895 static void sock_destroy( struct object *obj )
897 struct sock *sock = (struct sock *)obj;
899 assert( obj->ops == &sock_ops );
901 /* FIXME: special socket shutdown stuff? */
903 if ( sock->deferred )
904 release_object( sock->deferred );
906 async_wake_up( &sock->ifchange_q, STATUS_CANCELLED );
907 sock_release_ifchange( sock );
908 free_async_queue( &sock->read_q );
909 free_async_queue( &sock->write_q );
910 free_async_queue( &sock->ifchange_q );
911 free_async_queue( &sock->accept_q );
912 if (sock->event) release_object( sock->event );
913 if (sock->fd)
915 /* shut the socket down to force pending poll() calls in the client to return */
916 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
917 release_object( sock->fd );
921 static struct sock *create_socket(void)
923 struct sock *sock;
925 if (!(sock = alloc_object( &sock_ops ))) return NULL;
926 sock->fd = NULL;
927 sock->state = 0;
928 sock->mask = 0;
929 sock->hmask = 0;
930 sock->pmask = 0;
931 sock->polling = 0;
932 sock->flags = 0;
933 sock->proto = 0;
934 sock->type = 0;
935 sock->family = 0;
936 sock->event = NULL;
937 sock->window = 0;
938 sock->message = 0;
939 sock->wparam = 0;
940 sock->connect_time = 0;
941 sock->deferred = NULL;
942 sock->ifchange_obj = NULL;
943 sock->accept_recv_req = NULL;
944 init_async_queue( &sock->read_q );
945 init_async_queue( &sock->write_q );
946 init_async_queue( &sock->ifchange_q );
947 init_async_queue( &sock->accept_q );
948 memset( sock->errors, 0, sizeof(sock->errors) );
949 list_init( &sock->accept_list );
950 return sock;
953 static int get_unix_family( int family )
955 switch (family)
957 case WS_AF_INET: return AF_INET;
958 case WS_AF_INET6: return AF_INET6;
959 #ifdef HAS_IPX
960 case WS_AF_IPX: return AF_IPX;
961 #endif
962 #ifdef AF_IRDA
963 case WS_AF_IRDA: return AF_IRDA;
964 #endif
965 case WS_AF_UNSPEC: return AF_UNSPEC;
966 default: return -1;
970 static int get_unix_type( int type )
972 switch (type)
974 case WS_SOCK_DGRAM: return SOCK_DGRAM;
975 case WS_SOCK_RAW: return SOCK_RAW;
976 case WS_SOCK_STREAM: return SOCK_STREAM;
977 default: return -1;
981 static int get_unix_protocol( int protocol )
983 if (protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
984 return protocol;
986 switch (protocol)
988 case WS_IPPROTO_ICMP: return IPPROTO_ICMP;
989 case WS_IPPROTO_IGMP: return IPPROTO_IGMP;
990 case WS_IPPROTO_IP: return IPPROTO_IP;
991 case WS_IPPROTO_IPIP: return IPPROTO_IPIP;
992 case WS_IPPROTO_IPV6: return IPPROTO_IPV6;
993 case WS_IPPROTO_RAW: return IPPROTO_RAW;
994 case WS_IPPROTO_TCP: return IPPROTO_TCP;
995 case WS_IPPROTO_UDP: return IPPROTO_UDP;
996 default: return -1;
1000 static void set_dont_fragment( int fd, int level, int value )
1002 int optname;
1004 if (level == IPPROTO_IP)
1006 #ifdef IP_DONTFRAG
1007 optname = IP_DONTFRAG;
1008 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1009 optname = IP_MTU_DISCOVER;
1010 value = value ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
1011 #else
1012 return;
1013 #endif
1015 else
1017 #ifdef IPV6_DONTFRAG
1018 optname = IPV6_DONTFRAG;
1019 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1020 optname = IPV6_MTU_DISCOVER;
1021 value = value ? IPV6_PMTUDISC_DO : IPV6_PMTUDISC_DONT;
1022 #else
1023 return;
1024 #endif
1027 setsockopt( fd, level, optname, &value, sizeof(value) );
1030 static int init_socket( struct sock *sock, int family, int type, int protocol, unsigned int flags )
1032 unsigned int options = 0;
1033 int sockfd, unix_type, unix_family, unix_protocol;
1035 unix_family = get_unix_family( family );
1036 unix_type = get_unix_type( type );
1037 unix_protocol = get_unix_protocol( protocol );
1039 if (unix_protocol < 0)
1041 if (type && unix_type < 0)
1042 set_win32_error( WSAESOCKTNOSUPPORT );
1043 else
1044 set_win32_error( WSAEPROTONOSUPPORT );
1045 return -1;
1047 if (unix_family < 0)
1049 if (family >= 0 && unix_type < 0)
1050 set_win32_error( WSAESOCKTNOSUPPORT );
1051 else
1052 set_win32_error( WSAEAFNOSUPPORT );
1053 return -1;
1056 sockfd = socket( unix_family, unix_type, unix_protocol );
1057 if (sockfd == -1)
1059 if (errno == EINVAL) set_win32_error( WSAESOCKTNOSUPPORT );
1060 else set_win32_error( sock_get_error( errno ));
1061 return -1;
1063 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
1065 if (family == WS_AF_IPX && protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
1067 #ifdef HAS_IPX
1068 int ipx_type = protocol - WS_NSPROTO_IPX;
1070 #ifdef SOL_IPX
1071 setsockopt( sockfd, SOL_IPX, IPX_TYPE, &ipx_type, sizeof(ipx_type) );
1072 #else
1073 struct ipx val;
1074 /* Should we retrieve val using a getsockopt call and then
1075 * set the modified one? */
1076 val.ipx_pt = ipx_type;
1077 setsockopt( sockfd, 0, SO_DEFAULT_HEADERS, &val, sizeof(val) );
1078 #endif
1079 #endif
1082 if (unix_family == AF_INET || unix_family == AF_INET6)
1084 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1085 if (unix_type == SOCK_DGRAM || unix_type == SOCK_RAW) /* in Linux the global default can be enabled */
1086 set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, FALSE );
1087 else if (unix_type == SOCK_STREAM)
1088 set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, TRUE );
1091 #ifdef IPV6_V6ONLY
1092 if (unix_family == AF_INET6)
1094 static const int enable = 1;
1095 setsockopt( sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &enable, sizeof(enable) );
1097 #endif
1099 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
1100 sock->flags = flags;
1101 sock->proto = protocol;
1102 sock->type = type;
1103 sock->family = family;
1105 if (sock->fd)
1107 options = get_fd_options( sock->fd );
1108 release_object( sock->fd );
1111 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj, options )))
1113 return -1;
1115 sock_reselect( sock );
1116 clear_error();
1117 return 0;
1120 /* accepts a socket and inits it */
1121 static int accept_new_fd( struct sock *sock )
1124 /* Try to accept(2). We can't be safe that this an already connected socket
1125 * or that accept() is allowed on it. In those cases we will get -1/errno
1126 * return.
1128 struct sockaddr saddr;
1129 socklen_t slen = sizeof(saddr);
1130 int acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen );
1131 if (acceptfd != -1)
1132 fcntl( acceptfd, F_SETFL, O_NONBLOCK );
1133 else
1134 set_win32_error( sock_get_error( errno ));
1135 return acceptfd;
1138 /* accept a socket (creates a new fd) */
1139 static struct sock *accept_socket( struct sock *sock )
1141 struct sock *acceptsock;
1142 int acceptfd;
1144 if (get_unix_fd( sock->fd ) == -1) return NULL;
1146 if ( sock->deferred )
1148 acceptsock = sock->deferred;
1149 sock->deferred = NULL;
1151 else
1153 if ((acceptfd = accept_new_fd( sock )) == -1) return NULL;
1154 if (!(acceptsock = create_socket()))
1156 close( acceptfd );
1157 return NULL;
1160 /* newly created socket gets the same properties of the listening socket */
1161 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
1162 if (sock->state & FD_WINE_NONBLOCKING)
1163 acceptsock->state |= FD_WINE_NONBLOCKING;
1164 acceptsock->mask = sock->mask;
1165 acceptsock->proto = sock->proto;
1166 acceptsock->type = sock->type;
1167 acceptsock->family = sock->family;
1168 acceptsock->window = sock->window;
1169 acceptsock->message = sock->message;
1170 acceptsock->connect_time = current_time;
1171 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
1172 acceptsock->flags = sock->flags;
1173 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
1174 get_fd_options( sock->fd ) )))
1176 release_object( acceptsock );
1177 return NULL;
1180 clear_error();
1181 sock->pmask &= ~FD_ACCEPT;
1182 sock->hmask &= ~FD_ACCEPT;
1183 sock_reselect( sock );
1184 return acceptsock;
1187 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
1189 int acceptfd;
1190 struct fd *newfd;
1192 if (get_unix_fd( sock->fd ) == -1) return FALSE;
1194 if ( sock->deferred )
1196 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
1197 get_fd_options( acceptsock->fd ) );
1198 if ( !newfd )
1199 return FALSE;
1201 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
1203 release_object( sock->deferred );
1204 sock->deferred = NULL;
1206 else
1208 if ((acceptfd = accept_new_fd( sock )) == -1)
1209 return FALSE;
1211 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
1212 get_fd_options( acceptsock->fd ) )))
1213 return FALSE;
1216 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
1217 acceptsock->hmask = 0;
1218 acceptsock->pmask = 0;
1219 acceptsock->polling = 0;
1220 acceptsock->proto = sock->proto;
1221 acceptsock->type = sock->type;
1222 acceptsock->family = sock->family;
1223 acceptsock->wparam = 0;
1224 acceptsock->deferred = NULL;
1225 acceptsock->connect_time = current_time;
1226 fd_copy_completion( acceptsock->fd, newfd );
1227 release_object( acceptsock->fd );
1228 acceptsock->fd = newfd;
1230 clear_error();
1231 sock->pmask &= ~FD_ACCEPT;
1232 sock->hmask &= ~FD_ACCEPT;
1233 sock_reselect( sock );
1235 return TRUE;
1238 /* return an errno value mapped to a WSA error */
1239 static unsigned int sock_get_error( int err )
1241 switch (err)
1243 case EINTR: return WSAEINTR;
1244 case EBADF: return WSAEBADF;
1245 case EPERM:
1246 case EACCES: return WSAEACCES;
1247 case EFAULT: return WSAEFAULT;
1248 case EINVAL: return WSAEINVAL;
1249 case EMFILE: return WSAEMFILE;
1250 case EWOULDBLOCK: return WSAEWOULDBLOCK;
1251 case EINPROGRESS: return WSAEINPROGRESS;
1252 case EALREADY: return WSAEALREADY;
1253 case ENOTSOCK: return WSAENOTSOCK;
1254 case EDESTADDRREQ: return WSAEDESTADDRREQ;
1255 case EMSGSIZE: return WSAEMSGSIZE;
1256 case EPROTOTYPE: return WSAEPROTOTYPE;
1257 case ENOPROTOOPT: return WSAENOPROTOOPT;
1258 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
1259 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
1260 case EOPNOTSUPP: return WSAEOPNOTSUPP;
1261 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
1262 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
1263 case EADDRINUSE: return WSAEADDRINUSE;
1264 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
1265 case ENETDOWN: return WSAENETDOWN;
1266 case ENETUNREACH: return WSAENETUNREACH;
1267 case ENETRESET: return WSAENETRESET;
1268 case ECONNABORTED: return WSAECONNABORTED;
1269 case EPIPE:
1270 case ECONNRESET: return WSAECONNRESET;
1271 case ENOBUFS: return WSAENOBUFS;
1272 case EISCONN: return WSAEISCONN;
1273 case ENOTCONN: return WSAENOTCONN;
1274 case ESHUTDOWN: return WSAESHUTDOWN;
1275 case ETOOMANYREFS: return WSAETOOMANYREFS;
1276 case ETIMEDOUT: return WSAETIMEDOUT;
1277 case ECONNREFUSED: return WSAECONNREFUSED;
1278 case ELOOP: return WSAELOOP;
1279 case ENAMETOOLONG: return WSAENAMETOOLONG;
1280 case EHOSTDOWN: return WSAEHOSTDOWN;
1281 case EHOSTUNREACH: return WSAEHOSTUNREACH;
1282 case ENOTEMPTY: return WSAENOTEMPTY;
1283 #ifdef EPROCLIM
1284 case EPROCLIM: return WSAEPROCLIM;
1285 #endif
1286 #ifdef EUSERS
1287 case EUSERS: return WSAEUSERS;
1288 #endif
1289 #ifdef EDQUOT
1290 case EDQUOT: return WSAEDQUOT;
1291 #endif
1292 #ifdef ESTALE
1293 case ESTALE: return WSAESTALE;
1294 #endif
1295 #ifdef EREMOTE
1296 case EREMOTE: return WSAEREMOTE;
1297 #endif
1299 case 0: return 0;
1300 default:
1301 errno = err;
1302 perror("wineserver: sock_get_error() can't map error");
1303 return WSAEFAULT;
1307 static int sock_get_ntstatus( int err )
1309 switch ( err )
1311 case EBADF: return STATUS_INVALID_HANDLE;
1312 case EBUSY: return STATUS_DEVICE_BUSY;
1313 case EPERM:
1314 case EACCES: return STATUS_ACCESS_DENIED;
1315 case EFAULT: return STATUS_NO_MEMORY;
1316 case EINVAL: return STATUS_INVALID_PARAMETER;
1317 case ENFILE:
1318 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
1319 case EWOULDBLOCK: return STATUS_CANT_WAIT;
1320 case EINPROGRESS: return STATUS_PENDING;
1321 case EALREADY: return STATUS_NETWORK_BUSY;
1322 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
1323 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
1324 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
1325 case EPROTONOSUPPORT:
1326 case ESOCKTNOSUPPORT:
1327 case EPFNOSUPPORT:
1328 case EAFNOSUPPORT:
1329 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
1330 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
1331 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
1332 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
1333 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
1334 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
1335 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
1336 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
1337 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
1338 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
1339 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
1340 case ENETDOWN: return STATUS_NETWORK_BUSY;
1341 case EPIPE:
1342 case ECONNRESET: return STATUS_CONNECTION_RESET;
1343 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
1345 case 0: return STATUS_SUCCESS;
1346 default:
1347 errno = err;
1348 perror("wineserver: sock_get_ntstatus() can't map error");
1349 return STATUS_UNSUCCESSFUL;
1353 static struct accept_req *alloc_accept_req( struct sock *sock, struct sock *acceptsock, struct async *async,
1354 const struct afd_accept_into_params *params )
1356 struct accept_req *req = mem_alloc( sizeof(*req) );
1358 if (req)
1360 req->async = (struct async *)grab_object( async );
1361 req->iosb = async_get_iosb( async );
1362 req->sock = (struct sock *)grab_object( sock );
1363 req->acceptsock = acceptsock;
1364 if (acceptsock) grab_object( acceptsock );
1365 req->accepted = 0;
1366 req->recv_len = 0;
1367 req->local_len = 0;
1368 if (params)
1370 req->recv_len = params->recv_len;
1371 req->local_len = params->local_len;
1374 return req;
1377 static int sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1379 struct sock *sock = get_fd_user( fd );
1381 assert( sock->obj.ops == &sock_ops );
1383 if (get_unix_fd( fd ) == -1 && code != IOCTL_AFD_CREATE) return 0;
1385 switch(code)
1387 case IOCTL_AFD_CREATE:
1389 const struct afd_create_params *params = get_req_data();
1391 if (get_req_data_size() != sizeof(*params))
1393 set_error( STATUS_INVALID_PARAMETER );
1394 return 0;
1396 init_socket( sock, params->family, params->type, params->protocol, params->flags );
1397 return 0;
1400 case IOCTL_AFD_ACCEPT:
1402 struct sock *acceptsock;
1403 obj_handle_t handle;
1405 if (get_reply_max_size() != sizeof(handle))
1407 set_error( STATUS_BUFFER_TOO_SMALL );
1408 return 0;
1411 if (!(acceptsock = accept_socket( sock )))
1413 struct accept_req *req;
1415 if (sock->state & FD_WINE_NONBLOCKING) return 0;
1416 if (get_error() != (0xc0010000 | WSAEWOULDBLOCK)) return 0;
1418 if (!(req = alloc_accept_req( sock, NULL, async, NULL ))) return 0;
1419 list_add_tail( &sock->accept_list, &req->entry );
1421 async_set_completion_callback( async, free_accept_req, req );
1422 queue_async( &sock->accept_q, async );
1423 sock_reselect( sock );
1424 set_error( STATUS_PENDING );
1425 return 1;
1427 handle = alloc_handle( current->process, &acceptsock->obj,
1428 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
1429 acceptsock->wparam = handle;
1430 release_object( acceptsock );
1431 set_reply_data( &handle, sizeof(handle) );
1432 return 0;
1435 case IOCTL_AFD_ACCEPT_INTO:
1437 static const int access = FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | FILE_READ_DATA;
1438 const struct afd_accept_into_params *params = get_req_data();
1439 struct sock *acceptsock;
1440 unsigned int remote_len;
1441 struct accept_req *req;
1443 if (get_req_data_size() != sizeof(*params) ||
1444 get_reply_max_size() < params->recv_len ||
1445 get_reply_max_size() - params->recv_len < params->local_len)
1447 set_error( STATUS_BUFFER_TOO_SMALL );
1448 return 0;
1451 remote_len = get_reply_max_size() - params->recv_len - params->local_len;
1452 if (remote_len < sizeof(int))
1454 set_error( STATUS_INVALID_PARAMETER );
1455 return 0;
1458 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, params->accept_handle, access, &sock_ops )))
1459 return 0;
1461 if (acceptsock->accept_recv_req)
1463 release_object( acceptsock );
1464 set_win32_error( WSAEINVAL );
1465 return 0;
1468 if (!(req = alloc_accept_req( sock, acceptsock, async, params )))
1470 release_object( acceptsock );
1471 return 0;
1473 list_add_tail( &sock->accept_list, &req->entry );
1474 acceptsock->accept_recv_req = req;
1475 release_object( acceptsock );
1477 acceptsock->wparam = params->accept_handle;
1478 async_set_completion_callback( async, free_accept_req, req );
1479 queue_async( &sock->accept_q, async );
1480 sock_reselect( sock );
1481 set_error( STATUS_PENDING );
1482 return 1;
1485 case IOCTL_AFD_ADDRESS_LIST_CHANGE:
1486 if ((sock->state & FD_WINE_NONBLOCKING) && async_is_blocking( async ))
1488 set_win32_error( WSAEWOULDBLOCK );
1489 return 0;
1491 if (!sock_get_ifchange( sock )) return 0;
1492 queue_async( &sock->ifchange_q, async );
1493 set_error( STATUS_PENDING );
1494 return 1;
1496 default:
1497 set_error( STATUS_NOT_SUPPORTED );
1498 return 0;
1502 #ifdef HAVE_LINUX_RTNETLINK_H
1504 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
1505 static struct object *ifchange_object;
1507 static void ifchange_dump( struct object *obj, int verbose );
1508 static struct fd *ifchange_get_fd( struct object *obj );
1509 static void ifchange_destroy( struct object *obj );
1511 static int ifchange_get_poll_events( struct fd *fd );
1512 static void ifchange_poll_event( struct fd *fd, int event );
1514 struct ifchange
1516 struct object obj; /* object header */
1517 struct fd *fd; /* interface change file descriptor */
1518 struct list sockets; /* list of sockets to send interface change notifications */
1521 static const struct object_ops ifchange_ops =
1523 sizeof(struct ifchange), /* size */
1524 &no_type, /* type */
1525 ifchange_dump, /* dump */
1526 no_add_queue, /* add_queue */
1527 NULL, /* remove_queue */
1528 NULL, /* signaled */
1529 no_satisfied, /* satisfied */
1530 no_signal, /* signal */
1531 ifchange_get_fd, /* get_fd */
1532 default_map_access, /* map_access */
1533 default_get_sd, /* get_sd */
1534 default_set_sd, /* set_sd */
1535 no_get_full_name, /* get_full_name */
1536 no_lookup_name, /* lookup_name */
1537 no_link_name, /* link_name */
1538 NULL, /* unlink_name */
1539 no_open_file, /* open_file */
1540 no_kernel_obj_list, /* get_kernel_obj_list */
1541 no_close_handle, /* close_handle */
1542 ifchange_destroy /* destroy */
1545 static const struct fd_ops ifchange_fd_ops =
1547 ifchange_get_poll_events, /* get_poll_events */
1548 ifchange_poll_event, /* poll_event */
1549 NULL, /* get_fd_type */
1550 no_fd_read, /* read */
1551 no_fd_write, /* write */
1552 no_fd_flush, /* flush */
1553 no_fd_get_file_info, /* get_file_info */
1554 no_fd_get_volume_info, /* get_volume_info */
1555 no_fd_ioctl, /* ioctl */
1556 NULL, /* queue_async */
1557 NULL /* reselect_async */
1560 static void ifchange_dump( struct object *obj, int verbose )
1562 assert( obj->ops == &ifchange_ops );
1563 fprintf( stderr, "Interface change\n" );
1566 static struct fd *ifchange_get_fd( struct object *obj )
1568 struct ifchange *ifchange = (struct ifchange *)obj;
1569 return (struct fd *)grab_object( ifchange->fd );
1572 static void ifchange_destroy( struct object *obj )
1574 struct ifchange *ifchange = (struct ifchange *)obj;
1575 assert( obj->ops == &ifchange_ops );
1577 release_object( ifchange->fd );
1579 /* reset the global ifchange object so that it will be recreated if it is needed again */
1580 assert( obj == ifchange_object );
1581 ifchange_object = NULL;
1584 static int ifchange_get_poll_events( struct fd *fd )
1586 return POLLIN;
1589 /* wake up all the sockets waiting for a change notification event */
1590 static void ifchange_wake_up( struct object *obj, unsigned int status )
1592 struct ifchange *ifchange = (struct ifchange *)obj;
1593 struct list *ptr, *next;
1594 assert( obj->ops == &ifchange_ops );
1595 assert( obj == ifchange_object );
1597 LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
1599 struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
1601 assert( sock->ifchange_obj );
1602 async_wake_up( &sock->ifchange_q, status ); /* issue ifchange notification for the socket */
1603 sock_release_ifchange( sock ); /* remove socket from list and decrement ifchange refcount */
1607 static void ifchange_poll_event( struct fd *fd, int event )
1609 struct object *ifchange = get_fd_user( fd );
1610 unsigned int status = STATUS_PENDING;
1611 char buffer[PIPE_BUF];
1612 int r;
1614 r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
1615 if (r < 0)
1617 if (errno == EWOULDBLOCK || (EWOULDBLOCK != EAGAIN && errno == EAGAIN))
1618 return; /* retry when poll() says the socket is ready */
1619 status = sock_get_ntstatus( errno );
1621 else if (r > 0)
1623 struct nlmsghdr *nlh;
1625 for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
1627 if (nlh->nlmsg_type == NLMSG_DONE)
1628 break;
1629 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
1630 status = STATUS_SUCCESS;
1633 else status = STATUS_CANCELLED;
1635 if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
1638 #endif
1640 /* we only need one of these interface notification objects, all of the sockets dependent upon
1641 * it will wake up when a notification event occurs */
1642 static struct object *get_ifchange( void )
1644 #ifdef HAVE_LINUX_RTNETLINK_H
1645 struct ifchange *ifchange;
1646 struct sockaddr_nl addr;
1647 int unix_fd;
1649 if (ifchange_object)
1651 /* increment the refcount for each socket that uses the ifchange object */
1652 return grab_object( ifchange_object );
1655 /* create the socket we need for processing interface change notifications */
1656 unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
1657 if (unix_fd == -1)
1659 set_win32_error( sock_get_error( errno ));
1660 return NULL;
1662 fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
1663 memset( &addr, 0, sizeof(addr) );
1664 addr.nl_family = AF_NETLINK;
1665 addr.nl_groups = RTMGRP_IPV4_IFADDR;
1666 /* bind the socket to the special netlink kernel interface */
1667 if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
1669 close( unix_fd );
1670 set_win32_error( sock_get_error( errno ));
1671 return NULL;
1673 if (!(ifchange = alloc_object( &ifchange_ops )))
1675 close( unix_fd );
1676 set_error( STATUS_NO_MEMORY );
1677 return NULL;
1679 list_init( &ifchange->sockets );
1680 if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
1682 release_object( ifchange );
1683 set_error( STATUS_NO_MEMORY );
1684 return NULL;
1686 set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
1688 /* the ifchange object is now successfully configured */
1689 ifchange_object = &ifchange->obj;
1690 return &ifchange->obj;
1691 #else
1692 set_error( STATUS_NOT_SUPPORTED );
1693 return NULL;
1694 #endif
1697 /* add the socket to the interface change notification list */
1698 static void ifchange_add_sock( struct object *obj, struct sock *sock )
1700 #ifdef HAVE_LINUX_RTNETLINK_H
1701 struct ifchange *ifchange = (struct ifchange *)obj;
1703 list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
1704 #endif
1707 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1708 static struct object *sock_get_ifchange( struct sock *sock )
1710 struct object *ifchange;
1712 if (sock->ifchange_obj) /* reuse existing ifchange_obj for this socket */
1713 return sock->ifchange_obj;
1715 if (!(ifchange = get_ifchange()))
1716 return NULL;
1718 /* add the socket to the ifchange notification list */
1719 ifchange_add_sock( ifchange, sock );
1720 sock->ifchange_obj = ifchange;
1721 return ifchange;
1724 /* destroy an existing ifchange queue for a specific socket */
1725 static void sock_release_ifchange( struct sock *sock )
1727 if (sock->ifchange_obj)
1729 list_remove( &sock->ifchange_entry );
1730 release_object( sock->ifchange_obj );
1731 sock->ifchange_obj = NULL;
1735 static void socket_device_dump( struct object *obj, int verbose );
1736 static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
1737 unsigned int attr, struct object *root );
1738 static struct object *socket_device_open_file( struct object *obj, unsigned int access,
1739 unsigned int sharing, unsigned int options );
1741 static const struct object_ops socket_device_ops =
1743 sizeof(struct object), /* size */
1744 &device_type, /* type */
1745 socket_device_dump, /* dump */
1746 no_add_queue, /* add_queue */
1747 NULL, /* remove_queue */
1748 NULL, /* signaled */
1749 no_satisfied, /* satisfied */
1750 no_signal, /* signal */
1751 no_get_fd, /* get_fd */
1752 default_map_access, /* map_access */
1753 default_get_sd, /* get_sd */
1754 default_set_sd, /* set_sd */
1755 default_get_full_name, /* get_full_name */
1756 socket_device_lookup_name, /* lookup_name */
1757 directory_link_name, /* link_name */
1758 default_unlink_name, /* unlink_name */
1759 socket_device_open_file, /* open_file */
1760 no_kernel_obj_list, /* get_kernel_obj_list */
1761 no_close_handle, /* close_handle */
1762 no_destroy /* destroy */
1765 static void socket_device_dump( struct object *obj, int verbose )
1767 fputs( "Socket device\n", stderr );
1770 static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
1771 unsigned int attr, struct object *root )
1773 return NULL;
1776 static struct object *socket_device_open_file( struct object *obj, unsigned int access,
1777 unsigned int sharing, unsigned int options )
1779 struct sock *sock;
1781 if (!(sock = create_socket())) return NULL;
1782 if (!(sock->fd = alloc_pseudo_fd( &sock_fd_ops, &sock->obj, options )))
1784 release_object( sock );
1785 return NULL;
1787 return &sock->obj;
1790 struct object *create_socket_device( struct object *root, const struct unicode_str *name,
1791 unsigned int attr, const struct security_descriptor *sd )
1793 return create_named_object( root, &socket_device_ops, name, attr, sd );
1796 /* set socket event parameters */
1797 DECL_HANDLER(set_socket_event)
1799 struct sock *sock;
1800 struct event *old_event;
1802 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1803 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
1804 if (get_unix_fd( sock->fd ) == -1) return;
1805 old_event = sock->event;
1806 sock->mask = req->mask;
1807 sock->hmask &= ~req->mask; /* re-enable held events */
1808 sock->event = NULL;
1809 sock->window = req->window;
1810 sock->message = req->msg;
1811 sock->wparam = req->handle; /* wparam is the socket handle */
1812 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
1814 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
1816 sock_reselect( sock );
1818 sock->state |= FD_WINE_NONBLOCKING;
1820 /* if a network event is pending, signal the event object
1821 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1822 before a WSAEventSelect() was done on it.
1823 (when dealing with Asynchronous socket) */
1824 sock_wake_up( sock );
1826 if (old_event) release_object( old_event ); /* we're through with it */
1827 release_object( &sock->obj );
1830 /* get socket event parameters */
1831 DECL_HANDLER(get_socket_event)
1833 struct sock *sock;
1835 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1836 FILE_READ_ATTRIBUTES, &sock_ops ))) return;
1837 if (get_unix_fd( sock->fd ) == -1) return;
1838 reply->mask = sock->mask;
1839 reply->pmask = sock->pmask;
1840 reply->state = sock->state;
1841 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
1843 if (req->service)
1845 if (req->c_event)
1847 struct event *cevent = get_event_obj( current->process, req->c_event,
1848 EVENT_MODIFY_STATE );
1849 if (cevent)
1851 reset_event( cevent );
1852 release_object( cevent );
1855 sock->pmask = 0;
1856 sock_reselect( sock );
1858 release_object( &sock->obj );
1861 /* re-enable pending socket events */
1862 DECL_HANDLER(enable_socket_event)
1864 struct sock *sock;
1866 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
1867 FILE_WRITE_ATTRIBUTES, &sock_ops)))
1868 return;
1870 if (get_unix_fd( sock->fd ) == -1) return;
1872 /* for event-based notification, windows erases stale events */
1873 sock->pmask &= ~req->mask;
1875 sock->hmask &= ~req->mask;
1876 sock->state |= req->sstate;
1877 sock->state &= ~req->cstate;
1878 if (sock->type != WS_SOCK_STREAM) sock->state &= ~STREAM_FLAG_MASK;
1880 sock_reselect( sock );
1882 release_object( &sock->obj );
1885 DECL_HANDLER(set_socket_deferred)
1887 struct sock *sock, *acceptsock;
1889 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
1890 if ( !sock )
1891 return;
1893 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
1894 if ( !acceptsock )
1896 release_object( sock );
1897 return;
1899 sock->deferred = acceptsock;
1900 release_object( sock );
1903 DECL_HANDLER(get_socket_info)
1905 struct sock *sock;
1907 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1908 if (!sock) return;
1910 if (get_unix_fd( sock->fd ) == -1) return;
1912 reply->family = sock->family;
1913 reply->type = sock->type;
1914 reply->protocol = sock->proto;
1916 release_object( &sock->obj );