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
70 #define FD_MAX_EVENTS 10
72 #define FD_WRITE_BIT 1
74 #define FD_ACCEPT_BIT 3
75 #define FD_CONNECT_BIT 4
76 #define FD_CLOSE_BIT 5
79 * Define flags to be used with the WSAAsyncSelect() call.
81 #define FD_READ 0x00000001
82 #define FD_WRITE 0x00000002
83 #define FD_OOB 0x00000004
84 #define FD_ACCEPT 0x00000008
85 #define FD_CONNECT 0x00000010
86 #define FD_CLOSE 0x00000020
88 /* internal per-socket flags */
89 #define FD_WINE_LISTENING 0x10000000
90 #define FD_WINE_NONBLOCKING 0x20000000
91 #define FD_WINE_CONNECTED 0x40000000
92 #define FD_WINE_RAW 0x80000000
93 #define FD_WINE_INTERNAL 0xFFFF0000
97 struct object obj
; /* object header */
98 struct fd
*fd
; /* socket file descriptor */
99 unsigned int state
; /* status bits */
100 unsigned int mask
; /* event mask */
101 unsigned int hmask
; /* held (blocked) events */
102 unsigned int pmask
; /* pending events */
103 unsigned int flags
; /* socket flags */
104 int polling
; /* is socket being polled? */
105 unsigned short proto
; /* socket protocol */
106 unsigned short type
; /* socket type */
107 unsigned short family
; /* socket family */
108 struct event
*event
; /* event object */
109 user_handle_t window
; /* window to send the message to */
110 unsigned int message
; /* message to send */
111 obj_handle_t wparam
; /* message wparam (socket handle) */
112 int errors
[FD_MAX_EVENTS
]; /* event errors */
113 timeout_t connect_time
;/* time the socket was connected */
114 struct sock
*deferred
; /* socket that waits for a deferred accept */
115 struct async_queue
*read_q
; /* queue for asynchronous reads */
116 struct async_queue
*write_q
; /* queue for asynchronous writes */
117 struct async_queue
*ifchange_q
; /* queue for interface change notifications */
118 struct object
*ifchange_obj
; /* the interface change notification object */
119 struct list ifchange_entry
; /* entry in ifchange notification list */
122 static void sock_dump( struct object
*obj
, int verbose
);
123 static int sock_add_ifchange( struct sock
*sock
, const async_data_t
*async_data
);
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 async_queue
*sock_get_ifchange_q( struct sock
*sock
);
128 static void sock_destroy_ifchange_q( 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 obj_handle_t
sock_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async
,
134 int blocking
, const void *data
, data_size_t size
);
135 static void sock_queue_async( struct fd
*fd
, const async_data_t
*data
, int type
, int count
);
136 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
137 static void sock_cancel_async( struct fd
*fd
, struct process
*process
, struct thread
*thread
, client_ptr_t iosb
);
139 static int sock_get_ntstatus( int err
);
140 static int sock_get_error( int err
);
141 static void sock_set_error(void);
143 static const struct object_ops sock_ops
=
145 sizeof(struct sock
), /* size */
146 sock_dump
, /* dump */
147 no_get_type
, /* get_type */
148 add_queue
, /* add_queue */
149 remove_queue
, /* remove_queue */
150 sock_signaled
, /* signaled */
151 no_satisfied
, /* satisfied */
152 no_signal
, /* signal */
153 sock_get_fd
, /* get_fd */
154 default_fd_map_access
, /* map_access */
155 default_get_sd
, /* get_sd */
156 default_set_sd
, /* set_sd */
157 no_lookup_name
, /* lookup_name */
158 no_open_file
, /* open_file */
159 fd_close_handle
, /* close_handle */
160 sock_destroy
/* destroy */
163 static const struct fd_ops sock_fd_ops
=
165 sock_get_poll_events
, /* get_poll_events */
166 sock_poll_event
, /* poll_event */
167 no_flush
, /* flush */
168 sock_get_fd_type
, /* get_fd_type */
169 sock_ioctl
, /* ioctl */
170 sock_queue_async
, /* queue_async */
171 sock_reselect_async
, /* reselect_async */
172 sock_cancel_async
/* cancel_async */
176 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
177 * we post messages if there are multiple events. Used to send
178 * messages. The problem is if there is both a FD_CONNECT event and,
179 * say, an FD_READ event available on the same socket, we want to
180 * notify the app of the connect event first. Otherwise it may
181 * discard the read event because it thinks it hasn't connected yet.
183 static const int event_bitorder
[FD_MAX_EVENTS
] =
191 6, 7, 8, 9 /* leftovers */
194 /* Flags that make sense only for SOCK_STREAM sockets */
195 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
198 SOCK_SHUTDOWN_ERROR
= -1,
199 SOCK_SHUTDOWN_EOF
= 0,
200 SOCK_SHUTDOWN_POLLHUP
= 1
203 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
205 static sock_shutdown_t
sock_check_pollhup(void)
207 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
212 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
213 if ( shutdown( fd
[0], 1 ) ) goto out
;
219 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
220 n
= poll( &pfd
, 1, 1 );
221 if ( n
!= 1 ) goto out
; /* error or timeout */
222 if ( pfd
.revents
& POLLHUP
)
223 ret
= SOCK_SHUTDOWN_POLLHUP
;
224 else if ( pfd
.revents
& POLLIN
&&
225 read( fd
[1], &dummy
, 1 ) == 0 )
226 ret
= SOCK_SHUTDOWN_EOF
;
236 sock_shutdown_type
= sock_check_pollhup();
238 switch ( sock_shutdown_type
)
240 case SOCK_SHUTDOWN_EOF
:
241 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
243 case SOCK_SHUTDOWN_POLLHUP
:
244 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
247 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
248 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
252 static int sock_reselect( struct sock
*sock
)
254 int ev
= sock_get_poll_events( sock
->fd
);
257 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
259 if (!sock
->polling
) /* FIXME: should find a better way to do this */
261 /* previously unconnected socket, is this reselect supposed to connect it? */
262 if (!(sock
->state
& ~FD_WINE_NONBLOCKING
)) return 0;
263 /* ok, it is, attach it to the wineserver's main poll loop */
265 allow_fd_caching( sock
->fd
);
267 /* update condition mask */
268 set_fd_events( sock
->fd
, ev
);
272 /* wake anybody waiting on the socket event or send the associated message */
273 static void sock_wake_up( struct sock
*sock
)
275 unsigned int events
= sock
->pmask
& sock
->mask
;
278 if ( !events
) return;
282 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
283 set_event( sock
->event
);
287 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
288 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
290 int event
= event_bitorder
[i
];
291 if (sock
->pmask
& (1 << event
))
293 lparam_t lparam
= (1 << event
) | (sock_get_error(sock
->errors
[event
]) << 16);
294 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
298 sock_reselect( sock
);
302 static inline int sock_error( struct fd
*fd
)
304 unsigned int optval
= 0;
305 socklen_t optlen
= sizeof(optval
);
307 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
311 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
313 if ( sock
->flags
& WSA_FLAG_OVERLAPPED
)
315 if ( event
& (POLLIN
|POLLPRI
) && async_waiting( sock
->read_q
) )
317 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
318 async_wake_up( sock
->read_q
, STATUS_ALERTED
);
319 event
&= ~(POLLIN
|POLLPRI
);
321 if ( event
& POLLOUT
&& async_waiting( sock
->write_q
) )
323 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
324 async_wake_up( sock
->write_q
, STATUS_ALERTED
);
327 if ( event
& (POLLERR
|POLLHUP
) )
329 int status
= sock_get_ntstatus( error
);
331 if ( !(sock
->state
& FD_READ
) )
332 async_wake_up( sock
->read_q
, status
);
333 if ( !(sock
->state
& FD_WRITE
) )
334 async_wake_up( sock
->write_q
, status
);
340 static void sock_dispatch_events( struct sock
*sock
, int prevstate
, int event
, int error
)
342 if (prevstate
& FD_CONNECT
)
344 sock
->pmask
|= FD_CONNECT
;
345 sock
->hmask
|= FD_CONNECT
;
346 sock
->errors
[FD_CONNECT_BIT
] = error
;
349 if (prevstate
& FD_WINE_LISTENING
)
351 sock
->pmask
|= FD_ACCEPT
;
352 sock
->hmask
|= FD_ACCEPT
;
353 sock
->errors
[FD_ACCEPT_BIT
] = error
;
359 sock
->pmask
|= FD_READ
;
360 sock
->hmask
|= FD_READ
;
361 sock
->errors
[FD_READ_BIT
] = 0;
366 sock
->pmask
|= FD_WRITE
;
367 sock
->hmask
|= FD_WRITE
;
368 sock
->errors
[FD_WRITE_BIT
] = 0;
373 sock
->pmask
|= FD_OOB
;
374 sock
->hmask
|= FD_OOB
;
375 sock
->errors
[FD_OOB_BIT
] = 0;
378 if (event
& (POLLERR
|POLLHUP
))
380 sock
->pmask
|= FD_CLOSE
;
381 sock
->hmask
|= FD_CLOSE
;
382 sock
->errors
[FD_CLOSE_BIT
] = error
;
385 sock_wake_up( sock
);
388 static void sock_poll_event( struct fd
*fd
, int event
)
390 struct sock
*sock
= get_fd_user( fd
);
392 int prevstate
= sock
->state
;
395 assert( sock
->obj
.ops
== &sock_ops
);
397 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
399 /* we may change event later, remove from loop here */
400 if (event
& (POLLERR
|POLLHUP
)) set_fd_events( sock
->fd
, -1 );
402 if (sock
->state
& FD_CONNECT
)
404 if (event
& (POLLERR
|POLLHUP
))
406 /* we didn't get connected? */
407 sock
->state
&= ~FD_CONNECT
;
409 error
= sock_error( fd
);
411 else if (event
& POLLOUT
)
413 /* we got connected */
414 sock
->state
|= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
415 sock
->state
&= ~FD_CONNECT
;
416 sock
->connect_time
= current_time
;
419 else if (sock
->state
& FD_WINE_LISTENING
)
422 if (event
& (POLLERR
|POLLHUP
))
423 error
= sock_error( fd
);
427 /* normal data flow */
428 if ( sock
->type
== SOCK_STREAM
&& ( event
& POLLIN
) )
433 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
434 * has been closed, so we need to check for it explicitly here */
435 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
444 /* EAGAIN can happen if an async recv() falls between the server's poll()
445 call and the invocation of this routine */
446 if ( errno
!= EAGAIN
)
451 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
456 if ( (hangup_seen
|| event
& (POLLHUP
|POLLERR
)) && (sock
->state
& (FD_READ
|FD_WRITE
)) )
458 error
= error
? error
: sock_error( fd
);
459 if ( (event
& POLLERR
) || ( sock_shutdown_type
== SOCK_SHUTDOWN_EOF
&& (event
& POLLHUP
) ))
460 sock
->state
&= ~FD_WRITE
;
461 sock
->state
&= ~FD_READ
;
464 fprintf(stderr
, "socket %p aborted by error %d, event: %x\n", sock
, error
, event
);
471 event
= sock_dispatch_asyncs( sock
, event
, error
);
472 sock_dispatch_events( sock
, prevstate
, event
, error
);
474 /* if anyone is stupid enough to wait on the socket object itself,
475 * maybe we should wake them up too, just in case? */
476 wake_up( &sock
->obj
, 0 );
478 sock_reselect( sock
);
481 static void sock_dump( struct object
*obj
, int verbose
)
483 struct sock
*sock
= (struct sock
*)obj
;
484 assert( obj
->ops
== &sock_ops
);
485 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
486 sock
->fd
, sock
->state
,
487 sock
->mask
, sock
->pmask
, sock
->hmask
);
490 static int sock_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
492 struct sock
*sock
= (struct sock
*)obj
;
493 assert( obj
->ops
== &sock_ops
);
495 return check_fd_events( sock
->fd
, sock_get_poll_events( sock
->fd
) ) != 0;
498 static int sock_get_poll_events( struct fd
*fd
)
500 struct sock
*sock
= get_fd_user( fd
);
501 unsigned int mask
= sock
->mask
& ~sock
->hmask
;
502 unsigned int smask
= sock
->state
& mask
;
505 assert( sock
->obj
.ops
== &sock_ops
);
507 if (sock
->state
& FD_CONNECT
)
508 /* connecting, wait for writable */
511 if ( async_queued( sock
->read_q
) )
513 if ( async_waiting( sock
->read_q
) ) ev
|= POLLIN
| POLLPRI
;
515 else if (smask
& FD_READ
|| (sock
->state
& FD_WINE_LISTENING
&& mask
& FD_ACCEPT
))
516 ev
|= POLLIN
| POLLPRI
;
517 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
518 else if ( sock
->type
== SOCK_STREAM
&& sock
->state
& FD_READ
&& mask
& FD_CLOSE
&&
519 !(sock
->hmask
& FD_READ
) )
522 if ( async_queued( sock
->write_q
) )
524 if ( async_waiting( sock
->write_q
) ) ev
|= POLLOUT
;
526 else if (smask
& FD_WRITE
)
532 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
534 return FD_TYPE_SOCKET
;
537 obj_handle_t
sock_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async_data
,
538 int blocking
, const void *data
, data_size_t size
)
540 struct sock
*sock
= get_fd_user( fd
);
541 obj_handle_t wait_handle
= 0;
542 async_data_t new_data
;
544 assert( sock
->obj
.ops
== &sock_ops
);
548 case WS_SIO_ADDRESS_LIST_CHANGE
:
551 if (!(wait_handle
= alloc_wait_event( current
->process
))) return 0;
552 new_data
= *async_data
;
553 new_data
.event
= wait_handle
;
554 async_data
= &new_data
;
556 if (!sock_add_ifchange( sock
, async_data
) && wait_handle
)
558 close_handle( current
->process
, wait_handle
);
563 set_error( STATUS_NOT_SUPPORTED
);
568 static void sock_queue_async( struct fd
*fd
, const async_data_t
*data
, int type
, int count
)
570 struct sock
*sock
= get_fd_user( fd
);
572 struct async_queue
*queue
;
574 assert( sock
->obj
.ops
== &sock_ops
);
578 case ASYNC_TYPE_READ
:
579 if (!sock
->read_q
&& !(sock
->read_q
= create_async_queue( sock
->fd
))) return;
580 queue
= sock
->read_q
;
582 case ASYNC_TYPE_WRITE
:
583 if (!sock
->write_q
&& !(sock
->write_q
= create_async_queue( sock
->fd
))) return;
584 queue
= sock
->write_q
;
587 set_error( STATUS_INVALID_PARAMETER
);
591 if ( ( !( sock
->state
& (FD_READ
|FD_CONNECT
|FD_WINE_LISTENING
) ) && type
== ASYNC_TYPE_READ
) ||
592 ( !( sock
->state
& (FD_WRITE
|FD_CONNECT
) ) && type
== ASYNC_TYPE_WRITE
) )
594 set_error( STATUS_PIPE_DISCONNECTED
);
598 if (!(async
= create_async( current
, queue
, data
))) return;
599 release_object( async
);
601 sock_reselect( sock
);
603 set_error( STATUS_PENDING
);
606 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
608 struct sock
*sock
= get_fd_user( fd
);
609 sock_reselect( sock
);
612 static void sock_cancel_async( struct fd
*fd
, struct process
*process
, struct thread
*thread
, client_ptr_t iosb
)
614 struct sock
*sock
= get_fd_user( fd
);
616 assert( sock
->obj
.ops
== &sock_ops
);
618 n
+= async_wake_up_by( sock
->read_q
, process
, thread
, iosb
, STATUS_CANCELLED
);
619 n
+= async_wake_up_by( sock
->write_q
, process
, thread
, iosb
, STATUS_CANCELLED
);
621 set_error( STATUS_NOT_FOUND
);
624 static struct fd
*sock_get_fd( struct object
*obj
)
626 struct sock
*sock
= (struct sock
*)obj
;
627 return (struct fd
*)grab_object( sock
->fd
);
630 static void sock_destroy( struct object
*obj
)
632 struct sock
*sock
= (struct sock
*)obj
;
633 assert( obj
->ops
== &sock_ops
);
635 /* FIXME: special socket shutdown stuff? */
637 if ( sock
->deferred
)
638 release_object( sock
->deferred
);
640 free_async_queue( sock
->read_q
);
641 free_async_queue( sock
->write_q
);
642 sock_destroy_ifchange_q( sock
);
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 void init_sock(struct sock
*sock
)
666 sock
->connect_time
= 0;
667 sock
->deferred
= NULL
;
669 sock
->write_q
= NULL
;
670 sock
->ifchange_q
= NULL
;
671 sock
->ifchange_obj
= NULL
;
672 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
675 /* create a new and unconnected socket */
676 static struct object
*create_socket( int family
, int type
, int protocol
, unsigned int flags
)
681 sockfd
= socket( family
, type
, protocol
);
683 fprintf(stderr
,"socket(%d,%d,%d)=%d\n",family
,type
,protocol
,sockfd
);
689 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
690 if (!(sock
= alloc_object( &sock_ops
)))
696 sock
->state
= (type
!= SOCK_STREAM
) ? (FD_READ
|FD_WRITE
) : 0;
698 sock
->proto
= protocol
;
700 sock
->family
= family
;
702 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
,
703 (flags
& WSA_FLAG_OVERLAPPED
) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT
)))
705 release_object( sock
);
708 sock_reselect( sock
);
713 /* accepts a socket and inits it */
714 static int accept_new_fd( struct sock
*sock
)
717 /* Try to accept(2). We can't be safe that this an already connected socket
718 * or that accept() is allowed on it. In those cases we will get -1/errno
722 struct sockaddr saddr
;
723 socklen_t slen
= sizeof(saddr
);
724 acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
731 fcntl(acceptfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
735 /* accept a socket (creates a new fd) */
736 static struct sock
*accept_socket( obj_handle_t handle
)
738 struct sock
*acceptsock
;
742 sock
= (struct sock
*)get_handle_obj( current
->process
, handle
, FILE_READ_DATA
, &sock_ops
);
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
= alloc_object( &sock_ops
)))
761 release_object( sock
);
765 init_sock( acceptsock
);
766 /* newly created socket gets the same properties of the listening socket */
767 acceptsock
->state
= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
768 if (sock
->state
& FD_WINE_NONBLOCKING
)
769 acceptsock
->state
|= FD_WINE_NONBLOCKING
;
770 acceptsock
->mask
= sock
->mask
;
771 acceptsock
->proto
= sock
->proto
;
772 acceptsock
->type
= sock
->type
;
773 acceptsock
->family
= sock
->family
;
774 acceptsock
->window
= sock
->window
;
775 acceptsock
->message
= sock
->message
;
776 acceptsock
->connect_time
= current_time
;
777 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
778 acceptsock
->flags
= sock
->flags
;
779 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
780 get_fd_options( sock
->fd
) )))
782 release_object( acceptsock
);
783 release_object( sock
);
788 sock
->pmask
&= ~FD_ACCEPT
;
789 sock
->hmask
&= ~FD_ACCEPT
;
790 sock_reselect( sock
);
791 release_object( sock
);
795 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
799 if ( sock
->deferred
)
801 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
802 get_fd_options( acceptsock
->fd
) );
806 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
808 release_object( sock
->deferred
);
809 sock
->deferred
= NULL
;
813 if ((acceptfd
= accept_new_fd( sock
)) == -1)
816 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
817 get_fd_options( acceptsock
->fd
) )))
821 acceptsock
->state
|= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
822 acceptsock
->hmask
= 0;
823 acceptsock
->pmask
= 0;
824 acceptsock
->polling
= 0;
825 acceptsock
->proto
= sock
->proto
;
826 acceptsock
->type
= sock
->type
;
827 acceptsock
->family
= sock
->family
;
828 acceptsock
->wparam
= 0;
829 acceptsock
->deferred
= NULL
;
830 acceptsock
->connect_time
= current_time
;
831 release_object( acceptsock
->fd
);
832 acceptsock
->fd
= newfd
;
835 sock
->pmask
&= ~FD_ACCEPT
;
836 sock
->hmask
&= ~FD_ACCEPT
;
837 sock_reselect( sock
);
842 /* return an errno value mapped to a WSA error */
843 static int sock_get_error( int err
)
847 case EINTR
: return WSAEINTR
;
848 case EBADF
: return WSAEBADF
;
850 case EACCES
: return WSAEACCES
;
851 case EFAULT
: return WSAEFAULT
;
852 case EINVAL
: return WSAEINVAL
;
853 case EMFILE
: return WSAEMFILE
;
854 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
855 case EINPROGRESS
: return WSAEINPROGRESS
;
856 case EALREADY
: return WSAEALREADY
;
857 case ENOTSOCK
: return WSAENOTSOCK
;
858 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
859 case EMSGSIZE
: return WSAEMSGSIZE
;
860 case EPROTOTYPE
: return WSAEPROTOTYPE
;
861 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
862 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
863 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
864 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
865 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
866 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
867 case EADDRINUSE
: return WSAEADDRINUSE
;
868 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
869 case ENETDOWN
: return WSAENETDOWN
;
870 case ENETUNREACH
: return WSAENETUNREACH
;
871 case ENETRESET
: return WSAENETRESET
;
872 case ECONNABORTED
: return WSAECONNABORTED
;
874 case ECONNRESET
: return WSAECONNRESET
;
875 case ENOBUFS
: return WSAENOBUFS
;
876 case EISCONN
: return WSAEISCONN
;
877 case ENOTCONN
: return WSAENOTCONN
;
878 case ESHUTDOWN
: return WSAESHUTDOWN
;
879 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
880 case ETIMEDOUT
: return WSAETIMEDOUT
;
881 case ECONNREFUSED
: return WSAECONNREFUSED
;
882 case ELOOP
: return WSAELOOP
;
883 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
884 case EHOSTDOWN
: return WSAEHOSTDOWN
;
885 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
886 case ENOTEMPTY
: return WSAENOTEMPTY
;
888 case EPROCLIM
: return WSAEPROCLIM
;
891 case EUSERS
: return WSAEUSERS
;
894 case EDQUOT
: return WSAEDQUOT
;
897 case ESTALE
: return WSAESTALE
;
900 case EREMOTE
: return WSAEREMOTE
;
906 perror("wineserver: sock_get_error() can't map error");
911 static int sock_get_ntstatus( int err
)
915 case EBADF
: return STATUS_INVALID_HANDLE
;
916 case EBUSY
: return STATUS_DEVICE_BUSY
;
918 case EACCES
: return STATUS_ACCESS_DENIED
;
919 case EFAULT
: return STATUS_NO_MEMORY
;
920 case EINVAL
: return STATUS_INVALID_PARAMETER
;
922 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
923 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
924 case EINPROGRESS
: return STATUS_PENDING
;
925 case EALREADY
: return STATUS_NETWORK_BUSY
;
926 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
927 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
928 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
929 case EPROTONOSUPPORT
:
930 case ESOCKTNOSUPPORT
:
933 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
934 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
935 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
936 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
937 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
938 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
939 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
940 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
941 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
942 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
943 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
944 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
946 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
947 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
949 case 0: return STATUS_SUCCESS
;
952 perror("wineserver: sock_get_ntstatus() can't map error");
953 return STATUS_UNSUCCESSFUL
;
957 /* set the last error depending on errno */
958 static void sock_set_error(void)
960 set_error( sock_get_ntstatus( errno
) );
963 /* add interface change notification to a socket */
964 static int sock_add_ifchange( struct sock
*sock
, const async_data_t
*async_data
)
966 struct async_queue
*ifchange_q
;
969 if (!(ifchange_q
= sock_get_ifchange_q( sock
)))
972 if (!(async
= create_async( current
, ifchange_q
, async_data
)))
974 if (!async_queued( ifchange_q
))
975 sock_destroy_ifchange_q( sock
);
977 set_error( STATUS_NO_MEMORY
);
981 release_object( async
);
982 set_error( STATUS_PENDING
);
986 #ifdef HAVE_LINUX_RTNETLINK_H
988 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
989 static struct object
*ifchange_object
;
991 static void ifchange_dump( struct object
*obj
, int verbose
);
992 static struct fd
*ifchange_get_fd( struct object
*obj
);
993 static void ifchange_destroy( struct object
*obj
);
995 static int ifchange_get_poll_events( struct fd
*fd
);
996 static void ifchange_poll_event( struct fd
*fd
, int event
);
997 static void ifchange_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
1001 struct object obj
; /* object header */
1002 struct fd
*fd
; /* interface change file descriptor */
1003 struct list sockets
; /* list of sockets to send interface change notifications */
1006 static const struct object_ops ifchange_ops
=
1008 sizeof(struct ifchange
), /* size */
1009 ifchange_dump
, /* dump */
1010 no_get_type
, /* get_type */
1011 add_queue
, /* add_queue */
1012 NULL
, /* remove_queue */
1013 NULL
, /* signaled */
1014 no_satisfied
, /* satisfied */
1015 no_signal
, /* signal */
1016 ifchange_get_fd
, /* get_fd */
1017 default_fd_map_access
, /* map_access */
1018 default_get_sd
, /* get_sd */
1019 default_set_sd
, /* set_sd */
1020 no_lookup_name
, /* lookup_name */
1021 no_open_file
, /* open_file */
1022 no_close_handle
, /* close_handle */
1023 ifchange_destroy
/* destroy */
1026 static const struct fd_ops ifchange_fd_ops
=
1028 ifchange_get_poll_events
, /* get_poll_events */
1029 ifchange_poll_event
, /* poll_event */
1031 NULL
, /* get_fd_type */
1033 NULL
, /* queue_async */
1034 ifchange_reselect_async
, /* reselect_async */
1035 NULL
/* cancel_async */
1038 static void ifchange_dump( struct object
*obj
, int verbose
)
1040 assert( obj
->ops
== &ifchange_ops
);
1041 fprintf( stderr
, "Interface change\n" );
1044 static struct fd
*ifchange_get_fd( struct object
*obj
)
1046 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1047 return (struct fd
*)grab_object( ifchange
->fd
);
1050 static void ifchange_destroy( struct object
*obj
)
1052 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1053 assert( obj
->ops
== &ifchange_ops
);
1055 release_object( ifchange
->fd
);
1057 /* reset the global ifchange object so that it will be recreated if it is needed again */
1058 assert( obj
== ifchange_object
);
1059 ifchange_object
= NULL
;
1062 static int ifchange_get_poll_events( struct fd
*fd
)
1067 /* wake up all the sockets waiting for a change notification event */
1068 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
1070 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1071 struct list
*ptr
, *next
;
1072 assert( obj
->ops
== &ifchange_ops
);
1073 assert( obj
== ifchange_object
);
1075 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
1077 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
1079 assert( sock
->ifchange_q
);
1080 async_wake_up( sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
1081 sock_destroy_ifchange_q( sock
); /* remove socket from list and decrement ifchange refcount */
1085 static void ifchange_poll_event( struct fd
*fd
, int event
)
1087 struct object
*ifchange
= get_fd_user( fd
);
1088 unsigned int status
= STATUS_PENDING
;
1089 char buffer
[PIPE_BUF
];
1092 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
1095 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1096 return; /* retry when poll() says the socket is ready */
1097 status
= sock_get_ntstatus( errno
);
1101 struct nlmsghdr
*nlh
;
1103 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
1105 if (nlh
->nlmsg_type
== NLMSG_DONE
)
1107 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
1108 status
= STATUS_SUCCESS
;
1111 else status
= STATUS_CANCELLED
;
1113 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
1116 static void ifchange_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
1118 /* do nothing, this object is about to disappear */
1123 /* we only need one of these interface notification objects, all of the sockets dependent upon
1124 * it will wake up when a notification event occurs */
1125 static struct object
*get_ifchange( void )
1127 #ifdef HAVE_LINUX_RTNETLINK_H
1128 struct ifchange
*ifchange
;
1129 struct sockaddr_nl addr
;
1132 if (ifchange_object
)
1134 /* increment the refcount for each socket that uses the ifchange object */
1135 return grab_object( ifchange_object
);
1138 /* create the socket we need for processing interface change notifications */
1139 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1145 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1146 memset( &addr
, 0, sizeof(addr
) );
1147 addr
.nl_family
= AF_NETLINK
;
1148 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
1149 /* bind the socket to the special netlink kernel interface */
1150 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
1156 if (!(ifchange
= alloc_object( &ifchange_ops
)))
1159 set_error( STATUS_NO_MEMORY
);
1162 list_init( &ifchange
->sockets
);
1163 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
1165 release_object( ifchange
);
1166 set_error( STATUS_NO_MEMORY
);
1169 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
1171 /* the ifchange object is now successfully configured */
1172 ifchange_object
= &ifchange
->obj
;
1173 return &ifchange
->obj
;
1175 set_error( STATUS_NOT_SUPPORTED
);
1180 /* add the socket to the interface change notification list */
1181 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
1183 #ifdef HAVE_LINUX_RTNETLINK_H
1184 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1186 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
1190 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1191 static struct async_queue
*sock_get_ifchange_q( struct sock
*sock
)
1193 struct object
*ifchange
;
1196 if (sock
->ifchange_q
) /* reuse existing ifchange_q for this socket */
1197 return sock
->ifchange_q
;
1199 if (!(ifchange
= get_ifchange()))
1202 /* create the ifchange notification queue */
1203 fd
= ifchange
->ops
->get_fd( ifchange
);
1204 sock
->ifchange_q
= create_async_queue( fd
);
1205 release_object( fd
);
1206 if (!sock
->ifchange_q
)
1208 release_object( ifchange
);
1209 set_error( STATUS_NO_MEMORY
);
1213 /* add the socket to the ifchange notification list */
1214 ifchange_add_sock( ifchange
, sock
);
1215 sock
->ifchange_obj
= ifchange
;
1216 return sock
->ifchange_q
;
1219 /* destroy an existing ifchange queue for a specific socket */
1220 static void sock_destroy_ifchange_q( struct sock
*sock
)
1222 if (sock
->ifchange_q
)
1224 list_remove( &sock
->ifchange_entry
);
1225 free_async_queue( sock
->ifchange_q
);
1226 sock
->ifchange_q
= NULL
;
1227 release_object( sock
->ifchange_obj
);
1231 /* create a socket */
1232 DECL_HANDLER(create_socket
)
1237 if ((obj
= create_socket( req
->family
, req
->type
, req
->protocol
, req
->flags
)) != NULL
)
1239 reply
->handle
= alloc_handle( current
->process
, obj
, req
->access
, req
->attributes
);
1240 release_object( obj
);
1244 /* accept a socket */
1245 DECL_HANDLER(accept_socket
)
1250 if ((sock
= accept_socket( req
->lhandle
)) != NULL
)
1252 reply
->handle
= alloc_handle( current
->process
, &sock
->obj
, req
->access
, req
->attributes
);
1253 sock
->wparam
= reply
->handle
; /* wparam for message is the socket handle */
1254 sock_reselect( sock
);
1255 release_object( &sock
->obj
);
1259 /* accept a socket into an initialized socket */
1260 DECL_HANDLER(accept_into_socket
)
1262 struct sock
*sock
, *acceptsock
;
1263 const int all_attributes
= FILE_READ_ATTRIBUTES
|FILE_WRITE_ATTRIBUTES
|FILE_READ_DATA
;
1265 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->lhandle
,
1266 all_attributes
, &sock_ops
)))
1269 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, req
->ahandle
,
1270 all_attributes
, &sock_ops
)))
1272 release_object( sock
);
1276 if (accept_into_socket( sock
, acceptsock
))
1278 acceptsock
->wparam
= req
->ahandle
; /* wparam for message is the socket handle */
1279 sock_reselect( acceptsock
);
1281 release_object( acceptsock
);
1282 release_object( sock
);
1285 /* set socket event parameters */
1286 DECL_HANDLER(set_socket_event
)
1289 struct event
*old_event
;
1291 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1292 FILE_WRITE_ATTRIBUTES
, &sock_ops
))) return;
1293 old_event
= sock
->event
;
1294 sock
->mask
= req
->mask
;
1295 sock
->hmask
&= ~req
->mask
; /* re-enable held events */
1297 sock
->window
= req
->window
;
1298 sock
->message
= req
->msg
;
1299 sock
->wparam
= req
->handle
; /* wparam is the socket handle */
1300 if (req
->event
) sock
->event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
1302 if (debug_level
&& sock
->event
) fprintf(stderr
, "event ptr: %p\n", sock
->event
);
1304 sock_reselect( sock
);
1306 sock
->state
|= FD_WINE_NONBLOCKING
;
1308 /* if a network event is pending, signal the event object
1309 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1310 before a WSAEventSelect() was done on it.
1311 (when dealing with Asynchronous socket) */
1312 sock_wake_up( sock
);
1314 if (old_event
) release_object( old_event
); /* we're through with it */
1315 release_object( &sock
->obj
);
1318 /* get socket event parameters */
1319 DECL_HANDLER(get_socket_event
)
1323 int errors
[FD_MAX_EVENTS
];
1325 sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
, &sock_ops
);
1333 reply
->mask
= sock
->mask
;
1334 reply
->pmask
= sock
->pmask
;
1335 reply
->state
= sock
->state
;
1336 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
1337 errors
[i
] = sock_get_ntstatus(sock
->errors
[i
]);
1339 set_reply_data( errors
, min( get_reply_max_size(), sizeof(errors
) ));
1345 struct event
*cevent
= get_event_obj( current
->process
, req
->c_event
,
1346 EVENT_MODIFY_STATE
);
1349 reset_event( cevent
);
1350 release_object( cevent
);
1354 sock_reselect( sock
);
1356 release_object( &sock
->obj
);
1359 /* re-enable pending socket events */
1360 DECL_HANDLER(enable_socket_event
)
1364 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1365 FILE_WRITE_ATTRIBUTES
, &sock_ops
)))
1368 /* for event-based notification, windows erases stale events */
1369 sock
->pmask
&= ~req
->mask
;
1371 sock
->hmask
&= ~req
->mask
;
1372 sock
->state
|= req
->sstate
;
1373 sock
->state
&= ~req
->cstate
;
1374 if ( sock
->type
!= SOCK_STREAM
) sock
->state
&= ~STREAM_FLAG_MASK
;
1376 sock_reselect( sock
);
1378 release_object( &sock
->obj
);
1381 DECL_HANDLER(set_socket_deferred
)
1383 struct sock
*sock
, *acceptsock
;
1385 sock
=(struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
, &sock_ops
);
1389 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, req
->deferred
, 0, &sock_ops
);
1392 release_object( sock
);
1395 sock
->deferred
= acceptsock
;
1396 release_object( sock
);
1399 DECL_HANDLER(get_socket_info
)
1403 sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
, &sock_ops
);
1406 reply
->family
= sock
->family
;
1407 reply
->type
= sock
->type
;
1408 reply
->protocol
= sock
->proto
;
1410 release_object( &sock
->obj
);