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
98 #define FD_MAX_EVENTS 10
100 #define FD_WRITE_BIT 1
102 #define FD_ACCEPT_BIT 3
103 #define FD_CONNECT_BIT 4
104 #define FD_CLOSE_BIT 5
107 * Define flags to be used with the WSAAsyncSelect() call.
109 #define FD_READ 0x00000001
110 #define FD_WRITE 0x00000002
111 #define FD_OOB 0x00000004
112 #define FD_ACCEPT 0x00000008
113 #define FD_CONNECT 0x00000010
114 #define FD_CLOSE 0x00000020
116 /* internal per-socket flags */
117 #define FD_WINE_LISTENING 0x10000000
118 #define FD_WINE_NONBLOCKING 0x20000000
119 #define FD_WINE_CONNECTED 0x40000000
120 #define FD_WINE_RAW 0x80000000
121 #define FD_WINE_INTERNAL 0xFFFF0000
125 struct object obj
; /* object header */
126 struct fd
*fd
; /* socket file descriptor */
127 unsigned int state
; /* status bits */
128 unsigned int mask
; /* event mask */
129 unsigned int hmask
; /* held (blocked) events */
130 unsigned int pmask
; /* pending events */
131 unsigned int flags
; /* socket flags */
132 int polling
; /* is socket being polled? */
133 unsigned short proto
; /* socket protocol */
134 unsigned short type
; /* socket type */
135 unsigned short family
; /* socket family */
136 struct event
*event
; /* event object */
137 user_handle_t window
; /* window to send the message to */
138 unsigned int message
; /* message to send */
139 obj_handle_t wparam
; /* message wparam (socket handle) */
140 unsigned int errors
[FD_MAX_EVENTS
]; /* event errors */
141 timeout_t connect_time
;/* time the socket was connected */
142 struct sock
*deferred
; /* socket that waits for a deferred accept */
143 struct async_queue read_q
; /* queue for asynchronous reads */
144 struct async_queue write_q
; /* queue for asynchronous writes */
145 struct async_queue ifchange_q
; /* queue for interface change notifications */
146 struct object
*ifchange_obj
; /* the interface change notification object */
147 struct list ifchange_entry
; /* entry in ifchange notification list */
150 static void sock_dump( struct object
*obj
, int verbose
);
151 static int sock_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
152 static struct fd
*sock_get_fd( struct object
*obj
);
153 static void sock_destroy( struct object
*obj
);
154 static struct object
*sock_get_ifchange( struct sock
*sock
);
155 static void sock_release_ifchange( struct sock
*sock
);
157 static int sock_get_poll_events( struct fd
*fd
);
158 static void sock_poll_event( struct fd
*fd
, int event
);
159 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
160 static int sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
161 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
);
162 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
164 static int sock_get_ntstatus( int err
);
165 static unsigned int sock_get_error( int err
);
167 static const struct object_ops sock_ops
=
169 sizeof(struct sock
), /* size */
170 sock_dump
, /* dump */
171 no_get_type
, /* get_type */
172 add_queue
, /* add_queue */
173 remove_queue
, /* remove_queue */
174 sock_signaled
, /* signaled */
175 no_satisfied
, /* satisfied */
176 no_signal
, /* signal */
177 sock_get_fd
, /* get_fd */
178 default_fd_map_access
, /* map_access */
179 default_get_sd
, /* get_sd */
180 default_set_sd
, /* set_sd */
181 no_get_full_name
, /* get_full_name */
182 no_lookup_name
, /* lookup_name */
183 no_link_name
, /* link_name */
184 NULL
, /* unlink_name */
185 no_open_file
, /* open_file */
186 no_kernel_obj_list
, /* get_kernel_obj_list */
187 fd_close_handle
, /* close_handle */
188 sock_destroy
/* destroy */
191 static const struct fd_ops sock_fd_ops
=
193 sock_get_poll_events
, /* get_poll_events */
194 sock_poll_event
, /* poll_event */
195 sock_get_fd_type
, /* get_fd_type */
196 no_fd_read
, /* read */
197 no_fd_write
, /* write */
198 no_fd_flush
, /* flush */
199 default_fd_get_file_info
, /* get_file_info */
200 no_fd_get_volume_info
, /* get_volume_info */
201 sock_ioctl
, /* ioctl */
202 sock_queue_async
, /* queue_async */
203 sock_reselect_async
/* reselect_async */
207 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
208 * we post messages if there are multiple events. Used to send
209 * messages. The problem is if there is both a FD_CONNECT event and,
210 * say, an FD_READ event available on the same socket, we want to
211 * notify the app of the connect event first. Otherwise it may
212 * discard the read event because it thinks it hasn't connected yet.
214 static const int event_bitorder
[FD_MAX_EVENTS
] =
222 6, 7, 8, 9 /* leftovers */
225 /* Flags that make sense only for SOCK_STREAM sockets */
226 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
229 SOCK_SHUTDOWN_ERROR
= -1,
230 SOCK_SHUTDOWN_EOF
= 0,
231 SOCK_SHUTDOWN_POLLHUP
= 1
234 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
236 static sock_shutdown_t
sock_check_pollhup(void)
238 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
243 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) return ret
;
244 if ( shutdown( fd
[0], 1 ) ) goto out
;
250 /* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
251 n
= poll( &pfd
, 1, 1 );
252 if ( n
!= 1 ) goto out
; /* error or timeout */
253 if ( pfd
.revents
& POLLHUP
)
254 ret
= SOCK_SHUTDOWN_POLLHUP
;
255 else if ( pfd
.revents
& POLLIN
&&
256 read( fd
[1], &dummy
, 1 ) == 0 )
257 ret
= SOCK_SHUTDOWN_EOF
;
267 sock_shutdown_type
= sock_check_pollhup();
269 switch ( sock_shutdown_type
)
271 case SOCK_SHUTDOWN_EOF
:
272 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
274 case SOCK_SHUTDOWN_POLLHUP
:
275 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
278 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
279 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
283 static int sock_reselect( struct sock
*sock
)
285 int ev
= sock_get_poll_events( sock
->fd
);
288 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
290 if (!sock
->polling
) /* FIXME: should find a better way to do this */
292 /* previously unconnected socket, is this reselect supposed to connect it? */
293 if (!(sock
->state
& ~FD_WINE_NONBLOCKING
)) return 0;
294 /* ok, it is, attach it to the wineserver's main poll loop */
296 allow_fd_caching( sock
->fd
);
298 /* update condition mask */
299 set_fd_events( sock
->fd
, ev
);
303 /* wake anybody waiting on the socket event or send the associated message */
304 static void sock_wake_up( struct sock
*sock
)
306 unsigned int events
= sock
->pmask
& sock
->mask
;
309 if ( !events
) return;
313 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
314 set_event( sock
->event
);
318 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
319 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
321 int event
= event_bitorder
[i
];
322 if (sock
->pmask
& (1 << event
))
324 lparam_t lparam
= (1 << event
) | (sock
->errors
[event
] << 16);
325 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
329 sock_reselect( sock
);
333 static inline int sock_error( struct fd
*fd
)
335 unsigned int optval
= 0;
336 socklen_t optlen
= sizeof(optval
);
338 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
342 static int sock_dispatch_asyncs( struct sock
*sock
, int event
, int error
)
344 if (is_fd_overlapped( sock
->fd
))
346 if (event
& (POLLIN
|POLLPRI
) && async_waiting( &sock
->read_q
))
348 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
349 async_wake_up( &sock
->read_q
, STATUS_ALERTED
);
350 event
&= ~(POLLIN
|POLLPRI
);
352 if (event
& POLLOUT
&& async_waiting( &sock
->write_q
))
354 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
355 async_wake_up( &sock
->write_q
, STATUS_ALERTED
);
358 if ( event
& (POLLERR
|POLLHUP
) )
360 int status
= sock_get_ntstatus( error
);
362 if ( !(sock
->state
& FD_READ
) )
363 async_wake_up( &sock
->read_q
, status
);
364 if ( !(sock
->state
& FD_WRITE
) )
365 async_wake_up( &sock
->write_q
, status
);
371 static void sock_dispatch_events( struct sock
*sock
, int prevstate
, int event
, int error
)
373 if (prevstate
& FD_CONNECT
)
375 sock
->pmask
|= FD_CONNECT
;
376 sock
->hmask
|= FD_CONNECT
;
377 sock
->errors
[FD_CONNECT_BIT
] = sock_get_error( error
);
380 if (prevstate
& FD_WINE_LISTENING
)
382 sock
->pmask
|= FD_ACCEPT
;
383 sock
->hmask
|= FD_ACCEPT
;
384 sock
->errors
[FD_ACCEPT_BIT
] = sock_get_error( error
);
390 sock
->pmask
|= FD_READ
;
391 sock
->hmask
|= FD_READ
;
392 sock
->errors
[FD_READ_BIT
] = 0;
397 sock
->pmask
|= FD_WRITE
;
398 sock
->hmask
|= FD_WRITE
;
399 sock
->errors
[FD_WRITE_BIT
] = 0;
404 sock
->pmask
|= FD_OOB
;
405 sock
->hmask
|= FD_OOB
;
406 sock
->errors
[FD_OOB_BIT
] = 0;
409 if (event
& (POLLERR
|POLLHUP
))
411 sock
->pmask
|= FD_CLOSE
;
412 sock
->hmask
|= FD_CLOSE
;
413 sock
->errors
[FD_CLOSE_BIT
] = sock_get_error( error
);
416 sock_wake_up( sock
);
419 static void sock_poll_event( struct fd
*fd
, int event
)
421 struct sock
*sock
= get_fd_user( fd
);
423 int prevstate
= sock
->state
;
426 assert( sock
->obj
.ops
== &sock_ops
);
428 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
430 /* we may change event later, remove from loop here */
431 if (event
& (POLLERR
|POLLHUP
)) set_fd_events( sock
->fd
, -1 );
433 if (sock
->state
& FD_CONNECT
)
435 if (event
& (POLLERR
|POLLHUP
))
437 /* we didn't get connected? */
438 sock
->state
&= ~FD_CONNECT
;
440 error
= sock_error( fd
);
442 else if (event
& POLLOUT
)
444 /* we got connected */
445 sock
->state
|= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
446 sock
->state
&= ~FD_CONNECT
;
447 sock
->connect_time
= current_time
;
450 else if (sock
->state
& FD_WINE_LISTENING
)
453 if (event
& (POLLERR
|POLLHUP
))
454 error
= sock_error( fd
);
458 /* normal data flow */
459 if (sock
->type
== WS_SOCK_STREAM
&& (event
& POLLIN
))
464 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
465 * has been closed, so we need to check for it explicitly here */
466 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
475 /* EAGAIN can happen if an async recv() falls between the server's poll()
476 call and the invocation of this routine */
477 if ( errno
!= EAGAIN
)
482 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
487 if ( (hangup_seen
|| event
& (POLLHUP
|POLLERR
)) && (sock
->state
& (FD_READ
|FD_WRITE
)) )
489 error
= error
? error
: sock_error( fd
);
490 if ( (event
& POLLERR
) || ( sock_shutdown_type
== SOCK_SHUTDOWN_EOF
&& (event
& POLLHUP
) ))
491 sock
->state
&= ~FD_WRITE
;
492 sock
->state
&= ~FD_READ
;
495 fprintf(stderr
, "socket %p aborted by error %d, event: %x\n", sock
, error
, event
);
502 event
= sock_dispatch_asyncs( sock
, event
, error
);
503 sock_dispatch_events( sock
, prevstate
, event
, error
);
505 /* if anyone is stupid enough to wait on the socket object itself,
506 * maybe we should wake them up too, just in case? */
507 wake_up( &sock
->obj
, 0 );
509 sock_reselect( sock
);
512 static void sock_dump( struct object
*obj
, int verbose
)
514 struct sock
*sock
= (struct sock
*)obj
;
515 assert( obj
->ops
== &sock_ops
);
516 fprintf( stderr
, "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
517 sock
->fd
, sock
->state
,
518 sock
->mask
, sock
->pmask
, sock
->hmask
);
521 static int sock_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
523 struct sock
*sock
= (struct sock
*)obj
;
524 assert( obj
->ops
== &sock_ops
);
526 return check_fd_events( sock
->fd
, sock_get_poll_events( sock
->fd
) ) != 0;
529 static int sock_get_poll_events( struct fd
*fd
)
531 struct sock
*sock
= get_fd_user( fd
);
532 unsigned int mask
= sock
->mask
& ~sock
->hmask
;
533 unsigned int smask
= sock
->state
& mask
;
536 assert( sock
->obj
.ops
== &sock_ops
);
538 if (sock
->state
& FD_CONNECT
)
539 /* connecting, wait for writable */
542 if (async_queued( &sock
->read_q
))
544 if (async_waiting( &sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
546 else if (smask
& FD_READ
|| (sock
->state
& FD_WINE_LISTENING
&& mask
& FD_ACCEPT
))
547 ev
|= POLLIN
| POLLPRI
;
548 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
549 else if (sock
->type
== WS_SOCK_STREAM
&& (sock
->state
& FD_READ
) && (mask
& FD_CLOSE
) &&
550 !(sock
->hmask
& FD_READ
))
553 if (async_queued( &sock
->write_q
))
555 if (async_waiting( &sock
->write_q
)) ev
|= POLLOUT
;
557 else if (smask
& FD_WRITE
)
563 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
565 return FD_TYPE_SOCKET
;
568 static void sock_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
570 struct sock
*sock
= get_fd_user( fd
);
571 struct async_queue
*queue
;
573 assert( sock
->obj
.ops
== &sock_ops
);
577 case ASYNC_TYPE_READ
:
578 queue
= &sock
->read_q
;
580 case ASYNC_TYPE_WRITE
:
581 queue
= &sock
->write_q
;
584 set_error( STATUS_INVALID_PARAMETER
);
588 if ( ( !( sock
->state
& (FD_READ
|FD_CONNECT
|FD_WINE_LISTENING
) ) && type
== ASYNC_TYPE_READ
) ||
589 ( !( sock
->state
& (FD_WRITE
|FD_CONNECT
) ) && type
== ASYNC_TYPE_WRITE
) )
591 set_error( STATUS_PIPE_DISCONNECTED
);
595 queue_async( queue
, async
);
596 sock_reselect( sock
);
598 set_error( STATUS_PENDING
);
601 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
603 struct sock
*sock
= get_fd_user( fd
);
604 /* ignore reselect on ifchange queue */
605 if (&sock
->ifchange_q
!= queue
)
606 sock_reselect( sock
);
609 static struct fd
*sock_get_fd( struct object
*obj
)
611 struct sock
*sock
= (struct sock
*)obj
;
612 return (struct fd
*)grab_object( sock
->fd
);
615 static void sock_destroy( struct object
*obj
)
617 struct sock
*sock
= (struct sock
*)obj
;
618 assert( obj
->ops
== &sock_ops
);
620 /* FIXME: special socket shutdown stuff? */
622 if ( sock
->deferred
)
623 release_object( sock
->deferred
);
625 async_wake_up( &sock
->ifchange_q
, STATUS_CANCELLED
);
626 sock_release_ifchange( sock
);
627 free_async_queue( &sock
->read_q
);
628 free_async_queue( &sock
->write_q
);
629 free_async_queue( &sock
->ifchange_q
);
630 if (sock
->event
) release_object( sock
->event
);
633 /* shut the socket down to force pending poll() calls in the client to return */
634 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
635 release_object( sock
->fd
);
639 static struct sock
*create_socket(void)
643 if (!(sock
= alloc_object( &sock_ops
))) return NULL
;
658 sock
->connect_time
= 0;
659 sock
->deferred
= NULL
;
660 sock
->ifchange_obj
= NULL
;
661 init_async_queue( &sock
->read_q
);
662 init_async_queue( &sock
->write_q
);
663 init_async_queue( &sock
->ifchange_q
);
664 memset( sock
->errors
, 0, sizeof(sock
->errors
) );
668 static int get_unix_family( int family
)
672 case WS_AF_INET
: return AF_INET
;
673 case WS_AF_INET6
: return AF_INET6
;
675 case WS_AF_IPX
: return AF_IPX
;
678 case WS_AF_IRDA
: return AF_IRDA
;
680 case WS_AF_UNSPEC
: return AF_UNSPEC
;
685 static int get_unix_type( int type
)
689 case WS_SOCK_DGRAM
: return SOCK_DGRAM
;
690 case WS_SOCK_RAW
: return SOCK_RAW
;
691 case WS_SOCK_STREAM
: return SOCK_STREAM
;
696 static int get_unix_protocol( int protocol
)
698 if (protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
703 case WS_IPPROTO_ICMP
: return IPPROTO_ICMP
;
704 case WS_IPPROTO_IGMP
: return IPPROTO_IGMP
;
705 case WS_IPPROTO_IP
: return IPPROTO_IP
;
706 case WS_IPPROTO_IPIP
: return IPPROTO_IPIP
;
707 case WS_IPPROTO_IPV6
: return IPPROTO_IPV6
;
708 case WS_IPPROTO_RAW
: return IPPROTO_RAW
;
709 case WS_IPPROTO_TCP
: return IPPROTO_TCP
;
710 case WS_IPPROTO_UDP
: return IPPROTO_UDP
;
715 static void set_dont_fragment( int fd
, int level
, int value
)
719 if (level
== IPPROTO_IP
)
722 optname
= IP_DONTFRAG
;
723 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
724 optname
= IP_MTU_DISCOVER
;
725 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
733 optname
= IPV6_DONTFRAG
;
734 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
735 optname
= IPV6_MTU_DISCOVER
;
736 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
742 setsockopt( fd
, level
, optname
, &value
, sizeof(value
) );
745 static int init_socket( struct sock
*sock
, int family
, int type
, int protocol
, unsigned int flags
)
747 unsigned int options
= 0;
748 int sockfd
, unix_type
, unix_family
, unix_protocol
;
750 unix_family
= get_unix_family( family
);
751 unix_type
= get_unix_type( type
);
752 unix_protocol
= get_unix_protocol( protocol
);
754 if (unix_protocol
< 0)
756 if (type
&& unix_type
< 0)
757 set_win32_error( WSAESOCKTNOSUPPORT
);
759 set_win32_error( WSAEPROTONOSUPPORT
);
764 if (family
>= 0 && unix_type
< 0)
765 set_win32_error( WSAESOCKTNOSUPPORT
);
767 set_win32_error( WSAEAFNOSUPPORT
);
771 sockfd
= socket( unix_family
, unix_type
, unix_protocol
);
774 if (errno
== EINVAL
) set_win32_error( WSAESOCKTNOSUPPORT
);
775 else set_win32_error( sock_get_error( errno
));
778 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
780 if (family
== WS_AF_IPX
&& protocol
>= WS_NSPROTO_IPX
&& protocol
<= WS_NSPROTO_IPX
+ 255)
783 int ipx_type
= protocol
- WS_NSPROTO_IPX
;
786 setsockopt( sockfd
, SOL_IPX
, IPX_TYPE
, &ipx_type
, sizeof(ipx_type
) );
789 /* Should we retrieve val using a getsockopt call and then
790 * set the modified one? */
791 val
.ipx_pt
= ipx_type
;
792 setsockopt( sockfd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(val
) );
797 if (unix_family
== AF_INET
|| unix_family
== AF_INET6
)
799 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
800 if (unix_type
== SOCK_DGRAM
|| unix_type
== SOCK_RAW
) /* in Linux the global default can be enabled */
801 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
802 else if (unix_type
== SOCK_STREAM
)
803 set_dont_fragment( sockfd
, unix_family
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
807 if (unix_family
== AF_INET6
)
809 static const int enable
= 1;
810 setsockopt( sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
) );
814 sock
->state
= (type
!= SOCK_STREAM
) ? (FD_READ
|FD_WRITE
) : 0;
816 sock
->proto
= protocol
;
818 sock
->family
= family
;
822 options
= get_fd_options( sock
->fd
);
823 release_object( sock
->fd
);
826 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
, options
)))
830 sock_reselect( sock
);
835 /* accepts a socket and inits it */
836 static int accept_new_fd( struct sock
*sock
)
839 /* Try to accept(2). We can't be safe that this an already connected socket
840 * or that accept() is allowed on it. In those cases we will get -1/errno
843 struct sockaddr saddr
;
844 socklen_t slen
= sizeof(saddr
);
845 int acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
847 fcntl( acceptfd
, F_SETFL
, O_NONBLOCK
);
849 set_win32_error( sock_get_error( errno
));
853 /* accept a socket (creates a new fd) */
854 static struct sock
*accept_socket( struct sock
*sock
)
856 struct sock
*acceptsock
;
859 if (get_unix_fd( sock
->fd
) == -1) return NULL
;
861 if ( sock
->deferred
)
863 acceptsock
= sock
->deferred
;
864 sock
->deferred
= NULL
;
868 if ((acceptfd
= accept_new_fd( sock
)) == -1) return NULL
;
869 if (!(acceptsock
= create_socket()))
875 /* newly created socket gets the same properties of the listening socket */
876 acceptsock
->state
= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
877 if (sock
->state
& FD_WINE_NONBLOCKING
)
878 acceptsock
->state
|= FD_WINE_NONBLOCKING
;
879 acceptsock
->mask
= sock
->mask
;
880 acceptsock
->proto
= sock
->proto
;
881 acceptsock
->type
= sock
->type
;
882 acceptsock
->family
= sock
->family
;
883 acceptsock
->window
= sock
->window
;
884 acceptsock
->message
= sock
->message
;
885 acceptsock
->connect_time
= current_time
;
886 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
887 acceptsock
->flags
= sock
->flags
;
888 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
889 get_fd_options( sock
->fd
) )))
891 release_object( acceptsock
);
896 sock
->pmask
&= ~FD_ACCEPT
;
897 sock
->hmask
&= ~FD_ACCEPT
;
898 sock_reselect( sock
);
902 static int accept_into_socket( struct sock
*sock
, struct sock
*acceptsock
)
907 if (get_unix_fd( sock
->fd
) == -1) return FALSE
;
909 if ( sock
->deferred
)
911 newfd
= dup_fd_object( sock
->deferred
->fd
, 0, 0,
912 get_fd_options( acceptsock
->fd
) );
916 set_fd_user( newfd
, &sock_fd_ops
, &acceptsock
->obj
);
918 release_object( sock
->deferred
);
919 sock
->deferred
= NULL
;
923 if ((acceptfd
= accept_new_fd( sock
)) == -1)
926 if (!(newfd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
927 get_fd_options( acceptsock
->fd
) )))
931 acceptsock
->state
|= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
932 acceptsock
->hmask
= 0;
933 acceptsock
->pmask
= 0;
934 acceptsock
->polling
= 0;
935 acceptsock
->proto
= sock
->proto
;
936 acceptsock
->type
= sock
->type
;
937 acceptsock
->family
= sock
->family
;
938 acceptsock
->wparam
= 0;
939 acceptsock
->deferred
= NULL
;
940 acceptsock
->connect_time
= current_time
;
941 fd_copy_completion( acceptsock
->fd
, newfd
);
942 release_object( acceptsock
->fd
);
943 acceptsock
->fd
= newfd
;
946 sock
->pmask
&= ~FD_ACCEPT
;
947 sock
->hmask
&= ~FD_ACCEPT
;
948 sock_reselect( sock
);
953 /* return an errno value mapped to a WSA error */
954 static unsigned int sock_get_error( int err
)
958 case EINTR
: return WSAEINTR
;
959 case EBADF
: return WSAEBADF
;
961 case EACCES
: return WSAEACCES
;
962 case EFAULT
: return WSAEFAULT
;
963 case EINVAL
: return WSAEINVAL
;
964 case EMFILE
: return WSAEMFILE
;
965 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
966 case EINPROGRESS
: return WSAEINPROGRESS
;
967 case EALREADY
: return WSAEALREADY
;
968 case ENOTSOCK
: return WSAENOTSOCK
;
969 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
970 case EMSGSIZE
: return WSAEMSGSIZE
;
971 case EPROTOTYPE
: return WSAEPROTOTYPE
;
972 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
973 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
974 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
975 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
976 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
977 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
978 case EADDRINUSE
: return WSAEADDRINUSE
;
979 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
980 case ENETDOWN
: return WSAENETDOWN
;
981 case ENETUNREACH
: return WSAENETUNREACH
;
982 case ENETRESET
: return WSAENETRESET
;
983 case ECONNABORTED
: return WSAECONNABORTED
;
985 case ECONNRESET
: return WSAECONNRESET
;
986 case ENOBUFS
: return WSAENOBUFS
;
987 case EISCONN
: return WSAEISCONN
;
988 case ENOTCONN
: return WSAENOTCONN
;
989 case ESHUTDOWN
: return WSAESHUTDOWN
;
990 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
991 case ETIMEDOUT
: return WSAETIMEDOUT
;
992 case ECONNREFUSED
: return WSAECONNREFUSED
;
993 case ELOOP
: return WSAELOOP
;
994 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
995 case EHOSTDOWN
: return WSAEHOSTDOWN
;
996 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
997 case ENOTEMPTY
: return WSAENOTEMPTY
;
999 case EPROCLIM
: return WSAEPROCLIM
;
1002 case EUSERS
: return WSAEUSERS
;
1005 case EDQUOT
: return WSAEDQUOT
;
1008 case ESTALE
: return WSAESTALE
;
1011 case EREMOTE
: return WSAEREMOTE
;
1017 perror("wineserver: sock_get_error() can't map error");
1022 static int sock_get_ntstatus( int err
)
1026 case EBADF
: return STATUS_INVALID_HANDLE
;
1027 case EBUSY
: return STATUS_DEVICE_BUSY
;
1029 case EACCES
: return STATUS_ACCESS_DENIED
;
1030 case EFAULT
: return STATUS_NO_MEMORY
;
1031 case EINVAL
: return STATUS_INVALID_PARAMETER
;
1033 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
1034 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
1035 case EINPROGRESS
: return STATUS_PENDING
;
1036 case EALREADY
: return STATUS_NETWORK_BUSY
;
1037 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
1038 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
1039 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
1040 case EPROTONOSUPPORT
:
1041 case ESOCKTNOSUPPORT
:
1044 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
1045 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
1046 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
1047 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
1048 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
1049 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
1050 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
1051 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
1052 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
1053 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
1054 case EHOSTUNREACH
: return STATUS_HOST_UNREACHABLE
;
1055 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
1057 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
1058 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
1060 case 0: return STATUS_SUCCESS
;
1063 perror("wineserver: sock_get_ntstatus() can't map error");
1064 return STATUS_UNSUCCESSFUL
;
1068 static int sock_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
1070 struct sock
*sock
= get_fd_user( fd
);
1072 assert( sock
->obj
.ops
== &sock_ops
);
1074 if (get_unix_fd( fd
) == -1 && code
!= IOCTL_AFD_CREATE
) return 0;
1078 case IOCTL_AFD_CREATE
:
1080 const struct afd_create_params
*params
= get_req_data();
1082 if (get_req_data_size() != sizeof(*params
))
1084 set_error( STATUS_INVALID_PARAMETER
);
1087 init_socket( sock
, params
->family
, params
->type
, params
->protocol
, params
->flags
);
1091 case IOCTL_AFD_ACCEPT
:
1093 struct sock
*acceptsock
;
1094 obj_handle_t handle
;
1096 if (get_reply_max_size() != sizeof(handle
))
1098 set_error( STATUS_BUFFER_TOO_SMALL
);
1102 if (!(acceptsock
= accept_socket( sock
))) return 0;
1103 handle
= alloc_handle( current
->process
, &acceptsock
->obj
,
1104 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, OBJ_INHERIT
);
1105 acceptsock
->wparam
= handle
;
1106 release_object( acceptsock
);
1107 set_reply_data( &handle
, sizeof(handle
) );
1111 case IOCTL_AFD_ACCEPT_INTO
:
1113 static const int access
= FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
| FILE_READ_DATA
;
1114 struct sock
*acceptsock
;
1115 obj_handle_t handle
;
1117 if (get_req_data_size() != sizeof(handle
))
1119 set_error( STATUS_BUFFER_TOO_SMALL
);
1122 handle
= *(obj_handle_t
*)get_req_data();
1124 if (!(acceptsock
= (struct sock
*)get_handle_obj( current
->process
, handle
, access
, &sock_ops
)))
1126 if (accept_into_socket( sock
, acceptsock
))
1127 acceptsock
->wparam
= handle
;
1128 release_object( acceptsock
);
1132 case IOCTL_AFD_ADDRESS_LIST_CHANGE
:
1133 if ((sock
->state
& FD_WINE_NONBLOCKING
) && async_is_blocking( async
))
1135 set_win32_error( WSAEWOULDBLOCK
);
1138 if (!sock_get_ifchange( sock
)) return 0;
1139 queue_async( &sock
->ifchange_q
, async
);
1140 set_error( STATUS_PENDING
);
1144 set_error( STATUS_NOT_SUPPORTED
);
1149 #ifdef HAVE_LINUX_RTNETLINK_H
1151 /* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
1152 static struct object
*ifchange_object
;
1154 static void ifchange_dump( struct object
*obj
, int verbose
);
1155 static struct fd
*ifchange_get_fd( struct object
*obj
);
1156 static void ifchange_destroy( struct object
*obj
);
1158 static int ifchange_get_poll_events( struct fd
*fd
);
1159 static void ifchange_poll_event( struct fd
*fd
, int event
);
1163 struct object obj
; /* object header */
1164 struct fd
*fd
; /* interface change file descriptor */
1165 struct list sockets
; /* list of sockets to send interface change notifications */
1168 static const struct object_ops ifchange_ops
=
1170 sizeof(struct ifchange
), /* size */
1171 ifchange_dump
, /* dump */
1172 no_get_type
, /* get_type */
1173 add_queue
, /* add_queue */
1174 NULL
, /* remove_queue */
1175 NULL
, /* signaled */
1176 no_satisfied
, /* satisfied */
1177 no_signal
, /* signal */
1178 ifchange_get_fd
, /* get_fd */
1179 default_fd_map_access
, /* map_access */
1180 default_get_sd
, /* get_sd */
1181 default_set_sd
, /* set_sd */
1182 no_get_full_name
, /* get_full_name */
1183 no_lookup_name
, /* lookup_name */
1184 no_link_name
, /* link_name */
1185 NULL
, /* unlink_name */
1186 no_open_file
, /* open_file */
1187 no_kernel_obj_list
, /* get_kernel_obj_list */
1188 no_close_handle
, /* close_handle */
1189 ifchange_destroy
/* destroy */
1192 static const struct fd_ops ifchange_fd_ops
=
1194 ifchange_get_poll_events
, /* get_poll_events */
1195 ifchange_poll_event
, /* poll_event */
1196 NULL
, /* get_fd_type */
1197 no_fd_read
, /* read */
1198 no_fd_write
, /* write */
1199 no_fd_flush
, /* flush */
1200 no_fd_get_file_info
, /* get_file_info */
1201 no_fd_get_volume_info
, /* get_volume_info */
1202 no_fd_ioctl
, /* ioctl */
1203 NULL
, /* queue_async */
1204 NULL
/* reselect_async */
1207 static void ifchange_dump( struct object
*obj
, int verbose
)
1209 assert( obj
->ops
== &ifchange_ops
);
1210 fprintf( stderr
, "Interface change\n" );
1213 static struct fd
*ifchange_get_fd( struct object
*obj
)
1215 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1216 return (struct fd
*)grab_object( ifchange
->fd
);
1219 static void ifchange_destroy( struct object
*obj
)
1221 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1222 assert( obj
->ops
== &ifchange_ops
);
1224 release_object( ifchange
->fd
);
1226 /* reset the global ifchange object so that it will be recreated if it is needed again */
1227 assert( obj
== ifchange_object
);
1228 ifchange_object
= NULL
;
1231 static int ifchange_get_poll_events( struct fd
*fd
)
1236 /* wake up all the sockets waiting for a change notification event */
1237 static void ifchange_wake_up( struct object
*obj
, unsigned int status
)
1239 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1240 struct list
*ptr
, *next
;
1241 assert( obj
->ops
== &ifchange_ops
);
1242 assert( obj
== ifchange_object
);
1244 LIST_FOR_EACH_SAFE( ptr
, next
, &ifchange
->sockets
)
1246 struct sock
*sock
= LIST_ENTRY( ptr
, struct sock
, ifchange_entry
);
1248 assert( sock
->ifchange_obj
);
1249 async_wake_up( &sock
->ifchange_q
, status
); /* issue ifchange notification for the socket */
1250 sock_release_ifchange( sock
); /* remove socket from list and decrement ifchange refcount */
1254 static void ifchange_poll_event( struct fd
*fd
, int event
)
1256 struct object
*ifchange
= get_fd_user( fd
);
1257 unsigned int status
= STATUS_PENDING
;
1258 char buffer
[PIPE_BUF
];
1261 r
= recv( get_unix_fd(fd
), buffer
, sizeof(buffer
), MSG_DONTWAIT
);
1264 if (errno
== EWOULDBLOCK
|| (EWOULDBLOCK
!= EAGAIN
&& errno
== EAGAIN
))
1265 return; /* retry when poll() says the socket is ready */
1266 status
= sock_get_ntstatus( errno
);
1270 struct nlmsghdr
*nlh
;
1272 for (nlh
= (struct nlmsghdr
*)buffer
; NLMSG_OK(nlh
, r
); nlh
= NLMSG_NEXT(nlh
, r
))
1274 if (nlh
->nlmsg_type
== NLMSG_DONE
)
1276 if (nlh
->nlmsg_type
== RTM_NEWADDR
|| nlh
->nlmsg_type
== RTM_DELADDR
)
1277 status
= STATUS_SUCCESS
;
1280 else status
= STATUS_CANCELLED
;
1282 if (status
!= STATUS_PENDING
) ifchange_wake_up( ifchange
, status
);
1287 /* we only need one of these interface notification objects, all of the sockets dependent upon
1288 * it will wake up when a notification event occurs */
1289 static struct object
*get_ifchange( void )
1291 #ifdef HAVE_LINUX_RTNETLINK_H
1292 struct ifchange
*ifchange
;
1293 struct sockaddr_nl addr
;
1296 if (ifchange_object
)
1298 /* increment the refcount for each socket that uses the ifchange object */
1299 return grab_object( ifchange_object
);
1302 /* create the socket we need for processing interface change notifications */
1303 unix_fd
= socket( PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1306 set_win32_error( sock_get_error( errno
));
1309 fcntl( unix_fd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
1310 memset( &addr
, 0, sizeof(addr
) );
1311 addr
.nl_family
= AF_NETLINK
;
1312 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
;
1313 /* bind the socket to the special netlink kernel interface */
1314 if (bind( unix_fd
, (struct sockaddr
*)&addr
, sizeof(addr
) ) == -1)
1317 set_win32_error( sock_get_error( errno
));
1320 if (!(ifchange
= alloc_object( &ifchange_ops
)))
1323 set_error( STATUS_NO_MEMORY
);
1326 list_init( &ifchange
->sockets
);
1327 if (!(ifchange
->fd
= create_anonymous_fd( &ifchange_fd_ops
, unix_fd
, &ifchange
->obj
, 0 )))
1329 release_object( ifchange
);
1330 set_error( STATUS_NO_MEMORY
);
1333 set_fd_events( ifchange
->fd
, POLLIN
); /* enable read wakeup on the file descriptor */
1335 /* the ifchange object is now successfully configured */
1336 ifchange_object
= &ifchange
->obj
;
1337 return &ifchange
->obj
;
1339 set_error( STATUS_NOT_SUPPORTED
);
1344 /* add the socket to the interface change notification list */
1345 static void ifchange_add_sock( struct object
*obj
, struct sock
*sock
)
1347 #ifdef HAVE_LINUX_RTNETLINK_H
1348 struct ifchange
*ifchange
= (struct ifchange
*)obj
;
1350 list_add_tail( &ifchange
->sockets
, &sock
->ifchange_entry
);
1354 /* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
1355 static struct object
*sock_get_ifchange( struct sock
*sock
)
1357 struct object
*ifchange
;
1359 if (sock
->ifchange_obj
) /* reuse existing ifchange_obj for this socket */
1360 return sock
->ifchange_obj
;
1362 if (!(ifchange
= get_ifchange()))
1365 /* add the socket to the ifchange notification list */
1366 ifchange_add_sock( ifchange
, sock
);
1367 sock
->ifchange_obj
= ifchange
;
1371 /* destroy an existing ifchange queue for a specific socket */
1372 static void sock_release_ifchange( struct sock
*sock
)
1374 if (sock
->ifchange_obj
)
1376 list_remove( &sock
->ifchange_entry
);
1377 release_object( sock
->ifchange_obj
);
1378 sock
->ifchange_obj
= NULL
;
1382 static struct object_type
*socket_device_get_type( struct object
*obj
);
1383 static void socket_device_dump( struct object
*obj
, int verbose
);
1384 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
);
1385 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
1386 unsigned int sharing
, unsigned int options
);
1388 static const struct object_ops socket_device_ops
=
1390 sizeof(struct object
), /* size */
1391 socket_device_dump
, /* dump */
1392 socket_device_get_type
, /* get_type */
1393 no_add_queue
, /* add_queue */
1394 NULL
, /* remove_queue */
1395 NULL
, /* signaled */
1396 no_satisfied
, /* satisfied */
1397 no_signal
, /* signal */
1398 no_get_fd
, /* get_fd */
1399 default_fd_map_access
, /* map_access */
1400 default_get_sd
, /* get_sd */
1401 default_set_sd
, /* set_sd */
1402 default_get_full_name
, /* get_full_name */
1403 socket_device_lookup_name
, /* lookup_name */
1404 directory_link_name
, /* link_name */
1405 default_unlink_name
, /* unlink_name */
1406 socket_device_open_file
, /* open_file */
1407 no_kernel_obj_list
, /* get_kernel_obj_list */
1408 no_close_handle
, /* close_handle */
1409 no_destroy
/* destroy */
1412 static struct object_type
*socket_device_get_type( struct object
*obj
)
1414 static const WCHAR name
[] = {'D','e','v','i','c','e'};
1415 static const struct unicode_str str
= { name
, sizeof(name
) };
1416 return get_object_type( &str
);
1419 static void socket_device_dump( struct object
*obj
, int verbose
)
1421 fputs( "Socket device\n", stderr
);
1424 static struct object
*socket_device_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
)
1429 static struct object
*socket_device_open_file( struct object
*obj
, unsigned int access
,
1430 unsigned int sharing
, unsigned int options
)
1434 if (!(sock
= create_socket())) return NULL
;
1435 if (!(sock
->fd
= alloc_pseudo_fd( &sock_fd_ops
, &sock
->obj
, options
)))
1437 release_object( sock
);
1443 struct object
*create_socket_device( struct object
*root
, const struct unicode_str
*name
,
1444 unsigned int attr
, const struct security_descriptor
*sd
)
1446 return create_named_object( root
, &socket_device_ops
, name
, attr
, sd
);
1449 /* set socket event parameters */
1450 DECL_HANDLER(set_socket_event
)
1453 struct event
*old_event
;
1455 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1456 FILE_WRITE_ATTRIBUTES
, &sock_ops
))) return;
1457 if (get_unix_fd( sock
->fd
) == -1) return;
1458 old_event
= sock
->event
;
1459 sock
->mask
= req
->mask
;
1460 sock
->hmask
&= ~req
->mask
; /* re-enable held events */
1462 sock
->window
= req
->window
;
1463 sock
->message
= req
->msg
;
1464 sock
->wparam
= req
->handle
; /* wparam is the socket handle */
1465 if (req
->event
) sock
->event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
1467 if (debug_level
&& sock
->event
) fprintf(stderr
, "event ptr: %p\n", sock
->event
);
1469 sock_reselect( sock
);
1471 sock
->state
|= FD_WINE_NONBLOCKING
;
1473 /* if a network event is pending, signal the event object
1474 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
1475 before a WSAEventSelect() was done on it.
1476 (when dealing with Asynchronous socket) */
1477 sock_wake_up( sock
);
1479 if (old_event
) release_object( old_event
); /* we're through with it */
1480 release_object( &sock
->obj
);
1483 /* get socket event parameters */
1484 DECL_HANDLER(get_socket_event
)
1488 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1489 FILE_READ_ATTRIBUTES
, &sock_ops
))) return;
1490 if (get_unix_fd( sock
->fd
) == -1) return;
1491 reply
->mask
= sock
->mask
;
1492 reply
->pmask
= sock
->pmask
;
1493 reply
->state
= sock
->state
;
1494 set_reply_data( sock
->errors
, min( get_reply_max_size(), sizeof(sock
->errors
) ));
1500 struct event
*cevent
= get_event_obj( current
->process
, req
->c_event
,
1501 EVENT_MODIFY_STATE
);
1504 reset_event( cevent
);
1505 release_object( cevent
);
1509 sock_reselect( sock
);
1511 release_object( &sock
->obj
);
1514 /* re-enable pending socket events */
1515 DECL_HANDLER(enable_socket_event
)
1519 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
1520 FILE_WRITE_ATTRIBUTES
, &sock_ops
)))
1523 if (get_unix_fd( sock
->fd
) == -1) return;
1525 /* for event-based notification, windows erases stale events */
1526 sock
->pmask
&= ~req
->mask
;
1528 sock
->hmask
&= ~req
->mask
;
1529 sock
->state
|= req
->sstate
;
1530 sock
->state
&= ~req
->cstate
;
1531 if (sock
->type
!= WS_SOCK_STREAM
) sock
->state
&= ~STREAM_FLAG_MASK
;
1533 sock_reselect( sock
);
1535 release_object( &sock
->obj
);
1538 DECL_HANDLER(set_socket_deferred
)
1540 struct sock
*sock
, *acceptsock
;
1542 sock
=(struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
, &sock_ops
);
1546 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, req
->deferred
, 0, &sock_ops
);
1549 release_object( sock
);
1552 sock
->deferred
= acceptsock
;
1553 release_object( sock
);
1556 DECL_HANDLER(get_socket_info
)
1560 sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
, &sock_ops
);
1563 if (get_unix_fd( sock
->fd
) == -1) return;
1565 reply
->family
= sock
->family
;
1566 reply
->type
= sock
->type
;
1567 reply
->protocol
= sock
->proto
;
1569 release_object( &sock
->obj
);