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 # include <linux/ipx.h>
113 #ifdef HAVE_SYS_POLL_H
114 # include <sys/poll.h>
116 #ifdef HAVE_SYS_TIME_H
117 # include <sys/time.h>
120 #define NONAMELESSUNION
121 #define NONAMELESSSTRUCT
122 #include "ntstatus.h"
123 #define WIN32_NO_STATUS
128 #include "winerror.h"
130 #include "winsock2.h"
132 #include "ws2tcpip.h"
136 #include "iphlpapi.h"
137 #include "wine/server.h"
138 #include "wine/debug.h"
139 #include "wine/unicode.h"
142 # include "wsnwlink.h"
146 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
147 # define sipx_network sipx_addr.x_net
148 # define sipx_node sipx_addr.x_host.c_host
149 #endif /* __FreeBSD__ */
152 #define INADDR_NONE ~0UL
155 WINE_DEFAULT_DEBUG_CHANNEL(winsock
);
157 /* critical section to protect some non-reentrant net function */
158 extern CRITICAL_SECTION csWSgetXXXbyYYY
;
160 union generic_unix_sockaddr
162 struct sockaddr addr
;
163 char data
[128]; /* should be big enough for all families */
166 static inline const char *debugstr_sockaddr( const struct WS_sockaddr
*a
)
168 if (!a
) return "(nil)";
169 return wine_dbg_sprintf("{ family %d, address %s, port %d }",
170 ((const struct sockaddr_in
*)a
)->sin_family
,
171 inet_ntoa(((const struct sockaddr_in
*)a
)->sin_addr
),
172 ntohs(((const struct sockaddr_in
*)a
)->sin_port
));
175 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
176 #define SOCKET2HANDLE(s) ((HANDLE)(s))
177 #define HANDLE2SOCKET(h) ((SOCKET)(h))
179 /****************************************************************
180 * Async IO declarations
181 ****************************************************************/
183 typedef struct ws2_async
187 LPWSAOVERLAPPED user_overlapped
;
188 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func
;
189 IO_STATUS_BLOCK local_iosb
;
190 struct WS_sockaddr
*addr
;
193 int val
; /* for send operations */
194 int *ptr
; /* for recv operations */
197 unsigned int n_iovecs
;
198 unsigned int first_iovec
;
199 struct iovec iovec
[1];
202 /****************************************************************/
204 /* ----------------------------------- internal data */
206 /* ws_... struct conversion flags */
208 typedef struct /* WSAAsyncSelect() control struct */
210 HANDLE service
, event
, sock
;
216 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
217 #define WS_MAX_UDP_DATAGRAM 1024
218 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
220 /* hostent's, servent's and protent's are stored in one buffer per thread,
221 * as documented on MSDN for the functions that return any of the buffers */
222 struct per_thread_data
225 struct WS_hostent
*he_buffer
;
226 struct WS_servent
*se_buffer
;
227 struct WS_protoent
*pe_buffer
;
233 static INT num_startup
; /* reference counter */
234 static FARPROC blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
236 /* function prototypes */
237 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
238 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
);
239 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
241 int WSAIOCTL_GetInterfaceCount(void);
242 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
245 UINT
wsaHerrno(int errnr
);
247 #define MAP_OPTION(opt) { WS_##opt, opt }
249 static const int ws_sock_map
[][2] =
251 MAP_OPTION( SO_DEBUG
),
252 MAP_OPTION( SO_ACCEPTCONN
),
253 MAP_OPTION( SO_REUSEADDR
),
254 MAP_OPTION( SO_KEEPALIVE
),
255 MAP_OPTION( SO_DONTROUTE
),
256 MAP_OPTION( SO_BROADCAST
),
257 MAP_OPTION( SO_LINGER
),
258 MAP_OPTION( SO_OOBINLINE
),
259 MAP_OPTION( SO_SNDBUF
),
260 MAP_OPTION( SO_RCVBUF
),
261 MAP_OPTION( SO_ERROR
),
262 MAP_OPTION( SO_TYPE
),
264 MAP_OPTION( SO_RCVTIMEO
),
267 MAP_OPTION( SO_SNDTIMEO
),
271 static const int ws_tcp_map
[][2] =
274 MAP_OPTION( TCP_NODELAY
),
278 static const int ws_ip_map
[][2] =
280 MAP_OPTION( IP_MULTICAST_IF
),
281 MAP_OPTION( IP_MULTICAST_TTL
),
282 MAP_OPTION( IP_MULTICAST_LOOP
),
283 MAP_OPTION( IP_ADD_MEMBERSHIP
),
284 MAP_OPTION( IP_DROP_MEMBERSHIP
),
285 MAP_OPTION( IP_OPTIONS
),
287 MAP_OPTION( IP_HDRINCL
),
289 MAP_OPTION( IP_TOS
),
290 MAP_OPTION( IP_TTL
),
293 static const int ws_af_map
[][2] =
295 MAP_OPTION( AF_UNSPEC
),
296 MAP_OPTION( AF_INET
),
297 MAP_OPTION( AF_INET6
),
299 MAP_OPTION( AF_IPX
),
301 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
304 static const int ws_socktype_map
[][2] =
306 MAP_OPTION( SOCK_DGRAM
),
307 MAP_OPTION( SOCK_STREAM
),
308 MAP_OPTION( SOCK_RAW
),
309 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
312 static const int ws_proto_map
[][2] =
314 MAP_OPTION( IPPROTO_IP
),
315 MAP_OPTION( IPPROTO_TCP
),
316 MAP_OPTION( IPPROTO_UDP
),
317 MAP_OPTION( IPPROTO_ICMP
),
318 MAP_OPTION( IPPROTO_IGMP
),
319 MAP_OPTION( IPPROTO_RAW
),
320 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
323 static const int ws_aiflag_map
[][2] =
325 MAP_OPTION( AI_PASSIVE
),
326 MAP_OPTION( AI_CANONNAME
),
327 MAP_OPTION( AI_NUMERICHOST
),
328 /* Linux/UNIX knows a lot more. But Windows only
329 * has 3 as far as I could see. -Marcus
333 static const int ws_niflag_map
[][2] =
335 MAP_OPTION( NI_NOFQDN
),
336 MAP_OPTION( NI_NUMERICHOST
),
337 MAP_OPTION( NI_NAMEREQD
),
338 MAP_OPTION( NI_NUMERICSERV
),
339 MAP_OPTION( NI_DGRAM
),
342 static const int ws_eai_map
[][2] =
344 MAP_OPTION( EAI_AGAIN
),
345 MAP_OPTION( EAI_BADFLAGS
),
346 MAP_OPTION( EAI_FAIL
),
347 MAP_OPTION( EAI_FAMILY
),
348 MAP_OPTION( EAI_MEMORY
),
349 /* Note: EAI_NODATA is deprecated, but still
350 * used by Windows and Linux... We map the newer
351 * EAI_NONAME to EAI_NODATA for now until Windows
355 MAP_OPTION( EAI_NODATA
),
358 { WS_EAI_NODATA
, EAI_NONAME
},
361 MAP_OPTION( EAI_SERVICE
),
362 MAP_OPTION( EAI_SOCKTYPE
),
366 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
368 static inline DWORD
NtStatusToWSAError( const DWORD status
)
370 /* We only need to cover the status codes set by server async request handling */
374 case STATUS_SUCCESS
: wserr
= 0; break;
375 case STATUS_PENDING
: wserr
= WSA_IO_PENDING
; break;
376 case STATUS_OBJECT_TYPE_MISMATCH
: wserr
= WSAENOTSOCK
; break;
377 case STATUS_INVALID_HANDLE
: wserr
= WSAEBADF
; break;
378 case STATUS_INVALID_PARAMETER
: wserr
= WSAEINVAL
; break;
379 case STATUS_PIPE_DISCONNECTED
: wserr
= WSAESHUTDOWN
; break;
380 case STATUS_CANCELLED
: wserr
= WSA_OPERATION_ABORTED
; break;
381 case STATUS_TIMEOUT
: wserr
= WSAETIMEDOUT
; break;
382 case STATUS_NO_MEMORY
: wserr
= WSAEFAULT
; break;
384 if ( status
>= WSABASEERR
&& status
<= WSABASEERR
+1004 )
385 /* It is not an NT status code but a winsock error */
389 wserr
= RtlNtStatusToDosError( status
);
390 FIXME( "Status code %08x converted to DOS error code %x\n", status
, wserr
);
396 /* set last error code from NT status without mapping WSA errors */
397 static inline unsigned int set_error( unsigned int err
)
401 err
= NtStatusToWSAError( err
);
407 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
410 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
415 static inline void release_sock_fd( SOCKET s
, int fd
)
417 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
420 static void _enable_event( HANDLE s
, unsigned int event
,
421 unsigned int sstate
, unsigned int cstate
)
423 SERVER_START_REQ( enable_socket_event
)
427 req
->sstate
= sstate
;
428 req
->cstate
= cstate
;
429 wine_server_call( req
);
434 static int _is_blocking(SOCKET s
)
437 SERVER_START_REQ( get_socket_event
)
439 req
->handle
= SOCKET2HANDLE(s
);
440 req
->service
= FALSE
;
442 wine_server_call( req
);
443 ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
449 static unsigned int _get_sock_mask(SOCKET s
)
452 SERVER_START_REQ( get_socket_event
)
454 req
->handle
= SOCKET2HANDLE(s
);
455 req
->service
= FALSE
;
457 wine_server_call( req
);
464 static void _sync_sock_state(SOCKET s
)
466 /* do a dummy wineserver request in order to let
467 the wineserver run through its select loop once */
468 (void)_is_blocking(s
);
471 static int _get_sock_error(SOCKET s
, unsigned int bit
)
473 int events
[FD_MAX_EVENTS
];
475 SERVER_START_REQ( get_socket_event
)
477 req
->handle
= SOCKET2HANDLE(s
);
478 req
->service
= FALSE
;
480 wine_server_set_reply( req
, events
, sizeof(events
) );
481 wine_server_call( req
);
487 static struct per_thread_data
*get_per_thread_data(void)
489 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
490 /* lazy initialization */
493 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
494 NtCurrentTeb()->WinSockData
= ptb
;
499 static void free_per_thread_data(void)
501 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
505 /* delete scratch buffers */
506 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
507 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
508 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
509 ptb
->he_buffer
= NULL
;
510 ptb
->se_buffer
= NULL
;
511 ptb
->pe_buffer
= NULL
;
513 HeapFree( GetProcessHeap(), 0, ptb
);
514 NtCurrentTeb()->WinSockData
= NULL
;
517 /***********************************************************************
518 * DllMain (WS2_32.init)
520 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
522 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
524 case DLL_PROCESS_ATTACH
:
526 case DLL_PROCESS_DETACH
:
527 free_per_thread_data();
530 case DLL_THREAD_DETACH
:
531 free_per_thread_data();
537 /***********************************************************************
540 * Converts socket flags from Windows format.
541 * Return 1 if converted, 0 if not (error).
543 static int convert_sockopt(INT
*level
, INT
*optname
)
550 for(i
=0; i
<sizeof(ws_sock_map
)/sizeof(ws_sock_map
[0]); i
++) {
551 if( ws_sock_map
[i
][0] == *optname
)
553 *optname
= ws_sock_map
[i
][1];
557 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
560 *level
= IPPROTO_TCP
;
561 for(i
=0; i
<sizeof(ws_tcp_map
)/sizeof(ws_tcp_map
[0]); i
++) {
562 if ( ws_tcp_map
[i
][0] == *optname
)
564 *optname
= ws_tcp_map
[i
][1];
568 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
572 for(i
=0; i
<sizeof(ws_ip_map
)/sizeof(ws_ip_map
[0]); i
++) {
573 if (ws_ip_map
[i
][0] == *optname
)
575 *optname
= ws_ip_map
[i
][1];
579 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
581 default: FIXME("Unimplemented or unknown socket level\n");
586 /* ----------------------------------- Per-thread info (or per-process?) */
588 static char *strdup_lower(const char *str
)
591 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
595 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
598 else SetLastError(WSAENOBUFS
);
602 static inline int sock_error_p(int s
)
604 unsigned int optval
, optlen
;
606 optlen
= sizeof(optval
);
607 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
608 if (optval
) WARN("\t[%i] error: %d\n", s
, optval
);
612 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
613 * from an fd and return the value converted to milli seconds
614 * or -1 if there is an infinite time out */
615 static inline int get_rcvsnd_timeo( int fd
, int optname
)
618 unsigned int len
= sizeof(tv
);
619 int ret
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
621 ret
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
622 if( ret
<= 0 ) /* tv == {0,0} means infinite time out */
627 /* macro wrappers for portability */
629 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
631 #define GET_RCVTIMEO(fd) (-1)
635 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
637 #define GET_SNDTIMEO(fd) (-1)
640 /* utility: given an fd, will block until one of the events occurs */
641 static inline int do_block( int fd
, int events
, int timeout
)
649 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
660 convert_af_w2u(int windowsaf
) {
663 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
664 if (ws_af_map
[i
][0] == windowsaf
)
665 return ws_af_map
[i
][1];
666 FIXME("unhandled Windows address family %d\n", windowsaf
);
671 convert_af_u2w(int unixaf
) {
674 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
675 if (ws_af_map
[i
][1] == unixaf
)
676 return ws_af_map
[i
][0];
677 FIXME("unhandled UNIX address family %d\n", unixaf
);
682 convert_proto_w2u(int windowsproto
) {
685 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
686 if (ws_proto_map
[i
][0] == windowsproto
)
687 return ws_proto_map
[i
][1];
688 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
693 convert_proto_u2w(int unixproto
) {
696 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
697 if (ws_proto_map
[i
][1] == unixproto
)
698 return ws_proto_map
[i
][0];
699 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
704 convert_socktype_w2u(int windowssocktype
) {
707 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
708 if (ws_socktype_map
[i
][0] == windowssocktype
)
709 return ws_socktype_map
[i
][1];
710 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
715 convert_socktype_u2w(int unixsocktype
) {
718 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
719 if (ws_socktype_map
[i
][1] == unixsocktype
)
720 return ws_socktype_map
[i
][0];
721 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
725 /* ----------------------------------- API -----
727 * Init / cleanup / error checking.
730 /***********************************************************************
731 * WSAStartup (WS2_32.115)
733 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
735 TRACE("verReq=%x\n", wVersionRequested
);
737 if (LOBYTE(wVersionRequested
) < 1)
738 return WSAVERNOTSUPPORTED
;
740 if (!lpWSAData
) return WSAEINVAL
;
744 /* that's the whole of the negotiation for now */
745 lpWSAData
->wVersion
= wVersionRequested
;
746 /* return winsock information */
747 lpWSAData
->wHighVersion
= 0x0202;
748 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
749 strcpy(lpWSAData
->szSystemStatus
, "Running" );
750 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
751 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
752 /* don't do anything with lpWSAData->lpVendorInfo */
753 /* (some apps don't allocate the space for this field) */
755 TRACE("succeeded\n");
760 /***********************************************************************
761 * WSACleanup (WS2_32.116)
763 INT WINAPI
WSACleanup(void)
769 SetLastError(WSANOTINITIALISED
);
774 /***********************************************************************
775 * WSAGetLastError (WINSOCK.111)
776 * WSAGetLastError (WS2_32.111)
778 INT WINAPI
WSAGetLastError(void)
780 return GetLastError();
783 /***********************************************************************
784 * WSASetLastError (WS2_32.112)
786 void WINAPI
WSASetLastError(INT iError
) {
787 SetLastError(iError
);
790 static struct WS_hostent
*check_buffer_he(int size
)
792 struct per_thread_data
* ptb
= get_per_thread_data();
795 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
796 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
798 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
799 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
800 return ptb
->he_buffer
;
803 static struct WS_servent
*check_buffer_se(int size
)
805 struct per_thread_data
* ptb
= get_per_thread_data();
808 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
809 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
811 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
812 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
813 return ptb
->se_buffer
;
816 static struct WS_protoent
*check_buffer_pe(int size
)
818 struct per_thread_data
* ptb
= get_per_thread_data();
821 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
822 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
824 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
825 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
826 return ptb
->pe_buffer
;
829 /* ----------------------------------- i/o APIs */
832 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf)== WS_AF_IPX || (pf) == WS_AF_INET6)
834 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf) == WS_AF_INET6)
838 /**********************************************************************/
840 /* Returns the length of the converted address if successful, 0 if it was too small to
843 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
844 union generic_unix_sockaddr
*uaddr
)
846 unsigned int uaddrlen
= 0;
848 switch (wsaddr
->sa_family
)
853 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
854 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
856 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
859 uaddrlen
= sizeof(struct sockaddr_ipx
);
860 memset( uaddr
, 0, uaddrlen
);
861 uipx
->sipx_family
=AF_IPX
;
862 uipx
->sipx_port
=wsipx
->sa_socket
;
863 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
866 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
867 #ifdef IPX_FRAME_NONE
868 uipx
->sipx_type
=IPX_FRAME_NONE
;
874 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
875 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
877 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
878 * scope_id, one without.
880 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
881 uaddrlen
= sizeof(struct sockaddr_in6
);
882 memset( uaddr
, 0, uaddrlen
);
883 uin6
->sin6_family
= AF_INET6
;
884 uin6
->sin6_port
= win6
->sin6_port
;
885 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
886 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
887 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
889 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
892 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
896 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
897 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
899 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
901 uaddrlen
= sizeof(struct sockaddr_in
);
902 memset( uaddr
, 0, uaddrlen
);
903 uin
->sin_family
= AF_INET
;
904 uin
->sin_port
= win
->sin_port
;
905 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
909 /* Try to determine the needed space by the passed windows sockaddr space */
911 default: /* likely a ipv4 address */
912 case sizeof(struct WS_sockaddr_in
):
913 uaddrlen
= sizeof(struct sockaddr_in
);
916 case sizeof(struct WS_sockaddr_ipx
):
917 uaddrlen
= sizeof(struct sockaddr_ipx
);
920 case sizeof(struct WS_sockaddr_in6
):
921 case sizeof(struct WS_sockaddr_in6_old
):
922 uaddrlen
= sizeof(struct sockaddr_in6
);
925 memset( uaddr
, 0, uaddrlen
);
929 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
935 static BOOL
is_sockaddr_bound(const struct sockaddr
*uaddr
, int uaddrlen
)
937 switch (uaddr
->sa_family
)
941 FIXME("don't know how to tell if IPX socket is bound, assuming it is!\n");
946 static const struct sockaddr_in6 emptyAddr
;
947 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) uaddr
;
948 return in6
->sin6_port
|| memcmp(&in6
->sin6_addr
, &emptyAddr
.sin6_addr
, sizeof(struct in6_addr
));
952 static const struct sockaddr_in emptyAddr
;
953 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) uaddr
;
954 return in
->sin_port
|| memcmp(&in
->sin_addr
, &emptyAddr
.sin_addr
, sizeof(struct in_addr
));
959 FIXME("unknown address family %d\n", uaddr
->sa_family
);
964 /* Returns 0 if successful, -1 if the buffer is too small */
965 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
969 switch(uaddr
->sa_family
)
974 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
975 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
978 switch (*wsaddrlen
) /* how much can we copy? */
982 *wsaddrlen
= sizeof(*wsipx
);
983 wsipx
->sa_socket
=uipx
->sipx_port
;
987 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
995 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
1001 wsipx
->sa_family
=WS_AF_IPX
;
1012 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
1013 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
1015 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
1017 win6old
->sin6_family
= WS_AF_INET6
;
1018 win6old
->sin6_port
= uin6
->sin6_port
;
1019 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
1020 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1021 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1022 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1023 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
1024 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
1025 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
1026 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
1032 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
1033 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
1035 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
1037 win
->sin_family
= WS_AF_INET
;
1038 win
->sin_port
= uin
->sin_port
;
1039 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
1040 memset(win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
1041 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
1045 memset(wsaddr
,0,*wsaddrlen
);
1049 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
1055 /**************************************************************************
1056 * Functions for handling overlapped I/O
1057 **************************************************************************/
1059 /* user APC called upon async completion */
1060 static void WINAPI
ws2_async_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1062 ws2_async
*wsa
= arg
;
1064 if (wsa
->completion_func
) wsa
->completion_func( NtStatusToWSAError(iosb
->u
.Status
),
1065 iosb
->Information
, wsa
->user_overlapped
,
1067 HeapFree( GetProcessHeap(), 0, wsa
);
1070 /***********************************************************************
1071 * WS2_recv (INTERNAL)
1073 * Workhorse for both synchronous and asynchronous recv() operations.
1075 static int WS2_recv( int fd
, struct ws2_async
*wsa
)
1078 union generic_unix_sockaddr unix_sockaddr
;
1081 hdr
.msg_name
= NULL
;
1085 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
1086 hdr
.msg_name
= &unix_sockaddr
;
1089 hdr
.msg_namelen
= 0;
1091 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1092 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1093 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1094 hdr
.msg_accrights
= NULL
;
1095 hdr
.msg_accrightslen
= 0;
1097 hdr
.msg_control
= NULL
;
1098 hdr
.msg_controllen
= 0;
1102 if ( (n
= recvmsg(fd
, &hdr
, wsa
->flags
)) == -1 )
1105 /* if this socket is connected and lpFrom is not NULL, Linux doesn't give us
1106 * msg_name and msg_namelen from recvmsg, but it does set msg_namelen to zero.
1108 * quoting linux 2.6 net/ipv4/tcp.c:
1109 * "According to UNIX98, msg_name/msg_namelen are ignored
1110 * on connected socket. I was just happy when found this 8) --ANK"
1112 * likewise MSDN says that lpFrom and lpFromlen are ignored for
1113 * connection-oriented sockets, so don't try to update lpFrom.
1115 if (wsa
->addr
&& hdr
.msg_namelen
)
1116 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, wsa
->addr
, wsa
->addrlen
.ptr
);
1121 /***********************************************************************
1122 * WS2_async_recv (INTERNAL)
1124 * Handler for overlapped recv() operations.
1126 static NTSTATUS
WS2_async_recv( void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, ULONG_PTR
*total
)
1128 ws2_async
* wsa
= user
;
1133 case STATUS_ALERTED
:
1134 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
1137 result
= WS2_recv( fd
, wsa
);
1138 wine_server_release_fd( wsa
->hSocket
, fd
);
1141 status
= STATUS_SUCCESS
;
1142 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1146 if (errno
== EINTR
|| errno
== EAGAIN
)
1148 status
= STATUS_PENDING
;
1149 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1154 status
= wsaErrno(); /* FIXME: is this correct ???? */
1159 if (status
!= STATUS_PENDING
)
1161 iosb
->u
.Status
= status
;
1162 iosb
->Information
= *total
= result
;
1167 /***********************************************************************
1168 * WS2_send (INTERNAL)
1170 * Workhorse for both synchronous and asynchronous send() operations.
1172 static int WS2_send( int fd
, struct ws2_async
*wsa
)
1175 union generic_unix_sockaddr unix_addr
;
1177 hdr
.msg_name
= NULL
;
1178 hdr
.msg_namelen
= 0;
1182 hdr
.msg_name
= &unix_addr
;
1183 hdr
.msg_namelen
= ws_sockaddr_ws2u( wsa
->addr
, wsa
->addrlen
.val
, &unix_addr
);
1184 if ( !hdr
.msg_namelen
)
1190 #if defined(HAVE_IPX) && defined(SOL_IPX)
1191 if(wsa
->addr
->sa_family
== WS_AF_IPX
)
1193 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
1195 unsigned int len
=sizeof(int);
1197 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1198 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1199 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1200 * ipx type in the sockaddr_opx structure with the stored value.
1202 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
1203 uipx
->sipx_type
= val
;
1208 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1209 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1210 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1211 hdr
.msg_accrights
= NULL
;
1212 hdr
.msg_accrightslen
= 0;
1214 hdr
.msg_control
= NULL
;
1215 hdr
.msg_controllen
= 0;
1219 return sendmsg(fd
, &hdr
, wsa
->flags
);
1222 /***********************************************************************
1223 * WS2_async_send (INTERNAL)
1225 * Handler for overlapped send() operations.
1227 static NTSTATUS
WS2_async_send(void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, ULONG_PTR
*total
)
1229 ws2_async
* wsa
= user
;
1234 case STATUS_ALERTED
:
1235 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
1238 /* check to see if the data is ready (non-blocking) */
1239 result
= WS2_send( fd
, wsa
);
1240 wine_server_release_fd( wsa
->hSocket
, fd
);
1244 int totalLength
= 0;
1246 status
= STATUS_SUCCESS
;
1247 for (i
= 0; i
< wsa
->n_iovecs
; i
++)
1248 totalLength
+= wsa
->iovec
[i
].iov_len
;
1249 if (result
< totalLength
)
1250 _enable_event( wsa
->hSocket
, FD_WRITE
, 0, 0 );
1254 if (errno
== EINTR
|| errno
== EAGAIN
)
1256 status
= STATUS_PENDING
;
1257 _enable_event( wsa
->hSocket
, FD_WRITE
, 0, 0 );
1261 /* We set the status to a winsock error code and check for that
1262 later in NtStatusToWSAError () */
1263 status
= wsaErrno();
1269 if (status
!= STATUS_PENDING
)
1271 iosb
->u
.Status
= status
;
1272 iosb
->Information
= *total
= result
;
1277 /***********************************************************************
1278 * WS2_async_shutdown (INTERNAL)
1280 * Handler for shutdown() operations on overlapped sockets.
1282 static NTSTATUS
WS2_async_shutdown( void* user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
)
1284 ws2_async
* wsa
= user
;
1289 case STATUS_ALERTED
:
1290 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
1293 switch ( wsa
->type
)
1295 case ASYNC_TYPE_READ
: err
= shutdown( fd
, 0 ); break;
1296 case ASYNC_TYPE_WRITE
: err
= shutdown( fd
, 1 ); break;
1298 wine_server_release_fd( wsa
->hSocket
, fd
);
1299 status
= err
? wsaErrno() : STATUS_SUCCESS
;
1302 iosb
->u
.Status
= status
;
1306 /***********************************************************************
1307 * WS2_register_async_shutdown (INTERNAL)
1309 * Helper function for WS_shutdown() on overlapped sockets.
1311 static int WS2_register_async_shutdown( SOCKET s
, int type
)
1313 struct ws2_async
*wsa
;
1316 TRACE("s %ld type %d\n", s
, type
);
1318 wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
1322 wsa
->hSocket
= SOCKET2HANDLE(s
);
1324 wsa
->completion_func
= NULL
;
1326 SERVER_START_REQ( register_async
)
1328 req
->handle
= wsa
->hSocket
;
1330 req
->async
.callback
= WS2_async_shutdown
;
1331 req
->async
.iosb
= &wsa
->local_iosb
;
1332 req
->async
.arg
= wsa
;
1333 req
->async
.apc
= ws2_async_apc
;
1334 req
->async
.cvalue
= 0;
1335 status
= wine_server_call( req
);
1339 if (status
!= STATUS_PENDING
)
1341 HeapFree( GetProcessHeap(), 0, wsa
);
1342 return NtStatusToWSAError( status
);
1347 /***********************************************************************
1350 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
,
1356 TRACE("socket %04lx\n", s
);
1357 is_blocking
= _is_blocking(s
);
1362 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1363 if (fd
== -1) return INVALID_SOCKET
;
1365 do_block(fd
, POLLIN
, -1);
1366 _sync_sock_state(s
); /* let wineserver notice connection */
1367 release_sock_fd( s
, fd
);
1368 /* retrieve any error codes from it */
1369 SetLastError(_get_sock_error(s
, FD_ACCEPT_BIT
));
1370 /* FIXME: care about the error? */
1372 SERVER_START_REQ( accept_socket
)
1374 req
->lhandle
= SOCKET2HANDLE(s
);
1375 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
1376 req
->attributes
= OBJ_INHERIT
;
1377 set_error( wine_server_call( req
) );
1378 as
= HANDLE2SOCKET( reply
->handle
);
1383 if (addr
) WS_getpeername(as
, addr
, addrlen32
);
1386 } while (is_blocking
);
1387 return INVALID_SOCKET
;
1390 /***********************************************************************
1393 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
1395 int fd
= get_sock_fd( s
, 0, NULL
);
1396 int res
= SOCKET_ERROR
;
1398 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
1402 if (!name
|| (name
->sa_family
&& !SUPPORTED_PF(name
->sa_family
)))
1404 SetLastError(WSAEAFNOSUPPORT
);
1408 union generic_unix_sockaddr uaddr
;
1409 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
1412 SetLastError(WSAEFAULT
);
1417 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) &uaddr
;
1418 if (name
->sa_family
== WS_AF_INET6
&&
1419 !memcmp(&in6
->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
)))
1422 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
)) == -1)
1424 release_sock_fd( s
, fd
);
1425 SetLastError(WSAEAFNOSUPPORT
);
1426 return INVALID_SOCKET
;
1430 if (name
->sa_family
== WS_AF_INET
)
1432 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
1433 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
1435 /* Trying to bind to the default host interface, using
1436 * INADDR_ANY instead*/
1437 WARN("Trying to bind to magic IP address, using "
1438 "INADDR_ANY instead.\n");
1439 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_ANY
);
1442 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
1444 int loc_errno
= errno
;
1445 WARN("\tfailure - errno = %i\n", errno
);
1450 SetLastError(WSAENOTSOCK
);
1453 SetLastError(WSAEINVAL
);
1456 SetLastError(wsaErrno());
1462 res
=0; /* success */
1466 release_sock_fd( s
, fd
);
1471 /***********************************************************************
1472 * closesocket (WS2_32.3)
1474 int WINAPI
WS_closesocket(SOCKET s
)
1476 TRACE("socket %04lx\n", s
);
1477 if (CloseHandle(SOCKET2HANDLE(s
))) return 0;
1478 return SOCKET_ERROR
;
1481 /***********************************************************************
1482 * connect (WS2_32.4)
1484 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
1486 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1488 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
1492 union generic_unix_sockaddr uaddr
;
1493 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
1497 SetLastError(WSAEFAULT
);
1501 if (name
->sa_family
== WS_AF_INET
)
1503 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
1504 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
1506 /* Trying to connect to magic replace-loopback address,
1507 * assuming we really want to connect to localhost */
1508 TRACE("Trying to connect to magic IP address, using "
1509 "INADDR_LOOPBACK instead.\n");
1510 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_LOOPBACK
);
1514 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
1515 goto connect_success
;
1518 if (errno
== EINPROGRESS
)
1520 /* tell wineserver that a connection is in progress */
1521 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
1522 FD_CONNECT
|FD_READ
|FD_WRITE
,
1523 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
1524 if (_is_blocking(s
))
1528 do_block(fd
, POLLIN
| POLLOUT
, -1);
1529 _sync_sock_state(s
); /* let wineserver notice connection */
1530 /* retrieve any error codes from it */
1531 result
= _get_sock_error(s
, FD_CONNECT_BIT
);
1533 SetLastError(result
);
1536 goto connect_success
;
1541 SetLastError(WSAEWOULDBLOCK
);
1546 SetLastError(wsaErrno());
1548 release_sock_fd( s
, fd
);
1550 return SOCKET_ERROR
;
1553 release_sock_fd( s
, fd
);
1554 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
1555 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
1556 FD_CONNECT
|FD_WINE_LISTENING
);
1560 /***********************************************************************
1561 * WSAConnect (WS2_32.30)
1563 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
1564 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
1565 LPQOS lpSQOS
, LPQOS lpGQOS
)
1567 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
1568 FIXME("unsupported parameters!\n");
1569 return WS_connect( s
, name
, namelen
);
1573 /***********************************************************************
1574 * getpeername (WS2_32.5)
1576 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
1581 TRACE("socket: %04lx, ptr %p, len %08x\n", s
, name
, *namelen
);
1583 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1584 if( (name
== NULL
) || (namelen
== NULL
) )
1586 SetLastError( WSAEFAULT
);
1587 return SOCKET_ERROR
;
1590 fd
= get_sock_fd( s
, 0, NULL
);
1595 union generic_unix_sockaddr uaddr
;
1596 unsigned int uaddrlen
= sizeof(uaddr
);
1598 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
1600 SetLastError(wsaErrno());
1602 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
1604 /* The buffer was too small */
1605 SetLastError(WSAEFAULT
);
1611 release_sock_fd( s
, fd
);
1616 /***********************************************************************
1617 * getsockname (WS2_32.6)
1619 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
1624 TRACE("socket: %04lx, ptr %p, len %8x\n", s
, name
, *namelen
);
1626 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1627 if( (name
== NULL
) || (namelen
== NULL
) )
1629 SetLastError( WSAEFAULT
);
1630 return SOCKET_ERROR
;
1633 fd
= get_sock_fd( s
, 0, NULL
);
1638 union generic_unix_sockaddr uaddr
;
1639 unsigned int uaddrlen
= sizeof(uaddr
);
1641 if (getsockname(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
1643 SetLastError(wsaErrno());
1645 else if (!is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
1647 SetLastError(WSAEINVAL
);
1649 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
1651 /* The buffer was too small */
1652 SetLastError(WSAEFAULT
);
1658 release_sock_fd( s
, fd
);
1663 /***********************************************************************
1664 * getsockopt (WS2_32.7)
1666 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
1667 INT optname
, char *optval
, INT
*optlen
)
1672 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
1673 s
, level
, optname
, optval
, *optlen
);
1681 /* Handle common cases. The special cases are below, sorted
1683 case WS_SO_ACCEPTCONN
:
1684 case WS_SO_BROADCAST
:
1687 case WS_SO_KEEPALIVE
:
1688 case WS_SO_OOBINLINE
:
1690 case WS_SO_REUSEADDR
:
1693 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1694 return SOCKET_ERROR
;
1695 convert_sockopt(&level
, &optname
);
1696 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
1698 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1701 release_sock_fd( s
, fd
);
1704 case WS_SO_DONTLINGER
:
1706 struct linger lingval
;
1707 unsigned int len
= sizeof(struct linger
);
1709 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
1711 SetLastError(WSAEFAULT
);
1712 return SOCKET_ERROR
;
1714 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1715 return SOCKET_ERROR
;
1717 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
1719 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1724 *(BOOL
*)optval
= (lingval
.l_onoff
) ? FALSE
: TRUE
;
1725 *optlen
= sizeof(BOOL
);
1728 release_sock_fd( s
, fd
);
1732 /* As mentioned in setsockopt, Windows ignores this, so we
1733 * always return true here */
1734 case WS_SO_DONTROUTE
:
1735 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
1737 SetLastError(WSAEFAULT
);
1738 return SOCKET_ERROR
;
1740 *(BOOL
*)optval
= TRUE
;
1741 *optlen
= sizeof(BOOL
);
1746 struct linger lingval
;
1747 unsigned int len
= sizeof(struct linger
);
1749 /* struct linger and LINGER have different sizes */
1750 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
1752 SetLastError(WSAEFAULT
);
1753 return SOCKET_ERROR
;
1755 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1756 return SOCKET_ERROR
;
1758 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
1760 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1765 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
1766 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
1767 *optlen
= sizeof(struct linger
);
1770 release_sock_fd( s
, fd
);
1774 case WS_SO_MAX_MSG_SIZE
:
1775 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
1777 SetLastError(WSAEFAULT
);
1778 return SOCKET_ERROR
;
1780 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
1781 *(int *)optval
= 65507;
1782 *optlen
= sizeof(int);
1785 /* SO_OPENTYPE does not require a valid socket handle. */
1786 case WS_SO_OPENTYPE
:
1787 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
1789 SetLastError(WSAEFAULT
);
1790 return SOCKET_ERROR
;
1792 *(int *)optval
= get_per_thread_data()->opentype
;
1793 *optlen
= sizeof(int);
1794 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
1798 case WS_SO_RCVTIMEO
:
1801 case WS_SO_SNDTIMEO
:
1803 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
1806 unsigned int len
= sizeof(struct timeval
);
1808 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
1810 SetLastError(WSAEFAULT
);
1811 return SOCKET_ERROR
;
1813 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1814 return SOCKET_ERROR
;
1816 convert_sockopt(&level
, &optname
);
1817 if (getsockopt(fd
, level
, optname
, &tv
, &len
) != 0 )
1819 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1824 *(int *)optval
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
1825 *optlen
= sizeof(int);
1828 release_sock_fd( s
, fd
);
1833 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
1834 SetLastError(WSAENOPROTOOPT
);
1835 return SOCKET_ERROR
;
1836 } /* end switch(optname) */
1837 }/* end case WS_SOL_SOCKET */
1841 struct WS_sockaddr_ipx addr
;
1842 IPX_ADDRESS_DATA
*data
;
1847 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
1849 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (unsigned int*)optlen
) == -1)
1856 socklen_t len
=sizeof(struct ipx
);
1857 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
1860 *optval
= (int)val
.ipx_pt
;
1863 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
1864 release_sock_fd( s
, fd
);
1869 * On a Win2000 system with one network card there are usually
1870 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
1871 * Using this call you can then retrieve info about this all.
1872 * In case of Linux it is a bit different. Usually you have
1873 * only "one" device active and further it is not possible to
1874 * query things like the linkspeed.
1876 FIXME("IPX_ADDRESS\n");
1877 namelen
= sizeof(struct WS_sockaddr_ipx
);
1878 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
1879 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
1881 data
= (IPX_ADDRESS_DATA
*)optval
;
1882 memcpy(data
->nodenum
,addr
.sa_nodenum
,sizeof(data
->nodenum
));
1883 memcpy(data
->netnum
,addr
.sa_netnum
,sizeof(data
->netnum
));
1884 data
->adapternum
= 0;
1885 data
->wan
= FALSE
; /* We are not on a wan for now .. */
1886 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
1887 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
1888 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
1889 * note 1MB = 1000kB in this case */
1892 case IPX_MAX_ADAPTER_NUM
:
1893 FIXME("IPX_MAX_ADAPTER_NUM\n");
1894 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
1898 FIXME("IPX optname:%x\n", optname
);
1899 return SOCKET_ERROR
;
1900 }/* end switch(optname) */
1901 } /* end case NSPROTO_IPX */
1903 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
1904 case WS_IPPROTO_TCP
:
1907 case WS_TCP_NODELAY
:
1908 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1909 return SOCKET_ERROR
;
1910 convert_sockopt(&level
, &optname
);
1911 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
1913 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1916 release_sock_fd( s
, fd
);
1919 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
1920 return SOCKET_ERROR
;
1925 case WS_IP_ADD_MEMBERSHIP
:
1926 case WS_IP_DROP_MEMBERSHIP
:
1930 case WS_IP_MULTICAST_IF
:
1931 case WS_IP_MULTICAST_LOOP
:
1932 case WS_IP_MULTICAST_TTL
:
1936 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1937 return SOCKET_ERROR
;
1938 convert_sockopt(&level
, &optname
);
1939 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
1941 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1944 release_sock_fd( s
, fd
);
1946 case WS_IP_DONTFRAGMENT
:
1947 FIXME("WS_IP_DONTFRAGMENT is always false!\n");
1948 *(BOOL
*)optval
= FALSE
;
1951 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
1952 return SOCKET_ERROR
;
1955 FIXME("Unknown level: 0x%08x\n", level
);
1956 return SOCKET_ERROR
;
1957 } /* end switch(level) */
1960 /***********************************************************************
1964 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
1966 return htonl(hostlong
);
1970 /***********************************************************************
1974 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
1976 return htons(hostshort
);
1979 /***********************************************************************
1980 * WSAHtonl (WS2_32.46)
1981 * From MSDN description of error codes, this function should also
1982 * check if WinSock has been initialized and the socket is a valid
1983 * socket. But why? This function only translates a host byte order
1984 * u_long into a network byte order u_long...
1986 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
1990 *lpnetlong
= htonl(hostlong
);
1993 WSASetLastError(WSAEFAULT
);
1994 return SOCKET_ERROR
;
1997 /***********************************************************************
1998 * WSAHtons (WS2_32.47)
1999 * From MSDN description of error codes, this function should also
2000 * check if WinSock has been initialized and the socket is a valid
2001 * socket. But why? This function only translates a host byte order
2002 * u_short into a network byte order u_short...
2004 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
2009 *lpnetshort
= htons(hostshort
);
2012 WSASetLastError(WSAEFAULT
);
2013 return SOCKET_ERROR
;
2017 /***********************************************************************
2018 * inet_addr (WINSOCK.10)
2019 * inet_addr (WS2_32.11)
2021 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
2023 if (!cp
) return INADDR_NONE
;
2024 return inet_addr(cp
);
2028 /***********************************************************************
2029 * ntohl (WINSOCK.14)
2032 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
2034 return ntohl(netlong
);
2038 /***********************************************************************
2039 * ntohs (WINSOCK.15)
2042 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
2044 return ntohs(netshort
);
2048 /***********************************************************************
2049 * inet_ntoa (WS2_32.12)
2051 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
2053 /* use "buffer for dummies" here because some applications have a
2054 * propensity to decode addresses in ws_hostent structure without
2055 * saving them first...
2057 static char dbuffer
[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
2059 char* s
= inet_ntoa(*((struct in_addr
*)&in
));
2065 SetLastError(wsaErrno());
2069 /**********************************************************************
2070 * WSAIoctl (WS2_32.50)
2073 INT WINAPI
WSAIoctl(SOCKET s
,
2074 DWORD dwIoControlCode
,
2077 LPVOID lpbOutBuffer
,
2079 LPDWORD lpcbBytesReturned
,
2080 LPWSAOVERLAPPED lpOverlapped
,
2081 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2083 TRACE("%ld, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
2084 s
, dwIoControlCode
, lpvInBuffer
, cbInBuffer
, lpbOutBuffer
,
2085 cbOutBuffer
, lpcbBytesReturned
, lpOverlapped
, lpCompletionRoutine
);
2087 switch( dwIoControlCode
)
2090 if (cbInBuffer
!= sizeof(WS_u_long
)) {
2091 WSASetLastError(WSAEFAULT
);
2092 return SOCKET_ERROR
;
2094 return WS_ioctlsocket( s
, WS_FIONBIO
, lpvInBuffer
);
2097 if (cbOutBuffer
!= sizeof(WS_u_long
)) {
2098 WSASetLastError(WSAEFAULT
);
2099 return SOCKET_ERROR
;
2101 return WS_ioctlsocket( s
, WS_FIONREAD
, lpbOutBuffer
);
2103 case WS_SIO_GET_INTERFACE_LIST
:
2105 INTERFACE_INFO
* intArray
= (INTERFACE_INFO
*)lpbOutBuffer
;
2106 DWORD size
, numInt
, apiReturn
;
2109 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
2113 WSASetLastError(WSAEFAULT
);
2114 return SOCKET_ERROR
;
2116 if (!lpcbBytesReturned
)
2118 WSASetLastError(WSAEFAULT
);
2119 return SOCKET_ERROR
;
2122 fd
= get_sock_fd( s
, 0, NULL
);
2123 if (fd
== -1) return SOCKET_ERROR
;
2125 apiReturn
= GetAdaptersInfo(NULL
, &size
);
2126 if (apiReturn
== ERROR_NO_DATA
)
2130 else if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
2132 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
2136 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
2138 PIP_ADAPTER_INFO ptr
;
2140 if (size
*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > cbOutBuffer
)
2142 WARN("Buffer too small = %u, cbOutBuffer = %u\n", size
, cbOutBuffer
);
2143 HeapFree(GetProcessHeap(),0,table
);
2144 release_sock_fd( s
, fd
);
2145 WSASetLastError(WSAEFAULT
);
2146 return SOCKET_ERROR
;
2148 for (ptr
= table
, numInt
= 0; ptr
;
2149 ptr
= ptr
->Next
, intArray
++, numInt
++)
2151 unsigned int addr
, mask
, bcast
;
2152 struct ifreq ifInfo
;
2154 /* Socket Status Flags */
2155 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
2156 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
2158 ERR("Error obtaining status flags for socket!\n");
2159 HeapFree(GetProcessHeap(),0,table
);
2160 release_sock_fd( s
, fd
);
2161 WSASetLastError(WSAEINVAL
);
2162 return SOCKET_ERROR
;
2166 /* set flags; the values of IFF_* are not the same
2167 under Linux and Windows, therefore must generate
2169 intArray
->iiFlags
= 0;
2170 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
2171 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
2172 #ifdef IFF_POINTOPOINT
2173 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
2174 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
2176 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
2177 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
2178 if (ifInfo
.ifr_flags
& IFF_UP
)
2179 intArray
->iiFlags
|= WS_IFF_UP
;
2180 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
2181 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
2184 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
2185 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
2186 bcast
= addr
| ~mask
;
2187 intArray
->iiAddress
.AddressIn
.sin_family
= AF_INET
;
2188 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
2189 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
=
2191 intArray
->iiNetmask
.AddressIn
.sin_family
= AF_INET
;
2192 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
2193 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
=
2195 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
=
2197 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
2198 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.
2204 ERR("Unable to get interface table!\n");
2205 release_sock_fd( s
, fd
);
2206 HeapFree(GetProcessHeap(),0,table
);
2207 WSASetLastError(WSAEINVAL
);
2208 return SOCKET_ERROR
;
2210 HeapFree(GetProcessHeap(),0,table
);
2214 release_sock_fd( s
, fd
);
2215 WSASetLastError(WSAEINVAL
);
2216 return SOCKET_ERROR
;
2221 ERR("Unable to get interface table!\n");
2222 release_sock_fd( s
, fd
);
2223 WSASetLastError(WSAEINVAL
);
2224 return SOCKET_ERROR
;
2226 /* Calculate the size of the array being returned */
2227 *lpcbBytesReturned
= sizeof(INTERFACE_INFO
) * numInt
;
2228 release_sock_fd( s
, fd
);
2232 case WS_SIO_ADDRESS_LIST_CHANGE
:
2233 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
2234 /* FIXME: error and return code depend on whether socket was created
2235 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
2238 case WS_SIO_ADDRESS_LIST_QUERY
:
2242 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
2244 if (!lpcbBytesReturned
)
2246 WSASetLastError(WSAEFAULT
);
2247 return SOCKET_ERROR
;
2250 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
2252 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
2255 if (!table
|| GetAdaptersInfo(table
, &size
))
2257 HeapFree(GetProcessHeap(), 0, table
);
2258 WSASetLastError(WSAEINVAL
);
2259 return SOCKET_ERROR
;
2262 for (p
= table
, num
= 0; p
; p
= p
->Next
)
2263 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
2265 need
= sizeof(SOCKET_ADDRESS_LIST
) + sizeof(SOCKET_ADDRESS
) * (num
- 1);
2266 need
+= sizeof(SOCKADDR
) * num
;
2267 *lpcbBytesReturned
= need
;
2269 if (need
> cbOutBuffer
)
2271 HeapFree(GetProcessHeap(), 0, table
);
2272 WSASetLastError(WSAEFAULT
);
2273 return SOCKET_ERROR
;
2280 SOCKET_ADDRESS_LIST
*sa_list
= (SOCKET_ADDRESS_LIST
*)lpbOutBuffer
;
2281 SOCKADDR_IN
*sockaddr
;
2283 sa
= sa_list
->Address
;
2284 sockaddr
= (SOCKADDR_IN
*)((char *)sa
+ num
* sizeof(SOCKET_ADDRESS
));
2285 sa_list
->iAddressCount
= num
;
2287 for (p
= table
, i
= 0; p
; p
= p
->Next
)
2289 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
2291 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
2292 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
2294 sockaddr
[i
].sin_family
= AF_INET
;
2295 sockaddr
[i
].sin_port
= 0;
2296 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
2301 HeapFree(GetProcessHeap(), 0, table
);
2306 WARN("unable to get IP address list\n");
2307 WSASetLastError(WSAEINVAL
);
2308 return SOCKET_ERROR
;
2312 FIXME("SIO_FLUSH: stub.\n");
2315 case WS_SIO_GET_EXTENSION_FUNCTION_POINTER
:
2316 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER %s: stub\n", debugstr_guid(lpvInBuffer
));
2317 WSASetLastError(WSAEOPNOTSUPP
);
2318 return SOCKET_ERROR
;
2321 FIXME("unsupported WS_IOCTL cmd (%08x)\n", dwIoControlCode
);
2322 WSASetLastError(WSAEOPNOTSUPP
);
2323 return SOCKET_ERROR
;
2330 /***********************************************************************
2331 * ioctlsocket (WS2_32.10)
2333 int WINAPI
WS_ioctlsocket(SOCKET s
, LONG cmd
, WS_u_long
*argp
)
2338 TRACE("socket %04lx, cmd %08x, ptr %p\n", s
, cmd
, argp
);
2339 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
2340 if(IS_INTRESOURCE(argp
))
2342 SetLastError(WSAEFAULT
);
2343 return SOCKET_ERROR
;
2353 if( _get_sock_mask(s
) )
2355 /* AsyncSelect()'ed sockets are always nonblocking */
2356 if (*argp
) return 0;
2357 SetLastError(WSAEINVAL
);
2358 return SOCKET_ERROR
;
2360 fd
= get_sock_fd( s
, 0, NULL
);
2366 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
2367 ret
= fcntl( fd
, F_SETFL
, O_NONBLOCK
);
2371 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
2372 ret
= fcntl( fd
, F_SETFL
, 0 );
2374 release_sock_fd( s
, fd
);
2376 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2378 return SOCKET_ERROR
;
2385 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
2386 SetLastError(WSAEINVAL
);
2387 return SOCKET_ERROR
;
2389 case SIOCGIFBRDADDR
:
2390 case SIOCGIFNETMASK
:
2392 /* These don't need any special handling. They are used by
2393 WsControl, and are here to suppress an unnecessary warning. */
2397 /* Netscape tries hard to use bogus ioctl 0x667e */
2398 /* FIXME: 0x667e above is ('f' << 8) | 126, and is a low word of
2399 * FIONBIO (_IOW('f', 126, u_long)), how that should be handled?
2401 WARN("\tunknown WS_IOCTL cmd (%08x)\n", cmd
);
2405 fd
= get_sock_fd( s
, 0, NULL
);
2408 if( ioctl(fd
, newcmd
, (char*)argp
) == 0 )
2410 release_sock_fd( s
, fd
);
2413 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2414 release_sock_fd( s
, fd
);
2416 return SOCKET_ERROR
;
2419 /***********************************************************************
2420 * listen (WS2_32.13)
2422 int WINAPI
WS_listen(SOCKET s
, int backlog
)
2424 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2426 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
2429 if (listen(fd
, backlog
) == 0)
2431 release_sock_fd( s
, fd
);
2432 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
2434 FD_CONNECT
|FD_WINE_CONNECTED
);
2437 SetLastError(wsaErrno());
2438 release_sock_fd( s
, fd
);
2440 return SOCKET_ERROR
;
2443 /***********************************************************************
2446 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
2448 DWORD n
, dwFlags
= flags
;
2454 if ( WSARecvFrom(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
2455 return SOCKET_ERROR
;
2460 /***********************************************************************
2461 * recvfrom (WS2_32.17)
2463 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
2464 struct WS_sockaddr
*from
, int *fromlen
)
2466 DWORD n
, dwFlags
= flags
;
2472 if ( WSARecvFrom(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
) == SOCKET_ERROR
)
2473 return SOCKET_ERROR
;
2478 /* allocate a poll array for the corresponding fd sets */
2479 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
2480 const WS_fd_set
*exceptfds
, int *count_ptr
)
2482 int i
, j
= 0, count
= 0;
2485 if (readfds
) count
+= readfds
->fd_count
;
2486 if (writefds
) count
+= writefds
->fd_count
;
2487 if (exceptfds
) count
+= exceptfds
->fd_count
;
2489 if (!count
) return NULL
;
2490 if (!(fds
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(fds
[0])))) return NULL
;
2492 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
2494 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
2495 fds
[j
].events
= POLLIN
;
2499 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
2501 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
2502 fds
[j
].events
= POLLOUT
;
2506 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
2508 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
2509 fds
[j
].events
= POLLHUP
;
2515 /* release the file descriptor obtained in fd_sets_to_poll */
2516 /* must be called with the original fd_set arrays, before calling get_poll_results */
2517 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
2518 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
2524 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
2525 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
2529 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
2530 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
2534 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
2535 if (fds
[j
].fd
!= -1)
2537 /* make sure we have a real error before releasing the fd */
2538 if (!sock_error_p( fds
[j
].fd
)) fds
[j
].revents
= 0;
2539 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
2544 /* map the poll results back into the Windows fd sets */
2545 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
2546 const struct pollfd
*fds
)
2548 int i
, j
= 0, k
, total
= 0;
2552 for (i
= k
= 0; i
< readfds
->fd_count
; i
++, j
++)
2553 if (fds
[j
].revents
) readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
2554 readfds
->fd_count
= k
;
2559 for (i
= k
= 0; i
< writefds
->fd_count
; i
++, j
++)
2560 if (fds
[j
].revents
) writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
2561 writefds
->fd_count
= k
;
2566 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
2567 if (fds
[j
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
2568 exceptfds
->fd_count
= k
;
2575 /***********************************************************************
2576 * select (WS2_32.18)
2578 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
2579 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
2580 const struct WS_timeval
* ws_timeout
)
2582 struct pollfd
*pollfds
;
2583 int count
, ret
, timeout
= -1;
2585 TRACE("read %p, write %p, excp %p timeout %p\n",
2586 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
2588 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)) && count
)
2590 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
2591 return SOCKET_ERROR
;
2594 if (ws_timeout
) timeout
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
2596 ret
= poll( pollfds
, count
, timeout
);
2597 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
2599 if (ret
== -1) SetLastError(wsaErrno());
2600 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
2601 HeapFree( GetProcessHeap(), 0, pollfds
);
2605 /* helper to send completion messages for client-only i/o operation case */
2606 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
, ULONG_PTR Information
)
2610 SERVER_START_REQ( add_fd_completion
)
2612 req
->handle
= SOCKET2HANDLE(sock
);
2613 req
->cvalue
= CompletionValue
;
2614 req
->status
= CompletionStatus
;
2615 req
->information
= Information
;
2616 status
= wine_server_call( req
);
2622 /***********************************************************************
2625 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
2631 wsabuf
.buf
= (char*) buf
;
2633 if ( WSASendTo( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
2634 return SOCKET_ERROR
;
2639 /***********************************************************************
2640 * WSASend (WS2_32.72)
2642 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
2643 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
2644 LPWSAOVERLAPPED lpOverlapped
,
2645 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2647 return WSASendTo( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
2648 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
2651 /***********************************************************************
2652 * WSASendDisconnect (WS2_32.73)
2654 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
2656 return WS_shutdown( s
, SD_SEND
);
2660 /***********************************************************************
2661 * WSASendTo (WS2_32.74)
2663 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
2664 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
2665 const struct WS_sockaddr
*to
, int tolen
,
2666 LPWSAOVERLAPPED lpOverlapped
,
2667 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2669 unsigned int i
, options
;
2671 struct ws2_async
*wsa
;
2672 int totalLength
= 0;
2673 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
2675 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
2676 s
, lpBuffers
, dwBufferCount
, dwFlags
,
2677 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
2679 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
2680 TRACE( "fd=%d, options=%x\n", fd
, options
);
2682 if ( fd
== -1 ) return SOCKET_ERROR
;
2684 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
2690 wsa
->hSocket
= SOCKET2HANDLE(s
);
2691 wsa
->addr
= (struct WS_sockaddr
*)to
;
2692 wsa
->addrlen
.val
= tolen
;
2693 wsa
->flags
= dwFlags
;
2694 wsa
->n_iovecs
= dwBufferCount
;
2695 wsa
->first_iovec
= 0;
2696 for ( i
= 0; i
< dwBufferCount
; i
++ )
2698 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
2699 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
2700 totalLength
+= lpBuffers
[i
].len
;
2703 if (!lpNumberOfBytesSent
)
2711 n
= WS2_send( fd
, wsa
);
2712 if (n
!= -1 || errno
!= EINTR
) break;
2714 if (n
== -1 && errno
!= EAGAIN
)
2717 if (cvalue
) WS_AddCompletion( s
, cvalue
, err
, 0 );
2721 if ((lpOverlapped
|| lpCompletionRoutine
) &&
2722 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
2724 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
2726 wsa
->user_overlapped
= lpOverlapped
;
2727 wsa
->completion_func
= lpCompletionRoutine
;
2728 release_sock_fd( s
, fd
);
2732 iosb
->u
.Status
= STATUS_PENDING
;
2733 iosb
->Information
= 0;
2735 SERVER_START_REQ( register_async
)
2737 req
->handle
= wsa
->hSocket
;
2738 req
->type
= ASYNC_TYPE_WRITE
;
2739 req
->async
.callback
= WS2_async_send
;
2740 req
->async
.iosb
= iosb
;
2741 req
->async
.arg
= wsa
;
2742 req
->async
.apc
= ws2_async_apc
;
2743 req
->async
.event
= lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
;
2744 req
->async
.cvalue
= cvalue
;
2745 err
= wine_server_call( req
);
2749 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
2750 WSASetLastError( NtStatusToWSAError( err
));
2751 return SOCKET_ERROR
;
2754 iosb
->u
.Status
= STATUS_SUCCESS
;
2755 iosb
->Information
= n
;
2756 *lpNumberOfBytesSent
= n
;
2757 if (!wsa
->completion_func
)
2759 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
2760 SetEvent( lpOverlapped
->hEvent
);
2761 HeapFree( GetProcessHeap(), 0, wsa
);
2763 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
2764 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
2769 if ( _is_blocking(s
) )
2771 /* On a blocking non-overlapped stream socket,
2772 * sending blocks until the entire buffer is sent. */
2773 DWORD timeout_start
= GetTickCount();
2775 *lpNumberOfBytesSent
= 0;
2777 while (wsa
->first_iovec
< dwBufferCount
)
2780 int timeout
= GET_SNDTIMEO(fd
);
2784 *lpNumberOfBytesSent
+= n
;
2785 while (wsa
->first_iovec
< dwBufferCount
&& wsa
->iovec
[wsa
->first_iovec
].iov_len
<= n
)
2786 n
-= wsa
->iovec
[wsa
->first_iovec
++].iov_len
;
2787 if (wsa
->first_iovec
>= dwBufferCount
) break;
2788 wsa
->iovec
[wsa
->first_iovec
].iov_base
= (char*)wsa
->iovec
[wsa
->first_iovec
].iov_base
+ n
;
2789 wsa
->iovec
[wsa
->first_iovec
].iov_len
-= n
;
2794 timeout
-= GetTickCount() - timeout_start
;
2795 if (timeout
< 0) timeout
= 0;
2799 pfd
.events
= POLLOUT
;
2801 if (!timeout
|| !poll( &pfd
, 1, timeout
))
2804 goto error
; /* msdn says a timeout in send is fatal */
2807 n
= WS2_send( fd
, wsa
);
2808 if (n
== -1 && errno
!= EAGAIN
&& errno
!= EINTR
)
2815 else /* non-blocking */
2817 if (n
< totalLength
)
2818 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
2821 err
= WSAEWOULDBLOCK
;
2824 *lpNumberOfBytesSent
= n
;
2827 TRACE(" -> %i bytes\n", *lpNumberOfBytesSent
);
2829 HeapFree( GetProcessHeap(), 0, wsa
);
2830 release_sock_fd( s
, fd
);
2835 HeapFree( GetProcessHeap(), 0, wsa
);
2836 release_sock_fd( s
, fd
);
2837 WARN(" -> ERROR %d\n", err
);
2838 WSASetLastError(err
);
2839 return SOCKET_ERROR
;
2842 /***********************************************************************
2843 * sendto (WS2_32.20)
2845 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
2846 const struct WS_sockaddr
*to
, int tolen
)
2852 wsabuf
.buf
= (char*) buf
;
2854 if ( WSASendTo(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
2855 return SOCKET_ERROR
;
2860 /***********************************************************************
2861 * setsockopt (WS2_32.21)
2863 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
2864 const char *optval
, int optlen
)
2868 struct linger linger
;
2869 struct timeval tval
;
2871 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
2872 s
, level
, optname
, optval
, optlen
);
2874 /* some broken apps pass the value directly instead of a pointer to it */
2875 if(IS_INTRESOURCE(optval
))
2877 SetLastError(WSAEFAULT
);
2878 return SOCKET_ERROR
;
2886 /* Some options need some conversion before they can be sent to
2887 * setsockopt. The conversions are done here, then they will fall though
2888 * to the general case. Special options that are not passed to
2889 * setsockopt follow below that.*/
2891 case WS_SO_DONTLINGER
:
2892 linger
.l_onoff
= *((const int*)optval
) ? 0: 1;
2893 linger
.l_linger
= 0;
2895 optname
= SO_LINGER
;
2896 optval
= (char*)&linger
;
2897 optlen
= sizeof(struct linger
);
2901 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
2902 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
2903 /* FIXME: what is documented behavior if SO_LINGER optval
2906 optname
= SO_LINGER
;
2907 optval
= (char*)&linger
;
2908 optlen
= sizeof(struct linger
);
2912 if (*(const int*)optval
< 2048)
2914 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
2919 /* The options listed here don't need any special handling. Thanks to
2920 * the conversion happening above, options from there will fall through
2922 case WS_SO_ACCEPTCONN
:
2923 case WS_SO_BROADCAST
:
2925 case WS_SO_KEEPALIVE
:
2926 case WS_SO_OOBINLINE
:
2927 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
2928 * however, using it the BSD way fixes bug 8513 and seems to be what
2929 * most programmers assume, anyway */
2930 case WS_SO_REUSEADDR
:
2933 convert_sockopt(&level
, &optname
);
2936 /* SO_DEBUG is a privileged operation, ignore it. */
2938 TRACE("Ignoring SO_DEBUG\n");
2941 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
2942 * socket. According to MSDN, this option is silently ignored.*/
2943 case WS_SO_DONTROUTE
:
2944 TRACE("Ignoring SO_DONTROUTE\n");
2947 /* Stops two sockets from being bound to the same port. Always happens
2948 * on unix systems, so just drop it. */
2949 case WS_SO_EXCLUSIVEADDRUSE
:
2950 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
2953 /* SO_OPENTYPE does not require a valid socket handle. */
2954 case WS_SO_OPENTYPE
:
2955 if (!optlen
|| optlen
< sizeof(int) || !optval
)
2957 SetLastError(WSAEFAULT
);
2958 return SOCKET_ERROR
;
2960 get_per_thread_data()->opentype
= *(const int *)optval
;
2961 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
2965 case WS_SO_RCVTIMEO
:
2968 case WS_SO_SNDTIMEO
:
2970 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
2971 if (optval
&& optlen
== sizeof(UINT32
)) {
2972 /* WinSock passes milliseconds instead of struct timeval */
2973 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
2974 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
2975 /* min of 500 milliseconds */
2976 if (tval
.tv_sec
== 0 && tval
.tv_usec
< 500000)
2977 tval
.tv_usec
= 500000;
2978 optlen
= sizeof(struct timeval
);
2979 optval
= (char*)&tval
;
2980 } else if (optlen
== sizeof(struct timeval
)) {
2981 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
2983 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
2986 convert_sockopt(&level
, &optname
);
2991 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
2992 SetLastError(WSAENOPROTOOPT
);
2993 return SOCKET_ERROR
;
2995 break; /* case WS_SOL_SOCKET */
3002 fd
= get_sock_fd( s
, 0, NULL
);
3003 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval
, fd
);
3005 /* We try to set the ipx type on ipx socket level. */
3007 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, optlen
) == -1)
3009 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
3010 release_sock_fd( s
, fd
);
3011 return SOCKET_ERROR
;
3016 /* Should we retrieve val using a getsockopt call and then
3017 * set the modified one? */
3018 val
.ipx_pt
= *optval
;
3019 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
3022 release_sock_fd( s
, fd
);
3025 case IPX_FILTERPTYPE
:
3026 /* Sets the receive filter packet type, at the moment we don't support it */
3027 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
3028 /* Returning 0 is better for now than returning a SOCKET_ERROR */
3032 FIXME("opt_name:%x\n", optname
);
3033 return SOCKET_ERROR
;
3035 break; /* case NSPROTO_IPX */
3038 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
3039 case WS_IPPROTO_TCP
:
3042 case WS_TCP_NODELAY
:
3043 convert_sockopt(&level
, &optname
);
3046 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
3047 return SOCKET_ERROR
;
3054 case WS_IP_ADD_MEMBERSHIP
:
3055 case WS_IP_DROP_MEMBERSHIP
:
3059 case WS_IP_MULTICAST_IF
:
3060 case WS_IP_MULTICAST_LOOP
:
3061 case WS_IP_MULTICAST_TTL
:
3065 convert_sockopt(&level
, &optname
);
3067 case WS_IP_DONTFRAGMENT
:
3068 FIXME("IP_DONTFRAGMENT is silently ignored!\n");
3071 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
3072 return SOCKET_ERROR
;
3077 FIXME("Unknown level: 0x%08x\n", level
);
3078 return SOCKET_ERROR
;
3079 } /* end switch(level) */
3081 /* avoid endianness issues if argument is a 16-bit int */
3082 if (optval
&& optlen
< sizeof(int))
3084 woptval
= *((const INT16
*) optval
);
3085 optval
= (char*) &woptval
;
3088 fd
= get_sock_fd( s
, 0, NULL
);
3089 if (fd
== -1) return SOCKET_ERROR
;
3091 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
3093 release_sock_fd( s
, fd
);
3096 TRACE("Setting socket error, %d\n", wsaErrno());
3097 SetLastError(wsaErrno());
3098 release_sock_fd( s
, fd
);
3100 return SOCKET_ERROR
;
3103 /***********************************************************************
3104 * shutdown (WS2_32.22)
3106 int WINAPI
WS_shutdown(SOCKET s
, int how
)
3108 int fd
, err
= WSAENOTSOCK
;
3109 unsigned int options
, clear_flags
= 0;
3111 fd
= get_sock_fd( s
, 0, &options
);
3112 TRACE("socket %04lx, how %i %x\n", s
, how
, options
);
3115 return SOCKET_ERROR
;
3119 case 0: /* drop receives */
3120 clear_flags
|= FD_READ
;
3122 case 1: /* drop sends */
3123 clear_flags
|= FD_WRITE
;
3125 case 2: /* drop all */
3126 clear_flags
|= FD_READ
|FD_WRITE
;
3128 clear_flags
|= FD_WINE_LISTENING
;
3131 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
3136 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
3139 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
3143 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
3144 if (!err
) err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
3147 if (err
) goto error
;
3149 else /* non-overlapped mode */
3151 if ( shutdown( fd
, how
) )
3158 release_sock_fd( s
, fd
);
3159 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
3160 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
3164 release_sock_fd( s
, fd
);
3165 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
3166 WSASetLastError( err
);
3167 return SOCKET_ERROR
;
3170 /***********************************************************************
3171 * socket (WS2_32.23)
3173 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
3175 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
3177 return WSASocketA( af
, type
, protocol
, NULL
, 0,
3178 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
3182 /***********************************************************************
3183 * gethostbyaddr (WS2_32.51)
3185 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
3187 struct WS_hostent
*retval
= NULL
;
3188 struct hostent
* host
;
3190 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3193 struct hostent hostentry
;
3196 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
3198 int res
= gethostbyaddr_r(addr
, len
, type
,
3199 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
3200 if( res
!= ERANGE
) break;
3202 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
3204 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
3206 EnterCriticalSection( &csWSgetXXXbyYYY
);
3207 host
= gethostbyaddr(addr
, len
, type
);
3208 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
3210 if( host
!= NULL
) retval
= WS_dup_he(host
);
3211 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3212 HeapFree(GetProcessHeap(),0,extrabuf
);
3214 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3216 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
3220 /***********************************************************************
3221 * gethostbyname (WS2_32.52)
3223 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
3225 struct WS_hostent
*retval
= NULL
;
3226 struct hostent
* host
;
3227 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3230 struct hostent hostentry
;
3231 int locerr
= ENOBUFS
;
3234 if( !name
|| !name
[0]) {
3236 if( gethostname( buf
, 100) == -1) {
3237 SetLastError( WSAENOBUFS
); /* appropriate ? */
3241 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3243 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
3245 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
3246 if( res
!= ERANGE
) break;
3248 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
3250 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
3252 EnterCriticalSection( &csWSgetXXXbyYYY
);
3253 host
= gethostbyname(name
);
3254 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
3256 if (host
) retval
= WS_dup_he(host
);
3257 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3258 HeapFree(GetProcessHeap(),0,extrabuf
);
3260 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3262 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
3263 strcmp(name
, "localhost") != 0)
3265 /* hostname != "localhost" but has loopback address. replace by our
3266 * special address.*/
3267 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
3269 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
3274 /***********************************************************************
3275 * getprotobyname (WS2_32.53)
3277 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
3279 struct WS_protoent
* retval
= NULL
;
3280 #ifdef HAVE_GETPROTOBYNAME
3281 struct protoent
* proto
;
3282 EnterCriticalSection( &csWSgetXXXbyYYY
);
3283 if( (proto
= getprotobyname(name
)) != NULL
)
3285 retval
= WS_dup_pe(proto
);
3288 MESSAGE("protocol %s not found; You might want to add "
3289 "this to /etc/protocols\n", debugstr_a(name
) );
3290 SetLastError(WSANO_DATA
);
3292 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3294 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
3299 /***********************************************************************
3300 * getprotobynumber (WS2_32.54)
3302 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
3304 struct WS_protoent
* retval
= NULL
;
3305 #ifdef HAVE_GETPROTOBYNUMBER
3306 struct protoent
* proto
;
3307 EnterCriticalSection( &csWSgetXXXbyYYY
);
3308 if( (proto
= getprotobynumber(number
)) != NULL
)
3310 retval
= WS_dup_pe(proto
);
3313 MESSAGE("protocol number %d not found; You might want to add "
3314 "this to /etc/protocols\n", number
);
3315 SetLastError(WSANO_DATA
);
3317 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3319 TRACE("%i ret %p\n", number
, retval
);
3324 /***********************************************************************
3325 * getservbyname (WS2_32.55)
3327 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
3329 struct WS_servent
* retval
= NULL
;
3330 struct servent
* serv
;
3332 char *proto_str
= NULL
;
3334 if (!(name_str
= strdup_lower(name
))) return NULL
;
3336 if (proto
&& *proto
)
3338 if (!(proto_str
= strdup_lower(proto
)))
3340 HeapFree( GetProcessHeap(), 0, name_str
);
3345 EnterCriticalSection( &csWSgetXXXbyYYY
);
3346 serv
= getservbyname(name_str
, proto_str
);
3349 retval
= WS_dup_se(serv
);
3351 else SetLastError(WSANO_DATA
);
3352 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3353 HeapFree( GetProcessHeap(), 0, proto_str
);
3354 HeapFree( GetProcessHeap(), 0, name_str
);
3355 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
3359 /***********************************************************************
3360 * freeaddrinfo (WS2_32.@)
3362 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
3365 struct WS_addrinfo
*next
;
3367 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
3368 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
3369 next
= res
->ai_next
;
3370 HeapFree(GetProcessHeap(),0,res
);
3375 /* helper functions for getaddrinfo()/getnameinfo() */
3376 static int convert_aiflag_w2u(int winflags
) {
3377 int i
, unixflags
= 0;
3379 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
3380 if (ws_aiflag_map
[i
][0] & winflags
) {
3381 unixflags
|= ws_aiflag_map
[i
][1];
3382 winflags
&= ~ws_aiflag_map
[i
][0];
3385 FIXME("Unhandled windows AI_xxx flags %x\n", winflags
);
3389 static int convert_niflag_w2u(int winflags
) {
3390 int i
, unixflags
= 0;
3392 for (i
=0;i
<sizeof(ws_niflag_map
)/sizeof(ws_niflag_map
[0]);i
++)
3393 if (ws_niflag_map
[i
][0] & winflags
) {
3394 unixflags
|= ws_niflag_map
[i
][1];
3395 winflags
&= ~ws_niflag_map
[i
][0];
3398 FIXME("Unhandled windows NI_xxx flags %x\n", winflags
);
3402 static int convert_aiflag_u2w(int unixflags
) {
3403 int i
, winflags
= 0;
3405 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
3406 if (ws_aiflag_map
[i
][1] & unixflags
) {
3407 winflags
|= ws_aiflag_map
[i
][0];
3408 unixflags
&= ~ws_aiflag_map
[i
][1];
3410 if (unixflags
) /* will warn usually */
3411 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags
);
3415 static int convert_eai_u2w(int unixret
) {
3418 for (i
=0;ws_eai_map
[i
][0];i
++)
3419 if (ws_eai_map
[i
][1] == unixret
)
3420 return ws_eai_map
[i
][0];
3424 /***********************************************************************
3425 * getaddrinfo (WS2_32.@)
3427 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
3429 #ifdef HAVE_GETADDRINFO
3430 struct addrinfo
*unixaires
= NULL
;
3432 struct addrinfo unixhints
, *punixhints
= NULL
;
3433 CHAR
*node
= NULL
, *serv
= NULL
;
3436 if (!(node
= strdup_lower(nodename
))) return WSA_NOT_ENOUGH_MEMORY
;
3439 if (!(serv
= strdup_lower(servname
))) {
3440 HeapFree(GetProcessHeap(), 0, node
);
3441 return WSA_NOT_ENOUGH_MEMORY
;
3446 punixhints
= &unixhints
;
3448 memset(&unixhints
, 0, sizeof(unixhints
));
3449 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
3450 if (hints
->ai_family
== 0) /* wildcard, specific to getaddrinfo() */
3451 punixhints
->ai_family
= 0;
3453 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
3454 if (hints
->ai_socktype
== 0) /* wildcard, specific to getaddrinfo() */
3455 punixhints
->ai_socktype
= 0;
3457 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
3458 if (hints
->ai_protocol
== 0) /* wildcard, specific to getaddrinfo() */
3459 punixhints
->ai_protocol
= 0;
3461 punixhints
->ai_protocol
= convert_proto_w2u(hints
->ai_protocol
);
3464 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
3465 result
= getaddrinfo(nodename
, servname
, punixhints
, &unixaires
);
3467 TRACE("%s, %s %p -> %p %d\n", debugstr_a(nodename
), debugstr_a(servname
), hints
, res
, result
);
3469 HeapFree(GetProcessHeap(), 0, node
);
3470 HeapFree(GetProcessHeap(), 0, serv
);
3473 struct addrinfo
*xuai
= unixaires
;
3474 struct WS_addrinfo
**xai
= res
;
3478 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
3484 *xai
= ai
;xai
= &ai
->ai_next
;
3485 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
3486 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
3487 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
3488 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
3489 if (xuai
->ai_canonname
) {
3490 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
3491 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
3492 if (!ai
->ai_canonname
)
3494 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
3496 len
= xuai
->ai_addrlen
;
3497 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
3500 ai
->ai_addrlen
= len
;
3502 int winlen
= ai
->ai_addrlen
;
3504 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
3505 ai
->ai_addrlen
= winlen
;
3509 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
3512 ai
->ai_addrlen
= len
;
3514 xuai
= xuai
->ai_next
;
3516 freeaddrinfo(unixaires
);
3518 result
= convert_eai_u2w(result
);
3524 if (*res
) WS_freeaddrinfo(*res
);
3525 if (unixaires
) freeaddrinfo(unixaires
);
3527 return WSA_NOT_ENOUGH_MEMORY
;
3529 FIXME("getaddrinfo() failed, not found during buildtime.\n");
3534 /***********************************************************************
3535 * GetAddrInfoW (WS2_32.@)
3537 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
3539 FIXME("empty stub!\n");
3543 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
3544 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
3546 #ifdef HAVE_GETNAMEINFO
3548 union generic_unix_sockaddr sa_u
;
3551 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
3552 serv
, servlen
, flags
);
3554 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
3557 WSASetLastError(WSAEFAULT
);
3558 return WSA_NOT_ENOUGH_MEMORY
;
3560 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
3561 return convert_eai_u2w(ret
);
3563 FIXME("getnameinfo() failed, not found during buildtime.\n");
3568 /***********************************************************************
3569 * getservbyport (WS2_32.56)
3571 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
3573 struct WS_servent
* retval
= NULL
;
3574 #ifdef HAVE_GETSERVBYPORT
3575 struct servent
* serv
;
3576 char *proto_str
= NULL
;
3578 if (proto
&& *proto
)
3580 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
3582 EnterCriticalSection( &csWSgetXXXbyYYY
);
3583 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
3584 retval
= WS_dup_se(serv
);
3586 else SetLastError(WSANO_DATA
);
3587 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3588 HeapFree( GetProcessHeap(), 0, proto_str
);
3590 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
3595 /***********************************************************************
3596 * gethostname (WS2_32.57)
3598 int WINAPI
WS_gethostname(char *name
, int namelen
)
3600 TRACE("name %p, len %d\n", name
, namelen
);
3602 if (gethostname(name
, namelen
) == 0)
3604 TRACE("<- '%s'\n", name
);
3607 SetLastError((errno
== EINVAL
) ? WSAEFAULT
: wsaErrno());
3608 TRACE("<- ERROR !\n");
3609 return SOCKET_ERROR
;
3613 /* ------------------------------------- Windows sockets extensions -- *
3615 * ------------------------------------------------------------------- */
3617 /***********************************************************************
3618 * WSAEnumNetworkEvents (WS2_32.36)
3620 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
3624 TRACE("%08lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
3626 SERVER_START_REQ( get_socket_event
)
3628 req
->handle
= SOCKET2HANDLE(s
);
3629 req
->service
= TRUE
;
3630 req
->c_event
= hEvent
;
3631 wine_server_set_reply( req
, lpEvent
->iErrorCode
, sizeof(lpEvent
->iErrorCode
) );
3632 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
3636 SetLastError(WSAEINVAL
);
3637 return SOCKET_ERROR
;
3640 /***********************************************************************
3641 * WSAEventSelect (WS2_32.39)
3643 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
3647 TRACE("%08lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
3649 SERVER_START_REQ( set_socket_event
)
3651 req
->handle
= SOCKET2HANDLE(s
);
3653 req
->event
= hEvent
;
3656 ret
= wine_server_call( req
);
3660 SetLastError(WSAEINVAL
);
3661 return SOCKET_ERROR
;
3664 /**********************************************************************
3665 * WSAGetOverlappedResult (WS2_32.40)
3667 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
3668 LPDWORD lpcbTransfer
, BOOL fWait
,
3673 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
3674 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
3676 if ( lpOverlapped
== NULL
)
3678 ERR( "Invalid pointer\n" );
3679 WSASetLastError(WSA_INVALID_PARAMETER
);
3683 status
= lpOverlapped
->Internal
;
3684 if (status
== STATUS_PENDING
)
3688 SetLastError( WSA_IO_INCOMPLETE
);
3692 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
3693 INFINITE
) == WAIT_FAILED
)
3695 status
= lpOverlapped
->Internal
;
3699 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
3702 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
3704 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
3709 /***********************************************************************
3710 * WSAAsyncSelect (WS2_32.101)
3712 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
3716 TRACE("%lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
3718 SERVER_START_REQ( set_socket_event
)
3720 req
->handle
= SOCKET2HANDLE(s
);
3725 ret
= wine_server_call( req
);
3729 SetLastError(WSAEINVAL
);
3730 return SOCKET_ERROR
;
3733 /***********************************************************************
3734 * WSACreateEvent (WS2_32.31)
3737 WSAEVENT WINAPI
WSACreateEvent(void)
3739 /* Create a manual-reset event, with initial state: unsignaled */
3742 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
3745 /***********************************************************************
3746 * WSACloseEvent (WS2_32.29)
3749 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
3751 TRACE ("event=%p\n", event
);
3753 return CloseHandle(event
);
3756 /***********************************************************************
3757 * WSASocketA (WS2_32.78)
3760 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
3761 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
3762 GROUP g
, DWORD dwFlags
)
3765 WSAPROTOCOL_INFOW info
;
3767 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3768 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
3770 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
3772 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
3773 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
3774 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
3778 WSASetLastError( WSAEINVAL
);
3779 return SOCKET_ERROR
;
3782 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
3785 /***********************************************************************
3786 * WSASocketW (WS2_32.79)
3789 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
3790 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
3791 GROUP g
, DWORD dwFlags
)
3796 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
3797 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
3800 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3801 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
3803 /* hack for WSADuplicateSocket */
3804 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
3805 ret
= lpProtocolInfo
->dwCatalogEntryId
;
3806 TRACE("\tgot duplicate %04lx\n", ret
);
3810 /* convert the socket family and type */
3811 af
= convert_af_w2u(af
);
3812 type
= convert_socktype_w2u(type
);
3816 if (af
== FROM_PROTOCOL_INFO
)
3817 af
= lpProtocolInfo
->iAddressFamily
;
3818 if (type
== FROM_PROTOCOL_INFO
)
3819 type
= lpProtocolInfo
->iSocketType
;
3820 if (protocol
== FROM_PROTOCOL_INFO
)
3821 protocol
= lpProtocolInfo
->iProtocol
;
3824 if ( af
== AF_UNSPEC
) /* did they not specify the address family? */
3828 if (type
== SOCK_STREAM
) { af
= AF_INET
; break; }
3830 if (type
== SOCK_DGRAM
) { af
= AF_INET
; break; }
3831 default: SetLastError(WSAEPROTOTYPE
); return INVALID_SOCKET
;
3834 SERVER_START_REQ( create_socket
)
3838 req
->protocol
= protocol
;
3839 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
3840 req
->attributes
= OBJ_INHERIT
;
3841 req
->flags
= dwFlags
;
3842 set_error( wine_server_call( req
) );
3843 ret
= HANDLE2SOCKET( reply
->handle
);
3848 TRACE("\tcreated %04lx\n", ret
);
3852 if (GetLastError() == WSAEACCES
) /* raw socket denied */
3854 if (type
== SOCK_RAW
)
3855 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, this"
3856 " will fail unless you have special permissions.\n");
3858 MESSAGE("WS_SOCKET: Failed to create socket, this requires"
3859 " special permissions.\n");
3860 SetLastError(WSAESOCKTNOSUPPORT
);
3863 WARN("\t\tfailed!\n");
3864 return INVALID_SOCKET
;
3867 /***********************************************************************
3868 * WSAJoinLeaf (WS2_32.58)
3871 SOCKET WINAPI
WSAJoinLeaf(
3873 const struct WS_sockaddr
*addr
,
3875 LPWSABUF lpCallerData
,
3876 LPWSABUF lpCalleeData
,
3882 return INVALID_SOCKET
;
3885 /***********************************************************************
3886 * __WSAFDIsSet (WS2_32.151)
3888 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
3890 int i
= set
->fd_count
;
3892 TRACE("(%ld,%p(%i))\n", s
, set
, i
);
3895 if (set
->fd_array
[i
] == s
) return 1;
3899 /***********************************************************************
3900 * WSAIsBlocking (WINSOCK.114)
3901 * WSAIsBlocking (WS2_32.114)
3903 BOOL WINAPI
WSAIsBlocking(void)
3905 /* By default WinSock should set all its sockets to non-blocking mode
3906 * and poll in PeekMessage loop when processing "blocking" ones. This
3907 * function is supposed to tell if the program is in this loop. Our
3908 * blocking calls are truly blocking so we always return FALSE.
3910 * Note: It is allowed to call this function without prior WSAStartup().
3917 /***********************************************************************
3918 * WSACancelBlockingCall (WINSOCK.113)
3919 * WSACancelBlockingCall (WS2_32.113)
3921 INT WINAPI
WSACancelBlockingCall(void)
3927 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
3929 FIXME("How was this called?\n");
3934 /***********************************************************************
3935 * WSASetBlockingHook (WS2_32.109)
3937 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
3939 FARPROC prev
= blocking_hook
;
3940 blocking_hook
= lpBlockFunc
;
3941 TRACE("hook %p\n", lpBlockFunc
);
3946 /***********************************************************************
3947 * WSAUnhookBlockingHook (WS2_32.110)
3949 INT WINAPI
WSAUnhookBlockingHook(void)
3951 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
3956 /* ----------------------------------- end of API stuff */
3958 /* ----------------------------------- helper functions -
3960 * TODO: Merge WS_dup_..() stuff into one function that
3961 * would operate with a generic structure containing internal
3962 * pointers (via a template of some kind).
3965 static int list_size(char** l
, int item_size
)
3970 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
3971 j
+= (i
+ 1) * sizeof(char*); }
3975 static int list_dup(char** l_src
, char** l_to
, int item_size
)
3980 for (i
= 0; l_src
[i
]; i
++) ;
3981 p
= (char *)(l_to
+ i
+ 1);
3982 for (i
= 0; l_src
[i
]; i
++)
3984 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
3985 memcpy(p
, l_src
[i
], count
);
3990 return p
- (char *)l_to
;
3995 /* duplicate hostent entry
3996 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
3997 * Ditto for protoent and servent.
3999 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
4002 struct WS_hostent
*p_to
;
4004 int size
= (sizeof(*p_he
) +
4005 strlen(p_he
->h_name
) + 1 +
4006 list_size(p_he
->h_aliases
, 0) +
4007 list_size(p_he
->h_addr_list
, p_he
->h_length
));
4009 if (!(p_to
= check_buffer_he(size
))) return NULL
;
4010 p_to
->h_addrtype
= p_he
->h_addrtype
;
4011 p_to
->h_length
= p_he
->h_length
;
4013 p
= (char *)(p_to
+ 1);
4015 strcpy(p
, p_he
->h_name
);
4018 p_to
->h_aliases
= (char **)p
;
4019 p
+= list_dup(p_he
->h_aliases
, p_to
->h_aliases
, 0);
4021 p_to
->h_addr_list
= (char **)p
;
4022 list_dup(p_he
->h_addr_list
, p_to
->h_addr_list
, p_he
->h_length
);
4026 /* ----- protoent */
4028 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
)
4031 struct WS_protoent
*p_to
;
4033 int size
= (sizeof(*p_pe
) +
4034 strlen(p_pe
->p_name
) + 1 +
4035 list_size(p_pe
->p_aliases
, 0));
4037 if (!(p_to
= check_buffer_pe(size
))) return NULL
;
4038 p_to
->p_proto
= p_pe
->p_proto
;
4040 p
= (char *)(p_to
+ 1);
4042 strcpy(p
, p_pe
->p_name
);
4045 p_to
->p_aliases
= (char **)p
;
4046 list_dup(p_pe
->p_aliases
, p_to
->p_aliases
, 0);
4052 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
4055 struct WS_servent
*p_to
;
4057 int size
= (sizeof(*p_se
) +
4058 strlen(p_se
->s_proto
) + 1 +
4059 strlen(p_se
->s_name
) + 1 +
4060 list_size(p_se
->s_aliases
, 0));
4062 if (!(p_to
= check_buffer_se(size
))) return NULL
;
4063 p_to
->s_port
= p_se
->s_port
;
4065 p
= (char *)(p_to
+ 1);
4067 strcpy(p
, p_se
->s_name
);
4071 strcpy(p
, p_se
->s_proto
);
4074 p_to
->s_aliases
= (char **)p
;
4075 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
4079 /* ----------------------------------- error handling */
4083 int loc_errno
= errno
;
4084 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
4088 case EINTR
: return WSAEINTR
;
4089 case EBADF
: return WSAEBADF
;
4091 case EACCES
: return WSAEACCES
;
4092 case EFAULT
: return WSAEFAULT
;
4093 case EINVAL
: return WSAEINVAL
;
4094 case EMFILE
: return WSAEMFILE
;
4095 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
4096 case EINPROGRESS
: return WSAEINPROGRESS
;
4097 case EALREADY
: return WSAEALREADY
;
4098 case ENOTSOCK
: return WSAENOTSOCK
;
4099 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
4100 case EMSGSIZE
: return WSAEMSGSIZE
;
4101 case EPROTOTYPE
: return WSAEPROTOTYPE
;
4102 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
4103 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
4104 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
4105 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
4106 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
4107 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
4108 case EADDRINUSE
: return WSAEADDRINUSE
;
4109 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
4110 case ENETDOWN
: return WSAENETDOWN
;
4111 case ENETUNREACH
: return WSAENETUNREACH
;
4112 case ENETRESET
: return WSAENETRESET
;
4113 case ECONNABORTED
: return WSAECONNABORTED
;
4115 case ECONNRESET
: return WSAECONNRESET
;
4116 case ENOBUFS
: return WSAENOBUFS
;
4117 case EISCONN
: return WSAEISCONN
;
4118 case ENOTCONN
: return WSAENOTCONN
;
4119 case ESHUTDOWN
: return WSAESHUTDOWN
;
4120 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
4121 case ETIMEDOUT
: return WSAETIMEDOUT
;
4122 case ECONNREFUSED
: return WSAECONNREFUSED
;
4123 case ELOOP
: return WSAELOOP
;
4124 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
4125 case EHOSTDOWN
: return WSAEHOSTDOWN
;
4126 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
4127 case ENOTEMPTY
: return WSAENOTEMPTY
;
4129 case EPROCLIM
: return WSAEPROCLIM
;
4132 case EUSERS
: return WSAEUSERS
;
4135 case EDQUOT
: return WSAEDQUOT
;
4138 case ESTALE
: return WSAESTALE
;
4141 case EREMOTE
: return WSAEREMOTE
;
4144 /* just in case we ever get here and there are no problems */
4147 WARN("Unknown errno %d!\n", loc_errno
);
4148 return WSAEOPNOTSUPP
;
4152 UINT
wsaHerrno(int loc_errno
)
4155 WARN("h_errno %d.\n", loc_errno
);
4159 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
4160 case TRY_AGAIN
: return WSATRY_AGAIN
;
4161 case NO_RECOVERY
: return WSANO_RECOVERY
;
4162 case NO_DATA
: return WSANO_DATA
;
4163 case ENOBUFS
: return WSAENOBUFS
;
4167 WARN("Unknown h_errno %d!\n", loc_errno
);
4168 return WSAEOPNOTSUPP
;
4173 /***********************************************************************
4174 * WSARecv (WS2_32.67)
4176 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4177 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
4178 LPWSAOVERLAPPED lpOverlapped
,
4179 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4181 return WSARecvFrom(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
4182 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
);
4185 /***********************************************************************
4186 * WSARecvFrom (WS2_32.69)
4188 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4189 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
4190 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
4191 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4194 unsigned int i
, options
;
4196 struct ws2_async
*wsa
;
4197 DWORD timeout_start
= GetTickCount();
4198 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
4200 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
4201 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
4202 (lpFromlen
? *lpFromlen
: -1),
4203 lpOverlapped
, lpCompletionRoutine
);
4205 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
4206 TRACE( "fd=%d, options=%x\n", fd
, options
);
4208 if (fd
== -1) return SOCKET_ERROR
;
4210 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
4216 wsa
->hSocket
= SOCKET2HANDLE(s
);
4217 wsa
->flags
= *lpFlags
;
4219 wsa
->addrlen
.ptr
= lpFromlen
;
4220 wsa
->n_iovecs
= dwBufferCount
;
4221 wsa
->first_iovec
= 0;
4222 for (i
= 0; i
< dwBufferCount
; i
++)
4224 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
4225 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
4230 n
= WS2_recv( fd
, wsa
);
4233 if (errno
== EINTR
) continue;
4234 if (errno
!= EAGAIN
)
4237 if (cvalue
) WS_AddCompletion( s
, cvalue
, err
, 0 );
4242 *lpNumberOfBytesRecvd
= n
;
4244 if ((lpOverlapped
|| lpCompletionRoutine
) &&
4245 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
4247 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
4249 wsa
->user_overlapped
= lpOverlapped
;
4250 wsa
->completion_func
= lpCompletionRoutine
;
4251 release_sock_fd( s
, fd
);
4255 iosb
->u
.Status
= STATUS_PENDING
;
4256 iosb
->Information
= 0;
4258 SERVER_START_REQ( register_async
)
4260 req
->handle
= wsa
->hSocket
;
4261 req
->type
= ASYNC_TYPE_READ
;
4262 req
->async
.callback
= WS2_async_recv
;
4263 req
->async
.iosb
= iosb
;
4264 req
->async
.arg
= wsa
;
4265 req
->async
.apc
= ws2_async_apc
;
4266 req
->async
.event
= lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
;
4267 req
->async
.cvalue
= cvalue
;
4268 err
= wine_server_call( req
);
4272 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
4273 WSASetLastError( NtStatusToWSAError( err
));
4274 return SOCKET_ERROR
;
4277 iosb
->u
.Status
= STATUS_SUCCESS
;
4278 iosb
->Information
= n
;
4279 if (!wsa
->completion_func
)
4281 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
4282 SetEvent( lpOverlapped
->hEvent
);
4283 HeapFree( GetProcessHeap(), 0, wsa
);
4285 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
4286 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
4287 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4293 if ( _is_blocking(s
) )
4296 int timeout
= GET_RCVTIMEO(fd
);
4299 timeout
-= GetTickCount() - timeout_start
;
4300 if (timeout
< 0) timeout
= 0;
4304 pfd
.events
= POLLIN
;
4305 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
4307 if (!timeout
|| !poll( &pfd
, 1, timeout
))
4310 /* a timeout is not fatal */
4311 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4317 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4318 err
= WSAEWOULDBLOCK
;
4323 TRACE(" -> %i bytes\n", n
);
4324 HeapFree( GetProcessHeap(), 0, wsa
);
4325 release_sock_fd( s
, fd
);
4326 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4331 HeapFree( GetProcessHeap(), 0, wsa
);
4332 release_sock_fd( s
, fd
);
4333 WARN(" -> ERROR %d\n", err
);
4334 WSASetLastError( err
);
4335 return SOCKET_ERROR
;
4338 /***********************************************************************
4339 * WSCInstallProvider (WS2_32.88)
4341 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
4342 LPCWSTR lpszProviderDllPath
,
4343 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
4344 DWORD dwNumberOfEntries
,
4347 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
4348 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
4349 dwNumberOfEntries
, lpErrno
);
4355 /***********************************************************************
4356 * WSCDeinstallProvider (WS2_32.83)
4358 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
4360 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
4366 /***********************************************************************
4367 * WSAAccept (WS2_32.26)
4369 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
4370 LPCONDITIONPROC lpfnCondition
, DWORD dwCallbackData
)
4373 int ret
= 0, size
= 0;
4374 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
4375 /* QOS SQOS, GQOS; */
4378 SOCKADDR src_addr
, dst_addr
;
4380 TRACE("Socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %d\n",
4381 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
4384 size
= sizeof(src_addr
);
4385 cs
= WS_accept(s
, &src_addr
, &size
);
4387 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
4389 CallerId
.buf
= (char *)&src_addr
;
4390 CallerId
.len
= sizeof(src_addr
);
4392 CallerData
.buf
= NULL
;
4393 CallerData
.len
= (ULONG
)NULL
;
4395 WS_getsockname(cs
, &dst_addr
, &size
);
4397 CalleeId
.buf
= (char *)&dst_addr
;
4398 CalleeId
.len
= sizeof(dst_addr
);
4401 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
4402 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
4407 if (addr
&& addrlen
)
4408 addr
= memcpy(addr
, &src_addr
, (*addrlen
> size
) ? size
: *addrlen
);
4411 SERVER_START_REQ( set_socket_deferred
)
4413 req
->handle
= SOCKET2HANDLE(s
);
4414 req
->deferred
= SOCKET2HANDLE(cs
);
4415 if ( !wine_server_call_err ( req
) )
4417 SetLastError( WSATRY_AGAIN
);
4418 WS_closesocket( cs
);
4422 return SOCKET_ERROR
;
4425 SetLastError(WSAECONNREFUSED
);
4426 return SOCKET_ERROR
;
4428 FIXME("Unknown return type from Condition function\n");
4429 SetLastError(WSAENOTSOCK
);
4430 return SOCKET_ERROR
;
4434 /***********************************************************************
4435 * WSADuplicateSocketA (WS2_32.32)
4437 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
4441 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
4442 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
4443 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
4444 /* I don't know what the real Windoze does next, this is a hack */
4445 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
4446 * the target use the global duplicate, or we could copy a reference to us to the structure
4447 * and let the target duplicate it from us, but let's do it as simple as possible */
4448 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
4449 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
4450 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
4451 0, FALSE
, DUPLICATE_SAME_ACCESS
);
4452 CloseHandle(hProcess
);
4453 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
4457 /***********************************************************************
4458 * WSADuplicateSocketW (WS2_32.33)
4460 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
4464 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
4466 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
4467 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
4468 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
4469 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
4470 0, FALSE
, DUPLICATE_SAME_ACCESS
);
4471 CloseHandle(hProcess
);
4472 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
4476 /***********************************************************************
4477 * WSAInstallServiceClassA (WS2_32.48)
4479 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
4481 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
4482 WSASetLastError(WSAEACCES
);
4483 return SOCKET_ERROR
;
4486 /***********************************************************************
4487 * WSAInstallServiceClassW (WS2_32.49)
4489 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
4491 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
4492 WSASetLastError(WSAEACCES
);
4493 return SOCKET_ERROR
;
4496 /***********************************************************************
4497 * WSARemoveServiceClass (WS2_32.70)
4499 int WINAPI
WSARemoveServiceClass(LPGUID info
)
4501 FIXME("Request to remove service %p\n",info
);
4502 WSASetLastError(WSATYPE_NOT_FOUND
);
4503 return SOCKET_ERROR
;
4506 /***********************************************************************
4507 * WSAStringToAddressA (WS2_32.80)
4509 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
4511 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
4512 LPSOCKADDR lpAddress
,
4513 LPINT lpAddressLength
)
4516 LPSTR workBuffer
=NULL
,ptrPort
;
4518 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_a(AddressString
), AddressFamily
,
4519 lpProtocolInfo
, lpAddress
, lpAddressLength
);
4521 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
4525 WSASetLastError(WSAEINVAL
);
4526 return SOCKET_ERROR
;
4530 FIXME("ProtocolInfo not implemented.\n");
4532 workBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
4533 strlen(AddressString
) + 1);
4536 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4537 return SOCKET_ERROR
;
4540 strcpy(workBuffer
, AddressString
);
4542 switch(AddressFamily
)
4546 struct in_addr inetaddr
;
4548 /* If lpAddressLength is too small, tell caller the size we need */
4549 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
4551 *lpAddressLength
= sizeof(SOCKADDR_IN
);
4555 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
4557 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= AF_INET
;
4559 ptrPort
= strchr(workBuffer
, ':');
4562 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= (WS_u_short
)atoi(ptrPort
+1);
4567 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= 0;
4570 if(inet_aton(workBuffer
, &inetaddr
) > 0)
4572 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
4583 struct in6_addr inetaddr
;
4584 /* If lpAddressLength is too small, tell caller the size we need */
4585 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
4587 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
4591 #ifdef HAVE_INET_PTON
4592 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
4594 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_family
= WS_AF_INET6
;
4596 /* This one is a bit tricky. An IPv6 address contains colons, so the
4597 * check from IPv4 doesn't work like that. However, IPv6 addresses that
4598 * contain a port are written with braces like [fd12:3456:7890::1]:12345
4599 * so what we will do is to look for ']', check if the next char is a
4600 * colon, and if it is, parse the port as in IPv4. */
4602 ptrPort
= strchr(workBuffer
, ']');
4603 if(ptrPort
&& *(++ptrPort
) == ':')
4605 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= (WS_u_short
)atoi(ptrPort
+1);
4610 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= 0;
4613 if(inet_pton(AF_INET6
, workBuffer
, &inetaddr
) > 0)
4615 memcpy(&((LPSOCKADDR_IN6
)lpAddress
)->sin6_addr
, &inetaddr
,
4616 sizeof(struct in6_addr
));
4620 #endif /* HAVE_INET_PTON */
4626 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
4627 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
4631 HeapFree(GetProcessHeap(), 0, workBuffer
);
4634 WSASetLastError(res
);
4635 return SOCKET_ERROR
;
4638 /***********************************************************************
4639 * WSAStringToAddressW (WS2_32.81)
4641 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
4642 * If this should be the case, it would be required to map these digits
4643 * to Unicode digits (0-9) using FoldString first.
4645 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
4647 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
4648 LPSOCKADDR lpAddress
,
4649 LPINT lpAddressLength
)
4652 LPSTR workBuffer
=NULL
;
4653 WSAPROTOCOL_INFOA infoA
;
4654 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
4656 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
4657 lpAddress
, lpAddressLength
);
4659 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
4661 /* if ProtocolInfo is available - convert to ANSI variant */
4664 lpProtoInfoA
= &infoA
;
4665 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
4667 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
4668 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
4670 WSASetLastError( WSAEINVAL
);
4671 return SOCKET_ERROR
;
4677 /* Translate AddressString to ANSI code page - assumes that only
4678 standard digits 0-9 are used with this API call */
4679 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
4680 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
4684 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
4685 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
4686 lpAddress
,lpAddressLength
);
4687 HeapFree( GetProcessHeap(), 0, workBuffer
);
4691 res
= WSA_NOT_ENOUGH_MEMORY
;
4696 WSASetLastError(res
);
4697 return SOCKET_ERROR
;
4700 /***********************************************************************
4701 * WSAAddressToStringA (WS2_32.27)
4703 * See WSAAddressToStringW
4705 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
4706 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
4710 CHAR buffer
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4713 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
4715 if (!sockaddr
|| len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
4716 if (!string
|| !lenstr
) return SOCKET_ERROR
;
4718 /* sin_family is guaranteed to be the first u_short */
4719 if (((SOCKADDR_IN
*)sockaddr
)->sin_family
!= AF_INET
) return SOCKET_ERROR
;
4721 sprintf( buffer
, "%u.%u.%u.%u:%u",
4722 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
4723 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
4724 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
4725 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
4726 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
4728 p
= strchr( buffer
, ':' );
4729 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
4731 size
= strlen( buffer
);
4736 WSASetLastError(WSAEFAULT
);
4737 return SOCKET_ERROR
;
4740 strcpy( string
, buffer
);
4744 /***********************************************************************
4745 * WSAAddressToStringW (WS2_32.28)
4747 * Convert a sockaddr address into a readable address string.
4750 * sockaddr [I] Pointer to a sockaddr structure.
4751 * len [I] Size of the sockaddr structure.
4752 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
4753 * string [I/O] Pointer to a buffer to receive the address string.
4754 * lenstr [I/O] Size of the receive buffer in WCHARs.
4758 * Failure: SOCKET_ERROR
4761 * The 'info' parameter is ignored.
4764 * Only supports AF_INET addresses.
4766 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
4767 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
4771 WCHAR buffer
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4772 static const WCHAR format
[] = { '%','u','.','%','u','.','%','u','.','%','u',':','%','u',0 };
4775 TRACE( "(%p, %x, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
4777 if (!sockaddr
|| len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
4778 if (!string
|| !lenstr
) return SOCKET_ERROR
;
4780 /* sin_family is guaranteed to be the first u_short */
4781 if (((SOCKADDR_IN
*)sockaddr
)->sin_family
!= AF_INET
) return SOCKET_ERROR
;
4783 sprintfW( buffer
, format
,
4784 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
4785 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
4786 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
4787 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
4788 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
4790 p
= strchrW( buffer
, ':' );
4791 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
4793 size
= lstrlenW( buffer
);
4798 return SOCKET_ERROR
;
4801 lstrcpyW( string
, buffer
);
4805 /***********************************************************************
4806 * WSAEnumNameSpaceProvidersA (WS2_32.34)
4808 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
4810 FIXME( "(%p %p) Stub!\n", len
, buffer
);
4814 /***********************************************************************
4815 * WSAEnumNameSpaceProvidersW (WS2_32.35)
4817 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
4819 FIXME( "(%p %p) Stub!\n", len
, buffer
);
4823 /***********************************************************************
4824 * WSAGetQOSByName (WS2_32.41)
4826 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
4828 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
4832 /***********************************************************************
4833 * WSAGetServiceClassInfoA (WS2_32.42)
4835 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
4836 LPWSASERVICECLASSINFOA info
)
4838 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
4840 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4841 return SOCKET_ERROR
;
4844 /***********************************************************************
4845 * WSAGetServiceClassInfoW (WS2_32.43)
4847 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
4848 LPWSASERVICECLASSINFOW info
)
4850 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
4852 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4853 return SOCKET_ERROR
;
4856 /***********************************************************************
4857 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
4859 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
4861 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
4862 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4863 return SOCKET_ERROR
;
4866 /***********************************************************************
4867 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
4869 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
4871 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
4872 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4873 return SOCKET_ERROR
;
4876 /***********************************************************************
4877 * WSALookupServiceBeginA (WS2_32.59)
4879 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
4880 DWORD dwControlFlags
,
4883 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
4885 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4886 return SOCKET_ERROR
;
4889 /***********************************************************************
4890 * WSALookupServiceBeginW (WS2_32.60)
4892 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
4893 DWORD dwControlFlags
,
4896 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
4898 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4899 return SOCKET_ERROR
;
4902 /***********************************************************************
4903 * WSALookupServiceBeginW (WS2_32.61)
4905 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
4907 FIXME("(%p) Stub!\n", lookup
);
4911 /***********************************************************************
4912 * WSALookupServiceNextA (WS2_32.62)
4914 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
4916 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
4920 /***********************************************************************
4921 * WSALookupServiceNextW (WS2_32.63)
4923 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
4925 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
4929 /***********************************************************************
4930 * WSANtohl (WS2_32.64)
4932 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
4934 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
4936 if (!lphostlong
) return WSAEFAULT
;
4938 *lphostlong
= ntohl( netlong
);
4942 /***********************************************************************
4943 * WSANtohs (WS2_32.65)
4945 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
4947 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
4949 if (!lphostshort
) return WSAEFAULT
;
4951 *lphostshort
= ntohs( netshort
);
4955 /***********************************************************************
4956 * WSAProviderConfigChange (WS2_32.66)
4958 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
4959 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
4961 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
4962 return SOCKET_ERROR
;
4965 /***********************************************************************
4966 * WSARecvDisconnect (WS2_32.68)
4968 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
4970 TRACE( "(0x%04lx %p)\n", s
, disconnectdata
);
4972 return WS_shutdown( s
, 0 );
4975 /***********************************************************************
4976 * WSASetServiceA (WS2_32.76)
4978 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
4980 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
4984 /***********************************************************************
4985 * WSASetServiceW (WS2_32.77)
4987 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
4989 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
4993 /***********************************************************************
4994 * WSCEnableNSProvider (WS2_32.84)
4996 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
4998 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
5002 /***********************************************************************
5003 * WSCGetProviderPath (WS2_32.86)
5005 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
5007 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
5009 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
5011 *errcode
= WSAEINVAL
;
5012 return SOCKET_ERROR
;
5015 /***********************************************************************
5016 * WSCInstallNameSpace (WS2_32.87)
5018 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
5019 DWORD version
, LPGUID provider
)
5021 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
5022 namespace, version
, debugstr_guid(provider
) );
5026 /***********************************************************************
5027 * WSCUnInstallNameSpace (WS2_32.89)
5029 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
5031 FIXME("(%p) Stub!\n", lpProviderId
);
5035 /***********************************************************************
5036 * WSCWriteProviderOrder (WS2_32.91)
5038 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
5040 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);