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
99 #define FD_MAX_EVENTS 10
100 #define FD_READ_BIT 0
101 #define FD_WRITE_BIT 1
103 #define FD_ACCEPT_BIT 3
104 #define FD_CONNECT_BIT 4
105 #define FD_CLOSE_BIT 5
108 * Define flags to be used with the WSAAsyncSelect() call.
110 #define FD_READ 0x00000001
111 #define FD_WRITE 0x00000002
112 #define FD_OOB 0x00000004
113 #define FD_ACCEPT 0x00000008
114 #define FD_CONNECT 0x00000010
115 #define FD_CLOSE 0x00000020
117 /* internal per-socket flags */
118 #define FD_WINE_LISTENING 0x10000000
119 #define FD_WINE_NONBLOCKING 0x20000000
120 #define FD_WINE_CONNECTED 0x40000000
121 #define FD_WINE_RAW 0x80000000
122 #define FD_WINE_INTERNAL 0xFFFF0000
128 struct sock
*acceptsock
;
130 unsigned int recv_len
, local_len
;
135 struct object obj
; /* object header */
136 struct fd
*fd
; /* socket file descriptor */
137 unsigned int state
; /* status bits */
138 unsigned int mask
; /* event mask */
139 unsigned int hmask
; /* held (blocked) events */
140 unsigned int pmask
; /* pending events */
141 unsigned int flags
; /* socket flags */
142 int polling
; /* is socket being polled? */
143 unsigned short proto
; /* socket protocol */
144 unsigned short type
; /* socket type */
145 unsigned short family
; /* socket family */
146 struct event
*event
; /* event object */
147 user_handle_t window
; /* window to send the message to */
148 unsigned int message
; /* message to send */
149 obj_handle_t wparam
; /* message wparam (socket handle) */
150 unsigned int errors
[FD_MAX_EVENTS
]; /* event errors */
151 timeout_t connect_time
;/* time the socket was connected */
152 struct sock
*deferred
; /* socket that waits for a deferred accept */
153 struct async_queue read_q
; /* queue for asynchronous reads */
154 struct async_queue write_q
; /* queue for asynchronous writes */
155 struct async_queue ifchange_q
; /* queue for interface change notifications */
156 struct async_queue accept_q
; /* queue for asynchronous accepts */
157 struct object
*ifchange_obj
; /* the interface change notification object */
158 struct list ifchange_entry
; /* entry in ifchange notification list */
159 struct list accept_list
; /* list of pending accept requests */
160 struct accept_req
*accept_recv_req
; /* pending accept-into request which will recv on this socket */
163 static void sock_dump( struct object
*obj
, int verbose
);
164 static int sock_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
165 static struct fd
*sock_get_fd( struct object
*obj
);
166 static void sock_destroy( struct object
*obj
);
167 static struct object
*sock_get_ifchange( struct sock
*sock
);
168 static void sock_release_ifchange( struct sock
*sock
);
170 static int sock_get_poll_events( struct fd
*fd
);
171 static void sock_poll_event( struct fd
*fd
, int event
);
172 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
173 static int sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
174 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
);
175 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
177 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
);
178 static int sock_get_ntstatus( int err
);
179 static unsigned int sock_get_error( int err
);
181 static const struct object_ops sock_ops
=
183 sizeof(struct sock
), /* size */
184 sock_dump
, /* dump */
185 no_get_type
, /* get_type */
186 add_queue
, /* add_queue */
187 remove_queue
, /* remove_queue */
188 sock_signaled
, /* signaled */
189 no_satisfied
, /* satisfied */
190 no_signal
, /* signal */
191 sock_get_fd
, /* get_fd */
192 default_fd_map_access
, /* map_access */
193 default_get_sd
, /* get_sd */
194 default_set_sd
, /* set_sd */
195 no_get_full_name
, /* get_full_name */
196 no_lookup_name
, /* lookup_name */
197 no_link_name
, /* link_name */
198 NULL
, /* unlink_name */
199 no_open_file
, /* open_file */
200 no_kernel_obj_list
, /* get_kernel_obj_list */
201 fd_close_handle
, /* close_handle */
202 sock_destroy
/* destroy */
205 static const struct fd_ops sock_fd_ops
=
207 sock_get_poll_events
, /* get_poll_events */
208 sock_poll_event
, /* poll_event */
209 sock_get_fd_type
, /* get_fd_type */
210 no_fd_read
, /* read */
211 no_fd_write
, /* write */
212 no_fd_flush
, /* flush */
213 default_fd_get_file_info
, /* get_file_info */
214 no_fd_get_volume_info
, /* get_volume_info */
215 sock_ioctl
, /* ioctl */
216 sock_queue_async
, /* queue_async */
217 sock_reselect_async
/* reselect_async */
222 struct sockaddr addr
;
223 struct sockaddr_in in
;
224 struct sockaddr_in6 in6
;
226 struct sockaddr_ipx ipx
;
229 struct sockaddr_irda irda
;
233 static int sockaddr_from_unix( const union unix_sockaddr
*uaddr
, struct WS_sockaddr
*wsaddr
, socklen_t wsaddrlen
)
235 memset( wsaddr
, 0, wsaddrlen
);
237 switch (uaddr
->addr
.sa_family
)
241 struct WS_sockaddr_in win
= {0};
243 if (wsaddrlen
< sizeof(win
)) return -1;
244 win
.sin_family
= WS_AF_INET
;
245 win
.sin_port
= uaddr
->in
.sin_port
;
246 memcpy( &win
.sin_addr
, &uaddr
->in
.sin_addr
, sizeof(win
.sin_addr
) );
247 memcpy( wsaddr
, &win
, sizeof(win
) );
253 struct WS_sockaddr_in6 win
= {0};
255 if (wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
)) return -1;
256 win
.sin6_family
= WS_AF_INET6
;
257 win
.sin6_port
= uaddr
->in6
.sin6_port
;
258 win
.sin6_flowinfo
= uaddr
->in6
.sin6_flowinfo
;
259 memcpy( &win
.sin6_addr
, &uaddr
->in6
.sin6_addr
, sizeof(win
.sin6_addr
) );
260 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
261 win
.sin6_scope_id
= uaddr
->in6
.sin6_scope_id
;
263 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
))
265 memcpy( wsaddr
, &win
, sizeof(struct WS_sockaddr_in6
) );
266 return sizeof(struct WS_sockaddr_in6
);
268 memcpy( wsaddr
, &win
, sizeof(struct WS_sockaddr_in6_old
) );
269 return sizeof(struct WS_sockaddr_in6_old
);
275 struct WS_sockaddr_ipx win
= {0};
277 if (wsaddrlen
< sizeof(win
)) return -1;
278 win
.sa_family
= WS_AF_IPX
;
279 memcpy( win
.sa_netnum
, &uaddr
->ipx
.sipx_network
, sizeof(win
.sa_netnum
) );
280 memcpy( win
.sa_nodenum
, &uaddr
->ipx
.sipx_node
, sizeof(win
.sa_nodenum
) );
281 win
.sa_socket
= uaddr
->ipx
.sipx_port
;
282 memcpy( wsaddr
, &win
, sizeof(win
) );
292 if (wsaddrlen
< sizeof(win
)) return -1;
293 win
.irdaAddressFamily
= WS_AF_IRDA
;
294 memcpy( win
.irdaDeviceID
, &uaddr
->irda
.sir_addr
, sizeof(win
.irdaDeviceID
) );
295 if (uaddr
->irda
.sir_lsap_sel
!= LSAP_ANY
)
296 snprintf( win
.irdaServiceName
, sizeof(win
.irdaServiceName
), "LSAP-SEL%u", uaddr
->irda
.sir_lsap_sel
);
298 memcpy( win
.irdaServiceName
, uaddr
->irda
.sir_name
, sizeof(win
.irdaServiceName
) );
299 memcpy( wsaddr
, &win
, sizeof(win
) );
313 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
314 * we post messages if there are multiple events. Used to send
315 * messages. The problem is if there is both a FD_CONNECT event and,
316 * say, an FD_READ event available on the same socket, we want to
317 * notify the app of the connect event first. Otherwise it may
318 * discard the read event because it thinks it hasn't connected yet.
320 static const int event_bitorder
[FD_MAX_EVENTS
] =
328 6, 7, 8, 9 /* leftovers */
331 /* Flags that make sense only for SOCK_STREAM sockets */
332 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
335 SOCK_SHUTDOWN_ERROR
= -1,
336 SOCK_SHUTDOWN_EOF
= 0,
337 SOCK_SHUTDOWN_POLLHUP
= 1
340 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
342 static sock_shutdown_t
sock_check_pollhup(void)
344 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
349 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
350 if ( shutdown( fd
[0], 1 ) ) goto out
;
356 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
357 n
= poll( &pfd
, 1, 1 );
358 if ( n
!= 1 ) goto out
; /* error or timeout */
359 if ( pfd
.revents
& POLLHUP
)
360 ret
= SOCK_SHUTDOWN_POLLHUP
;
361 else if ( pfd
.revents
& POLLIN
&&
362 read( fd
[1], &dummy
, 1 ) == 0 )
363 ret
= SOCK_SHUTDOWN_EOF
;
373 sock_shutdown_type
= sock_check_pollhup();
375 switch ( sock_shutdown_type
)
377 case SOCK_SHUTDOWN_EOF
:
378 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
380 case SOCK_SHUTDOWN_POLLHUP
:
381 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
384 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
385 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
389 static int sock_reselect( struct sock
*sock
)
391 int ev
= sock_get_poll_events( sock
->fd
);
394 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
396 if (!sock
->polling
) /* FIXME: should find a better way to do this */
398 /* previously unconnected socket, is this reselect supposed to connect it? */
399 if (!(sock
->state
& ~FD_WINE_NONBLOCKING
)) return 0;
400 /* ok, it is, attach it to the wineserver's main poll loop */
402 allow_fd_caching( sock
->fd
);
404 /* update condition mask */
405 set_fd_events( sock
->fd
, ev
);
409 /* wake anybody waiting on the socket event or send the associated message */
410 static void sock_wake_up( struct sock
*sock
)
412 unsigned int events
= sock
->pmask
& sock
->mask
;
415 if ( !events
) return;
419 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
420 set_event( sock
->event
);
424 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
425 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
427 int event
= event_bitorder
[i
];
428 if (sock
->pmask
& (1 << event
))
430 lparam_t lparam
= (1 << event
) | (sock
->errors
[event
] << 16);
431 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
435 sock_reselect( sock
);
439 static inline int sock_error( struct fd
*fd
)
441 unsigned int optval
= 0;
442 socklen_t optlen
= sizeof(optval
);
444 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
448 static void free_accept_req( struct accept_req
*req
)
450 list_remove( &req
->entry
);
451 req
->acceptsock
->accept_recv_req
= NULL
;
452 release_object( req
->async
);
456 static void fill_accept_output( struct accept_req
*req
, struct iosb
*iosb
)
458 union unix_sockaddr unix_addr
;
459 struct WS_sockaddr
*win_addr
;
465 if (!(out_data
= mem_alloc( iosb
->out_size
))) return;
467 fd
= get_unix_fd( req
->acceptsock
->fd
);
469 if (req
->recv_len
&& (size
= recv( fd
, out_data
, req
->recv_len
, 0 )) < 0)
471 if (!req
->accepted
&& errno
== EWOULDBLOCK
)
474 sock_reselect( req
->acceptsock
);
475 set_error( STATUS_PENDING
);
479 set_win32_error( sock_get_error( errno
) );
486 if (req
->local_len
< sizeof(int))
488 set_error( STATUS_BUFFER_TOO_SMALL
);
493 unix_len
= sizeof(unix_addr
);
494 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ sizeof(int));
495 if (getsockname( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
496 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, req
->local_len
)) < 0)
498 set_win32_error( sock_get_error( errno
) );
502 memcpy( out_data
+ req
->recv_len
, &win_len
, sizeof(int) );
505 unix_len
= sizeof(unix_addr
);
506 win_addr
= (struct WS_sockaddr
*)(out_data
+ req
->recv_len
+ req
->local_len
+ sizeof(int));
507 if (getpeername( fd
, &unix_addr
.addr
, &unix_len
) < 0 ||
508 (win_len
= sockaddr_from_unix( &unix_addr
, win_addr
, iosb
->out_size
- req
->recv_len
- req
->local_len
)) < 0)
510 set_win32_error( sock_get_error( errno
) );
514 memcpy( out_data
+ req
->recv_len
+ req
->local_len
, &win_len
, sizeof(int) );
516 iosb
->status
= STATUS_SUCCESS
;
518 iosb
->out_data
= out_data
;
519 set_error( STATUS_ALERTED
);
522 static void complete_async_accept( struct sock
*sock
, struct accept_req
*req
)
524 struct sock
*acceptsock
= req
->acceptsock
;
525 struct async
*async
= req
->async
;
528 if (debug_level
) fprintf( stderr
, "completing accept request for socket %p\n", sock
);
530 if (!accept_into_socket( sock
, acceptsock
)) return;
532 iosb
= async_get_iosb( async
);
533 fill_accept_output( req
, iosb
);
534 release_object( iosb
);
537 static void complete_async_accept_recv( struct accept_req
*req
)
539 struct async
*async
= req
->async
;
542 if (debug_level
) fprintf( stderr
, "completing accept recv request for socket %p\n", req
->acceptsock
);
544 assert( req
->recv_len
);
546 iosb
= async_get_iosb( async
);
547 fill_accept_output( req
, iosb
);
548 release_object( iosb
);
551 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
553 if (event
& (POLLIN
| POLLPRI
))
555 struct accept_req
*req
;
557 LIST_FOR_EACH_ENTRY( req
, &sock
->accept_list
, struct accept_req
, entry
)
561 complete_async_accept( sock
, req
);
562 if (get_error() != STATUS_PENDING
)
563 async_terminate( req
->async
, get_error() );
568 if (sock
->accept_recv_req
)
570 complete_async_accept_recv( sock
->accept_recv_req
);
571 if (get_error() != STATUS_PENDING
)
572 async_terminate( sock
->accept_recv_req
->async
, get_error() );
576 if (is_fd_overlapped( sock
->fd
))
578 if (event
& (POLLIN
|POLLPRI
) && async_waiting( &sock
->read_q
))
580 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
581 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
582 event
&= ~(POLLIN
|POLLPRI
);
584 if (event
& POLLOUT
&& async_waiting( &sock
->write_q
))
586 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
587 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
592 if (event
& (POLLERR
| POLLHUP
))
594 int status
= sock_get_ntstatus( error
);
595 struct accept_req
*req
, *next
;
597 if (!(sock
->state
& FD_READ
))
598 async_wake_up( &sock
->read_q
, status
);
599 if (!(sock
->state
& FD_WRITE
))
600 async_wake_up( &sock
->write_q
, status
);
602 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
603 async_terminate( req
->async
, status
);
605 if (sock
->accept_recv_req
)
606 async_terminate( sock
->accept_recv_req
->async
, status
);
612 static void sock_dispatch_events( struct sock
*sock
, int prevstate
, int event
, int error
)
614 if (prevstate
& FD_CONNECT
)
616 sock
->pmask
|= FD_CONNECT
;
617 sock
->hmask
|= FD_CONNECT
;
618 sock
->errors
[FD_CONNECT_BIT
] = sock_get_error( error
);
621 if (prevstate
& FD_WINE_LISTENING
)
623 sock
->pmask
|= FD_ACCEPT
;
624 sock
->hmask
|= FD_ACCEPT
;
625 sock
->errors
[FD_ACCEPT_BIT
] = sock_get_error( error
);
631 sock
->pmask
|= FD_READ
;
632 sock
->hmask
|= FD_READ
;
633 sock
->errors
[FD_READ_BIT
] = 0;
638 sock
->pmask
|= FD_WRITE
;
639 sock
->hmask
|= FD_WRITE
;
640 sock
->errors
[FD_WRITE_BIT
] = 0;
645 sock
->pmask
|= FD_OOB
;
646 sock
->hmask
|= FD_OOB
;
647 sock
->errors
[FD_OOB_BIT
] = 0;
650 if (event
& (POLLERR
|POLLHUP
))
652 sock
->pmask
|= FD_CLOSE
;
653 sock
->hmask
|= FD_CLOSE
;
654 sock
->errors
[FD_CLOSE_BIT
] = sock_get_error( error
);
657 sock_wake_up( sock
);
660 static void sock_poll_event( struct fd
*fd
, int event
)
662 struct sock
*sock
= get_fd_user( fd
);
664 int prevstate
= sock
->state
;
667 assert( sock
->obj
.ops
== &sock_ops
);
669 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
671 /* we may change event later, remove from loop here */
672 if (event
& (POLLERR
|POLLHUP
)) set_fd_events( sock
->fd
, -1 );
674 if (sock
->state
& FD_CONNECT
)
676 if (event
& (POLLERR
|POLLHUP
))
678 /* we didn't get connected? */
679 sock
->state
&= ~FD_CONNECT
;
681 error
= sock_error( fd
);
683 else if (event
& POLLOUT
)
685 /* we got connected */
686 sock
->state
|= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
687 sock
->state
&= ~FD_CONNECT
;
688 sock
->connect_time
= current_time
;
691 else if (sock
->state
& FD_WINE_LISTENING
)
694 if (event
& (POLLERR
|POLLHUP
))
695 error
= sock_error( fd
);
699 /* normal data flow */
700 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
705 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
706 * has been closed, so we need to check for it explicitly here */
707 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
716 /* EAGAIN can happen if an async recv() falls between the server's poll()
717 call and the invocation of this routine */
718 if ( errno
!= EAGAIN
)
723 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
728 if ( (hangup_seen
|| event
& (POLLHUP
|POLLERR
)) && (sock
->state
& (FD_READ
|FD_WRITE
)) )
730 error
= error
? error
: sock_error( fd
);
731 if ( (event
& POLLERR
) || ( sock_shutdown_type
== SOCK_SHUTDOWN_EOF
&& (event
& POLLHUP
) ))
732 sock
->state
&= ~FD_WRITE
;
733 sock
->state
&= ~FD_READ
;
736 fprintf(stderr
, "socket %p aborted by error %d, event: %x\n", sock
, error
, event
);
743 event
= sock_dispatch_asyncs( sock
, event
, error
);
744 sock_dispatch_events( sock
, prevstate
, event
, error
);
746 /* if anyone is stupid enough to wait on the socket object itself,
747 * maybe we should wake them up too, just in case? */
748 wake_up( &sock
->obj
, 0 );
750 sock_reselect( sock
);
753 static void sock_dump( struct object
*obj
, int verbose
)
755 struct sock
*sock
= (struct sock
*)obj
;
756 assert( obj
->ops
== &sock_ops
);
757 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
758 sock
->fd
, sock
->state
,
759 sock
->mask
, sock
->pmask
, sock
->hmask
);
762 static int sock_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
764 struct sock
*sock
= (struct sock
*)obj
;
765 assert( obj
->ops
== &sock_ops
);
767 return check_fd_events( sock
->fd
, sock_get_poll_events( sock
->fd
) ) != 0;
770 static int sock_get_poll_events( struct fd
*fd
)
772 struct sock
*sock
= get_fd_user( fd
);
773 unsigned int mask
= sock
->mask
& ~sock
->hmask
;
774 unsigned int smask
= sock
->state
& mask
;
777 assert( sock
->obj
.ops
== &sock_ops
);
779 if (sock
->state
& FD_CONNECT
)
780 /* connecting, wait for writable */
783 if (!list_empty( &sock
->accept_list
) || sock
->accept_recv_req
)
785 ev
|= POLLIN
| POLLPRI
;
787 else if (async_queued( &sock
->read_q
))
789 if (async_waiting( &sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
791 else if (smask
& FD_READ
|| (sock
->state
& FD_WINE_LISTENING
&& mask
& FD_ACCEPT
))
792 ev
|= POLLIN
| POLLPRI
;
793 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
794 else if (sock
->type
== WS_SOCK_STREAM
&& (sock
->state
& FD_READ
) && (mask
& FD_CLOSE
) &&
795 !(sock
->hmask
& FD_READ
))
798 if (async_queued( &sock
->write_q
))
800 if (async_waiting( &sock
->write_q
)) ev
|= POLLOUT
;
802 else if (smask
& FD_WRITE
)
808 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
810 return FD_TYPE_SOCKET
;
813 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
815 struct sock
*sock
= get_fd_user( fd
);
816 struct async_queue
*queue
;
818 assert( sock
->obj
.ops
== &sock_ops
);
822 case ASYNC_TYPE_READ
:
823 queue
= &sock
->read_q
;
825 case ASYNC_TYPE_WRITE
:
826 queue
= &sock
->write_q
;
829 set_error( STATUS_INVALID_PARAMETER
);
833 if ( ( !( sock
->state
& (FD_READ
|FD_CONNECT
|FD_WINE_LISTENING
) ) && type
== ASYNC_TYPE_READ
) ||
834 ( !( sock
->state
& (FD_WRITE
|FD_CONNECT
) ) && type
== ASYNC_TYPE_WRITE
) )
836 set_error( STATUS_PIPE_DISCONNECTED
);
840 queue_async( queue
, async
);
841 sock_reselect( sock
);
843 set_error( STATUS_PENDING
);
846 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
848 struct sock
*sock
= get_fd_user( fd
);
849 struct accept_req
*req
, *next
;
851 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
853 struct iosb
*iosb
= async_get_iosb( req
->async
);
854 if (iosb
->status
!= STATUS_PENDING
)
855 free_accept_req( req
);
856 release_object( iosb
);
859 /* ignore reselect on ifchange queue */
860 if (&sock
->ifchange_q
!= queue
)
861 sock_reselect( sock
);
864 static struct fd
*sock_get_fd( struct object
*obj
)
866 struct sock
*sock
= (struct sock
*)obj
;
867 return (struct fd
*)grab_object( sock
->fd
);
870 static void sock_destroy( struct object
*obj
)
872 struct sock
*sock
= (struct sock
*)obj
;
873 struct accept_req
*req
, *next
;
875 assert( obj
->ops
== &sock_ops
);
877 /* FIXME: special socket shutdown stuff? */
879 if ( sock
->deferred
)
880 release_object( sock
->deferred
);
882 if (sock
->accept_recv_req
)
883 async_terminate( sock
->accept_recv_req
->async
, STATUS_CANCELLED
);
885 LIST_FOR_EACH_ENTRY_SAFE( req
, next
, &sock
->accept_list
, struct accept_req
, entry
)
886 async_terminate( req
->async
, STATUS_CANCELLED
);
888 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
889 sock_release_ifchange( sock
);
890 free_async_queue( &sock
->read_q
);
891 free_async_queue( &sock
->write_q
);
892 free_async_queue( &sock
->ifchange_q
);
893 free_async_queue( &sock
->accept_q
);
894 if (sock
->event
) release_object( sock
->event
);
897 /* shut the socket down to force pending poll() calls in the client to return */
898 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
899 release_object( sock
->fd
);
903 static struct sock
*create_socket(void)
907 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
922 sock
->connect_time
= 0;
923 sock
->deferred
= NULL
;
924 sock
->ifchange_obj
= NULL
;
925 sock
->accept_recv_req
= NULL
;
926 init_async_queue( &sock
->read_q
);
927 init_async_queue( &sock
->write_q
);
928 init_async_queue( &sock
->ifchange_q
);
929 init_async_queue( &sock
->accept_q
);
930 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
931 list_init( &sock
->accept_list
);
935 static int get_unix_family( int family
)
939 case WS_AF_INET
: return AF_INET
;
940 case WS_AF_INET6
: return AF_INET6
;
942 case WS_AF_IPX
: return AF_IPX
;
945 case WS_AF_IRDA
: return AF_IRDA
;
947 case WS_AF_UNSPEC
: return AF_UNSPEC
;
952 static int get_unix_type( int type
)
956 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
957 case WS_SOCK_RAW
: return SOCK_RAW
;
958 case WS_SOCK_STREAM
: return SOCK_STREAM
;
963 static int get_unix_protocol( int protocol
)
965 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
970 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
971 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
972 case WS_IPPROTO_IP
: return IPPROTO_IP
;
973 case WS_IPPROTO_IPIP
: return IPPROTO_IPIP
;
974 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
975 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
976 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
977 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
982 static void set_dont_fragment( int fd
, int level
, int value
)
986 if (level
== IPPROTO_IP
)
989 optname
= IP_DONTFRAG
;
990 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
991 optname
= IP_MTU_DISCOVER
;
992 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1000 optname
= IPV6_DONTFRAG
;
1001 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1002 optname
= IPV6_MTU_DISCOVER
;
1003 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1009 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
1012 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
, unsigned int flags
)
1014 unsigned int options
= 0;
1015 int sockfd
, unix_type
, unix_family
, unix_protocol
;
1017 unix_family
= get_unix_family( family
);
1018 unix_type
= get_unix_type( type
);
1019 unix_protocol
= get_unix_protocol( protocol
);
1021 if (unix_protocol
< 0)
1023 if (type
&& unix_type
< 0)
1024 set_win32_error( WSAESOCKTNOSUPPORT
);
1026 set_win32_error( WSAEPROTONOSUPPORT
);
1029 if (unix_family
< 0)
1031 if (family
>= 0 && unix_type
< 0)
1032 set_win32_error( WSAESOCKTNOSUPPORT
);
1034 set_win32_error( WSAEAFNOSUPPORT
);
1038 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
1041 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
1042 else set_win32_error( sock_get_error( errno
));
1045 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1047 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
1050 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
1053 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
1056 /* Should we retrieve val using a getsockopt call and then
1057 * set the modified one? */
1058 val
.ipx_pt
= ipx_type
;
1059 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
1064 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
1066 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
1067 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
1068 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
1069 else if (unix_type
== SOCK_STREAM
)
1070 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
1074 if (unix_family
== AF_INET6
)
1076 static const int enable
= 1;
1077 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
1081 sock
->state
= (type
!= SOCK_STREAM
) ? (FD_READ
|FD_WRITE
) : 0;
1082 sock
->flags
= flags
;
1083 sock
->proto
= protocol
;
1085 sock
->family
= family
;
1089 options
= get_fd_options( sock
->fd
);
1090 release_object( sock
->fd
);
1093 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
1097 sock_reselect( sock
);
1102 /* accepts a socket and inits it */
1103 static int accept_new_fd( struct sock
*sock
)
1106 /* Try to accept(2). We can't be safe that this an already connected socket
1107 * or that accept() is allowed on it. In those cases we will get -1/errno
1110 struct sockaddr saddr
;
1111 socklen_t slen
= sizeof(saddr
);
1112 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
1114 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
1116 set_win32_error( sock_get_error( errno
));
1120 /* accept a socket (creates a new fd) */
1121 static struct sock
*accept_socket( struct sock
*sock
)
1123 struct sock
*acceptsock
;
1126 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
1128 if ( sock
->deferred
)
1130 acceptsock
= sock
->deferred
;
1131 sock
->deferred
= NULL
;
1135 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
1136 if (!(acceptsock
= create_socket()))
1142 /* newly created socket gets the same properties of the listening socket */
1143 acceptsock
->state
= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
1144 if (sock
->state
& FD_WINE_NONBLOCKING
)
1145 acceptsock
->state
|= FD_WINE_NONBLOCKING
;
1146 acceptsock
->mask
= sock
->mask
;
1147 acceptsock
->proto
= sock
->proto
;
1148 acceptsock
->type
= sock
->type
;
1149 acceptsock
->family
= sock
->family
;
1150 acceptsock
->window
= sock
->window
;
1151 acceptsock
->message
= sock
->message
;
1152 acceptsock
->connect_time
= current_time
;
1153 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
1154 acceptsock
->flags
= sock
->flags
;
1155 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1156 get_fd_options( sock
->fd
) )))
1158 release_object( acceptsock
);
1163 sock
->pmask
&= ~FD_ACCEPT
;
1164 sock
->hmask
&= ~FD_ACCEPT
;
1165 sock_reselect( sock
);
1169 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
1174 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
1176 if ( sock
->deferred
)
1178 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
1179 get_fd_options( acceptsock
->fd
) );
1183 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
1185 release_object( sock
->deferred
);
1186 sock
->deferred
= NULL
;
1190 if ((acceptfd
= accept_new_fd( sock
)) == -1)
1193 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
1194 get_fd_options( acceptsock
->fd
) )))
1198 acceptsock
->state
|= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
1199 acceptsock
->hmask
= 0;
1200 acceptsock
->pmask
= 0;
1201 acceptsock
->polling
= 0;
1202 acceptsock
->proto
= sock
->proto
;
1203 acceptsock
->type
= sock
->type
;
1204 acceptsock
->family
= sock
->family
;
1205 acceptsock
->wparam
= 0;
1206 acceptsock
->deferred
= NULL
;
1207 acceptsock
->connect_time
= current_time
;
1208 fd_copy_completion( acceptsock
->fd
, newfd
);
1209 release_object( acceptsock
->fd
);
1210 acceptsock
->fd
= newfd
;
1213 sock
->pmask
&= ~FD_ACCEPT
;
1214 sock
->hmask
&= ~FD_ACCEPT
;
1215 sock_reselect( sock
);
1220 /* return an errno value mapped to a WSA error */
1221 static unsigned int sock_get_error( int err
)
1225 case EINTR
: return WSAEINTR
;
1226 case EBADF
: return WSAEBADF
;
1228 case EACCES
: return WSAEACCES
;
1229 case EFAULT
: return WSAEFAULT
;
1230 case EINVAL
: return WSAEINVAL
;
1231 case EMFILE
: return WSAEMFILE
;
1232 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
1233 case EINPROGRESS
: return WSAEINPROGRESS
;
1234 case EALREADY
: return WSAEALREADY
;
1235 case ENOTSOCK
: return WSAENOTSOCK
;
1236 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
1237 case EMSGSIZE
: return WSAEMSGSIZE
;
1238 case EPROTOTYPE
: return WSAEPROTOTYPE
;
1239 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
1240 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
1241 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
1242 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
1243 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
1244 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
1245 case EADDRINUSE
: return WSAEADDRINUSE
;
1246 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
1247 case ENETDOWN
: return WSAENETDOWN
;
1248 case ENETUNREACH
: return WSAENETUNREACH
;
1249 case ENETRESET
: return WSAENETRESET
;
1250 case ECONNABORTED
: return WSAECONNABORTED
;
1252 case ECONNRESET
: return WSAECONNRESET
;
1253 case ENOBUFS
: return WSAENOBUFS
;
1254 case EISCONN
: return WSAEISCONN
;
1255 case ENOTCONN
: return WSAENOTCONN
;
1256 case ESHUTDOWN
: return WSAESHUTDOWN
;
1257 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
1258 case ETIMEDOUT
: return WSAETIMEDOUT
;
1259 case ECONNREFUSED
: return WSAECONNREFUSED
;
1260 case ELOOP
: return WSAELOOP
;
1261 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
1262 case EHOSTDOWN
: return WSAEHOSTDOWN
;
1263 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
1264 case ENOTEMPTY
: return WSAENOTEMPTY
;
1266 case EPROCLIM
: return WSAEPROCLIM
;
1269 case EUSERS
: return WSAEUSERS
;
1272 case EDQUOT
: return WSAEDQUOT
;
1275 case ESTALE
: return WSAESTALE
;
1278 case EREMOTE
: return WSAEREMOTE
;
1284 perror("wineserver: sock_get_error() can't map error");
1289 static int sock_get_ntstatus( int err
)
1293 case EBADF
: return STATUS_INVALID_HANDLE
;
1294 case EBUSY
: return STATUS_DEVICE_BUSY
;
1296 case EACCES
: return STATUS_ACCESS_DENIED
;
1297 case EFAULT
: return STATUS_NO_MEMORY
;
1298 case EINVAL
: return STATUS_INVALID_PARAMETER
;
1300 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
1301 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
1302 case EINPROGRESS
: return STATUS_PENDING
;
1303 case EALREADY
: return STATUS_NETWORK_BUSY
;
1304 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
1305 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
1306 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
1307 case EPROTONOSUPPORT
:
1308 case ESOCKTNOSUPPORT
:
1311 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
1312 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
1313 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
1314 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
1315 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
1316 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
1317 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
1318 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
1319 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
1320 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
1321 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
1322 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
1324 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
1325 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
1327 case 0: return STATUS_SUCCESS
;
1330 perror("wineserver: sock_get_ntstatus() can't map error");
1331 return STATUS_UNSUCCESSFUL
;
1335 static struct accept_req
*alloc_accept_req( struct sock
*acceptsock
, struct async
*async
,
1336 const struct afd_accept_into_params
*params
)
1338 struct accept_req
*req
= mem_alloc( sizeof(*req
) );
1342 req
->async
= (struct async
*)grab_object( async
);
1343 req
->acceptsock
= acceptsock
;
1347 req
->recv_len
= params
->recv_len
;
1348 req
->local_len
= params
->local_len
;
1353 static int sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
1355 struct sock
*sock
= get_fd_user( fd
);
1357 assert( sock
->obj
.ops
== &sock_ops
);
1359 if (get_unix_fd( fd
) == -1 && code
!= IOCTL_AFD_CREATE
) return 0;
1363 case IOCTL_AFD_CREATE
:
1365 const struct afd_create_params
*params
= get_req_data();
1367 if (get_req_data_size() != sizeof(*params
))
1369 set_error( STATUS_INVALID_PARAMETER
);
1372 init_socket( sock
, params
->family
, params
->type
, params
->protocol
, params
->flags
);
1376 case IOCTL_AFD_ACCEPT
:
1378 struct sock
*acceptsock
;
1379 obj_handle_t handle
;
1381 if (get_reply_max_size() != sizeof(handle
))
1383 set_error( STATUS_BUFFER_TOO_SMALL
);
1387 if (!(acceptsock
= accept_socket( sock
))) return 0;
1388 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
1389 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
1390 acceptsock
->wparam
= handle
;
1391 release_object( acceptsock
);
1392 set_reply_data( &handle
, sizeof(handle
) );
1396 case IOCTL_AFD_ACCEPT_INTO
:
1398 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
1399 const struct afd_accept_into_params
*params
= get_req_data();
1400 struct sock
*acceptsock
;
1401 unsigned int remote_len
;
1402 struct accept_req
*req
;
1404 if (get_req_data_size() != sizeof(*params
) ||
1405 get_reply_max_size() < params
->recv_len
||
1406 get_reply_max_size() - params
->recv_len
< params
->local_len
)
1408 set_error( STATUS_BUFFER_TOO_SMALL
);
1412 remote_len
= get_reply_max_size() - params
->recv_len
- params
->local_len
;
1413 if (remote_len
< sizeof(int))
1415 set_error( STATUS_INVALID_PARAMETER
);
1419 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, params
->accept_handle
, access
, &sock_ops
)))
1422 if (!(req
= alloc_accept_req( acceptsock
, async
, params
))) return 0;
1423 list_add_tail( &sock
->accept_list
, &req
->entry
);
1424 acceptsock
->accept_recv_req
= req
;
1425 release_object( acceptsock
);
1427 acceptsock
->wparam
= params
->accept_handle
;
1428 queue_async( &sock
->accept_q
, async
);
1429 sock_reselect( sock
);
1430 set_error( STATUS_PENDING
);
1434 case IOCTL_AFD_ADDRESS_LIST_CHANGE
:
1435 if ((sock
->state
& FD_WINE_NONBLOCKING
) && async_is_blocking( async
))
1437 set_win32_error( WSAEWOULDBLOCK
);
1440 if (!sock_get_ifchange( sock
)) return 0;
1441 queue_async( &sock
->ifchange_q
, async
);
1442 set_error( STATUS_PENDING
);
1446 set_error( STATUS_NOT_SUPPORTED
);
1451 #ifdef HAVE_LINUX_RTNETLINK_H
1453 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
1454 static struct object
*ifchange_object
;
1456 static void ifchange_dump( struct object
*obj
, int verbose
);
1457 static struct fd
*ifchange_get_fd( struct object
*obj
);
1458 static void ifchange_destroy( struct object
*obj
);
1460 static int ifchange_get_poll_events( struct fd
*fd
);
1461 static void ifchange_poll_event( struct fd
*fd
, int event
);
1465 struct object obj
; /* object header */
1466 struct fd
*fd
; /* interface change file descriptor */
1467 struct list sockets
; /* list of sockets to send interface change notifications */
1470 static const struct object_ops ifchange_ops
=
1472 sizeof(struct ifchange
), /* size */
1473 ifchange_dump
, /* dump */
1474 no_get_type
, /* get_type */
1475 add_queue
, /* add_queue */
1476 NULL
, /* remove_queue */
1477 NULL
, /* signaled */
1478 no_satisfied
, /* satisfied */
1479 no_signal
, /* signal */
1480 ifchange_get_fd
, /* get_fd */
1481 default_fd_map_access
, /* map_access */
1482 default_get_sd
, /* get_sd */
1483 default_set_sd
, /* set_sd */
1484 no_get_full_name
, /* get_full_name */
1485 no_lookup_name
, /* lookup_name */
1486 no_link_name
, /* link_name */
1487 NULL
, /* unlink_name */
1488 no_open_file
, /* open_file */
1489 no_kernel_obj_list
, /* get_kernel_obj_list */
1490 no_close_handle
, /* close_handle */
1491 ifchange_destroy
/* destroy */
1494 static const struct fd_ops ifchange_fd_ops
=
1496 ifchange_get_poll_events
, /* get_poll_events */
1497 ifchange_poll_event
, /* poll_event */
1498 NULL
, /* get_fd_type */
1499 no_fd_read
, /* read */
1500 no_fd_write
, /* write */
1501 no_fd_flush
, /* flush */
1502 no_fd_get_file_info
, /* get_file_info */
1503 no_fd_get_volume_info
, /* get_volume_info */
1504 no_fd_ioctl
, /* ioctl */
1505 NULL
, /* queue_async */
1506 NULL
/* reselect_async */
1509 static void ifchange_dump( struct object
*obj
, int verbose
)
1511 assert( obj
->ops
== &ifchange_ops
);
1512 fprintf( stderr
, "Interface change\n" );
1515 static struct fd
*ifchange_get_fd( struct object
*obj
)
1517 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1518 return (struct fd
*)grab_object( ifchange
->fd
);
1521 static void ifchange_destroy( struct object
*obj
)
1523 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1524 assert( obj
->ops
== &ifchange_ops
);
1526 release_object( ifchange
->fd
);
1528 /* reset the global ifchange object so that it will be recreated if it is needed again */
1529 assert( obj
== ifchange_object
);
1530 ifchange_object
= NULL
;
1533 static int ifchange_get_poll_events( struct fd
*fd
)
1538 /* wake up all the sockets waiting for a change notification event */
1539 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
1541 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1542 struct list
*ptr
, *next
;
1543 assert( obj
->ops
== &ifchange_ops
);
1544 assert( obj
== ifchange_object
);
1546 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
1548 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
1550 assert( sock
->ifchange_obj
);
1551 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
1552 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
1556 static void ifchange_poll_event( struct fd
*fd
, int event
)
1558 struct object
*ifchange
= get_fd_user( fd
);
1559 unsigned int status
= STATUS_PENDING
;
1560 char buffer
[PIPE_BUF
];
1563 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
1566 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
1567 return; /* retry when poll() says the socket is ready */
1568 status
= sock_get_ntstatus( errno
);
1572 struct nlmsghdr
*nlh
;
1574 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
1576 if (nlh
->nlmsg_type
== NLMSG_DONE
)
1578 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
1579 status
= STATUS_SUCCESS
;
1582 else status
= STATUS_CANCELLED
;
1584 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
1589 /* we only need one of these interface notification objects, all of the sockets dependent upon
1590 * it will wake up when a notification event occurs */
1591 static struct object
*get_ifchange( void )
1593 #ifdef HAVE_LINUX_RTNETLINK_H
1594 struct ifchange
*ifchange
;
1595 struct sockaddr_nl addr
;
1598 if (ifchange_object
)
1600 /* increment the refcount for each socket that uses the ifchange object */
1601 return grab_object( ifchange_object
);
1604 /* create the socket we need for processing interface change notifications */
1605 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1608 set_win32_error( sock_get_error( errno
));
1611 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1612 memset( &addr
, 0, sizeof(addr
) );
1613 addr
.nl_family
= AF_NETLINK
;
1614 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
1615 /* bind the socket to the special netlink kernel interface */
1616 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
1619 set_win32_error( sock_get_error( errno
));
1622 if (!(ifchange
= alloc_object( &ifchange_ops
)))
1625 set_error( STATUS_NO_MEMORY
);
1628 list_init( &ifchange
->sockets
);
1629 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
1631 release_object( ifchange
);
1632 set_error( STATUS_NO_MEMORY
);
1635 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
1637 /* the ifchange object is now successfully configured */
1638 ifchange_object
= &ifchange
->obj
;
1639 return &ifchange
->obj
;
1641 set_error( STATUS_NOT_SUPPORTED
);
1646 /* add the socket to the interface change notification list */
1647 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
1649 #ifdef HAVE_LINUX_RTNETLINK_H
1650 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1652 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
1656 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1657 static struct object
*sock_get_ifchange( struct sock
*sock
)
1659 struct object
*ifchange
;
1661 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
1662 return sock
->ifchange_obj
;
1664 if (!(ifchange
= get_ifchange()))
1667 /* add the socket to the ifchange notification list */
1668 ifchange_add_sock( ifchange
, sock
);
1669 sock
->ifchange_obj
= ifchange
;
1673 /* destroy an existing ifchange queue for a specific socket */
1674 static void sock_release_ifchange( struct sock
*sock
)
1676 if (sock
->ifchange_obj
)
1678 list_remove( &sock
->ifchange_entry
);
1679 release_object( sock
->ifchange_obj
);
1680 sock
->ifchange_obj
= NULL
;
1684 static struct object_type
*socket_device_get_type( struct object
*obj
);
1685 static void socket_device_dump( struct object
*obj
, int verbose
);
1686 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
);
1687 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
1688 unsigned int sharing
, unsigned int options
);
1690 static const struct object_ops socket_device_ops
=
1692 sizeof(struct object
), /* size */
1693 socket_device_dump
, /* dump */
1694 socket_device_get_type
, /* get_type */
1695 no_add_queue
, /* add_queue */
1696 NULL
, /* remove_queue */
1697 NULL
, /* signaled */
1698 no_satisfied
, /* satisfied */
1699 no_signal
, /* signal */
1700 no_get_fd
, /* get_fd */
1701 default_fd_map_access
, /* map_access */
1702 default_get_sd
, /* get_sd */
1703 default_set_sd
, /* set_sd */
1704 default_get_full_name
, /* get_full_name */
1705 socket_device_lookup_name
, /* lookup_name */
1706 directory_link_name
, /* link_name */
1707 default_unlink_name
, /* unlink_name */
1708 socket_device_open_file
, /* open_file */
1709 no_kernel_obj_list
, /* get_kernel_obj_list */
1710 no_close_handle
, /* close_handle */
1711 no_destroy
/* destroy */
1714 static struct object_type
*socket_device_get_type( struct object
*obj
)
1716 static const WCHAR name
[] = {'D','e','v','i','c','e'};
1717 static const struct unicode_str str
= { name
, sizeof(name
) };
1718 return get_object_type( &str
);
1721 static void socket_device_dump( struct object
*obj
, int verbose
)
1723 fputs( "Socket device\n", stderr
);
1726 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
)
1731 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
1732 unsigned int sharing
, unsigned int options
)
1736 if (!(sock
= create_socket())) return NULL
;
1737 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
1739 release_object( sock
);
1745 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
1746 unsigned int attr
, const struct security_descriptor
*sd
)
1748 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
1751 /* set socket event parameters */
1752 DECL_HANDLER(set_socket_event
)
1755 struct event
*old_event
;
1757 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1758 FILE_WRITE_ATTRIBUTES
, &sock_ops
))) return;
1759 if (get_unix_fd( sock
->fd
) == -1) return;
1760 old_event
= sock
->event
;
1761 sock
->mask
= req
->mask
;
1762 sock
->hmask
&= ~req
->mask
; /* re-enable held events */
1764 sock
->window
= req
->window
;
1765 sock
->message
= req
->msg
;
1766 sock
->wparam
= req
->handle
; /* wparam is the socket handle */
1767 if (req
->event
) sock
->event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
1769 if (debug_level
&& sock
->event
) fprintf(stderr
, "event ptr: %p\n", sock
->event
);
1771 sock_reselect( sock
);
1773 sock
->state
|= FD_WINE_NONBLOCKING
;
1775 /* if a network event is pending, signal the event object
1776 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1777 before a WSAEventSelect() was done on it.
1778 (when dealing with Asynchronous socket) */
1779 sock_wake_up( sock
);
1781 if (old_event
) release_object( old_event
); /* we're through with it */
1782 release_object( &sock
->obj
);
1785 /* get socket event parameters */
1786 DECL_HANDLER(get_socket_event
)
1790 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1791 FILE_READ_ATTRIBUTES
, &sock_ops
))) return;
1792 if (get_unix_fd( sock
->fd
) == -1) return;
1793 reply
->mask
= sock
->mask
;
1794 reply
->pmask
= sock
->pmask
;
1795 reply
->state
= sock
->state
;
1796 set_reply_data( sock
->errors
, min( get_reply_max_size(), sizeof(sock
->errors
) ));
1802 struct event
*cevent
= get_event_obj( current
->process
, req
->c_event
,
1803 EVENT_MODIFY_STATE
);
1806 reset_event( cevent
);
1807 release_object( cevent
);
1811 sock_reselect( sock
);
1813 release_object( &sock
->obj
);
1816 /* re-enable pending socket events */
1817 DECL_HANDLER(enable_socket_event
)
1821 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1822 FILE_WRITE_ATTRIBUTES
, &sock_ops
)))
1825 if (get_unix_fd( sock
->fd
) == -1) return;
1827 /* for event-based notification, windows erases stale events */
1828 sock
->pmask
&= ~req
->mask
;
1830 sock
->hmask
&= ~req
->mask
;
1831 sock
->state
|= req
->sstate
;
1832 sock
->state
&= ~req
->cstate
;
1833 if (sock
->type
!= WS_SOCK_STREAM
) sock
->state
&= ~STREAM_FLAG_MASK
;
1835 sock_reselect( sock
);
1837 release_object( &sock
->obj
);
1840 DECL_HANDLER(set_socket_deferred
)
1842 struct sock
*sock
, *acceptsock
;
1844 sock
=(struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
, &sock_ops
);
1848 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, req
->deferred
, 0, &sock_ops
);
1851 release_object( sock
);
1854 sock
->deferred
= acceptsock
;
1855 release_object( sock
);
1858 DECL_HANDLER(get_socket_info
)
1862 sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
, &sock_ops
);
1865 if (get_unix_fd( sock
->fd
) == -1) return;
1867 reply
->family
= sock
->family
;
1868 reply
->type
= sock
->type
;
1869 reply
->protocol
= sock
->proto
;
1871 release_object( &sock
->obj
);