2 * based on Windows Sockets 1.1 specs
4 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
5 * Copyright (C) 2005 Marcus Meissner
6 * Copyright (C) 2006-2008 Kai Blin
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * NOTE: If you make any changes to fix a particular app, make sure
23 * they don't break something else like Netscape or telnet and ftp
24 * clients and servers (www.winsite.com got a lot of those).
28 #include "wine/port.h"
33 #include <sys/types.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 # include <sys/ioctl.h>
40 #ifdef HAVE_SYS_FILIO_H
41 # include <sys/filio.h>
43 #ifdef HAVE_SYS_SOCKIO_H
44 # include <sys/sockio.h>
48 # include <sys/so_ioctl.h>
51 #ifdef HAVE_SYS_PARAM_H
52 # include <sys/param.h>
58 #ifdef HAVE_SYS_WAIT_H
59 # include <sys/wait.h>
64 #ifdef HAVE_SYS_SOCKET_H
65 #include <sys/socket.h>
67 #ifdef HAVE_NETINET_IN_H
68 # include <netinet/in.h>
70 #ifdef HAVE_NETINET_TCP_H
71 # include <netinet/tcp.h>
73 #ifdef HAVE_ARPA_INET_H
74 # include <arpa/inet.h>
79 #ifdef HAVE_SYS_ERRNO_H
80 #include <sys/errno.h>
89 #ifdef HAVE_ARPA_NAMESER_H
90 # include <arpa/nameser.h>
99 #ifdef HAVE_NETIPX_IPX_H
100 # include <netipx/ipx.h>
102 #elif defined(HAVE_LINUX_IPX_H)
103 # ifdef HAVE_ASM_TYPES_H
104 # include <asm/types.h>
106 # include <linux/ipx.h>
113 #ifdef HAVE_SYS_POLL_H
114 # include <sys/poll.h>
116 #ifdef HAVE_SYS_TIME_H
117 # include <sys/time.h>
120 #define NONAMELESSUNION
121 #define NONAMELESSSTRUCT
122 #include "ntstatus.h"
123 #define WIN32_NO_STATUS
128 #include "winerror.h"
130 #include "winsock2.h"
132 #include "ws2tcpip.h"
136 #include "iphlpapi.h"
137 #include "wine/server.h"
138 #include "wine/debug.h"
139 #include "wine/unicode.h"
142 # include "wsnwlink.h"
146 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
147 # define sipx_network sipx_addr.x_net
148 # define sipx_node sipx_addr.x_host.c_host
149 #endif /* __FreeBSD__ */
152 #define INADDR_NONE ~0UL
155 WINE_DEFAULT_DEBUG_CHANNEL(winsock
);
157 /* critical section to protect some non-reentrant net function */
158 extern CRITICAL_SECTION csWSgetXXXbyYYY
;
160 union generic_unix_sockaddr
162 struct sockaddr addr
;
163 char data
[128]; /* should be big enough for all families */
166 static inline const char *debugstr_sockaddr( const struct WS_sockaddr
*a
)
168 if (!a
) return "(nil)";
169 return wine_dbg_sprintf("{ family %d, address %s, port %d }",
170 ((const struct sockaddr_in
*)a
)->sin_family
,
171 inet_ntoa(((const struct sockaddr_in
*)a
)->sin_addr
),
172 ntohs(((const struct sockaddr_in
*)a
)->sin_port
));
175 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
176 #define SOCKET2HANDLE(s) ((HANDLE)(s))
177 #define HANDLE2SOCKET(h) ((SOCKET)(h))
179 /****************************************************************
180 * Async IO declarations
181 ****************************************************************/
183 typedef struct ws2_async
187 LPWSAOVERLAPPED user_overlapped
;
188 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func
;
189 IO_STATUS_BLOCK local_iosb
;
190 struct iovec iovec
[WS_MSG_MAXIOVLEN
];
192 struct WS_sockaddr
*addr
;
195 int val
; /* for send operations */
196 int *ptr
; /* for recv operations */
201 /****************************************************************/
203 /* ----------------------------------- internal data */
205 /* ws_... struct conversion flags */
207 typedef struct /* WSAAsyncSelect() control struct */
209 HANDLE service
, event
, sock
;
215 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
216 #define WS_MAX_UDP_DATAGRAM 1024
217 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
219 /* hostent's, servent's and protent's are stored in one buffer per thread,
220 * as documented on MSDN for the functions that return any of the buffers */
221 struct per_thread_data
224 struct WS_hostent
*he_buffer
;
225 struct WS_servent
*se_buffer
;
226 struct WS_protoent
*pe_buffer
;
232 static INT num_startup
; /* reference counter */
233 static FARPROC blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
235 /* function prototypes */
236 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
237 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
);
238 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
240 int WSAIOCTL_GetInterfaceCount(void);
241 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
244 UINT
wsaHerrno(int errnr
);
246 #define MAP_OPTION(opt) { WS_##opt, opt }
248 static const int ws_sock_map
[][2] =
250 MAP_OPTION( SO_DEBUG
),
251 MAP_OPTION( SO_ACCEPTCONN
),
252 MAP_OPTION( SO_REUSEADDR
),
253 MAP_OPTION( SO_KEEPALIVE
),
254 MAP_OPTION( SO_DONTROUTE
),
255 MAP_OPTION( SO_BROADCAST
),
256 MAP_OPTION( SO_LINGER
),
257 MAP_OPTION( SO_OOBINLINE
),
258 MAP_OPTION( SO_SNDBUF
),
259 MAP_OPTION( SO_RCVBUF
),
260 MAP_OPTION( SO_ERROR
),
261 MAP_OPTION( SO_TYPE
),
263 MAP_OPTION( SO_RCVTIMEO
),
266 MAP_OPTION( SO_SNDTIMEO
),
270 static const int ws_tcp_map
[][2] =
273 MAP_OPTION( TCP_NODELAY
),
277 static const int ws_ip_map
[][2] =
279 MAP_OPTION( IP_MULTICAST_IF
),
280 MAP_OPTION( IP_MULTICAST_TTL
),
281 MAP_OPTION( IP_MULTICAST_LOOP
),
282 MAP_OPTION( IP_ADD_MEMBERSHIP
),
283 MAP_OPTION( IP_DROP_MEMBERSHIP
),
284 MAP_OPTION( IP_OPTIONS
),
286 MAP_OPTION( IP_HDRINCL
),
288 MAP_OPTION( IP_TOS
),
289 MAP_OPTION( IP_TTL
),
292 static const int ws_af_map
[][2] =
294 MAP_OPTION( AF_UNSPEC
),
295 MAP_OPTION( AF_INET
),
296 MAP_OPTION( AF_INET6
),
298 MAP_OPTION( AF_IPX
),
302 static const int ws_socktype_map
[][2] =
304 MAP_OPTION( SOCK_DGRAM
),
305 MAP_OPTION( SOCK_STREAM
),
306 MAP_OPTION( SOCK_RAW
),
309 static const int ws_proto_map
[][2] =
311 MAP_OPTION( IPPROTO_IP
),
312 MAP_OPTION( IPPROTO_TCP
),
313 MAP_OPTION( IPPROTO_UDP
),
314 MAP_OPTION( IPPROTO_ICMP
),
315 MAP_OPTION( IPPROTO_IGMP
),
316 MAP_OPTION( IPPROTO_RAW
),
319 static const int ws_aiflag_map
[][2] =
321 MAP_OPTION( AI_PASSIVE
),
322 MAP_OPTION( AI_CANONNAME
),
323 MAP_OPTION( AI_NUMERICHOST
),
324 /* Linux/UNIX knows a lot more. But Windows only
325 * has 3 as far as I could see. -Marcus
329 static const int ws_niflag_map
[][2] =
331 MAP_OPTION( NI_NOFQDN
),
332 MAP_OPTION( NI_NUMERICHOST
),
333 MAP_OPTION( NI_NAMEREQD
),
334 MAP_OPTION( NI_NUMERICSERV
),
335 MAP_OPTION( NI_DGRAM
),
338 static const int ws_eai_map
[][2] =
340 MAP_OPTION( EAI_AGAIN
),
341 MAP_OPTION( EAI_BADFLAGS
),
342 MAP_OPTION( EAI_FAIL
),
343 MAP_OPTION( EAI_FAMILY
),
344 MAP_OPTION( EAI_MEMORY
),
345 /* Note: EAI_NODATA is deprecated, but still
346 * used by Windows and Linux... We map the newer
347 * EAI_NONAME to EAI_NODATA for now until Windows
351 MAP_OPTION( EAI_NODATA
),
354 { WS_EAI_NODATA
, EAI_NONAME
},
357 MAP_OPTION( EAI_SERVICE
),
358 MAP_OPTION( EAI_SOCKTYPE
),
362 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
364 static inline DWORD
NtStatusToWSAError( const DWORD status
)
366 /* We only need to cover the status codes set by server async request handling */
370 case STATUS_SUCCESS
: wserr
= 0; break;
371 case STATUS_PENDING
: wserr
= WSA_IO_PENDING
; break;
372 case STATUS_OBJECT_TYPE_MISMATCH
: wserr
= WSAENOTSOCK
; break;
373 case STATUS_INVALID_HANDLE
: wserr
= WSAEBADF
; break;
374 case STATUS_INVALID_PARAMETER
: wserr
= WSAEINVAL
; break;
375 case STATUS_PIPE_DISCONNECTED
: wserr
= WSAESHUTDOWN
; break;
376 case STATUS_CANCELLED
: wserr
= WSA_OPERATION_ABORTED
; break;
377 case STATUS_TIMEOUT
: wserr
= WSAETIMEDOUT
; break;
378 case STATUS_NO_MEMORY
: wserr
= WSAEFAULT
; break;
380 if ( status
>= WSABASEERR
&& status
<= WSABASEERR
+1004 )
381 /* It is not an NT status code but a winsock error */
385 wserr
= RtlNtStatusToDosError( status
);
386 FIXME( "Status code %08x converted to DOS error code %x\n", status
, wserr
);
392 /* set last error code from NT status without mapping WSA errors */
393 static inline unsigned int set_error( unsigned int err
)
397 err
= NtStatusToWSAError( err
);
403 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
406 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
411 static inline void release_sock_fd( SOCKET s
, int fd
)
413 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
416 static void _enable_event( HANDLE s
, unsigned int event
,
417 unsigned int sstate
, unsigned int cstate
)
419 SERVER_START_REQ( enable_socket_event
)
423 req
->sstate
= sstate
;
424 req
->cstate
= cstate
;
425 wine_server_call( req
);
430 static int _is_blocking(SOCKET s
)
433 SERVER_START_REQ( get_socket_event
)
435 req
->handle
= SOCKET2HANDLE(s
);
436 req
->service
= FALSE
;
438 wine_server_call( req
);
439 ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
445 static unsigned int _get_sock_mask(SOCKET s
)
448 SERVER_START_REQ( get_socket_event
)
450 req
->handle
= SOCKET2HANDLE(s
);
451 req
->service
= FALSE
;
453 wine_server_call( req
);
460 static void _sync_sock_state(SOCKET s
)
462 /* do a dummy wineserver request in order to let
463 the wineserver run through its select loop once */
464 (void)_is_blocking(s
);
467 static int _get_sock_error(SOCKET s
, unsigned int bit
)
469 int events
[FD_MAX_EVENTS
];
471 SERVER_START_REQ( get_socket_event
)
473 req
->handle
= SOCKET2HANDLE(s
);
474 req
->service
= FALSE
;
476 wine_server_set_reply( req
, events
, sizeof(events
) );
477 wine_server_call( req
);
483 static struct per_thread_data
*get_per_thread_data(void)
485 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
486 /* lazy initialization */
489 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
490 NtCurrentTeb()->WinSockData
= ptb
;
495 static void free_per_thread_data(void)
497 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
501 /* delete scratch buffers */
502 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
503 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
504 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
505 ptb
->he_buffer
= NULL
;
506 ptb
->se_buffer
= NULL
;
507 ptb
->pe_buffer
= NULL
;
509 HeapFree( GetProcessHeap(), 0, ptb
);
510 NtCurrentTeb()->WinSockData
= NULL
;
513 /***********************************************************************
514 * DllMain (WS2_32.init)
516 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
518 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
520 case DLL_PROCESS_ATTACH
:
522 case DLL_PROCESS_DETACH
:
523 free_per_thread_data();
526 case DLL_THREAD_DETACH
:
527 free_per_thread_data();
533 /***********************************************************************
536 * Converts socket flags from Windows format.
537 * Return 1 if converted, 0 if not (error).
539 static int convert_sockopt(INT
*level
, INT
*optname
)
546 for(i
=0; i
<sizeof(ws_sock_map
)/sizeof(ws_sock_map
[0]); i
++) {
547 if( ws_sock_map
[i
][0] == *optname
)
549 *optname
= ws_sock_map
[i
][1];
553 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
556 *level
= IPPROTO_TCP
;
557 for(i
=0; i
<sizeof(ws_tcp_map
)/sizeof(ws_tcp_map
[0]); i
++) {
558 if ( ws_tcp_map
[i
][0] == *optname
)
560 *optname
= ws_tcp_map
[i
][1];
564 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
568 for(i
=0; i
<sizeof(ws_ip_map
)/sizeof(ws_ip_map
[0]); i
++) {
569 if (ws_ip_map
[i
][0] == *optname
)
571 *optname
= ws_ip_map
[i
][1];
575 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
577 default: FIXME("Unimplemented or unknown socket level\n");
582 static inline BOOL
is_timeout_option( int optname
)
585 if (optname
== SO_RCVTIMEO
) return TRUE
;
588 if (optname
== SO_SNDTIMEO
) return TRUE
;
593 /* ----------------------------------- Per-thread info (or per-process?) */
595 static char *strdup_lower(const char *str
)
598 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
602 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
605 else SetLastError(WSAENOBUFS
);
609 static inline int sock_error_p(int s
)
611 unsigned int optval
, optlen
;
613 optlen
= sizeof(optval
);
614 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
615 if (optval
) WARN("\t[%i] error: %d\n", s
, optval
);
619 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
620 * from an fd and return the value converted to milli seconds
621 * or -1 if there is an infinite time out */
622 static inline int get_rcvsnd_timeo( int fd
, int optname
)
625 unsigned int len
= sizeof(tv
);
626 int ret
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
628 ret
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
629 if( ret
<= 0 ) /* tv == {0,0} means infinite time out */
634 /* macro wrappers for portability */
636 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
638 #define GET_RCVTIMEO(fd) (-1)
642 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
644 #define GET_SNDTIMEO(fd) (-1)
647 /* utility: given an fd, will block until one of the events occurs */
648 static inline int do_block( int fd
, int events
, int timeout
)
656 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
667 convert_af_w2u(int windowsaf
) {
670 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
671 if (ws_af_map
[i
][0] == windowsaf
)
672 return ws_af_map
[i
][1];
673 FIXME("unhandled Windows address family %d\n", windowsaf
);
678 convert_af_u2w(int unixaf
) {
681 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
682 if (ws_af_map
[i
][1] == unixaf
)
683 return ws_af_map
[i
][0];
684 FIXME("unhandled UNIX address family %d\n", unixaf
);
689 convert_proto_w2u(int windowsproto
) {
692 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
693 if (ws_proto_map
[i
][0] == windowsproto
)
694 return ws_proto_map
[i
][1];
695 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
700 convert_proto_u2w(int unixproto
) {
703 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
704 if (ws_proto_map
[i
][1] == unixproto
)
705 return ws_proto_map
[i
][0];
706 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
711 convert_socktype_w2u(int windowssocktype
) {
714 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
715 if (ws_socktype_map
[i
][0] == windowssocktype
)
716 return ws_socktype_map
[i
][1];
717 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
722 convert_socktype_u2w(int unixsocktype
) {
725 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
726 if (ws_socktype_map
[i
][1] == unixsocktype
)
727 return ws_socktype_map
[i
][0];
728 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
732 /* ----------------------------------- API -----
734 * Init / cleanup / error checking.
737 /***********************************************************************
738 * WSAStartup (WS2_32.115)
740 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
742 TRACE("verReq=%x\n", wVersionRequested
);
744 if (LOBYTE(wVersionRequested
) < 1)
745 return WSAVERNOTSUPPORTED
;
747 if (!lpWSAData
) return WSAEINVAL
;
751 /* that's the whole of the negotiation for now */
752 lpWSAData
->wVersion
= wVersionRequested
;
753 /* return winsock information */
754 lpWSAData
->wHighVersion
= 0x0202;
755 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
756 strcpy(lpWSAData
->szSystemStatus
, "Running" );
757 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
758 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
759 /* don't do anything with lpWSAData->lpVendorInfo */
760 /* (some apps don't allocate the space for this field) */
762 TRACE("succeeded\n");
767 /***********************************************************************
768 * WSACleanup (WS2_32.116)
770 INT WINAPI
WSACleanup(void)
776 SetLastError(WSANOTINITIALISED
);
781 /***********************************************************************
782 * WSAGetLastError (WINSOCK.111)
783 * WSAGetLastError (WS2_32.111)
785 INT WINAPI
WSAGetLastError(void)
787 return GetLastError();
790 /***********************************************************************
791 * WSASetLastError (WS2_32.112)
793 void WINAPI
WSASetLastError(INT iError
) {
794 SetLastError(iError
);
797 static struct WS_hostent
*check_buffer_he(int size
)
799 struct per_thread_data
* ptb
= get_per_thread_data();
802 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
803 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
805 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
806 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
807 return ptb
->he_buffer
;
810 static struct WS_servent
*check_buffer_se(int size
)
812 struct per_thread_data
* ptb
= get_per_thread_data();
815 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
816 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
818 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
819 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
820 return ptb
->se_buffer
;
823 static struct WS_protoent
*check_buffer_pe(int size
)
825 struct per_thread_data
* ptb
= get_per_thread_data();
828 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
829 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
831 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
832 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
833 return ptb
->pe_buffer
;
836 /* ----------------------------------- i/o APIs */
839 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf)== WS_AF_IPX || (pf) == WS_AF_INET6)
841 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf) == WS_AF_INET6)
845 /**********************************************************************/
847 /* Returns the length of the converted address if successful, 0 if it was too small to
850 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
851 union generic_unix_sockaddr
*uaddr
)
853 unsigned int uaddrlen
= 0;
855 switch (wsaddr
->sa_family
)
860 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
861 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
863 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
866 uaddrlen
= sizeof(struct sockaddr_ipx
);
867 memset( uaddr
, 0, uaddrlen
);
868 uipx
->sipx_family
=AF_IPX
;
869 uipx
->sipx_port
=wsipx
->sa_socket
;
870 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
873 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
874 #ifdef IPX_FRAME_NONE
875 uipx
->sipx_type
=IPX_FRAME_NONE
;
881 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
882 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
884 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
885 * scope_id, one without.
887 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
888 uaddrlen
= sizeof(struct sockaddr_in6
);
889 memset( uaddr
, 0, uaddrlen
);
890 uin6
->sin6_family
= AF_INET6
;
891 uin6
->sin6_port
= win6
->sin6_port
;
892 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
893 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
894 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
896 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
899 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
903 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
904 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
906 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
908 uaddrlen
= sizeof(struct sockaddr_in
);
909 memset( uaddr
, 0, uaddrlen
);
910 uin
->sin_family
= AF_INET
;
911 uin
->sin_port
= win
->sin_port
;
912 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
916 /* Try to determine the needed space by the passed windows sockaddr space */
918 default: /* likely a ipv4 address */
919 case sizeof(struct WS_sockaddr_in
):
920 uaddrlen
= sizeof(struct sockaddr_in
);
923 case sizeof(struct WS_sockaddr_ipx
):
924 uaddrlen
= sizeof(struct sockaddr_ipx
);
927 case sizeof(struct WS_sockaddr_in6
):
928 case sizeof(struct WS_sockaddr_in6_old
):
929 uaddrlen
= sizeof(struct sockaddr_in6
);
932 memset( uaddr
, 0, uaddrlen
);
936 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
942 static BOOL
is_sockaddr_bound(const struct sockaddr
*uaddr
, int uaddrlen
)
944 switch (uaddr
->sa_family
)
948 FIXME("don't know how to tell if IPX socket is bound, assuming it is!\n");
953 static const struct sockaddr_in6 emptyAddr
;
954 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) uaddr
;
955 return in6
->sin6_port
|| memcmp(&in6
->sin6_addr
, &emptyAddr
.sin6_addr
, sizeof(struct in6_addr
));
959 static const struct sockaddr_in emptyAddr
;
960 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) uaddr
;
961 return in
->sin_port
|| memcmp(&in
->sin_addr
, &emptyAddr
.sin_addr
, sizeof(struct in_addr
));
966 FIXME("unknown address family %d\n", uaddr
->sa_family
);
971 /* Returns 0 if successful, -1 if the buffer is too small */
972 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
976 switch(uaddr
->sa_family
)
981 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
982 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
985 switch (*wsaddrlen
) /* how much can we copy? */
989 *wsaddrlen
= sizeof(*wsipx
);
990 wsipx
->sa_socket
=uipx
->sipx_port
;
994 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
1002 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
1008 wsipx
->sa_family
=WS_AF_IPX
;
1019 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
1020 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
1022 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
1024 win6old
->sin6_family
= WS_AF_INET6
;
1025 win6old
->sin6_port
= uin6
->sin6_port
;
1026 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
1027 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1028 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1029 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1030 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
1031 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
1032 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
1033 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
1039 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
1040 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
1042 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
1044 win
->sin_family
= WS_AF_INET
;
1045 win
->sin_port
= uin
->sin_port
;
1046 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
1047 memset(&win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
1048 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
1052 memset(wsaddr
,0,*wsaddrlen
);
1056 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
1062 /**************************************************************************
1063 * Functions for handling overlapped I/O
1064 **************************************************************************/
1066 /* user APC called upon async completion */
1067 static void WINAPI
ws2_async_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1069 ws2_async
*wsa
= arg
;
1071 if (wsa
->completion_func
) wsa
->completion_func( NtStatusToWSAError(iosb
->u
.Status
),
1072 iosb
->Information
, wsa
->user_overlapped
,
1074 HeapFree( GetProcessHeap(), 0, wsa
);
1077 /***********************************************************************
1078 * WS2_recv (INTERNAL)
1080 * Workhorse for both synchronous and asynchronous recv() operations.
1082 static int WS2_recv( int fd
, struct iovec
* iov
, int count
,
1083 struct WS_sockaddr
*lpFrom
, LPINT lpFromlen
,
1087 union generic_unix_sockaddr unix_sockaddr
;
1090 hdr
.msg_name
= NULL
;
1094 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
1095 hdr
.msg_name
= &unix_sockaddr
;
1098 hdr
.msg_namelen
= 0;
1101 hdr
.msg_iovlen
= count
;
1102 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1103 hdr
.msg_accrights
= NULL
;
1104 hdr
.msg_accrightslen
= 0;
1106 hdr
.msg_control
= NULL
;
1107 hdr
.msg_controllen
= 0;
1111 if ( (n
= recvmsg(fd
, &hdr
, *lpFlags
)) == -1 )
1115 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, lpFrom
, lpFromlen
) != 0 )
1117 /* The from buffer was too small, but we read the data
1118 * anyway. Is that really bad?
1125 /***********************************************************************
1126 * WS2_async_recv (INTERNAL)
1128 * Handler for overlapped recv() operations.
1130 static NTSTATUS
WS2_async_recv( void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, ULONG_PTR
*total
)
1132 ws2_async
* wsa
= user
;
1137 case STATUS_ALERTED
:
1138 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
1141 result
= WS2_recv( fd
, wsa
->iovec
, wsa
->n_iovecs
,
1142 wsa
->addr
, wsa
->addrlen
.ptr
, &wsa
->flags
);
1143 wine_server_release_fd( wsa
->hSocket
, fd
);
1146 status
= STATUS_SUCCESS
;
1147 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1151 if (errno
== EINTR
|| errno
== EAGAIN
)
1153 status
= STATUS_PENDING
;
1154 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1159 status
= wsaErrno(); /* FIXME: is this correct ???? */
1164 if (status
!= STATUS_PENDING
)
1166 iosb
->u
.Status
= status
;
1167 iosb
->Information
= *total
= result
;
1172 /***********************************************************************
1173 * WS2_send (INTERNAL)
1175 * Workhorse for both synchronous and asynchronous send() operations.
1177 static int WS2_send( int fd
, struct iovec
* iov
, int count
,
1178 const struct WS_sockaddr
*to
, INT tolen
, DWORD dwFlags
)
1181 union generic_unix_sockaddr unix_addr
;
1184 hdr
.msg_name
= NULL
;
1185 hdr
.msg_namelen
= 0;
1189 hdr
.msg_name
= &unix_addr
;
1190 hdr
.msg_namelen
= ws_sockaddr_ws2u( to
, tolen
, &unix_addr
);
1191 if ( !hdr
.msg_namelen
)
1197 #if defined(HAVE_IPX) && defined(SOL_IPX)
1198 if(to
->sa_family
== WS_AF_IPX
)
1200 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
1202 unsigned int len
=sizeof(int);
1204 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1205 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1206 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1207 * ipx type in the sockaddr_opx structure with the stored value.
1209 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
1210 uipx
->sipx_type
= val
;
1216 hdr
.msg_iovlen
= count
;
1217 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1218 hdr
.msg_accrights
= NULL
;
1219 hdr
.msg_accrightslen
= 0;
1221 hdr
.msg_control
= NULL
;
1222 hdr
.msg_controllen
= 0;
1226 return sendmsg(fd
, &hdr
, dwFlags
);
1229 /***********************************************************************
1230 * WS2_async_send (INTERNAL)
1232 * Handler for overlapped send() operations.
1234 static NTSTATUS
WS2_async_send(void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, ULONG_PTR
*total
)
1236 ws2_async
* wsa
= user
;
1241 case STATUS_ALERTED
:
1242 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
1245 /* check to see if the data is ready (non-blocking) */
1246 result
= WS2_send( fd
, wsa
->iovec
, wsa
->n_iovecs
, wsa
->addr
, wsa
->addrlen
.val
, wsa
->flags
);
1247 wine_server_release_fd( wsa
->hSocket
, fd
);
1251 status
= STATUS_SUCCESS
;
1252 _enable_event( wsa
->hSocket
, FD_WRITE
, 0, 0 );
1256 if (errno
== EINTR
|| errno
== EAGAIN
)
1258 status
= STATUS_PENDING
;
1259 _enable_event( wsa
->hSocket
, FD_WRITE
, 0, 0 );
1263 /* We set the status to a winsock error code and check for that
1264 later in NtStatusToWSAError () */
1265 status
= wsaErrno();
1271 if (status
!= STATUS_PENDING
)
1273 iosb
->u
.Status
= status
;
1274 iosb
->Information
= *total
= result
;
1279 /***********************************************************************
1280 * WS2_async_shutdown (INTERNAL)
1282 * Handler for shutdown() operations on overlapped sockets.
1284 static NTSTATUS
WS2_async_shutdown( void* user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
)
1286 ws2_async
* wsa
= user
;
1291 case STATUS_ALERTED
:
1292 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
1295 switch ( wsa
->type
)
1297 case ASYNC_TYPE_READ
: err
= shutdown( fd
, 0 ); break;
1298 case ASYNC_TYPE_WRITE
: err
= shutdown( fd
, 1 ); break;
1300 wine_server_release_fd( wsa
->hSocket
, fd
);
1301 status
= err
? wsaErrno() : STATUS_SUCCESS
;
1304 iosb
->u
.Status
= status
;
1308 /***********************************************************************
1309 * WS2_register_async_shutdown (INTERNAL)
1311 * Helper function for WS_shutdown() on overlapped sockets.
1313 static int WS2_register_async_shutdown( SOCKET s
, int type
)
1315 struct ws2_async
*wsa
;
1318 TRACE("s %ld type %d\n", s
, type
);
1320 wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
1324 wsa
->hSocket
= SOCKET2HANDLE(s
);
1326 wsa
->completion_func
= NULL
;
1328 SERVER_START_REQ( register_async
)
1330 req
->handle
= wsa
->hSocket
;
1332 req
->async
.callback
= WS2_async_shutdown
;
1333 req
->async
.iosb
= &wsa
->local_iosb
;
1334 req
->async
.arg
= wsa
;
1335 req
->async
.apc
= ws2_async_apc
;
1336 req
->async
.cvalue
= 0;
1337 status
= wine_server_call( req
);
1341 if (status
!= STATUS_PENDING
)
1343 HeapFree( GetProcessHeap(), 0, wsa
);
1344 return NtStatusToWSAError( status
);
1349 /***********************************************************************
1352 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
,
1358 TRACE("socket %04lx\n", s
);
1359 is_blocking
= _is_blocking(s
);
1364 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1365 if (fd
== -1) return INVALID_SOCKET
;
1367 do_block(fd
, POLLIN
, -1);
1368 _sync_sock_state(s
); /* let wineserver notice connection */
1369 release_sock_fd( s
, fd
);
1370 /* retrieve any error codes from it */
1371 SetLastError(_get_sock_error(s
, FD_ACCEPT_BIT
));
1372 /* FIXME: care about the error? */
1374 SERVER_START_REQ( accept_socket
)
1376 req
->lhandle
= SOCKET2HANDLE(s
);
1377 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
1378 req
->attributes
= OBJ_INHERIT
;
1379 set_error( wine_server_call( req
) );
1380 as
= HANDLE2SOCKET( reply
->handle
);
1385 if (addr
) WS_getpeername(as
, addr
, addrlen32
);
1388 } while (is_blocking
);
1389 return INVALID_SOCKET
;
1392 /***********************************************************************
1395 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
1397 int fd
= get_sock_fd( s
, 0, NULL
);
1398 int res
= SOCKET_ERROR
;
1400 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
1404 if (!name
|| (name
->sa_family
&& !SUPPORTED_PF(name
->sa_family
)))
1406 SetLastError(WSAEAFNOSUPPORT
);
1410 union generic_unix_sockaddr uaddr
;
1411 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
1414 SetLastError(WSAEFAULT
);
1419 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) &uaddr
;
1420 if (name
->sa_family
== WS_AF_INET6
&&
1421 !memcmp(&in6
->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
)))
1424 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
)) == -1)
1426 release_sock_fd( s
, fd
);
1427 SetLastError(WSAEAFNOSUPPORT
);
1428 return INVALID_SOCKET
;
1432 if (name
->sa_family
== WS_AF_INET
)
1434 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
1435 if (memcmp(&in4
->sin_addr
, &magic_loopback_addr
, 4) == 0)
1437 /* Trying to bind to the default host interface, using
1438 * INADDR_ANY instead*/
1439 WARN("Trying to bind to magic IP address, using "
1440 "INADDR_ANY instead.\n");
1441 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_ANY
);
1444 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
1446 int loc_errno
= errno
;
1447 WARN("\tfailure - errno = %i\n", errno
);
1452 SetLastError(WSAENOTSOCK
);
1455 SetLastError(WSAEINVAL
);
1458 SetLastError(wsaErrno());
1464 res
=0; /* success */
1468 release_sock_fd( s
, fd
);
1473 /***********************************************************************
1474 * closesocket (WS2_32.3)
1476 int WINAPI
WS_closesocket(SOCKET s
)
1478 TRACE("socket %04lx\n", s
);
1479 if (CloseHandle(SOCKET2HANDLE(s
))) return 0;
1480 return SOCKET_ERROR
;
1483 /***********************************************************************
1484 * connect (WS2_32.4)
1486 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
1488 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1490 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
1494 union generic_unix_sockaddr uaddr
;
1495 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
1499 SetLastError(WSAEFAULT
);
1503 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
1504 goto connect_success
;
1507 if (errno
== EINPROGRESS
)
1509 /* tell wineserver that a connection is in progress */
1510 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
1511 FD_CONNECT
|FD_READ
|FD_WRITE
,
1512 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
1513 if (_is_blocking(s
))
1517 do_block(fd
, POLLIN
| POLLOUT
, -1);
1518 _sync_sock_state(s
); /* let wineserver notice connection */
1519 /* retrieve any error codes from it */
1520 result
= _get_sock_error(s
, FD_CONNECT_BIT
);
1522 SetLastError(result
);
1525 goto connect_success
;
1530 SetLastError(WSAEWOULDBLOCK
);
1535 SetLastError(wsaErrno());
1537 release_sock_fd( s
, fd
);
1539 return SOCKET_ERROR
;
1542 release_sock_fd( s
, fd
);
1543 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
1544 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
1545 FD_CONNECT
|FD_WINE_LISTENING
);
1549 /***********************************************************************
1550 * WSAConnect (WS2_32.30)
1552 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
1553 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
1554 LPQOS lpSQOS
, LPQOS lpGQOS
)
1556 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
1557 FIXME("unsupported parameters!\n");
1558 return WS_connect( s
, name
, namelen
);
1562 /***********************************************************************
1563 * getpeername (WS2_32.5)
1565 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
1570 TRACE("socket: %04lx, ptr %p, len %08x\n", s
, name
, *namelen
);
1572 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1573 if( (name
== NULL
) || (namelen
== NULL
) )
1575 SetLastError( WSAEFAULT
);
1576 return SOCKET_ERROR
;
1579 fd
= get_sock_fd( s
, 0, NULL
);
1584 union generic_unix_sockaddr uaddr
;
1585 unsigned int uaddrlen
= sizeof(uaddr
);
1587 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
1589 SetLastError(wsaErrno());
1591 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
1593 /* The buffer was too small */
1594 SetLastError(WSAEFAULT
);
1600 release_sock_fd( s
, fd
);
1605 /***********************************************************************
1606 * getsockname (WS2_32.6)
1608 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
1613 TRACE("socket: %04lx, ptr %p, len %8x\n", s
, name
, *namelen
);
1615 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1616 if( (name
== NULL
) || (namelen
== NULL
) )
1618 SetLastError( WSAEFAULT
);
1619 return SOCKET_ERROR
;
1622 fd
= get_sock_fd( s
, 0, NULL
);
1627 union generic_unix_sockaddr uaddr
;
1628 unsigned int uaddrlen
= sizeof(uaddr
);
1630 if (getsockname(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
1632 SetLastError(wsaErrno());
1634 else if (!is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
1636 SetLastError(WSAEINVAL
);
1638 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
1640 /* The buffer was too small */
1641 SetLastError(WSAEFAULT
);
1647 release_sock_fd( s
, fd
);
1652 /***********************************************************************
1653 * getsockopt (WS2_32.7)
1655 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
1656 INT optname
, char *optval
, INT
*optlen
)
1661 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
1662 s
, level
, optname
, optval
, *optlen
);
1670 /* Handle common cases. The special cases are below, sorted
1672 case WS_SO_ACCEPTCONN
:
1673 case WS_SO_BROADCAST
:
1676 case WS_SO_KEEPALIVE
:
1677 case WS_SO_OOBINLINE
:
1679 case WS_SO_REUSEADDR
:
1682 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1683 return SOCKET_ERROR
;
1684 convert_sockopt(&level
, &optname
);
1685 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
1687 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1690 release_sock_fd( s
, fd
);
1693 case WS_SO_DONTLINGER
:
1695 struct linger lingval
;
1696 unsigned int len
= sizeof(struct linger
);
1698 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
1700 SetLastError(WSAEFAULT
);
1701 return SOCKET_ERROR
;
1703 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1704 return SOCKET_ERROR
;
1706 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
1708 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1713 *(BOOL
*)optval
= (lingval
.l_onoff
) ? FALSE
: TRUE
;
1714 *optlen
= sizeof(BOOL
);
1717 release_sock_fd( s
, fd
);
1721 /* As mentioned in setsockopt, Windows ignores this, so we
1722 * always return true here */
1723 case WS_SO_DONTROUTE
:
1724 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
1726 SetLastError(WSAEFAULT
);
1727 return SOCKET_ERROR
;
1729 *(BOOL
*)optval
= TRUE
;
1730 *optlen
= sizeof(BOOL
);
1735 struct linger lingval
;
1736 unsigned int len
= sizeof(struct linger
);
1738 /* struct linger and LINGER have different sizes */
1739 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
1741 SetLastError(WSAEFAULT
);
1742 return SOCKET_ERROR
;
1744 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1745 return SOCKET_ERROR
;
1747 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
1749 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1754 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
1755 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
1756 *optlen
= sizeof(struct linger
);
1759 release_sock_fd( s
, fd
);
1763 case WS_SO_MAX_MSG_SIZE
:
1764 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
1766 SetLastError(WSAEFAULT
);
1767 return SOCKET_ERROR
;
1769 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
1770 *(int *)optval
= 65507;
1771 *optlen
= sizeof(int);
1774 /* SO_OPENTYPE does not require a valid socket handle. */
1775 case WS_SO_OPENTYPE
:
1776 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
1778 SetLastError(WSAEFAULT
);
1779 return SOCKET_ERROR
;
1781 *(int *)optval
= get_per_thread_data()->opentype
;
1782 *optlen
= sizeof(int);
1783 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
1787 case WS_SO_RCVTIMEO
:
1790 case WS_SO_SNDTIMEO
:
1792 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
1795 unsigned int len
= sizeof(struct timeval
);
1797 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
1799 SetLastError(WSAEFAULT
);
1800 return SOCKET_ERROR
;
1802 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1803 return SOCKET_ERROR
;
1805 convert_sockopt(&level
, &optname
);
1806 if (getsockopt(fd
, level
, optname
, &tv
, &len
) != 0 )
1808 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1813 *(int *)optval
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
1814 *optlen
= sizeof(int);
1817 release_sock_fd( s
, fd
);
1822 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
1823 SetLastError(WSAENOPROTOOPT
);
1824 return SOCKET_ERROR
;
1825 } /* end switch(optname) */
1826 }/* end case WS_SOL_SOCKET */
1830 struct WS_sockaddr_ipx addr
;
1831 IPX_ADDRESS_DATA
*data
;
1836 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
1838 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (unsigned int*)optlen
) == -1)
1845 socklen_t len
=sizeof(struct ipx
);
1846 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
1849 *optval
= (int)val
.ipx_pt
;
1852 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
1853 release_sock_fd( s
, fd
);
1858 * On a Win2000 system with one network card there are usually
1859 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
1860 * Using this call you can then retrieve info about this all.
1861 * In case of Linux it is a bit different. Usually you have
1862 * only "one" device active and further it is not possible to
1863 * query things like the linkspeed.
1865 FIXME("IPX_ADDRESS\n");
1866 namelen
= sizeof(struct WS_sockaddr_ipx
);
1867 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
1868 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
1870 data
= (IPX_ADDRESS_DATA
*)optval
;
1871 memcpy(data
->nodenum
,&addr
.sa_nodenum
,sizeof(data
->nodenum
));
1872 memcpy(data
->netnum
,&addr
.sa_netnum
,sizeof(data
->netnum
));
1873 data
->adapternum
= 0;
1874 data
->wan
= FALSE
; /* We are not on a wan for now .. */
1875 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
1876 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
1877 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
1878 * note 1MB = 1000kB in this case */
1881 case IPX_MAX_ADAPTER_NUM
:
1882 FIXME("IPX_MAX_ADAPTER_NUM\n");
1883 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
1887 FIXME("IPX optname:%x\n", optname
);
1888 return SOCKET_ERROR
;
1889 }/* end switch(optname) */
1890 } /* end case NSPROTO_IPX */
1892 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
1893 case WS_IPPROTO_TCP
:
1896 case WS_TCP_NODELAY
:
1897 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1898 return SOCKET_ERROR
;
1899 convert_sockopt(&level
, &optname
);
1900 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
1902 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1905 release_sock_fd( s
, fd
);
1908 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
1909 return SOCKET_ERROR
;
1914 case WS_IP_ADD_MEMBERSHIP
:
1915 case WS_IP_DROP_MEMBERSHIP
:
1919 case WS_IP_MULTICAST_IF
:
1920 case WS_IP_MULTICAST_LOOP
:
1921 case WS_IP_MULTICAST_TTL
:
1925 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1926 return SOCKET_ERROR
;
1927 convert_sockopt(&level
, &optname
);
1928 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
1930 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1933 release_sock_fd( s
, fd
);
1935 case WS_IP_DONTFRAGMENT
:
1936 FIXME("WS_IP_DONTFRAGMENT is always false!\n");
1937 *(BOOL
*)optval
= FALSE
;
1940 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
1941 return SOCKET_ERROR
;
1944 FIXME("Unknown level: 0x%08x\n", level
);
1945 return SOCKET_ERROR
;
1946 } /* end switch(level) */
1949 /***********************************************************************
1953 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
1955 return htonl(hostlong
);
1959 /***********************************************************************
1963 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
1965 return htons(hostshort
);
1968 /***********************************************************************
1969 * WSAHtonl (WS2_32.46)
1970 * From MSDN description of error codes, this function should also
1971 * check if WinSock has been initialized and the socket is a valid
1972 * socket. But why? This function only translates a host byte order
1973 * u_long into a network byte order u_long...
1975 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
1979 *lpnetlong
= htonl(hostlong
);
1982 WSASetLastError(WSAEFAULT
);
1983 return SOCKET_ERROR
;
1986 /***********************************************************************
1987 * WSAHtons (WS2_32.47)
1988 * From MSDN description of error codes, this function should also
1989 * check if WinSock has been initialized and the socket is a valid
1990 * socket. But why? This function only translates a host byte order
1991 * u_short into a network byte order u_short...
1993 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
1998 *lpnetshort
= htons(hostshort
);
2001 WSASetLastError(WSAEFAULT
);
2002 return SOCKET_ERROR
;
2006 /***********************************************************************
2007 * inet_addr (WINSOCK.10)
2008 * inet_addr (WS2_32.11)
2010 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
2012 if (!cp
) return INADDR_NONE
;
2013 return inet_addr(cp
);
2017 /***********************************************************************
2018 * ntohl (WINSOCK.14)
2021 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
2023 return ntohl(netlong
);
2027 /***********************************************************************
2028 * ntohs (WINSOCK.15)
2031 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
2033 return ntohs(netshort
);
2037 /***********************************************************************
2038 * inet_ntoa (WS2_32.12)
2040 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
2042 /* use "buffer for dummies" here because some applications have a
2043 * propensity to decode addresses in ws_hostent structure without
2044 * saving them first...
2046 static char dbuffer
[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
2048 char* s
= inet_ntoa(*((struct in_addr
*)&in
));
2054 SetLastError(wsaErrno());
2058 /**********************************************************************
2059 * WSAIoctl (WS2_32.50)
2062 INT WINAPI
WSAIoctl(SOCKET s
,
2063 DWORD dwIoControlCode
,
2066 LPVOID lpbOutBuffer
,
2068 LPDWORD lpcbBytesReturned
,
2069 LPWSAOVERLAPPED lpOverlapped
,
2070 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2072 TRACE("%ld, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
2073 s
, dwIoControlCode
, lpvInBuffer
, cbInBuffer
, lpbOutBuffer
,
2074 cbOutBuffer
, lpcbBytesReturned
, lpOverlapped
, lpCompletionRoutine
);
2076 switch( dwIoControlCode
)
2079 if (cbInBuffer
!= sizeof(WS_u_long
)) {
2080 WSASetLastError(WSAEFAULT
);
2081 return SOCKET_ERROR
;
2083 return WS_ioctlsocket( s
, WS_FIONBIO
, lpvInBuffer
);
2086 if (cbOutBuffer
!= sizeof(WS_u_long
)) {
2087 WSASetLastError(WSAEFAULT
);
2088 return SOCKET_ERROR
;
2090 return WS_ioctlsocket( s
, WS_FIONREAD
, lpbOutBuffer
);
2092 case WS_SIO_GET_INTERFACE_LIST
:
2094 INTERFACE_INFO
* intArray
= (INTERFACE_INFO
*)lpbOutBuffer
;
2095 DWORD size
, numInt
, apiReturn
;
2098 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
2102 WSASetLastError(WSAEFAULT
);
2103 return SOCKET_ERROR
;
2105 if (!lpcbBytesReturned
)
2107 WSASetLastError(WSAEFAULT
);
2108 return SOCKET_ERROR
;
2111 fd
= get_sock_fd( s
, 0, NULL
);
2112 if (fd
== -1) return SOCKET_ERROR
;
2114 apiReturn
= GetAdaptersInfo(NULL
, &size
);
2115 if (apiReturn
== ERROR_NO_DATA
)
2119 else if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
2121 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
2125 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
2127 PIP_ADAPTER_INFO ptr
;
2129 if (size
*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > cbOutBuffer
)
2131 WARN("Buffer too small = %u, cbOutBuffer = %u\n", size
, cbOutBuffer
);
2132 HeapFree(GetProcessHeap(),0,table
);
2133 release_sock_fd( s
, fd
);
2134 WSASetLastError(WSAEFAULT
);
2135 return SOCKET_ERROR
;
2137 for (ptr
= table
, numInt
= 0; ptr
;
2138 ptr
= ptr
->Next
, intArray
++, numInt
++)
2140 unsigned int addr
, mask
, bcast
;
2141 struct ifreq ifInfo
;
2143 /* Socket Status Flags */
2144 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
2145 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
2147 ERR("Error obtaining status flags for socket!\n");
2148 HeapFree(GetProcessHeap(),0,table
);
2149 release_sock_fd( s
, fd
);
2150 WSASetLastError(WSAEINVAL
);
2151 return SOCKET_ERROR
;
2155 /* set flags; the values of IFF_* are not the same
2156 under Linux and Windows, therefore must generate
2158 intArray
->iiFlags
= 0;
2159 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
2160 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
2161 #ifdef IFF_POINTOPOINT
2162 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
2163 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
2165 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
2166 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
2167 if (ifInfo
.ifr_flags
& IFF_UP
)
2168 intArray
->iiFlags
|= WS_IFF_UP
;
2169 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
2170 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
2173 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
2174 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
2175 bcast
= addr
| ~mask
;
2176 intArray
->iiAddress
.AddressIn
.sin_family
= AF_INET
;
2177 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
2178 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
=
2180 intArray
->iiNetmask
.AddressIn
.sin_family
= AF_INET
;
2181 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
2182 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
=
2184 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
=
2186 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
2187 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.
2193 ERR("Unable to get interface table!\n");
2194 release_sock_fd( s
, fd
);
2195 HeapFree(GetProcessHeap(),0,table
);
2196 WSASetLastError(WSAEINVAL
);
2197 return SOCKET_ERROR
;
2199 HeapFree(GetProcessHeap(),0,table
);
2203 release_sock_fd( s
, fd
);
2204 WSASetLastError(WSAEINVAL
);
2205 return SOCKET_ERROR
;
2210 ERR("Unable to get interface table!\n");
2211 release_sock_fd( s
, fd
);
2212 WSASetLastError(WSAEINVAL
);
2213 return SOCKET_ERROR
;
2215 /* Calculate the size of the array being returned */
2216 *lpcbBytesReturned
= sizeof(INTERFACE_INFO
) * numInt
;
2217 release_sock_fd( s
, fd
);
2221 case WS_SIO_ADDRESS_LIST_CHANGE
:
2222 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
2223 /* FIXME: error and return code depend on whether socket was created
2224 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
2227 case WS_SIO_ADDRESS_LIST_QUERY
:
2231 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
2233 if (!lpcbBytesReturned
)
2235 WSASetLastError(WSAEFAULT
);
2236 return SOCKET_ERROR
;
2239 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
2241 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
2244 if (!table
|| GetAdaptersInfo(table
, &size
))
2246 HeapFree(GetProcessHeap(), 0, table
);
2247 WSASetLastError(WSAEINVAL
);
2248 return SOCKET_ERROR
;
2251 for (p
= table
, num
= 0; p
; p
= p
->Next
)
2252 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
2254 need
= sizeof(SOCKET_ADDRESS_LIST
) + sizeof(SOCKET_ADDRESS
) * (num
- 1);
2255 need
+= sizeof(SOCKADDR
) * num
;
2256 *lpcbBytesReturned
= need
;
2258 if (need
> cbOutBuffer
)
2260 HeapFree(GetProcessHeap(), 0, table
);
2261 WSASetLastError(WSAEFAULT
);
2262 return SOCKET_ERROR
;
2269 SOCKET_ADDRESS_LIST
*sa_list
= (SOCKET_ADDRESS_LIST
*)lpbOutBuffer
;
2270 SOCKADDR_IN
*sockaddr
;
2272 sa
= sa_list
->Address
;
2273 sockaddr
= (SOCKADDR_IN
*)((char *)sa
+ num
* sizeof(SOCKET_ADDRESS
));
2274 sa_list
->iAddressCount
= num
;
2276 for (p
= table
, i
= 0; p
; p
= p
->Next
)
2278 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
2280 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
2281 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
2283 sockaddr
[i
].sin_family
= AF_INET
;
2284 sockaddr
[i
].sin_port
= 0;
2285 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
2290 HeapFree(GetProcessHeap(), 0, table
);
2295 WARN("unable to get IP address list\n");
2296 WSASetLastError(WSAEINVAL
);
2297 return SOCKET_ERROR
;
2301 FIXME("SIO_FLUSH: stub.\n");
2304 case WS_SIO_GET_EXTENSION_FUNCTION_POINTER
:
2305 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER %s: stub\n", debugstr_guid(lpvInBuffer
));
2306 WSASetLastError(WSAEOPNOTSUPP
);
2307 return SOCKET_ERROR
;
2310 FIXME("unsupported WS_IOCTL cmd (%08x)\n", dwIoControlCode
);
2311 WSASetLastError(WSAEOPNOTSUPP
);
2312 return SOCKET_ERROR
;
2319 /***********************************************************************
2320 * ioctlsocket (WS2_32.10)
2322 int WINAPI
WS_ioctlsocket(SOCKET s
, LONG cmd
, WS_u_long
*argp
)
2327 TRACE("socket %04lx, cmd %08x, ptr %p\n", s
, cmd
, argp
);
2336 if( _get_sock_mask(s
) )
2338 /* AsyncSelect()'ed sockets are always nonblocking */
2339 if (*argp
) return 0;
2340 SetLastError(WSAEINVAL
);
2341 return SOCKET_ERROR
;
2343 fd
= get_sock_fd( s
, 0, NULL
);
2349 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
2350 ret
= fcntl( fd
, F_SETFL
, O_NONBLOCK
);
2354 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
2355 ret
= fcntl( fd
, F_SETFL
, 0 );
2357 release_sock_fd( s
, fd
);
2359 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2361 return SOCKET_ERROR
;
2368 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
2369 SetLastError(WSAEINVAL
);
2370 return SOCKET_ERROR
;
2372 case SIOCGIFBRDADDR
:
2373 case SIOCGIFNETMASK
:
2375 /* These don't need any special handling. They are used by
2376 WsControl, and are here to suppress an unnecessary warning. */
2380 /* Netscape tries hard to use bogus ioctl 0x667e */
2381 /* FIXME: 0x667e above is ('f' << 8) | 126, and is a low word of
2382 * FIONBIO (_IOW('f', 126, u_long)), how that should be handled?
2384 WARN("\tunknown WS_IOCTL cmd (%08x)\n", cmd
);
2388 fd
= get_sock_fd( s
, 0, NULL
);
2391 if( ioctl(fd
, newcmd
, (char*)argp
) == 0 )
2393 release_sock_fd( s
, fd
);
2396 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2397 release_sock_fd( s
, fd
);
2399 return SOCKET_ERROR
;
2402 /***********************************************************************
2403 * listen (WS2_32.13)
2405 int WINAPI
WS_listen(SOCKET s
, int backlog
)
2407 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2409 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
2412 if (listen(fd
, backlog
) == 0)
2414 release_sock_fd( s
, fd
);
2415 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
2417 FD_CONNECT
|FD_WINE_CONNECTED
);
2420 SetLastError(wsaErrno());
2421 release_sock_fd( s
, fd
);
2423 return SOCKET_ERROR
;
2426 /***********************************************************************
2429 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
2431 DWORD n
, dwFlags
= flags
;
2437 if ( WSARecvFrom(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
2438 return SOCKET_ERROR
;
2443 /***********************************************************************
2444 * recvfrom (WS2_32.17)
2446 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
2447 struct WS_sockaddr
*from
, int *fromlen
)
2449 DWORD n
, dwFlags
= flags
;
2455 if ( WSARecvFrom(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
) == SOCKET_ERROR
)
2456 return SOCKET_ERROR
;
2461 /* allocate a poll array for the corresponding fd sets */
2462 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
2463 const WS_fd_set
*exceptfds
, int *count_ptr
)
2465 int i
, j
= 0, count
= 0;
2468 if (readfds
) count
+= readfds
->fd_count
;
2469 if (writefds
) count
+= writefds
->fd_count
;
2470 if (exceptfds
) count
+= exceptfds
->fd_count
;
2472 if (!count
) return NULL
;
2473 if (!(fds
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(fds
[0])))) return NULL
;
2475 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
2477 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
2478 fds
[j
].events
= POLLIN
;
2482 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
2484 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
2485 fds
[j
].events
= POLLOUT
;
2489 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
2491 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
2492 fds
[j
].events
= POLLHUP
;
2498 /* release the file descriptor obtained in fd_sets_to_poll */
2499 /* must be called with the original fd_set arrays, before calling get_poll_results */
2500 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
2501 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
2507 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
2508 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
2512 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
2513 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
2517 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
2518 if (fds
[j
].fd
!= -1)
2520 /* make sure we have a real error before releasing the fd */
2521 if (!sock_error_p( fds
[j
].fd
)) fds
[j
].revents
= 0;
2522 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
2527 /* map the poll results back into the Windows fd sets */
2528 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
2529 const struct pollfd
*fds
)
2531 int i
, j
= 0, k
, total
= 0;
2535 for (i
= k
= 0; i
< readfds
->fd_count
; i
++, j
++)
2536 if (fds
[j
].revents
) readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
2537 readfds
->fd_count
= k
;
2542 for (i
= k
= 0; i
< writefds
->fd_count
; i
++, j
++)
2543 if (fds
[j
].revents
) writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
2544 writefds
->fd_count
= k
;
2549 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
2550 if (fds
[j
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
2551 exceptfds
->fd_count
= k
;
2558 /***********************************************************************
2559 * select (WS2_32.18)
2561 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
2562 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
2563 const struct WS_timeval
* ws_timeout
)
2565 struct pollfd
*pollfds
;
2566 int count
, ret
, timeout
= -1;
2568 TRACE("read %p, write %p, excp %p timeout %p\n",
2569 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
2571 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)) && count
)
2573 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
2574 return SOCKET_ERROR
;
2577 if (ws_timeout
) timeout
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
2579 ret
= poll( pollfds
, count
, timeout
);
2580 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
2582 if (ret
== -1) SetLastError(wsaErrno());
2583 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
2584 HeapFree( GetProcessHeap(), 0, pollfds
);
2588 /* helper to send completion messages for client-only i/o operation case */
2589 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
, ULONG_PTR Information
)
2593 SERVER_START_REQ( add_fd_completion
)
2595 req
->handle
= SOCKET2HANDLE(sock
);
2596 req
->cvalue
= CompletionValue
;
2597 req
->status
= CompletionStatus
;
2598 req
->information
= Information
;
2599 status
= wine_server_call( req
);
2605 /***********************************************************************
2608 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
2614 wsabuf
.buf
= (char*) buf
;
2616 if ( WSASendTo( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
2617 return SOCKET_ERROR
;
2622 /***********************************************************************
2623 * WSASend (WS2_32.72)
2625 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
2626 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
2627 LPWSAOVERLAPPED lpOverlapped
,
2628 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2630 return WSASendTo( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
2631 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
2634 /***********************************************************************
2635 * WSASendDisconnect (WS2_32.73)
2637 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
2639 return WS_shutdown( s
, SD_SEND
);
2643 /***********************************************************************
2644 * WSASendTo (WS2_32.74)
2646 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
2647 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
2648 const struct WS_sockaddr
*to
, int tolen
,
2649 LPWSAOVERLAPPED lpOverlapped
,
2650 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2652 unsigned int i
, options
;
2654 struct iovec iovec
[WS_MSG_MAXIOVLEN
];
2655 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
2657 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
2658 s
, lpBuffers
, dwBufferCount
, dwFlags
,
2659 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
2661 if (dwBufferCount
> WS_MSG_MAXIOVLEN
)
2663 WSASetLastError( WSAEINVAL
);
2664 return SOCKET_ERROR
;
2667 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
2668 TRACE( "fd=%d, options=%x\n", fd
, options
);
2670 if ( fd
== -1 ) return SOCKET_ERROR
;
2672 if ( !lpNumberOfBytesSent
)
2678 for ( i
= 0; i
< dwBufferCount
; i
++ )
2680 iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
2681 iovec
[i
].iov_len
= lpBuffers
[i
].len
;
2686 n
= WS2_send( fd
, iovec
, dwBufferCount
, to
, tolen
, dwFlags
);
2687 if (n
!= -1 || errno
!= EINTR
) break;
2689 if (n
== -1 && errno
!= EAGAIN
)
2692 if (cvalue
) WS_AddCompletion( s
, cvalue
, err
, 0 );
2696 if ((lpOverlapped
|| lpCompletionRoutine
) &&
2697 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
2699 IO_STATUS_BLOCK
*iosb
;
2700 struct ws2_async
*wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
2707 release_sock_fd( s
, fd
);
2709 wsa
->hSocket
= SOCKET2HANDLE(s
);
2710 wsa
->addr
= (struct WS_sockaddr
*)to
;
2711 wsa
->addrlen
.val
= tolen
;
2713 wsa
->user_overlapped
= lpOverlapped
;
2714 wsa
->completion_func
= lpCompletionRoutine
;
2715 wsa
->n_iovecs
= dwBufferCount
;
2716 memcpy( wsa
->iovec
, iovec
, dwBufferCount
* sizeof(*iovec
) );
2718 iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
2721 iosb
->u
.Status
= STATUS_PENDING
;
2722 iosb
->Information
= 0;
2724 SERVER_START_REQ( register_async
)
2726 req
->handle
= wsa
->hSocket
;
2727 req
->type
= ASYNC_TYPE_WRITE
;
2728 req
->async
.callback
= WS2_async_send
;
2729 req
->async
.iosb
= iosb
;
2730 req
->async
.arg
= wsa
;
2731 req
->async
.apc
= ws2_async_apc
;
2732 req
->async
.event
= lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
;
2733 req
->async
.cvalue
= cvalue
;
2734 err
= wine_server_call( req
);
2738 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
2739 WSASetLastError( NtStatusToWSAError( err
));
2740 return SOCKET_ERROR
;
2743 iosb
->u
.Status
= STATUS_SUCCESS
;
2744 iosb
->Information
= n
;
2745 *lpNumberOfBytesSent
= n
;
2746 if (!wsa
->completion_func
)
2748 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
2749 SetEvent( lpOverlapped
->hEvent
);
2750 HeapFree( GetProcessHeap(), 0, wsa
);
2752 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
2753 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
2757 if ( _is_blocking(s
) )
2759 /* On a blocking non-overlapped stream socket,
2760 * sending blocks until the entire buffer is sent. */
2761 DWORD timeout_start
= GetTickCount();
2762 unsigned int first_buff
= 0;
2764 *lpNumberOfBytesSent
= 0;
2766 while (first_buff
< dwBufferCount
)
2769 int timeout
= GET_SNDTIMEO(fd
);
2773 *lpNumberOfBytesSent
+= n
;
2774 while (first_buff
< dwBufferCount
&& iovec
[first_buff
].iov_len
<= n
)
2775 n
-= iovec
[first_buff
++].iov_len
;
2776 if (first_buff
>= dwBufferCount
) break;
2777 iovec
[first_buff
].iov_base
= (char*)iovec
[first_buff
].iov_base
+ n
;
2778 iovec
[first_buff
].iov_len
-= n
;
2783 timeout
-= GetTickCount() - timeout_start
;
2784 if (timeout
< 0) timeout
= 0;
2788 pfd
.events
= POLLOUT
;
2790 if (!timeout
|| !poll( &pfd
, 1, timeout
))
2793 goto error
; /* msdn says a timeout in send is fatal */
2796 n
= WS2_send( fd
, iovec
+ first_buff
, dwBufferCount
- first_buff
, to
, tolen
, dwFlags
);
2797 if (n
== -1 && errno
!= EAGAIN
&& errno
!= EINTR
)
2804 else /* non-blocking */
2806 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
2809 err
= WSAEWOULDBLOCK
;
2812 *lpNumberOfBytesSent
= n
;
2815 TRACE(" -> %i bytes\n", *lpNumberOfBytesSent
);
2817 release_sock_fd( s
, fd
);
2821 release_sock_fd( s
, fd
);
2822 WARN(" -> ERROR %d\n", err
);
2823 WSASetLastError(err
);
2824 return SOCKET_ERROR
;
2827 /***********************************************************************
2828 * sendto (WS2_32.20)
2830 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
2831 const struct WS_sockaddr
*to
, int tolen
)
2837 wsabuf
.buf
= (char*) buf
;
2839 if ( WSASendTo(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
2840 return SOCKET_ERROR
;
2845 /***********************************************************************
2846 * setsockopt (WS2_32.21)
2848 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
2849 const char *optval
, int optlen
)
2853 struct linger linger
;
2854 struct timeval tval
;
2856 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
2857 s
, level
, optname
, optval
, optlen
);
2859 /* some broken apps pass the value directly instead of a pointer to it */
2860 if(IS_INTRESOURCE(optval
))
2862 SetLastError(WSAEFAULT
);
2863 return SOCKET_ERROR
;
2871 /* Some options need some conversion before they can be sent to
2872 * setsockopt. The conversions are done here, then they will fall though
2873 * to the general case. Special options that are not passed to
2874 * setsockopt follow below that.*/
2876 case WS_SO_DONTLINGER
:
2877 linger
.l_onoff
= *((const int*)optval
) ? 0: 1;
2878 linger
.l_linger
= 0;
2880 optname
= SO_LINGER
;
2881 optval
= (char*)&linger
;
2882 optlen
= sizeof(struct linger
);
2886 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
2887 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
2888 /* FIXME: what is documented behavior if SO_LINGER optval
2891 optname
= SO_LINGER
;
2892 optval
= (char*)&linger
;
2893 optlen
= sizeof(struct linger
);
2897 if (*(const int*)optval
< 2048)
2899 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
2904 /* The options listed here don't need any special handling. Thanks to
2905 * the conversion happening above, options from there will fall through
2907 case WS_SO_ACCEPTCONN
:
2908 case WS_SO_BROADCAST
:
2910 case WS_SO_KEEPALIVE
:
2911 case WS_SO_OOBINLINE
:
2912 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
2913 * however, using it the BSD way fixes bug 8513 and seems to be what
2914 * most programmers assume, anyway */
2915 case WS_SO_REUSEADDR
:
2918 convert_sockopt(&level
, &optname
);
2921 /* SO_DEBUG is a privileged operation, ignore it. */
2923 TRACE("Ignoring SO_DEBUG\n");
2926 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
2927 * socket. According to MSDN, this option is silently ignored.*/
2928 case WS_SO_DONTROUTE
:
2929 TRACE("Ignoring SO_DONTROUTE\n");
2932 /* Stops two sockets from being bound to the same port. Always happens
2933 * on unix systems, so just drop it. */
2934 case WS_SO_EXCLUSIVEADDRUSE
:
2935 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
2938 /* SO_OPENTYPE does not require a valid socket handle. */
2939 case WS_SO_OPENTYPE
:
2940 if (!optlen
|| optlen
< sizeof(int) || !optval
)
2942 SetLastError(WSAEFAULT
);
2943 return SOCKET_ERROR
;
2945 get_per_thread_data()->opentype
= *(const int *)optval
;
2946 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
2950 case WS_SO_RCVTIMEO
:
2953 case WS_SO_SNDTIMEO
:
2955 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
2956 if (optval
&& optlen
== sizeof(UINT32
)) {
2957 /* WinSock passes milliseconds instead of struct timeval */
2958 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
2959 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
2960 /* min of 500 milliseconds */
2961 if (tval
.tv_sec
== 0 && tval
.tv_usec
< 500000)
2962 tval
.tv_usec
= 500000;
2963 optlen
= sizeof(struct timeval
);
2964 optval
= (char*)&tval
;
2965 } else if (optlen
== sizeof(struct timeval
)) {
2966 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
2968 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
2971 convert_sockopt(&level
, &optname
);
2976 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
2977 SetLastError(WSAENOPROTOOPT
);
2978 return SOCKET_ERROR
;
2980 break; /* case WS_SOL_SOCKET */
2987 fd
= get_sock_fd( s
, 0, NULL
);
2988 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval
, fd
);
2990 /* We try to set the ipx type on ipx socket level. */
2992 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, optlen
) == -1)
2994 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
2995 release_sock_fd( s
, fd
);
2996 return SOCKET_ERROR
;
3001 /* Should we retrieve val using a getsockopt call and then
3002 * set the modified one? */
3003 val
.ipx_pt
= *optval
;
3004 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
3007 release_sock_fd( s
, fd
);
3010 case IPX_FILTERPTYPE
:
3011 /* Sets the receive filter packet type, at the moment we don't support it */
3012 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
3013 /* Returning 0 is better for now than returning a SOCKET_ERROR */
3017 FIXME("opt_name:%x\n", optname
);
3018 return SOCKET_ERROR
;
3020 break; /* case NSPROTO_IPX */
3023 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
3024 case WS_IPPROTO_TCP
:
3027 case WS_TCP_NODELAY
:
3028 convert_sockopt(&level
, &optname
);
3031 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
3032 return SOCKET_ERROR
;
3039 case WS_IP_ADD_MEMBERSHIP
:
3040 case WS_IP_DROP_MEMBERSHIP
:
3044 case WS_IP_MULTICAST_IF
:
3045 case WS_IP_MULTICAST_LOOP
:
3046 case WS_IP_MULTICAST_TTL
:
3050 convert_sockopt(&level
, &optname
);
3052 case WS_IP_DONTFRAGMENT
:
3053 FIXME("IP_DONTFRAGMENT is silently ignored!\n");
3056 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
3057 return SOCKET_ERROR
;
3062 FIXME("Unknown level: 0x%08x\n", level
);
3063 return SOCKET_ERROR
;
3064 } /* end switch(level) */
3066 /* avoid endianness issues if argument is a 16-bit int */
3067 if (optval
&& optlen
< sizeof(int))
3069 woptval
= *((const INT16
*) optval
);
3070 optval
= (char*) &woptval
;
3073 fd
= get_sock_fd( s
, 0, NULL
);
3074 if (fd
== -1) return SOCKET_ERROR
;
3076 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
3078 release_sock_fd( s
, fd
);
3081 TRACE("Setting socket error, %d\n", wsaErrno());
3082 SetLastError(wsaErrno());
3083 release_sock_fd( s
, fd
);
3085 return SOCKET_ERROR
;
3088 /***********************************************************************
3089 * shutdown (WS2_32.22)
3091 int WINAPI
WS_shutdown(SOCKET s
, int how
)
3093 int fd
, err
= WSAENOTSOCK
;
3094 unsigned int options
, clear_flags
= 0;
3096 fd
= get_sock_fd( s
, 0, &options
);
3097 TRACE("socket %04lx, how %i %x\n", s
, how
, options
);
3100 return SOCKET_ERROR
;
3104 case 0: /* drop receives */
3105 clear_flags
|= FD_READ
;
3107 case 1: /* drop sends */
3108 clear_flags
|= FD_WRITE
;
3110 case 2: /* drop all */
3111 clear_flags
|= FD_READ
|FD_WRITE
;
3113 clear_flags
|= FD_WINE_LISTENING
;
3116 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
3121 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
3124 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
3128 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
3129 if (!err
) err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
3132 if (err
) goto error
;
3134 else /* non-overlapped mode */
3136 if ( shutdown( fd
, how
) )
3143 release_sock_fd( s
, fd
);
3144 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
3145 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
3149 release_sock_fd( s
, fd
);
3150 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
3151 WSASetLastError( err
);
3152 return SOCKET_ERROR
;
3155 /***********************************************************************
3156 * socket (WS2_32.23)
3158 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
3160 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
3162 return WSASocketA( af
, type
, protocol
, NULL
, 0,
3163 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
3167 /***********************************************************************
3168 * gethostbyaddr (WS2_32.51)
3170 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
3172 struct WS_hostent
*retval
= NULL
;
3173 struct hostent
* host
;
3175 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3178 struct hostent hostentry
;
3181 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
3183 int res
= gethostbyaddr_r(addr
, len
, type
,
3184 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
3185 if( res
!= ERANGE
) break;
3187 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
3189 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
3191 EnterCriticalSection( &csWSgetXXXbyYYY
);
3192 host
= gethostbyaddr(addr
, len
, type
);
3193 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
3195 if( host
!= NULL
) retval
= WS_dup_he(host
);
3196 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3197 HeapFree(GetProcessHeap(),0,extrabuf
);
3199 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3201 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
3205 /***********************************************************************
3206 * gethostbyname (WS2_32.52)
3208 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
3210 struct WS_hostent
*retval
= NULL
;
3211 struct hostent
* host
;
3212 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3215 struct hostent hostentry
;
3216 int locerr
= ENOBUFS
;
3219 if( !name
|| !name
[0]) {
3221 if( gethostname( buf
, 100) == -1) {
3222 SetLastError( WSAENOBUFS
); /* appropriate ? */
3226 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3228 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
3230 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
3231 if( res
!= ERANGE
) break;
3233 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
3235 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
3237 EnterCriticalSection( &csWSgetXXXbyYYY
);
3238 host
= gethostbyname(name
);
3239 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
3241 if (host
) retval
= WS_dup_he(host
);
3242 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3243 HeapFree(GetProcessHeap(),0,extrabuf
);
3245 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3247 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
3248 strcmp(name
, "localhost") != 0)
3250 /* hostname != "localhost" but has loopback address. replace by our
3251 * special address.*/
3252 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
3254 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
3259 /***********************************************************************
3260 * getprotobyname (WS2_32.53)
3262 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
3264 struct WS_protoent
* retval
= NULL
;
3265 #ifdef HAVE_GETPROTOBYNAME
3266 struct protoent
* proto
;
3267 EnterCriticalSection( &csWSgetXXXbyYYY
);
3268 if( (proto
= getprotobyname(name
)) != NULL
)
3270 retval
= WS_dup_pe(proto
);
3273 MESSAGE("protocol %s not found; You might want to add "
3274 "this to /etc/protocols\n", debugstr_a(name
) );
3275 SetLastError(WSANO_DATA
);
3277 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3279 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
3284 /***********************************************************************
3285 * getprotobynumber (WS2_32.54)
3287 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
3289 struct WS_protoent
* retval
= NULL
;
3290 #ifdef HAVE_GETPROTOBYNUMBER
3291 struct protoent
* proto
;
3292 EnterCriticalSection( &csWSgetXXXbyYYY
);
3293 if( (proto
= getprotobynumber(number
)) != NULL
)
3295 retval
= WS_dup_pe(proto
);
3298 MESSAGE("protocol number %d not found; You might want to add "
3299 "this to /etc/protocols\n", number
);
3300 SetLastError(WSANO_DATA
);
3302 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3304 TRACE("%i ret %p\n", number
, retval
);
3309 /***********************************************************************
3310 * getservbyname (WS2_32.55)
3312 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
3314 struct WS_servent
* retval
= NULL
;
3315 struct servent
* serv
;
3317 char *proto_str
= NULL
;
3319 if (!(name_str
= strdup_lower(name
))) return NULL
;
3321 if (proto
&& *proto
)
3323 if (!(proto_str
= strdup_lower(proto
)))
3325 HeapFree( GetProcessHeap(), 0, name_str
);
3330 EnterCriticalSection( &csWSgetXXXbyYYY
);
3331 serv
= getservbyname(name_str
, proto_str
);
3334 retval
= WS_dup_se(serv
);
3336 else SetLastError(WSANO_DATA
);
3337 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3338 HeapFree( GetProcessHeap(), 0, proto_str
);
3339 HeapFree( GetProcessHeap(), 0, name_str
);
3340 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
3344 /***********************************************************************
3345 * freeaddrinfo (WS2_32.@)
3347 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
3350 struct WS_addrinfo
*next
;
3352 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
3353 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
3354 next
= res
->ai_next
;
3355 HeapFree(GetProcessHeap(),0,res
);
3360 /* helper functions for getaddrinfo()/getnameinfo() */
3361 static int convert_aiflag_w2u(int winflags
) {
3362 int i
, unixflags
= 0;
3364 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
3365 if (ws_aiflag_map
[i
][0] & winflags
) {
3366 unixflags
|= ws_aiflag_map
[i
][1];
3367 winflags
&= ~ws_aiflag_map
[i
][0];
3370 FIXME("Unhandled windows AI_xxx flags %x\n", winflags
);
3374 static int convert_niflag_w2u(int winflags
) {
3375 int i
, unixflags
= 0;
3377 for (i
=0;i
<sizeof(ws_niflag_map
)/sizeof(ws_niflag_map
[0]);i
++)
3378 if (ws_niflag_map
[i
][0] & winflags
) {
3379 unixflags
|= ws_niflag_map
[i
][1];
3380 winflags
&= ~ws_niflag_map
[i
][0];
3383 FIXME("Unhandled windows NI_xxx flags %x\n", winflags
);
3387 static int convert_aiflag_u2w(int unixflags
) {
3388 int i
, winflags
= 0;
3390 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
3391 if (ws_aiflag_map
[i
][1] & unixflags
) {
3392 winflags
|= ws_aiflag_map
[i
][0];
3393 unixflags
&= ~ws_aiflag_map
[i
][1];
3395 if (unixflags
) /* will warn usually */
3396 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags
);
3400 static int convert_eai_u2w(int unixret
) {
3403 for (i
=0;ws_eai_map
[i
][0];i
++)
3404 if (ws_eai_map
[i
][1] == unixret
)
3405 return ws_eai_map
[i
][0];
3409 /***********************************************************************
3410 * getaddrinfo (WS2_32.@)
3412 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
3414 #ifdef HAVE_GETADDRINFO
3415 struct addrinfo
*unixaires
= NULL
;
3417 struct addrinfo unixhints
, *punixhints
= NULL
;
3418 CHAR
*node
= NULL
, *serv
= NULL
;
3421 if (!(node
= strdup_lower(nodename
))) return WSA_NOT_ENOUGH_MEMORY
;
3424 if (!(serv
= strdup_lower(servname
))) {
3425 HeapFree(GetProcessHeap(), 0, node
);
3426 return WSA_NOT_ENOUGH_MEMORY
;
3431 punixhints
= &unixhints
;
3433 memset(&unixhints
, 0, sizeof(unixhints
));
3434 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
3435 if (hints
->ai_family
== 0) /* wildcard, specific to getaddrinfo() */
3436 punixhints
->ai_family
= 0;
3438 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
3439 if (hints
->ai_socktype
== 0) /* wildcard, specific to getaddrinfo() */
3440 punixhints
->ai_socktype
= 0;
3442 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
3443 if (hints
->ai_protocol
== 0) /* wildcard, specific to getaddrinfo() */
3444 punixhints
->ai_protocol
= 0;
3446 punixhints
->ai_protocol
= convert_proto_w2u(hints
->ai_protocol
);
3449 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
3450 result
= getaddrinfo(nodename
, servname
, punixhints
, &unixaires
);
3452 TRACE("%s, %s %p -> %p %d\n", nodename
, servname
, hints
, res
, result
);
3454 HeapFree(GetProcessHeap(), 0, node
);
3455 HeapFree(GetProcessHeap(), 0, serv
);
3458 struct addrinfo
*xuai
= unixaires
;
3459 struct WS_addrinfo
**xai
= res
;
3463 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
3469 *xai
= ai
;xai
= &ai
->ai_next
;
3470 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
3471 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
3472 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
3473 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
3474 if (xuai
->ai_canonname
) {
3475 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
3476 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
3477 if (!ai
->ai_canonname
)
3479 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
3481 len
= xuai
->ai_addrlen
;
3482 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
3485 ai
->ai_addrlen
= len
;
3487 int winlen
= ai
->ai_addrlen
;
3489 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
3490 ai
->ai_addrlen
= winlen
;
3494 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
3497 ai
->ai_addrlen
= len
;
3499 xuai
= xuai
->ai_next
;
3501 freeaddrinfo(unixaires
);
3503 result
= convert_eai_u2w(result
);
3509 if (*res
) WS_freeaddrinfo(*res
);
3510 if (unixaires
) freeaddrinfo(unixaires
);
3512 return WSA_NOT_ENOUGH_MEMORY
;
3514 FIXME("getaddrinfo() failed, not found during buildtime.\n");
3519 /***********************************************************************
3520 * GetAddrInfoW (WS2_32.@)
3522 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
3524 FIXME("empty stub!\n");
3528 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
3529 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
3531 #ifdef HAVE_GETNAMEINFO
3533 union generic_unix_sockaddr sa_u
;
3536 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
3537 serv
, servlen
, flags
);
3539 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
3542 WSASetLastError(WSAEFAULT
);
3543 return WSA_NOT_ENOUGH_MEMORY
;
3545 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
3546 return convert_eai_u2w(ret
);
3548 FIXME("getnameinfo() failed, not found during buildtime.\n");
3553 /***********************************************************************
3554 * getservbyport (WS2_32.56)
3556 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
3558 struct WS_servent
* retval
= NULL
;
3559 #ifdef HAVE_GETSERVBYPORT
3560 struct servent
* serv
;
3561 char *proto_str
= NULL
;
3563 if (proto
&& *proto
)
3565 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
3567 EnterCriticalSection( &csWSgetXXXbyYYY
);
3568 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
3569 retval
= WS_dup_se(serv
);
3571 else SetLastError(WSANO_DATA
);
3572 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3573 HeapFree( GetProcessHeap(), 0, proto_str
);
3575 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
3580 /***********************************************************************
3581 * gethostname (WS2_32.57)
3583 int WINAPI
WS_gethostname(char *name
, int namelen
)
3585 TRACE("name %p, len %d\n", name
, namelen
);
3587 if (gethostname(name
, namelen
) == 0)
3589 TRACE("<- '%s'\n", name
);
3592 SetLastError((errno
== EINVAL
) ? WSAEFAULT
: wsaErrno());
3593 TRACE("<- ERROR !\n");
3594 return SOCKET_ERROR
;
3598 /* ------------------------------------- Windows sockets extensions -- *
3600 * ------------------------------------------------------------------- */
3602 /***********************************************************************
3603 * WSAEnumNetworkEvents (WS2_32.36)
3605 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
3609 TRACE("%08lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
3611 SERVER_START_REQ( get_socket_event
)
3613 req
->handle
= SOCKET2HANDLE(s
);
3614 req
->service
= TRUE
;
3615 req
->c_event
= hEvent
;
3616 wine_server_set_reply( req
, lpEvent
->iErrorCode
, sizeof(lpEvent
->iErrorCode
) );
3617 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
3621 SetLastError(WSAEINVAL
);
3622 return SOCKET_ERROR
;
3625 /***********************************************************************
3626 * WSAEventSelect (WS2_32.39)
3628 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
3632 TRACE("%08lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
3634 SERVER_START_REQ( set_socket_event
)
3636 req
->handle
= SOCKET2HANDLE(s
);
3638 req
->event
= hEvent
;
3641 ret
= wine_server_call( req
);
3645 SetLastError(WSAEINVAL
);
3646 return SOCKET_ERROR
;
3649 /**********************************************************************
3650 * WSAGetOverlappedResult (WS2_32.40)
3652 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
3653 LPDWORD lpcbTransfer
, BOOL fWait
,
3658 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
3659 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
3661 if ( lpOverlapped
== NULL
)
3663 ERR( "Invalid pointer\n" );
3664 WSASetLastError(WSA_INVALID_PARAMETER
);
3668 status
= lpOverlapped
->Internal
;
3669 if (status
== STATUS_PENDING
)
3673 SetLastError( WSA_IO_INCOMPLETE
);
3677 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
3678 INFINITE
) == WAIT_FAILED
)
3680 status
= lpOverlapped
->Internal
;
3684 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
3687 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
3689 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
3694 /***********************************************************************
3695 * WSAAsyncSelect (WS2_32.101)
3697 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
3701 TRACE("%lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
3703 SERVER_START_REQ( set_socket_event
)
3705 req
->handle
= SOCKET2HANDLE(s
);
3710 ret
= wine_server_call( req
);
3714 SetLastError(WSAEINVAL
);
3715 return SOCKET_ERROR
;
3718 /***********************************************************************
3719 * WSACreateEvent (WS2_32.31)
3722 WSAEVENT WINAPI
WSACreateEvent(void)
3724 /* Create a manual-reset event, with initial state: unsignaled */
3727 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
3730 /***********************************************************************
3731 * WSACloseEvent (WS2_32.29)
3734 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
3736 TRACE ("event=%p\n", event
);
3738 return CloseHandle(event
);
3741 /***********************************************************************
3742 * WSASocketA (WS2_32.78)
3745 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
3746 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
3747 GROUP g
, DWORD dwFlags
)
3750 WSAPROTOCOL_INFOW info
;
3752 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3753 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
3755 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
3757 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
3758 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
3759 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
3763 WSASetLastError( WSAEINVAL
);
3764 return SOCKET_ERROR
;
3767 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
3770 /***********************************************************************
3771 * WSASocketW (WS2_32.79)
3774 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
3775 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
3776 GROUP g
, DWORD dwFlags
)
3781 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
3782 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
3785 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3786 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
3788 /* hack for WSADuplicateSocket */
3789 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
3790 ret
= lpProtocolInfo
->dwCatalogEntryId
;
3791 TRACE("\tgot duplicate %04lx\n", ret
);
3795 /* check and convert the socket family */
3796 af
= convert_af_w2u(af
);
3799 FIXME("Unsupported socket family %d!\n", af
);
3800 SetLastError(WSAEAFNOSUPPORT
);
3801 return INVALID_SOCKET
;
3804 /* check the socket type */
3805 type
= convert_socktype_w2u(type
);
3808 SetLastError(WSAESOCKTNOSUPPORT
);
3809 return INVALID_SOCKET
;
3812 /* check the protocol type */
3813 if ( protocol
< 0 ) /* don't support negative values */
3815 SetLastError(WSAEPROTONOSUPPORT
);
3816 return INVALID_SOCKET
;
3819 if ( af
== AF_UNSPEC
) /* did they not specify the address family? */
3823 if (type
== SOCK_STREAM
) { af
= AF_INET
; break; }
3825 if (type
== SOCK_DGRAM
) { af
= AF_INET
; break; }
3826 default: SetLastError(WSAEPROTOTYPE
); return INVALID_SOCKET
;
3829 SERVER_START_REQ( create_socket
)
3833 req
->protocol
= protocol
;
3834 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
3835 req
->attributes
= OBJ_INHERIT
;
3836 req
->flags
= dwFlags
;
3837 set_error( wine_server_call( req
) );
3838 ret
= HANDLE2SOCKET( reply
->handle
);
3843 TRACE("\tcreated %04lx\n", ret
);
3847 if (GetLastError() == WSAEACCES
) /* raw socket denied */
3849 if (type
== SOCK_RAW
)
3850 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, this"
3851 " will fail unless you have special permissions.\n");
3853 MESSAGE("WS_SOCKET: Failed to create socket, this requires"
3854 " special permissions.\n");
3855 SetLastError(WSAESOCKTNOSUPPORT
);
3858 WARN("\t\tfailed!\n");
3859 return INVALID_SOCKET
;
3862 /***********************************************************************
3863 * WSAJoinLeaf (WS2_32.58)
3866 SOCKET WINAPI
WSAJoinLeaf(
3868 const struct WS_sockaddr
*addr
,
3870 LPWSABUF lpCallerData
,
3871 LPWSABUF lpCalleeData
,
3877 return INVALID_SOCKET
;
3880 /***********************************************************************
3881 * __WSAFDIsSet (WS2_32.151)
3883 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
3885 int i
= set
->fd_count
;
3887 TRACE("(%ld,%p(%i))\n", s
, set
, i
);
3890 if (set
->fd_array
[i
] == s
) return 1;
3894 /***********************************************************************
3895 * WSAIsBlocking (WINSOCK.114)
3896 * WSAIsBlocking (WS2_32.114)
3898 BOOL WINAPI
WSAIsBlocking(void)
3900 /* By default WinSock should set all its sockets to non-blocking mode
3901 * and poll in PeekMessage loop when processing "blocking" ones. This
3902 * function is supposed to tell if the program is in this loop. Our
3903 * blocking calls are truly blocking so we always return FALSE.
3905 * Note: It is allowed to call this function without prior WSAStartup().
3912 /***********************************************************************
3913 * WSACancelBlockingCall (WINSOCK.113)
3914 * WSACancelBlockingCall (WS2_32.113)
3916 INT WINAPI
WSACancelBlockingCall(void)
3922 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
3924 FIXME("How was this called?\n");
3929 /***********************************************************************
3930 * WSASetBlockingHook (WS2_32.109)
3932 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
3934 FARPROC prev
= blocking_hook
;
3935 blocking_hook
= lpBlockFunc
;
3936 TRACE("hook %p\n", lpBlockFunc
);
3941 /***********************************************************************
3942 * WSAUnhookBlockingHook (WS2_32.110)
3944 INT WINAPI
WSAUnhookBlockingHook(void)
3946 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
3951 /* ----------------------------------- end of API stuff */
3953 /* ----------------------------------- helper functions -
3955 * TODO: Merge WS_dup_..() stuff into one function that
3956 * would operate with a generic structure containing internal
3957 * pointers (via a template of some kind).
3960 static int list_size(char** l
, int item_size
)
3965 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
3966 j
+= (i
+ 1) * sizeof(char*); }
3970 static int list_dup(char** l_src
, char** l_to
, int item_size
)
3975 for (i
= 0; l_src
[i
]; i
++) ;
3976 p
= (char *)(l_to
+ i
+ 1);
3977 for (i
= 0; l_src
[i
]; i
++)
3979 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
3980 memcpy(p
, l_src
[i
], count
);
3985 return p
- (char *)l_to
;
3990 /* duplicate hostent entry
3991 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
3992 * Ditto for protoent and servent.
3994 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
3997 struct WS_hostent
*p_to
;
3999 int size
= (sizeof(*p_he
) +
4000 strlen(p_he
->h_name
) + 1 +
4001 list_size(p_he
->h_aliases
, 0) +
4002 list_size(p_he
->h_addr_list
, p_he
->h_length
));
4004 if (!(p_to
= check_buffer_he(size
))) return NULL
;
4005 p_to
->h_addrtype
= p_he
->h_addrtype
;
4006 p_to
->h_length
= p_he
->h_length
;
4008 p
= (char *)(p_to
+ 1);
4010 strcpy(p
, p_he
->h_name
);
4013 p_to
->h_aliases
= (char **)p
;
4014 p
+= list_dup(p_he
->h_aliases
, p_to
->h_aliases
, 0);
4016 p_to
->h_addr_list
= (char **)p
;
4017 list_dup(p_he
->h_addr_list
, p_to
->h_addr_list
, p_he
->h_length
);
4021 /* ----- protoent */
4023 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
)
4026 struct WS_protoent
*p_to
;
4028 int size
= (sizeof(*p_pe
) +
4029 strlen(p_pe
->p_name
) + 1 +
4030 list_size(p_pe
->p_aliases
, 0));
4032 if (!(p_to
= check_buffer_pe(size
))) return NULL
;
4033 p_to
->p_proto
= p_pe
->p_proto
;
4035 p
= (char *)(p_to
+ 1);
4037 strcpy(p
, p_pe
->p_name
);
4040 p_to
->p_aliases
= (char **)p
;
4041 list_dup(p_pe
->p_aliases
, p_to
->p_aliases
, 0);
4047 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
4050 struct WS_servent
*p_to
;
4052 int size
= (sizeof(*p_se
) +
4053 strlen(p_se
->s_proto
) + 1 +
4054 strlen(p_se
->s_name
) + 1 +
4055 list_size(p_se
->s_aliases
, 0));
4057 if (!(p_to
= check_buffer_se(size
))) return NULL
;
4058 p_to
->s_port
= p_se
->s_port
;
4060 p
= (char *)(p_to
+ 1);
4062 strcpy(p
, p_se
->s_name
);
4066 strcpy(p
, p_se
->s_proto
);
4069 p_to
->s_aliases
= (char **)p
;
4070 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
4074 /* ----------------------------------- error handling */
4078 int loc_errno
= errno
;
4079 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
4083 case EINTR
: return WSAEINTR
;
4084 case EBADF
: return WSAEBADF
;
4086 case EACCES
: return WSAEACCES
;
4087 case EFAULT
: return WSAEFAULT
;
4088 case EINVAL
: return WSAEINVAL
;
4089 case EMFILE
: return WSAEMFILE
;
4090 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
4091 case EINPROGRESS
: return WSAEINPROGRESS
;
4092 case EALREADY
: return WSAEALREADY
;
4093 case ENOTSOCK
: return WSAENOTSOCK
;
4094 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
4095 case EMSGSIZE
: return WSAEMSGSIZE
;
4096 case EPROTOTYPE
: return WSAEPROTOTYPE
;
4097 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
4098 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
4099 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
4100 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
4101 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
4102 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
4103 case EADDRINUSE
: return WSAEADDRINUSE
;
4104 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
4105 case ENETDOWN
: return WSAENETDOWN
;
4106 case ENETUNREACH
: return WSAENETUNREACH
;
4107 case ENETRESET
: return WSAENETRESET
;
4108 case ECONNABORTED
: return WSAECONNABORTED
;
4110 case ECONNRESET
: return WSAECONNRESET
;
4111 case ENOBUFS
: return WSAENOBUFS
;
4112 case EISCONN
: return WSAEISCONN
;
4113 case ENOTCONN
: return WSAENOTCONN
;
4114 case ESHUTDOWN
: return WSAESHUTDOWN
;
4115 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
4116 case ETIMEDOUT
: return WSAETIMEDOUT
;
4117 case ECONNREFUSED
: return WSAECONNREFUSED
;
4118 case ELOOP
: return WSAELOOP
;
4119 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
4120 case EHOSTDOWN
: return WSAEHOSTDOWN
;
4121 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
4122 case ENOTEMPTY
: return WSAENOTEMPTY
;
4124 case EPROCLIM
: return WSAEPROCLIM
;
4127 case EUSERS
: return WSAEUSERS
;
4130 case EDQUOT
: return WSAEDQUOT
;
4133 case ESTALE
: return WSAESTALE
;
4136 case EREMOTE
: return WSAEREMOTE
;
4139 /* just in case we ever get here and there are no problems */
4142 WARN("Unknown errno %d!\n", loc_errno
);
4143 return WSAEOPNOTSUPP
;
4147 UINT
wsaHerrno(int loc_errno
)
4150 WARN("h_errno %d.\n", loc_errno
);
4154 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
4155 case TRY_AGAIN
: return WSATRY_AGAIN
;
4156 case NO_RECOVERY
: return WSANO_RECOVERY
;
4157 case NO_DATA
: return WSANO_DATA
;
4158 case ENOBUFS
: return WSAENOBUFS
;
4162 WARN("Unknown h_errno %d!\n", loc_errno
);
4163 return WSAEOPNOTSUPP
;
4168 /***********************************************************************
4169 * WSARecv (WS2_32.67)
4171 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4172 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
4173 LPWSAOVERLAPPED lpOverlapped
,
4174 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4176 return WSARecvFrom(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
4177 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
);
4180 /***********************************************************************
4181 * WSARecvFrom (WS2_32.69)
4183 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4184 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
4185 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
4186 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4189 unsigned int i
, options
;
4191 DWORD timeout_start
= GetTickCount();
4192 struct iovec iovec
[WS_MSG_MAXIOVLEN
];
4193 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
4195 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
4196 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
4197 (lpFromlen
? *lpFromlen
: -1),
4198 lpOverlapped
, lpCompletionRoutine
);
4200 if (dwBufferCount
> WS_MSG_MAXIOVLEN
)
4202 WSASetLastError( WSAEINVAL
);
4203 return SOCKET_ERROR
;
4206 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
4207 TRACE( "fd=%d, options=%x\n", fd
, options
);
4209 if (fd
== -1) return SOCKET_ERROR
;
4211 for (i
= 0; i
< dwBufferCount
; i
++)
4213 iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
4214 iovec
[i
].iov_len
= lpBuffers
[i
].len
;
4219 n
= WS2_recv( fd
, iovec
, dwBufferCount
, lpFrom
, lpFromlen
, lpFlags
);
4222 if (errno
== EINTR
) continue;
4223 if (errno
!= EAGAIN
)
4226 if (cvalue
) WS_AddCompletion( s
, cvalue
, err
, 0 );
4231 *lpNumberOfBytesRecvd
= n
;
4233 if ((lpOverlapped
|| lpCompletionRoutine
) &&
4234 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
4236 IO_STATUS_BLOCK
*iosb
;
4237 struct ws2_async
*wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
4244 release_sock_fd( s
, fd
);
4246 wsa
->hSocket
= SOCKET2HANDLE(s
);
4247 wsa
->flags
= *lpFlags
;
4249 wsa
->addrlen
.ptr
= lpFromlen
;
4250 wsa
->user_overlapped
= lpOverlapped
;
4251 wsa
->completion_func
= lpCompletionRoutine
;
4252 wsa
->n_iovecs
= dwBufferCount
;
4253 memcpy( wsa
->iovec
, iovec
, dwBufferCount
* sizeof(*iovec
) );
4255 iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
4259 iosb
->u
.Status
= STATUS_PENDING
;
4260 iosb
->Information
= 0;
4262 SERVER_START_REQ( register_async
)
4264 req
->handle
= wsa
->hSocket
;
4265 req
->type
= ASYNC_TYPE_READ
;
4266 req
->async
.callback
= WS2_async_recv
;
4267 req
->async
.iosb
= iosb
;
4268 req
->async
.arg
= wsa
;
4269 req
->async
.apc
= ws2_async_apc
;
4270 req
->async
.event
= lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
;
4271 req
->async
.cvalue
= cvalue
;
4272 err
= wine_server_call( req
);
4276 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
4277 WSASetLastError( NtStatusToWSAError( err
));
4278 return SOCKET_ERROR
;
4281 iosb
->u
.Status
= STATUS_SUCCESS
;
4282 iosb
->Information
= n
;
4283 if (!wsa
->completion_func
)
4285 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
4286 SetEvent( lpOverlapped
->hEvent
);
4287 HeapFree( GetProcessHeap(), 0, wsa
);
4289 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
4290 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
4291 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4297 if ( _is_blocking(s
) )
4300 int timeout
= GET_RCVTIMEO(fd
);
4303 timeout
-= GetTickCount() - timeout_start
;
4304 if (timeout
< 0) timeout
= 0;
4308 pfd
.events
= POLLIN
;
4309 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
4311 if (!timeout
|| !poll( &pfd
, 1, timeout
))
4314 /* a timeout is not fatal */
4315 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4321 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4322 err
= WSAEWOULDBLOCK
;
4327 TRACE(" -> %i bytes\n", n
);
4328 release_sock_fd( s
, fd
);
4329 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4334 release_sock_fd( s
, fd
);
4335 WARN(" -> ERROR %d\n", err
);
4336 WSASetLastError( err
);
4337 return SOCKET_ERROR
;
4340 /***********************************************************************
4341 * WSCInstallProvider (WS2_32.88)
4343 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
4344 LPCWSTR lpszProviderDllPath
,
4345 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
4346 DWORD dwNumberOfEntries
,
4349 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
4350 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
4351 dwNumberOfEntries
, lpErrno
);
4357 /***********************************************************************
4358 * WSCDeinstallProvider (WS2_32.83)
4360 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
4362 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
4368 /***********************************************************************
4369 * WSAAccept (WS2_32.26)
4371 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
4372 LPCONDITIONPROC lpfnCondition
, DWORD dwCallbackData
)
4375 int ret
= 0, size
= 0;
4376 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
4377 /* QOS SQOS, GQOS; */
4380 SOCKADDR src_addr
, dst_addr
;
4382 TRACE("Socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %d\n",
4383 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
4386 size
= sizeof(src_addr
);
4387 cs
= WS_accept(s
, &src_addr
, &size
);
4389 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
4391 CallerId
.buf
= (char *)&src_addr
;
4392 CallerId
.len
= sizeof(src_addr
);
4394 CallerData
.buf
= NULL
;
4395 CallerData
.len
= (ULONG
)NULL
;
4397 WS_getsockname(cs
, &dst_addr
, &size
);
4399 CalleeId
.buf
= (char *)&dst_addr
;
4400 CalleeId
.len
= sizeof(dst_addr
);
4403 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
4404 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
4409 if (addr
&& addrlen
)
4410 addr
= memcpy(addr
, &src_addr
, (*addrlen
> size
) ? size
: *addrlen
);
4413 SERVER_START_REQ( set_socket_deferred
)
4415 req
->handle
= SOCKET2HANDLE(s
);
4416 req
->deferred
= SOCKET2HANDLE(cs
);
4417 if ( !wine_server_call_err ( req
) )
4419 SetLastError( WSATRY_AGAIN
);
4420 WS_closesocket( cs
);
4424 return SOCKET_ERROR
;
4427 SetLastError(WSAECONNREFUSED
);
4428 return SOCKET_ERROR
;
4430 FIXME("Unknown return type from Condition function\n");
4431 SetLastError(WSAENOTSOCK
);
4432 return SOCKET_ERROR
;
4436 /***********************************************************************
4437 * WSADuplicateSocketA (WS2_32.32)
4439 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
4443 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
4444 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
4445 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
4446 /* I don't know what the real Windoze does next, this is a hack */
4447 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
4448 * the target use the global duplicate, or we could copy a reference to us to the structure
4449 * and let the target duplicate it from us, but let's do it as simple as possible */
4450 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
4451 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
4452 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
4453 0, FALSE
, DUPLICATE_SAME_ACCESS
);
4454 CloseHandle(hProcess
);
4455 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
4459 /***********************************************************************
4460 * WSADuplicateSocketW (WS2_32.33)
4462 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
4466 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
4468 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
4469 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
4470 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
4471 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
4472 0, FALSE
, DUPLICATE_SAME_ACCESS
);
4473 CloseHandle(hProcess
);
4474 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
4478 /***********************************************************************
4479 * WSAInstallServiceClassA (WS2_32.48)
4481 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
4483 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
4484 WSASetLastError(WSAEACCES
);
4485 return SOCKET_ERROR
;
4488 /***********************************************************************
4489 * WSAInstallServiceClassW (WS2_32.49)
4491 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
4493 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
4494 WSASetLastError(WSAEACCES
);
4495 return SOCKET_ERROR
;
4498 /***********************************************************************
4499 * WSARemoveServiceClass (WS2_32.70)
4501 int WINAPI
WSARemoveServiceClass(LPGUID info
)
4503 FIXME("Request to remove service %p\n",info
);
4504 WSASetLastError(WSATYPE_NOT_FOUND
);
4505 return SOCKET_ERROR
;
4508 /***********************************************************************
4509 * WSAStringToAddressA (WS2_32.80)
4511 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
4513 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
4514 LPSOCKADDR lpAddress
,
4515 LPINT lpAddressLength
)
4518 LPSTR workBuffer
=NULL
,ptrPort
;
4520 TRACE( "(%s, %x, %p, %p, %p)\n", AddressString
, AddressFamily
, lpProtocolInfo
,
4521 lpAddress
, lpAddressLength
);
4523 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
4527 WSASetLastError(WSAEINVAL
);
4528 return SOCKET_ERROR
;
4532 FIXME("ProtocolInfo not implemented.\n");
4534 workBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
4535 strlen(AddressString
) + 1);
4538 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4539 return SOCKET_ERROR
;
4542 strcpy(workBuffer
, AddressString
);
4544 switch(AddressFamily
)
4548 struct in_addr inetaddr
;
4550 /* If lpAddressLength is too small, tell caller the size we need */
4551 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
4553 *lpAddressLength
= sizeof(SOCKADDR_IN
);
4557 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
4559 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= AF_INET
;
4561 ptrPort
= strchr(workBuffer
, ':');
4564 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= (WS_u_short
)atoi(ptrPort
+1);
4569 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= 0;
4572 if(inet_aton(workBuffer
, &inetaddr
) > 0)
4574 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
4585 struct in6_addr inetaddr
;
4586 /* If lpAddressLength is too small, tell caller the size we need */
4587 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
4589 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
4593 #ifdef HAVE_INET_PTON
4594 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
4596 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_family
= WS_AF_INET6
;
4598 /* This one is a bit tricky. An IPv6 address contains colons, so the
4599 * check from IPv4 doesn't work like that. However, IPv6 addresses that
4600 * contain a port are written with braces like [fd12:3456:7890::1]:12345
4601 * so what we will do is to look for ']', check if the next char is a
4602 * colon, and if it is, parse the port as in IPv4. */
4604 ptrPort
= strchr(workBuffer
, ']');
4605 if(ptrPort
&& *(++ptrPort
) == ':')
4607 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= (WS_u_short
)atoi(ptrPort
+1);
4612 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= 0;
4615 if(inet_pton(AF_INET6
, workBuffer
, &inetaddr
) > 0)
4617 memcpy(&((LPSOCKADDR_IN6
)lpAddress
)->sin6_addr
, &inetaddr
,
4618 sizeof(struct in6_addr
));
4622 #endif /* HAVE_INET_PTON */
4628 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
4629 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
4633 HeapFree(GetProcessHeap(), 0, workBuffer
);
4636 WSASetLastError(res
);
4637 return SOCKET_ERROR
;
4640 /***********************************************************************
4641 * WSAStringToAddressW (WS2_32.81)
4643 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
4644 * If this should be the case, it would be required to map these digits
4645 * to Unicode digits (0-9) using FoldString first.
4647 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
4649 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
4650 LPSOCKADDR lpAddress
,
4651 LPINT lpAddressLength
)
4654 LPSTR workBuffer
=NULL
;
4655 WSAPROTOCOL_INFOA infoA
;
4656 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
4658 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
4659 lpAddress
, lpAddressLength
);
4661 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
4663 /* if ProtocolInfo is available - convert to ANSI variant */
4666 lpProtoInfoA
= &infoA
;
4667 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
4669 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
4670 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
4672 WSASetLastError( WSAEINVAL
);
4673 return SOCKET_ERROR
;
4679 /* Translate AddressString to ANSI code page - assumes that only
4680 standard digits 0-9 are used with this API call */
4681 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
4682 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
4686 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
4687 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
4688 lpAddress
,lpAddressLength
);
4689 HeapFree( GetProcessHeap(), 0, workBuffer
);
4693 res
= WSA_NOT_ENOUGH_MEMORY
;
4698 WSASetLastError(res
);
4699 return SOCKET_ERROR
;
4702 /***********************************************************************
4703 * WSAAddressToStringA (WS2_32.27)
4705 * See WSAAddressToStringW
4707 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
4708 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
4712 CHAR buffer
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4715 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
4717 if (!sockaddr
|| len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
4718 if (!string
|| !lenstr
) return SOCKET_ERROR
;
4720 /* sin_family is guaranteed to be the first u_short */
4721 if (((SOCKADDR_IN
*)sockaddr
)->sin_family
!= AF_INET
) return SOCKET_ERROR
;
4723 sprintf( buffer
, "%u.%u.%u.%u:%u",
4724 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
4725 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
4726 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
4727 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
4728 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
4730 p
= strchr( buffer
, ':' );
4731 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
4733 size
= strlen( buffer
);
4738 WSASetLastError(WSAEFAULT
);
4739 return SOCKET_ERROR
;
4742 strcpy( string
, buffer
);
4746 /***********************************************************************
4747 * WSAAddressToStringW (WS2_32.28)
4749 * Convert a sockaddr address into a readable address string.
4752 * sockaddr [I] Pointer to a sockaddr structure.
4753 * len [I] Size of the sockaddr structure.
4754 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
4755 * string [I/O] Pointer to a buffer to receive the address string.
4756 * lenstr [I/O] Size of the receive buffer in WCHARs.
4760 * Failure: SOCKET_ERROR
4763 * The 'info' parameter is ignored.
4766 * Only supports AF_INET addresses.
4768 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
4769 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
4773 WCHAR buffer
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4774 static const WCHAR format
[] = { '%','u','.','%','u','.','%','u','.','%','u',':','%','u',0 };
4777 TRACE( "(%p, %x, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
4779 if (!sockaddr
|| len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
4780 if (!string
|| !lenstr
) return SOCKET_ERROR
;
4782 /* sin_family is guaranteed to be the first u_short */
4783 if (((SOCKADDR_IN
*)sockaddr
)->sin_family
!= AF_INET
) return SOCKET_ERROR
;
4785 sprintfW( buffer
, format
,
4786 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
4787 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
4788 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
4789 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
4790 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
4792 p
= strchrW( buffer
, ':' );
4793 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
4795 size
= lstrlenW( buffer
);
4800 return SOCKET_ERROR
;
4803 lstrcpyW( string
, buffer
);
4807 /***********************************************************************
4808 * WSAEnumNameSpaceProvidersA (WS2_32.34)
4810 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
4812 FIXME( "(%p %p) Stub!\n", len
, buffer
);
4816 /***********************************************************************
4817 * WSAEnumNameSpaceProvidersW (WS2_32.35)
4819 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
4821 FIXME( "(%p %p) Stub!\n", len
, buffer
);
4825 /***********************************************************************
4826 * WSAGetQOSByName (WS2_32.41)
4828 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
4830 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
4834 /***********************************************************************
4835 * WSAGetServiceClassInfoA (WS2_32.42)
4837 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
4838 LPWSASERVICECLASSINFOA info
)
4840 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
4842 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4843 return SOCKET_ERROR
;
4846 /***********************************************************************
4847 * WSAGetServiceClassInfoW (WS2_32.43)
4849 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
4850 LPWSASERVICECLASSINFOW info
)
4852 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
4854 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4855 return SOCKET_ERROR
;
4858 /***********************************************************************
4859 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
4861 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
4863 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
4864 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4865 return SOCKET_ERROR
;
4868 /***********************************************************************
4869 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
4871 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
4873 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
4874 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4875 return SOCKET_ERROR
;
4878 /***********************************************************************
4879 * WSALookupServiceBeginA (WS2_32.59)
4881 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
4882 DWORD dwControlFlags
,
4885 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
4887 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4888 return SOCKET_ERROR
;
4891 /***********************************************************************
4892 * WSALookupServiceBeginW (WS2_32.60)
4894 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
4895 DWORD dwControlFlags
,
4898 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
4900 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4901 return SOCKET_ERROR
;
4904 /***********************************************************************
4905 * WSALookupServiceBeginW (WS2_32.61)
4907 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
4909 FIXME("(%p) Stub!\n", lookup
);
4913 /***********************************************************************
4914 * WSALookupServiceNextA (WS2_32.62)
4916 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
4918 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
4922 /***********************************************************************
4923 * WSALookupServiceNextW (WS2_32.63)
4925 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
4927 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
4931 /***********************************************************************
4932 * WSANtohl (WS2_32.64)
4934 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
4936 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
4938 if (!lphostlong
) return WSAEFAULT
;
4940 *lphostlong
= ntohl( netlong
);
4944 /***********************************************************************
4945 * WSANtohs (WS2_32.65)
4947 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
4949 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
4951 if (!lphostshort
) return WSAEFAULT
;
4953 *lphostshort
= ntohs( netshort
);
4957 /***********************************************************************
4958 * WSAProviderConfigChange (WS2_32.66)
4960 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
4961 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
4963 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
4964 return SOCKET_ERROR
;
4967 /***********************************************************************
4968 * WSARecvDisconnect (WS2_32.68)
4970 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
4972 TRACE( "(0x%04lx %p)\n", s
, disconnectdata
);
4974 return WS_shutdown( s
, 0 );
4977 /***********************************************************************
4978 * WSASetServiceA (WS2_32.76)
4980 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
4982 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
4986 /***********************************************************************
4987 * WSASetServiceW (WS2_32.77)
4989 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
4991 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
4995 /***********************************************************************
4996 * WSCEnableNSProvider (WS2_32.84)
4998 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
5000 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
5004 /***********************************************************************
5005 * WSCGetProviderPath (WS2_32.86)
5007 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
5009 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
5011 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
5013 *errcode
= WSAEINVAL
;
5014 return SOCKET_ERROR
;
5017 /***********************************************************************
5018 * WSCInstallNameSpace (WS2_32.87)
5020 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
5021 DWORD version
, LPGUID provider
)
5023 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
5024 namespace, version
, debugstr_guid(provider
) );
5028 /***********************************************************************
5029 * WSCUnInstallNameSpace (WS2_32.89)
5031 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
5033 FIXME("(%p) Stub!\n", lpProviderId
);
5037 /***********************************************************************
5038 * WSCWriteProviderOrder (WS2_32.91)
5040 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
5042 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);