2 * based on Windows Sockets 1.1 specs
4 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
5 * Copyright (C) 2005 Marcus Meissner
6 * Copyright (C) 2006-2008 Kai Blin
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * NOTE: If you make any changes to fix a particular app, make sure
23 * they don't break something else like Netscape or telnet and ftp
24 * clients and servers (www.winsite.com got a lot of those).
28 #include "wine/port.h"
33 #include <sys/types.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 # include <sys/ioctl.h>
40 #ifdef HAVE_SYS_FILIO_H
41 # include <sys/filio.h>
43 #ifdef HAVE_SYS_SOCKIO_H
44 # include <sys/sockio.h>
48 # include <sys/so_ioctl.h>
51 #ifdef HAVE_SYS_PARAM_H
52 # include <sys/param.h>
58 #ifdef HAVE_SYS_WAIT_H
59 # include <sys/wait.h>
64 #ifdef HAVE_SYS_SOCKET_H
65 #include <sys/socket.h>
67 #ifdef HAVE_NETINET_IN_H
68 # include <netinet/in.h>
70 #ifdef HAVE_NETINET_TCP_H
71 # include <netinet/tcp.h>
73 #ifdef HAVE_ARPA_INET_H
74 # include <arpa/inet.h>
79 #ifdef HAVE_SYS_ERRNO_H
80 #include <sys/errno.h>
89 #ifdef HAVE_ARPA_NAMESER_H
90 # include <arpa/nameser.h>
99 #ifdef HAVE_NETIPX_IPX_H
100 # include <netipx/ipx.h>
102 #elif defined(HAVE_LINUX_IPX_H)
103 # ifdef HAVE_ASM_TYPES_H
104 # include <asm/types.h>
106 # ifdef HAVE_LINUX_TYPES_H
107 # include <linux/types.h>
109 # include <linux/ipx.h>
113 #ifdef HAVE_LINUX_IRDA_H
114 # ifdef HAVE_LINUX_TYPES_H
115 # include <linux/types.h>
117 # include <linux/irda.h>
124 #ifdef HAVE_SYS_POLL_H
125 # include <sys/poll.h>
127 #ifdef HAVE_SYS_TIME_H
128 # include <sys/time.h>
131 #define NONAMELESSUNION
132 #define NONAMELESSSTRUCT
133 #include "ntstatus.h"
134 #define WIN32_NO_STATUS
139 #include "winerror.h"
141 #include "winsock2.h"
143 #include "ws2tcpip.h"
149 #include "iphlpapi.h"
150 #include "wine/server.h"
151 #include "wine/debug.h"
152 #include "wine/exception.h"
153 #include "wine/unicode.h"
156 # include "wsnwlink.h"
160 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
161 # define sipx_network sipx_addr.x_net
162 # define sipx_node sipx_addr.x_host.c_host
163 #endif /* __FreeBSD__ */
166 #define INADDR_NONE ~0UL
169 WINE_DEFAULT_DEBUG_CHANNEL(winsock
);
170 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
174 * The actual definition of WSASendTo, wrapped in a different function name
175 * so that internal calls from ws2_32 itself will not trigger programs like
176 * Garena, which hooks WSASendTo/WSARecvFrom calls.
178 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
179 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
180 const struct WS_sockaddr
*to
, int tolen
,
181 LPWSAOVERLAPPED lpOverlapped
,
182 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
185 * Internal fundamental receive function, essentially WSARecvFrom with an
186 * additional parameter to support message control headers.
188 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
189 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
190 struct WS_sockaddr
*lpFrom
,
191 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
192 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
193 LPWSABUF lpControlBuffer
);
195 /* critical section to protect some non-reentrant net function */
196 static CRITICAL_SECTION csWSgetXXXbyYYY
;
197 static CRITICAL_SECTION_DEBUG critsect_debug
=
199 0, 0, &csWSgetXXXbyYYY
,
200 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
201 0, 0, { (DWORD_PTR
)(__FILE__
": csWSgetXXXbyYYY") }
203 static CRITICAL_SECTION csWSgetXXXbyYYY
= { &critsect_debug
, -1, 0, 0, 0, 0 };
205 union generic_unix_sockaddr
207 struct sockaddr addr
;
208 char data
[128]; /* should be big enough for all families */
211 static inline const char *debugstr_sockaddr( const struct WS_sockaddr
*a
)
213 if (!a
) return "(nil)";
214 switch (a
->sa_family
)
217 return wine_dbg_sprintf("{ family AF_INET, address %s, port %d }",
218 inet_ntoa(((const struct sockaddr_in
*)a
)->sin_addr
),
219 ntohs(((const struct sockaddr_in
*)a
)->sin_port
));
224 struct WS_sockaddr_in6
*sin
= (struct WS_sockaddr_in6
*)a
;
226 p
= WS_inet_ntop( WS_AF_INET6
, &sin
->sin6_addr
, buf
, sizeof(buf
) );
228 p
= "(unknown IPv6 address)";
229 return wine_dbg_sprintf("{ family AF_INET6, address %s, port %d }",
230 p
, ntohs(sin
->sin6_port
));
236 memcpy( &addr
, ((const SOCKADDR_IRDA
*)a
)->irdaDeviceID
, sizeof(addr
) );
237 addr
= ntohl( addr
);
238 return wine_dbg_sprintf("{ family AF_IRDA, addr %08x, name %s }",
240 ((const SOCKADDR_IRDA
*)a
)->irdaServiceName
);
243 return wine_dbg_sprintf("{ family %d }", a
->sa_family
);
247 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
248 #define SOCKET2HANDLE(s) ((HANDLE)(s))
249 #define HANDLE2SOCKET(h) ((SOCKET)(h))
251 /****************************************************************
252 * Async IO declarations
253 ****************************************************************/
255 typedef struct ws2_async
259 LPWSAOVERLAPPED user_overlapped
;
260 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func
;
261 IO_STATUS_BLOCK local_iosb
;
262 struct WS_sockaddr
*addr
;
265 int val
; /* for send operations */
266 int *ptr
; /* for recv operations */
271 unsigned int n_iovecs
;
272 unsigned int first_iovec
;
273 struct iovec iovec
[1];
276 typedef struct ws2_accept_async
278 HANDLE listen_socket
;
279 HANDLE accept_socket
;
280 LPOVERLAPPED user_overlapped
;
282 PVOID buf
; /* buffer to write data to */
286 struct ws2_async
*read
;
289 /****************************************************************/
291 /* ----------------------------------- internal data */
293 /* ws_... struct conversion flags */
295 typedef struct /* WSAAsyncSelect() control struct */
297 HANDLE service
, event
, sock
;
303 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
304 #define WS_MAX_UDP_DATAGRAM 1024
305 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
307 /* hostent's, servent's and protent's are stored in one buffer per thread,
308 * as documented on MSDN for the functions that return any of the buffers */
309 struct per_thread_data
312 struct WS_hostent
*he_buffer
;
313 struct WS_servent
*se_buffer
;
314 struct WS_protoent
*pe_buffer
;
320 /* internal: routing description information */
327 static INT num_startup
; /* reference counter */
328 static FARPROC blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
330 /* function prototypes */
331 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int addresses
, int fill_addresses
);
332 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
333 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
);
334 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
336 int WSAIOCTL_GetInterfaceCount(void);
337 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
339 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
, ULONG Information
);
341 #define MAP_OPTION(opt) { WS_##opt, opt }
343 static const int ws_sock_map
[][2] =
345 MAP_OPTION( SO_DEBUG
),
346 MAP_OPTION( SO_ACCEPTCONN
),
347 MAP_OPTION( SO_REUSEADDR
),
348 MAP_OPTION( SO_KEEPALIVE
),
349 MAP_OPTION( SO_DONTROUTE
),
350 MAP_OPTION( SO_BROADCAST
),
351 MAP_OPTION( SO_LINGER
),
352 MAP_OPTION( SO_OOBINLINE
),
353 MAP_OPTION( SO_SNDBUF
),
354 MAP_OPTION( SO_RCVBUF
),
355 MAP_OPTION( SO_ERROR
),
356 MAP_OPTION( SO_TYPE
),
358 MAP_OPTION( SO_RCVTIMEO
),
361 MAP_OPTION( SO_SNDTIMEO
),
365 static const int ws_tcp_map
[][2] =
368 MAP_OPTION( TCP_NODELAY
),
372 static const int ws_ip_map
[][2] =
374 MAP_OPTION( IP_MULTICAST_IF
),
375 MAP_OPTION( IP_MULTICAST_TTL
),
376 MAP_OPTION( IP_MULTICAST_LOOP
),
377 MAP_OPTION( IP_ADD_MEMBERSHIP
),
378 MAP_OPTION( IP_DROP_MEMBERSHIP
),
379 MAP_OPTION( IP_OPTIONS
),
381 MAP_OPTION( IP_HDRINCL
),
383 MAP_OPTION( IP_TOS
),
384 MAP_OPTION( IP_TTL
),
386 MAP_OPTION( IP_PKTINFO
),
390 static const int ws_ipv6_map
[][2] =
392 #ifdef IPV6_ADD_MEMBERSHIP
393 MAP_OPTION( IPV6_ADD_MEMBERSHIP
),
395 #ifdef IPV6_DROP_MEMBERSHIP
396 MAP_OPTION( IPV6_DROP_MEMBERSHIP
),
398 MAP_OPTION( IPV6_MULTICAST_IF
),
399 MAP_OPTION( IPV6_MULTICAST_HOPS
),
400 MAP_OPTION( IPV6_MULTICAST_LOOP
),
401 MAP_OPTION( IPV6_UNICAST_HOPS
),
402 MAP_OPTION( IPV6_V6ONLY
),
405 static const int ws_af_map
[][2] =
407 MAP_OPTION( AF_UNSPEC
),
408 MAP_OPTION( AF_INET
),
409 MAP_OPTION( AF_INET6
),
411 MAP_OPTION( AF_IPX
),
414 MAP_OPTION( AF_IRDA
),
416 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
419 static const int ws_socktype_map
[][2] =
421 MAP_OPTION( SOCK_DGRAM
),
422 MAP_OPTION( SOCK_STREAM
),
423 MAP_OPTION( SOCK_RAW
),
424 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
427 static const int ws_proto_map
[][2] =
429 MAP_OPTION( IPPROTO_IP
),
430 MAP_OPTION( IPPROTO_TCP
),
431 MAP_OPTION( IPPROTO_UDP
),
432 MAP_OPTION( IPPROTO_ICMP
),
433 MAP_OPTION( IPPROTO_IGMP
),
434 MAP_OPTION( IPPROTO_RAW
),
435 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
438 static const int ws_aiflag_map
[][2] =
440 MAP_OPTION( AI_PASSIVE
),
441 MAP_OPTION( AI_CANONNAME
),
442 MAP_OPTION( AI_NUMERICHOST
),
443 MAP_OPTION( AI_ADDRCONFIG
),
446 static const int ws_niflag_map
[][2] =
448 MAP_OPTION( NI_NOFQDN
),
449 MAP_OPTION( NI_NUMERICHOST
),
450 MAP_OPTION( NI_NAMEREQD
),
451 MAP_OPTION( NI_NUMERICSERV
),
452 MAP_OPTION( NI_DGRAM
),
455 static const int ws_eai_map
[][2] =
457 MAP_OPTION( EAI_AGAIN
),
458 MAP_OPTION( EAI_BADFLAGS
),
459 MAP_OPTION( EAI_FAIL
),
460 MAP_OPTION( EAI_FAMILY
),
461 MAP_OPTION( EAI_MEMORY
),
462 /* Note: EAI_NODATA is deprecated, but still
463 * used by Windows and Linux... We map the newer
464 * EAI_NONAME to EAI_NODATA for now until Windows
468 MAP_OPTION( EAI_NODATA
),
471 { WS_EAI_NODATA
, EAI_NONAME
},
474 MAP_OPTION( EAI_SERVICE
),
475 MAP_OPTION( EAI_SOCKTYPE
),
479 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
481 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
482 static inline WSACMSGHDR
*fill_control_message(int level
, int type
, WSACMSGHDR
*current
, ULONG
*maxsize
, void *data
, int len
)
484 ULONG msgsize
= sizeof(WSACMSGHDR
) + WSA_CMSG_ALIGN(len
);
485 char *ptr
= (char *) current
+ sizeof(WSACMSGHDR
);
487 /* Make sure there is at least enough room for this entry */
488 if (msgsize
> *maxsize
)
491 /* Fill in the entry */
492 current
->cmsg_len
= sizeof(WSACMSGHDR
) + len
;
493 current
->cmsg_level
= level
;
494 current
->cmsg_type
= type
;
495 memcpy(ptr
, data
, len
);
496 /* Return the pointer to where next entry should go */
497 return (WSACMSGHDR
*) (ptr
+ WSA_CMSG_ALIGN(len
));
500 static inline int convert_control_headers(struct msghdr
*hdr
, WSABUF
*control
)
503 WSACMSGHDR
*cmsg_win
= (WSACMSGHDR
*) control
->buf
, *ptr
;
504 ULONG ctlsize
= control
->len
;
505 struct cmsghdr
*cmsg_unix
;
508 /* Loop over all the headers, converting as appropriate */
509 for (cmsg_unix
= CMSG_FIRSTHDR(hdr
); cmsg_unix
!= NULL
; cmsg_unix
= CMSG_NXTHDR(hdr
, cmsg_unix
))
511 switch(cmsg_unix
->cmsg_level
)
514 switch(cmsg_unix
->cmsg_type
)
518 /* Convert the Unix IP_PKTINFO structure to the Windows version */
519 struct in_pktinfo
*data_unix
= (struct in_pktinfo
*) CMSG_DATA(cmsg_unix
);
520 struct WS_in_pktinfo data_win
;
522 memcpy(&data_win
.ipi_addr
,&data_unix
->ipi_addr
.s_addr
,4); /* 4 bytes = 32 address bits */
523 data_win
.ipi_ifindex
= data_unix
->ipi_ifindex
;
524 ptr
= fill_control_message(WS_IPPROTO_IP
, WS_IP_PKTINFO
, ptr
, &ctlsize
,
525 (void*)&data_win
, sizeof(data_win
));
526 if (!ptr
) goto error
;
529 FIXME("Unhandled IPPROTO_IP message header type %d\n", cmsg_unix
->cmsg_type
);
534 FIXME("Unhandled message header level %d\n", cmsg_unix
->cmsg_level
);
540 /* Set the length of the returned control headers */
541 control
->len
= (ptr
== NULL
? 0 : (char*)ptr
- (char*)cmsg_win
);
542 return (ptr
!= NULL
);
543 #else /* IP_PKTINFO */
546 #endif /* IP_PKTINFO */
548 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
550 /* ----------------------------------- error handling */
552 static NTSTATUS
sock_get_ntstatus( int err
)
556 case EBADF
: return STATUS_INVALID_HANDLE
;
557 case EBUSY
: return STATUS_DEVICE_BUSY
;
559 case EACCES
: return STATUS_ACCESS_DENIED
;
560 case EFAULT
: return STATUS_NO_MEMORY
;
561 case EINVAL
: return STATUS_INVALID_PARAMETER
;
563 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
564 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
565 case EINPROGRESS
: return STATUS_PENDING
;
566 case EALREADY
: return STATUS_NETWORK_BUSY
;
567 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
568 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
569 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
570 case EPROTONOSUPPORT
:
571 case ESOCKTNOSUPPORT
:
574 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
575 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
576 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
577 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
578 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
579 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
580 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
581 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
582 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
583 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
584 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
586 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
587 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
589 case 0: return STATUS_SUCCESS
;
591 WARN("Unknown errno %d!\n", err
);
592 return STATUS_UNSUCCESSFUL
;
596 static UINT
sock_get_error( int err
)
600 case EINTR
: return WSAEINTR
;
601 case EBADF
: return WSAEBADF
;
603 case EACCES
: return WSAEACCES
;
604 case EFAULT
: return WSAEFAULT
;
605 case EINVAL
: return WSAEINVAL
;
606 case EMFILE
: return WSAEMFILE
;
607 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
608 case EINPROGRESS
: return WSAEINPROGRESS
;
609 case EALREADY
: return WSAEALREADY
;
610 case ENOTSOCK
: return WSAENOTSOCK
;
611 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
612 case EMSGSIZE
: return WSAEMSGSIZE
;
613 case EPROTOTYPE
: return WSAEPROTOTYPE
;
614 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
615 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
616 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
617 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
618 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
619 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
620 case EADDRINUSE
: return WSAEADDRINUSE
;
621 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
622 case ENETDOWN
: return WSAENETDOWN
;
623 case ENETUNREACH
: return WSAENETUNREACH
;
624 case ENETRESET
: return WSAENETRESET
;
625 case ECONNABORTED
: return WSAECONNABORTED
;
627 case ECONNRESET
: return WSAECONNRESET
;
628 case ENOBUFS
: return WSAENOBUFS
;
629 case EISCONN
: return WSAEISCONN
;
630 case ENOTCONN
: return WSAENOTCONN
;
631 case ESHUTDOWN
: return WSAESHUTDOWN
;
632 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
633 case ETIMEDOUT
: return WSAETIMEDOUT
;
634 case ECONNREFUSED
: return WSAECONNREFUSED
;
635 case ELOOP
: return WSAELOOP
;
636 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
637 case EHOSTDOWN
: return WSAEHOSTDOWN
;
638 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
639 case ENOTEMPTY
: return WSAENOTEMPTY
;
641 case EPROCLIM
: return WSAEPROCLIM
;
644 case EUSERS
: return WSAEUSERS
;
647 case EDQUOT
: return WSAEDQUOT
;
650 case ESTALE
: return WSAESTALE
;
653 case EREMOTE
: return WSAEREMOTE
;
656 /* just in case we ever get here and there are no problems */
659 WARN("Unknown errno %d!\n", err
);
660 return WSAEOPNOTSUPP
;
664 static UINT
wsaErrno(void)
666 int loc_errno
= errno
;
667 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
669 return sock_get_error( loc_errno
);
672 /* most ws2 overlapped functions return an ntstatus-based error code */
673 static NTSTATUS
wsaErrStatus(void)
675 int loc_errno
= errno
;
676 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
678 return sock_get_ntstatus(loc_errno
);
681 static UINT
wsaHerrno(int loc_errno
)
683 WARN("h_errno %d.\n", loc_errno
);
687 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
688 case TRY_AGAIN
: return WSATRY_AGAIN
;
689 case NO_RECOVERY
: return WSANO_RECOVERY
;
690 case NO_DATA
: return WSANO_DATA
;
691 case ENOBUFS
: return WSAENOBUFS
;
695 WARN("Unknown h_errno %d!\n", loc_errno
);
696 return WSAEOPNOTSUPP
;
700 static inline DWORD
NtStatusToWSAError( const DWORD status
)
702 /* We only need to cover the status codes set by server async request handling */
706 case STATUS_SUCCESS
: wserr
= 0; break;
707 case STATUS_PENDING
: wserr
= WSA_IO_PENDING
; break;
708 case STATUS_OBJECT_TYPE_MISMATCH
: wserr
= WSAENOTSOCK
; break;
709 case STATUS_INVALID_HANDLE
: wserr
= WSAEBADF
; break;
710 case STATUS_INVALID_PARAMETER
: wserr
= WSAEINVAL
; break;
711 case STATUS_PIPE_DISCONNECTED
: wserr
= WSAESHUTDOWN
; break;
712 case STATUS_NETWORK_BUSY
: wserr
= WSAEALREADY
; break;
713 case STATUS_NETWORK_UNREACHABLE
: wserr
= WSAENETUNREACH
; break;
714 case STATUS_CONNECTION_REFUSED
: wserr
= WSAECONNREFUSED
; break;
715 case STATUS_CONNECTION_DISCONNECTED
: wserr
= WSAENOTCONN
; break;
716 case STATUS_CONNECTION_RESET
: wserr
= WSAECONNRESET
; break;
717 case STATUS_CONNECTION_ABORTED
: wserr
= WSAECONNABORTED
; break;
718 case STATUS_CANCELLED
: wserr
= WSA_OPERATION_ABORTED
; break;
719 case STATUS_ADDRESS_ALREADY_ASSOCIATED
: wserr
= WSAEADDRINUSE
; break;
720 case STATUS_IO_TIMEOUT
:
721 case STATUS_TIMEOUT
: wserr
= WSAETIMEDOUT
; break;
722 case STATUS_NO_MEMORY
: wserr
= WSAEFAULT
; break;
723 case STATUS_ACCESS_DENIED
: wserr
= WSAEACCES
; break;
724 case STATUS_TOO_MANY_OPENED_FILES
: wserr
= WSAEMFILE
; break;
725 case STATUS_CANT_WAIT
: wserr
= WSAEWOULDBLOCK
; break;
726 case STATUS_BUFFER_OVERFLOW
: wserr
= WSAEMSGSIZE
; break;
727 case STATUS_NOT_SUPPORTED
: wserr
= WSAEOPNOTSUPP
; break;
728 case STATUS_HOST_UNREACHABLE
: wserr
= WSAEHOSTUNREACH
; break;
731 wserr
= RtlNtStatusToDosError( status
);
732 FIXME( "Status code %08x converted to DOS error code %x\n", status
, wserr
);
737 /* set last error code from NT status without mapping WSA errors */
738 static inline unsigned int set_error( unsigned int err
)
742 err
= NtStatusToWSAError( err
);
748 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
751 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
756 static inline void release_sock_fd( SOCKET s
, int fd
)
758 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
761 static void _enable_event( HANDLE s
, unsigned int event
,
762 unsigned int sstate
, unsigned int cstate
)
764 SERVER_START_REQ( enable_socket_event
)
766 req
->handle
= wine_server_obj_handle( s
);
768 req
->sstate
= sstate
;
769 req
->cstate
= cstate
;
770 wine_server_call( req
);
775 static int _is_blocking(SOCKET s
)
778 SERVER_START_REQ( get_socket_event
)
780 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
781 req
->service
= FALSE
;
783 wine_server_call( req
);
784 ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
790 static unsigned int _get_sock_mask(SOCKET s
)
793 SERVER_START_REQ( get_socket_event
)
795 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
796 req
->service
= FALSE
;
798 wine_server_call( req
);
805 static void _sync_sock_state(SOCKET s
)
807 /* do a dummy wineserver request in order to let
808 the wineserver run through its select loop once */
809 (void)_is_blocking(s
);
812 static int _get_sock_error(SOCKET s
, unsigned int bit
)
814 int events
[FD_MAX_EVENTS
];
816 SERVER_START_REQ( get_socket_event
)
818 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
819 req
->service
= FALSE
;
821 wine_server_set_reply( req
, events
, sizeof(events
) );
822 wine_server_call( req
);
828 static struct per_thread_data
*get_per_thread_data(void)
830 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
831 /* lazy initialization */
834 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
835 NtCurrentTeb()->WinSockData
= ptb
;
840 static void free_per_thread_data(void)
842 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
846 /* delete scratch buffers */
847 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
848 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
849 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
850 ptb
->he_buffer
= NULL
;
851 ptb
->se_buffer
= NULL
;
852 ptb
->pe_buffer
= NULL
;
854 HeapFree( GetProcessHeap(), 0, ptb
);
855 NtCurrentTeb()->WinSockData
= NULL
;
858 /***********************************************************************
859 * DllMain (WS2_32.init)
861 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
863 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
865 case DLL_PROCESS_ATTACH
:
867 case DLL_PROCESS_DETACH
:
868 free_per_thread_data();
871 case DLL_THREAD_DETACH
:
872 free_per_thread_data();
878 /***********************************************************************
881 * Converts socket flags from Windows format.
882 * Return 1 if converted, 0 if not (error).
884 static int convert_sockopt(INT
*level
, INT
*optname
)
891 for(i
=0; i
<sizeof(ws_sock_map
)/sizeof(ws_sock_map
[0]); i
++) {
892 if( ws_sock_map
[i
][0] == *optname
)
894 *optname
= ws_sock_map
[i
][1];
898 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
901 *level
= IPPROTO_TCP
;
902 for(i
=0; i
<sizeof(ws_tcp_map
)/sizeof(ws_tcp_map
[0]); i
++) {
903 if ( ws_tcp_map
[i
][0] == *optname
)
905 *optname
= ws_tcp_map
[i
][1];
909 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
913 for(i
=0; i
<sizeof(ws_ip_map
)/sizeof(ws_ip_map
[0]); i
++) {
914 if (ws_ip_map
[i
][0] == *optname
)
916 *optname
= ws_ip_map
[i
][1];
920 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
922 case WS_IPPROTO_IPV6
:
923 *level
= IPPROTO_IPV6
;
924 for(i
=0; i
<sizeof(ws_ipv6_map
)/sizeof(ws_ipv6_map
[0]); i
++) {
925 if (ws_ipv6_map
[i
][0] == *optname
)
927 *optname
= ws_ipv6_map
[i
][1];
931 FIXME("Unknown IPPROTO_IPV6 optname 0x%x\n", *optname
);
933 default: FIXME("Unimplemented or unknown socket level\n");
938 /* ----------------------------------- Per-thread info (or per-process?) */
940 static char *strdup_lower(const char *str
)
943 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
947 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
950 else SetLastError(WSAENOBUFS
);
954 static inline int sock_error_p(int s
)
956 unsigned int optval
, optlen
;
958 optlen
= sizeof(optval
);
959 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
960 if (optval
) WARN("\t[%i] error: %d\n", s
, optval
);
964 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
965 * from an fd and return the value converted to milli seconds
966 * or -1 if there is an infinite time out */
967 static inline int get_rcvsnd_timeo( int fd
, int optname
)
970 unsigned int len
= sizeof(tv
);
971 int ret
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
973 ret
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
974 if( ret
<= 0 ) /* tv == {0,0} means infinite time out */
979 /* macro wrappers for portability */
981 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
983 #define GET_RCVTIMEO(fd) (-1)
987 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
989 #define GET_SNDTIMEO(fd) (-1)
992 /* utility: given an fd, will block until one of the events occurs */
993 static inline int do_block( int fd
, int events
, int timeout
)
1001 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
1012 convert_af_w2u(int windowsaf
) {
1015 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
1016 if (ws_af_map
[i
][0] == windowsaf
)
1017 return ws_af_map
[i
][1];
1018 FIXME("unhandled Windows address family %d\n", windowsaf
);
1023 convert_af_u2w(int unixaf
) {
1026 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
1027 if (ws_af_map
[i
][1] == unixaf
)
1028 return ws_af_map
[i
][0];
1029 FIXME("unhandled UNIX address family %d\n", unixaf
);
1034 convert_proto_w2u(int windowsproto
) {
1037 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
1038 if (ws_proto_map
[i
][0] == windowsproto
)
1039 return ws_proto_map
[i
][1];
1040 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
1045 convert_proto_u2w(int unixproto
) {
1048 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
1049 if (ws_proto_map
[i
][1] == unixproto
)
1050 return ws_proto_map
[i
][0];
1051 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
1056 convert_socktype_w2u(int windowssocktype
) {
1059 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
1060 if (ws_socktype_map
[i
][0] == windowssocktype
)
1061 return ws_socktype_map
[i
][1];
1062 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
1067 convert_socktype_u2w(int unixsocktype
) {
1070 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
1071 if (ws_socktype_map
[i
][1] == unixsocktype
)
1072 return ws_socktype_map
[i
][0];
1073 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
1077 /* ----------------------------------- API -----
1079 * Init / cleanup / error checking.
1082 /***********************************************************************
1083 * WSAStartup (WS2_32.115)
1085 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
1087 TRACE("verReq=%x\n", wVersionRequested
);
1089 if (LOBYTE(wVersionRequested
) < 1)
1090 return WSAVERNOTSUPPORTED
;
1092 if (!lpWSAData
) return WSAEINVAL
;
1096 /* that's the whole of the negotiation for now */
1097 lpWSAData
->wVersion
= wVersionRequested
;
1098 /* return winsock information */
1099 lpWSAData
->wHighVersion
= 0x0202;
1100 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
1101 strcpy(lpWSAData
->szSystemStatus
, "Running" );
1102 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
1103 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
1104 /* don't do anything with lpWSAData->lpVendorInfo */
1105 /* (some apps don't allocate the space for this field) */
1107 TRACE("succeeded\n");
1112 /***********************************************************************
1113 * WSACleanup (WS2_32.116)
1115 INT WINAPI
WSACleanup(void)
1121 SetLastError(WSANOTINITIALISED
);
1122 return SOCKET_ERROR
;
1126 /***********************************************************************
1127 * WSAGetLastError (WS2_32.111)
1129 INT WINAPI
WSAGetLastError(void)
1131 return GetLastError();
1134 /***********************************************************************
1135 * WSASetLastError (WS2_32.112)
1137 void WINAPI
WSASetLastError(INT iError
) {
1138 SetLastError(iError
);
1141 static struct WS_hostent
*check_buffer_he(int size
)
1143 struct per_thread_data
* ptb
= get_per_thread_data();
1146 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
1147 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1149 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
1150 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
1151 return ptb
->he_buffer
;
1154 static struct WS_servent
*check_buffer_se(int size
)
1156 struct per_thread_data
* ptb
= get_per_thread_data();
1159 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
1160 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1162 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
1163 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
1164 return ptb
->se_buffer
;
1167 static struct WS_protoent
*check_buffer_pe(int size
)
1169 struct per_thread_data
* ptb
= get_per_thread_data();
1172 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
1173 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1175 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
1176 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
1177 return ptb
->pe_buffer
;
1180 /* ----------------------------------- i/o APIs */
1182 static inline BOOL
supported_pf(int pf
)
1203 /**********************************************************************/
1205 /* Returns the length of the converted address if successful, 0 if it was too small to
1208 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
1209 union generic_unix_sockaddr
*uaddr
)
1211 unsigned int uaddrlen
= 0;
1213 switch (wsaddr
->sa_family
)
1218 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
1219 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
1221 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
1224 uaddrlen
= sizeof(struct sockaddr_ipx
);
1225 memset( uaddr
, 0, uaddrlen
);
1226 uipx
->sipx_family
=AF_IPX
;
1227 uipx
->sipx_port
=wsipx
->sa_socket
;
1228 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
1231 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
1232 #ifdef IPX_FRAME_NONE
1233 uipx
->sipx_type
=IPX_FRAME_NONE
;
1239 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
1240 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
1242 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
1243 * scope_id, one without.
1245 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
1246 uaddrlen
= sizeof(struct sockaddr_in6
);
1247 memset( uaddr
, 0, uaddrlen
);
1248 uin6
->sin6_family
= AF_INET6
;
1249 uin6
->sin6_port
= win6
->sin6_port
;
1250 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
1251 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1252 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
1254 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1257 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
1261 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
1262 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
1264 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
1266 uaddrlen
= sizeof(struct sockaddr_in
);
1267 memset( uaddr
, 0, uaddrlen
);
1268 uin
->sin_family
= AF_INET
;
1269 uin
->sin_port
= win
->sin_port
;
1270 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
1275 struct sockaddr_irda
*uin
= (struct sockaddr_irda
*)uaddr
;
1276 const SOCKADDR_IRDA
*win
= (const SOCKADDR_IRDA
*)wsaddr
;
1278 if (wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1280 uaddrlen
= sizeof(struct sockaddr_irda
);
1281 memset( uaddr
, 0, uaddrlen
);
1282 uin
->sir_family
= AF_IRDA
;
1283 if (!strncmp( win
->irdaServiceName
, "LSAP-SEL", strlen( "LSAP-SEL" ) ))
1285 unsigned int lsap_sel
= 0;
1287 sscanf( win
->irdaServiceName
, "LSAP-SEL%u", &lsap_sel
);
1288 uin
->sir_lsap_sel
= lsap_sel
;
1292 uin
->sir_lsap_sel
= LSAP_ANY
;
1293 memcpy( uin
->sir_name
, win
->irdaServiceName
, 25 );
1295 memcpy( &uin
->sir_addr
, win
->irdaDeviceID
, sizeof(uin
->sir_addr
) );
1299 case WS_AF_UNSPEC
: {
1300 /* Try to determine the needed space by the passed windows sockaddr space */
1301 switch (wsaddrlen
) {
1302 default: /* likely a ipv4 address */
1303 case sizeof(struct WS_sockaddr_in
):
1304 uaddrlen
= sizeof(struct sockaddr_in
);
1307 case sizeof(struct WS_sockaddr_ipx
):
1308 uaddrlen
= sizeof(struct sockaddr_ipx
);
1312 case sizeof(SOCKADDR_IRDA
):
1313 uaddrlen
= sizeof(struct sockaddr_irda
);
1316 case sizeof(struct WS_sockaddr_in6
):
1317 case sizeof(struct WS_sockaddr_in6_old
):
1318 uaddrlen
= sizeof(struct sockaddr_in6
);
1321 memset( uaddr
, 0, uaddrlen
);
1325 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
1331 static BOOL
is_sockaddr_bound(const struct sockaddr
*uaddr
, int uaddrlen
)
1333 switch (uaddr
->sa_family
)
1337 FIXME("don't know how to tell if IPX socket is bound, assuming it is!\n");
1342 static const struct sockaddr_in6 emptyAddr
;
1343 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) uaddr
;
1344 return in6
->sin6_port
|| memcmp(&in6
->sin6_addr
, &emptyAddr
.sin6_addr
, sizeof(struct in6_addr
));
1348 static const struct sockaddr_in emptyAddr
;
1349 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) uaddr
;
1350 return in
->sin_port
|| memcmp(&in
->sin_addr
, &emptyAddr
.sin_addr
, sizeof(struct in_addr
));
1355 FIXME("unknown address family %d\n", uaddr
->sa_family
);
1360 /* Returns 0 if successful, -1 if the buffer is too small */
1361 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
1365 switch(uaddr
->sa_family
)
1370 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
1371 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
1374 switch (*wsaddrlen
) /* how much can we copy? */
1378 *wsaddrlen
= sizeof(*wsipx
);
1379 wsipx
->sa_socket
=uipx
->sipx_port
;
1383 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
1391 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
1397 wsipx
->sa_family
=WS_AF_IPX
;
1409 const struct sockaddr_irda
*uin
= (const struct sockaddr_irda
*)uaddr
;
1410 SOCKADDR_IRDA
*win
= (SOCKADDR_IRDA
*)wsaddr
;
1412 if (*wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1414 win
->irdaAddressFamily
= WS_AF_IRDA
;
1415 memcpy( win
->irdaDeviceID
, &uin
->sir_addr
, sizeof(win
->irdaDeviceID
) );
1416 if (uin
->sir_lsap_sel
!= LSAP_ANY
)
1417 sprintf( win
->irdaServiceName
, "LSAP-SEL%u", uin
->sir_lsap_sel
);
1419 memcpy( win
->irdaServiceName
, uin
->sir_name
,
1420 sizeof(win
->irdaServiceName
) );
1425 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
1426 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
1428 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
1430 win6old
->sin6_family
= WS_AF_INET6
;
1431 win6old
->sin6_port
= uin6
->sin6_port
;
1432 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
1433 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1434 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1435 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
1436 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
1437 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
1438 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
1441 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1443 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1448 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
1449 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
1451 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
1453 win
->sin_family
= WS_AF_INET
;
1454 win
->sin_port
= uin
->sin_port
;
1455 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
1456 memset(win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
1457 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
1461 memset(wsaddr
,0,*wsaddrlen
);
1465 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
1471 /**************************************************************************
1472 * Functions for handling overlapped I/O
1473 **************************************************************************/
1475 /* user APC called upon async completion */
1476 static void WINAPI
ws2_async_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1478 ws2_async
*wsa
= arg
;
1480 if (wsa
->completion_func
) wsa
->completion_func( NtStatusToWSAError(iosb
->u
.Status
),
1481 iosb
->Information
, wsa
->user_overlapped
,
1483 HeapFree( GetProcessHeap(), 0, wsa
);
1486 /***********************************************************************
1487 * WS2_recv (INTERNAL)
1489 * Workhorse for both synchronous and asynchronous recv() operations.
1491 static int WS2_recv( int fd
, struct ws2_async
*wsa
)
1493 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1497 union generic_unix_sockaddr unix_sockaddr
;
1500 hdr
.msg_name
= NULL
;
1504 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
1505 hdr
.msg_name
= &unix_sockaddr
;
1508 hdr
.msg_namelen
= 0;
1510 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1511 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1512 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1513 hdr
.msg_accrights
= NULL
;
1514 hdr
.msg_accrightslen
= 0;
1516 hdr
.msg_control
= pktbuf
;
1517 hdr
.msg_controllen
= sizeof(pktbuf
);
1521 if ( (n
= recvmsg(fd
, &hdr
, wsa
->flags
)) == -1 )
1524 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1527 ERR("Message control headers cannot be properly supported on this system.\n");
1528 wsa
->control
->len
= 0;
1531 if (wsa
->control
&& !convert_control_headers(&hdr
, wsa
->control
))
1533 WARN("Application passed insufficient room for control headers.\n");
1534 *wsa
->lpFlags
|= WS_MSG_CTRUNC
;
1540 /* if this socket is connected and lpFrom is not NULL, Linux doesn't give us
1541 * msg_name and msg_namelen from recvmsg, but it does set msg_namelen to zero.
1543 * quoting linux 2.6 net/ipv4/tcp.c:
1544 * "According to UNIX98, msg_name/msg_namelen are ignored
1545 * on connected socket. I was just happy when found this 8) --ANK"
1547 * likewise MSDN says that lpFrom and lpFromlen are ignored for
1548 * connection-oriented sockets, so don't try to update lpFrom.
1550 if (wsa
->addr
&& hdr
.msg_namelen
)
1551 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, wsa
->addr
, wsa
->addrlen
.ptr
);
1556 /***********************************************************************
1557 * WS2_async_recv (INTERNAL)
1559 * Handler for overlapped recv() operations.
1561 static NTSTATUS
WS2_async_recv( void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
1563 ws2_async
* wsa
= user
;
1568 case STATUS_ALERTED
:
1569 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
1572 result
= WS2_recv( fd
, wsa
);
1573 wine_server_release_fd( wsa
->hSocket
, fd
);
1576 status
= STATUS_SUCCESS
;
1577 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1581 if (errno
== EINTR
|| errno
== EAGAIN
)
1583 status
= STATUS_PENDING
;
1584 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1589 status
= wsaErrStatus();
1594 if (status
!= STATUS_PENDING
)
1596 iosb
->u
.Status
= status
;
1597 iosb
->Information
= result
;
1598 *apc
= ws2_async_apc
;
1603 /* user APC called upon async accept completion */
1604 static void WINAPI
ws2_async_accept_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1606 struct ws2_accept_async
*wsa
= arg
;
1608 HeapFree( GetProcessHeap(), 0, wsa
->read
);
1609 HeapFree( GetProcessHeap(), 0, wsa
);
1612 /***********************************************************************
1613 * WS2_async_accept_recv (INTERNAL)
1615 * This function is used to finish the read part of an accept request. It is
1616 * needed to place the completion on the correct socket (listener).
1618 static NTSTATUS
WS2_async_accept_recv( void *arg
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
, void **apc
)
1621 struct ws2_accept_async
*wsa
= arg
;
1623 status
= WS2_async_recv( wsa
->read
, iosb
, status
, &junk
);
1624 if (status
== STATUS_PENDING
)
1627 if (wsa
->user_overlapped
->hEvent
)
1628 SetEvent(wsa
->user_overlapped
->hEvent
);
1630 WS_AddCompletion( HANDLE2SOCKET(wsa
->listen_socket
), wsa
->cvalue
, iosb
->u
.Status
, iosb
->Information
);
1632 *apc
= ws2_async_accept_apc
;
1636 /***********************************************************************
1637 * WS2_async_accept (INTERNAL)
1639 * This is the function called to satisfy the AcceptEx callback
1641 static NTSTATUS
WS2_async_accept( void *arg
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
, void **apc
)
1643 struct ws2_accept_async
*wsa
= arg
;
1647 TRACE("status: 0x%x listen: %p, accept: %p\n", status
, wsa
->listen_socket
, wsa
->accept_socket
);
1649 if (status
== STATUS_ALERTED
)
1651 SERVER_START_REQ( accept_into_socket
)
1653 req
->lhandle
= wine_server_obj_handle( wsa
->listen_socket
);
1654 req
->ahandle
= wine_server_obj_handle( wsa
->accept_socket
);
1655 status
= wine_server_call( req
);
1659 if (status
== STATUS_CANT_WAIT
)
1660 return STATUS_PENDING
;
1662 if (status
== STATUS_INVALID_HANDLE
)
1664 FIXME("AcceptEx accepting socket closed but request was not cancelled\n");
1665 status
= STATUS_CANCELLED
;
1668 else if (status
== STATUS_HANDLES_CLOSED
)
1669 status
= STATUS_CANCELLED
; /* strange windows behavior */
1671 if (status
!= STATUS_SUCCESS
)
1674 /* WS2 Spec says size param is extra 16 bytes long...what do we put in it? */
1675 addr
= ((char *)wsa
->buf
) + wsa
->data_len
;
1676 len
= wsa
->local_len
- sizeof(int);
1677 WS_getpeername(HANDLE2SOCKET(wsa
->accept_socket
),
1678 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
1681 addr
+= wsa
->local_len
;
1682 len
= wsa
->remote_len
- sizeof(int);
1683 WS_getsockname(HANDLE2SOCKET(wsa
->accept_socket
),
1684 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
1690 SERVER_START_REQ( register_async
)
1692 req
->type
= ASYNC_TYPE_READ
;
1693 req
->async
.handle
= wine_server_obj_handle( wsa
->accept_socket
);
1694 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept_recv
);
1695 req
->async
.iosb
= wine_server_client_ptr( iosb
);
1696 req
->async
.arg
= wine_server_client_ptr( wsa
);
1697 status
= wine_server_call( req
);
1701 if (status
!= STATUS_PENDING
)
1704 return STATUS_SUCCESS
;
1707 iosb
->u
.Status
= status
;
1708 iosb
->Information
= 0;
1710 if (wsa
->user_overlapped
->hEvent
)
1711 SetEvent(wsa
->user_overlapped
->hEvent
);
1713 WS_AddCompletion( HANDLE2SOCKET(wsa
->listen_socket
), wsa
->cvalue
, iosb
->u
.Status
, iosb
->Information
);
1715 *apc
= ws2_async_accept_apc
;
1719 /***********************************************************************
1720 * WS2_send (INTERNAL)
1722 * Workhorse for both synchronous and asynchronous send() operations.
1724 static int WS2_send( int fd
, struct ws2_async
*wsa
)
1727 union generic_unix_sockaddr unix_addr
;
1730 hdr
.msg_name
= NULL
;
1731 hdr
.msg_namelen
= 0;
1735 hdr
.msg_name
= &unix_addr
;
1736 hdr
.msg_namelen
= ws_sockaddr_ws2u( wsa
->addr
, wsa
->addrlen
.val
, &unix_addr
);
1737 if ( !hdr
.msg_namelen
)
1743 #if defined(HAVE_IPX) && defined(SOL_IPX)
1744 if(wsa
->addr
->sa_family
== WS_AF_IPX
)
1746 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
1748 unsigned int len
=sizeof(int);
1750 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1751 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1752 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1753 * ipx type in the sockaddr_opx structure with the stored value.
1755 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
1756 uipx
->sipx_type
= val
;
1761 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1762 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1763 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1764 hdr
.msg_accrights
= NULL
;
1765 hdr
.msg_accrightslen
= 0;
1767 hdr
.msg_control
= NULL
;
1768 hdr
.msg_controllen
= 0;
1772 ret
= sendmsg(fd
, &hdr
, wsa
->flags
);
1776 while (wsa
->first_iovec
< wsa
->n_iovecs
&& wsa
->iovec
[wsa
->first_iovec
].iov_len
<= n
)
1777 n
-= wsa
->iovec
[wsa
->first_iovec
++].iov_len
;
1778 if (wsa
->first_iovec
< wsa
->n_iovecs
)
1780 wsa
->iovec
[wsa
->first_iovec
].iov_base
= (char*)wsa
->iovec
[wsa
->first_iovec
].iov_base
+ n
;
1781 wsa
->iovec
[wsa
->first_iovec
].iov_len
-= n
;
1787 /***********************************************************************
1788 * WS2_async_send (INTERNAL)
1790 * Handler for overlapped send() operations.
1792 static NTSTATUS
WS2_async_send(void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
1794 ws2_async
* wsa
= user
;
1799 case STATUS_ALERTED
:
1800 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
1803 /* check to see if the data is ready (non-blocking) */
1804 result
= WS2_send( fd
, wsa
);
1805 wine_server_release_fd( wsa
->hSocket
, fd
);
1809 if (wsa
->first_iovec
< wsa
->n_iovecs
)
1810 status
= STATUS_PENDING
;
1812 status
= STATUS_SUCCESS
;
1814 iosb
->Information
+= result
;
1816 else if (errno
== EINTR
|| errno
== EAGAIN
)
1818 status
= STATUS_PENDING
;
1822 status
= wsaErrStatus();
1826 if (status
!= STATUS_PENDING
)
1828 iosb
->u
.Status
= status
;
1829 *apc
= ws2_async_apc
;
1834 /***********************************************************************
1835 * WS2_async_shutdown (INTERNAL)
1837 * Handler for shutdown() operations on overlapped sockets.
1839 static NTSTATUS
WS2_async_shutdown( void* user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
, void **apc
)
1841 ws2_async
* wsa
= user
;
1846 case STATUS_ALERTED
:
1847 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
1850 switch ( wsa
->type
)
1852 case ASYNC_TYPE_READ
: err
= shutdown( fd
, 0 ); break;
1853 case ASYNC_TYPE_WRITE
: err
= shutdown( fd
, 1 ); break;
1855 status
= err
? wsaErrStatus() : STATUS_SUCCESS
;
1856 wine_server_release_fd( wsa
->hSocket
, fd
);
1859 iosb
->u
.Status
= status
;
1860 iosb
->Information
= 0;
1861 *apc
= ws2_async_apc
;
1865 /***********************************************************************
1866 * WS2_register_async_shutdown (INTERNAL)
1868 * Helper function for WS_shutdown() on overlapped sockets.
1870 static int WS2_register_async_shutdown( SOCKET s
, int type
)
1872 struct ws2_async
*wsa
;
1875 TRACE("s %ld type %d\n", s
, type
);
1877 wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
1881 wsa
->hSocket
= SOCKET2HANDLE(s
);
1883 wsa
->completion_func
= NULL
;
1885 SERVER_START_REQ( register_async
)
1888 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
1889 req
->async
.callback
= wine_server_client_ptr( WS2_async_shutdown
);
1890 req
->async
.iosb
= wine_server_client_ptr( &wsa
->local_iosb
);
1891 req
->async
.arg
= wine_server_client_ptr( wsa
);
1892 req
->async
.cvalue
= 0;
1893 status
= wine_server_call( req
);
1897 if (status
!= STATUS_PENDING
)
1899 HeapFree( GetProcessHeap(), 0, wsa
);
1900 return NtStatusToWSAError( status
);
1905 /***********************************************************************
1908 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
,
1915 TRACE("socket %04lx\n", s
);
1916 is_blocking
= _is_blocking(s
);
1919 /* try accepting first (if there is a deferred connection) */
1920 SERVER_START_REQ( accept_socket
)
1922 req
->lhandle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1923 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
1924 req
->attributes
= OBJ_INHERIT
;
1925 status
= wine_server_call( req
);
1926 as
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
1931 if (addr
) WS_getpeername(as
, addr
, addrlen32
);
1934 if (is_blocking
&& status
== STATUS_CANT_WAIT
)
1936 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1938 do_block(fd
, POLLIN
, -1);
1939 _sync_sock_state(s
); /* let wineserver notice connection */
1940 release_sock_fd( s
, fd
);
1942 } while (is_blocking
&& status
== STATUS_CANT_WAIT
);
1945 return INVALID_SOCKET
;
1948 /***********************************************************************
1951 static BOOL WINAPI
WS2_AcceptEx(SOCKET listener
, SOCKET acceptor
, PVOID dest
, DWORD dest_len
,
1952 DWORD local_addr_len
, DWORD rem_addr_len
, LPDWORD received
,
1953 LPOVERLAPPED overlapped
)
1956 struct ws2_accept_async
*wsa
;
1958 ULONG_PTR cvalue
= (overlapped
&& ((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)overlapped
: 0;
1960 TRACE("(%lx, %lx, %p, %d, %d, %d, %p, %p)\n", listener
, acceptor
, dest
, dest_len
, local_addr_len
,
1961 rem_addr_len
, received
, overlapped
);
1965 SetLastError(WSAEINVAL
);
1971 SetLastError(WSA_INVALID_PARAMETER
);
1975 fd
= get_sock_fd( listener
, FILE_READ_DATA
, NULL
);
1978 SetLastError(WSAENOTSOCK
);
1981 release_sock_fd( listener
, fd
);
1983 fd
= get_sock_fd( acceptor
, FILE_READ_DATA
, NULL
);
1986 SetLastError(WSAEINVAL
);
1989 release_sock_fd( acceptor
, fd
);
1991 wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
1994 SetLastError(WSAEFAULT
);
1998 wsa
->listen_socket
= SOCKET2HANDLE(listener
);
1999 wsa
->accept_socket
= SOCKET2HANDLE(acceptor
);
2000 wsa
->user_overlapped
= overlapped
;
2001 wsa
->cvalue
= cvalue
;
2003 wsa
->data_len
= dest_len
;
2004 wsa
->local_len
= local_addr_len
;
2005 wsa
->remote_len
= rem_addr_len
;
2010 /* set up a read request if we need it */
2011 wsa
->read
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[1]) );
2014 HeapFree( GetProcessHeap(), 0, wsa
);
2015 SetLastError(WSAEFAULT
);
2019 wsa
->read
->hSocket
= wsa
->accept_socket
;
2020 wsa
->read
->flags
= 0;
2021 wsa
->read
->lpFlags
= &wsa
->read
->flags
;
2022 wsa
->read
->addr
= NULL
;
2023 wsa
->read
->addrlen
.ptr
= NULL
;
2024 wsa
->read
->control
= NULL
;
2025 wsa
->read
->n_iovecs
= 1;
2026 wsa
->read
->first_iovec
= 0;
2027 wsa
->read
->iovec
[0].iov_base
= wsa
->buf
;
2028 wsa
->read
->iovec
[0].iov_len
= wsa
->data_len
;
2031 SERVER_START_REQ( register_async
)
2033 req
->type
= ASYNC_TYPE_READ
;
2034 req
->async
.handle
= wine_server_obj_handle( SOCKET2HANDLE(listener
) );
2035 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept
);
2036 req
->async
.iosb
= wine_server_client_ptr( overlapped
);
2037 req
->async
.arg
= wine_server_client_ptr( wsa
);
2038 /* We don't set event or completion since we may also have to read */
2039 status
= wine_server_call( req
);
2043 if(status
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
2045 SetLastError( NtStatusToWSAError(status
) );
2049 /***********************************************************************
2050 * GetAcceptExSockaddrs
2052 static void WINAPI
WS2_GetAcceptExSockaddrs(PVOID buffer
, DWORD data_size
, DWORD local_size
, DWORD remote_size
,
2053 struct WS_sockaddr
**local_addr
, LPINT local_addr_len
,
2054 struct WS_sockaddr
**remote_addr
, LPINT remote_addr_len
)
2056 char *cbuf
= buffer
;
2057 TRACE("(%p, %d, %d, %d, %p, %p, %p, %p)\n", buffer
, data_size
, local_size
, remote_size
, local_addr
,
2058 local_addr_len
, remote_addr
, remote_addr_len
);
2061 *local_addr_len
= *(int *) cbuf
;
2062 *local_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
2066 *remote_addr_len
= *(int *) cbuf
;
2067 *remote_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
2070 /***********************************************************************
2073 * Perform a receive operation that is capable of returning message
2074 * control headers. It is important to note that the WSAMSG parameter
2075 * must remain valid throughout the operation, even when an overlapped
2076 * receive is performed.
2078 static int WINAPI
WS2_WSARecvMsg( SOCKET s
, LPWSAMSG msg
, LPDWORD lpNumberOfBytesRecvd
,
2079 LPWSAOVERLAPPED lpOverlapped
,
2080 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2084 SetLastError( WSAEFAULT
);
2085 return SOCKET_ERROR
;
2088 return WS2_recv_base( s
, msg
->lpBuffers
, msg
->dwBufferCount
, lpNumberOfBytesRecvd
,
2089 &msg
->dwFlags
, msg
->name
, &msg
->namelen
,
2090 lpOverlapped
, lpCompletionRoutine
, &msg
->Control
);
2093 /***********************************************************************
2096 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
2098 int fd
= get_sock_fd( s
, 0, NULL
);
2099 int res
= SOCKET_ERROR
;
2101 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
2105 if (!name
|| (name
->sa_family
&& !supported_pf(name
->sa_family
)))
2107 SetLastError(WSAEAFNOSUPPORT
);
2111 union generic_unix_sockaddr uaddr
;
2112 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
2115 SetLastError(WSAEFAULT
);
2120 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) &uaddr
;
2121 if (name
->sa_family
== WS_AF_INET6
&&
2122 !memcmp(&in6
->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
)))
2125 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
)) == -1)
2127 release_sock_fd( s
, fd
);
2128 SetLastError(WSAEAFNOSUPPORT
);
2129 return SOCKET_ERROR
;
2133 if (name
->sa_family
== WS_AF_INET
)
2135 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
2136 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
2138 /* Trying to bind to the default host interface, using
2139 * INADDR_ANY instead*/
2140 WARN("Trying to bind to magic IP address, using "
2141 "INADDR_ANY instead.\n");
2142 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_ANY
);
2145 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
2147 int loc_errno
= errno
;
2148 WARN("\tfailure - errno = %i\n", errno
);
2153 SetLastError(WSAENOTSOCK
);
2156 SetLastError(WSAEINVAL
);
2159 SetLastError(wsaErrno());
2165 res
=0; /* success */
2169 release_sock_fd( s
, fd
);
2174 /***********************************************************************
2175 * closesocket (WS2_32.3)
2177 int WINAPI
WS_closesocket(SOCKET s
)
2179 TRACE("socket %04lx\n", s
);
2180 if (CloseHandle(SOCKET2HANDLE(s
))) return 0;
2181 return SOCKET_ERROR
;
2184 static int do_connect(int fd
, const struct WS_sockaddr
* name
, int namelen
)
2186 union generic_unix_sockaddr uaddr
;
2187 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
2192 if (name
->sa_family
== WS_AF_INET
)
2194 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
2195 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
2197 /* Trying to connect to magic replace-loopback address,
2198 * assuming we really want to connect to localhost */
2199 TRACE("Trying to connect to magic IP address, using "
2200 "INADDR_LOOPBACK instead.\n");
2201 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_LOOPBACK
);
2205 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
2211 /***********************************************************************
2212 * connect (WS2_32.4)
2214 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
2216 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2218 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
2222 int ret
= do_connect(fd
, name
, namelen
);
2224 goto connect_success
;
2226 if (ret
== WSAEINPROGRESS
)
2228 /* tell wineserver that a connection is in progress */
2229 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2231 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
2232 if (_is_blocking(s
))
2236 do_block(fd
, POLLIN
| POLLOUT
, -1);
2237 _sync_sock_state(s
); /* let wineserver notice connection */
2238 /* retrieve any error codes from it */
2239 result
= _get_sock_error(s
, FD_CONNECT_BIT
);
2241 SetLastError(NtStatusToWSAError(result
));
2244 goto connect_success
;
2249 SetLastError(WSAEWOULDBLOCK
);
2256 release_sock_fd( s
, fd
);
2258 return SOCKET_ERROR
;
2261 release_sock_fd( s
, fd
);
2262 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2263 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
2264 FD_CONNECT
|FD_WINE_LISTENING
);
2268 /***********************************************************************
2269 * WSAConnect (WS2_32.30)
2271 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
2272 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
2273 LPQOS lpSQOS
, LPQOS lpGQOS
)
2275 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
2276 FIXME("unsupported parameters!\n");
2277 return WS_connect( s
, name
, namelen
);
2280 /***********************************************************************
2283 static BOOL WINAPI
WS2_ConnectEx(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
2284 PVOID sendBuf
, DWORD sendBufLen
, LPDWORD sent
, LPOVERLAPPED ov
)
2286 int fd
, ret
, status
;
2290 SetLastError( ERROR_INVALID_PARAMETER
);
2294 fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2297 SetLastError( WSAENOTSOCK
);
2301 TRACE("socket %04lx, ptr %p %s, length %d, sendptr %p, len %d, ov %p\n",
2302 s
, name
, debugstr_sockaddr(name
), namelen
, sendBuf
, sendBufLen
, ov
);
2304 /* FIXME: technically the socket has to be bound */
2305 ret
= do_connect(fd
, name
, namelen
);
2310 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2311 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
2312 FD_CONNECT
|FD_WINE_LISTENING
);
2314 wsabuf
.len
= sendBufLen
;
2315 wsabuf
.buf
= (char*) sendBuf
;
2317 /* WSASend takes care of completion if need be */
2318 if (WSASend(s
, &wsabuf
, sendBuf
? 1 : 0, sent
, 0, ov
, NULL
) != SOCKET_ERROR
)
2319 goto connection_success
;
2321 else if (ret
== WSAEINPROGRESS
)
2323 struct ws2_async
*wsa
;
2324 ULONG_PTR cvalue
= (((ULONG_PTR
)ov
->hEvent
& 1) == 0) ? (ULONG_PTR
)ov
: 0;
2326 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2328 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
2330 /* Indirectly call WSASend */
2331 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) )))
2333 SetLastError(WSAEFAULT
);
2337 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)ov
;
2338 iosb
->u
.Status
= STATUS_PENDING
;
2339 iosb
->Information
= 0;
2341 wsa
->hSocket
= SOCKET2HANDLE(s
);
2343 wsa
->addrlen
.val
= 0;
2345 wsa
->lpFlags
= &wsa
->flags
;
2346 wsa
->control
= NULL
;
2347 wsa
->n_iovecs
= sendBuf
? 1 : 0;
2348 wsa
->first_iovec
= 0;
2349 wsa
->completion_func
= NULL
;
2350 wsa
->iovec
[0].iov_base
= sendBuf
;
2351 wsa
->iovec
[0].iov_len
= sendBufLen
;
2353 SERVER_START_REQ( register_async
)
2355 req
->type
= ASYNC_TYPE_WRITE
;
2356 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
2357 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
2358 req
->async
.iosb
= wine_server_client_ptr( iosb
);
2359 req
->async
.arg
= wine_server_client_ptr( wsa
);
2360 req
->async
.event
= wine_server_obj_handle( ov
->hEvent
);
2361 req
->async
.cvalue
= cvalue
;
2362 status
= wine_server_call( req
);
2366 if (status
!= STATUS_PENDING
) HeapFree(GetProcessHeap(), 0, wsa
);
2368 /* If the connect already failed */
2369 if (status
== STATUS_PIPE_DISCONNECTED
)
2370 status
= _get_sock_error(s
, FD_CONNECT_BIT
);
2371 SetLastError( NtStatusToWSAError(status
) );
2379 release_sock_fd( s
, fd
);
2383 release_sock_fd( s
, fd
);
2388 /***********************************************************************
2389 * getpeername (WS2_32.5)
2391 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
2396 TRACE("socket: %04lx, ptr %p, len %08x\n", s
, name
, namelen
?*namelen
:0);
2398 fd
= get_sock_fd( s
, 0, NULL
);
2403 union generic_unix_sockaddr uaddr
;
2404 unsigned int uaddrlen
= sizeof(uaddr
);
2406 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) == 0)
2408 if (!name
|| !namelen
)
2409 SetLastError(WSAEFAULT
);
2410 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
2411 /* The buffer was too small */
2412 SetLastError(WSAEFAULT
);
2417 SetLastError(wsaErrno());
2418 release_sock_fd( s
, fd
);
2423 /***********************************************************************
2424 * getsockname (WS2_32.6)
2426 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
2431 TRACE("socket: %04lx, ptr %p, len %8x\n", s
, name
, *namelen
);
2433 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
2434 if( (name
== NULL
) || (namelen
== NULL
) )
2436 SetLastError( WSAEFAULT
);
2437 return SOCKET_ERROR
;
2440 fd
= get_sock_fd( s
, 0, NULL
);
2445 union generic_unix_sockaddr uaddr
;
2446 unsigned int uaddrlen
= sizeof(uaddr
);
2448 if (getsockname(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
2450 SetLastError(wsaErrno());
2452 else if (!is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
2454 SetLastError(WSAEINVAL
);
2456 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
2458 /* The buffer was too small */
2459 SetLastError(WSAEFAULT
);
2465 release_sock_fd( s
, fd
);
2470 /***********************************************************************
2471 * getsockopt (WS2_32.7)
2473 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
2474 INT optname
, char *optval
, INT
*optlen
)
2479 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
2480 s
, level
, optname
, optval
, *optlen
);
2488 /* Handle common cases. The special cases are below, sorted
2490 case WS_SO_ACCEPTCONN
:
2491 case WS_SO_BROADCAST
:
2494 case WS_SO_KEEPALIVE
:
2495 case WS_SO_OOBINLINE
:
2497 case WS_SO_REUSEADDR
:
2500 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2501 return SOCKET_ERROR
;
2502 convert_sockopt(&level
, &optname
);
2503 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2505 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2508 release_sock_fd( s
, fd
);
2511 case WS_SO_DONTLINGER
:
2513 struct linger lingval
;
2514 unsigned int len
= sizeof(struct linger
);
2516 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
2518 SetLastError(WSAEFAULT
);
2519 return SOCKET_ERROR
;
2521 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2522 return SOCKET_ERROR
;
2524 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
2526 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2531 *(BOOL
*)optval
= (lingval
.l_onoff
) ? FALSE
: TRUE
;
2532 *optlen
= sizeof(BOOL
);
2535 release_sock_fd( s
, fd
);
2539 /* As mentioned in setsockopt, Windows ignores this, so we
2540 * always return true here */
2541 case WS_SO_DONTROUTE
:
2542 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
2544 SetLastError(WSAEFAULT
);
2545 return SOCKET_ERROR
;
2547 *(BOOL
*)optval
= TRUE
;
2548 *optlen
= sizeof(BOOL
);
2553 struct linger lingval
;
2554 unsigned int len
= sizeof(struct linger
);
2556 /* struct linger and LINGER have different sizes */
2557 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
2559 SetLastError(WSAEFAULT
);
2560 return SOCKET_ERROR
;
2562 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2563 return SOCKET_ERROR
;
2565 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
2567 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2572 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
2573 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
2574 *optlen
= sizeof(struct linger
);
2577 release_sock_fd( s
, fd
);
2581 case WS_SO_MAX_MSG_SIZE
:
2582 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
2584 SetLastError(WSAEFAULT
);
2585 return SOCKET_ERROR
;
2587 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
2588 *(int *)optval
= 65507;
2589 *optlen
= sizeof(int);
2592 /* SO_OPENTYPE does not require a valid socket handle. */
2593 case WS_SO_OPENTYPE
:
2594 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
2596 SetLastError(WSAEFAULT
);
2597 return SOCKET_ERROR
;
2599 *(int *)optval
= get_per_thread_data()->opentype
;
2600 *optlen
= sizeof(int);
2601 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
2605 case WS_SO_RCVTIMEO
:
2608 case WS_SO_SNDTIMEO
:
2610 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
2613 unsigned int len
= sizeof(struct timeval
);
2615 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
2617 SetLastError(WSAEFAULT
);
2618 return SOCKET_ERROR
;
2620 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2621 return SOCKET_ERROR
;
2623 convert_sockopt(&level
, &optname
);
2624 if (getsockopt(fd
, level
, optname
, &tv
, &len
) != 0 )
2626 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2631 *(int *)optval
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
2632 *optlen
= sizeof(int);
2635 release_sock_fd( s
, fd
);
2640 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
2641 SetLastError(WSAENOPROTOOPT
);
2642 return SOCKET_ERROR
;
2643 } /* end switch(optname) */
2644 }/* end case WS_SOL_SOCKET */
2648 struct WS_sockaddr_ipx addr
;
2649 IPX_ADDRESS_DATA
*data
;
2654 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
2656 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (unsigned int*)optlen
) == -1)
2663 socklen_t len
=sizeof(struct ipx
);
2664 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
2667 *optval
= (int)val
.ipx_pt
;
2670 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
2671 release_sock_fd( s
, fd
);
2676 * On a Win2000 system with one network card there are usually
2677 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
2678 * Using this call you can then retrieve info about this all.
2679 * In case of Linux it is a bit different. Usually you have
2680 * only "one" device active and further it is not possible to
2681 * query things like the linkspeed.
2683 FIXME("IPX_ADDRESS\n");
2684 namelen
= sizeof(struct WS_sockaddr_ipx
);
2685 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
2686 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
2688 data
= (IPX_ADDRESS_DATA
*)optval
;
2689 memcpy(data
->nodenum
,addr
.sa_nodenum
,sizeof(data
->nodenum
));
2690 memcpy(data
->netnum
,addr
.sa_netnum
,sizeof(data
->netnum
));
2691 data
->adapternum
= 0;
2692 data
->wan
= FALSE
; /* We are not on a wan for now .. */
2693 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
2694 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
2695 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
2696 * note 1MB = 1000kB in this case */
2699 case IPX_MAX_ADAPTER_NUM
:
2700 FIXME("IPX_MAX_ADAPTER_NUM\n");
2701 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
2705 FIXME("IPX optname:%x\n", optname
);
2706 return SOCKET_ERROR
;
2707 }/* end switch(optname) */
2708 } /* end case NSPROTO_IPX */
2715 case WS_IRLMP_ENUMDEVICES
:
2717 static const int MAX_IRDA_DEVICES
= 10;
2718 char buf
[sizeof(struct irda_device_list
) +
2719 (MAX_IRDA_DEVICES
- 1) * sizeof(struct irda_device_info
)];
2721 socklen_t len
= sizeof(buf
);
2723 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2724 return SOCKET_ERROR
;
2725 res
= getsockopt( fd
, SOL_IRLMP
, IRLMP_ENUMDEVICES
, buf
, &len
);
2728 SetLastError(wsaErrno());
2729 return SOCKET_ERROR
;
2733 struct irda_device_list
*src
= (struct irda_device_list
*)buf
;
2734 DEVICELIST
*dst
= (DEVICELIST
*)optval
;
2735 INT needed
= sizeof(DEVICELIST
), i
;
2738 needed
+= (src
->len
- 1) * sizeof(IRDA_DEVICE_INFO
);
2739 if (*optlen
< needed
)
2741 SetLastError(WSAEFAULT
);
2742 return SOCKET_ERROR
;
2745 TRACE("IRLMP_ENUMDEVICES: %d devices found:\n", src
->len
);
2746 dst
->numDevice
= src
->len
;
2747 for (i
= 0; i
< src
->len
; i
++)
2749 TRACE("saddr = %08x, daddr = %08x, info = %s, hints = %02x%02x\n",
2750 src
->dev
[i
].saddr
, src
->dev
[i
].daddr
,
2751 src
->dev
[i
].info
, src
->dev
[i
].hints
[0],
2752 src
->dev
[i
].hints
[1]);
2753 memcpy( dst
->Device
[i
].irdaDeviceID
,
2755 sizeof(dst
->Device
[i
].irdaDeviceID
) ) ;
2756 memcpy( dst
->Device
[i
].irdaDeviceName
,
2758 sizeof(dst
->Device
[i
].irdaDeviceName
) ) ;
2759 memcpy( &dst
->Device
[i
].irdaDeviceHints1
,
2760 &src
->dev
[i
].hints
[0],
2761 sizeof(dst
->Device
[i
].irdaDeviceHints1
) ) ;
2762 memcpy( &dst
->Device
[i
].irdaDeviceHints2
,
2763 &src
->dev
[i
].hints
[1],
2764 sizeof(dst
->Device
[i
].irdaDeviceHints2
) ) ;
2765 dst
->Device
[i
].irdaCharSet
= src
->dev
[i
].charset
;
2771 FIXME("IrDA optname:0x%x\n", optname
);
2772 return SOCKET_ERROR
;
2774 break; /* case WS_SOL_IRLMP */
2777 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
2778 case WS_IPPROTO_TCP
:
2781 case WS_TCP_NODELAY
:
2782 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2783 return SOCKET_ERROR
;
2784 convert_sockopt(&level
, &optname
);
2785 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2787 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2790 release_sock_fd( s
, fd
);
2793 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
2794 return SOCKET_ERROR
;
2799 case WS_IP_ADD_MEMBERSHIP
:
2800 case WS_IP_DROP_MEMBERSHIP
:
2804 case WS_IP_MULTICAST_IF
:
2805 case WS_IP_MULTICAST_LOOP
:
2806 case WS_IP_MULTICAST_TTL
:
2813 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2814 return SOCKET_ERROR
;
2815 convert_sockopt(&level
, &optname
);
2816 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2818 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2821 release_sock_fd( s
, fd
);
2823 case WS_IP_DONTFRAGMENT
:
2824 FIXME("WS_IP_DONTFRAGMENT is always false!\n");
2825 *(BOOL
*)optval
= FALSE
;
2828 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
2829 return SOCKET_ERROR
;
2831 case WS_IPPROTO_IPV6
:
2834 #ifdef IPV6_ADD_MEMBERSHIP
2835 case WS_IPV6_ADD_MEMBERSHIP
:
2837 #ifdef IPV6_DROP_MEMBERSHIP
2838 case WS_IPV6_DROP_MEMBERSHIP
:
2840 case WS_IPV6_MULTICAST_IF
:
2841 case WS_IPV6_MULTICAST_HOPS
:
2842 case WS_IPV6_MULTICAST_LOOP
:
2843 case WS_IPV6_UNICAST_HOPS
:
2844 case WS_IPV6_V6ONLY
:
2845 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2846 return SOCKET_ERROR
;
2847 convert_sockopt(&level
, &optname
);
2848 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2850 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2853 release_sock_fd( s
, fd
);
2855 case WS_IPV6_DONTFRAG
:
2856 FIXME("WS_IPV6_DONTFRAG is always false!\n");
2857 *(BOOL
*)optval
= FALSE
;
2860 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
2861 return SOCKET_ERROR
;
2864 WARN("Unknown level: 0x%08x\n", level
);
2865 SetLastError(WSAEINVAL
);
2866 return SOCKET_ERROR
;
2867 } /* end switch(level) */
2870 /***********************************************************************
2873 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
2875 return htonl(hostlong
);
2879 /***********************************************************************
2882 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
2884 return htons(hostshort
);
2887 /***********************************************************************
2888 * WSAHtonl (WS2_32.46)
2889 * From MSDN description of error codes, this function should also
2890 * check if WinSock has been initialized and the socket is a valid
2891 * socket. But why? This function only translates a host byte order
2892 * u_long into a network byte order u_long...
2894 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
2898 *lpnetlong
= htonl(hostlong
);
2901 WSASetLastError(WSAEFAULT
);
2902 return SOCKET_ERROR
;
2905 /***********************************************************************
2906 * WSAHtons (WS2_32.47)
2907 * From MSDN description of error codes, this function should also
2908 * check if WinSock has been initialized and the socket is a valid
2909 * socket. But why? This function only translates a host byte order
2910 * u_short into a network byte order u_short...
2912 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
2917 *lpnetshort
= htons(hostshort
);
2920 WSASetLastError(WSAEFAULT
);
2921 return SOCKET_ERROR
;
2925 /***********************************************************************
2926 * inet_addr (WS2_32.11)
2928 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
2930 if (!cp
) return INADDR_NONE
;
2931 return inet_addr(cp
);
2935 /***********************************************************************
2938 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
2940 return ntohl(netlong
);
2944 /***********************************************************************
2947 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
2949 return ntohs(netshort
);
2953 /***********************************************************************
2954 * inet_ntoa (WS2_32.12)
2956 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
2958 /* use "buffer for dummies" here because some applications have a
2959 * propensity to decode addresses in ws_hostent structure without
2960 * saving them first...
2962 static char dbuffer
[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
2964 char* s
= inet_ntoa(*((struct in_addr
*)&in
));
2970 SetLastError(wsaErrno());
2974 static const char *debugstr_wsaioctl(DWORD ioctl
)
2976 const char *buf_type
, *family
;
2978 switch(ioctl
& 0x18000000)
2983 case WS_IOC_PROTOCOL
:
2984 family
= "IOC_PROTOCOL";
2987 family
= "IOC_VENDOR";
2989 default: /* WS_IOC_UNIX */
2991 BYTE size
= (ioctl
>> 16) & WS_IOCPARM_MASK
;
2992 char x
= (ioctl
& 0xff00) >> 8;
2993 BYTE y
= ioctl
& 0xff;
2996 switch (ioctl
& (WS_IOC_VOID
|WS_IOC_INOUT
))
3000 sprintf(args
, "%d, %d", x
, y
);
3004 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3008 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3012 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
3015 return wine_dbg_sprintf("%s(%s)", buf_type
, args
);
3019 /* We are different from WS_IOC_UNIX. */
3020 switch (ioctl
& (WS_IOC_VOID
|WS_IOC_INOUT
))
3023 buf_type
= "_WSAIO";
3026 buf_type
= "_WSAIORW";
3029 buf_type
= "_WSAIOW";
3032 buf_type
= "_WSAIOR";
3039 return wine_dbg_sprintf("%s(%s, %d)", buf_type
, family
,
3040 (USHORT
)(ioctl
& 0xffff));
3043 /**********************************************************************
3044 * WSAIoctl (WS2_32.50)
3047 INT WINAPI
WSAIoctl(SOCKET s
,
3048 DWORD dwIoControlCode
,
3051 LPVOID lpbOutBuffer
,
3053 LPDWORD lpcbBytesReturned
,
3054 LPWSAOVERLAPPED lpOverlapped
,
3055 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3057 TRACE("%ld, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
3058 s
, dwIoControlCode
, lpvInBuffer
, cbInBuffer
, lpbOutBuffer
,
3059 cbOutBuffer
, lpcbBytesReturned
, lpOverlapped
, lpCompletionRoutine
);
3061 switch( dwIoControlCode
)
3064 if (cbInBuffer
!= sizeof(WS_u_long
)) {
3065 WSASetLastError(WSAEFAULT
);
3066 return SOCKET_ERROR
;
3068 return WS_ioctlsocket( s
, WS_FIONBIO
, lpvInBuffer
);
3071 if (cbOutBuffer
!= sizeof(WS_u_long
)) {
3072 WSASetLastError(WSAEFAULT
);
3073 return SOCKET_ERROR
;
3075 return WS_ioctlsocket( s
, WS_FIONREAD
, lpbOutBuffer
);
3077 case WS_SIO_GET_INTERFACE_LIST
:
3079 INTERFACE_INFO
* intArray
= (INTERFACE_INFO
*)lpbOutBuffer
;
3080 DWORD size
, numInt
, apiReturn
;
3083 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
3087 WSASetLastError(WSAEFAULT
);
3088 return SOCKET_ERROR
;
3090 if (!lpcbBytesReturned
)
3092 WSASetLastError(WSAEFAULT
);
3093 return SOCKET_ERROR
;
3096 fd
= get_sock_fd( s
, 0, NULL
);
3097 if (fd
== -1) return SOCKET_ERROR
;
3099 apiReturn
= GetAdaptersInfo(NULL
, &size
);
3100 if (apiReturn
== ERROR_NO_DATA
)
3104 else if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
3106 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
3110 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
3112 PIP_ADAPTER_INFO ptr
;
3114 if (size
*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > cbOutBuffer
)
3116 WARN("Buffer too small = %u, cbOutBuffer = %u\n", size
, cbOutBuffer
);
3117 HeapFree(GetProcessHeap(),0,table
);
3118 release_sock_fd( s
, fd
);
3119 WSASetLastError(WSAEFAULT
);
3120 return SOCKET_ERROR
;
3122 for (ptr
= table
, numInt
= 0; ptr
;
3123 ptr
= ptr
->Next
, intArray
++, numInt
++)
3125 unsigned int addr
, mask
, bcast
;
3126 struct ifreq ifInfo
;
3128 /* Socket Status Flags */
3129 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
3130 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
3132 ERR("Error obtaining status flags for socket!\n");
3133 HeapFree(GetProcessHeap(),0,table
);
3134 release_sock_fd( s
, fd
);
3135 WSASetLastError(WSAEINVAL
);
3136 return SOCKET_ERROR
;
3140 /* set flags; the values of IFF_* are not the same
3141 under Linux and Windows, therefore must generate
3143 intArray
->iiFlags
= 0;
3144 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
3145 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
3146 #ifdef IFF_POINTOPOINT
3147 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
3148 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
3150 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
3151 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
3152 if (ifInfo
.ifr_flags
& IFF_UP
)
3153 intArray
->iiFlags
|= WS_IFF_UP
;
3154 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
3155 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
3158 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
3159 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
3160 bcast
= addr
| ~mask
;
3161 intArray
->iiAddress
.AddressIn
.sin_family
= AF_INET
;
3162 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
3163 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
=
3165 intArray
->iiNetmask
.AddressIn
.sin_family
= AF_INET
;
3166 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
3167 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
=
3169 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
=
3171 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
3172 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.
3178 ERR("Unable to get interface table!\n");
3179 release_sock_fd( s
, fd
);
3180 HeapFree(GetProcessHeap(),0,table
);
3181 WSASetLastError(WSAEINVAL
);
3182 return SOCKET_ERROR
;
3184 HeapFree(GetProcessHeap(),0,table
);
3188 release_sock_fd( s
, fd
);
3189 WSASetLastError(WSAEINVAL
);
3190 return SOCKET_ERROR
;
3195 ERR("Unable to get interface table!\n");
3196 release_sock_fd( s
, fd
);
3197 WSASetLastError(WSAEINVAL
);
3198 return SOCKET_ERROR
;
3200 /* Calculate the size of the array being returned */
3201 *lpcbBytesReturned
= sizeof(INTERFACE_INFO
) * numInt
;
3202 release_sock_fd( s
, fd
);
3206 case WS_SIO_ADDRESS_LIST_CHANGE
:
3207 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
3208 /* FIXME: error and return code depend on whether socket was created
3209 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
3212 case WS_SIO_ADDRESS_LIST_QUERY
:
3216 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
3218 if (!lpcbBytesReturned
)
3220 WSASetLastError(WSAEFAULT
);
3221 return SOCKET_ERROR
;
3224 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
3226 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
3229 if (!table
|| GetAdaptersInfo(table
, &size
))
3231 HeapFree(GetProcessHeap(), 0, table
);
3232 WSASetLastError(WSAEINVAL
);
3233 return SOCKET_ERROR
;
3236 for (p
= table
, num
= 0; p
; p
= p
->Next
)
3237 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
3239 need
= sizeof(SOCKET_ADDRESS_LIST
) + sizeof(SOCKET_ADDRESS
) * (num
- 1);
3240 need
+= sizeof(SOCKADDR
) * num
;
3241 *lpcbBytesReturned
= need
;
3243 if (need
> cbOutBuffer
)
3245 HeapFree(GetProcessHeap(), 0, table
);
3246 WSASetLastError(WSAEFAULT
);
3247 return SOCKET_ERROR
;
3254 SOCKET_ADDRESS_LIST
*sa_list
= (SOCKET_ADDRESS_LIST
*)lpbOutBuffer
;
3255 SOCKADDR_IN
*sockaddr
;
3257 sa
= sa_list
->Address
;
3258 sockaddr
= (SOCKADDR_IN
*)((char *)sa
+ num
* sizeof(SOCKET_ADDRESS
));
3259 sa_list
->iAddressCount
= num
;
3261 for (p
= table
, i
= 0; p
; p
= p
->Next
)
3263 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
3265 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
3266 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
3268 sockaddr
[i
].sin_family
= AF_INET
;
3269 sockaddr
[i
].sin_port
= 0;
3270 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
3275 HeapFree(GetProcessHeap(), 0, table
);
3280 WARN("unable to get IP address list\n");
3281 WSASetLastError(WSAEINVAL
);
3282 return SOCKET_ERROR
;
3286 FIXME("SIO_FLUSH: stub.\n");
3289 case WS_SIO_GET_EXTENSION_FUNCTION_POINTER
:
3291 static const GUID connectex_guid
= WSAID_CONNECTEX
;
3292 static const GUID disconnectex_guid
= WSAID_DISCONNECTEX
;
3293 static const GUID acceptex_guid
= WSAID_ACCEPTEX
;
3294 static const GUID getaccepexsockaddrs_guid
= WSAID_GETACCEPTEXSOCKADDRS
;
3295 static const GUID transmitfile_guid
= WSAID_TRANSMITFILE
;
3296 static const GUID transmitpackets_guid
= WSAID_TRANSMITPACKETS
;
3297 static const GUID wsarecvmsg_guid
= WSAID_WSARECVMSG
;
3298 static const GUID wsasendmsg_guid
= WSAID_WSASENDMSG
;
3300 if ( IsEqualGUID(&connectex_guid
, lpvInBuffer
) )
3302 *(LPFN_CONNECTEX
*)lpbOutBuffer
= WS2_ConnectEx
;
3305 else if ( IsEqualGUID(&disconnectex_guid
, lpvInBuffer
) )
3307 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented DisconnectEx\n");
3309 else if ( IsEqualGUID(&acceptex_guid
, lpvInBuffer
) )
3311 *(LPFN_ACCEPTEX
*)lpbOutBuffer
= WS2_AcceptEx
;
3314 else if ( IsEqualGUID(&getaccepexsockaddrs_guid
, lpvInBuffer
) )
3316 *(LPFN_GETACCEPTEXSOCKADDRS
*)lpbOutBuffer
= WS2_GetAcceptExSockaddrs
;
3319 else if ( IsEqualGUID(&transmitfile_guid
, lpvInBuffer
) )
3321 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitFile\n");
3323 else if ( IsEqualGUID(&transmitpackets_guid
, lpvInBuffer
) )
3325 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitPackets\n");
3327 else if ( IsEqualGUID(&wsarecvmsg_guid
, lpvInBuffer
) )
3329 *(LPFN_WSARECVMSG
*)lpbOutBuffer
= WS2_WSARecvMsg
;
3332 else if ( IsEqualGUID(&wsasendmsg_guid
, lpvInBuffer
) )
3334 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented WSASendMsg\n");
3337 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER %s: stub\n", debugstr_guid(lpvInBuffer
));
3339 WSASetLastError(WSAEOPNOTSUPP
);
3340 return SOCKET_ERROR
;
3343 case WS_SIO_KEEPALIVE_VALS
:
3346 struct tcp_keepalive
*k
= lpvInBuffer
;
3347 int keepalive
= k
->onoff
? 1 : 0;
3348 int keepidle
= k
->keepalivetime
/ 1000;
3349 int keepintvl
= k
->keepaliveinterval
/ 1000;
3353 WSASetLastError(WSAEINVAL
);
3354 return SOCKET_ERROR
;
3357 TRACE("onoff: %d, keepalivetime: %d, keepaliveinterval: %d\n", keepalive
, keepidle
, keepintvl
);
3359 fd
= get_sock_fd(s
, 0, NULL
);
3360 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&keepalive
, sizeof(int)) == -1)
3362 release_sock_fd(s
, fd
);
3363 WSASetLastError(WSAEINVAL
);
3364 return SOCKET_ERROR
;
3366 #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL)
3367 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, (void *)&keepidle
, sizeof(int)) == -1)
3369 release_sock_fd(s
, fd
);
3370 WSASetLastError(WSAEINVAL
);
3371 return SOCKET_ERROR
;
3373 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, (void *)&keepintvl
, sizeof(int)) == -1)
3375 release_sock_fd(s
, fd
);
3376 WSASetLastError(WSAEINVAL
);
3377 return SOCKET_ERROR
;
3380 FIXME("ignoring keepalive interval and timeout\n");
3383 release_sock_fd(s
, fd
);
3386 case WS_SIO_ROUTING_INTERFACE_QUERY
:
3388 struct WS_sockaddr
*daddr
= (struct WS_sockaddr
*)lpvInBuffer
;
3389 struct WS_sockaddr_in
*daddr_in
= (struct WS_sockaddr_in
*)daddr
;
3390 struct WS_sockaddr_in
*saddr_in
= (struct WS_sockaddr_in
*)lpbOutBuffer
;
3391 MIB_IPFORWARDROW row
;
3392 PMIB_IPADDRTABLE ipAddrTable
= NULL
;
3393 DWORD size
, i
, found_index
;
3395 TRACE("-> WS_SIO_ROUTING_INTERFACE_QUERY request\n");
3399 WSASetLastError(WSAEFAULT
);
3400 return SOCKET_ERROR
;
3402 if (cbInBuffer
< sizeof(struct WS_sockaddr
))
3404 WSASetLastError(WSAEFAULT
);
3405 return SOCKET_ERROR
;
3409 WSASetLastError(WSAEFAULT
);
3410 return SOCKET_ERROR
;
3412 if (!lpcbBytesReturned
)
3414 WSASetLastError(WSAEFAULT
);
3415 return SOCKET_ERROR
;
3417 if (daddr
->sa_family
!= AF_INET
)
3419 FIXME("unsupported address family %d\n", daddr
->sa_family
);
3420 WSASetLastError(WSAEAFNOSUPPORT
);
3421 return SOCKET_ERROR
;
3423 if (cbOutBuffer
< sizeof(struct WS_sockaddr_in
))
3425 WSASetLastError(WSAEFAULT
);
3426 return SOCKET_ERROR
;
3428 if (GetBestRoute(daddr_in
->sin_addr
.S_un
.S_addr
, 0, &row
) != NOERROR
)
3430 WSASetLastError(WSAEFAULT
);
3431 return SOCKET_ERROR
;
3433 if (GetIpAddrTable(NULL
, &size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
3435 WSASetLastError(WSAEFAULT
);
3436 return SOCKET_ERROR
;
3438 ipAddrTable
= HeapAlloc(GetProcessHeap(), 0, size
);
3439 if (GetIpAddrTable(ipAddrTable
, &size
, FALSE
))
3441 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3442 WSASetLastError(WSAEFAULT
);
3443 return SOCKET_ERROR
;
3445 for (i
= 0, found_index
= ipAddrTable
->dwNumEntries
;
3446 i
< ipAddrTable
->dwNumEntries
; i
++)
3448 if (ipAddrTable
->table
[i
].dwIndex
== row
.dwForwardIfIndex
)
3451 if (found_index
== ipAddrTable
->dwNumEntries
)
3453 ERR("no matching IP address for interface %d\n",
3454 row
.dwForwardIfIndex
);
3455 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3456 WSASetLastError(WSAEFAULT
);
3457 return SOCKET_ERROR
;
3459 saddr_in
->sin_family
= AF_INET
;
3460 saddr_in
->sin_addr
.S_un
.S_addr
= ipAddrTable
->table
[found_index
].dwAddr
;
3461 saddr_in
->sin_port
= 0;
3462 *lpcbBytesReturned
= sizeof(struct WS_sockaddr_in
);
3463 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3466 case WS_SIO_UDP_CONNRESET
:
3467 FIXME("WS_SIO_UDP_CONNRESET stub\n");
3470 FIXME("unsupported WS_IOCTL cmd (%s)\n",
3471 debugstr_wsaioctl(dwIoControlCode
));
3472 WSASetLastError(WSAEOPNOTSUPP
);
3473 return SOCKET_ERROR
;
3480 /***********************************************************************
3481 * ioctlsocket (WS2_32.10)
3483 int WINAPI
WS_ioctlsocket(SOCKET s
, LONG cmd
, WS_u_long
*argp
)
3488 TRACE("socket %04lx, cmd %08x, ptr %p\n", s
, cmd
, argp
);
3489 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
3490 if(IS_INTRESOURCE(argp
))
3492 SetLastError(WSAEFAULT
);
3493 return SOCKET_ERROR
;
3503 if( _get_sock_mask(s
) )
3505 /* AsyncSelect()'ed sockets are always nonblocking */
3506 if (*argp
) return 0;
3507 SetLastError(WSAEINVAL
);
3508 return SOCKET_ERROR
;
3511 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
3513 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
3521 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
3522 SetLastError(WSAEINVAL
);
3523 return SOCKET_ERROR
;
3525 case SIOCGIFBRDADDR
:
3526 case SIOCGIFNETMASK
:
3528 /* These don't need any special handling. They are used by
3529 WsControl, and are here to suppress an unnecessary warning. */
3533 /* Netscape tries hard to use bogus ioctl 0x667e */
3534 /* FIXME: 0x667e above is ('f' << 8) | 126, and is a low word of
3535 * FIONBIO (_IOW('f', 126, u_long)), how that should be handled?
3537 WARN("\tunknown WS_IOCTL cmd (%08x)\n", cmd
);
3541 fd
= get_sock_fd( s
, 0, NULL
);
3544 if( ioctl(fd
, newcmd
, (char*)argp
) == 0 )
3546 release_sock_fd( s
, fd
);
3549 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3550 release_sock_fd( s
, fd
);
3552 return SOCKET_ERROR
;
3555 /***********************************************************************
3556 * listen (WS2_32.13)
3558 int WINAPI
WS_listen(SOCKET s
, int backlog
)
3560 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
3562 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
3565 if (listen(fd
, backlog
) == 0)
3567 release_sock_fd( s
, fd
);
3568 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
3570 FD_CONNECT
|FD_WINE_CONNECTED
);
3573 SetLastError(wsaErrno());
3574 release_sock_fd( s
, fd
);
3576 return SOCKET_ERROR
;
3579 /***********************************************************************
3582 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
3584 DWORD n
, dwFlags
= flags
;
3590 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
3591 return SOCKET_ERROR
;
3596 /***********************************************************************
3597 * recvfrom (WS2_32.17)
3599 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
3600 struct WS_sockaddr
*from
, int *fromlen
)
3602 DWORD n
, dwFlags
= flags
;
3608 if ( WS2_recv_base(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
3609 return SOCKET_ERROR
;
3614 /* allocate a poll array for the corresponding fd sets */
3615 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
3616 const WS_fd_set
*exceptfds
, int *count_ptr
)
3618 unsigned int i
, j
= 0, count
= 0;
3621 if (readfds
) count
+= readfds
->fd_count
;
3622 if (writefds
) count
+= writefds
->fd_count
;
3623 if (exceptfds
) count
+= exceptfds
->fd_count
;
3627 SetLastError(WSAEINVAL
);
3630 if (!(fds
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(fds
[0]))))
3632 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
3636 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
3638 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
3639 if (fds
[j
].fd
== -1) goto failed
;
3640 fds
[j
].events
= POLLIN
;
3644 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
3646 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
3647 if (fds
[j
].fd
== -1) goto failed
;
3648 fds
[j
].events
= POLLOUT
;
3652 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3654 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
3655 if (fds
[j
].fd
== -1) goto failed
;
3656 fds
[j
].events
= POLLHUP
;
3665 for (i
= 0; i
< readfds
->fd_count
&& j
< count
; i
++, j
++)
3666 release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
3668 for (i
= 0; i
< writefds
->fd_count
&& j
< count
; i
++, j
++)
3669 release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
3671 for (i
= 0; i
< exceptfds
->fd_count
&& j
< count
; i
++, j
++)
3672 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
3673 HeapFree( GetProcessHeap(), 0, fds
);
3677 /* release the file descriptor obtained in fd_sets_to_poll */
3678 /* must be called with the original fd_set arrays, before calling get_poll_results */
3679 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
3680 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
3682 unsigned int i
, j
= 0;
3686 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
3687 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
3691 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
3692 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
3696 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3697 if (fds
[j
].fd
!= -1)
3699 /* make sure we have a real error before releasing the fd */
3700 if (!sock_error_p( fds
[j
].fd
)) fds
[j
].revents
= 0;
3701 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
3706 /* map the poll results back into the Windows fd sets */
3707 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
3708 const struct pollfd
*fds
)
3710 unsigned int i
, j
= 0, k
, total
= 0;
3714 for (i
= k
= 0; i
< readfds
->fd_count
; i
++, j
++)
3715 if (fds
[j
].revents
) readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
3716 readfds
->fd_count
= k
;
3721 for (i
= k
= 0; i
< writefds
->fd_count
; i
++, j
++)
3722 if (fds
[j
].revents
) writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
3723 writefds
->fd_count
= k
;
3728 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3729 if (fds
[j
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
3730 exceptfds
->fd_count
= k
;
3737 /***********************************************************************
3738 * select (WS2_32.18)
3740 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
3741 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
3742 const struct WS_timeval
* ws_timeout
)
3744 struct pollfd
*pollfds
;
3745 struct timeval tv1
, tv2
;
3747 int count
, ret
, timeout
= -1;
3749 TRACE("read %p, write %p, excp %p timeout %p\n",
3750 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
3752 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)))
3753 return SOCKET_ERROR
;
3757 torig
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
3759 gettimeofday( &tv1
, 0 );
3762 while ((ret
= poll( pollfds
, count
, timeout
)) < 0)
3766 if (!ws_timeout
) continue;
3767 gettimeofday( &tv2
, 0 );
3769 tv2
.tv_sec
-= tv1
.tv_sec
;
3770 tv2
.tv_usec
-= tv1
.tv_usec
;
3771 if (tv2
.tv_usec
< 0)
3773 tv2
.tv_usec
+= 1000000;
3777 timeout
= torig
- (tv2
.tv_sec
* 1000) - (tv2
.tv_usec
+ 999) / 1000;
3778 if (timeout
<= 0) break;
3781 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
3783 if (ret
== -1) SetLastError(wsaErrno());
3784 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
3785 HeapFree( GetProcessHeap(), 0, pollfds
);
3789 /* helper to send completion messages for client-only i/o operation case */
3790 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
,
3793 SERVER_START_REQ( add_fd_completion
)
3795 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(sock
) );
3796 req
->cvalue
= CompletionValue
;
3797 req
->status
= CompletionStatus
;
3798 req
->information
= Information
;
3799 wine_server_call( req
);
3805 /***********************************************************************
3808 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
3814 wsabuf
.buf
= (char*) buf
;
3816 if ( WS2_sendto( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
3817 return SOCKET_ERROR
;
3822 /***********************************************************************
3823 * WSASend (WS2_32.72)
3825 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
3826 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
3827 LPWSAOVERLAPPED lpOverlapped
,
3828 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3830 return WS2_sendto( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
3831 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
3834 /***********************************************************************
3835 * WSASendDisconnect (WS2_32.73)
3837 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
3839 return WS_shutdown( s
, SD_SEND
);
3843 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
3844 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
3845 const struct WS_sockaddr
*to
, int tolen
,
3846 LPWSAOVERLAPPED lpOverlapped
,
3847 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3849 unsigned int i
, options
;
3851 struct ws2_async
*wsa
;
3852 int totalLength
= 0;
3853 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
3855 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
3856 s
, lpBuffers
, dwBufferCount
, dwFlags
,
3857 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
3859 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
3860 TRACE( "fd=%d, options=%x\n", fd
, options
);
3862 if ( fd
== -1 ) return SOCKET_ERROR
;
3864 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
3870 wsa
->hSocket
= SOCKET2HANDLE(s
);
3871 wsa
->addr
= (struct WS_sockaddr
*)to
;
3872 wsa
->addrlen
.val
= tolen
;
3873 wsa
->flags
= dwFlags
;
3874 wsa
->lpFlags
= &wsa
->flags
;
3875 wsa
->control
= NULL
;
3876 wsa
->n_iovecs
= dwBufferCount
;
3877 wsa
->first_iovec
= 0;
3878 for ( i
= 0; i
< dwBufferCount
; i
++ )
3880 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
3881 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
3882 totalLength
+= lpBuffers
[i
].len
;
3885 if (!lpNumberOfBytesSent
)
3893 n
= WS2_send( fd
, wsa
);
3894 if (n
!= -1 || errno
!= EINTR
) break;
3896 if (n
== -1 && errno
!= EAGAIN
)
3898 int loc_errno
= errno
;
3900 if (cvalue
) WS_AddCompletion( s
, cvalue
, sock_get_ntstatus(loc_errno
), 0 );
3904 if ((lpOverlapped
|| lpCompletionRoutine
) &&
3905 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
3907 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
3909 wsa
->user_overlapped
= lpOverlapped
;
3910 wsa
->completion_func
= lpCompletionRoutine
;
3911 release_sock_fd( s
, fd
);
3913 if (n
== -1 || n
< totalLength
)
3915 iosb
->u
.Status
= STATUS_PENDING
;
3916 iosb
->Information
= n
== -1 ? 0 : n
;
3918 SERVER_START_REQ( register_async
)
3920 req
->type
= ASYNC_TYPE_WRITE
;
3921 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
3922 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
3923 req
->async
.iosb
= wine_server_client_ptr( iosb
);
3924 req
->async
.arg
= wine_server_client_ptr( wsa
);
3925 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
3926 req
->async
.cvalue
= cvalue
;
3927 err
= wine_server_call( req
);
3931 /* Enable the event only after starting the async. The server will deliver it as soon as
3932 the async is done. */
3933 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
3935 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
3936 WSASetLastError( NtStatusToWSAError( err
));
3937 return SOCKET_ERROR
;
3940 iosb
->u
.Status
= STATUS_SUCCESS
;
3941 iosb
->Information
= n
;
3942 *lpNumberOfBytesSent
= n
;
3943 if (!wsa
->completion_func
)
3945 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
3946 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
3947 HeapFree( GetProcessHeap(), 0, wsa
);
3949 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
3950 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
3955 if ( _is_blocking(s
) )
3957 /* On a blocking non-overlapped stream socket,
3958 * sending blocks until the entire buffer is sent. */
3959 DWORD timeout_start
= GetTickCount();
3961 *lpNumberOfBytesSent
= n
== -1 ? 0 : n
;
3963 while (wsa
->first_iovec
< wsa
->n_iovecs
)
3966 int timeout
= GET_SNDTIMEO(fd
);
3970 timeout
-= GetTickCount() - timeout_start
;
3971 if (timeout
< 0) timeout
= 0;
3975 pfd
.events
= POLLOUT
;
3977 if (!timeout
|| !poll( &pfd
, 1, timeout
))
3980 goto error
; /* msdn says a timeout in send is fatal */
3983 n
= WS2_send( fd
, wsa
);
3984 if (n
== -1 && errno
!= EAGAIN
&& errno
!= EINTR
)
3991 *lpNumberOfBytesSent
+= n
;
3994 else /* non-blocking */
3996 if (n
< totalLength
)
3997 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
4000 err
= WSAEWOULDBLOCK
;
4003 *lpNumberOfBytesSent
= n
;
4006 TRACE(" -> %i bytes\n", *lpNumberOfBytesSent
);
4008 HeapFree( GetProcessHeap(), 0, wsa
);
4009 release_sock_fd( s
, fd
);
4014 HeapFree( GetProcessHeap(), 0, wsa
);
4015 release_sock_fd( s
, fd
);
4016 WARN(" -> ERROR %d\n", err
);
4017 WSASetLastError(err
);
4018 return SOCKET_ERROR
;
4021 /***********************************************************************
4022 * WSASendTo (WS2_32.74)
4024 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4025 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
4026 const struct WS_sockaddr
*to
, int tolen
,
4027 LPWSAOVERLAPPED lpOverlapped
,
4028 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4030 return WS2_sendto( s
, lpBuffers
, dwBufferCount
,
4031 lpNumberOfBytesSent
, dwFlags
,
4033 lpOverlapped
, lpCompletionRoutine
);
4036 /***********************************************************************
4037 * sendto (WS2_32.20)
4039 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
4040 const struct WS_sockaddr
*to
, int tolen
)
4046 wsabuf
.buf
= (char*) buf
;
4048 if ( WS2_sendto(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
4049 return SOCKET_ERROR
;
4054 /***********************************************************************
4055 * setsockopt (WS2_32.21)
4057 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
4058 const char *optval
, int optlen
)
4062 struct linger linger
;
4063 struct timeval tval
;
4065 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
4066 s
, level
, optname
, optval
, optlen
);
4068 /* some broken apps pass the value directly instead of a pointer to it */
4069 if(optlen
&& IS_INTRESOURCE(optval
))
4071 SetLastError(WSAEFAULT
);
4072 return SOCKET_ERROR
;
4080 /* Some options need some conversion before they can be sent to
4081 * setsockopt. The conversions are done here, then they will fall though
4082 * to the general case. Special options that are not passed to
4083 * setsockopt follow below that.*/
4085 case WS_SO_DONTLINGER
:
4086 linger
.l_onoff
= *((const int*)optval
) ? 0: 1;
4087 linger
.l_linger
= 0;
4089 optname
= SO_LINGER
;
4090 optval
= (char*)&linger
;
4091 optlen
= sizeof(struct linger
);
4095 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
4096 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
4097 /* FIXME: what is documented behavior if SO_LINGER optval
4100 optname
= SO_LINGER
;
4101 optval
= (char*)&linger
;
4102 optlen
= sizeof(struct linger
);
4106 if (*(const int*)optval
< 2048)
4108 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
4113 /* The options listed here don't need any special handling. Thanks to
4114 * the conversion happening above, options from there will fall through
4116 case WS_SO_ACCEPTCONN
:
4117 case WS_SO_BROADCAST
:
4119 case WS_SO_KEEPALIVE
:
4120 case WS_SO_OOBINLINE
:
4121 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
4122 * however, using it the BSD way fixes bug 8513 and seems to be what
4123 * most programmers assume, anyway */
4124 case WS_SO_REUSEADDR
:
4127 convert_sockopt(&level
, &optname
);
4130 /* SO_DEBUG is a privileged operation, ignore it. */
4132 TRACE("Ignoring SO_DEBUG\n");
4135 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
4136 * socket. According to MSDN, this option is silently ignored.*/
4137 case WS_SO_DONTROUTE
:
4138 TRACE("Ignoring SO_DONTROUTE\n");
4141 /* Stops two sockets from being bound to the same port. Always happens
4142 * on unix systems, so just drop it. */
4143 case WS_SO_EXCLUSIVEADDRUSE
:
4144 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
4147 /* After a ConnectEx call succeeds, the socket can't be used with half of the
4148 * normal winsock functions on windows. We don't have that problem. */
4149 case WS_SO_UPDATE_CONNECT_CONTEXT
:
4150 TRACE("Ignoring SO_UPDATE_CONNECT_CONTEXT, since our sockets are normal\n");
4153 /* After a AcceptEx call succeeds, the socket can't be used with half of the
4154 * normal winsock functions on windows. We don't have that problem. */
4155 case WS_SO_UPDATE_ACCEPT_CONTEXT
:
4156 TRACE("Ignoring SO_UPDATE_ACCEPT_CONTEXT, since our sockets are normal\n");
4159 /* SO_OPENTYPE does not require a valid socket handle. */
4160 case WS_SO_OPENTYPE
:
4161 if (!optlen
|| optlen
< sizeof(int) || !optval
)
4163 SetLastError(WSAEFAULT
);
4164 return SOCKET_ERROR
;
4166 get_per_thread_data()->opentype
= *(const int *)optval
;
4167 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((const int*)optval
) );
4171 case WS_SO_RCVTIMEO
:
4174 case WS_SO_SNDTIMEO
:
4176 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
4177 if (optval
&& optlen
== sizeof(UINT32
)) {
4178 /* WinSock passes milliseconds instead of struct timeval */
4179 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
4180 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
4181 /* min of 500 milliseconds */
4182 if (tval
.tv_sec
== 0 && tval
.tv_usec
&& tval
.tv_usec
< 500000)
4183 tval
.tv_usec
= 500000;
4184 optlen
= sizeof(struct timeval
);
4185 optval
= (char*)&tval
;
4186 } else if (optlen
== sizeof(struct timeval
)) {
4187 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
4189 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
4192 convert_sockopt(&level
, &optname
);
4197 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
4198 SetLastError(WSAENOPROTOOPT
);
4199 return SOCKET_ERROR
;
4201 break; /* case WS_SOL_SOCKET */
4208 fd
= get_sock_fd( s
, 0, NULL
);
4209 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval
, fd
);
4211 /* We try to set the ipx type on ipx socket level. */
4213 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, optlen
) == -1)
4215 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
4216 release_sock_fd( s
, fd
);
4217 return SOCKET_ERROR
;
4222 /* Should we retrieve val using a getsockopt call and then
4223 * set the modified one? */
4224 val
.ipx_pt
= *optval
;
4225 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
4228 release_sock_fd( s
, fd
);
4231 case IPX_FILTERPTYPE
:
4232 /* Sets the receive filter packet type, at the moment we don't support it */
4233 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
4234 /* Returning 0 is better for now than returning a SOCKET_ERROR */
4238 FIXME("opt_name:%x\n", optname
);
4239 return SOCKET_ERROR
;
4241 break; /* case NSPROTO_IPX */
4244 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
4245 case WS_IPPROTO_TCP
:
4248 case WS_TCP_NODELAY
:
4249 convert_sockopt(&level
, &optname
);
4252 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
4253 return SOCKET_ERROR
;
4260 case WS_IP_ADD_MEMBERSHIP
:
4261 case WS_IP_DROP_MEMBERSHIP
:
4265 case WS_IP_MULTICAST_IF
:
4266 case WS_IP_MULTICAST_LOOP
:
4267 case WS_IP_MULTICAST_TTL
:
4274 convert_sockopt(&level
, &optname
);
4276 case WS_IP_DONTFRAGMENT
:
4277 FIXME("IP_DONTFRAGMENT is silently ignored!\n");
4280 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
4281 return SOCKET_ERROR
;
4285 case WS_IPPROTO_IPV6
:
4288 #ifdef IPV6_ADD_MEMBERSHIP
4289 case WS_IPV6_ADD_MEMBERSHIP
:
4291 #ifdef IPV6_DROP_MEMBERSHIP
4292 case WS_IPV6_DROP_MEMBERSHIP
:
4294 case WS_IPV6_MULTICAST_IF
:
4295 case WS_IPV6_MULTICAST_HOPS
:
4296 case WS_IPV6_MULTICAST_LOOP
:
4297 case WS_IPV6_UNICAST_HOPS
:
4298 case WS_IPV6_V6ONLY
:
4299 convert_sockopt(&level
, &optname
);
4301 case WS_IPV6_DONTFRAG
:
4302 FIXME("IPV6_DONTFRAG is silently ignored!\n");
4305 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
4306 return SOCKET_ERROR
;
4311 WARN("Unknown level: 0x%08x\n", level
);
4312 SetLastError(WSAEINVAL
);
4313 return SOCKET_ERROR
;
4314 } /* end switch(level) */
4316 /* avoid endianness issues if argument is a 16-bit int */
4317 if (optval
&& optlen
< sizeof(int))
4319 woptval
= *((const INT16
*) optval
);
4320 optval
= (char*) &woptval
;
4323 fd
= get_sock_fd( s
, 0, NULL
);
4324 if (fd
== -1) return SOCKET_ERROR
;
4326 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
4328 release_sock_fd( s
, fd
);
4331 TRACE("Setting socket error, %d\n", wsaErrno());
4332 SetLastError(wsaErrno());
4333 release_sock_fd( s
, fd
);
4335 return SOCKET_ERROR
;
4338 /***********************************************************************
4339 * shutdown (WS2_32.22)
4341 int WINAPI
WS_shutdown(SOCKET s
, int how
)
4343 int fd
, err
= WSAENOTSOCK
;
4344 unsigned int options
, clear_flags
= 0;
4346 fd
= get_sock_fd( s
, 0, &options
);
4347 TRACE("socket %04lx, how %i %x\n", s
, how
, options
);
4350 return SOCKET_ERROR
;
4354 case 0: /* drop receives */
4355 clear_flags
|= FD_READ
;
4357 case 1: /* drop sends */
4358 clear_flags
|= FD_WRITE
;
4360 case 2: /* drop all */
4361 clear_flags
|= FD_READ
|FD_WRITE
;
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
++)
4494 DWORD ifindex
, ifmetric
, exists
= FALSE
;
4496 if (routes
->table
[i
].dwForwardType
!= MIB_IPROUTE_TYPE_DIRECT
)
4498 ifindex
= routes
->table
[i
].dwForwardIfIndex
;
4499 ifmetric
= routes
->table
[i
].dwForwardMetric1
;
4500 /* Only store the lowest valued metric for an interface */
4501 for (j
= 0; j
< numroutes
; j
++)
4503 if (route_addrs
[j
].interface
== ifindex
)
4505 if (route_addrs
[j
].metric
> ifmetric
)
4506 route_addrs
[j
].metric
= ifmetric
;
4512 route_addrs
= HeapReAlloc(GetProcessHeap(), 0, route_addrs
, (numroutes
+1)*sizeof(struct route
));
4513 if (route_addrs
== NULL
)
4514 goto cleanup
; /* Memory allocation error, fail gracefully */
4515 route_addrs
[numroutes
].interface
= ifindex
;
4516 route_addrs
[numroutes
].metric
= ifmetric
;
4517 /* If no IP is found in the next step (for whatever reason)
4518 * then fall back to the magic loopback address.
4520 memcpy(&(route_addrs
[numroutes
].addr
.s_addr
), magic_loopback_addr
, 4);
4524 goto cleanup
; /* No routes, fall back to the Magic IP */
4525 /* Find the IP address associated with each found interface */
4526 for (i
= 0; i
< numroutes
; i
++)
4528 for (k
= adapters
; k
!= NULL
; k
= k
->Next
)
4530 char *ip
= k
->IpAddressList
.IpAddress
.String
;
4532 if (route_addrs
[i
].interface
== k
->Index
)
4533 route_addrs
[i
].addr
.s_addr
= (in_addr_t
) inet_addr(ip
);
4536 /* Allocate a hostent and enough memory for all the IPs,
4537 * including the NULL at the end of the list.
4539 hostlist
= WS_create_he(hostname
, 1, numroutes
+1, TRUE
);
4540 if (hostlist
== NULL
)
4541 goto cleanup
; /* Failed to allocate a hostent for the list of IPs */
4542 hostlist
->h_addr_list
[numroutes
] = NULL
; /* NULL-terminate the address list */
4543 hostlist
->h_aliases
[0] = NULL
; /* NULL-terminate the alias list */
4544 hostlist
->h_addrtype
= AF_INET
;
4545 hostlist
->h_length
= sizeof(struct in_addr
); /* = 4 */
4546 /* Reorder the entries when placing them in the host list, Windows expects
4547 * the IP list in order from highest priority to lowest (the critical thing
4548 * is that most applications expect the first IP to be the default route).
4551 for (i
= 0; i
< numroutes
; i
++)
4553 struct in_addr addr
;
4554 int metric
= 0xFFFF;
4556 memcpy(&addr
, magic_loopback_addr
, 4);
4557 for (j
= 0; j
< numroutes
; j
++)
4559 int this_metric
= route_addrs
[j
].metric
;
4561 if (this_metric
> last_metric
&& this_metric
< metric
)
4563 addr
= route_addrs
[j
].addr
;
4564 metric
= this_metric
;
4567 last_metric
= metric
;
4568 (*(struct in_addr
*) hostlist
->h_addr_list
[i
]) = addr
;
4571 /* Cleanup all allocated memory except the address list,
4572 * the address list is used by the calling app.
4575 HeapFree(GetProcessHeap(), 0, route_addrs
);
4576 HeapFree(GetProcessHeap(), 0, adapters
);
4577 HeapFree(GetProcessHeap(), 0, routes
);
4581 /***********************************************************************
4582 * gethostbyname (WS2_32.52)
4584 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
4586 struct WS_hostent
*retval
= NULL
;
4587 struct hostent
* host
;
4588 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4591 struct hostent hostentry
;
4592 int locerr
= ENOBUFS
;
4595 if( gethostname( hostname
, 100) == -1) {
4596 SetLastError( WSAENOBUFS
); /* appropriate ? */
4599 if( !name
|| !name
[0]) {
4602 /* If the hostname of the local machine is requested then return the
4603 * complete list of local IP addresses */
4604 if(strcmp(name
, hostname
) == 0)
4605 retval
= WS_get_local_ips(hostname
);
4606 /* If any other hostname was requested (or the routing table lookup failed)
4607 * then return the IP found by the host OS */
4610 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4612 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
4614 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
4615 if( res
!= ERANGE
) break;
4617 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
4619 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
4621 EnterCriticalSection( &csWSgetXXXbyYYY
);
4622 host
= gethostbyname(name
);
4623 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
4625 if (host
) retval
= WS_dup_he(host
);
4626 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4627 HeapFree(GetProcessHeap(),0,extrabuf
);
4629 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4632 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
4633 strcmp(name
, "localhost") != 0)
4635 /* hostname != "localhost" but has loopback address. replace by our
4636 * special address.*/
4637 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
4639 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
4644 /***********************************************************************
4645 * getprotobyname (WS2_32.53)
4647 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
4649 struct WS_protoent
* retval
= NULL
;
4650 #ifdef HAVE_GETPROTOBYNAME
4651 struct protoent
* proto
;
4652 EnterCriticalSection( &csWSgetXXXbyYYY
);
4653 if( (proto
= getprotobyname(name
)) != NULL
)
4655 retval
= WS_dup_pe(proto
);
4658 MESSAGE("protocol %s not found; You might want to add "
4659 "this to /etc/protocols\n", debugstr_a(name
) );
4660 SetLastError(WSANO_DATA
);
4662 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4664 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
4669 /***********************************************************************
4670 * getprotobynumber (WS2_32.54)
4672 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
4674 struct WS_protoent
* retval
= NULL
;
4675 #ifdef HAVE_GETPROTOBYNUMBER
4676 struct protoent
* proto
;
4677 EnterCriticalSection( &csWSgetXXXbyYYY
);
4678 if( (proto
= getprotobynumber(number
)) != NULL
)
4680 retval
= WS_dup_pe(proto
);
4683 MESSAGE("protocol number %d not found; You might want to add "
4684 "this to /etc/protocols\n", number
);
4685 SetLastError(WSANO_DATA
);
4687 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4689 TRACE("%i ret %p\n", number
, retval
);
4694 /***********************************************************************
4695 * getservbyname (WS2_32.55)
4697 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
4699 struct WS_servent
* retval
= NULL
;
4700 struct servent
* serv
;
4702 char *proto_str
= NULL
;
4704 if (!(name_str
= strdup_lower(name
))) return NULL
;
4706 if (proto
&& *proto
)
4708 if (!(proto_str
= strdup_lower(proto
)))
4710 HeapFree( GetProcessHeap(), 0, name_str
);
4715 EnterCriticalSection( &csWSgetXXXbyYYY
);
4716 serv
= getservbyname(name_str
, proto_str
);
4719 retval
= WS_dup_se(serv
);
4721 else SetLastError(WSANO_DATA
);
4722 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4723 HeapFree( GetProcessHeap(), 0, proto_str
);
4724 HeapFree( GetProcessHeap(), 0, name_str
);
4725 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
4729 /***********************************************************************
4730 * freeaddrinfo (WS2_32.@)
4732 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
4735 struct WS_addrinfo
*next
;
4737 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
4738 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
4739 next
= res
->ai_next
;
4740 HeapFree(GetProcessHeap(),0,res
);
4745 /* helper functions for getaddrinfo()/getnameinfo() */
4746 static int convert_aiflag_w2u(int winflags
) {
4750 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
4751 if (ws_aiflag_map
[i
][0] & winflags
) {
4752 unixflags
|= ws_aiflag_map
[i
][1];
4753 winflags
&= ~ws_aiflag_map
[i
][0];
4756 FIXME("Unhandled windows AI_xxx flags %x\n", winflags
);
4760 static int convert_niflag_w2u(int winflags
) {
4764 for (i
=0;i
<sizeof(ws_niflag_map
)/sizeof(ws_niflag_map
[0]);i
++)
4765 if (ws_niflag_map
[i
][0] & winflags
) {
4766 unixflags
|= ws_niflag_map
[i
][1];
4767 winflags
&= ~ws_niflag_map
[i
][0];
4770 FIXME("Unhandled windows NI_xxx flags %x\n", winflags
);
4774 static int convert_aiflag_u2w(int unixflags
) {
4778 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
4779 if (ws_aiflag_map
[i
][1] & unixflags
) {
4780 winflags
|= ws_aiflag_map
[i
][0];
4781 unixflags
&= ~ws_aiflag_map
[i
][1];
4783 if (unixflags
) /* will warn usually */
4784 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags
);
4788 static int convert_eai_u2w(int unixret
) {
4791 for (i
=0;ws_eai_map
[i
][0];i
++)
4792 if (ws_eai_map
[i
][1] == unixret
)
4793 return ws_eai_map
[i
][0];
4797 /***********************************************************************
4798 * getaddrinfo (WS2_32.@)
4800 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
4802 #ifdef HAVE_GETADDRINFO
4803 struct addrinfo
*unixaires
= NULL
;
4805 struct addrinfo unixhints
, *punixhints
= NULL
;
4806 CHAR
*node
= NULL
, *serv
= NULL
;
4809 if (!(node
= strdup_lower(nodename
))) return WSA_NOT_ENOUGH_MEMORY
;
4812 if (!(serv
= strdup_lower(servname
))) {
4813 HeapFree(GetProcessHeap(), 0, node
);
4814 return WSA_NOT_ENOUGH_MEMORY
;
4819 punixhints
= &unixhints
;
4821 memset(&unixhints
, 0, sizeof(unixhints
));
4822 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
4823 if (hints
->ai_family
== 0) /* wildcard, specific to getaddrinfo() */
4824 punixhints
->ai_family
= 0;
4826 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
4827 if (hints
->ai_socktype
== 0) /* wildcard, specific to getaddrinfo() */
4828 punixhints
->ai_socktype
= 0;
4830 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
4831 if (hints
->ai_protocol
== 0) /* wildcard, specific to getaddrinfo() */
4832 punixhints
->ai_protocol
= 0;
4834 punixhints
->ai_protocol
= convert_proto_w2u(hints
->ai_protocol
);
4837 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
4838 result
= getaddrinfo(nodename
, servname
, punixhints
, &unixaires
);
4840 TRACE("%s, %s %p -> %p %d\n", debugstr_a(nodename
), debugstr_a(servname
), hints
, res
, result
);
4842 HeapFree(GetProcessHeap(), 0, node
);
4843 HeapFree(GetProcessHeap(), 0, serv
);
4846 struct addrinfo
*xuai
= unixaires
;
4847 struct WS_addrinfo
**xai
= res
;
4851 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
4857 *xai
= ai
;xai
= &ai
->ai_next
;
4858 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
4859 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
4860 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
4861 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
4862 if (xuai
->ai_canonname
) {
4863 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
4864 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
4865 if (!ai
->ai_canonname
)
4867 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
4869 len
= xuai
->ai_addrlen
;
4870 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
4873 ai
->ai_addrlen
= len
;
4875 int winlen
= ai
->ai_addrlen
;
4877 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
4878 ai
->ai_addrlen
= winlen
;
4882 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
4885 ai
->ai_addrlen
= len
;
4887 xuai
= xuai
->ai_next
;
4889 freeaddrinfo(unixaires
);
4891 result
= convert_eai_u2w(result
);
4897 if (*res
) WS_freeaddrinfo(*res
);
4898 if (unixaires
) freeaddrinfo(unixaires
);
4900 return WSA_NOT_ENOUGH_MEMORY
;
4902 FIXME("getaddrinfo() failed, not found during buildtime.\n");
4907 static struct WS_addrinfoW
*addrinfo_AtoW(const struct WS_addrinfo
*ai
)
4909 struct WS_addrinfoW
*ret
;
4911 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfoW
)))) return NULL
;
4912 ret
->ai_flags
= ai
->ai_flags
;
4913 ret
->ai_family
= ai
->ai_family
;
4914 ret
->ai_socktype
= ai
->ai_socktype
;
4915 ret
->ai_protocol
= ai
->ai_protocol
;
4916 ret
->ai_addrlen
= ai
->ai_addrlen
;
4917 ret
->ai_canonname
= NULL
;
4918 ret
->ai_addr
= NULL
;
4919 ret
->ai_next
= NULL
;
4920 if (ai
->ai_canonname
)
4922 int len
= MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0);
4923 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
4925 HeapFree(GetProcessHeap(), 0, ret
);
4928 MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
);
4932 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
4934 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
4935 HeapFree(GetProcessHeap(), 0, ret
);
4938 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
4943 static struct WS_addrinfoW
*addrinfo_list_AtoW(const struct WS_addrinfo
*info
)
4945 struct WS_addrinfoW
*ret
, *infoW
;
4947 if (!(ret
= infoW
= addrinfo_AtoW(info
))) return NULL
;
4948 while (info
->ai_next
)
4950 if (!(infoW
->ai_next
= addrinfo_AtoW(info
->ai_next
)))
4955 infoW
= infoW
->ai_next
;
4956 info
= info
->ai_next
;
4961 static struct WS_addrinfo
*addrinfo_WtoA(const struct WS_addrinfoW
*ai
)
4963 struct WS_addrinfo
*ret
;
4965 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfo
)))) return NULL
;
4966 ret
->ai_flags
= ai
->ai_flags
;
4967 ret
->ai_family
= ai
->ai_family
;
4968 ret
->ai_socktype
= ai
->ai_socktype
;
4969 ret
->ai_protocol
= ai
->ai_protocol
;
4970 ret
->ai_addrlen
= ai
->ai_addrlen
;
4971 ret
->ai_canonname
= NULL
;
4972 ret
->ai_addr
= NULL
;
4973 ret
->ai_next
= NULL
;
4974 if (ai
->ai_canonname
)
4976 int len
= WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0, NULL
, NULL
);
4977 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
4979 HeapFree(GetProcessHeap(), 0, ret
);
4982 WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
, NULL
, NULL
);
4986 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
4988 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
4989 HeapFree(GetProcessHeap(), 0, ret
);
4992 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
4997 /***********************************************************************
4998 * GetAddrInfoW (WS2_32.@)
5000 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
5003 char *nodenameA
, *servnameA
= NULL
;
5004 struct WS_addrinfo
*resA
, *hintsA
= NULL
;
5006 if (!nodename
) return WSAHOST_NOT_FOUND
;
5008 len
= WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, NULL
, 0, NULL
, NULL
);
5009 if (!(nodenameA
= HeapAlloc(GetProcessHeap(), 0, len
))) return EAI_MEMORY
;
5010 WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, nodenameA
, len
, NULL
, NULL
);
5014 len
= WideCharToMultiByte(CP_ACP
, 0, servname
, -1, NULL
, 0, NULL
, NULL
);
5015 if (!(servnameA
= HeapAlloc(GetProcessHeap(), 0, len
)))
5017 HeapFree(GetProcessHeap(), 0, nodenameA
);
5020 WideCharToMultiByte(CP_ACP
, 0, servname
, -1, servnameA
, len
, NULL
, NULL
);
5023 if (hints
) hintsA
= addrinfo_WtoA(hints
);
5024 ret
= WS_getaddrinfo(nodenameA
, servnameA
, hintsA
, &resA
);
5025 WS_freeaddrinfo(hintsA
);
5029 *res
= addrinfo_list_AtoW(resA
);
5030 WS_freeaddrinfo(resA
);
5033 HeapFree(GetProcessHeap(), 0, nodenameA
);
5034 HeapFree(GetProcessHeap(), 0, servnameA
);
5038 /***********************************************************************
5039 * FreeAddrInfoW (WS2_32.@)
5041 void WINAPI
FreeAddrInfoW(PADDRINFOW ai
)
5046 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
5047 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
5049 HeapFree(GetProcessHeap(), 0, ai
);
5054 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
5055 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
5057 #ifdef HAVE_GETNAMEINFO
5059 union generic_unix_sockaddr sa_u
;
5062 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
5063 serv
, servlen
, flags
);
5065 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
5068 WSASetLastError(WSAEFAULT
);
5069 return WSA_NOT_ENOUGH_MEMORY
;
5071 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
5072 return convert_eai_u2w(ret
);
5074 FIXME("getnameinfo() failed, not found during buildtime.\n");
5079 /***********************************************************************
5080 * getservbyport (WS2_32.56)
5082 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
5084 struct WS_servent
* retval
= NULL
;
5085 #ifdef HAVE_GETSERVBYPORT
5086 struct servent
* serv
;
5087 char *proto_str
= NULL
;
5089 if (proto
&& *proto
)
5091 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
5093 EnterCriticalSection( &csWSgetXXXbyYYY
);
5094 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
5095 retval
= WS_dup_se(serv
);
5097 else SetLastError(WSANO_DATA
);
5098 LeaveCriticalSection( &csWSgetXXXbyYYY
);
5099 HeapFree( GetProcessHeap(), 0, proto_str
);
5101 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
5106 /***********************************************************************
5107 * gethostname (WS2_32.57)
5109 int WINAPI
WS_gethostname(char *name
, int namelen
)
5111 TRACE("name %p, len %d\n", name
, namelen
);
5113 if (gethostname(name
, namelen
) == 0)
5115 TRACE("<- '%s'\n", name
);
5118 SetLastError((errno
== EINVAL
) ? WSAEFAULT
: wsaErrno());
5119 TRACE("<- ERROR !\n");
5120 return SOCKET_ERROR
;
5124 /* ------------------------------------- Windows sockets extensions -- *
5126 * ------------------------------------------------------------------- */
5128 /***********************************************************************
5129 * WSAEnumNetworkEvents (WS2_32.36)
5131 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
5135 int errors
[FD_MAX_EVENTS
];
5137 TRACE("%08lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
5139 SERVER_START_REQ( get_socket_event
)
5141 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5142 req
->service
= TRUE
;
5143 req
->c_event
= wine_server_obj_handle( hEvent
);
5144 wine_server_set_reply( req
, errors
, sizeof(errors
) );
5145 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
5150 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
5151 lpEvent
->iErrorCode
[i
] = NtStatusToWSAError(errors
[i
]);
5154 SetLastError(WSAEINVAL
);
5155 return SOCKET_ERROR
;
5158 /***********************************************************************
5159 * WSAEventSelect (WS2_32.39)
5161 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
5165 TRACE("%08lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
5167 SERVER_START_REQ( set_socket_event
)
5169 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5171 req
->event
= wine_server_obj_handle( hEvent
);
5174 ret
= wine_server_call( req
);
5178 SetLastError(WSAEINVAL
);
5179 return SOCKET_ERROR
;
5182 /**********************************************************************
5183 * WSAGetOverlappedResult (WS2_32.40)
5185 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
5186 LPDWORD lpcbTransfer
, BOOL fWait
,
5191 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
5192 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
5194 if ( lpOverlapped
== NULL
)
5196 ERR( "Invalid pointer\n" );
5197 WSASetLastError(WSA_INVALID_PARAMETER
);
5201 status
= lpOverlapped
->Internal
;
5202 if (status
== STATUS_PENDING
)
5206 SetLastError( WSA_IO_INCOMPLETE
);
5210 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
5211 INFINITE
) == WAIT_FAILED
)
5213 status
= lpOverlapped
->Internal
;
5217 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
5220 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
5222 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
5227 /***********************************************************************
5228 * WSAAsyncSelect (WS2_32.101)
5230 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
5234 TRACE("%lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
5236 SERVER_START_REQ( set_socket_event
)
5238 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5241 req
->window
= wine_server_user_handle( hWnd
);
5243 ret
= wine_server_call( req
);
5247 SetLastError(WSAEINVAL
);
5248 return SOCKET_ERROR
;
5251 /***********************************************************************
5252 * WSACreateEvent (WS2_32.31)
5255 WSAEVENT WINAPI
WSACreateEvent(void)
5257 /* Create a manual-reset event, with initial state: unsignaled */
5260 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
5263 /***********************************************************************
5264 * WSACloseEvent (WS2_32.29)
5267 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
5269 TRACE ("event=%p\n", event
);
5271 return CloseHandle(event
);
5274 /***********************************************************************
5275 * WSASocketA (WS2_32.78)
5278 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
5279 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
5280 GROUP g
, DWORD dwFlags
)
5283 WSAPROTOCOL_INFOW info
;
5285 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
5286 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
5288 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
5290 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
5291 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
5292 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
5296 WSASetLastError( WSAEINVAL
);
5297 return SOCKET_ERROR
;
5300 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
5303 /***********************************************************************
5304 * WSASocketW (WS2_32.79)
5307 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
5308 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
5309 GROUP g
, DWORD dwFlags
)
5314 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
5315 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
5318 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
5319 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
5321 /* hack for WSADuplicateSocket */
5322 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
5323 ret
= lpProtocolInfo
->dwCatalogEntryId
;
5324 TRACE("\tgot duplicate %04lx\n", ret
);
5328 /* convert the socket family and type */
5329 af
= convert_af_w2u(af
);
5330 type
= convert_socktype_w2u(type
);
5334 if (af
== FROM_PROTOCOL_INFO
)
5335 af
= lpProtocolInfo
->iAddressFamily
;
5336 if (type
== FROM_PROTOCOL_INFO
)
5337 type
= lpProtocolInfo
->iSocketType
;
5338 if (protocol
== FROM_PROTOCOL_INFO
)
5339 protocol
= lpProtocolInfo
->iProtocol
;
5342 if ( af
== AF_UNSPEC
) /* did they not specify the address family? */
5346 if (type
== SOCK_STREAM
) { af
= AF_INET
; break; }
5348 if (type
== SOCK_DGRAM
) { af
= AF_INET
; break; }
5349 default: SetLastError(WSAEPROTOTYPE
); return INVALID_SOCKET
;
5352 SERVER_START_REQ( create_socket
)
5356 req
->protocol
= protocol
;
5357 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
5358 req
->attributes
= OBJ_INHERIT
;
5359 req
->flags
= dwFlags
;
5360 set_error( wine_server_call( req
) );
5361 ret
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
5366 TRACE("\tcreated %04lx\n", ret
);
5370 if (GetLastError() == WSAEACCES
) /* raw socket denied */
5372 if (type
== SOCK_RAW
)
5373 ERR_(winediag
)("Failed to create a socket of type SOCK_RAW, this requires special permissions.\n");
5375 ERR_(winediag
)("Failed to create socket, this requires special permissions.\n");
5376 SetLastError(WSAESOCKTNOSUPPORT
);
5379 WARN("\t\tfailed!\n");
5380 return INVALID_SOCKET
;
5383 /***********************************************************************
5384 * WSAJoinLeaf (WS2_32.58)
5387 SOCKET WINAPI
WSAJoinLeaf(
5389 const struct WS_sockaddr
*addr
,
5391 LPWSABUF lpCallerData
,
5392 LPWSABUF lpCalleeData
,
5398 return INVALID_SOCKET
;
5401 /***********************************************************************
5402 * __WSAFDIsSet (WS2_32.151)
5404 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
5406 int i
= set
->fd_count
;
5408 TRACE("(%ld,%p(%i))\n", s
, set
, i
);
5411 if (set
->fd_array
[i
] == s
) return 1;
5415 /***********************************************************************
5416 * WSAIsBlocking (WS2_32.114)
5418 BOOL WINAPI
WSAIsBlocking(void)
5420 /* By default WinSock should set all its sockets to non-blocking mode
5421 * and poll in PeekMessage loop when processing "blocking" ones. This
5422 * function is supposed to tell if the program is in this loop. Our
5423 * blocking calls are truly blocking so we always return FALSE.
5425 * Note: It is allowed to call this function without prior WSAStartup().
5432 /***********************************************************************
5433 * WSACancelBlockingCall (WS2_32.113)
5435 INT WINAPI
WSACancelBlockingCall(void)
5441 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
5443 FIXME("How was this called?\n");
5448 /***********************************************************************
5449 * WSASetBlockingHook (WS2_32.109)
5451 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
5453 FARPROC prev
= blocking_hook
;
5454 blocking_hook
= lpBlockFunc
;
5455 TRACE("hook %p\n", lpBlockFunc
);
5460 /***********************************************************************
5461 * WSAUnhookBlockingHook (WS2_32.110)
5463 INT WINAPI
WSAUnhookBlockingHook(void)
5465 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
5470 /* ----------------------------------- end of API stuff */
5472 /* ----------------------------------- helper functions -
5474 * TODO: Merge WS_dup_..() stuff into one function that
5475 * would operate with a generic structure containing internal
5476 * pointers (via a template of some kind).
5479 static int list_size(char** l
, int item_size
)
5484 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
5485 j
+= (i
+ 1) * sizeof(char*); }
5489 static int list_dup(char** l_src
, char** l_to
, int item_size
)
5494 for (i
= 0; l_src
[i
]; i
++) ;
5495 p
= (char *)(l_to
+ i
+ 1);
5496 for (i
= 0; l_src
[i
]; i
++)
5498 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
5499 memcpy(p
, l_src
[i
], count
);
5504 return p
- (char *)l_to
;
5509 /* create a hostent entry
5511 * Creates the entry with enough memory for the name, aliases
5512 * addresses, and the address pointers. Also copies the name
5513 * and sets up all the pointers. If "fill_addresses" is set then
5514 * sufficient memory for the addresses is also allocated and the
5515 * address pointers are set to this memory.
5517 * NOTE: The alias and address lists must be allocated with room
5518 * for the NULL item terminating the list. This is true even if
5519 * the list has no items ("aliases" and "addresses" must be
5520 * at least "1", a truly empty list is invalid).
5522 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int addresses
, int fill_addresses
)
5524 struct WS_hostent
*p_to
;
5527 int size
= (sizeof(struct WS_hostent
) +
5529 sizeof(char *)*aliases
+
5530 sizeof(char *)*addresses
);
5532 /* Allocate enough memory for the addresses */
5534 size
+= sizeof(struct in_addr
)*addresses
;
5536 if (!(p_to
= check_buffer_he(size
))) return NULL
;
5537 memset(p_to
, 0, size
);
5539 p
= (char *)(p_to
+ 1);
5544 p_to
->h_aliases
= (char **)p
;
5545 p
+= sizeof(char *)*aliases
;
5546 p_to
->h_addr_list
= (char **)p
;
5547 p
+= sizeof(char *)*addresses
;
5552 /* NOTE: h_aliases must be filled in manually, leave these
5553 * pointers NULL (already set to NULL by memset earlier).
5556 /* Fill in the list of address pointers */
5557 for (i
= 0; i
< addresses
; i
++)
5558 p_to
->h_addr_list
[i
] = (p
+= sizeof(struct in_addr
));
5559 p
+= sizeof(struct in_addr
);
5564 /* duplicate hostent entry
5565 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
5566 * Ditto for protoent and servent.
5568 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
5570 int addresses
= list_size(p_he
->h_addr_list
, p_he
->h_length
);
5571 int aliases
= list_size(p_he
->h_aliases
, 0);
5572 struct WS_hostent
*p_to
;
5574 p_to
= WS_create_he(p_he
->h_name
, aliases
, addresses
, FALSE
);
5576 if (!p_to
) return NULL
;
5577 p_to
->h_addrtype
= p_he
->h_addrtype
;
5578 p_to
->h_length
= p_he
->h_length
;
5580 list_dup(p_he
->h_aliases
, p_to
->h_aliases
, 0);
5581 list_dup(p_he
->h_addr_list
, p_to
->h_addr_list
, p_he
->h_length
);
5585 /* ----- protoent */
5587 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
)
5590 struct WS_protoent
*p_to
;
5592 int size
= (sizeof(*p_pe
) +
5593 strlen(p_pe
->p_name
) + 1 +
5594 list_size(p_pe
->p_aliases
, 0));
5596 if (!(p_to
= check_buffer_pe(size
))) return NULL
;
5597 p_to
->p_proto
= p_pe
->p_proto
;
5599 p
= (char *)(p_to
+ 1);
5601 strcpy(p
, p_pe
->p_name
);
5604 p_to
->p_aliases
= (char **)p
;
5605 list_dup(p_pe
->p_aliases
, p_to
->p_aliases
, 0);
5611 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
5614 struct WS_servent
*p_to
;
5616 int size
= (sizeof(*p_se
) +
5617 strlen(p_se
->s_proto
) + 1 +
5618 strlen(p_se
->s_name
) + 1 +
5619 list_size(p_se
->s_aliases
, 0));
5621 if (!(p_to
= check_buffer_se(size
))) return NULL
;
5622 p_to
->s_port
= p_se
->s_port
;
5624 p
= (char *)(p_to
+ 1);
5626 strcpy(p
, p_se
->s_name
);
5630 strcpy(p
, p_se
->s_proto
);
5633 p_to
->s_aliases
= (char **)p
;
5634 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
5639 /***********************************************************************
5640 * WSARecv (WS2_32.67)
5642 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5643 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
5644 LPWSAOVERLAPPED lpOverlapped
,
5645 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5647 return WS2_recv_base(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
5648 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
, NULL
);
5651 static int WS2_recv_base( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5652 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
5653 struct WS_sockaddr
*lpFrom
,
5654 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
5655 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
5656 LPWSABUF lpControlBuffer
)
5658 unsigned int i
, options
;
5660 struct ws2_async
*wsa
;
5661 DWORD timeout_start
= GetTickCount();
5662 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
5664 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
5665 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
5666 (lpFromlen
? *lpFromlen
: -1),
5667 lpOverlapped
, lpCompletionRoutine
);
5669 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
5670 TRACE( "fd=%d, options=%x\n", fd
, options
);
5672 if (fd
== -1) return SOCKET_ERROR
;
5674 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
5680 wsa
->hSocket
= SOCKET2HANDLE(s
);
5681 wsa
->flags
= *lpFlags
;
5682 wsa
->lpFlags
= lpFlags
;
5684 wsa
->addrlen
.ptr
= lpFromlen
;
5685 wsa
->control
= lpControlBuffer
;
5686 wsa
->n_iovecs
= dwBufferCount
;
5687 wsa
->first_iovec
= 0;
5688 for (i
= 0; i
< dwBufferCount
; i
++)
5690 /* check buffer first to trigger write watches */
5691 if (IsBadWritePtr( lpBuffers
[i
].buf
, lpBuffers
[i
].len
))
5696 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
5697 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
5702 n
= WS2_recv( fd
, wsa
);
5705 if (errno
== EINTR
) continue;
5706 if (errno
!= EAGAIN
)
5708 int loc_errno
= errno
;
5710 if (cvalue
) WS_AddCompletion( s
, cvalue
, sock_get_ntstatus(loc_errno
), 0 );
5715 *lpNumberOfBytesRecvd
= n
;
5717 if ((lpOverlapped
|| lpCompletionRoutine
) &&
5718 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
5720 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
5722 wsa
->user_overlapped
= lpOverlapped
;
5723 wsa
->completion_func
= lpCompletionRoutine
;
5724 release_sock_fd( s
, fd
);
5728 iosb
->u
.Status
= STATUS_PENDING
;
5729 iosb
->Information
= 0;
5731 SERVER_START_REQ( register_async
)
5733 req
->type
= ASYNC_TYPE_READ
;
5734 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
5735 req
->async
.callback
= wine_server_client_ptr( WS2_async_recv
);
5736 req
->async
.iosb
= wine_server_client_ptr( iosb
);
5737 req
->async
.arg
= wine_server_client_ptr( wsa
);
5738 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
5739 req
->async
.cvalue
= cvalue
;
5740 err
= wine_server_call( req
);
5744 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
5745 WSASetLastError( NtStatusToWSAError( err
));
5746 return SOCKET_ERROR
;
5749 iosb
->u
.Status
= STATUS_SUCCESS
;
5750 iosb
->Information
= n
;
5751 if (!wsa
->completion_func
)
5753 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
5754 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
5755 HeapFree( GetProcessHeap(), 0, wsa
);
5757 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
5758 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
5759 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5765 if ( _is_blocking(s
) )
5768 int timeout
= GET_RCVTIMEO(fd
);
5771 timeout
-= GetTickCount() - timeout_start
;
5772 if (timeout
< 0) timeout
= 0;
5776 pfd
.events
= POLLIN
;
5777 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
5779 if (!timeout
|| !poll( &pfd
, 1, timeout
))
5782 /* a timeout is not fatal */
5783 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5789 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5790 err
= WSAEWOULDBLOCK
;
5795 TRACE(" -> %i bytes\n", n
);
5796 HeapFree( GetProcessHeap(), 0, wsa
);
5797 release_sock_fd( s
, fd
);
5798 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5803 HeapFree( GetProcessHeap(), 0, wsa
);
5804 release_sock_fd( s
, fd
);
5805 WARN(" -> ERROR %d\n", err
);
5806 WSASetLastError( err
);
5807 return SOCKET_ERROR
;
5810 /***********************************************************************
5811 * WSARecvFrom (WS2_32.69)
5813 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5814 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
5815 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
5816 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5819 return WS2_recv_base( s
, lpBuffers
, dwBufferCount
,
5820 lpNumberOfBytesRecvd
, lpFlags
,
5822 lpOverlapped
, lpCompletionRoutine
, NULL
);
5825 /***********************************************************************
5826 * WSCInstallProvider (WS2_32.88)
5828 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
5829 LPCWSTR lpszProviderDllPath
,
5830 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
5831 DWORD dwNumberOfEntries
,
5834 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
5835 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
5836 dwNumberOfEntries
, lpErrno
);
5842 /***********************************************************************
5843 * WSCDeinstallProvider (WS2_32.83)
5845 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
5847 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
5853 /***********************************************************************
5854 * WSAAccept (WS2_32.26)
5856 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
5857 LPCONDITIONPROC lpfnCondition
, DWORD dwCallbackData
)
5860 int ret
= 0, size
= 0;
5861 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
5862 /* QOS SQOS, GQOS; */
5865 SOCKADDR src_addr
, dst_addr
;
5867 TRACE("Socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %d\n",
5868 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
5871 size
= sizeof(src_addr
);
5872 cs
= WS_accept(s
, &src_addr
, &size
);
5874 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
5876 if (!lpfnCondition
) return cs
;
5878 CallerId
.buf
= (char *)&src_addr
;
5879 CallerId
.len
= sizeof(src_addr
);
5881 CallerData
.buf
= NULL
;
5884 WS_getsockname(cs
, &dst_addr
, &size
);
5886 CalleeId
.buf
= (char *)&dst_addr
;
5887 CalleeId
.len
= sizeof(dst_addr
);
5890 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
5891 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
5896 if (addr
&& addrlen
)
5897 addr
= memcpy(addr
, &src_addr
, (*addrlen
> size
) ? size
: *addrlen
);
5900 SERVER_START_REQ( set_socket_deferred
)
5902 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5903 req
->deferred
= wine_server_obj_handle( SOCKET2HANDLE(cs
) );
5904 if ( !wine_server_call_err ( req
) )
5906 SetLastError( WSATRY_AGAIN
);
5907 WS_closesocket( cs
);
5911 return SOCKET_ERROR
;
5914 SetLastError(WSAECONNREFUSED
);
5915 return SOCKET_ERROR
;
5917 FIXME("Unknown return type from Condition function\n");
5918 SetLastError(WSAENOTSOCK
);
5919 return SOCKET_ERROR
;
5923 /***********************************************************************
5924 * WSADuplicateSocketA (WS2_32.32)
5926 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
5930 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
5931 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
5932 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
5933 /* I don't know what the real Windoze does next, this is a hack */
5934 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
5935 * the target use the global duplicate, or we could copy a reference to us to the structure
5936 * and let the target duplicate it from us, but let's do it as simple as possible */
5937 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
5938 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
5939 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
5940 0, FALSE
, DUPLICATE_SAME_ACCESS
);
5941 CloseHandle(hProcess
);
5942 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
5946 /***********************************************************************
5947 * WSADuplicateSocketW (WS2_32.33)
5949 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
5953 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
5955 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
5956 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
5957 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
5958 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
5959 0, FALSE
, DUPLICATE_SAME_ACCESS
);
5960 CloseHandle(hProcess
);
5961 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
5965 /***********************************************************************
5966 * WSAInstallServiceClassA (WS2_32.48)
5968 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
5970 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
5971 WSASetLastError(WSAEACCES
);
5972 return SOCKET_ERROR
;
5975 /***********************************************************************
5976 * WSAInstallServiceClassW (WS2_32.49)
5978 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
5980 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
5981 WSASetLastError(WSAEACCES
);
5982 return SOCKET_ERROR
;
5985 /***********************************************************************
5986 * WSARemoveServiceClass (WS2_32.70)
5988 int WINAPI
WSARemoveServiceClass(LPGUID info
)
5990 FIXME("Request to remove service %p\n",info
);
5991 WSASetLastError(WSATYPE_NOT_FOUND
);
5992 return SOCKET_ERROR
;
5995 /***********************************************************************
5996 * inet_ntop (WS2_32.@)
5998 PCSTR WINAPI
WS_inet_ntop( INT family
, PVOID addr
, PSTR buffer
, SIZE_T len
)
6000 #ifdef HAVE_INET_NTOP
6001 struct WS_in6_addr
*in6
;
6002 struct WS_in_addr
*in
;
6005 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
6008 WSASetLastError( STATUS_INVALID_PARAMETER
);
6017 pdst
= inet_ntop( AF_INET
, &in
->WS_s_addr
, buffer
, len
);
6023 pdst
= inet_ntop( AF_INET6
, in6
->WS_s6_addr
, buffer
, len
);
6027 WSASetLastError( WSAEAFNOSUPPORT
);
6031 if (!pdst
) WSASetLastError( STATUS_INVALID_PARAMETER
);
6034 FIXME( "not supported on this platform\n" );
6035 WSASetLastError( WSAEAFNOSUPPORT
);
6040 /***********************************************************************
6041 * WSAStringToAddressA (WS2_32.80)
6043 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
6045 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
6046 LPSOCKADDR lpAddress
,
6047 LPINT lpAddressLength
)
6050 LPSTR workBuffer
=NULL
,ptrPort
;
6052 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_a(AddressString
), AddressFamily
,
6053 lpProtocolInfo
, lpAddress
, lpAddressLength
);
6055 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
6059 WSASetLastError(WSAEINVAL
);
6060 return SOCKET_ERROR
;
6064 FIXME("ProtocolInfo not implemented.\n");
6066 workBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
6067 strlen(AddressString
) + 1);
6070 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6071 return SOCKET_ERROR
;
6074 strcpy(workBuffer
, AddressString
);
6076 switch(AddressFamily
)
6080 struct in_addr inetaddr
;
6082 /* If lpAddressLength is too small, tell caller the size we need */
6083 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
6085 *lpAddressLength
= sizeof(SOCKADDR_IN
);
6089 *lpAddressLength
= sizeof(SOCKADDR_IN
);
6090 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
6092 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= AF_INET
;
6094 ptrPort
= strchr(workBuffer
, ':');
6097 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= htons(atoi(ptrPort
+1));
6102 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= 0;
6105 if(inet_aton(workBuffer
, &inetaddr
) > 0)
6107 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
6118 struct in6_addr inetaddr
;
6119 /* If lpAddressLength is too small, tell caller the size we need */
6120 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
6122 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
6126 #ifdef HAVE_INET_PTON
6127 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
6128 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
6130 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_family
= WS_AF_INET6
;
6132 /* This one is a bit tricky. An IPv6 address contains colons, so the
6133 * check from IPv4 doesn't work like that. However, IPv6 addresses that
6134 * contain a port are written with braces like [fd12:3456:7890::1]:12345
6135 * so what we will do is to look for ']', check if the next char is a
6136 * colon, and if it is, parse the port as in IPv4. */
6138 ptrPort
= strchr(workBuffer
, ']');
6139 if(ptrPort
&& *(++ptrPort
) == ':')
6141 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= htons(atoi(ptrPort
+1));
6146 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= 0;
6149 if(inet_pton(AF_INET6
, workBuffer
, &inetaddr
) > 0)
6151 memcpy(&((LPSOCKADDR_IN6
)lpAddress
)->sin6_addr
, &inetaddr
,
6152 sizeof(struct in6_addr
));
6156 #endif /* HAVE_INET_PTON */
6162 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
6163 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
6167 HeapFree(GetProcessHeap(), 0, workBuffer
);
6170 WSASetLastError(res
);
6171 return SOCKET_ERROR
;
6174 /***********************************************************************
6175 * WSAStringToAddressW (WS2_32.81)
6177 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
6178 * If this should be the case, it would be required to map these digits
6179 * to Unicode digits (0-9) using FoldString first.
6181 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
6183 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
6184 LPSOCKADDR lpAddress
,
6185 LPINT lpAddressLength
)
6188 LPSTR workBuffer
=NULL
;
6189 WSAPROTOCOL_INFOA infoA
;
6190 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
6192 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
6193 lpAddress
, lpAddressLength
);
6195 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
6197 /* if ProtocolInfo is available - convert to ANSI variant */
6200 lpProtoInfoA
= &infoA
;
6201 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
6203 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
6204 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
6206 WSASetLastError( WSAEINVAL
);
6207 return SOCKET_ERROR
;
6213 /* Translate AddressString to ANSI code page - assumes that only
6214 standard digits 0-9 are used with this API call */
6215 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
6216 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
6220 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
6221 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
6222 lpAddress
,lpAddressLength
);
6223 HeapFree( GetProcessHeap(), 0, workBuffer
);
6227 res
= WSA_NOT_ENOUGH_MEMORY
;
6232 WSASetLastError(res
);
6233 return SOCKET_ERROR
;
6236 /***********************************************************************
6237 * WSAAddressToStringA (WS2_32.27)
6239 * See WSAAddressToStringW
6241 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
6242 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
6246 CHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
6249 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
6251 if (!sockaddr
) return SOCKET_ERROR
;
6252 if (!string
|| !lenstr
) return SOCKET_ERROR
;
6254 switch(sockaddr
->sa_family
)
6257 if (len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
6258 sprintf( buffer
, "%u.%u.%u.%u:%u",
6259 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
6260 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
6261 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
6262 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
6263 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
6265 p
= strchr( buffer
, ':' );
6266 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
6271 struct WS_sockaddr_in6
*sockaddr6
= (LPSOCKADDR_IN6
) sockaddr
;
6274 if (len
< sizeof(SOCKADDR_IN6
)) return SOCKET_ERROR
;
6275 if ((sockaddr6
->sin6_port
))
6276 strcpy(buffer
, "[");
6277 if (!WS_inet_ntop(WS_AF_INET6
, &sockaddr6
->sin6_addr
, buffer
+strlen(buffer
), sizeof(buffer
)))
6279 WSASetLastError(WSAEINVAL
);
6280 return SOCKET_ERROR
;
6282 if ((sockaddr6
->sin6_scope_id
))
6283 sprintf(buffer
+strlen(buffer
), "%%%u", sockaddr6
->sin6_scope_id
);
6284 if ((sockaddr6
->sin6_port
))
6285 sprintf(buffer
+strlen(buffer
), "]:%u", ntohs(sockaddr6
->sin6_port
));
6290 WSASetLastError(WSAEINVAL
);
6291 return SOCKET_ERROR
;
6294 size
= strlen( buffer
) + 1;
6299 WSASetLastError(WSAEFAULT
);
6300 return SOCKET_ERROR
;
6304 strcpy( string
, buffer
);
6308 /***********************************************************************
6309 * WSAAddressToStringW (WS2_32.28)
6311 * Convert a sockaddr address into a readable address string.
6314 * sockaddr [I] Pointer to a sockaddr structure.
6315 * len [I] Size of the sockaddr structure.
6316 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
6317 * string [I/O] Pointer to a buffer to receive the address string.
6318 * lenstr [I/O] Size of the receive buffer in WCHARs.
6322 * Failure: SOCKET_ERROR
6325 * The 'info' parameter is ignored.
6327 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
6328 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
6333 WCHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
6336 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
6339 ret
= WSAAddressToStringA(sockaddr
, len
, NULL
, bufAddr
, &size
);
6341 if (ret
) return ret
;
6343 MultiByteToWideChar( CP_ACP
, 0, bufAddr
, size
, buffer
, sizeof( buffer
)/sizeof(WCHAR
));
6348 WSASetLastError(WSAEFAULT
);
6349 return SOCKET_ERROR
;
6353 lstrcpyW( string
, buffer
);
6357 /***********************************************************************
6358 * WSAEnumNameSpaceProvidersA (WS2_32.34)
6360 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
6362 FIXME( "(%p %p) Stub!\n", len
, buffer
);
6366 /***********************************************************************
6367 * WSAEnumNameSpaceProvidersW (WS2_32.35)
6369 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
6371 FIXME( "(%p %p) Stub!\n", len
, buffer
);
6375 /***********************************************************************
6376 * WSAGetQOSByName (WS2_32.41)
6378 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
6380 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
6384 /***********************************************************************
6385 * WSAGetServiceClassInfoA (WS2_32.42)
6387 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
6388 LPWSASERVICECLASSINFOA info
)
6390 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
6392 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6393 return SOCKET_ERROR
;
6396 /***********************************************************************
6397 * WSAGetServiceClassInfoW (WS2_32.43)
6399 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
6400 LPWSASERVICECLASSINFOW info
)
6402 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
6404 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6405 return SOCKET_ERROR
;
6408 /***********************************************************************
6409 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
6411 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
6413 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
6414 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6415 return SOCKET_ERROR
;
6418 /***********************************************************************
6419 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
6421 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
6423 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
6424 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6425 return SOCKET_ERROR
;
6428 /***********************************************************************
6429 * WSALookupServiceBeginA (WS2_32.59)
6431 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
6432 DWORD dwControlFlags
,
6435 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
6437 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6438 return SOCKET_ERROR
;
6441 /***********************************************************************
6442 * WSALookupServiceBeginW (WS2_32.60)
6444 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
6445 DWORD dwControlFlags
,
6448 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
6450 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6451 return SOCKET_ERROR
;
6454 /***********************************************************************
6455 * WSALookupServiceBeginW (WS2_32.61)
6457 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
6459 FIXME("(%p) Stub!\n", lookup
);
6463 /***********************************************************************
6464 * WSALookupServiceNextA (WS2_32.62)
6466 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
6468 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
6469 WSASetLastError(WSA_E_NO_MORE
);
6470 return SOCKET_ERROR
;
6473 /***********************************************************************
6474 * WSALookupServiceNextW (WS2_32.63)
6476 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
6478 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
6479 WSASetLastError(WSA_E_NO_MORE
);
6480 return SOCKET_ERROR
;
6483 /***********************************************************************
6484 * WSANtohl (WS2_32.64)
6486 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
6488 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
6490 if (!lphostlong
) return WSAEFAULT
;
6492 *lphostlong
= ntohl( netlong
);
6496 /***********************************************************************
6497 * WSANtohs (WS2_32.65)
6499 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
6501 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
6503 if (!lphostshort
) return WSAEFAULT
;
6505 *lphostshort
= ntohs( netshort
);
6509 /***********************************************************************
6510 * WSAProviderConfigChange (WS2_32.66)
6512 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
6513 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
6515 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
6516 return SOCKET_ERROR
;
6519 /***********************************************************************
6520 * WSARecvDisconnect (WS2_32.68)
6522 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
6524 TRACE( "(0x%04lx %p)\n", s
, disconnectdata
);
6526 return WS_shutdown( s
, 0 );
6529 /***********************************************************************
6530 * WSASetServiceA (WS2_32.76)
6532 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
6534 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
6538 /***********************************************************************
6539 * WSASetServiceW (WS2_32.77)
6541 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
6543 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
6547 /***********************************************************************
6548 * WSCEnableNSProvider (WS2_32.84)
6550 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
6552 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
6556 /***********************************************************************
6557 * WSCGetProviderPath (WS2_32.86)
6559 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
6561 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
6563 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
6565 *errcode
= WSAEINVAL
;
6566 return SOCKET_ERROR
;
6569 /***********************************************************************
6570 * WSCInstallNameSpace (WS2_32.87)
6572 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
6573 DWORD version
, LPGUID provider
)
6575 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
6576 namespace, version
, debugstr_guid(provider
) );
6580 /***********************************************************************
6581 * WSCUnInstallNameSpace (WS2_32.89)
6583 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
6585 FIXME("(%p) Stub!\n", lpProviderId
);
6589 /***********************************************************************
6590 * WSCWriteProviderOrder (WS2_32.91)
6592 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
6594 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);
6598 /***********************************************************************
6599 * WSANSPIoctl (WS2_32.91)
6601 INT WINAPI
WSANSPIoctl( HANDLE hLookup
, DWORD dwControlCode
, LPVOID lpvInBuffer
,
6602 DWORD cbInBuffer
, LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
6603 LPDWORD lpcbBytesReturned
, LPWSACOMPLETION lpCompletion
)
6605 FIXME("(%p, 0x%08x, %p, 0x%08x, %p, 0x%08x, %p, %p) Stub!\n", hLookup
, dwControlCode
,
6606 lpvInBuffer
, cbInBuffer
, lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpCompletion
);
6607 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6608 return SOCKET_ERROR
;