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>
39 #ifdef HAVE_SYS_IOCTL_H
40 # include <sys/ioctl.h>
42 #ifdef HAVE_SYS_FILIO_H
43 # include <sys/filio.h>
45 #ifdef HAVE_SYS_SOCKIO_H
46 # include <sys/sockio.h>
50 # include <sys/so_ioctl.h>
53 #ifdef HAVE_SYS_PARAM_H
54 # include <sys/param.h>
60 #ifdef HAVE_SYS_WAIT_H
61 # include <sys/wait.h>
66 #ifdef HAVE_SYS_SOCKET_H
67 #include <sys/socket.h>
69 #ifdef HAVE_NETINET_IN_H
70 # include <netinet/in.h>
72 #ifdef HAVE_NETINET_TCP_H
73 # include <netinet/tcp.h>
75 #ifdef HAVE_ARPA_INET_H
76 # include <arpa/inet.h>
88 #ifdef HAVE_ARPA_NAMESER_H
89 # include <arpa/nameser.h>
97 #ifdef HAVE_LINUX_FILTER_H
98 # include <linux/filter.h>
101 #ifdef HAVE_NETIPX_IPX_H
102 # 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>
115 #ifdef HAVE_LINUX_IRDA_H
116 # ifdef HAVE_LINUX_TYPES_H
117 # include <linux/types.h>
119 # include <linux/irda.h>
126 #ifdef HAVE_SYS_POLL_H
127 # include <sys/poll.h>
129 #ifdef HAVE_SYS_TIME_H
130 # include <sys/time.h>
133 #define NONAMELESSUNION
134 #define NONAMELESSSTRUCT
135 #include "ntstatus.h"
136 #define WIN32_NO_STATUS
141 #include "winerror.h"
143 #include "winsock2.h"
145 #include "ws2tcpip.h"
148 #include "wshisotp.h"
152 #define USE_WC_PREFIX /* For CMSG_DATA */
153 #include "iphlpapi.h"
154 #include "wine/server.h"
155 #include "wine/debug.h"
156 #include "wine/exception.h"
157 #include "wine/unicode.h"
160 # include "wsnwlink.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 WINE_DEFAULT_DEBUG_CHANNEL(winsock
);
177 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
179 /* names of the protocols */
180 static const WCHAR NameIpxW
[] = {'I', 'P', 'X', '\0'};
181 static const WCHAR NameSpxW
[] = {'S', 'P', 'X', '\0'};
182 static const WCHAR NameSpxIIW
[] = {'S', 'P', 'X', ' ', 'I', 'I', '\0'};
183 static const WCHAR NameTcpW
[] = {'T', 'C', 'P', '/', 'I', 'P', '\0'};
184 static const WCHAR NameUdpW
[] = {'U', 'D', 'P', '/', 'I', 'P', '\0'};
186 /* Taken from Win2k */
187 static const GUID ProviderIdIP
= { 0xe70f1aa0, 0xab8b, 0x11cf,
188 { 0x8c, 0xa3, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } };
189 static const GUID ProviderIdIPX
= { 0x11058240, 0xbe47, 0x11cf,
190 { 0x95, 0xc8, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } };
191 static const GUID ProviderIdSPX
= { 0x11058241, 0xbe47, 0x11cf,
192 { 0x95, 0xc8, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } };
194 static const INT valid_protocols
[] =
204 #if defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER)
205 # define LINUX_BOUND_IF
206 struct interface_filter
{
207 struct sock_filter iface_memaddr
;
208 struct sock_filter iface_rule
;
209 struct sock_filter ip_memaddr
;
210 struct sock_filter ip_rule
;
211 struct sock_filter return_keep
;
212 struct sock_filter return_dump
;
214 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
215 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
216 /sizeof(struct sock_filter)
217 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
218 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
219 /sizeof(struct sock_filter)
220 # define FILTER_JUMP_NEXT() (u_char)(0)
221 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
222 static struct interface_filter generic_interface_filter
= {
223 /* This filter rule allows incoming packets on the specified interface, which works for all
224 * remotely generated packets and for locally generated broadcast packets. */
225 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
226 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
227 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
228 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
229 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
230 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
231 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
232 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
234 #endif /* LINUX_BOUND_IF */
237 * The actual definition of WSASendTo, wrapped in a different function name
238 * so that internal calls from ws2_32 itself will not trigger programs like
239 * Garena, which hooks WSASendTo/WSARecvFrom calls.
241 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
242 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
243 const struct WS_sockaddr
*to
, int tolen
,
244 LPWSAOVERLAPPED lpOverlapped
,
245 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
248 * Internal fundamental receive function, essentially WSARecvFrom with an
249 * additional parameter to support message control headers.
251 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
252 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
253 struct WS_sockaddr
*lpFrom
,
254 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
255 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
256 LPWSABUF lpControlBuffer
);
258 /* critical section to protect some non-reentrant net function */
259 static CRITICAL_SECTION csWSgetXXXbyYYY
;
260 static CRITICAL_SECTION_DEBUG critsect_debug
=
262 0, 0, &csWSgetXXXbyYYY
,
263 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
264 0, 0, { (DWORD_PTR
)(__FILE__
": csWSgetXXXbyYYY") }
266 static CRITICAL_SECTION csWSgetXXXbyYYY
= { &critsect_debug
, -1, 0, 0, 0, 0 };
268 union generic_unix_sockaddr
270 struct sockaddr addr
;
271 char data
[128]; /* should be big enough for all families */
274 static inline const char *debugstr_sockaddr( const struct WS_sockaddr
*a
)
276 if (!a
) return "(nil)";
277 switch (a
->sa_family
)
280 return wine_dbg_sprintf("{ family AF_INET, address %s, port %d }",
281 inet_ntoa(((const struct sockaddr_in
*)a
)->sin_addr
),
282 ntohs(((const struct sockaddr_in
*)a
)->sin_port
));
287 struct WS_sockaddr_in6
*sin
= (struct WS_sockaddr_in6
*)a
;
289 p
= WS_inet_ntop( WS_AF_INET6
, &sin
->sin6_addr
, buf
, sizeof(buf
) );
291 p
= "(unknown IPv6 address)";
292 return wine_dbg_sprintf("{ family AF_INET6, address %s, port %d }",
293 p
, ntohs(sin
->sin6_port
));
299 memcpy( &addr
, ((const SOCKADDR_IRDA
*)a
)->irdaDeviceID
, sizeof(addr
) );
300 addr
= ntohl( addr
);
301 return wine_dbg_sprintf("{ family AF_IRDA, addr %08x, name %s }",
303 ((const SOCKADDR_IRDA
*)a
)->irdaServiceName
);
306 return wine_dbg_sprintf("{ family %d }", a
->sa_family
);
310 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
311 #define SOCKET2HANDLE(s) ((HANDLE)(s))
312 #define HANDLE2SOCKET(h) ((SOCKET)(h))
314 /****************************************************************
315 * Async IO declarations
316 ****************************************************************/
318 typedef struct ws2_async
322 LPWSAOVERLAPPED user_overlapped
;
323 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func
;
324 IO_STATUS_BLOCK local_iosb
;
325 struct WS_sockaddr
*addr
;
328 int val
; /* for send operations */
329 int *ptr
; /* for recv operations */
334 unsigned int n_iovecs
;
335 unsigned int first_iovec
;
336 struct iovec iovec
[1];
339 typedef struct ws2_accept_async
341 HANDLE listen_socket
;
342 HANDLE accept_socket
;
343 LPOVERLAPPED user_overlapped
;
345 PVOID buf
; /* buffer to write data to */
349 struct ws2_async
*read
;
352 /****************************************************************/
354 /* ----------------------------------- internal data */
356 /* ws_... struct conversion flags */
358 typedef struct /* WSAAsyncSelect() control struct */
360 HANDLE service
, event
, sock
;
366 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
367 #define WS_MAX_UDP_DATAGRAM 1024
368 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
370 /* hostent's, servent's and protent's are stored in one buffer per thread,
371 * as documented on MSDN for the functions that return any of the buffers */
372 struct per_thread_data
375 struct WS_hostent
*he_buffer
;
376 struct WS_servent
*se_buffer
;
377 struct WS_protoent
*pe_buffer
;
383 /* internal: routing description information */
390 static INT num_startup
; /* reference counter */
391 static FARPROC blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
393 /* function prototypes */
394 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int aliases_size
, int addresses
, int address_length
);
395 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
396 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
);
397 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
398 static int ws_protocol_info(SOCKET s
, int unicode
, WSAPROTOCOL_INFOW
*buffer
, int *size
);
400 int WSAIOCTL_GetInterfaceCount(void);
401 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
403 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
, ULONG Information
);
405 #define MAP_OPTION(opt) { WS_##opt, opt }
407 static const int ws_sock_map
[][2] =
409 MAP_OPTION( SO_DEBUG
),
410 MAP_OPTION( SO_ACCEPTCONN
),
411 MAP_OPTION( SO_REUSEADDR
),
412 MAP_OPTION( SO_KEEPALIVE
),
413 MAP_OPTION( SO_DONTROUTE
),
414 MAP_OPTION( SO_BROADCAST
),
415 MAP_OPTION( SO_LINGER
),
416 MAP_OPTION( SO_OOBINLINE
),
417 MAP_OPTION( SO_SNDBUF
),
418 MAP_OPTION( SO_RCVBUF
),
419 MAP_OPTION( SO_ERROR
),
420 MAP_OPTION( SO_TYPE
),
422 MAP_OPTION( SO_RCVTIMEO
),
425 MAP_OPTION( SO_SNDTIMEO
),
429 static const int ws_tcp_map
[][2] =
432 MAP_OPTION( TCP_NODELAY
),
436 static const int ws_ip_map
[][2] =
438 MAP_OPTION( IP_MULTICAST_IF
),
439 MAP_OPTION( IP_MULTICAST_TTL
),
440 MAP_OPTION( IP_MULTICAST_LOOP
),
441 MAP_OPTION( IP_ADD_MEMBERSHIP
),
442 MAP_OPTION( IP_DROP_MEMBERSHIP
),
443 MAP_OPTION( IP_OPTIONS
),
445 MAP_OPTION( IP_HDRINCL
),
447 MAP_OPTION( IP_TOS
),
448 MAP_OPTION( IP_TTL
),
450 MAP_OPTION( IP_PKTINFO
),
453 MAP_OPTION( IP_UNICAST_IF
),
457 static const int ws_ipv6_map
[][2] =
459 #ifdef IPV6_ADD_MEMBERSHIP
460 MAP_OPTION( IPV6_ADD_MEMBERSHIP
),
462 #ifdef IPV6_DROP_MEMBERSHIP
463 MAP_OPTION( IPV6_DROP_MEMBERSHIP
),
465 MAP_OPTION( IPV6_MULTICAST_IF
),
466 MAP_OPTION( IPV6_MULTICAST_HOPS
),
467 MAP_OPTION( IPV6_MULTICAST_LOOP
),
468 MAP_OPTION( IPV6_UNICAST_HOPS
),
469 MAP_OPTION( IPV6_V6ONLY
),
470 #ifdef IPV6_UNICAST_IF
471 MAP_OPTION( IPV6_UNICAST_IF
),
475 static const int ws_af_map
[][2] =
477 MAP_OPTION( AF_UNSPEC
),
478 MAP_OPTION( AF_INET
),
479 MAP_OPTION( AF_INET6
),
481 MAP_OPTION( AF_IPX
),
484 MAP_OPTION( AF_IRDA
),
486 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
489 static const int ws_socktype_map
[][2] =
491 MAP_OPTION( SOCK_DGRAM
),
492 MAP_OPTION( SOCK_STREAM
),
493 MAP_OPTION( SOCK_RAW
),
494 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
497 static const int ws_proto_map
[][2] =
499 MAP_OPTION( IPPROTO_IP
),
500 MAP_OPTION( IPPROTO_TCP
),
501 MAP_OPTION( IPPROTO_UDP
),
502 MAP_OPTION( IPPROTO_ICMP
),
503 MAP_OPTION( IPPROTO_IGMP
),
504 MAP_OPTION( IPPROTO_RAW
),
505 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
508 static const int ws_aiflag_map
[][2] =
510 MAP_OPTION( AI_PASSIVE
),
511 MAP_OPTION( AI_CANONNAME
),
512 MAP_OPTION( AI_NUMERICHOST
),
513 MAP_OPTION( AI_ADDRCONFIG
),
516 static const int ws_niflag_map
[][2] =
518 MAP_OPTION( NI_NOFQDN
),
519 MAP_OPTION( NI_NUMERICHOST
),
520 MAP_OPTION( NI_NAMEREQD
),
521 MAP_OPTION( NI_NUMERICSERV
),
522 MAP_OPTION( NI_DGRAM
),
525 static const int ws_eai_map
[][2] =
527 MAP_OPTION( EAI_AGAIN
),
528 MAP_OPTION( EAI_BADFLAGS
),
529 MAP_OPTION( EAI_FAIL
),
530 MAP_OPTION( EAI_FAMILY
),
531 MAP_OPTION( EAI_MEMORY
),
532 /* Note: EAI_NODATA is deprecated, but still
533 * used by Windows and Linux... We map the newer
534 * EAI_NONAME to EAI_NODATA for now until Windows
538 MAP_OPTION( EAI_NODATA
),
541 { WS_EAI_NODATA
, EAI_NONAME
},
544 MAP_OPTION( EAI_SERVICE
),
545 MAP_OPTION( EAI_SOCKTYPE
),
549 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
551 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
552 static inline WSACMSGHDR
*fill_control_message(int level
, int type
, WSACMSGHDR
*current
, ULONG
*maxsize
, void *data
, int len
)
554 ULONG msgsize
= sizeof(WSACMSGHDR
) + WSA_CMSG_ALIGN(len
);
555 char *ptr
= (char *) current
+ sizeof(WSACMSGHDR
);
557 /* Make sure there is at least enough room for this entry */
558 if (msgsize
> *maxsize
)
561 /* Fill in the entry */
562 current
->cmsg_len
= sizeof(WSACMSGHDR
) + len
;
563 current
->cmsg_level
= level
;
564 current
->cmsg_type
= type
;
565 memcpy(ptr
, data
, len
);
566 /* Return the pointer to where next entry should go */
567 return (WSACMSGHDR
*) (ptr
+ WSA_CMSG_ALIGN(len
));
570 static inline int convert_control_headers(struct msghdr
*hdr
, WSABUF
*control
)
573 WSACMSGHDR
*cmsg_win
= (WSACMSGHDR
*) control
->buf
, *ptr
;
574 ULONG ctlsize
= control
->len
;
575 struct cmsghdr
*cmsg_unix
;
578 /* Loop over all the headers, converting as appropriate */
579 for (cmsg_unix
= CMSG_FIRSTHDR(hdr
); cmsg_unix
!= NULL
; cmsg_unix
= CMSG_NXTHDR(hdr
, cmsg_unix
))
581 switch(cmsg_unix
->cmsg_level
)
584 switch(cmsg_unix
->cmsg_type
)
588 /* Convert the Unix IP_PKTINFO structure to the Windows version */
589 struct in_pktinfo
*data_unix
= (struct in_pktinfo
*) CMSG_DATA(cmsg_unix
);
590 struct WS_in_pktinfo data_win
;
592 memcpy(&data_win
.ipi_addr
,&data_unix
->ipi_addr
.s_addr
,4); /* 4 bytes = 32 address bits */
593 data_win
.ipi_ifindex
= data_unix
->ipi_ifindex
;
594 ptr
= fill_control_message(WS_IPPROTO_IP
, WS_IP_PKTINFO
, ptr
, &ctlsize
,
595 (void*)&data_win
, sizeof(data_win
));
596 if (!ptr
) goto error
;
599 FIXME("Unhandled IPPROTO_IP message header type %d\n", cmsg_unix
->cmsg_type
);
604 FIXME("Unhandled message header level %d\n", cmsg_unix
->cmsg_level
);
610 /* Set the length of the returned control headers */
611 control
->len
= (ptr
== NULL
? 0 : (char*)ptr
- (char*)cmsg_win
);
612 return (ptr
!= NULL
);
613 #else /* IP_PKTINFO */
616 #endif /* IP_PKTINFO */
618 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
620 /* ----------------------------------- error handling */
622 static NTSTATUS
sock_get_ntstatus( int err
)
626 case EBADF
: return STATUS_INVALID_HANDLE
;
627 case EBUSY
: return STATUS_DEVICE_BUSY
;
629 case EACCES
: return STATUS_ACCESS_DENIED
;
630 case EFAULT
: return STATUS_NO_MEMORY
;
631 case EINVAL
: return STATUS_INVALID_PARAMETER
;
633 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
634 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
635 case EINPROGRESS
: return STATUS_PENDING
;
636 case EALREADY
: return STATUS_NETWORK_BUSY
;
637 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
638 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
639 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
640 case EPROTONOSUPPORT
:
641 case ESOCKTNOSUPPORT
:
644 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
645 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
646 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
647 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
648 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
649 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
650 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
651 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
652 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
653 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
654 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
656 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
657 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
659 case 0: return STATUS_SUCCESS
;
661 WARN("Unknown errno %d!\n", err
);
662 return STATUS_UNSUCCESSFUL
;
666 static UINT
sock_get_error( int err
)
670 case EINTR
: return WSAEINTR
;
671 case EBADF
: return WSAEBADF
;
673 case EACCES
: return WSAEACCES
;
674 case EFAULT
: return WSAEFAULT
;
675 case EINVAL
: return WSAEINVAL
;
676 case EMFILE
: return WSAEMFILE
;
677 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
678 case EINPROGRESS
: return WSAEINPROGRESS
;
679 case EALREADY
: return WSAEALREADY
;
680 case ENOTSOCK
: return WSAENOTSOCK
;
681 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
682 case EMSGSIZE
: return WSAEMSGSIZE
;
683 case EPROTOTYPE
: return WSAEPROTOTYPE
;
684 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
685 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
686 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
687 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
688 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
689 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
690 case EADDRINUSE
: return WSAEADDRINUSE
;
691 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
692 case ENETDOWN
: return WSAENETDOWN
;
693 case ENETUNREACH
: return WSAENETUNREACH
;
694 case ENETRESET
: return WSAENETRESET
;
695 case ECONNABORTED
: return WSAECONNABORTED
;
697 case ECONNRESET
: return WSAECONNRESET
;
698 case ENOBUFS
: return WSAENOBUFS
;
699 case EISCONN
: return WSAEISCONN
;
700 case ENOTCONN
: return WSAENOTCONN
;
701 case ESHUTDOWN
: return WSAESHUTDOWN
;
702 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
703 case ETIMEDOUT
: return WSAETIMEDOUT
;
704 case ECONNREFUSED
: return WSAECONNREFUSED
;
705 case ELOOP
: return WSAELOOP
;
706 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
707 case EHOSTDOWN
: return WSAEHOSTDOWN
;
708 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
709 case ENOTEMPTY
: return WSAENOTEMPTY
;
711 case EPROCLIM
: return WSAEPROCLIM
;
714 case EUSERS
: return WSAEUSERS
;
717 case EDQUOT
: return WSAEDQUOT
;
720 case ESTALE
: return WSAESTALE
;
723 case EREMOTE
: return WSAEREMOTE
;
726 /* just in case we ever get here and there are no problems */
729 WARN("Unknown errno %d!\n", err
);
730 return WSAEOPNOTSUPP
;
734 static UINT
wsaErrno(void)
736 int loc_errno
= errno
;
737 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
739 return sock_get_error( loc_errno
);
742 /* most ws2 overlapped functions return an ntstatus-based error code */
743 static NTSTATUS
wsaErrStatus(void)
745 int loc_errno
= errno
;
746 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
748 return sock_get_ntstatus(loc_errno
);
751 static UINT
wsaHerrno(int loc_errno
)
753 WARN("h_errno %d.\n", loc_errno
);
757 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
758 case TRY_AGAIN
: return WSATRY_AGAIN
;
759 case NO_RECOVERY
: return WSANO_RECOVERY
;
760 case NO_DATA
: return WSANO_DATA
;
761 case ENOBUFS
: return WSAENOBUFS
;
765 WARN("Unknown h_errno %d!\n", loc_errno
);
766 return WSAEOPNOTSUPP
;
770 static inline DWORD
NtStatusToWSAError( const DWORD status
)
772 /* We only need to cover the status codes set by server async request handling */
776 case STATUS_SUCCESS
: wserr
= 0; break;
777 case STATUS_PENDING
: wserr
= WSA_IO_PENDING
; break;
778 case STATUS_OBJECT_TYPE_MISMATCH
: wserr
= WSAENOTSOCK
; break;
779 case STATUS_INVALID_HANDLE
: wserr
= WSAEBADF
; break;
780 case STATUS_INVALID_PARAMETER
: wserr
= WSAEINVAL
; break;
781 case STATUS_PIPE_DISCONNECTED
: wserr
= WSAESHUTDOWN
; break;
782 case STATUS_NETWORK_BUSY
: wserr
= WSAEALREADY
; break;
783 case STATUS_NETWORK_UNREACHABLE
: wserr
= WSAENETUNREACH
; break;
784 case STATUS_CONNECTION_REFUSED
: wserr
= WSAECONNREFUSED
; break;
785 case STATUS_CONNECTION_DISCONNECTED
: wserr
= WSAENOTCONN
; break;
786 case STATUS_CONNECTION_RESET
: wserr
= WSAECONNRESET
; break;
787 case STATUS_CONNECTION_ABORTED
: wserr
= WSAECONNABORTED
; break;
788 case STATUS_CANCELLED
: wserr
= WSA_OPERATION_ABORTED
; break;
789 case STATUS_ADDRESS_ALREADY_ASSOCIATED
: wserr
= WSAEADDRINUSE
; break;
790 case STATUS_IO_TIMEOUT
:
791 case STATUS_TIMEOUT
: wserr
= WSAETIMEDOUT
; break;
792 case STATUS_NO_MEMORY
: wserr
= WSAEFAULT
; break;
793 case STATUS_ACCESS_DENIED
: wserr
= WSAEACCES
; break;
794 case STATUS_TOO_MANY_OPENED_FILES
: wserr
= WSAEMFILE
; break;
795 case STATUS_CANT_WAIT
: wserr
= WSAEWOULDBLOCK
; break;
796 case STATUS_BUFFER_OVERFLOW
: wserr
= WSAEMSGSIZE
; break;
797 case STATUS_NOT_SUPPORTED
: wserr
= WSAEOPNOTSUPP
; break;
798 case STATUS_HOST_UNREACHABLE
: wserr
= WSAEHOSTUNREACH
; break;
801 wserr
= RtlNtStatusToDosError( status
);
802 FIXME( "Status code %08x converted to DOS error code %x\n", status
, wserr
);
807 /* set last error code from NT status without mapping WSA errors */
808 static inline unsigned int set_error( unsigned int err
)
812 err
= NtStatusToWSAError( err
);
818 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
821 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
826 static inline void release_sock_fd( SOCKET s
, int fd
)
828 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
831 static void _enable_event( HANDLE s
, unsigned int event
,
832 unsigned int sstate
, unsigned int cstate
)
834 SERVER_START_REQ( enable_socket_event
)
836 req
->handle
= wine_server_obj_handle( s
);
838 req
->sstate
= sstate
;
839 req
->cstate
= cstate
;
840 wine_server_call( req
);
845 static NTSTATUS
_is_blocking(SOCKET s
, BOOL
*ret
)
848 SERVER_START_REQ( get_socket_event
)
850 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
851 req
->service
= FALSE
;
853 status
= wine_server_call( req
);
854 *ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
860 static unsigned int _get_sock_mask(SOCKET s
)
863 SERVER_START_REQ( get_socket_event
)
865 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
866 req
->service
= FALSE
;
868 wine_server_call( req
);
875 static void _sync_sock_state(SOCKET s
)
878 /* do a dummy wineserver request in order to let
879 the wineserver run through its select loop once */
880 (void)_is_blocking(s
, &dummy
);
883 static int _get_sock_error(SOCKET s
, unsigned int bit
)
885 int events
[FD_MAX_EVENTS
];
887 SERVER_START_REQ( get_socket_event
)
889 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
890 req
->service
= FALSE
;
892 wine_server_set_reply( req
, events
, sizeof(events
) );
893 wine_server_call( req
);
899 static struct per_thread_data
*get_per_thread_data(void)
901 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
902 /* lazy initialization */
905 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
906 NtCurrentTeb()->WinSockData
= ptb
;
911 static void free_per_thread_data(void)
913 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
917 /* delete scratch buffers */
918 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
919 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
920 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
921 ptb
->he_buffer
= NULL
;
922 ptb
->se_buffer
= NULL
;
923 ptb
->pe_buffer
= NULL
;
925 HeapFree( GetProcessHeap(), 0, ptb
);
926 NtCurrentTeb()->WinSockData
= NULL
;
929 /***********************************************************************
930 * DllMain (WS2_32.init)
932 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
934 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
936 case DLL_PROCESS_ATTACH
:
938 case DLL_PROCESS_DETACH
:
940 free_per_thread_data();
941 DeleteCriticalSection(&csWSgetXXXbyYYY
);
943 case DLL_THREAD_DETACH
:
944 free_per_thread_data();
950 /***********************************************************************
953 * Converts socket flags from Windows format.
954 * Return 1 if converted, 0 if not (error).
956 static int convert_sockopt(INT
*level
, INT
*optname
)
963 for(i
=0; i
<sizeof(ws_sock_map
)/sizeof(ws_sock_map
[0]); i
++) {
964 if( ws_sock_map
[i
][0] == *optname
)
966 *optname
= ws_sock_map
[i
][1];
970 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
973 *level
= IPPROTO_TCP
;
974 for(i
=0; i
<sizeof(ws_tcp_map
)/sizeof(ws_tcp_map
[0]); i
++) {
975 if ( ws_tcp_map
[i
][0] == *optname
)
977 *optname
= ws_tcp_map
[i
][1];
981 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
985 for(i
=0; i
<sizeof(ws_ip_map
)/sizeof(ws_ip_map
[0]); i
++) {
986 if (ws_ip_map
[i
][0] == *optname
)
988 *optname
= ws_ip_map
[i
][1];
992 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
994 case WS_IPPROTO_IPV6
:
995 *level
= IPPROTO_IPV6
;
996 for(i
=0; i
<sizeof(ws_ipv6_map
)/sizeof(ws_ipv6_map
[0]); i
++) {
997 if (ws_ipv6_map
[i
][0] == *optname
)
999 *optname
= ws_ipv6_map
[i
][1];
1003 FIXME("Unknown IPPROTO_IPV6 optname 0x%x\n", *optname
);
1005 default: FIXME("Unimplemented or unknown socket level\n");
1010 /* ----------------------------------- Per-thread info (or per-process?) */
1012 static char *strdup_lower(const char *str
)
1015 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
1019 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
1022 else SetLastError(WSAENOBUFS
);
1026 static inline int sock_error_p(int s
)
1028 unsigned int optval
;
1029 socklen_t optlen
= sizeof(optval
);
1030 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
1031 if (optval
) WARN("\t[%i] error: %d\n", s
, optval
);
1035 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
1036 * from an fd and return the value converted to milli seconds
1037 * or -1 if there is an infinite time out */
1038 static inline int get_rcvsnd_timeo( int fd
, int optname
)
1041 socklen_t len
= sizeof(tv
);
1042 int ret
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
1044 ret
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
1045 if( ret
<= 0 ) /* tv == {0,0} means infinite time out */
1050 /* macro wrappers for portability */
1052 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
1054 #define GET_RCVTIMEO(fd) (-1)
1058 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
1060 #define GET_SNDTIMEO(fd) (-1)
1063 /* utility: given an fd, will block until one of the events occurs */
1064 static inline int do_block( int fd
, int events
, int timeout
)
1070 pfd
.events
= events
;
1072 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
1083 convert_af_w2u(int windowsaf
) {
1086 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
1087 if (ws_af_map
[i
][0] == windowsaf
)
1088 return ws_af_map
[i
][1];
1089 FIXME("unhandled Windows address family %d\n", windowsaf
);
1094 convert_af_u2w(int unixaf
) {
1097 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
1098 if (ws_af_map
[i
][1] == unixaf
)
1099 return ws_af_map
[i
][0];
1100 FIXME("unhandled UNIX address family %d\n", unixaf
);
1105 convert_proto_w2u(int windowsproto
) {
1108 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
1109 if (ws_proto_map
[i
][0] == windowsproto
)
1110 return ws_proto_map
[i
][1];
1111 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
1116 convert_proto_u2w(int unixproto
) {
1119 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
1120 if (ws_proto_map
[i
][1] == unixproto
)
1121 return ws_proto_map
[i
][0];
1122 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
1127 convert_socktype_w2u(int windowssocktype
) {
1130 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
1131 if (ws_socktype_map
[i
][0] == windowssocktype
)
1132 return ws_socktype_map
[i
][1];
1133 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
1138 convert_socktype_u2w(int unixsocktype
) {
1141 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
1142 if (ws_socktype_map
[i
][1] == unixsocktype
)
1143 return ws_socktype_map
[i
][0];
1144 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
1148 /* ----------------------------------- API -----
1150 * Init / cleanup / error checking.
1153 /***********************************************************************
1154 * WSAStartup (WS2_32.115)
1156 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
1158 TRACE("verReq=%x\n", wVersionRequested
);
1160 if (LOBYTE(wVersionRequested
) < 1)
1161 return WSAVERNOTSUPPORTED
;
1163 if (!lpWSAData
) return WSAEINVAL
;
1167 /* that's the whole of the negotiation for now */
1168 lpWSAData
->wVersion
= wVersionRequested
;
1169 /* return winsock information */
1170 lpWSAData
->wHighVersion
= 0x0202;
1171 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
1172 strcpy(lpWSAData
->szSystemStatus
, "Running" );
1173 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
1174 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
1175 /* don't do anything with lpWSAData->lpVendorInfo */
1176 /* (some apps don't allocate the space for this field) */
1178 TRACE("succeeded\n");
1183 /***********************************************************************
1184 * WSACleanup (WS2_32.116)
1186 INT WINAPI
WSACleanup(void)
1192 SetLastError(WSANOTINITIALISED
);
1193 return SOCKET_ERROR
;
1197 /***********************************************************************
1198 * WSAGetLastError (WS2_32.111)
1200 INT WINAPI
WSAGetLastError(void)
1202 return GetLastError();
1205 /***********************************************************************
1206 * WSASetLastError (WS2_32.112)
1208 void WINAPI
WSASetLastError(INT iError
) {
1209 SetLastError(iError
);
1212 static struct WS_hostent
*check_buffer_he(int size
)
1214 struct per_thread_data
* ptb
= get_per_thread_data();
1217 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
1218 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1220 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
1221 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
1222 return ptb
->he_buffer
;
1225 static struct WS_servent
*check_buffer_se(int size
)
1227 struct per_thread_data
* ptb
= get_per_thread_data();
1230 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
1231 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1233 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
1234 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
1235 return ptb
->se_buffer
;
1238 static struct WS_protoent
*check_buffer_pe(int size
)
1240 struct per_thread_data
* ptb
= get_per_thread_data();
1243 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
1244 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1246 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
1247 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
1248 return ptb
->pe_buffer
;
1251 /* ----------------------------------- i/o APIs */
1253 static inline BOOL
supported_pf(int pf
)
1273 static inline BOOL
supported_protocol(int protocol
)
1276 for (i
= 0; i
< sizeof(valid_protocols
) / sizeof(valid_protocols
[0]); i
++)
1277 if (protocol
== valid_protocols
[i
])
1282 /**********************************************************************/
1284 /* Returns the length of the converted address if successful, 0 if it was too small to
1287 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
1288 union generic_unix_sockaddr
*uaddr
)
1290 unsigned int uaddrlen
= 0;
1292 switch (wsaddr
->sa_family
)
1297 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
1298 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
1300 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
1303 uaddrlen
= sizeof(struct sockaddr_ipx
);
1304 memset( uaddr
, 0, uaddrlen
);
1305 uipx
->sipx_family
=AF_IPX
;
1306 uipx
->sipx_port
=wsipx
->sa_socket
;
1307 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
1310 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
1311 #ifdef IPX_FRAME_NONE
1312 uipx
->sipx_type
=IPX_FRAME_NONE
;
1318 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
1319 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
1321 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
1322 * scope_id, one without.
1324 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
1325 uaddrlen
= sizeof(struct sockaddr_in6
);
1326 memset( uaddr
, 0, uaddrlen
);
1327 uin6
->sin6_family
= AF_INET6
;
1328 uin6
->sin6_port
= win6
->sin6_port
;
1329 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
1330 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1331 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
1333 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1336 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
1340 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
1341 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
1343 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
1345 uaddrlen
= sizeof(struct sockaddr_in
);
1346 memset( uaddr
, 0, uaddrlen
);
1347 uin
->sin_family
= AF_INET
;
1348 uin
->sin_port
= win
->sin_port
;
1349 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
1354 struct sockaddr_irda
*uin
= (struct sockaddr_irda
*)uaddr
;
1355 const SOCKADDR_IRDA
*win
= (const SOCKADDR_IRDA
*)wsaddr
;
1357 if (wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1359 uaddrlen
= sizeof(struct sockaddr_irda
);
1360 memset( uaddr
, 0, uaddrlen
);
1361 uin
->sir_family
= AF_IRDA
;
1362 if (!strncmp( win
->irdaServiceName
, "LSAP-SEL", strlen( "LSAP-SEL" ) ))
1364 unsigned int lsap_sel
= 0;
1366 sscanf( win
->irdaServiceName
, "LSAP-SEL%u", &lsap_sel
);
1367 uin
->sir_lsap_sel
= lsap_sel
;
1371 uin
->sir_lsap_sel
= LSAP_ANY
;
1372 memcpy( uin
->sir_name
, win
->irdaServiceName
, 25 );
1374 memcpy( &uin
->sir_addr
, win
->irdaDeviceID
, sizeof(uin
->sir_addr
) );
1378 case WS_AF_UNSPEC
: {
1379 /* Try to determine the needed space by the passed windows sockaddr space */
1380 switch (wsaddrlen
) {
1381 default: /* likely a ipv4 address */
1382 case sizeof(struct WS_sockaddr_in
):
1383 uaddrlen
= sizeof(struct sockaddr_in
);
1386 case sizeof(struct WS_sockaddr_ipx
):
1387 uaddrlen
= sizeof(struct sockaddr_ipx
);
1391 case sizeof(SOCKADDR_IRDA
):
1392 uaddrlen
= sizeof(struct sockaddr_irda
);
1395 case sizeof(struct WS_sockaddr_in6
):
1396 case sizeof(struct WS_sockaddr_in6_old
):
1397 uaddrlen
= sizeof(struct sockaddr_in6
);
1400 memset( uaddr
, 0, uaddrlen
);
1404 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
1410 static BOOL
is_sockaddr_bound(const struct sockaddr
*uaddr
, int uaddrlen
)
1412 switch (uaddr
->sa_family
)
1416 FIXME("don't know how to tell if IPX socket is bound, assuming it is!\n");
1421 static const struct sockaddr_in6 emptyAddr
;
1422 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) uaddr
;
1423 return in6
->sin6_port
|| memcmp(&in6
->sin6_addr
, &emptyAddr
.sin6_addr
, sizeof(struct in6_addr
));
1427 static const struct sockaddr_in emptyAddr
;
1428 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) uaddr
;
1429 return in
->sin_port
|| memcmp(&in
->sin_addr
, &emptyAddr
.sin_addr
, sizeof(struct in_addr
));
1434 FIXME("unknown address family %d\n", uaddr
->sa_family
);
1439 /* Returns 0 if successful, -1 if the buffer is too small */
1440 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
1444 switch(uaddr
->sa_family
)
1449 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
1450 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
1453 switch (*wsaddrlen
) /* how much can we copy? */
1457 *wsaddrlen
= sizeof(*wsipx
);
1458 wsipx
->sa_socket
=uipx
->sipx_port
;
1462 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
1470 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
1476 wsipx
->sa_family
=WS_AF_IPX
;
1488 const struct sockaddr_irda
*uin
= (const struct sockaddr_irda
*)uaddr
;
1489 SOCKADDR_IRDA
*win
= (SOCKADDR_IRDA
*)wsaddr
;
1491 if (*wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1493 win
->irdaAddressFamily
= WS_AF_IRDA
;
1494 memcpy( win
->irdaDeviceID
, &uin
->sir_addr
, sizeof(win
->irdaDeviceID
) );
1495 if (uin
->sir_lsap_sel
!= LSAP_ANY
)
1496 sprintf( win
->irdaServiceName
, "LSAP-SEL%u", uin
->sir_lsap_sel
);
1498 memcpy( win
->irdaServiceName
, uin
->sir_name
,
1499 sizeof(win
->irdaServiceName
) );
1504 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
1505 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
1507 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
1509 win6old
->sin6_family
= WS_AF_INET6
;
1510 win6old
->sin6_port
= uin6
->sin6_port
;
1511 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
1512 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1513 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1514 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
1515 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
1516 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
1517 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
1520 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1522 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1527 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
1528 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
1530 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
1532 win
->sin_family
= WS_AF_INET
;
1533 win
->sin_port
= uin
->sin_port
;
1534 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
1535 memset(win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
1536 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
1540 memset(wsaddr
,0,*wsaddrlen
);
1544 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
1550 static INT
WS_DuplicateSocket(BOOL unicode
, SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
1554 WSAPROTOCOL_INFOW infow
;
1556 TRACE("(unicode %d, socket %04lx, processid %x, buffer %p)\n",
1557 unicode
, s
, dwProcessId
, lpProtocolInfo
);
1559 if (!ws_protocol_info(s
, unicode
, &infow
, &size
))
1560 return SOCKET_ERROR
;
1562 if (!(hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
)))
1564 SetLastError(WSAEINVAL
);
1565 return SOCKET_ERROR
;
1568 if (!lpProtocolInfo
)
1570 CloseHandle(hProcess
);
1571 SetLastError(WSAEFAULT
);
1572 return SOCKET_ERROR
;
1575 /* I don't know what the real Windoze does next, this is a hack */
1576 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
1577 * the target use the global duplicate, or we could copy a reference to us to the structure
1578 * and let the target duplicate it from us, but let's do it as simple as possible */
1579 memcpy(lpProtocolInfo
, &infow
, size
);
1580 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
1581 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwServiceFlags3
,
1582 0, FALSE
, DUPLICATE_SAME_ACCESS
);
1583 CloseHandle(hProcess
);
1584 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
1588 /*****************************************************************************
1589 * WS_EnterSingleProtocolW [internal]
1591 * enters the protocol information of one given protocol into the given
1595 * TRUE if a protocol was entered into the buffer.
1598 * - only implemented for IPX, SPX, SPXII, TCP, UDP
1599 * - there is no check that the operating system supports the returned
1602 static BOOL
WS_EnterSingleProtocolW( INT protocol
, WSAPROTOCOL_INFOW
* info
)
1604 memset( info
, 0, sizeof(WSAPROTOCOL_INFOW
) );
1605 info
->iProtocol
= protocol
;
1609 case WS_IPPROTO_TCP
:
1610 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_EXPEDITED_DATA
|
1611 XP1_GRACEFUL_CLOSE
| XP1_GUARANTEED_ORDER
|
1612 XP1_GUARANTEED_DELIVERY
;
1613 info
->ProviderId
= ProviderIdIP
;
1614 info
->dwCatalogEntryId
= 0x3e9;
1615 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
1616 info
->ProtocolChain
.ChainLen
= 1;
1618 info
->iAddressFamily
= WS_AF_INET
;
1619 info
->iMaxSockAddr
= 0x10;
1620 info
->iMinSockAddr
= 0x10;
1621 info
->iSocketType
= WS_SOCK_STREAM
;
1622 strcpyW( info
->szProtocol
, NameTcpW
);
1625 case WS_IPPROTO_UDP
:
1626 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_SUPPORT_BROADCAST
|
1627 XP1_SUPPORT_MULTIPOINT
| XP1_MESSAGE_ORIENTED
|
1629 info
->ProviderId
= ProviderIdIP
;
1630 info
->dwCatalogEntryId
= 0x3ea;
1631 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
1632 info
->ProtocolChain
.ChainLen
= 1;
1634 info
->iAddressFamily
= WS_AF_INET
;
1635 info
->iMaxSockAddr
= 0x10;
1636 info
->iMinSockAddr
= 0x10;
1637 info
->iSocketType
= WS_SOCK_DGRAM
;
1638 info
->dwMessageSize
= 0xffbb;
1639 strcpyW( info
->szProtocol
, NameUdpW
);
1643 info
->dwServiceFlags1
= XP1_PARTIAL_MESSAGE
| XP1_SUPPORT_BROADCAST
|
1644 XP1_SUPPORT_MULTIPOINT
| XP1_MESSAGE_ORIENTED
|
1646 info
->ProviderId
= ProviderIdIPX
;
1647 info
->dwCatalogEntryId
= 0x406;
1648 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
1649 info
->ProtocolChain
.ChainLen
= 1;
1651 info
->iAddressFamily
= WS_AF_IPX
;
1652 info
->iMaxSockAddr
= 0x10;
1653 info
->iMinSockAddr
= 0x0e;
1654 info
->iSocketType
= WS_SOCK_DGRAM
;
1655 info
->iProtocolMaxOffset
= 0xff;
1656 info
->dwMessageSize
= 0x240;
1657 strcpyW( info
->szProtocol
, NameIpxW
);
1661 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_PSEUDO_STREAM
|
1662 XP1_MESSAGE_ORIENTED
| XP1_GUARANTEED_ORDER
|
1663 XP1_GUARANTEED_DELIVERY
;
1664 info
->ProviderId
= ProviderIdSPX
;
1665 info
->dwCatalogEntryId
= 0x407;
1666 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
1667 info
->ProtocolChain
.ChainLen
= 1;
1669 info
->iAddressFamily
= WS_AF_IPX
;
1670 info
->iMaxSockAddr
= 0x10;
1671 info
->iMinSockAddr
= 0x0e;
1672 info
->iSocketType
= WS_SOCK_SEQPACKET
;
1673 info
->dwMessageSize
= 0xffffffff;
1674 strcpyW( info
->szProtocol
, NameSpxW
);
1678 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_GRACEFUL_CLOSE
|
1679 XP1_PSEUDO_STREAM
| XP1_MESSAGE_ORIENTED
|
1680 XP1_GUARANTEED_ORDER
| XP1_GUARANTEED_DELIVERY
;
1681 info
->ProviderId
= ProviderIdSPX
;
1682 info
->dwCatalogEntryId
= 0x409;
1683 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
1684 info
->ProtocolChain
.ChainLen
= 1;
1686 info
->iAddressFamily
= WS_AF_IPX
;
1687 info
->iMaxSockAddr
= 0x10;
1688 info
->iMinSockAddr
= 0x0e;
1689 info
->iSocketType
= WS_SOCK_SEQPACKET
;
1690 info
->dwMessageSize
= 0xffffffff;
1691 strcpyW( info
->szProtocol
, NameSpxIIW
);
1695 FIXME("unknown Protocol <0x%08x>\n", protocol
);
1701 /*****************************************************************************
1702 * WS_EnterSingleProtocolA [internal]
1704 * see function WS_EnterSingleProtocolW
1707 static BOOL
WS_EnterSingleProtocolA( INT protocol
, WSAPROTOCOL_INFOA
* info
)
1709 WSAPROTOCOL_INFOW infow
;
1711 memset( info
, 0, sizeof(WSAPROTOCOL_INFOA
) );
1713 ret
= WS_EnterSingleProtocolW( protocol
, &infow
);
1716 /* convert the structure from W to A */
1717 memcpy( info
, &infow
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
1718 WideCharToMultiByte( CP_ACP
, 0, infow
.szProtocol
, -1,
1719 info
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
);
1725 static INT
WS_EnumProtocols( BOOL unicode
, const INT
*protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
)
1727 INT i
= 0, items
= 0;
1731 LPWSAPROTOCOL_INFOA a
;
1732 LPWSAPROTOCOL_INFOW w
;
1736 if (!protocols
) protocols
= valid_protocols
;
1738 while (protocols
[i
])
1740 if(supported_protocol(protocols
[i
++]))
1744 size
= items
* (unicode
? sizeof(WSAPROTOCOL_INFOW
) : sizeof(WSAPROTOCOL_INFOA
));
1746 TRACE("unicode %d, protocols %p, buffer %p, length %p %d, items %d, required %d\n",
1747 unicode
, protocols
, buffer
, len
, len
? *len
: 0, items
, size
);
1749 if (*len
< size
|| !buffer
)
1752 WSASetLastError(WSAENOBUFS
);
1753 return SOCKET_ERROR
;
1756 for (i
= items
= 0; protocols
[i
]; i
++)
1760 if (WS_EnterSingleProtocolW( protocols
[i
], &info
.w
[items
] ))
1765 if (WS_EnterSingleProtocolA( protocols
[i
], &info
.a
[items
] ))
1772 static BOOL
ws_protocol_info(SOCKET s
, int unicode
, WSAPROTOCOL_INFOW
*buffer
, int *size
)
1776 *size
= unicode
? sizeof(WSAPROTOCOL_INFOW
) : sizeof(WSAPROTOCOL_INFOA
);
1777 memset(buffer
, 0, *size
);
1779 SERVER_START_REQ( get_socket_info
)
1781 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1782 status
= wine_server_call( req
);
1785 buffer
->iAddressFamily
= convert_af_u2w(reply
->family
);
1786 buffer
->iSocketType
= convert_socktype_u2w(reply
->type
);
1787 buffer
->iProtocol
= convert_proto_u2w(reply
->protocol
);
1794 unsigned int err
= NtStatusToWSAError( status
);
1795 SetLastError( err
== WSAEBADF
? WSAENOTSOCK
: err
);
1800 WS_EnterSingleProtocolW( buffer
->iProtocol
, buffer
);
1802 WS_EnterSingleProtocolA( buffer
->iProtocol
, (WSAPROTOCOL_INFOA
*)buffer
);
1807 /**************************************************************************
1808 * Functions for handling overlapped I/O
1809 **************************************************************************/
1811 /* user APC called upon async completion */
1812 static void WINAPI
ws2_async_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1814 ws2_async
*wsa
= arg
;
1816 if (wsa
->completion_func
) wsa
->completion_func( NtStatusToWSAError(iosb
->u
.Status
),
1817 iosb
->Information
, wsa
->user_overlapped
,
1819 HeapFree( GetProcessHeap(), 0, wsa
);
1822 /***********************************************************************
1823 * WS2_recv (INTERNAL)
1825 * Workhorse for both synchronous and asynchronous recv() operations.
1827 static int WS2_recv( int fd
, struct ws2_async
*wsa
)
1829 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1833 union generic_unix_sockaddr unix_sockaddr
;
1836 hdr
.msg_name
= NULL
;
1840 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
1841 hdr
.msg_name
= &unix_sockaddr
;
1844 hdr
.msg_namelen
= 0;
1846 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1847 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1848 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1849 hdr
.msg_accrights
= NULL
;
1850 hdr
.msg_accrightslen
= 0;
1852 hdr
.msg_control
= pktbuf
;
1853 hdr
.msg_controllen
= sizeof(pktbuf
);
1857 if ( (n
= recvmsg(fd
, &hdr
, wsa
->flags
)) == -1 )
1860 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1863 ERR("Message control headers cannot be properly supported on this system.\n");
1864 wsa
->control
->len
= 0;
1867 if (wsa
->control
&& !convert_control_headers(&hdr
, wsa
->control
))
1869 WARN("Application passed insufficient room for control headers.\n");
1870 *wsa
->lpFlags
|= WS_MSG_CTRUNC
;
1876 /* if this socket is connected and lpFrom is not NULL, Linux doesn't give us
1877 * msg_name and msg_namelen from recvmsg, but it does set msg_namelen to zero.
1879 * quoting linux 2.6 net/ipv4/tcp.c:
1880 * "According to UNIX98, msg_name/msg_namelen are ignored
1881 * on connected socket. I was just happy when found this 8) --ANK"
1883 * likewise MSDN says that lpFrom and lpFromlen are ignored for
1884 * connection-oriented sockets, so don't try to update lpFrom.
1886 if (wsa
->addr
&& hdr
.msg_namelen
)
1887 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, wsa
->addr
, wsa
->addrlen
.ptr
);
1892 /***********************************************************************
1893 * WS2_async_recv (INTERNAL)
1895 * Handler for overlapped recv() operations.
1897 static NTSTATUS
WS2_async_recv( void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
1899 ws2_async
* wsa
= user
;
1904 case STATUS_ALERTED
:
1905 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
1908 result
= WS2_recv( fd
, wsa
);
1909 wine_server_release_fd( wsa
->hSocket
, fd
);
1912 status
= STATUS_SUCCESS
;
1913 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1917 if (errno
== EINTR
|| errno
== EAGAIN
)
1919 status
= STATUS_PENDING
;
1920 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1925 status
= wsaErrStatus();
1930 if (status
!= STATUS_PENDING
)
1932 iosb
->u
.Status
= status
;
1933 iosb
->Information
= result
;
1934 *apc
= ws2_async_apc
;
1939 /* user APC called upon async accept completion */
1940 static void WINAPI
ws2_async_accept_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1942 struct ws2_accept_async
*wsa
= arg
;
1944 HeapFree( GetProcessHeap(), 0, wsa
->read
);
1945 HeapFree( GetProcessHeap(), 0, wsa
);
1948 /***********************************************************************
1949 * WS2_async_accept_recv (INTERNAL)
1951 * This function is used to finish the read part of an accept request. It is
1952 * needed to place the completion on the correct socket (listener).
1954 static NTSTATUS
WS2_async_accept_recv( void *arg
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
, void **apc
)
1957 struct ws2_accept_async
*wsa
= arg
;
1959 status
= WS2_async_recv( wsa
->read
, iosb
, status
, &junk
);
1960 if (status
== STATUS_PENDING
)
1963 if (wsa
->user_overlapped
->hEvent
)
1964 SetEvent(wsa
->user_overlapped
->hEvent
);
1966 WS_AddCompletion( HANDLE2SOCKET(wsa
->listen_socket
), wsa
->cvalue
, iosb
->u
.Status
, iosb
->Information
);
1968 *apc
= ws2_async_accept_apc
;
1972 /***********************************************************************
1973 * WS2_async_accept (INTERNAL)
1975 * This is the function called to satisfy the AcceptEx callback
1977 static NTSTATUS
WS2_async_accept( void *arg
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
, void **apc
)
1979 struct ws2_accept_async
*wsa
= arg
;
1983 TRACE("status: 0x%x listen: %p, accept: %p\n", status
, wsa
->listen_socket
, wsa
->accept_socket
);
1985 if (status
== STATUS_ALERTED
)
1987 SERVER_START_REQ( accept_into_socket
)
1989 req
->lhandle
= wine_server_obj_handle( wsa
->listen_socket
);
1990 req
->ahandle
= wine_server_obj_handle( wsa
->accept_socket
);
1991 status
= wine_server_call( req
);
1995 if (status
== STATUS_CANT_WAIT
)
1996 return STATUS_PENDING
;
1998 if (status
== STATUS_INVALID_HANDLE
)
2000 FIXME("AcceptEx accepting socket closed but request was not cancelled\n");
2001 status
= STATUS_CANCELLED
;
2004 else if (status
== STATUS_HANDLES_CLOSED
)
2005 status
= STATUS_CANCELLED
; /* strange windows behavior */
2007 if (status
!= STATUS_SUCCESS
)
2010 /* WS2 Spec says size param is extra 16 bytes long...what do we put in it? */
2011 addr
= ((char *)wsa
->buf
) + wsa
->data_len
;
2012 len
= wsa
->local_len
- sizeof(int);
2013 WS_getsockname(HANDLE2SOCKET(wsa
->accept_socket
),
2014 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
2017 addr
+= wsa
->local_len
;
2018 len
= wsa
->remote_len
- sizeof(int);
2019 WS_getpeername(HANDLE2SOCKET(wsa
->accept_socket
),
2020 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
2026 SERVER_START_REQ( register_async
)
2028 req
->type
= ASYNC_TYPE_READ
;
2029 req
->async
.handle
= wine_server_obj_handle( wsa
->accept_socket
);
2030 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept_recv
);
2031 req
->async
.iosb
= wine_server_client_ptr( iosb
);
2032 req
->async
.arg
= wine_server_client_ptr( wsa
);
2033 status
= wine_server_call( req
);
2037 if (status
!= STATUS_PENDING
)
2040 /* The APC has finished but no completion should be sent for the operation yet, additional processing
2041 * needs to be performed by WS2_async_accept_recv() first. */
2042 return STATUS_MORE_PROCESSING_REQUIRED
;
2045 iosb
->u
.Status
= status
;
2046 iosb
->Information
= 0;
2048 if (wsa
->user_overlapped
->hEvent
)
2049 SetEvent(wsa
->user_overlapped
->hEvent
);
2051 *apc
= ws2_async_accept_apc
;
2055 /***********************************************************************
2056 * WS2_send (INTERNAL)
2058 * Workhorse for both synchronous and asynchronous send() operations.
2060 static int WS2_send( int fd
, struct ws2_async
*wsa
)
2063 union generic_unix_sockaddr unix_addr
;
2066 hdr
.msg_name
= NULL
;
2067 hdr
.msg_namelen
= 0;
2071 hdr
.msg_name
= &unix_addr
;
2072 hdr
.msg_namelen
= ws_sockaddr_ws2u( wsa
->addr
, wsa
->addrlen
.val
, &unix_addr
);
2073 if ( !hdr
.msg_namelen
)
2079 #if defined(HAS_IPX) && defined(SOL_IPX)
2080 if(wsa
->addr
->sa_family
== WS_AF_IPX
)
2082 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
2084 socklen_t len
= sizeof(int);
2086 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
2087 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
2088 * the packet type and then we can retrieve it using getsockopt. After that we can set the
2089 * ipx type in the sockaddr_opx structure with the stored value.
2091 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
2092 uipx
->sipx_type
= val
;
2097 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
2098 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
2099 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2100 hdr
.msg_accrights
= NULL
;
2101 hdr
.msg_accrightslen
= 0;
2103 hdr
.msg_control
= NULL
;
2104 hdr
.msg_controllen
= 0;
2108 ret
= sendmsg(fd
, &hdr
, wsa
->flags
);
2112 while (wsa
->first_iovec
< wsa
->n_iovecs
&& wsa
->iovec
[wsa
->first_iovec
].iov_len
<= n
)
2113 n
-= wsa
->iovec
[wsa
->first_iovec
++].iov_len
;
2114 if (wsa
->first_iovec
< wsa
->n_iovecs
)
2116 wsa
->iovec
[wsa
->first_iovec
].iov_base
= (char*)wsa
->iovec
[wsa
->first_iovec
].iov_base
+ n
;
2117 wsa
->iovec
[wsa
->first_iovec
].iov_len
-= n
;
2123 /***********************************************************************
2124 * WS2_async_send (INTERNAL)
2126 * Handler for overlapped send() operations.
2128 static NTSTATUS
WS2_async_send(void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
2130 ws2_async
* wsa
= user
;
2135 case STATUS_ALERTED
:
2136 if ( wsa
->n_iovecs
<= wsa
->first_iovec
)
2139 status
= STATUS_SUCCESS
;
2142 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
2145 /* check to see if the data is ready (non-blocking) */
2146 result
= WS2_send( fd
, wsa
);
2147 wine_server_release_fd( wsa
->hSocket
, fd
);
2151 if (wsa
->first_iovec
< wsa
->n_iovecs
)
2152 status
= STATUS_PENDING
;
2154 status
= STATUS_SUCCESS
;
2156 iosb
->Information
+= result
;
2158 else if (errno
== EINTR
|| errno
== EAGAIN
)
2160 status
= STATUS_PENDING
;
2164 status
= wsaErrStatus();
2168 if (status
!= STATUS_PENDING
)
2170 iosb
->u
.Status
= status
;
2171 *apc
= ws2_async_apc
;
2176 /***********************************************************************
2177 * WS2_async_shutdown (INTERNAL)
2179 * Handler for shutdown() operations on overlapped sockets.
2181 static NTSTATUS
WS2_async_shutdown( void* user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
, void **apc
)
2183 ws2_async
* wsa
= user
;
2188 case STATUS_ALERTED
:
2189 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
2192 switch ( wsa
->type
)
2194 case ASYNC_TYPE_READ
: err
= shutdown( fd
, 0 ); break;
2195 case ASYNC_TYPE_WRITE
: err
= shutdown( fd
, 1 ); break;
2197 status
= err
? wsaErrStatus() : STATUS_SUCCESS
;
2198 wine_server_release_fd( wsa
->hSocket
, fd
);
2201 iosb
->u
.Status
= status
;
2202 iosb
->Information
= 0;
2203 *apc
= ws2_async_apc
;
2207 /***********************************************************************
2208 * WS2_register_async_shutdown (INTERNAL)
2210 * Helper function for WS_shutdown() on overlapped sockets.
2212 static int WS2_register_async_shutdown( SOCKET s
, int type
)
2214 struct ws2_async
*wsa
;
2217 TRACE("s %ld type %d\n", s
, type
);
2219 wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( struct ws2_async
, iovec
[1] ));
2223 wsa
->hSocket
= SOCKET2HANDLE(s
);
2225 wsa
->completion_func
= NULL
;
2227 SERVER_START_REQ( register_async
)
2230 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
2231 req
->async
.callback
= wine_server_client_ptr( WS2_async_shutdown
);
2232 req
->async
.iosb
= wine_server_client_ptr( &wsa
->local_iosb
);
2233 req
->async
.arg
= wine_server_client_ptr( wsa
);
2234 req
->async
.cvalue
= 0;
2235 status
= wine_server_call( req
);
2239 if (status
!= STATUS_PENDING
)
2241 HeapFree( GetProcessHeap(), 0, wsa
);
2242 return NtStatusToWSAError( status
);
2247 /***********************************************************************
2250 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
,
2257 TRACE("socket %04lx\n", s
);
2258 status
= _is_blocking(s
, &is_blocking
);
2262 return INVALID_SOCKET
;
2266 /* try accepting first (if there is a deferred connection) */
2267 SERVER_START_REQ( accept_socket
)
2269 req
->lhandle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
2270 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
2271 req
->attributes
= OBJ_INHERIT
;
2272 status
= wine_server_call( req
);
2273 as
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
2278 if (addr
) WS_getpeername(as
, addr
, addrlen32
);
2281 if (is_blocking
&& status
== STATUS_CANT_WAIT
)
2283 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2285 do_block(fd
, POLLIN
, -1);
2286 _sync_sock_state(s
); /* let wineserver notice connection */
2287 release_sock_fd( s
, fd
);
2289 } while (is_blocking
&& status
== STATUS_CANT_WAIT
);
2292 return INVALID_SOCKET
;
2295 /***********************************************************************
2298 static BOOL WINAPI
WS2_AcceptEx(SOCKET listener
, SOCKET acceptor
, PVOID dest
, DWORD dest_len
,
2299 DWORD local_addr_len
, DWORD rem_addr_len
, LPDWORD received
,
2300 LPOVERLAPPED overlapped
)
2303 struct ws2_accept_async
*wsa
;
2305 ULONG_PTR cvalue
= (overlapped
&& ((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)overlapped
: 0;
2307 TRACE("(%lx, %lx, %p, %d, %d, %d, %p, %p)\n", listener
, acceptor
, dest
, dest_len
, local_addr_len
,
2308 rem_addr_len
, received
, overlapped
);
2312 SetLastError(WSAEINVAL
);
2318 SetLastError(WSA_INVALID_PARAMETER
);
2322 fd
= get_sock_fd( listener
, FILE_READ_DATA
, NULL
);
2325 SetLastError(WSAENOTSOCK
);
2328 release_sock_fd( listener
, fd
);
2330 fd
= get_sock_fd( acceptor
, FILE_READ_DATA
, NULL
);
2333 SetLastError(WSAEINVAL
);
2336 release_sock_fd( acceptor
, fd
);
2338 wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
2341 SetLastError(WSAEFAULT
);
2345 wsa
->listen_socket
= SOCKET2HANDLE(listener
);
2346 wsa
->accept_socket
= SOCKET2HANDLE(acceptor
);
2347 wsa
->user_overlapped
= overlapped
;
2348 wsa
->cvalue
= cvalue
;
2350 wsa
->data_len
= dest_len
;
2351 wsa
->local_len
= local_addr_len
;
2352 wsa
->remote_len
= rem_addr_len
;
2357 /* set up a read request if we need it */
2358 wsa
->read
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[1]) );
2361 HeapFree( GetProcessHeap(), 0, wsa
);
2362 SetLastError(WSAEFAULT
);
2366 wsa
->read
->hSocket
= wsa
->accept_socket
;
2367 wsa
->read
->flags
= 0;
2368 wsa
->read
->lpFlags
= &wsa
->read
->flags
;
2369 wsa
->read
->addr
= NULL
;
2370 wsa
->read
->addrlen
.ptr
= NULL
;
2371 wsa
->read
->control
= NULL
;
2372 wsa
->read
->n_iovecs
= 1;
2373 wsa
->read
->first_iovec
= 0;
2374 wsa
->read
->iovec
[0].iov_base
= wsa
->buf
;
2375 wsa
->read
->iovec
[0].iov_len
= wsa
->data_len
;
2378 SERVER_START_REQ( register_async
)
2380 req
->type
= ASYNC_TYPE_READ
;
2381 req
->async
.handle
= wine_server_obj_handle( SOCKET2HANDLE(listener
) );
2382 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept
);
2383 req
->async
.iosb
= wine_server_client_ptr( overlapped
);
2384 req
->async
.arg
= wine_server_client_ptr( wsa
);
2385 req
->async
.cvalue
= cvalue
;
2386 /* We don't set event since we may also have to read */
2387 status
= wine_server_call( req
);
2391 if(status
!= STATUS_PENDING
)
2393 HeapFree( GetProcessHeap(), 0, wsa
->read
);
2394 HeapFree( GetProcessHeap(), 0, wsa
);
2397 SetLastError( NtStatusToWSAError(status
) );
2401 /***********************************************************************
2402 * GetAcceptExSockaddrs
2404 static void WINAPI
WS2_GetAcceptExSockaddrs(PVOID buffer
, DWORD data_size
, DWORD local_size
, DWORD remote_size
,
2405 struct WS_sockaddr
**local_addr
, LPINT local_addr_len
,
2406 struct WS_sockaddr
**remote_addr
, LPINT remote_addr_len
)
2408 char *cbuf
= buffer
;
2409 TRACE("(%p, %d, %d, %d, %p, %p, %p, %p)\n", buffer
, data_size
, local_size
, remote_size
, local_addr
,
2410 local_addr_len
, remote_addr
, remote_addr_len
);
2413 *local_addr_len
= *(int *) cbuf
;
2414 *local_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
2418 *remote_addr_len
= *(int *) cbuf
;
2419 *remote_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
2422 /***********************************************************************
2425 * Perform a receive operation that is capable of returning message
2426 * control headers. It is important to note that the WSAMSG parameter
2427 * must remain valid throughout the operation, even when an overlapped
2428 * receive is performed.
2430 static int WINAPI
WS2_WSARecvMsg( SOCKET s
, LPWSAMSG msg
, LPDWORD lpNumberOfBytesRecvd
,
2431 LPWSAOVERLAPPED lpOverlapped
,
2432 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2436 SetLastError( WSAEFAULT
);
2437 return SOCKET_ERROR
;
2440 return WS2_recv_base( s
, msg
->lpBuffers
, msg
->dwBufferCount
, lpNumberOfBytesRecvd
,
2441 &msg
->dwFlags
, msg
->name
, &msg
->namelen
,
2442 lpOverlapped
, lpCompletionRoutine
, &msg
->Control
);
2445 /***********************************************************************
2446 * interface_bind (INTERNAL)
2448 * Take bind() calls on any name corresponding to a local network adapter and restrict the given socket to
2449 * operating only on the specified interface. This restriction consists of two components:
2450 * 1) An outgoing packet restriction suggesting the egress interface for all packets.
2451 * 2) An incoming packet restriction dropping packets not meant for the interface.
2452 * If the function succeeds in placing these restrictions (returns TRUE) then the name for the bind() may
2453 * safely be changed to INADDR_ANY, permitting the transmission and receipt of broadcast packets on the
2454 * socket. This behavior is only relevant to UDP sockets and is needed for applications that expect to be able
2455 * to receive broadcast packets on a socket that is bound to a specific network interface.
2457 static BOOL
interface_bind( SOCKET s
, int fd
, struct sockaddr
*addr
)
2459 struct sockaddr_in
*in_sock
= (struct sockaddr_in
*) addr
;
2460 unsigned int sock_type
= 0;
2461 socklen_t optlen
= sizeof(sock_type
);
2462 in_addr_t bind_addr
= in_sock
->sin_addr
.s_addr
;
2463 PIP_ADAPTER_INFO adapters
= NULL
, adapter
;
2468 if (bind_addr
== htonl(WS_INADDR_ANY
) || bind_addr
== htonl(WS_INADDR_LOOPBACK
))
2469 return FALSE
; /* Not binding to a network adapter, special interface binding unnecessary. */
2470 if (getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &sock_type
, &optlen
) == -1 || sock_type
!= SOCK_DGRAM
)
2471 return FALSE
; /* Special interface binding is only necessary for UDP datagrams. */
2472 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
2474 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
2475 if (adapters
== NULL
|| GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
2477 /* Search the IPv4 adapter list for the appropriate binding interface */
2478 for (adapter
= adapters
; adapter
!= NULL
; adapter
= adapter
->Next
)
2480 in_addr_t adapter_addr
= (in_addr_t
) inet_addr(adapter
->IpAddressList
.IpAddress
.String
);
2482 if (bind_addr
== adapter_addr
)
2484 #if defined(IP_BOUND_IF)
2485 /* IP_BOUND_IF sets both the incoming and outgoing restriction at once */
2486 if (setsockopt(fd
, IPPROTO_IP
, IP_BOUND_IF
, &adapter
->Index
, sizeof(adapter
->Index
)) != 0)
2489 #elif defined(LINUX_BOUND_IF)
2490 in_addr_t ifindex
= (in_addr_t
) htonl(adapter
->Index
);
2491 struct interface_filter specific_interface_filter
;
2492 struct sock_fprog filter_prog
;
2494 if (setsockopt(fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
)) != 0)
2495 goto cleanup
; /* Failed to suggest egress interface */
2496 specific_interface_filter
= generic_interface_filter
;
2497 specific_interface_filter
.iface_rule
.k
= adapter
->Index
;
2498 specific_interface_filter
.ip_rule
.k
= htonl(adapter_addr
);
2499 filter_prog
.len
= sizeof(generic_interface_filter
)/sizeof(struct sock_filter
);
2500 filter_prog
.filter
= (struct sock_filter
*) &specific_interface_filter
;
2501 if (setsockopt(fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
)) != 0)
2502 goto cleanup
; /* Failed to specify incoming packet filter */
2505 FIXME("Broadcast packets on interface-bound sockets are not currently supported on this platform, "
2506 "receiving broadcast packets will not work on socket %04lx.\n", s
);
2511 /* Will soon be switching to INADDR_ANY: permit address reuse */
2512 if (ret
&& setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
)) == 0)
2513 TRACE("Socket %04lx bound to interface index %d\n", s
, adapter
->Index
);
2519 ERR("Failed to bind to interface, receiving broadcast packets will not work on socket %04lx.\n", s
);
2520 HeapFree(GetProcessHeap(), 0, adapters
);
2524 /***********************************************************************
2527 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
2529 int fd
= get_sock_fd( s
, 0, NULL
);
2530 int res
= SOCKET_ERROR
;
2532 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
2536 if (!name
|| (name
->sa_family
&& !supported_pf(name
->sa_family
)))
2538 SetLastError(WSAEAFNOSUPPORT
);
2542 union generic_unix_sockaddr uaddr
;
2543 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
2546 SetLastError(WSAEFAULT
);
2551 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) &uaddr
;
2552 if (name
->sa_family
== WS_AF_INET6
&&
2553 !memcmp(&in6
->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
)))
2556 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
)) == -1)
2558 release_sock_fd( s
, fd
);
2559 SetLastError(WSAEAFNOSUPPORT
);
2560 return SOCKET_ERROR
;
2564 if (name
->sa_family
== WS_AF_INET
)
2566 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
2567 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
2569 /* Trying to bind to the default host interface, using
2570 * INADDR_ANY instead*/
2571 WARN("Trying to bind to magic IP address, using "
2572 "INADDR_ANY instead.\n");
2573 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_ANY
);
2575 else if (interface_bind(s
, fd
, &uaddr
.addr
))
2576 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_ANY
);
2578 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
2580 int loc_errno
= errno
;
2581 WARN("\tfailure - errno = %i\n", errno
);
2586 SetLastError(WSAENOTSOCK
);
2589 SetLastError(WSAEINVAL
);
2592 SetLastError(wsaErrno());
2598 res
=0; /* success */
2602 release_sock_fd( s
, fd
);
2607 /***********************************************************************
2608 * closesocket (WS2_32.3)
2610 int WINAPI
WS_closesocket(SOCKET s
)
2612 TRACE("socket %04lx\n", s
);
2613 if (CloseHandle(SOCKET2HANDLE(s
))) return 0;
2614 return SOCKET_ERROR
;
2617 static int do_connect(int fd
, const struct WS_sockaddr
* name
, int namelen
)
2619 union generic_unix_sockaddr uaddr
;
2620 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
2625 if (name
->sa_family
== WS_AF_INET
)
2627 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
2628 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
2630 /* Trying to connect to magic replace-loopback address,
2631 * assuming we really want to connect to localhost */
2632 TRACE("Trying to connect to magic IP address, using "
2633 "INADDR_LOOPBACK instead.\n");
2634 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_LOOPBACK
);
2638 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
2644 /***********************************************************************
2645 * connect (WS2_32.4)
2647 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
2649 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2651 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
2657 int ret
= do_connect(fd
, name
, namelen
);
2659 goto connect_success
;
2661 if (ret
== WSAEINPROGRESS
)
2663 /* tell wineserver that a connection is in progress */
2664 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2666 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
2667 status
= _is_blocking( s
, &is_blocking
);
2670 release_sock_fd( s
, fd
);
2671 set_error( status
);
2672 return SOCKET_ERROR
;
2678 do_block(fd
, POLLIN
| POLLOUT
, -1);
2679 _sync_sock_state(s
); /* let wineserver notice connection */
2680 /* retrieve any error codes from it */
2681 result
= _get_sock_error(s
, FD_CONNECT_BIT
);
2683 SetLastError(NtStatusToWSAError(result
));
2686 goto connect_success
;
2691 SetLastError(WSAEWOULDBLOCK
);
2698 release_sock_fd( s
, fd
);
2700 return SOCKET_ERROR
;
2703 release_sock_fd( s
, fd
);
2704 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2705 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
2706 FD_CONNECT
|FD_WINE_LISTENING
);
2710 /***********************************************************************
2711 * WSAConnect (WS2_32.30)
2713 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
2714 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
2715 LPQOS lpSQOS
, LPQOS lpGQOS
)
2717 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
2718 FIXME("unsupported parameters!\n");
2719 return WS_connect( s
, name
, namelen
);
2722 /***********************************************************************
2725 static BOOL WINAPI
WS2_ConnectEx(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
2726 PVOID sendBuf
, DWORD sendBufLen
, LPDWORD sent
, LPOVERLAPPED ov
)
2728 int fd
, ret
, status
;
2732 SetLastError( ERROR_INVALID_PARAMETER
);
2736 fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2739 SetLastError( WSAENOTSOCK
);
2743 TRACE("socket %04lx, ptr %p %s, length %d, sendptr %p, len %d, ov %p\n",
2744 s
, name
, debugstr_sockaddr(name
), namelen
, sendBuf
, sendBufLen
, ov
);
2746 /* FIXME: technically the socket has to be bound */
2747 ret
= do_connect(fd
, name
, namelen
);
2752 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2753 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
2754 FD_CONNECT
|FD_WINE_LISTENING
);
2756 wsabuf
.len
= sendBufLen
;
2757 wsabuf
.buf
= (char*) sendBuf
;
2759 /* WSASend takes care of completion if need be */
2760 if (WSASend(s
, &wsabuf
, sendBuf
? 1 : 0, sent
, 0, ov
, NULL
) != SOCKET_ERROR
)
2761 goto connection_success
;
2763 else if (ret
== WSAEINPROGRESS
)
2765 struct ws2_async
*wsa
;
2766 ULONG_PTR cvalue
= (((ULONG_PTR
)ov
->hEvent
& 1) == 0) ? (ULONG_PTR
)ov
: 0;
2768 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2770 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
2772 /* Indirectly call WSASend */
2773 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( struct ws2_async
, iovec
[1] ))))
2775 SetLastError(WSAEFAULT
);
2779 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)ov
;
2780 iosb
->u
.Status
= STATUS_PENDING
;
2781 iosb
->Information
= 0;
2783 wsa
->hSocket
= SOCKET2HANDLE(s
);
2785 wsa
->addrlen
.val
= 0;
2787 wsa
->lpFlags
= &wsa
->flags
;
2788 wsa
->control
= NULL
;
2789 wsa
->n_iovecs
= sendBuf
? 1 : 0;
2790 wsa
->first_iovec
= 0;
2791 wsa
->completion_func
= NULL
;
2792 wsa
->iovec
[0].iov_base
= sendBuf
;
2793 wsa
->iovec
[0].iov_len
= sendBufLen
;
2795 SERVER_START_REQ( register_async
)
2797 req
->type
= ASYNC_TYPE_WRITE
;
2798 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
2799 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
2800 req
->async
.iosb
= wine_server_client_ptr( iosb
);
2801 req
->async
.arg
= wine_server_client_ptr( wsa
);
2802 req
->async
.event
= wine_server_obj_handle( ov
->hEvent
);
2803 req
->async
.cvalue
= cvalue
;
2804 status
= wine_server_call( req
);
2808 if (status
!= STATUS_PENDING
) HeapFree(GetProcessHeap(), 0, wsa
);
2810 /* If the connect already failed */
2811 if (status
== STATUS_PIPE_DISCONNECTED
)
2812 status
= _get_sock_error(s
, FD_CONNECT_BIT
);
2813 SetLastError( NtStatusToWSAError(status
) );
2821 release_sock_fd( s
, fd
);
2825 release_sock_fd( s
, fd
);
2830 /***********************************************************************
2831 * getpeername (WS2_32.5)
2833 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
2838 TRACE("socket: %04lx, ptr %p, len %08x\n", s
, name
, namelen
? *namelen
: 0);
2840 fd
= get_sock_fd( s
, 0, NULL
);
2845 union generic_unix_sockaddr uaddr
;
2846 socklen_t uaddrlen
= sizeof(uaddr
);
2848 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) == 0)
2850 if (!name
|| !namelen
)
2851 SetLastError(WSAEFAULT
);
2852 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
2853 /* The buffer was too small */
2854 SetLastError(WSAEFAULT
);
2859 SetLastError(wsaErrno());
2860 release_sock_fd( s
, fd
);
2865 /***********************************************************************
2866 * getsockname (WS2_32.6)
2868 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
2873 TRACE("socket: %04lx, ptr %p, len %08x\n", s
, name
, namelen
? *namelen
: 0);
2875 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
2876 if( (name
== NULL
) || (namelen
== NULL
) )
2878 SetLastError( WSAEFAULT
);
2879 return SOCKET_ERROR
;
2882 fd
= get_sock_fd( s
, 0, NULL
);
2887 union generic_unix_sockaddr uaddr
;
2888 socklen_t uaddrlen
= sizeof(uaddr
);
2890 if (getsockname(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
2892 SetLastError(wsaErrno());
2894 else if (!is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
2896 SetLastError(WSAEINVAL
);
2898 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
2900 /* The buffer was too small */
2901 SetLastError(WSAEFAULT
);
2907 release_sock_fd( s
, fd
);
2912 /***********************************************************************
2913 * getsockopt (WS2_32.7)
2915 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
2916 INT optname
, char *optval
, INT
*optlen
)
2921 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
2922 s
, level
, optname
, optval
, *optlen
);
2930 /* Handle common cases. The special cases are below, sorted
2932 case WS_SO_BROADCAST
:
2935 case WS_SO_KEEPALIVE
:
2936 case WS_SO_OOBINLINE
:
2938 case WS_SO_REUSEADDR
:
2940 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2941 return SOCKET_ERROR
;
2942 convert_sockopt(&level
, &optname
);
2943 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
2945 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2948 release_sock_fd( s
, fd
);
2950 case WS_SO_ACCEPTCONN
:
2951 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2952 return SOCKET_ERROR
;
2953 if (getsockopt(fd
, SOL_SOCKET
, SO_ACCEPTCONN
, optval
, (socklen_t
*)optlen
) != 0 )
2955 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2959 /* BSD returns != 0 while Windows return exact == 1 */
2960 if (*(int *)optval
) *(int *)optval
= 1;
2962 release_sock_fd( s
, fd
);
2964 case WS_SO_DONTLINGER
:
2966 struct linger lingval
;
2967 socklen_t len
= sizeof(struct linger
);
2969 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
2971 SetLastError(WSAEFAULT
);
2972 return SOCKET_ERROR
;
2974 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2975 return SOCKET_ERROR
;
2977 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
2979 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2984 *(BOOL
*)optval
= !lingval
.l_onoff
;
2985 *optlen
= sizeof(BOOL
);
2988 release_sock_fd( s
, fd
);
2992 case WS_SO_CONNECT_TIME
:
2994 static int pretendtime
= 0;
2995 struct WS_sockaddr addr
;
2996 int len
= sizeof(addr
);
2998 if (!optlen
|| *optlen
< sizeof(DWORD
) || !optval
)
3000 SetLastError(WSAEFAULT
);
3001 return SOCKET_ERROR
;
3003 if (WS_getpeername(s
, &addr
, &len
) == SOCKET_ERROR
)
3004 *(DWORD
*)optval
= ~0u;
3007 if (!pretendtime
) FIXME("WS_SO_CONNECT_TIME - faking results\n");
3008 *(DWORD
*)optval
= pretendtime
++;
3010 *optlen
= sizeof(DWORD
);
3013 /* As mentioned in setsockopt, Windows ignores this, so we
3014 * always return true here */
3015 case WS_SO_DONTROUTE
:
3016 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
3018 SetLastError(WSAEFAULT
);
3019 return SOCKET_ERROR
;
3021 *(BOOL
*)optval
= TRUE
;
3022 *optlen
= sizeof(BOOL
);
3027 struct linger lingval
;
3029 socklen_t len
= sizeof(struct linger
), slen
= sizeof(int);
3031 /* struct linger and LINGER have different sizes */
3032 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
3034 SetLastError(WSAEFAULT
);
3035 return SOCKET_ERROR
;
3037 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3038 return SOCKET_ERROR
;
3040 if ((getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &so_type
, &slen
) == 0 && so_type
== SOCK_DGRAM
))
3042 SetLastError(WSAENOPROTOOPT
);
3045 else if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0)
3047 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3052 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
3053 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
3054 *optlen
= sizeof(struct linger
);
3057 release_sock_fd( s
, fd
);
3061 case WS_SO_MAX_MSG_SIZE
:
3062 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
3064 SetLastError(WSAEFAULT
);
3065 return SOCKET_ERROR
;
3067 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
3068 *(int *)optval
= 65507;
3069 *optlen
= sizeof(int);
3072 /* SO_OPENTYPE does not require a valid socket handle. */
3073 case WS_SO_OPENTYPE
:
3074 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
3076 SetLastError(WSAEFAULT
);
3077 return SOCKET_ERROR
;
3079 *(int *)optval
= get_per_thread_data()->opentype
;
3080 *optlen
= sizeof(int);
3081 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
3083 case WS_SO_PROTOCOL_INFOA
:
3084 case WS_SO_PROTOCOL_INFOW
:
3087 WSAPROTOCOL_INFOW infow
;
3089 ret
= ws_protocol_info(s
, optname
== WS_SO_PROTOCOL_INFOW
, &infow
, &size
);
3092 if (!optlen
|| !optval
|| *optlen
< size
)
3094 if(optlen
) *optlen
= size
;
3096 SetLastError(WSAEFAULT
);
3099 memcpy(optval
, &infow
, size
);
3101 return ret
? 0 : SOCKET_ERROR
;
3104 case WS_SO_RCVTIMEO
:
3107 case WS_SO_SNDTIMEO
:
3109 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
3112 socklen_t len
= sizeof(struct timeval
);
3114 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
3116 SetLastError(WSAEFAULT
);
3117 return SOCKET_ERROR
;
3119 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3120 return SOCKET_ERROR
;
3122 convert_sockopt(&level
, &optname
);
3123 if (getsockopt(fd
, level
, optname
, &tv
, &len
) != 0 )
3125 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3130 *(int *)optval
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
3131 *optlen
= sizeof(int);
3134 release_sock_fd( s
, fd
);
3140 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
3142 SetLastError(WSAEFAULT
);
3143 return SOCKET_ERROR
;
3145 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3146 return SOCKET_ERROR
;
3148 if (getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, optval
, (socklen_t
*)optlen
) != 0 )
3150 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3154 (*(int *)optval
) = convert_socktype_u2w(*(int *)optval
);
3156 release_sock_fd( s
, fd
);
3160 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
3161 SetLastError(WSAENOPROTOOPT
);
3162 return SOCKET_ERROR
;
3163 } /* end switch(optname) */
3164 }/* end case WS_SOL_SOCKET */
3168 struct WS_sockaddr_ipx addr
;
3169 IPX_ADDRESS_DATA
*data
;
3174 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
3176 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (socklen_t
*)optlen
) == -1)
3183 socklen_t len
=sizeof(struct ipx
);
3184 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
3187 *optval
= (int)val
.ipx_pt
;
3190 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
3191 release_sock_fd( s
, fd
);
3196 * On a Win2000 system with one network card there are usually
3197 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
3198 * Using this call you can then retrieve info about this all.
3199 * In case of Linux it is a bit different. Usually you have
3200 * only "one" device active and further it is not possible to
3201 * query things like the linkspeed.
3203 FIXME("IPX_ADDRESS\n");
3204 namelen
= sizeof(struct WS_sockaddr_ipx
);
3205 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
3206 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
3208 data
= (IPX_ADDRESS_DATA
*)optval
;
3209 memcpy(data
->nodenum
,addr
.sa_nodenum
,sizeof(data
->nodenum
));
3210 memcpy(data
->netnum
,addr
.sa_netnum
,sizeof(data
->netnum
));
3211 data
->adapternum
= 0;
3212 data
->wan
= FALSE
; /* We are not on a wan for now .. */
3213 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
3214 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
3215 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
3216 * note 1MB = 1000kB in this case */
3219 case IPX_MAX_ADAPTER_NUM
:
3220 FIXME("IPX_MAX_ADAPTER_NUM\n");
3221 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
3225 FIXME("IPX optname:%x\n", optname
);
3226 return SOCKET_ERROR
;
3227 }/* end switch(optname) */
3228 } /* end case NSPROTO_IPX */
3235 case WS_IRLMP_ENUMDEVICES
:
3237 static const int MAX_IRDA_DEVICES
= 10;
3238 char buf
[sizeof(struct irda_device_list
) +
3239 (MAX_IRDA_DEVICES
- 1) * sizeof(struct irda_device_info
)];
3241 socklen_t len
= sizeof(buf
);
3243 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3244 return SOCKET_ERROR
;
3245 res
= getsockopt( fd
, SOL_IRLMP
, IRLMP_ENUMDEVICES
, buf
, &len
);
3248 SetLastError(wsaErrno());
3249 return SOCKET_ERROR
;
3253 struct irda_device_list
*src
= (struct irda_device_list
*)buf
;
3254 DEVICELIST
*dst
= (DEVICELIST
*)optval
;
3255 INT needed
= sizeof(DEVICELIST
);
3259 needed
+= (src
->len
- 1) * sizeof(IRDA_DEVICE_INFO
);
3260 if (*optlen
< needed
)
3262 SetLastError(WSAEFAULT
);
3263 return SOCKET_ERROR
;
3266 TRACE("IRLMP_ENUMDEVICES: %d devices found:\n", src
->len
);
3267 dst
->numDevice
= src
->len
;
3268 for (i
= 0; i
< src
->len
; i
++)
3270 TRACE("saddr = %08x, daddr = %08x, info = %s, hints = %02x%02x\n",
3271 src
->dev
[i
].saddr
, src
->dev
[i
].daddr
,
3272 src
->dev
[i
].info
, src
->dev
[i
].hints
[0],
3273 src
->dev
[i
].hints
[1]);
3274 memcpy( dst
->Device
[i
].irdaDeviceID
,
3276 sizeof(dst
->Device
[i
].irdaDeviceID
) ) ;
3277 memcpy( dst
->Device
[i
].irdaDeviceName
,
3279 sizeof(dst
->Device
[i
].irdaDeviceName
) ) ;
3280 memcpy( &dst
->Device
[i
].irdaDeviceHints1
,
3281 &src
->dev
[i
].hints
[0],
3282 sizeof(dst
->Device
[i
].irdaDeviceHints1
) ) ;
3283 memcpy( &dst
->Device
[i
].irdaDeviceHints2
,
3284 &src
->dev
[i
].hints
[1],
3285 sizeof(dst
->Device
[i
].irdaDeviceHints2
) ) ;
3286 dst
->Device
[i
].irdaCharSet
= src
->dev
[i
].charset
;
3292 FIXME("IrDA optname:0x%x\n", optname
);
3293 return SOCKET_ERROR
;
3295 break; /* case WS_SOL_IRLMP */
3298 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
3299 case WS_IPPROTO_TCP
:
3302 case WS_TCP_NODELAY
:
3303 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3304 return SOCKET_ERROR
;
3305 convert_sockopt(&level
, &optname
);
3306 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
3308 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3311 release_sock_fd( s
, fd
);
3314 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
3315 return SOCKET_ERROR
;
3320 case WS_IP_ADD_MEMBERSHIP
:
3321 case WS_IP_DROP_MEMBERSHIP
:
3325 case WS_IP_MULTICAST_IF
:
3326 case WS_IP_MULTICAST_LOOP
:
3327 case WS_IP_MULTICAST_TTL
:
3334 #ifdef IP_UNICAST_IF
3335 case WS_IP_UNICAST_IF
:
3337 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3338 return SOCKET_ERROR
;
3339 convert_sockopt(&level
, &optname
);
3340 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
3342 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3345 release_sock_fd( s
, fd
);
3347 case WS_IP_DONTFRAGMENT
:
3348 FIXME("WS_IP_DONTFRAGMENT is always false!\n");
3349 *(BOOL
*)optval
= FALSE
;
3352 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
3353 return SOCKET_ERROR
;
3355 case WS_IPPROTO_IPV6
:
3358 #ifdef IPV6_ADD_MEMBERSHIP
3359 case WS_IPV6_ADD_MEMBERSHIP
:
3361 #ifdef IPV6_DROP_MEMBERSHIP
3362 case WS_IPV6_DROP_MEMBERSHIP
:
3364 case WS_IPV6_MULTICAST_IF
:
3365 case WS_IPV6_MULTICAST_HOPS
:
3366 case WS_IPV6_MULTICAST_LOOP
:
3367 case WS_IPV6_UNICAST_HOPS
:
3368 case WS_IPV6_V6ONLY
:
3369 #ifdef IPV6_UNICAST_IF
3370 case WS_IPV6_UNICAST_IF
:
3372 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3373 return SOCKET_ERROR
;
3374 convert_sockopt(&level
, &optname
);
3375 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
3377 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3380 release_sock_fd( s
, fd
);
3382 case WS_IPV6_DONTFRAG
:
3383 FIXME("WS_IPV6_DONTFRAG is always false!\n");
3384 *(BOOL
*)optval
= FALSE
;
3387 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
3388 return SOCKET_ERROR
;
3391 WARN("Unknown level: 0x%08x\n", level
);
3392 SetLastError(WSAEINVAL
);
3393 return SOCKET_ERROR
;
3394 } /* end switch(level) */
3397 /***********************************************************************
3400 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
3402 return htonl(hostlong
);
3406 /***********************************************************************
3409 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
3411 return htons(hostshort
);
3414 /***********************************************************************
3415 * WSAHtonl (WS2_32.46)
3416 * From MSDN description of error codes, this function should also
3417 * check if WinSock has been initialized and the socket is a valid
3418 * socket. But why? This function only translates a host byte order
3419 * u_long into a network byte order u_long...
3421 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
3425 *lpnetlong
= htonl(hostlong
);
3428 WSASetLastError(WSAEFAULT
);
3429 return SOCKET_ERROR
;
3432 /***********************************************************************
3433 * WSAHtons (WS2_32.47)
3434 * From MSDN description of error codes, this function should also
3435 * check if WinSock has been initialized and the socket is a valid
3436 * socket. But why? This function only translates a host byte order
3437 * u_short into a network byte order u_short...
3439 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
3444 *lpnetshort
= htons(hostshort
);
3447 WSASetLastError(WSAEFAULT
);
3448 return SOCKET_ERROR
;
3452 /***********************************************************************
3453 * inet_addr (WS2_32.11)
3455 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
3457 if (!cp
) return INADDR_NONE
;
3458 return inet_addr(cp
);
3462 /***********************************************************************
3465 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
3467 return ntohl(netlong
);
3471 /***********************************************************************
3474 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
3476 return ntohs(netshort
);
3480 /***********************************************************************
3481 * inet_ntoa (WS2_32.12)
3483 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
3485 /* use "buffer for dummies" here because some applications have a
3486 * propensity to decode addresses in ws_hostent structure without
3487 * saving them first...
3489 static char dbuffer
[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
3491 char* s
= inet_ntoa(*((struct in_addr
*)&in
));
3497 SetLastError(wsaErrno());
3501 static const char *debugstr_wsaioctl(DWORD ioctl
)
3503 const char *buf_type
, *family
;
3505 switch(ioctl
& 0x18000000)
3510 case WS_IOC_PROTOCOL
:
3511 family
= "IOC_PROTOCOL";
3514 family
= "IOC_VENDOR";
3516 default: /* WS_IOC_UNIX */
3518 BYTE size
= (ioctl
>> 16) & WS_IOCPARM_MASK
;
3519 char x
= (ioctl
& 0xff00) >> 8;
3520 BYTE y
= ioctl
& 0xff;
3523 switch (ioctl
& (WS_IOC_VOID
|WS_IOC_INOUT
))
3527 sprintf(args
, "%d, %d", x
, y
);
3531 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3535 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3539 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3542 return wine_dbg_sprintf("%s(%s)", buf_type
, args
);
3546 /* We are different from WS_IOC_UNIX. */
3547 switch (ioctl
& (WS_IOC_VOID
|WS_IOC_INOUT
))
3550 buf_type
= "_WSAIO";
3553 buf_type
= "_WSAIORW";
3556 buf_type
= "_WSAIOW";
3559 buf_type
= "_WSAIOR";
3566 return wine_dbg_sprintf("%s(%s, %d)", buf_type
, family
,
3567 (USHORT
)(ioctl
& 0xffff));
3570 /**********************************************************************
3571 * WSAIoctl (WS2_32.50)
3574 INT WINAPI
WSAIoctl(SOCKET s
, DWORD code
, LPVOID in_buff
, DWORD in_size
, LPVOID out_buff
,
3575 DWORD out_size
, LPDWORD ret_size
, LPWSAOVERLAPPED overlapped
,
3576 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
3579 DWORD status
= 0, total
= 0;
3581 TRACE("%ld, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
3582 s
, code
, in_buff
, in_size
, out_buff
, out_size
, ret_size
, overlapped
, completion
);
3587 if (in_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(in_buff
))
3589 WSASetLastError(WSAEFAULT
);
3590 return SOCKET_ERROR
;
3592 if (_get_sock_mask(s
))
3594 /* AsyncSelect()'ed sockets are always nonblocking */
3595 if (!*(WS_u_long
*)in_buff
) status
= WSAEINVAL
;
3598 if (*(WS_u_long
*)in_buff
)
3599 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
3601 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
3606 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
3608 WSASetLastError(WSAEFAULT
);
3609 return SOCKET_ERROR
;
3611 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
3612 if (ioctl(fd
, FIONREAD
, out_buff
) == -1)
3613 status
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
3614 release_sock_fd( s
, fd
);
3620 unsigned int oob
= 0, atmark
= 0;
3621 socklen_t oobsize
= sizeof(int);
3622 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
3624 WSASetLastError(WSAEFAULT
);
3625 return SOCKET_ERROR
;
3627 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
3628 /* SO_OOBINLINE sockets must always return TRUE to SIOCATMARK */
3629 if ((getsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &oob
, &oobsize
) == -1)
3630 || (!oob
&& ioctl(fd
, SIOCATMARK
, &atmark
) == -1))
3631 status
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
3634 /* The SIOCATMARK value read from ioctl() is reversed
3635 * because BSD returns TRUE if it's in the OOB mark
3636 * while Windows returns TRUE if there are NO OOB bytes.
3638 (*(WS_u_long
*) out_buff
) = oob
| !atmark
;
3641 release_sock_fd( s
, fd
);
3646 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
3647 SetLastError(WSAEINVAL
);
3648 return SOCKET_ERROR
;
3650 case WS_SIO_GET_INTERFACE_LIST
:
3652 INTERFACE_INFO
* intArray
= out_buff
;
3653 DWORD size
, numInt
= 0, apiReturn
;
3655 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
3657 if (!out_buff
|| !ret_size
)
3659 WSASetLastError(WSAEFAULT
);
3660 return SOCKET_ERROR
;
3663 fd
= get_sock_fd( s
, 0, NULL
);
3664 if (fd
== -1) return SOCKET_ERROR
;
3666 apiReturn
= GetAdaptersInfo(NULL
, &size
);
3667 if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
3669 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
3673 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
3675 PIP_ADAPTER_INFO ptr
;
3677 for (ptr
= table
, numInt
= 0; ptr
; ptr
= ptr
->Next
)
3679 unsigned int addr
, mask
, bcast
;
3680 struct ifreq ifInfo
;
3682 /* Skip interfaces without an IPv4 address. */
3683 if (ptr
->IpAddressList
.IpAddress
.String
[0] == '\0')
3686 if ((numInt
+ 1)*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > out_size
)
3688 WARN("Buffer too small = %u, out_size = %u\n", numInt
+ 1, out_size
);
3693 /* Socket Status Flags */
3694 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
3695 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
3697 ERR("Error obtaining status flags for socket!\n");
3703 /* set flags; the values of IFF_* are not the same
3704 under Linux and Windows, therefore must generate
3706 intArray
->iiFlags
= 0;
3707 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
3708 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
3709 #ifdef IFF_POINTOPOINT
3710 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
3711 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
3713 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
3714 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
3715 if (ifInfo
.ifr_flags
& IFF_UP
)
3716 intArray
->iiFlags
|= WS_IFF_UP
;
3717 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
3718 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
3721 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
3722 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
3723 bcast
= addr
| ~mask
;
3724 intArray
->iiAddress
.AddressIn
.sin_family
= AF_INET
;
3725 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
3726 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
=
3728 intArray
->iiNetmask
.AddressIn
.sin_family
= AF_INET
;
3729 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
3730 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
=
3732 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
=
3734 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
3735 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.
3743 ERR("Unable to get interface table!\n");
3746 HeapFree(GetProcessHeap(),0,table
);
3748 else status
= WSAEINVAL
;
3750 else if (apiReturn
!= ERROR_NO_DATA
)
3752 ERR("Unable to get interface table!\n");
3755 /* Calculate the size of the array being returned */
3756 total
= sizeof(INTERFACE_INFO
) * numInt
;
3757 release_sock_fd( s
, fd
);
3761 case WS_SIO_ADDRESS_LIST_CHANGE
:
3762 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
3763 /* FIXME: error and return code depend on whether socket was created
3764 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
3767 case WS_SIO_ADDRESS_LIST_QUERY
:
3771 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
3775 WSASetLastError(WSAEFAULT
);
3776 return SOCKET_ERROR
;
3779 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
3781 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
3784 if (!table
|| GetAdaptersInfo(table
, &size
))
3786 HeapFree(GetProcessHeap(), 0, table
);
3791 for (p
= table
, num
= 0; p
; p
= p
->Next
)
3792 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
3794 total
= sizeof(SOCKET_ADDRESS_LIST
) + sizeof(SOCKET_ADDRESS
) * (num
- 1);
3795 total
+= sizeof(SOCKADDR
) * num
;
3797 if (total
> out_size
)
3799 HeapFree(GetProcessHeap(), 0, table
);
3808 SOCKET_ADDRESS_LIST
*sa_list
= out_buff
;
3809 SOCKADDR_IN
*sockaddr
;
3811 sa
= sa_list
->Address
;
3812 sockaddr
= (SOCKADDR_IN
*)((char *)sa
+ num
* sizeof(SOCKET_ADDRESS
));
3813 sa_list
->iAddressCount
= num
;
3815 for (p
= table
, i
= 0; p
; p
= p
->Next
)
3817 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
3819 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
3820 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
3822 sockaddr
[i
].sin_family
= AF_INET
;
3823 sockaddr
[i
].sin_port
= 0;
3824 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
3829 HeapFree(GetProcessHeap(), 0, table
);
3833 WARN("unable to get IP address list\n");
3840 FIXME("SIO_FLUSH: stub.\n");
3843 case WS_SIO_GET_EXTENSION_FUNCTION_POINTER
:
3845 static const GUID connectex_guid
= WSAID_CONNECTEX
;
3846 static const GUID disconnectex_guid
= WSAID_DISCONNECTEX
;
3847 static const GUID acceptex_guid
= WSAID_ACCEPTEX
;
3848 static const GUID getaccepexsockaddrs_guid
= WSAID_GETACCEPTEXSOCKADDRS
;
3849 static const GUID transmitfile_guid
= WSAID_TRANSMITFILE
;
3850 static const GUID transmitpackets_guid
= WSAID_TRANSMITPACKETS
;
3851 static const GUID wsarecvmsg_guid
= WSAID_WSARECVMSG
;
3852 static const GUID wsasendmsg_guid
= WSAID_WSASENDMSG
;
3854 if ( IsEqualGUID(&connectex_guid
, in_buff
) )
3856 *(LPFN_CONNECTEX
*)out_buff
= WS2_ConnectEx
;
3859 else if ( IsEqualGUID(&disconnectex_guid
, in_buff
) )
3861 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented DisconnectEx\n");
3863 else if ( IsEqualGUID(&acceptex_guid
, in_buff
) )
3865 *(LPFN_ACCEPTEX
*)out_buff
= WS2_AcceptEx
;
3868 else if ( IsEqualGUID(&getaccepexsockaddrs_guid
, in_buff
) )
3870 *(LPFN_GETACCEPTEXSOCKADDRS
*)out_buff
= WS2_GetAcceptExSockaddrs
;
3873 else if ( IsEqualGUID(&transmitfile_guid
, in_buff
) )
3875 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitFile\n");
3877 else if ( IsEqualGUID(&transmitpackets_guid
, in_buff
) )
3879 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitPackets\n");
3881 else if ( IsEqualGUID(&wsarecvmsg_guid
, in_buff
) )
3883 *(LPFN_WSARECVMSG
*)out_buff
= WS2_WSARecvMsg
;
3886 else if ( IsEqualGUID(&wsasendmsg_guid
, in_buff
) )
3888 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented WSASendMsg\n");
3891 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER %s: stub\n", debugstr_guid(in_buff
));
3893 status
= WSAEOPNOTSUPP
;
3896 case WS_SIO_KEEPALIVE_VALS
:
3898 struct tcp_keepalive
*k
;
3899 int keepalive
, keepidle
, keepintvl
;
3901 if (!in_buff
|| in_size
< sizeof(struct tcp_keepalive
))
3903 WSASetLastError(WSAEFAULT
);
3904 return SOCKET_ERROR
;
3908 keepalive
= k
->onoff
? 1 : 0;
3909 keepidle
= max( 1, (k
->keepalivetime
+ 500) / 1000 );
3910 keepintvl
= max( 1, (k
->keepaliveinterval
+ 500) / 1000 );
3912 TRACE("onoff: %d, keepalivetime: %d, keepaliveinterval: %d\n", keepalive
, keepidle
, keepintvl
);
3914 fd
= get_sock_fd(s
, 0, NULL
);
3915 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&keepalive
, sizeof(int)) == -1)
3917 #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL)
3918 /* these values need to be set only if SO_KEEPALIVE is enabled */
3921 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, (void *)&keepidle
, sizeof(int)) == -1)
3923 else if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, (void *)&keepintvl
, sizeof(int)) == -1)
3928 FIXME("ignoring keepalive interval and timeout\n");
3930 release_sock_fd(s
, fd
);
3933 case WS_SIO_ROUTING_INTERFACE_QUERY
:
3935 struct WS_sockaddr
*daddr
= (struct WS_sockaddr
*)in_buff
;
3936 struct WS_sockaddr_in
*daddr_in
= (struct WS_sockaddr_in
*)daddr
;
3937 struct WS_sockaddr_in
*saddr_in
= out_buff
;
3938 MIB_IPFORWARDROW row
;
3939 PMIB_IPADDRTABLE ipAddrTable
= NULL
;
3940 DWORD size
, i
, found_index
;
3942 TRACE("-> WS_SIO_ROUTING_INTERFACE_QUERY request\n");
3944 if (!in_buff
|| in_size
< sizeof(struct WS_sockaddr
) ||
3945 !out_buff
|| out_size
< sizeof(struct WS_sockaddr_in
) || !ret_size
)
3947 WSASetLastError(WSAEFAULT
);
3948 return SOCKET_ERROR
;
3950 if (daddr
->sa_family
!= AF_INET
)
3952 FIXME("unsupported address family %d\n", daddr
->sa_family
);
3953 status
= WSAEAFNOSUPPORT
;
3956 if (GetBestRoute(daddr_in
->sin_addr
.S_un
.S_addr
, 0, &row
) != NOERROR
||
3957 GetIpAddrTable(NULL
, &size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
3962 ipAddrTable
= HeapAlloc(GetProcessHeap(), 0, size
);
3963 if (GetIpAddrTable(ipAddrTable
, &size
, FALSE
))
3965 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3969 for (i
= 0, found_index
= ipAddrTable
->dwNumEntries
;
3970 i
< ipAddrTable
->dwNumEntries
; i
++)
3972 if (ipAddrTable
->table
[i
].dwIndex
== row
.dwForwardIfIndex
)
3975 if (found_index
== ipAddrTable
->dwNumEntries
)
3977 ERR("no matching IP address for interface %d\n",
3978 row
.dwForwardIfIndex
);
3979 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3983 saddr_in
->sin_family
= AF_INET
;
3984 saddr_in
->sin_addr
.S_un
.S_addr
= ipAddrTable
->table
[found_index
].dwAddr
;
3985 saddr_in
->sin_port
= 0;
3986 total
= sizeof(struct WS_sockaddr_in
);
3987 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3990 case WS_SIO_SET_COMPATIBILITY_MODE
:
3991 TRACE("WS_SIO_SET_COMPATIBILITY_MODE ignored\n");
3992 status
= WSAEOPNOTSUPP
;
3994 case WS_SIO_UDP_CONNRESET
:
3995 FIXME("WS_SIO_UDP_CONNRESET stub\n");
3997 case 0x667e: /* Netscape tries hard to use bogus ioctl 0x667e */
3998 WSASetLastError(WSAEOPNOTSUPP
);
3999 return SOCKET_ERROR
;
4001 FIXME("unsupported WS_IOCTL cmd (%s)\n", debugstr_wsaioctl(code
));
4002 status
= WSAEOPNOTSUPP
;
4008 FIXME( "completion routine %p not supported\n", completion
);
4010 else if (overlapped
)
4012 ULONG_PTR cvalue
= (overlapped
&& ((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)overlapped
: 0;
4013 overlapped
->Internal
= status
;
4014 overlapped
->InternalHigh
= total
;
4015 if (overlapped
->hEvent
) NtSetEvent( overlapped
->hEvent
, NULL
);
4016 if (cvalue
) WS_AddCompletion( HANDLE2SOCKET(s
), cvalue
, status
, total
);
4021 if (ret_size
) *ret_size
= total
;
4024 SetLastError( status
);
4025 return SOCKET_ERROR
;
4029 /***********************************************************************
4030 * ioctlsocket (WS2_32.10)
4032 int WINAPI
WS_ioctlsocket(SOCKET s
, LONG cmd
, WS_u_long
*argp
)
4035 return WSAIoctl( s
, cmd
, argp
, sizeof(WS_u_long
), argp
, sizeof(WS_u_long
), &ret_size
, NULL
, NULL
);
4038 /***********************************************************************
4039 * listen (WS2_32.13)
4041 int WINAPI
WS_listen(SOCKET s
, int backlog
)
4043 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
), ret
= SOCKET_ERROR
;
4045 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
4048 union generic_unix_sockaddr uaddr
;
4049 socklen_t uaddrlen
= sizeof(uaddr
);
4051 if (getsockname(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
4053 SetLastError(wsaErrno());
4055 else if (!is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
4057 SetLastError(WSAEINVAL
);
4059 else if (listen(fd
, backlog
) == 0)
4061 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
4063 FD_CONNECT
|FD_WINE_CONNECTED
);
4067 SetLastError(wsaErrno());
4068 release_sock_fd( s
, fd
);
4071 SetLastError(WSAENOTSOCK
);
4075 /***********************************************************************
4078 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
4080 DWORD n
, dwFlags
= flags
;
4086 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
4087 return SOCKET_ERROR
;
4092 /***********************************************************************
4093 * recvfrom (WS2_32.17)
4095 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
4096 struct WS_sockaddr
*from
, int *fromlen
)
4098 DWORD n
, dwFlags
= flags
;
4104 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
4105 return SOCKET_ERROR
;
4110 /* allocate a poll array for the corresponding fd sets */
4111 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
4112 const WS_fd_set
*exceptfds
, int *count_ptr
)
4114 unsigned int i
, j
= 0, count
= 0;
4117 if (readfds
) count
+= readfds
->fd_count
;
4118 if (writefds
) count
+= writefds
->fd_count
;
4119 if (exceptfds
) count
+= exceptfds
->fd_count
;
4123 SetLastError(WSAEINVAL
);
4126 if (!(fds
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(fds
[0]))))
4128 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
4132 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
4134 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
4135 if (fds
[j
].fd
== -1) goto failed
;
4136 fds
[j
].events
= POLLIN
;
4140 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
4142 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
4143 if (fds
[j
].fd
== -1) goto failed
;
4144 fds
[j
].events
= POLLOUT
;
4148 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
4150 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
4151 if (fds
[j
].fd
== -1) goto failed
;
4152 fds
[j
].events
= POLLHUP
;
4161 for (i
= 0; i
< readfds
->fd_count
&& j
< count
; i
++, j
++)
4162 release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
4164 for (i
= 0; i
< writefds
->fd_count
&& j
< count
; i
++, j
++)
4165 release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
4167 for (i
= 0; i
< exceptfds
->fd_count
&& j
< count
; i
++, j
++)
4168 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
4169 HeapFree( GetProcessHeap(), 0, fds
);
4173 /* release the file descriptor obtained in fd_sets_to_poll */
4174 /* must be called with the original fd_set arrays, before calling get_poll_results */
4175 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
4176 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
4178 unsigned int i
, j
= 0;
4182 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
4183 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
4187 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
4188 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
4192 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
4193 if (fds
[j
].fd
!= -1)
4195 /* make sure we have a real error before releasing the fd */
4196 if (!sock_error_p( fds
[j
].fd
)) fds
[j
].revents
= 0;
4197 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
4202 /* map the poll results back into the Windows fd sets */
4203 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
4204 const struct pollfd
*fds
)
4206 unsigned int i
, j
= 0, k
, total
= 0;
4210 for (i
= k
= 0; i
< readfds
->fd_count
; i
++, j
++)
4211 if (fds
[j
].revents
) readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
4212 readfds
->fd_count
= k
;
4217 for (i
= k
= 0; i
< writefds
->fd_count
; i
++, j
++)
4218 if ((fds
[j
].revents
& POLLOUT
) && !(fds
[j
].revents
& POLLHUP
))
4219 writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
4220 writefds
->fd_count
= k
;
4225 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
4226 if (fds
[j
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
4227 exceptfds
->fd_count
= k
;
4234 /***********************************************************************
4235 * select (WS2_32.18)
4237 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
4238 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
4239 const struct WS_timeval
* ws_timeout
)
4241 struct pollfd
*pollfds
;
4242 struct timeval tv1
, tv2
;
4244 int count
, ret
, timeout
= -1;
4246 TRACE("read %p, write %p, excp %p timeout %p\n",
4247 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
4249 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)))
4250 return SOCKET_ERROR
;
4254 torig
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
4256 gettimeofday( &tv1
, 0 );
4259 while ((ret
= poll( pollfds
, count
, timeout
)) < 0)
4263 if (!ws_timeout
) continue;
4264 gettimeofday( &tv2
, 0 );
4266 tv2
.tv_sec
-= tv1
.tv_sec
;
4267 tv2
.tv_usec
-= tv1
.tv_usec
;
4268 if (tv2
.tv_usec
< 0)
4270 tv2
.tv_usec
+= 1000000;
4274 timeout
= torig
- (tv2
.tv_sec
* 1000) - (tv2
.tv_usec
+ 999) / 1000;
4275 if (timeout
<= 0) break;
4278 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
4280 if (ret
== -1) SetLastError(wsaErrno());
4281 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
4282 HeapFree( GetProcessHeap(), 0, pollfds
);
4286 /* helper to send completion messages for client-only i/o operation case */
4287 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
,
4290 SERVER_START_REQ( add_fd_completion
)
4292 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(sock
) );
4293 req
->cvalue
= CompletionValue
;
4294 req
->status
= CompletionStatus
;
4295 req
->information
= Information
;
4296 wine_server_call( req
);
4302 /***********************************************************************
4305 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
4311 wsabuf
.buf
= (char*) buf
;
4313 if ( WS2_sendto( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
4314 return SOCKET_ERROR
;
4319 /***********************************************************************
4320 * WSASend (WS2_32.72)
4322 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4323 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
4324 LPWSAOVERLAPPED lpOverlapped
,
4325 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4327 return WS2_sendto( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
4328 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
4331 /***********************************************************************
4332 * WSASendDisconnect (WS2_32.73)
4334 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
4336 return WS_shutdown( s
, SD_SEND
);
4340 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4341 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
4342 const struct WS_sockaddr
*to
, int tolen
,
4343 LPWSAOVERLAPPED lpOverlapped
,
4344 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4346 unsigned int i
, options
;
4348 struct ws2_async
*wsa
= NULL
;
4349 int totalLength
= 0;
4350 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
4354 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
4355 s
, lpBuffers
, dwBufferCount
, dwFlags
,
4356 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
4358 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
4359 TRACE( "fd=%d, options=%x\n", fd
, options
);
4361 if ( fd
== -1 ) return SOCKET_ERROR
;
4363 if (!lpOverlapped
&& !lpNumberOfBytesSent
)
4368 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
4374 wsa
->hSocket
= SOCKET2HANDLE(s
);
4375 wsa
->addr
= (struct WS_sockaddr
*)to
;
4376 wsa
->addrlen
.val
= tolen
;
4377 wsa
->flags
= dwFlags
;
4378 wsa
->lpFlags
= &wsa
->flags
;
4379 wsa
->control
= NULL
;
4380 wsa
->n_iovecs
= dwBufferCount
;
4381 wsa
->first_iovec
= 0;
4382 for ( i
= 0; i
< dwBufferCount
; i
++ )
4384 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
4385 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
4386 totalLength
+= lpBuffers
[i
].len
;
4391 n
= WS2_send( fd
, wsa
);
4392 if (n
!= -1 || errno
!= EINTR
) break;
4394 if (n
== -1 && errno
!= EAGAIN
)
4400 if ((lpOverlapped
|| lpCompletionRoutine
) &&
4401 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
4403 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
4405 wsa
->user_overlapped
= lpOverlapped
;
4406 wsa
->completion_func
= lpCompletionRoutine
;
4407 release_sock_fd( s
, fd
);
4409 if (n
== -1 || n
< totalLength
)
4411 iosb
->u
.Status
= STATUS_PENDING
;
4412 iosb
->Information
= n
== -1 ? 0 : n
;
4414 SERVER_START_REQ( register_async
)
4416 req
->type
= ASYNC_TYPE_WRITE
;
4417 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
4418 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
4419 req
->async
.iosb
= wine_server_client_ptr( iosb
);
4420 req
->async
.arg
= wine_server_client_ptr( wsa
);
4421 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
4422 req
->async
.cvalue
= cvalue
;
4423 err
= wine_server_call( req
);
4427 /* Enable the event only after starting the async. The server will deliver it as soon as
4428 the async is done. */
4429 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
4431 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
4432 WSASetLastError( NtStatusToWSAError( err
));
4433 return SOCKET_ERROR
;
4436 iosb
->u
.Status
= STATUS_SUCCESS
;
4437 iosb
->Information
= n
;
4438 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= n
;
4439 if (!wsa
->completion_func
)
4441 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
4442 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
4443 HeapFree( GetProcessHeap(), 0, wsa
);
4445 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
4446 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
4451 if ((err
= _is_blocking( s
, &is_blocking
)))
4453 err
= NtStatusToWSAError( err
);
4459 /* On a blocking non-overlapped stream socket,
4460 * sending blocks until the entire buffer is sent. */
4461 DWORD timeout_start
= GetTickCount();
4463 bytes_sent
= n
== -1 ? 0 : n
;
4465 while (wsa
->first_iovec
< wsa
->n_iovecs
)
4468 int timeout
= GET_SNDTIMEO(fd
);
4472 timeout
-= GetTickCount() - timeout_start
;
4473 if (timeout
< 0) timeout
= 0;
4477 pfd
.events
= POLLOUT
;
4479 if (!timeout
|| !poll( &pfd
, 1, timeout
))
4482 goto error
; /* msdn says a timeout in send is fatal */
4485 n
= WS2_send( fd
, wsa
);
4486 if (n
== -1 && errno
!= EAGAIN
&& errno
!= EINTR
)
4496 else /* non-blocking */
4498 if (n
< totalLength
)
4499 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
4502 err
= WSAEWOULDBLOCK
;
4508 TRACE(" -> %i bytes\n", bytes_sent
);
4510 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= bytes_sent
;
4511 HeapFree( GetProcessHeap(), 0, wsa
);
4512 release_sock_fd( s
, fd
);
4517 HeapFree( GetProcessHeap(), 0, wsa
);
4518 release_sock_fd( s
, fd
);
4519 WARN(" -> ERROR %d\n", err
);
4520 WSASetLastError(err
);
4521 return SOCKET_ERROR
;
4524 /***********************************************************************
4525 * WSASendTo (WS2_32.74)
4527 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4528 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
4529 const struct WS_sockaddr
*to
, int tolen
,
4530 LPWSAOVERLAPPED lpOverlapped
,
4531 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4533 return WS2_sendto( s
, lpBuffers
, dwBufferCount
,
4534 lpNumberOfBytesSent
, dwFlags
,
4536 lpOverlapped
, lpCompletionRoutine
);
4539 /***********************************************************************
4540 * sendto (WS2_32.20)
4542 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
4543 const struct WS_sockaddr
*to
, int tolen
)
4549 wsabuf
.buf
= (char*) buf
;
4551 if ( WS2_sendto(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
4552 return SOCKET_ERROR
;
4557 /***********************************************************************
4558 * setsockopt (WS2_32.21)
4560 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
4561 const char *optval
, int optlen
)
4565 struct linger linger
;
4566 struct timeval tval
;
4568 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
4569 s
, level
, optname
, optval
, optlen
);
4571 /* some broken apps pass the value directly instead of a pointer to it */
4572 if(optlen
&& IS_INTRESOURCE(optval
))
4574 SetLastError(WSAEFAULT
);
4575 return SOCKET_ERROR
;
4583 /* Some options need some conversion before they can be sent to
4584 * setsockopt. The conversions are done here, then they will fall though
4585 * to the general case. Special options that are not passed to
4586 * setsockopt follow below that.*/
4588 case WS_SO_DONTLINGER
:
4591 SetLastError(WSAEFAULT
);
4592 return SOCKET_ERROR
;
4594 linger
.l_onoff
= *(const int*)optval
== 0;
4595 linger
.l_linger
= 0;
4597 optname
= SO_LINGER
;
4598 optval
= (char*)&linger
;
4599 optlen
= sizeof(struct linger
);
4605 SetLastError(WSAEFAULT
);
4606 return SOCKET_ERROR
;
4608 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
4609 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
4611 optname
= SO_LINGER
;
4612 optval
= (char*)&linger
;
4613 optlen
= sizeof(struct linger
);
4617 if (*(const int*)optval
< 2048)
4619 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
4624 /* The options listed here don't need any special handling. Thanks to
4625 * the conversion happening above, options from there will fall through
4627 case WS_SO_ACCEPTCONN
:
4628 case WS_SO_BROADCAST
:
4630 case WS_SO_KEEPALIVE
:
4631 case WS_SO_OOBINLINE
:
4632 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
4633 * however, using it the BSD way fixes bug 8513 and seems to be what
4634 * most programmers assume, anyway */
4635 case WS_SO_REUSEADDR
:
4638 convert_sockopt(&level
, &optname
);
4641 /* SO_DEBUG is a privileged operation, ignore it. */
4643 TRACE("Ignoring SO_DEBUG\n");
4646 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
4647 * socket. According to MSDN, this option is silently ignored.*/
4648 case WS_SO_DONTROUTE
:
4649 TRACE("Ignoring SO_DONTROUTE\n");
4652 /* Stops two sockets from being bound to the same port. Always happens
4653 * on unix systems, so just drop it. */
4654 case WS_SO_EXCLUSIVEADDRUSE
:
4655 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
4658 /* After a ConnectEx call succeeds, the socket can't be used with half of the
4659 * normal winsock functions on windows. We don't have that problem. */
4660 case WS_SO_UPDATE_CONNECT_CONTEXT
:
4661 TRACE("Ignoring SO_UPDATE_CONNECT_CONTEXT, since our sockets are normal\n");
4664 /* After a AcceptEx call succeeds, the socket can't be used with half of the
4665 * normal winsock functions on windows. We don't have that problem. */
4666 case WS_SO_UPDATE_ACCEPT_CONTEXT
:
4667 TRACE("Ignoring SO_UPDATE_ACCEPT_CONTEXT, since our sockets are normal\n");
4670 /* SO_OPENTYPE does not require a valid socket handle. */
4671 case WS_SO_OPENTYPE
:
4672 if (!optlen
|| optlen
< sizeof(int) || !optval
)
4674 SetLastError(WSAEFAULT
);
4675 return SOCKET_ERROR
;
4677 get_per_thread_data()->opentype
= *(const int *)optval
;
4678 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((const int*)optval
) );
4682 case WS_SO_RCVTIMEO
:
4685 case WS_SO_SNDTIMEO
:
4687 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
4688 if (optval
&& optlen
== sizeof(UINT32
)) {
4689 /* WinSock passes milliseconds instead of struct timeval */
4690 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
4691 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
4692 /* min of 500 milliseconds */
4693 if (tval
.tv_sec
== 0 && tval
.tv_usec
&& tval
.tv_usec
< 500000)
4694 tval
.tv_usec
= 500000;
4695 optlen
= sizeof(struct timeval
);
4696 optval
= (char*)&tval
;
4697 } else if (optlen
== sizeof(struct timeval
)) {
4698 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
4700 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
4703 convert_sockopt(&level
, &optname
);
4708 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
4709 SetLastError(WSAENOPROTOOPT
);
4710 return SOCKET_ERROR
;
4712 break; /* case WS_SOL_SOCKET */
4719 fd
= get_sock_fd( s
, 0, NULL
);
4720 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval
, fd
);
4722 /* We try to set the ipx type on ipx socket level. */
4724 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, optlen
) == -1)
4726 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
4727 release_sock_fd( s
, fd
);
4728 return SOCKET_ERROR
;
4733 /* Should we retrieve val using a getsockopt call and then
4734 * set the modified one? */
4735 val
.ipx_pt
= *optval
;
4736 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
4739 release_sock_fd( s
, fd
);
4742 case IPX_FILTERPTYPE
:
4743 /* Sets the receive filter packet type, at the moment we don't support it */
4744 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
4745 /* Returning 0 is better for now than returning a SOCKET_ERROR */
4749 FIXME("opt_name:%x\n", optname
);
4750 return SOCKET_ERROR
;
4752 break; /* case NSPROTO_IPX */
4755 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
4756 case WS_IPPROTO_TCP
:
4759 case WS_TCP_NODELAY
:
4760 convert_sockopt(&level
, &optname
);
4763 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
4764 return SOCKET_ERROR
;
4771 case WS_IP_ADD_MEMBERSHIP
:
4772 case WS_IP_DROP_MEMBERSHIP
:
4776 case WS_IP_MULTICAST_IF
:
4777 case WS_IP_MULTICAST_LOOP
:
4778 case WS_IP_MULTICAST_TTL
:
4785 #ifdef IP_UNICAST_IF
4786 case WS_IP_UNICAST_IF
:
4788 convert_sockopt(&level
, &optname
);
4790 case WS_IP_DONTFRAGMENT
:
4791 FIXME("IP_DONTFRAGMENT is silently ignored!\n");
4794 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
4795 return SOCKET_ERROR
;
4799 case WS_IPPROTO_IPV6
:
4802 #ifdef IPV6_ADD_MEMBERSHIP
4803 case WS_IPV6_ADD_MEMBERSHIP
:
4805 #ifdef IPV6_DROP_MEMBERSHIP
4806 case WS_IPV6_DROP_MEMBERSHIP
:
4808 case WS_IPV6_MULTICAST_IF
:
4809 case WS_IPV6_MULTICAST_HOPS
:
4810 case WS_IPV6_MULTICAST_LOOP
:
4811 case WS_IPV6_UNICAST_HOPS
:
4812 case WS_IPV6_V6ONLY
:
4813 #ifdef IPV6_UNICAST_IF
4814 case WS_IPV6_UNICAST_IF
:
4816 convert_sockopt(&level
, &optname
);
4818 case WS_IPV6_DONTFRAG
:
4819 FIXME("IPV6_DONTFRAG is silently ignored!\n");
4821 case WS_IPV6_PROTECTION_LEVEL
:
4822 FIXME("IPV6_PROTECTION_LEVEL is ignored!\n");
4825 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
4826 return SOCKET_ERROR
;
4831 WARN("Unknown level: 0x%08x\n", level
);
4832 SetLastError(WSAEINVAL
);
4833 return SOCKET_ERROR
;
4834 } /* end switch(level) */
4836 /* avoid endianness issues if argument is a 16-bit int */
4837 if (optval
&& optlen
< sizeof(int))
4839 woptval
= *((const INT16
*) optval
);
4840 optval
= (char*) &woptval
;
4843 fd
= get_sock_fd( s
, 0, NULL
);
4844 if (fd
== -1) return SOCKET_ERROR
;
4846 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
4849 if (level
== SOL_SOCKET
&& optname
== SO_REUSEADDR
&&
4850 setsockopt(fd
, level
, SO_REUSEPORT
, optval
, optlen
) != 0)
4852 SetLastError(wsaErrno());
4853 release_sock_fd( s
, fd
);
4854 return SOCKET_ERROR
;
4857 release_sock_fd( s
, fd
);
4860 TRACE("Setting socket error, %d\n", wsaErrno());
4861 SetLastError(wsaErrno());
4862 release_sock_fd( s
, fd
);
4864 return SOCKET_ERROR
;
4867 /***********************************************************************
4868 * shutdown (WS2_32.22)
4870 int WINAPI
WS_shutdown(SOCKET s
, int how
)
4872 int fd
, err
= WSAENOTSOCK
;
4873 unsigned int options
, clear_flags
= 0;
4875 fd
= get_sock_fd( s
, 0, &options
);
4876 TRACE("socket %04lx, how %i %x\n", s
, how
, options
);
4879 return SOCKET_ERROR
;
4883 case 0: /* drop receives */
4884 clear_flags
|= FD_READ
;
4886 case 1: /* drop sends */
4887 clear_flags
|= FD_WRITE
;
4889 case 2: /* drop all */
4890 clear_flags
|= FD_READ
|FD_WRITE
;
4893 clear_flags
|= FD_WINE_LISTENING
;
4896 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
4901 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
4904 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
4908 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
4909 if (!err
) err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
4912 if (err
) goto error
;
4914 else /* non-overlapped mode */
4916 if ( shutdown( fd
, how
) )
4923 release_sock_fd( s
, fd
);
4924 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
4925 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
4929 release_sock_fd( s
, fd
);
4930 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
4931 WSASetLastError( err
);
4932 return SOCKET_ERROR
;
4935 /***********************************************************************
4936 * socket (WS2_32.23)
4938 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
4940 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
4942 return WSASocketA( af
, type
, protocol
, NULL
, 0,
4943 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
4947 /***********************************************************************
4948 * gethostbyaddr (WS2_32.51)
4950 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
4952 struct WS_hostent
*retval
= NULL
;
4953 struct hostent
* host
;
4955 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4958 struct hostent hostentry
;
4961 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
4963 int res
= gethostbyaddr_r(addr
, len
, type
,
4964 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
4965 if( res
!= ERANGE
) break;
4967 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
4969 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
4971 EnterCriticalSection( &csWSgetXXXbyYYY
);
4972 host
= gethostbyaddr(addr
, len
, type
);
4973 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
4975 if( host
!= NULL
) retval
= WS_dup_he(host
);
4976 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4977 HeapFree(GetProcessHeap(),0,extrabuf
);
4979 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4981 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
4985 /***********************************************************************
4986 * WS_get_local_ips (INTERNAL)
4988 * Returns the list of local IP addresses by going through the network
4989 * adapters and using the local routing table to sort the addresses
4990 * from highest routing priority to lowest routing priority. This
4991 * functionality is inferred from the description for obtaining local
4992 * IP addresses given in the Knowledge Base Article Q160215.
4994 * Please note that the returned hostent is only freed when the thread
4995 * closes and is replaced if another hostent is requested.
4997 static struct WS_hostent
* WS_get_local_ips( char *hostname
)
4999 int last_metric
, numroutes
= 0, i
, j
;
5001 PIP_ADAPTER_INFO adapters
= NULL
, k
;
5002 struct WS_hostent
*hostlist
= NULL
;
5003 PMIB_IPFORWARDTABLE routes
= NULL
;
5004 struct route
*route_addrs
= NULL
;
5005 DWORD adap_size
, route_size
;
5007 /* Obtain the size of the adapter list and routing table, also allocate memory */
5008 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
5010 if (GetIpForwardTable(NULL
, &route_size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
5012 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
5013 routes
= HeapAlloc(GetProcessHeap(), 0, route_size
);
5014 route_addrs
= HeapAlloc(GetProcessHeap(), 0, 0); /* HeapReAlloc doesn't work on NULL */
5015 if (adapters
== NULL
|| routes
== NULL
|| route_addrs
== NULL
)
5017 /* Obtain the adapter list and the full routing table */
5018 if (GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
5020 if (GetIpForwardTable(routes
, &route_size
, FALSE
) != NO_ERROR
)
5022 /* Store the interface associated with each route */
5023 for (n
= 0; n
< routes
->dwNumEntries
; n
++)
5026 DWORD ifmetric
, exists
= FALSE
;
5028 if (routes
->table
[n
].u1
.ForwardType
!= MIB_IPROUTE_TYPE_DIRECT
)
5030 ifindex
= routes
->table
[n
].dwForwardIfIndex
;
5031 ifmetric
= routes
->table
[n
].dwForwardMetric1
;
5032 /* Only store the lowest valued metric for an interface */
5033 for (j
= 0; j
< numroutes
; j
++)
5035 if (route_addrs
[j
].interface
== ifindex
)
5037 if (route_addrs
[j
].metric
> ifmetric
)
5038 route_addrs
[j
].metric
= ifmetric
;
5044 route_addrs
= HeapReAlloc(GetProcessHeap(), 0, route_addrs
, (numroutes
+1)*sizeof(struct route
));
5045 if (route_addrs
== NULL
)
5046 goto cleanup
; /* Memory allocation error, fail gracefully */
5047 route_addrs
[numroutes
].interface
= ifindex
;
5048 route_addrs
[numroutes
].metric
= ifmetric
;
5049 /* If no IP is found in the next step (for whatever reason)
5050 * then fall back to the magic loopback address.
5052 memcpy(&(route_addrs
[numroutes
].addr
.s_addr
), magic_loopback_addr
, 4);
5056 goto cleanup
; /* No routes, fall back to the Magic IP */
5057 /* Find the IP address associated with each found interface */
5058 for (i
= 0; i
< numroutes
; i
++)
5060 for (k
= adapters
; k
!= NULL
; k
= k
->Next
)
5062 char *ip
= k
->IpAddressList
.IpAddress
.String
;
5064 if (route_addrs
[i
].interface
== k
->Index
)
5065 route_addrs
[i
].addr
.s_addr
= (in_addr_t
) inet_addr(ip
);
5068 /* Allocate a hostent and enough memory for all the IPs,
5069 * including the NULL at the end of the list.
5071 hostlist
= WS_create_he(hostname
, 1, 0, numroutes
+1, sizeof(struct in_addr
));
5072 if (hostlist
== NULL
)
5073 goto cleanup
; /* Failed to allocate a hostent for the list of IPs */
5074 hostlist
->h_addr_list
[numroutes
] = NULL
; /* NULL-terminate the address list */
5075 hostlist
->h_aliases
[0] = NULL
; /* NULL-terminate the alias list */
5076 hostlist
->h_addrtype
= AF_INET
;
5077 hostlist
->h_length
= sizeof(struct in_addr
); /* = 4 */
5078 /* Reorder the entries when placing them in the host list, Windows expects
5079 * the IP list in order from highest priority to lowest (the critical thing
5080 * is that most applications expect the first IP to be the default route).
5083 for (i
= 0; i
< numroutes
; i
++)
5085 struct in_addr addr
;
5086 int metric
= 0xFFFF;
5088 memcpy(&addr
, magic_loopback_addr
, 4);
5089 for (j
= 0; j
< numroutes
; j
++)
5091 int this_metric
= route_addrs
[j
].metric
;
5093 if (this_metric
> last_metric
&& this_metric
< metric
)
5095 addr
= route_addrs
[j
].addr
;
5096 metric
= this_metric
;
5099 last_metric
= metric
;
5100 (*(struct in_addr
*) hostlist
->h_addr_list
[i
]) = addr
;
5103 /* Cleanup all allocated memory except the address list,
5104 * the address list is used by the calling app.
5107 HeapFree(GetProcessHeap(), 0, route_addrs
);
5108 HeapFree(GetProcessHeap(), 0, adapters
);
5109 HeapFree(GetProcessHeap(), 0, routes
);
5113 /***********************************************************************
5114 * gethostbyname (WS2_32.52)
5116 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
5118 struct WS_hostent
*retval
= NULL
;
5119 struct hostent
* host
;
5120 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
5123 struct hostent hostentry
;
5124 int locerr
= ENOBUFS
;
5128 SetLastError(WSANOTINITIALISED
);
5131 if( gethostname( hostname
, 100) == -1) {
5132 SetLastError( WSAENOBUFS
); /* appropriate ? */
5135 if( !name
|| !name
[0]) {
5138 /* If the hostname of the local machine is requested then return the
5139 * complete list of local IP addresses */
5140 if(strcmp(name
, hostname
) == 0)
5141 retval
= WS_get_local_ips(hostname
);
5142 /* If any other hostname was requested (or the routing table lookup failed)
5143 * then return the IP found by the host OS */
5146 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
5148 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
5150 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
5151 if( res
!= ERANGE
) break;
5153 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
5155 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
5157 EnterCriticalSection( &csWSgetXXXbyYYY
);
5158 host
= gethostbyname(name
);
5159 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
5161 if (host
) retval
= WS_dup_he(host
);
5162 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
5163 HeapFree(GetProcessHeap(),0,extrabuf
);
5165 LeaveCriticalSection( &csWSgetXXXbyYYY
);
5168 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
5169 strcmp(name
, "localhost") != 0)
5171 /* hostname != "localhost" but has loopback address. replace by our
5172 * special address.*/
5173 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
5175 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
5180 /***********************************************************************
5181 * getprotobyname (WS2_32.53)
5183 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
5185 struct WS_protoent
* retval
= NULL
;
5186 #ifdef HAVE_GETPROTOBYNAME
5187 struct protoent
* proto
;
5188 EnterCriticalSection( &csWSgetXXXbyYYY
);
5189 if( (proto
= getprotobyname(name
)) != NULL
)
5191 retval
= WS_dup_pe(proto
);
5194 MESSAGE("protocol %s not found; You might want to add "
5195 "this to /etc/protocols\n", debugstr_a(name
) );
5196 SetLastError(WSANO_DATA
);
5198 LeaveCriticalSection( &csWSgetXXXbyYYY
);
5200 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
5205 /***********************************************************************
5206 * getprotobynumber (WS2_32.54)
5208 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
5210 struct WS_protoent
* retval
= NULL
;
5211 #ifdef HAVE_GETPROTOBYNUMBER
5212 struct protoent
* proto
;
5213 EnterCriticalSection( &csWSgetXXXbyYYY
);
5214 if( (proto
= getprotobynumber(number
)) != NULL
)
5216 retval
= WS_dup_pe(proto
);
5219 MESSAGE("protocol number %d not found; You might want to add "
5220 "this to /etc/protocols\n", number
);
5221 SetLastError(WSANO_DATA
);
5223 LeaveCriticalSection( &csWSgetXXXbyYYY
);
5225 TRACE("%i ret %p\n", number
, retval
);
5230 /***********************************************************************
5231 * getservbyname (WS2_32.55)
5233 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
5235 struct WS_servent
* retval
= NULL
;
5236 struct servent
* serv
;
5238 char *proto_str
= NULL
;
5240 if (!(name_str
= strdup_lower(name
))) return NULL
;
5242 if (proto
&& *proto
)
5244 if (!(proto_str
= strdup_lower(proto
)))
5246 HeapFree( GetProcessHeap(), 0, name_str
);
5251 EnterCriticalSection( &csWSgetXXXbyYYY
);
5252 serv
= getservbyname(name_str
, proto_str
);
5255 retval
= WS_dup_se(serv
);
5257 else SetLastError(WSANO_DATA
);
5258 LeaveCriticalSection( &csWSgetXXXbyYYY
);
5259 HeapFree( GetProcessHeap(), 0, proto_str
);
5260 HeapFree( GetProcessHeap(), 0, name_str
);
5261 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
5265 /***********************************************************************
5266 * freeaddrinfo (WS2_32.@)
5268 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
5271 struct WS_addrinfo
*next
;
5273 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
5274 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
5275 next
= res
->ai_next
;
5276 HeapFree(GetProcessHeap(),0,res
);
5281 /* helper functions for getaddrinfo()/getnameinfo() */
5282 static int convert_aiflag_w2u(int winflags
) {
5286 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
5287 if (ws_aiflag_map
[i
][0] & winflags
) {
5288 unixflags
|= ws_aiflag_map
[i
][1];
5289 winflags
&= ~ws_aiflag_map
[i
][0];
5292 FIXME("Unhandled windows AI_xxx flags %x\n", winflags
);
5296 static int convert_niflag_w2u(int winflags
) {
5300 for (i
=0;i
<sizeof(ws_niflag_map
)/sizeof(ws_niflag_map
[0]);i
++)
5301 if (ws_niflag_map
[i
][0] & winflags
) {
5302 unixflags
|= ws_niflag_map
[i
][1];
5303 winflags
&= ~ws_niflag_map
[i
][0];
5306 FIXME("Unhandled windows NI_xxx flags %x\n", winflags
);
5310 static int convert_aiflag_u2w(int unixflags
) {
5314 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
5315 if (ws_aiflag_map
[i
][1] & unixflags
) {
5316 winflags
|= ws_aiflag_map
[i
][0];
5317 unixflags
&= ~ws_aiflag_map
[i
][1];
5319 if (unixflags
) /* will warn usually */
5320 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags
);
5324 static int convert_eai_u2w(int unixret
) {
5327 for (i
=0;ws_eai_map
[i
][0];i
++)
5328 if (ws_eai_map
[i
][1] == unixret
)
5329 return ws_eai_map
[i
][0];
5333 static char *get_hostname(void)
5338 GetComputerNameExA( ComputerNamePhysicalDnsHostname
, NULL
, &size
);
5339 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
5340 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, size
))) return NULL
;
5341 if (!GetComputerNameExA( ComputerNamePhysicalDnsHostname
, ret
, &size
))
5343 HeapFree( GetProcessHeap(), 0, ret
);
5349 /***********************************************************************
5350 * getaddrinfo (WS2_32.@)
5352 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
5354 #ifdef HAVE_GETADDRINFO
5355 struct addrinfo
*unixaires
= NULL
;
5357 struct addrinfo unixhints
, *punixhints
= NULL
;
5358 char *hostname
= NULL
;
5361 if (!nodename
&& !servname
) return WSAHOST_NOT_FOUND
;
5365 else if (!nodename
[0])
5367 node
= hostname
= get_hostname();
5368 if (!node
) return WSA_NOT_ENOUGH_MEMORY
;
5374 punixhints
= &unixhints
;
5376 memset(&unixhints
, 0, sizeof(unixhints
));
5377 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
5378 if (hints
->ai_family
== 0) /* wildcard, specific to getaddrinfo() */
5379 punixhints
->ai_family
= 0;
5381 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
5382 if (hints
->ai_socktype
== 0) /* wildcard, specific to getaddrinfo() */
5383 punixhints
->ai_socktype
= 0;
5385 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
5386 if (hints
->ai_protocol
== 0) /* wildcard, specific to getaddrinfo() */
5387 punixhints
->ai_protocol
= 0;
5389 punixhints
->ai_protocol
= convert_proto_w2u(hints
->ai_protocol
);
5392 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
5393 result
= getaddrinfo(node
, servname
, punixhints
, &unixaires
);
5395 TRACE("%s, %s %p -> %p %d\n", debugstr_a(nodename
), debugstr_a(servname
), hints
, res
, result
);
5396 HeapFree(GetProcessHeap(), 0, hostname
);
5399 struct addrinfo
*xuai
= unixaires
;
5400 struct WS_addrinfo
**xai
= res
;
5404 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
5410 *xai
= ai
;xai
= &ai
->ai_next
;
5411 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
5412 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
5413 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
5414 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
5415 if (xuai
->ai_canonname
) {
5416 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
5417 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
5418 if (!ai
->ai_canonname
)
5420 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
5422 len
= xuai
->ai_addrlen
;
5423 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
5426 ai
->ai_addrlen
= len
;
5428 int winlen
= ai
->ai_addrlen
;
5430 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
5431 ai
->ai_addrlen
= winlen
;
5435 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
5438 ai
->ai_addrlen
= len
;
5440 xuai
= xuai
->ai_next
;
5442 freeaddrinfo(unixaires
);
5444 result
= convert_eai_u2w(result
);
5450 if (*res
) WS_freeaddrinfo(*res
);
5451 if (unixaires
) freeaddrinfo(unixaires
);
5453 return WSA_NOT_ENOUGH_MEMORY
;
5455 FIXME("getaddrinfo() failed, not found during buildtime.\n");
5460 static struct WS_addrinfoW
*addrinfo_AtoW(const struct WS_addrinfo
*ai
)
5462 struct WS_addrinfoW
*ret
;
5464 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfoW
)))) return NULL
;
5465 ret
->ai_flags
= ai
->ai_flags
;
5466 ret
->ai_family
= ai
->ai_family
;
5467 ret
->ai_socktype
= ai
->ai_socktype
;
5468 ret
->ai_protocol
= ai
->ai_protocol
;
5469 ret
->ai_addrlen
= ai
->ai_addrlen
;
5470 ret
->ai_canonname
= NULL
;
5471 ret
->ai_addr
= NULL
;
5472 ret
->ai_next
= NULL
;
5473 if (ai
->ai_canonname
)
5475 int len
= MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0);
5476 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
5478 HeapFree(GetProcessHeap(), 0, ret
);
5481 MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
);
5485 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
5487 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
5488 HeapFree(GetProcessHeap(), 0, ret
);
5491 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
5496 static struct WS_addrinfoW
*addrinfo_list_AtoW(const struct WS_addrinfo
*info
)
5498 struct WS_addrinfoW
*ret
, *infoW
;
5500 if (!(ret
= infoW
= addrinfo_AtoW(info
))) return NULL
;
5501 while (info
->ai_next
)
5503 if (!(infoW
->ai_next
= addrinfo_AtoW(info
->ai_next
)))
5508 infoW
= infoW
->ai_next
;
5509 info
= info
->ai_next
;
5514 static struct WS_addrinfo
*addrinfo_WtoA(const struct WS_addrinfoW
*ai
)
5516 struct WS_addrinfo
*ret
;
5518 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfo
)))) return NULL
;
5519 ret
->ai_flags
= ai
->ai_flags
;
5520 ret
->ai_family
= ai
->ai_family
;
5521 ret
->ai_socktype
= ai
->ai_socktype
;
5522 ret
->ai_protocol
= ai
->ai_protocol
;
5523 ret
->ai_addrlen
= ai
->ai_addrlen
;
5524 ret
->ai_canonname
= NULL
;
5525 ret
->ai_addr
= NULL
;
5526 ret
->ai_next
= NULL
;
5527 if (ai
->ai_canonname
)
5529 int len
= WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0, NULL
, NULL
);
5530 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
5532 HeapFree(GetProcessHeap(), 0, ret
);
5535 WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
, NULL
, NULL
);
5539 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
5541 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
5542 HeapFree(GetProcessHeap(), 0, ret
);
5545 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
5550 /***********************************************************************
5551 * GetAddrInfoW (WS2_32.@)
5553 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
5556 char *nodenameA
= NULL
, *servnameA
= NULL
;
5557 struct WS_addrinfo
*resA
, *hintsA
= NULL
;
5561 len
= WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, NULL
, 0, NULL
, NULL
);
5562 if (!(nodenameA
= HeapAlloc(GetProcessHeap(), 0, len
))) return EAI_MEMORY
;
5563 WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, nodenameA
, len
, NULL
, NULL
);
5567 len
= WideCharToMultiByte(CP_ACP
, 0, servname
, -1, NULL
, 0, NULL
, NULL
);
5568 if (!(servnameA
= HeapAlloc(GetProcessHeap(), 0, len
)))
5570 HeapFree(GetProcessHeap(), 0, nodenameA
);
5573 WideCharToMultiByte(CP_ACP
, 0, servname
, -1, servnameA
, len
, NULL
, NULL
);
5576 if (hints
) hintsA
= addrinfo_WtoA(hints
);
5577 ret
= WS_getaddrinfo(nodenameA
, servnameA
, hintsA
, &resA
);
5578 WS_freeaddrinfo(hintsA
);
5582 *res
= addrinfo_list_AtoW(resA
);
5583 WS_freeaddrinfo(resA
);
5586 HeapFree(GetProcessHeap(), 0, nodenameA
);
5587 HeapFree(GetProcessHeap(), 0, servnameA
);
5591 /***********************************************************************
5592 * FreeAddrInfoW (WS2_32.@)
5594 void WINAPI
FreeAddrInfoW(PADDRINFOW ai
)
5599 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
5600 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
5602 HeapFree(GetProcessHeap(), 0, ai
);
5607 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
5608 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
5610 #ifdef HAVE_GETNAMEINFO
5612 union generic_unix_sockaddr sa_u
;
5615 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
5616 serv
, servlen
, flags
);
5618 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
5621 WSASetLastError(WSAEFAULT
);
5622 return WSA_NOT_ENOUGH_MEMORY
;
5624 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
5625 return convert_eai_u2w(ret
);
5627 FIXME("getnameinfo() failed, not found during buildtime.\n");
5632 int WINAPI
GetNameInfoW(const SOCKADDR
*sa
, WS_socklen_t salen
, PWCHAR host
,
5633 DWORD hostlen
, PWCHAR serv
, DWORD servlen
, INT flags
)
5636 char *hostA
= NULL
, *servA
= NULL
;
5638 if (host
&& (!(hostA
= HeapAlloc(GetProcessHeap(), 0, hostlen
)))) return EAI_MEMORY
;
5639 if (serv
&& (!(servA
= HeapAlloc(GetProcessHeap(), 0, servlen
))))
5641 HeapFree(GetProcessHeap(), 0, hostA
);
5645 ret
= WS_getnameinfo(sa
, salen
, hostA
, hostlen
, servA
, servlen
, flags
);
5648 if (host
) MultiByteToWideChar(CP_ACP
, 0, hostA
, -1, host
, hostlen
);
5649 if (serv
) MultiByteToWideChar(CP_ACP
, 0, servA
, -1, serv
, servlen
);
5652 HeapFree(GetProcessHeap(), 0, hostA
);
5653 HeapFree(GetProcessHeap(), 0, servA
);
5657 /***********************************************************************
5658 * getservbyport (WS2_32.56)
5660 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
5662 struct WS_servent
* retval
= NULL
;
5663 #ifdef HAVE_GETSERVBYPORT
5664 struct servent
* serv
;
5665 char *proto_str
= NULL
;
5667 if (proto
&& *proto
)
5669 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
5671 EnterCriticalSection( &csWSgetXXXbyYYY
);
5672 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
5673 retval
= WS_dup_se(serv
);
5675 else SetLastError(WSANO_DATA
);
5676 LeaveCriticalSection( &csWSgetXXXbyYYY
);
5677 HeapFree( GetProcessHeap(), 0, proto_str
);
5679 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
5684 /***********************************************************************
5685 * gethostname (WS2_32.57)
5687 int WINAPI
WS_gethostname(char *name
, int namelen
)
5689 TRACE("name %p, len %d\n", name
, namelen
);
5691 if (gethostname(name
, namelen
) == 0)
5693 TRACE("<- '%s'\n", name
);
5696 SetLastError((errno
== EINVAL
) ? WSAEFAULT
: wsaErrno());
5697 TRACE("<- ERROR !\n");
5698 return SOCKET_ERROR
;
5702 /* ------------------------------------- Windows sockets extensions -- *
5704 * ------------------------------------------------------------------- */
5706 /***********************************************************************
5707 * WSAEnumNetworkEvents (WS2_32.36)
5709 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
5713 int errors
[FD_MAX_EVENTS
];
5715 TRACE("%08lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
5717 SERVER_START_REQ( get_socket_event
)
5719 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5720 req
->service
= TRUE
;
5721 req
->c_event
= wine_server_obj_handle( hEvent
);
5722 wine_server_set_reply( req
, errors
, sizeof(errors
) );
5723 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
5728 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
5729 lpEvent
->iErrorCode
[i
] = NtStatusToWSAError(errors
[i
]);
5732 SetLastError(WSAEINVAL
);
5733 return SOCKET_ERROR
;
5736 /***********************************************************************
5737 * WSAEventSelect (WS2_32.39)
5739 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
5743 TRACE("%08lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
5745 SERVER_START_REQ( set_socket_event
)
5747 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5749 req
->event
= wine_server_obj_handle( hEvent
);
5752 ret
= wine_server_call( req
);
5756 SetLastError(WSAEINVAL
);
5757 return SOCKET_ERROR
;
5760 /**********************************************************************
5761 * WSAGetOverlappedResult (WS2_32.40)
5763 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
5764 LPDWORD lpcbTransfer
, BOOL fWait
,
5769 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
5770 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
5772 if ( lpOverlapped
== NULL
)
5774 ERR( "Invalid pointer\n" );
5775 WSASetLastError(WSA_INVALID_PARAMETER
);
5779 status
= lpOverlapped
->Internal
;
5780 if (status
== STATUS_PENDING
)
5784 SetLastError( WSA_IO_INCOMPLETE
);
5788 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
5789 INFINITE
) == WAIT_FAILED
)
5791 status
= lpOverlapped
->Internal
;
5795 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
5798 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
5800 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
5805 /***********************************************************************
5806 * WSAAsyncSelect (WS2_32.101)
5808 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
5812 TRACE("%lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
5814 SERVER_START_REQ( set_socket_event
)
5816 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5819 req
->window
= wine_server_user_handle( hWnd
);
5821 ret
= wine_server_call( req
);
5825 SetLastError(WSAEINVAL
);
5826 return SOCKET_ERROR
;
5829 /***********************************************************************
5830 * WSACreateEvent (WS2_32.31)
5833 WSAEVENT WINAPI
WSACreateEvent(void)
5835 /* Create a manual-reset event, with initial state: unsignaled */
5838 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
5841 /***********************************************************************
5842 * WSACloseEvent (WS2_32.29)
5845 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
5847 TRACE ("event=%p\n", event
);
5849 return CloseHandle(event
);
5852 /***********************************************************************
5853 * WSASocketA (WS2_32.78)
5856 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
5857 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
5858 GROUP g
, DWORD dwFlags
)
5861 WSAPROTOCOL_INFOW info
;
5863 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
5864 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
5866 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
5868 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
5869 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
5870 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
5874 WSASetLastError( WSAEINVAL
);
5875 return SOCKET_ERROR
;
5878 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
5881 /***********************************************************************
5882 * WSASocketW (WS2_32.79)
5885 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
5886 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
5887 GROUP g
, DWORD dwFlags
)
5891 int unixaf
, unixtype
;
5894 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
5895 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
5898 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
5899 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
5903 err
= WSANOTINITIALISED
;
5907 /* hack for WSADuplicateSocket */
5908 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
5909 ret
= lpProtocolInfo
->dwServiceFlags3
;
5910 TRACE("\tgot duplicate %04lx\n", ret
);
5916 if (af
== FROM_PROTOCOL_INFO
|| !af
)
5917 af
= lpProtocolInfo
->iAddressFamily
;
5918 if (type
== FROM_PROTOCOL_INFO
|| !type
)
5919 type
= lpProtocolInfo
->iSocketType
;
5920 if (protocol
== FROM_PROTOCOL_INFO
|| !protocol
)
5921 protocol
= lpProtocolInfo
->iProtocol
;
5924 if (!type
&& (af
|| protocol
))
5926 WSAPROTOCOL_INFOW infow
;
5928 /* default to the first valid protocol */
5930 protocol
= valid_protocols
[0];
5932 if (WS_EnterSingleProtocolW(protocol
, &infow
))
5934 type
= infow
.iSocketType
;
5936 /* after win2003 it's no longer possible to pass AF_UNSPEC
5937 using the protocol info struct */
5938 if (!lpProtocolInfo
&& af
== WS_AF_UNSPEC
)
5939 af
= infow
.iAddressFamily
;
5943 /* convert the socket family, type and protocol */
5944 unixaf
= convert_af_w2u(af
);
5945 unixtype
= convert_socktype_w2u(type
);
5946 protocol
= convert_proto_w2u(protocol
);
5947 if (unixaf
== AF_UNSPEC
) unixaf
= -1;
5949 /* filter invalid parameters */
5952 /* the type could not be converted */
5953 if (type
&& unixtype
< 0)
5955 err
= WSAESOCKTNOSUPPORT
;
5959 err
= WSAEPROTONOSUPPORT
;
5964 /* both family and protocol can't be invalid */
5971 /* family could not be converted and neither socket type */
5972 if (unixtype
< 0 && af
>= 0)
5975 err
= WSAESOCKTNOSUPPORT
;
5979 err
= WSAEAFNOSUPPORT
;
5983 SERVER_START_REQ( create_socket
)
5985 req
->family
= unixaf
;
5986 req
->type
= unixtype
;
5987 req
->protocol
= protocol
;
5988 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
5989 req
->attributes
= OBJ_INHERIT
;
5990 req
->flags
= dwFlags
;
5991 set_error( wine_server_call( req
) );
5992 ret
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
5997 TRACE("\tcreated %04lx\n", ret
);
6001 err
= GetLastError();
6002 if (err
== WSAEACCES
) /* raw socket denied */
6004 if (type
== SOCK_RAW
)
6005 ERR_(winediag
)("Failed to create a socket of type SOCK_RAW, this requires special permissions.\n");
6007 ERR_(winediag
)("Failed to create socket, this requires special permissions.\n");
6011 /* invalid combination of valid parameters, like SOCK_STREAM + IPPROTO_UDP */
6012 if (err
== WSAEINVAL
)
6013 err
= WSAESOCKTNOSUPPORT
;
6014 else if (err
== WSAEOPNOTSUPP
)
6015 err
= WSAEPROTONOSUPPORT
;
6019 WARN("\t\tfailed, error %d!\n", err
);
6021 return INVALID_SOCKET
;
6024 /***********************************************************************
6025 * WSAJoinLeaf (WS2_32.58)
6028 SOCKET WINAPI
WSAJoinLeaf(
6030 const struct WS_sockaddr
*addr
,
6032 LPWSABUF lpCallerData
,
6033 LPWSABUF lpCalleeData
,
6039 return INVALID_SOCKET
;
6042 /***********************************************************************
6043 * __WSAFDIsSet (WS2_32.151)
6045 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
6047 int i
= set
->fd_count
;
6049 TRACE("(%ld,%p(%i))\n", s
, set
, i
);
6052 if (set
->fd_array
[i
] == s
) return 1;
6056 /***********************************************************************
6057 * WSAIsBlocking (WS2_32.114)
6059 BOOL WINAPI
WSAIsBlocking(void)
6061 /* By default WinSock should set all its sockets to non-blocking mode
6062 * and poll in PeekMessage loop when processing "blocking" ones. This
6063 * function is supposed to tell if the program is in this loop. Our
6064 * blocking calls are truly blocking so we always return FALSE.
6066 * Note: It is allowed to call this function without prior WSAStartup().
6073 /***********************************************************************
6074 * WSACancelBlockingCall (WS2_32.113)
6076 INT WINAPI
WSACancelBlockingCall(void)
6082 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
6084 FIXME("How was this called?\n");
6089 /***********************************************************************
6090 * WSASetBlockingHook (WS2_32.109)
6092 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
6094 FARPROC prev
= blocking_hook
;
6095 blocking_hook
= lpBlockFunc
;
6096 TRACE("hook %p\n", lpBlockFunc
);
6101 /***********************************************************************
6102 * WSAUnhookBlockingHook (WS2_32.110)
6104 INT WINAPI
WSAUnhookBlockingHook(void)
6106 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
6111 /* ----------------------------------- end of API stuff */
6113 /* ----------------------------------- helper functions -
6115 * TODO: Merge WS_dup_..() stuff into one function that
6116 * would operate with a generic structure containing internal
6117 * pointers (via a template of some kind).
6120 static int list_size(char** l
, int item_size
)
6125 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
6126 j
+= (i
+ 1) * sizeof(char*); }
6130 static int list_dup(char** l_src
, char** l_to
, int item_size
)
6135 for (i
= 0; l_src
[i
]; i
++) ;
6136 p
= (char *)(l_to
+ i
+ 1);
6137 for (i
= 0; l_src
[i
]; i
++)
6139 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
6140 memcpy(p
, l_src
[i
], count
);
6145 return p
- (char *)l_to
;
6150 /* create a hostent entry
6152 * Creates the entry with enough memory for the name, aliases
6153 * addresses, and the address pointers. Also copies the name
6154 * and sets up all the pointers.
6156 * NOTE: The alias and address lists must be allocated with room
6157 * for the NULL item terminating the list. This is true even if
6158 * the list has no items ("aliases" and "addresses" must be
6159 * at least "1", a truly empty list is invalid).
6161 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int aliases_size
, int addresses
, int address_length
)
6163 struct WS_hostent
*p_to
;
6165 int size
= (sizeof(struct WS_hostent
) +
6167 sizeof(char *) * aliases
+
6169 sizeof(char *) * addresses
+
6170 address_length
* (addresses
- 1)), i
;
6172 if (!(p_to
= check_buffer_he(size
))) return NULL
;
6173 memset(p_to
, 0, size
);
6175 /* Use the memory in the same way winsock does.
6176 * First set the pointer for aliases, second set the pointers for addresses.
6177 * Third fill the addresses indexes, fourth jump aliases names size.
6178 * Fifth fill the hostname.
6179 * NOTE: This method is valid for OS version's >= XP.
6181 p
= (char *)(p_to
+ 1);
6182 p_to
->h_aliases
= (char **)p
;
6183 p
+= sizeof(char *)*aliases
;
6185 p_to
->h_addr_list
= (char **)p
;
6186 p
+= sizeof(char *)*addresses
;
6188 for (i
= 0, addresses
--; i
< addresses
; i
++, p
+= address_length
)
6189 p_to
->h_addr_list
[i
] = p
;
6191 /* NOTE: h_aliases must be filled in manually because we don't know each string
6192 * size, leave these pointers NULL (already set to NULL by memset earlier).
6202 /* duplicate hostent entry
6203 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
6204 * Ditto for protoent and servent.
6206 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
6208 int i
, addresses
= 0, alias_size
= 0;
6209 struct WS_hostent
*p_to
;
6212 for( i
= 0; p_he
->h_aliases
[i
]; i
++) alias_size
+= strlen(p_he
->h_aliases
[i
]) + 1;
6213 while (p_he
->h_addr_list
[addresses
]) addresses
++;
6215 p_to
= WS_create_he(p_he
->h_name
, i
+ 1, alias_size
, addresses
+ 1, p_he
->h_length
);
6217 if (!p_to
) return NULL
;
6218 p_to
->h_addrtype
= p_he
->h_addrtype
;
6219 p_to
->h_length
= p_he
->h_length
;
6221 for(i
= 0, p
= p_to
->h_addr_list
[0]; p_he
->h_addr_list
[i
]; i
++, p
+= p_to
->h_length
)
6222 memcpy(p
, p_he
->h_addr_list
[i
], p_to
->h_length
);
6224 /* Fill the aliases after the IP data */
6225 for(i
= 0; p_he
->h_aliases
[i
]; i
++)
6227 p_to
->h_aliases
[i
] = p
;
6228 strcpy(p
, p_he
->h_aliases
[i
]);
6235 /* ----- protoent */
6237 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
)
6240 struct WS_protoent
*p_to
;
6242 int size
= (sizeof(*p_pe
) +
6243 strlen(p_pe
->p_name
) + 1 +
6244 list_size(p_pe
->p_aliases
, 0));
6246 if (!(p_to
= check_buffer_pe(size
))) return NULL
;
6247 p_to
->p_proto
= p_pe
->p_proto
;
6249 p
= (char *)(p_to
+ 1);
6251 strcpy(p
, p_pe
->p_name
);
6254 p_to
->p_aliases
= (char **)p
;
6255 list_dup(p_pe
->p_aliases
, p_to
->p_aliases
, 0);
6261 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
6264 struct WS_servent
*p_to
;
6266 int size
= (sizeof(*p_se
) +
6267 strlen(p_se
->s_proto
) + 1 +
6268 strlen(p_se
->s_name
) + 1 +
6269 list_size(p_se
->s_aliases
, 0));
6271 if (!(p_to
= check_buffer_se(size
))) return NULL
;
6272 p_to
->s_port
= p_se
->s_port
;
6274 p
= (char *)(p_to
+ 1);
6276 strcpy(p
, p_se
->s_name
);
6280 strcpy(p
, p_se
->s_proto
);
6283 p_to
->s_aliases
= (char **)p
;
6284 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
6289 /***********************************************************************
6290 * WSARecv (WS2_32.67)
6292 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
6293 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
6294 LPWSAOVERLAPPED lpOverlapped
,
6295 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
6297 return WS2_recv_base(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
6298 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
, NULL
);
6301 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
6302 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
6303 struct WS_sockaddr
*lpFrom
,
6304 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
6305 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
6306 LPWSABUF lpControlBuffer
)
6308 unsigned int i
, options
;
6310 struct ws2_async
*wsa
;
6312 DWORD timeout_start
= GetTickCount();
6313 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
6315 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
6316 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
6317 (lpFromlen
? *lpFromlen
: -1),
6318 lpOverlapped
, lpCompletionRoutine
);
6320 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
6321 TRACE( "fd=%d, options=%x\n", fd
, options
);
6323 if (fd
== -1) return SOCKET_ERROR
;
6325 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
6331 wsa
->hSocket
= SOCKET2HANDLE(s
);
6332 wsa
->flags
= *lpFlags
;
6333 wsa
->lpFlags
= lpFlags
;
6335 wsa
->addrlen
.ptr
= lpFromlen
;
6336 wsa
->control
= lpControlBuffer
;
6337 wsa
->n_iovecs
= dwBufferCount
;
6338 wsa
->first_iovec
= 0;
6339 for (i
= 0; i
< dwBufferCount
; i
++)
6341 /* check buffer first to trigger write watches */
6342 if (IsBadWritePtr( lpBuffers
[i
].buf
, lpBuffers
[i
].len
))
6347 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
6348 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
6353 n
= WS2_recv( fd
, wsa
);
6356 if (errno
== EINTR
) continue;
6357 if (errno
!= EAGAIN
)
6359 int loc_errno
= errno
;
6361 if (cvalue
) WS_AddCompletion( s
, cvalue
, sock_get_ntstatus(loc_errno
), 0 );
6365 else if (lpNumberOfBytesRecvd
) *lpNumberOfBytesRecvd
= n
;
6367 if ((lpOverlapped
|| lpCompletionRoutine
) &&
6368 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
6370 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
6372 wsa
->user_overlapped
= lpOverlapped
;
6373 wsa
->completion_func
= lpCompletionRoutine
;
6374 release_sock_fd( s
, fd
);
6378 iosb
->u
.Status
= STATUS_PENDING
;
6379 iosb
->Information
= 0;
6381 SERVER_START_REQ( register_async
)
6383 req
->type
= ASYNC_TYPE_READ
;
6384 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
6385 req
->async
.callback
= wine_server_client_ptr( WS2_async_recv
);
6386 req
->async
.iosb
= wine_server_client_ptr( iosb
);
6387 req
->async
.arg
= wine_server_client_ptr( wsa
);
6388 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
6389 req
->async
.cvalue
= cvalue
;
6390 err
= wine_server_call( req
);
6394 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
6395 WSASetLastError( NtStatusToWSAError( err
));
6396 return SOCKET_ERROR
;
6399 iosb
->u
.Status
= STATUS_SUCCESS
;
6400 iosb
->Information
= n
;
6401 if (!wsa
->completion_func
)
6403 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
6404 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
6405 HeapFree( GetProcessHeap(), 0, wsa
);
6407 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
6408 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
6409 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
6415 if ((err
= _is_blocking( s
, &is_blocking
)))
6417 err
= NtStatusToWSAError( err
);
6424 int timeout
= GET_RCVTIMEO(fd
);
6427 timeout
-= GetTickCount() - timeout_start
;
6428 if (timeout
< 0) timeout
= 0;
6432 pfd
.events
= POLLIN
;
6433 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
6435 if (!timeout
|| !poll( &pfd
, 1, timeout
))
6438 /* a timeout is not fatal */
6439 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
6445 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
6446 err
= WSAEWOULDBLOCK
;
6451 TRACE(" -> %i bytes\n", n
);
6452 HeapFree( GetProcessHeap(), 0, wsa
);
6453 release_sock_fd( s
, fd
);
6454 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
6459 HeapFree( GetProcessHeap(), 0, wsa
);
6460 release_sock_fd( s
, fd
);
6461 WARN(" -> ERROR %d\n", err
);
6462 WSASetLastError( err
);
6463 return SOCKET_ERROR
;
6466 /***********************************************************************
6467 * WSARecvFrom (WS2_32.69)
6469 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
6470 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
6471 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
6472 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
6475 return WS2_recv_base( s
, lpBuffers
, dwBufferCount
,
6476 lpNumberOfBytesRecvd
, lpFlags
,
6478 lpOverlapped
, lpCompletionRoutine
, NULL
);
6481 /***********************************************************************
6482 * WSCInstallProvider (WS2_32.88)
6484 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
6485 LPCWSTR lpszProviderDllPath
,
6486 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
6487 DWORD dwNumberOfEntries
,
6490 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
6491 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
6492 dwNumberOfEntries
, lpErrno
);
6498 /***********************************************************************
6499 * WSCDeinstallProvider (WS2_32.83)
6501 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
6503 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
6509 /***********************************************************************
6510 * WSAAccept (WS2_32.26)
6512 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
6513 LPCONDITIONPROC lpfnCondition
, DWORD_PTR dwCallbackData
)
6516 int ret
= 0, size
= 0;
6517 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
6518 /* QOS SQOS, GQOS; */
6521 SOCKADDR src_addr
, dst_addr
;
6523 TRACE("Socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %ld\n",
6524 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
6527 size
= sizeof(src_addr
);
6528 cs
= WS_accept(s
, &src_addr
, &size
);
6530 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
6532 if (!lpfnCondition
) return cs
;
6534 CallerId
.buf
= (char *)&src_addr
;
6535 CallerId
.len
= sizeof(src_addr
);
6537 CallerData
.buf
= NULL
;
6540 WS_getsockname(cs
, &dst_addr
, &size
);
6542 CalleeId
.buf
= (char *)&dst_addr
;
6543 CalleeId
.len
= sizeof(dst_addr
);
6546 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
6547 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
6552 if (addr
&& addrlen
)
6553 memcpy(addr
, &src_addr
, (*addrlen
> size
) ? size
: *addrlen
);
6556 SERVER_START_REQ( set_socket_deferred
)
6558 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
6559 req
->deferred
= wine_server_obj_handle( SOCKET2HANDLE(cs
) );
6560 if ( !wine_server_call_err ( req
) )
6562 SetLastError( WSATRY_AGAIN
);
6563 WS_closesocket( cs
);
6567 return SOCKET_ERROR
;
6570 SetLastError(WSAECONNREFUSED
);
6571 return SOCKET_ERROR
;
6573 FIXME("Unknown return type from Condition function\n");
6574 SetLastError(WSAENOTSOCK
);
6575 return SOCKET_ERROR
;
6579 /***********************************************************************
6580 * WSADuplicateSocketA (WS2_32.32)
6582 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
6584 return WS_DuplicateSocket(FALSE
, s
, dwProcessId
, (LPWSAPROTOCOL_INFOW
) lpProtocolInfo
);
6587 /***********************************************************************
6588 * WSADuplicateSocketW (WS2_32.33)
6590 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
6592 return WS_DuplicateSocket(TRUE
, s
, dwProcessId
, lpProtocolInfo
);
6595 /***********************************************************************
6596 * WSAInstallServiceClassA (WS2_32.48)
6598 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
6600 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
6601 WSASetLastError(WSAEACCES
);
6602 return SOCKET_ERROR
;
6605 /***********************************************************************
6606 * WSAInstallServiceClassW (WS2_32.49)
6608 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
6610 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
6611 WSASetLastError(WSAEACCES
);
6612 return SOCKET_ERROR
;
6615 /***********************************************************************
6616 * WSARemoveServiceClass (WS2_32.70)
6618 int WINAPI
WSARemoveServiceClass(LPGUID info
)
6620 FIXME("Request to remove service %p\n",info
);
6621 WSASetLastError(WSATYPE_NOT_FOUND
);
6622 return SOCKET_ERROR
;
6625 /***********************************************************************
6626 * inet_ntop (WS2_32.@)
6628 PCSTR WINAPI
WS_inet_ntop( INT family
, PVOID addr
, PSTR buffer
, SIZE_T len
)
6630 #ifdef HAVE_INET_NTOP
6631 struct WS_in6_addr
*in6
;
6632 struct WS_in_addr
*in
;
6635 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
6638 WSASetLastError( STATUS_INVALID_PARAMETER
);
6647 pdst
= inet_ntop( AF_INET
, &in
->WS_s_addr
, buffer
, len
);
6653 pdst
= inet_ntop( AF_INET6
, in6
->WS_s6_addr
, buffer
, len
);
6657 WSASetLastError( WSAEAFNOSUPPORT
);
6661 if (!pdst
) WSASetLastError( STATUS_INVALID_PARAMETER
);
6664 FIXME( "not supported on this platform\n" );
6665 WSASetLastError( WSAEAFNOSUPPORT
);
6670 /***********************************************************************
6671 * WSAStringToAddressA (WS2_32.80)
6673 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
6675 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
6676 LPSOCKADDR lpAddress
,
6677 LPINT lpAddressLength
)
6680 LPSTR workBuffer
=NULL
,ptrPort
;
6682 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_a(AddressString
), AddressFamily
,
6683 lpProtocolInfo
, lpAddress
, lpAddressLength
);
6685 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
6689 WSASetLastError(WSAEINVAL
);
6690 return SOCKET_ERROR
;
6694 FIXME("ProtocolInfo not implemented.\n");
6696 workBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
6697 strlen(AddressString
) + 1);
6700 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6701 return SOCKET_ERROR
;
6704 strcpy(workBuffer
, AddressString
);
6706 switch(AddressFamily
)
6710 struct in_addr inetaddr
;
6712 /* If lpAddressLength is too small, tell caller the size we need */
6713 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
6715 *lpAddressLength
= sizeof(SOCKADDR_IN
);
6719 *lpAddressLength
= sizeof(SOCKADDR_IN
);
6720 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
6722 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= AF_INET
;
6724 ptrPort
= strchr(workBuffer
, ':');
6727 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= htons(atoi(ptrPort
+1));
6732 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= 0;
6735 if(inet_aton(workBuffer
, &inetaddr
) > 0)
6737 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
6748 struct in6_addr inetaddr
;
6749 /* If lpAddressLength is too small, tell caller the size we need */
6750 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
6752 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
6756 #ifdef HAVE_INET_PTON
6757 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
6758 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
6760 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_family
= WS_AF_INET6
;
6762 /* This one is a bit tricky. An IPv6 address contains colons, so the
6763 * check from IPv4 doesn't work like that. However, IPv6 addresses that
6764 * contain a port are written with braces like [fd12:3456:7890::1]:12345
6765 * so what we will do is to look for ']', check if the next char is a
6766 * colon, and if it is, parse the port as in IPv4. */
6768 ptrPort
= strchr(workBuffer
, ']');
6769 if(ptrPort
&& *(++ptrPort
) == ':')
6771 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= htons(atoi(ptrPort
+1));
6776 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= 0;
6779 if(inet_pton(AF_INET6
, workBuffer
, &inetaddr
) > 0)
6781 memcpy(&((LPSOCKADDR_IN6
)lpAddress
)->sin6_addr
, &inetaddr
,
6782 sizeof(struct in6_addr
));
6786 #endif /* HAVE_INET_PTON */
6792 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
6793 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
6797 HeapFree(GetProcessHeap(), 0, workBuffer
);
6800 WSASetLastError(res
);
6801 return SOCKET_ERROR
;
6804 /***********************************************************************
6805 * WSAStringToAddressW (WS2_32.81)
6807 * FIXME: Does anybody know if this function allows using Hebrew/Arabic/Chinese... digits?
6808 * If this should be the case, it would be required to map these digits
6809 * to Unicode digits (0-9) using FoldString first.
6811 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
6813 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
6814 LPSOCKADDR lpAddress
,
6815 LPINT lpAddressLength
)
6818 LPSTR workBuffer
=NULL
;
6819 WSAPROTOCOL_INFOA infoA
;
6820 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
6822 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
6823 lpAddress
, lpAddressLength
);
6825 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
6827 /* if ProtocolInfo is available - convert to ANSI variant */
6830 lpProtoInfoA
= &infoA
;
6831 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
6833 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
6834 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
6836 WSASetLastError( WSAEINVAL
);
6837 return SOCKET_ERROR
;
6843 /* Translate AddressString to ANSI code page - assumes that only
6844 standard digits 0-9 are used with this API call */
6845 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
6846 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
6850 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
6851 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
6852 lpAddress
,lpAddressLength
);
6853 HeapFree( GetProcessHeap(), 0, workBuffer
);
6857 res
= WSA_NOT_ENOUGH_MEMORY
;
6862 WSASetLastError(res
);
6863 return SOCKET_ERROR
;
6866 /***********************************************************************
6867 * WSAAddressToStringA (WS2_32.27)
6869 * See WSAAddressToStringW
6871 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
6872 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
6876 CHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
6879 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
6881 if (!sockaddr
) return SOCKET_ERROR
;
6882 if (!string
|| !lenstr
) return SOCKET_ERROR
;
6884 switch(sockaddr
->sa_family
)
6887 if (len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
6888 sprintf( buffer
, "%u.%u.%u.%u:%u",
6889 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
6890 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
6891 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
6892 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
6893 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
6895 p
= strchr( buffer
, ':' );
6896 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
6901 struct WS_sockaddr_in6
*sockaddr6
= (LPSOCKADDR_IN6
) sockaddr
;
6904 if (len
< sizeof(SOCKADDR_IN6
)) return SOCKET_ERROR
;
6905 if ((sockaddr6
->sin6_port
))
6906 strcpy(buffer
, "[");
6907 if (!WS_inet_ntop(WS_AF_INET6
, &sockaddr6
->sin6_addr
, buffer
+strlen(buffer
), sizeof(buffer
)))
6909 WSASetLastError(WSAEINVAL
);
6910 return SOCKET_ERROR
;
6912 if ((sockaddr6
->sin6_scope_id
))
6913 sprintf(buffer
+strlen(buffer
), "%%%u", sockaddr6
->sin6_scope_id
);
6914 if ((sockaddr6
->sin6_port
))
6915 sprintf(buffer
+strlen(buffer
), "]:%u", ntohs(sockaddr6
->sin6_port
));
6920 WSASetLastError(WSAEINVAL
);
6921 return SOCKET_ERROR
;
6924 size
= strlen( buffer
) + 1;
6929 WSASetLastError(WSAEFAULT
);
6930 return SOCKET_ERROR
;
6934 strcpy( string
, buffer
);
6938 /***********************************************************************
6939 * WSAAddressToStringW (WS2_32.28)
6941 * Convert a sockaddr address into a readable address string.
6944 * sockaddr [I] Pointer to a sockaddr structure.
6945 * len [I] Size of the sockaddr structure.
6946 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
6947 * string [I/O] Pointer to a buffer to receive the address string.
6948 * lenstr [I/O] Size of the receive buffer in WCHARs.
6952 * Failure: SOCKET_ERROR
6955 * The 'info' parameter is ignored.
6957 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
6958 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
6963 WCHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
6966 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
6969 ret
= WSAAddressToStringA(sockaddr
, len
, NULL
, bufAddr
, &size
);
6971 if (ret
) return ret
;
6973 MultiByteToWideChar( CP_ACP
, 0, bufAddr
, size
, buffer
, sizeof( buffer
)/sizeof(WCHAR
));
6978 WSASetLastError(WSAEFAULT
);
6979 return SOCKET_ERROR
;
6983 lstrcpyW( string
, buffer
);
6987 /***********************************************************************
6988 * WSAEnumNameSpaceProvidersA (WS2_32.34)
6990 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
6992 FIXME( "(%p %p) Stub!\n", len
, buffer
);
6996 /***********************************************************************
6997 * WSAEnumNameSpaceProvidersW (WS2_32.35)
6999 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
7001 FIXME( "(%p %p) Stub!\n", len
, buffer
);
7005 /***********************************************************************
7006 * WSAGetQOSByName (WS2_32.41)
7008 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
7010 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
7014 /***********************************************************************
7015 * WSAGetServiceClassInfoA (WS2_32.42)
7017 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
7018 LPWSASERVICECLASSINFOA info
)
7020 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
7022 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
7023 return SOCKET_ERROR
;
7026 /***********************************************************************
7027 * WSAGetServiceClassInfoW (WS2_32.43)
7029 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
7030 LPWSASERVICECLASSINFOW info
)
7032 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
7034 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
7035 return SOCKET_ERROR
;
7038 /***********************************************************************
7039 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
7041 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
7043 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
7044 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
7045 return SOCKET_ERROR
;
7048 /***********************************************************************
7049 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
7051 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
7053 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
7054 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
7055 return SOCKET_ERROR
;
7058 /***********************************************************************
7059 * WSALookupServiceBeginA (WS2_32.59)
7061 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
7062 DWORD dwControlFlags
,
7065 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
7067 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
7068 return SOCKET_ERROR
;
7071 /***********************************************************************
7072 * WSALookupServiceBeginW (WS2_32.60)
7074 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
7075 DWORD dwControlFlags
,
7078 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
7080 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
7081 return SOCKET_ERROR
;
7084 /***********************************************************************
7085 * WSALookupServiceEnd (WS2_32.61)
7087 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
7089 FIXME("(%p) Stub!\n", lookup
);
7093 /***********************************************************************
7094 * WSALookupServiceNextA (WS2_32.62)
7096 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
7098 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
7099 WSASetLastError(WSA_E_NO_MORE
);
7100 return SOCKET_ERROR
;
7103 /***********************************************************************
7104 * WSALookupServiceNextW (WS2_32.63)
7106 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
7108 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
7109 WSASetLastError(WSA_E_NO_MORE
);
7110 return SOCKET_ERROR
;
7113 /***********************************************************************
7114 * WSANtohl (WS2_32.64)
7116 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
7118 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
7120 if (!lphostlong
) return WSAEFAULT
;
7122 *lphostlong
= ntohl( netlong
);
7126 /***********************************************************************
7127 * WSANtohs (WS2_32.65)
7129 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
7131 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
7133 if (!lphostshort
) return WSAEFAULT
;
7135 *lphostshort
= ntohs( netshort
);
7139 /***********************************************************************
7140 * WSAProviderConfigChange (WS2_32.66)
7142 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
7143 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
7145 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
7146 return SOCKET_ERROR
;
7149 /***********************************************************************
7150 * WSARecvDisconnect (WS2_32.68)
7152 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
7154 TRACE( "(0x%04lx %p)\n", s
, disconnectdata
);
7156 return WS_shutdown( s
, 0 );
7159 /***********************************************************************
7160 * WSASetServiceA (WS2_32.76)
7162 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
7164 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
7168 /***********************************************************************
7169 * WSASetServiceW (WS2_32.77)
7171 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
7173 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
7177 /***********************************************************************
7178 * WSCEnableNSProvider (WS2_32.84)
7180 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
7182 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
7186 /***********************************************************************
7187 * WSCGetProviderPath (WS2_32.86)
7189 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
7191 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
7193 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
7195 *errcode
= WSAEINVAL
;
7196 return SOCKET_ERROR
;
7199 /***********************************************************************
7200 * WSCInstallNameSpace (WS2_32.87)
7202 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
7203 DWORD version
, LPGUID provider
)
7205 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
7206 namespace, version
, debugstr_guid(provider
) );
7210 /***********************************************************************
7211 * WSCUnInstallNameSpace (WS2_32.89)
7213 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
7215 FIXME("(%p) Stub!\n", lpProviderId
);
7219 /***********************************************************************
7220 * WSCWriteProviderOrder (WS2_32.91)
7222 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
7224 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);
7228 /***********************************************************************
7229 * WSANSPIoctl (WS2_32.91)
7231 INT WINAPI
WSANSPIoctl( HANDLE hLookup
, DWORD dwControlCode
, LPVOID lpvInBuffer
,
7232 DWORD cbInBuffer
, LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
7233 LPDWORD lpcbBytesReturned
, LPWSACOMPLETION lpCompletion
)
7235 FIXME("(%p, 0x%08x, %p, 0x%08x, %p, 0x%08x, %p, %p) Stub!\n", hLookup
, dwControlCode
,
7236 lpvInBuffer
, cbInBuffer
, lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpCompletion
);
7237 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
7238 return SOCKET_ERROR
;
7241 /*****************************************************************************
7242 * WSAEnumProtocolsA [WS2_32.@]
7244 * see function WSAEnumProtocolsW
7246 INT WINAPI
WSAEnumProtocolsA( LPINT protocols
, LPWSAPROTOCOL_INFOA buffer
, LPDWORD len
)
7248 return WS_EnumProtocols( FALSE
, protocols
, (LPWSAPROTOCOL_INFOW
) buffer
, len
);
7251 /*****************************************************************************
7252 * WSAEnumProtocolsW [WS2_32.@]
7254 * Retrieves information about specified set of active network protocols.
7257 * protocols [I] Pointer to null-terminated array of protocol id's. NULL
7258 * retrieves information on all available protocols.
7259 * buffer [I] Pointer to a buffer to be filled with WSAPROTOCOL_INFO
7261 * len [I/O] Pointer to a variable specifying buffer size. On output
7262 * the variable holds the number of bytes needed when the
7263 * specified size is too small.
7266 * Success: number of WSAPROTOCOL_INFO structures in buffer.
7267 * Failure: SOCKET_ERROR
7270 * NT4SP5 does not return SPX if protocols == NULL
7273 * - NT4SP5 returns in addition these list of NETBIOS protocols
7274 * (address family 17), each entry two times one for socket type 2 and 5
7276 * iProtocol szProtocol
7277 * 0x80000000 \Device\NwlnkNb
7278 * 0xfffffffa \Device\NetBT_CBENT7
7279 * 0xfffffffb \Device\Nbf_CBENT7
7280 * 0xfffffffc \Device\NetBT_NdisWan5
7281 * 0xfffffffd \Device\NetBT_El9202
7282 * 0xfffffffe \Device\Nbf_El9202
7283 * 0xffffffff \Device\Nbf_NdisWan4
7285 * - there is no check that the operating system supports the returned
7288 INT WINAPI
WSAEnumProtocolsW( LPINT protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
)
7290 return WS_EnumProtocols( TRUE
, protocols
, buffer
, len
);
7293 /*****************************************************************************
7294 * WSCEnumProtocols [WS2_32.@]
7297 * protocols [I] Null-terminated array of iProtocol values.
7298 * buffer [O] Buffer of WSAPROTOCOL_INFOW structures.
7299 * len [I/O] Size of buffer on input/output.
7300 * errno [O] Error code.
7303 * Success: number of protocols to be reported on.
7304 * Failure: SOCKET_ERROR. error is in errno.
7307 * Doesn't supply info on layered protocols.
7310 INT WINAPI
WSCEnumProtocols( LPINT protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
, LPINT err
)
7312 INT ret
= WSAEnumProtocolsW( protocols
, buffer
, len
);
7314 if (ret
== SOCKET_ERROR
) *err
= WSAENOBUFS
;