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 "wine/server.h"
158 #include "wine/debug.h"
159 #include "wine/exception.h"
160 #include "wine/unicode.h"
162 #if defined(linux) && !defined(IP_UNICAST_IF)
163 #define IP_UNICAST_IF 50
166 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
167 # define sipx_network sipx_addr.x_net
168 # define sipx_node sipx_addr.x_host.c_host
169 #endif /* __FreeBSD__ */
172 #define INADDR_NONE ~0UL
175 #if !defined(TCP_KEEPIDLE) && defined(TCP_KEEPALIVE)
176 /* TCP_KEEPALIVE is the Mac OS name for TCP_KEEPIDLE */
177 #define TCP_KEEPIDLE TCP_KEEPALIVE
180 #define FILE_USE_FILE_POINTER_POSITION ((LONGLONG)-2)
182 WINE_DEFAULT_DEBUG_CHANNEL(winsock
);
183 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
185 /* names of the protocols */
186 static const WCHAR NameIpxW
[] = {'I', 'P', 'X', '\0'};
187 static const WCHAR NameSpxW
[] = {'S', 'P', 'X', '\0'};
188 static const WCHAR NameSpxIIW
[] = {'S', 'P', 'X', ' ', 'I', 'I', '\0'};
189 static const WCHAR NameTcpW
[] = {'T', 'C', 'P', '/', 'I', 'P', '\0'};
190 static const WCHAR NameUdpW
[] = {'U', 'D', 'P', '/', 'I', 'P', '\0'};
192 /* Taken from Win2k */
193 static const GUID ProviderIdIP
= { 0xe70f1aa0, 0xab8b, 0x11cf,
194 { 0x8c, 0xa3, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } };
195 static const GUID ProviderIdIPX
= { 0x11058240, 0xbe47, 0x11cf,
196 { 0x95, 0xc8, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } };
197 static const GUID ProviderIdSPX
= { 0x11058241, 0xbe47, 0x11cf,
198 { 0x95, 0xc8, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } };
200 static const INT valid_protocols
[] =
210 #define IS_IPX_PROTO(X) ((X) >= WS_NSPROTO_IPX && (X) <= WS_NSPROTO_IPX + 255)
212 #if defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER)
213 # define LINUX_BOUND_IF
214 struct interface_filter
{
215 struct sock_filter iface_memaddr
;
216 struct sock_filter iface_rule
;
217 struct sock_filter ip_memaddr
;
218 struct sock_filter ip_rule
;
219 struct sock_filter return_keep
;
220 struct sock_filter return_dump
;
222 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
223 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
224 /sizeof(struct sock_filter)
225 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
226 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
227 /sizeof(struct sock_filter)
228 # define FILTER_JUMP_NEXT() (u_char)(0)
229 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
230 static struct interface_filter generic_interface_filter
= {
231 /* This filter rule allows incoming packets on the specified interface, which works for all
232 * remotely generated packets and for locally generated broadcast packets. */
233 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
234 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
235 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
236 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
237 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
238 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
239 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
240 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
242 #endif /* LINUX_BOUND_IF */
245 * The actual definition of WSASendTo, wrapped in a different function name
246 * so that internal calls from ws2_32 itself will not trigger programs like
247 * Garena, which hooks WSASendTo/WSARecvFrom calls.
249 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
250 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
251 const struct WS_sockaddr
*to
, int tolen
,
252 LPWSAOVERLAPPED lpOverlapped
,
253 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
256 * Internal fundamental receive function, essentially WSARecvFrom with an
257 * additional parameter to support message control headers.
259 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
260 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
261 struct WS_sockaddr
*lpFrom
,
262 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
263 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
264 LPWSABUF lpControlBuffer
);
266 /* critical section to protect some non-reentrant net function */
267 static CRITICAL_SECTION csWSgetXXXbyYYY
;
268 static CRITICAL_SECTION_DEBUG critsect_debug
=
270 0, 0, &csWSgetXXXbyYYY
,
271 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
272 0, 0, { (DWORD_PTR
)(__FILE__
": csWSgetXXXbyYYY") }
274 static CRITICAL_SECTION csWSgetXXXbyYYY
= { &critsect_debug
, -1, 0, 0, 0, 0 };
276 union generic_unix_sockaddr
278 struct sockaddr addr
;
279 char data
[128]; /* should be big enough for all families */
282 static inline const char *debugstr_sockaddr( const struct WS_sockaddr
*a
)
284 if (!a
) return "(nil)";
285 switch (a
->sa_family
)
291 struct WS_sockaddr_in
*sin
= (struct WS_sockaddr_in
*)a
;
293 p
= WS_inet_ntop( WS_AF_INET
, &sin
->sin_addr
, buf
, sizeof(buf
) );
295 p
= "(unknown IPv4 address)";
297 return wine_dbg_sprintf("{ family AF_INET, address %s, port %d }",
298 p
, ntohs(sin
->sin_port
));
304 struct WS_sockaddr_in6
*sin
= (struct WS_sockaddr_in6
*)a
;
306 p
= WS_inet_ntop( WS_AF_INET6
, &sin
->sin6_addr
, buf
, sizeof(buf
) );
308 p
= "(unknown IPv6 address)";
309 return wine_dbg_sprintf("{ family AF_INET6, address %s, port %d }",
310 p
, ntohs(sin
->sin6_port
));
315 char netnum
[16], nodenum
[16];
316 struct WS_sockaddr_ipx
*sin
= (struct WS_sockaddr_ipx
*)a
;
318 for (i
= 0;i
< 4; i
++) sprintf(netnum
+ i
* 2, "%02X", (unsigned char) sin
->sa_netnum
[i
]);
319 for (i
= 0;i
< 6; i
++) sprintf(nodenum
+ i
* 2, "%02X", (unsigned char) sin
->sa_nodenum
[i
]);
321 return wine_dbg_sprintf("{ family AF_IPX, address %s.%s, ipx socket %d }",
322 netnum
, nodenum
, sin
->sa_socket
);
328 memcpy( &addr
, ((const SOCKADDR_IRDA
*)a
)->irdaDeviceID
, sizeof(addr
) );
329 addr
= ntohl( addr
);
330 return wine_dbg_sprintf("{ family AF_IRDA, addr %08x, name %s }",
332 ((const SOCKADDR_IRDA
*)a
)->irdaServiceName
);
335 return wine_dbg_sprintf("{ family %d }", a
->sa_family
);
339 static inline const char *debugstr_sockopt(int level
, int optname
)
341 const char *stropt
= NULL
, *strlevel
= NULL
;
343 #define DEBUG_SOCKLEVEL(x) case (x): strlevel = #x
344 #define DEBUG_SOCKOPT(x) case (x): stropt = #x; break
348 DEBUG_SOCKLEVEL(WS_SOL_SOCKET
);
351 DEBUG_SOCKOPT(WS_SO_ACCEPTCONN
);
352 DEBUG_SOCKOPT(WS_SO_BROADCAST
);
353 DEBUG_SOCKOPT(WS_SO_BSP_STATE
);
354 DEBUG_SOCKOPT(WS_SO_CONDITIONAL_ACCEPT
);
355 DEBUG_SOCKOPT(WS_SO_CONNECT_TIME
);
356 DEBUG_SOCKOPT(WS_SO_DEBUG
);
357 DEBUG_SOCKOPT(WS_SO_DONTLINGER
);
358 DEBUG_SOCKOPT(WS_SO_DONTROUTE
);
359 DEBUG_SOCKOPT(WS_SO_ERROR
);
360 DEBUG_SOCKOPT(WS_SO_EXCLUSIVEADDRUSE
);
361 DEBUG_SOCKOPT(WS_SO_GROUP_ID
);
362 DEBUG_SOCKOPT(WS_SO_GROUP_PRIORITY
);
363 DEBUG_SOCKOPT(WS_SO_KEEPALIVE
);
364 DEBUG_SOCKOPT(WS_SO_LINGER
);
365 DEBUG_SOCKOPT(WS_SO_MAX_MSG_SIZE
);
366 DEBUG_SOCKOPT(WS_SO_OOBINLINE
);
367 DEBUG_SOCKOPT(WS_SO_OPENTYPE
);
368 DEBUG_SOCKOPT(WS_SO_PROTOCOL_INFOA
);
369 DEBUG_SOCKOPT(WS_SO_PROTOCOL_INFOW
);
370 DEBUG_SOCKOPT(WS_SO_RCVBUF
);
371 DEBUG_SOCKOPT(WS_SO_RCVTIMEO
);
372 DEBUG_SOCKOPT(WS_SO_REUSEADDR
);
373 DEBUG_SOCKOPT(WS_SO_SNDBUF
);
374 DEBUG_SOCKOPT(WS_SO_SNDTIMEO
);
375 DEBUG_SOCKOPT(WS_SO_TYPE
);
376 DEBUG_SOCKOPT(WS_SO_UPDATE_CONNECT_CONTEXT
);
380 DEBUG_SOCKLEVEL(WS_NSPROTO_IPX
);
383 DEBUG_SOCKOPT(WS_IPX_PTYPE
);
384 DEBUG_SOCKOPT(WS_IPX_FILTERPTYPE
);
385 DEBUG_SOCKOPT(WS_IPX_DSTYPE
);
386 DEBUG_SOCKOPT(WS_IPX_RECVHDR
);
387 DEBUG_SOCKOPT(WS_IPX_MAXSIZE
);
388 DEBUG_SOCKOPT(WS_IPX_ADDRESS
);
389 DEBUG_SOCKOPT(WS_IPX_MAX_ADAPTER_NUM
);
393 DEBUG_SOCKLEVEL(WS_SOL_IRLMP
);
396 DEBUG_SOCKOPT(WS_IRLMP_ENUMDEVICES
);
400 DEBUG_SOCKLEVEL(WS_IPPROTO_TCP
);
403 DEBUG_SOCKOPT(WS_TCP_BSDURGENT
);
404 DEBUG_SOCKOPT(WS_TCP_EXPEDITED_1122
);
405 DEBUG_SOCKOPT(WS_TCP_NODELAY
);
409 DEBUG_SOCKLEVEL(WS_IPPROTO_IP
);
412 DEBUG_SOCKOPT(WS_IP_ADD_MEMBERSHIP
);
413 DEBUG_SOCKOPT(WS_IP_DONTFRAGMENT
);
414 DEBUG_SOCKOPT(WS_IP_DROP_MEMBERSHIP
);
415 DEBUG_SOCKOPT(WS_IP_HDRINCL
);
416 DEBUG_SOCKOPT(WS_IP_MULTICAST_IF
);
417 DEBUG_SOCKOPT(WS_IP_MULTICAST_LOOP
);
418 DEBUG_SOCKOPT(WS_IP_MULTICAST_TTL
);
419 DEBUG_SOCKOPT(WS_IP_OPTIONS
);
420 DEBUG_SOCKOPT(WS_IP_PKTINFO
);
421 DEBUG_SOCKOPT(WS_IP_RECEIVE_BROADCAST
);
422 DEBUG_SOCKOPT(WS_IP_TOS
);
423 DEBUG_SOCKOPT(WS_IP_TTL
);
424 DEBUG_SOCKOPT(WS_IP_UNICAST_IF
);
428 DEBUG_SOCKLEVEL(WS_IPPROTO_IPV6
);
431 DEBUG_SOCKOPT(WS_IPV6_ADD_MEMBERSHIP
);
432 DEBUG_SOCKOPT(WS_IPV6_DROP_MEMBERSHIP
);
433 DEBUG_SOCKOPT(WS_IPV6_MULTICAST_IF
);
434 DEBUG_SOCKOPT(WS_IPV6_MULTICAST_HOPS
);
435 DEBUG_SOCKOPT(WS_IPV6_MULTICAST_LOOP
);
436 DEBUG_SOCKOPT(WS_IPV6_UNICAST_HOPS
);
437 DEBUG_SOCKOPT(WS_IPV6_V6ONLY
);
438 DEBUG_SOCKOPT(WS_IPV6_UNICAST_IF
);
439 DEBUG_SOCKOPT(WS_IPV6_DONTFRAG
);
443 #undef DEBUG_SOCKLEVEL
447 strlevel
= wine_dbg_sprintf("WS_0x%x", level
);
449 stropt
= wine_dbg_sprintf("WS_0x%x", optname
);
451 return wine_dbg_sprintf("level %s, name %s", strlevel
+ 3, stropt
+ 3);
454 static inline const char *debugstr_optval(const char *optval
, int optlenval
)
456 if (optval
&& !IS_INTRESOURCE(optval
) && optlenval
>= 1 && optlenval
<= sizeof(DWORD
))
459 memcpy(&value
, optval
, optlenval
);
460 return wine_dbg_sprintf("%p (%u)", optval
, value
);
462 return wine_dbg_sprintf("%p", optval
);
465 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
466 #define SOCKET2HANDLE(s) ((HANDLE)(s))
467 #define HANDLE2SOCKET(h) ((SOCKET)(h))
469 /****************************************************************
470 * Async IO declarations
471 ****************************************************************/
475 struct ws2_async_io
*next
;
478 struct ws2_async_shutdown
480 struct ws2_async_io io
;
482 IO_STATUS_BLOCK iosb
;
488 struct ws2_async_io io
;
490 LPWSAOVERLAPPED user_overlapped
;
491 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func
;
492 IO_STATUS_BLOCK local_iosb
;
493 struct WS_sockaddr
*addr
;
496 int val
; /* for send operations */
497 int *ptr
; /* for recv operations */
502 unsigned int n_iovecs
;
503 unsigned int first_iovec
;
504 struct iovec iovec
[1];
507 struct ws2_accept_async
509 struct ws2_async_io io
;
510 HANDLE listen_socket
;
511 HANDLE accept_socket
;
512 LPOVERLAPPED user_overlapped
;
514 PVOID buf
; /* buffer to write data to */
518 struct ws2_async
*read
;
521 struct ws2_transmitfile_async
523 struct ws2_async_io io
;
528 DWORD bytes_per_send
;
529 TRANSMIT_FILE_BUFFERS buffers
;
531 LARGE_INTEGER offset
;
532 struct ws2_async write
;
535 static struct ws2_async_io
*async_io_freelist
;
537 static void release_async_io( struct ws2_async_io
*io
)
541 struct ws2_async_io
*next
= async_io_freelist
;
543 if (InterlockedCompareExchangePointer( (void **)&async_io_freelist
, io
, next
) == next
) return;
547 static struct ws2_async_io
*alloc_async_io( DWORD size
)
549 /* first free remaining previous fileinfos */
551 struct ws2_async_io
*io
= InterlockedExchangePointer( (void **)&async_io_freelist
, NULL
);
555 struct ws2_async_io
*next
= io
->next
;
556 HeapFree( GetProcessHeap(), 0, io
);
560 return HeapAlloc( GetProcessHeap(), 0, size
);
563 /****************************************************************/
565 /* ----------------------------------- internal data */
567 /* ws_... struct conversion flags */
569 typedef struct /* WSAAsyncSelect() control struct */
571 HANDLE service
, event
, sock
;
577 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
578 #define WS_MAX_UDP_DATAGRAM 1024
579 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
581 /* hostent's, servent's and protent's are stored in one buffer per thread,
582 * as documented on MSDN for the functions that return any of the buffers */
583 struct per_thread_data
586 struct WS_hostent
*he_buffer
;
587 struct WS_servent
*se_buffer
;
588 struct WS_protoent
*pe_buffer
;
592 char ntoa_buffer
[16]; /* 4*3 digits + 3 '.' + 1 '\0' */
595 /* internal: routing description information */
599 DWORD metric
, default_route
;
602 static INT num_startup
; /* reference counter */
603 static FARPROC blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
605 /* function prototypes */
606 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int aliases_size
, int addresses
, int address_length
);
607 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
608 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
);
609 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
610 static int ws_protocol_info(SOCKET s
, int unicode
, WSAPROTOCOL_INFOW
*buffer
, int *size
);
612 int WSAIOCTL_GetInterfaceCount(void);
613 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
615 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
, ULONG Information
);
617 #define MAP_OPTION(opt) { WS_##opt, opt }
619 static const int ws_flags_map
[][2] =
621 MAP_OPTION( MSG_OOB
),
622 MAP_OPTION( MSG_PEEK
),
623 MAP_OPTION( MSG_DONTROUTE
),
624 MAP_OPTION( MSG_WAITALL
),
627 static const int ws_sock_map
[][2] =
629 MAP_OPTION( SO_DEBUG
),
630 MAP_OPTION( SO_ACCEPTCONN
),
631 MAP_OPTION( SO_REUSEADDR
),
632 MAP_OPTION( SO_KEEPALIVE
),
633 MAP_OPTION( SO_DONTROUTE
),
634 MAP_OPTION( SO_BROADCAST
),
635 MAP_OPTION( SO_LINGER
),
636 MAP_OPTION( SO_OOBINLINE
),
637 MAP_OPTION( SO_SNDBUF
),
638 MAP_OPTION( SO_RCVBUF
),
639 MAP_OPTION( SO_ERROR
),
640 MAP_OPTION( SO_TYPE
),
642 MAP_OPTION( SO_RCVTIMEO
),
645 MAP_OPTION( SO_SNDTIMEO
),
649 static const int ws_tcp_map
[][2] =
652 MAP_OPTION( TCP_NODELAY
),
656 static const int ws_ip_map
[][2] =
658 MAP_OPTION( IP_MULTICAST_IF
),
659 MAP_OPTION( IP_MULTICAST_TTL
),
660 MAP_OPTION( IP_MULTICAST_LOOP
),
661 MAP_OPTION( IP_ADD_MEMBERSHIP
),
662 MAP_OPTION( IP_DROP_MEMBERSHIP
),
663 MAP_OPTION( IP_OPTIONS
),
665 MAP_OPTION( IP_HDRINCL
),
667 MAP_OPTION( IP_TOS
),
668 MAP_OPTION( IP_TTL
),
670 MAP_OPTION( IP_PKTINFO
),
673 MAP_OPTION( IP_UNICAST_IF
),
677 static const int ws_ipv6_map
[][2] =
679 #ifdef IPV6_ADD_MEMBERSHIP
680 MAP_OPTION( IPV6_ADD_MEMBERSHIP
),
682 #ifdef IPV6_DROP_MEMBERSHIP
683 MAP_OPTION( IPV6_DROP_MEMBERSHIP
),
685 MAP_OPTION( IPV6_MULTICAST_IF
),
686 MAP_OPTION( IPV6_MULTICAST_HOPS
),
687 MAP_OPTION( IPV6_MULTICAST_LOOP
),
688 MAP_OPTION( IPV6_UNICAST_HOPS
),
689 MAP_OPTION( IPV6_V6ONLY
),
690 #ifdef IPV6_UNICAST_IF
691 MAP_OPTION( IPV6_UNICAST_IF
),
695 static const int ws_af_map
[][2] =
697 MAP_OPTION( AF_UNSPEC
),
698 MAP_OPTION( AF_INET
),
699 MAP_OPTION( AF_INET6
),
701 MAP_OPTION( AF_IPX
),
704 MAP_OPTION( AF_IRDA
),
706 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
709 static const int ws_socktype_map
[][2] =
711 MAP_OPTION( SOCK_DGRAM
),
712 MAP_OPTION( SOCK_STREAM
),
713 MAP_OPTION( SOCK_RAW
),
714 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
717 static const int ws_proto_map
[][2] =
719 MAP_OPTION( IPPROTO_IP
),
720 MAP_OPTION( IPPROTO_TCP
),
721 MAP_OPTION( IPPROTO_UDP
),
722 MAP_OPTION( IPPROTO_IPV6
),
723 MAP_OPTION( IPPROTO_ICMP
),
724 MAP_OPTION( IPPROTO_IGMP
),
725 MAP_OPTION( IPPROTO_RAW
),
726 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
729 static const int ws_aiflag_map
[][2] =
731 MAP_OPTION( AI_PASSIVE
),
732 MAP_OPTION( AI_CANONNAME
),
733 MAP_OPTION( AI_NUMERICHOST
),
734 #ifdef AI_NUMERICSERV
735 MAP_OPTION( AI_NUMERICSERV
),
738 MAP_OPTION( AI_V4MAPPED
),
740 MAP_OPTION( AI_ADDRCONFIG
),
743 static const int ws_niflag_map
[][2] =
745 MAP_OPTION( NI_NOFQDN
),
746 MAP_OPTION( NI_NUMERICHOST
),
747 MAP_OPTION( NI_NAMEREQD
),
748 MAP_OPTION( NI_NUMERICSERV
),
749 MAP_OPTION( NI_DGRAM
),
752 static const int ws_eai_map
[][2] =
754 MAP_OPTION( EAI_AGAIN
),
755 MAP_OPTION( EAI_BADFLAGS
),
756 MAP_OPTION( EAI_FAIL
),
757 MAP_OPTION( EAI_FAMILY
),
758 MAP_OPTION( EAI_MEMORY
),
759 /* Note: EAI_NODATA is deprecated, but still
760 * used by Windows and Linux... We map the newer
761 * EAI_NONAME to EAI_NODATA for now until Windows
765 MAP_OPTION( EAI_NODATA
),
768 { WS_EAI_NODATA
, EAI_NONAME
},
771 MAP_OPTION( EAI_SERVICE
),
772 MAP_OPTION( EAI_SOCKTYPE
),
776 static const int ws_poll_map
[][2] =
778 MAP_OPTION( POLLERR
),
779 MAP_OPTION( POLLHUP
),
780 MAP_OPTION( POLLNVAL
),
781 MAP_OPTION( POLLWRNORM
),
782 MAP_OPTION( POLLWRBAND
),
783 MAP_OPTION( POLLRDNORM
),
784 { WS_POLLRDBAND
, POLLPRI
}
787 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
789 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
791 static inline WSACMSGHDR
*fill_control_message(int level
, int type
, WSACMSGHDR
*current
, ULONG
*maxsize
, void *data
, int len
)
793 ULONG msgsize
= sizeof(WSACMSGHDR
) + WSA_CMSG_ALIGN(len
);
794 char *ptr
= (char *) current
+ sizeof(WSACMSGHDR
);
796 /* Make sure there is at least enough room for this entry */
797 if (msgsize
> *maxsize
)
800 /* Fill in the entry */
801 current
->cmsg_len
= sizeof(WSACMSGHDR
) + len
;
802 current
->cmsg_level
= level
;
803 current
->cmsg_type
= type
;
804 memcpy(ptr
, data
, len
);
805 /* Return the pointer to where next entry should go */
806 return (WSACMSGHDR
*) (ptr
+ WSA_CMSG_ALIGN(len
));
808 #endif /* IP_PKTINFO */
810 static inline int convert_control_headers(struct msghdr
*hdr
, WSABUF
*control
)
813 WSACMSGHDR
*cmsg_win
= (WSACMSGHDR
*) control
->buf
, *ptr
;
814 ULONG ctlsize
= control
->len
;
815 struct cmsghdr
*cmsg_unix
;
818 /* Loop over all the headers, converting as appropriate */
819 for (cmsg_unix
= CMSG_FIRSTHDR(hdr
); cmsg_unix
!= NULL
; cmsg_unix
= CMSG_NXTHDR(hdr
, cmsg_unix
))
821 switch(cmsg_unix
->cmsg_level
)
824 switch(cmsg_unix
->cmsg_type
)
828 /* Convert the Unix IP_PKTINFO structure to the Windows version */
829 struct in_pktinfo
*data_unix
= (struct in_pktinfo
*) CMSG_DATA(cmsg_unix
);
830 struct WS_in_pktinfo data_win
;
832 memcpy(&data_win
.ipi_addr
,&data_unix
->ipi_addr
.s_addr
,4); /* 4 bytes = 32 address bits */
833 data_win
.ipi_ifindex
= data_unix
->ipi_ifindex
;
834 ptr
= fill_control_message(WS_IPPROTO_IP
, WS_IP_PKTINFO
, ptr
, &ctlsize
,
835 (void*)&data_win
, sizeof(data_win
));
836 if (!ptr
) goto error
;
839 FIXME("Unhandled IPPROTO_IP message header type %d\n", cmsg_unix
->cmsg_type
);
844 FIXME("Unhandled message header level %d\n", cmsg_unix
->cmsg_level
);
850 /* Set the length of the returned control headers */
851 control
->len
= (ptr
== NULL
? 0 : (char*)ptr
- (char*)cmsg_win
);
852 return (ptr
!= NULL
);
853 #else /* IP_PKTINFO */
856 #endif /* IP_PKTINFO */
858 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
860 /* ----------------------------------- error handling */
862 static NTSTATUS
sock_get_ntstatus( int err
)
866 case EBADF
: return STATUS_INVALID_HANDLE
;
867 case EBUSY
: return STATUS_DEVICE_BUSY
;
869 case EACCES
: return STATUS_ACCESS_DENIED
;
870 case EFAULT
: return STATUS_NO_MEMORY
;
871 case EINVAL
: return STATUS_INVALID_PARAMETER
;
873 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
874 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
875 case EINPROGRESS
: return STATUS_PENDING
;
876 case EALREADY
: return STATUS_NETWORK_BUSY
;
877 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
878 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
879 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
880 case EPROTONOSUPPORT
:
881 case ESOCKTNOSUPPORT
:
884 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
885 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
886 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
887 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
888 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
889 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
890 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
891 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
892 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
893 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
894 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
896 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
897 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
899 case 0: return STATUS_SUCCESS
;
901 WARN("Unknown errno %d!\n", err
);
902 return STATUS_UNSUCCESSFUL
;
906 static UINT
sock_get_error( int err
)
910 case EINTR
: return WSAEINTR
;
912 case EACCES
: return WSAEACCES
;
913 case EFAULT
: return WSAEFAULT
;
914 case EINVAL
: return WSAEINVAL
;
915 case EMFILE
: return WSAEMFILE
;
916 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
917 case EINPROGRESS
: return WSAEINPROGRESS
;
918 case EALREADY
: return WSAEALREADY
;
920 case ENOTSOCK
: return WSAENOTSOCK
;
921 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
922 case EMSGSIZE
: return WSAEMSGSIZE
;
923 case EPROTOTYPE
: return WSAEPROTOTYPE
;
924 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
925 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
926 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
927 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
928 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
929 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
930 case EADDRINUSE
: return WSAEADDRINUSE
;
931 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
932 case ENETDOWN
: return WSAENETDOWN
;
933 case ENETUNREACH
: return WSAENETUNREACH
;
934 case ENETRESET
: return WSAENETRESET
;
935 case ECONNABORTED
: return WSAECONNABORTED
;
937 case ECONNRESET
: return WSAECONNRESET
;
938 case ENOBUFS
: return WSAENOBUFS
;
939 case EISCONN
: return WSAEISCONN
;
940 case ENOTCONN
: return WSAENOTCONN
;
941 case ESHUTDOWN
: return WSAESHUTDOWN
;
942 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
943 case ETIMEDOUT
: return WSAETIMEDOUT
;
944 case ECONNREFUSED
: return WSAECONNREFUSED
;
945 case ELOOP
: return WSAELOOP
;
946 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
947 case EHOSTDOWN
: return WSAEHOSTDOWN
;
948 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
949 case ENOTEMPTY
: return WSAENOTEMPTY
;
951 case EPROCLIM
: return WSAEPROCLIM
;
954 case EUSERS
: return WSAEUSERS
;
957 case EDQUOT
: return WSAEDQUOT
;
960 case ESTALE
: return WSAESTALE
;
963 case EREMOTE
: return WSAEREMOTE
;
966 /* just in case we ever get here and there are no problems */
969 WARN("Unknown errno %d!\n", err
);
970 return WSAEOPNOTSUPP
;
974 static UINT
wsaErrno(void)
976 int loc_errno
= errno
;
977 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
979 return sock_get_error( loc_errno
);
982 /* most ws2 overlapped functions return an ntstatus-based error code */
983 static NTSTATUS
wsaErrStatus(void)
985 int loc_errno
= errno
;
986 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
988 return sock_get_ntstatus(loc_errno
);
991 static UINT
wsaHerrno(int loc_errno
)
993 WARN("h_errno %d.\n", loc_errno
);
997 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
998 case TRY_AGAIN
: return WSATRY_AGAIN
;
999 case NO_RECOVERY
: return WSANO_RECOVERY
;
1000 case NO_DATA
: return WSANO_DATA
;
1001 case ENOBUFS
: return WSAENOBUFS
;
1005 WARN("Unknown h_errno %d!\n", loc_errno
);
1006 return WSAEOPNOTSUPP
;
1010 static inline DWORD
NtStatusToWSAError( const DWORD status
)
1012 /* We only need to cover the status codes set by server async request handling */
1016 case STATUS_SUCCESS
: wserr
= 0; break;
1017 case STATUS_PENDING
: wserr
= WSA_IO_PENDING
; break;
1018 case STATUS_OBJECT_TYPE_MISMATCH
: wserr
= WSAENOTSOCK
; break;
1019 case STATUS_INVALID_HANDLE
: wserr
= WSAEBADF
; break;
1020 case STATUS_INVALID_PARAMETER
: wserr
= WSAEINVAL
; break;
1021 case STATUS_PIPE_DISCONNECTED
: wserr
= WSAESHUTDOWN
; break;
1022 case STATUS_NETWORK_BUSY
: wserr
= WSAEALREADY
; break;
1023 case STATUS_NETWORK_UNREACHABLE
: wserr
= WSAENETUNREACH
; break;
1024 case STATUS_CONNECTION_REFUSED
: wserr
= WSAECONNREFUSED
; break;
1025 case STATUS_CONNECTION_DISCONNECTED
: wserr
= WSAENOTCONN
; break;
1026 case STATUS_CONNECTION_RESET
: wserr
= WSAECONNRESET
; break;
1027 case STATUS_CONNECTION_ABORTED
: wserr
= WSAECONNABORTED
; break;
1028 case STATUS_CANCELLED
: wserr
= WSA_OPERATION_ABORTED
; break;
1029 case STATUS_ADDRESS_ALREADY_ASSOCIATED
: wserr
= WSAEADDRINUSE
; break;
1030 case STATUS_IO_TIMEOUT
:
1031 case STATUS_TIMEOUT
: wserr
= WSAETIMEDOUT
; break;
1032 case STATUS_NO_MEMORY
: wserr
= WSAEFAULT
; break;
1033 case STATUS_ACCESS_DENIED
: wserr
= WSAEACCES
; break;
1034 case STATUS_TOO_MANY_OPENED_FILES
: wserr
= WSAEMFILE
; break;
1035 case STATUS_CANT_WAIT
: wserr
= WSAEWOULDBLOCK
; break;
1036 case STATUS_BUFFER_OVERFLOW
: wserr
= WSAEMSGSIZE
; break;
1037 case STATUS_NOT_SUPPORTED
: wserr
= WSAEOPNOTSUPP
; break;
1038 case STATUS_HOST_UNREACHABLE
: wserr
= WSAEHOSTUNREACH
; break;
1041 wserr
= RtlNtStatusToDosError( status
);
1042 FIXME( "Status code %08x converted to DOS error code %x\n", status
, wserr
);
1047 /* set last error code from NT status without mapping WSA errors */
1048 static inline unsigned int set_error( unsigned int err
)
1052 err
= NtStatusToWSAError( err
);
1053 SetLastError( err
);
1058 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
1061 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
1066 static inline void release_sock_fd( SOCKET s
, int fd
)
1068 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
1071 static void _enable_event( HANDLE s
, unsigned int event
,
1072 unsigned int sstate
, unsigned int cstate
)
1074 SERVER_START_REQ( enable_socket_event
)
1076 req
->handle
= wine_server_obj_handle( s
);
1078 req
->sstate
= sstate
;
1079 req
->cstate
= cstate
;
1080 wine_server_call( req
);
1085 static NTSTATUS
_is_blocking(SOCKET s
, BOOL
*ret
)
1088 SERVER_START_REQ( get_socket_event
)
1090 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1091 req
->service
= FALSE
;
1093 status
= wine_server_call( req
);
1094 *ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
1100 static unsigned int _get_sock_mask(SOCKET s
)
1103 SERVER_START_REQ( get_socket_event
)
1105 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1106 req
->service
= FALSE
;
1108 wine_server_call( req
);
1115 static void _sync_sock_state(SOCKET s
)
1118 /* do a dummy wineserver request in order to let
1119 the wineserver run through its select loop once */
1120 (void)_is_blocking(s
, &dummy
);
1123 static void _get_sock_errors(SOCKET s
, int *events
)
1125 SERVER_START_REQ( get_socket_event
)
1127 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1128 req
->service
= FALSE
;
1130 wine_server_set_reply( req
, events
, sizeof(int) * FD_MAX_EVENTS
);
1131 wine_server_call( req
);
1136 static int _get_sock_error(SOCKET s
, unsigned int bit
)
1138 int events
[FD_MAX_EVENTS
];
1139 _get_sock_errors(s
, events
);
1143 static int _get_fd_type(int fd
)
1146 socklen_t optlen
= sizeof(sock_type
);
1147 getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char*) &sock_type
, &optlen
);
1151 static struct per_thread_data
*get_per_thread_data(void)
1153 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
1154 /* lazy initialization */
1157 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
1158 NtCurrentTeb()->WinSockData
= ptb
;
1163 static void free_per_thread_data(void)
1165 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
1169 /* delete scratch buffers */
1170 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1171 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1172 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1173 ptb
->he_buffer
= NULL
;
1174 ptb
->se_buffer
= NULL
;
1175 ptb
->pe_buffer
= NULL
;
1177 HeapFree( GetProcessHeap(), 0, ptb
);
1178 NtCurrentTeb()->WinSockData
= NULL
;
1181 /***********************************************************************
1182 * DllMain (WS2_32.init)
1184 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
1186 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
1187 switch (fdwReason
) {
1188 case DLL_PROCESS_ATTACH
:
1190 case DLL_PROCESS_DETACH
:
1191 if (fImpLoad
) break;
1192 free_per_thread_data();
1193 DeleteCriticalSection(&csWSgetXXXbyYYY
);
1195 case DLL_THREAD_DETACH
:
1196 free_per_thread_data();
1202 /***********************************************************************
1205 * Converts send/recv flags from Windows format.
1206 * Return the converted flag bits, unsupported flags remain unchanged.
1208 static int convert_flags(int flags
)
1211 if (!flags
) return 0;
1213 for (out
= i
= 0; flags
&& i
< sizeof(ws_flags_map
) / sizeof(ws_flags_map
[0]); i
++)
1215 if (ws_flags_map
[i
][0] & flags
)
1217 out
|= ws_flags_map
[i
][1];
1218 flags
&= ~ws_flags_map
[i
][0];
1223 FIXME("Unknown send/recv flags 0x%x, using anyway...\n", flags
);
1229 /***********************************************************************
1232 * Converts socket flags from Windows format.
1233 * Return 1 if converted, 0 if not (error).
1235 static int convert_sockopt(INT
*level
, INT
*optname
)
1241 *level
= SOL_SOCKET
;
1242 for(i
=0; i
<sizeof(ws_sock_map
)/sizeof(ws_sock_map
[0]); i
++) {
1243 if( ws_sock_map
[i
][0] == *optname
)
1245 *optname
= ws_sock_map
[i
][1];
1249 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
1251 case WS_IPPROTO_TCP
:
1252 *level
= IPPROTO_TCP
;
1253 for(i
=0; i
<sizeof(ws_tcp_map
)/sizeof(ws_tcp_map
[0]); i
++) {
1254 if ( ws_tcp_map
[i
][0] == *optname
)
1256 *optname
= ws_tcp_map
[i
][1];
1260 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
1263 *level
= IPPROTO_IP
;
1264 for(i
=0; i
<sizeof(ws_ip_map
)/sizeof(ws_ip_map
[0]); i
++) {
1265 if (ws_ip_map
[i
][0] == *optname
)
1267 *optname
= ws_ip_map
[i
][1];
1271 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
1273 case WS_IPPROTO_IPV6
:
1274 *level
= IPPROTO_IPV6
;
1275 for(i
=0; i
<sizeof(ws_ipv6_map
)/sizeof(ws_ipv6_map
[0]); i
++) {
1276 if (ws_ipv6_map
[i
][0] == *optname
)
1278 *optname
= ws_ipv6_map
[i
][1];
1282 FIXME("Unknown IPPROTO_IPV6 optname 0x%x\n", *optname
);
1284 default: FIXME("Unimplemented or unknown socket level\n");
1289 /* ----------------------------------- Per-thread info (or per-process?) */
1291 static char *strdup_lower(const char *str
)
1294 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
1298 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
1301 else SetLastError(WSAENOBUFS
);
1305 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
1306 * from an fd and return the value converted to milli seconds
1307 * or 0 if there is an infinite time out */
1308 static inline INT64
get_rcvsnd_timeo( int fd
, BOOL is_recv
)
1311 socklen_t len
= sizeof(tv
);
1316 optname
= SO_RCVTIMEO
;
1322 optname
= SO_SNDTIMEO
;
1327 res
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
1330 return (UINT64
)tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
1333 /* utility: given an fd, will block until one of the events occurs */
1334 static inline int do_block( int fd
, int events
, int timeout
)
1340 pfd
.events
= events
;
1342 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
1353 convert_af_w2u(int windowsaf
) {
1356 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
1357 if (ws_af_map
[i
][0] == windowsaf
)
1358 return ws_af_map
[i
][1];
1359 FIXME("unhandled Windows address family %d\n", windowsaf
);
1364 convert_af_u2w(int unixaf
) {
1367 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
1368 if (ws_af_map
[i
][1] == unixaf
)
1369 return ws_af_map
[i
][0];
1370 FIXME("unhandled UNIX address family %d\n", unixaf
);
1375 convert_proto_w2u(int windowsproto
) {
1378 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
1379 if (ws_proto_map
[i
][0] == windowsproto
)
1380 return ws_proto_map
[i
][1];
1382 /* check for extended IPX */
1383 if (IS_IPX_PROTO(windowsproto
))
1384 return windowsproto
;
1386 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
1391 convert_proto_u2w(int unixproto
) {
1394 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
1395 if (ws_proto_map
[i
][1] == unixproto
)
1396 return ws_proto_map
[i
][0];
1398 /* if value is inside IPX range just return it - the kernel simply
1399 * echoes the value used in the socket() function */
1400 if (IS_IPX_PROTO(unixproto
))
1403 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
1408 convert_socktype_w2u(int windowssocktype
) {
1411 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
1412 if (ws_socktype_map
[i
][0] == windowssocktype
)
1413 return ws_socktype_map
[i
][1];
1414 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
1419 convert_socktype_u2w(int unixsocktype
) {
1422 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
1423 if (ws_socktype_map
[i
][1] == unixsocktype
)
1424 return ws_socktype_map
[i
][0];
1425 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
1429 static int convert_poll_w2u(int events
)
1432 for (i
= ret
= 0; events
&& i
< sizeof(ws_poll_map
) / sizeof(ws_poll_map
[0]); i
++)
1434 if (ws_poll_map
[i
][0] & events
)
1436 ret
|= ws_poll_map
[i
][1];
1437 events
&= ~ws_poll_map
[i
][0];
1442 FIXME("Unsupported WSAPoll() flags 0x%x\n", events
);
1446 static int convert_poll_u2w(int events
)
1449 for (i
= ret
= 0; events
&& i
< sizeof(ws_poll_map
) / sizeof(ws_poll_map
[0]); i
++)
1451 if (ws_poll_map
[i
][1] & events
)
1453 ret
|= ws_poll_map
[i
][0];
1454 events
&= ~ws_poll_map
[i
][1];
1459 FIXME("Unsupported poll() flags 0x%x\n", events
);
1463 static int set_ipx_packettype(int sock
, int ptype
)
1466 int fd
= get_sock_fd( sock
, 0, NULL
), ret
= 0;
1467 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", ptype
, fd
);
1469 if (fd
== -1) return SOCKET_ERROR
;
1471 /* We try to set the ipx type on ipx socket level. */
1473 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, &ptype
, sizeof(ptype
)) == -1)
1475 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
1481 /* Should we retrieve val using a getsockopt call and then
1482 * set the modified one? */
1484 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
1487 release_sock_fd( sock
, fd
);
1490 WARN("IPX support is not enabled, can't set packet type\n");
1491 return SOCKET_ERROR
;
1495 /* ----------------------------------- API -----
1497 * Init / cleanup / error checking.
1500 /***********************************************************************
1501 * WSAStartup (WS2_32.115)
1503 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
1505 TRACE("verReq=%x\n", wVersionRequested
);
1507 if (LOBYTE(wVersionRequested
) < 1)
1508 return WSAVERNOTSUPPORTED
;
1510 if (!lpWSAData
) return WSAEINVAL
;
1514 /* that's the whole of the negotiation for now */
1515 lpWSAData
->wVersion
= wVersionRequested
;
1516 /* return winsock information */
1517 lpWSAData
->wHighVersion
= 0x0202;
1518 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
1519 strcpy(lpWSAData
->szSystemStatus
, "Running" );
1520 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
1521 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
1522 /* don't do anything with lpWSAData->lpVendorInfo */
1523 /* (some apps don't allocate the space for this field) */
1525 TRACE("succeeded starts: %d\n", num_startup
);
1530 /***********************************************************************
1531 * WSACleanup (WS2_32.116)
1533 INT WINAPI
WSACleanup(void)
1537 TRACE("pending cleanups: %d\n", num_startup
);
1540 SetLastError(WSANOTINITIALISED
);
1541 return SOCKET_ERROR
;
1545 /***********************************************************************
1546 * WSAGetLastError (WS2_32.111)
1548 INT WINAPI
WSAGetLastError(void)
1550 return GetLastError();
1553 /***********************************************************************
1554 * WSASetLastError (WS2_32.112)
1556 void WINAPI
WSASetLastError(INT iError
) {
1557 SetLastError(iError
);
1560 static struct WS_hostent
*check_buffer_he(int size
)
1562 struct per_thread_data
* ptb
= get_per_thread_data();
1565 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
1566 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1568 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
1569 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
1570 return ptb
->he_buffer
;
1573 static struct WS_servent
*check_buffer_se(int size
)
1575 struct per_thread_data
* ptb
= get_per_thread_data();
1578 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
1579 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1581 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
1582 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
1583 return ptb
->se_buffer
;
1586 static struct WS_protoent
*check_buffer_pe(int size
)
1588 struct per_thread_data
* ptb
= get_per_thread_data();
1591 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
1592 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1594 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
1595 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
1596 return ptb
->pe_buffer
;
1599 /* ----------------------------------- i/o APIs */
1601 static inline BOOL
supported_pf(int pf
)
1621 static inline BOOL
supported_protocol(int protocol
)
1624 for (i
= 0; i
< sizeof(valid_protocols
) / sizeof(valid_protocols
[0]); i
++)
1625 if (protocol
== valid_protocols
[i
])
1630 /**********************************************************************/
1632 /* Returns the length of the converted address if successful, 0 if it was too small to
1635 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
1636 union generic_unix_sockaddr
*uaddr
)
1638 unsigned int uaddrlen
= 0;
1640 switch (wsaddr
->sa_family
)
1645 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
1646 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
1648 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
1651 uaddrlen
= sizeof(struct sockaddr_ipx
);
1652 memset( uaddr
, 0, uaddrlen
);
1653 uipx
->sipx_family
=AF_IPX
;
1654 uipx
->sipx_port
=wsipx
->sa_socket
;
1655 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
1658 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
1659 #ifdef IPX_FRAME_NONE
1660 uipx
->sipx_type
=IPX_FRAME_NONE
;
1666 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
1667 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
1669 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
1670 * scope_id, one without.
1672 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
1673 uaddrlen
= sizeof(struct sockaddr_in6
);
1674 memset( uaddr
, 0, uaddrlen
);
1675 uin6
->sin6_family
= AF_INET6
;
1676 uin6
->sin6_port
= win6
->sin6_port
;
1677 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
1678 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1679 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
1681 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1684 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
1688 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
1689 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
1691 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
1693 uaddrlen
= sizeof(struct sockaddr_in
);
1694 memset( uaddr
, 0, uaddrlen
);
1695 uin
->sin_family
= AF_INET
;
1696 uin
->sin_port
= win
->sin_port
;
1697 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
1702 struct sockaddr_irda
*uin
= (struct sockaddr_irda
*)uaddr
;
1703 const SOCKADDR_IRDA
*win
= (const SOCKADDR_IRDA
*)wsaddr
;
1705 if (wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1707 uaddrlen
= sizeof(struct sockaddr_irda
);
1708 memset( uaddr
, 0, uaddrlen
);
1709 uin
->sir_family
= AF_IRDA
;
1710 if (!strncmp( win
->irdaServiceName
, "LSAP-SEL", strlen( "LSAP-SEL" ) ))
1712 unsigned int lsap_sel
= 0;
1714 sscanf( win
->irdaServiceName
, "LSAP-SEL%u", &lsap_sel
);
1715 uin
->sir_lsap_sel
= lsap_sel
;
1719 uin
->sir_lsap_sel
= LSAP_ANY
;
1720 memcpy( uin
->sir_name
, win
->irdaServiceName
, 25 );
1722 memcpy( &uin
->sir_addr
, win
->irdaDeviceID
, sizeof(uin
->sir_addr
) );
1726 case WS_AF_UNSPEC
: {
1727 /* Try to determine the needed space by the passed windows sockaddr space */
1728 switch (wsaddrlen
) {
1729 default: /* likely an ipv4 address */
1730 case sizeof(struct WS_sockaddr_in
):
1731 uaddrlen
= sizeof(struct sockaddr_in
);
1734 case sizeof(struct WS_sockaddr_ipx
):
1735 uaddrlen
= sizeof(struct sockaddr_ipx
);
1739 case sizeof(SOCKADDR_IRDA
):
1740 uaddrlen
= sizeof(struct sockaddr_irda
);
1743 case sizeof(struct WS_sockaddr_in6
):
1744 case sizeof(struct WS_sockaddr_in6_old
):
1745 uaddrlen
= sizeof(struct sockaddr_in6
);
1748 memset( uaddr
, 0, uaddrlen
);
1752 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
1758 static BOOL
is_sockaddr_bound(const struct sockaddr
*uaddr
, int uaddrlen
)
1760 switch (uaddr
->sa_family
)
1765 static const struct sockaddr_ipx emptyAddr
;
1766 struct sockaddr_ipx
*ipx
= (struct sockaddr_ipx
*) uaddr
;
1767 return ipx
->sipx_port
1768 || memcmp(&ipx
->sipx_network
, &emptyAddr
.sipx_network
, sizeof(emptyAddr
.sipx_network
))
1769 || memcmp(&ipx
->sipx_node
, &emptyAddr
.sipx_node
, sizeof(emptyAddr
.sipx_node
));
1774 static const struct sockaddr_in6 emptyAddr
;
1775 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) uaddr
;
1776 return in6
->sin6_port
|| memcmp(&in6
->sin6_addr
, &emptyAddr
.sin6_addr
, sizeof(struct in6_addr
));
1780 static const struct sockaddr_in emptyAddr
;
1781 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) uaddr
;
1782 return in
->sin_port
|| memcmp(&in
->sin_addr
, &emptyAddr
.sin_addr
, sizeof(struct in_addr
));
1787 FIXME("unknown address family %d\n", uaddr
->sa_family
);
1792 /* Returns -1 if getsockname fails, 0 if not bound, 1 otherwise */
1793 static int is_fd_bound(int fd
, union generic_unix_sockaddr
*uaddr
, socklen_t
*uaddrlen
)
1795 union generic_unix_sockaddr inaddr
;
1799 if (!uaddr
) uaddr
= &inaddr
;
1800 if (!uaddrlen
) uaddrlen
= &inlen
;
1802 *uaddrlen
= sizeof(inaddr
);
1803 res
= getsockname(fd
, &uaddr
->addr
, uaddrlen
);
1804 if (!res
) res
= is_sockaddr_bound(&uaddr
->addr
, *uaddrlen
);
1808 /* Returns 0 if successful, -1 if the buffer is too small */
1809 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
1813 switch(uaddr
->sa_family
)
1818 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
1819 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
1822 switch (*wsaddrlen
) /* how much can we copy? */
1826 *wsaddrlen
= sizeof(*wsipx
);
1827 wsipx
->sa_socket
=uipx
->sipx_port
;
1831 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
1839 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
1845 wsipx
->sa_family
=WS_AF_IPX
;
1857 const struct sockaddr_irda
*uin
= (const struct sockaddr_irda
*)uaddr
;
1858 SOCKADDR_IRDA
*win
= (SOCKADDR_IRDA
*)wsaddr
;
1860 if (*wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1862 win
->irdaAddressFamily
= WS_AF_IRDA
;
1863 memcpy( win
->irdaDeviceID
, &uin
->sir_addr
, sizeof(win
->irdaDeviceID
) );
1864 if (uin
->sir_lsap_sel
!= LSAP_ANY
)
1865 sprintf( win
->irdaServiceName
, "LSAP-SEL%u", uin
->sir_lsap_sel
);
1867 memcpy( win
->irdaServiceName
, uin
->sir_name
,
1868 sizeof(win
->irdaServiceName
) );
1873 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
1874 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
1876 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
1878 win6old
->sin6_family
= WS_AF_INET6
;
1879 win6old
->sin6_port
= uin6
->sin6_port
;
1880 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
1881 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1882 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1883 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
1884 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
1885 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
1886 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
1889 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1891 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1896 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
1897 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
1899 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
1901 win
->sin_family
= WS_AF_INET
;
1902 win
->sin_port
= uin
->sin_port
;
1903 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
1904 memset(win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
1905 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
1909 memset(wsaddr
,0,*wsaddrlen
);
1913 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
1919 static INT
WS_DuplicateSocket(BOOL unicode
, SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
1923 WSAPROTOCOL_INFOW infow
;
1925 TRACE("(unicode %d, socket %04lx, processid %x, buffer %p)\n",
1926 unicode
, s
, dwProcessId
, lpProtocolInfo
);
1928 if (!ws_protocol_info(s
, unicode
, &infow
, &size
))
1929 return SOCKET_ERROR
;
1931 if (!(hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
)))
1933 SetLastError(WSAEINVAL
);
1934 return SOCKET_ERROR
;
1937 if (!lpProtocolInfo
)
1939 CloseHandle(hProcess
);
1940 SetLastError(WSAEFAULT
);
1941 return SOCKET_ERROR
;
1944 /* I don't know what the real Windoze does next, this is a hack */
1945 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
1946 * the target use the global duplicate, or we could copy a reference to us to the structure
1947 * and let the target duplicate it from us, but let's do it as simple as possible */
1948 memcpy(lpProtocolInfo
, &infow
, size
);
1949 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
1950 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwServiceFlags3
,
1951 0, FALSE
, DUPLICATE_SAME_ACCESS
);
1952 CloseHandle(hProcess
);
1953 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
1957 /*****************************************************************************
1958 * WS_EnterSingleProtocolW [internal]
1960 * enters the protocol information of one given protocol into the given
1964 * TRUE if a protocol was entered into the buffer.
1967 * - only implemented for IPX, SPX, SPXII, TCP, UDP
1968 * - there is no check that the operating system supports the returned
1971 static BOOL
WS_EnterSingleProtocolW( INT protocol
, WSAPROTOCOL_INFOW
* info
)
1973 memset( info
, 0, sizeof(WSAPROTOCOL_INFOW
) );
1974 info
->iProtocol
= protocol
;
1978 case WS_IPPROTO_TCP
:
1979 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_EXPEDITED_DATA
|
1980 XP1_GRACEFUL_CLOSE
| XP1_GUARANTEED_ORDER
|
1981 XP1_GUARANTEED_DELIVERY
;
1982 info
->ProviderId
= ProviderIdIP
;
1983 info
->dwCatalogEntryId
= 0x3e9;
1984 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
1985 info
->ProtocolChain
.ChainLen
= 1;
1987 info
->iAddressFamily
= WS_AF_INET
;
1988 info
->iMaxSockAddr
= 0x10;
1989 info
->iMinSockAddr
= 0x10;
1990 info
->iSocketType
= WS_SOCK_STREAM
;
1991 strcpyW( info
->szProtocol
, NameTcpW
);
1994 case WS_IPPROTO_UDP
:
1995 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_SUPPORT_BROADCAST
|
1996 XP1_SUPPORT_MULTIPOINT
| XP1_MESSAGE_ORIENTED
|
1998 info
->ProviderId
= ProviderIdIP
;
1999 info
->dwCatalogEntryId
= 0x3ea;
2000 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2001 info
->ProtocolChain
.ChainLen
= 1;
2003 info
->iAddressFamily
= WS_AF_INET
;
2004 info
->iMaxSockAddr
= 0x10;
2005 info
->iMinSockAddr
= 0x10;
2006 info
->iSocketType
= WS_SOCK_DGRAM
;
2007 info
->dwMessageSize
= 0xffbb;
2008 strcpyW( info
->szProtocol
, NameUdpW
);
2011 case WS_NSPROTO_IPX
:
2012 info
->dwServiceFlags1
= XP1_PARTIAL_MESSAGE
| XP1_SUPPORT_BROADCAST
|
2013 XP1_SUPPORT_MULTIPOINT
| XP1_MESSAGE_ORIENTED
|
2015 info
->ProviderId
= ProviderIdIPX
;
2016 info
->dwCatalogEntryId
= 0x406;
2017 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2018 info
->ProtocolChain
.ChainLen
= 1;
2020 info
->iAddressFamily
= WS_AF_IPX
;
2021 info
->iMaxSockAddr
= 0x10;
2022 info
->iMinSockAddr
= 0x0e;
2023 info
->iSocketType
= WS_SOCK_DGRAM
;
2024 info
->iProtocolMaxOffset
= 0xff;
2025 info
->dwMessageSize
= 0x240;
2026 strcpyW( info
->szProtocol
, NameIpxW
);
2029 case WS_NSPROTO_SPX
:
2030 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_PSEUDO_STREAM
|
2031 XP1_MESSAGE_ORIENTED
| XP1_GUARANTEED_ORDER
|
2032 XP1_GUARANTEED_DELIVERY
;
2033 info
->ProviderId
= ProviderIdSPX
;
2034 info
->dwCatalogEntryId
= 0x407;
2035 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2036 info
->ProtocolChain
.ChainLen
= 1;
2038 info
->iAddressFamily
= WS_AF_IPX
;
2039 info
->iMaxSockAddr
= 0x10;
2040 info
->iMinSockAddr
= 0x0e;
2041 info
->iSocketType
= WS_SOCK_SEQPACKET
;
2042 info
->dwMessageSize
= 0xffffffff;
2043 strcpyW( info
->szProtocol
, NameSpxW
);
2046 case WS_NSPROTO_SPXII
:
2047 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_GRACEFUL_CLOSE
|
2048 XP1_PSEUDO_STREAM
| XP1_MESSAGE_ORIENTED
|
2049 XP1_GUARANTEED_ORDER
| XP1_GUARANTEED_DELIVERY
;
2050 info
->ProviderId
= ProviderIdSPX
;
2051 info
->dwCatalogEntryId
= 0x409;
2052 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2053 info
->ProtocolChain
.ChainLen
= 1;
2055 info
->iAddressFamily
= WS_AF_IPX
;
2056 info
->iMaxSockAddr
= 0x10;
2057 info
->iMinSockAddr
= 0x0e;
2058 info
->iSocketType
= WS_SOCK_SEQPACKET
;
2059 info
->dwMessageSize
= 0xffffffff;
2060 strcpyW( info
->szProtocol
, NameSpxIIW
);
2064 FIXME("unknown Protocol <0x%08x>\n", protocol
);
2070 /*****************************************************************************
2071 * WS_EnterSingleProtocolA [internal]
2073 * see function WS_EnterSingleProtocolW
2076 static BOOL
WS_EnterSingleProtocolA( INT protocol
, WSAPROTOCOL_INFOA
* info
)
2078 WSAPROTOCOL_INFOW infow
;
2080 memset( info
, 0, sizeof(WSAPROTOCOL_INFOA
) );
2082 ret
= WS_EnterSingleProtocolW( protocol
, &infow
);
2085 /* convert the structure from W to A */
2086 memcpy( info
, &infow
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
2087 WideCharToMultiByte( CP_ACP
, 0, infow
.szProtocol
, -1,
2088 info
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
);
2094 static INT
WS_EnumProtocols( BOOL unicode
, const INT
*protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
)
2096 INT i
= 0, items
= 0;
2100 LPWSAPROTOCOL_INFOA a
;
2101 LPWSAPROTOCOL_INFOW w
;
2105 if (!protocols
) protocols
= valid_protocols
;
2107 while (protocols
[i
])
2109 if(supported_protocol(protocols
[i
++]))
2113 size
= items
* (unicode
? sizeof(WSAPROTOCOL_INFOW
) : sizeof(WSAPROTOCOL_INFOA
));
2115 TRACE("unicode %d, protocols %p, buffer %p, length %p %d, items %d, required %d\n",
2116 unicode
, protocols
, buffer
, len
, len
? *len
: 0, items
, size
);
2118 if (*len
< size
|| !buffer
)
2121 SetLastError(WSAENOBUFS
);
2122 return SOCKET_ERROR
;
2125 for (i
= items
= 0; protocols
[i
]; i
++)
2127 if (!supported_protocol(protocols
[i
])) continue;
2130 if (WS_EnterSingleProtocolW( protocols
[i
], &info
.w
[items
] ))
2135 if (WS_EnterSingleProtocolA( protocols
[i
], &info
.a
[items
] ))
2142 static BOOL
ws_protocol_info(SOCKET s
, int unicode
, WSAPROTOCOL_INFOW
*buffer
, int *size
)
2146 *size
= unicode
? sizeof(WSAPROTOCOL_INFOW
) : sizeof(WSAPROTOCOL_INFOA
);
2147 memset(buffer
, 0, *size
);
2149 SERVER_START_REQ( get_socket_info
)
2151 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
2152 status
= wine_server_call( req
);
2155 buffer
->iAddressFamily
= convert_af_u2w(reply
->family
);
2156 buffer
->iSocketType
= convert_socktype_u2w(reply
->type
);
2157 buffer
->iProtocol
= convert_proto_u2w(reply
->protocol
);
2164 unsigned int err
= NtStatusToWSAError( status
);
2165 SetLastError( err
== WSAEBADF
? WSAENOTSOCK
: err
);
2170 WS_EnterSingleProtocolW( buffer
->iProtocol
, buffer
);
2172 WS_EnterSingleProtocolA( buffer
->iProtocol
, (WSAPROTOCOL_INFOA
*)buffer
);
2177 /**************************************************************************
2178 * Functions for handling overlapped I/O
2179 **************************************************************************/
2181 /* user APC called upon async completion */
2182 static void WINAPI
ws2_async_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
2184 struct ws2_async
*wsa
= arg
;
2186 if (wsa
->completion_func
) wsa
->completion_func( NtStatusToWSAError(iosb
->u
.Status
),
2187 iosb
->Information
, wsa
->user_overlapped
,
2189 release_async_io( &wsa
->io
);
2192 /***********************************************************************
2193 * WS2_recv (INTERNAL)
2195 * Workhorse for both synchronous and asynchronous recv() operations.
2197 static int WS2_recv( int fd
, struct ws2_async
*wsa
, int flags
)
2199 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2203 union generic_unix_sockaddr unix_sockaddr
;
2206 hdr
.msg_name
= NULL
;
2210 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
2211 hdr
.msg_name
= &unix_sockaddr
;
2214 hdr
.msg_namelen
= 0;
2216 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
2217 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
2218 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2219 hdr
.msg_accrights
= NULL
;
2220 hdr
.msg_accrightslen
= 0;
2222 hdr
.msg_control
= pktbuf
;
2223 hdr
.msg_controllen
= sizeof(pktbuf
);
2227 while ((n
= recvmsg(fd
, &hdr
, flags
)) == -1)
2233 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2236 ERR("Message control headers cannot be properly supported on this system.\n");
2237 wsa
->control
->len
= 0;
2240 if (wsa
->control
&& !convert_control_headers(&hdr
, wsa
->control
))
2242 WARN("Application passed insufficient room for control headers.\n");
2243 *wsa
->lpFlags
|= WS_MSG_CTRUNC
;
2249 /* if this socket is connected and lpFrom is not NULL, Linux doesn't give us
2250 * msg_name and msg_namelen from recvmsg, but it does set msg_namelen to zero.
2252 * quoting linux 2.6 net/ipv4/tcp.c:
2253 * "According to UNIX98, msg_name/msg_namelen are ignored
2254 * on connected socket. I was just happy when found this 8) --ANK"
2256 * likewise MSDN says that lpFrom and lpFromlen are ignored for
2257 * connection-oriented sockets, so don't try to update lpFrom.
2259 if (wsa
->addr
&& hdr
.msg_namelen
)
2260 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, wsa
->addr
, wsa
->addrlen
.ptr
);
2265 /***********************************************************************
2266 * WS2_async_recv (INTERNAL)
2268 * Handler for overlapped recv() operations.
2270 static NTSTATUS
WS2_async_recv( void *user
, IO_STATUS_BLOCK
*iosb
,
2271 NTSTATUS status
, void **apc
, void **arg
)
2273 struct ws2_async
*wsa
= user
;
2278 case STATUS_ALERTED
:
2279 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
2282 result
= WS2_recv( fd
, wsa
, convert_flags(wsa
->flags
) );
2283 wine_server_release_fd( wsa
->hSocket
, fd
);
2286 status
= STATUS_SUCCESS
;
2287 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
2291 if (errno
== EAGAIN
)
2293 status
= STATUS_PENDING
;
2294 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
2299 status
= wsaErrStatus();
2304 if (status
!= STATUS_PENDING
)
2306 iosb
->u
.Status
= status
;
2307 iosb
->Information
= result
;
2308 if (wsa
->completion_func
)
2310 *apc
= ws2_async_apc
;
2314 release_async_io( &wsa
->io
);
2319 /***********************************************************************
2320 * WS2_async_accept_recv (INTERNAL)
2322 * This function is used to finish the read part of an accept request. It is
2323 * needed to place the completion on the correct socket (listener).
2325 static NTSTATUS
WS2_async_accept_recv( void *user
, IO_STATUS_BLOCK
*iosb
,
2326 NTSTATUS status
, void **apc
, void **arg
)
2329 struct ws2_accept_async
*wsa
= user
;
2331 status
= WS2_async_recv( wsa
->read
, iosb
, status
, &junk
, &junk
);
2332 if (status
== STATUS_PENDING
)
2336 WS_AddCompletion( HANDLE2SOCKET(wsa
->listen_socket
), wsa
->cvalue
, iosb
->u
.Status
, iosb
->Information
);
2338 release_async_io( &wsa
->io
);
2342 /***********************************************************************
2343 * WS2_async_accept (INTERNAL)
2345 * This is the function called to satisfy the AcceptEx callback
2347 static NTSTATUS
WS2_async_accept( void *user
, IO_STATUS_BLOCK
*iosb
,
2348 NTSTATUS status
, void **apc
, void **arg
)
2350 struct ws2_accept_async
*wsa
= user
;
2354 TRACE("status: 0x%x listen: %p, accept: %p\n", status
, wsa
->listen_socket
, wsa
->accept_socket
);
2356 if (status
== STATUS_ALERTED
)
2358 SERVER_START_REQ( accept_into_socket
)
2360 req
->lhandle
= wine_server_obj_handle( wsa
->listen_socket
);
2361 req
->ahandle
= wine_server_obj_handle( wsa
->accept_socket
);
2362 status
= wine_server_call( req
);
2366 if (status
== STATUS_CANT_WAIT
)
2367 return STATUS_PENDING
;
2369 if (status
== STATUS_INVALID_HANDLE
)
2371 FIXME("AcceptEx accepting socket closed but request was not cancelled\n");
2372 status
= STATUS_CANCELLED
;
2375 else if (status
== STATUS_HANDLES_CLOSED
)
2376 status
= STATUS_CANCELLED
; /* strange windows behavior */
2378 if (status
!= STATUS_SUCCESS
)
2381 /* WS2 Spec says size param is extra 16 bytes long...what do we put in it? */
2382 addr
= ((char *)wsa
->buf
) + wsa
->data_len
;
2383 len
= wsa
->local_len
- sizeof(int);
2384 WS_getsockname(HANDLE2SOCKET(wsa
->accept_socket
),
2385 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
2388 addr
+= wsa
->local_len
;
2389 len
= wsa
->remote_len
- sizeof(int);
2390 WS_getpeername(HANDLE2SOCKET(wsa
->accept_socket
),
2391 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
2397 SERVER_START_REQ( register_async
)
2399 req
->type
= ASYNC_TYPE_READ
;
2400 req
->async
.handle
= wine_server_obj_handle( wsa
->accept_socket
);
2401 req
->async
.event
= wine_server_obj_handle( wsa
->user_overlapped
->hEvent
);
2402 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept_recv
);
2403 req
->async
.iosb
= wine_server_client_ptr( iosb
);
2404 req
->async
.arg
= wine_server_client_ptr( wsa
);
2405 status
= wine_server_call( req
);
2409 if (status
!= STATUS_PENDING
)
2412 /* The APC has finished but no completion should be sent for the operation yet, additional processing
2413 * needs to be performed by WS2_async_accept_recv() first. */
2414 return STATUS_MORE_PROCESSING_REQUIRED
;
2417 iosb
->u
.Status
= status
;
2418 iosb
->Information
= 0;
2420 if (wsa
->read
) release_async_io( &wsa
->read
->io
);
2421 release_async_io( &wsa
->io
);
2425 /***********************************************************************
2426 * WS2_send (INTERNAL)
2428 * Workhorse for both synchronous and asynchronous send() operations.
2430 static int WS2_send( int fd
, struct ws2_async
*wsa
, int flags
)
2433 union generic_unix_sockaddr unix_addr
;
2436 hdr
.msg_name
= NULL
;
2437 hdr
.msg_namelen
= 0;
2441 hdr
.msg_name
= &unix_addr
;
2442 hdr
.msg_namelen
= ws_sockaddr_ws2u( wsa
->addr
, wsa
->addrlen
.val
, &unix_addr
);
2443 if ( !hdr
.msg_namelen
)
2449 #if defined(HAS_IPX) && defined(SOL_IPX)
2450 if(wsa
->addr
->sa_family
== WS_AF_IPX
)
2452 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
2454 socklen_t len
= sizeof(int);
2456 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
2457 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
2458 * the packet type and then we can retrieve it using getsockopt. After that we can set the
2459 * ipx type in the sockaddr_opx structure with the stored value.
2461 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
2462 uipx
->sipx_type
= val
;
2467 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
2468 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
2469 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2470 hdr
.msg_accrights
= NULL
;
2471 hdr
.msg_accrightslen
= 0;
2473 hdr
.msg_control
= NULL
;
2474 hdr
.msg_controllen
= 0;
2478 while ((ret
= sendmsg(fd
, &hdr
, flags
)) == -1)
2485 while (wsa
->first_iovec
< wsa
->n_iovecs
&& wsa
->iovec
[wsa
->first_iovec
].iov_len
<= n
)
2486 n
-= wsa
->iovec
[wsa
->first_iovec
++].iov_len
;
2487 if (wsa
->first_iovec
< wsa
->n_iovecs
)
2489 wsa
->iovec
[wsa
->first_iovec
].iov_base
= (char*)wsa
->iovec
[wsa
->first_iovec
].iov_base
+ n
;
2490 wsa
->iovec
[wsa
->first_iovec
].iov_len
-= n
;
2495 /***********************************************************************
2496 * WS2_async_send (INTERNAL)
2498 * Handler for overlapped send() operations.
2500 static NTSTATUS
WS2_async_send( void *user
, IO_STATUS_BLOCK
*iosb
,
2501 NTSTATUS status
, void **apc
, void **arg
)
2503 struct ws2_async
*wsa
= user
;
2508 case STATUS_ALERTED
:
2509 if ( wsa
->n_iovecs
<= wsa
->first_iovec
)
2512 status
= STATUS_SUCCESS
;
2515 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
2518 /* check to see if the data is ready (non-blocking) */
2519 result
= WS2_send( fd
, wsa
, convert_flags(wsa
->flags
) );
2520 wine_server_release_fd( wsa
->hSocket
, fd
);
2524 if (wsa
->first_iovec
< wsa
->n_iovecs
)
2525 status
= STATUS_PENDING
;
2527 status
= STATUS_SUCCESS
;
2529 iosb
->Information
+= result
;
2531 else if (errno
== EAGAIN
)
2533 status
= STATUS_PENDING
;
2537 status
= wsaErrStatus();
2541 if (status
!= STATUS_PENDING
)
2543 iosb
->u
.Status
= status
;
2544 if (wsa
->completion_func
)
2546 *apc
= ws2_async_apc
;
2550 release_async_io( &wsa
->io
);
2555 /***********************************************************************
2556 * WS2_async_shutdown (INTERNAL)
2558 * Handler for shutdown() operations on overlapped sockets.
2560 static NTSTATUS
WS2_async_shutdown( void *user
, IO_STATUS_BLOCK
*iosb
,
2561 NTSTATUS status
, void **apc
, void **arg
)
2563 struct ws2_async_shutdown
*wsa
= user
;
2568 case STATUS_ALERTED
:
2569 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
2572 switch ( wsa
->type
)
2574 case ASYNC_TYPE_READ
: err
= shutdown( fd
, 0 ); break;
2575 case ASYNC_TYPE_WRITE
: err
= shutdown( fd
, 1 ); break;
2577 status
= err
? wsaErrStatus() : STATUS_SUCCESS
;
2578 wine_server_release_fd( wsa
->hSocket
, fd
);
2581 iosb
->u
.Status
= status
;
2582 iosb
->Information
= 0;
2583 release_async_io( &wsa
->io
);
2587 /***********************************************************************
2588 * WS2_register_async_shutdown (INTERNAL)
2590 * Helper function for WS_shutdown() on overlapped sockets.
2592 static int WS2_register_async_shutdown( SOCKET s
, int type
)
2594 struct ws2_async_shutdown
*wsa
;
2597 TRACE("socket %04lx type %d\n", s
, type
);
2599 wsa
= (struct ws2_async_shutdown
*)alloc_async_io( sizeof(*wsa
) );
2603 wsa
->hSocket
= SOCKET2HANDLE(s
);
2606 SERVER_START_REQ( register_async
)
2609 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
2610 req
->async
.callback
= wine_server_client_ptr( WS2_async_shutdown
);
2611 req
->async
.iosb
= wine_server_client_ptr( &wsa
->iosb
);
2612 req
->async
.arg
= wine_server_client_ptr( wsa
);
2613 req
->async
.cvalue
= 0;
2614 status
= wine_server_call( req
);
2618 if (status
!= STATUS_PENDING
)
2620 HeapFree( GetProcessHeap(), 0, wsa
);
2621 return NtStatusToWSAError( status
);
2626 /***********************************************************************
2629 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
, int *addrlen32
)
2635 TRACE("socket %04lx\n", s
);
2636 status
= _is_blocking(s
, &is_blocking
);
2640 return INVALID_SOCKET
;
2644 /* try accepting first (if there is a deferred connection) */
2645 SERVER_START_REQ( accept_socket
)
2647 req
->lhandle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
2648 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
2649 req
->attributes
= OBJ_INHERIT
;
2650 status
= wine_server_call( req
);
2651 as
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
2656 if (addr
&& addrlen32
&& WS_getpeername(as
, addr
, addrlen32
))
2659 return SOCKET_ERROR
;
2661 TRACE("\taccepted %04lx\n", as
);
2664 if (is_blocking
&& status
== STATUS_CANT_WAIT
)
2666 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2668 do_block(fd
, POLLIN
, -1);
2669 _sync_sock_state(s
); /* let wineserver notice connection */
2670 release_sock_fd( s
, fd
);
2672 } while (is_blocking
&& status
== STATUS_CANT_WAIT
);
2675 return INVALID_SOCKET
;
2678 /***********************************************************************
2681 static BOOL WINAPI
WS2_AcceptEx(SOCKET listener
, SOCKET acceptor
, PVOID dest
, DWORD dest_len
,
2682 DWORD local_addr_len
, DWORD rem_addr_len
, LPDWORD received
,
2683 LPOVERLAPPED overlapped
)
2686 struct ws2_accept_async
*wsa
;
2689 TRACE("(%04lx, %04lx, %p, %d, %d, %d, %p, %p)\n", listener
, acceptor
, dest
, dest_len
, local_addr_len
,
2690 rem_addr_len
, received
, overlapped
);
2694 SetLastError(WSAEINVAL
);
2700 SetLastError(WSA_INVALID_PARAMETER
);
2706 SetLastError(WSAEFAULT
);
2710 fd
= get_sock_fd( listener
, FILE_READ_DATA
, NULL
);
2713 SetLastError(WSAENOTSOCK
);
2716 release_sock_fd( listener
, fd
);
2718 fd
= get_sock_fd( acceptor
, FILE_READ_DATA
, NULL
);
2721 SetLastError(WSAENOTSOCK
);
2724 release_sock_fd( acceptor
, fd
);
2726 wsa
= (struct ws2_accept_async
*)alloc_async_io( sizeof(*wsa
) );
2729 SetLastError(WSAEFAULT
);
2733 wsa
->listen_socket
= SOCKET2HANDLE(listener
);
2734 wsa
->accept_socket
= SOCKET2HANDLE(acceptor
);
2735 wsa
->user_overlapped
= overlapped
;
2736 wsa
->cvalue
= !((ULONG_PTR
)overlapped
->hEvent
& 1) ? (ULONG_PTR
)overlapped
: 0;
2738 wsa
->data_len
= dest_len
;
2739 wsa
->local_len
= local_addr_len
;
2740 wsa
->remote_len
= rem_addr_len
;
2745 /* set up a read request if we need it */
2746 wsa
->read
= (struct ws2_async
*)alloc_async_io( offsetof(struct ws2_async
, iovec
[1]) );
2749 HeapFree( GetProcessHeap(), 0, wsa
);
2750 SetLastError(WSAEFAULT
);
2754 wsa
->read
->hSocket
= wsa
->accept_socket
;
2755 wsa
->read
->flags
= 0;
2756 wsa
->read
->lpFlags
= &wsa
->read
->flags
;
2757 wsa
->read
->addr
= NULL
;
2758 wsa
->read
->addrlen
.ptr
= NULL
;
2759 wsa
->read
->control
= NULL
;
2760 wsa
->read
->n_iovecs
= 1;
2761 wsa
->read
->first_iovec
= 0;
2762 wsa
->read
->completion_func
= NULL
;
2763 wsa
->read
->iovec
[0].iov_base
= wsa
->buf
;
2764 wsa
->read
->iovec
[0].iov_len
= wsa
->data_len
;
2767 SERVER_START_REQ( register_async
)
2769 req
->type
= ASYNC_TYPE_READ
;
2770 req
->async
.handle
= wine_server_obj_handle( SOCKET2HANDLE(listener
) );
2771 req
->async
.event
= wine_server_obj_handle( overlapped
->hEvent
);
2772 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept
);
2773 req
->async
.iosb
= wine_server_client_ptr( overlapped
);
2774 req
->async
.arg
= wine_server_client_ptr( wsa
);
2775 req
->async
.cvalue
= wsa
->cvalue
;
2776 status
= wine_server_call( req
);
2780 if(status
!= STATUS_PENDING
)
2782 HeapFree( GetProcessHeap(), 0, wsa
->read
);
2783 HeapFree( GetProcessHeap(), 0, wsa
);
2786 SetLastError( NtStatusToWSAError(status
) );
2790 /***********************************************************************
2791 * WS2_ReadFile (INTERNAL)
2793 * Perform an APC-safe ReadFile operation
2795 static NTSTATUS
WS2_ReadFile(HANDLE hFile
, PIO_STATUS_BLOCK io_status
, char* buffer
, ULONG length
,
2796 PLARGE_INTEGER offset
)
2798 int result
= -1, unix_handle
;
2799 unsigned int options
;
2802 TRACE( "(%p,%p,0x%08x)\n", hFile
, buffer
,length
);
2804 status
= wine_server_handle_to_fd( hFile
, FILE_READ_DATA
, &unix_handle
, &options
);
2805 if (status
) return status
;
2807 while (result
== -1)
2809 if (offset
->QuadPart
!= FILE_USE_FILE_POINTER_POSITION
)
2810 result
= pread( unix_handle
, buffer
, length
, offset
->QuadPart
);
2812 result
= read( unix_handle
, buffer
, length
);
2818 status
= (length
? STATUS_END_OF_FILE
: STATUS_SUCCESS
);
2819 else if (result
!= -1)
2820 status
= STATUS_SUCCESS
;
2821 else if (errno
!= EAGAIN
)
2822 status
= wsaErrStatus();
2824 status
= STATUS_PENDING
;
2826 wine_server_release_fd( hFile
, unix_handle
);
2827 TRACE("= 0x%08x (%d)\n", status
, result
);
2828 if (status
== STATUS_SUCCESS
|| status
== STATUS_END_OF_FILE
)
2830 io_status
->u
.Status
= status
;
2831 io_status
->Information
= result
;
2837 /***********************************************************************
2838 * WS2_transmitfile_getbuffer (INTERNAL)
2840 * Pick the appropriate buffer for a TransmitFile send operation.
2842 static NTSTATUS
WS2_transmitfile_getbuffer( int fd
, struct ws2_transmitfile_async
*wsa
)
2844 /* send any incomplete writes from a previous iteration */
2845 if (wsa
->write
.first_iovec
< wsa
->write
.n_iovecs
)
2846 return STATUS_PENDING
;
2848 /* process the header (if applicable) */
2849 if (wsa
->buffers
.Head
)
2851 wsa
->write
.first_iovec
= 0;
2852 wsa
->write
.n_iovecs
= 1;
2853 wsa
->write
.iovec
[0].iov_base
= wsa
->buffers
.Head
;
2854 wsa
->write
.iovec
[0].iov_len
= wsa
->buffers
.HeadLength
;
2855 wsa
->buffers
.Head
= NULL
;
2856 return STATUS_PENDING
;
2859 /* process the main file */
2862 DWORD bytes_per_send
= wsa
->bytes_per_send
;
2863 IO_STATUS_BLOCK iosb
;
2866 iosb
.Information
= 0;
2867 /* when the size of the transfer is limited ensure that we don't go past that limit */
2868 if (wsa
->file_bytes
!= 0)
2869 bytes_per_send
= min(bytes_per_send
, wsa
->file_bytes
- wsa
->file_read
);
2870 status
= WS2_ReadFile( wsa
->file
, &iosb
, wsa
->buffer
, bytes_per_send
, &wsa
->offset
);
2871 if (wsa
->offset
.QuadPart
!= FILE_USE_FILE_POINTER_POSITION
)
2872 wsa
->offset
.QuadPart
+= iosb
.Information
;
2873 if (status
== STATUS_END_OF_FILE
)
2874 wsa
->file
= NULL
; /* continue on to the footer */
2875 else if (status
!= STATUS_SUCCESS
)
2879 if (iosb
.Information
)
2881 wsa
->write
.first_iovec
= 0;
2882 wsa
->write
.n_iovecs
= 1;
2883 wsa
->write
.iovec
[0].iov_base
= wsa
->buffer
;
2884 wsa
->write
.iovec
[0].iov_len
= iosb
.Information
;
2885 wsa
->file_read
+= iosb
.Information
;
2888 if (wsa
->file_bytes
!= 0 && wsa
->file_read
>= wsa
->file_bytes
)
2891 return STATUS_PENDING
;
2895 /* send the footer (if applicable) */
2896 if (wsa
->buffers
.Tail
)
2898 wsa
->write
.first_iovec
= 0;
2899 wsa
->write
.n_iovecs
= 1;
2900 wsa
->write
.iovec
[0].iov_base
= wsa
->buffers
.Tail
;
2901 wsa
->write
.iovec
[0].iov_len
= wsa
->buffers
.TailLength
;
2902 wsa
->buffers
.Tail
= NULL
;
2903 return STATUS_PENDING
;
2906 return STATUS_SUCCESS
;
2909 /***********************************************************************
2910 * WS2_transmitfile_base (INTERNAL)
2912 * Shared implementation for both synchronous and asynchronous TransmitFile.
2914 static NTSTATUS
WS2_transmitfile_base( int fd
, struct ws2_transmitfile_async
*wsa
)
2918 status
= WS2_transmitfile_getbuffer( fd
, wsa
);
2919 if (status
== STATUS_PENDING
)
2921 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)wsa
->write
.user_overlapped
;
2924 n
= WS2_send( fd
, &wsa
->write
, convert_flags(wsa
->write
.flags
) );
2927 if (iosb
) iosb
->Information
+= n
;
2929 else if (errno
!= EAGAIN
)
2930 return wsaErrStatus();
2936 /***********************************************************************
2937 * WS2_async_transmitfile (INTERNAL)
2939 * Asynchronous callback for overlapped TransmitFile operations.
2941 static NTSTATUS
WS2_async_transmitfile( void *user
, IO_STATUS_BLOCK
*iosb
,
2942 NTSTATUS status
, void **apc
, void **arg
)
2944 struct ws2_transmitfile_async
*wsa
= user
;
2947 if (status
== STATUS_ALERTED
)
2949 if (!(status
= wine_server_handle_to_fd( wsa
->write
.hSocket
, FILE_WRITE_DATA
, &fd
, NULL
)))
2951 status
= WS2_transmitfile_base( fd
, wsa
);
2952 wine_server_release_fd( wsa
->write
.hSocket
, fd
);
2954 if (status
== STATUS_PENDING
)
2958 iosb
->u
.Status
= status
;
2959 release_async_io( &wsa
->io
);
2963 /***********************************************************************
2966 static BOOL WINAPI
WS2_TransmitFile( SOCKET s
, HANDLE h
, DWORD file_bytes
, DWORD bytes_per_send
,
2967 LPOVERLAPPED overlapped
, LPTRANSMIT_FILE_BUFFERS buffers
,
2970 union generic_unix_sockaddr uaddr
;
2971 unsigned int uaddrlen
= sizeof(uaddr
);
2972 struct ws2_transmitfile_async
*wsa
;
2976 TRACE("(%lx, %p, %d, %d, %p, %p, %d)\n", s
, h
, file_bytes
, bytes_per_send
, overlapped
,
2979 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, NULL
);
2982 WSASetLastError( WSAENOTSOCK
);
2985 if (getpeername( fd
, &uaddr
.addr
, &uaddrlen
) != 0)
2987 release_sock_fd( s
, fd
);
2988 WSASetLastError( WSAENOTCONN
);
2992 FIXME("Flags are not currently supported (0x%x).\n", flags
);
2994 if (h
&& GetFileType( h
) != FILE_TYPE_DISK
)
2996 FIXME("Non-disk file handles are not currently supported.\n");
2997 release_sock_fd( s
, fd
);
2998 WSASetLastError( WSAEOPNOTSUPP
);
3002 /* set reasonable defaults when requested */
3003 if (!bytes_per_send
)
3004 bytes_per_send
= (1 << 16); /* Depends on OS version: PAGE_SIZE, 2*PAGE_SIZE, or 2^16 */
3006 if (!(wsa
= (struct ws2_transmitfile_async
*)alloc_async_io( sizeof(*wsa
) + bytes_per_send
)))
3008 release_sock_fd( s
, fd
);
3009 WSASetLastError( WSAEFAULT
);
3013 wsa
->buffers
= *buffers
;
3015 memset(&wsa
->buffers
, 0x0, sizeof(wsa
->buffers
));
3016 wsa
->buffer
= (char *)(wsa
+ 1);
3019 wsa
->file_bytes
= file_bytes
;
3020 wsa
->bytes_per_send
= bytes_per_send
;
3022 wsa
->offset
.QuadPart
= FILE_USE_FILE_POINTER_POSITION
;
3023 wsa
->write
.hSocket
= SOCKET2HANDLE(s
);
3024 wsa
->write
.addr
= NULL
;
3025 wsa
->write
.addrlen
.val
= 0;
3026 wsa
->write
.flags
= 0;
3027 wsa
->write
.lpFlags
= &wsa
->flags
;
3028 wsa
->write
.control
= NULL
;
3029 wsa
->write
.n_iovecs
= 0;
3030 wsa
->write
.first_iovec
= 0;
3031 wsa
->write
.user_overlapped
= overlapped
;
3034 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)overlapped
;
3037 wsa
->offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3038 wsa
->offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
3039 iosb
->u
.Status
= STATUS_PENDING
;
3040 iosb
->Information
= 0;
3041 SERVER_START_REQ( register_async
)
3043 req
->type
= ASYNC_TYPE_WRITE
;
3044 req
->async
.handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
3045 req
->async
.event
= wine_server_obj_handle( overlapped
->hEvent
);
3046 req
->async
.callback
= wine_server_client_ptr( WS2_async_transmitfile
);
3047 req
->async
.iosb
= wine_server_client_ptr( iosb
);
3048 req
->async
.arg
= wine_server_client_ptr( wsa
);
3049 status
= wine_server_call( req
);
3053 if(status
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
3054 release_sock_fd( s
, fd
);
3055 WSASetLastError( NtStatusToWSAError(status
) );
3061 status
= WS2_transmitfile_base( fd
, wsa
);
3062 if (status
== STATUS_PENDING
)
3065 do_block(fd
, POLLOUT
, -1);
3066 _sync_sock_state(s
); /* let wineserver notice connection */
3069 while (status
== STATUS_PENDING
);
3070 release_sock_fd( s
, fd
);
3072 if (status
!= STATUS_SUCCESS
)
3073 WSASetLastError( NtStatusToWSAError(status
) );
3074 HeapFree( GetProcessHeap(), 0, wsa
);
3075 return (status
== STATUS_SUCCESS
);
3078 /***********************************************************************
3079 * GetAcceptExSockaddrs
3081 static void WINAPI
WS2_GetAcceptExSockaddrs(PVOID buffer
, DWORD data_size
, DWORD local_size
, DWORD remote_size
,
3082 struct WS_sockaddr
**local_addr
, LPINT local_addr_len
,
3083 struct WS_sockaddr
**remote_addr
, LPINT remote_addr_len
)
3085 char *cbuf
= buffer
;
3086 TRACE("(%p, %d, %d, %d, %p, %p, %p, %p)\n", buffer
, data_size
, local_size
, remote_size
, local_addr
,
3087 local_addr_len
, remote_addr
, remote_addr_len
);
3090 *local_addr_len
= *(int *) cbuf
;
3091 *local_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
3095 *remote_addr_len
= *(int *) cbuf
;
3096 *remote_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
3099 /***********************************************************************
3102 int WINAPI
WSASendMsg( SOCKET s
, LPWSAMSG msg
, DWORD dwFlags
, LPDWORD lpNumberOfBytesSent
,
3103 LPWSAOVERLAPPED lpOverlapped
,
3104 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3108 SetLastError( WSAEFAULT
);
3109 return SOCKET_ERROR
;
3112 return WS2_sendto( s
, msg
->lpBuffers
, msg
->dwBufferCount
, lpNumberOfBytesSent
,
3113 dwFlags
, msg
->name
, msg
->namelen
,
3114 lpOverlapped
, lpCompletionRoutine
);
3117 /***********************************************************************
3120 * Perform a receive operation that is capable of returning message
3121 * control headers. It is important to note that the WSAMSG parameter
3122 * must remain valid throughout the operation, even when an overlapped
3123 * receive is performed.
3125 static int WINAPI
WS2_WSARecvMsg( SOCKET s
, LPWSAMSG msg
, LPDWORD lpNumberOfBytesRecvd
,
3126 LPWSAOVERLAPPED lpOverlapped
,
3127 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3131 SetLastError( WSAEFAULT
);
3132 return SOCKET_ERROR
;
3135 return WS2_recv_base( s
, msg
->lpBuffers
, msg
->dwBufferCount
, lpNumberOfBytesRecvd
,
3136 &msg
->dwFlags
, msg
->name
, &msg
->namelen
,
3137 lpOverlapped
, lpCompletionRoutine
, &msg
->Control
);
3140 /***********************************************************************
3141 * interface_bind (INTERNAL)
3143 * Take bind() calls on any name corresponding to a local network adapter and restrict the given socket to
3144 * operating only on the specified interface. This restriction consists of two components:
3145 * 1) An outgoing packet restriction suggesting the egress interface for all packets.
3146 * 2) An incoming packet restriction dropping packets not meant for the interface.
3147 * If the function succeeds in placing these restrictions (returns TRUE) then the name for the bind() may
3148 * safely be changed to INADDR_ANY, permitting the transmission and receipt of broadcast packets on the
3149 * socket. This behavior is only relevant to UDP sockets and is needed for applications that expect to be able
3150 * to receive broadcast packets on a socket that is bound to a specific network interface.
3152 static BOOL
interface_bind( SOCKET s
, int fd
, struct sockaddr
*addr
)
3154 struct sockaddr_in
*in_sock
= (struct sockaddr_in
*) addr
;
3155 in_addr_t bind_addr
= in_sock
->sin_addr
.s_addr
;
3156 PIP_ADAPTER_INFO adapters
= NULL
, adapter
;
3161 if (bind_addr
== htonl(INADDR_ANY
) || bind_addr
== htonl(INADDR_LOOPBACK
))
3162 return FALSE
; /* Not binding to a network adapter, special interface binding unnecessary. */
3163 if (_get_fd_type(fd
) != SOCK_DGRAM
)
3164 return FALSE
; /* Special interface binding is only necessary for UDP datagrams. */
3165 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
3167 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
3168 if (adapters
== NULL
|| GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
3170 /* Search the IPv4 adapter list for the appropriate binding interface */
3171 for (adapter
= adapters
; adapter
!= NULL
; adapter
= adapter
->Next
)
3173 in_addr_t adapter_addr
= (in_addr_t
) inet_addr(adapter
->IpAddressList
.IpAddress
.String
);
3175 if (bind_addr
== adapter_addr
)
3177 #if defined(IP_BOUND_IF)
3178 /* IP_BOUND_IF sets both the incoming and outgoing restriction at once */
3179 if (setsockopt(fd
, IPPROTO_IP
, IP_BOUND_IF
, &adapter
->Index
, sizeof(adapter
->Index
)) != 0)
3182 #elif defined(LINUX_BOUND_IF)
3183 in_addr_t ifindex
= (in_addr_t
) htonl(adapter
->Index
);
3184 struct interface_filter specific_interface_filter
;
3185 struct sock_fprog filter_prog
;
3187 if (setsockopt(fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
)) != 0)
3188 goto cleanup
; /* Failed to suggest egress interface */
3189 specific_interface_filter
= generic_interface_filter
;
3190 specific_interface_filter
.iface_rule
.k
= adapter
->Index
;
3191 specific_interface_filter
.ip_rule
.k
= htonl(adapter_addr
);
3192 filter_prog
.len
= sizeof(generic_interface_filter
)/sizeof(struct sock_filter
);
3193 filter_prog
.filter
= (struct sock_filter
*) &specific_interface_filter
;
3194 if (setsockopt(fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
)) != 0)
3195 goto cleanup
; /* Failed to specify incoming packet filter */
3198 FIXME("Broadcast packets on interface-bound sockets are not currently supported on this platform, "
3199 "receiving broadcast packets will not work on socket %04lx.\n", s
);
3204 /* Will soon be switching to INADDR_ANY: permit address reuse */
3205 if (ret
&& setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
)) == 0)
3206 TRACE("Socket %04lx bound to interface index %d\n", s
, adapter
->Index
);
3212 ERR("Failed to bind to interface, receiving broadcast packets will not work on socket %04lx.\n", s
);
3213 HeapFree(GetProcessHeap(), 0, adapters
);
3217 /***********************************************************************
3220 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
3222 int fd
= get_sock_fd( s
, 0, NULL
);
3223 int res
= SOCKET_ERROR
;
3225 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
3229 if (!name
|| (name
->sa_family
&& !supported_pf(name
->sa_family
)))
3231 SetLastError(WSAEAFNOSUPPORT
);
3235 union generic_unix_sockaddr uaddr
;
3236 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
3239 SetLastError(WSAEFAULT
);
3244 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) &uaddr
;
3245 if (name
->sa_family
== WS_AF_INET6
&&
3246 !memcmp(&in6
->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
)))
3249 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
)) == -1)
3251 release_sock_fd( s
, fd
);
3252 SetLastError(WSAEAFNOSUPPORT
);
3253 return SOCKET_ERROR
;
3257 if (name
->sa_family
== WS_AF_INET
)
3259 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
3260 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
3262 /* Trying to bind to the default host interface, using
3263 * INADDR_ANY instead*/
3264 WARN("Trying to bind to magic IP address, using "
3265 "INADDR_ANY instead.\n");
3266 in4
->sin_addr
.s_addr
= htonl(INADDR_ANY
);
3268 else if (interface_bind(s
, fd
, &uaddr
.addr
))
3269 in4
->sin_addr
.s_addr
= htonl(INADDR_ANY
);
3271 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
3273 int loc_errno
= errno
;
3274 WARN("\tfailure - errno = %i\n", errno
);
3279 SetLastError(WSAEINVAL
);
3284 socklen_t optlen
= sizeof(optval
);
3285 /* Windows >= 2003 will return different results depending on
3286 * SO_REUSEADDR, WSAEACCES may be returned representing that
3287 * the socket hijacking protection prevented the bind */
3288 if (!getsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&optval
, &optlen
) && optval
)
3290 SetLastError(WSAEACCES
);
3296 SetLastError(wsaErrno());
3302 res
=0; /* success */
3306 release_sock_fd( s
, fd
);
3311 /***********************************************************************
3312 * closesocket (WS2_32.3)
3314 int WINAPI
WS_closesocket(SOCKET s
)
3316 int res
= SOCKET_ERROR
, fd
;
3319 fd
= get_sock_fd(s
, FILE_READ_DATA
, NULL
);
3322 release_sock_fd(s
, fd
);
3323 if (CloseHandle(SOCKET2HANDLE(s
)))
3327 SetLastError(WSAENOTSOCK
);
3330 SetLastError(WSANOTINITIALISED
);
3331 TRACE("(socket %04lx) -> %d\n", s
, res
);
3335 static int do_connect(int fd
, const struct WS_sockaddr
* name
, int namelen
)
3337 union generic_unix_sockaddr uaddr
;
3338 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
3343 if (name
->sa_family
== WS_AF_INET
)
3345 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
3346 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
3348 /* Trying to connect to magic replace-loopback address,
3349 * assuming we really want to connect to localhost */
3350 TRACE("Trying to connect to magic IP address, using "
3351 "INADDR_LOOPBACK instead.\n");
3352 in4
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
3356 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
3362 /***********************************************************************
3363 * connect (WS2_32.4)
3365 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
3367 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
3369 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
3375 int ret
= do_connect(fd
, name
, namelen
);
3377 goto connect_success
;
3379 if (ret
== WSAEINPROGRESS
)
3381 /* tell wineserver that a connection is in progress */
3382 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3384 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
3385 status
= _is_blocking( s
, &is_blocking
);
3388 release_sock_fd( s
, fd
);
3389 set_error( status
);
3390 return SOCKET_ERROR
;
3396 do_block(fd
, POLLIN
| POLLOUT
, -1);
3397 _sync_sock_state(s
); /* let wineserver notice connection */
3398 /* retrieve any error codes from it */
3399 result
= _get_sock_error(s
, FD_CONNECT_BIT
);
3401 SetLastError(NtStatusToWSAError(result
));
3404 goto connect_success
;
3409 SetLastError(WSAEWOULDBLOCK
);
3416 release_sock_fd( s
, fd
);
3418 return SOCKET_ERROR
;
3421 release_sock_fd( s
, fd
);
3422 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3423 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
3424 FD_CONNECT
|FD_WINE_LISTENING
);
3425 TRACE("\tconnected %04lx\n", s
);
3429 /***********************************************************************
3430 * WSAConnect (WS2_32.30)
3432 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
3433 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
3434 LPQOS lpSQOS
, LPQOS lpGQOS
)
3436 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
3437 FIXME("unsupported parameters!\n");
3438 return WS_connect( s
, name
, namelen
);
3441 /***********************************************************************
3444 static BOOL WINAPI
WS2_ConnectEx(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
3445 PVOID sendBuf
, DWORD sendBufLen
, LPDWORD sent
, LPOVERLAPPED ov
)
3447 int fd
, ret
, status
;
3451 SetLastError( ERROR_INVALID_PARAMETER
);
3455 fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
3458 SetLastError( WSAENOTSOCK
);
3462 TRACE("socket %04lx, ptr %p %s, length %d, sendptr %p, len %d, ov %p\n",
3463 s
, name
, debugstr_sockaddr(name
), namelen
, sendBuf
, sendBufLen
, ov
);
3465 ret
= is_fd_bound(fd
, NULL
, NULL
);
3468 SetLastError(ret
== -1 ? wsaErrno() : WSAEINVAL
);
3469 release_sock_fd( s
, fd
);
3473 ret
= do_connect(fd
, name
, namelen
);
3478 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3479 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
3480 FD_CONNECT
|FD_WINE_LISTENING
);
3482 wsabuf
.len
= sendBufLen
;
3483 wsabuf
.buf
= (char*) sendBuf
;
3485 /* WSASend takes care of completion if need be */
3486 if (WSASend(s
, &wsabuf
, sendBuf
? 1 : 0, sent
, 0, ov
, NULL
) != SOCKET_ERROR
)
3487 goto connection_success
;
3489 else if (ret
== WSAEINPROGRESS
)
3491 struct ws2_async
*wsa
;
3492 ULONG_PTR cvalue
= (((ULONG_PTR
)ov
->hEvent
& 1) == 0) ? (ULONG_PTR
)ov
: 0;
3494 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3496 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
3498 /* Indirectly call WSASend */
3499 if (!(wsa
= (struct ws2_async
*)alloc_async_io( offsetof( struct ws2_async
, iovec
[1] ))))
3501 SetLastError(WSAEFAULT
);
3505 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)ov
;
3506 iosb
->u
.Status
= STATUS_PENDING
;
3507 iosb
->Information
= 0;
3509 wsa
->hSocket
= SOCKET2HANDLE(s
);
3511 wsa
->addrlen
.val
= 0;
3513 wsa
->lpFlags
= &wsa
->flags
;
3514 wsa
->control
= NULL
;
3515 wsa
->n_iovecs
= sendBuf
? 1 : 0;
3516 wsa
->first_iovec
= 0;
3517 wsa
->completion_func
= NULL
;
3518 wsa
->iovec
[0].iov_base
= sendBuf
;
3519 wsa
->iovec
[0].iov_len
= sendBufLen
;
3521 SERVER_START_REQ( register_async
)
3523 req
->type
= ASYNC_TYPE_WRITE
;
3524 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
3525 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
3526 req
->async
.iosb
= wine_server_client_ptr( iosb
);
3527 req
->async
.arg
= wine_server_client_ptr( wsa
);
3528 req
->async
.event
= wine_server_obj_handle( ov
->hEvent
);
3529 req
->async
.cvalue
= cvalue
;
3530 status
= wine_server_call( req
);
3534 if (status
!= STATUS_PENDING
) HeapFree(GetProcessHeap(), 0, wsa
);
3536 /* If the connect already failed */
3537 if (status
== STATUS_PIPE_DISCONNECTED
)
3538 status
= _get_sock_error(s
, FD_CONNECT_BIT
);
3539 SetLastError( NtStatusToWSAError(status
) );
3547 release_sock_fd( s
, fd
);
3551 release_sock_fd( s
, fd
);
3555 /***********************************************************************
3558 static BOOL WINAPI
WS2_DisconnectEx( SOCKET s
, LPOVERLAPPED ov
, DWORD flags
, DWORD reserved
)
3560 TRACE( "socket %04lx, ov %p, flags 0x%x, reserved 0x%x\n", s
, ov
, flags
, reserved
);
3562 if (flags
& TF_REUSE_SOCKET
)
3563 FIXME( "Reusing socket not supported yet\n" );
3567 ov
->Internal
= STATUS_PENDING
;
3568 ov
->InternalHigh
= 0;
3571 return !WS_shutdown( s
, SD_BOTH
);
3574 /***********************************************************************
3575 * getpeername (WS2_32.5)
3577 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
3582 TRACE("socket %04lx, ptr %p, len %08x\n", s
, name
, namelen
? *namelen
: 0);
3584 fd
= get_sock_fd( s
, 0, NULL
);
3589 union generic_unix_sockaddr uaddr
;
3590 socklen_t uaddrlen
= sizeof(uaddr
);
3592 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) == 0)
3594 if (!name
|| !namelen
)
3595 SetLastError(WSAEFAULT
);
3596 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
3597 /* The buffer was too small */
3598 SetLastError(WSAEFAULT
);
3602 TRACE("=> %s\n", debugstr_sockaddr(name
));
3606 SetLastError(wsaErrno());
3607 release_sock_fd( s
, fd
);
3612 /* When binding to an UDP address with filter support the getsockname call on the socket
3613 * will always return 0.0.0.0 instead of the filtered interface address. This function
3614 * checks if the socket is interface-bound on UDP and return the correct address.
3615 * This is required because applications often do a bind() with port zero followed by a
3616 * getsockname() to retrieve the port and address acquired.
3618 static void interface_bind_check(int fd
, struct sockaddr_in
*addr
)
3620 #if !defined(IP_BOUND_IF) && !defined(LINUX_BOUND_IF)
3626 /* Check for IPv4, address 0.0.0.0 and UDP socket */
3627 if (addr
->sin_family
!= AF_INET
|| addr
->sin_addr
.s_addr
!= 0)
3629 if (_get_fd_type(fd
) != SOCK_DGRAM
)
3633 len
= sizeof(ifindex
);
3634 #if defined(IP_BOUND_IF)
3635 getsockopt(fd
, IPPROTO_IP
, IP_BOUND_IF
, &ifindex
, &len
);
3636 #elif defined(LINUX_BOUND_IF)
3637 getsockopt(fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, &len
);
3638 if (ifindex
> 0) ifindex
= ntohl(ifindex
);
3642 PIP_ADAPTER_INFO adapters
, adapter
;
3645 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
3647 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
3648 if (adapters
&& GetAdaptersInfo(adapters
, &adap_size
) == NO_ERROR
)
3650 /* Search the IPv4 adapter list for the appropriate bound interface */
3651 for (adapter
= adapters
; adapter
!= NULL
; adapter
= adapter
->Next
)
3653 in_addr_t adapter_addr
;
3654 if (adapter
->Index
!= ifindex
) continue;
3656 adapter_addr
= inet_addr(adapter
->IpAddressList
.IpAddress
.String
);
3657 addr
->sin_addr
.s_addr
= adapter_addr
;
3658 TRACE("reporting interface address from adapter %d\n", ifindex
);
3662 HeapFree(GetProcessHeap(), 0, adapters
);
3667 /***********************************************************************
3668 * getsockname (WS2_32.6)
3670 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
3675 TRACE("socket %04lx, ptr %p, len %08x\n", s
, name
, namelen
? *namelen
: 0);
3677 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
3678 if( (name
== NULL
) || (namelen
== NULL
) )
3680 SetLastError( WSAEFAULT
);
3681 return SOCKET_ERROR
;
3684 fd
= get_sock_fd( s
, 0, NULL
);
3689 union generic_unix_sockaddr uaddr
;
3691 int bound
= is_fd_bound(fd
, &uaddr
, &uaddrlen
);
3695 SetLastError(bound
== -1 ? wsaErrno() : WSAEINVAL
);
3697 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
3699 /* The buffer was too small */
3700 SetLastError(WSAEFAULT
);
3704 interface_bind_check(fd
, (struct sockaddr_in
*) &uaddr
);
3705 if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
3707 /* The buffer was too small */
3708 SetLastError(WSAEFAULT
);
3713 TRACE("=> %s\n", debugstr_sockaddr(name
));
3716 release_sock_fd( s
, fd
);
3721 /***********************************************************************
3722 * getsockopt (WS2_32.7)
3724 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
3725 INT optname
, char *optval
, INT
*optlen
)
3730 TRACE("(socket %04lx, %s, optval %s, optlen %p (%d))\n", s
,
3731 debugstr_sockopt(level
, optname
), debugstr_optval(optval
, 0),
3732 optlen
, optlen
? *optlen
: 0);
3740 /* Handle common cases. The special cases are below, sorted
3742 case WS_SO_BROADCAST
:
3744 case WS_SO_KEEPALIVE
:
3745 case WS_SO_OOBINLINE
:
3747 case WS_SO_REUSEADDR
:
3749 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3750 return SOCKET_ERROR
;
3751 convert_sockopt(&level
, &optname
);
3752 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
3754 SetLastError(wsaErrno());
3757 release_sock_fd( s
, fd
);
3759 case WS_SO_ACCEPTCONN
:
3760 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3761 return SOCKET_ERROR
;
3762 if (getsockopt(fd
, SOL_SOCKET
, SO_ACCEPTCONN
, optval
, (socklen_t
*)optlen
) != 0 )
3764 SetLastError(wsaErrno());
3769 /* BSD returns != 0 while Windows return exact == 1 */
3770 if (*(int *)optval
) *(int *)optval
= 1;
3772 release_sock_fd( s
, fd
);
3774 case WS_SO_BSP_STATE
:
3776 int req_size
, addr_size
;
3777 WSAPROTOCOL_INFOW infow
;
3778 CSADDR_INFO
*csinfo
;
3780 ret
= ws_protocol_info(s
, TRUE
, &infow
, &addr_size
);
3783 if (infow
.iAddressFamily
== WS_AF_INET
)
3784 addr_size
= sizeof(struct sockaddr_in
);
3785 else if (infow
.iAddressFamily
== WS_AF_INET6
)
3786 addr_size
= sizeof(struct sockaddr_in6
);
3789 FIXME("Family %d is unsupported for SO_BSP_STATE\n", infow
.iAddressFamily
);
3790 SetLastError(WSAEAFNOSUPPORT
);
3791 return SOCKET_ERROR
;
3794 req_size
= sizeof(CSADDR_INFO
) + addr_size
* 2;
3795 if (*optlen
< req_size
)
3798 SetLastError(WSAEFAULT
);
3802 union generic_unix_sockaddr uaddr
;
3805 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3806 return SOCKET_ERROR
;
3808 csinfo
= (CSADDR_INFO
*) optval
;
3810 /* Check if the sock is bound */
3811 if (is_fd_bound(fd
, &uaddr
, &uaddrlen
) == 1)
3813 csinfo
->LocalAddr
.lpSockaddr
=
3814 (LPSOCKADDR
) (optval
+ sizeof(CSADDR_INFO
));
3815 ws_sockaddr_u2ws(&uaddr
.addr
, csinfo
->LocalAddr
.lpSockaddr
, &addr_size
);
3816 csinfo
->LocalAddr
.iSockaddrLength
= addr_size
;
3820 csinfo
->LocalAddr
.lpSockaddr
= NULL
;
3821 csinfo
->LocalAddr
.iSockaddrLength
= 0;
3824 /* Check if the sock is connected */
3825 if (!getpeername(fd
, &uaddr
.addr
, &uaddrlen
) &&
3826 is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
3828 csinfo
->RemoteAddr
.lpSockaddr
=
3829 (LPSOCKADDR
) (optval
+ sizeof(CSADDR_INFO
) + addr_size
);
3830 ws_sockaddr_u2ws(&uaddr
.addr
, csinfo
->RemoteAddr
.lpSockaddr
, &addr_size
);
3831 csinfo
->RemoteAddr
.iSockaddrLength
= addr_size
;
3835 csinfo
->RemoteAddr
.lpSockaddr
= NULL
;
3836 csinfo
->RemoteAddr
.iSockaddrLength
= 0;
3839 csinfo
->iSocketType
= infow
.iSocketType
;
3840 csinfo
->iProtocol
= infow
.iProtocol
;
3841 release_sock_fd( s
, fd
);
3844 return ret
? 0 : SOCKET_ERROR
;
3846 case WS_SO_DONTLINGER
:
3848 struct linger lingval
;
3849 socklen_t len
= sizeof(struct linger
);
3851 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
3853 SetLastError(WSAEFAULT
);
3854 return SOCKET_ERROR
;
3856 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3857 return SOCKET_ERROR
;
3859 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
3861 SetLastError(wsaErrno());
3866 *(BOOL
*)optval
= !lingval
.l_onoff
;
3867 *optlen
= sizeof(BOOL
);
3870 release_sock_fd( s
, fd
);
3874 case WS_SO_CONNECT_TIME
:
3876 static int pretendtime
= 0;
3877 struct WS_sockaddr addr
;
3878 int len
= sizeof(addr
);
3880 if (!optlen
|| *optlen
< sizeof(DWORD
) || !optval
)
3882 SetLastError(WSAEFAULT
);
3883 return SOCKET_ERROR
;
3885 if (WS_getpeername(s
, &addr
, &len
) == SOCKET_ERROR
)
3886 *(DWORD
*)optval
= ~0u;
3889 if (!pretendtime
) FIXME("WS_SO_CONNECT_TIME - faking results\n");
3890 *(DWORD
*)optval
= pretendtime
++;
3892 *optlen
= sizeof(DWORD
);
3895 /* As mentioned in setsockopt, Windows ignores this, so we
3896 * always return true here */
3897 case WS_SO_DONTROUTE
:
3898 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
3900 SetLastError(WSAEFAULT
);
3901 return SOCKET_ERROR
;
3903 *(BOOL
*)optval
= TRUE
;
3904 *optlen
= sizeof(BOOL
);
3909 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3910 return SOCKET_ERROR
;
3911 if (getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, optval
, (socklen_t
*)optlen
) != 0 )
3913 SetLastError(wsaErrno());
3916 release_sock_fd( s
, fd
);
3918 /* The wineserver may have swallowed the error before us */
3919 if (!ret
&& *(int*) optval
== 0)
3921 int i
, events
[FD_MAX_EVENTS
];
3922 _get_sock_errors(s
, events
);
3923 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
3927 events
[i
] = NtStatusToWSAError(events
[i
]);
3928 TRACE("returning SO_ERROR %d from wine server\n", events
[i
]);
3929 *(int*) optval
= events
[i
];
3939 struct linger lingval
;
3940 socklen_t len
= sizeof(struct linger
);
3942 /* struct linger and LINGER have different sizes */
3943 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
3945 SetLastError(WSAEFAULT
);
3946 return SOCKET_ERROR
;
3948 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3949 return SOCKET_ERROR
;
3951 if (_get_fd_type(fd
) == SOCK_DGRAM
)
3953 SetLastError(WSAENOPROTOOPT
);
3956 else if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0)
3958 SetLastError(wsaErrno());
3963 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
3964 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
3965 *optlen
= sizeof(struct linger
);
3968 release_sock_fd( s
, fd
);
3972 case WS_SO_MAX_MSG_SIZE
:
3973 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
3975 SetLastError(WSAEFAULT
);
3976 return SOCKET_ERROR
;
3978 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
3979 *(int *)optval
= 65507;
3980 *optlen
= sizeof(int);
3983 /* SO_OPENTYPE does not require a valid socket handle. */
3984 case WS_SO_OPENTYPE
:
3985 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
3987 SetLastError(WSAEFAULT
);
3988 return SOCKET_ERROR
;
3990 *(int *)optval
= get_per_thread_data()->opentype
;
3991 *optlen
= sizeof(int);
3992 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
3994 case WS_SO_PROTOCOL_INFOA
:
3995 case WS_SO_PROTOCOL_INFOW
:
3998 WSAPROTOCOL_INFOW infow
;
4000 ret
= ws_protocol_info(s
, optname
== WS_SO_PROTOCOL_INFOW
, &infow
, &size
);
4003 if (!optlen
|| !optval
|| *optlen
< size
)
4005 if(optlen
) *optlen
= size
;
4007 SetLastError(WSAEFAULT
);
4010 memcpy(optval
, &infow
, size
);
4012 return ret
? 0 : SOCKET_ERROR
;
4014 case WS_SO_RCVTIMEO
:
4015 case WS_SO_SNDTIMEO
:
4019 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
4021 SetLastError(WSAEFAULT
);
4022 return SOCKET_ERROR
;
4024 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4025 return SOCKET_ERROR
;
4027 timeout
= get_rcvsnd_timeo(fd
, optname
== WS_SO_RCVTIMEO
);
4028 *(int *)optval
= timeout
<= UINT_MAX
? timeout
: UINT_MAX
;
4030 release_sock_fd( s
, fd
);
4036 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
4038 SetLastError(WSAEFAULT
);
4039 return SOCKET_ERROR
;
4041 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4042 return SOCKET_ERROR
;
4044 sock_type
= _get_fd_type(fd
);
4045 if (sock_type
== -1)
4047 SetLastError(wsaErrno());
4051 (*(int *)optval
) = convert_socktype_u2w(sock_type
);
4053 release_sock_fd( s
, fd
);
4057 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
4058 SetLastError(WSAENOPROTOOPT
);
4059 return SOCKET_ERROR
;
4060 } /* end switch(optname) */
4061 }/* end case WS_SOL_SOCKET */
4063 case WS_NSPROTO_IPX
:
4065 struct WS_sockaddr_ipx addr
;
4066 IPX_ADDRESS_DATA
*data
;
4071 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
4073 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (socklen_t
*)optlen
) == -1)
4080 socklen_t len
=sizeof(struct ipx
);
4081 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
4084 *optval
= (int)val
.ipx_pt
;
4087 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
4088 release_sock_fd( s
, fd
);
4091 case WS_IPX_ADDRESS
:
4093 * On a Win2000 system with one network card there are usually
4094 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
4095 * Using this call you can then retrieve info about this all.
4096 * In case of Linux it is a bit different. Usually you have
4097 * only "one" device active and further it is not possible to
4098 * query things like the linkspeed.
4100 FIXME("IPX_ADDRESS\n");
4101 namelen
= sizeof(struct WS_sockaddr_ipx
);
4102 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
4103 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
4105 data
= (IPX_ADDRESS_DATA
*)optval
;
4106 memcpy(data
->nodenum
,addr
.sa_nodenum
,sizeof(data
->nodenum
));
4107 memcpy(data
->netnum
,addr
.sa_netnum
,sizeof(data
->netnum
));
4108 data
->adapternum
= 0;
4109 data
->wan
= FALSE
; /* We are not on a wan for now .. */
4110 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
4111 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
4112 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
4113 * note 1MB = 1000kB in this case */
4116 case WS_IPX_MAX_ADAPTER_NUM
:
4117 FIXME("IPX_MAX_ADAPTER_NUM\n");
4118 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
4122 FIXME("IPX optname:%x\n", optname
);
4123 return SOCKET_ERROR
;
4124 }/* end switch(optname) */
4125 } /* end case WS_NSPROTO_IPX */
4129 #define MAX_IRDA_DEVICES 10
4134 case WS_IRLMP_ENUMDEVICES
:
4136 char buf
[sizeof(struct irda_device_list
) +
4137 (MAX_IRDA_DEVICES
- 1) * sizeof(struct irda_device_info
)];
4139 socklen_t len
= sizeof(buf
);
4141 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4142 return SOCKET_ERROR
;
4143 res
= getsockopt( fd
, SOL_IRLMP
, IRLMP_ENUMDEVICES
, buf
, &len
);
4144 release_sock_fd( s
, fd
);
4147 SetLastError(wsaErrno());
4148 return SOCKET_ERROR
;
4152 struct irda_device_list
*src
= (struct irda_device_list
*)buf
;
4153 DEVICELIST
*dst
= (DEVICELIST
*)optval
;
4154 INT needed
= sizeof(DEVICELIST
);
4158 needed
+= (src
->len
- 1) * sizeof(IRDA_DEVICE_INFO
);
4159 if (*optlen
< needed
)
4161 SetLastError(WSAEFAULT
);
4162 return SOCKET_ERROR
;
4165 TRACE("IRLMP_ENUMDEVICES: %d devices found:\n", src
->len
);
4166 dst
->numDevice
= src
->len
;
4167 for (i
= 0; i
< src
->len
; i
++)
4169 TRACE("saddr = %08x, daddr = %08x, info = %s, hints = %02x%02x\n",
4170 src
->dev
[i
].saddr
, src
->dev
[i
].daddr
,
4171 src
->dev
[i
].info
, src
->dev
[i
].hints
[0],
4172 src
->dev
[i
].hints
[1]);
4173 memcpy( dst
->Device
[i
].irdaDeviceID
,
4175 sizeof(dst
->Device
[i
].irdaDeviceID
) ) ;
4176 memcpy( dst
->Device
[i
].irdaDeviceName
,
4178 sizeof(dst
->Device
[i
].irdaDeviceName
) ) ;
4179 memcpy( &dst
->Device
[i
].irdaDeviceHints1
,
4180 &src
->dev
[i
].hints
[0],
4181 sizeof(dst
->Device
[i
].irdaDeviceHints1
) ) ;
4182 memcpy( &dst
->Device
[i
].irdaDeviceHints2
,
4183 &src
->dev
[i
].hints
[1],
4184 sizeof(dst
->Device
[i
].irdaDeviceHints2
) ) ;
4185 dst
->Device
[i
].irdaCharSet
= src
->dev
[i
].charset
;
4191 FIXME("IrDA optname:0x%x\n", optname
);
4192 return SOCKET_ERROR
;
4194 break; /* case WS_SOL_IRLMP */
4195 #undef MAX_IRDA_DEVICES
4198 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
4199 case WS_IPPROTO_TCP
:
4202 case WS_TCP_NODELAY
:
4203 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4204 return SOCKET_ERROR
;
4205 convert_sockopt(&level
, &optname
);
4206 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
4208 SetLastError(wsaErrno());
4211 release_sock_fd( s
, fd
);
4214 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
4215 return SOCKET_ERROR
;
4220 case WS_IP_ADD_MEMBERSHIP
:
4221 case WS_IP_DROP_MEMBERSHIP
:
4225 case WS_IP_MULTICAST_IF
:
4226 case WS_IP_MULTICAST_LOOP
:
4227 case WS_IP_MULTICAST_TTL
:
4234 #ifdef IP_UNICAST_IF
4235 case WS_IP_UNICAST_IF
:
4237 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4238 return SOCKET_ERROR
;
4239 convert_sockopt(&level
, &optname
);
4240 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
4242 SetLastError(wsaErrno());
4245 release_sock_fd( s
, fd
);
4247 case WS_IP_DONTFRAGMENT
:
4248 FIXME("WS_IP_DONTFRAGMENT is always false!\n");
4249 *(BOOL
*)optval
= FALSE
;
4252 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
4253 return SOCKET_ERROR
;
4255 case WS_IPPROTO_IPV6
:
4258 #ifdef IPV6_ADD_MEMBERSHIP
4259 case WS_IPV6_ADD_MEMBERSHIP
:
4261 #ifdef IPV6_DROP_MEMBERSHIP
4262 case WS_IPV6_DROP_MEMBERSHIP
:
4264 case WS_IPV6_MULTICAST_IF
:
4265 case WS_IPV6_MULTICAST_HOPS
:
4266 case WS_IPV6_MULTICAST_LOOP
:
4267 case WS_IPV6_UNICAST_HOPS
:
4268 case WS_IPV6_V6ONLY
:
4269 #ifdef IPV6_UNICAST_IF
4270 case WS_IPV6_UNICAST_IF
:
4272 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4273 return SOCKET_ERROR
;
4274 convert_sockopt(&level
, &optname
);
4275 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
4277 SetLastError(wsaErrno());
4280 release_sock_fd( s
, fd
);
4282 case WS_IPV6_DONTFRAG
:
4283 FIXME("WS_IPV6_DONTFRAG is always false!\n");
4284 *(BOOL
*)optval
= FALSE
;
4287 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
4288 return SOCKET_ERROR
;
4291 WARN("Unknown level: 0x%08x\n", level
);
4292 SetLastError(WSAEINVAL
);
4293 return SOCKET_ERROR
;
4294 } /* end switch(level) */
4297 /***********************************************************************
4300 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
4302 return htonl(hostlong
);
4306 /***********************************************************************
4309 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
4311 return htons(hostshort
);
4314 /***********************************************************************
4315 * WSAHtonl (WS2_32.46)
4316 * From MSDN description of error codes, this function should also
4317 * check if WinSock has been initialized and the socket is a valid
4318 * socket. But why? This function only translates a host byte order
4319 * u_long into a network byte order u_long...
4321 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
4325 *lpnetlong
= htonl(hostlong
);
4328 SetLastError(WSAEFAULT
);
4329 return SOCKET_ERROR
;
4332 /***********************************************************************
4333 * WSAHtons (WS2_32.47)
4334 * From MSDN description of error codes, this function should also
4335 * check if WinSock has been initialized and the socket is a valid
4336 * socket. But why? This function only translates a host byte order
4337 * u_short into a network byte order u_short...
4339 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
4344 *lpnetshort
= htons(hostshort
);
4347 SetLastError(WSAEFAULT
);
4348 return SOCKET_ERROR
;
4352 /***********************************************************************
4353 * inet_addr (WS2_32.11)
4355 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
4357 if (!cp
) return INADDR_NONE
;
4358 return inet_addr(cp
);
4362 /***********************************************************************
4365 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
4367 return ntohl(netlong
);
4371 /***********************************************************************
4374 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
4376 return ntohs(netshort
);
4380 /***********************************************************************
4381 * inet_ntoa (WS2_32.12)
4383 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
4385 struct per_thread_data
*data
= get_per_thread_data();
4387 sprintf( data
->ntoa_buffer
, "%u.%u.%u.%u",
4388 (unsigned int)(ntohl( in
.WS_s_addr
) >> 24 & 0xff),
4389 (unsigned int)(ntohl( in
.WS_s_addr
) >> 16 & 0xff),
4390 (unsigned int)(ntohl( in
.WS_s_addr
) >> 8 & 0xff),
4391 (unsigned int)(ntohl( in
.WS_s_addr
) & 0xff) );
4393 return data
->ntoa_buffer
;
4396 static const char *debugstr_wsaioctl(DWORD code
)
4398 const char *name
= NULL
, *buf_type
, *family
;
4400 #define IOCTL_NAME(x) case x: name = #x; break
4403 IOCTL_NAME(WS_FIONBIO
);
4404 IOCTL_NAME(WS_FIONREAD
);
4405 IOCTL_NAME(WS_SIOCATMARK
);
4406 /* IOCTL_NAME(WS_SIO_ACQUIRE_PORT_RESERVATION); */
4407 IOCTL_NAME(WS_SIO_ADDRESS_LIST_CHANGE
);
4408 IOCTL_NAME(WS_SIO_ADDRESS_LIST_QUERY
);
4409 IOCTL_NAME(WS_SIO_ASSOCIATE_HANDLE
);
4410 /* IOCTL_NAME(WS_SIO_ASSOCIATE_PORT_RESERVATION);
4411 IOCTL_NAME(WS_SIO_BASE_HANDLE);
4412 IOCTL_NAME(WS_SIO_BSP_HANDLE);
4413 IOCTL_NAME(WS_SIO_BSP_HANDLE_SELECT);
4414 IOCTL_NAME(WS_SIO_BSP_HANDLE_POLL);
4415 IOCTL_NAME(WS_SIO_CHK_QOS); */
4416 IOCTL_NAME(WS_SIO_ENABLE_CIRCULAR_QUEUEING
);
4417 IOCTL_NAME(WS_SIO_FIND_ROUTE
);
4418 IOCTL_NAME(WS_SIO_FLUSH
);
4419 IOCTL_NAME(WS_SIO_GET_BROADCAST_ADDRESS
);
4420 IOCTL_NAME(WS_SIO_GET_EXTENSION_FUNCTION_POINTER
);
4421 IOCTL_NAME(WS_SIO_GET_GROUP_QOS
);
4422 IOCTL_NAME(WS_SIO_GET_INTERFACE_LIST
);
4423 /* IOCTL_NAME(WS_SIO_GET_INTERFACE_LIST_EX); */
4424 IOCTL_NAME(WS_SIO_GET_QOS
);
4425 /* IOCTL_NAME(WS_SIO_IDEAL_SEND_BACKLOG_CHANGE);
4426 IOCTL_NAME(WS_SIO_IDEAL_SEND_BACKLOG_QUERY); */
4427 IOCTL_NAME(WS_SIO_KEEPALIVE_VALS
);
4428 IOCTL_NAME(WS_SIO_MULTIPOINT_LOOPBACK
);
4429 IOCTL_NAME(WS_SIO_MULTICAST_SCOPE
);
4430 /* IOCTL_NAME(WS_SIO_QUERY_RSS_SCALABILITY_INFO);
4431 IOCTL_NAME(WS_SIO_QUERY_WFP_ALE_ENDPOINT_HANDLE); */
4432 IOCTL_NAME(WS_SIO_RCVALL
);
4433 IOCTL_NAME(WS_SIO_RCVALL_IGMPMCAST
);
4434 IOCTL_NAME(WS_SIO_RCVALL_MCAST
);
4435 /* IOCTL_NAME(WS_SIO_RELEASE_PORT_RESERVATION); */
4436 IOCTL_NAME(WS_SIO_ROUTING_INTERFACE_CHANGE
);
4437 IOCTL_NAME(WS_SIO_ROUTING_INTERFACE_QUERY
);
4438 IOCTL_NAME(WS_SIO_SET_COMPATIBILITY_MODE
);
4439 IOCTL_NAME(WS_SIO_SET_GROUP_QOS
);
4440 IOCTL_NAME(WS_SIO_SET_QOS
);
4441 IOCTL_NAME(WS_SIO_TRANSLATE_HANDLE
);
4442 IOCTL_NAME(WS_SIO_UDP_CONNRESET
);
4449 /* If this is not a known code split its bits */
4450 switch(code
& 0x18000000)
4455 case WS_IOC_PROTOCOL
:
4456 family
= "IOC_PROTOCOL";
4459 family
= "IOC_VENDOR";
4461 default: /* WS_IOC_UNIX */
4463 BYTE size
= (code
>> 16) & WS_IOCPARM_MASK
;
4464 char x
= (code
& 0xff00) >> 8;
4465 BYTE y
= code
& 0xff;
4468 switch (code
& (WS_IOC_VOID
|WS_IOC_INOUT
))
4472 sprintf(args
, "%d, %d", x
, y
);
4476 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
4480 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
4484 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
4487 return wine_dbg_sprintf("%s(%s)", buf_type
, args
);
4491 /* We are different from WS_IOC_UNIX. */
4492 switch (code
& (WS_IOC_VOID
|WS_IOC_INOUT
))
4495 buf_type
= "_WSAIO";
4498 buf_type
= "_WSAIORW";
4501 buf_type
= "_WSAIOW";
4504 buf_type
= "_WSAIOR";
4511 return wine_dbg_sprintf("%s(%s, %d)", buf_type
, family
,
4512 (USHORT
)(code
& 0xffff));
4515 /* do an ioctl call through the server */
4516 static DWORD
server_ioctl_sock( SOCKET s
, DWORD code
, LPVOID in_buff
, DWORD in_size
,
4517 LPVOID out_buff
, DWORD out_size
, LPDWORD ret_size
,
4518 LPWSAOVERLAPPED overlapped
,
4519 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
4521 HANDLE event
= overlapped
? overlapped
->hEvent
: 0;
4522 HANDLE handle
= SOCKET2HANDLE( s
);
4523 struct ws2_async
*wsa
;
4525 PIO_STATUS_BLOCK io
;
4527 if (!(wsa
= (struct ws2_async
*)alloc_async_io( sizeof(*wsa
) )))
4528 return WSA_NOT_ENOUGH_MEMORY
;
4529 wsa
->hSocket
= handle
;
4530 wsa
->user_overlapped
= overlapped
;
4531 wsa
->completion_func
= completion
;
4532 io
= (overlapped
? (PIO_STATUS_BLOCK
)overlapped
: &wsa
->local_iosb
);
4534 status
= NtDeviceIoControlFile( handle
, event
, ws2_async_apc
, wsa
, io
, code
,
4535 in_buff
, in_size
, out_buff
, out_size
);
4536 if (status
== STATUS_NOT_SUPPORTED
)
4538 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
4539 code
, code
>> 16, (code
>> 14) & 3, (code
>> 2) & 0xfff, code
& 3);
4541 else if (status
== STATUS_SUCCESS
)
4542 *ret_size
= io
->Information
; /* "Information" is the size written to the output buffer */
4544 if (status
!= STATUS_PENDING
) RtlFreeHeap( GetProcessHeap(), 0, wsa
);
4546 return NtStatusToWSAError( status
);
4549 /**********************************************************************
4550 * WSAIoctl (WS2_32.50)
4553 INT WINAPI
WSAIoctl(SOCKET s
, DWORD code
, LPVOID in_buff
, DWORD in_size
, LPVOID out_buff
,
4554 DWORD out_size
, LPDWORD ret_size
, LPWSAOVERLAPPED overlapped
,
4555 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
4558 DWORD status
= 0, total
= 0;
4560 TRACE("%04lx, %s, %p, %d, %p, %d, %p, %p, %p\n",
4561 s
, debugstr_wsaioctl(code
), in_buff
, in_size
, out_buff
, out_size
, ret_size
, overlapped
, completion
);
4566 if (in_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(in_buff
))
4568 SetLastError(WSAEFAULT
);
4569 return SOCKET_ERROR
;
4571 TRACE("-> FIONBIO (%x)\n", *(WS_u_long
*)in_buff
);
4572 if (_get_sock_mask(s
))
4574 /* AsyncSelect()'ed sockets are always nonblocking */
4575 if (!*(WS_u_long
*)in_buff
) status
= WSAEINVAL
;
4578 if (*(WS_u_long
*)in_buff
)
4579 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
4581 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
4586 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
4588 SetLastError(WSAEFAULT
);
4589 return SOCKET_ERROR
;
4591 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
4592 if (ioctl(fd
, FIONREAD
, out_buff
) == -1)
4593 status
= wsaErrno();
4594 release_sock_fd( s
, fd
);
4600 unsigned int oob
= 0, atmark
= 0;
4601 socklen_t oobsize
= sizeof(int);
4602 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
4604 SetLastError(WSAEFAULT
);
4605 return SOCKET_ERROR
;
4607 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
4608 /* SO_OOBINLINE sockets must always return TRUE to SIOCATMARK */
4609 if ((getsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &oob
, &oobsize
) == -1)
4610 || (!oob
&& ioctl(fd
, SIOCATMARK
, &atmark
) == -1))
4611 status
= wsaErrno();
4614 /* The SIOCATMARK value read from ioctl() is reversed
4615 * because BSD returns TRUE if it's in the OOB mark
4616 * while Windows returns TRUE if there are NO OOB bytes.
4618 (*(WS_u_long
*) out_buff
) = oob
|| !atmark
;
4621 release_sock_fd( s
, fd
);
4626 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
4627 SetLastError(WSAEINVAL
);
4628 return SOCKET_ERROR
;
4630 case WS_SIO_GET_INTERFACE_LIST
:
4632 INTERFACE_INFO
* intArray
= out_buff
;
4633 DWORD size
, numInt
= 0, apiReturn
;
4635 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
4637 if (!out_buff
|| !ret_size
)
4639 SetLastError(WSAEFAULT
);
4640 return SOCKET_ERROR
;
4643 fd
= get_sock_fd( s
, 0, NULL
);
4644 if (fd
== -1) return SOCKET_ERROR
;
4646 apiReturn
= GetAdaptersInfo(NULL
, &size
);
4647 if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
4649 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
4653 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
4655 PIP_ADAPTER_INFO ptr
;
4657 for (ptr
= table
, numInt
= 0; ptr
; ptr
= ptr
->Next
)
4659 unsigned int addr
, mask
, bcast
;
4660 struct ifreq ifInfo
;
4662 /* Skip interfaces without an IPv4 address. */
4663 if (ptr
->IpAddressList
.IpAddress
.String
[0] == '\0')
4666 if ((numInt
+ 1)*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > out_size
)
4668 WARN("Buffer too small = %u, out_size = %u\n", numInt
+ 1, out_size
);
4673 /* Socket Status Flags */
4674 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
4675 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
4677 ERR("Error obtaining status flags for socket!\n");
4683 /* set flags; the values of IFF_* are not the same
4684 under Linux and Windows, therefore must generate
4686 intArray
->iiFlags
= 0;
4687 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
4688 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
4689 #ifdef IFF_POINTOPOINT
4690 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
4691 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
4693 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
4694 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
4695 if (ifInfo
.ifr_flags
& IFF_UP
)
4696 intArray
->iiFlags
|= WS_IFF_UP
;
4697 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
4698 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
4701 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
4702 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
4703 bcast
= addr
| ~mask
;
4704 intArray
->iiAddress
.AddressIn
.sin_family
= WS_AF_INET
;
4705 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
4706 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
= addr
;
4708 intArray
->iiNetmask
.AddressIn
.sin_family
= WS_AF_INET
;
4709 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
4710 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
= mask
;
4712 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
= WS_AF_INET
;
4713 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
4714 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.WS_s_addr
= bcast
;
4721 ERR("Unable to get interface table!\n");
4724 HeapFree(GetProcessHeap(),0,table
);
4726 else status
= WSAEINVAL
;
4728 else if (apiReturn
!= ERROR_NO_DATA
)
4730 ERR("Unable to get interface table!\n");
4733 /* Calculate the size of the array being returned */
4734 total
= sizeof(INTERFACE_INFO
) * numInt
;
4735 release_sock_fd( s
, fd
);
4739 case WS_SIO_ADDRESS_LIST_QUERY
:
4743 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
4747 SetLastError(WSAEFAULT
);
4748 return SOCKET_ERROR
;
4751 if (out_size
&& out_size
< FIELD_OFFSET(SOCKET_ADDRESS_LIST
, Address
[0]))
4754 SetLastError(WSAEINVAL
);
4755 return SOCKET_ERROR
;
4758 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
4760 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
4761 SOCKET_ADDRESS_LIST
*sa_list
;
4762 SOCKADDR_IN
*sockaddr
;
4767 if (!table
|| GetAdaptersInfo(table
, &size
))
4769 HeapFree(GetProcessHeap(), 0, table
);
4774 for (p
= table
, num
= 0; p
; p
= p
->Next
)
4775 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
4777 total
= FIELD_OFFSET(SOCKET_ADDRESS_LIST
, Address
[num
]) + num
* sizeof(*sockaddr
);
4778 if (total
> out_size
|| !out_buff
)
4781 HeapFree(GetProcessHeap(), 0, table
);
4787 sa
= sa_list
->Address
;
4788 sockaddr
= (SOCKADDR_IN
*)&sa
[num
];
4789 sa_list
->iAddressCount
= num
;
4791 for (p
= table
, i
= 0; p
; p
= p
->Next
)
4793 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
4795 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
4796 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
4798 sockaddr
[i
].sin_family
= WS_AF_INET
;
4799 sockaddr
[i
].sin_port
= 0;
4800 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
4804 HeapFree(GetProcessHeap(), 0, table
);
4808 WARN("unable to get IP address list\n");
4815 FIXME("SIO_FLUSH: stub.\n");
4818 case WS_SIO_GET_EXTENSION_FUNCTION_POINTER
:
4820 static const GUID connectex_guid
= WSAID_CONNECTEX
;
4821 static const GUID disconnectex_guid
= WSAID_DISCONNECTEX
;
4822 static const GUID acceptex_guid
= WSAID_ACCEPTEX
;
4823 static const GUID getaccepexsockaddrs_guid
= WSAID_GETACCEPTEXSOCKADDRS
;
4824 static const GUID transmitfile_guid
= WSAID_TRANSMITFILE
;
4825 static const GUID transmitpackets_guid
= WSAID_TRANSMITPACKETS
;
4826 static const GUID wsarecvmsg_guid
= WSAID_WSARECVMSG
;
4827 static const GUID wsasendmsg_guid
= WSAID_WSASENDMSG
;
4829 if ( IsEqualGUID(&connectex_guid
, in_buff
) )
4831 *(LPFN_CONNECTEX
*)out_buff
= WS2_ConnectEx
;
4834 else if ( IsEqualGUID(&disconnectex_guid
, in_buff
) )
4836 *(LPFN_DISCONNECTEX
*)out_buff
= WS2_DisconnectEx
;
4839 else if ( IsEqualGUID(&acceptex_guid
, in_buff
) )
4841 *(LPFN_ACCEPTEX
*)out_buff
= WS2_AcceptEx
;
4844 else if ( IsEqualGUID(&getaccepexsockaddrs_guid
, in_buff
) )
4846 *(LPFN_GETACCEPTEXSOCKADDRS
*)out_buff
= WS2_GetAcceptExSockaddrs
;
4849 else if ( IsEqualGUID(&transmitfile_guid
, in_buff
) )
4851 *(LPFN_TRANSMITFILE
*)out_buff
= WS2_TransmitFile
;
4854 else if ( IsEqualGUID(&transmitpackets_guid
, in_buff
) )
4856 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitPackets\n");
4858 else if ( IsEqualGUID(&wsarecvmsg_guid
, in_buff
) )
4860 *(LPFN_WSARECVMSG
*)out_buff
= WS2_WSARecvMsg
;
4863 else if ( IsEqualGUID(&wsasendmsg_guid
, in_buff
) )
4865 *(LPFN_WSASENDMSG
*)out_buff
= WSASendMsg
;
4869 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER %s: stub\n", debugstr_guid(in_buff
));
4871 status
= WSAEOPNOTSUPP
;
4874 case WS_SIO_KEEPALIVE_VALS
:
4876 struct tcp_keepalive
*k
;
4877 int keepalive
, keepidle
, keepintvl
;
4879 if (!in_buff
|| in_size
< sizeof(struct tcp_keepalive
))
4881 SetLastError(WSAEFAULT
);
4882 return SOCKET_ERROR
;
4886 keepalive
= k
->onoff
? 1 : 0;
4887 keepidle
= max( 1, (k
->keepalivetime
+ 500) / 1000 );
4888 keepintvl
= max( 1, (k
->keepaliveinterval
+ 500) / 1000 );
4890 TRACE("onoff: %d, keepalivetime: %d, keepaliveinterval: %d\n", keepalive
, keepidle
, keepintvl
);
4892 fd
= get_sock_fd(s
, 0, NULL
);
4893 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&keepalive
, sizeof(int)) == -1)
4895 #if defined(TCP_KEEPIDLE) || defined(TCP_KEEPINTVL)
4896 /* these values need to be set only if SO_KEEPALIVE is enabled */
4899 #ifndef TCP_KEEPIDLE
4900 FIXME("ignoring keepalive timeout\n");
4902 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, (void *)&keepidle
, sizeof(int)) == -1)
4906 #ifdef TCP_KEEPINTVL
4907 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, (void *)&keepintvl
, sizeof(int)) == -1)
4910 FIXME("ignoring keepalive interval\n");
4915 FIXME("ignoring keepalive interval and timeout\n");
4917 release_sock_fd(s
, fd
);
4920 case WS_SIO_ROUTING_INTERFACE_QUERY
:
4922 struct WS_sockaddr
*daddr
= (struct WS_sockaddr
*)in_buff
;
4923 struct WS_sockaddr_in
*daddr_in
= (struct WS_sockaddr_in
*)daddr
;
4924 struct WS_sockaddr_in
*saddr_in
= out_buff
;
4925 MIB_IPFORWARDROW row
;
4926 PMIB_IPADDRTABLE ipAddrTable
= NULL
;
4927 DWORD size
, i
, found_index
;
4929 TRACE("-> WS_SIO_ROUTING_INTERFACE_QUERY request\n");
4931 if (!in_buff
|| in_size
< sizeof(struct WS_sockaddr
) ||
4932 !out_buff
|| out_size
< sizeof(struct WS_sockaddr_in
) || !ret_size
)
4934 SetLastError(WSAEFAULT
);
4935 return SOCKET_ERROR
;
4937 if (daddr
->sa_family
!= WS_AF_INET
)
4939 FIXME("unsupported address family %d\n", daddr
->sa_family
);
4940 status
= WSAEAFNOSUPPORT
;
4943 if (GetBestRoute(daddr_in
->sin_addr
.S_un
.S_addr
, 0, &row
) != NOERROR
||
4944 GetIpAddrTable(NULL
, &size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
4949 ipAddrTable
= HeapAlloc(GetProcessHeap(), 0, size
);
4950 if (GetIpAddrTable(ipAddrTable
, &size
, FALSE
))
4952 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
4956 for (i
= 0, found_index
= ipAddrTable
->dwNumEntries
;
4957 i
< ipAddrTable
->dwNumEntries
; i
++)
4959 if (ipAddrTable
->table
[i
].dwIndex
== row
.dwForwardIfIndex
)
4962 if (found_index
== ipAddrTable
->dwNumEntries
)
4964 ERR("no matching IP address for interface %d\n",
4965 row
.dwForwardIfIndex
);
4966 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
4970 saddr_in
->sin_family
= WS_AF_INET
;
4971 saddr_in
->sin_addr
.S_un
.S_addr
= ipAddrTable
->table
[found_index
].dwAddr
;
4972 saddr_in
->sin_port
= 0;
4973 total
= sizeof(struct WS_sockaddr_in
);
4974 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
4977 case WS_SIO_SET_COMPATIBILITY_MODE
:
4978 TRACE("WS_SIO_SET_COMPATIBILITY_MODE ignored\n");
4979 status
= WSAEOPNOTSUPP
;
4981 case WS_SIO_UDP_CONNRESET
:
4982 FIXME("WS_SIO_UDP_CONNRESET stub\n");
4984 case 0x667e: /* Netscape tries hard to use bogus ioctl 0x667e */
4985 SetLastError(WSAEOPNOTSUPP
);
4986 return SOCKET_ERROR
;
4988 status
= WSAEOPNOTSUPP
;
4992 if (status
== WSAEOPNOTSUPP
)
4994 status
= server_ioctl_sock(s
, code
, in_buff
, in_size
, out_buff
, out_size
, &total
,
4995 overlapped
, completion
);
4996 if (status
!= WSAEOPNOTSUPP
)
4998 if (status
== 0 || status
== WSA_IO_PENDING
)
4999 TRACE("-> %s request\n", debugstr_wsaioctl(code
));
5001 ERR("-> %s request failed with status 0x%x\n", debugstr_wsaioctl(code
), status
);
5003 /* overlapped and completion operations will be handled by the server */
5008 FIXME("unsupported WS_IOCTL cmd (%s)\n", debugstr_wsaioctl(code
));
5013 FIXME( "completion routine %p not supported\n", completion
);
5015 else if (overlapped
)
5017 ULONG_PTR cvalue
= (overlapped
&& ((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)overlapped
: 0;
5018 overlapped
->Internal
= status
;
5019 overlapped
->InternalHigh
= total
;
5020 if (overlapped
->hEvent
) NtSetEvent( overlapped
->hEvent
, NULL
);
5021 if (cvalue
) WS_AddCompletion( HANDLE2SOCKET(s
), cvalue
, status
, total
);
5026 if (ret_size
) *ret_size
= total
;
5029 SetLastError( status
);
5030 return SOCKET_ERROR
;
5034 /***********************************************************************
5035 * ioctlsocket (WS2_32.10)
5037 int WINAPI
WS_ioctlsocket(SOCKET s
, LONG cmd
, WS_u_long
*argp
)
5040 return WSAIoctl( s
, cmd
, argp
, sizeof(WS_u_long
), argp
, sizeof(WS_u_long
), &ret_size
, NULL
, NULL
);
5043 /***********************************************************************
5044 * listen (WS2_32.13)
5046 int WINAPI
WS_listen(SOCKET s
, int backlog
)
5048 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
), ret
= SOCKET_ERROR
;
5050 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
5053 int bound
= is_fd_bound(fd
, NULL
, NULL
);
5057 SetLastError(bound
== -1 ? wsaErrno() : WSAEINVAL
);
5059 else if (listen(fd
, backlog
) == 0)
5061 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
5063 FD_CONNECT
|FD_WINE_CONNECTED
);
5067 SetLastError(wsaErrno());
5068 release_sock_fd( s
, fd
);
5071 SetLastError(WSAENOTSOCK
);
5075 /***********************************************************************
5078 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
5080 DWORD n
, dwFlags
= flags
;
5086 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
5087 return SOCKET_ERROR
;
5092 /***********************************************************************
5093 * recvfrom (WS2_32.17)
5095 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
5096 struct WS_sockaddr
*from
, int *fromlen
)
5098 DWORD n
, dwFlags
= flags
;
5104 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
5105 return SOCKET_ERROR
;
5110 /* allocate a poll array for the corresponding fd sets */
5111 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
5112 const WS_fd_set
*exceptfds
, int *count_ptr
)
5114 unsigned int i
, j
= 0, count
= 0;
5117 if (readfds
) count
+= readfds
->fd_count
;
5118 if (writefds
) count
+= writefds
->fd_count
;
5119 if (exceptfds
) count
+= exceptfds
->fd_count
;
5123 SetLastError(WSAEINVAL
);
5126 if (!(fds
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(fds
[0]))))
5128 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
5132 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
5134 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
5135 if (fds
[j
].fd
== -1) goto failed
;
5137 if (is_fd_bound(fds
[j
].fd
, NULL
, NULL
) == 1)
5139 fds
[j
].events
= POLLIN
;
5143 release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
5149 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
5151 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
5152 if (fds
[j
].fd
== -1) goto failed
;
5154 if (is_fd_bound(fds
[j
].fd
, NULL
, NULL
) == 1 ||
5155 _get_fd_type(fds
[j
].fd
) == SOCK_DGRAM
)
5157 fds
[j
].events
= POLLOUT
;
5161 release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
5167 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
5169 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
5170 if (fds
[j
].fd
== -1) goto failed
;
5172 if (is_fd_bound(fds
[j
].fd
, NULL
, NULL
) == 1)
5174 int oob_inlined
= 0;
5175 socklen_t olen
= sizeof(oob_inlined
);
5177 fds
[j
].events
= POLLHUP
;
5179 /* Check if we need to test for urgent data or not */
5180 getsockopt(fds
[j
].fd
, SOL_SOCKET
, SO_OOBINLINE
, (char*) &oob_inlined
, &olen
);
5182 fds
[j
].events
|= POLLPRI
;
5186 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
5197 for (i
= 0; i
< readfds
->fd_count
&& j
< count
; i
++, j
++)
5198 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
5200 for (i
= 0; i
< writefds
->fd_count
&& j
< count
; i
++, j
++)
5201 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
5203 for (i
= 0; i
< exceptfds
->fd_count
&& j
< count
; i
++, j
++)
5204 if (fds
[j
].fd
!= -1) release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
5205 HeapFree( GetProcessHeap(), 0, fds
);
5209 /* release the file descriptor obtained in fd_sets_to_poll */
5210 /* must be called with the original fd_set arrays, before calling get_poll_results */
5211 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
5212 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
5214 unsigned int i
, j
= 0;
5218 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
5219 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
5223 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
5224 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
5228 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
5230 if (fds
[j
].fd
== -1) continue;
5231 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
5232 if (fds
[j
].revents
& POLLHUP
)
5234 int fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
5236 release_sock_fd( exceptfds
->fd_array
[i
], fd
);
5244 static int do_poll(struct pollfd
*pollfds
, int count
, int timeout
)
5246 struct timeval tv1
, tv2
;
5247 int ret
, torig
= timeout
;
5249 if (timeout
> 0) gettimeofday( &tv1
, 0 );
5251 while ((ret
= poll( pollfds
, count
, timeout
)) < 0)
5253 if (errno
!= EINTR
) break;
5254 if (timeout
< 0) continue;
5255 if (timeout
== 0) return 0;
5257 gettimeofday( &tv2
, 0 );
5259 tv2
.tv_sec
-= tv1
.tv_sec
;
5260 tv2
.tv_usec
-= tv1
.tv_usec
;
5261 if (tv2
.tv_usec
< 0)
5263 tv2
.tv_usec
+= 1000000;
5267 timeout
= torig
- (tv2
.tv_sec
* 1000) - (tv2
.tv_usec
+ 999) / 1000;
5268 if (timeout
<= 0) return 0;
5273 /* map the poll results back into the Windows fd sets */
5274 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
5275 const struct pollfd
*fds
)
5277 const struct pollfd
*poll_writefds
= fds
+ (readfds
? readfds
->fd_count
: 0);
5278 const struct pollfd
*poll_exceptfds
= poll_writefds
+ (writefds
? writefds
->fd_count
: 0);
5279 unsigned int i
, k
, total
= 0;
5283 for (i
= k
= 0; i
< readfds
->fd_count
; i
++)
5285 if (fds
[i
].revents
||
5286 (readfds
== writefds
&& (poll_writefds
[i
].revents
& POLLOUT
) && !(poll_writefds
[i
].revents
& POLLHUP
)) ||
5287 (readfds
== exceptfds
&& poll_exceptfds
[i
].revents
))
5288 readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
5290 readfds
->fd_count
= k
;
5293 if (writefds
&& writefds
!= readfds
)
5295 for (i
= k
= 0; i
< writefds
->fd_count
; i
++)
5297 if (((poll_writefds
[i
].revents
& POLLOUT
) && !(poll_writefds
[i
].revents
& POLLHUP
)) ||
5298 (writefds
== exceptfds
&& poll_exceptfds
[i
].revents
))
5299 writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
5301 writefds
->fd_count
= k
;
5304 if (exceptfds
&& exceptfds
!= readfds
&& exceptfds
!= writefds
)
5306 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++)
5307 if (poll_exceptfds
[i
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
5308 exceptfds
->fd_count
= k
;
5314 /***********************************************************************
5315 * select (WS2_32.18)
5317 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
5318 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
5319 const struct WS_timeval
* ws_timeout
)
5321 struct pollfd
*pollfds
;
5322 int count
, ret
, timeout
= -1;
5324 TRACE("read %p, write %p, excp %p timeout %p\n",
5325 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
5327 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)))
5328 return SOCKET_ERROR
;
5331 timeout
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
5333 ret
= do_poll(pollfds
, count
, timeout
);
5334 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
5336 if (ret
== -1) SetLastError(wsaErrno());
5337 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
5338 HeapFree( GetProcessHeap(), 0, pollfds
);
5342 /***********************************************************************
5345 int WINAPI
WSAPoll(WSAPOLLFD
*wfds
, ULONG count
, int timeout
)
5348 struct pollfd
*ufds
;
5352 SetLastError(WSAEINVAL
);
5353 return SOCKET_ERROR
;
5357 SetLastError(WSAEFAULT
);
5358 return SOCKET_ERROR
;
5361 if (!(ufds
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(ufds
[0]))))
5363 SetLastError(WSAENOBUFS
);
5364 return SOCKET_ERROR
;
5367 for (i
= 0; i
< count
; i
++)
5369 ufds
[i
].fd
= get_sock_fd(wfds
[i
].fd
, 0, NULL
);
5370 ufds
[i
].events
= convert_poll_w2u(wfds
[i
].events
);
5371 ufds
[i
].revents
= 0;
5374 ret
= do_poll(ufds
, count
, timeout
);
5376 for (i
= 0; i
< count
; i
++)
5378 if (ufds
[i
].fd
!= -1)
5380 release_sock_fd(wfds
[i
].fd
, ufds
[i
].fd
);
5381 if (ufds
[i
].revents
& POLLHUP
)
5383 /* Check if the socket still exists */
5384 int fd
= get_sock_fd(wfds
[i
].fd
, 0, NULL
);
5387 wfds
[i
].revents
= WS_POLLHUP
;
5388 release_sock_fd(wfds
[i
].fd
, fd
);
5391 wfds
[i
].revents
= WS_POLLNVAL
;
5394 wfds
[i
].revents
= convert_poll_u2w(ufds
[i
].revents
);
5397 wfds
[i
].revents
= WS_POLLNVAL
;
5400 HeapFree(GetProcessHeap(), 0, ufds
);
5404 /* helper to send completion messages for client-only i/o operation case */
5405 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
,
5408 SERVER_START_REQ( add_fd_completion
)
5410 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(sock
) );
5411 req
->cvalue
= CompletionValue
;
5412 req
->status
= CompletionStatus
;
5413 req
->information
= Information
;
5414 wine_server_call( req
);
5420 /***********************************************************************
5423 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
5429 wsabuf
.buf
= (char*) buf
;
5431 if ( WS2_sendto( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
5432 return SOCKET_ERROR
;
5437 /***********************************************************************
5438 * WSASend (WS2_32.72)
5440 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5441 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
5442 LPWSAOVERLAPPED lpOverlapped
,
5443 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5445 return WS2_sendto( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
5446 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
5449 /***********************************************************************
5450 * WSASendDisconnect (WS2_32.73)
5452 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
5454 return WS_shutdown( s
, SD_SEND
);
5458 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5459 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
5460 const struct WS_sockaddr
*to
, int tolen
,
5461 LPWSAOVERLAPPED lpOverlapped
,
5462 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5464 unsigned int i
, options
;
5465 int n
, fd
, err
, overlapped
, flags
;
5466 struct ws2_async
*wsa
= NULL
, localwsa
;
5467 int totalLength
= 0;
5471 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
5472 s
, lpBuffers
, dwBufferCount
, dwFlags
,
5473 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
5475 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
5476 TRACE( "fd=%d, options=%x\n", fd
, options
);
5478 if ( fd
== -1 ) return SOCKET_ERROR
;
5480 if (!lpOverlapped
&& !lpNumberOfBytesSent
)
5486 overlapped
= (lpOverlapped
|| lpCompletionRoutine
) &&
5487 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
5488 if (overlapped
|| dwBufferCount
> 1)
5490 if (!(wsa
= (struct ws2_async
*)alloc_async_io( offsetof(struct ws2_async
, iovec
[dwBufferCount
]))))
5499 wsa
->hSocket
= SOCKET2HANDLE(s
);
5500 wsa
->addr
= (struct WS_sockaddr
*)to
;
5501 wsa
->addrlen
.val
= tolen
;
5502 wsa
->flags
= dwFlags
;
5503 wsa
->lpFlags
= &wsa
->flags
;
5504 wsa
->control
= NULL
;
5505 wsa
->n_iovecs
= dwBufferCount
;
5506 wsa
->first_iovec
= 0;
5507 for ( i
= 0; i
< dwBufferCount
; i
++ )
5509 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
5510 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
5511 totalLength
+= lpBuffers
[i
].len
;
5514 flags
= convert_flags(dwFlags
);
5515 n
= WS2_send( fd
, wsa
, flags
);
5516 if (n
== -1 && errno
!= EAGAIN
)
5524 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
5525 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
5527 wsa
->user_overlapped
= lpOverlapped
;
5528 wsa
->completion_func
= lpCompletionRoutine
;
5529 release_sock_fd( s
, fd
);
5531 if (n
== -1 || n
< totalLength
)
5533 iosb
->u
.Status
= STATUS_PENDING
;
5534 iosb
->Information
= n
== -1 ? 0 : n
;
5536 SERVER_START_REQ( register_async
)
5538 req
->type
= ASYNC_TYPE_WRITE
;
5539 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
5540 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
5541 req
->async
.iosb
= wine_server_client_ptr( iosb
);
5542 req
->async
.arg
= wine_server_client_ptr( wsa
);
5543 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
5544 req
->async
.cvalue
= cvalue
;
5545 err
= wine_server_call( req
);
5549 /* Enable the event only after starting the async. The server will deliver it as soon as
5550 the async is done. */
5551 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
5553 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
5554 SetLastError(NtStatusToWSAError( err
));
5555 return SOCKET_ERROR
;
5558 iosb
->u
.Status
= STATUS_SUCCESS
;
5559 iosb
->Information
= n
;
5560 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= n
;
5561 if (!wsa
->completion_func
)
5563 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
5564 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
5565 HeapFree( GetProcessHeap(), 0, wsa
);
5567 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
5568 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
5569 SetLastError(ERROR_SUCCESS
);
5573 if ((err
= _is_blocking( s
, &is_blocking
)))
5575 err
= NtStatusToWSAError( err
);
5581 /* On a blocking non-overlapped stream socket,
5582 * sending blocks until the entire buffer is sent. */
5583 DWORD timeout_start
= GetTickCount();
5585 bytes_sent
= n
== -1 ? 0 : n
;
5587 while (wsa
->first_iovec
< wsa
->n_iovecs
)
5590 int poll_timeout
= -1;
5591 INT64 timeout
= get_rcvsnd_timeo(fd
, FALSE
);
5595 timeout
-= GetTickCount() - timeout_start
;
5596 if (timeout
< 0) poll_timeout
= 0;
5597 else poll_timeout
= timeout
<= INT_MAX
? timeout
: INT_MAX
;
5601 pfd
.events
= POLLOUT
;
5603 if (!poll_timeout
|| !poll( &pfd
, 1, poll_timeout
))
5606 goto error
; /* msdn says a timeout in send is fatal */
5609 n
= WS2_send( fd
, wsa
, flags
);
5610 if (n
== -1 && errno
!= EAGAIN
)
5620 else /* non-blocking */
5622 if (n
< totalLength
)
5623 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
5626 err
= WSAEWOULDBLOCK
;
5632 TRACE(" -> %i bytes\n", bytes_sent
);
5634 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= bytes_sent
;
5635 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
5636 release_sock_fd( s
, fd
);
5637 SetLastError(ERROR_SUCCESS
);
5641 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
5642 release_sock_fd( s
, fd
);
5643 WARN(" -> ERROR %d\n", err
);
5645 return SOCKET_ERROR
;
5648 /***********************************************************************
5649 * WSASendTo (WS2_32.74)
5651 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5652 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
5653 const struct WS_sockaddr
*to
, int tolen
,
5654 LPWSAOVERLAPPED lpOverlapped
,
5655 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5657 return WS2_sendto( s
, lpBuffers
, dwBufferCount
,
5658 lpNumberOfBytesSent
, dwFlags
,
5660 lpOverlapped
, lpCompletionRoutine
);
5663 /***********************************************************************
5664 * sendto (WS2_32.20)
5666 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
5667 const struct WS_sockaddr
*to
, int tolen
)
5673 wsabuf
.buf
= (char*) buf
;
5675 if ( WS2_sendto(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
5676 return SOCKET_ERROR
;
5681 /***********************************************************************
5682 * setsockopt (WS2_32.21)
5684 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
5685 const char *optval
, int optlen
)
5689 struct linger linger
;
5690 struct timeval tval
;
5692 TRACE("(socket %04lx, %s, optval %s, optlen %d)\n", s
,
5693 debugstr_sockopt(level
, optname
), debugstr_optval(optval
, optlen
),
5696 /* some broken apps pass the value directly instead of a pointer to it */
5697 if(optlen
&& IS_INTRESOURCE(optval
))
5699 SetLastError(WSAEFAULT
);
5700 return SOCKET_ERROR
;
5708 /* Some options need some conversion before they can be sent to
5709 * setsockopt. The conversions are done here, then they will fall through
5710 * to the general case. Special options that are not passed to
5711 * setsockopt follow below that.*/
5713 case WS_SO_DONTLINGER
:
5716 SetLastError(WSAEFAULT
);
5717 return SOCKET_ERROR
;
5719 linger
.l_onoff
= *(const int*)optval
== 0;
5720 linger
.l_linger
= 0;
5722 optname
= SO_LINGER
;
5723 optval
= (char*)&linger
;
5724 optlen
= sizeof(struct linger
);
5730 SetLastError(WSAEFAULT
);
5731 return SOCKET_ERROR
;
5733 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
5734 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
5736 optname
= SO_LINGER
;
5737 optval
= (char*)&linger
;
5738 optlen
= sizeof(struct linger
);
5742 if (*(const int*)optval
< 2048)
5744 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
5749 /* The options listed here don't need any special handling. Thanks to
5750 * the conversion happening above, options from there will fall through
5752 case WS_SO_ACCEPTCONN
:
5753 case WS_SO_BROADCAST
:
5755 case WS_SO_KEEPALIVE
:
5756 case WS_SO_OOBINLINE
:
5757 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
5758 * however, using it the BSD way fixes bug 8513 and seems to be what
5759 * most programmers assume, anyway */
5760 case WS_SO_REUSEADDR
:
5763 convert_sockopt(&level
, &optname
);
5766 /* SO_DEBUG is a privileged operation, ignore it. */
5768 TRACE("Ignoring SO_DEBUG\n");
5771 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
5772 * socket. According to MSDN, this option is silently ignored.*/
5773 case WS_SO_DONTROUTE
:
5774 TRACE("Ignoring SO_DONTROUTE\n");
5777 /* Stops two sockets from being bound to the same port. Always happens
5778 * on unix systems, so just drop it. */
5779 case WS_SO_EXCLUSIVEADDRUSE
:
5780 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
5783 /* After a ConnectEx call succeeds, the socket can't be used with half of the
5784 * normal winsock functions on windows. We don't have that problem. */
5785 case WS_SO_UPDATE_CONNECT_CONTEXT
:
5786 TRACE("Ignoring SO_UPDATE_CONNECT_CONTEXT, since our sockets are normal\n");
5789 /* After a AcceptEx call succeeds, the socket can't be used with half of the
5790 * normal winsock functions on windows. We don't have that problem. */
5791 case WS_SO_UPDATE_ACCEPT_CONTEXT
:
5792 TRACE("Ignoring SO_UPDATE_ACCEPT_CONTEXT, since our sockets are normal\n");
5795 /* SO_OPENTYPE does not require a valid socket handle. */
5796 case WS_SO_OPENTYPE
:
5797 if (!optlen
|| optlen
< sizeof(int) || !optval
)
5799 SetLastError(WSAEFAULT
);
5800 return SOCKET_ERROR
;
5802 get_per_thread_data()->opentype
= *(const int *)optval
;
5803 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((const int*)optval
) );
5807 case WS_SO_RCVTIMEO
:
5810 case WS_SO_SNDTIMEO
:
5812 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
5813 if (optval
&& optlen
== sizeof(UINT32
)) {
5814 /* WinSock passes milliseconds instead of struct timeval */
5815 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
5816 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
5817 /* min of 500 milliseconds */
5818 if (tval
.tv_sec
== 0 && tval
.tv_usec
&& tval
.tv_usec
< 500000)
5819 tval
.tv_usec
= 500000;
5820 optlen
= sizeof(struct timeval
);
5821 optval
= (char*)&tval
;
5822 } else if (optlen
== sizeof(struct timeval
)) {
5823 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
5825 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
5828 convert_sockopt(&level
, &optname
);
5833 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
5834 SetLastError(WSAENOPROTOOPT
);
5835 return SOCKET_ERROR
;
5837 break; /* case WS_SOL_SOCKET */
5840 case WS_NSPROTO_IPX
:
5844 return set_ipx_packettype(s
, *(int*)optval
);
5846 case WS_IPX_FILTERPTYPE
:
5847 /* Sets the receive filter packet type, at the moment we don't support it */
5848 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
5849 /* Returning 0 is better for now than returning a SOCKET_ERROR */
5853 FIXME("opt_name:%x\n", optname
);
5854 return SOCKET_ERROR
;
5856 break; /* case WS_NSPROTO_IPX */
5859 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
5860 case WS_IPPROTO_TCP
:
5863 case WS_TCP_NODELAY
:
5864 convert_sockopt(&level
, &optname
);
5867 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
5868 return SOCKET_ERROR
;
5875 case WS_IP_ADD_MEMBERSHIP
:
5876 case WS_IP_DROP_MEMBERSHIP
:
5880 case WS_IP_MULTICAST_IF
:
5881 case WS_IP_MULTICAST_LOOP
:
5882 case WS_IP_MULTICAST_TTL
:
5889 #ifdef IP_UNICAST_IF
5890 case WS_IP_UNICAST_IF
:
5892 convert_sockopt(&level
, &optname
);
5894 case WS_IP_DONTFRAGMENT
:
5895 FIXME("IP_DONTFRAGMENT is silently ignored!\n");
5898 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
5899 return SOCKET_ERROR
;
5903 case WS_IPPROTO_IPV6
:
5906 #ifdef IPV6_ADD_MEMBERSHIP
5907 case WS_IPV6_ADD_MEMBERSHIP
:
5909 #ifdef IPV6_DROP_MEMBERSHIP
5910 case WS_IPV6_DROP_MEMBERSHIP
:
5912 case WS_IPV6_MULTICAST_IF
:
5913 case WS_IPV6_MULTICAST_HOPS
:
5914 case WS_IPV6_MULTICAST_LOOP
:
5915 case WS_IPV6_UNICAST_HOPS
:
5916 case WS_IPV6_V6ONLY
:
5917 #ifdef IPV6_UNICAST_IF
5918 case WS_IPV6_UNICAST_IF
:
5920 convert_sockopt(&level
, &optname
);
5922 case WS_IPV6_DONTFRAG
:
5923 FIXME("IPV6_DONTFRAG is silently ignored!\n");
5925 case WS_IPV6_PROTECTION_LEVEL
:
5926 FIXME("IPV6_PROTECTION_LEVEL is ignored!\n");
5929 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
5930 return SOCKET_ERROR
;
5935 WARN("Unknown level: 0x%08x\n", level
);
5936 SetLastError(WSAEINVAL
);
5937 return SOCKET_ERROR
;
5938 } /* end switch(level) */
5940 /* avoid endianness issues if argument is a 16-bit int */
5941 if (optval
&& optlen
< sizeof(int))
5943 woptval
= *((const INT16
*) optval
);
5944 optval
= (char*) &woptval
;
5945 woptval
&= (1 << optlen
* 8) - 1;
5948 fd
= get_sock_fd( s
, 0, NULL
);
5949 if (fd
== -1) return SOCKET_ERROR
;
5951 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
5954 if (level
== SOL_SOCKET
&& optname
== SO_REUSEADDR
&&
5955 setsockopt(fd
, level
, SO_REUSEPORT
, optval
, optlen
) != 0)
5957 SetLastError(wsaErrno());
5958 release_sock_fd( s
, fd
);
5959 return SOCKET_ERROR
;
5962 release_sock_fd( s
, fd
);
5965 TRACE("Setting socket error, %d\n", wsaErrno());
5966 SetLastError(wsaErrno());
5967 release_sock_fd( s
, fd
);
5969 return SOCKET_ERROR
;
5972 /***********************************************************************
5973 * shutdown (WS2_32.22)
5975 int WINAPI
WS_shutdown(SOCKET s
, int how
)
5977 int fd
, err
= WSAENOTSOCK
;
5978 unsigned int options
= 0, clear_flags
= 0;
5980 fd
= get_sock_fd( s
, 0, &options
);
5981 TRACE("socket %04lx, how 0x%x, options 0x%x\n", s
, how
, options
);
5984 return SOCKET_ERROR
;
5988 case SD_RECEIVE
: /* drop receives */
5989 clear_flags
|= FD_READ
;
5991 case SD_SEND
: /* drop sends */
5992 clear_flags
|= FD_WRITE
;
5994 case SD_BOTH
: /* drop all */
5995 clear_flags
|= FD_READ
|FD_WRITE
;
5998 clear_flags
|= FD_WINE_LISTENING
;
6001 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
6006 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
6009 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
6013 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
6014 if (!err
) err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
6017 if (err
) goto error
;
6019 else /* non-overlapped mode */
6021 if ( shutdown( fd
, how
) )
6028 release_sock_fd( s
, fd
);
6029 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
6030 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
6034 release_sock_fd( s
, fd
);
6035 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
6036 SetLastError( err
);
6037 return SOCKET_ERROR
;
6040 /***********************************************************************
6041 * socket (WS2_32.23)
6043 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
6045 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
6047 return WSASocketW( af
, type
, protocol
, NULL
, 0,
6048 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
6052 /***********************************************************************
6053 * gethostbyaddr (WS2_32.51)
6055 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
6057 struct WS_hostent
*retval
= NULL
;
6058 struct hostent
* host
;
6059 int unixtype
= convert_af_w2u(type
);
6060 const char *paddr
= addr
;
6061 unsigned long loopback
;
6062 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6064 int ebufsize
= 1024;
6065 struct hostent hostentry
;
6066 int locerr
= ENOBUFS
;
6069 /* convert back the magic loopback address if necessary */
6070 if (unixtype
== AF_INET
&& len
== 4 && !memcmp(addr
, magic_loopback_addr
, 4))
6072 loopback
= htonl(INADDR_LOOPBACK
);
6073 paddr
= (char*) &loopback
;
6076 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6078 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
6080 int res
= gethostbyaddr_r(paddr
, len
, unixtype
,
6081 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
6082 if (res
!= ERANGE
) break;
6084 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
6086 if (host
) retval
= WS_dup_he(host
);
6087 else SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
6088 HeapFree(GetProcessHeap(),0,extrabuf
);
6090 EnterCriticalSection( &csWSgetXXXbyYYY
);
6091 host
= gethostbyaddr(paddr
, len
, unixtype
);
6092 if (host
) retval
= WS_dup_he(host
);
6093 else SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
6094 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6096 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
6100 /***********************************************************************
6101 * WS_compare_routes_by_metric_asc (INTERNAL)
6103 * Comparison function for qsort(), for sorting two routes (struct route)
6104 * by metric in ascending order.
6106 static int WS_compare_routes_by_metric_asc(const void *left
, const void *right
)
6108 const struct route
*a
= left
, *b
= right
;
6109 if (a
->default_route
&& b
->default_route
)
6110 return a
->default_route
- b
->default_route
;
6111 if (a
->default_route
&& !b
->default_route
)
6113 if (b
->default_route
&& !a
->default_route
)
6115 return a
->metric
- b
->metric
;
6118 /***********************************************************************
6119 * WS_get_local_ips (INTERNAL)
6121 * Returns the list of local IP addresses by going through the network
6122 * adapters and using the local routing table to sort the addresses
6123 * from highest routing priority to lowest routing priority. This
6124 * functionality is inferred from the description for obtaining local
6125 * IP addresses given in the Knowledge Base Article Q160215.
6127 * Please note that the returned hostent is only freed when the thread
6128 * closes and is replaced if another hostent is requested.
6130 static struct WS_hostent
* WS_get_local_ips( char *hostname
)
6132 int numroutes
= 0, i
, j
, default_routes
= 0;
6134 PIP_ADAPTER_INFO adapters
= NULL
, k
;
6135 struct WS_hostent
*hostlist
= NULL
;
6136 PMIB_IPFORWARDTABLE routes
= NULL
;
6137 struct route
*route_addrs
= NULL
;
6138 DWORD adap_size
, route_size
;
6140 /* Obtain the size of the adapter list and routing table, also allocate memory */
6141 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
6143 if (GetIpForwardTable(NULL
, &route_size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
6145 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
6146 routes
= HeapAlloc(GetProcessHeap(), 0, route_size
);
6147 route_addrs
= HeapAlloc(GetProcessHeap(), 0, 0); /* HeapReAlloc doesn't work on NULL */
6148 if (adapters
== NULL
|| routes
== NULL
|| route_addrs
== NULL
)
6150 /* Obtain the adapter list and the full routing table */
6151 if (GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
6153 if (GetIpForwardTable(routes
, &route_size
, FALSE
) != NO_ERROR
)
6155 /* Store the interface associated with each route */
6156 for (n
= 0; n
< routes
->dwNumEntries
; n
++)
6159 DWORD ifmetric
, ifdefault
= 0;
6160 BOOL exists
= FALSE
;
6162 /* Check if this is a default route (there may be more than one) */
6163 if (!routes
->table
[n
].dwForwardDest
)
6164 ifdefault
= ++default_routes
;
6165 else if (routes
->table
[n
].u1
.ForwardType
!= MIB_IPROUTE_TYPE_DIRECT
)
6167 ifindex
= routes
->table
[n
].dwForwardIfIndex
;
6168 ifmetric
= routes
->table
[n
].dwForwardMetric1
;
6169 /* Only store the lowest valued metric for an interface */
6170 for (j
= 0; j
< numroutes
; j
++)
6172 if (route_addrs
[j
].interface
== ifindex
)
6174 if (route_addrs
[j
].metric
> ifmetric
)
6175 route_addrs
[j
].metric
= ifmetric
;
6181 route_addrs
= HeapReAlloc(GetProcessHeap(), 0, route_addrs
, (numroutes
+1)*sizeof(struct route
));
6182 if (route_addrs
== NULL
)
6183 goto cleanup
; /* Memory allocation error, fail gracefully */
6184 route_addrs
[numroutes
].interface
= ifindex
;
6185 route_addrs
[numroutes
].metric
= ifmetric
;
6186 route_addrs
[numroutes
].default_route
= ifdefault
;
6187 /* If no IP is found in the next step (for whatever reason)
6188 * then fall back to the magic loopback address.
6190 memcpy(&(route_addrs
[numroutes
].addr
.s_addr
), magic_loopback_addr
, 4);
6194 goto cleanup
; /* No routes, fall back to the Magic IP */
6195 /* Find the IP address associated with each found interface */
6196 for (i
= 0; i
< numroutes
; i
++)
6198 for (k
= adapters
; k
!= NULL
; k
= k
->Next
)
6200 char *ip
= k
->IpAddressList
.IpAddress
.String
;
6202 if (route_addrs
[i
].interface
== k
->Index
)
6203 route_addrs
[i
].addr
.s_addr
= (in_addr_t
) inet_addr(ip
);
6206 /* Allocate a hostent and enough memory for all the IPs,
6207 * including the NULL at the end of the list.
6209 hostlist
= WS_create_he(hostname
, 1, 0, numroutes
+1, sizeof(struct in_addr
));
6210 if (hostlist
== NULL
)
6211 goto cleanup
; /* Failed to allocate a hostent for the list of IPs */
6212 hostlist
->h_addr_list
[numroutes
] = NULL
; /* NULL-terminate the address list */
6213 hostlist
->h_aliases
[0] = NULL
; /* NULL-terminate the alias list */
6214 hostlist
->h_addrtype
= AF_INET
;
6215 hostlist
->h_length
= sizeof(struct in_addr
); /* = 4 */
6216 /* Reorder the entries before placing them in the host list. Windows expects
6217 * the IP list in order from highest priority to lowest (the critical thing
6218 * is that most applications expect the first IP to be the default route).
6221 qsort(route_addrs
, numroutes
, sizeof(struct route
), WS_compare_routes_by_metric_asc
);
6223 for (i
= 0; i
< numroutes
; i
++)
6224 (*(struct in_addr
*) hostlist
->h_addr_list
[i
]) = route_addrs
[i
].addr
;
6226 /* Cleanup all allocated memory except the address list,
6227 * the address list is used by the calling app.
6230 HeapFree(GetProcessHeap(), 0, route_addrs
);
6231 HeapFree(GetProcessHeap(), 0, adapters
);
6232 HeapFree(GetProcessHeap(), 0, routes
);
6236 /***********************************************************************
6237 * gethostbyname (WS2_32.52)
6239 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
6241 struct WS_hostent
*retval
= NULL
;
6242 struct hostent
* host
;
6243 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6246 struct hostent hostentry
;
6247 int locerr
= ENOBUFS
;
6251 SetLastError(WSANOTINITIALISED
);
6254 if( gethostname( hostname
, 100) == -1) {
6255 SetLastError(WSAENOBUFS
); /* appropriate ? */
6258 if( !name
|| !name
[0]) {
6261 /* If the hostname of the local machine is requested then return the
6262 * complete list of local IP addresses */
6263 if(strcmp(name
, hostname
) == 0)
6264 retval
= WS_get_local_ips(hostname
);
6265 /* If any other hostname was requested (or the routing table lookup failed)
6266 * then return the IP found by the host OS */
6269 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6271 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
6273 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
6274 if( res
!= ERANGE
) break;
6276 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
6278 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
6280 EnterCriticalSection( &csWSgetXXXbyYYY
);
6281 host
= gethostbyname(name
);
6282 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
6284 if (host
) retval
= WS_dup_he(host
);
6285 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6286 HeapFree(GetProcessHeap(),0,extrabuf
);
6288 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6291 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
6292 strcmp(name
, "localhost") != 0)
6294 /* hostname != "localhost" but has loopback address. replace by our
6295 * special address.*/
6296 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
6298 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
6303 /***********************************************************************
6304 * getprotobyname (WS2_32.53)
6306 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
6308 struct WS_protoent
* retval
= NULL
;
6309 #ifdef HAVE_GETPROTOBYNAME
6310 struct protoent
* proto
;
6311 EnterCriticalSection( &csWSgetXXXbyYYY
);
6312 if( (proto
= getprotobyname(name
)) != NULL
)
6314 retval
= WS_dup_pe(proto
);
6317 MESSAGE("protocol %s not found; You might want to add "
6318 "this to /etc/protocols\n", debugstr_a(name
) );
6319 SetLastError(WSANO_DATA
);
6321 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6323 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
6328 /***********************************************************************
6329 * getprotobynumber (WS2_32.54)
6331 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
6333 struct WS_protoent
* retval
= NULL
;
6334 #ifdef HAVE_GETPROTOBYNUMBER
6335 struct protoent
* proto
;
6336 EnterCriticalSection( &csWSgetXXXbyYYY
);
6337 if( (proto
= getprotobynumber(number
)) != NULL
)
6339 retval
= WS_dup_pe(proto
);
6342 MESSAGE("protocol number %d not found; You might want to add "
6343 "this to /etc/protocols\n", number
);
6344 SetLastError(WSANO_DATA
);
6346 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6348 TRACE("%i ret %p\n", number
, retval
);
6353 /***********************************************************************
6354 * getservbyname (WS2_32.55)
6356 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
6358 struct WS_servent
* retval
= NULL
;
6359 struct servent
* serv
;
6361 char *proto_str
= NULL
;
6363 if (!(name_str
= strdup_lower(name
))) return NULL
;
6365 if (proto
&& *proto
)
6367 if (!(proto_str
= strdup_lower(proto
)))
6369 HeapFree( GetProcessHeap(), 0, name_str
);
6374 EnterCriticalSection( &csWSgetXXXbyYYY
);
6375 serv
= getservbyname(name_str
, proto_str
);
6378 retval
= WS_dup_se(serv
);
6380 else SetLastError(WSANO_DATA
);
6381 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6382 HeapFree( GetProcessHeap(), 0, proto_str
);
6383 HeapFree( GetProcessHeap(), 0, name_str
);
6384 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
6388 /***********************************************************************
6389 * freeaddrinfo (WS2_32.@)
6391 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
6394 struct WS_addrinfo
*next
;
6396 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
6397 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
6398 next
= res
->ai_next
;
6399 HeapFree(GetProcessHeap(),0,res
);
6404 /* helper functions for getaddrinfo()/getnameinfo() */
6405 static int convert_aiflag_w2u(int winflags
) {
6409 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
6410 if (ws_aiflag_map
[i
][0] & winflags
) {
6411 unixflags
|= ws_aiflag_map
[i
][1];
6412 winflags
&= ~ws_aiflag_map
[i
][0];
6415 FIXME("Unhandled windows AI_xxx flags %x\n", winflags
);
6419 static int convert_niflag_w2u(int winflags
) {
6423 for (i
=0;i
<sizeof(ws_niflag_map
)/sizeof(ws_niflag_map
[0]);i
++)
6424 if (ws_niflag_map
[i
][0] & winflags
) {
6425 unixflags
|= ws_niflag_map
[i
][1];
6426 winflags
&= ~ws_niflag_map
[i
][0];
6429 FIXME("Unhandled windows NI_xxx flags %x\n", winflags
);
6433 static int convert_aiflag_u2w(int unixflags
) {
6437 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
6438 if (ws_aiflag_map
[i
][1] & unixflags
) {
6439 winflags
|= ws_aiflag_map
[i
][0];
6440 unixflags
&= ~ws_aiflag_map
[i
][1];
6442 if (unixflags
) /* will warn usually */
6443 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags
);
6447 static int convert_eai_u2w(int unixret
) {
6450 if (!unixret
) return 0;
6452 for (i
=0;ws_eai_map
[i
][0];i
++)
6453 if (ws_eai_map
[i
][1] == unixret
)
6454 return ws_eai_map
[i
][0];
6456 if (unixret
== EAI_SYSTEM
)
6457 /* There are broken versions of glibc which return EAI_SYSTEM
6458 * and set errno to 0 instead of returning EAI_NONAME.
6460 return errno
? sock_get_error( errno
) : WS_EAI_NONAME
;
6462 FIXME("Unhandled unix EAI_xxx ret %d\n", unixret
);
6466 static char *get_hostname(void)
6471 GetComputerNameExA( ComputerNamePhysicalDnsHostname
, NULL
, &size
);
6472 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
6473 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, size
))) return NULL
;
6474 if (!GetComputerNameExA( ComputerNamePhysicalDnsHostname
, ret
, &size
))
6476 HeapFree( GetProcessHeap(), 0, ret
);
6482 /***********************************************************************
6483 * getaddrinfo (WS2_32.@)
6485 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
6487 #ifdef HAVE_GETADDRINFO
6488 struct addrinfo
*unixaires
= NULL
;
6490 struct addrinfo unixhints
, *punixhints
= NULL
;
6495 if (!nodename
&& !servname
)
6497 SetLastError(WSAHOST_NOT_FOUND
);
6498 return WSAHOST_NOT_FOUND
;
6501 hostname
= get_hostname();
6502 if (!hostname
) return WSA_NOT_ENOUGH_MEMORY
;
6506 else if (!nodename
[0])
6511 /* servname tweak required by OSX and BSD kernels */
6512 if (servname
&& !servname
[0]) servname
= "0";
6515 punixhints
= &unixhints
;
6517 memset(&unixhints
, 0, sizeof(unixhints
));
6518 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
6520 /* zero is a wildcard, no need to convert */
6521 if (hints
->ai_family
)
6522 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
6523 if (hints
->ai_socktype
)
6524 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
6525 if (hints
->ai_protocol
)
6526 punixhints
->ai_protocol
= max(convert_proto_w2u(hints
->ai_protocol
), 0);
6528 if (punixhints
->ai_socktype
< 0)
6530 SetLastError(WSAESOCKTNOSUPPORT
);
6531 HeapFree(GetProcessHeap(), 0, hostname
);
6532 return SOCKET_ERROR
;
6535 /* windows allows invalid combinations of socket type and protocol, unix does not.
6536 * fix the parameters here to make getaddrinfo call always work */
6537 if (punixhints
->ai_protocol
== IPPROTO_TCP
&&
6538 punixhints
->ai_socktype
!= SOCK_STREAM
&& punixhints
->ai_socktype
!= SOCK_SEQPACKET
)
6539 punixhints
->ai_socktype
= 0;
6541 else if (punixhints
->ai_protocol
== IPPROTO_UDP
&& punixhints
->ai_socktype
!= SOCK_DGRAM
)
6542 punixhints
->ai_socktype
= 0;
6544 else if (IS_IPX_PROTO(punixhints
->ai_protocol
) && punixhints
->ai_socktype
!= SOCK_DGRAM
)
6545 punixhints
->ai_socktype
= 0;
6547 else if (punixhints
->ai_protocol
== IPPROTO_IPV6
)
6548 punixhints
->ai_protocol
= 0;
6551 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
6552 result
= getaddrinfo(node
, servname
, punixhints
, &unixaires
);
6554 if (result
&& !strcmp(hostname
, node
))
6556 /* If it didn't work it means the host name IP is not in /etc/hosts, try again
6557 * by sending a NULL host and avoid sending a NULL servname too because that
6559 ERR_(winediag
)("Failed to resolve your host name IP\n");
6560 result
= getaddrinfo(NULL
, servname
? servname
: "0", punixhints
, &unixaires
);
6562 TRACE("%s, %s %p -> %p %d\n", debugstr_a(nodename
), debugstr_a(servname
), hints
, res
, result
);
6563 HeapFree(GetProcessHeap(), 0, hostname
);
6566 struct addrinfo
*xuai
= unixaires
;
6567 struct WS_addrinfo
**xai
= res
;
6571 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
6577 *xai
= ai
;xai
= &ai
->ai_next
;
6578 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
6579 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
6580 /* copy whatever was sent in the hints */
6582 ai
->ai_socktype
= hints
->ai_socktype
;
6583 ai
->ai_protocol
= hints
->ai_protocol
;
6585 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
6586 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
6588 if (xuai
->ai_canonname
) {
6589 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
6590 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
6591 if (!ai
->ai_canonname
)
6593 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
6595 len
= xuai
->ai_addrlen
;
6596 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
6599 ai
->ai_addrlen
= len
;
6601 int winlen
= ai
->ai_addrlen
;
6603 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
6604 ai
->ai_addrlen
= winlen
;
6608 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
6611 ai
->ai_addrlen
= len
;
6613 xuai
= xuai
->ai_next
;
6615 freeaddrinfo(unixaires
);
6617 if (TRACE_ON(winsock
))
6619 struct WS_addrinfo
*ai
= *res
;
6622 TRACE("=> %p, flags %#x, family %d, type %d, protocol %d, len %ld, name %s, addr %s\n",
6623 ai
, ai
->ai_flags
, ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
, ai
->ai_addrlen
,
6624 ai
->ai_canonname
, debugstr_sockaddr(ai
->ai_addr
));
6629 result
= convert_eai_u2w(result
);
6631 SetLastError(result
);
6635 if (*res
) WS_freeaddrinfo(*res
);
6636 if (unixaires
) freeaddrinfo(unixaires
);
6637 return WSA_NOT_ENOUGH_MEMORY
;
6639 FIXME("getaddrinfo() failed, not found during buildtime.\n");
6644 static struct WS_addrinfoW
*addrinfo_AtoW(const struct WS_addrinfo
*ai
)
6646 struct WS_addrinfoW
*ret
;
6648 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfoW
)))) return NULL
;
6649 ret
->ai_flags
= ai
->ai_flags
;
6650 ret
->ai_family
= ai
->ai_family
;
6651 ret
->ai_socktype
= ai
->ai_socktype
;
6652 ret
->ai_protocol
= ai
->ai_protocol
;
6653 ret
->ai_addrlen
= ai
->ai_addrlen
;
6654 ret
->ai_canonname
= NULL
;
6655 ret
->ai_addr
= NULL
;
6656 ret
->ai_next
= NULL
;
6657 if (ai
->ai_canonname
)
6659 int len
= MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0);
6660 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
))))
6662 HeapFree(GetProcessHeap(), 0, ret
);
6665 MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
);
6669 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, ai
->ai_addrlen
)))
6671 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
6672 HeapFree(GetProcessHeap(), 0, ret
);
6675 memcpy(ret
->ai_addr
, ai
->ai_addr
, ai
->ai_addrlen
);
6680 static struct WS_addrinfoW
*addrinfo_list_AtoW(const struct WS_addrinfo
*info
)
6682 struct WS_addrinfoW
*ret
, *infoW
;
6684 if (!(ret
= infoW
= addrinfo_AtoW(info
))) return NULL
;
6685 while (info
->ai_next
)
6687 if (!(infoW
->ai_next
= addrinfo_AtoW(info
->ai_next
)))
6692 infoW
= infoW
->ai_next
;
6693 info
= info
->ai_next
;
6698 static struct WS_addrinfo
*addrinfo_WtoA(const struct WS_addrinfoW
*ai
)
6700 struct WS_addrinfo
*ret
;
6702 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfo
)))) return NULL
;
6703 ret
->ai_flags
= ai
->ai_flags
;
6704 ret
->ai_family
= ai
->ai_family
;
6705 ret
->ai_socktype
= ai
->ai_socktype
;
6706 ret
->ai_protocol
= ai
->ai_protocol
;
6707 ret
->ai_addrlen
= ai
->ai_addrlen
;
6708 ret
->ai_canonname
= NULL
;
6709 ret
->ai_addr
= NULL
;
6710 ret
->ai_next
= NULL
;
6711 if (ai
->ai_canonname
)
6713 int len
= WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0, NULL
, NULL
);
6714 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
6716 HeapFree(GetProcessHeap(), 0, ret
);
6719 WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
, NULL
, NULL
);
6723 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
6725 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
6726 HeapFree(GetProcessHeap(), 0, ret
);
6729 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
6734 /***********************************************************************
6735 * GetAddrInfoExW (WS2_32.@)
6737 int WINAPI
GetAddrInfoExW(const WCHAR
*name
, const WCHAR
*servname
, DWORD
namespace, GUID
*namespace_id
,
6738 const ADDRINFOEXW
*hints
, ADDRINFOEXW
**result
, struct timeval
*timeout
, OVERLAPPED
*overlapped
,
6739 LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine
, HANDLE
*handle
)
6741 FIXME("(%s %s %x %s %p %p %p %p %p %p)\n", debugstr_w(name
), debugstr_w(servname
), namespace,
6742 debugstr_guid(namespace_id
), hints
, result
, timeout
, overlapped
, completion_routine
, handle
);
6743 return WSAHOST_NOT_FOUND
;
6746 /***********************************************************************
6747 * GetAddrInfoW (WS2_32.@)
6749 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
6752 char *nodenameA
= NULL
, *servnameA
= NULL
;
6753 struct WS_addrinfo
*resA
, *hintsA
= NULL
;
6758 len
= WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, NULL
, 0, NULL
, NULL
);
6759 if (!(nodenameA
= HeapAlloc(GetProcessHeap(), 0, len
))) return EAI_MEMORY
;
6760 WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, nodenameA
, len
, NULL
, NULL
);
6764 len
= WideCharToMultiByte(CP_ACP
, 0, servname
, -1, NULL
, 0, NULL
, NULL
);
6765 if (!(servnameA
= HeapAlloc(GetProcessHeap(), 0, len
)))
6767 HeapFree(GetProcessHeap(), 0, nodenameA
);
6770 WideCharToMultiByte(CP_ACP
, 0, servname
, -1, servnameA
, len
, NULL
, NULL
);
6773 if (hints
) hintsA
= addrinfo_WtoA(hints
);
6774 ret
= WS_getaddrinfo(nodenameA
, servnameA
, hintsA
, &resA
);
6775 WS_freeaddrinfo(hintsA
);
6779 *res
= addrinfo_list_AtoW(resA
);
6780 WS_freeaddrinfo(resA
);
6783 HeapFree(GetProcessHeap(), 0, nodenameA
);
6784 HeapFree(GetProcessHeap(), 0, servnameA
);
6788 /***********************************************************************
6789 * FreeAddrInfoW (WS2_32.@)
6791 void WINAPI
FreeAddrInfoW(PADDRINFOW ai
)
6796 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
6797 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
6799 HeapFree(GetProcessHeap(), 0, ai
);
6804 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
6805 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
6807 #ifdef HAVE_GETNAMEINFO
6809 union generic_unix_sockaddr sa_u
;
6812 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
6813 serv
, servlen
, flags
);
6815 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
6818 SetLastError(WSAEFAULT
);
6819 return WSA_NOT_ENOUGH_MEMORY
;
6821 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
6822 return convert_eai_u2w(ret
);
6824 FIXME("getnameinfo() failed, not found during buildtime.\n");
6829 int WINAPI
GetNameInfoW(const SOCKADDR
*sa
, WS_socklen_t salen
, PWCHAR host
,
6830 DWORD hostlen
, PWCHAR serv
, DWORD servlen
, INT flags
)
6833 char *hostA
= NULL
, *servA
= NULL
;
6835 if (host
&& (!(hostA
= HeapAlloc(GetProcessHeap(), 0, hostlen
)))) return EAI_MEMORY
;
6836 if (serv
&& (!(servA
= HeapAlloc(GetProcessHeap(), 0, servlen
))))
6838 HeapFree(GetProcessHeap(), 0, hostA
);
6842 ret
= WS_getnameinfo(sa
, salen
, hostA
, hostlen
, servA
, servlen
, flags
);
6845 if (host
) MultiByteToWideChar(CP_ACP
, 0, hostA
, -1, host
, hostlen
);
6846 if (serv
) MultiByteToWideChar(CP_ACP
, 0, servA
, -1, serv
, servlen
);
6849 HeapFree(GetProcessHeap(), 0, hostA
);
6850 HeapFree(GetProcessHeap(), 0, servA
);
6854 /***********************************************************************
6855 * getservbyport (WS2_32.56)
6857 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
6859 struct WS_servent
* retval
= NULL
;
6860 #ifdef HAVE_GETSERVBYPORT
6861 struct servent
* serv
;
6862 char *proto_str
= NULL
;
6864 if (proto
&& *proto
)
6866 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
6868 EnterCriticalSection( &csWSgetXXXbyYYY
);
6869 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
6870 retval
= WS_dup_se(serv
);
6872 else SetLastError(WSANO_DATA
);
6873 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6874 HeapFree( GetProcessHeap(), 0, proto_str
);
6876 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
6881 /***********************************************************************
6882 * gethostname (WS2_32.57)
6884 int WINAPI
WS_gethostname(char *name
, int namelen
)
6889 TRACE("name %p, len %d\n", name
, namelen
);
6893 SetLastError(WSAEFAULT
);
6894 return SOCKET_ERROR
;
6897 if (gethostname(buf
, sizeof(buf
)) != 0)
6899 SetLastError(wsaErrno());
6900 return SOCKET_ERROR
;
6903 TRACE("<- '%s'\n", buf
);
6906 WARN("Windows supports NetBIOS name length up to 15 bytes!\n");
6909 SetLastError(WSAEFAULT
);
6910 WARN("<- not enough space for hostname, required %d, got %d!\n", len
+ 1, namelen
);
6911 return SOCKET_ERROR
;
6918 /* ------------------------------------- Windows sockets extensions -- *
6920 * ------------------------------------------------------------------- */
6922 /***********************************************************************
6923 * WSAEnumNetworkEvents (WS2_32.36)
6925 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
6929 int errors
[FD_MAX_EVENTS
];
6931 TRACE("%04lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
6933 SERVER_START_REQ( get_socket_event
)
6935 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
6936 req
->service
= TRUE
;
6937 req
->c_event
= wine_server_obj_handle( hEvent
);
6938 wine_server_set_reply( req
, errors
, sizeof(errors
) );
6939 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
6944 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
6945 lpEvent
->iErrorCode
[i
] = NtStatusToWSAError(errors
[i
]);
6948 SetLastError(WSAEINVAL
);
6949 return SOCKET_ERROR
;
6952 /***********************************************************************
6953 * WSAEventSelect (WS2_32.39)
6955 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
6959 TRACE("%04lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
6961 SERVER_START_REQ( set_socket_event
)
6963 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
6965 req
->event
= wine_server_obj_handle( hEvent
);
6968 ret
= wine_server_call( req
);
6972 SetLastError(WSAEINVAL
);
6973 return SOCKET_ERROR
;
6976 /**********************************************************************
6977 * WSAGetOverlappedResult (WS2_32.40)
6979 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
6980 LPDWORD lpcbTransfer
, BOOL fWait
,
6985 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
6986 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
6988 if ( lpOverlapped
== NULL
)
6990 ERR( "Invalid pointer\n" );
6991 SetLastError(WSA_INVALID_PARAMETER
);
6995 status
= lpOverlapped
->Internal
;
6996 if (status
== STATUS_PENDING
)
7000 SetLastError( WSA_IO_INCOMPLETE
);
7004 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
7005 INFINITE
) == WAIT_FAILED
)
7007 status
= lpOverlapped
->Internal
;
7011 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
7014 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
7016 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
7021 /***********************************************************************
7022 * WSAAsyncSelect (WS2_32.101)
7024 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
7028 TRACE("%04lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
7030 SERVER_START_REQ( set_socket_event
)
7032 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
7035 req
->window
= wine_server_user_handle( hWnd
);
7037 ret
= wine_server_call( req
);
7041 SetLastError(WSAEINVAL
);
7042 return SOCKET_ERROR
;
7045 /***********************************************************************
7046 * WSACreateEvent (WS2_32.31)
7049 WSAEVENT WINAPI
WSACreateEvent(void)
7051 /* Create a manual-reset event, with initial state: unsignaled */
7054 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
7057 /***********************************************************************
7058 * WSACloseEvent (WS2_32.29)
7061 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
7063 TRACE ("event=%p\n", event
);
7065 return CloseHandle(event
);
7068 /***********************************************************************
7069 * WSASocketA (WS2_32.78)
7072 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
7073 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
7074 GROUP g
, DWORD dwFlags
)
7077 WSAPROTOCOL_INFOW info
;
7079 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
7080 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
7082 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
7084 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
7085 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
7086 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
7090 SetLastError(WSAEINVAL
);
7091 return SOCKET_ERROR
;
7094 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
7097 /***********************************************************************
7098 * WSASocketW (WS2_32.79)
7101 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
7102 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
7103 GROUP g
, DWORD dwFlags
)
7107 int unixaf
, unixtype
, ipxptype
= -1;
7110 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
7111 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
7114 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
7115 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
7119 err
= WSANOTINITIALISED
;
7123 /* hack for WSADuplicateSocket */
7124 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
7125 ret
= lpProtocolInfo
->dwServiceFlags3
;
7126 TRACE("\tgot duplicate %04lx\n", ret
);
7132 if (af
== FROM_PROTOCOL_INFO
|| !af
)
7133 af
= lpProtocolInfo
->iAddressFamily
;
7134 if (type
== FROM_PROTOCOL_INFO
|| !type
)
7135 type
= lpProtocolInfo
->iSocketType
;
7136 if (protocol
== FROM_PROTOCOL_INFO
|| !protocol
)
7137 protocol
= lpProtocolInfo
->iProtocol
;
7140 if (!type
&& (af
|| protocol
))
7142 int autoproto
= protocol
;
7143 WSAPROTOCOL_INFOW infow
;
7145 /* default to the first valid protocol */
7147 autoproto
= valid_protocols
[0];
7148 else if(IS_IPX_PROTO(autoproto
))
7149 autoproto
= WS_NSPROTO_IPX
;
7151 if (WS_EnterSingleProtocolW(autoproto
, &infow
))
7153 type
= infow
.iSocketType
;
7155 /* after win2003 it's no longer possible to pass AF_UNSPEC
7156 using the protocol info struct */
7157 if (!lpProtocolInfo
&& af
== WS_AF_UNSPEC
)
7158 af
= infow
.iAddressFamily
;
7163 Windows has an extension to the IPX protocol that allows one to create sockets
7164 and set the IPX packet type at the same time, to do that a caller will use
7165 a protocol like NSPROTO_IPX + <PACKET TYPE>
7167 if (IS_IPX_PROTO(protocol
))
7168 ipxptype
= protocol
- WS_NSPROTO_IPX
;
7170 /* convert the socket family, type and protocol */
7171 unixaf
= convert_af_w2u(af
);
7172 unixtype
= convert_socktype_w2u(type
);
7173 protocol
= convert_proto_w2u(protocol
);
7174 if (unixaf
== AF_UNSPEC
) unixaf
= -1;
7176 /* filter invalid parameters */
7179 /* the type could not be converted */
7180 if (type
&& unixtype
< 0)
7182 err
= WSAESOCKTNOSUPPORT
;
7186 err
= WSAEPROTONOSUPPORT
;
7191 /* both family and protocol can't be invalid */
7198 /* family could not be converted and neither socket type */
7199 if (unixtype
< 0 && af
>= 0)
7202 err
= WSAESOCKTNOSUPPORT
;
7206 err
= WSAEAFNOSUPPORT
;
7210 SERVER_START_REQ( create_socket
)
7212 req
->family
= unixaf
;
7213 req
->type
= unixtype
;
7214 req
->protocol
= protocol
;
7215 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
7216 req
->attributes
= (dwFlags
& WSA_FLAG_NO_HANDLE_INHERIT
) ? 0 : OBJ_INHERIT
;
7217 req
->flags
= dwFlags
& ~WSA_FLAG_NO_HANDLE_INHERIT
;
7218 set_error( wine_server_call( req
) );
7219 ret
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
7224 TRACE("\tcreated %04lx\n", ret
);
7226 set_ipx_packettype(ret
, ipxptype
);
7230 err
= GetLastError();
7231 if (err
== WSAEACCES
) /* raw socket denied */
7233 if (type
== SOCK_RAW
)
7234 ERR_(winediag
)("Failed to create a socket of type SOCK_RAW, this requires special permissions.\n");
7236 ERR_(winediag
)("Failed to create socket, this requires special permissions.\n");
7240 /* invalid combination of valid parameters, like SOCK_STREAM + IPPROTO_UDP */
7241 if (err
== WSAEINVAL
)
7242 err
= WSAESOCKTNOSUPPORT
;
7243 else if (err
== WSAEOPNOTSUPP
)
7244 err
= WSAEPROTONOSUPPORT
;
7248 WARN("\t\tfailed, error %d!\n", err
);
7250 return INVALID_SOCKET
;
7253 /***********************************************************************
7254 * WSAJoinLeaf (WS2_32.58)
7257 SOCKET WINAPI
WSAJoinLeaf(
7259 const struct WS_sockaddr
*addr
,
7261 LPWSABUF lpCallerData
,
7262 LPWSABUF lpCalleeData
,
7268 return INVALID_SOCKET
;
7271 /***********************************************************************
7272 * __WSAFDIsSet (WS2_32.151)
7274 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
7276 int i
= set
->fd_count
, ret
= 0;
7279 if (set
->fd_array
[i
] == s
)
7285 TRACE("(socket %04lx, fd_set %p, count %i) <- %d\n", s
, set
, set
->fd_count
, ret
);
7289 /***********************************************************************
7290 * WSAIsBlocking (WS2_32.114)
7292 BOOL WINAPI
WSAIsBlocking(void)
7294 /* By default WinSock should set all its sockets to non-blocking mode
7295 * and poll in PeekMessage loop when processing "blocking" ones. This
7296 * function is supposed to tell if the program is in this loop. Our
7297 * blocking calls are truly blocking so we always return FALSE.
7299 * Note: It is allowed to call this function without prior WSAStartup().
7306 /***********************************************************************
7307 * WSACancelBlockingCall (WS2_32.113)
7309 INT WINAPI
WSACancelBlockingCall(void)
7315 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
7317 FIXME("How was this called?\n");
7322 /***********************************************************************
7323 * WSASetBlockingHook (WS2_32.109)
7325 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
7327 FARPROC prev
= blocking_hook
;
7328 blocking_hook
= lpBlockFunc
;
7329 TRACE("hook %p\n", lpBlockFunc
);
7334 /***********************************************************************
7335 * WSAUnhookBlockingHook (WS2_32.110)
7337 INT WINAPI
WSAUnhookBlockingHook(void)
7339 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
7344 /* ----------------------------------- end of API stuff */
7346 /* ----------------------------------- helper functions -
7348 * TODO: Merge WS_dup_..() stuff into one function that
7349 * would operate with a generic structure containing internal
7350 * pointers (via a template of some kind).
7353 static int list_size(char** l
, int item_size
)
7358 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
7359 j
+= (i
+ 1) * sizeof(char*); }
7363 static int list_dup(char** l_src
, char** l_to
, int item_size
)
7368 for (i
= 0; l_src
[i
]; i
++) ;
7369 p
= (char *)(l_to
+ i
+ 1);
7370 for (i
= 0; l_src
[i
]; i
++)
7372 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
7373 memcpy(p
, l_src
[i
], count
);
7378 return p
- (char *)l_to
;
7383 /* create a hostent entry
7385 * Creates the entry with enough memory for the name, aliases
7386 * addresses, and the address pointers. Also copies the name
7387 * and sets up all the pointers.
7389 * NOTE: The alias and address lists must be allocated with room
7390 * for the NULL item terminating the list. This is true even if
7391 * the list has no items ("aliases" and "addresses" must be
7392 * at least "1", a truly empty list is invalid).
7394 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int aliases_size
, int addresses
, int address_length
)
7396 struct WS_hostent
*p_to
;
7398 int size
= (sizeof(struct WS_hostent
) +
7400 sizeof(char *) * aliases
+
7402 sizeof(char *) * addresses
+
7403 address_length
* (addresses
- 1)), i
;
7405 if (!(p_to
= check_buffer_he(size
))) return NULL
;
7406 memset(p_to
, 0, size
);
7408 /* Use the memory in the same way winsock does.
7409 * First set the pointer for aliases, second set the pointers for addresses.
7410 * Third fill the addresses indexes, fourth jump aliases names size.
7411 * Fifth fill the hostname.
7412 * NOTE: This method is valid for OS version's >= XP.
7414 p
= (char *)(p_to
+ 1);
7415 p_to
->h_aliases
= (char **)p
;
7416 p
+= sizeof(char *)*aliases
;
7418 p_to
->h_addr_list
= (char **)p
;
7419 p
+= sizeof(char *)*addresses
;
7421 for (i
= 0, addresses
--; i
< addresses
; i
++, p
+= address_length
)
7422 p_to
->h_addr_list
[i
] = p
;
7424 /* NOTE: h_aliases must be filled in manually because we don't know each string
7425 * size, leave these pointers NULL (already set to NULL by memset earlier).
7435 /* duplicate hostent entry
7436 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
7437 * Ditto for protoent and servent.
7439 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
7441 int i
, addresses
= 0, alias_size
= 0;
7442 struct WS_hostent
*p_to
;
7445 for( i
= 0; p_he
->h_aliases
[i
]; i
++) alias_size
+= strlen(p_he
->h_aliases
[i
]) + 1;
7446 while (p_he
->h_addr_list
[addresses
]) addresses
++;
7448 p_to
= WS_create_he(p_he
->h_name
, i
+ 1, alias_size
, addresses
+ 1, p_he
->h_length
);
7450 if (!p_to
) return NULL
;
7451 p_to
->h_addrtype
= convert_af_u2w(p_he
->h_addrtype
);
7452 p_to
->h_length
= p_he
->h_length
;
7454 for(i
= 0, p
= p_to
->h_addr_list
[0]; p_he
->h_addr_list
[i
]; i
++, p
+= p_to
->h_length
)
7455 memcpy(p
, p_he
->h_addr_list
[i
], p_to
->h_length
);
7457 /* Fill the aliases after the IP data */
7458 for(i
= 0; p_he
->h_aliases
[i
]; i
++)
7460 p_to
->h_aliases
[i
] = p
;
7461 strcpy(p
, p_he
->h_aliases
[i
]);
7468 /* ----- protoent */
7470 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
)
7473 struct WS_protoent
*p_to
;
7475 int size
= (sizeof(*p_pe
) +
7476 strlen(p_pe
->p_name
) + 1 +
7477 list_size(p_pe
->p_aliases
, 0));
7479 if (!(p_to
= check_buffer_pe(size
))) return NULL
;
7480 p_to
->p_proto
= p_pe
->p_proto
;
7482 p
= (char *)(p_to
+ 1);
7484 strcpy(p
, p_pe
->p_name
);
7487 p_to
->p_aliases
= (char **)p
;
7488 list_dup(p_pe
->p_aliases
, p_to
->p_aliases
, 0);
7494 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
7497 struct WS_servent
*p_to
;
7499 int size
= (sizeof(*p_se
) +
7500 strlen(p_se
->s_proto
) + 1 +
7501 strlen(p_se
->s_name
) + 1 +
7502 list_size(p_se
->s_aliases
, 0));
7504 if (!(p_to
= check_buffer_se(size
))) return NULL
;
7505 p_to
->s_port
= p_se
->s_port
;
7507 p
= (char *)(p_to
+ 1);
7509 strcpy(p
, p_se
->s_name
);
7513 strcpy(p
, p_se
->s_proto
);
7516 p_to
->s_aliases
= (char **)p
;
7517 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
7522 /***********************************************************************
7523 * WSARecv (WS2_32.67)
7525 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
7526 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
7527 LPWSAOVERLAPPED lpOverlapped
,
7528 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
7530 return WS2_recv_base(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
7531 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
, NULL
);
7534 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
7535 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
7536 struct WS_sockaddr
*lpFrom
,
7537 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
7538 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
7539 LPWSABUF lpControlBuffer
)
7541 unsigned int i
, options
;
7542 int n
, fd
, err
, overlapped
, flags
;
7543 struct ws2_async
*wsa
= NULL
, localwsa
;
7545 DWORD timeout_start
= GetTickCount();
7546 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
7548 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
7549 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
7550 (lpFromlen
? *lpFromlen
: -1),
7551 lpOverlapped
, lpCompletionRoutine
);
7553 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
7554 TRACE( "fd=%d, options=%x\n", fd
, options
);
7556 if (fd
== -1) return SOCKET_ERROR
;
7558 if (*lpFlags
& WS_MSG_OOB
)
7560 /* It's invalid to receive OOB data from an OOBINLINED socket
7561 * as OOB data is turned into normal data. */
7563 if (!getsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, (char*) &n
, &i
) && n
)
7570 overlapped
= (lpOverlapped
|| lpCompletionRoutine
) &&
7571 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
7572 if (overlapped
|| dwBufferCount
> 1)
7574 if (!(wsa
= (struct ws2_async
*)alloc_async_io( offsetof(struct ws2_async
, iovec
[dwBufferCount
]))))
7583 wsa
->hSocket
= SOCKET2HANDLE(s
);
7584 wsa
->flags
= *lpFlags
;
7585 wsa
->lpFlags
= lpFlags
;
7587 wsa
->addrlen
.ptr
= lpFromlen
;
7588 wsa
->control
= lpControlBuffer
;
7589 wsa
->n_iovecs
= dwBufferCount
;
7590 wsa
->first_iovec
= 0;
7591 for (i
= 0; i
< dwBufferCount
; i
++)
7593 /* check buffer first to trigger write watches */
7594 if (IsBadWritePtr( lpBuffers
[i
].buf
, lpBuffers
[i
].len
))
7599 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
7600 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
7603 flags
= convert_flags(wsa
->flags
);
7606 n
= WS2_recv( fd
, wsa
, flags
);
7609 /* Unix-like systems return EINVAL when attempting to read OOB data from
7610 * an empty socket buffer, convert that to a Windows expected return. */
7611 if ((flags
& MSG_OOB
) && errno
== EINVAL
)
7612 errno
= EWOULDBLOCK
;
7614 if (errno
!= EAGAIN
)
7616 int loc_errno
= errno
;
7618 if (cvalue
) WS_AddCompletion( s
, cvalue
, sock_get_ntstatus(loc_errno
), 0 );
7622 else if (lpNumberOfBytesRecvd
) *lpNumberOfBytesRecvd
= n
;
7626 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
7628 wsa
->user_overlapped
= lpOverlapped
;
7629 wsa
->completion_func
= lpCompletionRoutine
;
7630 release_sock_fd( s
, fd
);
7634 iosb
->u
.Status
= STATUS_PENDING
;
7635 iosb
->Information
= 0;
7637 SERVER_START_REQ( register_async
)
7639 req
->type
= ASYNC_TYPE_READ
;
7640 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
7641 req
->async
.callback
= wine_server_client_ptr( WS2_async_recv
);
7642 req
->async
.iosb
= wine_server_client_ptr( iosb
);
7643 req
->async
.arg
= wine_server_client_ptr( wsa
);
7644 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
7645 req
->async
.cvalue
= cvalue
;
7646 err
= wine_server_call( req
);
7650 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
7651 SetLastError(NtStatusToWSAError( err
));
7652 return SOCKET_ERROR
;
7655 iosb
->u
.Status
= STATUS_SUCCESS
;
7656 iosb
->Information
= n
;
7657 if (!wsa
->completion_func
)
7659 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
7660 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
7661 HeapFree( GetProcessHeap(), 0, wsa
);
7663 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
7664 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
7665 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
7671 if ((err
= _is_blocking( s
, &is_blocking
)))
7673 err
= NtStatusToWSAError( err
);
7680 int poll_timeout
= -1;
7681 INT64 timeout
= get_rcvsnd_timeo(fd
, TRUE
);
7685 timeout
-= GetTickCount() - timeout_start
;
7686 if (timeout
< 0) poll_timeout
= 0;
7687 else poll_timeout
= timeout
<= INT_MAX
? timeout
: INT_MAX
;
7691 pfd
.events
= POLLIN
;
7692 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
7694 if (!poll_timeout
|| !poll( &pfd
, 1, poll_timeout
))
7697 /* a timeout is not fatal */
7698 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
7704 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
7705 err
= WSAEWOULDBLOCK
;
7710 TRACE(" -> %i bytes\n", n
);
7711 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
7712 release_sock_fd( s
, fd
);
7713 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
7714 SetLastError(ERROR_SUCCESS
);
7719 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
7720 release_sock_fd( s
, fd
);
7721 WARN(" -> ERROR %d\n", err
);
7722 SetLastError( err
);
7723 return SOCKET_ERROR
;
7726 /***********************************************************************
7727 * WSARecvFrom (WS2_32.69)
7729 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
7730 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
7731 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
7732 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
7735 return WS2_recv_base( s
, lpBuffers
, dwBufferCount
,
7736 lpNumberOfBytesRecvd
, lpFlags
,
7738 lpOverlapped
, lpCompletionRoutine
, NULL
);
7741 /***********************************************************************
7742 * WSCInstallProvider (WS2_32.88)
7744 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
7745 LPCWSTR lpszProviderDllPath
,
7746 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
7747 DWORD dwNumberOfEntries
,
7750 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
7751 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
7752 dwNumberOfEntries
, lpErrno
);
7758 /***********************************************************************
7759 * WSCDeinstallProvider (WS2_32.83)
7761 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
7763 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
7769 /***********************************************************************
7770 * WSAAccept (WS2_32.26)
7772 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
7773 LPCONDITIONPROC lpfnCondition
, DWORD_PTR dwCallbackData
)
7777 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
7778 /* QOS SQOS, GQOS; */
7781 SOCKADDR src_addr
, dst_addr
;
7783 TRACE("socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %ld\n",
7784 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
7786 cs
= WS_accept(s
, addr
, addrlen
);
7787 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
7788 if (!lpfnCondition
) return cs
;
7790 if (addr
&& addrlen
)
7792 CallerId
.buf
= (char *)addr
;
7793 CallerId
.len
= *addrlen
;
7797 size
= sizeof(src_addr
);
7798 WS_getpeername(cs
, &src_addr
, &size
);
7799 CallerId
.buf
= (char *)&src_addr
;
7800 CallerId
.len
= size
;
7802 CallerData
.buf
= NULL
;
7805 size
= sizeof(dst_addr
);
7806 WS_getsockname(cs
, &dst_addr
, &size
);
7808 CalleeId
.buf
= (char *)&dst_addr
;
7809 CalleeId
.len
= sizeof(dst_addr
);
7811 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
7812 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
7819 SERVER_START_REQ( set_socket_deferred
)
7821 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
7822 req
->deferred
= wine_server_obj_handle( SOCKET2HANDLE(cs
) );
7823 if ( !wine_server_call_err ( req
) )
7825 SetLastError( WSATRY_AGAIN
);
7826 WS_closesocket( cs
);
7830 return SOCKET_ERROR
;
7833 SetLastError(WSAECONNREFUSED
);
7834 return SOCKET_ERROR
;
7836 FIXME("Unknown return type from Condition function\n");
7837 SetLastError(WSAENOTSOCK
);
7838 return SOCKET_ERROR
;
7842 /***********************************************************************
7843 * WSADuplicateSocketA (WS2_32.32)
7845 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
7847 return WS_DuplicateSocket(FALSE
, s
, dwProcessId
, (LPWSAPROTOCOL_INFOW
) lpProtocolInfo
);
7850 /***********************************************************************
7851 * WSADuplicateSocketW (WS2_32.33)
7853 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
7855 return WS_DuplicateSocket(TRUE
, s
, dwProcessId
, lpProtocolInfo
);
7858 /***********************************************************************
7859 * WSAInstallServiceClassA (WS2_32.48)
7861 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
7863 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
7864 SetLastError(WSAEACCES
);
7865 return SOCKET_ERROR
;
7868 /***********************************************************************
7869 * WSAInstallServiceClassW (WS2_32.49)
7871 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
7873 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
7874 SetLastError(WSAEACCES
);
7875 return SOCKET_ERROR
;
7878 /***********************************************************************
7879 * WSARemoveServiceClass (WS2_32.70)
7881 int WINAPI
WSARemoveServiceClass(LPGUID info
)
7883 FIXME("Request to remove service %p\n",info
);
7884 SetLastError(WSATYPE_NOT_FOUND
);
7885 return SOCKET_ERROR
;
7888 /***********************************************************************
7889 * inet_ntop (WS2_32.@)
7891 PCSTR WINAPI
WS_inet_ntop( INT family
, PVOID addr
, PSTR buffer
, SIZE_T len
)
7893 #ifdef HAVE_INET_NTOP
7894 struct WS_in6_addr
*in6
;
7895 struct WS_in_addr
*in
;
7898 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
7901 SetLastError( STATUS_INVALID_PARAMETER
);
7910 pdst
= inet_ntop( AF_INET
, &in
->WS_s_addr
, buffer
, len
);
7916 pdst
= inet_ntop( AF_INET6
, in6
->WS_s6_addr
, buffer
, len
);
7920 SetLastError( WSAEAFNOSUPPORT
);
7924 if (!pdst
) SetLastError( STATUS_INVALID_PARAMETER
);
7927 FIXME( "not supported on this platform\n" );
7928 SetLastError( WSAEAFNOSUPPORT
);
7933 /***********************************************************************
7934 * inet_pton (WS2_32.@)
7936 INT WINAPI
WS_inet_pton( INT family
, PCSTR addr
, PVOID buffer
)
7938 #ifdef HAVE_INET_PTON
7941 TRACE("family %d, addr %s, buffer (%p)\n", family
, debugstr_a(addr
), buffer
);
7943 if (!addr
|| !buffer
)
7945 SetLastError(WSAEFAULT
);
7946 return SOCKET_ERROR
;
7949 unixaf
= convert_af_w2u(family
);
7950 if (unixaf
!= AF_INET
&& unixaf
!= AF_INET6
)
7952 SetLastError(WSAEAFNOSUPPORT
);
7953 return SOCKET_ERROR
;
7956 ret
= inet_pton(unixaf
, addr
, buffer
);
7957 if (ret
== -1) SetLastError(wsaErrno());
7960 FIXME( "not supported on this platform\n" );
7961 SetLastError( WSAEAFNOSUPPORT
);
7962 return SOCKET_ERROR
;
7966 /***********************************************************************
7967 * InetPtonW (WS2_32.@)
7969 INT WINAPI
InetPtonW(INT family
, PCWSTR addr
, PVOID buffer
)
7975 TRACE("family %d, addr %s, buffer (%p)\n", family
, debugstr_w(addr
), buffer
);
7979 SetLastError(WSAEFAULT
);
7980 return SOCKET_ERROR
;
7983 len
= WideCharToMultiByte(CP_ACP
, 0, addr
, -1, NULL
, 0, NULL
, NULL
);
7984 if (!(addrA
= HeapAlloc(GetProcessHeap(), 0, len
)))
7986 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
7987 return SOCKET_ERROR
;
7989 WideCharToMultiByte(CP_ACP
, 0, addr
, -1, addrA
, len
, NULL
, NULL
);
7991 ret
= WS_inet_pton(family
, addrA
, buffer
);
7993 HeapFree(GetProcessHeap(), 0, addrA
);
7997 /***********************************************************************
7998 * InetNtopW (WS2_32.@)
8000 PCWSTR WINAPI
InetNtopW(INT family
, PVOID addr
, PWSTR buffer
, SIZE_T len
)
8002 char bufferA
[WS_INET6_ADDRSTRLEN
];
8005 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
8007 if (WS_inet_ntop(family
, addr
, bufferA
, sizeof(bufferA
)))
8009 if (MultiByteToWideChar(CP_ACP
, 0, bufferA
, -1, buffer
, len
))
8012 SetLastError(ERROR_INVALID_PARAMETER
);
8017 /***********************************************************************
8018 * WSAStringToAddressA (WS2_32.80)
8020 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
8022 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
8023 LPSOCKADDR lpAddress
,
8024 LPINT lpAddressLength
)
8027 LPSTR workBuffer
=NULL
,ptrPort
;
8029 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_a(AddressString
), AddressFamily
,
8030 lpProtocolInfo
, lpAddress
, lpAddressLength
);
8032 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
8036 SetLastError(WSAEINVAL
);
8037 return SOCKET_ERROR
;
8041 FIXME("ProtocolInfo not implemented.\n");
8043 workBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
8044 strlen(AddressString
) + 1);
8047 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8048 return SOCKET_ERROR
;
8051 strcpy(workBuffer
, AddressString
);
8053 switch(AddressFamily
)
8057 struct in_addr inetaddr
;
8059 /* If lpAddressLength is too small, tell caller the size we need */
8060 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
8062 *lpAddressLength
= sizeof(SOCKADDR_IN
);
8066 *lpAddressLength
= sizeof(SOCKADDR_IN
);
8067 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
8069 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= WS_AF_INET
;
8071 ptrPort
= strchr(workBuffer
, ':');
8074 /* User may have entered an IPv6 and asked to parse as IPv4 */
8075 if(strchr(ptrPort
+ 1, ':'))
8080 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= htons(atoi(ptrPort
+1));
8085 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= 0;
8088 if(inet_aton(workBuffer
, &inetaddr
) > 0)
8090 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
8101 struct in6_addr inetaddr
;
8102 /* If lpAddressLength is too small, tell caller the size we need */
8103 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
8105 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
8109 #ifdef HAVE_INET_PTON
8110 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
8111 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
8113 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_family
= WS_AF_INET6
;
8115 /* This one is a bit tricky. An IPv6 address contains colons, so the
8116 * check from IPv4 doesn't work like that. However, IPv6 addresses that
8117 * contain a port are written with braces like [fd12:3456:7890::1]:12345
8118 * so what we will do is to look for ']', check if the next char is a
8119 * colon, and if it is, parse the port as in IPv4. */
8121 ptrPort
= strchr(workBuffer
, ']');
8122 if(ptrPort
&& *(++ptrPort
) == ':')
8124 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= htons(atoi(ptrPort
+1));
8129 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= 0;
8132 if(inet_pton(AF_INET6
, workBuffer
, &inetaddr
) > 0)
8134 memcpy(&((LPSOCKADDR_IN6
)lpAddress
)->sin6_addr
, &inetaddr
,
8135 sizeof(struct in6_addr
));
8139 #endif /* HAVE_INET_PTON */
8145 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
8146 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
8150 HeapFree(GetProcessHeap(), 0, workBuffer
);
8154 return SOCKET_ERROR
;
8157 /***********************************************************************
8158 * WSAStringToAddressW (WS2_32.81)
8160 * FIXME: Does anybody know if this function allows using Hebrew/Arabic/Chinese... digits?
8161 * If this should be the case, it would be required to map these digits
8162 * to Unicode digits (0-9) using FoldString first.
8164 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
8166 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
8167 LPSOCKADDR lpAddress
,
8168 LPINT lpAddressLength
)
8171 LPSTR workBuffer
=NULL
;
8172 WSAPROTOCOL_INFOA infoA
;
8173 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
8175 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
8176 lpAddress
, lpAddressLength
);
8178 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
8180 /* if ProtocolInfo is available - convert to ANSI variant */
8183 lpProtoInfoA
= &infoA
;
8184 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
8186 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
8187 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
8189 SetLastError(WSAEINVAL
);
8190 return SOCKET_ERROR
;
8196 /* Translate AddressString to ANSI code page - assumes that only
8197 standard digits 0-9 are used with this API call */
8198 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
8199 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
8203 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
8204 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
8205 lpAddress
,lpAddressLength
);
8206 HeapFree( GetProcessHeap(), 0, workBuffer
);
8210 res
= WSA_NOT_ENOUGH_MEMORY
;
8216 return SOCKET_ERROR
;
8219 /***********************************************************************
8220 * WSAAddressToStringA (WS2_32.27)
8222 * See WSAAddressToStringW
8224 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
8225 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
8229 CHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
8232 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
8234 if (!sockaddr
) return SOCKET_ERROR
;
8235 if (!string
|| !lenstr
) return SOCKET_ERROR
;
8237 switch(sockaddr
->sa_family
)
8240 if (len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
8241 sprintf( buffer
, "%u.%u.%u.%u:%u",
8242 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
8243 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
8244 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
8245 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
8246 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
8248 p
= strchr( buffer
, ':' );
8249 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
8254 struct WS_sockaddr_in6
*sockaddr6
= (LPSOCKADDR_IN6
) sockaddr
;
8258 if (len
< sizeof(SOCKADDR_IN6
)) return SOCKET_ERROR
;
8259 if ((sockaddr6
->sin6_port
))
8260 strcpy(buffer
, "[");
8261 slen
= strlen(buffer
);
8262 if (!WS_inet_ntop(WS_AF_INET6
, &sockaddr6
->sin6_addr
, &buffer
[slen
], sizeof(buffer
) - slen
))
8264 SetLastError(WSAEINVAL
);
8265 return SOCKET_ERROR
;
8267 if ((sockaddr6
->sin6_scope_id
))
8268 sprintf(buffer
+strlen(buffer
), "%%%u", sockaddr6
->sin6_scope_id
);
8269 if ((sockaddr6
->sin6_port
))
8270 sprintf(buffer
+strlen(buffer
), "]:%u", ntohs(sockaddr6
->sin6_port
));
8275 SetLastError(WSAEINVAL
);
8276 return SOCKET_ERROR
;
8279 size
= strlen( buffer
) + 1;
8284 SetLastError(WSAEFAULT
);
8285 return SOCKET_ERROR
;
8288 TRACE("=> %s,%u bytes\n", debugstr_a(buffer
), size
);
8290 strcpy( string
, buffer
);
8294 /***********************************************************************
8295 * WSAAddressToStringW (WS2_32.28)
8297 * Convert a sockaddr address into a readable address string.
8300 * sockaddr [I] Pointer to a sockaddr structure.
8301 * len [I] Size of the sockaddr structure.
8302 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
8303 * string [I/O] Pointer to a buffer to receive the address string.
8304 * lenstr [I/O] Size of the receive buffer in WCHARs.
8308 * Failure: SOCKET_ERROR
8311 * The 'info' parameter is ignored.
8313 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
8314 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
8319 WCHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
8322 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
8325 ret
= WSAAddressToStringA(sockaddr
, len
, NULL
, bufAddr
, &size
);
8327 if (ret
) return ret
;
8329 MultiByteToWideChar( CP_ACP
, 0, bufAddr
, size
, buffer
, sizeof( buffer
)/sizeof(WCHAR
));
8334 SetLastError(WSAEFAULT
);
8335 return SOCKET_ERROR
;
8338 TRACE("=> %s,%u bytes\n", debugstr_w(buffer
), size
);
8340 lstrcpyW( string
, buffer
);
8344 /***********************************************************************
8345 * WSAEnumNameSpaceProvidersA (WS2_32.34)
8347 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
8349 FIXME( "(%p %p) Stub!\n", len
, buffer
);
8353 /***********************************************************************
8354 * WSAEnumNameSpaceProvidersW (WS2_32.35)
8356 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
8358 FIXME( "(%p %p) Stub!\n", len
, buffer
);
8362 /***********************************************************************
8363 * WSAGetQOSByName (WS2_32.41)
8365 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
8367 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
8371 /***********************************************************************
8372 * WSAGetServiceClassInfoA (WS2_32.42)
8374 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
8375 LPWSASERVICECLASSINFOA info
)
8377 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
8379 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8380 return SOCKET_ERROR
;
8383 /***********************************************************************
8384 * WSAGetServiceClassInfoW (WS2_32.43)
8386 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
8387 LPWSASERVICECLASSINFOW info
)
8389 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
8391 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8392 return SOCKET_ERROR
;
8395 /***********************************************************************
8396 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
8398 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
8400 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
8401 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8402 return SOCKET_ERROR
;
8405 /***********************************************************************
8406 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
8408 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
8410 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
8411 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8412 return SOCKET_ERROR
;
8415 /***********************************************************************
8416 * WSALookupServiceBeginA (WS2_32.59)
8418 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
8419 DWORD dwControlFlags
,
8422 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
8424 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8425 return SOCKET_ERROR
;
8428 /***********************************************************************
8429 * WSALookupServiceBeginW (WS2_32.60)
8431 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
8432 DWORD dwControlFlags
,
8435 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
8437 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8438 return SOCKET_ERROR
;
8441 /***********************************************************************
8442 * WSALookupServiceEnd (WS2_32.61)
8444 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
8446 FIXME("(%p) Stub!\n", lookup
);
8450 /***********************************************************************
8451 * WSALookupServiceNextA (WS2_32.62)
8453 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
8455 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
8456 SetLastError(WSA_E_NO_MORE
);
8457 return SOCKET_ERROR
;
8460 /***********************************************************************
8461 * WSALookupServiceNextW (WS2_32.63)
8463 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
8465 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
8466 SetLastError(WSA_E_NO_MORE
);
8467 return SOCKET_ERROR
;
8470 /***********************************************************************
8471 * WSANtohl (WS2_32.64)
8473 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
8475 TRACE( "(%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
8477 if (!lphostlong
) return WSAEFAULT
;
8479 *lphostlong
= ntohl( netlong
);
8483 /***********************************************************************
8484 * WSANtohs (WS2_32.65)
8486 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
8488 TRACE( "(%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
8490 if (!lphostshort
) return WSAEFAULT
;
8492 *lphostshort
= ntohs( netshort
);
8496 /***********************************************************************
8497 * WSAProviderConfigChange (WS2_32.66)
8499 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
8500 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
8502 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
8503 return SOCKET_ERROR
;
8506 /***********************************************************************
8507 * WSARecvDisconnect (WS2_32.68)
8509 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
8511 TRACE( "(%04lx %p)\n", s
, disconnectdata
);
8513 return WS_shutdown( s
, SD_RECEIVE
);
8516 /***********************************************************************
8517 * WSASetServiceA (WS2_32.76)
8519 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
8521 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
8525 /***********************************************************************
8526 * WSASetServiceW (WS2_32.77)
8528 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
8530 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
8534 /***********************************************************************
8535 * WSCEnableNSProvider (WS2_32.84)
8537 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
8539 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
8543 /***********************************************************************
8544 * WSCGetProviderPath (WS2_32.86)
8546 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
8548 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
8550 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
8552 *errcode
= WSAEINVAL
;
8553 return SOCKET_ERROR
;
8556 /***********************************************************************
8557 * WSCInstallNameSpace (WS2_32.87)
8559 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
8560 DWORD version
, LPGUID provider
)
8562 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
8563 namespace, version
, debugstr_guid(provider
) );
8567 /***********************************************************************
8568 * WSCUnInstallNameSpace (WS2_32.89)
8570 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
8572 FIXME("(%p) Stub!\n", lpProviderId
);
8576 /***********************************************************************
8577 * WSCWriteProviderOrder (WS2_32.91)
8579 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
8581 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);
8585 /***********************************************************************
8586 * WSANSPIoctl (WS2_32.91)
8588 INT WINAPI
WSANSPIoctl( HANDLE hLookup
, DWORD dwControlCode
, LPVOID lpvInBuffer
,
8589 DWORD cbInBuffer
, LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
8590 LPDWORD lpcbBytesReturned
, LPWSACOMPLETION lpCompletion
)
8592 FIXME("(%p, 0x%08x, %p, 0x%08x, %p, 0x%08x, %p, %p) Stub!\n", hLookup
, dwControlCode
,
8593 lpvInBuffer
, cbInBuffer
, lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpCompletion
);
8594 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8595 return SOCKET_ERROR
;
8598 /*****************************************************************************
8599 * WSAEnumProtocolsA [WS2_32.@]
8601 * see function WSAEnumProtocolsW
8603 INT WINAPI
WSAEnumProtocolsA( LPINT protocols
, LPWSAPROTOCOL_INFOA buffer
, LPDWORD len
)
8605 return WS_EnumProtocols( FALSE
, protocols
, (LPWSAPROTOCOL_INFOW
) buffer
, len
);
8608 /*****************************************************************************
8609 * WSAEnumProtocolsW [WS2_32.@]
8611 * Retrieves information about specified set of active network protocols.
8614 * protocols [I] Pointer to null-terminated array of protocol id's. NULL
8615 * retrieves information on all available protocols.
8616 * buffer [I] Pointer to a buffer to be filled with WSAPROTOCOL_INFO
8618 * len [I/O] Pointer to a variable specifying buffer size. On output
8619 * the variable holds the number of bytes needed when the
8620 * specified size is too small.
8623 * Success: number of WSAPROTOCOL_INFO structures in buffer.
8624 * Failure: SOCKET_ERROR
8627 * NT4SP5 does not return SPX if protocols == NULL
8630 * - NT4SP5 returns in addition these list of NETBIOS protocols
8631 * (address family 17), each entry two times one for socket type 2 and 5
8633 * iProtocol szProtocol
8634 * 0x80000000 \Device\NwlnkNb
8635 * 0xfffffffa \Device\NetBT_CBENT7
8636 * 0xfffffffb \Device\Nbf_CBENT7
8637 * 0xfffffffc \Device\NetBT_NdisWan5
8638 * 0xfffffffd \Device\NetBT_El9202
8639 * 0xfffffffe \Device\Nbf_El9202
8640 * 0xffffffff \Device\Nbf_NdisWan4
8642 * - there is no check that the operating system supports the returned
8645 INT WINAPI
WSAEnumProtocolsW( LPINT protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
)
8647 return WS_EnumProtocols( TRUE
, protocols
, buffer
, len
);
8650 /*****************************************************************************
8651 * WSCEnumProtocols [WS2_32.@]
8654 * protocols [I] Null-terminated array of iProtocol values.
8655 * buffer [O] Buffer of WSAPROTOCOL_INFOW structures.
8656 * len [I/O] Size of buffer on input/output.
8657 * errno [O] Error code.
8660 * Success: number of protocols to be reported on.
8661 * Failure: SOCKET_ERROR. error is in errno.
8664 * Doesn't supply info on layered protocols.
8667 INT WINAPI
WSCEnumProtocols( LPINT protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
, LPINT err
)
8669 INT ret
= WSAEnumProtocolsW( protocols
, buffer
, len
);
8671 if (ret
== SOCKET_ERROR
) *err
= WSAENOBUFS
;