jscript: Improve prop_get and prop_put debug traces.
[wine/zf.git] / server / sock.c
blob3aed5494612769b2e77ce26a8404571b18002a99
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 int sock_signaled( struct object *obj, struct wait_queue_entry *entry );
167 static struct fd *sock_get_fd( struct object *obj );
168 static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
169 static void sock_destroy( struct object *obj );
170 static struct object *sock_get_ifchange( struct sock *sock );
171 static void sock_release_ifchange( struct sock *sock );
173 static int sock_get_poll_events( struct fd *fd );
174 static void sock_poll_event( struct fd *fd, int event );
175 static enum server_fd_type sock_get_fd_type( struct fd *fd );
176 static int sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
177 static void sock_queue_async( struct fd *fd, struct async *async, int type, int count );
178 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
180 static int accept_into_socket( struct sock *sock, struct sock *acceptsock );
181 static struct sock *accept_socket( struct sock *sock );
182 static int sock_get_ntstatus( int err );
183 static unsigned int sock_get_error( int err );
185 static const struct object_ops sock_ops =
187 sizeof(struct sock), /* size */
188 &file_type, /* type */
189 sock_dump, /* dump */
190 add_queue, /* add_queue */
191 remove_queue, /* remove_queue */
192 sock_signaled, /* signaled */
193 no_satisfied, /* satisfied */
194 no_signal, /* signal */
195 sock_get_fd, /* get_fd */
196 default_map_access, /* map_access */
197 default_get_sd, /* get_sd */
198 default_set_sd, /* set_sd */
199 no_get_full_name, /* get_full_name */
200 no_lookup_name, /* lookup_name */
201 no_link_name, /* link_name */
202 NULL, /* unlink_name */
203 no_open_file, /* open_file */
204 no_kernel_obj_list, /* get_kernel_obj_list */
205 sock_close_handle, /* close_handle */
206 sock_destroy /* destroy */
209 static const struct fd_ops sock_fd_ops =
211 sock_get_poll_events, /* get_poll_events */
212 sock_poll_event, /* poll_event */
213 sock_get_fd_type, /* get_fd_type */
214 no_fd_read, /* read */
215 no_fd_write, /* write */
216 no_fd_flush, /* flush */
217 default_fd_get_file_info, /* get_file_info */
218 no_fd_get_volume_info, /* get_volume_info */
219 sock_ioctl, /* ioctl */
220 sock_queue_async, /* queue_async */
221 sock_reselect_async /* reselect_async */
224 union unix_sockaddr
226 struct sockaddr addr;
227 struct sockaddr_in in;
228 struct sockaddr_in6 in6;
229 #ifdef HAS_IPX
230 struct sockaddr_ipx ipx;
231 #endif
232 #ifdef HAS_IRDA
233 struct sockaddr_irda irda;
234 #endif
237 static int sockaddr_from_unix( const union unix_sockaddr *uaddr, struct WS_sockaddr *wsaddr, socklen_t wsaddrlen )
239 memset( wsaddr, 0, wsaddrlen );
241 switch (uaddr->addr.sa_family)
243 case AF_INET:
245 struct WS_sockaddr_in win = {0};
247 if (wsaddrlen < sizeof(win)) return -1;
248 win.sin_family = WS_AF_INET;
249 win.sin_port = uaddr->in.sin_port;
250 memcpy( &win.sin_addr, &uaddr->in.sin_addr, sizeof(win.sin_addr) );
251 memcpy( wsaddr, &win, sizeof(win) );
252 return sizeof(win);
255 case AF_INET6:
257 struct WS_sockaddr_in6 win = {0};
259 if (wsaddrlen < sizeof(struct WS_sockaddr_in6_old)) return -1;
260 win.sin6_family = WS_AF_INET6;
261 win.sin6_port = uaddr->in6.sin6_port;
262 win.sin6_flowinfo = uaddr->in6.sin6_flowinfo;
263 memcpy( &win.sin6_addr, &uaddr->in6.sin6_addr, sizeof(win.sin6_addr) );
264 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
265 win.sin6_scope_id = uaddr->in6.sin6_scope_id;
266 #endif
267 if (wsaddrlen >= sizeof(struct WS_sockaddr_in6))
269 memcpy( wsaddr, &win, sizeof(struct WS_sockaddr_in6) );
270 return sizeof(struct WS_sockaddr_in6);
272 memcpy( wsaddr, &win, sizeof(struct WS_sockaddr_in6_old) );
273 return sizeof(struct WS_sockaddr_in6_old);
276 #ifdef HAS_IPX
277 case AF_IPX:
279 struct WS_sockaddr_ipx win = {0};
281 if (wsaddrlen < sizeof(win)) return -1;
282 win.sa_family = WS_AF_IPX;
283 memcpy( win.sa_netnum, &uaddr->ipx.sipx_network, sizeof(win.sa_netnum) );
284 memcpy( win.sa_nodenum, &uaddr->ipx.sipx_node, sizeof(win.sa_nodenum) );
285 win.sa_socket = uaddr->ipx.sipx_port;
286 memcpy( wsaddr, &win, sizeof(win) );
287 return sizeof(win);
289 #endif
291 #ifdef HAS_IRDA
292 case AF_IRDA:
294 SOCKADDR_IRDA win;
296 if (wsaddrlen < sizeof(win)) return -1;
297 win.irdaAddressFamily = WS_AF_IRDA;
298 memcpy( win.irdaDeviceID, &uaddr->irda.sir_addr, sizeof(win.irdaDeviceID) );
299 if (uaddr->irda.sir_lsap_sel != LSAP_ANY)
300 snprintf( win.irdaServiceName, sizeof(win.irdaServiceName), "LSAP-SEL%u", uaddr->irda.sir_lsap_sel );
301 else
302 memcpy( win.irdaServiceName, uaddr->irda.sir_name, sizeof(win.irdaServiceName) );
303 memcpy( wsaddr, &win, sizeof(win) );
304 return sizeof(win);
306 #endif
308 case AF_UNSPEC:
309 return 0;
311 default:
312 return -1;
317 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
318 * we post messages if there are multiple events. Used to send
319 * messages. The problem is if there is both a FD_CONNECT event and,
320 * say, an FD_READ event available on the same socket, we want to
321 * notify the app of the connect event first. Otherwise it may
322 * discard the read event because it thinks it hasn't connected yet.
324 static const int event_bitorder[FD_MAX_EVENTS] =
326 FD_CONNECT_BIT,
327 FD_ACCEPT_BIT,
328 FD_OOB_BIT,
329 FD_WRITE_BIT,
330 FD_READ_BIT,
331 FD_CLOSE_BIT,
332 6, 7, 8, 9 /* leftovers */
335 /* Flags that make sense only for SOCK_STREAM sockets */
336 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
338 typedef enum {
339 SOCK_SHUTDOWN_ERROR = -1,
340 SOCK_SHUTDOWN_EOF = 0,
341 SOCK_SHUTDOWN_POLLHUP = 1
342 } sock_shutdown_t;
344 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
346 static sock_shutdown_t sock_check_pollhup(void)
348 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
349 int fd[2], n;
350 struct pollfd pfd;
351 char dummy;
353 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
354 if ( shutdown( fd[0], 1 ) ) goto out;
356 pfd.fd = fd[1];
357 pfd.events = POLLIN;
358 pfd.revents = 0;
360 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
361 n = poll( &pfd, 1, 1 );
362 if ( n != 1 ) goto out; /* error or timeout */
363 if ( pfd.revents & POLLHUP )
364 ret = SOCK_SHUTDOWN_POLLHUP;
365 else if ( pfd.revents & POLLIN &&
366 read( fd[1], &dummy, 1 ) == 0 )
367 ret = SOCK_SHUTDOWN_EOF;
369 out:
370 close( fd[0] );
371 close( fd[1] );
372 return ret;
375 void sock_init(void)
377 sock_shutdown_type = sock_check_pollhup();
379 switch ( sock_shutdown_type )
381 case SOCK_SHUTDOWN_EOF:
382 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
383 break;
384 case SOCK_SHUTDOWN_POLLHUP:
385 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
386 break;
387 default:
388 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
389 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
393 static int sock_reselect( struct sock *sock )
395 int ev = sock_get_poll_events( sock->fd );
397 if (debug_level)
398 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
400 if (!sock->polling) /* FIXME: should find a better way to do this */
402 /* previously unconnected socket, is this reselect supposed to connect it? */
403 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
404 /* ok, it is, attach it to the wineserver's main poll loop */
405 sock->polling = 1;
406 allow_fd_caching( sock->fd );
408 /* update condition mask */
409 set_fd_events( sock->fd, ev );
410 return ev;
413 /* wake anybody waiting on the socket event or send the associated message */
414 static void sock_wake_up( struct sock *sock )
416 unsigned int events = sock->pmask & sock->mask;
417 int i;
419 if ( !events ) return;
421 if (sock->event)
423 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
424 set_event( sock->event );
426 if (sock->window)
428 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
429 for (i = 0; i < FD_MAX_EVENTS; i++)
431 int event = event_bitorder[i];
432 if (sock->pmask & (1 << event))
434 lparam_t lparam = (1 << event) | (sock->errors[event] << 16);
435 post_message( sock->window, sock->message, sock->wparam, lparam );
438 sock->pmask = 0;
439 sock_reselect( sock );
443 static inline int sock_error( struct fd *fd )
445 unsigned int optval = 0;
446 socklen_t optlen = sizeof(optval);
448 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
449 return optval;
452 static void free_accept_req( void *private )
454 struct accept_req *req = private;
455 list_remove( &req->entry );
456 if (req->acceptsock)
458 req->acceptsock->accept_recv_req = NULL;
459 release_object( req->acceptsock );
461 release_object( req->async );
462 release_object( req->iosb );
463 release_object( req->sock );
464 free( req );
467 static void fill_accept_output( struct accept_req *req )
469 struct iosb *iosb = req->iosb;
470 union unix_sockaddr unix_addr;
471 struct WS_sockaddr *win_addr;
472 unsigned int remote_len;
473 socklen_t unix_len;
474 int fd, size = 0;
475 char *out_data;
476 int win_len;
478 if (!(out_data = mem_alloc( iosb->out_size ))) return;
480 fd = get_unix_fd( req->acceptsock->fd );
482 if (req->recv_len && (size = recv( fd, out_data, req->recv_len, 0 )) < 0)
484 if (!req->accepted && errno == EWOULDBLOCK)
486 req->accepted = 1;
487 sock_reselect( req->acceptsock );
488 set_error( STATUS_PENDING );
489 return;
492 set_win32_error( sock_get_error( errno ) );
493 free( out_data );
494 return;
497 if (req->local_len)
499 if (req->local_len < sizeof(int))
501 set_error( STATUS_BUFFER_TOO_SMALL );
502 free( out_data );
503 return;
506 unix_len = sizeof(unix_addr);
507 win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + sizeof(int));
508 if (getsockname( fd, &unix_addr.addr, &unix_len ) < 0 ||
509 (win_len = sockaddr_from_unix( &unix_addr, win_addr, req->local_len - sizeof(int) )) < 0)
511 set_win32_error( sock_get_error( errno ) );
512 free( out_data );
513 return;
515 memcpy( out_data + req->recv_len, &win_len, sizeof(int) );
518 unix_len = sizeof(unix_addr);
519 win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + req->local_len + sizeof(int));
520 remote_len = iosb->out_size - req->recv_len - req->local_len;
521 if (getpeername( fd, &unix_addr.addr, &unix_len ) < 0 ||
522 (win_len = sockaddr_from_unix( &unix_addr, win_addr, remote_len - sizeof(int) )) < 0)
524 set_win32_error( sock_get_error( errno ) );
525 free( out_data );
526 return;
528 memcpy( out_data + req->recv_len + req->local_len, &win_len, sizeof(int) );
530 iosb->status = STATUS_SUCCESS;
531 iosb->result = size;
532 iosb->out_data = out_data;
533 set_error( STATUS_ALERTED );
536 static void complete_async_accept( struct sock *sock, struct accept_req *req )
538 struct sock *acceptsock = req->acceptsock;
539 struct async *async = req->async;
541 if (debug_level) fprintf( stderr, "completing accept request for socket %p\n", sock );
543 if (acceptsock)
545 if (!accept_into_socket( sock, acceptsock )) return;
546 fill_accept_output( req );
548 else
550 struct iosb *iosb = req->iosb;
551 obj_handle_t handle;
553 if (!(acceptsock = accept_socket( sock ))) return;
554 handle = alloc_handle_no_access_check( async_get_thread( async )->process, &acceptsock->obj,
555 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
556 acceptsock->wparam = handle;
557 release_object( acceptsock );
558 if (!handle) return;
560 if (!(iosb->out_data = malloc( sizeof(handle) ))) return;
562 iosb->status = STATUS_SUCCESS;
563 iosb->out_size = sizeof(handle);
564 memcpy( iosb->out_data, &handle, sizeof(handle) );
565 set_error( STATUS_ALERTED );
569 static void complete_async_accept_recv( struct accept_req *req )
571 if (debug_level) fprintf( stderr, "completing accept recv request for socket %p\n", req->acceptsock );
573 assert( req->recv_len );
575 fill_accept_output( req );
578 static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
580 if (event & (POLLIN | POLLPRI))
582 struct accept_req *req;
584 LIST_FOR_EACH_ENTRY( req, &sock->accept_list, struct accept_req, entry )
586 if (req->iosb->status == STATUS_PENDING && !req->accepted)
588 complete_async_accept( sock, req );
589 if (get_error() != STATUS_PENDING)
590 async_terminate( req->async, get_error() );
591 break;
595 if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
597 complete_async_accept_recv( sock->accept_recv_req );
598 if (get_error() != STATUS_PENDING)
599 async_terminate( sock->accept_recv_req->async, get_error() );
603 if (is_fd_overlapped( sock->fd ))
605 if (event & (POLLIN|POLLPRI) && async_waiting( &sock->read_q ))
607 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
608 async_wake_up( &sock->read_q, STATUS_ALERTED );
609 event &= ~(POLLIN|POLLPRI);
611 if (event & POLLOUT && async_waiting( &sock->write_q ))
613 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
614 async_wake_up( &sock->write_q, STATUS_ALERTED );
615 event &= ~POLLOUT;
619 if (event & (POLLERR | POLLHUP))
621 int status = sock_get_ntstatus( error );
622 struct accept_req *req, *next;
624 if (!(sock->state & FD_READ))
625 async_wake_up( &sock->read_q, status );
626 if (!(sock->state & FD_WRITE))
627 async_wake_up( &sock->write_q, status );
629 LIST_FOR_EACH_ENTRY_SAFE( req, next, &sock->accept_list, struct accept_req, entry )
631 if (req->iosb->status == STATUS_PENDING)
632 async_terminate( req->async, status );
635 if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
636 async_terminate( sock->accept_recv_req->async, status );
639 return event;
642 static void sock_dispatch_events( struct sock *sock, int prevstate, int event, int error )
644 if (prevstate & FD_CONNECT)
646 sock->pmask |= FD_CONNECT;
647 sock->hmask |= FD_CONNECT;
648 sock->errors[FD_CONNECT_BIT] = sock_get_error( error );
649 goto end;
651 if (prevstate & FD_WINE_LISTENING)
653 sock->pmask |= FD_ACCEPT;
654 sock->hmask |= FD_ACCEPT;
655 sock->errors[FD_ACCEPT_BIT] = sock_get_error( error );
656 goto end;
659 if (event & POLLIN)
661 sock->pmask |= FD_READ;
662 sock->hmask |= FD_READ;
663 sock->errors[FD_READ_BIT] = 0;
666 if (event & POLLOUT)
668 sock->pmask |= FD_WRITE;
669 sock->hmask |= FD_WRITE;
670 sock->errors[FD_WRITE_BIT] = 0;
673 if (event & POLLPRI)
675 sock->pmask |= FD_OOB;
676 sock->hmask |= FD_OOB;
677 sock->errors[FD_OOB_BIT] = 0;
680 if (event & (POLLERR|POLLHUP))
682 sock->pmask |= FD_CLOSE;
683 sock->hmask |= FD_CLOSE;
684 sock->errors[FD_CLOSE_BIT] = sock_get_error( error );
686 end:
687 sock_wake_up( sock );
690 static void sock_poll_event( struct fd *fd, int event )
692 struct sock *sock = get_fd_user( fd );
693 int hangup_seen = 0;
694 int prevstate = sock->state;
695 int error = 0;
697 assert( sock->obj.ops == &sock_ops );
698 if (debug_level)
699 fprintf(stderr, "socket %p select event: %x\n", sock, event);
701 /* we may change event later, remove from loop here */
702 if (event & (POLLERR|POLLHUP)) set_fd_events( sock->fd, -1 );
704 if (sock->state & FD_CONNECT)
706 if (event & (POLLERR|POLLHUP))
708 /* we didn't get connected? */
709 sock->state &= ~FD_CONNECT;
710 event &= ~POLLOUT;
711 error = sock_error( fd );
713 else if (event & POLLOUT)
715 /* we got connected */
716 sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
717 sock->state &= ~FD_CONNECT;
718 sock->connect_time = current_time;
721 else if (sock->state & FD_WINE_LISTENING)
723 /* listening */
724 if (event & (POLLERR|POLLHUP))
725 error = sock_error( fd );
727 else
729 /* normal data flow */
730 if (sock->type == WS_SOCK_STREAM && (event & POLLIN))
732 char dummy;
733 int nr;
735 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
736 * has been closed, so we need to check for it explicitly here */
737 nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
738 if ( nr == 0 )
740 hangup_seen = 1;
741 event &= ~POLLIN;
743 else if ( nr < 0 )
745 event &= ~POLLIN;
746 /* EAGAIN can happen if an async recv() falls between the server's poll()
747 call and the invocation of this routine */
748 if ( errno != EAGAIN )
750 error = errno;
751 event |= POLLERR;
752 if ( debug_level )
753 fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
758 if ( (hangup_seen || event & (POLLHUP|POLLERR)) && (sock->state & (FD_READ|FD_WRITE)) )
760 error = error ? error : sock_error( fd );
761 if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
762 sock->state &= ~FD_WRITE;
763 sock->state &= ~FD_READ;
765 if (debug_level)
766 fprintf(stderr, "socket %p aborted by error %d, event: %x\n", sock, error, event);
769 if (hangup_seen)
770 event |= POLLHUP;
773 event = sock_dispatch_asyncs( sock, event, error );
774 sock_dispatch_events( sock, prevstate, event, error );
776 /* if anyone is stupid enough to wait on the socket object itself,
777 * maybe we should wake them up too, just in case? */
778 wake_up( &sock->obj, 0 );
780 sock_reselect( sock );
783 static void sock_dump( struct object *obj, int verbose )
785 struct sock *sock = (struct sock *)obj;
786 assert( obj->ops == &sock_ops );
787 fprintf( stderr, "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
788 sock->fd, sock->state,
789 sock->mask, sock->pmask, sock->hmask );
792 static int sock_signaled( struct object *obj, struct wait_queue_entry *entry )
794 struct sock *sock = (struct sock *)obj;
795 assert( obj->ops == &sock_ops );
797 return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
800 static int sock_get_poll_events( struct fd *fd )
802 struct sock *sock = get_fd_user( fd );
803 unsigned int mask = sock->mask & ~sock->hmask;
804 unsigned int smask = sock->state & mask;
805 int ev = 0;
807 assert( sock->obj.ops == &sock_ops );
809 if (sock->state & FD_CONNECT)
810 /* connecting, wait for writable */
811 return POLLOUT;
813 if (!list_empty( &sock->accept_list ) || sock->accept_recv_req )
815 ev |= POLLIN | POLLPRI;
817 else if (async_queued( &sock->read_q ))
819 if (async_waiting( &sock->read_q )) ev |= POLLIN | POLLPRI;
821 else if (smask & FD_READ || (sock->state & FD_WINE_LISTENING && mask & FD_ACCEPT))
822 ev |= POLLIN | POLLPRI;
823 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
824 else if (sock->type == WS_SOCK_STREAM && (sock->state & FD_READ) && (mask & FD_CLOSE) &&
825 !(sock->hmask & FD_READ))
826 ev |= POLLIN;
828 if (async_queued( &sock->write_q ))
830 if (async_waiting( &sock->write_q )) ev |= POLLOUT;
832 else if (smask & FD_WRITE)
833 ev |= POLLOUT;
835 return ev;
838 static enum server_fd_type sock_get_fd_type( struct fd *fd )
840 return FD_TYPE_SOCKET;
843 static void sock_queue_async( struct fd *fd, struct async *async, int type, int count )
845 struct sock *sock = get_fd_user( fd );
846 struct async_queue *queue;
848 assert( sock->obj.ops == &sock_ops );
850 switch (type)
852 case ASYNC_TYPE_READ:
853 queue = &sock->read_q;
854 break;
855 case ASYNC_TYPE_WRITE:
856 queue = &sock->write_q;
857 break;
858 default:
859 set_error( STATUS_INVALID_PARAMETER );
860 return;
863 if ( ( !( sock->state & (FD_READ|FD_CONNECT|FD_WINE_LISTENING) ) && type == ASYNC_TYPE_READ ) ||
864 ( !( sock->state & (FD_WRITE|FD_CONNECT) ) && type == ASYNC_TYPE_WRITE ) )
866 set_error( STATUS_PIPE_DISCONNECTED );
867 return;
870 queue_async( queue, async );
871 sock_reselect( sock );
873 set_error( STATUS_PENDING );
876 static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
878 struct sock *sock = get_fd_user( fd );
880 /* ignore reselect on ifchange queue */
881 if (&sock->ifchange_q != queue)
882 sock_reselect( sock );
885 static struct fd *sock_get_fd( struct object *obj )
887 struct sock *sock = (struct sock *)obj;
888 return (struct fd *)grab_object( sock->fd );
891 static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
893 struct sock *sock = (struct sock *)obj;
894 struct accept_req *req, *next;
896 if (sock->obj.handle_count == 1) /* last handle */
898 if (sock->accept_recv_req)
899 async_terminate( sock->accept_recv_req->async, STATUS_CANCELLED );
901 LIST_FOR_EACH_ENTRY_SAFE( req, next, &sock->accept_list, struct accept_req, entry )
902 async_terminate( req->async, STATUS_CANCELLED );
905 return fd_close_handle( obj, process, handle );
908 static void sock_destroy( struct object *obj )
910 struct sock *sock = (struct sock *)obj;
912 assert( obj->ops == &sock_ops );
914 /* FIXME: special socket shutdown stuff? */
916 if ( sock->deferred )
917 release_object( sock->deferred );
919 async_wake_up( &sock->ifchange_q, STATUS_CANCELLED );
920 sock_release_ifchange( sock );
921 free_async_queue( &sock->read_q );
922 free_async_queue( &sock->write_q );
923 free_async_queue( &sock->ifchange_q );
924 free_async_queue( &sock->accept_q );
925 if (sock->event) release_object( sock->event );
926 if (sock->fd)
928 /* shut the socket down to force pending poll() calls in the client to return */
929 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
930 release_object( sock->fd );
934 static struct sock *create_socket(void)
936 struct sock *sock;
938 if (!(sock = alloc_object( &sock_ops ))) return NULL;
939 sock->fd = NULL;
940 sock->state = 0;
941 sock->mask = 0;
942 sock->hmask = 0;
943 sock->pmask = 0;
944 sock->polling = 0;
945 sock->flags = 0;
946 sock->proto = 0;
947 sock->type = 0;
948 sock->family = 0;
949 sock->event = NULL;
950 sock->window = 0;
951 sock->message = 0;
952 sock->wparam = 0;
953 sock->connect_time = 0;
954 sock->deferred = NULL;
955 sock->ifchange_obj = NULL;
956 sock->accept_recv_req = NULL;
957 init_async_queue( &sock->read_q );
958 init_async_queue( &sock->write_q );
959 init_async_queue( &sock->ifchange_q );
960 init_async_queue( &sock->accept_q );
961 memset( sock->errors, 0, sizeof(sock->errors) );
962 list_init( &sock->accept_list );
963 return sock;
966 static int get_unix_family( int family )
968 switch (family)
970 case WS_AF_INET: return AF_INET;
971 case WS_AF_INET6: return AF_INET6;
972 #ifdef HAS_IPX
973 case WS_AF_IPX: return AF_IPX;
974 #endif
975 #ifdef AF_IRDA
976 case WS_AF_IRDA: return AF_IRDA;
977 #endif
978 case WS_AF_UNSPEC: return AF_UNSPEC;
979 default: return -1;
983 static int get_unix_type( int type )
985 switch (type)
987 case WS_SOCK_DGRAM: return SOCK_DGRAM;
988 case WS_SOCK_RAW: return SOCK_RAW;
989 case WS_SOCK_STREAM: return SOCK_STREAM;
990 default: return -1;
994 static int get_unix_protocol( int protocol )
996 if (protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
997 return protocol;
999 switch (protocol)
1001 case WS_IPPROTO_ICMP: return IPPROTO_ICMP;
1002 case WS_IPPROTO_IGMP: return IPPROTO_IGMP;
1003 case WS_IPPROTO_IP: return IPPROTO_IP;
1004 case WS_IPPROTO_IPIP: return IPPROTO_IPIP;
1005 case WS_IPPROTO_IPV6: return IPPROTO_IPV6;
1006 case WS_IPPROTO_RAW: return IPPROTO_RAW;
1007 case WS_IPPROTO_TCP: return IPPROTO_TCP;
1008 case WS_IPPROTO_UDP: return IPPROTO_UDP;
1009 default: return -1;
1013 static void set_dont_fragment( int fd, int level, int value )
1015 int optname;
1017 if (level == IPPROTO_IP)
1019 #ifdef IP_DONTFRAG
1020 optname = IP_DONTFRAG;
1021 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1022 optname = IP_MTU_DISCOVER;
1023 value = value ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
1024 #else
1025 return;
1026 #endif
1028 else
1030 #ifdef IPV6_DONTFRAG
1031 optname = IPV6_DONTFRAG;
1032 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1033 optname = IPV6_MTU_DISCOVER;
1034 value = value ? IPV6_PMTUDISC_DO : IPV6_PMTUDISC_DONT;
1035 #else
1036 return;
1037 #endif
1040 setsockopt( fd, level, optname, &value, sizeof(value) );
1043 static int init_socket( struct sock *sock, int family, int type, int protocol, unsigned int flags )
1045 unsigned int options = 0;
1046 int sockfd, unix_type, unix_family, unix_protocol;
1048 unix_family = get_unix_family( family );
1049 unix_type = get_unix_type( type );
1050 unix_protocol = get_unix_protocol( protocol );
1052 if (unix_protocol < 0)
1054 if (type && unix_type < 0)
1055 set_win32_error( WSAESOCKTNOSUPPORT );
1056 else
1057 set_win32_error( WSAEPROTONOSUPPORT );
1058 return -1;
1060 if (unix_family < 0)
1062 if (family >= 0 && unix_type < 0)
1063 set_win32_error( WSAESOCKTNOSUPPORT );
1064 else
1065 set_win32_error( WSAEAFNOSUPPORT );
1066 return -1;
1069 sockfd = socket( unix_family, unix_type, unix_protocol );
1070 if (sockfd == -1)
1072 if (errno == EINVAL) set_win32_error( WSAESOCKTNOSUPPORT );
1073 else set_win32_error( sock_get_error( errno ));
1074 return -1;
1076 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
1078 if (family == WS_AF_IPX && protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
1080 #ifdef HAS_IPX
1081 int ipx_type = protocol - WS_NSPROTO_IPX;
1083 #ifdef SOL_IPX
1084 setsockopt( sockfd, SOL_IPX, IPX_TYPE, &ipx_type, sizeof(ipx_type) );
1085 #else
1086 struct ipx val;
1087 /* Should we retrieve val using a getsockopt call and then
1088 * set the modified one? */
1089 val.ipx_pt = ipx_type;
1090 setsockopt( sockfd, 0, SO_DEFAULT_HEADERS, &val, sizeof(val) );
1091 #endif
1092 #endif
1095 if (unix_family == AF_INET || unix_family == AF_INET6)
1097 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1098 if (unix_type == SOCK_DGRAM || unix_type == SOCK_RAW) /* in Linux the global default can be enabled */
1099 set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, FALSE );
1100 else if (unix_type == SOCK_STREAM)
1101 set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, TRUE );
1104 #ifdef IPV6_V6ONLY
1105 if (unix_family == AF_INET6)
1107 static const int enable = 1;
1108 setsockopt( sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &enable, sizeof(enable) );
1110 #endif
1112 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
1113 sock->flags = flags;
1114 sock->proto = protocol;
1115 sock->type = type;
1116 sock->family = family;
1118 if (sock->fd)
1120 options = get_fd_options( sock->fd );
1121 release_object( sock->fd );
1124 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj, options )))
1126 return -1;
1128 sock_reselect( sock );
1129 clear_error();
1130 return 0;
1133 /* accepts a socket and inits it */
1134 static int accept_new_fd( struct sock *sock )
1137 /* Try to accept(2). We can't be safe that this an already connected socket
1138 * or that accept() is allowed on it. In those cases we will get -1/errno
1139 * return.
1141 struct sockaddr saddr;
1142 socklen_t slen = sizeof(saddr);
1143 int acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen );
1144 if (acceptfd != -1)
1145 fcntl( acceptfd, F_SETFL, O_NONBLOCK );
1146 else
1147 set_win32_error( sock_get_error( errno ));
1148 return acceptfd;
1151 /* accept a socket (creates a new fd) */
1152 static struct sock *accept_socket( struct sock *sock )
1154 struct sock *acceptsock;
1155 int acceptfd;
1157 if (get_unix_fd( sock->fd ) == -1) return NULL;
1159 if ( sock->deferred )
1161 acceptsock = sock->deferred;
1162 sock->deferred = NULL;
1164 else
1166 if ((acceptfd = accept_new_fd( sock )) == -1) return NULL;
1167 if (!(acceptsock = create_socket()))
1169 close( acceptfd );
1170 return NULL;
1173 /* newly created socket gets the same properties of the listening socket */
1174 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
1175 if (sock->state & FD_WINE_NONBLOCKING)
1176 acceptsock->state |= FD_WINE_NONBLOCKING;
1177 acceptsock->mask = sock->mask;
1178 acceptsock->proto = sock->proto;
1179 acceptsock->type = sock->type;
1180 acceptsock->family = sock->family;
1181 acceptsock->window = sock->window;
1182 acceptsock->message = sock->message;
1183 acceptsock->connect_time = current_time;
1184 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
1185 acceptsock->flags = sock->flags;
1186 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
1187 get_fd_options( sock->fd ) )))
1189 release_object( acceptsock );
1190 return NULL;
1193 clear_error();
1194 sock->pmask &= ~FD_ACCEPT;
1195 sock->hmask &= ~FD_ACCEPT;
1196 sock_reselect( sock );
1197 return acceptsock;
1200 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
1202 int acceptfd;
1203 struct fd *newfd;
1205 if (get_unix_fd( sock->fd ) == -1) return FALSE;
1207 if ( sock->deferred )
1209 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
1210 get_fd_options( acceptsock->fd ) );
1211 if ( !newfd )
1212 return FALSE;
1214 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
1216 release_object( sock->deferred );
1217 sock->deferred = NULL;
1219 else
1221 if ((acceptfd = accept_new_fd( sock )) == -1)
1222 return FALSE;
1224 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
1225 get_fd_options( acceptsock->fd ) )))
1226 return FALSE;
1229 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
1230 acceptsock->hmask = 0;
1231 acceptsock->pmask = 0;
1232 acceptsock->polling = 0;
1233 acceptsock->proto = sock->proto;
1234 acceptsock->type = sock->type;
1235 acceptsock->family = sock->family;
1236 acceptsock->wparam = 0;
1237 acceptsock->deferred = NULL;
1238 acceptsock->connect_time = current_time;
1239 fd_copy_completion( acceptsock->fd, newfd );
1240 release_object( acceptsock->fd );
1241 acceptsock->fd = newfd;
1243 clear_error();
1244 sock->pmask &= ~FD_ACCEPT;
1245 sock->hmask &= ~FD_ACCEPT;
1246 sock_reselect( sock );
1248 return TRUE;
1251 /* return an errno value mapped to a WSA error */
1252 static unsigned int sock_get_error( int err )
1254 switch (err)
1256 case EINTR: return WSAEINTR;
1257 case EBADF: return WSAEBADF;
1258 case EPERM:
1259 case EACCES: return WSAEACCES;
1260 case EFAULT: return WSAEFAULT;
1261 case EINVAL: return WSAEINVAL;
1262 case EMFILE: return WSAEMFILE;
1263 case EWOULDBLOCK: return WSAEWOULDBLOCK;
1264 case EINPROGRESS: return WSAEINPROGRESS;
1265 case EALREADY: return WSAEALREADY;
1266 case ENOTSOCK: return WSAENOTSOCK;
1267 case EDESTADDRREQ: return WSAEDESTADDRREQ;
1268 case EMSGSIZE: return WSAEMSGSIZE;
1269 case EPROTOTYPE: return WSAEPROTOTYPE;
1270 case ENOPROTOOPT: return WSAENOPROTOOPT;
1271 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
1272 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
1273 case EOPNOTSUPP: return WSAEOPNOTSUPP;
1274 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
1275 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
1276 case EADDRINUSE: return WSAEADDRINUSE;
1277 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
1278 case ENETDOWN: return WSAENETDOWN;
1279 case ENETUNREACH: return WSAENETUNREACH;
1280 case ENETRESET: return WSAENETRESET;
1281 case ECONNABORTED: return WSAECONNABORTED;
1282 case EPIPE:
1283 case ECONNRESET: return WSAECONNRESET;
1284 case ENOBUFS: return WSAENOBUFS;
1285 case EISCONN: return WSAEISCONN;
1286 case ENOTCONN: return WSAENOTCONN;
1287 case ESHUTDOWN: return WSAESHUTDOWN;
1288 case ETOOMANYREFS: return WSAETOOMANYREFS;
1289 case ETIMEDOUT: return WSAETIMEDOUT;
1290 case ECONNREFUSED: return WSAECONNREFUSED;
1291 case ELOOP: return WSAELOOP;
1292 case ENAMETOOLONG: return WSAENAMETOOLONG;
1293 case EHOSTDOWN: return WSAEHOSTDOWN;
1294 case EHOSTUNREACH: return WSAEHOSTUNREACH;
1295 case ENOTEMPTY: return WSAENOTEMPTY;
1296 #ifdef EPROCLIM
1297 case EPROCLIM: return WSAEPROCLIM;
1298 #endif
1299 #ifdef EUSERS
1300 case EUSERS: return WSAEUSERS;
1301 #endif
1302 #ifdef EDQUOT
1303 case EDQUOT: return WSAEDQUOT;
1304 #endif
1305 #ifdef ESTALE
1306 case ESTALE: return WSAESTALE;
1307 #endif
1308 #ifdef EREMOTE
1309 case EREMOTE: return WSAEREMOTE;
1310 #endif
1312 case 0: return 0;
1313 default:
1314 errno = err;
1315 perror("wineserver: sock_get_error() can't map error");
1316 return WSAEFAULT;
1320 static int sock_get_ntstatus( int err )
1322 switch ( err )
1324 case EBADF: return STATUS_INVALID_HANDLE;
1325 case EBUSY: return STATUS_DEVICE_BUSY;
1326 case EPERM:
1327 case EACCES: return STATUS_ACCESS_DENIED;
1328 case EFAULT: return STATUS_NO_MEMORY;
1329 case EINVAL: return STATUS_INVALID_PARAMETER;
1330 case ENFILE:
1331 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
1332 case EWOULDBLOCK: return STATUS_CANT_WAIT;
1333 case EINPROGRESS: return STATUS_PENDING;
1334 case EALREADY: return STATUS_NETWORK_BUSY;
1335 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
1336 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
1337 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
1338 case EPROTONOSUPPORT:
1339 case ESOCKTNOSUPPORT:
1340 case EPFNOSUPPORT:
1341 case EAFNOSUPPORT:
1342 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
1343 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
1344 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
1345 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
1346 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
1347 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
1348 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
1349 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
1350 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
1351 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
1352 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
1353 case ENETDOWN: return STATUS_NETWORK_BUSY;
1354 case EPIPE:
1355 case ECONNRESET: return STATUS_CONNECTION_RESET;
1356 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
1358 case 0: return STATUS_SUCCESS;
1359 default:
1360 errno = err;
1361 perror("wineserver: sock_get_ntstatus() can't map error");
1362 return STATUS_UNSUCCESSFUL;
1366 static struct accept_req *alloc_accept_req( struct sock *sock, struct sock *acceptsock, struct async *async,
1367 const struct afd_accept_into_params *params )
1369 struct accept_req *req = mem_alloc( sizeof(*req) );
1371 if (req)
1373 req->async = (struct async *)grab_object( async );
1374 req->iosb = async_get_iosb( async );
1375 req->sock = (struct sock *)grab_object( sock );
1376 req->acceptsock = acceptsock;
1377 if (acceptsock) grab_object( acceptsock );
1378 req->accepted = 0;
1379 req->recv_len = 0;
1380 req->local_len = 0;
1381 if (params)
1383 req->recv_len = params->recv_len;
1384 req->local_len = params->local_len;
1387 return req;
1390 static int sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1392 struct sock *sock = get_fd_user( fd );
1394 assert( sock->obj.ops == &sock_ops );
1396 if (get_unix_fd( fd ) == -1 && code != IOCTL_AFD_CREATE) return 0;
1398 switch(code)
1400 case IOCTL_AFD_CREATE:
1402 const struct afd_create_params *params = get_req_data();
1404 if (get_req_data_size() != sizeof(*params))
1406 set_error( STATUS_INVALID_PARAMETER );
1407 return 0;
1409 init_socket( sock, params->family, params->type, params->protocol, params->flags );
1410 return 0;
1413 case IOCTL_AFD_ACCEPT:
1415 struct sock *acceptsock;
1416 obj_handle_t handle;
1418 if (get_reply_max_size() != sizeof(handle))
1420 set_error( STATUS_BUFFER_TOO_SMALL );
1421 return 0;
1424 if (!(acceptsock = accept_socket( sock )))
1426 struct accept_req *req;
1428 if (sock->state & FD_WINE_NONBLOCKING) return 0;
1429 if (get_error() != (0xc0010000 | WSAEWOULDBLOCK)) return 0;
1431 if (!(req = alloc_accept_req( sock, NULL, async, NULL ))) return 0;
1432 list_add_tail( &sock->accept_list, &req->entry );
1434 async_set_completion_callback( async, free_accept_req, req );
1435 queue_async( &sock->accept_q, async );
1436 sock_reselect( sock );
1437 set_error( STATUS_PENDING );
1438 return 1;
1440 handle = alloc_handle( current->process, &acceptsock->obj,
1441 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
1442 acceptsock->wparam = handle;
1443 release_object( acceptsock );
1444 set_reply_data( &handle, sizeof(handle) );
1445 return 0;
1448 case IOCTL_AFD_ACCEPT_INTO:
1450 static const int access = FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | FILE_READ_DATA;
1451 const struct afd_accept_into_params *params = get_req_data();
1452 struct sock *acceptsock;
1453 unsigned int remote_len;
1454 struct accept_req *req;
1456 if (get_req_data_size() != sizeof(*params) ||
1457 get_reply_max_size() < params->recv_len ||
1458 get_reply_max_size() - params->recv_len < params->local_len)
1460 set_error( STATUS_BUFFER_TOO_SMALL );
1461 return 0;
1464 remote_len = get_reply_max_size() - params->recv_len - params->local_len;
1465 if (remote_len < sizeof(int))
1467 set_error( STATUS_INVALID_PARAMETER );
1468 return 0;
1471 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, params->accept_handle, access, &sock_ops )))
1472 return 0;
1474 if (acceptsock->accept_recv_req)
1476 release_object( acceptsock );
1477 set_win32_error( WSAEINVAL );
1478 return 0;
1481 if (!(req = alloc_accept_req( sock, acceptsock, async, params )))
1483 release_object( acceptsock );
1484 return 0;
1486 list_add_tail( &sock->accept_list, &req->entry );
1487 acceptsock->accept_recv_req = req;
1488 release_object( acceptsock );
1490 acceptsock->wparam = params->accept_handle;
1491 async_set_completion_callback( async, free_accept_req, req );
1492 queue_async( &sock->accept_q, async );
1493 sock_reselect( sock );
1494 set_error( STATUS_PENDING );
1495 return 1;
1498 case IOCTL_AFD_ADDRESS_LIST_CHANGE:
1499 if ((sock->state & FD_WINE_NONBLOCKING) && async_is_blocking( async ))
1501 set_win32_error( WSAEWOULDBLOCK );
1502 return 0;
1504 if (!sock_get_ifchange( sock )) return 0;
1505 queue_async( &sock->ifchange_q, async );
1506 set_error( STATUS_PENDING );
1507 return 1;
1509 default:
1510 set_error( STATUS_NOT_SUPPORTED );
1511 return 0;
1515 #ifdef HAVE_LINUX_RTNETLINK_H
1517 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
1518 static struct object *ifchange_object;
1520 static void ifchange_dump( struct object *obj, int verbose );
1521 static struct fd *ifchange_get_fd( struct object *obj );
1522 static void ifchange_destroy( struct object *obj );
1524 static int ifchange_get_poll_events( struct fd *fd );
1525 static void ifchange_poll_event( struct fd *fd, int event );
1527 struct ifchange
1529 struct object obj; /* object header */
1530 struct fd *fd; /* interface change file descriptor */
1531 struct list sockets; /* list of sockets to send interface change notifications */
1534 static const struct object_ops ifchange_ops =
1536 sizeof(struct ifchange), /* size */
1537 &no_type, /* type */
1538 ifchange_dump, /* dump */
1539 add_queue, /* add_queue */
1540 NULL, /* remove_queue */
1541 NULL, /* signaled */
1542 no_satisfied, /* satisfied */
1543 no_signal, /* signal */
1544 ifchange_get_fd, /* get_fd */
1545 default_map_access, /* map_access */
1546 default_get_sd, /* get_sd */
1547 default_set_sd, /* set_sd */
1548 no_get_full_name, /* get_full_name */
1549 no_lookup_name, /* lookup_name */
1550 no_link_name, /* link_name */
1551 NULL, /* unlink_name */
1552 no_open_file, /* open_file */
1553 no_kernel_obj_list, /* get_kernel_obj_list */
1554 no_close_handle, /* close_handle */
1555 ifchange_destroy /* destroy */
1558 static const struct fd_ops ifchange_fd_ops =
1560 ifchange_get_poll_events, /* get_poll_events */
1561 ifchange_poll_event, /* poll_event */
1562 NULL, /* get_fd_type */
1563 no_fd_read, /* read */
1564 no_fd_write, /* write */
1565 no_fd_flush, /* flush */
1566 no_fd_get_file_info, /* get_file_info */
1567 no_fd_get_volume_info, /* get_volume_info */
1568 no_fd_ioctl, /* ioctl */
1569 NULL, /* queue_async */
1570 NULL /* reselect_async */
1573 static void ifchange_dump( struct object *obj, int verbose )
1575 assert( obj->ops == &ifchange_ops );
1576 fprintf( stderr, "Interface change\n" );
1579 static struct fd *ifchange_get_fd( struct object *obj )
1581 struct ifchange *ifchange = (struct ifchange *)obj;
1582 return (struct fd *)grab_object( ifchange->fd );
1585 static void ifchange_destroy( struct object *obj )
1587 struct ifchange *ifchange = (struct ifchange *)obj;
1588 assert( obj->ops == &ifchange_ops );
1590 release_object( ifchange->fd );
1592 /* reset the global ifchange object so that it will be recreated if it is needed again */
1593 assert( obj == ifchange_object );
1594 ifchange_object = NULL;
1597 static int ifchange_get_poll_events( struct fd *fd )
1599 return POLLIN;
1602 /* wake up all the sockets waiting for a change notification event */
1603 static void ifchange_wake_up( struct object *obj, unsigned int status )
1605 struct ifchange *ifchange = (struct ifchange *)obj;
1606 struct list *ptr, *next;
1607 assert( obj->ops == &ifchange_ops );
1608 assert( obj == ifchange_object );
1610 LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
1612 struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
1614 assert( sock->ifchange_obj );
1615 async_wake_up( &sock->ifchange_q, status ); /* issue ifchange notification for the socket */
1616 sock_release_ifchange( sock ); /* remove socket from list and decrement ifchange refcount */
1620 static void ifchange_poll_event( struct fd *fd, int event )
1622 struct object *ifchange = get_fd_user( fd );
1623 unsigned int status = STATUS_PENDING;
1624 char buffer[PIPE_BUF];
1625 int r;
1627 r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
1628 if (r < 0)
1630 if (errno == EWOULDBLOCK || (EWOULDBLOCK != EAGAIN && errno == EAGAIN))
1631 return; /* retry when poll() says the socket is ready */
1632 status = sock_get_ntstatus( errno );
1634 else if (r > 0)
1636 struct nlmsghdr *nlh;
1638 for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
1640 if (nlh->nlmsg_type == NLMSG_DONE)
1641 break;
1642 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
1643 status = STATUS_SUCCESS;
1646 else status = STATUS_CANCELLED;
1648 if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
1651 #endif
1653 /* we only need one of these interface notification objects, all of the sockets dependent upon
1654 * it will wake up when a notification event occurs */
1655 static struct object *get_ifchange( void )
1657 #ifdef HAVE_LINUX_RTNETLINK_H
1658 struct ifchange *ifchange;
1659 struct sockaddr_nl addr;
1660 int unix_fd;
1662 if (ifchange_object)
1664 /* increment the refcount for each socket that uses the ifchange object */
1665 return grab_object( ifchange_object );
1668 /* create the socket we need for processing interface change notifications */
1669 unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
1670 if (unix_fd == -1)
1672 set_win32_error( sock_get_error( errno ));
1673 return NULL;
1675 fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
1676 memset( &addr, 0, sizeof(addr) );
1677 addr.nl_family = AF_NETLINK;
1678 addr.nl_groups = RTMGRP_IPV4_IFADDR;
1679 /* bind the socket to the special netlink kernel interface */
1680 if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
1682 close( unix_fd );
1683 set_win32_error( sock_get_error( errno ));
1684 return NULL;
1686 if (!(ifchange = alloc_object( &ifchange_ops )))
1688 close( unix_fd );
1689 set_error( STATUS_NO_MEMORY );
1690 return NULL;
1692 list_init( &ifchange->sockets );
1693 if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
1695 release_object( ifchange );
1696 set_error( STATUS_NO_MEMORY );
1697 return NULL;
1699 set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
1701 /* the ifchange object is now successfully configured */
1702 ifchange_object = &ifchange->obj;
1703 return &ifchange->obj;
1704 #else
1705 set_error( STATUS_NOT_SUPPORTED );
1706 return NULL;
1707 #endif
1710 /* add the socket to the interface change notification list */
1711 static void ifchange_add_sock( struct object *obj, struct sock *sock )
1713 #ifdef HAVE_LINUX_RTNETLINK_H
1714 struct ifchange *ifchange = (struct ifchange *)obj;
1716 list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
1717 #endif
1720 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1721 static struct object *sock_get_ifchange( struct sock *sock )
1723 struct object *ifchange;
1725 if (sock->ifchange_obj) /* reuse existing ifchange_obj for this socket */
1726 return sock->ifchange_obj;
1728 if (!(ifchange = get_ifchange()))
1729 return NULL;
1731 /* add the socket to the ifchange notification list */
1732 ifchange_add_sock( ifchange, sock );
1733 sock->ifchange_obj = ifchange;
1734 return ifchange;
1737 /* destroy an existing ifchange queue for a specific socket */
1738 static void sock_release_ifchange( struct sock *sock )
1740 if (sock->ifchange_obj)
1742 list_remove( &sock->ifchange_entry );
1743 release_object( sock->ifchange_obj );
1744 sock->ifchange_obj = NULL;
1748 static void socket_device_dump( struct object *obj, int verbose );
1749 static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
1750 unsigned int attr, struct object *root );
1751 static struct object *socket_device_open_file( struct object *obj, unsigned int access,
1752 unsigned int sharing, unsigned int options );
1754 static const struct object_ops socket_device_ops =
1756 sizeof(struct object), /* size */
1757 &device_type, /* type */
1758 socket_device_dump, /* dump */
1759 no_add_queue, /* add_queue */
1760 NULL, /* remove_queue */
1761 NULL, /* signaled */
1762 no_satisfied, /* satisfied */
1763 no_signal, /* signal */
1764 no_get_fd, /* get_fd */
1765 default_map_access, /* map_access */
1766 default_get_sd, /* get_sd */
1767 default_set_sd, /* set_sd */
1768 default_get_full_name, /* get_full_name */
1769 socket_device_lookup_name, /* lookup_name */
1770 directory_link_name, /* link_name */
1771 default_unlink_name, /* unlink_name */
1772 socket_device_open_file, /* open_file */
1773 no_kernel_obj_list, /* get_kernel_obj_list */
1774 no_close_handle, /* close_handle */
1775 no_destroy /* destroy */
1778 static void socket_device_dump( struct object *obj, int verbose )
1780 fputs( "Socket device\n", stderr );
1783 static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
1784 unsigned int attr, struct object *root )
1786 return NULL;
1789 static struct object *socket_device_open_file( struct object *obj, unsigned int access,
1790 unsigned int sharing, unsigned int options )
1792 struct sock *sock;
1794 if (!(sock = create_socket())) return NULL;
1795 if (!(sock->fd = alloc_pseudo_fd( &sock_fd_ops, &sock->obj, options )))
1797 release_object( sock );
1798 return NULL;
1800 return &sock->obj;
1803 struct object *create_socket_device( struct object *root, const struct unicode_str *name,
1804 unsigned int attr, const struct security_descriptor *sd )
1806 return create_named_object( root, &socket_device_ops, name, attr, sd );
1809 /* set socket event parameters */
1810 DECL_HANDLER(set_socket_event)
1812 struct sock *sock;
1813 struct event *old_event;
1815 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1816 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
1817 if (get_unix_fd( sock->fd ) == -1) return;
1818 old_event = sock->event;
1819 sock->mask = req->mask;
1820 sock->hmask &= ~req->mask; /* re-enable held events */
1821 sock->event = NULL;
1822 sock->window = req->window;
1823 sock->message = req->msg;
1824 sock->wparam = req->handle; /* wparam is the socket handle */
1825 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
1827 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
1829 sock_reselect( sock );
1831 sock->state |= FD_WINE_NONBLOCKING;
1833 /* if a network event is pending, signal the event object
1834 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1835 before a WSAEventSelect() was done on it.
1836 (when dealing with Asynchronous socket) */
1837 sock_wake_up( sock );
1839 if (old_event) release_object( old_event ); /* we're through with it */
1840 release_object( &sock->obj );
1843 /* get socket event parameters */
1844 DECL_HANDLER(get_socket_event)
1846 struct sock *sock;
1848 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1849 FILE_READ_ATTRIBUTES, &sock_ops ))) return;
1850 if (get_unix_fd( sock->fd ) == -1) return;
1851 reply->mask = sock->mask;
1852 reply->pmask = sock->pmask;
1853 reply->state = sock->state;
1854 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
1856 if (req->service)
1858 if (req->c_event)
1860 struct event *cevent = get_event_obj( current->process, req->c_event,
1861 EVENT_MODIFY_STATE );
1862 if (cevent)
1864 reset_event( cevent );
1865 release_object( cevent );
1868 sock->pmask = 0;
1869 sock_reselect( sock );
1871 release_object( &sock->obj );
1874 /* re-enable pending socket events */
1875 DECL_HANDLER(enable_socket_event)
1877 struct sock *sock;
1879 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
1880 FILE_WRITE_ATTRIBUTES, &sock_ops)))
1881 return;
1883 if (get_unix_fd( sock->fd ) == -1) return;
1885 /* for event-based notification, windows erases stale events */
1886 sock->pmask &= ~req->mask;
1888 sock->hmask &= ~req->mask;
1889 sock->state |= req->sstate;
1890 sock->state &= ~req->cstate;
1891 if (sock->type != WS_SOCK_STREAM) sock->state &= ~STREAM_FLAG_MASK;
1893 sock_reselect( sock );
1895 release_object( &sock->obj );
1898 DECL_HANDLER(set_socket_deferred)
1900 struct sock *sock, *acceptsock;
1902 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
1903 if ( !sock )
1904 return;
1906 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
1907 if ( !acceptsock )
1909 release_object( sock );
1910 return;
1912 sock->deferred = acceptsock;
1913 release_object( sock );
1916 DECL_HANDLER(get_socket_info)
1918 struct sock *sock;
1920 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1921 if (!sock) return;
1923 if (get_unix_fd( sock->fd ) == -1) return;
1925 reply->family = sock->family;
1926 reply->type = sock->type;
1927 reply->protocol = sock->proto;
1929 release_object( &sock->obj );