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_ADD_SOURCE_MEMBERSHIP
),
696 MAP_OPTION( IP_DROP_SOURCE_MEMBERSHIP
),
697 MAP_OPTION( IP_BLOCK_SOURCE
),
698 MAP_OPTION( IP_UNBLOCK_SOURCE
),
699 MAP_OPTION( IP_OPTIONS
),
701 MAP_OPTION( IP_HDRINCL
),
703 MAP_OPTION( IP_TOS
),
704 MAP_OPTION( IP_TTL
),
705 #if defined(IP_PKTINFO)
706 MAP_OPTION( IP_PKTINFO
),
707 #elif defined(IP_RECVDSTADDR)
708 { WS_IP_PKTINFO
, IP_RECVDSTADDR
},
711 MAP_OPTION( IP_UNICAST_IF
),
715 static const int ws_ipv6_map
[][2] =
717 #ifdef IPV6_ADD_MEMBERSHIP
718 MAP_OPTION( IPV6_ADD_MEMBERSHIP
),
720 #ifdef IPV6_DROP_MEMBERSHIP
721 MAP_OPTION( IPV6_DROP_MEMBERSHIP
),
723 MAP_OPTION( IPV6_MULTICAST_IF
),
724 MAP_OPTION( IPV6_MULTICAST_HOPS
),
725 MAP_OPTION( IPV6_MULTICAST_LOOP
),
726 MAP_OPTION( IPV6_UNICAST_HOPS
),
727 MAP_OPTION( IPV6_V6ONLY
),
728 #ifdef IPV6_UNICAST_IF
729 MAP_OPTION( IPV6_UNICAST_IF
),
733 static const int ws_af_map
[][2] =
735 MAP_OPTION( AF_UNSPEC
),
736 MAP_OPTION( AF_INET
),
737 MAP_OPTION( AF_INET6
),
739 MAP_OPTION( AF_IPX
),
742 MAP_OPTION( AF_IRDA
),
744 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
747 static const int ws_socktype_map
[][2] =
749 MAP_OPTION( SOCK_DGRAM
),
750 MAP_OPTION( SOCK_STREAM
),
751 MAP_OPTION( SOCK_RAW
),
752 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
755 static const int ws_proto_map
[][2] =
757 MAP_OPTION( IPPROTO_IP
),
758 MAP_OPTION( IPPROTO_TCP
),
759 MAP_OPTION( IPPROTO_UDP
),
760 MAP_OPTION( IPPROTO_IPV6
),
761 MAP_OPTION( IPPROTO_ICMP
),
762 MAP_OPTION( IPPROTO_IGMP
),
763 MAP_OPTION( IPPROTO_RAW
),
764 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
767 static const int ws_aiflag_map
[][2] =
769 MAP_OPTION( AI_PASSIVE
),
770 MAP_OPTION( AI_CANONNAME
),
771 MAP_OPTION( AI_NUMERICHOST
),
772 #ifdef AI_NUMERICSERV
773 MAP_OPTION( AI_NUMERICSERV
),
776 MAP_OPTION( AI_V4MAPPED
),
778 MAP_OPTION( AI_ADDRCONFIG
),
781 static const int ws_niflag_map
[][2] =
783 MAP_OPTION( NI_NOFQDN
),
784 MAP_OPTION( NI_NUMERICHOST
),
785 MAP_OPTION( NI_NAMEREQD
),
786 MAP_OPTION( NI_NUMERICSERV
),
787 MAP_OPTION( NI_DGRAM
),
790 static const int ws_eai_map
[][2] =
792 MAP_OPTION( EAI_AGAIN
),
793 MAP_OPTION( EAI_BADFLAGS
),
794 MAP_OPTION( EAI_FAIL
),
795 MAP_OPTION( EAI_FAMILY
),
796 MAP_OPTION( EAI_MEMORY
),
797 /* Note: EAI_NODATA is deprecated, but still
798 * used by Windows and Linux... We map the newer
799 * EAI_NONAME to EAI_NODATA for now until Windows
803 MAP_OPTION( EAI_NODATA
),
806 { WS_EAI_NODATA
, EAI_NONAME
},
809 MAP_OPTION( EAI_SERVICE
),
810 MAP_OPTION( EAI_SOCKTYPE
),
814 static const int ws_poll_map
[][2] =
816 MAP_OPTION( POLLERR
),
817 MAP_OPTION( POLLHUP
),
818 MAP_OPTION( POLLNVAL
),
819 MAP_OPTION( POLLWRNORM
),
820 MAP_OPTION( POLLWRBAND
),
821 MAP_OPTION( POLLRDNORM
),
822 { WS_POLLRDBAND
, POLLPRI
}
825 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
827 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
828 #if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
829 static inline WSACMSGHDR
*fill_control_message(int level
, int type
, WSACMSGHDR
*current
, ULONG
*maxsize
, void *data
, int len
)
831 ULONG msgsize
= sizeof(WSACMSGHDR
) + WSA_CMSG_ALIGN(len
);
832 char *ptr
= (char *) current
+ sizeof(WSACMSGHDR
);
834 /* Make sure there is at least enough room for this entry */
835 if (msgsize
> *maxsize
)
838 /* Fill in the entry */
839 current
->cmsg_len
= sizeof(WSACMSGHDR
) + len
;
840 current
->cmsg_level
= level
;
841 current
->cmsg_type
= type
;
842 memcpy(ptr
, data
, len
);
843 /* Return the pointer to where next entry should go */
844 return (WSACMSGHDR
*) (ptr
+ WSA_CMSG_ALIGN(len
));
846 #endif /* defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) */
848 static inline int convert_control_headers(struct msghdr
*hdr
, WSABUF
*control
)
850 #if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
851 WSACMSGHDR
*cmsg_win
= (WSACMSGHDR
*) control
->buf
, *ptr
;
852 ULONG ctlsize
= control
->len
;
853 struct cmsghdr
*cmsg_unix
;
856 /* Loop over all the headers, converting as appropriate */
857 for (cmsg_unix
= CMSG_FIRSTHDR(hdr
); cmsg_unix
!= NULL
; cmsg_unix
= CMSG_NXTHDR(hdr
, cmsg_unix
))
859 switch(cmsg_unix
->cmsg_level
)
862 switch(cmsg_unix
->cmsg_type
)
864 #if defined(IP_PKTINFO)
867 /* Convert the Unix IP_PKTINFO structure to the Windows version */
868 struct in_pktinfo
*data_unix
= (struct in_pktinfo
*) CMSG_DATA(cmsg_unix
);
869 struct WS_in_pktinfo data_win
;
871 memcpy(&data_win
.ipi_addr
,&data_unix
->ipi_addr
.s_addr
,4); /* 4 bytes = 32 address bits */
872 data_win
.ipi_ifindex
= data_unix
->ipi_ifindex
;
873 ptr
= fill_control_message(WS_IPPROTO_IP
, WS_IP_PKTINFO
, ptr
, &ctlsize
,
874 (void*)&data_win
, sizeof(data_win
));
875 if (!ptr
) goto error
;
877 #elif defined(IP_RECVDSTADDR)
880 struct in_addr
*addr_unix
= (struct in_addr
*) CMSG_DATA(cmsg_unix
);
881 struct WS_in_pktinfo data_win
;
883 memcpy(&data_win
.ipi_addr
, &addr_unix
->s_addr
, 4); /* 4 bytes = 32 address bits */
884 data_win
.ipi_ifindex
= 0; /* FIXME */
885 ptr
= fill_control_message(WS_IPPROTO_IP
, WS_IP_PKTINFO
, ptr
, &ctlsize
,
886 (void*)&data_win
, sizeof(data_win
));
887 if (!ptr
) goto error
;
889 #endif /* IP_PKTINFO */
891 FIXME("Unhandled IPPROTO_IP message header type %d\n", cmsg_unix
->cmsg_type
);
896 FIXME("Unhandled message header level %d\n", cmsg_unix
->cmsg_level
);
901 /* Set the length of the returned control headers */
902 control
->len
= (char*)ptr
- (char*)cmsg_win
;
907 #else /* defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) */
910 #endif /* defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) */
912 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
914 /* ----------------------------------- error handling */
916 static NTSTATUS
sock_get_ntstatus( int err
)
920 case EBADF
: return STATUS_INVALID_HANDLE
;
921 case EBUSY
: return STATUS_DEVICE_BUSY
;
923 case EACCES
: return STATUS_ACCESS_DENIED
;
924 case EFAULT
: return STATUS_NO_MEMORY
;
925 case EINVAL
: return STATUS_INVALID_PARAMETER
;
927 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
928 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
929 case EINPROGRESS
: return STATUS_PENDING
;
930 case EALREADY
: return STATUS_NETWORK_BUSY
;
931 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
932 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
933 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
934 case EPROTONOSUPPORT
:
935 case ESOCKTNOSUPPORT
:
938 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
939 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
940 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
941 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
942 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
943 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
944 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
945 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
946 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
947 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
948 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
950 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
951 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
953 case 0: return STATUS_SUCCESS
;
955 WARN("Unknown errno %d!\n", err
);
956 return STATUS_UNSUCCESSFUL
;
960 static UINT
sock_get_error( int err
)
964 case EINTR
: return WSAEINTR
;
966 case EACCES
: return WSAEACCES
;
967 case EFAULT
: return WSAEFAULT
;
968 case EINVAL
: return WSAEINVAL
;
969 case EMFILE
: return WSAEMFILE
;
970 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
971 case EINPROGRESS
: return WSAEINPROGRESS
;
972 case EALREADY
: return WSAEALREADY
;
974 case ENOTSOCK
: return WSAENOTSOCK
;
975 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
976 case EMSGSIZE
: return WSAEMSGSIZE
;
977 case EPROTOTYPE
: return WSAEPROTOTYPE
;
978 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
979 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
980 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
981 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
982 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
983 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
984 case EADDRINUSE
: return WSAEADDRINUSE
;
985 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
986 case ENETDOWN
: return WSAENETDOWN
;
987 case ENETUNREACH
: return WSAENETUNREACH
;
988 case ENETRESET
: return WSAENETRESET
;
989 case ECONNABORTED
: return WSAECONNABORTED
;
991 case ECONNRESET
: return WSAECONNRESET
;
992 case ENOBUFS
: return WSAENOBUFS
;
993 case EISCONN
: return WSAEISCONN
;
994 case ENOTCONN
: return WSAENOTCONN
;
995 case ESHUTDOWN
: return WSAESHUTDOWN
;
996 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
997 case ETIMEDOUT
: return WSAETIMEDOUT
;
998 case ECONNREFUSED
: return WSAECONNREFUSED
;
999 case ELOOP
: return WSAELOOP
;
1000 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
1001 case EHOSTDOWN
: return WSAEHOSTDOWN
;
1002 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
1003 case ENOTEMPTY
: return WSAENOTEMPTY
;
1005 case EPROCLIM
: return WSAEPROCLIM
;
1008 case EUSERS
: return WSAEUSERS
;
1011 case EDQUOT
: return WSAEDQUOT
;
1014 case ESTALE
: return WSAESTALE
;
1017 case EREMOTE
: return WSAEREMOTE
;
1020 /* just in case we ever get here and there are no problems */
1023 WARN("Unknown errno %d!\n", err
);
1024 return WSAEOPNOTSUPP
;
1028 static UINT
wsaErrno(void)
1030 int loc_errno
= errno
;
1031 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
1033 return sock_get_error( loc_errno
);
1036 /* most ws2 overlapped functions return an ntstatus-based error code */
1037 static NTSTATUS
wsaErrStatus(void)
1039 int loc_errno
= errno
;
1040 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
1042 return sock_get_ntstatus(loc_errno
);
1045 static UINT
wsaHerrno(int loc_errno
)
1047 WARN("h_errno %d.\n", loc_errno
);
1051 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
1052 case TRY_AGAIN
: return WSATRY_AGAIN
;
1053 case NO_RECOVERY
: return WSANO_RECOVERY
;
1054 case NO_DATA
: return WSANO_DATA
;
1055 case ENOBUFS
: return WSAENOBUFS
;
1059 WARN("Unknown h_errno %d!\n", loc_errno
);
1060 return WSAEOPNOTSUPP
;
1064 static NTSTATUS
sock_error_to_ntstatus( DWORD err
)
1068 case 0: return STATUS_SUCCESS
;
1069 case WSAEBADF
: return STATUS_INVALID_HANDLE
;
1070 case WSAEACCES
: return STATUS_ACCESS_DENIED
;
1071 case WSAEFAULT
: return STATUS_NO_MEMORY
;
1072 case WSAEINVAL
: return STATUS_INVALID_PARAMETER
;
1073 case WSAEMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
1074 case WSAEWOULDBLOCK
: return STATUS_CANT_WAIT
;
1075 case WSAEINPROGRESS
: return STATUS_PENDING
;
1076 case WSAEALREADY
: return STATUS_NETWORK_BUSY
;
1077 case WSAENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
1078 case WSAEDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
1079 case WSAEMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
1080 case WSAEPROTONOSUPPORT
:
1081 case WSAESOCKTNOSUPPORT
:
1082 case WSAEPFNOSUPPORT
:
1083 case WSAEAFNOSUPPORT
:
1084 case WSAEPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
1085 case WSAENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
1086 case WSAEOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
1087 case WSAEADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
1088 case WSAEADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
1089 case WSAECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
1090 case WSAESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
1091 case WSAENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
1092 case WSAETIMEDOUT
: return STATUS_IO_TIMEOUT
;
1093 case WSAENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
1094 case WSAENETDOWN
: return STATUS_NETWORK_BUSY
;
1095 case WSAECONNRESET
: return STATUS_CONNECTION_RESET
;
1096 case WSAECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
1098 FIXME("unmapped error %u\n", err
);
1099 return STATUS_UNSUCCESSFUL
;
1103 static DWORD
NtStatusToWSAError( DWORD status
)
1107 case STATUS_SUCCESS
: return 0;
1108 case STATUS_PENDING
: return WSA_IO_PENDING
;
1109 case STATUS_OBJECT_TYPE_MISMATCH
: return WSAENOTSOCK
;
1110 case STATUS_INVALID_HANDLE
: return WSAEBADF
;
1111 case STATUS_INVALID_PARAMETER
: return WSAEINVAL
;
1112 case STATUS_PIPE_DISCONNECTED
: return WSAESHUTDOWN
;
1113 case STATUS_NETWORK_BUSY
: return WSAEALREADY
;
1114 case STATUS_NETWORK_UNREACHABLE
: return WSAENETUNREACH
;
1115 case STATUS_CONNECTION_REFUSED
: return WSAECONNREFUSED
;
1116 case STATUS_CONNECTION_DISCONNECTED
: return WSAENOTCONN
;
1117 case STATUS_CONNECTION_RESET
: return WSAECONNRESET
;
1118 case STATUS_CONNECTION_ABORTED
: return WSAECONNABORTED
;
1119 case STATUS_CANCELLED
: return WSA_OPERATION_ABORTED
;
1120 case STATUS_ADDRESS_ALREADY_ASSOCIATED
: return WSAEADDRINUSE
;
1121 case STATUS_IO_TIMEOUT
:
1122 case STATUS_TIMEOUT
: return WSAETIMEDOUT
;
1123 case STATUS_NO_MEMORY
: return WSAEFAULT
;
1124 case STATUS_ACCESS_DENIED
: return WSAEACCES
;
1125 case STATUS_TOO_MANY_OPENED_FILES
: return WSAEMFILE
;
1126 case STATUS_CANT_WAIT
: return WSAEWOULDBLOCK
;
1127 case STATUS_BUFFER_OVERFLOW
: return WSAEMSGSIZE
;
1128 case STATUS_NOT_SUPPORTED
: return WSAEOPNOTSUPP
;
1129 case STATUS_HOST_UNREACHABLE
: return WSAEHOSTUNREACH
;
1130 default: return RtlNtStatusToDosError( status
);
1134 /* set last error code from NT status without mapping WSA errors */
1135 static inline unsigned int set_error( unsigned int err
)
1139 err
= NtStatusToWSAError( err
);
1140 SetLastError( err
);
1145 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
1148 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
1153 static inline void release_sock_fd( SOCKET s
, int fd
)
1155 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
1158 static void _enable_event( HANDLE s
, unsigned int event
,
1159 unsigned int sstate
, unsigned int cstate
)
1161 SERVER_START_REQ( enable_socket_event
)
1163 req
->handle
= wine_server_obj_handle( s
);
1165 req
->sstate
= sstate
;
1166 req
->cstate
= cstate
;
1167 wine_server_call( req
);
1172 static DWORD
sock_is_blocking(SOCKET s
, BOOL
*ret
)
1175 SERVER_START_REQ( get_socket_event
)
1177 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1178 req
->service
= FALSE
;
1180 err
= NtStatusToWSAError( wine_server_call( req
));
1181 *ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
1187 static unsigned int _get_sock_mask(SOCKET s
)
1190 SERVER_START_REQ( get_socket_event
)
1192 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1193 req
->service
= FALSE
;
1195 wine_server_call( req
);
1202 static void _sync_sock_state(SOCKET s
)
1205 /* do a dummy wineserver request in order to let
1206 the wineserver run through its select loop once */
1207 sock_is_blocking(s
, &dummy
);
1210 static void _get_sock_errors(SOCKET s
, int *events
)
1212 SERVER_START_REQ( get_socket_event
)
1214 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1215 req
->service
= FALSE
;
1217 wine_server_set_reply( req
, events
, sizeof(int) * FD_MAX_EVENTS
);
1218 wine_server_call( req
);
1223 static int get_sock_error(SOCKET s
, unsigned int bit
)
1225 int events
[FD_MAX_EVENTS
];
1226 _get_sock_errors(s
, events
);
1230 static int _get_fd_type(int fd
)
1233 socklen_t optlen
= sizeof(sock_type
);
1234 getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char*) &sock_type
, &optlen
);
1238 static BOOL
set_dont_fragment(SOCKET s
, int level
, BOOL value
)
1242 if (level
== IPPROTO_IP
)
1245 optname
= IP_DONTFRAG
;
1246 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
1247 optname
= IP_MTU_DISCOVER
;
1248 value
= value
? IP_PMTUDISC_DO
: IP_PMTUDISC_DONT
;
1252 FIXME("IP_DONTFRAGMENT for IPv4 not supported in this platform\n");
1253 return TRUE
; /* fake success */
1258 #ifdef IPV6_DONTFRAG
1259 optname
= IPV6_DONTFRAG
;
1260 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
1261 optname
= IPV6_MTU_DISCOVER
;
1262 value
= value
? IPV6_PMTUDISC_DO
: IPV6_PMTUDISC_DONT
;
1266 FIXME("IP_DONTFRAGMENT for IPv6 not supported in this platform\n");
1267 return TRUE
; /* fake success */
1271 fd
= get_sock_fd(s
, 0, NULL
);
1272 if (fd
== -1) return FALSE
;
1274 if (!setsockopt(fd
, level
, optname
, &value
, sizeof(value
)))
1278 WSASetLastError(wsaErrno());
1282 release_sock_fd(s
, fd
);
1286 static BOOL
get_dont_fragment(SOCKET s
, int level
, BOOL
*out
)
1288 int fd
, optname
, value
, not_expected
;
1289 socklen_t optlen
= sizeof(value
);
1291 if (level
== IPPROTO_IP
)
1294 optname
= IP_DONTFRAG
;
1296 #elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
1297 optname
= IP_MTU_DISCOVER
;
1298 not_expected
= IP_PMTUDISC_DONT
;
1302 FIXME("IP_DONTFRAGMENT for IPv4 not supported in this platform\n");
1303 return TRUE
; /* fake success */
1308 #ifdef IPV6_DONTFRAG
1309 optname
= IPV6_DONTFRAG
;
1311 #elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DONT)
1312 optname
= IPV6_MTU_DISCOVER
;
1313 not_expected
= IPV6_PMTUDISC_DONT
;
1317 FIXME("IP_DONTFRAGMENT for IPv6 not supported in this platform\n");
1318 return TRUE
; /* fake success */
1322 fd
= get_sock_fd(s
, 0, NULL
);
1323 if (fd
== -1) return FALSE
;
1325 if (!getsockopt(fd
, level
, optname
, &value
, &optlen
))
1327 *out
= value
!= not_expected
;
1332 WSASetLastError(wsaErrno());
1336 release_sock_fd(s
, fd
);
1340 static struct per_thread_data
*get_per_thread_data(void)
1342 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
1343 /* lazy initialization */
1346 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
1347 NtCurrentTeb()->WinSockData
= ptb
;
1352 static void free_per_thread_data(void)
1354 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
1358 /* delete scratch buffers */
1359 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1360 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1361 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1362 HeapFree( GetProcessHeap(), 0, ptb
->fd_cache
);
1364 HeapFree( GetProcessHeap(), 0, ptb
);
1365 NtCurrentTeb()->WinSockData
= NULL
;
1368 /***********************************************************************
1369 * DllMain (WS2_32.init)
1371 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
1373 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
1374 switch (fdwReason
) {
1375 case DLL_PROCESS_ATTACH
:
1377 case DLL_PROCESS_DETACH
:
1378 if (fImpLoad
) break;
1379 free_per_thread_data();
1380 DeleteCriticalSection(&csWSgetXXXbyYYY
);
1382 case DLL_THREAD_DETACH
:
1383 free_per_thread_data();
1389 /***********************************************************************
1392 * Converts send/recv flags from Windows format.
1393 * Return the converted flag bits, unsupported flags remain unchanged.
1395 static int convert_flags(int flags
)
1398 if (!flags
) return 0;
1400 for (out
= i
= 0; flags
&& i
< ARRAY_SIZE(ws_flags_map
); i
++)
1402 if (ws_flags_map
[i
][0] & flags
)
1404 out
|= ws_flags_map
[i
][1];
1405 flags
&= ~ws_flags_map
[i
][0];
1410 FIXME("Unknown send/recv flags 0x%x, using anyway...\n", flags
);
1416 /***********************************************************************
1419 * Converts socket flags from Windows format.
1420 * Return 1 if converted, 0 if not (error).
1422 static int convert_sockopt(INT
*level
, INT
*optname
)
1428 *level
= SOL_SOCKET
;
1429 for(i
= 0; i
< ARRAY_SIZE(ws_sock_map
); i
++) {
1430 if( ws_sock_map
[i
][0] == *optname
)
1432 *optname
= ws_sock_map
[i
][1];
1436 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
1438 case WS_IPPROTO_TCP
:
1439 *level
= IPPROTO_TCP
;
1440 for(i
= 0; i
< ARRAY_SIZE(ws_tcp_map
); i
++) {
1441 if ( ws_tcp_map
[i
][0] == *optname
)
1443 *optname
= ws_tcp_map
[i
][1];
1447 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
1450 *level
= IPPROTO_IP
;
1451 for(i
= 0; i
< ARRAY_SIZE(ws_ip_map
); i
++) {
1452 if (ws_ip_map
[i
][0] == *optname
)
1454 *optname
= ws_ip_map
[i
][1];
1458 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
1460 case WS_IPPROTO_IPV6
:
1461 *level
= IPPROTO_IPV6
;
1462 for(i
= 0; i
< ARRAY_SIZE(ws_ipv6_map
); i
++) {
1463 if (ws_ipv6_map
[i
][0] == *optname
)
1465 *optname
= ws_ipv6_map
[i
][1];
1469 FIXME("Unknown IPPROTO_IPV6 optname 0x%x\n", *optname
);
1471 default: FIXME("Unimplemented or unknown socket level\n");
1476 /* ----------------------------------- Per-thread info (or per-process?) */
1478 static char *strdup_lower(const char *str
)
1481 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
1485 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
1488 else SetLastError(WSAENOBUFS
);
1492 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
1493 * from an fd and return the value converted to milli seconds
1494 * or 0 if there is an infinite time out */
1495 static inline INT64
get_rcvsnd_timeo( int fd
, BOOL is_recv
)
1498 socklen_t len
= sizeof(tv
);
1503 optname
= SO_RCVTIMEO
;
1509 optname
= SO_SNDTIMEO
;
1514 res
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
1517 return (UINT64
)tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
1520 /* utility: given an fd, will block until one of the events occurs */
1521 static inline int do_block( int fd
, int events
, int timeout
)
1527 pfd
.events
= events
;
1529 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
1540 convert_af_w2u(int windowsaf
) {
1543 for (i
= 0; i
< ARRAY_SIZE(ws_af_map
); i
++)
1544 if (ws_af_map
[i
][0] == windowsaf
)
1545 return ws_af_map
[i
][1];
1546 FIXME("unhandled Windows address family %d\n", windowsaf
);
1551 convert_af_u2w(int unixaf
) {
1554 for (i
= 0; i
< ARRAY_SIZE(ws_af_map
); i
++)
1555 if (ws_af_map
[i
][1] == unixaf
)
1556 return ws_af_map
[i
][0];
1557 FIXME("unhandled UNIX address family %d\n", unixaf
);
1562 convert_proto_w2u(int windowsproto
) {
1565 for (i
= 0; i
< ARRAY_SIZE(ws_proto_map
); i
++)
1566 if (ws_proto_map
[i
][0] == windowsproto
)
1567 return ws_proto_map
[i
][1];
1569 /* check for extended IPX */
1570 if (IS_IPX_PROTO(windowsproto
))
1571 return windowsproto
;
1573 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
1578 convert_proto_u2w(int unixproto
) {
1581 for (i
= 0; i
< ARRAY_SIZE(ws_proto_map
); i
++)
1582 if (ws_proto_map
[i
][1] == unixproto
)
1583 return ws_proto_map
[i
][0];
1585 /* if value is inside IPX range just return it - the kernel simply
1586 * echoes the value used in the socket() function */
1587 if (IS_IPX_PROTO(unixproto
))
1590 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
1595 convert_socktype_w2u(int windowssocktype
) {
1598 for (i
= 0; i
< ARRAY_SIZE(ws_socktype_map
); i
++)
1599 if (ws_socktype_map
[i
][0] == windowssocktype
)
1600 return ws_socktype_map
[i
][1];
1601 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
1606 convert_socktype_u2w(int unixsocktype
) {
1609 for (i
= 0; i
< ARRAY_SIZE(ws_socktype_map
); i
++)
1610 if (ws_socktype_map
[i
][1] == unixsocktype
)
1611 return ws_socktype_map
[i
][0];
1612 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
1616 static int convert_poll_w2u(int events
)
1619 for (i
= ret
= 0; events
&& i
< ARRAY_SIZE(ws_poll_map
); i
++)
1621 if (ws_poll_map
[i
][0] & events
)
1623 ret
|= ws_poll_map
[i
][1];
1624 events
&= ~ws_poll_map
[i
][0];
1629 FIXME("Unsupported WSAPoll() flags 0x%x\n", events
);
1633 static int convert_poll_u2w(int events
)
1636 for (i
= ret
= 0; events
&& i
< ARRAY_SIZE(ws_poll_map
); i
++)
1638 if (ws_poll_map
[i
][1] & events
)
1640 ret
|= ws_poll_map
[i
][0];
1641 events
&= ~ws_poll_map
[i
][1];
1646 FIXME("Unsupported poll() flags 0x%x\n", events
);
1650 static int set_ipx_packettype(int sock
, int ptype
)
1653 int fd
= get_sock_fd( sock
, 0, NULL
), ret
= 0;
1654 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", ptype
, fd
);
1656 if (fd
== -1) return SOCKET_ERROR
;
1658 /* We try to set the ipx type on ipx socket level. */
1660 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, &ptype
, sizeof(ptype
)) == -1)
1662 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
1668 /* Should we retrieve val using a getsockopt call and then
1669 * set the modified one? */
1671 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
1674 release_sock_fd( sock
, fd
);
1677 WARN("IPX support is not enabled, can't set packet type\n");
1678 return SOCKET_ERROR
;
1682 /* ----------------------------------- API -----
1684 * Init / cleanup / error checking.
1687 /***********************************************************************
1688 * WSAStartup (WS2_32.115)
1690 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
1692 TRACE("verReq=%x\n", wVersionRequested
);
1694 if (LOBYTE(wVersionRequested
) < 1)
1695 return WSAVERNOTSUPPORTED
;
1697 if (!lpWSAData
) return WSAEINVAL
;
1701 /* that's the whole of the negotiation for now */
1702 lpWSAData
->wVersion
= wVersionRequested
;
1703 /* return winsock information */
1704 lpWSAData
->wHighVersion
= 0x0202;
1705 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
1706 strcpy(lpWSAData
->szSystemStatus
, "Running" );
1707 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
1708 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
1709 /* don't do anything with lpWSAData->lpVendorInfo */
1710 /* (some apps don't allocate the space for this field) */
1712 TRACE("succeeded starts: %d\n", num_startup
);
1717 /***********************************************************************
1718 * WSACleanup (WS2_32.116)
1720 INT WINAPI
WSACleanup(void)
1724 TRACE("pending cleanups: %d\n", num_startup
);
1727 SetLastError(WSANOTINITIALISED
);
1728 return SOCKET_ERROR
;
1732 /***********************************************************************
1733 * WSAGetLastError (WS2_32.111)
1735 INT WINAPI
WSAGetLastError(void)
1737 return GetLastError();
1740 /***********************************************************************
1741 * WSASetLastError (WS2_32.112)
1743 void WINAPI
WSASetLastError(INT iError
) {
1744 SetLastError(iError
);
1747 static struct WS_hostent
*check_buffer_he(int size
)
1749 struct per_thread_data
* ptb
= get_per_thread_data();
1752 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
1753 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1755 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
1756 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
1757 return ptb
->he_buffer
;
1760 static struct WS_servent
*check_buffer_se(int size
)
1762 struct per_thread_data
* ptb
= get_per_thread_data();
1765 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
1766 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1768 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
1769 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
1770 return ptb
->se_buffer
;
1773 static struct WS_protoent
*check_buffer_pe(int size
)
1775 struct per_thread_data
* ptb
= get_per_thread_data();
1778 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
1779 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1781 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
1782 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
1783 return ptb
->pe_buffer
;
1786 /* ----------------------------------- i/o APIs */
1788 static inline BOOL
supported_pf(int pf
)
1808 static inline BOOL
supported_protocol(int protocol
)
1811 for (i
= 0; i
< ARRAY_SIZE(valid_protocols
); i
++)
1812 if (protocol
== valid_protocols
[i
])
1817 /**********************************************************************/
1819 /* Returns the length of the converted address if successful, 0 if it was too
1820 * small to start with or unknown family or invalid address buffer.
1822 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
1823 union generic_unix_sockaddr
*uaddr
)
1825 unsigned int uaddrlen
= 0;
1830 switch (wsaddr
->sa_family
)
1835 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
1836 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
1838 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
1841 uaddrlen
= sizeof(struct sockaddr_ipx
);
1842 memset( uaddr
, 0, uaddrlen
);
1843 uipx
->sipx_family
=AF_IPX
;
1844 uipx
->sipx_port
=wsipx
->sa_socket
;
1845 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
1848 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
1849 #ifdef IPX_FRAME_NONE
1850 uipx
->sipx_type
=IPX_FRAME_NONE
;
1856 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
1857 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
1859 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
1860 * scope_id, one without.
1862 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
1863 uaddrlen
= sizeof(struct sockaddr_in6
);
1864 memset( uaddr
, 0, uaddrlen
);
1865 uin6
->sin6_family
= AF_INET6
;
1866 uin6
->sin6_port
= win6
->sin6_port
;
1867 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
1868 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1869 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
1871 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1874 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
1878 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
1879 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
1881 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
1883 uaddrlen
= sizeof(struct sockaddr_in
);
1884 memset( uaddr
, 0, uaddrlen
);
1885 uin
->sin_family
= AF_INET
;
1886 uin
->sin_port
= win
->sin_port
;
1887 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
1892 struct sockaddr_irda
*uin
= (struct sockaddr_irda
*)uaddr
;
1893 const SOCKADDR_IRDA
*win
= (const SOCKADDR_IRDA
*)wsaddr
;
1895 if (wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1897 uaddrlen
= sizeof(struct sockaddr_irda
);
1898 memset( uaddr
, 0, uaddrlen
);
1899 uin
->sir_family
= AF_IRDA
;
1900 if (!strncmp( win
->irdaServiceName
, "LSAP-SEL", strlen( "LSAP-SEL" ) ))
1902 unsigned int lsap_sel
= 0;
1904 sscanf( win
->irdaServiceName
, "LSAP-SEL%u", &lsap_sel
);
1905 uin
->sir_lsap_sel
= lsap_sel
;
1909 uin
->sir_lsap_sel
= LSAP_ANY
;
1910 memcpy( uin
->sir_name
, win
->irdaServiceName
, 25 );
1912 memcpy( &uin
->sir_addr
, win
->irdaDeviceID
, sizeof(uin
->sir_addr
) );
1916 case WS_AF_UNSPEC
: {
1917 /* Try to determine the needed space by the passed windows sockaddr space */
1918 switch (wsaddrlen
) {
1919 default: /* likely an ipv4 address */
1920 case sizeof(struct WS_sockaddr_in
):
1921 uaddrlen
= sizeof(struct sockaddr_in
);
1924 case sizeof(struct WS_sockaddr_ipx
):
1925 uaddrlen
= sizeof(struct sockaddr_ipx
);
1929 case sizeof(SOCKADDR_IRDA
):
1930 uaddrlen
= sizeof(struct sockaddr_irda
);
1933 case sizeof(struct WS_sockaddr_in6
):
1934 case sizeof(struct WS_sockaddr_in6_old
):
1935 uaddrlen
= sizeof(struct sockaddr_in6
);
1938 memset( uaddr
, 0, uaddrlen
);
1942 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
1948 static BOOL
is_sockaddr_bound(const struct sockaddr
*uaddr
, int uaddrlen
)
1950 switch (uaddr
->sa_family
)
1955 static const struct sockaddr_ipx emptyAddr
;
1956 struct sockaddr_ipx
*ipx
= (struct sockaddr_ipx
*) uaddr
;
1957 return ipx
->sipx_port
1958 || memcmp(&ipx
->sipx_network
, &emptyAddr
.sipx_network
, sizeof(emptyAddr
.sipx_network
))
1959 || memcmp(&ipx
->sipx_node
, &emptyAddr
.sipx_node
, sizeof(emptyAddr
.sipx_node
));
1964 static const struct sockaddr_in6 emptyAddr
;
1965 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) uaddr
;
1966 return in6
->sin6_port
|| memcmp(&in6
->sin6_addr
, &emptyAddr
.sin6_addr
, sizeof(struct in6_addr
));
1970 static const struct sockaddr_in emptyAddr
;
1971 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) uaddr
;
1972 return in
->sin_port
|| memcmp(&in
->sin_addr
, &emptyAddr
.sin_addr
, sizeof(struct in_addr
));
1977 FIXME("unknown address family %d\n", uaddr
->sa_family
);
1982 /* Returns -1 if getsockname fails, 0 if not bound, 1 otherwise */
1983 static int is_fd_bound(int fd
, union generic_unix_sockaddr
*uaddr
, socklen_t
*uaddrlen
)
1985 union generic_unix_sockaddr inaddr
;
1989 if (!uaddr
) uaddr
= &inaddr
;
1990 if (!uaddrlen
) uaddrlen
= &inlen
;
1992 *uaddrlen
= sizeof(inaddr
);
1993 res
= getsockname(fd
, &uaddr
->addr
, uaddrlen
);
1994 if (!res
) res
= is_sockaddr_bound(&uaddr
->addr
, *uaddrlen
);
1998 /* Returns 0 if successful, -1 if the buffer is too small */
1999 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
2003 switch(uaddr
->sa_family
)
2008 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
2009 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
2012 switch (*wsaddrlen
) /* how much can we copy? */
2016 *wsaddrlen
= sizeof(*wsipx
);
2017 wsipx
->sa_socket
=uipx
->sipx_port
;
2021 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
2029 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
2035 wsipx
->sa_family
=WS_AF_IPX
;
2047 const struct sockaddr_irda
*uin
= (const struct sockaddr_irda
*)uaddr
;
2048 SOCKADDR_IRDA
*win
= (SOCKADDR_IRDA
*)wsaddr
;
2050 if (*wsaddrlen
< sizeof(SOCKADDR_IRDA
))
2052 win
->irdaAddressFamily
= WS_AF_IRDA
;
2053 memcpy( win
->irdaDeviceID
, &uin
->sir_addr
, sizeof(win
->irdaDeviceID
) );
2054 if (uin
->sir_lsap_sel
!= LSAP_ANY
)
2055 sprintf( win
->irdaServiceName
, "LSAP-SEL%u", uin
->sir_lsap_sel
);
2057 memcpy( win
->irdaServiceName
, uin
->sir_name
,
2058 sizeof(win
->irdaServiceName
) );
2063 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
2064 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
2066 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
2068 win6old
->sin6_family
= WS_AF_INET6
;
2069 win6old
->sin6_port
= uin6
->sin6_port
;
2070 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
2071 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
2072 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
2073 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
2074 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
2075 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
2076 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
2079 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
2081 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
2086 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
2087 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
2089 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
2091 win
->sin_family
= WS_AF_INET
;
2092 win
->sin_port
= uin
->sin_port
;
2093 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
2094 memset(win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
2095 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
2099 memset(wsaddr
,0,*wsaddrlen
);
2103 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
2109 static INT
WS_DuplicateSocket(BOOL unicode
, SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
2113 WSAPROTOCOL_INFOW infow
;
2115 TRACE("(unicode %d, socket %04lx, processid %x, buffer %p)\n",
2116 unicode
, s
, dwProcessId
, lpProtocolInfo
);
2118 if (!ws_protocol_info(s
, unicode
, &infow
, &size
))
2119 return SOCKET_ERROR
;
2121 if (!(hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
)))
2123 SetLastError(WSAEINVAL
);
2124 return SOCKET_ERROR
;
2127 if (!lpProtocolInfo
)
2129 CloseHandle(hProcess
);
2130 SetLastError(WSAEFAULT
);
2131 return SOCKET_ERROR
;
2134 /* I don't know what the real Windoze does next, this is a hack */
2135 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
2136 * the target use the global duplicate, or we could copy a reference to us to the structure
2137 * and let the target duplicate it from us, but let's do it as simple as possible */
2138 memcpy(lpProtocolInfo
, &infow
, size
);
2139 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
2140 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwServiceFlags3
,
2141 0, FALSE
, DUPLICATE_SAME_ACCESS
);
2142 CloseHandle(hProcess
);
2143 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
2147 /*****************************************************************************
2148 * WS_EnterSingleProtocolW [internal]
2150 * enters the protocol information of one given protocol into the given
2154 * TRUE if a protocol was entered into the buffer.
2157 * - only implemented for IPX, SPX, SPXII, TCP, UDP
2158 * - there is no check that the operating system supports the returned
2161 static BOOL
WS_EnterSingleProtocolW( INT protocol
, WSAPROTOCOL_INFOW
* info
)
2163 memset( info
, 0, sizeof(WSAPROTOCOL_INFOW
) );
2164 info
->iProtocol
= protocol
;
2168 case WS_IPPROTO_TCP
:
2169 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_EXPEDITED_DATA
|
2170 XP1_GRACEFUL_CLOSE
| XP1_GUARANTEED_ORDER
|
2171 XP1_GUARANTEED_DELIVERY
;
2172 info
->ProviderId
= ProviderIdIP
;
2173 info
->dwCatalogEntryId
= 0x3e9;
2174 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2175 info
->ProtocolChain
.ChainLen
= 1;
2177 info
->iAddressFamily
= WS_AF_INET
;
2178 info
->iMaxSockAddr
= 0x10;
2179 info
->iMinSockAddr
= 0x10;
2180 info
->iSocketType
= WS_SOCK_STREAM
;
2181 strcpyW( info
->szProtocol
, NameTcpW
);
2184 case WS_IPPROTO_UDP
:
2185 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_SUPPORT_BROADCAST
|
2186 XP1_SUPPORT_MULTIPOINT
| XP1_MESSAGE_ORIENTED
|
2188 info
->ProviderId
= ProviderIdIP
;
2189 info
->dwCatalogEntryId
= 0x3ea;
2190 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2191 info
->ProtocolChain
.ChainLen
= 1;
2193 info
->iAddressFamily
= WS_AF_INET
;
2194 info
->iMaxSockAddr
= 0x10;
2195 info
->iMinSockAddr
= 0x10;
2196 info
->iSocketType
= WS_SOCK_DGRAM
;
2197 info
->dwMessageSize
= 0xffbb;
2198 strcpyW( info
->szProtocol
, NameUdpW
);
2201 case WS_NSPROTO_IPX
:
2202 info
->dwServiceFlags1
= XP1_PARTIAL_MESSAGE
| XP1_SUPPORT_BROADCAST
|
2203 XP1_SUPPORT_MULTIPOINT
| XP1_MESSAGE_ORIENTED
|
2205 info
->ProviderId
= ProviderIdIPX
;
2206 info
->dwCatalogEntryId
= 0x406;
2207 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2208 info
->ProtocolChain
.ChainLen
= 1;
2210 info
->iAddressFamily
= WS_AF_IPX
;
2211 info
->iMaxSockAddr
= 0x10;
2212 info
->iMinSockAddr
= 0x0e;
2213 info
->iSocketType
= WS_SOCK_DGRAM
;
2214 info
->iProtocolMaxOffset
= 0xff;
2215 info
->dwMessageSize
= 0x240;
2216 strcpyW( info
->szProtocol
, NameIpxW
);
2219 case WS_NSPROTO_SPX
:
2220 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_PSEUDO_STREAM
|
2221 XP1_MESSAGE_ORIENTED
| XP1_GUARANTEED_ORDER
|
2222 XP1_GUARANTEED_DELIVERY
;
2223 info
->ProviderId
= ProviderIdSPX
;
2224 info
->dwCatalogEntryId
= 0x407;
2225 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2226 info
->ProtocolChain
.ChainLen
= 1;
2228 info
->iAddressFamily
= WS_AF_IPX
;
2229 info
->iMaxSockAddr
= 0x10;
2230 info
->iMinSockAddr
= 0x0e;
2231 info
->iSocketType
= WS_SOCK_SEQPACKET
;
2232 info
->dwMessageSize
= 0xffffffff;
2233 strcpyW( info
->szProtocol
, NameSpxW
);
2236 case WS_NSPROTO_SPXII
:
2237 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_GRACEFUL_CLOSE
|
2238 XP1_PSEUDO_STREAM
| XP1_MESSAGE_ORIENTED
|
2239 XP1_GUARANTEED_ORDER
| XP1_GUARANTEED_DELIVERY
;
2240 info
->ProviderId
= ProviderIdSPX
;
2241 info
->dwCatalogEntryId
= 0x409;
2242 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
2243 info
->ProtocolChain
.ChainLen
= 1;
2245 info
->iAddressFamily
= WS_AF_IPX
;
2246 info
->iMaxSockAddr
= 0x10;
2247 info
->iMinSockAddr
= 0x0e;
2248 info
->iSocketType
= WS_SOCK_SEQPACKET
;
2249 info
->dwMessageSize
= 0xffffffff;
2250 strcpyW( info
->szProtocol
, NameSpxIIW
);
2254 FIXME("unknown Protocol <0x%08x>\n", protocol
);
2260 /*****************************************************************************
2261 * WS_EnterSingleProtocolA [internal]
2263 * see function WS_EnterSingleProtocolW
2266 static BOOL
WS_EnterSingleProtocolA( INT protocol
, WSAPROTOCOL_INFOA
* info
)
2268 WSAPROTOCOL_INFOW infow
;
2270 memset( info
, 0, sizeof(WSAPROTOCOL_INFOA
) );
2272 ret
= WS_EnterSingleProtocolW( protocol
, &infow
);
2275 /* convert the structure from W to A */
2276 memcpy( info
, &infow
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
2277 WideCharToMultiByte( CP_ACP
, 0, infow
.szProtocol
, -1,
2278 info
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
);
2284 static INT
WS_EnumProtocols( BOOL unicode
, const INT
*protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
)
2286 INT i
= 0, items
= 0;
2290 LPWSAPROTOCOL_INFOA a
;
2291 LPWSAPROTOCOL_INFOW w
;
2295 if (!protocols
) protocols
= valid_protocols
;
2297 while (protocols
[i
])
2299 if(supported_protocol(protocols
[i
++]))
2303 size
= items
* (unicode
? sizeof(WSAPROTOCOL_INFOW
) : sizeof(WSAPROTOCOL_INFOA
));
2305 TRACE("unicode %d, protocols %p, buffer %p, length %p %d, items %d, required %d\n",
2306 unicode
, protocols
, buffer
, len
, len
? *len
: 0, items
, size
);
2308 if (*len
< size
|| !buffer
)
2311 SetLastError(WSAENOBUFS
);
2312 return SOCKET_ERROR
;
2315 for (i
= items
= 0; protocols
[i
]; i
++)
2317 if (!supported_protocol(protocols
[i
])) continue;
2320 if (WS_EnterSingleProtocolW( protocols
[i
], &info
.w
[items
] ))
2325 if (WS_EnterSingleProtocolA( protocols
[i
], &info
.a
[items
] ))
2332 static BOOL
ws_protocol_info(SOCKET s
, int unicode
, WSAPROTOCOL_INFOW
*buffer
, int *size
)
2339 *size
= unicode
? sizeof(WSAPROTOCOL_INFOW
) : sizeof(WSAPROTOCOL_INFOA
);
2340 memset(buffer
, 0, *size
);
2342 SERVER_START_REQ( get_socket_info
)
2344 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
2345 status
= wine_server_call( req
);
2348 address_family
= convert_af_u2w(reply
->family
);
2349 socket_type
= convert_socktype_u2w(reply
->type
);
2350 protocol
= convert_proto_u2w(reply
->protocol
);
2357 unsigned int err
= NtStatusToWSAError( status
);
2358 SetLastError( err
== WSAEBADF
? WSAENOTSOCK
: err
);
2363 WS_EnterSingleProtocolW( protocol
, buffer
);
2365 WS_EnterSingleProtocolA( protocol
, (WSAPROTOCOL_INFOA
*)buffer
);
2366 buffer
->iAddressFamily
= address_family
;
2367 buffer
->iSocketType
= socket_type
;
2368 buffer
->iProtocol
= protocol
;
2373 /**************************************************************************
2374 * Functions for handling overlapped I/O
2375 **************************************************************************/
2377 /* user APC called upon async completion */
2378 static void WINAPI
ws2_async_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
2380 struct ws2_async
*wsa
= arg
;
2382 if (wsa
->completion_func
) wsa
->completion_func( NtStatusToWSAError(iosb
->u
.Status
),
2383 iosb
->Information
, wsa
->user_overlapped
,
2385 release_async_io( &wsa
->io
);
2388 /***********************************************************************
2389 * WS2_recv (INTERNAL)
2391 * Workhorse for both synchronous and asynchronous recv() operations.
2393 static int WS2_recv( int fd
, struct ws2_async
*wsa
, int flags
)
2395 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2399 union generic_unix_sockaddr unix_sockaddr
;
2402 hdr
.msg_name
= NULL
;
2406 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
2407 hdr
.msg_name
= &unix_sockaddr
;
2410 hdr
.msg_namelen
= 0;
2412 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
2413 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
2414 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2415 hdr
.msg_accrights
= NULL
;
2416 hdr
.msg_accrightslen
= 0;
2418 hdr
.msg_control
= pktbuf
;
2419 hdr
.msg_controllen
= sizeof(pktbuf
);
2423 while ((n
= __wine_locked_recvmsg( fd
, &hdr
, flags
)) == -1)
2429 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2432 ERR("Message control headers cannot be properly supported on this system.\n");
2433 wsa
->control
->len
= 0;
2436 if (wsa
->control
&& !convert_control_headers(&hdr
, wsa
->control
))
2438 WARN("Application passed insufficient room for control headers.\n");
2439 *wsa
->lpFlags
|= WS_MSG_CTRUNC
;
2445 /* if this socket is connected and lpFrom is not NULL, Linux doesn't give us
2446 * msg_name and msg_namelen from recvmsg, but it does set msg_namelen to zero.
2448 * quoting linux 2.6 net/ipv4/tcp.c:
2449 * "According to UNIX98, msg_name/msg_namelen are ignored
2450 * on connected socket. I was just happy when found this 8) --ANK"
2452 * likewise MSDN says that lpFrom and lpFromlen are ignored for
2453 * connection-oriented sockets, so don't try to update lpFrom.
2455 if (wsa
->addr
&& hdr
.msg_namelen
)
2456 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, wsa
->addr
, wsa
->addrlen
.ptr
);
2461 /***********************************************************************
2462 * WS2_async_recv (INTERNAL)
2464 * Handler for overlapped recv() operations.
2466 static NTSTATUS
WS2_async_recv( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
2468 struct ws2_async
*wsa
= user
;
2473 case STATUS_ALERTED
:
2474 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
2477 result
= WS2_recv( fd
, wsa
, convert_flags(wsa
->flags
) );
2478 wine_server_release_fd( wsa
->hSocket
, fd
);
2481 status
= STATUS_SUCCESS
;
2482 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
2486 if (errno
== EAGAIN
)
2488 status
= STATUS_PENDING
;
2489 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
2494 status
= wsaErrStatus();
2499 if (status
!= STATUS_PENDING
)
2501 iosb
->u
.Status
= status
;
2502 iosb
->Information
= result
;
2503 if (!wsa
->completion_func
)
2504 release_async_io( &wsa
->io
);
2509 /***********************************************************************
2510 * WS2_async_accept_recv (INTERNAL)
2512 * This function is used to finish the read part of an accept request. It is
2513 * needed to place the completion on the correct socket (listener).
2515 static NTSTATUS
WS2_async_accept_recv( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
2517 struct ws2_accept_async
*wsa
= user
;
2519 status
= WS2_async_recv( wsa
->read
, iosb
, status
);
2520 if (status
== STATUS_PENDING
)
2524 WS_AddCompletion( HANDLE2SOCKET(wsa
->listen_socket
), wsa
->cvalue
, iosb
->u
.Status
, iosb
->Information
, TRUE
);
2526 release_async_io( &wsa
->io
);
2530 /***********************************************************************
2531 * WS2_async_accept (INTERNAL)
2533 * This is the function called to satisfy the AcceptEx callback
2535 static NTSTATUS
WS2_async_accept( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
2537 struct ws2_accept_async
*wsa
= user
;
2541 TRACE("status: 0x%x listen: %p, accept: %p\n", status
, wsa
->listen_socket
, wsa
->accept_socket
);
2543 if (status
== STATUS_ALERTED
)
2545 SERVER_START_REQ( accept_into_socket
)
2547 req
->lhandle
= wine_server_obj_handle( wsa
->listen_socket
);
2548 req
->ahandle
= wine_server_obj_handle( wsa
->accept_socket
);
2549 status
= wine_server_call( req
);
2553 if (NtStatusToWSAError( status
) == WSAEWOULDBLOCK
)
2554 return STATUS_PENDING
;
2556 if (status
== STATUS_INVALID_HANDLE
)
2558 FIXME("AcceptEx accepting socket closed but request was not cancelled\n");
2559 status
= STATUS_CANCELLED
;
2562 else if (status
== STATUS_HANDLES_CLOSED
)
2563 status
= STATUS_CANCELLED
; /* strange windows behavior */
2565 if (status
!= STATUS_SUCCESS
)
2568 /* WS2 Spec says size param is extra 16 bytes long...what do we put in it? */
2569 addr
= ((char *)wsa
->buf
) + wsa
->data_len
;
2570 len
= wsa
->local_len
- sizeof(int);
2571 WS_getsockname(HANDLE2SOCKET(wsa
->accept_socket
),
2572 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
2575 addr
+= wsa
->local_len
;
2576 len
= wsa
->remote_len
- sizeof(int);
2577 WS_getpeername(HANDLE2SOCKET(wsa
->accept_socket
),
2578 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
2584 wsa
->io
.callback
= WS2_async_accept_recv
;
2585 status
= register_async( ASYNC_TYPE_READ
, wsa
->accept_socket
, &wsa
->io
,
2586 wsa
->user_overlapped
->hEvent
, NULL
, NULL
, iosb
);
2588 if (status
!= STATUS_PENDING
)
2591 /* The APC has finished but no completion should be sent for the operation yet, additional processing
2592 * needs to be performed by WS2_async_accept_recv() first. */
2593 return STATUS_MORE_PROCESSING_REQUIRED
;
2596 iosb
->u
.Status
= status
;
2597 iosb
->Information
= 0;
2599 if (wsa
->read
) release_async_io( &wsa
->read
->io
);
2600 release_async_io( &wsa
->io
);
2604 /***********************************************************************
2605 * WS2_send (INTERNAL)
2607 * Workhorse for both synchronous and asynchronous send() operations.
2609 static int WS2_send( int fd
, struct ws2_async
*wsa
, int flags
)
2612 union generic_unix_sockaddr unix_addr
;
2615 hdr
.msg_name
= NULL
;
2616 hdr
.msg_namelen
= 0;
2620 hdr
.msg_name
= &unix_addr
;
2621 hdr
.msg_namelen
= ws_sockaddr_ws2u( wsa
->addr
, wsa
->addrlen
.val
, &unix_addr
);
2622 if ( !hdr
.msg_namelen
)
2628 #if defined(HAS_IPX) && defined(SOL_IPX)
2629 if(wsa
->addr
->sa_family
== WS_AF_IPX
)
2631 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
2633 socklen_t len
= sizeof(int);
2635 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
2636 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
2637 * the packet type and then we can retrieve it using getsockopt. After that we can set the
2638 * ipx type in the sockaddr_opx structure with the stored value.
2640 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
2641 uipx
->sipx_type
= val
;
2646 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
2647 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
2648 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2649 hdr
.msg_accrights
= NULL
;
2650 hdr
.msg_accrightslen
= 0;
2652 hdr
.msg_control
= NULL
;
2653 hdr
.msg_controllen
= 0;
2657 while ((ret
= sendmsg(fd
, &hdr
, flags
)) == -1)
2659 if (errno
== EISCONN
)
2662 hdr
.msg_namelen
= 0;
2670 while (wsa
->first_iovec
< wsa
->n_iovecs
&& wsa
->iovec
[wsa
->first_iovec
].iov_len
<= n
)
2671 n
-= wsa
->iovec
[wsa
->first_iovec
++].iov_len
;
2672 if (wsa
->first_iovec
< wsa
->n_iovecs
)
2674 wsa
->iovec
[wsa
->first_iovec
].iov_base
= (char*)wsa
->iovec
[wsa
->first_iovec
].iov_base
+ n
;
2675 wsa
->iovec
[wsa
->first_iovec
].iov_len
-= n
;
2680 /***********************************************************************
2681 * WS2_async_send (INTERNAL)
2683 * Handler for overlapped send() operations.
2685 static NTSTATUS
WS2_async_send( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
2687 struct ws2_async
*wsa
= user
;
2692 case STATUS_ALERTED
:
2693 if ( wsa
->n_iovecs
<= wsa
->first_iovec
)
2696 status
= STATUS_SUCCESS
;
2699 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
2702 /* check to see if the data is ready (non-blocking) */
2703 result
= WS2_send( fd
, wsa
, convert_flags(wsa
->flags
) );
2704 wine_server_release_fd( wsa
->hSocket
, fd
);
2708 if (wsa
->first_iovec
< wsa
->n_iovecs
)
2709 status
= STATUS_PENDING
;
2711 status
= STATUS_SUCCESS
;
2713 iosb
->Information
+= result
;
2715 else if (errno
== EAGAIN
)
2717 status
= STATUS_PENDING
;
2721 status
= wsaErrStatus();
2725 if (status
!= STATUS_PENDING
)
2727 iosb
->u
.Status
= status
;
2728 if (!wsa
->completion_func
)
2729 release_async_io( &wsa
->io
);
2734 /***********************************************************************
2735 * WS2_async_shutdown (INTERNAL)
2737 * Handler for shutdown() operations on overlapped sockets.
2739 static NTSTATUS
WS2_async_shutdown( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
2741 struct ws2_async_shutdown
*wsa
= user
;
2746 case STATUS_ALERTED
:
2747 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
2750 switch ( wsa
->type
)
2752 case ASYNC_TYPE_READ
: err
= shutdown( fd
, 0 ); break;
2753 case ASYNC_TYPE_WRITE
: err
= shutdown( fd
, 1 ); break;
2755 status
= err
? wsaErrStatus() : STATUS_SUCCESS
;
2756 wine_server_release_fd( wsa
->hSocket
, fd
);
2759 iosb
->u
.Status
= status
;
2760 iosb
->Information
= 0;
2761 release_async_io( &wsa
->io
);
2765 /***********************************************************************
2766 * WS2_register_async_shutdown (INTERNAL)
2768 * Helper function for WS_shutdown() on overlapped sockets.
2770 static int WS2_register_async_shutdown( SOCKET s
, int type
)
2772 struct ws2_async_shutdown
*wsa
;
2775 TRACE("socket %04lx type %d\n", s
, type
);
2777 wsa
= (struct ws2_async_shutdown
*)alloc_async_io( sizeof(*wsa
), WS2_async_shutdown
);
2781 wsa
->hSocket
= SOCKET2HANDLE(s
);
2784 status
= register_async( type
, wsa
->hSocket
, &wsa
->io
, 0, NULL
, NULL
, &wsa
->iosb
);
2785 if (status
!= STATUS_PENDING
)
2787 HeapFree( GetProcessHeap(), 0, wsa
);
2788 return NtStatusToWSAError( status
);
2793 /***********************************************************************
2796 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
, int *addrlen32
)
2803 TRACE("socket %04lx\n", s
);
2804 err
= sock_is_blocking(s
, &is_blocking
);
2810 /* try accepting first (if there is a deferred connection) */
2811 SERVER_START_REQ( accept_socket
)
2813 req
->lhandle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
2814 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
2815 req
->attributes
= OBJ_INHERIT
;
2816 err
= NtStatusToWSAError( wine_server_call( req
));
2817 as
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
2822 if (addr
&& addrlen32
&& WS_getpeername(as
, addr
, addrlen32
))
2825 return SOCKET_ERROR
;
2827 TRACE("\taccepted %04lx\n", as
);
2830 if (!is_blocking
) break;
2831 if (err
!= WSAEWOULDBLOCK
) break;
2832 fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2834 do_block(fd
, POLLIN
, -1);
2835 _sync_sock_state(s
); /* let wineserver notice connection */
2836 release_sock_fd( s
, fd
);
2840 WARN(" -> ERROR %d\n", err
);
2842 return INVALID_SOCKET
;
2845 /***********************************************************************
2848 static BOOL WINAPI
WS2_AcceptEx(SOCKET listener
, SOCKET acceptor
, PVOID dest
, DWORD dest_len
,
2849 DWORD local_addr_len
, DWORD rem_addr_len
, LPDWORD received
,
2850 LPOVERLAPPED overlapped
)
2853 struct ws2_accept_async
*wsa
;
2856 TRACE("(%04lx, %04lx, %p, %d, %d, %d, %p, %p)\n", listener
, acceptor
, dest
, dest_len
, local_addr_len
,
2857 rem_addr_len
, received
, overlapped
);
2861 SetLastError(WSA_INVALID_PARAMETER
);
2864 overlapped
->Internal
= STATUS_PENDING
;
2868 SetLastError(WSAEINVAL
);
2874 SetLastError(WSAEFAULT
);
2878 fd
= get_sock_fd( listener
, FILE_READ_DATA
, NULL
);
2881 SetLastError(WSAENOTSOCK
);
2884 release_sock_fd( listener
, fd
);
2886 fd
= get_sock_fd( acceptor
, FILE_READ_DATA
, NULL
);
2889 SetLastError(WSAENOTSOCK
);
2892 release_sock_fd( acceptor
, fd
);
2894 wsa
= (struct ws2_accept_async
*)alloc_async_io( sizeof(*wsa
), WS2_async_accept
);
2897 SetLastError(WSAEFAULT
);
2901 wsa
->listen_socket
= SOCKET2HANDLE(listener
);
2902 wsa
->accept_socket
= SOCKET2HANDLE(acceptor
);
2903 wsa
->user_overlapped
= overlapped
;
2904 wsa
->cvalue
= !((ULONG_PTR
)overlapped
->hEvent
& 1) ? (ULONG_PTR
)overlapped
: 0;
2906 wsa
->data_len
= dest_len
;
2907 wsa
->local_len
= local_addr_len
;
2908 wsa
->remote_len
= rem_addr_len
;
2913 /* set up a read request if we need it */
2914 wsa
->read
= (struct ws2_async
*)alloc_async_io( offsetof(struct ws2_async
, iovec
[1]), WS2_async_accept_recv
);
2917 HeapFree( GetProcessHeap(), 0, wsa
);
2918 SetLastError(WSAEFAULT
);
2922 wsa
->read
->hSocket
= wsa
->accept_socket
;
2923 wsa
->read
->flags
= 0;
2924 wsa
->read
->lpFlags
= &wsa
->read
->flags
;
2925 wsa
->read
->addr
= NULL
;
2926 wsa
->read
->addrlen
.ptr
= NULL
;
2927 wsa
->read
->control
= NULL
;
2928 wsa
->read
->n_iovecs
= 1;
2929 wsa
->read
->first_iovec
= 0;
2930 wsa
->read
->completion_func
= NULL
;
2931 wsa
->read
->iovec
[0].iov_base
= wsa
->buf
;
2932 wsa
->read
->iovec
[0].iov_len
= wsa
->data_len
;
2935 status
= register_async( ASYNC_TYPE_READ
, SOCKET2HANDLE(listener
), &wsa
->io
,
2936 overlapped
->hEvent
, NULL
, (void *)wsa
->cvalue
, (IO_STATUS_BLOCK
*)overlapped
);
2938 if(status
!= STATUS_PENDING
)
2940 HeapFree( GetProcessHeap(), 0, wsa
->read
);
2941 HeapFree( GetProcessHeap(), 0, wsa
);
2944 SetLastError( NtStatusToWSAError(status
) );
2948 /***********************************************************************
2949 * WS2_ReadFile (INTERNAL)
2951 * Perform an APC-safe ReadFile operation
2953 static NTSTATUS
WS2_ReadFile(HANDLE hFile
, PIO_STATUS_BLOCK io_status
, char* buffer
, ULONG length
,
2954 PLARGE_INTEGER offset
)
2956 int result
= -1, unix_handle
;
2957 unsigned int options
;
2960 TRACE( "(%p,%p,0x%08x)\n", hFile
, buffer
,length
);
2962 status
= wine_server_handle_to_fd( hFile
, FILE_READ_DATA
, &unix_handle
, &options
);
2963 if (status
) return status
;
2965 while (result
== -1)
2967 if (offset
->QuadPart
!= FILE_USE_FILE_POINTER_POSITION
)
2968 result
= pread( unix_handle
, buffer
, length
, offset
->QuadPart
);
2970 result
= read( unix_handle
, buffer
, length
);
2976 status
= (length
? STATUS_END_OF_FILE
: STATUS_SUCCESS
);
2977 else if (result
!= -1)
2978 status
= STATUS_SUCCESS
;
2979 else if (errno
!= EAGAIN
)
2980 status
= wsaErrStatus();
2982 status
= STATUS_PENDING
;
2984 wine_server_release_fd( hFile
, unix_handle
);
2985 TRACE("= 0x%08x (%d)\n", status
, result
);
2986 if (status
== STATUS_SUCCESS
|| status
== STATUS_END_OF_FILE
)
2988 io_status
->u
.Status
= status
;
2989 io_status
->Information
= result
;
2995 /***********************************************************************
2996 * WS2_transmitfile_getbuffer (INTERNAL)
2998 * Pick the appropriate buffer for a TransmitFile send operation.
3000 static NTSTATUS
WS2_transmitfile_getbuffer( int fd
, struct ws2_transmitfile_async
*wsa
)
3002 /* send any incomplete writes from a previous iteration */
3003 if (wsa
->write
.first_iovec
< wsa
->write
.n_iovecs
)
3004 return STATUS_PENDING
;
3006 /* process the header (if applicable) */
3007 if (wsa
->buffers
.Head
)
3009 wsa
->write
.first_iovec
= 0;
3010 wsa
->write
.n_iovecs
= 1;
3011 wsa
->write
.iovec
[0].iov_base
= wsa
->buffers
.Head
;
3012 wsa
->write
.iovec
[0].iov_len
= wsa
->buffers
.HeadLength
;
3013 wsa
->buffers
.Head
= NULL
;
3014 return STATUS_PENDING
;
3017 /* process the main file */
3020 DWORD bytes_per_send
= wsa
->bytes_per_send
;
3021 IO_STATUS_BLOCK iosb
;
3024 iosb
.Information
= 0;
3025 /* when the size of the transfer is limited ensure that we don't go past that limit */
3026 if (wsa
->file_bytes
!= 0)
3027 bytes_per_send
= min(bytes_per_send
, wsa
->file_bytes
- wsa
->file_read
);
3028 status
= WS2_ReadFile( wsa
->file
, &iosb
, wsa
->buffer
, bytes_per_send
, &wsa
->offset
);
3029 if (wsa
->offset
.QuadPart
!= FILE_USE_FILE_POINTER_POSITION
)
3030 wsa
->offset
.QuadPart
+= iosb
.Information
;
3031 if (status
== STATUS_END_OF_FILE
)
3032 wsa
->file
= NULL
; /* continue on to the footer */
3033 else if (status
!= STATUS_SUCCESS
)
3037 if (iosb
.Information
)
3039 wsa
->write
.first_iovec
= 0;
3040 wsa
->write
.n_iovecs
= 1;
3041 wsa
->write
.iovec
[0].iov_base
= wsa
->buffer
;
3042 wsa
->write
.iovec
[0].iov_len
= iosb
.Information
;
3043 wsa
->file_read
+= iosb
.Information
;
3046 if (wsa
->file_bytes
!= 0 && wsa
->file_read
>= wsa
->file_bytes
)
3049 return STATUS_PENDING
;
3053 /* send the footer (if applicable) */
3054 if (wsa
->buffers
.Tail
)
3056 wsa
->write
.first_iovec
= 0;
3057 wsa
->write
.n_iovecs
= 1;
3058 wsa
->write
.iovec
[0].iov_base
= wsa
->buffers
.Tail
;
3059 wsa
->write
.iovec
[0].iov_len
= wsa
->buffers
.TailLength
;
3060 wsa
->buffers
.Tail
= NULL
;
3061 return STATUS_PENDING
;
3064 return STATUS_SUCCESS
;
3067 /***********************************************************************
3068 * WS2_transmitfile_base (INTERNAL)
3070 * Shared implementation for both synchronous and asynchronous TransmitFile.
3072 static NTSTATUS
WS2_transmitfile_base( int fd
, struct ws2_transmitfile_async
*wsa
)
3076 status
= WS2_transmitfile_getbuffer( fd
, wsa
);
3077 if (status
== STATUS_PENDING
)
3079 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)wsa
->write
.user_overlapped
;
3082 n
= WS2_send( fd
, &wsa
->write
, convert_flags(wsa
->write
.flags
) );
3085 if (iosb
) iosb
->Information
+= n
;
3087 else if (errno
!= EAGAIN
)
3088 return wsaErrStatus();
3094 /***********************************************************************
3095 * WS2_async_transmitfile (INTERNAL)
3097 * Asynchronous callback for overlapped TransmitFile operations.
3099 static NTSTATUS
WS2_async_transmitfile( void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
)
3101 struct ws2_transmitfile_async
*wsa
= user
;
3104 if (status
== STATUS_ALERTED
)
3106 if (!(status
= wine_server_handle_to_fd( wsa
->write
.hSocket
, FILE_WRITE_DATA
, &fd
, NULL
)))
3108 status
= WS2_transmitfile_base( fd
, wsa
);
3109 wine_server_release_fd( wsa
->write
.hSocket
, fd
);
3111 if (status
== STATUS_PENDING
)
3115 iosb
->u
.Status
= status
;
3116 release_async_io( &wsa
->io
);
3120 /***********************************************************************
3123 static BOOL WINAPI
WS2_TransmitFile( SOCKET s
, HANDLE h
, DWORD file_bytes
, DWORD bytes_per_send
,
3124 LPOVERLAPPED overlapped
, LPTRANSMIT_FILE_BUFFERS buffers
,
3127 union generic_unix_sockaddr uaddr
;
3128 socklen_t uaddrlen
= sizeof(uaddr
);
3129 struct ws2_transmitfile_async
*wsa
;
3133 TRACE("(%lx, %p, %d, %d, %p, %p, %d)\n", s
, h
, file_bytes
, bytes_per_send
, overlapped
,
3136 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, NULL
);
3139 WSASetLastError( WSAENOTSOCK
);
3142 if (getpeername( fd
, &uaddr
.addr
, &uaddrlen
) != 0)
3144 release_sock_fd( s
, fd
);
3145 WSASetLastError( WSAENOTCONN
);
3149 FIXME("Flags are not currently supported (0x%x).\n", flags
);
3151 if (h
&& GetFileType( h
) != FILE_TYPE_DISK
)
3153 FIXME("Non-disk file handles are not currently supported.\n");
3154 release_sock_fd( s
, fd
);
3155 WSASetLastError( WSAEOPNOTSUPP
);
3159 /* set reasonable defaults when requested */
3160 if (!bytes_per_send
)
3161 bytes_per_send
= (1 << 16); /* Depends on OS version: PAGE_SIZE, 2*PAGE_SIZE, or 2^16 */
3163 if (!(wsa
= (struct ws2_transmitfile_async
*)alloc_async_io( sizeof(*wsa
) + bytes_per_send
,
3164 WS2_async_transmitfile
)))
3166 release_sock_fd( s
, fd
);
3167 WSASetLastError( WSAEFAULT
);
3171 wsa
->buffers
= *buffers
;
3173 memset(&wsa
->buffers
, 0x0, sizeof(wsa
->buffers
));
3174 wsa
->buffer
= (char *)(wsa
+ 1);
3177 wsa
->file_bytes
= file_bytes
;
3178 wsa
->bytes_per_send
= bytes_per_send
;
3180 wsa
->offset
.QuadPart
= FILE_USE_FILE_POINTER_POSITION
;
3181 wsa
->write
.hSocket
= SOCKET2HANDLE(s
);
3182 wsa
->write
.addr
= NULL
;
3183 wsa
->write
.addrlen
.val
= 0;
3184 wsa
->write
.flags
= 0;
3185 wsa
->write
.lpFlags
= &wsa
->flags
;
3186 wsa
->write
.control
= NULL
;
3187 wsa
->write
.n_iovecs
= 0;
3188 wsa
->write
.first_iovec
= 0;
3189 wsa
->write
.user_overlapped
= overlapped
;
3192 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)overlapped
;
3195 wsa
->offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3196 wsa
->offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
3197 iosb
->u
.Status
= STATUS_PENDING
;
3198 iosb
->Information
= 0;
3199 status
= register_async( ASYNC_TYPE_WRITE
, SOCKET2HANDLE(s
), &wsa
->io
,
3200 overlapped
->hEvent
, NULL
, NULL
, iosb
);
3201 if(status
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
3202 release_sock_fd( s
, fd
);
3203 WSASetLastError( NtStatusToWSAError(status
) );
3209 status
= WS2_transmitfile_base( fd
, wsa
);
3210 if (status
== STATUS_PENDING
)
3213 do_block(fd
, POLLOUT
, -1);
3214 _sync_sock_state(s
); /* let wineserver notice connection */
3217 while (status
== STATUS_PENDING
);
3218 release_sock_fd( s
, fd
);
3220 if (status
!= STATUS_SUCCESS
)
3221 WSASetLastError( NtStatusToWSAError(status
) );
3222 HeapFree( GetProcessHeap(), 0, wsa
);
3223 return (status
== STATUS_SUCCESS
);
3226 /***********************************************************************
3227 * GetAcceptExSockaddrs
3229 static void WINAPI
WS2_GetAcceptExSockaddrs(PVOID buffer
, DWORD data_size
, DWORD local_size
, DWORD remote_size
,
3230 struct WS_sockaddr
**local_addr
, LPINT local_addr_len
,
3231 struct WS_sockaddr
**remote_addr
, LPINT remote_addr_len
)
3233 char *cbuf
= buffer
;
3234 TRACE("(%p, %d, %d, %d, %p, %p, %p, %p)\n", buffer
, data_size
, local_size
, remote_size
, local_addr
,
3235 local_addr_len
, remote_addr
, remote_addr_len
);
3238 *local_addr_len
= *(int *) cbuf
;
3239 *local_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
3243 *remote_addr_len
= *(int *) cbuf
;
3244 *remote_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
3247 /***********************************************************************
3250 int WINAPI
WSASendMsg( SOCKET s
, LPWSAMSG msg
, DWORD dwFlags
, LPDWORD lpNumberOfBytesSent
,
3251 LPWSAOVERLAPPED lpOverlapped
,
3252 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3256 SetLastError( WSAEFAULT
);
3257 return SOCKET_ERROR
;
3260 return WS2_sendto( s
, msg
->lpBuffers
, msg
->dwBufferCount
, lpNumberOfBytesSent
,
3261 dwFlags
, msg
->name
, msg
->namelen
,
3262 lpOverlapped
, lpCompletionRoutine
);
3265 /***********************************************************************
3268 * Perform a receive operation that is capable of returning message
3269 * control headers. It is important to note that the WSAMSG parameter
3270 * must remain valid throughout the operation, even when an overlapped
3271 * receive is performed.
3273 static int WINAPI
WS2_WSARecvMsg( SOCKET s
, LPWSAMSG msg
, LPDWORD lpNumberOfBytesRecvd
,
3274 LPWSAOVERLAPPED lpOverlapped
,
3275 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3279 SetLastError( WSAEFAULT
);
3280 return SOCKET_ERROR
;
3283 return WS2_recv_base( s
, msg
->lpBuffers
, msg
->dwBufferCount
, lpNumberOfBytesRecvd
,
3284 &msg
->dwFlags
, msg
->name
, &msg
->namelen
,
3285 lpOverlapped
, lpCompletionRoutine
, &msg
->Control
);
3288 /***********************************************************************
3289 * interface_bind (INTERNAL)
3291 * Take bind() calls on any name corresponding to a local network adapter and restrict the given socket to
3292 * operating only on the specified interface. This restriction consists of two components:
3293 * 1) An outgoing packet restriction suggesting the egress interface for all packets.
3294 * 2) An incoming packet restriction dropping packets not meant for the interface.
3295 * If the function succeeds in placing these restrictions (returns TRUE) then the name for the bind() may
3296 * safely be changed to INADDR_ANY, permitting the transmission and receipt of broadcast packets on the
3297 * socket. This behavior is only relevant to UDP sockets and is needed for applications that expect to be able
3298 * to receive broadcast packets on a socket that is bound to a specific network interface.
3300 static BOOL
interface_bind( SOCKET s
, int fd
, struct sockaddr
*addr
)
3302 struct sockaddr_in
*in_sock
= (struct sockaddr_in
*) addr
;
3303 in_addr_t bind_addr
= in_sock
->sin_addr
.s_addr
;
3304 PIP_ADAPTER_INFO adapters
= NULL
, adapter
;
3309 if (bind_addr
== htonl(INADDR_ANY
) || bind_addr
== htonl(INADDR_LOOPBACK
))
3310 return FALSE
; /* Not binding to a network adapter, special interface binding unnecessary. */
3311 if (_get_fd_type(fd
) != SOCK_DGRAM
)
3312 return FALSE
; /* Special interface binding is only necessary for UDP datagrams. */
3313 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
3315 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
3316 if (adapters
== NULL
|| GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
3318 /* Search the IPv4 adapter list for the appropriate binding interface */
3319 for (adapter
= adapters
; adapter
!= NULL
; adapter
= adapter
->Next
)
3321 in_addr_t adapter_addr
= (in_addr_t
) inet_addr(adapter
->IpAddressList
.IpAddress
.String
);
3323 if (bind_addr
== adapter_addr
)
3325 #if defined(IP_BOUND_IF)
3326 /* IP_BOUND_IF sets both the incoming and outgoing restriction at once */
3327 if (setsockopt(fd
, IPPROTO_IP
, IP_BOUND_IF
, &adapter
->Index
, sizeof(adapter
->Index
)) != 0)
3330 #elif defined(LINUX_BOUND_IF)
3331 in_addr_t ifindex
= (in_addr_t
) htonl(adapter
->Index
);
3332 struct interface_filter specific_interface_filter
;
3333 struct sock_fprog filter_prog
;
3335 if (setsockopt(fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
)) != 0)
3336 goto cleanup
; /* Failed to suggest egress interface */
3337 specific_interface_filter
= generic_interface_filter
;
3338 specific_interface_filter
.iface_rule
.k
= adapter
->Index
;
3339 specific_interface_filter
.ip_rule
.k
= htonl(adapter_addr
);
3340 filter_prog
.len
= sizeof(generic_interface_filter
)/sizeof(struct sock_filter
);
3341 filter_prog
.filter
= (struct sock_filter
*) &specific_interface_filter
;
3342 if (setsockopt(fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
)) != 0)
3343 goto cleanup
; /* Failed to specify incoming packet filter */
3346 FIXME("Broadcast packets on interface-bound sockets are not currently supported on this platform, "
3347 "receiving broadcast packets will not work on socket %04lx.\n", s
);
3352 /* Will soon be switching to INADDR_ANY: permit address reuse */
3353 if (ret
&& setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
)) == 0)
3354 TRACE("Socket %04lx bound to interface index %d\n", s
, adapter
->Index
);
3360 ERR("Failed to bind to interface, receiving broadcast packets will not work on socket %04lx.\n", s
);
3361 HeapFree(GetProcessHeap(), 0, adapters
);
3365 /***********************************************************************
3368 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
3370 int fd
= get_sock_fd( s
, 0, NULL
);
3371 int res
= SOCKET_ERROR
;
3373 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
3377 if (!name
|| (name
->sa_family
&& !supported_pf(name
->sa_family
)))
3379 SetLastError(WSAEAFNOSUPPORT
);
3383 union generic_unix_sockaddr uaddr
;
3384 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
3387 SetLastError(WSAEFAULT
);
3391 if (name
->sa_family
== WS_AF_INET
)
3393 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
3394 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
3396 /* Trying to bind to the default host interface, using
3397 * INADDR_ANY instead*/
3398 WARN("Trying to bind to magic IP address, using "
3399 "INADDR_ANY instead.\n");
3400 in4
->sin_addr
.s_addr
= htonl(INADDR_ANY
);
3402 else if (interface_bind(s
, fd
, &uaddr
.addr
))
3403 in4
->sin_addr
.s_addr
= htonl(INADDR_ANY
);
3405 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
3407 int loc_errno
= errno
;
3408 WARN("\tfailure - errno = %i\n", errno
);
3413 SetLastError(WSAEINVAL
);
3418 socklen_t optlen
= sizeof(optval
);
3419 /* Windows >= 2003 will return different results depending on
3420 * SO_REUSEADDR, WSAEACCES may be returned representing that
3421 * the socket hijacking protection prevented the bind */
3422 if (!getsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&optval
, &optlen
) && optval
)
3424 SetLastError(WSAEACCES
);
3430 SetLastError(wsaErrno());
3436 res
=0; /* success */
3440 release_sock_fd( s
, fd
);
3445 /***********************************************************************
3446 * closesocket (WS2_32.3)
3448 int WINAPI
WS_closesocket(SOCKET s
)
3450 int res
= SOCKET_ERROR
, fd
;
3453 fd
= get_sock_fd(s
, FILE_READ_DATA
, NULL
);
3456 release_sock_fd(s
, fd
);
3457 if (CloseHandle(SOCKET2HANDLE(s
)))
3461 SetLastError(WSAENOTSOCK
);
3464 SetLastError(WSANOTINITIALISED
);
3465 TRACE("(socket %04lx) -> %d\n", s
, res
);
3469 static int do_connect(int fd
, const struct WS_sockaddr
* name
, int namelen
)
3471 union generic_unix_sockaddr uaddr
;
3472 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
3477 if (name
->sa_family
== WS_AF_INET
)
3479 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
3480 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
3482 /* Trying to connect to magic replace-loopback address,
3483 * assuming we really want to connect to localhost */
3484 TRACE("Trying to connect to magic IP address, using "
3485 "INADDR_LOOPBACK instead.\n");
3486 in4
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
3490 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
3496 /***********************************************************************
3497 * connect (WS2_32.4)
3499 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
3501 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
3503 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
3508 int ret
= do_connect(fd
, name
, namelen
);
3510 goto connect_success
;
3512 if (ret
== WSAEINPROGRESS
)
3514 /* tell wineserver that a connection is in progress */
3515 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3517 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
3518 ret
= sock_is_blocking( s
, &is_blocking
);
3523 do_block(fd
, POLLIN
| POLLOUT
, -1);
3524 _sync_sock_state(s
); /* let wineserver notice connection */
3525 /* retrieve any error codes from it */
3526 if (!(ret
= get_sock_error(s
, FD_CONNECT_BIT
))) goto connect_success
;
3528 else ret
= WSAEWOULDBLOCK
;
3531 release_sock_fd( s
, fd
);
3534 return SOCKET_ERROR
;
3537 release_sock_fd( s
, fd
);
3538 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3539 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
3540 FD_CONNECT
|FD_WINE_LISTENING
);
3541 TRACE("\tconnected %04lx\n", s
);
3545 /***********************************************************************
3546 * WSAConnect (WS2_32.30)
3548 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
3549 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
3550 LPQOS lpSQOS
, LPQOS lpGQOS
)
3552 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
3553 FIXME("unsupported parameters!\n");
3554 return WS_connect( s
, name
, namelen
);
3557 /***********************************************************************
3560 static BOOL WINAPI
WS2_ConnectEx(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
3561 PVOID sendBuf
, DWORD sendBufLen
, LPDWORD sent
, LPOVERLAPPED ov
)
3563 int fd
, ret
, status
;
3567 SetLastError( ERROR_INVALID_PARAMETER
);
3571 fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
3574 SetLastError( WSAENOTSOCK
);
3578 TRACE("socket %04lx, ptr %p %s, length %d, sendptr %p, len %d, ov %p\n",
3579 s
, name
, debugstr_sockaddr(name
), namelen
, sendBuf
, sendBufLen
, ov
);
3581 ret
= is_fd_bound(fd
, NULL
, NULL
);
3584 SetLastError(ret
== -1 ? wsaErrno() : WSAEINVAL
);
3585 release_sock_fd( s
, fd
);
3589 ret
= do_connect(fd
, name
, namelen
);
3594 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3595 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
3596 FD_CONNECT
|FD_WINE_LISTENING
);
3598 wsabuf
.len
= sendBufLen
;
3599 wsabuf
.buf
= (char*) sendBuf
;
3601 /* WSASend takes care of completion if need be */
3602 if (WSASend(s
, &wsabuf
, sendBuf
? 1 : 0, sent
, 0, ov
, NULL
) != SOCKET_ERROR
)
3603 goto connection_success
;
3605 else if (ret
== WSAEINPROGRESS
)
3607 struct ws2_async
*wsa
;
3608 ULONG_PTR cvalue
= (((ULONG_PTR
)ov
->hEvent
& 1) == 0) ? (ULONG_PTR
)ov
: 0;
3610 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
3612 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
3614 /* Indirectly call WSASend */
3615 if (!(wsa
= (struct ws2_async
*)alloc_async_io( offsetof( struct ws2_async
, iovec
[1] ), WS2_async_send
)))
3617 SetLastError(WSAEFAULT
);
3621 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)ov
;
3622 iosb
->u
.Status
= STATUS_PENDING
;
3623 iosb
->Information
= 0;
3625 wsa
->hSocket
= SOCKET2HANDLE(s
);
3627 wsa
->addrlen
.val
= 0;
3629 wsa
->lpFlags
= &wsa
->flags
;
3630 wsa
->control
= NULL
;
3631 wsa
->n_iovecs
= sendBuf
? 1 : 0;
3632 wsa
->first_iovec
= 0;
3633 wsa
->completion_func
= NULL
;
3634 wsa
->iovec
[0].iov_base
= sendBuf
;
3635 wsa
->iovec
[0].iov_len
= sendBufLen
;
3637 status
= register_async( ASYNC_TYPE_WRITE
, wsa
->hSocket
, &wsa
->io
, ov
->hEvent
,
3638 NULL
, (void *)cvalue
, iosb
);
3639 if (status
!= STATUS_PENDING
) HeapFree(GetProcessHeap(), 0, wsa
);
3641 /* If the connect already failed */
3642 if (status
== STATUS_PIPE_DISCONNECTED
)
3644 ov
->Internal
= sock_error_to_ntstatus( get_sock_error( s
, FD_CONNECT_BIT
));
3645 ov
->InternalHigh
= 0;
3646 if (cvalue
) WS_AddCompletion( s
, cvalue
, ov
->Internal
, ov
->InternalHigh
, FALSE
);
3647 if (ov
->hEvent
) NtSetEvent( ov
->hEvent
, NULL
);
3648 status
= STATUS_PENDING
;
3650 SetLastError( NtStatusToWSAError(status
) );
3658 release_sock_fd( s
, fd
);
3662 release_sock_fd( s
, fd
);
3666 /***********************************************************************
3669 static BOOL WINAPI
WS2_DisconnectEx( SOCKET s
, LPOVERLAPPED ov
, DWORD flags
, DWORD reserved
)
3671 TRACE( "socket %04lx, ov %p, flags 0x%x, reserved 0x%x\n", s
, ov
, flags
, reserved
);
3673 if (flags
& TF_REUSE_SOCKET
)
3674 FIXME( "Reusing socket not supported yet\n" );
3678 ov
->Internal
= STATUS_PENDING
;
3679 ov
->InternalHigh
= 0;
3682 return !WS_shutdown( s
, SD_BOTH
);
3685 /***********************************************************************
3686 * getpeername (WS2_32.5)
3688 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
3693 TRACE("socket %04lx, ptr %p, len %08x\n", s
, name
, namelen
? *namelen
: 0);
3695 fd
= get_sock_fd( s
, 0, NULL
);
3700 union generic_unix_sockaddr uaddr
;
3701 socklen_t uaddrlen
= sizeof(uaddr
);
3703 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) == 0)
3705 if (!name
|| !namelen
)
3706 SetLastError(WSAEFAULT
);
3707 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
3708 /* The buffer was too small */
3709 SetLastError(WSAEFAULT
);
3713 TRACE("=> %s\n", debugstr_sockaddr(name
));
3717 SetLastError(wsaErrno());
3718 release_sock_fd( s
, fd
);
3723 /* When binding to an UDP address with filter support the getsockname call on the socket
3724 * will always return 0.0.0.0 instead of the filtered interface address. This function
3725 * checks if the socket is interface-bound on UDP and return the correct address.
3726 * This is required because applications often do a bind() with port zero followed by a
3727 * getsockname() to retrieve the port and address acquired.
3729 static void interface_bind_check(int fd
, struct sockaddr_in
*addr
)
3731 #if !defined(IP_BOUND_IF) && !defined(LINUX_BOUND_IF)
3734 unsigned int ifindex
;
3738 /* Check for IPv4, address 0.0.0.0 and UDP socket */
3739 if (addr
->sin_family
!= AF_INET
|| addr
->sin_addr
.s_addr
!= 0)
3741 if (_get_fd_type(fd
) != SOCK_DGRAM
)
3744 len
= sizeof(ifindex
);
3745 #if defined(IP_BOUND_IF)
3746 ret
= getsockopt(fd
, IPPROTO_IP
, IP_BOUND_IF
, &ifindex
, &len
);
3747 #elif defined(LINUX_BOUND_IF)
3748 ret
= getsockopt(fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, &len
);
3749 if (!ret
) ifindex
= ntohl(ifindex
);
3753 PIP_ADAPTER_INFO adapters
, adapter
;
3756 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
3758 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
3759 if (adapters
&& GetAdaptersInfo(adapters
, &adap_size
) == NO_ERROR
)
3761 /* Search the IPv4 adapter list for the appropriate bound interface */
3762 for (adapter
= adapters
; adapter
!= NULL
; adapter
= adapter
->Next
)
3764 in_addr_t adapter_addr
;
3765 if (adapter
->Index
!= ifindex
) continue;
3767 adapter_addr
= inet_addr(adapter
->IpAddressList
.IpAddress
.String
);
3768 addr
->sin_addr
.s_addr
= adapter_addr
;
3769 TRACE("reporting interface address from adapter %d\n", ifindex
);
3773 HeapFree(GetProcessHeap(), 0, adapters
);
3778 /***********************************************************************
3779 * getsockname (WS2_32.6)
3781 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
3786 TRACE("socket %04lx, ptr %p, len %08x\n", s
, name
, namelen
? *namelen
: 0);
3788 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
3789 if( (name
== NULL
) || (namelen
== NULL
) )
3791 SetLastError( WSAEFAULT
);
3792 return SOCKET_ERROR
;
3795 fd
= get_sock_fd( s
, 0, NULL
);
3800 union generic_unix_sockaddr uaddr
;
3802 int bound
= is_fd_bound(fd
, &uaddr
, &uaddrlen
);
3806 SetLastError(bound
== -1 ? wsaErrno() : WSAEINVAL
);
3808 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
3810 /* The buffer was too small */
3811 SetLastError(WSAEFAULT
);
3815 interface_bind_check(fd
, (struct sockaddr_in
*) &uaddr
);
3816 if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
3818 /* The buffer was too small */
3819 SetLastError(WSAEFAULT
);
3824 TRACE("=> %s\n", debugstr_sockaddr(name
));
3827 release_sock_fd( s
, fd
);
3832 /***********************************************************************
3833 * getsockopt (WS2_32.7)
3835 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
3836 INT optname
, char *optval
, INT
*optlen
)
3841 TRACE("(socket %04lx, %s, optval %s, optlen %p (%d))\n", s
,
3842 debugstr_sockopt(level
, optname
), debugstr_optval(optval
, 0),
3843 optlen
, optlen
? *optlen
: 0);
3851 /* Handle common cases. The special cases are below, sorted
3853 case WS_SO_BROADCAST
:
3855 case WS_SO_KEEPALIVE
:
3856 case WS_SO_OOBINLINE
:
3858 case WS_SO_REUSEADDR
:
3860 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3861 return SOCKET_ERROR
;
3862 convert_sockopt(&level
, &optname
);
3863 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
3865 SetLastError(wsaErrno());
3868 release_sock_fd( s
, fd
);
3870 case WS_SO_ACCEPTCONN
:
3871 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3872 return SOCKET_ERROR
;
3873 if (getsockopt(fd
, SOL_SOCKET
, SO_ACCEPTCONN
, optval
, (socklen_t
*)optlen
) != 0 )
3875 SetLastError(wsaErrno());
3880 /* BSD returns != 0 while Windows return exact == 1 */
3881 if (*(int *)optval
) *(int *)optval
= 1;
3883 release_sock_fd( s
, fd
);
3885 case WS_SO_BSP_STATE
:
3887 int req_size
, addr_size
;
3888 WSAPROTOCOL_INFOW infow
;
3889 CSADDR_INFO
*csinfo
;
3891 ret
= ws_protocol_info(s
, TRUE
, &infow
, &addr_size
);
3894 if (infow
.iAddressFamily
== WS_AF_INET
)
3895 addr_size
= sizeof(struct sockaddr_in
);
3896 else if (infow
.iAddressFamily
== WS_AF_INET6
)
3897 addr_size
= sizeof(struct sockaddr_in6
);
3900 FIXME("Family %d is unsupported for SO_BSP_STATE\n", infow
.iAddressFamily
);
3901 SetLastError(WSAEAFNOSUPPORT
);
3902 return SOCKET_ERROR
;
3905 req_size
= sizeof(CSADDR_INFO
) + addr_size
* 2;
3906 if (*optlen
< req_size
)
3909 SetLastError(WSAEFAULT
);
3913 union generic_unix_sockaddr uaddr
;
3916 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3917 return SOCKET_ERROR
;
3919 csinfo
= (CSADDR_INFO
*) optval
;
3921 /* Check if the sock is bound */
3922 if (is_fd_bound(fd
, &uaddr
, &uaddrlen
) == 1)
3924 csinfo
->LocalAddr
.lpSockaddr
=
3925 (LPSOCKADDR
) (optval
+ sizeof(CSADDR_INFO
));
3926 ws_sockaddr_u2ws(&uaddr
.addr
, csinfo
->LocalAddr
.lpSockaddr
, &addr_size
);
3927 csinfo
->LocalAddr
.iSockaddrLength
= addr_size
;
3931 csinfo
->LocalAddr
.lpSockaddr
= NULL
;
3932 csinfo
->LocalAddr
.iSockaddrLength
= 0;
3935 /* Check if the sock is connected */
3936 if (!getpeername(fd
, &uaddr
.addr
, &uaddrlen
) &&
3937 is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
3939 csinfo
->RemoteAddr
.lpSockaddr
=
3940 (LPSOCKADDR
) (optval
+ sizeof(CSADDR_INFO
) + addr_size
);
3941 ws_sockaddr_u2ws(&uaddr
.addr
, csinfo
->RemoteAddr
.lpSockaddr
, &addr_size
);
3942 csinfo
->RemoteAddr
.iSockaddrLength
= addr_size
;
3946 csinfo
->RemoteAddr
.lpSockaddr
= NULL
;
3947 csinfo
->RemoteAddr
.iSockaddrLength
= 0;
3950 csinfo
->iSocketType
= infow
.iSocketType
;
3951 csinfo
->iProtocol
= infow
.iProtocol
;
3952 release_sock_fd( s
, fd
);
3955 return ret
? 0 : SOCKET_ERROR
;
3957 case WS_SO_DONTLINGER
:
3959 struct linger lingval
;
3960 socklen_t len
= sizeof(struct linger
);
3962 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
3964 SetLastError(WSAEFAULT
);
3965 return SOCKET_ERROR
;
3967 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3968 return SOCKET_ERROR
;
3970 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
3972 SetLastError(wsaErrno());
3977 *(BOOL
*)optval
= !lingval
.l_onoff
;
3978 *optlen
= sizeof(BOOL
);
3981 release_sock_fd( s
, fd
);
3985 case WS_SO_CONNECT_TIME
:
3987 static int pretendtime
= 0;
3988 struct WS_sockaddr addr
;
3989 int len
= sizeof(addr
);
3991 if (!optlen
|| *optlen
< sizeof(DWORD
) || !optval
)
3993 SetLastError(WSAEFAULT
);
3994 return SOCKET_ERROR
;
3996 if (WS_getpeername(s
, &addr
, &len
) == SOCKET_ERROR
)
3997 *(DWORD
*)optval
= ~0u;
4000 if (!pretendtime
) FIXME("WS_SO_CONNECT_TIME - faking results\n");
4001 *(DWORD
*)optval
= pretendtime
++;
4003 *optlen
= sizeof(DWORD
);
4006 /* As mentioned in setsockopt, Windows ignores this, so we
4007 * always return true here */
4008 case WS_SO_DONTROUTE
:
4009 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
4011 SetLastError(WSAEFAULT
);
4012 return SOCKET_ERROR
;
4014 *(BOOL
*)optval
= TRUE
;
4015 *optlen
= sizeof(BOOL
);
4020 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4021 return SOCKET_ERROR
;
4022 if (getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, optval
, (socklen_t
*)optlen
) != 0 )
4024 SetLastError(wsaErrno());
4027 release_sock_fd( s
, fd
);
4029 /* The wineserver may have swallowed the error before us */
4030 if (!ret
&& *(int*) optval
== 0)
4032 int i
, events
[FD_MAX_EVENTS
];
4033 _get_sock_errors(s
, events
);
4034 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
4038 TRACE("returning SO_ERROR %d from wine server\n", events
[i
]);
4039 *(int*) optval
= events
[i
];
4049 struct linger lingval
;
4050 socklen_t len
= sizeof(struct linger
);
4052 /* struct linger and LINGER have different sizes */
4053 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
4055 SetLastError(WSAEFAULT
);
4056 return SOCKET_ERROR
;
4058 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4059 return SOCKET_ERROR
;
4061 if (_get_fd_type(fd
) == SOCK_DGRAM
)
4063 SetLastError(WSAENOPROTOOPT
);
4066 else if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0)
4068 SetLastError(wsaErrno());
4073 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
4074 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
4075 *optlen
= sizeof(struct linger
);
4078 release_sock_fd( s
, fd
);
4082 case WS_SO_MAX_MSG_SIZE
:
4083 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
4085 SetLastError(WSAEFAULT
);
4086 return SOCKET_ERROR
;
4088 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
4089 *(int *)optval
= 65507;
4090 *optlen
= sizeof(int);
4093 /* SO_OPENTYPE does not require a valid socket handle. */
4094 case WS_SO_OPENTYPE
:
4095 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
4097 SetLastError(WSAEFAULT
);
4098 return SOCKET_ERROR
;
4100 *(int *)optval
= get_per_thread_data()->opentype
;
4101 *optlen
= sizeof(int);
4102 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
4104 case WS_SO_PROTOCOL_INFOA
:
4105 case WS_SO_PROTOCOL_INFOW
:
4108 WSAPROTOCOL_INFOW infow
;
4110 ret
= ws_protocol_info(s
, optname
== WS_SO_PROTOCOL_INFOW
, &infow
, &size
);
4113 if (!optlen
|| !optval
|| *optlen
< size
)
4115 if(optlen
) *optlen
= size
;
4117 SetLastError(WSAEFAULT
);
4120 memcpy(optval
, &infow
, size
);
4122 return ret
? 0 : SOCKET_ERROR
;
4124 case WS_SO_RCVTIMEO
:
4125 case WS_SO_SNDTIMEO
:
4129 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
4131 SetLastError(WSAEFAULT
);
4132 return SOCKET_ERROR
;
4134 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4135 return SOCKET_ERROR
;
4137 timeout
= get_rcvsnd_timeo(fd
, optname
== WS_SO_RCVTIMEO
);
4138 *(int *)optval
= timeout
<= UINT_MAX
? timeout
: UINT_MAX
;
4140 release_sock_fd( s
, fd
);
4146 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
4148 SetLastError(WSAEFAULT
);
4149 return SOCKET_ERROR
;
4151 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4152 return SOCKET_ERROR
;
4154 sock_type
= _get_fd_type(fd
);
4155 if (sock_type
== -1)
4157 SetLastError(wsaErrno());
4161 (*(int *)optval
) = convert_socktype_u2w(sock_type
);
4163 release_sock_fd( s
, fd
);
4167 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
4168 SetLastError(WSAENOPROTOOPT
);
4169 return SOCKET_ERROR
;
4170 } /* end switch(optname) */
4171 }/* end case WS_SOL_SOCKET */
4173 case WS_NSPROTO_IPX
:
4175 struct WS_sockaddr_ipx addr
;
4176 IPX_ADDRESS_DATA
*data
;
4181 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
4183 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (socklen_t
*)optlen
) == -1)
4190 socklen_t len
=sizeof(struct ipx
);
4191 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
4194 *optval
= (int)val
.ipx_pt
;
4197 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
4198 release_sock_fd( s
, fd
);
4201 case WS_IPX_ADDRESS
:
4203 * On a Win2000 system with one network card there are usually
4204 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
4205 * Using this call you can then retrieve info about this all.
4206 * In case of Linux it is a bit different. Usually you have
4207 * only "one" device active and further it is not possible to
4208 * query things like the linkspeed.
4210 FIXME("IPX_ADDRESS\n");
4211 namelen
= sizeof(struct WS_sockaddr_ipx
);
4212 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
4213 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
4215 data
= (IPX_ADDRESS_DATA
*)optval
;
4216 memcpy(data
->nodenum
,addr
.sa_nodenum
,sizeof(data
->nodenum
));
4217 memcpy(data
->netnum
,addr
.sa_netnum
,sizeof(data
->netnum
));
4218 data
->adapternum
= 0;
4219 data
->wan
= FALSE
; /* We are not on a wan for now .. */
4220 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
4221 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
4222 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
4223 * note 1MB = 1000kB in this case */
4226 case WS_IPX_MAX_ADAPTER_NUM
:
4227 FIXME("IPX_MAX_ADAPTER_NUM\n");
4228 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
4232 FIXME("IPX optname:%x\n", optname
);
4233 return SOCKET_ERROR
;
4234 }/* end switch(optname) */
4235 } /* end case WS_NSPROTO_IPX */
4239 #define MAX_IRDA_DEVICES 10
4244 case WS_IRLMP_ENUMDEVICES
:
4246 char buf
[sizeof(struct irda_device_list
) +
4247 (MAX_IRDA_DEVICES
- 1) * sizeof(struct irda_device_info
)];
4249 socklen_t len
= sizeof(buf
);
4251 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4252 return SOCKET_ERROR
;
4253 res
= getsockopt( fd
, SOL_IRLMP
, IRLMP_ENUMDEVICES
, buf
, &len
);
4254 release_sock_fd( s
, fd
);
4257 SetLastError(wsaErrno());
4258 return SOCKET_ERROR
;
4262 struct irda_device_list
*src
= (struct irda_device_list
*)buf
;
4263 DEVICELIST
*dst
= (DEVICELIST
*)optval
;
4264 INT needed
= sizeof(DEVICELIST
);
4268 needed
+= (src
->len
- 1) * sizeof(IRDA_DEVICE_INFO
);
4269 if (*optlen
< needed
)
4271 SetLastError(WSAEFAULT
);
4272 return SOCKET_ERROR
;
4275 TRACE("IRLMP_ENUMDEVICES: %d devices found:\n", src
->len
);
4276 dst
->numDevice
= src
->len
;
4277 for (i
= 0; i
< src
->len
; i
++)
4279 TRACE("saddr = %08x, daddr = %08x, info = %s, hints = %02x%02x\n",
4280 src
->dev
[i
].saddr
, src
->dev
[i
].daddr
,
4281 src
->dev
[i
].info
, src
->dev
[i
].hints
[0],
4282 src
->dev
[i
].hints
[1]);
4283 memcpy( dst
->Device
[i
].irdaDeviceID
,
4285 sizeof(dst
->Device
[i
].irdaDeviceID
) ) ;
4286 memcpy( dst
->Device
[i
].irdaDeviceName
,
4288 sizeof(dst
->Device
[i
].irdaDeviceName
) ) ;
4289 memcpy( &dst
->Device
[i
].irdaDeviceHints1
,
4290 &src
->dev
[i
].hints
[0],
4291 sizeof(dst
->Device
[i
].irdaDeviceHints1
) ) ;
4292 memcpy( &dst
->Device
[i
].irdaDeviceHints2
,
4293 &src
->dev
[i
].hints
[1],
4294 sizeof(dst
->Device
[i
].irdaDeviceHints2
) ) ;
4295 dst
->Device
[i
].irdaCharSet
= src
->dev
[i
].charset
;
4301 FIXME("IrDA optname:0x%x\n", optname
);
4302 return SOCKET_ERROR
;
4304 break; /* case WS_SOL_IRLMP */
4305 #undef MAX_IRDA_DEVICES
4308 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
4309 case WS_IPPROTO_TCP
:
4312 case WS_TCP_NODELAY
:
4313 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4314 return SOCKET_ERROR
;
4315 convert_sockopt(&level
, &optname
);
4316 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
4318 SetLastError(wsaErrno());
4321 release_sock_fd( s
, fd
);
4324 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
4325 return SOCKET_ERROR
;
4330 case WS_IP_ADD_MEMBERSHIP
:
4331 case WS_IP_DROP_MEMBERSHIP
:
4335 case WS_IP_MULTICAST_IF
:
4336 case WS_IP_MULTICAST_LOOP
:
4337 case WS_IP_MULTICAST_TTL
:
4339 #if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
4344 #ifdef IP_UNICAST_IF
4345 case WS_IP_UNICAST_IF
:
4347 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4348 return SOCKET_ERROR
;
4349 convert_sockopt(&level
, &optname
);
4350 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
4352 SetLastError(wsaErrno());
4355 release_sock_fd( s
, fd
);
4357 case WS_IP_DONTFRAGMENT
:
4358 return get_dont_fragment(s
, IPPROTO_IP
, (BOOL
*)optval
) ? 0 : SOCKET_ERROR
;
4360 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
4361 return SOCKET_ERROR
;
4363 case WS_IPPROTO_IPV6
:
4366 #ifdef IPV6_ADD_MEMBERSHIP
4367 case WS_IPV6_ADD_MEMBERSHIP
:
4369 #ifdef IPV6_DROP_MEMBERSHIP
4370 case WS_IPV6_DROP_MEMBERSHIP
:
4372 case WS_IPV6_MULTICAST_IF
:
4373 case WS_IPV6_MULTICAST_HOPS
:
4374 case WS_IPV6_MULTICAST_LOOP
:
4375 case WS_IPV6_UNICAST_HOPS
:
4376 case WS_IPV6_V6ONLY
:
4377 #ifdef IPV6_UNICAST_IF
4378 case WS_IPV6_UNICAST_IF
:
4380 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
4381 return SOCKET_ERROR
;
4382 convert_sockopt(&level
, &optname
);
4383 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
4385 SetLastError(wsaErrno());
4388 release_sock_fd( s
, fd
);
4390 case WS_IPV6_DONTFRAG
:
4391 return get_dont_fragment(s
, IPPROTO_IPV6
, (BOOL
*)optval
) ? 0 : SOCKET_ERROR
;
4393 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
4394 return SOCKET_ERROR
;
4397 WARN("Unknown level: 0x%08x\n", level
);
4398 SetLastError(WSAEINVAL
);
4399 return SOCKET_ERROR
;
4400 } /* end switch(level) */
4403 /***********************************************************************
4406 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
4408 return htonl(hostlong
);
4412 /***********************************************************************
4415 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
4417 return htons(hostshort
);
4420 /***********************************************************************
4421 * WSAHtonl (WS2_32.46)
4422 * From MSDN description of error codes, this function should also
4423 * check if WinSock has been initialized and the socket is a valid
4424 * socket. But why? This function only translates a host byte order
4425 * u_long into a network byte order u_long...
4427 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
4431 *lpnetlong
= htonl(hostlong
);
4434 SetLastError(WSAEFAULT
);
4435 return SOCKET_ERROR
;
4438 /***********************************************************************
4439 * WSAHtons (WS2_32.47)
4440 * From MSDN description of error codes, this function should also
4441 * check if WinSock has been initialized and the socket is a valid
4442 * socket. But why? This function only translates a host byte order
4443 * u_short into a network byte order u_short...
4445 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
4450 *lpnetshort
= htons(hostshort
);
4453 SetLastError(WSAEFAULT
);
4454 return SOCKET_ERROR
;
4458 /***********************************************************************
4459 * inet_addr (WS2_32.11)
4461 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
4463 if (!cp
) return INADDR_NONE
;
4464 return inet_addr(cp
);
4468 /***********************************************************************
4471 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
4473 return ntohl(netlong
);
4477 /***********************************************************************
4480 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
4482 return ntohs(netshort
);
4486 /***********************************************************************
4487 * inet_ntoa (WS2_32.12)
4489 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
4491 unsigned int long_ip
= ntohl(in
.WS_s_addr
);
4492 struct per_thread_data
*data
= get_per_thread_data();
4494 sprintf( data
->ntoa_buffer
, "%u.%u.%u.%u",
4495 (long_ip
>> 24) & 0xff,
4496 (long_ip
>> 16) & 0xff,
4497 (long_ip
>> 8) & 0xff,
4500 return data
->ntoa_buffer
;
4503 static const char *debugstr_wsaioctl(DWORD code
)
4505 const char *name
= NULL
, *buf_type
, *family
;
4507 #define IOCTL_NAME(x) case x: name = #x; break
4510 IOCTL_NAME(WS_FIONBIO
);
4511 IOCTL_NAME(WS_FIONREAD
);
4512 IOCTL_NAME(WS_SIOCATMARK
);
4513 /* IOCTL_NAME(WS_SIO_ACQUIRE_PORT_RESERVATION); */
4514 IOCTL_NAME(WS_SIO_ADDRESS_LIST_CHANGE
);
4515 IOCTL_NAME(WS_SIO_ADDRESS_LIST_QUERY
);
4516 IOCTL_NAME(WS_SIO_ASSOCIATE_HANDLE
);
4517 /* IOCTL_NAME(WS_SIO_ASSOCIATE_PORT_RESERVATION);
4518 IOCTL_NAME(WS_SIO_BASE_HANDLE);
4519 IOCTL_NAME(WS_SIO_BSP_HANDLE);
4520 IOCTL_NAME(WS_SIO_BSP_HANDLE_SELECT);
4521 IOCTL_NAME(WS_SIO_BSP_HANDLE_POLL);
4522 IOCTL_NAME(WS_SIO_CHK_QOS); */
4523 IOCTL_NAME(WS_SIO_ENABLE_CIRCULAR_QUEUEING
);
4524 IOCTL_NAME(WS_SIO_FIND_ROUTE
);
4525 IOCTL_NAME(WS_SIO_FLUSH
);
4526 IOCTL_NAME(WS_SIO_GET_BROADCAST_ADDRESS
);
4527 IOCTL_NAME(WS_SIO_GET_EXTENSION_FUNCTION_POINTER
);
4528 IOCTL_NAME(WS_SIO_GET_GROUP_QOS
);
4529 IOCTL_NAME(WS_SIO_GET_INTERFACE_LIST
);
4530 /* IOCTL_NAME(WS_SIO_GET_INTERFACE_LIST_EX); */
4531 IOCTL_NAME(WS_SIO_GET_QOS
);
4532 /* IOCTL_NAME(WS_SIO_IDEAL_SEND_BACKLOG_CHANGE);
4533 IOCTL_NAME(WS_SIO_IDEAL_SEND_BACKLOG_QUERY); */
4534 IOCTL_NAME(WS_SIO_KEEPALIVE_VALS
);
4535 IOCTL_NAME(WS_SIO_MULTIPOINT_LOOPBACK
);
4536 IOCTL_NAME(WS_SIO_MULTICAST_SCOPE
);
4537 /* IOCTL_NAME(WS_SIO_QUERY_RSS_SCALABILITY_INFO);
4538 IOCTL_NAME(WS_SIO_QUERY_WFP_ALE_ENDPOINT_HANDLE); */
4539 IOCTL_NAME(WS_SIO_RCVALL
);
4540 IOCTL_NAME(WS_SIO_RCVALL_IGMPMCAST
);
4541 IOCTL_NAME(WS_SIO_RCVALL_MCAST
);
4542 /* IOCTL_NAME(WS_SIO_RELEASE_PORT_RESERVATION); */
4543 IOCTL_NAME(WS_SIO_ROUTING_INTERFACE_CHANGE
);
4544 IOCTL_NAME(WS_SIO_ROUTING_INTERFACE_QUERY
);
4545 IOCTL_NAME(WS_SIO_SET_COMPATIBILITY_MODE
);
4546 IOCTL_NAME(WS_SIO_SET_GROUP_QOS
);
4547 IOCTL_NAME(WS_SIO_SET_QOS
);
4548 IOCTL_NAME(WS_SIO_TRANSLATE_HANDLE
);
4549 IOCTL_NAME(WS_SIO_UDP_CONNRESET
);
4556 /* If this is not a known code split its bits */
4557 switch(code
& 0x18000000)
4562 case WS_IOC_PROTOCOL
:
4563 family
= "IOC_PROTOCOL";
4566 family
= "IOC_VENDOR";
4568 default: /* WS_IOC_UNIX */
4570 BYTE size
= (code
>> 16) & WS_IOCPARM_MASK
;
4571 char x
= (code
& 0xff00) >> 8;
4572 BYTE y
= code
& 0xff;
4575 switch (code
& (WS_IOC_VOID
|WS_IOC_INOUT
))
4579 sprintf(args
, "%d, %d", x
, y
);
4583 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
4587 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
4591 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
4594 return wine_dbg_sprintf("%s(%s)", buf_type
, args
);
4598 /* We are different from WS_IOC_UNIX. */
4599 switch (code
& (WS_IOC_VOID
|WS_IOC_INOUT
))
4602 buf_type
= "_WSAIO";
4605 buf_type
= "_WSAIORW";
4608 buf_type
= "_WSAIOW";
4611 buf_type
= "_WSAIOR";
4618 return wine_dbg_sprintf("%s(%s, %d)", buf_type
, family
,
4619 (USHORT
)(code
& 0xffff));
4622 /* do an ioctl call through the server */
4623 static DWORD
server_ioctl_sock( SOCKET s
, DWORD code
, LPVOID in_buff
, DWORD in_size
,
4624 LPVOID out_buff
, DWORD out_size
, LPDWORD ret_size
,
4625 LPWSAOVERLAPPED overlapped
,
4626 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
4628 HANDLE event
= overlapped
? overlapped
->hEvent
: 0;
4629 HANDLE handle
= SOCKET2HANDLE( s
);
4630 struct ws2_async
*wsa
= NULL
;
4631 IO_STATUS_BLOCK
*io
= (PIO_STATUS_BLOCK
)overlapped
, iosb
;
4632 void *cvalue
= NULL
;
4637 if (!(wsa
= (struct ws2_async
*)alloc_async_io( sizeof(*wsa
), NULL
)))
4638 return WSA_NOT_ENOUGH_MEMORY
;
4639 wsa
->hSocket
= handle
;
4640 wsa
->user_overlapped
= overlapped
;
4641 wsa
->completion_func
= completion
;
4642 if (!io
) io
= &wsa
->local_iosb
;
4647 else if (!((ULONG_PTR
)overlapped
->hEvent
& 1))
4648 cvalue
= overlapped
;
4650 status
= NtDeviceIoControlFile( handle
, event
, wsa
? ws2_async_apc
: NULL
, cvalue
, io
, code
,
4651 in_buff
, in_size
, out_buff
, out_size
);
4652 if (status
== STATUS_NOT_SUPPORTED
)
4654 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
4655 code
, code
>> 16, (code
>> 14) & 3, (code
>> 2) & 0xfff, code
& 3);
4657 else if (status
== STATUS_SUCCESS
)
4658 *ret_size
= io
->Information
; /* "Information" is the size written to the output buffer */
4660 if (status
!= STATUS_PENDING
) RtlFreeHeap( GetProcessHeap(), 0, wsa
);
4662 return NtStatusToWSAError( status
);
4665 /**********************************************************************
4666 * WSAIoctl (WS2_32.50)
4669 INT WINAPI
WSAIoctl(SOCKET s
, DWORD code
, LPVOID in_buff
, DWORD in_size
, LPVOID out_buff
,
4670 DWORD out_size
, LPDWORD ret_size
, LPWSAOVERLAPPED overlapped
,
4671 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
4674 DWORD status
= 0, total
= 0;
4676 TRACE("%04lx, %s, %p, %d, %p, %d, %p, %p, %p\n",
4677 s
, debugstr_wsaioctl(code
), in_buff
, in_size
, out_buff
, out_size
, ret_size
, overlapped
, completion
);
4682 if (in_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(in_buff
))
4684 SetLastError(WSAEFAULT
);
4685 return SOCKET_ERROR
;
4687 TRACE("-> FIONBIO (%x)\n", *(WS_u_long
*)in_buff
);
4688 if (_get_sock_mask(s
))
4690 /* AsyncSelect()'ed sockets are always nonblocking */
4691 if (!*(WS_u_long
*)in_buff
) status
= WSAEINVAL
;
4694 if (*(WS_u_long
*)in_buff
)
4695 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
4697 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
4704 socklen_t len
= sizeof(listening
);
4706 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
4708 SetLastError(WSAEFAULT
);
4709 return SOCKET_ERROR
;
4711 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
4714 /* On Linux, FIONREAD on listening socket always fails (see tcp(7)).
4715 However, it succeeds on native. */
4716 if (!getsockopt( fd
, SOL_SOCKET
, SO_ACCEPTCONN
, &listening
, &len
) && listening
)
4717 (*(WS_u_long
*) out_buff
) = 0;
4720 if (ioctl(fd
, FIONREAD
, out_buff
) == -1)
4721 status
= wsaErrno();
4722 release_sock_fd( s
, fd
);
4728 unsigned int oob
= 0, atmark
= 0;
4729 socklen_t oobsize
= sizeof(int);
4730 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
4732 SetLastError(WSAEFAULT
);
4733 return SOCKET_ERROR
;
4735 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
4736 /* SO_OOBINLINE sockets must always return TRUE to SIOCATMARK */
4737 if ((getsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &oob
, &oobsize
) == -1)
4738 || (!oob
&& ioctl(fd
, SIOCATMARK
, &atmark
) == -1))
4739 status
= wsaErrno();
4742 /* The SIOCATMARK value read from ioctl() is reversed
4743 * because BSD returns TRUE if it's in the OOB mark
4744 * while Windows returns TRUE if there are NO OOB bytes.
4746 (*(WS_u_long
*) out_buff
) = oob
|| !atmark
;
4749 release_sock_fd( s
, fd
);
4754 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
4755 SetLastError(WSAEINVAL
);
4756 return SOCKET_ERROR
;
4758 case WS_SIO_GET_INTERFACE_LIST
:
4760 INTERFACE_INFO
* intArray
= out_buff
;
4761 DWORD size
, numInt
= 0, apiReturn
;
4763 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
4765 if (!out_buff
|| !ret_size
)
4767 SetLastError(WSAEFAULT
);
4768 return SOCKET_ERROR
;
4771 fd
= get_sock_fd( s
, 0, NULL
);
4772 if (fd
== -1) return SOCKET_ERROR
;
4774 apiReturn
= GetAdaptersInfo(NULL
, &size
);
4775 if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
4777 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
4781 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
4783 PIP_ADAPTER_INFO ptr
;
4785 for (ptr
= table
, numInt
= 0; ptr
; ptr
= ptr
->Next
)
4787 unsigned int addr
, mask
, bcast
;
4788 struct ifreq ifInfo
;
4790 /* Skip interfaces without an IPv4 address. */
4791 if (ptr
->IpAddressList
.IpAddress
.String
[0] == '\0')
4794 if ((numInt
+ 1)*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > out_size
)
4796 WARN("Buffer too small = %u, out_size = %u\n", numInt
+ 1, out_size
);
4801 /* Socket Status Flags */
4802 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
4803 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
4805 ERR("Error obtaining status flags for socket!\n");
4811 /* set flags; the values of IFF_* are not the same
4812 under Linux and Windows, therefore must generate
4814 intArray
->iiFlags
= 0;
4815 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
4816 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
4817 #ifdef IFF_POINTOPOINT
4818 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
4819 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
4821 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
4822 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
4823 if (ifInfo
.ifr_flags
& IFF_UP
)
4824 intArray
->iiFlags
|= WS_IFF_UP
;
4825 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
4826 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
4829 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
4830 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
4831 bcast
= addr
| ~mask
;
4832 intArray
->iiAddress
.AddressIn
.sin_family
= WS_AF_INET
;
4833 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
4834 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
= addr
;
4836 intArray
->iiNetmask
.AddressIn
.sin_family
= WS_AF_INET
;
4837 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
4838 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
= mask
;
4840 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
= WS_AF_INET
;
4841 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
4842 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.WS_s_addr
= bcast
;
4849 ERR("Unable to get interface table!\n");
4852 HeapFree(GetProcessHeap(),0,table
);
4854 else status
= WSAEINVAL
;
4856 else if (apiReturn
!= ERROR_NO_DATA
)
4858 ERR("Unable to get interface table!\n");
4861 /* Calculate the size of the array being returned */
4862 total
= sizeof(INTERFACE_INFO
) * numInt
;
4863 release_sock_fd( s
, fd
);
4867 case WS_SIO_ADDRESS_LIST_QUERY
:
4871 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
4875 SetLastError(WSAEFAULT
);
4876 return SOCKET_ERROR
;
4879 if (out_size
&& out_size
< FIELD_OFFSET(SOCKET_ADDRESS_LIST
, Address
[0]))
4882 SetLastError(WSAEINVAL
);
4883 return SOCKET_ERROR
;
4886 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
4888 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
4889 SOCKET_ADDRESS_LIST
*sa_list
;
4890 SOCKADDR_IN
*sockaddr
;
4895 if (!table
|| GetAdaptersInfo(table
, &size
))
4897 HeapFree(GetProcessHeap(), 0, table
);
4902 for (p
= table
, num
= 0; p
; p
= p
->Next
)
4903 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
4905 total
= FIELD_OFFSET(SOCKET_ADDRESS_LIST
, Address
[num
]) + num
* sizeof(*sockaddr
);
4906 if (total
> out_size
|| !out_buff
)
4909 HeapFree(GetProcessHeap(), 0, table
);
4915 sa
= sa_list
->Address
;
4916 sockaddr
= (SOCKADDR_IN
*)&sa
[num
];
4917 sa_list
->iAddressCount
= num
;
4919 for (p
= table
, i
= 0; p
; p
= p
->Next
)
4921 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
4923 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
4924 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
4926 sockaddr
[i
].sin_family
= WS_AF_INET
;
4927 sockaddr
[i
].sin_port
= 0;
4928 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
4932 HeapFree(GetProcessHeap(), 0, table
);
4936 WARN("unable to get IP address list\n");
4943 FIXME("SIO_FLUSH: stub.\n");
4946 case WS_SIO_GET_EXTENSION_FUNCTION_POINTER
:
4948 #define EXTENSION_FUNCTION(x, y) { x, y, #y },
4955 EXTENSION_FUNCTION(WSAID_CONNECTEX
, WS2_ConnectEx
)
4956 EXTENSION_FUNCTION(WSAID_DISCONNECTEX
, WS2_DisconnectEx
)
4957 EXTENSION_FUNCTION(WSAID_ACCEPTEX
, WS2_AcceptEx
)
4958 EXTENSION_FUNCTION(WSAID_GETACCEPTEXSOCKADDRS
, WS2_GetAcceptExSockaddrs
)
4959 EXTENSION_FUNCTION(WSAID_TRANSMITFILE
, WS2_TransmitFile
)
4960 /* EXTENSION_FUNCTION(WSAID_TRANSMITPACKETS, WS2_TransmitPackets) */
4961 EXTENSION_FUNCTION(WSAID_WSARECVMSG
, WS2_WSARecvMsg
)
4962 EXTENSION_FUNCTION(WSAID_WSASENDMSG
, WSASendMsg
)
4964 #undef EXTENSION_FUNCTION
4968 for (i
= 0; i
< ARRAY_SIZE(guid_funcs
); i
++)
4970 if (IsEqualGUID(&guid_funcs
[i
].guid
, in_buff
))
4979 TRACE("-> got %s\n", guid_funcs
[i
].name
);
4980 *(void **)out_buff
= guid_funcs
[i
].func_ptr
;
4981 total
= sizeof(void *);
4985 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER %s: stub\n", debugstr_guid(in_buff
));
4986 status
= WSAEOPNOTSUPP
;
4989 case WS_SIO_KEEPALIVE_VALS
:
4991 struct tcp_keepalive
*k
;
4992 int keepalive
, keepidle
, keepintvl
;
4994 if (!in_buff
|| in_size
< sizeof(struct tcp_keepalive
))
4996 SetLastError(WSAEFAULT
);
4997 return SOCKET_ERROR
;
5001 keepalive
= k
->onoff
? 1 : 0;
5002 keepidle
= max( 1, (k
->keepalivetime
+ 500) / 1000 );
5003 keepintvl
= max( 1, (k
->keepaliveinterval
+ 500) / 1000 );
5005 TRACE("onoff: %d, keepalivetime: %d, keepaliveinterval: %d\n", keepalive
, keepidle
, keepintvl
);
5007 fd
= get_sock_fd(s
, 0, NULL
);
5008 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&keepalive
, sizeof(int)) == -1)
5010 #if defined(TCP_KEEPIDLE) || defined(TCP_KEEPINTVL)
5011 /* these values need to be set only if SO_KEEPALIVE is enabled */
5014 #ifndef TCP_KEEPIDLE
5015 FIXME("ignoring keepalive timeout\n");
5017 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, (void *)&keepidle
, sizeof(int)) == -1)
5021 #ifdef TCP_KEEPINTVL
5022 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, (void *)&keepintvl
, sizeof(int)) == -1)
5025 FIXME("ignoring keepalive interval\n");
5030 FIXME("ignoring keepalive interval and timeout\n");
5032 release_sock_fd(s
, fd
);
5035 case WS_SIO_ROUTING_INTERFACE_QUERY
:
5037 struct WS_sockaddr
*daddr
= (struct WS_sockaddr
*)in_buff
;
5038 struct WS_sockaddr_in
*daddr_in
= (struct WS_sockaddr_in
*)daddr
;
5039 struct WS_sockaddr_in
*saddr_in
= out_buff
;
5040 MIB_IPFORWARDROW row
;
5041 PMIB_IPADDRTABLE ipAddrTable
= NULL
;
5042 DWORD size
, i
, found_index
;
5044 TRACE("-> WS_SIO_ROUTING_INTERFACE_QUERY request\n");
5046 if (!in_buff
|| in_size
< sizeof(struct WS_sockaddr
) ||
5047 !out_buff
|| out_size
< sizeof(struct WS_sockaddr_in
) || !ret_size
)
5049 SetLastError(WSAEFAULT
);
5050 return SOCKET_ERROR
;
5052 if (daddr
->sa_family
!= WS_AF_INET
)
5054 FIXME("unsupported address family %d\n", daddr
->sa_family
);
5055 status
= WSAEAFNOSUPPORT
;
5058 if (GetBestRoute(daddr_in
->sin_addr
.S_un
.S_addr
, 0, &row
) != NOERROR
||
5059 GetIpAddrTable(NULL
, &size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
5064 ipAddrTable
= HeapAlloc(GetProcessHeap(), 0, size
);
5065 if (GetIpAddrTable(ipAddrTable
, &size
, FALSE
))
5067 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
5071 for (i
= 0, found_index
= ipAddrTable
->dwNumEntries
;
5072 i
< ipAddrTable
->dwNumEntries
; i
++)
5074 if (ipAddrTable
->table
[i
].dwIndex
== row
.dwForwardIfIndex
)
5077 if (found_index
== ipAddrTable
->dwNumEntries
)
5079 ERR("no matching IP address for interface %d\n",
5080 row
.dwForwardIfIndex
);
5081 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
5085 saddr_in
->sin_family
= WS_AF_INET
;
5086 saddr_in
->sin_addr
.S_un
.S_addr
= ipAddrTable
->table
[found_index
].dwAddr
;
5087 saddr_in
->sin_port
= 0;
5088 total
= sizeof(struct WS_sockaddr_in
);
5089 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
5092 case WS_SIO_SET_COMPATIBILITY_MODE
:
5093 TRACE("WS_SIO_SET_COMPATIBILITY_MODE ignored\n");
5094 status
= WSAEOPNOTSUPP
;
5096 case WS_SIO_UDP_CONNRESET
:
5097 FIXME("WS_SIO_UDP_CONNRESET stub\n");
5099 case 0x667e: /* Netscape tries hard to use bogus ioctl 0x667e */
5100 SetLastError(WSAEOPNOTSUPP
);
5101 return SOCKET_ERROR
;
5103 status
= WSAEOPNOTSUPP
;
5107 if (status
== WSAEOPNOTSUPP
)
5109 status
= server_ioctl_sock(s
, code
, in_buff
, in_size
, out_buff
, out_size
, &total
,
5110 overlapped
, completion
);
5111 if (status
!= WSAEOPNOTSUPP
)
5113 if (status
== 0 || status
== WSA_IO_PENDING
)
5114 TRACE("-> %s request\n", debugstr_wsaioctl(code
));
5116 ERR("-> %s request failed with status 0x%x\n", debugstr_wsaioctl(code
), status
);
5118 /* overlapped and completion operations will be handled by the server */
5123 FIXME("unsupported WS_IOCTL cmd (%s)\n", debugstr_wsaioctl(code
));
5128 FIXME( "completion routine %p not supported\n", completion
);
5130 else if (overlapped
)
5132 ULONG_PTR cvalue
= (overlapped
&& ((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)overlapped
: 0;
5133 overlapped
->Internal
= sock_error_to_ntstatus( status
);
5134 overlapped
->InternalHigh
= total
;
5135 if (cvalue
) WS_AddCompletion( HANDLE2SOCKET(s
), cvalue
, overlapped
->Internal
, total
, FALSE
);
5136 if (overlapped
->hEvent
) NtSetEvent( overlapped
->hEvent
, NULL
);
5141 if (ret_size
) *ret_size
= total
;
5144 SetLastError( status
);
5145 return SOCKET_ERROR
;
5149 /***********************************************************************
5150 * ioctlsocket (WS2_32.10)
5152 int WINAPI
WS_ioctlsocket(SOCKET s
, LONG cmd
, WS_u_long
*argp
)
5155 return WSAIoctl( s
, cmd
, argp
, sizeof(WS_u_long
), argp
, sizeof(WS_u_long
), &ret_size
, NULL
, NULL
);
5158 /***********************************************************************
5159 * listen (WS2_32.13)
5161 int WINAPI
WS_listen(SOCKET s
, int backlog
)
5163 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
), ret
= SOCKET_ERROR
;
5165 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
5168 int bound
= is_fd_bound(fd
, NULL
, NULL
);
5172 SetLastError(bound
== -1 ? wsaErrno() : WSAEINVAL
);
5174 else if (listen(fd
, backlog
) == 0)
5176 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
5178 FD_CONNECT
|FD_WINE_CONNECTED
);
5182 SetLastError(wsaErrno());
5183 release_sock_fd( s
, fd
);
5186 SetLastError(WSAENOTSOCK
);
5190 /***********************************************************************
5193 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
5195 DWORD n
, dwFlags
= flags
;
5201 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
5202 return SOCKET_ERROR
;
5207 /***********************************************************************
5208 * recvfrom (WS2_32.17)
5210 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
5211 struct WS_sockaddr
*from
, int *fromlen
)
5213 DWORD n
, dwFlags
= flags
;
5219 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
5220 return SOCKET_ERROR
;
5225 /* allocate a poll array for the corresponding fd sets */
5226 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
5227 const WS_fd_set
*exceptfds
, int *count_ptr
)
5229 unsigned int i
, j
= 0, count
= 0;
5231 struct per_thread_data
*ptb
= get_per_thread_data();
5233 if (readfds
) count
+= readfds
->fd_count
;
5234 if (writefds
) count
+= writefds
->fd_count
;
5235 if (exceptfds
) count
+= exceptfds
->fd_count
;
5239 SetLastError(WSAEINVAL
);
5243 /* check if the cache can hold all descriptors, if not do the resizing */
5244 if (ptb
->fd_count
< count
)
5246 if (!(fds
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(fds
[0]))))
5248 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
5251 HeapFree(GetProcessHeap(), 0, ptb
->fd_cache
);
5252 ptb
->fd_cache
= fds
;
5253 ptb
->fd_count
= count
;
5256 fds
= ptb
->fd_cache
;
5259 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
5261 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
5262 if (fds
[j
].fd
== -1) goto failed
;
5264 if (is_fd_bound(fds
[j
].fd
, NULL
, NULL
) == 1)
5266 fds
[j
].events
= POLLIN
;
5270 release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
5276 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
5278 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
5279 if (fds
[j
].fd
== -1) goto failed
;
5281 if (is_fd_bound(fds
[j
].fd
, NULL
, NULL
) == 1 ||
5282 _get_fd_type(fds
[j
].fd
) == SOCK_DGRAM
)
5284 fds
[j
].events
= POLLOUT
;
5288 release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
5294 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
5296 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
5297 if (fds
[j
].fd
== -1) goto failed
;
5299 if (is_fd_bound(fds
[j
].fd
, NULL
, NULL
) == 1)
5301 int oob_inlined
= 0;
5302 socklen_t olen
= sizeof(oob_inlined
);
5304 fds
[j
].events
= POLLHUP
;
5306 /* Check if we need to test for urgent data or not */
5307 getsockopt(fds
[j
].fd
, SOL_SOCKET
, SO_OOBINLINE
, (char*) &oob_inlined
, &olen
);
5309 fds
[j
].events
|= POLLPRI
;
5313 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
5324 for (i
= 0; i
< readfds
->fd_count
&& j
< count
; i
++, j
++)
5325 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
5327 for (i
= 0; i
< writefds
->fd_count
&& j
< count
; i
++, j
++)
5328 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
5330 for (i
= 0; i
< exceptfds
->fd_count
&& j
< count
; i
++, j
++)
5331 if (fds
[j
].fd
!= -1) release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
5335 /* release the file descriptor obtained in fd_sets_to_poll */
5336 /* must be called with the original fd_set arrays, before calling get_poll_results */
5337 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
5338 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
5340 unsigned int i
, j
= 0;
5344 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
5345 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
5349 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
5350 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
5354 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
5356 if (fds
[j
].fd
== -1) continue;
5357 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
5358 if (fds
[j
].revents
& POLLHUP
)
5360 int fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
5362 release_sock_fd( exceptfds
->fd_array
[i
], fd
);
5370 static int do_poll(struct pollfd
*pollfds
, int count
, int timeout
)
5372 struct timeval tv1
, tv2
;
5373 int ret
, torig
= timeout
;
5375 if (timeout
> 0) gettimeofday( &tv1
, 0 );
5377 while ((ret
= poll( pollfds
, count
, timeout
)) < 0)
5379 if (errno
!= EINTR
) break;
5380 if (timeout
< 0) continue;
5381 if (timeout
== 0) return 0;
5383 gettimeofday( &tv2
, 0 );
5385 tv2
.tv_sec
-= tv1
.tv_sec
;
5386 tv2
.tv_usec
-= tv1
.tv_usec
;
5387 if (tv2
.tv_usec
< 0)
5389 tv2
.tv_usec
+= 1000000;
5393 timeout
= torig
- (tv2
.tv_sec
* 1000) - (tv2
.tv_usec
+ 999) / 1000;
5394 if (timeout
<= 0) return 0;
5399 /* map the poll results back into the Windows fd sets */
5400 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
5401 const struct pollfd
*fds
)
5403 const struct pollfd
*poll_writefds
= fds
+ (readfds
? readfds
->fd_count
: 0);
5404 const struct pollfd
*poll_exceptfds
= poll_writefds
+ (writefds
? writefds
->fd_count
: 0);
5405 unsigned int i
, k
, total
= 0;
5409 for (i
= k
= 0; i
< readfds
->fd_count
; i
++)
5411 if (fds
[i
].revents
||
5412 (readfds
== writefds
&& (poll_writefds
[i
].revents
& POLLOUT
) && !(poll_writefds
[i
].revents
& POLLHUP
)) ||
5413 (readfds
== exceptfds
&& poll_exceptfds
[i
].revents
))
5414 readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
5416 readfds
->fd_count
= k
;
5419 if (writefds
&& writefds
!= readfds
)
5421 for (i
= k
= 0; i
< writefds
->fd_count
; i
++)
5423 if (((poll_writefds
[i
].revents
& POLLOUT
) && !(poll_writefds
[i
].revents
& POLLHUP
)) ||
5424 (writefds
== exceptfds
&& poll_exceptfds
[i
].revents
))
5425 writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
5427 writefds
->fd_count
= k
;
5430 if (exceptfds
&& exceptfds
!= readfds
&& exceptfds
!= writefds
)
5432 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++)
5433 if (poll_exceptfds
[i
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
5434 exceptfds
->fd_count
= k
;
5440 /***********************************************************************
5441 * select (WS2_32.18)
5443 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
5444 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
5445 const struct WS_timeval
* ws_timeout
)
5447 struct pollfd
*pollfds
;
5448 int count
, ret
, timeout
= -1;
5450 TRACE("read %p, write %p, excp %p timeout %p\n",
5451 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
5453 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)))
5454 return SOCKET_ERROR
;
5457 timeout
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
5459 ret
= do_poll(pollfds
, count
, timeout
);
5460 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
5462 if (ret
== -1) SetLastError(wsaErrno());
5463 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
5467 /***********************************************************************
5470 int WINAPI
WSAPoll(WSAPOLLFD
*wfds
, ULONG count
, int timeout
)
5473 struct pollfd
*ufds
;
5477 SetLastError(WSAEINVAL
);
5478 return SOCKET_ERROR
;
5482 SetLastError(WSAEFAULT
);
5483 return SOCKET_ERROR
;
5486 if (!(ufds
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(ufds
[0]))))
5488 SetLastError(WSAENOBUFS
);
5489 return SOCKET_ERROR
;
5492 for (i
= 0; i
< count
; i
++)
5494 ufds
[i
].fd
= get_sock_fd(wfds
[i
].fd
, 0, NULL
);
5495 ufds
[i
].events
= convert_poll_w2u(wfds
[i
].events
);
5496 ufds
[i
].revents
= 0;
5499 ret
= do_poll(ufds
, count
, timeout
);
5501 for (i
= 0; i
< count
; i
++)
5503 if (ufds
[i
].fd
!= -1)
5505 release_sock_fd(wfds
[i
].fd
, ufds
[i
].fd
);
5506 if (ufds
[i
].revents
& POLLHUP
)
5508 /* Check if the socket still exists */
5509 int fd
= get_sock_fd(wfds
[i
].fd
, 0, NULL
);
5512 wfds
[i
].revents
= WS_POLLHUP
;
5513 release_sock_fd(wfds
[i
].fd
, fd
);
5516 wfds
[i
].revents
= WS_POLLNVAL
;
5519 wfds
[i
].revents
= convert_poll_u2w(ufds
[i
].revents
);
5522 wfds
[i
].revents
= WS_POLLNVAL
;
5525 HeapFree(GetProcessHeap(), 0, ufds
);
5529 /* helper to send completion messages for client-only i/o operation case */
5530 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
,
5531 ULONG Information
, BOOL async
)
5533 SERVER_START_REQ( add_fd_completion
)
5535 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(sock
) );
5536 req
->cvalue
= CompletionValue
;
5537 req
->status
= CompletionStatus
;
5538 req
->information
= Information
;
5540 wine_server_call( req
);
5546 /***********************************************************************
5549 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
5555 wsabuf
.buf
= (char*) buf
;
5557 if ( WS2_sendto( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
5558 return SOCKET_ERROR
;
5563 /***********************************************************************
5564 * WSASend (WS2_32.72)
5566 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5567 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
5568 LPWSAOVERLAPPED lpOverlapped
,
5569 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5571 return WS2_sendto( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
5572 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
5575 /***********************************************************************
5576 * WSASendDisconnect (WS2_32.73)
5578 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
5580 return WS_shutdown( s
, SD_SEND
);
5584 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5585 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
5586 const struct WS_sockaddr
*to
, int tolen
,
5587 LPWSAOVERLAPPED lpOverlapped
,
5588 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5590 unsigned int i
, options
;
5591 int n
, fd
, err
, overlapped
, flags
;
5592 struct ws2_async
*wsa
= NULL
, localwsa
;
5593 int totalLength
= 0;
5597 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
5598 s
, lpBuffers
, dwBufferCount
, dwFlags
,
5599 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
5601 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
5602 TRACE( "fd=%d, options=%x\n", fd
, options
);
5604 if ( fd
== -1 ) return SOCKET_ERROR
;
5606 if (!lpOverlapped
&& !lpNumberOfBytesSent
)
5612 overlapped
= (lpOverlapped
|| lpCompletionRoutine
) &&
5613 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
5614 if (overlapped
|| dwBufferCount
> 1)
5616 if (!(wsa
= (struct ws2_async
*)alloc_async_io( offsetof(struct ws2_async
, iovec
[dwBufferCount
]),
5626 wsa
->hSocket
= SOCKET2HANDLE(s
);
5627 wsa
->addr
= (struct WS_sockaddr
*)to
;
5628 wsa
->addrlen
.val
= tolen
;
5629 wsa
->flags
= dwFlags
;
5630 wsa
->lpFlags
= &wsa
->flags
;
5631 wsa
->control
= NULL
;
5632 wsa
->n_iovecs
= dwBufferCount
;
5633 wsa
->first_iovec
= 0;
5634 for ( i
= 0; i
< dwBufferCount
; i
++ )
5636 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
5637 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
5638 totalLength
+= lpBuffers
[i
].len
;
5641 flags
= convert_flags(dwFlags
);
5642 n
= WS2_send( fd
, wsa
, flags
);
5643 if (n
== -1 && errno
!= EAGAIN
)
5651 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
5652 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
5654 wsa
->user_overlapped
= lpOverlapped
;
5655 wsa
->completion_func
= lpCompletionRoutine
;
5656 release_sock_fd( s
, fd
);
5658 if (n
== -1 || n
< totalLength
)
5660 iosb
->u
.Status
= STATUS_PENDING
;
5661 iosb
->Information
= n
== -1 ? 0 : n
;
5663 if (wsa
->completion_func
)
5664 err
= register_async( ASYNC_TYPE_WRITE
, wsa
->hSocket
, &wsa
->io
, NULL
,
5665 ws2_async_apc
, wsa
, iosb
);
5667 err
= register_async( ASYNC_TYPE_WRITE
, wsa
->hSocket
, &wsa
->io
, lpOverlapped
->hEvent
,
5668 NULL
, (void *)cvalue
, iosb
);
5670 /* Enable the event only after starting the async. The server will deliver it as soon as
5671 the async is done. */
5672 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
5674 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
5675 SetLastError(NtStatusToWSAError( err
));
5676 return SOCKET_ERROR
;
5679 iosb
->u
.Status
= STATUS_SUCCESS
;
5680 iosb
->Information
= n
;
5681 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= n
;
5682 if (!wsa
->completion_func
)
5684 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
, FALSE
);
5685 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
5686 HeapFree( GetProcessHeap(), 0, wsa
);
5688 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
5689 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
5690 SetLastError(ERROR_SUCCESS
);
5694 if ((err
= sock_is_blocking( s
, &is_blocking
))) goto error
;
5698 /* On a blocking non-overlapped stream socket,
5699 * sending blocks until the entire buffer is sent. */
5700 DWORD timeout_start
= GetTickCount();
5702 bytes_sent
= n
== -1 ? 0 : n
;
5704 while (wsa
->first_iovec
< wsa
->n_iovecs
)
5707 int poll_timeout
= -1;
5708 INT64 timeout
= get_rcvsnd_timeo(fd
, FALSE
);
5712 timeout
-= GetTickCount() - timeout_start
;
5713 if (timeout
< 0) poll_timeout
= 0;
5714 else poll_timeout
= timeout
<= INT_MAX
? timeout
: INT_MAX
;
5718 pfd
.events
= POLLOUT
;
5720 if (!poll_timeout
|| !poll( &pfd
, 1, poll_timeout
))
5723 goto error
; /* msdn says a timeout in send is fatal */
5726 n
= WS2_send( fd
, wsa
, flags
);
5727 if (n
== -1 && errno
!= EAGAIN
)
5737 else /* non-blocking */
5739 if (n
< totalLength
)
5740 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
5743 err
= WSAEWOULDBLOCK
;
5749 TRACE(" -> %i bytes\n", bytes_sent
);
5751 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= bytes_sent
;
5752 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
5753 release_sock_fd( s
, fd
);
5754 SetLastError(ERROR_SUCCESS
);
5758 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
5759 release_sock_fd( s
, fd
);
5760 WARN(" -> ERROR %d\n", err
);
5762 return SOCKET_ERROR
;
5765 /***********************************************************************
5766 * WSASendTo (WS2_32.74)
5768 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5769 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
5770 const struct WS_sockaddr
*to
, int tolen
,
5771 LPWSAOVERLAPPED lpOverlapped
,
5772 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5774 return WS2_sendto( s
, lpBuffers
, dwBufferCount
,
5775 lpNumberOfBytesSent
, dwFlags
,
5777 lpOverlapped
, lpCompletionRoutine
);
5780 /***********************************************************************
5781 * sendto (WS2_32.20)
5783 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
5784 const struct WS_sockaddr
*to
, int tolen
)
5790 wsabuf
.buf
= (char*) buf
;
5792 if ( WS2_sendto(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
5793 return SOCKET_ERROR
;
5798 /***********************************************************************
5799 * setsockopt (WS2_32.21)
5801 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
5802 const char *optval
, int optlen
)
5806 struct linger linger
;
5807 struct timeval tval
;
5808 struct ip_mreq_source mreq_source
;
5810 TRACE("(socket %04lx, %s, optval %s, optlen %d)\n", s
,
5811 debugstr_sockopt(level
, optname
), debugstr_optval(optval
, optlen
),
5814 /* some broken apps pass the value directly instead of a pointer to it */
5815 if(optlen
&& IS_INTRESOURCE(optval
))
5817 SetLastError(WSAEFAULT
);
5818 return SOCKET_ERROR
;
5826 /* Some options need some conversion before they can be sent to
5827 * setsockopt. The conversions are done here, then they will fall through
5828 * to the general case. Special options that are not passed to
5829 * setsockopt follow below that.*/
5831 case WS_SO_DONTLINGER
:
5834 SetLastError(WSAEFAULT
);
5835 return SOCKET_ERROR
;
5837 linger
.l_onoff
= *(const int*)optval
== 0;
5838 linger
.l_linger
= 0;
5840 optname
= SO_LINGER
;
5841 optval
= (char*)&linger
;
5842 optlen
= sizeof(struct linger
);
5848 SetLastError(WSAEFAULT
);
5849 return SOCKET_ERROR
;
5851 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
5852 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
5854 optname
= SO_LINGER
;
5855 optval
= (char*)&linger
;
5856 optlen
= sizeof(struct linger
);
5860 if (!*(const int *)optval
)
5862 FIXME("SO_SNDBUF ignoring request to disable send buffering\n");
5867 convert_sockopt(&level
, &optname
);
5871 if (*(const int*)optval
< 2048)
5873 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
5878 /* The options listed here don't need any special handling. Thanks to
5879 * the conversion happening above, options from there will fall through
5881 case WS_SO_ACCEPTCONN
:
5882 case WS_SO_BROADCAST
:
5884 case WS_SO_KEEPALIVE
:
5885 case WS_SO_OOBINLINE
:
5886 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
5887 * however, using it the BSD way fixes bug 8513 and seems to be what
5888 * most programmers assume, anyway */
5889 case WS_SO_REUSEADDR
:
5891 convert_sockopt(&level
, &optname
);
5894 /* SO_DEBUG is a privileged operation, ignore it. */
5896 TRACE("Ignoring SO_DEBUG\n");
5899 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
5900 * socket. According to MSDN, this option is silently ignored.*/
5901 case WS_SO_DONTROUTE
:
5902 TRACE("Ignoring SO_DONTROUTE\n");
5905 /* Stops two sockets from being bound to the same port. Always happens
5906 * on unix systems, so just drop it. */
5907 case WS_SO_EXCLUSIVEADDRUSE
:
5908 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
5911 /* After a ConnectEx call succeeds, the socket can't be used with half of the
5912 * normal winsock functions on windows. We don't have that problem. */
5913 case WS_SO_UPDATE_CONNECT_CONTEXT
:
5914 TRACE("Ignoring SO_UPDATE_CONNECT_CONTEXT, since our sockets are normal\n");
5917 /* After a AcceptEx call succeeds, the socket can't be used with half of the
5918 * normal winsock functions on windows. We don't have that problem. */
5919 case WS_SO_UPDATE_ACCEPT_CONTEXT
:
5920 TRACE("Ignoring SO_UPDATE_ACCEPT_CONTEXT, since our sockets are normal\n");
5923 /* SO_OPENTYPE does not require a valid socket handle. */
5924 case WS_SO_OPENTYPE
:
5925 if (!optlen
|| optlen
< sizeof(int) || !optval
)
5927 SetLastError(WSAEFAULT
);
5928 return SOCKET_ERROR
;
5930 get_per_thread_data()->opentype
= *(const int *)optval
;
5931 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((const int*)optval
) );
5935 case WS_SO_RCVTIMEO
:
5938 case WS_SO_SNDTIMEO
:
5940 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
5941 if (optval
&& optlen
== sizeof(UINT32
)) {
5942 /* WinSock passes milliseconds instead of struct timeval */
5943 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
5944 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
5945 /* min of 500 milliseconds */
5946 if (tval
.tv_sec
== 0 && tval
.tv_usec
&& tval
.tv_usec
< 500000)
5947 tval
.tv_usec
= 500000;
5948 optlen
= sizeof(struct timeval
);
5949 optval
= (char*)&tval
;
5950 } else if (optlen
== sizeof(struct timeval
)) {
5951 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
5953 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
5956 convert_sockopt(&level
, &optname
);
5960 case WS_SO_RANDOMIZE_PORT
:
5961 FIXME("Ignoring WS_SO_RANDOMIZE_PORT\n");
5964 case WS_SO_PORT_SCALABILITY
:
5965 FIXME("Ignoring WS_SO_PORT_SCALABILITY\n");
5968 case WS_SO_REUSE_UNICASTPORT
:
5969 FIXME("Ignoring WS_SO_REUSE_UNICASTPORT\n");
5972 case WS_SO_REUSE_MULTICASTPORT
:
5973 FIXME("Ignoring WS_SO_REUSE_MULTICASTPORT\n");
5977 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
5978 SetLastError(WSAENOPROTOOPT
);
5979 return SOCKET_ERROR
;
5981 break; /* case WS_SOL_SOCKET */
5984 case WS_NSPROTO_IPX
:
5988 return set_ipx_packettype(s
, *(int*)optval
);
5990 case WS_IPX_FILTERPTYPE
:
5991 /* Sets the receive filter packet type, at the moment we don't support it */
5992 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
5993 /* Returning 0 is better for now than returning a SOCKET_ERROR */
5997 FIXME("opt_name:%x\n", optname
);
5998 return SOCKET_ERROR
;
6000 break; /* case WS_NSPROTO_IPX */
6003 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
6004 case WS_IPPROTO_TCP
:
6007 case WS_TCP_NODELAY
:
6008 convert_sockopt(&level
, &optname
);
6011 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
6012 return SOCKET_ERROR
;
6019 case WS_IP_ADD_SOURCE_MEMBERSHIP
:
6020 case WS_IP_DROP_SOURCE_MEMBERSHIP
:
6021 case WS_IP_BLOCK_SOURCE
:
6022 case WS_IP_UNBLOCK_SOURCE
:
6024 WS_IP_MREQ_SOURCE
* val
= (void*)optval
;
6025 mreq_source
.imr_interface
.s_addr
= val
->imr_interface
.S_un
.S_addr
;
6026 mreq_source
.imr_multiaddr
.s_addr
= val
->imr_multiaddr
.S_un
.S_addr
;
6027 mreq_source
.imr_sourceaddr
.s_addr
= val
->imr_sourceaddr
.S_un
.S_addr
;
6029 optval
= (char*)&mreq_source
;
6030 optlen
= sizeof(mreq_source
);
6032 convert_sockopt(&level
, &optname
);
6035 case WS_IP_ADD_MEMBERSHIP
:
6036 case WS_IP_DROP_MEMBERSHIP
:
6040 case WS_IP_MULTICAST_IF
:
6041 case WS_IP_MULTICAST_LOOP
:
6042 case WS_IP_MULTICAST_TTL
:
6044 #if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
6049 #ifdef IP_UNICAST_IF
6050 case WS_IP_UNICAST_IF
:
6052 convert_sockopt(&level
, &optname
);
6054 case WS_IP_DONTFRAGMENT
:
6055 return set_dont_fragment(s
, IPPROTO_IP
, *(BOOL
*)optval
) ? 0 : SOCKET_ERROR
;
6057 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
6058 return SOCKET_ERROR
;
6062 case WS_IPPROTO_IPV6
:
6065 #ifdef IPV6_ADD_MEMBERSHIP
6066 case WS_IPV6_ADD_MEMBERSHIP
:
6068 #ifdef IPV6_DROP_MEMBERSHIP
6069 case WS_IPV6_DROP_MEMBERSHIP
:
6071 case WS_IPV6_MULTICAST_IF
:
6072 case WS_IPV6_MULTICAST_HOPS
:
6073 case WS_IPV6_MULTICAST_LOOP
:
6074 case WS_IPV6_UNICAST_HOPS
:
6075 #ifdef IPV6_UNICAST_IF
6076 case WS_IPV6_UNICAST_IF
:
6078 convert_sockopt(&level
, &optname
);
6080 case WS_IPV6_DONTFRAG
:
6081 return set_dont_fragment(s
, IPPROTO_IPV6
, *(BOOL
*)optval
) ? 0 : SOCKET_ERROR
;
6082 case WS_IPV6_PROTECTION_LEVEL
:
6083 FIXME("IPV6_PROTECTION_LEVEL is ignored!\n");
6085 case WS_IPV6_V6ONLY
:
6087 union generic_unix_sockaddr uaddr
;
6091 fd
= get_sock_fd( s
, 0, NULL
);
6092 if (fd
== -1) return SOCKET_ERROR
;
6094 bound
= is_fd_bound(fd
, &uaddr
, &uaddrlen
);
6095 release_sock_fd( s
, fd
);
6096 if (bound
== 0 && uaddr
.addr
.sa_family
== AF_INET
)
6098 /* Changing IPV6_V6ONLY succeeds on AF_INET (IPv4) socket
6099 * on Windows (with IPv6 support) if the socket is unbound.
6100 * It is essentially a noop, though Windows does store the value
6102 WARN("Silently ignoring IPPROTO_IPV6+IPV6_V6ONLY on AF_INET socket\n");
6105 level
= IPPROTO_IPV6
;
6106 optname
= IPV6_V6ONLY
;
6110 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
6111 return SOCKET_ERROR
;
6116 WARN("Unknown level: 0x%08x\n", level
);
6117 SetLastError(WSAEINVAL
);
6118 return SOCKET_ERROR
;
6119 } /* end switch(level) */
6121 /* avoid endianness issues if argument is a 16-bit int */
6122 if (optval
&& optlen
< sizeof(int))
6124 woptval
= *((const INT16
*) optval
);
6125 optval
= (char*) &woptval
;
6126 woptval
&= (1 << optlen
* 8) - 1;
6129 fd
= get_sock_fd( s
, 0, NULL
);
6130 if (fd
== -1) return SOCKET_ERROR
;
6132 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
6135 if (level
== SOL_SOCKET
&& optname
== SO_REUSEADDR
&&
6136 setsockopt(fd
, level
, SO_REUSEPORT
, optval
, optlen
) != 0)
6138 SetLastError(wsaErrno());
6139 release_sock_fd( s
, fd
);
6140 return SOCKET_ERROR
;
6143 release_sock_fd( s
, fd
);
6146 TRACE("Setting socket error, %d\n", wsaErrno());
6147 SetLastError(wsaErrno());
6148 release_sock_fd( s
, fd
);
6150 return SOCKET_ERROR
;
6153 /***********************************************************************
6154 * shutdown (WS2_32.22)
6156 int WINAPI
WS_shutdown(SOCKET s
, int how
)
6158 int fd
, err
= WSAENOTSOCK
;
6159 unsigned int options
= 0, clear_flags
= 0;
6161 fd
= get_sock_fd( s
, 0, &options
);
6162 TRACE("socket %04lx, how 0x%x, options 0x%x\n", s
, how
, options
);
6165 return SOCKET_ERROR
;
6169 case SD_RECEIVE
: /* drop receives */
6170 clear_flags
|= FD_READ
;
6172 case SD_SEND
: /* drop sends */
6173 clear_flags
|= FD_WRITE
;
6175 case SD_BOTH
: /* drop all */
6176 clear_flags
|= FD_READ
|FD_WRITE
;
6179 clear_flags
|= FD_WINE_LISTENING
;
6182 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
6187 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
6190 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
6194 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
6195 if (!err
) err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
6198 if (err
) goto error
;
6200 else /* non-overlapped mode */
6202 if ( shutdown( fd
, how
) )
6209 release_sock_fd( s
, fd
);
6210 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
6211 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
6215 release_sock_fd( s
, fd
);
6216 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
6217 SetLastError( err
);
6218 return SOCKET_ERROR
;
6221 /***********************************************************************
6222 * socket (WS2_32.23)
6224 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
6226 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
6228 return WSASocketW( af
, type
, protocol
, NULL
, 0,
6229 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
6233 /***********************************************************************
6234 * gethostbyaddr (WS2_32.51)
6236 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
6238 struct WS_hostent
*retval
= NULL
;
6239 struct hostent
* host
;
6240 int unixtype
= convert_af_w2u(type
);
6241 const char *paddr
= addr
;
6242 unsigned long loopback
;
6243 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6245 int ebufsize
= 1024;
6246 struct hostent hostentry
;
6247 int locerr
= ENOBUFS
;
6250 /* convert back the magic loopback address if necessary */
6251 if (unixtype
== AF_INET
&& len
== 4 && !memcmp(addr
, magic_loopback_addr
, 4))
6253 loopback
= htonl(INADDR_LOOPBACK
);
6254 paddr
= (char*) &loopback
;
6257 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6259 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
6261 int res
= gethostbyaddr_r(paddr
, len
, unixtype
,
6262 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
6263 if (res
!= ERANGE
) break;
6265 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
6267 if (host
) retval
= WS_dup_he(host
);
6268 else SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
6269 HeapFree(GetProcessHeap(),0,extrabuf
);
6271 EnterCriticalSection( &csWSgetXXXbyYYY
);
6272 host
= gethostbyaddr(paddr
, len
, unixtype
);
6273 if (host
) retval
= WS_dup_he(host
);
6274 else SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
6275 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6277 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
6281 /***********************************************************************
6282 * WS_compare_routes_by_metric_asc (INTERNAL)
6284 * Comparison function for qsort(), for sorting two routes (struct route)
6285 * by metric in ascending order.
6287 static int WS_compare_routes_by_metric_asc(const void *left
, const void *right
)
6289 const struct route
*a
= left
, *b
= right
;
6290 if (a
->default_route
&& b
->default_route
)
6291 return a
->default_route
- b
->default_route
;
6292 if (a
->default_route
&& !b
->default_route
)
6294 if (b
->default_route
&& !a
->default_route
)
6296 return a
->metric
- b
->metric
;
6299 /***********************************************************************
6300 * WS_get_local_ips (INTERNAL)
6302 * Returns the list of local IP addresses by going through the network
6303 * adapters and using the local routing table to sort the addresses
6304 * from highest routing priority to lowest routing priority. This
6305 * functionality is inferred from the description for obtaining local
6306 * IP addresses given in the Knowledge Base Article Q160215.
6308 * Please note that the returned hostent is only freed when the thread
6309 * closes and is replaced if another hostent is requested.
6311 static struct WS_hostent
* WS_get_local_ips( char *hostname
)
6313 int numroutes
= 0, i
, j
, default_routes
= 0;
6315 PIP_ADAPTER_INFO adapters
= NULL
, k
;
6316 struct WS_hostent
*hostlist
= NULL
;
6317 PMIB_IPFORWARDTABLE routes
= NULL
;
6318 struct route
*route_addrs
= NULL
;
6319 DWORD adap_size
, route_size
;
6321 /* Obtain the size of the adapter list and routing table, also allocate memory */
6322 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
6324 if (GetIpForwardTable(NULL
, &route_size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
6326 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
6327 routes
= HeapAlloc(GetProcessHeap(), 0, route_size
);
6328 if (adapters
== NULL
|| routes
== NULL
)
6330 /* Obtain the adapter list and the full routing table */
6331 if (GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
6333 if (GetIpForwardTable(routes
, &route_size
, FALSE
) != NO_ERROR
)
6335 /* Store the interface associated with each route */
6336 for (n
= 0; n
< routes
->dwNumEntries
; n
++)
6339 DWORD ifmetric
, ifdefault
= 0;
6340 BOOL exists
= FALSE
;
6342 /* Check if this is a default route (there may be more than one) */
6343 if (!routes
->table
[n
].dwForwardDest
)
6344 ifdefault
= ++default_routes
;
6345 else if (routes
->table
[n
].u1
.ForwardType
!= MIB_IPROUTE_TYPE_DIRECT
)
6347 ifindex
= routes
->table
[n
].dwForwardIfIndex
;
6348 ifmetric
= routes
->table
[n
].dwForwardMetric1
;
6349 /* Only store the lowest valued metric for an interface */
6350 for (j
= 0; j
< numroutes
; j
++)
6352 if (route_addrs
[j
].interface
== ifindex
)
6354 if (route_addrs
[j
].metric
> ifmetric
)
6355 route_addrs
[j
].metric
= ifmetric
;
6361 route_addrs
= heap_realloc(route_addrs
, (numroutes
+1)*sizeof(struct route
));
6362 if (route_addrs
== NULL
)
6363 goto cleanup
; /* Memory allocation error, fail gracefully */
6364 route_addrs
[numroutes
].interface
= ifindex
;
6365 route_addrs
[numroutes
].metric
= ifmetric
;
6366 route_addrs
[numroutes
].default_route
= ifdefault
;
6367 /* If no IP is found in the next step (for whatever reason)
6368 * then fall back to the magic loopback address.
6370 memcpy(&(route_addrs
[numroutes
].addr
.s_addr
), magic_loopback_addr
, 4);
6374 goto cleanup
; /* No routes, fall back to the Magic IP */
6375 /* Find the IP address associated with each found interface */
6376 for (i
= 0; i
< numroutes
; i
++)
6378 for (k
= adapters
; k
!= NULL
; k
= k
->Next
)
6380 char *ip
= k
->IpAddressList
.IpAddress
.String
;
6382 if (route_addrs
[i
].interface
== k
->Index
)
6383 route_addrs
[i
].addr
.s_addr
= (in_addr_t
) inet_addr(ip
);
6386 /* Allocate a hostent and enough memory for all the IPs,
6387 * including the NULL at the end of the list.
6389 hostlist
= WS_create_he(hostname
, 1, 0, numroutes
+1, sizeof(struct in_addr
));
6390 if (hostlist
== NULL
)
6391 goto cleanup
; /* Failed to allocate a hostent for the list of IPs */
6392 hostlist
->h_addr_list
[numroutes
] = NULL
; /* NULL-terminate the address list */
6393 hostlist
->h_aliases
[0] = NULL
; /* NULL-terminate the alias list */
6394 hostlist
->h_addrtype
= AF_INET
;
6395 hostlist
->h_length
= sizeof(struct in_addr
); /* = 4 */
6396 /* Reorder the entries before placing them in the host list. Windows expects
6397 * the IP list in order from highest priority to lowest (the critical thing
6398 * is that most applications expect the first IP to be the default route).
6401 qsort(route_addrs
, numroutes
, sizeof(struct route
), WS_compare_routes_by_metric_asc
);
6403 for (i
= 0; i
< numroutes
; i
++)
6404 (*(struct in_addr
*) hostlist
->h_addr_list
[i
]) = route_addrs
[i
].addr
;
6406 /* Cleanup all allocated memory except the address list,
6407 * the address list is used by the calling app.
6410 HeapFree(GetProcessHeap(), 0, route_addrs
);
6411 HeapFree(GetProcessHeap(), 0, adapters
);
6412 HeapFree(GetProcessHeap(), 0, routes
);
6416 /***********************************************************************
6417 * gethostbyname (WS2_32.52)
6419 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
6421 struct WS_hostent
*retval
= NULL
;
6422 struct hostent
* host
;
6423 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6426 struct hostent hostentry
;
6427 int locerr
= ENOBUFS
;
6431 SetLastError(WSANOTINITIALISED
);
6434 if( gethostname( hostname
, 100) == -1) {
6435 SetLastError(WSAENOBUFS
); /* appropriate ? */
6438 if( !name
|| !name
[0]) {
6441 /* If the hostname of the local machine is requested then return the
6442 * complete list of local IP addresses */
6443 if(strcmp(name
, hostname
) == 0)
6444 retval
= WS_get_local_ips(hostname
);
6445 /* If any other hostname was requested (or the routing table lookup failed)
6446 * then return the IP found by the host OS */
6449 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6451 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
6453 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
6454 if( res
!= ERANGE
) break;
6456 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
6458 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
6460 EnterCriticalSection( &csWSgetXXXbyYYY
);
6461 host
= gethostbyname(name
);
6462 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
6464 if (host
) retval
= WS_dup_he(host
);
6465 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
6466 HeapFree(GetProcessHeap(),0,extrabuf
);
6468 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6471 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
6472 strcmp(name
, "localhost") != 0)
6474 /* hostname != "localhost" but has loopback address. replace by our
6475 * special address.*/
6476 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
6478 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
6483 static const struct { int prot
; const char *names
[3]; } protocols
[] =
6485 { 0, { "ip", "IP" }},
6486 { 1, { "icmp", "ICMP" }},
6487 { 2, { "igmp", "IGMP" }},
6488 { 3, { "ggp", "GGP" }},
6489 { 6, { "tcp", "TCP" }},
6490 { 8, { "egp", "EGP" }},
6491 { 9, { "igp", "IGP" }},
6492 { 12, { "pup", "PUP" }},
6493 { 17, { "udp", "UDP" }},
6494 { 20, { "hmp", "HMP" }},
6495 { 22, { "xns-idp", "XNS-IDP" }},
6496 { 27, { "rdp", "RDP" }},
6497 { 29, { "iso-tp4", "ISO-TP4" }},
6498 { 33, { "dccp", "DCCP" }},
6499 { 36, { "xtp", "XTP" }},
6500 { 37, { "ddp", "DDP" }},
6501 { 38, { "idpr-cmtp", "IDPR-CMTP" }},
6502 { 41, { "ipv6", "IPv6" }},
6503 { 43, { "ipv6-route", "IPv6-Route" }},
6504 { 44, { "ipv6-frag", "IPv6-Frag" }},
6505 { 45, { "idrp", "IDRP" }},
6506 { 46, { "rsvp", "RSVP" }},
6507 { 47, { "gre", "GRE" }},
6508 { 50, { "esp", "ESP" }},
6509 { 51, { "ah", "AH" }},
6510 { 57, { "skip", "SKIP" }},
6511 { 58, { "ipv6-icmp", "IPv6-ICMP" }},
6512 { 59, { "ipv6-nonxt", "IPv6-NoNxt" }},
6513 { 60, { "ipv6-opts", "IPv6-Opts" }},
6514 { 66, { "rvd", "RVD" }},
6515 { 73, { "rspf", "RSPF" }},
6516 { 81, { "vmtp", "VMTP" }},
6517 { 88, { "eigrp", "EIGRP" }},
6518 { 89, { "ospf", "OSPFIGP" }},
6519 { 93, { "ax.25", "AX.25" }},
6520 { 94, { "ipip", "IPIP" }},
6521 { 97, { "etherip", "ETHERIP" }},
6522 { 98, { "encap", "ENCAP" }},
6523 { 103, { "pim", "PIM" }},
6524 { 108, { "ipcomp", "IPCOMP" }},
6525 { 112, { "vrrp", "VRRP" }},
6526 { 115, { "l2tp", "L2TP" }},
6527 { 124, { "isis", "ISIS" }},
6528 { 132, { "sctp", "SCTP" }},
6529 { 133, { "fc", "FC" }},
6530 { 135, { "mobility-header", "Mobility-Header" }},
6531 { 136, { "udplite", "UDPLite" }},
6532 { 137, { "mpls-in-ip", "MPLS-in-IP" }},
6533 { 139, { "hip", "HIP" }},
6534 { 140, { "shim6", "Shim6" }},
6535 { 141, { "wesp", "WESP" }},
6536 { 142, { "rohc", "ROHC" }},
6539 /***********************************************************************
6540 * getprotobyname (WS2_32.53)
6542 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
6544 struct WS_protoent
* retval
= NULL
;
6545 #ifdef HAVE_GETPROTOBYNAME
6546 struct protoent
* proto
;
6547 EnterCriticalSection( &csWSgetXXXbyYYY
);
6548 if( (proto
= getprotobyname(name
)) != NULL
)
6549 retval
= WS_create_pe( proto
->p_name
, proto
->p_aliases
, proto
->p_proto
);
6550 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6555 for (i
= 0; i
< ARRAY_SIZE(protocols
); i
++)
6557 if (_strnicmp( protocols
[i
].names
[0], name
, -1 )) continue;
6558 retval
= WS_create_pe( protocols
[i
].names
[0], (char **)protocols
[i
].names
+ 1,
6559 protocols
[i
].prot
);
6565 WARN( "protocol %s not found\n", debugstr_a(name
) );
6566 SetLastError(WSANO_DATA
);
6568 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
6573 /***********************************************************************
6574 * getprotobynumber (WS2_32.54)
6576 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
6578 struct WS_protoent
* retval
= NULL
;
6579 #ifdef HAVE_GETPROTOBYNUMBER
6580 struct protoent
* proto
;
6581 EnterCriticalSection( &csWSgetXXXbyYYY
);
6582 if( (proto
= getprotobynumber(number
)) != NULL
)
6583 retval
= WS_create_pe( proto
->p_name
, proto
->p_aliases
, proto
->p_proto
);
6584 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6589 for (i
= 0; i
< ARRAY_SIZE(protocols
); i
++)
6591 if (protocols
[i
].prot
!= number
) continue;
6592 retval
= WS_create_pe( protocols
[i
].names
[0], (char **)protocols
[i
].names
+ 1,
6593 protocols
[i
].prot
);
6599 WARN( "protocol %d not found\n", number
);
6600 SetLastError(WSANO_DATA
);
6602 TRACE("%i ret %p\n", number
, retval
);
6607 /***********************************************************************
6608 * getservbyname (WS2_32.55)
6610 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
6612 struct WS_servent
* retval
= NULL
;
6613 struct servent
* serv
;
6615 char *proto_str
= NULL
;
6617 if (!(name_str
= strdup_lower(name
))) return NULL
;
6619 if (proto
&& *proto
)
6621 if (!(proto_str
= strdup_lower(proto
)))
6623 HeapFree( GetProcessHeap(), 0, name_str
);
6628 EnterCriticalSection( &csWSgetXXXbyYYY
);
6629 serv
= getservbyname(name_str
, proto_str
);
6632 retval
= WS_dup_se(serv
);
6634 else SetLastError(WSANO_DATA
);
6635 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6636 HeapFree( GetProcessHeap(), 0, proto_str
);
6637 HeapFree( GetProcessHeap(), 0, name_str
);
6638 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
6642 /***********************************************************************
6643 * freeaddrinfo (WS2_32.@)
6645 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
6648 struct WS_addrinfo
*next
;
6650 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
6651 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
6652 next
= res
->ai_next
;
6653 HeapFree(GetProcessHeap(),0,res
);
6658 /* helper functions for getaddrinfo()/getnameinfo() */
6659 static int convert_aiflag_w2u(int winflags
) {
6663 for (i
= 0; i
< ARRAY_SIZE(ws_aiflag_map
); i
++)
6664 if (ws_aiflag_map
[i
][0] & winflags
) {
6665 unixflags
|= ws_aiflag_map
[i
][1];
6666 winflags
&= ~ws_aiflag_map
[i
][0];
6669 FIXME("Unhandled windows AI_xxx flags 0x%x\n", winflags
);
6673 static int convert_niflag_w2u(int winflags
) {
6677 for (i
= 0; i
< ARRAY_SIZE(ws_niflag_map
); i
++)
6678 if (ws_niflag_map
[i
][0] & winflags
) {
6679 unixflags
|= ws_niflag_map
[i
][1];
6680 winflags
&= ~ws_niflag_map
[i
][0];
6683 FIXME("Unhandled windows NI_xxx flags 0x%x\n", winflags
);
6687 static int convert_aiflag_u2w(int unixflags
) {
6691 for (i
= 0; i
< ARRAY_SIZE(ws_aiflag_map
); i
++)
6692 if (ws_aiflag_map
[i
][1] & unixflags
) {
6693 winflags
|= ws_aiflag_map
[i
][0];
6694 unixflags
&= ~ws_aiflag_map
[i
][1];
6697 WARN("Unhandled UNIX AI_xxx flags 0x%x\n", unixflags
);
6701 static int convert_eai_u2w(int unixret
) {
6704 if (!unixret
) return 0;
6706 for (i
=0;ws_eai_map
[i
][0];i
++)
6707 if (ws_eai_map
[i
][1] == unixret
)
6708 return ws_eai_map
[i
][0];
6710 if (unixret
== EAI_SYSTEM
)
6711 /* There are broken versions of glibc which return EAI_SYSTEM
6712 * and set errno to 0 instead of returning EAI_NONAME.
6714 return errno
? sock_get_error( errno
) : WS_EAI_NONAME
;
6716 FIXME("Unhandled unix EAI_xxx ret %d\n", unixret
);
6720 static char *get_fqdn(void)
6725 GetComputerNameExA( ComputerNamePhysicalDnsFullyQualified
, NULL
, &size
);
6726 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
6727 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, size
))) return NULL
;
6728 if (!GetComputerNameExA( ComputerNamePhysicalDnsFullyQualified
, ret
, &size
))
6730 HeapFree( GetProcessHeap(), 0, ret
);
6736 /***********************************************************************
6737 * getaddrinfo (WS2_32.@)
6739 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
6741 #ifdef HAVE_GETADDRINFO
6742 struct addrinfo
*unixaires
= NULL
;
6744 struct addrinfo unixhints
, *punixhints
= NULL
;
6745 char *dot
, *nodeV6
= NULL
, *fqdn
;
6747 size_t hostname_len
= 0;
6750 if (!nodename
&& !servname
)
6752 SetLastError(WSAHOST_NOT_FOUND
);
6753 return WSAHOST_NOT_FOUND
;
6757 if (!fqdn
) return WSA_NOT_ENOUGH_MEMORY
;
6758 dot
= strchr(fqdn
, '.');
6760 hostname_len
= dot
- fqdn
;
6764 else if (!nodename
[0])
6770 /* Check for [ipv6] or [ipv6]:portnumber, which are supported by Windows */
6771 if (!hints
|| hints
->ai_family
== WS_AF_UNSPEC
|| hints
->ai_family
== WS_AF_INET6
)
6773 char *close_bracket
;
6775 if (node
[0] == '[' && (close_bracket
= strchr(node
+ 1, ']')))
6777 nodeV6
= HeapAlloc(GetProcessHeap(), 0, close_bracket
- node
);
6780 HeapFree(GetProcessHeap(), 0, fqdn
);
6781 return WSA_NOT_ENOUGH_MEMORY
;
6783 lstrcpynA(nodeV6
, node
+ 1, close_bracket
- node
);
6789 /* servname tweak required by OSX and BSD kernels */
6790 if (servname
&& !servname
[0]) servname
= "0";
6793 punixhints
= &unixhints
;
6795 memset(&unixhints
, 0, sizeof(unixhints
));
6796 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
6798 /* zero is a wildcard, no need to convert */
6799 if (hints
->ai_family
)
6800 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
6801 if (hints
->ai_socktype
)
6802 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
6803 if (hints
->ai_protocol
)
6804 punixhints
->ai_protocol
= max(convert_proto_w2u(hints
->ai_protocol
), 0);
6806 if (punixhints
->ai_socktype
< 0)
6808 SetLastError(WSAESOCKTNOSUPPORT
);
6809 HeapFree(GetProcessHeap(), 0, fqdn
);
6810 HeapFree(GetProcessHeap(), 0, nodeV6
);
6811 return SOCKET_ERROR
;
6814 /* windows allows invalid combinations of socket type and protocol, unix does not.
6815 * fix the parameters here to make getaddrinfo call always work */
6816 if (punixhints
->ai_protocol
== IPPROTO_TCP
&&
6817 punixhints
->ai_socktype
!= SOCK_STREAM
&& punixhints
->ai_socktype
!= SOCK_SEQPACKET
)
6818 punixhints
->ai_socktype
= 0;
6820 else if (punixhints
->ai_protocol
== IPPROTO_UDP
&& punixhints
->ai_socktype
!= SOCK_DGRAM
)
6821 punixhints
->ai_socktype
= 0;
6823 else if (IS_IPX_PROTO(punixhints
->ai_protocol
) && punixhints
->ai_socktype
!= SOCK_DGRAM
)
6824 punixhints
->ai_socktype
= 0;
6826 else if (punixhints
->ai_protocol
== IPPROTO_IPV6
)
6827 punixhints
->ai_protocol
= 0;
6830 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
6831 result
= getaddrinfo(node
, servname
, punixhints
, &unixaires
);
6833 if (result
&& (!hints
|| !(hints
->ai_flags
& WS_AI_NUMERICHOST
))
6834 && (!strcmp(fqdn
, node
) || (!strncmp(fqdn
, node
, hostname_len
) && !node
[hostname_len
])))
6836 /* If it didn't work it means the host name IP is not in /etc/hosts, try again
6837 * by sending a NULL host and avoid sending a NULL servname too because that
6839 ERR_(winediag
)("Failed to resolve your host name IP\n");
6840 result
= getaddrinfo(NULL
, servname
? servname
: "0", punixhints
, &unixaires
);
6842 TRACE("%s, %s %p -> %p %d\n", debugstr_a(nodename
), debugstr_a(servname
), hints
, res
, result
);
6843 HeapFree(GetProcessHeap(), 0, fqdn
);
6844 HeapFree(GetProcessHeap(), 0, nodeV6
);
6847 struct addrinfo
*xuai
= unixaires
;
6848 struct WS_addrinfo
**xai
= res
;
6852 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
6858 *xai
= ai
;xai
= &ai
->ai_next
;
6859 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
6860 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
6861 /* copy whatever was sent in the hints */
6863 ai
->ai_socktype
= hints
->ai_socktype
;
6864 ai
->ai_protocol
= hints
->ai_protocol
;
6866 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
6867 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
6869 if (xuai
->ai_canonname
) {
6870 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
6871 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
6872 if (!ai
->ai_canonname
)
6874 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
6876 len
= xuai
->ai_addrlen
;
6877 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
6880 ai
->ai_addrlen
= len
;
6882 int winlen
= ai
->ai_addrlen
;
6884 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
6885 ai
->ai_addrlen
= winlen
;
6889 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
6892 ai
->ai_addrlen
= len
;
6894 xuai
= xuai
->ai_next
;
6896 freeaddrinfo(unixaires
);
6898 if (TRACE_ON(winsock
))
6900 struct WS_addrinfo
*ai
= *res
;
6903 TRACE("=> %p, flags %#x, family %d, type %d, protocol %d, len %ld, name %s, addr %s\n",
6904 ai
, ai
->ai_flags
, ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
, ai
->ai_addrlen
,
6905 ai
->ai_canonname
, debugstr_sockaddr(ai
->ai_addr
));
6910 result
= convert_eai_u2w(result
);
6912 SetLastError(result
);
6916 if (*res
) WS_freeaddrinfo(*res
);
6917 if (unixaires
) freeaddrinfo(unixaires
);
6918 return WSA_NOT_ENOUGH_MEMORY
;
6920 FIXME("getaddrinfo() failed, not found during buildtime.\n");
6925 static ADDRINFOEXW
*addrinfo_AtoW(const struct WS_addrinfo
*ai
)
6929 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(ADDRINFOEXW
)))) return NULL
;
6930 ret
->ai_flags
= ai
->ai_flags
;
6931 ret
->ai_family
= ai
->ai_family
;
6932 ret
->ai_socktype
= ai
->ai_socktype
;
6933 ret
->ai_protocol
= ai
->ai_protocol
;
6934 ret
->ai_addrlen
= ai
->ai_addrlen
;
6935 ret
->ai_canonname
= NULL
;
6936 ret
->ai_addr
= NULL
;
6937 ret
->ai_blob
= NULL
;
6938 ret
->ai_bloblen
= 0;
6939 ret
->ai_provider
= NULL
;
6940 ret
->ai_next
= NULL
;
6941 if (ai
->ai_canonname
)
6943 int len
= MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0);
6944 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
))))
6946 HeapFree(GetProcessHeap(), 0, ret
);
6949 MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
);
6953 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, ai
->ai_addrlen
)))
6955 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
6956 HeapFree(GetProcessHeap(), 0, ret
);
6959 memcpy(ret
->ai_addr
, ai
->ai_addr
, ai
->ai_addrlen
);
6964 static ADDRINFOEXW
*addrinfo_list_AtoW(const struct WS_addrinfo
*info
)
6966 ADDRINFOEXW
*ret
, *infoW
;
6968 if (!(ret
= infoW
= addrinfo_AtoW(info
))) return NULL
;
6969 while (info
->ai_next
)
6971 if (!(infoW
->ai_next
= addrinfo_AtoW(info
->ai_next
)))
6973 FreeAddrInfoExW(ret
);
6976 infoW
= infoW
->ai_next
;
6977 info
= info
->ai_next
;
6982 static struct WS_addrinfo
*addrinfo_WtoA(const struct WS_addrinfoW
*ai
)
6984 struct WS_addrinfo
*ret
;
6986 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfo
)))) return NULL
;
6987 ret
->ai_flags
= ai
->ai_flags
;
6988 ret
->ai_family
= ai
->ai_family
;
6989 ret
->ai_socktype
= ai
->ai_socktype
;
6990 ret
->ai_protocol
= ai
->ai_protocol
;
6991 ret
->ai_addrlen
= ai
->ai_addrlen
;
6992 ret
->ai_canonname
= NULL
;
6993 ret
->ai_addr
= NULL
;
6994 ret
->ai_next
= NULL
;
6995 if (ai
->ai_canonname
)
6997 int len
= WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0, NULL
, NULL
);
6998 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
7000 HeapFree(GetProcessHeap(), 0, ret
);
7003 WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
, NULL
, NULL
);
7007 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
7009 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
7010 HeapFree(GetProcessHeap(), 0, ret
);
7013 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
7018 struct getaddrinfo_args
7020 OVERLAPPED
*overlapped
;
7021 LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine
;
7022 ADDRINFOEXW
**result
;
7027 static void WINAPI
getaddrinfo_callback(TP_CALLBACK_INSTANCE
*instance
, void *context
)
7029 struct getaddrinfo_args
*args
= context
;
7030 OVERLAPPED
*overlapped
= args
->overlapped
;
7031 HANDLE event
= overlapped
->hEvent
;
7032 LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine
= args
->completion_routine
;
7033 struct WS_addrinfo
*res
;
7036 ret
= WS_getaddrinfo(args
->nodename
, args
->servname
, NULL
, &res
);
7039 *args
->result
= addrinfo_list_AtoW(res
);
7040 overlapped
->u
.Pointer
= args
->result
;
7041 WS_freeaddrinfo(res
);
7044 HeapFree(GetProcessHeap(), 0, args
->nodename
);
7045 HeapFree(GetProcessHeap(), 0, args
->servname
);
7046 HeapFree(GetProcessHeap(), 0, args
);
7048 overlapped
->Internal
= ret
;
7049 if (completion_routine
) completion_routine(ret
, 0, overlapped
);
7050 if (event
) SetEvent(event
);
7053 static int WS_getaddrinfoW(const WCHAR
*nodename
, const WCHAR
*servname
, const struct WS_addrinfo
*hints
, ADDRINFOEXW
**res
,
7054 OVERLAPPED
*overlapped
, LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine
)
7056 int ret
= EAI_MEMORY
, len
, i
;
7057 char *nodenameA
= NULL
, *servnameA
= NULL
;
7058 struct WS_addrinfo
*resA
;
7059 WCHAR
*local_nodenameW
= (WCHAR
*)nodename
;
7064 /* Is this an IDN? Most likely if any char is above the Ascii table, this
7065 * is the simplest validation possible, further validation will be done by
7066 * the native getaddrinfo() */
7067 for (i
= 0; nodename
[i
]; i
++)
7069 if (nodename
[i
] > 'z')
7074 if (hints
&& (hints
->ai_flags
& WS_AI_DISABLE_IDN_ENCODING
))
7076 /* Name requires conversion but it was disabled */
7077 ret
= WSAHOST_NOT_FOUND
;
7078 WSASetLastError(ret
);
7082 len
= IdnToAscii(0, nodename
, -1, NULL
, 0);
7085 ERR("Failed to convert %s to punycode\n", debugstr_w(nodename
));
7089 if (!(local_nodenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
)))) goto end
;
7090 IdnToAscii(0, nodename
, -1, local_nodenameW
, len
);
7093 if (local_nodenameW
)
7095 len
= WideCharToMultiByte(CP_ACP
, 0, local_nodenameW
, -1, NULL
, 0, NULL
, NULL
);
7096 if (!(nodenameA
= HeapAlloc(GetProcessHeap(), 0, len
))) goto end
;
7097 WideCharToMultiByte(CP_ACP
, 0, local_nodenameW
, -1, nodenameA
, len
, NULL
, NULL
);
7101 len
= WideCharToMultiByte(CP_ACP
, 0, servname
, -1, NULL
, 0, NULL
, NULL
);
7102 if (!(servnameA
= HeapAlloc(GetProcessHeap(), 0, len
))) goto end
;
7103 WideCharToMultiByte(CP_ACP
, 0, servname
, -1, servnameA
, len
, NULL
, NULL
);
7108 struct getaddrinfo_args
*args
;
7110 if (overlapped
->hEvent
&& completion_routine
)
7116 if (!(args
= HeapAlloc(GetProcessHeap(), 0, sizeof(*args
)))) goto end
;
7117 args
->overlapped
= overlapped
;
7118 args
->completion_routine
= completion_routine
;
7120 args
->nodename
= nodenameA
;
7121 args
->servname
= servnameA
;
7123 overlapped
->Internal
= WSAEINPROGRESS
;
7124 if (!TrySubmitThreadpoolCallback(getaddrinfo_callback
, args
, NULL
))
7126 HeapFree(GetProcessHeap(), 0, args
);
7127 ret
= GetLastError();
7131 if (local_nodenameW
!= nodename
)
7132 HeapFree(GetProcessHeap(), 0, local_nodenameW
);
7133 WSASetLastError(ERROR_IO_PENDING
);
7134 return ERROR_IO_PENDING
;
7137 ret
= WS_getaddrinfo(nodenameA
, servnameA
, hints
, &resA
);
7140 *res
= addrinfo_list_AtoW(resA
);
7141 WS_freeaddrinfo(resA
);
7145 if (local_nodenameW
!= nodename
)
7146 HeapFree(GetProcessHeap(), 0, local_nodenameW
);
7147 HeapFree(GetProcessHeap(), 0, nodenameA
);
7148 HeapFree(GetProcessHeap(), 0, servnameA
);
7152 /***********************************************************************
7153 * GetAddrInfoExW (WS2_32.@)
7155 int WINAPI
GetAddrInfoExW(const WCHAR
*name
, const WCHAR
*servname
, DWORD
namespace, GUID
*namespace_id
,
7156 const ADDRINFOEXW
*hints
, ADDRINFOEXW
**result
, struct WS_timeval
*timeout
, OVERLAPPED
*overlapped
,
7157 LPLOOKUPSERVICE_COMPLETION_ROUTINE completion_routine
, HANDLE
*handle
)
7161 TRACE("(%s %s %x %s %p %p %p %p %p %p)\n", debugstr_w(name
), debugstr_w(servname
), namespace,
7162 debugstr_guid(namespace_id
), hints
, result
, timeout
, overlapped
, completion_routine
, handle
);
7164 if (namespace != NS_DNS
)
7165 FIXME("Unsupported namespace %u\n", namespace);
7167 FIXME("Unsupported naemspace_id %s\n", debugstr_guid(namespace_id
));
7169 FIXME("Unsupported hints\n");
7171 FIXME("Unsupported timeout\n");
7173 FIXME("Unsupported cancel handle\n");
7175 ret
= WS_getaddrinfoW(name
, servname
, NULL
, result
, overlapped
, completion_routine
);
7176 if (ret
) return ret
;
7177 if (handle
) *handle
= (HANDLE
)0xdeadbeef;
7181 /***********************************************************************
7182 * GetAddrInfoExOverlappedResult (WS2_32.@)
7184 int WINAPI
GetAddrInfoExOverlappedResult(OVERLAPPED
*overlapped
)
7186 TRACE("(%p)\n", overlapped
);
7187 return overlapped
->Internal
;
7190 /***********************************************************************
7191 * GetAddrInfoExCancel (WS2_32.@)
7193 int WINAPI
GetAddrInfoExCancel(HANDLE
*handle
)
7195 FIXME("(%p)\n", handle
);
7196 return WSA_INVALID_HANDLE
;
7199 /***********************************************************************
7200 * GetAddrInfoW (WS2_32.@)
7202 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
7204 struct WS_addrinfo
*hintsA
= NULL
;
7206 int ret
= EAI_MEMORY
;
7208 TRACE("nodename %s, servname %s, hints %p, result %p\n",
7209 debugstr_w(nodename
), debugstr_w(servname
), hints
, res
);
7212 if (hints
) hintsA
= addrinfo_WtoA(hints
);
7213 ret
= WS_getaddrinfoW(nodename
, servname
, hintsA
, &resex
, NULL
, NULL
);
7214 WS_freeaddrinfo(hintsA
);
7215 if (ret
) return ret
;
7219 /* ADDRINFOEXW has layout compatible with ADDRINFOW except for ai_next field,
7220 * so we may convert it in place */
7221 *res
= (ADDRINFOW
*)resex
;
7223 ((ADDRINFOW
*)resex
)->ai_next
= (ADDRINFOW
*)resex
->ai_next
;
7224 resex
= resex
->ai_next
;
7230 /***********************************************************************
7231 * FreeAddrInfoW (WS2_32.@)
7233 void WINAPI
FreeAddrInfoW(PADDRINFOW ai
)
7238 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
7239 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
7241 HeapFree(GetProcessHeap(), 0, ai
);
7246 /***********************************************************************
7247 * FreeAddrInfoExW (WS2_32.@)
7249 void WINAPI
FreeAddrInfoExW(ADDRINFOEXW
*ai
)
7251 TRACE("(%p)\n", ai
);
7256 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
7257 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
7259 HeapFree(GetProcessHeap(), 0, ai
);
7264 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
7265 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
7267 #ifdef HAVE_GETNAMEINFO
7269 union generic_unix_sockaddr sa_u
;
7272 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
7273 serv
, servlen
, flags
);
7275 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
7278 SetLastError(WSAEFAULT
);
7279 return WSA_NOT_ENOUGH_MEMORY
;
7281 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
7282 return convert_eai_u2w(ret
);
7284 FIXME("getnameinfo() failed, not found during buildtime.\n");
7289 int WINAPI
GetNameInfoW(const SOCKADDR
*sa
, WS_socklen_t salen
, PWCHAR host
,
7290 DWORD hostlen
, PWCHAR serv
, DWORD servlen
, INT flags
)
7293 char *hostA
= NULL
, *servA
= NULL
;
7295 if (host
&& (!(hostA
= HeapAlloc(GetProcessHeap(), 0, hostlen
)))) return EAI_MEMORY
;
7296 if (serv
&& (!(servA
= HeapAlloc(GetProcessHeap(), 0, servlen
))))
7298 HeapFree(GetProcessHeap(), 0, hostA
);
7302 ret
= WS_getnameinfo(sa
, salen
, hostA
, hostlen
, servA
, servlen
, flags
);
7305 if (host
) MultiByteToWideChar(CP_ACP
, 0, hostA
, -1, host
, hostlen
);
7306 if (serv
) MultiByteToWideChar(CP_ACP
, 0, servA
, -1, serv
, servlen
);
7309 HeapFree(GetProcessHeap(), 0, hostA
);
7310 HeapFree(GetProcessHeap(), 0, servA
);
7314 /***********************************************************************
7315 * getservbyport (WS2_32.56)
7317 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
7319 struct WS_servent
* retval
= NULL
;
7320 #ifdef HAVE_GETSERVBYPORT
7321 struct servent
* serv
;
7322 char *proto_str
= NULL
;
7324 if (proto
&& *proto
)
7326 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
7328 EnterCriticalSection( &csWSgetXXXbyYYY
);
7329 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
7330 retval
= WS_dup_se(serv
);
7332 else SetLastError(WSANO_DATA
);
7333 LeaveCriticalSection( &csWSgetXXXbyYYY
);
7334 HeapFree( GetProcessHeap(), 0, proto_str
);
7336 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
7341 /***********************************************************************
7342 * gethostname (WS2_32.57)
7344 int WINAPI
WS_gethostname(char *name
, int namelen
)
7349 TRACE("name %p, len %d\n", name
, namelen
);
7353 SetLastError(WSAEFAULT
);
7354 return SOCKET_ERROR
;
7357 if (gethostname(buf
, sizeof(buf
)) != 0)
7359 SetLastError(wsaErrno());
7360 return SOCKET_ERROR
;
7363 TRACE("<- '%s'\n", buf
);
7366 WARN("Windows supports NetBIOS name length up to 15 bytes!\n");
7369 SetLastError(WSAEFAULT
);
7370 WARN("<- not enough space for hostname, required %d, got %d!\n", len
+ 1, namelen
);
7371 return SOCKET_ERROR
;
7378 /* ------------------------------------- Windows sockets extensions -- *
7380 * ------------------------------------------------------------------- */
7382 /***********************************************************************
7383 * WSAEnumNetworkEvents (WS2_32.36)
7385 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
7389 int errors
[FD_MAX_EVENTS
];
7391 TRACE("%04lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
7393 SERVER_START_REQ( get_socket_event
)
7395 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
7396 req
->service
= TRUE
;
7397 req
->c_event
= wine_server_obj_handle( hEvent
);
7398 wine_server_set_reply( req
, errors
, sizeof(errors
) );
7399 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
7404 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
7406 if (lpEvent
->lNetworkEvents
& (1 << i
))
7407 lpEvent
->iErrorCode
[i
] = errors
[i
];
7411 SetLastError(WSAEINVAL
);
7412 return SOCKET_ERROR
;
7415 /***********************************************************************
7416 * WSAEventSelect (WS2_32.39)
7418 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
7422 TRACE("%04lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
7424 SERVER_START_REQ( set_socket_event
)
7426 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
7428 req
->event
= wine_server_obj_handle( hEvent
);
7431 ret
= wine_server_call( req
);
7435 SetLastError(WSAEINVAL
);
7436 return SOCKET_ERROR
;
7439 /**********************************************************************
7440 * WSAGetOverlappedResult (WS2_32.40)
7442 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
7443 LPDWORD lpcbTransfer
, BOOL fWait
,
7448 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
7449 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
7451 if ( lpOverlapped
== NULL
)
7453 ERR( "Invalid pointer\n" );
7454 SetLastError(WSA_INVALID_PARAMETER
);
7458 status
= lpOverlapped
->Internal
;
7459 if (status
== STATUS_PENDING
)
7463 SetLastError( WSA_IO_INCOMPLETE
);
7467 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
7468 INFINITE
) == WAIT_FAILED
)
7470 status
= lpOverlapped
->Internal
;
7474 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
7477 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
7479 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
7484 /***********************************************************************
7485 * WSAAsyncSelect (WS2_32.101)
7487 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
7491 TRACE("%04lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
7493 SERVER_START_REQ( set_socket_event
)
7495 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
7498 req
->window
= wine_server_user_handle( hWnd
);
7500 ret
= wine_server_call( req
);
7504 SetLastError(WSAEINVAL
);
7505 return SOCKET_ERROR
;
7508 /***********************************************************************
7509 * WSACreateEvent (WS2_32.31)
7512 WSAEVENT WINAPI
WSACreateEvent(void)
7514 /* Create a manual-reset event, with initial state: unsignaled */
7517 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
7520 /***********************************************************************
7521 * WSACloseEvent (WS2_32.29)
7524 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
7526 TRACE ("event=%p\n", event
);
7528 return CloseHandle(event
);
7531 /***********************************************************************
7532 * WSASocketA (WS2_32.78)
7535 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
7536 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
7537 GROUP g
, DWORD dwFlags
)
7540 WSAPROTOCOL_INFOW info
;
7542 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
7543 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
7545 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
7547 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
7548 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
7549 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
7553 SetLastError(WSAEINVAL
);
7554 return SOCKET_ERROR
;
7557 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
7560 /***********************************************************************
7561 * WSASocketW (WS2_32.79)
7564 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
7565 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
7566 GROUP g
, DWORD dwFlags
)
7570 int unixaf
, unixtype
, ipxptype
= -1;
7573 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
7574 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
7577 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
7578 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
7582 err
= WSANOTINITIALISED
;
7586 /* hack for WSADuplicateSocket */
7587 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
7588 ret
= lpProtocolInfo
->dwServiceFlags3
;
7589 TRACE("\tgot duplicate %04lx\n", ret
);
7595 if (af
== FROM_PROTOCOL_INFO
|| !af
)
7596 af
= lpProtocolInfo
->iAddressFamily
;
7597 if (type
== FROM_PROTOCOL_INFO
|| !type
)
7598 type
= lpProtocolInfo
->iSocketType
;
7599 if (protocol
== FROM_PROTOCOL_INFO
|| !protocol
)
7600 protocol
= lpProtocolInfo
->iProtocol
;
7603 if (!type
&& (af
|| protocol
))
7605 int autoproto
= protocol
;
7606 WSAPROTOCOL_INFOW infow
;
7608 /* default to the first valid protocol */
7610 autoproto
= valid_protocols
[0];
7611 else if(IS_IPX_PROTO(autoproto
))
7612 autoproto
= WS_NSPROTO_IPX
;
7614 if (WS_EnterSingleProtocolW(autoproto
, &infow
))
7616 type
= infow
.iSocketType
;
7618 /* after win2003 it's no longer possible to pass AF_UNSPEC
7619 using the protocol info struct */
7620 if (!lpProtocolInfo
&& af
== WS_AF_UNSPEC
)
7621 af
= infow
.iAddressFamily
;
7626 Windows has an extension to the IPX protocol that allows one to create sockets
7627 and set the IPX packet type at the same time, to do that a caller will use
7628 a protocol like NSPROTO_IPX + <PACKET TYPE>
7630 if (IS_IPX_PROTO(protocol
))
7631 ipxptype
= protocol
- WS_NSPROTO_IPX
;
7633 /* convert the socket family, type and protocol */
7634 unixaf
= convert_af_w2u(af
);
7635 unixtype
= convert_socktype_w2u(type
);
7636 protocol
= convert_proto_w2u(protocol
);
7637 if (unixaf
== AF_UNSPEC
) unixaf
= -1;
7639 /* filter invalid parameters */
7642 /* the type could not be converted */
7643 if (type
&& unixtype
< 0)
7645 err
= WSAESOCKTNOSUPPORT
;
7649 err
= WSAEPROTONOSUPPORT
;
7654 /* both family and protocol can't be invalid */
7661 /* family could not be converted and neither socket type */
7662 if (unixtype
< 0 && af
>= 0)
7665 err
= WSAESOCKTNOSUPPORT
;
7669 err
= WSAEAFNOSUPPORT
;
7673 SERVER_START_REQ( create_socket
)
7675 req
->family
= unixaf
;
7676 req
->type
= unixtype
;
7677 req
->protocol
= protocol
;
7678 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
7679 req
->attributes
= (dwFlags
& WSA_FLAG_NO_HANDLE_INHERIT
) ? 0 : OBJ_INHERIT
;
7680 req
->flags
= dwFlags
& ~WSA_FLAG_NO_HANDLE_INHERIT
;
7681 err
= NtStatusToWSAError( wine_server_call( req
) );
7682 ret
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
7687 TRACE("\tcreated %04lx\n", ret
);
7689 set_ipx_packettype(ret
, ipxptype
);
7691 if (unixaf
== AF_INET
|| unixaf
== AF_INET6
)
7693 /* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
7694 if (unixtype
== SOCK_DGRAM
|| unixtype
== SOCK_RAW
) /* in Linux the global default can be enabled */
7695 set_dont_fragment(ret
, unixaf
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, FALSE
);
7696 else if (unixtype
== SOCK_STREAM
)
7697 set_dont_fragment(ret
, unixaf
== AF_INET6
? IPPROTO_IPV6
: IPPROTO_IP
, TRUE
);
7701 if (unixaf
== AF_INET6
)
7703 int fd
= get_sock_fd(ret
, 0, NULL
);
7706 /* IPV6_V6ONLY is set by default on Windows */
7708 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
)))
7709 WARN("\tsetting IPV6_V6ONLY failed - errno = %i\n", errno
);
7710 release_sock_fd(ret
, fd
);
7717 if (err
== WSAEACCES
) /* raw socket denied */
7719 if (type
== SOCK_RAW
)
7720 ERR_(winediag
)("Failed to create a socket of type SOCK_RAW, this requires special permissions.\n");
7722 ERR_(winediag
)("Failed to create socket, this requires special permissions.\n");
7726 WARN("\t\tfailed, error %d!\n", err
);
7728 return INVALID_SOCKET
;
7731 /***********************************************************************
7732 * WSAJoinLeaf (WS2_32.58)
7735 SOCKET WINAPI
WSAJoinLeaf(
7737 const struct WS_sockaddr
*addr
,
7739 LPWSABUF lpCallerData
,
7740 LPWSABUF lpCalleeData
,
7746 return INVALID_SOCKET
;
7749 /***********************************************************************
7750 * __WSAFDIsSet (WS2_32.151)
7752 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
7754 int i
= set
->fd_count
, ret
= 0;
7757 if (set
->fd_array
[i
] == s
)
7763 TRACE("(socket %04lx, fd_set %p, count %i) <- %d\n", s
, set
, set
->fd_count
, ret
);
7767 /***********************************************************************
7768 * WSAIsBlocking (WS2_32.114)
7770 BOOL WINAPI
WSAIsBlocking(void)
7772 /* By default WinSock should set all its sockets to non-blocking mode
7773 * and poll in PeekMessage loop when processing "blocking" ones. This
7774 * function is supposed to tell if the program is in this loop. Our
7775 * blocking calls are truly blocking so we always return FALSE.
7777 * Note: It is allowed to call this function without prior WSAStartup().
7784 /***********************************************************************
7785 * WSACancelBlockingCall (WS2_32.113)
7787 INT WINAPI
WSACancelBlockingCall(void)
7793 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
7795 FIXME("How was this called?\n");
7800 /***********************************************************************
7801 * WSASetBlockingHook (WS2_32.109)
7803 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
7805 FARPROC prev
= blocking_hook
;
7806 blocking_hook
= lpBlockFunc
;
7807 TRACE("hook %p\n", lpBlockFunc
);
7812 /***********************************************************************
7813 * WSAUnhookBlockingHook (WS2_32.110)
7815 INT WINAPI
WSAUnhookBlockingHook(void)
7817 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
7822 /* ----------------------------------- end of API stuff */
7824 /* ----------------------------------- helper functions -
7826 * TODO: Merge WS_dup_..() stuff into one function that
7827 * would operate with a generic structure containing internal
7828 * pointers (via a template of some kind).
7831 static int list_size(char** l
, int item_size
)
7836 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
7837 j
+= (i
+ 1) * sizeof(char*); }
7841 static int list_dup(char** l_src
, char** l_to
, int item_size
)
7846 for (i
= 0; l_src
[i
]; i
++) ;
7847 p
= (char *)(l_to
+ i
+ 1);
7848 for (i
= 0; l_src
[i
]; i
++)
7850 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
7851 memcpy(p
, l_src
[i
], count
);
7856 return p
- (char *)l_to
;
7861 /* create a hostent entry
7863 * Creates the entry with enough memory for the name, aliases
7864 * addresses, and the address pointers. Also copies the name
7865 * and sets up all the pointers.
7867 * NOTE: The alias and address lists must be allocated with room
7868 * for the NULL item terminating the list. This is true even if
7869 * the list has no items ("aliases" and "addresses" must be
7870 * at least "1", a truly empty list is invalid).
7872 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int aliases_size
, int addresses
, int address_length
)
7874 struct WS_hostent
*p_to
;
7876 int size
= (sizeof(struct WS_hostent
) +
7878 sizeof(char *) * aliases
+
7880 sizeof(char *) * addresses
+
7881 address_length
* (addresses
- 1)), i
;
7883 if (!(p_to
= check_buffer_he(size
))) return NULL
;
7884 memset(p_to
, 0, size
);
7886 /* Use the memory in the same way winsock does.
7887 * First set the pointer for aliases, second set the pointers for addresses.
7888 * Third fill the addresses indexes, fourth jump aliases names size.
7889 * Fifth fill the hostname.
7890 * NOTE: This method is valid for OS version's >= XP.
7892 p
= (char *)(p_to
+ 1);
7893 p_to
->h_aliases
= (char **)p
;
7894 p
+= sizeof(char *)*aliases
;
7896 p_to
->h_addr_list
= (char **)p
;
7897 p
+= sizeof(char *)*addresses
;
7899 for (i
= 0, addresses
--; i
< addresses
; i
++, p
+= address_length
)
7900 p_to
->h_addr_list
[i
] = p
;
7902 /* NOTE: h_aliases must be filled in manually because we don't know each string
7903 * size, leave these pointers NULL (already set to NULL by memset earlier).
7913 /* duplicate hostent entry
7914 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
7915 * Ditto for protoent and servent.
7917 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
7919 int i
, addresses
= 0, alias_size
= 0;
7920 struct WS_hostent
*p_to
;
7923 for( i
= 0; p_he
->h_aliases
[i
]; i
++) alias_size
+= strlen(p_he
->h_aliases
[i
]) + 1;
7924 while (p_he
->h_addr_list
[addresses
]) addresses
++;
7926 p_to
= WS_create_he(p_he
->h_name
, i
+ 1, alias_size
, addresses
+ 1, p_he
->h_length
);
7928 if (!p_to
) return NULL
;
7929 p_to
->h_addrtype
= convert_af_u2w(p_he
->h_addrtype
);
7930 p_to
->h_length
= p_he
->h_length
;
7932 for(i
= 0, p
= p_to
->h_addr_list
[0]; p_he
->h_addr_list
[i
]; i
++, p
+= p_to
->h_length
)
7933 memcpy(p
, p_he
->h_addr_list
[i
], p_to
->h_length
);
7935 /* Fill the aliases after the IP data */
7936 for(i
= 0; p_he
->h_aliases
[i
]; i
++)
7938 p_to
->h_aliases
[i
] = p
;
7939 strcpy(p
, p_he
->h_aliases
[i
]);
7946 /* ----- protoent */
7948 static struct WS_protoent
*WS_create_pe( const char *name
, char **aliases
, int prot
)
7950 struct WS_protoent
*ret
;
7951 unsigned int size
= sizeof(*ret
) + strlen(name
) + sizeof(char *) + list_size(aliases
, 0);
7953 if (!(ret
= check_buffer_pe( size
))) return NULL
;
7954 ret
->p_proto
= prot
;
7955 ret
->p_name
= (char *)(ret
+ 1);
7956 strcpy( ret
->p_name
, name
);
7957 ret
->p_aliases
= (char **)ret
->p_name
+ strlen(name
) / sizeof(char *) + 1;
7958 list_dup( aliases
, ret
->p_aliases
, 0 );
7964 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
7967 struct WS_servent
*p_to
;
7969 int size
= (sizeof(*p_se
) +
7970 strlen(p_se
->s_proto
) + 1 +
7971 strlen(p_se
->s_name
) + 1 +
7972 list_size(p_se
->s_aliases
, 0));
7974 if (!(p_to
= check_buffer_se(size
))) return NULL
;
7975 p_to
->s_port
= p_se
->s_port
;
7977 p
= (char *)(p_to
+ 1);
7979 strcpy(p
, p_se
->s_name
);
7983 strcpy(p
, p_se
->s_proto
);
7986 p_to
->s_aliases
= (char **)p
;
7987 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
7992 /***********************************************************************
7993 * WSARecv (WS2_32.67)
7995 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
7996 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
7997 LPWSAOVERLAPPED lpOverlapped
,
7998 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
8000 return WS2_recv_base(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
8001 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
, NULL
);
8004 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
8005 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
8006 struct WS_sockaddr
*lpFrom
,
8007 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
8008 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
8009 LPWSABUF lpControlBuffer
)
8011 unsigned int i
, options
;
8012 int n
, fd
, err
, overlapped
, flags
;
8013 struct ws2_async
*wsa
= NULL
, localwsa
;
8015 DWORD timeout_start
= GetTickCount();
8016 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
8018 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
8019 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
8020 (lpFromlen
? *lpFromlen
: -1),
8021 lpOverlapped
, lpCompletionRoutine
);
8023 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
8024 TRACE( "fd=%d, options=%x\n", fd
, options
);
8026 if (fd
== -1) return SOCKET_ERROR
;
8028 if (*lpFlags
& WS_MSG_OOB
)
8030 /* It's invalid to receive OOB data from an OOBINLINED socket
8031 * as OOB data is turned into normal data. */
8032 socklen_t len
= sizeof(n
);
8033 if (!getsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, (char*) &n
, &len
) && n
)
8040 overlapped
= (lpOverlapped
|| lpCompletionRoutine
) &&
8041 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
8042 if (overlapped
|| dwBufferCount
> 1)
8044 if (!(wsa
= (struct ws2_async
*)alloc_async_io( offsetof(struct ws2_async
, iovec
[dwBufferCount
]),
8054 wsa
->hSocket
= SOCKET2HANDLE(s
);
8055 wsa
->flags
= *lpFlags
;
8056 wsa
->lpFlags
= lpFlags
;
8058 wsa
->addrlen
.ptr
= lpFromlen
;
8059 wsa
->control
= lpControlBuffer
;
8060 wsa
->n_iovecs
= dwBufferCount
;
8061 wsa
->first_iovec
= 0;
8062 for (i
= 0; i
< dwBufferCount
; i
++)
8064 /* check buffer first to trigger write watches */
8065 if (IsBadWritePtr( lpBuffers
[i
].buf
, lpBuffers
[i
].len
))
8070 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
8071 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
8074 flags
= convert_flags(wsa
->flags
);
8077 n
= WS2_recv( fd
, wsa
, flags
);
8080 /* Unix-like systems return EINVAL when attempting to read OOB data from
8081 * an empty socket buffer, convert that to a Windows expected return. */
8082 if ((flags
& MSG_OOB
) && errno
== EINVAL
)
8083 errno
= EWOULDBLOCK
;
8085 if (errno
!= EAGAIN
)
8091 else if (lpNumberOfBytesRecvd
) *lpNumberOfBytesRecvd
= n
;
8095 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
8097 wsa
->user_overlapped
= lpOverlapped
;
8098 wsa
->completion_func
= lpCompletionRoutine
;
8099 release_sock_fd( s
, fd
);
8103 iosb
->u
.Status
= STATUS_PENDING
;
8104 iosb
->Information
= 0;
8106 if (wsa
->completion_func
)
8107 err
= register_async( ASYNC_TYPE_READ
, wsa
->hSocket
, &wsa
->io
, NULL
,
8108 ws2_async_apc
, wsa
, iosb
);
8110 err
= register_async( ASYNC_TYPE_READ
, wsa
->hSocket
, &wsa
->io
, lpOverlapped
->hEvent
,
8111 NULL
, (void *)cvalue
, iosb
);
8113 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
8114 SetLastError(NtStatusToWSAError( err
));
8115 return SOCKET_ERROR
;
8118 iosb
->u
.Status
= STATUS_SUCCESS
;
8119 iosb
->Information
= n
;
8120 if (!wsa
->completion_func
)
8122 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
, FALSE
);
8123 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
8124 HeapFree( GetProcessHeap(), 0, wsa
);
8126 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
8127 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
8128 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
8134 if ((err
= sock_is_blocking( s
, &is_blocking
))) goto error
;
8139 int poll_timeout
= -1;
8140 INT64 timeout
= get_rcvsnd_timeo(fd
, TRUE
);
8144 timeout
-= GetTickCount() - timeout_start
;
8145 if (timeout
< 0) poll_timeout
= 0;
8146 else poll_timeout
= timeout
<= INT_MAX
? timeout
: INT_MAX
;
8150 pfd
.events
= POLLIN
;
8151 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
8153 if (!poll_timeout
|| !poll( &pfd
, 1, poll_timeout
))
8156 /* a timeout is not fatal */
8157 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
8163 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
8164 err
= WSAEWOULDBLOCK
;
8169 TRACE(" -> %i bytes\n", n
);
8170 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
8171 release_sock_fd( s
, fd
);
8172 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
8173 SetLastError(ERROR_SUCCESS
);
8178 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
8179 release_sock_fd( s
, fd
);
8180 WARN(" -> ERROR %d\n", err
);
8181 SetLastError( err
);
8182 return SOCKET_ERROR
;
8185 /***********************************************************************
8186 * WSARecvFrom (WS2_32.69)
8188 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
8189 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
8190 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
8191 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
8194 return WS2_recv_base( s
, lpBuffers
, dwBufferCount
,
8195 lpNumberOfBytesRecvd
, lpFlags
,
8197 lpOverlapped
, lpCompletionRoutine
, NULL
);
8200 /***********************************************************************
8201 * WSCInstallProvider (WS2_32.88)
8203 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
8204 LPCWSTR lpszProviderDllPath
,
8205 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
8206 DWORD dwNumberOfEntries
,
8209 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
8210 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
8211 dwNumberOfEntries
, lpErrno
);
8217 /***********************************************************************
8218 * WSCDeinstallProvider (WS2_32.83)
8220 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
8222 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
8228 /***********************************************************************
8229 * WSAAccept (WS2_32.26)
8231 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
8232 LPCONDITIONPROC lpfnCondition
, DWORD_PTR dwCallbackData
)
8236 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
8237 /* QOS SQOS, GQOS; */
8240 SOCKADDR src_addr
, dst_addr
;
8242 TRACE("socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %ld\n",
8243 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
8245 cs
= WS_accept(s
, addr
, addrlen
);
8246 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
8247 if (!lpfnCondition
) return cs
;
8249 if (addr
&& addrlen
)
8251 CallerId
.buf
= (char *)addr
;
8252 CallerId
.len
= *addrlen
;
8256 size
= sizeof(src_addr
);
8257 WS_getpeername(cs
, &src_addr
, &size
);
8258 CallerId
.buf
= (char *)&src_addr
;
8259 CallerId
.len
= size
;
8261 CallerData
.buf
= NULL
;
8264 size
= sizeof(dst_addr
);
8265 WS_getsockname(cs
, &dst_addr
, &size
);
8267 CalleeId
.buf
= (char *)&dst_addr
;
8268 CalleeId
.len
= sizeof(dst_addr
);
8270 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
8271 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
8278 SERVER_START_REQ( set_socket_deferred
)
8280 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
8281 req
->deferred
= wine_server_obj_handle( SOCKET2HANDLE(cs
) );
8282 if ( !wine_server_call_err ( req
) )
8284 SetLastError( WSATRY_AGAIN
);
8285 WS_closesocket( cs
);
8289 return SOCKET_ERROR
;
8292 SetLastError(WSAECONNREFUSED
);
8293 return SOCKET_ERROR
;
8295 FIXME("Unknown return type from Condition function\n");
8296 SetLastError(WSAENOTSOCK
);
8297 return SOCKET_ERROR
;
8301 /***********************************************************************
8302 * WSADuplicateSocketA (WS2_32.32)
8304 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
8306 return WS_DuplicateSocket(FALSE
, s
, dwProcessId
, (LPWSAPROTOCOL_INFOW
) lpProtocolInfo
);
8309 /***********************************************************************
8310 * WSADuplicateSocketW (WS2_32.33)
8312 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
8314 return WS_DuplicateSocket(TRUE
, s
, dwProcessId
, lpProtocolInfo
);
8317 /***********************************************************************
8318 * WSAInstallServiceClassA (WS2_32.48)
8320 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
8322 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
8323 SetLastError(WSAEACCES
);
8324 return SOCKET_ERROR
;
8327 /***********************************************************************
8328 * WSAInstallServiceClassW (WS2_32.49)
8330 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
8332 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
8333 SetLastError(WSAEACCES
);
8334 return SOCKET_ERROR
;
8337 /***********************************************************************
8338 * WSARemoveServiceClass (WS2_32.70)
8340 int WINAPI
WSARemoveServiceClass(LPGUID info
)
8342 FIXME("Request to remove service %s\n", debugstr_guid(info
));
8343 SetLastError(WSATYPE_NOT_FOUND
);
8344 return SOCKET_ERROR
;
8347 /***********************************************************************
8348 * inet_ntop (WS2_32.@)
8350 PCSTR WINAPI
WS_inet_ntop( INT family
, PVOID addr
, PSTR buffer
, SIZE_T len
)
8352 #ifdef HAVE_INET_NTOP
8353 struct WS_in6_addr
*in6
;
8354 struct WS_in_addr
*in
;
8357 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
8360 SetLastError( STATUS_INVALID_PARAMETER
);
8369 pdst
= inet_ntop( AF_INET
, &in
->WS_s_addr
, buffer
, len
);
8375 pdst
= inet_ntop( AF_INET6
, in6
->WS_s6_addr
, buffer
, len
);
8379 SetLastError( WSAEAFNOSUPPORT
);
8383 if (!pdst
) SetLastError( STATUS_INVALID_PARAMETER
);
8386 FIXME( "not supported on this platform\n" );
8387 SetLastError( WSAEAFNOSUPPORT
);
8392 /***********************************************************************
8393 * inet_pton (WS2_32.@)
8395 INT WINAPI
WS_inet_pton( INT family
, PCSTR addr
, PVOID buffer
)
8397 #ifdef HAVE_INET_PTON
8400 TRACE("family %d, addr %s, buffer (%p)\n", family
, debugstr_a(addr
), buffer
);
8402 if (!addr
|| !buffer
)
8404 SetLastError(WSAEFAULT
);
8405 return SOCKET_ERROR
;
8408 unixaf
= convert_af_w2u(family
);
8409 if (unixaf
!= AF_INET
&& unixaf
!= AF_INET6
)
8411 SetLastError(WSAEAFNOSUPPORT
);
8412 return SOCKET_ERROR
;
8415 ret
= inet_pton(unixaf
, addr
, buffer
);
8416 if (ret
== -1) SetLastError(wsaErrno());
8419 FIXME( "not supported on this platform\n" );
8420 SetLastError( WSAEAFNOSUPPORT
);
8421 return SOCKET_ERROR
;
8425 /***********************************************************************
8426 * InetPtonW (WS2_32.@)
8428 INT WINAPI
InetPtonW(INT family
, PCWSTR addr
, PVOID buffer
)
8434 TRACE("family %d, addr %s, buffer (%p)\n", family
, debugstr_w(addr
), buffer
);
8438 SetLastError(WSAEFAULT
);
8439 return SOCKET_ERROR
;
8442 len
= WideCharToMultiByte(CP_ACP
, 0, addr
, -1, NULL
, 0, NULL
, NULL
);
8443 if (!(addrA
= HeapAlloc(GetProcessHeap(), 0, len
)))
8445 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8446 return SOCKET_ERROR
;
8448 WideCharToMultiByte(CP_ACP
, 0, addr
, -1, addrA
, len
, NULL
, NULL
);
8450 ret
= WS_inet_pton(family
, addrA
, buffer
);
8452 HeapFree(GetProcessHeap(), 0, addrA
);
8456 /***********************************************************************
8457 * InetNtopW (WS2_32.@)
8459 PCWSTR WINAPI
InetNtopW(INT family
, PVOID addr
, PWSTR buffer
, SIZE_T len
)
8461 char bufferA
[WS_INET6_ADDRSTRLEN
];
8464 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
8466 if (WS_inet_ntop(family
, addr
, bufferA
, sizeof(bufferA
)))
8468 if (MultiByteToWideChar(CP_ACP
, 0, bufferA
, -1, buffer
, len
))
8471 SetLastError(ERROR_INVALID_PARAMETER
);
8476 /***********************************************************************
8477 * WSAStringToAddressA (WS2_32.80)
8479 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
8481 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
8482 LPSOCKADDR lpAddress
,
8483 LPINT lpAddressLength
)
8486 LPSTR workBuffer
=NULL
,ptrPort
;
8488 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_a(AddressString
), AddressFamily
,
8489 lpProtocolInfo
, lpAddress
, lpAddressLength
);
8491 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
8495 SetLastError(WSAEINVAL
);
8496 return SOCKET_ERROR
;
8500 FIXME("ProtocolInfo not implemented.\n");
8502 workBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
8503 strlen(AddressString
) + 1);
8506 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8507 return SOCKET_ERROR
;
8510 strcpy(workBuffer
, AddressString
);
8512 switch(AddressFamily
)
8516 struct in_addr inetaddr
;
8518 /* If lpAddressLength is too small, tell caller the size we need */
8519 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
8521 *lpAddressLength
= sizeof(SOCKADDR_IN
);
8525 *lpAddressLength
= sizeof(SOCKADDR_IN
);
8526 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
8528 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= WS_AF_INET
;
8530 ptrPort
= strchr(workBuffer
, ':');
8533 /* User may have entered an IPv6 and asked to parse as IPv4 */
8534 if(strchr(ptrPort
+ 1, ':'))
8539 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= htons(atoi(ptrPort
+1));
8543 if(inet_aton(workBuffer
, &inetaddr
) > 0)
8545 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
8555 struct in6_addr inetaddr
;
8556 char *ptrAddr
= workBuffer
;
8558 /* If lpAddressLength is too small, tell caller the size we need */
8559 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
8561 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
8565 #ifdef HAVE_INET_PTON
8566 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
8567 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
8569 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_family
= WS_AF_INET6
;
8571 /* Valid IPv6 addresses can also be surrounded by [ ], and in this case
8572 * a port number may follow after like in [fd12:3456:7890::1]:12345
8573 * We need to cut the brackets and find the port if any. */
8575 if(*workBuffer
== '[')
8577 ptrPort
= strchr(workBuffer
, ']');
8580 SetLastError(WSAEINVAL
);
8581 return SOCKET_ERROR
;
8584 if (ptrPort
[1] == ':')
8585 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= htons(atoi(ptrPort
+ 2));
8588 ptrAddr
= workBuffer
+ 1;
8591 if(inet_pton(AF_INET6
, ptrAddr
, &inetaddr
) > 0)
8593 memcpy(&((LPSOCKADDR_IN6
)lpAddress
)->sin6_addr
, &inetaddr
,
8594 sizeof(struct in6_addr
));
8598 #endif /* HAVE_INET_PTON */
8604 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
8605 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
8609 HeapFree(GetProcessHeap(), 0, workBuffer
);
8613 return SOCKET_ERROR
;
8616 /***********************************************************************
8617 * WSAStringToAddressW (WS2_32.81)
8619 * FIXME: Does anybody know if this function allows using Hebrew/Arabic/Chinese... digits?
8620 * If this should be the case, it would be required to map these digits
8621 * to Unicode digits (0-9) using FoldString first.
8623 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
8625 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
8626 LPSOCKADDR lpAddress
,
8627 LPINT lpAddressLength
)
8630 LPSTR workBuffer
=NULL
;
8631 WSAPROTOCOL_INFOA infoA
;
8632 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
8634 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
8635 lpAddress
, lpAddressLength
);
8637 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
8639 /* if ProtocolInfo is available - convert to ANSI variant */
8642 lpProtoInfoA
= &infoA
;
8643 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
8645 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
8646 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
8648 SetLastError(WSAEINVAL
);
8649 return SOCKET_ERROR
;
8655 /* Translate AddressString to ANSI code page - assumes that only
8656 standard digits 0-9 are used with this API call */
8657 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
8658 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
8662 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
8663 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
8664 lpAddress
,lpAddressLength
);
8665 HeapFree( GetProcessHeap(), 0, workBuffer
);
8669 res
= WSA_NOT_ENOUGH_MEMORY
;
8675 return SOCKET_ERROR
;
8678 /***********************************************************************
8679 * WSAAddressToStringA (WS2_32.27)
8681 * See WSAAddressToStringW
8683 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
8684 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
8688 CHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
8691 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
8693 if (!sockaddr
) return SOCKET_ERROR
;
8694 if (!string
|| !lenstr
) return SOCKET_ERROR
;
8696 switch(sockaddr
->sa_family
)
8700 unsigned int long_ip
= ntohl(((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
);
8701 if (len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
8702 sprintf( buffer
, "%u.%u.%u.%u:%u",
8703 (long_ip
>> 24) & 0xff,
8704 (long_ip
>> 16) & 0xff,
8705 (long_ip
>> 8) & 0xff,
8707 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
8709 p
= strchr( buffer
, ':' );
8710 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
8715 struct WS_sockaddr_in6
*sockaddr6
= (LPSOCKADDR_IN6
) sockaddr
;
8719 if (len
< sizeof(SOCKADDR_IN6
)) return SOCKET_ERROR
;
8720 if ((sockaddr6
->sin6_port
))
8721 strcpy(buffer
, "[");
8722 slen
= strlen(buffer
);
8723 if (!WS_inet_ntop(WS_AF_INET6
, &sockaddr6
->sin6_addr
, &buffer
[slen
], sizeof(buffer
) - slen
))
8725 SetLastError(WSAEINVAL
);
8726 return SOCKET_ERROR
;
8728 if ((sockaddr6
->sin6_scope_id
))
8729 sprintf(buffer
+strlen(buffer
), "%%%u", sockaddr6
->sin6_scope_id
);
8730 if ((sockaddr6
->sin6_port
))
8731 sprintf(buffer
+strlen(buffer
), "]:%u", ntohs(sockaddr6
->sin6_port
));
8736 SetLastError(WSAEINVAL
);
8737 return SOCKET_ERROR
;
8740 size
= strlen( buffer
) + 1;
8745 SetLastError(WSAEFAULT
);
8746 return SOCKET_ERROR
;
8749 TRACE("=> %s,%u bytes\n", debugstr_a(buffer
), size
);
8751 strcpy( string
, buffer
);
8755 /***********************************************************************
8756 * WSAAddressToStringW (WS2_32.28)
8758 * Convert a sockaddr address into a readable address string.
8761 * sockaddr [I] Pointer to a sockaddr structure.
8762 * len [I] Size of the sockaddr structure.
8763 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
8764 * string [I/O] Pointer to a buffer to receive the address string.
8765 * lenstr [I/O] Size of the receive buffer in WCHARs.
8769 * Failure: SOCKET_ERROR
8772 * The 'info' parameter is ignored.
8774 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
8775 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
8780 WCHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
8783 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
8786 ret
= WSAAddressToStringA(sockaddr
, len
, NULL
, bufAddr
, &size
);
8788 if (ret
) return ret
;
8790 MultiByteToWideChar(CP_ACP
, 0, bufAddr
, size
, buffer
, ARRAY_SIZE(buffer
));
8795 SetLastError(WSAEFAULT
);
8796 return SOCKET_ERROR
;
8799 TRACE("=> %s,%u bytes\n", debugstr_w(buffer
), size
);
8801 lstrcpyW( string
, buffer
);
8805 /***********************************************************************
8806 * WSAEnumNameSpaceProvidersA (WS2_32.34)
8808 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
8810 FIXME( "(%p %p) Stub!\n", len
, buffer
);
8814 /***********************************************************************
8815 * WSAEnumNameSpaceProvidersW (WS2_32.35)
8817 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
8819 FIXME( "(%p %p) Stub!\n", len
, buffer
);
8823 /***********************************************************************
8824 * WSAGetQOSByName (WS2_32.41)
8826 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
8828 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
8832 /***********************************************************************
8833 * WSAGetServiceClassInfoA (WS2_32.42)
8835 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
8836 LPWSASERVICECLASSINFOA info
)
8838 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
8840 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8841 return SOCKET_ERROR
;
8844 /***********************************************************************
8845 * WSAGetServiceClassInfoW (WS2_32.43)
8847 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
8848 LPWSASERVICECLASSINFOW info
)
8850 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
8852 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8853 return SOCKET_ERROR
;
8856 /***********************************************************************
8857 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
8859 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
8861 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
8862 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8863 return SOCKET_ERROR
;
8866 /***********************************************************************
8867 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
8869 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
8871 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
8872 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8873 return SOCKET_ERROR
;
8876 /***********************************************************************
8877 * WSALookupServiceBeginA (WS2_32.59)
8879 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
8880 DWORD dwControlFlags
,
8883 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
8885 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8886 return SOCKET_ERROR
;
8889 /***********************************************************************
8890 * WSALookupServiceBeginW (WS2_32.60)
8892 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
8893 DWORD dwControlFlags
,
8896 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
8898 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
8899 return SOCKET_ERROR
;
8902 /***********************************************************************
8903 * WSALookupServiceEnd (WS2_32.61)
8905 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
8907 FIXME("(%p) Stub!\n", lookup
);
8911 /***********************************************************************
8912 * WSALookupServiceNextA (WS2_32.62)
8914 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
8916 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
8917 SetLastError(WSA_E_NO_MORE
);
8918 return SOCKET_ERROR
;
8921 /***********************************************************************
8922 * WSALookupServiceNextW (WS2_32.63)
8924 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
8926 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
8927 SetLastError(WSA_E_NO_MORE
);
8928 return SOCKET_ERROR
;
8931 /***********************************************************************
8932 * WSANtohl (WS2_32.64)
8934 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
8936 TRACE( "(%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
8938 if (!lphostlong
) return WSAEFAULT
;
8940 *lphostlong
= ntohl( netlong
);
8944 /***********************************************************************
8945 * WSANtohs (WS2_32.65)
8947 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
8949 TRACE( "(%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
8951 if (!lphostshort
) return WSAEFAULT
;
8953 *lphostshort
= ntohs( netshort
);
8957 /***********************************************************************
8958 * WSAProviderConfigChange (WS2_32.66)
8960 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
8961 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
8963 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
8964 return SOCKET_ERROR
;
8967 /***********************************************************************
8968 * WSARecvDisconnect (WS2_32.68)
8970 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
8972 TRACE( "(%04lx %p)\n", s
, disconnectdata
);
8974 return WS_shutdown( s
, SD_RECEIVE
);
8977 /***********************************************************************
8978 * WSASetServiceA (WS2_32.76)
8980 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
8982 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
8986 /***********************************************************************
8987 * WSASetServiceW (WS2_32.77)
8989 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
8991 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
8995 /***********************************************************************
8996 * WSCEnableNSProvider (WS2_32.84)
8998 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
9000 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
9004 /***********************************************************************
9005 * WSCGetProviderPath (WS2_32.86)
9007 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
9009 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
9011 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
9013 *errcode
= WSAEINVAL
;
9014 return SOCKET_ERROR
;
9017 /***********************************************************************
9018 * WSCInstallNameSpace (WS2_32.87)
9020 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
9021 DWORD version
, LPGUID provider
)
9023 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
9024 namespace, version
, debugstr_guid(provider
) );
9028 /***********************************************************************
9029 * WSCUnInstallNameSpace (WS2_32.89)
9031 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
9033 FIXME("(%s) Stub!\n", debugstr_guid(lpProviderId
));
9037 /***********************************************************************
9038 * WSCWriteProviderOrder (WS2_32.91)
9040 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
9042 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);
9046 /***********************************************************************
9047 * WSANSPIoctl (WS2_32.91)
9049 INT WINAPI
WSANSPIoctl( HANDLE hLookup
, DWORD dwControlCode
, LPVOID lpvInBuffer
,
9050 DWORD cbInBuffer
, LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
9051 LPDWORD lpcbBytesReturned
, LPWSACOMPLETION lpCompletion
)
9053 FIXME("(%p, 0x%08x, %p, 0x%08x, %p, 0x%08x, %p, %p) Stub!\n", hLookup
, dwControlCode
,
9054 lpvInBuffer
, cbInBuffer
, lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpCompletion
);
9055 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
9056 return SOCKET_ERROR
;
9059 /*****************************************************************************
9060 * WSAEnumProtocolsA [WS2_32.@]
9062 * see function WSAEnumProtocolsW
9064 INT WINAPI
WSAEnumProtocolsA( LPINT protocols
, LPWSAPROTOCOL_INFOA buffer
, LPDWORD len
)
9066 return WS_EnumProtocols( FALSE
, protocols
, (LPWSAPROTOCOL_INFOW
) buffer
, len
);
9069 /*****************************************************************************
9070 * WSAEnumProtocolsW [WS2_32.@]
9072 * Retrieves information about specified set of active network protocols.
9075 * protocols [I] Pointer to null-terminated array of protocol id's. NULL
9076 * retrieves information on all available protocols.
9077 * buffer [I] Pointer to a buffer to be filled with WSAPROTOCOL_INFO
9079 * len [I/O] Pointer to a variable specifying buffer size. On output
9080 * the variable holds the number of bytes needed when the
9081 * specified size is too small.
9084 * Success: number of WSAPROTOCOL_INFO structures in buffer.
9085 * Failure: SOCKET_ERROR
9088 * NT4SP5 does not return SPX if protocols == NULL
9091 * - NT4SP5 returns in addition these list of NETBIOS protocols
9092 * (address family 17), each entry two times one for socket type 2 and 5
9094 * iProtocol szProtocol
9095 * 0x80000000 \Device\NwlnkNb
9096 * 0xfffffffa \Device\NetBT_CBENT7
9097 * 0xfffffffb \Device\Nbf_CBENT7
9098 * 0xfffffffc \Device\NetBT_NdisWan5
9099 * 0xfffffffd \Device\NetBT_El9202
9100 * 0xfffffffe \Device\Nbf_El9202
9101 * 0xffffffff \Device\Nbf_NdisWan4
9103 * - there is no check that the operating system supports the returned
9106 INT WINAPI
WSAEnumProtocolsW( LPINT protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
)
9108 return WS_EnumProtocols( TRUE
, protocols
, buffer
, len
);
9111 /*****************************************************************************
9112 * WSCEnumProtocols [WS2_32.@]
9115 * protocols [I] Null-terminated array of iProtocol values.
9116 * buffer [O] Buffer of WSAPROTOCOL_INFOW structures.
9117 * len [I/O] Size of buffer on input/output.
9118 * errno [O] Error code.
9121 * Success: number of protocols to be reported on.
9122 * Failure: SOCKET_ERROR. error is in errno.
9125 * Doesn't supply info on layered protocols.
9128 INT WINAPI
WSCEnumProtocols( LPINT protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
, LPINT err
)
9130 INT ret
= WSAEnumProtocolsW( protocols
, buffer
, len
);
9132 if (ret
== SOCKET_ERROR
) *err
= WSAENOBUFS
;