2 * based on Windows Sockets 1.1 specs
4 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
5 * Copyright (C) 2005 Marcus Meissner
6 * Copyright (C) 2006-2008 Kai Blin
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * NOTE: If you make any changes to fix a particular app, make sure
23 * they don't break something else like Netscape or telnet and ftp
24 * clients and servers (www.winsite.com got a lot of those).
28 #include "wine/port.h"
33 #include <sys/types.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 # include <sys/ioctl.h>
40 #ifdef HAVE_SYS_FILIO_H
41 # include <sys/filio.h>
43 #ifdef HAVE_SYS_SOCKIO_H
44 # include <sys/sockio.h>
48 # include <sys/so_ioctl.h>
51 #ifdef HAVE_SYS_PARAM_H
52 # include <sys/param.h>
58 #ifdef HAVE_SYS_WAIT_H
59 # include <sys/wait.h>
64 #ifdef HAVE_SYS_SOCKET_H
65 #include <sys/socket.h>
67 #ifdef HAVE_NETINET_IN_H
68 # include <netinet/in.h>
70 #ifdef HAVE_NETINET_TCP_H
71 # include <netinet/tcp.h>
73 #ifdef HAVE_ARPA_INET_H
74 # include <arpa/inet.h>
86 #ifdef HAVE_ARPA_NAMESER_H
87 # include <arpa/nameser.h>
96 #ifdef HAVE_NETIPX_IPX_H
97 # include <netipx/ipx.h>
99 #elif defined(HAVE_LINUX_IPX_H)
100 # ifdef HAVE_ASM_TYPES_H
101 # include <asm/types.h>
103 # ifdef HAVE_LINUX_TYPES_H
104 # include <linux/types.h>
106 # include <linux/ipx.h>
110 #ifdef HAVE_LINUX_IRDA_H
111 # ifdef HAVE_LINUX_TYPES_H
112 # include <linux/types.h>
114 # include <linux/irda.h>
121 #ifdef HAVE_SYS_POLL_H
122 # include <sys/poll.h>
124 #ifdef HAVE_SYS_TIME_H
125 # include <sys/time.h>
128 #define NONAMELESSUNION
129 #define NONAMELESSSTRUCT
130 #include "ntstatus.h"
131 #define WIN32_NO_STATUS
136 #include "winerror.h"
138 #include "winsock2.h"
140 #include "ws2tcpip.h"
146 #define USE_WC_PREFIX /* For CMSG_DATA */
147 #include "iphlpapi.h"
148 #include "wine/server.h"
149 #include "wine/debug.h"
150 #include "wine/exception.h"
151 #include "wine/unicode.h"
154 # include "wsnwlink.h"
158 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
159 # define sipx_network sipx_addr.x_net
160 # define sipx_node sipx_addr.x_host.c_host
161 #endif /* __FreeBSD__ */
164 #define INADDR_NONE ~0UL
167 WINE_DEFAULT_DEBUG_CHANNEL(winsock
);
168 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
172 * The actual definition of WSASendTo, wrapped in a different function name
173 * so that internal calls from ws2_32 itself will not trigger programs like
174 * Garena, which hooks WSASendTo/WSARecvFrom calls.
176 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
177 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
178 const struct WS_sockaddr
*to
, int tolen
,
179 LPWSAOVERLAPPED lpOverlapped
,
180 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
183 * Internal fundamental receive function, essentially WSARecvFrom with an
184 * additional parameter to support message control headers.
186 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
187 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
188 struct WS_sockaddr
*lpFrom
,
189 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
190 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
191 LPWSABUF lpControlBuffer
);
193 /* critical section to protect some non-reentrant net function */
194 static CRITICAL_SECTION csWSgetXXXbyYYY
;
195 static CRITICAL_SECTION_DEBUG critsect_debug
=
197 0, 0, &csWSgetXXXbyYYY
,
198 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
199 0, 0, { (DWORD_PTR
)(__FILE__
": csWSgetXXXbyYYY") }
201 static CRITICAL_SECTION csWSgetXXXbyYYY
= { &critsect_debug
, -1, 0, 0, 0, 0 };
203 union generic_unix_sockaddr
205 struct sockaddr addr
;
206 char data
[128]; /* should be big enough for all families */
209 static inline const char *debugstr_sockaddr( const struct WS_sockaddr
*a
)
211 if (!a
) return "(nil)";
212 switch (a
->sa_family
)
215 return wine_dbg_sprintf("{ family AF_INET, address %s, port %d }",
216 inet_ntoa(((const struct sockaddr_in
*)a
)->sin_addr
),
217 ntohs(((const struct sockaddr_in
*)a
)->sin_port
));
222 struct WS_sockaddr_in6
*sin
= (struct WS_sockaddr_in6
*)a
;
224 p
= WS_inet_ntop( WS_AF_INET6
, &sin
->sin6_addr
, buf
, sizeof(buf
) );
226 p
= "(unknown IPv6 address)";
227 return wine_dbg_sprintf("{ family AF_INET6, address %s, port %d }",
228 p
, ntohs(sin
->sin6_port
));
234 memcpy( &addr
, ((const SOCKADDR_IRDA
*)a
)->irdaDeviceID
, sizeof(addr
) );
235 addr
= ntohl( addr
);
236 return wine_dbg_sprintf("{ family AF_IRDA, addr %08x, name %s }",
238 ((const SOCKADDR_IRDA
*)a
)->irdaServiceName
);
241 return wine_dbg_sprintf("{ family %d }", a
->sa_family
);
245 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
246 #define SOCKET2HANDLE(s) ((HANDLE)(s))
247 #define HANDLE2SOCKET(h) ((SOCKET)(h))
249 /****************************************************************
250 * Async IO declarations
251 ****************************************************************/
253 typedef struct ws2_async
257 LPWSAOVERLAPPED user_overlapped
;
258 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func
;
259 IO_STATUS_BLOCK local_iosb
;
260 struct WS_sockaddr
*addr
;
263 int val
; /* for send operations */
264 int *ptr
; /* for recv operations */
269 unsigned int n_iovecs
;
270 unsigned int first_iovec
;
271 struct iovec iovec
[1];
274 typedef struct ws2_accept_async
276 HANDLE listen_socket
;
277 HANDLE accept_socket
;
278 LPOVERLAPPED user_overlapped
;
280 PVOID buf
; /* buffer to write data to */
284 struct ws2_async
*read
;
287 /****************************************************************/
289 /* ----------------------------------- internal data */
291 /* ws_... struct conversion flags */
293 typedef struct /* WSAAsyncSelect() control struct */
295 HANDLE service
, event
, sock
;
301 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
302 #define WS_MAX_UDP_DATAGRAM 1024
303 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
305 /* hostent's, servent's and protent's are stored in one buffer per thread,
306 * as documented on MSDN for the functions that return any of the buffers */
307 struct per_thread_data
310 struct WS_hostent
*he_buffer
;
311 struct WS_servent
*se_buffer
;
312 struct WS_protoent
*pe_buffer
;
318 /* internal: routing description information */
325 static INT num_startup
; /* reference counter */
326 static FARPROC blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
328 /* function prototypes */
329 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int addresses
, int fill_addresses
);
330 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
331 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
);
332 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
334 int WSAIOCTL_GetInterfaceCount(void);
335 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
337 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
, ULONG Information
);
339 #define MAP_OPTION(opt) { WS_##opt, opt }
341 static const int ws_sock_map
[][2] =
343 MAP_OPTION( SO_DEBUG
),
344 MAP_OPTION( SO_ACCEPTCONN
),
345 MAP_OPTION( SO_REUSEADDR
),
346 MAP_OPTION( SO_KEEPALIVE
),
347 MAP_OPTION( SO_DONTROUTE
),
348 MAP_OPTION( SO_BROADCAST
),
349 MAP_OPTION( SO_LINGER
),
350 MAP_OPTION( SO_OOBINLINE
),
351 MAP_OPTION( SO_SNDBUF
),
352 MAP_OPTION( SO_RCVBUF
),
353 MAP_OPTION( SO_ERROR
),
354 MAP_OPTION( SO_TYPE
),
356 MAP_OPTION( SO_RCVTIMEO
),
359 MAP_OPTION( SO_SNDTIMEO
),
363 static const int ws_tcp_map
[][2] =
366 MAP_OPTION( TCP_NODELAY
),
370 static const int ws_ip_map
[][2] =
372 MAP_OPTION( IP_MULTICAST_IF
),
373 MAP_OPTION( IP_MULTICAST_TTL
),
374 MAP_OPTION( IP_MULTICAST_LOOP
),
375 MAP_OPTION( IP_ADD_MEMBERSHIP
),
376 MAP_OPTION( IP_DROP_MEMBERSHIP
),
377 MAP_OPTION( IP_OPTIONS
),
379 MAP_OPTION( IP_HDRINCL
),
381 MAP_OPTION( IP_TOS
),
382 MAP_OPTION( IP_TTL
),
384 MAP_OPTION( IP_PKTINFO
),
388 static const int ws_ipv6_map
[][2] =
390 #ifdef IPV6_ADD_MEMBERSHIP
391 MAP_OPTION( IPV6_ADD_MEMBERSHIP
),
393 #ifdef IPV6_DROP_MEMBERSHIP
394 MAP_OPTION( IPV6_DROP_MEMBERSHIP
),
396 MAP_OPTION( IPV6_MULTICAST_IF
),
397 MAP_OPTION( IPV6_MULTICAST_HOPS
),
398 MAP_OPTION( IPV6_MULTICAST_LOOP
),
399 MAP_OPTION( IPV6_UNICAST_HOPS
),
400 MAP_OPTION( IPV6_V6ONLY
),
403 static const int ws_af_map
[][2] =
405 MAP_OPTION( AF_UNSPEC
),
406 MAP_OPTION( AF_INET
),
407 MAP_OPTION( AF_INET6
),
409 MAP_OPTION( AF_IPX
),
412 MAP_OPTION( AF_IRDA
),
414 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
417 static const int ws_socktype_map
[][2] =
419 MAP_OPTION( SOCK_DGRAM
),
420 MAP_OPTION( SOCK_STREAM
),
421 MAP_OPTION( SOCK_RAW
),
422 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
425 static const int ws_proto_map
[][2] =
427 MAP_OPTION( IPPROTO_IP
),
428 MAP_OPTION( IPPROTO_TCP
),
429 MAP_OPTION( IPPROTO_UDP
),
430 MAP_OPTION( IPPROTO_ICMP
),
431 MAP_OPTION( IPPROTO_IGMP
),
432 MAP_OPTION( IPPROTO_RAW
),
433 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
436 static const int ws_aiflag_map
[][2] =
438 MAP_OPTION( AI_PASSIVE
),
439 MAP_OPTION( AI_CANONNAME
),
440 MAP_OPTION( AI_NUMERICHOST
),
441 MAP_OPTION( AI_ADDRCONFIG
),
444 static const int ws_niflag_map
[][2] =
446 MAP_OPTION( NI_NOFQDN
),
447 MAP_OPTION( NI_NUMERICHOST
),
448 MAP_OPTION( NI_NAMEREQD
),
449 MAP_OPTION( NI_NUMERICSERV
),
450 MAP_OPTION( NI_DGRAM
),
453 static const int ws_eai_map
[][2] =
455 MAP_OPTION( EAI_AGAIN
),
456 MAP_OPTION( EAI_BADFLAGS
),
457 MAP_OPTION( EAI_FAIL
),
458 MAP_OPTION( EAI_FAMILY
),
459 MAP_OPTION( EAI_MEMORY
),
460 /* Note: EAI_NODATA is deprecated, but still
461 * used by Windows and Linux... We map the newer
462 * EAI_NONAME to EAI_NODATA for now until Windows
466 MAP_OPTION( EAI_NODATA
),
469 { WS_EAI_NODATA
, EAI_NONAME
},
472 MAP_OPTION( EAI_SERVICE
),
473 MAP_OPTION( EAI_SOCKTYPE
),
477 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
479 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
480 static inline WSACMSGHDR
*fill_control_message(int level
, int type
, WSACMSGHDR
*current
, ULONG
*maxsize
, void *data
, int len
)
482 ULONG msgsize
= sizeof(WSACMSGHDR
) + WSA_CMSG_ALIGN(len
);
483 char *ptr
= (char *) current
+ sizeof(WSACMSGHDR
);
485 /* Make sure there is at least enough room for this entry */
486 if (msgsize
> *maxsize
)
489 /* Fill in the entry */
490 current
->cmsg_len
= sizeof(WSACMSGHDR
) + len
;
491 current
->cmsg_level
= level
;
492 current
->cmsg_type
= type
;
493 memcpy(ptr
, data
, len
);
494 /* Return the pointer to where next entry should go */
495 return (WSACMSGHDR
*) (ptr
+ WSA_CMSG_ALIGN(len
));
498 static inline int convert_control_headers(struct msghdr
*hdr
, WSABUF
*control
)
501 WSACMSGHDR
*cmsg_win
= (WSACMSGHDR
*) control
->buf
, *ptr
;
502 ULONG ctlsize
= control
->len
;
503 struct cmsghdr
*cmsg_unix
;
506 /* Loop over all the headers, converting as appropriate */
507 for (cmsg_unix
= CMSG_FIRSTHDR(hdr
); cmsg_unix
!= NULL
; cmsg_unix
= CMSG_NXTHDR(hdr
, cmsg_unix
))
509 switch(cmsg_unix
->cmsg_level
)
512 switch(cmsg_unix
->cmsg_type
)
516 /* Convert the Unix IP_PKTINFO structure to the Windows version */
517 struct in_pktinfo
*data_unix
= (struct in_pktinfo
*) CMSG_DATA(cmsg_unix
);
518 struct WS_in_pktinfo data_win
;
520 memcpy(&data_win
.ipi_addr
,&data_unix
->ipi_addr
.s_addr
,4); /* 4 bytes = 32 address bits */
521 data_win
.ipi_ifindex
= data_unix
->ipi_ifindex
;
522 ptr
= fill_control_message(WS_IPPROTO_IP
, WS_IP_PKTINFO
, ptr
, &ctlsize
,
523 (void*)&data_win
, sizeof(data_win
));
524 if (!ptr
) goto error
;
527 FIXME("Unhandled IPPROTO_IP message header type %d\n", cmsg_unix
->cmsg_type
);
532 FIXME("Unhandled message header level %d\n", cmsg_unix
->cmsg_level
);
538 /* Set the length of the returned control headers */
539 control
->len
= (ptr
== NULL
? 0 : (char*)ptr
- (char*)cmsg_win
);
540 return (ptr
!= NULL
);
541 #else /* IP_PKTINFO */
544 #endif /* IP_PKTINFO */
546 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
548 /* ----------------------------------- error handling */
550 static NTSTATUS
sock_get_ntstatus( int err
)
554 case EBADF
: return STATUS_INVALID_HANDLE
;
555 case EBUSY
: return STATUS_DEVICE_BUSY
;
557 case EACCES
: return STATUS_ACCESS_DENIED
;
558 case EFAULT
: return STATUS_NO_MEMORY
;
559 case EINVAL
: return STATUS_INVALID_PARAMETER
;
561 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
562 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
563 case EINPROGRESS
: return STATUS_PENDING
;
564 case EALREADY
: return STATUS_NETWORK_BUSY
;
565 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
566 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
567 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
568 case EPROTONOSUPPORT
:
569 case ESOCKTNOSUPPORT
:
572 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
573 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
574 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
575 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
576 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
577 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
578 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
579 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
580 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
581 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
582 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
584 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
585 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
587 case 0: return STATUS_SUCCESS
;
589 WARN("Unknown errno %d!\n", err
);
590 return STATUS_UNSUCCESSFUL
;
594 static UINT
sock_get_error( int err
)
598 case EINTR
: return WSAEINTR
;
599 case EBADF
: return WSAEBADF
;
601 case EACCES
: return WSAEACCES
;
602 case EFAULT
: return WSAEFAULT
;
603 case EINVAL
: return WSAEINVAL
;
604 case EMFILE
: return WSAEMFILE
;
605 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
606 case EINPROGRESS
: return WSAEINPROGRESS
;
607 case EALREADY
: return WSAEALREADY
;
608 case ENOTSOCK
: return WSAENOTSOCK
;
609 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
610 case EMSGSIZE
: return WSAEMSGSIZE
;
611 case EPROTOTYPE
: return WSAEPROTOTYPE
;
612 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
613 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
614 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
615 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
616 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
617 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
618 case EADDRINUSE
: return WSAEADDRINUSE
;
619 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
620 case ENETDOWN
: return WSAENETDOWN
;
621 case ENETUNREACH
: return WSAENETUNREACH
;
622 case ENETRESET
: return WSAENETRESET
;
623 case ECONNABORTED
: return WSAECONNABORTED
;
625 case ECONNRESET
: return WSAECONNRESET
;
626 case ENOBUFS
: return WSAENOBUFS
;
627 case EISCONN
: return WSAEISCONN
;
628 case ENOTCONN
: return WSAENOTCONN
;
629 case ESHUTDOWN
: return WSAESHUTDOWN
;
630 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
631 case ETIMEDOUT
: return WSAETIMEDOUT
;
632 case ECONNREFUSED
: return WSAECONNREFUSED
;
633 case ELOOP
: return WSAELOOP
;
634 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
635 case EHOSTDOWN
: return WSAEHOSTDOWN
;
636 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
637 case ENOTEMPTY
: return WSAENOTEMPTY
;
639 case EPROCLIM
: return WSAEPROCLIM
;
642 case EUSERS
: return WSAEUSERS
;
645 case EDQUOT
: return WSAEDQUOT
;
648 case ESTALE
: return WSAESTALE
;
651 case EREMOTE
: return WSAEREMOTE
;
654 /* just in case we ever get here and there are no problems */
657 WARN("Unknown errno %d!\n", err
);
658 return WSAEOPNOTSUPP
;
662 static UINT
wsaErrno(void)
664 int loc_errno
= errno
;
665 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
667 return sock_get_error( loc_errno
);
670 /* most ws2 overlapped functions return an ntstatus-based error code */
671 static NTSTATUS
wsaErrStatus(void)
673 int loc_errno
= errno
;
674 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
676 return sock_get_ntstatus(loc_errno
);
679 static UINT
wsaHerrno(int loc_errno
)
681 WARN("h_errno %d.\n", loc_errno
);
685 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
686 case TRY_AGAIN
: return WSATRY_AGAIN
;
687 case NO_RECOVERY
: return WSANO_RECOVERY
;
688 case NO_DATA
: return WSANO_DATA
;
689 case ENOBUFS
: return WSAENOBUFS
;
693 WARN("Unknown h_errno %d!\n", loc_errno
);
694 return WSAEOPNOTSUPP
;
698 static inline DWORD
NtStatusToWSAError( const DWORD status
)
700 /* We only need to cover the status codes set by server async request handling */
704 case STATUS_SUCCESS
: wserr
= 0; break;
705 case STATUS_PENDING
: wserr
= WSA_IO_PENDING
; break;
706 case STATUS_OBJECT_TYPE_MISMATCH
: wserr
= WSAENOTSOCK
; break;
707 case STATUS_INVALID_HANDLE
: wserr
= WSAEBADF
; break;
708 case STATUS_INVALID_PARAMETER
: wserr
= WSAEINVAL
; break;
709 case STATUS_PIPE_DISCONNECTED
: wserr
= WSAESHUTDOWN
; break;
710 case STATUS_NETWORK_BUSY
: wserr
= WSAEALREADY
; break;
711 case STATUS_NETWORK_UNREACHABLE
: wserr
= WSAENETUNREACH
; break;
712 case STATUS_CONNECTION_REFUSED
: wserr
= WSAECONNREFUSED
; break;
713 case STATUS_CONNECTION_DISCONNECTED
: wserr
= WSAENOTCONN
; break;
714 case STATUS_CONNECTION_RESET
: wserr
= WSAECONNRESET
; break;
715 case STATUS_CONNECTION_ABORTED
: wserr
= WSAECONNABORTED
; break;
716 case STATUS_CANCELLED
: wserr
= WSA_OPERATION_ABORTED
; break;
717 case STATUS_ADDRESS_ALREADY_ASSOCIATED
: wserr
= WSAEADDRINUSE
; break;
718 case STATUS_IO_TIMEOUT
:
719 case STATUS_TIMEOUT
: wserr
= WSAETIMEDOUT
; break;
720 case STATUS_NO_MEMORY
: wserr
= WSAEFAULT
; break;
721 case STATUS_ACCESS_DENIED
: wserr
= WSAEACCES
; break;
722 case STATUS_TOO_MANY_OPENED_FILES
: wserr
= WSAEMFILE
; break;
723 case STATUS_CANT_WAIT
: wserr
= WSAEWOULDBLOCK
; break;
724 case STATUS_BUFFER_OVERFLOW
: wserr
= WSAEMSGSIZE
; break;
725 case STATUS_NOT_SUPPORTED
: wserr
= WSAEOPNOTSUPP
; break;
726 case STATUS_HOST_UNREACHABLE
: wserr
= WSAEHOSTUNREACH
; break;
729 wserr
= RtlNtStatusToDosError( status
);
730 FIXME( "Status code %08x converted to DOS error code %x\n", status
, wserr
);
735 /* set last error code from NT status without mapping WSA errors */
736 static inline unsigned int set_error( unsigned int err
)
740 err
= NtStatusToWSAError( err
);
746 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
749 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
754 static inline void release_sock_fd( SOCKET s
, int fd
)
756 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
759 static void _enable_event( HANDLE s
, unsigned int event
,
760 unsigned int sstate
, unsigned int cstate
)
762 SERVER_START_REQ( enable_socket_event
)
764 req
->handle
= wine_server_obj_handle( s
);
766 req
->sstate
= sstate
;
767 req
->cstate
= cstate
;
768 wine_server_call( req
);
773 static int _is_blocking(SOCKET s
)
776 SERVER_START_REQ( get_socket_event
)
778 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
779 req
->service
= FALSE
;
781 wine_server_call( req
);
782 ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
788 static unsigned int _get_sock_mask(SOCKET s
)
791 SERVER_START_REQ( get_socket_event
)
793 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
794 req
->service
= FALSE
;
796 wine_server_call( req
);
803 static void _sync_sock_state(SOCKET s
)
805 /* do a dummy wineserver request in order to let
806 the wineserver run through its select loop once */
807 (void)_is_blocking(s
);
810 static int _get_sock_error(SOCKET s
, unsigned int bit
)
812 int events
[FD_MAX_EVENTS
];
814 SERVER_START_REQ( get_socket_event
)
816 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
817 req
->service
= FALSE
;
819 wine_server_set_reply( req
, events
, sizeof(events
) );
820 wine_server_call( req
);
826 static struct per_thread_data
*get_per_thread_data(void)
828 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
829 /* lazy initialization */
832 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
833 NtCurrentTeb()->WinSockData
= ptb
;
838 static void free_per_thread_data(void)
840 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
844 /* delete scratch buffers */
845 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
846 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
847 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
848 ptb
->he_buffer
= NULL
;
849 ptb
->se_buffer
= NULL
;
850 ptb
->pe_buffer
= NULL
;
852 HeapFree( GetProcessHeap(), 0, ptb
);
853 NtCurrentTeb()->WinSockData
= NULL
;
856 /***********************************************************************
857 * DllMain (WS2_32.init)
859 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
861 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
863 case DLL_PROCESS_ATTACH
:
865 case DLL_PROCESS_DETACH
:
866 free_per_thread_data();
867 DeleteCriticalSection(&csWSgetXXXbyYYY
);
870 case DLL_THREAD_DETACH
:
871 free_per_thread_data();
877 /***********************************************************************
880 * Converts socket flags from Windows format.
881 * Return 1 if converted, 0 if not (error).
883 static int convert_sockopt(INT
*level
, INT
*optname
)
890 for(i
=0; i
<sizeof(ws_sock_map
)/sizeof(ws_sock_map
[0]); i
++) {
891 if( ws_sock_map
[i
][0] == *optname
)
893 *optname
= ws_sock_map
[i
][1];
897 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
900 *level
= IPPROTO_TCP
;
901 for(i
=0; i
<sizeof(ws_tcp_map
)/sizeof(ws_tcp_map
[0]); i
++) {
902 if ( ws_tcp_map
[i
][0] == *optname
)
904 *optname
= ws_tcp_map
[i
][1];
908 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
912 for(i
=0; i
<sizeof(ws_ip_map
)/sizeof(ws_ip_map
[0]); i
++) {
913 if (ws_ip_map
[i
][0] == *optname
)
915 *optname
= ws_ip_map
[i
][1];
919 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
921 case WS_IPPROTO_IPV6
:
922 *level
= IPPROTO_IPV6
;
923 for(i
=0; i
<sizeof(ws_ipv6_map
)/sizeof(ws_ipv6_map
[0]); i
++) {
924 if (ws_ipv6_map
[i
][0] == *optname
)
926 *optname
= ws_ipv6_map
[i
][1];
930 FIXME("Unknown IPPROTO_IPV6 optname 0x%x\n", *optname
);
932 default: FIXME("Unimplemented or unknown socket level\n");
937 /* ----------------------------------- Per-thread info (or per-process?) */
939 static char *strdup_lower(const char *str
)
942 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
946 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
949 else SetLastError(WSAENOBUFS
);
953 static inline int sock_error_p(int s
)
955 unsigned int optval
, optlen
;
957 optlen
= sizeof(optval
);
958 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
959 if (optval
) WARN("\t[%i] error: %d\n", s
, optval
);
963 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
964 * from an fd and return the value converted to milli seconds
965 * or -1 if there is an infinite time out */
966 static inline int get_rcvsnd_timeo( int fd
, int optname
)
969 unsigned int len
= sizeof(tv
);
970 int ret
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
972 ret
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
973 if( ret
<= 0 ) /* tv == {0,0} means infinite time out */
978 /* macro wrappers for portability */
980 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
982 #define GET_RCVTIMEO(fd) (-1)
986 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
988 #define GET_SNDTIMEO(fd) (-1)
991 /* utility: given an fd, will block until one of the events occurs */
992 static inline int do_block( int fd
, int events
, int timeout
)
1000 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
1011 convert_af_w2u(int windowsaf
) {
1014 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
1015 if (ws_af_map
[i
][0] == windowsaf
)
1016 return ws_af_map
[i
][1];
1017 FIXME("unhandled Windows address family %d\n", windowsaf
);
1022 convert_af_u2w(int unixaf
) {
1025 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
1026 if (ws_af_map
[i
][1] == unixaf
)
1027 return ws_af_map
[i
][0];
1028 FIXME("unhandled UNIX address family %d\n", unixaf
);
1033 convert_proto_w2u(int windowsproto
) {
1036 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
1037 if (ws_proto_map
[i
][0] == windowsproto
)
1038 return ws_proto_map
[i
][1];
1039 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
1044 convert_proto_u2w(int unixproto
) {
1047 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
1048 if (ws_proto_map
[i
][1] == unixproto
)
1049 return ws_proto_map
[i
][0];
1050 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
1055 convert_socktype_w2u(int windowssocktype
) {
1058 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
1059 if (ws_socktype_map
[i
][0] == windowssocktype
)
1060 return ws_socktype_map
[i
][1];
1061 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
1066 convert_socktype_u2w(int unixsocktype
) {
1069 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
1070 if (ws_socktype_map
[i
][1] == unixsocktype
)
1071 return ws_socktype_map
[i
][0];
1072 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
1076 /* ----------------------------------- API -----
1078 * Init / cleanup / error checking.
1081 /***********************************************************************
1082 * WSAStartup (WS2_32.115)
1084 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
1086 TRACE("verReq=%x\n", wVersionRequested
);
1088 if (LOBYTE(wVersionRequested
) < 1)
1089 return WSAVERNOTSUPPORTED
;
1091 if (!lpWSAData
) return WSAEINVAL
;
1095 /* that's the whole of the negotiation for now */
1096 lpWSAData
->wVersion
= wVersionRequested
;
1097 /* return winsock information */
1098 lpWSAData
->wHighVersion
= 0x0202;
1099 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
1100 strcpy(lpWSAData
->szSystemStatus
, "Running" );
1101 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
1102 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
1103 /* don't do anything with lpWSAData->lpVendorInfo */
1104 /* (some apps don't allocate the space for this field) */
1106 TRACE("succeeded\n");
1111 /***********************************************************************
1112 * WSACleanup (WS2_32.116)
1114 INT WINAPI
WSACleanup(void)
1120 SetLastError(WSANOTINITIALISED
);
1121 return SOCKET_ERROR
;
1125 /***********************************************************************
1126 * WSAGetLastError (WS2_32.111)
1128 INT WINAPI
WSAGetLastError(void)
1130 return GetLastError();
1133 /***********************************************************************
1134 * WSASetLastError (WS2_32.112)
1136 void WINAPI
WSASetLastError(INT iError
) {
1137 SetLastError(iError
);
1140 static struct WS_hostent
*check_buffer_he(int size
)
1142 struct per_thread_data
* ptb
= get_per_thread_data();
1145 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
1146 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1148 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
1149 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
1150 return ptb
->he_buffer
;
1153 static struct WS_servent
*check_buffer_se(int size
)
1155 struct per_thread_data
* ptb
= get_per_thread_data();
1158 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
1159 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1161 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
1162 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
1163 return ptb
->se_buffer
;
1166 static struct WS_protoent
*check_buffer_pe(int size
)
1168 struct per_thread_data
* ptb
= get_per_thread_data();
1171 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
1172 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1174 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
1175 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
1176 return ptb
->pe_buffer
;
1179 /* ----------------------------------- i/o APIs */
1181 static inline BOOL
supported_pf(int pf
)
1202 /**********************************************************************/
1204 /* Returns the length of the converted address if successful, 0 if it was too small to
1207 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
1208 union generic_unix_sockaddr
*uaddr
)
1210 unsigned int uaddrlen
= 0;
1212 switch (wsaddr
->sa_family
)
1217 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
1218 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
1220 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
1223 uaddrlen
= sizeof(struct sockaddr_ipx
);
1224 memset( uaddr
, 0, uaddrlen
);
1225 uipx
->sipx_family
=AF_IPX
;
1226 uipx
->sipx_port
=wsipx
->sa_socket
;
1227 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
1230 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
1231 #ifdef IPX_FRAME_NONE
1232 uipx
->sipx_type
=IPX_FRAME_NONE
;
1238 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
1239 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
1241 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
1242 * scope_id, one without.
1244 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
1245 uaddrlen
= sizeof(struct sockaddr_in6
);
1246 memset( uaddr
, 0, uaddrlen
);
1247 uin6
->sin6_family
= AF_INET6
;
1248 uin6
->sin6_port
= win6
->sin6_port
;
1249 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
1250 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1251 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
1253 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1256 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
1260 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
1261 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
1263 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
1265 uaddrlen
= sizeof(struct sockaddr_in
);
1266 memset( uaddr
, 0, uaddrlen
);
1267 uin
->sin_family
= AF_INET
;
1268 uin
->sin_port
= win
->sin_port
;
1269 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
1274 struct sockaddr_irda
*uin
= (struct sockaddr_irda
*)uaddr
;
1275 const SOCKADDR_IRDA
*win
= (const SOCKADDR_IRDA
*)wsaddr
;
1277 if (wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1279 uaddrlen
= sizeof(struct sockaddr_irda
);
1280 memset( uaddr
, 0, uaddrlen
);
1281 uin
->sir_family
= AF_IRDA
;
1282 if (!strncmp( win
->irdaServiceName
, "LSAP-SEL", strlen( "LSAP-SEL" ) ))
1284 unsigned int lsap_sel
= 0;
1286 sscanf( win
->irdaServiceName
, "LSAP-SEL%u", &lsap_sel
);
1287 uin
->sir_lsap_sel
= lsap_sel
;
1291 uin
->sir_lsap_sel
= LSAP_ANY
;
1292 memcpy( uin
->sir_name
, win
->irdaServiceName
, 25 );
1294 memcpy( &uin
->sir_addr
, win
->irdaDeviceID
, sizeof(uin
->sir_addr
) );
1298 case WS_AF_UNSPEC
: {
1299 /* Try to determine the needed space by the passed windows sockaddr space */
1300 switch (wsaddrlen
) {
1301 default: /* likely a ipv4 address */
1302 case sizeof(struct WS_sockaddr_in
):
1303 uaddrlen
= sizeof(struct sockaddr_in
);
1306 case sizeof(struct WS_sockaddr_ipx
):
1307 uaddrlen
= sizeof(struct sockaddr_ipx
);
1311 case sizeof(SOCKADDR_IRDA
):
1312 uaddrlen
= sizeof(struct sockaddr_irda
);
1315 case sizeof(struct WS_sockaddr_in6
):
1316 case sizeof(struct WS_sockaddr_in6_old
):
1317 uaddrlen
= sizeof(struct sockaddr_in6
);
1320 memset( uaddr
, 0, uaddrlen
);
1324 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
1330 static BOOL
is_sockaddr_bound(const struct sockaddr
*uaddr
, int uaddrlen
)
1332 switch (uaddr
->sa_family
)
1336 FIXME("don't know how to tell if IPX socket is bound, assuming it is!\n");
1341 static const struct sockaddr_in6 emptyAddr
;
1342 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) uaddr
;
1343 return in6
->sin6_port
|| memcmp(&in6
->sin6_addr
, &emptyAddr
.sin6_addr
, sizeof(struct in6_addr
));
1347 static const struct sockaddr_in emptyAddr
;
1348 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) uaddr
;
1349 return in
->sin_port
|| memcmp(&in
->sin_addr
, &emptyAddr
.sin_addr
, sizeof(struct in_addr
));
1354 FIXME("unknown address family %d\n", uaddr
->sa_family
);
1359 /* Returns 0 if successful, -1 if the buffer is too small */
1360 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
1364 switch(uaddr
->sa_family
)
1369 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
1370 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
1373 switch (*wsaddrlen
) /* how much can we copy? */
1377 *wsaddrlen
= sizeof(*wsipx
);
1378 wsipx
->sa_socket
=uipx
->sipx_port
;
1382 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
1390 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
1396 wsipx
->sa_family
=WS_AF_IPX
;
1408 const struct sockaddr_irda
*uin
= (const struct sockaddr_irda
*)uaddr
;
1409 SOCKADDR_IRDA
*win
= (SOCKADDR_IRDA
*)wsaddr
;
1411 if (*wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1413 win
->irdaAddressFamily
= WS_AF_IRDA
;
1414 memcpy( win
->irdaDeviceID
, &uin
->sir_addr
, sizeof(win
->irdaDeviceID
) );
1415 if (uin
->sir_lsap_sel
!= LSAP_ANY
)
1416 sprintf( win
->irdaServiceName
, "LSAP-SEL%u", uin
->sir_lsap_sel
);
1418 memcpy( win
->irdaServiceName
, uin
->sir_name
,
1419 sizeof(win
->irdaServiceName
) );
1424 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
1425 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
1427 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
1429 win6old
->sin6_family
= WS_AF_INET6
;
1430 win6old
->sin6_port
= uin6
->sin6_port
;
1431 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
1432 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1433 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1434 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
1435 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
1436 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
1437 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
1440 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1442 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1447 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
1448 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
1450 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
1452 win
->sin_family
= WS_AF_INET
;
1453 win
->sin_port
= uin
->sin_port
;
1454 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
1455 memset(win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
1456 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
1460 memset(wsaddr
,0,*wsaddrlen
);
1464 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
1470 /**************************************************************************
1471 * Functions for handling overlapped I/O
1472 **************************************************************************/
1474 /* user APC called upon async completion */
1475 static void WINAPI
ws2_async_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1477 ws2_async
*wsa
= arg
;
1479 if (wsa
->completion_func
) wsa
->completion_func( NtStatusToWSAError(iosb
->u
.Status
),
1480 iosb
->Information
, wsa
->user_overlapped
,
1482 HeapFree( GetProcessHeap(), 0, wsa
);
1485 /***********************************************************************
1486 * WS2_recv (INTERNAL)
1488 * Workhorse for both synchronous and asynchronous recv() operations.
1490 static int WS2_recv( int fd
, struct ws2_async
*wsa
)
1492 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1496 union generic_unix_sockaddr unix_sockaddr
;
1499 hdr
.msg_name
= NULL
;
1503 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
1504 hdr
.msg_name
= &unix_sockaddr
;
1507 hdr
.msg_namelen
= 0;
1509 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1510 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1511 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1512 hdr
.msg_accrights
= NULL
;
1513 hdr
.msg_accrightslen
= 0;
1515 hdr
.msg_control
= pktbuf
;
1516 hdr
.msg_controllen
= sizeof(pktbuf
);
1520 if ( (n
= recvmsg(fd
, &hdr
, wsa
->flags
)) == -1 )
1523 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1526 ERR("Message control headers cannot be properly supported on this system.\n");
1527 wsa
->control
->len
= 0;
1530 if (wsa
->control
&& !convert_control_headers(&hdr
, wsa
->control
))
1532 WARN("Application passed insufficient room for control headers.\n");
1533 *wsa
->lpFlags
|= WS_MSG_CTRUNC
;
1539 /* if this socket is connected and lpFrom is not NULL, Linux doesn't give us
1540 * msg_name and msg_namelen from recvmsg, but it does set msg_namelen to zero.
1542 * quoting linux 2.6 net/ipv4/tcp.c:
1543 * "According to UNIX98, msg_name/msg_namelen are ignored
1544 * on connected socket. I was just happy when found this 8) --ANK"
1546 * likewise MSDN says that lpFrom and lpFromlen are ignored for
1547 * connection-oriented sockets, so don't try to update lpFrom.
1549 if (wsa
->addr
&& hdr
.msg_namelen
)
1550 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, wsa
->addr
, wsa
->addrlen
.ptr
);
1555 /***********************************************************************
1556 * WS2_async_recv (INTERNAL)
1558 * Handler for overlapped recv() operations.
1560 static NTSTATUS
WS2_async_recv( void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
1562 ws2_async
* wsa
= user
;
1567 case STATUS_ALERTED
:
1568 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
1571 result
= WS2_recv( fd
, wsa
);
1572 wine_server_release_fd( wsa
->hSocket
, fd
);
1575 status
= STATUS_SUCCESS
;
1576 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1580 if (errno
== EINTR
|| errno
== EAGAIN
)
1582 status
= STATUS_PENDING
;
1583 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1588 status
= wsaErrStatus();
1593 if (status
!= STATUS_PENDING
)
1595 iosb
->u
.Status
= status
;
1596 iosb
->Information
= result
;
1597 *apc
= ws2_async_apc
;
1602 /* user APC called upon async accept completion */
1603 static void WINAPI
ws2_async_accept_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1605 struct ws2_accept_async
*wsa
= arg
;
1607 HeapFree( GetProcessHeap(), 0, wsa
->read
);
1608 HeapFree( GetProcessHeap(), 0, wsa
);
1611 /***********************************************************************
1612 * WS2_async_accept_recv (INTERNAL)
1614 * This function is used to finish the read part of an accept request. It is
1615 * needed to place the completion on the correct socket (listener).
1617 static NTSTATUS
WS2_async_accept_recv( void *arg
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
, void **apc
)
1620 struct ws2_accept_async
*wsa
= arg
;
1622 status
= WS2_async_recv( wsa
->read
, iosb
, status
, &junk
);
1623 if (status
== STATUS_PENDING
)
1626 if (wsa
->user_overlapped
->hEvent
)
1627 SetEvent(wsa
->user_overlapped
->hEvent
);
1629 WS_AddCompletion( HANDLE2SOCKET(wsa
->listen_socket
), wsa
->cvalue
, iosb
->u
.Status
, iosb
->Information
);
1631 *apc
= ws2_async_accept_apc
;
1635 /***********************************************************************
1636 * WS2_async_accept (INTERNAL)
1638 * This is the function called to satisfy the AcceptEx callback
1640 static NTSTATUS
WS2_async_accept( void *arg
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
, void **apc
)
1642 struct ws2_accept_async
*wsa
= arg
;
1646 TRACE("status: 0x%x listen: %p, accept: %p\n", status
, wsa
->listen_socket
, wsa
->accept_socket
);
1648 if (status
== STATUS_ALERTED
)
1650 SERVER_START_REQ( accept_into_socket
)
1652 req
->lhandle
= wine_server_obj_handle( wsa
->listen_socket
);
1653 req
->ahandle
= wine_server_obj_handle( wsa
->accept_socket
);
1654 status
= wine_server_call( req
);
1658 if (status
== STATUS_CANT_WAIT
)
1659 return STATUS_PENDING
;
1661 if (status
== STATUS_INVALID_HANDLE
)
1663 FIXME("AcceptEx accepting socket closed but request was not cancelled\n");
1664 status
= STATUS_CANCELLED
;
1667 else if (status
== STATUS_HANDLES_CLOSED
)
1668 status
= STATUS_CANCELLED
; /* strange windows behavior */
1670 if (status
!= STATUS_SUCCESS
)
1673 /* WS2 Spec says size param is extra 16 bytes long...what do we put in it? */
1674 addr
= ((char *)wsa
->buf
) + wsa
->data_len
;
1675 len
= wsa
->local_len
- sizeof(int);
1676 WS_getsockname(HANDLE2SOCKET(wsa
->accept_socket
),
1677 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
1680 addr
+= wsa
->local_len
;
1681 len
= wsa
->remote_len
- sizeof(int);
1682 WS_getpeername(HANDLE2SOCKET(wsa
->accept_socket
),
1683 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
1689 SERVER_START_REQ( register_async
)
1691 req
->type
= ASYNC_TYPE_READ
;
1692 req
->async
.handle
= wine_server_obj_handle( wsa
->accept_socket
);
1693 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept_recv
);
1694 req
->async
.iosb
= wine_server_client_ptr( iosb
);
1695 req
->async
.arg
= wine_server_client_ptr( wsa
);
1696 status
= wine_server_call( req
);
1700 if (status
!= STATUS_PENDING
)
1703 return STATUS_SUCCESS
;
1706 iosb
->u
.Status
= status
;
1707 iosb
->Information
= 0;
1709 if (wsa
->user_overlapped
->hEvent
)
1710 SetEvent(wsa
->user_overlapped
->hEvent
);
1712 WS_AddCompletion( HANDLE2SOCKET(wsa
->listen_socket
), wsa
->cvalue
, iosb
->u
.Status
, iosb
->Information
);
1714 *apc
= ws2_async_accept_apc
;
1718 /***********************************************************************
1719 * WS2_send (INTERNAL)
1721 * Workhorse for both synchronous and asynchronous send() operations.
1723 static int WS2_send( int fd
, struct ws2_async
*wsa
)
1726 union generic_unix_sockaddr unix_addr
;
1729 hdr
.msg_name
= NULL
;
1730 hdr
.msg_namelen
= 0;
1734 hdr
.msg_name
= &unix_addr
;
1735 hdr
.msg_namelen
= ws_sockaddr_ws2u( wsa
->addr
, wsa
->addrlen
.val
, &unix_addr
);
1736 if ( !hdr
.msg_namelen
)
1742 #if defined(HAVE_IPX) && defined(SOL_IPX)
1743 if(wsa
->addr
->sa_family
== WS_AF_IPX
)
1745 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
1747 unsigned int len
=sizeof(int);
1749 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1750 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1751 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1752 * ipx type in the sockaddr_opx structure with the stored value.
1754 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
1755 uipx
->sipx_type
= val
;
1760 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1761 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1762 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1763 hdr
.msg_accrights
= NULL
;
1764 hdr
.msg_accrightslen
= 0;
1766 hdr
.msg_control
= NULL
;
1767 hdr
.msg_controllen
= 0;
1771 ret
= sendmsg(fd
, &hdr
, wsa
->flags
);
1775 while (wsa
->first_iovec
< wsa
->n_iovecs
&& wsa
->iovec
[wsa
->first_iovec
].iov_len
<= n
)
1776 n
-= wsa
->iovec
[wsa
->first_iovec
++].iov_len
;
1777 if (wsa
->first_iovec
< wsa
->n_iovecs
)
1779 wsa
->iovec
[wsa
->first_iovec
].iov_base
= (char*)wsa
->iovec
[wsa
->first_iovec
].iov_base
+ n
;
1780 wsa
->iovec
[wsa
->first_iovec
].iov_len
-= n
;
1786 /***********************************************************************
1787 * WS2_async_send (INTERNAL)
1789 * Handler for overlapped send() operations.
1791 static NTSTATUS
WS2_async_send(void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
1793 ws2_async
* wsa
= user
;
1798 case STATUS_ALERTED
:
1799 if ( wsa
->n_iovecs
<= wsa
->first_iovec
)
1802 status
= STATUS_SUCCESS
;
1805 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
1808 /* check to see if the data is ready (non-blocking) */
1809 result
= WS2_send( fd
, wsa
);
1810 wine_server_release_fd( wsa
->hSocket
, fd
);
1814 if (wsa
->first_iovec
< wsa
->n_iovecs
)
1815 status
= STATUS_PENDING
;
1817 status
= STATUS_SUCCESS
;
1819 iosb
->Information
+= result
;
1821 else if (errno
== EINTR
|| errno
== EAGAIN
)
1823 status
= STATUS_PENDING
;
1827 status
= wsaErrStatus();
1831 if (status
!= STATUS_PENDING
)
1833 iosb
->u
.Status
= status
;
1834 *apc
= ws2_async_apc
;
1839 /***********************************************************************
1840 * WS2_async_shutdown (INTERNAL)
1842 * Handler for shutdown() operations on overlapped sockets.
1844 static NTSTATUS
WS2_async_shutdown( void* user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
, void **apc
)
1846 ws2_async
* wsa
= user
;
1851 case STATUS_ALERTED
:
1852 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
1855 switch ( wsa
->type
)
1857 case ASYNC_TYPE_READ
: err
= shutdown( fd
, 0 ); break;
1858 case ASYNC_TYPE_WRITE
: err
= shutdown( fd
, 1 ); break;
1860 status
= err
? wsaErrStatus() : STATUS_SUCCESS
;
1861 wine_server_release_fd( wsa
->hSocket
, fd
);
1864 iosb
->u
.Status
= status
;
1865 iosb
->Information
= 0;
1866 *apc
= ws2_async_apc
;
1870 /***********************************************************************
1871 * WS2_register_async_shutdown (INTERNAL)
1873 * Helper function for WS_shutdown() on overlapped sockets.
1875 static int WS2_register_async_shutdown( SOCKET s
, int type
)
1877 struct ws2_async
*wsa
;
1880 TRACE("s %ld type %d\n", s
, type
);
1882 wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
1886 wsa
->hSocket
= SOCKET2HANDLE(s
);
1888 wsa
->completion_func
= NULL
;
1890 SERVER_START_REQ( register_async
)
1893 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
1894 req
->async
.callback
= wine_server_client_ptr( WS2_async_shutdown
);
1895 req
->async
.iosb
= wine_server_client_ptr( &wsa
->local_iosb
);
1896 req
->async
.arg
= wine_server_client_ptr( wsa
);
1897 req
->async
.cvalue
= 0;
1898 status
= wine_server_call( req
);
1902 if (status
!= STATUS_PENDING
)
1904 HeapFree( GetProcessHeap(), 0, wsa
);
1905 return NtStatusToWSAError( status
);
1910 /***********************************************************************
1913 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
,
1920 TRACE("socket %04lx\n", s
);
1921 is_blocking
= _is_blocking(s
);
1924 /* try accepting first (if there is a deferred connection) */
1925 SERVER_START_REQ( accept_socket
)
1927 req
->lhandle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1928 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
1929 req
->attributes
= OBJ_INHERIT
;
1930 status
= wine_server_call( req
);
1931 as
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
1936 if (addr
) WS_getpeername(as
, addr
, addrlen32
);
1939 if (is_blocking
&& status
== STATUS_CANT_WAIT
)
1941 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1943 do_block(fd
, POLLIN
, -1);
1944 _sync_sock_state(s
); /* let wineserver notice connection */
1945 release_sock_fd( s
, fd
);
1947 } while (is_blocking
&& status
== STATUS_CANT_WAIT
);
1950 return INVALID_SOCKET
;
1953 /***********************************************************************
1956 static BOOL WINAPI
WS2_AcceptEx(SOCKET listener
, SOCKET acceptor
, PVOID dest
, DWORD dest_len
,
1957 DWORD local_addr_len
, DWORD rem_addr_len
, LPDWORD received
,
1958 LPOVERLAPPED overlapped
)
1961 struct ws2_accept_async
*wsa
;
1963 ULONG_PTR cvalue
= (overlapped
&& ((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)overlapped
: 0;
1965 TRACE("(%lx, %lx, %p, %d, %d, %d, %p, %p)\n", listener
, acceptor
, dest
, dest_len
, local_addr_len
,
1966 rem_addr_len
, received
, overlapped
);
1970 SetLastError(WSAEINVAL
);
1976 SetLastError(WSA_INVALID_PARAMETER
);
1980 fd
= get_sock_fd( listener
, FILE_READ_DATA
, NULL
);
1983 SetLastError(WSAENOTSOCK
);
1986 release_sock_fd( listener
, fd
);
1988 fd
= get_sock_fd( acceptor
, FILE_READ_DATA
, NULL
);
1991 SetLastError(WSAEINVAL
);
1994 release_sock_fd( acceptor
, fd
);
1996 wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
1999 SetLastError(WSAEFAULT
);
2003 wsa
->listen_socket
= SOCKET2HANDLE(listener
);
2004 wsa
->accept_socket
= SOCKET2HANDLE(acceptor
);
2005 wsa
->user_overlapped
= overlapped
;
2006 wsa
->cvalue
= cvalue
;
2008 wsa
->data_len
= dest_len
;
2009 wsa
->local_len
= local_addr_len
;
2010 wsa
->remote_len
= rem_addr_len
;
2015 /* set up a read request if we need it */
2016 wsa
->read
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[1]) );
2019 HeapFree( GetProcessHeap(), 0, wsa
);
2020 SetLastError(WSAEFAULT
);
2024 wsa
->read
->hSocket
= wsa
->accept_socket
;
2025 wsa
->read
->flags
= 0;
2026 wsa
->read
->lpFlags
= &wsa
->read
->flags
;
2027 wsa
->read
->addr
= NULL
;
2028 wsa
->read
->addrlen
.ptr
= NULL
;
2029 wsa
->read
->control
= NULL
;
2030 wsa
->read
->n_iovecs
= 1;
2031 wsa
->read
->first_iovec
= 0;
2032 wsa
->read
->iovec
[0].iov_base
= wsa
->buf
;
2033 wsa
->read
->iovec
[0].iov_len
= wsa
->data_len
;
2036 SERVER_START_REQ( register_async
)
2038 req
->type
= ASYNC_TYPE_READ
;
2039 req
->async
.handle
= wine_server_obj_handle( SOCKET2HANDLE(listener
) );
2040 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept
);
2041 req
->async
.iosb
= wine_server_client_ptr( overlapped
);
2042 req
->async
.arg
= wine_server_client_ptr( wsa
);
2043 /* We don't set event or completion since we may also have to read */
2044 status
= wine_server_call( req
);
2048 if(status
!= STATUS_PENDING
)
2050 HeapFree( GetProcessHeap(), 0, wsa
->read
);
2051 HeapFree( GetProcessHeap(), 0, wsa
);
2054 SetLastError( NtStatusToWSAError(status
) );
2058 /***********************************************************************
2059 * GetAcceptExSockaddrs
2061 static void WINAPI
WS2_GetAcceptExSockaddrs(PVOID buffer
, DWORD data_size
, DWORD local_size
, DWORD remote_size
,
2062 struct WS_sockaddr
**local_addr
, LPINT local_addr_len
,
2063 struct WS_sockaddr
**remote_addr
, LPINT remote_addr_len
)
2065 char *cbuf
= buffer
;
2066 TRACE("(%p, %d, %d, %d, %p, %p, %p, %p)\n", buffer
, data_size
, local_size
, remote_size
, local_addr
,
2067 local_addr_len
, remote_addr
, remote_addr_len
);
2070 *local_addr_len
= *(int *) cbuf
;
2071 *local_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
2075 *remote_addr_len
= *(int *) cbuf
;
2076 *remote_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
2079 /***********************************************************************
2082 * Perform a receive operation that is capable of returning message
2083 * control headers. It is important to note that the WSAMSG parameter
2084 * must remain valid throughout the operation, even when an overlapped
2085 * receive is performed.
2087 static int WINAPI
WS2_WSARecvMsg( SOCKET s
, LPWSAMSG msg
, LPDWORD lpNumberOfBytesRecvd
,
2088 LPWSAOVERLAPPED lpOverlapped
,
2089 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2093 SetLastError( WSAEFAULT
);
2094 return SOCKET_ERROR
;
2097 return WS2_recv_base( s
, msg
->lpBuffers
, msg
->dwBufferCount
, lpNumberOfBytesRecvd
,
2098 &msg
->dwFlags
, msg
->name
, &msg
->namelen
,
2099 lpOverlapped
, lpCompletionRoutine
, &msg
->Control
);
2102 /***********************************************************************
2105 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
2107 int fd
= get_sock_fd( s
, 0, NULL
);
2108 int res
= SOCKET_ERROR
;
2110 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
2114 if (!name
|| (name
->sa_family
&& !supported_pf(name
->sa_family
)))
2116 SetLastError(WSAEAFNOSUPPORT
);
2120 union generic_unix_sockaddr uaddr
;
2121 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
2124 SetLastError(WSAEFAULT
);
2129 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) &uaddr
;
2130 if (name
->sa_family
== WS_AF_INET6
&&
2131 !memcmp(&in6
->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
)))
2134 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
)) == -1)
2136 release_sock_fd( s
, fd
);
2137 SetLastError(WSAEAFNOSUPPORT
);
2138 return SOCKET_ERROR
;
2142 if (name
->sa_family
== WS_AF_INET
)
2144 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
2145 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
2147 /* Trying to bind to the default host interface, using
2148 * INADDR_ANY instead*/
2149 WARN("Trying to bind to magic IP address, using "
2150 "INADDR_ANY instead.\n");
2151 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_ANY
);
2154 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
2156 int loc_errno
= errno
;
2157 WARN("\tfailure - errno = %i\n", errno
);
2162 SetLastError(WSAENOTSOCK
);
2165 SetLastError(WSAEINVAL
);
2168 SetLastError(wsaErrno());
2174 res
=0; /* success */
2178 release_sock_fd( s
, fd
);
2183 /***********************************************************************
2184 * closesocket (WS2_32.3)
2186 int WINAPI
WS_closesocket(SOCKET s
)
2188 TRACE("socket %04lx\n", s
);
2189 if (CloseHandle(SOCKET2HANDLE(s
))) return 0;
2190 return SOCKET_ERROR
;
2193 static int do_connect(int fd
, const struct WS_sockaddr
* name
, int namelen
)
2195 union generic_unix_sockaddr uaddr
;
2196 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
2201 if (name
->sa_family
== WS_AF_INET
)
2203 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
2204 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
2206 /* Trying to connect to magic replace-loopback address,
2207 * assuming we really want to connect to localhost */
2208 TRACE("Trying to connect to magic IP address, using "
2209 "INADDR_LOOPBACK instead.\n");
2210 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_LOOPBACK
);
2214 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
2220 /***********************************************************************
2221 * connect (WS2_32.4)
2223 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
2225 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2227 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
2231 int ret
= do_connect(fd
, name
, namelen
);
2233 goto connect_success
;
2235 if (ret
== WSAEINPROGRESS
)
2237 /* tell wineserver that a connection is in progress */
2238 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2240 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
2241 if (_is_blocking(s
))
2245 do_block(fd
, POLLIN
| POLLOUT
, -1);
2246 _sync_sock_state(s
); /* let wineserver notice connection */
2247 /* retrieve any error codes from it */
2248 result
= _get_sock_error(s
, FD_CONNECT_BIT
);
2250 SetLastError(NtStatusToWSAError(result
));
2253 goto connect_success
;
2258 SetLastError(WSAEWOULDBLOCK
);
2265 release_sock_fd( s
, fd
);
2267 return SOCKET_ERROR
;
2270 release_sock_fd( s
, fd
);
2271 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2272 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
2273 FD_CONNECT
|FD_WINE_LISTENING
);
2277 /***********************************************************************
2278 * WSAConnect (WS2_32.30)
2280 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
2281 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
2282 LPQOS lpSQOS
, LPQOS lpGQOS
)
2284 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
2285 FIXME("unsupported parameters!\n");
2286 return WS_connect( s
, name
, namelen
);
2289 /***********************************************************************
2292 static BOOL WINAPI
WS2_ConnectEx(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
2293 PVOID sendBuf
, DWORD sendBufLen
, LPDWORD sent
, LPOVERLAPPED ov
)
2295 int fd
, ret
, status
;
2299 SetLastError( ERROR_INVALID_PARAMETER
);
2303 fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2306 SetLastError( WSAENOTSOCK
);
2310 TRACE("socket %04lx, ptr %p %s, length %d, sendptr %p, len %d, ov %p\n",
2311 s
, name
, debugstr_sockaddr(name
), namelen
, sendBuf
, sendBufLen
, ov
);
2313 /* FIXME: technically the socket has to be bound */
2314 ret
= do_connect(fd
, name
, namelen
);
2319 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2320 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
2321 FD_CONNECT
|FD_WINE_LISTENING
);
2323 wsabuf
.len
= sendBufLen
;
2324 wsabuf
.buf
= (char*) sendBuf
;
2326 /* WSASend takes care of completion if need be */
2327 if (WSASend(s
, &wsabuf
, sendBuf
? 1 : 0, sent
, 0, ov
, NULL
) != SOCKET_ERROR
)
2328 goto connection_success
;
2330 else if (ret
== WSAEINPROGRESS
)
2332 struct ws2_async
*wsa
;
2333 ULONG_PTR cvalue
= (((ULONG_PTR
)ov
->hEvent
& 1) == 0) ? (ULONG_PTR
)ov
: 0;
2335 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2337 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
2339 /* Indirectly call WSASend */
2340 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) )))
2342 SetLastError(WSAEFAULT
);
2346 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)ov
;
2347 iosb
->u
.Status
= STATUS_PENDING
;
2348 iosb
->Information
= 0;
2350 wsa
->hSocket
= SOCKET2HANDLE(s
);
2352 wsa
->addrlen
.val
= 0;
2354 wsa
->lpFlags
= &wsa
->flags
;
2355 wsa
->control
= NULL
;
2356 wsa
->n_iovecs
= sendBuf
? 1 : 0;
2357 wsa
->first_iovec
= 0;
2358 wsa
->completion_func
= NULL
;
2359 wsa
->iovec
[0].iov_base
= sendBuf
;
2360 wsa
->iovec
[0].iov_len
= sendBufLen
;
2362 SERVER_START_REQ( register_async
)
2364 req
->type
= ASYNC_TYPE_WRITE
;
2365 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
2366 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
2367 req
->async
.iosb
= wine_server_client_ptr( iosb
);
2368 req
->async
.arg
= wine_server_client_ptr( wsa
);
2369 req
->async
.event
= wine_server_obj_handle( ov
->hEvent
);
2370 req
->async
.cvalue
= cvalue
;
2371 status
= wine_server_call( req
);
2375 if (status
!= STATUS_PENDING
) HeapFree(GetProcessHeap(), 0, wsa
);
2377 /* If the connect already failed */
2378 if (status
== STATUS_PIPE_DISCONNECTED
)
2379 status
= _get_sock_error(s
, FD_CONNECT_BIT
);
2380 SetLastError( NtStatusToWSAError(status
) );
2388 release_sock_fd( s
, fd
);
2392 release_sock_fd( s
, fd
);
2397 /***********************************************************************
2398 * getpeername (WS2_32.5)
2400 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
2405 TRACE("socket: %04lx, ptr %p, len %08x\n", s
, name
, namelen
?*namelen
:0);
2407 fd
= get_sock_fd( s
, 0, NULL
);
2412 union generic_unix_sockaddr uaddr
;
2413 unsigned int uaddrlen
= sizeof(uaddr
);
2415 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) == 0)
2417 if (!name
|| !namelen
)
2418 SetLastError(WSAEFAULT
);
2419 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
2420 /* The buffer was too small */
2421 SetLastError(WSAEFAULT
);
2426 SetLastError(wsaErrno());
2427 release_sock_fd( s
, fd
);
2432 /***********************************************************************
2433 * getsockname (WS2_32.6)
2435 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
2440 TRACE("socket: %04lx, ptr %p, len %8x\n", s
, name
, *namelen
);
2442 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
2443 if( (name
== NULL
) || (namelen
== NULL
) )
2445 SetLastError( WSAEFAULT
);
2446 return SOCKET_ERROR
;
2449 fd
= get_sock_fd( s
, 0, NULL
);
2454 union generic_unix_sockaddr uaddr
;
2455 unsigned int uaddrlen
= sizeof(uaddr
);
2457 if (getsockname(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
2459 SetLastError(wsaErrno());
2461 else if (!is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
2463 SetLastError(WSAEINVAL
);
2465 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
2467 /* The buffer was too small */
2468 SetLastError(WSAEFAULT
);
2474 release_sock_fd( s
, fd
);
2479 /***********************************************************************
2480 * getsockopt (WS2_32.7)
2482 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
2483 INT optname
, char *optval
, INT
*optlen
)
2488 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
2489 s
, level
, optname
, optval
, *optlen
);
2497 /* Handle common cases. The special cases are below, sorted
2499 case WS_SO_ACCEPTCONN
:
2500 case WS_SO_BROADCAST
:
2503 case WS_SO_KEEPALIVE
:
2504 case WS_SO_OOBINLINE
:
2506 case WS_SO_REUSEADDR
:
2509 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2510 return SOCKET_ERROR
;
2511 convert_sockopt(&level
, &optname
);
2512 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2514 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2517 release_sock_fd( s
, fd
);
2520 case WS_SO_DONTLINGER
:
2522 struct linger lingval
;
2523 unsigned int len
= sizeof(struct linger
);
2525 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
2527 SetLastError(WSAEFAULT
);
2528 return SOCKET_ERROR
;
2530 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2531 return SOCKET_ERROR
;
2533 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
2535 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2540 *(BOOL
*)optval
= (lingval
.l_onoff
) ? FALSE
: TRUE
;
2541 *optlen
= sizeof(BOOL
);
2544 release_sock_fd( s
, fd
);
2548 case WS_SO_CONNECT_TIME
:
2550 static int pretendtime
= 0;
2551 struct WS_sockaddr addr
;
2552 int len
= sizeof(addr
);
2554 if (!optlen
|| *optlen
< sizeof(DWORD
) || !optval
)
2556 SetLastError(WSAEFAULT
);
2557 return SOCKET_ERROR
;
2559 if (WS_getpeername(s
, &addr
, &len
) == SOCKET_ERROR
)
2560 *(DWORD
*)optval
= ~0u;
2563 if (!pretendtime
) FIXME("WS_SO_CONNECT_TIME - faking results\n");
2564 *(DWORD
*)optval
= pretendtime
++;
2566 *optlen
= sizeof(DWORD
);
2569 /* As mentioned in setsockopt, Windows ignores this, so we
2570 * always return true here */
2571 case WS_SO_DONTROUTE
:
2572 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
2574 SetLastError(WSAEFAULT
);
2575 return SOCKET_ERROR
;
2577 *(BOOL
*)optval
= TRUE
;
2578 *optlen
= sizeof(BOOL
);
2583 struct linger lingval
;
2585 unsigned int len
= sizeof(struct linger
), slen
= sizeof(int);
2587 /* struct linger and LINGER have different sizes */
2588 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
2590 SetLastError(WSAEFAULT
);
2591 return SOCKET_ERROR
;
2593 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2594 return SOCKET_ERROR
;
2596 if ((getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &so_type
, &slen
) == 0 && so_type
== SOCK_DGRAM
))
2598 SetLastError(WSAENOPROTOOPT
);
2601 else if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0)
2603 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2608 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
2609 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
2610 *optlen
= sizeof(struct linger
);
2613 release_sock_fd( s
, fd
);
2617 case WS_SO_MAX_MSG_SIZE
:
2618 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
2620 SetLastError(WSAEFAULT
);
2621 return SOCKET_ERROR
;
2623 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
2624 *(int *)optval
= 65507;
2625 *optlen
= sizeof(int);
2628 /* SO_OPENTYPE does not require a valid socket handle. */
2629 case WS_SO_OPENTYPE
:
2630 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
2632 SetLastError(WSAEFAULT
);
2633 return SOCKET_ERROR
;
2635 *(int *)optval
= get_per_thread_data()->opentype
;
2636 *optlen
= sizeof(int);
2637 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
2641 case WS_SO_RCVTIMEO
:
2644 case WS_SO_SNDTIMEO
:
2646 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
2649 unsigned int len
= sizeof(struct timeval
);
2651 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
2653 SetLastError(WSAEFAULT
);
2654 return SOCKET_ERROR
;
2656 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2657 return SOCKET_ERROR
;
2659 convert_sockopt(&level
, &optname
);
2660 if (getsockopt(fd
, level
, optname
, &tv
, &len
) != 0 )
2662 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2667 *(int *)optval
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
2668 *optlen
= sizeof(int);
2671 release_sock_fd( s
, fd
);
2676 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
2677 SetLastError(WSAENOPROTOOPT
);
2678 return SOCKET_ERROR
;
2679 } /* end switch(optname) */
2680 }/* end case WS_SOL_SOCKET */
2684 struct WS_sockaddr_ipx addr
;
2685 IPX_ADDRESS_DATA
*data
;
2690 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
2692 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (unsigned int*)optlen
) == -1)
2699 socklen_t len
=sizeof(struct ipx
);
2700 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
2703 *optval
= (int)val
.ipx_pt
;
2706 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
2707 release_sock_fd( s
, fd
);
2712 * On a Win2000 system with one network card there are usually
2713 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
2714 * Using this call you can then retrieve info about this all.
2715 * In case of Linux it is a bit different. Usually you have
2716 * only "one" device active and further it is not possible to
2717 * query things like the linkspeed.
2719 FIXME("IPX_ADDRESS\n");
2720 namelen
= sizeof(struct WS_sockaddr_ipx
);
2721 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
2722 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
2724 data
= (IPX_ADDRESS_DATA
*)optval
;
2725 memcpy(data
->nodenum
,addr
.sa_nodenum
,sizeof(data
->nodenum
));
2726 memcpy(data
->netnum
,addr
.sa_netnum
,sizeof(data
->netnum
));
2727 data
->adapternum
= 0;
2728 data
->wan
= FALSE
; /* We are not on a wan for now .. */
2729 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
2730 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
2731 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
2732 * note 1MB = 1000kB in this case */
2735 case IPX_MAX_ADAPTER_NUM
:
2736 FIXME("IPX_MAX_ADAPTER_NUM\n");
2737 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
2741 FIXME("IPX optname:%x\n", optname
);
2742 return SOCKET_ERROR
;
2743 }/* end switch(optname) */
2744 } /* end case NSPROTO_IPX */
2751 case WS_IRLMP_ENUMDEVICES
:
2753 static const int MAX_IRDA_DEVICES
= 10;
2754 char buf
[sizeof(struct irda_device_list
) +
2755 (MAX_IRDA_DEVICES
- 1) * sizeof(struct irda_device_info
)];
2757 socklen_t len
= sizeof(buf
);
2759 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2760 return SOCKET_ERROR
;
2761 res
= getsockopt( fd
, SOL_IRLMP
, IRLMP_ENUMDEVICES
, buf
, &len
);
2764 SetLastError(wsaErrno());
2765 return SOCKET_ERROR
;
2769 struct irda_device_list
*src
= (struct irda_device_list
*)buf
;
2770 DEVICELIST
*dst
= (DEVICELIST
*)optval
;
2771 INT needed
= sizeof(DEVICELIST
), i
;
2774 needed
+= (src
->len
- 1) * sizeof(IRDA_DEVICE_INFO
);
2775 if (*optlen
< needed
)
2777 SetLastError(WSAEFAULT
);
2778 return SOCKET_ERROR
;
2781 TRACE("IRLMP_ENUMDEVICES: %d devices found:\n", src
->len
);
2782 dst
->numDevice
= src
->len
;
2783 for (i
= 0; i
< src
->len
; i
++)
2785 TRACE("saddr = %08x, daddr = %08x, info = %s, hints = %02x%02x\n",
2786 src
->dev
[i
].saddr
, src
->dev
[i
].daddr
,
2787 src
->dev
[i
].info
, src
->dev
[i
].hints
[0],
2788 src
->dev
[i
].hints
[1]);
2789 memcpy( dst
->Device
[i
].irdaDeviceID
,
2791 sizeof(dst
->Device
[i
].irdaDeviceID
) ) ;
2792 memcpy( dst
->Device
[i
].irdaDeviceName
,
2794 sizeof(dst
->Device
[i
].irdaDeviceName
) ) ;
2795 memcpy( &dst
->Device
[i
].irdaDeviceHints1
,
2796 &src
->dev
[i
].hints
[0],
2797 sizeof(dst
->Device
[i
].irdaDeviceHints1
) ) ;
2798 memcpy( &dst
->Device
[i
].irdaDeviceHints2
,
2799 &src
->dev
[i
].hints
[1],
2800 sizeof(dst
->Device
[i
].irdaDeviceHints2
) ) ;
2801 dst
->Device
[i
].irdaCharSet
= src
->dev
[i
].charset
;
2807 FIXME("IrDA optname:0x%x\n", optname
);
2808 return SOCKET_ERROR
;
2810 break; /* case WS_SOL_IRLMP */
2813 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
2814 case WS_IPPROTO_TCP
:
2817 case WS_TCP_NODELAY
:
2818 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2819 return SOCKET_ERROR
;
2820 convert_sockopt(&level
, &optname
);
2821 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2823 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2826 release_sock_fd( s
, fd
);
2829 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
2830 return SOCKET_ERROR
;
2835 case WS_IP_ADD_MEMBERSHIP
:
2836 case WS_IP_DROP_MEMBERSHIP
:
2840 case WS_IP_MULTICAST_IF
:
2841 case WS_IP_MULTICAST_LOOP
:
2842 case WS_IP_MULTICAST_TTL
:
2849 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2850 return SOCKET_ERROR
;
2851 convert_sockopt(&level
, &optname
);
2852 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2854 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2857 release_sock_fd( s
, fd
);
2859 case WS_IP_DONTFRAGMENT
:
2860 FIXME("WS_IP_DONTFRAGMENT is always false!\n");
2861 *(BOOL
*)optval
= FALSE
;
2864 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
2865 return SOCKET_ERROR
;
2867 case WS_IPPROTO_IPV6
:
2870 #ifdef IPV6_ADD_MEMBERSHIP
2871 case WS_IPV6_ADD_MEMBERSHIP
:
2873 #ifdef IPV6_DROP_MEMBERSHIP
2874 case WS_IPV6_DROP_MEMBERSHIP
:
2876 case WS_IPV6_MULTICAST_IF
:
2877 case WS_IPV6_MULTICAST_HOPS
:
2878 case WS_IPV6_MULTICAST_LOOP
:
2879 case WS_IPV6_UNICAST_HOPS
:
2880 case WS_IPV6_V6ONLY
:
2881 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2882 return SOCKET_ERROR
;
2883 convert_sockopt(&level
, &optname
);
2884 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2886 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2889 release_sock_fd( s
, fd
);
2891 case WS_IPV6_DONTFRAG
:
2892 FIXME("WS_IPV6_DONTFRAG is always false!\n");
2893 *(BOOL
*)optval
= FALSE
;
2896 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
2897 return SOCKET_ERROR
;
2900 WARN("Unknown level: 0x%08x\n", level
);
2901 SetLastError(WSAEINVAL
);
2902 return SOCKET_ERROR
;
2903 } /* end switch(level) */
2906 /***********************************************************************
2909 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
2911 return htonl(hostlong
);
2915 /***********************************************************************
2918 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
2920 return htons(hostshort
);
2923 /***********************************************************************
2924 * WSAHtonl (WS2_32.46)
2925 * From MSDN description of error codes, this function should also
2926 * check if WinSock has been initialized and the socket is a valid
2927 * socket. But why? This function only translates a host byte order
2928 * u_long into a network byte order u_long...
2930 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
2934 *lpnetlong
= htonl(hostlong
);
2937 WSASetLastError(WSAEFAULT
);
2938 return SOCKET_ERROR
;
2941 /***********************************************************************
2942 * WSAHtons (WS2_32.47)
2943 * From MSDN description of error codes, this function should also
2944 * check if WinSock has been initialized and the socket is a valid
2945 * socket. But why? This function only translates a host byte order
2946 * u_short into a network byte order u_short...
2948 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
2953 *lpnetshort
= htons(hostshort
);
2956 WSASetLastError(WSAEFAULT
);
2957 return SOCKET_ERROR
;
2961 /***********************************************************************
2962 * inet_addr (WS2_32.11)
2964 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
2966 if (!cp
) return INADDR_NONE
;
2967 return inet_addr(cp
);
2971 /***********************************************************************
2974 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
2976 return ntohl(netlong
);
2980 /***********************************************************************
2983 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
2985 return ntohs(netshort
);
2989 /***********************************************************************
2990 * inet_ntoa (WS2_32.12)
2992 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
2994 /* use "buffer for dummies" here because some applications have a
2995 * propensity to decode addresses in ws_hostent structure without
2996 * saving them first...
2998 static char dbuffer
[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
3000 char* s
= inet_ntoa(*((struct in_addr
*)&in
));
3006 SetLastError(wsaErrno());
3010 static const char *debugstr_wsaioctl(DWORD ioctl
)
3012 const char *buf_type
, *family
;
3014 switch(ioctl
& 0x18000000)
3019 case WS_IOC_PROTOCOL
:
3020 family
= "IOC_PROTOCOL";
3023 family
= "IOC_VENDOR";
3025 default: /* WS_IOC_UNIX */
3027 BYTE size
= (ioctl
>> 16) & WS_IOCPARM_MASK
;
3028 char x
= (ioctl
& 0xff00) >> 8;
3029 BYTE y
= ioctl
& 0xff;
3032 switch (ioctl
& (WS_IOC_VOID
|WS_IOC_INOUT
))
3036 sprintf(args
, "%d, %d", x
, y
);
3040 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3044 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3048 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3051 return wine_dbg_sprintf("%s(%s)", buf_type
, args
);
3055 /* We are different from WS_IOC_UNIX. */
3056 switch (ioctl
& (WS_IOC_VOID
|WS_IOC_INOUT
))
3059 buf_type
= "_WSAIO";
3062 buf_type
= "_WSAIORW";
3065 buf_type
= "_WSAIOW";
3068 buf_type
= "_WSAIOR";
3075 return wine_dbg_sprintf("%s(%s, %d)", buf_type
, family
,
3076 (USHORT
)(ioctl
& 0xffff));
3079 /**********************************************************************
3080 * WSAIoctl (WS2_32.50)
3083 INT WINAPI
WSAIoctl(SOCKET s
, DWORD code
, LPVOID in_buff
, DWORD in_size
, LPVOID out_buff
,
3084 DWORD out_size
, LPDWORD ret_size
, LPWSAOVERLAPPED overlapped
,
3085 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
3088 DWORD status
= 0, total
= 0;
3090 TRACE("%ld, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
3091 s
, code
, in_buff
, in_size
, out_buff
, out_size
, ret_size
, overlapped
, completion
);
3096 if (in_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(in_buff
))
3098 WSASetLastError(WSAEFAULT
);
3099 return SOCKET_ERROR
;
3101 if (_get_sock_mask(s
))
3103 /* AsyncSelect()'ed sockets are always nonblocking */
3104 if (!*(WS_u_long
*)in_buff
) status
= WSAEINVAL
;
3107 if (*(WS_u_long
*)in_buff
)
3108 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
3110 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
3115 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
3117 WSASetLastError(WSAEFAULT
);
3118 return SOCKET_ERROR
;
3120 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
3121 if (ioctl(fd
, FIONREAD
, out_buff
) == -1)
3122 status
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
3123 release_sock_fd( s
, fd
);
3129 unsigned int oob
= 0, oobsize
= sizeof(int), atmark
= 0;
3130 if (out_size
!= sizeof(WS_u_long
) || IS_INTRESOURCE(out_buff
))
3132 WSASetLastError(WSAEFAULT
);
3133 return SOCKET_ERROR
;
3135 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
3136 /* SO_OOBINLINE sockets must always return TRUE to SIOCATMARK */
3137 if ((getsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &oob
, &oobsize
) == -1)
3138 || (!oob
&& ioctl(fd
, SIOCATMARK
, &atmark
) == -1))
3139 status
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
3142 /* The SIOCATMARK value read from ioctl() is reversed
3143 * because BSD returns TRUE if it's in the OOB mark
3144 * while Windows returns TRUE if there are NO OOB bytes.
3146 (*(WS_u_long
*) out_buff
) = oob
| !atmark
;
3149 release_sock_fd( s
, fd
);
3154 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
3155 SetLastError(WSAEINVAL
);
3156 return SOCKET_ERROR
;
3158 case WS_SIO_GET_INTERFACE_LIST
:
3160 INTERFACE_INFO
* intArray
= out_buff
;
3161 DWORD size
, numInt
= 0, apiReturn
;
3163 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
3165 if (!out_buff
|| !ret_size
)
3167 WSASetLastError(WSAEFAULT
);
3168 return SOCKET_ERROR
;
3171 fd
= get_sock_fd( s
, 0, NULL
);
3172 if (fd
== -1) return SOCKET_ERROR
;
3174 apiReturn
= GetAdaptersInfo(NULL
, &size
);
3175 if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
3177 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
3181 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
3183 PIP_ADAPTER_INFO ptr
;
3185 if (size
*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > out_size
)
3187 WARN("Buffer too small = %u, out_size = %u\n", size
, out_size
);
3188 HeapFree(GetProcessHeap(),0,table
);
3189 release_sock_fd( s
, fd
);
3193 for (ptr
= table
, numInt
= 0; ptr
;
3194 ptr
= ptr
->Next
, intArray
++, numInt
++)
3196 unsigned int addr
, mask
, bcast
;
3197 struct ifreq ifInfo
;
3199 /* Socket Status Flags */
3200 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
3201 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
3203 ERR("Error obtaining status flags for socket!\n");
3204 HeapFree(GetProcessHeap(),0,table
);
3205 release_sock_fd( s
, fd
);
3211 /* set flags; the values of IFF_* are not the same
3212 under Linux and Windows, therefore must generate
3214 intArray
->iiFlags
= 0;
3215 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
3216 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
3217 #ifdef IFF_POINTOPOINT
3218 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
3219 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
3221 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
3222 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
3223 if (ifInfo
.ifr_flags
& IFF_UP
)
3224 intArray
->iiFlags
|= WS_IFF_UP
;
3225 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
3226 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
3229 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
3230 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
3231 bcast
= addr
| ~mask
;
3232 intArray
->iiAddress
.AddressIn
.sin_family
= AF_INET
;
3233 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
3234 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
=
3236 intArray
->iiNetmask
.AddressIn
.sin_family
= AF_INET
;
3237 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
3238 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
=
3240 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
=
3242 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
3243 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.
3249 ERR("Unable to get interface table!\n");
3252 HeapFree(GetProcessHeap(),0,table
);
3254 else status
= WSAEINVAL
;
3256 else if (apiReturn
!= ERROR_NO_DATA
)
3258 ERR("Unable to get interface table!\n");
3261 /* Calculate the size of the array being returned */
3262 total
= sizeof(INTERFACE_INFO
) * numInt
;
3263 release_sock_fd( s
, fd
);
3267 case WS_SIO_ADDRESS_LIST_CHANGE
:
3268 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
3269 /* FIXME: error and return code depend on whether socket was created
3270 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
3273 case WS_SIO_ADDRESS_LIST_QUERY
:
3277 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
3281 WSASetLastError(WSAEFAULT
);
3282 return SOCKET_ERROR
;
3285 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
3287 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
3290 if (!table
|| GetAdaptersInfo(table
, &size
))
3292 HeapFree(GetProcessHeap(), 0, table
);
3297 for (p
= table
, num
= 0; p
; p
= p
->Next
)
3298 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
3300 total
= sizeof(SOCKET_ADDRESS_LIST
) + sizeof(SOCKET_ADDRESS
) * (num
- 1);
3301 total
+= sizeof(SOCKADDR
) * num
;
3303 if (total
> out_size
)
3305 HeapFree(GetProcessHeap(), 0, table
);
3314 SOCKET_ADDRESS_LIST
*sa_list
= out_buff
;
3315 SOCKADDR_IN
*sockaddr
;
3317 sa
= sa_list
->Address
;
3318 sockaddr
= (SOCKADDR_IN
*)((char *)sa
+ num
* sizeof(SOCKET_ADDRESS
));
3319 sa_list
->iAddressCount
= num
;
3321 for (p
= table
, i
= 0; p
; p
= p
->Next
)
3323 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
3325 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
3326 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
3328 sockaddr
[i
].sin_family
= AF_INET
;
3329 sockaddr
[i
].sin_port
= 0;
3330 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
3335 HeapFree(GetProcessHeap(), 0, table
);
3339 WARN("unable to get IP address list\n");
3346 FIXME("SIO_FLUSH: stub.\n");
3349 case WS_SIO_GET_EXTENSION_FUNCTION_POINTER
:
3351 static const GUID connectex_guid
= WSAID_CONNECTEX
;
3352 static const GUID disconnectex_guid
= WSAID_DISCONNECTEX
;
3353 static const GUID acceptex_guid
= WSAID_ACCEPTEX
;
3354 static const GUID getaccepexsockaddrs_guid
= WSAID_GETACCEPTEXSOCKADDRS
;
3355 static const GUID transmitfile_guid
= WSAID_TRANSMITFILE
;
3356 static const GUID transmitpackets_guid
= WSAID_TRANSMITPACKETS
;
3357 static const GUID wsarecvmsg_guid
= WSAID_WSARECVMSG
;
3358 static const GUID wsasendmsg_guid
= WSAID_WSASENDMSG
;
3360 if ( IsEqualGUID(&connectex_guid
, in_buff
) )
3362 *(LPFN_CONNECTEX
*)out_buff
= WS2_ConnectEx
;
3365 else if ( IsEqualGUID(&disconnectex_guid
, in_buff
) )
3367 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented DisconnectEx\n");
3369 else if ( IsEqualGUID(&acceptex_guid
, in_buff
) )
3371 *(LPFN_ACCEPTEX
*)out_buff
= WS2_AcceptEx
;
3374 else if ( IsEqualGUID(&getaccepexsockaddrs_guid
, in_buff
) )
3376 *(LPFN_GETACCEPTEXSOCKADDRS
*)out_buff
= WS2_GetAcceptExSockaddrs
;
3379 else if ( IsEqualGUID(&transmitfile_guid
, in_buff
) )
3381 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitFile\n");
3383 else if ( IsEqualGUID(&transmitpackets_guid
, in_buff
) )
3385 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitPackets\n");
3387 else if ( IsEqualGUID(&wsarecvmsg_guid
, in_buff
) )
3389 *(LPFN_WSARECVMSG
*)out_buff
= WS2_WSARecvMsg
;
3392 else if ( IsEqualGUID(&wsasendmsg_guid
, in_buff
) )
3394 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented WSASendMsg\n");
3397 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER %s: stub\n", debugstr_guid(in_buff
));
3399 status
= WSAEOPNOTSUPP
;
3402 case WS_SIO_KEEPALIVE_VALS
:
3404 struct tcp_keepalive
*k
;
3405 int keepalive
, keepidle
, keepintvl
;
3407 if (!in_buff
|| in_size
< sizeof(struct tcp_keepalive
))
3409 WSASetLastError(WSAEFAULT
);
3410 return SOCKET_ERROR
;
3414 keepalive
= k
->onoff
? 1 : 0;
3415 keepidle
= max( 1, (k
->keepalivetime
+ 500) / 1000 );
3416 keepintvl
= max( 1, (k
->keepaliveinterval
+ 500) / 1000 );
3418 TRACE("onoff: %d, keepalivetime: %d, keepaliveinterval: %d\n", keepalive
, keepidle
, keepintvl
);
3420 fd
= get_sock_fd(s
, 0, NULL
);
3421 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&keepalive
, sizeof(int)) == -1)
3423 #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL)
3424 /* these values need to be set only if SO_KEEPALIVE is enabled */
3427 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, (void *)&keepidle
, sizeof(int)) == -1)
3429 else if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, (void *)&keepintvl
, sizeof(int)) == -1)
3434 FIXME("ignoring keepalive interval and timeout\n");
3436 release_sock_fd(s
, fd
);
3439 case WS_SIO_ROUTING_INTERFACE_QUERY
:
3441 struct WS_sockaddr
*daddr
= (struct WS_sockaddr
*)in_buff
;
3442 struct WS_sockaddr_in
*daddr_in
= (struct WS_sockaddr_in
*)daddr
;
3443 struct WS_sockaddr_in
*saddr_in
= out_buff
;
3444 MIB_IPFORWARDROW row
;
3445 PMIB_IPADDRTABLE ipAddrTable
= NULL
;
3446 DWORD size
, i
, found_index
;
3448 TRACE("-> WS_SIO_ROUTING_INTERFACE_QUERY request\n");
3450 if (!in_buff
|| in_size
< sizeof(struct WS_sockaddr
) ||
3451 !out_buff
|| out_size
< sizeof(struct WS_sockaddr_in
) || !ret_size
)
3453 WSASetLastError(WSAEFAULT
);
3454 return SOCKET_ERROR
;
3456 if (daddr
->sa_family
!= AF_INET
)
3458 FIXME("unsupported address family %d\n", daddr
->sa_family
);
3459 status
= WSAEAFNOSUPPORT
;
3462 if (GetBestRoute(daddr_in
->sin_addr
.S_un
.S_addr
, 0, &row
) != NOERROR
||
3463 GetIpAddrTable(NULL
, &size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
3468 ipAddrTable
= HeapAlloc(GetProcessHeap(), 0, size
);
3469 if (GetIpAddrTable(ipAddrTable
, &size
, FALSE
))
3471 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3475 for (i
= 0, found_index
= ipAddrTable
->dwNumEntries
;
3476 i
< ipAddrTable
->dwNumEntries
; i
++)
3478 if (ipAddrTable
->table
[i
].dwIndex
== row
.dwForwardIfIndex
)
3481 if (found_index
== ipAddrTable
->dwNumEntries
)
3483 ERR("no matching IP address for interface %d\n",
3484 row
.dwForwardIfIndex
);
3485 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3489 saddr_in
->sin_family
= AF_INET
;
3490 saddr_in
->sin_addr
.S_un
.S_addr
= ipAddrTable
->table
[found_index
].dwAddr
;
3491 saddr_in
->sin_port
= 0;
3492 total
= sizeof(struct WS_sockaddr_in
);
3493 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3496 case WS_SIO_SET_COMPATIBILITY_MODE
:
3497 TRACE("WS_SIO_SET_COMPATIBILITY_MODE ignored\n");
3498 status
= WSAEOPNOTSUPP
;
3500 case WS_SIO_UDP_CONNRESET
:
3501 FIXME("WS_SIO_UDP_CONNRESET stub\n");
3503 case 0x667e: /* Netscape tries hard to use bogus ioctl 0x667e */
3504 WSASetLastError(WSAEOPNOTSUPP
);
3505 return SOCKET_ERROR
;
3507 FIXME("unsupported WS_IOCTL cmd (%s)\n", debugstr_wsaioctl(code
));
3508 status
= WSAEOPNOTSUPP
;
3514 FIXME( "completion routine %p not supported\n", completion
);
3516 else if (overlapped
)
3518 ULONG_PTR cvalue
= (overlapped
&& ((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)overlapped
: 0;
3519 overlapped
->Internal
= status
;
3520 overlapped
->InternalHigh
= total
;
3521 if (overlapped
->hEvent
) NtSetEvent( overlapped
->hEvent
, NULL
);
3522 if (cvalue
) WS_AddCompletion( HANDLE2SOCKET(s
), cvalue
, status
, total
);
3527 if (ret_size
) *ret_size
= total
;
3530 SetLastError( status
);
3531 return SOCKET_ERROR
;
3535 /***********************************************************************
3536 * ioctlsocket (WS2_32.10)
3538 int WINAPI
WS_ioctlsocket(SOCKET s
, LONG cmd
, WS_u_long
*argp
)
3541 return WSAIoctl( s
, cmd
, argp
, sizeof(WS_u_long
), argp
, sizeof(WS_u_long
), &ret_size
, NULL
, NULL
);
3544 /***********************************************************************
3545 * listen (WS2_32.13)
3547 int WINAPI
WS_listen(SOCKET s
, int backlog
)
3549 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
3551 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
3554 if (listen(fd
, backlog
) == 0)
3556 release_sock_fd( s
, fd
);
3557 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
3559 FD_CONNECT
|FD_WINE_CONNECTED
);
3562 SetLastError(wsaErrno());
3563 release_sock_fd( s
, fd
);
3565 return SOCKET_ERROR
;
3568 /***********************************************************************
3571 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
3573 DWORD n
, dwFlags
= flags
;
3579 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
3580 return SOCKET_ERROR
;
3585 /***********************************************************************
3586 * recvfrom (WS2_32.17)
3588 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
3589 struct WS_sockaddr
*from
, int *fromlen
)
3591 DWORD n
, dwFlags
= flags
;
3597 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
3598 return SOCKET_ERROR
;
3603 /* allocate a poll array for the corresponding fd sets */
3604 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
3605 const WS_fd_set
*exceptfds
, int *count_ptr
)
3607 unsigned int i
, j
= 0, count
= 0;
3610 if (readfds
) count
+= readfds
->fd_count
;
3611 if (writefds
) count
+= writefds
->fd_count
;
3612 if (exceptfds
) count
+= exceptfds
->fd_count
;
3616 SetLastError(WSAEINVAL
);
3619 if (!(fds
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(fds
[0]))))
3621 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
3625 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
3627 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
3628 if (fds
[j
].fd
== -1) goto failed
;
3629 fds
[j
].events
= POLLIN
;
3633 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
3635 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
3636 if (fds
[j
].fd
== -1) goto failed
;
3637 fds
[j
].events
= POLLOUT
;
3641 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3643 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
3644 if (fds
[j
].fd
== -1) goto failed
;
3645 fds
[j
].events
= POLLHUP
;
3654 for (i
= 0; i
< readfds
->fd_count
&& j
< count
; i
++, j
++)
3655 release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
3657 for (i
= 0; i
< writefds
->fd_count
&& j
< count
; i
++, j
++)
3658 release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
3660 for (i
= 0; i
< exceptfds
->fd_count
&& j
< count
; i
++, j
++)
3661 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
3662 HeapFree( GetProcessHeap(), 0, fds
);
3666 /* release the file descriptor obtained in fd_sets_to_poll */
3667 /* must be called with the original fd_set arrays, before calling get_poll_results */
3668 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
3669 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
3671 unsigned int i
, j
= 0;
3675 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
3676 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
3680 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
3681 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
3685 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3686 if (fds
[j
].fd
!= -1)
3688 /* make sure we have a real error before releasing the fd */
3689 if (!sock_error_p( fds
[j
].fd
)) fds
[j
].revents
= 0;
3690 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
3695 /* map the poll results back into the Windows fd sets */
3696 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
3697 const struct pollfd
*fds
)
3699 unsigned int i
, j
= 0, k
, total
= 0;
3703 for (i
= k
= 0; i
< readfds
->fd_count
; i
++, j
++)
3704 if (fds
[j
].revents
) readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
3705 readfds
->fd_count
= k
;
3710 for (i
= k
= 0; i
< writefds
->fd_count
; i
++, j
++)
3711 if ((fds
[j
].revents
& POLLOUT
) && !(fds
[j
].revents
& POLLHUP
))
3712 writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
3713 writefds
->fd_count
= k
;
3718 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3719 if (fds
[j
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
3720 exceptfds
->fd_count
= k
;
3727 /***********************************************************************
3728 * select (WS2_32.18)
3730 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
3731 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
3732 const struct WS_timeval
* ws_timeout
)
3734 struct pollfd
*pollfds
;
3735 struct timeval tv1
, tv2
;
3737 int count
, ret
, timeout
= -1;
3739 TRACE("read %p, write %p, excp %p timeout %p\n",
3740 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
3742 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)))
3743 return SOCKET_ERROR
;
3747 torig
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
3749 gettimeofday( &tv1
, 0 );
3752 while ((ret
= poll( pollfds
, count
, timeout
)) < 0)
3756 if (!ws_timeout
) continue;
3757 gettimeofday( &tv2
, 0 );
3759 tv2
.tv_sec
-= tv1
.tv_sec
;
3760 tv2
.tv_usec
-= tv1
.tv_usec
;
3761 if (tv2
.tv_usec
< 0)
3763 tv2
.tv_usec
+= 1000000;
3767 timeout
= torig
- (tv2
.tv_sec
* 1000) - (tv2
.tv_usec
+ 999) / 1000;
3768 if (timeout
<= 0) break;
3771 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
3773 if (ret
== -1) SetLastError(wsaErrno());
3774 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
3775 HeapFree( GetProcessHeap(), 0, pollfds
);
3779 /* helper to send completion messages for client-only i/o operation case */
3780 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
,
3783 SERVER_START_REQ( add_fd_completion
)
3785 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(sock
) );
3786 req
->cvalue
= CompletionValue
;
3787 req
->status
= CompletionStatus
;
3788 req
->information
= Information
;
3789 wine_server_call( req
);
3795 /***********************************************************************
3798 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
3804 wsabuf
.buf
= (char*) buf
;
3806 if ( WS2_sendto( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
3807 return SOCKET_ERROR
;
3812 /***********************************************************************
3813 * WSASend (WS2_32.72)
3815 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
3816 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
3817 LPWSAOVERLAPPED lpOverlapped
,
3818 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3820 return WS2_sendto( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
3821 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
3824 /***********************************************************************
3825 * WSASendDisconnect (WS2_32.73)
3827 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
3829 return WS_shutdown( s
, SD_SEND
);
3833 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
3834 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
3835 const struct WS_sockaddr
*to
, int tolen
,
3836 LPWSAOVERLAPPED lpOverlapped
,
3837 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3839 unsigned int i
, options
;
3841 struct ws2_async
*wsa
= NULL
;
3842 int totalLength
= 0;
3843 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
3846 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
3847 s
, lpBuffers
, dwBufferCount
, dwFlags
,
3848 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
3850 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
3851 TRACE( "fd=%d, options=%x\n", fd
, options
);
3853 if ( fd
== -1 ) return SOCKET_ERROR
;
3855 if (!lpOverlapped
&& !lpNumberOfBytesSent
)
3860 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
3866 wsa
->hSocket
= SOCKET2HANDLE(s
);
3867 wsa
->addr
= (struct WS_sockaddr
*)to
;
3868 wsa
->addrlen
.val
= tolen
;
3869 wsa
->flags
= dwFlags
;
3870 wsa
->lpFlags
= &wsa
->flags
;
3871 wsa
->control
= NULL
;
3872 wsa
->n_iovecs
= dwBufferCount
;
3873 wsa
->first_iovec
= 0;
3874 for ( i
= 0; i
< dwBufferCount
; i
++ )
3876 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
3877 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
3878 totalLength
+= lpBuffers
[i
].len
;
3883 n
= WS2_send( fd
, wsa
);
3884 if (n
!= -1 || errno
!= EINTR
) break;
3886 if (n
== -1 && errno
!= EAGAIN
)
3892 if ((lpOverlapped
|| lpCompletionRoutine
) &&
3893 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
3895 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
3897 wsa
->user_overlapped
= lpOverlapped
;
3898 wsa
->completion_func
= lpCompletionRoutine
;
3899 release_sock_fd( s
, fd
);
3901 if (n
== -1 || n
< totalLength
)
3903 iosb
->u
.Status
= STATUS_PENDING
;
3904 iosb
->Information
= n
== -1 ? 0 : n
;
3906 SERVER_START_REQ( register_async
)
3908 req
->type
= ASYNC_TYPE_WRITE
;
3909 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
3910 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
3911 req
->async
.iosb
= wine_server_client_ptr( iosb
);
3912 req
->async
.arg
= wine_server_client_ptr( wsa
);
3913 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
3914 req
->async
.cvalue
= cvalue
;
3915 err
= wine_server_call( req
);
3919 /* Enable the event only after starting the async. The server will deliver it as soon as
3920 the async is done. */
3921 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
3923 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
3924 WSASetLastError( NtStatusToWSAError( err
));
3925 return SOCKET_ERROR
;
3928 iosb
->u
.Status
= STATUS_SUCCESS
;
3929 iosb
->Information
= n
;
3930 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= n
;
3931 if (!wsa
->completion_func
)
3933 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
3934 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
3935 HeapFree( GetProcessHeap(), 0, wsa
);
3937 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
3938 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
3943 if ( _is_blocking(s
) )
3945 /* On a blocking non-overlapped stream socket,
3946 * sending blocks until the entire buffer is sent. */
3947 DWORD timeout_start
= GetTickCount();
3949 bytes_sent
= n
== -1 ? 0 : n
;
3951 while (wsa
->first_iovec
< wsa
->n_iovecs
)
3954 int timeout
= GET_SNDTIMEO(fd
);
3958 timeout
-= GetTickCount() - timeout_start
;
3959 if (timeout
< 0) timeout
= 0;
3963 pfd
.events
= POLLOUT
;
3965 if (!timeout
|| !poll( &pfd
, 1, timeout
))
3968 goto error
; /* msdn says a timeout in send is fatal */
3971 n
= WS2_send( fd
, wsa
);
3972 if (n
== -1 && errno
!= EAGAIN
&& errno
!= EINTR
)
3982 else /* non-blocking */
3984 if (n
< totalLength
)
3985 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
3988 err
= WSAEWOULDBLOCK
;
3994 TRACE(" -> %i bytes\n", bytes_sent
);
3996 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= bytes_sent
;
3997 HeapFree( GetProcessHeap(), 0, wsa
);
3998 release_sock_fd( s
, fd
);
4003 HeapFree( GetProcessHeap(), 0, wsa
);
4004 release_sock_fd( s
, fd
);
4005 WARN(" -> ERROR %d\n", err
);
4006 WSASetLastError(err
);
4007 return SOCKET_ERROR
;
4010 /***********************************************************************
4011 * WSASendTo (WS2_32.74)
4013 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4014 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
4015 const struct WS_sockaddr
*to
, int tolen
,
4016 LPWSAOVERLAPPED lpOverlapped
,
4017 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4019 return WS2_sendto( s
, lpBuffers
, dwBufferCount
,
4020 lpNumberOfBytesSent
, dwFlags
,
4022 lpOverlapped
, lpCompletionRoutine
);
4025 /***********************************************************************
4026 * sendto (WS2_32.20)
4028 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
4029 const struct WS_sockaddr
*to
, int tolen
)
4035 wsabuf
.buf
= (char*) buf
;
4037 if ( WS2_sendto(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
4038 return SOCKET_ERROR
;
4043 /***********************************************************************
4044 * setsockopt (WS2_32.21)
4046 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
4047 const char *optval
, int optlen
)
4051 struct linger linger
;
4052 struct timeval tval
;
4054 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
4055 s
, level
, optname
, optval
, optlen
);
4057 /* some broken apps pass the value directly instead of a pointer to it */
4058 if(optlen
&& IS_INTRESOURCE(optval
))
4060 SetLastError(WSAEFAULT
);
4061 return SOCKET_ERROR
;
4069 /* Some options need some conversion before they can be sent to
4070 * setsockopt. The conversions are done here, then they will fall though
4071 * to the general case. Special options that are not passed to
4072 * setsockopt follow below that.*/
4074 case WS_SO_DONTLINGER
:
4077 SetLastError(WSAEFAULT
);
4078 return SOCKET_ERROR
;
4080 linger
.l_onoff
= *((const int*)optval
) ? 0: 1;
4081 linger
.l_linger
= 0;
4083 optname
= SO_LINGER
;
4084 optval
= (char*)&linger
;
4085 optlen
= sizeof(struct linger
);
4091 SetLastError(WSAEFAULT
);
4092 return SOCKET_ERROR
;
4094 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
4095 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
4097 optname
= SO_LINGER
;
4098 optval
= (char*)&linger
;
4099 optlen
= sizeof(struct linger
);
4103 if (*(const int*)optval
< 2048)
4105 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
4110 /* The options listed here don't need any special handling. Thanks to
4111 * the conversion happening above, options from there will fall through
4113 case WS_SO_ACCEPTCONN
:
4114 case WS_SO_BROADCAST
:
4116 case WS_SO_KEEPALIVE
:
4117 case WS_SO_OOBINLINE
:
4118 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
4119 * however, using it the BSD way fixes bug 8513 and seems to be what
4120 * most programmers assume, anyway */
4121 case WS_SO_REUSEADDR
:
4124 convert_sockopt(&level
, &optname
);
4127 /* SO_DEBUG is a privileged operation, ignore it. */
4129 TRACE("Ignoring SO_DEBUG\n");
4132 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
4133 * socket. According to MSDN, this option is silently ignored.*/
4134 case WS_SO_DONTROUTE
:
4135 TRACE("Ignoring SO_DONTROUTE\n");
4138 /* Stops two sockets from being bound to the same port. Always happens
4139 * on unix systems, so just drop it. */
4140 case WS_SO_EXCLUSIVEADDRUSE
:
4141 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
4144 /* After a ConnectEx call succeeds, the socket can't be used with half of the
4145 * normal winsock functions on windows. We don't have that problem. */
4146 case WS_SO_UPDATE_CONNECT_CONTEXT
:
4147 TRACE("Ignoring SO_UPDATE_CONNECT_CONTEXT, since our sockets are normal\n");
4150 /* After a AcceptEx call succeeds, the socket can't be used with half of the
4151 * normal winsock functions on windows. We don't have that problem. */
4152 case WS_SO_UPDATE_ACCEPT_CONTEXT
:
4153 TRACE("Ignoring SO_UPDATE_ACCEPT_CONTEXT, since our sockets are normal\n");
4156 /* SO_OPENTYPE does not require a valid socket handle. */
4157 case WS_SO_OPENTYPE
:
4158 if (!optlen
|| optlen
< sizeof(int) || !optval
)
4160 SetLastError(WSAEFAULT
);
4161 return SOCKET_ERROR
;
4163 get_per_thread_data()->opentype
= *(const int *)optval
;
4164 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((const int*)optval
) );
4168 case WS_SO_RCVTIMEO
:
4171 case WS_SO_SNDTIMEO
:
4173 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
4174 if (optval
&& optlen
== sizeof(UINT32
)) {
4175 /* WinSock passes milliseconds instead of struct timeval */
4176 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
4177 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
4178 /* min of 500 milliseconds */
4179 if (tval
.tv_sec
== 0 && tval
.tv_usec
&& tval
.tv_usec
< 500000)
4180 tval
.tv_usec
= 500000;
4181 optlen
= sizeof(struct timeval
);
4182 optval
= (char*)&tval
;
4183 } else if (optlen
== sizeof(struct timeval
)) {
4184 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
4186 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
4189 convert_sockopt(&level
, &optname
);
4194 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
4195 SetLastError(WSAENOPROTOOPT
);
4196 return SOCKET_ERROR
;
4198 break; /* case WS_SOL_SOCKET */
4205 fd
= get_sock_fd( s
, 0, NULL
);
4206 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval
, fd
);
4208 /* We try to set the ipx type on ipx socket level. */
4210 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, optlen
) == -1)
4212 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
4213 release_sock_fd( s
, fd
);
4214 return SOCKET_ERROR
;
4219 /* Should we retrieve val using a getsockopt call and then
4220 * set the modified one? */
4221 val
.ipx_pt
= *optval
;
4222 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
4225 release_sock_fd( s
, fd
);
4228 case IPX_FILTERPTYPE
:
4229 /* Sets the receive filter packet type, at the moment we don't support it */
4230 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
4231 /* Returning 0 is better for now than returning a SOCKET_ERROR */
4235 FIXME("opt_name:%x\n", optname
);
4236 return SOCKET_ERROR
;
4238 break; /* case NSPROTO_IPX */
4241 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
4242 case WS_IPPROTO_TCP
:
4245 case WS_TCP_NODELAY
:
4246 convert_sockopt(&level
, &optname
);
4249 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
4250 return SOCKET_ERROR
;
4257 case WS_IP_ADD_MEMBERSHIP
:
4258 case WS_IP_DROP_MEMBERSHIP
:
4262 case WS_IP_MULTICAST_IF
:
4263 case WS_IP_MULTICAST_LOOP
:
4264 case WS_IP_MULTICAST_TTL
:
4271 convert_sockopt(&level
, &optname
);
4273 case WS_IP_DONTFRAGMENT
:
4274 FIXME("IP_DONTFRAGMENT is silently ignored!\n");
4277 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
4278 return SOCKET_ERROR
;
4282 case WS_IPPROTO_IPV6
:
4285 #ifdef IPV6_ADD_MEMBERSHIP
4286 case WS_IPV6_ADD_MEMBERSHIP
:
4288 #ifdef IPV6_DROP_MEMBERSHIP
4289 case WS_IPV6_DROP_MEMBERSHIP
:
4291 case WS_IPV6_MULTICAST_IF
:
4292 case WS_IPV6_MULTICAST_HOPS
:
4293 case WS_IPV6_MULTICAST_LOOP
:
4294 case WS_IPV6_UNICAST_HOPS
:
4295 case WS_IPV6_V6ONLY
:
4296 convert_sockopt(&level
, &optname
);
4298 case WS_IPV6_DONTFRAG
:
4299 FIXME("IPV6_DONTFRAG is silently ignored!\n");
4301 case WS_IPV6_PROTECTION_LEVEL
:
4302 FIXME("IPV6_PROTECTION_LEVEL is ignored!\n");
4305 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
4306 return SOCKET_ERROR
;
4311 WARN("Unknown level: 0x%08x\n", level
);
4312 SetLastError(WSAEINVAL
);
4313 return SOCKET_ERROR
;
4314 } /* end switch(level) */
4316 /* avoid endianness issues if argument is a 16-bit int */
4317 if (optval
&& optlen
< sizeof(int))
4319 woptval
= *((const INT16
*) optval
);
4320 optval
= (char*) &woptval
;
4323 fd
= get_sock_fd( s
, 0, NULL
);
4324 if (fd
== -1) return SOCKET_ERROR
;
4326 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
4328 release_sock_fd( s
, fd
);
4331 TRACE("Setting socket error, %d\n", wsaErrno());
4332 SetLastError(wsaErrno());
4333 release_sock_fd( s
, fd
);
4335 return SOCKET_ERROR
;
4338 /***********************************************************************
4339 * shutdown (WS2_32.22)
4341 int WINAPI
WS_shutdown(SOCKET s
, int how
)
4343 int fd
, err
= WSAENOTSOCK
;
4344 unsigned int options
, clear_flags
= 0;
4346 fd
= get_sock_fd( s
, 0, &options
);
4347 TRACE("socket %04lx, how %i %x\n", s
, how
, options
);
4350 return SOCKET_ERROR
;
4354 case 0: /* drop receives */
4355 clear_flags
|= FD_READ
;
4357 case 1: /* drop sends */
4358 clear_flags
|= FD_WRITE
;
4360 case 2: /* drop all */
4361 clear_flags
|= FD_READ
|FD_WRITE
;
4364 clear_flags
|= FD_WINE_LISTENING
;
4367 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
4372 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
4375 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
4379 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
4380 if (!err
) err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
4383 if (err
) goto error
;
4385 else /* non-overlapped mode */
4387 if ( shutdown( fd
, how
) )
4394 release_sock_fd( s
, fd
);
4395 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
4396 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
4400 release_sock_fd( s
, fd
);
4401 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
4402 WSASetLastError( err
);
4403 return SOCKET_ERROR
;
4406 /***********************************************************************
4407 * socket (WS2_32.23)
4409 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
4411 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
4413 return WSASocketA( af
, type
, protocol
, NULL
, 0,
4414 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
4418 /***********************************************************************
4419 * gethostbyaddr (WS2_32.51)
4421 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
4423 struct WS_hostent
*retval
= NULL
;
4424 struct hostent
* host
;
4426 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4429 struct hostent hostentry
;
4432 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
4434 int res
= gethostbyaddr_r(addr
, len
, type
,
4435 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
4436 if( res
!= ERANGE
) break;
4438 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
4440 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
4442 EnterCriticalSection( &csWSgetXXXbyYYY
);
4443 host
= gethostbyaddr(addr
, len
, type
);
4444 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
4446 if( host
!= NULL
) retval
= WS_dup_he(host
);
4447 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4448 HeapFree(GetProcessHeap(),0,extrabuf
);
4450 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4452 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
4456 /***********************************************************************
4457 * WS_get_local_ips (INTERNAL)
4459 * Returns the list of local IP addresses by going through the network
4460 * adapters and using the local routing table to sort the addresses
4461 * from highest routing priority to lowest routing priority. This
4462 * functionality is inferred from the description for obtaining local
4463 * IP addresses given in the Knowledge Base Article Q160215.
4465 * Please note that the returned hostent is only freed when the thread
4466 * closes and is replaced if another hostent is requested.
4468 static struct WS_hostent
* WS_get_local_ips( char *hostname
)
4470 int last_metric
, numroutes
= 0, i
, j
;
4471 PIP_ADAPTER_INFO adapters
= NULL
, k
;
4472 struct WS_hostent
*hostlist
= NULL
;
4473 PMIB_IPFORWARDTABLE routes
= NULL
;
4474 struct route
*route_addrs
= NULL
;
4475 DWORD adap_size
, route_size
;
4477 /* Obtain the size of the adapter list and routing table, also allocate memory */
4478 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
4480 if (GetIpForwardTable(NULL
, &route_size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
4482 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
4483 routes
= HeapAlloc(GetProcessHeap(), 0, route_size
);
4484 route_addrs
= HeapAlloc(GetProcessHeap(), 0, 0); /* HeapReAlloc doesn't work on NULL */
4485 if (adapters
== NULL
|| routes
== NULL
|| route_addrs
== NULL
)
4487 /* Obtain the adapter list and the full routing table */
4488 if (GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
4490 if (GetIpForwardTable(routes
, &route_size
, FALSE
) != NO_ERROR
)
4492 /* Store the interface associated with each route */
4493 for (i
= 0; i
< routes
->dwNumEntries
; i
++)
4496 DWORD ifmetric
, exists
= FALSE
;
4498 if (routes
->table
[i
].u1
.ForwardType
!= MIB_IPROUTE_TYPE_DIRECT
)
4500 ifindex
= routes
->table
[i
].dwForwardIfIndex
;
4501 ifmetric
= routes
->table
[i
].dwForwardMetric1
;
4502 /* Only store the lowest valued metric for an interface */
4503 for (j
= 0; j
< numroutes
; j
++)
4505 if (route_addrs
[j
].interface
== ifindex
)
4507 if (route_addrs
[j
].metric
> ifmetric
)
4508 route_addrs
[j
].metric
= ifmetric
;
4514 route_addrs
= HeapReAlloc(GetProcessHeap(), 0, route_addrs
, (numroutes
+1)*sizeof(struct route
));
4515 if (route_addrs
== NULL
)
4516 goto cleanup
; /* Memory allocation error, fail gracefully */
4517 route_addrs
[numroutes
].interface
= ifindex
;
4518 route_addrs
[numroutes
].metric
= ifmetric
;
4519 /* If no IP is found in the next step (for whatever reason)
4520 * then fall back to the magic loopback address.
4522 memcpy(&(route_addrs
[numroutes
].addr
.s_addr
), magic_loopback_addr
, 4);
4526 goto cleanup
; /* No routes, fall back to the Magic IP */
4527 /* Find the IP address associated with each found interface */
4528 for (i
= 0; i
< numroutes
; i
++)
4530 for (k
= adapters
; k
!= NULL
; k
= k
->Next
)
4532 char *ip
= k
->IpAddressList
.IpAddress
.String
;
4534 if (route_addrs
[i
].interface
== k
->Index
)
4535 route_addrs
[i
].addr
.s_addr
= (in_addr_t
) inet_addr(ip
);
4538 /* Allocate a hostent and enough memory for all the IPs,
4539 * including the NULL at the end of the list.
4541 hostlist
= WS_create_he(hostname
, 1, numroutes
+1, TRUE
);
4542 if (hostlist
== NULL
)
4543 goto cleanup
; /* Failed to allocate a hostent for the list of IPs */
4544 hostlist
->h_addr_list
[numroutes
] = NULL
; /* NULL-terminate the address list */
4545 hostlist
->h_aliases
[0] = NULL
; /* NULL-terminate the alias list */
4546 hostlist
->h_addrtype
= AF_INET
;
4547 hostlist
->h_length
= sizeof(struct in_addr
); /* = 4 */
4548 /* Reorder the entries when placing them in the host list, Windows expects
4549 * the IP list in order from highest priority to lowest (the critical thing
4550 * is that most applications expect the first IP to be the default route).
4553 for (i
= 0; i
< numroutes
; i
++)
4555 struct in_addr addr
;
4556 int metric
= 0xFFFF;
4558 memcpy(&addr
, magic_loopback_addr
, 4);
4559 for (j
= 0; j
< numroutes
; j
++)
4561 int this_metric
= route_addrs
[j
].metric
;
4563 if (this_metric
> last_metric
&& this_metric
< metric
)
4565 addr
= route_addrs
[j
].addr
;
4566 metric
= this_metric
;
4569 last_metric
= metric
;
4570 (*(struct in_addr
*) hostlist
->h_addr_list
[i
]) = addr
;
4573 /* Cleanup all allocated memory except the address list,
4574 * the address list is used by the calling app.
4577 HeapFree(GetProcessHeap(), 0, route_addrs
);
4578 HeapFree(GetProcessHeap(), 0, adapters
);
4579 HeapFree(GetProcessHeap(), 0, routes
);
4583 /***********************************************************************
4584 * gethostbyname (WS2_32.52)
4586 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
4588 struct WS_hostent
*retval
= NULL
;
4589 struct hostent
* host
;
4590 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4593 struct hostent hostentry
;
4594 int locerr
= ENOBUFS
;
4598 SetLastError(WSANOTINITIALISED
);
4601 if( gethostname( hostname
, 100) == -1) {
4602 SetLastError( WSAENOBUFS
); /* appropriate ? */
4605 if( !name
|| !name
[0]) {
4608 /* If the hostname of the local machine is requested then return the
4609 * complete list of local IP addresses */
4610 if(strcmp(name
, hostname
) == 0)
4611 retval
= WS_get_local_ips(hostname
);
4612 /* If any other hostname was requested (or the routing table lookup failed)
4613 * then return the IP found by the host OS */
4616 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4618 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
4620 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
4621 if( res
!= ERANGE
) break;
4623 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
4625 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
4627 EnterCriticalSection( &csWSgetXXXbyYYY
);
4628 host
= gethostbyname(name
);
4629 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
4631 if (host
) retval
= WS_dup_he(host
);
4632 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4633 HeapFree(GetProcessHeap(),0,extrabuf
);
4635 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4638 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
4639 strcmp(name
, "localhost") != 0)
4641 /* hostname != "localhost" but has loopback address. replace by our
4642 * special address.*/
4643 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
4645 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
4650 /***********************************************************************
4651 * getprotobyname (WS2_32.53)
4653 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
4655 struct WS_protoent
* retval
= NULL
;
4656 #ifdef HAVE_GETPROTOBYNAME
4657 struct protoent
* proto
;
4658 EnterCriticalSection( &csWSgetXXXbyYYY
);
4659 if( (proto
= getprotobyname(name
)) != NULL
)
4661 retval
= WS_dup_pe(proto
);
4664 MESSAGE("protocol %s not found; You might want to add "
4665 "this to /etc/protocols\n", debugstr_a(name
) );
4666 SetLastError(WSANO_DATA
);
4668 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4670 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
4675 /***********************************************************************
4676 * getprotobynumber (WS2_32.54)
4678 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
4680 struct WS_protoent
* retval
= NULL
;
4681 #ifdef HAVE_GETPROTOBYNUMBER
4682 struct protoent
* proto
;
4683 EnterCriticalSection( &csWSgetXXXbyYYY
);
4684 if( (proto
= getprotobynumber(number
)) != NULL
)
4686 retval
= WS_dup_pe(proto
);
4689 MESSAGE("protocol number %d not found; You might want to add "
4690 "this to /etc/protocols\n", number
);
4691 SetLastError(WSANO_DATA
);
4693 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4695 TRACE("%i ret %p\n", number
, retval
);
4700 /***********************************************************************
4701 * getservbyname (WS2_32.55)
4703 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
4705 struct WS_servent
* retval
= NULL
;
4706 struct servent
* serv
;
4708 char *proto_str
= NULL
;
4710 if (!(name_str
= strdup_lower(name
))) return NULL
;
4712 if (proto
&& *proto
)
4714 if (!(proto_str
= strdup_lower(proto
)))
4716 HeapFree( GetProcessHeap(), 0, name_str
);
4721 EnterCriticalSection( &csWSgetXXXbyYYY
);
4722 serv
= getservbyname(name_str
, proto_str
);
4725 retval
= WS_dup_se(serv
);
4727 else SetLastError(WSANO_DATA
);
4728 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4729 HeapFree( GetProcessHeap(), 0, proto_str
);
4730 HeapFree( GetProcessHeap(), 0, name_str
);
4731 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
4735 /***********************************************************************
4736 * freeaddrinfo (WS2_32.@)
4738 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
4741 struct WS_addrinfo
*next
;
4743 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
4744 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
4745 next
= res
->ai_next
;
4746 HeapFree(GetProcessHeap(),0,res
);
4751 /* helper functions for getaddrinfo()/getnameinfo() */
4752 static int convert_aiflag_w2u(int winflags
) {
4756 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
4757 if (ws_aiflag_map
[i
][0] & winflags
) {
4758 unixflags
|= ws_aiflag_map
[i
][1];
4759 winflags
&= ~ws_aiflag_map
[i
][0];
4762 FIXME("Unhandled windows AI_xxx flags %x\n", winflags
);
4766 static int convert_niflag_w2u(int winflags
) {
4770 for (i
=0;i
<sizeof(ws_niflag_map
)/sizeof(ws_niflag_map
[0]);i
++)
4771 if (ws_niflag_map
[i
][0] & winflags
) {
4772 unixflags
|= ws_niflag_map
[i
][1];
4773 winflags
&= ~ws_niflag_map
[i
][0];
4776 FIXME("Unhandled windows NI_xxx flags %x\n", winflags
);
4780 static int convert_aiflag_u2w(int unixflags
) {
4784 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
4785 if (ws_aiflag_map
[i
][1] & unixflags
) {
4786 winflags
|= ws_aiflag_map
[i
][0];
4787 unixflags
&= ~ws_aiflag_map
[i
][1];
4789 if (unixflags
) /* will warn usually */
4790 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags
);
4794 static int convert_eai_u2w(int unixret
) {
4797 for (i
=0;ws_eai_map
[i
][0];i
++)
4798 if (ws_eai_map
[i
][1] == unixret
)
4799 return ws_eai_map
[i
][0];
4803 static char *get_hostname(void)
4808 GetComputerNameExA( ComputerNamePhysicalDnsHostname
, NULL
, &size
);
4809 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
4810 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, size
))) return NULL
;
4811 if (!GetComputerNameExA( ComputerNamePhysicalDnsHostname
, ret
, &size
))
4813 HeapFree( GetProcessHeap(), 0, ret
);
4819 /***********************************************************************
4820 * getaddrinfo (WS2_32.@)
4822 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
4824 #ifdef HAVE_GETADDRINFO
4825 struct addrinfo
*unixaires
= NULL
;
4827 struct addrinfo unixhints
, *punixhints
= NULL
;
4828 char *hostname
= NULL
;
4831 if (!nodename
&& !servname
) return WSAHOST_NOT_FOUND
;
4835 else if (!nodename
[0])
4837 node
= hostname
= get_hostname();
4838 if (!node
) return WSA_NOT_ENOUGH_MEMORY
;
4844 punixhints
= &unixhints
;
4846 memset(&unixhints
, 0, sizeof(unixhints
));
4847 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
4848 if (hints
->ai_family
== 0) /* wildcard, specific to getaddrinfo() */
4849 punixhints
->ai_family
= 0;
4851 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
4852 if (hints
->ai_socktype
== 0) /* wildcard, specific to getaddrinfo() */
4853 punixhints
->ai_socktype
= 0;
4855 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
4856 if (hints
->ai_protocol
== 0) /* wildcard, specific to getaddrinfo() */
4857 punixhints
->ai_protocol
= 0;
4859 punixhints
->ai_protocol
= convert_proto_w2u(hints
->ai_protocol
);
4862 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
4863 result
= getaddrinfo(node
, servname
, punixhints
, &unixaires
);
4865 TRACE("%s, %s %p -> %p %d\n", debugstr_a(nodename
), debugstr_a(servname
), hints
, res
, result
);
4866 HeapFree(GetProcessHeap(), 0, hostname
);
4869 struct addrinfo
*xuai
= unixaires
;
4870 struct WS_addrinfo
**xai
= res
;
4874 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
4880 *xai
= ai
;xai
= &ai
->ai_next
;
4881 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
4882 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
4883 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
4884 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
4885 if (xuai
->ai_canonname
) {
4886 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
4887 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
4888 if (!ai
->ai_canonname
)
4890 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
4892 len
= xuai
->ai_addrlen
;
4893 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
4896 ai
->ai_addrlen
= len
;
4898 int winlen
= ai
->ai_addrlen
;
4900 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
4901 ai
->ai_addrlen
= winlen
;
4905 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
4908 ai
->ai_addrlen
= len
;
4910 xuai
= xuai
->ai_next
;
4912 freeaddrinfo(unixaires
);
4914 result
= convert_eai_u2w(result
);
4920 if (*res
) WS_freeaddrinfo(*res
);
4921 if (unixaires
) freeaddrinfo(unixaires
);
4923 return WSA_NOT_ENOUGH_MEMORY
;
4925 FIXME("getaddrinfo() failed, not found during buildtime.\n");
4930 static struct WS_addrinfoW
*addrinfo_AtoW(const struct WS_addrinfo
*ai
)
4932 struct WS_addrinfoW
*ret
;
4934 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfoW
)))) return NULL
;
4935 ret
->ai_flags
= ai
->ai_flags
;
4936 ret
->ai_family
= ai
->ai_family
;
4937 ret
->ai_socktype
= ai
->ai_socktype
;
4938 ret
->ai_protocol
= ai
->ai_protocol
;
4939 ret
->ai_addrlen
= ai
->ai_addrlen
;
4940 ret
->ai_canonname
= NULL
;
4941 ret
->ai_addr
= NULL
;
4942 ret
->ai_next
= NULL
;
4943 if (ai
->ai_canonname
)
4945 int len
= MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0);
4946 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
4948 HeapFree(GetProcessHeap(), 0, ret
);
4951 MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
);
4955 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
4957 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
4958 HeapFree(GetProcessHeap(), 0, ret
);
4961 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
4966 static struct WS_addrinfoW
*addrinfo_list_AtoW(const struct WS_addrinfo
*info
)
4968 struct WS_addrinfoW
*ret
, *infoW
;
4970 if (!(ret
= infoW
= addrinfo_AtoW(info
))) return NULL
;
4971 while (info
->ai_next
)
4973 if (!(infoW
->ai_next
= addrinfo_AtoW(info
->ai_next
)))
4978 infoW
= infoW
->ai_next
;
4979 info
= info
->ai_next
;
4984 static struct WS_addrinfo
*addrinfo_WtoA(const struct WS_addrinfoW
*ai
)
4986 struct WS_addrinfo
*ret
;
4988 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfo
)))) return NULL
;
4989 ret
->ai_flags
= ai
->ai_flags
;
4990 ret
->ai_family
= ai
->ai_family
;
4991 ret
->ai_socktype
= ai
->ai_socktype
;
4992 ret
->ai_protocol
= ai
->ai_protocol
;
4993 ret
->ai_addrlen
= ai
->ai_addrlen
;
4994 ret
->ai_canonname
= NULL
;
4995 ret
->ai_addr
= NULL
;
4996 ret
->ai_next
= NULL
;
4997 if (ai
->ai_canonname
)
4999 int len
= WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0, NULL
, NULL
);
5000 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
5002 HeapFree(GetProcessHeap(), 0, ret
);
5005 WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
, NULL
, NULL
);
5009 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
5011 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
5012 HeapFree(GetProcessHeap(), 0, ret
);
5015 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
5020 /***********************************************************************
5021 * GetAddrInfoW (WS2_32.@)
5023 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
5026 char *nodenameA
= NULL
, *servnameA
= NULL
;
5027 struct WS_addrinfo
*resA
, *hintsA
= NULL
;
5031 len
= WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, NULL
, 0, NULL
, NULL
);
5032 if (!(nodenameA
= HeapAlloc(GetProcessHeap(), 0, len
))) return EAI_MEMORY
;
5033 WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, nodenameA
, len
, NULL
, NULL
);
5037 len
= WideCharToMultiByte(CP_ACP
, 0, servname
, -1, NULL
, 0, NULL
, NULL
);
5038 if (!(servnameA
= HeapAlloc(GetProcessHeap(), 0, len
)))
5040 HeapFree(GetProcessHeap(), 0, nodenameA
);
5043 WideCharToMultiByte(CP_ACP
, 0, servname
, -1, servnameA
, len
, NULL
, NULL
);
5046 if (hints
) hintsA
= addrinfo_WtoA(hints
);
5047 ret
= WS_getaddrinfo(nodenameA
, servnameA
, hintsA
, &resA
);
5048 WS_freeaddrinfo(hintsA
);
5052 *res
= addrinfo_list_AtoW(resA
);
5053 WS_freeaddrinfo(resA
);
5056 HeapFree(GetProcessHeap(), 0, nodenameA
);
5057 HeapFree(GetProcessHeap(), 0, servnameA
);
5061 /***********************************************************************
5062 * FreeAddrInfoW (WS2_32.@)
5064 void WINAPI
FreeAddrInfoW(PADDRINFOW ai
)
5069 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
5070 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
5072 HeapFree(GetProcessHeap(), 0, ai
);
5077 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
5078 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
5080 #ifdef HAVE_GETNAMEINFO
5082 union generic_unix_sockaddr sa_u
;
5085 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
5086 serv
, servlen
, flags
);
5088 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
5091 WSASetLastError(WSAEFAULT
);
5092 return WSA_NOT_ENOUGH_MEMORY
;
5094 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
5095 return convert_eai_u2w(ret
);
5097 FIXME("getnameinfo() failed, not found during buildtime.\n");
5102 int WINAPI
GetNameInfoW(const SOCKADDR
*sa
, WS_socklen_t salen
, PWCHAR host
,
5103 DWORD hostlen
, PWCHAR serv
, DWORD servlen
, INT flags
)
5106 char *hostA
= NULL
, *servA
= NULL
;
5108 if (host
&& (!(hostA
= HeapAlloc(GetProcessHeap(), 0, hostlen
)))) return EAI_MEMORY
;
5109 if (serv
&& (!(servA
= HeapAlloc(GetProcessHeap(), 0, servlen
))))
5111 HeapFree(GetProcessHeap(), 0, hostA
);
5115 ret
= WS_getnameinfo(sa
, salen
, hostA
, hostlen
, servA
, servlen
, flags
);
5118 if (host
) MultiByteToWideChar(CP_ACP
, 0, hostA
, -1, host
, hostlen
);
5119 if (serv
) MultiByteToWideChar(CP_ACP
, 0, servA
, -1, serv
, servlen
);
5122 HeapFree(GetProcessHeap(), 0, hostA
);
5123 HeapFree(GetProcessHeap(), 0, servA
);
5127 /***********************************************************************
5128 * getservbyport (WS2_32.56)
5130 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
5132 struct WS_servent
* retval
= NULL
;
5133 #ifdef HAVE_GETSERVBYPORT
5134 struct servent
* serv
;
5135 char *proto_str
= NULL
;
5137 if (proto
&& *proto
)
5139 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
5141 EnterCriticalSection( &csWSgetXXXbyYYY
);
5142 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
5143 retval
= WS_dup_se(serv
);
5145 else SetLastError(WSANO_DATA
);
5146 LeaveCriticalSection( &csWSgetXXXbyYYY
);
5147 HeapFree( GetProcessHeap(), 0, proto_str
);
5149 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
5154 /***********************************************************************
5155 * gethostname (WS2_32.57)
5157 int WINAPI
WS_gethostname(char *name
, int namelen
)
5159 TRACE("name %p, len %d\n", name
, namelen
);
5161 if (gethostname(name
, namelen
) == 0)
5163 TRACE("<- '%s'\n", name
);
5166 SetLastError((errno
== EINVAL
) ? WSAEFAULT
: wsaErrno());
5167 TRACE("<- ERROR !\n");
5168 return SOCKET_ERROR
;
5172 /* ------------------------------------- Windows sockets extensions -- *
5174 * ------------------------------------------------------------------- */
5176 /***********************************************************************
5177 * WSAEnumNetworkEvents (WS2_32.36)
5179 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
5183 int errors
[FD_MAX_EVENTS
];
5185 TRACE("%08lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
5187 SERVER_START_REQ( get_socket_event
)
5189 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5190 req
->service
= TRUE
;
5191 req
->c_event
= wine_server_obj_handle( hEvent
);
5192 wine_server_set_reply( req
, errors
, sizeof(errors
) );
5193 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
5198 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
5199 lpEvent
->iErrorCode
[i
] = NtStatusToWSAError(errors
[i
]);
5202 SetLastError(WSAEINVAL
);
5203 return SOCKET_ERROR
;
5206 /***********************************************************************
5207 * WSAEventSelect (WS2_32.39)
5209 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
5213 TRACE("%08lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
5215 SERVER_START_REQ( set_socket_event
)
5217 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5219 req
->event
= wine_server_obj_handle( hEvent
);
5222 ret
= wine_server_call( req
);
5226 SetLastError(WSAEINVAL
);
5227 return SOCKET_ERROR
;
5230 /**********************************************************************
5231 * WSAGetOverlappedResult (WS2_32.40)
5233 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
5234 LPDWORD lpcbTransfer
, BOOL fWait
,
5239 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
5240 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
5242 if ( lpOverlapped
== NULL
)
5244 ERR( "Invalid pointer\n" );
5245 WSASetLastError(WSA_INVALID_PARAMETER
);
5249 status
= lpOverlapped
->Internal
;
5250 if (status
== STATUS_PENDING
)
5254 SetLastError( WSA_IO_INCOMPLETE
);
5258 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
5259 INFINITE
) == WAIT_FAILED
)
5261 status
= lpOverlapped
->Internal
;
5265 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
5268 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
5270 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
5275 /***********************************************************************
5276 * WSAAsyncSelect (WS2_32.101)
5278 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
5282 TRACE("%lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
5284 SERVER_START_REQ( set_socket_event
)
5286 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5289 req
->window
= wine_server_user_handle( hWnd
);
5291 ret
= wine_server_call( req
);
5295 SetLastError(WSAEINVAL
);
5296 return SOCKET_ERROR
;
5299 /***********************************************************************
5300 * WSACreateEvent (WS2_32.31)
5303 WSAEVENT WINAPI
WSACreateEvent(void)
5305 /* Create a manual-reset event, with initial state: unsignaled */
5308 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
5311 /***********************************************************************
5312 * WSACloseEvent (WS2_32.29)
5315 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
5317 TRACE ("event=%p\n", event
);
5319 return CloseHandle(event
);
5322 /***********************************************************************
5323 * WSASocketA (WS2_32.78)
5326 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
5327 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
5328 GROUP g
, DWORD dwFlags
)
5331 WSAPROTOCOL_INFOW info
;
5333 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
5334 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
5336 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
5338 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
5339 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
5340 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
5344 WSASetLastError( WSAEINVAL
);
5345 return SOCKET_ERROR
;
5348 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
5351 /***********************************************************************
5352 * WSASocketW (WS2_32.79)
5355 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
5356 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
5357 GROUP g
, DWORD dwFlags
)
5362 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
5363 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
5366 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
5367 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
5369 /* hack for WSADuplicateSocket */
5370 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
5371 ret
= lpProtocolInfo
->dwCatalogEntryId
;
5372 TRACE("\tgot duplicate %04lx\n", ret
);
5376 /* convert the socket family and type */
5377 af
= convert_af_w2u(af
);
5378 type
= convert_socktype_w2u(type
);
5382 if (af
== FROM_PROTOCOL_INFO
)
5383 af
= lpProtocolInfo
->iAddressFamily
;
5384 if (type
== FROM_PROTOCOL_INFO
)
5385 type
= lpProtocolInfo
->iSocketType
;
5386 if (protocol
== FROM_PROTOCOL_INFO
)
5387 protocol
= lpProtocolInfo
->iProtocol
;
5390 if ( af
== AF_UNSPEC
) /* did they not specify the address family? */
5392 if ((protocol
== IPPROTO_TCP
&& type
== SOCK_STREAM
) ||
5393 (protocol
== IPPROTO_UDP
&& type
== SOCK_DGRAM
))
5399 SetLastError(WSAEPROTOTYPE
);
5400 return INVALID_SOCKET
;
5404 SERVER_START_REQ( create_socket
)
5408 req
->protocol
= protocol
;
5409 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
5410 req
->attributes
= OBJ_INHERIT
;
5411 req
->flags
= dwFlags
;
5412 set_error( wine_server_call( req
) );
5413 ret
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
5418 TRACE("\tcreated %04lx\n", ret
);
5422 if (GetLastError() == WSAEACCES
) /* raw socket denied */
5424 if (type
== SOCK_RAW
)
5425 ERR_(winediag
)("Failed to create a socket of type SOCK_RAW, this requires special permissions.\n");
5427 ERR_(winediag
)("Failed to create socket, this requires special permissions.\n");
5428 SetLastError(WSAESOCKTNOSUPPORT
);
5431 WARN("\t\tfailed!\n");
5432 return INVALID_SOCKET
;
5435 /***********************************************************************
5436 * WSAJoinLeaf (WS2_32.58)
5439 SOCKET WINAPI
WSAJoinLeaf(
5441 const struct WS_sockaddr
*addr
,
5443 LPWSABUF lpCallerData
,
5444 LPWSABUF lpCalleeData
,
5450 return INVALID_SOCKET
;
5453 /***********************************************************************
5454 * __WSAFDIsSet (WS2_32.151)
5456 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
5458 int i
= set
->fd_count
;
5460 TRACE("(%ld,%p(%i))\n", s
, set
, i
);
5463 if (set
->fd_array
[i
] == s
) return 1;
5467 /***********************************************************************
5468 * WSAIsBlocking (WS2_32.114)
5470 BOOL WINAPI
WSAIsBlocking(void)
5472 /* By default WinSock should set all its sockets to non-blocking mode
5473 * and poll in PeekMessage loop when processing "blocking" ones. This
5474 * function is supposed to tell if the program is in this loop. Our
5475 * blocking calls are truly blocking so we always return FALSE.
5477 * Note: It is allowed to call this function without prior WSAStartup().
5484 /***********************************************************************
5485 * WSACancelBlockingCall (WS2_32.113)
5487 INT WINAPI
WSACancelBlockingCall(void)
5493 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
5495 FIXME("How was this called?\n");
5500 /***********************************************************************
5501 * WSASetBlockingHook (WS2_32.109)
5503 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
5505 FARPROC prev
= blocking_hook
;
5506 blocking_hook
= lpBlockFunc
;
5507 TRACE("hook %p\n", lpBlockFunc
);
5512 /***********************************************************************
5513 * WSAUnhookBlockingHook (WS2_32.110)
5515 INT WINAPI
WSAUnhookBlockingHook(void)
5517 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
5522 /* ----------------------------------- end of API stuff */
5524 /* ----------------------------------- helper functions -
5526 * TODO: Merge WS_dup_..() stuff into one function that
5527 * would operate with a generic structure containing internal
5528 * pointers (via a template of some kind).
5531 static int list_size(char** l
, int item_size
)
5536 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
5537 j
+= (i
+ 1) * sizeof(char*); }
5541 static int list_dup(char** l_src
, char** l_to
, int item_size
)
5546 for (i
= 0; l_src
[i
]; i
++) ;
5547 p
= (char *)(l_to
+ i
+ 1);
5548 for (i
= 0; l_src
[i
]; i
++)
5550 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
5551 memcpy(p
, l_src
[i
], count
);
5556 return p
- (char *)l_to
;
5561 /* create a hostent entry
5563 * Creates the entry with enough memory for the name, aliases
5564 * addresses, and the address pointers. Also copies the name
5565 * and sets up all the pointers. If "fill_addresses" is set then
5566 * sufficient memory for the addresses is also allocated and the
5567 * address pointers are set to this memory.
5569 * NOTE: The alias and address lists must be allocated with room
5570 * for the NULL item terminating the list. This is true even if
5571 * the list has no items ("aliases" and "addresses" must be
5572 * at least "1", a truly empty list is invalid).
5574 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int addresses
, int fill_addresses
)
5576 struct WS_hostent
*p_to
;
5579 int size
= (sizeof(struct WS_hostent
) +
5581 sizeof(char *)*aliases
+
5582 sizeof(char *)*addresses
);
5584 /* Allocate enough memory for the addresses */
5586 size
+= sizeof(struct in_addr
)*addresses
;
5588 if (!(p_to
= check_buffer_he(size
))) return NULL
;
5589 memset(p_to
, 0, size
);
5591 p
= (char *)(p_to
+ 1);
5596 p_to
->h_aliases
= (char **)p
;
5597 p
+= sizeof(char *)*aliases
;
5598 p_to
->h_addr_list
= (char **)p
;
5599 p
+= sizeof(char *)*addresses
;
5604 /* NOTE: h_aliases must be filled in manually, leave these
5605 * pointers NULL (already set to NULL by memset earlier).
5608 /* Fill in the list of address pointers */
5609 for (i
= 0; i
< addresses
; i
++)
5610 p_to
->h_addr_list
[i
] = (p
+= sizeof(struct in_addr
));
5611 p
+= sizeof(struct in_addr
);
5616 /* duplicate hostent entry
5617 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
5618 * Ditto for protoent and servent.
5620 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
5622 int addresses
= list_size(p_he
->h_addr_list
, p_he
->h_length
);
5623 int aliases
= list_size(p_he
->h_aliases
, 0);
5624 struct WS_hostent
*p_to
;
5626 p_to
= WS_create_he(p_he
->h_name
, aliases
, addresses
, FALSE
);
5628 if (!p_to
) return NULL
;
5629 p_to
->h_addrtype
= p_he
->h_addrtype
;
5630 p_to
->h_length
= p_he
->h_length
;
5632 list_dup(p_he
->h_aliases
, p_to
->h_aliases
, 0);
5633 list_dup(p_he
->h_addr_list
, p_to
->h_addr_list
, p_he
->h_length
);
5637 /* ----- protoent */
5639 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
)
5642 struct WS_protoent
*p_to
;
5644 int size
= (sizeof(*p_pe
) +
5645 strlen(p_pe
->p_name
) + 1 +
5646 list_size(p_pe
->p_aliases
, 0));
5648 if (!(p_to
= check_buffer_pe(size
))) return NULL
;
5649 p_to
->p_proto
= p_pe
->p_proto
;
5651 p
= (char *)(p_to
+ 1);
5653 strcpy(p
, p_pe
->p_name
);
5656 p_to
->p_aliases
= (char **)p
;
5657 list_dup(p_pe
->p_aliases
, p_to
->p_aliases
, 0);
5663 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
5666 struct WS_servent
*p_to
;
5668 int size
= (sizeof(*p_se
) +
5669 strlen(p_se
->s_proto
) + 1 +
5670 strlen(p_se
->s_name
) + 1 +
5671 list_size(p_se
->s_aliases
, 0));
5673 if (!(p_to
= check_buffer_se(size
))) return NULL
;
5674 p_to
->s_port
= p_se
->s_port
;
5676 p
= (char *)(p_to
+ 1);
5678 strcpy(p
, p_se
->s_name
);
5682 strcpy(p
, p_se
->s_proto
);
5685 p_to
->s_aliases
= (char **)p
;
5686 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
5691 /***********************************************************************
5692 * WSARecv (WS2_32.67)
5694 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5695 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
5696 LPWSAOVERLAPPED lpOverlapped
,
5697 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5699 return WS2_recv_base(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
5700 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
, NULL
);
5703 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5704 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
5705 struct WS_sockaddr
*lpFrom
,
5706 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
5707 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
5708 LPWSABUF lpControlBuffer
)
5710 unsigned int i
, options
;
5712 struct ws2_async
*wsa
;
5713 DWORD timeout_start
= GetTickCount();
5714 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
5716 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
5717 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
5718 (lpFromlen
? *lpFromlen
: -1),
5719 lpOverlapped
, lpCompletionRoutine
);
5721 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
5722 TRACE( "fd=%d, options=%x\n", fd
, options
);
5724 if (fd
== -1) return SOCKET_ERROR
;
5726 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
5732 wsa
->hSocket
= SOCKET2HANDLE(s
);
5733 wsa
->flags
= *lpFlags
;
5734 wsa
->lpFlags
= lpFlags
;
5736 wsa
->addrlen
.ptr
= lpFromlen
;
5737 wsa
->control
= lpControlBuffer
;
5738 wsa
->n_iovecs
= dwBufferCount
;
5739 wsa
->first_iovec
= 0;
5740 for (i
= 0; i
< dwBufferCount
; i
++)
5742 /* check buffer first to trigger write watches */
5743 if (IsBadWritePtr( lpBuffers
[i
].buf
, lpBuffers
[i
].len
))
5748 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
5749 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
5754 n
= WS2_recv( fd
, wsa
);
5757 if (errno
== EINTR
) continue;
5758 if (errno
!= EAGAIN
)
5760 int loc_errno
= errno
;
5762 if (cvalue
) WS_AddCompletion( s
, cvalue
, sock_get_ntstatus(loc_errno
), 0 );
5766 else if (lpNumberOfBytesRecvd
) *lpNumberOfBytesRecvd
= n
;
5768 if ((lpOverlapped
|| lpCompletionRoutine
) &&
5769 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
5771 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
5773 wsa
->user_overlapped
= lpOverlapped
;
5774 wsa
->completion_func
= lpCompletionRoutine
;
5775 release_sock_fd( s
, fd
);
5779 iosb
->u
.Status
= STATUS_PENDING
;
5780 iosb
->Information
= 0;
5782 SERVER_START_REQ( register_async
)
5784 req
->type
= ASYNC_TYPE_READ
;
5785 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
5786 req
->async
.callback
= wine_server_client_ptr( WS2_async_recv
);
5787 req
->async
.iosb
= wine_server_client_ptr( iosb
);
5788 req
->async
.arg
= wine_server_client_ptr( wsa
);
5789 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
5790 req
->async
.cvalue
= cvalue
;
5791 err
= wine_server_call( req
);
5795 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
5796 WSASetLastError( NtStatusToWSAError( err
));
5797 return SOCKET_ERROR
;
5800 iosb
->u
.Status
= STATUS_SUCCESS
;
5801 iosb
->Information
= n
;
5802 if (!wsa
->completion_func
)
5804 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
5805 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
5806 HeapFree( GetProcessHeap(), 0, wsa
);
5808 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
5809 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
5810 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5816 if ( _is_blocking(s
) )
5819 int timeout
= GET_RCVTIMEO(fd
);
5822 timeout
-= GetTickCount() - timeout_start
;
5823 if (timeout
< 0) timeout
= 0;
5827 pfd
.events
= POLLIN
;
5828 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
5830 if (!timeout
|| !poll( &pfd
, 1, timeout
))
5833 /* a timeout is not fatal */
5834 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5840 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5841 err
= WSAEWOULDBLOCK
;
5846 TRACE(" -> %i bytes\n", n
);
5847 HeapFree( GetProcessHeap(), 0, wsa
);
5848 release_sock_fd( s
, fd
);
5849 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5854 HeapFree( GetProcessHeap(), 0, wsa
);
5855 release_sock_fd( s
, fd
);
5856 WARN(" -> ERROR %d\n", err
);
5857 WSASetLastError( err
);
5858 return SOCKET_ERROR
;
5861 /***********************************************************************
5862 * WSARecvFrom (WS2_32.69)
5864 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5865 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
5866 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
5867 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5870 return WS2_recv_base( s
, lpBuffers
, dwBufferCount
,
5871 lpNumberOfBytesRecvd
, lpFlags
,
5873 lpOverlapped
, lpCompletionRoutine
, NULL
);
5876 /***********************************************************************
5877 * WSCInstallProvider (WS2_32.88)
5879 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
5880 LPCWSTR lpszProviderDllPath
,
5881 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
5882 DWORD dwNumberOfEntries
,
5885 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
5886 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
5887 dwNumberOfEntries
, lpErrno
);
5893 /***********************************************************************
5894 * WSCDeinstallProvider (WS2_32.83)
5896 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
5898 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
5904 /***********************************************************************
5905 * WSAAccept (WS2_32.26)
5907 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
5908 LPCONDITIONPROC lpfnCondition
, DWORD_PTR dwCallbackData
)
5911 int ret
= 0, size
= 0;
5912 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
5913 /* QOS SQOS, GQOS; */
5916 SOCKADDR src_addr
, dst_addr
;
5918 TRACE("Socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %ld\n",
5919 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
5922 size
= sizeof(src_addr
);
5923 cs
= WS_accept(s
, &src_addr
, &size
);
5925 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
5927 if (!lpfnCondition
) return cs
;
5929 CallerId
.buf
= (char *)&src_addr
;
5930 CallerId
.len
= sizeof(src_addr
);
5932 CallerData
.buf
= NULL
;
5935 WS_getsockname(cs
, &dst_addr
, &size
);
5937 CalleeId
.buf
= (char *)&dst_addr
;
5938 CalleeId
.len
= sizeof(dst_addr
);
5941 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
5942 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
5947 if (addr
&& addrlen
)
5948 memcpy(addr
, &src_addr
, (*addrlen
> size
) ? size
: *addrlen
);
5951 SERVER_START_REQ( set_socket_deferred
)
5953 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5954 req
->deferred
= wine_server_obj_handle( SOCKET2HANDLE(cs
) );
5955 if ( !wine_server_call_err ( req
) )
5957 SetLastError( WSATRY_AGAIN
);
5958 WS_closesocket( cs
);
5962 return SOCKET_ERROR
;
5965 SetLastError(WSAECONNREFUSED
);
5966 return SOCKET_ERROR
;
5968 FIXME("Unknown return type from Condition function\n");
5969 SetLastError(WSAENOTSOCK
);
5970 return SOCKET_ERROR
;
5974 /***********************************************************************
5975 * WSADuplicateSocketA (WS2_32.32)
5977 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
5981 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
5982 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
5983 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
5984 /* I don't know what the real Windoze does next, this is a hack */
5985 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
5986 * the target use the global duplicate, or we could copy a reference to us to the structure
5987 * and let the target duplicate it from us, but let's do it as simple as possible */
5988 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
5989 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
5990 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
5991 0, FALSE
, DUPLICATE_SAME_ACCESS
);
5992 CloseHandle(hProcess
);
5993 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
5997 /***********************************************************************
5998 * WSADuplicateSocketW (WS2_32.33)
6000 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
6004 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
6006 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
6007 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
6008 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
6009 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
6010 0, FALSE
, DUPLICATE_SAME_ACCESS
);
6011 CloseHandle(hProcess
);
6012 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
6016 /***********************************************************************
6017 * WSAInstallServiceClassA (WS2_32.48)
6019 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
6021 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
6022 WSASetLastError(WSAEACCES
);
6023 return SOCKET_ERROR
;
6026 /***********************************************************************
6027 * WSAInstallServiceClassW (WS2_32.49)
6029 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
6031 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
6032 WSASetLastError(WSAEACCES
);
6033 return SOCKET_ERROR
;
6036 /***********************************************************************
6037 * WSARemoveServiceClass (WS2_32.70)
6039 int WINAPI
WSARemoveServiceClass(LPGUID info
)
6041 FIXME("Request to remove service %p\n",info
);
6042 WSASetLastError(WSATYPE_NOT_FOUND
);
6043 return SOCKET_ERROR
;
6046 /***********************************************************************
6047 * inet_ntop (WS2_32.@)
6049 PCSTR WINAPI
WS_inet_ntop( INT family
, PVOID addr
, PSTR buffer
, SIZE_T len
)
6051 #ifdef HAVE_INET_NTOP
6052 struct WS_in6_addr
*in6
;
6053 struct WS_in_addr
*in
;
6056 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
6059 WSASetLastError( STATUS_INVALID_PARAMETER
);
6068 pdst
= inet_ntop( AF_INET
, &in
->WS_s_addr
, buffer
, len
);
6074 pdst
= inet_ntop( AF_INET6
, in6
->WS_s6_addr
, buffer
, len
);
6078 WSASetLastError( WSAEAFNOSUPPORT
);
6082 if (!pdst
) WSASetLastError( STATUS_INVALID_PARAMETER
);
6085 FIXME( "not supported on this platform\n" );
6086 WSASetLastError( WSAEAFNOSUPPORT
);
6091 /***********************************************************************
6092 * WSAStringToAddressA (WS2_32.80)
6094 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
6096 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
6097 LPSOCKADDR lpAddress
,
6098 LPINT lpAddressLength
)
6101 LPSTR workBuffer
=NULL
,ptrPort
;
6103 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_a(AddressString
), AddressFamily
,
6104 lpProtocolInfo
, lpAddress
, lpAddressLength
);
6106 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
6110 WSASetLastError(WSAEINVAL
);
6111 return SOCKET_ERROR
;
6115 FIXME("ProtocolInfo not implemented.\n");
6117 workBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
6118 strlen(AddressString
) + 1);
6121 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6122 return SOCKET_ERROR
;
6125 strcpy(workBuffer
, AddressString
);
6127 switch(AddressFamily
)
6131 struct in_addr inetaddr
;
6133 /* If lpAddressLength is too small, tell caller the size we need */
6134 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
6136 *lpAddressLength
= sizeof(SOCKADDR_IN
);
6140 *lpAddressLength
= sizeof(SOCKADDR_IN
);
6141 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
6143 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= AF_INET
;
6145 ptrPort
= strchr(workBuffer
, ':');
6148 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= htons(atoi(ptrPort
+1));
6153 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= 0;
6156 if(inet_aton(workBuffer
, &inetaddr
) > 0)
6158 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
6169 struct in6_addr inetaddr
;
6170 /* If lpAddressLength is too small, tell caller the size we need */
6171 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
6173 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
6177 #ifdef HAVE_INET_PTON
6178 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
6179 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
6181 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_family
= WS_AF_INET6
;
6183 /* This one is a bit tricky. An IPv6 address contains colons, so the
6184 * check from IPv4 doesn't work like that. However, IPv6 addresses that
6185 * contain a port are written with braces like [fd12:3456:7890::1]:12345
6186 * so what we will do is to look for ']', check if the next char is a
6187 * colon, and if it is, parse the port as in IPv4. */
6189 ptrPort
= strchr(workBuffer
, ']');
6190 if(ptrPort
&& *(++ptrPort
) == ':')
6192 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= htons(atoi(ptrPort
+1));
6197 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= 0;
6200 if(inet_pton(AF_INET6
, workBuffer
, &inetaddr
) > 0)
6202 memcpy(&((LPSOCKADDR_IN6
)lpAddress
)->sin6_addr
, &inetaddr
,
6203 sizeof(struct in6_addr
));
6207 #endif /* HAVE_INET_PTON */
6213 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
6214 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
6218 HeapFree(GetProcessHeap(), 0, workBuffer
);
6221 WSASetLastError(res
);
6222 return SOCKET_ERROR
;
6225 /***********************************************************************
6226 * WSAStringToAddressW (WS2_32.81)
6228 * Does anybody know if this function allows to use Hebrew/Arabic/Chinese... digits?
6229 * If this should be the case, it would be required to map these digits
6230 * to Unicode digits (0-9) using FoldString first.
6232 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
6234 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
6235 LPSOCKADDR lpAddress
,
6236 LPINT lpAddressLength
)
6239 LPSTR workBuffer
=NULL
;
6240 WSAPROTOCOL_INFOA infoA
;
6241 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
6243 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
6244 lpAddress
, lpAddressLength
);
6246 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
6248 /* if ProtocolInfo is available - convert to ANSI variant */
6251 lpProtoInfoA
= &infoA
;
6252 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
6254 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
6255 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
6257 WSASetLastError( WSAEINVAL
);
6258 return SOCKET_ERROR
;
6264 /* Translate AddressString to ANSI code page - assumes that only
6265 standard digits 0-9 are used with this API call */
6266 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
6267 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
6271 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
6272 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
6273 lpAddress
,lpAddressLength
);
6274 HeapFree( GetProcessHeap(), 0, workBuffer
);
6278 res
= WSA_NOT_ENOUGH_MEMORY
;
6283 WSASetLastError(res
);
6284 return SOCKET_ERROR
;
6287 /***********************************************************************
6288 * WSAAddressToStringA (WS2_32.27)
6290 * See WSAAddressToStringW
6292 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
6293 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
6297 CHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
6300 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
6302 if (!sockaddr
) return SOCKET_ERROR
;
6303 if (!string
|| !lenstr
) return SOCKET_ERROR
;
6305 switch(sockaddr
->sa_family
)
6308 if (len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
6309 sprintf( buffer
, "%u.%u.%u.%u:%u",
6310 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
6311 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
6312 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
6313 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
6314 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
6316 p
= strchr( buffer
, ':' );
6317 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
6322 struct WS_sockaddr_in6
*sockaddr6
= (LPSOCKADDR_IN6
) sockaddr
;
6325 if (len
< sizeof(SOCKADDR_IN6
)) return SOCKET_ERROR
;
6326 if ((sockaddr6
->sin6_port
))
6327 strcpy(buffer
, "[");
6328 if (!WS_inet_ntop(WS_AF_INET6
, &sockaddr6
->sin6_addr
, buffer
+strlen(buffer
), sizeof(buffer
)))
6330 WSASetLastError(WSAEINVAL
);
6331 return SOCKET_ERROR
;
6333 if ((sockaddr6
->sin6_scope_id
))
6334 sprintf(buffer
+strlen(buffer
), "%%%u", sockaddr6
->sin6_scope_id
);
6335 if ((sockaddr6
->sin6_port
))
6336 sprintf(buffer
+strlen(buffer
), "]:%u", ntohs(sockaddr6
->sin6_port
));
6341 WSASetLastError(WSAEINVAL
);
6342 return SOCKET_ERROR
;
6345 size
= strlen( buffer
) + 1;
6350 WSASetLastError(WSAEFAULT
);
6351 return SOCKET_ERROR
;
6355 strcpy( string
, buffer
);
6359 /***********************************************************************
6360 * WSAAddressToStringW (WS2_32.28)
6362 * Convert a sockaddr address into a readable address string.
6365 * sockaddr [I] Pointer to a sockaddr structure.
6366 * len [I] Size of the sockaddr structure.
6367 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
6368 * string [I/O] Pointer to a buffer to receive the address string.
6369 * lenstr [I/O] Size of the receive buffer in WCHARs.
6373 * Failure: SOCKET_ERROR
6376 * The 'info' parameter is ignored.
6378 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
6379 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
6384 WCHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
6387 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
6390 ret
= WSAAddressToStringA(sockaddr
, len
, NULL
, bufAddr
, &size
);
6392 if (ret
) return ret
;
6394 MultiByteToWideChar( CP_ACP
, 0, bufAddr
, size
, buffer
, sizeof( buffer
)/sizeof(WCHAR
));
6399 WSASetLastError(WSAEFAULT
);
6400 return SOCKET_ERROR
;
6404 lstrcpyW( string
, buffer
);
6408 /***********************************************************************
6409 * WSAEnumNameSpaceProvidersA (WS2_32.34)
6411 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
6413 FIXME( "(%p %p) Stub!\n", len
, buffer
);
6417 /***********************************************************************
6418 * WSAEnumNameSpaceProvidersW (WS2_32.35)
6420 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
6422 FIXME( "(%p %p) Stub!\n", len
, buffer
);
6426 /***********************************************************************
6427 * WSAGetQOSByName (WS2_32.41)
6429 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
6431 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
6435 /***********************************************************************
6436 * WSAGetServiceClassInfoA (WS2_32.42)
6438 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
6439 LPWSASERVICECLASSINFOA info
)
6441 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
6443 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6444 return SOCKET_ERROR
;
6447 /***********************************************************************
6448 * WSAGetServiceClassInfoW (WS2_32.43)
6450 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
6451 LPWSASERVICECLASSINFOW info
)
6453 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
6455 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6456 return SOCKET_ERROR
;
6459 /***********************************************************************
6460 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
6462 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
6464 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
6465 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6466 return SOCKET_ERROR
;
6469 /***********************************************************************
6470 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
6472 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
6474 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
6475 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6476 return SOCKET_ERROR
;
6479 /***********************************************************************
6480 * WSALookupServiceBeginA (WS2_32.59)
6482 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
6483 DWORD dwControlFlags
,
6486 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
6488 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6489 return SOCKET_ERROR
;
6492 /***********************************************************************
6493 * WSALookupServiceBeginW (WS2_32.60)
6495 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
6496 DWORD dwControlFlags
,
6499 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
6501 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6502 return SOCKET_ERROR
;
6505 /***********************************************************************
6506 * WSALookupServiceEnd (WS2_32.61)
6508 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
6510 FIXME("(%p) Stub!\n", lookup
);
6514 /***********************************************************************
6515 * WSALookupServiceNextA (WS2_32.62)
6517 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
6519 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
6520 WSASetLastError(WSA_E_NO_MORE
);
6521 return SOCKET_ERROR
;
6524 /***********************************************************************
6525 * WSALookupServiceNextW (WS2_32.63)
6527 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
6529 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
6530 WSASetLastError(WSA_E_NO_MORE
);
6531 return SOCKET_ERROR
;
6534 /***********************************************************************
6535 * WSANtohl (WS2_32.64)
6537 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
6539 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
6541 if (!lphostlong
) return WSAEFAULT
;
6543 *lphostlong
= ntohl( netlong
);
6547 /***********************************************************************
6548 * WSANtohs (WS2_32.65)
6550 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
6552 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
6554 if (!lphostshort
) return WSAEFAULT
;
6556 *lphostshort
= ntohs( netshort
);
6560 /***********************************************************************
6561 * WSAProviderConfigChange (WS2_32.66)
6563 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
6564 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
6566 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
6567 return SOCKET_ERROR
;
6570 /***********************************************************************
6571 * WSARecvDisconnect (WS2_32.68)
6573 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
6575 TRACE( "(0x%04lx %p)\n", s
, disconnectdata
);
6577 return WS_shutdown( s
, 0 );
6580 /***********************************************************************
6581 * WSASetServiceA (WS2_32.76)
6583 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
6585 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
6589 /***********************************************************************
6590 * WSASetServiceW (WS2_32.77)
6592 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
6594 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
6598 /***********************************************************************
6599 * WSCEnableNSProvider (WS2_32.84)
6601 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
6603 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
6607 /***********************************************************************
6608 * WSCGetProviderPath (WS2_32.86)
6610 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
6612 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
6614 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
6616 *errcode
= WSAEINVAL
;
6617 return SOCKET_ERROR
;
6620 /***********************************************************************
6621 * WSCInstallNameSpace (WS2_32.87)
6623 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
6624 DWORD version
, LPGUID provider
)
6626 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
6627 namespace, version
, debugstr_guid(provider
) );
6631 /***********************************************************************
6632 * WSCUnInstallNameSpace (WS2_32.89)
6634 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
6636 FIXME("(%p) Stub!\n", lpProviderId
);
6640 /***********************************************************************
6641 * WSCWriteProviderOrder (WS2_32.91)
6643 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
6645 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);
6649 /***********************************************************************
6650 * WSANSPIoctl (WS2_32.91)
6652 INT WINAPI
WSANSPIoctl( HANDLE hLookup
, DWORD dwControlCode
, LPVOID lpvInBuffer
,
6653 DWORD cbInBuffer
, LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
6654 LPDWORD lpcbBytesReturned
, LPWSACOMPLETION lpCompletion
)
6656 FIXME("(%p, 0x%08x, %p, 0x%08x, %p, 0x%08x, %p, %p) Stub!\n", hLookup
, dwControlCode
,
6657 lpvInBuffer
, cbInBuffer
, lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpCompletion
);
6658 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6659 return SOCKET_ERROR
;