2 * Server-side socket management
4 * Copyright (C) 1999 Marcus Meissner, Ove Kåven
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * FIXME: we use read|write access in all cases. Shouldn't we depend that
21 * on the access of the current handle?
33 #ifdef HAVE_NETINET_IN_H
34 # include <netinet/in.h>
40 #include <sys/types.h>
41 #ifdef HAVE_SYS_SOCKET_H
42 # include <sys/socket.h>
44 #ifdef HAVE_SYS_IOCTL_H
45 #include <sys/ioctl.h>
47 #ifdef HAVE_SYS_FILIO_H
48 # include <sys/filio.h>
53 #ifdef HAVE_LINUX_RTNETLINK_H
54 # include <linux/rtnetlink.h>
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>
63 # ifdef HAVE_LINUX_TYPES_H
64 # include <linux/types.h>
66 # include <linux/ipx.h>
68 #if defined(SOL_IPX) || defined(SO_DEFAULT_HEADERS)
72 #ifdef HAVE_LINUX_IRDA_H
73 # ifdef HAVE_LINUX_TYPES_H
74 # include <linux/types.h>
76 # include <linux/irda.h>
81 #define WIN32_NO_STATUS
100 #define FD_MAX_EVENTS 10
101 #define FD_READ_BIT 0
102 #define FD_WRITE_BIT 1
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
129 struct sock
*acceptsock
;
131 unsigned int recv_len
, local_len
;
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 */
224 struct sockaddr addr
;
225 struct sockaddr_in in
;
226 struct sockaddr_in6 in6
;
228 struct sockaddr_ipx ipx
;
231 struct sockaddr_irda irda
;
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
)
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
) );
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
;
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
);
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
) );
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
);
300 memcpy( win
.irdaServiceName
, uaddr
->irda
.sir_name
, sizeof(win
.irdaServiceName
) );
301 memcpy( wsaddr
, &win
, sizeof(win
) );
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
] =
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))
337 SOCK_SHUTDOWN_ERROR
= -1,
338 SOCK_SHUTDOWN_EOF
= 0,
339 SOCK_SHUTDOWN_POLLHUP
= 1
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
;
351 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
352 if ( shutdown( fd
[0], 1 ) ) goto out
;
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
;
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" );
382 case SOCK_SHUTDOWN_POLLHUP
:
383 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
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
);
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 */
404 allow_fd_caching( sock
->fd
);
406 /* update condition mask */
407 set_fd_events( sock
->fd
, 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
;
417 if ( !events
) return;
421 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
422 set_event( sock
->event
);
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
);
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
);
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
);
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
;
467 if (!(out_data
= mem_alloc( iosb
->out_size
))) return;
469 fd
= get_unix_fd( req
->acceptsock
->fd
);
471 if (req
->recv_len
&& (size
= recv( fd
, out_data
, req
->recv_len
, 0 )) < 0)
473 if (!req
->accepted
&& errno
== EWOULDBLOCK
)
476 sock_reselect( req
->acceptsock
);
477 set_error( STATUS_PENDING
);
481 set_win32_error( sock_get_error( errno
) );
488 if (req
->local_len
< sizeof(int))
490 set_error( STATUS_BUFFER_TOO_SMALL
);
495 unix_len
= sizeof(unix_addr
);
496 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ sizeof(int));
497 if (getsockname( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
498 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, req
->local_len
)) < 0)
500 set_win32_error( sock_get_error( errno
) );
504 memcpy( out_data
+ req
->recv_len
, &win_len
, sizeof(int) );
507 unix_len
= sizeof(unix_addr
);
508 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ req
->local_len
+ sizeof(int));
509 if (getpeername( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
510 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, iosb
->out_size
- req
->recv_len
- req
->local_len
)) < 0)
512 set_win32_error( sock_get_error( errno
) );
516 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
518 iosb
->status
= STATUS_SUCCESS
;
520 iosb
->out_data
= out_data
;
521 set_error( STATUS_ALERTED
);
524 static void complete_async_accept( struct sock
*sock
, struct accept_req
*req
)
526 struct sock
*acceptsock
= req
->acceptsock
;
527 struct async
*async
= req
->async
;
530 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
534 if (!accept_into_socket( sock
, acceptsock
)) return;
536 iosb
= async_get_iosb( async
);
537 fill_accept_output( req
, iosb
);
538 release_object( iosb
);
544 if (!(acceptsock
= accept_socket( sock
))) return;
545 handle
= alloc_handle_no_access_check( async_get_thread( async
)->process
, &acceptsock
->obj
,
546 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
547 acceptsock
->wparam
= handle
;
548 release_object( acceptsock
);
551 iosb
= async_get_iosb( async
);
552 if (!(iosb
->out_data
= malloc( sizeof(handle
) )))
554 release_object( iosb
);
557 iosb
->status
= STATUS_SUCCESS
;
558 iosb
->out_size
= sizeof(handle
);
559 memcpy( iosb
->out_data
, &handle
, sizeof(handle
) );
560 release_object( iosb
);
561 set_error( STATUS_ALERTED
);
565 static void complete_async_accept_recv( struct accept_req
*req
)
567 struct async
*async
= req
->async
;
570 if (debug_level
) fprintf( stderr
, "completing accept recv request for socket %p\n", req
->acceptsock
);
572 assert( req
->recv_len
);
574 iosb
= async_get_iosb( async
);
575 fill_accept_output( req
, iosb
);
576 release_object( iosb
);
579 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
581 if (event
& (POLLIN
| POLLPRI
))
583 struct accept_req
*req
;
585 LIST_FOR_EACH_ENTRY( req
, &sock
->accept_list
, struct accept_req
, entry
)
589 complete_async_accept( sock
, req
);
590 if (get_error() != STATUS_PENDING
)
591 async_terminate( req
->async
, get_error() );
596 if (sock
->accept_recv_req
)
598 complete_async_accept_recv( sock
->accept_recv_req
);
599 if (get_error() != STATUS_PENDING
)
600 async_terminate( sock
->accept_recv_req
->async
, get_error() );
604 if (is_fd_overlapped( sock
->fd
))
606 if (event
& (POLLIN
|POLLPRI
) && async_waiting( &sock
->read_q
))
608 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
609 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
610 event
&= ~(POLLIN
|POLLPRI
);
612 if (event
& POLLOUT
&& async_waiting( &sock
->write_q
))
614 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
615 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
620 if (event
& (POLLERR
| POLLHUP
))
622 int status
= sock_get_ntstatus( error
);
623 struct accept_req
*req
, *next
;
625 if (!(sock
->state
& FD_READ
))
626 async_wake_up( &sock
->read_q
, status
);
627 if (!(sock
->state
& FD_WRITE
))
628 async_wake_up( &sock
->write_q
, status
);
630 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
631 async_terminate( req
->async
, status
);
633 if (sock
->accept_recv_req
)
634 async_terminate( sock
->accept_recv_req
->async
, status
);
640 static void sock_dispatch_events( struct sock
*sock
, int prevstate
, int event
, int error
)
642 if (prevstate
& FD_CONNECT
)
644 sock
->pmask
|= FD_CONNECT
;
645 sock
->hmask
|= FD_CONNECT
;
646 sock
->errors
[FD_CONNECT_BIT
] = sock_get_error( error
);
649 if (prevstate
& FD_WINE_LISTENING
)
651 sock
->pmask
|= FD_ACCEPT
;
652 sock
->hmask
|= FD_ACCEPT
;
653 sock
->errors
[FD_ACCEPT_BIT
] = sock_get_error( error
);
659 sock
->pmask
|= FD_READ
;
660 sock
->hmask
|= FD_READ
;
661 sock
->errors
[FD_READ_BIT
] = 0;
666 sock
->pmask
|= FD_WRITE
;
667 sock
->hmask
|= FD_WRITE
;
668 sock
->errors
[FD_WRITE_BIT
] = 0;
673 sock
->pmask
|= FD_OOB
;
674 sock
->hmask
|= FD_OOB
;
675 sock
->errors
[FD_OOB_BIT
] = 0;
678 if (event
& (POLLERR
|POLLHUP
))
680 sock
->pmask
|= FD_CLOSE
;
681 sock
->hmask
|= FD_CLOSE
;
682 sock
->errors
[FD_CLOSE_BIT
] = sock_get_error( error
);
685 sock_wake_up( sock
);
688 static void sock_poll_event( struct fd
*fd
, int event
)
690 struct sock
*sock
= get_fd_user( fd
);
692 int prevstate
= sock
->state
;
695 assert( sock
->obj
.ops
== &sock_ops
);
697 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
699 /* we may change event later, remove from loop here */
700 if (event
& (POLLERR
|POLLHUP
)) set_fd_events( sock
->fd
, -1 );
702 if (sock
->state
& FD_CONNECT
)
704 if (event
& (POLLERR
|POLLHUP
))
706 /* we didn't get connected? */
707 sock
->state
&= ~FD_CONNECT
;
709 error
= sock_error( fd
);
711 else if (event
& POLLOUT
)
713 /* we got connected */
714 sock
->state
|= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
715 sock
->state
&= ~FD_CONNECT
;
716 sock
->connect_time
= current_time
;
719 else if (sock
->state
& FD_WINE_LISTENING
)
722 if (event
& (POLLERR
|POLLHUP
))
723 error
= sock_error( fd
);
727 /* normal data flow */
728 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
733 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
734 * has been closed, so we need to check for it explicitly here */
735 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
744 /* EAGAIN can happen if an async recv() falls between the server's poll()
745 call and the invocation of this routine */
746 if ( errno
!= EAGAIN
)
751 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
756 if ( (hangup_seen
|| event
& (POLLHUP
|POLLERR
)) && (sock
->state
& (FD_READ
|FD_WRITE
)) )
758 error
= error
? error
: sock_error( fd
);
759 if ( (event
& POLLERR
) || ( sock_shutdown_type
== SOCK_SHUTDOWN_EOF
&& (event
& POLLHUP
) ))
760 sock
->state
&= ~FD_WRITE
;
761 sock
->state
&= ~FD_READ
;
764 fprintf(stderr
, "socket %p aborted by error %d, event: %x\n", sock
, error
, event
);
771 event
= sock_dispatch_asyncs( sock
, event
, error
);
772 sock_dispatch_events( sock
, prevstate
, event
, error
);
774 /* if anyone is stupid enough to wait on the socket object itself,
775 * maybe we should wake them up too, just in case? */
776 wake_up( &sock
->obj
, 0 );
778 sock_reselect( sock
);
781 static void sock_dump( struct object
*obj
, int verbose
)
783 struct sock
*sock
= (struct sock
*)obj
;
784 assert( obj
->ops
== &sock_ops
);
785 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
786 sock
->fd
, sock
->state
,
787 sock
->mask
, sock
->pmask
, sock
->hmask
);
790 static int sock_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
792 struct sock
*sock
= (struct sock
*)obj
;
793 assert( obj
->ops
== &sock_ops
);
795 return check_fd_events( sock
->fd
, sock_get_poll_events( sock
->fd
) ) != 0;
798 static int sock_get_poll_events( struct fd
*fd
)
800 struct sock
*sock
= get_fd_user( fd
);
801 unsigned int mask
= sock
->mask
& ~sock
->hmask
;
802 unsigned int smask
= sock
->state
& mask
;
805 assert( sock
->obj
.ops
== &sock_ops
);
807 if (sock
->state
& FD_CONNECT
)
808 /* connecting, wait for writable */
811 if (!list_empty( &sock
->accept_list
) || sock
->accept_recv_req
)
813 ev
|= POLLIN
| POLLPRI
;
815 else if (async_queued( &sock
->read_q
))
817 if (async_waiting( &sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
819 else if (smask
& FD_READ
|| (sock
->state
& FD_WINE_LISTENING
&& mask
& FD_ACCEPT
))
820 ev
|= POLLIN
| POLLPRI
;
821 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
822 else if (sock
->type
== WS_SOCK_STREAM
&& (sock
->state
& FD_READ
) && (mask
& FD_CLOSE
) &&
823 !(sock
->hmask
& FD_READ
))
826 if (async_queued( &sock
->write_q
))
828 if (async_waiting( &sock
->write_q
)) ev
|= POLLOUT
;
830 else if (smask
& FD_WRITE
)
836 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
838 return FD_TYPE_SOCKET
;
841 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
843 struct sock
*sock
= get_fd_user( fd
);
844 struct async_queue
*queue
;
846 assert( sock
->obj
.ops
== &sock_ops
);
850 case ASYNC_TYPE_READ
:
851 queue
= &sock
->read_q
;
853 case ASYNC_TYPE_WRITE
:
854 queue
= &sock
->write_q
;
857 set_error( STATUS_INVALID_PARAMETER
);
861 if ( ( !( sock
->state
& (FD_READ
|FD_CONNECT
|FD_WINE_LISTENING
) ) && type
== ASYNC_TYPE_READ
) ||
862 ( !( sock
->state
& (FD_WRITE
|FD_CONNECT
) ) && type
== ASYNC_TYPE_WRITE
) )
864 set_error( STATUS_PIPE_DISCONNECTED
);
868 queue_async( queue
, async
);
869 sock_reselect( sock
);
871 set_error( STATUS_PENDING
);
874 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
876 struct sock
*sock
= get_fd_user( fd
);
877 struct accept_req
*req
, *next
;
879 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
881 struct iosb
*iosb
= async_get_iosb( req
->async
);
882 if (iosb
->status
!= STATUS_PENDING
)
883 free_accept_req( req
);
884 release_object( iosb
);
887 /* ignore reselect on ifchange queue */
888 if (&sock
->ifchange_q
!= queue
)
889 sock_reselect( sock
);
892 static struct fd
*sock_get_fd( struct object
*obj
)
894 struct sock
*sock
= (struct sock
*)obj
;
895 return (struct fd
*)grab_object( sock
->fd
);
898 static void sock_destroy( struct object
*obj
)
900 struct sock
*sock
= (struct sock
*)obj
;
901 struct accept_req
*req
, *next
;
903 assert( obj
->ops
== &sock_ops
);
905 /* FIXME: special socket shutdown stuff? */
907 if ( sock
->deferred
)
908 release_object( sock
->deferred
);
910 if (sock
->accept_recv_req
)
911 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
913 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
914 async_terminate( req
->async
, STATUS_CANCELLED
);
916 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
917 sock_release_ifchange( sock
);
918 free_async_queue( &sock
->read_q
);
919 free_async_queue( &sock
->write_q
);
920 free_async_queue( &sock
->ifchange_q
);
921 free_async_queue( &sock
->accept_q
);
922 if (sock
->event
) release_object( sock
->event
);
925 /* shut the socket down to force pending poll() calls in the client to return */
926 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
927 release_object( sock
->fd
);
931 static struct sock
*create_socket(void)
935 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
950 sock
->connect_time
= 0;
951 sock
->deferred
= NULL
;
952 sock
->ifchange_obj
= NULL
;
953 sock
->accept_recv_req
= NULL
;
954 init_async_queue( &sock
->read_q
);
955 init_async_queue( &sock
->write_q
);
956 init_async_queue( &sock
->ifchange_q
);
957 init_async_queue( &sock
->accept_q
);
958 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
959 list_init( &sock
->accept_list
);
963 static int get_unix_family( int family
)
967 case WS_AF_INET
: return AF_INET
;
968 case WS_AF_INET6
: return AF_INET6
;
970 case WS_AF_IPX
: return AF_IPX
;
973 case WS_AF_IRDA
: return AF_IRDA
;
975 case WS_AF_UNSPEC
: return AF_UNSPEC
;
980 static int get_unix_type( int type
)
984 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
985 case WS_SOCK_RAW
: return SOCK_RAW
;
986 case WS_SOCK_STREAM
: return SOCK_STREAM
;
991 static int get_unix_protocol( int protocol
)
993 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
998 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
999 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
1000 case WS_IPPROTO_IP
: return IPPROTO_IP
;
1001 case WS_IPPROTO_IPIP
: return IPPROTO_IPIP
;
1002 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
1003 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
1004 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
1005 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
1010 static void set_dont_fragment( int fd
, int level
, int value
)
1014 if (level
== IPPROTO_IP
)
1017 optname
= IP_DONTFRAG
;
1018 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1019 optname
= IP_MTU_DISCOVER
;
1020 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1027 #ifdef IPV6_DONTFRAG
1028 optname
= IPV6_DONTFRAG
;
1029 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1030 optname
= IPV6_MTU_DISCOVER
;
1031 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1037 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1040 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
, unsigned int flags
)
1042 unsigned int options
= 0;
1043 int sockfd
, unix_type
, unix_family
, unix_protocol
;
1045 unix_family
= get_unix_family( family
);
1046 unix_type
= get_unix_type( type
);
1047 unix_protocol
= get_unix_protocol( protocol
);
1049 if (unix_protocol
< 0)
1051 if (type
&& unix_type
< 0)
1052 set_win32_error( WSAESOCKTNOSUPPORT
);
1054 set_win32_error( WSAEPROTONOSUPPORT
);
1057 if (unix_family
< 0)
1059 if (family
>= 0 && unix_type
< 0)
1060 set_win32_error( WSAESOCKTNOSUPPORT
);
1062 set_win32_error( WSAEAFNOSUPPORT
);
1066 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1069 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1070 else set_win32_error( sock_get_error( errno
));
1073 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1075 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1078 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1081 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1084 /* Should we retrieve val using a getsockopt call and then
1085 * set the modified one? */
1086 val
.ipx_pt
= ipx_type
;
1087 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1092 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1094 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1095 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1096 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1097 else if (unix_type
== SOCK_STREAM
)
1098 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1102 if (unix_family
== AF_INET6
)
1104 static const int enable
= 1;
1105 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1109 sock
->state
= (type
!= SOCK_STREAM
) ? (FD_READ
|FD_WRITE
) : 0;
1110 sock
->flags
= flags
;
1111 sock
->proto
= protocol
;
1113 sock
->family
= family
;
1117 options
= get_fd_options( sock
->fd
);
1118 release_object( sock
->fd
);
1121 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1125 sock_reselect( sock
);
1130 /* accepts a socket and inits it */
1131 static int accept_new_fd( struct sock
*sock
)
1134 /* Try to accept(2). We can't be safe that this an already connected socket
1135 * or that accept() is allowed on it. In those cases we will get -1/errno
1138 struct sockaddr saddr
;
1139 socklen_t slen
= sizeof(saddr
);
1140 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1142 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1144 set_win32_error( sock_get_error( errno
));
1148 /* accept a socket (creates a new fd) */
1149 static struct sock
*accept_socket( struct sock
*sock
)
1151 struct sock
*acceptsock
;
1154 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1156 if ( sock
->deferred
)
1158 acceptsock
= sock
->deferred
;
1159 sock
->deferred
= NULL
;
1163 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1164 if (!(acceptsock
= create_socket()))
1170 /* newly created socket gets the same properties of the listening socket */
1171 acceptsock
->state
= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
1172 if (sock
->state
& FD_WINE_NONBLOCKING
)
1173 acceptsock
->state
|= FD_WINE_NONBLOCKING
;
1174 acceptsock
->mask
= sock
->mask
;
1175 acceptsock
->proto
= sock
->proto
;
1176 acceptsock
->type
= sock
->type
;
1177 acceptsock
->family
= sock
->family
;
1178 acceptsock
->window
= sock
->window
;
1179 acceptsock
->message
= sock
->message
;
1180 acceptsock
->connect_time
= current_time
;
1181 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
1182 acceptsock
->flags
= sock
->flags
;
1183 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1184 get_fd_options( sock
->fd
) )))
1186 release_object( acceptsock
);
1191 sock
->pmask
&= ~FD_ACCEPT
;
1192 sock
->hmask
&= ~FD_ACCEPT
;
1193 sock_reselect( sock
);
1197 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
1202 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
1204 if ( sock
->deferred
)
1206 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
1207 get_fd_options( acceptsock
->fd
) );
1211 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
1213 release_object( sock
->deferred
);
1214 sock
->deferred
= NULL
;
1218 if ((acceptfd
= accept_new_fd( sock
)) == -1)
1221 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1222 get_fd_options( acceptsock
->fd
) )))
1226 acceptsock
->state
|= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
1227 acceptsock
->hmask
= 0;
1228 acceptsock
->pmask
= 0;
1229 acceptsock
->polling
= 0;
1230 acceptsock
->proto
= sock
->proto
;
1231 acceptsock
->type
= sock
->type
;
1232 acceptsock
->family
= sock
->family
;
1233 acceptsock
->wparam
= 0;
1234 acceptsock
->deferred
= NULL
;
1235 acceptsock
->connect_time
= current_time
;
1236 fd_copy_completion( acceptsock
->fd
, newfd
);
1237 release_object( acceptsock
->fd
);
1238 acceptsock
->fd
= newfd
;
1241 sock
->pmask
&= ~FD_ACCEPT
;
1242 sock
->hmask
&= ~FD_ACCEPT
;
1243 sock_reselect( sock
);
1248 /* return an errno value mapped to a WSA error */
1249 static unsigned int sock_get_error( int err
)
1253 case EINTR
: return WSAEINTR
;
1254 case EBADF
: return WSAEBADF
;
1256 case EACCES
: return WSAEACCES
;
1257 case EFAULT
: return WSAEFAULT
;
1258 case EINVAL
: return WSAEINVAL
;
1259 case EMFILE
: return WSAEMFILE
;
1260 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
1261 case EINPROGRESS
: return WSAEINPROGRESS
;
1262 case EALREADY
: return WSAEALREADY
;
1263 case ENOTSOCK
: return WSAENOTSOCK
;
1264 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
1265 case EMSGSIZE
: return WSAEMSGSIZE
;
1266 case EPROTOTYPE
: return WSAEPROTOTYPE
;
1267 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
1268 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
1269 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
1270 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
1271 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
1272 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
1273 case EADDRINUSE
: return WSAEADDRINUSE
;
1274 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
1275 case ENETDOWN
: return WSAENETDOWN
;
1276 case ENETUNREACH
: return WSAENETUNREACH
;
1277 case ENETRESET
: return WSAENETRESET
;
1278 case ECONNABORTED
: return WSAECONNABORTED
;
1280 case ECONNRESET
: return WSAECONNRESET
;
1281 case ENOBUFS
: return WSAENOBUFS
;
1282 case EISCONN
: return WSAEISCONN
;
1283 case ENOTCONN
: return WSAENOTCONN
;
1284 case ESHUTDOWN
: return WSAESHUTDOWN
;
1285 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
1286 case ETIMEDOUT
: return WSAETIMEDOUT
;
1287 case ECONNREFUSED
: return WSAECONNREFUSED
;
1288 case ELOOP
: return WSAELOOP
;
1289 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
1290 case EHOSTDOWN
: return WSAEHOSTDOWN
;
1291 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
1292 case ENOTEMPTY
: return WSAENOTEMPTY
;
1294 case EPROCLIM
: return WSAEPROCLIM
;
1297 case EUSERS
: return WSAEUSERS
;
1300 case EDQUOT
: return WSAEDQUOT
;
1303 case ESTALE
: return WSAESTALE
;
1306 case EREMOTE
: return WSAEREMOTE
;
1312 perror("wineserver: sock_get_error() can't map error");
1317 static int sock_get_ntstatus( int err
)
1321 case EBADF
: return STATUS_INVALID_HANDLE
;
1322 case EBUSY
: return STATUS_DEVICE_BUSY
;
1324 case EACCES
: return STATUS_ACCESS_DENIED
;
1325 case EFAULT
: return STATUS_NO_MEMORY
;
1326 case EINVAL
: return STATUS_INVALID_PARAMETER
;
1328 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
1329 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
1330 case EINPROGRESS
: return STATUS_PENDING
;
1331 case EALREADY
: return STATUS_NETWORK_BUSY
;
1332 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
1333 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
1334 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
1335 case EPROTONOSUPPORT
:
1336 case ESOCKTNOSUPPORT
:
1339 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
1340 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
1341 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
1342 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
1343 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
1344 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
1345 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
1346 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
1347 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
1348 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
1349 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
1350 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
1352 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
1353 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
1355 case 0: return STATUS_SUCCESS
;
1358 perror("wineserver: sock_get_ntstatus() can't map error");
1359 return STATUS_UNSUCCESSFUL
;
1363 static struct accept_req
*alloc_accept_req( struct sock
*acceptsock
, struct async
*async
,
1364 const struct afd_accept_into_params
*params
)
1366 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
1370 req
->async
= (struct async
*)grab_object( async
);
1371 req
->acceptsock
= acceptsock
;
1377 req
->recv_len
= params
->recv_len
;
1378 req
->local_len
= params
->local_len
;
1384 static int sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
1386 struct sock
*sock
= get_fd_user( fd
);
1388 assert( sock
->obj
.ops
== &sock_ops
);
1390 if (get_unix_fd( fd
) == -1 && code
!= IOCTL_AFD_CREATE
) return 0;
1394 case IOCTL_AFD_CREATE
:
1396 const struct afd_create_params
*params
= get_req_data();
1398 if (get_req_data_size() != sizeof(*params
))
1400 set_error( STATUS_INVALID_PARAMETER
);
1403 init_socket( sock
, params
->family
, params
->type
, params
->protocol
, params
->flags
);
1407 case IOCTL_AFD_ACCEPT
:
1409 struct sock
*acceptsock
;
1410 obj_handle_t handle
;
1412 if (get_reply_max_size() != sizeof(handle
))
1414 set_error( STATUS_BUFFER_TOO_SMALL
);
1418 if (!(acceptsock
= accept_socket( sock
)))
1420 struct accept_req
*req
;
1422 if (sock
->state
& FD_WINE_NONBLOCKING
) return 0;
1423 if (get_error() != (0xc0010000 | WSAEWOULDBLOCK
)) return 0;
1425 if (!(req
= alloc_accept_req( NULL
, async
, NULL
))) return 0;
1426 list_add_tail( &sock
->accept_list
, &req
->entry
);
1428 queue_async( &sock
->accept_q
, async
);
1429 sock_reselect( sock
);
1430 set_error( STATUS_PENDING
);
1433 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
1434 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
1435 acceptsock
->wparam
= handle
;
1436 release_object( acceptsock
);
1437 set_reply_data( &handle
, sizeof(handle
) );
1441 case IOCTL_AFD_ACCEPT_INTO
:
1443 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
1444 const struct afd_accept_into_params
*params
= get_req_data();
1445 struct sock
*acceptsock
;
1446 unsigned int remote_len
;
1447 struct accept_req
*req
;
1449 if (get_req_data_size() != sizeof(*params
) ||
1450 get_reply_max_size() < params
->recv_len
||
1451 get_reply_max_size() - params
->recv_len
< params
->local_len
)
1453 set_error( STATUS_BUFFER_TOO_SMALL
);
1457 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
1458 if (remote_len
< sizeof(int))
1460 set_error( STATUS_INVALID_PARAMETER
);
1464 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
1467 if (acceptsock
->accept_recv_req
)
1469 release_object( acceptsock
);
1470 set_win32_error( WSAEINVAL
);
1474 if (!(req
= alloc_accept_req( acceptsock
, async
, params
))) return 0;
1475 list_add_tail( &sock
->accept_list
, &req
->entry
);
1476 acceptsock
->accept_recv_req
= req
;
1477 release_object( acceptsock
);
1479 acceptsock
->wparam
= params
->accept_handle
;
1480 queue_async( &sock
->accept_q
, async
);
1481 sock_reselect( sock
);
1482 set_error( STATUS_PENDING
);
1486 case IOCTL_AFD_ADDRESS_LIST_CHANGE
:
1487 if ((sock
->state
& FD_WINE_NONBLOCKING
) && async_is_blocking( async
))
1489 set_win32_error( WSAEWOULDBLOCK
);
1492 if (!sock_get_ifchange( sock
)) return 0;
1493 queue_async( &sock
->ifchange_q
, async
);
1494 set_error( STATUS_PENDING
);
1498 set_error( STATUS_NOT_SUPPORTED
);
1503 #ifdef HAVE_LINUX_RTNETLINK_H
1505 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
1506 static struct object
*ifchange_object
;
1508 static void ifchange_dump( struct object
*obj
, int verbose
);
1509 static struct fd
*ifchange_get_fd( struct object
*obj
);
1510 static void ifchange_destroy( struct object
*obj
);
1512 static int ifchange_get_poll_events( struct fd
*fd
);
1513 static void ifchange_poll_event( struct fd
*fd
, int event
);
1517 struct object obj
; /* object header */
1518 struct fd
*fd
; /* interface change file descriptor */
1519 struct list sockets
; /* list of sockets to send interface change notifications */
1522 static const struct object_ops ifchange_ops
=
1524 sizeof(struct ifchange
), /* size */
1525 ifchange_dump
, /* dump */
1526 no_get_type
, /* get_type */
1527 add_queue
, /* add_queue */
1528 NULL
, /* remove_queue */
1529 NULL
, /* signaled */
1530 no_satisfied
, /* satisfied */
1531 no_signal
, /* signal */
1532 ifchange_get_fd
, /* get_fd */
1533 default_fd_map_access
, /* map_access */
1534 default_get_sd
, /* get_sd */
1535 default_set_sd
, /* set_sd */
1536 no_get_full_name
, /* get_full_name */
1537 no_lookup_name
, /* lookup_name */
1538 no_link_name
, /* link_name */
1539 NULL
, /* unlink_name */
1540 no_open_file
, /* open_file */
1541 no_kernel_obj_list
, /* get_kernel_obj_list */
1542 no_close_handle
, /* close_handle */
1543 ifchange_destroy
/* destroy */
1546 static const struct fd_ops ifchange_fd_ops
=
1548 ifchange_get_poll_events
, /* get_poll_events */
1549 ifchange_poll_event
, /* poll_event */
1550 NULL
, /* get_fd_type */
1551 no_fd_read
, /* read */
1552 no_fd_write
, /* write */
1553 no_fd_flush
, /* flush */
1554 no_fd_get_file_info
, /* get_file_info */
1555 no_fd_get_volume_info
, /* get_volume_info */
1556 no_fd_ioctl
, /* ioctl */
1557 NULL
, /* queue_async */
1558 NULL
/* reselect_async */
1561 static void ifchange_dump( struct object
*obj
, int verbose
)
1563 assert( obj
->ops
== &ifchange_ops
);
1564 fprintf( stderr
, "Interface change\n" );
1567 static struct fd
*ifchange_get_fd( struct object
*obj
)
1569 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1570 return (struct fd
*)grab_object( ifchange
->fd
);
1573 static void ifchange_destroy( struct object
*obj
)
1575 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1576 assert( obj
->ops
== &ifchange_ops
);
1578 release_object( ifchange
->fd
);
1580 /* reset the global ifchange object so that it will be recreated if it is needed again */
1581 assert( obj
== ifchange_object
);
1582 ifchange_object
= NULL
;
1585 static int ifchange_get_poll_events( struct fd
*fd
)
1590 /* wake up all the sockets waiting for a change notification event */
1591 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
1593 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1594 struct list
*ptr
, *next
;
1595 assert( obj
->ops
== &ifchange_ops
);
1596 assert( obj
== ifchange_object
);
1598 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
1600 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
1602 assert( sock
->ifchange_obj
);
1603 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
1604 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
1608 static void ifchange_poll_event( struct fd
*fd
, int event
)
1610 struct object
*ifchange
= get_fd_user( fd
);
1611 unsigned int status
= STATUS_PENDING
;
1612 char buffer
[PIPE_BUF
];
1615 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
1618 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
1619 return; /* retry when poll() says the socket is ready */
1620 status
= sock_get_ntstatus( errno
);
1624 struct nlmsghdr
*nlh
;
1626 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
1628 if (nlh
->nlmsg_type
== NLMSG_DONE
)
1630 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
1631 status
= STATUS_SUCCESS
;
1634 else status
= STATUS_CANCELLED
;
1636 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
1641 /* we only need one of these interface notification objects, all of the sockets dependent upon
1642 * it will wake up when a notification event occurs */
1643 static struct object
*get_ifchange( void )
1645 #ifdef HAVE_LINUX_RTNETLINK_H
1646 struct ifchange
*ifchange
;
1647 struct sockaddr_nl addr
;
1650 if (ifchange_object
)
1652 /* increment the refcount for each socket that uses the ifchange object */
1653 return grab_object( ifchange_object
);
1656 /* create the socket we need for processing interface change notifications */
1657 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1660 set_win32_error( sock_get_error( errno
));
1663 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1664 memset( &addr
, 0, sizeof(addr
) );
1665 addr
.nl_family
= AF_NETLINK
;
1666 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
1667 /* bind the socket to the special netlink kernel interface */
1668 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
1671 set_win32_error( sock_get_error( errno
));
1674 if (!(ifchange
= alloc_object( &ifchange_ops
)))
1677 set_error( STATUS_NO_MEMORY
);
1680 list_init( &ifchange
->sockets
);
1681 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
1683 release_object( ifchange
);
1684 set_error( STATUS_NO_MEMORY
);
1687 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
1689 /* the ifchange object is now successfully configured */
1690 ifchange_object
= &ifchange
->obj
;
1691 return &ifchange
->obj
;
1693 set_error( STATUS_NOT_SUPPORTED
);
1698 /* add the socket to the interface change notification list */
1699 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
1701 #ifdef HAVE_LINUX_RTNETLINK_H
1702 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1704 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
1708 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1709 static struct object
*sock_get_ifchange( struct sock
*sock
)
1711 struct object
*ifchange
;
1713 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
1714 return sock
->ifchange_obj
;
1716 if (!(ifchange
= get_ifchange()))
1719 /* add the socket to the ifchange notification list */
1720 ifchange_add_sock( ifchange
, sock
);
1721 sock
->ifchange_obj
= ifchange
;
1725 /* destroy an existing ifchange queue for a specific socket */
1726 static void sock_release_ifchange( struct sock
*sock
)
1728 if (sock
->ifchange_obj
)
1730 list_remove( &sock
->ifchange_entry
);
1731 release_object( sock
->ifchange_obj
);
1732 sock
->ifchange_obj
= NULL
;
1736 static struct object_type
*socket_device_get_type( struct object
*obj
);
1737 static void socket_device_dump( struct object
*obj
, int verbose
);
1738 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
);
1739 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
1740 unsigned int sharing
, unsigned int options
);
1742 static const struct object_ops socket_device_ops
=
1744 sizeof(struct object
), /* size */
1745 socket_device_dump
, /* dump */
1746 socket_device_get_type
, /* get_type */
1747 no_add_queue
, /* add_queue */
1748 NULL
, /* remove_queue */
1749 NULL
, /* signaled */
1750 no_satisfied
, /* satisfied */
1751 no_signal
, /* signal */
1752 no_get_fd
, /* get_fd */
1753 default_fd_map_access
, /* map_access */
1754 default_get_sd
, /* get_sd */
1755 default_set_sd
, /* set_sd */
1756 default_get_full_name
, /* get_full_name */
1757 socket_device_lookup_name
, /* lookup_name */
1758 directory_link_name
, /* link_name */
1759 default_unlink_name
, /* unlink_name */
1760 socket_device_open_file
, /* open_file */
1761 no_kernel_obj_list
, /* get_kernel_obj_list */
1762 no_close_handle
, /* close_handle */
1763 no_destroy
/* destroy */
1766 static struct object_type
*socket_device_get_type( struct object
*obj
)
1768 static const WCHAR name
[] = {'D','e','v','i','c','e'};
1769 static const struct unicode_str str
= { name
, sizeof(name
) };
1770 return get_object_type( &str
);
1773 static void socket_device_dump( struct object
*obj
, int verbose
)
1775 fputs( "Socket device\n", stderr
);
1778 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
)
1783 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
1784 unsigned int sharing
, unsigned int options
)
1788 if (!(sock
= create_socket())) return NULL
;
1789 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
1791 release_object( sock
);
1797 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
1798 unsigned int attr
, const struct security_descriptor
*sd
)
1800 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
1803 /* set socket event parameters */
1804 DECL_HANDLER(set_socket_event
)
1807 struct event
*old_event
;
1809 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1810 FILE_WRITE_ATTRIBUTES
, &sock_ops
))) return;
1811 if (get_unix_fd( sock
->fd
) == -1) return;
1812 old_event
= sock
->event
;
1813 sock
->mask
= req
->mask
;
1814 sock
->hmask
&= ~req
->mask
; /* re-enable held events */
1816 sock
->window
= req
->window
;
1817 sock
->message
= req
->msg
;
1818 sock
->wparam
= req
->handle
; /* wparam is the socket handle */
1819 if (req
->event
) sock
->event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
1821 if (debug_level
&& sock
->event
) fprintf(stderr
, "event ptr: %p\n", sock
->event
);
1823 sock_reselect( sock
);
1825 sock
->state
|= FD_WINE_NONBLOCKING
;
1827 /* if a network event is pending, signal the event object
1828 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1829 before a WSAEventSelect() was done on it.
1830 (when dealing with Asynchronous socket) */
1831 sock_wake_up( sock
);
1833 if (old_event
) release_object( old_event
); /* we're through with it */
1834 release_object( &sock
->obj
);
1837 /* get socket event parameters */
1838 DECL_HANDLER(get_socket_event
)
1842 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1843 FILE_READ_ATTRIBUTES
, &sock_ops
))) return;
1844 if (get_unix_fd( sock
->fd
) == -1) return;
1845 reply
->mask
= sock
->mask
;
1846 reply
->pmask
= sock
->pmask
;
1847 reply
->state
= sock
->state
;
1848 set_reply_data( sock
->errors
, min( get_reply_max_size(), sizeof(sock
->errors
) ));
1854 struct event
*cevent
= get_event_obj( current
->process
, req
->c_event
,
1855 EVENT_MODIFY_STATE
);
1858 reset_event( cevent
);
1859 release_object( cevent
);
1863 sock_reselect( sock
);
1865 release_object( &sock
->obj
);
1868 /* re-enable pending socket events */
1869 DECL_HANDLER(enable_socket_event
)
1873 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1874 FILE_WRITE_ATTRIBUTES
, &sock_ops
)))
1877 if (get_unix_fd( sock
->fd
) == -1) return;
1879 /* for event-based notification, windows erases stale events */
1880 sock
->pmask
&= ~req
->mask
;
1882 sock
->hmask
&= ~req
->mask
;
1883 sock
->state
|= req
->sstate
;
1884 sock
->state
&= ~req
->cstate
;
1885 if (sock
->type
!= WS_SOCK_STREAM
) sock
->state
&= ~STREAM_FLAG_MASK
;
1887 sock_reselect( sock
);
1889 release_object( &sock
->obj
);
1892 DECL_HANDLER(set_socket_deferred
)
1894 struct sock
*sock
, *acceptsock
;
1896 sock
=(struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
, &sock_ops
);
1900 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, req
->deferred
, 0, &sock_ops
);
1903 release_object( sock
);
1906 sock
->deferred
= acceptsock
;
1907 release_object( sock
);
1910 DECL_HANDLER(get_socket_info
)
1914 sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
, &sock_ops
);
1917 if (get_unix_fd( sock
->fd
) == -1) return;
1919 reply
->family
= sock
->family
;
1920 reply
->type
= sock
->type
;
1921 reply
->protocol
= sock
->proto
;
1923 release_object( &sock
->obj
);