msvcp: Fix some spec file discrepancies.
[wine.git] / server / sock.c
blob5f207e7f1804192ec93f716805a74e7b047065df
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 sock *acceptsock;
130 int accepted;
131 unsigned int recv_len, local_len;
134 struct sock
136 struct object obj; /* object header */
137 struct fd *fd; /* socket file descriptor */
138 unsigned int state; /* status bits */
139 unsigned int mask; /* event mask */
140 unsigned int hmask; /* held (blocked) events */
141 unsigned int pmask; /* pending events */
142 unsigned int flags; /* socket flags */
143 int polling; /* is socket being polled? */
144 unsigned short proto; /* socket protocol */
145 unsigned short type; /* socket type */
146 unsigned short family; /* socket family */
147 struct event *event; /* event object */
148 user_handle_t window; /* window to send the message to */
149 unsigned int message; /* message to send */
150 obj_handle_t wparam; /* message wparam (socket handle) */
151 unsigned int errors[FD_MAX_EVENTS]; /* event errors */
152 timeout_t connect_time;/* time the socket was connected */
153 struct sock *deferred; /* socket that waits for a deferred accept */
154 struct async_queue read_q; /* queue for asynchronous reads */
155 struct async_queue write_q; /* queue for asynchronous writes */
156 struct async_queue ifchange_q; /* queue for interface change notifications */
157 struct async_queue accept_q; /* queue for asynchronous accepts */
158 struct object *ifchange_obj; /* the interface change notification object */
159 struct list ifchange_entry; /* entry in ifchange notification list */
160 struct list accept_list; /* list of pending accept requests */
161 struct accept_req *accept_recv_req; /* pending accept-into request which will recv on this socket */
164 static void sock_dump( struct object *obj, int verbose );
165 static int sock_signaled( struct object *obj, struct wait_queue_entry *entry );
166 static struct fd *sock_get_fd( struct object *obj );
167 static void sock_destroy( struct object *obj );
168 static struct object *sock_get_ifchange( struct sock *sock );
169 static void sock_release_ifchange( struct sock *sock );
171 static int sock_get_poll_events( struct fd *fd );
172 static void sock_poll_event( struct fd *fd, int event );
173 static enum server_fd_type sock_get_fd_type( struct fd *fd );
174 static int sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
175 static void sock_queue_async( struct fd *fd, struct async *async, int type, int count );
176 static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
178 static int accept_into_socket( struct sock *sock, struct sock *acceptsock );
179 static struct sock *accept_socket( struct sock *sock );
180 static int sock_get_ntstatus( int err );
181 static unsigned int sock_get_error( int err );
183 static const struct object_ops sock_ops =
185 sizeof(struct sock), /* size */
186 sock_dump, /* dump */
187 no_get_type, /* get_type */
188 add_queue, /* add_queue */
189 remove_queue, /* remove_queue */
190 sock_signaled, /* signaled */
191 no_satisfied, /* satisfied */
192 no_signal, /* signal */
193 sock_get_fd, /* get_fd */
194 default_fd_map_access, /* map_access */
195 default_get_sd, /* get_sd */
196 default_set_sd, /* set_sd */
197 no_get_full_name, /* get_full_name */
198 no_lookup_name, /* lookup_name */
199 no_link_name, /* link_name */
200 NULL, /* unlink_name */
201 no_open_file, /* open_file */
202 no_kernel_obj_list, /* get_kernel_obj_list */
203 fd_close_handle, /* close_handle */
204 sock_destroy /* destroy */
207 static const struct fd_ops sock_fd_ops =
209 sock_get_poll_events, /* get_poll_events */
210 sock_poll_event, /* poll_event */
211 sock_get_fd_type, /* get_fd_type */
212 no_fd_read, /* read */
213 no_fd_write, /* write */
214 no_fd_flush, /* flush */
215 default_fd_get_file_info, /* get_file_info */
216 no_fd_get_volume_info, /* get_volume_info */
217 sock_ioctl, /* ioctl */
218 sock_queue_async, /* queue_async */
219 sock_reselect_async /* reselect_async */
222 union unix_sockaddr
224 struct sockaddr addr;
225 struct sockaddr_in in;
226 struct sockaddr_in6 in6;
227 #ifdef HAS_IPX
228 struct sockaddr_ipx ipx;
229 #endif
230 #ifdef HAS_IRDA
231 struct sockaddr_irda irda;
232 #endif
235 static int sockaddr_from_unix( const union unix_sockaddr *uaddr, struct WS_sockaddr *wsaddr, socklen_t wsaddrlen )
237 memset( wsaddr, 0, wsaddrlen );
239 switch (uaddr->addr.sa_family)
241 case AF_INET:
243 struct WS_sockaddr_in win = {0};
245 if (wsaddrlen < sizeof(win)) return -1;
246 win.sin_family = WS_AF_INET;
247 win.sin_port = uaddr->in.sin_port;
248 memcpy( &win.sin_addr, &uaddr->in.sin_addr, sizeof(win.sin_addr) );
249 memcpy( wsaddr, &win, sizeof(win) );
250 return sizeof(win);
253 case AF_INET6:
255 struct WS_sockaddr_in6 win = {0};
257 if (wsaddrlen < sizeof(struct WS_sockaddr_in6_old)) return -1;
258 win.sin6_family = WS_AF_INET6;
259 win.sin6_port = uaddr->in6.sin6_port;
260 win.sin6_flowinfo = uaddr->in6.sin6_flowinfo;
261 memcpy( &win.sin6_addr, &uaddr->in6.sin6_addr, sizeof(win.sin6_addr) );
262 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
263 win.sin6_scope_id = uaddr->in6.sin6_scope_id;
264 #endif
265 if (wsaddrlen >= sizeof(struct WS_sockaddr_in6))
267 memcpy( wsaddr, &win, sizeof(struct WS_sockaddr_in6) );
268 return sizeof(struct WS_sockaddr_in6);
270 memcpy( wsaddr, &win, sizeof(struct WS_sockaddr_in6_old) );
271 return sizeof(struct WS_sockaddr_in6_old);
274 #ifdef HAS_IPX
275 case AF_IPX:
277 struct WS_sockaddr_ipx win = {0};
279 if (wsaddrlen < sizeof(win)) return -1;
280 win.sa_family = WS_AF_IPX;
281 memcpy( win.sa_netnum, &uaddr->ipx.sipx_network, sizeof(win.sa_netnum) );
282 memcpy( win.sa_nodenum, &uaddr->ipx.sipx_node, sizeof(win.sa_nodenum) );
283 win.sa_socket = uaddr->ipx.sipx_port;
284 memcpy( wsaddr, &win, sizeof(win) );
285 return sizeof(win);
287 #endif
289 #ifdef HAS_IRDA
290 case AF_IRDA:
292 SOCKADDR_IRDA win;
294 if (wsaddrlen < sizeof(win)) return -1;
295 win.irdaAddressFamily = WS_AF_IRDA;
296 memcpy( win.irdaDeviceID, &uaddr->irda.sir_addr, sizeof(win.irdaDeviceID) );
297 if (uaddr->irda.sir_lsap_sel != LSAP_ANY)
298 snprintf( win.irdaServiceName, sizeof(win.irdaServiceName), "LSAP-SEL%u", uaddr->irda.sir_lsap_sel );
299 else
300 memcpy( win.irdaServiceName, uaddr->irda.sir_name, sizeof(win.irdaServiceName) );
301 memcpy( wsaddr, &win, sizeof(win) );
302 return sizeof(win);
304 #endif
306 case AF_UNSPEC:
307 return 0;
309 default:
310 return -1;
315 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
316 * we post messages if there are multiple events. Used to send
317 * messages. The problem is if there is both a FD_CONNECT event and,
318 * say, an FD_READ event available on the same socket, we want to
319 * notify the app of the connect event first. Otherwise it may
320 * discard the read event because it thinks it hasn't connected yet.
322 static const int event_bitorder[FD_MAX_EVENTS] =
324 FD_CONNECT_BIT,
325 FD_ACCEPT_BIT,
326 FD_OOB_BIT,
327 FD_WRITE_BIT,
328 FD_READ_BIT,
329 FD_CLOSE_BIT,
330 6, 7, 8, 9 /* leftovers */
333 /* Flags that make sense only for SOCK_STREAM sockets */
334 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
336 typedef enum {
337 SOCK_SHUTDOWN_ERROR = -1,
338 SOCK_SHUTDOWN_EOF = 0,
339 SOCK_SHUTDOWN_POLLHUP = 1
340 } sock_shutdown_t;
342 static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
344 static sock_shutdown_t sock_check_pollhup(void)
346 sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
347 int fd[2], n;
348 struct pollfd pfd;
349 char dummy;
351 if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
352 if ( shutdown( fd[0], 1 ) ) goto out;
354 pfd.fd = fd[1];
355 pfd.events = POLLIN;
356 pfd.revents = 0;
358 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
359 n = poll( &pfd, 1, 1 );
360 if ( n != 1 ) goto out; /* error or timeout */
361 if ( pfd.revents & POLLHUP )
362 ret = SOCK_SHUTDOWN_POLLHUP;
363 else if ( pfd.revents & POLLIN &&
364 read( fd[1], &dummy, 1 ) == 0 )
365 ret = SOCK_SHUTDOWN_EOF;
367 out:
368 close( fd[0] );
369 close( fd[1] );
370 return ret;
373 void sock_init(void)
375 sock_shutdown_type = sock_check_pollhup();
377 switch ( sock_shutdown_type )
379 case SOCK_SHUTDOWN_EOF:
380 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
381 break;
382 case SOCK_SHUTDOWN_POLLHUP:
383 if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
384 break;
385 default:
386 fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
387 sock_shutdown_type = SOCK_SHUTDOWN_EOF;
391 static int sock_reselect( struct sock *sock )
393 int ev = sock_get_poll_events( sock->fd );
395 if (debug_level)
396 fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
398 if (!sock->polling) /* FIXME: should find a better way to do this */
400 /* previously unconnected socket, is this reselect supposed to connect it? */
401 if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
402 /* ok, it is, attach it to the wineserver's main poll loop */
403 sock->polling = 1;
404 allow_fd_caching( sock->fd );
406 /* update condition mask */
407 set_fd_events( sock->fd, ev );
408 return ev;
411 /* wake anybody waiting on the socket event or send the associated message */
412 static void sock_wake_up( struct sock *sock )
414 unsigned int events = sock->pmask & sock->mask;
415 int i;
417 if ( !events ) return;
419 if (sock->event)
421 if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
422 set_event( sock->event );
424 if (sock->window)
426 if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
427 for (i = 0; i < FD_MAX_EVENTS; i++)
429 int event = event_bitorder[i];
430 if (sock->pmask & (1 << event))
432 lparam_t lparam = (1 << event) | (sock->errors[event] << 16);
433 post_message( sock->window, sock->message, sock->wparam, lparam );
436 sock->pmask = 0;
437 sock_reselect( sock );
441 static inline int sock_error( struct fd *fd )
443 unsigned int optval = 0;
444 socklen_t optlen = sizeof(optval);
446 getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
447 return optval;
450 static void free_accept_req( struct accept_req *req )
452 list_remove( &req->entry );
453 if (req->acceptsock) req->acceptsock->accept_recv_req = NULL;
454 release_object( req->async );
455 free( req );
458 static void fill_accept_output( struct accept_req *req, struct iosb *iosb )
460 union unix_sockaddr unix_addr;
461 struct WS_sockaddr *win_addr;
462 unsigned int remote_len;
463 socklen_t unix_len;
464 int fd, size = 0;
465 char *out_data;
466 int win_len;
468 if (!(out_data = mem_alloc( iosb->out_size ))) return;
470 fd = get_unix_fd( req->acceptsock->fd );
472 if (req->recv_len && (size = recv( fd, out_data, req->recv_len, 0 )) < 0)
474 if (!req->accepted && errno == EWOULDBLOCK)
476 req->accepted = 1;
477 sock_reselect( req->acceptsock );
478 set_error( STATUS_PENDING );
479 return;
482 set_win32_error( sock_get_error( errno ) );
483 free( out_data );
484 return;
487 if (req->local_len)
489 if (req->local_len < sizeof(int))
491 set_error( STATUS_BUFFER_TOO_SMALL );
492 free( out_data );
493 return;
496 unix_len = sizeof(unix_addr);
497 win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + sizeof(int));
498 if (getsockname( fd, &unix_addr.addr, &unix_len ) < 0 ||
499 (win_len = sockaddr_from_unix( &unix_addr, win_addr, req->local_len - sizeof(int) )) < 0)
501 set_win32_error( sock_get_error( errno ) );
502 free( out_data );
503 return;
505 memcpy( out_data + req->recv_len, &win_len, sizeof(int) );
508 unix_len = sizeof(unix_addr);
509 win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + req->local_len + sizeof(int));
510 remote_len = iosb->out_size - req->recv_len - req->local_len;
511 if (getpeername( fd, &unix_addr.addr, &unix_len ) < 0 ||
512 (win_len = sockaddr_from_unix( &unix_addr, win_addr, remote_len - sizeof(int) )) < 0)
514 set_win32_error( sock_get_error( errno ) );
515 free( out_data );
516 return;
518 memcpy( out_data + req->recv_len + req->local_len, &win_len, sizeof(int) );
520 iosb->status = STATUS_SUCCESS;
521 iosb->result = size;
522 iosb->out_data = out_data;
523 set_error( STATUS_ALERTED );
526 static void complete_async_accept( struct sock *sock, struct accept_req *req )
528 struct sock *acceptsock = req->acceptsock;
529 struct async *async = req->async;
530 struct iosb *iosb;
532 if (debug_level) fprintf( stderr, "completing accept request for socket %p\n", sock );
534 if (acceptsock)
536 if (!accept_into_socket( sock, acceptsock )) return;
538 iosb = async_get_iosb( async );
539 fill_accept_output( req, iosb );
540 release_object( iosb );
542 else
544 obj_handle_t handle;
546 if (!(acceptsock = accept_socket( sock ))) return;
547 handle = alloc_handle_no_access_check( async_get_thread( async )->process, &acceptsock->obj,
548 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
549 acceptsock->wparam = handle;
550 release_object( acceptsock );
551 if (!handle) return;
553 iosb = async_get_iosb( async );
554 if (!(iosb->out_data = malloc( sizeof(handle) )))
556 release_object( iosb );
557 return;
559 iosb->status = STATUS_SUCCESS;
560 iosb->out_size = sizeof(handle);
561 memcpy( iosb->out_data, &handle, sizeof(handle) );
562 release_object( iosb );
563 set_error( STATUS_ALERTED );
567 static void complete_async_accept_recv( struct accept_req *req )
569 struct async *async = req->async;
570 struct iosb *iosb;
572 if (debug_level) fprintf( stderr, "completing accept recv request for socket %p\n", req->acceptsock );
574 assert( req->recv_len );
576 iosb = async_get_iosb( async );
577 fill_accept_output( req, iosb );
578 release_object( iosb );
581 static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
583 if (event & (POLLIN | POLLPRI))
585 struct accept_req *req;
587 LIST_FOR_EACH_ENTRY( req, &sock->accept_list, struct accept_req, entry )
589 if (!req->accepted)
591 complete_async_accept( sock, req );
592 if (get_error() != STATUS_PENDING)
593 async_terminate( req->async, get_error() );
594 break;
598 if (sock->accept_recv_req)
600 complete_async_accept_recv( sock->accept_recv_req );
601 if (get_error() != STATUS_PENDING)
602 async_terminate( sock->accept_recv_req->async, get_error() );
606 if (is_fd_overlapped( sock->fd ))
608 if (event & (POLLIN|POLLPRI) && async_waiting( &sock->read_q ))
610 if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
611 async_wake_up( &sock->read_q, STATUS_ALERTED );
612 event &= ~(POLLIN|POLLPRI);
614 if (event & POLLOUT && async_waiting( &sock->write_q ))
616 if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
617 async_wake_up( &sock->write_q, STATUS_ALERTED );
618 event &= ~POLLOUT;
622 if (event & (POLLERR | POLLHUP))
624 int status = sock_get_ntstatus( error );
625 struct accept_req *req, *next;
627 if (!(sock->state & FD_READ))
628 async_wake_up( &sock->read_q, status );
629 if (!(sock->state & FD_WRITE))
630 async_wake_up( &sock->write_q, status );
632 LIST_FOR_EACH_ENTRY_SAFE( req, next, &sock->accept_list, struct accept_req, entry )
633 async_terminate( req->async, status );
635 if (sock->accept_recv_req)
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 );
879 struct accept_req *req, *next;
881 LIST_FOR_EACH_ENTRY_SAFE( req, next, &sock->accept_list, struct accept_req, entry )
883 struct iosb *iosb = async_get_iosb( req->async );
884 if (iosb->status != STATUS_PENDING)
885 free_accept_req( req );
886 release_object( iosb );
889 /* ignore reselect on ifchange queue */
890 if (&sock->ifchange_q != queue)
891 sock_reselect( sock );
894 static struct fd *sock_get_fd( struct object *obj )
896 struct sock *sock = (struct sock *)obj;
897 return (struct fd *)grab_object( sock->fd );
900 static void sock_destroy( struct object *obj )
902 struct sock *sock = (struct sock *)obj;
903 struct accept_req *req, *next;
905 assert( obj->ops == &sock_ops );
907 /* FIXME: special socket shutdown stuff? */
909 if ( sock->deferred )
910 release_object( sock->deferred );
912 if (sock->accept_recv_req)
913 async_terminate( sock->accept_recv_req->async, STATUS_CANCELLED );
915 LIST_FOR_EACH_ENTRY_SAFE( req, next, &sock->accept_list, struct accept_req, entry )
916 async_terminate( req->async, STATUS_CANCELLED );
918 async_wake_up( &sock->ifchange_q, STATUS_CANCELLED );
919 sock_release_ifchange( sock );
920 free_async_queue( &sock->read_q );
921 free_async_queue( &sock->write_q );
922 free_async_queue( &sock->ifchange_q );
923 free_async_queue( &sock->accept_q );
924 if (sock->event) release_object( sock->event );
925 if (sock->fd)
927 /* shut the socket down to force pending poll() calls in the client to return */
928 shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
929 release_object( sock->fd );
933 static struct sock *create_socket(void)
935 struct sock *sock;
937 if (!(sock = alloc_object( &sock_ops ))) return NULL;
938 sock->fd = NULL;
939 sock->state = 0;
940 sock->mask = 0;
941 sock->hmask = 0;
942 sock->pmask = 0;
943 sock->polling = 0;
944 sock->flags = 0;
945 sock->proto = 0;
946 sock->type = 0;
947 sock->family = 0;
948 sock->event = NULL;
949 sock->window = 0;
950 sock->message = 0;
951 sock->wparam = 0;
952 sock->connect_time = 0;
953 sock->deferred = NULL;
954 sock->ifchange_obj = NULL;
955 sock->accept_recv_req = NULL;
956 init_async_queue( &sock->read_q );
957 init_async_queue( &sock->write_q );
958 init_async_queue( &sock->ifchange_q );
959 init_async_queue( &sock->accept_q );
960 memset( sock->errors, 0, sizeof(sock->errors) );
961 list_init( &sock->accept_list );
962 return sock;
965 static int get_unix_family( int family )
967 switch (family)
969 case WS_AF_INET: return AF_INET;
970 case WS_AF_INET6: return AF_INET6;
971 #ifdef HAS_IPX
972 case WS_AF_IPX: return AF_IPX;
973 #endif
974 #ifdef AF_IRDA
975 case WS_AF_IRDA: return AF_IRDA;
976 #endif
977 case WS_AF_UNSPEC: return AF_UNSPEC;
978 default: return -1;
982 static int get_unix_type( int type )
984 switch (type)
986 case WS_SOCK_DGRAM: return SOCK_DGRAM;
987 case WS_SOCK_RAW: return SOCK_RAW;
988 case WS_SOCK_STREAM: return SOCK_STREAM;
989 default: return -1;
993 static int get_unix_protocol( int protocol )
995 if (protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
996 return protocol;
998 switch (protocol)
1000 case WS_IPPROTO_ICMP: return IPPROTO_ICMP;
1001 case WS_IPPROTO_IGMP: return IPPROTO_IGMP;
1002 case WS_IPPROTO_IP: return IPPROTO_IP;
1003 case WS_IPPROTO_IPIP: return IPPROTO_IPIP;
1004 case WS_IPPROTO_IPV6: return IPPROTO_IPV6;
1005 case WS_IPPROTO_RAW: return IPPROTO_RAW;
1006 case WS_IPPROTO_TCP: return IPPROTO_TCP;
1007 case WS_IPPROTO_UDP: return IPPROTO_UDP;
1008 default: return -1;
1012 static void set_dont_fragment( int fd, int level, int value )
1014 int optname;
1016 if (level == IPPROTO_IP)
1018 #ifdef IP_DONTFRAG
1019 optname = IP_DONTFRAG;
1020 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1021 optname = IP_MTU_DISCOVER;
1022 value = value ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
1023 #else
1024 return;
1025 #endif
1027 else
1029 #ifdef IPV6_DONTFRAG
1030 optname = IPV6_DONTFRAG;
1031 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1032 optname = IPV6_MTU_DISCOVER;
1033 value = value ? IPV6_PMTUDISC_DO : IPV6_PMTUDISC_DONT;
1034 #else
1035 return;
1036 #endif
1039 setsockopt( fd, level, optname, &value, sizeof(value) );
1042 static int init_socket( struct sock *sock, int family, int type, int protocol, unsigned int flags )
1044 unsigned int options = 0;
1045 int sockfd, unix_type, unix_family, unix_protocol;
1047 unix_family = get_unix_family( family );
1048 unix_type = get_unix_type( type );
1049 unix_protocol = get_unix_protocol( protocol );
1051 if (unix_protocol < 0)
1053 if (type && unix_type < 0)
1054 set_win32_error( WSAESOCKTNOSUPPORT );
1055 else
1056 set_win32_error( WSAEPROTONOSUPPORT );
1057 return -1;
1059 if (unix_family < 0)
1061 if (family >= 0 && unix_type < 0)
1062 set_win32_error( WSAESOCKTNOSUPPORT );
1063 else
1064 set_win32_error( WSAEAFNOSUPPORT );
1065 return -1;
1068 sockfd = socket( unix_family, unix_type, unix_protocol );
1069 if (sockfd == -1)
1071 if (errno == EINVAL) set_win32_error( WSAESOCKTNOSUPPORT );
1072 else set_win32_error( sock_get_error( errno ));
1073 return -1;
1075 fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
1077 if (family == WS_AF_IPX && protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
1079 #ifdef HAS_IPX
1080 int ipx_type = protocol - WS_NSPROTO_IPX;
1082 #ifdef SOL_IPX
1083 setsockopt( sockfd, SOL_IPX, IPX_TYPE, &ipx_type, sizeof(ipx_type) );
1084 #else
1085 struct ipx val;
1086 /* Should we retrieve val using a getsockopt call and then
1087 * set the modified one? */
1088 val.ipx_pt = ipx_type;
1089 setsockopt( sockfd, 0, SO_DEFAULT_HEADERS, &val, sizeof(val) );
1090 #endif
1091 #endif
1094 if (unix_family == AF_INET || unix_family == AF_INET6)
1096 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1097 if (unix_type == SOCK_DGRAM || unix_type == SOCK_RAW) /* in Linux the global default can be enabled */
1098 set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, FALSE );
1099 else if (unix_type == SOCK_STREAM)
1100 set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, TRUE );
1103 #ifdef IPV6_V6ONLY
1104 if (unix_family == AF_INET6)
1106 static const int enable = 1;
1107 setsockopt( sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &enable, sizeof(enable) );
1109 #endif
1111 sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
1112 sock->flags = flags;
1113 sock->proto = protocol;
1114 sock->type = type;
1115 sock->family = family;
1117 if (sock->fd)
1119 options = get_fd_options( sock->fd );
1120 release_object( sock->fd );
1123 if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj, options )))
1125 return -1;
1127 sock_reselect( sock );
1128 clear_error();
1129 return 0;
1132 /* accepts a socket and inits it */
1133 static int accept_new_fd( struct sock *sock )
1136 /* Try to accept(2). We can't be safe that this an already connected socket
1137 * or that accept() is allowed on it. In those cases we will get -1/errno
1138 * return.
1140 struct sockaddr saddr;
1141 socklen_t slen = sizeof(saddr);
1142 int acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen );
1143 if (acceptfd != -1)
1144 fcntl( acceptfd, F_SETFL, O_NONBLOCK );
1145 else
1146 set_win32_error( sock_get_error( errno ));
1147 return acceptfd;
1150 /* accept a socket (creates a new fd) */
1151 static struct sock *accept_socket( struct sock *sock )
1153 struct sock *acceptsock;
1154 int acceptfd;
1156 if (get_unix_fd( sock->fd ) == -1) return NULL;
1158 if ( sock->deferred )
1160 acceptsock = sock->deferred;
1161 sock->deferred = NULL;
1163 else
1165 if ((acceptfd = accept_new_fd( sock )) == -1) return NULL;
1166 if (!(acceptsock = create_socket()))
1168 close( acceptfd );
1169 return NULL;
1172 /* newly created socket gets the same properties of the listening socket */
1173 acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
1174 if (sock->state & FD_WINE_NONBLOCKING)
1175 acceptsock->state |= FD_WINE_NONBLOCKING;
1176 acceptsock->mask = sock->mask;
1177 acceptsock->proto = sock->proto;
1178 acceptsock->type = sock->type;
1179 acceptsock->family = sock->family;
1180 acceptsock->window = sock->window;
1181 acceptsock->message = sock->message;
1182 acceptsock->connect_time = current_time;
1183 if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
1184 acceptsock->flags = sock->flags;
1185 if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
1186 get_fd_options( sock->fd ) )))
1188 release_object( acceptsock );
1189 return NULL;
1192 clear_error();
1193 sock->pmask &= ~FD_ACCEPT;
1194 sock->hmask &= ~FD_ACCEPT;
1195 sock_reselect( sock );
1196 return acceptsock;
1199 static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
1201 int acceptfd;
1202 struct fd *newfd;
1204 if (get_unix_fd( sock->fd ) == -1) return FALSE;
1206 if ( sock->deferred )
1208 newfd = dup_fd_object( sock->deferred->fd, 0, 0,
1209 get_fd_options( acceptsock->fd ) );
1210 if ( !newfd )
1211 return FALSE;
1213 set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
1215 release_object( sock->deferred );
1216 sock->deferred = NULL;
1218 else
1220 if ((acceptfd = accept_new_fd( sock )) == -1)
1221 return FALSE;
1223 if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
1224 get_fd_options( acceptsock->fd ) )))
1225 return FALSE;
1228 acceptsock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
1229 acceptsock->hmask = 0;
1230 acceptsock->pmask = 0;
1231 acceptsock->polling = 0;
1232 acceptsock->proto = sock->proto;
1233 acceptsock->type = sock->type;
1234 acceptsock->family = sock->family;
1235 acceptsock->wparam = 0;
1236 acceptsock->deferred = NULL;
1237 acceptsock->connect_time = current_time;
1238 fd_copy_completion( acceptsock->fd, newfd );
1239 release_object( acceptsock->fd );
1240 acceptsock->fd = newfd;
1242 clear_error();
1243 sock->pmask &= ~FD_ACCEPT;
1244 sock->hmask &= ~FD_ACCEPT;
1245 sock_reselect( sock );
1247 return TRUE;
1250 /* return an errno value mapped to a WSA error */
1251 static unsigned int sock_get_error( int err )
1253 switch (err)
1255 case EINTR: return WSAEINTR;
1256 case EBADF: return WSAEBADF;
1257 case EPERM:
1258 case EACCES: return WSAEACCES;
1259 case EFAULT: return WSAEFAULT;
1260 case EINVAL: return WSAEINVAL;
1261 case EMFILE: return WSAEMFILE;
1262 case EWOULDBLOCK: return WSAEWOULDBLOCK;
1263 case EINPROGRESS: return WSAEINPROGRESS;
1264 case EALREADY: return WSAEALREADY;
1265 case ENOTSOCK: return WSAENOTSOCK;
1266 case EDESTADDRREQ: return WSAEDESTADDRREQ;
1267 case EMSGSIZE: return WSAEMSGSIZE;
1268 case EPROTOTYPE: return WSAEPROTOTYPE;
1269 case ENOPROTOOPT: return WSAENOPROTOOPT;
1270 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
1271 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
1272 case EOPNOTSUPP: return WSAEOPNOTSUPP;
1273 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
1274 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
1275 case EADDRINUSE: return WSAEADDRINUSE;
1276 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
1277 case ENETDOWN: return WSAENETDOWN;
1278 case ENETUNREACH: return WSAENETUNREACH;
1279 case ENETRESET: return WSAENETRESET;
1280 case ECONNABORTED: return WSAECONNABORTED;
1281 case EPIPE:
1282 case ECONNRESET: return WSAECONNRESET;
1283 case ENOBUFS: return WSAENOBUFS;
1284 case EISCONN: return WSAEISCONN;
1285 case ENOTCONN: return WSAENOTCONN;
1286 case ESHUTDOWN: return WSAESHUTDOWN;
1287 case ETOOMANYREFS: return WSAETOOMANYREFS;
1288 case ETIMEDOUT: return WSAETIMEDOUT;
1289 case ECONNREFUSED: return WSAECONNREFUSED;
1290 case ELOOP: return WSAELOOP;
1291 case ENAMETOOLONG: return WSAENAMETOOLONG;
1292 case EHOSTDOWN: return WSAEHOSTDOWN;
1293 case EHOSTUNREACH: return WSAEHOSTUNREACH;
1294 case ENOTEMPTY: return WSAENOTEMPTY;
1295 #ifdef EPROCLIM
1296 case EPROCLIM: return WSAEPROCLIM;
1297 #endif
1298 #ifdef EUSERS
1299 case EUSERS: return WSAEUSERS;
1300 #endif
1301 #ifdef EDQUOT
1302 case EDQUOT: return WSAEDQUOT;
1303 #endif
1304 #ifdef ESTALE
1305 case ESTALE: return WSAESTALE;
1306 #endif
1307 #ifdef EREMOTE
1308 case EREMOTE: return WSAEREMOTE;
1309 #endif
1311 case 0: return 0;
1312 default:
1313 errno = err;
1314 perror("wineserver: sock_get_error() can't map error");
1315 return WSAEFAULT;
1319 static int sock_get_ntstatus( int err )
1321 switch ( err )
1323 case EBADF: return STATUS_INVALID_HANDLE;
1324 case EBUSY: return STATUS_DEVICE_BUSY;
1325 case EPERM:
1326 case EACCES: return STATUS_ACCESS_DENIED;
1327 case EFAULT: return STATUS_NO_MEMORY;
1328 case EINVAL: return STATUS_INVALID_PARAMETER;
1329 case ENFILE:
1330 case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
1331 case EWOULDBLOCK: return STATUS_CANT_WAIT;
1332 case EINPROGRESS: return STATUS_PENDING;
1333 case EALREADY: return STATUS_NETWORK_BUSY;
1334 case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
1335 case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
1336 case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
1337 case EPROTONOSUPPORT:
1338 case ESOCKTNOSUPPORT:
1339 case EPFNOSUPPORT:
1340 case EAFNOSUPPORT:
1341 case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
1342 case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
1343 case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
1344 case EADDRINUSE: return STATUS_ADDRESS_ALREADY_ASSOCIATED;
1345 case EADDRNOTAVAIL: return STATUS_INVALID_PARAMETER;
1346 case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
1347 case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
1348 case ENOTCONN: return STATUS_CONNECTION_DISCONNECTED;
1349 case ETIMEDOUT: return STATUS_IO_TIMEOUT;
1350 case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
1351 case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
1352 case ENETDOWN: return STATUS_NETWORK_BUSY;
1353 case EPIPE:
1354 case ECONNRESET: return STATUS_CONNECTION_RESET;
1355 case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
1357 case 0: return STATUS_SUCCESS;
1358 default:
1359 errno = err;
1360 perror("wineserver: sock_get_ntstatus() can't map error");
1361 return STATUS_UNSUCCESSFUL;
1365 static struct accept_req *alloc_accept_req( struct sock *acceptsock, struct async *async,
1366 const struct afd_accept_into_params *params )
1368 struct accept_req *req = mem_alloc( sizeof(*req) );
1370 if (req)
1372 req->async = (struct async *)grab_object( async );
1373 req->acceptsock = acceptsock;
1374 req->accepted = 0;
1375 req->recv_len = 0;
1376 req->local_len = 0;
1377 if (params)
1379 req->recv_len = params->recv_len;
1380 req->local_len = params->local_len;
1383 return req;
1386 static int sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1388 struct sock *sock = get_fd_user( fd );
1390 assert( sock->obj.ops == &sock_ops );
1392 if (get_unix_fd( fd ) == -1 && code != IOCTL_AFD_CREATE) return 0;
1394 switch(code)
1396 case IOCTL_AFD_CREATE:
1398 const struct afd_create_params *params = get_req_data();
1400 if (get_req_data_size() != sizeof(*params))
1402 set_error( STATUS_INVALID_PARAMETER );
1403 return 0;
1405 init_socket( sock, params->family, params->type, params->protocol, params->flags );
1406 return 0;
1409 case IOCTL_AFD_ACCEPT:
1411 struct sock *acceptsock;
1412 obj_handle_t handle;
1414 if (get_reply_max_size() != sizeof(handle))
1416 set_error( STATUS_BUFFER_TOO_SMALL );
1417 return 0;
1420 if (!(acceptsock = accept_socket( sock )))
1422 struct accept_req *req;
1424 if (sock->state & FD_WINE_NONBLOCKING) return 0;
1425 if (get_error() != (0xc0010000 | WSAEWOULDBLOCK)) return 0;
1427 if (!(req = alloc_accept_req( NULL, async, NULL ))) return 0;
1428 list_add_tail( &sock->accept_list, &req->entry );
1430 queue_async( &sock->accept_q, async );
1431 sock_reselect( sock );
1432 set_error( STATUS_PENDING );
1433 return 1;
1435 handle = alloc_handle( current->process, &acceptsock->obj,
1436 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
1437 acceptsock->wparam = handle;
1438 release_object( acceptsock );
1439 set_reply_data( &handle, sizeof(handle) );
1440 return 0;
1443 case IOCTL_AFD_ACCEPT_INTO:
1445 static const int access = FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | FILE_READ_DATA;
1446 const struct afd_accept_into_params *params = get_req_data();
1447 struct sock *acceptsock;
1448 unsigned int remote_len;
1449 struct accept_req *req;
1451 if (get_req_data_size() != sizeof(*params) ||
1452 get_reply_max_size() < params->recv_len ||
1453 get_reply_max_size() - params->recv_len < params->local_len)
1455 set_error( STATUS_BUFFER_TOO_SMALL );
1456 return 0;
1459 remote_len = get_reply_max_size() - params->recv_len - params->local_len;
1460 if (remote_len < sizeof(int))
1462 set_error( STATUS_INVALID_PARAMETER );
1463 return 0;
1466 if (!(acceptsock = (struct sock *)get_handle_obj( current->process, params->accept_handle, access, &sock_ops )))
1467 return 0;
1469 if (acceptsock->accept_recv_req)
1471 release_object( acceptsock );
1472 set_win32_error( WSAEINVAL );
1473 return 0;
1476 if (!(req = alloc_accept_req( acceptsock, async, params ))) return 0;
1477 list_add_tail( &sock->accept_list, &req->entry );
1478 acceptsock->accept_recv_req = req;
1479 release_object( acceptsock );
1481 acceptsock->wparam = params->accept_handle;
1482 queue_async( &sock->accept_q, async );
1483 sock_reselect( sock );
1484 set_error( STATUS_PENDING );
1485 return 1;
1488 case IOCTL_AFD_ADDRESS_LIST_CHANGE:
1489 if ((sock->state & FD_WINE_NONBLOCKING) && async_is_blocking( async ))
1491 set_win32_error( WSAEWOULDBLOCK );
1492 return 0;
1494 if (!sock_get_ifchange( sock )) return 0;
1495 queue_async( &sock->ifchange_q, async );
1496 set_error( STATUS_PENDING );
1497 return 1;
1499 default:
1500 set_error( STATUS_NOT_SUPPORTED );
1501 return 0;
1505 #ifdef HAVE_LINUX_RTNETLINK_H
1507 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
1508 static struct object *ifchange_object;
1510 static void ifchange_dump( struct object *obj, int verbose );
1511 static struct fd *ifchange_get_fd( struct object *obj );
1512 static void ifchange_destroy( struct object *obj );
1514 static int ifchange_get_poll_events( struct fd *fd );
1515 static void ifchange_poll_event( struct fd *fd, int event );
1517 struct ifchange
1519 struct object obj; /* object header */
1520 struct fd *fd; /* interface change file descriptor */
1521 struct list sockets; /* list of sockets to send interface change notifications */
1524 static const struct object_ops ifchange_ops =
1526 sizeof(struct ifchange), /* size */
1527 ifchange_dump, /* dump */
1528 no_get_type, /* get_type */
1529 add_queue, /* add_queue */
1530 NULL, /* remove_queue */
1531 NULL, /* signaled */
1532 no_satisfied, /* satisfied */
1533 no_signal, /* signal */
1534 ifchange_get_fd, /* get_fd */
1535 default_fd_map_access, /* map_access */
1536 default_get_sd, /* get_sd */
1537 default_set_sd, /* set_sd */
1538 no_get_full_name, /* get_full_name */
1539 no_lookup_name, /* lookup_name */
1540 no_link_name, /* link_name */
1541 NULL, /* unlink_name */
1542 no_open_file, /* open_file */
1543 no_kernel_obj_list, /* get_kernel_obj_list */
1544 no_close_handle, /* close_handle */
1545 ifchange_destroy /* destroy */
1548 static const struct fd_ops ifchange_fd_ops =
1550 ifchange_get_poll_events, /* get_poll_events */
1551 ifchange_poll_event, /* poll_event */
1552 NULL, /* get_fd_type */
1553 no_fd_read, /* read */
1554 no_fd_write, /* write */
1555 no_fd_flush, /* flush */
1556 no_fd_get_file_info, /* get_file_info */
1557 no_fd_get_volume_info, /* get_volume_info */
1558 no_fd_ioctl, /* ioctl */
1559 NULL, /* queue_async */
1560 NULL /* reselect_async */
1563 static void ifchange_dump( struct object *obj, int verbose )
1565 assert( obj->ops == &ifchange_ops );
1566 fprintf( stderr, "Interface change\n" );
1569 static struct fd *ifchange_get_fd( struct object *obj )
1571 struct ifchange *ifchange = (struct ifchange *)obj;
1572 return (struct fd *)grab_object( ifchange->fd );
1575 static void ifchange_destroy( struct object *obj )
1577 struct ifchange *ifchange = (struct ifchange *)obj;
1578 assert( obj->ops == &ifchange_ops );
1580 release_object( ifchange->fd );
1582 /* reset the global ifchange object so that it will be recreated if it is needed again */
1583 assert( obj == ifchange_object );
1584 ifchange_object = NULL;
1587 static int ifchange_get_poll_events( struct fd *fd )
1589 return POLLIN;
1592 /* wake up all the sockets waiting for a change notification event */
1593 static void ifchange_wake_up( struct object *obj, unsigned int status )
1595 struct ifchange *ifchange = (struct ifchange *)obj;
1596 struct list *ptr, *next;
1597 assert( obj->ops == &ifchange_ops );
1598 assert( obj == ifchange_object );
1600 LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
1602 struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
1604 assert( sock->ifchange_obj );
1605 async_wake_up( &sock->ifchange_q, status ); /* issue ifchange notification for the socket */
1606 sock_release_ifchange( sock ); /* remove socket from list and decrement ifchange refcount */
1610 static void ifchange_poll_event( struct fd *fd, int event )
1612 struct object *ifchange = get_fd_user( fd );
1613 unsigned int status = STATUS_PENDING;
1614 char buffer[PIPE_BUF];
1615 int r;
1617 r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
1618 if (r < 0)
1620 if (errno == EWOULDBLOCK || (EWOULDBLOCK != EAGAIN && errno == EAGAIN))
1621 return; /* retry when poll() says the socket is ready */
1622 status = sock_get_ntstatus( errno );
1624 else if (r > 0)
1626 struct nlmsghdr *nlh;
1628 for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
1630 if (nlh->nlmsg_type == NLMSG_DONE)
1631 break;
1632 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
1633 status = STATUS_SUCCESS;
1636 else status = STATUS_CANCELLED;
1638 if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
1641 #endif
1643 /* we only need one of these interface notification objects, all of the sockets dependent upon
1644 * it will wake up when a notification event occurs */
1645 static struct object *get_ifchange( void )
1647 #ifdef HAVE_LINUX_RTNETLINK_H
1648 struct ifchange *ifchange;
1649 struct sockaddr_nl addr;
1650 int unix_fd;
1652 if (ifchange_object)
1654 /* increment the refcount for each socket that uses the ifchange object */
1655 return grab_object( ifchange_object );
1658 /* create the socket we need for processing interface change notifications */
1659 unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
1660 if (unix_fd == -1)
1662 set_win32_error( sock_get_error( errno ));
1663 return NULL;
1665 fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
1666 memset( &addr, 0, sizeof(addr) );
1667 addr.nl_family = AF_NETLINK;
1668 addr.nl_groups = RTMGRP_IPV4_IFADDR;
1669 /* bind the socket to the special netlink kernel interface */
1670 if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
1672 close( unix_fd );
1673 set_win32_error( sock_get_error( errno ));
1674 return NULL;
1676 if (!(ifchange = alloc_object( &ifchange_ops )))
1678 close( unix_fd );
1679 set_error( STATUS_NO_MEMORY );
1680 return NULL;
1682 list_init( &ifchange->sockets );
1683 if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
1685 release_object( ifchange );
1686 set_error( STATUS_NO_MEMORY );
1687 return NULL;
1689 set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
1691 /* the ifchange object is now successfully configured */
1692 ifchange_object = &ifchange->obj;
1693 return &ifchange->obj;
1694 #else
1695 set_error( STATUS_NOT_SUPPORTED );
1696 return NULL;
1697 #endif
1700 /* add the socket to the interface change notification list */
1701 static void ifchange_add_sock( struct object *obj, struct sock *sock )
1703 #ifdef HAVE_LINUX_RTNETLINK_H
1704 struct ifchange *ifchange = (struct ifchange *)obj;
1706 list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
1707 #endif
1710 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1711 static struct object *sock_get_ifchange( struct sock *sock )
1713 struct object *ifchange;
1715 if (sock->ifchange_obj) /* reuse existing ifchange_obj for this socket */
1716 return sock->ifchange_obj;
1718 if (!(ifchange = get_ifchange()))
1719 return NULL;
1721 /* add the socket to the ifchange notification list */
1722 ifchange_add_sock( ifchange, sock );
1723 sock->ifchange_obj = ifchange;
1724 return ifchange;
1727 /* destroy an existing ifchange queue for a specific socket */
1728 static void sock_release_ifchange( struct sock *sock )
1730 if (sock->ifchange_obj)
1732 list_remove( &sock->ifchange_entry );
1733 release_object( sock->ifchange_obj );
1734 sock->ifchange_obj = NULL;
1738 static struct object_type *socket_device_get_type( struct object *obj );
1739 static void socket_device_dump( struct object *obj, int verbose );
1740 static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
1741 unsigned int attr, struct object *root );
1742 static struct object *socket_device_open_file( struct object *obj, unsigned int access,
1743 unsigned int sharing, unsigned int options );
1745 static const struct object_ops socket_device_ops =
1747 sizeof(struct object), /* size */
1748 socket_device_dump, /* dump */
1749 socket_device_get_type, /* get_type */
1750 no_add_queue, /* add_queue */
1751 NULL, /* remove_queue */
1752 NULL, /* signaled */
1753 no_satisfied, /* satisfied */
1754 no_signal, /* signal */
1755 no_get_fd, /* get_fd */
1756 default_fd_map_access, /* map_access */
1757 default_get_sd, /* get_sd */
1758 default_set_sd, /* set_sd */
1759 default_get_full_name, /* get_full_name */
1760 socket_device_lookup_name, /* lookup_name */
1761 directory_link_name, /* link_name */
1762 default_unlink_name, /* unlink_name */
1763 socket_device_open_file, /* open_file */
1764 no_kernel_obj_list, /* get_kernel_obj_list */
1765 no_close_handle, /* close_handle */
1766 no_destroy /* destroy */
1769 static struct object_type *socket_device_get_type( struct object *obj )
1771 static const WCHAR name[] = {'D','e','v','i','c','e'};
1772 static const struct unicode_str str = { name, sizeof(name) };
1773 return get_object_type( &str );
1776 static void socket_device_dump( struct object *obj, int verbose )
1778 fputs( "Socket device\n", stderr );
1781 static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
1782 unsigned int attr, struct object *root )
1784 return NULL;
1787 static struct object *socket_device_open_file( struct object *obj, unsigned int access,
1788 unsigned int sharing, unsigned int options )
1790 struct sock *sock;
1792 if (!(sock = create_socket())) return NULL;
1793 if (!(sock->fd = alloc_pseudo_fd( &sock_fd_ops, &sock->obj, options )))
1795 release_object( sock );
1796 return NULL;
1798 return &sock->obj;
1801 struct object *create_socket_device( struct object *root, const struct unicode_str *name,
1802 unsigned int attr, const struct security_descriptor *sd )
1804 return create_named_object( root, &socket_device_ops, name, attr, sd );
1807 /* set socket event parameters */
1808 DECL_HANDLER(set_socket_event)
1810 struct sock *sock;
1811 struct event *old_event;
1813 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1814 FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
1815 if (get_unix_fd( sock->fd ) == -1) return;
1816 old_event = sock->event;
1817 sock->mask = req->mask;
1818 sock->hmask &= ~req->mask; /* re-enable held events */
1819 sock->event = NULL;
1820 sock->window = req->window;
1821 sock->message = req->msg;
1822 sock->wparam = req->handle; /* wparam is the socket handle */
1823 if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
1825 if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
1827 sock_reselect( sock );
1829 sock->state |= FD_WINE_NONBLOCKING;
1831 /* if a network event is pending, signal the event object
1832 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1833 before a WSAEventSelect() was done on it.
1834 (when dealing with Asynchronous socket) */
1835 sock_wake_up( sock );
1837 if (old_event) release_object( old_event ); /* we're through with it */
1838 release_object( &sock->obj );
1841 /* get socket event parameters */
1842 DECL_HANDLER(get_socket_event)
1844 struct sock *sock;
1846 if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
1847 FILE_READ_ATTRIBUTES, &sock_ops ))) return;
1848 if (get_unix_fd( sock->fd ) == -1) return;
1849 reply->mask = sock->mask;
1850 reply->pmask = sock->pmask;
1851 reply->state = sock->state;
1852 set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
1854 if (req->service)
1856 if (req->c_event)
1858 struct event *cevent = get_event_obj( current->process, req->c_event,
1859 EVENT_MODIFY_STATE );
1860 if (cevent)
1862 reset_event( cevent );
1863 release_object( cevent );
1866 sock->pmask = 0;
1867 sock_reselect( sock );
1869 release_object( &sock->obj );
1872 /* re-enable pending socket events */
1873 DECL_HANDLER(enable_socket_event)
1875 struct sock *sock;
1877 if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
1878 FILE_WRITE_ATTRIBUTES, &sock_ops)))
1879 return;
1881 if (get_unix_fd( sock->fd ) == -1) return;
1883 /* for event-based notification, windows erases stale events */
1884 sock->pmask &= ~req->mask;
1886 sock->hmask &= ~req->mask;
1887 sock->state |= req->sstate;
1888 sock->state &= ~req->cstate;
1889 if (sock->type != WS_SOCK_STREAM) sock->state &= ~STREAM_FLAG_MASK;
1891 sock_reselect( sock );
1893 release_object( &sock->obj );
1896 DECL_HANDLER(set_socket_deferred)
1898 struct sock *sock, *acceptsock;
1900 sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
1901 if ( !sock )
1902 return;
1904 acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
1905 if ( !acceptsock )
1907 release_object( sock );
1908 return;
1910 sock->deferred = acceptsock;
1911 release_object( sock );
1914 DECL_HANDLER(get_socket_info)
1916 struct sock *sock;
1918 sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
1919 if (!sock) return;
1921 if (get_unix_fd( sock->fd ) == -1) return;
1923 reply->family = sock->family;
1924 reply->type = sock->type;
1925 reply->protocol = sock->proto;
1927 release_object( &sock->obj );