2 * based on Windows Sockets 1.1 specs
3 * (ftp.microsoft.com:/Advsys/winsock/spec11/WINSOCK.TXT)
5 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
6 * Copyright (C) 2005 Marcus Meissner
7 * Copyright (C) 2006 Kai Blin
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * NOTE: If you make any changes to fix a particular app, make sure
24 * they don't break something else like Netscape or telnet and ftp
25 * clients and servers (www.winsite.com got a lot of those).
29 #include "wine/port.h"
34 #include <sys/types.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 # include <sys/ioctl.h>
41 #ifdef HAVE_SYS_FILIO_H
42 # include <sys/filio.h>
44 #ifdef HAVE_SYS_SOCKIO_H
45 # include <sys/sockio.h>
49 # include <sys/so_ioctl.h>
52 #ifdef HAVE_SYS_PARAM_H
53 # include <sys/param.h>
59 #ifdef HAVE_SYS_WAIT_H
60 # include <sys/wait.h>
65 #ifdef HAVE_SYS_SOCKET_H
66 #include <sys/socket.h>
68 #ifdef HAVE_NETINET_IN_H
69 # include <netinet/in.h>
71 #ifdef HAVE_NETINET_TCP_H
72 # include <netinet/tcp.h>
74 #ifdef HAVE_ARPA_INET_H
75 # include <arpa/inet.h>
80 #ifdef HAVE_SYS_ERRNO_H
81 #include <sys/errno.h>
90 #ifdef HAVE_ARPA_NAMESER_H
91 # include <arpa/nameser.h>
100 #ifdef HAVE_NETIPX_IPX_H
101 # include <netipx/ipx.h>
103 #elif defined(HAVE_LINUX_IPX_H)
104 # ifdef HAVE_ASM_TYPES_H
105 # include <asm/types.h>
107 # include <linux/ipx.h>
114 #ifdef HAVE_SYS_POLL_H
115 # include <sys/poll.h>
117 #ifdef HAVE_SYS_TIME_H
118 # include <sys/time.h>
121 #define NONAMELESSUNION
122 #define NONAMELESSSTRUCT
123 #include "ntstatus.h"
124 #define WIN32_NO_STATUS
129 #include "winerror.h"
131 #include "winsock2.h"
133 #include "ws2tcpip.h"
137 #include "iphlpapi.h"
139 #include "wine/server.h"
140 #include "wine/debug.h"
141 #include "wine/unicode.h"
144 # include "wsnwlink.h"
148 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
149 # define sipx_network sipx_addr.x_net
150 # define sipx_node sipx_addr.x_host.c_host
151 #endif /* __FreeBSD__ */
154 #define INADDR_NONE ~0UL
157 WINE_DEFAULT_DEBUG_CHANNEL(winsock);
159 /* critical section to protect some non-rentrant net function */
160 extern CRITICAL_SECTION csWSgetXXXbyYYY;
162 inline static const char *debugstr_sockaddr( const struct WS_sockaddr *a )
164 if (!a) return "(nil)";
165 return wine_dbg_sprintf("{ family %d, address %s, port %d }",
166 ((const struct sockaddr_in *)a)->sin_family,
167 inet_ntoa(((const struct sockaddr_in *)a)->sin_addr),
168 ntohs(((const struct sockaddr_in *)a)->sin_port));
171 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
172 #define SOCKET2HANDLE(s) ((HANDLE)(s))
173 #define HANDLE2SOCKET(h) ((SOCKET)(h))
175 /****************************************************************
176 * Async IO declarations
177 ****************************************************************/
179 typedef struct ws2_async
182 enum ws2_mode {ws2m_read, ws2m_write, ws2m_sd_read, ws2m_sd_write} mode;
183 LPWSAOVERLAPPED user_overlapped;
184 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func;
187 struct WS_sockaddr *addr;
190 int val; /* for send operations */
191 int *ptr; /* for recv operations */
197 /****************************************************************/
199 /* ----------------------------------- internal data */
201 /* ws_... struct conversion flags */
203 typedef struct /* WSAAsyncSelect() control struct */
205 HANDLE service, event, sock;
211 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
212 #define WS_MAX_UDP_DATAGRAM 1024
213 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x );
215 /* hostent's, servent's and protent's are stored in one buffer per thread,
216 * as documented on MSDN for the functions that return any of the buffers */
217 struct per_thread_data
220 struct WS_hostent *he_buffer;
221 struct WS_servent *se_buffer;
222 struct WS_protoent *pe_buffer;
228 static INT num_startup; /* reference counter */
229 static FARPROC blocking_hook = WSA_DefaultBlockingHook;
231 /* function prototypes */
232 static struct WS_hostent *WS_dup_he(const struct hostent* p_he);
233 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe);
234 static struct WS_servent *WS_dup_se(const struct servent* p_se);
236 int WSAIOCTL_GetInterfaceCount(void);
237 int WSAIOCTL_GetInterfaceName(int intNumber, char *intName);
240 UINT wsaHerrno(int errnr);
242 #define MAP_OPTION(opt) { WS_##opt, opt }
244 static const int ws_sock_map[][2] =
246 MAP_OPTION( SO_DEBUG ),
247 MAP_OPTION( SO_ACCEPTCONN ),
248 MAP_OPTION( SO_REUSEADDR ),
249 MAP_OPTION( SO_KEEPALIVE ),
250 MAP_OPTION( SO_DONTROUTE ),
251 MAP_OPTION( SO_BROADCAST ),
252 MAP_OPTION( SO_LINGER ),
253 MAP_OPTION( SO_OOBINLINE ),
254 MAP_OPTION( SO_SNDBUF ),
255 MAP_OPTION( SO_RCVBUF ),
256 MAP_OPTION( SO_ERROR ),
257 MAP_OPTION( SO_TYPE ),
259 MAP_OPTION( SO_RCVTIMEO ),
262 MAP_OPTION( SO_SNDTIMEO ),
266 static const int ws_tcp_map[][2] =
269 MAP_OPTION( TCP_NODELAY ),
273 static const int ws_ip_map[][2] =
275 MAP_OPTION( IP_MULTICAST_IF ),
276 MAP_OPTION( IP_MULTICAST_TTL ),
277 MAP_OPTION( IP_MULTICAST_LOOP ),
278 MAP_OPTION( IP_ADD_MEMBERSHIP ),
279 MAP_OPTION( IP_DROP_MEMBERSHIP ),
280 MAP_OPTION( IP_OPTIONS ),
282 MAP_OPTION( IP_HDRINCL ),
284 MAP_OPTION( IP_TOS ),
285 MAP_OPTION( IP_TTL ),
288 static const int ws_af_map[][2] =
290 MAP_OPTION( AF_UNSPEC ),
291 MAP_OPTION( AF_INET ),
292 MAP_OPTION( AF_INET6 ),
294 MAP_OPTION( AF_IPX ),
298 static const int ws_socktype_map[][2] =
300 MAP_OPTION( SOCK_DGRAM ),
301 MAP_OPTION( SOCK_STREAM ),
302 MAP_OPTION( SOCK_RAW ),
305 static const int ws_proto_map[][2] =
307 MAP_OPTION( IPPROTO_IP ),
308 MAP_OPTION( IPPROTO_TCP ),
309 MAP_OPTION( IPPROTO_UDP ),
310 MAP_OPTION( IPPROTO_ICMP ),
311 MAP_OPTION( IPPROTO_IGMP ),
312 MAP_OPTION( IPPROTO_RAW ),
315 static const int ws_aiflag_map[][2] =
317 MAP_OPTION( AI_PASSIVE ),
318 MAP_OPTION( AI_CANONNAME ),
319 MAP_OPTION( AI_NUMERICHOST ),
320 /* Linux/UNIX knows a lot more. But Windows only
321 * has 3 as far as I could see. -Marcus
325 static const int ws_eai_map[][2] =
327 MAP_OPTION( EAI_AGAIN ),
328 MAP_OPTION( EAI_BADFLAGS ),
329 MAP_OPTION( EAI_FAIL ),
330 MAP_OPTION( EAI_FAMILY ),
331 MAP_OPTION( EAI_MEMORY ),
332 /* Note: EAI_NODATA is deprecated, but still
333 * used by Windows and Linux... We map the newer
334 * EAI_NONAME to EAI_NODATA for now until Windows
338 MAP_OPTION( EAI_NODATA ),
341 { WS_EAI_NODATA, EAI_NONAME },
344 MAP_OPTION( EAI_SERVICE ),
345 MAP_OPTION( EAI_SOCKTYPE ),
349 inline static DWORD NtStatusToWSAError( const DWORD status )
351 /* We only need to cover the status codes set by server async request handling */
355 case STATUS_SUCCESS: wserr = 0; break;
356 case STATUS_PENDING: wserr = WSA_IO_PENDING; break;
357 case STATUS_INVALID_HANDLE: wserr = WSAENOTSOCK; break; /* WSAEBADF ? */
358 case STATUS_INVALID_PARAMETER: wserr = WSAEINVAL; break;
359 case STATUS_PIPE_DISCONNECTED: wserr = WSAESHUTDOWN; break;
360 case STATUS_CANCELLED: wserr = WSA_OPERATION_ABORTED; break;
361 case STATUS_TIMEOUT: wserr = WSAETIMEDOUT; break;
362 case STATUS_NO_MEMORY: wserr = WSAEFAULT; break;
364 if ( status >= WSABASEERR && status <= WSABASEERR+1004 )
365 /* It is not a NT status code but a winsock error */
369 wserr = RtlNtStatusToDosError( status );
370 FIXME( "Status code %08x converted to DOS error code %x\n", status, wserr );
376 /* set last error code from NT status without mapping WSA errors */
377 inline static unsigned int set_error( unsigned int err )
381 err = NtStatusToWSAError( err );
387 inline static int get_sock_fd( SOCKET s, DWORD access, int *flags )
390 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s), access, &fd, flags ) ))
395 inline static void release_sock_fd( SOCKET s, int fd )
397 wine_server_release_fd( SOCKET2HANDLE(s), fd );
400 static void _enable_event( HANDLE s, unsigned int event,
401 unsigned int sstate, unsigned int cstate )
403 SERVER_START_REQ( enable_socket_event )
407 req->sstate = sstate;
408 req->cstate = cstate;
409 wine_server_call( req );
414 static int _is_blocking(SOCKET s)
417 SERVER_START_REQ( get_socket_event )
419 req->handle = SOCKET2HANDLE(s);
420 req->service = FALSE;
422 wine_server_call( req );
423 ret = (reply->state & FD_WINE_NONBLOCKING) == 0;
429 static unsigned int _get_sock_mask(SOCKET s)
432 SERVER_START_REQ( get_socket_event )
434 req->handle = SOCKET2HANDLE(s);
435 req->service = FALSE;
437 wine_server_call( req );
444 static void _sync_sock_state(SOCKET s)
446 /* do a dummy wineserver request in order to let
447 the wineserver run through its select loop once */
448 (void)_is_blocking(s);
451 static int _get_sock_error(SOCKET s, unsigned int bit)
453 int events[FD_MAX_EVENTS];
455 SERVER_START_REQ( get_socket_event )
457 req->handle = SOCKET2HANDLE(s);
458 req->service = FALSE;
460 wine_server_set_reply( req, events, sizeof(events) );
461 wine_server_call( req );
467 static struct per_thread_data *get_per_thread_data(void)
469 struct per_thread_data * ptb = NtCurrentTeb()->WinSockData;
470 /* lazy initialization */
473 ptb = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ptb) );
474 NtCurrentTeb()->WinSockData = ptb;
479 static void free_per_thread_data(void)
481 struct per_thread_data * ptb = NtCurrentTeb()->WinSockData;
485 /* delete scratch buffers */
486 HeapFree( GetProcessHeap(), 0, ptb->he_buffer );
487 HeapFree( GetProcessHeap(), 0, ptb->se_buffer );
488 HeapFree( GetProcessHeap(), 0, ptb->pe_buffer );
489 ptb->he_buffer = NULL;
490 ptb->se_buffer = NULL;
491 ptb->pe_buffer = NULL;
493 HeapFree( GetProcessHeap(), 0, ptb );
494 NtCurrentTeb()->WinSockData = NULL;
497 /***********************************************************************
498 * DllMain (WS2_32.init)
500 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
502 TRACE("%p 0x%x %p\n", hInstDLL, fdwReason, fImpLoad);
504 case DLL_PROCESS_ATTACH:
506 case DLL_PROCESS_DETACH:
507 free_per_thread_data();
510 case DLL_THREAD_DETACH:
511 free_per_thread_data();
517 /***********************************************************************
520 * Converts socket flags from Windows format.
521 * Return 1 if converted, 0 if not (error).
523 static int convert_sockopt(INT *level, INT *optname)
530 for(i=0; i<sizeof(ws_sock_map)/sizeof(ws_sock_map[0]); i++) {
531 if( ws_sock_map[i][0] == *optname )
533 *optname = ws_sock_map[i][1];
537 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname);
540 *level = IPPROTO_TCP;
541 for(i=0; i<sizeof(ws_tcp_map)/sizeof(ws_tcp_map[0]); i++) {
542 if ( ws_tcp_map[i][0] == *optname )
544 *optname = ws_tcp_map[i][1];
548 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname);
552 for(i=0; i<sizeof(ws_ip_map)/sizeof(ws_ip_map[0]); i++) {
553 if (ws_ip_map[i][0] == *optname )
555 *optname = ws_ip_map[i][1];
559 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname);
561 default: FIXME("Unimplemented or unknown socket level\n");
566 static inline BOOL is_timeout_option( int optname )
569 if (optname == SO_RCVTIMEO) return TRUE;
572 if (optname == SO_SNDTIMEO) return TRUE;
577 /* ----------------------------------- Per-thread info (or per-process?) */
579 static char *strdup_lower(const char *str)
582 char *ret = HeapAlloc( GetProcessHeap(), 0, strlen(str) + 1 );
586 for (i = 0; str[i]; i++) ret[i] = tolower(str[i]);
589 else SetLastError(WSAENOBUFS);
593 inline static int sock_error_p(int s)
595 unsigned int optval, optlen;
597 optlen = sizeof(optval);
598 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
599 if (optval) WARN("\t[%i] error: %d\n", s, optval);
603 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
604 * from an fd and return the value converted to milli seconds
605 * or -1 if there is an infinite time out */
606 static inline int get_rcvsnd_timeo( int fd, int optname)
609 unsigned int len = sizeof(tv);
610 int ret = getsockopt(fd, SOL_SOCKET, optname, &tv, &len);
612 ret = tv.tv_sec * 1000 + tv.tv_usec / 1000;
613 if( ret <= 0 ) /* tv == {0,0} means infinite time out */
618 /* macro wrappers for portability */
620 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
622 #define GET_RCVTIMEO(fd) (-1)
626 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
628 #define GET_SNDTIMEO(fd) (-1)
631 /* utility: given an fd, will block until one of the events occurs */
632 static inline int do_block( int fd, int events, int timeout )
640 while ((ret = poll(&pfd, 1, timeout)) < 0)
651 convert_af_w2u(int windowsaf) {
654 for (i=0;i<sizeof(ws_af_map)/sizeof(ws_af_map[0]);i++)
655 if (ws_af_map[i][0] == windowsaf)
656 return ws_af_map[i][1];
657 FIXME("unhandled Windows address family %d\n", windowsaf);
662 convert_af_u2w(int unixaf) {
665 for (i=0;i<sizeof(ws_af_map)/sizeof(ws_af_map[0]);i++)
666 if (ws_af_map[i][1] == unixaf)
667 return ws_af_map[i][0];
668 FIXME("unhandled UNIX address family %d\n", unixaf);
673 convert_proto_w2u(int windowsproto) {
676 for (i=0;i<sizeof(ws_proto_map)/sizeof(ws_proto_map[0]);i++)
677 if (ws_proto_map[i][0] == windowsproto)
678 return ws_proto_map[i][1];
679 FIXME("unhandled Windows socket protocol %d\n", windowsproto);
684 convert_proto_u2w(int unixproto) {
687 for (i=0;i<sizeof(ws_proto_map)/sizeof(ws_proto_map[0]);i++)
688 if (ws_proto_map[i][1] == unixproto)
689 return ws_proto_map[i][0];
690 FIXME("unhandled UNIX socket protocol %d\n", unixproto);
695 convert_socktype_w2u(int windowssocktype) {
698 for (i=0;i<sizeof(ws_socktype_map)/sizeof(ws_socktype_map[0]);i++)
699 if (ws_socktype_map[i][0] == windowssocktype)
700 return ws_socktype_map[i][1];
701 FIXME("unhandled Windows socket type %d\n", windowssocktype);
706 convert_socktype_u2w(int unixsocktype) {
709 for (i=0;i<sizeof(ws_socktype_map)/sizeof(ws_socktype_map[0]);i++)
710 if (ws_socktype_map[i][1] == unixsocktype)
711 return ws_socktype_map[i][0];
712 FIXME("unhandled UNIX socket type %d\n", unixsocktype);
716 /* ----------------------------------- API -----
718 * Init / cleanup / error checking.
721 /***********************************************************************
722 * WSAStartup (WS2_32.115)
724 int WINAPI WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData)
726 TRACE("verReq=%x\n", wVersionRequested);
728 if (LOBYTE(wVersionRequested) < 1)
729 return WSAVERNOTSUPPORTED;
731 if (!lpWSAData) return WSAEINVAL;
735 /* that's the whole of the negotiation for now */
736 lpWSAData->wVersion = wVersionRequested;
737 /* return winsock information */
738 lpWSAData->wHighVersion = 0x0202;
739 strcpy(lpWSAData->szDescription, "WinSock 2.0" );
740 strcpy(lpWSAData->szSystemStatus, "Running" );
741 lpWSAData->iMaxSockets = WS_MAX_SOCKETS_PER_PROCESS;
742 lpWSAData->iMaxUdpDg = WS_MAX_UDP_DATAGRAM;
743 /* don't do anything with lpWSAData->lpVendorInfo */
744 /* (some apps don't allocate the space for this field) */
746 TRACE("succeeded\n");
751 /***********************************************************************
752 * WSACleanup (WS2_32.116)
754 INT WINAPI WSACleanup(void)
760 SetLastError(WSANOTINITIALISED);
765 /***********************************************************************
766 * WSAGetLastError (WINSOCK.111)
767 * WSAGetLastError (WS2_32.111)
769 INT WINAPI WSAGetLastError(void)
771 return GetLastError();
774 /***********************************************************************
775 * WSASetLastError (WS2_32.112)
777 void WINAPI WSASetLastError(INT iError) {
778 SetLastError(iError);
781 static struct WS_hostent *check_buffer_he(int size)
783 struct per_thread_data * ptb = get_per_thread_data();
786 if (ptb->he_len >= size ) return ptb->he_buffer;
787 HeapFree( GetProcessHeap(), 0, ptb->he_buffer );
789 ptb->he_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->he_len = size) );
790 if (!ptb->he_buffer) SetLastError(WSAENOBUFS);
791 return ptb->he_buffer;
794 static struct WS_servent *check_buffer_se(int size)
796 struct per_thread_data * ptb = get_per_thread_data();
799 if (ptb->se_len >= size ) return ptb->se_buffer;
800 HeapFree( GetProcessHeap(), 0, ptb->se_buffer );
802 ptb->se_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->se_len = size) );
803 if (!ptb->se_buffer) SetLastError(WSAENOBUFS);
804 return ptb->se_buffer;
807 static struct WS_protoent *check_buffer_pe(int size)
809 struct per_thread_data * ptb = get_per_thread_data();
812 if (ptb->pe_len >= size ) return ptb->pe_buffer;
813 HeapFree( GetProcessHeap(), 0, ptb->pe_buffer );
815 ptb->pe_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->pe_len = size) );
816 if (!ptb->pe_buffer) SetLastError(WSAENOBUFS);
817 return ptb->pe_buffer;
820 /* ----------------------------------- i/o APIs */
823 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf)== WS_AF_IPX || (pf) == WS_AF_INET6)
825 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf) == WS_AF_INET6)
829 /**********************************************************************/
831 /* Returns the converted address if successful, NULL if it was too small to
832 * start with. Note that the returned pointer may be the original pointer
833 * if no conversion is necessary.
835 static struct sockaddr* ws_sockaddr_ws2u(const struct WS_sockaddr* wsaddr, int wsaddrlen, unsigned int *uaddrlen)
837 switch (wsaddr->sa_family)
842 const struct WS_sockaddr_ipx* wsipx=(const struct WS_sockaddr_ipx*)wsaddr;
843 struct sockaddr_ipx* uipx;
845 if (wsaddrlen<sizeof(struct WS_sockaddr_ipx))
848 *uaddrlen=sizeof(struct sockaddr_ipx);
849 uipx=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *uaddrlen);
850 uipx->sipx_family=AF_IPX;
851 uipx->sipx_port=wsipx->sa_socket;
852 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
855 memcpy(&uipx->sipx_network,wsipx->sa_netnum,sizeof(uipx->sipx_network)+sizeof(uipx->sipx_node));
856 #ifdef IPX_FRAME_NONE
857 uipx->sipx_type=IPX_FRAME_NONE;
859 return (struct sockaddr*)uipx;
863 struct sockaddr_in6* uin6;
864 const struct WS_sockaddr_in6* win6 = (const struct WS_sockaddr_in6*)wsaddr;
866 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
867 * scope_id, one without. Check:
868 * http://msdn.microsoft.com/library/en-us/winsock/winsock/sockaddr_2.asp
870 if (wsaddrlen >= sizeof(struct WS_sockaddr_in6_old)) {
871 *uaddrlen=sizeof(struct sockaddr_in6);
872 uin6=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *uaddrlen);
873 uin6->sin6_family = AF_INET6;
874 uin6->sin6_port = win6->sin6_port;
875 uin6->sin6_flowinfo = win6->sin6_flowinfo;
876 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
877 if (wsaddrlen >= sizeof(struct WS_sockaddr_in6)) uin6->sin6_scope_id = win6->sin6_scope_id;
879 memcpy(&uin6->sin6_addr,&win6->sin6_addr,16); /* 16 bytes = 128 address bits */
880 return (struct sockaddr*)uin6;
882 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen);
886 struct sockaddr_in* uin;
887 const struct WS_sockaddr_in* win = (const struct WS_sockaddr_in*)wsaddr;
889 if (wsaddrlen<sizeof(struct WS_sockaddr_in))
891 *uaddrlen=sizeof(struct sockaddr_in);
892 uin = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *uaddrlen);
893 uin->sin_family = AF_INET;
894 uin->sin_port = win->sin_port;
895 memcpy(&uin->sin_addr,&win->sin_addr,4); /* 4 bytes = 32 address bits */
896 return (struct sockaddr*)uin;
899 /* Try to determine the needed space by the passed windows sockaddr space */
901 default: /* likely a ipv4 address */
902 case sizeof(struct WS_sockaddr_in):
903 *uaddrlen = sizeof(struct sockaddr_in);
906 case sizeof(struct WS_sockaddr_ipx):
907 *uaddrlen = sizeof(struct sockaddr_ipx);
910 case sizeof(struct WS_sockaddr_in6):
911 case sizeof(struct WS_sockaddr_in6_old):
912 *uaddrlen = sizeof(struct sockaddr_in6);
915 return HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,*uaddrlen);
918 FIXME("Unknown address family %d, return NULL.\n", wsaddr->sa_family);
923 /* Allocates a Unix sockaddr structure to receive the data */
924 static inline struct sockaddr* ws_sockaddr_alloc(const struct WS_sockaddr* wsaddr, int* wsaddrlen, unsigned int* uaddrlen)
928 ERR( "WINE shouldn't pass a NULL wsaddr! Attempting to continue\n" );
930 /* This is not strictly the right thing to do. Hope it works however */
939 *uaddrlen=max(sizeof(struct sockaddr),*wsaddrlen);
941 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *uaddrlen);
944 /* Returns 0 if successful, -1 if the buffer is too small */
945 static int ws_sockaddr_u2ws(const struct sockaddr* uaddr, int uaddrlen, struct WS_sockaddr* wsaddr, int* wsaddrlen)
949 switch(uaddr->sa_family)
954 const struct sockaddr_ipx* uipx=(const struct sockaddr_ipx*)uaddr;
955 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
958 switch (*wsaddrlen) /* how much can we copy? */
963 wsipx->sa_socket=uipx->sipx_port;
967 memcpy(wsipx->sa_nodenum,uipx->sipx_node,sizeof(wsipx->sa_nodenum));
975 memcpy(wsipx->sa_netnum,&uipx->sipx_network,sizeof(wsipx->sa_netnum));
981 wsipx->sa_family=WS_AF_IPX;
992 const struct sockaddr_in6* uin6 = (const struct sockaddr_in6*)uaddr;
993 struct WS_sockaddr_in6_old* win6old = (struct WS_sockaddr_in6_old*)wsaddr;
995 if (*wsaddrlen < sizeof(struct WS_sockaddr_in6_old))
997 win6old->sin6_family = WS_AF_INET6;
998 win6old->sin6_port = uin6->sin6_port;
999 win6old->sin6_flowinfo = uin6->sin6_flowinfo;
1000 memcpy(&win6old->sin6_addr,&uin6->sin6_addr,16); /* 16 bytes = 128 address bits */
1001 *wsaddrlen = sizeof(struct WS_sockaddr_in6_old);
1002 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1003 if (*wsaddrlen >= sizeof(struct WS_sockaddr_in6)) {
1004 struct WS_sockaddr_in6* win6 = (struct WS_sockaddr_in6*)wsaddr;
1005 win6->sin6_scope_id = uin6->sin6_scope_id;
1006 *wsaddrlen = sizeof(struct WS_sockaddr_in6);
1012 const struct sockaddr_in* uin = (const struct sockaddr_in*)uaddr;
1013 struct WS_sockaddr_in* win = (struct WS_sockaddr_in*)wsaddr;
1015 if (*wsaddrlen < sizeof(struct WS_sockaddr_in))
1017 win->sin_family = WS_AF_INET;
1018 win->sin_port = uin->sin_port;
1019 memcpy(&win->sin_addr,&uin->sin_addr,4); /* 4 bytes = 32 address bits */
1020 *wsaddrlen = sizeof(struct WS_sockaddr_in);
1024 memset(wsaddr,0,*wsaddrlen);
1028 FIXME("Unknown address family %d\n", uaddr->sa_family);
1034 /* to be called to free the memory allocated by ws_sockaddr_ws2u or
1037 static inline void ws_sockaddr_free(const struct sockaddr* uaddr, const struct WS_sockaddr* wsaddr)
1039 if (uaddr!=(const struct sockaddr*)wsaddr)
1040 HeapFree(GetProcessHeap(), 0, (void *)uaddr);
1043 /**************************************************************************
1044 * Functions for handling overlapped I/O
1045 **************************************************************************/
1047 static void CALLBACK ws2_async_terminate(ws2_async* as, IO_STATUS_BLOCK* iosb)
1049 TRACE( "as: %p uovl %p ovl %p\n", as, as->user_overlapped, iosb );
1051 if (as->event) NtSetEvent( as->event, NULL );
1053 if (as->completion_func)
1054 as->completion_func( NtStatusToWSAError (iosb->u.Status),
1055 iosb->Information, as->user_overlapped, as->flags );
1056 if ( !as->user_overlapped )
1059 /* FIXME: I don't think this is really used */
1060 if ( as->overlapped->hEvent != INVALID_HANDLE_VALUE )
1061 WSACloseEvent( as->overlapped->hEvent );
1063 HeapFree( GetProcessHeap(), 0, iosb );
1066 HeapFree( GetProcessHeap(), 0, as->iovec );
1067 HeapFree( GetProcessHeap(), 0, as );
1070 /***********************************************************************
1071 * WS2_make_async (INTERNAL)
1074 static void WINAPI WS2_async_recv(void*, IO_STATUS_BLOCK*, ULONG);
1075 static void WINAPI WS2_async_send(void*, IO_STATUS_BLOCK*, ULONG);
1076 static void WINAPI WS2_async_shutdown( void*, IO_STATUS_BLOCK*, ULONG);
1078 inline static struct ws2_async*
1079 WS2_make_async(SOCKET s, enum ws2_mode mode, struct iovec *iovec, DWORD dwBufferCount,
1080 LPDWORD lpFlags, struct WS_sockaddr *addr,
1081 LPINT addrlen, LPWSAOVERLAPPED lpOverlapped,
1082 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
1083 IO_STATUS_BLOCK **piosb)
1085 struct ws2_async *wsa = HeapAlloc( GetProcessHeap(), 0, sizeof( ws2_async ) );
1087 TRACE( "wsa %p\n", wsa );
1092 wsa->hSocket = (HANDLE) s;
1098 wsa->flags = *lpFlags;
1099 wsa->addrlen.ptr = addrlen;
1104 wsa->addrlen.val = *addrlen;
1107 ERR("Invalid async mode: %d\n", mode);
1109 wsa->user_overlapped = lpOverlapped;
1110 wsa->completion_func = lpCompletionRoutine;
1112 wsa->n_iovecs = dwBufferCount;
1118 *piosb = (IO_STATUS_BLOCK*)lpOverlapped;
1119 if (!lpCompletionRoutine)
1121 wsa->event = lpOverlapped->hEvent;
1122 NtResetEvent(wsa->event, NULL);
1125 else if (!(*piosb = HeapAlloc( GetProcessHeap(), 0, sizeof(IO_STATUS_BLOCK))))
1128 (*piosb)->Information = 0;
1129 (*piosb)->u.Status = STATUS_PENDING;
1130 TRACE( "wsa %p, h %p, ev %p, iosb %p, uov %p, cfunc %p\n",
1131 wsa, wsa->hSocket, wsa->event,
1132 *piosb, wsa->user_overlapped, wsa->completion_func );
1138 HeapFree( GetProcessHeap(), 0, wsa );
1142 static ULONG ws2_queue_async(struct ws2_async* wsa, IO_STATUS_BLOCK* iosb)
1144 PIO_APC_ROUTINE apc;
1150 case ws2m_read: apc = WS2_async_recv; type = ASYNC_TYPE_READ; break;
1151 case ws2m_write: apc = WS2_async_send; type = ASYNC_TYPE_WRITE; break;
1152 case ws2m_sd_read: apc = WS2_async_shutdown; type = ASYNC_TYPE_READ; break;
1153 case ws2m_sd_write: apc = WS2_async_shutdown; type = ASYNC_TYPE_WRITE; break;
1154 default: FIXME("Unknown internal mode (%d)\n", wsa->mode); return STATUS_INVALID_PARAMETER;
1157 SERVER_START_REQ( register_async )
1159 req->handle = wsa->hSocket;
1164 req->count = iosb->Information;
1165 status = wine_server_call( req );
1169 if ( status ) iosb->u.Status = status;
1170 if ( iosb->u.Status != STATUS_PENDING )
1172 /* Note: we get here a non zero status when we couldn't queue the async
1173 * in the server. Therefore, we simply terminate the async.
1175 status = iosb->u.Status;
1176 ws2_async_terminate(wsa, iosb);
1179 NtCurrentTeb()->num_async_io++;
1180 return STATUS_SUCCESS;
1183 /***********************************************************************
1184 * WS2_recv (INTERNAL)
1186 * Workhorse for both synchronous and asynchronous recv() operations.
1188 static int WS2_recv( int fd, struct iovec* iov, int count,
1189 struct WS_sockaddr *lpFrom, LPINT lpFromlen,
1194 TRACE( "fd %d, iovec %p, count %d addr %s, len %p, flags %x\n",
1195 fd, iov, count, debugstr_sockaddr(lpFrom), lpFromlen, *lpFlags);
1197 hdr.msg_name = NULL;
1201 hdr.msg_namelen = *lpFromlen;
1202 hdr.msg_name = ws_sockaddr_alloc( lpFrom, lpFromlen, &hdr.msg_namelen );
1203 if ( !hdr.msg_name )
1205 WSASetLastError( WSAEFAULT );
1211 hdr.msg_namelen = 0;
1214 hdr.msg_iovlen = count;
1215 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1216 hdr.msg_accrights = NULL;
1217 hdr.msg_accrightslen = 0;
1219 hdr.msg_control = NULL;
1220 hdr.msg_controllen = 0;
1224 if ( (n = recvmsg(fd, &hdr, *lpFlags)) == -1 )
1226 TRACE( "recvmsg error %d\n", errno);
1231 ws_sockaddr_u2ws( hdr.msg_name, hdr.msg_namelen,
1232 lpFrom, lpFromlen ) != 0 )
1234 /* The from buffer was too small, but we read the data
1235 * anyway. Is that really bad?
1237 WSASetLastError( WSAEFAULT );
1238 WARN( "Address buffer too small\n" );
1243 ws_sockaddr_free( hdr.msg_name, lpFrom );
1244 TRACE("-> %d\n", n);
1248 /***********************************************************************
1249 * WS2_async_recv (INTERNAL)
1251 * Handler for overlapped recv() operations.
1253 static void WINAPI WS2_async_recv( void* ovp, IO_STATUS_BLOCK* iosb, ULONG status)
1255 ws2_async* wsa = (ws2_async*) ovp;
1256 int result, fd, err;
1258 TRACE( "(%p %p %x)\n", wsa, iosb, status );
1262 case STATUS_ALERTED:
1263 if ((iosb->u.Status = wine_server_handle_to_fd( wsa->hSocket, FILE_READ_DATA, &fd, NULL ) ))
1265 ws2_async_terminate(wsa, iosb);
1268 result = WS2_recv( fd, wsa->iovec, wsa->n_iovecs,
1269 wsa->addr, wsa->addrlen.ptr, &wsa->flags );
1270 wine_server_release_fd( wsa->hSocket, fd );
1273 iosb->u.Status = STATUS_SUCCESS;
1274 iosb->Information = result;
1275 TRACE( "received %d bytes\n", result );
1276 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1281 if ( err == WSAEINTR || err == WSAEWOULDBLOCK ) /* errno: EINTR / EAGAIN */
1283 iosb->u.Status = STATUS_PENDING;
1284 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1285 TRACE( "still pending\n" );
1289 iosb->u.Status = err; /* FIXME: is this correct ???? */
1290 TRACE( "Error: %x\n", err );
1293 if (iosb->u.Status == STATUS_PENDING)
1294 ws2_queue_async(wsa, iosb);
1296 ws2_async_terminate(wsa, iosb);
1299 FIXME( "status: %d\n", status );
1300 iosb->u.Status = status;
1301 ws2_async_terminate(wsa, iosb);
1306 /***********************************************************************
1307 * WS2_send (INTERNAL)
1309 * Workhorse for both synchronous and asynchronous send() operations.
1311 static int WS2_send( int fd, struct iovec* iov, int count,
1312 const struct WS_sockaddr *to, INT tolen, DWORD dwFlags )
1316 TRACE( "fd %d, iovec %p, count %d addr %s, len %d, flags %x\n",
1317 fd, iov, count, debugstr_sockaddr(to), tolen, dwFlags);
1319 hdr.msg_name = NULL;
1323 hdr.msg_name = (struct sockaddr*) ws_sockaddr_ws2u( to, tolen, &hdr.msg_namelen );
1324 if ( !hdr.msg_name )
1326 WSASetLastError( WSAEFAULT );
1331 if(to->sa_family == WS_AF_IPX)
1334 struct sockaddr_ipx* uipx = (struct sockaddr_ipx*)hdr.msg_name;
1336 unsigned int len=sizeof(int);
1338 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1339 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1340 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1341 * ipx type in the sockaddr_opx structure with the stored value.
1343 if(getsockopt(fd, SOL_IPX, IPX_TYPE, &val, &len) != -1)
1345 TRACE("ptype: %d (fd:%d)\n", val, fd);
1346 uipx->sipx_type = val;
1354 hdr.msg_namelen = 0;
1357 hdr.msg_iovlen = count;
1358 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1359 hdr.msg_accrights = NULL;
1360 hdr.msg_accrightslen = 0;
1362 hdr.msg_control = NULL;
1363 hdr.msg_controllen = 0;
1367 n = sendmsg(fd, &hdr, dwFlags);
1370 ws_sockaddr_free( hdr.msg_name, to );
1374 /***********************************************************************
1375 * WS2_async_send (INTERNAL)
1377 * Handler for overlapped send() operations.
1379 static void WINAPI WS2_async_send(void* as, IO_STATUS_BLOCK* iosb, ULONG status)
1381 ws2_async* wsa = (ws2_async*) as;
1384 TRACE( "(%p %p %x)\n", wsa, iosb, status );
1388 case STATUS_ALERTED:
1389 if (iosb->u.Status != STATUS_PENDING) FIXME("wrong %08x\n", iosb->u.Status);
1390 if ((iosb->u.Status = wine_server_handle_to_fd( wsa->hSocket, FILE_WRITE_DATA, &fd, NULL ) ))
1392 ws2_async_terminate(wsa, iosb);
1395 /* check to see if the data is ready (non-blocking) */
1396 result = WS2_send( fd, wsa->iovec, wsa->n_iovecs, wsa->addr, wsa->addrlen.val, wsa->flags );
1397 wine_server_release_fd( wsa->hSocket, fd );
1401 iosb->u.Status = STATUS_SUCCESS;
1402 iosb->Information = result;
1403 TRACE( "sent %d bytes\n", result );
1404 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1408 int err = wsaErrno();
1409 if ( err == WSAEINTR )
1411 iosb->u.Status = STATUS_PENDING;
1412 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1413 TRACE( "still pending\n" );
1417 /* We set the status to a winsock error code and check for that
1418 later in NtStatusToWSAError () */
1419 iosb->u.Status = err;
1420 TRACE( "Error: %x\n", err );
1423 if (iosb->u.Status == STATUS_PENDING)
1424 ws2_queue_async(wsa, iosb);
1426 ws2_async_terminate(wsa, iosb);
1429 FIXME( "status: %d\n", status );
1430 iosb->u.Status = status;
1431 ws2_async_terminate(wsa, iosb);
1437 /***********************************************************************
1438 * WS2_async_shutdown (INTERNAL)
1440 * Handler for shutdown() operations on overlapped sockets.
1442 static void WINAPI WS2_async_shutdown( void* as, PIO_STATUS_BLOCK iosb, ULONG status )
1444 ws2_async* wsa = (ws2_async*) as;
1447 TRACE( "async %p %d\n", wsa, wsa->mode );
1450 case STATUS_ALERTED:
1451 if ((iosb->u.Status = wine_server_handle_to_fd( wsa->hSocket, 0, &fd, NULL ) ))
1453 ws2_async_terminate(wsa, iosb);
1456 switch ( wsa->mode )
1458 case ws2m_sd_read: err = shutdown( fd, 0 ); break;
1459 case ws2m_sd_write: err = shutdown( fd, 1 ); break;
1460 default: ERR("invalid mode: %d\n", wsa->mode );
1462 wine_server_release_fd( wsa->hSocket, fd );
1463 iosb->u.Status = err ? wsaErrno() : STATUS_SUCCESS;
1464 if (iosb->u.Status == STATUS_PENDING)
1465 ws2_queue_async(wsa, iosb);
1467 ws2_async_terminate(wsa, iosb);
1470 iosb->u.Status = status;
1471 ws2_async_terminate(wsa, iosb);
1477 /***********************************************************************
1478 * WS2_register_async_shutdown (INTERNAL)
1480 * Helper function for WS_shutdown() on overlapped sockets.
1482 static int WS2_register_async_shutdown( SOCKET s, enum ws2_mode mode )
1484 struct ws2_async *wsa;
1485 int ret, err = WSAEFAULT;
1488 LPWSAOVERLAPPED ovl = HeapAlloc(GetProcessHeap(), 0, sizeof( WSAOVERLAPPED ));
1489 IO_STATUS_BLOCK *iosb = NULL;
1491 TRACE("s %d mode %d\n", s, mode);
1495 ovl->hEvent = WSACreateEvent();
1496 if ( ovl->hEvent == WSA_INVALID_EVENT )
1499 wsa = WS2_make_async( s, mode, NULL, 0, &dwflags, NULL, &len, ovl, NULL, &iosb );
1503 /* Hack: this will cause ws2_async_terminate() to free the overlapped structure */
1504 wsa->user_overlapped = NULL;
1505 if ( (ret = ws2_queue_async( wsa, iosb )) )
1507 err = NtStatusToWSAError( ret );
1510 /* Try immediate completion */
1511 while ( WaitForSingleObjectEx( ovl->hEvent, 0, TRUE ) == STATUS_USER_APC );
1515 WSACloseEvent( ovl->hEvent );
1517 HeapFree( GetProcessHeap(), 0, ovl );
1522 /***********************************************************************
1525 SOCKET WINAPI WS_accept(SOCKET s, struct WS_sockaddr *addr,
1531 TRACE("socket %04x\n", s );
1532 is_blocking = _is_blocking(s);
1537 int fd = get_sock_fd( s, FILE_READ_DATA, NULL );
1538 if (fd == -1) return INVALID_SOCKET;
1540 do_block(fd, POLLIN, -1);
1541 _sync_sock_state(s); /* let wineserver notice connection */
1542 release_sock_fd( s, fd );
1543 /* retrieve any error codes from it */
1544 SetLastError(_get_sock_error(s, FD_ACCEPT_BIT));
1545 /* FIXME: care about the error? */
1547 SERVER_START_REQ( accept_socket )
1549 req->lhandle = SOCKET2HANDLE(s);
1550 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
1551 req->attributes = OBJ_INHERIT;
1552 set_error( wine_server_call( req ) );
1553 as = HANDLE2SOCKET( reply->handle );
1558 if (addr) WS_getpeername(as, addr, addrlen32);
1561 } while (is_blocking);
1562 return INVALID_SOCKET;
1565 /***********************************************************************
1568 int WINAPI WS_bind(SOCKET s, const struct WS_sockaddr* name, int namelen)
1570 int fd = get_sock_fd( s, 0, NULL );
1571 int res = SOCKET_ERROR;
1573 TRACE("socket %04x, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1577 if (!name || (name->sa_family && !SUPPORTED_PF(name->sa_family)))
1579 SetLastError(WSAEAFNOSUPPORT);
1583 const struct sockaddr* uaddr;
1584 unsigned int uaddrlen;
1586 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1589 SetLastError(WSAEFAULT);
1593 if (bind(fd, uaddr, uaddrlen) < 0)
1595 int loc_errno = errno;
1596 WARN("\tfailure - errno = %i\n", errno);
1601 SetLastError(WSAENOTSOCK);
1604 SetLastError(WSAEINVAL);
1607 SetLastError(wsaErrno());
1613 res=0; /* success */
1615 ws_sockaddr_free(uaddr,name);
1618 release_sock_fd( s, fd );
1623 /***********************************************************************
1624 * closesocket (WS2_32.3)
1626 int WINAPI WS_closesocket(SOCKET s)
1628 TRACE("socket %04x\n", s);
1629 if (CloseHandle(SOCKET2HANDLE(s))) return 0;
1630 return SOCKET_ERROR;
1633 /***********************************************************************
1634 * connect (WS2_32.4)
1636 int WINAPI WS_connect(SOCKET s, const struct WS_sockaddr* name, int namelen)
1638 int fd = get_sock_fd( s, FILE_READ_DATA, NULL );
1640 TRACE("socket %04x, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1644 const struct sockaddr* uaddr;
1645 unsigned int uaddrlen;
1647 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1650 SetLastError(WSAEFAULT);
1656 rc=connect(fd, uaddr, uaddrlen);
1657 ws_sockaddr_free(uaddr,name);
1659 goto connect_success;
1662 if (errno == EINPROGRESS)
1664 /* tell wineserver that a connection is in progress */
1665 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1666 FD_CONNECT|FD_READ|FD_WRITE,
1667 FD_WINE_CONNECTED|FD_WINE_LISTENING);
1668 if (_is_blocking(s))
1672 do_block(fd, POLLIN | POLLOUT, -1);
1673 _sync_sock_state(s); /* let wineserver notice connection */
1674 /* retrieve any error codes from it */
1675 result = _get_sock_error(s, FD_CONNECT_BIT);
1677 SetLastError(result);
1680 goto connect_success;
1685 SetLastError(WSAEWOULDBLOCK);
1690 SetLastError(wsaErrno());
1692 release_sock_fd( s, fd );
1694 return SOCKET_ERROR;
1697 release_sock_fd( s, fd );
1698 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1699 FD_WINE_CONNECTED|FD_READ|FD_WRITE,
1700 FD_CONNECT|FD_WINE_LISTENING);
1704 /***********************************************************************
1705 * WSAConnect (WS2_32.30)
1707 int WINAPI WSAConnect( SOCKET s, const struct WS_sockaddr* name, int namelen,
1708 LPWSABUF lpCallerData, LPWSABUF lpCalleeData,
1709 LPQOS lpSQOS, LPQOS lpGQOS )
1711 if ( lpCallerData || lpCalleeData || lpSQOS || lpGQOS )
1712 FIXME("unsupported parameters!\n");
1713 return WS_connect( s, name, namelen );
1717 /***********************************************************************
1718 * getpeername (WS2_32.5)
1720 int WINAPI WS_getpeername(SOCKET s, struct WS_sockaddr *name, int *namelen)
1725 TRACE("socket: %04x, ptr %p, len %08x\n", s, name, *namelen);
1727 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1728 if( (name == NULL) || (namelen == NULL) )
1730 SetLastError( WSAEFAULT );
1731 return SOCKET_ERROR;
1734 fd = get_sock_fd( s, 0, NULL );
1739 struct sockaddr* uaddr;
1740 unsigned int uaddrlen;
1742 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1743 if (getpeername(fd, uaddr, &uaddrlen) != 0)
1745 SetLastError(wsaErrno());
1747 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1749 /* The buffer was too small */
1750 SetLastError(WSAEFAULT);
1756 ws_sockaddr_free(uaddr,name);
1757 release_sock_fd( s, fd );
1762 /***********************************************************************
1763 * getsockname (WS2_32.6)
1765 int WINAPI WS_getsockname(SOCKET s, struct WS_sockaddr *name, int *namelen)
1770 TRACE("socket: %04x, ptr %p, len %8x\n", s, name, *namelen);
1772 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1773 if( (name == NULL) || (namelen == NULL) )
1775 SetLastError( WSAEFAULT );
1776 return SOCKET_ERROR;
1779 fd = get_sock_fd( s, 0, NULL );
1784 struct sockaddr* uaddr;
1785 unsigned int uaddrlen;
1787 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1788 if (getsockname(fd, uaddr, &uaddrlen) != 0)
1790 SetLastError(wsaErrno());
1792 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1794 /* The buffer was too small */
1795 SetLastError(WSAEFAULT);
1801 ws_sockaddr_free(uaddr,name);
1802 release_sock_fd( s, fd );
1807 /***********************************************************************
1808 * getsockopt (WS2_32.7)
1810 INT WINAPI WS_getsockopt(SOCKET s, INT level,
1811 INT optname, char *optval, INT *optlen)
1816 TRACE("socket: %04x, level 0x%x, name 0x%x, ptr %p, len %d\n",
1817 s, level, optname, optval, *optlen);
1825 /* Handle common cases. The special cases are below, sorted
1827 case WS_SO_ACCEPTCONN:
1828 case WS_SO_BROADCAST:
1831 case WS_SO_KEEPALIVE:
1832 case WS_SO_OOBINLINE:
1836 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1837 return SOCKET_ERROR;
1838 convert_sockopt(&level, &optname);
1839 if (getsockopt(fd,(int) level, optname, optval,
1840 (unsigned int *)optlen) != 0 )
1842 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1845 release_sock_fd( s, fd );
1848 case WS_SO_DONTLINGER:
1850 struct linger lingval;
1851 unsigned int len = sizeof(struct linger);
1853 if (!optlen || *optlen < sizeof(BOOL)|| !optval)
1855 SetLastError(WSAEFAULT);
1856 return SOCKET_ERROR;
1858 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1859 return SOCKET_ERROR;
1861 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &lingval, &len) != 0 )
1863 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1868 *(BOOL *)optval = (lingval.l_onoff) ? FALSE : TRUE;
1869 *optlen = sizeof(BOOL);
1872 release_sock_fd( s, fd );
1876 /* As mentioned in setsockopt, Windows ignores this, so we
1877 * always return true here */
1878 case WS_SO_DONTROUTE:
1879 if (!optlen || *optlen < sizeof(BOOL) || !optval)
1881 SetLastError(WSAEFAULT);
1882 return SOCKET_ERROR;
1884 *(BOOL *)optval = TRUE;
1885 *optlen = sizeof(BOOL);
1890 struct linger lingval;
1891 unsigned int len = sizeof(struct linger);
1893 /* struct linger and LINGER have different sizes */
1894 if (!optlen || *optlen < sizeof(LINGER) || !optval)
1896 SetLastError(WSAEFAULT);
1897 return SOCKET_ERROR;
1899 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1900 return SOCKET_ERROR;
1902 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &lingval, &len) != 0 )
1904 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1909 ((LINGER *)optval)->l_onoff = lingval.l_onoff;
1910 ((LINGER *)optval)->l_linger = lingval.l_linger;
1911 *optlen = sizeof(struct linger);
1914 release_sock_fd( s, fd );
1918 case WS_SO_MAX_MSG_SIZE:
1919 if (!optlen || *optlen < sizeof(int) || !optval)
1921 SetLastError(WSAEFAULT);
1922 return SOCKET_ERROR;
1924 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
1925 *(int *)optval = 65507;
1926 *optlen = sizeof(int);
1929 /* SO_OPENTYPE does not require a valid socket handle. */
1930 case WS_SO_OPENTYPE:
1931 if (!optlen || *optlen < sizeof(int) || !optval)
1933 SetLastError(WSAEFAULT);
1934 return SOCKET_ERROR;
1936 *(int *)optval = get_per_thread_data()->opentype;
1937 *optlen = sizeof(int);
1938 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
1942 case WS_SO_RCVTIMEO:
1945 case WS_SO_SNDTIMEO:
1947 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
1950 unsigned int len = sizeof(struct timeval);
1952 if (!optlen || *optlen < sizeof(int)|| !optval)
1954 SetLastError(WSAEFAULT);
1955 return SOCKET_ERROR;
1957 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1958 return SOCKET_ERROR;
1960 convert_sockopt(&level, &optname);
1961 if (getsockopt(fd,(int) level, optname, &tv, &len) != 0 )
1963 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1968 *(int *)optval = tv.tv_sec * 1000 + tv.tv_usec / 1000;
1969 *optlen = sizeof(int);
1972 release_sock_fd( s, fd );
1976 /* As mentioned in setsockopt, the windows style SO_REUSEADDR is
1977 * not possible in Unix, so always return false here. */
1978 case WS_SO_REUSEADDR:
1979 if (!optlen || *optlen < sizeof(int) || !optval)
1981 SetLastError(WSAEFAULT);
1982 return SOCKET_ERROR;
1985 *optlen = sizeof(int);
1989 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname);
1990 SetLastError(WSAENOPROTOOPT);
1991 return SOCKET_ERROR;
1992 } /* end switch(optname) */
1993 }/* end case WS_SOL_SOCKET */
1997 struct WS_sockaddr_ipx addr;
1998 IPX_ADDRESS_DATA *data;
2003 if ((fd = get_sock_fd( s, 0, NULL )) == -1) return SOCKET_ERROR;
2005 if(getsockopt(fd, SOL_IPX, IPX_TYPE, optval, (unsigned int*)optlen) == -1)
2012 socklen_t len=sizeof(struct ipx);
2013 if(getsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, &len) == -1 )
2016 *optval = (int)val.ipx_pt;
2019 TRACE("ptype: %d (fd: %d)\n", *(int*)optval, fd);
2020 release_sock_fd( s, fd );
2025 * On a Win2000 system with one network card there are usually
2026 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
2027 * Using this call you can then retrieve info about this all.
2028 * In case of Linux it is a bit different. Usually you have
2029 * only "one" device active and further it is not possible to
2030 * query things like the linkspeed.
2032 FIXME("IPX_ADDRESS\n");
2033 namelen = sizeof(struct WS_sockaddr_ipx);
2034 memset(&addr, 0, sizeof(struct WS_sockaddr_ipx));
2035 WS_getsockname(s, (struct WS_sockaddr*)&addr, &namelen);
2037 data = (IPX_ADDRESS_DATA*)optval;
2038 memcpy(data->nodenum,&addr.sa_nodenum,sizeof(data->nodenum));
2039 memcpy(data->netnum,&addr.sa_netnum,sizeof(data->netnum));
2040 data->adapternum = 0;
2041 data->wan = FALSE; /* We are not on a wan for now .. */
2042 data->status = FALSE; /* Since we are not on a wan, the wan link isn't up */
2043 data->maxpkt = 1467; /* This value is the default one, at least on Win2k/WinXP */
2044 data->linkspeed = 100000; /* Set the line speed in 100bit/s to 10 Mbit;
2045 * note 1MB = 1000kB in this case */
2048 case IPX_MAX_ADAPTER_NUM:
2049 FIXME("IPX_MAX_ADAPTER_NUM\n");
2050 *(int*)optval = 1; /* As noted under IPX_ADDRESS we have just one card. */
2054 FIXME("IPX optname:%x\n", optname);
2055 return SOCKET_ERROR;
2056 }/* end switch(optname) */
2057 } /* end case NSPROTO_IPX */
2059 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
2060 case WS_IPPROTO_TCP:
2063 case WS_TCP_NODELAY:
2064 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
2065 return SOCKET_ERROR;
2066 convert_sockopt(&level, &optname);
2067 if (getsockopt(fd,(int) level, optname, optval,
2068 (unsigned int *)optlen) != 0 )
2070 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2073 release_sock_fd( s, fd );
2076 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname);
2077 return SOCKET_ERROR;
2082 case WS_IP_ADD_MEMBERSHIP:
2083 case WS_IP_DROP_MEMBERSHIP:
2087 case WS_IP_MULTICAST_IF:
2088 case WS_IP_MULTICAST_LOOP:
2089 case WS_IP_MULTICAST_TTL:
2093 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
2094 return SOCKET_ERROR;
2095 convert_sockopt(&level, &optname);
2096 if (getsockopt(fd,(int) level, optname, optval,
2097 (unsigned int *)optlen) != 0 )
2099 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2102 release_sock_fd( s, fd );
2105 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname);
2106 return SOCKET_ERROR;
2109 FIXME("Unknown level: 0x%08x\n", level);
2110 return SOCKET_ERROR;
2111 } /* end switch(level) */
2114 /***********************************************************************
2118 WS_u_long WINAPI WS_htonl(WS_u_long hostlong)
2120 return htonl(hostlong);
2124 /***********************************************************************
2128 WS_u_short WINAPI WS_htons(WS_u_short hostshort)
2130 return htons(hostshort);
2133 /***********************************************************************
2134 * WSAHtonl (WS2_32.46)
2135 * From MSDN decription of error codes, this function should also
2136 * check if WinSock has been initialized and the socket is a valid
2137 * socket. But why? This function only translates a host byte order
2138 * u_long into a network byte order u_long...
2140 int WINAPI WSAHtonl(SOCKET s, WS_u_long hostlong, WS_u_long *lpnetlong)
2144 *lpnetlong = htonl(hostlong);
2147 WSASetLastError(WSAEFAULT);
2148 return SOCKET_ERROR;
2151 /***********************************************************************
2152 * WSAHtons (WS2_32.47)
2153 * From MSDN decription of error codes, this function should also
2154 * check if WinSock has been initialized and the socket is a valid
2155 * socket. But why? This function only translates a host byte order
2156 * u_short into a network byte order u_short...
2158 int WINAPI WSAHtons(SOCKET s, WS_u_short hostshort, WS_u_short *lpnetshort)
2163 *lpnetshort = htons(hostshort);
2166 WSASetLastError(WSAEFAULT);
2167 return SOCKET_ERROR;
2171 /***********************************************************************
2172 * inet_addr (WINSOCK.10)
2173 * inet_addr (WS2_32.11)
2175 WS_u_long WINAPI WS_inet_addr(const char *cp)
2177 return inet_addr(cp);
2181 /***********************************************************************
2182 * ntohl (WINSOCK.14)
2185 WS_u_long WINAPI WS_ntohl(WS_u_long netlong)
2187 return ntohl(netlong);
2191 /***********************************************************************
2192 * ntohs (WINSOCK.15)
2195 WS_u_short WINAPI WS_ntohs(WS_u_short netshort)
2197 return ntohs(netshort);
2201 /***********************************************************************
2202 * inet_ntoa (WS2_32.12)
2204 char* WINAPI WS_inet_ntoa(struct WS_in_addr in)
2206 /* use "buffer for dummies" here because some applications have a
2207 * propensity to decode addresses in ws_hostent structure without
2208 * saving them first...
2210 static char dbuffer[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
2212 char* s = inet_ntoa(*((struct in_addr*)&in));
2218 SetLastError(wsaErrno());
2222 /**********************************************************************
2223 * WSAIoctl (WS2_32.50)
2226 INT WINAPI WSAIoctl(SOCKET s,
2227 DWORD dwIoControlCode,
2230 LPVOID lpbOutBuffer,
2232 LPDWORD lpcbBytesReturned,
2233 LPWSAOVERLAPPED lpOverlapped,
2234 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
2236 TRACE("%d, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
2237 s, dwIoControlCode, lpvInBuffer, cbInBuffer, lpbOutBuffer,
2238 cbOutBuffer, lpcbBytesReturned, lpOverlapped, lpCompletionRoutine);
2240 switch( dwIoControlCode )
2243 if (cbInBuffer != sizeof(WS_u_long)) {
2244 WSASetLastError(WSAEFAULT);
2245 return SOCKET_ERROR;
2247 return WS_ioctlsocket( s, WS_FIONBIO, lpvInBuffer);
2250 if (cbOutBuffer != sizeof(WS_u_long)) {
2251 WSASetLastError(WSAEFAULT);
2252 return SOCKET_ERROR;
2254 return WS_ioctlsocket( s, WS_FIONREAD, lpbOutBuffer);
2256 case WS_SIO_GET_INTERFACE_LIST:
2258 INTERFACE_INFO* intArray = (INTERFACE_INFO*)lpbOutBuffer;
2259 DWORD size, numInt, apiReturn;
2262 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
2266 WSASetLastError(WSAEFAULT);
2267 return SOCKET_ERROR;
2269 if (!lpcbBytesReturned)
2271 WSASetLastError(WSAEFAULT);
2272 return SOCKET_ERROR;
2275 fd = get_sock_fd( s, 0, NULL );
2276 if (fd == -1) return SOCKET_ERROR;
2278 apiReturn = GetAdaptersInfo(NULL, &size);
2279 if (apiReturn == ERROR_NO_DATA)
2283 else if (apiReturn == ERROR_BUFFER_OVERFLOW)
2285 PIP_ADAPTER_INFO table = HeapAlloc(GetProcessHeap(),0,size);
2289 if (GetAdaptersInfo(table, &size) == NO_ERROR)
2291 PIP_ADAPTER_INFO ptr;
2293 if (size*sizeof(INTERFACE_INFO)/sizeof(IP_ADAPTER_INFO) > cbOutBuffer)
2295 WARN("Buffer too small = %u, cbOutBuffer = %u\n", size, cbOutBuffer);
2296 HeapFree(GetProcessHeap(),0,table);
2297 release_sock_fd( s, fd );
2298 WSASetLastError(WSAEFAULT);
2299 return SOCKET_ERROR;
2301 for (ptr = table, numInt = 0; ptr;
2302 ptr = ptr->Next, intArray++, numInt++)
2304 unsigned int addr, mask, bcast;
2305 struct ifreq ifInfo;
2307 /* Socket Status Flags */
2308 lstrcpynA(ifInfo.ifr_name, ptr->AdapterName, IFNAMSIZ);
2309 if (ioctl(fd, SIOCGIFFLAGS, &ifInfo) < 0)
2311 ERR("Error obtaining status flags for socket!\n");
2312 HeapFree(GetProcessHeap(),0,table);
2313 release_sock_fd( s, fd );
2314 WSASetLastError(WSAEINVAL);
2315 return SOCKET_ERROR;
2319 /* set flags; the values of IFF_* are not the same
2320 under Linux and Windows, therefore must generate
2322 intArray->iiFlags = 0;
2323 if (ifInfo.ifr_flags & IFF_BROADCAST)
2324 intArray->iiFlags |= WS_IFF_BROADCAST;
2325 #ifdef IFF_POINTOPOINT
2326 if (ifInfo.ifr_flags & IFF_POINTOPOINT)
2327 intArray->iiFlags |= WS_IFF_POINTTOPOINT;
2329 if (ifInfo.ifr_flags & IFF_LOOPBACK)
2330 intArray->iiFlags |= WS_IFF_LOOPBACK;
2331 if (ifInfo.ifr_flags & IFF_UP)
2332 intArray->iiFlags |= WS_IFF_UP;
2333 if (ifInfo.ifr_flags & IFF_MULTICAST)
2334 intArray->iiFlags |= WS_IFF_MULTICAST;
2337 addr = inet_addr(ptr->IpAddressList.IpAddress.String);
2338 mask = inet_addr(ptr->IpAddressList.IpMask.String);
2339 bcast = addr | (addr & !mask);
2340 intArray->iiAddress.AddressIn.sin_family = AF_INET;
2341 intArray->iiAddress.AddressIn.sin_port = 0;
2342 intArray->iiAddress.AddressIn.sin_addr.WS_s_addr =
2344 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
2345 intArray->iiNetmask.AddressIn.sin_port = 0;
2346 intArray->iiNetmask.AddressIn.sin_addr.WS_s_addr =
2348 intArray->iiBroadcastAddress.AddressIn.sin_family =
2350 intArray->iiBroadcastAddress.AddressIn.sin_port = 0;
2351 intArray->iiBroadcastAddress.AddressIn.sin_addr.
2357 ERR("Unable to get interface table!\n");
2358 release_sock_fd( s, fd );
2359 HeapFree(GetProcessHeap(),0,table);
2360 WSASetLastError(WSAEINVAL);
2361 return SOCKET_ERROR;
2363 HeapFree(GetProcessHeap(),0,table);
2367 release_sock_fd( s, fd );
2368 WSASetLastError(WSAEINVAL);
2369 return SOCKET_ERROR;
2374 ERR("Unable to get interface table!\n");
2375 release_sock_fd( s, fd );
2376 WSASetLastError(WSAEINVAL);
2377 return SOCKET_ERROR;
2379 /* Calculate the size of the array being returned */
2380 *lpcbBytesReturned = sizeof(INTERFACE_INFO) * numInt;
2381 release_sock_fd( s, fd );
2385 case WS_SIO_ADDRESS_LIST_CHANGE:
2386 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
2387 /* FIXME: error and return code depend on whether socket was created
2388 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
2391 case WS_SIO_ADDRESS_LIST_QUERY:
2392 FIXME("-> SIO_ADDRESS_LIST_QUERY request: stub\n");
2396 FIXME("SIO_FLUSH: stub.\n");
2400 FIXME("unsupported WS_IOCTL cmd (%08x)\n", dwIoControlCode);
2401 WSASetLastError(WSAEOPNOTSUPP);
2402 return SOCKET_ERROR;
2409 /***********************************************************************
2410 * ioctlsocket (WS2_32.10)
2412 int WINAPI WS_ioctlsocket(SOCKET s, long cmd, WS_u_long *argp)
2417 TRACE("socket %04x, cmd %08lx, ptr %p\n", s, cmd, argp);
2426 if( _get_sock_mask(s) )
2428 /* AsyncSelect()'ed sockets are always nonblocking */
2429 if (*argp) return 0;
2430 SetLastError(WSAEINVAL);
2431 return SOCKET_ERROR;
2433 fd = get_sock_fd( s, 0, NULL );
2439 _enable_event(SOCKET2HANDLE(s), 0, FD_WINE_NONBLOCKING, 0);
2440 ret = fcntl( fd, F_SETFL, O_NONBLOCK );
2444 _enable_event(SOCKET2HANDLE(s), 0, 0, FD_WINE_NONBLOCKING);
2445 ret = fcntl( fd, F_SETFL, 0 );
2447 release_sock_fd( s, fd );
2449 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2451 return SOCKET_ERROR;
2458 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
2459 SetLastError(WSAEINVAL);
2460 return SOCKET_ERROR;
2462 case SIOCGIFBRDADDR:
2463 case SIOCGIFNETMASK:
2465 /* These don't need any special handling. They are used by
2466 WsControl, and are here to suppress an unnecessary warning. */
2470 /* Netscape tries hard to use bogus ioctl 0x667e */
2471 /* FIXME: 0x667e above is ('f' << 8) | 126, and is a low word of
2472 * FIONBIO (_IOW('f', 126, u_long)), how that should be handled?
2474 WARN("\tunknown WS_IOCTL cmd (%08lx)\n", cmd);
2478 fd = get_sock_fd( s, 0, NULL );
2481 if( ioctl(fd, newcmd, (char*)argp ) == 0 )
2483 release_sock_fd( s, fd );
2486 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2487 release_sock_fd( s, fd );
2489 return SOCKET_ERROR;
2492 /***********************************************************************
2493 * listen (WS2_32.13)
2495 int WINAPI WS_listen(SOCKET s, int backlog)
2497 int fd = get_sock_fd( s, FILE_READ_DATA, NULL );
2499 TRACE("socket %04x, backlog %d\n", s, backlog);
2502 if (listen(fd, backlog) == 0)
2504 release_sock_fd( s, fd );
2505 _enable_event(SOCKET2HANDLE(s), FD_ACCEPT,
2507 FD_CONNECT|FD_WINE_CONNECTED);
2510 SetLastError(wsaErrno());
2511 release_sock_fd( s, fd );
2513 return SOCKET_ERROR;
2516 /***********************************************************************
2519 int WINAPI WS_recv(SOCKET s, char *buf, int len, int flags)
2521 DWORD n, dwFlags = flags;
2527 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, NULL, NULL, NULL, NULL) == SOCKET_ERROR )
2528 return SOCKET_ERROR;
2533 /***********************************************************************
2534 * recvfrom (WS2_32.17)
2536 int WINAPI WS_recvfrom(SOCKET s, char *buf, INT len, int flags,
2537 struct WS_sockaddr *from, int *fromlen)
2539 DWORD n, dwFlags = flags;
2545 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, from, fromlen, NULL, NULL) == SOCKET_ERROR )
2546 return SOCKET_ERROR;
2551 /* allocate a poll array for the corresponding fd sets */
2552 static struct pollfd *fd_sets_to_poll( const WS_fd_set *readfds, const WS_fd_set *writefds,
2553 const WS_fd_set *exceptfds, int *count_ptr )
2555 int i, j = 0, count = 0;
2558 if (readfds) count += readfds->fd_count;
2559 if (writefds) count += writefds->fd_count;
2560 if (exceptfds) count += exceptfds->fd_count;
2562 if (!count) return NULL;
2563 if (!(fds = HeapAlloc( GetProcessHeap(), 0, count * sizeof(fds[0])))) return NULL;
2565 for (i = 0; i < readfds->fd_count; i++, j++)
2567 fds[j].fd = get_sock_fd( readfds->fd_array[i], FILE_READ_DATA, NULL );
2568 fds[j].events = POLLIN;
2572 for (i = 0; i < writefds->fd_count; i++, j++)
2574 fds[j].fd = get_sock_fd( writefds->fd_array[i], FILE_WRITE_DATA, NULL );
2575 fds[j].events = POLLOUT;
2579 for (i = 0; i < exceptfds->fd_count; i++, j++)
2581 fds[j].fd = get_sock_fd( exceptfds->fd_array[i], 0, NULL );
2582 fds[j].events = POLLHUP;
2588 /* release the file descriptor obtained in fd_sets_to_poll */
2589 /* must be called with the original fd_set arrays, before calling get_poll_results */
2590 static void release_poll_fds( const WS_fd_set *readfds, const WS_fd_set *writefds,
2591 const WS_fd_set *exceptfds, struct pollfd *fds )
2597 for (i = 0; i < readfds->fd_count; i++, j++)
2598 if (fds[j].fd != -1) release_sock_fd( readfds->fd_array[i], fds[j].fd );
2602 for (i = 0; i < writefds->fd_count; i++, j++)
2603 if (fds[j].fd != -1) release_sock_fd( writefds->fd_array[i], fds[j].fd );
2607 for (i = 0; i < exceptfds->fd_count; i++, j++)
2608 if (fds[j].fd != -1)
2610 /* make sure we have a real error before releasing the fd */
2611 if (!sock_error_p( fds[j].fd )) fds[j].revents = 0;
2612 release_sock_fd( exceptfds->fd_array[i], fds[j].fd );
2617 /* map the poll results back into the Windows fd sets */
2618 static int get_poll_results( WS_fd_set *readfds, WS_fd_set *writefds, WS_fd_set *exceptfds,
2619 const struct pollfd *fds )
2621 int i, j = 0, k, total = 0;
2625 for (i = k = 0; i < readfds->fd_count; i++, j++)
2626 if (fds[j].revents) readfds->fd_array[k++] = readfds->fd_array[i];
2627 readfds->fd_count = k;
2632 for (i = k = 0; i < writefds->fd_count; i++, j++)
2633 if (fds[j].revents) writefds->fd_array[k++] = writefds->fd_array[i];
2634 writefds->fd_count = k;
2639 for (i = k = 0; i < exceptfds->fd_count; i++, j++)
2640 if (fds[j].revents) exceptfds->fd_array[k++] = exceptfds->fd_array[i];
2641 exceptfds->fd_count = k;
2648 /***********************************************************************
2649 * select (WS2_32.18)
2651 int WINAPI WS_select(int nfds, WS_fd_set *ws_readfds,
2652 WS_fd_set *ws_writefds, WS_fd_set *ws_exceptfds,
2653 const struct WS_timeval* ws_timeout)
2655 struct pollfd *pollfds;
2656 int count, ret, timeout = -1;
2658 TRACE("read %p, write %p, excp %p timeout %p\n",
2659 ws_readfds, ws_writefds, ws_exceptfds, ws_timeout);
2661 if (!(pollfds = fd_sets_to_poll( ws_readfds, ws_writefds, ws_exceptfds, &count )) && count)
2663 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
2667 if (ws_timeout) timeout = (ws_timeout->tv_sec * 1000) + (ws_timeout->tv_usec + 999) / 1000;
2669 ret = poll( pollfds, count, timeout );
2670 release_poll_fds( ws_readfds, ws_writefds, ws_exceptfds, pollfds );
2672 if (ret == -1) SetLastError(wsaErrno());
2673 else ret = get_poll_results( ws_readfds, ws_writefds, ws_exceptfds, pollfds );
2674 HeapFree( GetProcessHeap(), 0, pollfds );
2679 /***********************************************************************
2682 int WINAPI WS_send(SOCKET s, const char *buf, int len, int flags)
2688 wsabuf.buf = (char*) buf;
2690 if ( WSASendTo( s, &wsabuf, 1, &n, flags, NULL, 0, NULL, NULL) == SOCKET_ERROR )
2691 return SOCKET_ERROR;
2696 /***********************************************************************
2697 * WSASend (WS2_32.72)
2699 INT WINAPI WSASend( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2700 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2701 LPWSAOVERLAPPED lpOverlapped,
2702 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2704 return WSASendTo( s, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags,
2705 NULL, 0, lpOverlapped, lpCompletionRoutine );
2708 /***********************************************************************
2709 * WSASendDisconnect (WS2_32.73)
2711 INT WINAPI WSASendDisconnect( SOCKET s, LPWSABUF lpBuffers )
2713 return WS_shutdown( s, SD_SEND );
2717 /***********************************************************************
2718 * WSASendTo (WS2_32.74)
2720 INT WINAPI WSASendTo( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2721 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2722 const struct WS_sockaddr *to, int tolen,
2723 LPWSAOVERLAPPED lpOverlapped,
2724 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2727 int n, fd, err = WSAENOTSOCK, flags, ret;
2728 struct iovec* iovec;
2729 struct ws2_async *wsa;
2730 IO_STATUS_BLOCK* iosb;
2732 TRACE("socket %04x, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
2733 s, lpBuffers, dwBufferCount, dwFlags,
2734 to, tolen, lpOverlapped, lpCompletionRoutine);
2736 fd = get_sock_fd( s, FILE_WRITE_DATA, &flags );
2737 TRACE( "fd=%d, flags=%x\n", fd, flags );
2739 if ( fd == -1 ) return SOCKET_ERROR;
2741 if (flags & FD_FLAG_SEND_SHUTDOWN)
2743 WSASetLastError( WSAESHUTDOWN );
2747 if ( !lpNumberOfBytesSent )
2753 iovec = HeapAlloc(GetProcessHeap(), 0, dwBufferCount * sizeof(struct iovec) );
2761 for ( i = 0; i < dwBufferCount; i++ )
2763 iovec[i].iov_base = lpBuffers[i].buf;
2764 iovec[i].iov_len = lpBuffers[i].len;
2767 if ( (lpOverlapped || lpCompletionRoutine) && flags & FD_FLAG_OVERLAPPED )
2769 wsa = WS2_make_async( s, ws2m_write, iovec, dwBufferCount,
2770 &dwFlags, (struct WS_sockaddr*) to, &tolen,
2771 lpOverlapped, lpCompletionRoutine, &iosb );
2778 if ( ( ret = ws2_queue_async( wsa, iosb ) ) )
2780 err = NtStatusToWSAError( ret );
2782 if ( !lpOverlapped )
2783 HeapFree( GetProcessHeap(), 0, iosb );
2784 HeapFree( GetProcessHeap(), 0, wsa );
2787 release_sock_fd( s, fd );
2789 /* Try immediate completion */
2792 if ( WSAGetOverlappedResult( s, lpOverlapped,
2793 lpNumberOfBytesSent, FALSE, &dwFlags) )
2796 if ( (err = WSAGetLastError()) != WSA_IO_INCOMPLETE )
2800 WSASetLastError( WSA_IO_PENDING );
2801 return SOCKET_ERROR;
2804 if (_is_blocking(s))
2806 /* FIXME: exceptfds? */
2807 int timeout = GET_SNDTIMEO(fd);
2808 if( !do_block(fd, POLLOUT, timeout)) {
2810 goto err_free; /* msdn says a timeout in send is fatal */
2814 n = WS2_send( fd, iovec, dwBufferCount, to, tolen, dwFlags );
2818 if ( err == WSAEWOULDBLOCK )
2819 _enable_event(SOCKET2HANDLE(s), FD_WRITE, 0, 0);
2823 TRACE(" -> %i bytes\n", n);
2824 *lpNumberOfBytesSent = n;
2826 HeapFree( GetProcessHeap(), 0, iovec );
2827 release_sock_fd( s, fd );
2831 HeapFree( GetProcessHeap(), 0, iovec );
2834 release_sock_fd( s, fd );
2837 WARN(" -> ERROR %d\n", err);
2838 WSASetLastError(err);
2839 return SOCKET_ERROR;
2842 /***********************************************************************
2843 * sendto (WS2_32.20)
2845 int WINAPI WS_sendto(SOCKET s, const char *buf, int len, int flags,
2846 const struct WS_sockaddr *to, int tolen)
2852 wsabuf.buf = (char*) buf;
2854 if ( WSASendTo(s, &wsabuf, 1, &n, flags, to, tolen, NULL, NULL) == SOCKET_ERROR )
2855 return SOCKET_ERROR;
2860 /***********************************************************************
2861 * setsockopt (WS2_32.21)
2863 int WINAPI WS_setsockopt(SOCKET s, int level, int optname,
2864 const char *optval, int optlen)
2868 struct linger linger;
2869 struct timeval tval;
2871 TRACE("socket: %04x, level %d, name %d, ptr %p, len %d\n",
2872 s, level, optname, optval, optlen);
2879 /* Some options need some conversion before they can be sent to
2880 * setsockopt. The conversions are done here, then they will fall though
2881 * to the general case. Special options that are not passed to
2882 * setsockopt follow below that.*/
2884 case WS_SO_DONTLINGER:
2885 linger.l_onoff = *((const int*)optval) ? 0: 1;
2886 linger.l_linger = 0;
2888 optname = SO_LINGER;
2889 optval = (char*)&linger;
2890 optlen = sizeof(struct linger);
2894 linger.l_onoff = ((LINGER*)optval)->l_onoff;
2895 linger.l_linger = ((LINGER*)optval)->l_linger;
2896 /* FIXME: what is documented behavior if SO_LINGER optval
2899 optname = SO_LINGER;
2900 optval = (char*)&linger;
2901 optlen = sizeof(struct linger);
2905 if (*(const int*)optval < 2048)
2907 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval );
2912 /* The options listed here don't need any special handling. Thanks to
2913 * the conversion happening above, options from there will fall through
2915 case WS_SO_ACCEPTCONN:
2916 case WS_SO_BROADCAST:
2918 case WS_SO_KEEPALIVE:
2919 case WS_SO_OOBINLINE:
2922 convert_sockopt(&level, &optname);
2925 /* SO_DEBUG is a privileged operation, ignore it. */
2927 TRACE("Ignoring SO_DEBUG\n");
2930 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
2931 * socket. According to MSDN, this option is silently ignored.*/
2932 case WS_SO_DONTROUTE:
2933 TRACE("Ignoring SO_DONTROUTE\n");
2936 /* Stops two sockets from being bound to the same port. Always happens
2937 * on unix systems, so just drop it. */
2938 case WS_SO_EXCLUSIVEADDRUSE:
2939 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
2942 /* SO_OPENTYPE does not require a valid socket handle. */
2943 case WS_SO_OPENTYPE:
2944 if (!optlen || optlen < sizeof(int) || !optval)
2946 SetLastError(WSAEFAULT);
2947 return SOCKET_ERROR;
2949 get_per_thread_data()->opentype = *(const int *)optval;
2950 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
2953 /* SO_REUSEADDR allows two applications to bind to the same port at at
2954 * same time. There is no direct way to do that in unix. While Wineserver
2955 * might do this, it does not seem useful for now, so just ignore it.*/
2956 case WS_SO_REUSEADDR:
2957 TRACE("Ignoring SO_REUSEADDR, does not translate\n");
2961 case WS_SO_RCVTIMEO:
2964 case WS_SO_SNDTIMEO:
2966 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
2967 if (optval && optlen == sizeof(UINT32)) {
2968 /* WinSock passes miliseconds instead of struct timeval */
2969 tval.tv_usec = (*(const UINT32*)optval % 1000) * 1000;
2970 tval.tv_sec = *(const UINT32*)optval / 1000;
2971 /* min of 500 milisec */
2972 if (tval.tv_sec == 0 && tval.tv_usec < 500000)
2973 tval.tv_usec = 500000;
2974 optlen = sizeof(struct timeval);
2975 optval = (char*)&tval;
2976 } else if (optlen == sizeof(struct timeval)) {
2977 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen);
2979 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen);
2982 convert_sockopt(&level, &optname);
2987 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname);
2988 SetLastError(WSAENOPROTOOPT);
2989 return SOCKET_ERROR;
2991 break; /* case WS_SOL_SOCKET */
2998 fd = get_sock_fd( s, 0, NULL );
2999 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval, fd);
3001 /* We try to set the ipx type on ipx socket level. */
3003 if(setsockopt(fd, SOL_IPX, IPX_TYPE, optval, optlen) == -1)
3005 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
3006 release_sock_fd( s, fd );
3007 return SOCKET_ERROR;
3012 /* Should we retrieve val using a getsockopt call and then
3013 * set the modified one? */
3014 val.ipx_pt = *optval;
3015 setsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, sizeof(struct ipx));
3018 release_sock_fd( s, fd );
3021 case IPX_FILTERPTYPE:
3022 /* Sets the receive filter packet type, at the moment we don't support it */
3023 FIXME("IPX_FILTERPTYPE: %x\n", *optval);
3024 /* Returning 0 is better for now than returning a SOCKET_ERROR */
3028 FIXME("opt_name:%x\n", optname);
3029 return SOCKET_ERROR;
3031 break; /* case NSPROTO_IPX */
3034 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
3035 case WS_IPPROTO_TCP:
3038 case WS_TCP_NODELAY:
3039 convert_sockopt(&level, &optname);
3042 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname);
3043 return SOCKET_ERROR;
3050 case WS_IP_ADD_MEMBERSHIP:
3051 case WS_IP_DROP_MEMBERSHIP:
3055 case WS_IP_MULTICAST_IF:
3056 case WS_IP_MULTICAST_LOOP:
3057 case WS_IP_MULTICAST_TTL:
3061 convert_sockopt(&level, &optname);
3064 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname);
3065 return SOCKET_ERROR;
3070 FIXME("Unknown level: 0x%08x\n", level);
3071 return SOCKET_ERROR;
3072 } /* end switch(level) */
3074 /* avoid endianness issues if argument is a 16-bit int */
3075 if (optval && optlen < sizeof(int))
3077 woptval= *((const INT16 *) optval);
3078 optval= (char*) &woptval;
3081 fd = get_sock_fd( s, 0, NULL );
3082 if (fd == -1) return SOCKET_ERROR;
3084 if (setsockopt(fd, level, optname, optval, optlen) == 0)
3086 release_sock_fd( s, fd );
3089 TRACE("Setting socket error, %d\n", wsaErrno());
3090 SetLastError(wsaErrno());
3091 release_sock_fd( s, fd );
3093 return SOCKET_ERROR;
3096 /***********************************************************************
3097 * shutdown (WS2_32.22)
3099 int WINAPI WS_shutdown(SOCKET s, int how)
3101 int fd, flags, err = WSAENOTSOCK;
3102 unsigned int clear_flags = 0;
3104 fd = get_sock_fd( s, 0, &flags );
3105 TRACE("socket %04x, how %i %x\n", s, how, flags );
3108 return SOCKET_ERROR;
3112 case 0: /* drop receives */
3113 clear_flags |= FD_READ;
3115 case 1: /* drop sends */
3116 clear_flags |= FD_WRITE;
3118 case 2: /* drop all */
3119 clear_flags |= FD_READ|FD_WRITE;
3121 clear_flags |= FD_WINE_LISTENING;
3124 if ( flags & FD_FLAG_OVERLAPPED ) {
3129 err = WS2_register_async_shutdown( s, ws2m_sd_read );
3132 err = WS2_register_async_shutdown( s, ws2m_sd_write );
3136 err = WS2_register_async_shutdown( s, ws2m_sd_read );
3137 if (!err) err = WS2_register_async_shutdown( s, ws2m_sd_write );
3140 if (err) goto error;
3142 else /* non-overlapped mode */
3144 if ( shutdown( fd, how ) )
3151 release_sock_fd( s, fd );
3152 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
3153 if ( how > 1) WSAAsyncSelect( s, 0, 0, 0 );
3157 release_sock_fd( s, fd );
3158 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
3159 WSASetLastError( err );
3160 return SOCKET_ERROR;
3163 /***********************************************************************
3164 * socket (WS2_32.23)
3166 SOCKET WINAPI WS_socket(int af, int type, int protocol)
3168 TRACE("af=%d type=%d protocol=%d\n", af, type, protocol);
3170 return WSASocketA( af, type, protocol, NULL, 0,
3171 get_per_thread_data()->opentype ? 0 : WSA_FLAG_OVERLAPPED );
3175 /***********************************************************************
3176 * gethostbyaddr (WS2_32.51)
3178 struct WS_hostent* WINAPI WS_gethostbyaddr(const char *addr, int len, int type)
3180 struct WS_hostent *retval = NULL;
3181 struct hostent* host;
3183 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3186 struct hostent hostentry;
3189 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
3191 int res = gethostbyaddr_r(addr, len, type,
3192 &hostentry, extrabuf, ebufsize, &host, &locerr);
3193 if( res != ERANGE) break;
3195 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
3197 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
3199 EnterCriticalSection( &csWSgetXXXbyYYY );
3200 host = gethostbyaddr(addr, len, type);
3201 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
3203 if( host != NULL ) retval = WS_dup_he(host);
3204 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3205 HeapFree(GetProcessHeap(),0,extrabuf);
3207 LeaveCriticalSection( &csWSgetXXXbyYYY );
3209 TRACE("ptr %p, len %d, type %d ret %p\n", addr, len, type, retval);
3213 /***********************************************************************
3214 * gethostbyname (WS2_32.52)
3216 struct WS_hostent* WINAPI WS_gethostbyname(const char* name)
3218 struct WS_hostent *retval = NULL;
3219 struct hostent* host;
3220 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3223 struct hostent hostentry;
3224 int locerr = ENOBUFS;
3229 if( gethostname( buf, 100) == -1) {
3230 SetLastError( WSAENOBUFS); /* appropriate ? */
3234 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3236 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
3238 int res = gethostbyname_r(name, &hostentry, extrabuf, ebufsize, &host, &locerr);
3239 if( res != ERANGE) break;
3241 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
3243 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
3245 EnterCriticalSection( &csWSgetXXXbyYYY );
3246 host = gethostbyname(name);
3247 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
3249 if (host) retval = WS_dup_he(host);
3250 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3251 HeapFree(GetProcessHeap(),0,extrabuf);
3253 LeaveCriticalSection( &csWSgetXXXbyYYY );
3255 TRACE( "%s ret %p\n", debugstr_a(name), retval );
3260 /***********************************************************************
3261 * getprotobyname (WS2_32.53)
3263 struct WS_protoent* WINAPI WS_getprotobyname(const char* name)
3265 struct WS_protoent* retval = NULL;
3266 #ifdef HAVE_GETPROTOBYNAME
3267 struct protoent* proto;
3268 EnterCriticalSection( &csWSgetXXXbyYYY );
3269 if( (proto = getprotobyname(name)) != NULL )
3271 retval = WS_dup_pe(proto);
3274 MESSAGE("protocol %s not found; You might want to add "
3275 "this to /etc/protocols\n", debugstr_a(name) );
3276 SetLastError(WSANO_DATA);
3278 LeaveCriticalSection( &csWSgetXXXbyYYY );
3280 TRACE( "%s ret %p\n", debugstr_a(name), retval );
3285 /***********************************************************************
3286 * getprotobynumber (WS2_32.54)
3288 struct WS_protoent* WINAPI WS_getprotobynumber(int number)
3290 struct WS_protoent* retval = NULL;
3291 #ifdef HAVE_GETPROTOBYNUMBER
3292 struct protoent* proto;
3293 EnterCriticalSection( &csWSgetXXXbyYYY );
3294 if( (proto = getprotobynumber(number)) != NULL )
3296 retval = WS_dup_pe(proto);
3299 MESSAGE("protocol number %d not found; You might want to add "
3300 "this to /etc/protocols\n", number );
3301 SetLastError(WSANO_DATA);
3303 LeaveCriticalSection( &csWSgetXXXbyYYY );
3305 TRACE("%i ret %p\n", number, retval);
3310 /***********************************************************************
3311 * getservbyname (WS2_32.55)
3313 struct WS_servent* WINAPI WS_getservbyname(const char *name, const char *proto)
3315 struct WS_servent* retval = NULL;
3316 struct servent* serv;
3318 char *proto_str = NULL;
3320 if (!(name_str = strdup_lower(name))) return NULL;
3322 if (proto && *proto)
3324 if (!(proto_str = strdup_lower(proto)))
3326 HeapFree( GetProcessHeap(), 0, name_str );
3331 EnterCriticalSection( &csWSgetXXXbyYYY );
3332 serv = getservbyname(name_str, proto_str);
3335 retval = WS_dup_se(serv);
3337 else SetLastError(WSANO_DATA);
3338 LeaveCriticalSection( &csWSgetXXXbyYYY );
3339 HeapFree( GetProcessHeap(), 0, proto_str );
3340 HeapFree( GetProcessHeap(), 0, name_str );
3341 TRACE( "%s, %s ret %p\n", debugstr_a(name), debugstr_a(proto), retval );
3345 /***********************************************************************
3346 * freeaddrinfo (WS2_32.@)
3348 void WINAPI WS_freeaddrinfo(struct WS_addrinfo *res)
3351 struct WS_addrinfo *next;
3353 HeapFree(GetProcessHeap(),0,res->ai_canonname);
3354 HeapFree(GetProcessHeap(),0,res->ai_addr);
3355 next = res->ai_next;
3356 HeapFree(GetProcessHeap(),0,res);
3361 /* helper functions for getaddrinfo() */
3362 static int convert_aiflag_w2u(int winflags) {
3363 int i, unixflags = 0;
3365 for (i=0;i<sizeof(ws_aiflag_map)/sizeof(ws_aiflag_map[0]);i++)
3366 if (ws_aiflag_map[i][0] & winflags) {
3367 unixflags |= ws_aiflag_map[i][1];
3368 winflags &= ~ws_aiflag_map[i][0];
3371 FIXME("Unhandled windows AI_xxx flags %x\n", winflags);
3375 static int convert_aiflag_u2w(int unixflags) {
3376 int i, winflags = 0;
3378 for (i=0;i<sizeof(ws_aiflag_map)/sizeof(ws_aiflag_map[0]);i++)
3379 if (ws_aiflag_map[i][1] & unixflags) {
3380 winflags |= ws_aiflag_map[i][0];
3381 unixflags &= ~ws_aiflag_map[i][1];
3383 if (unixflags) /* will warn usually */
3384 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags);
3388 static int convert_eai_u2w(int unixret) {
3391 for (i=0;ws_eai_map[i][0];i++)
3392 if (ws_eai_map[i][1] == unixret)
3393 return ws_eai_map[i][0];
3397 /***********************************************************************
3398 * getaddrinfo (WS2_32.@)
3400 int WINAPI WS_getaddrinfo(LPCSTR nodename, LPCSTR servname, const struct WS_addrinfo *hints, struct WS_addrinfo **res)
3402 #if HAVE_GETADDRINFO
3403 struct addrinfo *unixaires = NULL;
3405 struct addrinfo unixhints, *punixhints = NULL;
3406 CHAR *node = NULL, *serv = NULL;
3409 if (!(node = strdup_lower(nodename))) return WSA_NOT_ENOUGH_MEMORY;
3412 if (!(serv = strdup_lower(servname))) {
3413 HeapFree(GetProcessHeap(), 0, node);
3414 return WSA_NOT_ENOUGH_MEMORY;
3419 punixhints = &unixhints;
3421 memset(&unixhints, 0, sizeof(unixhints));
3422 punixhints->ai_flags = convert_aiflag_w2u(hints->ai_flags);
3423 if (hints->ai_family == 0) /* wildcard, specific to getaddrinfo() */
3424 punixhints->ai_family = 0;
3426 punixhints->ai_family = convert_af_w2u(hints->ai_family);
3427 if (hints->ai_socktype == 0) /* wildcard, specific to getaddrinfo() */
3428 punixhints->ai_socktype = 0;
3430 punixhints->ai_socktype = convert_socktype_w2u(hints->ai_socktype);
3431 if (hints->ai_protocol == 0) /* wildcard, specific to getaddrinfo() */
3432 punixhints->ai_protocol = 0;
3434 punixhints->ai_protocol = convert_proto_w2u(hints->ai_protocol);
3437 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
3438 result = getaddrinfo(nodename, servname, punixhints, &unixaires);
3440 TRACE("%s, %s %p -> %p %d\n", nodename, servname, hints, res, result);
3442 HeapFree(GetProcessHeap(), 0, node);
3443 HeapFree(GetProcessHeap(), 0, serv);
3446 struct addrinfo *xuai = unixaires;
3447 struct WS_addrinfo **xai = res;
3451 struct WS_addrinfo *ai = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, sizeof(struct WS_addrinfo));
3457 *xai = ai;xai = &ai->ai_next;
3458 ai->ai_flags = convert_aiflag_u2w(xuai->ai_flags);
3459 ai->ai_family = convert_af_u2w(xuai->ai_family);
3460 ai->ai_socktype = convert_socktype_u2w(xuai->ai_socktype);
3461 ai->ai_protocol = convert_proto_u2w(xuai->ai_protocol);
3462 if (xuai->ai_canonname) {
3463 TRACE("canon name - %s\n",debugstr_a(xuai->ai_canonname));
3464 ai->ai_canonname = HeapAlloc(GetProcessHeap(),0,strlen(xuai->ai_canonname)+1);
3465 if (!ai->ai_canonname)
3467 strcpy(ai->ai_canonname,xuai->ai_canonname);
3469 len = xuai->ai_addrlen;
3470 ai->ai_addr = HeapAlloc(GetProcessHeap(),0,len);
3473 ai->ai_addrlen = len;
3475 int winlen = ai->ai_addrlen;
3477 if (!ws_sockaddr_u2ws(xuai->ai_addr, xuai->ai_addrlen, ai->ai_addr, &winlen)) {
3478 ai->ai_addrlen = winlen;
3482 ai->ai_addr = HeapReAlloc(GetProcessHeap(),0,ai->ai_addr,len);
3485 ai->ai_addrlen = len;
3487 xuai = xuai->ai_next;
3489 freeaddrinfo(unixaires);
3491 result = convert_eai_u2w(result);
3496 if (*res) WS_freeaddrinfo(*res);
3497 if (unixaires) freeaddrinfo(unixaires);
3499 return WSA_NOT_ENOUGH_MEMORY;
3501 FIXME("getaddrinfo() failed, not found during buildtime.\n");
3506 /***********************************************************************
3507 * GetAddrInfoW (WS2_32.@)
3509 int WINAPI GetAddrInfoW(LPCWSTR nodename, LPCWSTR servname, const ADDRINFOW *hints, PADDRINFOW *res)
3511 FIXME("empty stub!\n");
3515 int WINAPI WS_getnameinfo(const SOCKADDR *sa, WS_socklen_t salen, PCHAR host,
3516 DWORD hostlen, PCHAR serv, DWORD servlen, INT flags)
3518 #if HAVE_GETNAMEINFO
3520 const struct sockaddr* sa_u;
3523 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa), salen, host, hostlen,
3524 serv, servlen, flags);
3526 sa_u = ws_sockaddr_ws2u(sa, salen, &size);
3529 WSASetLastError(WSAEFAULT);
3530 return WSA_NOT_ENOUGH_MEMORY;
3532 ret = getnameinfo(sa_u, size, host, hostlen, serv, servlen, convert_aiflag_w2u(flags));
3534 ws_sockaddr_free(sa_u, sa);
3535 return convert_eai_u2w(ret);
3537 FIXME("getnameinfo() failed, not found during buildtime.\n");
3542 /***********************************************************************
3543 * getservbyport (WS2_32.56)
3545 struct WS_servent* WINAPI WS_getservbyport(int port, const char *proto)
3547 struct WS_servent* retval = NULL;
3548 #ifdef HAVE_GETSERVBYPORT
3549 struct servent* serv;
3550 char *proto_str = NULL;
3552 if (proto && *proto)
3554 if (!(proto_str = strdup_lower(proto))) return NULL;
3556 EnterCriticalSection( &csWSgetXXXbyYYY );
3557 if( (serv = getservbyport(port, proto_str)) != NULL ) {
3558 retval = WS_dup_se(serv);
3560 else SetLastError(WSANO_DATA);
3561 LeaveCriticalSection( &csWSgetXXXbyYYY );
3562 HeapFree( GetProcessHeap(), 0, proto_str );
3564 TRACE("%d (i.e. port %d), %s ret %p\n", port, (int)ntohl(port), debugstr_a(proto), retval);
3569 /***********************************************************************
3570 * gethostname (WS2_32.57)
3572 int WINAPI WS_gethostname(char *name, int namelen)
3574 TRACE("name %p, len %d\n", name, namelen);
3576 if (gethostname(name, namelen) == 0)
3578 TRACE("<- '%s'\n", name);
3581 SetLastError((errno == EINVAL) ? WSAEFAULT : wsaErrno());
3582 TRACE("<- ERROR !\n");
3583 return SOCKET_ERROR;
3587 /* ------------------------------------- Windows sockets extensions -- *
3589 * ------------------------------------------------------------------- */
3591 /***********************************************************************
3592 * WSAEnumNetworkEvents (WS2_32.36)
3594 int WINAPI WSAEnumNetworkEvents(SOCKET s, WSAEVENT hEvent, LPWSANETWORKEVENTS lpEvent)
3598 TRACE("%08x, hEvent %p, lpEvent %p\n", s, hEvent, lpEvent );
3600 SERVER_START_REQ( get_socket_event )
3602 req->handle = SOCKET2HANDLE(s);
3603 req->service = TRUE;
3604 req->c_event = hEvent;
3605 wine_server_set_reply( req, lpEvent->iErrorCode, sizeof(lpEvent->iErrorCode) );
3606 if (!(ret = wine_server_call(req))) lpEvent->lNetworkEvents = reply->pmask & reply->mask;
3610 SetLastError(WSAEINVAL);
3611 return SOCKET_ERROR;
3614 /***********************************************************************
3615 * WSAEventSelect (WS2_32.39)
3617 int WINAPI WSAEventSelect(SOCKET s, WSAEVENT hEvent, long lEvent)
3621 TRACE("%08x, hEvent %p, event %08x\n", s, hEvent, (unsigned)lEvent );
3623 SERVER_START_REQ( set_socket_event )
3625 req->handle = SOCKET2HANDLE(s);
3627 req->event = hEvent;
3630 ret = wine_server_call( req );
3634 SetLastError(WSAEINVAL);
3635 return SOCKET_ERROR;
3638 /**********************************************************************
3639 * WSAGetOverlappedResult (WS2_32.40)
3641 BOOL WINAPI WSAGetOverlappedResult( SOCKET s, LPWSAOVERLAPPED lpOverlapped,
3642 LPDWORD lpcbTransfer, BOOL fWait,
3647 TRACE( "socket %04x ovl %p trans %p, wait %d flags %p\n",
3648 s, lpOverlapped, lpcbTransfer, fWait, lpdwFlags );
3650 if ( lpOverlapped == NULL )
3652 ERR( "Invalid pointer\n" );
3653 WSASetLastError(WSA_INVALID_PARAMETER);
3659 if (lpOverlapped->hEvent)
3660 while ( WaitForSingleObjectEx(lpOverlapped->hEvent,
3661 INFINITE, TRUE) == STATUS_USER_APC );
3662 else /* busy loop */
3663 while ( ((volatile OVERLAPPED*)lpOverlapped)->Internal == STATUS_PENDING )
3667 else if ( lpOverlapped->Internal == STATUS_PENDING )
3669 /* Wait in order to give APCs a chance to run. */
3670 /* This is cheating, so we must set the event again in case of success -
3671 it may be a non-manual reset event. */
3672 while ( (r = WaitForSingleObjectEx(lpOverlapped->hEvent, 0, TRUE)) == STATUS_USER_APC );
3673 if ( r == WAIT_OBJECT_0 && lpOverlapped->hEvent )
3674 NtSetEvent( lpOverlapped->hEvent, NULL );
3678 *lpcbTransfer = lpOverlapped->InternalHigh;
3681 *lpdwFlags = lpOverlapped->u.s.Offset;
3683 switch ( lpOverlapped->Internal )
3685 case STATUS_SUCCESS:
3687 case STATUS_PENDING:
3688 WSASetLastError( WSA_IO_INCOMPLETE );
3689 if (fWait) ERR("PENDING status after waiting!\n");
3692 WSASetLastError( NtStatusToWSAError( lpOverlapped->Internal ));
3698 /***********************************************************************
3699 * WSAAsyncSelect (WS2_32.101)
3701 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, long lEvent)
3705 TRACE("%x, hWnd %p, uMsg %08x, event %08lx\n", s, hWnd, uMsg, lEvent );
3707 SERVER_START_REQ( set_socket_event )
3709 req->handle = SOCKET2HANDLE(s);
3714 ret = wine_server_call( req );
3718 SetLastError(WSAEINVAL);
3719 return SOCKET_ERROR;
3722 /***********************************************************************
3723 * WSACreateEvent (WS2_32.31)
3726 WSAEVENT WINAPI WSACreateEvent(void)
3728 /* Create a manual-reset event, with initial state: unsignaled */
3731 return CreateEventW(NULL, TRUE, FALSE, NULL);
3734 /***********************************************************************
3735 * WSACloseEvent (WS2_32.29)
3738 BOOL WINAPI WSACloseEvent(WSAEVENT event)
3740 TRACE ("event=%p\n", event);
3742 return CloseHandle(event);
3745 /***********************************************************************
3746 * WSASocketA (WS2_32.78)
3749 SOCKET WINAPI WSASocketA(int af, int type, int protocol,
3750 LPWSAPROTOCOL_INFOA lpProtocolInfo,
3751 GROUP g, DWORD dwFlags)
3754 WSAPROTOCOL_INFOW info;
3756 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3757 af, type, protocol, lpProtocolInfo, g, dwFlags);
3759 if (!lpProtocolInfo) return WSASocketW(af, type, protocol, NULL, g, dwFlags);
3761 memcpy(&info, lpProtocolInfo, FIELD_OFFSET(WSAPROTOCOL_INFOW, szProtocol));
3762 len = MultiByteToWideChar(CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
3763 info.szProtocol, WSAPROTOCOL_LEN + 1);
3767 WSASetLastError( WSAEINVAL);
3768 return SOCKET_ERROR;
3771 return WSASocketW(af, type, protocol, &info, g, dwFlags);
3774 /***********************************************************************
3775 * WSASocketW (WS2_32.79)
3778 SOCKET WINAPI WSASocketW(int af, int type, int protocol,
3779 LPWSAPROTOCOL_INFOW lpProtocolInfo,
3780 GROUP g, DWORD dwFlags)
3785 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
3786 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
3789 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3790 af, type, protocol, lpProtocolInfo, g, dwFlags );
3792 /* hack for WSADuplicateSocket */
3793 if (lpProtocolInfo && lpProtocolInfo->dwServiceFlags4 == 0xff00ff00) {
3794 ret = lpProtocolInfo->dwCatalogEntryId;
3795 TRACE("\tgot duplicate %04x\n", ret);
3799 /* check and convert the socket family */
3800 af = convert_af_w2u(af);
3803 FIXME("Unsupported socket family %d!\n", af);
3804 SetLastError(WSAEAFNOSUPPORT);
3805 return INVALID_SOCKET;
3808 /* check the socket type */
3809 type = convert_socktype_w2u(type);
3812 SetLastError(WSAESOCKTNOSUPPORT);
3813 return INVALID_SOCKET;
3816 /* check the protocol type */
3817 if ( protocol < 0 ) /* don't support negative values */
3819 SetLastError(WSAEPROTONOSUPPORT);
3820 return INVALID_SOCKET;
3823 if ( af == AF_UNSPEC) /* did they not specify the address family? */
3827 if (type == SOCK_STREAM) { af = AF_INET; break; }
3829 if (type == SOCK_DGRAM) { af = AF_INET; break; }
3830 default: SetLastError(WSAEPROTOTYPE); return INVALID_SOCKET;
3833 SERVER_START_REQ( create_socket )
3837 req->protocol = protocol;
3838 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
3839 req->attributes = OBJ_INHERIT;
3840 req->flags = dwFlags;
3841 set_error( wine_server_call( req ) );
3842 ret = HANDLE2SOCKET( reply->handle );
3847 TRACE("\tcreated %04x\n", ret );
3851 if (GetLastError() == WSAEACCES) /* raw socket denied */
3853 if (type == SOCK_RAW)
3854 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, will fail unless running as root\n");
3856 MESSAGE("WS_SOCKET: not enough privileges to create socket, try running as root\n");
3857 SetLastError(WSAESOCKTNOSUPPORT);
3860 WARN("\t\tfailed!\n");
3861 return INVALID_SOCKET;
3864 /***********************************************************************
3865 * WSAJoinLeaf (WS2_32.58)
3868 SOCKET WINAPI WSAJoinLeaf(
3870 const struct WS_sockaddr *addr,
3872 LPWSABUF lpCallerData,
3873 LPWSABUF lpCalleeData,
3879 return INVALID_SOCKET;
3882 /***********************************************************************
3883 * __WSAFDIsSet (WS2_32.151)
3885 int WINAPI __WSAFDIsSet(SOCKET s, WS_fd_set *set)
3887 int i = set->fd_count;
3889 TRACE("(%d,%p(%i))\n", s, set, i);
3892 if (set->fd_array[i] == s) return 1;
3896 /***********************************************************************
3897 * WSAIsBlocking (WINSOCK.114)
3898 * WSAIsBlocking (WS2_32.114)
3900 BOOL WINAPI WSAIsBlocking(void)
3902 /* By default WinSock should set all its sockets to non-blocking mode
3903 * and poll in PeekMessage loop when processing "blocking" ones. This
3904 * function is supposed to tell if the program is in this loop. Our
3905 * blocking calls are truly blocking so we always return FALSE.
3907 * Note: It is allowed to call this function without prior WSAStartup().
3914 /***********************************************************************
3915 * WSACancelBlockingCall (WINSOCK.113)
3916 * WSACancelBlockingCall (WS2_32.113)
3918 INT WINAPI WSACancelBlockingCall(void)
3924 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x )
3926 FIXME("How was this called?\n");
3931 /***********************************************************************
3932 * WSASetBlockingHook (WS2_32.109)
3934 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
3936 FARPROC prev = blocking_hook;
3937 blocking_hook = lpBlockFunc;
3938 TRACE("hook %p\n", lpBlockFunc);
3943 /***********************************************************************
3944 * WSAUnhookBlockingHook (WS2_32.110)
3946 INT WINAPI WSAUnhookBlockingHook(void)
3948 blocking_hook = WSA_DefaultBlockingHook;
3953 /* ----------------------------------- end of API stuff */
3955 /* ----------------------------------- helper functions -
3957 * TODO: Merge WS_dup_..() stuff into one function that
3958 * would operate with a generic structure containing internal
3959 * pointers (via a template of some kind).
3962 static int list_size(char** l, int item_size)
3967 j += (item_size) ? item_size : strlen(l[i]) + 1;
3968 j += (i + 1) * sizeof(char*); }
3972 static int list_dup(char** l_src, char** l_to, int item_size)
3977 for (i = 0; l_src[i]; i++) ;
3978 p = (char *)(l_to + i + 1);
3979 for (i = 0; l_src[i]; i++)
3981 int count = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
3982 memcpy(p, l_src[i], count);
3987 return p - (char *)l_to;
3992 /* duplicate hostent entry
3993 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
3994 * Dito for protoent and servent.
3996 static struct WS_hostent *WS_dup_he(const struct hostent* p_he)
3999 struct WS_hostent *p_to;
4001 int size = (sizeof(*p_he) +
4002 strlen(p_he->h_name) + 1 +
4003 list_size(p_he->h_aliases, 0) +
4004 list_size(p_he->h_addr_list, p_he->h_length));
4006 if (!(p_to = check_buffer_he(size))) return NULL;
4007 p_to->h_addrtype = p_he->h_addrtype;
4008 p_to->h_length = p_he->h_length;
4010 p = (char *)(p_to + 1);
4012 strcpy(p, p_he->h_name);
4015 p_to->h_aliases = (char **)p;
4016 p += list_dup(p_he->h_aliases, p_to->h_aliases, 0);
4018 p_to->h_addr_list = (char **)p;
4019 list_dup(p_he->h_addr_list, p_to->h_addr_list, p_he->h_length);
4023 /* ----- protoent */
4025 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe)
4028 struct WS_protoent *p_to;
4030 int size = (sizeof(*p_pe) +
4031 strlen(p_pe->p_name) + 1 +
4032 list_size(p_pe->p_aliases, 0));
4034 if (!(p_to = check_buffer_pe(size))) return NULL;
4035 p_to->p_proto = p_pe->p_proto;
4037 p = (char *)(p_to + 1);
4039 strcpy(p, p_pe->p_name);
4042 p_to->p_aliases = (char **)p;
4043 list_dup(p_pe->p_aliases, p_to->p_aliases, 0);
4049 static struct WS_servent *WS_dup_se(const struct servent* p_se)
4052 struct WS_servent *p_to;
4054 int size = (sizeof(*p_se) +
4055 strlen(p_se->s_proto) + 1 +
4056 strlen(p_se->s_name) + 1 +
4057 list_size(p_se->s_aliases, 0));
4059 if (!(p_to = check_buffer_se(size))) return NULL;
4060 p_to->s_port = p_se->s_port;
4062 p = (char *)(p_to + 1);
4064 strcpy(p, p_se->s_name);
4068 strcpy(p, p_se->s_proto);
4071 p_to->s_aliases = (char **)p;
4072 list_dup(p_se->s_aliases, p_to->s_aliases, 0);
4076 /* ----------------------------------- error handling */
4080 int loc_errno = errno;
4081 WARN("errno %d, (%s).\n", loc_errno, strerror(loc_errno));
4085 case EINTR: return WSAEINTR;
4086 case EBADF: return WSAEBADF;
4088 case EACCES: return WSAEACCES;
4089 case EFAULT: return WSAEFAULT;
4090 case EINVAL: return WSAEINVAL;
4091 case EMFILE: return WSAEMFILE;
4092 case EWOULDBLOCK: return WSAEWOULDBLOCK;
4093 case EINPROGRESS: return WSAEINPROGRESS;
4094 case EALREADY: return WSAEALREADY;
4095 case ENOTSOCK: return WSAENOTSOCK;
4096 case EDESTADDRREQ: return WSAEDESTADDRREQ;
4097 case EMSGSIZE: return WSAEMSGSIZE;
4098 case EPROTOTYPE: return WSAEPROTOTYPE;
4099 case ENOPROTOOPT: return WSAENOPROTOOPT;
4100 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
4101 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
4102 case EOPNOTSUPP: return WSAEOPNOTSUPP;
4103 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
4104 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
4105 case EADDRINUSE: return WSAEADDRINUSE;
4106 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
4107 case ENETDOWN: return WSAENETDOWN;
4108 case ENETUNREACH: return WSAENETUNREACH;
4109 case ENETRESET: return WSAENETRESET;
4110 case ECONNABORTED: return WSAECONNABORTED;
4112 case ECONNRESET: return WSAECONNRESET;
4113 case ENOBUFS: return WSAENOBUFS;
4114 case EISCONN: return WSAEISCONN;
4115 case ENOTCONN: return WSAENOTCONN;
4116 case ESHUTDOWN: return WSAESHUTDOWN;
4117 case ETOOMANYREFS: return WSAETOOMANYREFS;
4118 case ETIMEDOUT: return WSAETIMEDOUT;
4119 case ECONNREFUSED: return WSAECONNREFUSED;
4120 case ELOOP: return WSAELOOP;
4121 case ENAMETOOLONG: return WSAENAMETOOLONG;
4122 case EHOSTDOWN: return WSAEHOSTDOWN;
4123 case EHOSTUNREACH: return WSAEHOSTUNREACH;
4124 case ENOTEMPTY: return WSAENOTEMPTY;
4126 case EPROCLIM: return WSAEPROCLIM;
4129 case EUSERS: return WSAEUSERS;
4132 case EDQUOT: return WSAEDQUOT;
4135 case ESTALE: return WSAESTALE;
4138 case EREMOTE: return WSAEREMOTE;
4141 /* just in case we ever get here and there are no problems */
4144 WARN("Unknown errno %d!\n", loc_errno);
4145 return WSAEOPNOTSUPP;
4149 UINT wsaHerrno(int loc_errno)
4152 WARN("h_errno %d.\n", loc_errno);
4156 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
4157 case TRY_AGAIN: return WSATRY_AGAIN;
4158 case NO_RECOVERY: return WSANO_RECOVERY;
4159 case NO_DATA: return WSANO_DATA;
4160 case ENOBUFS: return WSAENOBUFS;
4164 WARN("Unknown h_errno %d!\n", loc_errno);
4165 return WSAEOPNOTSUPP;
4170 /***********************************************************************
4171 * WSARecv (WS2_32.67)
4173 int WINAPI WSARecv(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
4174 LPDWORD NumberOfBytesReceived, LPDWORD lpFlags,
4175 LPWSAOVERLAPPED lpOverlapped,
4176 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
4178 return WSARecvFrom(s, lpBuffers, dwBufferCount, NumberOfBytesReceived, lpFlags,
4179 NULL, NULL, lpOverlapped, lpCompletionRoutine);
4182 /***********************************************************************
4183 * WSARecvFrom (WS2_32.69)
4185 INT WINAPI WSARecvFrom( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
4186 LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, struct WS_sockaddr *lpFrom,
4187 LPINT lpFromlen, LPWSAOVERLAPPED lpOverlapped,
4188 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
4192 int n, fd, err = WSAENOTSOCK, flags, ret;
4193 struct iovec* iovec;
4194 struct ws2_async *wsa;
4195 IO_STATUS_BLOCK* iosb;
4197 TRACE("socket %04x, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %ld, ovl %p, func %p\n",
4198 s, lpBuffers, dwBufferCount, *lpFlags, lpFrom,
4199 (lpFromlen ? *lpFromlen : -1L),
4200 lpOverlapped, lpCompletionRoutine);
4202 fd = get_sock_fd( s, FILE_READ_DATA, &flags );
4203 TRACE( "fd=%d, flags=%x\n", fd, flags );
4205 if (fd == -1) return SOCKET_ERROR;
4207 if (flags & FD_FLAG_RECV_SHUTDOWN)
4209 WSASetLastError( WSAESHUTDOWN );
4213 iovec = HeapAlloc( GetProcessHeap(), 0, dwBufferCount * sizeof (struct iovec) );
4220 for (i = 0; i < dwBufferCount; i++)
4222 iovec[i].iov_base = lpBuffers[i].buf;
4223 iovec[i].iov_len = lpBuffers[i].len;
4226 if ( (lpOverlapped || lpCompletionRoutine) && flags & FD_FLAG_OVERLAPPED )
4228 wsa = WS2_make_async( s, ws2m_read, iovec, dwBufferCount,
4229 lpFlags, lpFrom, lpFromlen,
4230 lpOverlapped, lpCompletionRoutine, &iosb );
4238 if ( ( ret = ws2_queue_async( wsa, iosb )) )
4240 err = NtStatusToWSAError( ret );
4242 if ( !lpOverlapped )
4243 HeapFree( GetProcessHeap(), 0, iosb );
4244 HeapFree( GetProcessHeap(), 0, wsa );
4247 release_sock_fd( s, fd );
4249 /* Try immediate completion */
4252 if ( WSAGetOverlappedResult( s, lpOverlapped,
4253 lpNumberOfBytesRecvd, FALSE, lpFlags) )
4256 if ( (err = WSAGetLastError()) != WSA_IO_INCOMPLETE )
4260 WSASetLastError( WSA_IO_PENDING );
4261 return SOCKET_ERROR;
4264 if ( _is_blocking(s) )
4267 /* FIXME: OOB and exceptfds? */
4268 int timeout = GET_RCVTIMEO(fd);
4269 if( !do_block(fd, POLLIN, timeout)) {
4271 /* a timeout is not fatal */
4272 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4277 n = WS2_recv( fd, iovec, dwBufferCount, lpFrom, lpFromlen, lpFlags );
4284 TRACE(" -> %i bytes\n", n);
4285 *lpNumberOfBytesRecvd = n;
4287 HeapFree(GetProcessHeap(), 0, iovec);
4288 release_sock_fd( s, fd );
4289 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4294 HeapFree(GetProcessHeap(), 0, iovec);
4297 release_sock_fd( s, fd );
4300 WARN(" -> ERROR %d\n", err);
4301 WSASetLastError( err );
4302 return SOCKET_ERROR;
4305 /***********************************************************************
4306 * WSCInstallProvider (WS2_32.88)
4308 INT WINAPI WSCInstallProvider( const LPGUID lpProviderId,
4309 LPCWSTR lpszProviderDllPath,
4310 const LPWSAPROTOCOL_INFOW lpProtocolInfoList,
4311 DWORD dwNumberOfEntries,
4314 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId),
4315 debugstr_w(lpszProviderDllPath), lpProtocolInfoList,
4316 dwNumberOfEntries, lpErrno);
4322 /***********************************************************************
4323 * WSCDeinstallProvider (WS2_32.83)
4325 INT WINAPI WSCDeinstallProvider(LPGUID lpProviderId, LPINT lpErrno)
4327 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId), lpErrno);
4333 /***********************************************************************
4334 * WSAAccept (WS2_32.26)
4336 SOCKET WINAPI WSAAccept( SOCKET s, struct WS_sockaddr *addr, LPINT addrlen,
4337 LPCONDITIONPROC lpfnCondition, DWORD dwCallbackData)
4340 int ret = 0, size = 0;
4341 WSABUF CallerId, CallerData, CalleeId, CalleeData;
4342 /* QOS SQOS, GQOS; */
4345 SOCKADDR src_addr, dst_addr;
4347 TRACE("Socket %04x, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %d\n",
4348 s, addr, addrlen, lpfnCondition, dwCallbackData);
4351 size = sizeof(src_addr);
4352 cs = WS_accept(s, &src_addr, &size);
4354 if (cs == SOCKET_ERROR) return SOCKET_ERROR;
4356 CallerId.buf = (char *)&src_addr;
4357 CallerId.len = sizeof(src_addr);
4359 CallerData.buf = NULL;
4360 CallerData.len = (ULONG)NULL;
4362 WS_getsockname(cs, &dst_addr, &size);
4364 CalleeId.buf = (char *)&dst_addr;
4365 CalleeId.len = sizeof(dst_addr);
4368 ret = (*lpfnCondition)(&CallerId, &CallerData, NULL, NULL,
4369 &CalleeId, &CalleeData, &g, dwCallbackData);
4374 if (addr && addrlen)
4375 addr = memcpy(addr, &src_addr, (*addrlen > size) ? size : *addrlen );
4378 SERVER_START_REQ( set_socket_deferred )
4380 req->handle = SOCKET2HANDLE(s);
4381 req->deferred = SOCKET2HANDLE(cs);
4382 if ( !wine_server_call_err ( req ) )
4384 SetLastError( WSATRY_AGAIN );
4385 WS_closesocket( cs );
4389 return SOCKET_ERROR;
4392 SetLastError(WSAECONNREFUSED);
4393 return SOCKET_ERROR;
4395 FIXME("Unknown return type from Condition function\n");
4396 SetLastError(WSAENOTSOCK);
4397 return SOCKET_ERROR;
4401 /***********************************************************************
4402 * WSADuplicateSocketA (WS2_32.32)
4404 int WINAPI WSADuplicateSocketA( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOA lpProtocolInfo )
4408 TRACE("(%d,%x,%p)\n", s, dwProcessId, lpProtocolInfo);
4409 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
4410 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
4411 /* I don't know what the real Windoze does next, this is a hack */
4412 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
4413 * the target use the global duplicate, or we could copy a reference to us to the structure
4414 * and let the target duplicate it from us, but let's do it as simple as possible */
4415 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
4416 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
4417 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
4418 0, FALSE, DUPLICATE_SAME_ACCESS);
4419 CloseHandle(hProcess);
4420 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
4424 /***********************************************************************
4425 * WSADuplicateSocketW (WS2_32.33)
4427 int WINAPI WSADuplicateSocketW( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOW lpProtocolInfo )
4431 TRACE("(%d,%x,%p)\n", s, dwProcessId, lpProtocolInfo);
4433 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
4434 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
4435 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
4436 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
4437 0, FALSE, DUPLICATE_SAME_ACCESS);
4438 CloseHandle(hProcess);
4439 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
4443 /***********************************************************************
4444 * WSAInstallServiceClassA (WS2_32.48)
4446 int WINAPI WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info)
4448 FIXME("Request to install service %s\n",debugstr_a(info->lpszServiceClassName));
4449 WSASetLastError(WSAEACCES);
4450 return SOCKET_ERROR;
4453 /***********************************************************************
4454 * WSAInstallServiceClassW (WS2_32.49)
4456 int WINAPI WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info)
4458 FIXME("Request to install service %s\n",debugstr_w(info->lpszServiceClassName));
4459 WSASetLastError(WSAEACCES);
4460 return SOCKET_ERROR;
4463 /***********************************************************************
4464 * WSARemoveServiceClass (WS2_32.70)
4466 int WINAPI WSARemoveServiceClass(LPGUID info)
4468 FIXME("Request to remove service %p\n",info);
4469 WSASetLastError(WSATYPE_NOT_FOUND);
4470 return SOCKET_ERROR;
4473 /***********************************************************************
4474 * WSAStringToAddressA (WS2_32.80)
4476 INT WINAPI WSAStringToAddressA(LPSTR AddressString,
4478 LPWSAPROTOCOL_INFOA lpProtocolInfo,
4479 LPSOCKADDR lpAddress,
4480 LPINT lpAddressLength)
4483 struct in_addr inetaddr;
4484 LPSTR workBuffer=NULL,ptrPort;
4486 TRACE( "(%s, %x, %p, %p, %p)\n", AddressString, AddressFamily, lpProtocolInfo,
4487 lpAddress, lpAddressLength );
4489 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
4493 workBuffer = HeapAlloc( GetProcessHeap(), 0, strlen(AddressString)+1 );
4496 strcpy(workBuffer,AddressString);
4497 switch (AddressFamily)
4500 /* caller wants to know the size of the socket buffer */
4501 if (*lpAddressLength < sizeof(SOCKADDR_IN))
4503 *lpAddressLength = sizeof(SOCKADDR_IN);
4508 /* caller wants to translate an AddressString into a SOCKADDR */
4511 memset(lpAddress,0,sizeof(SOCKADDR_IN));
4512 ((LPSOCKADDR_IN)lpAddress)->sin_family = AF_INET;
4513 ptrPort = strchr(workBuffer,':');
4516 ((LPSOCKADDR_IN)lpAddress)->sin_port = (WS_u_short)atoi(ptrPort+1);
4520 ((LPSOCKADDR_IN)lpAddress)->sin_port = 0;
4521 if (inet_aton(workBuffer, &inetaddr) > 0)
4523 ((LPSOCKADDR_IN)lpAddress)->sin_addr.WS_s_addr = inetaddr.s_addr;
4531 FIXME("(%s, %x, %p, %p, %p) - ProtocolInfo not implemented!\n",
4532 AddressString, AddressFamily,
4533 lpProtocolInfo, lpAddress, lpAddressLength);
4537 FIXME("(%s, %x, %p, %p, %p) - AddressFamiliy not implemented!\n",
4538 AddressString, AddressFamily,
4539 lpProtocolInfo, lpAddress, lpAddressLength);
4541 HeapFree( GetProcessHeap(), 0, workBuffer );
4544 res = WSA_NOT_ENOUGH_MEMORY;
4550 WSASetLastError(res);
4551 return SOCKET_ERROR;
4554 /***********************************************************************
4555 * WSAStringToAddressW (WS2_32.81)
4557 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
4558 * If this should be the case, it would be required to map these digits
4559 * to Unicode digits (0-9) using FoldString first.
4561 INT WINAPI WSAStringToAddressW(LPWSTR AddressString,
4563 LPWSAPROTOCOL_INFOW lpProtocolInfo,
4564 LPSOCKADDR lpAddress,
4565 LPINT lpAddressLength)
4568 LPSTR workBuffer=NULL;
4569 WSAPROTOCOL_INFOA infoA;
4570 LPWSAPROTOCOL_INFOA lpProtoInfoA = NULL;
4572 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString), AddressFamily, lpProtocolInfo,
4573 lpAddress, lpAddressLength );
4575 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
4577 /* if ProtocolInfo is available - convert to ANSI variant */
4580 lpProtoInfoA = &infoA;
4581 memcpy( lpProtoInfoA, lpProtocolInfo, FIELD_OFFSET( WSAPROTOCOL_INFOA, szProtocol ) );
4583 if (!WideCharToMultiByte( CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
4584 lpProtoInfoA->szProtocol, WSAPROTOCOL_LEN+1, NULL, NULL ))
4586 WSASetLastError( WSAEINVAL);
4587 return SOCKET_ERROR;
4593 /* Translate AddressString to ANSI code page - assumes that only
4594 standard digits 0-9 are used with this API call */
4595 sBuffer = WideCharToMultiByte( CP_ACP, 0, AddressString, -1, NULL, 0, NULL, NULL );
4596 workBuffer = HeapAlloc( GetProcessHeap(), 0, sBuffer );
4600 WideCharToMultiByte( CP_ACP, 0, AddressString, -1, workBuffer, sBuffer, NULL, NULL );
4601 res = WSAStringToAddressA(workBuffer,AddressFamily,lpProtoInfoA,
4602 lpAddress,lpAddressLength);
4603 HeapFree( GetProcessHeap(), 0, workBuffer );
4607 res = WSA_NOT_ENOUGH_MEMORY;
4612 WSASetLastError(res);
4613 return SOCKET_ERROR;
4616 /***********************************************************************
4617 * WSAAddressToStringA (WS2_32.27)
4619 * See WSAAddressToStringW
4621 INT WINAPI WSAAddressToStringA( LPSOCKADDR sockaddr, DWORD len,
4622 LPWSAPROTOCOL_INFOA info, LPSTR string,
4626 CHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4629 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
4631 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
4632 if (!string || !lenstr) return SOCKET_ERROR;
4634 /* sin_family is guaranteed to be the first u_short */
4635 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
4637 sprintf( buffer, "%u.%u.%u.%u:%u",
4638 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
4639 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
4640 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
4641 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
4642 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
4644 p = strchr( buffer, ':' );
4645 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
4647 size = strlen( buffer );
4652 WSASetLastError(WSAEFAULT);
4653 return SOCKET_ERROR;
4656 strcpy( string, buffer );
4660 /***********************************************************************
4661 * WSAAddressToStringW (WS2_32.28)
4663 * Convert a sockaddr address into a readable address string.
4666 * sockaddr [I] Pointer to a sockaddr structure.
4667 * len [I] Size of the sockaddr structure.
4668 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
4669 * string [I/O] Pointer to a buffer to receive the address string.
4670 * lenstr [I/O] Size of the receive buffer in WCHARs.
4674 * Failure: SOCKET_ERROR
4677 * The 'info' parameter is ignored.
4680 * Only supports AF_INET addresses.
4682 INT WINAPI WSAAddressToStringW( LPSOCKADDR sockaddr, DWORD len,
4683 LPWSAPROTOCOL_INFOW info, LPWSTR string,
4687 WCHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4688 static const WCHAR format[] = { '%','u','.','%','u','.','%','u','.','%','u',':','%','u',0 };
4691 TRACE( "(%p, %x, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
4693 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
4694 if (!string || !lenstr) return SOCKET_ERROR;
4696 /* sin_family is guaranteed to be the first u_short */
4697 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
4699 sprintfW( buffer, format,
4700 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
4701 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
4702 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
4703 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
4704 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
4706 p = strchrW( buffer, ':' );
4707 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
4709 size = lstrlenW( buffer );
4714 return SOCKET_ERROR;
4717 lstrcpyW( string, buffer );
4721 /***********************************************************************
4722 * WSAEnumNameSpaceProvidersA (WS2_32.34)
4724 INT WINAPI WSAEnumNameSpaceProvidersA( LPDWORD len, LPWSANAMESPACE_INFOA buffer )
4726 FIXME( "(%p %p) Stub!\n", len, buffer );
4730 /***********************************************************************
4731 * WSAEnumNameSpaceProvidersW (WS2_32.35)
4733 INT WINAPI WSAEnumNameSpaceProvidersW( LPDWORD len, LPWSANAMESPACE_INFOW buffer )
4735 FIXME( "(%p %p) Stub!\n", len, buffer );
4739 /***********************************************************************
4740 * WSAGetQOSByName (WS2_32.41)
4742 BOOL WINAPI WSAGetQOSByName( SOCKET s, LPWSABUF lpQOSName, LPQOS lpQOS )
4744 FIXME( "(0x%04x %p %p) Stub!\n", s, lpQOSName, lpQOS );
4748 /***********************************************************************
4749 * WSAGetServiceClassInfoA (WS2_32.42)
4751 INT WINAPI WSAGetServiceClassInfoA( LPGUID provider, LPGUID service, LPDWORD len,
4752 LPWSASERVICECLASSINFOA info )
4754 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider), debugstr_guid(service),
4756 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4757 return SOCKET_ERROR;
4760 /***********************************************************************
4761 * WSAGetServiceClassInfoW (WS2_32.43)
4763 INT WINAPI WSAGetServiceClassInfoW( LPGUID provider, LPGUID service, LPDWORD len,
4764 LPWSASERVICECLASSINFOW info )
4766 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider), debugstr_guid(service),
4768 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4769 return SOCKET_ERROR;
4772 /***********************************************************************
4773 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
4775 INT WINAPI WSAGetServiceClassNameByClassIdA( LPGUID class, LPSTR service, LPDWORD len )
4777 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4778 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4779 return SOCKET_ERROR;
4782 /***********************************************************************
4783 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
4785 INT WINAPI WSAGetServiceClassNameByClassIdW( LPGUID class, LPWSTR service, LPDWORD len )
4787 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4788 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4789 return SOCKET_ERROR;
4792 /***********************************************************************
4793 * WSALookupServiceBeginA (WS2_32.59)
4795 INT WINAPI WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions,
4796 DWORD dwControlFlags,
4799 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4801 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4802 return SOCKET_ERROR;
4805 /***********************************************************************
4806 * WSALookupServiceBeginW (WS2_32.60)
4808 INT WINAPI WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions,
4809 DWORD dwControlFlags,
4812 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4814 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4815 return SOCKET_ERROR;
4818 /***********************************************************************
4819 * WSALookupServiceBeginW (WS2_32.61)
4821 INT WINAPI WSALookupServiceEnd( HANDLE lookup )
4823 FIXME("(%p) Stub!\n", lookup );
4827 /***********************************************************************
4828 * WSALookupServiceNextA (WS2_32.62)
4830 INT WINAPI WSALookupServiceNextA( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETA results )
4832 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup, flags, len, results );
4836 /***********************************************************************
4837 * WSALookupServiceNextW (WS2_32.63)
4839 INT WINAPI WSALookupServiceNextW( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETW results )
4841 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup, flags, len, results );
4845 /***********************************************************************
4846 * WSANtohl (WS2_32.64)
4848 INT WINAPI WSANtohl( SOCKET s, WS_u_long netlong, WS_u_long* lphostlong )
4850 TRACE( "(0x%04x 0x%08x %p)\n", s, netlong, lphostlong );
4852 if (!lphostlong) return WSAEFAULT;
4854 *lphostlong = ntohl( netlong );
4858 /***********************************************************************
4859 * WSANtohs (WS2_32.65)
4861 INT WINAPI WSANtohs( SOCKET s, WS_u_short netshort, WS_u_short* lphostshort )
4863 TRACE( "(0x%04x 0x%08x %p)\n", s, netshort, lphostshort );
4865 if (!lphostshort) return WSAEFAULT;
4867 *lphostshort = ntohs( netshort );
4871 /***********************************************************************
4872 * WSAProviderConfigChange (WS2_32.66)
4874 INT WINAPI WSAProviderConfigChange( LPHANDLE handle, LPWSAOVERLAPPED overlapped,
4875 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion )
4877 FIXME( "(%p %p %p) Stub!\n", handle, overlapped, completion );
4878 return SOCKET_ERROR;
4881 /***********************************************************************
4882 * WSARecvDisconnect (WS2_32.68)
4884 INT WINAPI WSARecvDisconnect( SOCKET s, LPWSABUF disconnectdata )
4886 TRACE( "(0x%04x %p)\n", s, disconnectdata );
4888 return WS_shutdown( s, 0 );
4891 /***********************************************************************
4892 * WSASetServiceA (WS2_32.76)
4894 INT WINAPI WSASetServiceA( LPWSAQUERYSETA query, WSAESETSERVICEOP operation, DWORD flags )
4896 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query, operation, flags );
4900 /***********************************************************************
4901 * WSASetServiceW (WS2_32.77)
4903 INT WINAPI WSASetServiceW( LPWSAQUERYSETW query, WSAESETSERVICEOP operation, DWORD flags )
4905 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query, operation, flags );
4909 /***********************************************************************
4910 * WSCEnableNSProvider (WS2_32.84)
4912 INT WINAPI WSCEnableNSProvider( LPGUID provider, BOOL enable )
4914 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider), enable );
4918 /***********************************************************************
4919 * WSCGetProviderPath (WS2_32.86)
4921 INT WINAPI WSCGetProviderPath( LPGUID provider, LPWSTR path, LPINT len, LPINT errcode )
4923 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider), path, len, errcode );
4925 if (!errcode || !provider || !len) return WSAEFAULT;
4927 *errcode = WSAEINVAL;
4928 return SOCKET_ERROR;
4931 /***********************************************************************
4932 * WSCInstallNameSpace (WS2_32.87)
4934 INT WINAPI WSCInstallNameSpace( LPWSTR identifier, LPWSTR path, DWORD namespace,
4935 DWORD version, LPGUID provider )
4937 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier), debugstr_w(path),
4938 namespace, version, debugstr_guid(provider) );
4942 /***********************************************************************
4943 * WSCUnInstallNameSpace (WS2_32.89)
4945 INT WINAPI WSCUnInstallNameSpace( LPGUID lpProviderId )
4947 FIXME("(%p) Stub!\n", lpProviderId);
4951 /***********************************************************************
4952 * WSCWriteProviderOrder (WS2_32.91)
4954 INT WINAPI WSCWriteProviderOrder( LPDWORD entry, DWORD number )
4956 FIXME("(%p 0x%08x) Stub!\n", entry, number);