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/WSARecvFrom, wrapped in a different
175 * function name, so that internal calls from ws2_32 itself will not trigger
176 * programs like 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
);
184 static int WS2_recvfrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
185 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
,
186 struct WS_sockaddr
*lpFrom
,
187 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
188 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
190 /* critical section to protect some non-reentrant net function */
191 static CRITICAL_SECTION csWSgetXXXbyYYY
;
192 static CRITICAL_SECTION_DEBUG critsect_debug
=
194 0, 0, &csWSgetXXXbyYYY
,
195 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
196 0, 0, { (DWORD_PTR
)(__FILE__
": csWSgetXXXbyYYY") }
198 static CRITICAL_SECTION csWSgetXXXbyYYY
= { &critsect_debug
, -1, 0, 0, 0, 0 };
200 union generic_unix_sockaddr
202 struct sockaddr addr
;
203 char data
[128]; /* should be big enough for all families */
206 static inline const char *debugstr_sockaddr( const struct WS_sockaddr
*a
)
208 if (!a
) return "(nil)";
209 switch (a
->sa_family
)
212 return wine_dbg_sprintf("{ family AF_INET, address %s, port %d }",
213 inet_ntoa(((const struct sockaddr_in
*)a
)->sin_addr
),
214 ntohs(((const struct sockaddr_in
*)a
)->sin_port
));
219 struct WS_sockaddr_in6
*sin
= (struct WS_sockaddr_in6
*)a
;
221 p
= WS_inet_ntop( WS_AF_INET6
, &sin
->sin6_addr
, buf
, sizeof(buf
) );
223 p
= "(unknown IPv6 address)";
224 return wine_dbg_sprintf("{ family AF_INET6, address %s, port %d }",
225 p
, ntohs(sin
->sin6_port
));
231 memcpy( &addr
, ((const SOCKADDR_IRDA
*)a
)->irdaDeviceID
, sizeof(addr
) );
232 addr
= ntohl( addr
);
233 return wine_dbg_sprintf("{ family AF_IRDA, addr %08x, name %s }",
235 ((const SOCKADDR_IRDA
*)a
)->irdaServiceName
);
238 return wine_dbg_sprintf("{ family %d }", a
->sa_family
);
242 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
243 #define SOCKET2HANDLE(s) ((HANDLE)(s))
244 #define HANDLE2SOCKET(h) ((SOCKET)(h))
246 /****************************************************************
247 * Async IO declarations
248 ****************************************************************/
250 typedef struct ws2_async
254 LPWSAOVERLAPPED user_overlapped
;
255 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func
;
256 IO_STATUS_BLOCK local_iosb
;
257 struct WS_sockaddr
*addr
;
260 int val
; /* for send operations */
261 int *ptr
; /* for recv operations */
264 unsigned int n_iovecs
;
265 unsigned int first_iovec
;
266 struct iovec iovec
[1];
269 typedef struct ws2_accept_async
271 HANDLE listen_socket
;
272 HANDLE accept_socket
;
273 LPOVERLAPPED user_overlapped
;
275 PVOID buf
; /* buffer to write data to */
279 struct ws2_async
*read
;
282 /****************************************************************/
284 /* ----------------------------------- internal data */
286 /* ws_... struct conversion flags */
288 typedef struct /* WSAAsyncSelect() control struct */
290 HANDLE service
, event
, sock
;
296 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
297 #define WS_MAX_UDP_DATAGRAM 1024
298 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
300 /* hostent's, servent's and protent's are stored in one buffer per thread,
301 * as documented on MSDN for the functions that return any of the buffers */
302 struct per_thread_data
305 struct WS_hostent
*he_buffer
;
306 struct WS_servent
*se_buffer
;
307 struct WS_protoent
*pe_buffer
;
313 /* internal: routing description information */
320 static INT num_startup
; /* reference counter */
321 static FARPROC blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
323 /* function prototypes */
324 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int addresses
, int fill_addresses
);
325 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
326 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
);
327 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
329 int WSAIOCTL_GetInterfaceCount(void);
330 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
332 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
, ULONG Information
);
334 #define MAP_OPTION(opt) { WS_##opt, opt }
336 static const int ws_sock_map
[][2] =
338 MAP_OPTION( SO_DEBUG
),
339 MAP_OPTION( SO_ACCEPTCONN
),
340 MAP_OPTION( SO_REUSEADDR
),
341 MAP_OPTION( SO_KEEPALIVE
),
342 MAP_OPTION( SO_DONTROUTE
),
343 MAP_OPTION( SO_BROADCAST
),
344 MAP_OPTION( SO_LINGER
),
345 MAP_OPTION( SO_OOBINLINE
),
346 MAP_OPTION( SO_SNDBUF
),
347 MAP_OPTION( SO_RCVBUF
),
348 MAP_OPTION( SO_ERROR
),
349 MAP_OPTION( SO_TYPE
),
351 MAP_OPTION( SO_RCVTIMEO
),
354 MAP_OPTION( SO_SNDTIMEO
),
358 static const int ws_tcp_map
[][2] =
361 MAP_OPTION( TCP_NODELAY
),
365 static const int ws_ip_map
[][2] =
367 MAP_OPTION( IP_MULTICAST_IF
),
368 MAP_OPTION( IP_MULTICAST_TTL
),
369 MAP_OPTION( IP_MULTICAST_LOOP
),
370 MAP_OPTION( IP_ADD_MEMBERSHIP
),
371 MAP_OPTION( IP_DROP_MEMBERSHIP
),
372 MAP_OPTION( IP_OPTIONS
),
374 MAP_OPTION( IP_HDRINCL
),
376 MAP_OPTION( IP_TOS
),
377 MAP_OPTION( IP_TTL
),
380 static const int ws_ipv6_map
[][2] =
382 #ifdef IPV6_ADD_MEMBERSHIP
383 MAP_OPTION( IPV6_ADD_MEMBERSHIP
),
385 #ifdef IPV6_DROP_MEMBERSHIP
386 MAP_OPTION( IPV6_DROP_MEMBERSHIP
),
388 MAP_OPTION( IPV6_MULTICAST_IF
),
389 MAP_OPTION( IPV6_MULTICAST_HOPS
),
390 MAP_OPTION( IPV6_MULTICAST_LOOP
),
391 MAP_OPTION( IPV6_UNICAST_HOPS
),
392 MAP_OPTION( IPV6_V6ONLY
),
395 static const int ws_af_map
[][2] =
397 MAP_OPTION( AF_UNSPEC
),
398 MAP_OPTION( AF_INET
),
399 MAP_OPTION( AF_INET6
),
401 MAP_OPTION( AF_IPX
),
404 MAP_OPTION( AF_IRDA
),
406 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
409 static const int ws_socktype_map
[][2] =
411 MAP_OPTION( SOCK_DGRAM
),
412 MAP_OPTION( SOCK_STREAM
),
413 MAP_OPTION( SOCK_RAW
),
414 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
417 static const int ws_proto_map
[][2] =
419 MAP_OPTION( IPPROTO_IP
),
420 MAP_OPTION( IPPROTO_TCP
),
421 MAP_OPTION( IPPROTO_UDP
),
422 MAP_OPTION( IPPROTO_ICMP
),
423 MAP_OPTION( IPPROTO_IGMP
),
424 MAP_OPTION( IPPROTO_RAW
),
425 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
428 static const int ws_aiflag_map
[][2] =
430 MAP_OPTION( AI_PASSIVE
),
431 MAP_OPTION( AI_CANONNAME
),
432 MAP_OPTION( AI_NUMERICHOST
),
433 MAP_OPTION( AI_ADDRCONFIG
),
436 static const int ws_niflag_map
[][2] =
438 MAP_OPTION( NI_NOFQDN
),
439 MAP_OPTION( NI_NUMERICHOST
),
440 MAP_OPTION( NI_NAMEREQD
),
441 MAP_OPTION( NI_NUMERICSERV
),
442 MAP_OPTION( NI_DGRAM
),
445 static const int ws_eai_map
[][2] =
447 MAP_OPTION( EAI_AGAIN
),
448 MAP_OPTION( EAI_BADFLAGS
),
449 MAP_OPTION( EAI_FAIL
),
450 MAP_OPTION( EAI_FAMILY
),
451 MAP_OPTION( EAI_MEMORY
),
452 /* Note: EAI_NODATA is deprecated, but still
453 * used by Windows and Linux... We map the newer
454 * EAI_NONAME to EAI_NODATA for now until Windows
458 MAP_OPTION( EAI_NODATA
),
461 { WS_EAI_NODATA
, EAI_NONAME
},
464 MAP_OPTION( EAI_SERVICE
),
465 MAP_OPTION( EAI_SOCKTYPE
),
469 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
471 /* ----------------------------------- error handling */
473 static NTSTATUS
sock_get_ntstatus( int err
)
477 case EBADF
: return STATUS_INVALID_HANDLE
;
478 case EBUSY
: return STATUS_DEVICE_BUSY
;
480 case EACCES
: return STATUS_ACCESS_DENIED
;
481 case EFAULT
: return STATUS_NO_MEMORY
;
482 case EINVAL
: return STATUS_INVALID_PARAMETER
;
484 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
485 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
486 case EINPROGRESS
: return STATUS_PENDING
;
487 case EALREADY
: return STATUS_NETWORK_BUSY
;
488 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
489 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
490 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
491 case EPROTONOSUPPORT
:
492 case ESOCKTNOSUPPORT
:
495 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
496 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
497 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
498 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
499 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
500 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
501 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
502 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
503 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
504 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
505 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
507 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
508 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
510 case 0: return STATUS_SUCCESS
;
512 WARN("Unknown errno %d!\n", err
);
513 return STATUS_UNSUCCESSFUL
;
517 static UINT
sock_get_error( int err
)
521 case EINTR
: return WSAEINTR
;
522 case EBADF
: return WSAEBADF
;
524 case EACCES
: return WSAEACCES
;
525 case EFAULT
: return WSAEFAULT
;
526 case EINVAL
: return WSAEINVAL
;
527 case EMFILE
: return WSAEMFILE
;
528 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
529 case EINPROGRESS
: return WSAEINPROGRESS
;
530 case EALREADY
: return WSAEALREADY
;
531 case ENOTSOCK
: return WSAENOTSOCK
;
532 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
533 case EMSGSIZE
: return WSAEMSGSIZE
;
534 case EPROTOTYPE
: return WSAEPROTOTYPE
;
535 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
536 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
537 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
538 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
539 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
540 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
541 case EADDRINUSE
: return WSAEADDRINUSE
;
542 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
543 case ENETDOWN
: return WSAENETDOWN
;
544 case ENETUNREACH
: return WSAENETUNREACH
;
545 case ENETRESET
: return WSAENETRESET
;
546 case ECONNABORTED
: return WSAECONNABORTED
;
548 case ECONNRESET
: return WSAECONNRESET
;
549 case ENOBUFS
: return WSAENOBUFS
;
550 case EISCONN
: return WSAEISCONN
;
551 case ENOTCONN
: return WSAENOTCONN
;
552 case ESHUTDOWN
: return WSAESHUTDOWN
;
553 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
554 case ETIMEDOUT
: return WSAETIMEDOUT
;
555 case ECONNREFUSED
: return WSAECONNREFUSED
;
556 case ELOOP
: return WSAELOOP
;
557 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
558 case EHOSTDOWN
: return WSAEHOSTDOWN
;
559 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
560 case ENOTEMPTY
: return WSAENOTEMPTY
;
562 case EPROCLIM
: return WSAEPROCLIM
;
565 case EUSERS
: return WSAEUSERS
;
568 case EDQUOT
: return WSAEDQUOT
;
571 case ESTALE
: return WSAESTALE
;
574 case EREMOTE
: return WSAEREMOTE
;
577 /* just in case we ever get here and there are no problems */
580 WARN("Unknown errno %d!\n", err
);
581 return WSAEOPNOTSUPP
;
585 static UINT
wsaErrno(void)
587 int loc_errno
= errno
;
588 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
590 return sock_get_error( loc_errno
);
593 /* most ws2 overlapped functions return an ntstatus-based error code */
594 static NTSTATUS
wsaErrStatus(void)
596 int loc_errno
= errno
;
597 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
599 return sock_get_ntstatus(loc_errno
);
602 static UINT
wsaHerrno(int loc_errno
)
604 WARN("h_errno %d.\n", loc_errno
);
608 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
609 case TRY_AGAIN
: return WSATRY_AGAIN
;
610 case NO_RECOVERY
: return WSANO_RECOVERY
;
611 case NO_DATA
: return WSANO_DATA
;
612 case ENOBUFS
: return WSAENOBUFS
;
616 WARN("Unknown h_errno %d!\n", loc_errno
);
617 return WSAEOPNOTSUPP
;
621 static inline DWORD
NtStatusToWSAError( const DWORD status
)
623 /* We only need to cover the status codes set by server async request handling */
627 case STATUS_SUCCESS
: wserr
= 0; break;
628 case STATUS_PENDING
: wserr
= WSA_IO_PENDING
; break;
629 case STATUS_OBJECT_TYPE_MISMATCH
: wserr
= WSAENOTSOCK
; break;
630 case STATUS_INVALID_HANDLE
: wserr
= WSAEBADF
; break;
631 case STATUS_INVALID_PARAMETER
: wserr
= WSAEINVAL
; break;
632 case STATUS_PIPE_DISCONNECTED
: wserr
= WSAESHUTDOWN
; break;
633 case STATUS_NETWORK_BUSY
: wserr
= WSAEALREADY
; break;
634 case STATUS_NETWORK_UNREACHABLE
: wserr
= WSAENETUNREACH
; break;
635 case STATUS_CONNECTION_REFUSED
: wserr
= WSAECONNREFUSED
; break;
636 case STATUS_CONNECTION_DISCONNECTED
: wserr
= WSAENOTCONN
; break;
637 case STATUS_CONNECTION_RESET
: wserr
= WSAECONNRESET
; break;
638 case STATUS_CONNECTION_ABORTED
: wserr
= WSAECONNABORTED
; break;
639 case STATUS_CANCELLED
: wserr
= WSA_OPERATION_ABORTED
; break;
640 case STATUS_ADDRESS_ALREADY_ASSOCIATED
: wserr
= WSAEADDRINUSE
; break;
641 case STATUS_IO_TIMEOUT
:
642 case STATUS_TIMEOUT
: wserr
= WSAETIMEDOUT
; break;
643 case STATUS_NO_MEMORY
: wserr
= WSAEFAULT
; break;
644 case STATUS_ACCESS_DENIED
: wserr
= WSAEACCES
; break;
645 case STATUS_TOO_MANY_OPENED_FILES
: wserr
= WSAEMFILE
; break;
646 case STATUS_CANT_WAIT
: wserr
= WSAEWOULDBLOCK
; break;
647 case STATUS_BUFFER_OVERFLOW
: wserr
= WSAEMSGSIZE
; break;
648 case STATUS_NOT_SUPPORTED
: wserr
= WSAEOPNOTSUPP
; break;
649 case STATUS_HOST_UNREACHABLE
: wserr
= WSAEHOSTUNREACH
; break;
652 wserr
= RtlNtStatusToDosError( status
);
653 FIXME( "Status code %08x converted to DOS error code %x\n", status
, wserr
);
658 /* set last error code from NT status without mapping WSA errors */
659 static inline unsigned int set_error( unsigned int err
)
663 err
= NtStatusToWSAError( err
);
669 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
672 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
677 static inline void release_sock_fd( SOCKET s
, int fd
)
679 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
682 static void _enable_event( HANDLE s
, unsigned int event
,
683 unsigned int sstate
, unsigned int cstate
)
685 SERVER_START_REQ( enable_socket_event
)
687 req
->handle
= wine_server_obj_handle( s
);
689 req
->sstate
= sstate
;
690 req
->cstate
= cstate
;
691 wine_server_call( req
);
696 static int _is_blocking(SOCKET s
)
699 SERVER_START_REQ( get_socket_event
)
701 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
702 req
->service
= FALSE
;
704 wine_server_call( req
);
705 ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
711 static unsigned int _get_sock_mask(SOCKET s
)
714 SERVER_START_REQ( get_socket_event
)
716 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
717 req
->service
= FALSE
;
719 wine_server_call( req
);
726 static void _sync_sock_state(SOCKET s
)
728 /* do a dummy wineserver request in order to let
729 the wineserver run through its select loop once */
730 (void)_is_blocking(s
);
733 static int _get_sock_error(SOCKET s
, unsigned int bit
)
735 int events
[FD_MAX_EVENTS
];
737 SERVER_START_REQ( get_socket_event
)
739 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
740 req
->service
= FALSE
;
742 wine_server_set_reply( req
, events
, sizeof(events
) );
743 wine_server_call( req
);
749 static struct per_thread_data
*get_per_thread_data(void)
751 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
752 /* lazy initialization */
755 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
756 NtCurrentTeb()->WinSockData
= ptb
;
761 static void free_per_thread_data(void)
763 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
767 /* delete scratch buffers */
768 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
769 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
770 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
771 ptb
->he_buffer
= NULL
;
772 ptb
->se_buffer
= NULL
;
773 ptb
->pe_buffer
= NULL
;
775 HeapFree( GetProcessHeap(), 0, ptb
);
776 NtCurrentTeb()->WinSockData
= NULL
;
779 /***********************************************************************
780 * DllMain (WS2_32.init)
782 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
784 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
786 case DLL_PROCESS_ATTACH
:
788 case DLL_PROCESS_DETACH
:
789 free_per_thread_data();
792 case DLL_THREAD_DETACH
:
793 free_per_thread_data();
799 /***********************************************************************
802 * Converts socket flags from Windows format.
803 * Return 1 if converted, 0 if not (error).
805 static int convert_sockopt(INT
*level
, INT
*optname
)
812 for(i
=0; i
<sizeof(ws_sock_map
)/sizeof(ws_sock_map
[0]); i
++) {
813 if( ws_sock_map
[i
][0] == *optname
)
815 *optname
= ws_sock_map
[i
][1];
819 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
822 *level
= IPPROTO_TCP
;
823 for(i
=0; i
<sizeof(ws_tcp_map
)/sizeof(ws_tcp_map
[0]); i
++) {
824 if ( ws_tcp_map
[i
][0] == *optname
)
826 *optname
= ws_tcp_map
[i
][1];
830 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
834 for(i
=0; i
<sizeof(ws_ip_map
)/sizeof(ws_ip_map
[0]); i
++) {
835 if (ws_ip_map
[i
][0] == *optname
)
837 *optname
= ws_ip_map
[i
][1];
841 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
843 case WS_IPPROTO_IPV6
:
844 *level
= IPPROTO_IPV6
;
845 for(i
=0; i
<sizeof(ws_ipv6_map
)/sizeof(ws_ipv6_map
[0]); i
++) {
846 if (ws_ipv6_map
[i
][0] == *optname
)
848 *optname
= ws_ipv6_map
[i
][1];
852 FIXME("Unknown IPPROTO_IPV6 optname 0x%x\n", *optname
);
854 default: FIXME("Unimplemented or unknown socket level\n");
859 /* ----------------------------------- Per-thread info (or per-process?) */
861 static char *strdup_lower(const char *str
)
864 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
868 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
871 else SetLastError(WSAENOBUFS
);
875 static inline int sock_error_p(int s
)
877 unsigned int optval
, optlen
;
879 optlen
= sizeof(optval
);
880 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
881 if (optval
) WARN("\t[%i] error: %d\n", s
, optval
);
885 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
886 * from an fd and return the value converted to milli seconds
887 * or -1 if there is an infinite time out */
888 static inline int get_rcvsnd_timeo( int fd
, int optname
)
891 unsigned int len
= sizeof(tv
);
892 int ret
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
894 ret
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
895 if( ret
<= 0 ) /* tv == {0,0} means infinite time out */
900 /* macro wrappers for portability */
902 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
904 #define GET_RCVTIMEO(fd) (-1)
908 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
910 #define GET_SNDTIMEO(fd) (-1)
913 /* utility: given an fd, will block until one of the events occurs */
914 static inline int do_block( int fd
, int events
, int timeout
)
922 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
933 convert_af_w2u(int windowsaf
) {
936 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
937 if (ws_af_map
[i
][0] == windowsaf
)
938 return ws_af_map
[i
][1];
939 FIXME("unhandled Windows address family %d\n", windowsaf
);
944 convert_af_u2w(int unixaf
) {
947 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
948 if (ws_af_map
[i
][1] == unixaf
)
949 return ws_af_map
[i
][0];
950 FIXME("unhandled UNIX address family %d\n", unixaf
);
955 convert_proto_w2u(int windowsproto
) {
958 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
959 if (ws_proto_map
[i
][0] == windowsproto
)
960 return ws_proto_map
[i
][1];
961 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
966 convert_proto_u2w(int unixproto
) {
969 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
970 if (ws_proto_map
[i
][1] == unixproto
)
971 return ws_proto_map
[i
][0];
972 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
977 convert_socktype_w2u(int windowssocktype
) {
980 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
981 if (ws_socktype_map
[i
][0] == windowssocktype
)
982 return ws_socktype_map
[i
][1];
983 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
988 convert_socktype_u2w(int unixsocktype
) {
991 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
992 if (ws_socktype_map
[i
][1] == unixsocktype
)
993 return ws_socktype_map
[i
][0];
994 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
998 /* ----------------------------------- API -----
1000 * Init / cleanup / error checking.
1003 /***********************************************************************
1004 * WSAStartup (WS2_32.115)
1006 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
1008 TRACE("verReq=%x\n", wVersionRequested
);
1010 if (LOBYTE(wVersionRequested
) < 1)
1011 return WSAVERNOTSUPPORTED
;
1013 if (!lpWSAData
) return WSAEINVAL
;
1017 /* that's the whole of the negotiation for now */
1018 lpWSAData
->wVersion
= wVersionRequested
;
1019 /* return winsock information */
1020 lpWSAData
->wHighVersion
= 0x0202;
1021 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
1022 strcpy(lpWSAData
->szSystemStatus
, "Running" );
1023 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
1024 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
1025 /* don't do anything with lpWSAData->lpVendorInfo */
1026 /* (some apps don't allocate the space for this field) */
1028 TRACE("succeeded\n");
1033 /***********************************************************************
1034 * WSACleanup (WS2_32.116)
1036 INT WINAPI
WSACleanup(void)
1042 SetLastError(WSANOTINITIALISED
);
1043 return SOCKET_ERROR
;
1047 /***********************************************************************
1048 * WSAGetLastError (WS2_32.111)
1050 INT WINAPI
WSAGetLastError(void)
1052 return GetLastError();
1055 /***********************************************************************
1056 * WSASetLastError (WS2_32.112)
1058 void WINAPI
WSASetLastError(INT iError
) {
1059 SetLastError(iError
);
1062 static struct WS_hostent
*check_buffer_he(int size
)
1064 struct per_thread_data
* ptb
= get_per_thread_data();
1067 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
1068 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1070 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
1071 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
1072 return ptb
->he_buffer
;
1075 static struct WS_servent
*check_buffer_se(int size
)
1077 struct per_thread_data
* ptb
= get_per_thread_data();
1080 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
1081 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1083 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
1084 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
1085 return ptb
->se_buffer
;
1088 static struct WS_protoent
*check_buffer_pe(int size
)
1090 struct per_thread_data
* ptb
= get_per_thread_data();
1093 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
1094 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1096 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
1097 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
1098 return ptb
->pe_buffer
;
1101 /* ----------------------------------- i/o APIs */
1103 static inline BOOL
supported_pf(int pf
)
1124 /**********************************************************************/
1126 /* Returns the length of the converted address if successful, 0 if it was too small to
1129 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
1130 union generic_unix_sockaddr
*uaddr
)
1132 unsigned int uaddrlen
= 0;
1134 switch (wsaddr
->sa_family
)
1139 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
1140 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
1142 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
1145 uaddrlen
= sizeof(struct sockaddr_ipx
);
1146 memset( uaddr
, 0, uaddrlen
);
1147 uipx
->sipx_family
=AF_IPX
;
1148 uipx
->sipx_port
=wsipx
->sa_socket
;
1149 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
1152 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
1153 #ifdef IPX_FRAME_NONE
1154 uipx
->sipx_type
=IPX_FRAME_NONE
;
1160 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
1161 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
1163 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
1164 * scope_id, one without.
1166 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
1167 uaddrlen
= sizeof(struct sockaddr_in6
);
1168 memset( uaddr
, 0, uaddrlen
);
1169 uin6
->sin6_family
= AF_INET6
;
1170 uin6
->sin6_port
= win6
->sin6_port
;
1171 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
1172 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1173 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
1175 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1178 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
1182 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
1183 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
1185 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
1187 uaddrlen
= sizeof(struct sockaddr_in
);
1188 memset( uaddr
, 0, uaddrlen
);
1189 uin
->sin_family
= AF_INET
;
1190 uin
->sin_port
= win
->sin_port
;
1191 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
1196 struct sockaddr_irda
*uin
= (struct sockaddr_irda
*)uaddr
;
1197 const SOCKADDR_IRDA
*win
= (const SOCKADDR_IRDA
*)wsaddr
;
1199 if (wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1201 uaddrlen
= sizeof(struct sockaddr_irda
);
1202 memset( uaddr
, 0, uaddrlen
);
1203 uin
->sir_family
= AF_IRDA
;
1204 if (!strncmp( win
->irdaServiceName
, "LSAP-SEL", strlen( "LSAP-SEL" ) ))
1206 unsigned int lsap_sel
= 0;
1208 sscanf( win
->irdaServiceName
, "LSAP-SEL%u", &lsap_sel
);
1209 uin
->sir_lsap_sel
= lsap_sel
;
1213 uin
->sir_lsap_sel
= LSAP_ANY
;
1214 memcpy( uin
->sir_name
, win
->irdaServiceName
, 25 );
1216 memcpy( &uin
->sir_addr
, win
->irdaDeviceID
, sizeof(uin
->sir_addr
) );
1220 case WS_AF_UNSPEC
: {
1221 /* Try to determine the needed space by the passed windows sockaddr space */
1222 switch (wsaddrlen
) {
1223 default: /* likely a ipv4 address */
1224 case sizeof(struct WS_sockaddr_in
):
1225 uaddrlen
= sizeof(struct sockaddr_in
);
1228 case sizeof(struct WS_sockaddr_ipx
):
1229 uaddrlen
= sizeof(struct sockaddr_ipx
);
1233 case sizeof(SOCKADDR_IRDA
):
1234 uaddrlen
= sizeof(struct sockaddr_irda
);
1237 case sizeof(struct WS_sockaddr_in6
):
1238 case sizeof(struct WS_sockaddr_in6_old
):
1239 uaddrlen
= sizeof(struct sockaddr_in6
);
1242 memset( uaddr
, 0, uaddrlen
);
1246 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
1252 static BOOL
is_sockaddr_bound(const struct sockaddr
*uaddr
, int uaddrlen
)
1254 switch (uaddr
->sa_family
)
1258 FIXME("don't know how to tell if IPX socket is bound, assuming it is!\n");
1263 static const struct sockaddr_in6 emptyAddr
;
1264 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) uaddr
;
1265 return in6
->sin6_port
|| memcmp(&in6
->sin6_addr
, &emptyAddr
.sin6_addr
, sizeof(struct in6_addr
));
1269 static const struct sockaddr_in emptyAddr
;
1270 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) uaddr
;
1271 return in
->sin_port
|| memcmp(&in
->sin_addr
, &emptyAddr
.sin_addr
, sizeof(struct in_addr
));
1276 FIXME("unknown address family %d\n", uaddr
->sa_family
);
1281 /* Returns 0 if successful, -1 if the buffer is too small */
1282 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
1286 switch(uaddr
->sa_family
)
1291 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
1292 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
1295 switch (*wsaddrlen
) /* how much can we copy? */
1299 *wsaddrlen
= sizeof(*wsipx
);
1300 wsipx
->sa_socket
=uipx
->sipx_port
;
1304 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
1312 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
1318 wsipx
->sa_family
=WS_AF_IPX
;
1330 const struct sockaddr_irda
*uin
= (const struct sockaddr_irda
*)uaddr
;
1331 SOCKADDR_IRDA
*win
= (SOCKADDR_IRDA
*)wsaddr
;
1333 if (*wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1335 win
->irdaAddressFamily
= WS_AF_IRDA
;
1336 memcpy( win
->irdaDeviceID
, &uin
->sir_addr
, sizeof(win
->irdaDeviceID
) );
1337 if (uin
->sir_lsap_sel
!= LSAP_ANY
)
1338 sprintf( win
->irdaServiceName
, "LSAP-SEL%u", uin
->sir_lsap_sel
);
1340 memcpy( win
->irdaServiceName
, uin
->sir_name
,
1341 sizeof(win
->irdaServiceName
) );
1346 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
1347 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
1349 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
1351 win6old
->sin6_family
= WS_AF_INET6
;
1352 win6old
->sin6_port
= uin6
->sin6_port
;
1353 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
1354 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1355 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1356 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
1357 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
1358 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
1359 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
1362 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1364 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1369 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
1370 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
1372 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
1374 win
->sin_family
= WS_AF_INET
;
1375 win
->sin_port
= uin
->sin_port
;
1376 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
1377 memset(win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
1378 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
1382 memset(wsaddr
,0,*wsaddrlen
);
1386 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
1392 /**************************************************************************
1393 * Functions for handling overlapped I/O
1394 **************************************************************************/
1396 /* user APC called upon async completion */
1397 static void WINAPI
ws2_async_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1399 ws2_async
*wsa
= arg
;
1401 if (wsa
->completion_func
) wsa
->completion_func( NtStatusToWSAError(iosb
->u
.Status
),
1402 iosb
->Information
, wsa
->user_overlapped
,
1404 HeapFree( GetProcessHeap(), 0, wsa
);
1407 /***********************************************************************
1408 * WS2_recv (INTERNAL)
1410 * Workhorse for both synchronous and asynchronous recv() operations.
1412 static int WS2_recv( int fd
, struct ws2_async
*wsa
)
1415 union generic_unix_sockaddr unix_sockaddr
;
1418 hdr
.msg_name
= NULL
;
1422 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
1423 hdr
.msg_name
= &unix_sockaddr
;
1426 hdr
.msg_namelen
= 0;
1428 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1429 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1430 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1431 hdr
.msg_accrights
= NULL
;
1432 hdr
.msg_accrightslen
= 0;
1434 hdr
.msg_control
= NULL
;
1435 hdr
.msg_controllen
= 0;
1439 if ( (n
= recvmsg(fd
, &hdr
, wsa
->flags
)) == -1 )
1442 /* if this socket is connected and lpFrom is not NULL, Linux doesn't give us
1443 * msg_name and msg_namelen from recvmsg, but it does set msg_namelen to zero.
1445 * quoting linux 2.6 net/ipv4/tcp.c:
1446 * "According to UNIX98, msg_name/msg_namelen are ignored
1447 * on connected socket. I was just happy when found this 8) --ANK"
1449 * likewise MSDN says that lpFrom and lpFromlen are ignored for
1450 * connection-oriented sockets, so don't try to update lpFrom.
1452 if (wsa
->addr
&& hdr
.msg_namelen
)
1453 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, wsa
->addr
, wsa
->addrlen
.ptr
);
1458 /***********************************************************************
1459 * WS2_async_recv (INTERNAL)
1461 * Handler for overlapped recv() operations.
1463 static NTSTATUS
WS2_async_recv( void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
1465 ws2_async
* wsa
= user
;
1470 case STATUS_ALERTED
:
1471 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
1474 result
= WS2_recv( fd
, wsa
);
1475 wine_server_release_fd( wsa
->hSocket
, fd
);
1478 status
= STATUS_SUCCESS
;
1479 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1483 if (errno
== EINTR
|| errno
== EAGAIN
)
1485 status
= STATUS_PENDING
;
1486 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1491 status
= wsaErrStatus();
1496 if (status
!= STATUS_PENDING
)
1498 iosb
->u
.Status
= status
;
1499 iosb
->Information
= result
;
1500 *apc
= ws2_async_apc
;
1505 /* user APC called upon async accept completion */
1506 static void WINAPI
ws2_async_accept_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1508 struct ws2_accept_async
*wsa
= arg
;
1510 HeapFree( GetProcessHeap(), 0, wsa
->read
);
1511 HeapFree( GetProcessHeap(), 0, wsa
);
1514 /***********************************************************************
1515 * WS2_async_accept_recv (INTERNAL)
1517 * This function is used to finish the read part of an accept request. It is
1518 * needed to place the completion on the correct socket (listener).
1520 static NTSTATUS
WS2_async_accept_recv( void *arg
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
, void **apc
)
1523 struct ws2_accept_async
*wsa
= arg
;
1525 status
= WS2_async_recv( wsa
->read
, iosb
, status
, &junk
);
1526 if (status
== STATUS_PENDING
)
1529 if (wsa
->user_overlapped
->hEvent
)
1530 SetEvent(wsa
->user_overlapped
->hEvent
);
1532 WS_AddCompletion( HANDLE2SOCKET(wsa
->listen_socket
), wsa
->cvalue
, iosb
->u
.Status
, iosb
->Information
);
1534 *apc
= ws2_async_accept_apc
;
1538 /***********************************************************************
1539 * WS2_async_accept (INTERNAL)
1541 * This is the function called to satisfy the AcceptEx callback
1543 static NTSTATUS
WS2_async_accept( void *arg
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
, void **apc
)
1545 struct ws2_accept_async
*wsa
= arg
;
1549 TRACE("status: 0x%x listen: %p, accept: %p\n", status
, wsa
->listen_socket
, wsa
->accept_socket
);
1551 if (status
== STATUS_ALERTED
)
1553 SERVER_START_REQ( accept_into_socket
)
1555 req
->lhandle
= wine_server_obj_handle( wsa
->listen_socket
);
1556 req
->ahandle
= wine_server_obj_handle( wsa
->accept_socket
);
1557 status
= wine_server_call( req
);
1561 if (status
== STATUS_CANT_WAIT
)
1562 return STATUS_PENDING
;
1564 if (status
== STATUS_INVALID_HANDLE
)
1566 FIXME("AcceptEx accepting socket closed but request was not cancelled\n");
1567 status
= STATUS_CANCELLED
;
1570 else if (status
== STATUS_HANDLES_CLOSED
)
1571 status
= STATUS_CANCELLED
; /* strange windows behavior */
1573 if (status
!= STATUS_SUCCESS
)
1576 /* WS2 Spec says size param is extra 16 bytes long...what do we put in it? */
1577 addr
= ((char *)wsa
->buf
) + wsa
->data_len
;
1578 len
= wsa
->local_len
- sizeof(int);
1579 WS_getpeername(HANDLE2SOCKET(wsa
->accept_socket
),
1580 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
1583 addr
+= wsa
->local_len
;
1584 len
= wsa
->remote_len
- sizeof(int);
1585 WS_getsockname(HANDLE2SOCKET(wsa
->accept_socket
),
1586 (struct WS_sockaddr
*)(addr
+ sizeof(int)), &len
);
1592 SERVER_START_REQ( register_async
)
1594 req
->type
= ASYNC_TYPE_READ
;
1595 req
->async
.handle
= wine_server_obj_handle( wsa
->accept_socket
);
1596 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept_recv
);
1597 req
->async
.iosb
= wine_server_client_ptr( iosb
);
1598 req
->async
.arg
= wine_server_client_ptr( wsa
);
1599 status
= wine_server_call( req
);
1603 if (status
!= STATUS_PENDING
)
1606 return STATUS_SUCCESS
;
1609 iosb
->u
.Status
= status
;
1610 iosb
->Information
= 0;
1612 if (wsa
->user_overlapped
->hEvent
)
1613 SetEvent(wsa
->user_overlapped
->hEvent
);
1615 WS_AddCompletion( HANDLE2SOCKET(wsa
->listen_socket
), wsa
->cvalue
, iosb
->u
.Status
, iosb
->Information
);
1617 *apc
= ws2_async_accept_apc
;
1621 /***********************************************************************
1622 * WS2_send (INTERNAL)
1624 * Workhorse for both synchronous and asynchronous send() operations.
1626 static int WS2_send( int fd
, struct ws2_async
*wsa
)
1629 union generic_unix_sockaddr unix_addr
;
1631 hdr
.msg_name
= NULL
;
1632 hdr
.msg_namelen
= 0;
1636 hdr
.msg_name
= &unix_addr
;
1637 hdr
.msg_namelen
= ws_sockaddr_ws2u( wsa
->addr
, wsa
->addrlen
.val
, &unix_addr
);
1638 if ( !hdr
.msg_namelen
)
1644 #if defined(HAVE_IPX) && defined(SOL_IPX)
1645 if(wsa
->addr
->sa_family
== WS_AF_IPX
)
1647 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
1649 unsigned int len
=sizeof(int);
1651 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1652 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1653 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1654 * ipx type in the sockaddr_opx structure with the stored value.
1656 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
1657 uipx
->sipx_type
= val
;
1662 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1663 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1664 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1665 hdr
.msg_accrights
= NULL
;
1666 hdr
.msg_accrightslen
= 0;
1668 hdr
.msg_control
= NULL
;
1669 hdr
.msg_controllen
= 0;
1673 return sendmsg(fd
, &hdr
, wsa
->flags
);
1676 /***********************************************************************
1677 * WS2_async_send (INTERNAL)
1679 * Handler for overlapped send() operations.
1681 static NTSTATUS
WS2_async_send(void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
1683 ws2_async
* wsa
= user
;
1688 case STATUS_ALERTED
:
1689 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
1692 /* check to see if the data is ready (non-blocking) */
1693 result
= WS2_send( fd
, wsa
);
1694 wine_server_release_fd( wsa
->hSocket
, fd
);
1698 int totalLength
= 0;
1700 status
= STATUS_SUCCESS
;
1701 for (i
= 0; i
< wsa
->n_iovecs
; i
++)
1702 totalLength
+= wsa
->iovec
[i
].iov_len
;
1703 if (result
< totalLength
)
1704 _enable_event( wsa
->hSocket
, FD_WRITE
, 0, 0 );
1708 if (errno
== EINTR
|| errno
== EAGAIN
)
1710 status
= STATUS_PENDING
;
1711 _enable_event( wsa
->hSocket
, FD_WRITE
, 0, 0 );
1715 status
= wsaErrStatus();
1721 if (status
!= STATUS_PENDING
)
1723 iosb
->u
.Status
= status
;
1724 iosb
->Information
= result
;
1725 *apc
= ws2_async_apc
;
1730 /***********************************************************************
1731 * WS2_async_shutdown (INTERNAL)
1733 * Handler for shutdown() operations on overlapped sockets.
1735 static NTSTATUS
WS2_async_shutdown( void* user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
, void **apc
)
1737 ws2_async
* wsa
= user
;
1742 case STATUS_ALERTED
:
1743 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
1746 switch ( wsa
->type
)
1748 case ASYNC_TYPE_READ
: err
= shutdown( fd
, 0 ); break;
1749 case ASYNC_TYPE_WRITE
: err
= shutdown( fd
, 1 ); break;
1751 status
= err
? wsaErrStatus() : STATUS_SUCCESS
;
1752 wine_server_release_fd( wsa
->hSocket
, fd
);
1755 iosb
->u
.Status
= status
;
1756 iosb
->Information
= 0;
1757 *apc
= ws2_async_apc
;
1761 /***********************************************************************
1762 * WS2_register_async_shutdown (INTERNAL)
1764 * Helper function for WS_shutdown() on overlapped sockets.
1766 static int WS2_register_async_shutdown( SOCKET s
, int type
)
1768 struct ws2_async
*wsa
;
1771 TRACE("s %ld type %d\n", s
, type
);
1773 wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
1777 wsa
->hSocket
= SOCKET2HANDLE(s
);
1779 wsa
->completion_func
= NULL
;
1781 SERVER_START_REQ( register_async
)
1784 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
1785 req
->async
.callback
= wine_server_client_ptr( WS2_async_shutdown
);
1786 req
->async
.iosb
= wine_server_client_ptr( &wsa
->local_iosb
);
1787 req
->async
.arg
= wine_server_client_ptr( wsa
);
1788 req
->async
.cvalue
= 0;
1789 status
= wine_server_call( req
);
1793 if (status
!= STATUS_PENDING
)
1795 HeapFree( GetProcessHeap(), 0, wsa
);
1796 return NtStatusToWSAError( status
);
1801 /***********************************************************************
1804 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
,
1811 TRACE("socket %04lx\n", s
);
1812 is_blocking
= _is_blocking(s
);
1815 /* try accepting first (if there is a deferred connection) */
1816 SERVER_START_REQ( accept_socket
)
1818 req
->lhandle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1819 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
1820 req
->attributes
= OBJ_INHERIT
;
1821 status
= wine_server_call( req
);
1822 as
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
1827 if (addr
) WS_getpeername(as
, addr
, addrlen32
);
1830 if (is_blocking
&& status
== STATUS_CANT_WAIT
)
1832 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1834 do_block(fd
, POLLIN
, -1);
1835 _sync_sock_state(s
); /* let wineserver notice connection */
1836 release_sock_fd( s
, fd
);
1838 } while (is_blocking
&& status
== STATUS_CANT_WAIT
);
1841 return INVALID_SOCKET
;
1844 /***********************************************************************
1847 static BOOL WINAPI
WS2_AcceptEx(SOCKET listener
, SOCKET acceptor
, PVOID dest
, DWORD dest_len
,
1848 DWORD local_addr_len
, DWORD rem_addr_len
, LPDWORD received
,
1849 LPOVERLAPPED overlapped
)
1852 struct ws2_accept_async
*wsa
;
1854 ULONG_PTR cvalue
= (overlapped
&& ((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)overlapped
: 0;
1856 TRACE("(%lx, %lx, %p, %d, %d, %d, %p, %p)\n", listener
, acceptor
, dest
, dest_len
, local_addr_len
,
1857 rem_addr_len
, received
, overlapped
);
1861 SetLastError(WSAEINVAL
);
1867 SetLastError(WSA_INVALID_PARAMETER
);
1871 fd
= get_sock_fd( listener
, FILE_READ_DATA
, NULL
);
1874 SetLastError(WSAENOTSOCK
);
1877 release_sock_fd( listener
, fd
);
1879 fd
= get_sock_fd( acceptor
, FILE_READ_DATA
, NULL
);
1882 SetLastError(WSAEINVAL
);
1885 release_sock_fd( acceptor
, fd
);
1887 wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
1890 SetLastError(WSAEFAULT
);
1894 wsa
->listen_socket
= SOCKET2HANDLE(listener
);
1895 wsa
->accept_socket
= SOCKET2HANDLE(acceptor
);
1896 wsa
->user_overlapped
= overlapped
;
1897 wsa
->cvalue
= cvalue
;
1899 wsa
->data_len
= dest_len
;
1900 wsa
->local_len
= local_addr_len
;
1901 wsa
->remote_len
= rem_addr_len
;
1906 /* set up a read request if we need it */
1907 wsa
->read
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[1]) );
1910 HeapFree( GetProcessHeap(), 0, wsa
);
1911 SetLastError(WSAEFAULT
);
1915 wsa
->read
->hSocket
= wsa
->accept_socket
;
1916 wsa
->read
->flags
= 0;
1917 wsa
->read
->addr
= NULL
;
1918 wsa
->read
->addrlen
.ptr
= NULL
;
1919 wsa
->read
->n_iovecs
= 1;
1920 wsa
->read
->first_iovec
= 0;
1921 wsa
->read
->iovec
[0].iov_base
= wsa
->buf
;
1922 wsa
->read
->iovec
[0].iov_len
= wsa
->data_len
;
1925 SERVER_START_REQ( register_async
)
1927 req
->type
= ASYNC_TYPE_READ
;
1928 req
->async
.handle
= wine_server_obj_handle( SOCKET2HANDLE(listener
) );
1929 req
->async
.callback
= wine_server_client_ptr( WS2_async_accept
);
1930 req
->async
.iosb
= wine_server_client_ptr( overlapped
);
1931 req
->async
.arg
= wine_server_client_ptr( wsa
);
1932 /* We don't set event or completion since we may also have to read */
1933 status
= wine_server_call( req
);
1937 if(status
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
1939 SetLastError( NtStatusToWSAError(status
) );
1943 /***********************************************************************
1944 * GetAcceptExSockaddrs
1946 static void WINAPI
WS2_GetAcceptExSockaddrs(PVOID buffer
, DWORD data_size
, DWORD local_size
, DWORD remote_size
,
1947 struct WS_sockaddr
**local_addr
, LPINT local_addr_len
,
1948 struct WS_sockaddr
**remote_addr
, LPINT remote_addr_len
)
1950 char *cbuf
= buffer
;
1951 TRACE("(%p, %d, %d, %d, %p, %p, %p, %p)\n", buffer
, data_size
, local_size
, remote_size
, local_addr
,
1952 local_addr_len
, remote_addr
, remote_addr_len
);
1955 *local_addr_len
= *(int *) cbuf
;
1956 *local_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
1960 *remote_addr_len
= *(int *) cbuf
;
1961 *remote_addr
= (struct WS_sockaddr
*)(cbuf
+ sizeof(int));
1964 /***********************************************************************
1967 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
1969 int fd
= get_sock_fd( s
, 0, NULL
);
1970 int res
= SOCKET_ERROR
;
1972 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
1976 if (!name
|| (name
->sa_family
&& !supported_pf(name
->sa_family
)))
1978 SetLastError(WSAEAFNOSUPPORT
);
1982 union generic_unix_sockaddr uaddr
;
1983 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
1986 SetLastError(WSAEFAULT
);
1991 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) &uaddr
;
1992 if (name
->sa_family
== WS_AF_INET6
&&
1993 !memcmp(&in6
->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
)))
1996 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
)) == -1)
1998 release_sock_fd( s
, fd
);
1999 SetLastError(WSAEAFNOSUPPORT
);
2000 return SOCKET_ERROR
;
2004 if (name
->sa_family
== WS_AF_INET
)
2006 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
2007 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
2009 /* Trying to bind to the default host interface, using
2010 * INADDR_ANY instead*/
2011 WARN("Trying to bind to magic IP address, using "
2012 "INADDR_ANY instead.\n");
2013 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_ANY
);
2016 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
2018 int loc_errno
= errno
;
2019 WARN("\tfailure - errno = %i\n", errno
);
2024 SetLastError(WSAENOTSOCK
);
2027 SetLastError(WSAEINVAL
);
2030 SetLastError(wsaErrno());
2036 res
=0; /* success */
2040 release_sock_fd( s
, fd
);
2045 /***********************************************************************
2046 * closesocket (WS2_32.3)
2048 int WINAPI
WS_closesocket(SOCKET s
)
2050 TRACE("socket %04lx\n", s
);
2051 if (CloseHandle(SOCKET2HANDLE(s
))) return 0;
2052 return SOCKET_ERROR
;
2055 static int do_connect(int fd
, const struct WS_sockaddr
* name
, int namelen
)
2057 union generic_unix_sockaddr uaddr
;
2058 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
2063 if (name
->sa_family
== WS_AF_INET
)
2065 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
2066 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
2068 /* Trying to connect to magic replace-loopback address,
2069 * assuming we really want to connect to localhost */
2070 TRACE("Trying to connect to magic IP address, using "
2071 "INADDR_LOOPBACK instead.\n");
2072 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_LOOPBACK
);
2076 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
2082 /***********************************************************************
2083 * connect (WS2_32.4)
2085 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
2087 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2089 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
2093 int ret
= do_connect(fd
, name
, namelen
);
2095 goto connect_success
;
2097 if (ret
== WSAEINPROGRESS
)
2099 /* tell wineserver that a connection is in progress */
2100 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2102 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
2103 if (_is_blocking(s
))
2107 do_block(fd
, POLLIN
| POLLOUT
, -1);
2108 _sync_sock_state(s
); /* let wineserver notice connection */
2109 /* retrieve any error codes from it */
2110 result
= _get_sock_error(s
, FD_CONNECT_BIT
);
2112 SetLastError(NtStatusToWSAError(result
));
2115 goto connect_success
;
2120 SetLastError(WSAEWOULDBLOCK
);
2127 release_sock_fd( s
, fd
);
2129 return SOCKET_ERROR
;
2132 release_sock_fd( s
, fd
);
2133 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2134 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
2135 FD_CONNECT
|FD_WINE_LISTENING
);
2139 /***********************************************************************
2140 * WSAConnect (WS2_32.30)
2142 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
2143 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
2144 LPQOS lpSQOS
, LPQOS lpGQOS
)
2146 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
2147 FIXME("unsupported parameters!\n");
2148 return WS_connect( s
, name
, namelen
);
2151 /***********************************************************************
2154 static BOOL WINAPI
WS2_ConnectEx(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
2155 PVOID sendBuf
, DWORD sendBufLen
, LPDWORD sent
, LPOVERLAPPED ov
)
2157 int fd
, ret
, status
;
2161 SetLastError( ERROR_INVALID_PARAMETER
);
2165 fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2168 SetLastError( WSAENOTSOCK
);
2172 TRACE("socket %04lx, ptr %p %s, length %d, sendptr %p, len %d, ov %p\n",
2173 s
, name
, debugstr_sockaddr(name
), namelen
, sendBuf
, sendBufLen
, ov
);
2175 /* FIXME: technically the socket has to be bound */
2176 ret
= do_connect(fd
, name
, namelen
);
2181 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2182 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
2183 FD_CONNECT
|FD_WINE_LISTENING
);
2185 wsabuf
.len
= sendBufLen
;
2186 wsabuf
.buf
= (char*) sendBuf
;
2188 /* WSASend takes care of completion if need be */
2189 if (WSASend(s
, &wsabuf
, sendBuf
? 1 : 0, sent
, 0, ov
, NULL
) != SOCKET_ERROR
)
2190 goto connection_success
;
2192 else if (ret
== WSAEINPROGRESS
)
2194 struct ws2_async
*wsa
;
2195 ULONG_PTR cvalue
= (((ULONG_PTR
)ov
->hEvent
& 1) == 0) ? (ULONG_PTR
)ov
: 0;
2197 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
2199 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
2201 /* Indirectly call WSASend */
2202 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) )))
2204 SetLastError(WSAEFAULT
);
2208 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)ov
;
2209 iosb
->u
.Status
= STATUS_PENDING
;
2210 iosb
->Information
= 0;
2212 wsa
->hSocket
= SOCKET2HANDLE(s
);
2214 wsa
->addrlen
.val
= 0;
2216 wsa
->n_iovecs
= sendBuf
? 1 : 0;
2217 wsa
->first_iovec
= 0;
2218 wsa
->completion_func
= NULL
;
2219 wsa
->iovec
[0].iov_base
= sendBuf
;
2220 wsa
->iovec
[0].iov_len
= sendBufLen
;
2222 SERVER_START_REQ( register_async
)
2224 req
->type
= ASYNC_TYPE_WRITE
;
2225 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
2226 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
2227 req
->async
.iosb
= wine_server_client_ptr( iosb
);
2228 req
->async
.arg
= wine_server_client_ptr( wsa
);
2229 req
->async
.event
= wine_server_obj_handle( ov
->hEvent
);
2230 req
->async
.cvalue
= cvalue
;
2231 status
= wine_server_call( req
);
2235 if (status
!= STATUS_PENDING
) HeapFree(GetProcessHeap(), 0, wsa
);
2237 /* If the connect already failed */
2238 if (status
== STATUS_PIPE_DISCONNECTED
)
2239 status
= _get_sock_error(s
, FD_CONNECT_BIT
);
2240 SetLastError( NtStatusToWSAError(status
) );
2248 release_sock_fd( s
, fd
);
2252 release_sock_fd( s
, fd
);
2257 /***********************************************************************
2258 * getpeername (WS2_32.5)
2260 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
2265 TRACE("socket: %04lx, ptr %p, len %08x\n", s
, name
, namelen
?*namelen
:0);
2267 fd
= get_sock_fd( s
, 0, NULL
);
2272 union generic_unix_sockaddr uaddr
;
2273 unsigned int uaddrlen
= sizeof(uaddr
);
2275 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) == 0)
2277 if (!name
|| !namelen
)
2278 SetLastError(WSAEFAULT
);
2279 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
2280 /* The buffer was too small */
2281 SetLastError(WSAEFAULT
);
2286 SetLastError(wsaErrno());
2287 release_sock_fd( s
, fd
);
2292 /***********************************************************************
2293 * getsockname (WS2_32.6)
2295 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
2300 TRACE("socket: %04lx, ptr %p, len %8x\n", s
, name
, *namelen
);
2302 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
2303 if( (name
== NULL
) || (namelen
== NULL
) )
2305 SetLastError( WSAEFAULT
);
2306 return SOCKET_ERROR
;
2309 fd
= get_sock_fd( s
, 0, NULL
);
2314 union generic_unix_sockaddr uaddr
;
2315 unsigned int uaddrlen
= sizeof(uaddr
);
2317 if (getsockname(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
2319 SetLastError(wsaErrno());
2321 else if (!is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
2323 SetLastError(WSAEINVAL
);
2325 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
2327 /* The buffer was too small */
2328 SetLastError(WSAEFAULT
);
2334 release_sock_fd( s
, fd
);
2339 /***********************************************************************
2340 * getsockopt (WS2_32.7)
2342 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
2343 INT optname
, char *optval
, INT
*optlen
)
2348 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
2349 s
, level
, optname
, optval
, *optlen
);
2357 /* Handle common cases. The special cases are below, sorted
2359 case WS_SO_ACCEPTCONN
:
2360 case WS_SO_BROADCAST
:
2363 case WS_SO_KEEPALIVE
:
2364 case WS_SO_OOBINLINE
:
2366 case WS_SO_REUSEADDR
:
2369 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2370 return SOCKET_ERROR
;
2371 convert_sockopt(&level
, &optname
);
2372 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2374 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2377 release_sock_fd( s
, fd
);
2380 case WS_SO_DONTLINGER
:
2382 struct linger lingval
;
2383 unsigned int len
= sizeof(struct linger
);
2385 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
2387 SetLastError(WSAEFAULT
);
2388 return SOCKET_ERROR
;
2390 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2391 return SOCKET_ERROR
;
2393 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
2395 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2400 *(BOOL
*)optval
= (lingval
.l_onoff
) ? FALSE
: TRUE
;
2401 *optlen
= sizeof(BOOL
);
2404 release_sock_fd( s
, fd
);
2408 /* As mentioned in setsockopt, Windows ignores this, so we
2409 * always return true here */
2410 case WS_SO_DONTROUTE
:
2411 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
2413 SetLastError(WSAEFAULT
);
2414 return SOCKET_ERROR
;
2416 *(BOOL
*)optval
= TRUE
;
2417 *optlen
= sizeof(BOOL
);
2422 struct linger lingval
;
2423 unsigned int len
= sizeof(struct linger
);
2425 /* struct linger and LINGER have different sizes */
2426 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
2428 SetLastError(WSAEFAULT
);
2429 return SOCKET_ERROR
;
2431 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2432 return SOCKET_ERROR
;
2434 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
2436 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2441 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
2442 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
2443 *optlen
= sizeof(struct linger
);
2446 release_sock_fd( s
, fd
);
2450 case WS_SO_MAX_MSG_SIZE
:
2451 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
2453 SetLastError(WSAEFAULT
);
2454 return SOCKET_ERROR
;
2456 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
2457 *(int *)optval
= 65507;
2458 *optlen
= sizeof(int);
2461 /* SO_OPENTYPE does not require a valid socket handle. */
2462 case WS_SO_OPENTYPE
:
2463 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
2465 SetLastError(WSAEFAULT
);
2466 return SOCKET_ERROR
;
2468 *(int *)optval
= get_per_thread_data()->opentype
;
2469 *optlen
= sizeof(int);
2470 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
2474 case WS_SO_RCVTIMEO
:
2477 case WS_SO_SNDTIMEO
:
2479 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
2482 unsigned int len
= sizeof(struct timeval
);
2484 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
2486 SetLastError(WSAEFAULT
);
2487 return SOCKET_ERROR
;
2489 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2490 return SOCKET_ERROR
;
2492 convert_sockopt(&level
, &optname
);
2493 if (getsockopt(fd
, level
, optname
, &tv
, &len
) != 0 )
2495 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2500 *(int *)optval
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
2501 *optlen
= sizeof(int);
2504 release_sock_fd( s
, fd
);
2509 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
2510 SetLastError(WSAENOPROTOOPT
);
2511 return SOCKET_ERROR
;
2512 } /* end switch(optname) */
2513 }/* end case WS_SOL_SOCKET */
2517 struct WS_sockaddr_ipx addr
;
2518 IPX_ADDRESS_DATA
*data
;
2523 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
2525 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (unsigned int*)optlen
) == -1)
2532 socklen_t len
=sizeof(struct ipx
);
2533 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
2536 *optval
= (int)val
.ipx_pt
;
2539 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
2540 release_sock_fd( s
, fd
);
2545 * On a Win2000 system with one network card there are usually
2546 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
2547 * Using this call you can then retrieve info about this all.
2548 * In case of Linux it is a bit different. Usually you have
2549 * only "one" device active and further it is not possible to
2550 * query things like the linkspeed.
2552 FIXME("IPX_ADDRESS\n");
2553 namelen
= sizeof(struct WS_sockaddr_ipx
);
2554 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
2555 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
2557 data
= (IPX_ADDRESS_DATA
*)optval
;
2558 memcpy(data
->nodenum
,addr
.sa_nodenum
,sizeof(data
->nodenum
));
2559 memcpy(data
->netnum
,addr
.sa_netnum
,sizeof(data
->netnum
));
2560 data
->adapternum
= 0;
2561 data
->wan
= FALSE
; /* We are not on a wan for now .. */
2562 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
2563 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
2564 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
2565 * note 1MB = 1000kB in this case */
2568 case IPX_MAX_ADAPTER_NUM
:
2569 FIXME("IPX_MAX_ADAPTER_NUM\n");
2570 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
2574 FIXME("IPX optname:%x\n", optname
);
2575 return SOCKET_ERROR
;
2576 }/* end switch(optname) */
2577 } /* end case NSPROTO_IPX */
2584 case WS_IRLMP_ENUMDEVICES
:
2586 static const int MAX_IRDA_DEVICES
= 10;
2587 char buf
[sizeof(struct irda_device_list
) +
2588 (MAX_IRDA_DEVICES
- 1) * sizeof(struct irda_device_info
)];
2590 socklen_t len
= sizeof(buf
);
2592 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2593 return SOCKET_ERROR
;
2594 res
= getsockopt( fd
, SOL_IRLMP
, IRLMP_ENUMDEVICES
, buf
, &len
);
2597 SetLastError(wsaErrno());
2598 return SOCKET_ERROR
;
2602 struct irda_device_list
*src
= (struct irda_device_list
*)buf
;
2603 DEVICELIST
*dst
= (DEVICELIST
*)optval
;
2604 INT needed
= sizeof(DEVICELIST
), i
;
2607 needed
+= (src
->len
- 1) * sizeof(IRDA_DEVICE_INFO
);
2608 if (*optlen
< needed
)
2610 SetLastError(WSAEFAULT
);
2611 return SOCKET_ERROR
;
2614 TRACE("IRLMP_ENUMDEVICES: %d devices found:\n", src
->len
);
2615 dst
->numDevice
= src
->len
;
2616 for (i
= 0; i
< src
->len
; i
++)
2618 TRACE("saddr = %08x, daddr = %08x, info = %s, hints = %02x%02x\n",
2619 src
->dev
[i
].saddr
, src
->dev
[i
].daddr
,
2620 src
->dev
[i
].info
, src
->dev
[i
].hints
[0],
2621 src
->dev
[i
].hints
[1]);
2622 memcpy( dst
->Device
[i
].irdaDeviceID
,
2624 sizeof(dst
->Device
[i
].irdaDeviceID
) ) ;
2625 memcpy( dst
->Device
[i
].irdaDeviceName
,
2627 sizeof(dst
->Device
[i
].irdaDeviceName
) ) ;
2628 memcpy( &dst
->Device
[i
].irdaDeviceHints1
,
2629 &src
->dev
[i
].hints
[0],
2630 sizeof(dst
->Device
[i
].irdaDeviceHints1
) ) ;
2631 memcpy( &dst
->Device
[i
].irdaDeviceHints2
,
2632 &src
->dev
[i
].hints
[1],
2633 sizeof(dst
->Device
[i
].irdaDeviceHints2
) ) ;
2634 dst
->Device
[i
].irdaCharSet
= src
->dev
[i
].charset
;
2640 FIXME("IrDA optname:0x%x\n", optname
);
2641 return SOCKET_ERROR
;
2643 break; /* case WS_SOL_IRLMP */
2646 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
2647 case WS_IPPROTO_TCP
:
2650 case WS_TCP_NODELAY
:
2651 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2652 return SOCKET_ERROR
;
2653 convert_sockopt(&level
, &optname
);
2654 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2656 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2659 release_sock_fd( s
, fd
);
2662 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
2663 return SOCKET_ERROR
;
2668 case WS_IP_ADD_MEMBERSHIP
:
2669 case WS_IP_DROP_MEMBERSHIP
:
2673 case WS_IP_MULTICAST_IF
:
2674 case WS_IP_MULTICAST_LOOP
:
2675 case WS_IP_MULTICAST_TTL
:
2679 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2680 return SOCKET_ERROR
;
2681 convert_sockopt(&level
, &optname
);
2682 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2684 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2687 release_sock_fd( s
, fd
);
2689 case WS_IP_DONTFRAGMENT
:
2690 FIXME("WS_IP_DONTFRAGMENT is always false!\n");
2691 *(BOOL
*)optval
= FALSE
;
2694 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
2695 return SOCKET_ERROR
;
2697 case WS_IPPROTO_IPV6
:
2700 #ifdef IPV6_ADD_MEMBERSHIP
2701 case WS_IPV6_ADD_MEMBERSHIP
:
2703 #ifdef IPV6_DROP_MEMBERSHIP
2704 case WS_IPV6_DROP_MEMBERSHIP
:
2706 case WS_IPV6_MULTICAST_IF
:
2707 case WS_IPV6_MULTICAST_HOPS
:
2708 case WS_IPV6_MULTICAST_LOOP
:
2709 case WS_IPV6_UNICAST_HOPS
:
2710 case WS_IPV6_V6ONLY
:
2711 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2712 return SOCKET_ERROR
;
2713 convert_sockopt(&level
, &optname
);
2714 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2716 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2719 release_sock_fd( s
, fd
);
2721 case WS_IPV6_DONTFRAG
:
2722 FIXME("WS_IPV6_DONTFRAG is always false!\n");
2723 *(BOOL
*)optval
= FALSE
;
2726 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
2727 return SOCKET_ERROR
;
2730 WARN("Unknown level: 0x%08x\n", level
);
2731 SetLastError(WSAEINVAL
);
2732 return SOCKET_ERROR
;
2733 } /* end switch(level) */
2736 /***********************************************************************
2739 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
2741 return htonl(hostlong
);
2745 /***********************************************************************
2748 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
2750 return htons(hostshort
);
2753 /***********************************************************************
2754 * WSAHtonl (WS2_32.46)
2755 * From MSDN description of error codes, this function should also
2756 * check if WinSock has been initialized and the socket is a valid
2757 * socket. But why? This function only translates a host byte order
2758 * u_long into a network byte order u_long...
2760 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
2764 *lpnetlong
= htonl(hostlong
);
2767 WSASetLastError(WSAEFAULT
);
2768 return SOCKET_ERROR
;
2771 /***********************************************************************
2772 * WSAHtons (WS2_32.47)
2773 * From MSDN description of error codes, this function should also
2774 * check if WinSock has been initialized and the socket is a valid
2775 * socket. But why? This function only translates a host byte order
2776 * u_short into a network byte order u_short...
2778 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
2783 *lpnetshort
= htons(hostshort
);
2786 WSASetLastError(WSAEFAULT
);
2787 return SOCKET_ERROR
;
2791 /***********************************************************************
2792 * inet_addr (WS2_32.11)
2794 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
2796 if (!cp
) return INADDR_NONE
;
2797 return inet_addr(cp
);
2801 /***********************************************************************
2804 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
2806 return ntohl(netlong
);
2810 /***********************************************************************
2813 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
2815 return ntohs(netshort
);
2819 /***********************************************************************
2820 * inet_ntoa (WS2_32.12)
2822 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
2824 /* use "buffer for dummies" here because some applications have a
2825 * propensity to decode addresses in ws_hostent structure without
2826 * saving them first...
2828 static char dbuffer
[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
2830 char* s
= inet_ntoa(*((struct in_addr
*)&in
));
2836 SetLastError(wsaErrno());
2840 static const char *debugstr_wsaioctl(DWORD ioctl
)
2842 switch(ioctl
& 0x18000000)
2846 BYTE size
= (ioctl
>> 16) & WS_IOCPARM_MASK
;
2847 char x
= (ioctl
& 0xff00) >> 8;
2848 BYTE y
= ioctl
& 0xff;
2849 const char *buf_type
;
2852 switch (ioctl
& (WS_IOC_VOID
|WS_IOC_INOUT
))
2856 sprintf(args
, "%d, %d", x
, y
);
2860 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
2864 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
2868 sprintf(args
, "'%c', %d, %d", x
, y
, size
);
2871 return wine_dbg_sprintf("%s(%s)", buf_type
, args
);
2875 USHORT code
= ioctl
& 0xffff;
2876 const char *family
, *buf_type
;
2878 /* This switch looks redundant, but isn't: the case WS_IOC_UNIX
2879 * is handled differently than all others.
2881 switch(ioctl
& 0x18000000)
2886 case WS_IOC_PROTOCOL
:
2887 family
= "IOC_PROTOCOL";
2890 family
= "IOC_VENDOR";
2893 switch (ioctl
& (WS_IOC_VOID
|WS_IOC_INOUT
))
2896 buf_type
= "_WSAIO";
2899 buf_type
= "_WSAIORW";
2902 buf_type
= "_WSAIOW";
2905 buf_type
= "_WSAIOR";
2911 return wine_dbg_sprintf("%s(%s, %d)", buf_type
, family
, code
);
2916 /**********************************************************************
2917 * WSAIoctl (WS2_32.50)
2920 INT WINAPI
WSAIoctl(SOCKET s
,
2921 DWORD dwIoControlCode
,
2924 LPVOID lpbOutBuffer
,
2926 LPDWORD lpcbBytesReturned
,
2927 LPWSAOVERLAPPED lpOverlapped
,
2928 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2930 TRACE("%ld, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
2931 s
, dwIoControlCode
, lpvInBuffer
, cbInBuffer
, lpbOutBuffer
,
2932 cbOutBuffer
, lpcbBytesReturned
, lpOverlapped
, lpCompletionRoutine
);
2934 switch( dwIoControlCode
)
2937 if (cbInBuffer
!= sizeof(WS_u_long
)) {
2938 WSASetLastError(WSAEFAULT
);
2939 return SOCKET_ERROR
;
2941 return WS_ioctlsocket( s
, WS_FIONBIO
, lpvInBuffer
);
2944 if (cbOutBuffer
!= sizeof(WS_u_long
)) {
2945 WSASetLastError(WSAEFAULT
);
2946 return SOCKET_ERROR
;
2948 return WS_ioctlsocket( s
, WS_FIONREAD
, lpbOutBuffer
);
2950 case WS_SIO_GET_INTERFACE_LIST
:
2952 INTERFACE_INFO
* intArray
= (INTERFACE_INFO
*)lpbOutBuffer
;
2953 DWORD size
, numInt
, apiReturn
;
2956 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
2960 WSASetLastError(WSAEFAULT
);
2961 return SOCKET_ERROR
;
2963 if (!lpcbBytesReturned
)
2965 WSASetLastError(WSAEFAULT
);
2966 return SOCKET_ERROR
;
2969 fd
= get_sock_fd( s
, 0, NULL
);
2970 if (fd
== -1) return SOCKET_ERROR
;
2972 apiReturn
= GetAdaptersInfo(NULL
, &size
);
2973 if (apiReturn
== ERROR_NO_DATA
)
2977 else if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
2979 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
2983 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
2985 PIP_ADAPTER_INFO ptr
;
2987 if (size
*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > cbOutBuffer
)
2989 WARN("Buffer too small = %u, cbOutBuffer = %u\n", size
, cbOutBuffer
);
2990 HeapFree(GetProcessHeap(),0,table
);
2991 release_sock_fd( s
, fd
);
2992 WSASetLastError(WSAEFAULT
);
2993 return SOCKET_ERROR
;
2995 for (ptr
= table
, numInt
= 0; ptr
;
2996 ptr
= ptr
->Next
, intArray
++, numInt
++)
2998 unsigned int addr
, mask
, bcast
;
2999 struct ifreq ifInfo
;
3001 /* Socket Status Flags */
3002 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
3003 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
3005 ERR("Error obtaining status flags for socket!\n");
3006 HeapFree(GetProcessHeap(),0,table
);
3007 release_sock_fd( s
, fd
);
3008 WSASetLastError(WSAEINVAL
);
3009 return SOCKET_ERROR
;
3013 /* set flags; the values of IFF_* are not the same
3014 under Linux and Windows, therefore must generate
3016 intArray
->iiFlags
= 0;
3017 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
3018 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
3019 #ifdef IFF_POINTOPOINT
3020 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
3021 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
3023 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
3024 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
3025 if (ifInfo
.ifr_flags
& IFF_UP
)
3026 intArray
->iiFlags
|= WS_IFF_UP
;
3027 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
3028 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
3031 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
3032 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
3033 bcast
= addr
| ~mask
;
3034 intArray
->iiAddress
.AddressIn
.sin_family
= AF_INET
;
3035 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
3036 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
=
3038 intArray
->iiNetmask
.AddressIn
.sin_family
= AF_INET
;
3039 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
3040 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
=
3042 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
=
3044 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
3045 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.
3051 ERR("Unable to get interface table!\n");
3052 release_sock_fd( s
, fd
);
3053 HeapFree(GetProcessHeap(),0,table
);
3054 WSASetLastError(WSAEINVAL
);
3055 return SOCKET_ERROR
;
3057 HeapFree(GetProcessHeap(),0,table
);
3061 release_sock_fd( s
, fd
);
3062 WSASetLastError(WSAEINVAL
);
3063 return SOCKET_ERROR
;
3068 ERR("Unable to get interface table!\n");
3069 release_sock_fd( s
, fd
);
3070 WSASetLastError(WSAEINVAL
);
3071 return SOCKET_ERROR
;
3073 /* Calculate the size of the array being returned */
3074 *lpcbBytesReturned
= sizeof(INTERFACE_INFO
) * numInt
;
3075 release_sock_fd( s
, fd
);
3079 case WS_SIO_ADDRESS_LIST_CHANGE
:
3080 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
3081 /* FIXME: error and return code depend on whether socket was created
3082 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
3085 case WS_SIO_ADDRESS_LIST_QUERY
:
3089 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
3091 if (!lpcbBytesReturned
)
3093 WSASetLastError(WSAEFAULT
);
3094 return SOCKET_ERROR
;
3097 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
3099 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
3102 if (!table
|| GetAdaptersInfo(table
, &size
))
3104 HeapFree(GetProcessHeap(), 0, table
);
3105 WSASetLastError(WSAEINVAL
);
3106 return SOCKET_ERROR
;
3109 for (p
= table
, num
= 0; p
; p
= p
->Next
)
3110 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
3112 need
= sizeof(SOCKET_ADDRESS_LIST
) + sizeof(SOCKET_ADDRESS
) * (num
- 1);
3113 need
+= sizeof(SOCKADDR
) * num
;
3114 *lpcbBytesReturned
= need
;
3116 if (need
> cbOutBuffer
)
3118 HeapFree(GetProcessHeap(), 0, table
);
3119 WSASetLastError(WSAEFAULT
);
3120 return SOCKET_ERROR
;
3127 SOCKET_ADDRESS_LIST
*sa_list
= (SOCKET_ADDRESS_LIST
*)lpbOutBuffer
;
3128 SOCKADDR_IN
*sockaddr
;
3130 sa
= sa_list
->Address
;
3131 sockaddr
= (SOCKADDR_IN
*)((char *)sa
+ num
* sizeof(SOCKET_ADDRESS
));
3132 sa_list
->iAddressCount
= num
;
3134 for (p
= table
, i
= 0; p
; p
= p
->Next
)
3136 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
3138 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
3139 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
3141 sockaddr
[i
].sin_family
= AF_INET
;
3142 sockaddr
[i
].sin_port
= 0;
3143 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
3148 HeapFree(GetProcessHeap(), 0, table
);
3153 WARN("unable to get IP address list\n");
3154 WSASetLastError(WSAEINVAL
);
3155 return SOCKET_ERROR
;
3159 FIXME("SIO_FLUSH: stub.\n");
3162 case WS_SIO_GET_EXTENSION_FUNCTION_POINTER
:
3164 static const GUID connectex_guid
= WSAID_CONNECTEX
;
3165 static const GUID disconnectex_guid
= WSAID_DISCONNECTEX
;
3166 static const GUID acceptex_guid
= WSAID_ACCEPTEX
;
3167 static const GUID getaccepexsockaddrs_guid
= WSAID_GETACCEPTEXSOCKADDRS
;
3168 static const GUID transmitfile_guid
= WSAID_TRANSMITFILE
;
3169 static const GUID transmitpackets_guid
= WSAID_TRANSMITPACKETS
;
3170 static const GUID wsarecvmsg_guid
= WSAID_WSARECVMSG
;
3171 static const GUID wsasendmsg_guid
= WSAID_WSASENDMSG
;
3173 if ( IsEqualGUID(&connectex_guid
, lpvInBuffer
) )
3175 *(LPFN_CONNECTEX
*)lpbOutBuffer
= WS2_ConnectEx
;
3178 else if ( IsEqualGUID(&disconnectex_guid
, lpvInBuffer
) )
3180 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented DisconnectEx\n");
3182 else if ( IsEqualGUID(&acceptex_guid
, lpvInBuffer
) )
3184 *(LPFN_ACCEPTEX
*)lpbOutBuffer
= WS2_AcceptEx
;
3187 else if ( IsEqualGUID(&getaccepexsockaddrs_guid
, lpvInBuffer
) )
3189 *(LPFN_GETACCEPTEXSOCKADDRS
*)lpbOutBuffer
= WS2_GetAcceptExSockaddrs
;
3192 else if ( IsEqualGUID(&transmitfile_guid
, lpvInBuffer
) )
3194 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitFile\n");
3196 else if ( IsEqualGUID(&transmitpackets_guid
, lpvInBuffer
) )
3198 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitPackets\n");
3200 else if ( IsEqualGUID(&wsarecvmsg_guid
, lpvInBuffer
) )
3202 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented WSARecvMsg\n");
3204 else if ( IsEqualGUID(&wsasendmsg_guid
, lpvInBuffer
) )
3206 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented WSASendMsg\n");
3209 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER %s: stub\n", debugstr_guid(lpvInBuffer
));
3211 WSASetLastError(WSAEOPNOTSUPP
);
3212 return SOCKET_ERROR
;
3215 case WS_SIO_KEEPALIVE_VALS
:
3218 struct tcp_keepalive
*k
= lpvInBuffer
;
3219 int keepalive
= k
->onoff
? 1 : 0;
3220 int keepidle
= k
->keepalivetime
/ 1000;
3221 int keepintvl
= k
->keepaliveinterval
/ 1000;
3225 WSASetLastError(WSAEINVAL
);
3226 return SOCKET_ERROR
;
3229 TRACE("onoff: %d, keepalivetime: %d, keepaliveinterval: %d\n", keepalive
, keepidle
, keepintvl
);
3231 fd
= get_sock_fd(s
, 0, NULL
);
3232 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&keepalive
, sizeof(int)) == -1)
3234 release_sock_fd(s
, fd
);
3235 WSASetLastError(WSAEINVAL
);
3236 return SOCKET_ERROR
;
3238 #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL)
3239 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, (void *)&keepidle
, sizeof(int)) == -1)
3241 release_sock_fd(s
, fd
);
3242 WSASetLastError(WSAEINVAL
);
3243 return SOCKET_ERROR
;
3245 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, (void *)&keepintvl
, sizeof(int)) == -1)
3247 release_sock_fd(s
, fd
);
3248 WSASetLastError(WSAEINVAL
);
3249 return SOCKET_ERROR
;
3252 FIXME("ignoring keepalive interval and timeout\n");
3255 release_sock_fd(s
, fd
);
3258 case WS_SIO_ROUTING_INTERFACE_QUERY
:
3260 struct WS_sockaddr
*daddr
= (struct WS_sockaddr
*)lpvInBuffer
;
3261 struct WS_sockaddr_in
*daddr_in
= (struct WS_sockaddr_in
*)daddr
;
3262 struct WS_sockaddr_in
*saddr_in
= (struct WS_sockaddr_in
*)lpbOutBuffer
;
3263 MIB_IPFORWARDROW row
;
3264 PMIB_IPADDRTABLE ipAddrTable
= NULL
;
3265 DWORD size
, i
, found_index
;
3267 TRACE("-> WS_SIO_ROUTING_INTERFACE_QUERY request\n");
3271 WSASetLastError(WSAEFAULT
);
3272 return SOCKET_ERROR
;
3274 if (cbInBuffer
< sizeof(struct WS_sockaddr
))
3276 WSASetLastError(WSAEFAULT
);
3277 return SOCKET_ERROR
;
3281 WSASetLastError(WSAEFAULT
);
3282 return SOCKET_ERROR
;
3284 if (!lpcbBytesReturned
)
3286 WSASetLastError(WSAEFAULT
);
3287 return SOCKET_ERROR
;
3289 if (daddr
->sa_family
!= AF_INET
)
3291 FIXME("unsupported address family %d\n", daddr
->sa_family
);
3292 WSASetLastError(WSAEAFNOSUPPORT
);
3293 return SOCKET_ERROR
;
3295 if (cbOutBuffer
< sizeof(struct WS_sockaddr_in
))
3297 WSASetLastError(WSAEFAULT
);
3298 return SOCKET_ERROR
;
3300 if (GetBestRoute(daddr_in
->sin_addr
.S_un
.S_addr
, 0, &row
) != NOERROR
)
3302 WSASetLastError(WSAEFAULT
);
3303 return SOCKET_ERROR
;
3305 if (GetIpAddrTable(NULL
, &size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
3307 WSASetLastError(WSAEFAULT
);
3308 return SOCKET_ERROR
;
3310 ipAddrTable
= HeapAlloc(GetProcessHeap(), 0, size
);
3311 if (GetIpAddrTable(ipAddrTable
, &size
, FALSE
))
3313 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3314 WSASetLastError(WSAEFAULT
);
3315 return SOCKET_ERROR
;
3317 for (i
= 0, found_index
= ipAddrTable
->dwNumEntries
;
3318 i
< ipAddrTable
->dwNumEntries
; i
++)
3320 if (ipAddrTable
->table
[i
].dwIndex
== row
.dwForwardIfIndex
)
3323 if (found_index
== ipAddrTable
->dwNumEntries
)
3325 ERR("no matching IP address for interface %d\n",
3326 row
.dwForwardIfIndex
);
3327 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3328 WSASetLastError(WSAEFAULT
);
3329 return SOCKET_ERROR
;
3331 saddr_in
->sin_family
= AF_INET
;
3332 saddr_in
->sin_addr
.S_un
.S_addr
= ipAddrTable
->table
[found_index
].dwAddr
;
3333 saddr_in
->sin_port
= 0;
3334 *lpcbBytesReturned
= sizeof(struct WS_sockaddr_in
);
3335 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
3338 case WS_SIO_UDP_CONNRESET
:
3339 FIXME("WS_SIO_UDP_CONNRESET stub\n");
3342 FIXME("unsupported WS_IOCTL cmd (%s)\n",
3343 debugstr_wsaioctl(dwIoControlCode
));
3344 WSASetLastError(WSAEOPNOTSUPP
);
3345 return SOCKET_ERROR
;
3352 /***********************************************************************
3353 * ioctlsocket (WS2_32.10)
3355 int WINAPI
WS_ioctlsocket(SOCKET s
, LONG cmd
, WS_u_long
*argp
)
3360 TRACE("socket %04lx, cmd %08x, ptr %p\n", s
, cmd
, argp
);
3361 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
3362 if(IS_INTRESOURCE(argp
))
3364 SetLastError(WSAEFAULT
);
3365 return SOCKET_ERROR
;
3375 if( _get_sock_mask(s
) )
3377 /* AsyncSelect()'ed sockets are always nonblocking */
3378 if (*argp
) return 0;
3379 SetLastError(WSAEINVAL
);
3380 return SOCKET_ERROR
;
3383 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
3385 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
3393 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
3394 SetLastError(WSAEINVAL
);
3395 return SOCKET_ERROR
;
3397 case SIOCGIFBRDADDR
:
3398 case SIOCGIFNETMASK
:
3400 /* These don't need any special handling. They are used by
3401 WsControl, and are here to suppress an unnecessary warning. */
3405 /* Netscape tries hard to use bogus ioctl 0x667e */
3406 /* FIXME: 0x667e above is ('f' << 8) | 126, and is a low word of
3407 * FIONBIO (_IOW('f', 126, u_long)), how that should be handled?
3409 WARN("\tunknown WS_IOCTL cmd (%08x)\n", cmd
);
3413 fd
= get_sock_fd( s
, 0, NULL
);
3416 if( ioctl(fd
, newcmd
, (char*)argp
) == 0 )
3418 release_sock_fd( s
, fd
);
3421 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3422 release_sock_fd( s
, fd
);
3424 return SOCKET_ERROR
;
3427 /***********************************************************************
3428 * listen (WS2_32.13)
3430 int WINAPI
WS_listen(SOCKET s
, int backlog
)
3432 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
3434 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
3437 if (listen(fd
, backlog
) == 0)
3439 release_sock_fd( s
, fd
);
3440 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
3442 FD_CONNECT
|FD_WINE_CONNECTED
);
3445 SetLastError(wsaErrno());
3446 release_sock_fd( s
, fd
);
3448 return SOCKET_ERROR
;
3451 /***********************************************************************
3454 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
3456 DWORD n
, dwFlags
= flags
;
3462 if ( WS2_recvfrom(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
3463 return SOCKET_ERROR
;
3468 /***********************************************************************
3469 * recvfrom (WS2_32.17)
3471 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
3472 struct WS_sockaddr
*from
, int *fromlen
)
3474 DWORD n
, dwFlags
= flags
;
3480 if ( WS2_recvfrom(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
) == SOCKET_ERROR
)
3481 return SOCKET_ERROR
;
3486 /* allocate a poll array for the corresponding fd sets */
3487 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
3488 const WS_fd_set
*exceptfds
, int *count_ptr
)
3490 unsigned int i
, j
= 0, count
= 0;
3493 if (readfds
) count
+= readfds
->fd_count
;
3494 if (writefds
) count
+= writefds
->fd_count
;
3495 if (exceptfds
) count
+= exceptfds
->fd_count
;
3499 SetLastError(WSAEINVAL
);
3502 if (!(fds
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(fds
[0]))))
3504 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
3508 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
3510 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
3511 if (fds
[j
].fd
== -1) goto failed
;
3512 fds
[j
].events
= POLLIN
;
3516 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
3518 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
3519 if (fds
[j
].fd
== -1) goto failed
;
3520 fds
[j
].events
= POLLOUT
;
3524 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3526 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
3527 if (fds
[j
].fd
== -1) goto failed
;
3528 fds
[j
].events
= POLLHUP
;
3537 for (i
= 0; i
< readfds
->fd_count
&& j
< count
; i
++, j
++)
3538 release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
3540 for (i
= 0; i
< writefds
->fd_count
&& j
< count
; i
++, j
++)
3541 release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
3543 for (i
= 0; i
< exceptfds
->fd_count
&& j
< count
; i
++, j
++)
3544 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
3545 HeapFree( GetProcessHeap(), 0, fds
);
3549 /* release the file descriptor obtained in fd_sets_to_poll */
3550 /* must be called with the original fd_set arrays, before calling get_poll_results */
3551 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
3552 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
3554 unsigned int i
, j
= 0;
3558 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
3559 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
3563 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
3564 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
3568 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3569 if (fds
[j
].fd
!= -1)
3571 /* make sure we have a real error before releasing the fd */
3572 if (!sock_error_p( fds
[j
].fd
)) fds
[j
].revents
= 0;
3573 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
3578 /* map the poll results back into the Windows fd sets */
3579 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
3580 const struct pollfd
*fds
)
3582 unsigned int i
, j
= 0, k
, total
= 0;
3586 for (i
= k
= 0; i
< readfds
->fd_count
; i
++, j
++)
3587 if (fds
[j
].revents
) readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
3588 readfds
->fd_count
= k
;
3593 for (i
= k
= 0; i
< writefds
->fd_count
; i
++, j
++)
3594 if (fds
[j
].revents
) writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
3595 writefds
->fd_count
= k
;
3600 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3601 if (fds
[j
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
3602 exceptfds
->fd_count
= k
;
3609 /***********************************************************************
3610 * select (WS2_32.18)
3612 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
3613 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
3614 const struct WS_timeval
* ws_timeout
)
3616 struct pollfd
*pollfds
;
3617 struct timeval tv1
, tv2
;
3619 int count
, ret
, timeout
= -1;
3621 TRACE("read %p, write %p, excp %p timeout %p\n",
3622 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
3624 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)))
3625 return SOCKET_ERROR
;
3629 torig
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
3631 gettimeofday( &tv1
, 0 );
3634 while ((ret
= poll( pollfds
, count
, timeout
)) < 0)
3638 if (!ws_timeout
) continue;
3639 gettimeofday( &tv2
, 0 );
3641 tv2
.tv_sec
-= tv1
.tv_sec
;
3642 tv2
.tv_usec
-= tv1
.tv_usec
;
3643 if (tv2
.tv_usec
< 0)
3645 tv2
.tv_usec
+= 1000000;
3649 timeout
= torig
- (tv2
.tv_sec
* 1000) - (tv2
.tv_usec
+ 999) / 1000;
3650 if (timeout
<= 0) break;
3653 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
3655 if (ret
== -1) SetLastError(wsaErrno());
3656 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
3657 HeapFree( GetProcessHeap(), 0, pollfds
);
3661 /* helper to send completion messages for client-only i/o operation case */
3662 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
,
3665 SERVER_START_REQ( add_fd_completion
)
3667 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(sock
) );
3668 req
->cvalue
= CompletionValue
;
3669 req
->status
= CompletionStatus
;
3670 req
->information
= Information
;
3671 wine_server_call( req
);
3677 /***********************************************************************
3680 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
3686 wsabuf
.buf
= (char*) buf
;
3688 if ( WS2_sendto( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
3689 return SOCKET_ERROR
;
3694 /***********************************************************************
3695 * WSASend (WS2_32.72)
3697 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
3698 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
3699 LPWSAOVERLAPPED lpOverlapped
,
3700 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3702 return WS2_sendto( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
3703 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
3706 /***********************************************************************
3707 * WSASendDisconnect (WS2_32.73)
3709 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
3711 return WS_shutdown( s
, SD_SEND
);
3715 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
3716 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
3717 const struct WS_sockaddr
*to
, int tolen
,
3718 LPWSAOVERLAPPED lpOverlapped
,
3719 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3721 unsigned int i
, options
;
3723 struct ws2_async
*wsa
;
3724 int totalLength
= 0;
3725 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
3727 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
3728 s
, lpBuffers
, dwBufferCount
, dwFlags
,
3729 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
3731 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
3732 TRACE( "fd=%d, options=%x\n", fd
, options
);
3734 if ( fd
== -1 ) return SOCKET_ERROR
;
3736 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
3742 wsa
->hSocket
= SOCKET2HANDLE(s
);
3743 wsa
->addr
= (struct WS_sockaddr
*)to
;
3744 wsa
->addrlen
.val
= tolen
;
3745 wsa
->flags
= dwFlags
;
3746 wsa
->n_iovecs
= dwBufferCount
;
3747 wsa
->first_iovec
= 0;
3748 for ( i
= 0; i
< dwBufferCount
; i
++ )
3750 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
3751 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
3752 totalLength
+= lpBuffers
[i
].len
;
3755 if (!lpNumberOfBytesSent
)
3763 n
= WS2_send( fd
, wsa
);
3764 if (n
!= -1 || errno
!= EINTR
) break;
3766 if (n
== -1 && errno
!= EAGAIN
)
3768 int loc_errno
= errno
;
3770 if (cvalue
) WS_AddCompletion( s
, cvalue
, sock_get_ntstatus(loc_errno
), 0 );
3774 if ((lpOverlapped
|| lpCompletionRoutine
) &&
3775 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
3777 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
3779 wsa
->user_overlapped
= lpOverlapped
;
3780 wsa
->completion_func
= lpCompletionRoutine
;
3781 release_sock_fd( s
, fd
);
3785 iosb
->u
.Status
= STATUS_PENDING
;
3786 iosb
->Information
= 0;
3788 SERVER_START_REQ( register_async
)
3790 req
->type
= ASYNC_TYPE_WRITE
;
3791 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
3792 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
3793 req
->async
.iosb
= wine_server_client_ptr( iosb
);
3794 req
->async
.arg
= wine_server_client_ptr( wsa
);
3795 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
3796 req
->async
.cvalue
= cvalue
;
3797 err
= wine_server_call( req
);
3801 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
3802 WSASetLastError( NtStatusToWSAError( err
));
3803 return SOCKET_ERROR
;
3806 iosb
->u
.Status
= STATUS_SUCCESS
;
3807 iosb
->Information
= n
;
3808 *lpNumberOfBytesSent
= n
;
3809 if (!wsa
->completion_func
)
3811 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
3812 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
3813 HeapFree( GetProcessHeap(), 0, wsa
);
3815 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
3816 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
3821 if ( _is_blocking(s
) )
3823 /* On a blocking non-overlapped stream socket,
3824 * sending blocks until the entire buffer is sent. */
3825 DWORD timeout_start
= GetTickCount();
3827 *lpNumberOfBytesSent
= 0;
3829 while (wsa
->first_iovec
< dwBufferCount
)
3832 int timeout
= GET_SNDTIMEO(fd
);
3836 *lpNumberOfBytesSent
+= n
;
3837 while (wsa
->first_iovec
< dwBufferCount
&& wsa
->iovec
[wsa
->first_iovec
].iov_len
<= n
)
3838 n
-= wsa
->iovec
[wsa
->first_iovec
++].iov_len
;
3839 if (wsa
->first_iovec
>= dwBufferCount
) break;
3840 wsa
->iovec
[wsa
->first_iovec
].iov_base
= (char*)wsa
->iovec
[wsa
->first_iovec
].iov_base
+ n
;
3841 wsa
->iovec
[wsa
->first_iovec
].iov_len
-= n
;
3846 timeout
-= GetTickCount() - timeout_start
;
3847 if (timeout
< 0) timeout
= 0;
3851 pfd
.events
= POLLOUT
;
3853 if (!timeout
|| !poll( &pfd
, 1, timeout
))
3856 goto error
; /* msdn says a timeout in send is fatal */
3859 n
= WS2_send( fd
, wsa
);
3860 if (n
== -1 && errno
!= EAGAIN
&& errno
!= EINTR
)
3867 else /* non-blocking */
3869 if (n
< totalLength
)
3870 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
3873 err
= WSAEWOULDBLOCK
;
3876 *lpNumberOfBytesSent
= n
;
3879 TRACE(" -> %i bytes\n", *lpNumberOfBytesSent
);
3881 HeapFree( GetProcessHeap(), 0, wsa
);
3882 release_sock_fd( s
, fd
);
3887 HeapFree( GetProcessHeap(), 0, wsa
);
3888 release_sock_fd( s
, fd
);
3889 WARN(" -> ERROR %d\n", err
);
3890 WSASetLastError(err
);
3891 return SOCKET_ERROR
;
3894 /***********************************************************************
3895 * WSASendTo (WS2_32.74)
3897 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
3898 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
3899 const struct WS_sockaddr
*to
, int tolen
,
3900 LPWSAOVERLAPPED lpOverlapped
,
3901 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3903 return WS2_sendto( s
, lpBuffers
, dwBufferCount
,
3904 lpNumberOfBytesSent
, dwFlags
,
3906 lpOverlapped
, lpCompletionRoutine
);
3909 /***********************************************************************
3910 * sendto (WS2_32.20)
3912 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
3913 const struct WS_sockaddr
*to
, int tolen
)
3919 wsabuf
.buf
= (char*) buf
;
3921 if ( WS2_sendto(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
3922 return SOCKET_ERROR
;
3927 /***********************************************************************
3928 * setsockopt (WS2_32.21)
3930 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
3931 const char *optval
, int optlen
)
3935 struct linger linger
;
3936 struct timeval tval
;
3938 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
3939 s
, level
, optname
, optval
, optlen
);
3941 /* some broken apps pass the value directly instead of a pointer to it */
3942 if(optlen
&& IS_INTRESOURCE(optval
))
3944 SetLastError(WSAEFAULT
);
3945 return SOCKET_ERROR
;
3953 /* Some options need some conversion before they can be sent to
3954 * setsockopt. The conversions are done here, then they will fall though
3955 * to the general case. Special options that are not passed to
3956 * setsockopt follow below that.*/
3958 case WS_SO_DONTLINGER
:
3959 linger
.l_onoff
= *((const int*)optval
) ? 0: 1;
3960 linger
.l_linger
= 0;
3962 optname
= SO_LINGER
;
3963 optval
= (char*)&linger
;
3964 optlen
= sizeof(struct linger
);
3968 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
3969 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
3970 /* FIXME: what is documented behavior if SO_LINGER optval
3973 optname
= SO_LINGER
;
3974 optval
= (char*)&linger
;
3975 optlen
= sizeof(struct linger
);
3979 if (*(const int*)optval
< 2048)
3981 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
3986 /* The options listed here don't need any special handling. Thanks to
3987 * the conversion happening above, options from there will fall through
3989 case WS_SO_ACCEPTCONN
:
3990 case WS_SO_BROADCAST
:
3992 case WS_SO_KEEPALIVE
:
3993 case WS_SO_OOBINLINE
:
3994 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
3995 * however, using it the BSD way fixes bug 8513 and seems to be what
3996 * most programmers assume, anyway */
3997 case WS_SO_REUSEADDR
:
4000 convert_sockopt(&level
, &optname
);
4003 /* SO_DEBUG is a privileged operation, ignore it. */
4005 TRACE("Ignoring SO_DEBUG\n");
4008 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
4009 * socket. According to MSDN, this option is silently ignored.*/
4010 case WS_SO_DONTROUTE
:
4011 TRACE("Ignoring SO_DONTROUTE\n");
4014 /* Stops two sockets from being bound to the same port. Always happens
4015 * on unix systems, so just drop it. */
4016 case WS_SO_EXCLUSIVEADDRUSE
:
4017 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
4020 /* After a ConnectEx call succeeds, the socket can't be used with half of the
4021 * normal winsock functions on windows. We don't have that problem. */
4022 case WS_SO_UPDATE_CONNECT_CONTEXT
:
4023 TRACE("Ignoring SO_UPDATE_CONNECT_CONTEXT, since our sockets are normal\n");
4026 /* After a AcceptEx call succeeds, the socket can't be used with half of the
4027 * normal winsock functions on windows. We don't have that problem. */
4028 case WS_SO_UPDATE_ACCEPT_CONTEXT
:
4029 TRACE("Ignoring SO_UPDATE_ACCEPT_CONTEXT, since our sockets are normal\n");
4032 /* SO_OPENTYPE does not require a valid socket handle. */
4033 case WS_SO_OPENTYPE
:
4034 if (!optlen
|| optlen
< sizeof(int) || !optval
)
4036 SetLastError(WSAEFAULT
);
4037 return SOCKET_ERROR
;
4039 get_per_thread_data()->opentype
= *(const int *)optval
;
4040 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((const int*)optval
) );
4044 case WS_SO_RCVTIMEO
:
4047 case WS_SO_SNDTIMEO
:
4049 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
4050 if (optval
&& optlen
== sizeof(UINT32
)) {
4051 /* WinSock passes milliseconds instead of struct timeval */
4052 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
4053 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
4054 /* min of 500 milliseconds */
4055 if (tval
.tv_sec
== 0 && tval
.tv_usec
&& tval
.tv_usec
< 500000)
4056 tval
.tv_usec
= 500000;
4057 optlen
= sizeof(struct timeval
);
4058 optval
= (char*)&tval
;
4059 } else if (optlen
== sizeof(struct timeval
)) {
4060 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
4062 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
4065 convert_sockopt(&level
, &optname
);
4070 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
4071 SetLastError(WSAENOPROTOOPT
);
4072 return SOCKET_ERROR
;
4074 break; /* case WS_SOL_SOCKET */
4081 fd
= get_sock_fd( s
, 0, NULL
);
4082 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval
, fd
);
4084 /* We try to set the ipx type on ipx socket level. */
4086 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, optlen
) == -1)
4088 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
4089 release_sock_fd( s
, fd
);
4090 return SOCKET_ERROR
;
4095 /* Should we retrieve val using a getsockopt call and then
4096 * set the modified one? */
4097 val
.ipx_pt
= *optval
;
4098 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
4101 release_sock_fd( s
, fd
);
4104 case IPX_FILTERPTYPE
:
4105 /* Sets the receive filter packet type, at the moment we don't support it */
4106 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
4107 /* Returning 0 is better for now than returning a SOCKET_ERROR */
4111 FIXME("opt_name:%x\n", optname
);
4112 return SOCKET_ERROR
;
4114 break; /* case NSPROTO_IPX */
4117 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
4118 case WS_IPPROTO_TCP
:
4121 case WS_TCP_NODELAY
:
4122 convert_sockopt(&level
, &optname
);
4125 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
4126 return SOCKET_ERROR
;
4133 case WS_IP_ADD_MEMBERSHIP
:
4134 case WS_IP_DROP_MEMBERSHIP
:
4138 case WS_IP_MULTICAST_IF
:
4139 case WS_IP_MULTICAST_LOOP
:
4140 case WS_IP_MULTICAST_TTL
:
4144 convert_sockopt(&level
, &optname
);
4146 case WS_IP_DONTFRAGMENT
:
4147 FIXME("IP_DONTFRAGMENT is silently ignored!\n");
4150 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
4151 return SOCKET_ERROR
;
4155 case WS_IPPROTO_IPV6
:
4158 #ifdef IPV6_ADD_MEMBERSHIP
4159 case WS_IPV6_ADD_MEMBERSHIP
:
4161 #ifdef IPV6_DROP_MEMBERSHIP
4162 case WS_IPV6_DROP_MEMBERSHIP
:
4164 case WS_IPV6_MULTICAST_IF
:
4165 case WS_IPV6_MULTICAST_HOPS
:
4166 case WS_IPV6_MULTICAST_LOOP
:
4167 case WS_IPV6_UNICAST_HOPS
:
4168 case WS_IPV6_V6ONLY
:
4169 convert_sockopt(&level
, &optname
);
4171 case WS_IPV6_DONTFRAG
:
4172 FIXME("IPV6_DONTFRAG is silently ignored!\n");
4175 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
4176 return SOCKET_ERROR
;
4181 WARN("Unknown level: 0x%08x\n", level
);
4182 SetLastError(WSAEINVAL
);
4183 return SOCKET_ERROR
;
4184 } /* end switch(level) */
4186 /* avoid endianness issues if argument is a 16-bit int */
4187 if (optval
&& optlen
< sizeof(int))
4189 woptval
= *((const INT16
*) optval
);
4190 optval
= (char*) &woptval
;
4193 fd
= get_sock_fd( s
, 0, NULL
);
4194 if (fd
== -1) return SOCKET_ERROR
;
4196 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
4198 release_sock_fd( s
, fd
);
4201 TRACE("Setting socket error, %d\n", wsaErrno());
4202 SetLastError(wsaErrno());
4203 release_sock_fd( s
, fd
);
4205 return SOCKET_ERROR
;
4208 /***********************************************************************
4209 * shutdown (WS2_32.22)
4211 int WINAPI
WS_shutdown(SOCKET s
, int how
)
4213 int fd
, err
= WSAENOTSOCK
;
4214 unsigned int options
, clear_flags
= 0;
4216 fd
= get_sock_fd( s
, 0, &options
);
4217 TRACE("socket %04lx, how %i %x\n", s
, how
, options
);
4220 return SOCKET_ERROR
;
4224 case 0: /* drop receives */
4225 clear_flags
|= FD_READ
;
4227 case 1: /* drop sends */
4228 clear_flags
|= FD_WRITE
;
4230 case 2: /* drop all */
4231 clear_flags
|= FD_READ
|FD_WRITE
;
4233 clear_flags
|= FD_WINE_LISTENING
;
4236 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
4241 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
4244 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
4248 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
4249 if (!err
) err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
4252 if (err
) goto error
;
4254 else /* non-overlapped mode */
4256 if ( shutdown( fd
, how
) )
4263 release_sock_fd( s
, fd
);
4264 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
4265 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
4269 release_sock_fd( s
, fd
);
4270 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
4271 WSASetLastError( err
);
4272 return SOCKET_ERROR
;
4275 /***********************************************************************
4276 * socket (WS2_32.23)
4278 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
4280 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
4282 return WSASocketA( af
, type
, protocol
, NULL
, 0,
4283 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
4287 /***********************************************************************
4288 * gethostbyaddr (WS2_32.51)
4290 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
4292 struct WS_hostent
*retval
= NULL
;
4293 struct hostent
* host
;
4295 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4298 struct hostent hostentry
;
4301 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
4303 int res
= gethostbyaddr_r(addr
, len
, type
,
4304 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
4305 if( res
!= ERANGE
) break;
4307 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
4309 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
4311 EnterCriticalSection( &csWSgetXXXbyYYY
);
4312 host
= gethostbyaddr(addr
, len
, type
);
4313 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
4315 if( host
!= NULL
) retval
= WS_dup_he(host
);
4316 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4317 HeapFree(GetProcessHeap(),0,extrabuf
);
4319 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4321 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
4325 /***********************************************************************
4326 * WS_get_local_ips (INTERNAL)
4328 * Returns the list of local IP addresses by going through the network
4329 * adapters and using the local routing table to sort the addresses
4330 * from highest routing priority to lowest routing priority. This
4331 * functionality is inferred from the description for obtaining local
4332 * IP addresses given in the Knowledge Base Article Q160215.
4334 * Please note that the returned hostent is only freed when the thread
4335 * closes and is replaced if another hostent is requested.
4337 static struct WS_hostent
* WS_get_local_ips( char *hostname
)
4339 int last_metric
, numroutes
= 0, i
, j
;
4340 PIP_ADAPTER_INFO adapters
= NULL
, k
;
4341 struct WS_hostent
*hostlist
= NULL
;
4342 PMIB_IPFORWARDTABLE routes
= NULL
;
4343 struct route
*route_addrs
= NULL
;
4344 DWORD adap_size
, route_size
;
4346 /* Obtain the size of the adapter list and routing table, also allocate memory */
4347 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
4349 if (GetIpForwardTable(NULL
, &route_size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
4351 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
4352 routes
= HeapAlloc(GetProcessHeap(), 0, route_size
);
4353 route_addrs
= HeapAlloc(GetProcessHeap(), 0, 0); /* HeapReAlloc doesn't work on NULL */
4354 if (adapters
== NULL
|| routes
== NULL
|| route_addrs
== NULL
)
4356 /* Obtain the adapter list and the full routing table */
4357 if (GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
4359 if (GetIpForwardTable(routes
, &route_size
, FALSE
) != NO_ERROR
)
4361 /* Store the interface associated with each route */
4362 for (i
= 0; i
< routes
->dwNumEntries
; i
++)
4364 DWORD ifindex
, ifmetric
, exists
= FALSE
;
4366 if (routes
->table
[i
].dwForwardType
!= MIB_IPROUTE_TYPE_DIRECT
)
4368 ifindex
= routes
->table
[i
].dwForwardIfIndex
;
4369 ifmetric
= routes
->table
[i
].dwForwardMetric1
;
4370 /* Only store the lowest valued metric for an interface */
4371 for (j
= 0; j
< numroutes
; j
++)
4373 if (route_addrs
[j
].interface
== ifindex
)
4375 if (route_addrs
[j
].metric
> ifmetric
)
4376 route_addrs
[j
].metric
= ifmetric
;
4382 route_addrs
= HeapReAlloc(GetProcessHeap(), 0, route_addrs
, (numroutes
+1)*sizeof(struct route
));
4383 if (route_addrs
== NULL
)
4384 goto cleanup
; /* Memory allocation error, fail gracefully */
4385 route_addrs
[numroutes
].interface
= ifindex
;
4386 route_addrs
[numroutes
].metric
= ifmetric
;
4387 /* If no IP is found in the next step (for whatever reason)
4388 * then fall back to the magic loopback address.
4390 memcpy(&(route_addrs
[numroutes
].addr
.s_addr
), magic_loopback_addr
, 4);
4394 goto cleanup
; /* No routes, fall back to the Magic IP */
4395 /* Find the IP address associated with each found interface */
4396 for (i
= 0; i
< numroutes
; i
++)
4398 for (k
= adapters
; k
!= NULL
; k
= k
->Next
)
4400 char *ip
= k
->IpAddressList
.IpAddress
.String
;
4402 if (route_addrs
[i
].interface
== k
->Index
)
4403 route_addrs
[i
].addr
.s_addr
= (in_addr_t
) inet_addr(ip
);
4406 /* Allocate a hostent and enough memory for all the IPs,
4407 * including the NULL at the end of the list.
4409 hostlist
= WS_create_he(hostname
, 1, numroutes
+1, TRUE
);
4410 if (hostlist
== NULL
)
4411 goto cleanup
; /* Failed to allocate a hostent for the list of IPs */
4412 hostlist
->h_addr_list
[numroutes
] = NULL
; /* NULL-terminate the address list */
4413 hostlist
->h_aliases
[0] = NULL
; /* NULL-terminate the alias list */
4414 hostlist
->h_addrtype
= AF_INET
;
4415 hostlist
->h_length
= sizeof(struct in_addr
); /* = 4 */
4416 /* Reorder the entries when placing them in the host list, Windows expects
4417 * the IP list in order from highest priority to lowest (the critical thing
4418 * is that most applications expect the first IP to be the default route).
4421 for (i
= 0; i
< numroutes
; i
++)
4423 struct in_addr addr
;
4424 int metric
= 0xFFFF;
4426 memcpy(&addr
, magic_loopback_addr
, 4);
4427 for (j
= 0; j
< numroutes
; j
++)
4429 int this_metric
= route_addrs
[j
].metric
;
4431 if (this_metric
> last_metric
&& this_metric
< metric
)
4433 addr
= route_addrs
[j
].addr
;
4434 metric
= this_metric
;
4437 last_metric
= metric
;
4438 (*(struct in_addr
*) hostlist
->h_addr_list
[i
]) = addr
;
4441 /* Cleanup all allocated memory except the address list,
4442 * the address list is used by the calling app.
4445 HeapFree(GetProcessHeap(), 0, route_addrs
);
4446 HeapFree(GetProcessHeap(), 0, adapters
);
4447 HeapFree(GetProcessHeap(), 0, routes
);
4451 /***********************************************************************
4452 * gethostbyname (WS2_32.52)
4454 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
4456 struct WS_hostent
*retval
= NULL
;
4457 struct hostent
* host
;
4458 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4461 struct hostent hostentry
;
4462 int locerr
= ENOBUFS
;
4465 if( gethostname( hostname
, 100) == -1) {
4466 SetLastError( WSAENOBUFS
); /* appropriate ? */
4469 if( !name
|| !name
[0]) {
4472 /* If the hostname of the local machine is requested then return the
4473 * complete list of local IP addresses */
4474 if(strcmp(name
, hostname
) == 0)
4475 retval
= WS_get_local_ips(hostname
);
4476 /* If any other hostname was requested (or the routing table lookup failed)
4477 * then return the IP found by the host OS */
4480 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4482 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
4484 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
4485 if( res
!= ERANGE
) break;
4487 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
4489 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
4491 EnterCriticalSection( &csWSgetXXXbyYYY
);
4492 host
= gethostbyname(name
);
4493 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
4495 if (host
) retval
= WS_dup_he(host
);
4496 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4497 HeapFree(GetProcessHeap(),0,extrabuf
);
4499 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4502 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
4503 strcmp(name
, "localhost") != 0)
4505 /* hostname != "localhost" but has loopback address. replace by our
4506 * special address.*/
4507 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
4509 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
4514 /***********************************************************************
4515 * getprotobyname (WS2_32.53)
4517 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
4519 struct WS_protoent
* retval
= NULL
;
4520 #ifdef HAVE_GETPROTOBYNAME
4521 struct protoent
* proto
;
4522 EnterCriticalSection( &csWSgetXXXbyYYY
);
4523 if( (proto
= getprotobyname(name
)) != NULL
)
4525 retval
= WS_dup_pe(proto
);
4528 MESSAGE("protocol %s not found; You might want to add "
4529 "this to /etc/protocols\n", debugstr_a(name
) );
4530 SetLastError(WSANO_DATA
);
4532 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4534 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
4539 /***********************************************************************
4540 * getprotobynumber (WS2_32.54)
4542 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
4544 struct WS_protoent
* retval
= NULL
;
4545 #ifdef HAVE_GETPROTOBYNUMBER
4546 struct protoent
* proto
;
4547 EnterCriticalSection( &csWSgetXXXbyYYY
);
4548 if( (proto
= getprotobynumber(number
)) != NULL
)
4550 retval
= WS_dup_pe(proto
);
4553 MESSAGE("protocol number %d not found; You might want to add "
4554 "this to /etc/protocols\n", number
);
4555 SetLastError(WSANO_DATA
);
4557 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4559 TRACE("%i ret %p\n", number
, retval
);
4564 /***********************************************************************
4565 * getservbyname (WS2_32.55)
4567 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
4569 struct WS_servent
* retval
= NULL
;
4570 struct servent
* serv
;
4572 char *proto_str
= NULL
;
4574 if (!(name_str
= strdup_lower(name
))) return NULL
;
4576 if (proto
&& *proto
)
4578 if (!(proto_str
= strdup_lower(proto
)))
4580 HeapFree( GetProcessHeap(), 0, name_str
);
4585 EnterCriticalSection( &csWSgetXXXbyYYY
);
4586 serv
= getservbyname(name_str
, proto_str
);
4589 retval
= WS_dup_se(serv
);
4591 else SetLastError(WSANO_DATA
);
4592 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4593 HeapFree( GetProcessHeap(), 0, proto_str
);
4594 HeapFree( GetProcessHeap(), 0, name_str
);
4595 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
4599 /***********************************************************************
4600 * freeaddrinfo (WS2_32.@)
4602 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
4605 struct WS_addrinfo
*next
;
4607 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
4608 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
4609 next
= res
->ai_next
;
4610 HeapFree(GetProcessHeap(),0,res
);
4615 /* helper functions for getaddrinfo()/getnameinfo() */
4616 static int convert_aiflag_w2u(int winflags
) {
4620 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
4621 if (ws_aiflag_map
[i
][0] & winflags
) {
4622 unixflags
|= ws_aiflag_map
[i
][1];
4623 winflags
&= ~ws_aiflag_map
[i
][0];
4626 FIXME("Unhandled windows AI_xxx flags %x\n", winflags
);
4630 static int convert_niflag_w2u(int winflags
) {
4634 for (i
=0;i
<sizeof(ws_niflag_map
)/sizeof(ws_niflag_map
[0]);i
++)
4635 if (ws_niflag_map
[i
][0] & winflags
) {
4636 unixflags
|= ws_niflag_map
[i
][1];
4637 winflags
&= ~ws_niflag_map
[i
][0];
4640 FIXME("Unhandled windows NI_xxx flags %x\n", winflags
);
4644 static int convert_aiflag_u2w(int unixflags
) {
4648 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
4649 if (ws_aiflag_map
[i
][1] & unixflags
) {
4650 winflags
|= ws_aiflag_map
[i
][0];
4651 unixflags
&= ~ws_aiflag_map
[i
][1];
4653 if (unixflags
) /* will warn usually */
4654 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags
);
4658 static int convert_eai_u2w(int unixret
) {
4661 for (i
=0;ws_eai_map
[i
][0];i
++)
4662 if (ws_eai_map
[i
][1] == unixret
)
4663 return ws_eai_map
[i
][0];
4667 /***********************************************************************
4668 * getaddrinfo (WS2_32.@)
4670 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
4672 #ifdef HAVE_GETADDRINFO
4673 struct addrinfo
*unixaires
= NULL
;
4675 struct addrinfo unixhints
, *punixhints
= NULL
;
4676 CHAR
*node
= NULL
, *serv
= NULL
;
4679 if (!(node
= strdup_lower(nodename
))) return WSA_NOT_ENOUGH_MEMORY
;
4682 if (!(serv
= strdup_lower(servname
))) {
4683 HeapFree(GetProcessHeap(), 0, node
);
4684 return WSA_NOT_ENOUGH_MEMORY
;
4689 punixhints
= &unixhints
;
4691 memset(&unixhints
, 0, sizeof(unixhints
));
4692 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
4693 if (hints
->ai_family
== 0) /* wildcard, specific to getaddrinfo() */
4694 punixhints
->ai_family
= 0;
4696 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
4697 if (hints
->ai_socktype
== 0) /* wildcard, specific to getaddrinfo() */
4698 punixhints
->ai_socktype
= 0;
4700 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
4701 if (hints
->ai_protocol
== 0) /* wildcard, specific to getaddrinfo() */
4702 punixhints
->ai_protocol
= 0;
4704 punixhints
->ai_protocol
= convert_proto_w2u(hints
->ai_protocol
);
4707 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
4708 result
= getaddrinfo(nodename
, servname
, punixhints
, &unixaires
);
4710 TRACE("%s, %s %p -> %p %d\n", debugstr_a(nodename
), debugstr_a(servname
), hints
, res
, result
);
4712 HeapFree(GetProcessHeap(), 0, node
);
4713 HeapFree(GetProcessHeap(), 0, serv
);
4716 struct addrinfo
*xuai
= unixaires
;
4717 struct WS_addrinfo
**xai
= res
;
4721 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
4727 *xai
= ai
;xai
= &ai
->ai_next
;
4728 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
4729 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
4730 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
4731 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
4732 if (xuai
->ai_canonname
) {
4733 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
4734 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
4735 if (!ai
->ai_canonname
)
4737 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
4739 len
= xuai
->ai_addrlen
;
4740 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
4743 ai
->ai_addrlen
= len
;
4745 int winlen
= ai
->ai_addrlen
;
4747 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
4748 ai
->ai_addrlen
= winlen
;
4752 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
4755 ai
->ai_addrlen
= len
;
4757 xuai
= xuai
->ai_next
;
4759 freeaddrinfo(unixaires
);
4761 result
= convert_eai_u2w(result
);
4767 if (*res
) WS_freeaddrinfo(*res
);
4768 if (unixaires
) freeaddrinfo(unixaires
);
4770 return WSA_NOT_ENOUGH_MEMORY
;
4772 FIXME("getaddrinfo() failed, not found during buildtime.\n");
4777 static struct WS_addrinfoW
*addrinfo_AtoW(const struct WS_addrinfo
*ai
)
4779 struct WS_addrinfoW
*ret
;
4781 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfoW
)))) return NULL
;
4782 ret
->ai_flags
= ai
->ai_flags
;
4783 ret
->ai_family
= ai
->ai_family
;
4784 ret
->ai_socktype
= ai
->ai_socktype
;
4785 ret
->ai_protocol
= ai
->ai_protocol
;
4786 ret
->ai_addrlen
= ai
->ai_addrlen
;
4787 ret
->ai_canonname
= NULL
;
4788 ret
->ai_addr
= NULL
;
4789 ret
->ai_next
= NULL
;
4790 if (ai
->ai_canonname
)
4792 int len
= MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0);
4793 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
4795 HeapFree(GetProcessHeap(), 0, ret
);
4798 MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
);
4802 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
4804 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
4805 HeapFree(GetProcessHeap(), 0, ret
);
4808 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
4813 static struct WS_addrinfoW
*addrinfo_list_AtoW(const struct WS_addrinfo
*info
)
4815 struct WS_addrinfoW
*ret
, *infoW
;
4817 if (!(ret
= infoW
= addrinfo_AtoW(info
))) return NULL
;
4818 while (info
->ai_next
)
4820 if (!(infoW
->ai_next
= addrinfo_AtoW(info
->ai_next
)))
4825 infoW
= infoW
->ai_next
;
4826 info
= info
->ai_next
;
4831 static struct WS_addrinfo
*addrinfo_WtoA(const struct WS_addrinfoW
*ai
)
4833 struct WS_addrinfo
*ret
;
4835 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfo
)))) return NULL
;
4836 ret
->ai_flags
= ai
->ai_flags
;
4837 ret
->ai_family
= ai
->ai_family
;
4838 ret
->ai_socktype
= ai
->ai_socktype
;
4839 ret
->ai_protocol
= ai
->ai_protocol
;
4840 ret
->ai_addrlen
= ai
->ai_addrlen
;
4841 ret
->ai_canonname
= NULL
;
4842 ret
->ai_addr
= NULL
;
4843 ret
->ai_next
= NULL
;
4844 if (ai
->ai_canonname
)
4846 int len
= WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0, NULL
, NULL
);
4847 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
4849 HeapFree(GetProcessHeap(), 0, ret
);
4852 WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
, NULL
, NULL
);
4856 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
4858 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
4859 HeapFree(GetProcessHeap(), 0, ret
);
4862 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
4867 /***********************************************************************
4868 * GetAddrInfoW (WS2_32.@)
4870 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
4873 char *nodenameA
, *servnameA
= NULL
;
4874 struct WS_addrinfo
*resA
, *hintsA
= NULL
;
4876 if (!nodename
) return WSAHOST_NOT_FOUND
;
4878 len
= WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, NULL
, 0, NULL
, NULL
);
4879 if (!(nodenameA
= HeapAlloc(GetProcessHeap(), 0, len
))) return EAI_MEMORY
;
4880 WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, nodenameA
, len
, NULL
, NULL
);
4884 len
= WideCharToMultiByte(CP_ACP
, 0, servname
, -1, NULL
, 0, NULL
, NULL
);
4885 if (!(servnameA
= HeapAlloc(GetProcessHeap(), 0, len
)))
4887 HeapFree(GetProcessHeap(), 0, nodenameA
);
4890 WideCharToMultiByte(CP_ACP
, 0, servname
, -1, servnameA
, len
, NULL
, NULL
);
4893 if (hints
) hintsA
= addrinfo_WtoA(hints
);
4894 ret
= WS_getaddrinfo(nodenameA
, servnameA
, hintsA
, &resA
);
4895 WS_freeaddrinfo(hintsA
);
4899 *res
= addrinfo_list_AtoW(resA
);
4900 WS_freeaddrinfo(resA
);
4903 HeapFree(GetProcessHeap(), 0, nodenameA
);
4904 HeapFree(GetProcessHeap(), 0, servnameA
);
4908 /***********************************************************************
4909 * FreeAddrInfoW (WS2_32.@)
4911 void WINAPI
FreeAddrInfoW(PADDRINFOW ai
)
4916 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
4917 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
4919 HeapFree(GetProcessHeap(), 0, ai
);
4924 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
4925 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
4927 #ifdef HAVE_GETNAMEINFO
4929 union generic_unix_sockaddr sa_u
;
4932 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
4933 serv
, servlen
, flags
);
4935 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
4938 WSASetLastError(WSAEFAULT
);
4939 return WSA_NOT_ENOUGH_MEMORY
;
4941 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
4942 return convert_eai_u2w(ret
);
4944 FIXME("getnameinfo() failed, not found during buildtime.\n");
4949 /***********************************************************************
4950 * getservbyport (WS2_32.56)
4952 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
4954 struct WS_servent
* retval
= NULL
;
4955 #ifdef HAVE_GETSERVBYPORT
4956 struct servent
* serv
;
4957 char *proto_str
= NULL
;
4959 if (proto
&& *proto
)
4961 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
4963 EnterCriticalSection( &csWSgetXXXbyYYY
);
4964 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
4965 retval
= WS_dup_se(serv
);
4967 else SetLastError(WSANO_DATA
);
4968 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4969 HeapFree( GetProcessHeap(), 0, proto_str
);
4971 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
4976 /***********************************************************************
4977 * gethostname (WS2_32.57)
4979 int WINAPI
WS_gethostname(char *name
, int namelen
)
4981 TRACE("name %p, len %d\n", name
, namelen
);
4983 if (gethostname(name
, namelen
) == 0)
4985 TRACE("<- '%s'\n", name
);
4988 SetLastError((errno
== EINVAL
) ? WSAEFAULT
: wsaErrno());
4989 TRACE("<- ERROR !\n");
4990 return SOCKET_ERROR
;
4994 /* ------------------------------------- Windows sockets extensions -- *
4996 * ------------------------------------------------------------------- */
4998 /***********************************************************************
4999 * WSAEnumNetworkEvents (WS2_32.36)
5001 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
5005 int errors
[FD_MAX_EVENTS
];
5007 TRACE("%08lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
5009 SERVER_START_REQ( get_socket_event
)
5011 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5012 req
->service
= TRUE
;
5013 req
->c_event
= wine_server_obj_handle( hEvent
);
5014 wine_server_set_reply( req
, errors
, sizeof(errors
) );
5015 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
5020 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
5021 lpEvent
->iErrorCode
[i
] = NtStatusToWSAError(errors
[i
]);
5024 SetLastError(WSAEINVAL
);
5025 return SOCKET_ERROR
;
5028 /***********************************************************************
5029 * WSAEventSelect (WS2_32.39)
5031 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
5035 TRACE("%08lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
5037 SERVER_START_REQ( set_socket_event
)
5039 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5041 req
->event
= wine_server_obj_handle( hEvent
);
5044 ret
= wine_server_call( req
);
5048 SetLastError(WSAEINVAL
);
5049 return SOCKET_ERROR
;
5052 /**********************************************************************
5053 * WSAGetOverlappedResult (WS2_32.40)
5055 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
5056 LPDWORD lpcbTransfer
, BOOL fWait
,
5061 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
5062 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
5064 if ( lpOverlapped
== NULL
)
5066 ERR( "Invalid pointer\n" );
5067 WSASetLastError(WSA_INVALID_PARAMETER
);
5071 status
= lpOverlapped
->Internal
;
5072 if (status
== STATUS_PENDING
)
5076 SetLastError( WSA_IO_INCOMPLETE
);
5080 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
5081 INFINITE
) == WAIT_FAILED
)
5083 status
= lpOverlapped
->Internal
;
5087 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
5090 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
5092 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
5097 /***********************************************************************
5098 * WSAAsyncSelect (WS2_32.101)
5100 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
5104 TRACE("%lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
5106 SERVER_START_REQ( set_socket_event
)
5108 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5111 req
->window
= wine_server_user_handle( hWnd
);
5113 ret
= wine_server_call( req
);
5117 SetLastError(WSAEINVAL
);
5118 return SOCKET_ERROR
;
5121 /***********************************************************************
5122 * WSACreateEvent (WS2_32.31)
5125 WSAEVENT WINAPI
WSACreateEvent(void)
5127 /* Create a manual-reset event, with initial state: unsignaled */
5130 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
5133 /***********************************************************************
5134 * WSACloseEvent (WS2_32.29)
5137 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
5139 TRACE ("event=%p\n", event
);
5141 return CloseHandle(event
);
5144 /***********************************************************************
5145 * WSASocketA (WS2_32.78)
5148 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
5149 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
5150 GROUP g
, DWORD dwFlags
)
5153 WSAPROTOCOL_INFOW info
;
5155 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
5156 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
5158 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
5160 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
5161 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
5162 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
5166 WSASetLastError( WSAEINVAL
);
5167 return SOCKET_ERROR
;
5170 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
5173 /***********************************************************************
5174 * WSASocketW (WS2_32.79)
5177 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
5178 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
5179 GROUP g
, DWORD dwFlags
)
5184 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
5185 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
5188 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
5189 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
5191 /* hack for WSADuplicateSocket */
5192 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
5193 ret
= lpProtocolInfo
->dwCatalogEntryId
;
5194 TRACE("\tgot duplicate %04lx\n", ret
);
5198 /* convert the socket family and type */
5199 af
= convert_af_w2u(af
);
5200 type
= convert_socktype_w2u(type
);
5204 if (af
== FROM_PROTOCOL_INFO
)
5205 af
= lpProtocolInfo
->iAddressFamily
;
5206 if (type
== FROM_PROTOCOL_INFO
)
5207 type
= lpProtocolInfo
->iSocketType
;
5208 if (protocol
== FROM_PROTOCOL_INFO
)
5209 protocol
= lpProtocolInfo
->iProtocol
;
5212 if ( af
== AF_UNSPEC
) /* did they not specify the address family? */
5216 if (type
== SOCK_STREAM
) { af
= AF_INET
; break; }
5218 if (type
== SOCK_DGRAM
) { af
= AF_INET
; break; }
5219 default: SetLastError(WSAEPROTOTYPE
); return INVALID_SOCKET
;
5222 SERVER_START_REQ( create_socket
)
5226 req
->protocol
= protocol
;
5227 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
5228 req
->attributes
= OBJ_INHERIT
;
5229 req
->flags
= dwFlags
;
5230 set_error( wine_server_call( req
) );
5231 ret
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
5236 TRACE("\tcreated %04lx\n", ret
);
5240 if (GetLastError() == WSAEACCES
) /* raw socket denied */
5242 if (type
== SOCK_RAW
)
5243 ERR_(winediag
)("Failed to create a socket of type SOCK_RAW, this requires special permissions.\n");
5245 ERR_(winediag
)("Failed to create socket, this requires special permissions.\n");
5246 SetLastError(WSAESOCKTNOSUPPORT
);
5249 WARN("\t\tfailed!\n");
5250 return INVALID_SOCKET
;
5253 /***********************************************************************
5254 * WSAJoinLeaf (WS2_32.58)
5257 SOCKET WINAPI
WSAJoinLeaf(
5259 const struct WS_sockaddr
*addr
,
5261 LPWSABUF lpCallerData
,
5262 LPWSABUF lpCalleeData
,
5268 return INVALID_SOCKET
;
5271 /***********************************************************************
5272 * __WSAFDIsSet (WS2_32.151)
5274 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
5276 int i
= set
->fd_count
;
5278 TRACE("(%ld,%p(%i))\n", s
, set
, i
);
5281 if (set
->fd_array
[i
] == s
) return 1;
5285 /***********************************************************************
5286 * WSAIsBlocking (WS2_32.114)
5288 BOOL WINAPI
WSAIsBlocking(void)
5290 /* By default WinSock should set all its sockets to non-blocking mode
5291 * and poll in PeekMessage loop when processing "blocking" ones. This
5292 * function is supposed to tell if the program is in this loop. Our
5293 * blocking calls are truly blocking so we always return FALSE.
5295 * Note: It is allowed to call this function without prior WSAStartup().
5302 /***********************************************************************
5303 * WSACancelBlockingCall (WS2_32.113)
5305 INT WINAPI
WSACancelBlockingCall(void)
5311 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
5313 FIXME("How was this called?\n");
5318 /***********************************************************************
5319 * WSASetBlockingHook (WS2_32.109)
5321 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
5323 FARPROC prev
= blocking_hook
;
5324 blocking_hook
= lpBlockFunc
;
5325 TRACE("hook %p\n", lpBlockFunc
);
5330 /***********************************************************************
5331 * WSAUnhookBlockingHook (WS2_32.110)
5333 INT WINAPI
WSAUnhookBlockingHook(void)
5335 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
5340 /* ----------------------------------- end of API stuff */
5342 /* ----------------------------------- helper functions -
5344 * TODO: Merge WS_dup_..() stuff into one function that
5345 * would operate with a generic structure containing internal
5346 * pointers (via a template of some kind).
5349 static int list_size(char** l
, int item_size
)
5354 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
5355 j
+= (i
+ 1) * sizeof(char*); }
5359 static int list_dup(char** l_src
, char** l_to
, int item_size
)
5364 for (i
= 0; l_src
[i
]; i
++) ;
5365 p
= (char *)(l_to
+ i
+ 1);
5366 for (i
= 0; l_src
[i
]; i
++)
5368 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
5369 memcpy(p
, l_src
[i
], count
);
5374 return p
- (char *)l_to
;
5379 /* create a hostent entry
5381 * Creates the entry with enough memory for the name, aliases
5382 * addresses, and the address pointers. Also copies the name
5383 * and sets up all the pointers. If "fill_addresses" is set then
5384 * sufficient memory for the addresses is also allocated and the
5385 * address pointers are set to this memory.
5387 * NOTE: The alias and address lists must be allocated with room
5388 * for the NULL item terminating the list. This is true even if
5389 * the list has no items ("aliases" and "addresses" must be
5390 * at least "1", a truly empty list is invalid).
5392 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int addresses
, int fill_addresses
)
5394 struct WS_hostent
*p_to
;
5397 int size
= (sizeof(struct WS_hostent
) +
5399 sizeof(char *)*aliases
+
5400 sizeof(char *)*addresses
);
5402 /* Allocate enough memory for the addresses */
5404 size
+= sizeof(struct in_addr
)*addresses
;
5406 if (!(p_to
= check_buffer_he(size
))) return NULL
;
5407 memset(p_to
, 0, size
);
5409 p
= (char *)(p_to
+ 1);
5414 p_to
->h_aliases
= (char **)p
;
5415 p
+= sizeof(char *)*aliases
;
5416 p_to
->h_addr_list
= (char **)p
;
5417 p
+= sizeof(char *)*addresses
;
5422 /* NOTE: h_aliases must be filled in manually, leave these
5423 * pointers NULL (already set to NULL by memset earlier).
5426 /* Fill in the list of address pointers */
5427 for (i
= 0; i
< addresses
; i
++)
5428 p_to
->h_addr_list
[i
] = (p
+= sizeof(struct in_addr
));
5429 p
+= sizeof(struct in_addr
);
5434 /* duplicate hostent entry
5435 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
5436 * Ditto for protoent and servent.
5438 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
5440 int addresses
= list_size(p_he
->h_addr_list
, p_he
->h_length
);
5441 int aliases
= list_size(p_he
->h_aliases
, 0);
5442 struct WS_hostent
*p_to
;
5444 p_to
= WS_create_he(p_he
->h_name
, aliases
, addresses
, FALSE
);
5446 if (!p_to
) return NULL
;
5447 p_to
->h_addrtype
= p_he
->h_addrtype
;
5448 p_to
->h_length
= p_he
->h_length
;
5450 list_dup(p_he
->h_aliases
, p_to
->h_aliases
, 0);
5451 list_dup(p_he
->h_addr_list
, p_to
->h_addr_list
, p_he
->h_length
);
5455 /* ----- protoent */
5457 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
)
5460 struct WS_protoent
*p_to
;
5462 int size
= (sizeof(*p_pe
) +
5463 strlen(p_pe
->p_name
) + 1 +
5464 list_size(p_pe
->p_aliases
, 0));
5466 if (!(p_to
= check_buffer_pe(size
))) return NULL
;
5467 p_to
->p_proto
= p_pe
->p_proto
;
5469 p
= (char *)(p_to
+ 1);
5471 strcpy(p
, p_pe
->p_name
);
5474 p_to
->p_aliases
= (char **)p
;
5475 list_dup(p_pe
->p_aliases
, p_to
->p_aliases
, 0);
5481 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
5484 struct WS_servent
*p_to
;
5486 int size
= (sizeof(*p_se
) +
5487 strlen(p_se
->s_proto
) + 1 +
5488 strlen(p_se
->s_name
) + 1 +
5489 list_size(p_se
->s_aliases
, 0));
5491 if (!(p_to
= check_buffer_se(size
))) return NULL
;
5492 p_to
->s_port
= p_se
->s_port
;
5494 p
= (char *)(p_to
+ 1);
5496 strcpy(p
, p_se
->s_name
);
5500 strcpy(p
, p_se
->s_proto
);
5503 p_to
->s_aliases
= (char **)p
;
5504 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
5509 /***********************************************************************
5510 * WSARecv (WS2_32.67)
5512 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5513 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
5514 LPWSAOVERLAPPED lpOverlapped
,
5515 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5517 return WS2_recvfrom(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
5518 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
);
5521 static int WS2_recvfrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5522 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
5523 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
5524 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5527 unsigned int i
, options
;
5529 struct ws2_async
*wsa
;
5530 DWORD timeout_start
= GetTickCount();
5531 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
5533 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
5534 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
5535 (lpFromlen
? *lpFromlen
: -1),
5536 lpOverlapped
, lpCompletionRoutine
);
5538 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
5539 TRACE( "fd=%d, options=%x\n", fd
, options
);
5541 if (fd
== -1) return SOCKET_ERROR
;
5543 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
5549 wsa
->hSocket
= SOCKET2HANDLE(s
);
5550 wsa
->flags
= *lpFlags
;
5552 wsa
->addrlen
.ptr
= lpFromlen
;
5553 wsa
->n_iovecs
= dwBufferCount
;
5554 wsa
->first_iovec
= 0;
5555 for (i
= 0; i
< dwBufferCount
; i
++)
5557 /* check buffer first to trigger write watches */
5558 if (IsBadWritePtr( lpBuffers
[i
].buf
, lpBuffers
[i
].len
))
5563 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
5564 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
5569 n
= WS2_recv( fd
, wsa
);
5572 if (errno
== EINTR
) continue;
5573 if (errno
!= EAGAIN
)
5575 int loc_errno
= errno
;
5577 if (cvalue
) WS_AddCompletion( s
, cvalue
, sock_get_ntstatus(loc_errno
), 0 );
5582 *lpNumberOfBytesRecvd
= n
;
5584 if ((lpOverlapped
|| lpCompletionRoutine
) &&
5585 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
5587 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
5589 wsa
->user_overlapped
= lpOverlapped
;
5590 wsa
->completion_func
= lpCompletionRoutine
;
5591 release_sock_fd( s
, fd
);
5595 iosb
->u
.Status
= STATUS_PENDING
;
5596 iosb
->Information
= 0;
5598 SERVER_START_REQ( register_async
)
5600 req
->type
= ASYNC_TYPE_READ
;
5601 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
5602 req
->async
.callback
= wine_server_client_ptr( WS2_async_recv
);
5603 req
->async
.iosb
= wine_server_client_ptr( iosb
);
5604 req
->async
.arg
= wine_server_client_ptr( wsa
);
5605 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
5606 req
->async
.cvalue
= cvalue
;
5607 err
= wine_server_call( req
);
5611 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
5612 WSASetLastError( NtStatusToWSAError( err
));
5613 return SOCKET_ERROR
;
5616 iosb
->u
.Status
= STATUS_SUCCESS
;
5617 iosb
->Information
= n
;
5618 if (!wsa
->completion_func
)
5620 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
5621 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
5622 HeapFree( GetProcessHeap(), 0, wsa
);
5624 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
5625 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
5626 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5632 if ( _is_blocking(s
) )
5635 int timeout
= GET_RCVTIMEO(fd
);
5638 timeout
-= GetTickCount() - timeout_start
;
5639 if (timeout
< 0) timeout
= 0;
5643 pfd
.events
= POLLIN
;
5644 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
5646 if (!timeout
|| !poll( &pfd
, 1, timeout
))
5649 /* a timeout is not fatal */
5650 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5656 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5657 err
= WSAEWOULDBLOCK
;
5662 TRACE(" -> %i bytes\n", n
);
5663 HeapFree( GetProcessHeap(), 0, wsa
);
5664 release_sock_fd( s
, fd
);
5665 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5670 HeapFree( GetProcessHeap(), 0, wsa
);
5671 release_sock_fd( s
, fd
);
5672 WARN(" -> ERROR %d\n", err
);
5673 WSASetLastError( err
);
5674 return SOCKET_ERROR
;
5677 /***********************************************************************
5678 * WSARecvFrom (WS2_32.69)
5680 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5681 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
5682 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
5683 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5686 return WS2_recvfrom( s
, lpBuffers
, dwBufferCount
,
5687 lpNumberOfBytesRecvd
, lpFlags
,
5689 lpOverlapped
, lpCompletionRoutine
);
5692 /***********************************************************************
5693 * WSCInstallProvider (WS2_32.88)
5695 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
5696 LPCWSTR lpszProviderDllPath
,
5697 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
5698 DWORD dwNumberOfEntries
,
5701 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
5702 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
5703 dwNumberOfEntries
, lpErrno
);
5709 /***********************************************************************
5710 * WSCDeinstallProvider (WS2_32.83)
5712 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
5714 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
5720 /***********************************************************************
5721 * WSAAccept (WS2_32.26)
5723 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
5724 LPCONDITIONPROC lpfnCondition
, DWORD dwCallbackData
)
5727 int ret
= 0, size
= 0;
5728 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
5729 /* QOS SQOS, GQOS; */
5732 SOCKADDR src_addr
, dst_addr
;
5734 TRACE("Socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %d\n",
5735 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
5738 size
= sizeof(src_addr
);
5739 cs
= WS_accept(s
, &src_addr
, &size
);
5741 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
5743 if (!lpfnCondition
) return cs
;
5745 CallerId
.buf
= (char *)&src_addr
;
5746 CallerId
.len
= sizeof(src_addr
);
5748 CallerData
.buf
= NULL
;
5751 WS_getsockname(cs
, &dst_addr
, &size
);
5753 CalleeId
.buf
= (char *)&dst_addr
;
5754 CalleeId
.len
= sizeof(dst_addr
);
5757 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
5758 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
5763 if (addr
&& addrlen
)
5764 addr
= memcpy(addr
, &src_addr
, (*addrlen
> size
) ? size
: *addrlen
);
5767 SERVER_START_REQ( set_socket_deferred
)
5769 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5770 req
->deferred
= wine_server_obj_handle( SOCKET2HANDLE(cs
) );
5771 if ( !wine_server_call_err ( req
) )
5773 SetLastError( WSATRY_AGAIN
);
5774 WS_closesocket( cs
);
5778 return SOCKET_ERROR
;
5781 SetLastError(WSAECONNREFUSED
);
5782 return SOCKET_ERROR
;
5784 FIXME("Unknown return type from Condition function\n");
5785 SetLastError(WSAENOTSOCK
);
5786 return SOCKET_ERROR
;
5790 /***********************************************************************
5791 * WSADuplicateSocketA (WS2_32.32)
5793 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
5797 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
5798 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
5799 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
5800 /* I don't know what the real Windoze does next, this is a hack */
5801 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
5802 * the target use the global duplicate, or we could copy a reference to us to the structure
5803 * and let the target duplicate it from us, but let's do it as simple as possible */
5804 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
5805 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
5806 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
5807 0, FALSE
, DUPLICATE_SAME_ACCESS
);
5808 CloseHandle(hProcess
);
5809 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
5813 /***********************************************************************
5814 * WSADuplicateSocketW (WS2_32.33)
5816 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
5820 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
5822 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
5823 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
5824 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
5825 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
5826 0, FALSE
, DUPLICATE_SAME_ACCESS
);
5827 CloseHandle(hProcess
);
5828 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
5832 /***********************************************************************
5833 * WSAInstallServiceClassA (WS2_32.48)
5835 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
5837 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
5838 WSASetLastError(WSAEACCES
);
5839 return SOCKET_ERROR
;
5842 /***********************************************************************
5843 * WSAInstallServiceClassW (WS2_32.49)
5845 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
5847 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
5848 WSASetLastError(WSAEACCES
);
5849 return SOCKET_ERROR
;
5852 /***********************************************************************
5853 * WSARemoveServiceClass (WS2_32.70)
5855 int WINAPI
WSARemoveServiceClass(LPGUID info
)
5857 FIXME("Request to remove service %p\n",info
);
5858 WSASetLastError(WSATYPE_NOT_FOUND
);
5859 return SOCKET_ERROR
;
5862 /***********************************************************************
5863 * inet_ntop (WS2_32.@)
5865 PCSTR WINAPI
WS_inet_ntop( INT family
, PVOID addr
, PSTR buffer
, SIZE_T len
)
5867 #ifdef HAVE_INET_NTOP
5868 struct WS_in6_addr
*in6
;
5869 struct WS_in_addr
*in
;
5872 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
5875 WSASetLastError( STATUS_INVALID_PARAMETER
);
5884 pdst
= inet_ntop( AF_INET
, &in
->WS_s_addr
, buffer
, len
);
5890 pdst
= inet_ntop( AF_INET6
, in6
->WS_s6_addr
, buffer
, len
);
5894 WSASetLastError( WSAEAFNOSUPPORT
);
5898 if (!pdst
) WSASetLastError( STATUS_INVALID_PARAMETER
);
5901 FIXME( "not supported on this platform\n" );
5902 WSASetLastError( WSAEAFNOSUPPORT
);
5907 /***********************************************************************
5908 * WSAStringToAddressA (WS2_32.80)
5910 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
5912 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
5913 LPSOCKADDR lpAddress
,
5914 LPINT lpAddressLength
)
5917 LPSTR workBuffer
=NULL
,ptrPort
;
5919 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_a(AddressString
), AddressFamily
,
5920 lpProtocolInfo
, lpAddress
, lpAddressLength
);
5922 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
5926 WSASetLastError(WSAEINVAL
);
5927 return SOCKET_ERROR
;
5931 FIXME("ProtocolInfo not implemented.\n");
5933 workBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
5934 strlen(AddressString
) + 1);
5937 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
5938 return SOCKET_ERROR
;
5941 strcpy(workBuffer
, AddressString
);
5943 switch(AddressFamily
)
5947 struct in_addr inetaddr
;
5949 /* If lpAddressLength is too small, tell caller the size we need */
5950 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
5952 *lpAddressLength
= sizeof(SOCKADDR_IN
);
5956 *lpAddressLength
= sizeof(SOCKADDR_IN
);
5957 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
5959 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= AF_INET
;
5961 ptrPort
= strchr(workBuffer
, ':');
5964 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= htons(atoi(ptrPort
+1));
5969 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= 0;
5972 if(inet_aton(workBuffer
, &inetaddr
) > 0)
5974 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
5985 struct in6_addr inetaddr
;
5986 /* If lpAddressLength is too small, tell caller the size we need */
5987 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
5989 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
5993 #ifdef HAVE_INET_PTON
5994 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
5995 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
5997 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_family
= WS_AF_INET6
;
5999 /* This one is a bit tricky. An IPv6 address contains colons, so the
6000 * check from IPv4 doesn't work like that. However, IPv6 addresses that
6001 * contain a port are written with braces like [fd12:3456:7890::1]:12345
6002 * so what we will do is to look for ']', check if the next char is a
6003 * colon, and if it is, parse the port as in IPv4. */
6005 ptrPort
= strchr(workBuffer
, ']');
6006 if(ptrPort
&& *(++ptrPort
) == ':')
6008 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= htons(atoi(ptrPort
+1));
6013 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= 0;
6016 if(inet_pton(AF_INET6
, workBuffer
, &inetaddr
) > 0)
6018 memcpy(&((LPSOCKADDR_IN6
)lpAddress
)->sin6_addr
, &inetaddr
,
6019 sizeof(struct in6_addr
));
6023 #endif /* HAVE_INET_PTON */
6029 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
6030 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
6034 HeapFree(GetProcessHeap(), 0, workBuffer
);
6037 WSASetLastError(res
);
6038 return SOCKET_ERROR
;
6041 /***********************************************************************
6042 * WSAStringToAddressW (WS2_32.81)
6044 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
6045 * If this should be the case, it would be required to map these digits
6046 * to Unicode digits (0-9) using FoldString first.
6048 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
6050 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
6051 LPSOCKADDR lpAddress
,
6052 LPINT lpAddressLength
)
6055 LPSTR workBuffer
=NULL
;
6056 WSAPROTOCOL_INFOA infoA
;
6057 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
6059 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
6060 lpAddress
, lpAddressLength
);
6062 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
6064 /* if ProtocolInfo is available - convert to ANSI variant */
6067 lpProtoInfoA
= &infoA
;
6068 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
6070 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
6071 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
6073 WSASetLastError( WSAEINVAL
);
6074 return SOCKET_ERROR
;
6080 /* Translate AddressString to ANSI code page - assumes that only
6081 standard digits 0-9 are used with this API call */
6082 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
6083 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
6087 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
6088 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
6089 lpAddress
,lpAddressLength
);
6090 HeapFree( GetProcessHeap(), 0, workBuffer
);
6094 res
= WSA_NOT_ENOUGH_MEMORY
;
6099 WSASetLastError(res
);
6100 return SOCKET_ERROR
;
6103 /***********************************************************************
6104 * WSAAddressToStringA (WS2_32.27)
6106 * See WSAAddressToStringW
6108 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
6109 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
6113 CHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
6116 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
6118 if (!sockaddr
) return SOCKET_ERROR
;
6119 if (!string
|| !lenstr
) return SOCKET_ERROR
;
6121 switch(sockaddr
->sa_family
)
6124 if (len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
6125 sprintf( buffer
, "%u.%u.%u.%u:%u",
6126 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
6127 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
6128 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
6129 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
6130 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
6132 p
= strchr( buffer
, ':' );
6133 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
6138 struct WS_sockaddr_in6
*sockaddr6
= (LPSOCKADDR_IN6
) sockaddr
;
6141 if (len
< sizeof(SOCKADDR_IN6
)) return SOCKET_ERROR
;
6142 if ((sockaddr6
->sin6_port
))
6143 strcpy(buffer
, "[");
6144 if (!WS_inet_ntop(WS_AF_INET6
, &sockaddr6
->sin6_addr
, buffer
+strlen(buffer
), sizeof(buffer
)))
6146 WSASetLastError(WSAEINVAL
);
6147 return SOCKET_ERROR
;
6149 if ((sockaddr6
->sin6_scope_id
))
6150 sprintf(buffer
+strlen(buffer
), "%%%u", sockaddr6
->sin6_scope_id
);
6151 if ((sockaddr6
->sin6_port
))
6152 sprintf(buffer
+strlen(buffer
), "]:%u", ntohs(sockaddr6
->sin6_port
));
6157 WSASetLastError(WSAEINVAL
);
6158 return SOCKET_ERROR
;
6161 size
= strlen( buffer
) + 1;
6166 WSASetLastError(WSAEFAULT
);
6167 return SOCKET_ERROR
;
6171 strcpy( string
, buffer
);
6175 /***********************************************************************
6176 * WSAAddressToStringW (WS2_32.28)
6178 * Convert a sockaddr address into a readable address string.
6181 * sockaddr [I] Pointer to a sockaddr structure.
6182 * len [I] Size of the sockaddr structure.
6183 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
6184 * string [I/O] Pointer to a buffer to receive the address string.
6185 * lenstr [I/O] Size of the receive buffer in WCHARs.
6189 * Failure: SOCKET_ERROR
6192 * The 'info' parameter is ignored.
6194 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
6195 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
6200 WCHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
6203 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
6206 ret
= WSAAddressToStringA(sockaddr
, len
, NULL
, bufAddr
, &size
);
6208 if (ret
) return ret
;
6210 MultiByteToWideChar( CP_ACP
, 0, bufAddr
, size
, buffer
, sizeof( buffer
)/sizeof(WCHAR
));
6215 WSASetLastError(WSAEFAULT
);
6216 return SOCKET_ERROR
;
6220 lstrcpyW( string
, buffer
);
6224 /***********************************************************************
6225 * WSAEnumNameSpaceProvidersA (WS2_32.34)
6227 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
6229 FIXME( "(%p %p) Stub!\n", len
, buffer
);
6233 /***********************************************************************
6234 * WSAEnumNameSpaceProvidersW (WS2_32.35)
6236 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
6238 FIXME( "(%p %p) Stub!\n", len
, buffer
);
6242 /***********************************************************************
6243 * WSAGetQOSByName (WS2_32.41)
6245 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
6247 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
6251 /***********************************************************************
6252 * WSAGetServiceClassInfoA (WS2_32.42)
6254 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
6255 LPWSASERVICECLASSINFOA info
)
6257 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
6259 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6260 return SOCKET_ERROR
;
6263 /***********************************************************************
6264 * WSAGetServiceClassInfoW (WS2_32.43)
6266 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
6267 LPWSASERVICECLASSINFOW info
)
6269 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
6271 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6272 return SOCKET_ERROR
;
6275 /***********************************************************************
6276 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
6278 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
6280 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
6281 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6282 return SOCKET_ERROR
;
6285 /***********************************************************************
6286 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
6288 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
6290 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
6291 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6292 return SOCKET_ERROR
;
6295 /***********************************************************************
6296 * WSALookupServiceBeginA (WS2_32.59)
6298 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
6299 DWORD dwControlFlags
,
6302 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
6304 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6305 return SOCKET_ERROR
;
6308 /***********************************************************************
6309 * WSALookupServiceBeginW (WS2_32.60)
6311 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
6312 DWORD dwControlFlags
,
6315 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
6317 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6318 return SOCKET_ERROR
;
6321 /***********************************************************************
6322 * WSALookupServiceBeginW (WS2_32.61)
6324 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
6326 FIXME("(%p) Stub!\n", lookup
);
6330 /***********************************************************************
6331 * WSALookupServiceNextA (WS2_32.62)
6333 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
6335 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
6336 WSASetLastError(WSA_E_NO_MORE
);
6337 return SOCKET_ERROR
;
6340 /***********************************************************************
6341 * WSALookupServiceNextW (WS2_32.63)
6343 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
6345 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
6346 WSASetLastError(WSA_E_NO_MORE
);
6347 return SOCKET_ERROR
;
6350 /***********************************************************************
6351 * WSANtohl (WS2_32.64)
6353 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
6355 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
6357 if (!lphostlong
) return WSAEFAULT
;
6359 *lphostlong
= ntohl( netlong
);
6363 /***********************************************************************
6364 * WSANtohs (WS2_32.65)
6366 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
6368 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
6370 if (!lphostshort
) return WSAEFAULT
;
6372 *lphostshort
= ntohs( netshort
);
6376 /***********************************************************************
6377 * WSAProviderConfigChange (WS2_32.66)
6379 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
6380 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
6382 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
6383 return SOCKET_ERROR
;
6386 /***********************************************************************
6387 * WSARecvDisconnect (WS2_32.68)
6389 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
6391 TRACE( "(0x%04lx %p)\n", s
, disconnectdata
);
6393 return WS_shutdown( s
, 0 );
6396 /***********************************************************************
6397 * WSASetServiceA (WS2_32.76)
6399 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
6401 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
6405 /***********************************************************************
6406 * WSASetServiceW (WS2_32.77)
6408 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
6410 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
6414 /***********************************************************************
6415 * WSCEnableNSProvider (WS2_32.84)
6417 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
6419 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
6423 /***********************************************************************
6424 * WSCGetProviderPath (WS2_32.86)
6426 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
6428 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
6430 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
6432 *errcode
= WSAEINVAL
;
6433 return SOCKET_ERROR
;
6436 /***********************************************************************
6437 * WSCInstallNameSpace (WS2_32.87)
6439 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
6440 DWORD version
, LPGUID provider
)
6442 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
6443 namespace, version
, debugstr_guid(provider
) );
6447 /***********************************************************************
6448 * WSCUnInstallNameSpace (WS2_32.89)
6450 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
6452 FIXME("(%p) Stub!\n", lpProviderId
);
6456 /***********************************************************************
6457 * WSCWriteProviderOrder (WS2_32.91)
6459 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
6461 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);
6465 /***********************************************************************
6466 * WSANSPIoctl (WS2_32.91)
6468 INT WINAPI
WSANSPIoctl( HANDLE hLookup
, DWORD dwControlCode
, LPVOID lpvInBuffer
,
6469 DWORD cbInBuffer
, LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
6470 LPDWORD lpcbBytesReturned
, LPWSACOMPLETION lpCompletion
)
6472 FIXME("(%p, 0x%08x, %p, 0x%08x, %p, 0x%08x, %p, %p) Stub!\n", hLookup
, dwControlCode
,
6473 lpvInBuffer
, cbInBuffer
, lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpCompletion
);
6474 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
6475 return SOCKET_ERROR
;