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"
161 #include "wine/heap.h"
163 #if defined(linux) && !defined(IP_UNICAST_IF)
164 #define IP_UNICAST_IF 50
167 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
168 # define sipx_network sipx_addr.x_net
169 # define sipx_node sipx_addr.x_host.c_host
170 #endif /* __FreeBSD__ */
173 #define INADDR_NONE ~0UL
176 #if !defined(TCP_KEEPIDLE) && defined(TCP_KEEPALIVE)
177 /* TCP_KEEPALIVE is the Mac OS name for TCP_KEEPIDLE */
178 #define TCP_KEEPIDLE TCP_KEEPALIVE
181 #define FILE_USE_FILE_POINTER_POSITION ((LONGLONG)-2)
183 WINE_DEFAULT_DEBUG_CHANNEL(winsock
);
184 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
186 /* names of the protocols */
187 static const WCHAR NameIpxW
[] = {'I', 'P', 'X', '\0'};
188 static const WCHAR NameSpxW
[] = {'S', 'P', 'X', '\0'};
189 static const WCHAR NameSpxIIW
[] = {'S', 'P', 'X', ' ', 'I', 'I', '\0'};
190 static const WCHAR NameTcpW
[] = {'T', 'C', 'P', '/', 'I', 'P', '\0'};
191 static const WCHAR NameUdpW
[] = {'U', 'D', 'P', '/', 'I', 'P', '\0'};
193 /* Taken from Win2k */
194 static const GUID ProviderIdIP
= { 0xe70f1aa0, 0xab8b, 0x11cf,
195 { 0x8c, 0xa3, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } };
196 static const GUID ProviderIdIPX
= { 0x11058240, 0xbe47, 0x11cf,
197 { 0x95, 0xc8, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } };
198 static const GUID ProviderIdSPX
= { 0x11058241, 0xbe47, 0x11cf,
199 { 0x95, 0xc8, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } };
201 static const INT valid_protocols
[] =
211 #define IS_IPX_PROTO(X) ((X) >= WS_NSPROTO_IPX && (X) <= WS_NSPROTO_IPX + 255)
213 #if defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER)
214 # define LINUX_BOUND_IF
215 struct interface_filter
{
216 struct sock_filter iface_memaddr
;
217 struct sock_filter iface_rule
;
218 struct sock_filter ip_memaddr
;
219 struct sock_filter ip_rule
;
220 struct sock_filter return_keep
;
221 struct sock_filter return_dump
;
223 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
224 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
225 /sizeof(struct sock_filter)
226 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
227 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
228 /sizeof(struct sock_filter)
229 # define FILTER_JUMP_NEXT() (u_char)(0)
230 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
231 static struct interface_filter generic_interface_filter
= {
232 /* This filter rule allows incoming packets on the specified interface, which works for all
233 * remotely generated packets and for locally generated broadcast packets. */
234 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
235 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
236 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
237 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
238 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
239 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
240 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
241 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
243 #endif /* LINUX_BOUND_IF */
245 extern ssize_t CDECL
__wine_locked_recvmsg( int fd
, struct msghdr
*hdr
, int flags
);
248 * The actual definition of WSASendTo, wrapped in a different function name
249 * so that internal calls from ws2_32 itself will not trigger programs like
250 * Garena, which hooks WSASendTo/WSARecvFrom calls.
252 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
253 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
254 const struct WS_sockaddr
*to
, int tolen
,
255 LPWSAOVERLAPPED lpOverlapped
,
256 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
259 * Internal fundamental receive function, essentially WSARecvFrom with an
260 * additional parameter to support message control headers.
262 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
263 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
264 struct WS_sockaddr
*lpFrom
,
265 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
266 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
267 LPWSABUF lpControlBuffer
);
269 /* critical section to protect some non-reentrant net function */
270 static CRITICAL_SECTION csWSgetXXXbyYYY
;
271 static CRITICAL_SECTION_DEBUG critsect_debug
=
273 0, 0, &csWSgetXXXbyYYY
,
274 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
275 0, 0, { (DWORD_PTR
)(__FILE__
": csWSgetXXXbyYYY") }
277 static CRITICAL_SECTION csWSgetXXXbyYYY
= { &critsect_debug
, -1, 0, 0, 0, 0 };
279 union generic_unix_sockaddr
281 struct sockaddr addr
;
282 char data
[128]; /* should be big enough for all families */
285 static inline const char *debugstr_sockaddr( const struct WS_sockaddr
*a
)
287 if (!a
) return "(nil)";
288 switch (a
->sa_family
)
294 struct WS_sockaddr_in
*sin
= (struct WS_sockaddr_in
*)a
;
296 p
= WS_inet_ntop( WS_AF_INET
, &sin
->sin_addr
, buf
, sizeof(buf
) );
298 p
= "(unknown IPv4 address)";
300 return wine_dbg_sprintf("{ family AF_INET, address %s, port %d }",
301 p
, ntohs(sin
->sin_port
));
307 struct WS_sockaddr_in6
*sin
= (struct WS_sockaddr_in6
*)a
;
309 p
= WS_inet_ntop( WS_AF_INET6
, &sin
->sin6_addr
, buf
, sizeof(buf
) );
311 p
= "(unknown IPv6 address)";
312 return wine_dbg_sprintf("{ family AF_INET6, address %s, port %d }",
313 p
, ntohs(sin
->sin6_port
));
318 char netnum
[16], nodenum
[16];
319 struct WS_sockaddr_ipx
*sin
= (struct WS_sockaddr_ipx
*)a
;
321 for (i
= 0;i
< 4; i
++) sprintf(netnum
+ i
* 2, "%02X", (unsigned char) sin
->sa_netnum
[i
]);
322 for (i
= 0;i
< 6; i
++) sprintf(nodenum
+ i
* 2, "%02X", (unsigned char) sin
->sa_nodenum
[i
]);
324 return wine_dbg_sprintf("{ family AF_IPX, address %s.%s, ipx socket %d }",
325 netnum
, nodenum
, sin
->sa_socket
);
331 memcpy( &addr
, ((const SOCKADDR_IRDA
*)a
)->irdaDeviceID
, sizeof(addr
) );
332 addr
= ntohl( addr
);
333 return wine_dbg_sprintf("{ family AF_IRDA, addr %08x, name %s }",
335 ((const SOCKADDR_IRDA
*)a
)->irdaServiceName
);
338 return wine_dbg_sprintf("{ family %d }", a
->sa_family
);
342 static inline const char *debugstr_sockopt(int level
, int optname
)
344 const char *stropt
= NULL
, *strlevel
= NULL
;
346 #define DEBUG_SOCKLEVEL(x) case (x): strlevel = #x
347 #define DEBUG_SOCKOPT(x) case (x): stropt = #x; break
351 DEBUG_SOCKLEVEL(WS_SOL_SOCKET
);
354 DEBUG_SOCKOPT(WS_SO_ACCEPTCONN
);
355 DEBUG_SOCKOPT(WS_SO_BROADCAST
);
356 DEBUG_SOCKOPT(WS_SO_BSP_STATE
);
357 DEBUG_SOCKOPT(WS_SO_CONDITIONAL_ACCEPT
);
358 DEBUG_SOCKOPT(WS_SO_CONNECT_TIME
);
359 DEBUG_SOCKOPT(WS_SO_DEBUG
);
360 DEBUG_SOCKOPT(WS_SO_DONTLINGER
);
361 DEBUG_SOCKOPT(WS_SO_DONTROUTE
);
362 DEBUG_SOCKOPT(WS_SO_ERROR
);
363 DEBUG_SOCKOPT(WS_SO_EXCLUSIVEADDRUSE
);
364 DEBUG_SOCKOPT(WS_SO_GROUP_ID
);
365 DEBUG_SOCKOPT(WS_SO_GROUP_PRIORITY
);
366 DEBUG_SOCKOPT(WS_SO_KEEPALIVE
);
367 DEBUG_SOCKOPT(WS_SO_LINGER
);
368 DEBUG_SOCKOPT(WS_SO_MAX_MSG_SIZE
);
369 DEBUG_SOCKOPT(WS_SO_OOBINLINE
);
370 DEBUG_SOCKOPT(WS_SO_OPENTYPE
);
371 DEBUG_SOCKOPT(WS_SO_PROTOCOL_INFOA
);
372 DEBUG_SOCKOPT(WS_SO_PROTOCOL_INFOW
);
373 DEBUG_SOCKOPT(WS_SO_RCVBUF
);
374 DEBUG_SOCKOPT(WS_SO_RCVTIMEO
);
375 DEBUG_SOCKOPT(WS_SO_REUSEADDR
);
376 DEBUG_SOCKOPT(WS_SO_SNDBUF
);
377 DEBUG_SOCKOPT(WS_SO_SNDTIMEO
);
378 DEBUG_SOCKOPT(WS_SO_TYPE
);
379 DEBUG_SOCKOPT(WS_SO_UPDATE_CONNECT_CONTEXT
);
383 DEBUG_SOCKLEVEL(WS_NSPROTO_IPX
);
386 DEBUG_SOCKOPT(WS_IPX_PTYPE
);
387 DEBUG_SOCKOPT(WS_IPX_FILTERPTYPE
);
388 DEBUG_SOCKOPT(WS_IPX_DSTYPE
);
389 DEBUG_SOCKOPT(WS_IPX_RECVHDR
);
390 DEBUG_SOCKOPT(WS_IPX_MAXSIZE
);
391 DEBUG_SOCKOPT(WS_IPX_ADDRESS
);
392 DEBUG_SOCKOPT(WS_IPX_MAX_ADAPTER_NUM
);
396 DEBUG_SOCKLEVEL(WS_SOL_IRLMP
);
399 DEBUG_SOCKOPT(WS_IRLMP_ENUMDEVICES
);
403 DEBUG_SOCKLEVEL(WS_IPPROTO_TCP
);
406 DEBUG_SOCKOPT(WS_TCP_BSDURGENT
);
407 DEBUG_SOCKOPT(WS_TCP_EXPEDITED_1122
);
408 DEBUG_SOCKOPT(WS_TCP_NODELAY
);
412 DEBUG_SOCKLEVEL(WS_IPPROTO_IP
);
415 DEBUG_SOCKOPT(WS_IP_ADD_MEMBERSHIP
);
416 DEBUG_SOCKOPT(WS_IP_DONTFRAGMENT
);
417 DEBUG_SOCKOPT(WS_IP_DROP_MEMBERSHIP
);
418 DEBUG_SOCKOPT(WS_IP_HDRINCL
);
419 DEBUG_SOCKOPT(WS_IP_MULTICAST_IF
);
420 DEBUG_SOCKOPT(WS_IP_MULTICAST_LOOP
);
421 DEBUG_SOCKOPT(WS_IP_MULTICAST_TTL
);
422 DEBUG_SOCKOPT(WS_IP_OPTIONS
);
423 DEBUG_SOCKOPT(WS_IP_PKTINFO
);
424 DEBUG_SOCKOPT(WS_IP_RECEIVE_BROADCAST
);
425 DEBUG_SOCKOPT(WS_IP_TOS
);
426 DEBUG_SOCKOPT(WS_IP_TTL
);
427 DEBUG_SOCKOPT(WS_IP_UNICAST_IF
);
431 DEBUG_SOCKLEVEL(WS_IPPROTO_IPV6
);
434 DEBUG_SOCKOPT(WS_IPV6_ADD_MEMBERSHIP
);
435 DEBUG_SOCKOPT(WS_IPV6_DROP_MEMBERSHIP
);
436 DEBUG_SOCKOPT(WS_IPV6_MULTICAST_IF
);
437 DEBUG_SOCKOPT(WS_IPV6_MULTICAST_HOPS
);
438 DEBUG_SOCKOPT(WS_IPV6_MULTICAST_LOOP
);
439 DEBUG_SOCKOPT(WS_IPV6_UNICAST_HOPS
);
440 DEBUG_SOCKOPT(WS_IPV6_V6ONLY
);
441 DEBUG_SOCKOPT(WS_IPV6_UNICAST_IF
);
442 DEBUG_SOCKOPT(WS_IPV6_DONTFRAG
);
446 #undef DEBUG_SOCKLEVEL
450 strlevel
= wine_dbg_sprintf("WS_0x%x", level
);
452 stropt
= wine_dbg_sprintf("WS_0x%x", optname
);
454 return wine_dbg_sprintf("level %s, name %s", strlevel
+ 3, stropt
+ 3);
457 static inline const char *debugstr_optval(const char *optval
, int optlenval
)
459 if (optval
&& !IS_INTRESOURCE(optval
) && optlenval
>= 1 && optlenval
<= sizeof(DWORD
))
462 memcpy(&value
, optval
, optlenval
);
463 return wine_dbg_sprintf("%p (%u)", optval
, value
);
465 return wine_dbg_sprintf("%p", optval
);
468 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
469 #define SOCKET2HANDLE(s) ((HANDLE)(s))
470 #define HANDLE2SOCKET(h) ((SOCKET)(h))
472 /****************************************************************
473 * Async IO declarations
474 ****************************************************************/
476 typedef NTSTATUS
async_callback_t( void *user
, IO_STATUS_BLOCK
*io
, NTSTATUS status
);
480 async_callback_t
*callback
; /* must be the first field */
481 struct ws2_async_io
*next
;
484 struct ws2_async_shutdown
486 struct ws2_async_io io
;
488 IO_STATUS_BLOCK iosb
;
494 struct ws2_async_io io
;
496 LPWSAOVERLAPPED user_overlapped
;
497 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func
;
498 IO_STATUS_BLOCK local_iosb
;
499 struct WS_sockaddr
*addr
;
502 int val
; /* for send operations */
503 int *ptr
; /* for recv operations */
508 unsigned int n_iovecs
;
509 unsigned int first_iovec
;
510 struct iovec iovec
[1];
513 struct ws2_accept_async
515 struct ws2_async_io io
;
516 HANDLE listen_socket
;
517 HANDLE accept_socket
;
518 LPOVERLAPPED user_overlapped
;
520 PVOID buf
; /* buffer to write data to */
524 struct ws2_async
*read
;
527 struct ws2_transmitfile_async
529 struct ws2_async_io io
;
534 DWORD bytes_per_send
;
535 TRANSMIT_FILE_BUFFERS buffers
;
537 LARGE_INTEGER offset
;
538 struct ws2_async write
;
541 static struct ws2_async_io
*async_io_freelist
;
543 static void release_async_io( struct ws2_async_io
*io
)
547 struct ws2_async_io
*next
= async_io_freelist
;
549 if (InterlockedCompareExchangePointer( (void **)&async_io_freelist
, io
, next
) == next
) return;
553 static struct ws2_async_io
*alloc_async_io( DWORD size
, async_callback_t callback
)
555 /* first free remaining previous fileinfos */
557 struct ws2_async_io
*io
= InterlockedExchangePointer( (void **)&async_io_freelist
, NULL
);
561 struct ws2_async_io
*next
= io
->next
;
562 HeapFree( GetProcessHeap(), 0, io
);
566 io
= HeapAlloc( GetProcessHeap(), 0, size
);
567 if (io
) io
->callback
= callback
;
571 static NTSTATUS
register_async( int type
, HANDLE handle
, struct ws2_async_io
*async
, HANDLE event
,
572 PIO_APC_ROUTINE apc
, void *apc_context
, IO_STATUS_BLOCK
*io
)
576 SERVER_START_REQ( register_async
)
579 req
->async
.handle
= wine_server_obj_handle( handle
);
580 req
->async
.user
= wine_server_client_ptr( async
);
581 req
->async
.iosb
= wine_server_client_ptr( io
);
582 req
->async
.event
= wine_server_obj_handle( event
);
583 req
->async
.apc
= wine_server_client_ptr( apc
);
584 req
->async
.apc_context
= wine_server_client_ptr( apc_context
);
585 status
= wine_server_call( req
);
592 /****************************************************************/
594 /* ----------------------------------- internal data */
596 /* ws_... struct conversion flags */
598 typedef struct /* WSAAsyncSelect() control struct */
600 HANDLE service
, event
, sock
;
606 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
607 #define WS_MAX_UDP_DATAGRAM 1024
608 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
610 /* hostent's, servent's and protent's are stored in one buffer per thread,
611 * as documented on MSDN for the functions that return any of the buffers */
612 struct per_thread_data
615 struct WS_hostent
*he_buffer
;
616 struct WS_servent
*se_buffer
;
617 struct WS_protoent
*pe_buffer
;
618 struct pollfd
*fd_cache
;
619 unsigned int fd_count
;
623 char ntoa_buffer
[16]; /* 4*3 digits + 3 '.' + 1 '\0' */
626 /* internal: routing description information */
630 DWORD metric
, default_route
;
633 static INT num_startup
; /* reference counter */
634 static FARPROC blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
636 /* function prototypes */
637 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int aliases_size
, int addresses
, int address_length
);
638 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
639 static struct WS_protoent
*WS_create_pe( const char *name
, char **aliases
, int prot
);
640 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
641 static int ws_protocol_info(SOCKET s
, int unicode
, WSAPROTOCOL_INFOW
*buffer
, int *size
);
643 int WSAIOCTL_GetInterfaceCount(void);
644 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
646 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
, ULONG Information
, BOOL force
);
648 #define MAP_OPTION(opt) { WS_##opt, opt }
650 static const int ws_flags_map
[][2] =
652 MAP_OPTION( MSG_OOB
),
653 MAP_OPTION( MSG_PEEK
),
654 MAP_OPTION( MSG_DONTROUTE
),
655 MAP_OPTION( MSG_WAITALL
),
656 { WS_MSG_PARTIAL
, 0 },
659 static const int ws_sock_map
[][2] =
661 MAP_OPTION( SO_DEBUG
),
662 MAP_OPTION( SO_ACCEPTCONN
),
663 MAP_OPTION( SO_REUSEADDR
),
664 MAP_OPTION( SO_KEEPALIVE
),
665 MAP_OPTION( SO_DONTROUTE
),
666 MAP_OPTION( SO_BROADCAST
),
667 MAP_OPTION( SO_LINGER
),
668 MAP_OPTION( SO_OOBINLINE
),
669 MAP_OPTION( SO_SNDBUF
),
670 MAP_OPTION( SO_RCVBUF
),
671 MAP_OPTION( SO_ERROR
),
672 MAP_OPTION( SO_TYPE
),
674 MAP_OPTION( SO_RCVTIMEO
),
677 MAP_OPTION( SO_SNDTIMEO
),
681 static const int ws_tcp_map
[][2] =
684 MAP_OPTION( TCP_NODELAY
),
688 static const int ws_ip_map
[][2] =
690 MAP_OPTION( IP_MULTICAST_IF
),
691 MAP_OPTION( IP_MULTICAST_TTL
),
692 MAP_OPTION( IP_MULTICAST_LOOP
),
693 MAP_OPTION( IP_ADD_MEMBERSHIP
),
694 MAP_OPTION( IP_DROP_MEMBERSHIP
),
695 MAP_OPTION( IP_OPTIONS
),
697 MAP_OPTION( IP_HDRINCL
),
699 MAP_OPTION( IP_TOS
),
700 MAP_OPTION( IP_TTL
),
701 #if defined(IP_PKTINFO)
702 MAP_OPTION( IP_PKTINFO
),
703 #elif defined(IP_RECVDSTADDR)
704 { WS_IP_PKTINFO
, IP_RECVDSTADDR
},
707 MAP_OPTION( IP_UNICAST_IF
),
711 static const int ws_ipv6_map
[][2] =
713 #ifdef IPV6_ADD_MEMBERSHIP
714 MAP_OPTION( IPV6_ADD_MEMBERSHIP
),
716 #ifdef IPV6_DROP_MEMBERSHIP
717 MAP_OPTION( IPV6_DROP_MEMBERSHIP
),
719 MAP_OPTION( IPV6_MULTICAST_IF
),
720 MAP_OPTION( IPV6_MULTICAST_HOPS
),
721 MAP_OPTION( IPV6_MULTICAST_LOOP
),
722 MAP_OPTION( IPV6_UNICAST_HOPS
),
723 MAP_OPTION( IPV6_V6ONLY
),
724 #ifdef IPV6_UNICAST_IF
725 MAP_OPTION( IPV6_UNICAST_IF
),
729 static const int ws_af_map
[][2] =
731 MAP_OPTION( AF_UNSPEC
),
732 MAP_OPTION( AF_INET
),
733 MAP_OPTION( AF_INET6
),
735 MAP_OPTION( AF_IPX
),
738 MAP_OPTION( AF_IRDA
),
740 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
743 static const int ws_socktype_map
[][2] =
745 MAP_OPTION( SOCK_DGRAM
),
746 MAP_OPTION( SOCK_STREAM
),
747 MAP_OPTION( SOCK_RAW
),
748 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
751 static const int ws_proto_map
[][2] =
753 MAP_OPTION( IPPROTO_IP
),
754 MAP_OPTION( IPPROTO_TCP
),
755 MAP_OPTION( IPPROTO_UDP
),
756 MAP_OPTION( IPPROTO_IPV6
),
757 MAP_OPTION( IPPROTO_ICMP
),
758 MAP_OPTION( IPPROTO_IGMP
),
759 MAP_OPTION( IPPROTO_RAW
),
760 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
763 static const int ws_aiflag_map
[][2] =
765 MAP_OPTION( AI_PASSIVE
),
766 MAP_OPTION( AI_CANONNAME
),
767 MAP_OPTION( AI_NUMERICHOST
),
768 #ifdef AI_NUMERICSERV
769 MAP_OPTION( AI_NUMERICSERV
),
772 MAP_OPTION( AI_V4MAPPED
),
774 MAP_OPTION( AI_ADDRCONFIG
),
777 static const int ws_niflag_map
[][2] =
779 MAP_OPTION( NI_NOFQDN
),
780 MAP_OPTION( NI_NUMERICHOST
),
781 MAP_OPTION( NI_NAMEREQD
),
782 MAP_OPTION( NI_NUMERICSERV
),
783 MAP_OPTION( NI_DGRAM
),
786 static const int ws_eai_map
[][2] =
788 MAP_OPTION( EAI_AGAIN
),
789 MAP_OPTION( EAI_BADFLAGS
),
790 MAP_OPTION( EAI_FAIL
),
791 MAP_OPTION( EAI_FAMILY
),
792 MAP_OPTION( EAI_MEMORY
),
793 /* Note: EAI_NODATA is deprecated, but still
794 * used by Windows and Linux... We map the newer
795 * EAI_NONAME to EAI_NODATA for now until Windows
799 MAP_OPTION( EAI_NODATA
),
802 { WS_EAI_NODATA
, EAI_NONAME
},
805 MAP_OPTION( EAI_SERVICE
),
806 MAP_OPTION( EAI_SOCKTYPE
),
810 static const int ws_poll_map
[][2] =
812 MAP_OPTION( POLLERR
),
813 MAP_OPTION( POLLHUP
),
814 MAP_OPTION( POLLNVAL
),
815 MAP_OPTION( POLLWRNORM
),
816 MAP_OPTION( POLLWRBAND
),
817 MAP_OPTION( POLLRDNORM
),
818 { WS_POLLRDBAND
, POLLPRI
}
821 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
823 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
824 #if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
825 static inline WSACMSGHDR
*fill_control_message(int level
, int type
, WSACMSGHDR
*current
, ULONG
*maxsize
, void *data
, int len
)
827 ULONG msgsize
= sizeof(WSACMSGHDR
) + WSA_CMSG_ALIGN(len
);
828 char *ptr
= (char *) current
+ sizeof(WSACMSGHDR
);
830 /* Make sure there is at least enough room for this entry */
831 if (msgsize
> *maxsize
)
834 /* Fill in the entry */
835 current
->cmsg_len
= sizeof(WSACMSGHDR
) + len
;
836 current
->cmsg_level
= level
;
837 current
->cmsg_type
= type
;
838 memcpy(ptr
, data
, len
);
839 /* Return the pointer to where next entry should go */
840 return (WSACMSGHDR
*) (ptr
+ WSA_CMSG_ALIGN(len
));
842 #endif /* defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) */
844 static inline int convert_control_headers(struct msghdr
*hdr
, WSABUF
*control
)
846 #if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
847 WSACMSGHDR
*cmsg_win
= (WSACMSGHDR
*) control
->buf
, *ptr
;
848 ULONG ctlsize
= control
->len
;
849 struct cmsghdr
*cmsg_unix
;
852 /* Loop over all the headers, converting as appropriate */
853 for (cmsg_unix
= CMSG_FIRSTHDR(hdr
); cmsg_unix
!= NULL
; cmsg_unix
= CMSG_NXTHDR(hdr
, cmsg_unix
))
855 switch(cmsg_unix
->cmsg_level
)
858 switch(cmsg_unix
->cmsg_type
)
860 #if defined(IP_PKTINFO)
863 /* Convert the Unix IP_PKTINFO structure to the Windows version */
864 struct in_pktinfo
*data_unix
= (struct in_pktinfo
*) CMSG_DATA(cmsg_unix
);
865 struct WS_in_pktinfo data_win
;
867 memcpy(&data_win
.ipi_addr
,&data_unix
->ipi_addr
.s_addr
,4); /* 4 bytes = 32 address bits */
868 data_win
.ipi_ifindex
= data_unix
->ipi_ifindex
;
869 ptr
= fill_control_message(WS_IPPROTO_IP
, WS_IP_PKTINFO
, ptr
, &ctlsize
,
870 (void*)&data_win
, sizeof(data_win
));
871 if (!ptr
) goto error
;
873 #elif defined(IP_RECVDSTADDR)
876 struct in_addr
*addr_unix
= (struct in_addr
*) CMSG_DATA(cmsg_unix
);
877 struct WS_in_pktinfo data_win
;
879 memcpy(&data_win
.ipi_addr
, &addr_unix
->s_addr
, 4); /* 4 bytes = 32 address bits */
880 data_win
.ipi_ifindex
= 0; /* FIXME */
881 ptr
= fill_control_message(WS_IPPROTO_IP
, WS_IP_PKTINFO
, ptr
, &ctlsize
,
882 (void*)&data_win
, sizeof(data_win
));
883 if (!ptr
) goto error
;
885 #endif /* IP_PKTINFO */
887 FIXME("Unhandled IPPROTO_IP message header type %d\n", cmsg_unix
->cmsg_type
);
892 FIXME("Unhandled message header level %d\n", cmsg_unix
->cmsg_level
);
897 /* Set the length of the returned control headers */
898 control
->len
= (char*)ptr
- (char*)cmsg_win
;
903 #else /* defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) */
906 #endif /* defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) */
908 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
910 /* ----------------------------------- error handling */
912 static NTSTATUS
sock_get_ntstatus( int err
)
916 case EBADF
: return STATUS_INVALID_HANDLE
;
917 case EBUSY
: return STATUS_DEVICE_BUSY
;
919 case EACCES
: return STATUS_ACCESS_DENIED
;
920 case EFAULT
: return STATUS_NO_MEMORY
;
921 case EINVAL
: return STATUS_INVALID_PARAMETER
;
923 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
924 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
925 case EINPROGRESS
: return STATUS_PENDING
;
926 case EALREADY
: return STATUS_NETWORK_BUSY
;
927 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
928 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
929 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
930 case EPROTONOSUPPORT
:
931 case ESOCKTNOSUPPORT
:
934 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
935 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
936 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
937 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
938 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
939 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
940 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
941 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
942 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
943 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
944 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
946 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
947 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
949 case 0: return STATUS_SUCCESS
;
951 WARN("Unknown errno %d!\n", err
);
952 return STATUS_UNSUCCESSFUL
;
956 static UINT
sock_get_error( int err
)
960 case EINTR
: return WSAEINTR
;
962 case EACCES
: return WSAEACCES
;
963 case EFAULT
: return WSAEFAULT
;
964 case EINVAL
: return WSAEINVAL
;
965 case EMFILE
: return WSAEMFILE
;
966 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
967 case EINPROGRESS
: return WSAEINPROGRESS
;
968 case EALREADY
: return WSAEALREADY
;
970 case ENOTSOCK
: return WSAENOTSOCK
;
971 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
972 case EMSGSIZE
: return WSAEMSGSIZE
;
973 case EPROTOTYPE
: return WSAEPROTOTYPE
;
974 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
975 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
976 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
977 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
978 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
979 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
980 case EADDRINUSE
: return WSAEADDRINUSE
;
981 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
982 case ENETDOWN
: return WSAENETDOWN
;
983 case ENETUNREACH
: return WSAENETUNREACH
;
984 case ENETRESET
: return WSAENETRESET
;
985 case ECONNABORTED
: return WSAECONNABORTED
;
987 case ECONNRESET
: return WSAECONNRESET
;
988 case ENOBUFS
: return WSAENOBUFS
;
989 case EISCONN
: return WSAEISCONN
;
990 case ENOTCONN
: return WSAENOTCONN
;
991 case ESHUTDOWN
: return WSAESHUTDOWN
;
992 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
993 case ETIMEDOUT
: return WSAETIMEDOUT
;
994 case ECONNREFUSED
: return WSAECONNREFUSED
;
995 case ELOOP
: return WSAELOOP
;
996 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
997 case EHOSTDOWN
: return WSAEHOSTDOWN
;
998 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
999 case ENOTEMPTY
: return WSAENOTEMPTY
;
1001 case EPROCLIM
: return WSAEPROCLIM
;
1004 case EUSERS
: return WSAEUSERS
;
1007 case EDQUOT
: return WSAEDQUOT
;
1010 case ESTALE
: return WSAESTALE
;
1013 case EREMOTE
: return WSAEREMOTE
;
1016 /* just in case we ever get here and there are no problems */
1019 WARN("Unknown errno %d!\n", err
);
1020 return WSAEOPNOTSUPP
;
1024 static UINT
wsaErrno(void)
1026 int loc_errno
= errno
;
1027 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
1029 return sock_get_error( loc_errno
);
1032 /* most ws2 overlapped functions return an ntstatus-based error code */
1033 static NTSTATUS
wsaErrStatus(void)
1035 int loc_errno
= errno
;
1036 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
1038 return sock_get_ntstatus(loc_errno
);
1041 static UINT
wsaHerrno(int loc_errno
)
1043 WARN("h_errno %d.\n", loc_errno
);
1047 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
1048 case TRY_AGAIN
: return WSATRY_AGAIN
;
1049 case NO_RECOVERY
: return WSANO_RECOVERY
;
1050 case NO_DATA
: return WSANO_DATA
;
1051 case ENOBUFS
: return WSAENOBUFS
;
1055 WARN("Unknown h_errno %d!\n", loc_errno
);
1056 return WSAEOPNOTSUPP
;
1060 static NTSTATUS
sock_error_to_ntstatus( DWORD err
)
1064 case 0: return STATUS_SUCCESS
;
1065 case WSAEBADF
: return STATUS_INVALID_HANDLE
;
1066 case WSAEACCES
: return STATUS_ACCESS_DENIED
;
1067 case WSAEFAULT
: return STATUS_NO_MEMORY
;
1068 case WSAEINVAL
: return STATUS_INVALID_PARAMETER
;
1069 case WSAEMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
1070 case WSAEWOULDBLOCK
: return STATUS_CANT_WAIT
;
1071 case WSAEINPROGRESS
: return STATUS_PENDING
;
1072 case WSAEALREADY
: return STATUS_NETWORK_BUSY
;
1073 case WSAENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
1074 case WSAEDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
1075 case WSAEMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
1076 case WSAEPROTONOSUPPORT
:
1077 case WSAESOCKTNOSUPPORT
:
1078 case WSAEPFNOSUPPORT
:
1079 case WSAEAFNOSUPPORT
:
1080 case WSAEPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
1081 case WSAENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
1082 case WSAEOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
1083 case WSAEADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
1084 case WSAEADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
1085 case WSAECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
1086 case WSAESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
1087 case WSAENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
1088 case WSAETIMEDOUT
: return STATUS_IO_TIMEOUT
;
1089 case WSAENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
1090 case WSAENETDOWN
: return STATUS_NETWORK_BUSY
;
1091 case WSAECONNRESET
: return STATUS_CONNECTION_RESET
;
1092 case WSAECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
1094 FIXME("unmapped error %u\n", err
);
1095 return STATUS_UNSUCCESSFUL
;
1099 static DWORD
NtStatusToWSAError( DWORD status
)
1103 case STATUS_SUCCESS
: return 0;
1104 case STATUS_PENDING
: return WSA_IO_PENDING
;
1105 case STATUS_OBJECT_TYPE_MISMATCH
: return WSAENOTSOCK
;
1106 case STATUS_INVALID_HANDLE
: return WSAEBADF
;
1107 case STATUS_INVALID_PARAMETER
: return WSAEINVAL
;
1108 case STATUS_PIPE_DISCONNECTED
: return WSAESHUTDOWN
;
1109 case STATUS_NETWORK_BUSY
: return WSAEALREADY
;
1110 case STATUS_NETWORK_UNREACHABLE
: return WSAENETUNREACH
;
1111 case STATUS_CONNECTION_REFUSED
: return WSAECONNREFUSED
;
1112 case STATUS_CONNECTION_DISCONNECTED
: return WSAENOTCONN
;
1113 case STATUS_CONNECTION_RESET
: return WSAECONNRESET
;
1114 case STATUS_CONNECTION_ABORTED
: return WSAECONNABORTED
;
1115 case STATUS_CANCELLED
: return WSA_OPERATION_ABORTED
;
1116 case STATUS_ADDRESS_ALREADY_ASSOCIATED
: return WSAEADDRINUSE
;
1117 case STATUS_IO_TIMEOUT
:
1118 case STATUS_TIMEOUT
: return WSAETIMEDOUT
;
1119 case STATUS_NO_MEMORY
: return WSAEFAULT
;
1120 case STATUS_ACCESS_DENIED
: return WSAEACCES
;
1121 case STATUS_TOO_MANY_OPENED_FILES
: return WSAEMFILE
;
1122 case STATUS_CANT_WAIT
: return WSAEWOULDBLOCK
;
1123 case STATUS_BUFFER_OVERFLOW
: return WSAEMSGSIZE
;
1124 case STATUS_NOT_SUPPORTED
: return WSAEOPNOTSUPP
;
1125 case STATUS_HOST_UNREACHABLE
: return WSAEHOSTUNREACH
;
1126 default: return RtlNtStatusToDosError( status
);
1130 /* set last error code from NT status without mapping WSA errors */
1131 static inline unsigned int set_error( unsigned int err
)
1135 err
= NtStatusToWSAError( err
);
1136 SetLastError( err
);
1141 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
1144 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
1149 static inline void release_sock_fd( SOCKET s
, int fd
)
1151 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
1154 static void _enable_event( HANDLE s
, unsigned int event
,
1155 unsigned int sstate
, unsigned int cstate
)
1157 SERVER_START_REQ( enable_socket_event
)
1159 req
->handle
= wine_server_obj_handle( s
);
1161 req
->sstate
= sstate
;
1162 req
->cstate
= cstate
;
1163 wine_server_call( req
);
1168 static DWORD
sock_is_blocking(SOCKET s
, BOOL
*ret
)
1171 SERVER_START_REQ( get_socket_event
)
1173 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1174 req
->service
= FALSE
;
1176 err
= NtStatusToWSAError( wine_server_call( req
));
1177 *ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
1183 static unsigned int _get_sock_mask(SOCKET s
)
1186 SERVER_START_REQ( get_socket_event
)
1188 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1189 req
->service
= FALSE
;
1191 wine_server_call( req
);
1198 static void _sync_sock_state(SOCKET s
)
1201 /* do a dummy wineserver request in order to let
1202 the wineserver run through its select loop once */
1203 sock_is_blocking(s
, &dummy
);
1206 static void _get_sock_errors(SOCKET s
, int *events
)
1208 SERVER_START_REQ( get_socket_event
)
1210 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1211 req
->service
= FALSE
;
1213 wine_server_set_reply( req
, events
, sizeof(int) * FD_MAX_EVENTS
);
1214 wine_server_call( req
);
1219 static int get_sock_error(SOCKET s
, unsigned int bit
)
1221 int events
[FD_MAX_EVENTS
];
1222 _get_sock_errors(s
, events
);
1226 static int _get_fd_type(int fd
)
1229 socklen_t optlen
= sizeof(sock_type
);
1230 getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char*) &sock_type
, &optlen
);
1234 static BOOL
set_dont_fragment(SOCKET s
, int level
, BOOL value
)
1238 if (level
== IPPROTO_IP
)
1241 optname
= IP_DONTFRAG
;
1242 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1243 optname
= IP_MTU_DISCOVER
;
1244 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1248 FIXME("IP_DONTFRAGMENT for IPv4 not supported in this platform\n");
1249 return TRUE
; /* fake success */
1254 #ifdef IPV6_DONTFRAG
1255 optname
= IPV6_DONTFRAG
;
1256 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1257 optname
= IPV6_MTU_DISCOVER
;
1258 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1262 FIXME("IP_DONTFRAGMENT for IPv6 not supported in this platform\n");
1263 return TRUE
; /* fake success */
1267 fd
= get_sock_fd(s
, 0, NULL
);
1268 if (fd
== -1) return FALSE
;
1270 if (!setsockopt(fd
, level
, optname
, &value
, sizeof(value
)))
1274 WSASetLastError(wsaErrno());
1278 release_sock_fd(s
, fd
);
1282 static BOOL
get_dont_fragment(SOCKET s
, int level
, BOOL
*out
)
1284 int fd
, optname
, value
, not_expected
;
1285 socklen_t optlen
= sizeof(value
);
1287 if (level
== IPPROTO_IP
)
1290 optname
= IP_DONTFRAG
;
1292 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
1293 optname
= IP_MTU_DISCOVER
;
1294 not_expected
= IP_PMTUDISC_DONT
;
1298 FIXME("IP_DONTFRAGMENT for IPv4 not supported in this platform\n");
1299 return TRUE
; /* fake success */
1304 #ifdef IPV6_DONTFRAG
1305 optname
= IPV6_DONTFRAG
;
1307 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DONT)
1308 optname
= IPV6_MTU_DISCOVER
;
1309 not_expected
= IPV6_PMTUDISC_DONT
;
1313 FIXME("IP_DONTFRAGMENT for IPv6 not supported in this platform\n");
1314 return TRUE
; /* fake success */
1318 fd
= get_sock_fd(s
, 0, NULL
);
1319 if (fd
== -1) return FALSE
;
1321 if (!getsockopt(fd
, level
, optname
, &value
, &optlen
))
1323 *out
= value
!= not_expected
;
1328 WSASetLastError(wsaErrno());
1332 release_sock_fd(s
, fd
);
1336 static struct per_thread_data
*get_per_thread_data(void)
1338 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
1339 /* lazy initialization */
1342 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
1343 NtCurrentTeb()->WinSockData
= ptb
;
1348 static void free_per_thread_data(void)
1350 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
1354 /* delete scratch buffers */
1355 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1356 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1357 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1358 HeapFree( GetProcessHeap(), 0, ptb
->fd_cache
);
1360 HeapFree( GetProcessHeap(), 0, ptb
);
1361 NtCurrentTeb()->WinSockData
= NULL
;
1364 /***********************************************************************
1365 * DllMain (WS2_32.init)
1367 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
1369 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
1370 switch (fdwReason
) {
1371 case DLL_PROCESS_ATTACH
:
1373 case DLL_PROCESS_DETACH
:
1374 if (fImpLoad
) break;
1375 free_per_thread_data();
1376 DeleteCriticalSection(&csWSgetXXXbyYYY
);
1378 case DLL_THREAD_DETACH
:
1379 free_per_thread_data();
1385 /***********************************************************************
1388 * Converts send/recv flags from Windows format.
1389 * Return the converted flag bits, unsupported flags remain unchanged.
1391 static int convert_flags(int flags
)
1394 if (!flags
) return 0;
1396 for (out
= i
= 0; flags
&& i
< ARRAY_SIZE(ws_flags_map
); i
++)
1398 if (ws_flags_map
[i
][0] & flags
)
1400 out
|= ws_flags_map
[i
][1];
1401 flags
&= ~ws_flags_map
[i
][0];
1406 FIXME("Unknown send/recv flags 0x%x, using anyway...\n", flags
);
1412 /***********************************************************************
1415 * Converts socket flags from Windows format.
1416 * Return 1 if converted, 0 if not (error).
1418 static int convert_sockopt(INT
*level
, INT
*optname
)
1424 *level
= SOL_SOCKET
;
1425 for(i
= 0; i
< ARRAY_SIZE(ws_sock_map
); i
++) {
1426 if( ws_sock_map
[i
][0] == *optname
)
1428 *optname
= ws_sock_map
[i
][1];
1432 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
1434 case WS_IPPROTO_TCP
:
1435 *level
= IPPROTO_TCP
;
1436 for(i
= 0; i
< ARRAY_SIZE(ws_tcp_map
); i
++) {
1437 if ( ws_tcp_map
[i
][0] == *optname
)
1439 *optname
= ws_tcp_map
[i
][1];
1443 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
1446 *level
= IPPROTO_IP
;
1447 for(i
= 0; i
< ARRAY_SIZE(ws_ip_map
); i
++) {
1448 if (ws_ip_map
[i
][0] == *optname
)
1450 *optname
= ws_ip_map
[i
][1];
1454 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
1456 case WS_IPPROTO_IPV6
:
1457 *level
= IPPROTO_IPV6
;
1458 for(i
= 0; i
< ARRAY_SIZE(ws_ipv6_map
); i
++) {
1459 if (ws_ipv6_map
[i
][0] == *optname
)
1461 *optname
= ws_ipv6_map
[i
][1];
1465 FIXME("Unknown IPPROTO_IPV6 optname 0x%x\n", *optname
);
1467 default: FIXME("Unimplemented or unknown socket level\n");
1472 /* ----------------------------------- Per-thread info (or per-process?) */
1474 static char *strdup_lower(const char *str
)
1477 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
1481 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
1484 else SetLastError(WSAENOBUFS
);
1488 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
1489 * from an fd and return the value converted to milli seconds
1490 * or 0 if there is an infinite time out */
1491 static inline INT64
get_rcvsnd_timeo( int fd
, BOOL is_recv
)
1494 socklen_t len
= sizeof(tv
);
1499 optname
= SO_RCVTIMEO
;
1505 optname
= SO_SNDTIMEO
;
1510 res
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
1513 return (UINT64
)tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
1516 /* utility: given an fd, will block until one of the events occurs */
1517 static inline int do_block( int fd
, int events
, int timeout
)
1523 pfd
.events
= events
;
1525 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
1536 convert_af_w2u(int windowsaf
) {
1539 for (i
= 0; i
< ARRAY_SIZE(ws_af_map
); i
++)
1540 if (ws_af_map
[i
][0] == windowsaf
)
1541 return ws_af_map
[i
][1];
1542 FIXME("unhandled Windows address family %d\n", windowsaf
);
1547 convert_af_u2w(int unixaf
) {
1550 for (i
= 0; i
< ARRAY_SIZE(ws_af_map
); i
++)
1551 if (ws_af_map
[i
][1] == unixaf
)
1552 return ws_af_map
[i
][0];
1553 FIXME("unhandled UNIX address family %d\n", unixaf
);
1558 convert_proto_w2u(int windowsproto
) {
1561 for (i
= 0; i
< ARRAY_SIZE(ws_proto_map
); i
++)
1562 if (ws_proto_map
[i
][0] == windowsproto
)
1563 return ws_proto_map
[i
][1];
1565 /* check for extended IPX */
1566 if (IS_IPX_PROTO(windowsproto
))
1567 return windowsproto
;
1569 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
1574 convert_proto_u2w(int unixproto
) {
1577 for (i
= 0; i
< ARRAY_SIZE(ws_proto_map
); i
++)
1578 if (ws_proto_map
[i
][1] == unixproto
)
1579 return ws_proto_map
[i
][0];
1581 /* if value is inside IPX range just return it - the kernel simply
1582 * echoes the value used in the socket() function */
1583 if (IS_IPX_PROTO(unixproto
))
1586 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
1591 convert_socktype_w2u(int windowssocktype
) {
1594 for (i
= 0; i
< ARRAY_SIZE(ws_socktype_map
); i
++)
1595 if (ws_socktype_map
[i
][0] == windowssocktype
)
1596 return ws_socktype_map
[i
][1];
1597 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
1602 convert_socktype_u2w(int unixsocktype
) {
1605 for (i
= 0; i
< ARRAY_SIZE(ws_socktype_map
); i
++)
1606 if (ws_socktype_map
[i
][1] == unixsocktype
)
1607 return ws_socktype_map
[i
][0];
1608 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
1612 static int convert_poll_w2u(int events
)
1615 for (i
= ret
= 0; events
&& i
< ARRAY_SIZE(ws_poll_map
); i
++)
1617 if (ws_poll_map
[i
][0] & events
)
1619 ret
|= ws_poll_map
[i
][1];
1620 events
&= ~ws_poll_map
[i
][0];
1625 FIXME("Unsupported WSAPoll() flags 0x%x\n", events
);
1629 static int convert_poll_u2w(int events
)
1632 for (i
= ret
= 0; events
&& i
< ARRAY_SIZE(ws_poll_map
); i
++)
1634 if (ws_poll_map
[i
][1] & events
)
1636 ret
|= ws_poll_map
[i
][0];
1637 events
&= ~ws_poll_map
[i
][1];
1642 FIXME("Unsupported poll() flags 0x%x\n", events
);
1646 static int set_ipx_packettype(int sock
, int ptype
)
1649 int fd
= get_sock_fd( sock
, 0, NULL
), ret
= 0;
1650 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", ptype
, fd
);
1652 if (fd
== -1) return SOCKET_ERROR
;
1654 /* We try to set the ipx type on ipx socket level. */
1656 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, &ptype
, sizeof(ptype
)) == -1)
1658 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
1664 /* Should we retrieve val using a getsockopt call and then
1665 * set the modified one? */
1667 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
1670 release_sock_fd( sock
, fd
);
1673 WARN("IPX support is not enabled, can't set packet type\n");
1674 return SOCKET_ERROR
;
1678 /* ----------------------------------- API -----
1680 * Init / cleanup / error checking.
1683 /***********************************************************************
1684 * WSAStartup (WS2_32.115)
1686 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
1688 TRACE("verReq=%x\n", wVersionRequested
);
1690 if (LOBYTE(wVersionRequested
) < 1)
1691 return WSAVERNOTSUPPORTED
;
1693 if (!lpWSAData
) return WSAEINVAL
;
1697 /* that's the whole of the negotiation for now */
1698 lpWSAData
->wVersion
= wVersionRequested
;
1699 /* return winsock information */
1700 lpWSAData
->wHighVersion
= 0x0202;
1701 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
1702 strcpy(lpWSAData
->szSystemStatus
, "Running" );
1703 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
1704 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
1705 /* don't do anything with lpWSAData->lpVendorInfo */
1706 /* (some apps don't allocate the space for this field) */
1708 TRACE("succeeded starts: %d\n", num_startup
);
1713 /***********************************************************************
1714 * WSACleanup (WS2_32.116)
1716 INT WINAPI
WSACleanup(void)
1720 TRACE("pending cleanups: %d\n", num_startup
);
1723 SetLastError(WSANOTINITIALISED
);
1724 return SOCKET_ERROR
;
1728 /***********************************************************************
1729 * WSAGetLastError (WS2_32.111)
1731 INT WINAPI
WSAGetLastError(void)
1733 return GetLastError();
1736 /***********************************************************************
1737 * WSASetLastError (WS2_32.112)
1739 void WINAPI
WSASetLastError(INT iError
) {
1740 SetLastError(iError
);
1743 static struct WS_hostent
*check_buffer_he(int size
)
1745 struct per_thread_data
* ptb
= get_per_thread_data();
1748 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
1749 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1751 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
1752 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
1753 return ptb
->he_buffer
;
1756 static struct WS_servent
*check_buffer_se(int size
)
1758 struct per_thread_data
* ptb
= get_per_thread_data();
1761 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
1762 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1764 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
1765 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
1766 return ptb
->se_buffer
;
1769 static struct WS_protoent
*check_buffer_pe(int size
)
1771 struct per_thread_data
* ptb
= get_per_thread_data();
1774 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
1775 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1777 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
1778 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
1779 return ptb
->pe_buffer
;
1782 /* ----------------------------------- i/o APIs */
1784 static inline BOOL
supported_pf(int pf
)
1804 static inline BOOL
supported_protocol(int protocol
)
1807 for (i
= 0; i
< ARRAY_SIZE(valid_protocols
); i
++)
1808 if (protocol
== valid_protocols
[i
])
1813 /**********************************************************************/
1815 /* Returns the length of the converted address if successful, 0 if it was too
1816 * small to start with or unknown family or invalid address buffer.
1818 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
1819 union generic_unix_sockaddr
*uaddr
)
1821 unsigned int uaddrlen
= 0;
1826 switch (wsaddr
->sa_family
)
1831 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
1832 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
1834 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
1837 uaddrlen
= sizeof(struct sockaddr_ipx
);
1838 memset( uaddr
, 0, uaddrlen
);
1839 uipx
->sipx_family
=AF_IPX
;
1840 uipx
->sipx_port
=wsipx
->sa_socket
;
1841 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
1844 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
1845 #ifdef IPX_FRAME_NONE
1846 uipx
->sipx_type
=IPX_FRAME_NONE
;
1852 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
1853 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
1855 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
1856 * scope_id, one without.
1858 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
1859 uaddrlen
= sizeof(struct sockaddr_in6
);
1860 memset( uaddr
, 0, uaddrlen
);
1861 uin6
->sin6_family
= AF_INET6
;
1862 uin6
->sin6_port
= win6
->sin6_port
;
1863 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
1864 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1865 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
1867 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1870 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
1874 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
1875 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
1877 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
1879 uaddrlen
= sizeof(struct sockaddr_in
);
1880 memset( uaddr
, 0, uaddrlen
);
1881 uin
->sin_family
= AF_INET
;
1882 uin
->sin_port
= win
->sin_port
;
1883 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
1888 struct sockaddr_irda
*uin
= (struct sockaddr_irda
*)uaddr
;
1889 const SOCKADDR_IRDA
*win
= (const SOCKADDR_IRDA
*)wsaddr
;
1891 if (wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1893 uaddrlen
= sizeof(struct sockaddr_irda
);
1894 memset( uaddr
, 0, uaddrlen
);
1895 uin
->sir_family
= AF_IRDA
;
1896 if (!strncmp( win
->irdaServiceName
, "LSAP-SEL", strlen( "LSAP-SEL" ) ))
1898 unsigned int lsap_sel
= 0;
1900 sscanf( win
->irdaServiceName
, "LSAP-SEL%u", &lsap_sel
);
1901 uin
->sir_lsap_sel
= lsap_sel
;
1905 uin
->sir_lsap_sel
= LSAP_ANY
;
1906 memcpy( uin
->sir_name
, win
->irdaServiceName
, 25 );
1908 memcpy( &uin
->sir_addr
, win
->irdaDeviceID
, sizeof(uin
->sir_addr
) );
1912 case WS_AF_UNSPEC
: {
1913 /* Try to determine the needed space by the passed windows sockaddr space */
1914 switch (wsaddrlen
) {
1915 default: /* likely an ipv4 address */
1916 case sizeof(struct WS_sockaddr_in
):
1917 uaddrlen
= sizeof(struct sockaddr_in
);
1920 case sizeof(struct WS_sockaddr_ipx
):
1921 uaddrlen
= sizeof(struct sockaddr_ipx
);
1925 case sizeof(SOCKADDR_IRDA
):
1926 uaddrlen
= sizeof(struct sockaddr_irda
);
1929 case sizeof(struct WS_sockaddr_in6
):
1930 case sizeof(struct WS_sockaddr_in6_old
):
1931 uaddrlen
= sizeof(struct sockaddr_in6
);
1934 memset( uaddr
, 0, uaddrlen
);
1938 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
1944 static BOOL
is_sockaddr_bound(const struct sockaddr
*uaddr
, int uaddrlen
)
1946 switch (uaddr
->sa_family
)
1951 static const struct sockaddr_ipx emptyAddr
;
1952 struct sockaddr_ipx
*ipx
= (struct sockaddr_ipx
*) uaddr
;
1953 return ipx
->sipx_port
1954 || memcmp(&ipx
->sipx_network
, &emptyAddr
.sipx_network
, sizeof(emptyAddr
.sipx_network
))
1955 || memcmp(&ipx
->sipx_node
, &emptyAddr
.sipx_node
, sizeof(emptyAddr
.sipx_node
));
1960 static const struct sockaddr_in6 emptyAddr
;
1961 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) uaddr
;
1962 return in6
->sin6_port
|| memcmp(&in6
->sin6_addr
, &emptyAddr
.sin6_addr
, sizeof(struct in6_addr
));
1966 static const struct sockaddr_in emptyAddr
;
1967 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) uaddr
;
1968 return in
->sin_port
|| memcmp(&in
->sin_addr
, &emptyAddr
.sin_addr
, sizeof(struct in_addr
));
1973 FIXME("unknown address family %d\n", uaddr
->sa_family
);
1978 /* Returns -1 if getsockname fails, 0 if not bound, 1 otherwise */
1979 static int is_fd_bound(int fd
, union generic_unix_sockaddr
*uaddr
, socklen_t
*uaddrlen
)
1981 union generic_unix_sockaddr inaddr
;
1985 if (!uaddr
) uaddr
= &inaddr
;
1986 if (!uaddrlen
) uaddrlen
= &inlen
;
1988 *uaddrlen
= sizeof(inaddr
);
1989 res
= getsockname(fd
, &uaddr
->addr
, uaddrlen
);
1990 if (!res
) res
= is_sockaddr_bound(&uaddr
->addr
, *uaddrlen
);
1994 /* Returns 0 if successful, -1 if the buffer is too small */
1995 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
1999 switch(uaddr
->sa_family
)
2004 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
2005 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
2008 switch (*wsaddrlen
) /* how much can we copy? */
2012 *wsaddrlen
= sizeof(*wsipx
);
2013 wsipx
->sa_socket
=uipx
->sipx_port
;
2017 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
2025 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
2031 wsipx
->sa_family
=WS_AF_IPX
;
2043 const struct sockaddr_irda
*uin
= (const struct sockaddr_irda
*)uaddr
;
2044 SOCKADDR_IRDA
*win
= (SOCKADDR_IRDA
*)wsaddr
;
2046 if (*wsaddrlen
< sizeof(SOCKADDR_IRDA
))
2048 win
->irdaAddressFamily
= WS_AF_IRDA
;
2049 memcpy( win
->irdaDeviceID
, &uin
->sir_addr
, sizeof(win
->irdaDeviceID
) );
2050 if (uin
->sir_lsap_sel
!= LSAP_ANY
)
2051 sprintf( win
->irdaServiceName
, "LSAP-SEL%u", uin
->sir_lsap_sel
);
2053 memcpy( win
->irdaServiceName
, uin
->sir_name
,
2054 sizeof(win
->irdaServiceName
) );
2059 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
2060 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
2062 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
2064 win6old
->sin6_family
= WS_AF_INET6
;
2065 win6old
->sin6_port
= uin6
->sin6_port
;
2066 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
2067 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
2068 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2069 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
2070 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
2071 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
2072 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
2075 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
2077 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
2082 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
2083 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
2085 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
2087 win
->sin_family
= WS_AF_INET
;
2088 win
->sin_port
= uin
->sin_port
;
2089 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
2090 memset(win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
2091 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
2095 memset(wsaddr
,0,*wsaddrlen
);
2099 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
2105 static INT
WS_DuplicateSocket(BOOL unicode
, SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
2109 WSAPROTOCOL_INFOW infow
;
2111 TRACE("(unicode %d, socket %04lx, processid %x, buffer %p)\n",
2112 unicode
, s
, dwProcessId
, lpProtocolInfo
);
2114 if (!ws_protocol_info(s
, unicode
, &infow
, &size
))
2115 return SOCKET_ERROR
;
2117 if (!(hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
)))
2119 SetLastError(WSAEINVAL
);
2120 return SOCKET_ERROR
;
2123 if (!lpProtocolInfo
)
2125 CloseHandle(hProcess
);
2126 SetLastError(WSAEFAULT
);
2127 return SOCKET_ERROR
;
2130 /* I don't know what the real Windoze does next, this is a hack */
2131 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
2132 * the target use the global duplicate, or we could copy a reference to us to the structure
2133 * and let the target duplicate it from us, but let's do it as simple as possible */
2134 memcpy(lpProtocolInfo
, &infow
, size
);
2135 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
2136 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwServiceFlags3
,
2137 0, FALSE
, DUPLICATE_SAME_ACCESS
);
2138 CloseHandle(hProcess
);
2139 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
2143 /*****************************************************************************
2144 * WS_EnterSingleProtocolW [internal]
2146 * enters the protocol information of one given protocol into the given
2150 * TRUE if a protocol was entered into the buffer.
2153 * - only implemented for IPX, SPX, SPXII, TCP, UDP
2154 * - there is no check that the operating system supports the returned
2157 static BOOL
WS_EnterSingleProtocolW( INT protocol
, WSAPROTOCOL_INFOW
* info
)
2159 memset( info
, 0, sizeof(WSAPROTOCOL_INFOW
) );
2160 info
->iProtocol
= protocol
;
2164 case WS_IPPROTO_TCP
:
2165 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_EXPEDITED_DATA
|
2166 XP1_GRACEFUL_CLOSE
| XP1_GUARANTEED_ORDER
|
2167 XP1_GUARANTEED_DELIVERY
;
2168 info
->ProviderId
= ProviderIdIP
;
2169 info
->dwCatalogEntryId
= 0x3e9;
2170 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2171 info
->ProtocolChain
.ChainLen
= 1;
2173 info
->iAddressFamily
= WS_AF_INET
;
2174 info
->iMaxSockAddr
= 0x10;
2175 info
->iMinSockAddr
= 0x10;
2176 info
->iSocketType
= WS_SOCK_STREAM
;
2177 strcpyW( info
->szProtocol
, NameTcpW
);
2180 case WS_IPPROTO_UDP
:
2181 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_SUPPORT_BROADCAST
|
2182 XP1_SUPPORT_MULTIPOINT
| XP1_MESSAGE_ORIENTED
|
2184 info
->ProviderId
= ProviderIdIP
;
2185 info
->dwCatalogEntryId
= 0x3ea;
2186 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2187 info
->ProtocolChain
.ChainLen
= 1;
2189 info
->iAddressFamily
= WS_AF_INET
;
2190 info
->iMaxSockAddr
= 0x10;
2191 info
->iMinSockAddr
= 0x10;
2192 info
->iSocketType
= WS_SOCK_DGRAM
;
2193 info
->dwMessageSize
= 0xffbb;
2194 strcpyW( info
->szProtocol
, NameUdpW
);
2197 case WS_NSPROTO_IPX
:
2198 info
->dwServiceFlags1
= XP1_PARTIAL_MESSAGE
| XP1_SUPPORT_BROADCAST
|
2199 XP1_SUPPORT_MULTIPOINT
| XP1_MESSAGE_ORIENTED
|
2201 info
->ProviderId
= ProviderIdIPX
;
2202 info
->dwCatalogEntryId
= 0x406;
2203 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2204 info
->ProtocolChain
.ChainLen
= 1;
2206 info
->iAddressFamily
= WS_AF_IPX
;
2207 info
->iMaxSockAddr
= 0x10;
2208 info
->iMinSockAddr
= 0x0e;
2209 info
->iSocketType
= WS_SOCK_DGRAM
;
2210 info
->iProtocolMaxOffset
= 0xff;
2211 info
->dwMessageSize
= 0x240;
2212 strcpyW( info
->szProtocol
, NameIpxW
);
2215 case WS_NSPROTO_SPX
:
2216 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_PSEUDO_STREAM
|
2217 XP1_MESSAGE_ORIENTED
| XP1_GUARANTEED_ORDER
|
2218 XP1_GUARANTEED_DELIVERY
;
2219 info
->ProviderId
= ProviderIdSPX
;
2220 info
->dwCatalogEntryId
= 0x407;
2221 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2222 info
->ProtocolChain
.ChainLen
= 1;
2224 info
->iAddressFamily
= WS_AF_IPX
;
2225 info
->iMaxSockAddr
= 0x10;
2226 info
->iMinSockAddr
= 0x0e;
2227 info
->iSocketType
= WS_SOCK_SEQPACKET
;
2228 info
->dwMessageSize
= 0xffffffff;
2229 strcpyW( info
->szProtocol
, NameSpxW
);
2232 case WS_NSPROTO_SPXII
:
2233 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_GRACEFUL_CLOSE
|
2234 XP1_PSEUDO_STREAM
| XP1_MESSAGE_ORIENTED
|
2235 XP1_GUARANTEED_ORDER
| XP1_GUARANTEED_DELIVERY
;
2236 info
->ProviderId
= ProviderIdSPX
;
2237 info
->dwCatalogEntryId
= 0x409;
2238 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2239 info
->ProtocolChain
.ChainLen
= 1;
2241 info
->iAddressFamily
= WS_AF_IPX
;
2242 info
->iMaxSockAddr
= 0x10;
2243 info
->iMinSockAddr
= 0x0e;
2244 info
->iSocketType
= WS_SOCK_SEQPACKET
;
2245 info
->dwMessageSize
= 0xffffffff;
2246 strcpyW( info
->szProtocol
, NameSpxIIW
);
2250 FIXME("unknown Protocol <0x%08x>\n", protocol
);
2256 /*****************************************************************************
2257 * WS_EnterSingleProtocolA [internal]
2259 * see function WS_EnterSingleProtocolW
2262 static BOOL
WS_EnterSingleProtocolA( INT protocol
, WSAPROTOCOL_INFOA
* info
)
2264 WSAPROTOCOL_INFOW infow
;
2266 memset( info
, 0, sizeof(WSAPROTOCOL_INFOA
) );
2268 ret
= WS_EnterSingleProtocolW( protocol
, &infow
);
2271 /* convert the structure from W to A */
2272 memcpy( info
, &infow
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
2273 WideCharToMultiByte( CP_ACP
, 0, infow
.szProtocol
, -1,
2274 info
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
);
2280 static INT
WS_EnumProtocols( BOOL unicode
, const INT
*protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
)
2282 INT i
= 0, items
= 0;
2286 LPWSAPROTOCOL_INFOA a
;
2287 LPWSAPROTOCOL_INFOW w
;
2291 if (!protocols
) protocols
= valid_protocols
;
2293 while (protocols
[i
])
2295 if(supported_protocol(protocols
[i
++]))
2299 size
= items
* (unicode
? sizeof(WSAPROTOCOL_INFOW
) : sizeof(WSAPROTOCOL_INFOA
));
2301 TRACE("unicode %d, protocols %p, buffer %p, length %p %d, items %d, required %d\n",
2302 unicode
, protocols
, buffer
, len
, len
? *len
: 0, items
, size
);
2304 if (*len
< size
|| !buffer
)
2307 SetLastError(WSAENOBUFS
);
2308 return SOCKET_ERROR
;
2311 for (i
= items
= 0; protocols
[i
]; i
++)
2313 if (!supported_protocol(protocols
[i
])) continue;
2316 if (WS_EnterSingleProtocolW( protocols
[i
], &info
.w
[items
] ))
2321 if (WS_EnterSingleProtocolA( protocols
[i
], &info
.a
[items
] ))
2328 static BOOL
ws_protocol_info(SOCKET s
, int unicode
, WSAPROTOCOL_INFOW
*buffer
, int *size
)
2335 *size
= unicode
? sizeof(WSAPROTOCOL_INFOW
) : sizeof(WSAPROTOCOL_INFOA
);
2336 memset(buffer
, 0, *size
);
2338 SERVER_START_REQ( get_socket_info
)
2340 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
2341 status
= wine_server_call( req
);
2344 address_family
= convert_af_u2w(reply
->family
);
2345 socket_type
= convert_socktype_u2w(reply
->type
);
2346 protocol
= convert_proto_u2w(reply
->protocol
);
2353 unsigned int err
= NtStatusToWSAError( status
);
2354 SetLastError( err
== WSAEBADF
? WSAENOTSOCK
: err
);
2359 WS_EnterSingleProtocolW( protocol
, buffer
);
2361 WS_EnterSingleProtocolA( protocol
, (WSAPROTOCOL_INFOA
*)buffer
);
2362 buffer
->iAddressFamily
= address_family
;
2363 buffer
->iSocketType
= socket_type
;
2364 buffer
->iProtocol
= protocol
;
2369 /**************************************************************************
2370 * Functions for handling overlapped I/O
2371 **************************************************************************/
2373 /* user APC called upon async completion */
2374 static void WINAPI
ws2_async_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
2376 struct ws2_async
*wsa
= arg
;
2378 if (wsa
->completion_func
) wsa
->completion_func( NtStatusToWSAError(iosb
->u
.Status
),
2379 iosb
->Information
, wsa
->user_overlapped
,
2381 release_async_io( &wsa
->io
);
2384 /***********************************************************************
2385 * WS2_recv (INTERNAL)
2387 * Workhorse for both synchronous and asynchronous recv() operations.
2389 static int WS2_recv( int fd
, struct ws2_async
*wsa
, int flags
)
2391 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2395 union generic_unix_sockaddr unix_sockaddr
;
2398 hdr
.msg_name
= NULL
;
2402 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
2403 hdr
.msg_name
= &unix_sockaddr
;
2406 hdr
.msg_namelen
= 0;
2408 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
2409 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
2410 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2411 hdr
.msg_accrights
= NULL
;
2412 hdr
.msg_accrightslen
= 0;
2414 hdr
.msg_control
= pktbuf
;
2415 hdr
.msg_controllen
= sizeof(pktbuf
);
2419 while ((n
= __wine_locked_recvmsg( fd
, &hdr
, flags
)) == -1)
2425 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2428 ERR("Message control headers cannot be properly supported on this system.\n");
2429 wsa
->control
->len
= 0;
2432 if (wsa
->control
&& !convert_control_headers(&hdr
, wsa
->control
))
2434 WARN("Application passed insufficient room for control headers.\n");
2435 *wsa
->lpFlags
|= WS_MSG_CTRUNC
;
2441 /* if this socket is connected and lpFrom is not NULL, Linux doesn't give us
2442 * msg_name and msg_namelen from recvmsg, but it does set msg_namelen to zero.
2444 * quoting linux 2.6 net/ipv4/tcp.c:
2445 * "According to UNIX98, msg_name/msg_namelen are ignored
2446 * on connected socket. I was just happy when found this 8) --ANK"
2448 * likewise MSDN says that lpFrom and lpFromlen are ignored for
2449 * connection-oriented sockets, so don't try to update lpFrom.
2451 if (wsa
->addr
&& hdr
.msg_namelen
)
2452 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, wsa
->addr
, wsa
->addrlen
.ptr
);
2457 /***********************************************************************
2458 * WS2_async_recv (INTERNAL)
2460 * Handler for overlapped recv() operations.
2462 static NTSTATUS
WS2_async_recv( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
2464 struct ws2_async
*wsa
= user
;
2469 case STATUS_ALERTED
:
2470 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
2473 result
= WS2_recv( fd
, wsa
, convert_flags(wsa
->flags
) );
2474 wine_server_release_fd( wsa
->hSocket
, fd
);
2477 status
= STATUS_SUCCESS
;
2478 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
2482 if (errno
== EAGAIN
)
2484 status
= STATUS_PENDING
;
2485 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
2490 status
= wsaErrStatus();
2495 if (status
!= STATUS_PENDING
)
2497 iosb
->u
.Status
= status
;
2498 iosb
->Information
= result
;
2499 if (!wsa
->completion_func
)
2500 release_async_io( &wsa
->io
);
2505 /***********************************************************************
2506 * WS2_async_accept_recv (INTERNAL)
2508 * This function is used to finish the read part of an accept request. It is
2509 * needed to place the completion on the correct socket (listener).
2511 static NTSTATUS
WS2_async_accept_recv( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
2513 struct ws2_accept_async
*wsa
= user
;
2515 status
= WS2_async_recv( wsa
->read
, iosb
, status
);
2516 if (status
== STATUS_PENDING
)
2520 WS_AddCompletion( HANDLE2SOCKET(wsa
->listen_socket
), wsa
->cvalue
, iosb
->u
.Status
, iosb
->Information
, TRUE
);
2522 release_async_io( &wsa
->io
);
2526 /***********************************************************************
2527 * WS2_async_accept (INTERNAL)
2529 * This is the function called to satisfy the AcceptEx callback
2531 static NTSTATUS
WS2_async_accept( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
2533 struct ws2_accept_async
*wsa
= user
;
2537 TRACE("status: 0x%x listen: %p, accept: %p\n", status
, wsa
->listen_socket
, wsa
->accept_socket
);
2539 if (status
== STATUS_ALERTED
)
2541 SERVER_START_REQ( accept_into_socket
)
2543 req
->lhandle
= wine_server_obj_handle( wsa
->listen_socket
);
2544 req
->ahandle
= wine_server_obj_handle( wsa
->accept_socket
);
2545 status
= wine_server_call( req
);
2549 if (NtStatusToWSAError( status
) == WSAEWOULDBLOCK
)
2550 return STATUS_PENDING
;
2552 if (status
== STATUS_INVALID_HANDLE
)
2554 FIXME("AcceptEx accepting socket closed but request was not cancelled\n");
2555 status
= STATUS_CANCELLED
;
2558 else if (status
== STATUS_HANDLES_CLOSED
)
2559 status
= STATUS_CANCELLED
; /* strange windows behavior */
2561 if (status
!= STATUS_SUCCESS
)
2564 /* WS2 Spec says size param is extra 16 bytes long...what do we put in it? */
2565 addr
= ((char *)wsa
->buf
) + wsa
->data_len
;
2566 len
= wsa
->local_len
- sizeof(int);
2567 WS_getsockname(HANDLE2SOCKET(wsa
->accept_socket
),
2568 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
2571 addr
+= wsa
->local_len
;
2572 len
= wsa
->remote_len
- sizeof(int);
2573 WS_getpeername(HANDLE2SOCKET(wsa
->accept_socket
),
2574 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
2580 wsa
->io
.callback
= WS2_async_accept_recv
;
2581 status
= register_async( ASYNC_TYPE_READ
, wsa
->accept_socket
, &wsa
->io
,
2582 wsa
->user_overlapped
->hEvent
, NULL
, NULL
, iosb
);
2584 if (status
!= STATUS_PENDING
)
2587 /* The APC has finished but no completion should be sent for the operation yet, additional processing
2588 * needs to be performed by WS2_async_accept_recv() first. */
2589 return STATUS_MORE_PROCESSING_REQUIRED
;
2592 iosb
->u
.Status
= status
;
2593 iosb
->Information
= 0;
2595 if (wsa
->read
) release_async_io( &wsa
->read
->io
);
2596 release_async_io( &wsa
->io
);
2600 /***********************************************************************
2601 * WS2_send (INTERNAL)
2603 * Workhorse for both synchronous and asynchronous send() operations.
2605 static int WS2_send( int fd
, struct ws2_async
*wsa
, int flags
)
2608 union generic_unix_sockaddr unix_addr
;
2611 hdr
.msg_name
= NULL
;
2612 hdr
.msg_namelen
= 0;
2616 hdr
.msg_name
= &unix_addr
;
2617 hdr
.msg_namelen
= ws_sockaddr_ws2u( wsa
->addr
, wsa
->addrlen
.val
, &unix_addr
);
2618 if ( !hdr
.msg_namelen
)
2624 #if defined(HAS_IPX) && defined(SOL_IPX)
2625 if(wsa
->addr
->sa_family
== WS_AF_IPX
)
2627 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
2629 socklen_t len
= sizeof(int);
2631 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
2632 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
2633 * the packet type and then we can retrieve it using getsockopt. After that we can set the
2634 * ipx type in the sockaddr_opx structure with the stored value.
2636 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
2637 uipx
->sipx_type
= val
;
2642 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
2643 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
2644 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2645 hdr
.msg_accrights
= NULL
;
2646 hdr
.msg_accrightslen
= 0;
2648 hdr
.msg_control
= NULL
;
2649 hdr
.msg_controllen
= 0;
2653 while ((ret
= sendmsg(fd
, &hdr
, flags
)) == -1)
2655 if (errno
== EISCONN
)
2658 hdr
.msg_namelen
= 0;
2666 while (wsa
->first_iovec
< wsa
->n_iovecs
&& wsa
->iovec
[wsa
->first_iovec
].iov_len
<= n
)
2667 n
-= wsa
->iovec
[wsa
->first_iovec
++].iov_len
;
2668 if (wsa
->first_iovec
< wsa
->n_iovecs
)
2670 wsa
->iovec
[wsa
->first_iovec
].iov_base
= (char*)wsa
->iovec
[wsa
->first_iovec
].iov_base
+ n
;
2671 wsa
->iovec
[wsa
->first_iovec
].iov_len
-= n
;
2676 /***********************************************************************
2677 * WS2_async_send (INTERNAL)
2679 * Handler for overlapped send() operations.
2681 static NTSTATUS
WS2_async_send( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
2683 struct ws2_async
*wsa
= user
;
2688 case STATUS_ALERTED
:
2689 if ( wsa
->n_iovecs
<= wsa
->first_iovec
)
2692 status
= STATUS_SUCCESS
;
2695 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
2698 /* check to see if the data is ready (non-blocking) */
2699 result
= WS2_send( fd
, wsa
, convert_flags(wsa
->flags
) );
2700 wine_server_release_fd( wsa
->hSocket
, fd
);
2704 if (wsa
->first_iovec
< wsa
->n_iovecs
)
2705 status
= STATUS_PENDING
;
2707 status
= STATUS_SUCCESS
;
2709 iosb
->Information
+= result
;
2711 else if (errno
== EAGAIN
)
2713 status
= STATUS_PENDING
;
2717 status
= wsaErrStatus();
2721 if (status
!= STATUS_PENDING
)
2723 iosb
->u
.Status
= status
;
2724 if (!wsa
->completion_func
)
2725 release_async_io( &wsa
->io
);
2730 /***********************************************************************
2731 * WS2_async_shutdown (INTERNAL)
2733 * Handler for shutdown() operations on overlapped sockets.
2735 static NTSTATUS
WS2_async_shutdown( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
2737 struct ws2_async_shutdown
*wsa
= user
;
2742 case STATUS_ALERTED
:
2743 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
2746 switch ( wsa
->type
)
2748 case ASYNC_TYPE_READ
: err
= shutdown( fd
, 0 ); break;
2749 case ASYNC_TYPE_WRITE
: err
= shutdown( fd
, 1 ); break;
2751 status
= err
? wsaErrStatus() : STATUS_SUCCESS
;
2752 wine_server_release_fd( wsa
->hSocket
, fd
);
2755 iosb
->u
.Status
= status
;
2756 iosb
->Information
= 0;
2757 release_async_io( &wsa
->io
);
2761 /***********************************************************************
2762 * WS2_register_async_shutdown (INTERNAL)
2764 * Helper function for WS_shutdown() on overlapped sockets.
2766 static int WS2_register_async_shutdown( SOCKET s
, int type
)
2768 struct ws2_async_shutdown
*wsa
;
2771 TRACE("socket %04lx type %d\n", s
, type
);
2773 wsa
= (struct ws2_async_shutdown
*)alloc_async_io( sizeof(*wsa
), WS2_async_shutdown
);
2777 wsa
->hSocket
= SOCKET2HANDLE(s
);
2780 status
= register_async( type
, wsa
->hSocket
, &wsa
->io
, 0, NULL
, NULL
, &wsa
->iosb
);
2781 if (status
!= STATUS_PENDING
)
2783 HeapFree( GetProcessHeap(), 0, wsa
);
2784 return NtStatusToWSAError( status
);
2789 /***********************************************************************
2792 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
, int *addrlen32
)
2799 TRACE("socket %04lx\n", s
);
2800 err
= sock_is_blocking(s
, &is_blocking
);
2806 /* try accepting first (if there is a deferred connection) */
2807 SERVER_START_REQ( accept_socket
)
2809 req
->lhandle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
2810 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
2811 req
->attributes
= OBJ_INHERIT
;
2812 err
= NtStatusToWSAError( wine_server_call( req
));
2813 as
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
2818 if (addr
&& addrlen32
&& WS_getpeername(as
, addr
, addrlen32
))
2821 return SOCKET_ERROR
;
2823 TRACE("\taccepted %04lx\n", as
);
2826 if (!is_blocking
) break;
2827 if (err
!= WSAEWOULDBLOCK
) break;
2828 fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2830 do_block(fd
, POLLIN
, -1);
2831 _sync_sock_state(s
); /* let wineserver notice connection */
2832 release_sock_fd( s
, fd
);
2836 WARN(" -> ERROR %d\n", err
);
2838 return INVALID_SOCKET
;
2841 /***********************************************************************
2844 static BOOL WINAPI
WS2_AcceptEx(SOCKET listener
, SOCKET acceptor
, PVOID dest
, DWORD dest_len
,
2845 DWORD local_addr_len
, DWORD rem_addr_len
, LPDWORD received
,
2846 LPOVERLAPPED overlapped
)
2849 struct ws2_accept_async
*wsa
;
2852 TRACE("(%04lx, %04lx, %p, %d, %d, %d, %p, %p)\n", listener
, acceptor
, dest
, dest_len
, local_addr_len
,
2853 rem_addr_len
, received
, overlapped
);
2857 SetLastError(WSA_INVALID_PARAMETER
);
2860 overlapped
->Internal
= STATUS_PENDING
;
2864 SetLastError(WSAEINVAL
);
2870 SetLastError(WSAEFAULT
);
2874 fd
= get_sock_fd( listener
, FILE_READ_DATA
, NULL
);
2877 SetLastError(WSAENOTSOCK
);
2880 release_sock_fd( listener
, fd
);
2882 fd
= get_sock_fd( acceptor
, FILE_READ_DATA
, NULL
);
2885 SetLastError(WSAENOTSOCK
);
2888 release_sock_fd( acceptor
, fd
);
2890 wsa
= (struct ws2_accept_async
*)alloc_async_io( sizeof(*wsa
), WS2_async_accept
);
2893 SetLastError(WSAEFAULT
);
2897 wsa
->listen_socket
= SOCKET2HANDLE(listener
);
2898 wsa
->accept_socket
= SOCKET2HANDLE(acceptor
);
2899 wsa
->user_overlapped
= overlapped
;
2900 wsa
->cvalue
= !((ULONG_PTR
)overlapped
->hEvent
& 1) ? (ULONG_PTR
)overlapped
: 0;
2902 wsa
->data_len
= dest_len
;
2903 wsa
->local_len
= local_addr_len
;
2904 wsa
->remote_len
= rem_addr_len
;
2909 /* set up a read request if we need it */
2910 wsa
->read
= (struct ws2_async
*)alloc_async_io( offsetof(struct ws2_async
, iovec
[1]), WS2_async_accept_recv
);
2913 HeapFree( GetProcessHeap(), 0, wsa
);
2914 SetLastError(WSAEFAULT
);
2918 wsa
->read
->hSocket
= wsa
->accept_socket
;
2919 wsa
->read
->flags
= 0;
2920 wsa
->read
->lpFlags
= &wsa
->read
->flags
;
2921 wsa
->read
->addr
= NULL
;
2922 wsa
->read
->addrlen
.ptr
= NULL
;
2923 wsa
->read
->control
= NULL
;
2924 wsa
->read
->n_iovecs
= 1;
2925 wsa
->read
->first_iovec
= 0;
2926 wsa
->read
->completion_func
= NULL
;
2927 wsa
->read
->iovec
[0].iov_base
= wsa
->buf
;
2928 wsa
->read
->iovec
[0].iov_len
= wsa
->data_len
;
2931 status
= register_async( ASYNC_TYPE_READ
, SOCKET2HANDLE(listener
), &wsa
->io
,
2932 overlapped
->hEvent
, NULL
, (void *)wsa
->cvalue
, (IO_STATUS_BLOCK
*)overlapped
);
2934 if(status
!= STATUS_PENDING
)
2936 HeapFree( GetProcessHeap(), 0, wsa
->read
);
2937 HeapFree( GetProcessHeap(), 0, wsa
);
2940 SetLastError( NtStatusToWSAError(status
) );
2944 /***********************************************************************
2945 * WS2_ReadFile (INTERNAL)
2947 * Perform an APC-safe ReadFile operation
2949 static NTSTATUS
WS2_ReadFile(HANDLE hFile
, PIO_STATUS_BLOCK io_status
, char* buffer
, ULONG length
,
2950 PLARGE_INTEGER offset
)
2952 int result
= -1, unix_handle
;
2953 unsigned int options
;
2956 TRACE( "(%p,%p,0x%08x)\n", hFile
, buffer
,length
);
2958 status
= wine_server_handle_to_fd( hFile
, FILE_READ_DATA
, &unix_handle
, &options
);
2959 if (status
) return status
;
2961 while (result
== -1)
2963 if (offset
->QuadPart
!= FILE_USE_FILE_POINTER_POSITION
)
2964 result
= pread( unix_handle
, buffer
, length
, offset
->QuadPart
);
2966 result
= read( unix_handle
, buffer
, length
);
2972 status
= (length
? STATUS_END_OF_FILE
: STATUS_SUCCESS
);
2973 else if (result
!= -1)
2974 status
= STATUS_SUCCESS
;
2975 else if (errno
!= EAGAIN
)
2976 status
= wsaErrStatus();
2978 status
= STATUS_PENDING
;
2980 wine_server_release_fd( hFile
, unix_handle
);
2981 TRACE("= 0x%08x (%d)\n", status
, result
);
2982 if (status
== STATUS_SUCCESS
|| status
== STATUS_END_OF_FILE
)
2984 io_status
->u
.Status
= status
;
2985 io_status
->Information
= result
;
2991 /***********************************************************************
2992 * WS2_transmitfile_getbuffer (INTERNAL)
2994 * Pick the appropriate buffer for a TransmitFile send operation.
2996 static NTSTATUS
WS2_transmitfile_getbuffer( int fd
, struct ws2_transmitfile_async
*wsa
)
2998 /* send any incomplete writes from a previous iteration */
2999 if (wsa
->write
.first_iovec
< wsa
->write
.n_iovecs
)
3000 return STATUS_PENDING
;
3002 /* process the header (if applicable) */
3003 if (wsa
->buffers
.Head
)
3005 wsa
->write
.first_iovec
= 0;
3006 wsa
->write
.n_iovecs
= 1;
3007 wsa
->write
.iovec
[0].iov_base
= wsa
->buffers
.Head
;
3008 wsa
->write
.iovec
[0].iov_len
= wsa
->buffers
.HeadLength
;
3009 wsa
->buffers
.Head
= NULL
;
3010 return STATUS_PENDING
;
3013 /* process the main file */
3016 DWORD bytes_per_send
= wsa
->bytes_per_send
;
3017 IO_STATUS_BLOCK iosb
;
3020 iosb
.Information
= 0;
3021 /* when the size of the transfer is limited ensure that we don't go past that limit */
3022 if (wsa
->file_bytes
!= 0)
3023 bytes_per_send
= min(bytes_per_send
, wsa
->file_bytes
- wsa
->file_read
);
3024 status
= WS2_ReadFile( wsa
->file
, &iosb
, wsa
->buffer
, bytes_per_send
, &wsa
->offset
);
3025 if (wsa
->offset
.QuadPart
!= FILE_USE_FILE_POINTER_POSITION
)
3026 wsa
->offset
.QuadPart
+= iosb
.Information
;
3027 if (status
== STATUS_END_OF_FILE
)
3028 wsa
->file
= NULL
; /* continue on to the footer */
3029 else if (status
!= STATUS_SUCCESS
)
3033 if (iosb
.Information
)
3035 wsa
->write
.first_iovec
= 0;
3036 wsa
->write
.n_iovecs
= 1;
3037 wsa
->write
.iovec
[0].iov_base
= wsa
->buffer
;
3038 wsa
->write
.iovec
[0].iov_len
= iosb
.Information
;
3039 wsa
->file_read
+= iosb
.Information
;
3042 if (wsa
->file_bytes
!= 0 && wsa
->file_read
>= wsa
->file_bytes
)
3045 return STATUS_PENDING
;
3049 /* send the footer (if applicable) */
3050 if (wsa
->buffers
.Tail
)
3052 wsa
->write
.first_iovec
= 0;
3053 wsa
->write
.n_iovecs
= 1;
3054 wsa
->write
.iovec
[0].iov_base
= wsa
->buffers
.Tail
;
3055 wsa
->write
.iovec
[0].iov_len
= wsa
->buffers
.TailLength
;
3056 wsa
->buffers
.Tail
= NULL
;
3057 return STATUS_PENDING
;
3060 return STATUS_SUCCESS
;
3063 /***********************************************************************
3064 * WS2_transmitfile_base (INTERNAL)
3066 * Shared implementation for both synchronous and asynchronous TransmitFile.
3068 static NTSTATUS
WS2_transmitfile_base( int fd
, struct ws2_transmitfile_async
*wsa
)
3072 status
= WS2_transmitfile_getbuffer( fd
, wsa
);
3073 if (status
== STATUS_PENDING
)
3075 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)wsa
->write
.user_overlapped
;
3078 n
= WS2_send( fd
, &wsa
->write
, convert_flags(wsa
->write
.flags
) );
3081 if (iosb
) iosb
->Information
+= n
;
3083 else if (errno
!= EAGAIN
)
3084 return wsaErrStatus();
3090 /***********************************************************************
3091 * WS2_async_transmitfile (INTERNAL)
3093 * Asynchronous callback for overlapped TransmitFile operations.
3095 static NTSTATUS
WS2_async_transmitfile( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
3097 struct ws2_transmitfile_async
*wsa
= user
;
3100 if (status
== STATUS_ALERTED
)
3102 if (!(status
= wine_server_handle_to_fd( wsa
->write
.hSocket
, FILE_WRITE_DATA
, &fd
, NULL
)))
3104 status
= WS2_transmitfile_base( fd
, wsa
);
3105 wine_server_release_fd( wsa
->write
.hSocket
, fd
);
3107 if (status
== STATUS_PENDING
)
3111 iosb
->u
.Status
= status
;
3112 release_async_io( &wsa
->io
);
3116 /***********************************************************************
3119 static BOOL WINAPI
WS2_TransmitFile( SOCKET s
, HANDLE h
, DWORD file_bytes
, DWORD bytes_per_send
,
3120 LPOVERLAPPED overlapped
, LPTRANSMIT_FILE_BUFFERS buffers
,
3123 union generic_unix_sockaddr uaddr
;
3124 socklen_t uaddrlen
= sizeof(uaddr
);
3125 struct ws2_transmitfile_async
*wsa
;
3129 TRACE("(%lx, %p, %d, %d, %p, %p, %d)\n", s
, h
, file_bytes
, bytes_per_send
, overlapped
,
3132 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, NULL
);
3135 WSASetLastError( WSAENOTSOCK
);
3138 if (getpeername( fd
, &uaddr
.addr
, &uaddrlen
) != 0)
3140 release_sock_fd( s
, fd
);
3141 WSASetLastError( WSAENOTCONN
);
3145 FIXME("Flags are not currently supported (0x%x).\n", flags
);
3147 if (h
&& GetFileType( h
) != FILE_TYPE_DISK
)
3149 FIXME("Non-disk file handles are not currently supported.\n");
3150 release_sock_fd( s
, fd
);
3151 WSASetLastError( WSAEOPNOTSUPP
);
3155 /* set reasonable defaults when requested */
3156 if (!bytes_per_send
)
3157 bytes_per_send
= (1 << 16); /* Depends on OS version: PAGE_SIZE, 2*PAGE_SIZE, or 2^16 */
3159 if (!(wsa
= (struct ws2_transmitfile_async
*)alloc_async_io( sizeof(*wsa
) + bytes_per_send
,
3160 WS2_async_transmitfile
)))
3162 release_sock_fd( s
, fd
);
3163 WSASetLastError( WSAEFAULT
);
3167 wsa
->buffers
= *buffers
;
3169 memset(&wsa
->buffers
, 0x0, sizeof(wsa
->buffers
));
3170 wsa
->buffer
= (char *)(wsa
+ 1);
3173 wsa
->file_bytes
= file_bytes
;
3174 wsa
->bytes_per_send
= bytes_per_send
;
3176 wsa
->offset
.QuadPart
= FILE_USE_FILE_POINTER_POSITION
;
3177 wsa
->write
.hSocket
= SOCKET2HANDLE(s
);
3178 wsa
->write
.addr
= NULL
;
3179 wsa
->write
.addrlen
.val
= 0;
3180 wsa
->write
.flags
= 0;
3181 wsa
->write
.lpFlags
= &wsa
->flags
;
3182 wsa
->write
.control
= NULL
;
3183 wsa
->write
.n_iovecs
= 0;
3184 wsa
->write
.first_iovec
= 0;
3185 wsa
->write
.user_overlapped
= overlapped
;
3188 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)overlapped
;
3191 wsa
->offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3192 wsa
->offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
3193 iosb
->u
.Status
= STATUS_PENDING
;
3194 iosb
->Information
= 0;
3195 status
= register_async( ASYNC_TYPE_WRITE
, SOCKET2HANDLE(s
), &wsa
->io
,
3196 overlapped
->hEvent
, NULL
, NULL
, iosb
);
3197 if(status
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
3198 release_sock_fd( s
, fd
);
3199 WSASetLastError( NtStatusToWSAError(status
) );
3205 status
= WS2_transmitfile_base( fd
, wsa
);
3206 if (status
== STATUS_PENDING
)
3209 do_block(fd
, POLLOUT
, -1);
3210 _sync_sock_state(s
); /* let wineserver notice connection */
3213 while (status
== STATUS_PENDING
);
3214 release_sock_fd( s
, fd
);
3216 if (status
!= STATUS_SUCCESS
)
3217 WSASetLastError( NtStatusToWSAError(status
) );
3218 HeapFree( GetProcessHeap(), 0, wsa
);
3219 return (status
== STATUS_SUCCESS
);
3222 /***********************************************************************
3223 * GetAcceptExSockaddrs
3225 static void WINAPI
WS2_GetAcceptExSockaddrs(PVOID buffer
, DWORD data_size
, DWORD local_size
, DWORD remote_size
,
3226 struct WS_sockaddr
**local_addr
, LPINT local_addr_len
,
3227 struct WS_sockaddr
**remote_addr
, LPINT remote_addr_len
)
3229 char *cbuf
= buffer
;
3230 TRACE("(%p, %d, %d, %d, %p, %p, %p, %p)\n", buffer
, data_size
, local_size
, remote_size
, local_addr
,
3231 local_addr_len
, remote_addr
, remote_addr_len
);
3234 *local_addr_len
= *(int *) cbuf
;
3235 *local_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
3239 *remote_addr_len
= *(int *) cbuf
;
3240 *remote_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
3243 /***********************************************************************
3246 int WINAPI
WSASendMsg( SOCKET s
, LPWSAMSG msg
, DWORD dwFlags
, LPDWORD lpNumberOfBytesSent
,
3247 LPWSAOVERLAPPED lpOverlapped
,
3248 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3252 SetLastError( WSAEFAULT
);
3253 return SOCKET_ERROR
;
3256 return WS2_sendto( s
, msg
->lpBuffers
, msg
->dwBufferCount
, lpNumberOfBytesSent
,
3257 dwFlags
, msg
->name
, msg
->namelen
,
3258 lpOverlapped
, lpCompletionRoutine
);
3261 /***********************************************************************
3264 * Perform a receive operation that is capable of returning message
3265 * control headers. It is important to note that the WSAMSG parameter
3266 * must remain valid throughout the operation, even when an overlapped
3267 * receive is performed.
3269 static int WINAPI
WS2_WSARecvMsg( SOCKET s
, LPWSAMSG msg
, LPDWORD lpNumberOfBytesRecvd
,
3270 LPWSAOVERLAPPED lpOverlapped
,
3271 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3275 SetLastError( WSAEFAULT
);
3276 return SOCKET_ERROR
;
3279 return WS2_recv_base( s
, msg
->lpBuffers
, msg
->dwBufferCount
, lpNumberOfBytesRecvd
,
3280 &msg
->dwFlags
, msg
->name
, &msg
->namelen
,
3281 lpOverlapped
, lpCompletionRoutine
, &msg
->Control
);
3284 /***********************************************************************
3285 * interface_bind (INTERNAL)
3287 * Take bind() calls on any name corresponding to a local network adapter and restrict the given socket to
3288 * operating only on the specified interface. This restriction consists of two components:
3289 * 1) An outgoing packet restriction suggesting the egress interface for all packets.
3290 * 2) An incoming packet restriction dropping packets not meant for the interface.
3291 * If the function succeeds in placing these restrictions (returns TRUE) then the name for the bind() may
3292 * safely be changed to INADDR_ANY, permitting the transmission and receipt of broadcast packets on the
3293 * socket. This behavior is only relevant to UDP sockets and is needed for applications that expect to be able
3294 * to receive broadcast packets on a socket that is bound to a specific network interface.
3296 static BOOL
interface_bind( SOCKET s
, int fd
, struct sockaddr
*addr
)
3298 struct sockaddr_in
*in_sock
= (struct sockaddr_in
*) addr
;
3299 in_addr_t bind_addr
= in_sock
->sin_addr
.s_addr
;
3300 PIP_ADAPTER_INFO adapters
= NULL
, adapter
;
3305 if (bind_addr
== htonl(INADDR_ANY
) || bind_addr
== htonl(INADDR_LOOPBACK
))
3306 return FALSE
; /* Not binding to a network adapter, special interface binding unnecessary. */
3307 if (_get_fd_type(fd
) != SOCK_DGRAM
)
3308 return FALSE
; /* Special interface binding is only necessary for UDP datagrams. */
3309 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
3311 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
3312 if (adapters
== NULL
|| GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
3314 /* Search the IPv4 adapter list for the appropriate binding interface */
3315 for (adapter
= adapters
; adapter
!= NULL
; adapter
= adapter
->Next
)
3317 in_addr_t adapter_addr
= (in_addr_t
) inet_addr(adapter
->IpAddressList
.IpAddress
.String
);
3319 if (bind_addr
== adapter_addr
)
3321 #if defined(IP_BOUND_IF)
3322 /* IP_BOUND_IF sets both the incoming and outgoing restriction at once */
3323 if (setsockopt(fd
, IPPROTO_IP
, IP_BOUND_IF
, &adapter
->Index
, sizeof(adapter
->Index
)) != 0)
3326 #elif defined(LINUX_BOUND_IF)
3327 in_addr_t ifindex
= (in_addr_t
) htonl(adapter
->Index
);
3328 struct interface_filter specific_interface_filter
;
3329 struct sock_fprog filter_prog
;
3331 if (setsockopt(fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
)) != 0)
3332 goto cleanup
; /* Failed to suggest egress interface */
3333 specific_interface_filter
= generic_interface_filter
;
3334 specific_interface_filter
.iface_rule
.k
= adapter
->Index
;
3335 specific_interface_filter
.ip_rule
.k
= htonl(adapter_addr
);
3336 filter_prog
.len
= sizeof(generic_interface_filter
)/sizeof(struct sock_filter
);
3337 filter_prog
.filter
= (struct sock_filter
*) &specific_interface_filter
;
3338 if (setsockopt(fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
)) != 0)
3339 goto cleanup
; /* Failed to specify incoming packet filter */
3342 FIXME("Broadcast packets on interface-bound sockets are not currently supported on this platform, "
3343 "receiving broadcast packets will not work on socket %04lx.\n", s
);
3348 /* Will soon be switching to INADDR_ANY: permit address reuse */
3349 if (ret
&& setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
)) == 0)
3350 TRACE("Socket %04lx bound to interface index %d\n", s
, adapter
->Index
);
3356 ERR("Failed to bind to interface, receiving broadcast packets will not work on socket %04lx.\n", s
);
3357 HeapFree(GetProcessHeap(), 0, adapters
);
3361 /***********************************************************************
3364 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
3366 int fd
= get_sock_fd( s
, 0, NULL
);
3367 int res
= SOCKET_ERROR
;
3369 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
3373 if (!name
|| (name
->sa_family
&& !supported_pf(name
->sa_family
)))
3375 SetLastError(WSAEAFNOSUPPORT
);
3379 union generic_unix_sockaddr uaddr
;
3380 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
3383 SetLastError(WSAEFAULT
);
3387 if (name
->sa_family
== WS_AF_INET
)
3389 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
3390 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
3392 /* Trying to bind to the default host interface, using
3393 * INADDR_ANY instead*/
3394 WARN("Trying to bind to magic IP address, using "
3395 "INADDR_ANY instead.\n");
3396 in4
->sin_addr
.s_addr
= htonl(INADDR_ANY
);
3398 else if (interface_bind(s
, fd
, &uaddr
.addr
))
3399 in4
->sin_addr
.s_addr
= htonl(INADDR_ANY
);
3401 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
3403 int loc_errno
= errno
;
3404 WARN("\tfailure - errno = %i\n", errno
);
3409 SetLastError(WSAEINVAL
);
3414 socklen_t optlen
= sizeof(optval
);
3415 /* Windows >= 2003 will return different results depending on
3416 * SO_REUSEADDR, WSAEACCES may be returned representing that
3417 * the socket hijacking protection prevented the bind */
3418 if (!getsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&optval
, &optlen
) && optval
)
3420 SetLastError(WSAEACCES
);
3426 SetLastError(wsaErrno());
3432 res
=0; /* success */
3436 release_sock_fd( s
, fd
);
3441 /***********************************************************************
3442 * closesocket (WS2_32.3)
3444 int WINAPI
WS_closesocket(SOCKET s
)
3446 int res
= SOCKET_ERROR
, fd
;
3449 fd
= get_sock_fd(s
, FILE_READ_DATA
, NULL
);
3452 release_sock_fd(s
, fd
);
3453 if (CloseHandle(SOCKET2HANDLE(s
)))
3457 SetLastError(WSAENOTSOCK
);
3460 SetLastError(WSANOTINITIALISED
);
3461 TRACE("(socket %04lx) -> %d\n", s
, res
);
3465 static int do_connect(int fd
, const struct WS_sockaddr
* name
, int namelen
)
3467 union generic_unix_sockaddr uaddr
;
3468 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
3473 if (name
->sa_family
== WS_AF_INET
)
3475 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
3476 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
3478 /* Trying to connect to magic replace-loopback address,
3479 * assuming we really want to connect to localhost */
3480 TRACE("Trying to connect to magic IP address, using "
3481 "INADDR_LOOPBACK instead.\n");
3482 in4
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
3486 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
3492 /***********************************************************************
3493 * connect (WS2_32.4)
3495 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
3497 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
3499 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
3504 int ret
= do_connect(fd
, name
, namelen
);
3506 goto connect_success
;
3508 if (ret
== WSAEINPROGRESS
)
3510 /* tell wineserver that a connection is in progress */
3511 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3513 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
3514 ret
= sock_is_blocking( s
, &is_blocking
);
3519 do_block(fd
, POLLIN
| POLLOUT
, -1);
3520 _sync_sock_state(s
); /* let wineserver notice connection */
3521 /* retrieve any error codes from it */
3522 if (!(ret
= get_sock_error(s
, FD_CONNECT_BIT
))) goto connect_success
;
3524 else ret
= WSAEWOULDBLOCK
;
3527 release_sock_fd( s
, fd
);
3530 return SOCKET_ERROR
;
3533 release_sock_fd( s
, fd
);
3534 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3535 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
3536 FD_CONNECT
|FD_WINE_LISTENING
);
3537 TRACE("\tconnected %04lx\n", s
);
3541 /***********************************************************************
3542 * WSAConnect (WS2_32.30)
3544 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
3545 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
3546 LPQOS lpSQOS
, LPQOS lpGQOS
)
3548 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
3549 FIXME("unsupported parameters!\n");
3550 return WS_connect( s
, name
, namelen
);
3553 /***********************************************************************
3556 static BOOL WINAPI
WS2_ConnectEx(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
3557 PVOID sendBuf
, DWORD sendBufLen
, LPDWORD sent
, LPOVERLAPPED ov
)
3559 int fd
, ret
, status
;
3563 SetLastError( ERROR_INVALID_PARAMETER
);
3567 fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
3570 SetLastError( WSAENOTSOCK
);
3574 TRACE("socket %04lx, ptr %p %s, length %d, sendptr %p, len %d, ov %p\n",
3575 s
, name
, debugstr_sockaddr(name
), namelen
, sendBuf
, sendBufLen
, ov
);
3577 ret
= is_fd_bound(fd
, NULL
, NULL
);
3580 SetLastError(ret
== -1 ? wsaErrno() : WSAEINVAL
);
3581 release_sock_fd( s
, fd
);
3585 ret
= do_connect(fd
, name
, namelen
);
3590 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3591 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
3592 FD_CONNECT
|FD_WINE_LISTENING
);
3594 wsabuf
.len
= sendBufLen
;
3595 wsabuf
.buf
= (char*) sendBuf
;
3597 /* WSASend takes care of completion if need be */
3598 if (WSASend(s
, &wsabuf
, sendBuf
? 1 : 0, sent
, 0, ov
, NULL
) != SOCKET_ERROR
)
3599 goto connection_success
;
3601 else if (ret
== WSAEINPROGRESS
)
3603 struct ws2_async
*wsa
;
3604 ULONG_PTR cvalue
= (((ULONG_PTR
)ov
->hEvent
& 1) == 0) ? (ULONG_PTR
)ov
: 0;
3606 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3608 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
3610 /* Indirectly call WSASend */
3611 if (!(wsa
= (struct ws2_async
*)alloc_async_io( offsetof( struct ws2_async
, iovec
[1] ), WS2_async_send
)))
3613 SetLastError(WSAEFAULT
);
3617 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)ov
;
3618 iosb
->u
.Status
= STATUS_PENDING
;
3619 iosb
->Information
= 0;
3621 wsa
->hSocket
= SOCKET2HANDLE(s
);
3623 wsa
->addrlen
.val
= 0;
3625 wsa
->lpFlags
= &wsa
->flags
;
3626 wsa
->control
= NULL
;
3627 wsa
->n_iovecs
= sendBuf
? 1 : 0;
3628 wsa
->first_iovec
= 0;
3629 wsa
->completion_func
= NULL
;
3630 wsa
->iovec
[0].iov_base
= sendBuf
;
3631 wsa
->iovec
[0].iov_len
= sendBufLen
;
3633 status
= register_async( ASYNC_TYPE_WRITE
, wsa
->hSocket
, &wsa
->io
, ov
->hEvent
,
3634 NULL
, (void *)cvalue
, iosb
);
3635 if (status
!= STATUS_PENDING
) HeapFree(GetProcessHeap(), 0, wsa
);
3637 /* If the connect already failed */
3638 if (status
== STATUS_PIPE_DISCONNECTED
)
3640 ov
->Internal
= sock_error_to_ntstatus( get_sock_error( s
, FD_CONNECT_BIT
));
3641 ov
->InternalHigh
= 0;
3642 if (cvalue
) WS_AddCompletion( s
, cvalue
, ov
->Internal
, ov
->InternalHigh
, FALSE
);
3643 if (ov
->hEvent
) NtSetEvent( ov
->hEvent
, NULL
);
3644 status
= STATUS_PENDING
;
3646 SetLastError( NtStatusToWSAError(status
) );
3654 release_sock_fd( s
, fd
);
3658 release_sock_fd( s
, fd
);
3662 /***********************************************************************
3665 static BOOL WINAPI
WS2_DisconnectEx( SOCKET s
, LPOVERLAPPED ov
, DWORD flags
, DWORD reserved
)
3667 TRACE( "socket %04lx, ov %p, flags 0x%x, reserved 0x%x\n", s
, ov
, flags
, reserved
);
3669 if (flags
& TF_REUSE_SOCKET
)
3670 FIXME( "Reusing socket not supported yet\n" );
3674 ov
->Internal
= STATUS_PENDING
;
3675 ov
->InternalHigh
= 0;
3678 return !WS_shutdown( s
, SD_BOTH
);
3681 /***********************************************************************
3682 * getpeername (WS2_32.5)
3684 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
3689 TRACE("socket %04lx, ptr %p, len %08x\n", s
, name
, namelen
? *namelen
: 0);
3691 fd
= get_sock_fd( s
, 0, NULL
);
3696 union generic_unix_sockaddr uaddr
;
3697 socklen_t uaddrlen
= sizeof(uaddr
);
3699 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) == 0)
3701 if (!name
|| !namelen
)
3702 SetLastError(WSAEFAULT
);
3703 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
3704 /* The buffer was too small */
3705 SetLastError(WSAEFAULT
);
3709 TRACE("=> %s\n", debugstr_sockaddr(name
));
3713 SetLastError(wsaErrno());
3714 release_sock_fd( s
, fd
);
3719 /* When binding to an UDP address with filter support the getsockname call on the socket
3720 * will always return 0.0.0.0 instead of the filtered interface address. This function
3721 * checks if the socket is interface-bound on UDP and return the correct address.
3722 * This is required because applications often do a bind() with port zero followed by a
3723 * getsockname() to retrieve the port and address acquired.
3725 static void interface_bind_check(int fd
, struct sockaddr_in
*addr
)
3727 #if !defined(IP_BOUND_IF) && !defined(LINUX_BOUND_IF)
3730 unsigned int ifindex
;
3734 /* Check for IPv4, address 0.0.0.0 and UDP socket */
3735 if (addr
->sin_family
!= AF_INET
|| addr
->sin_addr
.s_addr
!= 0)
3737 if (_get_fd_type(fd
) != SOCK_DGRAM
)
3740 len
= sizeof(ifindex
);
3741 #if defined(IP_BOUND_IF)
3742 ret
= getsockopt(fd
, IPPROTO_IP
, IP_BOUND_IF
, &ifindex
, &len
);
3743 #elif defined(LINUX_BOUND_IF)
3744 ret
= getsockopt(fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, &len
);
3745 if (!ret
) ifindex
= ntohl(ifindex
);
3749 PIP_ADAPTER_INFO adapters
, adapter
;
3752 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
3754 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
3755 if (adapters
&& GetAdaptersInfo(adapters
, &adap_size
) == NO_ERROR
)
3757 /* Search the IPv4 adapter list for the appropriate bound interface */
3758 for (adapter
= adapters
; adapter
!= NULL
; adapter
= adapter
->Next
)
3760 in_addr_t adapter_addr
;
3761 if (adapter
->Index
!= ifindex
) continue;
3763 adapter_addr
= inet_addr(adapter
->IpAddressList
.IpAddress
.String
);
3764 addr
->sin_addr
.s_addr
= adapter_addr
;
3765 TRACE("reporting interface address from adapter %d\n", ifindex
);
3769 HeapFree(GetProcessHeap(), 0, adapters
);
3774 /***********************************************************************
3775 * getsockname (WS2_32.6)
3777 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
3782 TRACE("socket %04lx, ptr %p, len %08x\n", s
, name
, namelen
? *namelen
: 0);
3784 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
3785 if( (name
== NULL
) || (namelen
== NULL
) )
3787 SetLastError( WSAEFAULT
);
3788 return SOCKET_ERROR
;
3791 fd
= get_sock_fd( s
, 0, NULL
);
3796 union generic_unix_sockaddr uaddr
;
3798 int bound
= is_fd_bound(fd
, &uaddr
, &uaddrlen
);
3802 SetLastError(bound
== -1 ? wsaErrno() : WSAEINVAL
);
3804 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
3806 /* The buffer was too small */
3807 SetLastError(WSAEFAULT
);
3811 interface_bind_check(fd
, (struct sockaddr_in
*) &uaddr
);
3812 if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
3814 /* The buffer was too small */
3815 SetLastError(WSAEFAULT
);
3820 TRACE("=> %s\n", debugstr_sockaddr(name
));
3823 release_sock_fd( s
, fd
);
3828 /***********************************************************************
3829 * getsockopt (WS2_32.7)
3831 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
3832 INT optname
, char *optval
, INT
*optlen
)
3837 TRACE("(socket %04lx, %s, optval %s, optlen %p (%d))\n", s
,
3838 debugstr_sockopt(level
, optname
), debugstr_optval(optval
, 0),
3839 optlen
, optlen
? *optlen
: 0);
3847 /* Handle common cases. The special cases are below, sorted
3849 case WS_SO_BROADCAST
:
3851 case WS_SO_KEEPALIVE
:
3852 case WS_SO_OOBINLINE
:
3854 case WS_SO_REUSEADDR
:
3856 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3857 return SOCKET_ERROR
;
3858 convert_sockopt(&level
, &optname
);
3859 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
3861 SetLastError(wsaErrno());
3864 release_sock_fd( s
, fd
);
3866 case WS_SO_ACCEPTCONN
:
3867 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3868 return SOCKET_ERROR
;
3869 if (getsockopt(fd
, SOL_SOCKET
, SO_ACCEPTCONN
, optval
, (socklen_t
*)optlen
) != 0 )
3871 SetLastError(wsaErrno());
3876 /* BSD returns != 0 while Windows return exact == 1 */
3877 if (*(int *)optval
) *(int *)optval
= 1;
3879 release_sock_fd( s
, fd
);
3881 case WS_SO_BSP_STATE
:
3883 int req_size
, addr_size
;
3884 WSAPROTOCOL_INFOW infow
;
3885 CSADDR_INFO
*csinfo
;
3887 ret
= ws_protocol_info(s
, TRUE
, &infow
, &addr_size
);
3890 if (infow
.iAddressFamily
== WS_AF_INET
)
3891 addr_size
= sizeof(struct sockaddr_in
);
3892 else if (infow
.iAddressFamily
== WS_AF_INET6
)
3893 addr_size
= sizeof(struct sockaddr_in6
);
3896 FIXME("Family %d is unsupported for SO_BSP_STATE\n", infow
.iAddressFamily
);
3897 SetLastError(WSAEAFNOSUPPORT
);
3898 return SOCKET_ERROR
;
3901 req_size
= sizeof(CSADDR_INFO
) + addr_size
* 2;
3902 if (*optlen
< req_size
)
3905 SetLastError(WSAEFAULT
);
3909 union generic_unix_sockaddr uaddr
;
3912 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3913 return SOCKET_ERROR
;
3915 csinfo
= (CSADDR_INFO
*) optval
;
3917 /* Check if the sock is bound */
3918 if (is_fd_bound(fd
, &uaddr
, &uaddrlen
) == 1)
3920 csinfo
->LocalAddr
.lpSockaddr
=
3921 (LPSOCKADDR
) (optval
+ sizeof(CSADDR_INFO
));
3922 ws_sockaddr_u2ws(&uaddr
.addr
, csinfo
->LocalAddr
.lpSockaddr
, &addr_size
);
3923 csinfo
->LocalAddr
.iSockaddrLength
= addr_size
;
3927 csinfo
->LocalAddr
.lpSockaddr
= NULL
;
3928 csinfo
->LocalAddr
.iSockaddrLength
= 0;
3931 /* Check if the sock is connected */
3932 if (!getpeername(fd
, &uaddr
.addr
, &uaddrlen
) &&
3933 is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
3935 csinfo
->RemoteAddr
.lpSockaddr
=
3936 (LPSOCKADDR
) (optval
+ sizeof(CSADDR_INFO
) + addr_size
);
3937 ws_sockaddr_u2ws(&uaddr
.addr
, csinfo
->RemoteAddr
.lpSockaddr
, &addr_size
);
3938 csinfo
->RemoteAddr
.iSockaddrLength
= addr_size
;
3942 csinfo
->RemoteAddr
.lpSockaddr
= NULL
;
3943 csinfo
->RemoteAddr
.iSockaddrLength
= 0;
3946 csinfo
->iSocketType
= infow
.iSocketType
;
3947 csinfo
->iProtocol
= infow
.iProtocol
;
3948 release_sock_fd( s
, fd
);
3951 return ret
? 0 : SOCKET_ERROR
;
3953 case WS_SO_DONTLINGER
:
3955 struct linger lingval
;
3956 socklen_t len
= sizeof(struct linger
);
3958 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
3960 SetLastError(WSAEFAULT
);
3961 return SOCKET_ERROR
;
3963 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3964 return SOCKET_ERROR
;
3966 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
3968 SetLastError(wsaErrno());
3973 *(BOOL
*)optval
= !lingval
.l_onoff
;
3974 *optlen
= sizeof(BOOL
);
3977 release_sock_fd( s
, fd
);
3981 case WS_SO_CONNECT_TIME
:
3983 static int pretendtime
= 0;
3984 struct WS_sockaddr addr
;
3985 int len
= sizeof(addr
);
3987 if (!optlen
|| *optlen
< sizeof(DWORD
) || !optval
)
3989 SetLastError(WSAEFAULT
);
3990 return SOCKET_ERROR
;
3992 if (WS_getpeername(s
, &addr
, &len
) == SOCKET_ERROR
)
3993 *(DWORD
*)optval
= ~0u;
3996 if (!pretendtime
) FIXME("WS_SO_CONNECT_TIME - faking results\n");
3997 *(DWORD
*)optval
= pretendtime
++;
3999 *optlen
= sizeof(DWORD
);
4002 /* As mentioned in setsockopt, Windows ignores this, so we
4003 * always return true here */
4004 case WS_SO_DONTROUTE
:
4005 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
4007 SetLastError(WSAEFAULT
);
4008 return SOCKET_ERROR
;
4010 *(BOOL
*)optval
= TRUE
;
4011 *optlen
= sizeof(BOOL
);
4016 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4017 return SOCKET_ERROR
;
4018 if (getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, optval
, (socklen_t
*)optlen
) != 0 )
4020 SetLastError(wsaErrno());
4023 release_sock_fd( s
, fd
);
4025 /* The wineserver may have swallowed the error before us */
4026 if (!ret
&& *(int*) optval
== 0)
4028 int i
, events
[FD_MAX_EVENTS
];
4029 _get_sock_errors(s
, events
);
4030 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
4034 TRACE("returning SO_ERROR %d from wine server\n", events
[i
]);
4035 *(int*) optval
= events
[i
];
4045 struct linger lingval
;
4046 socklen_t len
= sizeof(struct linger
);
4048 /* struct linger and LINGER have different sizes */
4049 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
4051 SetLastError(WSAEFAULT
);
4052 return SOCKET_ERROR
;
4054 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4055 return SOCKET_ERROR
;
4057 if (_get_fd_type(fd
) == SOCK_DGRAM
)
4059 SetLastError(WSAENOPROTOOPT
);
4062 else if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0)
4064 SetLastError(wsaErrno());
4069 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
4070 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
4071 *optlen
= sizeof(struct linger
);
4074 release_sock_fd( s
, fd
);
4078 case WS_SO_MAX_MSG_SIZE
:
4079 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
4081 SetLastError(WSAEFAULT
);
4082 return SOCKET_ERROR
;
4084 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
4085 *(int *)optval
= 65507;
4086 *optlen
= sizeof(int);
4089 /* SO_OPENTYPE does not require a valid socket handle. */
4090 case WS_SO_OPENTYPE
:
4091 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
4093 SetLastError(WSAEFAULT
);
4094 return SOCKET_ERROR
;
4096 *(int *)optval
= get_per_thread_data()->opentype
;
4097 *optlen
= sizeof(int);
4098 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
4100 case WS_SO_PROTOCOL_INFOA
:
4101 case WS_SO_PROTOCOL_INFOW
:
4104 WSAPROTOCOL_INFOW infow
;
4106 ret
= ws_protocol_info(s
, optname
== WS_SO_PROTOCOL_INFOW
, &infow
, &size
);
4109 if (!optlen
|| !optval
|| *optlen
< size
)
4111 if(optlen
) *optlen
= size
;
4113 SetLastError(WSAEFAULT
);
4116 memcpy(optval
, &infow
, size
);
4118 return ret
? 0 : SOCKET_ERROR
;
4120 case WS_SO_RCVTIMEO
:
4121 case WS_SO_SNDTIMEO
:
4125 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
4127 SetLastError(WSAEFAULT
);
4128 return SOCKET_ERROR
;
4130 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4131 return SOCKET_ERROR
;
4133 timeout
= get_rcvsnd_timeo(fd
, optname
== WS_SO_RCVTIMEO
);
4134 *(int *)optval
= timeout
<= UINT_MAX
? timeout
: UINT_MAX
;
4136 release_sock_fd( s
, fd
);
4142 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
4144 SetLastError(WSAEFAULT
);
4145 return SOCKET_ERROR
;
4147 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4148 return SOCKET_ERROR
;
4150 sock_type
= _get_fd_type(fd
);
4151 if (sock_type
== -1)
4153 SetLastError(wsaErrno());
4157 (*(int *)optval
) = convert_socktype_u2w(sock_type
);
4159 release_sock_fd( s
, fd
);
4163 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
4164 SetLastError(WSAENOPROTOOPT
);
4165 return SOCKET_ERROR
;
4166 } /* end switch(optname) */
4167 }/* end case WS_SOL_SOCKET */
4169 case WS_NSPROTO_IPX
:
4171 struct WS_sockaddr_ipx addr
;
4172 IPX_ADDRESS_DATA
*data
;
4177 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
4179 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (socklen_t
*)optlen
) == -1)
4186 socklen_t len
=sizeof(struct ipx
);
4187 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
4190 *optval
= (int)val
.ipx_pt
;
4193 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
4194 release_sock_fd( s
, fd
);
4197 case WS_IPX_ADDRESS
:
4199 * On a Win2000 system with one network card there are usually
4200 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
4201 * Using this call you can then retrieve info about this all.
4202 * In case of Linux it is a bit different. Usually you have
4203 * only "one" device active and further it is not possible to
4204 * query things like the linkspeed.
4206 FIXME("IPX_ADDRESS\n");
4207 namelen
= sizeof(struct WS_sockaddr_ipx
);
4208 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
4209 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
4211 data
= (IPX_ADDRESS_DATA
*)optval
;
4212 memcpy(data
->nodenum
,addr
.sa_nodenum
,sizeof(data
->nodenum
));
4213 memcpy(data
->netnum
,addr
.sa_netnum
,sizeof(data
->netnum
));
4214 data
->adapternum
= 0;
4215 data
->wan
= FALSE
; /* We are not on a wan for now .. */
4216 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
4217 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
4218 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
4219 * note 1MB = 1000kB in this case */
4222 case WS_IPX_MAX_ADAPTER_NUM
:
4223 FIXME("IPX_MAX_ADAPTER_NUM\n");
4224 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
4228 FIXME("IPX optname:%x\n", optname
);
4229 return SOCKET_ERROR
;
4230 }/* end switch(optname) */
4231 } /* end case WS_NSPROTO_IPX */
4235 #define MAX_IRDA_DEVICES 10
4240 case WS_IRLMP_ENUMDEVICES
:
4242 char buf
[sizeof(struct irda_device_list
) +
4243 (MAX_IRDA_DEVICES
- 1) * sizeof(struct irda_device_info
)];
4245 socklen_t len
= sizeof(buf
);
4247 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4248 return SOCKET_ERROR
;
4249 res
= getsockopt( fd
, SOL_IRLMP
, IRLMP_ENUMDEVICES
, buf
, &len
);
4250 release_sock_fd( s
, fd
);
4253 SetLastError(wsaErrno());
4254 return SOCKET_ERROR
;
4258 struct irda_device_list
*src
= (struct irda_device_list
*)buf
;
4259 DEVICELIST
*dst
= (DEVICELIST
*)optval
;
4260 INT needed
= sizeof(DEVICELIST
);
4264 needed
+= (src
->len
- 1) * sizeof(IRDA_DEVICE_INFO
);
4265 if (*optlen
< needed
)
4267 SetLastError(WSAEFAULT
);
4268 return SOCKET_ERROR
;
4271 TRACE("IRLMP_ENUMDEVICES: %d devices found:\n", src
->len
);
4272 dst
->numDevice
= src
->len
;
4273 for (i
= 0; i
< src
->len
; i
++)
4275 TRACE("saddr = %08x, daddr = %08x, info = %s, hints = %02x%02x\n",
4276 src
->dev
[i
].saddr
, src
->dev
[i
].daddr
,
4277 src
->dev
[i
].info
, src
->dev
[i
].hints
[0],
4278 src
->dev
[i
].hints
[1]);
4279 memcpy( dst
->Device
[i
].irdaDeviceID
,
4281 sizeof(dst
->Device
[i
].irdaDeviceID
) ) ;
4282 memcpy( dst
->Device
[i
].irdaDeviceName
,
4284 sizeof(dst
->Device
[i
].irdaDeviceName
) ) ;
4285 memcpy( &dst
->Device
[i
].irdaDeviceHints1
,
4286 &src
->dev
[i
].hints
[0],
4287 sizeof(dst
->Device
[i
].irdaDeviceHints1
) ) ;
4288 memcpy( &dst
->Device
[i
].irdaDeviceHints2
,
4289 &src
->dev
[i
].hints
[1],
4290 sizeof(dst
->Device
[i
].irdaDeviceHints2
) ) ;
4291 dst
->Device
[i
].irdaCharSet
= src
->dev
[i
].charset
;
4297 FIXME("IrDA optname:0x%x\n", optname
);
4298 return SOCKET_ERROR
;
4300 break; /* case WS_SOL_IRLMP */
4301 #undef MAX_IRDA_DEVICES
4304 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
4305 case WS_IPPROTO_TCP
:
4308 case WS_TCP_NODELAY
:
4309 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4310 return SOCKET_ERROR
;
4311 convert_sockopt(&level
, &optname
);
4312 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
4314 SetLastError(wsaErrno());
4317 release_sock_fd( s
, fd
);
4320 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
4321 return SOCKET_ERROR
;
4326 case WS_IP_ADD_MEMBERSHIP
:
4327 case WS_IP_DROP_MEMBERSHIP
:
4331 case WS_IP_MULTICAST_IF
:
4332 case WS_IP_MULTICAST_LOOP
:
4333 case WS_IP_MULTICAST_TTL
:
4335 #if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
4340 #ifdef IP_UNICAST_IF
4341 case WS_IP_UNICAST_IF
:
4343 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4344 return SOCKET_ERROR
;
4345 convert_sockopt(&level
, &optname
);
4346 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
4348 SetLastError(wsaErrno());
4351 release_sock_fd( s
, fd
);
4353 case WS_IP_DONTFRAGMENT
:
4354 return get_dont_fragment(s
, IPPROTO_IP
, (BOOL
*)optval
) ? 0 : SOCKET_ERROR
;
4356 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
4357 return SOCKET_ERROR
;
4359 case WS_IPPROTO_IPV6
:
4362 #ifdef IPV6_ADD_MEMBERSHIP
4363 case WS_IPV6_ADD_MEMBERSHIP
:
4365 #ifdef IPV6_DROP_MEMBERSHIP
4366 case WS_IPV6_DROP_MEMBERSHIP
:
4368 case WS_IPV6_MULTICAST_IF
:
4369 case WS_IPV6_MULTICAST_HOPS
:
4370 case WS_IPV6_MULTICAST_LOOP
:
4371 case WS_IPV6_UNICAST_HOPS
:
4372 case WS_IPV6_V6ONLY
:
4373 #ifdef IPV6_UNICAST_IF
4374 case WS_IPV6_UNICAST_IF
:
4376 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4377 return SOCKET_ERROR
;
4378 convert_sockopt(&level
, &optname
);
4379 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
4381 SetLastError(wsaErrno());
4384 release_sock_fd( s
, fd
);
4386 case WS_IPV6_DONTFRAG
:
4387 return get_dont_fragment(s
, IPPROTO_IPV6
, (BOOL
*)optval
) ? 0 : SOCKET_ERROR
;
4389 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
4390 return SOCKET_ERROR
;
4393 WARN("Unknown level: 0x%08x\n", level
);
4394 SetLastError(WSAEINVAL
);
4395 return SOCKET_ERROR
;
4396 } /* end switch(level) */
4399 /***********************************************************************
4402 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
4404 return htonl(hostlong
);
4408 /***********************************************************************
4411 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
4413 return htons(hostshort
);
4416 /***********************************************************************
4417 * WSAHtonl (WS2_32.46)
4418 * From MSDN description of error codes, this function should also
4419 * check if WinSock has been initialized and the socket is a valid
4420 * socket. But why? This function only translates a host byte order
4421 * u_long into a network byte order u_long...
4423 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
4427 *lpnetlong
= htonl(hostlong
);
4430 SetLastError(WSAEFAULT
);
4431 return SOCKET_ERROR
;
4434 /***********************************************************************
4435 * WSAHtons (WS2_32.47)
4436 * From MSDN description of error codes, this function should also
4437 * check if WinSock has been initialized and the socket is a valid
4438 * socket. But why? This function only translates a host byte order
4439 * u_short into a network byte order u_short...
4441 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
4446 *lpnetshort
= htons(hostshort
);
4449 SetLastError(WSAEFAULT
);
4450 return SOCKET_ERROR
;
4454 /***********************************************************************
4455 * inet_addr (WS2_32.11)
4457 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
4459 if (!cp
) return INADDR_NONE
;
4460 return inet_addr(cp
);
4464 /***********************************************************************
4467 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
4469 return ntohl(netlong
);
4473 /***********************************************************************
4476 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
4478 return ntohs(netshort
);
4482 /***********************************************************************
4483 * inet_ntoa (WS2_32.12)
4485 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
4487 unsigned int long_ip
= ntohl(in
.WS_s_addr
);
4488 struct per_thread_data
*data
= get_per_thread_data();
4490 sprintf( data
->ntoa_buffer
, "%u.%u.%u.%u",
4491 (long_ip
>> 24) & 0xff,
4492 (long_ip
>> 16) & 0xff,
4493 (long_ip
>> 8) & 0xff,
4496 return data
->ntoa_buffer
;
4499 static const char *debugstr_wsaioctl(DWORD code
)
4501 const char *name
= NULL
, *buf_type
, *family
;
4503 #define IOCTL_NAME(x) case x: name = #x; break
4506 IOCTL_NAME(WS_FIONBIO
);
4507 IOCTL_NAME(WS_FIONREAD
);
4508 IOCTL_NAME(WS_SIOCATMARK
);
4509 /* IOCTL_NAME(WS_SIO_ACQUIRE_PORT_RESERVATION); */
4510 IOCTL_NAME(WS_SIO_ADDRESS_LIST_CHANGE
);
4511 IOCTL_NAME(WS_SIO_ADDRESS_LIST_QUERY
);
4512 IOCTL_NAME(WS_SIO_ASSOCIATE_HANDLE
);
4513 /* IOCTL_NAME(WS_SIO_ASSOCIATE_PORT_RESERVATION);
4514 IOCTL_NAME(WS_SIO_BASE_HANDLE);
4515 IOCTL_NAME(WS_SIO_BSP_HANDLE);
4516 IOCTL_NAME(WS_SIO_BSP_HANDLE_SELECT);
4517 IOCTL_NAME(WS_SIO_BSP_HANDLE_POLL);
4518 IOCTL_NAME(WS_SIO_CHK_QOS); */
4519 IOCTL_NAME(WS_SIO_ENABLE_CIRCULAR_QUEUEING
);
4520 IOCTL_NAME(WS_SIO_FIND_ROUTE
);
4521 IOCTL_NAME(WS_SIO_FLUSH
);
4522 IOCTL_NAME(WS_SIO_GET_BROADCAST_ADDRESS
);
4523 IOCTL_NAME(WS_SIO_GET_EXTENSION_FUNCTION_POINTER
);
4524 IOCTL_NAME(WS_SIO_GET_GROUP_QOS
);
4525 IOCTL_NAME(WS_SIO_GET_INTERFACE_LIST
);
4526 /* IOCTL_NAME(WS_SIO_GET_INTERFACE_LIST_EX); */
4527 IOCTL_NAME(WS_SIO_GET_QOS
);
4528 /* IOCTL_NAME(WS_SIO_IDEAL_SEND_BACKLOG_CHANGE);
4529 IOCTL_NAME(WS_SIO_IDEAL_SEND_BACKLOG_QUERY); */
4530 IOCTL_NAME(WS_SIO_KEEPALIVE_VALS
);
4531 IOCTL_NAME(WS_SIO_MULTIPOINT_LOOPBACK
);
4532 IOCTL_NAME(WS_SIO_MULTICAST_SCOPE
);
4533 /* IOCTL_NAME(WS_SIO_QUERY_RSS_SCALABILITY_INFO);
4534 IOCTL_NAME(WS_SIO_QUERY_WFP_ALE_ENDPOINT_HANDLE); */
4535 IOCTL_NAME(WS_SIO_RCVALL
);
4536 IOCTL_NAME(WS_SIO_RCVALL_IGMPMCAST
);
4537 IOCTL_NAME(WS_SIO_RCVALL_MCAST
);
4538 /* IOCTL_NAME(WS_SIO_RELEASE_PORT_RESERVATION); */
4539 IOCTL_NAME(WS_SIO_ROUTING_INTERFACE_CHANGE
);
4540 IOCTL_NAME(WS_SIO_ROUTING_INTERFACE_QUERY
);
4541 IOCTL_NAME(WS_SIO_SET_COMPATIBILITY_MODE
);
4542 IOCTL_NAME(WS_SIO_SET_GROUP_QOS
);
4543 IOCTL_NAME(WS_SIO_SET_QOS
);
4544 IOCTL_NAME(WS_SIO_TRANSLATE_HANDLE
);
4545 IOCTL_NAME(WS_SIO_UDP_CONNRESET
);
4552 /* If this is not a known code split its bits */
4553 switch(code
& 0x18000000)
4558 case WS_IOC_PROTOCOL
:
4559 family
= "IOC_PROTOCOL";
4562 family
= "IOC_VENDOR";
4564 default: /* WS_IOC_UNIX */
4566 BYTE size
= (code
>> 16) & WS_IOCPARM_MASK
;
4567 char x
= (code
& 0xff00) >> 8;
4568 BYTE y
= code
& 0xff;
4571 switch (code
& (WS_IOC_VOID
|WS_IOC_INOUT
))
4575 sprintf(args
, "%d, %d", x
, y
);
4579 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
4583 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
4587 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
4590 return wine_dbg_sprintf("%s(%s)", buf_type
, args
);
4594 /* We are different from WS_IOC_UNIX. */
4595 switch (code
& (WS_IOC_VOID
|WS_IOC_INOUT
))
4598 buf_type
= "_WSAIO";
4601 buf_type
= "_WSAIORW";
4604 buf_type
= "_WSAIOW";
4607 buf_type
= "_WSAIOR";
4614 return wine_dbg_sprintf("%s(%s, %d)", buf_type
, family
,
4615 (USHORT
)(code
& 0xffff));
4618 /* do an ioctl call through the server */
4619 static DWORD
server_ioctl_sock( SOCKET s
, DWORD code
, LPVOID in_buff
, DWORD in_size
,
4620 LPVOID out_buff
, DWORD out_size
, LPDWORD ret_size
,
4621 LPWSAOVERLAPPED overlapped
,
4622 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
4624 HANDLE event
= overlapped
? overlapped
->hEvent
: 0;
4625 HANDLE handle
= SOCKET2HANDLE( s
);
4626 struct ws2_async
*wsa
= NULL
;
4627 IO_STATUS_BLOCK
*io
= (PIO_STATUS_BLOCK
)overlapped
, iosb
;
4628 void *cvalue
= NULL
;
4633 if (!(wsa
= (struct ws2_async
*)alloc_async_io( sizeof(*wsa
), NULL
)))
4634 return WSA_NOT_ENOUGH_MEMORY
;
4635 wsa
->hSocket
= handle
;
4636 wsa
->user_overlapped
= overlapped
;
4637 wsa
->completion_func
= completion
;
4638 if (!io
) io
= &wsa
->local_iosb
;
4643 else if (!((ULONG_PTR
)overlapped
->hEvent
& 1))
4644 cvalue
= overlapped
;
4646 status
= NtDeviceIoControlFile( handle
, event
, wsa
? ws2_async_apc
: NULL
, cvalue
, io
, code
,
4647 in_buff
, in_size
, out_buff
, out_size
);
4648 if (status
== STATUS_NOT_SUPPORTED
)
4650 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
4651 code
, code
>> 16, (code
>> 14) & 3, (code
>> 2) & 0xfff, code
& 3);
4653 else if (status
== STATUS_SUCCESS
)
4654 *ret_size
= io
->Information
; /* "Information" is the size written to the output buffer */
4656 if (status
!= STATUS_PENDING
) RtlFreeHeap( GetProcessHeap(), 0, wsa
);
4658 return NtStatusToWSAError( status
);
4661 /**********************************************************************
4662 * WSAIoctl (WS2_32.50)
4665 INT WINAPI
WSAIoctl(SOCKET s
, DWORD code
, LPVOID in_buff
, DWORD in_size
, LPVOID out_buff
,
4666 DWORD out_size
, LPDWORD ret_size
, LPWSAOVERLAPPED overlapped
,
4667 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
4670 DWORD status
= 0, total
= 0;
4672 TRACE("%04lx, %s, %p, %d, %p, %d, %p, %p, %p\n",
4673 s
, debugstr_wsaioctl(code
), in_buff
, in_size
, out_buff
, out_size
, ret_size
, overlapped
, completion
);
4678 if (in_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(in_buff
))
4680 SetLastError(WSAEFAULT
);
4681 return SOCKET_ERROR
;
4683 TRACE("-> FIONBIO (%x)\n", *(WS_u_long
*)in_buff
);
4684 if (_get_sock_mask(s
))
4686 /* AsyncSelect()'ed sockets are always nonblocking */
4687 if (!*(WS_u_long
*)in_buff
) status
= WSAEINVAL
;
4690 if (*(WS_u_long
*)in_buff
)
4691 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
4693 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
4700 socklen_t len
= sizeof(listening
);
4702 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
4704 SetLastError(WSAEFAULT
);
4705 return SOCKET_ERROR
;
4707 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
4710 /* On Linux, FIONREAD on listening socket always fails (see tcp(7)).
4711 However, it succeeds on native. */
4712 if (!getsockopt( fd
, SOL_SOCKET
, SO_ACCEPTCONN
, &listening
, &len
) && listening
)
4713 (*(WS_u_long
*) out_buff
) = 0;
4716 if (ioctl(fd
, FIONREAD
, out_buff
) == -1)
4717 status
= wsaErrno();
4718 release_sock_fd( s
, fd
);
4724 unsigned int oob
= 0, atmark
= 0;
4725 socklen_t oobsize
= sizeof(int);
4726 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
4728 SetLastError(WSAEFAULT
);
4729 return SOCKET_ERROR
;
4731 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
4732 /* SO_OOBINLINE sockets must always return TRUE to SIOCATMARK */
4733 if ((getsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &oob
, &oobsize
) == -1)
4734 || (!oob
&& ioctl(fd
, SIOCATMARK
, &atmark
) == -1))
4735 status
= wsaErrno();
4738 /* The SIOCATMARK value read from ioctl() is reversed
4739 * because BSD returns TRUE if it's in the OOB mark
4740 * while Windows returns TRUE if there are NO OOB bytes.
4742 (*(WS_u_long
*) out_buff
) = oob
|| !atmark
;
4745 release_sock_fd( s
, fd
);
4750 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
4751 SetLastError(WSAEINVAL
);
4752 return SOCKET_ERROR
;
4754 case WS_SIO_GET_INTERFACE_LIST
:
4756 INTERFACE_INFO
* intArray
= out_buff
;
4757 DWORD size
, numInt
= 0, apiReturn
;
4759 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
4761 if (!out_buff
|| !ret_size
)
4763 SetLastError(WSAEFAULT
);
4764 return SOCKET_ERROR
;
4767 fd
= get_sock_fd( s
, 0, NULL
);
4768 if (fd
== -1) return SOCKET_ERROR
;
4770 apiReturn
= GetAdaptersInfo(NULL
, &size
);
4771 if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
4773 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
4777 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
4779 PIP_ADAPTER_INFO ptr
;
4781 for (ptr
= table
, numInt
= 0; ptr
; ptr
= ptr
->Next
)
4783 unsigned int addr
, mask
, bcast
;
4784 struct ifreq ifInfo
;
4786 /* Skip interfaces without an IPv4 address. */
4787 if (ptr
->IpAddressList
.IpAddress
.String
[0] == '\0')
4790 if ((numInt
+ 1)*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > out_size
)
4792 WARN("Buffer too small = %u, out_size = %u\n", numInt
+ 1, out_size
);
4797 /* Socket Status Flags */
4798 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
4799 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
4801 ERR("Error obtaining status flags for socket!\n");
4807 /* set flags; the values of IFF_* are not the same
4808 under Linux and Windows, therefore must generate
4810 intArray
->iiFlags
= 0;
4811 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
4812 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
4813 #ifdef IFF_POINTOPOINT
4814 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
4815 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
4817 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
4818 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
4819 if (ifInfo
.ifr_flags
& IFF_UP
)
4820 intArray
->iiFlags
|= WS_IFF_UP
;
4821 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
4822 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
4825 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
4826 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
4827 bcast
= addr
| ~mask
;
4828 intArray
->iiAddress
.AddressIn
.sin_family
= WS_AF_INET
;
4829 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
4830 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
= addr
;
4832 intArray
->iiNetmask
.AddressIn
.sin_family
= WS_AF_INET
;
4833 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
4834 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
= mask
;
4836 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
= WS_AF_INET
;
4837 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
4838 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.WS_s_addr
= bcast
;
4845 ERR("Unable to get interface table!\n");
4848 HeapFree(GetProcessHeap(),0,table
);
4850 else status
= WSAEINVAL
;
4852 else if (apiReturn
!= ERROR_NO_DATA
)
4854 ERR("Unable to get interface table!\n");
4857 /* Calculate the size of the array being returned */
4858 total
= sizeof(INTERFACE_INFO
) * numInt
;
4859 release_sock_fd( s
, fd
);
4863 case WS_SIO_ADDRESS_LIST_QUERY
:
4867 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
4871 SetLastError(WSAEFAULT
);
4872 return SOCKET_ERROR
;
4875 if (out_size
&& out_size
< FIELD_OFFSET(SOCKET_ADDRESS_LIST
, Address
[0]))
4878 SetLastError(WSAEINVAL
);
4879 return SOCKET_ERROR
;
4882 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
4884 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
4885 SOCKET_ADDRESS_LIST
*sa_list
;
4886 SOCKADDR_IN
*sockaddr
;
4891 if (!table
|| GetAdaptersInfo(table
, &size
))
4893 HeapFree(GetProcessHeap(), 0, table
);
4898 for (p
= table
, num
= 0; p
; p
= p
->Next
)
4899 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
4901 total
= FIELD_OFFSET(SOCKET_ADDRESS_LIST
, Address
[num
]) + num
* sizeof(*sockaddr
);
4902 if (total
> out_size
|| !out_buff
)
4905 HeapFree(GetProcessHeap(), 0, table
);
4911 sa
= sa_list
->Address
;
4912 sockaddr
= (SOCKADDR_IN
*)&sa
[num
];
4913 sa_list
->iAddressCount
= num
;
4915 for (p
= table
, i
= 0; p
; p
= p
->Next
)
4917 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
4919 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
4920 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
4922 sockaddr
[i
].sin_family
= WS_AF_INET
;
4923 sockaddr
[i
].sin_port
= 0;
4924 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
4928 HeapFree(GetProcessHeap(), 0, table
);
4932 WARN("unable to get IP address list\n");
4939 FIXME("SIO_FLUSH: stub.\n");
4942 case WS_SIO_GET_EXTENSION_FUNCTION_POINTER
:
4944 #define EXTENSION_FUNCTION(x, y) { x, y, #y },
4951 EXTENSION_FUNCTION(WSAID_CONNECTEX
, WS2_ConnectEx
)
4952 EXTENSION_FUNCTION(WSAID_DISCONNECTEX
, WS2_DisconnectEx
)
4953 EXTENSION_FUNCTION(WSAID_ACCEPTEX
, WS2_AcceptEx
)
4954 EXTENSION_FUNCTION(WSAID_GETACCEPTEXSOCKADDRS
, WS2_GetAcceptExSockaddrs
)
4955 EXTENSION_FUNCTION(WSAID_TRANSMITFILE
, WS2_TransmitFile
)
4956 /* EXTENSION_FUNCTION(WSAID_TRANSMITPACKETS, WS2_TransmitPackets) */
4957 EXTENSION_FUNCTION(WSAID_WSARECVMSG
, WS2_WSARecvMsg
)
4958 EXTENSION_FUNCTION(WSAID_WSASENDMSG
, WSASendMsg
)
4960 #undef EXTENSION_FUNCTION
4964 for (i
= 0; i
< ARRAY_SIZE(guid_funcs
); i
++)
4966 if (IsEqualGUID(&guid_funcs
[i
].guid
, in_buff
))
4975 TRACE("-> got %s\n", guid_funcs
[i
].name
);
4976 *(void **)out_buff
= guid_funcs
[i
].func_ptr
;
4977 total
= sizeof(void *);
4981 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER %s: stub\n", debugstr_guid(in_buff
));
4982 status
= WSAEOPNOTSUPP
;
4985 case WS_SIO_KEEPALIVE_VALS
:
4987 struct tcp_keepalive
*k
;
4988 int keepalive
, keepidle
, keepintvl
;
4990 if (!in_buff
|| in_size
< sizeof(struct tcp_keepalive
))
4992 SetLastError(WSAEFAULT
);
4993 return SOCKET_ERROR
;
4997 keepalive
= k
->onoff
? 1 : 0;
4998 keepidle
= max( 1, (k
->keepalivetime
+ 500) / 1000 );
4999 keepintvl
= max( 1, (k
->keepaliveinterval
+ 500) / 1000 );
5001 TRACE("onoff: %d, keepalivetime: %d, keepaliveinterval: %d\n", keepalive
, keepidle
, keepintvl
);
5003 fd
= get_sock_fd(s
, 0, NULL
);
5004 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&keepalive
, sizeof(int)) == -1)
5006 #if defined(TCP_KEEPIDLE) || defined(TCP_KEEPINTVL)
5007 /* these values need to be set only if SO_KEEPALIVE is enabled */
5010 #ifndef TCP_KEEPIDLE
5011 FIXME("ignoring keepalive timeout\n");
5013 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, (void *)&keepidle
, sizeof(int)) == -1)
5017 #ifdef TCP_KEEPINTVL
5018 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, (void *)&keepintvl
, sizeof(int)) == -1)
5021 FIXME("ignoring keepalive interval\n");
5026 FIXME("ignoring keepalive interval and timeout\n");
5028 release_sock_fd(s
, fd
);
5031 case WS_SIO_ROUTING_INTERFACE_QUERY
:
5033 struct WS_sockaddr
*daddr
= (struct WS_sockaddr
*)in_buff
;
5034 struct WS_sockaddr_in
*daddr_in
= (struct WS_sockaddr_in
*)daddr
;
5035 struct WS_sockaddr_in
*saddr_in
= out_buff
;
5036 MIB_IPFORWARDROW row
;
5037 PMIB_IPADDRTABLE ipAddrTable
= NULL
;
5038 DWORD size
, i
, found_index
;
5040 TRACE("-> WS_SIO_ROUTING_INTERFACE_QUERY request\n");
5042 if (!in_buff
|| in_size
< sizeof(struct WS_sockaddr
) ||
5043 !out_buff
|| out_size
< sizeof(struct WS_sockaddr_in
) || !ret_size
)
5045 SetLastError(WSAEFAULT
);
5046 return SOCKET_ERROR
;
5048 if (daddr
->sa_family
!= WS_AF_INET
)
5050 FIXME("unsupported address family %d\n", daddr
->sa_family
);
5051 status
= WSAEAFNOSUPPORT
;
5054 if (GetBestRoute(daddr_in
->sin_addr
.S_un
.S_addr
, 0, &row
) != NOERROR
||
5055 GetIpAddrTable(NULL
, &size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
5060 ipAddrTable
= HeapAlloc(GetProcessHeap(), 0, size
);
5061 if (GetIpAddrTable(ipAddrTable
, &size
, FALSE
))
5063 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
5067 for (i
= 0, found_index
= ipAddrTable
->dwNumEntries
;
5068 i
< ipAddrTable
->dwNumEntries
; i
++)
5070 if (ipAddrTable
->table
[i
].dwIndex
== row
.dwForwardIfIndex
)
5073 if (found_index
== ipAddrTable
->dwNumEntries
)
5075 ERR("no matching IP address for interface %d\n",
5076 row
.dwForwardIfIndex
);
5077 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
5081 saddr_in
->sin_family
= WS_AF_INET
;
5082 saddr_in
->sin_addr
.S_un
.S_addr
= ipAddrTable
->table
[found_index
].dwAddr
;
5083 saddr_in
->sin_port
= 0;
5084 total
= sizeof(struct WS_sockaddr_in
);
5085 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
5088 case WS_SIO_SET_COMPATIBILITY_MODE
:
5089 TRACE("WS_SIO_SET_COMPATIBILITY_MODE ignored\n");
5090 status
= WSAEOPNOTSUPP
;
5092 case WS_SIO_UDP_CONNRESET
:
5093 FIXME("WS_SIO_UDP_CONNRESET stub\n");
5095 case 0x667e: /* Netscape tries hard to use bogus ioctl 0x667e */
5096 SetLastError(WSAEOPNOTSUPP
);
5097 return SOCKET_ERROR
;
5099 status
= WSAEOPNOTSUPP
;
5103 if (status
== WSAEOPNOTSUPP
)
5105 status
= server_ioctl_sock(s
, code
, in_buff
, in_size
, out_buff
, out_size
, &total
,
5106 overlapped
, completion
);
5107 if (status
!= WSAEOPNOTSUPP
)
5109 if (status
== 0 || status
== WSA_IO_PENDING
)
5110 TRACE("-> %s request\n", debugstr_wsaioctl(code
));
5112 ERR("-> %s request failed with status 0x%x\n", debugstr_wsaioctl(code
), status
);
5114 /* overlapped and completion operations will be handled by the server */
5119 FIXME("unsupported WS_IOCTL cmd (%s)\n", debugstr_wsaioctl(code
));
5124 FIXME( "completion routine %p not supported\n", completion
);
5126 else if (overlapped
)
5128 ULONG_PTR cvalue
= (overlapped
&& ((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)overlapped
: 0;
5129 overlapped
->Internal
= sock_error_to_ntstatus( status
);
5130 overlapped
->InternalHigh
= total
;
5131 if (cvalue
) WS_AddCompletion( HANDLE2SOCKET(s
), cvalue
, overlapped
->Internal
, total
, FALSE
);
5132 if (overlapped
->hEvent
) NtSetEvent( overlapped
->hEvent
, NULL
);
5137 if (ret_size
) *ret_size
= total
;
5140 SetLastError( status
);
5141 return SOCKET_ERROR
;
5145 /***********************************************************************
5146 * ioctlsocket (WS2_32.10)
5148 int WINAPI
WS_ioctlsocket(SOCKET s
, LONG cmd
, WS_u_long
*argp
)
5151 return WSAIoctl( s
, cmd
, argp
, sizeof(WS_u_long
), argp
, sizeof(WS_u_long
), &ret_size
, NULL
, NULL
);
5154 /***********************************************************************
5155 * listen (WS2_32.13)
5157 int WINAPI
WS_listen(SOCKET s
, int backlog
)
5159 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
), ret
= SOCKET_ERROR
;
5161 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
5164 int bound
= is_fd_bound(fd
, NULL
, NULL
);
5168 SetLastError(bound
== -1 ? wsaErrno() : WSAEINVAL
);
5170 else if (listen(fd
, backlog
) == 0)
5172 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
5174 FD_CONNECT
|FD_WINE_CONNECTED
);
5178 SetLastError(wsaErrno());
5179 release_sock_fd( s
, fd
);
5182 SetLastError(WSAENOTSOCK
);
5186 /***********************************************************************
5189 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
5191 DWORD n
, dwFlags
= flags
;
5197 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
5198 return SOCKET_ERROR
;
5203 /***********************************************************************
5204 * recvfrom (WS2_32.17)
5206 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
5207 struct WS_sockaddr
*from
, int *fromlen
)
5209 DWORD n
, dwFlags
= flags
;
5215 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
5216 return SOCKET_ERROR
;
5221 /* allocate a poll array for the corresponding fd sets */
5222 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
5223 const WS_fd_set
*exceptfds
, int *count_ptr
)
5225 unsigned int i
, j
= 0, count
= 0;
5227 struct per_thread_data
*ptb
= get_per_thread_data();
5229 if (readfds
) count
+= readfds
->fd_count
;
5230 if (writefds
) count
+= writefds
->fd_count
;
5231 if (exceptfds
) count
+= exceptfds
->fd_count
;
5235 SetLastError(WSAEINVAL
);
5239 /* check if the cache can hold all descriptors, if not do the resizing */
5240 if (ptb
->fd_count
< count
)
5242 if (!(fds
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(fds
[0]))))
5244 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
5247 HeapFree(GetProcessHeap(), 0, ptb
->fd_cache
);
5248 ptb
->fd_cache
= fds
;
5249 ptb
->fd_count
= count
;
5252 fds
= ptb
->fd_cache
;
5255 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
5257 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
5258 if (fds
[j
].fd
== -1) goto failed
;
5260 if (is_fd_bound(fds
[j
].fd
, NULL
, NULL
) == 1)
5262 fds
[j
].events
= POLLIN
;
5266 release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
5272 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
5274 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
5275 if (fds
[j
].fd
== -1) goto failed
;
5277 if (is_fd_bound(fds
[j
].fd
, NULL
, NULL
) == 1 ||
5278 _get_fd_type(fds
[j
].fd
) == SOCK_DGRAM
)
5280 fds
[j
].events
= POLLOUT
;
5284 release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
5290 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
5292 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
5293 if (fds
[j
].fd
== -1) goto failed
;
5295 if (is_fd_bound(fds
[j
].fd
, NULL
, NULL
) == 1)
5297 int oob_inlined
= 0;
5298 socklen_t olen
= sizeof(oob_inlined
);
5300 fds
[j
].events
= POLLHUP
;
5302 /* Check if we need to test for urgent data or not */
5303 getsockopt(fds
[j
].fd
, SOL_SOCKET
, SO_OOBINLINE
, (char*) &oob_inlined
, &olen
);
5305 fds
[j
].events
|= POLLPRI
;
5309 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
5320 for (i
= 0; i
< readfds
->fd_count
&& j
< count
; i
++, j
++)
5321 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
5323 for (i
= 0; i
< writefds
->fd_count
&& j
< count
; i
++, j
++)
5324 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
5326 for (i
= 0; i
< exceptfds
->fd_count
&& j
< count
; i
++, j
++)
5327 if (fds
[j
].fd
!= -1) release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
5331 /* release the file descriptor obtained in fd_sets_to_poll */
5332 /* must be called with the original fd_set arrays, before calling get_poll_results */
5333 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
5334 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
5336 unsigned int i
, j
= 0;
5340 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
5341 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
5345 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
5346 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
5350 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
5352 if (fds
[j
].fd
== -1) continue;
5353 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
5354 if (fds
[j
].revents
& POLLHUP
)
5356 int fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
5358 release_sock_fd( exceptfds
->fd_array
[i
], fd
);
5366 static int do_poll(struct pollfd
*pollfds
, int count
, int timeout
)
5368 struct timeval tv1
, tv2
;
5369 int ret
, torig
= timeout
;
5371 if (timeout
> 0) gettimeofday( &tv1
, 0 );
5373 while ((ret
= poll( pollfds
, count
, timeout
)) < 0)
5375 if (errno
!= EINTR
) break;
5376 if (timeout
< 0) continue;
5377 if (timeout
== 0) return 0;
5379 gettimeofday( &tv2
, 0 );
5381 tv2
.tv_sec
-= tv1
.tv_sec
;
5382 tv2
.tv_usec
-= tv1
.tv_usec
;
5383 if (tv2
.tv_usec
< 0)
5385 tv2
.tv_usec
+= 1000000;
5389 timeout
= torig
- (tv2
.tv_sec
* 1000) - (tv2
.tv_usec
+ 999) / 1000;
5390 if (timeout
<= 0) return 0;
5395 /* map the poll results back into the Windows fd sets */
5396 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
5397 const struct pollfd
*fds
)
5399 const struct pollfd
*poll_writefds
= fds
+ (readfds
? readfds
->fd_count
: 0);
5400 const struct pollfd
*poll_exceptfds
= poll_writefds
+ (writefds
? writefds
->fd_count
: 0);
5401 unsigned int i
, k
, total
= 0;
5405 for (i
= k
= 0; i
< readfds
->fd_count
; i
++)
5407 if (fds
[i
].revents
||
5408 (readfds
== writefds
&& (poll_writefds
[i
].revents
& POLLOUT
) && !(poll_writefds
[i
].revents
& POLLHUP
)) ||
5409 (readfds
== exceptfds
&& poll_exceptfds
[i
].revents
))
5410 readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
5412 readfds
->fd_count
= k
;
5415 if (writefds
&& writefds
!= readfds
)
5417 for (i
= k
= 0; i
< writefds
->fd_count
; i
++)
5419 if (((poll_writefds
[i
].revents
& POLLOUT
) && !(poll_writefds
[i
].revents
& POLLHUP
)) ||
5420 (writefds
== exceptfds
&& poll_exceptfds
[i
].revents
))
5421 writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
5423 writefds
->fd_count
= k
;
5426 if (exceptfds
&& exceptfds
!= readfds
&& exceptfds
!= writefds
)
5428 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++)
5429 if (poll_exceptfds
[i
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
5430 exceptfds
->fd_count
= k
;
5436 /***********************************************************************
5437 * select (WS2_32.18)
5439 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
5440 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
5441 const struct WS_timeval
* ws_timeout
)
5443 struct pollfd
*pollfds
;
5444 int count
, ret
, timeout
= -1;
5446 TRACE("read %p, write %p, excp %p timeout %p\n",
5447 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
5449 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)))
5450 return SOCKET_ERROR
;
5453 timeout
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
5455 ret
= do_poll(pollfds
, count
, timeout
);
5456 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
5458 if (ret
== -1) SetLastError(wsaErrno());
5459 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
5463 /***********************************************************************
5466 int WINAPI
WSAPoll(WSAPOLLFD
*wfds
, ULONG count
, int timeout
)
5469 struct pollfd
*ufds
;
5473 SetLastError(WSAEINVAL
);
5474 return SOCKET_ERROR
;
5478 SetLastError(WSAEFAULT
);
5479 return SOCKET_ERROR
;
5482 if (!(ufds
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(ufds
[0]))))
5484 SetLastError(WSAENOBUFS
);
5485 return SOCKET_ERROR
;
5488 for (i
= 0; i
< count
; i
++)
5490 ufds
[i
].fd
= get_sock_fd(wfds
[i
].fd
, 0, NULL
);
5491 ufds
[i
].events
= convert_poll_w2u(wfds
[i
].events
);
5492 ufds
[i
].revents
= 0;
5495 ret
= do_poll(ufds
, count
, timeout
);
5497 for (i
= 0; i
< count
; i
++)
5499 if (ufds
[i
].fd
!= -1)
5501 release_sock_fd(wfds
[i
].fd
, ufds
[i
].fd
);
5502 if (ufds
[i
].revents
& POLLHUP
)
5504 /* Check if the socket still exists */
5505 int fd
= get_sock_fd(wfds
[i
].fd
, 0, NULL
);
5508 wfds
[i
].revents
= WS_POLLHUP
;
5509 release_sock_fd(wfds
[i
].fd
, fd
);
5512 wfds
[i
].revents
= WS_POLLNVAL
;
5515 wfds
[i
].revents
= convert_poll_u2w(ufds
[i
].revents
);
5518 wfds
[i
].revents
= WS_POLLNVAL
;
5521 HeapFree(GetProcessHeap(), 0, ufds
);
5525 /* helper to send completion messages for client-only i/o operation case */
5526 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
,
5527 ULONG Information
, BOOL async
)
5529 SERVER_START_REQ( add_fd_completion
)
5531 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(sock
) );
5532 req
->cvalue
= CompletionValue
;
5533 req
->status
= CompletionStatus
;
5534 req
->information
= Information
;
5536 wine_server_call( req
);
5542 /***********************************************************************
5545 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
5551 wsabuf
.buf
= (char*) buf
;
5553 if ( WS2_sendto( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
5554 return SOCKET_ERROR
;
5559 /***********************************************************************
5560 * WSASend (WS2_32.72)
5562 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5563 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
5564 LPWSAOVERLAPPED lpOverlapped
,
5565 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5567 return WS2_sendto( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
5568 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
5571 /***********************************************************************
5572 * WSASendDisconnect (WS2_32.73)
5574 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
5576 return WS_shutdown( s
, SD_SEND
);
5580 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5581 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
5582 const struct WS_sockaddr
*to
, int tolen
,
5583 LPWSAOVERLAPPED lpOverlapped
,
5584 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5586 unsigned int i
, options
;
5587 int n
, fd
, err
, overlapped
, flags
;
5588 struct ws2_async
*wsa
= NULL
, localwsa
;
5589 int totalLength
= 0;
5593 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
5594 s
, lpBuffers
, dwBufferCount
, dwFlags
,
5595 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
5597 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
5598 TRACE( "fd=%d, options=%x\n", fd
, options
);
5600 if ( fd
== -1 ) return SOCKET_ERROR
;
5602 if (!lpOverlapped
&& !lpNumberOfBytesSent
)
5608 overlapped
= (lpOverlapped
|| lpCompletionRoutine
) &&
5609 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
5610 if (overlapped
|| dwBufferCount
> 1)
5612 if (!(wsa
= (struct ws2_async
*)alloc_async_io( offsetof(struct ws2_async
, iovec
[dwBufferCount
]),
5622 wsa
->hSocket
= SOCKET2HANDLE(s
);
5623 wsa
->addr
= (struct WS_sockaddr
*)to
;
5624 wsa
->addrlen
.val
= tolen
;
5625 wsa
->flags
= dwFlags
;
5626 wsa
->lpFlags
= &wsa
->flags
;
5627 wsa
->control
= NULL
;
5628 wsa
->n_iovecs
= dwBufferCount
;
5629 wsa
->first_iovec
= 0;
5630 for ( i
= 0; i
< dwBufferCount
; i
++ )
5632 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
5633 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
5634 totalLength
+= lpBuffers
[i
].len
;
5637 flags
= convert_flags(dwFlags
);
5638 n
= WS2_send( fd
, wsa
, flags
);
5639 if (n
== -1 && errno
!= EAGAIN
)
5647 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
5648 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
5650 wsa
->user_overlapped
= lpOverlapped
;
5651 wsa
->completion_func
= lpCompletionRoutine
;
5652 release_sock_fd( s
, fd
);
5654 if (n
== -1 || n
< totalLength
)
5656 iosb
->u
.Status
= STATUS_PENDING
;
5657 iosb
->Information
= n
== -1 ? 0 : n
;
5659 if (wsa
->completion_func
)
5660 err
= register_async( ASYNC_TYPE_WRITE
, wsa
->hSocket
, &wsa
->io
, NULL
,
5661 ws2_async_apc
, wsa
, iosb
);
5663 err
= register_async( ASYNC_TYPE_WRITE
, wsa
->hSocket
, &wsa
->io
, lpOverlapped
->hEvent
,
5664 NULL
, (void *)cvalue
, iosb
);
5666 /* Enable the event only after starting the async. The server will deliver it as soon as
5667 the async is done. */
5668 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
5670 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
5671 SetLastError(NtStatusToWSAError( err
));
5672 return SOCKET_ERROR
;
5675 iosb
->u
.Status
= STATUS_SUCCESS
;
5676 iosb
->Information
= n
;
5677 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= n
;
5678 if (!wsa
->completion_func
)
5680 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
, FALSE
);
5681 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
5682 HeapFree( GetProcessHeap(), 0, wsa
);
5684 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
5685 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
5686 SetLastError(ERROR_SUCCESS
);
5690 if ((err
= sock_is_blocking( s
, &is_blocking
))) goto error
;
5694 /* On a blocking non-overlapped stream socket,
5695 * sending blocks until the entire buffer is sent. */
5696 DWORD timeout_start
= GetTickCount();
5698 bytes_sent
= n
== -1 ? 0 : n
;
5700 while (wsa
->first_iovec
< wsa
->n_iovecs
)
5703 int poll_timeout
= -1;
5704 INT64 timeout
= get_rcvsnd_timeo(fd
, FALSE
);
5708 timeout
-= GetTickCount() - timeout_start
;
5709 if (timeout
< 0) poll_timeout
= 0;
5710 else poll_timeout
= timeout
<= INT_MAX
? timeout
: INT_MAX
;
5714 pfd
.events
= POLLOUT
;
5716 if (!poll_timeout
|| !poll( &pfd
, 1, poll_timeout
))
5719 goto error
; /* msdn says a timeout in send is fatal */
5722 n
= WS2_send( fd
, wsa
, flags
);
5723 if (n
== -1 && errno
!= EAGAIN
)
5733 else /* non-blocking */
5735 if (n
< totalLength
)
5736 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
5739 err
= WSAEWOULDBLOCK
;
5745 TRACE(" -> %i bytes\n", bytes_sent
);
5747 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= bytes_sent
;
5748 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
5749 release_sock_fd( s
, fd
);
5750 SetLastError(ERROR_SUCCESS
);
5754 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
5755 release_sock_fd( s
, fd
);
5756 WARN(" -> ERROR %d\n", err
);
5758 return SOCKET_ERROR
;
5761 /***********************************************************************
5762 * WSASendTo (WS2_32.74)
5764 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5765 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
5766 const struct WS_sockaddr
*to
, int tolen
,
5767 LPWSAOVERLAPPED lpOverlapped
,
5768 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5770 return WS2_sendto( s
, lpBuffers
, dwBufferCount
,
5771 lpNumberOfBytesSent
, dwFlags
,
5773 lpOverlapped
, lpCompletionRoutine
);
5776 /***********************************************************************
5777 * sendto (WS2_32.20)
5779 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
5780 const struct WS_sockaddr
*to
, int tolen
)
5786 wsabuf
.buf
= (char*) buf
;
5788 if ( WS2_sendto(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
5789 return SOCKET_ERROR
;
5794 /***********************************************************************
5795 * setsockopt (WS2_32.21)
5797 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
5798 const char *optval
, int optlen
)
5802 struct linger linger
;
5803 struct timeval tval
;
5805 TRACE("(socket %04lx, %s, optval %s, optlen %d)\n", s
,
5806 debugstr_sockopt(level
, optname
), debugstr_optval(optval
, optlen
),
5809 /* some broken apps pass the value directly instead of a pointer to it */
5810 if(optlen
&& IS_INTRESOURCE(optval
))
5812 SetLastError(WSAEFAULT
);
5813 return SOCKET_ERROR
;
5821 /* Some options need some conversion before they can be sent to
5822 * setsockopt. The conversions are done here, then they will fall through
5823 * to the general case. Special options that are not passed to
5824 * setsockopt follow below that.*/
5826 case WS_SO_DONTLINGER
:
5829 SetLastError(WSAEFAULT
);
5830 return SOCKET_ERROR
;
5832 linger
.l_onoff
= *(const int*)optval
== 0;
5833 linger
.l_linger
= 0;
5835 optname
= SO_LINGER
;
5836 optval
= (char*)&linger
;
5837 optlen
= sizeof(struct linger
);
5843 SetLastError(WSAEFAULT
);
5844 return SOCKET_ERROR
;
5846 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
5847 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
5849 optname
= SO_LINGER
;
5850 optval
= (char*)&linger
;
5851 optlen
= sizeof(struct linger
);
5855 if (!*(const int *)optval
)
5857 FIXME("SO_SNDBUF ignoring request to disable send buffering\n");
5862 convert_sockopt(&level
, &optname
);
5866 if (*(const int*)optval
< 2048)
5868 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
5873 /* The options listed here don't need any special handling. Thanks to
5874 * the conversion happening above, options from there will fall through
5876 case WS_SO_ACCEPTCONN
:
5877 case WS_SO_BROADCAST
:
5879 case WS_SO_KEEPALIVE
:
5880 case WS_SO_OOBINLINE
:
5881 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
5882 * however, using it the BSD way fixes bug 8513 and seems to be what
5883 * most programmers assume, anyway */
5884 case WS_SO_REUSEADDR
:
5886 convert_sockopt(&level
, &optname
);
5889 /* SO_DEBUG is a privileged operation, ignore it. */
5891 TRACE("Ignoring SO_DEBUG\n");
5894 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
5895 * socket. According to MSDN, this option is silently ignored.*/
5896 case WS_SO_DONTROUTE
:
5897 TRACE("Ignoring SO_DONTROUTE\n");
5900 /* Stops two sockets from being bound to the same port. Always happens
5901 * on unix systems, so just drop it. */
5902 case WS_SO_EXCLUSIVEADDRUSE
:
5903 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
5906 /* After a ConnectEx call succeeds, the socket can't be used with half of the
5907 * normal winsock functions on windows. We don't have that problem. */
5908 case WS_SO_UPDATE_CONNECT_CONTEXT
:
5909 TRACE("Ignoring SO_UPDATE_CONNECT_CONTEXT, since our sockets are normal\n");
5912 /* After a AcceptEx call succeeds, the socket can't be used with half of the
5913 * normal winsock functions on windows. We don't have that problem. */
5914 case WS_SO_UPDATE_ACCEPT_CONTEXT
:
5915 TRACE("Ignoring SO_UPDATE_ACCEPT_CONTEXT, since our sockets are normal\n");
5918 /* SO_OPENTYPE does not require a valid socket handle. */
5919 case WS_SO_OPENTYPE
:
5920 if (!optlen
|| optlen
< sizeof(int) || !optval
)
5922 SetLastError(WSAEFAULT
);
5923 return SOCKET_ERROR
;
5925 get_per_thread_data()->opentype
= *(const int *)optval
;
5926 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((const int*)optval
) );
5930 case WS_SO_RCVTIMEO
:
5933 case WS_SO_SNDTIMEO
:
5935 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
5936 if (optval
&& optlen
== sizeof(UINT32
)) {
5937 /* WinSock passes milliseconds instead of struct timeval */
5938 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
5939 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
5940 /* min of 500 milliseconds */
5941 if (tval
.tv_sec
== 0 && tval
.tv_usec
&& tval
.tv_usec
< 500000)
5942 tval
.tv_usec
= 500000;
5943 optlen
= sizeof(struct timeval
);
5944 optval
= (char*)&tval
;
5945 } else if (optlen
== sizeof(struct timeval
)) {
5946 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
5948 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
5951 convert_sockopt(&level
, &optname
);
5955 case WS_SO_RANDOMIZE_PORT
:
5956 FIXME("Ignoring WS_SO_RANDOMIZE_PORT\n");
5959 case WS_SO_PORT_SCALABILITY
:
5960 FIXME("Ignoring WS_SO_PORT_SCALABILITY\n");
5963 case WS_SO_REUSE_UNICASTPORT
:
5964 FIXME("Ignoring WS_SO_REUSE_UNICASTPORT\n");
5967 case WS_SO_REUSE_MULTICASTPORT
:
5968 FIXME("Ignoring WS_SO_REUSE_MULTICASTPORT\n");
5972 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
5973 SetLastError(WSAENOPROTOOPT
);
5974 return SOCKET_ERROR
;
5976 break; /* case WS_SOL_SOCKET */
5979 case WS_NSPROTO_IPX
:
5983 return set_ipx_packettype(s
, *(int*)optval
);
5985 case WS_IPX_FILTERPTYPE
:
5986 /* Sets the receive filter packet type, at the moment we don't support it */
5987 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
5988 /* Returning 0 is better for now than returning a SOCKET_ERROR */
5992 FIXME("opt_name:%x\n", optname
);
5993 return SOCKET_ERROR
;
5995 break; /* case WS_NSPROTO_IPX */
5998 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
5999 case WS_IPPROTO_TCP
:
6002 case WS_TCP_NODELAY
:
6003 convert_sockopt(&level
, &optname
);
6006 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
6007 return SOCKET_ERROR
;
6014 case WS_IP_ADD_MEMBERSHIP
:
6015 case WS_IP_DROP_MEMBERSHIP
:
6019 case WS_IP_MULTICAST_IF
:
6020 case WS_IP_MULTICAST_LOOP
:
6021 case WS_IP_MULTICAST_TTL
:
6023 #if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
6028 #ifdef IP_UNICAST_IF
6029 case WS_IP_UNICAST_IF
:
6031 convert_sockopt(&level
, &optname
);
6033 case WS_IP_DONTFRAGMENT
:
6034 return set_dont_fragment(s
, IPPROTO_IP
, *(BOOL
*)optval
) ? 0 : SOCKET_ERROR
;
6036 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
6037 return SOCKET_ERROR
;
6041 case WS_IPPROTO_IPV6
:
6044 #ifdef IPV6_ADD_MEMBERSHIP
6045 case WS_IPV6_ADD_MEMBERSHIP
:
6047 #ifdef IPV6_DROP_MEMBERSHIP
6048 case WS_IPV6_DROP_MEMBERSHIP
:
6050 case WS_IPV6_MULTICAST_IF
:
6051 case WS_IPV6_MULTICAST_HOPS
:
6052 case WS_IPV6_MULTICAST_LOOP
:
6053 case WS_IPV6_UNICAST_HOPS
:
6054 #ifdef IPV6_UNICAST_IF
6055 case WS_IPV6_UNICAST_IF
:
6057 convert_sockopt(&level
, &optname
);
6059 case WS_IPV6_DONTFRAG
:
6060 return set_dont_fragment(s
, IPPROTO_IPV6
, *(BOOL
*)optval
) ? 0 : SOCKET_ERROR
;
6061 case WS_IPV6_PROTECTION_LEVEL
:
6062 FIXME("IPV6_PROTECTION_LEVEL is ignored!\n");
6064 case WS_IPV6_V6ONLY
:
6066 union generic_unix_sockaddr uaddr
;
6070 fd
= get_sock_fd( s
, 0, NULL
);
6071 if (fd
== -1) return SOCKET_ERROR
;
6073 bound
= is_fd_bound(fd
, &uaddr
, &uaddrlen
);
6074 release_sock_fd( s
, fd
);
6075 if (bound
== 0 && uaddr
.addr
.sa_family
== AF_INET
)
6077 /* Changing IPV6_V6ONLY succeeds on AF_INET (IPv4) socket
6078 * on Windows (with IPv6 support) if the socket is unbound.
6079 * It is essentially a noop, though Windows does store the value
6081 WARN("Silently ignoring IPPROTO_IPV6+IPV6_V6ONLY on AF_INET socket\n");
6084 level
= IPPROTO_IPV6
;
6085 optname
= IPV6_V6ONLY
;
6089 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
6090 return SOCKET_ERROR
;
6095 WARN("Unknown level: 0x%08x\n", level
);
6096 SetLastError(WSAEINVAL
);
6097 return SOCKET_ERROR
;
6098 } /* end switch(level) */
6100 /* avoid endianness issues if argument is a 16-bit int */
6101 if (optval
&& optlen
< sizeof(int))
6103 woptval
= *((const INT16
*) optval
);
6104 optval
= (char*) &woptval
;
6105 woptval
&= (1 << optlen
* 8) - 1;
6108 fd
= get_sock_fd( s
, 0, NULL
);
6109 if (fd
== -1) return SOCKET_ERROR
;
6111 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
6114 if (level
== SOL_SOCKET
&& optname
== SO_REUSEADDR
&&
6115 setsockopt(fd
, level
, SO_REUSEPORT
, optval
, optlen
) != 0)
6117 SetLastError(wsaErrno());
6118 release_sock_fd( s
, fd
);
6119 return SOCKET_ERROR
;
6122 release_sock_fd( s
, fd
);
6125 TRACE("Setting socket error, %d\n", wsaErrno());
6126 SetLastError(wsaErrno());
6127 release_sock_fd( s
, fd
);
6129 return SOCKET_ERROR
;
6132 /***********************************************************************
6133 * shutdown (WS2_32.22)
6135 int WINAPI
WS_shutdown(SOCKET s
, int how
)
6137 int fd
, err
= WSAENOTSOCK
;
6138 unsigned int options
= 0, clear_flags
= 0;
6140 fd
= get_sock_fd( s
, 0, &options
);
6141 TRACE("socket %04lx, how 0x%x, options 0x%x\n", s
, how
, options
);
6144 return SOCKET_ERROR
;
6148 case SD_RECEIVE
: /* drop receives */
6149 clear_flags
|= FD_READ
;
6151 case SD_SEND
: /* drop sends */
6152 clear_flags
|= FD_WRITE
;
6154 case SD_BOTH
: /* drop all */
6155 clear_flags
|= FD_READ
|FD_WRITE
;
6158 clear_flags
|= FD_WINE_LISTENING
;
6161 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
6166 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
6169 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
6173 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
6174 if (!err
) err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
6177 if (err
) goto error
;
6179 else /* non-overlapped mode */
6181 if ( shutdown( fd
, how
) )
6188 release_sock_fd( s
, fd
);
6189 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
6190 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
6194 release_sock_fd( s
, fd
);
6195 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
6196 SetLastError( err
);
6197 return SOCKET_ERROR
;
6200 /***********************************************************************
6201 * socket (WS2_32.23)
6203 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
6205 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
6207 return WSASocketW( af
, type
, protocol
, NULL
, 0,
6208 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
6212 /***********************************************************************
6213 * gethostbyaddr (WS2_32.51)
6215 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
6217 struct WS_hostent
*retval
= NULL
;
6218 struct hostent
* host
;
6219 int unixtype
= convert_af_w2u(type
);
6220 const char *paddr
= addr
;
6221 unsigned long loopback
;
6222 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6224 int ebufsize
= 1024;
6225 struct hostent hostentry
;
6226 int locerr
= ENOBUFS
;
6229 /* convert back the magic loopback address if necessary */
6230 if (unixtype
== AF_INET
&& len
== 4 && !memcmp(addr
, magic_loopback_addr
, 4))
6232 loopback
= htonl(INADDR_LOOPBACK
);
6233 paddr
= (char*) &loopback
;
6236 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6238 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
6240 int res
= gethostbyaddr_r(paddr
, len
, unixtype
,
6241 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
6242 if (res
!= ERANGE
) break;
6244 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
6246 if (host
) retval
= WS_dup_he(host
);
6247 else SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
6248 HeapFree(GetProcessHeap(),0,extrabuf
);
6250 EnterCriticalSection( &csWSgetXXXbyYYY
);
6251 host
= gethostbyaddr(paddr
, len
, unixtype
);
6252 if (host
) retval
= WS_dup_he(host
);
6253 else SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
6254 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6256 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
6260 /***********************************************************************
6261 * WS_compare_routes_by_metric_asc (INTERNAL)
6263 * Comparison function for qsort(), for sorting two routes (struct route)
6264 * by metric in ascending order.
6266 static int WS_compare_routes_by_metric_asc(const void *left
, const void *right
)
6268 const struct route
*a
= left
, *b
= right
;
6269 if (a
->default_route
&& b
->default_route
)
6270 return a
->default_route
- b
->default_route
;
6271 if (a
->default_route
&& !b
->default_route
)
6273 if (b
->default_route
&& !a
->default_route
)
6275 return a
->metric
- b
->metric
;
6278 /***********************************************************************
6279 * WS_get_local_ips (INTERNAL)
6281 * Returns the list of local IP addresses by going through the network
6282 * adapters and using the local routing table to sort the addresses
6283 * from highest routing priority to lowest routing priority. This
6284 * functionality is inferred from the description for obtaining local
6285 * IP addresses given in the Knowledge Base Article Q160215.
6287 * Please note that the returned hostent is only freed when the thread
6288 * closes and is replaced if another hostent is requested.
6290 static struct WS_hostent
* WS_get_local_ips( char *hostname
)
6292 int numroutes
= 0, i
, j
, default_routes
= 0;
6294 PIP_ADAPTER_INFO adapters
= NULL
, k
;
6295 struct WS_hostent
*hostlist
= NULL
;
6296 PMIB_IPFORWARDTABLE routes
= NULL
;
6297 struct route
*route_addrs
= NULL
;
6298 DWORD adap_size
, route_size
;
6300 /* Obtain the size of the adapter list and routing table, also allocate memory */
6301 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
6303 if (GetIpForwardTable(NULL
, &route_size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
6305 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
6306 routes
= HeapAlloc(GetProcessHeap(), 0, route_size
);
6307 if (adapters
== NULL
|| routes
== NULL
)
6309 /* Obtain the adapter list and the full routing table */
6310 if (GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
6312 if (GetIpForwardTable(routes
, &route_size
, FALSE
) != NO_ERROR
)
6314 /* Store the interface associated with each route */
6315 for (n
= 0; n
< routes
->dwNumEntries
; n
++)
6318 DWORD ifmetric
, ifdefault
= 0;
6319 BOOL exists
= FALSE
;
6321 /* Check if this is a default route (there may be more than one) */
6322 if (!routes
->table
[n
].dwForwardDest
)
6323 ifdefault
= ++default_routes
;
6324 else if (routes
->table
[n
].u1
.ForwardType
!= MIB_IPROUTE_TYPE_DIRECT
)
6326 ifindex
= routes
->table
[n
].dwForwardIfIndex
;
6327 ifmetric
= routes
->table
[n
].dwForwardMetric1
;
6328 /* Only store the lowest valued metric for an interface */
6329 for (j
= 0; j
< numroutes
; j
++)
6331 if (route_addrs
[j
].interface
== ifindex
)
6333 if (route_addrs
[j
].metric
> ifmetric
)
6334 route_addrs
[j
].metric
= ifmetric
;
6340 route_addrs
= heap_realloc(route_addrs
, (numroutes
+1)*sizeof(struct route
));
6341 if (route_addrs
== NULL
)
6342 goto cleanup
; /* Memory allocation error, fail gracefully */
6343 route_addrs
[numroutes
].interface
= ifindex
;
6344 route_addrs
[numroutes
].metric
= ifmetric
;
6345 route_addrs
[numroutes
].default_route
= ifdefault
;
6346 /* If no IP is found in the next step (for whatever reason)
6347 * then fall back to the magic loopback address.
6349 memcpy(&(route_addrs
[numroutes
].addr
.s_addr
), magic_loopback_addr
, 4);
6353 goto cleanup
; /* No routes, fall back to the Magic IP */
6354 /* Find the IP address associated with each found interface */
6355 for (i
= 0; i
< numroutes
; i
++)
6357 for (k
= adapters
; k
!= NULL
; k
= k
->Next
)
6359 char *ip
= k
->IpAddressList
.IpAddress
.String
;
6361 if (route_addrs
[i
].interface
== k
->Index
)
6362 route_addrs
[i
].addr
.s_addr
= (in_addr_t
) inet_addr(ip
);
6365 /* Allocate a hostent and enough memory for all the IPs,
6366 * including the NULL at the end of the list.
6368 hostlist
= WS_create_he(hostname
, 1, 0, numroutes
+1, sizeof(struct in_addr
));
6369 if (hostlist
== NULL
)
6370 goto cleanup
; /* Failed to allocate a hostent for the list of IPs */
6371 hostlist
->h_addr_list
[numroutes
] = NULL
; /* NULL-terminate the address list */
6372 hostlist
->h_aliases
[0] = NULL
; /* NULL-terminate the alias list */
6373 hostlist
->h_addrtype
= AF_INET
;
6374 hostlist
->h_length
= sizeof(struct in_addr
); /* = 4 */
6375 /* Reorder the entries before placing them in the host list. Windows expects
6376 * the IP list in order from highest priority to lowest (the critical thing
6377 * is that most applications expect the first IP to be the default route).
6380 qsort(route_addrs
, numroutes
, sizeof(struct route
), WS_compare_routes_by_metric_asc
);
6382 for (i
= 0; i
< numroutes
; i
++)
6383 (*(struct in_addr
*) hostlist
->h_addr_list
[i
]) = route_addrs
[i
].addr
;
6385 /* Cleanup all allocated memory except the address list,
6386 * the address list is used by the calling app.
6389 HeapFree(GetProcessHeap(), 0, route_addrs
);
6390 HeapFree(GetProcessHeap(), 0, adapters
);
6391 HeapFree(GetProcessHeap(), 0, routes
);
6395 /***********************************************************************
6396 * gethostbyname (WS2_32.52)
6398 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
6400 struct WS_hostent
*retval
= NULL
;
6401 struct hostent
* host
;
6402 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6405 struct hostent hostentry
;
6406 int locerr
= ENOBUFS
;
6410 SetLastError(WSANOTINITIALISED
);
6413 if( gethostname( hostname
, 100) == -1) {
6414 SetLastError(WSAENOBUFS
); /* appropriate ? */
6417 if( !name
|| !name
[0]) {
6420 /* If the hostname of the local machine is requested then return the
6421 * complete list of local IP addresses */
6422 if(strcmp(name
, hostname
) == 0)
6423 retval
= WS_get_local_ips(hostname
);
6424 /* If any other hostname was requested (or the routing table lookup failed)
6425 * then return the IP found by the host OS */
6428 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6430 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
6432 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
6433 if( res
!= ERANGE
) break;
6435 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
6437 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
6439 EnterCriticalSection( &csWSgetXXXbyYYY
);
6440 host
= gethostbyname(name
);
6441 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
6443 if (host
) retval
= WS_dup_he(host
);
6444 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6445 HeapFree(GetProcessHeap(),0,extrabuf
);
6447 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6450 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
6451 strcmp(name
, "localhost") != 0)
6453 /* hostname != "localhost" but has loopback address. replace by our
6454 * special address.*/
6455 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
6457 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
6462 static const struct { int prot
; const char *names
[3]; } protocols
[] =
6464 { 0, { "ip", "IP" }},
6465 { 1, { "icmp", "ICMP" }},
6466 { 2, { "igmp", "IGMP" }},
6467 { 3, { "ggp", "GGP" }},
6468 { 6, { "tcp", "TCP" }},
6469 { 8, { "egp", "EGP" }},
6470 { 9, { "igp", "IGP" }},
6471 { 12, { "pup", "PUP" }},
6472 { 17, { "udp", "UDP" }},
6473 { 20, { "hmp", "HMP" }},
6474 { 22, { "xns-idp", "XNS-IDP" }},
6475 { 27, { "rdp", "RDP" }},
6476 { 29, { "iso-tp4", "ISO-TP4" }},
6477 { 33, { "dccp", "DCCP" }},
6478 { 36, { "xtp", "XTP" }},
6479 { 37, { "ddp", "DDP" }},
6480 { 38, { "idpr-cmtp", "IDPR-CMTP" }},
6481 { 41, { "ipv6", "IPv6" }},
6482 { 43, { "ipv6-route", "IPv6-Route" }},
6483 { 44, { "ipv6-frag", "IPv6-Frag" }},
6484 { 45, { "idrp", "IDRP" }},
6485 { 46, { "rsvp", "RSVP" }},
6486 { 47, { "gre", "GRE" }},
6487 { 50, { "esp", "ESP" }},
6488 { 51, { "ah", "AH" }},
6489 { 57, { "skip", "SKIP" }},
6490 { 58, { "ipv6-icmp", "IPv6-ICMP" }},
6491 { 59, { "ipv6-nonxt", "IPv6-NoNxt" }},
6492 { 60, { "ipv6-opts", "IPv6-Opts" }},
6493 { 66, { "rvd", "RVD" }},
6494 { 73, { "rspf", "RSPF" }},
6495 { 81, { "vmtp", "VMTP" }},
6496 { 88, { "eigrp", "EIGRP" }},
6497 { 89, { "ospf", "OSPFIGP" }},
6498 { 93, { "ax.25", "AX.25" }},
6499 { 94, { "ipip", "IPIP" }},
6500 { 97, { "etherip", "ETHERIP" }},
6501 { 98, { "encap", "ENCAP" }},
6502 { 103, { "pim", "PIM" }},
6503 { 108, { "ipcomp", "IPCOMP" }},
6504 { 112, { "vrrp", "VRRP" }},
6505 { 115, { "l2tp", "L2TP" }},
6506 { 124, { "isis", "ISIS" }},
6507 { 132, { "sctp", "SCTP" }},
6508 { 133, { "fc", "FC" }},
6509 { 135, { "mobility-header", "Mobility-Header" }},
6510 { 136, { "udplite", "UDPLite" }},
6511 { 137, { "mpls-in-ip", "MPLS-in-IP" }},
6512 { 139, { "hip", "HIP" }},
6513 { 140, { "shim6", "Shim6" }},
6514 { 141, { "wesp", "WESP" }},
6515 { 142, { "rohc", "ROHC" }},
6518 /***********************************************************************
6519 * getprotobyname (WS2_32.53)
6521 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
6523 struct WS_protoent
* retval
= NULL
;
6524 #ifdef HAVE_GETPROTOBYNAME
6525 struct protoent
* proto
;
6526 EnterCriticalSection( &csWSgetXXXbyYYY
);
6527 if( (proto
= getprotobyname(name
)) != NULL
)
6528 retval
= WS_create_pe( proto
->p_name
, proto
->p_aliases
, proto
->p_proto
);
6529 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6534 for (i
= 0; i
< ARRAY_SIZE(protocols
); i
++)
6536 if (_strnicmp( protocols
[i
].names
[0], name
, -1 )) continue;
6537 retval
= WS_create_pe( protocols
[i
].names
[0], (char **)protocols
[i
].names
+ 1,
6538 protocols
[i
].prot
);
6544 WARN( "protocol %s not found\n", debugstr_a(name
) );
6545 SetLastError(WSANO_DATA
);
6547 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
6552 /***********************************************************************
6553 * getprotobynumber (WS2_32.54)
6555 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
6557 struct WS_protoent
* retval
= NULL
;
6558 #ifdef HAVE_GETPROTOBYNUMBER
6559 struct protoent
* proto
;
6560 EnterCriticalSection( &csWSgetXXXbyYYY
);
6561 if( (proto
= getprotobynumber(number
)) != NULL
)
6562 retval
= WS_create_pe( proto
->p_name
, proto
->p_aliases
, proto
->p_proto
);
6563 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6568 for (i
= 0; i
< ARRAY_SIZE(protocols
); i
++)
6570 if (protocols
[i
].prot
!= number
) continue;
6571 retval
= WS_create_pe( protocols
[i
].names
[0], (char **)protocols
[i
].names
+ 1,
6572 protocols
[i
].prot
);
6578 WARN( "protocol %d not found\n", number
);
6579 SetLastError(WSANO_DATA
);
6581 TRACE("%i ret %p\n", number
, retval
);
6586 /***********************************************************************
6587 * getservbyname (WS2_32.55)
6589 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
6591 struct WS_servent
* retval
= NULL
;
6592 struct servent
* serv
;
6594 char *proto_str
= NULL
;
6596 if (!(name_str
= strdup_lower(name
))) return NULL
;
6598 if (proto
&& *proto
)
6600 if (!(proto_str
= strdup_lower(proto
)))
6602 HeapFree( GetProcessHeap(), 0, name_str
);
6607 EnterCriticalSection( &csWSgetXXXbyYYY
);
6608 serv
= getservbyname(name_str
, proto_str
);
6611 retval
= WS_dup_se(serv
);
6613 else SetLastError(WSANO_DATA
);
6614 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6615 HeapFree( GetProcessHeap(), 0, proto_str
);
6616 HeapFree( GetProcessHeap(), 0, name_str
);
6617 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
6621 /***********************************************************************
6622 * freeaddrinfo (WS2_32.@)
6624 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
6627 struct WS_addrinfo
*next
;
6629 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
6630 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
6631 next
= res
->ai_next
;
6632 HeapFree(GetProcessHeap(),0,res
);
6637 /* helper functions for getaddrinfo()/getnameinfo() */
6638 static int convert_aiflag_w2u(int winflags
) {
6642 for (i
= 0; i
< ARRAY_SIZE(ws_aiflag_map
); i
++)
6643 if (ws_aiflag_map
[i
][0] & winflags
) {
6644 unixflags
|= ws_aiflag_map
[i
][1];
6645 winflags
&= ~ws_aiflag_map
[i
][0];
6648 FIXME("Unhandled windows AI_xxx flags 0x%x\n", winflags
);
6652 static int convert_niflag_w2u(int winflags
) {
6656 for (i
= 0; i
< ARRAY_SIZE(ws_niflag_map
); i
++)
6657 if (ws_niflag_map
[i
][0] & winflags
) {
6658 unixflags
|= ws_niflag_map
[i
][1];
6659 winflags
&= ~ws_niflag_map
[i
][0];
6662 FIXME("Unhandled windows NI_xxx flags 0x%x\n", winflags
);
6666 static int convert_aiflag_u2w(int unixflags
) {
6670 for (i
= 0; i
< ARRAY_SIZE(ws_aiflag_map
); i
++)
6671 if (ws_aiflag_map
[i
][1] & unixflags
) {
6672 winflags
|= ws_aiflag_map
[i
][0];
6673 unixflags
&= ~ws_aiflag_map
[i
][1];
6676 WARN("Unhandled UNIX AI_xxx flags 0x%x\n", unixflags
);
6680 static int convert_eai_u2w(int unixret
) {
6683 if (!unixret
) return 0;
6685 for (i
=0;ws_eai_map
[i
][0];i
++)
6686 if (ws_eai_map
[i
][1] == unixret
)
6687 return ws_eai_map
[i
][0];
6689 if (unixret
== EAI_SYSTEM
)
6690 /* There are broken versions of glibc which return EAI_SYSTEM
6691 * and set errno to 0 instead of returning EAI_NONAME.
6693 return errno
? sock_get_error( errno
) : WS_EAI_NONAME
;
6695 FIXME("Unhandled unix EAI_xxx ret %d\n", unixret
);
6699 static char *get_fqdn(void)
6704 GetComputerNameExA( ComputerNamePhysicalDnsFullyQualified
, NULL
, &size
);
6705 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
6706 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, size
))) return NULL
;
6707 if (!GetComputerNameExA( ComputerNamePhysicalDnsFullyQualified
, ret
, &size
))
6709 HeapFree( GetProcessHeap(), 0, ret
);
6715 /***********************************************************************
6716 * getaddrinfo (WS2_32.@)
6718 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
6720 #ifdef HAVE_GETADDRINFO
6721 struct addrinfo
*unixaires
= NULL
;
6723 struct addrinfo unixhints
, *punixhints
= NULL
;
6724 char *dot
, *nodeV6
= NULL
, *fqdn
;
6726 size_t hostname_len
= 0;
6729 if (!nodename
&& !servname
)
6731 SetLastError(WSAHOST_NOT_FOUND
);
6732 return WSAHOST_NOT_FOUND
;
6736 if (!fqdn
) return WSA_NOT_ENOUGH_MEMORY
;
6737 dot
= strchr(fqdn
, '.');
6739 hostname_len
= dot
- fqdn
;
6743 else if (!nodename
[0])
6749 /* Check for [ipv6] or [ipv6]:portnumber, which are supported by Windows */
6750 if (!hints
|| hints
->ai_family
== WS_AF_UNSPEC
|| hints
->ai_family
== WS_AF_INET6
)
6752 char *close_bracket
;
6754 if (node
[0] == '[' && (close_bracket
= strchr(node
+ 1, ']')))
6756 nodeV6
= HeapAlloc(GetProcessHeap(), 0, close_bracket
- node
);
6759 HeapFree(GetProcessHeap(), 0, fqdn
);
6760 return WSA_NOT_ENOUGH_MEMORY
;
6762 lstrcpynA(nodeV6
, node
+ 1, close_bracket
- node
);
6768 /* servname tweak required by OSX and BSD kernels */
6769 if (servname
&& !servname
[0]) servname
= "0";
6772 punixhints
= &unixhints
;
6774 memset(&unixhints
, 0, sizeof(unixhints
));
6775 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
6777 /* zero is a wildcard, no need to convert */
6778 if (hints
->ai_family
)
6779 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
6780 if (hints
->ai_socktype
)
6781 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
6782 if (hints
->ai_protocol
)
6783 punixhints
->ai_protocol
= max(convert_proto_w2u(hints
->ai_protocol
), 0);
6785 if (punixhints
->ai_socktype
< 0)
6787 SetLastError(WSAESOCKTNOSUPPORT
);
6788 HeapFree(GetProcessHeap(), 0, fqdn
);
6789 HeapFree(GetProcessHeap(), 0, nodeV6
);
6790 return SOCKET_ERROR
;
6793 /* windows allows invalid combinations of socket type and protocol, unix does not.
6794 * fix the parameters here to make getaddrinfo call always work */
6795 if (punixhints
->ai_protocol
== IPPROTO_TCP
&&
6796 punixhints
->ai_socktype
!= SOCK_STREAM
&& punixhints
->ai_socktype
!= SOCK_SEQPACKET
)
6797 punixhints
->ai_socktype
= 0;
6799 else if (punixhints
->ai_protocol
== IPPROTO_UDP
&& punixhints
->ai_socktype
!= SOCK_DGRAM
)
6800 punixhints
->ai_socktype
= 0;
6802 else if (IS_IPX_PROTO(punixhints
->ai_protocol
) && punixhints
->ai_socktype
!= SOCK_DGRAM
)
6803 punixhints
->ai_socktype
= 0;
6805 else if (punixhints
->ai_protocol
== IPPROTO_IPV6
)
6806 punixhints
->ai_protocol
= 0;
6809 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
6810 result
= getaddrinfo(node
, servname
, punixhints
, &unixaires
);
6812 if (result
&& (!hints
|| !(hints
->ai_flags
& WS_AI_NUMERICHOST
))
6813 && (!strcmp(fqdn
, node
) || (!strncmp(fqdn
, node
, hostname_len
) && !node
[hostname_len
])))
6815 /* If it didn't work it means the host name IP is not in /etc/hosts, try again
6816 * by sending a NULL host and avoid sending a NULL servname too because that
6818 ERR_(winediag
)("Failed to resolve your host name IP\n");
6819 result
= getaddrinfo(NULL
, servname
? servname
: "0", punixhints
, &unixaires
);
6821 TRACE("%s, %s %p -> %p %d\n", debugstr_a(nodename
), debugstr_a(servname
), hints
, res
, result
);
6822 HeapFree(GetProcessHeap(), 0, fqdn
);
6823 HeapFree(GetProcessHeap(), 0, nodeV6
);
6826 struct addrinfo
*xuai
= unixaires
;
6827 struct WS_addrinfo
**xai
= res
;
6831 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
6837 *xai
= ai
;xai
= &ai
->ai_next
;
6838 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
6839 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
6840 /* copy whatever was sent in the hints */
6842 ai
->ai_socktype
= hints
->ai_socktype
;
6843 ai
->ai_protocol
= hints
->ai_protocol
;
6845 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
6846 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
6848 if (xuai
->ai_canonname
) {
6849 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
6850 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
6851 if (!ai
->ai_canonname
)
6853 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
6855 len
= xuai
->ai_addrlen
;
6856 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
6859 ai
->ai_addrlen
= len
;
6861 int winlen
= ai
->ai_addrlen
;
6863 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
6864 ai
->ai_addrlen
= winlen
;
6868 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
6871 ai
->ai_addrlen
= len
;
6873 xuai
= xuai
->ai_next
;
6875 freeaddrinfo(unixaires
);
6877 if (TRACE_ON(winsock
))
6879 struct WS_addrinfo
*ai
= *res
;
6882 TRACE("=> %p, flags %#x, family %d, type %d, protocol %d, len %ld, name %s, addr %s\n",
6883 ai
, ai
->ai_flags
, ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
, ai
->ai_addrlen
,
6884 ai
->ai_canonname
, debugstr_sockaddr(ai
->ai_addr
));
6889 result
= convert_eai_u2w(result
);
6891 SetLastError(result
);
6895 if (*res
) WS_freeaddrinfo(*res
);
6896 if (unixaires
) freeaddrinfo(unixaires
);
6897 return WSA_NOT_ENOUGH_MEMORY
;
6899 FIXME("getaddrinfo() failed, not found during buildtime.\n");
6904 static ADDRINFOEXW
*addrinfo_AtoW(const struct WS_addrinfo
*ai
)
6908 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(ADDRINFOEXW
)))) return NULL
;
6909 ret
->ai_flags
= ai
->ai_flags
;
6910 ret
->ai_family
= ai
->ai_family
;
6911 ret
->ai_socktype
= ai
->ai_socktype
;
6912 ret
->ai_protocol
= ai
->ai_protocol
;
6913 ret
->ai_addrlen
= ai
->ai_addrlen
;
6914 ret
->ai_canonname
= NULL
;
6915 ret
->ai_addr
= NULL
;
6916 ret
->ai_blob
= NULL
;
6917 ret
->ai_bloblen
= 0;
6918 ret
->ai_provider
= NULL
;
6919 ret
->ai_next
= NULL
;
6920 if (ai
->ai_canonname
)
6922 int len
= MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0);
6923 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
))))
6925 HeapFree(GetProcessHeap(), 0, ret
);
6928 MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
);
6932 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, ai
->ai_addrlen
)))
6934 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
6935 HeapFree(GetProcessHeap(), 0, ret
);
6938 memcpy(ret
->ai_addr
, ai
->ai_addr
, ai
->ai_addrlen
);
6943 static ADDRINFOEXW
*addrinfo_list_AtoW(const struct WS_addrinfo
*info
)
6945 ADDRINFOEXW
*ret
, *infoW
;
6947 if (!(ret
= infoW
= addrinfo_AtoW(info
))) return NULL
;
6948 while (info
->ai_next
)
6950 if (!(infoW
->ai_next
= addrinfo_AtoW(info
->ai_next
)))
6952 FreeAddrInfoExW(ret
);
6955 infoW
= infoW
->ai_next
;
6956 info
= info
->ai_next
;
6961 static struct WS_addrinfo
*addrinfo_WtoA(const struct WS_addrinfoW
*ai
)
6963 struct WS_addrinfo
*ret
;
6965 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfo
)))) return NULL
;
6966 ret
->ai_flags
= ai
->ai_flags
;
6967 ret
->ai_family
= ai
->ai_family
;
6968 ret
->ai_socktype
= ai
->ai_socktype
;
6969 ret
->ai_protocol
= ai
->ai_protocol
;
6970 ret
->ai_addrlen
= ai
->ai_addrlen
;
6971 ret
->ai_canonname
= NULL
;
6972 ret
->ai_addr
= NULL
;
6973 ret
->ai_next
= NULL
;
6974 if (ai
->ai_canonname
)
6976 int len
= WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0, NULL
, NULL
);
6977 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
6979 HeapFree(GetProcessHeap(), 0, ret
);
6982 WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
, NULL
, NULL
);
6986 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
6988 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
6989 HeapFree(GetProcessHeap(), 0, ret
);
6992 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
6997 struct getaddrinfo_args
6999 OVERLAPPED
*overlapped
;
7000 LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine
;
7001 ADDRINFOEXW
**result
;
7006 static void WINAPI
getaddrinfo_callback(TP_CALLBACK_INSTANCE
*instance
, void *context
)
7008 struct getaddrinfo_args
*args
= context
;
7009 OVERLAPPED
*overlapped
= args
->overlapped
;
7010 HANDLE event
= overlapped
->hEvent
;
7011 LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine
= args
->completion_routine
;
7012 struct WS_addrinfo
*res
;
7015 ret
= WS_getaddrinfo(args
->nodename
, args
->servname
, NULL
, &res
);
7018 *args
->result
= addrinfo_list_AtoW(res
);
7019 overlapped
->u
.Pointer
= args
->result
;
7020 WS_freeaddrinfo(res
);
7023 HeapFree(GetProcessHeap(), 0, args
->nodename
);
7024 HeapFree(GetProcessHeap(), 0, args
->servname
);
7025 HeapFree(GetProcessHeap(), 0, args
);
7027 overlapped
->Internal
= ret
;
7028 if (completion_routine
) completion_routine(ret
, 0, overlapped
);
7029 if (event
) SetEvent(event
);
7032 static int WS_getaddrinfoW(const WCHAR
*nodename
, const WCHAR
*servname
, const struct WS_addrinfo
*hints
, ADDRINFOEXW
**res
,
7033 OVERLAPPED
*overlapped
, LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine
)
7035 int ret
= EAI_MEMORY
, len
, i
;
7036 char *nodenameA
= NULL
, *servnameA
= NULL
;
7037 struct WS_addrinfo
*resA
;
7038 WCHAR
*local_nodenameW
= (WCHAR
*)nodename
;
7043 /* Is this an IDN? Most likely if any char is above the Ascii table, this
7044 * is the simplest validation possible, further validation will be done by
7045 * the native getaddrinfo() */
7046 for (i
= 0; nodename
[i
]; i
++)
7048 if (nodename
[i
] > 'z')
7053 if (hints
&& (hints
->ai_flags
& WS_AI_DISABLE_IDN_ENCODING
))
7055 /* Name requires conversion but it was disabled */
7056 ret
= WSAHOST_NOT_FOUND
;
7057 WSASetLastError(ret
);
7061 len
= IdnToAscii(0, nodename
, -1, NULL
, 0);
7064 ERR("Failed to convert %s to punycode\n", debugstr_w(nodename
));
7068 if (!(local_nodenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
)))) goto end
;
7069 IdnToAscii(0, nodename
, -1, local_nodenameW
, len
);
7072 if (local_nodenameW
)
7074 len
= WideCharToMultiByte(CP_ACP
, 0, local_nodenameW
, -1, NULL
, 0, NULL
, NULL
);
7075 if (!(nodenameA
= HeapAlloc(GetProcessHeap(), 0, len
))) goto end
;
7076 WideCharToMultiByte(CP_ACP
, 0, local_nodenameW
, -1, nodenameA
, len
, NULL
, NULL
);
7080 len
= WideCharToMultiByte(CP_ACP
, 0, servname
, -1, NULL
, 0, NULL
, NULL
);
7081 if (!(servnameA
= HeapAlloc(GetProcessHeap(), 0, len
))) goto end
;
7082 WideCharToMultiByte(CP_ACP
, 0, servname
, -1, servnameA
, len
, NULL
, NULL
);
7087 struct getaddrinfo_args
*args
;
7089 if (overlapped
->hEvent
&& completion_routine
)
7095 if (!(args
= HeapAlloc(GetProcessHeap(), 0, sizeof(*args
)))) goto end
;
7096 args
->overlapped
= overlapped
;
7097 args
->completion_routine
= completion_routine
;
7099 args
->nodename
= nodenameA
;
7100 args
->servname
= servnameA
;
7102 overlapped
->Internal
= WSAEINPROGRESS
;
7103 if (!TrySubmitThreadpoolCallback(getaddrinfo_callback
, args
, NULL
))
7105 HeapFree(GetProcessHeap(), 0, args
);
7106 ret
= GetLastError();
7110 if (local_nodenameW
!= nodename
)
7111 HeapFree(GetProcessHeap(), 0, local_nodenameW
);
7112 WSASetLastError(ERROR_IO_PENDING
);
7113 return ERROR_IO_PENDING
;
7116 ret
= WS_getaddrinfo(nodenameA
, servnameA
, hints
, &resA
);
7119 *res
= addrinfo_list_AtoW(resA
);
7120 WS_freeaddrinfo(resA
);
7124 if (local_nodenameW
!= nodename
)
7125 HeapFree(GetProcessHeap(), 0, local_nodenameW
);
7126 HeapFree(GetProcessHeap(), 0, nodenameA
);
7127 HeapFree(GetProcessHeap(), 0, servnameA
);
7131 /***********************************************************************
7132 * GetAddrInfoExW (WS2_32.@)
7134 int WINAPI
GetAddrInfoExW(const WCHAR
*name
, const WCHAR
*servname
, DWORD
namespace, GUID
*namespace_id
,
7135 const ADDRINFOEXW
*hints
, ADDRINFOEXW
**result
, struct WS_timeval
*timeout
, OVERLAPPED
*overlapped
,
7136 LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine
, HANDLE
*handle
)
7140 TRACE("(%s %s %x %s %p %p %p %p %p %p)\n", debugstr_w(name
), debugstr_w(servname
), namespace,
7141 debugstr_guid(namespace_id
), hints
, result
, timeout
, overlapped
, completion_routine
, handle
);
7143 if (namespace != NS_DNS
)
7144 FIXME("Unsupported namespace %u\n", namespace);
7146 FIXME("Unsupported naemspace_id %s\n", debugstr_guid(namespace_id
));
7148 FIXME("Unsupported hints\n");
7150 FIXME("Unsupported timeout\n");
7152 FIXME("Unsupported cancel handle\n");
7154 ret
= WS_getaddrinfoW(name
, servname
, NULL
, result
, overlapped
, completion_routine
);
7155 if (ret
) return ret
;
7156 if (handle
) *handle
= (HANDLE
)0xdeadbeef;
7160 /***********************************************************************
7161 * GetAddrInfoExOverlappedResult (WS2_32.@)
7163 int WINAPI
GetAddrInfoExOverlappedResult(OVERLAPPED
*overlapped
)
7165 TRACE("(%p)\n", overlapped
);
7166 return overlapped
->Internal
;
7169 /***********************************************************************
7170 * GetAddrInfoExCancel (WS2_32.@)
7172 int WINAPI
GetAddrInfoExCancel(HANDLE
*handle
)
7174 FIXME("(%p)\n", handle
);
7175 return WSA_INVALID_HANDLE
;
7178 /***********************************************************************
7179 * GetAddrInfoW (WS2_32.@)
7181 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
7183 struct WS_addrinfo
*hintsA
= NULL
;
7185 int ret
= EAI_MEMORY
;
7187 TRACE("nodename %s, servname %s, hints %p, result %p\n",
7188 debugstr_w(nodename
), debugstr_w(servname
), hints
, res
);
7191 if (hints
) hintsA
= addrinfo_WtoA(hints
);
7192 ret
= WS_getaddrinfoW(nodename
, servname
, hintsA
, &resex
, NULL
, NULL
);
7193 WS_freeaddrinfo(hintsA
);
7194 if (ret
) return ret
;
7198 /* ADDRINFOEXW has layout compatible with ADDRINFOW except for ai_next field,
7199 * so we may convert it in place */
7200 *res
= (ADDRINFOW
*)resex
;
7202 ((ADDRINFOW
*)resex
)->ai_next
= (ADDRINFOW
*)resex
->ai_next
;
7203 resex
= resex
->ai_next
;
7209 /***********************************************************************
7210 * FreeAddrInfoW (WS2_32.@)
7212 void WINAPI
FreeAddrInfoW(PADDRINFOW ai
)
7217 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
7218 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
7220 HeapFree(GetProcessHeap(), 0, ai
);
7225 /***********************************************************************
7226 * FreeAddrInfoExW (WS2_32.@)
7228 void WINAPI
FreeAddrInfoExW(ADDRINFOEXW
*ai
)
7230 TRACE("(%p)\n", ai
);
7235 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
7236 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
7238 HeapFree(GetProcessHeap(), 0, ai
);
7243 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
7244 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
7246 #ifdef HAVE_GETNAMEINFO
7248 union generic_unix_sockaddr sa_u
;
7251 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
7252 serv
, servlen
, flags
);
7254 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
7257 SetLastError(WSAEFAULT
);
7258 return WSA_NOT_ENOUGH_MEMORY
;
7260 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
7261 return convert_eai_u2w(ret
);
7263 FIXME("getnameinfo() failed, not found during buildtime.\n");
7268 int WINAPI
GetNameInfoW(const SOCKADDR
*sa
, WS_socklen_t salen
, PWCHAR host
,
7269 DWORD hostlen
, PWCHAR serv
, DWORD servlen
, INT flags
)
7272 char *hostA
= NULL
, *servA
= NULL
;
7274 if (host
&& (!(hostA
= HeapAlloc(GetProcessHeap(), 0, hostlen
)))) return EAI_MEMORY
;
7275 if (serv
&& (!(servA
= HeapAlloc(GetProcessHeap(), 0, servlen
))))
7277 HeapFree(GetProcessHeap(), 0, hostA
);
7281 ret
= WS_getnameinfo(sa
, salen
, hostA
, hostlen
, servA
, servlen
, flags
);
7284 if (host
) MultiByteToWideChar(CP_ACP
, 0, hostA
, -1, host
, hostlen
);
7285 if (serv
) MultiByteToWideChar(CP_ACP
, 0, servA
, -1, serv
, servlen
);
7288 HeapFree(GetProcessHeap(), 0, hostA
);
7289 HeapFree(GetProcessHeap(), 0, servA
);
7293 /***********************************************************************
7294 * getservbyport (WS2_32.56)
7296 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
7298 struct WS_servent
* retval
= NULL
;
7299 #ifdef HAVE_GETSERVBYPORT
7300 struct servent
* serv
;
7301 char *proto_str
= NULL
;
7303 if (proto
&& *proto
)
7305 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
7307 EnterCriticalSection( &csWSgetXXXbyYYY
);
7308 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
7309 retval
= WS_dup_se(serv
);
7311 else SetLastError(WSANO_DATA
);
7312 LeaveCriticalSection( &csWSgetXXXbyYYY
);
7313 HeapFree( GetProcessHeap(), 0, proto_str
);
7315 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
7320 /***********************************************************************
7321 * gethostname (WS2_32.57)
7323 int WINAPI
WS_gethostname(char *name
, int namelen
)
7328 TRACE("name %p, len %d\n", name
, namelen
);
7332 SetLastError(WSAEFAULT
);
7333 return SOCKET_ERROR
;
7336 if (gethostname(buf
, sizeof(buf
)) != 0)
7338 SetLastError(wsaErrno());
7339 return SOCKET_ERROR
;
7342 TRACE("<- '%s'\n", buf
);
7345 WARN("Windows supports NetBIOS name length up to 15 bytes!\n");
7348 SetLastError(WSAEFAULT
);
7349 WARN("<- not enough space for hostname, required %d, got %d!\n", len
+ 1, namelen
);
7350 return SOCKET_ERROR
;
7357 /* ------------------------------------- Windows sockets extensions -- *
7359 * ------------------------------------------------------------------- */
7361 /***********************************************************************
7362 * WSAEnumNetworkEvents (WS2_32.36)
7364 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
7368 int errors
[FD_MAX_EVENTS
];
7370 TRACE("%04lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
7372 SERVER_START_REQ( get_socket_event
)
7374 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
7375 req
->service
= TRUE
;
7376 req
->c_event
= wine_server_obj_handle( hEvent
);
7377 wine_server_set_reply( req
, errors
, sizeof(errors
) );
7378 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
7383 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
7385 if (lpEvent
->lNetworkEvents
& (1 << i
))
7386 lpEvent
->iErrorCode
[i
] = errors
[i
];
7390 SetLastError(WSAEINVAL
);
7391 return SOCKET_ERROR
;
7394 /***********************************************************************
7395 * WSAEventSelect (WS2_32.39)
7397 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
7401 TRACE("%04lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
7403 SERVER_START_REQ( set_socket_event
)
7405 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
7407 req
->event
= wine_server_obj_handle( hEvent
);
7410 ret
= wine_server_call( req
);
7414 SetLastError(WSAEINVAL
);
7415 return SOCKET_ERROR
;
7418 /**********************************************************************
7419 * WSAGetOverlappedResult (WS2_32.40)
7421 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
7422 LPDWORD lpcbTransfer
, BOOL fWait
,
7427 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
7428 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
7430 if ( lpOverlapped
== NULL
)
7432 ERR( "Invalid pointer\n" );
7433 SetLastError(WSA_INVALID_PARAMETER
);
7437 status
= lpOverlapped
->Internal
;
7438 if (status
== STATUS_PENDING
)
7442 SetLastError( WSA_IO_INCOMPLETE
);
7446 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
7447 INFINITE
) == WAIT_FAILED
)
7449 status
= lpOverlapped
->Internal
;
7453 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
7456 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
7458 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
7463 /***********************************************************************
7464 * WSAAsyncSelect (WS2_32.101)
7466 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
7470 TRACE("%04lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
7472 SERVER_START_REQ( set_socket_event
)
7474 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
7477 req
->window
= wine_server_user_handle( hWnd
);
7479 ret
= wine_server_call( req
);
7483 SetLastError(WSAEINVAL
);
7484 return SOCKET_ERROR
;
7487 /***********************************************************************
7488 * WSACreateEvent (WS2_32.31)
7491 WSAEVENT WINAPI
WSACreateEvent(void)
7493 /* Create a manual-reset event, with initial state: unsignaled */
7496 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
7499 /***********************************************************************
7500 * WSACloseEvent (WS2_32.29)
7503 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
7505 TRACE ("event=%p\n", event
);
7507 return CloseHandle(event
);
7510 /***********************************************************************
7511 * WSASocketA (WS2_32.78)
7514 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
7515 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
7516 GROUP g
, DWORD dwFlags
)
7519 WSAPROTOCOL_INFOW info
;
7521 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
7522 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
7524 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
7526 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
7527 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
7528 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
7532 SetLastError(WSAEINVAL
);
7533 return SOCKET_ERROR
;
7536 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
7539 /***********************************************************************
7540 * WSASocketW (WS2_32.79)
7543 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
7544 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
7545 GROUP g
, DWORD dwFlags
)
7549 int unixaf
, unixtype
, ipxptype
= -1;
7552 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
7553 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
7556 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
7557 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
7561 err
= WSANOTINITIALISED
;
7565 /* hack for WSADuplicateSocket */
7566 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
7567 ret
= lpProtocolInfo
->dwServiceFlags3
;
7568 TRACE("\tgot duplicate %04lx\n", ret
);
7574 if (af
== FROM_PROTOCOL_INFO
|| !af
)
7575 af
= lpProtocolInfo
->iAddressFamily
;
7576 if (type
== FROM_PROTOCOL_INFO
|| !type
)
7577 type
= lpProtocolInfo
->iSocketType
;
7578 if (protocol
== FROM_PROTOCOL_INFO
|| !protocol
)
7579 protocol
= lpProtocolInfo
->iProtocol
;
7582 if (!type
&& (af
|| protocol
))
7584 int autoproto
= protocol
;
7585 WSAPROTOCOL_INFOW infow
;
7587 /* default to the first valid protocol */
7589 autoproto
= valid_protocols
[0];
7590 else if(IS_IPX_PROTO(autoproto
))
7591 autoproto
= WS_NSPROTO_IPX
;
7593 if (WS_EnterSingleProtocolW(autoproto
, &infow
))
7595 type
= infow
.iSocketType
;
7597 /* after win2003 it's no longer possible to pass AF_UNSPEC
7598 using the protocol info struct */
7599 if (!lpProtocolInfo
&& af
== WS_AF_UNSPEC
)
7600 af
= infow
.iAddressFamily
;
7605 Windows has an extension to the IPX protocol that allows one to create sockets
7606 and set the IPX packet type at the same time, to do that a caller will use
7607 a protocol like NSPROTO_IPX + <PACKET TYPE>
7609 if (IS_IPX_PROTO(protocol
))
7610 ipxptype
= protocol
- WS_NSPROTO_IPX
;
7612 /* convert the socket family, type and protocol */
7613 unixaf
= convert_af_w2u(af
);
7614 unixtype
= convert_socktype_w2u(type
);
7615 protocol
= convert_proto_w2u(protocol
);
7616 if (unixaf
== AF_UNSPEC
) unixaf
= -1;
7618 /* filter invalid parameters */
7621 /* the type could not be converted */
7622 if (type
&& unixtype
< 0)
7624 err
= WSAESOCKTNOSUPPORT
;
7628 err
= WSAEPROTONOSUPPORT
;
7633 /* both family and protocol can't be invalid */
7640 /* family could not be converted and neither socket type */
7641 if (unixtype
< 0 && af
>= 0)
7644 err
= WSAESOCKTNOSUPPORT
;
7648 err
= WSAEAFNOSUPPORT
;
7652 SERVER_START_REQ( create_socket
)
7654 req
->family
= unixaf
;
7655 req
->type
= unixtype
;
7656 req
->protocol
= protocol
;
7657 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
7658 req
->attributes
= (dwFlags
& WSA_FLAG_NO_HANDLE_INHERIT
) ? 0 : OBJ_INHERIT
;
7659 req
->flags
= dwFlags
& ~WSA_FLAG_NO_HANDLE_INHERIT
;
7660 err
= NtStatusToWSAError( wine_server_call( req
) );
7661 ret
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
7666 TRACE("\tcreated %04lx\n", ret
);
7668 set_ipx_packettype(ret
, ipxptype
);
7670 if (unixaf
== AF_INET
|| unixaf
== AF_INET6
)
7672 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
7673 if (unixtype
== SOCK_DGRAM
|| unixtype
== SOCK_RAW
) /* in Linux the global default can be enabled */
7674 set_dont_fragment(ret
, unixaf
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
7675 else if (unixtype
== SOCK_STREAM
)
7676 set_dont_fragment(ret
, unixaf
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
7680 if (unixaf
== AF_INET6
)
7682 int fd
= get_sock_fd(ret
, 0, NULL
);
7685 /* IPV6_V6ONLY is set by default on Windows */
7687 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
)))
7688 WARN("\tsetting IPV6_V6ONLY failed - errno = %i\n", errno
);
7689 release_sock_fd(ret
, fd
);
7696 if (err
== WSAEACCES
) /* raw socket denied */
7698 if (type
== SOCK_RAW
)
7699 ERR_(winediag
)("Failed to create a socket of type SOCK_RAW, this requires special permissions.\n");
7701 ERR_(winediag
)("Failed to create socket, this requires special permissions.\n");
7705 WARN("\t\tfailed, error %d!\n", err
);
7707 return INVALID_SOCKET
;
7710 /***********************************************************************
7711 * WSAJoinLeaf (WS2_32.58)
7714 SOCKET WINAPI
WSAJoinLeaf(
7716 const struct WS_sockaddr
*addr
,
7718 LPWSABUF lpCallerData
,
7719 LPWSABUF lpCalleeData
,
7725 return INVALID_SOCKET
;
7728 /***********************************************************************
7729 * __WSAFDIsSet (WS2_32.151)
7731 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
7733 int i
= set
->fd_count
, ret
= 0;
7736 if (set
->fd_array
[i
] == s
)
7742 TRACE("(socket %04lx, fd_set %p, count %i) <- %d\n", s
, set
, set
->fd_count
, ret
);
7746 /***********************************************************************
7747 * WSAIsBlocking (WS2_32.114)
7749 BOOL WINAPI
WSAIsBlocking(void)
7751 /* By default WinSock should set all its sockets to non-blocking mode
7752 * and poll in PeekMessage loop when processing "blocking" ones. This
7753 * function is supposed to tell if the program is in this loop. Our
7754 * blocking calls are truly blocking so we always return FALSE.
7756 * Note: It is allowed to call this function without prior WSAStartup().
7763 /***********************************************************************
7764 * WSACancelBlockingCall (WS2_32.113)
7766 INT WINAPI
WSACancelBlockingCall(void)
7772 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
7774 FIXME("How was this called?\n");
7779 /***********************************************************************
7780 * WSASetBlockingHook (WS2_32.109)
7782 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
7784 FARPROC prev
= blocking_hook
;
7785 blocking_hook
= lpBlockFunc
;
7786 TRACE("hook %p\n", lpBlockFunc
);
7791 /***********************************************************************
7792 * WSAUnhookBlockingHook (WS2_32.110)
7794 INT WINAPI
WSAUnhookBlockingHook(void)
7796 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
7801 /* ----------------------------------- end of API stuff */
7803 /* ----------------------------------- helper functions -
7805 * TODO: Merge WS_dup_..() stuff into one function that
7806 * would operate with a generic structure containing internal
7807 * pointers (via a template of some kind).
7810 static int list_size(char** l
, int item_size
)
7815 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
7816 j
+= (i
+ 1) * sizeof(char*); }
7820 static int list_dup(char** l_src
, char** l_to
, int item_size
)
7825 for (i
= 0; l_src
[i
]; i
++) ;
7826 p
= (char *)(l_to
+ i
+ 1);
7827 for (i
= 0; l_src
[i
]; i
++)
7829 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
7830 memcpy(p
, l_src
[i
], count
);
7835 return p
- (char *)l_to
;
7840 /* create a hostent entry
7842 * Creates the entry with enough memory for the name, aliases
7843 * addresses, and the address pointers. Also copies the name
7844 * and sets up all the pointers.
7846 * NOTE: The alias and address lists must be allocated with room
7847 * for the NULL item terminating the list. This is true even if
7848 * the list has no items ("aliases" and "addresses" must be
7849 * at least "1", a truly empty list is invalid).
7851 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int aliases_size
, int addresses
, int address_length
)
7853 struct WS_hostent
*p_to
;
7855 int size
= (sizeof(struct WS_hostent
) +
7857 sizeof(char *) * aliases
+
7859 sizeof(char *) * addresses
+
7860 address_length
* (addresses
- 1)), i
;
7862 if (!(p_to
= check_buffer_he(size
))) return NULL
;
7863 memset(p_to
, 0, size
);
7865 /* Use the memory in the same way winsock does.
7866 * First set the pointer for aliases, second set the pointers for addresses.
7867 * Third fill the addresses indexes, fourth jump aliases names size.
7868 * Fifth fill the hostname.
7869 * NOTE: This method is valid for OS version's >= XP.
7871 p
= (char *)(p_to
+ 1);
7872 p_to
->h_aliases
= (char **)p
;
7873 p
+= sizeof(char *)*aliases
;
7875 p_to
->h_addr_list
= (char **)p
;
7876 p
+= sizeof(char *)*addresses
;
7878 for (i
= 0, addresses
--; i
< addresses
; i
++, p
+= address_length
)
7879 p_to
->h_addr_list
[i
] = p
;
7881 /* NOTE: h_aliases must be filled in manually because we don't know each string
7882 * size, leave these pointers NULL (already set to NULL by memset earlier).
7892 /* duplicate hostent entry
7893 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
7894 * Ditto for protoent and servent.
7896 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
7898 int i
, addresses
= 0, alias_size
= 0;
7899 struct WS_hostent
*p_to
;
7902 for( i
= 0; p_he
->h_aliases
[i
]; i
++) alias_size
+= strlen(p_he
->h_aliases
[i
]) + 1;
7903 while (p_he
->h_addr_list
[addresses
]) addresses
++;
7905 p_to
= WS_create_he(p_he
->h_name
, i
+ 1, alias_size
, addresses
+ 1, p_he
->h_length
);
7907 if (!p_to
) return NULL
;
7908 p_to
->h_addrtype
= convert_af_u2w(p_he
->h_addrtype
);
7909 p_to
->h_length
= p_he
->h_length
;
7911 for(i
= 0, p
= p_to
->h_addr_list
[0]; p_he
->h_addr_list
[i
]; i
++, p
+= p_to
->h_length
)
7912 memcpy(p
, p_he
->h_addr_list
[i
], p_to
->h_length
);
7914 /* Fill the aliases after the IP data */
7915 for(i
= 0; p_he
->h_aliases
[i
]; i
++)
7917 p_to
->h_aliases
[i
] = p
;
7918 strcpy(p
, p_he
->h_aliases
[i
]);
7925 /* ----- protoent */
7927 static struct WS_protoent
*WS_create_pe( const char *name
, char **aliases
, int prot
)
7929 struct WS_protoent
*ret
;
7930 unsigned int size
= sizeof(*ret
) + strlen(name
) + sizeof(char *) + list_size(aliases
, 0);
7932 if (!(ret
= check_buffer_pe( size
))) return NULL
;
7933 ret
->p_proto
= prot
;
7934 ret
->p_name
= (char *)(ret
+ 1);
7935 strcpy( ret
->p_name
, name
);
7936 ret
->p_aliases
= (char **)ret
->p_name
+ strlen(name
) / sizeof(char *) + 1;
7937 list_dup( aliases
, ret
->p_aliases
, 0 );
7943 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
7946 struct WS_servent
*p_to
;
7948 int size
= (sizeof(*p_se
) +
7949 strlen(p_se
->s_proto
) + 1 +
7950 strlen(p_se
->s_name
) + 1 +
7951 list_size(p_se
->s_aliases
, 0));
7953 if (!(p_to
= check_buffer_se(size
))) return NULL
;
7954 p_to
->s_port
= p_se
->s_port
;
7956 p
= (char *)(p_to
+ 1);
7958 strcpy(p
, p_se
->s_name
);
7962 strcpy(p
, p_se
->s_proto
);
7965 p_to
->s_aliases
= (char **)p
;
7966 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
7971 /***********************************************************************
7972 * WSARecv (WS2_32.67)
7974 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
7975 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
7976 LPWSAOVERLAPPED lpOverlapped
,
7977 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
7979 return WS2_recv_base(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
7980 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
, NULL
);
7983 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
7984 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
7985 struct WS_sockaddr
*lpFrom
,
7986 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
7987 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
7988 LPWSABUF lpControlBuffer
)
7990 unsigned int i
, options
;
7991 int n
, fd
, err
, overlapped
, flags
;
7992 struct ws2_async
*wsa
= NULL
, localwsa
;
7994 DWORD timeout_start
= GetTickCount();
7995 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
7997 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
7998 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
7999 (lpFromlen
? *lpFromlen
: -1),
8000 lpOverlapped
, lpCompletionRoutine
);
8002 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
8003 TRACE( "fd=%d, options=%x\n", fd
, options
);
8005 if (fd
== -1) return SOCKET_ERROR
;
8007 if (*lpFlags
& WS_MSG_OOB
)
8009 /* It's invalid to receive OOB data from an OOBINLINED socket
8010 * as OOB data is turned into normal data. */
8011 socklen_t len
= sizeof(n
);
8012 if (!getsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, (char*) &n
, &len
) && n
)
8019 overlapped
= (lpOverlapped
|| lpCompletionRoutine
) &&
8020 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
8021 if (overlapped
|| dwBufferCount
> 1)
8023 if (!(wsa
= (struct ws2_async
*)alloc_async_io( offsetof(struct ws2_async
, iovec
[dwBufferCount
]),
8033 wsa
->hSocket
= SOCKET2HANDLE(s
);
8034 wsa
->flags
= *lpFlags
;
8035 wsa
->lpFlags
= lpFlags
;
8037 wsa
->addrlen
.ptr
= lpFromlen
;
8038 wsa
->control
= lpControlBuffer
;
8039 wsa
->n_iovecs
= dwBufferCount
;
8040 wsa
->first_iovec
= 0;
8041 for (i
= 0; i
< dwBufferCount
; i
++)
8043 /* check buffer first to trigger write watches */
8044 if (IsBadWritePtr( lpBuffers
[i
].buf
, lpBuffers
[i
].len
))
8049 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
8050 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
8053 flags
= convert_flags(wsa
->flags
);
8056 n
= WS2_recv( fd
, wsa
, flags
);
8059 /* Unix-like systems return EINVAL when attempting to read OOB data from
8060 * an empty socket buffer, convert that to a Windows expected return. */
8061 if ((flags
& MSG_OOB
) && errno
== EINVAL
)
8062 errno
= EWOULDBLOCK
;
8064 if (errno
!= EAGAIN
)
8070 else if (lpNumberOfBytesRecvd
) *lpNumberOfBytesRecvd
= n
;
8074 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
8076 wsa
->user_overlapped
= lpOverlapped
;
8077 wsa
->completion_func
= lpCompletionRoutine
;
8078 release_sock_fd( s
, fd
);
8082 iosb
->u
.Status
= STATUS_PENDING
;
8083 iosb
->Information
= 0;
8085 if (wsa
->completion_func
)
8086 err
= register_async( ASYNC_TYPE_READ
, wsa
->hSocket
, &wsa
->io
, NULL
,
8087 ws2_async_apc
, wsa
, iosb
);
8089 err
= register_async( ASYNC_TYPE_READ
, wsa
->hSocket
, &wsa
->io
, lpOverlapped
->hEvent
,
8090 NULL
, (void *)cvalue
, iosb
);
8092 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
8093 SetLastError(NtStatusToWSAError( err
));
8094 return SOCKET_ERROR
;
8097 iosb
->u
.Status
= STATUS_SUCCESS
;
8098 iosb
->Information
= n
;
8099 if (!wsa
->completion_func
)
8101 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
, FALSE
);
8102 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
8103 HeapFree( GetProcessHeap(), 0, wsa
);
8105 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
8106 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
8107 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
8113 if ((err
= sock_is_blocking( s
, &is_blocking
))) goto error
;
8118 int poll_timeout
= -1;
8119 INT64 timeout
= get_rcvsnd_timeo(fd
, TRUE
);
8123 timeout
-= GetTickCount() - timeout_start
;
8124 if (timeout
< 0) poll_timeout
= 0;
8125 else poll_timeout
= timeout
<= INT_MAX
? timeout
: INT_MAX
;
8129 pfd
.events
= POLLIN
;
8130 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
8132 if (!poll_timeout
|| !poll( &pfd
, 1, poll_timeout
))
8135 /* a timeout is not fatal */
8136 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
8142 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
8143 err
= WSAEWOULDBLOCK
;
8148 TRACE(" -> %i bytes\n", n
);
8149 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
8150 release_sock_fd( s
, fd
);
8151 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
8152 SetLastError(ERROR_SUCCESS
);
8157 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
8158 release_sock_fd( s
, fd
);
8159 WARN(" -> ERROR %d\n", err
);
8160 SetLastError( err
);
8161 return SOCKET_ERROR
;
8164 /***********************************************************************
8165 * WSARecvFrom (WS2_32.69)
8167 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
8168 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
8169 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
8170 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
8173 return WS2_recv_base( s
, lpBuffers
, dwBufferCount
,
8174 lpNumberOfBytesRecvd
, lpFlags
,
8176 lpOverlapped
, lpCompletionRoutine
, NULL
);
8179 /***********************************************************************
8180 * WSCInstallProvider (WS2_32.88)
8182 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
8183 LPCWSTR lpszProviderDllPath
,
8184 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
8185 DWORD dwNumberOfEntries
,
8188 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
8189 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
8190 dwNumberOfEntries
, lpErrno
);
8196 /***********************************************************************
8197 * WSCDeinstallProvider (WS2_32.83)
8199 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
8201 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
8207 /***********************************************************************
8208 * WSAAccept (WS2_32.26)
8210 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
8211 LPCONDITIONPROC lpfnCondition
, DWORD_PTR dwCallbackData
)
8215 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
8216 /* QOS SQOS, GQOS; */
8219 SOCKADDR src_addr
, dst_addr
;
8221 TRACE("socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %ld\n",
8222 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
8224 cs
= WS_accept(s
, addr
, addrlen
);
8225 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
8226 if (!lpfnCondition
) return cs
;
8228 if (addr
&& addrlen
)
8230 CallerId
.buf
= (char *)addr
;
8231 CallerId
.len
= *addrlen
;
8235 size
= sizeof(src_addr
);
8236 WS_getpeername(cs
, &src_addr
, &size
);
8237 CallerId
.buf
= (char *)&src_addr
;
8238 CallerId
.len
= size
;
8240 CallerData
.buf
= NULL
;
8243 size
= sizeof(dst_addr
);
8244 WS_getsockname(cs
, &dst_addr
, &size
);
8246 CalleeId
.buf
= (char *)&dst_addr
;
8247 CalleeId
.len
= sizeof(dst_addr
);
8249 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
8250 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
8257 SERVER_START_REQ( set_socket_deferred
)
8259 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
8260 req
->deferred
= wine_server_obj_handle( SOCKET2HANDLE(cs
) );
8261 if ( !wine_server_call_err ( req
) )
8263 SetLastError( WSATRY_AGAIN
);
8264 WS_closesocket( cs
);
8268 return SOCKET_ERROR
;
8271 SetLastError(WSAECONNREFUSED
);
8272 return SOCKET_ERROR
;
8274 FIXME("Unknown return type from Condition function\n");
8275 SetLastError(WSAENOTSOCK
);
8276 return SOCKET_ERROR
;
8280 /***********************************************************************
8281 * WSADuplicateSocketA (WS2_32.32)
8283 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
8285 return WS_DuplicateSocket(FALSE
, s
, dwProcessId
, (LPWSAPROTOCOL_INFOW
) lpProtocolInfo
);
8288 /***********************************************************************
8289 * WSADuplicateSocketW (WS2_32.33)
8291 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
8293 return WS_DuplicateSocket(TRUE
, s
, dwProcessId
, lpProtocolInfo
);
8296 /***********************************************************************
8297 * WSAInstallServiceClassA (WS2_32.48)
8299 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
8301 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
8302 SetLastError(WSAEACCES
);
8303 return SOCKET_ERROR
;
8306 /***********************************************************************
8307 * WSAInstallServiceClassW (WS2_32.49)
8309 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
8311 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
8312 SetLastError(WSAEACCES
);
8313 return SOCKET_ERROR
;
8316 /***********************************************************************
8317 * WSARemoveServiceClass (WS2_32.70)
8319 int WINAPI
WSARemoveServiceClass(LPGUID info
)
8321 FIXME("Request to remove service %s\n", debugstr_guid(info
));
8322 SetLastError(WSATYPE_NOT_FOUND
);
8323 return SOCKET_ERROR
;
8326 /***********************************************************************
8327 * inet_ntop (WS2_32.@)
8329 PCSTR WINAPI
WS_inet_ntop( INT family
, PVOID addr
, PSTR buffer
, SIZE_T len
)
8331 #ifdef HAVE_INET_NTOP
8332 struct WS_in6_addr
*in6
;
8333 struct WS_in_addr
*in
;
8336 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
8339 SetLastError( STATUS_INVALID_PARAMETER
);
8348 pdst
= inet_ntop( AF_INET
, &in
->WS_s_addr
, buffer
, len
);
8354 pdst
= inet_ntop( AF_INET6
, in6
->WS_s6_addr
, buffer
, len
);
8358 SetLastError( WSAEAFNOSUPPORT
);
8362 if (!pdst
) SetLastError( STATUS_INVALID_PARAMETER
);
8365 FIXME( "not supported on this platform\n" );
8366 SetLastError( WSAEAFNOSUPPORT
);
8371 /***********************************************************************
8372 * inet_pton (WS2_32.@)
8374 INT WINAPI
WS_inet_pton( INT family
, PCSTR addr
, PVOID buffer
)
8376 #ifdef HAVE_INET_PTON
8379 TRACE("family %d, addr %s, buffer (%p)\n", family
, debugstr_a(addr
), buffer
);
8381 if (!addr
|| !buffer
)
8383 SetLastError(WSAEFAULT
);
8384 return SOCKET_ERROR
;
8387 unixaf
= convert_af_w2u(family
);
8388 if (unixaf
!= AF_INET
&& unixaf
!= AF_INET6
)
8390 SetLastError(WSAEAFNOSUPPORT
);
8391 return SOCKET_ERROR
;
8394 ret
= inet_pton(unixaf
, addr
, buffer
);
8395 if (ret
== -1) SetLastError(wsaErrno());
8398 FIXME( "not supported on this platform\n" );
8399 SetLastError( WSAEAFNOSUPPORT
);
8400 return SOCKET_ERROR
;
8404 /***********************************************************************
8405 * InetPtonW (WS2_32.@)
8407 INT WINAPI
InetPtonW(INT family
, PCWSTR addr
, PVOID buffer
)
8413 TRACE("family %d, addr %s, buffer (%p)\n", family
, debugstr_w(addr
), buffer
);
8417 SetLastError(WSAEFAULT
);
8418 return SOCKET_ERROR
;
8421 len
= WideCharToMultiByte(CP_ACP
, 0, addr
, -1, NULL
, 0, NULL
, NULL
);
8422 if (!(addrA
= HeapAlloc(GetProcessHeap(), 0, len
)))
8424 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8425 return SOCKET_ERROR
;
8427 WideCharToMultiByte(CP_ACP
, 0, addr
, -1, addrA
, len
, NULL
, NULL
);
8429 ret
= WS_inet_pton(family
, addrA
, buffer
);
8431 HeapFree(GetProcessHeap(), 0, addrA
);
8435 /***********************************************************************
8436 * InetNtopW (WS2_32.@)
8438 PCWSTR WINAPI
InetNtopW(INT family
, PVOID addr
, PWSTR buffer
, SIZE_T len
)
8440 char bufferA
[WS_INET6_ADDRSTRLEN
];
8443 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
8445 if (WS_inet_ntop(family
, addr
, bufferA
, sizeof(bufferA
)))
8447 if (MultiByteToWideChar(CP_ACP
, 0, bufferA
, -1, buffer
, len
))
8450 SetLastError(ERROR_INVALID_PARAMETER
);
8455 /***********************************************************************
8456 * WSAStringToAddressA (WS2_32.80)
8458 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
8460 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
8461 LPSOCKADDR lpAddress
,
8462 LPINT lpAddressLength
)
8465 LPSTR workBuffer
=NULL
,ptrPort
;
8467 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_a(AddressString
), AddressFamily
,
8468 lpProtocolInfo
, lpAddress
, lpAddressLength
);
8470 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
8474 SetLastError(WSAEINVAL
);
8475 return SOCKET_ERROR
;
8479 FIXME("ProtocolInfo not implemented.\n");
8481 workBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
8482 strlen(AddressString
) + 1);
8485 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8486 return SOCKET_ERROR
;
8489 strcpy(workBuffer
, AddressString
);
8491 switch(AddressFamily
)
8495 struct in_addr inetaddr
;
8497 /* If lpAddressLength is too small, tell caller the size we need */
8498 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
8500 *lpAddressLength
= sizeof(SOCKADDR_IN
);
8504 *lpAddressLength
= sizeof(SOCKADDR_IN
);
8505 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
8507 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= WS_AF_INET
;
8509 ptrPort
= strchr(workBuffer
, ':');
8512 /* User may have entered an IPv6 and asked to parse as IPv4 */
8513 if(strchr(ptrPort
+ 1, ':'))
8518 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= htons(atoi(ptrPort
+1));
8522 if(inet_aton(workBuffer
, &inetaddr
) > 0)
8524 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
8534 struct in6_addr inetaddr
;
8535 char *ptrAddr
= workBuffer
;
8537 /* If lpAddressLength is too small, tell caller the size we need */
8538 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
8540 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
8544 #ifdef HAVE_INET_PTON
8545 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
8546 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
8548 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_family
= WS_AF_INET6
;
8550 /* Valid IPv6 addresses can also be surrounded by [ ], and in this case
8551 * a port number may follow after like in [fd12:3456:7890::1]:12345
8552 * We need to cut the brackets and find the port if any. */
8554 if(*workBuffer
== '[')
8556 ptrPort
= strchr(workBuffer
, ']');
8559 SetLastError(WSAEINVAL
);
8560 return SOCKET_ERROR
;
8563 if (ptrPort
[1] == ':')
8564 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= htons(atoi(ptrPort
+ 2));
8567 ptrAddr
= workBuffer
+ 1;
8570 if(inet_pton(AF_INET6
, ptrAddr
, &inetaddr
) > 0)
8572 memcpy(&((LPSOCKADDR_IN6
)lpAddress
)->sin6_addr
, &inetaddr
,
8573 sizeof(struct in6_addr
));
8577 #endif /* HAVE_INET_PTON */
8583 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
8584 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
8588 HeapFree(GetProcessHeap(), 0, workBuffer
);
8592 return SOCKET_ERROR
;
8595 /***********************************************************************
8596 * WSAStringToAddressW (WS2_32.81)
8598 * FIXME: Does anybody know if this function allows using Hebrew/Arabic/Chinese... digits?
8599 * If this should be the case, it would be required to map these digits
8600 * to Unicode digits (0-9) using FoldString first.
8602 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
8604 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
8605 LPSOCKADDR lpAddress
,
8606 LPINT lpAddressLength
)
8609 LPSTR workBuffer
=NULL
;
8610 WSAPROTOCOL_INFOA infoA
;
8611 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
8613 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
8614 lpAddress
, lpAddressLength
);
8616 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
8618 /* if ProtocolInfo is available - convert to ANSI variant */
8621 lpProtoInfoA
= &infoA
;
8622 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
8624 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
8625 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
8627 SetLastError(WSAEINVAL
);
8628 return SOCKET_ERROR
;
8634 /* Translate AddressString to ANSI code page - assumes that only
8635 standard digits 0-9 are used with this API call */
8636 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
8637 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
8641 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
8642 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
8643 lpAddress
,lpAddressLength
);
8644 HeapFree( GetProcessHeap(), 0, workBuffer
);
8648 res
= WSA_NOT_ENOUGH_MEMORY
;
8654 return SOCKET_ERROR
;
8657 /***********************************************************************
8658 * WSAAddressToStringA (WS2_32.27)
8660 * See WSAAddressToStringW
8662 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
8663 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
8667 CHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
8670 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
8672 if (!sockaddr
) return SOCKET_ERROR
;
8673 if (!string
|| !lenstr
) return SOCKET_ERROR
;
8675 switch(sockaddr
->sa_family
)
8679 unsigned int long_ip
= ntohl(((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
);
8680 if (len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
8681 sprintf( buffer
, "%u.%u.%u.%u:%u",
8682 (long_ip
>> 24) & 0xff,
8683 (long_ip
>> 16) & 0xff,
8684 (long_ip
>> 8) & 0xff,
8686 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
8688 p
= strchr( buffer
, ':' );
8689 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
8694 struct WS_sockaddr_in6
*sockaddr6
= (LPSOCKADDR_IN6
) sockaddr
;
8698 if (len
< sizeof(SOCKADDR_IN6
)) return SOCKET_ERROR
;
8699 if ((sockaddr6
->sin6_port
))
8700 strcpy(buffer
, "[");
8701 slen
= strlen(buffer
);
8702 if (!WS_inet_ntop(WS_AF_INET6
, &sockaddr6
->sin6_addr
, &buffer
[slen
], sizeof(buffer
) - slen
))
8704 SetLastError(WSAEINVAL
);
8705 return SOCKET_ERROR
;
8707 if ((sockaddr6
->sin6_scope_id
))
8708 sprintf(buffer
+strlen(buffer
), "%%%u", sockaddr6
->sin6_scope_id
);
8709 if ((sockaddr6
->sin6_port
))
8710 sprintf(buffer
+strlen(buffer
), "]:%u", ntohs(sockaddr6
->sin6_port
));
8715 SetLastError(WSAEINVAL
);
8716 return SOCKET_ERROR
;
8719 size
= strlen( buffer
) + 1;
8724 SetLastError(WSAEFAULT
);
8725 return SOCKET_ERROR
;
8728 TRACE("=> %s,%u bytes\n", debugstr_a(buffer
), size
);
8730 strcpy( string
, buffer
);
8734 /***********************************************************************
8735 * WSAAddressToStringW (WS2_32.28)
8737 * Convert a sockaddr address into a readable address string.
8740 * sockaddr [I] Pointer to a sockaddr structure.
8741 * len [I] Size of the sockaddr structure.
8742 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
8743 * string [I/O] Pointer to a buffer to receive the address string.
8744 * lenstr [I/O] Size of the receive buffer in WCHARs.
8748 * Failure: SOCKET_ERROR
8751 * The 'info' parameter is ignored.
8753 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
8754 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
8759 WCHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
8762 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
8765 ret
= WSAAddressToStringA(sockaddr
, len
, NULL
, bufAddr
, &size
);
8767 if (ret
) return ret
;
8769 MultiByteToWideChar(CP_ACP
, 0, bufAddr
, size
, buffer
, ARRAY_SIZE(buffer
));
8774 SetLastError(WSAEFAULT
);
8775 return SOCKET_ERROR
;
8778 TRACE("=> %s,%u bytes\n", debugstr_w(buffer
), size
);
8780 lstrcpyW( string
, buffer
);
8784 /***********************************************************************
8785 * WSAEnumNameSpaceProvidersA (WS2_32.34)
8787 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
8789 FIXME( "(%p %p) Stub!\n", len
, buffer
);
8793 /***********************************************************************
8794 * WSAEnumNameSpaceProvidersW (WS2_32.35)
8796 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
8798 FIXME( "(%p %p) Stub!\n", len
, buffer
);
8802 /***********************************************************************
8803 * WSAGetQOSByName (WS2_32.41)
8805 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
8807 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
8811 /***********************************************************************
8812 * WSAGetServiceClassInfoA (WS2_32.42)
8814 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
8815 LPWSASERVICECLASSINFOA info
)
8817 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
8819 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8820 return SOCKET_ERROR
;
8823 /***********************************************************************
8824 * WSAGetServiceClassInfoW (WS2_32.43)
8826 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
8827 LPWSASERVICECLASSINFOW info
)
8829 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
8831 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8832 return SOCKET_ERROR
;
8835 /***********************************************************************
8836 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
8838 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
8840 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
8841 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8842 return SOCKET_ERROR
;
8845 /***********************************************************************
8846 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
8848 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
8850 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
8851 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8852 return SOCKET_ERROR
;
8855 /***********************************************************************
8856 * WSALookupServiceBeginA (WS2_32.59)
8858 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
8859 DWORD dwControlFlags
,
8862 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
8864 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8865 return SOCKET_ERROR
;
8868 /***********************************************************************
8869 * WSALookupServiceBeginW (WS2_32.60)
8871 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
8872 DWORD dwControlFlags
,
8875 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
8877 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8878 return SOCKET_ERROR
;
8881 /***********************************************************************
8882 * WSALookupServiceEnd (WS2_32.61)
8884 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
8886 FIXME("(%p) Stub!\n", lookup
);
8890 /***********************************************************************
8891 * WSALookupServiceNextA (WS2_32.62)
8893 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
8895 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
8896 SetLastError(WSA_E_NO_MORE
);
8897 return SOCKET_ERROR
;
8900 /***********************************************************************
8901 * WSALookupServiceNextW (WS2_32.63)
8903 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
8905 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
8906 SetLastError(WSA_E_NO_MORE
);
8907 return SOCKET_ERROR
;
8910 /***********************************************************************
8911 * WSANtohl (WS2_32.64)
8913 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
8915 TRACE( "(%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
8917 if (!lphostlong
) return WSAEFAULT
;
8919 *lphostlong
= ntohl( netlong
);
8923 /***********************************************************************
8924 * WSANtohs (WS2_32.65)
8926 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
8928 TRACE( "(%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
8930 if (!lphostshort
) return WSAEFAULT
;
8932 *lphostshort
= ntohs( netshort
);
8936 /***********************************************************************
8937 * WSAProviderConfigChange (WS2_32.66)
8939 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
8940 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
8942 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
8943 return SOCKET_ERROR
;
8946 /***********************************************************************
8947 * WSARecvDisconnect (WS2_32.68)
8949 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
8951 TRACE( "(%04lx %p)\n", s
, disconnectdata
);
8953 return WS_shutdown( s
, SD_RECEIVE
);
8956 /***********************************************************************
8957 * WSASetServiceA (WS2_32.76)
8959 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
8961 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
8965 /***********************************************************************
8966 * WSASetServiceW (WS2_32.77)
8968 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
8970 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
8974 /***********************************************************************
8975 * WSCEnableNSProvider (WS2_32.84)
8977 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
8979 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
8983 /***********************************************************************
8984 * WSCGetProviderPath (WS2_32.86)
8986 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
8988 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
8990 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
8992 *errcode
= WSAEINVAL
;
8993 return SOCKET_ERROR
;
8996 /***********************************************************************
8997 * WSCInstallNameSpace (WS2_32.87)
8999 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
9000 DWORD version
, LPGUID provider
)
9002 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
9003 namespace, version
, debugstr_guid(provider
) );
9007 /***********************************************************************
9008 * WSCUnInstallNameSpace (WS2_32.89)
9010 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
9012 FIXME("(%s) Stub!\n", debugstr_guid(lpProviderId
));
9016 /***********************************************************************
9017 * WSCWriteProviderOrder (WS2_32.91)
9019 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
9021 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);
9025 /***********************************************************************
9026 * WSANSPIoctl (WS2_32.91)
9028 INT WINAPI
WSANSPIoctl( HANDLE hLookup
, DWORD dwControlCode
, LPVOID lpvInBuffer
,
9029 DWORD cbInBuffer
, LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
9030 LPDWORD lpcbBytesReturned
, LPWSACOMPLETION lpCompletion
)
9032 FIXME("(%p, 0x%08x, %p, 0x%08x, %p, 0x%08x, %p, %p) Stub!\n", hLookup
, dwControlCode
,
9033 lpvInBuffer
, cbInBuffer
, lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpCompletion
);
9034 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
9035 return SOCKET_ERROR
;
9038 /*****************************************************************************
9039 * WSAEnumProtocolsA [WS2_32.@]
9041 * see function WSAEnumProtocolsW
9043 INT WINAPI
WSAEnumProtocolsA( LPINT protocols
, LPWSAPROTOCOL_INFOA buffer
, LPDWORD len
)
9045 return WS_EnumProtocols( FALSE
, protocols
, (LPWSAPROTOCOL_INFOW
) buffer
, len
);
9048 /*****************************************************************************
9049 * WSAEnumProtocolsW [WS2_32.@]
9051 * Retrieves information about specified set of active network protocols.
9054 * protocols [I] Pointer to null-terminated array of protocol id's. NULL
9055 * retrieves information on all available protocols.
9056 * buffer [I] Pointer to a buffer to be filled with WSAPROTOCOL_INFO
9058 * len [I/O] Pointer to a variable specifying buffer size. On output
9059 * the variable holds the number of bytes needed when the
9060 * specified size is too small.
9063 * Success: number of WSAPROTOCOL_INFO structures in buffer.
9064 * Failure: SOCKET_ERROR
9067 * NT4SP5 does not return SPX if protocols == NULL
9070 * - NT4SP5 returns in addition these list of NETBIOS protocols
9071 * (address family 17), each entry two times one for socket type 2 and 5
9073 * iProtocol szProtocol
9074 * 0x80000000 \Device\NwlnkNb
9075 * 0xfffffffa \Device\NetBT_CBENT7
9076 * 0xfffffffb \Device\Nbf_CBENT7
9077 * 0xfffffffc \Device\NetBT_NdisWan5
9078 * 0xfffffffd \Device\NetBT_El9202
9079 * 0xfffffffe \Device\Nbf_El9202
9080 * 0xffffffff \Device\Nbf_NdisWan4
9082 * - there is no check that the operating system supports the returned
9085 INT WINAPI
WSAEnumProtocolsW( LPINT protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
)
9087 return WS_EnumProtocols( TRUE
, protocols
, buffer
, len
);
9090 /*****************************************************************************
9091 * WSCEnumProtocols [WS2_32.@]
9094 * protocols [I] Null-terminated array of iProtocol values.
9095 * buffer [O] Buffer of WSAPROTOCOL_INFOW structures.
9096 * len [I/O] Size of buffer on input/output.
9097 * errno [O] Error code.
9100 * Success: number of protocols to be reported on.
9101 * Failure: SOCKET_ERROR. error is in errno.
9104 * Doesn't supply info on layered protocols.
9107 INT WINAPI
WSCEnumProtocols( LPINT protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
, LPINT err
)
9109 INT ret
= WSAEnumProtocolsW( protocols
, buffer
, len
);
9111 if (ret
== SOCKET_ERROR
) *err
= WSAENOBUFS
;