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>
103 #elif defined(HAVE_LINUX_IPX_H)
104 # ifdef HAVE_ASM_TYPES_H
105 # include <asm/types.h>
107 # ifdef HAVE_LINUX_TYPES_H
108 # include <linux/types.h>
110 # include <linux/ipx.h>
112 #if defined(SOL_IPX) || defined(SO_DEFAULT_HEADERS)
116 #ifdef HAVE_LINUX_IRDA_H
117 # ifdef HAVE_LINUX_TYPES_H
118 # include <linux/types.h>
120 # include <linux/irda.h>
127 #ifdef HAVE_SYS_POLL_H
128 # include <sys/poll.h>
130 #ifdef HAVE_SYS_TIME_H
131 # include <sys/time.h>
134 #define NONAMELESSUNION
135 #define NONAMELESSSTRUCT
136 #include "ntstatus.h"
137 #define WIN32_NO_STATUS
142 #include "winerror.h"
144 #include "winsock2.h"
146 #include "ws2tcpip.h"
149 #include "wshisotp.h"
153 #define USE_WC_PREFIX /* For CMSG_DATA */
154 #include "iphlpapi.h"
155 #include "wine/server.h"
156 #include "wine/debug.h"
157 #include "wine/exception.h"
158 #include "wine/unicode.h"
161 # include "wsnwlink.h"
164 #if defined(linux) && !defined(IP_UNICAST_IF)
165 #define IP_UNICAST_IF 50
168 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
169 # define sipx_network sipx_addr.x_net
170 # define sipx_node sipx_addr.x_host.c_host
171 #endif /* __FreeBSD__ */
174 #define INADDR_NONE ~0UL
177 WINE_DEFAULT_DEBUG_CHANNEL(winsock
);
178 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
180 /* names of the protocols */
181 static const WCHAR NameIpxW
[] = {'I', 'P', 'X', '\0'};
182 static const WCHAR NameSpxW
[] = {'S', 'P', 'X', '\0'};
183 static const WCHAR NameSpxIIW
[] = {'S', 'P', 'X', ' ', 'I', 'I', '\0'};
184 static const WCHAR NameTcpW
[] = {'T', 'C', 'P', '/', 'I', 'P', '\0'};
185 static const WCHAR NameUdpW
[] = {'U', 'D', 'P', '/', 'I', 'P', '\0'};
187 /* Taken from Win2k */
188 static const GUID ProviderIdIP
= { 0xe70f1aa0, 0xab8b, 0x11cf,
189 { 0x8c, 0xa3, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } };
190 static const GUID ProviderIdIPX
= { 0x11058240, 0xbe47, 0x11cf,
191 { 0x95, 0xc8, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } };
192 static const GUID ProviderIdSPX
= { 0x11058241, 0xbe47, 0x11cf,
193 { 0x95, 0xc8, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } };
195 static const INT valid_protocols
[] =
205 #define IS_IPX_PROTO(X) ((X) >= WS_NSPROTO_IPX && (X) <= WS_NSPROTO_IPX + 255)
207 #if defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER)
208 # define LINUX_BOUND_IF
209 struct interface_filter
{
210 struct sock_filter iface_memaddr
;
211 struct sock_filter iface_rule
;
212 struct sock_filter ip_memaddr
;
213 struct sock_filter ip_rule
;
214 struct sock_filter return_keep
;
215 struct sock_filter return_dump
;
217 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
218 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
219 /sizeof(struct sock_filter)
220 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
221 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
222 /sizeof(struct sock_filter)
223 # define FILTER_JUMP_NEXT() (u_char)(0)
224 # define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
225 static struct interface_filter generic_interface_filter
= {
226 /* This filter rule allows incoming packets on the specified interface, which works for all
227 * remotely generated packets and for locally generated broadcast packets. */
228 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
229 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_NEXT()),
230 /* This rule allows locally generated packets targeted at the specific IP address of the chosen
231 * adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
232 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_NET_OFF
+SKF_NET_DESTIP
),
233 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule
), FILTER_JUMP_DUMP(ip_rule
)),
234 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
235 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
237 #endif /* LINUX_BOUND_IF */
240 * The actual definition of WSASendTo, wrapped in a different function name
241 * so that internal calls from ws2_32 itself will not trigger programs like
242 * Garena, which hooks WSASendTo/WSARecvFrom calls.
244 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
245 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
246 const struct WS_sockaddr
*to
, int tolen
,
247 LPWSAOVERLAPPED lpOverlapped
,
248 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
251 * Internal fundamental receive function, essentially WSARecvFrom with an
252 * additional parameter to support message control headers.
254 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
255 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
256 struct WS_sockaddr
*lpFrom
,
257 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
258 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
259 LPWSABUF lpControlBuffer
);
261 /* critical section to protect some non-reentrant net function */
262 static CRITICAL_SECTION csWSgetXXXbyYYY
;
263 static CRITICAL_SECTION_DEBUG critsect_debug
=
265 0, 0, &csWSgetXXXbyYYY
,
266 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
267 0, 0, { (DWORD_PTR
)(__FILE__
": csWSgetXXXbyYYY") }
269 static CRITICAL_SECTION csWSgetXXXbyYYY
= { &critsect_debug
, -1, 0, 0, 0, 0 };
271 union generic_unix_sockaddr
273 struct sockaddr addr
;
274 char data
[128]; /* should be big enough for all families */
277 static inline const char *debugstr_sockaddr( const struct WS_sockaddr
*a
)
279 if (!a
) return "(nil)";
280 switch (a
->sa_family
)
283 return wine_dbg_sprintf("{ family AF_INET, address %s, port %d }",
284 inet_ntoa(((const struct sockaddr_in
*)a
)->sin_addr
),
285 ntohs(((const struct sockaddr_in
*)a
)->sin_port
));
290 struct WS_sockaddr_in6
*sin
= (struct WS_sockaddr_in6
*)a
;
292 p
= WS_inet_ntop( WS_AF_INET6
, &sin
->sin6_addr
, buf
, sizeof(buf
) );
294 p
= "(unknown IPv6 address)";
295 return wine_dbg_sprintf("{ family AF_INET6, address %s, port %d }",
296 p
, ntohs(sin
->sin6_port
));
301 char netnum
[16], nodenum
[16];
302 struct WS_sockaddr_ipx
*sin
= (struct WS_sockaddr_ipx
*)a
;
304 for (i
= 0;i
< 4; i
++) sprintf(netnum
+ i
* 2, "%02X", (unsigned char) sin
->sa_netnum
[i
]);
305 for (i
= 0;i
< 6; i
++) sprintf(nodenum
+ i
* 2, "%02X", (unsigned char) sin
->sa_nodenum
[i
]);
307 return wine_dbg_sprintf("{ family AF_IPX, address %s.%s, ipx socket %d }",
308 netnum
, nodenum
, sin
->sa_socket
);
314 memcpy( &addr
, ((const SOCKADDR_IRDA
*)a
)->irdaDeviceID
, sizeof(addr
) );
315 addr
= ntohl( addr
);
316 return wine_dbg_sprintf("{ family AF_IRDA, addr %08x, name %s }",
318 ((const SOCKADDR_IRDA
*)a
)->irdaServiceName
);
321 return wine_dbg_sprintf("{ family %d }", a
->sa_family
);
325 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
326 #define SOCKET2HANDLE(s) ((HANDLE)(s))
327 #define HANDLE2SOCKET(h) ((SOCKET)(h))
329 /****************************************************************
330 * Async IO declarations
331 ****************************************************************/
335 struct ws2_async_io
*next
;
338 struct ws2_async_shutdown
340 struct ws2_async_io io
;
342 IO_STATUS_BLOCK iosb
;
348 struct ws2_async_io io
;
350 LPWSAOVERLAPPED user_overlapped
;
351 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func
;
352 IO_STATUS_BLOCK local_iosb
;
353 struct WS_sockaddr
*addr
;
356 int val
; /* for send operations */
357 int *ptr
; /* for recv operations */
362 unsigned int n_iovecs
;
363 unsigned int first_iovec
;
364 struct iovec iovec
[1];
367 struct ws2_accept_async
369 struct ws2_async_io io
;
370 HANDLE listen_socket
;
371 HANDLE accept_socket
;
372 LPOVERLAPPED user_overlapped
;
374 PVOID buf
; /* buffer to write data to */
378 struct ws2_async
*read
;
381 static struct ws2_async_io
*async_io_freelist
;
383 static void release_async_io( struct ws2_async_io
*io
)
387 struct ws2_async_io
*next
= async_io_freelist
;
389 if (InterlockedCompareExchangePointer( (void **)&async_io_freelist
, io
, next
) == next
) return;
393 static struct ws2_async_io
*alloc_async_io( DWORD size
)
395 /* first free remaining previous fileinfos */
397 struct ws2_async_io
*io
= InterlockedExchangePointer( (void **)&async_io_freelist
, NULL
);
401 struct ws2_async_io
*next
= io
->next
;
402 HeapFree( GetProcessHeap(), 0, io
);
406 return HeapAlloc( GetProcessHeap(), 0, size
);
409 /****************************************************************/
411 /* ----------------------------------- internal data */
413 /* ws_... struct conversion flags */
415 typedef struct /* WSAAsyncSelect() control struct */
417 HANDLE service
, event
, sock
;
423 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
424 #define WS_MAX_UDP_DATAGRAM 1024
425 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
427 /* hostent's, servent's and protent's are stored in one buffer per thread,
428 * as documented on MSDN for the functions that return any of the buffers */
429 struct per_thread_data
432 struct WS_hostent
*he_buffer
;
433 struct WS_servent
*se_buffer
;
434 struct WS_protoent
*pe_buffer
;
438 char ntoa_buffer
[16]; /* 4*3 digits + 3 '.' + 1 '\0' */
441 /* internal: routing description information */
448 static INT num_startup
; /* reference counter */
449 static FARPROC blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
451 /* function prototypes */
452 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int aliases_size
, int addresses
, int address_length
);
453 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
454 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
);
455 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
456 static int ws_protocol_info(SOCKET s
, int unicode
, WSAPROTOCOL_INFOW
*buffer
, int *size
);
458 int WSAIOCTL_GetInterfaceCount(void);
459 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
461 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
, ULONG Information
);
463 #define MAP_OPTION(opt) { WS_##opt, opt }
465 static const int ws_flags_map
[][2] =
467 MAP_OPTION( MSG_OOB
),
468 MAP_OPTION( MSG_PEEK
),
469 MAP_OPTION( MSG_DONTROUTE
),
470 MAP_OPTION( MSG_WAITALL
),
473 static const int ws_sock_map
[][2] =
475 MAP_OPTION( SO_DEBUG
),
476 MAP_OPTION( SO_ACCEPTCONN
),
477 MAP_OPTION( SO_REUSEADDR
),
478 MAP_OPTION( SO_KEEPALIVE
),
479 MAP_OPTION( SO_DONTROUTE
),
480 MAP_OPTION( SO_BROADCAST
),
481 MAP_OPTION( SO_LINGER
),
482 MAP_OPTION( SO_OOBINLINE
),
483 MAP_OPTION( SO_SNDBUF
),
484 MAP_OPTION( SO_RCVBUF
),
485 MAP_OPTION( SO_ERROR
),
486 MAP_OPTION( SO_TYPE
),
488 MAP_OPTION( SO_RCVTIMEO
),
491 MAP_OPTION( SO_SNDTIMEO
),
495 static const int ws_tcp_map
[][2] =
498 MAP_OPTION( TCP_NODELAY
),
502 static const int ws_ip_map
[][2] =
504 MAP_OPTION( IP_MULTICAST_IF
),
505 MAP_OPTION( IP_MULTICAST_TTL
),
506 MAP_OPTION( IP_MULTICAST_LOOP
),
507 MAP_OPTION( IP_ADD_MEMBERSHIP
),
508 MAP_OPTION( IP_DROP_MEMBERSHIP
),
509 MAP_OPTION( IP_OPTIONS
),
511 MAP_OPTION( IP_HDRINCL
),
513 MAP_OPTION( IP_TOS
),
514 MAP_OPTION( IP_TTL
),
516 MAP_OPTION( IP_PKTINFO
),
519 MAP_OPTION( IP_UNICAST_IF
),
523 static const int ws_ipv6_map
[][2] =
525 #ifdef IPV6_ADD_MEMBERSHIP
526 MAP_OPTION( IPV6_ADD_MEMBERSHIP
),
528 #ifdef IPV6_DROP_MEMBERSHIP
529 MAP_OPTION( IPV6_DROP_MEMBERSHIP
),
531 MAP_OPTION( IPV6_MULTICAST_IF
),
532 MAP_OPTION( IPV6_MULTICAST_HOPS
),
533 MAP_OPTION( IPV6_MULTICAST_LOOP
),
534 MAP_OPTION( IPV6_UNICAST_HOPS
),
535 MAP_OPTION( IPV6_V6ONLY
),
536 #ifdef IPV6_UNICAST_IF
537 MAP_OPTION( IPV6_UNICAST_IF
),
541 static const int ws_af_map
[][2] =
543 MAP_OPTION( AF_UNSPEC
),
544 MAP_OPTION( AF_INET
),
545 MAP_OPTION( AF_INET6
),
547 MAP_OPTION( AF_IPX
),
550 MAP_OPTION( AF_IRDA
),
552 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
555 static const int ws_socktype_map
[][2] =
557 MAP_OPTION( SOCK_DGRAM
),
558 MAP_OPTION( SOCK_STREAM
),
559 MAP_OPTION( SOCK_RAW
),
560 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
563 static const int ws_proto_map
[][2] =
565 MAP_OPTION( IPPROTO_IP
),
566 MAP_OPTION( IPPROTO_TCP
),
567 MAP_OPTION( IPPROTO_UDP
),
568 MAP_OPTION( IPPROTO_ICMP
),
569 MAP_OPTION( IPPROTO_IGMP
),
570 MAP_OPTION( IPPROTO_RAW
),
571 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
574 static const int ws_aiflag_map
[][2] =
576 MAP_OPTION( AI_PASSIVE
),
577 MAP_OPTION( AI_CANONNAME
),
578 MAP_OPTION( AI_NUMERICHOST
),
579 #ifdef AI_NUMERICSERV
580 MAP_OPTION( AI_NUMERICSERV
),
583 MAP_OPTION( AI_V4MAPPED
),
585 MAP_OPTION( AI_ADDRCONFIG
),
588 static const int ws_niflag_map
[][2] =
590 MAP_OPTION( NI_NOFQDN
),
591 MAP_OPTION( NI_NUMERICHOST
),
592 MAP_OPTION( NI_NAMEREQD
),
593 MAP_OPTION( NI_NUMERICSERV
),
594 MAP_OPTION( NI_DGRAM
),
597 static const int ws_eai_map
[][2] =
599 MAP_OPTION( EAI_AGAIN
),
600 MAP_OPTION( EAI_BADFLAGS
),
601 MAP_OPTION( EAI_FAIL
),
602 MAP_OPTION( EAI_FAMILY
),
603 MAP_OPTION( EAI_MEMORY
),
604 /* Note: EAI_NODATA is deprecated, but still
605 * used by Windows and Linux... We map the newer
606 * EAI_NONAME to EAI_NODATA for now until Windows
610 MAP_OPTION( EAI_NODATA
),
613 { WS_EAI_NODATA
, EAI_NONAME
},
616 MAP_OPTION( EAI_SERVICE
),
617 MAP_OPTION( EAI_SOCKTYPE
),
621 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
623 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
625 static inline WSACMSGHDR
*fill_control_message(int level
, int type
, WSACMSGHDR
*current
, ULONG
*maxsize
, void *data
, int len
)
627 ULONG msgsize
= sizeof(WSACMSGHDR
) + WSA_CMSG_ALIGN(len
);
628 char *ptr
= (char *) current
+ sizeof(WSACMSGHDR
);
630 /* Make sure there is at least enough room for this entry */
631 if (msgsize
> *maxsize
)
634 /* Fill in the entry */
635 current
->cmsg_len
= sizeof(WSACMSGHDR
) + len
;
636 current
->cmsg_level
= level
;
637 current
->cmsg_type
= type
;
638 memcpy(ptr
, data
, len
);
639 /* Return the pointer to where next entry should go */
640 return (WSACMSGHDR
*) (ptr
+ WSA_CMSG_ALIGN(len
));
642 #endif /* IP_PKTINFO */
644 static inline int convert_control_headers(struct msghdr
*hdr
, WSABUF
*control
)
647 WSACMSGHDR
*cmsg_win
= (WSACMSGHDR
*) control
->buf
, *ptr
;
648 ULONG ctlsize
= control
->len
;
649 struct cmsghdr
*cmsg_unix
;
652 /* Loop over all the headers, converting as appropriate */
653 for (cmsg_unix
= CMSG_FIRSTHDR(hdr
); cmsg_unix
!= NULL
; cmsg_unix
= CMSG_NXTHDR(hdr
, cmsg_unix
))
655 switch(cmsg_unix
->cmsg_level
)
658 switch(cmsg_unix
->cmsg_type
)
662 /* Convert the Unix IP_PKTINFO structure to the Windows version */
663 struct in_pktinfo
*data_unix
= (struct in_pktinfo
*) CMSG_DATA(cmsg_unix
);
664 struct WS_in_pktinfo data_win
;
666 memcpy(&data_win
.ipi_addr
,&data_unix
->ipi_addr
.s_addr
,4); /* 4 bytes = 32 address bits */
667 data_win
.ipi_ifindex
= data_unix
->ipi_ifindex
;
668 ptr
= fill_control_message(WS_IPPROTO_IP
, WS_IP_PKTINFO
, ptr
, &ctlsize
,
669 (void*)&data_win
, sizeof(data_win
));
670 if (!ptr
) goto error
;
673 FIXME("Unhandled IPPROTO_IP message header type %d\n", cmsg_unix
->cmsg_type
);
678 FIXME("Unhandled message header level %d\n", cmsg_unix
->cmsg_level
);
684 /* Set the length of the returned control headers */
685 control
->len
= (ptr
== NULL
? 0 : (char*)ptr
- (char*)cmsg_win
);
686 return (ptr
!= NULL
);
687 #else /* IP_PKTINFO */
690 #endif /* IP_PKTINFO */
692 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
694 /* ----------------------------------- error handling */
696 static NTSTATUS
sock_get_ntstatus( int err
)
700 case EBADF
: return STATUS_INVALID_HANDLE
;
701 case EBUSY
: return STATUS_DEVICE_BUSY
;
703 case EACCES
: return STATUS_ACCESS_DENIED
;
704 case EFAULT
: return STATUS_NO_MEMORY
;
705 case EINVAL
: return STATUS_INVALID_PARAMETER
;
707 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
708 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
709 case EINPROGRESS
: return STATUS_PENDING
;
710 case EALREADY
: return STATUS_NETWORK_BUSY
;
711 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
712 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
713 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
714 case EPROTONOSUPPORT
:
715 case ESOCKTNOSUPPORT
:
718 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
719 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
720 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
721 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
722 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
723 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
724 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
725 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
726 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
727 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
728 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
730 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
731 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
733 case 0: return STATUS_SUCCESS
;
735 WARN("Unknown errno %d!\n", err
);
736 return STATUS_UNSUCCESSFUL
;
740 static UINT
sock_get_error( int err
)
744 case EINTR
: return WSAEINTR
;
745 case EBADF
: return WSAEBADF
;
747 case EACCES
: return WSAEACCES
;
748 case EFAULT
: return WSAEFAULT
;
749 case EINVAL
: return WSAEINVAL
;
750 case EMFILE
: return WSAEMFILE
;
751 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
752 case EINPROGRESS
: return WSAEINPROGRESS
;
753 case EALREADY
: return WSAEALREADY
;
754 case ENOTSOCK
: return WSAENOTSOCK
;
755 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
756 case EMSGSIZE
: return WSAEMSGSIZE
;
757 case EPROTOTYPE
: return WSAEPROTOTYPE
;
758 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
759 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
760 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
761 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
762 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
763 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
764 case EADDRINUSE
: return WSAEADDRINUSE
;
765 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
766 case ENETDOWN
: return WSAENETDOWN
;
767 case ENETUNREACH
: return WSAENETUNREACH
;
768 case ENETRESET
: return WSAENETRESET
;
769 case ECONNABORTED
: return WSAECONNABORTED
;
771 case ECONNRESET
: return WSAECONNRESET
;
772 case ENOBUFS
: return WSAENOBUFS
;
773 case EISCONN
: return WSAEISCONN
;
774 case ENOTCONN
: return WSAENOTCONN
;
775 case ESHUTDOWN
: return WSAESHUTDOWN
;
776 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
777 case ETIMEDOUT
: return WSAETIMEDOUT
;
778 case ECONNREFUSED
: return WSAECONNREFUSED
;
779 case ELOOP
: return WSAELOOP
;
780 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
781 case EHOSTDOWN
: return WSAEHOSTDOWN
;
782 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
783 case ENOTEMPTY
: return WSAENOTEMPTY
;
785 case EPROCLIM
: return WSAEPROCLIM
;
788 case EUSERS
: return WSAEUSERS
;
791 case EDQUOT
: return WSAEDQUOT
;
794 case ESTALE
: return WSAESTALE
;
797 case EREMOTE
: return WSAEREMOTE
;
800 /* just in case we ever get here and there are no problems */
803 WARN("Unknown errno %d!\n", err
);
804 return WSAEOPNOTSUPP
;
808 static UINT
wsaErrno(void)
810 int loc_errno
= errno
;
811 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
813 return sock_get_error( loc_errno
);
816 /* most ws2 overlapped functions return an ntstatus-based error code */
817 static NTSTATUS
wsaErrStatus(void)
819 int loc_errno
= errno
;
820 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
822 return sock_get_ntstatus(loc_errno
);
825 static UINT
wsaHerrno(int loc_errno
)
827 WARN("h_errno %d.\n", loc_errno
);
831 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
832 case TRY_AGAIN
: return WSATRY_AGAIN
;
833 case NO_RECOVERY
: return WSANO_RECOVERY
;
834 case NO_DATA
: return WSANO_DATA
;
835 case ENOBUFS
: return WSAENOBUFS
;
839 WARN("Unknown h_errno %d!\n", loc_errno
);
840 return WSAEOPNOTSUPP
;
844 static inline DWORD
NtStatusToWSAError( const DWORD status
)
846 /* We only need to cover the status codes set by server async request handling */
850 case STATUS_SUCCESS
: wserr
= 0; break;
851 case STATUS_PENDING
: wserr
= WSA_IO_PENDING
; break;
852 case STATUS_OBJECT_TYPE_MISMATCH
: wserr
= WSAENOTSOCK
; break;
853 case STATUS_INVALID_HANDLE
: wserr
= WSAEBADF
; break;
854 case STATUS_INVALID_PARAMETER
: wserr
= WSAEINVAL
; break;
855 case STATUS_PIPE_DISCONNECTED
: wserr
= WSAESHUTDOWN
; break;
856 case STATUS_NETWORK_BUSY
: wserr
= WSAEALREADY
; break;
857 case STATUS_NETWORK_UNREACHABLE
: wserr
= WSAENETUNREACH
; break;
858 case STATUS_CONNECTION_REFUSED
: wserr
= WSAECONNREFUSED
; break;
859 case STATUS_CONNECTION_DISCONNECTED
: wserr
= WSAENOTCONN
; break;
860 case STATUS_CONNECTION_RESET
: wserr
= WSAECONNRESET
; break;
861 case STATUS_CONNECTION_ABORTED
: wserr
= WSAECONNABORTED
; break;
862 case STATUS_CANCELLED
: wserr
= WSA_OPERATION_ABORTED
; break;
863 case STATUS_ADDRESS_ALREADY_ASSOCIATED
: wserr
= WSAEADDRINUSE
; break;
864 case STATUS_IO_TIMEOUT
:
865 case STATUS_TIMEOUT
: wserr
= WSAETIMEDOUT
; break;
866 case STATUS_NO_MEMORY
: wserr
= WSAEFAULT
; break;
867 case STATUS_ACCESS_DENIED
: wserr
= WSAEACCES
; break;
868 case STATUS_TOO_MANY_OPENED_FILES
: wserr
= WSAEMFILE
; break;
869 case STATUS_CANT_WAIT
: wserr
= WSAEWOULDBLOCK
; break;
870 case STATUS_BUFFER_OVERFLOW
: wserr
= WSAEMSGSIZE
; break;
871 case STATUS_NOT_SUPPORTED
: wserr
= WSAEOPNOTSUPP
; break;
872 case STATUS_HOST_UNREACHABLE
: wserr
= WSAEHOSTUNREACH
; break;
875 wserr
= RtlNtStatusToDosError( status
);
876 FIXME( "Status code %08x converted to DOS error code %x\n", status
, wserr
);
881 /* set last error code from NT status without mapping WSA errors */
882 static inline unsigned int set_error( unsigned int err
)
886 err
= NtStatusToWSAError( err
);
892 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
895 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
900 static inline void release_sock_fd( SOCKET s
, int fd
)
902 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
905 static void _enable_event( HANDLE s
, unsigned int event
,
906 unsigned int sstate
, unsigned int cstate
)
908 SERVER_START_REQ( enable_socket_event
)
910 req
->handle
= wine_server_obj_handle( s
);
912 req
->sstate
= sstate
;
913 req
->cstate
= cstate
;
914 wine_server_call( req
);
919 static NTSTATUS
_is_blocking(SOCKET s
, BOOL
*ret
)
922 SERVER_START_REQ( get_socket_event
)
924 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
925 req
->service
= FALSE
;
927 status
= wine_server_call( req
);
928 *ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
934 static unsigned int _get_sock_mask(SOCKET s
)
937 SERVER_START_REQ( get_socket_event
)
939 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
940 req
->service
= FALSE
;
942 wine_server_call( req
);
949 static void _sync_sock_state(SOCKET s
)
952 /* do a dummy wineserver request in order to let
953 the wineserver run through its select loop once */
954 (void)_is_blocking(s
, &dummy
);
957 static int _get_sock_error(SOCKET s
, unsigned int bit
)
959 int events
[FD_MAX_EVENTS
];
961 SERVER_START_REQ( get_socket_event
)
963 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
964 req
->service
= FALSE
;
966 wine_server_set_reply( req
, events
, sizeof(events
) );
967 wine_server_call( req
);
973 static struct per_thread_data
*get_per_thread_data(void)
975 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
976 /* lazy initialization */
979 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
980 NtCurrentTeb()->WinSockData
= ptb
;
985 static void free_per_thread_data(void)
987 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
991 /* delete scratch buffers */
992 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
993 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
994 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
995 ptb
->he_buffer
= NULL
;
996 ptb
->se_buffer
= NULL
;
997 ptb
->pe_buffer
= NULL
;
999 HeapFree( GetProcessHeap(), 0, ptb
);
1000 NtCurrentTeb()->WinSockData
= NULL
;
1003 /***********************************************************************
1004 * DllMain (WS2_32.init)
1006 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
1008 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
1009 switch (fdwReason
) {
1010 case DLL_PROCESS_ATTACH
:
1012 case DLL_PROCESS_DETACH
:
1013 if (fImpLoad
) break;
1014 free_per_thread_data();
1015 DeleteCriticalSection(&csWSgetXXXbyYYY
);
1017 case DLL_THREAD_DETACH
:
1018 free_per_thread_data();
1024 /***********************************************************************
1027 * Converts send/recv flags from Windows format.
1028 * Return the converted flag bits, unsupported flags remain unchanged.
1030 static int convert_flags(int flags
)
1033 if (!flags
) return 0;
1035 for (out
= i
= 0; flags
&& i
< sizeof(ws_flags_map
) / sizeof(ws_flags_map
[0]); i
++)
1037 if (ws_flags_map
[i
][0] & flags
)
1039 out
|= ws_flags_map
[i
][1];
1040 flags
&= ~ws_flags_map
[i
][0];
1045 FIXME("Unknown send/recv flags 0x%x, using anyway...\n", flags
);
1051 /***********************************************************************
1054 * Converts socket flags from Windows format.
1055 * Return 1 if converted, 0 if not (error).
1057 static int convert_sockopt(INT
*level
, INT
*optname
)
1063 *level
= SOL_SOCKET
;
1064 for(i
=0; i
<sizeof(ws_sock_map
)/sizeof(ws_sock_map
[0]); i
++) {
1065 if( ws_sock_map
[i
][0] == *optname
)
1067 *optname
= ws_sock_map
[i
][1];
1071 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
1073 case WS_IPPROTO_TCP
:
1074 *level
= IPPROTO_TCP
;
1075 for(i
=0; i
<sizeof(ws_tcp_map
)/sizeof(ws_tcp_map
[0]); i
++) {
1076 if ( ws_tcp_map
[i
][0] == *optname
)
1078 *optname
= ws_tcp_map
[i
][1];
1082 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
1085 *level
= IPPROTO_IP
;
1086 for(i
=0; i
<sizeof(ws_ip_map
)/sizeof(ws_ip_map
[0]); i
++) {
1087 if (ws_ip_map
[i
][0] == *optname
)
1089 *optname
= ws_ip_map
[i
][1];
1093 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
1095 case WS_IPPROTO_IPV6
:
1096 *level
= IPPROTO_IPV6
;
1097 for(i
=0; i
<sizeof(ws_ipv6_map
)/sizeof(ws_ipv6_map
[0]); i
++) {
1098 if (ws_ipv6_map
[i
][0] == *optname
)
1100 *optname
= ws_ipv6_map
[i
][1];
1104 FIXME("Unknown IPPROTO_IPV6 optname 0x%x\n", *optname
);
1106 default: FIXME("Unimplemented or unknown socket level\n");
1111 /* ----------------------------------- Per-thread info (or per-process?) */
1113 static char *strdup_lower(const char *str
)
1116 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
1120 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
1123 else SetLastError(WSAENOBUFS
);
1127 static inline int sock_error_p(int s
)
1129 unsigned int optval
;
1130 socklen_t optlen
= sizeof(optval
);
1131 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
1132 if (optval
) WARN("\t[%i] error: %d\n", s
, optval
);
1136 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
1137 * from an fd and return the value converted to milli seconds
1138 * or -1 if there is an infinite time out */
1139 static inline int get_rcvsnd_timeo( int fd
, int optname
)
1142 socklen_t len
= sizeof(tv
);
1143 int ret
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
1145 ret
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
1146 if( ret
<= 0 ) /* tv == {0,0} means infinite time out */
1151 /* macro wrappers for portability */
1153 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
1155 #define GET_RCVTIMEO(fd) (-1)
1159 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
1161 #define GET_SNDTIMEO(fd) (-1)
1164 /* utility: given an fd, will block until one of the events occurs */
1165 static inline int do_block( int fd
, int events
, int timeout
)
1171 pfd
.events
= events
;
1173 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
1184 convert_af_w2u(int windowsaf
) {
1187 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
1188 if (ws_af_map
[i
][0] == windowsaf
)
1189 return ws_af_map
[i
][1];
1190 FIXME("unhandled Windows address family %d\n", windowsaf
);
1195 convert_af_u2w(int unixaf
) {
1198 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
1199 if (ws_af_map
[i
][1] == unixaf
)
1200 return ws_af_map
[i
][0];
1201 FIXME("unhandled UNIX address family %d\n", unixaf
);
1206 convert_proto_w2u(int windowsproto
) {
1209 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
1210 if (ws_proto_map
[i
][0] == windowsproto
)
1211 return ws_proto_map
[i
][1];
1213 /* check for extended IPX */
1214 if (IS_IPX_PROTO(windowsproto
))
1215 return windowsproto
;
1217 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
1222 convert_proto_u2w(int unixproto
) {
1225 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
1226 if (ws_proto_map
[i
][1] == unixproto
)
1227 return ws_proto_map
[i
][0];
1229 /* if value is inside IPX range just return it - the kernel simply
1230 * echoes the value used in the socket() function */
1231 if (IS_IPX_PROTO(unixproto
))
1234 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
1239 convert_socktype_w2u(int windowssocktype
) {
1242 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
1243 if (ws_socktype_map
[i
][0] == windowssocktype
)
1244 return ws_socktype_map
[i
][1];
1245 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
1250 convert_socktype_u2w(int unixsocktype
) {
1253 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
1254 if (ws_socktype_map
[i
][1] == unixsocktype
)
1255 return ws_socktype_map
[i
][0];
1256 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
1260 static int set_ipx_packettype(int sock
, int ptype
)
1263 int fd
= get_sock_fd( sock
, 0, NULL
), ret
= 0;
1264 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", ptype
, fd
);
1266 if (fd
== -1) return SOCKET_ERROR
;
1268 /* We try to set the ipx type on ipx socket level. */
1270 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, &ptype
, sizeof(ptype
)) == -1)
1272 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
1278 /* Should we retrieve val using a getsockopt call and then
1279 * set the modified one? */
1281 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
1284 release_sock_fd( sock
, fd
);
1287 WARN("IPX support is not enabled, can't set packet type\n");
1288 return SOCKET_ERROR
;
1292 /* ----------------------------------- API -----
1294 * Init / cleanup / error checking.
1297 /***********************************************************************
1298 * WSAStartup (WS2_32.115)
1300 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
1302 TRACE("verReq=%x\n", wVersionRequested
);
1304 if (LOBYTE(wVersionRequested
) < 1)
1305 return WSAVERNOTSUPPORTED
;
1307 if (!lpWSAData
) return WSAEINVAL
;
1311 /* that's the whole of the negotiation for now */
1312 lpWSAData
->wVersion
= wVersionRequested
;
1313 /* return winsock information */
1314 lpWSAData
->wHighVersion
= 0x0202;
1315 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
1316 strcpy(lpWSAData
->szSystemStatus
, "Running" );
1317 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
1318 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
1319 /* don't do anything with lpWSAData->lpVendorInfo */
1320 /* (some apps don't allocate the space for this field) */
1322 TRACE("succeeded starts: %d\n", num_startup
);
1327 /***********************************************************************
1328 * WSACleanup (WS2_32.116)
1330 INT WINAPI
WSACleanup(void)
1334 TRACE("pending cleanups: %d\n", num_startup
);
1337 SetLastError(WSANOTINITIALISED
);
1338 return SOCKET_ERROR
;
1342 /***********************************************************************
1343 * WSAGetLastError (WS2_32.111)
1345 INT WINAPI
WSAGetLastError(void)
1347 return GetLastError();
1350 /***********************************************************************
1351 * WSASetLastError (WS2_32.112)
1353 void WINAPI
WSASetLastError(INT iError
) {
1354 SetLastError(iError
);
1357 static struct WS_hostent
*check_buffer_he(int size
)
1359 struct per_thread_data
* ptb
= get_per_thread_data();
1362 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
1363 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1365 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
1366 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
1367 return ptb
->he_buffer
;
1370 static struct WS_servent
*check_buffer_se(int size
)
1372 struct per_thread_data
* ptb
= get_per_thread_data();
1375 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
1376 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1378 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
1379 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
1380 return ptb
->se_buffer
;
1383 static struct WS_protoent
*check_buffer_pe(int size
)
1385 struct per_thread_data
* ptb
= get_per_thread_data();
1388 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
1389 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1391 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
1392 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
1393 return ptb
->pe_buffer
;
1396 /* ----------------------------------- i/o APIs */
1398 static inline BOOL
supported_pf(int pf
)
1418 static inline BOOL
supported_protocol(int protocol
)
1421 for (i
= 0; i
< sizeof(valid_protocols
) / sizeof(valid_protocols
[0]); i
++)
1422 if (protocol
== valid_protocols
[i
])
1427 /**********************************************************************/
1429 /* Returns the length of the converted address if successful, 0 if it was too small to
1432 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
1433 union generic_unix_sockaddr
*uaddr
)
1435 unsigned int uaddrlen
= 0;
1437 switch (wsaddr
->sa_family
)
1442 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
1443 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
1445 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
1448 uaddrlen
= sizeof(struct sockaddr_ipx
);
1449 memset( uaddr
, 0, uaddrlen
);
1450 uipx
->sipx_family
=AF_IPX
;
1451 uipx
->sipx_port
=wsipx
->sa_socket
;
1452 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
1455 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
1456 #ifdef IPX_FRAME_NONE
1457 uipx
->sipx_type
=IPX_FRAME_NONE
;
1463 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
1464 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
1466 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
1467 * scope_id, one without.
1469 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
1470 uaddrlen
= sizeof(struct sockaddr_in6
);
1471 memset( uaddr
, 0, uaddrlen
);
1472 uin6
->sin6_family
= AF_INET6
;
1473 uin6
->sin6_port
= win6
->sin6_port
;
1474 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
1475 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1476 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
1478 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1481 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
1485 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
1486 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
1488 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
1490 uaddrlen
= sizeof(struct sockaddr_in
);
1491 memset( uaddr
, 0, uaddrlen
);
1492 uin
->sin_family
= AF_INET
;
1493 uin
->sin_port
= win
->sin_port
;
1494 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
1499 struct sockaddr_irda
*uin
= (struct sockaddr_irda
*)uaddr
;
1500 const SOCKADDR_IRDA
*win
= (const SOCKADDR_IRDA
*)wsaddr
;
1502 if (wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1504 uaddrlen
= sizeof(struct sockaddr_irda
);
1505 memset( uaddr
, 0, uaddrlen
);
1506 uin
->sir_family
= AF_IRDA
;
1507 if (!strncmp( win
->irdaServiceName
, "LSAP-SEL", strlen( "LSAP-SEL" ) ))
1509 unsigned int lsap_sel
= 0;
1511 sscanf( win
->irdaServiceName
, "LSAP-SEL%u", &lsap_sel
);
1512 uin
->sir_lsap_sel
= lsap_sel
;
1516 uin
->sir_lsap_sel
= LSAP_ANY
;
1517 memcpy( uin
->sir_name
, win
->irdaServiceName
, 25 );
1519 memcpy( &uin
->sir_addr
, win
->irdaDeviceID
, sizeof(uin
->sir_addr
) );
1523 case WS_AF_UNSPEC
: {
1524 /* Try to determine the needed space by the passed windows sockaddr space */
1525 switch (wsaddrlen
) {
1526 default: /* likely an ipv4 address */
1527 case sizeof(struct WS_sockaddr_in
):
1528 uaddrlen
= sizeof(struct sockaddr_in
);
1531 case sizeof(struct WS_sockaddr_ipx
):
1532 uaddrlen
= sizeof(struct sockaddr_ipx
);
1536 case sizeof(SOCKADDR_IRDA
):
1537 uaddrlen
= sizeof(struct sockaddr_irda
);
1540 case sizeof(struct WS_sockaddr_in6
):
1541 case sizeof(struct WS_sockaddr_in6_old
):
1542 uaddrlen
= sizeof(struct sockaddr_in6
);
1545 memset( uaddr
, 0, uaddrlen
);
1549 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
1555 static BOOL
is_sockaddr_bound(const struct sockaddr
*uaddr
, int uaddrlen
)
1557 switch (uaddr
->sa_family
)
1562 static const struct sockaddr_ipx emptyAddr
;
1563 struct sockaddr_ipx
*ipx
= (struct sockaddr_ipx
*) uaddr
;
1564 return ipx
->sipx_port
1565 || memcmp(&ipx
->sipx_network
, &emptyAddr
.sipx_network
, sizeof(emptyAddr
.sipx_network
))
1566 || memcmp(&ipx
->sipx_node
, &emptyAddr
.sipx_node
, sizeof(emptyAddr
.sipx_node
));
1571 static const struct sockaddr_in6 emptyAddr
;
1572 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) uaddr
;
1573 return in6
->sin6_port
|| memcmp(&in6
->sin6_addr
, &emptyAddr
.sin6_addr
, sizeof(struct in6_addr
));
1577 static const struct sockaddr_in emptyAddr
;
1578 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) uaddr
;
1579 return in
->sin_port
|| memcmp(&in
->sin_addr
, &emptyAddr
.sin_addr
, sizeof(struct in_addr
));
1584 FIXME("unknown address family %d\n", uaddr
->sa_family
);
1589 /* Returns 0 if successful, -1 if the buffer is too small */
1590 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
1594 switch(uaddr
->sa_family
)
1599 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
1600 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
1603 switch (*wsaddrlen
) /* how much can we copy? */
1607 *wsaddrlen
= sizeof(*wsipx
);
1608 wsipx
->sa_socket
=uipx
->sipx_port
;
1612 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
1620 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
1626 wsipx
->sa_family
=WS_AF_IPX
;
1638 const struct sockaddr_irda
*uin
= (const struct sockaddr_irda
*)uaddr
;
1639 SOCKADDR_IRDA
*win
= (SOCKADDR_IRDA
*)wsaddr
;
1641 if (*wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1643 win
->irdaAddressFamily
= WS_AF_IRDA
;
1644 memcpy( win
->irdaDeviceID
, &uin
->sir_addr
, sizeof(win
->irdaDeviceID
) );
1645 if (uin
->sir_lsap_sel
!= LSAP_ANY
)
1646 sprintf( win
->irdaServiceName
, "LSAP-SEL%u", uin
->sir_lsap_sel
);
1648 memcpy( win
->irdaServiceName
, uin
->sir_name
,
1649 sizeof(win
->irdaServiceName
) );
1654 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
1655 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
1657 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
1659 win6old
->sin6_family
= WS_AF_INET6
;
1660 win6old
->sin6_port
= uin6
->sin6_port
;
1661 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
1662 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1663 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1664 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
1665 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
1666 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
1667 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
1670 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1672 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1677 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
1678 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
1680 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
1682 win
->sin_family
= WS_AF_INET
;
1683 win
->sin_port
= uin
->sin_port
;
1684 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
1685 memset(win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
1686 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
1690 memset(wsaddr
,0,*wsaddrlen
);
1694 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
1700 static INT
WS_DuplicateSocket(BOOL unicode
, SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
1704 WSAPROTOCOL_INFOW infow
;
1706 TRACE("(unicode %d, socket %04lx, processid %x, buffer %p)\n",
1707 unicode
, s
, dwProcessId
, lpProtocolInfo
);
1709 if (!ws_protocol_info(s
, unicode
, &infow
, &size
))
1710 return SOCKET_ERROR
;
1712 if (!(hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
)))
1714 SetLastError(WSAEINVAL
);
1715 return SOCKET_ERROR
;
1718 if (!lpProtocolInfo
)
1720 CloseHandle(hProcess
);
1721 SetLastError(WSAEFAULT
);
1722 return SOCKET_ERROR
;
1725 /* I don't know what the real Windoze does next, this is a hack */
1726 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
1727 * the target use the global duplicate, or we could copy a reference to us to the structure
1728 * and let the target duplicate it from us, but let's do it as simple as possible */
1729 memcpy(lpProtocolInfo
, &infow
, size
);
1730 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
1731 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwServiceFlags3
,
1732 0, FALSE
, DUPLICATE_SAME_ACCESS
);
1733 CloseHandle(hProcess
);
1734 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
1738 /*****************************************************************************
1739 * WS_EnterSingleProtocolW [internal]
1741 * enters the protocol information of one given protocol into the given
1745 * TRUE if a protocol was entered into the buffer.
1748 * - only implemented for IPX, SPX, SPXII, TCP, UDP
1749 * - there is no check that the operating system supports the returned
1752 static BOOL
WS_EnterSingleProtocolW( INT protocol
, WSAPROTOCOL_INFOW
* info
)
1754 memset( info
, 0, sizeof(WSAPROTOCOL_INFOW
) );
1755 info
->iProtocol
= protocol
;
1759 case WS_IPPROTO_TCP
:
1760 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_EXPEDITED_DATA
|
1761 XP1_GRACEFUL_CLOSE
| XP1_GUARANTEED_ORDER
|
1762 XP1_GUARANTEED_DELIVERY
;
1763 info
->ProviderId
= ProviderIdIP
;
1764 info
->dwCatalogEntryId
= 0x3e9;
1765 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
1766 info
->ProtocolChain
.ChainLen
= 1;
1768 info
->iAddressFamily
= WS_AF_INET
;
1769 info
->iMaxSockAddr
= 0x10;
1770 info
->iMinSockAddr
= 0x10;
1771 info
->iSocketType
= WS_SOCK_STREAM
;
1772 strcpyW( info
->szProtocol
, NameTcpW
);
1775 case WS_IPPROTO_UDP
:
1776 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_SUPPORT_BROADCAST
|
1777 XP1_SUPPORT_MULTIPOINT
| XP1_MESSAGE_ORIENTED
|
1779 info
->ProviderId
= ProviderIdIP
;
1780 info
->dwCatalogEntryId
= 0x3ea;
1781 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
1782 info
->ProtocolChain
.ChainLen
= 1;
1784 info
->iAddressFamily
= WS_AF_INET
;
1785 info
->iMaxSockAddr
= 0x10;
1786 info
->iMinSockAddr
= 0x10;
1787 info
->iSocketType
= WS_SOCK_DGRAM
;
1788 info
->dwMessageSize
= 0xffbb;
1789 strcpyW( info
->szProtocol
, NameUdpW
);
1792 case WS_NSPROTO_IPX
:
1793 info
->dwServiceFlags1
= XP1_PARTIAL_MESSAGE
| XP1_SUPPORT_BROADCAST
|
1794 XP1_SUPPORT_MULTIPOINT
| XP1_MESSAGE_ORIENTED
|
1796 info
->ProviderId
= ProviderIdIPX
;
1797 info
->dwCatalogEntryId
= 0x406;
1798 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
1799 info
->ProtocolChain
.ChainLen
= 1;
1801 info
->iAddressFamily
= WS_AF_IPX
;
1802 info
->iMaxSockAddr
= 0x10;
1803 info
->iMinSockAddr
= 0x0e;
1804 info
->iSocketType
= WS_SOCK_DGRAM
;
1805 info
->iProtocolMaxOffset
= 0xff;
1806 info
->dwMessageSize
= 0x240;
1807 strcpyW( info
->szProtocol
, NameIpxW
);
1810 case WS_NSPROTO_SPX
:
1811 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_PSEUDO_STREAM
|
1812 XP1_MESSAGE_ORIENTED
| XP1_GUARANTEED_ORDER
|
1813 XP1_GUARANTEED_DELIVERY
;
1814 info
->ProviderId
= ProviderIdSPX
;
1815 info
->dwCatalogEntryId
= 0x407;
1816 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
1817 info
->ProtocolChain
.ChainLen
= 1;
1819 info
->iAddressFamily
= WS_AF_IPX
;
1820 info
->iMaxSockAddr
= 0x10;
1821 info
->iMinSockAddr
= 0x0e;
1822 info
->iSocketType
= WS_SOCK_SEQPACKET
;
1823 info
->dwMessageSize
= 0xffffffff;
1824 strcpyW( info
->szProtocol
, NameSpxW
);
1827 case WS_NSPROTO_SPXII
:
1828 info
->dwServiceFlags1
= XP1_IFS_HANDLES
| XP1_GRACEFUL_CLOSE
|
1829 XP1_PSEUDO_STREAM
| XP1_MESSAGE_ORIENTED
|
1830 XP1_GUARANTEED_ORDER
| XP1_GUARANTEED_DELIVERY
;
1831 info
->ProviderId
= ProviderIdSPX
;
1832 info
->dwCatalogEntryId
= 0x409;
1833 info
->dwProviderFlags
= PFL_MATCHES_PROTOCOL_ZERO
;
1834 info
->ProtocolChain
.ChainLen
= 1;
1836 info
->iAddressFamily
= WS_AF_IPX
;
1837 info
->iMaxSockAddr
= 0x10;
1838 info
->iMinSockAddr
= 0x0e;
1839 info
->iSocketType
= WS_SOCK_SEQPACKET
;
1840 info
->dwMessageSize
= 0xffffffff;
1841 strcpyW( info
->szProtocol
, NameSpxIIW
);
1845 FIXME("unknown Protocol <0x%08x>\n", protocol
);
1851 /*****************************************************************************
1852 * WS_EnterSingleProtocolA [internal]
1854 * see function WS_EnterSingleProtocolW
1857 static BOOL
WS_EnterSingleProtocolA( INT protocol
, WSAPROTOCOL_INFOA
* info
)
1859 WSAPROTOCOL_INFOW infow
;
1861 memset( info
, 0, sizeof(WSAPROTOCOL_INFOA
) );
1863 ret
= WS_EnterSingleProtocolW( protocol
, &infow
);
1866 /* convert the structure from W to A */
1867 memcpy( info
, &infow
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
1868 WideCharToMultiByte( CP_ACP
, 0, infow
.szProtocol
, -1,
1869 info
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
);
1875 static INT
WS_EnumProtocols( BOOL unicode
, const INT
*protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
)
1877 INT i
= 0, items
= 0;
1881 LPWSAPROTOCOL_INFOA a
;
1882 LPWSAPROTOCOL_INFOW w
;
1886 if (!protocols
) protocols
= valid_protocols
;
1888 while (protocols
[i
])
1890 if(supported_protocol(protocols
[i
++]))
1894 size
= items
* (unicode
? sizeof(WSAPROTOCOL_INFOW
) : sizeof(WSAPROTOCOL_INFOA
));
1896 TRACE("unicode %d, protocols %p, buffer %p, length %p %d, items %d, required %d\n",
1897 unicode
, protocols
, buffer
, len
, len
? *len
: 0, items
, size
);
1899 if (*len
< size
|| !buffer
)
1902 SetLastError(WSAENOBUFS
);
1903 return SOCKET_ERROR
;
1906 for (i
= items
= 0; protocols
[i
]; i
++)
1908 if (!supported_protocol(protocols
[i
])) continue;
1911 if (WS_EnterSingleProtocolW( protocols
[i
], &info
.w
[items
] ))
1916 if (WS_EnterSingleProtocolA( protocols
[i
], &info
.a
[items
] ))
1923 static BOOL
ws_protocol_info(SOCKET s
, int unicode
, WSAPROTOCOL_INFOW
*buffer
, int *size
)
1927 *size
= unicode
? sizeof(WSAPROTOCOL_INFOW
) : sizeof(WSAPROTOCOL_INFOA
);
1928 memset(buffer
, 0, *size
);
1930 SERVER_START_REQ( get_socket_info
)
1932 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1933 status
= wine_server_call( req
);
1936 buffer
->iAddressFamily
= convert_af_u2w(reply
->family
);
1937 buffer
->iSocketType
= convert_socktype_u2w(reply
->type
);
1938 buffer
->iProtocol
= convert_proto_u2w(reply
->protocol
);
1945 unsigned int err
= NtStatusToWSAError( status
);
1946 SetLastError( err
== WSAEBADF
? WSAENOTSOCK
: err
);
1951 WS_EnterSingleProtocolW( buffer
->iProtocol
, buffer
);
1953 WS_EnterSingleProtocolA( buffer
->iProtocol
, (WSAPROTOCOL_INFOA
*)buffer
);
1958 /**************************************************************************
1959 * Functions for handling overlapped I/O
1960 **************************************************************************/
1962 /* user APC called upon async completion */
1963 static void WINAPI
ws2_async_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1965 struct ws2_async
*wsa
= arg
;
1967 if (wsa
->completion_func
) wsa
->completion_func( NtStatusToWSAError(iosb
->u
.Status
),
1968 iosb
->Information
, wsa
->user_overlapped
,
1970 release_async_io( &wsa
->io
);
1973 /***********************************************************************
1974 * WS2_recv (INTERNAL)
1976 * Workhorse for both synchronous and asynchronous recv() operations.
1978 static int WS2_recv( int fd
, struct ws2_async
*wsa
, int flags
)
1980 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1984 union generic_unix_sockaddr unix_sockaddr
;
1987 hdr
.msg_name
= NULL
;
1991 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
1992 hdr
.msg_name
= &unix_sockaddr
;
1995 hdr
.msg_namelen
= 0;
1997 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1998 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1999 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2000 hdr
.msg_accrights
= NULL
;
2001 hdr
.msg_accrightslen
= 0;
2003 hdr
.msg_control
= pktbuf
;
2004 hdr
.msg_controllen
= sizeof(pktbuf
);
2008 while ((n
= recvmsg(fd
, &hdr
, flags
)) == -1)
2014 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2017 ERR("Message control headers cannot be properly supported on this system.\n");
2018 wsa
->control
->len
= 0;
2021 if (wsa
->control
&& !convert_control_headers(&hdr
, wsa
->control
))
2023 WARN("Application passed insufficient room for control headers.\n");
2024 *wsa
->lpFlags
|= WS_MSG_CTRUNC
;
2030 /* if this socket is connected and lpFrom is not NULL, Linux doesn't give us
2031 * msg_name and msg_namelen from recvmsg, but it does set msg_namelen to zero.
2033 * quoting linux 2.6 net/ipv4/tcp.c:
2034 * "According to UNIX98, msg_name/msg_namelen are ignored
2035 * on connected socket. I was just happy when found this 8) --ANK"
2037 * likewise MSDN says that lpFrom and lpFromlen are ignored for
2038 * connection-oriented sockets, so don't try to update lpFrom.
2040 if (wsa
->addr
&& hdr
.msg_namelen
)
2041 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, wsa
->addr
, wsa
->addrlen
.ptr
);
2046 /***********************************************************************
2047 * WS2_async_recv (INTERNAL)
2049 * Handler for overlapped recv() operations.
2051 static NTSTATUS
WS2_async_recv( void *user
, IO_STATUS_BLOCK
*iosb
,
2052 NTSTATUS status
, void **apc
, void **arg
)
2054 struct ws2_async
*wsa
= user
;
2059 case STATUS_ALERTED
:
2060 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
2063 result
= WS2_recv( fd
, wsa
, convert_flags(wsa
->flags
) );
2064 wine_server_release_fd( wsa
->hSocket
, fd
);
2067 status
= STATUS_SUCCESS
;
2068 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
2072 if (errno
== EAGAIN
)
2074 status
= STATUS_PENDING
;
2075 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
2080 status
= wsaErrStatus();
2085 if (status
!= STATUS_PENDING
)
2087 iosb
->u
.Status
= status
;
2088 iosb
->Information
= result
;
2089 if (wsa
->completion_func
)
2091 *apc
= ws2_async_apc
;
2095 release_async_io( &wsa
->io
);
2100 /***********************************************************************
2101 * WS2_async_accept_recv (INTERNAL)
2103 * This function is used to finish the read part of an accept request. It is
2104 * needed to place the completion on the correct socket (listener).
2106 static NTSTATUS
WS2_async_accept_recv( void *user
, IO_STATUS_BLOCK
*iosb
,
2107 NTSTATUS status
, void **apc
, void **arg
)
2110 struct ws2_accept_async
*wsa
= user
;
2112 status
= WS2_async_recv( wsa
->read
, iosb
, status
, &junk
, &junk
);
2113 if (status
== STATUS_PENDING
)
2117 WS_AddCompletion( HANDLE2SOCKET(wsa
->listen_socket
), wsa
->cvalue
, iosb
->u
.Status
, iosb
->Information
);
2119 release_async_io( &wsa
->io
);
2123 /***********************************************************************
2124 * WS2_async_accept (INTERNAL)
2126 * This is the function called to satisfy the AcceptEx callback
2128 static NTSTATUS
WS2_async_accept( void *user
, IO_STATUS_BLOCK
*iosb
,
2129 NTSTATUS status
, void **apc
, void **arg
)
2131 struct ws2_accept_async
*wsa
= user
;
2135 TRACE("status: 0x%x listen: %p, accept: %p\n", status
, wsa
->listen_socket
, wsa
->accept_socket
);
2137 if (status
== STATUS_ALERTED
)
2139 SERVER_START_REQ( accept_into_socket
)
2141 req
->lhandle
= wine_server_obj_handle( wsa
->listen_socket
);
2142 req
->ahandle
= wine_server_obj_handle( wsa
->accept_socket
);
2143 status
= wine_server_call( req
);
2147 if (status
== STATUS_CANT_WAIT
)
2148 return STATUS_PENDING
;
2150 if (status
== STATUS_INVALID_HANDLE
)
2152 FIXME("AcceptEx accepting socket closed but request was not cancelled\n");
2153 status
= STATUS_CANCELLED
;
2156 else if (status
== STATUS_HANDLES_CLOSED
)
2157 status
= STATUS_CANCELLED
; /* strange windows behavior */
2159 if (status
!= STATUS_SUCCESS
)
2162 /* WS2 Spec says size param is extra 16 bytes long...what do we put in it? */
2163 addr
= ((char *)wsa
->buf
) + wsa
->data_len
;
2164 len
= wsa
->local_len
- sizeof(int);
2165 WS_getsockname(HANDLE2SOCKET(wsa
->accept_socket
),
2166 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
2169 addr
+= wsa
->local_len
;
2170 len
= wsa
->remote_len
- sizeof(int);
2171 WS_getpeername(HANDLE2SOCKET(wsa
->accept_socket
),
2172 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
2178 SERVER_START_REQ( register_async
)
2180 req
->type
= ASYNC_TYPE_READ
;
2181 req
->async
.handle
= wine_server_obj_handle( wsa
->accept_socket
);
2182 req
->async
.event
= wine_server_obj_handle( wsa
->user_overlapped
->hEvent
);
2183 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept_recv
);
2184 req
->async
.iosb
= wine_server_client_ptr( iosb
);
2185 req
->async
.arg
= wine_server_client_ptr( wsa
);
2186 status
= wine_server_call( req
);
2190 if (status
!= STATUS_PENDING
)
2193 /* The APC has finished but no completion should be sent for the operation yet, additional processing
2194 * needs to be performed by WS2_async_accept_recv() first. */
2195 return STATUS_MORE_PROCESSING_REQUIRED
;
2198 iosb
->u
.Status
= status
;
2199 iosb
->Information
= 0;
2201 if (wsa
->read
) release_async_io( &wsa
->read
->io
);
2202 release_async_io( &wsa
->io
);
2206 /***********************************************************************
2207 * WS2_send (INTERNAL)
2209 * Workhorse for both synchronous and asynchronous send() operations.
2211 static int WS2_send( int fd
, struct ws2_async
*wsa
, int flags
)
2214 union generic_unix_sockaddr unix_addr
;
2217 hdr
.msg_name
= NULL
;
2218 hdr
.msg_namelen
= 0;
2222 hdr
.msg_name
= &unix_addr
;
2223 hdr
.msg_namelen
= ws_sockaddr_ws2u( wsa
->addr
, wsa
->addrlen
.val
, &unix_addr
);
2224 if ( !hdr
.msg_namelen
)
2230 #if defined(HAS_IPX) && defined(SOL_IPX)
2231 if(wsa
->addr
->sa_family
== WS_AF_IPX
)
2233 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
2235 socklen_t len
= sizeof(int);
2237 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
2238 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
2239 * the packet type and then we can retrieve it using getsockopt. After that we can set the
2240 * ipx type in the sockaddr_opx structure with the stored value.
2242 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
2243 uipx
->sipx_type
= val
;
2248 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
2249 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
2250 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
2251 hdr
.msg_accrights
= NULL
;
2252 hdr
.msg_accrightslen
= 0;
2254 hdr
.msg_control
= NULL
;
2255 hdr
.msg_controllen
= 0;
2259 while ((ret
= sendmsg(fd
, &hdr
, flags
)) == -1)
2266 while (wsa
->first_iovec
< wsa
->n_iovecs
&& wsa
->iovec
[wsa
->first_iovec
].iov_len
<= n
)
2267 n
-= wsa
->iovec
[wsa
->first_iovec
++].iov_len
;
2268 if (wsa
->first_iovec
< wsa
->n_iovecs
)
2270 wsa
->iovec
[wsa
->first_iovec
].iov_base
= (char*)wsa
->iovec
[wsa
->first_iovec
].iov_base
+ n
;
2271 wsa
->iovec
[wsa
->first_iovec
].iov_len
-= n
;
2276 /***********************************************************************
2277 * WS2_async_send (INTERNAL)
2279 * Handler for overlapped send() operations.
2281 static NTSTATUS
WS2_async_send( void *user
, IO_STATUS_BLOCK
*iosb
,
2282 NTSTATUS status
, void **apc
, void **arg
)
2284 struct ws2_async
*wsa
= user
;
2289 case STATUS_ALERTED
:
2290 if ( wsa
->n_iovecs
<= wsa
->first_iovec
)
2293 status
= STATUS_SUCCESS
;
2296 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
2299 /* check to see if the data is ready (non-blocking) */
2300 result
= WS2_send( fd
, wsa
, convert_flags(wsa
->flags
) );
2301 wine_server_release_fd( wsa
->hSocket
, fd
);
2305 if (wsa
->first_iovec
< wsa
->n_iovecs
)
2306 status
= STATUS_PENDING
;
2308 status
= STATUS_SUCCESS
;
2310 iosb
->Information
+= result
;
2312 else if (errno
== EAGAIN
)
2314 status
= STATUS_PENDING
;
2318 status
= wsaErrStatus();
2322 if (status
!= STATUS_PENDING
)
2324 iosb
->u
.Status
= status
;
2325 if (wsa
->completion_func
)
2327 *apc
= ws2_async_apc
;
2331 release_async_io( &wsa
->io
);
2336 /***********************************************************************
2337 * WS2_async_shutdown (INTERNAL)
2339 * Handler for shutdown() operations on overlapped sockets.
2341 static NTSTATUS
WS2_async_shutdown( void *user
, IO_STATUS_BLOCK
*iosb
,
2342 NTSTATUS status
, void **apc
, void **arg
)
2344 struct ws2_async_shutdown
*wsa
= user
;
2349 case STATUS_ALERTED
:
2350 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
2353 switch ( wsa
->type
)
2355 case ASYNC_TYPE_READ
: err
= shutdown( fd
, 0 ); break;
2356 case ASYNC_TYPE_WRITE
: err
= shutdown( fd
, 1 ); break;
2358 status
= err
? wsaErrStatus() : STATUS_SUCCESS
;
2359 wine_server_release_fd( wsa
->hSocket
, fd
);
2362 iosb
->u
.Status
= status
;
2363 iosb
->Information
= 0;
2364 release_async_io( &wsa
->io
);
2368 /***********************************************************************
2369 * WS2_register_async_shutdown (INTERNAL)
2371 * Helper function for WS_shutdown() on overlapped sockets.
2373 static int WS2_register_async_shutdown( SOCKET s
, int type
)
2375 struct ws2_async_shutdown
*wsa
;
2378 TRACE("socket %04lx type %d\n", s
, type
);
2380 wsa
= (struct ws2_async_shutdown
*)alloc_async_io( sizeof(*wsa
) );
2384 wsa
->hSocket
= SOCKET2HANDLE(s
);
2387 SERVER_START_REQ( register_async
)
2390 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
2391 req
->async
.callback
= wine_server_client_ptr( WS2_async_shutdown
);
2392 req
->async
.iosb
= wine_server_client_ptr( &wsa
->iosb
);
2393 req
->async
.arg
= wine_server_client_ptr( wsa
);
2394 req
->async
.cvalue
= 0;
2395 status
= wine_server_call( req
);
2399 if (status
!= STATUS_PENDING
)
2401 HeapFree( GetProcessHeap(), 0, wsa
);
2402 return NtStatusToWSAError( status
);
2407 /***********************************************************************
2410 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
, int *addrlen32
)
2416 TRACE("socket %04lx\n", s
);
2417 status
= _is_blocking(s
, &is_blocking
);
2421 return INVALID_SOCKET
;
2425 /* try accepting first (if there is a deferred connection) */
2426 SERVER_START_REQ( accept_socket
)
2428 req
->lhandle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
2429 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
2430 req
->attributes
= OBJ_INHERIT
;
2431 status
= wine_server_call( req
);
2432 as
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
2437 if (addr
&& WS_getpeername(as
, addr
, addrlen32
))
2440 return SOCKET_ERROR
;
2444 if (is_blocking
&& status
== STATUS_CANT_WAIT
)
2446 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2448 do_block(fd
, POLLIN
, -1);
2449 _sync_sock_state(s
); /* let wineserver notice connection */
2450 release_sock_fd( s
, fd
);
2452 } while (is_blocking
&& status
== STATUS_CANT_WAIT
);
2455 return INVALID_SOCKET
;
2458 /***********************************************************************
2461 static BOOL WINAPI
WS2_AcceptEx(SOCKET listener
, SOCKET acceptor
, PVOID dest
, DWORD dest_len
,
2462 DWORD local_addr_len
, DWORD rem_addr_len
, LPDWORD received
,
2463 LPOVERLAPPED overlapped
)
2466 struct ws2_accept_async
*wsa
;
2469 TRACE("(%04lx, %04lx, %p, %d, %d, %d, %p, %p)\n", listener
, acceptor
, dest
, dest_len
, local_addr_len
,
2470 rem_addr_len
, received
, overlapped
);
2474 SetLastError(WSAEINVAL
);
2480 SetLastError(WSA_INVALID_PARAMETER
);
2486 SetLastError(WSAEFAULT
);
2490 fd
= get_sock_fd( listener
, FILE_READ_DATA
, NULL
);
2493 SetLastError(WSAENOTSOCK
);
2496 release_sock_fd( listener
, fd
);
2498 fd
= get_sock_fd( acceptor
, FILE_READ_DATA
, NULL
);
2501 SetLastError(WSAENOTSOCK
);
2504 release_sock_fd( acceptor
, fd
);
2506 wsa
= (struct ws2_accept_async
*)alloc_async_io( sizeof(*wsa
) );
2509 SetLastError(WSAEFAULT
);
2513 wsa
->listen_socket
= SOCKET2HANDLE(listener
);
2514 wsa
->accept_socket
= SOCKET2HANDLE(acceptor
);
2515 wsa
->user_overlapped
= overlapped
;
2516 wsa
->cvalue
= !((ULONG_PTR
)overlapped
->hEvent
& 1) ? (ULONG_PTR
)overlapped
: 0;
2518 wsa
->data_len
= dest_len
;
2519 wsa
->local_len
= local_addr_len
;
2520 wsa
->remote_len
= rem_addr_len
;
2525 /* set up a read request if we need it */
2526 wsa
->read
= (struct ws2_async
*)alloc_async_io( offsetof(struct ws2_async
, iovec
[1]) );
2529 HeapFree( GetProcessHeap(), 0, wsa
);
2530 SetLastError(WSAEFAULT
);
2534 wsa
->read
->hSocket
= wsa
->accept_socket
;
2535 wsa
->read
->flags
= 0;
2536 wsa
->read
->lpFlags
= &wsa
->read
->flags
;
2537 wsa
->read
->addr
= NULL
;
2538 wsa
->read
->addrlen
.ptr
= NULL
;
2539 wsa
->read
->control
= NULL
;
2540 wsa
->read
->n_iovecs
= 1;
2541 wsa
->read
->first_iovec
= 0;
2542 wsa
->read
->completion_func
= NULL
;
2543 wsa
->read
->iovec
[0].iov_base
= wsa
->buf
;
2544 wsa
->read
->iovec
[0].iov_len
= wsa
->data_len
;
2547 SERVER_START_REQ( register_async
)
2549 req
->type
= ASYNC_TYPE_READ
;
2550 req
->async
.handle
= wine_server_obj_handle( SOCKET2HANDLE(listener
) );
2551 req
->async
.event
= wine_server_obj_handle( overlapped
->hEvent
);
2552 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept
);
2553 req
->async
.iosb
= wine_server_client_ptr( overlapped
);
2554 req
->async
.arg
= wine_server_client_ptr( wsa
);
2555 req
->async
.cvalue
= wsa
->cvalue
;
2556 status
= wine_server_call( req
);
2560 if(status
!= STATUS_PENDING
)
2562 HeapFree( GetProcessHeap(), 0, wsa
->read
);
2563 HeapFree( GetProcessHeap(), 0, wsa
);
2566 SetLastError( NtStatusToWSAError(status
) );
2570 /***********************************************************************
2571 * GetAcceptExSockaddrs
2573 static void WINAPI
WS2_GetAcceptExSockaddrs(PVOID buffer
, DWORD data_size
, DWORD local_size
, DWORD remote_size
,
2574 struct WS_sockaddr
**local_addr
, LPINT local_addr_len
,
2575 struct WS_sockaddr
**remote_addr
, LPINT remote_addr_len
)
2577 char *cbuf
= buffer
;
2578 TRACE("(%p, %d, %d, %d, %p, %p, %p, %p)\n", buffer
, data_size
, local_size
, remote_size
, local_addr
,
2579 local_addr_len
, remote_addr
, remote_addr_len
);
2582 *local_addr_len
= *(int *) cbuf
;
2583 *local_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
2587 *remote_addr_len
= *(int *) cbuf
;
2588 *remote_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
2591 /***********************************************************************
2594 int WINAPI
WSASendMsg( SOCKET s
, LPWSAMSG msg
, DWORD dwFlags
, LPDWORD lpNumberOfBytesSent
,
2595 LPWSAOVERLAPPED lpOverlapped
,
2596 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2600 SetLastError( WSAEFAULT
);
2601 return SOCKET_ERROR
;
2604 return WS2_sendto( s
, msg
->lpBuffers
, msg
->dwBufferCount
, lpNumberOfBytesSent
,
2605 dwFlags
, msg
->name
, msg
->namelen
,
2606 lpOverlapped
, lpCompletionRoutine
);
2609 /***********************************************************************
2612 * Perform a receive operation that is capable of returning message
2613 * control headers. It is important to note that the WSAMSG parameter
2614 * must remain valid throughout the operation, even when an overlapped
2615 * receive is performed.
2617 static int WINAPI
WS2_WSARecvMsg( SOCKET s
, LPWSAMSG msg
, LPDWORD lpNumberOfBytesRecvd
,
2618 LPWSAOVERLAPPED lpOverlapped
,
2619 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2623 SetLastError( WSAEFAULT
);
2624 return SOCKET_ERROR
;
2627 return WS2_recv_base( s
, msg
->lpBuffers
, msg
->dwBufferCount
, lpNumberOfBytesRecvd
,
2628 &msg
->dwFlags
, msg
->name
, &msg
->namelen
,
2629 lpOverlapped
, lpCompletionRoutine
, &msg
->Control
);
2632 /***********************************************************************
2633 * interface_bind (INTERNAL)
2635 * Take bind() calls on any name corresponding to a local network adapter and restrict the given socket to
2636 * operating only on the specified interface. This restriction consists of two components:
2637 * 1) An outgoing packet restriction suggesting the egress interface for all packets.
2638 * 2) An incoming packet restriction dropping packets not meant for the interface.
2639 * If the function succeeds in placing these restrictions (returns TRUE) then the name for the bind() may
2640 * safely be changed to INADDR_ANY, permitting the transmission and receipt of broadcast packets on the
2641 * socket. This behavior is only relevant to UDP sockets and is needed for applications that expect to be able
2642 * to receive broadcast packets on a socket that is bound to a specific network interface.
2644 static BOOL
interface_bind( SOCKET s
, int fd
, struct sockaddr
*addr
)
2646 struct sockaddr_in
*in_sock
= (struct sockaddr_in
*) addr
;
2647 unsigned int sock_type
= 0;
2648 socklen_t optlen
= sizeof(sock_type
);
2649 in_addr_t bind_addr
= in_sock
->sin_addr
.s_addr
;
2650 PIP_ADAPTER_INFO adapters
= NULL
, adapter
;
2655 if (bind_addr
== htonl(INADDR_ANY
) || bind_addr
== htonl(INADDR_LOOPBACK
))
2656 return FALSE
; /* Not binding to a network adapter, special interface binding unnecessary. */
2657 if (getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &sock_type
, &optlen
) == -1 || sock_type
!= SOCK_DGRAM
)
2658 return FALSE
; /* Special interface binding is only necessary for UDP datagrams. */
2659 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
2661 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
2662 if (adapters
== NULL
|| GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
2664 /* Search the IPv4 adapter list for the appropriate binding interface */
2665 for (adapter
= adapters
; adapter
!= NULL
; adapter
= adapter
->Next
)
2667 in_addr_t adapter_addr
= (in_addr_t
) inet_addr(adapter
->IpAddressList
.IpAddress
.String
);
2669 if (bind_addr
== adapter_addr
)
2671 #if defined(IP_BOUND_IF)
2672 /* IP_BOUND_IF sets both the incoming and outgoing restriction at once */
2673 if (setsockopt(fd
, IPPROTO_IP
, IP_BOUND_IF
, &adapter
->Index
, sizeof(adapter
->Index
)) != 0)
2676 #elif defined(LINUX_BOUND_IF)
2677 in_addr_t ifindex
= (in_addr_t
) htonl(adapter
->Index
);
2678 struct interface_filter specific_interface_filter
;
2679 struct sock_fprog filter_prog
;
2681 if (setsockopt(fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
)) != 0)
2682 goto cleanup
; /* Failed to suggest egress interface */
2683 specific_interface_filter
= generic_interface_filter
;
2684 specific_interface_filter
.iface_rule
.k
= adapter
->Index
;
2685 specific_interface_filter
.ip_rule
.k
= htonl(adapter_addr
);
2686 filter_prog
.len
= sizeof(generic_interface_filter
)/sizeof(struct sock_filter
);
2687 filter_prog
.filter
= (struct sock_filter
*) &specific_interface_filter
;
2688 if (setsockopt(fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
)) != 0)
2689 goto cleanup
; /* Failed to specify incoming packet filter */
2692 FIXME("Broadcast packets on interface-bound sockets are not currently supported on this platform, "
2693 "receiving broadcast packets will not work on socket %04lx.\n", s
);
2698 /* Will soon be switching to INADDR_ANY: permit address reuse */
2699 if (ret
&& setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
)) == 0)
2700 TRACE("Socket %04lx bound to interface index %d\n", s
, adapter
->Index
);
2706 ERR("Failed to bind to interface, receiving broadcast packets will not work on socket %04lx.\n", s
);
2707 HeapFree(GetProcessHeap(), 0, adapters
);
2711 /***********************************************************************
2714 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
2716 int fd
= get_sock_fd( s
, 0, NULL
);
2717 int res
= SOCKET_ERROR
;
2719 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
2723 if (!name
|| (name
->sa_family
&& !supported_pf(name
->sa_family
)))
2725 SetLastError(WSAEAFNOSUPPORT
);
2729 union generic_unix_sockaddr uaddr
;
2730 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
2733 SetLastError(WSAEFAULT
);
2738 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) &uaddr
;
2739 if (name
->sa_family
== WS_AF_INET6
&&
2740 !memcmp(&in6
->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
)))
2743 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
)) == -1)
2745 release_sock_fd( s
, fd
);
2746 SetLastError(WSAEAFNOSUPPORT
);
2747 return SOCKET_ERROR
;
2751 if (name
->sa_family
== WS_AF_INET
)
2753 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
2754 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
2756 /* Trying to bind to the default host interface, using
2757 * INADDR_ANY instead*/
2758 WARN("Trying to bind to magic IP address, using "
2759 "INADDR_ANY instead.\n");
2760 in4
->sin_addr
.s_addr
= htonl(INADDR_ANY
);
2762 else if (interface_bind(s
, fd
, &uaddr
.addr
))
2763 in4
->sin_addr
.s_addr
= htonl(INADDR_ANY
);
2765 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
2767 int loc_errno
= errno
;
2768 WARN("\tfailure - errno = %i\n", errno
);
2773 SetLastError(WSAENOTSOCK
);
2776 SetLastError(WSAEINVAL
);
2781 socklen_t optlen
= sizeof(optval
);
2782 /* Windows >= 2003 will return different results depending on
2783 * SO_REUSEADDR, WSAEACCES may be returned representing that
2784 * the socket hijacking protection prevented the bind */
2785 if (!getsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&optval
, &optlen
) && optval
)
2787 SetLastError(WSAEACCES
);
2793 SetLastError(wsaErrno());
2799 res
=0; /* success */
2803 release_sock_fd( s
, fd
);
2808 /***********************************************************************
2809 * closesocket (WS2_32.3)
2811 int WINAPI
WS_closesocket(SOCKET s
)
2813 int res
= SOCKET_ERROR
, fd
;
2816 fd
= get_sock_fd(s
, FILE_READ_DATA
, NULL
);
2819 release_sock_fd(s
, fd
);
2820 if (CloseHandle(SOCKET2HANDLE(s
)))
2824 SetLastError(WSAENOTSOCK
);
2827 SetLastError(WSANOTINITIALISED
);
2828 TRACE("(socket %04lx) -> %d\n", s
, res
);
2832 static int do_connect(int fd
, const struct WS_sockaddr
* name
, int namelen
)
2834 union generic_unix_sockaddr uaddr
;
2835 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
2840 if (name
->sa_family
== WS_AF_INET
)
2842 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
2843 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
2845 /* Trying to connect to magic replace-loopback address,
2846 * assuming we really want to connect to localhost */
2847 TRACE("Trying to connect to magic IP address, using "
2848 "INADDR_LOOPBACK instead.\n");
2849 in4
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
2853 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
2859 /***********************************************************************
2860 * connect (WS2_32.4)
2862 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
2864 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2866 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
2872 int ret
= do_connect(fd
, name
, namelen
);
2874 goto connect_success
;
2876 if (ret
== WSAEINPROGRESS
)
2878 /* tell wineserver that a connection is in progress */
2879 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2881 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
2882 status
= _is_blocking( s
, &is_blocking
);
2885 release_sock_fd( s
, fd
);
2886 set_error( status
);
2887 return SOCKET_ERROR
;
2893 do_block(fd
, POLLIN
| POLLOUT
, -1);
2894 _sync_sock_state(s
); /* let wineserver notice connection */
2895 /* retrieve any error codes from it */
2896 result
= _get_sock_error(s
, FD_CONNECT_BIT
);
2898 SetLastError(NtStatusToWSAError(result
));
2901 goto connect_success
;
2906 SetLastError(WSAEWOULDBLOCK
);
2913 release_sock_fd( s
, fd
);
2915 return SOCKET_ERROR
;
2918 release_sock_fd( s
, fd
);
2919 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2920 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
2921 FD_CONNECT
|FD_WINE_LISTENING
);
2925 /***********************************************************************
2926 * WSAConnect (WS2_32.30)
2928 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
2929 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
2930 LPQOS lpSQOS
, LPQOS lpGQOS
)
2932 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
2933 FIXME("unsupported parameters!\n");
2934 return WS_connect( s
, name
, namelen
);
2937 /***********************************************************************
2940 static BOOL WINAPI
WS2_ConnectEx(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
2941 PVOID sendBuf
, DWORD sendBufLen
, LPDWORD sent
, LPOVERLAPPED ov
)
2943 int fd
, ret
, status
;
2944 union generic_unix_sockaddr uaddr
;
2945 socklen_t uaddrlen
= sizeof(uaddr
);
2949 SetLastError( ERROR_INVALID_PARAMETER
);
2953 fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2956 SetLastError( WSAENOTSOCK
);
2960 TRACE("socket %04lx, ptr %p %s, length %d, sendptr %p, len %d, ov %p\n",
2961 s
, name
, debugstr_sockaddr(name
), namelen
, sendBuf
, sendBufLen
, ov
);
2963 if (getsockname(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
2965 SetLastError(wsaErrno());
2968 else if (!is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
2970 SetLastError(WSAEINVAL
);
2974 ret
= do_connect(fd
, name
, namelen
);
2979 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2980 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
2981 FD_CONNECT
|FD_WINE_LISTENING
);
2983 wsabuf
.len
= sendBufLen
;
2984 wsabuf
.buf
= (char*) sendBuf
;
2986 /* WSASend takes care of completion if need be */
2987 if (WSASend(s
, &wsabuf
, sendBuf
? 1 : 0, sent
, 0, ov
, NULL
) != SOCKET_ERROR
)
2988 goto connection_success
;
2990 else if (ret
== WSAEINPROGRESS
)
2992 struct ws2_async
*wsa
;
2993 ULONG_PTR cvalue
= (((ULONG_PTR
)ov
->hEvent
& 1) == 0) ? (ULONG_PTR
)ov
: 0;
2995 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2997 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
2999 /* Indirectly call WSASend */
3000 if (!(wsa
= (struct ws2_async
*)alloc_async_io( offsetof( struct ws2_async
, iovec
[1] ))))
3002 SetLastError(WSAEFAULT
);
3006 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)ov
;
3007 iosb
->u
.Status
= STATUS_PENDING
;
3008 iosb
->Information
= 0;
3010 wsa
->hSocket
= SOCKET2HANDLE(s
);
3012 wsa
->addrlen
.val
= 0;
3014 wsa
->lpFlags
= &wsa
->flags
;
3015 wsa
->control
= NULL
;
3016 wsa
->n_iovecs
= sendBuf
? 1 : 0;
3017 wsa
->first_iovec
= 0;
3018 wsa
->completion_func
= NULL
;
3019 wsa
->iovec
[0].iov_base
= sendBuf
;
3020 wsa
->iovec
[0].iov_len
= sendBufLen
;
3022 SERVER_START_REQ( register_async
)
3024 req
->type
= ASYNC_TYPE_WRITE
;
3025 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
3026 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
3027 req
->async
.iosb
= wine_server_client_ptr( iosb
);
3028 req
->async
.arg
= wine_server_client_ptr( wsa
);
3029 req
->async
.event
= wine_server_obj_handle( ov
->hEvent
);
3030 req
->async
.cvalue
= cvalue
;
3031 status
= wine_server_call( req
);
3035 if (status
!= STATUS_PENDING
) HeapFree(GetProcessHeap(), 0, wsa
);
3037 /* If the connect already failed */
3038 if (status
== STATUS_PIPE_DISCONNECTED
)
3039 status
= _get_sock_error(s
, FD_CONNECT_BIT
);
3040 SetLastError( NtStatusToWSAError(status
) );
3048 release_sock_fd( s
, fd
);
3052 release_sock_fd( s
, fd
);
3057 /***********************************************************************
3058 * getpeername (WS2_32.5)
3060 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
3065 TRACE("socket %04lx, ptr %p, len %08x\n", s
, name
, namelen
? *namelen
: 0);
3067 fd
= get_sock_fd( s
, 0, NULL
);
3072 union generic_unix_sockaddr uaddr
;
3073 socklen_t uaddrlen
= sizeof(uaddr
);
3075 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) == 0)
3077 if (!name
|| !namelen
)
3078 SetLastError(WSAEFAULT
);
3079 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
3080 /* The buffer was too small */
3081 SetLastError(WSAEFAULT
);
3085 TRACE("=> %s\n", debugstr_sockaddr(name
));
3089 SetLastError(wsaErrno());
3090 release_sock_fd( s
, fd
);
3095 /***********************************************************************
3096 * getsockname (WS2_32.6)
3098 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
3103 TRACE("socket %04lx, ptr %p, len %08x\n", s
, name
, namelen
? *namelen
: 0);
3105 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
3106 if( (name
== NULL
) || (namelen
== NULL
) )
3108 SetLastError( WSAEFAULT
);
3109 return SOCKET_ERROR
;
3112 fd
= get_sock_fd( s
, 0, NULL
);
3117 union generic_unix_sockaddr uaddr
;
3118 socklen_t uaddrlen
= sizeof(uaddr
);
3120 if (getsockname(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
3122 SetLastError(wsaErrno());
3124 else if (!is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
3126 SetLastError(WSAEINVAL
);
3128 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
3130 /* The buffer was too small */
3131 SetLastError(WSAEFAULT
);
3136 TRACE("=> %s\n", debugstr_sockaddr(name
));
3138 release_sock_fd( s
, fd
);
3143 /***********************************************************************
3144 * getsockopt (WS2_32.7)
3146 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
3147 INT optname
, char *optval
, INT
*optlen
)
3152 TRACE("socket %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
3153 s
, level
, optname
, optval
, optlen
? *optlen
: 0);
3161 /* Handle common cases. The special cases are below, sorted
3163 case WS_SO_BROADCAST
:
3166 case WS_SO_KEEPALIVE
:
3167 case WS_SO_OOBINLINE
:
3169 case WS_SO_REUSEADDR
:
3171 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3172 return SOCKET_ERROR
;
3173 convert_sockopt(&level
, &optname
);
3174 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
3176 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3179 release_sock_fd( s
, fd
);
3181 case WS_SO_ACCEPTCONN
:
3182 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3183 return SOCKET_ERROR
;
3184 if (getsockopt(fd
, SOL_SOCKET
, SO_ACCEPTCONN
, optval
, (socklen_t
*)optlen
) != 0 )
3186 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3191 /* BSD returns != 0 while Windows return exact == 1 */
3192 if (*(int *)optval
) *(int *)optval
= 1;
3194 release_sock_fd( s
, fd
);
3196 case WS_SO_BSP_STATE
:
3198 int req_size
, addr_size
;
3199 WSAPROTOCOL_INFOW infow
;
3200 CSADDR_INFO
*csinfo
;
3202 ret
= ws_protocol_info(s
, TRUE
, &infow
, &addr_size
);
3205 if (infow
.iAddressFamily
== WS_AF_INET
)
3206 addr_size
= sizeof(struct sockaddr_in
);
3207 else if (infow
.iAddressFamily
== WS_AF_INET6
)
3208 addr_size
= sizeof(struct sockaddr_in6
);
3211 FIXME("Family %d is unsupported for SO_BSP_STATE\n", infow
.iAddressFamily
);
3212 SetLastError(WSAEAFNOSUPPORT
);
3213 return SOCKET_ERROR
;
3216 req_size
= sizeof(CSADDR_INFO
) + addr_size
* 2;
3217 if (*optlen
< req_size
)
3220 SetLastError(WSAEFAULT
);
3224 union generic_unix_sockaddr uaddr
;
3225 socklen_t uaddrlen
= sizeof(uaddr
);
3227 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3228 return SOCKET_ERROR
;
3230 csinfo
= (CSADDR_INFO
*) optval
;
3232 /* Check if the sock is bound */
3233 if (!getsockname(fd
, &uaddr
.addr
, &uaddrlen
) &&
3234 is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
3236 csinfo
->LocalAddr
.lpSockaddr
=
3237 (LPSOCKADDR
) (optval
+ sizeof(CSADDR_INFO
));
3238 ws_sockaddr_u2ws(&uaddr
.addr
, csinfo
->LocalAddr
.lpSockaddr
, &addr_size
);
3239 csinfo
->LocalAddr
.iSockaddrLength
= addr_size
;
3243 csinfo
->LocalAddr
.lpSockaddr
= NULL
;
3244 csinfo
->LocalAddr
.iSockaddrLength
= 0;
3247 /* Check if the sock is connected */
3248 if (!getpeername(fd
, &uaddr
.addr
, &uaddrlen
) &&
3249 is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
3251 csinfo
->RemoteAddr
.lpSockaddr
=
3252 (LPSOCKADDR
) (optval
+ sizeof(CSADDR_INFO
) + addr_size
);
3253 ws_sockaddr_u2ws(&uaddr
.addr
, csinfo
->RemoteAddr
.lpSockaddr
, &addr_size
);
3254 csinfo
->RemoteAddr
.iSockaddrLength
= addr_size
;
3258 csinfo
->RemoteAddr
.lpSockaddr
= NULL
;
3259 csinfo
->RemoteAddr
.iSockaddrLength
= 0;
3262 csinfo
->iSocketType
= infow
.iSocketType
;
3263 csinfo
->iProtocol
= infow
.iProtocol
;
3264 release_sock_fd( s
, fd
);
3267 return ret
? 0 : SOCKET_ERROR
;
3269 case WS_SO_DONTLINGER
:
3271 struct linger lingval
;
3272 socklen_t len
= sizeof(struct linger
);
3274 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
3276 SetLastError(WSAEFAULT
);
3277 return SOCKET_ERROR
;
3279 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3280 return SOCKET_ERROR
;
3282 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
3284 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3289 *(BOOL
*)optval
= !lingval
.l_onoff
;
3290 *optlen
= sizeof(BOOL
);
3293 release_sock_fd( s
, fd
);
3297 case WS_SO_CONNECT_TIME
:
3299 static int pretendtime
= 0;
3300 struct WS_sockaddr addr
;
3301 int len
= sizeof(addr
);
3303 if (!optlen
|| *optlen
< sizeof(DWORD
) || !optval
)
3305 SetLastError(WSAEFAULT
);
3306 return SOCKET_ERROR
;
3308 if (WS_getpeername(s
, &addr
, &len
) == SOCKET_ERROR
)
3309 *(DWORD
*)optval
= ~0u;
3312 if (!pretendtime
) FIXME("WS_SO_CONNECT_TIME - faking results\n");
3313 *(DWORD
*)optval
= pretendtime
++;
3315 *optlen
= sizeof(DWORD
);
3318 /* As mentioned in setsockopt, Windows ignores this, so we
3319 * always return true here */
3320 case WS_SO_DONTROUTE
:
3321 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
3323 SetLastError(WSAEFAULT
);
3324 return SOCKET_ERROR
;
3326 *(BOOL
*)optval
= TRUE
;
3327 *optlen
= sizeof(BOOL
);
3332 struct linger lingval
;
3334 socklen_t len
= sizeof(struct linger
), slen
= sizeof(int);
3336 /* struct linger and LINGER have different sizes */
3337 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
3339 SetLastError(WSAEFAULT
);
3340 return SOCKET_ERROR
;
3342 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3343 return SOCKET_ERROR
;
3345 if ((getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &so_type
, &slen
) == 0 && so_type
== SOCK_DGRAM
))
3347 SetLastError(WSAENOPROTOOPT
);
3350 else if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0)
3352 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3357 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
3358 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
3359 *optlen
= sizeof(struct linger
);
3362 release_sock_fd( s
, fd
);
3366 case WS_SO_MAX_MSG_SIZE
:
3367 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
3369 SetLastError(WSAEFAULT
);
3370 return SOCKET_ERROR
;
3372 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
3373 *(int *)optval
= 65507;
3374 *optlen
= sizeof(int);
3377 /* SO_OPENTYPE does not require a valid socket handle. */
3378 case WS_SO_OPENTYPE
:
3379 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
3381 SetLastError(WSAEFAULT
);
3382 return SOCKET_ERROR
;
3384 *(int *)optval
= get_per_thread_data()->opentype
;
3385 *optlen
= sizeof(int);
3386 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
3388 case WS_SO_PROTOCOL_INFOA
:
3389 case WS_SO_PROTOCOL_INFOW
:
3392 WSAPROTOCOL_INFOW infow
;
3394 ret
= ws_protocol_info(s
, optname
== WS_SO_PROTOCOL_INFOW
, &infow
, &size
);
3397 if (!optlen
|| !optval
|| *optlen
< size
)
3399 if(optlen
) *optlen
= size
;
3401 SetLastError(WSAEFAULT
);
3404 memcpy(optval
, &infow
, size
);
3406 return ret
? 0 : SOCKET_ERROR
;
3409 case WS_SO_RCVTIMEO
:
3412 case WS_SO_SNDTIMEO
:
3414 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
3417 socklen_t len
= sizeof(struct timeval
);
3419 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
3421 SetLastError(WSAEFAULT
);
3422 return SOCKET_ERROR
;
3424 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3425 return SOCKET_ERROR
;
3427 convert_sockopt(&level
, &optname
);
3428 if (getsockopt(fd
, level
, optname
, &tv
, &len
) != 0 )
3430 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3435 *(int *)optval
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
3436 *optlen
= sizeof(int);
3439 release_sock_fd( s
, fd
);
3445 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
3447 SetLastError(WSAEFAULT
);
3448 return SOCKET_ERROR
;
3450 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3451 return SOCKET_ERROR
;
3453 if (getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, optval
, (socklen_t
*)optlen
) != 0 )
3455 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3459 (*(int *)optval
) = convert_socktype_u2w(*(int *)optval
);
3461 release_sock_fd( s
, fd
);
3465 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
3466 SetLastError(WSAENOPROTOOPT
);
3467 return SOCKET_ERROR
;
3468 } /* end switch(optname) */
3469 }/* end case WS_SOL_SOCKET */
3471 case WS_NSPROTO_IPX
:
3473 struct WS_sockaddr_ipx addr
;
3474 IPX_ADDRESS_DATA
*data
;
3479 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
3481 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (socklen_t
*)optlen
) == -1)
3488 socklen_t len
=sizeof(struct ipx
);
3489 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
3492 *optval
= (int)val
.ipx_pt
;
3495 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
3496 release_sock_fd( s
, fd
);
3499 case WS_IPX_ADDRESS
:
3501 * On a Win2000 system with one network card there are usually
3502 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
3503 * Using this call you can then retrieve info about this all.
3504 * In case of Linux it is a bit different. Usually you have
3505 * only "one" device active and further it is not possible to
3506 * query things like the linkspeed.
3508 FIXME("IPX_ADDRESS\n");
3509 namelen
= sizeof(struct WS_sockaddr_ipx
);
3510 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
3511 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
3513 data
= (IPX_ADDRESS_DATA
*)optval
;
3514 memcpy(data
->nodenum
,addr
.sa_nodenum
,sizeof(data
->nodenum
));
3515 memcpy(data
->netnum
,addr
.sa_netnum
,sizeof(data
->netnum
));
3516 data
->adapternum
= 0;
3517 data
->wan
= FALSE
; /* We are not on a wan for now .. */
3518 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
3519 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
3520 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
3521 * note 1MB = 1000kB in this case */
3524 case WS_IPX_MAX_ADAPTER_NUM
:
3525 FIXME("IPX_MAX_ADAPTER_NUM\n");
3526 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
3530 FIXME("IPX optname:%x\n", optname
);
3531 return SOCKET_ERROR
;
3532 }/* end switch(optname) */
3533 } /* end case WS_NSPROTO_IPX */
3537 #define MAX_IRDA_DEVICES 10
3542 case WS_IRLMP_ENUMDEVICES
:
3544 char buf
[sizeof(struct irda_device_list
) +
3545 (MAX_IRDA_DEVICES
- 1) * sizeof(struct irda_device_info
)];
3547 socklen_t len
= sizeof(buf
);
3549 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3550 return SOCKET_ERROR
;
3551 res
= getsockopt( fd
, SOL_IRLMP
, IRLMP_ENUMDEVICES
, buf
, &len
);
3552 release_sock_fd( s
, fd
);
3555 SetLastError(wsaErrno());
3556 return SOCKET_ERROR
;
3560 struct irda_device_list
*src
= (struct irda_device_list
*)buf
;
3561 DEVICELIST
*dst
= (DEVICELIST
*)optval
;
3562 INT needed
= sizeof(DEVICELIST
);
3566 needed
+= (src
->len
- 1) * sizeof(IRDA_DEVICE_INFO
);
3567 if (*optlen
< needed
)
3569 SetLastError(WSAEFAULT
);
3570 return SOCKET_ERROR
;
3573 TRACE("IRLMP_ENUMDEVICES: %d devices found:\n", src
->len
);
3574 dst
->numDevice
= src
->len
;
3575 for (i
= 0; i
< src
->len
; i
++)
3577 TRACE("saddr = %08x, daddr = %08x, info = %s, hints = %02x%02x\n",
3578 src
->dev
[i
].saddr
, src
->dev
[i
].daddr
,
3579 src
->dev
[i
].info
, src
->dev
[i
].hints
[0],
3580 src
->dev
[i
].hints
[1]);
3581 memcpy( dst
->Device
[i
].irdaDeviceID
,
3583 sizeof(dst
->Device
[i
].irdaDeviceID
) ) ;
3584 memcpy( dst
->Device
[i
].irdaDeviceName
,
3586 sizeof(dst
->Device
[i
].irdaDeviceName
) ) ;
3587 memcpy( &dst
->Device
[i
].irdaDeviceHints1
,
3588 &src
->dev
[i
].hints
[0],
3589 sizeof(dst
->Device
[i
].irdaDeviceHints1
) ) ;
3590 memcpy( &dst
->Device
[i
].irdaDeviceHints2
,
3591 &src
->dev
[i
].hints
[1],
3592 sizeof(dst
->Device
[i
].irdaDeviceHints2
) ) ;
3593 dst
->Device
[i
].irdaCharSet
= src
->dev
[i
].charset
;
3599 FIXME("IrDA optname:0x%x\n", optname
);
3600 return SOCKET_ERROR
;
3602 break; /* case WS_SOL_IRLMP */
3603 #undef MAX_IRDA_DEVICES
3606 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
3607 case WS_IPPROTO_TCP
:
3610 case WS_TCP_NODELAY
:
3611 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3612 return SOCKET_ERROR
;
3613 convert_sockopt(&level
, &optname
);
3614 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
3616 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3619 release_sock_fd( s
, fd
);
3622 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
3623 return SOCKET_ERROR
;
3628 case WS_IP_ADD_MEMBERSHIP
:
3629 case WS_IP_DROP_MEMBERSHIP
:
3633 case WS_IP_MULTICAST_IF
:
3634 case WS_IP_MULTICAST_LOOP
:
3635 case WS_IP_MULTICAST_TTL
:
3642 #ifdef IP_UNICAST_IF
3643 case WS_IP_UNICAST_IF
:
3645 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3646 return SOCKET_ERROR
;
3647 convert_sockopt(&level
, &optname
);
3648 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
3650 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3653 release_sock_fd( s
, fd
);
3655 case WS_IP_DONTFRAGMENT
:
3656 FIXME("WS_IP_DONTFRAGMENT is always false!\n");
3657 *(BOOL
*)optval
= FALSE
;
3660 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
3661 return SOCKET_ERROR
;
3663 case WS_IPPROTO_IPV6
:
3666 #ifdef IPV6_ADD_MEMBERSHIP
3667 case WS_IPV6_ADD_MEMBERSHIP
:
3669 #ifdef IPV6_DROP_MEMBERSHIP
3670 case WS_IPV6_DROP_MEMBERSHIP
:
3672 case WS_IPV6_MULTICAST_IF
:
3673 case WS_IPV6_MULTICAST_HOPS
:
3674 case WS_IPV6_MULTICAST_LOOP
:
3675 case WS_IPV6_UNICAST_HOPS
:
3676 case WS_IPV6_V6ONLY
:
3677 #ifdef IPV6_UNICAST_IF
3678 case WS_IPV6_UNICAST_IF
:
3680 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3681 return SOCKET_ERROR
;
3682 convert_sockopt(&level
, &optname
);
3683 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
3685 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3688 release_sock_fd( s
, fd
);
3690 case WS_IPV6_DONTFRAG
:
3691 FIXME("WS_IPV6_DONTFRAG is always false!\n");
3692 *(BOOL
*)optval
= FALSE
;
3695 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
3696 return SOCKET_ERROR
;
3699 WARN("Unknown level: 0x%08x\n", level
);
3700 SetLastError(WSAEINVAL
);
3701 return SOCKET_ERROR
;
3702 } /* end switch(level) */
3705 /***********************************************************************
3708 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
3710 return htonl(hostlong
);
3714 /***********************************************************************
3717 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
3719 return htons(hostshort
);
3722 /***********************************************************************
3723 * WSAHtonl (WS2_32.46)
3724 * From MSDN description of error codes, this function should also
3725 * check if WinSock has been initialized and the socket is a valid
3726 * socket. But why? This function only translates a host byte order
3727 * u_long into a network byte order u_long...
3729 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
3733 *lpnetlong
= htonl(hostlong
);
3736 SetLastError(WSAEFAULT
);
3737 return SOCKET_ERROR
;
3740 /***********************************************************************
3741 * WSAHtons (WS2_32.47)
3742 * From MSDN description of error codes, this function should also
3743 * check if WinSock has been initialized and the socket is a valid
3744 * socket. But why? This function only translates a host byte order
3745 * u_short into a network byte order u_short...
3747 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
3752 *lpnetshort
= htons(hostshort
);
3755 SetLastError(WSAEFAULT
);
3756 return SOCKET_ERROR
;
3760 /***********************************************************************
3761 * inet_addr (WS2_32.11)
3763 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
3765 if (!cp
) return INADDR_NONE
;
3766 return inet_addr(cp
);
3770 /***********************************************************************
3773 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
3775 return ntohl(netlong
);
3779 /***********************************************************************
3782 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
3784 return ntohs(netshort
);
3788 /***********************************************************************
3789 * inet_ntoa (WS2_32.12)
3791 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
3793 char* s
= inet_ntoa(*((struct in_addr
*)&in
));
3796 struct per_thread_data
*data
= get_per_thread_data();
3797 strcpy(data
->ntoa_buffer
, s
);
3798 return data
->ntoa_buffer
;
3800 SetLastError(wsaErrno());
3804 static const char *debugstr_wsaioctl(DWORD ioctl
)
3806 const char *buf_type
, *family
;
3808 switch(ioctl
& 0x18000000)
3813 case WS_IOC_PROTOCOL
:
3814 family
= "IOC_PROTOCOL";
3817 family
= "IOC_VENDOR";
3819 default: /* WS_IOC_UNIX */
3821 BYTE size
= (ioctl
>> 16) & WS_IOCPARM_MASK
;
3822 char x
= (ioctl
& 0xff00) >> 8;
3823 BYTE y
= ioctl
& 0xff;
3826 switch (ioctl
& (WS_IOC_VOID
|WS_IOC_INOUT
))
3830 sprintf(args
, "%d, %d", x
, y
);
3834 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3838 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3842 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3845 return wine_dbg_sprintf("%s(%s)", buf_type
, args
);
3849 /* We are different from WS_IOC_UNIX. */
3850 switch (ioctl
& (WS_IOC_VOID
|WS_IOC_INOUT
))
3853 buf_type
= "_WSAIO";
3856 buf_type
= "_WSAIORW";
3859 buf_type
= "_WSAIOW";
3862 buf_type
= "_WSAIOR";
3869 return wine_dbg_sprintf("%s(%s, %d)", buf_type
, family
,
3870 (USHORT
)(ioctl
& 0xffff));
3873 /* do an ioctl call through the server */
3874 static DWORD
server_ioctl_sock( SOCKET s
, DWORD code
, LPVOID in_buff
, DWORD in_size
,
3875 LPVOID out_buff
, DWORD out_size
, LPDWORD ret_size
,
3876 LPWSAOVERLAPPED overlapped
,
3877 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
3879 HANDLE event
= overlapped
? overlapped
->hEvent
: 0;
3880 HANDLE handle
= SOCKET2HANDLE( s
);
3881 struct ws2_async
*wsa
;
3883 PIO_STATUS_BLOCK io
;
3885 if (!(wsa
= (struct ws2_async
*)alloc_async_io( sizeof(*wsa
) )))
3886 return WSA_NOT_ENOUGH_MEMORY
;
3887 wsa
->hSocket
= handle
;
3888 wsa
->user_overlapped
= overlapped
;
3889 wsa
->completion_func
= completion
;
3890 io
= (overlapped
? (PIO_STATUS_BLOCK
)overlapped
: &wsa
->local_iosb
);
3892 status
= NtDeviceIoControlFile( handle
, event
, ws2_async_apc
, wsa
, io
, code
,
3893 in_buff
, in_size
, out_buff
, out_size
);
3894 if (status
== STATUS_NOT_SUPPORTED
)
3896 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
3897 code
, code
>> 16, (code
>> 14) & 3, (code
>> 2) & 0xfff, code
& 3);
3899 else if (status
== STATUS_SUCCESS
)
3900 *ret_size
= io
->Information
; /* "Information" is the size written to the output buffer */
3902 if (status
!= STATUS_PENDING
) RtlFreeHeap( GetProcessHeap(), 0, wsa
);
3904 return NtStatusToWSAError( status
);
3907 /**********************************************************************
3908 * WSAIoctl (WS2_32.50)
3911 INT WINAPI
WSAIoctl(SOCKET s
, DWORD code
, LPVOID in_buff
, DWORD in_size
, LPVOID out_buff
,
3912 DWORD out_size
, LPDWORD ret_size
, LPWSAOVERLAPPED overlapped
,
3913 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
3916 DWORD status
= 0, total
= 0;
3918 TRACE("%04lx, %s, %p, %d, %p, %d, %p, %p, %p\n",
3919 s
, debugstr_wsaioctl(code
), in_buff
, in_size
, out_buff
, out_size
, ret_size
, overlapped
, completion
);
3924 if (in_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(in_buff
))
3926 SetLastError(WSAEFAULT
);
3927 return SOCKET_ERROR
;
3929 TRACE("-> FIONBIO (%x)\n", *(WS_u_long
*)in_buff
);
3930 if (_get_sock_mask(s
))
3932 /* AsyncSelect()'ed sockets are always nonblocking */
3933 if (!*(WS_u_long
*)in_buff
) status
= WSAEINVAL
;
3936 if (*(WS_u_long
*)in_buff
)
3937 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
3939 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
3944 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
3946 SetLastError(WSAEFAULT
);
3947 return SOCKET_ERROR
;
3949 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
3950 if (ioctl(fd
, FIONREAD
, out_buff
) == -1)
3951 status
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
3952 release_sock_fd( s
, fd
);
3958 unsigned int oob
= 0, atmark
= 0;
3959 socklen_t oobsize
= sizeof(int);
3960 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
3962 SetLastError(WSAEFAULT
);
3963 return SOCKET_ERROR
;
3965 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
3966 /* SO_OOBINLINE sockets must always return TRUE to SIOCATMARK */
3967 if ((getsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &oob
, &oobsize
) == -1)
3968 || (!oob
&& ioctl(fd
, SIOCATMARK
, &atmark
) == -1))
3969 status
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
3972 /* The SIOCATMARK value read from ioctl() is reversed
3973 * because BSD returns TRUE if it's in the OOB mark
3974 * while Windows returns TRUE if there are NO OOB bytes.
3976 (*(WS_u_long
*) out_buff
) = oob
|| !atmark
;
3979 release_sock_fd( s
, fd
);
3984 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
3985 SetLastError(WSAEINVAL
);
3986 return SOCKET_ERROR
;
3988 case WS_SIO_GET_INTERFACE_LIST
:
3990 INTERFACE_INFO
* intArray
= out_buff
;
3991 DWORD size
, numInt
= 0, apiReturn
;
3993 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
3995 if (!out_buff
|| !ret_size
)
3997 SetLastError(WSAEFAULT
);
3998 return SOCKET_ERROR
;
4001 fd
= get_sock_fd( s
, 0, NULL
);
4002 if (fd
== -1) return SOCKET_ERROR
;
4004 apiReturn
= GetAdaptersInfo(NULL
, &size
);
4005 if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
4007 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
4011 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
4013 PIP_ADAPTER_INFO ptr
;
4015 for (ptr
= table
, numInt
= 0; ptr
; ptr
= ptr
->Next
)
4017 unsigned int addr
, mask
, bcast
;
4018 struct ifreq ifInfo
;
4020 /* Skip interfaces without an IPv4 address. */
4021 if (ptr
->IpAddressList
.IpAddress
.String
[0] == '\0')
4024 if ((numInt
+ 1)*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > out_size
)
4026 WARN("Buffer too small = %u, out_size = %u\n", numInt
+ 1, out_size
);
4031 /* Socket Status Flags */
4032 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
4033 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
4035 ERR("Error obtaining status flags for socket!\n");
4041 /* set flags; the values of IFF_* are not the same
4042 under Linux and Windows, therefore must generate
4044 intArray
->iiFlags
= 0;
4045 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
4046 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
4047 #ifdef IFF_POINTOPOINT
4048 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
4049 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
4051 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
4052 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
4053 if (ifInfo
.ifr_flags
& IFF_UP
)
4054 intArray
->iiFlags
|= WS_IFF_UP
;
4055 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
4056 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
4059 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
4060 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
4061 bcast
= addr
| ~mask
;
4062 intArray
->iiAddress
.AddressIn
.sin_family
= AF_INET
;
4063 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
4064 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
=
4066 intArray
->iiNetmask
.AddressIn
.sin_family
= AF_INET
;
4067 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
4068 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
=
4070 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
=
4072 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
4073 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.
4081 ERR("Unable to get interface table!\n");
4084 HeapFree(GetProcessHeap(),0,table
);
4086 else status
= WSAEINVAL
;
4088 else if (apiReturn
!= ERROR_NO_DATA
)
4090 ERR("Unable to get interface table!\n");
4093 /* Calculate the size of the array being returned */
4094 total
= sizeof(INTERFACE_INFO
) * numInt
;
4095 release_sock_fd( s
, fd
);
4099 case WS_SIO_ADDRESS_LIST_QUERY
:
4103 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
4107 SetLastError(WSAEFAULT
);
4108 return SOCKET_ERROR
;
4111 if (out_size
&& out_size
< FIELD_OFFSET(SOCKET_ADDRESS_LIST
, Address
[0]))
4114 SetLastError(WSAEINVAL
);
4115 return SOCKET_ERROR
;
4118 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
4120 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
4121 SOCKET_ADDRESS_LIST
*sa_list
;
4122 SOCKADDR_IN
*sockaddr
;
4127 if (!table
|| GetAdaptersInfo(table
, &size
))
4129 HeapFree(GetProcessHeap(), 0, table
);
4134 for (p
= table
, num
= 0; p
; p
= p
->Next
)
4135 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
4137 total
= FIELD_OFFSET(SOCKET_ADDRESS_LIST
, Address
[num
]) + num
* sizeof(*sockaddr
);
4138 if (total
> out_size
|| !out_buff
)
4141 HeapFree(GetProcessHeap(), 0, table
);
4147 sa
= sa_list
->Address
;
4148 sockaddr
= (SOCKADDR_IN
*)&sa
[num
];
4149 sa_list
->iAddressCount
= num
;
4151 for (p
= table
, i
= 0; p
; p
= p
->Next
)
4153 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
4155 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
4156 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
4158 sockaddr
[i
].sin_family
= AF_INET
;
4159 sockaddr
[i
].sin_port
= 0;
4160 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
4164 HeapFree(GetProcessHeap(), 0, table
);
4168 WARN("unable to get IP address list\n");
4175 FIXME("SIO_FLUSH: stub.\n");
4178 case WS_SIO_GET_EXTENSION_FUNCTION_POINTER
:
4180 static const GUID connectex_guid
= WSAID_CONNECTEX
;
4181 static const GUID disconnectex_guid
= WSAID_DISCONNECTEX
;
4182 static const GUID acceptex_guid
= WSAID_ACCEPTEX
;
4183 static const GUID getaccepexsockaddrs_guid
= WSAID_GETACCEPTEXSOCKADDRS
;
4184 static const GUID transmitfile_guid
= WSAID_TRANSMITFILE
;
4185 static const GUID transmitpackets_guid
= WSAID_TRANSMITPACKETS
;
4186 static const GUID wsarecvmsg_guid
= WSAID_WSARECVMSG
;
4187 static const GUID wsasendmsg_guid
= WSAID_WSASENDMSG
;
4189 if ( IsEqualGUID(&connectex_guid
, in_buff
) )
4191 *(LPFN_CONNECTEX
*)out_buff
= WS2_ConnectEx
;
4194 else if ( IsEqualGUID(&disconnectex_guid
, in_buff
) )
4196 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented DisconnectEx\n");
4198 else if ( IsEqualGUID(&acceptex_guid
, in_buff
) )
4200 *(LPFN_ACCEPTEX
*)out_buff
= WS2_AcceptEx
;
4203 else if ( IsEqualGUID(&getaccepexsockaddrs_guid
, in_buff
) )
4205 *(LPFN_GETACCEPTEXSOCKADDRS
*)out_buff
= WS2_GetAcceptExSockaddrs
;
4208 else if ( IsEqualGUID(&transmitfile_guid
, in_buff
) )
4210 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitFile\n");
4212 else if ( IsEqualGUID(&transmitpackets_guid
, in_buff
) )
4214 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitPackets\n");
4216 else if ( IsEqualGUID(&wsarecvmsg_guid
, in_buff
) )
4218 *(LPFN_WSARECVMSG
*)out_buff
= WS2_WSARecvMsg
;
4221 else if ( IsEqualGUID(&wsasendmsg_guid
, in_buff
) )
4223 *(LPFN_WSASENDMSG
*)out_buff
= WSASendMsg
;
4227 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER %s: stub\n", debugstr_guid(in_buff
));
4229 status
= WSAEOPNOTSUPP
;
4232 case WS_SIO_KEEPALIVE_VALS
:
4234 struct tcp_keepalive
*k
;
4235 int keepalive
, keepidle
, keepintvl
;
4237 if (!in_buff
|| in_size
< sizeof(struct tcp_keepalive
))
4239 SetLastError(WSAEFAULT
);
4240 return SOCKET_ERROR
;
4244 keepalive
= k
->onoff
? 1 : 0;
4245 keepidle
= max( 1, (k
->keepalivetime
+ 500) / 1000 );
4246 keepintvl
= max( 1, (k
->keepaliveinterval
+ 500) / 1000 );
4248 TRACE("onoff: %d, keepalivetime: %d, keepaliveinterval: %d\n", keepalive
, keepidle
, keepintvl
);
4250 fd
= get_sock_fd(s
, 0, NULL
);
4251 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&keepalive
, sizeof(int)) == -1)
4253 #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL)
4254 /* these values need to be set only if SO_KEEPALIVE is enabled */
4257 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, (void *)&keepidle
, sizeof(int)) == -1)
4259 else if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, (void *)&keepintvl
, sizeof(int)) == -1)
4264 FIXME("ignoring keepalive interval and timeout\n");
4266 release_sock_fd(s
, fd
);
4269 case WS_SIO_ROUTING_INTERFACE_QUERY
:
4271 struct WS_sockaddr
*daddr
= (struct WS_sockaddr
*)in_buff
;
4272 struct WS_sockaddr_in
*daddr_in
= (struct WS_sockaddr_in
*)daddr
;
4273 struct WS_sockaddr_in
*saddr_in
= out_buff
;
4274 MIB_IPFORWARDROW row
;
4275 PMIB_IPADDRTABLE ipAddrTable
= NULL
;
4276 DWORD size
, i
, found_index
;
4278 TRACE("-> WS_SIO_ROUTING_INTERFACE_QUERY request\n");
4280 if (!in_buff
|| in_size
< sizeof(struct WS_sockaddr
) ||
4281 !out_buff
|| out_size
< sizeof(struct WS_sockaddr_in
) || !ret_size
)
4283 SetLastError(WSAEFAULT
);
4284 return SOCKET_ERROR
;
4286 if (daddr
->sa_family
!= AF_INET
)
4288 FIXME("unsupported address family %d\n", daddr
->sa_family
);
4289 status
= WSAEAFNOSUPPORT
;
4292 if (GetBestRoute(daddr_in
->sin_addr
.S_un
.S_addr
, 0, &row
) != NOERROR
||
4293 GetIpAddrTable(NULL
, &size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
4298 ipAddrTable
= HeapAlloc(GetProcessHeap(), 0, size
);
4299 if (GetIpAddrTable(ipAddrTable
, &size
, FALSE
))
4301 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
4305 for (i
= 0, found_index
= ipAddrTable
->dwNumEntries
;
4306 i
< ipAddrTable
->dwNumEntries
; i
++)
4308 if (ipAddrTable
->table
[i
].dwIndex
== row
.dwForwardIfIndex
)
4311 if (found_index
== ipAddrTable
->dwNumEntries
)
4313 ERR("no matching IP address for interface %d\n",
4314 row
.dwForwardIfIndex
);
4315 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
4319 saddr_in
->sin_family
= AF_INET
;
4320 saddr_in
->sin_addr
.S_un
.S_addr
= ipAddrTable
->table
[found_index
].dwAddr
;
4321 saddr_in
->sin_port
= 0;
4322 total
= sizeof(struct WS_sockaddr_in
);
4323 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
4326 case WS_SIO_SET_COMPATIBILITY_MODE
:
4327 TRACE("WS_SIO_SET_COMPATIBILITY_MODE ignored\n");
4328 status
= WSAEOPNOTSUPP
;
4330 case WS_SIO_UDP_CONNRESET
:
4331 FIXME("WS_SIO_UDP_CONNRESET stub\n");
4333 case 0x667e: /* Netscape tries hard to use bogus ioctl 0x667e */
4334 SetLastError(WSAEOPNOTSUPP
);
4335 return SOCKET_ERROR
;
4337 status
= WSAEOPNOTSUPP
;
4341 if (status
== WSAEOPNOTSUPP
)
4343 status
= server_ioctl_sock(s
, code
, in_buff
, in_size
, out_buff
, out_size
, &total
,
4344 overlapped
, completion
);
4345 if (status
!= WSAEOPNOTSUPP
)
4347 if (status
== 0 || status
== WSA_IO_PENDING
)
4348 TRACE("-> %s request\n", debugstr_wsaioctl(code
));
4350 ERR("-> %s request failed with status 0x%x\n", debugstr_wsaioctl(code
), status
);
4352 /* overlapped and completion operations will be handled by the server */
4357 FIXME("unsupported WS_IOCTL cmd (%s)\n", debugstr_wsaioctl(code
));
4362 FIXME( "completion routine %p not supported\n", completion
);
4364 else if (overlapped
)
4366 ULONG_PTR cvalue
= (overlapped
&& ((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)overlapped
: 0;
4367 overlapped
->Internal
= status
;
4368 overlapped
->InternalHigh
= total
;
4369 if (overlapped
->hEvent
) NtSetEvent( overlapped
->hEvent
, NULL
);
4370 if (cvalue
) WS_AddCompletion( HANDLE2SOCKET(s
), cvalue
, status
, total
);
4375 if (ret_size
) *ret_size
= total
;
4378 SetLastError( status
);
4379 return SOCKET_ERROR
;
4383 /***********************************************************************
4384 * ioctlsocket (WS2_32.10)
4386 int WINAPI
WS_ioctlsocket(SOCKET s
, LONG cmd
, WS_u_long
*argp
)
4389 return WSAIoctl( s
, cmd
, argp
, sizeof(WS_u_long
), argp
, sizeof(WS_u_long
), &ret_size
, NULL
, NULL
);
4392 /***********************************************************************
4393 * listen (WS2_32.13)
4395 int WINAPI
WS_listen(SOCKET s
, int backlog
)
4397 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
), ret
= SOCKET_ERROR
;
4399 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
4402 union generic_unix_sockaddr uaddr
;
4403 socklen_t uaddrlen
= sizeof(uaddr
);
4405 if (getsockname(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
4407 SetLastError(wsaErrno());
4409 else if (!is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
4411 SetLastError(WSAEINVAL
);
4413 else if (listen(fd
, backlog
) == 0)
4415 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
4417 FD_CONNECT
|FD_WINE_CONNECTED
);
4421 SetLastError(wsaErrno());
4422 release_sock_fd( s
, fd
);
4425 SetLastError(WSAENOTSOCK
);
4429 /***********************************************************************
4432 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
4434 DWORD n
, dwFlags
= flags
;
4440 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
4441 return SOCKET_ERROR
;
4446 /***********************************************************************
4447 * recvfrom (WS2_32.17)
4449 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
4450 struct WS_sockaddr
*from
, int *fromlen
)
4452 DWORD n
, dwFlags
= flags
;
4458 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
4459 return SOCKET_ERROR
;
4464 /* allocate a poll array for the corresponding fd sets */
4465 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
4466 const WS_fd_set
*exceptfds
, int *count_ptr
)
4468 unsigned int i
, j
= 0, count
= 0;
4471 if (readfds
) count
+= readfds
->fd_count
;
4472 if (writefds
) count
+= writefds
->fd_count
;
4473 if (exceptfds
) count
+= exceptfds
->fd_count
;
4477 SetLastError(WSAEINVAL
);
4480 if (!(fds
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(fds
[0]))))
4482 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
4486 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
4488 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
4489 if (fds
[j
].fd
== -1) goto failed
;
4490 fds
[j
].events
= POLLIN
;
4494 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
4496 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
4497 if (fds
[j
].fd
== -1) goto failed
;
4498 fds
[j
].events
= POLLOUT
;
4502 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
4504 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
4505 if (fds
[j
].fd
== -1) goto failed
;
4506 fds
[j
].events
= POLLHUP
;
4515 for (i
= 0; i
< readfds
->fd_count
&& j
< count
; i
++, j
++)
4516 release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
4518 for (i
= 0; i
< writefds
->fd_count
&& j
< count
; i
++, j
++)
4519 release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
4521 for (i
= 0; i
< exceptfds
->fd_count
&& j
< count
; i
++, j
++)
4522 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
4523 HeapFree( GetProcessHeap(), 0, fds
);
4527 /* release the file descriptor obtained in fd_sets_to_poll */
4528 /* must be called with the original fd_set arrays, before calling get_poll_results */
4529 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
4530 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
4532 unsigned int i
, j
= 0;
4536 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
4537 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
4541 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
4542 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
4546 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
4547 if (fds
[j
].fd
!= -1)
4549 /* make sure we have a real error before releasing the fd */
4550 if (!sock_error_p( fds
[j
].fd
)) fds
[j
].revents
= 0;
4551 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
4556 /* map the poll results back into the Windows fd sets */
4557 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
4558 const struct pollfd
*fds
)
4560 const struct pollfd
*poll_writefds
= fds
+ (readfds
? readfds
->fd_count
: 0);
4561 const struct pollfd
*poll_exceptfds
= poll_writefds
+ (writefds
? writefds
->fd_count
: 0);
4562 unsigned int i
, k
, total
= 0;
4566 for (i
= k
= 0; i
< readfds
->fd_count
; i
++)
4568 if (fds
[i
].revents
||
4569 (readfds
== writefds
&& (poll_writefds
[i
].revents
& POLLOUT
) && !(poll_writefds
[i
].revents
& POLLHUP
)) ||
4570 (readfds
== exceptfds
&& poll_exceptfds
[i
].revents
))
4571 readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
4573 readfds
->fd_count
= k
;
4576 if (writefds
&& writefds
!= readfds
)
4578 for (i
= k
= 0; i
< writefds
->fd_count
; i
++)
4580 if (((poll_writefds
[i
].revents
& POLLOUT
) && !(poll_writefds
[i
].revents
& POLLHUP
)) ||
4581 (writefds
== exceptfds
&& poll_exceptfds
[i
].revents
))
4582 writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
4584 writefds
->fd_count
= k
;
4587 if (exceptfds
&& exceptfds
!= readfds
&& exceptfds
!= writefds
)
4589 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++)
4590 if (poll_exceptfds
[i
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
4591 exceptfds
->fd_count
= k
;
4598 /***********************************************************************
4599 * select (WS2_32.18)
4601 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
4602 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
4603 const struct WS_timeval
* ws_timeout
)
4605 struct pollfd
*pollfds
;
4606 struct timeval tv1
, tv2
;
4608 int count
, ret
, timeout
= -1;
4610 TRACE("read %p, write %p, excp %p timeout %p\n",
4611 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
4613 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)))
4614 return SOCKET_ERROR
;
4618 torig
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
4620 gettimeofday( &tv1
, 0 );
4623 while ((ret
= poll( pollfds
, count
, timeout
)) < 0)
4627 if (!ws_timeout
) continue;
4628 gettimeofday( &tv2
, 0 );
4630 tv2
.tv_sec
-= tv1
.tv_sec
;
4631 tv2
.tv_usec
-= tv1
.tv_usec
;
4632 if (tv2
.tv_usec
< 0)
4634 tv2
.tv_usec
+= 1000000;
4638 timeout
= torig
- (tv2
.tv_sec
* 1000) - (tv2
.tv_usec
+ 999) / 1000;
4639 if (timeout
<= 0) break;
4642 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
4644 if (ret
== -1) SetLastError(wsaErrno());
4645 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
4646 HeapFree( GetProcessHeap(), 0, pollfds
);
4650 /* helper to send completion messages for client-only i/o operation case */
4651 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
,
4654 SERVER_START_REQ( add_fd_completion
)
4656 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(sock
) );
4657 req
->cvalue
= CompletionValue
;
4658 req
->status
= CompletionStatus
;
4659 req
->information
= Information
;
4660 wine_server_call( req
);
4666 /***********************************************************************
4669 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
4675 wsabuf
.buf
= (char*) buf
;
4677 if ( WS2_sendto( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
4678 return SOCKET_ERROR
;
4683 /***********************************************************************
4684 * WSASend (WS2_32.72)
4686 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4687 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
4688 LPWSAOVERLAPPED lpOverlapped
,
4689 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4691 return WS2_sendto( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
4692 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
4695 /***********************************************************************
4696 * WSASendDisconnect (WS2_32.73)
4698 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
4700 return WS_shutdown( s
, SD_SEND
);
4704 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4705 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
4706 const struct WS_sockaddr
*to
, int tolen
,
4707 LPWSAOVERLAPPED lpOverlapped
,
4708 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4710 unsigned int i
, options
;
4711 int n
, fd
, err
, overlapped
, flags
;
4712 struct ws2_async
*wsa
= NULL
, localwsa
;
4713 int totalLength
= 0;
4717 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
4718 s
, lpBuffers
, dwBufferCount
, dwFlags
,
4719 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
4721 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
4722 TRACE( "fd=%d, options=%x\n", fd
, options
);
4724 if ( fd
== -1 ) return SOCKET_ERROR
;
4726 if (!lpOverlapped
&& !lpNumberOfBytesSent
)
4732 overlapped
= (lpOverlapped
|| lpCompletionRoutine
) &&
4733 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
4734 if (overlapped
|| dwBufferCount
> 1)
4736 if (!(wsa
= (struct ws2_async
*)alloc_async_io( offsetof(struct ws2_async
, iovec
[dwBufferCount
]))))
4745 wsa
->hSocket
= SOCKET2HANDLE(s
);
4746 wsa
->addr
= (struct WS_sockaddr
*)to
;
4747 wsa
->addrlen
.val
= tolen
;
4748 wsa
->flags
= dwFlags
;
4749 wsa
->lpFlags
= &wsa
->flags
;
4750 wsa
->control
= NULL
;
4751 wsa
->n_iovecs
= dwBufferCount
;
4752 wsa
->first_iovec
= 0;
4753 for ( i
= 0; i
< dwBufferCount
; i
++ )
4755 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
4756 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
4757 totalLength
+= lpBuffers
[i
].len
;
4760 flags
= convert_flags(dwFlags
);
4761 n
= WS2_send( fd
, wsa
, flags
);
4762 if (n
== -1 && errno
!= EAGAIN
)
4770 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
4771 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
4773 wsa
->user_overlapped
= lpOverlapped
;
4774 wsa
->completion_func
= lpCompletionRoutine
;
4775 release_sock_fd( s
, fd
);
4777 if (n
== -1 || n
< totalLength
)
4779 iosb
->u
.Status
= STATUS_PENDING
;
4780 iosb
->Information
= n
== -1 ? 0 : n
;
4782 SERVER_START_REQ( register_async
)
4784 req
->type
= ASYNC_TYPE_WRITE
;
4785 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
4786 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
4787 req
->async
.iosb
= wine_server_client_ptr( iosb
);
4788 req
->async
.arg
= wine_server_client_ptr( wsa
);
4789 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
4790 req
->async
.cvalue
= cvalue
;
4791 err
= wine_server_call( req
);
4795 /* Enable the event only after starting the async. The server will deliver it as soon as
4796 the async is done. */
4797 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
4799 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
4800 SetLastError(NtStatusToWSAError( err
));
4801 return SOCKET_ERROR
;
4804 iosb
->u
.Status
= STATUS_SUCCESS
;
4805 iosb
->Information
= n
;
4806 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= n
;
4807 if (!wsa
->completion_func
)
4809 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
4810 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
4811 HeapFree( GetProcessHeap(), 0, wsa
);
4813 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
4814 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
4815 SetLastError(ERROR_SUCCESS
);
4819 if ((err
= _is_blocking( s
, &is_blocking
)))
4821 err
= NtStatusToWSAError( err
);
4827 /* On a blocking non-overlapped stream socket,
4828 * sending blocks until the entire buffer is sent. */
4829 DWORD timeout_start
= GetTickCount();
4831 bytes_sent
= n
== -1 ? 0 : n
;
4833 while (wsa
->first_iovec
< wsa
->n_iovecs
)
4836 int timeout
= GET_SNDTIMEO(fd
);
4840 timeout
-= GetTickCount() - timeout_start
;
4841 if (timeout
< 0) timeout
= 0;
4845 pfd
.events
= POLLOUT
;
4847 if (!timeout
|| !poll( &pfd
, 1, timeout
))
4850 goto error
; /* msdn says a timeout in send is fatal */
4853 n
= WS2_send( fd
, wsa
, flags
);
4854 if (n
== -1 && errno
!= EAGAIN
)
4864 else /* non-blocking */
4866 if (n
< totalLength
)
4867 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
4870 err
= WSAEWOULDBLOCK
;
4876 TRACE(" -> %i bytes\n", bytes_sent
);
4878 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= bytes_sent
;
4879 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
4880 release_sock_fd( s
, fd
);
4881 SetLastError(ERROR_SUCCESS
);
4885 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
4886 release_sock_fd( s
, fd
);
4887 WARN(" -> ERROR %d\n", err
);
4889 return SOCKET_ERROR
;
4892 /***********************************************************************
4893 * WSASendTo (WS2_32.74)
4895 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4896 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
4897 const struct WS_sockaddr
*to
, int tolen
,
4898 LPWSAOVERLAPPED lpOverlapped
,
4899 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4901 return WS2_sendto( s
, lpBuffers
, dwBufferCount
,
4902 lpNumberOfBytesSent
, dwFlags
,
4904 lpOverlapped
, lpCompletionRoutine
);
4907 /***********************************************************************
4908 * sendto (WS2_32.20)
4910 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
4911 const struct WS_sockaddr
*to
, int tolen
)
4917 wsabuf
.buf
= (char*) buf
;
4919 if ( WS2_sendto(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
4920 return SOCKET_ERROR
;
4925 /***********************************************************************
4926 * setsockopt (WS2_32.21)
4928 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
4929 const char *optval
, int optlen
)
4933 struct linger linger
;
4934 struct timeval tval
;
4936 TRACE("socket %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
4937 s
, level
, optname
, optval
, optlen
);
4939 /* some broken apps pass the value directly instead of a pointer to it */
4940 if(optlen
&& IS_INTRESOURCE(optval
))
4942 SetLastError(WSAEFAULT
);
4943 return SOCKET_ERROR
;
4951 /* Some options need some conversion before they can be sent to
4952 * setsockopt. The conversions are done here, then they will fall through
4953 * to the general case. Special options that are not passed to
4954 * setsockopt follow below that.*/
4956 case WS_SO_DONTLINGER
:
4959 SetLastError(WSAEFAULT
);
4960 return SOCKET_ERROR
;
4962 linger
.l_onoff
= *(const int*)optval
== 0;
4963 linger
.l_linger
= 0;
4965 optname
= SO_LINGER
;
4966 optval
= (char*)&linger
;
4967 optlen
= sizeof(struct linger
);
4973 SetLastError(WSAEFAULT
);
4974 return SOCKET_ERROR
;
4976 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
4977 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
4979 optname
= SO_LINGER
;
4980 optval
= (char*)&linger
;
4981 optlen
= sizeof(struct linger
);
4985 if (*(const int*)optval
< 2048)
4987 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
4992 /* The options listed here don't need any special handling. Thanks to
4993 * the conversion happening above, options from there will fall through
4995 case WS_SO_ACCEPTCONN
:
4996 case WS_SO_BROADCAST
:
4998 case WS_SO_KEEPALIVE
:
4999 case WS_SO_OOBINLINE
:
5000 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
5001 * however, using it the BSD way fixes bug 8513 and seems to be what
5002 * most programmers assume, anyway */
5003 case WS_SO_REUSEADDR
:
5006 convert_sockopt(&level
, &optname
);
5009 /* SO_DEBUG is a privileged operation, ignore it. */
5011 TRACE("Ignoring SO_DEBUG\n");
5014 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
5015 * socket. According to MSDN, this option is silently ignored.*/
5016 case WS_SO_DONTROUTE
:
5017 TRACE("Ignoring SO_DONTROUTE\n");
5020 /* Stops two sockets from being bound to the same port. Always happens
5021 * on unix systems, so just drop it. */
5022 case WS_SO_EXCLUSIVEADDRUSE
:
5023 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
5026 /* After a ConnectEx call succeeds, the socket can't be used with half of the
5027 * normal winsock functions on windows. We don't have that problem. */
5028 case WS_SO_UPDATE_CONNECT_CONTEXT
:
5029 TRACE("Ignoring SO_UPDATE_CONNECT_CONTEXT, since our sockets are normal\n");
5032 /* After a AcceptEx call succeeds, the socket can't be used with half of the
5033 * normal winsock functions on windows. We don't have that problem. */
5034 case WS_SO_UPDATE_ACCEPT_CONTEXT
:
5035 TRACE("Ignoring SO_UPDATE_ACCEPT_CONTEXT, since our sockets are normal\n");
5038 /* SO_OPENTYPE does not require a valid socket handle. */
5039 case WS_SO_OPENTYPE
:
5040 if (!optlen
|| optlen
< sizeof(int) || !optval
)
5042 SetLastError(WSAEFAULT
);
5043 return SOCKET_ERROR
;
5045 get_per_thread_data()->opentype
= *(const int *)optval
;
5046 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((const int*)optval
) );
5050 case WS_SO_RCVTIMEO
:
5053 case WS_SO_SNDTIMEO
:
5055 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
5056 if (optval
&& optlen
== sizeof(UINT32
)) {
5057 /* WinSock passes milliseconds instead of struct timeval */
5058 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
5059 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
5060 /* min of 500 milliseconds */
5061 if (tval
.tv_sec
== 0 && tval
.tv_usec
&& tval
.tv_usec
< 500000)
5062 tval
.tv_usec
= 500000;
5063 optlen
= sizeof(struct timeval
);
5064 optval
= (char*)&tval
;
5065 } else if (optlen
== sizeof(struct timeval
)) {
5066 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
5068 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
5071 convert_sockopt(&level
, &optname
);
5076 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
5077 SetLastError(WSAENOPROTOOPT
);
5078 return SOCKET_ERROR
;
5080 break; /* case WS_SOL_SOCKET */
5083 case WS_NSPROTO_IPX
:
5087 return set_ipx_packettype(s
, *(int*)optval
);
5089 case WS_IPX_FILTERPTYPE
:
5090 /* Sets the receive filter packet type, at the moment we don't support it */
5091 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
5092 /* Returning 0 is better for now than returning a SOCKET_ERROR */
5096 FIXME("opt_name:%x\n", optname
);
5097 return SOCKET_ERROR
;
5099 break; /* case WS_NSPROTO_IPX */
5102 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
5103 case WS_IPPROTO_TCP
:
5106 case WS_TCP_NODELAY
:
5107 convert_sockopt(&level
, &optname
);
5110 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
5111 return SOCKET_ERROR
;
5118 case WS_IP_ADD_MEMBERSHIP
:
5119 case WS_IP_DROP_MEMBERSHIP
:
5123 case WS_IP_MULTICAST_IF
:
5124 case WS_IP_MULTICAST_LOOP
:
5125 case WS_IP_MULTICAST_TTL
:
5132 #ifdef IP_UNICAST_IF
5133 case WS_IP_UNICAST_IF
:
5135 convert_sockopt(&level
, &optname
);
5137 case WS_IP_DONTFRAGMENT
:
5138 FIXME("IP_DONTFRAGMENT is silently ignored!\n");
5141 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
5142 return SOCKET_ERROR
;
5146 case WS_IPPROTO_IPV6
:
5149 #ifdef IPV6_ADD_MEMBERSHIP
5150 case WS_IPV6_ADD_MEMBERSHIP
:
5152 #ifdef IPV6_DROP_MEMBERSHIP
5153 case WS_IPV6_DROP_MEMBERSHIP
:
5155 case WS_IPV6_MULTICAST_IF
:
5156 case WS_IPV6_MULTICAST_HOPS
:
5157 case WS_IPV6_MULTICAST_LOOP
:
5158 case WS_IPV6_UNICAST_HOPS
:
5159 case WS_IPV6_V6ONLY
:
5160 #ifdef IPV6_UNICAST_IF
5161 case WS_IPV6_UNICAST_IF
:
5163 convert_sockopt(&level
, &optname
);
5165 case WS_IPV6_DONTFRAG
:
5166 FIXME("IPV6_DONTFRAG is silently ignored!\n");
5168 case WS_IPV6_PROTECTION_LEVEL
:
5169 FIXME("IPV6_PROTECTION_LEVEL is ignored!\n");
5172 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
5173 return SOCKET_ERROR
;
5178 WARN("Unknown level: 0x%08x\n", level
);
5179 SetLastError(WSAEINVAL
);
5180 return SOCKET_ERROR
;
5181 } /* end switch(level) */
5183 /* avoid endianness issues if argument is a 16-bit int */
5184 if (optval
&& optlen
< sizeof(int))
5186 woptval
= *((const INT16
*) optval
);
5187 optval
= (char*) &woptval
;
5188 woptval
&= (1 << optlen
* 8) - 1;
5191 fd
= get_sock_fd( s
, 0, NULL
);
5192 if (fd
== -1) return SOCKET_ERROR
;
5194 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
5197 if (level
== SOL_SOCKET
&& optname
== SO_REUSEADDR
&&
5198 setsockopt(fd
, level
, SO_REUSEPORT
, optval
, optlen
) != 0)
5200 SetLastError(wsaErrno());
5201 release_sock_fd( s
, fd
);
5202 return SOCKET_ERROR
;
5205 release_sock_fd( s
, fd
);
5208 TRACE("Setting socket error, %d\n", wsaErrno());
5209 SetLastError(wsaErrno());
5210 release_sock_fd( s
, fd
);
5212 return SOCKET_ERROR
;
5215 /***********************************************************************
5216 * shutdown (WS2_32.22)
5218 int WINAPI
WS_shutdown(SOCKET s
, int how
)
5220 int fd
, err
= WSAENOTSOCK
;
5221 unsigned int options
, clear_flags
= 0;
5223 fd
= get_sock_fd( s
, 0, &options
);
5224 TRACE("socket %04lx, how %i %x\n", s
, how
, options
);
5227 return SOCKET_ERROR
;
5231 case SD_RECEIVE
: /* drop receives */
5232 clear_flags
|= FD_READ
;
5234 case SD_SEND
: /* drop sends */
5235 clear_flags
|= FD_WRITE
;
5237 case SD_BOTH
: /* drop all */
5238 clear_flags
|= FD_READ
|FD_WRITE
;
5241 clear_flags
|= FD_WINE_LISTENING
;
5244 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
5249 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
5252 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
5256 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
5257 if (!err
) err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
5260 if (err
) goto error
;
5262 else /* non-overlapped mode */
5264 if ( shutdown( fd
, how
) )
5271 release_sock_fd( s
, fd
);
5272 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
5273 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
5277 release_sock_fd( s
, fd
);
5278 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
5279 SetLastError( err
);
5280 return SOCKET_ERROR
;
5283 /***********************************************************************
5284 * socket (WS2_32.23)
5286 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
5288 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
5290 return WSASocketA( af
, type
, protocol
, NULL
, 0,
5291 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
5295 /***********************************************************************
5296 * gethostbyaddr (WS2_32.51)
5298 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
5300 struct WS_hostent
*retval
= NULL
;
5301 struct hostent
* host
;
5302 int unixtype
= convert_af_w2u(type
);
5303 const char *paddr
= addr
;
5304 unsigned long loopback
;
5305 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
5307 int ebufsize
= 1024;
5308 struct hostent hostentry
;
5309 int locerr
= ENOBUFS
;
5312 /* convert back the magic loopback address if necessary */
5313 if (unixtype
== AF_INET
&& len
== 4 && !memcmp(addr
, magic_loopback_addr
, 4))
5315 loopback
= htonl(INADDR_LOOPBACK
);
5316 paddr
= (char*) &loopback
;
5319 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
5321 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
5323 int res
= gethostbyaddr_r(paddr
, len
, unixtype
,
5324 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
5325 if (res
!= ERANGE
) break;
5327 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
5329 if (host
) retval
= WS_dup_he(host
);
5330 else SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
5331 HeapFree(GetProcessHeap(),0,extrabuf
);
5333 EnterCriticalSection( &csWSgetXXXbyYYY
);
5334 host
= gethostbyaddr(paddr
, len
, unixtype
);
5335 if (host
) retval
= WS_dup_he(host
);
5336 else SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
5337 LeaveCriticalSection( &csWSgetXXXbyYYY
);
5339 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
5343 /***********************************************************************
5344 * WS_compare_routes_by_metric_asc (INTERNAL)
5346 * Comparison function for qsort(), for sorting two routes (struct route)
5347 * by metric in ascending order.
5349 static int WS_compare_routes_by_metric_asc(const void *left
, const void *right
)
5351 return ((const struct route
*)left
)->metric
- ((const struct route
*)right
)->metric
;
5354 /***********************************************************************
5355 * WS_get_local_ips (INTERNAL)
5357 * Returns the list of local IP addresses by going through the network
5358 * adapters and using the local routing table to sort the addresses
5359 * from highest routing priority to lowest routing priority. This
5360 * functionality is inferred from the description for obtaining local
5361 * IP addresses given in the Knowledge Base Article Q160215.
5363 * Please note that the returned hostent is only freed when the thread
5364 * closes and is replaced if another hostent is requested.
5366 static struct WS_hostent
* WS_get_local_ips( char *hostname
)
5368 int numroutes
= 0, i
, j
;
5370 PIP_ADAPTER_INFO adapters
= NULL
, k
;
5371 struct WS_hostent
*hostlist
= NULL
;
5372 PMIB_IPFORWARDTABLE routes
= NULL
;
5373 struct route
*route_addrs
= NULL
;
5374 DWORD adap_size
, route_size
;
5376 /* Obtain the size of the adapter list and routing table, also allocate memory */
5377 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
5379 if (GetIpForwardTable(NULL
, &route_size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
5381 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
5382 routes
= HeapAlloc(GetProcessHeap(), 0, route_size
);
5383 route_addrs
= HeapAlloc(GetProcessHeap(), 0, 0); /* HeapReAlloc doesn't work on NULL */
5384 if (adapters
== NULL
|| routes
== NULL
|| route_addrs
== NULL
)
5386 /* Obtain the adapter list and the full routing table */
5387 if (GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
5389 if (GetIpForwardTable(routes
, &route_size
, FALSE
) != NO_ERROR
)
5391 /* Store the interface associated with each route */
5392 for (n
= 0; n
< routes
->dwNumEntries
; n
++)
5396 BOOL exists
= FALSE
;
5398 if (routes
->table
[n
].u1
.ForwardType
!= MIB_IPROUTE_TYPE_DIRECT
)
5400 ifindex
= routes
->table
[n
].dwForwardIfIndex
;
5401 ifmetric
= routes
->table
[n
].dwForwardMetric1
;
5402 /* Only store the lowest valued metric for an interface */
5403 for (j
= 0; j
< numroutes
; j
++)
5405 if (route_addrs
[j
].interface
== ifindex
)
5407 if (route_addrs
[j
].metric
> ifmetric
)
5408 route_addrs
[j
].metric
= ifmetric
;
5414 route_addrs
= HeapReAlloc(GetProcessHeap(), 0, route_addrs
, (numroutes
+1)*sizeof(struct route
));
5415 if (route_addrs
== NULL
)
5416 goto cleanup
; /* Memory allocation error, fail gracefully */
5417 route_addrs
[numroutes
].interface
= ifindex
;
5418 route_addrs
[numroutes
].metric
= ifmetric
;
5419 /* If no IP is found in the next step (for whatever reason)
5420 * then fall back to the magic loopback address.
5422 memcpy(&(route_addrs
[numroutes
].addr
.s_addr
), magic_loopback_addr
, 4);
5426 goto cleanup
; /* No routes, fall back to the Magic IP */
5427 /* Find the IP address associated with each found interface */
5428 for (i
= 0; i
< numroutes
; i
++)
5430 for (k
= adapters
; k
!= NULL
; k
= k
->Next
)
5432 char *ip
= k
->IpAddressList
.IpAddress
.String
;
5434 if (route_addrs
[i
].interface
== k
->Index
)
5435 route_addrs
[i
].addr
.s_addr
= (in_addr_t
) inet_addr(ip
);
5438 /* Allocate a hostent and enough memory for all the IPs,
5439 * including the NULL at the end of the list.
5441 hostlist
= WS_create_he(hostname
, 1, 0, numroutes
+1, sizeof(struct in_addr
));
5442 if (hostlist
== NULL
)
5443 goto cleanup
; /* Failed to allocate a hostent for the list of IPs */
5444 hostlist
->h_addr_list
[numroutes
] = NULL
; /* NULL-terminate the address list */
5445 hostlist
->h_aliases
[0] = NULL
; /* NULL-terminate the alias list */
5446 hostlist
->h_addrtype
= AF_INET
;
5447 hostlist
->h_length
= sizeof(struct in_addr
); /* = 4 */
5448 /* Reorder the entries before placing them in the host list. Windows expects
5449 * the IP list in order from highest priority to lowest (the critical thing
5450 * is that most applications expect the first IP to be the default route).
5453 qsort(route_addrs
, numroutes
, sizeof(struct route
), WS_compare_routes_by_metric_asc
);
5455 for (i
= 0; i
< numroutes
; i
++)
5456 (*(struct in_addr
*) hostlist
->h_addr_list
[i
]) = route_addrs
[i
].addr
;
5458 /* Cleanup all allocated memory except the address list,
5459 * the address list is used by the calling app.
5462 HeapFree(GetProcessHeap(), 0, route_addrs
);
5463 HeapFree(GetProcessHeap(), 0, adapters
);
5464 HeapFree(GetProcessHeap(), 0, routes
);
5468 /***********************************************************************
5469 * gethostbyname (WS2_32.52)
5471 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
5473 struct WS_hostent
*retval
= NULL
;
5474 struct hostent
* host
;
5475 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
5478 struct hostent hostentry
;
5479 int locerr
= ENOBUFS
;
5483 SetLastError(WSANOTINITIALISED
);
5486 if( gethostname( hostname
, 100) == -1) {
5487 SetLastError(WSAENOBUFS
); /* appropriate ? */
5490 if( !name
|| !name
[0]) {
5493 /* If the hostname of the local machine is requested then return the
5494 * complete list of local IP addresses */
5495 if(strcmp(name
, hostname
) == 0)
5496 retval
= WS_get_local_ips(hostname
);
5497 /* If any other hostname was requested (or the routing table lookup failed)
5498 * then return the IP found by the host OS */
5501 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
5503 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
5505 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
5506 if( res
!= ERANGE
) break;
5508 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
5510 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
5512 EnterCriticalSection( &csWSgetXXXbyYYY
);
5513 host
= gethostbyname(name
);
5514 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
5516 if (host
) retval
= WS_dup_he(host
);
5517 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
5518 HeapFree(GetProcessHeap(),0,extrabuf
);
5520 LeaveCriticalSection( &csWSgetXXXbyYYY
);
5523 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
5524 strcmp(name
, "localhost") != 0)
5526 /* hostname != "localhost" but has loopback address. replace by our
5527 * special address.*/
5528 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
5530 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
5535 /***********************************************************************
5536 * getprotobyname (WS2_32.53)
5538 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
5540 struct WS_protoent
* retval
= NULL
;
5541 #ifdef HAVE_GETPROTOBYNAME
5542 struct protoent
* proto
;
5543 EnterCriticalSection( &csWSgetXXXbyYYY
);
5544 if( (proto
= getprotobyname(name
)) != NULL
)
5546 retval
= WS_dup_pe(proto
);
5549 MESSAGE("protocol %s not found; You might want to add "
5550 "this to /etc/protocols\n", debugstr_a(name
) );
5551 SetLastError(WSANO_DATA
);
5553 LeaveCriticalSection( &csWSgetXXXbyYYY
);
5555 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
5560 /***********************************************************************
5561 * getprotobynumber (WS2_32.54)
5563 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
5565 struct WS_protoent
* retval
= NULL
;
5566 #ifdef HAVE_GETPROTOBYNUMBER
5567 struct protoent
* proto
;
5568 EnterCriticalSection( &csWSgetXXXbyYYY
);
5569 if( (proto
= getprotobynumber(number
)) != NULL
)
5571 retval
= WS_dup_pe(proto
);
5574 MESSAGE("protocol number %d not found; You might want to add "
5575 "this to /etc/protocols\n", number
);
5576 SetLastError(WSANO_DATA
);
5578 LeaveCriticalSection( &csWSgetXXXbyYYY
);
5580 TRACE("%i ret %p\n", number
, retval
);
5585 /***********************************************************************
5586 * getservbyname (WS2_32.55)
5588 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
5590 struct WS_servent
* retval
= NULL
;
5591 struct servent
* serv
;
5593 char *proto_str
= NULL
;
5595 if (!(name_str
= strdup_lower(name
))) return NULL
;
5597 if (proto
&& *proto
)
5599 if (!(proto_str
= strdup_lower(proto
)))
5601 HeapFree( GetProcessHeap(), 0, name_str
);
5606 EnterCriticalSection( &csWSgetXXXbyYYY
);
5607 serv
= getservbyname(name_str
, proto_str
);
5610 retval
= WS_dup_se(serv
);
5612 else SetLastError(WSANO_DATA
);
5613 LeaveCriticalSection( &csWSgetXXXbyYYY
);
5614 HeapFree( GetProcessHeap(), 0, proto_str
);
5615 HeapFree( GetProcessHeap(), 0, name_str
);
5616 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
5620 /***********************************************************************
5621 * freeaddrinfo (WS2_32.@)
5623 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
5626 struct WS_addrinfo
*next
;
5628 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
5629 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
5630 next
= res
->ai_next
;
5631 HeapFree(GetProcessHeap(),0,res
);
5636 /* helper functions for getaddrinfo()/getnameinfo() */
5637 static int convert_aiflag_w2u(int winflags
) {
5641 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
5642 if (ws_aiflag_map
[i
][0] & winflags
) {
5643 unixflags
|= ws_aiflag_map
[i
][1];
5644 winflags
&= ~ws_aiflag_map
[i
][0];
5647 FIXME("Unhandled windows AI_xxx flags %x\n", winflags
);
5651 static int convert_niflag_w2u(int winflags
) {
5655 for (i
=0;i
<sizeof(ws_niflag_map
)/sizeof(ws_niflag_map
[0]);i
++)
5656 if (ws_niflag_map
[i
][0] & winflags
) {
5657 unixflags
|= ws_niflag_map
[i
][1];
5658 winflags
&= ~ws_niflag_map
[i
][0];
5661 FIXME("Unhandled windows NI_xxx flags %x\n", winflags
);
5665 static int convert_aiflag_u2w(int unixflags
) {
5669 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
5670 if (ws_aiflag_map
[i
][1] & unixflags
) {
5671 winflags
|= ws_aiflag_map
[i
][0];
5672 unixflags
&= ~ws_aiflag_map
[i
][1];
5674 if (unixflags
) /* will warn usually */
5675 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags
);
5679 static int convert_eai_u2w(int unixret
) {
5682 if (!unixret
) return 0;
5684 for (i
=0;ws_eai_map
[i
][0];i
++)
5685 if (ws_eai_map
[i
][1] == unixret
)
5686 return ws_eai_map
[i
][0];
5688 if (unixret
== EAI_SYSTEM
)
5689 /* There are broken versions of glibc which return EAI_SYSTEM
5690 * and set errno to 0 instead of returning EAI_NONAME.
5692 return errno
? sock_get_error( errno
) : WS_EAI_NONAME
;
5694 FIXME("Unhandled unix EAI_xxx ret %d\n", unixret
);
5698 static char *get_hostname(void)
5703 GetComputerNameExA( ComputerNamePhysicalDnsHostname
, NULL
, &size
);
5704 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
5705 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, size
))) return NULL
;
5706 if (!GetComputerNameExA( ComputerNamePhysicalDnsHostname
, ret
, &size
))
5708 HeapFree( GetProcessHeap(), 0, ret
);
5714 /***********************************************************************
5715 * getaddrinfo (WS2_32.@)
5717 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
5719 #ifdef HAVE_GETADDRINFO
5720 struct addrinfo
*unixaires
= NULL
;
5722 struct addrinfo unixhints
, *punixhints
= NULL
;
5723 char *hostname
= NULL
;
5727 if (!nodename
&& !servname
)
5729 SetLastError(WSAHOST_NOT_FOUND
);
5730 return WSAHOST_NOT_FOUND
;
5735 else if (!nodename
[0])
5737 node
= hostname
= get_hostname();
5738 if (!node
) return WSA_NOT_ENOUGH_MEMORY
;
5743 /* servname tweak required by OSX and BSD kernels */
5744 if (servname
&& !servname
[0]) servname
= "0";
5747 punixhints
= &unixhints
;
5749 memset(&unixhints
, 0, sizeof(unixhints
));
5750 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
5752 /* zero is a wildcard, no need to convert */
5753 if (hints
->ai_family
)
5754 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
5755 if (hints
->ai_socktype
)
5756 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
5757 if (hints
->ai_protocol
)
5758 punixhints
->ai_protocol
= max(convert_proto_w2u(hints
->ai_protocol
), 0);
5760 if (punixhints
->ai_socktype
< 0)
5762 SetLastError(WSAESOCKTNOSUPPORT
);
5763 HeapFree(GetProcessHeap(), 0, hostname
);
5764 return SOCKET_ERROR
;
5767 /* windows allows invalid combinations of socket type and protocol, unix does not.
5768 * fix the parameters here to make getaddrinfo call always work */
5769 if (punixhints
->ai_protocol
== IPPROTO_TCP
&&
5770 punixhints
->ai_socktype
!= SOCK_STREAM
&& punixhints
->ai_socktype
!= SOCK_SEQPACKET
)
5771 punixhints
->ai_socktype
= 0;
5773 else if (punixhints
->ai_protocol
== IPPROTO_UDP
&& punixhints
->ai_socktype
!= SOCK_DGRAM
)
5774 punixhints
->ai_socktype
= 0;
5776 else if (IS_IPX_PROTO(punixhints
->ai_protocol
) && punixhints
->ai_socktype
!= SOCK_DGRAM
)
5777 punixhints
->ai_socktype
= 0;
5780 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
5781 result
= getaddrinfo(node
, servname
, punixhints
, &unixaires
);
5783 TRACE("%s, %s %p -> %p %d\n", debugstr_a(nodename
), debugstr_a(servname
), hints
, res
, result
);
5784 HeapFree(GetProcessHeap(), 0, hostname
);
5787 struct addrinfo
*xuai
= unixaires
;
5788 struct WS_addrinfo
**xai
= res
;
5792 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
5798 *xai
= ai
;xai
= &ai
->ai_next
;
5799 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
5800 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
5801 /* copy whatever was sent in the hints */
5803 ai
->ai_socktype
= hints
->ai_socktype
;
5804 ai
->ai_protocol
= hints
->ai_protocol
;
5806 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
5807 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
5809 if (xuai
->ai_canonname
) {
5810 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
5811 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
5812 if (!ai
->ai_canonname
)
5814 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
5816 len
= xuai
->ai_addrlen
;
5817 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
5820 ai
->ai_addrlen
= len
;
5822 int winlen
= ai
->ai_addrlen
;
5824 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
5825 ai
->ai_addrlen
= winlen
;
5829 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
5832 ai
->ai_addrlen
= len
;
5834 xuai
= xuai
->ai_next
;
5836 freeaddrinfo(unixaires
);
5838 if (TRACE_ON(winsock
))
5840 struct WS_addrinfo
*ai
= *res
;
5843 TRACE("=> %p, flags %#x, family %d, type %d, protocol %d, len %ld, name %s, addr %s\n",
5844 ai
, ai
->ai_flags
, ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
, ai
->ai_addrlen
,
5845 ai
->ai_canonname
, debugstr_sockaddr(ai
->ai_addr
));
5850 result
= convert_eai_u2w(result
);
5852 SetLastError(result
);
5856 if (*res
) WS_freeaddrinfo(*res
);
5857 if (unixaires
) freeaddrinfo(unixaires
);
5858 return WSA_NOT_ENOUGH_MEMORY
;
5860 FIXME("getaddrinfo() failed, not found during buildtime.\n");
5865 static struct WS_addrinfoW
*addrinfo_AtoW(const struct WS_addrinfo
*ai
)
5867 struct WS_addrinfoW
*ret
;
5869 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfoW
)))) return NULL
;
5870 ret
->ai_flags
= ai
->ai_flags
;
5871 ret
->ai_family
= ai
->ai_family
;
5872 ret
->ai_socktype
= ai
->ai_socktype
;
5873 ret
->ai_protocol
= ai
->ai_protocol
;
5874 ret
->ai_addrlen
= ai
->ai_addrlen
;
5875 ret
->ai_canonname
= NULL
;
5876 ret
->ai_addr
= NULL
;
5877 ret
->ai_next
= NULL
;
5878 if (ai
->ai_canonname
)
5880 int len
= MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0);
5881 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
5883 HeapFree(GetProcessHeap(), 0, ret
);
5886 MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
);
5890 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, ai
->ai_addrlen
)))
5892 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
5893 HeapFree(GetProcessHeap(), 0, ret
);
5896 memcpy(ret
->ai_addr
, ai
->ai_addr
, ai
->ai_addrlen
);
5901 static struct WS_addrinfoW
*addrinfo_list_AtoW(const struct WS_addrinfo
*info
)
5903 struct WS_addrinfoW
*ret
, *infoW
;
5905 if (!(ret
= infoW
= addrinfo_AtoW(info
))) return NULL
;
5906 while (info
->ai_next
)
5908 if (!(infoW
->ai_next
= addrinfo_AtoW(info
->ai_next
)))
5913 infoW
= infoW
->ai_next
;
5914 info
= info
->ai_next
;
5919 static struct WS_addrinfo
*addrinfo_WtoA(const struct WS_addrinfoW
*ai
)
5921 struct WS_addrinfo
*ret
;
5923 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfo
)))) return NULL
;
5924 ret
->ai_flags
= ai
->ai_flags
;
5925 ret
->ai_family
= ai
->ai_family
;
5926 ret
->ai_socktype
= ai
->ai_socktype
;
5927 ret
->ai_protocol
= ai
->ai_protocol
;
5928 ret
->ai_addrlen
= ai
->ai_addrlen
;
5929 ret
->ai_canonname
= NULL
;
5930 ret
->ai_addr
= NULL
;
5931 ret
->ai_next
= NULL
;
5932 if (ai
->ai_canonname
)
5934 int len
= WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0, NULL
, NULL
);
5935 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
5937 HeapFree(GetProcessHeap(), 0, ret
);
5940 WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
, NULL
, NULL
);
5944 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
5946 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
5947 HeapFree(GetProcessHeap(), 0, ret
);
5950 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
5955 /***********************************************************************
5956 * GetAddrInfoW (WS2_32.@)
5958 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
5961 char *nodenameA
= NULL
, *servnameA
= NULL
;
5962 struct WS_addrinfo
*resA
, *hintsA
= NULL
;
5967 len
= WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, NULL
, 0, NULL
, NULL
);
5968 if (!(nodenameA
= HeapAlloc(GetProcessHeap(), 0, len
))) return EAI_MEMORY
;
5969 WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, nodenameA
, len
, NULL
, NULL
);
5973 len
= WideCharToMultiByte(CP_ACP
, 0, servname
, -1, NULL
, 0, NULL
, NULL
);
5974 if (!(servnameA
= HeapAlloc(GetProcessHeap(), 0, len
)))
5976 HeapFree(GetProcessHeap(), 0, nodenameA
);
5979 WideCharToMultiByte(CP_ACP
, 0, servname
, -1, servnameA
, len
, NULL
, NULL
);
5982 if (hints
) hintsA
= addrinfo_WtoA(hints
);
5983 ret
= WS_getaddrinfo(nodenameA
, servnameA
, hintsA
, &resA
);
5984 WS_freeaddrinfo(hintsA
);
5988 *res
= addrinfo_list_AtoW(resA
);
5989 WS_freeaddrinfo(resA
);
5992 HeapFree(GetProcessHeap(), 0, nodenameA
);
5993 HeapFree(GetProcessHeap(), 0, servnameA
);
5997 /***********************************************************************
5998 * FreeAddrInfoW (WS2_32.@)
6000 void WINAPI
FreeAddrInfoW(PADDRINFOW ai
)
6005 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
6006 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
6008 HeapFree(GetProcessHeap(), 0, ai
);
6013 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
6014 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
6016 #ifdef HAVE_GETNAMEINFO
6018 union generic_unix_sockaddr sa_u
;
6021 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
6022 serv
, servlen
, flags
);
6024 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
6027 SetLastError(WSAEFAULT
);
6028 return WSA_NOT_ENOUGH_MEMORY
;
6030 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
6031 return convert_eai_u2w(ret
);
6033 FIXME("getnameinfo() failed, not found during buildtime.\n");
6038 int WINAPI
GetNameInfoW(const SOCKADDR
*sa
, WS_socklen_t salen
, PWCHAR host
,
6039 DWORD hostlen
, PWCHAR serv
, DWORD servlen
, INT flags
)
6042 char *hostA
= NULL
, *servA
= NULL
;
6044 if (host
&& (!(hostA
= HeapAlloc(GetProcessHeap(), 0, hostlen
)))) return EAI_MEMORY
;
6045 if (serv
&& (!(servA
= HeapAlloc(GetProcessHeap(), 0, servlen
))))
6047 HeapFree(GetProcessHeap(), 0, hostA
);
6051 ret
= WS_getnameinfo(sa
, salen
, hostA
, hostlen
, servA
, servlen
, flags
);
6054 if (host
) MultiByteToWideChar(CP_ACP
, 0, hostA
, -1, host
, hostlen
);
6055 if (serv
) MultiByteToWideChar(CP_ACP
, 0, servA
, -1, serv
, servlen
);
6058 HeapFree(GetProcessHeap(), 0, hostA
);
6059 HeapFree(GetProcessHeap(), 0, servA
);
6063 /***********************************************************************
6064 * getservbyport (WS2_32.56)
6066 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
6068 struct WS_servent
* retval
= NULL
;
6069 #ifdef HAVE_GETSERVBYPORT
6070 struct servent
* serv
;
6071 char *proto_str
= NULL
;
6073 if (proto
&& *proto
)
6075 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
6077 EnterCriticalSection( &csWSgetXXXbyYYY
);
6078 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
6079 retval
= WS_dup_se(serv
);
6081 else SetLastError(WSANO_DATA
);
6082 LeaveCriticalSection( &csWSgetXXXbyYYY
);
6083 HeapFree( GetProcessHeap(), 0, proto_str
);
6085 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
6090 /***********************************************************************
6091 * gethostname (WS2_32.57)
6093 int WINAPI
WS_gethostname(char *name
, int namelen
)
6098 TRACE("name %p, len %d\n", name
, namelen
);
6102 SetLastError(WSAEFAULT
);
6103 return SOCKET_ERROR
;
6106 if (gethostname(buf
, sizeof(buf
)) != 0)
6108 SetLastError(wsaErrno());
6109 return SOCKET_ERROR
;
6112 TRACE("<- '%s'\n", buf
);
6115 WARN("Windows supports NetBIOS name length up to 15 bytes!\n");
6118 SetLastError(WSAEFAULT
);
6119 WARN("<- not enough space for hostname, required %d, got %d!\n", len
+ 1, namelen
);
6120 return SOCKET_ERROR
;
6127 /* ------------------------------------- Windows sockets extensions -- *
6129 * ------------------------------------------------------------------- */
6131 /***********************************************************************
6132 * WSAEnumNetworkEvents (WS2_32.36)
6134 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
6138 int errors
[FD_MAX_EVENTS
];
6140 TRACE("%04lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
6142 SERVER_START_REQ( get_socket_event
)
6144 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
6145 req
->service
= TRUE
;
6146 req
->c_event
= wine_server_obj_handle( hEvent
);
6147 wine_server_set_reply( req
, errors
, sizeof(errors
) );
6148 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
6153 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
6154 lpEvent
->iErrorCode
[i
] = NtStatusToWSAError(errors
[i
]);
6157 SetLastError(WSAEINVAL
);
6158 return SOCKET_ERROR
;
6161 /***********************************************************************
6162 * WSAEventSelect (WS2_32.39)
6164 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
6168 TRACE("%04lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
6170 SERVER_START_REQ( set_socket_event
)
6172 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
6174 req
->event
= wine_server_obj_handle( hEvent
);
6177 ret
= wine_server_call( req
);
6181 SetLastError(WSAEINVAL
);
6182 return SOCKET_ERROR
;
6185 /**********************************************************************
6186 * WSAGetOverlappedResult (WS2_32.40)
6188 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
6189 LPDWORD lpcbTransfer
, BOOL fWait
,
6194 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
6195 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
6197 if ( lpOverlapped
== NULL
)
6199 ERR( "Invalid pointer\n" );
6200 SetLastError(WSA_INVALID_PARAMETER
);
6204 status
= lpOverlapped
->Internal
;
6205 if (status
== STATUS_PENDING
)
6209 SetLastError( WSA_IO_INCOMPLETE
);
6213 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
6214 INFINITE
) == WAIT_FAILED
)
6216 status
= lpOverlapped
->Internal
;
6220 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
6223 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
6225 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
6230 /***********************************************************************
6231 * WSAAsyncSelect (WS2_32.101)
6233 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
6237 TRACE("%04lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
6239 SERVER_START_REQ( set_socket_event
)
6241 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
6244 req
->window
= wine_server_user_handle( hWnd
);
6246 ret
= wine_server_call( req
);
6250 SetLastError(WSAEINVAL
);
6251 return SOCKET_ERROR
;
6254 /***********************************************************************
6255 * WSACreateEvent (WS2_32.31)
6258 WSAEVENT WINAPI
WSACreateEvent(void)
6260 /* Create a manual-reset event, with initial state: unsignaled */
6263 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
6266 /***********************************************************************
6267 * WSACloseEvent (WS2_32.29)
6270 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
6272 TRACE ("event=%p\n", event
);
6274 return CloseHandle(event
);
6277 /***********************************************************************
6278 * WSASocketA (WS2_32.78)
6281 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
6282 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
6283 GROUP g
, DWORD dwFlags
)
6286 WSAPROTOCOL_INFOW info
;
6288 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
6289 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
6291 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
6293 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
6294 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
6295 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
6299 SetLastError(WSAEINVAL
);
6300 return SOCKET_ERROR
;
6303 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
6306 /***********************************************************************
6307 * WSASocketW (WS2_32.79)
6310 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
6311 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
6312 GROUP g
, DWORD dwFlags
)
6316 int unixaf
, unixtype
, ipxptype
= -1;
6319 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
6320 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
6323 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
6324 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
6328 err
= WSANOTINITIALISED
;
6332 /* hack for WSADuplicateSocket */
6333 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
6334 ret
= lpProtocolInfo
->dwServiceFlags3
;
6335 TRACE("\tgot duplicate %04lx\n", ret
);
6341 if (af
== FROM_PROTOCOL_INFO
|| !af
)
6342 af
= lpProtocolInfo
->iAddressFamily
;
6343 if (type
== FROM_PROTOCOL_INFO
|| !type
)
6344 type
= lpProtocolInfo
->iSocketType
;
6345 if (protocol
== FROM_PROTOCOL_INFO
|| !protocol
)
6346 protocol
= lpProtocolInfo
->iProtocol
;
6349 if (!type
&& (af
|| protocol
))
6351 int autoproto
= protocol
;
6352 WSAPROTOCOL_INFOW infow
;
6354 /* default to the first valid protocol */
6356 autoproto
= valid_protocols
[0];
6357 else if(IS_IPX_PROTO(autoproto
))
6358 autoproto
= WS_NSPROTO_IPX
;
6360 if (WS_EnterSingleProtocolW(autoproto
, &infow
))
6362 type
= infow
.iSocketType
;
6364 /* after win2003 it's no longer possible to pass AF_UNSPEC
6365 using the protocol info struct */
6366 if (!lpProtocolInfo
&& af
== WS_AF_UNSPEC
)
6367 af
= infow
.iAddressFamily
;
6372 Windows has an extension to the IPX protocol that allows one to create sockets
6373 and set the IPX packet type at the same time, to do that a caller will use
6374 a protocol like NSPROTO_IPX + <PACKET TYPE>
6376 if (IS_IPX_PROTO(protocol
))
6377 ipxptype
= protocol
- WS_NSPROTO_IPX
;
6379 /* convert the socket family, type and protocol */
6380 unixaf
= convert_af_w2u(af
);
6381 unixtype
= convert_socktype_w2u(type
);
6382 protocol
= convert_proto_w2u(protocol
);
6383 if (unixaf
== AF_UNSPEC
) unixaf
= -1;
6385 /* filter invalid parameters */
6388 /* the type could not be converted */
6389 if (type
&& unixtype
< 0)
6391 err
= WSAESOCKTNOSUPPORT
;
6395 err
= WSAEPROTONOSUPPORT
;
6400 /* both family and protocol can't be invalid */
6407 /* family could not be converted and neither socket type */
6408 if (unixtype
< 0 && af
>= 0)
6411 err
= WSAESOCKTNOSUPPORT
;
6415 err
= WSAEAFNOSUPPORT
;
6419 SERVER_START_REQ( create_socket
)
6421 req
->family
= unixaf
;
6422 req
->type
= unixtype
;
6423 req
->protocol
= protocol
;
6424 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
6425 req
->attributes
= (dwFlags
& WSA_FLAG_NO_HANDLE_INHERIT
) ? 0 : OBJ_INHERIT
;
6426 req
->flags
= dwFlags
& ~WSA_FLAG_NO_HANDLE_INHERIT
;
6427 set_error( wine_server_call( req
) );
6428 ret
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
6433 TRACE("\tcreated %04lx\n", ret
);
6435 set_ipx_packettype(ret
, ipxptype
);
6439 err
= GetLastError();
6440 if (err
== WSAEACCES
) /* raw socket denied */
6442 if (type
== SOCK_RAW
)
6443 ERR_(winediag
)("Failed to create a socket of type SOCK_RAW, this requires special permissions.\n");
6445 ERR_(winediag
)("Failed to create socket, this requires special permissions.\n");
6449 /* invalid combination of valid parameters, like SOCK_STREAM + IPPROTO_UDP */
6450 if (err
== WSAEINVAL
)
6451 err
= WSAESOCKTNOSUPPORT
;
6452 else if (err
== WSAEOPNOTSUPP
)
6453 err
= WSAEPROTONOSUPPORT
;
6457 WARN("\t\tfailed, error %d!\n", err
);
6459 return INVALID_SOCKET
;
6462 /***********************************************************************
6463 * WSAJoinLeaf (WS2_32.58)
6466 SOCKET WINAPI
WSAJoinLeaf(
6468 const struct WS_sockaddr
*addr
,
6470 LPWSABUF lpCallerData
,
6471 LPWSABUF lpCalleeData
,
6477 return INVALID_SOCKET
;
6480 /***********************************************************************
6481 * __WSAFDIsSet (WS2_32.151)
6483 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
6485 int i
= set
->fd_count
, ret
= 0;
6488 if (set
->fd_array
[i
] == s
)
6494 TRACE("(socket %04lx, fd_set %p, count %i) <- %d\n", s
, set
, set
->fd_count
, ret
);
6498 /***********************************************************************
6499 * WSAIsBlocking (WS2_32.114)
6501 BOOL WINAPI
WSAIsBlocking(void)
6503 /* By default WinSock should set all its sockets to non-blocking mode
6504 * and poll in PeekMessage loop when processing "blocking" ones. This
6505 * function is supposed to tell if the program is in this loop. Our
6506 * blocking calls are truly blocking so we always return FALSE.
6508 * Note: It is allowed to call this function without prior WSAStartup().
6515 /***********************************************************************
6516 * WSACancelBlockingCall (WS2_32.113)
6518 INT WINAPI
WSACancelBlockingCall(void)
6524 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
6526 FIXME("How was this called?\n");
6531 /***********************************************************************
6532 * WSASetBlockingHook (WS2_32.109)
6534 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
6536 FARPROC prev
= blocking_hook
;
6537 blocking_hook
= lpBlockFunc
;
6538 TRACE("hook %p\n", lpBlockFunc
);
6543 /***********************************************************************
6544 * WSAUnhookBlockingHook (WS2_32.110)
6546 INT WINAPI
WSAUnhookBlockingHook(void)
6548 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
6553 /* ----------------------------------- end of API stuff */
6555 /* ----------------------------------- helper functions -
6557 * TODO: Merge WS_dup_..() stuff into one function that
6558 * would operate with a generic structure containing internal
6559 * pointers (via a template of some kind).
6562 static int list_size(char** l
, int item_size
)
6567 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
6568 j
+= (i
+ 1) * sizeof(char*); }
6572 static int list_dup(char** l_src
, char** l_to
, int item_size
)
6577 for (i
= 0; l_src
[i
]; i
++) ;
6578 p
= (char *)(l_to
+ i
+ 1);
6579 for (i
= 0; l_src
[i
]; i
++)
6581 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
6582 memcpy(p
, l_src
[i
], count
);
6587 return p
- (char *)l_to
;
6592 /* create a hostent entry
6594 * Creates the entry with enough memory for the name, aliases
6595 * addresses, and the address pointers. Also copies the name
6596 * and sets up all the pointers.
6598 * NOTE: The alias and address lists must be allocated with room
6599 * for the NULL item terminating the list. This is true even if
6600 * the list has no items ("aliases" and "addresses" must be
6601 * at least "1", a truly empty list is invalid).
6603 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int aliases_size
, int addresses
, int address_length
)
6605 struct WS_hostent
*p_to
;
6607 int size
= (sizeof(struct WS_hostent
) +
6609 sizeof(char *) * aliases
+
6611 sizeof(char *) * addresses
+
6612 address_length
* (addresses
- 1)), i
;
6614 if (!(p_to
= check_buffer_he(size
))) return NULL
;
6615 memset(p_to
, 0, size
);
6617 /* Use the memory in the same way winsock does.
6618 * First set the pointer for aliases, second set the pointers for addresses.
6619 * Third fill the addresses indexes, fourth jump aliases names size.
6620 * Fifth fill the hostname.
6621 * NOTE: This method is valid for OS version's >= XP.
6623 p
= (char *)(p_to
+ 1);
6624 p_to
->h_aliases
= (char **)p
;
6625 p
+= sizeof(char *)*aliases
;
6627 p_to
->h_addr_list
= (char **)p
;
6628 p
+= sizeof(char *)*addresses
;
6630 for (i
= 0, addresses
--; i
< addresses
; i
++, p
+= address_length
)
6631 p_to
->h_addr_list
[i
] = p
;
6633 /* NOTE: h_aliases must be filled in manually because we don't know each string
6634 * size, leave these pointers NULL (already set to NULL by memset earlier).
6644 /* duplicate hostent entry
6645 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
6646 * Ditto for protoent and servent.
6648 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
6650 int i
, addresses
= 0, alias_size
= 0;
6651 struct WS_hostent
*p_to
;
6654 for( i
= 0; p_he
->h_aliases
[i
]; i
++) alias_size
+= strlen(p_he
->h_aliases
[i
]) + 1;
6655 while (p_he
->h_addr_list
[addresses
]) addresses
++;
6657 p_to
= WS_create_he(p_he
->h_name
, i
+ 1, alias_size
, addresses
+ 1, p_he
->h_length
);
6659 if (!p_to
) return NULL
;
6660 p_to
->h_addrtype
= convert_af_u2w(p_he
->h_addrtype
);
6661 p_to
->h_length
= p_he
->h_length
;
6663 for(i
= 0, p
= p_to
->h_addr_list
[0]; p_he
->h_addr_list
[i
]; i
++, p
+= p_to
->h_length
)
6664 memcpy(p
, p_he
->h_addr_list
[i
], p_to
->h_length
);
6666 /* Fill the aliases after the IP data */
6667 for(i
= 0; p_he
->h_aliases
[i
]; i
++)
6669 p_to
->h_aliases
[i
] = p
;
6670 strcpy(p
, p_he
->h_aliases
[i
]);
6677 /* ----- protoent */
6679 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
)
6682 struct WS_protoent
*p_to
;
6684 int size
= (sizeof(*p_pe
) +
6685 strlen(p_pe
->p_name
) + 1 +
6686 list_size(p_pe
->p_aliases
, 0));
6688 if (!(p_to
= check_buffer_pe(size
))) return NULL
;
6689 p_to
->p_proto
= p_pe
->p_proto
;
6691 p
= (char *)(p_to
+ 1);
6693 strcpy(p
, p_pe
->p_name
);
6696 p_to
->p_aliases
= (char **)p
;
6697 list_dup(p_pe
->p_aliases
, p_to
->p_aliases
, 0);
6703 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
6706 struct WS_servent
*p_to
;
6708 int size
= (sizeof(*p_se
) +
6709 strlen(p_se
->s_proto
) + 1 +
6710 strlen(p_se
->s_name
) + 1 +
6711 list_size(p_se
->s_aliases
, 0));
6713 if (!(p_to
= check_buffer_se(size
))) return NULL
;
6714 p_to
->s_port
= p_se
->s_port
;
6716 p
= (char *)(p_to
+ 1);
6718 strcpy(p
, p_se
->s_name
);
6722 strcpy(p
, p_se
->s_proto
);
6725 p_to
->s_aliases
= (char **)p
;
6726 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
6731 /***********************************************************************
6732 * WSARecv (WS2_32.67)
6734 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
6735 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
6736 LPWSAOVERLAPPED lpOverlapped
,
6737 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
6739 return WS2_recv_base(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
6740 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
, NULL
);
6743 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
6744 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
6745 struct WS_sockaddr
*lpFrom
,
6746 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
6747 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
6748 LPWSABUF lpControlBuffer
)
6750 unsigned int i
, options
;
6751 int n
, fd
, err
, overlapped
, flags
;
6752 struct ws2_async
*wsa
= NULL
, localwsa
;
6754 DWORD timeout_start
= GetTickCount();
6755 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
6757 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
6758 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
6759 (lpFromlen
? *lpFromlen
: -1),
6760 lpOverlapped
, lpCompletionRoutine
);
6762 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
6763 TRACE( "fd=%d, options=%x\n", fd
, options
);
6765 if (fd
== -1) return SOCKET_ERROR
;
6767 if (*lpFlags
& WS_MSG_OOB
)
6769 /* It's invalid to receive OOB data from an OOBINLINED socket
6770 * as OOB data is turned into normal data. */
6772 if (!getsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, (char*) &n
, &i
) && n
)
6779 overlapped
= (lpOverlapped
|| lpCompletionRoutine
) &&
6780 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
6781 if (overlapped
|| dwBufferCount
> 1)
6783 if (!(wsa
= (struct ws2_async
*)alloc_async_io( offsetof(struct ws2_async
, iovec
[dwBufferCount
]))))
6792 wsa
->hSocket
= SOCKET2HANDLE(s
);
6793 wsa
->flags
= *lpFlags
;
6794 wsa
->lpFlags
= lpFlags
;
6796 wsa
->addrlen
.ptr
= lpFromlen
;
6797 wsa
->control
= lpControlBuffer
;
6798 wsa
->n_iovecs
= dwBufferCount
;
6799 wsa
->first_iovec
= 0;
6800 for (i
= 0; i
< dwBufferCount
; i
++)
6802 /* check buffer first to trigger write watches */
6803 if (IsBadWritePtr( lpBuffers
[i
].buf
, lpBuffers
[i
].len
))
6808 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
6809 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
6812 flags
= convert_flags(wsa
->flags
);
6815 n
= WS2_recv( fd
, wsa
, flags
);
6818 if (errno
!= EAGAIN
)
6820 int loc_errno
= errno
;
6822 if (cvalue
) WS_AddCompletion( s
, cvalue
, sock_get_ntstatus(loc_errno
), 0 );
6826 else if (lpNumberOfBytesRecvd
) *lpNumberOfBytesRecvd
= n
;
6830 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
6832 wsa
->user_overlapped
= lpOverlapped
;
6833 wsa
->completion_func
= lpCompletionRoutine
;
6834 release_sock_fd( s
, fd
);
6838 iosb
->u
.Status
= STATUS_PENDING
;
6839 iosb
->Information
= 0;
6841 SERVER_START_REQ( register_async
)
6843 req
->type
= ASYNC_TYPE_READ
;
6844 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
6845 req
->async
.callback
= wine_server_client_ptr( WS2_async_recv
);
6846 req
->async
.iosb
= wine_server_client_ptr( iosb
);
6847 req
->async
.arg
= wine_server_client_ptr( wsa
);
6848 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
6849 req
->async
.cvalue
= cvalue
;
6850 err
= wine_server_call( req
);
6854 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
6855 SetLastError(NtStatusToWSAError( err
));
6856 return SOCKET_ERROR
;
6859 iosb
->u
.Status
= STATUS_SUCCESS
;
6860 iosb
->Information
= n
;
6861 if (!wsa
->completion_func
)
6863 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
6864 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
6865 HeapFree( GetProcessHeap(), 0, wsa
);
6867 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
6868 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
6869 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
6875 if ((err
= _is_blocking( s
, &is_blocking
)))
6877 err
= NtStatusToWSAError( err
);
6884 int timeout
= GET_RCVTIMEO(fd
);
6887 timeout
-= GetTickCount() - timeout_start
;
6888 if (timeout
< 0) timeout
= 0;
6892 pfd
.events
= POLLIN
;
6893 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
6895 if (!timeout
|| !poll( &pfd
, 1, timeout
))
6898 /* a timeout is not fatal */
6899 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
6905 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
6906 err
= WSAEWOULDBLOCK
;
6911 TRACE(" -> %i bytes\n", n
);
6912 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
6913 release_sock_fd( s
, fd
);
6914 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
6915 SetLastError(ERROR_SUCCESS
);
6920 if (wsa
!= &localwsa
) HeapFree( GetProcessHeap(), 0, wsa
);
6921 release_sock_fd( s
, fd
);
6922 WARN(" -> ERROR %d\n", err
);
6923 SetLastError( err
);
6924 return SOCKET_ERROR
;
6927 /***********************************************************************
6928 * WSARecvFrom (WS2_32.69)
6930 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
6931 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
6932 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
6933 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
6936 return WS2_recv_base( s
, lpBuffers
, dwBufferCount
,
6937 lpNumberOfBytesRecvd
, lpFlags
,
6939 lpOverlapped
, lpCompletionRoutine
, NULL
);
6942 /***********************************************************************
6943 * WSCInstallProvider (WS2_32.88)
6945 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
6946 LPCWSTR lpszProviderDllPath
,
6947 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
6948 DWORD dwNumberOfEntries
,
6951 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
6952 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
6953 dwNumberOfEntries
, lpErrno
);
6959 /***********************************************************************
6960 * WSCDeinstallProvider (WS2_32.83)
6962 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
6964 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
6970 /***********************************************************************
6971 * WSAAccept (WS2_32.26)
6973 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
6974 LPCONDITIONPROC lpfnCondition
, DWORD_PTR dwCallbackData
)
6978 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
6979 /* QOS SQOS, GQOS; */
6982 SOCKADDR src_addr
, dst_addr
;
6984 TRACE("socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %ld\n",
6985 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
6987 cs
= WS_accept(s
, addr
, addrlen
);
6988 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
6989 if (!lpfnCondition
) return cs
;
6991 if (addr
&& addrlen
)
6993 CallerId
.buf
= (char *)addr
;
6994 CallerId
.len
= *addrlen
;
6998 size
= sizeof(src_addr
);
6999 WS_getpeername(cs
, &src_addr
, &size
);
7000 CallerId
.buf
= (char *)&src_addr
;
7001 CallerId
.len
= size
;
7003 CallerData
.buf
= NULL
;
7006 size
= sizeof(dst_addr
);
7007 WS_getsockname(cs
, &dst_addr
, &size
);
7009 CalleeId
.buf
= (char *)&dst_addr
;
7010 CalleeId
.len
= sizeof(dst_addr
);
7012 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
7013 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
7020 SERVER_START_REQ( set_socket_deferred
)
7022 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
7023 req
->deferred
= wine_server_obj_handle( SOCKET2HANDLE(cs
) );
7024 if ( !wine_server_call_err ( req
) )
7026 SetLastError( WSATRY_AGAIN
);
7027 WS_closesocket( cs
);
7031 return SOCKET_ERROR
;
7034 SetLastError(WSAECONNREFUSED
);
7035 return SOCKET_ERROR
;
7037 FIXME("Unknown return type from Condition function\n");
7038 SetLastError(WSAENOTSOCK
);
7039 return SOCKET_ERROR
;
7043 /***********************************************************************
7044 * WSADuplicateSocketA (WS2_32.32)
7046 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
7048 return WS_DuplicateSocket(FALSE
, s
, dwProcessId
, (LPWSAPROTOCOL_INFOW
) lpProtocolInfo
);
7051 /***********************************************************************
7052 * WSADuplicateSocketW (WS2_32.33)
7054 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
7056 return WS_DuplicateSocket(TRUE
, s
, dwProcessId
, lpProtocolInfo
);
7059 /***********************************************************************
7060 * WSAInstallServiceClassA (WS2_32.48)
7062 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
7064 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
7065 SetLastError(WSAEACCES
);
7066 return SOCKET_ERROR
;
7069 /***********************************************************************
7070 * WSAInstallServiceClassW (WS2_32.49)
7072 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
7074 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
7075 SetLastError(WSAEACCES
);
7076 return SOCKET_ERROR
;
7079 /***********************************************************************
7080 * WSARemoveServiceClass (WS2_32.70)
7082 int WINAPI
WSARemoveServiceClass(LPGUID info
)
7084 FIXME("Request to remove service %p\n",info
);
7085 SetLastError(WSATYPE_NOT_FOUND
);
7086 return SOCKET_ERROR
;
7089 /***********************************************************************
7090 * inet_ntop (WS2_32.@)
7092 PCSTR WINAPI
WS_inet_ntop( INT family
, PVOID addr
, PSTR buffer
, SIZE_T len
)
7094 #ifdef HAVE_INET_NTOP
7095 struct WS_in6_addr
*in6
;
7096 struct WS_in_addr
*in
;
7099 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
7102 SetLastError( STATUS_INVALID_PARAMETER
);
7111 pdst
= inet_ntop( AF_INET
, &in
->WS_s_addr
, buffer
, len
);
7117 pdst
= inet_ntop( AF_INET6
, in6
->WS_s6_addr
, buffer
, len
);
7121 SetLastError( WSAEAFNOSUPPORT
);
7125 if (!pdst
) SetLastError( STATUS_INVALID_PARAMETER
);
7128 FIXME( "not supported on this platform\n" );
7129 SetLastError( WSAEAFNOSUPPORT
);
7134 /***********************************************************************
7135 * inet_pton (WS2_32.@)
7137 INT WINAPI
WS_inet_pton( INT family
, PCSTR addr
, PVOID buffer
)
7139 #ifdef HAVE_INET_PTON
7142 TRACE("family %d, addr '%s', buffer (%p)\n", family
, addr
? addr
: "(null)", buffer
);
7144 if (!addr
|| !buffer
)
7146 SetLastError(WSAEFAULT
);
7147 return SOCKET_ERROR
;
7150 unixaf
= convert_af_w2u(family
);
7151 if (unixaf
!= AF_INET
&& unixaf
!= AF_INET6
)
7153 SetLastError(WSAEAFNOSUPPORT
);
7154 return SOCKET_ERROR
;
7157 ret
= inet_pton(unixaf
, addr
, buffer
);
7158 if (ret
== -1) SetLastError(wsaErrno());
7161 FIXME( "not supported on this platform\n" );
7162 SetLastError( WSAEAFNOSUPPORT
);
7163 return SOCKET_ERROR
;
7168 /***********************************************************************
7169 * WSAStringToAddressA (WS2_32.80)
7171 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
7173 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
7174 LPSOCKADDR lpAddress
,
7175 LPINT lpAddressLength
)
7178 LPSTR workBuffer
=NULL
,ptrPort
;
7180 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_a(AddressString
), AddressFamily
,
7181 lpProtocolInfo
, lpAddress
, lpAddressLength
);
7183 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
7187 SetLastError(WSAEINVAL
);
7188 return SOCKET_ERROR
;
7192 FIXME("ProtocolInfo not implemented.\n");
7194 workBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
7195 strlen(AddressString
) + 1);
7198 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
7199 return SOCKET_ERROR
;
7202 strcpy(workBuffer
, AddressString
);
7204 switch(AddressFamily
)
7208 struct in_addr inetaddr
;
7210 /* If lpAddressLength is too small, tell caller the size we need */
7211 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
7213 *lpAddressLength
= sizeof(SOCKADDR_IN
);
7217 *lpAddressLength
= sizeof(SOCKADDR_IN
);
7218 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
7220 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= WS_AF_INET
;
7222 ptrPort
= strchr(workBuffer
, ':');
7225 /* User may have entered an IPv6 and asked to parse as IPv4 */
7226 if(strchr(ptrPort
+ 1, ':'))
7231 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= htons(atoi(ptrPort
+1));
7236 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= 0;
7239 if(inet_aton(workBuffer
, &inetaddr
) > 0)
7241 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
7252 struct in6_addr inetaddr
;
7253 /* If lpAddressLength is too small, tell caller the size we need */
7254 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
7256 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
7260 #ifdef HAVE_INET_PTON
7261 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
7262 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
7264 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_family
= WS_AF_INET6
;
7266 /* This one is a bit tricky. An IPv6 address contains colons, so the
7267 * check from IPv4 doesn't work like that. However, IPv6 addresses that
7268 * contain a port are written with braces like [fd12:3456:7890::1]:12345
7269 * so what we will do is to look for ']', check if the next char is a
7270 * colon, and if it is, parse the port as in IPv4. */
7272 ptrPort
= strchr(workBuffer
, ']');
7273 if(ptrPort
&& *(++ptrPort
) == ':')
7275 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= htons(atoi(ptrPort
+1));
7280 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= 0;
7283 if(inet_pton(AF_INET6
, workBuffer
, &inetaddr
) > 0)
7285 memcpy(&((LPSOCKADDR_IN6
)lpAddress
)->sin6_addr
, &inetaddr
,
7286 sizeof(struct in6_addr
));
7290 #endif /* HAVE_INET_PTON */
7296 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
7297 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
7301 HeapFree(GetProcessHeap(), 0, workBuffer
);
7305 return SOCKET_ERROR
;
7308 /***********************************************************************
7309 * WSAStringToAddressW (WS2_32.81)
7311 * FIXME: Does anybody know if this function allows using Hebrew/Arabic/Chinese... digits?
7312 * If this should be the case, it would be required to map these digits
7313 * to Unicode digits (0-9) using FoldString first.
7315 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
7317 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
7318 LPSOCKADDR lpAddress
,
7319 LPINT lpAddressLength
)
7322 LPSTR workBuffer
=NULL
;
7323 WSAPROTOCOL_INFOA infoA
;
7324 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
7326 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
7327 lpAddress
, lpAddressLength
);
7329 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
7331 /* if ProtocolInfo is available - convert to ANSI variant */
7334 lpProtoInfoA
= &infoA
;
7335 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
7337 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
7338 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
7340 SetLastError(WSAEINVAL
);
7341 return SOCKET_ERROR
;
7347 /* Translate AddressString to ANSI code page - assumes that only
7348 standard digits 0-9 are used with this API call */
7349 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
7350 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
7354 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
7355 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
7356 lpAddress
,lpAddressLength
);
7357 HeapFree( GetProcessHeap(), 0, workBuffer
);
7361 res
= WSA_NOT_ENOUGH_MEMORY
;
7367 return SOCKET_ERROR
;
7370 /***********************************************************************
7371 * WSAAddressToStringA (WS2_32.27)
7373 * See WSAAddressToStringW
7375 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
7376 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
7380 CHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
7383 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
7385 if (!sockaddr
) return SOCKET_ERROR
;
7386 if (!string
|| !lenstr
) return SOCKET_ERROR
;
7388 switch(sockaddr
->sa_family
)
7391 if (len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
7392 sprintf( buffer
, "%u.%u.%u.%u:%u",
7393 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
7394 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
7395 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
7396 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
7397 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
7399 p
= strchr( buffer
, ':' );
7400 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
7405 struct WS_sockaddr_in6
*sockaddr6
= (LPSOCKADDR_IN6
) sockaddr
;
7408 if (len
< sizeof(SOCKADDR_IN6
)) return SOCKET_ERROR
;
7409 if ((sockaddr6
->sin6_port
))
7410 strcpy(buffer
, "[");
7411 if (!WS_inet_ntop(WS_AF_INET6
, &sockaddr6
->sin6_addr
, buffer
+strlen(buffer
), sizeof(buffer
)))
7413 SetLastError(WSAEINVAL
);
7414 return SOCKET_ERROR
;
7416 if ((sockaddr6
->sin6_scope_id
))
7417 sprintf(buffer
+strlen(buffer
), "%%%u", sockaddr6
->sin6_scope_id
);
7418 if ((sockaddr6
->sin6_port
))
7419 sprintf(buffer
+strlen(buffer
), "]:%u", ntohs(sockaddr6
->sin6_port
));
7424 SetLastError(WSAEINVAL
);
7425 return SOCKET_ERROR
;
7428 size
= strlen( buffer
) + 1;
7433 SetLastError(WSAEFAULT
);
7434 return SOCKET_ERROR
;
7437 TRACE("=> %s,%u bytes\n", debugstr_a(buffer
), size
);
7439 strcpy( string
, buffer
);
7443 /***********************************************************************
7444 * WSAAddressToStringW (WS2_32.28)
7446 * Convert a sockaddr address into a readable address string.
7449 * sockaddr [I] Pointer to a sockaddr structure.
7450 * len [I] Size of the sockaddr structure.
7451 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
7452 * string [I/O] Pointer to a buffer to receive the address string.
7453 * lenstr [I/O] Size of the receive buffer in WCHARs.
7457 * Failure: SOCKET_ERROR
7460 * The 'info' parameter is ignored.
7462 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
7463 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
7468 WCHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
7471 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
7474 ret
= WSAAddressToStringA(sockaddr
, len
, NULL
, bufAddr
, &size
);
7476 if (ret
) return ret
;
7478 MultiByteToWideChar( CP_ACP
, 0, bufAddr
, size
, buffer
, sizeof( buffer
)/sizeof(WCHAR
));
7483 SetLastError(WSAEFAULT
);
7484 return SOCKET_ERROR
;
7487 TRACE("=> %s,%u bytes\n", debugstr_w(buffer
), size
);
7489 lstrcpyW( string
, buffer
);
7493 /***********************************************************************
7494 * WSAEnumNameSpaceProvidersA (WS2_32.34)
7496 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
7498 FIXME( "(%p %p) Stub!\n", len
, buffer
);
7502 /***********************************************************************
7503 * WSAEnumNameSpaceProvidersW (WS2_32.35)
7505 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
7507 FIXME( "(%p %p) Stub!\n", len
, buffer
);
7511 /***********************************************************************
7512 * WSAGetQOSByName (WS2_32.41)
7514 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
7516 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
7520 /***********************************************************************
7521 * WSAGetServiceClassInfoA (WS2_32.42)
7523 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
7524 LPWSASERVICECLASSINFOA info
)
7526 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
7528 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
7529 return SOCKET_ERROR
;
7532 /***********************************************************************
7533 * WSAGetServiceClassInfoW (WS2_32.43)
7535 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
7536 LPWSASERVICECLASSINFOW info
)
7538 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
7540 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
7541 return SOCKET_ERROR
;
7544 /***********************************************************************
7545 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
7547 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
7549 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
7550 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
7551 return SOCKET_ERROR
;
7554 /***********************************************************************
7555 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
7557 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
7559 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
7560 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
7561 return SOCKET_ERROR
;
7564 /***********************************************************************
7565 * WSALookupServiceBeginA (WS2_32.59)
7567 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
7568 DWORD dwControlFlags
,
7571 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
7573 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
7574 return SOCKET_ERROR
;
7577 /***********************************************************************
7578 * WSALookupServiceBeginW (WS2_32.60)
7580 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
7581 DWORD dwControlFlags
,
7584 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
7586 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
7587 return SOCKET_ERROR
;
7590 /***********************************************************************
7591 * WSALookupServiceEnd (WS2_32.61)
7593 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
7595 FIXME("(%p) Stub!\n", lookup
);
7599 /***********************************************************************
7600 * WSALookupServiceNextA (WS2_32.62)
7602 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
7604 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
7605 SetLastError(WSA_E_NO_MORE
);
7606 return SOCKET_ERROR
;
7609 /***********************************************************************
7610 * WSALookupServiceNextW (WS2_32.63)
7612 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
7614 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
7615 SetLastError(WSA_E_NO_MORE
);
7616 return SOCKET_ERROR
;
7619 /***********************************************************************
7620 * WSANtohl (WS2_32.64)
7622 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
7624 TRACE( "(%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
7626 if (!lphostlong
) return WSAEFAULT
;
7628 *lphostlong
= ntohl( netlong
);
7632 /***********************************************************************
7633 * WSANtohs (WS2_32.65)
7635 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
7637 TRACE( "(%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
7639 if (!lphostshort
) return WSAEFAULT
;
7641 *lphostshort
= ntohs( netshort
);
7645 /***********************************************************************
7646 * WSAProviderConfigChange (WS2_32.66)
7648 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
7649 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
7651 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
7652 return SOCKET_ERROR
;
7655 /***********************************************************************
7656 * WSARecvDisconnect (WS2_32.68)
7658 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
7660 TRACE( "(%04lx %p)\n", s
, disconnectdata
);
7662 return WS_shutdown( s
, SD_RECEIVE
);
7665 /***********************************************************************
7666 * WSASetServiceA (WS2_32.76)
7668 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
7670 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
7674 /***********************************************************************
7675 * WSASetServiceW (WS2_32.77)
7677 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
7679 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
7683 /***********************************************************************
7684 * WSCEnableNSProvider (WS2_32.84)
7686 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
7688 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
7692 /***********************************************************************
7693 * WSCGetProviderPath (WS2_32.86)
7695 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
7697 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
7699 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
7701 *errcode
= WSAEINVAL
;
7702 return SOCKET_ERROR
;
7705 /***********************************************************************
7706 * WSCInstallNameSpace (WS2_32.87)
7708 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
7709 DWORD version
, LPGUID provider
)
7711 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
7712 namespace, version
, debugstr_guid(provider
) );
7716 /***********************************************************************
7717 * WSCUnInstallNameSpace (WS2_32.89)
7719 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
7721 FIXME("(%p) Stub!\n", lpProviderId
);
7725 /***********************************************************************
7726 * WSCWriteProviderOrder (WS2_32.91)
7728 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
7730 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);
7734 /***********************************************************************
7735 * WSANSPIoctl (WS2_32.91)
7737 INT WINAPI
WSANSPIoctl( HANDLE hLookup
, DWORD dwControlCode
, LPVOID lpvInBuffer
,
7738 DWORD cbInBuffer
, LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
7739 LPDWORD lpcbBytesReturned
, LPWSACOMPLETION lpCompletion
)
7741 FIXME("(%p, 0x%08x, %p, 0x%08x, %p, 0x%08x, %p, %p) Stub!\n", hLookup
, dwControlCode
,
7742 lpvInBuffer
, cbInBuffer
, lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpCompletion
);
7743 SetLastError(WSA_NOT_ENOUGH_MEMORY
);
7744 return SOCKET_ERROR
;
7747 /*****************************************************************************
7748 * WSAEnumProtocolsA [WS2_32.@]
7750 * see function WSAEnumProtocolsW
7752 INT WINAPI
WSAEnumProtocolsA( LPINT protocols
, LPWSAPROTOCOL_INFOA buffer
, LPDWORD len
)
7754 return WS_EnumProtocols( FALSE
, protocols
, (LPWSAPROTOCOL_INFOW
) buffer
, len
);
7757 /*****************************************************************************
7758 * WSAEnumProtocolsW [WS2_32.@]
7760 * Retrieves information about specified set of active network protocols.
7763 * protocols [I] Pointer to null-terminated array of protocol id's. NULL
7764 * retrieves information on all available protocols.
7765 * buffer [I] Pointer to a buffer to be filled with WSAPROTOCOL_INFO
7767 * len [I/O] Pointer to a variable specifying buffer size. On output
7768 * the variable holds the number of bytes needed when the
7769 * specified size is too small.
7772 * Success: number of WSAPROTOCOL_INFO structures in buffer.
7773 * Failure: SOCKET_ERROR
7776 * NT4SP5 does not return SPX if protocols == NULL
7779 * - NT4SP5 returns in addition these list of NETBIOS protocols
7780 * (address family 17), each entry two times one for socket type 2 and 5
7782 * iProtocol szProtocol
7783 * 0x80000000 \Device\NwlnkNb
7784 * 0xfffffffa \Device\NetBT_CBENT7
7785 * 0xfffffffb \Device\Nbf_CBENT7
7786 * 0xfffffffc \Device\NetBT_NdisWan5
7787 * 0xfffffffd \Device\NetBT_El9202
7788 * 0xfffffffe \Device\Nbf_El9202
7789 * 0xffffffff \Device\Nbf_NdisWan4
7791 * - there is no check that the operating system supports the returned
7794 INT WINAPI
WSAEnumProtocolsW( LPINT protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
)
7796 return WS_EnumProtocols( TRUE
, protocols
, buffer
, len
);
7799 /*****************************************************************************
7800 * WSCEnumProtocols [WS2_32.@]
7803 * protocols [I] Null-terminated array of iProtocol values.
7804 * buffer [O] Buffer of WSAPROTOCOL_INFOW structures.
7805 * len [I/O] Size of buffer on input/output.
7806 * errno [O] Error code.
7809 * Success: number of protocols to be reported on.
7810 * Failure: SOCKET_ERROR. error is in errno.
7813 * Doesn't supply info on layered protocols.
7816 INT WINAPI
WSCEnumProtocols( LPINT protocols
, LPWSAPROTOCOL_INFOW buffer
, LPDWORD len
, LPINT err
)
7818 INT ret
= WSAEnumProtocolsW( protocols
, buffer
, len
);
7820 if (ret
== SOCKET_ERROR
) *err
= WSAENOBUFS
;