2 * based on Windows Sockets 1.1 specs
3 * (ftp.microsoft.com:/Advsys/winsock/spec11/WINSOCK.TXT)
5 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
6 * Copyright (C) 2005 Marcus Meissner
7 * Copyright (C) 2006 Kai Blin
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * NOTE: If you make any changes to fix a particular app, make sure
24 * they don't break something else like Netscape or telnet and ftp
25 * clients and servers (www.winsite.com got a lot of those).
29 #include "wine/port.h"
34 #include <sys/types.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 # include <sys/ioctl.h>
41 #ifdef HAVE_SYS_FILIO_H
42 # include <sys/filio.h>
44 #ifdef HAVE_SYS_SOCKIO_H
45 # include <sys/sockio.h>
49 # include <sys/so_ioctl.h>
52 #ifdef HAVE_SYS_PARAM_H
53 # include <sys/param.h>
59 #ifdef HAVE_SYS_WAIT_H
60 # include <sys/wait.h>
65 #ifdef HAVE_SYS_SOCKET_H
66 #include <sys/socket.h>
68 #ifdef HAVE_NETINET_IN_H
69 # include <netinet/in.h>
71 #ifdef HAVE_NETINET_TCP_H
72 # include <netinet/tcp.h>
74 #ifdef HAVE_ARPA_INET_H
75 # include <arpa/inet.h>
80 #ifdef HAVE_SYS_ERRNO_H
81 #include <sys/errno.h>
90 #ifdef HAVE_ARPA_NAMESER_H
91 # include <arpa/nameser.h>
100 #ifdef HAVE_NETIPX_IPX_H
101 # include <netipx/ipx.h>
103 #elif defined(HAVE_LINUX_IPX_H)
104 # ifdef HAVE_ASM_TYPES_H
105 # include <asm/types.h>
107 # include <linux/ipx.h>
114 #ifdef HAVE_SYS_POLL_H
115 # include <sys/poll.h>
117 #ifdef HAVE_SYS_TIME_H
118 # include <sys/time.h>
121 #define NONAMELESSUNION
122 #define NONAMELESSSTRUCT
123 #include "ntstatus.h"
124 #define WIN32_NO_STATUS
129 #include "winerror.h"
131 #include "winsock2.h"
133 #include "ws2tcpip.h"
137 #include "iphlpapi.h"
139 #include "wine/server.h"
140 #include "wine/debug.h"
141 #include "wine/unicode.h"
144 # include "wsnwlink.h"
148 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
149 # define sipx_network sipx_addr.x_net
150 # define sipx_node sipx_addr.x_host.c_host
151 #endif /* __FreeBSD__ */
154 #define INADDR_NONE ~0UL
157 WINE_DEFAULT_DEBUG_CHANNEL(winsock
);
159 /* critical section to protect some non-rentrant net function */
160 extern CRITICAL_SECTION csWSgetXXXbyYYY
;
162 union generic_unix_sockaddr
164 struct sockaddr addr
;
165 char data
[128]; /* should be big enough for all families */
168 static inline const char *debugstr_sockaddr( const struct WS_sockaddr
*a
)
170 if (!a
) return "(nil)";
171 return wine_dbg_sprintf("{ family %d, address %s, port %d }",
172 ((const struct sockaddr_in
*)a
)->sin_family
,
173 inet_ntoa(((const struct sockaddr_in
*)a
)->sin_addr
),
174 ntohs(((const struct sockaddr_in
*)a
)->sin_port
));
177 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
178 #define SOCKET2HANDLE(s) ((HANDLE)(s))
179 #define HANDLE2SOCKET(h) ((SOCKET)(h))
181 /****************************************************************
182 * Async IO declarations
183 ****************************************************************/
185 typedef struct ws2_async
188 enum ws2_mode
{ws2m_read
, ws2m_write
, ws2m_sd_read
, ws2m_sd_write
} mode
;
189 LPWSAOVERLAPPED user_overlapped
;
190 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func
;
191 IO_STATUS_BLOCK local_iosb
;
194 struct WS_sockaddr
*addr
;
197 int val
; /* for send operations */
198 int *ptr
; /* for recv operations */
204 /****************************************************************/
206 /* ----------------------------------- internal data */
208 /* ws_... struct conversion flags */
210 typedef struct /* WSAAsyncSelect() control struct */
212 HANDLE service
, event
, sock
;
218 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
219 #define WS_MAX_UDP_DATAGRAM 1024
220 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
222 /* hostent's, servent's and protent's are stored in one buffer per thread,
223 * as documented on MSDN for the functions that return any of the buffers */
224 struct per_thread_data
227 struct WS_hostent
*he_buffer
;
228 struct WS_servent
*se_buffer
;
229 struct WS_protoent
*pe_buffer
;
235 static INT num_startup
; /* reference counter */
236 static FARPROC blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
238 /* function prototypes */
239 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
240 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
);
241 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
243 int WSAIOCTL_GetInterfaceCount(void);
244 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
247 UINT
wsaHerrno(int errnr
);
249 #define MAP_OPTION(opt) { WS_##opt, opt }
251 static const int ws_sock_map
[][2] =
253 MAP_OPTION( SO_DEBUG
),
254 MAP_OPTION( SO_ACCEPTCONN
),
255 MAP_OPTION( SO_REUSEADDR
),
256 MAP_OPTION( SO_KEEPALIVE
),
257 MAP_OPTION( SO_DONTROUTE
),
258 MAP_OPTION( SO_BROADCAST
),
259 MAP_OPTION( SO_LINGER
),
260 MAP_OPTION( SO_OOBINLINE
),
261 MAP_OPTION( SO_SNDBUF
),
262 MAP_OPTION( SO_RCVBUF
),
263 MAP_OPTION( SO_ERROR
),
264 MAP_OPTION( SO_TYPE
),
266 MAP_OPTION( SO_RCVTIMEO
),
269 MAP_OPTION( SO_SNDTIMEO
),
273 static const int ws_tcp_map
[][2] =
276 MAP_OPTION( TCP_NODELAY
),
280 static const int ws_ip_map
[][2] =
282 MAP_OPTION( IP_MULTICAST_IF
),
283 MAP_OPTION( IP_MULTICAST_TTL
),
284 MAP_OPTION( IP_MULTICAST_LOOP
),
285 MAP_OPTION( IP_ADD_MEMBERSHIP
),
286 MAP_OPTION( IP_DROP_MEMBERSHIP
),
287 MAP_OPTION( IP_OPTIONS
),
289 MAP_OPTION( IP_HDRINCL
),
291 MAP_OPTION( IP_TOS
),
292 MAP_OPTION( IP_TTL
),
295 static const int ws_af_map
[][2] =
297 MAP_OPTION( AF_UNSPEC
),
298 MAP_OPTION( AF_INET
),
299 MAP_OPTION( AF_INET6
),
301 MAP_OPTION( AF_IPX
),
305 static const int ws_socktype_map
[][2] =
307 MAP_OPTION( SOCK_DGRAM
),
308 MAP_OPTION( SOCK_STREAM
),
309 MAP_OPTION( SOCK_RAW
),
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
),
322 static const int ws_aiflag_map
[][2] =
324 MAP_OPTION( AI_PASSIVE
),
325 MAP_OPTION( AI_CANONNAME
),
326 MAP_OPTION( AI_NUMERICHOST
),
327 /* Linux/UNIX knows a lot more. But Windows only
328 * has 3 as far as I could see. -Marcus
332 static const int ws_eai_map
[][2] =
334 MAP_OPTION( EAI_AGAIN
),
335 MAP_OPTION( EAI_BADFLAGS
),
336 MAP_OPTION( EAI_FAIL
),
337 MAP_OPTION( EAI_FAMILY
),
338 MAP_OPTION( EAI_MEMORY
),
339 /* Note: EAI_NODATA is deprecated, but still
340 * used by Windows and Linux... We map the newer
341 * EAI_NONAME to EAI_NODATA for now until Windows
345 MAP_OPTION( EAI_NODATA
),
348 { WS_EAI_NODATA
, EAI_NONAME
},
351 MAP_OPTION( EAI_SERVICE
),
352 MAP_OPTION( EAI_SOCKTYPE
),
356 static inline DWORD
NtStatusToWSAError( const DWORD status
)
358 /* We only need to cover the status codes set by server async request handling */
362 case STATUS_SUCCESS
: wserr
= 0; break;
363 case STATUS_PENDING
: wserr
= WSA_IO_PENDING
; break;
364 case STATUS_INVALID_HANDLE
: wserr
= WSAENOTSOCK
; break; /* WSAEBADF ? */
365 case STATUS_INVALID_PARAMETER
: wserr
= WSAEINVAL
; break;
366 case STATUS_PIPE_DISCONNECTED
: wserr
= WSAESHUTDOWN
; break;
367 case STATUS_CANCELLED
: wserr
= WSA_OPERATION_ABORTED
; break;
368 case STATUS_TIMEOUT
: wserr
= WSAETIMEDOUT
; break;
369 case STATUS_NO_MEMORY
: wserr
= WSAEFAULT
; break;
371 if ( status
>= WSABASEERR
&& status
<= WSABASEERR
+1004 )
372 /* It is not an NT status code but a winsock error */
376 wserr
= RtlNtStatusToDosError( status
);
377 FIXME( "Status code %08x converted to DOS error code %x\n", status
, wserr
);
383 /* set last error code from NT status without mapping WSA errors */
384 static inline unsigned int set_error( unsigned int err
)
388 err
= NtStatusToWSAError( err
);
394 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
397 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
402 static inline void release_sock_fd( SOCKET s
, int fd
)
404 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
407 static void _enable_event( HANDLE s
, unsigned int event
,
408 unsigned int sstate
, unsigned int cstate
)
410 SERVER_START_REQ( enable_socket_event
)
414 req
->sstate
= sstate
;
415 req
->cstate
= cstate
;
416 wine_server_call( req
);
421 static int _is_blocking(SOCKET s
)
424 SERVER_START_REQ( get_socket_event
)
426 req
->handle
= SOCKET2HANDLE(s
);
427 req
->service
= FALSE
;
429 wine_server_call( req
);
430 ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
436 static unsigned int _get_sock_mask(SOCKET s
)
439 SERVER_START_REQ( get_socket_event
)
441 req
->handle
= SOCKET2HANDLE(s
);
442 req
->service
= FALSE
;
444 wine_server_call( req
);
451 static void _sync_sock_state(SOCKET s
)
453 /* do a dummy wineserver request in order to let
454 the wineserver run through its select loop once */
455 (void)_is_blocking(s
);
458 static int _get_sock_error(SOCKET s
, unsigned int bit
)
460 int events
[FD_MAX_EVENTS
];
462 SERVER_START_REQ( get_socket_event
)
464 req
->handle
= SOCKET2HANDLE(s
);
465 req
->service
= FALSE
;
467 wine_server_set_reply( req
, events
, sizeof(events
) );
468 wine_server_call( req
);
474 static struct per_thread_data
*get_per_thread_data(void)
476 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
477 /* lazy initialization */
480 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
481 NtCurrentTeb()->WinSockData
= ptb
;
486 static void free_per_thread_data(void)
488 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
492 /* delete scratch buffers */
493 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
494 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
495 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
496 ptb
->he_buffer
= NULL
;
497 ptb
->se_buffer
= NULL
;
498 ptb
->pe_buffer
= NULL
;
500 HeapFree( GetProcessHeap(), 0, ptb
);
501 NtCurrentTeb()->WinSockData
= NULL
;
504 /***********************************************************************
505 * DllMain (WS2_32.init)
507 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
509 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
511 case DLL_PROCESS_ATTACH
:
513 case DLL_PROCESS_DETACH
:
514 free_per_thread_data();
517 case DLL_THREAD_DETACH
:
518 free_per_thread_data();
524 /***********************************************************************
527 * Converts socket flags from Windows format.
528 * Return 1 if converted, 0 if not (error).
530 static int convert_sockopt(INT
*level
, INT
*optname
)
537 for(i
=0; i
<sizeof(ws_sock_map
)/sizeof(ws_sock_map
[0]); i
++) {
538 if( ws_sock_map
[i
][0] == *optname
)
540 *optname
= ws_sock_map
[i
][1];
544 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
547 *level
= IPPROTO_TCP
;
548 for(i
=0; i
<sizeof(ws_tcp_map
)/sizeof(ws_tcp_map
[0]); i
++) {
549 if ( ws_tcp_map
[i
][0] == *optname
)
551 *optname
= ws_tcp_map
[i
][1];
555 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
559 for(i
=0; i
<sizeof(ws_ip_map
)/sizeof(ws_ip_map
[0]); i
++) {
560 if (ws_ip_map
[i
][0] == *optname
)
562 *optname
= ws_ip_map
[i
][1];
566 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
568 default: FIXME("Unimplemented or unknown socket level\n");
573 static inline BOOL
is_timeout_option( int optname
)
576 if (optname
== SO_RCVTIMEO
) return TRUE
;
579 if (optname
== SO_SNDTIMEO
) return TRUE
;
584 /* ----------------------------------- Per-thread info (or per-process?) */
586 static char *strdup_lower(const char *str
)
589 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
593 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
596 else SetLastError(WSAENOBUFS
);
600 static inline int sock_error_p(int s
)
602 unsigned int optval
, optlen
;
604 optlen
= sizeof(optval
);
605 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
606 if (optval
) WARN("\t[%i] error: %d\n", s
, optval
);
610 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
611 * from an fd and return the value converted to milli seconds
612 * or -1 if there is an infinite time out */
613 static inline int get_rcvsnd_timeo( int fd
, int optname
)
616 unsigned int len
= sizeof(tv
);
617 int ret
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
619 ret
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
620 if( ret
<= 0 ) /* tv == {0,0} means infinite time out */
625 /* macro wrappers for portability */
627 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
629 #define GET_RCVTIMEO(fd) (-1)
633 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
635 #define GET_SNDTIMEO(fd) (-1)
638 /* utility: given an fd, will block until one of the events occurs */
639 static inline int do_block( int fd
, int events
, int timeout
)
647 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
658 convert_af_w2u(int windowsaf
) {
661 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
662 if (ws_af_map
[i
][0] == windowsaf
)
663 return ws_af_map
[i
][1];
664 FIXME("unhandled Windows address family %d\n", windowsaf
);
669 convert_af_u2w(int unixaf
) {
672 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
673 if (ws_af_map
[i
][1] == unixaf
)
674 return ws_af_map
[i
][0];
675 FIXME("unhandled UNIX address family %d\n", unixaf
);
680 convert_proto_w2u(int windowsproto
) {
683 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
684 if (ws_proto_map
[i
][0] == windowsproto
)
685 return ws_proto_map
[i
][1];
686 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
691 convert_proto_u2w(int unixproto
) {
694 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
695 if (ws_proto_map
[i
][1] == unixproto
)
696 return ws_proto_map
[i
][0];
697 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
702 convert_socktype_w2u(int windowssocktype
) {
705 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
706 if (ws_socktype_map
[i
][0] == windowssocktype
)
707 return ws_socktype_map
[i
][1];
708 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
713 convert_socktype_u2w(int unixsocktype
) {
716 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
717 if (ws_socktype_map
[i
][1] == unixsocktype
)
718 return ws_socktype_map
[i
][0];
719 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
723 /* ----------------------------------- API -----
725 * Init / cleanup / error checking.
728 /***********************************************************************
729 * WSAStartup (WS2_32.115)
731 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
733 TRACE("verReq=%x\n", wVersionRequested
);
735 if (LOBYTE(wVersionRequested
) < 1)
736 return WSAVERNOTSUPPORTED
;
738 if (!lpWSAData
) return WSAEINVAL
;
742 /* that's the whole of the negotiation for now */
743 lpWSAData
->wVersion
= wVersionRequested
;
744 /* return winsock information */
745 lpWSAData
->wHighVersion
= 0x0202;
746 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
747 strcpy(lpWSAData
->szSystemStatus
, "Running" );
748 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
749 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
750 /* don't do anything with lpWSAData->lpVendorInfo */
751 /* (some apps don't allocate the space for this field) */
753 TRACE("succeeded\n");
758 /***********************************************************************
759 * WSACleanup (WS2_32.116)
761 INT WINAPI
WSACleanup(void)
767 SetLastError(WSANOTINITIALISED
);
772 /***********************************************************************
773 * WSAGetLastError (WINSOCK.111)
774 * WSAGetLastError (WS2_32.111)
776 INT WINAPI
WSAGetLastError(void)
778 return GetLastError();
781 /***********************************************************************
782 * WSASetLastError (WS2_32.112)
784 void WINAPI
WSASetLastError(INT iError
) {
785 SetLastError(iError
);
788 static struct WS_hostent
*check_buffer_he(int size
)
790 struct per_thread_data
* ptb
= get_per_thread_data();
793 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
794 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
796 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
797 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
798 return ptb
->he_buffer
;
801 static struct WS_servent
*check_buffer_se(int size
)
803 struct per_thread_data
* ptb
= get_per_thread_data();
806 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
807 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
809 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
810 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
811 return ptb
->se_buffer
;
814 static struct WS_protoent
*check_buffer_pe(int size
)
816 struct per_thread_data
* ptb
= get_per_thread_data();
819 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
820 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
822 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
823 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
824 return ptb
->pe_buffer
;
827 /* ----------------------------------- i/o APIs */
830 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf)== WS_AF_IPX || (pf) == WS_AF_INET6)
832 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf) == WS_AF_INET6)
836 /**********************************************************************/
838 /* Returns the length of the converted address if successful, 0 if it was too small to
841 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
842 union generic_unix_sockaddr
*uaddr
)
844 unsigned int uaddrlen
= 0;
846 switch (wsaddr
->sa_family
)
851 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
852 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
854 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
857 uaddrlen
= sizeof(struct sockaddr_ipx
);
858 uipx
->sipx_family
=AF_IPX
;
859 uipx
->sipx_port
=wsipx
->sa_socket
;
860 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
863 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
864 #ifdef IPX_FRAME_NONE
865 uipx
->sipx_type
=IPX_FRAME_NONE
;
871 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
872 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
874 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
875 * scope_id, one without. Check:
876 * http://msdn.microsoft.com/library/en-us/winsock/winsock/sockaddr_2.asp
878 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
879 uaddrlen
= sizeof(struct sockaddr_in6
);
880 uin6
->sin6_family
= AF_INET6
;
881 uin6
->sin6_port
= win6
->sin6_port
;
882 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
883 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
884 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
886 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
889 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
893 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
894 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
896 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
898 uaddrlen
= sizeof(struct sockaddr_in
);
899 uin
->sin_family
= AF_INET
;
900 uin
->sin_port
= win
->sin_port
;
901 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
905 /* Try to determine the needed space by the passed windows sockaddr space */
907 default: /* likely a ipv4 address */
908 case sizeof(struct WS_sockaddr_in
):
909 uaddrlen
= sizeof(struct sockaddr_in
);
912 case sizeof(struct WS_sockaddr_ipx
):
913 uaddrlen
= sizeof(struct sockaddr_ipx
);
916 case sizeof(struct WS_sockaddr_in6
):
917 case sizeof(struct WS_sockaddr_in6_old
):
918 uaddrlen
= sizeof(struct sockaddr_in6
);
921 memset( uaddr
, 0, uaddrlen
);
925 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
931 /* Returns 0 if successful, -1 if the buffer is too small */
932 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
936 switch(uaddr
->sa_family
)
941 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
942 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
945 switch (*wsaddrlen
) /* how much can we copy? */
949 *wsaddrlen
= sizeof(*wsipx
);
950 wsipx
->sa_socket
=uipx
->sipx_port
;
954 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
962 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
968 wsipx
->sa_family
=WS_AF_IPX
;
979 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
980 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
982 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
984 win6old
->sin6_family
= WS_AF_INET6
;
985 win6old
->sin6_port
= uin6
->sin6_port
;
986 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
987 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
988 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
989 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
990 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
991 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
992 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
993 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
999 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
1000 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
1002 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
1004 win
->sin_family
= WS_AF_INET
;
1005 win
->sin_port
= uin
->sin_port
;
1006 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
1007 memset(&win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
1008 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
1012 memset(wsaddr
,0,*wsaddrlen
);
1016 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
1022 /**************************************************************************
1023 * Functions for handling overlapped I/O
1024 **************************************************************************/
1026 static void ws2_async_terminate(ws2_async
* as
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, ULONG count
)
1028 TRACE( "as: %p uovl %p ovl %p\n", as
, as
->user_overlapped
, iosb
);
1030 iosb
->u
.Status
= status
;
1031 iosb
->Information
= count
;
1032 if (as
->completion_func
)
1033 as
->completion_func( NtStatusToWSAError(status
),
1034 count
, as
->user_overlapped
, as
->flags
);
1036 HeapFree( GetProcessHeap(), 0, as
->iovec
);
1037 HeapFree( GetProcessHeap(), 0, as
);
1040 /***********************************************************************
1041 * WS2_make_async (INTERNAL)
1044 static NTSTATUS
WS2_async_recv(void*, IO_STATUS_BLOCK
*, NTSTATUS
);
1045 static NTSTATUS
WS2_async_send(void*, IO_STATUS_BLOCK
*, NTSTATUS
);
1046 static NTSTATUS
WS2_async_shutdown( void*, IO_STATUS_BLOCK
*, NTSTATUS
);
1048 static inline struct ws2_async
*
1049 WS2_make_async(SOCKET s
, enum ws2_mode mode
, struct iovec
*iovec
, DWORD dwBufferCount
,
1050 LPDWORD lpFlags
, struct WS_sockaddr
*addr
,
1051 LPINT addrlen
, LPWSAOVERLAPPED lpOverlapped
,
1052 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
1053 IO_STATUS_BLOCK
**piosb
)
1055 struct ws2_async
*wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof( ws2_async
) );
1057 TRACE( "wsa %p\n", wsa
);
1062 wsa
->hSocket
= (HANDLE
) s
;
1068 wsa
->flags
= *lpFlags
;
1069 wsa
->addrlen
.ptr
= addrlen
;
1074 wsa
->addrlen
.val
= *addrlen
;
1077 ERR("Invalid async mode: %d\n", mode
);
1079 wsa
->user_overlapped
= lpOverlapped
;
1080 wsa
->completion_func
= lpCompletionRoutine
;
1082 wsa
->n_iovecs
= dwBufferCount
;
1088 *piosb
= (IO_STATUS_BLOCK
*)lpOverlapped
;
1089 if (!lpCompletionRoutine
)
1090 wsa
->event
= lpOverlapped
->hEvent
;
1093 *piosb
= &wsa
->local_iosb
;
1095 TRACE( "wsa %p, h %p, ev %p, iosb %p, uov %p, cfunc %p\n",
1096 wsa
, wsa
->hSocket
, wsa
->event
,
1097 *piosb
, wsa
->user_overlapped
, wsa
->completion_func
);
1102 static ULONG
ws2_queue_async(struct ws2_async
* wsa
, IO_STATUS_BLOCK
* iosb
)
1104 NTSTATUS (*apc
)(void *, IO_STATUS_BLOCK
*, NTSTATUS
);
1110 case ws2m_read
: apc
= WS2_async_recv
; type
= ASYNC_TYPE_READ
; break;
1111 case ws2m_write
: apc
= WS2_async_send
; type
= ASYNC_TYPE_WRITE
; break;
1112 case ws2m_sd_read
: apc
= WS2_async_shutdown
; type
= ASYNC_TYPE_READ
; break;
1113 case ws2m_sd_write
: apc
= WS2_async_shutdown
; type
= ASYNC_TYPE_WRITE
; break;
1114 default: FIXME("Unknown internal mode (%d)\n", wsa
->mode
); return STATUS_INVALID_PARAMETER
;
1117 SERVER_START_REQ( register_async
)
1119 req
->handle
= wsa
->hSocket
;
1120 req
->async
.callback
= apc
;
1121 req
->async
.iosb
= iosb
;
1122 req
->async
.arg
= wsa
;
1123 req
->async
.event
= wsa
->event
;
1125 status
= wine_server_call( req
);
1129 if (status
!= STATUS_PENDING
)
1130 ws2_async_terminate(wsa
, iosb
, status
, 0);
1132 NtCurrentTeb()->num_async_io
++;
1136 /***********************************************************************
1137 * WS2_recv (INTERNAL)
1139 * Workhorse for both synchronous and asynchronous recv() operations.
1141 static int WS2_recv( int fd
, struct iovec
* iov
, int count
,
1142 struct WS_sockaddr
*lpFrom
, LPINT lpFromlen
,
1146 union generic_unix_sockaddr unix_sockaddr
;
1148 TRACE( "fd %d, iovec %p, count %d addr %s, len %p, flags %x\n",
1149 fd
, iov
, count
, debugstr_sockaddr(lpFrom
), lpFromlen
, *lpFlags
);
1151 hdr
.msg_name
= NULL
;
1155 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
1156 hdr
.msg_name
= &unix_sockaddr
;
1159 hdr
.msg_namelen
= 0;
1162 hdr
.msg_iovlen
= count
;
1163 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1164 hdr
.msg_accrights
= NULL
;
1165 hdr
.msg_accrightslen
= 0;
1167 hdr
.msg_control
= NULL
;
1168 hdr
.msg_controllen
= 0;
1172 if ( (n
= recvmsg(fd
, &hdr
, *lpFlags
)) == -1 )
1174 TRACE( "recvmsg error %d\n", errno
);
1179 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, lpFrom
, lpFromlen
) != 0 )
1181 /* The from buffer was too small, but we read the data
1182 * anyway. Is that really bad?
1184 WSASetLastError( WSAEFAULT
);
1185 WARN( "Address buffer too small\n" );
1189 TRACE("-> %d\n", n
);
1193 /***********************************************************************
1194 * WS2_async_recv (INTERNAL)
1196 * Handler for overlapped recv() operations.
1198 static NTSTATUS
WS2_async_recv( void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
)
1200 ws2_async
* wsa
= user
;
1201 int result
= 0, fd
, err
;
1203 TRACE( "(%p %p %x)\n", wsa
, iosb
, status
);
1207 case STATUS_ALERTED
:
1208 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
1211 result
= WS2_recv( fd
, wsa
->iovec
, wsa
->n_iovecs
,
1212 wsa
->addr
, wsa
->addrlen
.ptr
, &wsa
->flags
);
1213 wine_server_release_fd( wsa
->hSocket
, fd
);
1216 status
= STATUS_SUCCESS
;
1217 TRACE( "received %d bytes\n", result
);
1218 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1223 if ( err
== WSAEINTR
|| err
== WSAEWOULDBLOCK
) /* errno: EINTR / EAGAIN */
1225 status
= STATUS_PENDING
;
1226 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1227 TRACE( "still pending\n" );
1232 status
= err
; /* FIXME: is this correct ???? */
1233 TRACE( "Error: %x\n", err
);
1238 if (status
!= STATUS_PENDING
) ws2_async_terminate(wsa
, iosb
, status
, result
);
1242 /***********************************************************************
1243 * WS2_send (INTERNAL)
1245 * Workhorse for both synchronous and asynchronous send() operations.
1247 static int WS2_send( int fd
, struct iovec
* iov
, int count
,
1248 const struct WS_sockaddr
*to
, INT tolen
, DWORD dwFlags
)
1251 union generic_unix_sockaddr unix_addr
;
1253 TRACE( "fd %d, iovec %p, count %d, addr %s, len %d, flags %x\n",
1254 fd
, iov
, count
, debugstr_sockaddr(to
), tolen
, dwFlags
);
1256 hdr
.msg_name
= NULL
;
1260 hdr
.msg_name
= &unix_addr
;
1261 hdr
.msg_namelen
= ws_sockaddr_ws2u( to
, tolen
, &unix_addr
);
1262 if ( !hdr
.msg_namelen
)
1264 WSASetLastError( WSAEFAULT
);
1265 return SOCKET_ERROR
;
1269 if(to
->sa_family
== WS_AF_IPX
)
1272 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
1274 unsigned int len
=sizeof(int);
1276 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1277 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1278 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1279 * ipx type in the sockaddr_opx structure with the stored value.
1281 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
1283 TRACE("ptype: %d (fd:%d)\n", val
, fd
);
1284 uipx
->sipx_type
= val
;
1292 hdr
.msg_namelen
= 0;
1295 hdr
.msg_iovlen
= count
;
1296 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1297 hdr
.msg_accrights
= NULL
;
1298 hdr
.msg_accrightslen
= 0;
1300 hdr
.msg_control
= NULL
;
1301 hdr
.msg_controllen
= 0;
1305 return sendmsg(fd
, &hdr
, dwFlags
);
1308 /***********************************************************************
1309 * WS2_async_send (INTERNAL)
1311 * Handler for overlapped send() operations.
1313 static NTSTATUS
WS2_async_send(void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
)
1315 ws2_async
* wsa
= user
;
1318 TRACE( "(%p %p %x)\n", wsa
, iosb
, status
);
1322 case STATUS_ALERTED
:
1323 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
1326 /* check to see if the data is ready (non-blocking) */
1327 result
= WS2_send( fd
, wsa
->iovec
, wsa
->n_iovecs
, wsa
->addr
, wsa
->addrlen
.val
, wsa
->flags
);
1328 wine_server_release_fd( wsa
->hSocket
, fd
);
1332 status
= STATUS_SUCCESS
;
1333 TRACE( "sent %d bytes\n", result
);
1334 _enable_event( wsa
->hSocket
, FD_WRITE
, 0, 0 );
1338 int err
= wsaErrno();
1339 if ( err
== WSAEINTR
)
1341 status
= STATUS_PENDING
;
1342 _enable_event( wsa
->hSocket
, FD_WRITE
, 0, 0 );
1343 TRACE( "still pending\n" );
1347 /* We set the status to a winsock error code and check for that
1348 later in NtStatusToWSAError () */
1351 TRACE( "Error: %x\n", err
);
1356 if (status
!= STATUS_PENDING
) ws2_async_terminate(wsa
, iosb
, status
, result
);
1360 /***********************************************************************
1361 * WS2_async_shutdown (INTERNAL)
1363 * Handler for shutdown() operations on overlapped sockets.
1365 static NTSTATUS
WS2_async_shutdown( void* user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
)
1367 ws2_async
* wsa
= user
;
1370 TRACE( "async %p %d\n", wsa
, wsa
->mode
);
1373 case STATUS_ALERTED
:
1374 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
1377 switch ( wsa
->mode
)
1379 case ws2m_sd_read
: err
= shutdown( fd
, 0 ); break;
1380 case ws2m_sd_write
: err
= shutdown( fd
, 1 ); break;
1381 default: ERR("invalid mode: %d\n", wsa
->mode
);
1383 wine_server_release_fd( wsa
->hSocket
, fd
);
1384 status
= err
? wsaErrno() : STATUS_SUCCESS
;
1387 ws2_async_terminate(wsa
, iosb
, status
, 0);
1391 /***********************************************************************
1392 * WS2_register_async_shutdown (INTERNAL)
1394 * Helper function for WS_shutdown() on overlapped sockets.
1396 static int WS2_register_async_shutdown( SOCKET s
, enum ws2_mode mode
)
1398 struct ws2_async
*wsa
;
1399 int ret
, err
= WSAEFAULT
;
1402 LPWSAOVERLAPPED ovl
= HeapAlloc(GetProcessHeap(), 0, sizeof( WSAOVERLAPPED
));
1403 IO_STATUS_BLOCK
*iosb
= NULL
;
1405 TRACE("s %ld mode %d\n", s
, mode
);
1409 ovl
->hEvent
= WSACreateEvent();
1410 if ( ovl
->hEvent
== WSA_INVALID_EVENT
)
1413 wsa
= WS2_make_async( s
, mode
, NULL
, 0, &dwflags
, NULL
, &len
, ovl
, NULL
, &iosb
);
1417 /* Hack: this will cause ws2_async_terminate() to free the overlapped structure */
1418 wsa
->user_overlapped
= NULL
;
1419 if ((ret
= ws2_queue_async( wsa
, iosb
)) != STATUS_PENDING
)
1421 err
= NtStatusToWSAError( ret
);
1424 /* Try immediate completion */
1425 while ( WaitForSingleObjectEx( ovl
->hEvent
, 0, TRUE
) == STATUS_USER_APC
);
1429 WSACloseEvent( ovl
->hEvent
);
1431 HeapFree( GetProcessHeap(), 0, ovl
);
1436 /***********************************************************************
1439 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
,
1445 TRACE("socket %04lx\n", s
);
1446 is_blocking
= _is_blocking(s
);
1451 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1452 if (fd
== -1) return INVALID_SOCKET
;
1454 do_block(fd
, POLLIN
, -1);
1455 _sync_sock_state(s
); /* let wineserver notice connection */
1456 release_sock_fd( s
, fd
);
1457 /* retrieve any error codes from it */
1458 SetLastError(_get_sock_error(s
, FD_ACCEPT_BIT
));
1459 /* FIXME: care about the error? */
1461 SERVER_START_REQ( accept_socket
)
1463 req
->lhandle
= SOCKET2HANDLE(s
);
1464 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
1465 req
->attributes
= OBJ_INHERIT
;
1466 set_error( wine_server_call( req
) );
1467 as
= HANDLE2SOCKET( reply
->handle
);
1472 if (addr
) WS_getpeername(as
, addr
, addrlen32
);
1475 } while (is_blocking
);
1476 return INVALID_SOCKET
;
1479 /***********************************************************************
1482 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
1484 int fd
= get_sock_fd( s
, 0, NULL
);
1485 int res
= SOCKET_ERROR
;
1487 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
1491 if (!name
|| (name
->sa_family
&& !SUPPORTED_PF(name
->sa_family
)))
1493 SetLastError(WSAEAFNOSUPPORT
);
1497 union generic_unix_sockaddr uaddr
;
1498 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
1501 SetLastError(WSAEFAULT
);
1505 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
1507 int loc_errno
= errno
;
1508 WARN("\tfailure - errno = %i\n", errno
);
1513 SetLastError(WSAENOTSOCK
);
1516 SetLastError(WSAEINVAL
);
1519 SetLastError(wsaErrno());
1525 res
=0; /* success */
1529 release_sock_fd( s
, fd
);
1534 /***********************************************************************
1535 * closesocket (WS2_32.3)
1537 int WINAPI
WS_closesocket(SOCKET s
)
1539 TRACE("socket %04lx\n", s
);
1540 if (CloseHandle(SOCKET2HANDLE(s
))) return 0;
1541 return SOCKET_ERROR
;
1544 /***********************************************************************
1545 * connect (WS2_32.4)
1547 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
1549 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1551 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
1555 union generic_unix_sockaddr uaddr
;
1556 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
1560 SetLastError(WSAEFAULT
);
1564 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
1565 goto connect_success
;
1568 if (errno
== EINPROGRESS
)
1570 /* tell wineserver that a connection is in progress */
1571 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
1572 FD_CONNECT
|FD_READ
|FD_WRITE
,
1573 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
1574 if (_is_blocking(s
))
1578 do_block(fd
, POLLIN
| POLLOUT
, -1);
1579 _sync_sock_state(s
); /* let wineserver notice connection */
1580 /* retrieve any error codes from it */
1581 result
= _get_sock_error(s
, FD_CONNECT_BIT
);
1583 SetLastError(result
);
1586 goto connect_success
;
1591 SetLastError(WSAEWOULDBLOCK
);
1596 SetLastError(wsaErrno());
1598 release_sock_fd( s
, fd
);
1600 return SOCKET_ERROR
;
1603 release_sock_fd( s
, fd
);
1604 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
1605 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
1606 FD_CONNECT
|FD_WINE_LISTENING
);
1610 /***********************************************************************
1611 * WSAConnect (WS2_32.30)
1613 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
1614 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
1615 LPQOS lpSQOS
, LPQOS lpGQOS
)
1617 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
1618 FIXME("unsupported parameters!\n");
1619 return WS_connect( s
, name
, namelen
);
1623 /***********************************************************************
1624 * getpeername (WS2_32.5)
1626 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
1631 TRACE("socket: %04lx, ptr %p, len %08x\n", s
, name
, *namelen
);
1633 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1634 if( (name
== NULL
) || (namelen
== NULL
) )
1636 SetLastError( WSAEFAULT
);
1637 return SOCKET_ERROR
;
1640 fd
= get_sock_fd( s
, 0, NULL
);
1645 union generic_unix_sockaddr uaddr
;
1646 unsigned int uaddrlen
= sizeof(uaddr
);
1648 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
1650 SetLastError(wsaErrno());
1652 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
1654 /* The buffer was too small */
1655 SetLastError(WSAEFAULT
);
1661 release_sock_fd( s
, fd
);
1666 /***********************************************************************
1667 * getsockname (WS2_32.6)
1669 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
1674 TRACE("socket: %04lx, ptr %p, len %8x\n", s
, name
, *namelen
);
1676 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1677 if( (name
== NULL
) || (namelen
== NULL
) )
1679 SetLastError( WSAEFAULT
);
1680 return SOCKET_ERROR
;
1683 fd
= get_sock_fd( s
, 0, NULL
);
1688 union generic_unix_sockaddr uaddr
;
1689 unsigned int uaddrlen
= sizeof(uaddr
);
1691 if (getsockname(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
1693 SetLastError(wsaErrno());
1695 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
1697 /* The buffer was too small */
1698 SetLastError(WSAEFAULT
);
1704 release_sock_fd( s
, fd
);
1709 /***********************************************************************
1710 * getsockopt (WS2_32.7)
1712 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
1713 INT optname
, char *optval
, INT
*optlen
)
1718 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
1719 s
, level
, optname
, optval
, *optlen
);
1727 /* Handle common cases. The special cases are below, sorted
1729 case WS_SO_ACCEPTCONN
:
1730 case WS_SO_BROADCAST
:
1733 case WS_SO_KEEPALIVE
:
1734 case WS_SO_OOBINLINE
:
1738 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1739 return SOCKET_ERROR
;
1740 convert_sockopt(&level
, &optname
);
1741 if (getsockopt(fd
,(int) level
, optname
, optval
,
1742 (unsigned int *)optlen
) != 0 )
1744 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1747 release_sock_fd( s
, fd
);
1750 case WS_SO_DONTLINGER
:
1752 struct linger lingval
;
1753 unsigned int len
= sizeof(struct linger
);
1755 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
1757 SetLastError(WSAEFAULT
);
1758 return SOCKET_ERROR
;
1760 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1761 return SOCKET_ERROR
;
1763 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
1765 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1770 *(BOOL
*)optval
= (lingval
.l_onoff
) ? FALSE
: TRUE
;
1771 *optlen
= sizeof(BOOL
);
1774 release_sock_fd( s
, fd
);
1778 /* As mentioned in setsockopt, Windows ignores this, so we
1779 * always return true here */
1780 case WS_SO_DONTROUTE
:
1781 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
1783 SetLastError(WSAEFAULT
);
1784 return SOCKET_ERROR
;
1786 *(BOOL
*)optval
= TRUE
;
1787 *optlen
= sizeof(BOOL
);
1792 struct linger lingval
;
1793 unsigned int len
= sizeof(struct linger
);
1795 /* struct linger and LINGER have different sizes */
1796 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
1798 SetLastError(WSAEFAULT
);
1799 return SOCKET_ERROR
;
1801 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1802 return SOCKET_ERROR
;
1804 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
1806 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1811 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
1812 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
1813 *optlen
= sizeof(struct linger
);
1816 release_sock_fd( s
, fd
);
1820 case WS_SO_MAX_MSG_SIZE
:
1821 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
1823 SetLastError(WSAEFAULT
);
1824 return SOCKET_ERROR
;
1826 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
1827 *(int *)optval
= 65507;
1828 *optlen
= sizeof(int);
1831 /* SO_OPENTYPE does not require a valid socket handle. */
1832 case WS_SO_OPENTYPE
:
1833 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
1835 SetLastError(WSAEFAULT
);
1836 return SOCKET_ERROR
;
1838 *(int *)optval
= get_per_thread_data()->opentype
;
1839 *optlen
= sizeof(int);
1840 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
1844 case WS_SO_RCVTIMEO
:
1847 case WS_SO_SNDTIMEO
:
1849 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
1852 unsigned int len
= sizeof(struct timeval
);
1854 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
1856 SetLastError(WSAEFAULT
);
1857 return SOCKET_ERROR
;
1859 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1860 return SOCKET_ERROR
;
1862 convert_sockopt(&level
, &optname
);
1863 if (getsockopt(fd
,(int) level
, optname
, &tv
, &len
) != 0 )
1865 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1870 *(int *)optval
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
1871 *optlen
= sizeof(int);
1874 release_sock_fd( s
, fd
);
1878 /* As mentioned in setsockopt, the windows style SO_REUSEADDR is
1879 * not possible in Unix, so always return false here. */
1880 case WS_SO_REUSEADDR
:
1881 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
1883 SetLastError(WSAEFAULT
);
1884 return SOCKET_ERROR
;
1887 *optlen
= sizeof(int);
1891 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
1892 SetLastError(WSAENOPROTOOPT
);
1893 return SOCKET_ERROR
;
1894 } /* end switch(optname) */
1895 }/* end case WS_SOL_SOCKET */
1899 struct WS_sockaddr_ipx addr
;
1900 IPX_ADDRESS_DATA
*data
;
1905 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
1907 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (unsigned int*)optlen
) == -1)
1914 socklen_t len
=sizeof(struct ipx
);
1915 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
1918 *optval
= (int)val
.ipx_pt
;
1921 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
1922 release_sock_fd( s
, fd
);
1927 * On a Win2000 system with one network card there are usually
1928 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
1929 * Using this call you can then retrieve info about this all.
1930 * In case of Linux it is a bit different. Usually you have
1931 * only "one" device active and further it is not possible to
1932 * query things like the linkspeed.
1934 FIXME("IPX_ADDRESS\n");
1935 namelen
= sizeof(struct WS_sockaddr_ipx
);
1936 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
1937 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
1939 data
= (IPX_ADDRESS_DATA
*)optval
;
1940 memcpy(data
->nodenum
,&addr
.sa_nodenum
,sizeof(data
->nodenum
));
1941 memcpy(data
->netnum
,&addr
.sa_netnum
,sizeof(data
->netnum
));
1942 data
->adapternum
= 0;
1943 data
->wan
= FALSE
; /* We are not on a wan for now .. */
1944 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
1945 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
1946 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
1947 * note 1MB = 1000kB in this case */
1950 case IPX_MAX_ADAPTER_NUM
:
1951 FIXME("IPX_MAX_ADAPTER_NUM\n");
1952 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
1956 FIXME("IPX optname:%x\n", optname
);
1957 return SOCKET_ERROR
;
1958 }/* end switch(optname) */
1959 } /* end case NSPROTO_IPX */
1961 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
1962 case WS_IPPROTO_TCP
:
1965 case WS_TCP_NODELAY
:
1966 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1967 return SOCKET_ERROR
;
1968 convert_sockopt(&level
, &optname
);
1969 if (getsockopt(fd
,(int) level
, optname
, optval
,
1970 (unsigned int *)optlen
) != 0 )
1972 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1975 release_sock_fd( s
, fd
);
1978 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
1979 return SOCKET_ERROR
;
1984 case WS_IP_ADD_MEMBERSHIP
:
1985 case WS_IP_DROP_MEMBERSHIP
:
1989 case WS_IP_MULTICAST_IF
:
1990 case WS_IP_MULTICAST_LOOP
:
1991 case WS_IP_MULTICAST_TTL
:
1995 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1996 return SOCKET_ERROR
;
1997 convert_sockopt(&level
, &optname
);
1998 if (getsockopt(fd
,(int) level
, optname
, optval
,
1999 (unsigned int *)optlen
) != 0 )
2001 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2004 release_sock_fd( s
, fd
);
2007 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
2008 return SOCKET_ERROR
;
2011 FIXME("Unknown level: 0x%08x\n", level
);
2012 return SOCKET_ERROR
;
2013 } /* end switch(level) */
2016 /***********************************************************************
2020 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
2022 return htonl(hostlong
);
2026 /***********************************************************************
2030 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
2032 return htons(hostshort
);
2035 /***********************************************************************
2036 * WSAHtonl (WS2_32.46)
2037 * From MSDN decription of error codes, this function should also
2038 * check if WinSock has been initialized and the socket is a valid
2039 * socket. But why? This function only translates a host byte order
2040 * u_long into a network byte order u_long...
2042 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
2046 *lpnetlong
= htonl(hostlong
);
2049 WSASetLastError(WSAEFAULT
);
2050 return SOCKET_ERROR
;
2053 /***********************************************************************
2054 * WSAHtons (WS2_32.47)
2055 * From MSDN decription of error codes, this function should also
2056 * check if WinSock has been initialized and the socket is a valid
2057 * socket. But why? This function only translates a host byte order
2058 * u_short into a network byte order u_short...
2060 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
2065 *lpnetshort
= htons(hostshort
);
2068 WSASetLastError(WSAEFAULT
);
2069 return SOCKET_ERROR
;
2073 /***********************************************************************
2074 * inet_addr (WINSOCK.10)
2075 * inet_addr (WS2_32.11)
2077 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
2079 if (!cp
) return INADDR_NONE
;
2080 return inet_addr(cp
);
2084 /***********************************************************************
2085 * ntohl (WINSOCK.14)
2088 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
2090 return ntohl(netlong
);
2094 /***********************************************************************
2095 * ntohs (WINSOCK.15)
2098 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
2100 return ntohs(netshort
);
2104 /***********************************************************************
2105 * inet_ntoa (WS2_32.12)
2107 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
2109 /* use "buffer for dummies" here because some applications have a
2110 * propensity to decode addresses in ws_hostent structure without
2111 * saving them first...
2113 static char dbuffer
[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
2115 char* s
= inet_ntoa(*((struct in_addr
*)&in
));
2121 SetLastError(wsaErrno());
2125 /**********************************************************************
2126 * WSAIoctl (WS2_32.50)
2129 INT WINAPI
WSAIoctl(SOCKET s
,
2130 DWORD dwIoControlCode
,
2133 LPVOID lpbOutBuffer
,
2135 LPDWORD lpcbBytesReturned
,
2136 LPWSAOVERLAPPED lpOverlapped
,
2137 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2139 TRACE("%ld, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
2140 s
, dwIoControlCode
, lpvInBuffer
, cbInBuffer
, lpbOutBuffer
,
2141 cbOutBuffer
, lpcbBytesReturned
, lpOverlapped
, lpCompletionRoutine
);
2143 switch( dwIoControlCode
)
2146 if (cbInBuffer
!= sizeof(WS_u_long
)) {
2147 WSASetLastError(WSAEFAULT
);
2148 return SOCKET_ERROR
;
2150 return WS_ioctlsocket( s
, WS_FIONBIO
, lpvInBuffer
);
2153 if (cbOutBuffer
!= sizeof(WS_u_long
)) {
2154 WSASetLastError(WSAEFAULT
);
2155 return SOCKET_ERROR
;
2157 return WS_ioctlsocket( s
, WS_FIONREAD
, lpbOutBuffer
);
2159 case WS_SIO_GET_INTERFACE_LIST
:
2161 INTERFACE_INFO
* intArray
= (INTERFACE_INFO
*)lpbOutBuffer
;
2162 DWORD size
, numInt
, apiReturn
;
2165 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
2169 WSASetLastError(WSAEFAULT
);
2170 return SOCKET_ERROR
;
2172 if (!lpcbBytesReturned
)
2174 WSASetLastError(WSAEFAULT
);
2175 return SOCKET_ERROR
;
2178 fd
= get_sock_fd( s
, 0, NULL
);
2179 if (fd
== -1) return SOCKET_ERROR
;
2181 apiReturn
= GetAdaptersInfo(NULL
, &size
);
2182 if (apiReturn
== ERROR_NO_DATA
)
2186 else if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
2188 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
2192 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
2194 PIP_ADAPTER_INFO ptr
;
2196 if (size
*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > cbOutBuffer
)
2198 WARN("Buffer too small = %u, cbOutBuffer = %u\n", size
, cbOutBuffer
);
2199 HeapFree(GetProcessHeap(),0,table
);
2200 release_sock_fd( s
, fd
);
2201 WSASetLastError(WSAEFAULT
);
2202 return SOCKET_ERROR
;
2204 for (ptr
= table
, numInt
= 0; ptr
;
2205 ptr
= ptr
->Next
, intArray
++, numInt
++)
2207 unsigned int addr
, mask
, bcast
;
2208 struct ifreq ifInfo
;
2210 /* Socket Status Flags */
2211 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
2212 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
2214 ERR("Error obtaining status flags for socket!\n");
2215 HeapFree(GetProcessHeap(),0,table
);
2216 release_sock_fd( s
, fd
);
2217 WSASetLastError(WSAEINVAL
);
2218 return SOCKET_ERROR
;
2222 /* set flags; the values of IFF_* are not the same
2223 under Linux and Windows, therefore must generate
2225 intArray
->iiFlags
= 0;
2226 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
2227 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
2228 #ifdef IFF_POINTOPOINT
2229 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
2230 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
2232 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
2233 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
2234 if (ifInfo
.ifr_flags
& IFF_UP
)
2235 intArray
->iiFlags
|= WS_IFF_UP
;
2236 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
2237 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
2240 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
2241 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
2242 bcast
= addr
| (addr
& !mask
);
2243 intArray
->iiAddress
.AddressIn
.sin_family
= AF_INET
;
2244 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
2245 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
=
2247 intArray
->iiNetmask
.AddressIn
.sin_family
= AF_INET
;
2248 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
2249 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
=
2251 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
=
2253 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
2254 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.
2260 ERR("Unable to get interface table!\n");
2261 release_sock_fd( s
, fd
);
2262 HeapFree(GetProcessHeap(),0,table
);
2263 WSASetLastError(WSAEINVAL
);
2264 return SOCKET_ERROR
;
2266 HeapFree(GetProcessHeap(),0,table
);
2270 release_sock_fd( s
, fd
);
2271 WSASetLastError(WSAEINVAL
);
2272 return SOCKET_ERROR
;
2277 ERR("Unable to get interface table!\n");
2278 release_sock_fd( s
, fd
);
2279 WSASetLastError(WSAEINVAL
);
2280 return SOCKET_ERROR
;
2282 /* Calculate the size of the array being returned */
2283 *lpcbBytesReturned
= sizeof(INTERFACE_INFO
) * numInt
;
2284 release_sock_fd( s
, fd
);
2288 case WS_SIO_ADDRESS_LIST_CHANGE
:
2289 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
2290 /* FIXME: error and return code depend on whether socket was created
2291 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
2294 case WS_SIO_ADDRESS_LIST_QUERY
:
2298 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
2300 if (!lpcbBytesReturned
)
2302 WSASetLastError(WSAEFAULT
);
2303 return SOCKET_ERROR
;
2306 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
2308 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
2311 if (!table
|| GetAdaptersInfo(table
, &size
))
2313 HeapFree(GetProcessHeap(), 0, table
);
2314 WSASetLastError(WSAEINVAL
);
2315 return SOCKET_ERROR
;
2318 for (p
= table
, num
= 0; p
; p
= p
->Next
)
2319 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
2321 need
= sizeof(SOCKET_ADDRESS_LIST
) + sizeof(SOCKET_ADDRESS
) * (num
- 1);
2322 need
+= sizeof(SOCKADDR
) * num
;
2323 *lpcbBytesReturned
= need
;
2325 if (need
> cbOutBuffer
)
2327 HeapFree(GetProcessHeap(), 0, table
);
2328 WSASetLastError(WSAEFAULT
);
2329 return SOCKET_ERROR
;
2336 SOCKET_ADDRESS_LIST
*sa_list
= (SOCKET_ADDRESS_LIST
*)lpbOutBuffer
;
2337 SOCKADDR_IN
*sockaddr
;
2339 sa
= sa_list
->Address
;
2340 sockaddr
= (SOCKADDR_IN
*)((char *)sa
+ num
* sizeof(SOCKET_ADDRESS
));
2341 sa_list
->iAddressCount
= num
;
2343 for (p
= table
, i
= 0; p
; p
= p
->Next
)
2345 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
2347 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
2348 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
2350 sockaddr
[i
].sin_family
= AF_INET
;
2351 sockaddr
[i
].sin_port
= 0;
2352 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
2357 HeapFree(GetProcessHeap(), 0, table
);
2362 WARN("unable to get IP address list\n");
2363 WSASetLastError(WSAEINVAL
);
2364 return SOCKET_ERROR
;
2368 FIXME("SIO_FLUSH: stub.\n");
2372 FIXME("unsupported WS_IOCTL cmd (%08x)\n", dwIoControlCode
);
2373 WSASetLastError(WSAEOPNOTSUPP
);
2374 return SOCKET_ERROR
;
2381 /***********************************************************************
2382 * ioctlsocket (WS2_32.10)
2384 int WINAPI
WS_ioctlsocket(SOCKET s
, long cmd
, WS_u_long
*argp
)
2389 TRACE("socket %04lx, cmd %08lx, ptr %p\n", s
, cmd
, argp
);
2398 if( _get_sock_mask(s
) )
2400 /* AsyncSelect()'ed sockets are always nonblocking */
2401 if (*argp
) return 0;
2402 SetLastError(WSAEINVAL
);
2403 return SOCKET_ERROR
;
2405 fd
= get_sock_fd( s
, 0, NULL
);
2411 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
2412 ret
= fcntl( fd
, F_SETFL
, O_NONBLOCK
);
2416 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
2417 ret
= fcntl( fd
, F_SETFL
, 0 );
2419 release_sock_fd( s
, fd
);
2421 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2423 return SOCKET_ERROR
;
2430 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
2431 SetLastError(WSAEINVAL
);
2432 return SOCKET_ERROR
;
2434 case SIOCGIFBRDADDR
:
2435 case SIOCGIFNETMASK
:
2437 /* These don't need any special handling. They are used by
2438 WsControl, and are here to suppress an unnecessary warning. */
2442 /* Netscape tries hard to use bogus ioctl 0x667e */
2443 /* FIXME: 0x667e above is ('f' << 8) | 126, and is a low word of
2444 * FIONBIO (_IOW('f', 126, u_long)), how that should be handled?
2446 WARN("\tunknown WS_IOCTL cmd (%08lx)\n", cmd
);
2450 fd
= get_sock_fd( s
, 0, NULL
);
2453 if( ioctl(fd
, newcmd
, (char*)argp
) == 0 )
2455 release_sock_fd( s
, fd
);
2458 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2459 release_sock_fd( s
, fd
);
2461 return SOCKET_ERROR
;
2464 /***********************************************************************
2465 * listen (WS2_32.13)
2467 int WINAPI
WS_listen(SOCKET s
, int backlog
)
2469 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2471 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
2474 if (listen(fd
, backlog
) == 0)
2476 release_sock_fd( s
, fd
);
2477 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
2479 FD_CONNECT
|FD_WINE_CONNECTED
);
2482 SetLastError(wsaErrno());
2483 release_sock_fd( s
, fd
);
2485 return SOCKET_ERROR
;
2488 /***********************************************************************
2491 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
2493 DWORD n
, dwFlags
= flags
;
2499 if ( WSARecvFrom(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
2500 return SOCKET_ERROR
;
2505 /***********************************************************************
2506 * recvfrom (WS2_32.17)
2508 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
2509 struct WS_sockaddr
*from
, int *fromlen
)
2511 DWORD n
, dwFlags
= flags
;
2517 if ( WSARecvFrom(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
) == SOCKET_ERROR
)
2518 return SOCKET_ERROR
;
2523 /* allocate a poll array for the corresponding fd sets */
2524 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
2525 const WS_fd_set
*exceptfds
, int *count_ptr
)
2527 int i
, j
= 0, count
= 0;
2530 if (readfds
) count
+= readfds
->fd_count
;
2531 if (writefds
) count
+= writefds
->fd_count
;
2532 if (exceptfds
) count
+= exceptfds
->fd_count
;
2534 if (!count
) return NULL
;
2535 if (!(fds
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(fds
[0])))) return NULL
;
2537 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
2539 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
2540 fds
[j
].events
= POLLIN
;
2544 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
2546 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
2547 fds
[j
].events
= POLLOUT
;
2551 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
2553 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
2554 fds
[j
].events
= POLLHUP
;
2560 /* release the file descriptor obtained in fd_sets_to_poll */
2561 /* must be called with the original fd_set arrays, before calling get_poll_results */
2562 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
2563 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
2569 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
2570 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
2574 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
2575 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
2579 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
2580 if (fds
[j
].fd
!= -1)
2582 /* make sure we have a real error before releasing the fd */
2583 if (!sock_error_p( fds
[j
].fd
)) fds
[j
].revents
= 0;
2584 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
2589 /* map the poll results back into the Windows fd sets */
2590 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
2591 const struct pollfd
*fds
)
2593 int i
, j
= 0, k
, total
= 0;
2597 for (i
= k
= 0; i
< readfds
->fd_count
; i
++, j
++)
2598 if (fds
[j
].revents
) readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
2599 readfds
->fd_count
= k
;
2604 for (i
= k
= 0; i
< writefds
->fd_count
; i
++, j
++)
2605 if (fds
[j
].revents
) writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
2606 writefds
->fd_count
= k
;
2611 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
2612 if (fds
[j
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
2613 exceptfds
->fd_count
= k
;
2620 /***********************************************************************
2621 * select (WS2_32.18)
2623 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
2624 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
2625 const struct WS_timeval
* ws_timeout
)
2627 struct pollfd
*pollfds
;
2628 int count
, ret
, timeout
= -1;
2630 TRACE("read %p, write %p, excp %p timeout %p\n",
2631 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
2633 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)) && count
)
2635 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
2636 return SOCKET_ERROR
;
2639 if (ws_timeout
) timeout
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
2641 ret
= poll( pollfds
, count
, timeout
);
2642 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
2644 if (ret
== -1) SetLastError(wsaErrno());
2645 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
2646 HeapFree( GetProcessHeap(), 0, pollfds
);
2651 /***********************************************************************
2654 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
2660 wsabuf
.buf
= (char*) buf
;
2662 if ( WSASendTo( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
2663 return SOCKET_ERROR
;
2668 /***********************************************************************
2669 * WSASend (WS2_32.72)
2671 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
2672 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
2673 LPWSAOVERLAPPED lpOverlapped
,
2674 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2676 return WSASendTo( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
2677 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
2680 /***********************************************************************
2681 * WSASendDisconnect (WS2_32.73)
2683 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
2685 return WS_shutdown( s
, SD_SEND
);
2689 /***********************************************************************
2690 * WSASendTo (WS2_32.74)
2692 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
2693 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
2694 const struct WS_sockaddr
*to
, int tolen
,
2695 LPWSAOVERLAPPED lpOverlapped
,
2696 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2698 unsigned int i
, options
;
2699 int n
, fd
, err
= WSAENOTSOCK
, ret
;
2700 struct iovec
* iovec
;
2701 struct ws2_async
*wsa
;
2702 IO_STATUS_BLOCK
* iosb
;
2704 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
2705 s
, lpBuffers
, dwBufferCount
, dwFlags
,
2706 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
2708 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
2709 TRACE( "fd=%d, options=%x\n", fd
, options
);
2711 if ( fd
== -1 ) return SOCKET_ERROR
;
2713 if ( !lpNumberOfBytesSent
)
2719 iovec
= HeapAlloc(GetProcessHeap(), 0, dwBufferCount
* sizeof(struct iovec
) );
2727 for ( i
= 0; i
< dwBufferCount
; i
++ )
2729 iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
2730 iovec
[i
].iov_len
= lpBuffers
[i
].len
;
2733 if ( (lpOverlapped
|| lpCompletionRoutine
) &&
2734 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
2736 wsa
= WS2_make_async( s
, ws2m_write
, iovec
, dwBufferCount
,
2737 &dwFlags
, (struct WS_sockaddr
*) to
, &tolen
,
2738 lpOverlapped
, lpCompletionRoutine
, &iosb
);
2745 if ((ret
= ws2_queue_async( wsa
, iosb
)) != STATUS_PENDING
)
2747 err
= NtStatusToWSAError( ret
);
2749 HeapFree( GetProcessHeap(), 0, wsa
);
2752 release_sock_fd( s
, fd
);
2754 /* Try immediate completion */
2757 if ( WSAGetOverlappedResult( s
, lpOverlapped
,
2758 lpNumberOfBytesSent
, FALSE
, &dwFlags
) )
2761 if ( (err
= WSAGetLastError()) != WSA_IO_INCOMPLETE
)
2765 WSASetLastError( WSA_IO_PENDING
);
2766 return SOCKET_ERROR
;
2769 *lpNumberOfBytesSent
= 0;
2770 if ( _is_blocking(s
) )
2772 /* On a blocking non-overlapped stream socket,
2773 * sending blocks until the entire buffer is sent. */
2774 struct iovec
*piovec
= iovec
;
2775 int finish_time
= GET_SNDTIMEO(fd
);
2776 if (finish_time
>= 0)
2777 finish_time
+= GetTickCount();
2778 while ( dwBufferCount
> 0 )
2781 if ( finish_time
>= 0 )
2783 timeout
= finish_time
- GetTickCount();
2788 timeout
= finish_time
;
2789 /* FIXME: exceptfds? */
2790 if( !do_block(fd
, POLLOUT
, timeout
)) {
2792 goto err_free
; /* msdn says a timeout in send is fatal */
2795 n
= WS2_send( fd
, piovec
, dwBufferCount
, to
, tolen
, dwFlags
);
2801 *lpNumberOfBytesSent
+= n
;
2805 if ( piovec
->iov_len
> n
)
2807 piovec
->iov_base
= (char*)piovec
->iov_base
+ n
;
2808 piovec
->iov_len
-= n
;
2813 n
-= piovec
->iov_len
;
2822 n
= WS2_send( fd
, iovec
, dwBufferCount
, to
, tolen
, dwFlags
);
2826 if ( err
== WSAEWOULDBLOCK
)
2827 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
2831 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
2832 *lpNumberOfBytesSent
= n
;
2835 TRACE(" -> %i bytes\n", *lpNumberOfBytesSent
);
2837 HeapFree( GetProcessHeap(), 0, iovec
);
2838 release_sock_fd( s
, fd
);
2842 HeapFree( GetProcessHeap(), 0, iovec
);
2845 release_sock_fd( s
, fd
);
2848 WARN(" -> ERROR %d\n", err
);
2849 WSASetLastError(err
);
2850 return SOCKET_ERROR
;
2853 /***********************************************************************
2854 * sendto (WS2_32.20)
2856 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
2857 const struct WS_sockaddr
*to
, int tolen
)
2863 wsabuf
.buf
= (char*) buf
;
2865 if ( WSASendTo(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
2866 return SOCKET_ERROR
;
2871 /***********************************************************************
2872 * setsockopt (WS2_32.21)
2874 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
2875 const char *optval
, int optlen
)
2879 struct linger linger
;
2880 struct timeval tval
;
2882 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
2883 s
, level
, optname
, optval
, optlen
);
2890 /* Some options need some conversion before they can be sent to
2891 * setsockopt. The conversions are done here, then they will fall though
2892 * to the general case. Special options that are not passed to
2893 * setsockopt follow below that.*/
2895 case WS_SO_DONTLINGER
:
2896 linger
.l_onoff
= *((const int*)optval
) ? 0: 1;
2897 linger
.l_linger
= 0;
2899 optname
= SO_LINGER
;
2900 optval
= (char*)&linger
;
2901 optlen
= sizeof(struct linger
);
2905 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
2906 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
2907 /* FIXME: what is documented behavior if SO_LINGER optval
2910 optname
= SO_LINGER
;
2911 optval
= (char*)&linger
;
2912 optlen
= sizeof(struct linger
);
2916 if (*(const int*)optval
< 2048)
2918 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
2923 /* The options listed here don't need any special handling. Thanks to
2924 * the conversion happening above, options from there will fall through
2926 case WS_SO_ACCEPTCONN
:
2927 case WS_SO_BROADCAST
:
2929 case WS_SO_KEEPALIVE
:
2930 case WS_SO_OOBINLINE
:
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
) );
2964 /* SO_REUSEADDR allows two applications to bind to the same port at at
2965 * same time. There is no direct way to do that in unix. While Wineserver
2966 * might do this, it does not seem useful for now, so just ignore it.*/
2967 case WS_SO_REUSEADDR
:
2968 TRACE("Ignoring SO_REUSEADDR, does not translate\n");
2972 case WS_SO_RCVTIMEO
:
2975 case WS_SO_SNDTIMEO
:
2977 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
2978 if (optval
&& optlen
== sizeof(UINT32
)) {
2979 /* WinSock passes miliseconds instead of struct timeval */
2980 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
2981 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
2982 /* min of 500 milisec */
2983 if (tval
.tv_sec
== 0 && tval
.tv_usec
< 500000)
2984 tval
.tv_usec
= 500000;
2985 optlen
= sizeof(struct timeval
);
2986 optval
= (char*)&tval
;
2987 } else if (optlen
== sizeof(struct timeval
)) {
2988 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
2990 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
2993 convert_sockopt(&level
, &optname
);
2998 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
2999 SetLastError(WSAENOPROTOOPT
);
3000 return SOCKET_ERROR
;
3002 break; /* case WS_SOL_SOCKET */
3009 fd
= get_sock_fd( s
, 0, NULL
);
3010 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval
, fd
);
3012 /* We try to set the ipx type on ipx socket level. */
3014 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, optlen
) == -1)
3016 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
3017 release_sock_fd( s
, fd
);
3018 return SOCKET_ERROR
;
3023 /* Should we retrieve val using a getsockopt call and then
3024 * set the modified one? */
3025 val
.ipx_pt
= *optval
;
3026 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
3029 release_sock_fd( s
, fd
);
3032 case IPX_FILTERPTYPE
:
3033 /* Sets the receive filter packet type, at the moment we don't support it */
3034 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
3035 /* Returning 0 is better for now than returning a SOCKET_ERROR */
3039 FIXME("opt_name:%x\n", optname
);
3040 return SOCKET_ERROR
;
3042 break; /* case NSPROTO_IPX */
3045 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
3046 case WS_IPPROTO_TCP
:
3049 case WS_TCP_NODELAY
:
3050 convert_sockopt(&level
, &optname
);
3053 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
3054 return SOCKET_ERROR
;
3061 case WS_IP_ADD_MEMBERSHIP
:
3062 case WS_IP_DROP_MEMBERSHIP
:
3066 case WS_IP_MULTICAST_IF
:
3067 case WS_IP_MULTICAST_LOOP
:
3068 case WS_IP_MULTICAST_TTL
:
3072 convert_sockopt(&level
, &optname
);
3075 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
3076 return SOCKET_ERROR
;
3081 FIXME("Unknown level: 0x%08x\n", level
);
3082 return SOCKET_ERROR
;
3083 } /* end switch(level) */
3085 /* avoid endianness issues if argument is a 16-bit int */
3086 if (optval
&& optlen
< sizeof(int))
3088 woptval
= *((const INT16
*) optval
);
3089 optval
= (char*) &woptval
;
3092 fd
= get_sock_fd( s
, 0, NULL
);
3093 if (fd
== -1) return SOCKET_ERROR
;
3095 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
3097 release_sock_fd( s
, fd
);
3100 TRACE("Setting socket error, %d\n", wsaErrno());
3101 SetLastError(wsaErrno());
3102 release_sock_fd( s
, fd
);
3104 return SOCKET_ERROR
;
3107 /***********************************************************************
3108 * shutdown (WS2_32.22)
3110 int WINAPI
WS_shutdown(SOCKET s
, int how
)
3112 int fd
, err
= WSAENOTSOCK
;
3113 unsigned int options
, clear_flags
= 0;
3115 fd
= get_sock_fd( s
, 0, &options
);
3116 TRACE("socket %04lx, how %i %x\n", s
, how
, options
);
3119 return SOCKET_ERROR
;
3123 case 0: /* drop receives */
3124 clear_flags
|= FD_READ
;
3126 case 1: /* drop sends */
3127 clear_flags
|= FD_WRITE
;
3129 case 2: /* drop all */
3130 clear_flags
|= FD_READ
|FD_WRITE
;
3132 clear_flags
|= FD_WINE_LISTENING
;
3135 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
3140 err
= WS2_register_async_shutdown( s
, ws2m_sd_read
);
3143 err
= WS2_register_async_shutdown( s
, ws2m_sd_write
);
3147 err
= WS2_register_async_shutdown( s
, ws2m_sd_read
);
3148 if (!err
) err
= WS2_register_async_shutdown( s
, ws2m_sd_write
);
3151 if (err
) goto error
;
3153 else /* non-overlapped mode */
3155 if ( shutdown( fd
, how
) )
3162 release_sock_fd( s
, fd
);
3163 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
3164 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
3168 release_sock_fd( s
, fd
);
3169 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
3170 WSASetLastError( err
);
3171 return SOCKET_ERROR
;
3174 /***********************************************************************
3175 * socket (WS2_32.23)
3177 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
3179 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
3181 return WSASocketA( af
, type
, protocol
, NULL
, 0,
3182 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
3186 /***********************************************************************
3187 * gethostbyaddr (WS2_32.51)
3189 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
3191 struct WS_hostent
*retval
= NULL
;
3192 struct hostent
* host
;
3194 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3197 struct hostent hostentry
;
3200 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
3202 int res
= gethostbyaddr_r(addr
, len
, type
,
3203 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
3204 if( res
!= ERANGE
) break;
3206 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
3208 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
3210 EnterCriticalSection( &csWSgetXXXbyYYY
);
3211 host
= gethostbyaddr(addr
, len
, type
);
3212 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
3214 if( host
!= NULL
) retval
= WS_dup_he(host
);
3215 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3216 HeapFree(GetProcessHeap(),0,extrabuf
);
3218 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3220 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
3224 /***********************************************************************
3225 * gethostbyname (WS2_32.52)
3227 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
3229 struct WS_hostent
*retval
= NULL
;
3230 struct hostent
* host
;
3231 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3234 struct hostent hostentry
;
3235 int locerr
= ENOBUFS
;
3240 if( gethostname( buf
, 100) == -1) {
3241 SetLastError( WSAENOBUFS
); /* appropriate ? */
3245 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3247 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
3249 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
3250 if( res
!= ERANGE
) break;
3252 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
3254 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
3256 EnterCriticalSection( &csWSgetXXXbyYYY
);
3257 host
= gethostbyname(name
);
3258 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
3260 if (host
) retval
= WS_dup_he(host
);
3261 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3262 HeapFree(GetProcessHeap(),0,extrabuf
);
3264 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3266 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
3271 /***********************************************************************
3272 * getprotobyname (WS2_32.53)
3274 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
3276 struct WS_protoent
* retval
= NULL
;
3277 #ifdef HAVE_GETPROTOBYNAME
3278 struct protoent
* proto
;
3279 EnterCriticalSection( &csWSgetXXXbyYYY
);
3280 if( (proto
= getprotobyname(name
)) != NULL
)
3282 retval
= WS_dup_pe(proto
);
3285 MESSAGE("protocol %s not found; You might want to add "
3286 "this to /etc/protocols\n", debugstr_a(name
) );
3287 SetLastError(WSANO_DATA
);
3289 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3291 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
3296 /***********************************************************************
3297 * getprotobynumber (WS2_32.54)
3299 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
3301 struct WS_protoent
* retval
= NULL
;
3302 #ifdef HAVE_GETPROTOBYNUMBER
3303 struct protoent
* proto
;
3304 EnterCriticalSection( &csWSgetXXXbyYYY
);
3305 if( (proto
= getprotobynumber(number
)) != NULL
)
3307 retval
= WS_dup_pe(proto
);
3310 MESSAGE("protocol number %d not found; You might want to add "
3311 "this to /etc/protocols\n", number
);
3312 SetLastError(WSANO_DATA
);
3314 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3316 TRACE("%i ret %p\n", number
, retval
);
3321 /***********************************************************************
3322 * getservbyname (WS2_32.55)
3324 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
3326 struct WS_servent
* retval
= NULL
;
3327 struct servent
* serv
;
3329 char *proto_str
= NULL
;
3331 if (!(name_str
= strdup_lower(name
))) return NULL
;
3333 if (proto
&& *proto
)
3335 if (!(proto_str
= strdup_lower(proto
)))
3337 HeapFree( GetProcessHeap(), 0, name_str
);
3342 EnterCriticalSection( &csWSgetXXXbyYYY
);
3343 serv
= getservbyname(name_str
, proto_str
);
3346 retval
= WS_dup_se(serv
);
3348 else SetLastError(WSANO_DATA
);
3349 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3350 HeapFree( GetProcessHeap(), 0, proto_str
);
3351 HeapFree( GetProcessHeap(), 0, name_str
);
3352 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
3356 /***********************************************************************
3357 * freeaddrinfo (WS2_32.@)
3359 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
3362 struct WS_addrinfo
*next
;
3364 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
3365 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
3366 next
= res
->ai_next
;
3367 HeapFree(GetProcessHeap(),0,res
);
3372 /* helper functions for getaddrinfo() */
3373 static int convert_aiflag_w2u(int winflags
) {
3374 int i
, unixflags
= 0;
3376 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
3377 if (ws_aiflag_map
[i
][0] & winflags
) {
3378 unixflags
|= ws_aiflag_map
[i
][1];
3379 winflags
&= ~ws_aiflag_map
[i
][0];
3382 FIXME("Unhandled windows AI_xxx flags %x\n", winflags
);
3386 static int convert_aiflag_u2w(int unixflags
) {
3387 int i
, winflags
= 0;
3389 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
3390 if (ws_aiflag_map
[i
][1] & unixflags
) {
3391 winflags
|= ws_aiflag_map
[i
][0];
3392 unixflags
&= ~ws_aiflag_map
[i
][1];
3394 if (unixflags
) /* will warn usually */
3395 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags
);
3399 static int convert_eai_u2w(int unixret
) {
3402 for (i
=0;ws_eai_map
[i
][0];i
++)
3403 if (ws_eai_map
[i
][1] == unixret
)
3404 return ws_eai_map
[i
][0];
3408 /***********************************************************************
3409 * getaddrinfo (WS2_32.@)
3411 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
3413 #if HAVE_GETADDRINFO
3414 struct addrinfo
*unixaires
= NULL
;
3416 struct addrinfo unixhints
, *punixhints
= NULL
;
3417 CHAR
*node
= NULL
, *serv
= NULL
;
3420 if (!(node
= strdup_lower(nodename
))) return WSA_NOT_ENOUGH_MEMORY
;
3423 if (!(serv
= strdup_lower(servname
))) {
3424 HeapFree(GetProcessHeap(), 0, node
);
3425 return WSA_NOT_ENOUGH_MEMORY
;
3430 punixhints
= &unixhints
;
3432 memset(&unixhints
, 0, sizeof(unixhints
));
3433 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
3434 if (hints
->ai_family
== 0) /* wildcard, specific to getaddrinfo() */
3435 punixhints
->ai_family
= 0;
3437 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
3438 if (hints
->ai_socktype
== 0) /* wildcard, specific to getaddrinfo() */
3439 punixhints
->ai_socktype
= 0;
3441 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
3442 if (hints
->ai_protocol
== 0) /* wildcard, specific to getaddrinfo() */
3443 punixhints
->ai_protocol
= 0;
3445 punixhints
->ai_protocol
= convert_proto_w2u(hints
->ai_protocol
);
3448 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
3449 result
= getaddrinfo(nodename
, servname
, punixhints
, &unixaires
);
3451 TRACE("%s, %s %p -> %p %d\n", nodename
, servname
, hints
, res
, result
);
3453 HeapFree(GetProcessHeap(), 0, node
);
3454 HeapFree(GetProcessHeap(), 0, serv
);
3457 struct addrinfo
*xuai
= unixaires
;
3458 struct WS_addrinfo
**xai
= res
;
3462 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
3468 *xai
= ai
;xai
= &ai
->ai_next
;
3469 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
3470 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
3471 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
3472 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
3473 if (xuai
->ai_canonname
) {
3474 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
3475 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
3476 if (!ai
->ai_canonname
)
3478 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
3480 len
= xuai
->ai_addrlen
;
3481 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
3484 ai
->ai_addrlen
= len
;
3486 int winlen
= ai
->ai_addrlen
;
3488 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
3489 ai
->ai_addrlen
= winlen
;
3493 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
3496 ai
->ai_addrlen
= len
;
3498 xuai
= xuai
->ai_next
;
3500 freeaddrinfo(unixaires
);
3502 result
= convert_eai_u2w(result
);
3507 if (*res
) WS_freeaddrinfo(*res
);
3508 if (unixaires
) freeaddrinfo(unixaires
);
3510 return WSA_NOT_ENOUGH_MEMORY
;
3512 FIXME("getaddrinfo() failed, not found during buildtime.\n");
3517 /***********************************************************************
3518 * GetAddrInfoW (WS2_32.@)
3520 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
3522 FIXME("empty stub!\n");
3526 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
3527 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
3529 #if HAVE_GETNAMEINFO
3531 union generic_unix_sockaddr sa_u
;
3534 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
3535 serv
, servlen
, flags
);
3537 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
3540 WSASetLastError(WSAEFAULT
);
3541 return WSA_NOT_ENOUGH_MEMORY
;
3543 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_aiflag_w2u(flags
));
3544 return convert_eai_u2w(ret
);
3546 FIXME("getnameinfo() failed, not found during buildtime.\n");
3551 /***********************************************************************
3552 * getservbyport (WS2_32.56)
3554 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
3556 struct WS_servent
* retval
= NULL
;
3557 #ifdef HAVE_GETSERVBYPORT
3558 struct servent
* serv
;
3559 char *proto_str
= NULL
;
3561 if (proto
&& *proto
)
3563 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
3565 EnterCriticalSection( &csWSgetXXXbyYYY
);
3566 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
3567 retval
= WS_dup_se(serv
);
3569 else SetLastError(WSANO_DATA
);
3570 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3571 HeapFree( GetProcessHeap(), 0, proto_str
);
3573 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
3578 /***********************************************************************
3579 * gethostname (WS2_32.57)
3581 int WINAPI
WS_gethostname(char *name
, int namelen
)
3583 TRACE("name %p, len %d\n", name
, namelen
);
3585 if (gethostname(name
, namelen
) == 0)
3587 TRACE("<- '%s'\n", name
);
3590 SetLastError((errno
== EINVAL
) ? WSAEFAULT
: wsaErrno());
3591 TRACE("<- ERROR !\n");
3592 return SOCKET_ERROR
;
3596 /* ------------------------------------- Windows sockets extensions -- *
3598 * ------------------------------------------------------------------- */
3600 /***********************************************************************
3601 * WSAEnumNetworkEvents (WS2_32.36)
3603 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
3607 TRACE("%08lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
3609 SERVER_START_REQ( get_socket_event
)
3611 req
->handle
= SOCKET2HANDLE(s
);
3612 req
->service
= TRUE
;
3613 req
->c_event
= hEvent
;
3614 wine_server_set_reply( req
, lpEvent
->iErrorCode
, sizeof(lpEvent
->iErrorCode
) );
3615 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
3619 SetLastError(WSAEINVAL
);
3620 return SOCKET_ERROR
;
3623 /***********************************************************************
3624 * WSAEventSelect (WS2_32.39)
3626 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, long lEvent
)
3630 TRACE("%08lx, hEvent %p, event %08x\n", s
, hEvent
, (unsigned)lEvent
);
3632 SERVER_START_REQ( set_socket_event
)
3634 req
->handle
= SOCKET2HANDLE(s
);
3636 req
->event
= hEvent
;
3639 ret
= wine_server_call( req
);
3643 SetLastError(WSAEINVAL
);
3644 return SOCKET_ERROR
;
3647 /**********************************************************************
3648 * WSAGetOverlappedResult (WS2_32.40)
3650 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
3651 LPDWORD lpcbTransfer
, BOOL fWait
,
3656 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
3657 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
3659 if ( lpOverlapped
== NULL
)
3661 ERR( "Invalid pointer\n" );
3662 WSASetLastError(WSA_INVALID_PARAMETER
);
3668 if (lpOverlapped
->hEvent
)
3669 while ( WaitForSingleObjectEx(lpOverlapped
->hEvent
,
3670 INFINITE
, TRUE
) == STATUS_USER_APC
);
3671 else /* busy loop */
3672 while ( ((volatile OVERLAPPED
*)lpOverlapped
)->Internal
== STATUS_PENDING
)
3676 else if ( lpOverlapped
->Internal
== STATUS_PENDING
)
3678 /* Wait in order to give APCs a chance to run. */
3679 /* This is cheating, so we must set the event again in case of success -
3680 it may be a non-manual reset event. */
3681 while ( (r
= WaitForSingleObjectEx(lpOverlapped
->hEvent
, 0, TRUE
)) == STATUS_USER_APC
);
3682 if ( r
== WAIT_OBJECT_0
&& lpOverlapped
->hEvent
)
3683 NtSetEvent( lpOverlapped
->hEvent
, NULL
);
3687 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
3690 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
3692 switch ( lpOverlapped
->Internal
)
3694 case STATUS_SUCCESS
:
3696 case STATUS_PENDING
:
3697 WSASetLastError( WSA_IO_INCOMPLETE
);
3698 if (fWait
) ERR("PENDING status after waiting!\n");
3701 WSASetLastError( NtStatusToWSAError( lpOverlapped
->Internal
));
3707 /***********************************************************************
3708 * WSAAsyncSelect (WS2_32.101)
3710 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, long lEvent
)
3714 TRACE("%lx, hWnd %p, uMsg %08x, event %08lx\n", s
, hWnd
, uMsg
, lEvent
);
3716 SERVER_START_REQ( set_socket_event
)
3718 req
->handle
= SOCKET2HANDLE(s
);
3723 ret
= wine_server_call( req
);
3727 SetLastError(WSAEINVAL
);
3728 return SOCKET_ERROR
;
3731 /***********************************************************************
3732 * WSACreateEvent (WS2_32.31)
3735 WSAEVENT WINAPI
WSACreateEvent(void)
3737 /* Create a manual-reset event, with initial state: unsignaled */
3740 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
3743 /***********************************************************************
3744 * WSACloseEvent (WS2_32.29)
3747 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
3749 TRACE ("event=%p\n", event
);
3751 return CloseHandle(event
);
3754 /***********************************************************************
3755 * WSASocketA (WS2_32.78)
3758 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
3759 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
3760 GROUP g
, DWORD dwFlags
)
3763 WSAPROTOCOL_INFOW info
;
3765 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3766 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
3768 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
3770 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
3771 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
3772 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
3776 WSASetLastError( WSAEINVAL
);
3777 return SOCKET_ERROR
;
3780 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
3783 /***********************************************************************
3784 * WSASocketW (WS2_32.79)
3787 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
3788 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
3789 GROUP g
, DWORD dwFlags
)
3794 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
3795 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
3798 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3799 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
3801 /* hack for WSADuplicateSocket */
3802 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
3803 ret
= lpProtocolInfo
->dwCatalogEntryId
;
3804 TRACE("\tgot duplicate %04lx\n", ret
);
3808 /* check and convert the socket family */
3809 af
= convert_af_w2u(af
);
3812 FIXME("Unsupported socket family %d!\n", af
);
3813 SetLastError(WSAEAFNOSUPPORT
);
3814 return INVALID_SOCKET
;
3817 /* check the socket type */
3818 type
= convert_socktype_w2u(type
);
3821 SetLastError(WSAESOCKTNOSUPPORT
);
3822 return INVALID_SOCKET
;
3825 /* check the protocol type */
3826 if ( protocol
< 0 ) /* don't support negative values */
3828 SetLastError(WSAEPROTONOSUPPORT
);
3829 return INVALID_SOCKET
;
3832 if ( af
== AF_UNSPEC
) /* did they not specify the address family? */
3836 if (type
== SOCK_STREAM
) { af
= AF_INET
; break; }
3838 if (type
== SOCK_DGRAM
) { af
= AF_INET
; break; }
3839 default: SetLastError(WSAEPROTOTYPE
); return INVALID_SOCKET
;
3842 SERVER_START_REQ( create_socket
)
3846 req
->protocol
= protocol
;
3847 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
3848 req
->attributes
= OBJ_INHERIT
;
3849 req
->flags
= dwFlags
;
3850 set_error( wine_server_call( req
) );
3851 ret
= HANDLE2SOCKET( reply
->handle
);
3856 TRACE("\tcreated %04lx\n", ret
);
3860 if (GetLastError() == WSAEACCES
) /* raw socket denied */
3862 if (type
== SOCK_RAW
)
3863 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, will fail unless running as root\n");
3865 MESSAGE("WS_SOCKET: not enough privileges to create socket, try running as root\n");
3866 SetLastError(WSAESOCKTNOSUPPORT
);
3869 WARN("\t\tfailed!\n");
3870 return INVALID_SOCKET
;
3873 /***********************************************************************
3874 * WSAJoinLeaf (WS2_32.58)
3877 SOCKET WINAPI
WSAJoinLeaf(
3879 const struct WS_sockaddr
*addr
,
3881 LPWSABUF lpCallerData
,
3882 LPWSABUF lpCalleeData
,
3888 return INVALID_SOCKET
;
3891 /***********************************************************************
3892 * __WSAFDIsSet (WS2_32.151)
3894 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
3896 int i
= set
->fd_count
;
3898 TRACE("(%ld,%p(%i))\n", s
, set
, i
);
3901 if (set
->fd_array
[i
] == s
) return 1;
3905 /***********************************************************************
3906 * WSAIsBlocking (WINSOCK.114)
3907 * WSAIsBlocking (WS2_32.114)
3909 BOOL WINAPI
WSAIsBlocking(void)
3911 /* By default WinSock should set all its sockets to non-blocking mode
3912 * and poll in PeekMessage loop when processing "blocking" ones. This
3913 * function is supposed to tell if the program is in this loop. Our
3914 * blocking calls are truly blocking so we always return FALSE.
3916 * Note: It is allowed to call this function without prior WSAStartup().
3923 /***********************************************************************
3924 * WSACancelBlockingCall (WINSOCK.113)
3925 * WSACancelBlockingCall (WS2_32.113)
3927 INT WINAPI
WSACancelBlockingCall(void)
3933 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
3935 FIXME("How was this called?\n");
3940 /***********************************************************************
3941 * WSASetBlockingHook (WS2_32.109)
3943 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
3945 FARPROC prev
= blocking_hook
;
3946 blocking_hook
= lpBlockFunc
;
3947 TRACE("hook %p\n", lpBlockFunc
);
3952 /***********************************************************************
3953 * WSAUnhookBlockingHook (WS2_32.110)
3955 INT WINAPI
WSAUnhookBlockingHook(void)
3957 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
3962 /* ----------------------------------- end of API stuff */
3964 /* ----------------------------------- helper functions -
3966 * TODO: Merge WS_dup_..() stuff into one function that
3967 * would operate with a generic structure containing internal
3968 * pointers (via a template of some kind).
3971 static int list_size(char** l
, int item_size
)
3976 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
3977 j
+= (i
+ 1) * sizeof(char*); }
3981 static int list_dup(char** l_src
, char** l_to
, int item_size
)
3986 for (i
= 0; l_src
[i
]; i
++) ;
3987 p
= (char *)(l_to
+ i
+ 1);
3988 for (i
= 0; l_src
[i
]; i
++)
3990 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
3991 memcpy(p
, l_src
[i
], count
);
3996 return p
- (char *)l_to
;
4001 /* duplicate hostent entry
4002 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
4003 * Dito for protoent and servent.
4005 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
4008 struct WS_hostent
*p_to
;
4010 int size
= (sizeof(*p_he
) +
4011 strlen(p_he
->h_name
) + 1 +
4012 list_size(p_he
->h_aliases
, 0) +
4013 list_size(p_he
->h_addr_list
, p_he
->h_length
));
4015 if (!(p_to
= check_buffer_he(size
))) return NULL
;
4016 p_to
->h_addrtype
= p_he
->h_addrtype
;
4017 p_to
->h_length
= p_he
->h_length
;
4019 p
= (char *)(p_to
+ 1);
4021 strcpy(p
, p_he
->h_name
);
4024 p_to
->h_aliases
= (char **)p
;
4025 p
+= list_dup(p_he
->h_aliases
, p_to
->h_aliases
, 0);
4027 p_to
->h_addr_list
= (char **)p
;
4028 list_dup(p_he
->h_addr_list
, p_to
->h_addr_list
, p_he
->h_length
);
4032 /* ----- protoent */
4034 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
)
4037 struct WS_protoent
*p_to
;
4039 int size
= (sizeof(*p_pe
) +
4040 strlen(p_pe
->p_name
) + 1 +
4041 list_size(p_pe
->p_aliases
, 0));
4043 if (!(p_to
= check_buffer_pe(size
))) return NULL
;
4044 p_to
->p_proto
= p_pe
->p_proto
;
4046 p
= (char *)(p_to
+ 1);
4048 strcpy(p
, p_pe
->p_name
);
4051 p_to
->p_aliases
= (char **)p
;
4052 list_dup(p_pe
->p_aliases
, p_to
->p_aliases
, 0);
4058 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
4061 struct WS_servent
*p_to
;
4063 int size
= (sizeof(*p_se
) +
4064 strlen(p_se
->s_proto
) + 1 +
4065 strlen(p_se
->s_name
) + 1 +
4066 list_size(p_se
->s_aliases
, 0));
4068 if (!(p_to
= check_buffer_se(size
))) return NULL
;
4069 p_to
->s_port
= p_se
->s_port
;
4071 p
= (char *)(p_to
+ 1);
4073 strcpy(p
, p_se
->s_name
);
4077 strcpy(p
, p_se
->s_proto
);
4080 p_to
->s_aliases
= (char **)p
;
4081 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
4085 /* ----------------------------------- error handling */
4089 int loc_errno
= errno
;
4090 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
4094 case EINTR
: return WSAEINTR
;
4095 case EBADF
: return WSAEBADF
;
4097 case EACCES
: return WSAEACCES
;
4098 case EFAULT
: return WSAEFAULT
;
4099 case EINVAL
: return WSAEINVAL
;
4100 case EMFILE
: return WSAEMFILE
;
4101 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
4102 case EINPROGRESS
: return WSAEINPROGRESS
;
4103 case EALREADY
: return WSAEALREADY
;
4104 case ENOTSOCK
: return WSAENOTSOCK
;
4105 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
4106 case EMSGSIZE
: return WSAEMSGSIZE
;
4107 case EPROTOTYPE
: return WSAEPROTOTYPE
;
4108 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
4109 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
4110 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
4111 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
4112 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
4113 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
4114 case EADDRINUSE
: return WSAEADDRINUSE
;
4115 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
4116 case ENETDOWN
: return WSAENETDOWN
;
4117 case ENETUNREACH
: return WSAENETUNREACH
;
4118 case ENETRESET
: return WSAENETRESET
;
4119 case ECONNABORTED
: return WSAECONNABORTED
;
4121 case ECONNRESET
: return WSAECONNRESET
;
4122 case ENOBUFS
: return WSAENOBUFS
;
4123 case EISCONN
: return WSAEISCONN
;
4124 case ENOTCONN
: return WSAENOTCONN
;
4125 case ESHUTDOWN
: return WSAESHUTDOWN
;
4126 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
4127 case ETIMEDOUT
: return WSAETIMEDOUT
;
4128 case ECONNREFUSED
: return WSAECONNREFUSED
;
4129 case ELOOP
: return WSAELOOP
;
4130 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
4131 case EHOSTDOWN
: return WSAEHOSTDOWN
;
4132 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
4133 case ENOTEMPTY
: return WSAENOTEMPTY
;
4135 case EPROCLIM
: return WSAEPROCLIM
;
4138 case EUSERS
: return WSAEUSERS
;
4141 case EDQUOT
: return WSAEDQUOT
;
4144 case ESTALE
: return WSAESTALE
;
4147 case EREMOTE
: return WSAEREMOTE
;
4150 /* just in case we ever get here and there are no problems */
4153 WARN("Unknown errno %d!\n", loc_errno
);
4154 return WSAEOPNOTSUPP
;
4158 UINT
wsaHerrno(int loc_errno
)
4161 WARN("h_errno %d.\n", loc_errno
);
4165 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
4166 case TRY_AGAIN
: return WSATRY_AGAIN
;
4167 case NO_RECOVERY
: return WSANO_RECOVERY
;
4168 case NO_DATA
: return WSANO_DATA
;
4169 case ENOBUFS
: return WSAENOBUFS
;
4173 WARN("Unknown h_errno %d!\n", loc_errno
);
4174 return WSAEOPNOTSUPP
;
4179 /***********************************************************************
4180 * WSARecv (WS2_32.67)
4182 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4183 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
4184 LPWSAOVERLAPPED lpOverlapped
,
4185 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4187 return WSARecvFrom(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
4188 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
);
4191 /***********************************************************************
4192 * WSARecvFrom (WS2_32.69)
4194 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4195 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
4196 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
4197 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4200 unsigned int i
, options
;
4202 DWORD timeout_start
= GetTickCount();
4203 struct iovec
* iovec
;
4204 struct ws2_async
*wsa
;
4205 IO_STATUS_BLOCK
* iosb
;
4207 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
4208 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
4209 (lpFromlen
? *lpFromlen
: -1),
4210 lpOverlapped
, lpCompletionRoutine
);
4212 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
4213 TRACE( "fd=%d, options=%x\n", fd
, options
);
4215 if (fd
== -1) return SOCKET_ERROR
;
4217 iovec
= HeapAlloc( GetProcessHeap(), 0, dwBufferCount
* sizeof (struct iovec
) );
4224 for (i
= 0; i
< dwBufferCount
; i
++)
4226 iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
4227 iovec
[i
].iov_len
= lpBuffers
[i
].len
;
4232 n
= WS2_recv( fd
, iovec
, dwBufferCount
, lpFrom
, lpFromlen
, lpFlags
);
4235 if (errno
== EINTR
) continue;
4236 if (errno
!= EAGAIN
)
4242 if ((lpOverlapped
|| lpCompletionRoutine
) &&
4243 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
4245 wsa
= WS2_make_async( s
, ws2m_read
, iovec
, dwBufferCount
,
4246 lpFlags
, lpFrom
, lpFromlen
,
4247 lpOverlapped
, lpCompletionRoutine
, &iosb
);
4253 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
.event
= lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
;
4266 err
= wine_server_call( req
);
4270 if (err
== STATUS_PENDING
)
4271 NtCurrentTeb()->num_async_io
++;
4273 ws2_async_terminate(wsa
, iosb
, err
, 0);
4275 WSASetLastError( NtStatusToWSAError( err
));
4276 return SOCKET_ERROR
;
4279 if ( _is_blocking(s
) )
4282 int timeout
= GET_RCVTIMEO(fd
);
4285 timeout
-= GetTickCount() - timeout_start
;
4286 if (timeout
< 0) timeout
= 0;
4290 pfd
.events
= POLLIN
;
4291 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
4293 if (!timeout
|| !poll( &pfd
, 1, timeout
))
4296 /* a timeout is not fatal */
4297 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4303 err
= WSAEWOULDBLOCK
;
4308 TRACE(" -> %i bytes\n", n
);
4309 *lpNumberOfBytesRecvd
= n
;
4311 HeapFree(GetProcessHeap(), 0, iovec
);
4312 release_sock_fd( s
, fd
);
4313 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4318 HeapFree(GetProcessHeap(), 0, iovec
);
4319 release_sock_fd( s
, fd
);
4320 WARN(" -> ERROR %d\n", err
);
4321 WSASetLastError( err
);
4322 return SOCKET_ERROR
;
4325 /***********************************************************************
4326 * WSCInstallProvider (WS2_32.88)
4328 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
4329 LPCWSTR lpszProviderDllPath
,
4330 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
4331 DWORD dwNumberOfEntries
,
4334 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
4335 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
4336 dwNumberOfEntries
, lpErrno
);
4342 /***********************************************************************
4343 * WSCDeinstallProvider (WS2_32.83)
4345 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
4347 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
4353 /***********************************************************************
4354 * WSAAccept (WS2_32.26)
4356 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
4357 LPCONDITIONPROC lpfnCondition
, DWORD dwCallbackData
)
4360 int ret
= 0, size
= 0;
4361 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
4362 /* QOS SQOS, GQOS; */
4365 SOCKADDR src_addr
, dst_addr
;
4367 TRACE("Socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %d\n",
4368 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
4371 size
= sizeof(src_addr
);
4372 cs
= WS_accept(s
, &src_addr
, &size
);
4374 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
4376 CallerId
.buf
= (char *)&src_addr
;
4377 CallerId
.len
= sizeof(src_addr
);
4379 CallerData
.buf
= NULL
;
4380 CallerData
.len
= (ULONG
)NULL
;
4382 WS_getsockname(cs
, &dst_addr
, &size
);
4384 CalleeId
.buf
= (char *)&dst_addr
;
4385 CalleeId
.len
= sizeof(dst_addr
);
4388 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
4389 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
4394 if (addr
&& addrlen
)
4395 addr
= memcpy(addr
, &src_addr
, (*addrlen
> size
) ? size
: *addrlen
);
4398 SERVER_START_REQ( set_socket_deferred
)
4400 req
->handle
= SOCKET2HANDLE(s
);
4401 req
->deferred
= SOCKET2HANDLE(cs
);
4402 if ( !wine_server_call_err ( req
) )
4404 SetLastError( WSATRY_AGAIN
);
4405 WS_closesocket( cs
);
4409 return SOCKET_ERROR
;
4412 SetLastError(WSAECONNREFUSED
);
4413 return SOCKET_ERROR
;
4415 FIXME("Unknown return type from Condition function\n");
4416 SetLastError(WSAENOTSOCK
);
4417 return SOCKET_ERROR
;
4421 /***********************************************************************
4422 * WSADuplicateSocketA (WS2_32.32)
4424 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
4428 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
4429 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
4430 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
4431 /* I don't know what the real Windoze does next, this is a hack */
4432 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
4433 * the target use the global duplicate, or we could copy a reference to us to the structure
4434 * and let the target duplicate it from us, but let's do it as simple as possible */
4435 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
4436 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
4437 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
4438 0, FALSE
, DUPLICATE_SAME_ACCESS
);
4439 CloseHandle(hProcess
);
4440 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
4444 /***********************************************************************
4445 * WSADuplicateSocketW (WS2_32.33)
4447 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
4451 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
4453 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
4454 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
4455 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
4456 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
4457 0, FALSE
, DUPLICATE_SAME_ACCESS
);
4458 CloseHandle(hProcess
);
4459 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
4463 /***********************************************************************
4464 * WSAInstallServiceClassA (WS2_32.48)
4466 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
4468 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
4469 WSASetLastError(WSAEACCES
);
4470 return SOCKET_ERROR
;
4473 /***********************************************************************
4474 * WSAInstallServiceClassW (WS2_32.49)
4476 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
4478 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
4479 WSASetLastError(WSAEACCES
);
4480 return SOCKET_ERROR
;
4483 /***********************************************************************
4484 * WSARemoveServiceClass (WS2_32.70)
4486 int WINAPI
WSARemoveServiceClass(LPGUID info
)
4488 FIXME("Request to remove service %p\n",info
);
4489 WSASetLastError(WSATYPE_NOT_FOUND
);
4490 return SOCKET_ERROR
;
4493 /***********************************************************************
4494 * WSAStringToAddressA (WS2_32.80)
4496 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
4498 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
4499 LPSOCKADDR lpAddress
,
4500 LPINT lpAddressLength
)
4503 struct in_addr inetaddr
;
4504 LPSTR workBuffer
=NULL
,ptrPort
;
4506 TRACE( "(%s, %x, %p, %p, %p)\n", AddressString
, AddressFamily
, lpProtocolInfo
,
4507 lpAddress
, lpAddressLength
);
4509 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
4513 workBuffer
= HeapAlloc( GetProcessHeap(), 0, strlen(AddressString
)+1 );
4516 strcpy(workBuffer
,AddressString
);
4517 switch (AddressFamily
)
4520 /* caller wants to know the size of the socket buffer */
4521 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
4523 *lpAddressLength
= sizeof(SOCKADDR_IN
);
4528 /* caller wants to translate an AddressString into a SOCKADDR */
4531 memset(lpAddress
,0,sizeof(SOCKADDR_IN
));
4532 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= AF_INET
;
4533 ptrPort
= strchr(workBuffer
,':');
4536 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= (WS_u_short
)atoi(ptrPort
+1);
4540 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= 0;
4541 if (inet_aton(workBuffer
, &inetaddr
) > 0)
4543 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
4551 FIXME("(%s, %x, %p, %p, %p) - ProtocolInfo not implemented!\n",
4552 AddressString
, AddressFamily
,
4553 lpProtocolInfo
, lpAddress
, lpAddressLength
);
4557 FIXME("(%s, %x, %p, %p, %p) - AddressFamiliy not implemented!\n",
4558 AddressString
, AddressFamily
,
4559 lpProtocolInfo
, lpAddress
, lpAddressLength
);
4561 HeapFree( GetProcessHeap(), 0, workBuffer
);
4564 res
= WSA_NOT_ENOUGH_MEMORY
;
4570 WSASetLastError(res
);
4571 return SOCKET_ERROR
;
4574 /***********************************************************************
4575 * WSAStringToAddressW (WS2_32.81)
4577 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
4578 * If this should be the case, it would be required to map these digits
4579 * to Unicode digits (0-9) using FoldString first.
4581 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
4583 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
4584 LPSOCKADDR lpAddress
,
4585 LPINT lpAddressLength
)
4588 LPSTR workBuffer
=NULL
;
4589 WSAPROTOCOL_INFOA infoA
;
4590 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
4592 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
4593 lpAddress
, lpAddressLength
);
4595 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
4597 /* if ProtocolInfo is available - convert to ANSI variant */
4600 lpProtoInfoA
= &infoA
;
4601 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
4603 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
4604 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
4606 WSASetLastError( WSAEINVAL
);
4607 return SOCKET_ERROR
;
4613 /* Translate AddressString to ANSI code page - assumes that only
4614 standard digits 0-9 are used with this API call */
4615 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
4616 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
4620 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
4621 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
4622 lpAddress
,lpAddressLength
);
4623 HeapFree( GetProcessHeap(), 0, workBuffer
);
4627 res
= WSA_NOT_ENOUGH_MEMORY
;
4632 WSASetLastError(res
);
4633 return SOCKET_ERROR
;
4636 /***********************************************************************
4637 * WSAAddressToStringA (WS2_32.27)
4639 * See WSAAddressToStringW
4641 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
4642 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
4646 CHAR buffer
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4649 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
4651 if (!sockaddr
|| len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
4652 if (!string
|| !lenstr
) return SOCKET_ERROR
;
4654 /* sin_family is guaranteed to be the first u_short */
4655 if (((SOCKADDR_IN
*)sockaddr
)->sin_family
!= AF_INET
) return SOCKET_ERROR
;
4657 sprintf( buffer
, "%u.%u.%u.%u:%u",
4658 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
4659 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
4660 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
4661 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
4662 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
4664 p
= strchr( buffer
, ':' );
4665 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
4667 size
= strlen( buffer
);
4672 WSASetLastError(WSAEFAULT
);
4673 return SOCKET_ERROR
;
4676 strcpy( string
, buffer
);
4680 /***********************************************************************
4681 * WSAAddressToStringW (WS2_32.28)
4683 * Convert a sockaddr address into a readable address string.
4686 * sockaddr [I] Pointer to a sockaddr structure.
4687 * len [I] Size of the sockaddr structure.
4688 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
4689 * string [I/O] Pointer to a buffer to receive the address string.
4690 * lenstr [I/O] Size of the receive buffer in WCHARs.
4694 * Failure: SOCKET_ERROR
4697 * The 'info' parameter is ignored.
4700 * Only supports AF_INET addresses.
4702 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
4703 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
4707 WCHAR buffer
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4708 static const WCHAR format
[] = { '%','u','.','%','u','.','%','u','.','%','u',':','%','u',0 };
4711 TRACE( "(%p, %x, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
4713 if (!sockaddr
|| len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
4714 if (!string
|| !lenstr
) return SOCKET_ERROR
;
4716 /* sin_family is guaranteed to be the first u_short */
4717 if (((SOCKADDR_IN
*)sockaddr
)->sin_family
!= AF_INET
) return SOCKET_ERROR
;
4719 sprintfW( buffer
, format
,
4720 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
4721 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
4722 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
4723 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
4724 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
4726 p
= strchrW( buffer
, ':' );
4727 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
4729 size
= lstrlenW( buffer
);
4734 return SOCKET_ERROR
;
4737 lstrcpyW( string
, buffer
);
4741 /***********************************************************************
4742 * WSAEnumNameSpaceProvidersA (WS2_32.34)
4744 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
4746 FIXME( "(%p %p) Stub!\n", len
, buffer
);
4750 /***********************************************************************
4751 * WSAEnumNameSpaceProvidersW (WS2_32.35)
4753 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
4755 FIXME( "(%p %p) Stub!\n", len
, buffer
);
4759 /***********************************************************************
4760 * WSAGetQOSByName (WS2_32.41)
4762 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
4764 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
4768 /***********************************************************************
4769 * WSAGetServiceClassInfoA (WS2_32.42)
4771 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
4772 LPWSASERVICECLASSINFOA info
)
4774 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
4776 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4777 return SOCKET_ERROR
;
4780 /***********************************************************************
4781 * WSAGetServiceClassInfoW (WS2_32.43)
4783 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
4784 LPWSASERVICECLASSINFOW info
)
4786 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
4788 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4789 return SOCKET_ERROR
;
4792 /***********************************************************************
4793 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
4795 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
4797 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
4798 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4799 return SOCKET_ERROR
;
4802 /***********************************************************************
4803 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
4805 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
4807 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
4808 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4809 return SOCKET_ERROR
;
4812 /***********************************************************************
4813 * WSALookupServiceBeginA (WS2_32.59)
4815 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
4816 DWORD dwControlFlags
,
4819 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
4821 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4822 return SOCKET_ERROR
;
4825 /***********************************************************************
4826 * WSALookupServiceBeginW (WS2_32.60)
4828 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
4829 DWORD dwControlFlags
,
4832 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
4834 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4835 return SOCKET_ERROR
;
4838 /***********************************************************************
4839 * WSALookupServiceBeginW (WS2_32.61)
4841 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
4843 FIXME("(%p) Stub!\n", lookup
);
4847 /***********************************************************************
4848 * WSALookupServiceNextA (WS2_32.62)
4850 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
4852 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
4856 /***********************************************************************
4857 * WSALookupServiceNextW (WS2_32.63)
4859 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
4861 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
4865 /***********************************************************************
4866 * WSANtohl (WS2_32.64)
4868 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
4870 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
4872 if (!lphostlong
) return WSAEFAULT
;
4874 *lphostlong
= ntohl( netlong
);
4878 /***********************************************************************
4879 * WSANtohs (WS2_32.65)
4881 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
4883 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
4885 if (!lphostshort
) return WSAEFAULT
;
4887 *lphostshort
= ntohs( netshort
);
4891 /***********************************************************************
4892 * WSAProviderConfigChange (WS2_32.66)
4894 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
4895 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
4897 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
4898 return SOCKET_ERROR
;
4901 /***********************************************************************
4902 * WSARecvDisconnect (WS2_32.68)
4904 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
4906 TRACE( "(0x%04lx %p)\n", s
, disconnectdata
);
4908 return WS_shutdown( s
, 0 );
4911 /***********************************************************************
4912 * WSASetServiceA (WS2_32.76)
4914 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
4916 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
4920 /***********************************************************************
4921 * WSASetServiceW (WS2_32.77)
4923 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
4925 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
4929 /***********************************************************************
4930 * WSCEnableNSProvider (WS2_32.84)
4932 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
4934 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
4938 /***********************************************************************
4939 * WSCGetProviderPath (WS2_32.86)
4941 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
4943 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
4945 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
4947 *errcode
= WSAEINVAL
;
4948 return SOCKET_ERROR
;
4951 /***********************************************************************
4952 * WSCInstallNameSpace (WS2_32.87)
4954 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
4955 DWORD version
, LPGUID provider
)
4957 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
4958 namespace, version
, debugstr_guid(provider
) );
4962 /***********************************************************************
4963 * WSCUnInstallNameSpace (WS2_32.89)
4965 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
4967 FIXME("(%p) Stub!\n", lpProviderId
);
4971 /***********************************************************************
4972 * WSCWriteProviderOrder (WS2_32.91)
4974 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
4976 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);