2 * based on Windows Sockets 1.1 specs
4 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
5 * Copyright (C) 2005 Marcus Meissner
6 * Copyright (C) 2006-2008 Kai Blin
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * NOTE: If you make any changes to fix a particular app, make sure
23 * they don't break something else like Netscape or telnet and ftp
24 * clients and servers (www.winsite.com got a lot of those).
28 #include "wine/port.h"
33 #include <sys/types.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 # include <sys/ioctl.h>
40 #ifdef HAVE_SYS_FILIO_H
41 # include <sys/filio.h>
43 #ifdef HAVE_SYS_SOCKIO_H
44 # include <sys/sockio.h>
48 # include <sys/so_ioctl.h>
51 #ifdef HAVE_SYS_PARAM_H
52 # include <sys/param.h>
58 #ifdef HAVE_SYS_WAIT_H
59 # include <sys/wait.h>
64 #ifdef HAVE_SYS_SOCKET_H
65 #include <sys/socket.h>
67 #ifdef HAVE_NETINET_IN_H
68 # include <netinet/in.h>
70 #ifdef HAVE_NETINET_TCP_H
71 # include <netinet/tcp.h>
73 #ifdef HAVE_ARPA_INET_H
74 # include <arpa/inet.h>
86 #ifdef HAVE_ARPA_NAMESER_H
87 # include <arpa/nameser.h>
95 #ifdef HAVE_LINUX_FILTER_H
96 # include <linux/filter.h>
99 #ifdef HAVE_NETIPX_IPX_H
100 # include <netipx/ipx.h>
102 #elif defined(HAVE_LINUX_IPX_H)
103 # ifdef HAVE_ASM_TYPES_H
104 # include <asm/types.h>
106 # ifdef HAVE_LINUX_TYPES_H
107 # include <linux/types.h>
109 # include <linux/ipx.h>
113 #ifdef HAVE_LINUX_IRDA_H
114 # ifdef HAVE_LINUX_TYPES_H
115 # include <linux/types.h>
117 # include <linux/irda.h>
124 #ifdef HAVE_SYS_POLL_H
125 # include <sys/poll.h>
127 #ifdef HAVE_SYS_TIME_H
128 # include <sys/time.h>
131 #define NONAMELESSUNION
132 #define NONAMELESSSTRUCT
133 #include "ntstatus.h"
134 #define WIN32_NO_STATUS
139 #include "winerror.h"
141 #include "winsock2.h"
143 #include "ws2tcpip.h"
149 #define USE_WC_PREFIX /* For CMSG_DATA */
150 #include "iphlpapi.h"
151 #include "wine/server.h"
152 #include "wine/debug.h"
153 #include "wine/exception.h"
154 #include "wine/unicode.h"
157 # include "wsnwlink.h"
160 #if defined(linux) && !defined(IP_UNICAST_IF)
161 #define IP_UNICAST_IF 50
164 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
165 # define sipx_network sipx_addr.x_net
166 # define sipx_node sipx_addr.x_host.c_host
167 #endif /* __FreeBSD__ */
170 #define INADDR_NONE ~0UL
173 WINE_DEFAULT_DEBUG_CHANNEL(winsock
);
174 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
176 #if defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER)
177 # define LINUX_BOUND_IF
178 struct interface_filter
{
179 struct sock_filter iface_memaddr
;
180 struct sock_filter iface_rule
;
181 struct sock_filter return_keep
;
182 struct sock_filter return_dump
;
184 # define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
185 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
186 /sizeof(struct sock_filter)
187 # define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
188 -offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
189 /sizeof(struct sock_filter)
190 static struct interface_filter generic_interface_filter
= {
191 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
, SKF_AD_OFF
+SKF_AD_IFINDEX
),
192 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule
), FILTER_JUMP_DUMP(iface_rule
)),
193 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1), /* keep packet */
194 BPF_STMT(BPF_RET
+BPF_K
, 0) /* dump packet */
196 #endif /* LINUX_BOUND_IF */
199 * The actual definition of WSASendTo, wrapped in a different function name
200 * so that internal calls from ws2_32 itself will not trigger programs like
201 * Garena, which hooks WSASendTo/WSARecvFrom calls.
203 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
204 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
205 const struct WS_sockaddr
*to
, int tolen
,
206 LPWSAOVERLAPPED lpOverlapped
,
207 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
210 * Internal fundamental receive function, essentially WSARecvFrom with an
211 * additional parameter to support message control headers.
213 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
214 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
215 struct WS_sockaddr
*lpFrom
,
216 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
217 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
218 LPWSABUF lpControlBuffer
);
220 /* critical section to protect some non-reentrant net function */
221 static CRITICAL_SECTION csWSgetXXXbyYYY
;
222 static CRITICAL_SECTION_DEBUG critsect_debug
=
224 0, 0, &csWSgetXXXbyYYY
,
225 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
226 0, 0, { (DWORD_PTR
)(__FILE__
": csWSgetXXXbyYYY") }
228 static CRITICAL_SECTION csWSgetXXXbyYYY
= { &critsect_debug
, -1, 0, 0, 0, 0 };
230 union generic_unix_sockaddr
232 struct sockaddr addr
;
233 char data
[128]; /* should be big enough for all families */
236 static inline const char *debugstr_sockaddr( const struct WS_sockaddr
*a
)
238 if (!a
) return "(nil)";
239 switch (a
->sa_family
)
242 return wine_dbg_sprintf("{ family AF_INET, address %s, port %d }",
243 inet_ntoa(((const struct sockaddr_in
*)a
)->sin_addr
),
244 ntohs(((const struct sockaddr_in
*)a
)->sin_port
));
249 struct WS_sockaddr_in6
*sin
= (struct WS_sockaddr_in6
*)a
;
251 p
= WS_inet_ntop( WS_AF_INET6
, &sin
->sin6_addr
, buf
, sizeof(buf
) );
253 p
= "(unknown IPv6 address)";
254 return wine_dbg_sprintf("{ family AF_INET6, address %s, port %d }",
255 p
, ntohs(sin
->sin6_port
));
261 memcpy( &addr
, ((const SOCKADDR_IRDA
*)a
)->irdaDeviceID
, sizeof(addr
) );
262 addr
= ntohl( addr
);
263 return wine_dbg_sprintf("{ family AF_IRDA, addr %08x, name %s }",
265 ((const SOCKADDR_IRDA
*)a
)->irdaServiceName
);
268 return wine_dbg_sprintf("{ family %d }", a
->sa_family
);
272 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
273 #define SOCKET2HANDLE(s) ((HANDLE)(s))
274 #define HANDLE2SOCKET(h) ((SOCKET)(h))
276 /****************************************************************
277 * Async IO declarations
278 ****************************************************************/
280 typedef struct ws2_async
284 LPWSAOVERLAPPED user_overlapped
;
285 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func
;
286 IO_STATUS_BLOCK local_iosb
;
287 struct WS_sockaddr
*addr
;
290 int val
; /* for send operations */
291 int *ptr
; /* for recv operations */
296 unsigned int n_iovecs
;
297 unsigned int first_iovec
;
298 struct iovec iovec
[1];
301 typedef struct ws2_accept_async
303 HANDLE listen_socket
;
304 HANDLE accept_socket
;
305 LPOVERLAPPED user_overlapped
;
307 PVOID buf
; /* buffer to write data to */
311 struct ws2_async
*read
;
314 /****************************************************************/
316 /* ----------------------------------- internal data */
318 /* ws_... struct conversion flags */
320 typedef struct /* WSAAsyncSelect() control struct */
322 HANDLE service
, event
, sock
;
328 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
329 #define WS_MAX_UDP_DATAGRAM 1024
330 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
332 /* hostent's, servent's and protent's are stored in one buffer per thread,
333 * as documented on MSDN for the functions that return any of the buffers */
334 struct per_thread_data
337 struct WS_hostent
*he_buffer
;
338 struct WS_servent
*se_buffer
;
339 struct WS_protoent
*pe_buffer
;
345 /* internal: routing description information */
352 static INT num_startup
; /* reference counter */
353 static FARPROC blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
355 /* function prototypes */
356 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int aliases_size
, int addresses
, int address_length
);
357 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
358 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
);
359 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
361 int WSAIOCTL_GetInterfaceCount(void);
362 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
364 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
, ULONG Information
);
366 #define MAP_OPTION(opt) { WS_##opt, opt }
368 static const int ws_sock_map
[][2] =
370 MAP_OPTION( SO_DEBUG
),
371 MAP_OPTION( SO_ACCEPTCONN
),
372 MAP_OPTION( SO_REUSEADDR
),
373 MAP_OPTION( SO_KEEPALIVE
),
374 MAP_OPTION( SO_DONTROUTE
),
375 MAP_OPTION( SO_BROADCAST
),
376 MAP_OPTION( SO_LINGER
),
377 MAP_OPTION( SO_OOBINLINE
),
378 MAP_OPTION( SO_SNDBUF
),
379 MAP_OPTION( SO_RCVBUF
),
380 MAP_OPTION( SO_ERROR
),
381 MAP_OPTION( SO_TYPE
),
383 MAP_OPTION( SO_RCVTIMEO
),
386 MAP_OPTION( SO_SNDTIMEO
),
390 static const int ws_tcp_map
[][2] =
393 MAP_OPTION( TCP_NODELAY
),
397 static const int ws_ip_map
[][2] =
399 MAP_OPTION( IP_MULTICAST_IF
),
400 MAP_OPTION( IP_MULTICAST_TTL
),
401 MAP_OPTION( IP_MULTICAST_LOOP
),
402 MAP_OPTION( IP_ADD_MEMBERSHIP
),
403 MAP_OPTION( IP_DROP_MEMBERSHIP
),
404 MAP_OPTION( IP_OPTIONS
),
406 MAP_OPTION( IP_HDRINCL
),
408 MAP_OPTION( IP_TOS
),
409 MAP_OPTION( IP_TTL
),
411 MAP_OPTION( IP_PKTINFO
),
414 MAP_OPTION( IP_UNICAST_IF
),
418 static const int ws_ipv6_map
[][2] =
420 #ifdef IPV6_ADD_MEMBERSHIP
421 MAP_OPTION( IPV6_ADD_MEMBERSHIP
),
423 #ifdef IPV6_DROP_MEMBERSHIP
424 MAP_OPTION( IPV6_DROP_MEMBERSHIP
),
426 MAP_OPTION( IPV6_MULTICAST_IF
),
427 MAP_OPTION( IPV6_MULTICAST_HOPS
),
428 MAP_OPTION( IPV6_MULTICAST_LOOP
),
429 MAP_OPTION( IPV6_UNICAST_HOPS
),
430 MAP_OPTION( IPV6_V6ONLY
),
431 #ifdef IPV6_UNICAST_IF
432 MAP_OPTION( IPV6_UNICAST_IF
),
436 static const int ws_af_map
[][2] =
438 MAP_OPTION( AF_UNSPEC
),
439 MAP_OPTION( AF_INET
),
440 MAP_OPTION( AF_INET6
),
442 MAP_OPTION( AF_IPX
),
445 MAP_OPTION( AF_IRDA
),
447 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
450 static const int ws_socktype_map
[][2] =
452 MAP_OPTION( SOCK_DGRAM
),
453 MAP_OPTION( SOCK_STREAM
),
454 MAP_OPTION( SOCK_RAW
),
455 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
458 static const int ws_proto_map
[][2] =
460 MAP_OPTION( IPPROTO_IP
),
461 MAP_OPTION( IPPROTO_TCP
),
462 MAP_OPTION( IPPROTO_UDP
),
463 MAP_OPTION( IPPROTO_ICMP
),
464 MAP_OPTION( IPPROTO_IGMP
),
465 MAP_OPTION( IPPROTO_RAW
),
466 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
469 static const int ws_aiflag_map
[][2] =
471 MAP_OPTION( AI_PASSIVE
),
472 MAP_OPTION( AI_CANONNAME
),
473 MAP_OPTION( AI_NUMERICHOST
),
474 MAP_OPTION( AI_ADDRCONFIG
),
477 static const int ws_niflag_map
[][2] =
479 MAP_OPTION( NI_NOFQDN
),
480 MAP_OPTION( NI_NUMERICHOST
),
481 MAP_OPTION( NI_NAMEREQD
),
482 MAP_OPTION( NI_NUMERICSERV
),
483 MAP_OPTION( NI_DGRAM
),
486 static const int ws_eai_map
[][2] =
488 MAP_OPTION( EAI_AGAIN
),
489 MAP_OPTION( EAI_BADFLAGS
),
490 MAP_OPTION( EAI_FAIL
),
491 MAP_OPTION( EAI_FAMILY
),
492 MAP_OPTION( EAI_MEMORY
),
493 /* Note: EAI_NODATA is deprecated, but still
494 * used by Windows and Linux... We map the newer
495 * EAI_NONAME to EAI_NODATA for now until Windows
499 MAP_OPTION( EAI_NODATA
),
502 { WS_EAI_NODATA
, EAI_NONAME
},
505 MAP_OPTION( EAI_SERVICE
),
506 MAP_OPTION( EAI_SOCKTYPE
),
510 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
512 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
513 static inline WSACMSGHDR
*fill_control_message(int level
, int type
, WSACMSGHDR
*current
, ULONG
*maxsize
, void *data
, int len
)
515 ULONG msgsize
= sizeof(WSACMSGHDR
) + WSA_CMSG_ALIGN(len
);
516 char *ptr
= (char *) current
+ sizeof(WSACMSGHDR
);
518 /* Make sure there is at least enough room for this entry */
519 if (msgsize
> *maxsize
)
522 /* Fill in the entry */
523 current
->cmsg_len
= sizeof(WSACMSGHDR
) + len
;
524 current
->cmsg_level
= level
;
525 current
->cmsg_type
= type
;
526 memcpy(ptr
, data
, len
);
527 /* Return the pointer to where next entry should go */
528 return (WSACMSGHDR
*) (ptr
+ WSA_CMSG_ALIGN(len
));
531 static inline int convert_control_headers(struct msghdr
*hdr
, WSABUF
*control
)
534 WSACMSGHDR
*cmsg_win
= (WSACMSGHDR
*) control
->buf
, *ptr
;
535 ULONG ctlsize
= control
->len
;
536 struct cmsghdr
*cmsg_unix
;
539 /* Loop over all the headers, converting as appropriate */
540 for (cmsg_unix
= CMSG_FIRSTHDR(hdr
); cmsg_unix
!= NULL
; cmsg_unix
= CMSG_NXTHDR(hdr
, cmsg_unix
))
542 switch(cmsg_unix
->cmsg_level
)
545 switch(cmsg_unix
->cmsg_type
)
549 /* Convert the Unix IP_PKTINFO structure to the Windows version */
550 struct in_pktinfo
*data_unix
= (struct in_pktinfo
*) CMSG_DATA(cmsg_unix
);
551 struct WS_in_pktinfo data_win
;
553 memcpy(&data_win
.ipi_addr
,&data_unix
->ipi_addr
.s_addr
,4); /* 4 bytes = 32 address bits */
554 data_win
.ipi_ifindex
= data_unix
->ipi_ifindex
;
555 ptr
= fill_control_message(WS_IPPROTO_IP
, WS_IP_PKTINFO
, ptr
, &ctlsize
,
556 (void*)&data_win
, sizeof(data_win
));
557 if (!ptr
) goto error
;
560 FIXME("Unhandled IPPROTO_IP message header type %d\n", cmsg_unix
->cmsg_type
);
565 FIXME("Unhandled message header level %d\n", cmsg_unix
->cmsg_level
);
571 /* Set the length of the returned control headers */
572 control
->len
= (ptr
== NULL
? 0 : (char*)ptr
- (char*)cmsg_win
);
573 return (ptr
!= NULL
);
574 #else /* IP_PKTINFO */
577 #endif /* IP_PKTINFO */
579 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
581 /* ----------------------------------- error handling */
583 static NTSTATUS
sock_get_ntstatus( int err
)
587 case EBADF
: return STATUS_INVALID_HANDLE
;
588 case EBUSY
: return STATUS_DEVICE_BUSY
;
590 case EACCES
: return STATUS_ACCESS_DENIED
;
591 case EFAULT
: return STATUS_NO_MEMORY
;
592 case EINVAL
: return STATUS_INVALID_PARAMETER
;
594 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
595 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
596 case EINPROGRESS
: return STATUS_PENDING
;
597 case EALREADY
: return STATUS_NETWORK_BUSY
;
598 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
599 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
600 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
601 case EPROTONOSUPPORT
:
602 case ESOCKTNOSUPPORT
:
605 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
606 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
607 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
608 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
609 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
610 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
611 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
612 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
613 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
614 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
615 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
617 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
618 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
620 case 0: return STATUS_SUCCESS
;
622 WARN("Unknown errno %d!\n", err
);
623 return STATUS_UNSUCCESSFUL
;
627 static UINT
sock_get_error( int err
)
631 case EINTR
: return WSAEINTR
;
632 case EBADF
: return WSAEBADF
;
634 case EACCES
: return WSAEACCES
;
635 case EFAULT
: return WSAEFAULT
;
636 case EINVAL
: return WSAEINVAL
;
637 case EMFILE
: return WSAEMFILE
;
638 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
639 case EINPROGRESS
: return WSAEINPROGRESS
;
640 case EALREADY
: return WSAEALREADY
;
641 case ENOTSOCK
: return WSAENOTSOCK
;
642 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
643 case EMSGSIZE
: return WSAEMSGSIZE
;
644 case EPROTOTYPE
: return WSAEPROTOTYPE
;
645 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
646 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
647 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
648 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
649 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
650 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
651 case EADDRINUSE
: return WSAEADDRINUSE
;
652 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
653 case ENETDOWN
: return WSAENETDOWN
;
654 case ENETUNREACH
: return WSAENETUNREACH
;
655 case ENETRESET
: return WSAENETRESET
;
656 case ECONNABORTED
: return WSAECONNABORTED
;
658 case ECONNRESET
: return WSAECONNRESET
;
659 case ENOBUFS
: return WSAENOBUFS
;
660 case EISCONN
: return WSAEISCONN
;
661 case ENOTCONN
: return WSAENOTCONN
;
662 case ESHUTDOWN
: return WSAESHUTDOWN
;
663 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
664 case ETIMEDOUT
: return WSAETIMEDOUT
;
665 case ECONNREFUSED
: return WSAECONNREFUSED
;
666 case ELOOP
: return WSAELOOP
;
667 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
668 case EHOSTDOWN
: return WSAEHOSTDOWN
;
669 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
670 case ENOTEMPTY
: return WSAENOTEMPTY
;
672 case EPROCLIM
: return WSAEPROCLIM
;
675 case EUSERS
: return WSAEUSERS
;
678 case EDQUOT
: return WSAEDQUOT
;
681 case ESTALE
: return WSAESTALE
;
684 case EREMOTE
: return WSAEREMOTE
;
687 /* just in case we ever get here and there are no problems */
690 WARN("Unknown errno %d!\n", err
);
691 return WSAEOPNOTSUPP
;
695 static UINT
wsaErrno(void)
697 int loc_errno
= errno
;
698 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
700 return sock_get_error( loc_errno
);
703 /* most ws2 overlapped functions return an ntstatus-based error code */
704 static NTSTATUS
wsaErrStatus(void)
706 int loc_errno
= errno
;
707 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
709 return sock_get_ntstatus(loc_errno
);
712 static UINT
wsaHerrno(int loc_errno
)
714 WARN("h_errno %d.\n", loc_errno
);
718 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
719 case TRY_AGAIN
: return WSATRY_AGAIN
;
720 case NO_RECOVERY
: return WSANO_RECOVERY
;
721 case NO_DATA
: return WSANO_DATA
;
722 case ENOBUFS
: return WSAENOBUFS
;
726 WARN("Unknown h_errno %d!\n", loc_errno
);
727 return WSAEOPNOTSUPP
;
731 static inline DWORD
NtStatusToWSAError( const DWORD status
)
733 /* We only need to cover the status codes set by server async request handling */
737 case STATUS_SUCCESS
: wserr
= 0; break;
738 case STATUS_PENDING
: wserr
= WSA_IO_PENDING
; break;
739 case STATUS_OBJECT_TYPE_MISMATCH
: wserr
= WSAENOTSOCK
; break;
740 case STATUS_INVALID_HANDLE
: wserr
= WSAEBADF
; break;
741 case STATUS_INVALID_PARAMETER
: wserr
= WSAEINVAL
; break;
742 case STATUS_PIPE_DISCONNECTED
: wserr
= WSAESHUTDOWN
; break;
743 case STATUS_NETWORK_BUSY
: wserr
= WSAEALREADY
; break;
744 case STATUS_NETWORK_UNREACHABLE
: wserr
= WSAENETUNREACH
; break;
745 case STATUS_CONNECTION_REFUSED
: wserr
= WSAECONNREFUSED
; break;
746 case STATUS_CONNECTION_DISCONNECTED
: wserr
= WSAENOTCONN
; break;
747 case STATUS_CONNECTION_RESET
: wserr
= WSAECONNRESET
; break;
748 case STATUS_CONNECTION_ABORTED
: wserr
= WSAECONNABORTED
; break;
749 case STATUS_CANCELLED
: wserr
= WSA_OPERATION_ABORTED
; break;
750 case STATUS_ADDRESS_ALREADY_ASSOCIATED
: wserr
= WSAEADDRINUSE
; break;
751 case STATUS_IO_TIMEOUT
:
752 case STATUS_TIMEOUT
: wserr
= WSAETIMEDOUT
; break;
753 case STATUS_NO_MEMORY
: wserr
= WSAEFAULT
; break;
754 case STATUS_ACCESS_DENIED
: wserr
= WSAEACCES
; break;
755 case STATUS_TOO_MANY_OPENED_FILES
: wserr
= WSAEMFILE
; break;
756 case STATUS_CANT_WAIT
: wserr
= WSAEWOULDBLOCK
; break;
757 case STATUS_BUFFER_OVERFLOW
: wserr
= WSAEMSGSIZE
; break;
758 case STATUS_NOT_SUPPORTED
: wserr
= WSAEOPNOTSUPP
; break;
759 case STATUS_HOST_UNREACHABLE
: wserr
= WSAEHOSTUNREACH
; break;
762 wserr
= RtlNtStatusToDosError( status
);
763 FIXME( "Status code %08x converted to DOS error code %x\n", status
, wserr
);
768 /* set last error code from NT status without mapping WSA errors */
769 static inline unsigned int set_error( unsigned int err
)
773 err
= NtStatusToWSAError( err
);
779 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
782 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
787 static inline void release_sock_fd( SOCKET s
, int fd
)
789 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
792 static void _enable_event( HANDLE s
, unsigned int event
,
793 unsigned int sstate
, unsigned int cstate
)
795 SERVER_START_REQ( enable_socket_event
)
797 req
->handle
= wine_server_obj_handle( s
);
799 req
->sstate
= sstate
;
800 req
->cstate
= cstate
;
801 wine_server_call( req
);
806 static int _is_blocking(SOCKET s
)
809 SERVER_START_REQ( get_socket_event
)
811 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
812 req
->service
= FALSE
;
814 wine_server_call( req
);
815 ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
821 static unsigned int _get_sock_mask(SOCKET s
)
824 SERVER_START_REQ( get_socket_event
)
826 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
827 req
->service
= FALSE
;
829 wine_server_call( req
);
836 static void _sync_sock_state(SOCKET s
)
838 /* do a dummy wineserver request in order to let
839 the wineserver run through its select loop once */
840 (void)_is_blocking(s
);
843 static int _get_sock_error(SOCKET s
, unsigned int bit
)
845 int events
[FD_MAX_EVENTS
];
847 SERVER_START_REQ( get_socket_event
)
849 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
850 req
->service
= FALSE
;
852 wine_server_set_reply( req
, events
, sizeof(events
) );
853 wine_server_call( req
);
859 static struct per_thread_data
*get_per_thread_data(void)
861 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
862 /* lazy initialization */
865 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
866 NtCurrentTeb()->WinSockData
= ptb
;
871 static void free_per_thread_data(void)
873 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
877 /* delete scratch buffers */
878 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
879 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
880 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
881 ptb
->he_buffer
= NULL
;
882 ptb
->se_buffer
= NULL
;
883 ptb
->pe_buffer
= NULL
;
885 HeapFree( GetProcessHeap(), 0, ptb
);
886 NtCurrentTeb()->WinSockData
= NULL
;
889 /***********************************************************************
890 * DllMain (WS2_32.init)
892 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
894 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
896 case DLL_PROCESS_ATTACH
:
898 case DLL_PROCESS_DETACH
:
899 free_per_thread_data();
900 DeleteCriticalSection(&csWSgetXXXbyYYY
);
903 case DLL_THREAD_DETACH
:
904 free_per_thread_data();
910 /***********************************************************************
913 * Converts socket flags from Windows format.
914 * Return 1 if converted, 0 if not (error).
916 static int convert_sockopt(INT
*level
, INT
*optname
)
923 for(i
=0; i
<sizeof(ws_sock_map
)/sizeof(ws_sock_map
[0]); i
++) {
924 if( ws_sock_map
[i
][0] == *optname
)
926 *optname
= ws_sock_map
[i
][1];
930 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
933 *level
= IPPROTO_TCP
;
934 for(i
=0; i
<sizeof(ws_tcp_map
)/sizeof(ws_tcp_map
[0]); i
++) {
935 if ( ws_tcp_map
[i
][0] == *optname
)
937 *optname
= ws_tcp_map
[i
][1];
941 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
945 for(i
=0; i
<sizeof(ws_ip_map
)/sizeof(ws_ip_map
[0]); i
++) {
946 if (ws_ip_map
[i
][0] == *optname
)
948 *optname
= ws_ip_map
[i
][1];
952 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
954 case WS_IPPROTO_IPV6
:
955 *level
= IPPROTO_IPV6
;
956 for(i
=0; i
<sizeof(ws_ipv6_map
)/sizeof(ws_ipv6_map
[0]); i
++) {
957 if (ws_ipv6_map
[i
][0] == *optname
)
959 *optname
= ws_ipv6_map
[i
][1];
963 FIXME("Unknown IPPROTO_IPV6 optname 0x%x\n", *optname
);
965 default: FIXME("Unimplemented or unknown socket level\n");
970 /* ----------------------------------- Per-thread info (or per-process?) */
972 static char *strdup_lower(const char *str
)
975 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
979 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
982 else SetLastError(WSAENOBUFS
);
986 static inline int sock_error_p(int s
)
989 socklen_t optlen
= sizeof(optval
);
990 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
991 if (optval
) WARN("\t[%i] error: %d\n", s
, optval
);
995 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
996 * from an fd and return the value converted to milli seconds
997 * or -1 if there is an infinite time out */
998 static inline int get_rcvsnd_timeo( int fd
, int optname
)
1001 socklen_t len
= sizeof(tv
);
1002 int ret
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
1004 ret
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
1005 if( ret
<= 0 ) /* tv == {0,0} means infinite time out */
1010 /* macro wrappers for portability */
1012 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
1014 #define GET_RCVTIMEO(fd) (-1)
1018 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
1020 #define GET_SNDTIMEO(fd) (-1)
1023 /* utility: given an fd, will block until one of the events occurs */
1024 static inline int do_block( int fd
, int events
, int timeout
)
1030 pfd
.events
= events
;
1032 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
1043 convert_af_w2u(int windowsaf
) {
1046 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
1047 if (ws_af_map
[i
][0] == windowsaf
)
1048 return ws_af_map
[i
][1];
1049 FIXME("unhandled Windows address family %d\n", windowsaf
);
1054 convert_af_u2w(int unixaf
) {
1057 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
1058 if (ws_af_map
[i
][1] == unixaf
)
1059 return ws_af_map
[i
][0];
1060 FIXME("unhandled UNIX address family %d\n", unixaf
);
1065 convert_proto_w2u(int windowsproto
) {
1068 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
1069 if (ws_proto_map
[i
][0] == windowsproto
)
1070 return ws_proto_map
[i
][1];
1071 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
1076 convert_proto_u2w(int unixproto
) {
1079 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
1080 if (ws_proto_map
[i
][1] == unixproto
)
1081 return ws_proto_map
[i
][0];
1082 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
1087 convert_socktype_w2u(int windowssocktype
) {
1090 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
1091 if (ws_socktype_map
[i
][0] == windowssocktype
)
1092 return ws_socktype_map
[i
][1];
1093 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
1098 convert_socktype_u2w(int unixsocktype
) {
1101 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
1102 if (ws_socktype_map
[i
][1] == unixsocktype
)
1103 return ws_socktype_map
[i
][0];
1104 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
1108 /* ----------------------------------- API -----
1110 * Init / cleanup / error checking.
1113 /***********************************************************************
1114 * WSAStartup (WS2_32.115)
1116 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
1118 TRACE("verReq=%x\n", wVersionRequested
);
1120 if (LOBYTE(wVersionRequested
) < 1)
1121 return WSAVERNOTSUPPORTED
;
1123 if (!lpWSAData
) return WSAEINVAL
;
1127 /* that's the whole of the negotiation for now */
1128 lpWSAData
->wVersion
= wVersionRequested
;
1129 /* return winsock information */
1130 lpWSAData
->wHighVersion
= 0x0202;
1131 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
1132 strcpy(lpWSAData
->szSystemStatus
, "Running" );
1133 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
1134 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
1135 /* don't do anything with lpWSAData->lpVendorInfo */
1136 /* (some apps don't allocate the space for this field) */
1138 TRACE("succeeded\n");
1143 /***********************************************************************
1144 * WSACleanup (WS2_32.116)
1146 INT WINAPI
WSACleanup(void)
1152 SetLastError(WSANOTINITIALISED
);
1153 return SOCKET_ERROR
;
1157 /***********************************************************************
1158 * WSAGetLastError (WS2_32.111)
1160 INT WINAPI
WSAGetLastError(void)
1162 return GetLastError();
1165 /***********************************************************************
1166 * WSASetLastError (WS2_32.112)
1168 void WINAPI
WSASetLastError(INT iError
) {
1169 SetLastError(iError
);
1172 static struct WS_hostent
*check_buffer_he(int size
)
1174 struct per_thread_data
* ptb
= get_per_thread_data();
1177 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
1178 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1180 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
1181 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
1182 return ptb
->he_buffer
;
1185 static struct WS_servent
*check_buffer_se(int size
)
1187 struct per_thread_data
* ptb
= get_per_thread_data();
1190 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
1191 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1193 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
1194 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
1195 return ptb
->se_buffer
;
1198 static struct WS_protoent
*check_buffer_pe(int size
)
1200 struct per_thread_data
* ptb
= get_per_thread_data();
1203 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
1204 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1206 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
1207 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
1208 return ptb
->pe_buffer
;
1211 /* ----------------------------------- i/o APIs */
1213 static inline BOOL
supported_pf(int pf
)
1234 /**********************************************************************/
1236 /* Returns the length of the converted address if successful, 0 if it was too small to
1239 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
1240 union generic_unix_sockaddr
*uaddr
)
1242 unsigned int uaddrlen
= 0;
1244 switch (wsaddr
->sa_family
)
1249 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
1250 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
1252 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
1255 uaddrlen
= sizeof(struct sockaddr_ipx
);
1256 memset( uaddr
, 0, uaddrlen
);
1257 uipx
->sipx_family
=AF_IPX
;
1258 uipx
->sipx_port
=wsipx
->sa_socket
;
1259 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
1262 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
1263 #ifdef IPX_FRAME_NONE
1264 uipx
->sipx_type
=IPX_FRAME_NONE
;
1270 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
1271 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
1273 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
1274 * scope_id, one without.
1276 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
1277 uaddrlen
= sizeof(struct sockaddr_in6
);
1278 memset( uaddr
, 0, uaddrlen
);
1279 uin6
->sin6_family
= AF_INET6
;
1280 uin6
->sin6_port
= win6
->sin6_port
;
1281 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
1282 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1283 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
1285 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1288 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
1292 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
1293 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
1295 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
1297 uaddrlen
= sizeof(struct sockaddr_in
);
1298 memset( uaddr
, 0, uaddrlen
);
1299 uin
->sin_family
= AF_INET
;
1300 uin
->sin_port
= win
->sin_port
;
1301 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
1306 struct sockaddr_irda
*uin
= (struct sockaddr_irda
*)uaddr
;
1307 const SOCKADDR_IRDA
*win
= (const SOCKADDR_IRDA
*)wsaddr
;
1309 if (wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1311 uaddrlen
= sizeof(struct sockaddr_irda
);
1312 memset( uaddr
, 0, uaddrlen
);
1313 uin
->sir_family
= AF_IRDA
;
1314 if (!strncmp( win
->irdaServiceName
, "LSAP-SEL", strlen( "LSAP-SEL" ) ))
1316 unsigned int lsap_sel
= 0;
1318 sscanf( win
->irdaServiceName
, "LSAP-SEL%u", &lsap_sel
);
1319 uin
->sir_lsap_sel
= lsap_sel
;
1323 uin
->sir_lsap_sel
= LSAP_ANY
;
1324 memcpy( uin
->sir_name
, win
->irdaServiceName
, 25 );
1326 memcpy( &uin
->sir_addr
, win
->irdaDeviceID
, sizeof(uin
->sir_addr
) );
1330 case WS_AF_UNSPEC
: {
1331 /* Try to determine the needed space by the passed windows sockaddr space */
1332 switch (wsaddrlen
) {
1333 default: /* likely a ipv4 address */
1334 case sizeof(struct WS_sockaddr_in
):
1335 uaddrlen
= sizeof(struct sockaddr_in
);
1338 case sizeof(struct WS_sockaddr_ipx
):
1339 uaddrlen
= sizeof(struct sockaddr_ipx
);
1343 case sizeof(SOCKADDR_IRDA
):
1344 uaddrlen
= sizeof(struct sockaddr_irda
);
1347 case sizeof(struct WS_sockaddr_in6
):
1348 case sizeof(struct WS_sockaddr_in6_old
):
1349 uaddrlen
= sizeof(struct sockaddr_in6
);
1352 memset( uaddr
, 0, uaddrlen
);
1356 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
1362 static BOOL
is_sockaddr_bound(const struct sockaddr
*uaddr
, int uaddrlen
)
1364 switch (uaddr
->sa_family
)
1368 FIXME("don't know how to tell if IPX socket is bound, assuming it is!\n");
1373 static const struct sockaddr_in6 emptyAddr
;
1374 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) uaddr
;
1375 return in6
->sin6_port
|| memcmp(&in6
->sin6_addr
, &emptyAddr
.sin6_addr
, sizeof(struct in6_addr
));
1379 static const struct sockaddr_in emptyAddr
;
1380 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) uaddr
;
1381 return in
->sin_port
|| memcmp(&in
->sin_addr
, &emptyAddr
.sin_addr
, sizeof(struct in_addr
));
1386 FIXME("unknown address family %d\n", uaddr
->sa_family
);
1391 /* Returns 0 if successful, -1 if the buffer is too small */
1392 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
1396 switch(uaddr
->sa_family
)
1401 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
1402 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
1405 switch (*wsaddrlen
) /* how much can we copy? */
1409 *wsaddrlen
= sizeof(*wsipx
);
1410 wsipx
->sa_socket
=uipx
->sipx_port
;
1414 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
1422 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
1428 wsipx
->sa_family
=WS_AF_IPX
;
1440 const struct sockaddr_irda
*uin
= (const struct sockaddr_irda
*)uaddr
;
1441 SOCKADDR_IRDA
*win
= (SOCKADDR_IRDA
*)wsaddr
;
1443 if (*wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1445 win
->irdaAddressFamily
= WS_AF_IRDA
;
1446 memcpy( win
->irdaDeviceID
, &uin
->sir_addr
, sizeof(win
->irdaDeviceID
) );
1447 if (uin
->sir_lsap_sel
!= LSAP_ANY
)
1448 sprintf( win
->irdaServiceName
, "LSAP-SEL%u", uin
->sir_lsap_sel
);
1450 memcpy( win
->irdaServiceName
, uin
->sir_name
,
1451 sizeof(win
->irdaServiceName
) );
1456 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
1457 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
1459 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
1461 win6old
->sin6_family
= WS_AF_INET6
;
1462 win6old
->sin6_port
= uin6
->sin6_port
;
1463 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
1464 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1465 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1466 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
1467 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
1468 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
1469 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
1472 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1474 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1479 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
1480 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
1482 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
1484 win
->sin_family
= WS_AF_INET
;
1485 win
->sin_port
= uin
->sin_port
;
1486 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
1487 memset(win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
1488 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
1492 memset(wsaddr
,0,*wsaddrlen
);
1496 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
1502 /**************************************************************************
1503 * Functions for handling overlapped I/O
1504 **************************************************************************/
1506 /* user APC called upon async completion */
1507 static void WINAPI
ws2_async_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1509 ws2_async
*wsa
= arg
;
1511 if (wsa
->completion_func
) wsa
->completion_func( NtStatusToWSAError(iosb
->u
.Status
),
1512 iosb
->Information
, wsa
->user_overlapped
,
1514 HeapFree( GetProcessHeap(), 0, wsa
);
1517 /***********************************************************************
1518 * WS2_recv (INTERNAL)
1520 * Workhorse for both synchronous and asynchronous recv() operations.
1522 static int WS2_recv( int fd
, struct ws2_async
*wsa
)
1524 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1528 union generic_unix_sockaddr unix_sockaddr
;
1531 hdr
.msg_name
= NULL
;
1535 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
1536 hdr
.msg_name
= &unix_sockaddr
;
1539 hdr
.msg_namelen
= 0;
1541 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1542 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1543 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1544 hdr
.msg_accrights
= NULL
;
1545 hdr
.msg_accrightslen
= 0;
1547 hdr
.msg_control
= pktbuf
;
1548 hdr
.msg_controllen
= sizeof(pktbuf
);
1552 if ( (n
= recvmsg(fd
, &hdr
, wsa
->flags
)) == -1 )
1555 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1558 ERR("Message control headers cannot be properly supported on this system.\n");
1559 wsa
->control
->len
= 0;
1562 if (wsa
->control
&& !convert_control_headers(&hdr
, wsa
->control
))
1564 WARN("Application passed insufficient room for control headers.\n");
1565 *wsa
->lpFlags
|= WS_MSG_CTRUNC
;
1571 /* if this socket is connected and lpFrom is not NULL, Linux doesn't give us
1572 * msg_name and msg_namelen from recvmsg, but it does set msg_namelen to zero.
1574 * quoting linux 2.6 net/ipv4/tcp.c:
1575 * "According to UNIX98, msg_name/msg_namelen are ignored
1576 * on connected socket. I was just happy when found this 8) --ANK"
1578 * likewise MSDN says that lpFrom and lpFromlen are ignored for
1579 * connection-oriented sockets, so don't try to update lpFrom.
1581 if (wsa
->addr
&& hdr
.msg_namelen
)
1582 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, wsa
->addr
, wsa
->addrlen
.ptr
);
1587 /***********************************************************************
1588 * WS2_async_recv (INTERNAL)
1590 * Handler for overlapped recv() operations.
1592 static NTSTATUS
WS2_async_recv( void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
1594 ws2_async
* wsa
= user
;
1599 case STATUS_ALERTED
:
1600 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
1603 result
= WS2_recv( fd
, wsa
);
1604 wine_server_release_fd( wsa
->hSocket
, fd
);
1607 status
= STATUS_SUCCESS
;
1608 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1612 if (errno
== EINTR
|| errno
== EAGAIN
)
1614 status
= STATUS_PENDING
;
1615 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1620 status
= wsaErrStatus();
1625 if (status
!= STATUS_PENDING
)
1627 iosb
->u
.Status
= status
;
1628 iosb
->Information
= result
;
1629 *apc
= ws2_async_apc
;
1634 /* user APC called upon async accept completion */
1635 static void WINAPI
ws2_async_accept_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1637 struct ws2_accept_async
*wsa
= arg
;
1639 HeapFree( GetProcessHeap(), 0, wsa
->read
);
1640 HeapFree( GetProcessHeap(), 0, wsa
);
1643 /***********************************************************************
1644 * WS2_async_accept_recv (INTERNAL)
1646 * This function is used to finish the read part of an accept request. It is
1647 * needed to place the completion on the correct socket (listener).
1649 static NTSTATUS
WS2_async_accept_recv( void *arg
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
, void **apc
)
1652 struct ws2_accept_async
*wsa
= arg
;
1654 status
= WS2_async_recv( wsa
->read
, iosb
, status
, &junk
);
1655 if (status
== STATUS_PENDING
)
1658 if (wsa
->user_overlapped
->hEvent
)
1659 SetEvent(wsa
->user_overlapped
->hEvent
);
1661 WS_AddCompletion( HANDLE2SOCKET(wsa
->listen_socket
), wsa
->cvalue
, iosb
->u
.Status
, iosb
->Information
);
1663 *apc
= ws2_async_accept_apc
;
1667 /***********************************************************************
1668 * WS2_async_accept (INTERNAL)
1670 * This is the function called to satisfy the AcceptEx callback
1672 static NTSTATUS
WS2_async_accept( void *arg
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
, void **apc
)
1674 struct ws2_accept_async
*wsa
= arg
;
1678 TRACE("status: 0x%x listen: %p, accept: %p\n", status
, wsa
->listen_socket
, wsa
->accept_socket
);
1680 if (status
== STATUS_ALERTED
)
1682 SERVER_START_REQ( accept_into_socket
)
1684 req
->lhandle
= wine_server_obj_handle( wsa
->listen_socket
);
1685 req
->ahandle
= wine_server_obj_handle( wsa
->accept_socket
);
1686 status
= wine_server_call( req
);
1690 if (status
== STATUS_CANT_WAIT
)
1691 return STATUS_PENDING
;
1693 if (status
== STATUS_INVALID_HANDLE
)
1695 FIXME("AcceptEx accepting socket closed but request was not cancelled\n");
1696 status
= STATUS_CANCELLED
;
1699 else if (status
== STATUS_HANDLES_CLOSED
)
1700 status
= STATUS_CANCELLED
; /* strange windows behavior */
1702 if (status
!= STATUS_SUCCESS
)
1705 /* WS2 Spec says size param is extra 16 bytes long...what do we put in it? */
1706 addr
= ((char *)wsa
->buf
) + wsa
->data_len
;
1707 len
= wsa
->local_len
- sizeof(int);
1708 WS_getsockname(HANDLE2SOCKET(wsa
->accept_socket
),
1709 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
1712 addr
+= wsa
->local_len
;
1713 len
= wsa
->remote_len
- sizeof(int);
1714 WS_getpeername(HANDLE2SOCKET(wsa
->accept_socket
),
1715 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
1721 SERVER_START_REQ( register_async
)
1723 req
->type
= ASYNC_TYPE_READ
;
1724 req
->async
.handle
= wine_server_obj_handle( wsa
->accept_socket
);
1725 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept_recv
);
1726 req
->async
.iosb
= wine_server_client_ptr( iosb
);
1727 req
->async
.arg
= wine_server_client_ptr( wsa
);
1728 status
= wine_server_call( req
);
1732 if (status
!= STATUS_PENDING
)
1735 /* The APC has finished but no completion should be sent for the operation yet, additional processing
1736 * needs to be performed by WS2_async_accept_recv() first. */
1737 return STATUS_MORE_PROCESSING_REQUIRED
;
1740 iosb
->u
.Status
= status
;
1741 iosb
->Information
= 0;
1743 if (wsa
->user_overlapped
->hEvent
)
1744 SetEvent(wsa
->user_overlapped
->hEvent
);
1746 *apc
= ws2_async_accept_apc
;
1750 /***********************************************************************
1751 * WS2_send (INTERNAL)
1753 * Workhorse for both synchronous and asynchronous send() operations.
1755 static int WS2_send( int fd
, struct ws2_async
*wsa
)
1758 union generic_unix_sockaddr unix_addr
;
1761 hdr
.msg_name
= NULL
;
1762 hdr
.msg_namelen
= 0;
1766 hdr
.msg_name
= &unix_addr
;
1767 hdr
.msg_namelen
= ws_sockaddr_ws2u( wsa
->addr
, wsa
->addrlen
.val
, &unix_addr
);
1768 if ( !hdr
.msg_namelen
)
1774 #if defined(HAVE_IPX) && defined(SOL_IPX)
1775 if(wsa
->addr
->sa_family
== WS_AF_IPX
)
1777 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
1779 socklen_t len
= sizeof(int);
1781 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1782 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1783 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1784 * ipx type in the sockaddr_opx structure with the stored value.
1786 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
1787 uipx
->sipx_type
= val
;
1792 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1793 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1794 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1795 hdr
.msg_accrights
= NULL
;
1796 hdr
.msg_accrightslen
= 0;
1798 hdr
.msg_control
= NULL
;
1799 hdr
.msg_controllen
= 0;
1803 ret
= sendmsg(fd
, &hdr
, wsa
->flags
);
1807 while (wsa
->first_iovec
< wsa
->n_iovecs
&& wsa
->iovec
[wsa
->first_iovec
].iov_len
<= n
)
1808 n
-= wsa
->iovec
[wsa
->first_iovec
++].iov_len
;
1809 if (wsa
->first_iovec
< wsa
->n_iovecs
)
1811 wsa
->iovec
[wsa
->first_iovec
].iov_base
= (char*)wsa
->iovec
[wsa
->first_iovec
].iov_base
+ n
;
1812 wsa
->iovec
[wsa
->first_iovec
].iov_len
-= n
;
1818 /***********************************************************************
1819 * WS2_async_send (INTERNAL)
1821 * Handler for overlapped send() operations.
1823 static NTSTATUS
WS2_async_send(void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
1825 ws2_async
* wsa
= user
;
1830 case STATUS_ALERTED
:
1831 if ( wsa
->n_iovecs
<= wsa
->first_iovec
)
1834 status
= STATUS_SUCCESS
;
1837 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
1840 /* check to see if the data is ready (non-blocking) */
1841 result
= WS2_send( fd
, wsa
);
1842 wine_server_release_fd( wsa
->hSocket
, fd
);
1846 if (wsa
->first_iovec
< wsa
->n_iovecs
)
1847 status
= STATUS_PENDING
;
1849 status
= STATUS_SUCCESS
;
1851 iosb
->Information
+= result
;
1853 else if (errno
== EINTR
|| errno
== EAGAIN
)
1855 status
= STATUS_PENDING
;
1859 status
= wsaErrStatus();
1863 if (status
!= STATUS_PENDING
)
1865 iosb
->u
.Status
= status
;
1866 *apc
= ws2_async_apc
;
1871 /***********************************************************************
1872 * WS2_async_shutdown (INTERNAL)
1874 * Handler for shutdown() operations on overlapped sockets.
1876 static NTSTATUS
WS2_async_shutdown( void* user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
, void **apc
)
1878 ws2_async
* wsa
= user
;
1883 case STATUS_ALERTED
:
1884 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
1887 switch ( wsa
->type
)
1889 case ASYNC_TYPE_READ
: err
= shutdown( fd
, 0 ); break;
1890 case ASYNC_TYPE_WRITE
: err
= shutdown( fd
, 1 ); break;
1892 status
= err
? wsaErrStatus() : STATUS_SUCCESS
;
1893 wine_server_release_fd( wsa
->hSocket
, fd
);
1896 iosb
->u
.Status
= status
;
1897 iosb
->Information
= 0;
1898 *apc
= ws2_async_apc
;
1902 /***********************************************************************
1903 * WS2_register_async_shutdown (INTERNAL)
1905 * Helper function for WS_shutdown() on overlapped sockets.
1907 static int WS2_register_async_shutdown( SOCKET s
, int type
)
1909 struct ws2_async
*wsa
;
1912 TRACE("s %ld type %d\n", s
, type
);
1914 wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( struct ws2_async
, iovec
[1] ));
1918 wsa
->hSocket
= SOCKET2HANDLE(s
);
1920 wsa
->completion_func
= NULL
;
1922 SERVER_START_REQ( register_async
)
1925 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
1926 req
->async
.callback
= wine_server_client_ptr( WS2_async_shutdown
);
1927 req
->async
.iosb
= wine_server_client_ptr( &wsa
->local_iosb
);
1928 req
->async
.arg
= wine_server_client_ptr( wsa
);
1929 req
->async
.cvalue
= 0;
1930 status
= wine_server_call( req
);
1934 if (status
!= STATUS_PENDING
)
1936 HeapFree( GetProcessHeap(), 0, wsa
);
1937 return NtStatusToWSAError( status
);
1942 /***********************************************************************
1945 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
,
1952 TRACE("socket %04lx\n", s
);
1953 is_blocking
= _is_blocking(s
);
1956 /* try accepting first (if there is a deferred connection) */
1957 SERVER_START_REQ( accept_socket
)
1959 req
->lhandle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1960 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
1961 req
->attributes
= OBJ_INHERIT
;
1962 status
= wine_server_call( req
);
1963 as
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
1968 if (addr
) WS_getpeername(as
, addr
, addrlen32
);
1971 if (is_blocking
&& status
== STATUS_CANT_WAIT
)
1973 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1975 do_block(fd
, POLLIN
, -1);
1976 _sync_sock_state(s
); /* let wineserver notice connection */
1977 release_sock_fd( s
, fd
);
1979 } while (is_blocking
&& status
== STATUS_CANT_WAIT
);
1982 return INVALID_SOCKET
;
1985 /***********************************************************************
1988 static BOOL WINAPI
WS2_AcceptEx(SOCKET listener
, SOCKET acceptor
, PVOID dest
, DWORD dest_len
,
1989 DWORD local_addr_len
, DWORD rem_addr_len
, LPDWORD received
,
1990 LPOVERLAPPED overlapped
)
1993 struct ws2_accept_async
*wsa
;
1995 ULONG_PTR cvalue
= (overlapped
&& ((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)overlapped
: 0;
1997 TRACE("(%lx, %lx, %p, %d, %d, %d, %p, %p)\n", listener
, acceptor
, dest
, dest_len
, local_addr_len
,
1998 rem_addr_len
, received
, overlapped
);
2002 SetLastError(WSAEINVAL
);
2008 SetLastError(WSA_INVALID_PARAMETER
);
2012 fd
= get_sock_fd( listener
, FILE_READ_DATA
, NULL
);
2015 SetLastError(WSAENOTSOCK
);
2018 release_sock_fd( listener
, fd
);
2020 fd
= get_sock_fd( acceptor
, FILE_READ_DATA
, NULL
);
2023 SetLastError(WSAEINVAL
);
2026 release_sock_fd( acceptor
, fd
);
2028 wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
2031 SetLastError(WSAEFAULT
);
2035 wsa
->listen_socket
= SOCKET2HANDLE(listener
);
2036 wsa
->accept_socket
= SOCKET2HANDLE(acceptor
);
2037 wsa
->user_overlapped
= overlapped
;
2038 wsa
->cvalue
= cvalue
;
2040 wsa
->data_len
= dest_len
;
2041 wsa
->local_len
= local_addr_len
;
2042 wsa
->remote_len
= rem_addr_len
;
2047 /* set up a read request if we need it */
2048 wsa
->read
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[1]) );
2051 HeapFree( GetProcessHeap(), 0, wsa
);
2052 SetLastError(WSAEFAULT
);
2056 wsa
->read
->hSocket
= wsa
->accept_socket
;
2057 wsa
->read
->flags
= 0;
2058 wsa
->read
->lpFlags
= &wsa
->read
->flags
;
2059 wsa
->read
->addr
= NULL
;
2060 wsa
->read
->addrlen
.ptr
= NULL
;
2061 wsa
->read
->control
= NULL
;
2062 wsa
->read
->n_iovecs
= 1;
2063 wsa
->read
->first_iovec
= 0;
2064 wsa
->read
->iovec
[0].iov_base
= wsa
->buf
;
2065 wsa
->read
->iovec
[0].iov_len
= wsa
->data_len
;
2068 SERVER_START_REQ( register_async
)
2070 req
->type
= ASYNC_TYPE_READ
;
2071 req
->async
.handle
= wine_server_obj_handle( SOCKET2HANDLE(listener
) );
2072 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept
);
2073 req
->async
.iosb
= wine_server_client_ptr( overlapped
);
2074 req
->async
.arg
= wine_server_client_ptr( wsa
);
2075 req
->async
.cvalue
= cvalue
;
2076 /* We don't set event since we may also have to read */
2077 status
= wine_server_call( req
);
2081 if(status
!= STATUS_PENDING
)
2083 HeapFree( GetProcessHeap(), 0, wsa
->read
);
2084 HeapFree( GetProcessHeap(), 0, wsa
);
2087 SetLastError( NtStatusToWSAError(status
) );
2091 /***********************************************************************
2092 * GetAcceptExSockaddrs
2094 static void WINAPI
WS2_GetAcceptExSockaddrs(PVOID buffer
, DWORD data_size
, DWORD local_size
, DWORD remote_size
,
2095 struct WS_sockaddr
**local_addr
, LPINT local_addr_len
,
2096 struct WS_sockaddr
**remote_addr
, LPINT remote_addr_len
)
2098 char *cbuf
= buffer
;
2099 TRACE("(%p, %d, %d, %d, %p, %p, %p, %p)\n", buffer
, data_size
, local_size
, remote_size
, local_addr
,
2100 local_addr_len
, remote_addr
, remote_addr_len
);
2103 *local_addr_len
= *(int *) cbuf
;
2104 *local_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
2108 *remote_addr_len
= *(int *) cbuf
;
2109 *remote_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
2112 /***********************************************************************
2115 * Perform a receive operation that is capable of returning message
2116 * control headers. It is important to note that the WSAMSG parameter
2117 * must remain valid throughout the operation, even when an overlapped
2118 * receive is performed.
2120 static int WINAPI
WS2_WSARecvMsg( SOCKET s
, LPWSAMSG msg
, LPDWORD lpNumberOfBytesRecvd
,
2121 LPWSAOVERLAPPED lpOverlapped
,
2122 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2126 SetLastError( WSAEFAULT
);
2127 return SOCKET_ERROR
;
2130 return WS2_recv_base( s
, msg
->lpBuffers
, msg
->dwBufferCount
, lpNumberOfBytesRecvd
,
2131 &msg
->dwFlags
, msg
->name
, &msg
->namelen
,
2132 lpOverlapped
, lpCompletionRoutine
, &msg
->Control
);
2135 /***********************************************************************
2136 * interface_bind (INTERNAL)
2138 * Take bind() calls on any name corresponding to a local network adapter and restrict the given socket to
2139 * operating only on the specified interface. This restriction consists of two components:
2140 * 1) An outgoing packet restriction suggesting the egress interface for all packets.
2141 * 2) An incoming packet restriction dropping packets not meant for the interface.
2142 * If the function succeeds in placing these restrictions (returns TRUE) then the name for the bind() may
2143 * safely be changed to INADDR_ANY, permitting the transmission and receipt of broadcast packets on the
2144 * socket. This behavior is only relevant to UDP sockets and is needed for applications that expect to be able
2145 * to receive broadcast packets on a socket that is bound to a specific network interface.
2147 static BOOL
interface_bind( SOCKET s
, int fd
, struct sockaddr
*addr
)
2149 struct sockaddr_in
*in_sock
= (struct sockaddr_in
*) addr
;
2150 unsigned int sock_type
= 0;
2151 socklen_t optlen
= sizeof(sock_type
);
2152 in_addr_t bind_addr
= in_sock
->sin_addr
.s_addr
;
2153 PIP_ADAPTER_INFO adapters
= NULL
, adapter
;
2158 if (bind_addr
== htonl(WS_INADDR_ANY
) || bind_addr
== htonl(WS_INADDR_LOOPBACK
))
2159 return FALSE
; /* Not binding to a network adapter, special interface binding unnecessary. */
2160 if (getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &sock_type
, &optlen
) == -1 || sock_type
!= SOCK_DGRAM
)
2161 return FALSE
; /* Special interface binding is only necessary for UDP datagrams. */
2162 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
2164 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
2165 if (adapters
== NULL
|| GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
2167 /* Search the IPv4 adapter list for the appropriate binding interface */
2168 for (adapter
= adapters
; adapter
!= NULL
; adapter
= adapter
->Next
)
2170 in_addr_t adapter_addr
= (in_addr_t
) inet_addr(adapter
->IpAddressList
.IpAddress
.String
);
2172 if (bind_addr
== adapter_addr
)
2174 #if defined(IP_BOUND_IF)
2175 /* IP_BOUND_IF sets both the incoming and outgoing restriction at once */
2176 if (setsockopt(fd
, IPPROTO_IP
, IP_BOUND_IF
, &adapter
->Index
, sizeof(adapter
->Index
)) != 0)
2179 #elif defined(LINUX_BOUND_IF)
2180 in_addr_t ifindex
= (in_addr_t
) htonl(adapter
->Index
);
2181 struct interface_filter specific_interface_filter
;
2182 struct sock_fprog filter_prog
;
2184 if (setsockopt(fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
)) != 0)
2185 goto cleanup
; /* Failed to suggest egress interface */
2186 specific_interface_filter
= generic_interface_filter
;
2187 specific_interface_filter
.iface_rule
.k
= adapter
->Index
;
2188 filter_prog
.len
= sizeof(generic_interface_filter
)/sizeof(struct sock_filter
);
2189 filter_prog
.filter
= (struct sock_filter
*) &specific_interface_filter
;
2190 if (setsockopt(fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter_prog
, sizeof(filter_prog
)) != 0)
2191 goto cleanup
; /* Failed to specify incoming packet filter */
2194 FIXME("Broadcast packets on interface-bound sockets are not currently supported on this platform, "
2195 "receiving broadcast packets will not work on socket %04lx.\n", s
);
2200 /* Will soon be switching to INADDR_ANY: permit address reuse */
2201 if (ret
&& setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
)) == 0)
2202 TRACE("Socket %04lx bound to interface index %d\n", s
, adapter
->Index
);
2208 ERR("Failed to bind to interface, receiving broadcast packets will not work on socket %04lx.\n", s
);
2209 HeapFree(GetProcessHeap(), 0, adapters
);
2213 /***********************************************************************
2216 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
2218 int fd
= get_sock_fd( s
, 0, NULL
);
2219 int res
= SOCKET_ERROR
;
2221 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
2225 if (!name
|| (name
->sa_family
&& !supported_pf(name
->sa_family
)))
2227 SetLastError(WSAEAFNOSUPPORT
);
2231 union generic_unix_sockaddr uaddr
;
2232 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
2235 SetLastError(WSAEFAULT
);
2240 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) &uaddr
;
2241 if (name
->sa_family
== WS_AF_INET6
&&
2242 !memcmp(&in6
->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
)))
2245 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
)) == -1)
2247 release_sock_fd( s
, fd
);
2248 SetLastError(WSAEAFNOSUPPORT
);
2249 return SOCKET_ERROR
;
2253 if (name
->sa_family
== WS_AF_INET
)
2255 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
2256 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
2258 /* Trying to bind to the default host interface, using
2259 * INADDR_ANY instead*/
2260 WARN("Trying to bind to magic IP address, using "
2261 "INADDR_ANY instead.\n");
2262 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_ANY
);
2264 else if (interface_bind(s
, fd
, &uaddr
.addr
))
2265 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_ANY
);
2267 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
2269 int loc_errno
= errno
;
2270 WARN("\tfailure - errno = %i\n", errno
);
2275 SetLastError(WSAENOTSOCK
);
2278 SetLastError(WSAEINVAL
);
2281 SetLastError(wsaErrno());
2287 res
=0; /* success */
2291 release_sock_fd( s
, fd
);
2296 /***********************************************************************
2297 * closesocket (WS2_32.3)
2299 int WINAPI
WS_closesocket(SOCKET s
)
2301 TRACE("socket %04lx\n", s
);
2302 if (CloseHandle(SOCKET2HANDLE(s
))) return 0;
2303 return SOCKET_ERROR
;
2306 static int do_connect(int fd
, const struct WS_sockaddr
* name
, int namelen
)
2308 union generic_unix_sockaddr uaddr
;
2309 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
2314 if (name
->sa_family
== WS_AF_INET
)
2316 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
2317 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
2319 /* Trying to connect to magic replace-loopback address,
2320 * assuming we really want to connect to localhost */
2321 TRACE("Trying to connect to magic IP address, using "
2322 "INADDR_LOOPBACK instead.\n");
2323 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_LOOPBACK
);
2327 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
2333 /***********************************************************************
2334 * connect (WS2_32.4)
2336 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
2338 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2340 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
2344 int ret
= do_connect(fd
, name
, namelen
);
2346 goto connect_success
;
2348 if (ret
== WSAEINPROGRESS
)
2350 /* tell wineserver that a connection is in progress */
2351 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2353 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
2354 if (_is_blocking(s
))
2358 do_block(fd
, POLLIN
| POLLOUT
, -1);
2359 _sync_sock_state(s
); /* let wineserver notice connection */
2360 /* retrieve any error codes from it */
2361 result
= _get_sock_error(s
, FD_CONNECT_BIT
);
2363 SetLastError(NtStatusToWSAError(result
));
2366 goto connect_success
;
2371 SetLastError(WSAEWOULDBLOCK
);
2378 release_sock_fd( s
, fd
);
2380 return SOCKET_ERROR
;
2383 release_sock_fd( s
, fd
);
2384 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2385 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
2386 FD_CONNECT
|FD_WINE_LISTENING
);
2390 /***********************************************************************
2391 * WSAConnect (WS2_32.30)
2393 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
2394 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
2395 LPQOS lpSQOS
, LPQOS lpGQOS
)
2397 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
2398 FIXME("unsupported parameters!\n");
2399 return WS_connect( s
, name
, namelen
);
2402 /***********************************************************************
2405 static BOOL WINAPI
WS2_ConnectEx(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
2406 PVOID sendBuf
, DWORD sendBufLen
, LPDWORD sent
, LPOVERLAPPED ov
)
2408 int fd
, ret
, status
;
2412 SetLastError( ERROR_INVALID_PARAMETER
);
2416 fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2419 SetLastError( WSAENOTSOCK
);
2423 TRACE("socket %04lx, ptr %p %s, length %d, sendptr %p, len %d, ov %p\n",
2424 s
, name
, debugstr_sockaddr(name
), namelen
, sendBuf
, sendBufLen
, ov
);
2426 /* FIXME: technically the socket has to be bound */
2427 ret
= do_connect(fd
, name
, namelen
);
2432 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2433 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
2434 FD_CONNECT
|FD_WINE_LISTENING
);
2436 wsabuf
.len
= sendBufLen
;
2437 wsabuf
.buf
= (char*) sendBuf
;
2439 /* WSASend takes care of completion if need be */
2440 if (WSASend(s
, &wsabuf
, sendBuf
? 1 : 0, sent
, 0, ov
, NULL
) != SOCKET_ERROR
)
2441 goto connection_success
;
2443 else if (ret
== WSAEINPROGRESS
)
2445 struct ws2_async
*wsa
;
2446 ULONG_PTR cvalue
= (((ULONG_PTR
)ov
->hEvent
& 1) == 0) ? (ULONG_PTR
)ov
: 0;
2448 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2450 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
2452 /* Indirectly call WSASend */
2453 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( struct ws2_async
, iovec
[1] ))))
2455 SetLastError(WSAEFAULT
);
2459 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)ov
;
2460 iosb
->u
.Status
= STATUS_PENDING
;
2461 iosb
->Information
= 0;
2463 wsa
->hSocket
= SOCKET2HANDLE(s
);
2465 wsa
->addrlen
.val
= 0;
2467 wsa
->lpFlags
= &wsa
->flags
;
2468 wsa
->control
= NULL
;
2469 wsa
->n_iovecs
= sendBuf
? 1 : 0;
2470 wsa
->first_iovec
= 0;
2471 wsa
->completion_func
= NULL
;
2472 wsa
->iovec
[0].iov_base
= sendBuf
;
2473 wsa
->iovec
[0].iov_len
= sendBufLen
;
2475 SERVER_START_REQ( register_async
)
2477 req
->type
= ASYNC_TYPE_WRITE
;
2478 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
2479 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
2480 req
->async
.iosb
= wine_server_client_ptr( iosb
);
2481 req
->async
.arg
= wine_server_client_ptr( wsa
);
2482 req
->async
.event
= wine_server_obj_handle( ov
->hEvent
);
2483 req
->async
.cvalue
= cvalue
;
2484 status
= wine_server_call( req
);
2488 if (status
!= STATUS_PENDING
) HeapFree(GetProcessHeap(), 0, wsa
);
2490 /* If the connect already failed */
2491 if (status
== STATUS_PIPE_DISCONNECTED
)
2492 status
= _get_sock_error(s
, FD_CONNECT_BIT
);
2493 SetLastError( NtStatusToWSAError(status
) );
2501 release_sock_fd( s
, fd
);
2505 release_sock_fd( s
, fd
);
2510 /***********************************************************************
2511 * getpeername (WS2_32.5)
2513 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
2518 TRACE("socket: %04lx, ptr %p, len %08x\n", s
, name
, namelen
? *namelen
: 0);
2520 fd
= get_sock_fd( s
, 0, NULL
);
2525 union generic_unix_sockaddr uaddr
;
2526 socklen_t uaddrlen
= sizeof(uaddr
);
2528 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) == 0)
2530 if (!name
|| !namelen
)
2531 SetLastError(WSAEFAULT
);
2532 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
2533 /* The buffer was too small */
2534 SetLastError(WSAEFAULT
);
2539 SetLastError(wsaErrno());
2540 release_sock_fd( s
, fd
);
2545 /***********************************************************************
2546 * getsockname (WS2_32.6)
2548 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
2553 TRACE("socket: %04lx, ptr %p, len %08x\n", s
, name
, namelen
? *namelen
: 0);
2555 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
2556 if( (name
== NULL
) || (namelen
== NULL
) )
2558 SetLastError( WSAEFAULT
);
2559 return SOCKET_ERROR
;
2562 fd
= get_sock_fd( s
, 0, NULL
);
2567 union generic_unix_sockaddr uaddr
;
2568 socklen_t uaddrlen
= sizeof(uaddr
);
2570 if (getsockname(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
2572 SetLastError(wsaErrno());
2574 else if (!is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
2576 SetLastError(WSAEINVAL
);
2578 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
2580 /* The buffer was too small */
2581 SetLastError(WSAEFAULT
);
2587 release_sock_fd( s
, fd
);
2592 /***********************************************************************
2593 * getsockopt (WS2_32.7)
2595 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
2596 INT optname
, char *optval
, INT
*optlen
)
2601 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
2602 s
, level
, optname
, optval
, *optlen
);
2610 /* Handle common cases. The special cases are below, sorted
2612 case WS_SO_ACCEPTCONN
:
2613 case WS_SO_BROADCAST
:
2616 case WS_SO_KEEPALIVE
:
2617 case WS_SO_OOBINLINE
:
2619 case WS_SO_REUSEADDR
:
2622 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2623 return SOCKET_ERROR
;
2624 convert_sockopt(&level
, &optname
);
2625 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
2627 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2630 release_sock_fd( s
, fd
);
2633 case WS_SO_DONTLINGER
:
2635 struct linger lingval
;
2636 socklen_t len
= sizeof(struct linger
);
2638 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
2640 SetLastError(WSAEFAULT
);
2641 return SOCKET_ERROR
;
2643 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2644 return SOCKET_ERROR
;
2646 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
2648 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2653 *(BOOL
*)optval
= !lingval
.l_onoff
;
2654 *optlen
= sizeof(BOOL
);
2657 release_sock_fd( s
, fd
);
2661 case WS_SO_CONNECT_TIME
:
2663 static int pretendtime
= 0;
2664 struct WS_sockaddr addr
;
2665 int len
= sizeof(addr
);
2667 if (!optlen
|| *optlen
< sizeof(DWORD
) || !optval
)
2669 SetLastError(WSAEFAULT
);
2670 return SOCKET_ERROR
;
2672 if (WS_getpeername(s
, &addr
, &len
) == SOCKET_ERROR
)
2673 *(DWORD
*)optval
= ~0u;
2676 if (!pretendtime
) FIXME("WS_SO_CONNECT_TIME - faking results\n");
2677 *(DWORD
*)optval
= pretendtime
++;
2679 *optlen
= sizeof(DWORD
);
2682 /* As mentioned in setsockopt, Windows ignores this, so we
2683 * always return true here */
2684 case WS_SO_DONTROUTE
:
2685 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
2687 SetLastError(WSAEFAULT
);
2688 return SOCKET_ERROR
;
2690 *(BOOL
*)optval
= TRUE
;
2691 *optlen
= sizeof(BOOL
);
2696 struct linger lingval
;
2698 socklen_t len
= sizeof(struct linger
), slen
= sizeof(int);
2700 /* struct linger and LINGER have different sizes */
2701 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
2703 SetLastError(WSAEFAULT
);
2704 return SOCKET_ERROR
;
2706 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2707 return SOCKET_ERROR
;
2709 if ((getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &so_type
, &slen
) == 0 && so_type
== SOCK_DGRAM
))
2711 SetLastError(WSAENOPROTOOPT
);
2714 else if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0)
2716 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2721 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
2722 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
2723 *optlen
= sizeof(struct linger
);
2726 release_sock_fd( s
, fd
);
2730 case WS_SO_MAX_MSG_SIZE
:
2731 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
2733 SetLastError(WSAEFAULT
);
2734 return SOCKET_ERROR
;
2736 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
2737 *(int *)optval
= 65507;
2738 *optlen
= sizeof(int);
2741 /* SO_OPENTYPE does not require a valid socket handle. */
2742 case WS_SO_OPENTYPE
:
2743 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
2745 SetLastError(WSAEFAULT
);
2746 return SOCKET_ERROR
;
2748 *(int *)optval
= get_per_thread_data()->opentype
;
2749 *optlen
= sizeof(int);
2750 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
2754 case WS_SO_RCVTIMEO
:
2757 case WS_SO_SNDTIMEO
:
2759 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
2762 socklen_t len
= sizeof(struct timeval
);
2764 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
2766 SetLastError(WSAEFAULT
);
2767 return SOCKET_ERROR
;
2769 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2770 return SOCKET_ERROR
;
2772 convert_sockopt(&level
, &optname
);
2773 if (getsockopt(fd
, level
, optname
, &tv
, &len
) != 0 )
2775 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2780 *(int *)optval
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
2781 *optlen
= sizeof(int);
2784 release_sock_fd( s
, fd
);
2789 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
2790 SetLastError(WSAENOPROTOOPT
);
2791 return SOCKET_ERROR
;
2792 } /* end switch(optname) */
2793 }/* end case WS_SOL_SOCKET */
2797 struct WS_sockaddr_ipx addr
;
2798 IPX_ADDRESS_DATA
*data
;
2803 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
2805 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (socklen_t
*)optlen
) == -1)
2812 socklen_t len
=sizeof(struct ipx
);
2813 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
2816 *optval
= (int)val
.ipx_pt
;
2819 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
2820 release_sock_fd( s
, fd
);
2825 * On a Win2000 system with one network card there are usually
2826 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
2827 * Using this call you can then retrieve info about this all.
2828 * In case of Linux it is a bit different. Usually you have
2829 * only "one" device active and further it is not possible to
2830 * query things like the linkspeed.
2832 FIXME("IPX_ADDRESS\n");
2833 namelen
= sizeof(struct WS_sockaddr_ipx
);
2834 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
2835 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
2837 data
= (IPX_ADDRESS_DATA
*)optval
;
2838 memcpy(data
->nodenum
,addr
.sa_nodenum
,sizeof(data
->nodenum
));
2839 memcpy(data
->netnum
,addr
.sa_netnum
,sizeof(data
->netnum
));
2840 data
->adapternum
= 0;
2841 data
->wan
= FALSE
; /* We are not on a wan for now .. */
2842 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
2843 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
2844 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
2845 * note 1MB = 1000kB in this case */
2848 case IPX_MAX_ADAPTER_NUM
:
2849 FIXME("IPX_MAX_ADAPTER_NUM\n");
2850 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
2854 FIXME("IPX optname:%x\n", optname
);
2855 return SOCKET_ERROR
;
2856 }/* end switch(optname) */
2857 } /* end case NSPROTO_IPX */
2864 case WS_IRLMP_ENUMDEVICES
:
2866 static const int MAX_IRDA_DEVICES
= 10;
2867 char buf
[sizeof(struct irda_device_list
) +
2868 (MAX_IRDA_DEVICES
- 1) * sizeof(struct irda_device_info
)];
2870 socklen_t len
= sizeof(buf
);
2872 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2873 return SOCKET_ERROR
;
2874 res
= getsockopt( fd
, SOL_IRLMP
, IRLMP_ENUMDEVICES
, buf
, &len
);
2877 SetLastError(wsaErrno());
2878 return SOCKET_ERROR
;
2882 struct irda_device_list
*src
= (struct irda_device_list
*)buf
;
2883 DEVICELIST
*dst
= (DEVICELIST
*)optval
;
2884 INT needed
= sizeof(DEVICELIST
), i
;
2887 needed
+= (src
->len
- 1) * sizeof(IRDA_DEVICE_INFO
);
2888 if (*optlen
< needed
)
2890 SetLastError(WSAEFAULT
);
2891 return SOCKET_ERROR
;
2894 TRACE("IRLMP_ENUMDEVICES: %d devices found:\n", src
->len
);
2895 dst
->numDevice
= src
->len
;
2896 for (i
= 0; i
< src
->len
; i
++)
2898 TRACE("saddr = %08x, daddr = %08x, info = %s, hints = %02x%02x\n",
2899 src
->dev
[i
].saddr
, src
->dev
[i
].daddr
,
2900 src
->dev
[i
].info
, src
->dev
[i
].hints
[0],
2901 src
->dev
[i
].hints
[1]);
2902 memcpy( dst
->Device
[i
].irdaDeviceID
,
2904 sizeof(dst
->Device
[i
].irdaDeviceID
) ) ;
2905 memcpy( dst
->Device
[i
].irdaDeviceName
,
2907 sizeof(dst
->Device
[i
].irdaDeviceName
) ) ;
2908 memcpy( &dst
->Device
[i
].irdaDeviceHints1
,
2909 &src
->dev
[i
].hints
[0],
2910 sizeof(dst
->Device
[i
].irdaDeviceHints1
) ) ;
2911 memcpy( &dst
->Device
[i
].irdaDeviceHints2
,
2912 &src
->dev
[i
].hints
[1],
2913 sizeof(dst
->Device
[i
].irdaDeviceHints2
) ) ;
2914 dst
->Device
[i
].irdaCharSet
= src
->dev
[i
].charset
;
2920 FIXME("IrDA optname:0x%x\n", optname
);
2921 return SOCKET_ERROR
;
2923 break; /* case WS_SOL_IRLMP */
2926 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
2927 case WS_IPPROTO_TCP
:
2930 case WS_TCP_NODELAY
:
2931 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2932 return SOCKET_ERROR
;
2933 convert_sockopt(&level
, &optname
);
2934 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
2936 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2939 release_sock_fd( s
, fd
);
2942 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
2943 return SOCKET_ERROR
;
2948 case WS_IP_ADD_MEMBERSHIP
:
2949 case WS_IP_DROP_MEMBERSHIP
:
2953 case WS_IP_MULTICAST_IF
:
2954 case WS_IP_MULTICAST_LOOP
:
2955 case WS_IP_MULTICAST_TTL
:
2962 #ifdef IP_UNICAST_IF
2963 case WS_IP_UNICAST_IF
:
2965 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2966 return SOCKET_ERROR
;
2967 convert_sockopt(&level
, &optname
);
2968 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
2970 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2973 release_sock_fd( s
, fd
);
2975 case WS_IP_DONTFRAGMENT
:
2976 FIXME("WS_IP_DONTFRAGMENT is always false!\n");
2977 *(BOOL
*)optval
= FALSE
;
2980 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
2981 return SOCKET_ERROR
;
2983 case WS_IPPROTO_IPV6
:
2986 #ifdef IPV6_ADD_MEMBERSHIP
2987 case WS_IPV6_ADD_MEMBERSHIP
:
2989 #ifdef IPV6_DROP_MEMBERSHIP
2990 case WS_IPV6_DROP_MEMBERSHIP
:
2992 case WS_IPV6_MULTICAST_IF
:
2993 case WS_IPV6_MULTICAST_HOPS
:
2994 case WS_IPV6_MULTICAST_LOOP
:
2995 case WS_IPV6_UNICAST_HOPS
:
2996 case WS_IPV6_V6ONLY
:
2997 #ifdef IPV6_UNICAST_IF
2998 case WS_IPV6_UNICAST_IF
:
3000 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
3001 return SOCKET_ERROR
;
3002 convert_sockopt(&level
, &optname
);
3003 if (getsockopt(fd
, level
, optname
, optval
, (socklen_t
*)optlen
) != 0 )
3005 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3008 release_sock_fd( s
, fd
);
3010 case WS_IPV6_DONTFRAG
:
3011 FIXME("WS_IPV6_DONTFRAG is always false!\n");
3012 *(BOOL
*)optval
= FALSE
;
3015 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
3016 return SOCKET_ERROR
;
3019 WARN("Unknown level: 0x%08x\n", level
);
3020 SetLastError(WSAEINVAL
);
3021 return SOCKET_ERROR
;
3022 } /* end switch(level) */
3025 /***********************************************************************
3028 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
3030 return htonl(hostlong
);
3034 /***********************************************************************
3037 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
3039 return htons(hostshort
);
3042 /***********************************************************************
3043 * WSAHtonl (WS2_32.46)
3044 * From MSDN description of error codes, this function should also
3045 * check if WinSock has been initialized and the socket is a valid
3046 * socket. But why? This function only translates a host byte order
3047 * u_long into a network byte order u_long...
3049 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
3053 *lpnetlong
= htonl(hostlong
);
3056 WSASetLastError(WSAEFAULT
);
3057 return SOCKET_ERROR
;
3060 /***********************************************************************
3061 * WSAHtons (WS2_32.47)
3062 * From MSDN description of error codes, this function should also
3063 * check if WinSock has been initialized and the socket is a valid
3064 * socket. But why? This function only translates a host byte order
3065 * u_short into a network byte order u_short...
3067 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
3072 *lpnetshort
= htons(hostshort
);
3075 WSASetLastError(WSAEFAULT
);
3076 return SOCKET_ERROR
;
3080 /***********************************************************************
3081 * inet_addr (WS2_32.11)
3083 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
3085 if (!cp
) return INADDR_NONE
;
3086 return inet_addr(cp
);
3090 /***********************************************************************
3093 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
3095 return ntohl(netlong
);
3099 /***********************************************************************
3102 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
3104 return ntohs(netshort
);
3108 /***********************************************************************
3109 * inet_ntoa (WS2_32.12)
3111 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
3113 /* use "buffer for dummies" here because some applications have a
3114 * propensity to decode addresses in ws_hostent structure without
3115 * saving them first...
3117 static char dbuffer
[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
3119 char* s
= inet_ntoa(*((struct in_addr
*)&in
));
3125 SetLastError(wsaErrno());
3129 static const char *debugstr_wsaioctl(DWORD ioctl
)
3131 const char *buf_type
, *family
;
3133 switch(ioctl
& 0x18000000)
3138 case WS_IOC_PROTOCOL
:
3139 family
= "IOC_PROTOCOL";
3142 family
= "IOC_VENDOR";
3144 default: /* WS_IOC_UNIX */
3146 BYTE size
= (ioctl
>> 16) & WS_IOCPARM_MASK
;
3147 char x
= (ioctl
& 0xff00) >> 8;
3148 BYTE y
= ioctl
& 0xff;
3151 switch (ioctl
& (WS_IOC_VOID
|WS_IOC_INOUT
))
3155 sprintf(args
, "%d, %d", x
, y
);
3159 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3163 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3167 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3170 return wine_dbg_sprintf("%s(%s)", buf_type
, args
);
3174 /* We are different from WS_IOC_UNIX. */
3175 switch (ioctl
& (WS_IOC_VOID
|WS_IOC_INOUT
))
3178 buf_type
= "_WSAIO";
3181 buf_type
= "_WSAIORW";
3184 buf_type
= "_WSAIOW";
3187 buf_type
= "_WSAIOR";
3194 return wine_dbg_sprintf("%s(%s, %d)", buf_type
, family
,
3195 (USHORT
)(ioctl
& 0xffff));
3198 /**********************************************************************
3199 * WSAIoctl (WS2_32.50)
3202 INT WINAPI
WSAIoctl(SOCKET s
, DWORD code
, LPVOID in_buff
, DWORD in_size
, LPVOID out_buff
,
3203 DWORD out_size
, LPDWORD ret_size
, LPWSAOVERLAPPED overlapped
,
3204 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
3207 DWORD status
= 0, total
= 0;
3209 TRACE("%ld, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
3210 s
, code
, in_buff
, in_size
, out_buff
, out_size
, ret_size
, overlapped
, completion
);
3215 if (in_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(in_buff
))
3217 WSASetLastError(WSAEFAULT
);
3218 return SOCKET_ERROR
;
3220 if (_get_sock_mask(s
))
3222 /* AsyncSelect()'ed sockets are always nonblocking */
3223 if (!*(WS_u_long
*)in_buff
) status
= WSAEINVAL
;
3226 if (*(WS_u_long
*)in_buff
)
3227 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
3229 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
3234 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
3236 WSASetLastError(WSAEFAULT
);
3237 return SOCKET_ERROR
;
3239 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
3240 if (ioctl(fd
, FIONREAD
, out_buff
) == -1)
3241 status
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
3242 release_sock_fd( s
, fd
);
3248 unsigned int oob
= 0, atmark
= 0;
3249 socklen_t oobsize
= sizeof(int);
3250 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
3252 WSASetLastError(WSAEFAULT
);
3253 return SOCKET_ERROR
;
3255 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
3256 /* SO_OOBINLINE sockets must always return TRUE to SIOCATMARK */
3257 if ((getsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &oob
, &oobsize
) == -1)
3258 || (!oob
&& ioctl(fd
, SIOCATMARK
, &atmark
) == -1))
3259 status
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
3262 /* The SIOCATMARK value read from ioctl() is reversed
3263 * because BSD returns TRUE if it's in the OOB mark
3264 * while Windows returns TRUE if there are NO OOB bytes.
3266 (*(WS_u_long
*) out_buff
) = oob
| !atmark
;
3269 release_sock_fd( s
, fd
);
3274 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
3275 SetLastError(WSAEINVAL
);
3276 return SOCKET_ERROR
;
3278 case WS_SIO_GET_INTERFACE_LIST
:
3280 INTERFACE_INFO
* intArray
= out_buff
;
3281 DWORD size
, numInt
= 0, apiReturn
;
3283 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
3285 if (!out_buff
|| !ret_size
)
3287 WSASetLastError(WSAEFAULT
);
3288 return SOCKET_ERROR
;
3291 fd
= get_sock_fd( s
, 0, NULL
);
3292 if (fd
== -1) return SOCKET_ERROR
;
3294 apiReturn
= GetAdaptersInfo(NULL
, &size
);
3295 if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
3297 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
3301 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
3303 PIP_ADAPTER_INFO ptr
;
3305 for (ptr
= table
, numInt
= 0; ptr
; ptr
= ptr
->Next
)
3307 unsigned int addr
, mask
, bcast
;
3308 struct ifreq ifInfo
;
3310 /* Skip interfaces without an IPv4 address. */
3311 if (ptr
->IpAddressList
.IpAddress
.String
[0] == '\0')
3314 if ((numInt
+ 1)*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > out_size
)
3316 WARN("Buffer too small = %u, out_size = %u\n", numInt
+ 1, out_size
);
3321 /* Socket Status Flags */
3322 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
3323 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
3325 ERR("Error obtaining status flags for socket!\n");
3331 /* set flags; the values of IFF_* are not the same
3332 under Linux and Windows, therefore must generate
3334 intArray
->iiFlags
= 0;
3335 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
3336 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
3337 #ifdef IFF_POINTOPOINT
3338 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
3339 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
3341 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
3342 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
3343 if (ifInfo
.ifr_flags
& IFF_UP
)
3344 intArray
->iiFlags
|= WS_IFF_UP
;
3345 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
3346 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
3349 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
3350 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
3351 bcast
= addr
| ~mask
;
3352 intArray
->iiAddress
.AddressIn
.sin_family
= AF_INET
;
3353 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
3354 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
=
3356 intArray
->iiNetmask
.AddressIn
.sin_family
= AF_INET
;
3357 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
3358 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
=
3360 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
=
3362 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
3363 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.
3371 ERR("Unable to get interface table!\n");
3374 HeapFree(GetProcessHeap(),0,table
);
3376 else status
= WSAEINVAL
;
3378 else if (apiReturn
!= ERROR_NO_DATA
)
3380 ERR("Unable to get interface table!\n");
3383 /* Calculate the size of the array being returned */
3384 total
= sizeof(INTERFACE_INFO
) * numInt
;
3385 release_sock_fd( s
, fd
);
3389 case WS_SIO_ADDRESS_LIST_CHANGE
:
3390 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
3391 /* FIXME: error and return code depend on whether socket was created
3392 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
3395 case WS_SIO_ADDRESS_LIST_QUERY
:
3399 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
3403 WSASetLastError(WSAEFAULT
);
3404 return SOCKET_ERROR
;
3407 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
3409 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
3412 if (!table
|| GetAdaptersInfo(table
, &size
))
3414 HeapFree(GetProcessHeap(), 0, table
);
3419 for (p
= table
, num
= 0; p
; p
= p
->Next
)
3420 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
3422 total
= sizeof(SOCKET_ADDRESS_LIST
) + sizeof(SOCKET_ADDRESS
) * (num
- 1);
3423 total
+= sizeof(SOCKADDR
) * num
;
3425 if (total
> out_size
)
3427 HeapFree(GetProcessHeap(), 0, table
);
3436 SOCKET_ADDRESS_LIST
*sa_list
= out_buff
;
3437 SOCKADDR_IN
*sockaddr
;
3439 sa
= sa_list
->Address
;
3440 sockaddr
= (SOCKADDR_IN
*)((char *)sa
+ num
* sizeof(SOCKET_ADDRESS
));
3441 sa_list
->iAddressCount
= num
;
3443 for (p
= table
, i
= 0; p
; p
= p
->Next
)
3445 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
3447 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
3448 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
3450 sockaddr
[i
].sin_family
= AF_INET
;
3451 sockaddr
[i
].sin_port
= 0;
3452 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
3457 HeapFree(GetProcessHeap(), 0, table
);
3461 WARN("unable to get IP address list\n");
3468 FIXME("SIO_FLUSH: stub.\n");
3471 case WS_SIO_GET_EXTENSION_FUNCTION_POINTER
:
3473 static const GUID connectex_guid
= WSAID_CONNECTEX
;
3474 static const GUID disconnectex_guid
= WSAID_DISCONNECTEX
;
3475 static const GUID acceptex_guid
= WSAID_ACCEPTEX
;
3476 static const GUID getaccepexsockaddrs_guid
= WSAID_GETACCEPTEXSOCKADDRS
;
3477 static const GUID transmitfile_guid
= WSAID_TRANSMITFILE
;
3478 static const GUID transmitpackets_guid
= WSAID_TRANSMITPACKETS
;
3479 static const GUID wsarecvmsg_guid
= WSAID_WSARECVMSG
;
3480 static const GUID wsasendmsg_guid
= WSAID_WSASENDMSG
;
3482 if ( IsEqualGUID(&connectex_guid
, in_buff
) )
3484 *(LPFN_CONNECTEX
*)out_buff
= WS2_ConnectEx
;
3487 else if ( IsEqualGUID(&disconnectex_guid
, in_buff
) )
3489 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented DisconnectEx\n");
3491 else if ( IsEqualGUID(&acceptex_guid
, in_buff
) )
3493 *(LPFN_ACCEPTEX
*)out_buff
= WS2_AcceptEx
;
3496 else if ( IsEqualGUID(&getaccepexsockaddrs_guid
, in_buff
) )
3498 *(LPFN_GETACCEPTEXSOCKADDRS
*)out_buff
= WS2_GetAcceptExSockaddrs
;
3501 else if ( IsEqualGUID(&transmitfile_guid
, in_buff
) )
3503 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitFile\n");
3505 else if ( IsEqualGUID(&transmitpackets_guid
, in_buff
) )
3507 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitPackets\n");
3509 else if ( IsEqualGUID(&wsarecvmsg_guid
, in_buff
) )
3511 *(LPFN_WSARECVMSG
*)out_buff
= WS2_WSARecvMsg
;
3514 else if ( IsEqualGUID(&wsasendmsg_guid
, in_buff
) )
3516 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented WSASendMsg\n");
3519 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER %s: stub\n", debugstr_guid(in_buff
));
3521 status
= WSAEOPNOTSUPP
;
3524 case WS_SIO_KEEPALIVE_VALS
:
3526 struct tcp_keepalive
*k
;
3527 int keepalive
, keepidle
, keepintvl
;
3529 if (!in_buff
|| in_size
< sizeof(struct tcp_keepalive
))
3531 WSASetLastError(WSAEFAULT
);
3532 return SOCKET_ERROR
;
3536 keepalive
= k
->onoff
? 1 : 0;
3537 keepidle
= max( 1, (k
->keepalivetime
+ 500) / 1000 );
3538 keepintvl
= max( 1, (k
->keepaliveinterval
+ 500) / 1000 );
3540 TRACE("onoff: %d, keepalivetime: %d, keepaliveinterval: %d\n", keepalive
, keepidle
, keepintvl
);
3542 fd
= get_sock_fd(s
, 0, NULL
);
3543 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&keepalive
, sizeof(int)) == -1)
3545 #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL)
3546 /* these values need to be set only if SO_KEEPALIVE is enabled */
3549 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, (void *)&keepidle
, sizeof(int)) == -1)
3551 else if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, (void *)&keepintvl
, sizeof(int)) == -1)
3556 FIXME("ignoring keepalive interval and timeout\n");
3558 release_sock_fd(s
, fd
);
3561 case WS_SIO_ROUTING_INTERFACE_QUERY
:
3563 struct WS_sockaddr
*daddr
= (struct WS_sockaddr
*)in_buff
;
3564 struct WS_sockaddr_in
*daddr_in
= (struct WS_sockaddr_in
*)daddr
;
3565 struct WS_sockaddr_in
*saddr_in
= out_buff
;
3566 MIB_IPFORWARDROW row
;
3567 PMIB_IPADDRTABLE ipAddrTable
= NULL
;
3568 DWORD size
, i
, found_index
;
3570 TRACE("-> WS_SIO_ROUTING_INTERFACE_QUERY request\n");
3572 if (!in_buff
|| in_size
< sizeof(struct WS_sockaddr
) ||
3573 !out_buff
|| out_size
< sizeof(struct WS_sockaddr_in
) || !ret_size
)
3575 WSASetLastError(WSAEFAULT
);
3576 return SOCKET_ERROR
;
3578 if (daddr
->sa_family
!= AF_INET
)
3580 FIXME("unsupported address family %d\n", daddr
->sa_family
);
3581 status
= WSAEAFNOSUPPORT
;
3584 if (GetBestRoute(daddr_in
->sin_addr
.S_un
.S_addr
, 0, &row
) != NOERROR
||
3585 GetIpAddrTable(NULL
, &size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
3590 ipAddrTable
= HeapAlloc(GetProcessHeap(), 0, size
);
3591 if (GetIpAddrTable(ipAddrTable
, &size
, FALSE
))
3593 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3597 for (i
= 0, found_index
= ipAddrTable
->dwNumEntries
;
3598 i
< ipAddrTable
->dwNumEntries
; i
++)
3600 if (ipAddrTable
->table
[i
].dwIndex
== row
.dwForwardIfIndex
)
3603 if (found_index
== ipAddrTable
->dwNumEntries
)
3605 ERR("no matching IP address for interface %d\n",
3606 row
.dwForwardIfIndex
);
3607 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3611 saddr_in
->sin_family
= AF_INET
;
3612 saddr_in
->sin_addr
.S_un
.S_addr
= ipAddrTable
->table
[found_index
].dwAddr
;
3613 saddr_in
->sin_port
= 0;
3614 total
= sizeof(struct WS_sockaddr_in
);
3615 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3618 case WS_SIO_SET_COMPATIBILITY_MODE
:
3619 TRACE("WS_SIO_SET_COMPATIBILITY_MODE ignored\n");
3620 status
= WSAEOPNOTSUPP
;
3622 case WS_SIO_UDP_CONNRESET
:
3623 FIXME("WS_SIO_UDP_CONNRESET stub\n");
3625 case 0x667e: /* Netscape tries hard to use bogus ioctl 0x667e */
3626 WSASetLastError(WSAEOPNOTSUPP
);
3627 return SOCKET_ERROR
;
3629 FIXME("unsupported WS_IOCTL cmd (%s)\n", debugstr_wsaioctl(code
));
3630 status
= WSAEOPNOTSUPP
;
3636 FIXME( "completion routine %p not supported\n", completion
);
3638 else if (overlapped
)
3640 ULONG_PTR cvalue
= (overlapped
&& ((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)overlapped
: 0;
3641 overlapped
->Internal
= status
;
3642 overlapped
->InternalHigh
= total
;
3643 if (overlapped
->hEvent
) NtSetEvent( overlapped
->hEvent
, NULL
);
3644 if (cvalue
) WS_AddCompletion( HANDLE2SOCKET(s
), cvalue
, status
, total
);
3649 if (ret_size
) *ret_size
= total
;
3652 SetLastError( status
);
3653 return SOCKET_ERROR
;
3657 /***********************************************************************
3658 * ioctlsocket (WS2_32.10)
3660 int WINAPI
WS_ioctlsocket(SOCKET s
, LONG cmd
, WS_u_long
*argp
)
3663 return WSAIoctl( s
, cmd
, argp
, sizeof(WS_u_long
), argp
, sizeof(WS_u_long
), &ret_size
, NULL
, NULL
);
3666 /***********************************************************************
3667 * listen (WS2_32.13)
3669 int WINAPI
WS_listen(SOCKET s
, int backlog
)
3671 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
3673 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
3676 if (listen(fd
, backlog
) == 0)
3678 release_sock_fd( s
, fd
);
3679 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
3681 FD_CONNECT
|FD_WINE_CONNECTED
);
3684 SetLastError(wsaErrno());
3685 release_sock_fd( s
, fd
);
3687 return SOCKET_ERROR
;
3690 /***********************************************************************
3693 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
3695 DWORD n
, dwFlags
= flags
;
3701 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
3702 return SOCKET_ERROR
;
3707 /***********************************************************************
3708 * recvfrom (WS2_32.17)
3710 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
3711 struct WS_sockaddr
*from
, int *fromlen
)
3713 DWORD n
, dwFlags
= flags
;
3719 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
3720 return SOCKET_ERROR
;
3725 /* allocate a poll array for the corresponding fd sets */
3726 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
3727 const WS_fd_set
*exceptfds
, int *count_ptr
)
3729 unsigned int i
, j
= 0, count
= 0;
3732 if (readfds
) count
+= readfds
->fd_count
;
3733 if (writefds
) count
+= writefds
->fd_count
;
3734 if (exceptfds
) count
+= exceptfds
->fd_count
;
3738 SetLastError(WSAEINVAL
);
3741 if (!(fds
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(fds
[0]))))
3743 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
3747 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
3749 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
3750 if (fds
[j
].fd
== -1) goto failed
;
3751 fds
[j
].events
= POLLIN
;
3755 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
3757 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
3758 if (fds
[j
].fd
== -1) goto failed
;
3759 fds
[j
].events
= POLLOUT
;
3763 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3765 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
3766 if (fds
[j
].fd
== -1) goto failed
;
3767 fds
[j
].events
= POLLHUP
;
3776 for (i
= 0; i
< readfds
->fd_count
&& j
< count
; i
++, j
++)
3777 release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
3779 for (i
= 0; i
< writefds
->fd_count
&& j
< count
; i
++, j
++)
3780 release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
3782 for (i
= 0; i
< exceptfds
->fd_count
&& j
< count
; i
++, j
++)
3783 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
3784 HeapFree( GetProcessHeap(), 0, fds
);
3788 /* release the file descriptor obtained in fd_sets_to_poll */
3789 /* must be called with the original fd_set arrays, before calling get_poll_results */
3790 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
3791 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
3793 unsigned int i
, j
= 0;
3797 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
3798 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
3802 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
3803 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
3807 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3808 if (fds
[j
].fd
!= -1)
3810 /* make sure we have a real error before releasing the fd */
3811 if (!sock_error_p( fds
[j
].fd
)) fds
[j
].revents
= 0;
3812 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
3817 /* map the poll results back into the Windows fd sets */
3818 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
3819 const struct pollfd
*fds
)
3821 unsigned int i
, j
= 0, k
, total
= 0;
3825 for (i
= k
= 0; i
< readfds
->fd_count
; i
++, j
++)
3826 if (fds
[j
].revents
) readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
3827 readfds
->fd_count
= k
;
3832 for (i
= k
= 0; i
< writefds
->fd_count
; i
++, j
++)
3833 if ((fds
[j
].revents
& POLLOUT
) && !(fds
[j
].revents
& POLLHUP
))
3834 writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
3835 writefds
->fd_count
= k
;
3840 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3841 if (fds
[j
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
3842 exceptfds
->fd_count
= k
;
3849 /***********************************************************************
3850 * select (WS2_32.18)
3852 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
3853 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
3854 const struct WS_timeval
* ws_timeout
)
3856 struct pollfd
*pollfds
;
3857 struct timeval tv1
, tv2
;
3859 int count
, ret
, timeout
= -1;
3861 TRACE("read %p, write %p, excp %p timeout %p\n",
3862 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
3864 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)))
3865 return SOCKET_ERROR
;
3869 torig
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
3871 gettimeofday( &tv1
, 0 );
3874 while ((ret
= poll( pollfds
, count
, timeout
)) < 0)
3878 if (!ws_timeout
) continue;
3879 gettimeofday( &tv2
, 0 );
3881 tv2
.tv_sec
-= tv1
.tv_sec
;
3882 tv2
.tv_usec
-= tv1
.tv_usec
;
3883 if (tv2
.tv_usec
< 0)
3885 tv2
.tv_usec
+= 1000000;
3889 timeout
= torig
- (tv2
.tv_sec
* 1000) - (tv2
.tv_usec
+ 999) / 1000;
3890 if (timeout
<= 0) break;
3893 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
3895 if (ret
== -1) SetLastError(wsaErrno());
3896 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
3897 HeapFree( GetProcessHeap(), 0, pollfds
);
3901 /* helper to send completion messages for client-only i/o operation case */
3902 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
,
3905 SERVER_START_REQ( add_fd_completion
)
3907 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(sock
) );
3908 req
->cvalue
= CompletionValue
;
3909 req
->status
= CompletionStatus
;
3910 req
->information
= Information
;
3911 wine_server_call( req
);
3917 /***********************************************************************
3920 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
3926 wsabuf
.buf
= (char*) buf
;
3928 if ( WS2_sendto( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
3929 return SOCKET_ERROR
;
3934 /***********************************************************************
3935 * WSASend (WS2_32.72)
3937 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
3938 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
3939 LPWSAOVERLAPPED lpOverlapped
,
3940 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3942 return WS2_sendto( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
3943 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
3946 /***********************************************************************
3947 * WSASendDisconnect (WS2_32.73)
3949 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
3951 return WS_shutdown( s
, SD_SEND
);
3955 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
3956 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
3957 const struct WS_sockaddr
*to
, int tolen
,
3958 LPWSAOVERLAPPED lpOverlapped
,
3959 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3961 unsigned int i
, options
;
3963 struct ws2_async
*wsa
= NULL
;
3964 int totalLength
= 0;
3965 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
3968 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
3969 s
, lpBuffers
, dwBufferCount
, dwFlags
,
3970 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
3972 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
3973 TRACE( "fd=%d, options=%x\n", fd
, options
);
3975 if ( fd
== -1 ) return SOCKET_ERROR
;
3977 if (!lpOverlapped
&& !lpNumberOfBytesSent
)
3982 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
3988 wsa
->hSocket
= SOCKET2HANDLE(s
);
3989 wsa
->addr
= (struct WS_sockaddr
*)to
;
3990 wsa
->addrlen
.val
= tolen
;
3991 wsa
->flags
= dwFlags
;
3992 wsa
->lpFlags
= &wsa
->flags
;
3993 wsa
->control
= NULL
;
3994 wsa
->n_iovecs
= dwBufferCount
;
3995 wsa
->first_iovec
= 0;
3996 for ( i
= 0; i
< dwBufferCount
; i
++ )
3998 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
3999 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
4000 totalLength
+= lpBuffers
[i
].len
;
4005 n
= WS2_send( fd
, wsa
);
4006 if (n
!= -1 || errno
!= EINTR
) break;
4008 if (n
== -1 && errno
!= EAGAIN
)
4014 if ((lpOverlapped
|| lpCompletionRoutine
) &&
4015 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
4017 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
4019 wsa
->user_overlapped
= lpOverlapped
;
4020 wsa
->completion_func
= lpCompletionRoutine
;
4021 release_sock_fd( s
, fd
);
4023 if (n
== -1 || n
< totalLength
)
4025 iosb
->u
.Status
= STATUS_PENDING
;
4026 iosb
->Information
= n
== -1 ? 0 : n
;
4028 SERVER_START_REQ( register_async
)
4030 req
->type
= ASYNC_TYPE_WRITE
;
4031 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
4032 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
4033 req
->async
.iosb
= wine_server_client_ptr( iosb
);
4034 req
->async
.arg
= wine_server_client_ptr( wsa
);
4035 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
4036 req
->async
.cvalue
= cvalue
;
4037 err
= wine_server_call( req
);
4041 /* Enable the event only after starting the async. The server will deliver it as soon as
4042 the async is done. */
4043 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
4045 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
4046 WSASetLastError( NtStatusToWSAError( err
));
4047 return SOCKET_ERROR
;
4050 iosb
->u
.Status
= STATUS_SUCCESS
;
4051 iosb
->Information
= n
;
4052 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= n
;
4053 if (!wsa
->completion_func
)
4055 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
4056 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
4057 HeapFree( GetProcessHeap(), 0, wsa
);
4059 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
4060 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
4065 if ( _is_blocking(s
) )
4067 /* On a blocking non-overlapped stream socket,
4068 * sending blocks until the entire buffer is sent. */
4069 DWORD timeout_start
= GetTickCount();
4071 bytes_sent
= n
== -1 ? 0 : n
;
4073 while (wsa
->first_iovec
< wsa
->n_iovecs
)
4076 int timeout
= GET_SNDTIMEO(fd
);
4080 timeout
-= GetTickCount() - timeout_start
;
4081 if (timeout
< 0) timeout
= 0;
4085 pfd
.events
= POLLOUT
;
4087 if (!timeout
|| !poll( &pfd
, 1, timeout
))
4090 goto error
; /* msdn says a timeout in send is fatal */
4093 n
= WS2_send( fd
, wsa
);
4094 if (n
== -1 && errno
!= EAGAIN
&& errno
!= EINTR
)
4104 else /* non-blocking */
4106 if (n
< totalLength
)
4107 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
4110 err
= WSAEWOULDBLOCK
;
4116 TRACE(" -> %i bytes\n", bytes_sent
);
4118 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= bytes_sent
;
4119 HeapFree( GetProcessHeap(), 0, wsa
);
4120 release_sock_fd( s
, fd
);
4125 HeapFree( GetProcessHeap(), 0, wsa
);
4126 release_sock_fd( s
, fd
);
4127 WARN(" -> ERROR %d\n", err
);
4128 WSASetLastError(err
);
4129 return SOCKET_ERROR
;
4132 /***********************************************************************
4133 * WSASendTo (WS2_32.74)
4135 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4136 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
4137 const struct WS_sockaddr
*to
, int tolen
,
4138 LPWSAOVERLAPPED lpOverlapped
,
4139 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4141 return WS2_sendto( s
, lpBuffers
, dwBufferCount
,
4142 lpNumberOfBytesSent
, dwFlags
,
4144 lpOverlapped
, lpCompletionRoutine
);
4147 /***********************************************************************
4148 * sendto (WS2_32.20)
4150 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
4151 const struct WS_sockaddr
*to
, int tolen
)
4157 wsabuf
.buf
= (char*) buf
;
4159 if ( WS2_sendto(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
4160 return SOCKET_ERROR
;
4165 /***********************************************************************
4166 * setsockopt (WS2_32.21)
4168 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
4169 const char *optval
, int optlen
)
4173 struct linger linger
;
4174 struct timeval tval
;
4176 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
4177 s
, level
, optname
, optval
, optlen
);
4179 /* some broken apps pass the value directly instead of a pointer to it */
4180 if(optlen
&& IS_INTRESOURCE(optval
))
4182 SetLastError(WSAEFAULT
);
4183 return SOCKET_ERROR
;
4191 /* Some options need some conversion before they can be sent to
4192 * setsockopt. The conversions are done here, then they will fall though
4193 * to the general case. Special options that are not passed to
4194 * setsockopt follow below that.*/
4196 case WS_SO_DONTLINGER
:
4199 SetLastError(WSAEFAULT
);
4200 return SOCKET_ERROR
;
4202 linger
.l_onoff
= *(const int*)optval
== 0;
4203 linger
.l_linger
= 0;
4205 optname
= SO_LINGER
;
4206 optval
= (char*)&linger
;
4207 optlen
= sizeof(struct linger
);
4213 SetLastError(WSAEFAULT
);
4214 return SOCKET_ERROR
;
4216 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
4217 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
4219 optname
= SO_LINGER
;
4220 optval
= (char*)&linger
;
4221 optlen
= sizeof(struct linger
);
4225 if (*(const int*)optval
< 2048)
4227 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
4232 /* The options listed here don't need any special handling. Thanks to
4233 * the conversion happening above, options from there will fall through
4235 case WS_SO_ACCEPTCONN
:
4236 case WS_SO_BROADCAST
:
4238 case WS_SO_KEEPALIVE
:
4239 case WS_SO_OOBINLINE
:
4240 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
4241 * however, using it the BSD way fixes bug 8513 and seems to be what
4242 * most programmers assume, anyway */
4243 case WS_SO_REUSEADDR
:
4246 convert_sockopt(&level
, &optname
);
4249 /* SO_DEBUG is a privileged operation, ignore it. */
4251 TRACE("Ignoring SO_DEBUG\n");
4254 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
4255 * socket. According to MSDN, this option is silently ignored.*/
4256 case WS_SO_DONTROUTE
:
4257 TRACE("Ignoring SO_DONTROUTE\n");
4260 /* Stops two sockets from being bound to the same port. Always happens
4261 * on unix systems, so just drop it. */
4262 case WS_SO_EXCLUSIVEADDRUSE
:
4263 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
4266 /* After a ConnectEx call succeeds, the socket can't be used with half of the
4267 * normal winsock functions on windows. We don't have that problem. */
4268 case WS_SO_UPDATE_CONNECT_CONTEXT
:
4269 TRACE("Ignoring SO_UPDATE_CONNECT_CONTEXT, since our sockets are normal\n");
4272 /* After a AcceptEx call succeeds, the socket can't be used with half of the
4273 * normal winsock functions on windows. We don't have that problem. */
4274 case WS_SO_UPDATE_ACCEPT_CONTEXT
:
4275 TRACE("Ignoring SO_UPDATE_ACCEPT_CONTEXT, since our sockets are normal\n");
4278 /* SO_OPENTYPE does not require a valid socket handle. */
4279 case WS_SO_OPENTYPE
:
4280 if (!optlen
|| optlen
< sizeof(int) || !optval
)
4282 SetLastError(WSAEFAULT
);
4283 return SOCKET_ERROR
;
4285 get_per_thread_data()->opentype
= *(const int *)optval
;
4286 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((const int*)optval
) );
4290 case WS_SO_RCVTIMEO
:
4293 case WS_SO_SNDTIMEO
:
4295 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
4296 if (optval
&& optlen
== sizeof(UINT32
)) {
4297 /* WinSock passes milliseconds instead of struct timeval */
4298 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
4299 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
4300 /* min of 500 milliseconds */
4301 if (tval
.tv_sec
== 0 && tval
.tv_usec
&& tval
.tv_usec
< 500000)
4302 tval
.tv_usec
= 500000;
4303 optlen
= sizeof(struct timeval
);
4304 optval
= (char*)&tval
;
4305 } else if (optlen
== sizeof(struct timeval
)) {
4306 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
4308 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
4311 convert_sockopt(&level
, &optname
);
4316 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
4317 SetLastError(WSAENOPROTOOPT
);
4318 return SOCKET_ERROR
;
4320 break; /* case WS_SOL_SOCKET */
4327 fd
= get_sock_fd( s
, 0, NULL
);
4328 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval
, fd
);
4330 /* We try to set the ipx type on ipx socket level. */
4332 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, optlen
) == -1)
4334 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
4335 release_sock_fd( s
, fd
);
4336 return SOCKET_ERROR
;
4341 /* Should we retrieve val using a getsockopt call and then
4342 * set the modified one? */
4343 val
.ipx_pt
= *optval
;
4344 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
4347 release_sock_fd( s
, fd
);
4350 case IPX_FILTERPTYPE
:
4351 /* Sets the receive filter packet type, at the moment we don't support it */
4352 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
4353 /* Returning 0 is better for now than returning a SOCKET_ERROR */
4357 FIXME("opt_name:%x\n", optname
);
4358 return SOCKET_ERROR
;
4360 break; /* case NSPROTO_IPX */
4363 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
4364 case WS_IPPROTO_TCP
:
4367 case WS_TCP_NODELAY
:
4368 convert_sockopt(&level
, &optname
);
4371 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
4372 return SOCKET_ERROR
;
4379 case WS_IP_ADD_MEMBERSHIP
:
4380 case WS_IP_DROP_MEMBERSHIP
:
4384 case WS_IP_MULTICAST_IF
:
4385 case WS_IP_MULTICAST_LOOP
:
4386 case WS_IP_MULTICAST_TTL
:
4393 #ifdef IP_UNICAST_IF
4394 case WS_IP_UNICAST_IF
:
4396 convert_sockopt(&level
, &optname
);
4398 case WS_IP_DONTFRAGMENT
:
4399 FIXME("IP_DONTFRAGMENT is silently ignored!\n");
4402 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
4403 return SOCKET_ERROR
;
4407 case WS_IPPROTO_IPV6
:
4410 #ifdef IPV6_ADD_MEMBERSHIP
4411 case WS_IPV6_ADD_MEMBERSHIP
:
4413 #ifdef IPV6_DROP_MEMBERSHIP
4414 case WS_IPV6_DROP_MEMBERSHIP
:
4416 case WS_IPV6_MULTICAST_IF
:
4417 case WS_IPV6_MULTICAST_HOPS
:
4418 case WS_IPV6_MULTICAST_LOOP
:
4419 case WS_IPV6_UNICAST_HOPS
:
4420 case WS_IPV6_V6ONLY
:
4421 #ifdef IPV6_UNICAST_IF
4422 case WS_IPV6_UNICAST_IF
:
4424 convert_sockopt(&level
, &optname
);
4426 case WS_IPV6_DONTFRAG
:
4427 FIXME("IPV6_DONTFRAG is silently ignored!\n");
4429 case WS_IPV6_PROTECTION_LEVEL
:
4430 FIXME("IPV6_PROTECTION_LEVEL is ignored!\n");
4433 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
4434 return SOCKET_ERROR
;
4439 WARN("Unknown level: 0x%08x\n", level
);
4440 SetLastError(WSAEINVAL
);
4441 return SOCKET_ERROR
;
4442 } /* end switch(level) */
4444 /* avoid endianness issues if argument is a 16-bit int */
4445 if (optval
&& optlen
< sizeof(int))
4447 woptval
= *((const INT16
*) optval
);
4448 optval
= (char*) &woptval
;
4451 fd
= get_sock_fd( s
, 0, NULL
);
4452 if (fd
== -1) return SOCKET_ERROR
;
4454 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
4457 if (level
== SOL_SOCKET
&& optname
== SO_REUSEADDR
&&
4458 setsockopt(fd
, level
, SO_REUSEPORT
, optval
, optlen
) != 0)
4460 SetLastError(wsaErrno());
4461 release_sock_fd( s
, fd
);
4462 return SOCKET_ERROR
;
4465 release_sock_fd( s
, fd
);
4468 TRACE("Setting socket error, %d\n", wsaErrno());
4469 SetLastError(wsaErrno());
4470 release_sock_fd( s
, fd
);
4472 return SOCKET_ERROR
;
4475 /***********************************************************************
4476 * shutdown (WS2_32.22)
4478 int WINAPI
WS_shutdown(SOCKET s
, int how
)
4480 int fd
, err
= WSAENOTSOCK
;
4481 unsigned int options
, clear_flags
= 0;
4483 fd
= get_sock_fd( s
, 0, &options
);
4484 TRACE("socket %04lx, how %i %x\n", s
, how
, options
);
4487 return SOCKET_ERROR
;
4491 case 0: /* drop receives */
4492 clear_flags
|= FD_READ
;
4494 case 1: /* drop sends */
4495 clear_flags
|= FD_WRITE
;
4497 case 2: /* drop all */
4498 clear_flags
|= FD_READ
|FD_WRITE
;
4501 clear_flags
|= FD_WINE_LISTENING
;
4504 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
4509 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
4512 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
4516 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
4517 if (!err
) err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
4520 if (err
) goto error
;
4522 else /* non-overlapped mode */
4524 if ( shutdown( fd
, how
) )
4531 release_sock_fd( s
, fd
);
4532 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
4533 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
4537 release_sock_fd( s
, fd
);
4538 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
4539 WSASetLastError( err
);
4540 return SOCKET_ERROR
;
4543 /***********************************************************************
4544 * socket (WS2_32.23)
4546 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
4548 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
4550 return WSASocketA( af
, type
, protocol
, NULL
, 0,
4551 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
4555 /***********************************************************************
4556 * gethostbyaddr (WS2_32.51)
4558 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
4560 struct WS_hostent
*retval
= NULL
;
4561 struct hostent
* host
;
4563 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4566 struct hostent hostentry
;
4569 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
4571 int res
= gethostbyaddr_r(addr
, len
, type
,
4572 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
4573 if( res
!= ERANGE
) break;
4575 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
4577 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
4579 EnterCriticalSection( &csWSgetXXXbyYYY
);
4580 host
= gethostbyaddr(addr
, len
, type
);
4581 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
4583 if( host
!= NULL
) retval
= WS_dup_he(host
);
4584 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4585 HeapFree(GetProcessHeap(),0,extrabuf
);
4587 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4589 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
4593 /***********************************************************************
4594 * WS_get_local_ips (INTERNAL)
4596 * Returns the list of local IP addresses by going through the network
4597 * adapters and using the local routing table to sort the addresses
4598 * from highest routing priority to lowest routing priority. This
4599 * functionality is inferred from the description for obtaining local
4600 * IP addresses given in the Knowledge Base Article Q160215.
4602 * Please note that the returned hostent is only freed when the thread
4603 * closes and is replaced if another hostent is requested.
4605 static struct WS_hostent
* WS_get_local_ips( char *hostname
)
4607 int last_metric
, numroutes
= 0, i
, j
;
4608 PIP_ADAPTER_INFO adapters
= NULL
, k
;
4609 struct WS_hostent
*hostlist
= NULL
;
4610 PMIB_IPFORWARDTABLE routes
= NULL
;
4611 struct route
*route_addrs
= NULL
;
4612 DWORD adap_size
, route_size
;
4614 /* Obtain the size of the adapter list and routing table, also allocate memory */
4615 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
4617 if (GetIpForwardTable(NULL
, &route_size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
4619 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
4620 routes
= HeapAlloc(GetProcessHeap(), 0, route_size
);
4621 route_addrs
= HeapAlloc(GetProcessHeap(), 0, 0); /* HeapReAlloc doesn't work on NULL */
4622 if (adapters
== NULL
|| routes
== NULL
|| route_addrs
== NULL
)
4624 /* Obtain the adapter list and the full routing table */
4625 if (GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
4627 if (GetIpForwardTable(routes
, &route_size
, FALSE
) != NO_ERROR
)
4629 /* Store the interface associated with each route */
4630 for (i
= 0; i
< routes
->dwNumEntries
; i
++)
4633 DWORD ifmetric
, exists
= FALSE
;
4635 if (routes
->table
[i
].u1
.ForwardType
!= MIB_IPROUTE_TYPE_DIRECT
)
4637 ifindex
= routes
->table
[i
].dwForwardIfIndex
;
4638 ifmetric
= routes
->table
[i
].dwForwardMetric1
;
4639 /* Only store the lowest valued metric for an interface */
4640 for (j
= 0; j
< numroutes
; j
++)
4642 if (route_addrs
[j
].interface
== ifindex
)
4644 if (route_addrs
[j
].metric
> ifmetric
)
4645 route_addrs
[j
].metric
= ifmetric
;
4651 route_addrs
= HeapReAlloc(GetProcessHeap(), 0, route_addrs
, (numroutes
+1)*sizeof(struct route
));
4652 if (route_addrs
== NULL
)
4653 goto cleanup
; /* Memory allocation error, fail gracefully */
4654 route_addrs
[numroutes
].interface
= ifindex
;
4655 route_addrs
[numroutes
].metric
= ifmetric
;
4656 /* If no IP is found in the next step (for whatever reason)
4657 * then fall back to the magic loopback address.
4659 memcpy(&(route_addrs
[numroutes
].addr
.s_addr
), magic_loopback_addr
, 4);
4663 goto cleanup
; /* No routes, fall back to the Magic IP */
4664 /* Find the IP address associated with each found interface */
4665 for (i
= 0; i
< numroutes
; i
++)
4667 for (k
= adapters
; k
!= NULL
; k
= k
->Next
)
4669 char *ip
= k
->IpAddressList
.IpAddress
.String
;
4671 if (route_addrs
[i
].interface
== k
->Index
)
4672 route_addrs
[i
].addr
.s_addr
= (in_addr_t
) inet_addr(ip
);
4675 /* Allocate a hostent and enough memory for all the IPs,
4676 * including the NULL at the end of the list.
4678 hostlist
= WS_create_he(hostname
, 1, 0, numroutes
+1, sizeof(struct in_addr
));
4679 if (hostlist
== NULL
)
4680 goto cleanup
; /* Failed to allocate a hostent for the list of IPs */
4681 hostlist
->h_addr_list
[numroutes
] = NULL
; /* NULL-terminate the address list */
4682 hostlist
->h_aliases
[0] = NULL
; /* NULL-terminate the alias list */
4683 hostlist
->h_addrtype
= AF_INET
;
4684 hostlist
->h_length
= sizeof(struct in_addr
); /* = 4 */
4685 /* Reorder the entries when placing them in the host list, Windows expects
4686 * the IP list in order from highest priority to lowest (the critical thing
4687 * is that most applications expect the first IP to be the default route).
4690 for (i
= 0; i
< numroutes
; i
++)
4692 struct in_addr addr
;
4693 int metric
= 0xFFFF;
4695 memcpy(&addr
, magic_loopback_addr
, 4);
4696 for (j
= 0; j
< numroutes
; j
++)
4698 int this_metric
= route_addrs
[j
].metric
;
4700 if (this_metric
> last_metric
&& this_metric
< metric
)
4702 addr
= route_addrs
[j
].addr
;
4703 metric
= this_metric
;
4706 last_metric
= metric
;
4707 (*(struct in_addr
*) hostlist
->h_addr_list
[i
]) = addr
;
4710 /* Cleanup all allocated memory except the address list,
4711 * the address list is used by the calling app.
4714 HeapFree(GetProcessHeap(), 0, route_addrs
);
4715 HeapFree(GetProcessHeap(), 0, adapters
);
4716 HeapFree(GetProcessHeap(), 0, routes
);
4720 /***********************************************************************
4721 * gethostbyname (WS2_32.52)
4723 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
4725 struct WS_hostent
*retval
= NULL
;
4726 struct hostent
* host
;
4727 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4730 struct hostent hostentry
;
4731 int locerr
= ENOBUFS
;
4735 SetLastError(WSANOTINITIALISED
);
4738 if( gethostname( hostname
, 100) == -1) {
4739 SetLastError( WSAENOBUFS
); /* appropriate ? */
4742 if( !name
|| !name
[0]) {
4745 /* If the hostname of the local machine is requested then return the
4746 * complete list of local IP addresses */
4747 if(strcmp(name
, hostname
) == 0)
4748 retval
= WS_get_local_ips(hostname
);
4749 /* If any other hostname was requested (or the routing table lookup failed)
4750 * then return the IP found by the host OS */
4753 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4755 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
4757 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
4758 if( res
!= ERANGE
) break;
4760 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
4762 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
4764 EnterCriticalSection( &csWSgetXXXbyYYY
);
4765 host
= gethostbyname(name
);
4766 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
4768 if (host
) retval
= WS_dup_he(host
);
4769 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4770 HeapFree(GetProcessHeap(),0,extrabuf
);
4772 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4775 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
4776 strcmp(name
, "localhost") != 0)
4778 /* hostname != "localhost" but has loopback address. replace by our
4779 * special address.*/
4780 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
4782 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
4787 /***********************************************************************
4788 * getprotobyname (WS2_32.53)
4790 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
4792 struct WS_protoent
* retval
= NULL
;
4793 #ifdef HAVE_GETPROTOBYNAME
4794 struct protoent
* proto
;
4795 EnterCriticalSection( &csWSgetXXXbyYYY
);
4796 if( (proto
= getprotobyname(name
)) != NULL
)
4798 retval
= WS_dup_pe(proto
);
4801 MESSAGE("protocol %s not found; You might want to add "
4802 "this to /etc/protocols\n", debugstr_a(name
) );
4803 SetLastError(WSANO_DATA
);
4805 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4807 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
4812 /***********************************************************************
4813 * getprotobynumber (WS2_32.54)
4815 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
4817 struct WS_protoent
* retval
= NULL
;
4818 #ifdef HAVE_GETPROTOBYNUMBER
4819 struct protoent
* proto
;
4820 EnterCriticalSection( &csWSgetXXXbyYYY
);
4821 if( (proto
= getprotobynumber(number
)) != NULL
)
4823 retval
= WS_dup_pe(proto
);
4826 MESSAGE("protocol number %d not found; You might want to add "
4827 "this to /etc/protocols\n", number
);
4828 SetLastError(WSANO_DATA
);
4830 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4832 TRACE("%i ret %p\n", number
, retval
);
4837 /***********************************************************************
4838 * getservbyname (WS2_32.55)
4840 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
4842 struct WS_servent
* retval
= NULL
;
4843 struct servent
* serv
;
4845 char *proto_str
= NULL
;
4847 if (!(name_str
= strdup_lower(name
))) return NULL
;
4849 if (proto
&& *proto
)
4851 if (!(proto_str
= strdup_lower(proto
)))
4853 HeapFree( GetProcessHeap(), 0, name_str
);
4858 EnterCriticalSection( &csWSgetXXXbyYYY
);
4859 serv
= getservbyname(name_str
, proto_str
);
4862 retval
= WS_dup_se(serv
);
4864 else SetLastError(WSANO_DATA
);
4865 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4866 HeapFree( GetProcessHeap(), 0, proto_str
);
4867 HeapFree( GetProcessHeap(), 0, name_str
);
4868 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
4872 /***********************************************************************
4873 * freeaddrinfo (WS2_32.@)
4875 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
4878 struct WS_addrinfo
*next
;
4880 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
4881 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
4882 next
= res
->ai_next
;
4883 HeapFree(GetProcessHeap(),0,res
);
4888 /* helper functions for getaddrinfo()/getnameinfo() */
4889 static int convert_aiflag_w2u(int winflags
) {
4893 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
4894 if (ws_aiflag_map
[i
][0] & winflags
) {
4895 unixflags
|= ws_aiflag_map
[i
][1];
4896 winflags
&= ~ws_aiflag_map
[i
][0];
4899 FIXME("Unhandled windows AI_xxx flags %x\n", winflags
);
4903 static int convert_niflag_w2u(int winflags
) {
4907 for (i
=0;i
<sizeof(ws_niflag_map
)/sizeof(ws_niflag_map
[0]);i
++)
4908 if (ws_niflag_map
[i
][0] & winflags
) {
4909 unixflags
|= ws_niflag_map
[i
][1];
4910 winflags
&= ~ws_niflag_map
[i
][0];
4913 FIXME("Unhandled windows NI_xxx flags %x\n", winflags
);
4917 static int convert_aiflag_u2w(int unixflags
) {
4921 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
4922 if (ws_aiflag_map
[i
][1] & unixflags
) {
4923 winflags
|= ws_aiflag_map
[i
][0];
4924 unixflags
&= ~ws_aiflag_map
[i
][1];
4926 if (unixflags
) /* will warn usually */
4927 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags
);
4931 static int convert_eai_u2w(int unixret
) {
4934 for (i
=0;ws_eai_map
[i
][0];i
++)
4935 if (ws_eai_map
[i
][1] == unixret
)
4936 return ws_eai_map
[i
][0];
4940 static char *get_hostname(void)
4945 GetComputerNameExA( ComputerNamePhysicalDnsHostname
, NULL
, &size
);
4946 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
4947 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, size
))) return NULL
;
4948 if (!GetComputerNameExA( ComputerNamePhysicalDnsHostname
, ret
, &size
))
4950 HeapFree( GetProcessHeap(), 0, ret
);
4956 /***********************************************************************
4957 * getaddrinfo (WS2_32.@)
4959 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
4961 #ifdef HAVE_GETADDRINFO
4962 struct addrinfo
*unixaires
= NULL
;
4964 struct addrinfo unixhints
, *punixhints
= NULL
;
4965 char *hostname
= NULL
;
4968 if (!nodename
&& !servname
) return WSAHOST_NOT_FOUND
;
4972 else if (!nodename
[0])
4974 node
= hostname
= get_hostname();
4975 if (!node
) return WSA_NOT_ENOUGH_MEMORY
;
4981 punixhints
= &unixhints
;
4983 memset(&unixhints
, 0, sizeof(unixhints
));
4984 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
4985 if (hints
->ai_family
== 0) /* wildcard, specific to getaddrinfo() */
4986 punixhints
->ai_family
= 0;
4988 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
4989 if (hints
->ai_socktype
== 0) /* wildcard, specific to getaddrinfo() */
4990 punixhints
->ai_socktype
= 0;
4992 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
4993 if (hints
->ai_protocol
== 0) /* wildcard, specific to getaddrinfo() */
4994 punixhints
->ai_protocol
= 0;
4996 punixhints
->ai_protocol
= convert_proto_w2u(hints
->ai_protocol
);
4999 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
5000 result
= getaddrinfo(node
, servname
, punixhints
, &unixaires
);
5002 TRACE("%s, %s %p -> %p %d\n", debugstr_a(nodename
), debugstr_a(servname
), hints
, res
, result
);
5003 HeapFree(GetProcessHeap(), 0, hostname
);
5006 struct addrinfo
*xuai
= unixaires
;
5007 struct WS_addrinfo
**xai
= res
;
5011 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
5017 *xai
= ai
;xai
= &ai
->ai_next
;
5018 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
5019 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
5020 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
5021 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
5022 if (xuai
->ai_canonname
) {
5023 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
5024 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
5025 if (!ai
->ai_canonname
)
5027 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
5029 len
= xuai
->ai_addrlen
;
5030 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
5033 ai
->ai_addrlen
= len
;
5035 int winlen
= ai
->ai_addrlen
;
5037 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
5038 ai
->ai_addrlen
= winlen
;
5042 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
5045 ai
->ai_addrlen
= len
;
5047 xuai
= xuai
->ai_next
;
5049 freeaddrinfo(unixaires
);
5051 result
= convert_eai_u2w(result
);
5057 if (*res
) WS_freeaddrinfo(*res
);
5058 if (unixaires
) freeaddrinfo(unixaires
);
5060 return WSA_NOT_ENOUGH_MEMORY
;
5062 FIXME("getaddrinfo() failed, not found during buildtime.\n");
5067 static struct WS_addrinfoW
*addrinfo_AtoW(const struct WS_addrinfo
*ai
)
5069 struct WS_addrinfoW
*ret
;
5071 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfoW
)))) return NULL
;
5072 ret
->ai_flags
= ai
->ai_flags
;
5073 ret
->ai_family
= ai
->ai_family
;
5074 ret
->ai_socktype
= ai
->ai_socktype
;
5075 ret
->ai_protocol
= ai
->ai_protocol
;
5076 ret
->ai_addrlen
= ai
->ai_addrlen
;
5077 ret
->ai_canonname
= NULL
;
5078 ret
->ai_addr
= NULL
;
5079 ret
->ai_next
= NULL
;
5080 if (ai
->ai_canonname
)
5082 int len
= MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0);
5083 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
5085 HeapFree(GetProcessHeap(), 0, ret
);
5088 MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
);
5092 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
5094 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
5095 HeapFree(GetProcessHeap(), 0, ret
);
5098 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
5103 static struct WS_addrinfoW
*addrinfo_list_AtoW(const struct WS_addrinfo
*info
)
5105 struct WS_addrinfoW
*ret
, *infoW
;
5107 if (!(ret
= infoW
= addrinfo_AtoW(info
))) return NULL
;
5108 while (info
->ai_next
)
5110 if (!(infoW
->ai_next
= addrinfo_AtoW(info
->ai_next
)))
5115 infoW
= infoW
->ai_next
;
5116 info
= info
->ai_next
;
5121 static struct WS_addrinfo
*addrinfo_WtoA(const struct WS_addrinfoW
*ai
)
5123 struct WS_addrinfo
*ret
;
5125 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfo
)))) return NULL
;
5126 ret
->ai_flags
= ai
->ai_flags
;
5127 ret
->ai_family
= ai
->ai_family
;
5128 ret
->ai_socktype
= ai
->ai_socktype
;
5129 ret
->ai_protocol
= ai
->ai_protocol
;
5130 ret
->ai_addrlen
= ai
->ai_addrlen
;
5131 ret
->ai_canonname
= NULL
;
5132 ret
->ai_addr
= NULL
;
5133 ret
->ai_next
= NULL
;
5134 if (ai
->ai_canonname
)
5136 int len
= WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0, NULL
, NULL
);
5137 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
5139 HeapFree(GetProcessHeap(), 0, ret
);
5142 WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
, NULL
, NULL
);
5146 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
5148 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
5149 HeapFree(GetProcessHeap(), 0, ret
);
5152 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
5157 /***********************************************************************
5158 * GetAddrInfoW (WS2_32.@)
5160 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
5163 char *nodenameA
= NULL
, *servnameA
= NULL
;
5164 struct WS_addrinfo
*resA
, *hintsA
= NULL
;
5168 len
= WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, NULL
, 0, NULL
, NULL
);
5169 if (!(nodenameA
= HeapAlloc(GetProcessHeap(), 0, len
))) return EAI_MEMORY
;
5170 WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, nodenameA
, len
, NULL
, NULL
);
5174 len
= WideCharToMultiByte(CP_ACP
, 0, servname
, -1, NULL
, 0, NULL
, NULL
);
5175 if (!(servnameA
= HeapAlloc(GetProcessHeap(), 0, len
)))
5177 HeapFree(GetProcessHeap(), 0, nodenameA
);
5180 WideCharToMultiByte(CP_ACP
, 0, servname
, -1, servnameA
, len
, NULL
, NULL
);
5183 if (hints
) hintsA
= addrinfo_WtoA(hints
);
5184 ret
= WS_getaddrinfo(nodenameA
, servnameA
, hintsA
, &resA
);
5185 WS_freeaddrinfo(hintsA
);
5189 *res
= addrinfo_list_AtoW(resA
);
5190 WS_freeaddrinfo(resA
);
5193 HeapFree(GetProcessHeap(), 0, nodenameA
);
5194 HeapFree(GetProcessHeap(), 0, servnameA
);
5198 /***********************************************************************
5199 * FreeAddrInfoW (WS2_32.@)
5201 void WINAPI
FreeAddrInfoW(PADDRINFOW ai
)
5206 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
5207 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
5209 HeapFree(GetProcessHeap(), 0, ai
);
5214 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
5215 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
5217 #ifdef HAVE_GETNAMEINFO
5219 union generic_unix_sockaddr sa_u
;
5222 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
5223 serv
, servlen
, flags
);
5225 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
5228 WSASetLastError(WSAEFAULT
);
5229 return WSA_NOT_ENOUGH_MEMORY
;
5231 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
5232 return convert_eai_u2w(ret
);
5234 FIXME("getnameinfo() failed, not found during buildtime.\n");
5239 int WINAPI
GetNameInfoW(const SOCKADDR
*sa
, WS_socklen_t salen
, PWCHAR host
,
5240 DWORD hostlen
, PWCHAR serv
, DWORD servlen
, INT flags
)
5243 char *hostA
= NULL
, *servA
= NULL
;
5245 if (host
&& (!(hostA
= HeapAlloc(GetProcessHeap(), 0, hostlen
)))) return EAI_MEMORY
;
5246 if (serv
&& (!(servA
= HeapAlloc(GetProcessHeap(), 0, servlen
))))
5248 HeapFree(GetProcessHeap(), 0, hostA
);
5252 ret
= WS_getnameinfo(sa
, salen
, hostA
, hostlen
, servA
, servlen
, flags
);
5255 if (host
) MultiByteToWideChar(CP_ACP
, 0, hostA
, -1, host
, hostlen
);
5256 if (serv
) MultiByteToWideChar(CP_ACP
, 0, servA
, -1, serv
, servlen
);
5259 HeapFree(GetProcessHeap(), 0, hostA
);
5260 HeapFree(GetProcessHeap(), 0, servA
);
5264 /***********************************************************************
5265 * getservbyport (WS2_32.56)
5267 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
5269 struct WS_servent
* retval
= NULL
;
5270 #ifdef HAVE_GETSERVBYPORT
5271 struct servent
* serv
;
5272 char *proto_str
= NULL
;
5274 if (proto
&& *proto
)
5276 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
5278 EnterCriticalSection( &csWSgetXXXbyYYY
);
5279 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
5280 retval
= WS_dup_se(serv
);
5282 else SetLastError(WSANO_DATA
);
5283 LeaveCriticalSection( &csWSgetXXXbyYYY
);
5284 HeapFree( GetProcessHeap(), 0, proto_str
);
5286 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
5291 /***********************************************************************
5292 * gethostname (WS2_32.57)
5294 int WINAPI
WS_gethostname(char *name
, int namelen
)
5296 TRACE("name %p, len %d\n", name
, namelen
);
5298 if (gethostname(name
, namelen
) == 0)
5300 TRACE("<- '%s'\n", name
);
5303 SetLastError((errno
== EINVAL
) ? WSAEFAULT
: wsaErrno());
5304 TRACE("<- ERROR !\n");
5305 return SOCKET_ERROR
;
5309 /* ------------------------------------- Windows sockets extensions -- *
5311 * ------------------------------------------------------------------- */
5313 /***********************************************************************
5314 * WSAEnumNetworkEvents (WS2_32.36)
5316 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
5320 int errors
[FD_MAX_EVENTS
];
5322 TRACE("%08lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
5324 SERVER_START_REQ( get_socket_event
)
5326 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5327 req
->service
= TRUE
;
5328 req
->c_event
= wine_server_obj_handle( hEvent
);
5329 wine_server_set_reply( req
, errors
, sizeof(errors
) );
5330 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
5335 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
5336 lpEvent
->iErrorCode
[i
] = NtStatusToWSAError(errors
[i
]);
5339 SetLastError(WSAEINVAL
);
5340 return SOCKET_ERROR
;
5343 /***********************************************************************
5344 * WSAEventSelect (WS2_32.39)
5346 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
5350 TRACE("%08lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
5352 SERVER_START_REQ( set_socket_event
)
5354 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5356 req
->event
= wine_server_obj_handle( hEvent
);
5359 ret
= wine_server_call( req
);
5363 SetLastError(WSAEINVAL
);
5364 return SOCKET_ERROR
;
5367 /**********************************************************************
5368 * WSAGetOverlappedResult (WS2_32.40)
5370 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
5371 LPDWORD lpcbTransfer
, BOOL fWait
,
5376 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
5377 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
5379 if ( lpOverlapped
== NULL
)
5381 ERR( "Invalid pointer\n" );
5382 WSASetLastError(WSA_INVALID_PARAMETER
);
5386 status
= lpOverlapped
->Internal
;
5387 if (status
== STATUS_PENDING
)
5391 SetLastError( WSA_IO_INCOMPLETE
);
5395 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
5396 INFINITE
) == WAIT_FAILED
)
5398 status
= lpOverlapped
->Internal
;
5402 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
5405 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
5407 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
5412 /***********************************************************************
5413 * WSAAsyncSelect (WS2_32.101)
5415 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
5419 TRACE("%lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
5421 SERVER_START_REQ( set_socket_event
)
5423 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5426 req
->window
= wine_server_user_handle( hWnd
);
5428 ret
= wine_server_call( req
);
5432 SetLastError(WSAEINVAL
);
5433 return SOCKET_ERROR
;
5436 /***********************************************************************
5437 * WSACreateEvent (WS2_32.31)
5440 WSAEVENT WINAPI
WSACreateEvent(void)
5442 /* Create a manual-reset event, with initial state: unsignaled */
5445 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
5448 /***********************************************************************
5449 * WSACloseEvent (WS2_32.29)
5452 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
5454 TRACE ("event=%p\n", event
);
5456 return CloseHandle(event
);
5459 /***********************************************************************
5460 * WSASocketA (WS2_32.78)
5463 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
5464 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
5465 GROUP g
, DWORD dwFlags
)
5468 WSAPROTOCOL_INFOW info
;
5470 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
5471 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
5473 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
5475 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
5476 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
5477 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
5481 WSASetLastError( WSAEINVAL
);
5482 return SOCKET_ERROR
;
5485 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
5488 /***********************************************************************
5489 * WSASocketW (WS2_32.79)
5492 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
5493 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
5494 GROUP g
, DWORD dwFlags
)
5499 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
5500 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
5503 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
5504 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
5506 /* hack for WSADuplicateSocket */
5507 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
5508 ret
= lpProtocolInfo
->dwCatalogEntryId
;
5509 TRACE("\tgot duplicate %04lx\n", ret
);
5513 /* convert the socket family and type */
5514 af
= convert_af_w2u(af
);
5515 type
= convert_socktype_w2u(type
);
5519 if (af
== FROM_PROTOCOL_INFO
)
5520 af
= lpProtocolInfo
->iAddressFamily
;
5521 if (type
== FROM_PROTOCOL_INFO
)
5522 type
= lpProtocolInfo
->iSocketType
;
5523 if (protocol
== FROM_PROTOCOL_INFO
)
5524 protocol
= lpProtocolInfo
->iProtocol
;
5527 if ( af
== AF_UNSPEC
) /* did they not specify the address family? */
5529 if ((protocol
== IPPROTO_TCP
&& type
== SOCK_STREAM
) ||
5530 (protocol
== IPPROTO_UDP
&& type
== SOCK_DGRAM
))
5536 SetLastError(WSAEPROTOTYPE
);
5537 return INVALID_SOCKET
;
5541 SERVER_START_REQ( create_socket
)
5545 req
->protocol
= protocol
;
5546 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
5547 req
->attributes
= OBJ_INHERIT
;
5548 req
->flags
= dwFlags
;
5549 set_error( wine_server_call( req
) );
5550 ret
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
5555 TRACE("\tcreated %04lx\n", ret
);
5559 if (GetLastError() == WSAEACCES
) /* raw socket denied */
5561 if (type
== SOCK_RAW
)
5562 ERR_(winediag
)("Failed to create a socket of type SOCK_RAW, this requires special permissions.\n");
5564 ERR_(winediag
)("Failed to create socket, this requires special permissions.\n");
5565 SetLastError(WSAESOCKTNOSUPPORT
);
5568 WARN("\t\tfailed!\n");
5569 return INVALID_SOCKET
;
5572 /***********************************************************************
5573 * WSAJoinLeaf (WS2_32.58)
5576 SOCKET WINAPI
WSAJoinLeaf(
5578 const struct WS_sockaddr
*addr
,
5580 LPWSABUF lpCallerData
,
5581 LPWSABUF lpCalleeData
,
5587 return INVALID_SOCKET
;
5590 /***********************************************************************
5591 * __WSAFDIsSet (WS2_32.151)
5593 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
5595 int i
= set
->fd_count
;
5597 TRACE("(%ld,%p(%i))\n", s
, set
, i
);
5600 if (set
->fd_array
[i
] == s
) return 1;
5604 /***********************************************************************
5605 * WSAIsBlocking (WS2_32.114)
5607 BOOL WINAPI
WSAIsBlocking(void)
5609 /* By default WinSock should set all its sockets to non-blocking mode
5610 * and poll in PeekMessage loop when processing "blocking" ones. This
5611 * function is supposed to tell if the program is in this loop. Our
5612 * blocking calls are truly blocking so we always return FALSE.
5614 * Note: It is allowed to call this function without prior WSAStartup().
5621 /***********************************************************************
5622 * WSACancelBlockingCall (WS2_32.113)
5624 INT WINAPI
WSACancelBlockingCall(void)
5630 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
5632 FIXME("How was this called?\n");
5637 /***********************************************************************
5638 * WSASetBlockingHook (WS2_32.109)
5640 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
5642 FARPROC prev
= blocking_hook
;
5643 blocking_hook
= lpBlockFunc
;
5644 TRACE("hook %p\n", lpBlockFunc
);
5649 /***********************************************************************
5650 * WSAUnhookBlockingHook (WS2_32.110)
5652 INT WINAPI
WSAUnhookBlockingHook(void)
5654 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
5659 /* ----------------------------------- end of API stuff */
5661 /* ----------------------------------- helper functions -
5663 * TODO: Merge WS_dup_..() stuff into one function that
5664 * would operate with a generic structure containing internal
5665 * pointers (via a template of some kind).
5668 static int list_size(char** l
, int item_size
)
5673 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
5674 j
+= (i
+ 1) * sizeof(char*); }
5678 static int list_dup(char** l_src
, char** l_to
, int item_size
)
5683 for (i
= 0; l_src
[i
]; i
++) ;
5684 p
= (char *)(l_to
+ i
+ 1);
5685 for (i
= 0; l_src
[i
]; i
++)
5687 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
5688 memcpy(p
, l_src
[i
], count
);
5693 return p
- (char *)l_to
;
5698 /* create a hostent entry
5700 * Creates the entry with enough memory for the name, aliases
5701 * addresses, and the address pointers. Also copies the name
5702 * and sets up all the pointers.
5704 * NOTE: The alias and address lists must be allocated with room
5705 * for the NULL item terminating the list. This is true even if
5706 * the list has no items ("aliases" and "addresses" must be
5707 * at least "1", a truly empty list is invalid).
5709 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int aliases_size
, int addresses
, int address_length
)
5711 struct WS_hostent
*p_to
;
5713 int size
= (sizeof(struct WS_hostent
) +
5715 sizeof(char *) * aliases
+
5717 sizeof(char *) * addresses
+
5718 address_length
* (addresses
- 1)), i
;
5720 if (!(p_to
= check_buffer_he(size
))) return NULL
;
5721 memset(p_to
, 0, size
);
5723 /* Use the memory in the same way winsock does.
5724 * First set the pointer for aliases, second set the pointers for addresses.
5725 * Third fill the addresses indexes, fourth jump aliases names size.
5726 * Fifth fill the hostname.
5727 * NOTE: This method is valid for OS version's >= XP.
5729 p
= (char *)(p_to
+ 1);
5730 p_to
->h_aliases
= (char **)p
;
5731 p
+= sizeof(char *)*aliases
;
5733 p_to
->h_addr_list
= (char **)p
;
5734 p
+= sizeof(char *)*addresses
;
5736 for (i
= 0, addresses
--; i
< addresses
; i
++, p
+= address_length
)
5737 p_to
->h_addr_list
[i
] = p
;
5739 /* NOTE: h_aliases must be filled in manually because we don't know each string
5740 * size, leave these pointers NULL (already set to NULL by memset earlier).
5750 /* duplicate hostent entry
5751 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
5752 * Ditto for protoent and servent.
5754 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
5756 int i
, addresses
= 0, alias_size
= 0;
5757 struct WS_hostent
*p_to
;
5760 for( i
= 0; p_he
->h_aliases
[i
]; i
++) alias_size
+= strlen(p_he
->h_aliases
[i
]) + 1;
5761 while (p_he
->h_addr_list
[addresses
]) addresses
++;
5763 p_to
= WS_create_he(p_he
->h_name
, i
+ 1, alias_size
, addresses
+ 1, p_he
->h_length
);
5765 if (!p_to
) return NULL
;
5766 p_to
->h_addrtype
= p_he
->h_addrtype
;
5767 p_to
->h_length
= p_he
->h_length
;
5769 for(i
= 0, p
= p_to
->h_addr_list
[0]; p_he
->h_addr_list
[i
]; i
++, p
+= p_to
->h_length
)
5770 memcpy(p
, p_he
->h_addr_list
[i
], p_to
->h_length
);
5772 /* Fill the aliases after the IP data */
5773 for(i
= 0; p_he
->h_aliases
[i
]; i
++)
5775 p_to
->h_aliases
[i
] = p
;
5776 strcpy(p
, p_he
->h_aliases
[i
]);
5783 /* ----- protoent */
5785 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
)
5788 struct WS_protoent
*p_to
;
5790 int size
= (sizeof(*p_pe
) +
5791 strlen(p_pe
->p_name
) + 1 +
5792 list_size(p_pe
->p_aliases
, 0));
5794 if (!(p_to
= check_buffer_pe(size
))) return NULL
;
5795 p_to
->p_proto
= p_pe
->p_proto
;
5797 p
= (char *)(p_to
+ 1);
5799 strcpy(p
, p_pe
->p_name
);
5802 p_to
->p_aliases
= (char **)p
;
5803 list_dup(p_pe
->p_aliases
, p_to
->p_aliases
, 0);
5809 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
5812 struct WS_servent
*p_to
;
5814 int size
= (sizeof(*p_se
) +
5815 strlen(p_se
->s_proto
) + 1 +
5816 strlen(p_se
->s_name
) + 1 +
5817 list_size(p_se
->s_aliases
, 0));
5819 if (!(p_to
= check_buffer_se(size
))) return NULL
;
5820 p_to
->s_port
= p_se
->s_port
;
5822 p
= (char *)(p_to
+ 1);
5824 strcpy(p
, p_se
->s_name
);
5828 strcpy(p
, p_se
->s_proto
);
5831 p_to
->s_aliases
= (char **)p
;
5832 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
5837 /***********************************************************************
5838 * WSARecv (WS2_32.67)
5840 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5841 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
5842 LPWSAOVERLAPPED lpOverlapped
,
5843 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5845 return WS2_recv_base(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
5846 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
, NULL
);
5849 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5850 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
5851 struct WS_sockaddr
*lpFrom
,
5852 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
5853 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
5854 LPWSABUF lpControlBuffer
)
5856 unsigned int i
, options
;
5858 struct ws2_async
*wsa
;
5859 DWORD timeout_start
= GetTickCount();
5860 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
5862 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
5863 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
5864 (lpFromlen
? *lpFromlen
: -1),
5865 lpOverlapped
, lpCompletionRoutine
);
5867 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
5868 TRACE( "fd=%d, options=%x\n", fd
, options
);
5870 if (fd
== -1) return SOCKET_ERROR
;
5872 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
5878 wsa
->hSocket
= SOCKET2HANDLE(s
);
5879 wsa
->flags
= *lpFlags
;
5880 wsa
->lpFlags
= lpFlags
;
5882 wsa
->addrlen
.ptr
= lpFromlen
;
5883 wsa
->control
= lpControlBuffer
;
5884 wsa
->n_iovecs
= dwBufferCount
;
5885 wsa
->first_iovec
= 0;
5886 for (i
= 0; i
< dwBufferCount
; i
++)
5888 /* check buffer first to trigger write watches */
5889 if (IsBadWritePtr( lpBuffers
[i
].buf
, lpBuffers
[i
].len
))
5894 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
5895 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
5900 n
= WS2_recv( fd
, wsa
);
5903 if (errno
== EINTR
) continue;
5904 if (errno
!= EAGAIN
)
5906 int loc_errno
= errno
;
5908 if (cvalue
) WS_AddCompletion( s
, cvalue
, sock_get_ntstatus(loc_errno
), 0 );
5912 else if (lpNumberOfBytesRecvd
) *lpNumberOfBytesRecvd
= n
;
5914 if ((lpOverlapped
|| lpCompletionRoutine
) &&
5915 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
5917 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
5919 wsa
->user_overlapped
= lpOverlapped
;
5920 wsa
->completion_func
= lpCompletionRoutine
;
5921 release_sock_fd( s
, fd
);
5925 iosb
->u
.Status
= STATUS_PENDING
;
5926 iosb
->Information
= 0;
5928 SERVER_START_REQ( register_async
)
5930 req
->type
= ASYNC_TYPE_READ
;
5931 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
5932 req
->async
.callback
= wine_server_client_ptr( WS2_async_recv
);
5933 req
->async
.iosb
= wine_server_client_ptr( iosb
);
5934 req
->async
.arg
= wine_server_client_ptr( wsa
);
5935 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
5936 req
->async
.cvalue
= cvalue
;
5937 err
= wine_server_call( req
);
5941 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
5942 WSASetLastError( NtStatusToWSAError( err
));
5943 return SOCKET_ERROR
;
5946 iosb
->u
.Status
= STATUS_SUCCESS
;
5947 iosb
->Information
= n
;
5948 if (!wsa
->completion_func
)
5950 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
5951 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
5952 HeapFree( GetProcessHeap(), 0, wsa
);
5954 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
5955 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
5956 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5962 if ( _is_blocking(s
) )
5965 int timeout
= GET_RCVTIMEO(fd
);
5968 timeout
-= GetTickCount() - timeout_start
;
5969 if (timeout
< 0) timeout
= 0;
5973 pfd
.events
= POLLIN
;
5974 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
5976 if (!timeout
|| !poll( &pfd
, 1, timeout
))
5979 /* a timeout is not fatal */
5980 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5986 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5987 err
= WSAEWOULDBLOCK
;
5992 TRACE(" -> %i bytes\n", n
);
5993 HeapFree( GetProcessHeap(), 0, wsa
);
5994 release_sock_fd( s
, fd
);
5995 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
6000 HeapFree( GetProcessHeap(), 0, wsa
);
6001 release_sock_fd( s
, fd
);
6002 WARN(" -> ERROR %d\n", err
);
6003 WSASetLastError( err
);
6004 return SOCKET_ERROR
;
6007 /***********************************************************************
6008 * WSARecvFrom (WS2_32.69)
6010 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
6011 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
6012 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
6013 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
6016 return WS2_recv_base( s
, lpBuffers
, dwBufferCount
,
6017 lpNumberOfBytesRecvd
, lpFlags
,
6019 lpOverlapped
, lpCompletionRoutine
, NULL
);
6022 /***********************************************************************
6023 * WSCInstallProvider (WS2_32.88)
6025 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
6026 LPCWSTR lpszProviderDllPath
,
6027 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
6028 DWORD dwNumberOfEntries
,
6031 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
6032 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
6033 dwNumberOfEntries
, lpErrno
);
6039 /***********************************************************************
6040 * WSCDeinstallProvider (WS2_32.83)
6042 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
6044 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
6050 /***********************************************************************
6051 * WSAAccept (WS2_32.26)
6053 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
6054 LPCONDITIONPROC lpfnCondition
, DWORD_PTR dwCallbackData
)
6057 int ret
= 0, size
= 0;
6058 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
6059 /* QOS SQOS, GQOS; */
6062 SOCKADDR src_addr
, dst_addr
;
6064 TRACE("Socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %ld\n",
6065 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
6068 size
= sizeof(src_addr
);
6069 cs
= WS_accept(s
, &src_addr
, &size
);
6071 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
6073 if (!lpfnCondition
) return cs
;
6075 CallerId
.buf
= (char *)&src_addr
;
6076 CallerId
.len
= sizeof(src_addr
);
6078 CallerData
.buf
= NULL
;
6081 WS_getsockname(cs
, &dst_addr
, &size
);
6083 CalleeId
.buf
= (char *)&dst_addr
;
6084 CalleeId
.len
= sizeof(dst_addr
);
6087 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
6088 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
6093 if (addr
&& addrlen
)
6094 memcpy(addr
, &src_addr
, (*addrlen
> size
) ? size
: *addrlen
);
6097 SERVER_START_REQ( set_socket_deferred
)
6099 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
6100 req
->deferred
= wine_server_obj_handle( SOCKET2HANDLE(cs
) );
6101 if ( !wine_server_call_err ( req
) )
6103 SetLastError( WSATRY_AGAIN
);
6104 WS_closesocket( cs
);
6108 return SOCKET_ERROR
;
6111 SetLastError(WSAECONNREFUSED
);
6112 return SOCKET_ERROR
;
6114 FIXME("Unknown return type from Condition function\n");
6115 SetLastError(WSAENOTSOCK
);
6116 return SOCKET_ERROR
;
6120 /***********************************************************************
6121 * WSADuplicateSocketA (WS2_32.32)
6123 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
6127 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
6128 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
6129 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
6130 /* I don't know what the real Windoze does next, this is a hack */
6131 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
6132 * the target use the global duplicate, or we could copy a reference to us to the structure
6133 * and let the target duplicate it from us, but let's do it as simple as possible */
6134 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
6135 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
6136 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
6137 0, FALSE
, DUPLICATE_SAME_ACCESS
);
6138 CloseHandle(hProcess
);
6139 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
6143 /***********************************************************************
6144 * WSADuplicateSocketW (WS2_32.33)
6146 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
6150 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
6152 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
6153 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
6154 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
6155 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
6156 0, FALSE
, DUPLICATE_SAME_ACCESS
);
6157 CloseHandle(hProcess
);
6158 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
6162 /***********************************************************************
6163 * WSAInstallServiceClassA (WS2_32.48)
6165 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
6167 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
6168 WSASetLastError(WSAEACCES
);
6169 return SOCKET_ERROR
;
6172 /***********************************************************************
6173 * WSAInstallServiceClassW (WS2_32.49)
6175 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
6177 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
6178 WSASetLastError(WSAEACCES
);
6179 return SOCKET_ERROR
;
6182 /***********************************************************************
6183 * WSARemoveServiceClass (WS2_32.70)
6185 int WINAPI
WSARemoveServiceClass(LPGUID info
)
6187 FIXME("Request to remove service %p\n",info
);
6188 WSASetLastError(WSATYPE_NOT_FOUND
);
6189 return SOCKET_ERROR
;
6192 /***********************************************************************
6193 * inet_ntop (WS2_32.@)
6195 PCSTR WINAPI
WS_inet_ntop( INT family
, PVOID addr
, PSTR buffer
, SIZE_T len
)
6197 #ifdef HAVE_INET_NTOP
6198 struct WS_in6_addr
*in6
;
6199 struct WS_in_addr
*in
;
6202 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
6205 WSASetLastError( STATUS_INVALID_PARAMETER
);
6214 pdst
= inet_ntop( AF_INET
, &in
->WS_s_addr
, buffer
, len
);
6220 pdst
= inet_ntop( AF_INET6
, in6
->WS_s6_addr
, buffer
, len
);
6224 WSASetLastError( WSAEAFNOSUPPORT
);
6228 if (!pdst
) WSASetLastError( STATUS_INVALID_PARAMETER
);
6231 FIXME( "not supported on this platform\n" );
6232 WSASetLastError( WSAEAFNOSUPPORT
);
6237 /***********************************************************************
6238 * WSAStringToAddressA (WS2_32.80)
6240 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
6242 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
6243 LPSOCKADDR lpAddress
,
6244 LPINT lpAddressLength
)
6247 LPSTR workBuffer
=NULL
,ptrPort
;
6249 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_a(AddressString
), AddressFamily
,
6250 lpProtocolInfo
, lpAddress
, lpAddressLength
);
6252 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
6256 WSASetLastError(WSAEINVAL
);
6257 return SOCKET_ERROR
;
6261 FIXME("ProtocolInfo not implemented.\n");
6263 workBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
6264 strlen(AddressString
) + 1);
6267 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6268 return SOCKET_ERROR
;
6271 strcpy(workBuffer
, AddressString
);
6273 switch(AddressFamily
)
6277 struct in_addr inetaddr
;
6279 /* If lpAddressLength is too small, tell caller the size we need */
6280 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
6282 *lpAddressLength
= sizeof(SOCKADDR_IN
);
6286 *lpAddressLength
= sizeof(SOCKADDR_IN
);
6287 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
6289 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= AF_INET
;
6291 ptrPort
= strchr(workBuffer
, ':');
6294 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= htons(atoi(ptrPort
+1));
6299 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= 0;
6302 if(inet_aton(workBuffer
, &inetaddr
) > 0)
6304 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
6315 struct in6_addr inetaddr
;
6316 /* If lpAddressLength is too small, tell caller the size we need */
6317 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
6319 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
6323 #ifdef HAVE_INET_PTON
6324 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
6325 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
6327 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_family
= WS_AF_INET6
;
6329 /* This one is a bit tricky. An IPv6 address contains colons, so the
6330 * check from IPv4 doesn't work like that. However, IPv6 addresses that
6331 * contain a port are written with braces like [fd12:3456:7890::1]:12345
6332 * so what we will do is to look for ']', check if the next char is a
6333 * colon, and if it is, parse the port as in IPv4. */
6335 ptrPort
= strchr(workBuffer
, ']');
6336 if(ptrPort
&& *(++ptrPort
) == ':')
6338 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= htons(atoi(ptrPort
+1));
6343 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= 0;
6346 if(inet_pton(AF_INET6
, workBuffer
, &inetaddr
) > 0)
6348 memcpy(&((LPSOCKADDR_IN6
)lpAddress
)->sin6_addr
, &inetaddr
,
6349 sizeof(struct in6_addr
));
6353 #endif /* HAVE_INET_PTON */
6359 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
6360 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
6364 HeapFree(GetProcessHeap(), 0, workBuffer
);
6367 WSASetLastError(res
);
6368 return SOCKET_ERROR
;
6371 /***********************************************************************
6372 * WSAStringToAddressW (WS2_32.81)
6374 * FIXME: Does anybody know if this function allows using Hebrew/Arabic/Chinese... digits?
6375 * If this should be the case, it would be required to map these digits
6376 * to Unicode digits (0-9) using FoldString first.
6378 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
6380 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
6381 LPSOCKADDR lpAddress
,
6382 LPINT lpAddressLength
)
6385 LPSTR workBuffer
=NULL
;
6386 WSAPROTOCOL_INFOA infoA
;
6387 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
6389 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
6390 lpAddress
, lpAddressLength
);
6392 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
6394 /* if ProtocolInfo is available - convert to ANSI variant */
6397 lpProtoInfoA
= &infoA
;
6398 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
6400 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
6401 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
6403 WSASetLastError( WSAEINVAL
);
6404 return SOCKET_ERROR
;
6410 /* Translate AddressString to ANSI code page - assumes that only
6411 standard digits 0-9 are used with this API call */
6412 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
6413 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
6417 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
6418 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
6419 lpAddress
,lpAddressLength
);
6420 HeapFree( GetProcessHeap(), 0, workBuffer
);
6424 res
= WSA_NOT_ENOUGH_MEMORY
;
6429 WSASetLastError(res
);
6430 return SOCKET_ERROR
;
6433 /***********************************************************************
6434 * WSAAddressToStringA (WS2_32.27)
6436 * See WSAAddressToStringW
6438 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
6439 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
6443 CHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
6446 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
6448 if (!sockaddr
) return SOCKET_ERROR
;
6449 if (!string
|| !lenstr
) return SOCKET_ERROR
;
6451 switch(sockaddr
->sa_family
)
6454 if (len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
6455 sprintf( buffer
, "%u.%u.%u.%u:%u",
6456 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
6457 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
6458 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
6459 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
6460 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
6462 p
= strchr( buffer
, ':' );
6463 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
6468 struct WS_sockaddr_in6
*sockaddr6
= (LPSOCKADDR_IN6
) sockaddr
;
6471 if (len
< sizeof(SOCKADDR_IN6
)) return SOCKET_ERROR
;
6472 if ((sockaddr6
->sin6_port
))
6473 strcpy(buffer
, "[");
6474 if (!WS_inet_ntop(WS_AF_INET6
, &sockaddr6
->sin6_addr
, buffer
+strlen(buffer
), sizeof(buffer
)))
6476 WSASetLastError(WSAEINVAL
);
6477 return SOCKET_ERROR
;
6479 if ((sockaddr6
->sin6_scope_id
))
6480 sprintf(buffer
+strlen(buffer
), "%%%u", sockaddr6
->sin6_scope_id
);
6481 if ((sockaddr6
->sin6_port
))
6482 sprintf(buffer
+strlen(buffer
), "]:%u", ntohs(sockaddr6
->sin6_port
));
6487 WSASetLastError(WSAEINVAL
);
6488 return SOCKET_ERROR
;
6491 size
= strlen( buffer
) + 1;
6496 WSASetLastError(WSAEFAULT
);
6497 return SOCKET_ERROR
;
6501 strcpy( string
, buffer
);
6505 /***********************************************************************
6506 * WSAAddressToStringW (WS2_32.28)
6508 * Convert a sockaddr address into a readable address string.
6511 * sockaddr [I] Pointer to a sockaddr structure.
6512 * len [I] Size of the sockaddr structure.
6513 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
6514 * string [I/O] Pointer to a buffer to receive the address string.
6515 * lenstr [I/O] Size of the receive buffer in WCHARs.
6519 * Failure: SOCKET_ERROR
6522 * The 'info' parameter is ignored.
6524 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
6525 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
6530 WCHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
6533 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
6536 ret
= WSAAddressToStringA(sockaddr
, len
, NULL
, bufAddr
, &size
);
6538 if (ret
) return ret
;
6540 MultiByteToWideChar( CP_ACP
, 0, bufAddr
, size
, buffer
, sizeof( buffer
)/sizeof(WCHAR
));
6545 WSASetLastError(WSAEFAULT
);
6546 return SOCKET_ERROR
;
6550 lstrcpyW( string
, buffer
);
6554 /***********************************************************************
6555 * WSAEnumNameSpaceProvidersA (WS2_32.34)
6557 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
6559 FIXME( "(%p %p) Stub!\n", len
, buffer
);
6563 /***********************************************************************
6564 * WSAEnumNameSpaceProvidersW (WS2_32.35)
6566 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
6568 FIXME( "(%p %p) Stub!\n", len
, buffer
);
6572 /***********************************************************************
6573 * WSAGetQOSByName (WS2_32.41)
6575 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
6577 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
6581 /***********************************************************************
6582 * WSAGetServiceClassInfoA (WS2_32.42)
6584 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
6585 LPWSASERVICECLASSINFOA info
)
6587 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
6589 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6590 return SOCKET_ERROR
;
6593 /***********************************************************************
6594 * WSAGetServiceClassInfoW (WS2_32.43)
6596 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
6597 LPWSASERVICECLASSINFOW info
)
6599 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
6601 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6602 return SOCKET_ERROR
;
6605 /***********************************************************************
6606 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
6608 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
6610 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
6611 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6612 return SOCKET_ERROR
;
6615 /***********************************************************************
6616 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
6618 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
6620 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
6621 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6622 return SOCKET_ERROR
;
6625 /***********************************************************************
6626 * WSALookupServiceBeginA (WS2_32.59)
6628 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
6629 DWORD dwControlFlags
,
6632 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
6634 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6635 return SOCKET_ERROR
;
6638 /***********************************************************************
6639 * WSALookupServiceBeginW (WS2_32.60)
6641 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
6642 DWORD dwControlFlags
,
6645 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
6647 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6648 return SOCKET_ERROR
;
6651 /***********************************************************************
6652 * WSALookupServiceEnd (WS2_32.61)
6654 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
6656 FIXME("(%p) Stub!\n", lookup
);
6660 /***********************************************************************
6661 * WSALookupServiceNextA (WS2_32.62)
6663 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
6665 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
6666 WSASetLastError(WSA_E_NO_MORE
);
6667 return SOCKET_ERROR
;
6670 /***********************************************************************
6671 * WSALookupServiceNextW (WS2_32.63)
6673 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
6675 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
6676 WSASetLastError(WSA_E_NO_MORE
);
6677 return SOCKET_ERROR
;
6680 /***********************************************************************
6681 * WSANtohl (WS2_32.64)
6683 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
6685 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
6687 if (!lphostlong
) return WSAEFAULT
;
6689 *lphostlong
= ntohl( netlong
);
6693 /***********************************************************************
6694 * WSANtohs (WS2_32.65)
6696 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
6698 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
6700 if (!lphostshort
) return WSAEFAULT
;
6702 *lphostshort
= ntohs( netshort
);
6706 /***********************************************************************
6707 * WSAProviderConfigChange (WS2_32.66)
6709 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
6710 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
6712 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
6713 return SOCKET_ERROR
;
6716 /***********************************************************************
6717 * WSARecvDisconnect (WS2_32.68)
6719 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
6721 TRACE( "(0x%04lx %p)\n", s
, disconnectdata
);
6723 return WS_shutdown( s
, 0 );
6726 /***********************************************************************
6727 * WSASetServiceA (WS2_32.76)
6729 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
6731 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
6735 /***********************************************************************
6736 * WSASetServiceW (WS2_32.77)
6738 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
6740 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
6744 /***********************************************************************
6745 * WSCEnableNSProvider (WS2_32.84)
6747 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
6749 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
6753 /***********************************************************************
6754 * WSCGetProviderPath (WS2_32.86)
6756 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
6758 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
6760 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
6762 *errcode
= WSAEINVAL
;
6763 return SOCKET_ERROR
;
6766 /***********************************************************************
6767 * WSCInstallNameSpace (WS2_32.87)
6769 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
6770 DWORD version
, LPGUID provider
)
6772 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
6773 namespace, version
, debugstr_guid(provider
) );
6777 /***********************************************************************
6778 * WSCUnInstallNameSpace (WS2_32.89)
6780 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
6782 FIXME("(%p) Stub!\n", lpProviderId
);
6786 /***********************************************************************
6787 * WSCWriteProviderOrder (WS2_32.91)
6789 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
6791 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);
6795 /***********************************************************************
6796 * WSANSPIoctl (WS2_32.91)
6798 INT WINAPI
WSANSPIoctl( HANDLE hLookup
, DWORD dwControlCode
, LPVOID lpvInBuffer
,
6799 DWORD cbInBuffer
, LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
6800 LPDWORD lpcbBytesReturned
, LPWSACOMPLETION lpCompletion
)
6802 FIXME("(%p, 0x%08x, %p, 0x%08x, %p, 0x%08x, %p, %p) Stub!\n", hLookup
, dwControlCode
,
6803 lpvInBuffer
, cbInBuffer
, lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpCompletion
);
6804 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6805 return SOCKET_ERROR
;