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
)
3888 int loc_errno
= errno
;
3890 if (cvalue
) WS_AddCompletion( s
, cvalue
, sock_get_ntstatus(loc_errno
), 0 );
3894 if ((lpOverlapped
|| lpCompletionRoutine
) &&
3895 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
3897 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
3899 wsa
->user_overlapped
= lpOverlapped
;
3900 wsa
->completion_func
= lpCompletionRoutine
;
3901 release_sock_fd( s
, fd
);
3903 if (n
== -1 || n
< totalLength
)
3905 iosb
->u
.Status
= STATUS_PENDING
;
3906 iosb
->Information
= n
== -1 ? 0 : n
;
3908 SERVER_START_REQ( register_async
)
3910 req
->type
= ASYNC_TYPE_WRITE
;
3911 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
3912 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
3913 req
->async
.iosb
= wine_server_client_ptr( iosb
);
3914 req
->async
.arg
= wine_server_client_ptr( wsa
);
3915 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
3916 req
->async
.cvalue
= cvalue
;
3917 err
= wine_server_call( req
);
3921 /* Enable the event only after starting the async. The server will deliver it as soon as
3922 the async is done. */
3923 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
3925 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
3926 WSASetLastError( NtStatusToWSAError( err
));
3927 return SOCKET_ERROR
;
3930 iosb
->u
.Status
= STATUS_SUCCESS
;
3931 iosb
->Information
= n
;
3932 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= n
;
3933 if (!wsa
->completion_func
)
3935 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
3936 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
3937 HeapFree( GetProcessHeap(), 0, wsa
);
3939 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
3940 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
3945 if ( _is_blocking(s
) )
3947 /* On a blocking non-overlapped stream socket,
3948 * sending blocks until the entire buffer is sent. */
3949 DWORD timeout_start
= GetTickCount();
3951 bytes_sent
= n
== -1 ? 0 : n
;
3953 while (wsa
->first_iovec
< wsa
->n_iovecs
)
3956 int timeout
= GET_SNDTIMEO(fd
);
3960 timeout
-= GetTickCount() - timeout_start
;
3961 if (timeout
< 0) timeout
= 0;
3965 pfd
.events
= POLLOUT
;
3967 if (!timeout
|| !poll( &pfd
, 1, timeout
))
3970 goto error
; /* msdn says a timeout in send is fatal */
3973 n
= WS2_send( fd
, wsa
);
3974 if (n
== -1 && errno
!= EAGAIN
&& errno
!= EINTR
)
3984 else /* non-blocking */
3986 if (n
< totalLength
)
3987 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
3990 err
= WSAEWOULDBLOCK
;
3996 TRACE(" -> %i bytes\n", bytes_sent
);
3998 if (lpNumberOfBytesSent
) *lpNumberOfBytesSent
= bytes_sent
;
3999 HeapFree( GetProcessHeap(), 0, wsa
);
4000 release_sock_fd( s
, fd
);
4005 HeapFree( GetProcessHeap(), 0, wsa
);
4006 release_sock_fd( s
, fd
);
4007 WARN(" -> ERROR %d\n", err
);
4008 WSASetLastError(err
);
4009 return SOCKET_ERROR
;
4012 /***********************************************************************
4013 * WSASendTo (WS2_32.74)
4015 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4016 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
4017 const struct WS_sockaddr
*to
, int tolen
,
4018 LPWSAOVERLAPPED lpOverlapped
,
4019 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4021 return WS2_sendto( s
, lpBuffers
, dwBufferCount
,
4022 lpNumberOfBytesSent
, dwFlags
,
4024 lpOverlapped
, lpCompletionRoutine
);
4027 /***********************************************************************
4028 * sendto (WS2_32.20)
4030 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
4031 const struct WS_sockaddr
*to
, int tolen
)
4037 wsabuf
.buf
= (char*) buf
;
4039 if ( WS2_sendto(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
4040 return SOCKET_ERROR
;
4045 /***********************************************************************
4046 * setsockopt (WS2_32.21)
4048 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
4049 const char *optval
, int optlen
)
4053 struct linger linger
;
4054 struct timeval tval
;
4056 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
4057 s
, level
, optname
, optval
, optlen
);
4059 /* some broken apps pass the value directly instead of a pointer to it */
4060 if(optlen
&& IS_INTRESOURCE(optval
))
4062 SetLastError(WSAEFAULT
);
4063 return SOCKET_ERROR
;
4071 /* Some options need some conversion before they can be sent to
4072 * setsockopt. The conversions are done here, then they will fall though
4073 * to the general case. Special options that are not passed to
4074 * setsockopt follow below that.*/
4076 case WS_SO_DONTLINGER
:
4079 SetLastError(WSAEFAULT
);
4080 return SOCKET_ERROR
;
4082 linger
.l_onoff
= *((const int*)optval
) ? 0: 1;
4083 linger
.l_linger
= 0;
4085 optname
= SO_LINGER
;
4086 optval
= (char*)&linger
;
4087 optlen
= sizeof(struct linger
);
4093 SetLastError(WSAEFAULT
);
4094 return SOCKET_ERROR
;
4096 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
4097 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
4099 optname
= SO_LINGER
;
4100 optval
= (char*)&linger
;
4101 optlen
= sizeof(struct linger
);
4105 if (*(const int*)optval
< 2048)
4107 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
4112 /* The options listed here don't need any special handling. Thanks to
4113 * the conversion happening above, options from there will fall through
4115 case WS_SO_ACCEPTCONN
:
4116 case WS_SO_BROADCAST
:
4118 case WS_SO_KEEPALIVE
:
4119 case WS_SO_OOBINLINE
:
4120 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
4121 * however, using it the BSD way fixes bug 8513 and seems to be what
4122 * most programmers assume, anyway */
4123 case WS_SO_REUSEADDR
:
4126 convert_sockopt(&level
, &optname
);
4129 /* SO_DEBUG is a privileged operation, ignore it. */
4131 TRACE("Ignoring SO_DEBUG\n");
4134 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
4135 * socket. According to MSDN, this option is silently ignored.*/
4136 case WS_SO_DONTROUTE
:
4137 TRACE("Ignoring SO_DONTROUTE\n");
4140 /* Stops two sockets from being bound to the same port. Always happens
4141 * on unix systems, so just drop it. */
4142 case WS_SO_EXCLUSIVEADDRUSE
:
4143 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
4146 /* After a ConnectEx call succeeds, the socket can't be used with half of the
4147 * normal winsock functions on windows. We don't have that problem. */
4148 case WS_SO_UPDATE_CONNECT_CONTEXT
:
4149 TRACE("Ignoring SO_UPDATE_CONNECT_CONTEXT, since our sockets are normal\n");
4152 /* After a AcceptEx call succeeds, the socket can't be used with half of the
4153 * normal winsock functions on windows. We don't have that problem. */
4154 case WS_SO_UPDATE_ACCEPT_CONTEXT
:
4155 TRACE("Ignoring SO_UPDATE_ACCEPT_CONTEXT, since our sockets are normal\n");
4158 /* SO_OPENTYPE does not require a valid socket handle. */
4159 case WS_SO_OPENTYPE
:
4160 if (!optlen
|| optlen
< sizeof(int) || !optval
)
4162 SetLastError(WSAEFAULT
);
4163 return SOCKET_ERROR
;
4165 get_per_thread_data()->opentype
= *(const int *)optval
;
4166 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((const int*)optval
) );
4170 case WS_SO_RCVTIMEO
:
4173 case WS_SO_SNDTIMEO
:
4175 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
4176 if (optval
&& optlen
== sizeof(UINT32
)) {
4177 /* WinSock passes milliseconds instead of struct timeval */
4178 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
4179 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
4180 /* min of 500 milliseconds */
4181 if (tval
.tv_sec
== 0 && tval
.tv_usec
&& tval
.tv_usec
< 500000)
4182 tval
.tv_usec
= 500000;
4183 optlen
= sizeof(struct timeval
);
4184 optval
= (char*)&tval
;
4185 } else if (optlen
== sizeof(struct timeval
)) {
4186 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
4188 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
4191 convert_sockopt(&level
, &optname
);
4196 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
4197 SetLastError(WSAENOPROTOOPT
);
4198 return SOCKET_ERROR
;
4200 break; /* case WS_SOL_SOCKET */
4207 fd
= get_sock_fd( s
, 0, NULL
);
4208 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval
, fd
);
4210 /* We try to set the ipx type on ipx socket level. */
4212 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, optlen
) == -1)
4214 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
4215 release_sock_fd( s
, fd
);
4216 return SOCKET_ERROR
;
4221 /* Should we retrieve val using a getsockopt call and then
4222 * set the modified one? */
4223 val
.ipx_pt
= *optval
;
4224 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
4227 release_sock_fd( s
, fd
);
4230 case IPX_FILTERPTYPE
:
4231 /* Sets the receive filter packet type, at the moment we don't support it */
4232 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
4233 /* Returning 0 is better for now than returning a SOCKET_ERROR */
4237 FIXME("opt_name:%x\n", optname
);
4238 return SOCKET_ERROR
;
4240 break; /* case NSPROTO_IPX */
4243 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
4244 case WS_IPPROTO_TCP
:
4247 case WS_TCP_NODELAY
:
4248 convert_sockopt(&level
, &optname
);
4251 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
4252 return SOCKET_ERROR
;
4259 case WS_IP_ADD_MEMBERSHIP
:
4260 case WS_IP_DROP_MEMBERSHIP
:
4264 case WS_IP_MULTICAST_IF
:
4265 case WS_IP_MULTICAST_LOOP
:
4266 case WS_IP_MULTICAST_TTL
:
4273 convert_sockopt(&level
, &optname
);
4275 case WS_IP_DONTFRAGMENT
:
4276 FIXME("IP_DONTFRAGMENT is silently ignored!\n");
4279 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
4280 return SOCKET_ERROR
;
4284 case WS_IPPROTO_IPV6
:
4287 #ifdef IPV6_ADD_MEMBERSHIP
4288 case WS_IPV6_ADD_MEMBERSHIP
:
4290 #ifdef IPV6_DROP_MEMBERSHIP
4291 case WS_IPV6_DROP_MEMBERSHIP
:
4293 case WS_IPV6_MULTICAST_IF
:
4294 case WS_IPV6_MULTICAST_HOPS
:
4295 case WS_IPV6_MULTICAST_LOOP
:
4296 case WS_IPV6_UNICAST_HOPS
:
4297 case WS_IPV6_V6ONLY
:
4298 convert_sockopt(&level
, &optname
);
4300 case WS_IPV6_DONTFRAG
:
4301 FIXME("IPV6_DONTFRAG is silently ignored!\n");
4304 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
4305 return SOCKET_ERROR
;
4310 WARN("Unknown level: 0x%08x\n", level
);
4311 SetLastError(WSAEINVAL
);
4312 return SOCKET_ERROR
;
4313 } /* end switch(level) */
4315 /* avoid endianness issues if argument is a 16-bit int */
4316 if (optval
&& optlen
< sizeof(int))
4318 woptval
= *((const INT16
*) optval
);
4319 optval
= (char*) &woptval
;
4322 fd
= get_sock_fd( s
, 0, NULL
);
4323 if (fd
== -1) return SOCKET_ERROR
;
4325 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
4327 release_sock_fd( s
, fd
);
4330 TRACE("Setting socket error, %d\n", wsaErrno());
4331 SetLastError(wsaErrno());
4332 release_sock_fd( s
, fd
);
4334 return SOCKET_ERROR
;
4337 /***********************************************************************
4338 * shutdown (WS2_32.22)
4340 int WINAPI
WS_shutdown(SOCKET s
, int how
)
4342 int fd
, err
= WSAENOTSOCK
;
4343 unsigned int options
, clear_flags
= 0;
4345 fd
= get_sock_fd( s
, 0, &options
);
4346 TRACE("socket %04lx, how %i %x\n", s
, how
, options
);
4349 return SOCKET_ERROR
;
4353 case 0: /* drop receives */
4354 clear_flags
|= FD_READ
;
4356 case 1: /* drop sends */
4357 clear_flags
|= FD_WRITE
;
4359 case 2: /* drop all */
4360 clear_flags
|= FD_READ
|FD_WRITE
;
4363 clear_flags
|= FD_WINE_LISTENING
;
4366 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
4371 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
4374 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
4378 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
4379 if (!err
) err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
4382 if (err
) goto error
;
4384 else /* non-overlapped mode */
4386 if ( shutdown( fd
, how
) )
4393 release_sock_fd( s
, fd
);
4394 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
4395 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
4399 release_sock_fd( s
, fd
);
4400 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
4401 WSASetLastError( err
);
4402 return SOCKET_ERROR
;
4405 /***********************************************************************
4406 * socket (WS2_32.23)
4408 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
4410 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
4412 return WSASocketA( af
, type
, protocol
, NULL
, 0,
4413 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
4417 /***********************************************************************
4418 * gethostbyaddr (WS2_32.51)
4420 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
4422 struct WS_hostent
*retval
= NULL
;
4423 struct hostent
* host
;
4425 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4428 struct hostent hostentry
;
4431 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
4433 int res
= gethostbyaddr_r(addr
, len
, type
,
4434 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
4435 if( res
!= ERANGE
) break;
4437 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
4439 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
4441 EnterCriticalSection( &csWSgetXXXbyYYY
);
4442 host
= gethostbyaddr(addr
, len
, type
);
4443 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
4445 if( host
!= NULL
) retval
= WS_dup_he(host
);
4446 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4447 HeapFree(GetProcessHeap(),0,extrabuf
);
4449 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4451 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
4455 /***********************************************************************
4456 * WS_get_local_ips (INTERNAL)
4458 * Returns the list of local IP addresses by going through the network
4459 * adapters and using the local routing table to sort the addresses
4460 * from highest routing priority to lowest routing priority. This
4461 * functionality is inferred from the description for obtaining local
4462 * IP addresses given in the Knowledge Base Article Q160215.
4464 * Please note that the returned hostent is only freed when the thread
4465 * closes and is replaced if another hostent is requested.
4467 static struct WS_hostent
* WS_get_local_ips( char *hostname
)
4469 int last_metric
, numroutes
= 0, i
, j
;
4470 PIP_ADAPTER_INFO adapters
= NULL
, k
;
4471 struct WS_hostent
*hostlist
= NULL
;
4472 PMIB_IPFORWARDTABLE routes
= NULL
;
4473 struct route
*route_addrs
= NULL
;
4474 DWORD adap_size
, route_size
;
4476 /* Obtain the size of the adapter list and routing table, also allocate memory */
4477 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
4479 if (GetIpForwardTable(NULL
, &route_size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
4481 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
4482 routes
= HeapAlloc(GetProcessHeap(), 0, route_size
);
4483 route_addrs
= HeapAlloc(GetProcessHeap(), 0, 0); /* HeapReAlloc doesn't work on NULL */
4484 if (adapters
== NULL
|| routes
== NULL
|| route_addrs
== NULL
)
4486 /* Obtain the adapter list and the full routing table */
4487 if (GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
4489 if (GetIpForwardTable(routes
, &route_size
, FALSE
) != NO_ERROR
)
4491 /* Store the interface associated with each route */
4492 for (i
= 0; i
< routes
->dwNumEntries
; i
++)
4495 DWORD ifmetric
, exists
= FALSE
;
4497 if (routes
->table
[i
].u1
.ForwardType
!= MIB_IPROUTE_TYPE_DIRECT
)
4499 ifindex
= routes
->table
[i
].dwForwardIfIndex
;
4500 ifmetric
= routes
->table
[i
].dwForwardMetric1
;
4501 /* Only store the lowest valued metric for an interface */
4502 for (j
= 0; j
< numroutes
; j
++)
4504 if (route_addrs
[j
].interface
== ifindex
)
4506 if (route_addrs
[j
].metric
> ifmetric
)
4507 route_addrs
[j
].metric
= ifmetric
;
4513 route_addrs
= HeapReAlloc(GetProcessHeap(), 0, route_addrs
, (numroutes
+1)*sizeof(struct route
));
4514 if (route_addrs
== NULL
)
4515 goto cleanup
; /* Memory allocation error, fail gracefully */
4516 route_addrs
[numroutes
].interface
= ifindex
;
4517 route_addrs
[numroutes
].metric
= ifmetric
;
4518 /* If no IP is found in the next step (for whatever reason)
4519 * then fall back to the magic loopback address.
4521 memcpy(&(route_addrs
[numroutes
].addr
.s_addr
), magic_loopback_addr
, 4);
4525 goto cleanup
; /* No routes, fall back to the Magic IP */
4526 /* Find the IP address associated with each found interface */
4527 for (i
= 0; i
< numroutes
; i
++)
4529 for (k
= adapters
; k
!= NULL
; k
= k
->Next
)
4531 char *ip
= k
->IpAddressList
.IpAddress
.String
;
4533 if (route_addrs
[i
].interface
== k
->Index
)
4534 route_addrs
[i
].addr
.s_addr
= (in_addr_t
) inet_addr(ip
);
4537 /* Allocate a hostent and enough memory for all the IPs,
4538 * including the NULL at the end of the list.
4540 hostlist
= WS_create_he(hostname
, 1, numroutes
+1, TRUE
);
4541 if (hostlist
== NULL
)
4542 goto cleanup
; /* Failed to allocate a hostent for the list of IPs */
4543 hostlist
->h_addr_list
[numroutes
] = NULL
; /* NULL-terminate the address list */
4544 hostlist
->h_aliases
[0] = NULL
; /* NULL-terminate the alias list */
4545 hostlist
->h_addrtype
= AF_INET
;
4546 hostlist
->h_length
= sizeof(struct in_addr
); /* = 4 */
4547 /* Reorder the entries when placing them in the host list, Windows expects
4548 * the IP list in order from highest priority to lowest (the critical thing
4549 * is that most applications expect the first IP to be the default route).
4552 for (i
= 0; i
< numroutes
; i
++)
4554 struct in_addr addr
;
4555 int metric
= 0xFFFF;
4557 memcpy(&addr
, magic_loopback_addr
, 4);
4558 for (j
= 0; j
< numroutes
; j
++)
4560 int this_metric
= route_addrs
[j
].metric
;
4562 if (this_metric
> last_metric
&& this_metric
< metric
)
4564 addr
= route_addrs
[j
].addr
;
4565 metric
= this_metric
;
4568 last_metric
= metric
;
4569 (*(struct in_addr
*) hostlist
->h_addr_list
[i
]) = addr
;
4572 /* Cleanup all allocated memory except the address list,
4573 * the address list is used by the calling app.
4576 HeapFree(GetProcessHeap(), 0, route_addrs
);
4577 HeapFree(GetProcessHeap(), 0, adapters
);
4578 HeapFree(GetProcessHeap(), 0, routes
);
4582 /***********************************************************************
4583 * gethostbyname (WS2_32.52)
4585 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
4587 struct WS_hostent
*retval
= NULL
;
4588 struct hostent
* host
;
4589 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4592 struct hostent hostentry
;
4593 int locerr
= ENOBUFS
;
4597 SetLastError(WSANOTINITIALISED
);
4600 if( gethostname( hostname
, 100) == -1) {
4601 SetLastError( WSAENOBUFS
); /* appropriate ? */
4604 if( !name
|| !name
[0]) {
4607 /* If the hostname of the local machine is requested then return the
4608 * complete list of local IP addresses */
4609 if(strcmp(name
, hostname
) == 0)
4610 retval
= WS_get_local_ips(hostname
);
4611 /* If any other hostname was requested (or the routing table lookup failed)
4612 * then return the IP found by the host OS */
4615 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4617 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
4619 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
4620 if( res
!= ERANGE
) break;
4622 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
4624 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
4626 EnterCriticalSection( &csWSgetXXXbyYYY
);
4627 host
= gethostbyname(name
);
4628 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
4630 if (host
) retval
= WS_dup_he(host
);
4631 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4632 HeapFree(GetProcessHeap(),0,extrabuf
);
4634 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4637 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
4638 strcmp(name
, "localhost") != 0)
4640 /* hostname != "localhost" but has loopback address. replace by our
4641 * special address.*/
4642 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
4644 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
4649 /***********************************************************************
4650 * getprotobyname (WS2_32.53)
4652 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
4654 struct WS_protoent
* retval
= NULL
;
4655 #ifdef HAVE_GETPROTOBYNAME
4656 struct protoent
* proto
;
4657 EnterCriticalSection( &csWSgetXXXbyYYY
);
4658 if( (proto
= getprotobyname(name
)) != NULL
)
4660 retval
= WS_dup_pe(proto
);
4663 MESSAGE("protocol %s not found; You might want to add "
4664 "this to /etc/protocols\n", debugstr_a(name
) );
4665 SetLastError(WSANO_DATA
);
4667 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4669 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
4674 /***********************************************************************
4675 * getprotobynumber (WS2_32.54)
4677 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
4679 struct WS_protoent
* retval
= NULL
;
4680 #ifdef HAVE_GETPROTOBYNUMBER
4681 struct protoent
* proto
;
4682 EnterCriticalSection( &csWSgetXXXbyYYY
);
4683 if( (proto
= getprotobynumber(number
)) != NULL
)
4685 retval
= WS_dup_pe(proto
);
4688 MESSAGE("protocol number %d not found; You might want to add "
4689 "this to /etc/protocols\n", number
);
4690 SetLastError(WSANO_DATA
);
4692 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4694 TRACE("%i ret %p\n", number
, retval
);
4699 /***********************************************************************
4700 * getservbyname (WS2_32.55)
4702 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
4704 struct WS_servent
* retval
= NULL
;
4705 struct servent
* serv
;
4707 char *proto_str
= NULL
;
4709 if (!(name_str
= strdup_lower(name
))) return NULL
;
4711 if (proto
&& *proto
)
4713 if (!(proto_str
= strdup_lower(proto
)))
4715 HeapFree( GetProcessHeap(), 0, name_str
);
4720 EnterCriticalSection( &csWSgetXXXbyYYY
);
4721 serv
= getservbyname(name_str
, proto_str
);
4724 retval
= WS_dup_se(serv
);
4726 else SetLastError(WSANO_DATA
);
4727 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4728 HeapFree( GetProcessHeap(), 0, proto_str
);
4729 HeapFree( GetProcessHeap(), 0, name_str
);
4730 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
4734 /***********************************************************************
4735 * freeaddrinfo (WS2_32.@)
4737 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
4740 struct WS_addrinfo
*next
;
4742 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
4743 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
4744 next
= res
->ai_next
;
4745 HeapFree(GetProcessHeap(),0,res
);
4750 /* helper functions for getaddrinfo()/getnameinfo() */
4751 static int convert_aiflag_w2u(int winflags
) {
4755 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
4756 if (ws_aiflag_map
[i
][0] & winflags
) {
4757 unixflags
|= ws_aiflag_map
[i
][1];
4758 winflags
&= ~ws_aiflag_map
[i
][0];
4761 FIXME("Unhandled windows AI_xxx flags %x\n", winflags
);
4765 static int convert_niflag_w2u(int winflags
) {
4769 for (i
=0;i
<sizeof(ws_niflag_map
)/sizeof(ws_niflag_map
[0]);i
++)
4770 if (ws_niflag_map
[i
][0] & winflags
) {
4771 unixflags
|= ws_niflag_map
[i
][1];
4772 winflags
&= ~ws_niflag_map
[i
][0];
4775 FIXME("Unhandled windows NI_xxx flags %x\n", winflags
);
4779 static int convert_aiflag_u2w(int unixflags
) {
4783 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
4784 if (ws_aiflag_map
[i
][1] & unixflags
) {
4785 winflags
|= ws_aiflag_map
[i
][0];
4786 unixflags
&= ~ws_aiflag_map
[i
][1];
4788 if (unixflags
) /* will warn usually */
4789 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags
);
4793 static int convert_eai_u2w(int unixret
) {
4796 for (i
=0;ws_eai_map
[i
][0];i
++)
4797 if (ws_eai_map
[i
][1] == unixret
)
4798 return ws_eai_map
[i
][0];
4802 static char *get_hostname(void)
4807 GetComputerNameExA( ComputerNamePhysicalDnsHostname
, NULL
, &size
);
4808 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
4809 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, size
))) return NULL
;
4810 if (!GetComputerNameExA( ComputerNamePhysicalDnsHostname
, ret
, &size
))
4812 HeapFree( GetProcessHeap(), 0, ret
);
4818 /***********************************************************************
4819 * getaddrinfo (WS2_32.@)
4821 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
4823 #ifdef HAVE_GETADDRINFO
4824 struct addrinfo
*unixaires
= NULL
;
4826 struct addrinfo unixhints
, *punixhints
= NULL
;
4827 char *hostname
= NULL
;
4830 if (!nodename
&& !servname
) return WSAHOST_NOT_FOUND
;
4834 else if (!nodename
[0])
4836 node
= hostname
= get_hostname();
4837 if (!node
) return WSA_NOT_ENOUGH_MEMORY
;
4843 punixhints
= &unixhints
;
4845 memset(&unixhints
, 0, sizeof(unixhints
));
4846 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
4847 if (hints
->ai_family
== 0) /* wildcard, specific to getaddrinfo() */
4848 punixhints
->ai_family
= 0;
4850 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
4851 if (hints
->ai_socktype
== 0) /* wildcard, specific to getaddrinfo() */
4852 punixhints
->ai_socktype
= 0;
4854 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
4855 if (hints
->ai_protocol
== 0) /* wildcard, specific to getaddrinfo() */
4856 punixhints
->ai_protocol
= 0;
4858 punixhints
->ai_protocol
= convert_proto_w2u(hints
->ai_protocol
);
4861 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
4862 result
= getaddrinfo(node
, servname
, punixhints
, &unixaires
);
4864 TRACE("%s, %s %p -> %p %d\n", debugstr_a(nodename
), debugstr_a(servname
), hints
, res
, result
);
4865 HeapFree(GetProcessHeap(), 0, hostname
);
4868 struct addrinfo
*xuai
= unixaires
;
4869 struct WS_addrinfo
**xai
= res
;
4873 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
4879 *xai
= ai
;xai
= &ai
->ai_next
;
4880 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
4881 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
4882 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
4883 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
4884 if (xuai
->ai_canonname
) {
4885 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
4886 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
4887 if (!ai
->ai_canonname
)
4889 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
4891 len
= xuai
->ai_addrlen
;
4892 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
4895 ai
->ai_addrlen
= len
;
4897 int winlen
= ai
->ai_addrlen
;
4899 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
4900 ai
->ai_addrlen
= winlen
;
4904 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
4907 ai
->ai_addrlen
= len
;
4909 xuai
= xuai
->ai_next
;
4911 freeaddrinfo(unixaires
);
4913 result
= convert_eai_u2w(result
);
4919 if (*res
) WS_freeaddrinfo(*res
);
4920 if (unixaires
) freeaddrinfo(unixaires
);
4922 return WSA_NOT_ENOUGH_MEMORY
;
4924 FIXME("getaddrinfo() failed, not found during buildtime.\n");
4929 static struct WS_addrinfoW
*addrinfo_AtoW(const struct WS_addrinfo
*ai
)
4931 struct WS_addrinfoW
*ret
;
4933 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfoW
)))) return NULL
;
4934 ret
->ai_flags
= ai
->ai_flags
;
4935 ret
->ai_family
= ai
->ai_family
;
4936 ret
->ai_socktype
= ai
->ai_socktype
;
4937 ret
->ai_protocol
= ai
->ai_protocol
;
4938 ret
->ai_addrlen
= ai
->ai_addrlen
;
4939 ret
->ai_canonname
= NULL
;
4940 ret
->ai_addr
= NULL
;
4941 ret
->ai_next
= NULL
;
4942 if (ai
->ai_canonname
)
4944 int len
= MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0);
4945 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
4947 HeapFree(GetProcessHeap(), 0, ret
);
4950 MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
);
4954 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
4956 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
4957 HeapFree(GetProcessHeap(), 0, ret
);
4960 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
4965 static struct WS_addrinfoW
*addrinfo_list_AtoW(const struct WS_addrinfo
*info
)
4967 struct WS_addrinfoW
*ret
, *infoW
;
4969 if (!(ret
= infoW
= addrinfo_AtoW(info
))) return NULL
;
4970 while (info
->ai_next
)
4972 if (!(infoW
->ai_next
= addrinfo_AtoW(info
->ai_next
)))
4977 infoW
= infoW
->ai_next
;
4978 info
= info
->ai_next
;
4983 static struct WS_addrinfo
*addrinfo_WtoA(const struct WS_addrinfoW
*ai
)
4985 struct WS_addrinfo
*ret
;
4987 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfo
)))) return NULL
;
4988 ret
->ai_flags
= ai
->ai_flags
;
4989 ret
->ai_family
= ai
->ai_family
;
4990 ret
->ai_socktype
= ai
->ai_socktype
;
4991 ret
->ai_protocol
= ai
->ai_protocol
;
4992 ret
->ai_addrlen
= ai
->ai_addrlen
;
4993 ret
->ai_canonname
= NULL
;
4994 ret
->ai_addr
= NULL
;
4995 ret
->ai_next
= NULL
;
4996 if (ai
->ai_canonname
)
4998 int len
= WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0, NULL
, NULL
);
4999 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
5001 HeapFree(GetProcessHeap(), 0, ret
);
5004 WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
, NULL
, NULL
);
5008 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
5010 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
5011 HeapFree(GetProcessHeap(), 0, ret
);
5014 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
5019 /***********************************************************************
5020 * GetAddrInfoW (WS2_32.@)
5022 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
5025 char *nodenameA
= NULL
, *servnameA
= NULL
;
5026 struct WS_addrinfo
*resA
, *hintsA
= NULL
;
5030 len
= WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, NULL
, 0, NULL
, NULL
);
5031 if (!(nodenameA
= HeapAlloc(GetProcessHeap(), 0, len
))) return EAI_MEMORY
;
5032 WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, nodenameA
, len
, NULL
, NULL
);
5036 len
= WideCharToMultiByte(CP_ACP
, 0, servname
, -1, NULL
, 0, NULL
, NULL
);
5037 if (!(servnameA
= HeapAlloc(GetProcessHeap(), 0, len
)))
5039 HeapFree(GetProcessHeap(), 0, nodenameA
);
5042 WideCharToMultiByte(CP_ACP
, 0, servname
, -1, servnameA
, len
, NULL
, NULL
);
5045 if (hints
) hintsA
= addrinfo_WtoA(hints
);
5046 ret
= WS_getaddrinfo(nodenameA
, servnameA
, hintsA
, &resA
);
5047 WS_freeaddrinfo(hintsA
);
5051 *res
= addrinfo_list_AtoW(resA
);
5052 WS_freeaddrinfo(resA
);
5055 HeapFree(GetProcessHeap(), 0, nodenameA
);
5056 HeapFree(GetProcessHeap(), 0, servnameA
);
5060 /***********************************************************************
5061 * FreeAddrInfoW (WS2_32.@)
5063 void WINAPI
FreeAddrInfoW(PADDRINFOW ai
)
5068 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
5069 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
5071 HeapFree(GetProcessHeap(), 0, ai
);
5076 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
5077 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
5079 #ifdef HAVE_GETNAMEINFO
5081 union generic_unix_sockaddr sa_u
;
5084 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
5085 serv
, servlen
, flags
);
5087 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
5090 WSASetLastError(WSAEFAULT
);
5091 return WSA_NOT_ENOUGH_MEMORY
;
5093 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
5094 return convert_eai_u2w(ret
);
5096 FIXME("getnameinfo() failed, not found during buildtime.\n");
5101 int WINAPI
GetNameInfoW(const SOCKADDR
*sa
, WS_socklen_t salen
, PWCHAR host
,
5102 DWORD hostlen
, PWCHAR serv
, DWORD servlen
, INT flags
)
5105 char *hostA
= NULL
, *servA
= NULL
;
5107 if (host
&& (!(hostA
= HeapAlloc(GetProcessHeap(), 0, hostlen
)))) return EAI_MEMORY
;
5108 if (serv
&& (!(servA
= HeapAlloc(GetProcessHeap(), 0, servlen
))))
5110 HeapFree(GetProcessHeap(), 0, hostA
);
5114 ret
= WS_getnameinfo(sa
, salen
, hostA
, hostlen
, servA
, servlen
, flags
);
5117 if (host
) MultiByteToWideChar(CP_ACP
, 0, hostA
, -1, host
, hostlen
);
5118 if (serv
) MultiByteToWideChar(CP_ACP
, 0, servA
, -1, serv
, servlen
);
5121 HeapFree(GetProcessHeap(), 0, hostA
);
5122 HeapFree(GetProcessHeap(), 0, servA
);
5126 /***********************************************************************
5127 * getservbyport (WS2_32.56)
5129 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
5131 struct WS_servent
* retval
= NULL
;
5132 #ifdef HAVE_GETSERVBYPORT
5133 struct servent
* serv
;
5134 char *proto_str
= NULL
;
5136 if (proto
&& *proto
)
5138 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
5140 EnterCriticalSection( &csWSgetXXXbyYYY
);
5141 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
5142 retval
= WS_dup_se(serv
);
5144 else SetLastError(WSANO_DATA
);
5145 LeaveCriticalSection( &csWSgetXXXbyYYY
);
5146 HeapFree( GetProcessHeap(), 0, proto_str
);
5148 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
5153 /***********************************************************************
5154 * gethostname (WS2_32.57)
5156 int WINAPI
WS_gethostname(char *name
, int namelen
)
5158 TRACE("name %p, len %d\n", name
, namelen
);
5160 if (gethostname(name
, namelen
) == 0)
5162 TRACE("<- '%s'\n", name
);
5165 SetLastError((errno
== EINVAL
) ? WSAEFAULT
: wsaErrno());
5166 TRACE("<- ERROR !\n");
5167 return SOCKET_ERROR
;
5171 /* ------------------------------------- Windows sockets extensions -- *
5173 * ------------------------------------------------------------------- */
5175 /***********************************************************************
5176 * WSAEnumNetworkEvents (WS2_32.36)
5178 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
5182 int errors
[FD_MAX_EVENTS
];
5184 TRACE("%08lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
5186 SERVER_START_REQ( get_socket_event
)
5188 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5189 req
->service
= TRUE
;
5190 req
->c_event
= wine_server_obj_handle( hEvent
);
5191 wine_server_set_reply( req
, errors
, sizeof(errors
) );
5192 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
5197 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
5198 lpEvent
->iErrorCode
[i
] = NtStatusToWSAError(errors
[i
]);
5201 SetLastError(WSAEINVAL
);
5202 return SOCKET_ERROR
;
5205 /***********************************************************************
5206 * WSAEventSelect (WS2_32.39)
5208 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
5212 TRACE("%08lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
5214 SERVER_START_REQ( set_socket_event
)
5216 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5218 req
->event
= wine_server_obj_handle( hEvent
);
5221 ret
= wine_server_call( req
);
5225 SetLastError(WSAEINVAL
);
5226 return SOCKET_ERROR
;
5229 /**********************************************************************
5230 * WSAGetOverlappedResult (WS2_32.40)
5232 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
5233 LPDWORD lpcbTransfer
, BOOL fWait
,
5238 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
5239 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
5241 if ( lpOverlapped
== NULL
)
5243 ERR( "Invalid pointer\n" );
5244 WSASetLastError(WSA_INVALID_PARAMETER
);
5248 status
= lpOverlapped
->Internal
;
5249 if (status
== STATUS_PENDING
)
5253 SetLastError( WSA_IO_INCOMPLETE
);
5257 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
5258 INFINITE
) == WAIT_FAILED
)
5260 status
= lpOverlapped
->Internal
;
5264 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
5267 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
5269 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
5274 /***********************************************************************
5275 * WSAAsyncSelect (WS2_32.101)
5277 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
5281 TRACE("%lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
5283 SERVER_START_REQ( set_socket_event
)
5285 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5288 req
->window
= wine_server_user_handle( hWnd
);
5290 ret
= wine_server_call( req
);
5294 SetLastError(WSAEINVAL
);
5295 return SOCKET_ERROR
;
5298 /***********************************************************************
5299 * WSACreateEvent (WS2_32.31)
5302 WSAEVENT WINAPI
WSACreateEvent(void)
5304 /* Create a manual-reset event, with initial state: unsignaled */
5307 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
5310 /***********************************************************************
5311 * WSACloseEvent (WS2_32.29)
5314 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
5316 TRACE ("event=%p\n", event
);
5318 return CloseHandle(event
);
5321 /***********************************************************************
5322 * WSASocketA (WS2_32.78)
5325 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
5326 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
5327 GROUP g
, DWORD dwFlags
)
5330 WSAPROTOCOL_INFOW info
;
5332 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
5333 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
5335 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
5337 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
5338 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
5339 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
5343 WSASetLastError( WSAEINVAL
);
5344 return SOCKET_ERROR
;
5347 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
5350 /***********************************************************************
5351 * WSASocketW (WS2_32.79)
5354 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
5355 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
5356 GROUP g
, DWORD dwFlags
)
5361 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
5362 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
5365 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
5366 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
5368 /* hack for WSADuplicateSocket */
5369 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
5370 ret
= lpProtocolInfo
->dwCatalogEntryId
;
5371 TRACE("\tgot duplicate %04lx\n", ret
);
5375 /* convert the socket family and type */
5376 af
= convert_af_w2u(af
);
5377 type
= convert_socktype_w2u(type
);
5381 if (af
== FROM_PROTOCOL_INFO
)
5382 af
= lpProtocolInfo
->iAddressFamily
;
5383 if (type
== FROM_PROTOCOL_INFO
)
5384 type
= lpProtocolInfo
->iSocketType
;
5385 if (protocol
== FROM_PROTOCOL_INFO
)
5386 protocol
= lpProtocolInfo
->iProtocol
;
5389 if ( af
== AF_UNSPEC
) /* did they not specify the address family? */
5391 if ((protocol
== IPPROTO_TCP
&& type
== SOCK_STREAM
) ||
5392 (protocol
== IPPROTO_UDP
&& type
== SOCK_DGRAM
))
5398 SetLastError(WSAEPROTOTYPE
);
5399 return INVALID_SOCKET
;
5403 SERVER_START_REQ( create_socket
)
5407 req
->protocol
= protocol
;
5408 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
5409 req
->attributes
= OBJ_INHERIT
;
5410 req
->flags
= dwFlags
;
5411 set_error( wine_server_call( req
) );
5412 ret
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
5417 TRACE("\tcreated %04lx\n", ret
);
5421 if (GetLastError() == WSAEACCES
) /* raw socket denied */
5423 if (type
== SOCK_RAW
)
5424 ERR_(winediag
)("Failed to create a socket of type SOCK_RAW, this requires special permissions.\n");
5426 ERR_(winediag
)("Failed to create socket, this requires special permissions.\n");
5427 SetLastError(WSAESOCKTNOSUPPORT
);
5430 WARN("\t\tfailed!\n");
5431 return INVALID_SOCKET
;
5434 /***********************************************************************
5435 * WSAJoinLeaf (WS2_32.58)
5438 SOCKET WINAPI
WSAJoinLeaf(
5440 const struct WS_sockaddr
*addr
,
5442 LPWSABUF lpCallerData
,
5443 LPWSABUF lpCalleeData
,
5449 return INVALID_SOCKET
;
5452 /***********************************************************************
5453 * __WSAFDIsSet (WS2_32.151)
5455 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
5457 int i
= set
->fd_count
;
5459 TRACE("(%ld,%p(%i))\n", s
, set
, i
);
5462 if (set
->fd_array
[i
] == s
) return 1;
5466 /***********************************************************************
5467 * WSAIsBlocking (WS2_32.114)
5469 BOOL WINAPI
WSAIsBlocking(void)
5471 /* By default WinSock should set all its sockets to non-blocking mode
5472 * and poll in PeekMessage loop when processing "blocking" ones. This
5473 * function is supposed to tell if the program is in this loop. Our
5474 * blocking calls are truly blocking so we always return FALSE.
5476 * Note: It is allowed to call this function without prior WSAStartup().
5483 /***********************************************************************
5484 * WSACancelBlockingCall (WS2_32.113)
5486 INT WINAPI
WSACancelBlockingCall(void)
5492 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
5494 FIXME("How was this called?\n");
5499 /***********************************************************************
5500 * WSASetBlockingHook (WS2_32.109)
5502 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
5504 FARPROC prev
= blocking_hook
;
5505 blocking_hook
= lpBlockFunc
;
5506 TRACE("hook %p\n", lpBlockFunc
);
5511 /***********************************************************************
5512 * WSAUnhookBlockingHook (WS2_32.110)
5514 INT WINAPI
WSAUnhookBlockingHook(void)
5516 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
5521 /* ----------------------------------- end of API stuff */
5523 /* ----------------------------------- helper functions -
5525 * TODO: Merge WS_dup_..() stuff into one function that
5526 * would operate with a generic structure containing internal
5527 * pointers (via a template of some kind).
5530 static int list_size(char** l
, int item_size
)
5535 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
5536 j
+= (i
+ 1) * sizeof(char*); }
5540 static int list_dup(char** l_src
, char** l_to
, int item_size
)
5545 for (i
= 0; l_src
[i
]; i
++) ;
5546 p
= (char *)(l_to
+ i
+ 1);
5547 for (i
= 0; l_src
[i
]; i
++)
5549 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
5550 memcpy(p
, l_src
[i
], count
);
5555 return p
- (char *)l_to
;
5560 /* create a hostent entry
5562 * Creates the entry with enough memory for the name, aliases
5563 * addresses, and the address pointers. Also copies the name
5564 * and sets up all the pointers. If "fill_addresses" is set then
5565 * sufficient memory for the addresses is also allocated and the
5566 * address pointers are set to this memory.
5568 * NOTE: The alias and address lists must be allocated with room
5569 * for the NULL item terminating the list. This is true even if
5570 * the list has no items ("aliases" and "addresses" must be
5571 * at least "1", a truly empty list is invalid).
5573 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int addresses
, int fill_addresses
)
5575 struct WS_hostent
*p_to
;
5578 int size
= (sizeof(struct WS_hostent
) +
5580 sizeof(char *)*aliases
+
5581 sizeof(char *)*addresses
);
5583 /* Allocate enough memory for the addresses */
5585 size
+= sizeof(struct in_addr
)*addresses
;
5587 if (!(p_to
= check_buffer_he(size
))) return NULL
;
5588 memset(p_to
, 0, size
);
5590 p
= (char *)(p_to
+ 1);
5595 p_to
->h_aliases
= (char **)p
;
5596 p
+= sizeof(char *)*aliases
;
5597 p_to
->h_addr_list
= (char **)p
;
5598 p
+= sizeof(char *)*addresses
;
5603 /* NOTE: h_aliases must be filled in manually, leave these
5604 * pointers NULL (already set to NULL by memset earlier).
5607 /* Fill in the list of address pointers */
5608 for (i
= 0; i
< addresses
; i
++)
5609 p_to
->h_addr_list
[i
] = (p
+= sizeof(struct in_addr
));
5610 p
+= sizeof(struct in_addr
);
5615 /* duplicate hostent entry
5616 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
5617 * Ditto for protoent and servent.
5619 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
5621 int addresses
= list_size(p_he
->h_addr_list
, p_he
->h_length
);
5622 int aliases
= list_size(p_he
->h_aliases
, 0);
5623 struct WS_hostent
*p_to
;
5625 p_to
= WS_create_he(p_he
->h_name
, aliases
, addresses
, FALSE
);
5627 if (!p_to
) return NULL
;
5628 p_to
->h_addrtype
= p_he
->h_addrtype
;
5629 p_to
->h_length
= p_he
->h_length
;
5631 list_dup(p_he
->h_aliases
, p_to
->h_aliases
, 0);
5632 list_dup(p_he
->h_addr_list
, p_to
->h_addr_list
, p_he
->h_length
);
5636 /* ----- protoent */
5638 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
)
5641 struct WS_protoent
*p_to
;
5643 int size
= (sizeof(*p_pe
) +
5644 strlen(p_pe
->p_name
) + 1 +
5645 list_size(p_pe
->p_aliases
, 0));
5647 if (!(p_to
= check_buffer_pe(size
))) return NULL
;
5648 p_to
->p_proto
= p_pe
->p_proto
;
5650 p
= (char *)(p_to
+ 1);
5652 strcpy(p
, p_pe
->p_name
);
5655 p_to
->p_aliases
= (char **)p
;
5656 list_dup(p_pe
->p_aliases
, p_to
->p_aliases
, 0);
5662 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
5665 struct WS_servent
*p_to
;
5667 int size
= (sizeof(*p_se
) +
5668 strlen(p_se
->s_proto
) + 1 +
5669 strlen(p_se
->s_name
) + 1 +
5670 list_size(p_se
->s_aliases
, 0));
5672 if (!(p_to
= check_buffer_se(size
))) return NULL
;
5673 p_to
->s_port
= p_se
->s_port
;
5675 p
= (char *)(p_to
+ 1);
5677 strcpy(p
, p_se
->s_name
);
5681 strcpy(p
, p_se
->s_proto
);
5684 p_to
->s_aliases
= (char **)p
;
5685 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
5690 /***********************************************************************
5691 * WSARecv (WS2_32.67)
5693 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5694 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
5695 LPWSAOVERLAPPED lpOverlapped
,
5696 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5698 return WS2_recv_base(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
5699 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
, NULL
);
5702 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5703 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
5704 struct WS_sockaddr
*lpFrom
,
5705 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
5706 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
5707 LPWSABUF lpControlBuffer
)
5709 unsigned int i
, options
;
5711 struct ws2_async
*wsa
;
5712 DWORD timeout_start
= GetTickCount();
5713 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
5715 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
5716 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
5717 (lpFromlen
? *lpFromlen
: -1),
5718 lpOverlapped
, lpCompletionRoutine
);
5720 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
5721 TRACE( "fd=%d, options=%x\n", fd
, options
);
5723 if (fd
== -1) return SOCKET_ERROR
;
5725 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
5731 wsa
->hSocket
= SOCKET2HANDLE(s
);
5732 wsa
->flags
= *lpFlags
;
5733 wsa
->lpFlags
= lpFlags
;
5735 wsa
->addrlen
.ptr
= lpFromlen
;
5736 wsa
->control
= lpControlBuffer
;
5737 wsa
->n_iovecs
= dwBufferCount
;
5738 wsa
->first_iovec
= 0;
5739 for (i
= 0; i
< dwBufferCount
; i
++)
5741 /* check buffer first to trigger write watches */
5742 if (IsBadWritePtr( lpBuffers
[i
].buf
, lpBuffers
[i
].len
))
5747 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
5748 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
5753 n
= WS2_recv( fd
, wsa
);
5756 if (errno
== EINTR
) continue;
5757 if (errno
!= EAGAIN
)
5759 int loc_errno
= errno
;
5761 if (cvalue
) WS_AddCompletion( s
, cvalue
, sock_get_ntstatus(loc_errno
), 0 );
5765 else if (lpNumberOfBytesRecvd
) *lpNumberOfBytesRecvd
= n
;
5767 if ((lpOverlapped
|| lpCompletionRoutine
) &&
5768 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
5770 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
5772 wsa
->user_overlapped
= lpOverlapped
;
5773 wsa
->completion_func
= lpCompletionRoutine
;
5774 release_sock_fd( s
, fd
);
5778 iosb
->u
.Status
= STATUS_PENDING
;
5779 iosb
->Information
= 0;
5781 SERVER_START_REQ( register_async
)
5783 req
->type
= ASYNC_TYPE_READ
;
5784 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
5785 req
->async
.callback
= wine_server_client_ptr( WS2_async_recv
);
5786 req
->async
.iosb
= wine_server_client_ptr( iosb
);
5787 req
->async
.arg
= wine_server_client_ptr( wsa
);
5788 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
5789 req
->async
.cvalue
= cvalue
;
5790 err
= wine_server_call( req
);
5794 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
5795 WSASetLastError( NtStatusToWSAError( err
));
5796 return SOCKET_ERROR
;
5799 iosb
->u
.Status
= STATUS_SUCCESS
;
5800 iosb
->Information
= n
;
5801 if (!wsa
->completion_func
)
5803 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
5804 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
5805 HeapFree( GetProcessHeap(), 0, wsa
);
5807 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
5808 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
5809 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5815 if ( _is_blocking(s
) )
5818 int timeout
= GET_RCVTIMEO(fd
);
5821 timeout
-= GetTickCount() - timeout_start
;
5822 if (timeout
< 0) timeout
= 0;
5826 pfd
.events
= POLLIN
;
5827 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
5829 if (!timeout
|| !poll( &pfd
, 1, timeout
))
5832 /* a timeout is not fatal */
5833 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5839 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5840 err
= WSAEWOULDBLOCK
;
5845 TRACE(" -> %i bytes\n", n
);
5846 HeapFree( GetProcessHeap(), 0, wsa
);
5847 release_sock_fd( s
, fd
);
5848 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5853 HeapFree( GetProcessHeap(), 0, wsa
);
5854 release_sock_fd( s
, fd
);
5855 WARN(" -> ERROR %d\n", err
);
5856 WSASetLastError( err
);
5857 return SOCKET_ERROR
;
5860 /***********************************************************************
5861 * WSARecvFrom (WS2_32.69)
5863 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5864 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
5865 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
5866 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5869 return WS2_recv_base( s
, lpBuffers
, dwBufferCount
,
5870 lpNumberOfBytesRecvd
, lpFlags
,
5872 lpOverlapped
, lpCompletionRoutine
, NULL
);
5875 /***********************************************************************
5876 * WSCInstallProvider (WS2_32.88)
5878 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
5879 LPCWSTR lpszProviderDllPath
,
5880 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
5881 DWORD dwNumberOfEntries
,
5884 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
5885 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
5886 dwNumberOfEntries
, lpErrno
);
5892 /***********************************************************************
5893 * WSCDeinstallProvider (WS2_32.83)
5895 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
5897 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
5903 /***********************************************************************
5904 * WSAAccept (WS2_32.26)
5906 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
5907 LPCONDITIONPROC lpfnCondition
, DWORD_PTR dwCallbackData
)
5910 int ret
= 0, size
= 0;
5911 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
5912 /* QOS SQOS, GQOS; */
5915 SOCKADDR src_addr
, dst_addr
;
5917 TRACE("Socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %ld\n",
5918 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
5921 size
= sizeof(src_addr
);
5922 cs
= WS_accept(s
, &src_addr
, &size
);
5924 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
5926 if (!lpfnCondition
) return cs
;
5928 CallerId
.buf
= (char *)&src_addr
;
5929 CallerId
.len
= sizeof(src_addr
);
5931 CallerData
.buf
= NULL
;
5934 WS_getsockname(cs
, &dst_addr
, &size
);
5936 CalleeId
.buf
= (char *)&dst_addr
;
5937 CalleeId
.len
= sizeof(dst_addr
);
5940 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
5941 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
5946 if (addr
&& addrlen
)
5947 memcpy(addr
, &src_addr
, (*addrlen
> size
) ? size
: *addrlen
);
5950 SERVER_START_REQ( set_socket_deferred
)
5952 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5953 req
->deferred
= wine_server_obj_handle( SOCKET2HANDLE(cs
) );
5954 if ( !wine_server_call_err ( req
) )
5956 SetLastError( WSATRY_AGAIN
);
5957 WS_closesocket( cs
);
5961 return SOCKET_ERROR
;
5964 SetLastError(WSAECONNREFUSED
);
5965 return SOCKET_ERROR
;
5967 FIXME("Unknown return type from Condition function\n");
5968 SetLastError(WSAENOTSOCK
);
5969 return SOCKET_ERROR
;
5973 /***********************************************************************
5974 * WSADuplicateSocketA (WS2_32.32)
5976 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
5980 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
5981 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
5982 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
5983 /* I don't know what the real Windoze does next, this is a hack */
5984 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
5985 * the target use the global duplicate, or we could copy a reference to us to the structure
5986 * and let the target duplicate it from us, but let's do it as simple as possible */
5987 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
5988 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
5989 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
5990 0, FALSE
, DUPLICATE_SAME_ACCESS
);
5991 CloseHandle(hProcess
);
5992 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
5996 /***********************************************************************
5997 * WSADuplicateSocketW (WS2_32.33)
5999 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
6003 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
6005 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
6006 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
6007 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
6008 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
6009 0, FALSE
, DUPLICATE_SAME_ACCESS
);
6010 CloseHandle(hProcess
);
6011 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
6015 /***********************************************************************
6016 * WSAInstallServiceClassA (WS2_32.48)
6018 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
6020 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
6021 WSASetLastError(WSAEACCES
);
6022 return SOCKET_ERROR
;
6025 /***********************************************************************
6026 * WSAInstallServiceClassW (WS2_32.49)
6028 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
6030 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
6031 WSASetLastError(WSAEACCES
);
6032 return SOCKET_ERROR
;
6035 /***********************************************************************
6036 * WSARemoveServiceClass (WS2_32.70)
6038 int WINAPI
WSARemoveServiceClass(LPGUID info
)
6040 FIXME("Request to remove service %p\n",info
);
6041 WSASetLastError(WSATYPE_NOT_FOUND
);
6042 return SOCKET_ERROR
;
6045 /***********************************************************************
6046 * inet_ntop (WS2_32.@)
6048 PCSTR WINAPI
WS_inet_ntop( INT family
, PVOID addr
, PSTR buffer
, SIZE_T len
)
6050 #ifdef HAVE_INET_NTOP
6051 struct WS_in6_addr
*in6
;
6052 struct WS_in_addr
*in
;
6055 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
6058 WSASetLastError( STATUS_INVALID_PARAMETER
);
6067 pdst
= inet_ntop( AF_INET
, &in
->WS_s_addr
, buffer
, len
);
6073 pdst
= inet_ntop( AF_INET6
, in6
->WS_s6_addr
, buffer
, len
);
6077 WSASetLastError( WSAEAFNOSUPPORT
);
6081 if (!pdst
) WSASetLastError( STATUS_INVALID_PARAMETER
);
6084 FIXME( "not supported on this platform\n" );
6085 WSASetLastError( WSAEAFNOSUPPORT
);
6090 /***********************************************************************
6091 * WSAStringToAddressA (WS2_32.80)
6093 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
6095 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
6096 LPSOCKADDR lpAddress
,
6097 LPINT lpAddressLength
)
6100 LPSTR workBuffer
=NULL
,ptrPort
;
6102 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_a(AddressString
), AddressFamily
,
6103 lpProtocolInfo
, lpAddress
, lpAddressLength
);
6105 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
6109 WSASetLastError(WSAEINVAL
);
6110 return SOCKET_ERROR
;
6114 FIXME("ProtocolInfo not implemented.\n");
6116 workBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
6117 strlen(AddressString
) + 1);
6120 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6121 return SOCKET_ERROR
;
6124 strcpy(workBuffer
, AddressString
);
6126 switch(AddressFamily
)
6130 struct in_addr inetaddr
;
6132 /* If lpAddressLength is too small, tell caller the size we need */
6133 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
6135 *lpAddressLength
= sizeof(SOCKADDR_IN
);
6139 *lpAddressLength
= sizeof(SOCKADDR_IN
);
6140 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
6142 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= AF_INET
;
6144 ptrPort
= strchr(workBuffer
, ':');
6147 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= htons(atoi(ptrPort
+1));
6152 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= 0;
6155 if(inet_aton(workBuffer
, &inetaddr
) > 0)
6157 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
6168 struct in6_addr inetaddr
;
6169 /* If lpAddressLength is too small, tell caller the size we need */
6170 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
6172 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
6176 #ifdef HAVE_INET_PTON
6177 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
6178 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
6180 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_family
= WS_AF_INET6
;
6182 /* This one is a bit tricky. An IPv6 address contains colons, so the
6183 * check from IPv4 doesn't work like that. However, IPv6 addresses that
6184 * contain a port are written with braces like [fd12:3456:7890::1]:12345
6185 * so what we will do is to look for ']', check if the next char is a
6186 * colon, and if it is, parse the port as in IPv4. */
6188 ptrPort
= strchr(workBuffer
, ']');
6189 if(ptrPort
&& *(++ptrPort
) == ':')
6191 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= htons(atoi(ptrPort
+1));
6196 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= 0;
6199 if(inet_pton(AF_INET6
, workBuffer
, &inetaddr
) > 0)
6201 memcpy(&((LPSOCKADDR_IN6
)lpAddress
)->sin6_addr
, &inetaddr
,
6202 sizeof(struct in6_addr
));
6206 #endif /* HAVE_INET_PTON */
6212 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
6213 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
6217 HeapFree(GetProcessHeap(), 0, workBuffer
);
6220 WSASetLastError(res
);
6221 return SOCKET_ERROR
;
6224 /***********************************************************************
6225 * WSAStringToAddressW (WS2_32.81)
6227 * Does anybody know if this function allows to use Hebrew/Arabic/Chinese... digits?
6228 * If this should be the case, it would be required to map these digits
6229 * to Unicode digits (0-9) using FoldString first.
6231 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
6233 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
6234 LPSOCKADDR lpAddress
,
6235 LPINT lpAddressLength
)
6238 LPSTR workBuffer
=NULL
;
6239 WSAPROTOCOL_INFOA infoA
;
6240 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
6242 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
6243 lpAddress
, lpAddressLength
);
6245 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
6247 /* if ProtocolInfo is available - convert to ANSI variant */
6250 lpProtoInfoA
= &infoA
;
6251 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
6253 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
6254 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
6256 WSASetLastError( WSAEINVAL
);
6257 return SOCKET_ERROR
;
6263 /* Translate AddressString to ANSI code page - assumes that only
6264 standard digits 0-9 are used with this API call */
6265 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
6266 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
6270 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
6271 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
6272 lpAddress
,lpAddressLength
);
6273 HeapFree( GetProcessHeap(), 0, workBuffer
);
6277 res
= WSA_NOT_ENOUGH_MEMORY
;
6282 WSASetLastError(res
);
6283 return SOCKET_ERROR
;
6286 /***********************************************************************
6287 * WSAAddressToStringA (WS2_32.27)
6289 * See WSAAddressToStringW
6291 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
6292 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
6296 CHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
6299 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
6301 if (!sockaddr
) return SOCKET_ERROR
;
6302 if (!string
|| !lenstr
) return SOCKET_ERROR
;
6304 switch(sockaddr
->sa_family
)
6307 if (len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
6308 sprintf( buffer
, "%u.%u.%u.%u:%u",
6309 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
6310 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
6311 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
6312 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
6313 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
6315 p
= strchr( buffer
, ':' );
6316 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
6321 struct WS_sockaddr_in6
*sockaddr6
= (LPSOCKADDR_IN6
) sockaddr
;
6324 if (len
< sizeof(SOCKADDR_IN6
)) return SOCKET_ERROR
;
6325 if ((sockaddr6
->sin6_port
))
6326 strcpy(buffer
, "[");
6327 if (!WS_inet_ntop(WS_AF_INET6
, &sockaddr6
->sin6_addr
, buffer
+strlen(buffer
), sizeof(buffer
)))
6329 WSASetLastError(WSAEINVAL
);
6330 return SOCKET_ERROR
;
6332 if ((sockaddr6
->sin6_scope_id
))
6333 sprintf(buffer
+strlen(buffer
), "%%%u", sockaddr6
->sin6_scope_id
);
6334 if ((sockaddr6
->sin6_port
))
6335 sprintf(buffer
+strlen(buffer
), "]:%u", ntohs(sockaddr6
->sin6_port
));
6340 WSASetLastError(WSAEINVAL
);
6341 return SOCKET_ERROR
;
6344 size
= strlen( buffer
) + 1;
6349 WSASetLastError(WSAEFAULT
);
6350 return SOCKET_ERROR
;
6354 strcpy( string
, buffer
);
6358 /***********************************************************************
6359 * WSAAddressToStringW (WS2_32.28)
6361 * Convert a sockaddr address into a readable address string.
6364 * sockaddr [I] Pointer to a sockaddr structure.
6365 * len [I] Size of the sockaddr structure.
6366 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
6367 * string [I/O] Pointer to a buffer to receive the address string.
6368 * lenstr [I/O] Size of the receive buffer in WCHARs.
6372 * Failure: SOCKET_ERROR
6375 * The 'info' parameter is ignored.
6377 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
6378 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
6383 WCHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
6386 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
6389 ret
= WSAAddressToStringA(sockaddr
, len
, NULL
, bufAddr
, &size
);
6391 if (ret
) return ret
;
6393 MultiByteToWideChar( CP_ACP
, 0, bufAddr
, size
, buffer
, sizeof( buffer
)/sizeof(WCHAR
));
6398 WSASetLastError(WSAEFAULT
);
6399 return SOCKET_ERROR
;
6403 lstrcpyW( string
, buffer
);
6407 /***********************************************************************
6408 * WSAEnumNameSpaceProvidersA (WS2_32.34)
6410 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
6412 FIXME( "(%p %p) Stub!\n", len
, buffer
);
6416 /***********************************************************************
6417 * WSAEnumNameSpaceProvidersW (WS2_32.35)
6419 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
6421 FIXME( "(%p %p) Stub!\n", len
, buffer
);
6425 /***********************************************************************
6426 * WSAGetQOSByName (WS2_32.41)
6428 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
6430 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
6434 /***********************************************************************
6435 * WSAGetServiceClassInfoA (WS2_32.42)
6437 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
6438 LPWSASERVICECLASSINFOA info
)
6440 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
6442 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6443 return SOCKET_ERROR
;
6446 /***********************************************************************
6447 * WSAGetServiceClassInfoW (WS2_32.43)
6449 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
6450 LPWSASERVICECLASSINFOW info
)
6452 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
6454 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6455 return SOCKET_ERROR
;
6458 /***********************************************************************
6459 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
6461 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
6463 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
6464 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6465 return SOCKET_ERROR
;
6468 /***********************************************************************
6469 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
6471 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
6473 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
6474 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6475 return SOCKET_ERROR
;
6478 /***********************************************************************
6479 * WSALookupServiceBeginA (WS2_32.59)
6481 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
6482 DWORD dwControlFlags
,
6485 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
6487 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6488 return SOCKET_ERROR
;
6491 /***********************************************************************
6492 * WSALookupServiceBeginW (WS2_32.60)
6494 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
6495 DWORD dwControlFlags
,
6498 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
6500 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6501 return SOCKET_ERROR
;
6504 /***********************************************************************
6505 * WSALookupServiceEnd (WS2_32.61)
6507 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
6509 FIXME("(%p) Stub!\n", lookup
);
6513 /***********************************************************************
6514 * WSALookupServiceNextA (WS2_32.62)
6516 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
6518 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
6519 WSASetLastError(WSA_E_NO_MORE
);
6520 return SOCKET_ERROR
;
6523 /***********************************************************************
6524 * WSALookupServiceNextW (WS2_32.63)
6526 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
6528 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
6529 WSASetLastError(WSA_E_NO_MORE
);
6530 return SOCKET_ERROR
;
6533 /***********************************************************************
6534 * WSANtohl (WS2_32.64)
6536 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
6538 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
6540 if (!lphostlong
) return WSAEFAULT
;
6542 *lphostlong
= ntohl( netlong
);
6546 /***********************************************************************
6547 * WSANtohs (WS2_32.65)
6549 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
6551 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
6553 if (!lphostshort
) return WSAEFAULT
;
6555 *lphostshort
= ntohs( netshort
);
6559 /***********************************************************************
6560 * WSAProviderConfigChange (WS2_32.66)
6562 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
6563 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
6565 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
6566 return SOCKET_ERROR
;
6569 /***********************************************************************
6570 * WSARecvDisconnect (WS2_32.68)
6572 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
6574 TRACE( "(0x%04lx %p)\n", s
, disconnectdata
);
6576 return WS_shutdown( s
, 0 );
6579 /***********************************************************************
6580 * WSASetServiceA (WS2_32.76)
6582 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
6584 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
6588 /***********************************************************************
6589 * WSASetServiceW (WS2_32.77)
6591 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
6593 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
6597 /***********************************************************************
6598 * WSCEnableNSProvider (WS2_32.84)
6600 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
6602 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
6606 /***********************************************************************
6607 * WSCGetProviderPath (WS2_32.86)
6609 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
6611 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
6613 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
6615 *errcode
= WSAEINVAL
;
6616 return SOCKET_ERROR
;
6619 /***********************************************************************
6620 * WSCInstallNameSpace (WS2_32.87)
6622 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
6623 DWORD version
, LPGUID provider
)
6625 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
6626 namespace, version
, debugstr_guid(provider
) );
6630 /***********************************************************************
6631 * WSCUnInstallNameSpace (WS2_32.89)
6633 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
6635 FIXME("(%p) Stub!\n", lpProviderId
);
6639 /***********************************************************************
6640 * WSCWriteProviderOrder (WS2_32.91)
6642 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
6644 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);
6648 /***********************************************************************
6649 * WSANSPIoctl (WS2_32.91)
6651 INT WINAPI
WSANSPIoctl( HANDLE hLookup
, DWORD dwControlCode
, LPVOID lpvInBuffer
,
6652 DWORD cbInBuffer
, LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
6653 LPDWORD lpcbBytesReturned
, LPWSACOMPLETION lpCompletion
)
6655 FIXME("(%p, 0x%08x, %p, 0x%08x, %p, 0x%08x, %p, %p) Stub!\n", hLookup
, dwControlCode
,
6656 lpvInBuffer
, cbInBuffer
, lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpCompletion
);
6657 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6658 return SOCKET_ERROR
;