2 * based on Windows Sockets 1.1 specs
3 * (ftp.microsoft.com:/Advsys/winsock/spec11/WINSOCK.TXT)
5 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
6 * Copyright (C) 2005 Marcus Meissner
7 * Copyright (C) 2006 Kai Blin
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * NOTE: If you make any changes to fix a particular app, make sure
24 * they don't break something else like Netscape or telnet and ftp
25 * clients and servers (www.winsite.com got a lot of those).
29 #include "wine/port.h"
34 #include <sys/types.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 # include <sys/ioctl.h>
41 #ifdef HAVE_SYS_FILIO_H
42 # include <sys/filio.h>
44 #ifdef HAVE_SYS_SOCKIO_H
45 # include <sys/sockio.h>
49 # include <sys/so_ioctl.h>
52 #ifdef HAVE_SYS_PARAM_H
53 # include <sys/param.h>
59 #ifdef HAVE_SYS_WAIT_H
60 # include <sys/wait.h>
65 #ifdef HAVE_SYS_SOCKET_H
66 #include <sys/socket.h>
68 #ifdef HAVE_NETINET_IN_H
69 # include <netinet/in.h>
71 #ifdef HAVE_NETINET_TCP_H
72 # include <netinet/tcp.h>
74 #ifdef HAVE_ARPA_INET_H
75 # include <arpa/inet.h>
80 #ifdef HAVE_SYS_ERRNO_H
81 #include <sys/errno.h>
90 #ifdef HAVE_ARPA_NAMESER_H
91 # include <arpa/nameser.h>
100 #ifdef HAVE_NETIPX_IPX_H
101 # include <netipx/ipx.h>
103 #elif defined(HAVE_LINUX_IPX_H)
104 # ifdef HAVE_ASM_TYPES_H
105 # include <asm/types.h>
107 # include <linux/ipx.h>
114 #ifdef HAVE_SYS_POLL_H
115 # include <sys/poll.h>
117 #ifdef HAVE_SYS_TIME_H
118 # include <sys/time.h>
121 #define NONAMELESSUNION
122 #define NONAMELESSSTRUCT
123 #include "ntstatus.h"
124 #define WIN32_NO_STATUS
129 #include "winerror.h"
131 #include "winsock2.h"
133 #include "ws2tcpip.h"
137 #include "iphlpapi.h"
138 #include "wine/server.h"
139 #include "wine/debug.h"
140 #include "wine/unicode.h"
143 # include "wsnwlink.h"
147 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
148 # define sipx_network sipx_addr.x_net
149 # define sipx_node sipx_addr.x_host.c_host
150 #endif /* __FreeBSD__ */
153 #define INADDR_NONE ~0UL
156 WINE_DEFAULT_DEBUG_CHANNEL(winsock);
158 /* critical section to protect some non-rentrant net function */
159 extern CRITICAL_SECTION csWSgetXXXbyYYY;
161 union generic_unix_sockaddr
163 struct sockaddr addr;
164 char data[128]; /* should be big enough for all families */
167 static inline const char *debugstr_sockaddr( const struct WS_sockaddr *a )
169 if (!a) return "(nil)";
170 return wine_dbg_sprintf("{ family %d, address %s, port %d }",
171 ((const struct sockaddr_in *)a)->sin_family,
172 inet_ntoa(((const struct sockaddr_in *)a)->sin_addr),
173 ntohs(((const struct sockaddr_in *)a)->sin_port));
176 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
177 #define SOCKET2HANDLE(s) ((HANDLE)(s))
178 #define HANDLE2SOCKET(h) ((SOCKET)(h))
180 /****************************************************************
181 * Async IO declarations
182 ****************************************************************/
184 typedef struct ws2_async
188 LPWSAOVERLAPPED user_overlapped;
189 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func;
190 IO_STATUS_BLOCK local_iosb;
191 struct iovec iovec[WS_MSG_MAXIOVLEN];
193 struct WS_sockaddr *addr;
196 int val; /* for send operations */
197 int *ptr; /* for recv operations */
202 /****************************************************************/
204 /* ----------------------------------- internal data */
206 /* ws_... struct conversion flags */
208 typedef struct /* WSAAsyncSelect() control struct */
210 HANDLE service, event, sock;
216 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
217 #define WS_MAX_UDP_DATAGRAM 1024
218 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x );
220 /* hostent's, servent's and protent's are stored in one buffer per thread,
221 * as documented on MSDN for the functions that return any of the buffers */
222 struct per_thread_data
225 struct WS_hostent *he_buffer;
226 struct WS_servent *se_buffer;
227 struct WS_protoent *pe_buffer;
233 static INT num_startup; /* reference counter */
234 static FARPROC blocking_hook = (FARPROC)WSA_DefaultBlockingHook;
236 /* function prototypes */
237 static struct WS_hostent *WS_dup_he(const struct hostent* p_he);
238 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe);
239 static struct WS_servent *WS_dup_se(const struct servent* p_se);
241 int WSAIOCTL_GetInterfaceCount(void);
242 int WSAIOCTL_GetInterfaceName(int intNumber, char *intName);
245 UINT wsaHerrno(int errnr);
247 #define MAP_OPTION(opt) { WS_##opt, opt }
249 static const int ws_sock_map[][2] =
251 MAP_OPTION( SO_DEBUG ),
252 MAP_OPTION( SO_ACCEPTCONN ),
253 MAP_OPTION( SO_REUSEADDR ),
254 MAP_OPTION( SO_KEEPALIVE ),
255 MAP_OPTION( SO_DONTROUTE ),
256 MAP_OPTION( SO_BROADCAST ),
257 MAP_OPTION( SO_LINGER ),
258 MAP_OPTION( SO_OOBINLINE ),
259 MAP_OPTION( SO_SNDBUF ),
260 MAP_OPTION( SO_RCVBUF ),
261 MAP_OPTION( SO_ERROR ),
262 MAP_OPTION( SO_TYPE ),
264 MAP_OPTION( SO_RCVTIMEO ),
267 MAP_OPTION( SO_SNDTIMEO ),
271 static const int ws_tcp_map[][2] =
274 MAP_OPTION( TCP_NODELAY ),
278 static const int ws_ip_map[][2] =
280 MAP_OPTION( IP_MULTICAST_IF ),
281 MAP_OPTION( IP_MULTICAST_TTL ),
282 MAP_OPTION( IP_MULTICAST_LOOP ),
283 MAP_OPTION( IP_ADD_MEMBERSHIP ),
284 MAP_OPTION( IP_DROP_MEMBERSHIP ),
285 MAP_OPTION( IP_OPTIONS ),
287 MAP_OPTION( IP_HDRINCL ),
289 MAP_OPTION( IP_TOS ),
290 MAP_OPTION( IP_TTL ),
293 static const int ws_af_map[][2] =
295 MAP_OPTION( AF_UNSPEC ),
296 MAP_OPTION( AF_INET ),
297 MAP_OPTION( AF_INET6 ),
299 MAP_OPTION( AF_IPX ),
303 static const int ws_socktype_map[][2] =
305 MAP_OPTION( SOCK_DGRAM ),
306 MAP_OPTION( SOCK_STREAM ),
307 MAP_OPTION( SOCK_RAW ),
310 static const int ws_proto_map[][2] =
312 MAP_OPTION( IPPROTO_IP ),
313 MAP_OPTION( IPPROTO_TCP ),
314 MAP_OPTION( IPPROTO_UDP ),
315 MAP_OPTION( IPPROTO_ICMP ),
316 MAP_OPTION( IPPROTO_IGMP ),
317 MAP_OPTION( IPPROTO_RAW ),
320 static const int ws_aiflag_map[][2] =
322 MAP_OPTION( AI_PASSIVE ),
323 MAP_OPTION( AI_CANONNAME ),
324 MAP_OPTION( AI_NUMERICHOST ),
325 /* Linux/UNIX knows a lot more. But Windows only
326 * has 3 as far as I could see. -Marcus
330 static const int ws_eai_map[][2] =
332 MAP_OPTION( EAI_AGAIN ),
333 MAP_OPTION( EAI_BADFLAGS ),
334 MAP_OPTION( EAI_FAIL ),
335 MAP_OPTION( EAI_FAMILY ),
336 MAP_OPTION( EAI_MEMORY ),
337 /* Note: EAI_NODATA is deprecated, but still
338 * used by Windows and Linux... We map the newer
339 * EAI_NONAME to EAI_NODATA for now until Windows
343 MAP_OPTION( EAI_NODATA ),
346 { WS_EAI_NODATA, EAI_NONAME },
349 MAP_OPTION( EAI_SERVICE ),
350 MAP_OPTION( EAI_SOCKTYPE ),
354 static inline DWORD NtStatusToWSAError( const DWORD status )
356 /* We only need to cover the status codes set by server async request handling */
360 case STATUS_SUCCESS: wserr = 0; break;
361 case STATUS_PENDING: wserr = WSA_IO_PENDING; break;
362 case STATUS_OBJECT_TYPE_MISMATCH: wserr = WSAENOTSOCK; break;
363 case STATUS_INVALID_HANDLE: wserr = WSAEBADF; break;
364 case STATUS_INVALID_PARAMETER: wserr = WSAEINVAL; break;
365 case STATUS_PIPE_DISCONNECTED: wserr = WSAESHUTDOWN; break;
366 case STATUS_CANCELLED: wserr = WSA_OPERATION_ABORTED; break;
367 case STATUS_TIMEOUT: wserr = WSAETIMEDOUT; break;
368 case STATUS_NO_MEMORY: wserr = WSAEFAULT; break;
370 if ( status >= WSABASEERR && status <= WSABASEERR+1004 )
371 /* It is not an NT status code but a winsock error */
375 wserr = RtlNtStatusToDosError( status );
376 FIXME( "Status code %08x converted to DOS error code %x\n", status, wserr );
382 /* set last error code from NT status without mapping WSA errors */
383 static inline unsigned int set_error( unsigned int err )
387 err = NtStatusToWSAError( err );
393 static inline int get_sock_fd( SOCKET s, DWORD access, unsigned int *options )
396 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s), access, &fd, options ) ))
401 static inline void release_sock_fd( SOCKET s, int fd )
403 wine_server_release_fd( SOCKET2HANDLE(s), fd );
406 static void _enable_event( HANDLE s, unsigned int event,
407 unsigned int sstate, unsigned int cstate )
409 SERVER_START_REQ( enable_socket_event )
413 req->sstate = sstate;
414 req->cstate = cstate;
415 wine_server_call( req );
420 static int _is_blocking(SOCKET s)
423 SERVER_START_REQ( get_socket_event )
425 req->handle = SOCKET2HANDLE(s);
426 req->service = FALSE;
428 wine_server_call( req );
429 ret = (reply->state & FD_WINE_NONBLOCKING) == 0;
435 static unsigned int _get_sock_mask(SOCKET s)
438 SERVER_START_REQ( get_socket_event )
440 req->handle = SOCKET2HANDLE(s);
441 req->service = FALSE;
443 wine_server_call( req );
450 static void _sync_sock_state(SOCKET s)
452 /* do a dummy wineserver request in order to let
453 the wineserver run through its select loop once */
454 (void)_is_blocking(s);
457 static int _get_sock_error(SOCKET s, unsigned int bit)
459 int events[FD_MAX_EVENTS];
461 SERVER_START_REQ( get_socket_event )
463 req->handle = SOCKET2HANDLE(s);
464 req->service = FALSE;
466 wine_server_set_reply( req, events, sizeof(events) );
467 wine_server_call( req );
473 static struct per_thread_data *get_per_thread_data(void)
475 struct per_thread_data * ptb = NtCurrentTeb()->WinSockData;
476 /* lazy initialization */
479 ptb = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ptb) );
480 NtCurrentTeb()->WinSockData = ptb;
485 static void free_per_thread_data(void)
487 struct per_thread_data * ptb = NtCurrentTeb()->WinSockData;
491 /* delete scratch buffers */
492 HeapFree( GetProcessHeap(), 0, ptb->he_buffer );
493 HeapFree( GetProcessHeap(), 0, ptb->se_buffer );
494 HeapFree( GetProcessHeap(), 0, ptb->pe_buffer );
495 ptb->he_buffer = NULL;
496 ptb->se_buffer = NULL;
497 ptb->pe_buffer = NULL;
499 HeapFree( GetProcessHeap(), 0, ptb );
500 NtCurrentTeb()->WinSockData = NULL;
503 /***********************************************************************
504 * DllMain (WS2_32.init)
506 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
508 TRACE("%p 0x%x %p\n", hInstDLL, fdwReason, fImpLoad);
510 case DLL_PROCESS_ATTACH:
512 case DLL_PROCESS_DETACH:
513 free_per_thread_data();
516 case DLL_THREAD_DETACH:
517 free_per_thread_data();
523 /***********************************************************************
526 * Converts socket flags from Windows format.
527 * Return 1 if converted, 0 if not (error).
529 static int convert_sockopt(INT *level, INT *optname)
536 for(i=0; i<sizeof(ws_sock_map)/sizeof(ws_sock_map[0]); i++) {
537 if( ws_sock_map[i][0] == *optname )
539 *optname = ws_sock_map[i][1];
543 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname);
546 *level = IPPROTO_TCP;
547 for(i=0; i<sizeof(ws_tcp_map)/sizeof(ws_tcp_map[0]); i++) {
548 if ( ws_tcp_map[i][0] == *optname )
550 *optname = ws_tcp_map[i][1];
554 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname);
558 for(i=0; i<sizeof(ws_ip_map)/sizeof(ws_ip_map[0]); i++) {
559 if (ws_ip_map[i][0] == *optname )
561 *optname = ws_ip_map[i][1];
565 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname);
567 default: FIXME("Unimplemented or unknown socket level\n");
572 static inline BOOL is_timeout_option( int optname )
575 if (optname == SO_RCVTIMEO) return TRUE;
578 if (optname == SO_SNDTIMEO) return TRUE;
583 /* ----------------------------------- Per-thread info (or per-process?) */
585 static char *strdup_lower(const char *str)
588 char *ret = HeapAlloc( GetProcessHeap(), 0, strlen(str) + 1 );
592 for (i = 0; str[i]; i++) ret[i] = tolower(str[i]);
595 else SetLastError(WSAENOBUFS);
599 static inline int sock_error_p(int s)
601 unsigned int optval, optlen;
603 optlen = sizeof(optval);
604 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
605 if (optval) WARN("\t[%i] error: %d\n", s, optval);
609 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
610 * from an fd and return the value converted to milli seconds
611 * or -1 if there is an infinite time out */
612 static inline int get_rcvsnd_timeo( int fd, int optname)
615 unsigned int len = sizeof(tv);
616 int ret = getsockopt(fd, SOL_SOCKET, optname, &tv, &len);
618 ret = tv.tv_sec * 1000 + tv.tv_usec / 1000;
619 if( ret <= 0 ) /* tv == {0,0} means infinite time out */
624 /* macro wrappers for portability */
626 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
628 #define GET_RCVTIMEO(fd) (-1)
632 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
634 #define GET_SNDTIMEO(fd) (-1)
637 /* utility: given an fd, will block until one of the events occurs */
638 static inline int do_block( int fd, int events, int timeout )
646 while ((ret = poll(&pfd, 1, timeout)) < 0)
657 convert_af_w2u(int windowsaf) {
660 for (i=0;i<sizeof(ws_af_map)/sizeof(ws_af_map[0]);i++)
661 if (ws_af_map[i][0] == windowsaf)
662 return ws_af_map[i][1];
663 FIXME("unhandled Windows address family %d\n", windowsaf);
668 convert_af_u2w(int unixaf) {
671 for (i=0;i<sizeof(ws_af_map)/sizeof(ws_af_map[0]);i++)
672 if (ws_af_map[i][1] == unixaf)
673 return ws_af_map[i][0];
674 FIXME("unhandled UNIX address family %d\n", unixaf);
679 convert_proto_w2u(int windowsproto) {
682 for (i=0;i<sizeof(ws_proto_map)/sizeof(ws_proto_map[0]);i++)
683 if (ws_proto_map[i][0] == windowsproto)
684 return ws_proto_map[i][1];
685 FIXME("unhandled Windows socket protocol %d\n", windowsproto);
690 convert_proto_u2w(int unixproto) {
693 for (i=0;i<sizeof(ws_proto_map)/sizeof(ws_proto_map[0]);i++)
694 if (ws_proto_map[i][1] == unixproto)
695 return ws_proto_map[i][0];
696 FIXME("unhandled UNIX socket protocol %d\n", unixproto);
701 convert_socktype_w2u(int windowssocktype) {
704 for (i=0;i<sizeof(ws_socktype_map)/sizeof(ws_socktype_map[0]);i++)
705 if (ws_socktype_map[i][0] == windowssocktype)
706 return ws_socktype_map[i][1];
707 FIXME("unhandled Windows socket type %d\n", windowssocktype);
712 convert_socktype_u2w(int unixsocktype) {
715 for (i=0;i<sizeof(ws_socktype_map)/sizeof(ws_socktype_map[0]);i++)
716 if (ws_socktype_map[i][1] == unixsocktype)
717 return ws_socktype_map[i][0];
718 FIXME("unhandled UNIX socket type %d\n", unixsocktype);
722 /* ----------------------------------- API -----
724 * Init / cleanup / error checking.
727 /***********************************************************************
728 * WSAStartup (WS2_32.115)
730 int WINAPI WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData)
732 TRACE("verReq=%x\n", wVersionRequested);
734 if (LOBYTE(wVersionRequested) < 1)
735 return WSAVERNOTSUPPORTED;
737 if (!lpWSAData) return WSAEINVAL;
741 /* that's the whole of the negotiation for now */
742 lpWSAData->wVersion = wVersionRequested;
743 /* return winsock information */
744 lpWSAData->wHighVersion = 0x0202;
745 strcpy(lpWSAData->szDescription, "WinSock 2.0" );
746 strcpy(lpWSAData->szSystemStatus, "Running" );
747 lpWSAData->iMaxSockets = WS_MAX_SOCKETS_PER_PROCESS;
748 lpWSAData->iMaxUdpDg = WS_MAX_UDP_DATAGRAM;
749 /* don't do anything with lpWSAData->lpVendorInfo */
750 /* (some apps don't allocate the space for this field) */
752 TRACE("succeeded\n");
757 /***********************************************************************
758 * WSACleanup (WS2_32.116)
760 INT WINAPI WSACleanup(void)
766 SetLastError(WSANOTINITIALISED);
771 /***********************************************************************
772 * WSAGetLastError (WINSOCK.111)
773 * WSAGetLastError (WS2_32.111)
775 INT WINAPI WSAGetLastError(void)
777 return GetLastError();
780 /***********************************************************************
781 * WSASetLastError (WS2_32.112)
783 void WINAPI WSASetLastError(INT iError) {
784 SetLastError(iError);
787 static struct WS_hostent *check_buffer_he(int size)
789 struct per_thread_data * ptb = get_per_thread_data();
792 if (ptb->he_len >= size ) return ptb->he_buffer;
793 HeapFree( GetProcessHeap(), 0, ptb->he_buffer );
795 ptb->he_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->he_len = size) );
796 if (!ptb->he_buffer) SetLastError(WSAENOBUFS);
797 return ptb->he_buffer;
800 static struct WS_servent *check_buffer_se(int size)
802 struct per_thread_data * ptb = get_per_thread_data();
805 if (ptb->se_len >= size ) return ptb->se_buffer;
806 HeapFree( GetProcessHeap(), 0, ptb->se_buffer );
808 ptb->se_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->se_len = size) );
809 if (!ptb->se_buffer) SetLastError(WSAENOBUFS);
810 return ptb->se_buffer;
813 static struct WS_protoent *check_buffer_pe(int size)
815 struct per_thread_data * ptb = get_per_thread_data();
818 if (ptb->pe_len >= size ) return ptb->pe_buffer;
819 HeapFree( GetProcessHeap(), 0, ptb->pe_buffer );
821 ptb->pe_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->pe_len = size) );
822 if (!ptb->pe_buffer) SetLastError(WSAENOBUFS);
823 return ptb->pe_buffer;
826 /* ----------------------------------- i/o APIs */
829 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf)== WS_AF_IPX || (pf) == WS_AF_INET6)
831 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf) == WS_AF_INET6)
835 /**********************************************************************/
837 /* Returns the length of the converted address if successful, 0 if it was too small to
840 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr* wsaddr, int wsaddrlen,
841 union generic_unix_sockaddr *uaddr)
843 unsigned int uaddrlen = 0;
845 switch (wsaddr->sa_family)
850 const struct WS_sockaddr_ipx* wsipx=(const struct WS_sockaddr_ipx*)wsaddr;
851 struct sockaddr_ipx* uipx = (struct sockaddr_ipx *)uaddr;
853 if (wsaddrlen<sizeof(struct WS_sockaddr_ipx))
856 uaddrlen = sizeof(struct sockaddr_ipx);
857 memset( uaddr, 0, uaddrlen );
858 uipx->sipx_family=AF_IPX;
859 uipx->sipx_port=wsipx->sa_socket;
860 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
863 memcpy(&uipx->sipx_network,wsipx->sa_netnum,sizeof(uipx->sipx_network)+sizeof(uipx->sipx_node));
864 #ifdef IPX_FRAME_NONE
865 uipx->sipx_type=IPX_FRAME_NONE;
871 struct sockaddr_in6* uin6 = (struct sockaddr_in6 *)uaddr;
872 const struct WS_sockaddr_in6* win6 = (const struct WS_sockaddr_in6*)wsaddr;
874 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
875 * scope_id, one without. Check:
876 * http://msdn.microsoft.com/library/en-us/winsock/winsock/sockaddr_2.asp
878 if (wsaddrlen >= sizeof(struct WS_sockaddr_in6_old)) {
879 uaddrlen = sizeof(struct sockaddr_in6);
880 memset( uaddr, 0, uaddrlen );
881 uin6->sin6_family = AF_INET6;
882 uin6->sin6_port = win6->sin6_port;
883 uin6->sin6_flowinfo = win6->sin6_flowinfo;
884 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
885 if (wsaddrlen >= sizeof(struct WS_sockaddr_in6)) uin6->sin6_scope_id = win6->sin6_scope_id;
887 memcpy(&uin6->sin6_addr,&win6->sin6_addr,16); /* 16 bytes = 128 address bits */
890 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen);
894 struct sockaddr_in* uin = (struct sockaddr_in *)uaddr;
895 const struct WS_sockaddr_in* win = (const struct WS_sockaddr_in*)wsaddr;
897 if (wsaddrlen<sizeof(struct WS_sockaddr_in))
899 uaddrlen = sizeof(struct sockaddr_in);
900 memset( uaddr, 0, uaddrlen );
901 uin->sin_family = AF_INET;
902 uin->sin_port = win->sin_port;
903 memcpy(&uin->sin_addr,&win->sin_addr,4); /* 4 bytes = 32 address bits */
907 /* Try to determine the needed space by the passed windows sockaddr space */
909 default: /* likely a ipv4 address */
910 case sizeof(struct WS_sockaddr_in):
911 uaddrlen = sizeof(struct sockaddr_in);
914 case sizeof(struct WS_sockaddr_ipx):
915 uaddrlen = sizeof(struct sockaddr_ipx);
918 case sizeof(struct WS_sockaddr_in6):
919 case sizeof(struct WS_sockaddr_in6_old):
920 uaddrlen = sizeof(struct sockaddr_in6);
923 memset( uaddr, 0, uaddrlen );
927 FIXME("Unknown address family %d, return NULL.\n", wsaddr->sa_family);
933 static BOOL is_sockaddr_bound(const struct sockaddr *uaddr, int uaddrlen)
935 switch (uaddr->sa_family)
939 FIXME("don't know how to tell if IPX socket is bound, assuming it is!\n");
944 static const struct sockaddr_in6 emptyAddr;
945 const struct sockaddr_in6 *in6 = (const struct sockaddr_in6*) uaddr;
946 return in6->sin6_port || memcmp(&in6->sin6_addr, &emptyAddr.sin6_addr, sizeof(struct in6_addr));
950 static const struct sockaddr_in emptyAddr;
951 const struct sockaddr_in *in = (const struct sockaddr_in*) uaddr;
952 return in->sin_port || memcmp(&in->sin_addr, &emptyAddr.sin_addr, sizeof(struct in_addr));
957 FIXME("unknown address family %d\n", uaddr->sa_family);
962 /* Returns 0 if successful, -1 if the buffer is too small */
963 static int ws_sockaddr_u2ws(const struct sockaddr* uaddr, struct WS_sockaddr* wsaddr, int* wsaddrlen)
967 switch(uaddr->sa_family)
972 const struct sockaddr_ipx* uipx=(const struct sockaddr_ipx*)uaddr;
973 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
976 switch (*wsaddrlen) /* how much can we copy? */
980 *wsaddrlen = sizeof(*wsipx);
981 wsipx->sa_socket=uipx->sipx_port;
985 memcpy(wsipx->sa_nodenum,uipx->sipx_node,sizeof(wsipx->sa_nodenum));
993 memcpy(wsipx->sa_netnum,&uipx->sipx_network,sizeof(wsipx->sa_netnum));
999 wsipx->sa_family=WS_AF_IPX;
1010 const struct sockaddr_in6* uin6 = (const struct sockaddr_in6*)uaddr;
1011 struct WS_sockaddr_in6_old* win6old = (struct WS_sockaddr_in6_old*)wsaddr;
1013 if (*wsaddrlen < sizeof(struct WS_sockaddr_in6_old))
1015 win6old->sin6_family = WS_AF_INET6;
1016 win6old->sin6_port = uin6->sin6_port;
1017 win6old->sin6_flowinfo = uin6->sin6_flowinfo;
1018 memcpy(&win6old->sin6_addr,&uin6->sin6_addr,16); /* 16 bytes = 128 address bits */
1019 *wsaddrlen = sizeof(struct WS_sockaddr_in6_old);
1020 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1021 if (*wsaddrlen >= sizeof(struct WS_sockaddr_in6)) {
1022 struct WS_sockaddr_in6* win6 = (struct WS_sockaddr_in6*)wsaddr;
1023 win6->sin6_scope_id = uin6->sin6_scope_id;
1024 *wsaddrlen = sizeof(struct WS_sockaddr_in6);
1030 const struct sockaddr_in* uin = (const struct sockaddr_in*)uaddr;
1031 struct WS_sockaddr_in* win = (struct WS_sockaddr_in*)wsaddr;
1033 if (*wsaddrlen < sizeof(struct WS_sockaddr_in))
1035 win->sin_family = WS_AF_INET;
1036 win->sin_port = uin->sin_port;
1037 memcpy(&win->sin_addr,&uin->sin_addr,4); /* 4 bytes = 32 address bits */
1038 memset(&win->sin_zero, 0, 8); /* Make sure the null padding is null */
1039 *wsaddrlen = sizeof(struct WS_sockaddr_in);
1043 memset(wsaddr,0,*wsaddrlen);
1047 FIXME("Unknown address family %d\n", uaddr->sa_family);
1053 /**************************************************************************
1054 * Functions for handling overlapped I/O
1055 **************************************************************************/
1057 /* user APC called upon async completion */
1058 static void WINAPI ws2_async_apc( void *arg, IO_STATUS_BLOCK *iosb, ULONG reserved )
1060 ws2_async *wsa = arg;
1062 if (wsa->completion_func) wsa->completion_func( NtStatusToWSAError(iosb->u.Status),
1063 iosb->Information, wsa->user_overlapped,
1065 HeapFree( GetProcessHeap(), 0, wsa );
1068 /***********************************************************************
1069 * WS2_recv (INTERNAL)
1071 * Workhorse for both synchronous and asynchronous recv() operations.
1073 static int WS2_recv( int fd, struct iovec* iov, int count,
1074 struct WS_sockaddr *lpFrom, LPINT lpFromlen,
1078 union generic_unix_sockaddr unix_sockaddr;
1081 hdr.msg_name = NULL;
1085 hdr.msg_namelen = sizeof(unix_sockaddr);
1086 hdr.msg_name = &unix_sockaddr;
1089 hdr.msg_namelen = 0;
1092 hdr.msg_iovlen = count;
1093 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1094 hdr.msg_accrights = NULL;
1095 hdr.msg_accrightslen = 0;
1097 hdr.msg_control = NULL;
1098 hdr.msg_controllen = 0;
1102 if ( (n = recvmsg(fd, &hdr, *lpFlags)) == -1 )
1106 ws_sockaddr_u2ws( &unix_sockaddr.addr, lpFrom, lpFromlen ) != 0 )
1108 /* The from buffer was too small, but we read the data
1109 * anyway. Is that really bad?
1116 /***********************************************************************
1117 * WS2_async_recv (INTERNAL)
1119 * Handler for overlapped recv() operations.
1121 static NTSTATUS WS2_async_recv( void* user, IO_STATUS_BLOCK* iosb, NTSTATUS status)
1123 ws2_async* wsa = user;
1128 case STATUS_ALERTED:
1129 if ((status = wine_server_handle_to_fd( wsa->hSocket, FILE_READ_DATA, &fd, NULL ) ))
1132 result = WS2_recv( fd, wsa->iovec, wsa->n_iovecs,
1133 wsa->addr, wsa->addrlen.ptr, &wsa->flags );
1134 wine_server_release_fd( wsa->hSocket, fd );
1137 status = STATUS_SUCCESS;
1138 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1142 if (errno == EINTR || errno == EAGAIN)
1144 status = STATUS_PENDING;
1145 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1150 status = wsaErrno(); /* FIXME: is this correct ???? */
1155 if (status != STATUS_PENDING)
1157 iosb->u.Status = status;
1158 iosb->Information = result;
1163 /***********************************************************************
1164 * WS2_send (INTERNAL)
1166 * Workhorse for both synchronous and asynchronous send() operations.
1168 static int WS2_send( int fd, struct iovec* iov, int count,
1169 const struct WS_sockaddr *to, INT tolen, DWORD dwFlags )
1172 union generic_unix_sockaddr unix_addr;
1175 hdr.msg_name = NULL;
1176 hdr.msg_namelen = 0;
1180 hdr.msg_name = &unix_addr;
1181 hdr.msg_namelen = ws_sockaddr_ws2u( to, tolen, &unix_addr );
1182 if ( !hdr.msg_namelen )
1188 #if defined(HAVE_IPX) && defined(SOL_IPX)
1189 if(to->sa_family == WS_AF_IPX)
1191 struct sockaddr_ipx* uipx = (struct sockaddr_ipx*)hdr.msg_name;
1193 unsigned int len=sizeof(int);
1195 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1196 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1197 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1198 * ipx type in the sockaddr_opx structure with the stored value.
1200 if(getsockopt(fd, SOL_IPX, IPX_TYPE, &val, &len) != -1)
1201 uipx->sipx_type = val;
1207 hdr.msg_iovlen = count;
1208 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1209 hdr.msg_accrights = NULL;
1210 hdr.msg_accrightslen = 0;
1212 hdr.msg_control = NULL;
1213 hdr.msg_controllen = 0;
1217 return sendmsg(fd, &hdr, dwFlags);
1220 /***********************************************************************
1221 * WS2_async_send (INTERNAL)
1223 * Handler for overlapped send() operations.
1225 static NTSTATUS WS2_async_send(void* user, IO_STATUS_BLOCK* iosb, NTSTATUS status)
1227 ws2_async* wsa = user;
1232 case STATUS_ALERTED:
1233 if ((status = wine_server_handle_to_fd( wsa->hSocket, FILE_WRITE_DATA, &fd, NULL ) ))
1236 /* check to see if the data is ready (non-blocking) */
1237 result = WS2_send( fd, wsa->iovec, wsa->n_iovecs, wsa->addr, wsa->addrlen.val, wsa->flags );
1238 wine_server_release_fd( wsa->hSocket, fd );
1242 status = STATUS_SUCCESS;
1243 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1247 if (errno == EINTR || errno == EAGAIN)
1249 status = STATUS_PENDING;
1250 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1254 /* We set the status to a winsock error code and check for that
1255 later in NtStatusToWSAError () */
1256 status = wsaErrno();
1262 if (status != STATUS_PENDING)
1264 iosb->u.Status = status;
1265 iosb->Information = result;
1270 /***********************************************************************
1271 * WS2_async_shutdown (INTERNAL)
1273 * Handler for shutdown() operations on overlapped sockets.
1275 static NTSTATUS WS2_async_shutdown( void* user, PIO_STATUS_BLOCK iosb, NTSTATUS status )
1277 ws2_async* wsa = user;
1282 case STATUS_ALERTED:
1283 if ((status = wine_server_handle_to_fd( wsa->hSocket, 0, &fd, NULL ) ))
1286 switch ( wsa->type )
1288 case ASYNC_TYPE_READ: err = shutdown( fd, 0 ); break;
1289 case ASYNC_TYPE_WRITE: err = shutdown( fd, 1 ); break;
1291 wine_server_release_fd( wsa->hSocket, fd );
1292 status = err ? wsaErrno() : STATUS_SUCCESS;
1295 iosb->u.Status = status;
1299 /***********************************************************************
1300 * WS2_register_async_shutdown (INTERNAL)
1302 * Helper function for WS_shutdown() on overlapped sockets.
1304 static int WS2_register_async_shutdown( SOCKET s, int type )
1306 struct ws2_async *wsa;
1309 TRACE("s %ld type %d\n", s, type);
1311 wsa = HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa) );
1315 wsa->hSocket = SOCKET2HANDLE(s);
1317 wsa->completion_func = NULL;
1319 SERVER_START_REQ( register_async )
1321 req->handle = wsa->hSocket;
1323 req->async.callback = WS2_async_shutdown;
1324 req->async.iosb = &wsa->local_iosb;
1325 req->async.arg = wsa;
1326 req->async.apc = ws2_async_apc;
1327 status = wine_server_call( req );
1331 if (status != STATUS_PENDING)
1333 HeapFree( GetProcessHeap(), 0, wsa );
1334 return NtStatusToWSAError( status );
1339 /***********************************************************************
1342 SOCKET WINAPI WS_accept(SOCKET s, struct WS_sockaddr *addr,
1348 TRACE("socket %04lx\n", s );
1349 is_blocking = _is_blocking(s);
1354 int fd = get_sock_fd( s, FILE_READ_DATA, NULL );
1355 if (fd == -1) return INVALID_SOCKET;
1357 do_block(fd, POLLIN, -1);
1358 _sync_sock_state(s); /* let wineserver notice connection */
1359 release_sock_fd( s, fd );
1360 /* retrieve any error codes from it */
1361 SetLastError(_get_sock_error(s, FD_ACCEPT_BIT));
1362 /* FIXME: care about the error? */
1364 SERVER_START_REQ( accept_socket )
1366 req->lhandle = SOCKET2HANDLE(s);
1367 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
1368 req->attributes = OBJ_INHERIT;
1369 set_error( wine_server_call( req ) );
1370 as = HANDLE2SOCKET( reply->handle );
1375 if (addr) WS_getpeername(as, addr, addrlen32);
1378 } while (is_blocking);
1379 return INVALID_SOCKET;
1382 /***********************************************************************
1385 int WINAPI WS_bind(SOCKET s, const struct WS_sockaddr* name, int namelen)
1387 int fd = get_sock_fd( s, 0, NULL );
1388 int res = SOCKET_ERROR;
1390 TRACE("socket %04lx, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1394 if (!name || (name->sa_family && !SUPPORTED_PF(name->sa_family)))
1396 SetLastError(WSAEAFNOSUPPORT);
1400 union generic_unix_sockaddr uaddr;
1401 unsigned int uaddrlen = ws_sockaddr_ws2u(name, namelen, &uaddr);
1404 SetLastError(WSAEFAULT);
1408 if (bind(fd, &uaddr.addr, uaddrlen) < 0)
1410 int loc_errno = errno;
1411 WARN("\tfailure - errno = %i\n", errno);
1416 SetLastError(WSAENOTSOCK);
1419 SetLastError(WSAEINVAL);
1422 SetLastError(wsaErrno());
1428 res=0; /* success */
1432 release_sock_fd( s, fd );
1437 /***********************************************************************
1438 * closesocket (WS2_32.3)
1440 int WINAPI WS_closesocket(SOCKET s)
1442 TRACE("socket %04lx\n", s);
1443 if (CloseHandle(SOCKET2HANDLE(s))) return 0;
1444 return SOCKET_ERROR;
1447 /***********************************************************************
1448 * connect (WS2_32.4)
1450 int WINAPI WS_connect(SOCKET s, const struct WS_sockaddr* name, int namelen)
1452 int fd = get_sock_fd( s, FILE_READ_DATA, NULL );
1454 TRACE("socket %04lx, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1458 union generic_unix_sockaddr uaddr;
1459 unsigned int uaddrlen = ws_sockaddr_ws2u(name, namelen, &uaddr);
1463 SetLastError(WSAEFAULT);
1467 if (connect(fd, &uaddr.addr, uaddrlen) == 0)
1468 goto connect_success;
1471 if (errno == EINPROGRESS)
1473 /* tell wineserver that a connection is in progress */
1474 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1475 FD_CONNECT|FD_READ|FD_WRITE,
1476 FD_WINE_CONNECTED|FD_WINE_LISTENING);
1477 if (_is_blocking(s))
1481 do_block(fd, POLLIN | POLLOUT, -1);
1482 _sync_sock_state(s); /* let wineserver notice connection */
1483 /* retrieve any error codes from it */
1484 result = _get_sock_error(s, FD_CONNECT_BIT);
1486 SetLastError(result);
1489 goto connect_success;
1494 SetLastError(WSAEWOULDBLOCK);
1499 SetLastError(wsaErrno());
1501 release_sock_fd( s, fd );
1503 return SOCKET_ERROR;
1506 release_sock_fd( s, fd );
1507 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1508 FD_WINE_CONNECTED|FD_READ|FD_WRITE,
1509 FD_CONNECT|FD_WINE_LISTENING);
1513 /***********************************************************************
1514 * WSAConnect (WS2_32.30)
1516 int WINAPI WSAConnect( SOCKET s, const struct WS_sockaddr* name, int namelen,
1517 LPWSABUF lpCallerData, LPWSABUF lpCalleeData,
1518 LPQOS lpSQOS, LPQOS lpGQOS )
1520 if ( lpCallerData || lpCalleeData || lpSQOS || lpGQOS )
1521 FIXME("unsupported parameters!\n");
1522 return WS_connect( s, name, namelen );
1526 /***********************************************************************
1527 * getpeername (WS2_32.5)
1529 int WINAPI WS_getpeername(SOCKET s, struct WS_sockaddr *name, int *namelen)
1534 TRACE("socket: %04lx, ptr %p, len %08x\n", s, name, *namelen);
1536 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1537 if( (name == NULL) || (namelen == NULL) )
1539 SetLastError( WSAEFAULT );
1540 return SOCKET_ERROR;
1543 fd = get_sock_fd( s, 0, NULL );
1548 union generic_unix_sockaddr uaddr;
1549 unsigned int uaddrlen = sizeof(uaddr);
1551 if (getpeername(fd, &uaddr.addr, &uaddrlen) != 0)
1553 SetLastError(wsaErrno());
1555 else if (ws_sockaddr_u2ws(&uaddr.addr, name, namelen) != 0)
1557 /* The buffer was too small */
1558 SetLastError(WSAEFAULT);
1564 release_sock_fd( s, fd );
1569 /***********************************************************************
1570 * getsockname (WS2_32.6)
1572 int WINAPI WS_getsockname(SOCKET s, struct WS_sockaddr *name, int *namelen)
1577 TRACE("socket: %04lx, ptr %p, len %8x\n", s, name, *namelen);
1579 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1580 if( (name == NULL) || (namelen == NULL) )
1582 SetLastError( WSAEFAULT );
1583 return SOCKET_ERROR;
1586 fd = get_sock_fd( s, 0, NULL );
1591 union generic_unix_sockaddr uaddr;
1592 unsigned int uaddrlen = sizeof(uaddr);
1594 if (getsockname(fd, &uaddr.addr, &uaddrlen) != 0)
1596 SetLastError(wsaErrno());
1598 else if (!is_sockaddr_bound(&uaddr.addr, uaddrlen))
1600 SetLastError(WSAEINVAL);
1602 else if (ws_sockaddr_u2ws(&uaddr.addr, name, namelen) != 0)
1604 /* The buffer was too small */
1605 SetLastError(WSAEFAULT);
1611 release_sock_fd( s, fd );
1616 /***********************************************************************
1617 * getsockopt (WS2_32.7)
1619 INT WINAPI WS_getsockopt(SOCKET s, INT level,
1620 INT optname, char *optval, INT *optlen)
1625 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
1626 s, level, optname, optval, *optlen);
1634 /* Handle common cases. The special cases are below, sorted
1636 case WS_SO_ACCEPTCONN:
1637 case WS_SO_BROADCAST:
1640 case WS_SO_KEEPALIVE:
1641 case WS_SO_OOBINLINE:
1645 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1646 return SOCKET_ERROR;
1647 convert_sockopt(&level, &optname);
1648 if (getsockopt(fd,(int) level, optname, optval,
1649 (unsigned int *)optlen) != 0 )
1651 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1654 release_sock_fd( s, fd );
1657 case WS_SO_DONTLINGER:
1659 struct linger lingval;
1660 unsigned int len = sizeof(struct linger);
1662 if (!optlen || *optlen < sizeof(BOOL)|| !optval)
1664 SetLastError(WSAEFAULT);
1665 return SOCKET_ERROR;
1667 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1668 return SOCKET_ERROR;
1670 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &lingval, &len) != 0 )
1672 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1677 *(BOOL *)optval = (lingval.l_onoff) ? FALSE : TRUE;
1678 *optlen = sizeof(BOOL);
1681 release_sock_fd( s, fd );
1685 /* As mentioned in setsockopt, Windows ignores this, so we
1686 * always return true here */
1687 case WS_SO_DONTROUTE:
1688 if (!optlen || *optlen < sizeof(BOOL) || !optval)
1690 SetLastError(WSAEFAULT);
1691 return SOCKET_ERROR;
1693 *(BOOL *)optval = TRUE;
1694 *optlen = sizeof(BOOL);
1699 struct linger lingval;
1700 unsigned int len = sizeof(struct linger);
1702 /* struct linger and LINGER have different sizes */
1703 if (!optlen || *optlen < sizeof(LINGER) || !optval)
1705 SetLastError(WSAEFAULT);
1706 return SOCKET_ERROR;
1708 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1709 return SOCKET_ERROR;
1711 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &lingval, &len) != 0 )
1713 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1718 ((LINGER *)optval)->l_onoff = lingval.l_onoff;
1719 ((LINGER *)optval)->l_linger = lingval.l_linger;
1720 *optlen = sizeof(struct linger);
1723 release_sock_fd( s, fd );
1727 case WS_SO_MAX_MSG_SIZE:
1728 if (!optlen || *optlen < sizeof(int) || !optval)
1730 SetLastError(WSAEFAULT);
1731 return SOCKET_ERROR;
1733 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
1734 *(int *)optval = 65507;
1735 *optlen = sizeof(int);
1738 /* SO_OPENTYPE does not require a valid socket handle. */
1739 case WS_SO_OPENTYPE:
1740 if (!optlen || *optlen < sizeof(int) || !optval)
1742 SetLastError(WSAEFAULT);
1743 return SOCKET_ERROR;
1745 *(int *)optval = get_per_thread_data()->opentype;
1746 *optlen = sizeof(int);
1747 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
1751 case WS_SO_RCVTIMEO:
1754 case WS_SO_SNDTIMEO:
1756 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
1759 unsigned int len = sizeof(struct timeval);
1761 if (!optlen || *optlen < sizeof(int)|| !optval)
1763 SetLastError(WSAEFAULT);
1764 return SOCKET_ERROR;
1766 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1767 return SOCKET_ERROR;
1769 convert_sockopt(&level, &optname);
1770 if (getsockopt(fd,(int) level, optname, &tv, &len) != 0 )
1772 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1777 *(int *)optval = tv.tv_sec * 1000 + tv.tv_usec / 1000;
1778 *optlen = sizeof(int);
1781 release_sock_fd( s, fd );
1785 /* As mentioned in setsockopt, the windows style SO_REUSEADDR is
1786 * not possible in Unix, so always return false here. */
1787 case WS_SO_REUSEADDR:
1788 if (!optlen || *optlen < sizeof(int) || !optval)
1790 SetLastError(WSAEFAULT);
1791 return SOCKET_ERROR;
1794 *optlen = sizeof(int);
1798 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname);
1799 SetLastError(WSAENOPROTOOPT);
1800 return SOCKET_ERROR;
1801 } /* end switch(optname) */
1802 }/* end case WS_SOL_SOCKET */
1806 struct WS_sockaddr_ipx addr;
1807 IPX_ADDRESS_DATA *data;
1812 if ((fd = get_sock_fd( s, 0, NULL )) == -1) return SOCKET_ERROR;
1814 if(getsockopt(fd, SOL_IPX, IPX_TYPE, optval, (unsigned int*)optlen) == -1)
1821 socklen_t len=sizeof(struct ipx);
1822 if(getsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, &len) == -1 )
1825 *optval = (int)val.ipx_pt;
1828 TRACE("ptype: %d (fd: %d)\n", *(int*)optval, fd);
1829 release_sock_fd( s, fd );
1834 * On a Win2000 system with one network card there are usually
1835 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
1836 * Using this call you can then retrieve info about this all.
1837 * In case of Linux it is a bit different. Usually you have
1838 * only "one" device active and further it is not possible to
1839 * query things like the linkspeed.
1841 FIXME("IPX_ADDRESS\n");
1842 namelen = sizeof(struct WS_sockaddr_ipx);
1843 memset(&addr, 0, sizeof(struct WS_sockaddr_ipx));
1844 WS_getsockname(s, (struct WS_sockaddr*)&addr, &namelen);
1846 data = (IPX_ADDRESS_DATA*)optval;
1847 memcpy(data->nodenum,&addr.sa_nodenum,sizeof(data->nodenum));
1848 memcpy(data->netnum,&addr.sa_netnum,sizeof(data->netnum));
1849 data->adapternum = 0;
1850 data->wan = FALSE; /* We are not on a wan for now .. */
1851 data->status = FALSE; /* Since we are not on a wan, the wan link isn't up */
1852 data->maxpkt = 1467; /* This value is the default one, at least on Win2k/WinXP */
1853 data->linkspeed = 100000; /* Set the line speed in 100bit/s to 10 Mbit;
1854 * note 1MB = 1000kB in this case */
1857 case IPX_MAX_ADAPTER_NUM:
1858 FIXME("IPX_MAX_ADAPTER_NUM\n");
1859 *(int*)optval = 1; /* As noted under IPX_ADDRESS we have just one card. */
1863 FIXME("IPX optname:%x\n", optname);
1864 return SOCKET_ERROR;
1865 }/* end switch(optname) */
1866 } /* end case NSPROTO_IPX */
1868 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
1869 case WS_IPPROTO_TCP:
1872 case WS_TCP_NODELAY:
1873 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1874 return SOCKET_ERROR;
1875 convert_sockopt(&level, &optname);
1876 if (getsockopt(fd,(int) level, optname, optval,
1877 (unsigned int *)optlen) != 0 )
1879 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1882 release_sock_fd( s, fd );
1885 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname);
1886 return SOCKET_ERROR;
1891 case WS_IP_ADD_MEMBERSHIP:
1892 case WS_IP_DROP_MEMBERSHIP:
1896 case WS_IP_MULTICAST_IF:
1897 case WS_IP_MULTICAST_LOOP:
1898 case WS_IP_MULTICAST_TTL:
1902 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1903 return SOCKET_ERROR;
1904 convert_sockopt(&level, &optname);
1905 if (getsockopt(fd,(int) level, optname, optval,
1906 (unsigned int *)optlen) != 0 )
1908 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1911 release_sock_fd( s, fd );
1914 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname);
1915 return SOCKET_ERROR;
1918 FIXME("Unknown level: 0x%08x\n", level);
1919 return SOCKET_ERROR;
1920 } /* end switch(level) */
1923 /***********************************************************************
1927 WS_u_long WINAPI WS_htonl(WS_u_long hostlong)
1929 return htonl(hostlong);
1933 /***********************************************************************
1937 WS_u_short WINAPI WS_htons(WS_u_short hostshort)
1939 return htons(hostshort);
1942 /***********************************************************************
1943 * WSAHtonl (WS2_32.46)
1944 * From MSDN decription of error codes, this function should also
1945 * check if WinSock has been initialized and the socket is a valid
1946 * socket. But why? This function only translates a host byte order
1947 * u_long into a network byte order u_long...
1949 int WINAPI WSAHtonl(SOCKET s, WS_u_long hostlong, WS_u_long *lpnetlong)
1953 *lpnetlong = htonl(hostlong);
1956 WSASetLastError(WSAEFAULT);
1957 return SOCKET_ERROR;
1960 /***********************************************************************
1961 * WSAHtons (WS2_32.47)
1962 * From MSDN decription of error codes, this function should also
1963 * check if WinSock has been initialized and the socket is a valid
1964 * socket. But why? This function only translates a host byte order
1965 * u_short into a network byte order u_short...
1967 int WINAPI WSAHtons(SOCKET s, WS_u_short hostshort, WS_u_short *lpnetshort)
1972 *lpnetshort = htons(hostshort);
1975 WSASetLastError(WSAEFAULT);
1976 return SOCKET_ERROR;
1980 /***********************************************************************
1981 * inet_addr (WINSOCK.10)
1982 * inet_addr (WS2_32.11)
1984 WS_u_long WINAPI WS_inet_addr(const char *cp)
1986 if (!cp) return INADDR_NONE;
1987 return inet_addr(cp);
1991 /***********************************************************************
1992 * ntohl (WINSOCK.14)
1995 WS_u_long WINAPI WS_ntohl(WS_u_long netlong)
1997 return ntohl(netlong);
2001 /***********************************************************************
2002 * ntohs (WINSOCK.15)
2005 WS_u_short WINAPI WS_ntohs(WS_u_short netshort)
2007 return ntohs(netshort);
2011 /***********************************************************************
2012 * inet_ntoa (WS2_32.12)
2014 char* WINAPI WS_inet_ntoa(struct WS_in_addr in)
2016 /* use "buffer for dummies" here because some applications have a
2017 * propensity to decode addresses in ws_hostent structure without
2018 * saving them first...
2020 static char dbuffer[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
2022 char* s = inet_ntoa(*((struct in_addr*)&in));
2028 SetLastError(wsaErrno());
2032 /**********************************************************************
2033 * WSAIoctl (WS2_32.50)
2036 INT WINAPI WSAIoctl(SOCKET s,
2037 DWORD dwIoControlCode,
2040 LPVOID lpbOutBuffer,
2042 LPDWORD lpcbBytesReturned,
2043 LPWSAOVERLAPPED lpOverlapped,
2044 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
2046 TRACE("%ld, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
2047 s, dwIoControlCode, lpvInBuffer, cbInBuffer, lpbOutBuffer,
2048 cbOutBuffer, lpcbBytesReturned, lpOverlapped, lpCompletionRoutine);
2050 switch( dwIoControlCode )
2053 if (cbInBuffer != sizeof(WS_u_long)) {
2054 WSASetLastError(WSAEFAULT);
2055 return SOCKET_ERROR;
2057 return WS_ioctlsocket( s, WS_FIONBIO, lpvInBuffer);
2060 if (cbOutBuffer != sizeof(WS_u_long)) {
2061 WSASetLastError(WSAEFAULT);
2062 return SOCKET_ERROR;
2064 return WS_ioctlsocket( s, WS_FIONREAD, lpbOutBuffer);
2066 case WS_SIO_GET_INTERFACE_LIST:
2068 INTERFACE_INFO* intArray = (INTERFACE_INFO*)lpbOutBuffer;
2069 DWORD size, numInt, apiReturn;
2072 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
2076 WSASetLastError(WSAEFAULT);
2077 return SOCKET_ERROR;
2079 if (!lpcbBytesReturned)
2081 WSASetLastError(WSAEFAULT);
2082 return SOCKET_ERROR;
2085 fd = get_sock_fd( s, 0, NULL );
2086 if (fd == -1) return SOCKET_ERROR;
2088 apiReturn = GetAdaptersInfo(NULL, &size);
2089 if (apiReturn == ERROR_NO_DATA)
2093 else if (apiReturn == ERROR_BUFFER_OVERFLOW)
2095 PIP_ADAPTER_INFO table = HeapAlloc(GetProcessHeap(),0,size);
2099 if (GetAdaptersInfo(table, &size) == NO_ERROR)
2101 PIP_ADAPTER_INFO ptr;
2103 if (size*sizeof(INTERFACE_INFO)/sizeof(IP_ADAPTER_INFO) > cbOutBuffer)
2105 WARN("Buffer too small = %u, cbOutBuffer = %u\n", size, cbOutBuffer);
2106 HeapFree(GetProcessHeap(),0,table);
2107 release_sock_fd( s, fd );
2108 WSASetLastError(WSAEFAULT);
2109 return SOCKET_ERROR;
2111 for (ptr = table, numInt = 0; ptr;
2112 ptr = ptr->Next, intArray++, numInt++)
2114 unsigned int addr, mask, bcast;
2115 struct ifreq ifInfo;
2117 /* Socket Status Flags */
2118 lstrcpynA(ifInfo.ifr_name, ptr->AdapterName, IFNAMSIZ);
2119 if (ioctl(fd, SIOCGIFFLAGS, &ifInfo) < 0)
2121 ERR("Error obtaining status flags for socket!\n");
2122 HeapFree(GetProcessHeap(),0,table);
2123 release_sock_fd( s, fd );
2124 WSASetLastError(WSAEINVAL);
2125 return SOCKET_ERROR;
2129 /* set flags; the values of IFF_* are not the same
2130 under Linux and Windows, therefore must generate
2132 intArray->iiFlags = 0;
2133 if (ifInfo.ifr_flags & IFF_BROADCAST)
2134 intArray->iiFlags |= WS_IFF_BROADCAST;
2135 #ifdef IFF_POINTOPOINT
2136 if (ifInfo.ifr_flags & IFF_POINTOPOINT)
2137 intArray->iiFlags |= WS_IFF_POINTTOPOINT;
2139 if (ifInfo.ifr_flags & IFF_LOOPBACK)
2140 intArray->iiFlags |= WS_IFF_LOOPBACK;
2141 if (ifInfo.ifr_flags & IFF_UP)
2142 intArray->iiFlags |= WS_IFF_UP;
2143 if (ifInfo.ifr_flags & IFF_MULTICAST)
2144 intArray->iiFlags |= WS_IFF_MULTICAST;
2147 addr = inet_addr(ptr->IpAddressList.IpAddress.String);
2148 mask = inet_addr(ptr->IpAddressList.IpMask.String);
2149 bcast = addr | (addr & !mask);
2150 intArray->iiAddress.AddressIn.sin_family = AF_INET;
2151 intArray->iiAddress.AddressIn.sin_port = 0;
2152 intArray->iiAddress.AddressIn.sin_addr.WS_s_addr =
2154 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
2155 intArray->iiNetmask.AddressIn.sin_port = 0;
2156 intArray->iiNetmask.AddressIn.sin_addr.WS_s_addr =
2158 intArray->iiBroadcastAddress.AddressIn.sin_family =
2160 intArray->iiBroadcastAddress.AddressIn.sin_port = 0;
2161 intArray->iiBroadcastAddress.AddressIn.sin_addr.
2167 ERR("Unable to get interface table!\n");
2168 release_sock_fd( s, fd );
2169 HeapFree(GetProcessHeap(),0,table);
2170 WSASetLastError(WSAEINVAL);
2171 return SOCKET_ERROR;
2173 HeapFree(GetProcessHeap(),0,table);
2177 release_sock_fd( s, fd );
2178 WSASetLastError(WSAEINVAL);
2179 return SOCKET_ERROR;
2184 ERR("Unable to get interface table!\n");
2185 release_sock_fd( s, fd );
2186 WSASetLastError(WSAEINVAL);
2187 return SOCKET_ERROR;
2189 /* Calculate the size of the array being returned */
2190 *lpcbBytesReturned = sizeof(INTERFACE_INFO) * numInt;
2191 release_sock_fd( s, fd );
2195 case WS_SIO_ADDRESS_LIST_CHANGE:
2196 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
2197 /* FIXME: error and return code depend on whether socket was created
2198 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
2201 case WS_SIO_ADDRESS_LIST_QUERY:
2205 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
2207 if (!lpcbBytesReturned)
2209 WSASetLastError(WSAEFAULT);
2210 return SOCKET_ERROR;
2213 if (GetAdaptersInfo(NULL, &size) == ERROR_BUFFER_OVERFLOW)
2215 IP_ADAPTER_INFO *p, *table = HeapAlloc(GetProcessHeap(), 0, size);
2218 if (!table || GetAdaptersInfo(table, &size))
2220 HeapFree(GetProcessHeap(), 0, table);
2221 WSASetLastError(WSAEINVAL);
2222 return SOCKET_ERROR;
2225 for (p = table, num = 0; p; p = p->Next)
2226 if (p->IpAddressList.IpAddress.String[0]) num++;
2228 need = sizeof(SOCKET_ADDRESS_LIST) + sizeof(SOCKET_ADDRESS) * (num - 1);
2229 need += sizeof(SOCKADDR) * num;
2230 *lpcbBytesReturned = need;
2232 if (need > cbOutBuffer)
2234 HeapFree(GetProcessHeap(), 0, table);
2235 WSASetLastError(WSAEFAULT);
2236 return SOCKET_ERROR;
2243 SOCKET_ADDRESS_LIST *sa_list = (SOCKET_ADDRESS_LIST *)lpbOutBuffer;
2244 SOCKADDR_IN *sockaddr;
2246 sa = sa_list->Address;
2247 sockaddr = (SOCKADDR_IN *)((char *)sa + num * sizeof(SOCKET_ADDRESS));
2248 sa_list->iAddressCount = num;
2250 for (p = table, i = 0; p; p = p->Next)
2252 if (!p->IpAddressList.IpAddress.String[0]) continue;
2254 sa[i].lpSockaddr = (SOCKADDR *)&sockaddr[i];
2255 sa[i].iSockaddrLength = sizeof(SOCKADDR);
2257 sockaddr[i].sin_family = AF_INET;
2258 sockaddr[i].sin_port = 0;
2259 sockaddr[i].sin_addr.WS_s_addr = inet_addr(p->IpAddressList.IpAddress.String);
2264 HeapFree(GetProcessHeap(), 0, table);
2269 WARN("unable to get IP address list\n");
2270 WSASetLastError(WSAEINVAL);
2271 return SOCKET_ERROR;
2275 FIXME("SIO_FLUSH: stub.\n");
2279 FIXME("unsupported WS_IOCTL cmd (%08x)\n", dwIoControlCode);
2280 WSASetLastError(WSAEOPNOTSUPP);
2281 return SOCKET_ERROR;
2288 /***********************************************************************
2289 * ioctlsocket (WS2_32.10)
2291 int WINAPI WS_ioctlsocket(SOCKET s, LONG cmd, WS_u_long *argp)
2296 TRACE("socket %04lx, cmd %08x, ptr %p\n", s, cmd, argp);
2305 if( _get_sock_mask(s) )
2307 /* AsyncSelect()'ed sockets are always nonblocking */
2308 if (*argp) return 0;
2309 SetLastError(WSAEINVAL);
2310 return SOCKET_ERROR;
2312 fd = get_sock_fd( s, 0, NULL );
2318 _enable_event(SOCKET2HANDLE(s), 0, FD_WINE_NONBLOCKING, 0);
2319 ret = fcntl( fd, F_SETFL, O_NONBLOCK );
2323 _enable_event(SOCKET2HANDLE(s), 0, 0, FD_WINE_NONBLOCKING);
2324 ret = fcntl( fd, F_SETFL, 0 );
2326 release_sock_fd( s, fd );
2328 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2330 return SOCKET_ERROR;
2337 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
2338 SetLastError(WSAEINVAL);
2339 return SOCKET_ERROR;
2341 case SIOCGIFBRDADDR:
2342 case SIOCGIFNETMASK:
2344 /* These don't need any special handling. They are used by
2345 WsControl, and are here to suppress an unnecessary warning. */
2349 /* Netscape tries hard to use bogus ioctl 0x667e */
2350 /* FIXME: 0x667e above is ('f' << 8) | 126, and is a low word of
2351 * FIONBIO (_IOW('f', 126, u_long)), how that should be handled?
2353 WARN("\tunknown WS_IOCTL cmd (%08x)\n", cmd);
2357 fd = get_sock_fd( s, 0, NULL );
2360 if( ioctl(fd, newcmd, (char*)argp ) == 0 )
2362 release_sock_fd( s, fd );
2365 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2366 release_sock_fd( s, fd );
2368 return SOCKET_ERROR;
2371 /***********************************************************************
2372 * listen (WS2_32.13)
2374 int WINAPI WS_listen(SOCKET s, int backlog)
2376 int fd = get_sock_fd( s, FILE_READ_DATA, NULL );
2378 TRACE("socket %04lx, backlog %d\n", s, backlog);
2381 if (listen(fd, backlog) == 0)
2383 release_sock_fd( s, fd );
2384 _enable_event(SOCKET2HANDLE(s), FD_ACCEPT,
2386 FD_CONNECT|FD_WINE_CONNECTED);
2389 SetLastError(wsaErrno());
2390 release_sock_fd( s, fd );
2392 return SOCKET_ERROR;
2395 /***********************************************************************
2398 int WINAPI WS_recv(SOCKET s, char *buf, int len, int flags)
2400 DWORD n, dwFlags = flags;
2406 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, NULL, NULL, NULL, NULL) == SOCKET_ERROR )
2407 return SOCKET_ERROR;
2412 /***********************************************************************
2413 * recvfrom (WS2_32.17)
2415 int WINAPI WS_recvfrom(SOCKET s, char *buf, INT len, int flags,
2416 struct WS_sockaddr *from, int *fromlen)
2418 DWORD n, dwFlags = flags;
2424 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, from, fromlen, NULL, NULL) == SOCKET_ERROR )
2425 return SOCKET_ERROR;
2430 /* allocate a poll array for the corresponding fd sets */
2431 static struct pollfd *fd_sets_to_poll( const WS_fd_set *readfds, const WS_fd_set *writefds,
2432 const WS_fd_set *exceptfds, int *count_ptr )
2434 int i, j = 0, count = 0;
2437 if (readfds) count += readfds->fd_count;
2438 if (writefds) count += writefds->fd_count;
2439 if (exceptfds) count += exceptfds->fd_count;
2441 if (!count) return NULL;
2442 if (!(fds = HeapAlloc( GetProcessHeap(), 0, count * sizeof(fds[0])))) return NULL;
2444 for (i = 0; i < readfds->fd_count; i++, j++)
2446 fds[j].fd = get_sock_fd( readfds->fd_array[i], FILE_READ_DATA, NULL );
2447 fds[j].events = POLLIN;
2451 for (i = 0; i < writefds->fd_count; i++, j++)
2453 fds[j].fd = get_sock_fd( writefds->fd_array[i], FILE_WRITE_DATA, NULL );
2454 fds[j].events = POLLOUT;
2458 for (i = 0; i < exceptfds->fd_count; i++, j++)
2460 fds[j].fd = get_sock_fd( exceptfds->fd_array[i], 0, NULL );
2461 fds[j].events = POLLHUP;
2467 /* release the file descriptor obtained in fd_sets_to_poll */
2468 /* must be called with the original fd_set arrays, before calling get_poll_results */
2469 static void release_poll_fds( const WS_fd_set *readfds, const WS_fd_set *writefds,
2470 const WS_fd_set *exceptfds, struct pollfd *fds )
2476 for (i = 0; i < readfds->fd_count; i++, j++)
2477 if (fds[j].fd != -1) release_sock_fd( readfds->fd_array[i], fds[j].fd );
2481 for (i = 0; i < writefds->fd_count; i++, j++)
2482 if (fds[j].fd != -1) release_sock_fd( writefds->fd_array[i], fds[j].fd );
2486 for (i = 0; i < exceptfds->fd_count; i++, j++)
2487 if (fds[j].fd != -1)
2489 /* make sure we have a real error before releasing the fd */
2490 if (!sock_error_p( fds[j].fd )) fds[j].revents = 0;
2491 release_sock_fd( exceptfds->fd_array[i], fds[j].fd );
2496 /* map the poll results back into the Windows fd sets */
2497 static int get_poll_results( WS_fd_set *readfds, WS_fd_set *writefds, WS_fd_set *exceptfds,
2498 const struct pollfd *fds )
2500 int i, j = 0, k, total = 0;
2504 for (i = k = 0; i < readfds->fd_count; i++, j++)
2505 if (fds[j].revents) readfds->fd_array[k++] = readfds->fd_array[i];
2506 readfds->fd_count = k;
2511 for (i = k = 0; i < writefds->fd_count; i++, j++)
2512 if (fds[j].revents) writefds->fd_array[k++] = writefds->fd_array[i];
2513 writefds->fd_count = k;
2518 for (i = k = 0; i < exceptfds->fd_count; i++, j++)
2519 if (fds[j].revents) exceptfds->fd_array[k++] = exceptfds->fd_array[i];
2520 exceptfds->fd_count = k;
2527 /***********************************************************************
2528 * select (WS2_32.18)
2530 int WINAPI WS_select(int nfds, WS_fd_set *ws_readfds,
2531 WS_fd_set *ws_writefds, WS_fd_set *ws_exceptfds,
2532 const struct WS_timeval* ws_timeout)
2534 struct pollfd *pollfds;
2535 int count, ret, timeout = -1;
2537 TRACE("read %p, write %p, excp %p timeout %p\n",
2538 ws_readfds, ws_writefds, ws_exceptfds, ws_timeout);
2540 if (!(pollfds = fd_sets_to_poll( ws_readfds, ws_writefds, ws_exceptfds, &count )) && count)
2542 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
2543 return SOCKET_ERROR;
2546 if (ws_timeout) timeout = (ws_timeout->tv_sec * 1000) + (ws_timeout->tv_usec + 999) / 1000;
2548 ret = poll( pollfds, count, timeout );
2549 release_poll_fds( ws_readfds, ws_writefds, ws_exceptfds, pollfds );
2551 if (ret == -1) SetLastError(wsaErrno());
2552 else ret = get_poll_results( ws_readfds, ws_writefds, ws_exceptfds, pollfds );
2553 HeapFree( GetProcessHeap(), 0, pollfds );
2558 /***********************************************************************
2561 int WINAPI WS_send(SOCKET s, const char *buf, int len, int flags)
2567 wsabuf.buf = (char*) buf;
2569 if ( WSASendTo( s, &wsabuf, 1, &n, flags, NULL, 0, NULL, NULL) == SOCKET_ERROR )
2570 return SOCKET_ERROR;
2575 /***********************************************************************
2576 * WSASend (WS2_32.72)
2578 INT WINAPI WSASend( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2579 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2580 LPWSAOVERLAPPED lpOverlapped,
2581 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2583 return WSASendTo( s, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags,
2584 NULL, 0, lpOverlapped, lpCompletionRoutine );
2587 /***********************************************************************
2588 * WSASendDisconnect (WS2_32.73)
2590 INT WINAPI WSASendDisconnect( SOCKET s, LPWSABUF lpBuffers )
2592 return WS_shutdown( s, SD_SEND );
2596 /***********************************************************************
2597 * WSASendTo (WS2_32.74)
2599 INT WINAPI WSASendTo( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2600 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2601 const struct WS_sockaddr *to, int tolen,
2602 LPWSAOVERLAPPED lpOverlapped,
2603 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2605 unsigned int i, options;
2607 struct iovec iovec[WS_MSG_MAXIOVLEN];
2609 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
2610 s, lpBuffers, dwBufferCount, dwFlags,
2611 to, tolen, lpOverlapped, lpCompletionRoutine);
2613 if (dwBufferCount > WS_MSG_MAXIOVLEN)
2615 WSASetLastError( WSAEINVAL );
2616 return SOCKET_ERROR;
2619 fd = get_sock_fd( s, FILE_WRITE_DATA, &options );
2620 TRACE( "fd=%d, options=%x\n", fd, options );
2622 if ( fd == -1 ) return SOCKET_ERROR;
2624 if ( !lpNumberOfBytesSent )
2630 for ( i = 0; i < dwBufferCount; i++ )
2632 iovec[i].iov_base = lpBuffers[i].buf;
2633 iovec[i].iov_len = lpBuffers[i].len;
2638 n = WS2_send( fd, iovec, dwBufferCount, to, tolen, dwFlags );
2639 if (n != -1 || errno != EINTR) break;
2641 if (n == -1 && errno != EAGAIN)
2647 if ((lpOverlapped || lpCompletionRoutine) &&
2648 !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
2650 IO_STATUS_BLOCK *iosb;
2651 struct ws2_async *wsa = HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa) );
2658 release_sock_fd( s, fd );
2660 wsa->hSocket = SOCKET2HANDLE(s);
2661 wsa->addr = (struct WS_sockaddr *)to;
2662 wsa->addrlen.val = tolen;
2664 wsa->user_overlapped = lpOverlapped;
2665 wsa->completion_func = lpCompletionRoutine;
2666 wsa->n_iovecs = dwBufferCount;
2667 memcpy( wsa->iovec, iovec, dwBufferCount * sizeof(*iovec) );
2669 iosb = lpOverlapped ? (IO_STATUS_BLOCK *)lpOverlapped : &wsa->local_iosb;
2672 iosb->u.Status = STATUS_PENDING;
2673 iosb->Information = 0;
2675 SERVER_START_REQ( register_async )
2677 req->handle = wsa->hSocket;
2678 req->type = ASYNC_TYPE_WRITE;
2679 req->async.callback = WS2_async_send;
2680 req->async.iosb = iosb;
2681 req->async.arg = wsa;
2682 req->async.apc = ws2_async_apc;
2683 req->async.event = lpCompletionRoutine ? 0 : lpOverlapped->hEvent;
2684 err = wine_server_call( req );
2688 if (err != STATUS_PENDING) HeapFree( GetProcessHeap(), 0, wsa );
2689 WSASetLastError( NtStatusToWSAError( err ));
2690 return SOCKET_ERROR;
2693 iosb->u.Status = STATUS_SUCCESS;
2694 iosb->Information = n;
2695 *lpNumberOfBytesSent = n;
2696 if (!wsa->completion_func)
2698 SetEvent( lpOverlapped->hEvent );
2699 HeapFree( GetProcessHeap(), 0, wsa );
2701 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)ws2_async_apc,
2702 (ULONG_PTR)wsa, (ULONG_PTR)iosb, 0 );
2706 if ( _is_blocking(s) )
2708 /* On a blocking non-overlapped stream socket,
2709 * sending blocks until the entire buffer is sent. */
2710 DWORD timeout_start = GetTickCount();
2711 unsigned int first_buff = 0;
2713 *lpNumberOfBytesSent = 0;
2715 while (first_buff < dwBufferCount)
2718 int timeout = GET_SNDTIMEO(fd);
2722 *lpNumberOfBytesSent += n;
2723 while (first_buff < dwBufferCount && iovec[first_buff].iov_len <= n)
2724 n -= iovec[first_buff++].iov_len;
2725 if (first_buff >= dwBufferCount) break;
2726 iovec[first_buff].iov_base = (char*)iovec[first_buff].iov_base + n;
2727 iovec[first_buff].iov_len -= n;
2732 timeout -= timeout_start - GetTickCount();
2733 if (timeout < 0) timeout = 0;
2737 pfd.events = POLLOUT;
2739 if (!timeout || !poll( &pfd, 1, timeout ))
2742 goto error; /* msdn says a timeout in send is fatal */
2745 n = WS2_send( fd, iovec + first_buff, dwBufferCount - first_buff, to, tolen, dwFlags );
2746 if (n == -1 && errno != EAGAIN && errno != EINTR)
2753 else /* non-blocking */
2755 _enable_event(SOCKET2HANDLE(s), FD_WRITE, 0, 0);
2758 err = WSAEWOULDBLOCK;
2761 *lpNumberOfBytesSent = n;
2764 TRACE(" -> %i bytes\n", *lpNumberOfBytesSent);
2766 release_sock_fd( s, fd );
2770 release_sock_fd( s, fd );
2771 WARN(" -> ERROR %d\n", err);
2772 WSASetLastError(err);
2773 return SOCKET_ERROR;
2776 /***********************************************************************
2777 * sendto (WS2_32.20)
2779 int WINAPI WS_sendto(SOCKET s, const char *buf, int len, int flags,
2780 const struct WS_sockaddr *to, int tolen)
2786 wsabuf.buf = (char*) buf;
2788 if ( WSASendTo(s, &wsabuf, 1, &n, flags, to, tolen, NULL, NULL) == SOCKET_ERROR )
2789 return SOCKET_ERROR;
2794 /***********************************************************************
2795 * setsockopt (WS2_32.21)
2797 int WINAPI WS_setsockopt(SOCKET s, int level, int optname,
2798 const char *optval, int optlen)
2802 struct linger linger;
2803 struct timeval tval;
2805 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
2806 s, level, optname, optval, optlen);
2813 /* Some options need some conversion before they can be sent to
2814 * setsockopt. The conversions are done here, then they will fall though
2815 * to the general case. Special options that are not passed to
2816 * setsockopt follow below that.*/
2818 case WS_SO_DONTLINGER:
2819 linger.l_onoff = *((const int*)optval) ? 0: 1;
2820 linger.l_linger = 0;
2822 optname = SO_LINGER;
2823 optval = (char*)&linger;
2824 optlen = sizeof(struct linger);
2828 linger.l_onoff = ((LINGER*)optval)->l_onoff;
2829 linger.l_linger = ((LINGER*)optval)->l_linger;
2830 /* FIXME: what is documented behavior if SO_LINGER optval
2833 optname = SO_LINGER;
2834 optval = (char*)&linger;
2835 optlen = sizeof(struct linger);
2839 if (*(const int*)optval < 2048)
2841 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval );
2846 /* The options listed here don't need any special handling. Thanks to
2847 * the conversion happening above, options from there will fall through
2849 case WS_SO_ACCEPTCONN:
2850 case WS_SO_BROADCAST:
2852 case WS_SO_KEEPALIVE:
2853 case WS_SO_OOBINLINE:
2856 convert_sockopt(&level, &optname);
2859 /* SO_DEBUG is a privileged operation, ignore it. */
2861 TRACE("Ignoring SO_DEBUG\n");
2864 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
2865 * socket. According to MSDN, this option is silently ignored.*/
2866 case WS_SO_DONTROUTE:
2867 TRACE("Ignoring SO_DONTROUTE\n");
2870 /* Stops two sockets from being bound to the same port. Always happens
2871 * on unix systems, so just drop it. */
2872 case WS_SO_EXCLUSIVEADDRUSE:
2873 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
2876 /* SO_OPENTYPE does not require a valid socket handle. */
2877 case WS_SO_OPENTYPE:
2878 if (!optlen || optlen < sizeof(int) || !optval)
2880 SetLastError(WSAEFAULT);
2881 return SOCKET_ERROR;
2883 get_per_thread_data()->opentype = *(const int *)optval;
2884 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
2887 /* SO_REUSEADDR allows two applications to bind to the same port at at
2888 * same time. There is no direct way to do that in unix. While Wineserver
2889 * might do this, it does not seem useful for now, so just ignore it.*/
2890 case WS_SO_REUSEADDR:
2891 TRACE("Ignoring SO_REUSEADDR, does not translate\n");
2895 case WS_SO_RCVTIMEO:
2898 case WS_SO_SNDTIMEO:
2900 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
2901 if (optval && optlen == sizeof(UINT32)) {
2902 /* WinSock passes miliseconds instead of struct timeval */
2903 tval.tv_usec = (*(const UINT32*)optval % 1000) * 1000;
2904 tval.tv_sec = *(const UINT32*)optval / 1000;
2905 /* min of 500 milisec */
2906 if (tval.tv_sec == 0 && tval.tv_usec < 500000)
2907 tval.tv_usec = 500000;
2908 optlen = sizeof(struct timeval);
2909 optval = (char*)&tval;
2910 } else if (optlen == sizeof(struct timeval)) {
2911 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen);
2913 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen);
2916 convert_sockopt(&level, &optname);
2921 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname);
2922 SetLastError(WSAENOPROTOOPT);
2923 return SOCKET_ERROR;
2925 break; /* case WS_SOL_SOCKET */
2932 fd = get_sock_fd( s, 0, NULL );
2933 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval, fd);
2935 /* We try to set the ipx type on ipx socket level. */
2937 if(setsockopt(fd, SOL_IPX, IPX_TYPE, optval, optlen) == -1)
2939 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
2940 release_sock_fd( s, fd );
2941 return SOCKET_ERROR;
2946 /* Should we retrieve val using a getsockopt call and then
2947 * set the modified one? */
2948 val.ipx_pt = *optval;
2949 setsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, sizeof(struct ipx));
2952 release_sock_fd( s, fd );
2955 case IPX_FILTERPTYPE:
2956 /* Sets the receive filter packet type, at the moment we don't support it */
2957 FIXME("IPX_FILTERPTYPE: %x\n", *optval);
2958 /* Returning 0 is better for now than returning a SOCKET_ERROR */
2962 FIXME("opt_name:%x\n", optname);
2963 return SOCKET_ERROR;
2965 break; /* case NSPROTO_IPX */
2968 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
2969 case WS_IPPROTO_TCP:
2972 case WS_TCP_NODELAY:
2973 convert_sockopt(&level, &optname);
2976 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname);
2977 return SOCKET_ERROR;
2984 case WS_IP_ADD_MEMBERSHIP:
2985 case WS_IP_DROP_MEMBERSHIP:
2989 case WS_IP_MULTICAST_IF:
2990 case WS_IP_MULTICAST_LOOP:
2991 case WS_IP_MULTICAST_TTL:
2995 convert_sockopt(&level, &optname);
2998 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname);
2999 return SOCKET_ERROR;
3004 FIXME("Unknown level: 0x%08x\n", level);
3005 return SOCKET_ERROR;
3006 } /* end switch(level) */
3008 /* avoid endianness issues if argument is a 16-bit int */
3009 if (optval && optlen < sizeof(int))
3011 woptval= *((const INT16 *) optval);
3012 optval= (char*) &woptval;
3015 fd = get_sock_fd( s, 0, NULL );
3016 if (fd == -1) return SOCKET_ERROR;
3018 if (setsockopt(fd, level, optname, optval, optlen) == 0)
3020 release_sock_fd( s, fd );
3023 TRACE("Setting socket error, %d\n", wsaErrno());
3024 SetLastError(wsaErrno());
3025 release_sock_fd( s, fd );
3027 return SOCKET_ERROR;
3030 /***********************************************************************
3031 * shutdown (WS2_32.22)
3033 int WINAPI WS_shutdown(SOCKET s, int how)
3035 int fd, err = WSAENOTSOCK;
3036 unsigned int options, clear_flags = 0;
3038 fd = get_sock_fd( s, 0, &options );
3039 TRACE("socket %04lx, how %i %x\n", s, how, options );
3042 return SOCKET_ERROR;
3046 case 0: /* drop receives */
3047 clear_flags |= FD_READ;
3049 case 1: /* drop sends */
3050 clear_flags |= FD_WRITE;
3052 case 2: /* drop all */
3053 clear_flags |= FD_READ|FD_WRITE;
3055 clear_flags |= FD_WINE_LISTENING;
3058 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
3063 err = WS2_register_async_shutdown( s, ASYNC_TYPE_READ );
3066 err = WS2_register_async_shutdown( s, ASYNC_TYPE_WRITE );
3070 err = WS2_register_async_shutdown( s, ASYNC_TYPE_READ );
3071 if (!err) err = WS2_register_async_shutdown( s, ASYNC_TYPE_WRITE );
3074 if (err) goto error;
3076 else /* non-overlapped mode */
3078 if ( shutdown( fd, how ) )
3085 release_sock_fd( s, fd );
3086 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
3087 if ( how > 1) WSAAsyncSelect( s, 0, 0, 0 );
3091 release_sock_fd( s, fd );
3092 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
3093 WSASetLastError( err );
3094 return SOCKET_ERROR;
3097 /***********************************************************************
3098 * socket (WS2_32.23)
3100 SOCKET WINAPI WS_socket(int af, int type, int protocol)
3102 TRACE("af=%d type=%d protocol=%d\n", af, type, protocol);
3104 return WSASocketA( af, type, protocol, NULL, 0,
3105 get_per_thread_data()->opentype ? 0 : WSA_FLAG_OVERLAPPED );
3109 /***********************************************************************
3110 * gethostbyaddr (WS2_32.51)
3112 struct WS_hostent* WINAPI WS_gethostbyaddr(const char *addr, int len, int type)
3114 struct WS_hostent *retval = NULL;
3115 struct hostent* host;
3117 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3120 struct hostent hostentry;
3123 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
3125 int res = gethostbyaddr_r(addr, len, type,
3126 &hostentry, extrabuf, ebufsize, &host, &locerr);
3127 if( res != ERANGE) break;
3129 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
3131 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
3133 EnterCriticalSection( &csWSgetXXXbyYYY );
3134 host = gethostbyaddr(addr, len, type);
3135 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
3137 if( host != NULL ) retval = WS_dup_he(host);
3138 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3139 HeapFree(GetProcessHeap(),0,extrabuf);
3141 LeaveCriticalSection( &csWSgetXXXbyYYY );
3143 TRACE("ptr %p, len %d, type %d ret %p\n", addr, len, type, retval);
3147 /***********************************************************************
3148 * gethostbyname (WS2_32.52)
3150 struct WS_hostent* WINAPI WS_gethostbyname(const char* name)
3152 struct WS_hostent *retval = NULL;
3153 struct hostent* host;
3154 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3157 struct hostent hostentry;
3158 int locerr = ENOBUFS;
3161 if( !name || !name[0]) {
3163 if( gethostname( buf, 100) == -1) {
3164 SetLastError( WSAENOBUFS); /* appropriate ? */
3168 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3170 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
3172 int res = gethostbyname_r(name, &hostentry, extrabuf, ebufsize, &host, &locerr);
3173 if( res != ERANGE) break;
3175 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
3177 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
3179 EnterCriticalSection( &csWSgetXXXbyYYY );
3180 host = gethostbyname(name);
3181 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
3183 if (host) retval = WS_dup_he(host);
3184 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3185 HeapFree(GetProcessHeap(),0,extrabuf);
3187 LeaveCriticalSection( &csWSgetXXXbyYYY );
3189 TRACE( "%s ret %p\n", debugstr_a(name), retval );
3194 /***********************************************************************
3195 * getprotobyname (WS2_32.53)
3197 struct WS_protoent* WINAPI WS_getprotobyname(const char* name)
3199 struct WS_protoent* retval = NULL;
3200 #ifdef HAVE_GETPROTOBYNAME
3201 struct protoent* proto;
3202 EnterCriticalSection( &csWSgetXXXbyYYY );
3203 if( (proto = getprotobyname(name)) != NULL )
3205 retval = WS_dup_pe(proto);
3208 MESSAGE("protocol %s not found; You might want to add "
3209 "this to /etc/protocols\n", debugstr_a(name) );
3210 SetLastError(WSANO_DATA);
3212 LeaveCriticalSection( &csWSgetXXXbyYYY );
3214 TRACE( "%s ret %p\n", debugstr_a(name), retval );
3219 /***********************************************************************
3220 * getprotobynumber (WS2_32.54)
3222 struct WS_protoent* WINAPI WS_getprotobynumber(int number)
3224 struct WS_protoent* retval = NULL;
3225 #ifdef HAVE_GETPROTOBYNUMBER
3226 struct protoent* proto;
3227 EnterCriticalSection( &csWSgetXXXbyYYY );
3228 if( (proto = getprotobynumber(number)) != NULL )
3230 retval = WS_dup_pe(proto);
3233 MESSAGE("protocol number %d not found; You might want to add "
3234 "this to /etc/protocols\n", number );
3235 SetLastError(WSANO_DATA);
3237 LeaveCriticalSection( &csWSgetXXXbyYYY );
3239 TRACE("%i ret %p\n", number, retval);
3244 /***********************************************************************
3245 * getservbyname (WS2_32.55)
3247 struct WS_servent* WINAPI WS_getservbyname(const char *name, const char *proto)
3249 struct WS_servent* retval = NULL;
3250 struct servent* serv;
3252 char *proto_str = NULL;
3254 if (!(name_str = strdup_lower(name))) return NULL;
3256 if (proto && *proto)
3258 if (!(proto_str = strdup_lower(proto)))
3260 HeapFree( GetProcessHeap(), 0, name_str );
3265 EnterCriticalSection( &csWSgetXXXbyYYY );
3266 serv = getservbyname(name_str, proto_str);
3269 retval = WS_dup_se(serv);
3271 else SetLastError(WSANO_DATA);
3272 LeaveCriticalSection( &csWSgetXXXbyYYY );
3273 HeapFree( GetProcessHeap(), 0, proto_str );
3274 HeapFree( GetProcessHeap(), 0, name_str );
3275 TRACE( "%s, %s ret %p\n", debugstr_a(name), debugstr_a(proto), retval );
3279 /***********************************************************************
3280 * freeaddrinfo (WS2_32.@)
3282 void WINAPI WS_freeaddrinfo(struct WS_addrinfo *res)
3285 struct WS_addrinfo *next;
3287 HeapFree(GetProcessHeap(),0,res->ai_canonname);
3288 HeapFree(GetProcessHeap(),0,res->ai_addr);
3289 next = res->ai_next;
3290 HeapFree(GetProcessHeap(),0,res);
3295 /* helper functions for getaddrinfo() */
3296 static int convert_aiflag_w2u(int winflags) {
3297 int i, unixflags = 0;
3299 for (i=0;i<sizeof(ws_aiflag_map)/sizeof(ws_aiflag_map[0]);i++)
3300 if (ws_aiflag_map[i][0] & winflags) {
3301 unixflags |= ws_aiflag_map[i][1];
3302 winflags &= ~ws_aiflag_map[i][0];
3305 FIXME("Unhandled windows AI_xxx flags %x\n", winflags);
3309 static int convert_aiflag_u2w(int unixflags) {
3310 int i, winflags = 0;
3312 for (i=0;i<sizeof(ws_aiflag_map)/sizeof(ws_aiflag_map[0]);i++)
3313 if (ws_aiflag_map[i][1] & unixflags) {
3314 winflags |= ws_aiflag_map[i][0];
3315 unixflags &= ~ws_aiflag_map[i][1];
3317 if (unixflags) /* will warn usually */
3318 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags);
3322 static int convert_eai_u2w(int unixret) {
3325 for (i=0;ws_eai_map[i][0];i++)
3326 if (ws_eai_map[i][1] == unixret)
3327 return ws_eai_map[i][0];
3331 /***********************************************************************
3332 * getaddrinfo (WS2_32.@)
3334 int WINAPI WS_getaddrinfo(LPCSTR nodename, LPCSTR servname, const struct WS_addrinfo *hints, struct WS_addrinfo **res)
3336 #if HAVE_GETADDRINFO
3337 struct addrinfo *unixaires = NULL;
3339 struct addrinfo unixhints, *punixhints = NULL;
3340 CHAR *node = NULL, *serv = NULL;
3343 if (!(node = strdup_lower(nodename))) return WSA_NOT_ENOUGH_MEMORY;
3346 if (!(serv = strdup_lower(servname))) {
3347 HeapFree(GetProcessHeap(), 0, node);
3348 return WSA_NOT_ENOUGH_MEMORY;
3353 punixhints = &unixhints;
3355 memset(&unixhints, 0, sizeof(unixhints));
3356 punixhints->ai_flags = convert_aiflag_w2u(hints->ai_flags);
3357 if (hints->ai_family == 0) /* wildcard, specific to getaddrinfo() */
3358 punixhints->ai_family = 0;
3360 punixhints->ai_family = convert_af_w2u(hints->ai_family);
3361 if (hints->ai_socktype == 0) /* wildcard, specific to getaddrinfo() */
3362 punixhints->ai_socktype = 0;
3364 punixhints->ai_socktype = convert_socktype_w2u(hints->ai_socktype);
3365 if (hints->ai_protocol == 0) /* wildcard, specific to getaddrinfo() */
3366 punixhints->ai_protocol = 0;
3368 punixhints->ai_protocol = convert_proto_w2u(hints->ai_protocol);
3371 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
3372 result = getaddrinfo(nodename, servname, punixhints, &unixaires);
3374 TRACE("%s, %s %p -> %p %d\n", nodename, servname, hints, res, result);
3376 HeapFree(GetProcessHeap(), 0, node);
3377 HeapFree(GetProcessHeap(), 0, serv);
3380 struct addrinfo *xuai = unixaires;
3381 struct WS_addrinfo **xai = res;
3385 struct WS_addrinfo *ai = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, sizeof(struct WS_addrinfo));
3391 *xai = ai;xai = &ai->ai_next;
3392 ai->ai_flags = convert_aiflag_u2w(xuai->ai_flags);
3393 ai->ai_family = convert_af_u2w(xuai->ai_family);
3394 ai->ai_socktype = convert_socktype_u2w(xuai->ai_socktype);
3395 ai->ai_protocol = convert_proto_u2w(xuai->ai_protocol);
3396 if (xuai->ai_canonname) {
3397 TRACE("canon name - %s\n",debugstr_a(xuai->ai_canonname));
3398 ai->ai_canonname = HeapAlloc(GetProcessHeap(),0,strlen(xuai->ai_canonname)+1);
3399 if (!ai->ai_canonname)
3401 strcpy(ai->ai_canonname,xuai->ai_canonname);
3403 len = xuai->ai_addrlen;
3404 ai->ai_addr = HeapAlloc(GetProcessHeap(),0,len);
3407 ai->ai_addrlen = len;
3409 int winlen = ai->ai_addrlen;
3411 if (!ws_sockaddr_u2ws(xuai->ai_addr, ai->ai_addr, &winlen)) {
3412 ai->ai_addrlen = winlen;
3416 ai->ai_addr = HeapReAlloc(GetProcessHeap(),0,ai->ai_addr,len);
3419 ai->ai_addrlen = len;
3421 xuai = xuai->ai_next;
3423 freeaddrinfo(unixaires);
3425 result = convert_eai_u2w(result);
3430 if (*res) WS_freeaddrinfo(*res);
3431 if (unixaires) freeaddrinfo(unixaires);
3433 return WSA_NOT_ENOUGH_MEMORY;
3435 FIXME("getaddrinfo() failed, not found during buildtime.\n");
3440 /***********************************************************************
3441 * GetAddrInfoW (WS2_32.@)
3443 int WINAPI GetAddrInfoW(LPCWSTR nodename, LPCWSTR servname, const ADDRINFOW *hints, PADDRINFOW *res)
3445 FIXME("empty stub!\n");
3449 int WINAPI WS_getnameinfo(const SOCKADDR *sa, WS_socklen_t salen, PCHAR host,
3450 DWORD hostlen, PCHAR serv, DWORD servlen, INT flags)
3452 #if HAVE_GETNAMEINFO
3454 union generic_unix_sockaddr sa_u;
3457 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa), salen, host, hostlen,
3458 serv, servlen, flags);
3460 size = ws_sockaddr_ws2u(sa, salen, &sa_u);
3463 WSASetLastError(WSAEFAULT);
3464 return WSA_NOT_ENOUGH_MEMORY;
3466 ret = getnameinfo(&sa_u.addr, size, host, hostlen, serv, servlen, convert_aiflag_w2u(flags));
3467 return convert_eai_u2w(ret);
3469 FIXME("getnameinfo() failed, not found during buildtime.\n");
3474 /***********************************************************************
3475 * getservbyport (WS2_32.56)
3477 struct WS_servent* WINAPI WS_getservbyport(int port, const char *proto)
3479 struct WS_servent* retval = NULL;
3480 #ifdef HAVE_GETSERVBYPORT
3481 struct servent* serv;
3482 char *proto_str = NULL;
3484 if (proto && *proto)
3486 if (!(proto_str = strdup_lower(proto))) return NULL;
3488 EnterCriticalSection( &csWSgetXXXbyYYY );
3489 if( (serv = getservbyport(port, proto_str)) != NULL ) {
3490 retval = WS_dup_se(serv);
3492 else SetLastError(WSANO_DATA);
3493 LeaveCriticalSection( &csWSgetXXXbyYYY );
3494 HeapFree( GetProcessHeap(), 0, proto_str );
3496 TRACE("%d (i.e. port %d), %s ret %p\n", port, (int)ntohl(port), debugstr_a(proto), retval);
3501 /***********************************************************************
3502 * gethostname (WS2_32.57)
3504 int WINAPI WS_gethostname(char *name, int namelen)
3506 TRACE("name %p, len %d\n", name, namelen);
3508 if (gethostname(name, namelen) == 0)
3510 TRACE("<- '%s'\n", name);
3513 SetLastError((errno == EINVAL) ? WSAEFAULT : wsaErrno());
3514 TRACE("<- ERROR !\n");
3515 return SOCKET_ERROR;
3519 /* ------------------------------------- Windows sockets extensions -- *
3521 * ------------------------------------------------------------------- */
3523 /***********************************************************************
3524 * WSAEnumNetworkEvents (WS2_32.36)
3526 int WINAPI WSAEnumNetworkEvents(SOCKET s, WSAEVENT hEvent, LPWSANETWORKEVENTS lpEvent)
3530 TRACE("%08lx, hEvent %p, lpEvent %p\n", s, hEvent, lpEvent );
3532 SERVER_START_REQ( get_socket_event )
3534 req->handle = SOCKET2HANDLE(s);
3535 req->service = TRUE;
3536 req->c_event = hEvent;
3537 wine_server_set_reply( req, lpEvent->iErrorCode, sizeof(lpEvent->iErrorCode) );
3538 if (!(ret = wine_server_call(req))) lpEvent->lNetworkEvents = reply->pmask & reply->mask;
3542 SetLastError(WSAEINVAL);
3543 return SOCKET_ERROR;
3546 /***********************************************************************
3547 * WSAEventSelect (WS2_32.39)
3549 int WINAPI WSAEventSelect(SOCKET s, WSAEVENT hEvent, LONG lEvent)
3553 TRACE("%08lx, hEvent %p, event %08x\n", s, hEvent, lEvent);
3555 SERVER_START_REQ( set_socket_event )
3557 req->handle = SOCKET2HANDLE(s);
3559 req->event = hEvent;
3562 ret = wine_server_call( req );
3566 SetLastError(WSAEINVAL);
3567 return SOCKET_ERROR;
3570 /**********************************************************************
3571 * WSAGetOverlappedResult (WS2_32.40)
3573 BOOL WINAPI WSAGetOverlappedResult( SOCKET s, LPWSAOVERLAPPED lpOverlapped,
3574 LPDWORD lpcbTransfer, BOOL fWait,
3579 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
3580 s, lpOverlapped, lpcbTransfer, fWait, lpdwFlags );
3582 if ( lpOverlapped == NULL )
3584 ERR( "Invalid pointer\n" );
3585 WSASetLastError(WSA_INVALID_PARAMETER);
3589 status = lpOverlapped->Internal;
3590 if (status == STATUS_PENDING)
3594 SetLastError( WSA_IO_INCOMPLETE );
3598 if (WaitForSingleObject( lpOverlapped->hEvent ? lpOverlapped->hEvent : SOCKET2HANDLE(s),
3599 INFINITE ) == WAIT_FAILED)
3601 status = lpOverlapped->Internal;
3605 *lpcbTransfer = lpOverlapped->InternalHigh;
3608 *lpdwFlags = lpOverlapped->u.s.Offset;
3610 if (status) SetLastError( RtlNtStatusToDosError(status) );
3615 /***********************************************************************
3616 * WSAAsyncSelect (WS2_32.101)
3618 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, LONG lEvent)
3622 TRACE("%lx, hWnd %p, uMsg %08x, event %08x\n", s, hWnd, uMsg, lEvent);
3624 SERVER_START_REQ( set_socket_event )
3626 req->handle = SOCKET2HANDLE(s);
3631 ret = wine_server_call( req );
3635 SetLastError(WSAEINVAL);
3636 return SOCKET_ERROR;
3639 /***********************************************************************
3640 * WSACreateEvent (WS2_32.31)
3643 WSAEVENT WINAPI WSACreateEvent(void)
3645 /* Create a manual-reset event, with initial state: unsignaled */
3648 return CreateEventW(NULL, TRUE, FALSE, NULL);
3651 /***********************************************************************
3652 * WSACloseEvent (WS2_32.29)
3655 BOOL WINAPI WSACloseEvent(WSAEVENT event)
3657 TRACE ("event=%p\n", event);
3659 return CloseHandle(event);
3662 /***********************************************************************
3663 * WSASocketA (WS2_32.78)
3666 SOCKET WINAPI WSASocketA(int af, int type, int protocol,
3667 LPWSAPROTOCOL_INFOA lpProtocolInfo,
3668 GROUP g, DWORD dwFlags)
3671 WSAPROTOCOL_INFOW info;
3673 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3674 af, type, protocol, lpProtocolInfo, g, dwFlags);
3676 if (!lpProtocolInfo) return WSASocketW(af, type, protocol, NULL, g, dwFlags);
3678 memcpy(&info, lpProtocolInfo, FIELD_OFFSET(WSAPROTOCOL_INFOW, szProtocol));
3679 len = MultiByteToWideChar(CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
3680 info.szProtocol, WSAPROTOCOL_LEN + 1);
3684 WSASetLastError( WSAEINVAL);
3685 return SOCKET_ERROR;
3688 return WSASocketW(af, type, protocol, &info, g, dwFlags);
3691 /***********************************************************************
3692 * WSASocketW (WS2_32.79)
3695 SOCKET WINAPI WSASocketW(int af, int type, int protocol,
3696 LPWSAPROTOCOL_INFOW lpProtocolInfo,
3697 GROUP g, DWORD dwFlags)
3702 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
3703 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
3706 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3707 af, type, protocol, lpProtocolInfo, g, dwFlags );
3709 /* hack for WSADuplicateSocket */
3710 if (lpProtocolInfo && lpProtocolInfo->dwServiceFlags4 == 0xff00ff00) {
3711 ret = lpProtocolInfo->dwCatalogEntryId;
3712 TRACE("\tgot duplicate %04lx\n", ret);
3716 /* check and convert the socket family */
3717 af = convert_af_w2u(af);
3720 FIXME("Unsupported socket family %d!\n", af);
3721 SetLastError(WSAEAFNOSUPPORT);
3722 return INVALID_SOCKET;
3725 /* check the socket type */
3726 type = convert_socktype_w2u(type);
3729 SetLastError(WSAESOCKTNOSUPPORT);
3730 return INVALID_SOCKET;
3733 /* check the protocol type */
3734 if ( protocol < 0 ) /* don't support negative values */
3736 SetLastError(WSAEPROTONOSUPPORT);
3737 return INVALID_SOCKET;
3740 if ( af == AF_UNSPEC) /* did they not specify the address family? */
3744 if (type == SOCK_STREAM) { af = AF_INET; break; }
3746 if (type == SOCK_DGRAM) { af = AF_INET; break; }
3747 default: SetLastError(WSAEPROTOTYPE); return INVALID_SOCKET;
3750 SERVER_START_REQ( create_socket )
3754 req->protocol = protocol;
3755 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
3756 req->attributes = OBJ_INHERIT;
3757 req->flags = dwFlags;
3758 set_error( wine_server_call( req ) );
3759 ret = HANDLE2SOCKET( reply->handle );
3764 TRACE("\tcreated %04lx\n", ret );
3768 if (GetLastError() == WSAEACCES) /* raw socket denied */
3770 if (type == SOCK_RAW)
3771 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, this"
3772 " will fail unless you have special permissions.\n");
3774 MESSAGE("WS_SOCKET: Failed to create socket, this requires"
3775 " special permissions.\n");
3776 SetLastError(WSAESOCKTNOSUPPORT);
3779 WARN("\t\tfailed!\n");
3780 return INVALID_SOCKET;
3783 /***********************************************************************
3784 * WSAJoinLeaf (WS2_32.58)
3787 SOCKET WINAPI WSAJoinLeaf(
3789 const struct WS_sockaddr *addr,
3791 LPWSABUF lpCallerData,
3792 LPWSABUF lpCalleeData,
3798 return INVALID_SOCKET;
3801 /***********************************************************************
3802 * __WSAFDIsSet (WS2_32.151)
3804 int WINAPI __WSAFDIsSet(SOCKET s, WS_fd_set *set)
3806 int i = set->fd_count;
3808 TRACE("(%ld,%p(%i))\n", s, set, i);
3811 if (set->fd_array[i] == s) return 1;
3815 /***********************************************************************
3816 * WSAIsBlocking (WINSOCK.114)
3817 * WSAIsBlocking (WS2_32.114)
3819 BOOL WINAPI WSAIsBlocking(void)
3821 /* By default WinSock should set all its sockets to non-blocking mode
3822 * and poll in PeekMessage loop when processing "blocking" ones. This
3823 * function is supposed to tell if the program is in this loop. Our
3824 * blocking calls are truly blocking so we always return FALSE.
3826 * Note: It is allowed to call this function without prior WSAStartup().
3833 /***********************************************************************
3834 * WSACancelBlockingCall (WINSOCK.113)
3835 * WSACancelBlockingCall (WS2_32.113)
3837 INT WINAPI WSACancelBlockingCall(void)
3843 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x )
3845 FIXME("How was this called?\n");
3850 /***********************************************************************
3851 * WSASetBlockingHook (WS2_32.109)
3853 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
3855 FARPROC prev = blocking_hook;
3856 blocking_hook = lpBlockFunc;
3857 TRACE("hook %p\n", lpBlockFunc);
3862 /***********************************************************************
3863 * WSAUnhookBlockingHook (WS2_32.110)
3865 INT WINAPI WSAUnhookBlockingHook(void)
3867 blocking_hook = (FARPROC)WSA_DefaultBlockingHook;
3872 /* ----------------------------------- end of API stuff */
3874 /* ----------------------------------- helper functions -
3876 * TODO: Merge WS_dup_..() stuff into one function that
3877 * would operate with a generic structure containing internal
3878 * pointers (via a template of some kind).
3881 static int list_size(char** l, int item_size)
3886 j += (item_size) ? item_size : strlen(l[i]) + 1;
3887 j += (i + 1) * sizeof(char*); }
3891 static int list_dup(char** l_src, char** l_to, int item_size)
3896 for (i = 0; l_src[i]; i++) ;
3897 p = (char *)(l_to + i + 1);
3898 for (i = 0; l_src[i]; i++)
3900 int count = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
3901 memcpy(p, l_src[i], count);
3906 return p - (char *)l_to;
3911 /* duplicate hostent entry
3912 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
3913 * Dito for protoent and servent.
3915 static struct WS_hostent *WS_dup_he(const struct hostent* p_he)
3918 struct WS_hostent *p_to;
3920 int size = (sizeof(*p_he) +
3921 strlen(p_he->h_name) + 1 +
3922 list_size(p_he->h_aliases, 0) +
3923 list_size(p_he->h_addr_list, p_he->h_length));
3925 if (!(p_to = check_buffer_he(size))) return NULL;
3926 p_to->h_addrtype = p_he->h_addrtype;
3927 p_to->h_length = p_he->h_length;
3929 p = (char *)(p_to + 1);
3931 strcpy(p, p_he->h_name);
3934 p_to->h_aliases = (char **)p;
3935 p += list_dup(p_he->h_aliases, p_to->h_aliases, 0);
3937 p_to->h_addr_list = (char **)p;
3938 list_dup(p_he->h_addr_list, p_to->h_addr_list, p_he->h_length);
3942 /* ----- protoent */
3944 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe)
3947 struct WS_protoent *p_to;
3949 int size = (sizeof(*p_pe) +
3950 strlen(p_pe->p_name) + 1 +
3951 list_size(p_pe->p_aliases, 0));
3953 if (!(p_to = check_buffer_pe(size))) return NULL;
3954 p_to->p_proto = p_pe->p_proto;
3956 p = (char *)(p_to + 1);
3958 strcpy(p, p_pe->p_name);
3961 p_to->p_aliases = (char **)p;
3962 list_dup(p_pe->p_aliases, p_to->p_aliases, 0);
3968 static struct WS_servent *WS_dup_se(const struct servent* p_se)
3971 struct WS_servent *p_to;
3973 int size = (sizeof(*p_se) +
3974 strlen(p_se->s_proto) + 1 +
3975 strlen(p_se->s_name) + 1 +
3976 list_size(p_se->s_aliases, 0));
3978 if (!(p_to = check_buffer_se(size))) return NULL;
3979 p_to->s_port = p_se->s_port;
3981 p = (char *)(p_to + 1);
3983 strcpy(p, p_se->s_name);
3987 strcpy(p, p_se->s_proto);
3990 p_to->s_aliases = (char **)p;
3991 list_dup(p_se->s_aliases, p_to->s_aliases, 0);
3995 /* ----------------------------------- error handling */
3999 int loc_errno = errno;
4000 WARN("errno %d, (%s).\n", loc_errno, strerror(loc_errno));
4004 case EINTR: return WSAEINTR;
4005 case EBADF: return WSAEBADF;
4007 case EACCES: return WSAEACCES;
4008 case EFAULT: return WSAEFAULT;
4009 case EINVAL: return WSAEINVAL;
4010 case EMFILE: return WSAEMFILE;
4011 case EWOULDBLOCK: return WSAEWOULDBLOCK;
4012 case EINPROGRESS: return WSAEINPROGRESS;
4013 case EALREADY: return WSAEALREADY;
4014 case ENOTSOCK: return WSAENOTSOCK;
4015 case EDESTADDRREQ: return WSAEDESTADDRREQ;
4016 case EMSGSIZE: return WSAEMSGSIZE;
4017 case EPROTOTYPE: return WSAEPROTOTYPE;
4018 case ENOPROTOOPT: return WSAENOPROTOOPT;
4019 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
4020 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
4021 case EOPNOTSUPP: return WSAEOPNOTSUPP;
4022 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
4023 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
4024 case EADDRINUSE: return WSAEADDRINUSE;
4025 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
4026 case ENETDOWN: return WSAENETDOWN;
4027 case ENETUNREACH: return WSAENETUNREACH;
4028 case ENETRESET: return WSAENETRESET;
4029 case ECONNABORTED: return WSAECONNABORTED;
4031 case ECONNRESET: return WSAECONNRESET;
4032 case ENOBUFS: return WSAENOBUFS;
4033 case EISCONN: return WSAEISCONN;
4034 case ENOTCONN: return WSAENOTCONN;
4035 case ESHUTDOWN: return WSAESHUTDOWN;
4036 case ETOOMANYREFS: return WSAETOOMANYREFS;
4037 case ETIMEDOUT: return WSAETIMEDOUT;
4038 case ECONNREFUSED: return WSAECONNREFUSED;
4039 case ELOOP: return WSAELOOP;
4040 case ENAMETOOLONG: return WSAENAMETOOLONG;
4041 case EHOSTDOWN: return WSAEHOSTDOWN;
4042 case EHOSTUNREACH: return WSAEHOSTUNREACH;
4043 case ENOTEMPTY: return WSAENOTEMPTY;
4045 case EPROCLIM: return WSAEPROCLIM;
4048 case EUSERS: return WSAEUSERS;
4051 case EDQUOT: return WSAEDQUOT;
4054 case ESTALE: return WSAESTALE;
4057 case EREMOTE: return WSAEREMOTE;
4060 /* just in case we ever get here and there are no problems */
4063 WARN("Unknown errno %d!\n", loc_errno);
4064 return WSAEOPNOTSUPP;
4068 UINT wsaHerrno(int loc_errno)
4071 WARN("h_errno %d.\n", loc_errno);
4075 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
4076 case TRY_AGAIN: return WSATRY_AGAIN;
4077 case NO_RECOVERY: return WSANO_RECOVERY;
4078 case NO_DATA: return WSANO_DATA;
4079 case ENOBUFS: return WSAENOBUFS;
4083 WARN("Unknown h_errno %d!\n", loc_errno);
4084 return WSAEOPNOTSUPP;
4089 /***********************************************************************
4090 * WSARecv (WS2_32.67)
4092 int WINAPI WSARecv(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
4093 LPDWORD NumberOfBytesReceived, LPDWORD lpFlags,
4094 LPWSAOVERLAPPED lpOverlapped,
4095 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
4097 return WSARecvFrom(s, lpBuffers, dwBufferCount, NumberOfBytesReceived, lpFlags,
4098 NULL, NULL, lpOverlapped, lpCompletionRoutine);
4101 /***********************************************************************
4102 * WSARecvFrom (WS2_32.69)
4104 INT WINAPI WSARecvFrom( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
4105 LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, struct WS_sockaddr *lpFrom,
4106 LPINT lpFromlen, LPWSAOVERLAPPED lpOverlapped,
4107 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
4110 unsigned int i, options;
4112 DWORD timeout_start = GetTickCount();
4113 struct iovec iovec[WS_MSG_MAXIOVLEN];
4115 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
4116 s, lpBuffers, dwBufferCount, *lpFlags, lpFrom,
4117 (lpFromlen ? *lpFromlen : -1),
4118 lpOverlapped, lpCompletionRoutine);
4120 if (dwBufferCount > WS_MSG_MAXIOVLEN)
4122 WSASetLastError( WSAEINVAL );
4123 return SOCKET_ERROR;
4126 fd = get_sock_fd( s, FILE_READ_DATA, &options );
4127 TRACE( "fd=%d, options=%x\n", fd, options );
4129 if (fd == -1) return SOCKET_ERROR;
4131 for (i = 0; i < dwBufferCount; i++)
4133 iovec[i].iov_base = lpBuffers[i].buf;
4134 iovec[i].iov_len = lpBuffers[i].len;
4139 n = WS2_recv( fd, iovec, dwBufferCount, lpFrom, lpFromlen, lpFlags );
4142 if (errno == EINTR) continue;
4143 if (errno != EAGAIN)
4150 *lpNumberOfBytesRecvd = n;
4152 if ((lpOverlapped || lpCompletionRoutine) &&
4153 !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
4155 IO_STATUS_BLOCK *iosb;
4156 struct ws2_async *wsa = HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa) );
4163 release_sock_fd( s, fd );
4165 wsa->hSocket = SOCKET2HANDLE(s);
4166 wsa->flags = *lpFlags;
4168 wsa->addrlen.ptr = lpFromlen;
4169 wsa->user_overlapped = lpOverlapped;
4170 wsa->completion_func = lpCompletionRoutine;
4171 wsa->n_iovecs = dwBufferCount;
4172 memcpy( wsa->iovec, iovec, dwBufferCount * sizeof(*iovec) );
4174 iosb = lpOverlapped ? (IO_STATUS_BLOCK *)lpOverlapped : &wsa->local_iosb;
4178 iosb->u.Status = STATUS_PENDING;
4179 iosb->Information = 0;
4181 SERVER_START_REQ( register_async )
4183 req->handle = wsa->hSocket;
4184 req->type = ASYNC_TYPE_READ;
4185 req->async.callback = WS2_async_recv;
4186 req->async.iosb = iosb;
4187 req->async.arg = wsa;
4188 req->async.apc = ws2_async_apc;
4189 req->async.event = lpCompletionRoutine ? 0 : lpOverlapped->hEvent;
4190 err = wine_server_call( req );
4194 if (err != STATUS_PENDING) HeapFree( GetProcessHeap(), 0, wsa );
4195 WSASetLastError( NtStatusToWSAError( err ));
4196 return SOCKET_ERROR;
4199 iosb->u.Status = STATUS_SUCCESS;
4200 iosb->Information = n;
4201 if (!wsa->completion_func)
4203 SetEvent( lpOverlapped->hEvent );
4204 HeapFree( GetProcessHeap(), 0, wsa );
4206 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)ws2_async_apc,
4207 (ULONG_PTR)wsa, (ULONG_PTR)iosb, 0 );
4208 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4214 if ( _is_blocking(s) )
4217 int timeout = GET_RCVTIMEO(fd);
4220 timeout -= GetTickCount() - timeout_start;
4221 if (timeout < 0) timeout = 0;
4225 pfd.events = POLLIN;
4226 if (*lpFlags & WS_MSG_OOB) pfd.events |= POLLPRI;
4228 if (!timeout || !poll( &pfd, 1, timeout ))
4231 /* a timeout is not fatal */
4232 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4238 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4239 err = WSAEWOULDBLOCK;
4244 TRACE(" -> %i bytes\n", n);
4245 release_sock_fd( s, fd );
4246 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4251 release_sock_fd( s, fd );
4252 WARN(" -> ERROR %d\n", err);
4253 WSASetLastError( err );
4254 return SOCKET_ERROR;
4257 /***********************************************************************
4258 * WSCInstallProvider (WS2_32.88)
4260 INT WINAPI WSCInstallProvider( const LPGUID lpProviderId,
4261 LPCWSTR lpszProviderDllPath,
4262 const LPWSAPROTOCOL_INFOW lpProtocolInfoList,
4263 DWORD dwNumberOfEntries,
4266 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId),
4267 debugstr_w(lpszProviderDllPath), lpProtocolInfoList,
4268 dwNumberOfEntries, lpErrno);
4274 /***********************************************************************
4275 * WSCDeinstallProvider (WS2_32.83)
4277 INT WINAPI WSCDeinstallProvider(LPGUID lpProviderId, LPINT lpErrno)
4279 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId), lpErrno);
4285 /***********************************************************************
4286 * WSAAccept (WS2_32.26)
4288 SOCKET WINAPI WSAAccept( SOCKET s, struct WS_sockaddr *addr, LPINT addrlen,
4289 LPCONDITIONPROC lpfnCondition, DWORD dwCallbackData)
4292 int ret = 0, size = 0;
4293 WSABUF CallerId, CallerData, CalleeId, CalleeData;
4294 /* QOS SQOS, GQOS; */
4297 SOCKADDR src_addr, dst_addr;
4299 TRACE("Socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %d\n",
4300 s, addr, addrlen, lpfnCondition, dwCallbackData);
4303 size = sizeof(src_addr);
4304 cs = WS_accept(s, &src_addr, &size);
4306 if (cs == SOCKET_ERROR) return SOCKET_ERROR;
4308 CallerId.buf = (char *)&src_addr;
4309 CallerId.len = sizeof(src_addr);
4311 CallerData.buf = NULL;
4312 CallerData.len = (ULONG)NULL;
4314 WS_getsockname(cs, &dst_addr, &size);
4316 CalleeId.buf = (char *)&dst_addr;
4317 CalleeId.len = sizeof(dst_addr);
4320 ret = (*lpfnCondition)(&CallerId, &CallerData, NULL, NULL,
4321 &CalleeId, &CalleeData, &g, dwCallbackData);
4326 if (addr && addrlen)
4327 addr = memcpy(addr, &src_addr, (*addrlen > size) ? size : *addrlen );
4330 SERVER_START_REQ( set_socket_deferred )
4332 req->handle = SOCKET2HANDLE(s);
4333 req->deferred = SOCKET2HANDLE(cs);
4334 if ( !wine_server_call_err ( req ) )
4336 SetLastError( WSATRY_AGAIN );
4337 WS_closesocket( cs );
4341 return SOCKET_ERROR;
4344 SetLastError(WSAECONNREFUSED);
4345 return SOCKET_ERROR;
4347 FIXME("Unknown return type from Condition function\n");
4348 SetLastError(WSAENOTSOCK);
4349 return SOCKET_ERROR;
4353 /***********************************************************************
4354 * WSADuplicateSocketA (WS2_32.32)
4356 int WINAPI WSADuplicateSocketA( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOA lpProtocolInfo )
4360 TRACE("(%ld,%x,%p)\n", s, dwProcessId, lpProtocolInfo);
4361 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
4362 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
4363 /* I don't know what the real Windoze does next, this is a hack */
4364 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
4365 * the target use the global duplicate, or we could copy a reference to us to the structure
4366 * and let the target duplicate it from us, but let's do it as simple as possible */
4367 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
4368 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
4369 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
4370 0, FALSE, DUPLICATE_SAME_ACCESS);
4371 CloseHandle(hProcess);
4372 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
4376 /***********************************************************************
4377 * WSADuplicateSocketW (WS2_32.33)
4379 int WINAPI WSADuplicateSocketW( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOW lpProtocolInfo )
4383 TRACE("(%ld,%x,%p)\n", s, dwProcessId, lpProtocolInfo);
4385 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
4386 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
4387 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
4388 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
4389 0, FALSE, DUPLICATE_SAME_ACCESS);
4390 CloseHandle(hProcess);
4391 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
4395 /***********************************************************************
4396 * WSAInstallServiceClassA (WS2_32.48)
4398 int WINAPI WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info)
4400 FIXME("Request to install service %s\n",debugstr_a(info->lpszServiceClassName));
4401 WSASetLastError(WSAEACCES);
4402 return SOCKET_ERROR;
4405 /***********************************************************************
4406 * WSAInstallServiceClassW (WS2_32.49)
4408 int WINAPI WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info)
4410 FIXME("Request to install service %s\n",debugstr_w(info->lpszServiceClassName));
4411 WSASetLastError(WSAEACCES);
4412 return SOCKET_ERROR;
4415 /***********************************************************************
4416 * WSARemoveServiceClass (WS2_32.70)
4418 int WINAPI WSARemoveServiceClass(LPGUID info)
4420 FIXME("Request to remove service %p\n",info);
4421 WSASetLastError(WSATYPE_NOT_FOUND);
4422 return SOCKET_ERROR;
4425 /***********************************************************************
4426 * WSAStringToAddressA (WS2_32.80)
4428 INT WINAPI WSAStringToAddressA(LPSTR AddressString,
4430 LPWSAPROTOCOL_INFOA lpProtocolInfo,
4431 LPSOCKADDR lpAddress,
4432 LPINT lpAddressLength)
4435 LPSTR workBuffer=NULL,ptrPort;
4437 TRACE( "(%s, %x, %p, %p, %p)\n", AddressString, AddressFamily, lpProtocolInfo,
4438 lpAddress, lpAddressLength );
4440 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
4444 WSASetLastError(WSAEINVAL);
4445 return SOCKET_ERROR;
4449 FIXME("ProtocolInfo not implemented.\n");
4451 workBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
4452 strlen(AddressString) + 1);
4455 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4456 return SOCKET_ERROR;
4459 strcpy(workBuffer, AddressString);
4461 switch(AddressFamily)
4465 struct in_addr inetaddr;
4467 /* If lpAddressLength is too small, tell caller the size we need */
4468 if (*lpAddressLength < sizeof(SOCKADDR_IN))
4470 *lpAddressLength = sizeof(SOCKADDR_IN);
4474 memset(lpAddress, 0, sizeof(SOCKADDR_IN));
4476 ((LPSOCKADDR_IN)lpAddress)->sin_family = AF_INET;
4478 ptrPort = strchr(workBuffer, ':');
4481 ((LPSOCKADDR_IN)lpAddress)->sin_port = (WS_u_short)atoi(ptrPort+1);
4486 ((LPSOCKADDR_IN)lpAddress)->sin_port = 0;
4489 if(inet_aton(workBuffer, &inetaddr) > 0)
4491 ((LPSOCKADDR_IN)lpAddress)->sin_addr.WS_s_addr = inetaddr.s_addr;
4502 struct in6_addr inetaddr;
4503 /* If lpAddressLength is too small, tell caller the size we need */
4504 if (*lpAddressLength < sizeof(SOCKADDR_IN6))
4506 *lpAddressLength = sizeof(SOCKADDR_IN6);
4510 #ifdef HAVE_INET_PTON
4511 memset(lpAddress, 0, sizeof(SOCKADDR_IN6));
4513 ((LPSOCKADDR_IN6)lpAddress)->sin6_family = WS_AF_INET6;
4515 /* This one is a bit tricky. An IPv6 address contains colons, so the
4516 * check from IPv4 doesn't work like that. However, IPv6 addresses that
4517 * contain a port are written with braces like [fd12:3456:7890::1]:12345
4518 * so what we will do is to look for ']', check if the next char is a
4519 * colon, and if it is, parse the port as in IPv4. */
4521 ptrPort = strchr(workBuffer, ']');
4522 if(ptrPort && *(++ptrPort) == ':')
4524 ((LPSOCKADDR_IN6)lpAddress)->sin6_port = (WS_u_short)atoi(ptrPort+1);
4529 ((LPSOCKADDR_IN6)lpAddress)->sin6_port = 0;
4532 if(inet_pton(AF_INET6, workBuffer, &inetaddr) > 0)
4534 memcpy(&((LPSOCKADDR_IN6)lpAddress)->sin6_addr, &inetaddr,
4535 sizeof(struct in6_addr));
4539 #endif /* HAVE_INET_PTON */
4545 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
4546 TRACE("Unsupported address family specified: %d.\n", AddressFamily);
4550 HeapFree(GetProcessHeap(), 0, workBuffer);
4553 WSASetLastError(res);
4554 return SOCKET_ERROR;
4557 /***********************************************************************
4558 * WSAStringToAddressW (WS2_32.81)
4560 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
4561 * If this should be the case, it would be required to map these digits
4562 * to Unicode digits (0-9) using FoldString first.
4564 INT WINAPI WSAStringToAddressW(LPWSTR AddressString,
4566 LPWSAPROTOCOL_INFOW lpProtocolInfo,
4567 LPSOCKADDR lpAddress,
4568 LPINT lpAddressLength)
4571 LPSTR workBuffer=NULL;
4572 WSAPROTOCOL_INFOA infoA;
4573 LPWSAPROTOCOL_INFOA lpProtoInfoA = NULL;
4575 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString), AddressFamily, lpProtocolInfo,
4576 lpAddress, lpAddressLength );
4578 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
4580 /* if ProtocolInfo is available - convert to ANSI variant */
4583 lpProtoInfoA = &infoA;
4584 memcpy( lpProtoInfoA, lpProtocolInfo, FIELD_OFFSET( WSAPROTOCOL_INFOA, szProtocol ) );
4586 if (!WideCharToMultiByte( CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
4587 lpProtoInfoA->szProtocol, WSAPROTOCOL_LEN+1, NULL, NULL ))
4589 WSASetLastError( WSAEINVAL);
4590 return SOCKET_ERROR;
4596 /* Translate AddressString to ANSI code page - assumes that only
4597 standard digits 0-9 are used with this API call */
4598 sBuffer = WideCharToMultiByte( CP_ACP, 0, AddressString, -1, NULL, 0, NULL, NULL );
4599 workBuffer = HeapAlloc( GetProcessHeap(), 0, sBuffer );
4603 WideCharToMultiByte( CP_ACP, 0, AddressString, -1, workBuffer, sBuffer, NULL, NULL );
4604 res = WSAStringToAddressA(workBuffer,AddressFamily,lpProtoInfoA,
4605 lpAddress,lpAddressLength);
4606 HeapFree( GetProcessHeap(), 0, workBuffer );
4610 res = WSA_NOT_ENOUGH_MEMORY;
4615 WSASetLastError(res);
4616 return SOCKET_ERROR;
4619 /***********************************************************************
4620 * WSAAddressToStringA (WS2_32.27)
4622 * See WSAAddressToStringW
4624 INT WINAPI WSAAddressToStringA( LPSOCKADDR sockaddr, DWORD len,
4625 LPWSAPROTOCOL_INFOA info, LPSTR string,
4629 CHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4632 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
4634 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
4635 if (!string || !lenstr) return SOCKET_ERROR;
4637 /* sin_family is guaranteed to be the first u_short */
4638 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
4640 sprintf( buffer, "%u.%u.%u.%u:%u",
4641 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
4642 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
4643 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
4644 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
4645 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
4647 p = strchr( buffer, ':' );
4648 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
4650 size = strlen( buffer );
4655 WSASetLastError(WSAEFAULT);
4656 return SOCKET_ERROR;
4659 strcpy( string, buffer );
4663 /***********************************************************************
4664 * WSAAddressToStringW (WS2_32.28)
4666 * Convert a sockaddr address into a readable address string.
4669 * sockaddr [I] Pointer to a sockaddr structure.
4670 * len [I] Size of the sockaddr structure.
4671 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
4672 * string [I/O] Pointer to a buffer to receive the address string.
4673 * lenstr [I/O] Size of the receive buffer in WCHARs.
4677 * Failure: SOCKET_ERROR
4680 * The 'info' parameter is ignored.
4683 * Only supports AF_INET addresses.
4685 INT WINAPI WSAAddressToStringW( LPSOCKADDR sockaddr, DWORD len,
4686 LPWSAPROTOCOL_INFOW info, LPWSTR string,
4690 WCHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4691 static const WCHAR format[] = { '%','u','.','%','u','.','%','u','.','%','u',':','%','u',0 };
4694 TRACE( "(%p, %x, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
4696 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
4697 if (!string || !lenstr) return SOCKET_ERROR;
4699 /* sin_family is guaranteed to be the first u_short */
4700 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
4702 sprintfW( buffer, format,
4703 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
4704 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
4705 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
4706 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
4707 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
4709 p = strchrW( buffer, ':' );
4710 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
4712 size = lstrlenW( buffer );
4717 return SOCKET_ERROR;
4720 lstrcpyW( string, buffer );
4724 /***********************************************************************
4725 * WSAEnumNameSpaceProvidersA (WS2_32.34)
4727 INT WINAPI WSAEnumNameSpaceProvidersA( LPDWORD len, LPWSANAMESPACE_INFOA buffer )
4729 FIXME( "(%p %p) Stub!\n", len, buffer );
4733 /***********************************************************************
4734 * WSAEnumNameSpaceProvidersW (WS2_32.35)
4736 INT WINAPI WSAEnumNameSpaceProvidersW( LPDWORD len, LPWSANAMESPACE_INFOW buffer )
4738 FIXME( "(%p %p) Stub!\n", len, buffer );
4742 /***********************************************************************
4743 * WSAGetQOSByName (WS2_32.41)
4745 BOOL WINAPI WSAGetQOSByName( SOCKET s, LPWSABUF lpQOSName, LPQOS lpQOS )
4747 FIXME( "(0x%04lx %p %p) Stub!\n", s, lpQOSName, lpQOS );
4751 /***********************************************************************
4752 * WSAGetServiceClassInfoA (WS2_32.42)
4754 INT WINAPI WSAGetServiceClassInfoA( LPGUID provider, LPGUID service, LPDWORD len,
4755 LPWSASERVICECLASSINFOA info )
4757 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider), debugstr_guid(service),
4759 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4760 return SOCKET_ERROR;
4763 /***********************************************************************
4764 * WSAGetServiceClassInfoW (WS2_32.43)
4766 INT WINAPI WSAGetServiceClassInfoW( LPGUID provider, LPGUID service, LPDWORD len,
4767 LPWSASERVICECLASSINFOW info )
4769 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider), debugstr_guid(service),
4771 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4772 return SOCKET_ERROR;
4775 /***********************************************************************
4776 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
4778 INT WINAPI WSAGetServiceClassNameByClassIdA( LPGUID class, LPSTR service, LPDWORD len )
4780 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4781 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4782 return SOCKET_ERROR;
4785 /***********************************************************************
4786 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
4788 INT WINAPI WSAGetServiceClassNameByClassIdW( LPGUID class, LPWSTR service, LPDWORD len )
4790 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4791 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4792 return SOCKET_ERROR;
4795 /***********************************************************************
4796 * WSALookupServiceBeginA (WS2_32.59)
4798 INT WINAPI WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions,
4799 DWORD dwControlFlags,
4802 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4804 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4805 return SOCKET_ERROR;
4808 /***********************************************************************
4809 * WSALookupServiceBeginW (WS2_32.60)
4811 INT WINAPI WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions,
4812 DWORD dwControlFlags,
4815 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4817 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4818 return SOCKET_ERROR;
4821 /***********************************************************************
4822 * WSALookupServiceBeginW (WS2_32.61)
4824 INT WINAPI WSALookupServiceEnd( HANDLE lookup )
4826 FIXME("(%p) Stub!\n", lookup );
4830 /***********************************************************************
4831 * WSALookupServiceNextA (WS2_32.62)
4833 INT WINAPI WSALookupServiceNextA( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETA results )
4835 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup, flags, len, results );
4839 /***********************************************************************
4840 * WSALookupServiceNextW (WS2_32.63)
4842 INT WINAPI WSALookupServiceNextW( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETW results )
4844 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup, flags, len, results );
4848 /***********************************************************************
4849 * WSANtohl (WS2_32.64)
4851 INT WINAPI WSANtohl( SOCKET s, WS_u_long netlong, WS_u_long* lphostlong )
4853 TRACE( "(0x%04lx 0x%08x %p)\n", s, netlong, lphostlong );
4855 if (!lphostlong) return WSAEFAULT;
4857 *lphostlong = ntohl( netlong );
4861 /***********************************************************************
4862 * WSANtohs (WS2_32.65)
4864 INT WINAPI WSANtohs( SOCKET s, WS_u_short netshort, WS_u_short* lphostshort )
4866 TRACE( "(0x%04lx 0x%08x %p)\n", s, netshort, lphostshort );
4868 if (!lphostshort) return WSAEFAULT;
4870 *lphostshort = ntohs( netshort );
4874 /***********************************************************************
4875 * WSAProviderConfigChange (WS2_32.66)
4877 INT WINAPI WSAProviderConfigChange( LPHANDLE handle, LPWSAOVERLAPPED overlapped,
4878 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion )
4880 FIXME( "(%p %p %p) Stub!\n", handle, overlapped, completion );
4881 return SOCKET_ERROR;
4884 /***********************************************************************
4885 * WSARecvDisconnect (WS2_32.68)
4887 INT WINAPI WSARecvDisconnect( SOCKET s, LPWSABUF disconnectdata )
4889 TRACE( "(0x%04lx %p)\n", s, disconnectdata );
4891 return WS_shutdown( s, 0 );
4894 /***********************************************************************
4895 * WSASetServiceA (WS2_32.76)
4897 INT WINAPI WSASetServiceA( LPWSAQUERYSETA query, WSAESETSERVICEOP operation, DWORD flags )
4899 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query, operation, flags );
4903 /***********************************************************************
4904 * WSASetServiceW (WS2_32.77)
4906 INT WINAPI WSASetServiceW( LPWSAQUERYSETW query, WSAESETSERVICEOP operation, DWORD flags )
4908 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query, operation, flags );
4912 /***********************************************************************
4913 * WSCEnableNSProvider (WS2_32.84)
4915 INT WINAPI WSCEnableNSProvider( LPGUID provider, BOOL enable )
4917 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider), enable );
4921 /***********************************************************************
4922 * WSCGetProviderPath (WS2_32.86)
4924 INT WINAPI WSCGetProviderPath( LPGUID provider, LPWSTR path, LPINT len, LPINT errcode )
4926 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider), path, len, errcode );
4928 if (!errcode || !provider || !len) return WSAEFAULT;
4930 *errcode = WSAEINVAL;
4931 return SOCKET_ERROR;
4934 /***********************************************************************
4935 * WSCInstallNameSpace (WS2_32.87)
4937 INT WINAPI WSCInstallNameSpace( LPWSTR identifier, LPWSTR path, DWORD namespace,
4938 DWORD version, LPGUID provider )
4940 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier), debugstr_w(path),
4941 namespace, version, debugstr_guid(provider) );
4945 /***********************************************************************
4946 * WSCUnInstallNameSpace (WS2_32.89)
4948 INT WINAPI WSCUnInstallNameSpace( LPGUID lpProviderId )
4950 FIXME("(%p) Stub!\n", lpProviderId);
4954 /***********************************************************************
4955 * WSCWriteProviderOrder (WS2_32.91)
4957 INT WINAPI WSCWriteProviderOrder( LPDWORD entry, DWORD number )
4959 FIXME("(%p 0x%08x) Stub!\n", entry, number);