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>
79 #ifdef HAVE_SYS_ERRNO_H
80 #include <sys/errno.h>
89 #ifdef HAVE_ARPA_NAMESER_H
90 # include <arpa/nameser.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 #include "iphlpapi.h"
150 #include "wine/server.h"
151 #include "wine/debug.h"
152 #include "wine/exception.h"
153 #include "wine/unicode.h"
156 # include "wsnwlink.h"
160 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
161 # define sipx_network sipx_addr.x_net
162 # define sipx_node sipx_addr.x_host.c_host
163 #endif /* __FreeBSD__ */
166 #define INADDR_NONE ~0UL
169 WINE_DEFAULT_DEBUG_CHANNEL(winsock
);
170 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
174 * The actual definition of WSASendTo, wrapped in a different function name
175 * so that internal calls from ws2_32 itself will not trigger programs like
176 * Garena, which hooks WSASendTo/WSARecvFrom calls.
178 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
179 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
180 const struct WS_sockaddr
*to
, int tolen
,
181 LPWSAOVERLAPPED lpOverlapped
,
182 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
185 * Internal fundamental receive function, essentially WSARecvFrom with an
186 * additional parameter to support message control headers.
188 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
189 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
190 struct WS_sockaddr
*lpFrom
,
191 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
192 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
193 LPWSABUF lpControlBuffer
);
195 /* critical section to protect some non-reentrant net function */
196 static CRITICAL_SECTION csWSgetXXXbyYYY
;
197 static CRITICAL_SECTION_DEBUG critsect_debug
=
199 0, 0, &csWSgetXXXbyYYY
,
200 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
201 0, 0, { (DWORD_PTR
)(__FILE__
": csWSgetXXXbyYYY") }
203 static CRITICAL_SECTION csWSgetXXXbyYYY
= { &critsect_debug
, -1, 0, 0, 0, 0 };
205 union generic_unix_sockaddr
207 struct sockaddr addr
;
208 char data
[128]; /* should be big enough for all families */
211 static inline const char *debugstr_sockaddr( const struct WS_sockaddr
*a
)
213 if (!a
) return "(nil)";
214 switch (a
->sa_family
)
217 return wine_dbg_sprintf("{ family AF_INET, address %s, port %d }",
218 inet_ntoa(((const struct sockaddr_in
*)a
)->sin_addr
),
219 ntohs(((const struct sockaddr_in
*)a
)->sin_port
));
224 struct WS_sockaddr_in6
*sin
= (struct WS_sockaddr_in6
*)a
;
226 p
= WS_inet_ntop( WS_AF_INET6
, &sin
->sin6_addr
, buf
, sizeof(buf
) );
228 p
= "(unknown IPv6 address)";
229 return wine_dbg_sprintf("{ family AF_INET6, address %s, port %d }",
230 p
, ntohs(sin
->sin6_port
));
236 memcpy( &addr
, ((const SOCKADDR_IRDA
*)a
)->irdaDeviceID
, sizeof(addr
) );
237 addr
= ntohl( addr
);
238 return wine_dbg_sprintf("{ family AF_IRDA, addr %08x, name %s }",
240 ((const SOCKADDR_IRDA
*)a
)->irdaServiceName
);
243 return wine_dbg_sprintf("{ family %d }", a
->sa_family
);
247 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
248 #define SOCKET2HANDLE(s) ((HANDLE)(s))
249 #define HANDLE2SOCKET(h) ((SOCKET)(h))
251 /****************************************************************
252 * Async IO declarations
253 ****************************************************************/
255 typedef struct ws2_async
259 LPWSAOVERLAPPED user_overlapped
;
260 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func
;
261 IO_STATUS_BLOCK local_iosb
;
262 struct WS_sockaddr
*addr
;
265 int val
; /* for send operations */
266 int *ptr
; /* for recv operations */
271 unsigned int n_iovecs
;
272 unsigned int first_iovec
;
273 struct iovec iovec
[1];
276 typedef struct ws2_accept_async
278 HANDLE listen_socket
;
279 HANDLE accept_socket
;
280 LPOVERLAPPED user_overlapped
;
282 PVOID buf
; /* buffer to write data to */
286 struct ws2_async
*read
;
289 /****************************************************************/
291 /* ----------------------------------- internal data */
293 /* ws_... struct conversion flags */
295 typedef struct /* WSAAsyncSelect() control struct */
297 HANDLE service
, event
, sock
;
303 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
304 #define WS_MAX_UDP_DATAGRAM 1024
305 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
307 /* hostent's, servent's and protent's are stored in one buffer per thread,
308 * as documented on MSDN for the functions that return any of the buffers */
309 struct per_thread_data
312 struct WS_hostent
*he_buffer
;
313 struct WS_servent
*se_buffer
;
314 struct WS_protoent
*pe_buffer
;
320 /* internal: routing description information */
327 static INT num_startup
; /* reference counter */
328 static FARPROC blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
330 /* function prototypes */
331 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int addresses
, int fill_addresses
);
332 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
333 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
);
334 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
336 int WSAIOCTL_GetInterfaceCount(void);
337 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
339 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
, ULONG Information
);
341 #define MAP_OPTION(opt) { WS_##opt, opt }
343 static const int ws_sock_map
[][2] =
345 MAP_OPTION( SO_DEBUG
),
346 MAP_OPTION( SO_ACCEPTCONN
),
347 MAP_OPTION( SO_REUSEADDR
),
348 MAP_OPTION( SO_KEEPALIVE
),
349 MAP_OPTION( SO_DONTROUTE
),
350 MAP_OPTION( SO_BROADCAST
),
351 MAP_OPTION( SO_LINGER
),
352 MAP_OPTION( SO_OOBINLINE
),
353 MAP_OPTION( SO_SNDBUF
),
354 MAP_OPTION( SO_RCVBUF
),
355 MAP_OPTION( SO_ERROR
),
356 MAP_OPTION( SO_TYPE
),
358 MAP_OPTION( SO_RCVTIMEO
),
361 MAP_OPTION( SO_SNDTIMEO
),
365 static const int ws_tcp_map
[][2] =
368 MAP_OPTION( TCP_NODELAY
),
372 static const int ws_ip_map
[][2] =
374 MAP_OPTION( IP_MULTICAST_IF
),
375 MAP_OPTION( IP_MULTICAST_TTL
),
376 MAP_OPTION( IP_MULTICAST_LOOP
),
377 MAP_OPTION( IP_ADD_MEMBERSHIP
),
378 MAP_OPTION( IP_DROP_MEMBERSHIP
),
379 MAP_OPTION( IP_OPTIONS
),
381 MAP_OPTION( IP_HDRINCL
),
383 MAP_OPTION( IP_TOS
),
384 MAP_OPTION( IP_TTL
),
386 MAP_OPTION( IP_PKTINFO
),
390 static const int ws_ipv6_map
[][2] =
392 #ifdef IPV6_ADD_MEMBERSHIP
393 MAP_OPTION( IPV6_ADD_MEMBERSHIP
),
395 #ifdef IPV6_DROP_MEMBERSHIP
396 MAP_OPTION( IPV6_DROP_MEMBERSHIP
),
398 MAP_OPTION( IPV6_MULTICAST_IF
),
399 MAP_OPTION( IPV6_MULTICAST_HOPS
),
400 MAP_OPTION( IPV6_MULTICAST_LOOP
),
401 MAP_OPTION( IPV6_UNICAST_HOPS
),
402 MAP_OPTION( IPV6_V6ONLY
),
405 static const int ws_af_map
[][2] =
407 MAP_OPTION( AF_UNSPEC
),
408 MAP_OPTION( AF_INET
),
409 MAP_OPTION( AF_INET6
),
411 MAP_OPTION( AF_IPX
),
414 MAP_OPTION( AF_IRDA
),
416 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
419 static const int ws_socktype_map
[][2] =
421 MAP_OPTION( SOCK_DGRAM
),
422 MAP_OPTION( SOCK_STREAM
),
423 MAP_OPTION( SOCK_RAW
),
424 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
427 static const int ws_proto_map
[][2] =
429 MAP_OPTION( IPPROTO_IP
),
430 MAP_OPTION( IPPROTO_TCP
),
431 MAP_OPTION( IPPROTO_UDP
),
432 MAP_OPTION( IPPROTO_ICMP
),
433 MAP_OPTION( IPPROTO_IGMP
),
434 MAP_OPTION( IPPROTO_RAW
),
435 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
438 static const int ws_aiflag_map
[][2] =
440 MAP_OPTION( AI_PASSIVE
),
441 MAP_OPTION( AI_CANONNAME
),
442 MAP_OPTION( AI_NUMERICHOST
),
443 MAP_OPTION( AI_ADDRCONFIG
),
446 static const int ws_niflag_map
[][2] =
448 MAP_OPTION( NI_NOFQDN
),
449 MAP_OPTION( NI_NUMERICHOST
),
450 MAP_OPTION( NI_NAMEREQD
),
451 MAP_OPTION( NI_NUMERICSERV
),
452 MAP_OPTION( NI_DGRAM
),
455 static const int ws_eai_map
[][2] =
457 MAP_OPTION( EAI_AGAIN
),
458 MAP_OPTION( EAI_BADFLAGS
),
459 MAP_OPTION( EAI_FAIL
),
460 MAP_OPTION( EAI_FAMILY
),
461 MAP_OPTION( EAI_MEMORY
),
462 /* Note: EAI_NODATA is deprecated, but still
463 * used by Windows and Linux... We map the newer
464 * EAI_NONAME to EAI_NODATA for now until Windows
468 MAP_OPTION( EAI_NODATA
),
471 { WS_EAI_NODATA
, EAI_NONAME
},
474 MAP_OPTION( EAI_SERVICE
),
475 MAP_OPTION( EAI_SOCKTYPE
),
479 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
481 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
482 static inline WSACMSGHDR
*fill_control_message(int level
, int type
, WSACMSGHDR
*current
, ULONG
*maxsize
, void *data
, int len
)
484 ULONG msgsize
= sizeof(WSACMSGHDR
) + WSA_CMSG_ALIGN(len
);
485 char *ptr
= (char *) current
+ sizeof(WSACMSGHDR
);
487 /* Make sure there is at least enough room for this entry */
488 if (msgsize
> *maxsize
)
491 /* Fill in the entry */
492 current
->cmsg_len
= sizeof(WSACMSGHDR
) + len
;
493 current
->cmsg_level
= level
;
494 current
->cmsg_type
= type
;
495 memcpy(ptr
, data
, len
);
496 /* Return the pointer to where next entry should go */
497 return (WSACMSGHDR
*) (ptr
+ WSA_CMSG_ALIGN(len
));
500 static inline int convert_control_headers(struct msghdr
*hdr
, WSABUF
*control
)
503 WSACMSGHDR
*cmsg_win
= (WSACMSGHDR
*) control
->buf
, *ptr
;
504 ULONG ctlsize
= control
->len
;
505 struct cmsghdr
*cmsg_unix
;
508 /* Loop over all the headers, converting as appropriate */
509 for (cmsg_unix
= CMSG_FIRSTHDR(hdr
); cmsg_unix
!= NULL
; cmsg_unix
= CMSG_NXTHDR(hdr
, cmsg_unix
))
511 switch(cmsg_unix
->cmsg_level
)
514 switch(cmsg_unix
->cmsg_type
)
518 /* Convert the Unix IP_PKTINFO structure to the Windows version */
519 struct in_pktinfo
*data_unix
= (struct in_pktinfo
*) CMSG_DATA(cmsg_unix
);
520 struct WS_in_pktinfo data_win
;
522 memcpy(&data_win
.ipi_addr
,&data_unix
->ipi_addr
.s_addr
,4); /* 4 bytes = 32 address bits */
523 data_win
.ipi_ifindex
= data_unix
->ipi_ifindex
;
524 ptr
= fill_control_message(WS_IPPROTO_IP
, WS_IP_PKTINFO
, ptr
, &ctlsize
,
525 (void*)&data_win
, sizeof(data_win
));
526 if (!ptr
) goto error
;
529 FIXME("Unhandled IPPROTO_IP message header type %d\n", cmsg_unix
->cmsg_type
);
534 FIXME("Unhandled message header level %d\n", cmsg_unix
->cmsg_level
);
540 /* Set the length of the returned control headers */
541 control
->len
= (ptr
== NULL
? 0 : (char*)ptr
- (char*)cmsg_win
);
542 return (ptr
!= NULL
);
543 #else /* IP_PKTINFO */
546 #endif /* IP_PKTINFO */
548 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
550 /* ----------------------------------- error handling */
552 static NTSTATUS
sock_get_ntstatus( int err
)
556 case EBADF
: return STATUS_INVALID_HANDLE
;
557 case EBUSY
: return STATUS_DEVICE_BUSY
;
559 case EACCES
: return STATUS_ACCESS_DENIED
;
560 case EFAULT
: return STATUS_NO_MEMORY
;
561 case EINVAL
: return STATUS_INVALID_PARAMETER
;
563 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
564 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
565 case EINPROGRESS
: return STATUS_PENDING
;
566 case EALREADY
: return STATUS_NETWORK_BUSY
;
567 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
568 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
569 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
570 case EPROTONOSUPPORT
:
571 case ESOCKTNOSUPPORT
:
574 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
575 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
576 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
577 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
578 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
579 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
580 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
581 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
582 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
583 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
584 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
586 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
587 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
589 case 0: return STATUS_SUCCESS
;
591 WARN("Unknown errno %d!\n", err
);
592 return STATUS_UNSUCCESSFUL
;
596 static UINT
sock_get_error( int err
)
600 case EINTR
: return WSAEINTR
;
601 case EBADF
: return WSAEBADF
;
603 case EACCES
: return WSAEACCES
;
604 case EFAULT
: return WSAEFAULT
;
605 case EINVAL
: return WSAEINVAL
;
606 case EMFILE
: return WSAEMFILE
;
607 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
608 case EINPROGRESS
: return WSAEINPROGRESS
;
609 case EALREADY
: return WSAEALREADY
;
610 case ENOTSOCK
: return WSAENOTSOCK
;
611 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
612 case EMSGSIZE
: return WSAEMSGSIZE
;
613 case EPROTOTYPE
: return WSAEPROTOTYPE
;
614 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
615 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
616 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
617 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
618 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
619 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
620 case EADDRINUSE
: return WSAEADDRINUSE
;
621 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
622 case ENETDOWN
: return WSAENETDOWN
;
623 case ENETUNREACH
: return WSAENETUNREACH
;
624 case ENETRESET
: return WSAENETRESET
;
625 case ECONNABORTED
: return WSAECONNABORTED
;
627 case ECONNRESET
: return WSAECONNRESET
;
628 case ENOBUFS
: return WSAENOBUFS
;
629 case EISCONN
: return WSAEISCONN
;
630 case ENOTCONN
: return WSAENOTCONN
;
631 case ESHUTDOWN
: return WSAESHUTDOWN
;
632 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
633 case ETIMEDOUT
: return WSAETIMEDOUT
;
634 case ECONNREFUSED
: return WSAECONNREFUSED
;
635 case ELOOP
: return WSAELOOP
;
636 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
637 case EHOSTDOWN
: return WSAEHOSTDOWN
;
638 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
639 case ENOTEMPTY
: return WSAENOTEMPTY
;
641 case EPROCLIM
: return WSAEPROCLIM
;
644 case EUSERS
: return WSAEUSERS
;
647 case EDQUOT
: return WSAEDQUOT
;
650 case ESTALE
: return WSAESTALE
;
653 case EREMOTE
: return WSAEREMOTE
;
656 /* just in case we ever get here and there are no problems */
659 WARN("Unknown errno %d!\n", err
);
660 return WSAEOPNOTSUPP
;
664 static UINT
wsaErrno(void)
666 int loc_errno
= errno
;
667 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
669 return sock_get_error( loc_errno
);
672 /* most ws2 overlapped functions return an ntstatus-based error code */
673 static NTSTATUS
wsaErrStatus(void)
675 int loc_errno
= errno
;
676 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
678 return sock_get_ntstatus(loc_errno
);
681 static UINT
wsaHerrno(int loc_errno
)
683 WARN("h_errno %d.\n", loc_errno
);
687 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
688 case TRY_AGAIN
: return WSATRY_AGAIN
;
689 case NO_RECOVERY
: return WSANO_RECOVERY
;
690 case NO_DATA
: return WSANO_DATA
;
691 case ENOBUFS
: return WSAENOBUFS
;
695 WARN("Unknown h_errno %d!\n", loc_errno
);
696 return WSAEOPNOTSUPP
;
700 static inline DWORD
NtStatusToWSAError( const DWORD status
)
702 /* We only need to cover the status codes set by server async request handling */
706 case STATUS_SUCCESS
: wserr
= 0; break;
707 case STATUS_PENDING
: wserr
= WSA_IO_PENDING
; break;
708 case STATUS_OBJECT_TYPE_MISMATCH
: wserr
= WSAENOTSOCK
; break;
709 case STATUS_INVALID_HANDLE
: wserr
= WSAEBADF
; break;
710 case STATUS_INVALID_PARAMETER
: wserr
= WSAEINVAL
; break;
711 case STATUS_PIPE_DISCONNECTED
: wserr
= WSAESHUTDOWN
; break;
712 case STATUS_NETWORK_BUSY
: wserr
= WSAEALREADY
; break;
713 case STATUS_NETWORK_UNREACHABLE
: wserr
= WSAENETUNREACH
; break;
714 case STATUS_CONNECTION_REFUSED
: wserr
= WSAECONNREFUSED
; break;
715 case STATUS_CONNECTION_DISCONNECTED
: wserr
= WSAENOTCONN
; break;
716 case STATUS_CONNECTION_RESET
: wserr
= WSAECONNRESET
; break;
717 case STATUS_CONNECTION_ABORTED
: wserr
= WSAECONNABORTED
; break;
718 case STATUS_CANCELLED
: wserr
= WSA_OPERATION_ABORTED
; break;
719 case STATUS_ADDRESS_ALREADY_ASSOCIATED
: wserr
= WSAEADDRINUSE
; break;
720 case STATUS_IO_TIMEOUT
:
721 case STATUS_TIMEOUT
: wserr
= WSAETIMEDOUT
; break;
722 case STATUS_NO_MEMORY
: wserr
= WSAEFAULT
; break;
723 case STATUS_ACCESS_DENIED
: wserr
= WSAEACCES
; break;
724 case STATUS_TOO_MANY_OPENED_FILES
: wserr
= WSAEMFILE
; break;
725 case STATUS_CANT_WAIT
: wserr
= WSAEWOULDBLOCK
; break;
726 case STATUS_BUFFER_OVERFLOW
: wserr
= WSAEMSGSIZE
; break;
727 case STATUS_NOT_SUPPORTED
: wserr
= WSAEOPNOTSUPP
; break;
728 case STATUS_HOST_UNREACHABLE
: wserr
= WSAEHOSTUNREACH
; break;
731 wserr
= RtlNtStatusToDosError( status
);
732 FIXME( "Status code %08x converted to DOS error code %x\n", status
, wserr
);
737 /* set last error code from NT status without mapping WSA errors */
738 static inline unsigned int set_error( unsigned int err
)
742 err
= NtStatusToWSAError( err
);
748 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
751 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
756 static inline void release_sock_fd( SOCKET s
, int fd
)
758 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
761 static void _enable_event( HANDLE s
, unsigned int event
,
762 unsigned int sstate
, unsigned int cstate
)
764 SERVER_START_REQ( enable_socket_event
)
766 req
->handle
= wine_server_obj_handle( s
);
768 req
->sstate
= sstate
;
769 req
->cstate
= cstate
;
770 wine_server_call( req
);
775 static int _is_blocking(SOCKET s
)
778 SERVER_START_REQ( get_socket_event
)
780 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
781 req
->service
= FALSE
;
783 wine_server_call( req
);
784 ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
790 static unsigned int _get_sock_mask(SOCKET s
)
793 SERVER_START_REQ( get_socket_event
)
795 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
796 req
->service
= FALSE
;
798 wine_server_call( req
);
805 static void _sync_sock_state(SOCKET s
)
807 /* do a dummy wineserver request in order to let
808 the wineserver run through its select loop once */
809 (void)_is_blocking(s
);
812 static int _get_sock_error(SOCKET s
, unsigned int bit
)
814 int events
[FD_MAX_EVENTS
];
816 SERVER_START_REQ( get_socket_event
)
818 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
819 req
->service
= FALSE
;
821 wine_server_set_reply( req
, events
, sizeof(events
) );
822 wine_server_call( req
);
828 static struct per_thread_data
*get_per_thread_data(void)
830 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
831 /* lazy initialization */
834 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
835 NtCurrentTeb()->WinSockData
= ptb
;
840 static void free_per_thread_data(void)
842 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
846 /* delete scratch buffers */
847 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
848 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
849 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
850 ptb
->he_buffer
= NULL
;
851 ptb
->se_buffer
= NULL
;
852 ptb
->pe_buffer
= NULL
;
854 HeapFree( GetProcessHeap(), 0, ptb
);
855 NtCurrentTeb()->WinSockData
= NULL
;
858 /***********************************************************************
859 * DllMain (WS2_32.init)
861 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
863 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
865 case DLL_PROCESS_ATTACH
:
867 case DLL_PROCESS_DETACH
:
868 free_per_thread_data();
869 DeleteCriticalSection(&csWSgetXXXbyYYY
);
872 case DLL_THREAD_DETACH
:
873 free_per_thread_data();
879 /***********************************************************************
882 * Converts socket flags from Windows format.
883 * Return 1 if converted, 0 if not (error).
885 static int convert_sockopt(INT
*level
, INT
*optname
)
892 for(i
=0; i
<sizeof(ws_sock_map
)/sizeof(ws_sock_map
[0]); i
++) {
893 if( ws_sock_map
[i
][0] == *optname
)
895 *optname
= ws_sock_map
[i
][1];
899 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
902 *level
= IPPROTO_TCP
;
903 for(i
=0; i
<sizeof(ws_tcp_map
)/sizeof(ws_tcp_map
[0]); i
++) {
904 if ( ws_tcp_map
[i
][0] == *optname
)
906 *optname
= ws_tcp_map
[i
][1];
910 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
914 for(i
=0; i
<sizeof(ws_ip_map
)/sizeof(ws_ip_map
[0]); i
++) {
915 if (ws_ip_map
[i
][0] == *optname
)
917 *optname
= ws_ip_map
[i
][1];
921 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
923 case WS_IPPROTO_IPV6
:
924 *level
= IPPROTO_IPV6
;
925 for(i
=0; i
<sizeof(ws_ipv6_map
)/sizeof(ws_ipv6_map
[0]); i
++) {
926 if (ws_ipv6_map
[i
][0] == *optname
)
928 *optname
= ws_ipv6_map
[i
][1];
932 FIXME("Unknown IPPROTO_IPV6 optname 0x%x\n", *optname
);
934 default: FIXME("Unimplemented or unknown socket level\n");
939 /* ----------------------------------- Per-thread info (or per-process?) */
941 static char *strdup_lower(const char *str
)
944 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
948 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
951 else SetLastError(WSAENOBUFS
);
955 static inline int sock_error_p(int s
)
957 unsigned int optval
, optlen
;
959 optlen
= sizeof(optval
);
960 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
961 if (optval
) WARN("\t[%i] error: %d\n", s
, optval
);
965 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
966 * from an fd and return the value converted to milli seconds
967 * or -1 if there is an infinite time out */
968 static inline int get_rcvsnd_timeo( int fd
, int optname
)
971 unsigned int len
= sizeof(tv
);
972 int ret
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
974 ret
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
975 if( ret
<= 0 ) /* tv == {0,0} means infinite time out */
980 /* macro wrappers for portability */
982 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
984 #define GET_RCVTIMEO(fd) (-1)
988 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
990 #define GET_SNDTIMEO(fd) (-1)
993 /* utility: given an fd, will block until one of the events occurs */
994 static inline int do_block( int fd
, int events
, int timeout
)
1000 pfd
.events
= events
;
1002 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
1013 convert_af_w2u(int windowsaf
) {
1016 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
1017 if (ws_af_map
[i
][0] == windowsaf
)
1018 return ws_af_map
[i
][1];
1019 FIXME("unhandled Windows address family %d\n", windowsaf
);
1024 convert_af_u2w(int unixaf
) {
1027 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
1028 if (ws_af_map
[i
][1] == unixaf
)
1029 return ws_af_map
[i
][0];
1030 FIXME("unhandled UNIX address family %d\n", unixaf
);
1035 convert_proto_w2u(int windowsproto
) {
1038 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
1039 if (ws_proto_map
[i
][0] == windowsproto
)
1040 return ws_proto_map
[i
][1];
1041 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
1046 convert_proto_u2w(int unixproto
) {
1049 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
1050 if (ws_proto_map
[i
][1] == unixproto
)
1051 return ws_proto_map
[i
][0];
1052 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
1057 convert_socktype_w2u(int windowssocktype
) {
1060 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
1061 if (ws_socktype_map
[i
][0] == windowssocktype
)
1062 return ws_socktype_map
[i
][1];
1063 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
1068 convert_socktype_u2w(int unixsocktype
) {
1071 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
1072 if (ws_socktype_map
[i
][1] == unixsocktype
)
1073 return ws_socktype_map
[i
][0];
1074 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
1078 /* ----------------------------------- API -----
1080 * Init / cleanup / error checking.
1083 /***********************************************************************
1084 * WSAStartup (WS2_32.115)
1086 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
1088 TRACE("verReq=%x\n", wVersionRequested
);
1090 if (LOBYTE(wVersionRequested
) < 1)
1091 return WSAVERNOTSUPPORTED
;
1093 if (!lpWSAData
) return WSAEINVAL
;
1097 /* that's the whole of the negotiation for now */
1098 lpWSAData
->wVersion
= wVersionRequested
;
1099 /* return winsock information */
1100 lpWSAData
->wHighVersion
= 0x0202;
1101 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
1102 strcpy(lpWSAData
->szSystemStatus
, "Running" );
1103 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
1104 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
1105 /* don't do anything with lpWSAData->lpVendorInfo */
1106 /* (some apps don't allocate the space for this field) */
1108 TRACE("succeeded\n");
1113 /***********************************************************************
1114 * WSACleanup (WS2_32.116)
1116 INT WINAPI
WSACleanup(void)
1122 SetLastError(WSANOTINITIALISED
);
1123 return SOCKET_ERROR
;
1127 /***********************************************************************
1128 * WSAGetLastError (WS2_32.111)
1130 INT WINAPI
WSAGetLastError(void)
1132 return GetLastError();
1135 /***********************************************************************
1136 * WSASetLastError (WS2_32.112)
1138 void WINAPI
WSASetLastError(INT iError
) {
1139 SetLastError(iError
);
1142 static struct WS_hostent
*check_buffer_he(int size
)
1144 struct per_thread_data
* ptb
= get_per_thread_data();
1147 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
1148 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1150 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
1151 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
1152 return ptb
->he_buffer
;
1155 static struct WS_servent
*check_buffer_se(int size
)
1157 struct per_thread_data
* ptb
= get_per_thread_data();
1160 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
1161 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1163 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
1164 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
1165 return ptb
->se_buffer
;
1168 static struct WS_protoent
*check_buffer_pe(int size
)
1170 struct per_thread_data
* ptb
= get_per_thread_data();
1173 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
1174 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1176 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
1177 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
1178 return ptb
->pe_buffer
;
1181 /* ----------------------------------- i/o APIs */
1183 static inline BOOL
supported_pf(int pf
)
1204 /**********************************************************************/
1206 /* Returns the length of the converted address if successful, 0 if it was too small to
1209 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
1210 union generic_unix_sockaddr
*uaddr
)
1212 unsigned int uaddrlen
= 0;
1214 switch (wsaddr
->sa_family
)
1219 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
1220 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
1222 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
1225 uaddrlen
= sizeof(struct sockaddr_ipx
);
1226 memset( uaddr
, 0, uaddrlen
);
1227 uipx
->sipx_family
=AF_IPX
;
1228 uipx
->sipx_port
=wsipx
->sa_socket
;
1229 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
1232 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
1233 #ifdef IPX_FRAME_NONE
1234 uipx
->sipx_type
=IPX_FRAME_NONE
;
1240 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
1241 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
1243 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
1244 * scope_id, one without.
1246 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
1247 uaddrlen
= sizeof(struct sockaddr_in6
);
1248 memset( uaddr
, 0, uaddrlen
);
1249 uin6
->sin6_family
= AF_INET6
;
1250 uin6
->sin6_port
= win6
->sin6_port
;
1251 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
1252 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1253 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
1255 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1258 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
1262 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
1263 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
1265 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
1267 uaddrlen
= sizeof(struct sockaddr_in
);
1268 memset( uaddr
, 0, uaddrlen
);
1269 uin
->sin_family
= AF_INET
;
1270 uin
->sin_port
= win
->sin_port
;
1271 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
1276 struct sockaddr_irda
*uin
= (struct sockaddr_irda
*)uaddr
;
1277 const SOCKADDR_IRDA
*win
= (const SOCKADDR_IRDA
*)wsaddr
;
1279 if (wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1281 uaddrlen
= sizeof(struct sockaddr_irda
);
1282 memset( uaddr
, 0, uaddrlen
);
1283 uin
->sir_family
= AF_IRDA
;
1284 if (!strncmp( win
->irdaServiceName
, "LSAP-SEL", strlen( "LSAP-SEL" ) ))
1286 unsigned int lsap_sel
= 0;
1288 sscanf( win
->irdaServiceName
, "LSAP-SEL%u", &lsap_sel
);
1289 uin
->sir_lsap_sel
= lsap_sel
;
1293 uin
->sir_lsap_sel
= LSAP_ANY
;
1294 memcpy( uin
->sir_name
, win
->irdaServiceName
, 25 );
1296 memcpy( &uin
->sir_addr
, win
->irdaDeviceID
, sizeof(uin
->sir_addr
) );
1300 case WS_AF_UNSPEC
: {
1301 /* Try to determine the needed space by the passed windows sockaddr space */
1302 switch (wsaddrlen
) {
1303 default: /* likely a ipv4 address */
1304 case sizeof(struct WS_sockaddr_in
):
1305 uaddrlen
= sizeof(struct sockaddr_in
);
1308 case sizeof(struct WS_sockaddr_ipx
):
1309 uaddrlen
= sizeof(struct sockaddr_ipx
);
1313 case sizeof(SOCKADDR_IRDA
):
1314 uaddrlen
= sizeof(struct sockaddr_irda
);
1317 case sizeof(struct WS_sockaddr_in6
):
1318 case sizeof(struct WS_sockaddr_in6_old
):
1319 uaddrlen
= sizeof(struct sockaddr_in6
);
1322 memset( uaddr
, 0, uaddrlen
);
1326 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
1332 static BOOL
is_sockaddr_bound(const struct sockaddr
*uaddr
, int uaddrlen
)
1334 switch (uaddr
->sa_family
)
1338 FIXME("don't know how to tell if IPX socket is bound, assuming it is!\n");
1343 static const struct sockaddr_in6 emptyAddr
;
1344 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) uaddr
;
1345 return in6
->sin6_port
|| memcmp(&in6
->sin6_addr
, &emptyAddr
.sin6_addr
, sizeof(struct in6_addr
));
1349 static const struct sockaddr_in emptyAddr
;
1350 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) uaddr
;
1351 return in
->sin_port
|| memcmp(&in
->sin_addr
, &emptyAddr
.sin_addr
, sizeof(struct in_addr
));
1356 FIXME("unknown address family %d\n", uaddr
->sa_family
);
1361 /* Returns 0 if successful, -1 if the buffer is too small */
1362 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
1366 switch(uaddr
->sa_family
)
1371 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
1372 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
1375 switch (*wsaddrlen
) /* how much can we copy? */
1379 *wsaddrlen
= sizeof(*wsipx
);
1380 wsipx
->sa_socket
=uipx
->sipx_port
;
1384 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
1392 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
1398 wsipx
->sa_family
=WS_AF_IPX
;
1410 const struct sockaddr_irda
*uin
= (const struct sockaddr_irda
*)uaddr
;
1411 SOCKADDR_IRDA
*win
= (SOCKADDR_IRDA
*)wsaddr
;
1413 if (*wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1415 win
->irdaAddressFamily
= WS_AF_IRDA
;
1416 memcpy( win
->irdaDeviceID
, &uin
->sir_addr
, sizeof(win
->irdaDeviceID
) );
1417 if (uin
->sir_lsap_sel
!= LSAP_ANY
)
1418 sprintf( win
->irdaServiceName
, "LSAP-SEL%u", uin
->sir_lsap_sel
);
1420 memcpy( win
->irdaServiceName
, uin
->sir_name
,
1421 sizeof(win
->irdaServiceName
) );
1426 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
1427 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
1429 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
1431 win6old
->sin6_family
= WS_AF_INET6
;
1432 win6old
->sin6_port
= uin6
->sin6_port
;
1433 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
1434 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1435 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1436 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
1437 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
1438 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
1439 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
1442 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1444 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1449 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
1450 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
1452 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
1454 win
->sin_family
= WS_AF_INET
;
1455 win
->sin_port
= uin
->sin_port
;
1456 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
1457 memset(win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
1458 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
1462 memset(wsaddr
,0,*wsaddrlen
);
1466 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
1472 /**************************************************************************
1473 * Functions for handling overlapped I/O
1474 **************************************************************************/
1476 /* user APC called upon async completion */
1477 static void WINAPI
ws2_async_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1479 ws2_async
*wsa
= arg
;
1481 if (wsa
->completion_func
) wsa
->completion_func( NtStatusToWSAError(iosb
->u
.Status
),
1482 iosb
->Information
, wsa
->user_overlapped
,
1484 HeapFree( GetProcessHeap(), 0, wsa
);
1487 /***********************************************************************
1488 * WS2_recv (INTERNAL)
1490 * Workhorse for both synchronous and asynchronous recv() operations.
1492 static int WS2_recv( int fd
, struct ws2_async
*wsa
)
1494 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1498 union generic_unix_sockaddr unix_sockaddr
;
1501 hdr
.msg_name
= NULL
;
1505 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
1506 hdr
.msg_name
= &unix_sockaddr
;
1509 hdr
.msg_namelen
= 0;
1511 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1512 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1513 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1514 hdr
.msg_accrights
= NULL
;
1515 hdr
.msg_accrightslen
= 0;
1517 hdr
.msg_control
= pktbuf
;
1518 hdr
.msg_controllen
= sizeof(pktbuf
);
1522 if ( (n
= recvmsg(fd
, &hdr
, wsa
->flags
)) == -1 )
1525 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1528 ERR("Message control headers cannot be properly supported on this system.\n");
1529 wsa
->control
->len
= 0;
1532 if (wsa
->control
&& !convert_control_headers(&hdr
, wsa
->control
))
1534 WARN("Application passed insufficient room for control headers.\n");
1535 *wsa
->lpFlags
|= WS_MSG_CTRUNC
;
1541 /* if this socket is connected and lpFrom is not NULL, Linux doesn't give us
1542 * msg_name and msg_namelen from recvmsg, but it does set msg_namelen to zero.
1544 * quoting linux 2.6 net/ipv4/tcp.c:
1545 * "According to UNIX98, msg_name/msg_namelen are ignored
1546 * on connected socket. I was just happy when found this 8) --ANK"
1548 * likewise MSDN says that lpFrom and lpFromlen are ignored for
1549 * connection-oriented sockets, so don't try to update lpFrom.
1551 if (wsa
->addr
&& hdr
.msg_namelen
)
1552 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, wsa
->addr
, wsa
->addrlen
.ptr
);
1557 /***********************************************************************
1558 * WS2_async_recv (INTERNAL)
1560 * Handler for overlapped recv() operations.
1562 static NTSTATUS
WS2_async_recv( void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
1564 ws2_async
* wsa
= user
;
1569 case STATUS_ALERTED
:
1570 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
1573 result
= WS2_recv( fd
, wsa
);
1574 wine_server_release_fd( wsa
->hSocket
, fd
);
1577 status
= STATUS_SUCCESS
;
1578 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1582 if (errno
== EINTR
|| errno
== EAGAIN
)
1584 status
= STATUS_PENDING
;
1585 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1590 status
= wsaErrStatus();
1595 if (status
!= STATUS_PENDING
)
1597 iosb
->u
.Status
= status
;
1598 iosb
->Information
= result
;
1599 *apc
= ws2_async_apc
;
1604 /* user APC called upon async accept completion */
1605 static void WINAPI
ws2_async_accept_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1607 struct ws2_accept_async
*wsa
= arg
;
1609 HeapFree( GetProcessHeap(), 0, wsa
->read
);
1610 HeapFree( GetProcessHeap(), 0, wsa
);
1613 /***********************************************************************
1614 * WS2_async_accept_recv (INTERNAL)
1616 * This function is used to finish the read part of an accept request. It is
1617 * needed to place the completion on the correct socket (listener).
1619 static NTSTATUS
WS2_async_accept_recv( void *arg
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
, void **apc
)
1622 struct ws2_accept_async
*wsa
= arg
;
1624 status
= WS2_async_recv( wsa
->read
, iosb
, status
, &junk
);
1625 if (status
== STATUS_PENDING
)
1628 if (wsa
->user_overlapped
->hEvent
)
1629 SetEvent(wsa
->user_overlapped
->hEvent
);
1631 WS_AddCompletion( HANDLE2SOCKET(wsa
->listen_socket
), wsa
->cvalue
, iosb
->u
.Status
, iosb
->Information
);
1633 *apc
= ws2_async_accept_apc
;
1637 /***********************************************************************
1638 * WS2_async_accept (INTERNAL)
1640 * This is the function called to satisfy the AcceptEx callback
1642 static NTSTATUS
WS2_async_accept( void *arg
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
, void **apc
)
1644 struct ws2_accept_async
*wsa
= arg
;
1648 TRACE("status: 0x%x listen: %p, accept: %p\n", status
, wsa
->listen_socket
, wsa
->accept_socket
);
1650 if (status
== STATUS_ALERTED
)
1652 SERVER_START_REQ( accept_into_socket
)
1654 req
->lhandle
= wine_server_obj_handle( wsa
->listen_socket
);
1655 req
->ahandle
= wine_server_obj_handle( wsa
->accept_socket
);
1656 status
= wine_server_call( req
);
1660 if (status
== STATUS_CANT_WAIT
)
1661 return STATUS_PENDING
;
1663 if (status
== STATUS_INVALID_HANDLE
)
1665 FIXME("AcceptEx accepting socket closed but request was not cancelled\n");
1666 status
= STATUS_CANCELLED
;
1669 else if (status
== STATUS_HANDLES_CLOSED
)
1670 status
= STATUS_CANCELLED
; /* strange windows behavior */
1672 if (status
!= STATUS_SUCCESS
)
1675 /* WS2 Spec says size param is extra 16 bytes long...what do we put in it? */
1676 addr
= ((char *)wsa
->buf
) + wsa
->data_len
;
1677 len
= wsa
->local_len
- sizeof(int);
1678 WS_getsockname(HANDLE2SOCKET(wsa
->accept_socket
),
1679 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
1682 addr
+= wsa
->local_len
;
1683 len
= wsa
->remote_len
- sizeof(int);
1684 WS_getpeername(HANDLE2SOCKET(wsa
->accept_socket
),
1685 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
1691 SERVER_START_REQ( register_async
)
1693 req
->type
= ASYNC_TYPE_READ
;
1694 req
->async
.handle
= wine_server_obj_handle( wsa
->accept_socket
);
1695 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept_recv
);
1696 req
->async
.iosb
= wine_server_client_ptr( iosb
);
1697 req
->async
.arg
= wine_server_client_ptr( wsa
);
1698 status
= wine_server_call( req
);
1702 if (status
!= STATUS_PENDING
)
1705 return STATUS_SUCCESS
;
1708 iosb
->u
.Status
= status
;
1709 iosb
->Information
= 0;
1711 if (wsa
->user_overlapped
->hEvent
)
1712 SetEvent(wsa
->user_overlapped
->hEvent
);
1714 WS_AddCompletion( HANDLE2SOCKET(wsa
->listen_socket
), wsa
->cvalue
, iosb
->u
.Status
, iosb
->Information
);
1716 *apc
= ws2_async_accept_apc
;
1720 /***********************************************************************
1721 * WS2_send (INTERNAL)
1723 * Workhorse for both synchronous and asynchronous send() operations.
1725 static int WS2_send( int fd
, struct ws2_async
*wsa
)
1728 union generic_unix_sockaddr unix_addr
;
1731 hdr
.msg_name
= NULL
;
1732 hdr
.msg_namelen
= 0;
1736 hdr
.msg_name
= &unix_addr
;
1737 hdr
.msg_namelen
= ws_sockaddr_ws2u( wsa
->addr
, wsa
->addrlen
.val
, &unix_addr
);
1738 if ( !hdr
.msg_namelen
)
1744 #if defined(HAVE_IPX) && defined(SOL_IPX)
1745 if(wsa
->addr
->sa_family
== WS_AF_IPX
)
1747 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
1749 unsigned int len
=sizeof(int);
1751 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1752 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1753 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1754 * ipx type in the sockaddr_opx structure with the stored value.
1756 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
1757 uipx
->sipx_type
= val
;
1762 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1763 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1764 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1765 hdr
.msg_accrights
= NULL
;
1766 hdr
.msg_accrightslen
= 0;
1768 hdr
.msg_control
= NULL
;
1769 hdr
.msg_controllen
= 0;
1773 ret
= sendmsg(fd
, &hdr
, wsa
->flags
);
1777 while (wsa
->first_iovec
< wsa
->n_iovecs
&& wsa
->iovec
[wsa
->first_iovec
].iov_len
<= n
)
1778 n
-= wsa
->iovec
[wsa
->first_iovec
++].iov_len
;
1779 if (wsa
->first_iovec
< wsa
->n_iovecs
)
1781 wsa
->iovec
[wsa
->first_iovec
].iov_base
= (char*)wsa
->iovec
[wsa
->first_iovec
].iov_base
+ n
;
1782 wsa
->iovec
[wsa
->first_iovec
].iov_len
-= n
;
1788 /***********************************************************************
1789 * WS2_async_send (INTERNAL)
1791 * Handler for overlapped send() operations.
1793 static NTSTATUS
WS2_async_send(void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
1795 ws2_async
* wsa
= user
;
1800 case STATUS_ALERTED
:
1801 if ( wsa
->n_iovecs
<= wsa
->first_iovec
)
1804 status
= STATUS_SUCCESS
;
1807 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
1810 /* check to see if the data is ready (non-blocking) */
1811 result
= WS2_send( fd
, wsa
);
1812 wine_server_release_fd( wsa
->hSocket
, fd
);
1816 if (wsa
->first_iovec
< wsa
->n_iovecs
)
1817 status
= STATUS_PENDING
;
1819 status
= STATUS_SUCCESS
;
1821 iosb
->Information
+= result
;
1823 else if (errno
== EINTR
|| errno
== EAGAIN
)
1825 status
= STATUS_PENDING
;
1829 status
= wsaErrStatus();
1833 if (status
!= STATUS_PENDING
)
1835 iosb
->u
.Status
= status
;
1836 *apc
= ws2_async_apc
;
1841 /***********************************************************************
1842 * WS2_async_shutdown (INTERNAL)
1844 * Handler for shutdown() operations on overlapped sockets.
1846 static NTSTATUS
WS2_async_shutdown( void* user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
, void **apc
)
1848 ws2_async
* wsa
= user
;
1853 case STATUS_ALERTED
:
1854 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
1857 switch ( wsa
->type
)
1859 case ASYNC_TYPE_READ
: err
= shutdown( fd
, 0 ); break;
1860 case ASYNC_TYPE_WRITE
: err
= shutdown( fd
, 1 ); break;
1862 status
= err
? wsaErrStatus() : STATUS_SUCCESS
;
1863 wine_server_release_fd( wsa
->hSocket
, fd
);
1866 iosb
->u
.Status
= status
;
1867 iosb
->Information
= 0;
1868 *apc
= ws2_async_apc
;
1872 /***********************************************************************
1873 * WS2_register_async_shutdown (INTERNAL)
1875 * Helper function for WS_shutdown() on overlapped sockets.
1877 static int WS2_register_async_shutdown( SOCKET s
, int type
)
1879 struct ws2_async
*wsa
;
1882 TRACE("s %ld type %d\n", s
, type
);
1884 wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
1888 wsa
->hSocket
= SOCKET2HANDLE(s
);
1890 wsa
->completion_func
= NULL
;
1892 SERVER_START_REQ( register_async
)
1895 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
1896 req
->async
.callback
= wine_server_client_ptr( WS2_async_shutdown
);
1897 req
->async
.iosb
= wine_server_client_ptr( &wsa
->local_iosb
);
1898 req
->async
.arg
= wine_server_client_ptr( wsa
);
1899 req
->async
.cvalue
= 0;
1900 status
= wine_server_call( req
);
1904 if (status
!= STATUS_PENDING
)
1906 HeapFree( GetProcessHeap(), 0, wsa
);
1907 return NtStatusToWSAError( status
);
1912 /***********************************************************************
1915 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
,
1922 TRACE("socket %04lx\n", s
);
1923 is_blocking
= _is_blocking(s
);
1926 /* try accepting first (if there is a deferred connection) */
1927 SERVER_START_REQ( accept_socket
)
1929 req
->lhandle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1930 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
1931 req
->attributes
= OBJ_INHERIT
;
1932 status
= wine_server_call( req
);
1933 as
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
1938 if (addr
) WS_getpeername(as
, addr
, addrlen32
);
1941 if (is_blocking
&& status
== STATUS_CANT_WAIT
)
1943 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1945 do_block(fd
, POLLIN
, -1);
1946 _sync_sock_state(s
); /* let wineserver notice connection */
1947 release_sock_fd( s
, fd
);
1949 } while (is_blocking
&& status
== STATUS_CANT_WAIT
);
1952 return INVALID_SOCKET
;
1955 /***********************************************************************
1958 static BOOL WINAPI
WS2_AcceptEx(SOCKET listener
, SOCKET acceptor
, PVOID dest
, DWORD dest_len
,
1959 DWORD local_addr_len
, DWORD rem_addr_len
, LPDWORD received
,
1960 LPOVERLAPPED overlapped
)
1963 struct ws2_accept_async
*wsa
;
1965 ULONG_PTR cvalue
= (overlapped
&& ((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)overlapped
: 0;
1967 TRACE("(%lx, %lx, %p, %d, %d, %d, %p, %p)\n", listener
, acceptor
, dest
, dest_len
, local_addr_len
,
1968 rem_addr_len
, received
, overlapped
);
1972 SetLastError(WSAEINVAL
);
1978 SetLastError(WSA_INVALID_PARAMETER
);
1982 fd
= get_sock_fd( listener
, FILE_READ_DATA
, NULL
);
1985 SetLastError(WSAENOTSOCK
);
1988 release_sock_fd( listener
, fd
);
1990 fd
= get_sock_fd( acceptor
, FILE_READ_DATA
, NULL
);
1993 SetLastError(WSAEINVAL
);
1996 release_sock_fd( acceptor
, fd
);
1998 wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
2001 SetLastError(WSAEFAULT
);
2005 wsa
->listen_socket
= SOCKET2HANDLE(listener
);
2006 wsa
->accept_socket
= SOCKET2HANDLE(acceptor
);
2007 wsa
->user_overlapped
= overlapped
;
2008 wsa
->cvalue
= cvalue
;
2010 wsa
->data_len
= dest_len
;
2011 wsa
->local_len
= local_addr_len
;
2012 wsa
->remote_len
= rem_addr_len
;
2017 /* set up a read request if we need it */
2018 wsa
->read
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[1]) );
2021 HeapFree( GetProcessHeap(), 0, wsa
);
2022 SetLastError(WSAEFAULT
);
2026 wsa
->read
->hSocket
= wsa
->accept_socket
;
2027 wsa
->read
->flags
= 0;
2028 wsa
->read
->lpFlags
= &wsa
->read
->flags
;
2029 wsa
->read
->addr
= NULL
;
2030 wsa
->read
->addrlen
.ptr
= NULL
;
2031 wsa
->read
->control
= NULL
;
2032 wsa
->read
->n_iovecs
= 1;
2033 wsa
->read
->first_iovec
= 0;
2034 wsa
->read
->iovec
[0].iov_base
= wsa
->buf
;
2035 wsa
->read
->iovec
[0].iov_len
= wsa
->data_len
;
2038 SERVER_START_REQ( register_async
)
2040 req
->type
= ASYNC_TYPE_READ
;
2041 req
->async
.handle
= wine_server_obj_handle( SOCKET2HANDLE(listener
) );
2042 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept
);
2043 req
->async
.iosb
= wine_server_client_ptr( overlapped
);
2044 req
->async
.arg
= wine_server_client_ptr( wsa
);
2045 /* We don't set event or completion since we may also have to read */
2046 status
= wine_server_call( req
);
2050 if(status
!= STATUS_PENDING
)
2052 HeapFree( GetProcessHeap(), 0, wsa
->read
);
2053 HeapFree( GetProcessHeap(), 0, wsa
);
2056 SetLastError( NtStatusToWSAError(status
) );
2060 /***********************************************************************
2061 * GetAcceptExSockaddrs
2063 static void WINAPI
WS2_GetAcceptExSockaddrs(PVOID buffer
, DWORD data_size
, DWORD local_size
, DWORD remote_size
,
2064 struct WS_sockaddr
**local_addr
, LPINT local_addr_len
,
2065 struct WS_sockaddr
**remote_addr
, LPINT remote_addr_len
)
2067 char *cbuf
= buffer
;
2068 TRACE("(%p, %d, %d, %d, %p, %p, %p, %p)\n", buffer
, data_size
, local_size
, remote_size
, local_addr
,
2069 local_addr_len
, remote_addr
, remote_addr_len
);
2072 *local_addr_len
= *(int *) cbuf
;
2073 *local_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
2077 *remote_addr_len
= *(int *) cbuf
;
2078 *remote_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
2081 /***********************************************************************
2084 * Perform a receive operation that is capable of returning message
2085 * control headers. It is important to note that the WSAMSG parameter
2086 * must remain valid throughout the operation, even when an overlapped
2087 * receive is performed.
2089 static int WINAPI
WS2_WSARecvMsg( SOCKET s
, LPWSAMSG msg
, LPDWORD lpNumberOfBytesRecvd
,
2090 LPWSAOVERLAPPED lpOverlapped
,
2091 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2095 SetLastError( WSAEFAULT
);
2096 return SOCKET_ERROR
;
2099 return WS2_recv_base( s
, msg
->lpBuffers
, msg
->dwBufferCount
, lpNumberOfBytesRecvd
,
2100 &msg
->dwFlags
, msg
->name
, &msg
->namelen
,
2101 lpOverlapped
, lpCompletionRoutine
, &msg
->Control
);
2104 /***********************************************************************
2107 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
2109 int fd
= get_sock_fd( s
, 0, NULL
);
2110 int res
= SOCKET_ERROR
;
2112 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
2116 if (!name
|| (name
->sa_family
&& !supported_pf(name
->sa_family
)))
2118 SetLastError(WSAEAFNOSUPPORT
);
2122 union generic_unix_sockaddr uaddr
;
2123 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
2126 SetLastError(WSAEFAULT
);
2131 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) &uaddr
;
2132 if (name
->sa_family
== WS_AF_INET6
&&
2133 !memcmp(&in6
->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
)))
2136 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
)) == -1)
2138 release_sock_fd( s
, fd
);
2139 SetLastError(WSAEAFNOSUPPORT
);
2140 return SOCKET_ERROR
;
2144 if (name
->sa_family
== WS_AF_INET
)
2146 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
2147 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
2149 /* Trying to bind to the default host interface, using
2150 * INADDR_ANY instead*/
2151 WARN("Trying to bind to magic IP address, using "
2152 "INADDR_ANY instead.\n");
2153 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_ANY
);
2156 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
2158 int loc_errno
= errno
;
2159 WARN("\tfailure - errno = %i\n", errno
);
2164 SetLastError(WSAENOTSOCK
);
2167 SetLastError(WSAEINVAL
);
2170 SetLastError(wsaErrno());
2176 res
=0; /* success */
2180 release_sock_fd( s
, fd
);
2185 /***********************************************************************
2186 * closesocket (WS2_32.3)
2188 int WINAPI
WS_closesocket(SOCKET s
)
2190 TRACE("socket %04lx\n", s
);
2191 if (CloseHandle(SOCKET2HANDLE(s
))) return 0;
2192 return SOCKET_ERROR
;
2195 static int do_connect(int fd
, const struct WS_sockaddr
* name
, int namelen
)
2197 union generic_unix_sockaddr uaddr
;
2198 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
2203 if (name
->sa_family
== WS_AF_INET
)
2205 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
2206 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
2208 /* Trying to connect to magic replace-loopback address,
2209 * assuming we really want to connect to localhost */
2210 TRACE("Trying to connect to magic IP address, using "
2211 "INADDR_LOOPBACK instead.\n");
2212 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_LOOPBACK
);
2216 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
2222 /***********************************************************************
2223 * connect (WS2_32.4)
2225 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
2227 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2229 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
2233 int ret
= do_connect(fd
, name
, namelen
);
2235 goto connect_success
;
2237 if (ret
== WSAEINPROGRESS
)
2239 /* tell wineserver that a connection is in progress */
2240 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2242 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
2243 if (_is_blocking(s
))
2247 do_block(fd
, POLLIN
| POLLOUT
, -1);
2248 _sync_sock_state(s
); /* let wineserver notice connection */
2249 /* retrieve any error codes from it */
2250 result
= _get_sock_error(s
, FD_CONNECT_BIT
);
2252 SetLastError(NtStatusToWSAError(result
));
2255 goto connect_success
;
2260 SetLastError(WSAEWOULDBLOCK
);
2267 release_sock_fd( s
, fd
);
2269 return SOCKET_ERROR
;
2272 release_sock_fd( s
, fd
);
2273 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2274 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
2275 FD_CONNECT
|FD_WINE_LISTENING
);
2279 /***********************************************************************
2280 * WSAConnect (WS2_32.30)
2282 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
2283 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
2284 LPQOS lpSQOS
, LPQOS lpGQOS
)
2286 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
2287 FIXME("unsupported parameters!\n");
2288 return WS_connect( s
, name
, namelen
);
2291 /***********************************************************************
2294 static BOOL WINAPI
WS2_ConnectEx(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
2295 PVOID sendBuf
, DWORD sendBufLen
, LPDWORD sent
, LPOVERLAPPED ov
)
2297 int fd
, ret
, status
;
2301 SetLastError( ERROR_INVALID_PARAMETER
);
2305 fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2308 SetLastError( WSAENOTSOCK
);
2312 TRACE("socket %04lx, ptr %p %s, length %d, sendptr %p, len %d, ov %p\n",
2313 s
, name
, debugstr_sockaddr(name
), namelen
, sendBuf
, sendBufLen
, ov
);
2315 /* FIXME: technically the socket has to be bound */
2316 ret
= do_connect(fd
, name
, namelen
);
2321 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2322 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
2323 FD_CONNECT
|FD_WINE_LISTENING
);
2325 wsabuf
.len
= sendBufLen
;
2326 wsabuf
.buf
= (char*) sendBuf
;
2328 /* WSASend takes care of completion if need be */
2329 if (WSASend(s
, &wsabuf
, sendBuf
? 1 : 0, sent
, 0, ov
, NULL
) != SOCKET_ERROR
)
2330 goto connection_success
;
2332 else if (ret
== WSAEINPROGRESS
)
2334 struct ws2_async
*wsa
;
2335 ULONG_PTR cvalue
= (((ULONG_PTR
)ov
->hEvent
& 1) == 0) ? (ULONG_PTR
)ov
: 0;
2337 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2339 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
2341 /* Indirectly call WSASend */
2342 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) )))
2344 SetLastError(WSAEFAULT
);
2348 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)ov
;
2349 iosb
->u
.Status
= STATUS_PENDING
;
2350 iosb
->Information
= 0;
2352 wsa
->hSocket
= SOCKET2HANDLE(s
);
2354 wsa
->addrlen
.val
= 0;
2356 wsa
->lpFlags
= &wsa
->flags
;
2357 wsa
->control
= NULL
;
2358 wsa
->n_iovecs
= sendBuf
? 1 : 0;
2359 wsa
->first_iovec
= 0;
2360 wsa
->completion_func
= NULL
;
2361 wsa
->iovec
[0].iov_base
= sendBuf
;
2362 wsa
->iovec
[0].iov_len
= sendBufLen
;
2364 SERVER_START_REQ( register_async
)
2366 req
->type
= ASYNC_TYPE_WRITE
;
2367 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
2368 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
2369 req
->async
.iosb
= wine_server_client_ptr( iosb
);
2370 req
->async
.arg
= wine_server_client_ptr( wsa
);
2371 req
->async
.event
= wine_server_obj_handle( ov
->hEvent
);
2372 req
->async
.cvalue
= cvalue
;
2373 status
= wine_server_call( req
);
2377 if (status
!= STATUS_PENDING
) HeapFree(GetProcessHeap(), 0, wsa
);
2379 /* If the connect already failed */
2380 if (status
== STATUS_PIPE_DISCONNECTED
)
2381 status
= _get_sock_error(s
, FD_CONNECT_BIT
);
2382 SetLastError( NtStatusToWSAError(status
) );
2390 release_sock_fd( s
, fd
);
2394 release_sock_fd( s
, fd
);
2399 /***********************************************************************
2400 * getpeername (WS2_32.5)
2402 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
2407 TRACE("socket: %04lx, ptr %p, len %08x\n", s
, name
, namelen
?*namelen
:0);
2409 fd
= get_sock_fd( s
, 0, NULL
);
2414 union generic_unix_sockaddr uaddr
;
2415 unsigned int uaddrlen
= sizeof(uaddr
);
2417 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) == 0)
2419 if (!name
|| !namelen
)
2420 SetLastError(WSAEFAULT
);
2421 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
2422 /* The buffer was too small */
2423 SetLastError(WSAEFAULT
);
2428 SetLastError(wsaErrno());
2429 release_sock_fd( s
, fd
);
2434 /***********************************************************************
2435 * getsockname (WS2_32.6)
2437 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
2442 TRACE("socket: %04lx, ptr %p, len %8x\n", s
, name
, *namelen
);
2444 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
2445 if( (name
== NULL
) || (namelen
== NULL
) )
2447 SetLastError( WSAEFAULT
);
2448 return SOCKET_ERROR
;
2451 fd
= get_sock_fd( s
, 0, NULL
);
2456 union generic_unix_sockaddr uaddr
;
2457 unsigned int uaddrlen
= sizeof(uaddr
);
2459 if (getsockname(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
2461 SetLastError(wsaErrno());
2463 else if (!is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
2465 SetLastError(WSAEINVAL
);
2467 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
2469 /* The buffer was too small */
2470 SetLastError(WSAEFAULT
);
2476 release_sock_fd( s
, fd
);
2481 /***********************************************************************
2482 * getsockopt (WS2_32.7)
2484 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
2485 INT optname
, char *optval
, INT
*optlen
)
2490 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
2491 s
, level
, optname
, optval
, *optlen
);
2499 /* Handle common cases. The special cases are below, sorted
2501 case WS_SO_ACCEPTCONN
:
2502 case WS_SO_BROADCAST
:
2505 case WS_SO_KEEPALIVE
:
2506 case WS_SO_OOBINLINE
:
2508 case WS_SO_REUSEADDR
:
2511 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2512 return SOCKET_ERROR
;
2513 convert_sockopt(&level
, &optname
);
2514 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2516 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2519 release_sock_fd( s
, fd
);
2522 case WS_SO_DONTLINGER
:
2524 struct linger lingval
;
2525 unsigned int len
= sizeof(struct linger
);
2527 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
2529 SetLastError(WSAEFAULT
);
2530 return SOCKET_ERROR
;
2532 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2533 return SOCKET_ERROR
;
2535 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
2537 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2542 *(BOOL
*)optval
= (lingval
.l_onoff
) ? FALSE
: TRUE
;
2543 *optlen
= sizeof(BOOL
);
2546 release_sock_fd( s
, fd
);
2550 case WS_SO_CONNECT_TIME
:
2552 static int pretendtime
= 0;
2554 if (!pretendtime
) FIXME("WS_SO_CONNECT_TIME - faking results\n");
2555 if (!optlen
|| *optlen
< sizeof(DWORD
) || !optval
)
2557 SetLastError(WSAEFAULT
);
2558 return SOCKET_ERROR
;
2560 *(DWORD
*)optval
= pretendtime
++;
2561 *optlen
= sizeof(DWORD
);
2564 /* As mentioned in setsockopt, Windows ignores this, so we
2565 * always return true here */
2566 case WS_SO_DONTROUTE
:
2567 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
2569 SetLastError(WSAEFAULT
);
2570 return SOCKET_ERROR
;
2572 *(BOOL
*)optval
= TRUE
;
2573 *optlen
= sizeof(BOOL
);
2578 struct linger lingval
;
2580 unsigned int len
= sizeof(struct linger
), slen
= sizeof(int);
2582 /* struct linger and LINGER have different sizes */
2583 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
2585 SetLastError(WSAEFAULT
);
2586 return SOCKET_ERROR
;
2588 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2589 return SOCKET_ERROR
;
2591 if ((getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &so_type
, &slen
) == 0 && so_type
== SOCK_DGRAM
))
2593 SetLastError(WSAENOPROTOOPT
);
2596 else if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0)
2598 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2603 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
2604 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
2605 *optlen
= sizeof(struct linger
);
2608 release_sock_fd( s
, fd
);
2612 case WS_SO_MAX_MSG_SIZE
:
2613 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
2615 SetLastError(WSAEFAULT
);
2616 return SOCKET_ERROR
;
2618 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
2619 *(int *)optval
= 65507;
2620 *optlen
= sizeof(int);
2623 /* SO_OPENTYPE does not require a valid socket handle. */
2624 case WS_SO_OPENTYPE
:
2625 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
2627 SetLastError(WSAEFAULT
);
2628 return SOCKET_ERROR
;
2630 *(int *)optval
= get_per_thread_data()->opentype
;
2631 *optlen
= sizeof(int);
2632 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
2636 case WS_SO_RCVTIMEO
:
2639 case WS_SO_SNDTIMEO
:
2641 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
2644 unsigned int len
= sizeof(struct timeval
);
2646 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
2648 SetLastError(WSAEFAULT
);
2649 return SOCKET_ERROR
;
2651 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2652 return SOCKET_ERROR
;
2654 convert_sockopt(&level
, &optname
);
2655 if (getsockopt(fd
, level
, optname
, &tv
, &len
) != 0 )
2657 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2662 *(int *)optval
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
2663 *optlen
= sizeof(int);
2666 release_sock_fd( s
, fd
);
2671 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
2672 SetLastError(WSAENOPROTOOPT
);
2673 return SOCKET_ERROR
;
2674 } /* end switch(optname) */
2675 }/* end case WS_SOL_SOCKET */
2679 struct WS_sockaddr_ipx addr
;
2680 IPX_ADDRESS_DATA
*data
;
2685 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
2687 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (unsigned int*)optlen
) == -1)
2694 socklen_t len
=sizeof(struct ipx
);
2695 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
2698 *optval
= (int)val
.ipx_pt
;
2701 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
2702 release_sock_fd( s
, fd
);
2707 * On a Win2000 system with one network card there are usually
2708 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
2709 * Using this call you can then retrieve info about this all.
2710 * In case of Linux it is a bit different. Usually you have
2711 * only "one" device active and further it is not possible to
2712 * query things like the linkspeed.
2714 FIXME("IPX_ADDRESS\n");
2715 namelen
= sizeof(struct WS_sockaddr_ipx
);
2716 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
2717 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
2719 data
= (IPX_ADDRESS_DATA
*)optval
;
2720 memcpy(data
->nodenum
,addr
.sa_nodenum
,sizeof(data
->nodenum
));
2721 memcpy(data
->netnum
,addr
.sa_netnum
,sizeof(data
->netnum
));
2722 data
->adapternum
= 0;
2723 data
->wan
= FALSE
; /* We are not on a wan for now .. */
2724 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
2725 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
2726 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
2727 * note 1MB = 1000kB in this case */
2730 case IPX_MAX_ADAPTER_NUM
:
2731 FIXME("IPX_MAX_ADAPTER_NUM\n");
2732 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
2736 FIXME("IPX optname:%x\n", optname
);
2737 return SOCKET_ERROR
;
2738 }/* end switch(optname) */
2739 } /* end case NSPROTO_IPX */
2746 case WS_IRLMP_ENUMDEVICES
:
2748 static const int MAX_IRDA_DEVICES
= 10;
2749 char buf
[sizeof(struct irda_device_list
) +
2750 (MAX_IRDA_DEVICES
- 1) * sizeof(struct irda_device_info
)];
2752 socklen_t len
= sizeof(buf
);
2754 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2755 return SOCKET_ERROR
;
2756 res
= getsockopt( fd
, SOL_IRLMP
, IRLMP_ENUMDEVICES
, buf
, &len
);
2759 SetLastError(wsaErrno());
2760 return SOCKET_ERROR
;
2764 struct irda_device_list
*src
= (struct irda_device_list
*)buf
;
2765 DEVICELIST
*dst
= (DEVICELIST
*)optval
;
2766 INT needed
= sizeof(DEVICELIST
), i
;
2769 needed
+= (src
->len
- 1) * sizeof(IRDA_DEVICE_INFO
);
2770 if (*optlen
< needed
)
2772 SetLastError(WSAEFAULT
);
2773 return SOCKET_ERROR
;
2776 TRACE("IRLMP_ENUMDEVICES: %d devices found:\n", src
->len
);
2777 dst
->numDevice
= src
->len
;
2778 for (i
= 0; i
< src
->len
; i
++)
2780 TRACE("saddr = %08x, daddr = %08x, info = %s, hints = %02x%02x\n",
2781 src
->dev
[i
].saddr
, src
->dev
[i
].daddr
,
2782 src
->dev
[i
].info
, src
->dev
[i
].hints
[0],
2783 src
->dev
[i
].hints
[1]);
2784 memcpy( dst
->Device
[i
].irdaDeviceID
,
2786 sizeof(dst
->Device
[i
].irdaDeviceID
) ) ;
2787 memcpy( dst
->Device
[i
].irdaDeviceName
,
2789 sizeof(dst
->Device
[i
].irdaDeviceName
) ) ;
2790 memcpy( &dst
->Device
[i
].irdaDeviceHints1
,
2791 &src
->dev
[i
].hints
[0],
2792 sizeof(dst
->Device
[i
].irdaDeviceHints1
) ) ;
2793 memcpy( &dst
->Device
[i
].irdaDeviceHints2
,
2794 &src
->dev
[i
].hints
[1],
2795 sizeof(dst
->Device
[i
].irdaDeviceHints2
) ) ;
2796 dst
->Device
[i
].irdaCharSet
= src
->dev
[i
].charset
;
2802 FIXME("IrDA optname:0x%x\n", optname
);
2803 return SOCKET_ERROR
;
2805 break; /* case WS_SOL_IRLMP */
2808 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
2809 case WS_IPPROTO_TCP
:
2812 case WS_TCP_NODELAY
:
2813 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2814 return SOCKET_ERROR
;
2815 convert_sockopt(&level
, &optname
);
2816 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2818 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2821 release_sock_fd( s
, fd
);
2824 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
2825 return SOCKET_ERROR
;
2830 case WS_IP_ADD_MEMBERSHIP
:
2831 case WS_IP_DROP_MEMBERSHIP
:
2835 case WS_IP_MULTICAST_IF
:
2836 case WS_IP_MULTICAST_LOOP
:
2837 case WS_IP_MULTICAST_TTL
:
2844 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2845 return SOCKET_ERROR
;
2846 convert_sockopt(&level
, &optname
);
2847 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2849 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2852 release_sock_fd( s
, fd
);
2854 case WS_IP_DONTFRAGMENT
:
2855 FIXME("WS_IP_DONTFRAGMENT is always false!\n");
2856 *(BOOL
*)optval
= FALSE
;
2859 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
2860 return SOCKET_ERROR
;
2862 case WS_IPPROTO_IPV6
:
2865 #ifdef IPV6_ADD_MEMBERSHIP
2866 case WS_IPV6_ADD_MEMBERSHIP
:
2868 #ifdef IPV6_DROP_MEMBERSHIP
2869 case WS_IPV6_DROP_MEMBERSHIP
:
2871 case WS_IPV6_MULTICAST_IF
:
2872 case WS_IPV6_MULTICAST_HOPS
:
2873 case WS_IPV6_MULTICAST_LOOP
:
2874 case WS_IPV6_UNICAST_HOPS
:
2875 case WS_IPV6_V6ONLY
:
2876 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2877 return SOCKET_ERROR
;
2878 convert_sockopt(&level
, &optname
);
2879 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2881 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2884 release_sock_fd( s
, fd
);
2886 case WS_IPV6_DONTFRAG
:
2887 FIXME("WS_IPV6_DONTFRAG is always false!\n");
2888 *(BOOL
*)optval
= FALSE
;
2891 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
2892 return SOCKET_ERROR
;
2895 WARN("Unknown level: 0x%08x\n", level
);
2896 SetLastError(WSAEINVAL
);
2897 return SOCKET_ERROR
;
2898 } /* end switch(level) */
2901 /***********************************************************************
2904 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
2906 return htonl(hostlong
);
2910 /***********************************************************************
2913 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
2915 return htons(hostshort
);
2918 /***********************************************************************
2919 * WSAHtonl (WS2_32.46)
2920 * From MSDN description of error codes, this function should also
2921 * check if WinSock has been initialized and the socket is a valid
2922 * socket. But why? This function only translates a host byte order
2923 * u_long into a network byte order u_long...
2925 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
2929 *lpnetlong
= htonl(hostlong
);
2932 WSASetLastError(WSAEFAULT
);
2933 return SOCKET_ERROR
;
2936 /***********************************************************************
2937 * WSAHtons (WS2_32.47)
2938 * From MSDN description of error codes, this function should also
2939 * check if WinSock has been initialized and the socket is a valid
2940 * socket. But why? This function only translates a host byte order
2941 * u_short into a network byte order u_short...
2943 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
2948 *lpnetshort
= htons(hostshort
);
2951 WSASetLastError(WSAEFAULT
);
2952 return SOCKET_ERROR
;
2956 /***********************************************************************
2957 * inet_addr (WS2_32.11)
2959 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
2961 if (!cp
) return INADDR_NONE
;
2962 return inet_addr(cp
);
2966 /***********************************************************************
2969 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
2971 return ntohl(netlong
);
2975 /***********************************************************************
2978 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
2980 return ntohs(netshort
);
2984 /***********************************************************************
2985 * inet_ntoa (WS2_32.12)
2987 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
2989 /* use "buffer for dummies" here because some applications have a
2990 * propensity to decode addresses in ws_hostent structure without
2991 * saving them first...
2993 static char dbuffer
[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
2995 char* s
= inet_ntoa(*((struct in_addr
*)&in
));
3001 SetLastError(wsaErrno());
3005 static const char *debugstr_wsaioctl(DWORD ioctl
)
3007 const char *buf_type
, *family
;
3009 switch(ioctl
& 0x18000000)
3014 case WS_IOC_PROTOCOL
:
3015 family
= "IOC_PROTOCOL";
3018 family
= "IOC_VENDOR";
3020 default: /* WS_IOC_UNIX */
3022 BYTE size
= (ioctl
>> 16) & WS_IOCPARM_MASK
;
3023 char x
= (ioctl
& 0xff00) >> 8;
3024 BYTE y
= ioctl
& 0xff;
3027 switch (ioctl
& (WS_IOC_VOID
|WS_IOC_INOUT
))
3031 sprintf(args
, "%d, %d", x
, y
);
3035 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3039 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3043 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3046 return wine_dbg_sprintf("%s(%s)", buf_type
, args
);
3050 /* We are different from WS_IOC_UNIX. */
3051 switch (ioctl
& (WS_IOC_VOID
|WS_IOC_INOUT
))
3054 buf_type
= "_WSAIO";
3057 buf_type
= "_WSAIORW";
3060 buf_type
= "_WSAIOW";
3063 buf_type
= "_WSAIOR";
3070 return wine_dbg_sprintf("%s(%s, %d)", buf_type
, family
,
3071 (USHORT
)(ioctl
& 0xffff));
3074 /**********************************************************************
3075 * WSAIoctl (WS2_32.50)
3078 INT WINAPI
WSAIoctl(SOCKET s
, DWORD code
, LPVOID in_buff
, DWORD in_size
, LPVOID out_buff
,
3079 DWORD out_size
, LPDWORD ret_size
, LPWSAOVERLAPPED overlapped
,
3080 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
3083 DWORD status
= 0, total
= 0;
3085 TRACE("%ld, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
3086 s
, code
, in_buff
, in_size
, out_buff
, out_size
, ret_size
, overlapped
, completion
);
3091 if (in_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(in_buff
))
3093 WSASetLastError(WSAEFAULT
);
3094 return SOCKET_ERROR
;
3096 if (_get_sock_mask(s
))
3098 /* AsyncSelect()'ed sockets are always nonblocking */
3099 if (!*(WS_u_long
*)in_buff
) status
= WSAEINVAL
;
3102 if (*(WS_u_long
*)in_buff
)
3103 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
3105 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
3110 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
3112 WSASetLastError(WSAEFAULT
);
3113 return SOCKET_ERROR
;
3115 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
3116 if (ioctl(fd
, FIONREAD
, out_buff
) == -1)
3117 status
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
3118 release_sock_fd( s
, fd
);
3124 unsigned int oob
= 0, oobsize
= sizeof(int), atmark
= 0;
3125 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
3127 WSASetLastError(WSAEFAULT
);
3128 return SOCKET_ERROR
;
3130 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
3131 /* SO_OOBINLINE sockets must always return TRUE to SIOCATMARK */
3132 if ((getsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &oob
, &oobsize
) == -1)
3133 || (!oob
&& ioctl(fd
, SIOCATMARK
, &atmark
) == -1))
3134 status
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
3137 /* The SIOCATMARK value read from ioctl() is reversed
3138 * because BSD returns TRUE if it's in the OOB mark
3139 * while Windows returns TRUE if there are NO OOB bytes.
3141 (*(WS_u_long
*) out_buff
) = oob
| !atmark
;
3144 release_sock_fd( s
, fd
);
3149 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
3150 SetLastError(WSAEINVAL
);
3151 return SOCKET_ERROR
;
3153 case WS_SIO_GET_INTERFACE_LIST
:
3155 INTERFACE_INFO
* intArray
= out_buff
;
3156 DWORD size
, numInt
= 0, apiReturn
;
3158 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
3160 if (!out_buff
|| !ret_size
)
3162 WSASetLastError(WSAEFAULT
);
3163 return SOCKET_ERROR
;
3166 fd
= get_sock_fd( s
, 0, NULL
);
3167 if (fd
== -1) return SOCKET_ERROR
;
3169 apiReturn
= GetAdaptersInfo(NULL
, &size
);
3170 if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
3172 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
3176 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
3178 PIP_ADAPTER_INFO ptr
;
3180 if (size
*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > out_size
)
3182 WARN("Buffer too small = %u, out_size = %u\n", size
, out_size
);
3183 HeapFree(GetProcessHeap(),0,table
);
3184 release_sock_fd( s
, fd
);
3188 for (ptr
= table
, numInt
= 0; ptr
;
3189 ptr
= ptr
->Next
, intArray
++, numInt
++)
3191 unsigned int addr
, mask
, bcast
;
3192 struct ifreq ifInfo
;
3194 /* Socket Status Flags */
3195 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
3196 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
3198 ERR("Error obtaining status flags for socket!\n");
3199 HeapFree(GetProcessHeap(),0,table
);
3200 release_sock_fd( s
, fd
);
3206 /* set flags; the values of IFF_* are not the same
3207 under Linux and Windows, therefore must generate
3209 intArray
->iiFlags
= 0;
3210 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
3211 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
3212 #ifdef IFF_POINTOPOINT
3213 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
3214 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
3216 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
3217 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
3218 if (ifInfo
.ifr_flags
& IFF_UP
)
3219 intArray
->iiFlags
|= WS_IFF_UP
;
3220 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
3221 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
3224 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
3225 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
3226 bcast
= addr
| ~mask
;
3227 intArray
->iiAddress
.AddressIn
.sin_family
= AF_INET
;
3228 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
3229 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
=
3231 intArray
->iiNetmask
.AddressIn
.sin_family
= AF_INET
;
3232 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
3233 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
=
3235 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
=
3237 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
3238 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.
3244 ERR("Unable to get interface table!\n");
3247 HeapFree(GetProcessHeap(),0,table
);
3249 else status
= WSAEINVAL
;
3251 else if (apiReturn
!= ERROR_NO_DATA
)
3253 ERR("Unable to get interface table!\n");
3256 /* Calculate the size of the array being returned */
3257 total
= sizeof(INTERFACE_INFO
) * numInt
;
3258 release_sock_fd( s
, fd
);
3262 case WS_SIO_ADDRESS_LIST_CHANGE
:
3263 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
3264 /* FIXME: error and return code depend on whether socket was created
3265 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
3268 case WS_SIO_ADDRESS_LIST_QUERY
:
3272 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
3276 WSASetLastError(WSAEFAULT
);
3277 return SOCKET_ERROR
;
3280 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
3282 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
3285 if (!table
|| GetAdaptersInfo(table
, &size
))
3287 HeapFree(GetProcessHeap(), 0, table
);
3292 for (p
= table
, num
= 0; p
; p
= p
->Next
)
3293 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
3295 total
= sizeof(SOCKET_ADDRESS_LIST
) + sizeof(SOCKET_ADDRESS
) * (num
- 1);
3296 total
+= sizeof(SOCKADDR
) * num
;
3298 if (total
> out_size
)
3300 HeapFree(GetProcessHeap(), 0, table
);
3309 SOCKET_ADDRESS_LIST
*sa_list
= out_buff
;
3310 SOCKADDR_IN
*sockaddr
;
3312 sa
= sa_list
->Address
;
3313 sockaddr
= (SOCKADDR_IN
*)((char *)sa
+ num
* sizeof(SOCKET_ADDRESS
));
3314 sa_list
->iAddressCount
= num
;
3316 for (p
= table
, i
= 0; p
; p
= p
->Next
)
3318 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
3320 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
3321 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
3323 sockaddr
[i
].sin_family
= AF_INET
;
3324 sockaddr
[i
].sin_port
= 0;
3325 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
3330 HeapFree(GetProcessHeap(), 0, table
);
3334 WARN("unable to get IP address list\n");
3341 FIXME("SIO_FLUSH: stub.\n");
3344 case WS_SIO_GET_EXTENSION_FUNCTION_POINTER
:
3346 static const GUID connectex_guid
= WSAID_CONNECTEX
;
3347 static const GUID disconnectex_guid
= WSAID_DISCONNECTEX
;
3348 static const GUID acceptex_guid
= WSAID_ACCEPTEX
;
3349 static const GUID getaccepexsockaddrs_guid
= WSAID_GETACCEPTEXSOCKADDRS
;
3350 static const GUID transmitfile_guid
= WSAID_TRANSMITFILE
;
3351 static const GUID transmitpackets_guid
= WSAID_TRANSMITPACKETS
;
3352 static const GUID wsarecvmsg_guid
= WSAID_WSARECVMSG
;
3353 static const GUID wsasendmsg_guid
= WSAID_WSASENDMSG
;
3355 if ( IsEqualGUID(&connectex_guid
, in_buff
) )
3357 *(LPFN_CONNECTEX
*)out_buff
= WS2_ConnectEx
;
3360 else if ( IsEqualGUID(&disconnectex_guid
, in_buff
) )
3362 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented DisconnectEx\n");
3364 else if ( IsEqualGUID(&acceptex_guid
, in_buff
) )
3366 *(LPFN_ACCEPTEX
*)out_buff
= WS2_AcceptEx
;
3369 else if ( IsEqualGUID(&getaccepexsockaddrs_guid
, in_buff
) )
3371 *(LPFN_GETACCEPTEXSOCKADDRS
*)out_buff
= WS2_GetAcceptExSockaddrs
;
3374 else if ( IsEqualGUID(&transmitfile_guid
, in_buff
) )
3376 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitFile\n");
3378 else if ( IsEqualGUID(&transmitpackets_guid
, in_buff
) )
3380 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitPackets\n");
3382 else if ( IsEqualGUID(&wsarecvmsg_guid
, in_buff
) )
3384 *(LPFN_WSARECVMSG
*)out_buff
= WS2_WSARecvMsg
;
3387 else if ( IsEqualGUID(&wsasendmsg_guid
, in_buff
) )
3389 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented WSASendMsg\n");
3392 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER %s: stub\n", debugstr_guid(in_buff
));
3394 status
= WSAEOPNOTSUPP
;
3397 case WS_SIO_KEEPALIVE_VALS
:
3399 struct tcp_keepalive
*k
;
3400 int keepalive
, keepidle
, keepintvl
;
3402 if (!in_buff
|| in_size
< sizeof(struct tcp_keepalive
))
3404 WSASetLastError(WSAEFAULT
);
3405 return SOCKET_ERROR
;
3409 keepalive
= k
->onoff
? 1 : 0;
3410 keepidle
= max( 1, (k
->keepalivetime
+ 500) / 1000 );
3411 keepintvl
= max( 1, (k
->keepaliveinterval
+ 500) / 1000 );
3413 TRACE("onoff: %d, keepalivetime: %d, keepaliveinterval: %d\n", keepalive
, keepidle
, keepintvl
);
3415 fd
= get_sock_fd(s
, 0, NULL
);
3416 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&keepalive
, sizeof(int)) == -1)
3418 #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL)
3419 /* these values need to be set only if SO_KEEPALIVE is enabled */
3422 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, (void *)&keepidle
, sizeof(int)) == -1)
3424 else if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, (void *)&keepintvl
, sizeof(int)) == -1)
3429 FIXME("ignoring keepalive interval and timeout\n");
3431 release_sock_fd(s
, fd
);
3434 case WS_SIO_ROUTING_INTERFACE_QUERY
:
3436 struct WS_sockaddr
*daddr
= (struct WS_sockaddr
*)in_buff
;
3437 struct WS_sockaddr_in
*daddr_in
= (struct WS_sockaddr_in
*)daddr
;
3438 struct WS_sockaddr_in
*saddr_in
= out_buff
;
3439 MIB_IPFORWARDROW row
;
3440 PMIB_IPADDRTABLE ipAddrTable
= NULL
;
3441 DWORD size
, i
, found_index
;
3443 TRACE("-> WS_SIO_ROUTING_INTERFACE_QUERY request\n");
3445 if (!in_buff
|| in_size
< sizeof(struct WS_sockaddr
) ||
3446 !out_buff
|| out_size
< sizeof(struct WS_sockaddr_in
) || !ret_size
)
3448 WSASetLastError(WSAEFAULT
);
3449 return SOCKET_ERROR
;
3451 if (daddr
->sa_family
!= AF_INET
)
3453 FIXME("unsupported address family %d\n", daddr
->sa_family
);
3454 status
= WSAEAFNOSUPPORT
;
3457 if (GetBestRoute(daddr_in
->sin_addr
.S_un
.S_addr
, 0, &row
) != NOERROR
||
3458 GetIpAddrTable(NULL
, &size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
3463 ipAddrTable
= HeapAlloc(GetProcessHeap(), 0, size
);
3464 if (GetIpAddrTable(ipAddrTable
, &size
, FALSE
))
3466 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3470 for (i
= 0, found_index
= ipAddrTable
->dwNumEntries
;
3471 i
< ipAddrTable
->dwNumEntries
; i
++)
3473 if (ipAddrTable
->table
[i
].dwIndex
== row
.dwForwardIfIndex
)
3476 if (found_index
== ipAddrTable
->dwNumEntries
)
3478 ERR("no matching IP address for interface %d\n",
3479 row
.dwForwardIfIndex
);
3480 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3484 saddr_in
->sin_family
= AF_INET
;
3485 saddr_in
->sin_addr
.S_un
.S_addr
= ipAddrTable
->table
[found_index
].dwAddr
;
3486 saddr_in
->sin_port
= 0;
3487 total
= sizeof(struct WS_sockaddr_in
);
3488 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3491 case WS_SIO_SET_COMPATIBILITY_MODE
:
3492 TRACE("WS_SIO_SET_COMPATIBILITY_MODE ignored\n");
3493 status
= WSAEOPNOTSUPP
;
3495 case WS_SIO_UDP_CONNRESET
:
3496 FIXME("WS_SIO_UDP_CONNRESET stub\n");
3498 case 0x667e: /* Netscape tries hard to use bogus ioctl 0x667e */
3499 WSASetLastError(WSAEOPNOTSUPP
);
3500 return SOCKET_ERROR
;
3502 FIXME("unsupported WS_IOCTL cmd (%s)\n", debugstr_wsaioctl(code
));
3503 status
= WSAEOPNOTSUPP
;
3509 FIXME( "completion routine %p not supported\n", completion
);
3511 else if (overlapped
)
3513 ULONG_PTR cvalue
= (overlapped
&& ((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)overlapped
: 0;
3514 overlapped
->Internal
= status
;
3515 overlapped
->InternalHigh
= total
;
3516 if (overlapped
->hEvent
) NtSetEvent( overlapped
->hEvent
, NULL
);
3517 if (cvalue
) WS_AddCompletion( HANDLE2SOCKET(s
), cvalue
, status
, total
);
3522 if (ret_size
) *ret_size
= total
;
3525 SetLastError( status
);
3526 return SOCKET_ERROR
;
3530 /***********************************************************************
3531 * ioctlsocket (WS2_32.10)
3533 int WINAPI
WS_ioctlsocket(SOCKET s
, LONG cmd
, WS_u_long
*argp
)
3536 return WSAIoctl( s
, cmd
, argp
, sizeof(WS_u_long
), argp
, sizeof(WS_u_long
), &ret_size
, NULL
, NULL
);
3539 /***********************************************************************
3540 * listen (WS2_32.13)
3542 int WINAPI
WS_listen(SOCKET s
, int backlog
)
3544 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
3546 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
3549 if (listen(fd
, backlog
) == 0)
3551 release_sock_fd( s
, fd
);
3552 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
3554 FD_CONNECT
|FD_WINE_CONNECTED
);
3557 SetLastError(wsaErrno());
3558 release_sock_fd( s
, fd
);
3560 return SOCKET_ERROR
;
3563 /***********************************************************************
3566 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
3568 DWORD n
, dwFlags
= flags
;
3574 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
3575 return SOCKET_ERROR
;
3580 /***********************************************************************
3581 * recvfrom (WS2_32.17)
3583 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
3584 struct WS_sockaddr
*from
, int *fromlen
)
3586 DWORD n
, dwFlags
= flags
;
3592 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
3593 return SOCKET_ERROR
;
3598 /* allocate a poll array for the corresponding fd sets */
3599 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
3600 const WS_fd_set
*exceptfds
, int *count_ptr
)
3602 unsigned int i
, j
= 0, count
= 0;
3605 if (readfds
) count
+= readfds
->fd_count
;
3606 if (writefds
) count
+= writefds
->fd_count
;
3607 if (exceptfds
) count
+= exceptfds
->fd_count
;
3611 SetLastError(WSAEINVAL
);
3614 if (!(fds
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(fds
[0]))))
3616 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
3620 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
3622 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
3623 if (fds
[j
].fd
== -1) goto failed
;
3624 fds
[j
].events
= POLLIN
;
3628 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
3630 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
3631 if (fds
[j
].fd
== -1) goto failed
;
3632 fds
[j
].events
= POLLOUT
;
3636 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3638 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
3639 if (fds
[j
].fd
== -1) goto failed
;
3640 fds
[j
].events
= POLLHUP
;
3649 for (i
= 0; i
< readfds
->fd_count
&& j
< count
; i
++, j
++)
3650 release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
3652 for (i
= 0; i
< writefds
->fd_count
&& j
< count
; i
++, j
++)
3653 release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
3655 for (i
= 0; i
< exceptfds
->fd_count
&& j
< count
; i
++, j
++)
3656 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
3657 HeapFree( GetProcessHeap(), 0, fds
);
3661 /* release the file descriptor obtained in fd_sets_to_poll */
3662 /* must be called with the original fd_set arrays, before calling get_poll_results */
3663 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
3664 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
3666 unsigned int i
, j
= 0;
3670 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
3671 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
3675 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
3676 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
3680 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3681 if (fds
[j
].fd
!= -1)
3683 /* make sure we have a real error before releasing the fd */
3684 if (!sock_error_p( fds
[j
].fd
)) fds
[j
].revents
= 0;
3685 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
3690 /* map the poll results back into the Windows fd sets */
3691 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
3692 const struct pollfd
*fds
)
3694 unsigned int i
, j
= 0, k
, total
= 0;
3698 for (i
= k
= 0; i
< readfds
->fd_count
; i
++, j
++)
3699 if (fds
[j
].revents
) readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
3700 readfds
->fd_count
= k
;
3705 for (i
= k
= 0; i
< writefds
->fd_count
; i
++, j
++)
3706 if ((fds
[j
].revents
& POLLOUT
) && !(fds
[j
].revents
& POLLHUP
))
3707 writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
3708 writefds
->fd_count
= k
;
3713 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3714 if (fds
[j
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
3715 exceptfds
->fd_count
= k
;
3722 /***********************************************************************
3723 * select (WS2_32.18)
3725 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
3726 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
3727 const struct WS_timeval
* ws_timeout
)
3729 struct pollfd
*pollfds
;
3730 struct timeval tv1
, tv2
;
3732 int count
, ret
, timeout
= -1;
3734 TRACE("read %p, write %p, excp %p timeout %p\n",
3735 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
3737 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)))
3738 return SOCKET_ERROR
;
3742 torig
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
3744 gettimeofday( &tv1
, 0 );
3747 while ((ret
= poll( pollfds
, count
, timeout
)) < 0)
3751 if (!ws_timeout
) continue;
3752 gettimeofday( &tv2
, 0 );
3754 tv2
.tv_sec
-= tv1
.tv_sec
;
3755 tv2
.tv_usec
-= tv1
.tv_usec
;
3756 if (tv2
.tv_usec
< 0)
3758 tv2
.tv_usec
+= 1000000;
3762 timeout
= torig
- (tv2
.tv_sec
* 1000) - (tv2
.tv_usec
+ 999) / 1000;
3763 if (timeout
<= 0) break;
3766 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
3768 if (ret
== -1) SetLastError(wsaErrno());
3769 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
3770 HeapFree( GetProcessHeap(), 0, pollfds
);
3774 /* helper to send completion messages for client-only i/o operation case */
3775 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
,
3778 SERVER_START_REQ( add_fd_completion
)
3780 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(sock
) );
3781 req
->cvalue
= CompletionValue
;
3782 req
->status
= CompletionStatus
;
3783 req
->information
= Information
;
3784 wine_server_call( req
);
3790 /***********************************************************************
3793 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
3799 wsabuf
.buf
= (char*) buf
;
3801 if ( WS2_sendto( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
3802 return SOCKET_ERROR
;
3807 /***********************************************************************
3808 * WSASend (WS2_32.72)
3810 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
3811 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
3812 LPWSAOVERLAPPED lpOverlapped
,
3813 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3815 return WS2_sendto( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
3816 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
3819 /***********************************************************************
3820 * WSASendDisconnect (WS2_32.73)
3822 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
3824 return WS_shutdown( s
, SD_SEND
);
3828 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
3829 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
3830 const struct WS_sockaddr
*to
, int tolen
,
3831 LPWSAOVERLAPPED lpOverlapped
,
3832 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3834 unsigned int i
, options
;
3836 struct ws2_async
*wsa
= NULL
;
3837 int totalLength
= 0;
3838 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
3841 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
3842 s
, lpBuffers
, dwBufferCount
, dwFlags
,
3843 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
3845 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
3846 TRACE( "fd=%d, options=%x\n", fd
, options
);
3848 if ( fd
== -1 ) return SOCKET_ERROR
;
3850 if (!lpOverlapped
&& !lpNumberOfBytesSent
)
3855 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
3861 wsa
->hSocket
= SOCKET2HANDLE(s
);
3862 wsa
->addr
= (struct WS_sockaddr
*)to
;
3863 wsa
->addrlen
.val
= tolen
;
3864 wsa
->flags
= dwFlags
;
3865 wsa
->lpFlags
= &wsa
->flags
;
3866 wsa
->control
= NULL
;
3867 wsa
->n_iovecs
= dwBufferCount
;
3868 wsa
->first_iovec
= 0;
3869 for ( i
= 0; i
< dwBufferCount
; i
++ )
3871 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
3872 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
3873 totalLength
+= lpBuffers
[i
].len
;
3878 n
= WS2_send( fd
, wsa
);
3879 if (n
!= -1 || errno
!= EINTR
) break;
3881 if (n
== -1 && errno
!= EAGAIN
)
3883 int loc_errno
= errno
;
3885 if (cvalue
) WS_AddCompletion( s
, cvalue
, sock_get_ntstatus(loc_errno
), 0 );
3889 if ((lpOverlapped
|| lpCompletionRoutine
) &&
3890 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
3892 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
3894 wsa
->user_overlapped
= lpOverlapped
;
3895 wsa
->completion_func
= lpCompletionRoutine
;
3896 release_sock_fd( s
, fd
);
3898 if (n
== -1 || n
< totalLength
)
3900 iosb
->u
.Status
= STATUS_PENDING
;
3901 iosb
->Information
= n
== -1 ? 0 : n
;
3903 SERVER_START_REQ( register_async
)
3905 req
->type
= ASYNC_TYPE_WRITE
;
3906 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
3907 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
3908 req
->async
.iosb
= wine_server_client_ptr( iosb
);
3909 req
->async
.arg
= wine_server_client_ptr( wsa
);
3910 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
3911 req
->async
.cvalue
= cvalue
;
3912 err
= wine_server_call( req
);
3916 /* Enable the event only after starting the async. The server will deliver it as soon as
3917 the async is done. */
3918 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
3920 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
3921 WSASetLastError( NtStatusToWSAError( err
));
3922 return SOCKET_ERROR
;
3925 iosb
->u
.Status
= STATUS_SUCCESS
;
3926 iosb
->Information
= n
;
3927 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= n
;
3928 if (!wsa
->completion_func
)
3930 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
3931 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
3932 HeapFree( GetProcessHeap(), 0, wsa
);
3934 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
3935 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
3940 if ( _is_blocking(s
) )
3942 /* On a blocking non-overlapped stream socket,
3943 * sending blocks until the entire buffer is sent. */
3944 DWORD timeout_start
= GetTickCount();
3946 bytes_sent
= n
== -1 ? 0 : n
;
3948 while (wsa
->first_iovec
< wsa
->n_iovecs
)
3951 int timeout
= GET_SNDTIMEO(fd
);
3955 timeout
-= GetTickCount() - timeout_start
;
3956 if (timeout
< 0) timeout
= 0;
3960 pfd
.events
= POLLOUT
;
3962 if (!timeout
|| !poll( &pfd
, 1, timeout
))
3965 goto error
; /* msdn says a timeout in send is fatal */
3968 n
= WS2_send( fd
, wsa
);
3969 if (n
== -1 && errno
!= EAGAIN
&& errno
!= EINTR
)
3979 else /* non-blocking */
3981 if (n
< totalLength
)
3982 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
3985 err
= WSAEWOULDBLOCK
;
3991 TRACE(" -> %i bytes\n", bytes_sent
);
3993 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= bytes_sent
;
3994 HeapFree( GetProcessHeap(), 0, wsa
);
3995 release_sock_fd( s
, fd
);
4000 HeapFree( GetProcessHeap(), 0, wsa
);
4001 release_sock_fd( s
, fd
);
4002 WARN(" -> ERROR %d\n", err
);
4003 WSASetLastError(err
);
4004 return SOCKET_ERROR
;
4007 /***********************************************************************
4008 * WSASendTo (WS2_32.74)
4010 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4011 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
4012 const struct WS_sockaddr
*to
, int tolen
,
4013 LPWSAOVERLAPPED lpOverlapped
,
4014 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4016 return WS2_sendto( s
, lpBuffers
, dwBufferCount
,
4017 lpNumberOfBytesSent
, dwFlags
,
4019 lpOverlapped
, lpCompletionRoutine
);
4022 /***********************************************************************
4023 * sendto (WS2_32.20)
4025 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
4026 const struct WS_sockaddr
*to
, int tolen
)
4032 wsabuf
.buf
= (char*) buf
;
4034 if ( WS2_sendto(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
4035 return SOCKET_ERROR
;
4040 /***********************************************************************
4041 * setsockopt (WS2_32.21)
4043 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
4044 const char *optval
, int optlen
)
4048 struct linger linger
;
4049 struct timeval tval
;
4051 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
4052 s
, level
, optname
, optval
, optlen
);
4054 /* some broken apps pass the value directly instead of a pointer to it */
4055 if(optlen
&& IS_INTRESOURCE(optval
))
4057 SetLastError(WSAEFAULT
);
4058 return SOCKET_ERROR
;
4066 /* Some options need some conversion before they can be sent to
4067 * setsockopt. The conversions are done here, then they will fall though
4068 * to the general case. Special options that are not passed to
4069 * setsockopt follow below that.*/
4071 case WS_SO_DONTLINGER
:
4074 SetLastError(WSAEFAULT
);
4075 return SOCKET_ERROR
;
4077 linger
.l_onoff
= *((const int*)optval
) ? 0: 1;
4078 linger
.l_linger
= 0;
4080 optname
= SO_LINGER
;
4081 optval
= (char*)&linger
;
4082 optlen
= sizeof(struct linger
);
4088 SetLastError(WSAEFAULT
);
4089 return SOCKET_ERROR
;
4091 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
4092 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
4094 optname
= SO_LINGER
;
4095 optval
= (char*)&linger
;
4096 optlen
= sizeof(struct linger
);
4100 if (*(const int*)optval
< 2048)
4102 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
4107 /* The options listed here don't need any special handling. Thanks to
4108 * the conversion happening above, options from there will fall through
4110 case WS_SO_ACCEPTCONN
:
4111 case WS_SO_BROADCAST
:
4113 case WS_SO_KEEPALIVE
:
4114 case WS_SO_OOBINLINE
:
4115 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
4116 * however, using it the BSD way fixes bug 8513 and seems to be what
4117 * most programmers assume, anyway */
4118 case WS_SO_REUSEADDR
:
4121 convert_sockopt(&level
, &optname
);
4124 /* SO_DEBUG is a privileged operation, ignore it. */
4126 TRACE("Ignoring SO_DEBUG\n");
4129 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
4130 * socket. According to MSDN, this option is silently ignored.*/
4131 case WS_SO_DONTROUTE
:
4132 TRACE("Ignoring SO_DONTROUTE\n");
4135 /* Stops two sockets from being bound to the same port. Always happens
4136 * on unix systems, so just drop it. */
4137 case WS_SO_EXCLUSIVEADDRUSE
:
4138 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
4141 /* After a ConnectEx call succeeds, the socket can't be used with half of the
4142 * normal winsock functions on windows. We don't have that problem. */
4143 case WS_SO_UPDATE_CONNECT_CONTEXT
:
4144 TRACE("Ignoring SO_UPDATE_CONNECT_CONTEXT, since our sockets are normal\n");
4147 /* After a AcceptEx call succeeds, the socket can't be used with half of the
4148 * normal winsock functions on windows. We don't have that problem. */
4149 case WS_SO_UPDATE_ACCEPT_CONTEXT
:
4150 TRACE("Ignoring SO_UPDATE_ACCEPT_CONTEXT, since our sockets are normal\n");
4153 /* SO_OPENTYPE does not require a valid socket handle. */
4154 case WS_SO_OPENTYPE
:
4155 if (!optlen
|| optlen
< sizeof(int) || !optval
)
4157 SetLastError(WSAEFAULT
);
4158 return SOCKET_ERROR
;
4160 get_per_thread_data()->opentype
= *(const int *)optval
;
4161 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((const int*)optval
) );
4165 case WS_SO_RCVTIMEO
:
4168 case WS_SO_SNDTIMEO
:
4170 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
4171 if (optval
&& optlen
== sizeof(UINT32
)) {
4172 /* WinSock passes milliseconds instead of struct timeval */
4173 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
4174 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
4175 /* min of 500 milliseconds */
4176 if (tval
.tv_sec
== 0 && tval
.tv_usec
&& tval
.tv_usec
< 500000)
4177 tval
.tv_usec
= 500000;
4178 optlen
= sizeof(struct timeval
);
4179 optval
= (char*)&tval
;
4180 } else if (optlen
== sizeof(struct timeval
)) {
4181 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
4183 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
4186 convert_sockopt(&level
, &optname
);
4191 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
4192 SetLastError(WSAENOPROTOOPT
);
4193 return SOCKET_ERROR
;
4195 break; /* case WS_SOL_SOCKET */
4202 fd
= get_sock_fd( s
, 0, NULL
);
4203 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval
, fd
);
4205 /* We try to set the ipx type on ipx socket level. */
4207 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, optlen
) == -1)
4209 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
4210 release_sock_fd( s
, fd
);
4211 return SOCKET_ERROR
;
4216 /* Should we retrieve val using a getsockopt call and then
4217 * set the modified one? */
4218 val
.ipx_pt
= *optval
;
4219 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
4222 release_sock_fd( s
, fd
);
4225 case IPX_FILTERPTYPE
:
4226 /* Sets the receive filter packet type, at the moment we don't support it */
4227 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
4228 /* Returning 0 is better for now than returning a SOCKET_ERROR */
4232 FIXME("opt_name:%x\n", optname
);
4233 return SOCKET_ERROR
;
4235 break; /* case NSPROTO_IPX */
4238 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
4239 case WS_IPPROTO_TCP
:
4242 case WS_TCP_NODELAY
:
4243 convert_sockopt(&level
, &optname
);
4246 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
4247 return SOCKET_ERROR
;
4254 case WS_IP_ADD_MEMBERSHIP
:
4255 case WS_IP_DROP_MEMBERSHIP
:
4259 case WS_IP_MULTICAST_IF
:
4260 case WS_IP_MULTICAST_LOOP
:
4261 case WS_IP_MULTICAST_TTL
:
4268 convert_sockopt(&level
, &optname
);
4270 case WS_IP_DONTFRAGMENT
:
4271 FIXME("IP_DONTFRAGMENT is silently ignored!\n");
4274 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
4275 return SOCKET_ERROR
;
4279 case WS_IPPROTO_IPV6
:
4282 #ifdef IPV6_ADD_MEMBERSHIP
4283 case WS_IPV6_ADD_MEMBERSHIP
:
4285 #ifdef IPV6_DROP_MEMBERSHIP
4286 case WS_IPV6_DROP_MEMBERSHIP
:
4288 case WS_IPV6_MULTICAST_IF
:
4289 case WS_IPV6_MULTICAST_HOPS
:
4290 case WS_IPV6_MULTICAST_LOOP
:
4291 case WS_IPV6_UNICAST_HOPS
:
4292 case WS_IPV6_V6ONLY
:
4293 convert_sockopt(&level
, &optname
);
4295 case WS_IPV6_DONTFRAG
:
4296 FIXME("IPV6_DONTFRAG is silently ignored!\n");
4299 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
4300 return SOCKET_ERROR
;
4305 WARN("Unknown level: 0x%08x\n", level
);
4306 SetLastError(WSAEINVAL
);
4307 return SOCKET_ERROR
;
4308 } /* end switch(level) */
4310 /* avoid endianness issues if argument is a 16-bit int */
4311 if (optval
&& optlen
< sizeof(int))
4313 woptval
= *((const INT16
*) optval
);
4314 optval
= (char*) &woptval
;
4317 fd
= get_sock_fd( s
, 0, NULL
);
4318 if (fd
== -1) return SOCKET_ERROR
;
4320 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
4322 release_sock_fd( s
, fd
);
4325 TRACE("Setting socket error, %d\n", wsaErrno());
4326 SetLastError(wsaErrno());
4327 release_sock_fd( s
, fd
);
4329 return SOCKET_ERROR
;
4332 /***********************************************************************
4333 * shutdown (WS2_32.22)
4335 int WINAPI
WS_shutdown(SOCKET s
, int how
)
4337 int fd
, err
= WSAENOTSOCK
;
4338 unsigned int options
, clear_flags
= 0;
4340 fd
= get_sock_fd( s
, 0, &options
);
4341 TRACE("socket %04lx, how %i %x\n", s
, how
, options
);
4344 return SOCKET_ERROR
;
4348 case 0: /* drop receives */
4349 clear_flags
|= FD_READ
;
4351 case 1: /* drop sends */
4352 clear_flags
|= FD_WRITE
;
4354 case 2: /* drop all */
4355 clear_flags
|= FD_READ
|FD_WRITE
;
4358 clear_flags
|= FD_WINE_LISTENING
;
4361 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
4366 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
4369 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
4373 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
4374 if (!err
) err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
4377 if (err
) goto error
;
4379 else /* non-overlapped mode */
4381 if ( shutdown( fd
, how
) )
4388 release_sock_fd( s
, fd
);
4389 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
4390 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
4394 release_sock_fd( s
, fd
);
4395 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
4396 WSASetLastError( err
);
4397 return SOCKET_ERROR
;
4400 /***********************************************************************
4401 * socket (WS2_32.23)
4403 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
4405 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
4407 return WSASocketA( af
, type
, protocol
, NULL
, 0,
4408 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
4412 /***********************************************************************
4413 * gethostbyaddr (WS2_32.51)
4415 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
4417 struct WS_hostent
*retval
= NULL
;
4418 struct hostent
* host
;
4420 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4423 struct hostent hostentry
;
4426 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
4428 int res
= gethostbyaddr_r(addr
, len
, type
,
4429 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
4430 if( res
!= ERANGE
) break;
4432 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
4434 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
4436 EnterCriticalSection( &csWSgetXXXbyYYY
);
4437 host
= gethostbyaddr(addr
, len
, type
);
4438 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
4440 if( host
!= NULL
) retval
= WS_dup_he(host
);
4441 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4442 HeapFree(GetProcessHeap(),0,extrabuf
);
4444 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4446 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
4450 /***********************************************************************
4451 * WS_get_local_ips (INTERNAL)
4453 * Returns the list of local IP addresses by going through the network
4454 * adapters and using the local routing table to sort the addresses
4455 * from highest routing priority to lowest routing priority. This
4456 * functionality is inferred from the description for obtaining local
4457 * IP addresses given in the Knowledge Base Article Q160215.
4459 * Please note that the returned hostent is only freed when the thread
4460 * closes and is replaced if another hostent is requested.
4462 static struct WS_hostent
* WS_get_local_ips( char *hostname
)
4464 int last_metric
, numroutes
= 0, i
, j
;
4465 PIP_ADAPTER_INFO adapters
= NULL
, k
;
4466 struct WS_hostent
*hostlist
= NULL
;
4467 PMIB_IPFORWARDTABLE routes
= NULL
;
4468 struct route
*route_addrs
= NULL
;
4469 DWORD adap_size
, route_size
;
4471 /* Obtain the size of the adapter list and routing table, also allocate memory */
4472 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
4474 if (GetIpForwardTable(NULL
, &route_size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
4476 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
4477 routes
= HeapAlloc(GetProcessHeap(), 0, route_size
);
4478 route_addrs
= HeapAlloc(GetProcessHeap(), 0, 0); /* HeapReAlloc doesn't work on NULL */
4479 if (adapters
== NULL
|| routes
== NULL
|| route_addrs
== NULL
)
4481 /* Obtain the adapter list and the full routing table */
4482 if (GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
4484 if (GetIpForwardTable(routes
, &route_size
, FALSE
) != NO_ERROR
)
4486 /* Store the interface associated with each route */
4487 for (i
= 0; i
< routes
->dwNumEntries
; i
++)
4489 DWORD ifindex
, ifmetric
, exists
= FALSE
;
4491 if (routes
->table
[i
].dwForwardType
!= MIB_IPROUTE_TYPE_DIRECT
)
4493 ifindex
= routes
->table
[i
].dwForwardIfIndex
;
4494 ifmetric
= routes
->table
[i
].dwForwardMetric1
;
4495 /* Only store the lowest valued metric for an interface */
4496 for (j
= 0; j
< numroutes
; j
++)
4498 if (route_addrs
[j
].interface
== ifindex
)
4500 if (route_addrs
[j
].metric
> ifmetric
)
4501 route_addrs
[j
].metric
= ifmetric
;
4507 route_addrs
= HeapReAlloc(GetProcessHeap(), 0, route_addrs
, (numroutes
+1)*sizeof(struct route
));
4508 if (route_addrs
== NULL
)
4509 goto cleanup
; /* Memory allocation error, fail gracefully */
4510 route_addrs
[numroutes
].interface
= ifindex
;
4511 route_addrs
[numroutes
].metric
= ifmetric
;
4512 /* If no IP is found in the next step (for whatever reason)
4513 * then fall back to the magic loopback address.
4515 memcpy(&(route_addrs
[numroutes
].addr
.s_addr
), magic_loopback_addr
, 4);
4519 goto cleanup
; /* No routes, fall back to the Magic IP */
4520 /* Find the IP address associated with each found interface */
4521 for (i
= 0; i
< numroutes
; i
++)
4523 for (k
= adapters
; k
!= NULL
; k
= k
->Next
)
4525 char *ip
= k
->IpAddressList
.IpAddress
.String
;
4527 if (route_addrs
[i
].interface
== k
->Index
)
4528 route_addrs
[i
].addr
.s_addr
= (in_addr_t
) inet_addr(ip
);
4531 /* Allocate a hostent and enough memory for all the IPs,
4532 * including the NULL at the end of the list.
4534 hostlist
= WS_create_he(hostname
, 1, numroutes
+1, TRUE
);
4535 if (hostlist
== NULL
)
4536 goto cleanup
; /* Failed to allocate a hostent for the list of IPs */
4537 hostlist
->h_addr_list
[numroutes
] = NULL
; /* NULL-terminate the address list */
4538 hostlist
->h_aliases
[0] = NULL
; /* NULL-terminate the alias list */
4539 hostlist
->h_addrtype
= AF_INET
;
4540 hostlist
->h_length
= sizeof(struct in_addr
); /* = 4 */
4541 /* Reorder the entries when placing them in the host list, Windows expects
4542 * the IP list in order from highest priority to lowest (the critical thing
4543 * is that most applications expect the first IP to be the default route).
4546 for (i
= 0; i
< numroutes
; i
++)
4548 struct in_addr addr
;
4549 int metric
= 0xFFFF;
4551 memcpy(&addr
, magic_loopback_addr
, 4);
4552 for (j
= 0; j
< numroutes
; j
++)
4554 int this_metric
= route_addrs
[j
].metric
;
4556 if (this_metric
> last_metric
&& this_metric
< metric
)
4558 addr
= route_addrs
[j
].addr
;
4559 metric
= this_metric
;
4562 last_metric
= metric
;
4563 (*(struct in_addr
*) hostlist
->h_addr_list
[i
]) = addr
;
4566 /* Cleanup all allocated memory except the address list,
4567 * the address list is used by the calling app.
4570 HeapFree(GetProcessHeap(), 0, route_addrs
);
4571 HeapFree(GetProcessHeap(), 0, adapters
);
4572 HeapFree(GetProcessHeap(), 0, routes
);
4576 /***********************************************************************
4577 * gethostbyname (WS2_32.52)
4579 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
4581 struct WS_hostent
*retval
= NULL
;
4582 struct hostent
* host
;
4583 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4586 struct hostent hostentry
;
4587 int locerr
= ENOBUFS
;
4591 SetLastError(WSANOTINITIALISED
);
4594 if( gethostname( hostname
, 100) == -1) {
4595 SetLastError( WSAENOBUFS
); /* appropriate ? */
4598 if( !name
|| !name
[0]) {
4601 /* If the hostname of the local machine is requested then return the
4602 * complete list of local IP addresses */
4603 if(strcmp(name
, hostname
) == 0)
4604 retval
= WS_get_local_ips(hostname
);
4605 /* If any other hostname was requested (or the routing table lookup failed)
4606 * then return the IP found by the host OS */
4609 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4611 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
4613 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
4614 if( res
!= ERANGE
) break;
4616 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
4618 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
4620 EnterCriticalSection( &csWSgetXXXbyYYY
);
4621 host
= gethostbyname(name
);
4622 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
4624 if (host
) retval
= WS_dup_he(host
);
4625 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4626 HeapFree(GetProcessHeap(),0,extrabuf
);
4628 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4631 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
4632 strcmp(name
, "localhost") != 0)
4634 /* hostname != "localhost" but has loopback address. replace by our
4635 * special address.*/
4636 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
4638 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
4643 /***********************************************************************
4644 * getprotobyname (WS2_32.53)
4646 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
4648 struct WS_protoent
* retval
= NULL
;
4649 #ifdef HAVE_GETPROTOBYNAME
4650 struct protoent
* proto
;
4651 EnterCriticalSection( &csWSgetXXXbyYYY
);
4652 if( (proto
= getprotobyname(name
)) != NULL
)
4654 retval
= WS_dup_pe(proto
);
4657 MESSAGE("protocol %s not found; You might want to add "
4658 "this to /etc/protocols\n", debugstr_a(name
) );
4659 SetLastError(WSANO_DATA
);
4661 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4663 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
4668 /***********************************************************************
4669 * getprotobynumber (WS2_32.54)
4671 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
4673 struct WS_protoent
* retval
= NULL
;
4674 #ifdef HAVE_GETPROTOBYNUMBER
4675 struct protoent
* proto
;
4676 EnterCriticalSection( &csWSgetXXXbyYYY
);
4677 if( (proto
= getprotobynumber(number
)) != NULL
)
4679 retval
= WS_dup_pe(proto
);
4682 MESSAGE("protocol number %d not found; You might want to add "
4683 "this to /etc/protocols\n", number
);
4684 SetLastError(WSANO_DATA
);
4686 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4688 TRACE("%i ret %p\n", number
, retval
);
4693 /***********************************************************************
4694 * getservbyname (WS2_32.55)
4696 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
4698 struct WS_servent
* retval
= NULL
;
4699 struct servent
* serv
;
4701 char *proto_str
= NULL
;
4703 if (!(name_str
= strdup_lower(name
))) return NULL
;
4705 if (proto
&& *proto
)
4707 if (!(proto_str
= strdup_lower(proto
)))
4709 HeapFree( GetProcessHeap(), 0, name_str
);
4714 EnterCriticalSection( &csWSgetXXXbyYYY
);
4715 serv
= getservbyname(name_str
, proto_str
);
4718 retval
= WS_dup_se(serv
);
4720 else SetLastError(WSANO_DATA
);
4721 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4722 HeapFree( GetProcessHeap(), 0, proto_str
);
4723 HeapFree( GetProcessHeap(), 0, name_str
);
4724 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
4728 /***********************************************************************
4729 * freeaddrinfo (WS2_32.@)
4731 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
4734 struct WS_addrinfo
*next
;
4736 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
4737 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
4738 next
= res
->ai_next
;
4739 HeapFree(GetProcessHeap(),0,res
);
4744 /* helper functions for getaddrinfo()/getnameinfo() */
4745 static int convert_aiflag_w2u(int winflags
) {
4749 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
4750 if (ws_aiflag_map
[i
][0] & winflags
) {
4751 unixflags
|= ws_aiflag_map
[i
][1];
4752 winflags
&= ~ws_aiflag_map
[i
][0];
4755 FIXME("Unhandled windows AI_xxx flags %x\n", winflags
);
4759 static int convert_niflag_w2u(int winflags
) {
4763 for (i
=0;i
<sizeof(ws_niflag_map
)/sizeof(ws_niflag_map
[0]);i
++)
4764 if (ws_niflag_map
[i
][0] & winflags
) {
4765 unixflags
|= ws_niflag_map
[i
][1];
4766 winflags
&= ~ws_niflag_map
[i
][0];
4769 FIXME("Unhandled windows NI_xxx flags %x\n", winflags
);
4773 static int convert_aiflag_u2w(int unixflags
) {
4777 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
4778 if (ws_aiflag_map
[i
][1] & unixflags
) {
4779 winflags
|= ws_aiflag_map
[i
][0];
4780 unixflags
&= ~ws_aiflag_map
[i
][1];
4782 if (unixflags
) /* will warn usually */
4783 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags
);
4787 static int convert_eai_u2w(int unixret
) {
4790 for (i
=0;ws_eai_map
[i
][0];i
++)
4791 if (ws_eai_map
[i
][1] == unixret
)
4792 return ws_eai_map
[i
][0];
4796 static char *get_hostname(void)
4801 GetComputerNameExA( ComputerNamePhysicalDnsHostname
, NULL
, &size
);
4802 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
4803 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, size
))) return NULL
;
4804 if (!GetComputerNameExA( ComputerNamePhysicalDnsHostname
, ret
, &size
))
4806 HeapFree( GetProcessHeap(), 0, ret
);
4812 /***********************************************************************
4813 * getaddrinfo (WS2_32.@)
4815 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
4817 #ifdef HAVE_GETADDRINFO
4818 struct addrinfo
*unixaires
= NULL
;
4820 struct addrinfo unixhints
, *punixhints
= NULL
;
4821 char *hostname
= NULL
;
4824 if (!nodename
&& !servname
) return WSAHOST_NOT_FOUND
;
4828 else if (!nodename
[0])
4830 node
= hostname
= get_hostname();
4831 if (!node
) return WSA_NOT_ENOUGH_MEMORY
;
4837 punixhints
= &unixhints
;
4839 memset(&unixhints
, 0, sizeof(unixhints
));
4840 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
4841 if (hints
->ai_family
== 0) /* wildcard, specific to getaddrinfo() */
4842 punixhints
->ai_family
= 0;
4844 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
4845 if (hints
->ai_socktype
== 0) /* wildcard, specific to getaddrinfo() */
4846 punixhints
->ai_socktype
= 0;
4848 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
4849 if (hints
->ai_protocol
== 0) /* wildcard, specific to getaddrinfo() */
4850 punixhints
->ai_protocol
= 0;
4852 punixhints
->ai_protocol
= convert_proto_w2u(hints
->ai_protocol
);
4855 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
4856 result
= getaddrinfo(node
, servname
, punixhints
, &unixaires
);
4858 TRACE("%s, %s %p -> %p %d\n", debugstr_a(nodename
), debugstr_a(servname
), hints
, res
, result
);
4859 HeapFree(GetProcessHeap(), 0, hostname
);
4862 struct addrinfo
*xuai
= unixaires
;
4863 struct WS_addrinfo
**xai
= res
;
4867 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
4873 *xai
= ai
;xai
= &ai
->ai_next
;
4874 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
4875 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
4876 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
4877 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
4878 if (xuai
->ai_canonname
) {
4879 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
4880 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
4881 if (!ai
->ai_canonname
)
4883 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
4885 len
= xuai
->ai_addrlen
;
4886 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
4889 ai
->ai_addrlen
= len
;
4891 int winlen
= ai
->ai_addrlen
;
4893 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
4894 ai
->ai_addrlen
= winlen
;
4898 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
4901 ai
->ai_addrlen
= len
;
4903 xuai
= xuai
->ai_next
;
4905 freeaddrinfo(unixaires
);
4907 result
= convert_eai_u2w(result
);
4913 if (*res
) WS_freeaddrinfo(*res
);
4914 if (unixaires
) freeaddrinfo(unixaires
);
4916 return WSA_NOT_ENOUGH_MEMORY
;
4918 FIXME("getaddrinfo() failed, not found during buildtime.\n");
4923 static struct WS_addrinfoW
*addrinfo_AtoW(const struct WS_addrinfo
*ai
)
4925 struct WS_addrinfoW
*ret
;
4927 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfoW
)))) return NULL
;
4928 ret
->ai_flags
= ai
->ai_flags
;
4929 ret
->ai_family
= ai
->ai_family
;
4930 ret
->ai_socktype
= ai
->ai_socktype
;
4931 ret
->ai_protocol
= ai
->ai_protocol
;
4932 ret
->ai_addrlen
= ai
->ai_addrlen
;
4933 ret
->ai_canonname
= NULL
;
4934 ret
->ai_addr
= NULL
;
4935 ret
->ai_next
= NULL
;
4936 if (ai
->ai_canonname
)
4938 int len
= MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0);
4939 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
4941 HeapFree(GetProcessHeap(), 0, ret
);
4944 MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
);
4948 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
4950 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
4951 HeapFree(GetProcessHeap(), 0, ret
);
4954 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
4959 static struct WS_addrinfoW
*addrinfo_list_AtoW(const struct WS_addrinfo
*info
)
4961 struct WS_addrinfoW
*ret
, *infoW
;
4963 if (!(ret
= infoW
= addrinfo_AtoW(info
))) return NULL
;
4964 while (info
->ai_next
)
4966 if (!(infoW
->ai_next
= addrinfo_AtoW(info
->ai_next
)))
4971 infoW
= infoW
->ai_next
;
4972 info
= info
->ai_next
;
4977 static struct WS_addrinfo
*addrinfo_WtoA(const struct WS_addrinfoW
*ai
)
4979 struct WS_addrinfo
*ret
;
4981 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfo
)))) return NULL
;
4982 ret
->ai_flags
= ai
->ai_flags
;
4983 ret
->ai_family
= ai
->ai_family
;
4984 ret
->ai_socktype
= ai
->ai_socktype
;
4985 ret
->ai_protocol
= ai
->ai_protocol
;
4986 ret
->ai_addrlen
= ai
->ai_addrlen
;
4987 ret
->ai_canonname
= NULL
;
4988 ret
->ai_addr
= NULL
;
4989 ret
->ai_next
= NULL
;
4990 if (ai
->ai_canonname
)
4992 int len
= WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0, NULL
, NULL
);
4993 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
4995 HeapFree(GetProcessHeap(), 0, ret
);
4998 WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
, NULL
, NULL
);
5002 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
5004 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
5005 HeapFree(GetProcessHeap(), 0, ret
);
5008 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
5013 /***********************************************************************
5014 * GetAddrInfoW (WS2_32.@)
5016 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
5019 char *nodenameA
= NULL
, *servnameA
= NULL
;
5020 struct WS_addrinfo
*resA
, *hintsA
= NULL
;
5024 len
= WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, NULL
, 0, NULL
, NULL
);
5025 if (!(nodenameA
= HeapAlloc(GetProcessHeap(), 0, len
))) return EAI_MEMORY
;
5026 WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, nodenameA
, len
, NULL
, NULL
);
5030 len
= WideCharToMultiByte(CP_ACP
, 0, servname
, -1, NULL
, 0, NULL
, NULL
);
5031 if (!(servnameA
= HeapAlloc(GetProcessHeap(), 0, len
)))
5033 HeapFree(GetProcessHeap(), 0, nodenameA
);
5036 WideCharToMultiByte(CP_ACP
, 0, servname
, -1, servnameA
, len
, NULL
, NULL
);
5039 if (hints
) hintsA
= addrinfo_WtoA(hints
);
5040 ret
= WS_getaddrinfo(nodenameA
, servnameA
, hintsA
, &resA
);
5041 WS_freeaddrinfo(hintsA
);
5045 *res
= addrinfo_list_AtoW(resA
);
5046 WS_freeaddrinfo(resA
);
5049 HeapFree(GetProcessHeap(), 0, nodenameA
);
5050 HeapFree(GetProcessHeap(), 0, servnameA
);
5054 /***********************************************************************
5055 * FreeAddrInfoW (WS2_32.@)
5057 void WINAPI
FreeAddrInfoW(PADDRINFOW ai
)
5062 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
5063 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
5065 HeapFree(GetProcessHeap(), 0, ai
);
5070 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
5071 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
5073 #ifdef HAVE_GETNAMEINFO
5075 union generic_unix_sockaddr sa_u
;
5078 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
5079 serv
, servlen
, flags
);
5081 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
5084 WSASetLastError(WSAEFAULT
);
5085 return WSA_NOT_ENOUGH_MEMORY
;
5087 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
5088 return convert_eai_u2w(ret
);
5090 FIXME("getnameinfo() failed, not found during buildtime.\n");
5095 int WINAPI
GetNameInfoW(const SOCKADDR
*sa
, WS_socklen_t salen
, PWCHAR host
,
5096 DWORD hostlen
, PWCHAR serv
, DWORD servlen
, INT flags
)
5099 char *hostA
= NULL
, *servA
= NULL
;
5101 if (host
&& (!(hostA
= HeapAlloc(GetProcessHeap(), 0, hostlen
)))) return EAI_MEMORY
;
5102 if (serv
&& (!(servA
= HeapAlloc(GetProcessHeap(), 0, servlen
))))
5104 HeapFree(GetProcessHeap(), 0, hostA
);
5108 ret
= WS_getnameinfo(sa
, salen
, hostA
, hostlen
, servA
, servlen
, flags
);
5111 if (host
) MultiByteToWideChar(CP_ACP
, 0, hostA
, -1, host
, hostlen
);
5112 if (serv
) MultiByteToWideChar(CP_ACP
, 0, servA
, -1, serv
, servlen
);
5115 HeapFree(GetProcessHeap(), 0, hostA
);
5116 HeapFree(GetProcessHeap(), 0, servA
);
5120 /***********************************************************************
5121 * getservbyport (WS2_32.56)
5123 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
5125 struct WS_servent
* retval
= NULL
;
5126 #ifdef HAVE_GETSERVBYPORT
5127 struct servent
* serv
;
5128 char *proto_str
= NULL
;
5130 if (proto
&& *proto
)
5132 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
5134 EnterCriticalSection( &csWSgetXXXbyYYY
);
5135 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
5136 retval
= WS_dup_se(serv
);
5138 else SetLastError(WSANO_DATA
);
5139 LeaveCriticalSection( &csWSgetXXXbyYYY
);
5140 HeapFree( GetProcessHeap(), 0, proto_str
);
5142 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
5147 /***********************************************************************
5148 * gethostname (WS2_32.57)
5150 int WINAPI
WS_gethostname(char *name
, int namelen
)
5152 TRACE("name %p, len %d\n", name
, namelen
);
5154 if (gethostname(name
, namelen
) == 0)
5156 TRACE("<- '%s'\n", name
);
5159 SetLastError((errno
== EINVAL
) ? WSAEFAULT
: wsaErrno());
5160 TRACE("<- ERROR !\n");
5161 return SOCKET_ERROR
;
5165 /* ------------------------------------- Windows sockets extensions -- *
5167 * ------------------------------------------------------------------- */
5169 /***********************************************************************
5170 * WSAEnumNetworkEvents (WS2_32.36)
5172 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
5176 int errors
[FD_MAX_EVENTS
];
5178 TRACE("%08lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
5180 SERVER_START_REQ( get_socket_event
)
5182 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5183 req
->service
= TRUE
;
5184 req
->c_event
= wine_server_obj_handle( hEvent
);
5185 wine_server_set_reply( req
, errors
, sizeof(errors
) );
5186 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
5191 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
5192 lpEvent
->iErrorCode
[i
] = NtStatusToWSAError(errors
[i
]);
5195 SetLastError(WSAEINVAL
);
5196 return SOCKET_ERROR
;
5199 /***********************************************************************
5200 * WSAEventSelect (WS2_32.39)
5202 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
5206 TRACE("%08lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
5208 SERVER_START_REQ( set_socket_event
)
5210 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5212 req
->event
= wine_server_obj_handle( hEvent
);
5215 ret
= wine_server_call( req
);
5219 SetLastError(WSAEINVAL
);
5220 return SOCKET_ERROR
;
5223 /**********************************************************************
5224 * WSAGetOverlappedResult (WS2_32.40)
5226 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
5227 LPDWORD lpcbTransfer
, BOOL fWait
,
5232 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
5233 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
5235 if ( lpOverlapped
== NULL
)
5237 ERR( "Invalid pointer\n" );
5238 WSASetLastError(WSA_INVALID_PARAMETER
);
5242 status
= lpOverlapped
->Internal
;
5243 if (status
== STATUS_PENDING
)
5247 SetLastError( WSA_IO_INCOMPLETE
);
5251 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
5252 INFINITE
) == WAIT_FAILED
)
5254 status
= lpOverlapped
->Internal
;
5258 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
5261 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
5263 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
5268 /***********************************************************************
5269 * WSAAsyncSelect (WS2_32.101)
5271 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
5275 TRACE("%lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
5277 SERVER_START_REQ( set_socket_event
)
5279 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5282 req
->window
= wine_server_user_handle( hWnd
);
5284 ret
= wine_server_call( req
);
5288 SetLastError(WSAEINVAL
);
5289 return SOCKET_ERROR
;
5292 /***********************************************************************
5293 * WSACreateEvent (WS2_32.31)
5296 WSAEVENT WINAPI
WSACreateEvent(void)
5298 /* Create a manual-reset event, with initial state: unsignaled */
5301 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
5304 /***********************************************************************
5305 * WSACloseEvent (WS2_32.29)
5308 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
5310 TRACE ("event=%p\n", event
);
5312 return CloseHandle(event
);
5315 /***********************************************************************
5316 * WSASocketA (WS2_32.78)
5319 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
5320 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
5321 GROUP g
, DWORD dwFlags
)
5324 WSAPROTOCOL_INFOW info
;
5326 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
5327 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
5329 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
5331 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
5332 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
5333 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
5337 WSASetLastError( WSAEINVAL
);
5338 return SOCKET_ERROR
;
5341 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
5344 /***********************************************************************
5345 * WSASocketW (WS2_32.79)
5348 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
5349 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
5350 GROUP g
, DWORD dwFlags
)
5355 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
5356 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
5359 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
5360 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
5362 /* hack for WSADuplicateSocket */
5363 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
5364 ret
= lpProtocolInfo
->dwCatalogEntryId
;
5365 TRACE("\tgot duplicate %04lx\n", ret
);
5369 /* convert the socket family and type */
5370 af
= convert_af_w2u(af
);
5371 type
= convert_socktype_w2u(type
);
5375 if (af
== FROM_PROTOCOL_INFO
)
5376 af
= lpProtocolInfo
->iAddressFamily
;
5377 if (type
== FROM_PROTOCOL_INFO
)
5378 type
= lpProtocolInfo
->iSocketType
;
5379 if (protocol
== FROM_PROTOCOL_INFO
)
5380 protocol
= lpProtocolInfo
->iProtocol
;
5383 if ( af
== AF_UNSPEC
) /* did they not specify the address family? */
5385 if ((protocol
== IPPROTO_TCP
&& type
== SOCK_STREAM
) ||
5386 (protocol
== IPPROTO_UDP
&& type
== SOCK_DGRAM
))
5392 SetLastError(WSAEPROTOTYPE
);
5393 return INVALID_SOCKET
;
5397 SERVER_START_REQ( create_socket
)
5401 req
->protocol
= protocol
;
5402 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
5403 req
->attributes
= OBJ_INHERIT
;
5404 req
->flags
= dwFlags
;
5405 set_error( wine_server_call( req
) );
5406 ret
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
5411 TRACE("\tcreated %04lx\n", ret
);
5415 if (GetLastError() == WSAEACCES
) /* raw socket denied */
5417 if (type
== SOCK_RAW
)
5418 ERR_(winediag
)("Failed to create a socket of type SOCK_RAW, this requires special permissions.\n");
5420 ERR_(winediag
)("Failed to create socket, this requires special permissions.\n");
5421 SetLastError(WSAESOCKTNOSUPPORT
);
5424 WARN("\t\tfailed!\n");
5425 return INVALID_SOCKET
;
5428 /***********************************************************************
5429 * WSAJoinLeaf (WS2_32.58)
5432 SOCKET WINAPI
WSAJoinLeaf(
5434 const struct WS_sockaddr
*addr
,
5436 LPWSABUF lpCallerData
,
5437 LPWSABUF lpCalleeData
,
5443 return INVALID_SOCKET
;
5446 /***********************************************************************
5447 * __WSAFDIsSet (WS2_32.151)
5449 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
5451 int i
= set
->fd_count
;
5453 TRACE("(%ld,%p(%i))\n", s
, set
, i
);
5456 if (set
->fd_array
[i
] == s
) return 1;
5460 /***********************************************************************
5461 * WSAIsBlocking (WS2_32.114)
5463 BOOL WINAPI
WSAIsBlocking(void)
5465 /* By default WinSock should set all its sockets to non-blocking mode
5466 * and poll in PeekMessage loop when processing "blocking" ones. This
5467 * function is supposed to tell if the program is in this loop. Our
5468 * blocking calls are truly blocking so we always return FALSE.
5470 * Note: It is allowed to call this function without prior WSAStartup().
5477 /***********************************************************************
5478 * WSACancelBlockingCall (WS2_32.113)
5480 INT WINAPI
WSACancelBlockingCall(void)
5486 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
5488 FIXME("How was this called?\n");
5493 /***********************************************************************
5494 * WSASetBlockingHook (WS2_32.109)
5496 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
5498 FARPROC prev
= blocking_hook
;
5499 blocking_hook
= lpBlockFunc
;
5500 TRACE("hook %p\n", lpBlockFunc
);
5505 /***********************************************************************
5506 * WSAUnhookBlockingHook (WS2_32.110)
5508 INT WINAPI
WSAUnhookBlockingHook(void)
5510 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
5515 /* ----------------------------------- end of API stuff */
5517 /* ----------------------------------- helper functions -
5519 * TODO: Merge WS_dup_..() stuff into one function that
5520 * would operate with a generic structure containing internal
5521 * pointers (via a template of some kind).
5524 static int list_size(char** l
, int item_size
)
5529 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
5530 j
+= (i
+ 1) * sizeof(char*); }
5534 static int list_dup(char** l_src
, char** l_to
, int item_size
)
5539 for (i
= 0; l_src
[i
]; i
++) ;
5540 p
= (char *)(l_to
+ i
+ 1);
5541 for (i
= 0; l_src
[i
]; i
++)
5543 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
5544 memcpy(p
, l_src
[i
], count
);
5549 return p
- (char *)l_to
;
5554 /* create a hostent entry
5556 * Creates the entry with enough memory for the name, aliases
5557 * addresses, and the address pointers. Also copies the name
5558 * and sets up all the pointers. If "fill_addresses" is set then
5559 * sufficient memory for the addresses is also allocated and the
5560 * address pointers are set to this memory.
5562 * NOTE: The alias and address lists must be allocated with room
5563 * for the NULL item terminating the list. This is true even if
5564 * the list has no items ("aliases" and "addresses" must be
5565 * at least "1", a truly empty list is invalid).
5567 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int addresses
, int fill_addresses
)
5569 struct WS_hostent
*p_to
;
5572 int size
= (sizeof(struct WS_hostent
) +
5574 sizeof(char *)*aliases
+
5575 sizeof(char *)*addresses
);
5577 /* Allocate enough memory for the addresses */
5579 size
+= sizeof(struct in_addr
)*addresses
;
5581 if (!(p_to
= check_buffer_he(size
))) return NULL
;
5582 memset(p_to
, 0, size
);
5584 p
= (char *)(p_to
+ 1);
5589 p_to
->h_aliases
= (char **)p
;
5590 p
+= sizeof(char *)*aliases
;
5591 p_to
->h_addr_list
= (char **)p
;
5592 p
+= sizeof(char *)*addresses
;
5597 /* NOTE: h_aliases must be filled in manually, leave these
5598 * pointers NULL (already set to NULL by memset earlier).
5601 /* Fill in the list of address pointers */
5602 for (i
= 0; i
< addresses
; i
++)
5603 p_to
->h_addr_list
[i
] = (p
+= sizeof(struct in_addr
));
5604 p
+= sizeof(struct in_addr
);
5609 /* duplicate hostent entry
5610 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
5611 * Ditto for protoent and servent.
5613 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
5615 int addresses
= list_size(p_he
->h_addr_list
, p_he
->h_length
);
5616 int aliases
= list_size(p_he
->h_aliases
, 0);
5617 struct WS_hostent
*p_to
;
5619 p_to
= WS_create_he(p_he
->h_name
, aliases
, addresses
, FALSE
);
5621 if (!p_to
) return NULL
;
5622 p_to
->h_addrtype
= p_he
->h_addrtype
;
5623 p_to
->h_length
= p_he
->h_length
;
5625 list_dup(p_he
->h_aliases
, p_to
->h_aliases
, 0);
5626 list_dup(p_he
->h_addr_list
, p_to
->h_addr_list
, p_he
->h_length
);
5630 /* ----- protoent */
5632 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
)
5635 struct WS_protoent
*p_to
;
5637 int size
= (sizeof(*p_pe
) +
5638 strlen(p_pe
->p_name
) + 1 +
5639 list_size(p_pe
->p_aliases
, 0));
5641 if (!(p_to
= check_buffer_pe(size
))) return NULL
;
5642 p_to
->p_proto
= p_pe
->p_proto
;
5644 p
= (char *)(p_to
+ 1);
5646 strcpy(p
, p_pe
->p_name
);
5649 p_to
->p_aliases
= (char **)p
;
5650 list_dup(p_pe
->p_aliases
, p_to
->p_aliases
, 0);
5656 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
5659 struct WS_servent
*p_to
;
5661 int size
= (sizeof(*p_se
) +
5662 strlen(p_se
->s_proto
) + 1 +
5663 strlen(p_se
->s_name
) + 1 +
5664 list_size(p_se
->s_aliases
, 0));
5666 if (!(p_to
= check_buffer_se(size
))) return NULL
;
5667 p_to
->s_port
= p_se
->s_port
;
5669 p
= (char *)(p_to
+ 1);
5671 strcpy(p
, p_se
->s_name
);
5675 strcpy(p
, p_se
->s_proto
);
5678 p_to
->s_aliases
= (char **)p
;
5679 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
5684 /***********************************************************************
5685 * WSARecv (WS2_32.67)
5687 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5688 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
5689 LPWSAOVERLAPPED lpOverlapped
,
5690 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5692 return WS2_recv_base(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
5693 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
, NULL
);
5696 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5697 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
5698 struct WS_sockaddr
*lpFrom
,
5699 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
5700 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
5701 LPWSABUF lpControlBuffer
)
5703 unsigned int i
, options
;
5705 struct ws2_async
*wsa
;
5706 DWORD timeout_start
= GetTickCount();
5707 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
5709 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
5710 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
5711 (lpFromlen
? *lpFromlen
: -1),
5712 lpOverlapped
, lpCompletionRoutine
);
5714 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
5715 TRACE( "fd=%d, options=%x\n", fd
, options
);
5717 if (fd
== -1) return SOCKET_ERROR
;
5719 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
5725 wsa
->hSocket
= SOCKET2HANDLE(s
);
5726 wsa
->flags
= *lpFlags
;
5727 wsa
->lpFlags
= lpFlags
;
5729 wsa
->addrlen
.ptr
= lpFromlen
;
5730 wsa
->control
= lpControlBuffer
;
5731 wsa
->n_iovecs
= dwBufferCount
;
5732 wsa
->first_iovec
= 0;
5733 for (i
= 0; i
< dwBufferCount
; i
++)
5735 /* check buffer first to trigger write watches */
5736 if (IsBadWritePtr( lpBuffers
[i
].buf
, lpBuffers
[i
].len
))
5741 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
5742 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
5747 n
= WS2_recv( fd
, wsa
);
5750 if (errno
== EINTR
) continue;
5751 if (errno
!= EAGAIN
)
5753 int loc_errno
= errno
;
5755 if (cvalue
) WS_AddCompletion( s
, cvalue
, sock_get_ntstatus(loc_errno
), 0 );
5759 else if (lpNumberOfBytesRecvd
) *lpNumberOfBytesRecvd
= n
;
5761 if ((lpOverlapped
|| lpCompletionRoutine
) &&
5762 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
5764 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
5766 wsa
->user_overlapped
= lpOverlapped
;
5767 wsa
->completion_func
= lpCompletionRoutine
;
5768 release_sock_fd( s
, fd
);
5772 iosb
->u
.Status
= STATUS_PENDING
;
5773 iosb
->Information
= 0;
5775 SERVER_START_REQ( register_async
)
5777 req
->type
= ASYNC_TYPE_READ
;
5778 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
5779 req
->async
.callback
= wine_server_client_ptr( WS2_async_recv
);
5780 req
->async
.iosb
= wine_server_client_ptr( iosb
);
5781 req
->async
.arg
= wine_server_client_ptr( wsa
);
5782 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
5783 req
->async
.cvalue
= cvalue
;
5784 err
= wine_server_call( req
);
5788 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
5789 WSASetLastError( NtStatusToWSAError( err
));
5790 return SOCKET_ERROR
;
5793 iosb
->u
.Status
= STATUS_SUCCESS
;
5794 iosb
->Information
= n
;
5795 if (!wsa
->completion_func
)
5797 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
5798 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
5799 HeapFree( GetProcessHeap(), 0, wsa
);
5801 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
5802 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
5803 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5809 if ( _is_blocking(s
) )
5812 int timeout
= GET_RCVTIMEO(fd
);
5815 timeout
-= GetTickCount() - timeout_start
;
5816 if (timeout
< 0) timeout
= 0;
5820 pfd
.events
= POLLIN
;
5821 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
5823 if (!timeout
|| !poll( &pfd
, 1, timeout
))
5826 /* a timeout is not fatal */
5827 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5833 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5834 err
= WSAEWOULDBLOCK
;
5839 TRACE(" -> %i bytes\n", n
);
5840 HeapFree( GetProcessHeap(), 0, wsa
);
5841 release_sock_fd( s
, fd
);
5842 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5847 HeapFree( GetProcessHeap(), 0, wsa
);
5848 release_sock_fd( s
, fd
);
5849 WARN(" -> ERROR %d\n", err
);
5850 WSASetLastError( err
);
5851 return SOCKET_ERROR
;
5854 /***********************************************************************
5855 * WSARecvFrom (WS2_32.69)
5857 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5858 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
5859 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
5860 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5863 return WS2_recv_base( s
, lpBuffers
, dwBufferCount
,
5864 lpNumberOfBytesRecvd
, lpFlags
,
5866 lpOverlapped
, lpCompletionRoutine
, NULL
);
5869 /***********************************************************************
5870 * WSCInstallProvider (WS2_32.88)
5872 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
5873 LPCWSTR lpszProviderDllPath
,
5874 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
5875 DWORD dwNumberOfEntries
,
5878 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
5879 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
5880 dwNumberOfEntries
, lpErrno
);
5886 /***********************************************************************
5887 * WSCDeinstallProvider (WS2_32.83)
5889 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
5891 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
5897 /***********************************************************************
5898 * WSAAccept (WS2_32.26)
5900 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
5901 LPCONDITIONPROC lpfnCondition
, DWORD_PTR dwCallbackData
)
5904 int ret
= 0, size
= 0;
5905 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
5906 /* QOS SQOS, GQOS; */
5909 SOCKADDR src_addr
, dst_addr
;
5911 TRACE("Socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %ld\n",
5912 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
5915 size
= sizeof(src_addr
);
5916 cs
= WS_accept(s
, &src_addr
, &size
);
5918 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
5920 if (!lpfnCondition
) return cs
;
5922 CallerId
.buf
= (char *)&src_addr
;
5923 CallerId
.len
= sizeof(src_addr
);
5925 CallerData
.buf
= NULL
;
5928 WS_getsockname(cs
, &dst_addr
, &size
);
5930 CalleeId
.buf
= (char *)&dst_addr
;
5931 CalleeId
.len
= sizeof(dst_addr
);
5934 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
5935 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
5940 if (addr
&& addrlen
)
5941 memcpy(addr
, &src_addr
, (*addrlen
> size
) ? size
: *addrlen
);
5944 SERVER_START_REQ( set_socket_deferred
)
5946 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5947 req
->deferred
= wine_server_obj_handle( SOCKET2HANDLE(cs
) );
5948 if ( !wine_server_call_err ( req
) )
5950 SetLastError( WSATRY_AGAIN
);
5951 WS_closesocket( cs
);
5955 return SOCKET_ERROR
;
5958 SetLastError(WSAECONNREFUSED
);
5959 return SOCKET_ERROR
;
5961 FIXME("Unknown return type from Condition function\n");
5962 SetLastError(WSAENOTSOCK
);
5963 return SOCKET_ERROR
;
5967 /***********************************************************************
5968 * WSADuplicateSocketA (WS2_32.32)
5970 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
5974 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
5975 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
5976 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
5977 /* I don't know what the real Windoze does next, this is a hack */
5978 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
5979 * the target use the global duplicate, or we could copy a reference to us to the structure
5980 * and let the target duplicate it from us, but let's do it as simple as possible */
5981 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
5982 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
5983 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
5984 0, FALSE
, DUPLICATE_SAME_ACCESS
);
5985 CloseHandle(hProcess
);
5986 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
5990 /***********************************************************************
5991 * WSADuplicateSocketW (WS2_32.33)
5993 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
5997 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
5999 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
6000 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
6001 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
6002 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
6003 0, FALSE
, DUPLICATE_SAME_ACCESS
);
6004 CloseHandle(hProcess
);
6005 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
6009 /***********************************************************************
6010 * WSAInstallServiceClassA (WS2_32.48)
6012 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
6014 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
6015 WSASetLastError(WSAEACCES
);
6016 return SOCKET_ERROR
;
6019 /***********************************************************************
6020 * WSAInstallServiceClassW (WS2_32.49)
6022 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
6024 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
6025 WSASetLastError(WSAEACCES
);
6026 return SOCKET_ERROR
;
6029 /***********************************************************************
6030 * WSARemoveServiceClass (WS2_32.70)
6032 int WINAPI
WSARemoveServiceClass(LPGUID info
)
6034 FIXME("Request to remove service %p\n",info
);
6035 WSASetLastError(WSATYPE_NOT_FOUND
);
6036 return SOCKET_ERROR
;
6039 /***********************************************************************
6040 * inet_ntop (WS2_32.@)
6042 PCSTR WINAPI
WS_inet_ntop( INT family
, PVOID addr
, PSTR buffer
, SIZE_T len
)
6044 #ifdef HAVE_INET_NTOP
6045 struct WS_in6_addr
*in6
;
6046 struct WS_in_addr
*in
;
6049 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
6052 WSASetLastError( STATUS_INVALID_PARAMETER
);
6061 pdst
= inet_ntop( AF_INET
, &in
->WS_s_addr
, buffer
, len
);
6067 pdst
= inet_ntop( AF_INET6
, in6
->WS_s6_addr
, buffer
, len
);
6071 WSASetLastError( WSAEAFNOSUPPORT
);
6075 if (!pdst
) WSASetLastError( STATUS_INVALID_PARAMETER
);
6078 FIXME( "not supported on this platform\n" );
6079 WSASetLastError( WSAEAFNOSUPPORT
);
6084 /***********************************************************************
6085 * WSAStringToAddressA (WS2_32.80)
6087 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
6089 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
6090 LPSOCKADDR lpAddress
,
6091 LPINT lpAddressLength
)
6094 LPSTR workBuffer
=NULL
,ptrPort
;
6096 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_a(AddressString
), AddressFamily
,
6097 lpProtocolInfo
, lpAddress
, lpAddressLength
);
6099 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
6103 WSASetLastError(WSAEINVAL
);
6104 return SOCKET_ERROR
;
6108 FIXME("ProtocolInfo not implemented.\n");
6110 workBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
6111 strlen(AddressString
) + 1);
6114 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6115 return SOCKET_ERROR
;
6118 strcpy(workBuffer
, AddressString
);
6120 switch(AddressFamily
)
6124 struct in_addr inetaddr
;
6126 /* If lpAddressLength is too small, tell caller the size we need */
6127 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
6129 *lpAddressLength
= sizeof(SOCKADDR_IN
);
6133 *lpAddressLength
= sizeof(SOCKADDR_IN
);
6134 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
6136 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= AF_INET
;
6138 ptrPort
= strchr(workBuffer
, ':');
6141 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= htons(atoi(ptrPort
+1));
6146 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= 0;
6149 if(inet_aton(workBuffer
, &inetaddr
) > 0)
6151 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
6162 struct in6_addr inetaddr
;
6163 /* If lpAddressLength is too small, tell caller the size we need */
6164 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
6166 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
6170 #ifdef HAVE_INET_PTON
6171 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
6172 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
6174 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_family
= WS_AF_INET6
;
6176 /* This one is a bit tricky. An IPv6 address contains colons, so the
6177 * check from IPv4 doesn't work like that. However, IPv6 addresses that
6178 * contain a port are written with braces like [fd12:3456:7890::1]:12345
6179 * so what we will do is to look for ']', check if the next char is a
6180 * colon, and if it is, parse the port as in IPv4. */
6182 ptrPort
= strchr(workBuffer
, ']');
6183 if(ptrPort
&& *(++ptrPort
) == ':')
6185 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= htons(atoi(ptrPort
+1));
6190 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= 0;
6193 if(inet_pton(AF_INET6
, workBuffer
, &inetaddr
) > 0)
6195 memcpy(&((LPSOCKADDR_IN6
)lpAddress
)->sin6_addr
, &inetaddr
,
6196 sizeof(struct in6_addr
));
6200 #endif /* HAVE_INET_PTON */
6206 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
6207 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
6211 HeapFree(GetProcessHeap(), 0, workBuffer
);
6214 WSASetLastError(res
);
6215 return SOCKET_ERROR
;
6218 /***********************************************************************
6219 * WSAStringToAddressW (WS2_32.81)
6221 * Does anybody know if this function allows to use Hebrew/Arabic/Chinese... digits?
6222 * If this should be the case, it would be required to map these digits
6223 * to Unicode digits (0-9) using FoldString first.
6225 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
6227 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
6228 LPSOCKADDR lpAddress
,
6229 LPINT lpAddressLength
)
6232 LPSTR workBuffer
=NULL
;
6233 WSAPROTOCOL_INFOA infoA
;
6234 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
6236 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
6237 lpAddress
, lpAddressLength
);
6239 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
6241 /* if ProtocolInfo is available - convert to ANSI variant */
6244 lpProtoInfoA
= &infoA
;
6245 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
6247 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
6248 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
6250 WSASetLastError( WSAEINVAL
);
6251 return SOCKET_ERROR
;
6257 /* Translate AddressString to ANSI code page - assumes that only
6258 standard digits 0-9 are used with this API call */
6259 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
6260 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
6264 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
6265 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
6266 lpAddress
,lpAddressLength
);
6267 HeapFree( GetProcessHeap(), 0, workBuffer
);
6271 res
= WSA_NOT_ENOUGH_MEMORY
;
6276 WSASetLastError(res
);
6277 return SOCKET_ERROR
;
6280 /***********************************************************************
6281 * WSAAddressToStringA (WS2_32.27)
6283 * See WSAAddressToStringW
6285 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
6286 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
6290 CHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
6293 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
6295 if (!sockaddr
) return SOCKET_ERROR
;
6296 if (!string
|| !lenstr
) return SOCKET_ERROR
;
6298 switch(sockaddr
->sa_family
)
6301 if (len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
6302 sprintf( buffer
, "%u.%u.%u.%u:%u",
6303 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
6304 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
6305 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
6306 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
6307 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
6309 p
= strchr( buffer
, ':' );
6310 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
6315 struct WS_sockaddr_in6
*sockaddr6
= (LPSOCKADDR_IN6
) sockaddr
;
6318 if (len
< sizeof(SOCKADDR_IN6
)) return SOCKET_ERROR
;
6319 if ((sockaddr6
->sin6_port
))
6320 strcpy(buffer
, "[");
6321 if (!WS_inet_ntop(WS_AF_INET6
, &sockaddr6
->sin6_addr
, buffer
+strlen(buffer
), sizeof(buffer
)))
6323 WSASetLastError(WSAEINVAL
);
6324 return SOCKET_ERROR
;
6326 if ((sockaddr6
->sin6_scope_id
))
6327 sprintf(buffer
+strlen(buffer
), "%%%u", sockaddr6
->sin6_scope_id
);
6328 if ((sockaddr6
->sin6_port
))
6329 sprintf(buffer
+strlen(buffer
), "]:%u", ntohs(sockaddr6
->sin6_port
));
6334 WSASetLastError(WSAEINVAL
);
6335 return SOCKET_ERROR
;
6338 size
= strlen( buffer
) + 1;
6343 WSASetLastError(WSAEFAULT
);
6344 return SOCKET_ERROR
;
6348 strcpy( string
, buffer
);
6352 /***********************************************************************
6353 * WSAAddressToStringW (WS2_32.28)
6355 * Convert a sockaddr address into a readable address string.
6358 * sockaddr [I] Pointer to a sockaddr structure.
6359 * len [I] Size of the sockaddr structure.
6360 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
6361 * string [I/O] Pointer to a buffer to receive the address string.
6362 * lenstr [I/O] Size of the receive buffer in WCHARs.
6366 * Failure: SOCKET_ERROR
6369 * The 'info' parameter is ignored.
6371 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
6372 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
6377 WCHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
6380 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
6383 ret
= WSAAddressToStringA(sockaddr
, len
, NULL
, bufAddr
, &size
);
6385 if (ret
) return ret
;
6387 MultiByteToWideChar( CP_ACP
, 0, bufAddr
, size
, buffer
, sizeof( buffer
)/sizeof(WCHAR
));
6392 WSASetLastError(WSAEFAULT
);
6393 return SOCKET_ERROR
;
6397 lstrcpyW( string
, buffer
);
6401 /***********************************************************************
6402 * WSAEnumNameSpaceProvidersA (WS2_32.34)
6404 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
6406 FIXME( "(%p %p) Stub!\n", len
, buffer
);
6410 /***********************************************************************
6411 * WSAEnumNameSpaceProvidersW (WS2_32.35)
6413 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
6415 FIXME( "(%p %p) Stub!\n", len
, buffer
);
6419 /***********************************************************************
6420 * WSAGetQOSByName (WS2_32.41)
6422 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
6424 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
6428 /***********************************************************************
6429 * WSAGetServiceClassInfoA (WS2_32.42)
6431 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
6432 LPWSASERVICECLASSINFOA info
)
6434 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
6436 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6437 return SOCKET_ERROR
;
6440 /***********************************************************************
6441 * WSAGetServiceClassInfoW (WS2_32.43)
6443 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
6444 LPWSASERVICECLASSINFOW info
)
6446 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
6448 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6449 return SOCKET_ERROR
;
6452 /***********************************************************************
6453 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
6455 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
6457 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
6458 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6459 return SOCKET_ERROR
;
6462 /***********************************************************************
6463 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
6465 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
6467 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
6468 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6469 return SOCKET_ERROR
;
6472 /***********************************************************************
6473 * WSALookupServiceBeginA (WS2_32.59)
6475 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
6476 DWORD dwControlFlags
,
6479 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
6481 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6482 return SOCKET_ERROR
;
6485 /***********************************************************************
6486 * WSALookupServiceBeginW (WS2_32.60)
6488 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
6489 DWORD dwControlFlags
,
6492 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
6494 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6495 return SOCKET_ERROR
;
6498 /***********************************************************************
6499 * WSALookupServiceEnd (WS2_32.61)
6501 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
6503 FIXME("(%p) Stub!\n", lookup
);
6507 /***********************************************************************
6508 * WSALookupServiceNextA (WS2_32.62)
6510 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
6512 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
6513 WSASetLastError(WSA_E_NO_MORE
);
6514 return SOCKET_ERROR
;
6517 /***********************************************************************
6518 * WSALookupServiceNextW (WS2_32.63)
6520 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
6522 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
6523 WSASetLastError(WSA_E_NO_MORE
);
6524 return SOCKET_ERROR
;
6527 /***********************************************************************
6528 * WSANtohl (WS2_32.64)
6530 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
6532 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
6534 if (!lphostlong
) return WSAEFAULT
;
6536 *lphostlong
= ntohl( netlong
);
6540 /***********************************************************************
6541 * WSANtohs (WS2_32.65)
6543 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
6545 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
6547 if (!lphostshort
) return WSAEFAULT
;
6549 *lphostshort
= ntohs( netshort
);
6553 /***********************************************************************
6554 * WSAProviderConfigChange (WS2_32.66)
6556 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
6557 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
6559 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
6560 return SOCKET_ERROR
;
6563 /***********************************************************************
6564 * WSARecvDisconnect (WS2_32.68)
6566 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
6568 TRACE( "(0x%04lx %p)\n", s
, disconnectdata
);
6570 return WS_shutdown( s
, 0 );
6573 /***********************************************************************
6574 * WSASetServiceA (WS2_32.76)
6576 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
6578 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
6582 /***********************************************************************
6583 * WSASetServiceW (WS2_32.77)
6585 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
6587 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
6591 /***********************************************************************
6592 * WSCEnableNSProvider (WS2_32.84)
6594 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
6596 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
6600 /***********************************************************************
6601 * WSCGetProviderPath (WS2_32.86)
6603 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
6605 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
6607 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
6609 *errcode
= WSAEINVAL
;
6610 return SOCKET_ERROR
;
6613 /***********************************************************************
6614 * WSCInstallNameSpace (WS2_32.87)
6616 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
6617 DWORD version
, LPGUID provider
)
6619 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
6620 namespace, version
, debugstr_guid(provider
) );
6624 /***********************************************************************
6625 * WSCUnInstallNameSpace (WS2_32.89)
6627 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
6629 FIXME("(%p) Stub!\n", lpProviderId
);
6633 /***********************************************************************
6634 * WSCWriteProviderOrder (WS2_32.91)
6636 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
6638 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);
6642 /***********************************************************************
6643 * WSANSPIoctl (WS2_32.91)
6645 INT WINAPI
WSANSPIoctl( HANDLE hLookup
, DWORD dwControlCode
, LPVOID lpvInBuffer
,
6646 DWORD cbInBuffer
, LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
6647 LPDWORD lpcbBytesReturned
, LPWSACOMPLETION lpCompletion
)
6649 FIXME("(%p, 0x%08x, %p, 0x%08x, %p, 0x%08x, %p, %p) Stub!\n", hLookup
, dwControlCode
,
6650 lpvInBuffer
, cbInBuffer
, lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpCompletion
);
6651 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6652 return SOCKET_ERROR
;