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 /****************************************************************/
271 /* ----------------------------------- internal data */
273 /* ws_... struct conversion flags */
275 typedef struct /* WSAAsyncSelect() control struct */
277 HANDLE service
, event
, sock
;
283 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
284 #define WS_MAX_UDP_DATAGRAM 1024
285 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
287 /* hostent's, servent's and protent's are stored in one buffer per thread,
288 * as documented on MSDN for the functions that return any of the buffers */
289 struct per_thread_data
292 struct WS_hostent
*he_buffer
;
293 struct WS_servent
*se_buffer
;
294 struct WS_protoent
*pe_buffer
;
300 /* internal: routing description information */
307 static INT num_startup
; /* reference counter */
308 static FARPROC blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
310 /* function prototypes */
311 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int addresses
, int fill_addresses
);
312 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
313 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
);
314 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
316 int WSAIOCTL_GetInterfaceCount(void);
317 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
319 #define MAP_OPTION(opt) { WS_##opt, opt }
321 static const int ws_sock_map
[][2] =
323 MAP_OPTION( SO_DEBUG
),
324 MAP_OPTION( SO_ACCEPTCONN
),
325 MAP_OPTION( SO_REUSEADDR
),
326 MAP_OPTION( SO_KEEPALIVE
),
327 MAP_OPTION( SO_DONTROUTE
),
328 MAP_OPTION( SO_BROADCAST
),
329 MAP_OPTION( SO_LINGER
),
330 MAP_OPTION( SO_OOBINLINE
),
331 MAP_OPTION( SO_SNDBUF
),
332 MAP_OPTION( SO_RCVBUF
),
333 MAP_OPTION( SO_ERROR
),
334 MAP_OPTION( SO_TYPE
),
336 MAP_OPTION( SO_RCVTIMEO
),
339 MAP_OPTION( SO_SNDTIMEO
),
343 static const int ws_tcp_map
[][2] =
346 MAP_OPTION( TCP_NODELAY
),
350 static const int ws_ip_map
[][2] =
352 MAP_OPTION( IP_MULTICAST_IF
),
353 MAP_OPTION( IP_MULTICAST_TTL
),
354 MAP_OPTION( IP_MULTICAST_LOOP
),
355 MAP_OPTION( IP_ADD_MEMBERSHIP
),
356 MAP_OPTION( IP_DROP_MEMBERSHIP
),
357 MAP_OPTION( IP_OPTIONS
),
359 MAP_OPTION( IP_HDRINCL
),
361 MAP_OPTION( IP_TOS
),
362 MAP_OPTION( IP_TTL
),
365 static const int ws_ipv6_map
[][2] =
367 #ifdef IPV6_ADD_MEMBERSHIP
368 MAP_OPTION( IPV6_ADD_MEMBERSHIP
),
370 #ifdef IPV6_DROP_MEMBERSHIP
371 MAP_OPTION( IPV6_DROP_MEMBERSHIP
),
373 MAP_OPTION( IPV6_MULTICAST_IF
),
374 MAP_OPTION( IPV6_MULTICAST_HOPS
),
375 MAP_OPTION( IPV6_MULTICAST_LOOP
),
376 MAP_OPTION( IPV6_UNICAST_HOPS
),
377 MAP_OPTION( IPV6_V6ONLY
),
380 static const int ws_af_map
[][2] =
382 MAP_OPTION( AF_UNSPEC
),
383 MAP_OPTION( AF_INET
),
384 MAP_OPTION( AF_INET6
),
386 MAP_OPTION( AF_IPX
),
389 MAP_OPTION( AF_IRDA
),
391 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
394 static const int ws_socktype_map
[][2] =
396 MAP_OPTION( SOCK_DGRAM
),
397 MAP_OPTION( SOCK_STREAM
),
398 MAP_OPTION( SOCK_RAW
),
399 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
402 static const int ws_proto_map
[][2] =
404 MAP_OPTION( IPPROTO_IP
),
405 MAP_OPTION( IPPROTO_TCP
),
406 MAP_OPTION( IPPROTO_UDP
),
407 MAP_OPTION( IPPROTO_ICMP
),
408 MAP_OPTION( IPPROTO_IGMP
),
409 MAP_OPTION( IPPROTO_RAW
),
410 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
413 static const int ws_aiflag_map
[][2] =
415 MAP_OPTION( AI_PASSIVE
),
416 MAP_OPTION( AI_CANONNAME
),
417 MAP_OPTION( AI_NUMERICHOST
),
418 /* Linux/UNIX knows a lot more. But Windows only
419 * has 3 as far as I could see. -Marcus
423 static const int ws_niflag_map
[][2] =
425 MAP_OPTION( NI_NOFQDN
),
426 MAP_OPTION( NI_NUMERICHOST
),
427 MAP_OPTION( NI_NAMEREQD
),
428 MAP_OPTION( NI_NUMERICSERV
),
429 MAP_OPTION( NI_DGRAM
),
432 static const int ws_eai_map
[][2] =
434 MAP_OPTION( EAI_AGAIN
),
435 MAP_OPTION( EAI_BADFLAGS
),
436 MAP_OPTION( EAI_FAIL
),
437 MAP_OPTION( EAI_FAMILY
),
438 MAP_OPTION( EAI_MEMORY
),
439 /* Note: EAI_NODATA is deprecated, but still
440 * used by Windows and Linux... We map the newer
441 * EAI_NONAME to EAI_NODATA for now until Windows
445 MAP_OPTION( EAI_NODATA
),
448 { WS_EAI_NODATA
, EAI_NONAME
},
451 MAP_OPTION( EAI_SERVICE
),
452 MAP_OPTION( EAI_SOCKTYPE
),
456 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
458 /* ----------------------------------- error handling */
460 static NTSTATUS
sock_get_ntstatus( int err
)
464 case EBADF
: return STATUS_INVALID_HANDLE
;
465 case EBUSY
: return STATUS_DEVICE_BUSY
;
467 case EACCES
: return STATUS_ACCESS_DENIED
;
468 case EFAULT
: return STATUS_NO_MEMORY
;
469 case EINVAL
: return STATUS_INVALID_PARAMETER
;
471 case EMFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
472 case EWOULDBLOCK
: return STATUS_CANT_WAIT
;
473 case EINPROGRESS
: return STATUS_PENDING
;
474 case EALREADY
: return STATUS_NETWORK_BUSY
;
475 case ENOTSOCK
: return STATUS_OBJECT_TYPE_MISMATCH
;
476 case EDESTADDRREQ
: return STATUS_INVALID_PARAMETER
;
477 case EMSGSIZE
: return STATUS_BUFFER_OVERFLOW
;
478 case EPROTONOSUPPORT
:
479 case ESOCKTNOSUPPORT
:
482 case EPROTOTYPE
: return STATUS_NOT_SUPPORTED
;
483 case ENOPROTOOPT
: return STATUS_INVALID_PARAMETER
;
484 case EOPNOTSUPP
: return STATUS_NOT_SUPPORTED
;
485 case EADDRINUSE
: return STATUS_ADDRESS_ALREADY_ASSOCIATED
;
486 case EADDRNOTAVAIL
: return STATUS_INVALID_PARAMETER
;
487 case ECONNREFUSED
: return STATUS_CONNECTION_REFUSED
;
488 case ESHUTDOWN
: return STATUS_PIPE_DISCONNECTED
;
489 case ENOTCONN
: return STATUS_CONNECTION_DISCONNECTED
;
490 case ETIMEDOUT
: return STATUS_IO_TIMEOUT
;
491 case ENETUNREACH
: return STATUS_NETWORK_UNREACHABLE
;
492 case ENETDOWN
: return STATUS_NETWORK_BUSY
;
494 case ECONNRESET
: return STATUS_CONNECTION_RESET
;
495 case ECONNABORTED
: return STATUS_CONNECTION_ABORTED
;
497 case 0: return STATUS_SUCCESS
;
499 WARN("Unknown errno %d!\n", err
);
500 return STATUS_UNSUCCESSFUL
;
504 static UINT
sock_get_error( int err
)
508 case EINTR
: return WSAEINTR
;
509 case EBADF
: return WSAEBADF
;
511 case EACCES
: return WSAEACCES
;
512 case EFAULT
: return WSAEFAULT
;
513 case EINVAL
: return WSAEINVAL
;
514 case EMFILE
: return WSAEMFILE
;
515 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
516 case EINPROGRESS
: return WSAEINPROGRESS
;
517 case EALREADY
: return WSAEALREADY
;
518 case ENOTSOCK
: return WSAENOTSOCK
;
519 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
520 case EMSGSIZE
: return WSAEMSGSIZE
;
521 case EPROTOTYPE
: return WSAEPROTOTYPE
;
522 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
523 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
524 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
525 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
526 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
527 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
528 case EADDRINUSE
: return WSAEADDRINUSE
;
529 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
530 case ENETDOWN
: return WSAENETDOWN
;
531 case ENETUNREACH
: return WSAENETUNREACH
;
532 case ENETRESET
: return WSAENETRESET
;
533 case ECONNABORTED
: return WSAECONNABORTED
;
535 case ECONNRESET
: return WSAECONNRESET
;
536 case ENOBUFS
: return WSAENOBUFS
;
537 case EISCONN
: return WSAEISCONN
;
538 case ENOTCONN
: return WSAENOTCONN
;
539 case ESHUTDOWN
: return WSAESHUTDOWN
;
540 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
541 case ETIMEDOUT
: return WSAETIMEDOUT
;
542 case ECONNREFUSED
: return WSAECONNREFUSED
;
543 case ELOOP
: return WSAELOOP
;
544 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
545 case EHOSTDOWN
: return WSAEHOSTDOWN
;
546 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
547 case ENOTEMPTY
: return WSAENOTEMPTY
;
549 case EPROCLIM
: return WSAEPROCLIM
;
552 case EUSERS
: return WSAEUSERS
;
555 case EDQUOT
: return WSAEDQUOT
;
558 case ESTALE
: return WSAESTALE
;
561 case EREMOTE
: return WSAEREMOTE
;
564 /* just in case we ever get here and there are no problems */
567 WARN("Unknown errno %d!\n", err
);
568 return WSAEOPNOTSUPP
;
572 static UINT
wsaErrno(void)
574 int loc_errno
= errno
;
575 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
577 return sock_get_error( loc_errno
);
580 /* most ws2 overlapped functions return an ntstatus-based error code */
581 static NTSTATUS
wsaErrStatus(void)
583 int loc_errno
= errno
;
584 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
586 return sock_get_ntstatus(loc_errno
);
589 static UINT
wsaHerrno(int loc_errno
)
591 WARN("h_errno %d.\n", loc_errno
);
595 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
596 case TRY_AGAIN
: return WSATRY_AGAIN
;
597 case NO_RECOVERY
: return WSANO_RECOVERY
;
598 case NO_DATA
: return WSANO_DATA
;
599 case ENOBUFS
: return WSAENOBUFS
;
603 WARN("Unknown h_errno %d!\n", loc_errno
);
604 return WSAEOPNOTSUPP
;
608 static inline DWORD
NtStatusToWSAError( const DWORD status
)
610 /* We only need to cover the status codes set by server async request handling */
614 case STATUS_SUCCESS
: wserr
= 0; break;
615 case STATUS_PENDING
: wserr
= WSA_IO_PENDING
; break;
616 case STATUS_OBJECT_TYPE_MISMATCH
: wserr
= WSAENOTSOCK
; break;
617 case STATUS_INVALID_HANDLE
: wserr
= WSAEBADF
; break;
618 case STATUS_INVALID_PARAMETER
: wserr
= WSAEINVAL
; break;
619 case STATUS_PIPE_DISCONNECTED
: wserr
= WSAESHUTDOWN
; break;
620 case STATUS_NETWORK_BUSY
: wserr
= WSAEALREADY
; break;
621 case STATUS_NETWORK_UNREACHABLE
: wserr
= WSAENETUNREACH
; break;
622 case STATUS_CONNECTION_REFUSED
: wserr
= WSAECONNREFUSED
; break;
623 case STATUS_CONNECTION_DISCONNECTED
: wserr
= WSAENOTCONN
; break;
624 case STATUS_CONNECTION_RESET
: wserr
= WSAECONNRESET
; break;
625 case STATUS_CONNECTION_ABORTED
: wserr
= WSAECONNABORTED
; break;
626 case STATUS_CANCELLED
: wserr
= WSA_OPERATION_ABORTED
; break;
627 case STATUS_ADDRESS_ALREADY_ASSOCIATED
: wserr
= WSAEADDRINUSE
; break;
628 case STATUS_IO_TIMEOUT
:
629 case STATUS_TIMEOUT
: wserr
= WSAETIMEDOUT
; break;
630 case STATUS_NO_MEMORY
: wserr
= WSAEFAULT
; break;
631 case STATUS_ACCESS_DENIED
: wserr
= WSAEACCES
; break;
632 case STATUS_TOO_MANY_OPENED_FILES
: wserr
= WSAEMFILE
; break;
633 case STATUS_CANT_WAIT
: wserr
= WSAEWOULDBLOCK
; break;
634 case STATUS_BUFFER_OVERFLOW
: wserr
= WSAEMSGSIZE
; break;
635 case STATUS_NOT_SUPPORTED
: wserr
= WSAEOPNOTSUPP
; break;
636 case STATUS_HOST_UNREACHABLE
: wserr
= WSAEHOSTUNREACH
; break;
639 wserr
= RtlNtStatusToDosError( status
);
640 FIXME( "Status code %08x converted to DOS error code %x\n", status
, wserr
);
645 /* set last error code from NT status without mapping WSA errors */
646 static inline unsigned int set_error( unsigned int err
)
650 err
= NtStatusToWSAError( err
);
656 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
659 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
664 static inline void release_sock_fd( SOCKET s
, int fd
)
666 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
669 static void _enable_event( HANDLE s
, unsigned int event
,
670 unsigned int sstate
, unsigned int cstate
)
672 SERVER_START_REQ( enable_socket_event
)
674 req
->handle
= wine_server_obj_handle( s
);
676 req
->sstate
= sstate
;
677 req
->cstate
= cstate
;
678 wine_server_call( req
);
683 static int _is_blocking(SOCKET s
)
686 SERVER_START_REQ( get_socket_event
)
688 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
689 req
->service
= FALSE
;
691 wine_server_call( req
);
692 ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
698 static unsigned int _get_sock_mask(SOCKET s
)
701 SERVER_START_REQ( get_socket_event
)
703 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
704 req
->service
= FALSE
;
706 wine_server_call( req
);
713 static void _sync_sock_state(SOCKET s
)
715 /* do a dummy wineserver request in order to let
716 the wineserver run through its select loop once */
717 (void)_is_blocking(s
);
720 static int _get_sock_error(SOCKET s
, unsigned int bit
)
722 int events
[FD_MAX_EVENTS
];
724 SERVER_START_REQ( get_socket_event
)
726 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
727 req
->service
= FALSE
;
729 wine_server_set_reply( req
, events
, sizeof(events
) );
730 wine_server_call( req
);
736 static struct per_thread_data
*get_per_thread_data(void)
738 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
739 /* lazy initialization */
742 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
743 NtCurrentTeb()->WinSockData
= ptb
;
748 static void free_per_thread_data(void)
750 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
754 /* delete scratch buffers */
755 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
756 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
757 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
758 ptb
->he_buffer
= NULL
;
759 ptb
->se_buffer
= NULL
;
760 ptb
->pe_buffer
= NULL
;
762 HeapFree( GetProcessHeap(), 0, ptb
);
763 NtCurrentTeb()->WinSockData
= NULL
;
766 /***********************************************************************
767 * DllMain (WS2_32.init)
769 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
771 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
773 case DLL_PROCESS_ATTACH
:
775 case DLL_PROCESS_DETACH
:
776 free_per_thread_data();
779 case DLL_THREAD_DETACH
:
780 free_per_thread_data();
786 /***********************************************************************
789 * Converts socket flags from Windows format.
790 * Return 1 if converted, 0 if not (error).
792 static int convert_sockopt(INT
*level
, INT
*optname
)
799 for(i
=0; i
<sizeof(ws_sock_map
)/sizeof(ws_sock_map
[0]); i
++) {
800 if( ws_sock_map
[i
][0] == *optname
)
802 *optname
= ws_sock_map
[i
][1];
806 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
809 *level
= IPPROTO_TCP
;
810 for(i
=0; i
<sizeof(ws_tcp_map
)/sizeof(ws_tcp_map
[0]); i
++) {
811 if ( ws_tcp_map
[i
][0] == *optname
)
813 *optname
= ws_tcp_map
[i
][1];
817 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
821 for(i
=0; i
<sizeof(ws_ip_map
)/sizeof(ws_ip_map
[0]); i
++) {
822 if (ws_ip_map
[i
][0] == *optname
)
824 *optname
= ws_ip_map
[i
][1];
828 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
830 case WS_IPPROTO_IPV6
:
831 *level
= IPPROTO_IPV6
;
832 for(i
=0; i
<sizeof(ws_ipv6_map
)/sizeof(ws_ipv6_map
[0]); i
++) {
833 if (ws_ipv6_map
[i
][0] == *optname
)
835 *optname
= ws_ipv6_map
[i
][1];
839 FIXME("Unknown IPPROTO_IPV6 optname 0x%x\n", *optname
);
841 default: FIXME("Unimplemented or unknown socket level\n");
846 /* ----------------------------------- Per-thread info (or per-process?) */
848 static char *strdup_lower(const char *str
)
851 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
855 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
858 else SetLastError(WSAENOBUFS
);
862 static inline int sock_error_p(int s
)
864 unsigned int optval
, optlen
;
866 optlen
= sizeof(optval
);
867 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
868 if (optval
) WARN("\t[%i] error: %d\n", s
, optval
);
872 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
873 * from an fd and return the value converted to milli seconds
874 * or -1 if there is an infinite time out */
875 static inline int get_rcvsnd_timeo( int fd
, int optname
)
878 unsigned int len
= sizeof(tv
);
879 int ret
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
881 ret
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
882 if( ret
<= 0 ) /* tv == {0,0} means infinite time out */
887 /* macro wrappers for portability */
889 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
891 #define GET_RCVTIMEO(fd) (-1)
895 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
897 #define GET_SNDTIMEO(fd) (-1)
900 /* utility: given an fd, will block until one of the events occurs */
901 static inline int do_block( int fd
, int events
, int timeout
)
909 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
920 convert_af_w2u(int windowsaf
) {
923 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
924 if (ws_af_map
[i
][0] == windowsaf
)
925 return ws_af_map
[i
][1];
926 FIXME("unhandled Windows address family %d\n", windowsaf
);
931 convert_af_u2w(int unixaf
) {
934 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
935 if (ws_af_map
[i
][1] == unixaf
)
936 return ws_af_map
[i
][0];
937 FIXME("unhandled UNIX address family %d\n", unixaf
);
942 convert_proto_w2u(int windowsproto
) {
945 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
946 if (ws_proto_map
[i
][0] == windowsproto
)
947 return ws_proto_map
[i
][1];
948 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
953 convert_proto_u2w(int unixproto
) {
956 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
957 if (ws_proto_map
[i
][1] == unixproto
)
958 return ws_proto_map
[i
][0];
959 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
964 convert_socktype_w2u(int windowssocktype
) {
967 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
968 if (ws_socktype_map
[i
][0] == windowssocktype
)
969 return ws_socktype_map
[i
][1];
970 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
975 convert_socktype_u2w(int unixsocktype
) {
978 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
979 if (ws_socktype_map
[i
][1] == unixsocktype
)
980 return ws_socktype_map
[i
][0];
981 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
985 /* ----------------------------------- API -----
987 * Init / cleanup / error checking.
990 /***********************************************************************
991 * WSAStartup (WS2_32.115)
993 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
995 TRACE("verReq=%x\n", wVersionRequested
);
997 if (LOBYTE(wVersionRequested
) < 1)
998 return WSAVERNOTSUPPORTED
;
1000 if (!lpWSAData
) return WSAEINVAL
;
1004 /* that's the whole of the negotiation for now */
1005 lpWSAData
->wVersion
= wVersionRequested
;
1006 /* return winsock information */
1007 lpWSAData
->wHighVersion
= 0x0202;
1008 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
1009 strcpy(lpWSAData
->szSystemStatus
, "Running" );
1010 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
1011 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
1012 /* don't do anything with lpWSAData->lpVendorInfo */
1013 /* (some apps don't allocate the space for this field) */
1015 TRACE("succeeded\n");
1020 /***********************************************************************
1021 * WSACleanup (WS2_32.116)
1023 INT WINAPI
WSACleanup(void)
1029 SetLastError(WSANOTINITIALISED
);
1030 return SOCKET_ERROR
;
1034 /***********************************************************************
1035 * WSAGetLastError (WS2_32.111)
1037 INT WINAPI
WSAGetLastError(void)
1039 return GetLastError();
1042 /***********************************************************************
1043 * WSASetLastError (WS2_32.112)
1045 void WINAPI
WSASetLastError(INT iError
) {
1046 SetLastError(iError
);
1049 static struct WS_hostent
*check_buffer_he(int size
)
1051 struct per_thread_data
* ptb
= get_per_thread_data();
1054 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
1055 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
1057 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
1058 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
1059 return ptb
->he_buffer
;
1062 static struct WS_servent
*check_buffer_se(int size
)
1064 struct per_thread_data
* ptb
= get_per_thread_data();
1067 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
1068 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
1070 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
1071 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
1072 return ptb
->se_buffer
;
1075 static struct WS_protoent
*check_buffer_pe(int size
)
1077 struct per_thread_data
* ptb
= get_per_thread_data();
1080 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
1081 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
1083 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
1084 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
1085 return ptb
->pe_buffer
;
1088 /* ----------------------------------- i/o APIs */
1090 static inline BOOL
supported_pf(int pf
)
1111 /**********************************************************************/
1113 /* Returns the length of the converted address if successful, 0 if it was too small to
1116 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
1117 union generic_unix_sockaddr
*uaddr
)
1119 unsigned int uaddrlen
= 0;
1121 switch (wsaddr
->sa_family
)
1126 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
1127 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
1129 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
1132 uaddrlen
= sizeof(struct sockaddr_ipx
);
1133 memset( uaddr
, 0, uaddrlen
);
1134 uipx
->sipx_family
=AF_IPX
;
1135 uipx
->sipx_port
=wsipx
->sa_socket
;
1136 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
1139 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
1140 #ifdef IPX_FRAME_NONE
1141 uipx
->sipx_type
=IPX_FRAME_NONE
;
1147 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
1148 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
1150 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
1151 * scope_id, one without.
1153 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
1154 uaddrlen
= sizeof(struct sockaddr_in6
);
1155 memset( uaddr
, 0, uaddrlen
);
1156 uin6
->sin6_family
= AF_INET6
;
1157 uin6
->sin6_port
= win6
->sin6_port
;
1158 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
1159 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1160 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
1162 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1165 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
1169 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
1170 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
1172 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
1174 uaddrlen
= sizeof(struct sockaddr_in
);
1175 memset( uaddr
, 0, uaddrlen
);
1176 uin
->sin_family
= AF_INET
;
1177 uin
->sin_port
= win
->sin_port
;
1178 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
1183 struct sockaddr_irda
*uin
= (struct sockaddr_irda
*)uaddr
;
1184 const SOCKADDR_IRDA
*win
= (const SOCKADDR_IRDA
*)wsaddr
;
1186 if (wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1188 uaddrlen
= sizeof(struct sockaddr_irda
);
1189 memset( uaddr
, 0, uaddrlen
);
1190 uin
->sir_family
= AF_IRDA
;
1191 if (!strncmp( win
->irdaServiceName
, "LSAP-SEL", strlen( "LSAP-SEL" ) ))
1193 unsigned int lsap_sel
= 0;
1195 sscanf( win
->irdaServiceName
, "LSAP-SEL%u", &lsap_sel
);
1196 uin
->sir_lsap_sel
= lsap_sel
;
1200 uin
->sir_lsap_sel
= LSAP_ANY
;
1201 memcpy( uin
->sir_name
, win
->irdaServiceName
, 25 );
1203 memcpy( &uin
->sir_addr
, win
->irdaDeviceID
, sizeof(uin
->sir_addr
) );
1207 case WS_AF_UNSPEC
: {
1208 /* Try to determine the needed space by the passed windows sockaddr space */
1209 switch (wsaddrlen
) {
1210 default: /* likely a ipv4 address */
1211 case sizeof(struct WS_sockaddr_in
):
1212 uaddrlen
= sizeof(struct sockaddr_in
);
1215 case sizeof(struct WS_sockaddr_ipx
):
1216 uaddrlen
= sizeof(struct sockaddr_ipx
);
1220 case sizeof(SOCKADDR_IRDA
):
1221 uaddrlen
= sizeof(struct sockaddr_irda
);
1224 case sizeof(struct WS_sockaddr_in6
):
1225 case sizeof(struct WS_sockaddr_in6_old
):
1226 uaddrlen
= sizeof(struct sockaddr_in6
);
1229 memset( uaddr
, 0, uaddrlen
);
1233 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
1239 static BOOL
is_sockaddr_bound(const struct sockaddr
*uaddr
, int uaddrlen
)
1241 switch (uaddr
->sa_family
)
1245 FIXME("don't know how to tell if IPX socket is bound, assuming it is!\n");
1250 static const struct sockaddr_in6 emptyAddr
;
1251 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) uaddr
;
1252 return in6
->sin6_port
|| memcmp(&in6
->sin6_addr
, &emptyAddr
.sin6_addr
, sizeof(struct in6_addr
));
1256 static const struct sockaddr_in emptyAddr
;
1257 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) uaddr
;
1258 return in
->sin_port
|| memcmp(&in
->sin_addr
, &emptyAddr
.sin_addr
, sizeof(struct in_addr
));
1263 FIXME("unknown address family %d\n", uaddr
->sa_family
);
1268 /* Returns 0 if successful, -1 if the buffer is too small */
1269 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
1273 switch(uaddr
->sa_family
)
1278 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
1279 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
1282 switch (*wsaddrlen
) /* how much can we copy? */
1286 *wsaddrlen
= sizeof(*wsipx
);
1287 wsipx
->sa_socket
=uipx
->sipx_port
;
1291 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
1299 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
1305 wsipx
->sa_family
=WS_AF_IPX
;
1317 const struct sockaddr_irda
*uin
= (const struct sockaddr_irda
*)uaddr
;
1318 SOCKADDR_IRDA
*win
= (SOCKADDR_IRDA
*)wsaddr
;
1320 if (*wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1322 win
->irdaAddressFamily
= WS_AF_IRDA
;
1323 memcpy( win
->irdaDeviceID
, &uin
->sir_addr
, sizeof(win
->irdaDeviceID
) );
1324 if (uin
->sir_lsap_sel
!= LSAP_ANY
)
1325 sprintf( win
->irdaServiceName
, "LSAP-SEL%u", uin
->sir_lsap_sel
);
1327 memcpy( win
->irdaServiceName
, uin
->sir_name
,
1328 sizeof(win
->irdaServiceName
) );
1333 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
1334 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
1336 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
1338 win6old
->sin6_family
= WS_AF_INET6
;
1339 win6old
->sin6_port
= uin6
->sin6_port
;
1340 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
1341 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1342 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1343 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
1344 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
1345 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
1346 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
1349 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1351 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1356 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
1357 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
1359 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
1361 win
->sin_family
= WS_AF_INET
;
1362 win
->sin_port
= uin
->sin_port
;
1363 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
1364 memset(win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
1365 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
1369 memset(wsaddr
,0,*wsaddrlen
);
1373 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
1379 /**************************************************************************
1380 * Functions for handling overlapped I/O
1381 **************************************************************************/
1383 /* user APC called upon async completion */
1384 static void WINAPI
ws2_async_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1386 ws2_async
*wsa
= arg
;
1388 if (wsa
->completion_func
) wsa
->completion_func( NtStatusToWSAError(iosb
->u
.Status
),
1389 iosb
->Information
, wsa
->user_overlapped
,
1391 HeapFree( GetProcessHeap(), 0, wsa
);
1394 /***********************************************************************
1395 * WS2_recv (INTERNAL)
1397 * Workhorse for both synchronous and asynchronous recv() operations.
1399 static int WS2_recv( int fd
, struct ws2_async
*wsa
)
1402 union generic_unix_sockaddr unix_sockaddr
;
1405 hdr
.msg_name
= NULL
;
1409 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
1410 hdr
.msg_name
= &unix_sockaddr
;
1413 hdr
.msg_namelen
= 0;
1415 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1416 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1417 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1418 hdr
.msg_accrights
= NULL
;
1419 hdr
.msg_accrightslen
= 0;
1421 hdr
.msg_control
= NULL
;
1422 hdr
.msg_controllen
= 0;
1426 if ( (n
= recvmsg(fd
, &hdr
, wsa
->flags
)) == -1 )
1429 /* if this socket is connected and lpFrom is not NULL, Linux doesn't give us
1430 * msg_name and msg_namelen from recvmsg, but it does set msg_namelen to zero.
1432 * quoting linux 2.6 net/ipv4/tcp.c:
1433 * "According to UNIX98, msg_name/msg_namelen are ignored
1434 * on connected socket. I was just happy when found this 8) --ANK"
1436 * likewise MSDN says that lpFrom and lpFromlen are ignored for
1437 * connection-oriented sockets, so don't try to update lpFrom.
1439 if (wsa
->addr
&& hdr
.msg_namelen
)
1440 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, wsa
->addr
, wsa
->addrlen
.ptr
);
1445 /***********************************************************************
1446 * WS2_async_recv (INTERNAL)
1448 * Handler for overlapped recv() operations.
1450 static NTSTATUS
WS2_async_recv( void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
1452 ws2_async
* wsa
= user
;
1457 case STATUS_ALERTED
:
1458 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
1461 result
= WS2_recv( fd
, wsa
);
1462 wine_server_release_fd( wsa
->hSocket
, fd
);
1465 status
= STATUS_SUCCESS
;
1466 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1470 if (errno
== EINTR
|| errno
== EAGAIN
)
1472 status
= STATUS_PENDING
;
1473 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1478 status
= wsaErrStatus();
1483 if (status
!= STATUS_PENDING
)
1485 iosb
->u
.Status
= status
;
1486 iosb
->Information
= result
;
1487 *apc
= ws2_async_apc
;
1492 /***********************************************************************
1493 * WS2_send (INTERNAL)
1495 * Workhorse for both synchronous and asynchronous send() operations.
1497 static int WS2_send( int fd
, struct ws2_async
*wsa
)
1500 union generic_unix_sockaddr unix_addr
;
1502 hdr
.msg_name
= NULL
;
1503 hdr
.msg_namelen
= 0;
1507 hdr
.msg_name
= &unix_addr
;
1508 hdr
.msg_namelen
= ws_sockaddr_ws2u( wsa
->addr
, wsa
->addrlen
.val
, &unix_addr
);
1509 if ( !hdr
.msg_namelen
)
1515 #if defined(HAVE_IPX) && defined(SOL_IPX)
1516 if(wsa
->addr
->sa_family
== WS_AF_IPX
)
1518 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
1520 unsigned int len
=sizeof(int);
1522 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1523 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1524 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1525 * ipx type in the sockaddr_opx structure with the stored value.
1527 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
1528 uipx
->sipx_type
= val
;
1533 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1534 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1535 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1536 hdr
.msg_accrights
= NULL
;
1537 hdr
.msg_accrightslen
= 0;
1539 hdr
.msg_control
= NULL
;
1540 hdr
.msg_controllen
= 0;
1544 return sendmsg(fd
, &hdr
, wsa
->flags
);
1547 /***********************************************************************
1548 * WS2_async_send (INTERNAL)
1550 * Handler for overlapped send() operations.
1552 static NTSTATUS
WS2_async_send(void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
1554 ws2_async
* wsa
= user
;
1559 case STATUS_ALERTED
:
1560 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
1563 /* check to see if the data is ready (non-blocking) */
1564 result
= WS2_send( fd
, wsa
);
1565 wine_server_release_fd( wsa
->hSocket
, fd
);
1569 int totalLength
= 0;
1571 status
= STATUS_SUCCESS
;
1572 for (i
= 0; i
< wsa
->n_iovecs
; i
++)
1573 totalLength
+= wsa
->iovec
[i
].iov_len
;
1574 if (result
< totalLength
)
1575 _enable_event( wsa
->hSocket
, FD_WRITE
, 0, 0 );
1579 if (errno
== EINTR
|| errno
== EAGAIN
)
1581 status
= STATUS_PENDING
;
1582 _enable_event( wsa
->hSocket
, FD_WRITE
, 0, 0 );
1586 status
= wsaErrStatus();
1592 if (status
!= STATUS_PENDING
)
1594 iosb
->u
.Status
= status
;
1595 iosb
->Information
= result
;
1596 *apc
= ws2_async_apc
;
1601 /***********************************************************************
1602 * WS2_async_shutdown (INTERNAL)
1604 * Handler for shutdown() operations on overlapped sockets.
1606 static NTSTATUS
WS2_async_shutdown( void* user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
, void **apc
)
1608 ws2_async
* wsa
= user
;
1613 case STATUS_ALERTED
:
1614 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
1617 switch ( wsa
->type
)
1619 case ASYNC_TYPE_READ
: err
= shutdown( fd
, 0 ); break;
1620 case ASYNC_TYPE_WRITE
: err
= shutdown( fd
, 1 ); break;
1622 status
= err
? wsaErrStatus() : STATUS_SUCCESS
;
1623 wine_server_release_fd( wsa
->hSocket
, fd
);
1626 iosb
->u
.Status
= status
;
1627 iosb
->Information
= 0;
1628 *apc
= ws2_async_apc
;
1632 /***********************************************************************
1633 * WS2_register_async_shutdown (INTERNAL)
1635 * Helper function for WS_shutdown() on overlapped sockets.
1637 static int WS2_register_async_shutdown( SOCKET s
, int type
)
1639 struct ws2_async
*wsa
;
1642 TRACE("s %ld type %d\n", s
, type
);
1644 wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
1648 wsa
->hSocket
= SOCKET2HANDLE(s
);
1650 wsa
->completion_func
= NULL
;
1652 SERVER_START_REQ( register_async
)
1655 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
1656 req
->async
.callback
= wine_server_client_ptr( WS2_async_shutdown
);
1657 req
->async
.iosb
= wine_server_client_ptr( &wsa
->local_iosb
);
1658 req
->async
.arg
= wine_server_client_ptr( wsa
);
1659 req
->async
.cvalue
= 0;
1660 status
= wine_server_call( req
);
1664 if (status
!= STATUS_PENDING
)
1666 HeapFree( GetProcessHeap(), 0, wsa
);
1667 return NtStatusToWSAError( status
);
1672 /***********************************************************************
1675 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
,
1682 TRACE("socket %04lx\n", s
);
1683 is_blocking
= _is_blocking(s
);
1686 /* try accepting first (if there is a deferred connection) */
1687 SERVER_START_REQ( accept_socket
)
1689 req
->lhandle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1690 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
1691 req
->attributes
= OBJ_INHERIT
;
1692 status
= wine_server_call( req
);
1693 as
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
1698 if (addr
) WS_getpeername(as
, addr
, addrlen32
);
1701 if (is_blocking
&& status
== STATUS_CANT_WAIT
)
1703 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1705 do_block(fd
, POLLIN
, -1);
1706 _sync_sock_state(s
); /* let wineserver notice connection */
1707 release_sock_fd( s
, fd
);
1709 } while (is_blocking
&& status
== STATUS_CANT_WAIT
);
1712 return INVALID_SOCKET
;
1715 /***********************************************************************
1718 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
1720 int fd
= get_sock_fd( s
, 0, NULL
);
1721 int res
= SOCKET_ERROR
;
1723 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
1727 if (!name
|| (name
->sa_family
&& !supported_pf(name
->sa_family
)))
1729 SetLastError(WSAEAFNOSUPPORT
);
1733 union generic_unix_sockaddr uaddr
;
1734 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
1737 SetLastError(WSAEFAULT
);
1742 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) &uaddr
;
1743 if (name
->sa_family
== WS_AF_INET6
&&
1744 !memcmp(&in6
->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
)))
1747 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
)) == -1)
1749 release_sock_fd( s
, fd
);
1750 SetLastError(WSAEAFNOSUPPORT
);
1751 return SOCKET_ERROR
;
1755 if (name
->sa_family
== WS_AF_INET
)
1757 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
1758 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
1760 /* Trying to bind to the default host interface, using
1761 * INADDR_ANY instead*/
1762 WARN("Trying to bind to magic IP address, using "
1763 "INADDR_ANY instead.\n");
1764 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_ANY
);
1767 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
1769 int loc_errno
= errno
;
1770 WARN("\tfailure - errno = %i\n", errno
);
1775 SetLastError(WSAENOTSOCK
);
1778 SetLastError(WSAEINVAL
);
1781 SetLastError(wsaErrno());
1787 res
=0; /* success */
1791 release_sock_fd( s
, fd
);
1796 /***********************************************************************
1797 * closesocket (WS2_32.3)
1799 int WINAPI
WS_closesocket(SOCKET s
)
1801 TRACE("socket %04lx\n", s
);
1802 if (CloseHandle(SOCKET2HANDLE(s
))) return 0;
1803 return SOCKET_ERROR
;
1806 static int do_connect(int fd
, const struct WS_sockaddr
* name
, int namelen
)
1808 union generic_unix_sockaddr uaddr
;
1809 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
1814 if (name
->sa_family
== WS_AF_INET
)
1816 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
1817 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
1819 /* Trying to connect to magic replace-loopback address,
1820 * assuming we really want to connect to localhost */
1821 TRACE("Trying to connect to magic IP address, using "
1822 "INADDR_LOOPBACK instead.\n");
1823 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_LOOPBACK
);
1827 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
1833 /***********************************************************************
1834 * connect (WS2_32.4)
1836 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
1838 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1840 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
1844 int ret
= do_connect(fd
, name
, namelen
);
1846 goto connect_success
;
1848 if (ret
== WSAEINPROGRESS
)
1850 /* tell wineserver that a connection is in progress */
1851 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
1853 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
1854 if (_is_blocking(s
))
1858 do_block(fd
, POLLIN
| POLLOUT
, -1);
1859 _sync_sock_state(s
); /* let wineserver notice connection */
1860 /* retrieve any error codes from it */
1861 result
= _get_sock_error(s
, FD_CONNECT_BIT
);
1863 SetLastError(NtStatusToWSAError(result
));
1866 goto connect_success
;
1871 SetLastError(WSAEWOULDBLOCK
);
1878 release_sock_fd( s
, fd
);
1880 return SOCKET_ERROR
;
1883 release_sock_fd( s
, fd
);
1884 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
1885 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
1886 FD_CONNECT
|FD_WINE_LISTENING
);
1890 /***********************************************************************
1891 * WSAConnect (WS2_32.30)
1893 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
1894 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
1895 LPQOS lpSQOS
, LPQOS lpGQOS
)
1897 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
1898 FIXME("unsupported parameters!\n");
1899 return WS_connect( s
, name
, namelen
);
1902 /***********************************************************************
1905 static BOOL WINAPI
WS2_ConnectEx(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
1906 PVOID sendBuf
, DWORD sendBufLen
, LPDWORD sent
, LPOVERLAPPED ov
)
1908 int fd
, ret
, status
;
1912 SetLastError( ERROR_INVALID_PARAMETER
);
1916 fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1919 SetLastError( WSAENOTSOCK
);
1923 TRACE("socket %04lx, ptr %p %s, length %d, sendptr %p, len %d, ov %p\n",
1924 s
, name
, debugstr_sockaddr(name
), namelen
, sendBuf
, sendBufLen
, ov
);
1926 /* FIXME: technically the socket has to be bound */
1927 ret
= do_connect(fd
, name
, namelen
);
1932 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
1933 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
1934 FD_CONNECT
|FD_WINE_LISTENING
);
1936 wsabuf
.len
= sendBufLen
;
1937 wsabuf
.buf
= (char*) sendBuf
;
1939 /* WSASend takes care of completion if need be */
1940 if (WSASend(s
, &wsabuf
, sendBuf
? 1 : 0, sent
, 0, ov
, NULL
) != SOCKET_ERROR
)
1941 goto connection_success
;
1943 else if (ret
== WSAEINPROGRESS
)
1945 struct ws2_async
*wsa
;
1946 ULONG_PTR cvalue
= (((ULONG_PTR
)ov
->hEvent
& 1) == 0) ? (ULONG_PTR
)ov
: 0;
1948 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
1950 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
1952 /* Indirectly call WSASend */
1953 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) )))
1955 SetLastError(WSAEFAULT
);
1959 IO_STATUS_BLOCK
*iosb
= (IO_STATUS_BLOCK
*)ov
;
1960 iosb
->u
.Status
= STATUS_PENDING
;
1961 iosb
->Information
= 0;
1963 wsa
->hSocket
= SOCKET2HANDLE(s
);
1965 wsa
->addrlen
.val
= 0;
1967 wsa
->n_iovecs
= sendBuf
? 1 : 0;
1968 wsa
->first_iovec
= 0;
1969 wsa
->completion_func
= NULL
;
1970 wsa
->iovec
[0].iov_base
= sendBuf
;
1971 wsa
->iovec
[0].iov_len
= sendBufLen
;
1973 SERVER_START_REQ( register_async
)
1975 req
->type
= ASYNC_TYPE_WRITE
;
1976 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
1977 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
1978 req
->async
.iosb
= wine_server_client_ptr( iosb
);
1979 req
->async
.arg
= wine_server_client_ptr( wsa
);
1980 req
->async
.event
= wine_server_obj_handle( ov
->hEvent
);
1981 req
->async
.cvalue
= cvalue
;
1982 status
= wine_server_call( req
);
1986 if (status
!= STATUS_PENDING
) HeapFree(GetProcessHeap(), 0, wsa
);
1988 /* If the connect already failed */
1989 if (status
== STATUS_PIPE_DISCONNECTED
)
1990 status
= _get_sock_error(s
, FD_CONNECT_BIT
);
1991 SetLastError( NtStatusToWSAError(status
) );
1999 release_sock_fd( s
, fd
);
2003 release_sock_fd( s
, fd
);
2008 /***********************************************************************
2009 * getpeername (WS2_32.5)
2011 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
2016 TRACE("socket: %04lx, ptr %p, len %08x\n", s
, name
, namelen
?*namelen
:0);
2018 fd
= get_sock_fd( s
, 0, NULL
);
2023 union generic_unix_sockaddr uaddr
;
2024 unsigned int uaddrlen
= sizeof(uaddr
);
2026 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) == 0)
2028 if (!name
|| !namelen
)
2029 SetLastError(WSAEFAULT
);
2030 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
2031 /* The buffer was too small */
2032 SetLastError(WSAEFAULT
);
2037 SetLastError(wsaErrno());
2038 release_sock_fd( s
, fd
);
2043 /***********************************************************************
2044 * getsockname (WS2_32.6)
2046 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
2051 TRACE("socket: %04lx, ptr %p, len %8x\n", s
, name
, *namelen
);
2053 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
2054 if( (name
== NULL
) || (namelen
== NULL
) )
2056 SetLastError( WSAEFAULT
);
2057 return SOCKET_ERROR
;
2060 fd
= get_sock_fd( s
, 0, NULL
);
2065 union generic_unix_sockaddr uaddr
;
2066 unsigned int uaddrlen
= sizeof(uaddr
);
2068 if (getsockname(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
2070 SetLastError(wsaErrno());
2072 else if (!is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
2074 SetLastError(WSAEINVAL
);
2076 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
2078 /* The buffer was too small */
2079 SetLastError(WSAEFAULT
);
2085 release_sock_fd( s
, fd
);
2090 /***********************************************************************
2091 * getsockopt (WS2_32.7)
2093 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
2094 INT optname
, char *optval
, INT
*optlen
)
2099 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
2100 s
, level
, optname
, optval
, *optlen
);
2108 /* Handle common cases. The special cases are below, sorted
2110 case WS_SO_ACCEPTCONN
:
2111 case WS_SO_BROADCAST
:
2114 case WS_SO_KEEPALIVE
:
2115 case WS_SO_OOBINLINE
:
2117 case WS_SO_REUSEADDR
:
2120 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2121 return SOCKET_ERROR
;
2122 convert_sockopt(&level
, &optname
);
2123 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2125 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2128 release_sock_fd( s
, fd
);
2131 case WS_SO_DONTLINGER
:
2133 struct linger lingval
;
2134 unsigned int len
= sizeof(struct linger
);
2136 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
2138 SetLastError(WSAEFAULT
);
2139 return SOCKET_ERROR
;
2141 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2142 return SOCKET_ERROR
;
2144 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
2146 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2151 *(BOOL
*)optval
= (lingval
.l_onoff
) ? FALSE
: TRUE
;
2152 *optlen
= sizeof(BOOL
);
2155 release_sock_fd( s
, fd
);
2159 /* As mentioned in setsockopt, Windows ignores this, so we
2160 * always return true here */
2161 case WS_SO_DONTROUTE
:
2162 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
2164 SetLastError(WSAEFAULT
);
2165 return SOCKET_ERROR
;
2167 *(BOOL
*)optval
= TRUE
;
2168 *optlen
= sizeof(BOOL
);
2173 struct linger lingval
;
2174 unsigned int len
= sizeof(struct linger
);
2176 /* struct linger and LINGER have different sizes */
2177 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
2179 SetLastError(WSAEFAULT
);
2180 return SOCKET_ERROR
;
2182 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2183 return SOCKET_ERROR
;
2185 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
2187 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2192 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
2193 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
2194 *optlen
= sizeof(struct linger
);
2197 release_sock_fd( s
, fd
);
2201 case WS_SO_MAX_MSG_SIZE
:
2202 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
2204 SetLastError(WSAEFAULT
);
2205 return SOCKET_ERROR
;
2207 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
2208 *(int *)optval
= 65507;
2209 *optlen
= sizeof(int);
2212 /* SO_OPENTYPE does not require a valid socket handle. */
2213 case WS_SO_OPENTYPE
:
2214 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
2216 SetLastError(WSAEFAULT
);
2217 return SOCKET_ERROR
;
2219 *(int *)optval
= get_per_thread_data()->opentype
;
2220 *optlen
= sizeof(int);
2221 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
2225 case WS_SO_RCVTIMEO
:
2228 case WS_SO_SNDTIMEO
:
2230 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
2233 unsigned int len
= sizeof(struct timeval
);
2235 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
2237 SetLastError(WSAEFAULT
);
2238 return SOCKET_ERROR
;
2240 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2241 return SOCKET_ERROR
;
2243 convert_sockopt(&level
, &optname
);
2244 if (getsockopt(fd
, level
, optname
, &tv
, &len
) != 0 )
2246 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2251 *(int *)optval
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
2252 *optlen
= sizeof(int);
2255 release_sock_fd( s
, fd
);
2260 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
2261 SetLastError(WSAENOPROTOOPT
);
2262 return SOCKET_ERROR
;
2263 } /* end switch(optname) */
2264 }/* end case WS_SOL_SOCKET */
2268 struct WS_sockaddr_ipx addr
;
2269 IPX_ADDRESS_DATA
*data
;
2274 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
2276 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (unsigned int*)optlen
) == -1)
2283 socklen_t len
=sizeof(struct ipx
);
2284 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
2287 *optval
= (int)val
.ipx_pt
;
2290 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
2291 release_sock_fd( s
, fd
);
2296 * On a Win2000 system with one network card there are usually
2297 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
2298 * Using this call you can then retrieve info about this all.
2299 * In case of Linux it is a bit different. Usually you have
2300 * only "one" device active and further it is not possible to
2301 * query things like the linkspeed.
2303 FIXME("IPX_ADDRESS\n");
2304 namelen
= sizeof(struct WS_sockaddr_ipx
);
2305 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
2306 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
2308 data
= (IPX_ADDRESS_DATA
*)optval
;
2309 memcpy(data
->nodenum
,addr
.sa_nodenum
,sizeof(data
->nodenum
));
2310 memcpy(data
->netnum
,addr
.sa_netnum
,sizeof(data
->netnum
));
2311 data
->adapternum
= 0;
2312 data
->wan
= FALSE
; /* We are not on a wan for now .. */
2313 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
2314 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
2315 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
2316 * note 1MB = 1000kB in this case */
2319 case IPX_MAX_ADAPTER_NUM
:
2320 FIXME("IPX_MAX_ADAPTER_NUM\n");
2321 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
2325 FIXME("IPX optname:%x\n", optname
);
2326 return SOCKET_ERROR
;
2327 }/* end switch(optname) */
2328 } /* end case NSPROTO_IPX */
2335 case WS_IRLMP_ENUMDEVICES
:
2337 static const int MAX_IRDA_DEVICES
= 10;
2338 char buf
[sizeof(struct irda_device_list
) +
2339 (MAX_IRDA_DEVICES
- 1) * sizeof(struct irda_device_info
)];
2341 socklen_t len
= sizeof(buf
);
2343 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2344 return SOCKET_ERROR
;
2345 res
= getsockopt( fd
, SOL_IRLMP
, IRLMP_ENUMDEVICES
, buf
, &len
);
2348 SetLastError(wsaErrno());
2349 return SOCKET_ERROR
;
2353 struct irda_device_list
*src
= (struct irda_device_list
*)buf
;
2354 DEVICELIST
*dst
= (DEVICELIST
*)optval
;
2355 INT needed
= sizeof(DEVICELIST
), i
;
2358 needed
+= (src
->len
- 1) * sizeof(IRDA_DEVICE_INFO
);
2359 if (*optlen
< needed
)
2361 SetLastError(WSAEFAULT
);
2362 return SOCKET_ERROR
;
2365 TRACE("IRLMP_ENUMDEVICES: %d devices found:\n", src
->len
);
2366 dst
->numDevice
= src
->len
;
2367 for (i
= 0; i
< src
->len
; i
++)
2369 TRACE("saddr = %08x, daddr = %08x, info = %s, hints = %02x%02x\n",
2370 src
->dev
[i
].saddr
, src
->dev
[i
].daddr
,
2371 src
->dev
[i
].info
, src
->dev
[i
].hints
[0],
2372 src
->dev
[i
].hints
[1]);
2373 memcpy( dst
->Device
[i
].irdaDeviceID
,
2375 sizeof(dst
->Device
[i
].irdaDeviceID
) ) ;
2376 memcpy( dst
->Device
[i
].irdaDeviceName
,
2378 sizeof(dst
->Device
[i
].irdaDeviceName
) ) ;
2379 memcpy( &dst
->Device
[i
].irdaDeviceHints1
,
2380 &src
->dev
[i
].hints
[0],
2381 sizeof(dst
->Device
[i
].irdaDeviceHints1
) ) ;
2382 memcpy( &dst
->Device
[i
].irdaDeviceHints2
,
2383 &src
->dev
[i
].hints
[1],
2384 sizeof(dst
->Device
[i
].irdaDeviceHints2
) ) ;
2385 dst
->Device
[i
].irdaCharSet
= src
->dev
[i
].charset
;
2391 FIXME("IrDA optname:0x%x\n", optname
);
2392 return SOCKET_ERROR
;
2394 break; /* case WS_SOL_IRLMP */
2397 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
2398 case WS_IPPROTO_TCP
:
2401 case WS_TCP_NODELAY
:
2402 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2403 return SOCKET_ERROR
;
2404 convert_sockopt(&level
, &optname
);
2405 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2407 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2410 release_sock_fd( s
, fd
);
2413 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
2414 return SOCKET_ERROR
;
2419 case WS_IP_ADD_MEMBERSHIP
:
2420 case WS_IP_DROP_MEMBERSHIP
:
2424 case WS_IP_MULTICAST_IF
:
2425 case WS_IP_MULTICAST_LOOP
:
2426 case WS_IP_MULTICAST_TTL
:
2430 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2431 return SOCKET_ERROR
;
2432 convert_sockopt(&level
, &optname
);
2433 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2435 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2438 release_sock_fd( s
, fd
);
2440 case WS_IP_DONTFRAGMENT
:
2441 FIXME("WS_IP_DONTFRAGMENT is always false!\n");
2442 *(BOOL
*)optval
= FALSE
;
2445 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
2446 return SOCKET_ERROR
;
2448 case WS_IPPROTO_IPV6
:
2451 #ifdef IPV6_ADD_MEMBERSHIP
2452 case WS_IPV6_ADD_MEMBERSHIP
:
2454 #ifdef IPV6_DROP_MEMBERSHIP
2455 case WS_IPV6_DROP_MEMBERSHIP
:
2457 case WS_IPV6_MULTICAST_IF
:
2458 case WS_IPV6_MULTICAST_HOPS
:
2459 case WS_IPV6_MULTICAST_LOOP
:
2460 case WS_IPV6_UNICAST_HOPS
:
2461 case WS_IPV6_V6ONLY
:
2462 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2463 return SOCKET_ERROR
;
2464 convert_sockopt(&level
, &optname
);
2465 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2467 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2470 release_sock_fd( s
, fd
);
2472 case WS_IPV6_DONTFRAG
:
2473 FIXME("WS_IPV6_DONTFRAG is always false!\n");
2474 *(BOOL
*)optval
= FALSE
;
2477 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
2478 return SOCKET_ERROR
;
2481 WARN("Unknown level: 0x%08x\n", level
);
2482 SetLastError(WSAEINVAL
);
2483 return SOCKET_ERROR
;
2484 } /* end switch(level) */
2487 /***********************************************************************
2490 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
2492 return htonl(hostlong
);
2496 /***********************************************************************
2499 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
2501 return htons(hostshort
);
2504 /***********************************************************************
2505 * WSAHtonl (WS2_32.46)
2506 * From MSDN description of error codes, this function should also
2507 * check if WinSock has been initialized and the socket is a valid
2508 * socket. But why? This function only translates a host byte order
2509 * u_long into a network byte order u_long...
2511 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
2515 *lpnetlong
= htonl(hostlong
);
2518 WSASetLastError(WSAEFAULT
);
2519 return SOCKET_ERROR
;
2522 /***********************************************************************
2523 * WSAHtons (WS2_32.47)
2524 * From MSDN description of error codes, this function should also
2525 * check if WinSock has been initialized and the socket is a valid
2526 * socket. But why? This function only translates a host byte order
2527 * u_short into a network byte order u_short...
2529 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
2534 *lpnetshort
= htons(hostshort
);
2537 WSASetLastError(WSAEFAULT
);
2538 return SOCKET_ERROR
;
2542 /***********************************************************************
2543 * inet_addr (WS2_32.11)
2545 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
2547 if (!cp
) return INADDR_NONE
;
2548 return inet_addr(cp
);
2552 /***********************************************************************
2555 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
2557 return ntohl(netlong
);
2561 /***********************************************************************
2564 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
2566 return ntohs(netshort
);
2570 /***********************************************************************
2571 * inet_ntoa (WS2_32.12)
2573 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
2575 /* use "buffer for dummies" here because some applications have a
2576 * propensity to decode addresses in ws_hostent structure without
2577 * saving them first...
2579 static char dbuffer
[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
2581 char* s
= inet_ntoa(*((struct in_addr
*)&in
));
2587 SetLastError(wsaErrno());
2591 /**********************************************************************
2592 * WSAIoctl (WS2_32.50)
2595 INT WINAPI
WSAIoctl(SOCKET s
,
2596 DWORD dwIoControlCode
,
2599 LPVOID lpbOutBuffer
,
2601 LPDWORD lpcbBytesReturned
,
2602 LPWSAOVERLAPPED lpOverlapped
,
2603 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2605 TRACE("%ld, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
2606 s
, dwIoControlCode
, lpvInBuffer
, cbInBuffer
, lpbOutBuffer
,
2607 cbOutBuffer
, lpcbBytesReturned
, lpOverlapped
, lpCompletionRoutine
);
2609 switch( dwIoControlCode
)
2612 if (cbInBuffer
!= sizeof(WS_u_long
)) {
2613 WSASetLastError(WSAEFAULT
);
2614 return SOCKET_ERROR
;
2616 return WS_ioctlsocket( s
, WS_FIONBIO
, lpvInBuffer
);
2619 if (cbOutBuffer
!= sizeof(WS_u_long
)) {
2620 WSASetLastError(WSAEFAULT
);
2621 return SOCKET_ERROR
;
2623 return WS_ioctlsocket( s
, WS_FIONREAD
, lpbOutBuffer
);
2625 case WS_SIO_GET_INTERFACE_LIST
:
2627 INTERFACE_INFO
* intArray
= (INTERFACE_INFO
*)lpbOutBuffer
;
2628 DWORD size
, numInt
, apiReturn
;
2631 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
2635 WSASetLastError(WSAEFAULT
);
2636 return SOCKET_ERROR
;
2638 if (!lpcbBytesReturned
)
2640 WSASetLastError(WSAEFAULT
);
2641 return SOCKET_ERROR
;
2644 fd
= get_sock_fd( s
, 0, NULL
);
2645 if (fd
== -1) return SOCKET_ERROR
;
2647 apiReturn
= GetAdaptersInfo(NULL
, &size
);
2648 if (apiReturn
== ERROR_NO_DATA
)
2652 else if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
2654 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
2658 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
2660 PIP_ADAPTER_INFO ptr
;
2662 if (size
*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > cbOutBuffer
)
2664 WARN("Buffer too small = %u, cbOutBuffer = %u\n", size
, cbOutBuffer
);
2665 HeapFree(GetProcessHeap(),0,table
);
2666 release_sock_fd( s
, fd
);
2667 WSASetLastError(WSAEFAULT
);
2668 return SOCKET_ERROR
;
2670 for (ptr
= table
, numInt
= 0; ptr
;
2671 ptr
= ptr
->Next
, intArray
++, numInt
++)
2673 unsigned int addr
, mask
, bcast
;
2674 struct ifreq ifInfo
;
2676 /* Socket Status Flags */
2677 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
2678 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
2680 ERR("Error obtaining status flags for socket!\n");
2681 HeapFree(GetProcessHeap(),0,table
);
2682 release_sock_fd( s
, fd
);
2683 WSASetLastError(WSAEINVAL
);
2684 return SOCKET_ERROR
;
2688 /* set flags; the values of IFF_* are not the same
2689 under Linux and Windows, therefore must generate
2691 intArray
->iiFlags
= 0;
2692 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
2693 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
2694 #ifdef IFF_POINTOPOINT
2695 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
2696 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
2698 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
2699 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
2700 if (ifInfo
.ifr_flags
& IFF_UP
)
2701 intArray
->iiFlags
|= WS_IFF_UP
;
2702 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
2703 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
2706 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
2707 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
2708 bcast
= addr
| ~mask
;
2709 intArray
->iiAddress
.AddressIn
.sin_family
= AF_INET
;
2710 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
2711 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
=
2713 intArray
->iiNetmask
.AddressIn
.sin_family
= AF_INET
;
2714 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
2715 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
=
2717 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
=
2719 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
2720 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.
2726 ERR("Unable to get interface table!\n");
2727 release_sock_fd( s
, fd
);
2728 HeapFree(GetProcessHeap(),0,table
);
2729 WSASetLastError(WSAEINVAL
);
2730 return SOCKET_ERROR
;
2732 HeapFree(GetProcessHeap(),0,table
);
2736 release_sock_fd( s
, fd
);
2737 WSASetLastError(WSAEINVAL
);
2738 return SOCKET_ERROR
;
2743 ERR("Unable to get interface table!\n");
2744 release_sock_fd( s
, fd
);
2745 WSASetLastError(WSAEINVAL
);
2746 return SOCKET_ERROR
;
2748 /* Calculate the size of the array being returned */
2749 *lpcbBytesReturned
= sizeof(INTERFACE_INFO
) * numInt
;
2750 release_sock_fd( s
, fd
);
2754 case WS_SIO_ADDRESS_LIST_CHANGE
:
2755 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
2756 /* FIXME: error and return code depend on whether socket was created
2757 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
2760 case WS_SIO_ADDRESS_LIST_QUERY
:
2764 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
2766 if (!lpcbBytesReturned
)
2768 WSASetLastError(WSAEFAULT
);
2769 return SOCKET_ERROR
;
2772 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
2774 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
2777 if (!table
|| GetAdaptersInfo(table
, &size
))
2779 HeapFree(GetProcessHeap(), 0, table
);
2780 WSASetLastError(WSAEINVAL
);
2781 return SOCKET_ERROR
;
2784 for (p
= table
, num
= 0; p
; p
= p
->Next
)
2785 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
2787 need
= sizeof(SOCKET_ADDRESS_LIST
) + sizeof(SOCKET_ADDRESS
) * (num
- 1);
2788 need
+= sizeof(SOCKADDR
) * num
;
2789 *lpcbBytesReturned
= need
;
2791 if (need
> cbOutBuffer
)
2793 HeapFree(GetProcessHeap(), 0, table
);
2794 WSASetLastError(WSAEFAULT
);
2795 return SOCKET_ERROR
;
2802 SOCKET_ADDRESS_LIST
*sa_list
= (SOCKET_ADDRESS_LIST
*)lpbOutBuffer
;
2803 SOCKADDR_IN
*sockaddr
;
2805 sa
= sa_list
->Address
;
2806 sockaddr
= (SOCKADDR_IN
*)((char *)sa
+ num
* sizeof(SOCKET_ADDRESS
));
2807 sa_list
->iAddressCount
= num
;
2809 for (p
= table
, i
= 0; p
; p
= p
->Next
)
2811 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
2813 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
2814 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
2816 sockaddr
[i
].sin_family
= AF_INET
;
2817 sockaddr
[i
].sin_port
= 0;
2818 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
2823 HeapFree(GetProcessHeap(), 0, table
);
2828 WARN("unable to get IP address list\n");
2829 WSASetLastError(WSAEINVAL
);
2830 return SOCKET_ERROR
;
2834 FIXME("SIO_FLUSH: stub.\n");
2837 case WS_SIO_GET_EXTENSION_FUNCTION_POINTER
:
2839 static const GUID connectex_guid
= WSAID_CONNECTEX
;
2840 static const GUID disconnectex_guid
= WSAID_DISCONNECTEX
;
2841 static const GUID acceptex_guid
= WSAID_ACCEPTEX
;
2842 static const GUID getaccepexsockaddrs_guid
= WSAID_GETACCEPTEXSOCKADDRS
;
2843 static const GUID transmitfile_guid
= WSAID_TRANSMITFILE
;
2844 static const GUID transmitpackets_guid
= WSAID_TRANSMITPACKETS
;
2845 static const GUID wsarecvmsg_guid
= WSAID_WSARECVMSG
;
2846 static const GUID wsasendmsg_guid
= WSAID_WSASENDMSG
;
2848 if ( IsEqualGUID(&connectex_guid
, lpvInBuffer
) )
2850 *(LPFN_CONNECTEX
*)lpbOutBuffer
= WS2_ConnectEx
;
2853 else if ( IsEqualGUID(&disconnectex_guid
, lpvInBuffer
) )
2855 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented DisconnectEx\n");
2857 else if ( IsEqualGUID(&acceptex_guid
, lpvInBuffer
) )
2859 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented AcceptEx\n");
2861 else if ( IsEqualGUID(&getaccepexsockaddrs_guid
, lpvInBuffer
) )
2863 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented GetAcceptExSockaddrs\n");
2865 else if ( IsEqualGUID(&transmitfile_guid
, lpvInBuffer
) )
2867 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitFile\n");
2869 else if ( IsEqualGUID(&transmitpackets_guid
, lpvInBuffer
) )
2871 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented TransmitPackets\n");
2873 else if ( IsEqualGUID(&wsarecvmsg_guid
, lpvInBuffer
) )
2875 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented WSARecvMsg\n");
2877 else if ( IsEqualGUID(&wsasendmsg_guid
, lpvInBuffer
) )
2879 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER: unimplemented WSASendMsg\n");
2882 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER %s: stub\n", debugstr_guid(lpvInBuffer
));
2884 WSASetLastError(WSAEOPNOTSUPP
);
2885 return SOCKET_ERROR
;
2888 case WS_SIO_KEEPALIVE_VALS
:
2891 struct tcp_keepalive
*k
= lpvInBuffer
;
2892 int keepalive
= k
->onoff
? 1 : 0;
2893 int keepidle
= k
->keepalivetime
/ 1000;
2894 int keepintvl
= k
->keepaliveinterval
/ 1000;
2898 WSASetLastError(WSAEINVAL
);
2899 return SOCKET_ERROR
;
2902 TRACE("onoff: %d, keepalivetime: %d, keepaliveinterval: %d\n", keepalive
, keepidle
, keepintvl
);
2904 fd
= get_sock_fd(s
, 0, NULL
);
2905 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&keepalive
, sizeof(int)) == -1)
2907 release_sock_fd(s
, fd
);
2908 WSASetLastError(WSAEINVAL
);
2909 return SOCKET_ERROR
;
2911 #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL)
2912 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, (void *)&keepidle
, sizeof(int)) == -1)
2914 release_sock_fd(s
, fd
);
2915 WSASetLastError(WSAEINVAL
);
2916 return SOCKET_ERROR
;
2918 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, (void *)&keepintvl
, sizeof(int)) == -1)
2920 release_sock_fd(s
, fd
);
2921 WSASetLastError(WSAEINVAL
);
2922 return SOCKET_ERROR
;
2925 FIXME("ignoring keepalive interval and timeout\n");
2928 release_sock_fd(s
, fd
);
2931 case WS_SIO_UDP_CONNRESET
:
2932 FIXME("WS_SIO_UDP_CONNRESET stub\n");
2935 FIXME("unsupported WS_IOCTL cmd (%08x)\n", dwIoControlCode
);
2936 WSASetLastError(WSAEOPNOTSUPP
);
2937 return SOCKET_ERROR
;
2944 /***********************************************************************
2945 * ioctlsocket (WS2_32.10)
2947 int WINAPI
WS_ioctlsocket(SOCKET s
, LONG cmd
, WS_u_long
*argp
)
2952 TRACE("socket %04lx, cmd %08x, ptr %p\n", s
, cmd
, argp
);
2953 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
2954 if(IS_INTRESOURCE(argp
))
2956 SetLastError(WSAEFAULT
);
2957 return SOCKET_ERROR
;
2967 if( _get_sock_mask(s
) )
2969 /* AsyncSelect()'ed sockets are always nonblocking */
2970 if (*argp
) return 0;
2971 SetLastError(WSAEINVAL
);
2972 return SOCKET_ERROR
;
2975 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
2977 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
2985 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
2986 SetLastError(WSAEINVAL
);
2987 return SOCKET_ERROR
;
2989 case SIOCGIFBRDADDR
:
2990 case SIOCGIFNETMASK
:
2992 /* These don't need any special handling. They are used by
2993 WsControl, and are here to suppress an unnecessary warning. */
2997 /* Netscape tries hard to use bogus ioctl 0x667e */
2998 /* FIXME: 0x667e above is ('f' << 8) | 126, and is a low word of
2999 * FIONBIO (_IOW('f', 126, u_long)), how that should be handled?
3001 WARN("\tunknown WS_IOCTL cmd (%08x)\n", cmd
);
3005 fd
= get_sock_fd( s
, 0, NULL
);
3008 if( ioctl(fd
, newcmd
, (char*)argp
) == 0 )
3010 release_sock_fd( s
, fd
);
3013 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
3014 release_sock_fd( s
, fd
);
3016 return SOCKET_ERROR
;
3019 /***********************************************************************
3020 * listen (WS2_32.13)
3022 int WINAPI
WS_listen(SOCKET s
, int backlog
)
3024 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
3026 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
3029 if (listen(fd
, backlog
) == 0)
3031 release_sock_fd( s
, fd
);
3032 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
3034 FD_CONNECT
|FD_WINE_CONNECTED
);
3037 SetLastError(wsaErrno());
3038 release_sock_fd( s
, fd
);
3040 return SOCKET_ERROR
;
3043 /***********************************************************************
3046 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
3048 DWORD n
, dwFlags
= flags
;
3054 if ( WS2_recvfrom(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
3055 return SOCKET_ERROR
;
3060 /***********************************************************************
3061 * recvfrom (WS2_32.17)
3063 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
3064 struct WS_sockaddr
*from
, int *fromlen
)
3066 DWORD n
, dwFlags
= flags
;
3072 if ( WS2_recvfrom(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
) == SOCKET_ERROR
)
3073 return SOCKET_ERROR
;
3078 /* allocate a poll array for the corresponding fd sets */
3079 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
3080 const WS_fd_set
*exceptfds
, int *count_ptr
)
3082 unsigned int i
, j
= 0, count
= 0;
3085 if (readfds
) count
+= readfds
->fd_count
;
3086 if (writefds
) count
+= writefds
->fd_count
;
3087 if (exceptfds
) count
+= exceptfds
->fd_count
;
3089 if (!count
) return NULL
;
3090 if (!(fds
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(fds
[0])))) return NULL
;
3092 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
3094 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
3095 fds
[j
].events
= POLLIN
;
3099 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
3101 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
3102 fds
[j
].events
= POLLOUT
;
3106 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3108 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
3109 fds
[j
].events
= POLLHUP
;
3115 /* release the file descriptor obtained in fd_sets_to_poll */
3116 /* must be called with the original fd_set arrays, before calling get_poll_results */
3117 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
3118 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
3120 unsigned int i
, j
= 0;
3124 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
3125 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
3129 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
3130 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
3134 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3135 if (fds
[j
].fd
!= -1)
3137 /* make sure we have a real error before releasing the fd */
3138 if (!sock_error_p( fds
[j
].fd
)) fds
[j
].revents
= 0;
3139 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
3144 /* map the poll results back into the Windows fd sets */
3145 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
3146 const struct pollfd
*fds
)
3148 unsigned int i
, j
= 0, k
, total
= 0;
3152 for (i
= k
= 0; i
< readfds
->fd_count
; i
++, j
++)
3153 if (fds
[j
].revents
) readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
3154 readfds
->fd_count
= k
;
3159 for (i
= k
= 0; i
< writefds
->fd_count
; i
++, j
++)
3160 if (fds
[j
].revents
) writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
3161 writefds
->fd_count
= k
;
3166 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
3167 if (fds
[j
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
3168 exceptfds
->fd_count
= k
;
3175 /***********************************************************************
3176 * select (WS2_32.18)
3178 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
3179 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
3180 const struct WS_timeval
* ws_timeout
)
3182 struct pollfd
*pollfds
;
3183 struct timeval tv1
, tv2
;
3185 int count
, ret
, timeout
= -1;
3187 TRACE("read %p, write %p, excp %p timeout %p\n",
3188 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
3190 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)) && count
)
3192 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
3193 return SOCKET_ERROR
;
3198 torig
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
3200 gettimeofday( &tv1
, 0 );
3203 while ((ret
= poll( pollfds
, count
, timeout
)) < 0)
3207 if (!ws_timeout
) continue;
3208 gettimeofday( &tv2
, 0 );
3210 tv2
.tv_sec
-= tv1
.tv_sec
;
3211 tv2
.tv_usec
-= tv1
.tv_usec
;
3212 if (tv2
.tv_usec
< 0)
3214 tv2
.tv_usec
+= 1000000;
3218 timeout
= torig
- (tv2
.tv_sec
* 1000) - (tv2
.tv_usec
+ 999) / 1000;
3219 if (timeout
<= 0) break;
3222 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
3224 if (ret
== -1) SetLastError(wsaErrno());
3225 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
3226 HeapFree( GetProcessHeap(), 0, pollfds
);
3230 /* helper to send completion messages for client-only i/o operation case */
3231 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
,
3234 SERVER_START_REQ( add_fd_completion
)
3236 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(sock
) );
3237 req
->cvalue
= CompletionValue
;
3238 req
->status
= CompletionStatus
;
3239 req
->information
= Information
;
3240 wine_server_call( req
);
3246 /***********************************************************************
3249 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
3255 wsabuf
.buf
= (char*) buf
;
3257 if ( WS2_sendto( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
3258 return SOCKET_ERROR
;
3263 /***********************************************************************
3264 * WSASend (WS2_32.72)
3266 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
3267 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
3268 LPWSAOVERLAPPED lpOverlapped
,
3269 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3271 return WS2_sendto( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
3272 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
3275 /***********************************************************************
3276 * WSASendDisconnect (WS2_32.73)
3278 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
3280 return WS_shutdown( s
, SD_SEND
);
3284 static int WS2_sendto( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
3285 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
3286 const struct WS_sockaddr
*to
, int tolen
,
3287 LPWSAOVERLAPPED lpOverlapped
,
3288 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3290 unsigned int i
, options
;
3292 struct ws2_async
*wsa
;
3293 int totalLength
= 0;
3294 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
3296 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
3297 s
, lpBuffers
, dwBufferCount
, dwFlags
,
3298 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
3300 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
3301 TRACE( "fd=%d, options=%x\n", fd
, options
);
3303 if ( fd
== -1 ) return SOCKET_ERROR
;
3305 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
3311 wsa
->hSocket
= SOCKET2HANDLE(s
);
3312 wsa
->addr
= (struct WS_sockaddr
*)to
;
3313 wsa
->addrlen
.val
= tolen
;
3314 wsa
->flags
= dwFlags
;
3315 wsa
->n_iovecs
= dwBufferCount
;
3316 wsa
->first_iovec
= 0;
3317 for ( i
= 0; i
< dwBufferCount
; i
++ )
3319 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
3320 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
3321 totalLength
+= lpBuffers
[i
].len
;
3324 if (!lpNumberOfBytesSent
)
3332 n
= WS2_send( fd
, wsa
);
3333 if (n
!= -1 || errno
!= EINTR
) break;
3335 if (n
== -1 && errno
!= EAGAIN
)
3337 int loc_errno
= errno
;
3339 if (cvalue
) WS_AddCompletion( s
, cvalue
, sock_get_ntstatus(loc_errno
), 0 );
3343 if ((lpOverlapped
|| lpCompletionRoutine
) &&
3344 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
3346 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
3348 wsa
->user_overlapped
= lpOverlapped
;
3349 wsa
->completion_func
= lpCompletionRoutine
;
3350 release_sock_fd( s
, fd
);
3354 iosb
->u
.Status
= STATUS_PENDING
;
3355 iosb
->Information
= 0;
3357 SERVER_START_REQ( register_async
)
3359 req
->type
= ASYNC_TYPE_WRITE
;
3360 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
3361 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
3362 req
->async
.iosb
= wine_server_client_ptr( iosb
);
3363 req
->async
.arg
= wine_server_client_ptr( wsa
);
3364 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
3365 req
->async
.cvalue
= cvalue
;
3366 err
= wine_server_call( req
);
3370 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
3371 WSASetLastError( NtStatusToWSAError( err
));
3372 return SOCKET_ERROR
;
3375 iosb
->u
.Status
= STATUS_SUCCESS
;
3376 iosb
->Information
= n
;
3377 *lpNumberOfBytesSent
= n
;
3378 if (!wsa
->completion_func
)
3380 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
3381 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
3382 HeapFree( GetProcessHeap(), 0, wsa
);
3384 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
3385 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
3390 if ( _is_blocking(s
) )
3392 /* On a blocking non-overlapped stream socket,
3393 * sending blocks until the entire buffer is sent. */
3394 DWORD timeout_start
= GetTickCount();
3396 *lpNumberOfBytesSent
= 0;
3398 while (wsa
->first_iovec
< dwBufferCount
)
3401 int timeout
= GET_SNDTIMEO(fd
);
3405 *lpNumberOfBytesSent
+= n
;
3406 while (wsa
->first_iovec
< dwBufferCount
&& wsa
->iovec
[wsa
->first_iovec
].iov_len
<= n
)
3407 n
-= wsa
->iovec
[wsa
->first_iovec
++].iov_len
;
3408 if (wsa
->first_iovec
>= dwBufferCount
) break;
3409 wsa
->iovec
[wsa
->first_iovec
].iov_base
= (char*)wsa
->iovec
[wsa
->first_iovec
].iov_base
+ n
;
3410 wsa
->iovec
[wsa
->first_iovec
].iov_len
-= n
;
3415 timeout
-= GetTickCount() - timeout_start
;
3416 if (timeout
< 0) timeout
= 0;
3420 pfd
.events
= POLLOUT
;
3422 if (!timeout
|| !poll( &pfd
, 1, timeout
))
3425 goto error
; /* msdn says a timeout in send is fatal */
3428 n
= WS2_send( fd
, wsa
);
3429 if (n
== -1 && errno
!= EAGAIN
&& errno
!= EINTR
)
3436 else /* non-blocking */
3438 if (n
< totalLength
)
3439 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
3442 err
= WSAEWOULDBLOCK
;
3445 *lpNumberOfBytesSent
= n
;
3448 TRACE(" -> %i bytes\n", *lpNumberOfBytesSent
);
3450 HeapFree( GetProcessHeap(), 0, wsa
);
3451 release_sock_fd( s
, fd
);
3456 HeapFree( GetProcessHeap(), 0, wsa
);
3457 release_sock_fd( s
, fd
);
3458 WARN(" -> ERROR %d\n", err
);
3459 WSASetLastError(err
);
3460 return SOCKET_ERROR
;
3463 /***********************************************************************
3464 * WSASendTo (WS2_32.74)
3466 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
3467 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
3468 const struct WS_sockaddr
*to
, int tolen
,
3469 LPWSAOVERLAPPED lpOverlapped
,
3470 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3472 return WS2_sendto( s
, lpBuffers
, dwBufferCount
,
3473 lpNumberOfBytesSent
, dwFlags
,
3475 lpOverlapped
, lpCompletionRoutine
);
3478 /***********************************************************************
3479 * sendto (WS2_32.20)
3481 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
3482 const struct WS_sockaddr
*to
, int tolen
)
3488 wsabuf
.buf
= (char*) buf
;
3490 if ( WS2_sendto(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
3491 return SOCKET_ERROR
;
3496 /***********************************************************************
3497 * setsockopt (WS2_32.21)
3499 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
3500 const char *optval
, int optlen
)
3504 struct linger linger
;
3505 struct timeval tval
;
3507 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
3508 s
, level
, optname
, optval
, optlen
);
3510 /* some broken apps pass the value directly instead of a pointer to it */
3511 if(optlen
&& IS_INTRESOURCE(optval
))
3513 SetLastError(WSAEFAULT
);
3514 return SOCKET_ERROR
;
3522 /* Some options need some conversion before they can be sent to
3523 * setsockopt. The conversions are done here, then they will fall though
3524 * to the general case. Special options that are not passed to
3525 * setsockopt follow below that.*/
3527 case WS_SO_DONTLINGER
:
3528 linger
.l_onoff
= *((const int*)optval
) ? 0: 1;
3529 linger
.l_linger
= 0;
3531 optname
= SO_LINGER
;
3532 optval
= (char*)&linger
;
3533 optlen
= sizeof(struct linger
);
3537 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
3538 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
3539 /* FIXME: what is documented behavior if SO_LINGER optval
3542 optname
= SO_LINGER
;
3543 optval
= (char*)&linger
;
3544 optlen
= sizeof(struct linger
);
3548 if (*(const int*)optval
< 2048)
3550 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
3555 /* The options listed here don't need any special handling. Thanks to
3556 * the conversion happening above, options from there will fall through
3558 case WS_SO_ACCEPTCONN
:
3559 case WS_SO_BROADCAST
:
3561 case WS_SO_KEEPALIVE
:
3562 case WS_SO_OOBINLINE
:
3563 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
3564 * however, using it the BSD way fixes bug 8513 and seems to be what
3565 * most programmers assume, anyway */
3566 case WS_SO_REUSEADDR
:
3569 convert_sockopt(&level
, &optname
);
3572 /* SO_DEBUG is a privileged operation, ignore it. */
3574 TRACE("Ignoring SO_DEBUG\n");
3577 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
3578 * socket. According to MSDN, this option is silently ignored.*/
3579 case WS_SO_DONTROUTE
:
3580 TRACE("Ignoring SO_DONTROUTE\n");
3583 /* Stops two sockets from being bound to the same port. Always happens
3584 * on unix systems, so just drop it. */
3585 case WS_SO_EXCLUSIVEADDRUSE
:
3586 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
3589 /* After a ConnectEx call succeeds, the socket can't be used with half of the
3590 * normal winsock functions on windows. We don't have that problem. */
3591 case WS_SO_UPDATE_CONNECT_CONTEXT
:
3592 TRACE("Ignoring SO_UPDATE_CONNECT_CONTEXT, since our sockets are normal\n");
3595 /* SO_OPENTYPE does not require a valid socket handle. */
3596 case WS_SO_OPENTYPE
:
3597 if (!optlen
|| optlen
< sizeof(int) || !optval
)
3599 SetLastError(WSAEFAULT
);
3600 return SOCKET_ERROR
;
3602 get_per_thread_data()->opentype
= *(const int *)optval
;
3603 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((const int*)optval
) );
3607 case WS_SO_RCVTIMEO
:
3610 case WS_SO_SNDTIMEO
:
3612 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
3613 if (optval
&& optlen
== sizeof(UINT32
)) {
3614 /* WinSock passes milliseconds instead of struct timeval */
3615 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
3616 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
3617 /* min of 500 milliseconds */
3618 if (tval
.tv_sec
== 0 && tval
.tv_usec
&& tval
.tv_usec
< 500000)
3619 tval
.tv_usec
= 500000;
3620 optlen
= sizeof(struct timeval
);
3621 optval
= (char*)&tval
;
3622 } else if (optlen
== sizeof(struct timeval
)) {
3623 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
3625 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
3628 convert_sockopt(&level
, &optname
);
3633 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
3634 SetLastError(WSAENOPROTOOPT
);
3635 return SOCKET_ERROR
;
3637 break; /* case WS_SOL_SOCKET */
3644 fd
= get_sock_fd( s
, 0, NULL
);
3645 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval
, fd
);
3647 /* We try to set the ipx type on ipx socket level. */
3649 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, optlen
) == -1)
3651 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
3652 release_sock_fd( s
, fd
);
3653 return SOCKET_ERROR
;
3658 /* Should we retrieve val using a getsockopt call and then
3659 * set the modified one? */
3660 val
.ipx_pt
= *optval
;
3661 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
3664 release_sock_fd( s
, fd
);
3667 case IPX_FILTERPTYPE
:
3668 /* Sets the receive filter packet type, at the moment we don't support it */
3669 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
3670 /* Returning 0 is better for now than returning a SOCKET_ERROR */
3674 FIXME("opt_name:%x\n", optname
);
3675 return SOCKET_ERROR
;
3677 break; /* case NSPROTO_IPX */
3680 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
3681 case WS_IPPROTO_TCP
:
3684 case WS_TCP_NODELAY
:
3685 convert_sockopt(&level
, &optname
);
3688 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
3689 return SOCKET_ERROR
;
3696 case WS_IP_ADD_MEMBERSHIP
:
3697 case WS_IP_DROP_MEMBERSHIP
:
3701 case WS_IP_MULTICAST_IF
:
3702 case WS_IP_MULTICAST_LOOP
:
3703 case WS_IP_MULTICAST_TTL
:
3707 convert_sockopt(&level
, &optname
);
3709 case WS_IP_DONTFRAGMENT
:
3710 FIXME("IP_DONTFRAGMENT is silently ignored!\n");
3713 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
3714 return SOCKET_ERROR
;
3718 case WS_IPPROTO_IPV6
:
3721 #ifdef IPV6_ADD_MEMBERSHIP
3722 case WS_IPV6_ADD_MEMBERSHIP
:
3724 #ifdef IPV6_DROP_MEMBERSHIP
3725 case WS_IPV6_DROP_MEMBERSHIP
:
3727 case WS_IPV6_MULTICAST_IF
:
3728 case WS_IPV6_MULTICAST_HOPS
:
3729 case WS_IPV6_MULTICAST_LOOP
:
3730 case WS_IPV6_UNICAST_HOPS
:
3731 case WS_IPV6_V6ONLY
:
3732 convert_sockopt(&level
, &optname
);
3734 case WS_IPV6_DONTFRAG
:
3735 FIXME("IPV6_DONTFRAG is silently ignored!\n");
3738 FIXME("Unknown IPPROTO_IPV6 optname 0x%08x\n", optname
);
3739 return SOCKET_ERROR
;
3744 WARN("Unknown level: 0x%08x\n", level
);
3745 SetLastError(WSAEINVAL
);
3746 return SOCKET_ERROR
;
3747 } /* end switch(level) */
3749 /* avoid endianness issues if argument is a 16-bit int */
3750 if (optval
&& optlen
< sizeof(int))
3752 woptval
= *((const INT16
*) optval
);
3753 optval
= (char*) &woptval
;
3756 fd
= get_sock_fd( s
, 0, NULL
);
3757 if (fd
== -1) return SOCKET_ERROR
;
3759 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
3761 release_sock_fd( s
, fd
);
3764 TRACE("Setting socket error, %d\n", wsaErrno());
3765 SetLastError(wsaErrno());
3766 release_sock_fd( s
, fd
);
3768 return SOCKET_ERROR
;
3771 /***********************************************************************
3772 * shutdown (WS2_32.22)
3774 int WINAPI
WS_shutdown(SOCKET s
, int how
)
3776 int fd
, err
= WSAENOTSOCK
;
3777 unsigned int options
, clear_flags
= 0;
3779 fd
= get_sock_fd( s
, 0, &options
);
3780 TRACE("socket %04lx, how %i %x\n", s
, how
, options
);
3783 return SOCKET_ERROR
;
3787 case 0: /* drop receives */
3788 clear_flags
|= FD_READ
;
3790 case 1: /* drop sends */
3791 clear_flags
|= FD_WRITE
;
3793 case 2: /* drop all */
3794 clear_flags
|= FD_READ
|FD_WRITE
;
3796 clear_flags
|= FD_WINE_LISTENING
;
3799 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
3804 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
3807 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
3811 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
3812 if (!err
) err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
3815 if (err
) goto error
;
3817 else /* non-overlapped mode */
3819 if ( shutdown( fd
, how
) )
3826 release_sock_fd( s
, fd
);
3827 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
3828 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
3832 release_sock_fd( s
, fd
);
3833 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
3834 WSASetLastError( err
);
3835 return SOCKET_ERROR
;
3838 /***********************************************************************
3839 * socket (WS2_32.23)
3841 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
3843 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
3845 return WSASocketA( af
, type
, protocol
, NULL
, 0,
3846 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
3850 /***********************************************************************
3851 * gethostbyaddr (WS2_32.51)
3853 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
3855 struct WS_hostent
*retval
= NULL
;
3856 struct hostent
* host
;
3858 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3861 struct hostent hostentry
;
3864 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
3866 int res
= gethostbyaddr_r(addr
, len
, type
,
3867 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
3868 if( res
!= ERANGE
) break;
3870 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
3872 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
3874 EnterCriticalSection( &csWSgetXXXbyYYY
);
3875 host
= gethostbyaddr(addr
, len
, type
);
3876 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
3878 if( host
!= NULL
) retval
= WS_dup_he(host
);
3879 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3880 HeapFree(GetProcessHeap(),0,extrabuf
);
3882 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3884 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
3888 /***********************************************************************
3889 * WS_get_local_ips (INTERNAL)
3891 * Returns the list of local IP addresses by going through the network
3892 * adapters and using the local routing table to sort the addresses
3893 * from highest routing priority to lowest routing priority. This
3894 * functionality is inferred from the description for obtaining local
3895 * IP addresses given in the Knowledge Base Article Q160215.
3897 * Please note that the returned hostent is only freed when the thread
3898 * closes and is replaced if another hostent is requested.
3900 static struct WS_hostent
* WS_get_local_ips( char *hostname
)
3902 int last_metric
, numroutes
= 0, i
, j
;
3903 PIP_ADAPTER_INFO adapters
= NULL
, k
;
3904 struct WS_hostent
*hostlist
= NULL
;
3905 PMIB_IPFORWARDTABLE routes
= NULL
;
3906 struct route
*route_addrs
= NULL
;
3907 DWORD adap_size
, route_size
;
3909 /* Obtain the size of the adapter list and routing table, also allocate memory */
3910 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
3912 if (GetIpForwardTable(NULL
, &route_size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
3914 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
3915 routes
= HeapAlloc(GetProcessHeap(), 0, route_size
);
3916 route_addrs
= HeapAlloc(GetProcessHeap(), 0, 0); /* HeapReAlloc doesn't work on NULL */
3917 if (adapters
== NULL
|| routes
== NULL
|| route_addrs
== NULL
)
3919 /* Obtain the adapter list and the full routing table */
3920 if (GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
3922 if (GetIpForwardTable(routes
, &route_size
, FALSE
) != NO_ERROR
)
3924 /* Store the interface associated with each route */
3925 for (i
= 0; i
< routes
->dwNumEntries
; i
++)
3927 DWORD ifindex
, ifmetric
, exists
= FALSE
;
3929 if (routes
->table
[i
].dwForwardType
!= MIB_IPROUTE_TYPE_DIRECT
)
3931 ifindex
= routes
->table
[i
].dwForwardIfIndex
;
3932 ifmetric
= routes
->table
[i
].dwForwardMetric1
;
3933 /* Only store the lowest valued metric for an interface */
3934 for (j
= 0; j
< numroutes
; j
++)
3936 if (route_addrs
[j
].interface
== ifindex
)
3938 if (route_addrs
[j
].metric
> ifmetric
)
3939 route_addrs
[j
].metric
= ifmetric
;
3945 route_addrs
= HeapReAlloc(GetProcessHeap(), 0, route_addrs
, (numroutes
+1)*sizeof(struct route
));
3946 if (route_addrs
== NULL
)
3947 goto cleanup
; /* Memory allocation error, fail gracefully */
3948 route_addrs
[numroutes
].interface
= ifindex
;
3949 route_addrs
[numroutes
].metric
= ifmetric
;
3950 /* If no IP is found in the next step (for whatever reason)
3951 * then fall back to the magic loopback address.
3953 memcpy(&(route_addrs
[numroutes
].addr
.s_addr
), magic_loopback_addr
, 4);
3957 goto cleanup
; /* No routes, fall back to the Magic IP */
3958 /* Find the IP address associated with each found interface */
3959 for (i
= 0; i
< numroutes
; i
++)
3961 for (k
= adapters
; k
!= NULL
; k
= k
->Next
)
3963 char *ip
= k
->IpAddressList
.IpAddress
.String
;
3965 if (route_addrs
[i
].interface
== k
->Index
)
3966 route_addrs
[i
].addr
.s_addr
= (in_addr_t
) inet_addr(ip
);
3969 /* Allocate a hostent and enough memory for all the IPs,
3970 * including the NULL at the end of the list.
3972 hostlist
= WS_create_he(hostname
, 1, numroutes
+1, TRUE
);
3973 if (hostlist
== NULL
)
3974 goto cleanup
; /* Failed to allocate a hostent for the list of IPs */
3975 hostlist
->h_addr_list
[numroutes
] = NULL
; /* NULL-terminate the address list */
3976 hostlist
->h_aliases
[0] = NULL
; /* NULL-terminate the alias list */
3977 hostlist
->h_addrtype
= AF_INET
;
3978 hostlist
->h_length
= sizeof(struct in_addr
); /* = 4 */
3979 /* Reorder the entries when placing them in the host list, Windows expects
3980 * the IP list in order from highest priority to lowest (the critical thing
3981 * is that most applications expect the first IP to be the default route).
3984 for (i
= 0; i
< numroutes
; i
++)
3986 struct in_addr addr
;
3987 int metric
= 0xFFFF;
3989 memcpy(&addr
, magic_loopback_addr
, 4);
3990 for (j
= 0; j
< numroutes
; j
++)
3992 int this_metric
= route_addrs
[j
].metric
;
3994 if (this_metric
> last_metric
&& this_metric
< metric
)
3996 addr
= route_addrs
[j
].addr
;
3997 metric
= this_metric
;
4000 last_metric
= metric
;
4001 (*(struct in_addr
*) hostlist
->h_addr_list
[i
]) = addr
;
4004 /* Cleanup all allocated memory except the address list,
4005 * the address list is used by the calling app.
4008 HeapFree(GetProcessHeap(), 0, route_addrs
);
4009 HeapFree(GetProcessHeap(), 0, adapters
);
4010 HeapFree(GetProcessHeap(), 0, routes
);
4014 /***********************************************************************
4015 * gethostbyname (WS2_32.52)
4017 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
4019 struct WS_hostent
*retval
= NULL
;
4020 struct hostent
* host
;
4021 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4024 struct hostent hostentry
;
4025 int locerr
= ENOBUFS
;
4028 if( gethostname( hostname
, 100) == -1) {
4029 SetLastError( WSAENOBUFS
); /* appropriate ? */
4032 if( !name
|| !name
[0]) {
4035 /* If the hostname of the local machine is requested then return the
4036 * complete list of local IP addresses */
4037 if(strcmp(name
, hostname
) == 0)
4038 retval
= WS_get_local_ips(hostname
);
4039 /* If any other hostname was requested (or the routing table lookup failed)
4040 * then return the IP found by the host OS */
4043 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4045 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
4047 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
4048 if( res
!= ERANGE
) break;
4050 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
4052 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
4054 EnterCriticalSection( &csWSgetXXXbyYYY
);
4055 host
= gethostbyname(name
);
4056 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
4058 if (host
) retval
= WS_dup_he(host
);
4059 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
4060 HeapFree(GetProcessHeap(),0,extrabuf
);
4062 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4065 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
4066 strcmp(name
, "localhost") != 0)
4068 /* hostname != "localhost" but has loopback address. replace by our
4069 * special address.*/
4070 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
4072 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
4077 /***********************************************************************
4078 * getprotobyname (WS2_32.53)
4080 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
4082 struct WS_protoent
* retval
= NULL
;
4083 #ifdef HAVE_GETPROTOBYNAME
4084 struct protoent
* proto
;
4085 EnterCriticalSection( &csWSgetXXXbyYYY
);
4086 if( (proto
= getprotobyname(name
)) != NULL
)
4088 retval
= WS_dup_pe(proto
);
4091 MESSAGE("protocol %s not found; You might want to add "
4092 "this to /etc/protocols\n", debugstr_a(name
) );
4093 SetLastError(WSANO_DATA
);
4095 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4097 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
4102 /***********************************************************************
4103 * getprotobynumber (WS2_32.54)
4105 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
4107 struct WS_protoent
* retval
= NULL
;
4108 #ifdef HAVE_GETPROTOBYNUMBER
4109 struct protoent
* proto
;
4110 EnterCriticalSection( &csWSgetXXXbyYYY
);
4111 if( (proto
= getprotobynumber(number
)) != NULL
)
4113 retval
= WS_dup_pe(proto
);
4116 MESSAGE("protocol number %d not found; You might want to add "
4117 "this to /etc/protocols\n", number
);
4118 SetLastError(WSANO_DATA
);
4120 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4122 TRACE("%i ret %p\n", number
, retval
);
4127 /***********************************************************************
4128 * getservbyname (WS2_32.55)
4130 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
4132 struct WS_servent
* retval
= NULL
;
4133 struct servent
* serv
;
4135 char *proto_str
= NULL
;
4137 if (!(name_str
= strdup_lower(name
))) return NULL
;
4139 if (proto
&& *proto
)
4141 if (!(proto_str
= strdup_lower(proto
)))
4143 HeapFree( GetProcessHeap(), 0, name_str
);
4148 EnterCriticalSection( &csWSgetXXXbyYYY
);
4149 serv
= getservbyname(name_str
, proto_str
);
4152 retval
= WS_dup_se(serv
);
4154 else SetLastError(WSANO_DATA
);
4155 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4156 HeapFree( GetProcessHeap(), 0, proto_str
);
4157 HeapFree( GetProcessHeap(), 0, name_str
);
4158 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
4162 /***********************************************************************
4163 * freeaddrinfo (WS2_32.@)
4165 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
4168 struct WS_addrinfo
*next
;
4170 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
4171 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
4172 next
= res
->ai_next
;
4173 HeapFree(GetProcessHeap(),0,res
);
4178 /* helper functions for getaddrinfo()/getnameinfo() */
4179 static int convert_aiflag_w2u(int winflags
) {
4183 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
4184 if (ws_aiflag_map
[i
][0] & winflags
) {
4185 unixflags
|= ws_aiflag_map
[i
][1];
4186 winflags
&= ~ws_aiflag_map
[i
][0];
4189 FIXME("Unhandled windows AI_xxx flags %x\n", winflags
);
4193 static int convert_niflag_w2u(int winflags
) {
4197 for (i
=0;i
<sizeof(ws_niflag_map
)/sizeof(ws_niflag_map
[0]);i
++)
4198 if (ws_niflag_map
[i
][0] & winflags
) {
4199 unixflags
|= ws_niflag_map
[i
][1];
4200 winflags
&= ~ws_niflag_map
[i
][0];
4203 FIXME("Unhandled windows NI_xxx flags %x\n", winflags
);
4207 static int convert_aiflag_u2w(int unixflags
) {
4211 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
4212 if (ws_aiflag_map
[i
][1] & unixflags
) {
4213 winflags
|= ws_aiflag_map
[i
][0];
4214 unixflags
&= ~ws_aiflag_map
[i
][1];
4216 if (unixflags
) /* will warn usually */
4217 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags
);
4221 static int convert_eai_u2w(int unixret
) {
4224 for (i
=0;ws_eai_map
[i
][0];i
++)
4225 if (ws_eai_map
[i
][1] == unixret
)
4226 return ws_eai_map
[i
][0];
4230 /***********************************************************************
4231 * getaddrinfo (WS2_32.@)
4233 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
4235 #ifdef HAVE_GETADDRINFO
4236 struct addrinfo
*unixaires
= NULL
;
4238 struct addrinfo unixhints
, *punixhints
= NULL
;
4239 CHAR
*node
= NULL
, *serv
= NULL
;
4242 if (!(node
= strdup_lower(nodename
))) return WSA_NOT_ENOUGH_MEMORY
;
4245 if (!(serv
= strdup_lower(servname
))) {
4246 HeapFree(GetProcessHeap(), 0, node
);
4247 return WSA_NOT_ENOUGH_MEMORY
;
4252 punixhints
= &unixhints
;
4254 memset(&unixhints
, 0, sizeof(unixhints
));
4255 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
4256 if (hints
->ai_family
== 0) /* wildcard, specific to getaddrinfo() */
4257 punixhints
->ai_family
= 0;
4259 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
4260 if (hints
->ai_socktype
== 0) /* wildcard, specific to getaddrinfo() */
4261 punixhints
->ai_socktype
= 0;
4263 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
4264 if (hints
->ai_protocol
== 0) /* wildcard, specific to getaddrinfo() */
4265 punixhints
->ai_protocol
= 0;
4267 punixhints
->ai_protocol
= convert_proto_w2u(hints
->ai_protocol
);
4270 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
4271 result
= getaddrinfo(nodename
, servname
, punixhints
, &unixaires
);
4273 TRACE("%s, %s %p -> %p %d\n", debugstr_a(nodename
), debugstr_a(servname
), hints
, res
, result
);
4275 HeapFree(GetProcessHeap(), 0, node
);
4276 HeapFree(GetProcessHeap(), 0, serv
);
4279 struct addrinfo
*xuai
= unixaires
;
4280 struct WS_addrinfo
**xai
= res
;
4284 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
4290 *xai
= ai
;xai
= &ai
->ai_next
;
4291 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
4292 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
4293 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
4294 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
4295 if (xuai
->ai_canonname
) {
4296 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
4297 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
4298 if (!ai
->ai_canonname
)
4300 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
4302 len
= xuai
->ai_addrlen
;
4303 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
4306 ai
->ai_addrlen
= len
;
4308 int winlen
= ai
->ai_addrlen
;
4310 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
4311 ai
->ai_addrlen
= winlen
;
4315 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
4318 ai
->ai_addrlen
= len
;
4320 xuai
= xuai
->ai_next
;
4322 freeaddrinfo(unixaires
);
4324 result
= convert_eai_u2w(result
);
4330 if (*res
) WS_freeaddrinfo(*res
);
4331 if (unixaires
) freeaddrinfo(unixaires
);
4333 return WSA_NOT_ENOUGH_MEMORY
;
4335 FIXME("getaddrinfo() failed, not found during buildtime.\n");
4340 static struct WS_addrinfoW
*addrinfo_AtoW(const struct WS_addrinfo
*ai
)
4342 struct WS_addrinfoW
*ret
;
4344 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfoW
)))) return NULL
;
4345 ret
->ai_flags
= ai
->ai_flags
;
4346 ret
->ai_family
= ai
->ai_family
;
4347 ret
->ai_socktype
= ai
->ai_socktype
;
4348 ret
->ai_protocol
= ai
->ai_protocol
;
4349 ret
->ai_addrlen
= ai
->ai_addrlen
;
4350 ret
->ai_canonname
= NULL
;
4351 ret
->ai_addr
= NULL
;
4352 ret
->ai_next
= NULL
;
4353 if (ai
->ai_canonname
)
4355 int len
= MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0);
4356 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
4358 HeapFree(GetProcessHeap(), 0, ret
);
4361 MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
);
4365 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
4367 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
4368 HeapFree(GetProcessHeap(), 0, ret
);
4371 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
4376 static struct WS_addrinfoW
*addrinfo_list_AtoW(const struct WS_addrinfo
*info
)
4378 struct WS_addrinfoW
*ret
, *infoW
;
4380 if (!(ret
= infoW
= addrinfo_AtoW(info
))) return NULL
;
4381 while (info
->ai_next
)
4383 if (!(infoW
->ai_next
= addrinfo_AtoW(info
->ai_next
)))
4388 infoW
= infoW
->ai_next
;
4389 info
= info
->ai_next
;
4394 static struct WS_addrinfo
*addrinfo_WtoA(const struct WS_addrinfoW
*ai
)
4396 struct WS_addrinfo
*ret
;
4398 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfo
)))) return NULL
;
4399 ret
->ai_flags
= ai
->ai_flags
;
4400 ret
->ai_family
= ai
->ai_family
;
4401 ret
->ai_socktype
= ai
->ai_socktype
;
4402 ret
->ai_protocol
= ai
->ai_protocol
;
4403 ret
->ai_addrlen
= ai
->ai_addrlen
;
4404 ret
->ai_canonname
= NULL
;
4405 ret
->ai_addr
= NULL
;
4406 ret
->ai_next
= NULL
;
4407 if (ai
->ai_canonname
)
4409 int len
= WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0, NULL
, NULL
);
4410 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
4412 HeapFree(GetProcessHeap(), 0, ret
);
4415 WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
, NULL
, NULL
);
4419 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
4421 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
4422 HeapFree(GetProcessHeap(), 0, ret
);
4425 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
4430 /***********************************************************************
4431 * GetAddrInfoW (WS2_32.@)
4433 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
4436 char *nodenameA
, *servnameA
= NULL
;
4437 struct WS_addrinfo
*resA
, *hintsA
= NULL
;
4439 if (!nodename
) return WSAHOST_NOT_FOUND
;
4441 len
= WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, NULL
, 0, NULL
, NULL
);
4442 if (!(nodenameA
= HeapAlloc(GetProcessHeap(), 0, len
))) return EAI_MEMORY
;
4443 WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, nodenameA
, len
, NULL
, NULL
);
4447 len
= WideCharToMultiByte(CP_ACP
, 0, servname
, -1, NULL
, 0, NULL
, NULL
);
4448 if (!(servnameA
= HeapAlloc(GetProcessHeap(), 0, len
)))
4450 HeapFree(GetProcessHeap(), 0, nodenameA
);
4453 WideCharToMultiByte(CP_ACP
, 0, servname
, -1, servnameA
, len
, NULL
, NULL
);
4456 if (hints
) hintsA
= addrinfo_WtoA(hints
);
4457 ret
= WS_getaddrinfo(nodenameA
, servnameA
, hintsA
, &resA
);
4458 WS_freeaddrinfo(hintsA
);
4462 *res
= addrinfo_list_AtoW(resA
);
4463 WS_freeaddrinfo(resA
);
4466 HeapFree(GetProcessHeap(), 0, nodenameA
);
4467 HeapFree(GetProcessHeap(), 0, servnameA
);
4471 /***********************************************************************
4472 * FreeAddrInfoW (WS2_32.@)
4474 void WINAPI
FreeAddrInfoW(PADDRINFOW ai
)
4479 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
4480 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
4482 HeapFree(GetProcessHeap(), 0, ai
);
4487 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
4488 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
4490 #ifdef HAVE_GETNAMEINFO
4492 union generic_unix_sockaddr sa_u
;
4495 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
4496 serv
, servlen
, flags
);
4498 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
4501 WSASetLastError(WSAEFAULT
);
4502 return WSA_NOT_ENOUGH_MEMORY
;
4504 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
4505 return convert_eai_u2w(ret
);
4507 FIXME("getnameinfo() failed, not found during buildtime.\n");
4512 /***********************************************************************
4513 * getservbyport (WS2_32.56)
4515 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
4517 struct WS_servent
* retval
= NULL
;
4518 #ifdef HAVE_GETSERVBYPORT
4519 struct servent
* serv
;
4520 char *proto_str
= NULL
;
4522 if (proto
&& *proto
)
4524 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
4526 EnterCriticalSection( &csWSgetXXXbyYYY
);
4527 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
4528 retval
= WS_dup_se(serv
);
4530 else SetLastError(WSANO_DATA
);
4531 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4532 HeapFree( GetProcessHeap(), 0, proto_str
);
4534 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
4539 /***********************************************************************
4540 * gethostname (WS2_32.57)
4542 int WINAPI
WS_gethostname(char *name
, int namelen
)
4544 TRACE("name %p, len %d\n", name
, namelen
);
4546 if (gethostname(name
, namelen
) == 0)
4548 TRACE("<- '%s'\n", name
);
4551 SetLastError((errno
== EINVAL
) ? WSAEFAULT
: wsaErrno());
4552 TRACE("<- ERROR !\n");
4553 return SOCKET_ERROR
;
4557 /* ------------------------------------- Windows sockets extensions -- *
4559 * ------------------------------------------------------------------- */
4561 /***********************************************************************
4562 * WSAEnumNetworkEvents (WS2_32.36)
4564 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
4568 int errors
[FD_MAX_EVENTS
];
4570 TRACE("%08lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
4572 SERVER_START_REQ( get_socket_event
)
4574 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
4575 req
->service
= TRUE
;
4576 req
->c_event
= wine_server_obj_handle( hEvent
);
4577 wine_server_set_reply( req
, errors
, sizeof(errors
) );
4578 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
4583 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
4584 lpEvent
->iErrorCode
[i
] = NtStatusToWSAError(errors
[i
]);
4587 SetLastError(WSAEINVAL
);
4588 return SOCKET_ERROR
;
4591 /***********************************************************************
4592 * WSAEventSelect (WS2_32.39)
4594 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
4598 TRACE("%08lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
4600 SERVER_START_REQ( set_socket_event
)
4602 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
4604 req
->event
= wine_server_obj_handle( hEvent
);
4607 ret
= wine_server_call( req
);
4611 SetLastError(WSAEINVAL
);
4612 return SOCKET_ERROR
;
4615 /**********************************************************************
4616 * WSAGetOverlappedResult (WS2_32.40)
4618 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
4619 LPDWORD lpcbTransfer
, BOOL fWait
,
4624 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
4625 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
4627 if ( lpOverlapped
== NULL
)
4629 ERR( "Invalid pointer\n" );
4630 WSASetLastError(WSA_INVALID_PARAMETER
);
4634 status
= lpOverlapped
->Internal
;
4635 if (status
== STATUS_PENDING
)
4639 SetLastError( WSA_IO_INCOMPLETE
);
4643 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
4644 INFINITE
) == WAIT_FAILED
)
4646 status
= lpOverlapped
->Internal
;
4650 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
4653 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
4655 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
4660 /***********************************************************************
4661 * WSAAsyncSelect (WS2_32.101)
4663 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
4667 TRACE("%lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
4669 SERVER_START_REQ( set_socket_event
)
4671 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
4674 req
->window
= wine_server_user_handle( hWnd
);
4676 ret
= wine_server_call( req
);
4680 SetLastError(WSAEINVAL
);
4681 return SOCKET_ERROR
;
4684 /***********************************************************************
4685 * WSACreateEvent (WS2_32.31)
4688 WSAEVENT WINAPI
WSACreateEvent(void)
4690 /* Create a manual-reset event, with initial state: unsignaled */
4693 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
4696 /***********************************************************************
4697 * WSACloseEvent (WS2_32.29)
4700 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
4702 TRACE ("event=%p\n", event
);
4704 return CloseHandle(event
);
4707 /***********************************************************************
4708 * WSASocketA (WS2_32.78)
4711 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
4712 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
4713 GROUP g
, DWORD dwFlags
)
4716 WSAPROTOCOL_INFOW info
;
4718 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
4719 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
4721 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
4723 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
4724 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
4725 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
4729 WSASetLastError( WSAEINVAL
);
4730 return SOCKET_ERROR
;
4733 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
4736 /***********************************************************************
4737 * WSASocketW (WS2_32.79)
4740 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
4741 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
4742 GROUP g
, DWORD dwFlags
)
4747 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
4748 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
4751 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
4752 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
4754 /* hack for WSADuplicateSocket */
4755 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
4756 ret
= lpProtocolInfo
->dwCatalogEntryId
;
4757 TRACE("\tgot duplicate %04lx\n", ret
);
4761 /* convert the socket family and type */
4762 af
= convert_af_w2u(af
);
4763 type
= convert_socktype_w2u(type
);
4767 if (af
== FROM_PROTOCOL_INFO
)
4768 af
= lpProtocolInfo
->iAddressFamily
;
4769 if (type
== FROM_PROTOCOL_INFO
)
4770 type
= lpProtocolInfo
->iSocketType
;
4771 if (protocol
== FROM_PROTOCOL_INFO
)
4772 protocol
= lpProtocolInfo
->iProtocol
;
4775 if ( af
== AF_UNSPEC
) /* did they not specify the address family? */
4779 if (type
== SOCK_STREAM
) { af
= AF_INET
; break; }
4781 if (type
== SOCK_DGRAM
) { af
= AF_INET
; break; }
4782 default: SetLastError(WSAEPROTOTYPE
); return INVALID_SOCKET
;
4785 SERVER_START_REQ( create_socket
)
4789 req
->protocol
= protocol
;
4790 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
4791 req
->attributes
= OBJ_INHERIT
;
4792 req
->flags
= dwFlags
;
4793 set_error( wine_server_call( req
) );
4794 ret
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
4799 TRACE("\tcreated %04lx\n", ret
);
4803 if (GetLastError() == WSAEACCES
) /* raw socket denied */
4805 if (type
== SOCK_RAW
)
4806 ERR_(winediag
)("Failed to create a socket of type SOCK_RAW, this requires special permissions.\n");
4808 ERR_(winediag
)("Failed to create socket, this requires special permissions.\n");
4809 SetLastError(WSAESOCKTNOSUPPORT
);
4812 WARN("\t\tfailed!\n");
4813 return INVALID_SOCKET
;
4816 /***********************************************************************
4817 * WSAJoinLeaf (WS2_32.58)
4820 SOCKET WINAPI
WSAJoinLeaf(
4822 const struct WS_sockaddr
*addr
,
4824 LPWSABUF lpCallerData
,
4825 LPWSABUF lpCalleeData
,
4831 return INVALID_SOCKET
;
4834 /***********************************************************************
4835 * __WSAFDIsSet (WS2_32.151)
4837 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
4839 int i
= set
->fd_count
;
4841 TRACE("(%ld,%p(%i))\n", s
, set
, i
);
4844 if (set
->fd_array
[i
] == s
) return 1;
4848 /***********************************************************************
4849 * WSAIsBlocking (WS2_32.114)
4851 BOOL WINAPI
WSAIsBlocking(void)
4853 /* By default WinSock should set all its sockets to non-blocking mode
4854 * and poll in PeekMessage loop when processing "blocking" ones. This
4855 * function is supposed to tell if the program is in this loop. Our
4856 * blocking calls are truly blocking so we always return FALSE.
4858 * Note: It is allowed to call this function without prior WSAStartup().
4865 /***********************************************************************
4866 * WSACancelBlockingCall (WS2_32.113)
4868 INT WINAPI
WSACancelBlockingCall(void)
4874 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
4876 FIXME("How was this called?\n");
4881 /***********************************************************************
4882 * WSASetBlockingHook (WS2_32.109)
4884 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
4886 FARPROC prev
= blocking_hook
;
4887 blocking_hook
= lpBlockFunc
;
4888 TRACE("hook %p\n", lpBlockFunc
);
4893 /***********************************************************************
4894 * WSAUnhookBlockingHook (WS2_32.110)
4896 INT WINAPI
WSAUnhookBlockingHook(void)
4898 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
4903 /* ----------------------------------- end of API stuff */
4905 /* ----------------------------------- helper functions -
4907 * TODO: Merge WS_dup_..() stuff into one function that
4908 * would operate with a generic structure containing internal
4909 * pointers (via a template of some kind).
4912 static int list_size(char** l
, int item_size
)
4917 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
4918 j
+= (i
+ 1) * sizeof(char*); }
4922 static int list_dup(char** l_src
, char** l_to
, int item_size
)
4927 for (i
= 0; l_src
[i
]; i
++) ;
4928 p
= (char *)(l_to
+ i
+ 1);
4929 for (i
= 0; l_src
[i
]; i
++)
4931 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
4932 memcpy(p
, l_src
[i
], count
);
4937 return p
- (char *)l_to
;
4942 /* create a hostent entry
4944 * Creates the entry with enough memory for the name, aliases
4945 * addresses, and the address pointers. Also copies the name
4946 * and sets up all the pointers. If "fill_addresses" is set then
4947 * sufficient memory for the addresses is also allocated and the
4948 * address pointers are set to this memory.
4950 * NOTE: The alias and address lists must be allocated with room
4951 * for the NULL item terminating the list. This is true even if
4952 * the list has no items ("aliases" and "addresses" must be
4953 * at least "1", a truly empty list is invalid).
4955 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int addresses
, int fill_addresses
)
4957 struct WS_hostent
*p_to
;
4960 int size
= (sizeof(struct WS_hostent
) +
4962 sizeof(char *)*aliases
+
4963 sizeof(char *)*addresses
);
4965 /* Allocate enough memory for the addresses */
4967 size
+= sizeof(struct in_addr
)*addresses
;
4969 if (!(p_to
= check_buffer_he(size
))) return NULL
;
4970 memset(p_to
, 0, size
);
4972 p
= (char *)(p_to
+ 1);
4977 p_to
->h_aliases
= (char **)p
;
4978 p
+= sizeof(char *)*aliases
;
4979 p_to
->h_addr_list
= (char **)p
;
4980 p
+= sizeof(char *)*addresses
;
4985 /* NOTE: h_aliases must be filled in manually, leave these
4986 * pointers NULL (already set to NULL by memset earlier).
4989 /* Fill in the list of address pointers */
4990 for (i
= 0; i
< addresses
; i
++)
4991 p_to
->h_addr_list
[i
] = (p
+= sizeof(struct in_addr
));
4992 p
+= sizeof(struct in_addr
);
4997 /* duplicate hostent entry
4998 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
4999 * Ditto for protoent and servent.
5001 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
5003 int addresses
= list_size(p_he
->h_addr_list
, p_he
->h_length
);
5004 int aliases
= list_size(p_he
->h_aliases
, 0);
5005 struct WS_hostent
*p_to
;
5007 p_to
= WS_create_he(p_he
->h_name
, aliases
, addresses
, FALSE
);
5009 if (!p_to
) return NULL
;
5010 p_to
->h_addrtype
= p_he
->h_addrtype
;
5011 p_to
->h_length
= p_he
->h_length
;
5013 list_dup(p_he
->h_aliases
, p_to
->h_aliases
, 0);
5014 list_dup(p_he
->h_addr_list
, p_to
->h_addr_list
, p_he
->h_length
);
5018 /* ----- protoent */
5020 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
)
5023 struct WS_protoent
*p_to
;
5025 int size
= (sizeof(*p_pe
) +
5026 strlen(p_pe
->p_name
) + 1 +
5027 list_size(p_pe
->p_aliases
, 0));
5029 if (!(p_to
= check_buffer_pe(size
))) return NULL
;
5030 p_to
->p_proto
= p_pe
->p_proto
;
5032 p
= (char *)(p_to
+ 1);
5034 strcpy(p
, p_pe
->p_name
);
5037 p_to
->p_aliases
= (char **)p
;
5038 list_dup(p_pe
->p_aliases
, p_to
->p_aliases
, 0);
5044 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
5047 struct WS_servent
*p_to
;
5049 int size
= (sizeof(*p_se
) +
5050 strlen(p_se
->s_proto
) + 1 +
5051 strlen(p_se
->s_name
) + 1 +
5052 list_size(p_se
->s_aliases
, 0));
5054 if (!(p_to
= check_buffer_se(size
))) return NULL
;
5055 p_to
->s_port
= p_se
->s_port
;
5057 p
= (char *)(p_to
+ 1);
5059 strcpy(p
, p_se
->s_name
);
5063 strcpy(p
, p_se
->s_proto
);
5066 p_to
->s_aliases
= (char **)p
;
5067 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
5072 /***********************************************************************
5073 * WSARecv (WS2_32.67)
5075 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5076 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
5077 LPWSAOVERLAPPED lpOverlapped
,
5078 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5080 return WS2_recvfrom(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
5081 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
);
5084 static int WS2_recvfrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5085 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
5086 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
5087 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5090 unsigned int i
, options
;
5092 struct ws2_async
*wsa
;
5093 DWORD timeout_start
= GetTickCount();
5094 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
5096 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
5097 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
5098 (lpFromlen
? *lpFromlen
: -1),
5099 lpOverlapped
, lpCompletionRoutine
);
5101 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
5102 TRACE( "fd=%d, options=%x\n", fd
, options
);
5104 if (fd
== -1) return SOCKET_ERROR
;
5106 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
5112 wsa
->hSocket
= SOCKET2HANDLE(s
);
5113 wsa
->flags
= *lpFlags
;
5115 wsa
->addrlen
.ptr
= lpFromlen
;
5116 wsa
->n_iovecs
= dwBufferCount
;
5117 wsa
->first_iovec
= 0;
5118 for (i
= 0; i
< dwBufferCount
; i
++)
5120 /* check buffer first to trigger write watches */
5121 if (IsBadWritePtr( lpBuffers
[i
].buf
, lpBuffers
[i
].len
))
5126 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
5127 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
5132 n
= WS2_recv( fd
, wsa
);
5135 if (errno
== EINTR
) continue;
5136 if (errno
!= EAGAIN
)
5138 int loc_errno
= errno
;
5140 if (cvalue
) WS_AddCompletion( s
, cvalue
, sock_get_ntstatus(loc_errno
), 0 );
5145 *lpNumberOfBytesRecvd
= n
;
5147 if ((lpOverlapped
|| lpCompletionRoutine
) &&
5148 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
5150 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
5152 wsa
->user_overlapped
= lpOverlapped
;
5153 wsa
->completion_func
= lpCompletionRoutine
;
5154 release_sock_fd( s
, fd
);
5158 iosb
->u
.Status
= STATUS_PENDING
;
5159 iosb
->Information
= 0;
5161 SERVER_START_REQ( register_async
)
5163 req
->type
= ASYNC_TYPE_READ
;
5164 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
5165 req
->async
.callback
= wine_server_client_ptr( WS2_async_recv
);
5166 req
->async
.iosb
= wine_server_client_ptr( iosb
);
5167 req
->async
.arg
= wine_server_client_ptr( wsa
);
5168 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
5169 req
->async
.cvalue
= cvalue
;
5170 err
= wine_server_call( req
);
5174 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
5175 WSASetLastError( NtStatusToWSAError( err
));
5176 return SOCKET_ERROR
;
5179 iosb
->u
.Status
= STATUS_SUCCESS
;
5180 iosb
->Information
= n
;
5181 if (!wsa
->completion_func
)
5183 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
5184 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
5185 HeapFree( GetProcessHeap(), 0, wsa
);
5187 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
5188 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
5189 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5195 if ( _is_blocking(s
) )
5198 int timeout
= GET_RCVTIMEO(fd
);
5201 timeout
-= GetTickCount() - timeout_start
;
5202 if (timeout
< 0) timeout
= 0;
5206 pfd
.events
= POLLIN
;
5207 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
5209 if (!timeout
|| !poll( &pfd
, 1, timeout
))
5212 /* a timeout is not fatal */
5213 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5219 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5220 err
= WSAEWOULDBLOCK
;
5225 TRACE(" -> %i bytes\n", n
);
5226 HeapFree( GetProcessHeap(), 0, wsa
);
5227 release_sock_fd( s
, fd
);
5228 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
5233 HeapFree( GetProcessHeap(), 0, wsa
);
5234 release_sock_fd( s
, fd
);
5235 WARN(" -> ERROR %d\n", err
);
5236 WSASetLastError( err
);
5237 return SOCKET_ERROR
;
5240 /***********************************************************************
5241 * WSARecvFrom (WS2_32.69)
5243 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
5244 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
5245 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
5246 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
5249 return WS2_recvfrom( s
, lpBuffers
, dwBufferCount
,
5250 lpNumberOfBytesRecvd
, lpFlags
,
5252 lpOverlapped
, lpCompletionRoutine
);
5255 /***********************************************************************
5256 * WSCInstallProvider (WS2_32.88)
5258 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
5259 LPCWSTR lpszProviderDllPath
,
5260 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
5261 DWORD dwNumberOfEntries
,
5264 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
5265 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
5266 dwNumberOfEntries
, lpErrno
);
5272 /***********************************************************************
5273 * WSCDeinstallProvider (WS2_32.83)
5275 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
5277 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
5283 /***********************************************************************
5284 * WSAAccept (WS2_32.26)
5286 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
5287 LPCONDITIONPROC lpfnCondition
, DWORD dwCallbackData
)
5290 int ret
= 0, size
= 0;
5291 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
5292 /* QOS SQOS, GQOS; */
5295 SOCKADDR src_addr
, dst_addr
;
5297 TRACE("Socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %d\n",
5298 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
5301 size
= sizeof(src_addr
);
5302 cs
= WS_accept(s
, &src_addr
, &size
);
5304 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
5306 if (!lpfnCondition
) return cs
;
5308 CallerId
.buf
= (char *)&src_addr
;
5309 CallerId
.len
= sizeof(src_addr
);
5311 CallerData
.buf
= NULL
;
5314 WS_getsockname(cs
, &dst_addr
, &size
);
5316 CalleeId
.buf
= (char *)&dst_addr
;
5317 CalleeId
.len
= sizeof(dst_addr
);
5320 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
5321 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
5326 if (addr
&& addrlen
)
5327 addr
= memcpy(addr
, &src_addr
, (*addrlen
> size
) ? size
: *addrlen
);
5330 SERVER_START_REQ( set_socket_deferred
)
5332 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
5333 req
->deferred
= wine_server_obj_handle( SOCKET2HANDLE(cs
) );
5334 if ( !wine_server_call_err ( req
) )
5336 SetLastError( WSATRY_AGAIN
);
5337 WS_closesocket( cs
);
5341 return SOCKET_ERROR
;
5344 SetLastError(WSAECONNREFUSED
);
5345 return SOCKET_ERROR
;
5347 FIXME("Unknown return type from Condition function\n");
5348 SetLastError(WSAENOTSOCK
);
5349 return SOCKET_ERROR
;
5353 /***********************************************************************
5354 * WSADuplicateSocketA (WS2_32.32)
5356 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
5360 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
5361 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
5362 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
5363 /* I don't know what the real Windoze does next, this is a hack */
5364 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
5365 * the target use the global duplicate, or we could copy a reference to us to the structure
5366 * and let the target duplicate it from us, but let's do it as simple as possible */
5367 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
5368 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
5369 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
5370 0, FALSE
, DUPLICATE_SAME_ACCESS
);
5371 CloseHandle(hProcess
);
5372 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
5376 /***********************************************************************
5377 * WSADuplicateSocketW (WS2_32.33)
5379 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
5383 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
5385 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
5386 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
5387 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
5388 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
5389 0, FALSE
, DUPLICATE_SAME_ACCESS
);
5390 CloseHandle(hProcess
);
5391 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
5395 /***********************************************************************
5396 * WSAInstallServiceClassA (WS2_32.48)
5398 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
5400 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
5401 WSASetLastError(WSAEACCES
);
5402 return SOCKET_ERROR
;
5405 /***********************************************************************
5406 * WSAInstallServiceClassW (WS2_32.49)
5408 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
5410 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
5411 WSASetLastError(WSAEACCES
);
5412 return SOCKET_ERROR
;
5415 /***********************************************************************
5416 * WSARemoveServiceClass (WS2_32.70)
5418 int WINAPI
WSARemoveServiceClass(LPGUID info
)
5420 FIXME("Request to remove service %p\n",info
);
5421 WSASetLastError(WSATYPE_NOT_FOUND
);
5422 return SOCKET_ERROR
;
5425 /***********************************************************************
5426 * inet_ntop (WS2_32.@)
5428 PCSTR WINAPI
WS_inet_ntop( INT family
, PVOID addr
, PSTR buffer
, SIZE_T len
)
5430 #ifdef HAVE_INET_NTOP
5431 struct WS_in6_addr
*in6
;
5432 struct WS_in_addr
*in
;
5435 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
5438 WSASetLastError( STATUS_INVALID_PARAMETER
);
5447 pdst
= inet_ntop( AF_INET
, &in
->WS_s_addr
, buffer
, len
);
5453 pdst
= inet_ntop( AF_INET6
, in6
->WS_s6_addr
, buffer
, len
);
5457 WSASetLastError( WSAEAFNOSUPPORT
);
5461 if (!pdst
) WSASetLastError( STATUS_INVALID_PARAMETER
);
5464 FIXME( "not supported on this platform\n" );
5465 WSASetLastError( WSAEAFNOSUPPORT
);
5470 /***********************************************************************
5471 * WSAStringToAddressA (WS2_32.80)
5473 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
5475 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
5476 LPSOCKADDR lpAddress
,
5477 LPINT lpAddressLength
)
5480 LPSTR workBuffer
=NULL
,ptrPort
;
5482 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_a(AddressString
), AddressFamily
,
5483 lpProtocolInfo
, lpAddress
, lpAddressLength
);
5485 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
5489 WSASetLastError(WSAEINVAL
);
5490 return SOCKET_ERROR
;
5494 FIXME("ProtocolInfo not implemented.\n");
5496 workBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
5497 strlen(AddressString
) + 1);
5500 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
5501 return SOCKET_ERROR
;
5504 strcpy(workBuffer
, AddressString
);
5506 switch(AddressFamily
)
5510 struct in_addr inetaddr
;
5512 /* If lpAddressLength is too small, tell caller the size we need */
5513 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
5515 *lpAddressLength
= sizeof(SOCKADDR_IN
);
5519 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
5521 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= AF_INET
;
5523 ptrPort
= strchr(workBuffer
, ':');
5526 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= htons(atoi(ptrPort
+1));
5531 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= 0;
5534 if(inet_aton(workBuffer
, &inetaddr
) > 0)
5536 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
5547 struct in6_addr inetaddr
;
5548 /* If lpAddressLength is too small, tell caller the size we need */
5549 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
5551 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
5555 #ifdef HAVE_INET_PTON
5556 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
5558 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_family
= WS_AF_INET6
;
5560 /* This one is a bit tricky. An IPv6 address contains colons, so the
5561 * check from IPv4 doesn't work like that. However, IPv6 addresses that
5562 * contain a port are written with braces like [fd12:3456:7890::1]:12345
5563 * so what we will do is to look for ']', check if the next char is a
5564 * colon, and if it is, parse the port as in IPv4. */
5566 ptrPort
= strchr(workBuffer
, ']');
5567 if(ptrPort
&& *(++ptrPort
) == ':')
5569 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= htons(atoi(ptrPort
+1));
5574 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= 0;
5577 if(inet_pton(AF_INET6
, workBuffer
, &inetaddr
) > 0)
5579 memcpy(&((LPSOCKADDR_IN6
)lpAddress
)->sin6_addr
, &inetaddr
,
5580 sizeof(struct in6_addr
));
5584 #endif /* HAVE_INET_PTON */
5590 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
5591 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
5595 HeapFree(GetProcessHeap(), 0, workBuffer
);
5598 WSASetLastError(res
);
5599 return SOCKET_ERROR
;
5602 /***********************************************************************
5603 * WSAStringToAddressW (WS2_32.81)
5605 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
5606 * If this should be the case, it would be required to map these digits
5607 * to Unicode digits (0-9) using FoldString first.
5609 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
5611 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
5612 LPSOCKADDR lpAddress
,
5613 LPINT lpAddressLength
)
5616 LPSTR workBuffer
=NULL
;
5617 WSAPROTOCOL_INFOA infoA
;
5618 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
5620 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
5621 lpAddress
, lpAddressLength
);
5623 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
5625 /* if ProtocolInfo is available - convert to ANSI variant */
5628 lpProtoInfoA
= &infoA
;
5629 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
5631 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
5632 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
5634 WSASetLastError( WSAEINVAL
);
5635 return SOCKET_ERROR
;
5641 /* Translate AddressString to ANSI code page - assumes that only
5642 standard digits 0-9 are used with this API call */
5643 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
5644 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
5648 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
5649 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
5650 lpAddress
,lpAddressLength
);
5651 HeapFree( GetProcessHeap(), 0, workBuffer
);
5655 res
= WSA_NOT_ENOUGH_MEMORY
;
5660 WSASetLastError(res
);
5661 return SOCKET_ERROR
;
5664 /***********************************************************************
5665 * WSAAddressToStringA (WS2_32.27)
5667 * See WSAAddressToStringW
5669 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
5670 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
5674 CHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
5677 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
5679 if (!sockaddr
) return SOCKET_ERROR
;
5680 if (!string
|| !lenstr
) return SOCKET_ERROR
;
5682 switch(sockaddr
->sa_family
)
5685 if (len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
5686 sprintf( buffer
, "%u.%u.%u.%u:%u",
5687 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
5688 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
5689 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
5690 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
5691 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
5693 p
= strchr( buffer
, ':' );
5694 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
5699 struct WS_sockaddr_in6
*sockaddr6
= (LPSOCKADDR_IN6
) sockaddr
;
5702 if (len
< sizeof(SOCKADDR_IN6
)) return SOCKET_ERROR
;
5703 if ((sockaddr6
->sin6_port
))
5704 strcpy(buffer
, "[");
5705 if (!WS_inet_ntop(WS_AF_INET6
, &sockaddr6
->sin6_addr
, buffer
+strlen(buffer
), sizeof(buffer
)))
5707 WSASetLastError(WSAEINVAL
);
5708 return SOCKET_ERROR
;
5710 if ((sockaddr6
->sin6_scope_id
))
5711 sprintf(buffer
+strlen(buffer
), "%%%u", sockaddr6
->sin6_scope_id
);
5712 if ((sockaddr6
->sin6_port
))
5713 sprintf(buffer
+strlen(buffer
), "]:%u", ntohs(sockaddr6
->sin6_port
));
5718 WSASetLastError(WSAEINVAL
);
5719 return SOCKET_ERROR
;
5722 size
= strlen( buffer
) + 1;
5727 WSASetLastError(WSAEFAULT
);
5728 return SOCKET_ERROR
;
5732 strcpy( string
, buffer
);
5736 /***********************************************************************
5737 * WSAAddressToStringW (WS2_32.28)
5739 * Convert a sockaddr address into a readable address string.
5742 * sockaddr [I] Pointer to a sockaddr structure.
5743 * len [I] Size of the sockaddr structure.
5744 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
5745 * string [I/O] Pointer to a buffer to receive the address string.
5746 * lenstr [I/O] Size of the receive buffer in WCHARs.
5750 * Failure: SOCKET_ERROR
5753 * The 'info' parameter is ignored.
5755 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
5756 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
5761 WCHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
5764 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
5767 ret
= WSAAddressToStringA(sockaddr
, len
, NULL
, bufAddr
, &size
);
5769 if (ret
) return ret
;
5771 MultiByteToWideChar( CP_ACP
, 0, bufAddr
, size
, buffer
, sizeof( buffer
)/sizeof(WCHAR
));
5776 WSASetLastError(WSAEFAULT
);
5777 return SOCKET_ERROR
;
5781 lstrcpyW( string
, buffer
);
5785 /***********************************************************************
5786 * WSAEnumNameSpaceProvidersA (WS2_32.34)
5788 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
5790 FIXME( "(%p %p) Stub!\n", len
, buffer
);
5794 /***********************************************************************
5795 * WSAEnumNameSpaceProvidersW (WS2_32.35)
5797 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
5799 FIXME( "(%p %p) Stub!\n", len
, buffer
);
5803 /***********************************************************************
5804 * WSAGetQOSByName (WS2_32.41)
5806 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
5808 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
5812 /***********************************************************************
5813 * WSAGetServiceClassInfoA (WS2_32.42)
5815 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
5816 LPWSASERVICECLASSINFOA info
)
5818 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
5820 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
5821 return SOCKET_ERROR
;
5824 /***********************************************************************
5825 * WSAGetServiceClassInfoW (WS2_32.43)
5827 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
5828 LPWSASERVICECLASSINFOW info
)
5830 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
5832 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
5833 return SOCKET_ERROR
;
5836 /***********************************************************************
5837 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
5839 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
5841 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
5842 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
5843 return SOCKET_ERROR
;
5846 /***********************************************************************
5847 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
5849 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
5851 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
5852 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
5853 return SOCKET_ERROR
;
5856 /***********************************************************************
5857 * WSALookupServiceBeginA (WS2_32.59)
5859 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
5860 DWORD dwControlFlags
,
5863 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
5865 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
5866 return SOCKET_ERROR
;
5869 /***********************************************************************
5870 * WSALookupServiceBeginW (WS2_32.60)
5872 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
5873 DWORD dwControlFlags
,
5876 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
5878 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
5879 return SOCKET_ERROR
;
5882 /***********************************************************************
5883 * WSALookupServiceBeginW (WS2_32.61)
5885 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
5887 FIXME("(%p) Stub!\n", lookup
);
5891 /***********************************************************************
5892 * WSALookupServiceNextA (WS2_32.62)
5894 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
5896 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
5900 /***********************************************************************
5901 * WSALookupServiceNextW (WS2_32.63)
5903 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
5905 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
5909 /***********************************************************************
5910 * WSANtohl (WS2_32.64)
5912 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
5914 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
5916 if (!lphostlong
) return WSAEFAULT
;
5918 *lphostlong
= ntohl( netlong
);
5922 /***********************************************************************
5923 * WSANtohs (WS2_32.65)
5925 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
5927 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
5929 if (!lphostshort
) return WSAEFAULT
;
5931 *lphostshort
= ntohs( netshort
);
5935 /***********************************************************************
5936 * WSAProviderConfigChange (WS2_32.66)
5938 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
5939 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
5941 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
5942 return SOCKET_ERROR
;
5945 /***********************************************************************
5946 * WSARecvDisconnect (WS2_32.68)
5948 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
5950 TRACE( "(0x%04lx %p)\n", s
, disconnectdata
);
5952 return WS_shutdown( s
, 0 );
5955 /***********************************************************************
5956 * WSASetServiceA (WS2_32.76)
5958 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
5960 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
5964 /***********************************************************************
5965 * WSASetServiceW (WS2_32.77)
5967 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
5969 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
5973 /***********************************************************************
5974 * WSCEnableNSProvider (WS2_32.84)
5976 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
5978 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
5982 /***********************************************************************
5983 * WSCGetProviderPath (WS2_32.86)
5985 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
5987 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
5989 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
5991 *errcode
= WSAEINVAL
;
5992 return SOCKET_ERROR
;
5995 /***********************************************************************
5996 * WSCInstallNameSpace (WS2_32.87)
5998 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
5999 DWORD version
, LPGUID provider
)
6001 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
6002 namespace, version
, debugstr_guid(provider
) );
6006 /***********************************************************************
6007 * WSCUnInstallNameSpace (WS2_32.89)
6009 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
6011 FIXME("(%p) Stub!\n", lpProviderId
);
6015 /***********************************************************************
6016 * WSCWriteProviderOrder (WS2_32.91)
6018 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
6020 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);