2 * based on Windows Sockets 1.1 specs
4 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
5 * Copyright (C) 2001 Stefan Leichter
6 * Copyright (C) 2004 Hans Leidekker
7 * Copyright (C) 2005 Marcus Meissner
8 * Copyright (C) 2006-2008 Kai Blin
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * NOTE: If you make any changes to fix a particular app, make sure
25 * they don't break something else like Netscape or telnet and ftp
26 * clients and servers (www.winsite.com got a lot of those).
30 #include "wine/port.h"
35 #include <sys/types.h>
40 #ifdef HAVE_SYS_IOCTL_H
41 # include <sys/ioctl.h>
43 #ifdef HAVE_SYS_FILIO_H
44 # include <sys/filio.h>
46 #ifdef HAVE_SYS_SOCKIO_H
47 # include <sys/sockio.h>
51 # include <sys/so_ioctl.h>
54 #ifdef HAVE_SYS_PARAM_H
55 # include <sys/param.h>
61 #ifdef HAVE_SYS_WAIT_H
62 # include <sys/wait.h>
67 #ifdef HAVE_SYS_SOCKET_H
68 #include <sys/socket.h>
70 #ifdef HAVE_NETINET_IN_H
71 # include <netinet/in.h>
73 #ifdef HAVE_NETINET_TCP_H
74 # include <netinet/tcp.h>
76 #ifdef HAVE_ARPA_INET_H
77 # include <arpa/inet.h>
89 #ifdef HAVE_ARPA_NAMESER_H
90 # include <arpa/nameser.h>
98 #ifdef HAVE_LINUX_FILTER_H
99 # include <linux/filter.h>
102 #ifdef HAVE_NETIPX_IPX_H
103 # include <netipx/ipx.h>
104 #elif defined(HAVE_LINUX_IPX_H)
105 # ifdef HAVE_ASM_TYPES_H
106 # include <asm/types.h>
108 # ifdef HAVE_LINUX_TYPES_H
109 # include <linux/types.h>
111 # include <linux/ipx.h>
113 #if defined(SOL_IPX) || defined(SO_DEFAULT_HEADERS)
117 #ifdef HAVE_LINUX_IRDA_H
118 # ifdef HAVE_LINUX_TYPES_H
119 # include <linux/types.h>
121 # include <linux/irda.h>
128 #ifdef HAVE_SYS_POLL_H
129 # include <sys/poll.h>
131 #ifdef HAVE_SYS_TIME_H
132 # include <sys/time.h>
135 #define NONAMELESSUNION
136 #define NONAMELESSSTRUCT
137 #include "ntstatus.h"
138 #define WIN32_NO_STATUS
143 #include "winerror.h"
145 #include "winsock2.h"
147 #include "ws2tcpip.h"
150 #include "wsnwlink.h"
151 #include "wshisotp.h"
155 #define USE_WC_PREFIX /* For CMSG_DATA */
156 #include "iphlpapi.h"
157 #include "ip2string.h"
158 #include "wine/server.h"
159 #include "wine/debug.h"
160 #include "wine/exception.h"
161 #include "wine/unicode.h"
162 #include "wine/heap.h"
164 #if defined(linux) && !defined(IP_UNICAST_IF)
165 #define IP_UNICAST_IF 50
168 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
169 # define sipx_network sipx_addr.x_net
170 # define sipx_node sipx_addr.x_host.c_host
171 #endif /* __FreeBSD__ */
174 #define INADDR_NONE ~0UL
177 #if !defined(TCP_KEEPIDLE) && defined(TCP_KEEPALIVE)
178 /* TCP_KEEPALIVE is the Mac OS name for TCP_KEEPIDLE */
179 #define TCP_KEEPIDLE TCP_KEEPALIVE
182 #define FILE_USE_FILE_POINTER_POSITION ((LONGLONG)-2)
184 WINE_DEFAULT_DEBUG_CHANNEL(winsock
);
185 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
187 /* names of the protocols */
188 static const WCHAR NameIpxW
[] = {'I', 'P', 'X', '\0'};
189 static const WCHAR NameSpxW
[] = {'S', 'P', 'X', '\0'};
190 static const WCHAR NameSpxIIW
[] = {'S', 'P', 'X', ' ', 'I', 'I', '\0'};
191 static const WCHAR NameTcpW
[] = {'T', 'C', 'P', '/', 'I', 'P', '\0'};
192 static const WCHAR NameUdpW
[] = {'U', 'D', 'P', '/', 'I', 'P', '\0'};
194 /* Taken from Win2k */
195 static const GUID ProviderIdIP
= { 0xe70f1aa0, 0xab8b, 0x11cf,
196 { 0x8c, 0xa3, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } };
197 static const GUID ProviderIdIPX
= { 0x11058240, 0xbe47, 0x11cf,
198 { 0x95, 0xc8, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } };
199 static const GUID ProviderIdSPX
= { 0x11058241, 0xbe47, 0x11cf,
200 { 0x95, 0xc8, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } };
202 static const INT valid_protocols
[] =
212 #define IS_IPX_PROTO(X) ((X) >= WS_NSPROTO_IPX && (X) <= WS_NSPROTO_IPX + 255)
214 #if defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER)
215 # define LINUX_BOUND_IF
216 struct interface_filter
{
217 struct sock_filter iface_memaddr
;
218 struct sock_filter iface_rule
;
219 struct sock_filter ip_memaddr
;
220 struct sock_filter ip_rule
;
221 struct sock_filter return_keep
;
222 struct sock_filter return_dump
;
224 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
225 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
226 /sizeof(struct sock_filter)
227 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
228 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
229 /sizeof(struct sock_filter)
230 # define FILTER_JUMP_NEXT() (u_char)(0)
231 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
232 static struct interface_filter generic_interface_filter
= {
233 /* This filter rule allows incoming packets on the specified interface, which works for all
234 * remotely generated packets and for locally generated broadcast packets. */
235 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
236 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
237 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
238 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
239 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
240 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
241 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
242 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
244 #endif /* LINUX_BOUND_IF */
246 extern ssize_t CDECL
__wine_locked_recvmsg( int fd
, struct msghdr
*hdr
, int flags
);
249 * The actual definition of WSASendTo, wrapped in a different function name
250 * so that internal calls from ws2_32 itself will not trigger programs like
251 * Garena, which hooks WSASendTo/WSARecvFrom calls.
253 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
254 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
255 const struct WS_sockaddr
*to
, int tolen
,
256 LPWSAOVERLAPPED lpOverlapped
,
257 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
260 * Internal fundamental receive function, essentially WSARecvFrom with an
261 * additional parameter to support message control headers.
263 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
264 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
265 struct WS_sockaddr
*lpFrom
,
266 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
267 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
268 LPWSABUF lpControlBuffer
);
270 /* critical section to protect some non-reentrant net function */
271 static CRITICAL_SECTION csWSgetXXXbyYYY
;
272 static CRITICAL_SECTION_DEBUG critsect_debug
=
274 0, 0, &csWSgetXXXbyYYY
,
275 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
276 0, 0, { (DWORD_PTR
)(__FILE__
": csWSgetXXXbyYYY") }
278 static CRITICAL_SECTION csWSgetXXXbyYYY
= { &critsect_debug
, -1, 0, 0, 0, 0 };
280 static in_addr_t
*if_addr_cache
;
281 static unsigned int if_addr_cache_size
;
282 static CRITICAL_SECTION cs_if_addr_cache
;
283 static CRITICAL_SECTION_DEBUG cs_if_addr_cache_debug
=
285 0, 0, &cs_if_addr_cache
,
286 { &cs_if_addr_cache_debug
.ProcessLocksList
, &cs_if_addr_cache_debug
.ProcessLocksList
},
287 0, 0, { (DWORD_PTR
)(__FILE__
": cs_if_addr_cache") }
289 static CRITICAL_SECTION cs_if_addr_cache
= { &cs_if_addr_cache_debug
, -1, 0, 0, 0, 0 };
291 union generic_unix_sockaddr
293 struct sockaddr addr
;
294 char data
[128]; /* should be big enough for all families */
297 static inline const char *debugstr_sockaddr( const struct WS_sockaddr
*a
)
299 if (!a
) return "(nil)";
300 switch (a
->sa_family
)
306 struct WS_sockaddr_in
*sin
= (struct WS_sockaddr_in
*)a
;
308 p
= WS_inet_ntop( WS_AF_INET
, &sin
->sin_addr
, buf
, sizeof(buf
) );
310 p
= "(unknown IPv4 address)";
312 return wine_dbg_sprintf("{ family AF_INET, address %s, port %d }",
313 p
, ntohs(sin
->sin_port
));
319 struct WS_sockaddr_in6
*sin
= (struct WS_sockaddr_in6
*)a
;
321 p
= WS_inet_ntop( WS_AF_INET6
, &sin
->sin6_addr
, buf
, sizeof(buf
) );
323 p
= "(unknown IPv6 address)";
324 return wine_dbg_sprintf("{ family AF_INET6, address %s, port %d }",
325 p
, ntohs(sin
->sin6_port
));
330 char netnum
[16], nodenum
[16];
331 struct WS_sockaddr_ipx
*sin
= (struct WS_sockaddr_ipx
*)a
;
333 for (i
= 0;i
< 4; i
++) sprintf(netnum
+ i
* 2, "%02X", (unsigned char) sin
->sa_netnum
[i
]);
334 for (i
= 0;i
< 6; i
++) sprintf(nodenum
+ i
* 2, "%02X", (unsigned char) sin
->sa_nodenum
[i
]);
336 return wine_dbg_sprintf("{ family AF_IPX, address %s.%s, ipx socket %d }",
337 netnum
, nodenum
, sin
->sa_socket
);
343 memcpy( &addr
, ((const SOCKADDR_IRDA
*)a
)->irdaDeviceID
, sizeof(addr
) );
344 addr
= ntohl( addr
);
345 return wine_dbg_sprintf("{ family AF_IRDA, addr %08x, name %s }",
347 ((const SOCKADDR_IRDA
*)a
)->irdaServiceName
);
350 return wine_dbg_sprintf("{ family %d }", a
->sa_family
);
354 static inline const char *debugstr_sockopt(int level
, int optname
)
356 const char *stropt
= NULL
, *strlevel
= NULL
;
358 #define DEBUG_SOCKLEVEL(x) case (x): strlevel = #x
359 #define DEBUG_SOCKOPT(x) case (x): stropt = #x; break
363 DEBUG_SOCKLEVEL(WS_SOL_SOCKET
);
366 DEBUG_SOCKOPT(WS_SO_ACCEPTCONN
);
367 DEBUG_SOCKOPT(WS_SO_BROADCAST
);
368 DEBUG_SOCKOPT(WS_SO_BSP_STATE
);
369 DEBUG_SOCKOPT(WS_SO_CONDITIONAL_ACCEPT
);
370 DEBUG_SOCKOPT(WS_SO_CONNECT_TIME
);
371 DEBUG_SOCKOPT(WS_SO_DEBUG
);
372 DEBUG_SOCKOPT(WS_SO_DONTLINGER
);
373 DEBUG_SOCKOPT(WS_SO_DONTROUTE
);
374 DEBUG_SOCKOPT(WS_SO_ERROR
);
375 DEBUG_SOCKOPT(WS_SO_EXCLUSIVEADDRUSE
);
376 DEBUG_SOCKOPT(WS_SO_GROUP_ID
);
377 DEBUG_SOCKOPT(WS_SO_GROUP_PRIORITY
);
378 DEBUG_SOCKOPT(WS_SO_KEEPALIVE
);
379 DEBUG_SOCKOPT(WS_SO_LINGER
);
380 DEBUG_SOCKOPT(WS_SO_MAX_MSG_SIZE
);
381 DEBUG_SOCKOPT(WS_SO_OOBINLINE
);
382 DEBUG_SOCKOPT(WS_SO_OPENTYPE
);
383 DEBUG_SOCKOPT(WS_SO_PROTOCOL_INFOA
);
384 DEBUG_SOCKOPT(WS_SO_PROTOCOL_INFOW
);
385 DEBUG_SOCKOPT(WS_SO_RCVBUF
);
386 DEBUG_SOCKOPT(WS_SO_RCVTIMEO
);
387 DEBUG_SOCKOPT(WS_SO_REUSEADDR
);
388 DEBUG_SOCKOPT(WS_SO_SNDBUF
);
389 DEBUG_SOCKOPT(WS_SO_SNDTIMEO
);
390 DEBUG_SOCKOPT(WS_SO_TYPE
);
391 DEBUG_SOCKOPT(WS_SO_UPDATE_CONNECT_CONTEXT
);
395 DEBUG_SOCKLEVEL(WS_NSPROTO_IPX
);
398 DEBUG_SOCKOPT(WS_IPX_PTYPE
);
399 DEBUG_SOCKOPT(WS_IPX_FILTERPTYPE
);
400 DEBUG_SOCKOPT(WS_IPX_DSTYPE
);
401 DEBUG_SOCKOPT(WS_IPX_RECVHDR
);
402 DEBUG_SOCKOPT(WS_IPX_MAXSIZE
);
403 DEBUG_SOCKOPT(WS_IPX_ADDRESS
);
404 DEBUG_SOCKOPT(WS_IPX_MAX_ADAPTER_NUM
);
408 DEBUG_SOCKLEVEL(WS_SOL_IRLMP
);
411 DEBUG_SOCKOPT(WS_IRLMP_ENUMDEVICES
);
415 DEBUG_SOCKLEVEL(WS_IPPROTO_TCP
);
418 DEBUG_SOCKOPT(WS_TCP_BSDURGENT
);
419 DEBUG_SOCKOPT(WS_TCP_EXPEDITED_1122
);
420 DEBUG_SOCKOPT(WS_TCP_NODELAY
);
424 DEBUG_SOCKLEVEL(WS_IPPROTO_IP
);
427 DEBUG_SOCKOPT(WS_IP_ADD_MEMBERSHIP
);
428 DEBUG_SOCKOPT(WS_IP_DONTFRAGMENT
);
429 DEBUG_SOCKOPT(WS_IP_DROP_MEMBERSHIP
);
430 DEBUG_SOCKOPT(WS_IP_HDRINCL
);
431 DEBUG_SOCKOPT(WS_IP_MULTICAST_IF
);
432 DEBUG_SOCKOPT(WS_IP_MULTICAST_LOOP
);
433 DEBUG_SOCKOPT(WS_IP_MULTICAST_TTL
);
434 DEBUG_SOCKOPT(WS_IP_OPTIONS
);
435 DEBUG_SOCKOPT(WS_IP_PKTINFO
);
436 DEBUG_SOCKOPT(WS_IP_RECEIVE_BROADCAST
);
437 DEBUG_SOCKOPT(WS_IP_TOS
);
438 DEBUG_SOCKOPT(WS_IP_TTL
);
439 DEBUG_SOCKOPT(WS_IP_UNICAST_IF
);
443 DEBUG_SOCKLEVEL(WS_IPPROTO_IPV6
);
446 DEBUG_SOCKOPT(WS_IPV6_ADD_MEMBERSHIP
);
447 DEBUG_SOCKOPT(WS_IPV6_DROP_MEMBERSHIP
);
448 DEBUG_SOCKOPT(WS_IPV6_MULTICAST_IF
);
449 DEBUG_SOCKOPT(WS_IPV6_MULTICAST_HOPS
);
450 DEBUG_SOCKOPT(WS_IPV6_MULTICAST_LOOP
);
451 DEBUG_SOCKOPT(WS_IPV6_UNICAST_HOPS
);
452 DEBUG_SOCKOPT(WS_IPV6_V6ONLY
);
453 DEBUG_SOCKOPT(WS_IPV6_UNICAST_IF
);
454 DEBUG_SOCKOPT(WS_IPV6_DONTFRAG
);
458 #undef DEBUG_SOCKLEVEL
462 strlevel
= wine_dbg_sprintf("WS_0x%x", level
);
464 stropt
= wine_dbg_sprintf("WS_0x%x", optname
);
466 return wine_dbg_sprintf("level %s, name %s", strlevel
+ 3, stropt
+ 3);
469 static inline const char *debugstr_optval(const char *optval
, int optlenval
)
471 if (optval
&& !IS_INTRESOURCE(optval
) && optlenval
>= 1 && optlenval
<= sizeof(DWORD
))
474 memcpy(&value
, optval
, optlenval
);
475 return wine_dbg_sprintf("%p (%u)", optval
, value
);
477 return wine_dbg_sprintf("%p", optval
);
480 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
481 #define SOCKET2HANDLE(s) ((HANDLE)(s))
482 #define HANDLE2SOCKET(h) ((SOCKET)(h))
484 /****************************************************************
485 * Async IO declarations
486 ****************************************************************/
488 typedef NTSTATUS
async_callback_t( void *user
, IO_STATUS_BLOCK
*io
, NTSTATUS status
);
492 async_callback_t
*callback
; /* must be the first field */
493 struct ws2_async_io
*next
;
496 struct ws2_async_shutdown
498 struct ws2_async_io io
;
500 IO_STATUS_BLOCK iosb
;
506 struct ws2_async_io io
;
508 LPWSAOVERLAPPED user_overlapped
;
509 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func
;
510 IO_STATUS_BLOCK local_iosb
;
511 struct WS_sockaddr
*addr
;
514 int val
; /* for send operations */
515 int *ptr
; /* for recv operations */
520 unsigned int n_iovecs
;
521 unsigned int first_iovec
;
522 struct iovec iovec
[1];
525 struct ws2_accept_async
527 struct ws2_async_io io
;
528 HANDLE listen_socket
;
529 HANDLE accept_socket
;
530 LPOVERLAPPED user_overlapped
;
532 PVOID buf
; /* buffer to write data to */
536 struct ws2_async
*read
;
539 struct ws2_transmitfile_async
541 struct ws2_async_io io
;
546 DWORD bytes_per_send
;
547 TRANSMIT_FILE_BUFFERS buffers
;
549 LARGE_INTEGER offset
;
550 struct ws2_async write
;
553 static struct ws2_async_io
*async_io_freelist
;
555 static void release_async_io( struct ws2_async_io
*io
)
559 struct ws2_async_io
*next
= async_io_freelist
;
561 if (InterlockedCompareExchangePointer( (void **)&async_io_freelist
, io
, next
) == next
) return;
565 static struct ws2_async_io
*alloc_async_io( DWORD size
, async_callback_t callback
)
567 /* first free remaining previous fileinfos */
569 struct ws2_async_io
*io
= InterlockedExchangePointer( (void **)&async_io_freelist
, NULL
);
573 struct ws2_async_io
*next
= io
->next
;
574 HeapFree( GetProcessHeap(), 0, io
);
578 io
= HeapAlloc( GetProcessHeap(), 0, size
);
579 if (io
) io
->callback
= callback
;
583 static NTSTATUS
register_async( int type
, HANDLE handle
, struct ws2_async_io
*async
, HANDLE event
,
584 PIO_APC_ROUTINE apc
, void *apc_context
, IO_STATUS_BLOCK
*io
)
588 SERVER_START_REQ( register_async
)
591 req
->async
.handle
= wine_server_obj_handle( handle
);
592 req
->async
.user
= wine_server_client_ptr( async
);
593 req
->async
.iosb
= wine_server_client_ptr( io
);
594 req
->async
.event
= wine_server_obj_handle( event
);
595 req
->async
.apc
= wine_server_client_ptr( apc
);
596 req
->async
.apc_context
= wine_server_client_ptr( apc_context
);
597 status
= wine_server_call( req
);
604 /****************************************************************/
606 /* ----------------------------------- internal data */
608 /* ws_... struct conversion flags */
610 typedef struct /* WSAAsyncSelect() control struct */
612 HANDLE service
, event
, sock
;
618 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
619 #define WS_MAX_UDP_DATAGRAM 1024
620 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
622 /* hostent's, servent's and protent's are stored in one buffer per thread,
623 * as documented on MSDN for the functions that return any of the buffers */
624 struct per_thread_data
627 struct WS_hostent
*he_buffer
;
628 struct WS_servent
*se_buffer
;
629 struct WS_protoent
*pe_buffer
;
630 struct pollfd
*fd_cache
;
631 unsigned int fd_count
;
635 char ntoa_buffer
[16]; /* 4*3 digits + 3 '.' + 1 '\0' */
638 /* internal: routing description information */
642 DWORD metric
, default_route
;
645 static INT num_startup
; /* reference counter */
646 static FARPROC blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
648 /* function prototypes */
649 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int aliases_size
, int addresses
, int address_length
);
650 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
651 static struct WS_protoent
*WS_create_pe( const char *name
, char **aliases
, int prot
);
652 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
653 static int ws_protocol_info(SOCKET s
, int unicode
, WSAPROTOCOL_INFOW
*buffer
, int *size
);
655 int WSAIOCTL_GetInterfaceCount(void);
656 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
658 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
, ULONG Information
, BOOL force
);
660 #define MAP_OPTION(opt) { WS_##opt, opt }
662 static const int ws_flags_map
[][2] =
664 MAP_OPTION( MSG_OOB
),
665 MAP_OPTION( MSG_PEEK
),
666 MAP_OPTION( MSG_DONTROUTE
),
667 MAP_OPTION( MSG_WAITALL
),
668 { WS_MSG_PARTIAL
, 0 },
671 static const int ws_sock_map
[][2] =
673 MAP_OPTION( SO_DEBUG
),
674 MAP_OPTION( SO_ACCEPTCONN
),
675 MAP_OPTION( SO_REUSEADDR
),
676 MAP_OPTION( SO_KEEPALIVE
),
677 MAP_OPTION( SO_DONTROUTE
),
678 MAP_OPTION( SO_BROADCAST
),
679 MAP_OPTION( SO_LINGER
),
680 MAP_OPTION( SO_OOBINLINE
),
681 MAP_OPTION( SO_SNDBUF
),
682 MAP_OPTION( SO_RCVBUF
),
683 MAP_OPTION( SO_ERROR
),
684 MAP_OPTION( SO_TYPE
),
686 MAP_OPTION( SO_RCVTIMEO
),
689 MAP_OPTION( SO_SNDTIMEO
),
693 static const int ws_tcp_map
[][2] =
696 MAP_OPTION( TCP_NODELAY
),
700 static const int ws_ip_map
[][2] =
702 MAP_OPTION( IP_MULTICAST_IF
),
703 MAP_OPTION( IP_MULTICAST_TTL
),
704 MAP_OPTION( IP_MULTICAST_LOOP
),
705 MAP_OPTION( IP_ADD_MEMBERSHIP
),
706 MAP_OPTION( IP_DROP_MEMBERSHIP
),
707 MAP_OPTION( IP_ADD_SOURCE_MEMBERSHIP
),
708 MAP_OPTION( IP_DROP_SOURCE_MEMBERSHIP
),
709 MAP_OPTION( IP_BLOCK_SOURCE
),
710 MAP_OPTION( IP_UNBLOCK_SOURCE
),
711 MAP_OPTION( IP_OPTIONS
),
713 MAP_OPTION( IP_HDRINCL
),
715 MAP_OPTION( IP_TOS
),
716 MAP_OPTION( IP_TTL
),
717 #if defined(IP_PKTINFO)
718 MAP_OPTION( IP_PKTINFO
),
719 #elif defined(IP_RECVDSTADDR)
720 { WS_IP_PKTINFO
, IP_RECVDSTADDR
},
723 MAP_OPTION( IP_UNICAST_IF
),
727 static const int ws_ipv6_map
[][2] =
729 #ifdef IPV6_ADD_MEMBERSHIP
730 MAP_OPTION( IPV6_ADD_MEMBERSHIP
),
732 #ifdef IPV6_DROP_MEMBERSHIP
733 MAP_OPTION( IPV6_DROP_MEMBERSHIP
),
735 MAP_OPTION( IPV6_MULTICAST_IF
),
736 MAP_OPTION( IPV6_MULTICAST_HOPS
),
737 MAP_OPTION( IPV6_MULTICAST_LOOP
),
738 MAP_OPTION( IPV6_UNICAST_HOPS
),
739 MAP_OPTION( IPV6_V6ONLY
),
740 #ifdef IPV6_UNICAST_IF
741 MAP_OPTION( IPV6_UNICAST_IF
),
745 static const int ws_af_map
[][2] =
747 MAP_OPTION( AF_UNSPEC
),
748 MAP_OPTION( AF_INET
),
749 MAP_OPTION( AF_INET6
),
751 MAP_OPTION( AF_IPX
),
754 MAP_OPTION( AF_IRDA
),
756 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
759 static const int ws_socktype_map
[][2] =
761 MAP_OPTION( SOCK_DGRAM
),
762 MAP_OPTION( SOCK_STREAM
),
763 MAP_OPTION( SOCK_RAW
),
764 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
767 static const int ws_proto_map
[][2] =
769 MAP_OPTION( IPPROTO_IP
),
770 MAP_OPTION( IPPROTO_TCP
),
771 MAP_OPTION( IPPROTO_UDP
),
772 MAP_OPTION( IPPROTO_IPV6
),
773 MAP_OPTION( IPPROTO_ICMP
),
774 MAP_OPTION( IPPROTO_IGMP
),
775 MAP_OPTION( IPPROTO_RAW
),
776 MAP_OPTION( IPPROTO_IPIP
),
777 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
780 static const int ws_aiflag_map
[][2] =
782 MAP_OPTION( AI_PASSIVE
),
783 MAP_OPTION( AI_CANONNAME
),
784 MAP_OPTION( AI_NUMERICHOST
),
785 #ifdef AI_NUMERICSERV
786 MAP_OPTION( AI_NUMERICSERV
),
789 MAP_OPTION( AI_V4MAPPED
),
791 MAP_OPTION( AI_ADDRCONFIG
),
794 static const int ws_niflag_map
[][2] =
796 MAP_OPTION( NI_NOFQDN
),
797 MAP_OPTION( NI_NUMERICHOST
),
798 MAP_OPTION( NI_NAMEREQD
),
799 MAP_OPTION( NI_NUMERICSERV
),
800 MAP_OPTION( NI_DGRAM
),
803 static const int ws_eai_map
[][2] =
805 MAP_OPTION( EAI_AGAIN
),
806 MAP_OPTION( EAI_BADFLAGS
),
807 MAP_OPTION( EAI_FAIL
),
808 MAP_OPTION( EAI_FAMILY
),
809 MAP_OPTION( EAI_MEMORY
),
810 /* Note: EAI_NODATA is deprecated, but still
811 * used by Windows and Linux... We map the newer
812 * EAI_NONAME to EAI_NODATA for now until Windows
816 MAP_OPTION( EAI_NODATA
),
819 { WS_EAI_NODATA
, EAI_NONAME
},
822 MAP_OPTION( EAI_SERVICE
),
823 MAP_OPTION( EAI_SOCKTYPE
),
827 static const int ws_poll_map
[][2] =
829 MAP_OPTION( POLLERR
),
830 MAP_OPTION( POLLHUP
),
831 MAP_OPTION( POLLNVAL
),
832 MAP_OPTION( POLLWRNORM
),
833 MAP_OPTION( POLLWRBAND
),
834 MAP_OPTION( POLLRDNORM
),
835 { WS_POLLRDBAND
, POLLPRI
}
838 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
840 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
841 #if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
842 static inline WSACMSGHDR
*fill_control_message(int level
, int type
, WSACMSGHDR
*current
, ULONG
*maxsize
, void *data
, int len
)
844 ULONG msgsize
= sizeof(WSACMSGHDR
) + WSA_CMSG_ALIGN(len
);
845 char *ptr
= (char *) current
+ sizeof(WSACMSGHDR
);
847 /* Make sure there is at least enough room for this entry */
848 if (msgsize
> *maxsize
)
851 /* Fill in the entry */
852 current
->cmsg_len
= sizeof(WSACMSGHDR
) + len
;
853 current
->cmsg_level
= level
;
854 current
->cmsg_type
= type
;
855 memcpy(ptr
, data
, len
);
856 /* Return the pointer to where next entry should go */
857 return (WSACMSGHDR
*) (ptr
+ WSA_CMSG_ALIGN(len
));
859 #endif /* defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) */
861 static inline int convert_control_headers(struct msghdr
*hdr
, WSABUF
*control
)
863 #if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
864 WSACMSGHDR
*cmsg_win
= (WSACMSGHDR
*) control
->buf
, *ptr
;
865 ULONG ctlsize
= control
->len
;
866 struct cmsghdr
*cmsg_unix
;
869 /* Loop over all the headers, converting as appropriate */
870 for (cmsg_unix
= CMSG_FIRSTHDR(hdr
); cmsg_unix
!= NULL
; cmsg_unix
= CMSG_NXTHDR(hdr
, cmsg_unix
))
872 switch(cmsg_unix
->cmsg_level
)
875 switch(cmsg_unix
->cmsg_type
)
877 #if defined(IP_PKTINFO)
880 /* Convert the Unix IP_PKTINFO structure to the Windows version */
881 struct in_pktinfo
*data_unix
= (struct in_pktinfo
*) CMSG_DATA(cmsg_unix
);
882 struct WS_in_pktinfo data_win
;
884 memcpy(&data_win
.ipi_addr
,&data_unix
->ipi_addr
.s_addr
,4); /* 4 bytes = 32 address bits */
885 data_win
.ipi_ifindex
= data_unix
->ipi_ifindex
;
886 ptr
= fill_control_message(WS_IPPROTO_IP
, WS_IP_PKTINFO
, ptr
, &ctlsize
,
887 (void*)&data_win
, sizeof(data_win
));
888 if (!ptr
) goto error
;
890 #elif defined(IP_RECVDSTADDR)
893 struct in_addr
*addr_unix
= (struct in_addr
*) CMSG_DATA(cmsg_unix
);
894 struct WS_in_pktinfo data_win
;
896 memcpy(&data_win
.ipi_addr
, &addr_unix
->s_addr
, 4); /* 4 bytes = 32 address bits */
897 data_win
.ipi_ifindex
= 0; /* FIXME */
898 ptr
= fill_control_message(WS_IPPROTO_IP
, WS_IP_PKTINFO
, ptr
, &ctlsize
,
899 (void*)&data_win
, sizeof(data_win
));
900 if (!ptr
) goto error
;
902 #endif /* IP_PKTINFO */
904 FIXME("Unhandled IPPROTO_IP message header type %d\n", cmsg_unix
->cmsg_type
);
909 FIXME("Unhandled message header level %d\n", cmsg_unix
->cmsg_level
);
914 /* Set the length of the returned control headers */
915 control
->len
= (char*)ptr
- (char*)cmsg_win
;
920 #else /* defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) */
923 #endif /* defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) */
925 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
927 /* ----------------------------------- error handling */
929 static NTSTATUS
sock_get_ntstatus( int err
)
933 case EBADF
: return STATUS_INVALID_HANDLE
;
934 case EBUSY
: return STATUS_DEVICE_BUSY
;
936 case EACCES
: return STATUS_ACCESS_DENIED
;
937 case EFAULT
: return STATUS_NO_MEMORY
;
938 case EINVAL
: return STATUS_INVALID_PARAMETER
;
940 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
941 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
942 case EINPROGRESS
: return STATUS_PENDING
;
943 case EALREADY
: return STATUS_NETWORK_BUSY
;
944 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
945 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
946 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
947 case EPROTONOSUPPORT
:
948 case ESOCKTNOSUPPORT
:
951 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
952 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
953 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
954 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
955 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
956 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
957 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
958 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
959 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
960 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
961 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
963 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
964 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
966 case 0: return STATUS_SUCCESS
;
968 WARN("Unknown errno %d!\n", err
);
969 return STATUS_UNSUCCESSFUL
;
973 static UINT
sock_get_error( int err
)
977 case EINTR
: return WSAEINTR
;
979 case EACCES
: return WSAEACCES
;
980 case EFAULT
: return WSAEFAULT
;
981 case EINVAL
: return WSAEINVAL
;
982 case EMFILE
: return WSAEMFILE
;
983 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
984 case EINPROGRESS
: return WSAEINPROGRESS
;
985 case EALREADY
: return WSAEALREADY
;
987 case ENOTSOCK
: return WSAENOTSOCK
;
988 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
989 case EMSGSIZE
: return WSAEMSGSIZE
;
990 case EPROTOTYPE
: return WSAEPROTOTYPE
;
991 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
992 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
993 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
994 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
995 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
996 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
997 case EADDRINUSE
: return WSAEADDRINUSE
;
998 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
999 case ENETDOWN
: return WSAENETDOWN
;
1000 case ENETUNREACH
: return WSAENETUNREACH
;
1001 case ENETRESET
: return WSAENETRESET
;
1002 case ECONNABORTED
: return WSAECONNABORTED
;
1004 case ECONNRESET
: return WSAECONNRESET
;
1005 case ENOBUFS
: return WSAENOBUFS
;
1006 case EISCONN
: return WSAEISCONN
;
1007 case ENOTCONN
: return WSAENOTCONN
;
1008 case ESHUTDOWN
: return WSAESHUTDOWN
;
1009 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
1010 case ETIMEDOUT
: return WSAETIMEDOUT
;
1011 case ECONNREFUSED
: return WSAECONNREFUSED
;
1012 case ELOOP
: return WSAELOOP
;
1013 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
1014 case EHOSTDOWN
: return WSAEHOSTDOWN
;
1015 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
1016 case ENOTEMPTY
: return WSAENOTEMPTY
;
1018 case EPROCLIM
: return WSAEPROCLIM
;
1021 case EUSERS
: return WSAEUSERS
;
1024 case EDQUOT
: return WSAEDQUOT
;
1027 case ESTALE
: return WSAESTALE
;
1030 case EREMOTE
: return WSAEREMOTE
;
1033 /* just in case we ever get here and there are no problems */
1036 WARN("Unknown errno %d!\n", err
);
1037 return WSAEOPNOTSUPP
;
1041 static UINT
wsaErrno(void)
1043 int loc_errno
= errno
;
1044 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
1046 return sock_get_error( loc_errno
);
1049 /* most ws2 overlapped functions return an ntstatus-based error code */
1050 static NTSTATUS
wsaErrStatus(void)
1052 int loc_errno
= errno
;
1053 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
1055 return sock_get_ntstatus(loc_errno
);
1058 static UINT
wsaHerrno(int loc_errno
)
1060 WARN("h_errno %d.\n", loc_errno
);
1064 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
1065 case TRY_AGAIN
: return WSATRY_AGAIN
;
1066 case NO_RECOVERY
: return WSANO_RECOVERY
;
1067 case NO_DATA
: return WSANO_DATA
;
1068 case ENOBUFS
: return WSAENOBUFS
;
1072 WARN("Unknown h_errno %d!\n", loc_errno
);
1073 return WSAEOPNOTSUPP
;
1077 static NTSTATUS
sock_error_to_ntstatus( DWORD err
)
1081 case 0: return STATUS_SUCCESS
;
1082 case WSAEBADF
: return STATUS_INVALID_HANDLE
;
1083 case WSAEACCES
: return STATUS_ACCESS_DENIED
;
1084 case WSAEFAULT
: return STATUS_NO_MEMORY
;
1085 case WSAEINVAL
: return STATUS_INVALID_PARAMETER
;
1086 case WSAEMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
1087 case WSAEWOULDBLOCK
: return STATUS_CANT_WAIT
;
1088 case WSAEINPROGRESS
: return STATUS_PENDING
;
1089 case WSAEALREADY
: return STATUS_NETWORK_BUSY
;
1090 case WSAENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
1091 case WSAEDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
1092 case WSAEMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
1093 case WSAEPROTONOSUPPORT
:
1094 case WSAESOCKTNOSUPPORT
:
1095 case WSAEPFNOSUPPORT
:
1096 case WSAEAFNOSUPPORT
:
1097 case WSAEPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
1098 case WSAENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
1099 case WSAEOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
1100 case WSAEADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
1101 case WSAEADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
1102 case WSAECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
1103 case WSAESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
1104 case WSAENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
1105 case WSAETIMEDOUT
: return STATUS_IO_TIMEOUT
;
1106 case WSAENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
1107 case WSAENETDOWN
: return STATUS_NETWORK_BUSY
;
1108 case WSAECONNRESET
: return STATUS_CONNECTION_RESET
;
1109 case WSAECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
1111 FIXME("unmapped error %u\n", err
);
1112 return STATUS_UNSUCCESSFUL
;
1116 static DWORD
NtStatusToWSAError( DWORD status
)
1120 case STATUS_SUCCESS
: return 0;
1121 case STATUS_PENDING
: return WSA_IO_PENDING
;
1122 case STATUS_INVALID_HANDLE
:
1123 case STATUS_OBJECT_TYPE_MISMATCH
: return WSAENOTSOCK
;
1124 case STATUS_INVALID_PARAMETER
: return WSAEINVAL
;
1125 case STATUS_PIPE_DISCONNECTED
: return WSAESHUTDOWN
;
1126 case STATUS_NETWORK_BUSY
: return WSAEALREADY
;
1127 case STATUS_NETWORK_UNREACHABLE
: return WSAENETUNREACH
;
1128 case STATUS_CONNECTION_REFUSED
: return WSAECONNREFUSED
;
1129 case STATUS_CONNECTION_DISCONNECTED
: return WSAENOTCONN
;
1130 case STATUS_CONNECTION_RESET
: return WSAECONNRESET
;
1131 case STATUS_CONNECTION_ABORTED
: return WSAECONNABORTED
;
1132 case STATUS_CANCELLED
: return WSA_OPERATION_ABORTED
;
1133 case STATUS_ADDRESS_ALREADY_ASSOCIATED
: return WSAEADDRINUSE
;
1134 case STATUS_IO_TIMEOUT
:
1135 case STATUS_TIMEOUT
: return WSAETIMEDOUT
;
1136 case STATUS_NO_MEMORY
: return WSAEFAULT
;
1137 case STATUS_ACCESS_DENIED
: return WSAEACCES
;
1138 case STATUS_TOO_MANY_OPENED_FILES
: return WSAEMFILE
;
1139 case STATUS_CANT_WAIT
: return WSAEWOULDBLOCK
;
1140 case STATUS_BUFFER_OVERFLOW
: return WSAEMSGSIZE
;
1141 case STATUS_NOT_SUPPORTED
: return WSAEOPNOTSUPP
;
1142 case STATUS_HOST_UNREACHABLE
: return WSAEHOSTUNREACH
;
1143 default: return RtlNtStatusToDosError( status
);
1147 /* set last error code from NT status without mapping WSA errors */
1148 static inline unsigned int set_error( unsigned int err
)
1152 err
= NtStatusToWSAError( err
);
1153 SetLastError( err
);
1158 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
1161 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
1166 static inline void release_sock_fd( SOCKET s
, int fd
)
1168 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
1171 static void _enable_event( HANDLE s
, unsigned int event
,
1172 unsigned int sstate
, unsigned int cstate
)
1174 SERVER_START_REQ( enable_socket_event
)
1176 req
->handle
= wine_server_obj_handle( s
);
1178 req
->sstate
= sstate
;
1179 req
->cstate
= cstate
;
1180 wine_server_call( req
);
1185 static DWORD
sock_is_blocking(SOCKET s
, BOOL
*ret
)
1188 SERVER_START_REQ( get_socket_event
)
1190 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1191 req
->service
= FALSE
;
1193 err
= NtStatusToWSAError( wine_server_call( req
));
1194 *ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
1200 static unsigned int _get_sock_mask(SOCKET s
)
1203 SERVER_START_REQ( get_socket_event
)
1205 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1206 req
->service
= FALSE
;
1208 wine_server_call( req
);
1215 static void _sync_sock_state(SOCKET s
)
1218 /* do a dummy wineserver request in order to let
1219 the wineserver run through its select loop once */
1220 sock_is_blocking(s
, &dummy
);
1223 static void _get_sock_errors(SOCKET s
, int *events
)
1225 SERVER_START_REQ( get_socket_event
)
1227 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1228 req
->service
= FALSE
;
1230 wine_server_set_reply( req
, events
, sizeof(int) * FD_MAX_EVENTS
);
1231 wine_server_call( req
);
1236 static int get_sock_error(SOCKET s
, unsigned int bit
)
1238 int events
[FD_MAX_EVENTS
];
1239 _get_sock_errors(s
, events
);
1243 static int _get_fd_type(int fd
)
1246 socklen_t optlen
= sizeof(sock_type
);
1247 getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char*) &sock_type
, &optlen
);
1251 static BOOL
set_dont_fragment(SOCKET s
, int level
, BOOL value
)
1255 if (level
== IPPROTO_IP
)
1258 optname
= IP_DONTFRAG
;
1259 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1260 optname
= IP_MTU_DISCOVER
;
1261 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1265 FIXME("IP_DONTFRAGMENT for IPv4 not supported in this platform\n");
1266 return TRUE
; /* fake success */
1271 #ifdef IPV6_DONTFRAG
1272 optname
= IPV6_DONTFRAG
;
1273 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1274 optname
= IPV6_MTU_DISCOVER
;
1275 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1279 FIXME("IP_DONTFRAGMENT for IPv6 not supported in this platform\n");
1280 return TRUE
; /* fake success */
1284 fd
= get_sock_fd(s
, 0, NULL
);
1285 if (fd
== -1) return FALSE
;
1287 if (!setsockopt(fd
, level
, optname
, &value
, sizeof(value
)))
1291 WSASetLastError(wsaErrno());
1295 release_sock_fd(s
, fd
);
1299 static BOOL
get_dont_fragment(SOCKET s
, int level
, BOOL
*out
)
1301 int fd
, optname
, value
, not_expected
;
1302 socklen_t optlen
= sizeof(value
);
1304 if (level
== IPPROTO_IP
)
1307 optname
= IP_DONTFRAG
;
1309 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
1310 optname
= IP_MTU_DISCOVER
;
1311 not_expected
= IP_PMTUDISC_DONT
;
1315 FIXME("IP_DONTFRAGMENT for IPv4 not supported in this platform\n");
1316 return TRUE
; /* fake success */
1321 #ifdef IPV6_DONTFRAG
1322 optname
= IPV6_DONTFRAG
;
1324 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DONT)
1325 optname
= IPV6_MTU_DISCOVER
;
1326 not_expected
= IPV6_PMTUDISC_DONT
;
1330 FIXME("IP_DONTFRAGMENT for IPv6 not supported in this platform\n");
1331 return TRUE
; /* fake success */
1335 fd
= get_sock_fd(s
, 0, NULL
);
1336 if (fd
== -1) return FALSE
;
1338 if (!getsockopt(fd
, level
, optname
, &value
, &optlen
))
1340 *out
= value
!= not_expected
;
1345 WSASetLastError(wsaErrno());
1349 release_sock_fd(s
, fd
);
1353 static struct per_thread_data
*get_per_thread_data(void)
1355 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
1356 /* lazy initialization */
1359 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
1360 NtCurrentTeb()->WinSockData
= ptb
;
1365 static void free_per_thread_data(void)
1367 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
1371 /* delete scratch buffers */
1372 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1373 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1374 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1375 HeapFree( GetProcessHeap(), 0, ptb
->fd_cache
);
1377 HeapFree( GetProcessHeap(), 0, ptb
);
1378 NtCurrentTeb()->WinSockData
= NULL
;
1381 /***********************************************************************
1382 * DllMain (WS2_32.init)
1384 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
1386 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
1387 switch (fdwReason
) {
1388 case DLL_PROCESS_ATTACH
:
1390 case DLL_PROCESS_DETACH
:
1391 if (fImpLoad
) break;
1392 free_per_thread_data();
1393 DeleteCriticalSection(&csWSgetXXXbyYYY
);
1395 case DLL_THREAD_DETACH
:
1396 free_per_thread_data();
1402 /***********************************************************************
1405 * Converts send/recv flags from Windows format.
1406 * Return the converted flag bits, unsupported flags remain unchanged.
1408 static int convert_flags(int flags
)
1411 if (!flags
) return 0;
1413 for (out
= i
= 0; flags
&& i
< ARRAY_SIZE(ws_flags_map
); i
++)
1415 if (ws_flags_map
[i
][0] & flags
)
1417 out
|= ws_flags_map
[i
][1];
1418 flags
&= ~ws_flags_map
[i
][0];
1423 FIXME("Unknown send/recv flags 0x%x, using anyway...\n", flags
);
1429 /***********************************************************************
1432 * Converts socket flags from Windows format.
1433 * Return 1 if converted, 0 if not (error).
1435 static int convert_sockopt(INT
*level
, INT
*optname
)
1441 *level
= SOL_SOCKET
;
1442 for(i
= 0; i
< ARRAY_SIZE(ws_sock_map
); i
++) {
1443 if( ws_sock_map
[i
][0] == *optname
)
1445 *optname
= ws_sock_map
[i
][1];
1449 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
1451 case WS_IPPROTO_TCP
:
1452 *level
= IPPROTO_TCP
;
1453 for(i
= 0; i
< ARRAY_SIZE(ws_tcp_map
); i
++) {
1454 if ( ws_tcp_map
[i
][0] == *optname
)
1456 *optname
= ws_tcp_map
[i
][1];
1460 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
1463 *level
= IPPROTO_IP
;
1464 for(i
= 0; i
< ARRAY_SIZE(ws_ip_map
); i
++) {
1465 if (ws_ip_map
[i
][0] == *optname
)
1467 *optname
= ws_ip_map
[i
][1];
1471 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
1473 case WS_IPPROTO_IPV6
:
1474 *level
= IPPROTO_IPV6
;
1475 for(i
= 0; i
< ARRAY_SIZE(ws_ipv6_map
); i
++) {
1476 if (ws_ipv6_map
[i
][0] == *optname
)
1478 *optname
= ws_ipv6_map
[i
][1];
1482 FIXME("Unknown IPPROTO_IPV6 optname 0x%x\n", *optname
);
1484 default: FIXME("Unimplemented or unknown socket level\n");
1489 /* ----------------------------------- Per-thread info (or per-process?) */
1491 static char *strdup_lower(const char *str
)
1494 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
1498 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
1501 else SetLastError(WSAENOBUFS
);
1505 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
1506 * from an fd and return the value converted to milli seconds
1507 * or 0 if there is an infinite time out */
1508 static inline INT64
get_rcvsnd_timeo( int fd
, BOOL is_recv
)
1511 socklen_t len
= sizeof(tv
);
1516 optname
= SO_RCVTIMEO
;
1522 optname
= SO_SNDTIMEO
;
1527 res
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
1530 return (UINT64
)tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
1533 /* utility: given an fd, will block until one of the events occurs */
1534 static inline int do_block( int fd
, int events
, int timeout
)
1540 pfd
.events
= events
;
1542 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
1553 convert_af_w2u(int windowsaf
) {
1556 for (i
= 0; i
< ARRAY_SIZE(ws_af_map
); i
++)
1557 if (ws_af_map
[i
][0] == windowsaf
)
1558 return ws_af_map
[i
][1];
1559 FIXME("unhandled Windows address family %d\n", windowsaf
);
1564 convert_af_u2w(int unixaf
) {
1567 for (i
= 0; i
< ARRAY_SIZE(ws_af_map
); i
++)
1568 if (ws_af_map
[i
][1] == unixaf
)
1569 return ws_af_map
[i
][0];
1570 FIXME("unhandled UNIX address family %d\n", unixaf
);
1575 convert_proto_w2u(int windowsproto
) {
1578 for (i
= 0; i
< ARRAY_SIZE(ws_proto_map
); i
++)
1579 if (ws_proto_map
[i
][0] == windowsproto
)
1580 return ws_proto_map
[i
][1];
1582 /* check for extended IPX */
1583 if (IS_IPX_PROTO(windowsproto
))
1584 return windowsproto
;
1586 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
1591 convert_proto_u2w(int unixproto
) {
1594 for (i
= 0; i
< ARRAY_SIZE(ws_proto_map
); i
++)
1595 if (ws_proto_map
[i
][1] == unixproto
)
1596 return ws_proto_map
[i
][0];
1598 /* if value is inside IPX range just return it - the kernel simply
1599 * echoes the value used in the socket() function */
1600 if (IS_IPX_PROTO(unixproto
))
1603 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
1608 convert_socktype_w2u(int windowssocktype
) {
1611 for (i
= 0; i
< ARRAY_SIZE(ws_socktype_map
); i
++)
1612 if (ws_socktype_map
[i
][0] == windowssocktype
)
1613 return ws_socktype_map
[i
][1];
1614 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
1619 convert_socktype_u2w(int unixsocktype
) {
1622 for (i
= 0; i
< ARRAY_SIZE(ws_socktype_map
); i
++)
1623 if (ws_socktype_map
[i
][1] == unixsocktype
)
1624 return ws_socktype_map
[i
][0];
1625 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
1629 static int convert_poll_w2u(int events
)
1632 for (i
= ret
= 0; events
&& i
< ARRAY_SIZE(ws_poll_map
); i
++)
1634 if (ws_poll_map
[i
][0] & events
)
1636 ret
|= ws_poll_map
[i
][1];
1637 events
&= ~ws_poll_map
[i
][0];
1642 FIXME("Unsupported WSAPoll() flags 0x%x\n", events
);
1646 static int convert_poll_u2w(int events
)
1649 for (i
= ret
= 0; events
&& i
< ARRAY_SIZE(ws_poll_map
); i
++)
1651 if (ws_poll_map
[i
][1] & events
)
1653 ret
|= ws_poll_map
[i
][0];
1654 events
&= ~ws_poll_map
[i
][1];
1659 FIXME("Unsupported poll() flags 0x%x\n", events
);
1663 static int set_ipx_packettype(int sock
, int ptype
)
1666 int fd
= get_sock_fd( sock
, 0, NULL
), ret
= 0;
1667 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", ptype
, fd
);
1669 if (fd
== -1) return SOCKET_ERROR
;
1671 /* We try to set the ipx type on ipx socket level. */
1673 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, &ptype
, sizeof(ptype
)) == -1)
1675 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
1681 /* Should we retrieve val using a getsockopt call and then
1682 * set the modified one? */
1684 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
1687 release_sock_fd( sock
, fd
);
1690 WARN("IPX support is not enabled, can't set packet type\n");
1691 return SOCKET_ERROR
;
1695 /* ----------------------------------- API -----
1697 * Init / cleanup / error checking.
1700 /***********************************************************************
1701 * WSAStartup (WS2_32.115)
1703 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
1705 TRACE("verReq=%x\n", wVersionRequested
);
1707 if (LOBYTE(wVersionRequested
) < 1)
1708 return WSAVERNOTSUPPORTED
;
1710 if (!lpWSAData
) return WSAEINVAL
;
1714 /* that's the whole of the negotiation for now */
1715 lpWSAData
->wVersion
= wVersionRequested
;
1716 /* return winsock information */
1717 lpWSAData
->wHighVersion
= 0x0202;
1718 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
1719 strcpy(lpWSAData
->szSystemStatus
, "Running" );
1720 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
1721 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
1722 /* don't do anything with lpWSAData->lpVendorInfo */
1723 /* (some apps don't allocate the space for this field) */
1725 TRACE("succeeded starts: %d\n", num_startup
);
1730 /***********************************************************************
1731 * WSACleanup (WS2_32.116)
1733 INT WINAPI
WSACleanup(void)
1737 TRACE("pending cleanups: %d\n", num_startup
);
1740 SetLastError(WSANOTINITIALISED
);
1741 return SOCKET_ERROR
;
1745 /***********************************************************************
1746 * WSAGetLastError (WS2_32.111)
1748 INT WINAPI
WSAGetLastError(void)
1750 return GetLastError();
1753 /***********************************************************************
1754 * WSASetLastError (WS2_32.112)
1756 void WINAPI
WSASetLastError(INT iError
) {
1757 SetLastError(iError
);
1760 static struct WS_hostent
*check_buffer_he(int size
)
1762 struct per_thread_data
* ptb
= get_per_thread_data();
1765 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
1766 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1768 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
1769 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
1770 return ptb
->he_buffer
;
1773 static struct WS_servent
*check_buffer_se(int size
)
1775 struct per_thread_data
* ptb
= get_per_thread_data();
1778 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
1779 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1781 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
1782 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
1783 return ptb
->se_buffer
;
1786 static struct WS_protoent
*check_buffer_pe(int size
)
1788 struct per_thread_data
* ptb
= get_per_thread_data();
1791 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
1792 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1794 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
1795 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
1796 return ptb
->pe_buffer
;
1799 /* ----------------------------------- i/o APIs */
1801 static inline BOOL
supported_pf(int pf
)
1821 static inline BOOL
supported_protocol(int protocol
)
1824 for (i
= 0; i
< ARRAY_SIZE(valid_protocols
); i
++)
1825 if (protocol
== valid_protocols
[i
])
1830 /**********************************************************************/
1832 /* Returns the length of the converted address if successful, 0 if it was too
1833 * small to start with or unknown family or invalid address buffer.
1835 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
1836 union generic_unix_sockaddr
*uaddr
)
1838 unsigned int uaddrlen
= 0;
1843 switch (wsaddr
->sa_family
)
1848 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
1849 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
1851 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
1854 uaddrlen
= sizeof(struct sockaddr_ipx
);
1855 memset( uaddr
, 0, uaddrlen
);
1856 uipx
->sipx_family
=AF_IPX
;
1857 uipx
->sipx_port
=wsipx
->sa_socket
;
1858 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
1861 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
1862 #ifdef IPX_FRAME_NONE
1863 uipx
->sipx_type
=IPX_FRAME_NONE
;
1869 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
1870 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
1872 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
1873 * scope_id, one without.
1875 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
1876 uaddrlen
= sizeof(struct sockaddr_in6
);
1877 memset( uaddr
, 0, uaddrlen
);
1878 uin6
->sin6_family
= AF_INET6
;
1879 uin6
->sin6_port
= win6
->sin6_port
;
1880 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
1881 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1882 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
1884 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1887 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
1891 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
1892 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
1894 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
1896 uaddrlen
= sizeof(struct sockaddr_in
);
1897 memset( uaddr
, 0, uaddrlen
);
1898 uin
->sin_family
= AF_INET
;
1899 uin
->sin_port
= win
->sin_port
;
1900 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
1905 struct sockaddr_irda
*uin
= (struct sockaddr_irda
*)uaddr
;
1906 const SOCKADDR_IRDA
*win
= (const SOCKADDR_IRDA
*)wsaddr
;
1908 if (wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1910 uaddrlen
= sizeof(struct sockaddr_irda
);
1911 memset( uaddr
, 0, uaddrlen
);
1912 uin
->sir_family
= AF_IRDA
;
1913 if (!strncmp( win
->irdaServiceName
, "LSAP-SEL", strlen( "LSAP-SEL" ) ))
1915 unsigned int lsap_sel
= 0;
1917 sscanf( win
->irdaServiceName
, "LSAP-SEL%u", &lsap_sel
);
1918 uin
->sir_lsap_sel
= lsap_sel
;
1922 uin
->sir_lsap_sel
= LSAP_ANY
;
1923 memcpy( uin
->sir_name
, win
->irdaServiceName
, 25 );
1925 memcpy( &uin
->sir_addr
, win
->irdaDeviceID
, sizeof(uin
->sir_addr
) );
1929 case WS_AF_UNSPEC
: {
1930 /* Try to determine the needed space by the passed windows sockaddr space */
1931 switch (wsaddrlen
) {
1932 default: /* likely an ipv4 address */
1933 case sizeof(struct WS_sockaddr_in
):
1934 uaddrlen
= sizeof(struct sockaddr_in
);
1937 case sizeof(struct WS_sockaddr_ipx
):
1938 uaddrlen
= sizeof(struct sockaddr_ipx
);
1942 case sizeof(SOCKADDR_IRDA
):
1943 uaddrlen
= sizeof(struct sockaddr_irda
);
1946 case sizeof(struct WS_sockaddr_in6
):
1947 case sizeof(struct WS_sockaddr_in6_old
):
1948 uaddrlen
= sizeof(struct sockaddr_in6
);
1951 memset( uaddr
, 0, uaddrlen
);
1955 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
1961 static BOOL
is_sockaddr_bound(const struct sockaddr
*uaddr
, int uaddrlen
)
1963 switch (uaddr
->sa_family
)
1968 static const struct sockaddr_ipx emptyAddr
;
1969 struct sockaddr_ipx
*ipx
= (struct sockaddr_ipx
*) uaddr
;
1970 return ipx
->sipx_port
1971 || memcmp(&ipx
->sipx_network
, &emptyAddr
.sipx_network
, sizeof(emptyAddr
.sipx_network
))
1972 || memcmp(&ipx
->sipx_node
, &emptyAddr
.sipx_node
, sizeof(emptyAddr
.sipx_node
));
1977 static const struct sockaddr_in6 emptyAddr
;
1978 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) uaddr
;
1979 return in6
->sin6_port
|| memcmp(&in6
->sin6_addr
, &emptyAddr
.sin6_addr
, sizeof(struct in6_addr
));
1983 static const struct sockaddr_in emptyAddr
;
1984 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) uaddr
;
1985 return in
->sin_port
|| memcmp(&in
->sin_addr
, &emptyAddr
.sin_addr
, sizeof(struct in_addr
));
1990 FIXME("unknown address family %d\n", uaddr
->sa_family
);
1995 /* Returns -1 if getsockname fails, 0 if not bound, 1 otherwise */
1996 static int is_fd_bound(int fd
, union generic_unix_sockaddr
*uaddr
, socklen_t
*uaddrlen
)
1998 union generic_unix_sockaddr inaddr
;
2002 if (!uaddr
) uaddr
= &inaddr
;
2003 if (!uaddrlen
) uaddrlen
= &inlen
;
2005 *uaddrlen
= sizeof(inaddr
);
2006 res
= getsockname(fd
, &uaddr
->addr
, uaddrlen
);
2007 if (!res
) res
= is_sockaddr_bound(&uaddr
->addr
, *uaddrlen
);
2011 /* Returns 0 if successful, -1 if the buffer is too small */
2012 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
2016 switch(uaddr
->sa_family
)
2021 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
2022 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
2025 switch (*wsaddrlen
) /* how much can we copy? */
2029 *wsaddrlen
= sizeof(*wsipx
);
2030 wsipx
->sa_socket
=uipx
->sipx_port
;
2034 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
2042 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
2048 wsipx
->sa_family
=WS_AF_IPX
;
2060 const struct sockaddr_irda
*uin
= (const struct sockaddr_irda
*)uaddr
;
2061 SOCKADDR_IRDA
*win
= (SOCKADDR_IRDA
*)wsaddr
;
2063 if (*wsaddrlen
< sizeof(SOCKADDR_IRDA
))
2065 win
->irdaAddressFamily
= WS_AF_IRDA
;
2066 memcpy( win
->irdaDeviceID
, &uin
->sir_addr
, sizeof(win
->irdaDeviceID
) );
2067 if (uin
->sir_lsap_sel
!= LSAP_ANY
)
2068 sprintf( win
->irdaServiceName
, "LSAP-SEL%u", uin
->sir_lsap_sel
);
2070 memcpy( win
->irdaServiceName
, uin
->sir_name
,
2071 sizeof(win
->irdaServiceName
) );
2076 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
2077 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
2079 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
2081 win6old
->sin6_family
= WS_AF_INET6
;
2082 win6old
->sin6_port
= uin6
->sin6_port
;
2083 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
2084 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
2085 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2086 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
2087 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
2088 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
2089 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
2092 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
2094 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
2099 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
2100 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
2102 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
2104 win
->sin_family
= WS_AF_INET
;
2105 win
->sin_port
= uin
->sin_port
;
2106 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
2107 memset(win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
2108 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
2112 memset(wsaddr
,0,*wsaddrlen
);
2116 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
2122 static INT
WS_DuplicateSocket(BOOL unicode
, SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
2126 WSAPROTOCOL_INFOW infow
;
2128 TRACE("(unicode %d, socket %04lx, processid %x, buffer %p)\n",
2129 unicode
, s
, dwProcessId
, lpProtocolInfo
);
2131 if (!ws_protocol_info(s
, unicode
, &infow
, &size
))
2132 return SOCKET_ERROR
;
2134 if (!(hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
)))
2136 SetLastError(WSAEINVAL
);
2137 return SOCKET_ERROR
;
2140 if (!lpProtocolInfo
)
2142 CloseHandle(hProcess
);
2143 SetLastError(WSAEFAULT
);
2144 return SOCKET_ERROR
;
2147 /* I don't know what the real Windoze does next, this is a hack */
2148 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
2149 * the target use the global duplicate, or we could copy a reference to us to the structure
2150 * and let the target duplicate it from us, but let's do it as simple as possible */
2151 memcpy(lpProtocolInfo
, &infow
, size
);
2152 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
2153 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwServiceFlags3
,
2154 0, FALSE
, DUPLICATE_SAME_ACCESS
);
2155 CloseHandle(hProcess
);
2156 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
2160 /*****************************************************************************
2161 * WS_EnterSingleProtocolW [internal]
2163 * enters the protocol information of one given protocol into the given
2167 * TRUE if a protocol was entered into the buffer.
2170 * - only implemented for IPX, SPX, SPXII, TCP, UDP
2171 * - there is no check that the operating system supports the returned
2174 static BOOL
WS_EnterSingleProtocolW( INT protocol
, WSAPROTOCOL_INFOW
* info
)
2176 memset( info
, 0, sizeof(WSAPROTOCOL_INFOW
) );
2177 info
->iProtocol
= protocol
;
2181 case WS_IPPROTO_TCP
:
2182 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_EXPEDITED_DATA
|
2183 XP1_GRACEFUL_CLOSE
| XP1_GUARANTEED_ORDER
|
2184 XP1_GUARANTEED_DELIVERY
;
2185 info
->ProviderId
= ProviderIdIP
;
2186 info
->dwCatalogEntryId
= 0x3e9;
2187 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2188 info
->ProtocolChain
.ChainLen
= 1;
2190 info
->iAddressFamily
= WS_AF_INET
;
2191 info
->iMaxSockAddr
= 0x10;
2192 info
->iMinSockAddr
= 0x10;
2193 info
->iSocketType
= WS_SOCK_STREAM
;
2194 strcpyW( info
->szProtocol
, NameTcpW
);
2197 case WS_IPPROTO_UDP
:
2198 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_SUPPORT_BROADCAST
|
2199 XP1_SUPPORT_MULTIPOINT
| XP1_MESSAGE_ORIENTED
|
2201 info
->ProviderId
= ProviderIdIP
;
2202 info
->dwCatalogEntryId
= 0x3ea;
2203 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2204 info
->ProtocolChain
.ChainLen
= 1;
2206 info
->iAddressFamily
= WS_AF_INET
;
2207 info
->iMaxSockAddr
= 0x10;
2208 info
->iMinSockAddr
= 0x10;
2209 info
->iSocketType
= WS_SOCK_DGRAM
;
2210 info
->dwMessageSize
= 0xffbb;
2211 strcpyW( info
->szProtocol
, NameUdpW
);
2214 case WS_NSPROTO_IPX
:
2215 info
->dwServiceFlags1
= XP1_PARTIAL_MESSAGE
| XP1_SUPPORT_BROADCAST
|
2216 XP1_SUPPORT_MULTIPOINT
| XP1_MESSAGE_ORIENTED
|
2218 info
->ProviderId
= ProviderIdIPX
;
2219 info
->dwCatalogEntryId
= 0x406;
2220 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2221 info
->ProtocolChain
.ChainLen
= 1;
2223 info
->iAddressFamily
= WS_AF_IPX
;
2224 info
->iMaxSockAddr
= 0x10;
2225 info
->iMinSockAddr
= 0x0e;
2226 info
->iSocketType
= WS_SOCK_DGRAM
;
2227 info
->iProtocolMaxOffset
= 0xff;
2228 info
->dwMessageSize
= 0x240;
2229 strcpyW( info
->szProtocol
, NameIpxW
);
2232 case WS_NSPROTO_SPX
:
2233 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_PSEUDO_STREAM
|
2234 XP1_MESSAGE_ORIENTED
| XP1_GUARANTEED_ORDER
|
2235 XP1_GUARANTEED_DELIVERY
;
2236 info
->ProviderId
= ProviderIdSPX
;
2237 info
->dwCatalogEntryId
= 0x407;
2238 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2239 info
->ProtocolChain
.ChainLen
= 1;
2241 info
->iAddressFamily
= WS_AF_IPX
;
2242 info
->iMaxSockAddr
= 0x10;
2243 info
->iMinSockAddr
= 0x0e;
2244 info
->iSocketType
= WS_SOCK_SEQPACKET
;
2245 info
->dwMessageSize
= 0xffffffff;
2246 strcpyW( info
->szProtocol
, NameSpxW
);
2249 case WS_NSPROTO_SPXII
:
2250 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_GRACEFUL_CLOSE
|
2251 XP1_PSEUDO_STREAM
| XP1_MESSAGE_ORIENTED
|
2252 XP1_GUARANTEED_ORDER
| XP1_GUARANTEED_DELIVERY
;
2253 info
->ProviderId
= ProviderIdSPX
;
2254 info
->dwCatalogEntryId
= 0x409;
2255 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2256 info
->ProtocolChain
.ChainLen
= 1;
2258 info
->iAddressFamily
= WS_AF_IPX
;
2259 info
->iMaxSockAddr
= 0x10;
2260 info
->iMinSockAddr
= 0x0e;
2261 info
->iSocketType
= WS_SOCK_SEQPACKET
;
2262 info
->dwMessageSize
= 0xffffffff;
2263 strcpyW( info
->szProtocol
, NameSpxIIW
);
2267 FIXME("unknown Protocol <0x%08x>\n", protocol
);
2273 /*****************************************************************************
2274 * WS_EnterSingleProtocolA [internal]
2276 * see function WS_EnterSingleProtocolW
2279 static BOOL
WS_EnterSingleProtocolA( INT protocol
, WSAPROTOCOL_INFOA
* info
)
2281 WSAPROTOCOL_INFOW infow
;
2283 memset( info
, 0, sizeof(WSAPROTOCOL_INFOA
) );
2285 ret
= WS_EnterSingleProtocolW( protocol
, &infow
);
2288 /* convert the structure from W to A */
2289 memcpy( info
, &infow
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
2290 WideCharToMultiByte( CP_ACP
, 0, infow
.szProtocol
, -1,
2291 info
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
);
2297 static INT
WS_EnumProtocols( BOOL unicode
, const INT
*protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
)
2299 INT i
= 0, items
= 0;
2303 LPWSAPROTOCOL_INFOA a
;
2304 LPWSAPROTOCOL_INFOW w
;
2308 if (!protocols
) protocols
= valid_protocols
;
2310 while (protocols
[i
])
2312 if(supported_protocol(protocols
[i
++]))
2316 size
= items
* (unicode
? sizeof(WSAPROTOCOL_INFOW
) : sizeof(WSAPROTOCOL_INFOA
));
2318 TRACE("unicode %d, protocols %p, buffer %p, length %p %d, items %d, required %d\n",
2319 unicode
, protocols
, buffer
, len
, len
? *len
: 0, items
, size
);
2321 if (*len
< size
|| !buffer
)
2324 SetLastError(WSAENOBUFS
);
2325 return SOCKET_ERROR
;
2328 for (i
= items
= 0; protocols
[i
]; i
++)
2330 if (!supported_protocol(protocols
[i
])) continue;
2333 if (WS_EnterSingleProtocolW( protocols
[i
], &info
.w
[items
] ))
2338 if (WS_EnterSingleProtocolA( protocols
[i
], &info
.a
[items
] ))
2345 static BOOL
ws_protocol_info(SOCKET s
, int unicode
, WSAPROTOCOL_INFOW
*buffer
, int *size
)
2352 *size
= unicode
? sizeof(WSAPROTOCOL_INFOW
) : sizeof(WSAPROTOCOL_INFOA
);
2353 memset(buffer
, 0, *size
);
2355 SERVER_START_REQ( get_socket_info
)
2357 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
2358 status
= wine_server_call( req
);
2361 address_family
= convert_af_u2w(reply
->family
);
2362 socket_type
= convert_socktype_u2w(reply
->type
);
2363 protocol
= convert_proto_u2w(reply
->protocol
);
2370 unsigned int err
= NtStatusToWSAError( status
);
2371 SetLastError( err
== WSAEBADF
? WSAENOTSOCK
: err
);
2376 WS_EnterSingleProtocolW( protocol
, buffer
);
2378 WS_EnterSingleProtocolA( protocol
, (WSAPROTOCOL_INFOA
*)buffer
);
2379 buffer
->iAddressFamily
= address_family
;
2380 buffer
->iSocketType
= socket_type
;
2381 buffer
->iProtocol
= protocol
;
2386 /**************************************************************************
2387 * Functions for handling overlapped I/O
2388 **************************************************************************/
2390 /* user APC called upon async completion */
2391 static void WINAPI
ws2_async_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
2393 struct ws2_async
*wsa
= arg
;
2395 if (wsa
->completion_func
) wsa
->completion_func( NtStatusToWSAError(iosb
->u
.Status
),
2396 iosb
->Information
, wsa
->user_overlapped
,
2398 release_async_io( &wsa
->io
);
2401 /***********************************************************************
2402 * WS2_recv (INTERNAL)
2404 * Workhorse for both synchronous and asynchronous recv() operations.
2406 static int WS2_recv( int fd
, struct ws2_async
*wsa
, int flags
)
2408 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2412 union generic_unix_sockaddr unix_sockaddr
;
2415 hdr
.msg_name
= NULL
;
2419 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
2420 hdr
.msg_name
= &unix_sockaddr
;
2423 hdr
.msg_namelen
= 0;
2425 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
2426 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
2427 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2428 hdr
.msg_accrights
= NULL
;
2429 hdr
.msg_accrightslen
= 0;
2431 hdr
.msg_control
= pktbuf
;
2432 hdr
.msg_controllen
= sizeof(pktbuf
);
2436 while ((n
= __wine_locked_recvmsg( fd
, &hdr
, flags
)) == -1)
2442 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2445 ERR("Message control headers cannot be properly supported on this system.\n");
2446 wsa
->control
->len
= 0;
2449 if (wsa
->control
&& !convert_control_headers(&hdr
, wsa
->control
))
2451 WARN("Application passed insufficient room for control headers.\n");
2452 *wsa
->lpFlags
|= WS_MSG_CTRUNC
;
2458 /* if this socket is connected and lpFrom is not NULL, Linux doesn't give us
2459 * msg_name and msg_namelen from recvmsg, but it does set msg_namelen to zero.
2461 * quoting linux 2.6 net/ipv4/tcp.c:
2462 * "According to UNIX98, msg_name/msg_namelen are ignored
2463 * on connected socket. I was just happy when found this 8) --ANK"
2465 * likewise MSDN says that lpFrom and lpFromlen are ignored for
2466 * connection-oriented sockets, so don't try to update lpFrom.
2468 if (wsa
->addr
&& hdr
.msg_namelen
)
2469 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, wsa
->addr
, wsa
->addrlen
.ptr
);
2474 /***********************************************************************
2475 * WS2_async_recv (INTERNAL)
2477 * Handler for overlapped recv() operations.
2479 static NTSTATUS
WS2_async_recv( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
2481 struct ws2_async
*wsa
= user
;
2486 case STATUS_ALERTED
:
2487 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
2490 result
= WS2_recv( fd
, wsa
, convert_flags(wsa
->flags
) );
2491 wine_server_release_fd( wsa
->hSocket
, fd
);
2494 status
= STATUS_SUCCESS
;
2495 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
2499 if (errno
== EAGAIN
)
2501 status
= STATUS_PENDING
;
2502 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
2507 status
= wsaErrStatus();
2512 if (status
!= STATUS_PENDING
)
2514 iosb
->u
.Status
= status
;
2515 iosb
->Information
= result
;
2516 if (!wsa
->completion_func
)
2517 release_async_io( &wsa
->io
);
2522 /***********************************************************************
2523 * WS2_async_accept_recv (INTERNAL)
2525 * This function is used to finish the read part of an accept request. It is
2526 * needed to place the completion on the correct socket (listener).
2528 static NTSTATUS
WS2_async_accept_recv( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
2530 struct ws2_accept_async
*wsa
= user
;
2532 status
= WS2_async_recv( wsa
->read
, iosb
, status
);
2533 if (status
== STATUS_PENDING
)
2537 WS_AddCompletion( HANDLE2SOCKET(wsa
->listen_socket
), wsa
->cvalue
, iosb
->u
.Status
, iosb
->Information
, TRUE
);
2539 release_async_io( &wsa
->io
);
2543 /***********************************************************************
2544 * WS2_async_accept (INTERNAL)
2546 * This is the function called to satisfy the AcceptEx callback
2548 static NTSTATUS
WS2_async_accept( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
2550 struct ws2_accept_async
*wsa
= user
;
2554 TRACE("status: 0x%x listen: %p, accept: %p\n", status
, wsa
->listen_socket
, wsa
->accept_socket
);
2556 if (status
== STATUS_ALERTED
)
2558 SERVER_START_REQ( accept_into_socket
)
2560 req
->lhandle
= wine_server_obj_handle( wsa
->listen_socket
);
2561 req
->ahandle
= wine_server_obj_handle( wsa
->accept_socket
);
2562 status
= wine_server_call( req
);
2566 if (NtStatusToWSAError( status
) == WSAEWOULDBLOCK
)
2567 return STATUS_PENDING
;
2569 if (status
== STATUS_INVALID_HANDLE
)
2571 FIXME("AcceptEx accepting socket closed but request was not cancelled\n");
2572 status
= STATUS_CANCELLED
;
2575 else if (status
== STATUS_HANDLES_CLOSED
)
2576 status
= STATUS_CANCELLED
; /* strange windows behavior */
2578 if (status
!= STATUS_SUCCESS
)
2581 /* WS2 Spec says size param is extra 16 bytes long...what do we put in it? */
2582 addr
= ((char *)wsa
->buf
) + wsa
->data_len
;
2583 len
= wsa
->local_len
- sizeof(int);
2584 WS_getsockname(HANDLE2SOCKET(wsa
->accept_socket
),
2585 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
2588 addr
+= wsa
->local_len
;
2589 len
= wsa
->remote_len
- sizeof(int);
2590 WS_getpeername(HANDLE2SOCKET(wsa
->accept_socket
),
2591 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
2597 wsa
->io
.callback
= WS2_async_accept_recv
;
2598 status
= register_async( ASYNC_TYPE_READ
, wsa
->accept_socket
, &wsa
->io
,
2599 wsa
->user_overlapped
->hEvent
, NULL
, NULL
, iosb
);
2601 if (status
!= STATUS_PENDING
)
2604 /* The APC has finished but no completion should be sent for the operation yet, additional processing
2605 * needs to be performed by WS2_async_accept_recv() first. */
2606 return STATUS_MORE_PROCESSING_REQUIRED
;
2609 iosb
->u
.Status
= status
;
2610 iosb
->Information
= 0;
2612 if (wsa
->read
) release_async_io( &wsa
->read
->io
);
2613 release_async_io( &wsa
->io
);
2617 /***********************************************************************
2618 * WS2_send (INTERNAL)
2620 * Workhorse for both synchronous and asynchronous send() operations.
2622 static int WS2_send( int fd
, struct ws2_async
*wsa
, int flags
)
2625 union generic_unix_sockaddr unix_addr
;
2628 hdr
.msg_name
= NULL
;
2629 hdr
.msg_namelen
= 0;
2633 hdr
.msg_name
= &unix_addr
;
2634 hdr
.msg_namelen
= ws_sockaddr_ws2u( wsa
->addr
, wsa
->addrlen
.val
, &unix_addr
);
2635 if ( !hdr
.msg_namelen
)
2641 #if defined(HAS_IPX) && defined(SOL_IPX)
2642 if(wsa
->addr
->sa_family
== WS_AF_IPX
)
2644 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
2646 socklen_t len
= sizeof(int);
2648 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
2649 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
2650 * the packet type and then we can retrieve it using getsockopt. After that we can set the
2651 * ipx type in the sockaddr_opx structure with the stored value.
2653 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
2654 uipx
->sipx_type
= val
;
2659 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
2660 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
2661 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2662 hdr
.msg_accrights
= NULL
;
2663 hdr
.msg_accrightslen
= 0;
2665 hdr
.msg_control
= NULL
;
2666 hdr
.msg_controllen
= 0;
2670 while ((ret
= sendmsg(fd
, &hdr
, flags
)) == -1)
2672 if (errno
== EISCONN
)
2675 hdr
.msg_namelen
= 0;
2683 while (wsa
->first_iovec
< wsa
->n_iovecs
&& wsa
->iovec
[wsa
->first_iovec
].iov_len
<= n
)
2684 n
-= wsa
->iovec
[wsa
->first_iovec
++].iov_len
;
2685 if (wsa
->first_iovec
< wsa
->n_iovecs
)
2687 wsa
->iovec
[wsa
->first_iovec
].iov_base
= (char*)wsa
->iovec
[wsa
->first_iovec
].iov_base
+ n
;
2688 wsa
->iovec
[wsa
->first_iovec
].iov_len
-= n
;
2693 /***********************************************************************
2694 * WS2_async_send (INTERNAL)
2696 * Handler for overlapped send() operations.
2698 static NTSTATUS
WS2_async_send( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
2700 struct ws2_async
*wsa
= user
;
2705 case STATUS_ALERTED
:
2706 if ( wsa
->n_iovecs
<= wsa
->first_iovec
)
2709 status
= STATUS_SUCCESS
;
2712 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
2715 /* check to see if the data is ready (non-blocking) */
2716 result
= WS2_send( fd
, wsa
, convert_flags(wsa
->flags
) );
2717 wine_server_release_fd( wsa
->hSocket
, fd
);
2721 if (wsa
->first_iovec
< wsa
->n_iovecs
)
2722 status
= STATUS_PENDING
;
2724 status
= STATUS_SUCCESS
;
2726 iosb
->Information
+= result
;
2728 else if (errno
== EAGAIN
)
2730 status
= STATUS_PENDING
;
2734 status
= wsaErrStatus();
2738 if (status
!= STATUS_PENDING
)
2740 iosb
->u
.Status
= status
;
2741 if (!wsa
->completion_func
)
2742 release_async_io( &wsa
->io
);
2747 /***********************************************************************
2748 * WS2_async_shutdown (INTERNAL)
2750 * Handler for shutdown() operations on overlapped sockets.
2752 static NTSTATUS
WS2_async_shutdown( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
2754 struct ws2_async_shutdown
*wsa
= user
;
2759 case STATUS_ALERTED
:
2760 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
2763 switch ( wsa
->type
)
2765 case ASYNC_TYPE_READ
: err
= shutdown( fd
, 0 ); break;
2766 case ASYNC_TYPE_WRITE
: err
= shutdown( fd
, 1 ); break;
2768 status
= err
? wsaErrStatus() : STATUS_SUCCESS
;
2769 wine_server_release_fd( wsa
->hSocket
, fd
);
2772 iosb
->u
.Status
= status
;
2773 iosb
->Information
= 0;
2774 release_async_io( &wsa
->io
);
2778 /***********************************************************************
2779 * WS2_register_async_shutdown (INTERNAL)
2781 * Helper function for WS_shutdown() on overlapped sockets.
2783 static int WS2_register_async_shutdown( SOCKET s
, int type
)
2785 struct ws2_async_shutdown
*wsa
;
2788 TRACE("socket %04lx type %d\n", s
, type
);
2790 wsa
= (struct ws2_async_shutdown
*)alloc_async_io( sizeof(*wsa
), WS2_async_shutdown
);
2794 wsa
->hSocket
= SOCKET2HANDLE(s
);
2797 status
= register_async( type
, wsa
->hSocket
, &wsa
->io
, 0, NULL
, NULL
, &wsa
->iosb
);
2798 if (status
!= STATUS_PENDING
)
2800 HeapFree( GetProcessHeap(), 0, wsa
);
2801 return NtStatusToWSAError( status
);
2806 /***********************************************************************
2809 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
, int *addrlen32
)
2816 TRACE("socket %04lx\n", s
);
2817 err
= sock_is_blocking(s
, &is_blocking
);
2823 /* try accepting first (if there is a deferred connection) */
2824 SERVER_START_REQ( accept_socket
)
2826 req
->lhandle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
2827 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
2828 req
->attributes
= OBJ_INHERIT
;
2829 err
= NtStatusToWSAError( wine_server_call( req
));
2830 as
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
2835 if (addr
&& addrlen32
&& WS_getpeername(as
, addr
, addrlen32
))
2838 return SOCKET_ERROR
;
2840 TRACE("\taccepted %04lx\n", as
);
2843 if (!is_blocking
) break;
2844 if (err
!= WSAEWOULDBLOCK
) break;
2845 fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2847 do_block(fd
, POLLIN
, -1);
2848 _sync_sock_state(s
); /* let wineserver notice connection */
2849 release_sock_fd( s
, fd
);
2853 WARN(" -> ERROR %d\n", err
);
2855 return INVALID_SOCKET
;
2858 /***********************************************************************
2861 static BOOL WINAPI
WS2_AcceptEx(SOCKET listener
, SOCKET acceptor
, PVOID dest
, DWORD dest_len
,
2862 DWORD local_addr_len
, DWORD rem_addr_len
, LPDWORD received
,
2863 LPOVERLAPPED overlapped
)
2866 struct ws2_accept_async
*wsa
;
2869 TRACE("(%04lx, %04lx, %p, %d, %d, %d, %p, %p)\n", listener
, acceptor
, dest
, dest_len
, local_addr_len
,
2870 rem_addr_len
, received
, overlapped
);
2874 SetLastError(WSA_INVALID_PARAMETER
);
2877 overlapped
->Internal
= STATUS_PENDING
;
2881 SetLastError(WSAEINVAL
);
2887 SetLastError(WSAEFAULT
);
2891 fd
= get_sock_fd( listener
, FILE_READ_DATA
, NULL
);
2892 if (fd
== -1) return FALSE
;
2893 release_sock_fd( listener
, fd
);
2895 fd
= get_sock_fd( acceptor
, FILE_READ_DATA
, NULL
);
2896 if (fd
== -1) return FALSE
;
2897 release_sock_fd( acceptor
, fd
);
2899 wsa
= (struct ws2_accept_async
*)alloc_async_io( sizeof(*wsa
), WS2_async_accept
);
2902 SetLastError(WSAEFAULT
);
2906 wsa
->listen_socket
= SOCKET2HANDLE(listener
);
2907 wsa
->accept_socket
= SOCKET2HANDLE(acceptor
);
2908 wsa
->user_overlapped
= overlapped
;
2909 wsa
->cvalue
= !((ULONG_PTR
)overlapped
->hEvent
& 1) ? (ULONG_PTR
)overlapped
: 0;
2911 wsa
->data_len
= dest_len
;
2912 wsa
->local_len
= local_addr_len
;
2913 wsa
->remote_len
= rem_addr_len
;
2918 /* set up a read request if we need it */
2919 wsa
->read
= (struct ws2_async
*)alloc_async_io( offsetof(struct ws2_async
, iovec
[1]), WS2_async_accept_recv
);
2922 HeapFree( GetProcessHeap(), 0, wsa
);
2923 SetLastError(WSAEFAULT
);
2927 wsa
->read
->hSocket
= wsa
->accept_socket
;
2928 wsa
->read
->flags
= 0;
2929 wsa
->read
->lpFlags
= &wsa
->read
->flags
;
2930 wsa
->read
->addr
= NULL
;
2931 wsa
->read
->addrlen
.ptr
= NULL
;
2932 wsa
->read
->control
= NULL
;
2933 wsa
->read
->n_iovecs
= 1;
2934 wsa
->read
->first_iovec
= 0;
2935 wsa
->read
->completion_func
= NULL
;
2936 wsa
->read
->iovec
[0].iov_base
= wsa
->buf
;
2937 wsa
->read
->iovec
[0].iov_len
= wsa
->data_len
;
2940 status
= register_async( ASYNC_TYPE_READ
, SOCKET2HANDLE(listener
), &wsa
->io
,
2941 overlapped
->hEvent
, NULL
, (void *)wsa
->cvalue
, (IO_STATUS_BLOCK
*)overlapped
);
2943 if(status
!= STATUS_PENDING
)
2945 HeapFree( GetProcessHeap(), 0, wsa
->read
);
2946 HeapFree( GetProcessHeap(), 0, wsa
);
2949 SetLastError( NtStatusToWSAError(status
) );
2953 /***********************************************************************
2954 * WS2_ReadFile (INTERNAL)
2956 * Perform an APC-safe ReadFile operation
2958 static NTSTATUS
WS2_ReadFile(HANDLE hFile
, PIO_STATUS_BLOCK io_status
, char* buffer
, ULONG length
,
2959 PLARGE_INTEGER offset
)
2961 int result
= -1, unix_handle
;
2962 unsigned int options
;
2965 TRACE( "(%p,%p,0x%08x)\n", hFile
, buffer
,length
);
2967 status
= wine_server_handle_to_fd( hFile
, FILE_READ_DATA
, &unix_handle
, &options
);
2968 if (status
) return status
;
2970 while (result
== -1)
2972 if (offset
->QuadPart
!= FILE_USE_FILE_POINTER_POSITION
)
2973 result
= pread( unix_handle
, buffer
, length
, offset
->QuadPart
);
2975 result
= read( unix_handle
, buffer
, length
);
2981 status
= (length
? STATUS_END_OF_FILE
: STATUS_SUCCESS
);
2982 else if (result
!= -1)
2983 status
= STATUS_SUCCESS
;
2984 else if (errno
!= EAGAIN
)
2985 status
= wsaErrStatus();
2987 status
= STATUS_PENDING
;
2989 wine_server_release_fd( hFile
, unix_handle
);
2990 TRACE("= 0x%08x (%d)\n", status
, result
);
2991 if (status
== STATUS_SUCCESS
|| status
== STATUS_END_OF_FILE
)
2993 io_status
->u
.Status
= status
;
2994 io_status
->Information
= result
;
3000 /***********************************************************************
3001 * WS2_transmitfile_getbuffer (INTERNAL)
3003 * Pick the appropriate buffer for a TransmitFile send operation.
3005 static NTSTATUS
WS2_transmitfile_getbuffer( int fd
, struct ws2_transmitfile_async
*wsa
)
3007 /* send any incomplete writes from a previous iteration */
3008 if (wsa
->write
.first_iovec
< wsa
->write
.n_iovecs
)
3009 return STATUS_PENDING
;
3011 /* process the header (if applicable) */
3012 if (wsa
->buffers
.Head
)
3014 wsa
->write
.first_iovec
= 0;
3015 wsa
->write
.n_iovecs
= 1;
3016 wsa
->write
.iovec
[0].iov_base
= wsa
->buffers
.Head
;
3017 wsa
->write
.iovec
[0].iov_len
= wsa
->buffers
.HeadLength
;
3018 wsa
->buffers
.Head
= NULL
;
3019 return STATUS_PENDING
;
3022 /* process the main file */
3025 DWORD bytes_per_send
= wsa
->bytes_per_send
;
3026 IO_STATUS_BLOCK iosb
;
3029 iosb
.Information
= 0;
3030 /* when the size of the transfer is limited ensure that we don't go past that limit */
3031 if (wsa
->file_bytes
!= 0)
3032 bytes_per_send
= min(bytes_per_send
, wsa
->file_bytes
- wsa
->file_read
);
3033 status
= WS2_ReadFile( wsa
->file
, &iosb
, wsa
->buffer
, bytes_per_send
, &wsa
->offset
);
3034 if (wsa
->offset
.QuadPart
!= FILE_USE_FILE_POINTER_POSITION
)
3035 wsa
->offset
.QuadPart
+= iosb
.Information
;
3036 if (status
== STATUS_END_OF_FILE
)
3037 wsa
->file
= NULL
; /* continue on to the footer */
3038 else if (status
!= STATUS_SUCCESS
)
3042 if (iosb
.Information
)
3044 wsa
->write
.first_iovec
= 0;
3045 wsa
->write
.n_iovecs
= 1;
3046 wsa
->write
.iovec
[0].iov_base
= wsa
->buffer
;
3047 wsa
->write
.iovec
[0].iov_len
= iosb
.Information
;
3048 wsa
->file_read
+= iosb
.Information
;
3051 if (wsa
->file_bytes
!= 0 && wsa
->file_read
>= wsa
->file_bytes
)
3054 return STATUS_PENDING
;
3058 /* send the footer (if applicable) */
3059 if (wsa
->buffers
.Tail
)
3061 wsa
->write
.first_iovec
= 0;
3062 wsa
->write
.n_iovecs
= 1;
3063 wsa
->write
.iovec
[0].iov_base
= wsa
->buffers
.Tail
;
3064 wsa
->write
.iovec
[0].iov_len
= wsa
->buffers
.TailLength
;
3065 wsa
->buffers
.Tail
= NULL
;
3066 return STATUS_PENDING
;
3069 return STATUS_SUCCESS
;
3072 /***********************************************************************
3073 * WS2_transmitfile_base (INTERNAL)
3075 * Shared implementation for both synchronous and asynchronous TransmitFile.
3077 static NTSTATUS
WS2_transmitfile_base( int fd
, struct ws2_transmitfile_async
*wsa
)
3081 status
= WS2_transmitfile_getbuffer( fd
, wsa
);
3082 if (status
== STATUS_PENDING
)
3084 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)wsa
->write
.user_overlapped
;
3087 n
= WS2_send( fd
, &wsa
->write
, convert_flags(wsa
->write
.flags
) );
3090 if (iosb
) iosb
->Information
+= n
;
3092 else if (errno
!= EAGAIN
)
3093 return wsaErrStatus();
3099 /***********************************************************************
3100 * WS2_async_transmitfile (INTERNAL)
3102 * Asynchronous callback for overlapped TransmitFile operations.
3104 static NTSTATUS
WS2_async_transmitfile( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
3106 struct ws2_transmitfile_async
*wsa
= user
;
3109 if (status
== STATUS_ALERTED
)
3111 if (!(status
= wine_server_handle_to_fd( wsa
->write
.hSocket
, FILE_WRITE_DATA
, &fd
, NULL
)))
3113 status
= WS2_transmitfile_base( fd
, wsa
);
3114 wine_server_release_fd( wsa
->write
.hSocket
, fd
);
3116 if (status
== STATUS_PENDING
)
3120 iosb
->u
.Status
= status
;
3121 release_async_io( &wsa
->io
);
3125 /***********************************************************************
3128 static BOOL WINAPI
WS2_TransmitFile( SOCKET s
, HANDLE h
, DWORD file_bytes
, DWORD bytes_per_send
,
3129 LPOVERLAPPED overlapped
, LPTRANSMIT_FILE_BUFFERS buffers
,
3132 union generic_unix_sockaddr uaddr
;
3133 socklen_t uaddrlen
= sizeof(uaddr
);
3134 struct ws2_transmitfile_async
*wsa
;
3138 TRACE("(%lx, %p, %d, %d, %p, %p, %d)\n", s
, h
, file_bytes
, bytes_per_send
, overlapped
,
3141 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, NULL
);
3142 if (fd
== -1) return FALSE
;
3144 if (getpeername( fd
, &uaddr
.addr
, &uaddrlen
) != 0)
3146 release_sock_fd( s
, fd
);
3147 WSASetLastError( WSAENOTCONN
);
3151 FIXME("Flags are not currently supported (0x%x).\n", flags
);
3153 if (h
&& GetFileType( h
) != FILE_TYPE_DISK
)
3155 FIXME("Non-disk file handles are not currently supported.\n");
3156 release_sock_fd( s
, fd
);
3157 WSASetLastError( WSAEOPNOTSUPP
);
3161 /* set reasonable defaults when requested */
3162 if (!bytes_per_send
)
3163 bytes_per_send
= (1 << 16); /* Depends on OS version: PAGE_SIZE, 2*PAGE_SIZE, or 2^16 */
3165 if (!(wsa
= (struct ws2_transmitfile_async
*)alloc_async_io( sizeof(*wsa
) + bytes_per_send
,
3166 WS2_async_transmitfile
)))
3168 release_sock_fd( s
, fd
);
3169 WSASetLastError( WSAEFAULT
);
3173 wsa
->buffers
= *buffers
;
3175 memset(&wsa
->buffers
, 0x0, sizeof(wsa
->buffers
));
3176 wsa
->buffer
= (char *)(wsa
+ 1);
3179 wsa
->file_bytes
= file_bytes
;
3180 wsa
->bytes_per_send
= bytes_per_send
;
3182 wsa
->offset
.QuadPart
= FILE_USE_FILE_POINTER_POSITION
;
3183 wsa
->write
.hSocket
= SOCKET2HANDLE(s
);
3184 wsa
->write
.addr
= NULL
;
3185 wsa
->write
.addrlen
.val
= 0;
3186 wsa
->write
.flags
= 0;
3187 wsa
->write
.lpFlags
= &wsa
->flags
;
3188 wsa
->write
.control
= NULL
;
3189 wsa
->write
.n_iovecs
= 0;
3190 wsa
->write
.first_iovec
= 0;
3191 wsa
->write
.user_overlapped
= overlapped
;
3194 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)overlapped
;
3197 wsa
->offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3198 wsa
->offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
3199 iosb
->u
.Status
= STATUS_PENDING
;
3200 iosb
->Information
= 0;
3201 status
= register_async( ASYNC_TYPE_WRITE
, SOCKET2HANDLE(s
), &wsa
->io
,
3202 overlapped
->hEvent
, NULL
, NULL
, iosb
);
3203 if(status
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
3204 release_sock_fd( s
, fd
);
3205 WSASetLastError( NtStatusToWSAError(status
) );
3211 status
= WS2_transmitfile_base( fd
, wsa
);
3212 if (status
== STATUS_PENDING
)
3215 do_block(fd
, POLLOUT
, -1);
3216 _sync_sock_state(s
); /* let wineserver notice connection */
3219 while (status
== STATUS_PENDING
);
3220 release_sock_fd( s
, fd
);
3222 if (status
!= STATUS_SUCCESS
)
3223 WSASetLastError( NtStatusToWSAError(status
) );
3224 HeapFree( GetProcessHeap(), 0, wsa
);
3225 return (status
== STATUS_SUCCESS
);
3228 /***********************************************************************
3229 * GetAcceptExSockaddrs
3231 static void WINAPI
WS2_GetAcceptExSockaddrs(PVOID buffer
, DWORD data_size
, DWORD local_size
, DWORD remote_size
,
3232 struct WS_sockaddr
**local_addr
, LPINT local_addr_len
,
3233 struct WS_sockaddr
**remote_addr
, LPINT remote_addr_len
)
3235 char *cbuf
= buffer
;
3236 TRACE("(%p, %d, %d, %d, %p, %p, %p, %p)\n", buffer
, data_size
, local_size
, remote_size
, local_addr
,
3237 local_addr_len
, remote_addr
, remote_addr_len
);
3240 *local_addr_len
= *(int *) cbuf
;
3241 *local_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
3245 *remote_addr_len
= *(int *) cbuf
;
3246 *remote_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
3249 /***********************************************************************
3252 int WINAPI
WSASendMsg( SOCKET s
, LPWSAMSG msg
, DWORD dwFlags
, LPDWORD lpNumberOfBytesSent
,
3253 LPWSAOVERLAPPED lpOverlapped
,
3254 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3258 SetLastError( WSAEFAULT
);
3259 return SOCKET_ERROR
;
3262 return WS2_sendto( s
, msg
->lpBuffers
, msg
->dwBufferCount
, lpNumberOfBytesSent
,
3263 dwFlags
, msg
->name
, msg
->namelen
,
3264 lpOverlapped
, lpCompletionRoutine
);
3267 /***********************************************************************
3270 * Perform a receive operation that is capable of returning message
3271 * control headers. It is important to note that the WSAMSG parameter
3272 * must remain valid throughout the operation, even when an overlapped
3273 * receive is performed.
3275 static int WINAPI
WS2_WSARecvMsg( SOCKET s
, LPWSAMSG msg
, LPDWORD lpNumberOfBytesRecvd
,
3276 LPWSAOVERLAPPED lpOverlapped
,
3277 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3281 SetLastError( WSAEFAULT
);
3282 return SOCKET_ERROR
;
3285 return WS2_recv_base( s
, msg
->lpBuffers
, msg
->dwBufferCount
, lpNumberOfBytesRecvd
,
3286 &msg
->dwFlags
, msg
->name
, &msg
->namelen
,
3287 lpOverlapped
, lpCompletionRoutine
, &msg
->Control
);
3290 /***********************************************************************
3291 * interface_bind (INTERNAL)
3293 * Take bind() calls on any name corresponding to a local network adapter and restrict the given socket to
3294 * operating only on the specified interface. This restriction consists of two components:
3295 * 1) An outgoing packet restriction suggesting the egress interface for all packets.
3296 * 2) An incoming packet restriction dropping packets not meant for the interface.
3297 * If the function succeeds in placing these restrictions (returns TRUE) then the name for the bind() may
3298 * safely be changed to INADDR_ANY, permitting the transmission and receipt of broadcast packets on the
3299 * socket. This behavior is only relevant to UDP sockets and is needed for applications that expect to be able
3300 * to receive broadcast packets on a socket that is bound to a specific network interface.
3302 static BOOL
interface_bind( SOCKET s
, int fd
, struct sockaddr
*addr
)
3304 struct sockaddr_in
*in_sock
= (struct sockaddr_in
*) addr
;
3305 in_addr_t bind_addr
= in_sock
->sin_addr
.s_addr
;
3306 PIP_ADAPTER_INFO adapters
= NULL
, adapter
;
3311 if (bind_addr
== htonl(INADDR_ANY
) || bind_addr
== htonl(INADDR_LOOPBACK
))
3312 return FALSE
; /* Not binding to a network adapter, special interface binding unnecessary. */
3313 if (_get_fd_type(fd
) != SOCK_DGRAM
)
3314 return FALSE
; /* Special interface binding is only necessary for UDP datagrams. */
3315 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
3317 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
3318 if (adapters
== NULL
|| GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
3320 /* Search the IPv4 adapter list for the appropriate binding interface */
3321 for (adapter
= adapters
; adapter
!= NULL
; adapter
= adapter
->Next
)
3323 in_addr_t adapter_addr
= (in_addr_t
) inet_addr(adapter
->IpAddressList
.IpAddress
.String
);
3325 if (bind_addr
== adapter_addr
)
3327 #if defined(IP_BOUND_IF)
3328 /* IP_BOUND_IF sets both the incoming and outgoing restriction at once */
3329 if (setsockopt(fd
, IPPROTO_IP
, IP_BOUND_IF
, &adapter
->Index
, sizeof(adapter
->Index
)) != 0)
3332 #elif defined(LINUX_BOUND_IF)
3333 in_addr_t ifindex
= (in_addr_t
) htonl(adapter
->Index
);
3334 struct interface_filter specific_interface_filter
;
3335 struct sock_fprog filter_prog
;
3337 if (setsockopt(fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
)) != 0)
3338 goto cleanup
; /* Failed to suggest egress interface */
3339 specific_interface_filter
= generic_interface_filter
;
3340 specific_interface_filter
.iface_rule
.k
= adapter
->Index
;
3341 specific_interface_filter
.ip_rule
.k
= htonl(adapter_addr
);
3342 filter_prog
.len
= sizeof(generic_interface_filter
)/sizeof(struct sock_filter
);
3343 filter_prog
.filter
= (struct sock_filter
*) &specific_interface_filter
;
3344 if (setsockopt(fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
)) != 0)
3345 goto cleanup
; /* Failed to specify incoming packet filter */
3348 FIXME("Broadcast packets on interface-bound sockets are not currently supported on this platform, "
3349 "receiving broadcast packets will not work on socket %04lx.\n", s
);
3353 EnterCriticalSection(&cs_if_addr_cache
);
3354 if (if_addr_cache_size
<= adapter
->Index
)
3356 unsigned int new_size
;
3359 new_size
= max(if_addr_cache_size
* 2, adapter
->Index
+ 1);
3360 if (!(new = heap_realloc(if_addr_cache
, sizeof(*if_addr_cache
) * new_size
)))
3362 ERR("No memory.\n");
3364 LeaveCriticalSection(&cs_if_addr_cache
);
3367 memset(new + if_addr_cache_size
, 0, sizeof(*if_addr_cache
)
3368 * (new_size
- if_addr_cache_size
));
3369 if_addr_cache
= new;
3370 if_addr_cache_size
= new_size
;
3372 if (if_addr_cache
[adapter
->Index
] && if_addr_cache
[adapter
->Index
] != adapter_addr
)
3373 WARN("Adapter addr for iface index %u has changed.\n", adapter
->Index
);
3375 if_addr_cache
[adapter
->Index
] = adapter_addr
;
3376 LeaveCriticalSection(&cs_if_addr_cache
);
3381 /* Will soon be switching to INADDR_ANY: permit address reuse */
3382 if (ret
&& setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
)) == 0)
3383 TRACE("Socket %04lx bound to interface index %d\n", s
, adapter
->Index
);
3389 ERR("Failed to bind to interface, receiving broadcast packets will not work on socket %04lx.\n", s
);
3390 HeapFree(GetProcessHeap(), 0, adapters
);
3394 /***********************************************************************
3397 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
3399 int fd
= get_sock_fd( s
, 0, NULL
);
3400 int res
= SOCKET_ERROR
;
3402 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
3406 if (!name
|| (name
->sa_family
&& !supported_pf(name
->sa_family
)))
3408 SetLastError(WSAEAFNOSUPPORT
);
3412 union generic_unix_sockaddr uaddr
;
3413 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
3416 SetLastError(WSAEFAULT
);
3420 if (name
->sa_family
== WS_AF_INET
)
3422 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
3423 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
3425 /* Trying to bind to the default host interface, using
3426 * INADDR_ANY instead*/
3427 WARN("Trying to bind to magic IP address, using "
3428 "INADDR_ANY instead.\n");
3429 in4
->sin_addr
.s_addr
= htonl(INADDR_ANY
);
3431 else if (interface_bind(s
, fd
, &uaddr
.addr
))
3432 in4
->sin_addr
.s_addr
= htonl(INADDR_ANY
);
3434 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
3436 int loc_errno
= errno
;
3437 WARN("\tfailure - errno = %i\n", errno
);
3442 SetLastError(WSAEINVAL
);
3447 socklen_t optlen
= sizeof(optval
);
3448 /* Windows >= 2003 will return different results depending on
3449 * SO_REUSEADDR, WSAEACCES may be returned representing that
3450 * the socket hijacking protection prevented the bind */
3451 if (!getsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&optval
, &optlen
) && optval
)
3453 SetLastError(WSAEACCES
);
3459 SetLastError(wsaErrno());
3465 res
=0; /* success */
3469 release_sock_fd( s
, fd
);
3474 /***********************************************************************
3475 * closesocket (WS2_32.3)
3477 int WINAPI
WS_closesocket(SOCKET s
)
3479 int res
= SOCKET_ERROR
, fd
;
3482 fd
= get_sock_fd(s
, FILE_READ_DATA
, NULL
);
3485 release_sock_fd(s
, fd
);
3486 if (CloseHandle(SOCKET2HANDLE(s
)))
3491 SetLastError(WSANOTINITIALISED
);
3492 TRACE("(socket %04lx) -> %d\n", s
, res
);
3496 static int do_connect(int fd
, const struct WS_sockaddr
* name
, int namelen
)
3498 union generic_unix_sockaddr uaddr
;
3499 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
3504 if (name
->sa_family
== WS_AF_INET
)
3506 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
3507 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
3509 /* Trying to connect to magic replace-loopback address,
3510 * assuming we really want to connect to localhost */
3511 TRACE("Trying to connect to magic IP address, using "
3512 "INADDR_LOOPBACK instead.\n");
3513 in4
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
3517 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
3523 /***********************************************************************
3524 * connect (WS2_32.4)
3526 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
3528 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
3530 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
3535 int ret
= do_connect(fd
, name
, namelen
);
3537 goto connect_success
;
3539 if (ret
== WSAEINPROGRESS
)
3541 /* tell wineserver that a connection is in progress */
3542 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3544 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
3545 ret
= sock_is_blocking( s
, &is_blocking
);
3550 do_block(fd
, POLLIN
| POLLOUT
, -1);
3551 _sync_sock_state(s
); /* let wineserver notice connection */
3552 /* retrieve any error codes from it */
3553 if (!(ret
= get_sock_error(s
, FD_CONNECT_BIT
))) goto connect_success
;
3555 else ret
= WSAEWOULDBLOCK
;
3558 release_sock_fd( s
, fd
);
3561 return SOCKET_ERROR
;
3564 release_sock_fd( s
, fd
);
3565 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3566 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
3567 FD_CONNECT
|FD_WINE_LISTENING
);
3568 TRACE("\tconnected %04lx\n", s
);
3572 /***********************************************************************
3573 * WSAConnect (WS2_32.30)
3575 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
3576 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
3577 LPQOS lpSQOS
, LPQOS lpGQOS
)
3579 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
3580 FIXME("unsupported parameters!\n");
3581 return WS_connect( s
, name
, namelen
);
3584 /***********************************************************************
3587 static BOOL WINAPI
WS2_ConnectEx(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
3588 PVOID sendBuf
, DWORD sendBufLen
, LPDWORD sent
, LPOVERLAPPED ov
)
3590 int fd
, ret
, status
;
3594 SetLastError( ERROR_INVALID_PARAMETER
);
3598 fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
3599 if (fd
== -1) return FALSE
;
3601 TRACE("socket %04lx, ptr %p %s, length %d, sendptr %p, len %d, ov %p\n",
3602 s
, name
, debugstr_sockaddr(name
), namelen
, sendBuf
, sendBufLen
, ov
);
3604 ret
= is_fd_bound(fd
, NULL
, NULL
);
3607 SetLastError(ret
== -1 ? wsaErrno() : WSAEINVAL
);
3608 release_sock_fd( s
, fd
);
3612 ret
= do_connect(fd
, name
, namelen
);
3617 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3618 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
3619 FD_CONNECT
|FD_WINE_LISTENING
);
3621 wsabuf
.len
= sendBufLen
;
3622 wsabuf
.buf
= (char*) sendBuf
;
3624 /* WSASend takes care of completion if need be */
3625 if (WSASend(s
, &wsabuf
, sendBuf
? 1 : 0, sent
, 0, ov
, NULL
) != SOCKET_ERROR
)
3626 goto connection_success
;
3628 else if (ret
== WSAEINPROGRESS
)
3630 struct ws2_async
*wsa
;
3631 ULONG_PTR cvalue
= (((ULONG_PTR
)ov
->hEvent
& 1) == 0) ? (ULONG_PTR
)ov
: 0;
3633 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3635 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
3637 /* Indirectly call WSASend */
3638 if (!(wsa
= (struct ws2_async
*)alloc_async_io( offsetof( struct ws2_async
, iovec
[1] ), WS2_async_send
)))
3640 SetLastError(WSAEFAULT
);
3644 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)ov
;
3645 iosb
->u
.Status
= STATUS_PENDING
;
3646 iosb
->Information
= 0;
3648 wsa
->hSocket
= SOCKET2HANDLE(s
);
3650 wsa
->addrlen
.val
= 0;
3652 wsa
->lpFlags
= &wsa
->flags
;
3653 wsa
->control
= NULL
;
3654 wsa
->n_iovecs
= sendBuf
? 1 : 0;
3655 wsa
->first_iovec
= 0;
3656 wsa
->completion_func
= NULL
;
3657 wsa
->iovec
[0].iov_base
= sendBuf
;
3658 wsa
->iovec
[0].iov_len
= sendBufLen
;
3660 status
= register_async( ASYNC_TYPE_WRITE
, wsa
->hSocket
, &wsa
->io
, ov
->hEvent
,
3661 NULL
, (void *)cvalue
, iosb
);
3662 if (status
!= STATUS_PENDING
) HeapFree(GetProcessHeap(), 0, wsa
);
3664 /* If the connect already failed */
3665 if (status
== STATUS_PIPE_DISCONNECTED
)
3667 ov
->Internal
= sock_error_to_ntstatus( get_sock_error( s
, FD_CONNECT_BIT
));
3668 ov
->InternalHigh
= 0;
3669 if (cvalue
) WS_AddCompletion( s
, cvalue
, ov
->Internal
, ov
->InternalHigh
, FALSE
);
3670 if (ov
->hEvent
) NtSetEvent( ov
->hEvent
, NULL
);
3671 status
= STATUS_PENDING
;
3673 SetLastError( NtStatusToWSAError(status
) );
3681 release_sock_fd( s
, fd
);
3685 release_sock_fd( s
, fd
);
3689 /***********************************************************************
3692 static BOOL WINAPI
WS2_DisconnectEx( SOCKET s
, LPOVERLAPPED ov
, DWORD flags
, DWORD reserved
)
3694 TRACE( "socket %04lx, ov %p, flags 0x%x, reserved 0x%x\n", s
, ov
, flags
, reserved
);
3696 if (flags
& TF_REUSE_SOCKET
)
3697 FIXME( "Reusing socket not supported yet\n" );
3701 ov
->Internal
= STATUS_PENDING
;
3702 ov
->InternalHigh
= 0;
3705 return !WS_shutdown( s
, SD_BOTH
);
3708 /***********************************************************************
3709 * getpeername (WS2_32.5)
3711 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
3716 TRACE("socket %04lx, ptr %p, len %08x\n", s
, name
, namelen
? *namelen
: 0);
3718 fd
= get_sock_fd( s
, 0, NULL
);
3723 union generic_unix_sockaddr uaddr
;
3724 socklen_t uaddrlen
= sizeof(uaddr
);
3726 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) == 0)
3728 if (!name
|| !namelen
)
3729 SetLastError(WSAEFAULT
);
3730 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
3731 /* The buffer was too small */
3732 SetLastError(WSAEFAULT
);
3736 TRACE("=> %s\n", debugstr_sockaddr(name
));
3740 SetLastError(wsaErrno());
3741 release_sock_fd( s
, fd
);
3746 /* When binding to an UDP address with filter support the getsockname call on the socket
3747 * will always return 0.0.0.0 instead of the filtered interface address. This function
3748 * checks if the socket is interface-bound on UDP and return the correct address.
3749 * This is required because applications often do a bind() with port zero followed by a
3750 * getsockname() to retrieve the port and address acquired.
3752 static void interface_bind_check(int fd
, struct sockaddr_in
*addr
)
3754 #if !defined(IP_BOUND_IF) && !defined(LINUX_BOUND_IF)
3757 unsigned int ifindex
;
3761 /* Check for IPv4, address 0.0.0.0 and UDP socket */
3762 if (addr
->sin_family
!= AF_INET
|| addr
->sin_addr
.s_addr
!= 0)
3764 if (_get_fd_type(fd
) != SOCK_DGRAM
)
3767 len
= sizeof(ifindex
);
3768 #if defined(IP_BOUND_IF)
3769 ret
= getsockopt(fd
, IPPROTO_IP
, IP_BOUND_IF
, &ifindex
, &len
);
3770 #elif defined(LINUX_BOUND_IF)
3771 ret
= getsockopt(fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, &len
);
3772 if (!ret
) ifindex
= ntohl(ifindex
);
3774 if (!ret
&& ifindex
)
3776 EnterCriticalSection(&cs_if_addr_cache
);
3777 if (ifindex
< if_addr_cache_size
)
3778 addr
->sin_addr
.s_addr
= if_addr_cache
[ifindex
];
3780 ERR("No cache entry for ifindex %u.\n", ifindex
);
3781 LeaveCriticalSection(&cs_if_addr_cache
);
3786 /***********************************************************************
3787 * getsockname (WS2_32.6)
3789 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
3794 TRACE("socket %04lx, ptr %p, len %08x\n", s
, name
, namelen
? *namelen
: 0);
3796 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
3797 if( (name
== NULL
) || (namelen
== NULL
) )
3799 SetLastError( WSAEFAULT
);
3800 return SOCKET_ERROR
;
3803 fd
= get_sock_fd( s
, 0, NULL
);
3808 union generic_unix_sockaddr uaddr
;
3810 int bound
= is_fd_bound(fd
, &uaddr
, &uaddrlen
);
3814 SetLastError(bound
== -1 ? wsaErrno() : WSAEINVAL
);
3816 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
3818 /* The buffer was too small */
3819 SetLastError(WSAEFAULT
);
3823 interface_bind_check(fd
, (struct sockaddr_in
*) &uaddr
);
3824 if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
3826 /* The buffer was too small */
3827 SetLastError(WSAEFAULT
);
3832 TRACE("=> %s\n", debugstr_sockaddr(name
));
3835 release_sock_fd( s
, fd
);
3840 /***********************************************************************
3841 * getsockopt (WS2_32.7)
3843 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
3844 INT optname
, char *optval
, INT
*optlen
)
3849 TRACE("(socket %04lx, %s, optval %s, optlen %p (%d))\n", s
,
3850 debugstr_sockopt(level
, optname
), debugstr_optval(optval
, 0),
3851 optlen
, optlen
? *optlen
: 0);
3859 /* Handle common cases. The special cases are below, sorted
3861 case WS_SO_BROADCAST
:
3863 case WS_SO_KEEPALIVE
:
3864 case WS_SO_OOBINLINE
:
3866 case WS_SO_REUSEADDR
:
3868 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3869 return SOCKET_ERROR
;
3870 convert_sockopt(&level
, &optname
);
3871 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
3873 SetLastError(wsaErrno());
3876 release_sock_fd( s
, fd
);
3878 case WS_SO_ACCEPTCONN
:
3879 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3880 return SOCKET_ERROR
;
3881 if (getsockopt(fd
, SOL_SOCKET
, SO_ACCEPTCONN
, optval
, (socklen_t
*)optlen
) != 0 )
3883 SetLastError(wsaErrno());
3888 /* BSD returns != 0 while Windows return exact == 1 */
3889 if (*(int *)optval
) *(int *)optval
= 1;
3891 release_sock_fd( s
, fd
);
3893 case WS_SO_BSP_STATE
:
3895 int req_size
, addr_size
;
3896 WSAPROTOCOL_INFOW infow
;
3897 CSADDR_INFO
*csinfo
;
3899 ret
= ws_protocol_info(s
, TRUE
, &infow
, &addr_size
);
3902 if (infow
.iAddressFamily
== WS_AF_INET
)
3903 addr_size
= sizeof(struct sockaddr_in
);
3904 else if (infow
.iAddressFamily
== WS_AF_INET6
)
3905 addr_size
= sizeof(struct sockaddr_in6
);
3908 FIXME("Family %d is unsupported for SO_BSP_STATE\n", infow
.iAddressFamily
);
3909 SetLastError(WSAEAFNOSUPPORT
);
3910 return SOCKET_ERROR
;
3913 req_size
= sizeof(CSADDR_INFO
) + addr_size
* 2;
3914 if (*optlen
< req_size
)
3917 SetLastError(WSAEFAULT
);
3921 union generic_unix_sockaddr uaddr
;
3924 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3925 return SOCKET_ERROR
;
3927 csinfo
= (CSADDR_INFO
*) optval
;
3929 /* Check if the sock is bound */
3930 if (is_fd_bound(fd
, &uaddr
, &uaddrlen
) == 1)
3932 csinfo
->LocalAddr
.lpSockaddr
=
3933 (LPSOCKADDR
) (optval
+ sizeof(CSADDR_INFO
));
3934 ws_sockaddr_u2ws(&uaddr
.addr
, csinfo
->LocalAddr
.lpSockaddr
, &addr_size
);
3935 csinfo
->LocalAddr
.iSockaddrLength
= addr_size
;
3939 csinfo
->LocalAddr
.lpSockaddr
= NULL
;
3940 csinfo
->LocalAddr
.iSockaddrLength
= 0;
3943 /* Check if the sock is connected */
3944 if (!getpeername(fd
, &uaddr
.addr
, &uaddrlen
) &&
3945 is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
3947 csinfo
->RemoteAddr
.lpSockaddr
=
3948 (LPSOCKADDR
) (optval
+ sizeof(CSADDR_INFO
) + addr_size
);
3949 ws_sockaddr_u2ws(&uaddr
.addr
, csinfo
->RemoteAddr
.lpSockaddr
, &addr_size
);
3950 csinfo
->RemoteAddr
.iSockaddrLength
= addr_size
;
3954 csinfo
->RemoteAddr
.lpSockaddr
= NULL
;
3955 csinfo
->RemoteAddr
.iSockaddrLength
= 0;
3958 csinfo
->iSocketType
= infow
.iSocketType
;
3959 csinfo
->iProtocol
= infow
.iProtocol
;
3960 release_sock_fd( s
, fd
);
3963 return ret
? 0 : SOCKET_ERROR
;
3965 case WS_SO_DONTLINGER
:
3967 struct linger lingval
;
3968 socklen_t len
= sizeof(struct linger
);
3970 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
3972 SetLastError(WSAEFAULT
);
3973 return SOCKET_ERROR
;
3975 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3976 return SOCKET_ERROR
;
3978 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
3980 SetLastError(wsaErrno());
3985 *(BOOL
*)optval
= !lingval
.l_onoff
;
3986 *optlen
= sizeof(BOOL
);
3989 release_sock_fd( s
, fd
);
3993 case WS_SO_CONNECT_TIME
:
3995 static int pretendtime
= 0;
3996 struct WS_sockaddr addr
;
3997 int len
= sizeof(addr
);
3999 if (!optlen
|| *optlen
< sizeof(DWORD
) || !optval
)
4001 SetLastError(WSAEFAULT
);
4002 return SOCKET_ERROR
;
4004 if (WS_getpeername(s
, &addr
, &len
) == SOCKET_ERROR
)
4005 *(DWORD
*)optval
= ~0u;
4008 if (!pretendtime
) FIXME("WS_SO_CONNECT_TIME - faking results\n");
4009 *(DWORD
*)optval
= pretendtime
++;
4011 *optlen
= sizeof(DWORD
);
4014 /* As mentioned in setsockopt, Windows ignores this, so we
4015 * always return true here */
4016 case WS_SO_DONTROUTE
:
4017 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
4019 SetLastError(WSAEFAULT
);
4020 return SOCKET_ERROR
;
4022 *(BOOL
*)optval
= TRUE
;
4023 *optlen
= sizeof(BOOL
);
4028 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4029 return SOCKET_ERROR
;
4030 if (getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, optval
, (socklen_t
*)optlen
) != 0 )
4032 SetLastError(wsaErrno());
4035 release_sock_fd( s
, fd
);
4037 /* The wineserver may have swallowed the error before us */
4038 if (!ret
&& *(int*) optval
== 0)
4040 int i
, events
[FD_MAX_EVENTS
];
4041 _get_sock_errors(s
, events
);
4042 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
4046 TRACE("returning SO_ERROR %d from wine server\n", events
[i
]);
4047 *(int*) optval
= events
[i
];
4057 struct linger lingval
;
4058 socklen_t len
= sizeof(struct linger
);
4060 /* struct linger and LINGER have different sizes */
4061 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
4063 SetLastError(WSAEFAULT
);
4064 return SOCKET_ERROR
;
4066 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4067 return SOCKET_ERROR
;
4069 if (_get_fd_type(fd
) == SOCK_DGRAM
)
4071 SetLastError(WSAENOPROTOOPT
);
4074 else if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0)
4076 SetLastError(wsaErrno());
4081 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
4082 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
4083 *optlen
= sizeof(struct linger
);
4086 release_sock_fd( s
, fd
);
4090 case WS_SO_MAX_MSG_SIZE
:
4091 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
4093 SetLastError(WSAEFAULT
);
4094 return SOCKET_ERROR
;
4096 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
4097 *(int *)optval
= 65507;
4098 *optlen
= sizeof(int);
4101 /* SO_OPENTYPE does not require a valid socket handle. */
4102 case WS_SO_OPENTYPE
:
4103 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
4105 SetLastError(WSAEFAULT
);
4106 return SOCKET_ERROR
;
4108 *(int *)optval
= get_per_thread_data()->opentype
;
4109 *optlen
= sizeof(int);
4110 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
4112 case WS_SO_PROTOCOL_INFOA
:
4113 case WS_SO_PROTOCOL_INFOW
:
4116 WSAPROTOCOL_INFOW infow
;
4118 ret
= ws_protocol_info(s
, optname
== WS_SO_PROTOCOL_INFOW
, &infow
, &size
);
4121 if (!optlen
|| !optval
|| *optlen
< size
)
4123 if(optlen
) *optlen
= size
;
4125 SetLastError(WSAEFAULT
);
4128 memcpy(optval
, &infow
, size
);
4130 return ret
? 0 : SOCKET_ERROR
;
4132 case WS_SO_RCVTIMEO
:
4133 case WS_SO_SNDTIMEO
:
4137 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
4139 SetLastError(WSAEFAULT
);
4140 return SOCKET_ERROR
;
4142 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4143 return SOCKET_ERROR
;
4145 timeout
= get_rcvsnd_timeo(fd
, optname
== WS_SO_RCVTIMEO
);
4146 *(int *)optval
= timeout
<= UINT_MAX
? timeout
: UINT_MAX
;
4148 release_sock_fd( s
, fd
);
4154 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
4156 SetLastError(WSAEFAULT
);
4157 return SOCKET_ERROR
;
4159 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4160 return SOCKET_ERROR
;
4162 sock_type
= _get_fd_type(fd
);
4163 if (sock_type
== -1)
4165 SetLastError(wsaErrno());
4169 (*(int *)optval
) = convert_socktype_u2w(sock_type
);
4171 release_sock_fd( s
, fd
);
4175 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
4176 SetLastError(WSAENOPROTOOPT
);
4177 return SOCKET_ERROR
;
4178 } /* end switch(optname) */
4179 }/* end case WS_SOL_SOCKET */
4181 case WS_NSPROTO_IPX
:
4183 struct WS_sockaddr_ipx addr
;
4184 IPX_ADDRESS_DATA
*data
;
4189 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
4191 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (socklen_t
*)optlen
) == -1)
4198 socklen_t len
=sizeof(struct ipx
);
4199 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
4202 *optval
= (int)val
.ipx_pt
;
4205 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
4206 release_sock_fd( s
, fd
);
4209 case WS_IPX_ADDRESS
:
4211 * On a Win2000 system with one network card there are usually
4212 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
4213 * Using this call you can then retrieve info about this all.
4214 * In case of Linux it is a bit different. Usually you have
4215 * only "one" device active and further it is not possible to
4216 * query things like the linkspeed.
4218 FIXME("IPX_ADDRESS\n");
4219 namelen
= sizeof(struct WS_sockaddr_ipx
);
4220 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
4221 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
4223 data
= (IPX_ADDRESS_DATA
*)optval
;
4224 memcpy(data
->nodenum
,addr
.sa_nodenum
,sizeof(data
->nodenum
));
4225 memcpy(data
->netnum
,addr
.sa_netnum
,sizeof(data
->netnum
));
4226 data
->adapternum
= 0;
4227 data
->wan
= FALSE
; /* We are not on a wan for now .. */
4228 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
4229 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
4230 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
4231 * note 1MB = 1000kB in this case */
4234 case WS_IPX_MAX_ADAPTER_NUM
:
4235 FIXME("IPX_MAX_ADAPTER_NUM\n");
4236 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
4240 FIXME("IPX optname:%x\n", optname
);
4241 return SOCKET_ERROR
;
4242 }/* end switch(optname) */
4243 } /* end case WS_NSPROTO_IPX */
4247 #define MAX_IRDA_DEVICES 10
4252 case WS_IRLMP_ENUMDEVICES
:
4254 char buf
[sizeof(struct irda_device_list
) +
4255 (MAX_IRDA_DEVICES
- 1) * sizeof(struct irda_device_info
)];
4257 socklen_t len
= sizeof(buf
);
4259 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4260 return SOCKET_ERROR
;
4261 res
= getsockopt( fd
, SOL_IRLMP
, IRLMP_ENUMDEVICES
, buf
, &len
);
4262 release_sock_fd( s
, fd
);
4265 SetLastError(wsaErrno());
4266 return SOCKET_ERROR
;
4270 struct irda_device_list
*src
= (struct irda_device_list
*)buf
;
4271 DEVICELIST
*dst
= (DEVICELIST
*)optval
;
4272 INT needed
= sizeof(DEVICELIST
);
4276 needed
+= (src
->len
- 1) * sizeof(IRDA_DEVICE_INFO
);
4277 if (*optlen
< needed
)
4279 SetLastError(WSAEFAULT
);
4280 return SOCKET_ERROR
;
4283 TRACE("IRLMP_ENUMDEVICES: %d devices found:\n", src
->len
);
4284 dst
->numDevice
= src
->len
;
4285 for (i
= 0; i
< src
->len
; i
++)
4287 TRACE("saddr = %08x, daddr = %08x, info = %s, hints = %02x%02x\n",
4288 src
->dev
[i
].saddr
, src
->dev
[i
].daddr
,
4289 src
->dev
[i
].info
, src
->dev
[i
].hints
[0],
4290 src
->dev
[i
].hints
[1]);
4291 memcpy( dst
->Device
[i
].irdaDeviceID
,
4293 sizeof(dst
->Device
[i
].irdaDeviceID
) ) ;
4294 memcpy( dst
->Device
[i
].irdaDeviceName
,
4296 sizeof(dst
->Device
[i
].irdaDeviceName
) ) ;
4297 memcpy( &dst
->Device
[i
].irdaDeviceHints1
,
4298 &src
->dev
[i
].hints
[0],
4299 sizeof(dst
->Device
[i
].irdaDeviceHints1
) ) ;
4300 memcpy( &dst
->Device
[i
].irdaDeviceHints2
,
4301 &src
->dev
[i
].hints
[1],
4302 sizeof(dst
->Device
[i
].irdaDeviceHints2
) ) ;
4303 dst
->Device
[i
].irdaCharSet
= src
->dev
[i
].charset
;
4309 FIXME("IrDA optname:0x%x\n", optname
);
4310 return SOCKET_ERROR
;
4312 break; /* case WS_SOL_IRLMP */
4313 #undef MAX_IRDA_DEVICES
4316 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
4317 case WS_IPPROTO_TCP
:
4320 case WS_TCP_NODELAY
:
4321 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4322 return SOCKET_ERROR
;
4323 convert_sockopt(&level
, &optname
);
4324 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
4326 SetLastError(wsaErrno());
4329 release_sock_fd( s
, fd
);
4332 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
4333 return SOCKET_ERROR
;
4338 case WS_IP_ADD_MEMBERSHIP
:
4339 case WS_IP_DROP_MEMBERSHIP
:
4343 case WS_IP_MULTICAST_IF
:
4344 case WS_IP_MULTICAST_LOOP
:
4345 case WS_IP_MULTICAST_TTL
:
4347 #if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
4352 #ifdef IP_UNICAST_IF
4353 case WS_IP_UNICAST_IF
:
4355 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4356 return SOCKET_ERROR
;
4357 convert_sockopt(&level
, &optname
);
4358 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
4360 SetLastError(wsaErrno());
4363 release_sock_fd( s
, fd
);
4365 case WS_IP_DONTFRAGMENT
:
4366 return get_dont_fragment(s
, IPPROTO_IP
, (BOOL
*)optval
) ? 0 : SOCKET_ERROR
;
4368 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
4369 return SOCKET_ERROR
;
4371 case WS_IPPROTO_IPV6
:
4374 #ifdef IPV6_ADD_MEMBERSHIP
4375 case WS_IPV6_ADD_MEMBERSHIP
:
4377 #ifdef IPV6_DROP_MEMBERSHIP
4378 case WS_IPV6_DROP_MEMBERSHIP
:
4380 case WS_IPV6_MULTICAST_IF
:
4381 case WS_IPV6_MULTICAST_HOPS
:
4382 case WS_IPV6_MULTICAST_LOOP
:
4383 case WS_IPV6_UNICAST_HOPS
:
4384 case WS_IPV6_V6ONLY
:
4385 #ifdef IPV6_UNICAST_IF
4386 case WS_IPV6_UNICAST_IF
:
4388 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4389 return SOCKET_ERROR
;
4390 convert_sockopt(&level
, &optname
);
4391 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
4393 SetLastError(wsaErrno());
4396 release_sock_fd( s
, fd
);
4398 case WS_IPV6_DONTFRAG
:
4399 return get_dont_fragment(s
, IPPROTO_IPV6
, (BOOL
*)optval
) ? 0 : SOCKET_ERROR
;
4401 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
4402 return SOCKET_ERROR
;
4405 WARN("Unknown level: 0x%08x\n", level
);
4406 SetLastError(WSAEINVAL
);
4407 return SOCKET_ERROR
;
4408 } /* end switch(level) */
4411 /***********************************************************************
4414 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
4416 return htonl(hostlong
);
4420 /***********************************************************************
4423 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
4425 return htons(hostshort
);
4428 /***********************************************************************
4429 * WSAHtonl (WS2_32.46)
4430 * From MSDN description of error codes, this function should also
4431 * check if WinSock has been initialized and the socket is a valid
4432 * socket. But why? This function only translates a host byte order
4433 * u_long into a network byte order u_long...
4435 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
4439 *lpnetlong
= htonl(hostlong
);
4442 SetLastError(WSAEFAULT
);
4443 return SOCKET_ERROR
;
4446 /***********************************************************************
4447 * WSAHtons (WS2_32.47)
4448 * From MSDN description of error codes, this function should also
4449 * check if WinSock has been initialized and the socket is a valid
4450 * socket. But why? This function only translates a host byte order
4451 * u_short into a network byte order u_short...
4453 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
4458 *lpnetshort
= htons(hostshort
);
4461 SetLastError(WSAEFAULT
);
4462 return SOCKET_ERROR
;
4466 /***********************************************************************
4467 * inet_addr (WS2_32.11)
4469 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
4471 if (!cp
) return INADDR_NONE
;
4472 return inet_addr(cp
);
4476 /***********************************************************************
4479 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
4481 return ntohl(netlong
);
4485 /***********************************************************************
4488 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
4490 return ntohs(netshort
);
4494 /***********************************************************************
4495 * inet_ntoa (WS2_32.12)
4497 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
4499 unsigned int long_ip
= ntohl(in
.WS_s_addr
);
4500 struct per_thread_data
*data
= get_per_thread_data();
4502 sprintf( data
->ntoa_buffer
, "%u.%u.%u.%u",
4503 (long_ip
>> 24) & 0xff,
4504 (long_ip
>> 16) & 0xff,
4505 (long_ip
>> 8) & 0xff,
4508 return data
->ntoa_buffer
;
4511 static const char *debugstr_wsaioctl(DWORD code
)
4513 const char *name
= NULL
, *buf_type
, *family
;
4515 #define IOCTL_NAME(x) case x: name = #x; break
4518 IOCTL_NAME(WS_FIONBIO
);
4519 IOCTL_NAME(WS_FIONREAD
);
4520 IOCTL_NAME(WS_SIOCATMARK
);
4521 /* IOCTL_NAME(WS_SIO_ACQUIRE_PORT_RESERVATION); */
4522 IOCTL_NAME(WS_SIO_ADDRESS_LIST_CHANGE
);
4523 IOCTL_NAME(WS_SIO_ADDRESS_LIST_QUERY
);
4524 IOCTL_NAME(WS_SIO_ASSOCIATE_HANDLE
);
4525 /* IOCTL_NAME(WS_SIO_ASSOCIATE_PORT_RESERVATION);
4526 IOCTL_NAME(WS_SIO_BASE_HANDLE);
4527 IOCTL_NAME(WS_SIO_BSP_HANDLE);
4528 IOCTL_NAME(WS_SIO_BSP_HANDLE_SELECT);
4529 IOCTL_NAME(WS_SIO_BSP_HANDLE_POLL);
4530 IOCTL_NAME(WS_SIO_CHK_QOS); */
4531 IOCTL_NAME(WS_SIO_ENABLE_CIRCULAR_QUEUEING
);
4532 IOCTL_NAME(WS_SIO_FIND_ROUTE
);
4533 IOCTL_NAME(WS_SIO_FLUSH
);
4534 IOCTL_NAME(WS_SIO_GET_BROADCAST_ADDRESS
);
4535 IOCTL_NAME(WS_SIO_GET_EXTENSION_FUNCTION_POINTER
);
4536 IOCTL_NAME(WS_SIO_GET_GROUP_QOS
);
4537 IOCTL_NAME(WS_SIO_GET_INTERFACE_LIST
);
4538 /* IOCTL_NAME(WS_SIO_GET_INTERFACE_LIST_EX); */
4539 IOCTL_NAME(WS_SIO_GET_QOS
);
4540 IOCTL_NAME(WS_SIO_IDEAL_SEND_BACKLOG_CHANGE
);
4541 IOCTL_NAME(WS_SIO_IDEAL_SEND_BACKLOG_QUERY
);
4542 IOCTL_NAME(WS_SIO_KEEPALIVE_VALS
);
4543 IOCTL_NAME(WS_SIO_MULTIPOINT_LOOPBACK
);
4544 IOCTL_NAME(WS_SIO_MULTICAST_SCOPE
);
4545 /* IOCTL_NAME(WS_SIO_QUERY_RSS_SCALABILITY_INFO);
4546 IOCTL_NAME(WS_SIO_QUERY_WFP_ALE_ENDPOINT_HANDLE); */
4547 IOCTL_NAME(WS_SIO_RCVALL
);
4548 IOCTL_NAME(WS_SIO_RCVALL_IGMPMCAST
);
4549 IOCTL_NAME(WS_SIO_RCVALL_MCAST
);
4550 /* IOCTL_NAME(WS_SIO_RELEASE_PORT_RESERVATION); */
4551 IOCTL_NAME(WS_SIO_ROUTING_INTERFACE_CHANGE
);
4552 IOCTL_NAME(WS_SIO_ROUTING_INTERFACE_QUERY
);
4553 IOCTL_NAME(WS_SIO_SET_COMPATIBILITY_MODE
);
4554 IOCTL_NAME(WS_SIO_SET_GROUP_QOS
);
4555 IOCTL_NAME(WS_SIO_SET_QOS
);
4556 IOCTL_NAME(WS_SIO_TRANSLATE_HANDLE
);
4557 IOCTL_NAME(WS_SIO_UDP_CONNRESET
);
4564 /* If this is not a known code split its bits */
4565 switch(code
& 0x18000000)
4570 case WS_IOC_PROTOCOL
:
4571 family
= "IOC_PROTOCOL";
4574 family
= "IOC_VENDOR";
4576 default: /* WS_IOC_UNIX */
4578 BYTE size
= (code
>> 16) & WS_IOCPARM_MASK
;
4579 char x
= (code
& 0xff00) >> 8;
4580 BYTE y
= code
& 0xff;
4583 switch (code
& (WS_IOC_VOID
|WS_IOC_INOUT
))
4587 sprintf(args
, "%d, %d", x
, y
);
4591 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
4595 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
4599 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
4602 return wine_dbg_sprintf("%s(%s)", buf_type
, args
);
4606 /* We are different from WS_IOC_UNIX. */
4607 switch (code
& (WS_IOC_VOID
|WS_IOC_INOUT
))
4610 buf_type
= "_WSAIO";
4613 buf_type
= "_WSAIORW";
4616 buf_type
= "_WSAIOW";
4619 buf_type
= "_WSAIOR";
4626 return wine_dbg_sprintf("%s(%s, %d)", buf_type
, family
,
4627 (USHORT
)(code
& 0xffff));
4630 /* do an ioctl call through the server */
4631 static DWORD
server_ioctl_sock( SOCKET s
, DWORD code
, LPVOID in_buff
, DWORD in_size
,
4632 LPVOID out_buff
, DWORD out_size
, LPDWORD ret_size
,
4633 LPWSAOVERLAPPED overlapped
,
4634 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
4636 HANDLE event
= overlapped
? overlapped
->hEvent
: 0;
4637 HANDLE handle
= SOCKET2HANDLE( s
);
4638 struct ws2_async
*wsa
= NULL
;
4639 IO_STATUS_BLOCK
*io
= (PIO_STATUS_BLOCK
)overlapped
, iosb
;
4640 void *cvalue
= NULL
;
4645 if (!(wsa
= (struct ws2_async
*)alloc_async_io( sizeof(*wsa
), NULL
)))
4646 return WSA_NOT_ENOUGH_MEMORY
;
4647 wsa
->hSocket
= handle
;
4648 wsa
->user_overlapped
= overlapped
;
4649 wsa
->completion_func
= completion
;
4650 if (!io
) io
= &wsa
->local_iosb
;
4655 else if (!((ULONG_PTR
)overlapped
->hEvent
& 1))
4656 cvalue
= overlapped
;
4658 status
= NtDeviceIoControlFile( handle
, event
, wsa
? ws2_async_apc
: NULL
, cvalue
, io
, code
,
4659 in_buff
, in_size
, out_buff
, out_size
);
4660 if (status
== STATUS_NOT_SUPPORTED
)
4662 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
4663 code
, code
>> 16, (code
>> 14) & 3, (code
>> 2) & 0xfff, code
& 3);
4665 else if (status
== STATUS_SUCCESS
)
4666 *ret_size
= io
->Information
; /* "Information" is the size written to the output buffer */
4668 if (status
!= STATUS_PENDING
) RtlFreeHeap( GetProcessHeap(), 0, wsa
);
4670 return NtStatusToWSAError( status
);
4673 /**********************************************************************
4674 * WSAIoctl (WS2_32.50)
4677 INT WINAPI
WSAIoctl(SOCKET s
, DWORD code
, LPVOID in_buff
, DWORD in_size
, LPVOID out_buff
,
4678 DWORD out_size
, LPDWORD ret_size
, LPWSAOVERLAPPED overlapped
,
4679 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
4682 DWORD status
= 0, total
= 0;
4684 TRACE("%04lx, %s, %p, %d, %p, %d, %p, %p, %p\n",
4685 s
, debugstr_wsaioctl(code
), in_buff
, in_size
, out_buff
, out_size
, ret_size
, overlapped
, completion
);
4690 if (in_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(in_buff
))
4692 SetLastError(WSAEFAULT
);
4693 return SOCKET_ERROR
;
4695 TRACE("-> FIONBIO (%x)\n", *(WS_u_long
*)in_buff
);
4696 if (_get_sock_mask(s
))
4698 /* AsyncSelect()'ed sockets are always nonblocking */
4699 if (!*(WS_u_long
*)in_buff
) status
= WSAEINVAL
;
4702 if (*(WS_u_long
*)in_buff
)
4703 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
4705 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
4712 socklen_t len
= sizeof(listening
);
4714 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
4716 SetLastError(WSAEFAULT
);
4717 return SOCKET_ERROR
;
4719 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
4722 /* On Linux, FIONREAD on listening socket always fails (see tcp(7)).
4723 However, it succeeds on native. */
4724 if (!getsockopt( fd
, SOL_SOCKET
, SO_ACCEPTCONN
, &listening
, &len
) && listening
)
4725 (*(WS_u_long
*) out_buff
) = 0;
4728 if (ioctl(fd
, FIONREAD
, out_buff
) == -1)
4729 status
= wsaErrno();
4730 release_sock_fd( s
, fd
);
4736 unsigned int oob
= 0, atmark
= 0;
4737 socklen_t oobsize
= sizeof(int);
4738 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
4740 SetLastError(WSAEFAULT
);
4741 return SOCKET_ERROR
;
4743 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
4744 /* SO_OOBINLINE sockets must always return TRUE to SIOCATMARK */
4745 if ((getsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &oob
, &oobsize
) == -1)
4746 || (!oob
&& ioctl(fd
, SIOCATMARK
, &atmark
) == -1))
4747 status
= wsaErrno();
4750 /* The SIOCATMARK value read from ioctl() is reversed
4751 * because BSD returns TRUE if it's in the OOB mark
4752 * while Windows returns TRUE if there are NO OOB bytes.
4754 (*(WS_u_long
*) out_buff
) = oob
|| !atmark
;
4757 release_sock_fd( s
, fd
);
4762 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
4763 SetLastError(WSAEINVAL
);
4764 return SOCKET_ERROR
;
4766 case WS_SIO_GET_INTERFACE_LIST
:
4768 INTERFACE_INFO
* intArray
= out_buff
;
4769 DWORD size
, numInt
= 0, apiReturn
;
4771 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
4773 if (!out_buff
|| !ret_size
)
4775 SetLastError(WSAEFAULT
);
4776 return SOCKET_ERROR
;
4779 fd
= get_sock_fd( s
, 0, NULL
);
4780 if (fd
== -1) return SOCKET_ERROR
;
4782 apiReturn
= GetAdaptersInfo(NULL
, &size
);
4783 if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
4785 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
4789 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
4791 PIP_ADAPTER_INFO ptr
;
4793 for (ptr
= table
, numInt
= 0; ptr
; ptr
= ptr
->Next
)
4795 unsigned int addr
, mask
, bcast
;
4796 struct ifreq ifInfo
;
4798 /* Skip interfaces without an IPv4 address. */
4799 if (ptr
->IpAddressList
.IpAddress
.String
[0] == '\0')
4802 if ((numInt
+ 1)*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > out_size
)
4804 WARN("Buffer too small = %u, out_size = %u\n", numInt
+ 1, out_size
);
4809 /* Socket Status Flags */
4810 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
4811 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
4813 ERR("Error obtaining status flags for socket!\n");
4819 /* set flags; the values of IFF_* are not the same
4820 under Linux and Windows, therefore must generate
4822 intArray
->iiFlags
= 0;
4823 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
4824 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
4825 #ifdef IFF_POINTOPOINT
4826 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
4827 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
4829 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
4830 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
4831 if (ifInfo
.ifr_flags
& IFF_UP
)
4832 intArray
->iiFlags
|= WS_IFF_UP
;
4833 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
4834 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
4837 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
4838 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
4839 bcast
= addr
| ~mask
;
4840 intArray
->iiAddress
.AddressIn
.sin_family
= WS_AF_INET
;
4841 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
4842 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
= addr
;
4844 intArray
->iiNetmask
.AddressIn
.sin_family
= WS_AF_INET
;
4845 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
4846 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
= mask
;
4848 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
= WS_AF_INET
;
4849 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
4850 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.WS_s_addr
= bcast
;
4857 ERR("Unable to get interface table!\n");
4860 HeapFree(GetProcessHeap(),0,table
);
4862 else status
= WSAEINVAL
;
4864 else if (apiReturn
!= ERROR_NO_DATA
)
4866 ERR("Unable to get interface table!\n");
4869 /* Calculate the size of the array being returned */
4870 total
= sizeof(INTERFACE_INFO
) * numInt
;
4871 release_sock_fd( s
, fd
);
4875 case WS_SIO_ADDRESS_LIST_QUERY
:
4879 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
4883 SetLastError(WSAEFAULT
);
4884 return SOCKET_ERROR
;
4887 if (out_size
&& out_size
< FIELD_OFFSET(SOCKET_ADDRESS_LIST
, Address
[0]))
4890 SetLastError(WSAEINVAL
);
4891 return SOCKET_ERROR
;
4894 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
4896 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
4897 SOCKET_ADDRESS_LIST
*sa_list
;
4898 SOCKADDR_IN
*sockaddr
;
4903 if (!table
|| GetAdaptersInfo(table
, &size
))
4905 HeapFree(GetProcessHeap(), 0, table
);
4910 for (p
= table
, num
= 0; p
; p
= p
->Next
)
4911 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
4913 total
= FIELD_OFFSET(SOCKET_ADDRESS_LIST
, Address
[num
]) + num
* sizeof(*sockaddr
);
4914 if (total
> out_size
|| !out_buff
)
4917 HeapFree(GetProcessHeap(), 0, table
);
4923 sa
= sa_list
->Address
;
4924 sockaddr
= (SOCKADDR_IN
*)&sa
[num
];
4925 sa_list
->iAddressCount
= num
;
4927 for (p
= table
, i
= 0; p
; p
= p
->Next
)
4929 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
4931 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
4932 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
4934 sockaddr
[i
].sin_family
= WS_AF_INET
;
4935 sockaddr
[i
].sin_port
= 0;
4936 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
4940 HeapFree(GetProcessHeap(), 0, table
);
4944 WARN("unable to get IP address list\n");
4951 FIXME("SIO_FLUSH: stub.\n");
4954 case WS_SIO_GET_EXTENSION_FUNCTION_POINTER
:
4956 #define EXTENSION_FUNCTION(x, y) { x, y, #y },
4963 EXTENSION_FUNCTION(WSAID_CONNECTEX
, WS2_ConnectEx
)
4964 EXTENSION_FUNCTION(WSAID_DISCONNECTEX
, WS2_DisconnectEx
)
4965 EXTENSION_FUNCTION(WSAID_ACCEPTEX
, WS2_AcceptEx
)
4966 EXTENSION_FUNCTION(WSAID_GETACCEPTEXSOCKADDRS
, WS2_GetAcceptExSockaddrs
)
4967 EXTENSION_FUNCTION(WSAID_TRANSMITFILE
, WS2_TransmitFile
)
4968 /* EXTENSION_FUNCTION(WSAID_TRANSMITPACKETS, WS2_TransmitPackets) */
4969 EXTENSION_FUNCTION(WSAID_WSARECVMSG
, WS2_WSARecvMsg
)
4970 EXTENSION_FUNCTION(WSAID_WSASENDMSG
, WSASendMsg
)
4972 #undef EXTENSION_FUNCTION
4976 for (i
= 0; i
< ARRAY_SIZE(guid_funcs
); i
++)
4978 if (IsEqualGUID(&guid_funcs
[i
].guid
, in_buff
))
4987 TRACE("-> got %s\n", guid_funcs
[i
].name
);
4988 *(void **)out_buff
= guid_funcs
[i
].func_ptr
;
4989 total
= sizeof(void *);
4993 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER %s: stub\n", debugstr_guid(in_buff
));
4994 status
= WSAEOPNOTSUPP
;
4997 case WS_SIO_KEEPALIVE_VALS
:
4999 struct tcp_keepalive
*k
;
5000 int keepalive
, keepidle
, keepintvl
;
5002 if (!in_buff
|| in_size
< sizeof(struct tcp_keepalive
))
5004 SetLastError(WSAEFAULT
);
5005 return SOCKET_ERROR
;
5009 keepalive
= k
->onoff
? 1 : 0;
5010 keepidle
= max( 1, (k
->keepalivetime
+ 500) / 1000 );
5011 keepintvl
= max( 1, (k
->keepaliveinterval
+ 500) / 1000 );
5013 TRACE("onoff: %d, keepalivetime: %d, keepaliveinterval: %d\n", keepalive
, keepidle
, keepintvl
);
5015 fd
= get_sock_fd(s
, 0, NULL
);
5016 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&keepalive
, sizeof(int)) == -1)
5018 #if defined(TCP_KEEPIDLE) || defined(TCP_KEEPINTVL)
5019 /* these values need to be set only if SO_KEEPALIVE is enabled */
5022 #ifndef TCP_KEEPIDLE
5023 FIXME("ignoring keepalive timeout\n");
5025 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, (void *)&keepidle
, sizeof(int)) == -1)
5029 #ifdef TCP_KEEPINTVL
5030 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, (void *)&keepintvl
, sizeof(int)) == -1)
5033 FIXME("ignoring keepalive interval\n");
5038 FIXME("ignoring keepalive interval and timeout\n");
5040 release_sock_fd(s
, fd
);
5043 case WS_SIO_ROUTING_INTERFACE_QUERY
:
5045 struct WS_sockaddr
*daddr
= (struct WS_sockaddr
*)in_buff
;
5046 struct WS_sockaddr_in
*daddr_in
= (struct WS_sockaddr_in
*)daddr
;
5047 struct WS_sockaddr_in
*saddr_in
= out_buff
;
5048 MIB_IPFORWARDROW row
;
5049 PMIB_IPADDRTABLE ipAddrTable
= NULL
;
5050 DWORD size
, i
, found_index
;
5052 TRACE("-> WS_SIO_ROUTING_INTERFACE_QUERY request\n");
5054 if (!in_buff
|| in_size
< sizeof(struct WS_sockaddr
) ||
5055 !out_buff
|| out_size
< sizeof(struct WS_sockaddr_in
) || !ret_size
)
5057 SetLastError(WSAEFAULT
);
5058 return SOCKET_ERROR
;
5060 if (daddr
->sa_family
!= WS_AF_INET
)
5062 FIXME("unsupported address family %d\n", daddr
->sa_family
);
5063 status
= WSAEAFNOSUPPORT
;
5066 if (GetBestRoute(daddr_in
->sin_addr
.S_un
.S_addr
, 0, &row
) != NOERROR
||
5067 GetIpAddrTable(NULL
, &size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
5072 ipAddrTable
= HeapAlloc(GetProcessHeap(), 0, size
);
5073 if (GetIpAddrTable(ipAddrTable
, &size
, FALSE
))
5075 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
5079 for (i
= 0, found_index
= ipAddrTable
->dwNumEntries
;
5080 i
< ipAddrTable
->dwNumEntries
; i
++)
5082 if (ipAddrTable
->table
[i
].dwIndex
== row
.dwForwardIfIndex
)
5085 if (found_index
== ipAddrTable
->dwNumEntries
)
5087 ERR("no matching IP address for interface %d\n",
5088 row
.dwForwardIfIndex
);
5089 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
5093 saddr_in
->sin_family
= WS_AF_INET
;
5094 saddr_in
->sin_addr
.S_un
.S_addr
= ipAddrTable
->table
[found_index
].dwAddr
;
5095 saddr_in
->sin_port
= 0;
5096 total
= sizeof(struct WS_sockaddr_in
);
5097 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
5100 case WS_SIO_SET_COMPATIBILITY_MODE
:
5101 TRACE("WS_SIO_SET_COMPATIBILITY_MODE ignored\n");
5102 status
= WSAEOPNOTSUPP
;
5104 case WS_SIO_UDP_CONNRESET
:
5105 FIXME("WS_SIO_UDP_CONNRESET stub\n");
5107 case 0x667e: /* Netscape tries hard to use bogus ioctl 0x667e */
5108 SetLastError(WSAEOPNOTSUPP
);
5109 return SOCKET_ERROR
;
5111 status
= WSAEOPNOTSUPP
;
5115 if (status
== WSAEOPNOTSUPP
)
5117 status
= server_ioctl_sock(s
, code
, in_buff
, in_size
, out_buff
, out_size
, &total
,
5118 overlapped
, completion
);
5119 if (status
!= WSAEOPNOTSUPP
)
5121 if (status
== 0 || status
== WSA_IO_PENDING
)
5122 TRACE("-> %s request\n", debugstr_wsaioctl(code
));
5124 ERR("-> %s request failed with status 0x%x\n", debugstr_wsaioctl(code
), status
);
5126 /* overlapped and completion operations will be handled by the server */
5131 FIXME("unsupported WS_IOCTL cmd (%s)\n", debugstr_wsaioctl(code
));
5136 FIXME( "completion routine %p not supported\n", completion
);
5138 else if (overlapped
)
5140 ULONG_PTR cvalue
= (overlapped
&& ((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)overlapped
: 0;
5141 overlapped
->Internal
= sock_error_to_ntstatus( status
);
5142 overlapped
->InternalHigh
= total
;
5143 if (cvalue
) WS_AddCompletion( HANDLE2SOCKET(s
), cvalue
, overlapped
->Internal
, total
, FALSE
);
5144 if (overlapped
->hEvent
) NtSetEvent( overlapped
->hEvent
, NULL
);
5149 if (ret_size
) *ret_size
= total
;
5152 SetLastError( status
);
5153 return SOCKET_ERROR
;
5157 /***********************************************************************
5158 * ioctlsocket (WS2_32.10)
5160 int WINAPI
WS_ioctlsocket(SOCKET s
, LONG cmd
, WS_u_long
*argp
)
5163 return WSAIoctl( s
, cmd
, argp
, sizeof(WS_u_long
), argp
, sizeof(WS_u_long
), &ret_size
, NULL
, NULL
);
5166 /***********************************************************************
5167 * listen (WS2_32.13)
5169 int WINAPI
WS_listen(SOCKET s
, int backlog
)
5171 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
), ret
= SOCKET_ERROR
;
5173 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
5176 int bound
= is_fd_bound(fd
, NULL
, NULL
);
5180 SetLastError(bound
== -1 ? wsaErrno() : WSAEINVAL
);
5182 else if (listen(fd
, backlog
) == 0)
5184 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
5186 FD_CONNECT
|FD_WINE_CONNECTED
);
5190 SetLastError(wsaErrno());
5191 release_sock_fd( s
, fd
);
5197 /***********************************************************************
5200 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
5202 DWORD n
, dwFlags
= flags
;
5208 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
5209 return SOCKET_ERROR
;
5214 /***********************************************************************
5215 * recvfrom (WS2_32.17)
5217 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
5218 struct WS_sockaddr
*from
, int *fromlen
)
5220 DWORD n
, dwFlags
= flags
;
5226 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
5227 return SOCKET_ERROR
;
5232 /* allocate a poll array for the corresponding fd sets */
5233 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
5234 const WS_fd_set
*exceptfds
, int *count_ptr
)
5236 unsigned int i
, j
= 0, count
= 0;
5238 struct per_thread_data
*ptb
= get_per_thread_data();
5240 if (readfds
) count
+= readfds
->fd_count
;
5241 if (writefds
) count
+= writefds
->fd_count
;
5242 if (exceptfds
) count
+= exceptfds
->fd_count
;
5246 SetLastError(WSAEINVAL
);
5250 /* check if the cache can hold all descriptors, if not do the resizing */
5251 if (ptb
->fd_count
< count
)
5253 if (!(fds
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(fds
[0]))))
5255 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
5258 HeapFree(GetProcessHeap(), 0, ptb
->fd_cache
);
5259 ptb
->fd_cache
= fds
;
5260 ptb
->fd_count
= count
;
5263 fds
= ptb
->fd_cache
;
5266 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
5268 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
5269 if (fds
[j
].fd
== -1) goto failed
;
5271 if (is_fd_bound(fds
[j
].fd
, NULL
, NULL
) == 1)
5273 fds
[j
].events
= POLLIN
;
5277 release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
5283 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
5285 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
5286 if (fds
[j
].fd
== -1) goto failed
;
5288 if (is_fd_bound(fds
[j
].fd
, NULL
, NULL
) == 1 ||
5289 _get_fd_type(fds
[j
].fd
) == SOCK_DGRAM
)
5291 fds
[j
].events
= POLLOUT
;
5295 release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
5301 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
5303 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
5304 if (fds
[j
].fd
== -1) goto failed
;
5306 if (is_fd_bound(fds
[j
].fd
, NULL
, NULL
) == 1)
5308 int oob_inlined
= 0;
5309 socklen_t olen
= sizeof(oob_inlined
);
5311 fds
[j
].events
= POLLHUP
;
5313 /* Check if we need to test for urgent data or not */
5314 getsockopt(fds
[j
].fd
, SOL_SOCKET
, SO_OOBINLINE
, (char*) &oob_inlined
, &olen
);
5316 fds
[j
].events
|= POLLPRI
;
5320 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
5331 for (i
= 0; i
< readfds
->fd_count
&& j
< count
; i
++, j
++)
5332 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
5334 for (i
= 0; i
< writefds
->fd_count
&& j
< count
; i
++, j
++)
5335 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
5337 for (i
= 0; i
< exceptfds
->fd_count
&& j
< count
; i
++, j
++)
5338 if (fds
[j
].fd
!= -1) release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
5342 /* release the file descriptor obtained in fd_sets_to_poll */
5343 /* must be called with the original fd_set arrays, before calling get_poll_results */
5344 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
5345 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
5347 unsigned int i
, j
= 0;
5351 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
5352 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
5356 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
5357 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
5361 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
5363 if (fds
[j
].fd
== -1) continue;
5364 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
5365 if (fds
[j
].revents
& POLLHUP
)
5367 int fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
5369 release_sock_fd( exceptfds
->fd_array
[i
], fd
);
5377 static int do_poll(struct pollfd
*pollfds
, int count
, int timeout
)
5379 struct timeval tv1
, tv2
;
5380 int ret
, torig
= timeout
;
5382 if (timeout
> 0) gettimeofday( &tv1
, 0 );
5384 while ((ret
= poll( pollfds
, count
, timeout
)) < 0)
5386 if (errno
!= EINTR
) break;
5387 if (timeout
< 0) continue;
5388 if (timeout
== 0) return 0;
5390 gettimeofday( &tv2
, 0 );
5392 tv2
.tv_sec
-= tv1
.tv_sec
;
5393 tv2
.tv_usec
-= tv1
.tv_usec
;
5394 if (tv2
.tv_usec
< 0)
5396 tv2
.tv_usec
+= 1000000;
5400 timeout
= torig
- (tv2
.tv_sec
* 1000) - (tv2
.tv_usec
+ 999) / 1000;
5401 if (timeout
<= 0) return 0;
5406 /* map the poll results back into the Windows fd sets */
5407 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
5408 const struct pollfd
*fds
)
5410 const struct pollfd
*poll_writefds
= fds
+ (readfds
? readfds
->fd_count
: 0);
5411 const struct pollfd
*poll_exceptfds
= poll_writefds
+ (writefds
? writefds
->fd_count
: 0);
5412 unsigned int i
, k
, total
= 0;
5416 for (i
= k
= 0; i
< readfds
->fd_count
; i
++)
5418 if (fds
[i
].revents
||
5419 (readfds
== writefds
&& (poll_writefds
[i
].revents
& POLLOUT
) && !(poll_writefds
[i
].revents
& POLLHUP
)) ||
5420 (readfds
== exceptfds
&& poll_exceptfds
[i
].revents
))
5421 readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
5423 readfds
->fd_count
= k
;
5426 if (writefds
&& writefds
!= readfds
)
5428 for (i
= k
= 0; i
< writefds
->fd_count
; i
++)
5430 if (((poll_writefds
[i
].revents
& POLLOUT
) && !(poll_writefds
[i
].revents
& POLLHUP
)) ||
5431 (writefds
== exceptfds
&& poll_exceptfds
[i
].revents
))
5432 writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
5434 writefds
->fd_count
= k
;
5437 if (exceptfds
&& exceptfds
!= readfds
&& exceptfds
!= writefds
)
5439 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++)
5440 if (poll_exceptfds
[i
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
5441 exceptfds
->fd_count
= k
;
5447 /***********************************************************************
5448 * select (WS2_32.18)
5450 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
5451 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
5452 const struct WS_timeval
* ws_timeout
)
5454 struct pollfd
*pollfds
;
5455 int count
, ret
, timeout
= -1;
5457 TRACE("read %p, write %p, excp %p timeout %p\n",
5458 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
5460 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)))
5461 return SOCKET_ERROR
;
5464 timeout
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
5466 ret
= do_poll(pollfds
, count
, timeout
);
5467 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
5469 if (ret
== -1) SetLastError(wsaErrno());
5470 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
5474 /***********************************************************************
5477 int WINAPI
WSAPoll(WSAPOLLFD
*wfds
, ULONG count
, int timeout
)
5480 struct pollfd
*ufds
;
5484 SetLastError(WSAEINVAL
);
5485 return SOCKET_ERROR
;
5489 SetLastError(WSAEFAULT
);
5490 return SOCKET_ERROR
;
5493 if (!(ufds
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(ufds
[0]))))
5495 SetLastError(WSAENOBUFS
);
5496 return SOCKET_ERROR
;
5499 for (i
= 0; i
< count
; i
++)
5501 ufds
[i
].fd
= get_sock_fd(wfds
[i
].fd
, 0, NULL
);
5502 ufds
[i
].events
= convert_poll_w2u(wfds
[i
].events
);
5503 ufds
[i
].revents
= 0;
5506 ret
= do_poll(ufds
, count
, timeout
);
5508 for (i
= 0; i
< count
; i
++)
5510 if (ufds
[i
].fd
!= -1)
5512 release_sock_fd(wfds
[i
].fd
, ufds
[i
].fd
);
5513 if (ufds
[i
].revents
& POLLHUP
)
5515 /* Check if the socket still exists */
5516 int fd
= get_sock_fd(wfds
[i
].fd
, 0, NULL
);
5519 wfds
[i
].revents
= WS_POLLHUP
;
5520 release_sock_fd(wfds
[i
].fd
, fd
);
5523 wfds
[i
].revents
= WS_POLLNVAL
;
5526 wfds
[i
].revents
= convert_poll_u2w(ufds
[i
].revents
);
5529 wfds
[i
].revents
= WS_POLLNVAL
;
5532 HeapFree(GetProcessHeap(), 0, ufds
);
5536 /* helper to send completion messages for client-only i/o operation case */
5537 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
,
5538 ULONG Information
, BOOL async
)
5540 SERVER_START_REQ( add_fd_completion
)
5542 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(sock
) );
5543 req
->cvalue
= CompletionValue
;
5544 req
->status
= CompletionStatus
;
5545 req
->information
= Information
;
5547 wine_server_call( req
);
5553 /***********************************************************************
5556 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
5562 wsabuf
.buf
= (char*) buf
;
5564 if ( WS2_sendto( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
5565 return SOCKET_ERROR
;
5570 /***********************************************************************
5571 * WSASend (WS2_32.72)
5573 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5574 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
5575 LPWSAOVERLAPPED lpOverlapped
,
5576 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5578 return WS2_sendto( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
5579 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
5582 /***********************************************************************
5583 * WSASendDisconnect (WS2_32.73)
5585 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
5587 return WS_shutdown( s
, SD_SEND
);
5591 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5592 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
5593 const struct WS_sockaddr
*to
, int tolen
,
5594 LPWSAOVERLAPPED lpOverlapped
,
5595 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5597 unsigned int i
, options
;
5598 int n
, fd
, err
, overlapped
, flags
;
5599 struct ws2_async
*wsa
= NULL
, localwsa
;
5600 int totalLength
= 0;
5604 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
5605 s
, lpBuffers
, dwBufferCount
, dwFlags
,
5606 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
5608 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
5609 TRACE( "fd=%d, options=%x\n", fd
, options
);
5611 if ( fd
== -1 ) return SOCKET_ERROR
;
5613 if (!lpOverlapped
&& !lpNumberOfBytesSent
)
5619 overlapped
= (lpOverlapped
|| lpCompletionRoutine
) &&
5620 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
5621 if (overlapped
|| dwBufferCount
> 1)
5623 if (!(wsa
= (struct ws2_async
*)alloc_async_io( offsetof(struct ws2_async
, iovec
[dwBufferCount
]),
5633 wsa
->hSocket
= SOCKET2HANDLE(s
);
5634 wsa
->addr
= (struct WS_sockaddr
*)to
;
5635 wsa
->addrlen
.val
= tolen
;
5636 wsa
->flags
= dwFlags
;
5637 wsa
->lpFlags
= &wsa
->flags
;
5638 wsa
->control
= NULL
;
5639 wsa
->n_iovecs
= dwBufferCount
;
5640 wsa
->first_iovec
= 0;
5641 for ( i
= 0; i
< dwBufferCount
; i
++ )
5643 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
5644 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
5645 totalLength
+= lpBuffers
[i
].len
;
5648 flags
= convert_flags(dwFlags
);
5649 n
= WS2_send( fd
, wsa
, flags
);
5650 if (n
== -1 && errno
!= EAGAIN
)
5658 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
5659 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
5661 wsa
->user_overlapped
= lpOverlapped
;
5662 wsa
->completion_func
= lpCompletionRoutine
;
5663 release_sock_fd( s
, fd
);
5665 if (n
== -1 || n
< totalLength
)
5667 iosb
->u
.Status
= STATUS_PENDING
;
5668 iosb
->Information
= n
== -1 ? 0 : n
;
5670 if (wsa
->completion_func
)
5671 err
= register_async( ASYNC_TYPE_WRITE
, wsa
->hSocket
, &wsa
->io
, NULL
,
5672 ws2_async_apc
, wsa
, iosb
);
5674 err
= register_async( ASYNC_TYPE_WRITE
, wsa
->hSocket
, &wsa
->io
, lpOverlapped
->hEvent
,
5675 NULL
, (void *)cvalue
, iosb
);
5677 /* Enable the event only after starting the async. The server will deliver it as soon as
5678 the async is done. */
5679 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
5681 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
5682 SetLastError(NtStatusToWSAError( err
));
5683 return SOCKET_ERROR
;
5686 iosb
->u
.Status
= STATUS_SUCCESS
;
5687 iosb
->Information
= n
;
5688 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= n
;
5689 if (!wsa
->completion_func
)
5691 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
, FALSE
);
5692 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
5693 HeapFree( GetProcessHeap(), 0, wsa
);
5695 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
5696 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
5697 SetLastError(ERROR_SUCCESS
);
5701 if ((err
= sock_is_blocking( s
, &is_blocking
))) goto error
;
5705 /* On a blocking non-overlapped stream socket,
5706 * sending blocks until the entire buffer is sent. */
5707 DWORD timeout_start
= GetTickCount();
5709 bytes_sent
= n
== -1 ? 0 : n
;
5711 while (wsa
->first_iovec
< wsa
->n_iovecs
)
5714 int poll_timeout
= -1;
5715 INT64 timeout
= get_rcvsnd_timeo(fd
, FALSE
);
5719 timeout
-= GetTickCount() - timeout_start
;
5720 if (timeout
< 0) poll_timeout
= 0;
5721 else poll_timeout
= timeout
<= INT_MAX
? timeout
: INT_MAX
;
5725 pfd
.events
= POLLOUT
;
5727 if (!poll_timeout
|| !poll( &pfd
, 1, poll_timeout
))
5730 goto error
; /* msdn says a timeout in send is fatal */
5733 n
= WS2_send( fd
, wsa
, flags
);
5734 if (n
== -1 && errno
!= EAGAIN
)
5744 else /* non-blocking */
5746 if (n
< totalLength
)
5747 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
5750 err
= WSAEWOULDBLOCK
;
5756 TRACE(" -> %i bytes\n", bytes_sent
);
5758 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= bytes_sent
;
5759 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
5760 release_sock_fd( s
, fd
);
5761 SetLastError(ERROR_SUCCESS
);
5765 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
5766 release_sock_fd( s
, fd
);
5767 WARN(" -> ERROR %d\n", err
);
5769 return SOCKET_ERROR
;
5772 /***********************************************************************
5773 * WSASendTo (WS2_32.74)
5775 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5776 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
5777 const struct WS_sockaddr
*to
, int tolen
,
5778 LPWSAOVERLAPPED lpOverlapped
,
5779 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5781 return WS2_sendto( s
, lpBuffers
, dwBufferCount
,
5782 lpNumberOfBytesSent
, dwFlags
,
5784 lpOverlapped
, lpCompletionRoutine
);
5787 /***********************************************************************
5788 * sendto (WS2_32.20)
5790 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
5791 const struct WS_sockaddr
*to
, int tolen
)
5797 wsabuf
.buf
= (char*) buf
;
5799 if ( WS2_sendto(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
5800 return SOCKET_ERROR
;
5805 /***********************************************************************
5806 * setsockopt (WS2_32.21)
5808 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
5809 const char *optval
, int optlen
)
5813 struct linger linger
;
5814 struct timeval tval
;
5815 struct ip_mreq_source mreq_source
;
5817 TRACE("(socket %04lx, %s, optval %s, optlen %d)\n", s
,
5818 debugstr_sockopt(level
, optname
), debugstr_optval(optval
, optlen
),
5821 /* some broken apps pass the value directly instead of a pointer to it */
5822 if(optlen
&& IS_INTRESOURCE(optval
))
5824 SetLastError(WSAEFAULT
);
5825 return SOCKET_ERROR
;
5833 /* Some options need some conversion before they can be sent to
5834 * setsockopt. The conversions are done here, then they will fall through
5835 * to the general case. Special options that are not passed to
5836 * setsockopt follow below that.*/
5838 case WS_SO_DONTLINGER
:
5841 SetLastError(WSAEFAULT
);
5842 return SOCKET_ERROR
;
5844 linger
.l_onoff
= *(const int*)optval
== 0;
5845 linger
.l_linger
= 0;
5847 optname
= SO_LINGER
;
5848 optval
= (char*)&linger
;
5849 optlen
= sizeof(struct linger
);
5855 SetLastError(WSAEFAULT
);
5856 return SOCKET_ERROR
;
5858 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
5859 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
5861 optname
= SO_LINGER
;
5862 optval
= (char*)&linger
;
5863 optlen
= sizeof(struct linger
);
5867 if (!*(const int *)optval
)
5869 FIXME("SO_SNDBUF ignoring request to disable send buffering\n");
5874 convert_sockopt(&level
, &optname
);
5878 if (*(const int*)optval
< 2048)
5880 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
5885 /* The options listed here don't need any special handling. Thanks to
5886 * the conversion happening above, options from there will fall through
5888 case WS_SO_ACCEPTCONN
:
5889 case WS_SO_BROADCAST
:
5891 case WS_SO_KEEPALIVE
:
5892 case WS_SO_OOBINLINE
:
5893 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
5894 * however, using it the BSD way fixes bug 8513 and seems to be what
5895 * most programmers assume, anyway */
5896 case WS_SO_REUSEADDR
:
5898 convert_sockopt(&level
, &optname
);
5901 /* SO_DEBUG is a privileged operation, ignore it. */
5903 TRACE("Ignoring SO_DEBUG\n");
5906 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
5907 * socket. According to MSDN, this option is silently ignored.*/
5908 case WS_SO_DONTROUTE
:
5909 TRACE("Ignoring SO_DONTROUTE\n");
5912 /* Stops two sockets from being bound to the same port. Always happens
5913 * on unix systems, so just drop it. */
5914 case WS_SO_EXCLUSIVEADDRUSE
:
5915 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
5918 /* After a ConnectEx call succeeds, the socket can't be used with half of the
5919 * normal winsock functions on windows. We don't have that problem. */
5920 case WS_SO_UPDATE_CONNECT_CONTEXT
:
5921 TRACE("Ignoring SO_UPDATE_CONNECT_CONTEXT, since our sockets are normal\n");
5924 /* After a AcceptEx call succeeds, the socket can't be used with half of the
5925 * normal winsock functions on windows. We don't have that problem. */
5926 case WS_SO_UPDATE_ACCEPT_CONTEXT
:
5927 TRACE("Ignoring SO_UPDATE_ACCEPT_CONTEXT, since our sockets are normal\n");
5930 /* SO_OPENTYPE does not require a valid socket handle. */
5931 case WS_SO_OPENTYPE
:
5932 if (!optlen
|| optlen
< sizeof(int) || !optval
)
5934 SetLastError(WSAEFAULT
);
5935 return SOCKET_ERROR
;
5937 get_per_thread_data()->opentype
= *(const int *)optval
;
5938 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((const int*)optval
) );
5942 case WS_SO_RCVTIMEO
:
5945 case WS_SO_SNDTIMEO
:
5947 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
5948 if (optval
&& optlen
== sizeof(UINT32
)) {
5949 /* WinSock passes milliseconds instead of struct timeval */
5950 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
5951 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
5952 /* min of 500 milliseconds */
5953 if (tval
.tv_sec
== 0 && tval
.tv_usec
&& tval
.tv_usec
< 500000)
5954 tval
.tv_usec
= 500000;
5955 optlen
= sizeof(struct timeval
);
5956 optval
= (char*)&tval
;
5957 } else if (optlen
== sizeof(struct timeval
)) {
5958 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
5960 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
5963 convert_sockopt(&level
, &optname
);
5967 case WS_SO_RANDOMIZE_PORT
:
5968 FIXME("Ignoring WS_SO_RANDOMIZE_PORT\n");
5971 case WS_SO_PORT_SCALABILITY
:
5972 FIXME("Ignoring WS_SO_PORT_SCALABILITY\n");
5975 case WS_SO_REUSE_UNICASTPORT
:
5976 FIXME("Ignoring WS_SO_REUSE_UNICASTPORT\n");
5979 case WS_SO_REUSE_MULTICASTPORT
:
5980 FIXME("Ignoring WS_SO_REUSE_MULTICASTPORT\n");
5984 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
5985 SetLastError(WSAENOPROTOOPT
);
5986 return SOCKET_ERROR
;
5988 break; /* case WS_SOL_SOCKET */
5991 case WS_NSPROTO_IPX
:
5995 return set_ipx_packettype(s
, *(int*)optval
);
5997 case WS_IPX_FILTERPTYPE
:
5998 /* Sets the receive filter packet type, at the moment we don't support it */
5999 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
6000 /* Returning 0 is better for now than returning a SOCKET_ERROR */
6004 FIXME("opt_name:%x\n", optname
);
6005 return SOCKET_ERROR
;
6007 break; /* case WS_NSPROTO_IPX */
6010 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
6011 case WS_IPPROTO_TCP
:
6014 case WS_TCP_NODELAY
:
6015 convert_sockopt(&level
, &optname
);
6018 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
6019 return SOCKET_ERROR
;
6026 case WS_IP_ADD_SOURCE_MEMBERSHIP
:
6027 case WS_IP_DROP_SOURCE_MEMBERSHIP
:
6028 case WS_IP_BLOCK_SOURCE
:
6029 case WS_IP_UNBLOCK_SOURCE
:
6031 WS_IP_MREQ_SOURCE
* val
= (void*)optval
;
6032 mreq_source
.imr_interface
.s_addr
= val
->imr_interface
.S_un
.S_addr
;
6033 mreq_source
.imr_multiaddr
.s_addr
= val
->imr_multiaddr
.S_un
.S_addr
;
6034 mreq_source
.imr_sourceaddr
.s_addr
= val
->imr_sourceaddr
.S_un
.S_addr
;
6036 optval
= (char*)&mreq_source
;
6037 optlen
= sizeof(mreq_source
);
6039 convert_sockopt(&level
, &optname
);
6042 case WS_IP_ADD_MEMBERSHIP
:
6043 case WS_IP_DROP_MEMBERSHIP
:
6047 case WS_IP_MULTICAST_IF
:
6048 case WS_IP_MULTICAST_LOOP
:
6049 case WS_IP_MULTICAST_TTL
:
6051 #if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
6056 #ifdef IP_UNICAST_IF
6057 case WS_IP_UNICAST_IF
:
6059 convert_sockopt(&level
, &optname
);
6061 case WS_IP_DONTFRAGMENT
:
6062 return set_dont_fragment(s
, IPPROTO_IP
, *(BOOL
*)optval
) ? 0 : SOCKET_ERROR
;
6064 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
6065 return SOCKET_ERROR
;
6069 case WS_IPPROTO_IPV6
:
6072 #ifdef IPV6_ADD_MEMBERSHIP
6073 case WS_IPV6_ADD_MEMBERSHIP
:
6075 #ifdef IPV6_DROP_MEMBERSHIP
6076 case WS_IPV6_DROP_MEMBERSHIP
:
6078 case WS_IPV6_MULTICAST_IF
:
6079 case WS_IPV6_MULTICAST_HOPS
:
6080 case WS_IPV6_MULTICAST_LOOP
:
6081 case WS_IPV6_UNICAST_HOPS
:
6082 #ifdef IPV6_UNICAST_IF
6083 case WS_IPV6_UNICAST_IF
:
6085 convert_sockopt(&level
, &optname
);
6087 case WS_IPV6_DONTFRAG
:
6088 return set_dont_fragment(s
, IPPROTO_IPV6
, *(BOOL
*)optval
) ? 0 : SOCKET_ERROR
;
6089 case WS_IPV6_PROTECTION_LEVEL
:
6090 FIXME("IPV6_PROTECTION_LEVEL is ignored!\n");
6092 case WS_IPV6_V6ONLY
:
6094 union generic_unix_sockaddr uaddr
;
6098 fd
= get_sock_fd( s
, 0, NULL
);
6099 if (fd
== -1) return SOCKET_ERROR
;
6101 bound
= is_fd_bound(fd
, &uaddr
, &uaddrlen
);
6102 release_sock_fd( s
, fd
);
6103 if (bound
== 0 && uaddr
.addr
.sa_family
== AF_INET
)
6105 /* Changing IPV6_V6ONLY succeeds on AF_INET (IPv4) socket
6106 * on Windows (with IPv6 support) if the socket is unbound.
6107 * It is essentially a noop, though Windows does store the value
6109 WARN("Silently ignoring IPPROTO_IPV6+IPV6_V6ONLY on AF_INET socket\n");
6112 level
= IPPROTO_IPV6
;
6113 optname
= IPV6_V6ONLY
;
6117 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
6118 return SOCKET_ERROR
;
6123 WARN("Unknown level: 0x%08x\n", level
);
6124 SetLastError(WSAEINVAL
);
6125 return SOCKET_ERROR
;
6126 } /* end switch(level) */
6128 /* avoid endianness issues if argument is a 16-bit int */
6129 if (optval
&& optlen
< sizeof(int))
6131 woptval
= *((const INT16
*) optval
);
6132 optval
= (char*) &woptval
;
6133 woptval
&= (1 << optlen
* 8) - 1;
6136 fd
= get_sock_fd( s
, 0, NULL
);
6137 if (fd
== -1) return SOCKET_ERROR
;
6139 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
6142 if (level
== SOL_SOCKET
&& optname
== SO_REUSEADDR
&&
6143 setsockopt(fd
, level
, SO_REUSEPORT
, optval
, optlen
) != 0)
6145 SetLastError(wsaErrno());
6146 release_sock_fd( s
, fd
);
6147 return SOCKET_ERROR
;
6150 release_sock_fd( s
, fd
);
6153 TRACE("Setting socket error, %d\n", wsaErrno());
6154 SetLastError(wsaErrno());
6155 release_sock_fd( s
, fd
);
6157 return SOCKET_ERROR
;
6160 /***********************************************************************
6161 * shutdown (WS2_32.22)
6163 int WINAPI
WS_shutdown(SOCKET s
, int how
)
6165 int fd
, err
= WSAENOTSOCK
;
6166 unsigned int options
= 0, clear_flags
= 0;
6168 fd
= get_sock_fd( s
, 0, &options
);
6169 TRACE("socket %04lx, how 0x%x, options 0x%x\n", s
, how
, options
);
6172 return SOCKET_ERROR
;
6176 case SD_RECEIVE
: /* drop receives */
6177 clear_flags
|= FD_READ
;
6179 case SD_SEND
: /* drop sends */
6180 clear_flags
|= FD_WRITE
;
6182 case SD_BOTH
: /* drop all */
6183 clear_flags
|= FD_READ
|FD_WRITE
;
6186 clear_flags
|= FD_WINE_LISTENING
;
6189 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
6194 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
6197 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
6201 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
6202 if (!err
) err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
6205 if (err
) goto error
;
6207 else /* non-overlapped mode */
6209 if ( shutdown( fd
, how
) )
6216 release_sock_fd( s
, fd
);
6217 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
6218 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
6222 release_sock_fd( s
, fd
);
6223 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
6224 SetLastError( err
);
6225 return SOCKET_ERROR
;
6228 /***********************************************************************
6229 * socket (WS2_32.23)
6231 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
6233 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
6235 return WSASocketW( af
, type
, protocol
, NULL
, 0,
6236 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
6240 /***********************************************************************
6241 * gethostbyaddr (WS2_32.51)
6243 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
6245 struct WS_hostent
*retval
= NULL
;
6246 struct hostent
* host
;
6247 int unixtype
= convert_af_w2u(type
);
6248 const char *paddr
= addr
;
6249 unsigned long loopback
;
6250 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6252 int ebufsize
= 1024;
6253 struct hostent hostentry
;
6254 int locerr
= ENOBUFS
;
6257 /* convert back the magic loopback address if necessary */
6258 if (unixtype
== AF_INET
&& len
== 4 && !memcmp(addr
, magic_loopback_addr
, 4))
6260 loopback
= htonl(INADDR_LOOPBACK
);
6261 paddr
= (char*) &loopback
;
6264 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6266 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
6268 int res
= gethostbyaddr_r(paddr
, len
, unixtype
,
6269 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
6270 if (res
!= ERANGE
) break;
6272 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
6274 if (host
) retval
= WS_dup_he(host
);
6275 else SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
6276 HeapFree(GetProcessHeap(),0,extrabuf
);
6278 EnterCriticalSection( &csWSgetXXXbyYYY
);
6279 host
= gethostbyaddr(paddr
, len
, unixtype
);
6280 if (host
) retval
= WS_dup_he(host
);
6281 else SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
6282 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6284 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
6288 /***********************************************************************
6289 * WS_compare_routes_by_metric_asc (INTERNAL)
6291 * Comparison function for qsort(), for sorting two routes (struct route)
6292 * by metric in ascending order.
6294 static int WS_compare_routes_by_metric_asc(const void *left
, const void *right
)
6296 const struct route
*a
= left
, *b
= right
;
6297 if (a
->default_route
&& b
->default_route
)
6298 return a
->default_route
- b
->default_route
;
6299 if (a
->default_route
&& !b
->default_route
)
6301 if (b
->default_route
&& !a
->default_route
)
6303 return a
->metric
- b
->metric
;
6306 /***********************************************************************
6307 * WS_get_local_ips (INTERNAL)
6309 * Returns the list of local IP addresses by going through the network
6310 * adapters and using the local routing table to sort the addresses
6311 * from highest routing priority to lowest routing priority. This
6312 * functionality is inferred from the description for obtaining local
6313 * IP addresses given in the Knowledge Base Article Q160215.
6315 * Please note that the returned hostent is only freed when the thread
6316 * closes and is replaced if another hostent is requested.
6318 static struct WS_hostent
* WS_get_local_ips( char *hostname
)
6320 int numroutes
= 0, i
, j
, default_routes
= 0;
6322 PIP_ADAPTER_INFO adapters
= NULL
, k
;
6323 struct WS_hostent
*hostlist
= NULL
;
6324 PMIB_IPFORWARDTABLE routes
= NULL
;
6325 struct route
*route_addrs
= NULL
;
6326 DWORD adap_size
, route_size
;
6328 /* Obtain the size of the adapter list and routing table, also allocate memory */
6329 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
6331 if (GetIpForwardTable(NULL
, &route_size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
6333 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
6334 routes
= HeapAlloc(GetProcessHeap(), 0, route_size
);
6335 if (adapters
== NULL
|| routes
== NULL
)
6337 /* Obtain the adapter list and the full routing table */
6338 if (GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
6340 if (GetIpForwardTable(routes
, &route_size
, FALSE
) != NO_ERROR
)
6342 /* Store the interface associated with each route */
6343 for (n
= 0; n
< routes
->dwNumEntries
; n
++)
6346 DWORD ifmetric
, ifdefault
= 0;
6347 BOOL exists
= FALSE
;
6349 /* Check if this is a default route (there may be more than one) */
6350 if (!routes
->table
[n
].dwForwardDest
)
6351 ifdefault
= ++default_routes
;
6352 else if (routes
->table
[n
].u1
.ForwardType
!= MIB_IPROUTE_TYPE_DIRECT
)
6354 ifindex
= routes
->table
[n
].dwForwardIfIndex
;
6355 ifmetric
= routes
->table
[n
].dwForwardMetric1
;
6356 /* Only store the lowest valued metric for an interface */
6357 for (j
= 0; j
< numroutes
; j
++)
6359 if (route_addrs
[j
].interface
== ifindex
)
6361 if (route_addrs
[j
].metric
> ifmetric
)
6362 route_addrs
[j
].metric
= ifmetric
;
6368 route_addrs
= heap_realloc(route_addrs
, (numroutes
+1)*sizeof(struct route
));
6369 if (route_addrs
== NULL
)
6370 goto cleanup
; /* Memory allocation error, fail gracefully */
6371 route_addrs
[numroutes
].interface
= ifindex
;
6372 route_addrs
[numroutes
].metric
= ifmetric
;
6373 route_addrs
[numroutes
].default_route
= ifdefault
;
6374 /* If no IP is found in the next step (for whatever reason)
6375 * then fall back to the magic loopback address.
6377 memcpy(&(route_addrs
[numroutes
].addr
.s_addr
), magic_loopback_addr
, 4);
6381 goto cleanup
; /* No routes, fall back to the Magic IP */
6382 /* Find the IP address associated with each found interface */
6383 for (i
= 0; i
< numroutes
; i
++)
6385 for (k
= adapters
; k
!= NULL
; k
= k
->Next
)
6387 char *ip
= k
->IpAddressList
.IpAddress
.String
;
6389 if (route_addrs
[i
].interface
== k
->Index
)
6390 route_addrs
[i
].addr
.s_addr
= (in_addr_t
) inet_addr(ip
);
6393 /* Allocate a hostent and enough memory for all the IPs,
6394 * including the NULL at the end of the list.
6396 hostlist
= WS_create_he(hostname
, 1, 0, numroutes
+1, sizeof(struct in_addr
));
6397 if (hostlist
== NULL
)
6398 goto cleanup
; /* Failed to allocate a hostent for the list of IPs */
6399 hostlist
->h_addr_list
[numroutes
] = NULL
; /* NULL-terminate the address list */
6400 hostlist
->h_aliases
[0] = NULL
; /* NULL-terminate the alias list */
6401 hostlist
->h_addrtype
= AF_INET
;
6402 hostlist
->h_length
= sizeof(struct in_addr
); /* = 4 */
6403 /* Reorder the entries before placing them in the host list. Windows expects
6404 * the IP list in order from highest priority to lowest (the critical thing
6405 * is that most applications expect the first IP to be the default route).
6408 qsort(route_addrs
, numroutes
, sizeof(struct route
), WS_compare_routes_by_metric_asc
);
6410 for (i
= 0; i
< numroutes
; i
++)
6411 (*(struct in_addr
*) hostlist
->h_addr_list
[i
]) = route_addrs
[i
].addr
;
6413 /* Cleanup all allocated memory except the address list,
6414 * the address list is used by the calling app.
6417 HeapFree(GetProcessHeap(), 0, route_addrs
);
6418 HeapFree(GetProcessHeap(), 0, adapters
);
6419 HeapFree(GetProcessHeap(), 0, routes
);
6423 /***********************************************************************
6424 * gethostbyname (WS2_32.52)
6426 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
6428 struct WS_hostent
*retval
= NULL
;
6429 struct hostent
* host
;
6430 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6433 struct hostent hostentry
;
6434 int locerr
= ENOBUFS
;
6438 SetLastError(WSANOTINITIALISED
);
6441 if( gethostname( hostname
, 100) == -1) {
6442 SetLastError(WSAENOBUFS
); /* appropriate ? */
6445 if( !name
|| !name
[0]) {
6448 /* If the hostname of the local machine is requested then return the
6449 * complete list of local IP addresses */
6450 if(strcmp(name
, hostname
) == 0)
6451 retval
= WS_get_local_ips(hostname
);
6452 /* If any other hostname was requested (or the routing table lookup failed)
6453 * then return the IP found by the host OS */
6456 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6458 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
6460 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
6461 if( res
!= ERANGE
) break;
6463 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
6465 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
6467 EnterCriticalSection( &csWSgetXXXbyYYY
);
6468 host
= gethostbyname(name
);
6469 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
6471 if (host
) retval
= WS_dup_he(host
);
6472 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6473 HeapFree(GetProcessHeap(),0,extrabuf
);
6475 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6478 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
6479 strcmp(name
, "localhost") != 0)
6481 /* hostname != "localhost" but has loopback address. replace by our
6482 * special address.*/
6483 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
6485 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
6490 static const struct { int prot
; const char *names
[3]; } protocols
[] =
6492 { 0, { "ip", "IP" }},
6493 { 1, { "icmp", "ICMP" }},
6494 { 3, { "ggp", "GGP" }},
6495 { 6, { "tcp", "TCP" }},
6496 { 8, { "egp", "EGP" }},
6497 { 12, { "pup", "PUP" }},
6498 { 17, { "udp", "UDP" }},
6499 { 20, { "hmp", "HMP" }},
6500 { 22, { "xns-idp", "XNS-IDP" }},
6501 { 27, { "rdp", "RDP" }},
6502 { 41, { "ipv6", "IPv6" }},
6503 { 43, { "ipv6-route", "IPv6-Route" }},
6504 { 44, { "ipv6-frag", "IPv6-Frag" }},
6505 { 50, { "esp", "ESP" }},
6506 { 51, { "ah", "AH" }},
6507 { 58, { "ipv6-icmp", "IPv6-ICMP" }},
6508 { 59, { "ipv6-nonxt", "IPv6-NoNxt" }},
6509 { 60, { "ipv6-opts", "IPv6-Opts" }},
6510 { 66, { "rvd", "RVD" }},
6513 /***********************************************************************
6514 * getprotobyname (WS2_32.53)
6516 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
6518 struct WS_protoent
* retval
= NULL
;
6521 for (i
= 0; i
< ARRAY_SIZE(protocols
); i
++)
6523 if (_strnicmp( protocols
[i
].names
[0], name
, -1 )) continue;
6524 retval
= WS_create_pe( protocols
[i
].names
[0], (char **)protocols
[i
].names
+ 1,
6525 protocols
[i
].prot
);
6530 WARN( "protocol %s not found\n", debugstr_a(name
) );
6531 SetLastError(WSANO_DATA
);
6533 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
6538 /***********************************************************************
6539 * getprotobynumber (WS2_32.54)
6541 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
6543 struct WS_protoent
* retval
= NULL
;
6546 for (i
= 0; i
< ARRAY_SIZE(protocols
); i
++)
6548 if (protocols
[i
].prot
!= number
) continue;
6549 retval
= WS_create_pe( protocols
[i
].names
[0], (char **)protocols
[i
].names
+ 1,
6550 protocols
[i
].prot
);
6555 WARN( "protocol %d not found\n", number
);
6556 SetLastError(WSANO_DATA
);
6558 TRACE("%i ret %p\n", number
, retval
);
6563 /***********************************************************************
6564 * getservbyname (WS2_32.55)
6566 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
6568 struct WS_servent
* retval
= NULL
;
6569 struct servent
* serv
;
6571 char *proto_str
= NULL
;
6573 if (!(name_str
= strdup_lower(name
))) return NULL
;
6575 if (proto
&& *proto
)
6577 if (!(proto_str
= strdup_lower(proto
)))
6579 HeapFree( GetProcessHeap(), 0, name_str
);
6584 EnterCriticalSection( &csWSgetXXXbyYYY
);
6585 serv
= getservbyname(name_str
, proto_str
);
6588 retval
= WS_dup_se(serv
);
6590 else SetLastError(WSANO_DATA
);
6591 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6592 HeapFree( GetProcessHeap(), 0, proto_str
);
6593 HeapFree( GetProcessHeap(), 0, name_str
);
6594 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
6598 /***********************************************************************
6599 * freeaddrinfo (WS2_32.@)
6601 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
6604 struct WS_addrinfo
*next
;
6606 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
6607 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
6608 next
= res
->ai_next
;
6609 HeapFree(GetProcessHeap(),0,res
);
6614 /* helper functions for getaddrinfo()/getnameinfo() */
6615 static int convert_aiflag_w2u(int winflags
) {
6619 for (i
= 0; i
< ARRAY_SIZE(ws_aiflag_map
); i
++)
6620 if (ws_aiflag_map
[i
][0] & winflags
) {
6621 unixflags
|= ws_aiflag_map
[i
][1];
6622 winflags
&= ~ws_aiflag_map
[i
][0];
6625 FIXME("Unhandled windows AI_xxx flags 0x%x\n", winflags
);
6629 static int convert_niflag_w2u(int winflags
) {
6633 for (i
= 0; i
< ARRAY_SIZE(ws_niflag_map
); i
++)
6634 if (ws_niflag_map
[i
][0] & winflags
) {
6635 unixflags
|= ws_niflag_map
[i
][1];
6636 winflags
&= ~ws_niflag_map
[i
][0];
6639 FIXME("Unhandled windows NI_xxx flags 0x%x\n", winflags
);
6643 static int convert_aiflag_u2w(int unixflags
) {
6647 for (i
= 0; i
< ARRAY_SIZE(ws_aiflag_map
); i
++)
6648 if (ws_aiflag_map
[i
][1] & unixflags
) {
6649 winflags
|= ws_aiflag_map
[i
][0];
6650 unixflags
&= ~ws_aiflag_map
[i
][1];
6653 WARN("Unhandled UNIX AI_xxx flags 0x%x\n", unixflags
);
6657 static int convert_eai_u2w(int unixret
) {
6660 if (!unixret
) return 0;
6662 for (i
=0;ws_eai_map
[i
][0];i
++)
6663 if (ws_eai_map
[i
][1] == unixret
)
6664 return ws_eai_map
[i
][0];
6666 if (unixret
== EAI_SYSTEM
)
6667 /* There are broken versions of glibc which return EAI_SYSTEM
6668 * and set errno to 0 instead of returning EAI_NONAME.
6670 return errno
? sock_get_error( errno
) : WS_EAI_NONAME
;
6672 FIXME("Unhandled unix EAI_xxx ret %d\n", unixret
);
6676 static char *get_fqdn(void)
6681 GetComputerNameExA( ComputerNamePhysicalDnsFullyQualified
, NULL
, &size
);
6682 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
6683 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, size
))) return NULL
;
6684 if (!GetComputerNameExA( ComputerNamePhysicalDnsFullyQualified
, ret
, &size
))
6686 HeapFree( GetProcessHeap(), 0, ret
);
6692 /***********************************************************************
6693 * getaddrinfo (WS2_32.@)
6695 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
6697 #ifdef HAVE_GETADDRINFO
6698 struct addrinfo
*unixaires
= NULL
;
6700 struct addrinfo unixhints
, *punixhints
= NULL
;
6701 char *dot
, *nodeV6
= NULL
, *fqdn
;
6703 size_t hostname_len
= 0;
6706 if (!nodename
&& !servname
)
6708 SetLastError(WSAHOST_NOT_FOUND
);
6709 return WSAHOST_NOT_FOUND
;
6713 if (!fqdn
) return WSA_NOT_ENOUGH_MEMORY
;
6714 dot
= strchr(fqdn
, '.');
6716 hostname_len
= dot
- fqdn
;
6720 else if (!nodename
[0])
6726 /* Check for [ipv6] or [ipv6]:portnumber, which are supported by Windows */
6727 if (!hints
|| hints
->ai_family
== WS_AF_UNSPEC
|| hints
->ai_family
== WS_AF_INET6
)
6729 char *close_bracket
;
6731 if (node
[0] == '[' && (close_bracket
= strchr(node
+ 1, ']')))
6733 nodeV6
= HeapAlloc(GetProcessHeap(), 0, close_bracket
- node
);
6736 HeapFree(GetProcessHeap(), 0, fqdn
);
6737 return WSA_NOT_ENOUGH_MEMORY
;
6739 lstrcpynA(nodeV6
, node
+ 1, close_bracket
- node
);
6745 /* servname tweak required by OSX and BSD kernels */
6746 if (servname
&& !servname
[0]) servname
= "0";
6749 punixhints
= &unixhints
;
6751 memset(&unixhints
, 0, sizeof(unixhints
));
6752 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
6754 /* zero is a wildcard, no need to convert */
6755 if (hints
->ai_family
)
6756 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
6757 if (hints
->ai_socktype
)
6758 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
6759 if (hints
->ai_protocol
)
6760 punixhints
->ai_protocol
= max(convert_proto_w2u(hints
->ai_protocol
), 0);
6762 if (punixhints
->ai_socktype
< 0)
6764 SetLastError(WSAESOCKTNOSUPPORT
);
6765 HeapFree(GetProcessHeap(), 0, fqdn
);
6766 HeapFree(GetProcessHeap(), 0, nodeV6
);
6767 return SOCKET_ERROR
;
6770 /* windows allows invalid combinations of socket type and protocol, unix does not.
6771 * fix the parameters here to make getaddrinfo call always work */
6772 if (punixhints
->ai_protocol
== IPPROTO_TCP
&&
6773 punixhints
->ai_socktype
!= SOCK_STREAM
&& punixhints
->ai_socktype
!= SOCK_SEQPACKET
)
6774 punixhints
->ai_socktype
= 0;
6776 else if (punixhints
->ai_protocol
== IPPROTO_UDP
&& punixhints
->ai_socktype
!= SOCK_DGRAM
)
6777 punixhints
->ai_socktype
= 0;
6779 else if (IS_IPX_PROTO(punixhints
->ai_protocol
) && punixhints
->ai_socktype
!= SOCK_DGRAM
)
6780 punixhints
->ai_socktype
= 0;
6782 else if (punixhints
->ai_protocol
== IPPROTO_IPV6
)
6783 punixhints
->ai_protocol
= 0;
6786 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
6787 result
= getaddrinfo(node
, servname
, punixhints
, &unixaires
);
6789 if (result
&& (!hints
|| !(hints
->ai_flags
& WS_AI_NUMERICHOST
))
6790 && (!strcmp(fqdn
, node
) || (!strncmp(fqdn
, node
, hostname_len
) && !node
[hostname_len
])))
6792 /* If it didn't work it means the host name IP is not in /etc/hosts, try again
6793 * by sending a NULL host and avoid sending a NULL servname too because that
6795 ERR_(winediag
)("Failed to resolve your host name IP\n");
6796 result
= getaddrinfo(NULL
, servname
? servname
: "0", punixhints
, &unixaires
);
6798 TRACE("%s, %s %p -> %p %d\n", debugstr_a(nodename
), debugstr_a(servname
), hints
, res
, result
);
6799 HeapFree(GetProcessHeap(), 0, fqdn
);
6800 HeapFree(GetProcessHeap(), 0, nodeV6
);
6803 struct addrinfo
*xuai
= unixaires
;
6804 struct WS_addrinfo
**xai
= res
;
6808 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
6814 *xai
= ai
;xai
= &ai
->ai_next
;
6815 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
6816 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
6817 /* copy whatever was sent in the hints */
6819 ai
->ai_socktype
= hints
->ai_socktype
;
6820 ai
->ai_protocol
= hints
->ai_protocol
;
6822 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
6823 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
6825 if (xuai
->ai_canonname
) {
6826 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
6827 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
6828 if (!ai
->ai_canonname
)
6830 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
6832 len
= xuai
->ai_addrlen
;
6833 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
6836 ai
->ai_addrlen
= len
;
6838 int winlen
= ai
->ai_addrlen
;
6840 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
6841 ai
->ai_addrlen
= winlen
;
6845 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
6848 ai
->ai_addrlen
= len
;
6850 xuai
= xuai
->ai_next
;
6852 freeaddrinfo(unixaires
);
6854 if (TRACE_ON(winsock
))
6856 struct WS_addrinfo
*ai
= *res
;
6859 TRACE("=> %p, flags %#x, family %d, type %d, protocol %d, len %ld, name %s, addr %s\n",
6860 ai
, ai
->ai_flags
, ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
, ai
->ai_addrlen
,
6861 ai
->ai_canonname
, debugstr_sockaddr(ai
->ai_addr
));
6866 result
= convert_eai_u2w(result
);
6868 SetLastError(result
);
6872 if (*res
) WS_freeaddrinfo(*res
);
6873 if (unixaires
) freeaddrinfo(unixaires
);
6874 return WSA_NOT_ENOUGH_MEMORY
;
6876 FIXME("getaddrinfo() failed, not found during buildtime.\n");
6881 static ADDRINFOEXW
*addrinfo_AtoW(const struct WS_addrinfo
*ai
)
6885 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(ADDRINFOEXW
)))) return NULL
;
6886 ret
->ai_flags
= ai
->ai_flags
;
6887 ret
->ai_family
= ai
->ai_family
;
6888 ret
->ai_socktype
= ai
->ai_socktype
;
6889 ret
->ai_protocol
= ai
->ai_protocol
;
6890 ret
->ai_addrlen
= ai
->ai_addrlen
;
6891 ret
->ai_canonname
= NULL
;
6892 ret
->ai_addr
= NULL
;
6893 ret
->ai_blob
= NULL
;
6894 ret
->ai_bloblen
= 0;
6895 ret
->ai_provider
= NULL
;
6896 ret
->ai_next
= NULL
;
6897 if (ai
->ai_canonname
)
6899 int len
= MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0);
6900 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
))))
6902 HeapFree(GetProcessHeap(), 0, ret
);
6905 MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
);
6909 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, ai
->ai_addrlen
)))
6911 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
6912 HeapFree(GetProcessHeap(), 0, ret
);
6915 memcpy(ret
->ai_addr
, ai
->ai_addr
, ai
->ai_addrlen
);
6920 static ADDRINFOEXW
*addrinfo_list_AtoW(const struct WS_addrinfo
*info
)
6922 ADDRINFOEXW
*ret
, *infoW
;
6924 if (!(ret
= infoW
= addrinfo_AtoW(info
))) return NULL
;
6925 while (info
->ai_next
)
6927 if (!(infoW
->ai_next
= addrinfo_AtoW(info
->ai_next
)))
6929 FreeAddrInfoExW(ret
);
6932 infoW
= infoW
->ai_next
;
6933 info
= info
->ai_next
;
6938 static struct WS_addrinfo
*addrinfo_WtoA(const struct WS_addrinfoW
*ai
)
6940 struct WS_addrinfo
*ret
;
6942 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfo
)))) return NULL
;
6943 ret
->ai_flags
= ai
->ai_flags
;
6944 ret
->ai_family
= ai
->ai_family
;
6945 ret
->ai_socktype
= ai
->ai_socktype
;
6946 ret
->ai_protocol
= ai
->ai_protocol
;
6947 ret
->ai_addrlen
= ai
->ai_addrlen
;
6948 ret
->ai_canonname
= NULL
;
6949 ret
->ai_addr
= NULL
;
6950 ret
->ai_next
= NULL
;
6951 if (ai
->ai_canonname
)
6953 int len
= WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0, NULL
, NULL
);
6954 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
6956 HeapFree(GetProcessHeap(), 0, ret
);
6959 WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
, NULL
, NULL
);
6963 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
6965 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
6966 HeapFree(GetProcessHeap(), 0, ret
);
6969 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
6974 struct getaddrinfo_args
6976 OVERLAPPED
*overlapped
;
6977 LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine
;
6978 ADDRINFOEXW
**result
;
6983 static void WINAPI
getaddrinfo_callback(TP_CALLBACK_INSTANCE
*instance
, void *context
)
6985 struct getaddrinfo_args
*args
= context
;
6986 OVERLAPPED
*overlapped
= args
->overlapped
;
6987 HANDLE event
= overlapped
->hEvent
;
6988 LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine
= args
->completion_routine
;
6989 struct WS_addrinfo
*res
;
6992 ret
= WS_getaddrinfo(args
->nodename
, args
->servname
, NULL
, &res
);
6995 *args
->result
= addrinfo_list_AtoW(res
);
6996 overlapped
->u
.Pointer
= args
->result
;
6997 WS_freeaddrinfo(res
);
7000 HeapFree(GetProcessHeap(), 0, args
->nodename
);
7001 HeapFree(GetProcessHeap(), 0, args
->servname
);
7002 HeapFree(GetProcessHeap(), 0, args
);
7004 overlapped
->Internal
= ret
;
7005 if (completion_routine
) completion_routine(ret
, 0, overlapped
);
7006 if (event
) SetEvent(event
);
7009 static int WS_getaddrinfoW(const WCHAR
*nodename
, const WCHAR
*servname
, const struct WS_addrinfo
*hints
, ADDRINFOEXW
**res
,
7010 OVERLAPPED
*overlapped
, LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine
)
7012 int ret
= EAI_MEMORY
, len
, i
;
7013 char *nodenameA
= NULL
, *servnameA
= NULL
;
7014 struct WS_addrinfo
*resA
;
7015 WCHAR
*local_nodenameW
= (WCHAR
*)nodename
;
7020 /* Is this an IDN? Most likely if any char is above the Ascii table, this
7021 * is the simplest validation possible, further validation will be done by
7022 * the native getaddrinfo() */
7023 for (i
= 0; nodename
[i
]; i
++)
7025 if (nodename
[i
] > 'z')
7030 if (hints
&& (hints
->ai_flags
& WS_AI_DISABLE_IDN_ENCODING
))
7032 /* Name requires conversion but it was disabled */
7033 ret
= WSAHOST_NOT_FOUND
;
7034 WSASetLastError(ret
);
7038 len
= IdnToAscii(0, nodename
, -1, NULL
, 0);
7041 ERR("Failed to convert %s to punycode\n", debugstr_w(nodename
));
7045 if (!(local_nodenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
)))) goto end
;
7046 IdnToAscii(0, nodename
, -1, local_nodenameW
, len
);
7049 if (local_nodenameW
)
7051 len
= WideCharToMultiByte(CP_ACP
, 0, local_nodenameW
, -1, NULL
, 0, NULL
, NULL
);
7052 if (!(nodenameA
= HeapAlloc(GetProcessHeap(), 0, len
))) goto end
;
7053 WideCharToMultiByte(CP_ACP
, 0, local_nodenameW
, -1, nodenameA
, len
, NULL
, NULL
);
7057 len
= WideCharToMultiByte(CP_ACP
, 0, servname
, -1, NULL
, 0, NULL
, NULL
);
7058 if (!(servnameA
= HeapAlloc(GetProcessHeap(), 0, len
))) goto end
;
7059 WideCharToMultiByte(CP_ACP
, 0, servname
, -1, servnameA
, len
, NULL
, NULL
);
7064 struct getaddrinfo_args
*args
;
7066 if (overlapped
->hEvent
&& completion_routine
)
7072 if (!(args
= HeapAlloc(GetProcessHeap(), 0, sizeof(*args
)))) goto end
;
7073 args
->overlapped
= overlapped
;
7074 args
->completion_routine
= completion_routine
;
7076 args
->nodename
= nodenameA
;
7077 args
->servname
= servnameA
;
7079 overlapped
->Internal
= WSAEINPROGRESS
;
7080 if (!TrySubmitThreadpoolCallback(getaddrinfo_callback
, args
, NULL
))
7082 HeapFree(GetProcessHeap(), 0, args
);
7083 ret
= GetLastError();
7087 if (local_nodenameW
!= nodename
)
7088 HeapFree(GetProcessHeap(), 0, local_nodenameW
);
7089 WSASetLastError(ERROR_IO_PENDING
);
7090 return ERROR_IO_PENDING
;
7093 ret
= WS_getaddrinfo(nodenameA
, servnameA
, hints
, &resA
);
7096 *res
= addrinfo_list_AtoW(resA
);
7097 WS_freeaddrinfo(resA
);
7101 if (local_nodenameW
!= nodename
)
7102 HeapFree(GetProcessHeap(), 0, local_nodenameW
);
7103 HeapFree(GetProcessHeap(), 0, nodenameA
);
7104 HeapFree(GetProcessHeap(), 0, servnameA
);
7108 /***********************************************************************
7109 * GetAddrInfoExW (WS2_32.@)
7111 int WINAPI
GetAddrInfoExW(const WCHAR
*name
, const WCHAR
*servname
, DWORD
namespace, GUID
*namespace_id
,
7112 const ADDRINFOEXW
*hints
, ADDRINFOEXW
**result
, struct WS_timeval
*timeout
, OVERLAPPED
*overlapped
,
7113 LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine
, HANDLE
*handle
)
7117 TRACE("(%s %s %x %s %p %p %p %p %p %p)\n", debugstr_w(name
), debugstr_w(servname
), namespace,
7118 debugstr_guid(namespace_id
), hints
, result
, timeout
, overlapped
, completion_routine
, handle
);
7120 if (namespace != NS_DNS
)
7121 FIXME("Unsupported namespace %u\n", namespace);
7123 FIXME("Unsupported naemspace_id %s\n", debugstr_guid(namespace_id
));
7125 FIXME("Unsupported hints\n");
7127 FIXME("Unsupported timeout\n");
7129 FIXME("Unsupported cancel handle\n");
7131 ret
= WS_getaddrinfoW(name
, servname
, NULL
, result
, overlapped
, completion_routine
);
7132 if (ret
) return ret
;
7133 if (handle
) *handle
= (HANDLE
)0xdeadbeef;
7137 /***********************************************************************
7138 * GetAddrInfoExOverlappedResult (WS2_32.@)
7140 int WINAPI
GetAddrInfoExOverlappedResult(OVERLAPPED
*overlapped
)
7142 TRACE("(%p)\n", overlapped
);
7143 return overlapped
->Internal
;
7146 /***********************************************************************
7147 * GetAddrInfoExCancel (WS2_32.@)
7149 int WINAPI
GetAddrInfoExCancel(HANDLE
*handle
)
7151 FIXME("(%p)\n", handle
);
7152 return WSA_INVALID_HANDLE
;
7155 /***********************************************************************
7156 * GetAddrInfoW (WS2_32.@)
7158 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
7160 struct WS_addrinfo
*hintsA
= NULL
;
7162 int ret
= EAI_MEMORY
;
7164 TRACE("nodename %s, servname %s, hints %p, result %p\n",
7165 debugstr_w(nodename
), debugstr_w(servname
), hints
, res
);
7168 if (hints
) hintsA
= addrinfo_WtoA(hints
);
7169 ret
= WS_getaddrinfoW(nodename
, servname
, hintsA
, &resex
, NULL
, NULL
);
7170 WS_freeaddrinfo(hintsA
);
7171 if (ret
) return ret
;
7175 /* ADDRINFOEXW has layout compatible with ADDRINFOW except for ai_next field,
7176 * so we may convert it in place */
7177 *res
= (ADDRINFOW
*)resex
;
7179 ((ADDRINFOW
*)resex
)->ai_next
= (ADDRINFOW
*)resex
->ai_next
;
7180 resex
= resex
->ai_next
;
7186 /***********************************************************************
7187 * FreeAddrInfoW (WS2_32.@)
7189 void WINAPI
FreeAddrInfoW(PADDRINFOW ai
)
7194 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
7195 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
7197 HeapFree(GetProcessHeap(), 0, ai
);
7202 /***********************************************************************
7203 * FreeAddrInfoEx (WS2_32.@)
7205 void WINAPI
FreeAddrInfoEx(ADDRINFOEXA
*ai
)
7207 TRACE("(%p)\n", ai
);
7212 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
7213 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
7215 HeapFree(GetProcessHeap(), 0, ai
);
7220 /***********************************************************************
7221 * FreeAddrInfoExW (WS2_32.@)
7223 void WINAPI
FreeAddrInfoExW(ADDRINFOEXW
*ai
)
7225 TRACE("(%p)\n", ai
);
7230 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
7231 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
7233 HeapFree(GetProcessHeap(), 0, ai
);
7238 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
7239 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
7241 #ifdef HAVE_GETNAMEINFO
7243 union generic_unix_sockaddr sa_u
;
7246 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
7247 serv
, servlen
, flags
);
7249 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
7252 SetLastError(WSAEFAULT
);
7253 return WSA_NOT_ENOUGH_MEMORY
;
7255 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
7256 return convert_eai_u2w(ret
);
7258 FIXME("getnameinfo() failed, not found during buildtime.\n");
7263 int WINAPI
GetNameInfoW(const SOCKADDR
*sa
, WS_socklen_t salen
, PWCHAR host
,
7264 DWORD hostlen
, PWCHAR serv
, DWORD servlen
, INT flags
)
7267 char *hostA
= NULL
, *servA
= NULL
;
7269 if (host
&& (!(hostA
= HeapAlloc(GetProcessHeap(), 0, hostlen
)))) return EAI_MEMORY
;
7270 if (serv
&& (!(servA
= HeapAlloc(GetProcessHeap(), 0, servlen
))))
7272 HeapFree(GetProcessHeap(), 0, hostA
);
7276 ret
= WS_getnameinfo(sa
, salen
, hostA
, hostlen
, servA
, servlen
, flags
);
7279 if (host
) MultiByteToWideChar(CP_ACP
, 0, hostA
, -1, host
, hostlen
);
7280 if (serv
) MultiByteToWideChar(CP_ACP
, 0, servA
, -1, serv
, servlen
);
7283 HeapFree(GetProcessHeap(), 0, hostA
);
7284 HeapFree(GetProcessHeap(), 0, servA
);
7288 /***********************************************************************
7289 * getservbyport (WS2_32.56)
7291 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
7293 struct WS_servent
* retval
= NULL
;
7294 #ifdef HAVE_GETSERVBYPORT
7295 struct servent
* serv
;
7296 char *proto_str
= NULL
;
7298 if (proto
&& *proto
)
7300 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
7302 EnterCriticalSection( &csWSgetXXXbyYYY
);
7303 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
7304 retval
= WS_dup_se(serv
);
7306 else SetLastError(WSANO_DATA
);
7307 LeaveCriticalSection( &csWSgetXXXbyYYY
);
7308 HeapFree( GetProcessHeap(), 0, proto_str
);
7310 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
7315 /***********************************************************************
7316 * gethostname (WS2_32.57)
7318 int WINAPI
WS_gethostname(char *name
, int namelen
)
7323 TRACE("name %p, len %d\n", name
, namelen
);
7327 SetLastError(WSAEFAULT
);
7328 return SOCKET_ERROR
;
7331 if (gethostname(buf
, sizeof(buf
)) != 0)
7333 SetLastError(wsaErrno());
7334 return SOCKET_ERROR
;
7337 TRACE("<- '%s'\n", buf
);
7340 WARN("Windows supports NetBIOS name length up to 15 bytes!\n");
7343 SetLastError(WSAEFAULT
);
7344 WARN("<- not enough space for hostname, required %d, got %d!\n", len
+ 1, namelen
);
7345 return SOCKET_ERROR
;
7352 /* ------------------------------------- Windows sockets extensions -- *
7354 * ------------------------------------------------------------------- */
7356 /***********************************************************************
7357 * WSAEnumNetworkEvents (WS2_32.36)
7359 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
7363 int errors
[FD_MAX_EVENTS
];
7365 TRACE("%04lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
7367 SERVER_START_REQ( get_socket_event
)
7369 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
7370 req
->service
= TRUE
;
7371 req
->c_event
= wine_server_obj_handle( hEvent
);
7372 wine_server_set_reply( req
, errors
, sizeof(errors
) );
7373 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
7378 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
7380 if (lpEvent
->lNetworkEvents
& (1 << i
))
7381 lpEvent
->iErrorCode
[i
] = errors
[i
];
7385 SetLastError(WSAEINVAL
);
7386 return SOCKET_ERROR
;
7389 /***********************************************************************
7390 * WSAEventSelect (WS2_32.39)
7392 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
7396 TRACE("%04lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
7398 SERVER_START_REQ( set_socket_event
)
7400 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
7402 req
->event
= wine_server_obj_handle( hEvent
);
7405 ret
= wine_server_call( req
);
7409 SetLastError(WSAEINVAL
);
7410 return SOCKET_ERROR
;
7413 /**********************************************************************
7414 * WSAGetOverlappedResult (WS2_32.40)
7416 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
7417 LPDWORD lpcbTransfer
, BOOL fWait
,
7422 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
7423 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
7425 if ( lpOverlapped
== NULL
)
7427 ERR( "Invalid pointer\n" );
7428 SetLastError(WSA_INVALID_PARAMETER
);
7432 status
= lpOverlapped
->Internal
;
7433 if (status
== STATUS_PENDING
)
7437 SetLastError( WSA_IO_INCOMPLETE
);
7441 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
7442 INFINITE
) == WAIT_FAILED
)
7444 status
= lpOverlapped
->Internal
;
7448 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
7451 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
7453 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
7458 /***********************************************************************
7459 * WSAAsyncSelect (WS2_32.101)
7461 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
7465 TRACE("%04lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
7467 SERVER_START_REQ( set_socket_event
)
7469 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
7472 req
->window
= wine_server_user_handle( hWnd
);
7474 ret
= wine_server_call( req
);
7478 SetLastError(WSAEINVAL
);
7479 return SOCKET_ERROR
;
7482 /***********************************************************************
7483 * WSACreateEvent (WS2_32.31)
7486 WSAEVENT WINAPI
WSACreateEvent(void)
7488 /* Create a manual-reset event, with initial state: unsignaled */
7491 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
7494 /***********************************************************************
7495 * WSACloseEvent (WS2_32.29)
7498 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
7500 TRACE ("event=%p\n", event
);
7502 return CloseHandle(event
);
7505 /***********************************************************************
7506 * WSASocketA (WS2_32.78)
7509 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
7510 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
7511 GROUP g
, DWORD dwFlags
)
7514 WSAPROTOCOL_INFOW info
;
7516 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
7517 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
7519 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
7521 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
7522 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
7523 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
7527 SetLastError(WSAEINVAL
);
7528 return SOCKET_ERROR
;
7531 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
7534 /***********************************************************************
7535 * WSASocketW (WS2_32.79)
7538 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
7539 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
7540 GROUP g
, DWORD dwFlags
)
7544 int unixaf
, unixtype
, ipxptype
= -1;
7547 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
7548 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
7551 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
7552 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
7556 err
= WSANOTINITIALISED
;
7560 /* hack for WSADuplicateSocket */
7561 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
7562 ret
= lpProtocolInfo
->dwServiceFlags3
;
7563 TRACE("\tgot duplicate %04lx\n", ret
);
7569 if (af
== FROM_PROTOCOL_INFO
|| !af
)
7570 af
= lpProtocolInfo
->iAddressFamily
;
7571 if (type
== FROM_PROTOCOL_INFO
|| !type
)
7572 type
= lpProtocolInfo
->iSocketType
;
7573 if (protocol
== FROM_PROTOCOL_INFO
|| !protocol
)
7574 protocol
= lpProtocolInfo
->iProtocol
;
7577 if (!type
&& (af
|| protocol
))
7579 int autoproto
= protocol
;
7580 WSAPROTOCOL_INFOW infow
;
7582 /* default to the first valid protocol */
7584 autoproto
= valid_protocols
[0];
7585 else if(IS_IPX_PROTO(autoproto
))
7586 autoproto
= WS_NSPROTO_IPX
;
7588 if (WS_EnterSingleProtocolW(autoproto
, &infow
))
7590 type
= infow
.iSocketType
;
7592 /* after win2003 it's no longer possible to pass AF_UNSPEC
7593 using the protocol info struct */
7594 if (!lpProtocolInfo
&& af
== WS_AF_UNSPEC
)
7595 af
= infow
.iAddressFamily
;
7600 Windows has an extension to the IPX protocol that allows one to create sockets
7601 and set the IPX packet type at the same time, to do that a caller will use
7602 a protocol like NSPROTO_IPX + <PACKET TYPE>
7604 if (IS_IPX_PROTO(protocol
))
7605 ipxptype
= protocol
- WS_NSPROTO_IPX
;
7607 /* convert the socket family, type and protocol */
7608 unixaf
= convert_af_w2u(af
);
7609 unixtype
= convert_socktype_w2u(type
);
7610 protocol
= convert_proto_w2u(protocol
);
7611 if (unixaf
== AF_UNSPEC
) unixaf
= -1;
7613 /* filter invalid parameters */
7616 /* the type could not be converted */
7617 if (type
&& unixtype
< 0)
7619 err
= WSAESOCKTNOSUPPORT
;
7623 err
= WSAEPROTONOSUPPORT
;
7628 /* both family and protocol can't be invalid */
7635 /* family could not be converted and neither socket type */
7636 if (unixtype
< 0 && af
>= 0)
7639 err
= WSAESOCKTNOSUPPORT
;
7643 err
= WSAEAFNOSUPPORT
;
7647 SERVER_START_REQ( create_socket
)
7649 req
->family
= unixaf
;
7650 req
->type
= unixtype
;
7651 req
->protocol
= protocol
;
7652 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
7653 req
->attributes
= (dwFlags
& WSA_FLAG_NO_HANDLE_INHERIT
) ? 0 : OBJ_INHERIT
;
7654 req
->flags
= dwFlags
& ~WSA_FLAG_NO_HANDLE_INHERIT
;
7655 err
= NtStatusToWSAError( wine_server_call( req
) );
7656 ret
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
7661 TRACE("\tcreated %04lx\n", ret
);
7663 set_ipx_packettype(ret
, ipxptype
);
7665 if (unixaf
== AF_INET
|| unixaf
== AF_INET6
)
7667 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
7668 if (unixtype
== SOCK_DGRAM
|| unixtype
== SOCK_RAW
) /* in Linux the global default can be enabled */
7669 set_dont_fragment(ret
, unixaf
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
7670 else if (unixtype
== SOCK_STREAM
)
7671 set_dont_fragment(ret
, unixaf
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
7675 if (unixaf
== AF_INET6
)
7677 int fd
= get_sock_fd(ret
, 0, NULL
);
7680 /* IPV6_V6ONLY is set by default on Windows */
7682 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
)))
7683 WARN("\tsetting IPV6_V6ONLY failed - errno = %i\n", errno
);
7684 release_sock_fd(ret
, fd
);
7691 if (err
== WSAEACCES
) /* raw socket denied */
7693 if (type
== SOCK_RAW
)
7694 ERR_(winediag
)("Failed to create a socket of type SOCK_RAW, this requires special permissions.\n");
7696 ERR_(winediag
)("Failed to create socket, this requires special permissions.\n");
7700 WARN("\t\tfailed, error %d!\n", err
);
7702 return INVALID_SOCKET
;
7705 /***********************************************************************
7706 * WSAJoinLeaf (WS2_32.58)
7709 SOCKET WINAPI
WSAJoinLeaf(
7711 const struct WS_sockaddr
*addr
,
7713 LPWSABUF lpCallerData
,
7714 LPWSABUF lpCalleeData
,
7720 return INVALID_SOCKET
;
7723 /***********************************************************************
7724 * __WSAFDIsSet (WS2_32.151)
7726 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
7728 int i
= set
->fd_count
, ret
= 0;
7731 if (set
->fd_array
[i
] == s
)
7737 TRACE("(socket %04lx, fd_set %p, count %i) <- %d\n", s
, set
, set
->fd_count
, ret
);
7741 /***********************************************************************
7742 * WSAIsBlocking (WS2_32.114)
7744 BOOL WINAPI
WSAIsBlocking(void)
7746 /* By default WinSock should set all its sockets to non-blocking mode
7747 * and poll in PeekMessage loop when processing "blocking" ones. This
7748 * function is supposed to tell if the program is in this loop. Our
7749 * blocking calls are truly blocking so we always return FALSE.
7751 * Note: It is allowed to call this function without prior WSAStartup().
7758 /***********************************************************************
7759 * WSACancelBlockingCall (WS2_32.113)
7761 INT WINAPI
WSACancelBlockingCall(void)
7767 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
7769 FIXME("How was this called?\n");
7774 /***********************************************************************
7775 * WSASetBlockingHook (WS2_32.109)
7777 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
7779 FARPROC prev
= blocking_hook
;
7780 blocking_hook
= lpBlockFunc
;
7781 TRACE("hook %p\n", lpBlockFunc
);
7786 /***********************************************************************
7787 * WSAUnhookBlockingHook (WS2_32.110)
7789 INT WINAPI
WSAUnhookBlockingHook(void)
7791 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
7796 /* ----------------------------------- end of API stuff */
7798 /* ----------------------------------- helper functions -
7800 * TODO: Merge WS_dup_..() stuff into one function that
7801 * would operate with a generic structure containing internal
7802 * pointers (via a template of some kind).
7805 static int list_size(char** l
, int item_size
)
7810 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
7811 j
+= (i
+ 1) * sizeof(char*); }
7815 static int list_dup(char** l_src
, char** l_to
, int item_size
)
7820 for (i
= 0; l_src
[i
]; i
++) ;
7821 p
= (char *)(l_to
+ i
+ 1);
7822 for (i
= 0; l_src
[i
]; i
++)
7824 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
7825 memcpy(p
, l_src
[i
], count
);
7830 return p
- (char *)l_to
;
7835 /* create a hostent entry
7837 * Creates the entry with enough memory for the name, aliases
7838 * addresses, and the address pointers. Also copies the name
7839 * and sets up all the pointers.
7841 * NOTE: The alias and address lists must be allocated with room
7842 * for the NULL item terminating the list. This is true even if
7843 * the list has no items ("aliases" and "addresses" must be
7844 * at least "1", a truly empty list is invalid).
7846 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int aliases_size
, int addresses
, int address_length
)
7848 struct WS_hostent
*p_to
;
7850 int size
= (sizeof(struct WS_hostent
) +
7852 sizeof(char *) * aliases
+
7854 sizeof(char *) * addresses
+
7855 address_length
* (addresses
- 1)), i
;
7857 if (!(p_to
= check_buffer_he(size
))) return NULL
;
7858 memset(p_to
, 0, size
);
7860 /* Use the memory in the same way winsock does.
7861 * First set the pointer for aliases, second set the pointers for addresses.
7862 * Third fill the addresses indexes, fourth jump aliases names size.
7863 * Fifth fill the hostname.
7864 * NOTE: This method is valid for OS version's >= XP.
7866 p
= (char *)(p_to
+ 1);
7867 p_to
->h_aliases
= (char **)p
;
7868 p
+= sizeof(char *)*aliases
;
7870 p_to
->h_addr_list
= (char **)p
;
7871 p
+= sizeof(char *)*addresses
;
7873 for (i
= 0, addresses
--; i
< addresses
; i
++, p
+= address_length
)
7874 p_to
->h_addr_list
[i
] = p
;
7876 /* NOTE: h_aliases must be filled in manually because we don't know each string
7877 * size, leave these pointers NULL (already set to NULL by memset earlier).
7887 /* duplicate hostent entry
7888 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
7889 * Ditto for protoent and servent.
7891 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
7893 int i
, addresses
= 0, alias_size
= 0;
7894 struct WS_hostent
*p_to
;
7897 for( i
= 0; p_he
->h_aliases
[i
]; i
++) alias_size
+= strlen(p_he
->h_aliases
[i
]) + 1;
7898 while (p_he
->h_addr_list
[addresses
]) addresses
++;
7900 p_to
= WS_create_he(p_he
->h_name
, i
+ 1, alias_size
, addresses
+ 1, p_he
->h_length
);
7902 if (!p_to
) return NULL
;
7903 p_to
->h_addrtype
= convert_af_u2w(p_he
->h_addrtype
);
7904 p_to
->h_length
= p_he
->h_length
;
7906 for(i
= 0, p
= p_to
->h_addr_list
[0]; p_he
->h_addr_list
[i
]; i
++, p
+= p_to
->h_length
)
7907 memcpy(p
, p_he
->h_addr_list
[i
], p_to
->h_length
);
7909 /* Fill the aliases after the IP data */
7910 for(i
= 0; p_he
->h_aliases
[i
]; i
++)
7912 p_to
->h_aliases
[i
] = p
;
7913 strcpy(p
, p_he
->h_aliases
[i
]);
7920 /* ----- protoent */
7922 static struct WS_protoent
*WS_create_pe( const char *name
, char **aliases
, int prot
)
7924 struct WS_protoent
*ret
;
7925 unsigned int size
= sizeof(*ret
) + strlen(name
) + sizeof(char *) + list_size(aliases
, 0);
7927 if (!(ret
= check_buffer_pe( size
))) return NULL
;
7928 ret
->p_proto
= prot
;
7929 ret
->p_name
= (char *)(ret
+ 1);
7930 strcpy( ret
->p_name
, name
);
7931 ret
->p_aliases
= (char **)ret
->p_name
+ strlen(name
) / sizeof(char *) + 1;
7932 list_dup( aliases
, ret
->p_aliases
, 0 );
7938 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
7941 struct WS_servent
*p_to
;
7943 int size
= (sizeof(*p_se
) +
7944 strlen(p_se
->s_proto
) + 1 +
7945 strlen(p_se
->s_name
) + 1 +
7946 list_size(p_se
->s_aliases
, 0));
7948 if (!(p_to
= check_buffer_se(size
))) return NULL
;
7949 p_to
->s_port
= p_se
->s_port
;
7951 p
= (char *)(p_to
+ 1);
7953 strcpy(p
, p_se
->s_name
);
7957 strcpy(p
, p_se
->s_proto
);
7960 p_to
->s_aliases
= (char **)p
;
7961 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
7966 /***********************************************************************
7967 * WSARecv (WS2_32.67)
7969 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
7970 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
7971 LPWSAOVERLAPPED lpOverlapped
,
7972 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
7974 return WS2_recv_base(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
7975 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
, NULL
);
7978 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
7979 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
7980 struct WS_sockaddr
*lpFrom
,
7981 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
7982 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
7983 LPWSABUF lpControlBuffer
)
7985 unsigned int i
, options
;
7986 int n
, fd
, err
, overlapped
, flags
;
7987 struct ws2_async
*wsa
= NULL
, localwsa
;
7989 DWORD timeout_start
= GetTickCount();
7990 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
7992 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
7993 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
7994 (lpFromlen
? *lpFromlen
: -1),
7995 lpOverlapped
, lpCompletionRoutine
);
7997 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
7998 TRACE( "fd=%d, options=%x\n", fd
, options
);
8000 if (fd
== -1) return SOCKET_ERROR
;
8002 if (*lpFlags
& WS_MSG_OOB
)
8004 /* It's invalid to receive OOB data from an OOBINLINED socket
8005 * as OOB data is turned into normal data. */
8006 socklen_t len
= sizeof(n
);
8007 if (!getsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, (char*) &n
, &len
) && n
)
8014 overlapped
= (lpOverlapped
|| lpCompletionRoutine
) &&
8015 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
8016 if (overlapped
|| dwBufferCount
> 1)
8018 if (!(wsa
= (struct ws2_async
*)alloc_async_io( offsetof(struct ws2_async
, iovec
[dwBufferCount
]),
8028 wsa
->hSocket
= SOCKET2HANDLE(s
);
8029 wsa
->flags
= *lpFlags
;
8030 wsa
->lpFlags
= lpFlags
;
8032 wsa
->addrlen
.ptr
= lpFromlen
;
8033 wsa
->control
= lpControlBuffer
;
8034 wsa
->n_iovecs
= dwBufferCount
;
8035 wsa
->first_iovec
= 0;
8036 for (i
= 0; i
< dwBufferCount
; i
++)
8038 /* check buffer first to trigger write watches */
8039 if (IsBadWritePtr( lpBuffers
[i
].buf
, lpBuffers
[i
].len
))
8044 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
8045 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
8048 flags
= convert_flags(wsa
->flags
);
8051 n
= WS2_recv( fd
, wsa
, flags
);
8054 /* Unix-like systems return EINVAL when attempting to read OOB data from
8055 * an empty socket buffer, convert that to a Windows expected return. */
8056 if ((flags
& MSG_OOB
) && errno
== EINVAL
)
8057 errno
= EWOULDBLOCK
;
8059 if (errno
!= EAGAIN
)
8065 else if (lpNumberOfBytesRecvd
) *lpNumberOfBytesRecvd
= n
;
8069 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
8071 wsa
->user_overlapped
= lpOverlapped
;
8072 wsa
->completion_func
= lpCompletionRoutine
;
8073 release_sock_fd( s
, fd
);
8077 iosb
->u
.Status
= STATUS_PENDING
;
8078 iosb
->Information
= 0;
8080 if (wsa
->completion_func
)
8081 err
= register_async( ASYNC_TYPE_READ
, wsa
->hSocket
, &wsa
->io
, NULL
,
8082 ws2_async_apc
, wsa
, iosb
);
8084 err
= register_async( ASYNC_TYPE_READ
, wsa
->hSocket
, &wsa
->io
, lpOverlapped
->hEvent
,
8085 NULL
, (void *)cvalue
, iosb
);
8087 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
8088 SetLastError(NtStatusToWSAError( err
));
8089 return SOCKET_ERROR
;
8092 iosb
->u
.Status
= STATUS_SUCCESS
;
8093 iosb
->Information
= n
;
8094 if (!wsa
->completion_func
)
8096 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
, FALSE
);
8097 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
8098 HeapFree( GetProcessHeap(), 0, wsa
);
8100 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
8101 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
8102 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
8108 if ((err
= sock_is_blocking( s
, &is_blocking
))) goto error
;
8113 int poll_timeout
= -1;
8114 INT64 timeout
= get_rcvsnd_timeo(fd
, TRUE
);
8118 timeout
-= GetTickCount() - timeout_start
;
8119 if (timeout
< 0) poll_timeout
= 0;
8120 else poll_timeout
= timeout
<= INT_MAX
? timeout
: INT_MAX
;
8124 pfd
.events
= POLLIN
;
8125 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
8127 if (!poll_timeout
|| !poll( &pfd
, 1, poll_timeout
))
8130 /* a timeout is not fatal */
8131 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
8137 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
8138 err
= WSAEWOULDBLOCK
;
8143 TRACE(" -> %i bytes\n", n
);
8144 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
8145 release_sock_fd( s
, fd
);
8146 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
8147 SetLastError(ERROR_SUCCESS
);
8152 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
8153 release_sock_fd( s
, fd
);
8154 WARN(" -> ERROR %d\n", err
);
8155 SetLastError( err
);
8156 return SOCKET_ERROR
;
8159 /***********************************************************************
8160 * WSARecvFrom (WS2_32.69)
8162 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
8163 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
8164 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
8165 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
8168 return WS2_recv_base( s
, lpBuffers
, dwBufferCount
,
8169 lpNumberOfBytesRecvd
, lpFlags
,
8171 lpOverlapped
, lpCompletionRoutine
, NULL
);
8174 /***********************************************************************
8175 * WSCInstallProvider (WS2_32.88)
8177 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
8178 LPCWSTR lpszProviderDllPath
,
8179 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
8180 DWORD dwNumberOfEntries
,
8183 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
8184 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
8185 dwNumberOfEntries
, lpErrno
);
8191 /***********************************************************************
8192 * WSCDeinstallProvider (WS2_32.83)
8194 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
8196 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
8202 /***********************************************************************
8203 * WSAAccept (WS2_32.26)
8205 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
8206 LPCONDITIONPROC lpfnCondition
, DWORD_PTR dwCallbackData
)
8210 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
8211 /* QOS SQOS, GQOS; */
8214 SOCKADDR src_addr
, dst_addr
;
8216 TRACE("socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %ld\n",
8217 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
8219 cs
= WS_accept(s
, addr
, addrlen
);
8220 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
8221 if (!lpfnCondition
) return cs
;
8223 if (addr
&& addrlen
)
8225 CallerId
.buf
= (char *)addr
;
8226 CallerId
.len
= *addrlen
;
8230 size
= sizeof(src_addr
);
8231 WS_getpeername(cs
, &src_addr
, &size
);
8232 CallerId
.buf
= (char *)&src_addr
;
8233 CallerId
.len
= size
;
8235 CallerData
.buf
= NULL
;
8238 size
= sizeof(dst_addr
);
8239 WS_getsockname(cs
, &dst_addr
, &size
);
8241 CalleeId
.buf
= (char *)&dst_addr
;
8242 CalleeId
.len
= sizeof(dst_addr
);
8244 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
8245 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
8252 SERVER_START_REQ( set_socket_deferred
)
8254 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
8255 req
->deferred
= wine_server_obj_handle( SOCKET2HANDLE(cs
) );
8256 if ( !wine_server_call_err ( req
) )
8258 SetLastError( WSATRY_AGAIN
);
8259 WS_closesocket( cs
);
8263 return SOCKET_ERROR
;
8266 SetLastError(WSAECONNREFUSED
);
8267 return SOCKET_ERROR
;
8269 FIXME("Unknown return type from Condition function\n");
8270 SetLastError(WSAENOTSOCK
);
8271 return SOCKET_ERROR
;
8275 /***********************************************************************
8276 * WSADuplicateSocketA (WS2_32.32)
8278 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
8280 return WS_DuplicateSocket(FALSE
, s
, dwProcessId
, (LPWSAPROTOCOL_INFOW
) lpProtocolInfo
);
8283 /***********************************************************************
8284 * WSADuplicateSocketW (WS2_32.33)
8286 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
8288 return WS_DuplicateSocket(TRUE
, s
, dwProcessId
, lpProtocolInfo
);
8291 /***********************************************************************
8292 * WSAInstallServiceClassA (WS2_32.48)
8294 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
8296 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
8297 SetLastError(WSAEACCES
);
8298 return SOCKET_ERROR
;
8301 /***********************************************************************
8302 * WSAInstallServiceClassW (WS2_32.49)
8304 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
8306 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
8307 SetLastError(WSAEACCES
);
8308 return SOCKET_ERROR
;
8311 /***********************************************************************
8312 * WSARemoveServiceClass (WS2_32.70)
8314 int WINAPI
WSARemoveServiceClass(LPGUID info
)
8316 FIXME("Request to remove service %s\n", debugstr_guid(info
));
8317 SetLastError(WSATYPE_NOT_FOUND
);
8318 return SOCKET_ERROR
;
8321 /***********************************************************************
8322 * inet_ntop (WS2_32.@)
8324 PCSTR WINAPI
WS_inet_ntop( INT family
, PVOID addr
, PSTR buffer
, SIZE_T len
)
8327 ULONG size
= min( len
, (ULONG
)-1 );
8329 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
8332 SetLastError( STATUS_INVALID_PARAMETER
);
8340 status
= RtlIpv4AddressToStringExA( (IN_ADDR
*)addr
, 0, buffer
, &size
);
8345 status
= RtlIpv6AddressToStringExA( (IN6_ADDR
*)addr
, 0, 0, buffer
, &size
);
8349 SetLastError( WSAEAFNOSUPPORT
);
8353 if (status
== STATUS_SUCCESS
) return buffer
;
8354 SetLastError( STATUS_INVALID_PARAMETER
);
8358 /***********************************************************************
8359 * inet_pton (WS2_32.@)
8361 INT WINAPI
WS_inet_pton(INT family
, const char *addr
, void *buffer
)
8364 const char *terminator
;
8366 TRACE("family %d, addr %s, buffer (%p)\n", family
, debugstr_a(addr
), buffer
);
8368 if (!addr
|| !buffer
)
8370 SetLastError(WSAEFAULT
);
8371 return SOCKET_ERROR
;
8377 status
= RtlIpv4StringToAddressA(addr
, TRUE
, &terminator
, buffer
);
8380 status
= RtlIpv6StringToAddressA(addr
, &terminator
, buffer
);
8383 SetLastError(WSAEAFNOSUPPORT
);
8384 return SOCKET_ERROR
;
8387 return (status
== STATUS_SUCCESS
&& *terminator
== 0);
8390 /***********************************************************************
8391 * InetPtonW (WS2_32.@)
8393 INT WINAPI
InetPtonW(INT family
, PCWSTR addr
, PVOID buffer
)
8399 TRACE("family %d, addr %s, buffer (%p)\n", family
, debugstr_w(addr
), buffer
);
8403 SetLastError(WSAEFAULT
);
8404 return SOCKET_ERROR
;
8407 len
= WideCharToMultiByte(CP_ACP
, 0, addr
, -1, NULL
, 0, NULL
, NULL
);
8408 if (!(addrA
= HeapAlloc(GetProcessHeap(), 0, len
)))
8410 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8411 return SOCKET_ERROR
;
8413 WideCharToMultiByte(CP_ACP
, 0, addr
, -1, addrA
, len
, NULL
, NULL
);
8415 ret
= WS_inet_pton(family
, addrA
, buffer
);
8416 if (!ret
) SetLastError(WSAEINVAL
);
8418 HeapFree(GetProcessHeap(), 0, addrA
);
8422 /***********************************************************************
8423 * InetNtopW (WS2_32.@)
8425 PCWSTR WINAPI
InetNtopW(INT family
, PVOID addr
, PWSTR buffer
, SIZE_T len
)
8427 char bufferA
[WS_INET6_ADDRSTRLEN
];
8430 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
8432 if (WS_inet_ntop(family
, addr
, bufferA
, sizeof(bufferA
)))
8434 if (MultiByteToWideChar(CP_ACP
, 0, bufferA
, -1, buffer
, len
))
8437 SetLastError(ERROR_INVALID_PARAMETER
);
8442 /***********************************************************************
8443 * WSAStringToAddressA (WS2_32.80)
8445 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
8447 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
8448 LPSOCKADDR lpAddress
,
8449 LPINT lpAddressLength
)
8454 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_a(AddressString
), AddressFamily
,
8455 lpProtocolInfo
, lpAddress
, lpAddressLength
);
8457 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
8461 SetLastError(WSAEINVAL
);
8462 return SOCKET_ERROR
;
8466 FIXME("ProtocolInfo not implemented.\n");
8468 switch(AddressFamily
)
8472 SOCKADDR_IN
*addr4
= (SOCKADDR_IN
*)lpAddress
;
8474 /* If lpAddressLength is too small, tell caller the size we need */
8475 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
8477 *lpAddressLength
= sizeof(SOCKADDR_IN
);
8481 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
8483 status
= RtlIpv4StringToAddressExA(AddressString
, FALSE
, &addr4
->sin_addr
, &addr4
->sin_port
);
8484 if (status
!= STATUS_SUCCESS
)
8489 addr4
->sin_family
= WS_AF_INET
;
8490 *lpAddressLength
= sizeof(SOCKADDR_IN
);
8495 SOCKADDR_IN6
*addr6
= (SOCKADDR_IN6
*)lpAddress
;
8497 /* If lpAddressLength is too small, tell caller the size we need */
8498 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
8500 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
8504 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
8506 status
= RtlIpv6StringToAddressExA(AddressString
, &addr6
->sin6_addr
, &addr6
->sin6_scope_id
, &addr6
->sin6_port
);
8507 if (status
!= STATUS_SUCCESS
)
8512 addr6
->sin6_family
= WS_AF_INET6
;
8513 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
8517 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
8518 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
8524 return SOCKET_ERROR
;
8527 /***********************************************************************
8528 * WSAStringToAddressW (WS2_32.81)
8530 * FIXME: Does anybody know if this function allows using Hebrew/Arabic/Chinese... digits?
8531 * If this should be the case, it would be required to map these digits
8532 * to Unicode digits (0-9) using FoldString first.
8534 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
8536 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
8537 LPSOCKADDR lpAddress
,
8538 LPINT lpAddressLength
)
8541 LPSTR workBuffer
=NULL
;
8542 WSAPROTOCOL_INFOA infoA
;
8543 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
8545 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
8546 lpAddress
, lpAddressLength
);
8548 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
8550 /* if ProtocolInfo is available - convert to ANSI variant */
8553 lpProtoInfoA
= &infoA
;
8554 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
8556 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
8557 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
8559 SetLastError(WSAEINVAL
);
8560 return SOCKET_ERROR
;
8566 /* Translate AddressString to ANSI code page - assumes that only
8567 standard digits 0-9 are used with this API call */
8568 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
8569 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
8573 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
8574 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
8575 lpAddress
,lpAddressLength
);
8576 HeapFree( GetProcessHeap(), 0, workBuffer
);
8580 res
= WSA_NOT_ENOUGH_MEMORY
;
8586 return SOCKET_ERROR
;
8589 /***********************************************************************
8590 * WSAAddressToStringA (WS2_32.27)
8592 * See WSAAddressToStringW
8594 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
8595 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
8599 CHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
8602 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
8604 if (!sockaddr
) return SOCKET_ERROR
;
8605 if (!string
|| !lenstr
) return SOCKET_ERROR
;
8607 switch(sockaddr
->sa_family
)
8611 unsigned int long_ip
= ntohl(((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
);
8612 if (len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
8613 sprintf( buffer
, "%u.%u.%u.%u:%u",
8614 (long_ip
>> 24) & 0xff,
8615 (long_ip
>> 16) & 0xff,
8616 (long_ip
>> 8) & 0xff,
8618 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
8620 p
= strchr( buffer
, ':' );
8621 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
8626 struct WS_sockaddr_in6
*sockaddr6
= (LPSOCKADDR_IN6
) sockaddr
;
8630 if (len
< sizeof(SOCKADDR_IN6
)) return SOCKET_ERROR
;
8631 if ((sockaddr6
->sin6_port
))
8632 strcpy(buffer
, "[");
8633 slen
= strlen(buffer
);
8634 if (!WS_inet_ntop(WS_AF_INET6
, &sockaddr6
->sin6_addr
, &buffer
[slen
], sizeof(buffer
) - slen
))
8636 SetLastError(WSAEINVAL
);
8637 return SOCKET_ERROR
;
8639 if ((sockaddr6
->sin6_scope_id
))
8640 sprintf(buffer
+strlen(buffer
), "%%%u", sockaddr6
->sin6_scope_id
);
8641 if ((sockaddr6
->sin6_port
))
8642 sprintf(buffer
+strlen(buffer
), "]:%u", ntohs(sockaddr6
->sin6_port
));
8647 SetLastError(WSAEINVAL
);
8648 return SOCKET_ERROR
;
8651 size
= strlen( buffer
) + 1;
8656 SetLastError(WSAEFAULT
);
8657 return SOCKET_ERROR
;
8660 TRACE("=> %s,%u bytes\n", debugstr_a(buffer
), size
);
8662 strcpy( string
, buffer
);
8666 /***********************************************************************
8667 * WSAAddressToStringW (WS2_32.28)
8669 * Convert a sockaddr address into a readable address string.
8672 * sockaddr [I] Pointer to a sockaddr structure.
8673 * len [I] Size of the sockaddr structure.
8674 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
8675 * string [I/O] Pointer to a buffer to receive the address string.
8676 * lenstr [I/O] Size of the receive buffer in WCHARs.
8680 * Failure: SOCKET_ERROR
8683 * The 'info' parameter is ignored.
8685 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
8686 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
8691 WCHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
8694 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
8697 ret
= WSAAddressToStringA(sockaddr
, len
, NULL
, bufAddr
, &size
);
8699 if (ret
) return ret
;
8701 MultiByteToWideChar(CP_ACP
, 0, bufAddr
, size
, buffer
, ARRAY_SIZE(buffer
));
8706 SetLastError(WSAEFAULT
);
8707 return SOCKET_ERROR
;
8710 TRACE("=> %s,%u bytes\n", debugstr_w(buffer
), size
);
8712 lstrcpyW( string
, buffer
);
8716 /***********************************************************************
8717 * WSAEnumNameSpaceProvidersA (WS2_32.34)
8719 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
8721 FIXME( "(%p %p) Stub!\n", len
, buffer
);
8725 /***********************************************************************
8726 * WSAEnumNameSpaceProvidersW (WS2_32.35)
8728 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
8730 FIXME( "(%p %p) Stub!\n", len
, buffer
);
8734 /***********************************************************************
8735 * WSAGetQOSByName (WS2_32.41)
8737 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
8739 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
8743 /***********************************************************************
8744 * WSAGetServiceClassInfoA (WS2_32.42)
8746 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
8747 LPWSASERVICECLASSINFOA info
)
8749 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
8751 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8752 return SOCKET_ERROR
;
8755 /***********************************************************************
8756 * WSAGetServiceClassInfoW (WS2_32.43)
8758 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
8759 LPWSASERVICECLASSINFOW info
)
8761 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
8763 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8764 return SOCKET_ERROR
;
8767 /***********************************************************************
8768 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
8770 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
8772 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
8773 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8774 return SOCKET_ERROR
;
8777 /***********************************************************************
8778 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
8780 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
8782 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
8783 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8784 return SOCKET_ERROR
;
8787 /***********************************************************************
8788 * WSALookupServiceBeginA (WS2_32.59)
8790 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
8791 DWORD dwControlFlags
,
8794 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
8796 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8797 return SOCKET_ERROR
;
8800 /***********************************************************************
8801 * WSALookupServiceBeginW (WS2_32.60)
8803 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
8804 DWORD dwControlFlags
,
8807 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
8809 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8810 return SOCKET_ERROR
;
8813 /***********************************************************************
8814 * WSALookupServiceEnd (WS2_32.61)
8816 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
8818 FIXME("(%p) Stub!\n", lookup
);
8822 /***********************************************************************
8823 * WSALookupServiceNextA (WS2_32.62)
8825 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
8827 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
8828 SetLastError(WSA_E_NO_MORE
);
8829 return SOCKET_ERROR
;
8832 /***********************************************************************
8833 * WSALookupServiceNextW (WS2_32.63)
8835 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
8837 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
8838 SetLastError(WSA_E_NO_MORE
);
8839 return SOCKET_ERROR
;
8842 /***********************************************************************
8843 * WSANtohl (WS2_32.64)
8845 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
8847 TRACE( "(%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
8849 if (!lphostlong
) return WSAEFAULT
;
8851 *lphostlong
= ntohl( netlong
);
8855 /***********************************************************************
8856 * WSANtohs (WS2_32.65)
8858 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
8860 TRACE( "(%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
8862 if (!lphostshort
) return WSAEFAULT
;
8864 *lphostshort
= ntohs( netshort
);
8868 /***********************************************************************
8869 * WSAProviderConfigChange (WS2_32.66)
8871 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
8872 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
8874 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
8875 return SOCKET_ERROR
;
8878 /***********************************************************************
8879 * WSARecvDisconnect (WS2_32.68)
8881 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
8883 TRACE( "(%04lx %p)\n", s
, disconnectdata
);
8885 return WS_shutdown( s
, SD_RECEIVE
);
8888 /***********************************************************************
8889 * WSASetServiceA (WS2_32.76)
8891 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
8893 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
8897 /***********************************************************************
8898 * WSASetServiceW (WS2_32.77)
8900 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
8902 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
8906 /***********************************************************************
8907 * WSCEnableNSProvider (WS2_32.84)
8909 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
8911 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
8915 /***********************************************************************
8916 * WSCGetProviderInfo
8918 INT WINAPI
WSCGetProviderInfo( LPGUID provider
, WSC_PROVIDER_INFO_TYPE info_type
,
8919 PBYTE info
, size_t* len
, DWORD flags
, LPINT errcode
)
8921 FIXME( "(%s 0x%08x %p %p 0x%08x %p) Stub!\n",
8922 debugstr_guid(provider
), info_type
, info
, len
, flags
, errcode
);
8925 return SOCKET_ERROR
;
8928 *errcode
= WSAEFAULT
;
8929 return SOCKET_ERROR
;
8932 *errcode
= WSANO_RECOVERY
;
8933 return SOCKET_ERROR
;
8936 /***********************************************************************
8937 * WSCGetProviderPath (WS2_32.86)
8939 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
8941 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
8943 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
8945 *errcode
= WSAEINVAL
;
8946 return SOCKET_ERROR
;
8949 /***********************************************************************
8950 * WSCInstallNameSpace (WS2_32.87)
8952 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
8953 DWORD version
, LPGUID provider
)
8955 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
8956 namespace, version
, debugstr_guid(provider
) );
8960 /***********************************************************************
8961 * WSCUnInstallNameSpace (WS2_32.89)
8963 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
8965 FIXME("(%s) Stub!\n", debugstr_guid(lpProviderId
));
8969 /***********************************************************************
8970 * WSCWriteProviderOrder (WS2_32.91)
8972 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
8974 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);
8978 /***********************************************************************
8979 * WSCSetApplicationCategory (WS2_32.@)
8981 INT WINAPI
WSCSetApplicationCategory( LPCWSTR path
, DWORD len
, LPCWSTR extra
, DWORD extralen
,
8982 DWORD lspcat
, DWORD
*prev_lspcat
, LPINT err
)
8984 FIXME("(%s %d %s %d %d %p) Stub!\n", debugstr_w(path
), len
, debugstr_w(extra
),
8985 extralen
, lspcat
, prev_lspcat
);
8989 /***********************************************************************
8990 * WSANSPIoctl (WS2_32.91)
8992 INT WINAPI
WSANSPIoctl( HANDLE hLookup
, DWORD dwControlCode
, LPVOID lpvInBuffer
,
8993 DWORD cbInBuffer
, LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
8994 LPDWORD lpcbBytesReturned
, LPWSACOMPLETION lpCompletion
)
8996 FIXME("(%p, 0x%08x, %p, 0x%08x, %p, 0x%08x, %p, %p) Stub!\n", hLookup
, dwControlCode
,
8997 lpvInBuffer
, cbInBuffer
, lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpCompletion
);
8998 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8999 return SOCKET_ERROR
;
9002 /*****************************************************************************
9003 * WSAEnumProtocolsA [WS2_32.@]
9005 * see function WSAEnumProtocolsW
9007 INT WINAPI
WSAEnumProtocolsA( LPINT protocols
, LPWSAPROTOCOL_INFOA buffer
, LPDWORD len
)
9009 return WS_EnumProtocols( FALSE
, protocols
, (LPWSAPROTOCOL_INFOW
) buffer
, len
);
9012 /*****************************************************************************
9013 * WSAEnumProtocolsW [WS2_32.@]
9015 * Retrieves information about specified set of active network protocols.
9018 * protocols [I] Pointer to null-terminated array of protocol id's. NULL
9019 * retrieves information on all available protocols.
9020 * buffer [I] Pointer to a buffer to be filled with WSAPROTOCOL_INFO
9022 * len [I/O] Pointer to a variable specifying buffer size. On output
9023 * the variable holds the number of bytes needed when the
9024 * specified size is too small.
9027 * Success: number of WSAPROTOCOL_INFO structures in buffer.
9028 * Failure: SOCKET_ERROR
9031 * NT4SP5 does not return SPX if protocols == NULL
9034 * - NT4SP5 returns in addition these list of NETBIOS protocols
9035 * (address family 17), each entry two times one for socket type 2 and 5
9037 * iProtocol szProtocol
9038 * 0x80000000 \Device\NwlnkNb
9039 * 0xfffffffa \Device\NetBT_CBENT7
9040 * 0xfffffffb \Device\Nbf_CBENT7
9041 * 0xfffffffc \Device\NetBT_NdisWan5
9042 * 0xfffffffd \Device\NetBT_El9202
9043 * 0xfffffffe \Device\Nbf_El9202
9044 * 0xffffffff \Device\Nbf_NdisWan4
9046 * - there is no check that the operating system supports the returned
9049 INT WINAPI
WSAEnumProtocolsW( LPINT protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
)
9051 return WS_EnumProtocols( TRUE
, protocols
, buffer
, len
);
9054 /*****************************************************************************
9055 * WSCEnumProtocols [WS2_32.@]
9058 * protocols [I] Null-terminated array of iProtocol values.
9059 * buffer [O] Buffer of WSAPROTOCOL_INFOW structures.
9060 * len [I/O] Size of buffer on input/output.
9061 * errno [O] Error code.
9064 * Success: number of protocols to be reported on.
9065 * Failure: SOCKET_ERROR. error is in errno.
9068 * Doesn't supply info on layered protocols.
9071 INT WINAPI
WSCEnumProtocols( LPINT protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
, LPINT err
)
9073 INT ret
= WSAEnumProtocolsW( protocols
, buffer
, len
);
9075 if (ret
== SOCKET_ERROR
) *err
= WSAENOBUFS
;