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?
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
44 #ifdef HAVE_SYS_FILIO_H
45 # include <sys/filio.h>
50 #ifdef HAVE_LINUX_RTNETLINK_H
51 # include <linux/rtnetlink.h>
55 #define WIN32_NO_STATUS
71 #define FD_MAX_EVENTS 10
73 #define FD_WRITE_BIT 1
75 #define FD_ACCEPT_BIT 3
76 #define FD_CONNECT_BIT 4
77 #define FD_CLOSE_BIT 5
80 * Define flags to be used with the WSAAsyncSelect() call.
82 #define FD_READ 0x00000001
83 #define FD_WRITE 0x00000002
84 #define FD_OOB 0x00000004
85 #define FD_ACCEPT 0x00000008
86 #define FD_CONNECT 0x00000010
87 #define FD_CLOSE 0x00000020
89 /* internal per-socket flags */
90 #define FD_WINE_LISTENING 0x10000000
91 #define FD_WINE_NONBLOCKING 0x20000000
92 #define FD_WINE_CONNECTED 0x40000000
93 #define FD_WINE_RAW 0x80000000
94 #define FD_WINE_INTERNAL 0xFFFF0000
98 struct object obj
; /* object header */
99 struct fd
*fd
; /* socket file descriptor */
100 unsigned int state
; /* status bits */
101 unsigned int mask
; /* event mask */
102 unsigned int hmask
; /* held (blocked) events */
103 unsigned int pmask
; /* pending events */
104 unsigned int flags
; /* socket flags */
105 int polling
; /* is socket being polled? */
106 unsigned short proto
; /* socket protocol */
107 unsigned short type
; /* socket type */
108 unsigned short family
; /* socket family */
109 struct event
*event
; /* event object */
110 user_handle_t window
; /* window to send the message to */
111 unsigned int message
; /* message to send */
112 obj_handle_t wparam
; /* message wparam (socket handle) */
113 unsigned int errors
[FD_MAX_EVENTS
]; /* event errors */
114 timeout_t connect_time
;/* time the socket was connected */
115 struct sock
*deferred
; /* socket that waits for a deferred accept */
116 struct async_queue read_q
; /* queue for asynchronous reads */
117 struct async_queue write_q
; /* queue for asynchronous writes */
118 struct async_queue ifchange_q
; /* queue for interface change notifications */
119 struct object
*ifchange_obj
; /* the interface change notification object */
120 struct list ifchange_entry
; /* entry in ifchange notification list */
123 static void sock_dump( struct object
*obj
, int verbose
);
124 static int sock_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
125 static struct fd
*sock_get_fd( struct object
*obj
);
126 static void sock_destroy( struct object
*obj
);
127 static struct object
*sock_get_ifchange( struct sock
*sock
);
128 static void sock_release_ifchange( struct sock
*sock
);
130 static int sock_get_poll_events( struct fd
*fd
);
131 static void sock_poll_event( struct fd
*fd
, int event
);
132 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
133 static int sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
134 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
);
135 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
137 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
, unsigned int flags
);
138 static int sock_get_ntstatus( int err
);
139 static unsigned int sock_get_error( int err
);
141 static const struct object_ops sock_ops
=
143 sizeof(struct sock
), /* size */
144 sock_dump
, /* dump */
145 no_get_type
, /* get_type */
146 add_queue
, /* add_queue */
147 remove_queue
, /* remove_queue */
148 sock_signaled
, /* signaled */
149 no_satisfied
, /* satisfied */
150 no_signal
, /* signal */
151 sock_get_fd
, /* get_fd */
152 default_fd_map_access
, /* map_access */
153 default_get_sd
, /* get_sd */
154 default_set_sd
, /* set_sd */
155 no_get_full_name
, /* get_full_name */
156 no_lookup_name
, /* lookup_name */
157 no_link_name
, /* link_name */
158 NULL
, /* unlink_name */
159 no_open_file
, /* open_file */
160 no_kernel_obj_list
, /* get_kernel_obj_list */
161 fd_close_handle
, /* close_handle */
162 sock_destroy
/* destroy */
165 static const struct fd_ops sock_fd_ops
=
167 sock_get_poll_events
, /* get_poll_events */
168 sock_poll_event
, /* poll_event */
169 sock_get_fd_type
, /* get_fd_type */
170 no_fd_read
, /* read */
171 no_fd_write
, /* write */
172 no_fd_flush
, /* flush */
173 default_fd_get_file_info
, /* get_file_info */
174 no_fd_get_volume_info
, /* get_volume_info */
175 sock_ioctl
, /* ioctl */
176 sock_queue_async
, /* queue_async */
177 sock_reselect_async
/* reselect_async */
181 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
182 * we post messages if there are multiple events. Used to send
183 * messages. The problem is if there is both a FD_CONNECT event and,
184 * say, an FD_READ event available on the same socket, we want to
185 * notify the app of the connect event first. Otherwise it may
186 * discard the read event because it thinks it hasn't connected yet.
188 static const int event_bitorder
[FD_MAX_EVENTS
] =
196 6, 7, 8, 9 /* leftovers */
199 /* Flags that make sense only for SOCK_STREAM sockets */
200 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
203 SOCK_SHUTDOWN_ERROR
= -1,
204 SOCK_SHUTDOWN_EOF
= 0,
205 SOCK_SHUTDOWN_POLLHUP
= 1
208 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
210 static sock_shutdown_t
sock_check_pollhup(void)
212 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
217 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
218 if ( shutdown( fd
[0], 1 ) ) goto out
;
224 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
225 n
= poll( &pfd
, 1, 1 );
226 if ( n
!= 1 ) goto out
; /* error or timeout */
227 if ( pfd
.revents
& POLLHUP
)
228 ret
= SOCK_SHUTDOWN_POLLHUP
;
229 else if ( pfd
.revents
& POLLIN
&&
230 read( fd
[1], &dummy
, 1 ) == 0 )
231 ret
= SOCK_SHUTDOWN_EOF
;
241 sock_shutdown_type
= sock_check_pollhup();
243 switch ( sock_shutdown_type
)
245 case SOCK_SHUTDOWN_EOF
:
246 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
248 case SOCK_SHUTDOWN_POLLHUP
:
249 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
252 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
253 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
257 static int sock_reselect( struct sock
*sock
)
259 int ev
= sock_get_poll_events( sock
->fd
);
262 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
264 if (!sock
->polling
) /* FIXME: should find a better way to do this */
266 /* previously unconnected socket, is this reselect supposed to connect it? */
267 if (!(sock
->state
& ~FD_WINE_NONBLOCKING
)) return 0;
268 /* ok, it is, attach it to the wineserver's main poll loop */
270 allow_fd_caching( sock
->fd
);
272 /* update condition mask */
273 set_fd_events( sock
->fd
, ev
);
277 /* wake anybody waiting on the socket event or send the associated message */
278 static void sock_wake_up( struct sock
*sock
)
280 unsigned int events
= sock
->pmask
& sock
->mask
;
283 if ( !events
) return;
287 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
288 set_event( sock
->event
);
292 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
293 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
295 int event
= event_bitorder
[i
];
296 if (sock
->pmask
& (1 << event
))
298 lparam_t lparam
= (1 << event
) | (sock
->errors
[event
] << 16);
299 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
303 sock_reselect( sock
);
307 static inline int sock_error( struct fd
*fd
)
309 unsigned int optval
= 0;
310 socklen_t optlen
= sizeof(optval
);
312 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
316 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
318 if (is_fd_overlapped( sock
->fd
))
320 if (event
& (POLLIN
|POLLPRI
) && async_waiting( &sock
->read_q
))
322 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
323 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
324 event
&= ~(POLLIN
|POLLPRI
);
326 if (event
& POLLOUT
&& async_waiting( &sock
->write_q
))
328 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
329 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
332 if ( event
& (POLLERR
|POLLHUP
) )
334 int status
= sock_get_ntstatus( error
);
336 if ( !(sock
->state
& FD_READ
) )
337 async_wake_up( &sock
->read_q
, status
);
338 if ( !(sock
->state
& FD_WRITE
) )
339 async_wake_up( &sock
->write_q
, status
);
345 static void sock_dispatch_events( struct sock
*sock
, int prevstate
, int event
, int error
)
347 if (prevstate
& FD_CONNECT
)
349 sock
->pmask
|= FD_CONNECT
;
350 sock
->hmask
|= FD_CONNECT
;
351 sock
->errors
[FD_CONNECT_BIT
] = sock_get_error( error
);
354 if (prevstate
& FD_WINE_LISTENING
)
356 sock
->pmask
|= FD_ACCEPT
;
357 sock
->hmask
|= FD_ACCEPT
;
358 sock
->errors
[FD_ACCEPT_BIT
] = sock_get_error( error
);
364 sock
->pmask
|= FD_READ
;
365 sock
->hmask
|= FD_READ
;
366 sock
->errors
[FD_READ_BIT
] = 0;
371 sock
->pmask
|= FD_WRITE
;
372 sock
->hmask
|= FD_WRITE
;
373 sock
->errors
[FD_WRITE_BIT
] = 0;
378 sock
->pmask
|= FD_OOB
;
379 sock
->hmask
|= FD_OOB
;
380 sock
->errors
[FD_OOB_BIT
] = 0;
383 if (event
& (POLLERR
|POLLHUP
))
385 sock
->pmask
|= FD_CLOSE
;
386 sock
->hmask
|= FD_CLOSE
;
387 sock
->errors
[FD_CLOSE_BIT
] = sock_get_error( error
);
390 sock_wake_up( sock
);
393 static void sock_poll_event( struct fd
*fd
, int event
)
395 struct sock
*sock
= get_fd_user( fd
);
397 int prevstate
= sock
->state
;
400 assert( sock
->obj
.ops
== &sock_ops
);
402 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
404 /* we may change event later, remove from loop here */
405 if (event
& (POLLERR
|POLLHUP
)) set_fd_events( sock
->fd
, -1 );
407 if (sock
->state
& FD_CONNECT
)
409 if (event
& (POLLERR
|POLLHUP
))
411 /* we didn't get connected? */
412 sock
->state
&= ~FD_CONNECT
;
414 error
= sock_error( fd
);
416 else if (event
& POLLOUT
)
418 /* we got connected */
419 sock
->state
|= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
420 sock
->state
&= ~FD_CONNECT
;
421 sock
->connect_time
= current_time
;
424 else if (sock
->state
& FD_WINE_LISTENING
)
427 if (event
& (POLLERR
|POLLHUP
))
428 error
= sock_error( fd
);
432 /* normal data flow */
433 if ( sock
->type
== SOCK_STREAM
&& ( event
& POLLIN
) )
438 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
439 * has been closed, so we need to check for it explicitly here */
440 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
449 /* EAGAIN can happen if an async recv() falls between the server's poll()
450 call and the invocation of this routine */
451 if ( errno
!= EAGAIN
)
456 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
461 if ( (hangup_seen
|| event
& (POLLHUP
|POLLERR
)) && (sock
->state
& (FD_READ
|FD_WRITE
)) )
463 error
= error
? error
: sock_error( fd
);
464 if ( (event
& POLLERR
) || ( sock_shutdown_type
== SOCK_SHUTDOWN_EOF
&& (event
& POLLHUP
) ))
465 sock
->state
&= ~FD_WRITE
;
466 sock
->state
&= ~FD_READ
;
469 fprintf(stderr
, "socket %p aborted by error %d, event: %x\n", sock
, error
, event
);
476 event
= sock_dispatch_asyncs( sock
, event
, error
);
477 sock_dispatch_events( sock
, prevstate
, event
, error
);
479 /* if anyone is stupid enough to wait on the socket object itself,
480 * maybe we should wake them up too, just in case? */
481 wake_up( &sock
->obj
, 0 );
483 sock_reselect( sock
);
486 static void sock_dump( struct object
*obj
, int verbose
)
488 struct sock
*sock
= (struct sock
*)obj
;
489 assert( obj
->ops
== &sock_ops
);
490 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
491 sock
->fd
, sock
->state
,
492 sock
->mask
, sock
->pmask
, sock
->hmask
);
495 static int sock_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
497 struct sock
*sock
= (struct sock
*)obj
;
498 assert( obj
->ops
== &sock_ops
);
500 return check_fd_events( sock
->fd
, sock_get_poll_events( sock
->fd
) ) != 0;
503 static int sock_get_poll_events( struct fd
*fd
)
505 struct sock
*sock
= get_fd_user( fd
);
506 unsigned int mask
= sock
->mask
& ~sock
->hmask
;
507 unsigned int smask
= sock
->state
& mask
;
510 assert( sock
->obj
.ops
== &sock_ops
);
512 if (sock
->state
& FD_CONNECT
)
513 /* connecting, wait for writable */
516 if (async_queued( &sock
->read_q
))
518 if (async_waiting( &sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
520 else if (smask
& FD_READ
|| (sock
->state
& FD_WINE_LISTENING
&& mask
& FD_ACCEPT
))
521 ev
|= POLLIN
| POLLPRI
;
522 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
523 else if ( sock
->type
== SOCK_STREAM
&& sock
->state
& FD_READ
&& mask
& FD_CLOSE
&&
524 !(sock
->hmask
& FD_READ
) )
527 if (async_queued( &sock
->write_q
))
529 if (async_waiting( &sock
->write_q
)) ev
|= POLLOUT
;
531 else if (smask
& FD_WRITE
)
537 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
539 return FD_TYPE_SOCKET
;
542 static int sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
544 struct sock
*sock
= get_fd_user( fd
);
546 assert( sock
->obj
.ops
== &sock_ops
);
548 if (get_unix_fd( fd
) == -1 && code
!= IOCTL_AFD_CREATE
) return 0;
552 case IOCTL_AFD_CREATE
:
554 const struct afd_create_params
*params
= get_req_data();
556 if (get_req_data_size() != sizeof(*params
))
558 set_error( STATUS_INVALID_PARAMETER
);
561 init_socket( sock
, params
->family
, params
->type
, params
->protocol
, params
->flags
);
565 case WS_SIO_ADDRESS_LIST_CHANGE
:
566 if ((sock
->state
& FD_WINE_NONBLOCKING
) && async_is_blocking( async
))
568 set_win32_error( WSAEWOULDBLOCK
);
571 if (!sock_get_ifchange( sock
)) return 0;
572 queue_async( &sock
->ifchange_q
, async
);
573 set_error( STATUS_PENDING
);
576 set_error( STATUS_NOT_SUPPORTED
);
581 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
583 struct sock
*sock
= get_fd_user( fd
);
584 struct async_queue
*queue
;
586 assert( sock
->obj
.ops
== &sock_ops
);
590 case ASYNC_TYPE_READ
:
591 queue
= &sock
->read_q
;
593 case ASYNC_TYPE_WRITE
:
594 queue
= &sock
->write_q
;
597 set_error( STATUS_INVALID_PARAMETER
);
601 if ( ( !( sock
->state
& (FD_READ
|FD_CONNECT
|FD_WINE_LISTENING
) ) && type
== ASYNC_TYPE_READ
) ||
602 ( !( sock
->state
& (FD_WRITE
|FD_CONNECT
) ) && type
== ASYNC_TYPE_WRITE
) )
604 set_error( STATUS_PIPE_DISCONNECTED
);
608 queue_async( queue
, async
);
609 sock_reselect( sock
);
611 set_error( STATUS_PENDING
);
614 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
616 struct sock
*sock
= get_fd_user( fd
);
617 /* ignore reselect on ifchange queue */
618 if (&sock
->ifchange_q
!= queue
)
619 sock_reselect( sock
);
622 static struct fd
*sock_get_fd( struct object
*obj
)
624 struct sock
*sock
= (struct sock
*)obj
;
625 return (struct fd
*)grab_object( sock
->fd
);
628 static void sock_destroy( struct object
*obj
)
630 struct sock
*sock
= (struct sock
*)obj
;
631 assert( obj
->ops
== &sock_ops
);
633 /* FIXME: special socket shutdown stuff? */
635 if ( sock
->deferred
)
636 release_object( sock
->deferred
);
638 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
639 sock_release_ifchange( sock
);
640 free_async_queue( &sock
->read_q
);
641 free_async_queue( &sock
->write_q
);
642 free_async_queue( &sock
->ifchange_q
);
643 if (sock
->event
) release_object( sock
->event
);
646 /* shut the socket down to force pending poll() calls in the client to return */
647 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
648 release_object( sock
->fd
);
652 static struct sock
*create_socket(void)
656 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
671 sock
->connect_time
= 0;
672 sock
->deferred
= NULL
;
673 sock
->ifchange_obj
= NULL
;
674 init_async_queue( &sock
->read_q
);
675 init_async_queue( &sock
->write_q
);
676 init_async_queue( &sock
->ifchange_q
);
677 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
681 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
, unsigned int flags
)
683 unsigned int options
= 0;
686 sockfd
= socket( family
, type
, protocol
);
689 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
690 else set_win32_error( sock_get_error( errno
));
693 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
694 sock
->state
= (type
!= SOCK_STREAM
) ? (FD_READ
|FD_WRITE
) : 0;
696 sock
->proto
= protocol
;
698 sock
->family
= family
;
702 options
= get_fd_options( sock
->fd
);
703 release_object( sock
->fd
);
706 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
710 sock_reselect( sock
);
715 /* accepts a socket and inits it */
716 static int accept_new_fd( struct sock
*sock
)
719 /* Try to accept(2). We can't be safe that this an already connected socket
720 * or that accept() is allowed on it. In those cases we will get -1/errno
723 struct sockaddr saddr
;
724 socklen_t slen
= sizeof(saddr
);
725 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
727 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
729 set_win32_error( sock_get_error( errno
));
733 /* accept a socket (creates a new fd) */
734 static struct sock
*accept_socket( obj_handle_t handle
)
736 struct sock
*acceptsock
;
740 sock
= (struct sock
*)get_handle_obj( current
->process
, handle
, FILE_READ_DATA
, &sock_ops
);
744 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
746 if ( sock
->deferred
)
748 acceptsock
= sock
->deferred
;
749 sock
->deferred
= NULL
;
753 if ((acceptfd
= accept_new_fd( sock
)) == -1)
755 release_object( sock
);
758 if (!(acceptsock
= create_socket()))
761 release_object( sock
);
765 /* newly created socket gets the same properties of the listening socket */
766 acceptsock
->state
= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
767 if (sock
->state
& FD_WINE_NONBLOCKING
)
768 acceptsock
->state
|= FD_WINE_NONBLOCKING
;
769 acceptsock
->mask
= sock
->mask
;
770 acceptsock
->proto
= sock
->proto
;
771 acceptsock
->type
= sock
->type
;
772 acceptsock
->family
= sock
->family
;
773 acceptsock
->window
= sock
->window
;
774 acceptsock
->message
= sock
->message
;
775 acceptsock
->connect_time
= current_time
;
776 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
777 acceptsock
->flags
= sock
->flags
;
778 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
779 get_fd_options( sock
->fd
) )))
781 release_object( acceptsock
);
782 release_object( sock
);
787 sock
->pmask
&= ~FD_ACCEPT
;
788 sock
->hmask
&= ~FD_ACCEPT
;
789 sock_reselect( sock
);
790 release_object( sock
);
794 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
799 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
801 if ( sock
->deferred
)
803 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
804 get_fd_options( acceptsock
->fd
) );
808 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
810 release_object( sock
->deferred
);
811 sock
->deferred
= NULL
;
815 if ((acceptfd
= accept_new_fd( sock
)) == -1)
818 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
819 get_fd_options( acceptsock
->fd
) )))
823 acceptsock
->state
|= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
824 acceptsock
->hmask
= 0;
825 acceptsock
->pmask
= 0;
826 acceptsock
->polling
= 0;
827 acceptsock
->proto
= sock
->proto
;
828 acceptsock
->type
= sock
->type
;
829 acceptsock
->family
= sock
->family
;
830 acceptsock
->wparam
= 0;
831 acceptsock
->deferred
= NULL
;
832 acceptsock
->connect_time
= current_time
;
833 fd_copy_completion( acceptsock
->fd
, newfd
);
834 release_object( acceptsock
->fd
);
835 acceptsock
->fd
= newfd
;
838 sock
->pmask
&= ~FD_ACCEPT
;
839 sock
->hmask
&= ~FD_ACCEPT
;
840 sock_reselect( sock
);
845 /* return an errno value mapped to a WSA error */
846 static unsigned int sock_get_error( int err
)
850 case EINTR
: return WSAEINTR
;
851 case EBADF
: return WSAEBADF
;
853 case EACCES
: return WSAEACCES
;
854 case EFAULT
: return WSAEFAULT
;
855 case EINVAL
: return WSAEINVAL
;
856 case EMFILE
: return WSAEMFILE
;
857 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
858 case EINPROGRESS
: return WSAEINPROGRESS
;
859 case EALREADY
: return WSAEALREADY
;
860 case ENOTSOCK
: return WSAENOTSOCK
;
861 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
862 case EMSGSIZE
: return WSAEMSGSIZE
;
863 case EPROTOTYPE
: return WSAEPROTOTYPE
;
864 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
865 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
866 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
867 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
868 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
869 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
870 case EADDRINUSE
: return WSAEADDRINUSE
;
871 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
872 case ENETDOWN
: return WSAENETDOWN
;
873 case ENETUNREACH
: return WSAENETUNREACH
;
874 case ENETRESET
: return WSAENETRESET
;
875 case ECONNABORTED
: return WSAECONNABORTED
;
877 case ECONNRESET
: return WSAECONNRESET
;
878 case ENOBUFS
: return WSAENOBUFS
;
879 case EISCONN
: return WSAEISCONN
;
880 case ENOTCONN
: return WSAENOTCONN
;
881 case ESHUTDOWN
: return WSAESHUTDOWN
;
882 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
883 case ETIMEDOUT
: return WSAETIMEDOUT
;
884 case ECONNREFUSED
: return WSAECONNREFUSED
;
885 case ELOOP
: return WSAELOOP
;
886 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
887 case EHOSTDOWN
: return WSAEHOSTDOWN
;
888 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
889 case ENOTEMPTY
: return WSAENOTEMPTY
;
891 case EPROCLIM
: return WSAEPROCLIM
;
894 case EUSERS
: return WSAEUSERS
;
897 case EDQUOT
: return WSAEDQUOT
;
900 case ESTALE
: return WSAESTALE
;
903 case EREMOTE
: return WSAEREMOTE
;
909 perror("wineserver: sock_get_error() can't map error");
914 static int sock_get_ntstatus( int err
)
918 case EBADF
: return STATUS_INVALID_HANDLE
;
919 case EBUSY
: return STATUS_DEVICE_BUSY
;
921 case EACCES
: return STATUS_ACCESS_DENIED
;
922 case EFAULT
: return STATUS_NO_MEMORY
;
923 case EINVAL
: return STATUS_INVALID_PARAMETER
;
925 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
926 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
927 case EINPROGRESS
: return STATUS_PENDING
;
928 case EALREADY
: return STATUS_NETWORK_BUSY
;
929 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
930 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
931 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
932 case EPROTONOSUPPORT
:
933 case ESOCKTNOSUPPORT
:
936 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
937 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
938 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
939 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
940 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
941 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
942 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
943 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
944 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
945 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
946 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
947 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
949 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
950 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
952 case 0: return STATUS_SUCCESS
;
955 perror("wineserver: sock_get_ntstatus() can't map error");
956 return STATUS_UNSUCCESSFUL
;
960 #ifdef HAVE_LINUX_RTNETLINK_H
962 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
963 static struct object
*ifchange_object
;
965 static void ifchange_dump( struct object
*obj
, int verbose
);
966 static struct fd
*ifchange_get_fd( struct object
*obj
);
967 static void ifchange_destroy( struct object
*obj
);
969 static int ifchange_get_poll_events( struct fd
*fd
);
970 static void ifchange_poll_event( struct fd
*fd
, int event
);
974 struct object obj
; /* object header */
975 struct fd
*fd
; /* interface change file descriptor */
976 struct list sockets
; /* list of sockets to send interface change notifications */
979 static const struct object_ops ifchange_ops
=
981 sizeof(struct ifchange
), /* size */
982 ifchange_dump
, /* dump */
983 no_get_type
, /* get_type */
984 add_queue
, /* add_queue */
985 NULL
, /* remove_queue */
987 no_satisfied
, /* satisfied */
988 no_signal
, /* signal */
989 ifchange_get_fd
, /* get_fd */
990 default_fd_map_access
, /* map_access */
991 default_get_sd
, /* get_sd */
992 default_set_sd
, /* set_sd */
993 no_get_full_name
, /* get_full_name */
994 no_lookup_name
, /* lookup_name */
995 no_link_name
, /* link_name */
996 NULL
, /* unlink_name */
997 no_open_file
, /* open_file */
998 no_kernel_obj_list
, /* get_kernel_obj_list */
999 no_close_handle
, /* close_handle */
1000 ifchange_destroy
/* destroy */
1003 static const struct fd_ops ifchange_fd_ops
=
1005 ifchange_get_poll_events
, /* get_poll_events */
1006 ifchange_poll_event
, /* poll_event */
1007 NULL
, /* get_fd_type */
1008 no_fd_read
, /* read */
1009 no_fd_write
, /* write */
1010 no_fd_flush
, /* flush */
1011 no_fd_get_file_info
, /* get_file_info */
1012 no_fd_get_volume_info
, /* get_volume_info */
1013 no_fd_ioctl
, /* ioctl */
1014 NULL
, /* queue_async */
1015 NULL
/* reselect_async */
1018 static void ifchange_dump( struct object
*obj
, int verbose
)
1020 assert( obj
->ops
== &ifchange_ops
);
1021 fprintf( stderr
, "Interface change\n" );
1024 static struct fd
*ifchange_get_fd( struct object
*obj
)
1026 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1027 return (struct fd
*)grab_object( ifchange
->fd
);
1030 static void ifchange_destroy( struct object
*obj
)
1032 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1033 assert( obj
->ops
== &ifchange_ops
);
1035 release_object( ifchange
->fd
);
1037 /* reset the global ifchange object so that it will be recreated if it is needed again */
1038 assert( obj
== ifchange_object
);
1039 ifchange_object
= NULL
;
1042 static int ifchange_get_poll_events( struct fd
*fd
)
1047 /* wake up all the sockets waiting for a change notification event */
1048 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
1050 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1051 struct list
*ptr
, *next
;
1052 assert( obj
->ops
== &ifchange_ops
);
1053 assert( obj
== ifchange_object
);
1055 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
1057 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
1059 assert( sock
->ifchange_obj
);
1060 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
1061 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
1065 static void ifchange_poll_event( struct fd
*fd
, int event
)
1067 struct object
*ifchange
= get_fd_user( fd
);
1068 unsigned int status
= STATUS_PENDING
;
1069 char buffer
[PIPE_BUF
];
1072 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
1075 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
1076 return; /* retry when poll() says the socket is ready */
1077 status
= sock_get_ntstatus( errno
);
1081 struct nlmsghdr
*nlh
;
1083 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
1085 if (nlh
->nlmsg_type
== NLMSG_DONE
)
1087 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
1088 status
= STATUS_SUCCESS
;
1091 else status
= STATUS_CANCELLED
;
1093 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
1098 /* we only need one of these interface notification objects, all of the sockets dependent upon
1099 * it will wake up when a notification event occurs */
1100 static struct object
*get_ifchange( void )
1102 #ifdef HAVE_LINUX_RTNETLINK_H
1103 struct ifchange
*ifchange
;
1104 struct sockaddr_nl addr
;
1107 if (ifchange_object
)
1109 /* increment the refcount for each socket that uses the ifchange object */
1110 return grab_object( ifchange_object
);
1113 /* create the socket we need for processing interface change notifications */
1114 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1117 set_win32_error( sock_get_error( errno
));
1120 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1121 memset( &addr
, 0, sizeof(addr
) );
1122 addr
.nl_family
= AF_NETLINK
;
1123 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
1124 /* bind the socket to the special netlink kernel interface */
1125 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
1128 set_win32_error( sock_get_error( errno
));
1131 if (!(ifchange
= alloc_object( &ifchange_ops
)))
1134 set_error( STATUS_NO_MEMORY
);
1137 list_init( &ifchange
->sockets
);
1138 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
1140 release_object( ifchange
);
1141 set_error( STATUS_NO_MEMORY
);
1144 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
1146 /* the ifchange object is now successfully configured */
1147 ifchange_object
= &ifchange
->obj
;
1148 return &ifchange
->obj
;
1150 set_error( STATUS_NOT_SUPPORTED
);
1155 /* add the socket to the interface change notification list */
1156 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
1158 #ifdef HAVE_LINUX_RTNETLINK_H
1159 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1161 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
1165 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1166 static struct object
*sock_get_ifchange( struct sock
*sock
)
1168 struct object
*ifchange
;
1170 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
1171 return sock
->ifchange_obj
;
1173 if (!(ifchange
= get_ifchange()))
1176 /* add the socket to the ifchange notification list */
1177 ifchange_add_sock( ifchange
, sock
);
1178 sock
->ifchange_obj
= ifchange
;
1182 /* destroy an existing ifchange queue for a specific socket */
1183 static void sock_release_ifchange( struct sock
*sock
)
1185 if (sock
->ifchange_obj
)
1187 list_remove( &sock
->ifchange_entry
);
1188 release_object( sock
->ifchange_obj
);
1189 sock
->ifchange_obj
= NULL
;
1193 static struct object_type
*socket_device_get_type( struct object
*obj
);
1194 static void socket_device_dump( struct object
*obj
, int verbose
);
1195 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
);
1196 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
1197 unsigned int sharing
, unsigned int options
);
1199 static const struct object_ops socket_device_ops
=
1201 sizeof(struct object
), /* size */
1202 socket_device_dump
, /* dump */
1203 socket_device_get_type
, /* get_type */
1204 no_add_queue
, /* add_queue */
1205 NULL
, /* remove_queue */
1206 NULL
, /* signaled */
1207 no_satisfied
, /* satisfied */
1208 no_signal
, /* signal */
1209 no_get_fd
, /* get_fd */
1210 default_fd_map_access
, /* map_access */
1211 default_get_sd
, /* get_sd */
1212 default_set_sd
, /* set_sd */
1213 default_get_full_name
, /* get_full_name */
1214 socket_device_lookup_name
, /* lookup_name */
1215 directory_link_name
, /* link_name */
1216 default_unlink_name
, /* unlink_name */
1217 socket_device_open_file
, /* open_file */
1218 no_kernel_obj_list
, /* get_kernel_obj_list */
1219 no_close_handle
, /* close_handle */
1220 no_destroy
/* destroy */
1223 static struct object_type
*socket_device_get_type( struct object
*obj
)
1225 static const WCHAR name
[] = {'D','e','v','i','c','e'};
1226 static const struct unicode_str str
= { name
, sizeof(name
) };
1227 return get_object_type( &str
);
1230 static void socket_device_dump( struct object
*obj
, int verbose
)
1232 fputs( "Socket device\n", stderr
);
1235 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
)
1240 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
1241 unsigned int sharing
, unsigned int options
)
1245 if (!(sock
= create_socket())) return NULL
;
1246 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
1248 release_object( sock
);
1254 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
1255 unsigned int attr
, const struct security_descriptor
*sd
)
1257 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
1260 /* accept a socket */
1261 DECL_HANDLER(accept_socket
)
1266 if ((sock
= accept_socket( req
->lhandle
)) != NULL
)
1268 reply
->handle
= alloc_handle( current
->process
, &sock
->obj
, req
->access
, req
->attributes
);
1269 sock
->wparam
= reply
->handle
; /* wparam for message is the socket handle */
1270 sock_reselect( sock
);
1271 release_object( &sock
->obj
);
1275 /* accept a socket into an initialized socket */
1276 DECL_HANDLER(accept_into_socket
)
1278 struct sock
*sock
, *acceptsock
;
1279 const int all_attributes
= FILE_READ_ATTRIBUTES
|FILE_WRITE_ATTRIBUTES
|FILE_READ_DATA
;
1281 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->lhandle
,
1282 all_attributes
, &sock_ops
)))
1285 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, req
->ahandle
,
1286 all_attributes
, &sock_ops
)))
1288 release_object( sock
);
1292 if (accept_into_socket( sock
, acceptsock
))
1294 acceptsock
->wparam
= req
->ahandle
; /* wparam for message is the socket handle */
1295 sock_reselect( acceptsock
);
1297 release_object( acceptsock
);
1298 release_object( sock
);
1301 /* set socket event parameters */
1302 DECL_HANDLER(set_socket_event
)
1305 struct event
*old_event
;
1307 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1308 FILE_WRITE_ATTRIBUTES
, &sock_ops
))) return;
1309 if (get_unix_fd( sock
->fd
) == -1) return;
1310 old_event
= sock
->event
;
1311 sock
->mask
= req
->mask
;
1312 sock
->hmask
&= ~req
->mask
; /* re-enable held events */
1314 sock
->window
= req
->window
;
1315 sock
->message
= req
->msg
;
1316 sock
->wparam
= req
->handle
; /* wparam is the socket handle */
1317 if (req
->event
) sock
->event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
1319 if (debug_level
&& sock
->event
) fprintf(stderr
, "event ptr: %p\n", sock
->event
);
1321 sock_reselect( sock
);
1323 sock
->state
|= FD_WINE_NONBLOCKING
;
1325 /* if a network event is pending, signal the event object
1326 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1327 before a WSAEventSelect() was done on it.
1328 (when dealing with Asynchronous socket) */
1329 sock_wake_up( sock
);
1331 if (old_event
) release_object( old_event
); /* we're through with it */
1332 release_object( &sock
->obj
);
1335 /* get socket event parameters */
1336 DECL_HANDLER(get_socket_event
)
1340 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1341 FILE_READ_ATTRIBUTES
, &sock_ops
))) return;
1342 if (get_unix_fd( sock
->fd
) == -1) return;
1343 reply
->mask
= sock
->mask
;
1344 reply
->pmask
= sock
->pmask
;
1345 reply
->state
= sock
->state
;
1346 set_reply_data( sock
->errors
, min( get_reply_max_size(), sizeof(sock
->errors
) ));
1352 struct event
*cevent
= get_event_obj( current
->process
, req
->c_event
,
1353 EVENT_MODIFY_STATE
);
1356 reset_event( cevent
);
1357 release_object( cevent
);
1361 sock_reselect( sock
);
1363 release_object( &sock
->obj
);
1366 /* re-enable pending socket events */
1367 DECL_HANDLER(enable_socket_event
)
1371 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1372 FILE_WRITE_ATTRIBUTES
, &sock_ops
)))
1375 if (get_unix_fd( sock
->fd
) == -1) return;
1377 /* for event-based notification, windows erases stale events */
1378 sock
->pmask
&= ~req
->mask
;
1380 sock
->hmask
&= ~req
->mask
;
1381 sock
->state
|= req
->sstate
;
1382 sock
->state
&= ~req
->cstate
;
1383 if ( sock
->type
!= SOCK_STREAM
) sock
->state
&= ~STREAM_FLAG_MASK
;
1385 sock_reselect( sock
);
1387 release_object( &sock
->obj
);
1390 DECL_HANDLER(set_socket_deferred
)
1392 struct sock
*sock
, *acceptsock
;
1394 sock
=(struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
, &sock_ops
);
1398 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, req
->deferred
, 0, &sock_ops
);
1401 release_object( sock
);
1404 sock
->deferred
= acceptsock
;
1405 release_object( sock
);
1408 DECL_HANDLER(get_socket_info
)
1412 sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
, &sock_ops
);
1415 if (get_unix_fd( sock
->fd
) == -1) return;
1417 reply
->family
= sock
->family
;
1418 reply
->type
= sock
->type
;
1419 reply
->protocol
= sock
->proto
;
1421 release_object( &sock
->obj
);