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
;
462 unsigned int remote_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
)
477 sock_reselect( req
->acceptsock
);
478 set_error( STATUS_PENDING
);
482 set_win32_error( sock_get_error( errno
) );
489 if (req
->local_len
< sizeof(int))
491 set_error( STATUS_BUFFER_TOO_SMALL
);
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
) );
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
) );
518 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
520 iosb
->status
= STATUS_SUCCESS
;
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
;
532 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
536 if (!accept_into_socket( sock
, acceptsock
)) return;
538 iosb
= async_get_iosb( async
);
539 fill_accept_output( req
, iosb
);
540 release_object( iosb
);
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
);
553 iosb
= async_get_iosb( async
);
554 if (!(iosb
->out_data
= malloc( sizeof(handle
) )))
556 release_object( iosb
);
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
;
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
)
591 complete_async_accept( sock
, req
);
592 if (get_error() != STATUS_PENDING
)
593 async_terminate( req
->async
, get_error() );
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
);
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
);
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
);
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
);
661 sock
->pmask
|= FD_READ
;
662 sock
->hmask
|= FD_READ
;
663 sock
->errors
[FD_READ_BIT
] = 0;
668 sock
->pmask
|= FD_WRITE
;
669 sock
->hmask
|= FD_WRITE
;
670 sock
->errors
[FD_WRITE_BIT
] = 0;
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
);
687 sock_wake_up( sock
);
690 static void sock_poll_event( struct fd
*fd
, int event
)
692 struct sock
*sock
= get_fd_user( fd
);
694 int prevstate
= sock
->state
;
697 assert( sock
->obj
.ops
== &sock_ops
);
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
;
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
)
724 if (event
& (POLLERR
|POLLHUP
))
725 error
= sock_error( fd
);
729 /* normal data flow */
730 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
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
);
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
)
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
;
766 fprintf(stderr
, "socket %p aborted by error %d, event: %x\n", sock
, error
, event
);
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
;
807 assert( sock
->obj
.ops
== &sock_ops
);
809 if (sock
->state
& FD_CONNECT
)
810 /* connecting, wait for writable */
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
))
828 if (async_queued( &sock
->write_q
))
830 if (async_waiting( &sock
->write_q
)) ev
|= POLLOUT
;
832 else if (smask
& FD_WRITE
)
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
);
852 case ASYNC_TYPE_READ
:
853 queue
= &sock
->read_q
;
855 case ASYNC_TYPE_WRITE
:
856 queue
= &sock
->write_q
;
859 set_error( STATUS_INVALID_PARAMETER
);
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
);
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
);
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)
937 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
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
);
965 static int get_unix_family( int family
)
969 case WS_AF_INET
: return AF_INET
;
970 case WS_AF_INET6
: return AF_INET6
;
972 case WS_AF_IPX
: return AF_IPX
;
975 case WS_AF_IRDA
: return AF_IRDA
;
977 case WS_AF_UNSPEC
: return AF_UNSPEC
;
982 static int get_unix_type( int 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
;
993 static int get_unix_protocol( int protocol
)
995 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
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
;
1012 static void set_dont_fragment( int fd
, int level
, int value
)
1016 if (level
== IPPROTO_IP
)
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
;
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
;
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
);
1056 set_win32_error( WSAEPROTONOSUPPORT
);
1059 if (unix_family
< 0)
1061 if (family
>= 0 && unix_type
< 0)
1062 set_win32_error( WSAESOCKTNOSUPPORT
);
1064 set_win32_error( WSAEAFNOSUPPORT
);
1068 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1071 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1072 else set_win32_error( sock_get_error( errno
));
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)
1080 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1083 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
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
) );
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
);
1104 if (unix_family
== AF_INET6
)
1106 static const int enable
= 1;
1107 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1111 sock
->state
= (type
!= SOCK_STREAM
) ? (FD_READ
|FD_WRITE
) : 0;
1112 sock
->flags
= flags
;
1113 sock
->proto
= protocol
;
1115 sock
->family
= family
;
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
)))
1127 sock_reselect( sock
);
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
1140 struct sockaddr saddr
;
1141 socklen_t slen
= sizeof(saddr
);
1142 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1144 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1146 set_win32_error( sock_get_error( errno
));
1150 /* accept a socket (creates a new fd) */
1151 static struct sock
*accept_socket( struct sock
*sock
)
1153 struct sock
*acceptsock
;
1156 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1158 if ( sock
->deferred
)
1160 acceptsock
= sock
->deferred
;
1161 sock
->deferred
= NULL
;
1165 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1166 if (!(acceptsock
= create_socket()))
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
);
1193 sock
->pmask
&= ~FD_ACCEPT
;
1194 sock
->hmask
&= ~FD_ACCEPT
;
1195 sock_reselect( sock
);
1199 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
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
) );
1213 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
1215 release_object( sock
->deferred
);
1216 sock
->deferred
= NULL
;
1220 if ((acceptfd
= accept_new_fd( sock
)) == -1)
1223 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1224 get_fd_options( acceptsock
->fd
) )))
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
;
1243 sock
->pmask
&= ~FD_ACCEPT
;
1244 sock
->hmask
&= ~FD_ACCEPT
;
1245 sock_reselect( sock
);
1250 /* return an errno value mapped to a WSA error */
1251 static unsigned int sock_get_error( int err
)
1255 case EINTR
: return WSAEINTR
;
1256 case EBADF
: return WSAEBADF
;
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
;
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
;
1296 case EPROCLIM
: return WSAEPROCLIM
;
1299 case EUSERS
: return WSAEUSERS
;
1302 case EDQUOT
: return WSAEDQUOT
;
1305 case ESTALE
: return WSAESTALE
;
1308 case EREMOTE
: return WSAEREMOTE
;
1314 perror("wineserver: sock_get_error() can't map error");
1319 static int sock_get_ntstatus( int err
)
1323 case EBADF
: return STATUS_INVALID_HANDLE
;
1324 case EBUSY
: return STATUS_DEVICE_BUSY
;
1326 case EACCES
: return STATUS_ACCESS_DENIED
;
1327 case EFAULT
: return STATUS_NO_MEMORY
;
1328 case EINVAL
: return STATUS_INVALID_PARAMETER
;
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
:
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
;
1354 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
1355 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
1357 case 0: return STATUS_SUCCESS
;
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
) );
1372 req
->async
= (struct async
*)grab_object( async
);
1373 req
->acceptsock
= acceptsock
;
1379 req
->recv_len
= params
->recv_len
;
1380 req
->local_len
= params
->local_len
;
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;
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
);
1405 init_socket( sock
, params
->family
, params
->type
, params
->protocol
, params
->flags
);
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
);
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
);
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
) );
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
);
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
);
1466 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
1469 if (acceptsock
->accept_recv_req
)
1471 release_object( acceptsock
);
1472 set_win32_error( WSAEINVAL
);
1476 if (!(req
= alloc_accept_req( acceptsock
, async
, params
)))
1478 release_object( acceptsock
);
1481 list_add_tail( &sock
->accept_list
, &req
->entry
);
1482 acceptsock
->accept_recv_req
= req
;
1483 release_object( acceptsock
);
1485 acceptsock
->wparam
= params
->accept_handle
;
1486 queue_async( &sock
->accept_q
, async
);
1487 sock_reselect( sock
);
1488 set_error( STATUS_PENDING
);
1492 case IOCTL_AFD_ADDRESS_LIST_CHANGE
:
1493 if ((sock
->state
& FD_WINE_NONBLOCKING
) && async_is_blocking( async
))
1495 set_win32_error( WSAEWOULDBLOCK
);
1498 if (!sock_get_ifchange( sock
)) return 0;
1499 queue_async( &sock
->ifchange_q
, async
);
1500 set_error( STATUS_PENDING
);
1504 set_error( STATUS_NOT_SUPPORTED
);
1509 #ifdef HAVE_LINUX_RTNETLINK_H
1511 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
1512 static struct object
*ifchange_object
;
1514 static void ifchange_dump( struct object
*obj
, int verbose
);
1515 static struct fd
*ifchange_get_fd( struct object
*obj
);
1516 static void ifchange_destroy( struct object
*obj
);
1518 static int ifchange_get_poll_events( struct fd
*fd
);
1519 static void ifchange_poll_event( struct fd
*fd
, int event
);
1523 struct object obj
; /* object header */
1524 struct fd
*fd
; /* interface change file descriptor */
1525 struct list sockets
; /* list of sockets to send interface change notifications */
1528 static const struct object_ops ifchange_ops
=
1530 sizeof(struct ifchange
), /* size */
1531 ifchange_dump
, /* dump */
1532 no_get_type
, /* get_type */
1533 add_queue
, /* add_queue */
1534 NULL
, /* remove_queue */
1535 NULL
, /* signaled */
1536 no_satisfied
, /* satisfied */
1537 no_signal
, /* signal */
1538 ifchange_get_fd
, /* get_fd */
1539 default_fd_map_access
, /* map_access */
1540 default_get_sd
, /* get_sd */
1541 default_set_sd
, /* set_sd */
1542 no_get_full_name
, /* get_full_name */
1543 no_lookup_name
, /* lookup_name */
1544 no_link_name
, /* link_name */
1545 NULL
, /* unlink_name */
1546 no_open_file
, /* open_file */
1547 no_kernel_obj_list
, /* get_kernel_obj_list */
1548 no_close_handle
, /* close_handle */
1549 ifchange_destroy
/* destroy */
1552 static const struct fd_ops ifchange_fd_ops
=
1554 ifchange_get_poll_events
, /* get_poll_events */
1555 ifchange_poll_event
, /* poll_event */
1556 NULL
, /* get_fd_type */
1557 no_fd_read
, /* read */
1558 no_fd_write
, /* write */
1559 no_fd_flush
, /* flush */
1560 no_fd_get_file_info
, /* get_file_info */
1561 no_fd_get_volume_info
, /* get_volume_info */
1562 no_fd_ioctl
, /* ioctl */
1563 NULL
, /* queue_async */
1564 NULL
/* reselect_async */
1567 static void ifchange_dump( struct object
*obj
, int verbose
)
1569 assert( obj
->ops
== &ifchange_ops
);
1570 fprintf( stderr
, "Interface change\n" );
1573 static struct fd
*ifchange_get_fd( struct object
*obj
)
1575 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1576 return (struct fd
*)grab_object( ifchange
->fd
);
1579 static void ifchange_destroy( struct object
*obj
)
1581 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1582 assert( obj
->ops
== &ifchange_ops
);
1584 release_object( ifchange
->fd
);
1586 /* reset the global ifchange object so that it will be recreated if it is needed again */
1587 assert( obj
== ifchange_object
);
1588 ifchange_object
= NULL
;
1591 static int ifchange_get_poll_events( struct fd
*fd
)
1596 /* wake up all the sockets waiting for a change notification event */
1597 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
1599 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1600 struct list
*ptr
, *next
;
1601 assert( obj
->ops
== &ifchange_ops
);
1602 assert( obj
== ifchange_object
);
1604 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
1606 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
1608 assert( sock
->ifchange_obj
);
1609 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
1610 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
1614 static void ifchange_poll_event( struct fd
*fd
, int event
)
1616 struct object
*ifchange
= get_fd_user( fd
);
1617 unsigned int status
= STATUS_PENDING
;
1618 char buffer
[PIPE_BUF
];
1621 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
1624 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
1625 return; /* retry when poll() says the socket is ready */
1626 status
= sock_get_ntstatus( errno
);
1630 struct nlmsghdr
*nlh
;
1632 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
1634 if (nlh
->nlmsg_type
== NLMSG_DONE
)
1636 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
1637 status
= STATUS_SUCCESS
;
1640 else status
= STATUS_CANCELLED
;
1642 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
1647 /* we only need one of these interface notification objects, all of the sockets dependent upon
1648 * it will wake up when a notification event occurs */
1649 static struct object
*get_ifchange( void )
1651 #ifdef HAVE_LINUX_RTNETLINK_H
1652 struct ifchange
*ifchange
;
1653 struct sockaddr_nl addr
;
1656 if (ifchange_object
)
1658 /* increment the refcount for each socket that uses the ifchange object */
1659 return grab_object( ifchange_object
);
1662 /* create the socket we need for processing interface change notifications */
1663 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1666 set_win32_error( sock_get_error( errno
));
1669 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1670 memset( &addr
, 0, sizeof(addr
) );
1671 addr
.nl_family
= AF_NETLINK
;
1672 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
1673 /* bind the socket to the special netlink kernel interface */
1674 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
1677 set_win32_error( sock_get_error( errno
));
1680 if (!(ifchange
= alloc_object( &ifchange_ops
)))
1683 set_error( STATUS_NO_MEMORY
);
1686 list_init( &ifchange
->sockets
);
1687 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
1689 release_object( ifchange
);
1690 set_error( STATUS_NO_MEMORY
);
1693 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
1695 /* the ifchange object is now successfully configured */
1696 ifchange_object
= &ifchange
->obj
;
1697 return &ifchange
->obj
;
1699 set_error( STATUS_NOT_SUPPORTED
);
1704 /* add the socket to the interface change notification list */
1705 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
1707 #ifdef HAVE_LINUX_RTNETLINK_H
1708 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1710 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
1714 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1715 static struct object
*sock_get_ifchange( struct sock
*sock
)
1717 struct object
*ifchange
;
1719 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
1720 return sock
->ifchange_obj
;
1722 if (!(ifchange
= get_ifchange()))
1725 /* add the socket to the ifchange notification list */
1726 ifchange_add_sock( ifchange
, sock
);
1727 sock
->ifchange_obj
= ifchange
;
1731 /* destroy an existing ifchange queue for a specific socket */
1732 static void sock_release_ifchange( struct sock
*sock
)
1734 if (sock
->ifchange_obj
)
1736 list_remove( &sock
->ifchange_entry
);
1737 release_object( sock
->ifchange_obj
);
1738 sock
->ifchange_obj
= NULL
;
1742 static struct object_type
*socket_device_get_type( struct object
*obj
);
1743 static void socket_device_dump( struct object
*obj
, int verbose
);
1744 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
1745 unsigned int attr
, struct object
*root
);
1746 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
1747 unsigned int sharing
, unsigned int options
);
1749 static const struct object_ops socket_device_ops
=
1751 sizeof(struct object
), /* size */
1752 socket_device_dump
, /* dump */
1753 socket_device_get_type
, /* get_type */
1754 no_add_queue
, /* add_queue */
1755 NULL
, /* remove_queue */
1756 NULL
, /* signaled */
1757 no_satisfied
, /* satisfied */
1758 no_signal
, /* signal */
1759 no_get_fd
, /* get_fd */
1760 default_fd_map_access
, /* map_access */
1761 default_get_sd
, /* get_sd */
1762 default_set_sd
, /* set_sd */
1763 default_get_full_name
, /* get_full_name */
1764 socket_device_lookup_name
, /* lookup_name */
1765 directory_link_name
, /* link_name */
1766 default_unlink_name
, /* unlink_name */
1767 socket_device_open_file
, /* open_file */
1768 no_kernel_obj_list
, /* get_kernel_obj_list */
1769 no_close_handle
, /* close_handle */
1770 no_destroy
/* destroy */
1773 static struct object_type
*socket_device_get_type( struct object
*obj
)
1775 static const WCHAR name
[] = {'D','e','v','i','c','e'};
1776 static const struct unicode_str str
= { name
, sizeof(name
) };
1777 return get_object_type( &str
);
1780 static void socket_device_dump( struct object
*obj
, int verbose
)
1782 fputs( "Socket device\n", stderr
);
1785 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
1786 unsigned int attr
, struct object
*root
)
1791 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
1792 unsigned int sharing
, unsigned int options
)
1796 if (!(sock
= create_socket())) return NULL
;
1797 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
1799 release_object( sock
);
1805 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
1806 unsigned int attr
, const struct security_descriptor
*sd
)
1808 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
1811 /* set socket event parameters */
1812 DECL_HANDLER(set_socket_event
)
1815 struct event
*old_event
;
1817 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1818 FILE_WRITE_ATTRIBUTES
, &sock_ops
))) return;
1819 if (get_unix_fd( sock
->fd
) == -1) return;
1820 old_event
= sock
->event
;
1821 sock
->mask
= req
->mask
;
1822 sock
->hmask
&= ~req
->mask
; /* re-enable held events */
1824 sock
->window
= req
->window
;
1825 sock
->message
= req
->msg
;
1826 sock
->wparam
= req
->handle
; /* wparam is the socket handle */
1827 if (req
->event
) sock
->event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
1829 if (debug_level
&& sock
->event
) fprintf(stderr
, "event ptr: %p\n", sock
->event
);
1831 sock_reselect( sock
);
1833 sock
->state
|= FD_WINE_NONBLOCKING
;
1835 /* if a network event is pending, signal the event object
1836 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1837 before a WSAEventSelect() was done on it.
1838 (when dealing with Asynchronous socket) */
1839 sock_wake_up( sock
);
1841 if (old_event
) release_object( old_event
); /* we're through with it */
1842 release_object( &sock
->obj
);
1845 /* get socket event parameters */
1846 DECL_HANDLER(get_socket_event
)
1850 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1851 FILE_READ_ATTRIBUTES
, &sock_ops
))) return;
1852 if (get_unix_fd( sock
->fd
) == -1) return;
1853 reply
->mask
= sock
->mask
;
1854 reply
->pmask
= sock
->pmask
;
1855 reply
->state
= sock
->state
;
1856 set_reply_data( sock
->errors
, min( get_reply_max_size(), sizeof(sock
->errors
) ));
1862 struct event
*cevent
= get_event_obj( current
->process
, req
->c_event
,
1863 EVENT_MODIFY_STATE
);
1866 reset_event( cevent
);
1867 release_object( cevent
);
1871 sock_reselect( sock
);
1873 release_object( &sock
->obj
);
1876 /* re-enable pending socket events */
1877 DECL_HANDLER(enable_socket_event
)
1881 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1882 FILE_WRITE_ATTRIBUTES
, &sock_ops
)))
1885 if (get_unix_fd( sock
->fd
) == -1) return;
1887 /* for event-based notification, windows erases stale events */
1888 sock
->pmask
&= ~req
->mask
;
1890 sock
->hmask
&= ~req
->mask
;
1891 sock
->state
|= req
->sstate
;
1892 sock
->state
&= ~req
->cstate
;
1893 if (sock
->type
!= WS_SOCK_STREAM
) sock
->state
&= ~STREAM_FLAG_MASK
;
1895 sock_reselect( sock
);
1897 release_object( &sock
->obj
);
1900 DECL_HANDLER(set_socket_deferred
)
1902 struct sock
*sock
, *acceptsock
;
1904 sock
=(struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
, &sock_ops
);
1908 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, req
->deferred
, 0, &sock_ops
);
1911 release_object( sock
);
1914 sock
->deferred
= acceptsock
;
1915 release_object( sock
);
1918 DECL_HANDLER(get_socket_info
)
1922 sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
, &sock_ops
);
1925 if (get_unix_fd( sock
->fd
) == -1) return;
1927 reply
->family
= sock
->family
;
1928 reply
->type
= sock
->type
;
1929 reply
->protocol
= sock
->proto
;
1931 release_object( &sock
->obj
);