2 * based on Windows Sockets 1.1 specs
3 * (ftp.microsoft.com:/Advsys/winsock/spec11/WINSOCK.TXT)
5 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
6 * Copyright (C) 2005 Marcus Meissner
7 * Copyright (C) 2006 Kai Blin
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * NOTE: If you make any changes to fix a particular app, make sure
24 * they don't break something else like Netscape or telnet and ftp
25 * clients and servers (www.winsite.com got a lot of those).
29 #include "wine/port.h"
34 #include <sys/types.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 # include <sys/ioctl.h>
41 #ifdef HAVE_SYS_FILIO_H
42 # include <sys/filio.h>
44 #ifdef HAVE_SYS_SOCKIO_H
45 # include <sys/sockio.h>
49 # include <sys/so_ioctl.h>
52 #ifdef HAVE_SYS_PARAM_H
53 # include <sys/param.h>
59 #ifdef HAVE_SYS_WAIT_H
60 # include <sys/wait.h>
65 #ifdef HAVE_SYS_SOCKET_H
66 #include <sys/socket.h>
68 #ifdef HAVE_NETINET_IN_H
69 # include <netinet/in.h>
71 #ifdef HAVE_NETINET_TCP_H
72 # include <netinet/tcp.h>
74 #ifdef HAVE_ARPA_INET_H
75 # include <arpa/inet.h>
80 #ifdef HAVE_SYS_ERRNO_H
81 #include <sys/errno.h>
90 #ifdef HAVE_ARPA_NAMESER_H
91 # include <arpa/nameser.h>
100 #ifdef HAVE_NETIPX_IPX_H
101 # include <netipx/ipx.h>
103 #elif defined(HAVE_LINUX_IPX_H)
104 # ifdef HAVE_ASM_TYPES_H
105 # include <asm/types.h>
107 # include <linux/ipx.h>
114 #ifdef HAVE_SYS_POLL_H
115 # include <sys/poll.h>
117 #ifdef HAVE_SYS_TIME_H
118 # include <sys/time.h>
121 #define NONAMELESSUNION
122 #define NONAMELESSSTRUCT
123 #include "ntstatus.h"
124 #define WIN32_NO_STATUS
129 #include "winerror.h"
131 #include "winsock2.h"
133 #include "ws2tcpip.h"
137 #include "iphlpapi.h"
138 #include "wine/server.h"
139 #include "wine/debug.h"
140 #include "wine/unicode.h"
143 # include "wsnwlink.h"
147 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
148 # define sipx_network sipx_addr.x_net
149 # define sipx_node sipx_addr.x_host.c_host
150 #endif /* __FreeBSD__ */
153 #define INADDR_NONE ~0UL
156 WINE_DEFAULT_DEBUG_CHANNEL(winsock);
158 /* critical section to protect some non-rentrant net function */
159 extern CRITICAL_SECTION csWSgetXXXbyYYY;
161 union generic_unix_sockaddr
163 struct sockaddr addr;
164 char data[128]; /* should be big enough for all families */
167 static inline const char *debugstr_sockaddr( const struct WS_sockaddr *a )
169 if (!a) return "(nil)";
170 return wine_dbg_sprintf("{ family %d, address %s, port %d }",
171 ((const struct sockaddr_in *)a)->sin_family,
172 inet_ntoa(((const struct sockaddr_in *)a)->sin_addr),
173 ntohs(((const struct sockaddr_in *)a)->sin_port));
176 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
177 #define SOCKET2HANDLE(s) ((HANDLE)(s))
178 #define HANDLE2SOCKET(h) ((SOCKET)(h))
180 /****************************************************************
181 * Async IO declarations
182 ****************************************************************/
184 typedef struct ws2_async
188 LPWSAOVERLAPPED user_overlapped;
189 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func;
190 IO_STATUS_BLOCK local_iosb;
191 struct iovec iovec[WS_MSG_MAXIOVLEN];
193 struct WS_sockaddr *addr;
196 int val; /* for send operations */
197 int *ptr; /* for recv operations */
202 /****************************************************************/
204 /* ----------------------------------- internal data */
206 /* ws_... struct conversion flags */
208 typedef struct /* WSAAsyncSelect() control struct */
210 HANDLE service, event, sock;
216 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
217 #define WS_MAX_UDP_DATAGRAM 1024
218 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x );
220 /* hostent's, servent's and protent's are stored in one buffer per thread,
221 * as documented on MSDN for the functions that return any of the buffers */
222 struct per_thread_data
225 struct WS_hostent *he_buffer;
226 struct WS_servent *se_buffer;
227 struct WS_protoent *pe_buffer;
233 static INT num_startup; /* reference counter */
234 static FARPROC blocking_hook = (FARPROC)WSA_DefaultBlockingHook;
236 /* function prototypes */
237 static struct WS_hostent *WS_dup_he(const struct hostent* p_he);
238 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe);
239 static struct WS_servent *WS_dup_se(const struct servent* p_se);
241 int WSAIOCTL_GetInterfaceCount(void);
242 int WSAIOCTL_GetInterfaceName(int intNumber, char *intName);
245 UINT wsaHerrno(int errnr);
247 #define MAP_OPTION(opt) { WS_##opt, opt }
249 static const int ws_sock_map[][2] =
251 MAP_OPTION( SO_DEBUG ),
252 MAP_OPTION( SO_ACCEPTCONN ),
253 MAP_OPTION( SO_REUSEADDR ),
254 MAP_OPTION( SO_KEEPALIVE ),
255 MAP_OPTION( SO_DONTROUTE ),
256 MAP_OPTION( SO_BROADCAST ),
257 MAP_OPTION( SO_LINGER ),
258 MAP_OPTION( SO_OOBINLINE ),
259 MAP_OPTION( SO_SNDBUF ),
260 MAP_OPTION( SO_RCVBUF ),
261 MAP_OPTION( SO_ERROR ),
262 MAP_OPTION( SO_TYPE ),
264 MAP_OPTION( SO_RCVTIMEO ),
267 MAP_OPTION( SO_SNDTIMEO ),
271 static const int ws_tcp_map[][2] =
274 MAP_OPTION( TCP_NODELAY ),
278 static const int ws_ip_map[][2] =
280 MAP_OPTION( IP_MULTICAST_IF ),
281 MAP_OPTION( IP_MULTICAST_TTL ),
282 MAP_OPTION( IP_MULTICAST_LOOP ),
283 MAP_OPTION( IP_ADD_MEMBERSHIP ),
284 MAP_OPTION( IP_DROP_MEMBERSHIP ),
285 MAP_OPTION( IP_OPTIONS ),
287 MAP_OPTION( IP_HDRINCL ),
289 MAP_OPTION( IP_TOS ),
290 MAP_OPTION( IP_TTL ),
293 static const int ws_af_map[][2] =
295 MAP_OPTION( AF_UNSPEC ),
296 MAP_OPTION( AF_INET ),
297 MAP_OPTION( AF_INET6 ),
299 MAP_OPTION( AF_IPX ),
303 static const int ws_socktype_map[][2] =
305 MAP_OPTION( SOCK_DGRAM ),
306 MAP_OPTION( SOCK_STREAM ),
307 MAP_OPTION( SOCK_RAW ),
310 static const int ws_proto_map[][2] =
312 MAP_OPTION( IPPROTO_IP ),
313 MAP_OPTION( IPPROTO_TCP ),
314 MAP_OPTION( IPPROTO_UDP ),
315 MAP_OPTION( IPPROTO_ICMP ),
316 MAP_OPTION( IPPROTO_IGMP ),
317 MAP_OPTION( IPPROTO_RAW ),
320 static const int ws_aiflag_map[][2] =
322 MAP_OPTION( AI_PASSIVE ),
323 MAP_OPTION( AI_CANONNAME ),
324 MAP_OPTION( AI_NUMERICHOST ),
325 /* Linux/UNIX knows a lot more. But Windows only
326 * has 3 as far as I could see. -Marcus
330 static const int ws_eai_map[][2] =
332 MAP_OPTION( EAI_AGAIN ),
333 MAP_OPTION( EAI_BADFLAGS ),
334 MAP_OPTION( EAI_FAIL ),
335 MAP_OPTION( EAI_FAMILY ),
336 MAP_OPTION( EAI_MEMORY ),
337 /* Note: EAI_NODATA is deprecated, but still
338 * used by Windows and Linux... We map the newer
339 * EAI_NONAME to EAI_NODATA for now until Windows
343 MAP_OPTION( EAI_NODATA ),
346 { WS_EAI_NODATA, EAI_NONAME },
349 MAP_OPTION( EAI_SERVICE ),
350 MAP_OPTION( EAI_SOCKTYPE ),
354 static inline DWORD NtStatusToWSAError( const DWORD status )
356 /* We only need to cover the status codes set by server async request handling */
360 case STATUS_SUCCESS: wserr = 0; break;
361 case STATUS_PENDING: wserr = WSA_IO_PENDING; break;
362 case STATUS_OBJECT_TYPE_MISMATCH: wserr = WSAENOTSOCK; break;
363 case STATUS_INVALID_HANDLE: wserr = WSAEBADF; break;
364 case STATUS_INVALID_PARAMETER: wserr = WSAEINVAL; break;
365 case STATUS_PIPE_DISCONNECTED: wserr = WSAESHUTDOWN; break;
366 case STATUS_CANCELLED: wserr = WSA_OPERATION_ABORTED; break;
367 case STATUS_TIMEOUT: wserr = WSAETIMEDOUT; break;
368 case STATUS_NO_MEMORY: wserr = WSAEFAULT; break;
370 if ( status >= WSABASEERR && status <= WSABASEERR+1004 )
371 /* It is not an NT status code but a winsock error */
375 wserr = RtlNtStatusToDosError( status );
376 FIXME( "Status code %08x converted to DOS error code %x\n", status, wserr );
382 /* set last error code from NT status without mapping WSA errors */
383 static inline unsigned int set_error( unsigned int err )
387 err = NtStatusToWSAError( err );
393 static inline int get_sock_fd( SOCKET s, DWORD access, unsigned int *options )
396 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s), access, &fd, options ) ))
401 static inline void release_sock_fd( SOCKET s, int fd )
403 wine_server_release_fd( SOCKET2HANDLE(s), fd );
406 static void _enable_event( HANDLE s, unsigned int event,
407 unsigned int sstate, unsigned int cstate )
409 SERVER_START_REQ( enable_socket_event )
413 req->sstate = sstate;
414 req->cstate = cstate;
415 wine_server_call( req );
420 static int _is_blocking(SOCKET s)
423 SERVER_START_REQ( get_socket_event )
425 req->handle = SOCKET2HANDLE(s);
426 req->service = FALSE;
428 wine_server_call( req );
429 ret = (reply->state & FD_WINE_NONBLOCKING) == 0;
435 static unsigned int _get_sock_mask(SOCKET s)
438 SERVER_START_REQ( get_socket_event )
440 req->handle = SOCKET2HANDLE(s);
441 req->service = FALSE;
443 wine_server_call( req );
450 static void _sync_sock_state(SOCKET s)
452 /* do a dummy wineserver request in order to let
453 the wineserver run through its select loop once */
454 (void)_is_blocking(s);
457 static int _get_sock_error(SOCKET s, unsigned int bit)
459 int events[FD_MAX_EVENTS];
461 SERVER_START_REQ( get_socket_event )
463 req->handle = SOCKET2HANDLE(s);
464 req->service = FALSE;
466 wine_server_set_reply( req, events, sizeof(events) );
467 wine_server_call( req );
473 static struct per_thread_data *get_per_thread_data(void)
475 struct per_thread_data * ptb = NtCurrentTeb()->WinSockData;
476 /* lazy initialization */
479 ptb = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ptb) );
480 NtCurrentTeb()->WinSockData = ptb;
485 static void free_per_thread_data(void)
487 struct per_thread_data * ptb = NtCurrentTeb()->WinSockData;
491 /* delete scratch buffers */
492 HeapFree( GetProcessHeap(), 0, ptb->he_buffer );
493 HeapFree( GetProcessHeap(), 0, ptb->se_buffer );
494 HeapFree( GetProcessHeap(), 0, ptb->pe_buffer );
495 ptb->he_buffer = NULL;
496 ptb->se_buffer = NULL;
497 ptb->pe_buffer = NULL;
499 HeapFree( GetProcessHeap(), 0, ptb );
500 NtCurrentTeb()->WinSockData = NULL;
503 /***********************************************************************
504 * DllMain (WS2_32.init)
506 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
508 TRACE("%p 0x%x %p\n", hInstDLL, fdwReason, fImpLoad);
510 case DLL_PROCESS_ATTACH:
512 case DLL_PROCESS_DETACH:
513 free_per_thread_data();
516 case DLL_THREAD_DETACH:
517 free_per_thread_data();
523 /***********************************************************************
526 * Converts socket flags from Windows format.
527 * Return 1 if converted, 0 if not (error).
529 static int convert_sockopt(INT *level, INT *optname)
536 for(i=0; i<sizeof(ws_sock_map)/sizeof(ws_sock_map[0]); i++) {
537 if( ws_sock_map[i][0] == *optname )
539 *optname = ws_sock_map[i][1];
543 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname);
546 *level = IPPROTO_TCP;
547 for(i=0; i<sizeof(ws_tcp_map)/sizeof(ws_tcp_map[0]); i++) {
548 if ( ws_tcp_map[i][0] == *optname )
550 *optname = ws_tcp_map[i][1];
554 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname);
558 for(i=0; i<sizeof(ws_ip_map)/sizeof(ws_ip_map[0]); i++) {
559 if (ws_ip_map[i][0] == *optname )
561 *optname = ws_ip_map[i][1];
565 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname);
567 default: FIXME("Unimplemented or unknown socket level\n");
572 static inline BOOL is_timeout_option( int optname )
575 if (optname == SO_RCVTIMEO) return TRUE;
578 if (optname == SO_SNDTIMEO) return TRUE;
583 /* ----------------------------------- Per-thread info (or per-process?) */
585 static char *strdup_lower(const char *str)
588 char *ret = HeapAlloc( GetProcessHeap(), 0, strlen(str) + 1 );
592 for (i = 0; str[i]; i++) ret[i] = tolower(str[i]);
595 else SetLastError(WSAENOBUFS);
599 static inline int sock_error_p(int s)
601 unsigned int optval, optlen;
603 optlen = sizeof(optval);
604 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
605 if (optval) WARN("\t[%i] error: %d\n", s, optval);
609 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
610 * from an fd and return the value converted to milli seconds
611 * or -1 if there is an infinite time out */
612 static inline int get_rcvsnd_timeo( int fd, int optname)
615 unsigned int len = sizeof(tv);
616 int ret = getsockopt(fd, SOL_SOCKET, optname, &tv, &len);
618 ret = tv.tv_sec * 1000 + tv.tv_usec / 1000;
619 if( ret <= 0 ) /* tv == {0,0} means infinite time out */
624 /* macro wrappers for portability */
626 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
628 #define GET_RCVTIMEO(fd) (-1)
632 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
634 #define GET_SNDTIMEO(fd) (-1)
637 /* utility: given an fd, will block until one of the events occurs */
638 static inline int do_block( int fd, int events, int timeout )
646 while ((ret = poll(&pfd, 1, timeout)) < 0)
657 convert_af_w2u(int windowsaf) {
660 for (i=0;i<sizeof(ws_af_map)/sizeof(ws_af_map[0]);i++)
661 if (ws_af_map[i][0] == windowsaf)
662 return ws_af_map[i][1];
663 FIXME("unhandled Windows address family %d\n", windowsaf);
668 convert_af_u2w(int unixaf) {
671 for (i=0;i<sizeof(ws_af_map)/sizeof(ws_af_map[0]);i++)
672 if (ws_af_map[i][1] == unixaf)
673 return ws_af_map[i][0];
674 FIXME("unhandled UNIX address family %d\n", unixaf);
679 convert_proto_w2u(int windowsproto) {
682 for (i=0;i<sizeof(ws_proto_map)/sizeof(ws_proto_map[0]);i++)
683 if (ws_proto_map[i][0] == windowsproto)
684 return ws_proto_map[i][1];
685 FIXME("unhandled Windows socket protocol %d\n", windowsproto);
690 convert_proto_u2w(int unixproto) {
693 for (i=0;i<sizeof(ws_proto_map)/sizeof(ws_proto_map[0]);i++)
694 if (ws_proto_map[i][1] == unixproto)
695 return ws_proto_map[i][0];
696 FIXME("unhandled UNIX socket protocol %d\n", unixproto);
701 convert_socktype_w2u(int windowssocktype) {
704 for (i=0;i<sizeof(ws_socktype_map)/sizeof(ws_socktype_map[0]);i++)
705 if (ws_socktype_map[i][0] == windowssocktype)
706 return ws_socktype_map[i][1];
707 FIXME("unhandled Windows socket type %d\n", windowssocktype);
712 convert_socktype_u2w(int unixsocktype) {
715 for (i=0;i<sizeof(ws_socktype_map)/sizeof(ws_socktype_map[0]);i++)
716 if (ws_socktype_map[i][1] == unixsocktype)
717 return ws_socktype_map[i][0];
718 FIXME("unhandled UNIX socket type %d\n", unixsocktype);
722 /* ----------------------------------- API -----
724 * Init / cleanup / error checking.
727 /***********************************************************************
728 * WSAStartup (WS2_32.115)
730 int WINAPI WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData)
732 TRACE("verReq=%x\n", wVersionRequested);
734 if (LOBYTE(wVersionRequested) < 1)
735 return WSAVERNOTSUPPORTED;
737 if (!lpWSAData) return WSAEINVAL;
741 /* that's the whole of the negotiation for now */
742 lpWSAData->wVersion = wVersionRequested;
743 /* return winsock information */
744 lpWSAData->wHighVersion = 0x0202;
745 strcpy(lpWSAData->szDescription, "WinSock 2.0" );
746 strcpy(lpWSAData->szSystemStatus, "Running" );
747 lpWSAData->iMaxSockets = WS_MAX_SOCKETS_PER_PROCESS;
748 lpWSAData->iMaxUdpDg = WS_MAX_UDP_DATAGRAM;
749 /* don't do anything with lpWSAData->lpVendorInfo */
750 /* (some apps don't allocate the space for this field) */
752 TRACE("succeeded\n");
757 /***********************************************************************
758 * WSACleanup (WS2_32.116)
760 INT WINAPI WSACleanup(void)
766 SetLastError(WSANOTINITIALISED);
771 /***********************************************************************
772 * WSAGetLastError (WINSOCK.111)
773 * WSAGetLastError (WS2_32.111)
775 INT WINAPI WSAGetLastError(void)
777 return GetLastError();
780 /***********************************************************************
781 * WSASetLastError (WS2_32.112)
783 void WINAPI WSASetLastError(INT iError) {
784 SetLastError(iError);
787 static struct WS_hostent *check_buffer_he(int size)
789 struct per_thread_data * ptb = get_per_thread_data();
792 if (ptb->he_len >= size ) return ptb->he_buffer;
793 HeapFree( GetProcessHeap(), 0, ptb->he_buffer );
795 ptb->he_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->he_len = size) );
796 if (!ptb->he_buffer) SetLastError(WSAENOBUFS);
797 return ptb->he_buffer;
800 static struct WS_servent *check_buffer_se(int size)
802 struct per_thread_data * ptb = get_per_thread_data();
805 if (ptb->se_len >= size ) return ptb->se_buffer;
806 HeapFree( GetProcessHeap(), 0, ptb->se_buffer );
808 ptb->se_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->se_len = size) );
809 if (!ptb->se_buffer) SetLastError(WSAENOBUFS);
810 return ptb->se_buffer;
813 static struct WS_protoent *check_buffer_pe(int size)
815 struct per_thread_data * ptb = get_per_thread_data();
818 if (ptb->pe_len >= size ) return ptb->pe_buffer;
819 HeapFree( GetProcessHeap(), 0, ptb->pe_buffer );
821 ptb->pe_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->pe_len = size) );
822 if (!ptb->pe_buffer) SetLastError(WSAENOBUFS);
823 return ptb->pe_buffer;
826 /* ----------------------------------- i/o APIs */
829 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf)== WS_AF_IPX || (pf) == WS_AF_INET6)
831 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf) == WS_AF_INET6)
835 /**********************************************************************/
837 /* Returns the length of the converted address if successful, 0 if it was too small to
840 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr* wsaddr, int wsaddrlen,
841 union generic_unix_sockaddr *uaddr)
843 unsigned int uaddrlen = 0;
845 switch (wsaddr->sa_family)
850 const struct WS_sockaddr_ipx* wsipx=(const struct WS_sockaddr_ipx*)wsaddr;
851 struct sockaddr_ipx* uipx = (struct sockaddr_ipx *)uaddr;
853 if (wsaddrlen<sizeof(struct WS_sockaddr_ipx))
856 uaddrlen = sizeof(struct sockaddr_ipx);
857 memset( uaddr, 0, uaddrlen );
858 uipx->sipx_family=AF_IPX;
859 uipx->sipx_port=wsipx->sa_socket;
860 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
863 memcpy(&uipx->sipx_network,wsipx->sa_netnum,sizeof(uipx->sipx_network)+sizeof(uipx->sipx_node));
864 #ifdef IPX_FRAME_NONE
865 uipx->sipx_type=IPX_FRAME_NONE;
871 struct sockaddr_in6* uin6 = (struct sockaddr_in6 *)uaddr;
872 const struct WS_sockaddr_in6* win6 = (const struct WS_sockaddr_in6*)wsaddr;
874 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
875 * scope_id, one without. Check:
876 * http://msdn.microsoft.com/library/en-us/winsock/winsock/sockaddr_2.asp
878 if (wsaddrlen >= sizeof(struct WS_sockaddr_in6_old)) {
879 uaddrlen = sizeof(struct sockaddr_in6);
880 memset( uaddr, 0, uaddrlen );
881 uin6->sin6_family = AF_INET6;
882 uin6->sin6_port = win6->sin6_port;
883 uin6->sin6_flowinfo = win6->sin6_flowinfo;
884 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
885 if (wsaddrlen >= sizeof(struct WS_sockaddr_in6)) uin6->sin6_scope_id = win6->sin6_scope_id;
887 memcpy(&uin6->sin6_addr,&win6->sin6_addr,16); /* 16 bytes = 128 address bits */
890 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen);
894 struct sockaddr_in* uin = (struct sockaddr_in *)uaddr;
895 const struct WS_sockaddr_in* win = (const struct WS_sockaddr_in*)wsaddr;
897 if (wsaddrlen<sizeof(struct WS_sockaddr_in))
899 uaddrlen = sizeof(struct sockaddr_in);
900 memset( uaddr, 0, uaddrlen );
901 uin->sin_family = AF_INET;
902 uin->sin_port = win->sin_port;
903 memcpy(&uin->sin_addr,&win->sin_addr,4); /* 4 bytes = 32 address bits */
907 /* Try to determine the needed space by the passed windows sockaddr space */
909 default: /* likely a ipv4 address */
910 case sizeof(struct WS_sockaddr_in):
911 uaddrlen = sizeof(struct sockaddr_in);
914 case sizeof(struct WS_sockaddr_ipx):
915 uaddrlen = sizeof(struct sockaddr_ipx);
918 case sizeof(struct WS_sockaddr_in6):
919 case sizeof(struct WS_sockaddr_in6_old):
920 uaddrlen = sizeof(struct sockaddr_in6);
923 memset( uaddr, 0, uaddrlen );
927 FIXME("Unknown address family %d, return NULL.\n", wsaddr->sa_family);
933 static BOOL is_sockaddr_bound(const struct sockaddr *uaddr, int uaddrlen)
935 switch (uaddr->sa_family)
939 FIXME("don't know how to tell if IPX socket is bound, assuming it is!\n");
944 static const struct sockaddr_in6 emptyAddr;
945 const struct sockaddr_in6 *in6 = (const struct sockaddr_in6*) uaddr;
946 return in6->sin6_port || memcmp(&in6->sin6_addr, &emptyAddr.sin6_addr, sizeof(struct in6_addr));
950 static const struct sockaddr_in emptyAddr;
951 const struct sockaddr_in *in = (const struct sockaddr_in*) uaddr;
952 return in->sin_port || memcmp(&in->sin_addr, &emptyAddr.sin_addr, sizeof(struct in_addr));
957 FIXME("unknown address family %d\n", uaddr->sa_family);
962 /* Returns 0 if successful, -1 if the buffer is too small */
963 static int ws_sockaddr_u2ws(const struct sockaddr* uaddr, struct WS_sockaddr* wsaddr, int* wsaddrlen)
967 switch(uaddr->sa_family)
972 const struct sockaddr_ipx* uipx=(const struct sockaddr_ipx*)uaddr;
973 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
976 switch (*wsaddrlen) /* how much can we copy? */
980 *wsaddrlen = sizeof(*wsipx);
981 wsipx->sa_socket=uipx->sipx_port;
985 memcpy(wsipx->sa_nodenum,uipx->sipx_node,sizeof(wsipx->sa_nodenum));
993 memcpy(wsipx->sa_netnum,&uipx->sipx_network,sizeof(wsipx->sa_netnum));
999 wsipx->sa_family=WS_AF_IPX;
1010 const struct sockaddr_in6* uin6 = (const struct sockaddr_in6*)uaddr;
1011 struct WS_sockaddr_in6_old* win6old = (struct WS_sockaddr_in6_old*)wsaddr;
1013 if (*wsaddrlen < sizeof(struct WS_sockaddr_in6_old))
1015 win6old->sin6_family = WS_AF_INET6;
1016 win6old->sin6_port = uin6->sin6_port;
1017 win6old->sin6_flowinfo = uin6->sin6_flowinfo;
1018 memcpy(&win6old->sin6_addr,&uin6->sin6_addr,16); /* 16 bytes = 128 address bits */
1019 *wsaddrlen = sizeof(struct WS_sockaddr_in6_old);
1020 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1021 if (*wsaddrlen >= sizeof(struct WS_sockaddr_in6)) {
1022 struct WS_sockaddr_in6* win6 = (struct WS_sockaddr_in6*)wsaddr;
1023 win6->sin6_scope_id = uin6->sin6_scope_id;
1024 *wsaddrlen = sizeof(struct WS_sockaddr_in6);
1030 const struct sockaddr_in* uin = (const struct sockaddr_in*)uaddr;
1031 struct WS_sockaddr_in* win = (struct WS_sockaddr_in*)wsaddr;
1033 if (*wsaddrlen < sizeof(struct WS_sockaddr_in))
1035 win->sin_family = WS_AF_INET;
1036 win->sin_port = uin->sin_port;
1037 memcpy(&win->sin_addr,&uin->sin_addr,4); /* 4 bytes = 32 address bits */
1038 memset(&win->sin_zero, 0, 8); /* Make sure the null padding is null */
1039 *wsaddrlen = sizeof(struct WS_sockaddr_in);
1043 memset(wsaddr,0,*wsaddrlen);
1047 FIXME("Unknown address family %d\n", uaddr->sa_family);
1053 /**************************************************************************
1054 * Functions for handling overlapped I/O
1055 **************************************************************************/
1057 /* user APC called upon async completion */
1058 static void WINAPI ws2_async_apc( void *arg, IO_STATUS_BLOCK *iosb, ULONG reserved )
1060 ws2_async *wsa = arg;
1062 if (wsa->completion_func) wsa->completion_func( NtStatusToWSAError(iosb->u.Status),
1063 iosb->Information, wsa->user_overlapped,
1065 HeapFree( GetProcessHeap(), 0, wsa );
1068 /***********************************************************************
1069 * WS2_recv (INTERNAL)
1071 * Workhorse for both synchronous and asynchronous recv() operations.
1073 static int WS2_recv( int fd, struct iovec* iov, int count,
1074 struct WS_sockaddr *lpFrom, LPINT lpFromlen,
1078 union generic_unix_sockaddr unix_sockaddr;
1081 hdr.msg_name = NULL;
1085 hdr.msg_namelen = sizeof(unix_sockaddr);
1086 hdr.msg_name = &unix_sockaddr;
1089 hdr.msg_namelen = 0;
1092 hdr.msg_iovlen = count;
1093 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1094 hdr.msg_accrights = NULL;
1095 hdr.msg_accrightslen = 0;
1097 hdr.msg_control = NULL;
1098 hdr.msg_controllen = 0;
1102 if ( (n = recvmsg(fd, &hdr, *lpFlags)) == -1 )
1106 ws_sockaddr_u2ws( &unix_sockaddr.addr, lpFrom, lpFromlen ) != 0 )
1108 /* The from buffer was too small, but we read the data
1109 * anyway. Is that really bad?
1116 /***********************************************************************
1117 * WS2_async_recv (INTERNAL)
1119 * Handler for overlapped recv() operations.
1121 static NTSTATUS WS2_async_recv( void* user, IO_STATUS_BLOCK* iosb, NTSTATUS status)
1123 ws2_async* wsa = user;
1128 case STATUS_ALERTED:
1129 if ((status = wine_server_handle_to_fd( wsa->hSocket, FILE_READ_DATA, &fd, NULL ) ))
1132 result = WS2_recv( fd, wsa->iovec, wsa->n_iovecs,
1133 wsa->addr, wsa->addrlen.ptr, &wsa->flags );
1134 wine_server_release_fd( wsa->hSocket, fd );
1137 status = STATUS_SUCCESS;
1138 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1142 if (errno == EINTR || errno == EAGAIN)
1144 status = STATUS_PENDING;
1145 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1150 status = wsaErrno(); /* FIXME: is this correct ???? */
1155 if (status != STATUS_PENDING)
1157 iosb->u.Status = status;
1158 iosb->Information = result;
1163 /***********************************************************************
1164 * WS2_send (INTERNAL)
1166 * Workhorse for both synchronous and asynchronous send() operations.
1168 static int WS2_send( int fd, struct iovec* iov, int count,
1169 const struct WS_sockaddr *to, INT tolen, DWORD dwFlags )
1172 union generic_unix_sockaddr unix_addr;
1175 hdr.msg_name = NULL;
1176 hdr.msg_namelen = 0;
1180 hdr.msg_name = &unix_addr;
1181 hdr.msg_namelen = ws_sockaddr_ws2u( to, tolen, &unix_addr );
1182 if ( !hdr.msg_namelen )
1188 #if defined(HAVE_IPX) && defined(SOL_IPX)
1189 if(to->sa_family == WS_AF_IPX)
1191 struct sockaddr_ipx* uipx = (struct sockaddr_ipx*)hdr.msg_name;
1193 unsigned int len=sizeof(int);
1195 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1196 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1197 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1198 * ipx type in the sockaddr_opx structure with the stored value.
1200 if(getsockopt(fd, SOL_IPX, IPX_TYPE, &val, &len) != -1)
1201 uipx->sipx_type = val;
1207 hdr.msg_iovlen = count;
1208 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1209 hdr.msg_accrights = NULL;
1210 hdr.msg_accrightslen = 0;
1212 hdr.msg_control = NULL;
1213 hdr.msg_controllen = 0;
1217 return sendmsg(fd, &hdr, dwFlags);
1220 /***********************************************************************
1221 * WS2_async_send (INTERNAL)
1223 * Handler for overlapped send() operations.
1225 static NTSTATUS WS2_async_send(void* user, IO_STATUS_BLOCK* iosb, NTSTATUS status)
1227 ws2_async* wsa = user;
1232 case STATUS_ALERTED:
1233 if ((status = wine_server_handle_to_fd( wsa->hSocket, FILE_WRITE_DATA, &fd, NULL ) ))
1236 /* check to see if the data is ready (non-blocking) */
1237 result = WS2_send( fd, wsa->iovec, wsa->n_iovecs, wsa->addr, wsa->addrlen.val, wsa->flags );
1238 wine_server_release_fd( wsa->hSocket, fd );
1242 status = STATUS_SUCCESS;
1243 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1247 if (errno == EINTR || errno == EAGAIN)
1249 status = STATUS_PENDING;
1250 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1254 /* We set the status to a winsock error code and check for that
1255 later in NtStatusToWSAError () */
1256 status = wsaErrno();
1262 if (status != STATUS_PENDING)
1264 iosb->u.Status = status;
1265 iosb->Information = result;
1270 /***********************************************************************
1271 * WS2_async_shutdown (INTERNAL)
1273 * Handler for shutdown() operations on overlapped sockets.
1275 static NTSTATUS WS2_async_shutdown( void* user, PIO_STATUS_BLOCK iosb, NTSTATUS status )
1277 ws2_async* wsa = user;
1282 case STATUS_ALERTED:
1283 if ((status = wine_server_handle_to_fd( wsa->hSocket, 0, &fd, NULL ) ))
1286 switch ( wsa->type )
1288 case ASYNC_TYPE_READ: err = shutdown( fd, 0 ); break;
1289 case ASYNC_TYPE_WRITE: err = shutdown( fd, 1 ); break;
1291 wine_server_release_fd( wsa->hSocket, fd );
1292 status = err ? wsaErrno() : STATUS_SUCCESS;
1295 iosb->u.Status = status;
1299 /***********************************************************************
1300 * WS2_register_async_shutdown (INTERNAL)
1302 * Helper function for WS_shutdown() on overlapped sockets.
1304 static int WS2_register_async_shutdown( SOCKET s, int type )
1306 struct ws2_async *wsa;
1309 TRACE("s %ld type %d\n", s, type);
1311 wsa = HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa) );
1315 wsa->hSocket = SOCKET2HANDLE(s);
1317 wsa->completion_func = NULL;
1319 SERVER_START_REQ( register_async )
1321 req->handle = wsa->hSocket;
1323 req->async.callback = WS2_async_shutdown;
1324 req->async.iosb = &wsa->local_iosb;
1325 req->async.arg = wsa;
1326 req->async.apc = ws2_async_apc;
1327 status = wine_server_call( req );
1331 if (status != STATUS_PENDING)
1333 HeapFree( GetProcessHeap(), 0, wsa );
1334 return NtStatusToWSAError( status );
1339 /***********************************************************************
1342 SOCKET WINAPI WS_accept(SOCKET s, struct WS_sockaddr *addr,
1348 TRACE("socket %04lx\n", s );
1349 is_blocking = _is_blocking(s);
1354 int fd = get_sock_fd( s, FILE_READ_DATA, NULL );
1355 if (fd == -1) return INVALID_SOCKET;
1357 do_block(fd, POLLIN, -1);
1358 _sync_sock_state(s); /* let wineserver notice connection */
1359 release_sock_fd( s, fd );
1360 /* retrieve any error codes from it */
1361 SetLastError(_get_sock_error(s, FD_ACCEPT_BIT));
1362 /* FIXME: care about the error? */
1364 SERVER_START_REQ( accept_socket )
1366 req->lhandle = SOCKET2HANDLE(s);
1367 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
1368 req->attributes = OBJ_INHERIT;
1369 set_error( wine_server_call( req ) );
1370 as = HANDLE2SOCKET( reply->handle );
1375 if (addr) WS_getpeername(as, addr, addrlen32);
1378 } while (is_blocking);
1379 return INVALID_SOCKET;
1382 /***********************************************************************
1385 int WINAPI WS_bind(SOCKET s, const struct WS_sockaddr* name, int namelen)
1387 int fd = get_sock_fd( s, 0, NULL );
1388 int res = SOCKET_ERROR;
1390 TRACE("socket %04lx, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1394 if (!name || (name->sa_family && !SUPPORTED_PF(name->sa_family)))
1396 SetLastError(WSAEAFNOSUPPORT);
1400 union generic_unix_sockaddr uaddr;
1401 unsigned int uaddrlen = ws_sockaddr_ws2u(name, namelen, &uaddr);
1404 SetLastError(WSAEFAULT);
1409 const struct sockaddr_in6 *in6 = (const struct sockaddr_in6*) &uaddr;
1410 if (name->sa_family == WS_AF_INET6 &&
1411 !memcmp(&in6->sin6_addr, &in6addr_any, sizeof(struct in6_addr)))
1414 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &enable, sizeof(enable)) == -1)
1416 release_sock_fd( s, fd );
1417 SetLastError(WSAEAFNOSUPPORT);
1418 return INVALID_SOCKET;
1422 if (bind(fd, &uaddr.addr, uaddrlen) < 0)
1424 int loc_errno = errno;
1425 WARN("\tfailure - errno = %i\n", errno);
1430 SetLastError(WSAENOTSOCK);
1433 SetLastError(WSAEINVAL);
1436 SetLastError(wsaErrno());
1442 res=0; /* success */
1446 release_sock_fd( s, fd );
1451 /***********************************************************************
1452 * closesocket (WS2_32.3)
1454 int WINAPI WS_closesocket(SOCKET s)
1456 TRACE("socket %04lx\n", s);
1457 if (CloseHandle(SOCKET2HANDLE(s))) return 0;
1458 return SOCKET_ERROR;
1461 /***********************************************************************
1462 * connect (WS2_32.4)
1464 int WINAPI WS_connect(SOCKET s, const struct WS_sockaddr* name, int namelen)
1466 int fd = get_sock_fd( s, FILE_READ_DATA, NULL );
1468 TRACE("socket %04lx, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1472 union generic_unix_sockaddr uaddr;
1473 unsigned int uaddrlen = ws_sockaddr_ws2u(name, namelen, &uaddr);
1477 SetLastError(WSAEFAULT);
1481 if (connect(fd, &uaddr.addr, uaddrlen) == 0)
1482 goto connect_success;
1485 if (errno == EINPROGRESS)
1487 /* tell wineserver that a connection is in progress */
1488 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1489 FD_CONNECT|FD_READ|FD_WRITE,
1490 FD_WINE_CONNECTED|FD_WINE_LISTENING);
1491 if (_is_blocking(s))
1495 do_block(fd, POLLIN | POLLOUT, -1);
1496 _sync_sock_state(s); /* let wineserver notice connection */
1497 /* retrieve any error codes from it */
1498 result = _get_sock_error(s, FD_CONNECT_BIT);
1500 SetLastError(result);
1503 goto connect_success;
1508 SetLastError(WSAEWOULDBLOCK);
1513 SetLastError(wsaErrno());
1515 release_sock_fd( s, fd );
1517 return SOCKET_ERROR;
1520 release_sock_fd( s, fd );
1521 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1522 FD_WINE_CONNECTED|FD_READ|FD_WRITE,
1523 FD_CONNECT|FD_WINE_LISTENING);
1527 /***********************************************************************
1528 * WSAConnect (WS2_32.30)
1530 int WINAPI WSAConnect( SOCKET s, const struct WS_sockaddr* name, int namelen,
1531 LPWSABUF lpCallerData, LPWSABUF lpCalleeData,
1532 LPQOS lpSQOS, LPQOS lpGQOS )
1534 if ( lpCallerData || lpCalleeData || lpSQOS || lpGQOS )
1535 FIXME("unsupported parameters!\n");
1536 return WS_connect( s, name, namelen );
1540 /***********************************************************************
1541 * getpeername (WS2_32.5)
1543 int WINAPI WS_getpeername(SOCKET s, struct WS_sockaddr *name, int *namelen)
1548 TRACE("socket: %04lx, ptr %p, len %08x\n", s, name, *namelen);
1550 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1551 if( (name == NULL) || (namelen == NULL) )
1553 SetLastError( WSAEFAULT );
1554 return SOCKET_ERROR;
1557 fd = get_sock_fd( s, 0, NULL );
1562 union generic_unix_sockaddr uaddr;
1563 unsigned int uaddrlen = sizeof(uaddr);
1565 if (getpeername(fd, &uaddr.addr, &uaddrlen) != 0)
1567 SetLastError(wsaErrno());
1569 else if (ws_sockaddr_u2ws(&uaddr.addr, name, namelen) != 0)
1571 /* The buffer was too small */
1572 SetLastError(WSAEFAULT);
1578 release_sock_fd( s, fd );
1583 /***********************************************************************
1584 * getsockname (WS2_32.6)
1586 int WINAPI WS_getsockname(SOCKET s, struct WS_sockaddr *name, int *namelen)
1591 TRACE("socket: %04lx, ptr %p, len %8x\n", s, name, *namelen);
1593 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1594 if( (name == NULL) || (namelen == NULL) )
1596 SetLastError( WSAEFAULT );
1597 return SOCKET_ERROR;
1600 fd = get_sock_fd( s, 0, NULL );
1605 union generic_unix_sockaddr uaddr;
1606 unsigned int uaddrlen = sizeof(uaddr);
1608 if (getsockname(fd, &uaddr.addr, &uaddrlen) != 0)
1610 SetLastError(wsaErrno());
1612 else if (!is_sockaddr_bound(&uaddr.addr, uaddrlen))
1614 SetLastError(WSAEINVAL);
1616 else if (ws_sockaddr_u2ws(&uaddr.addr, name, namelen) != 0)
1618 /* The buffer was too small */
1619 SetLastError(WSAEFAULT);
1625 release_sock_fd( s, fd );
1630 /***********************************************************************
1631 * getsockopt (WS2_32.7)
1633 INT WINAPI WS_getsockopt(SOCKET s, INT level,
1634 INT optname, char *optval, INT *optlen)
1639 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
1640 s, level, optname, optval, *optlen);
1648 /* Handle common cases. The special cases are below, sorted
1650 case WS_SO_ACCEPTCONN:
1651 case WS_SO_BROADCAST:
1654 case WS_SO_KEEPALIVE:
1655 case WS_SO_OOBINLINE:
1659 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1660 return SOCKET_ERROR;
1661 convert_sockopt(&level, &optname);
1662 if (getsockopt(fd,(int) level, optname, optval,
1663 (unsigned int *)optlen) != 0 )
1665 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1668 release_sock_fd( s, fd );
1671 case WS_SO_DONTLINGER:
1673 struct linger lingval;
1674 unsigned int len = sizeof(struct linger);
1676 if (!optlen || *optlen < sizeof(BOOL)|| !optval)
1678 SetLastError(WSAEFAULT);
1679 return SOCKET_ERROR;
1681 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1682 return SOCKET_ERROR;
1684 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &lingval, &len) != 0 )
1686 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1691 *(BOOL *)optval = (lingval.l_onoff) ? FALSE : TRUE;
1692 *optlen = sizeof(BOOL);
1695 release_sock_fd( s, fd );
1699 /* As mentioned in setsockopt, Windows ignores this, so we
1700 * always return true here */
1701 case WS_SO_DONTROUTE:
1702 if (!optlen || *optlen < sizeof(BOOL) || !optval)
1704 SetLastError(WSAEFAULT);
1705 return SOCKET_ERROR;
1707 *(BOOL *)optval = TRUE;
1708 *optlen = sizeof(BOOL);
1713 struct linger lingval;
1714 unsigned int len = sizeof(struct linger);
1716 /* struct linger and LINGER have different sizes */
1717 if (!optlen || *optlen < sizeof(LINGER) || !optval)
1719 SetLastError(WSAEFAULT);
1720 return SOCKET_ERROR;
1722 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1723 return SOCKET_ERROR;
1725 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &lingval, &len) != 0 )
1727 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1732 ((LINGER *)optval)->l_onoff = lingval.l_onoff;
1733 ((LINGER *)optval)->l_linger = lingval.l_linger;
1734 *optlen = sizeof(struct linger);
1737 release_sock_fd( s, fd );
1741 case WS_SO_MAX_MSG_SIZE:
1742 if (!optlen || *optlen < sizeof(int) || !optval)
1744 SetLastError(WSAEFAULT);
1745 return SOCKET_ERROR;
1747 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
1748 *(int *)optval = 65507;
1749 *optlen = sizeof(int);
1752 /* SO_OPENTYPE does not require a valid socket handle. */
1753 case WS_SO_OPENTYPE:
1754 if (!optlen || *optlen < sizeof(int) || !optval)
1756 SetLastError(WSAEFAULT);
1757 return SOCKET_ERROR;
1759 *(int *)optval = get_per_thread_data()->opentype;
1760 *optlen = sizeof(int);
1761 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
1765 case WS_SO_RCVTIMEO:
1768 case WS_SO_SNDTIMEO:
1770 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
1773 unsigned int len = sizeof(struct timeval);
1775 if (!optlen || *optlen < sizeof(int)|| !optval)
1777 SetLastError(WSAEFAULT);
1778 return SOCKET_ERROR;
1780 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1781 return SOCKET_ERROR;
1783 convert_sockopt(&level, &optname);
1784 if (getsockopt(fd,(int) level, optname, &tv, &len) != 0 )
1786 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1791 *(int *)optval = tv.tv_sec * 1000 + tv.tv_usec / 1000;
1792 *optlen = sizeof(int);
1795 release_sock_fd( s, fd );
1799 /* As mentioned in setsockopt, the windows style SO_REUSEADDR is
1800 * not possible in Unix, so always return false here. */
1801 case WS_SO_REUSEADDR:
1802 if (!optlen || *optlen < sizeof(int) || !optval)
1804 SetLastError(WSAEFAULT);
1805 return SOCKET_ERROR;
1808 *optlen = sizeof(int);
1812 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname);
1813 SetLastError(WSAENOPROTOOPT);
1814 return SOCKET_ERROR;
1815 } /* end switch(optname) */
1816 }/* end case WS_SOL_SOCKET */
1820 struct WS_sockaddr_ipx addr;
1821 IPX_ADDRESS_DATA *data;
1826 if ((fd = get_sock_fd( s, 0, NULL )) == -1) return SOCKET_ERROR;
1828 if(getsockopt(fd, SOL_IPX, IPX_TYPE, optval, (unsigned int*)optlen) == -1)
1835 socklen_t len=sizeof(struct ipx);
1836 if(getsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, &len) == -1 )
1839 *optval = (int)val.ipx_pt;
1842 TRACE("ptype: %d (fd: %d)\n", *(int*)optval, fd);
1843 release_sock_fd( s, fd );
1848 * On a Win2000 system with one network card there are usually
1849 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
1850 * Using this call you can then retrieve info about this all.
1851 * In case of Linux it is a bit different. Usually you have
1852 * only "one" device active and further it is not possible to
1853 * query things like the linkspeed.
1855 FIXME("IPX_ADDRESS\n");
1856 namelen = sizeof(struct WS_sockaddr_ipx);
1857 memset(&addr, 0, sizeof(struct WS_sockaddr_ipx));
1858 WS_getsockname(s, (struct WS_sockaddr*)&addr, &namelen);
1860 data = (IPX_ADDRESS_DATA*)optval;
1861 memcpy(data->nodenum,&addr.sa_nodenum,sizeof(data->nodenum));
1862 memcpy(data->netnum,&addr.sa_netnum,sizeof(data->netnum));
1863 data->adapternum = 0;
1864 data->wan = FALSE; /* We are not on a wan for now .. */
1865 data->status = FALSE; /* Since we are not on a wan, the wan link isn't up */
1866 data->maxpkt = 1467; /* This value is the default one, at least on Win2k/WinXP */
1867 data->linkspeed = 100000; /* Set the line speed in 100bit/s to 10 Mbit;
1868 * note 1MB = 1000kB in this case */
1871 case IPX_MAX_ADAPTER_NUM:
1872 FIXME("IPX_MAX_ADAPTER_NUM\n");
1873 *(int*)optval = 1; /* As noted under IPX_ADDRESS we have just one card. */
1877 FIXME("IPX optname:%x\n", optname);
1878 return SOCKET_ERROR;
1879 }/* end switch(optname) */
1880 } /* end case NSPROTO_IPX */
1882 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
1883 case WS_IPPROTO_TCP:
1886 case WS_TCP_NODELAY:
1887 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1888 return SOCKET_ERROR;
1889 convert_sockopt(&level, &optname);
1890 if (getsockopt(fd,(int) level, optname, optval,
1891 (unsigned int *)optlen) != 0 )
1893 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1896 release_sock_fd( s, fd );
1899 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname);
1900 return SOCKET_ERROR;
1905 case WS_IP_ADD_MEMBERSHIP:
1906 case WS_IP_DROP_MEMBERSHIP:
1910 case WS_IP_MULTICAST_IF:
1911 case WS_IP_MULTICAST_LOOP:
1912 case WS_IP_MULTICAST_TTL:
1916 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1917 return SOCKET_ERROR;
1918 convert_sockopt(&level, &optname);
1919 if (getsockopt(fd,(int) level, optname, optval,
1920 (unsigned int *)optlen) != 0 )
1922 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1925 release_sock_fd( s, fd );
1927 case WS_IP_DONTFRAGMENT:
1928 FIXME("WS_IP_DONTFRAGMENT is always false!\n");
1929 *(BOOL*)optval = FALSE;
1932 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname);
1933 return SOCKET_ERROR;
1936 FIXME("Unknown level: 0x%08x\n", level);
1937 return SOCKET_ERROR;
1938 } /* end switch(level) */
1941 /***********************************************************************
1945 WS_u_long WINAPI WS_htonl(WS_u_long hostlong)
1947 return htonl(hostlong);
1951 /***********************************************************************
1955 WS_u_short WINAPI WS_htons(WS_u_short hostshort)
1957 return htons(hostshort);
1960 /***********************************************************************
1961 * WSAHtonl (WS2_32.46)
1962 * From MSDN decription of error codes, this function should also
1963 * check if WinSock has been initialized and the socket is a valid
1964 * socket. But why? This function only translates a host byte order
1965 * u_long into a network byte order u_long...
1967 int WINAPI WSAHtonl(SOCKET s, WS_u_long hostlong, WS_u_long *lpnetlong)
1971 *lpnetlong = htonl(hostlong);
1974 WSASetLastError(WSAEFAULT);
1975 return SOCKET_ERROR;
1978 /***********************************************************************
1979 * WSAHtons (WS2_32.47)
1980 * From MSDN decription of error codes, this function should also
1981 * check if WinSock has been initialized and the socket is a valid
1982 * socket. But why? This function only translates a host byte order
1983 * u_short into a network byte order u_short...
1985 int WINAPI WSAHtons(SOCKET s, WS_u_short hostshort, WS_u_short *lpnetshort)
1990 *lpnetshort = htons(hostshort);
1993 WSASetLastError(WSAEFAULT);
1994 return SOCKET_ERROR;
1998 /***********************************************************************
1999 * inet_addr (WINSOCK.10)
2000 * inet_addr (WS2_32.11)
2002 WS_u_long WINAPI WS_inet_addr(const char *cp)
2004 if (!cp) return INADDR_NONE;
2005 return inet_addr(cp);
2009 /***********************************************************************
2010 * ntohl (WINSOCK.14)
2013 WS_u_long WINAPI WS_ntohl(WS_u_long netlong)
2015 return ntohl(netlong);
2019 /***********************************************************************
2020 * ntohs (WINSOCK.15)
2023 WS_u_short WINAPI WS_ntohs(WS_u_short netshort)
2025 return ntohs(netshort);
2029 /***********************************************************************
2030 * inet_ntoa (WS2_32.12)
2032 char* WINAPI WS_inet_ntoa(struct WS_in_addr in)
2034 /* use "buffer for dummies" here because some applications have a
2035 * propensity to decode addresses in ws_hostent structure without
2036 * saving them first...
2038 static char dbuffer[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
2040 char* s = inet_ntoa(*((struct in_addr*)&in));
2046 SetLastError(wsaErrno());
2050 /**********************************************************************
2051 * WSAIoctl (WS2_32.50)
2054 INT WINAPI WSAIoctl(SOCKET s,
2055 DWORD dwIoControlCode,
2058 LPVOID lpbOutBuffer,
2060 LPDWORD lpcbBytesReturned,
2061 LPWSAOVERLAPPED lpOverlapped,
2062 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
2064 TRACE("%ld, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
2065 s, dwIoControlCode, lpvInBuffer, cbInBuffer, lpbOutBuffer,
2066 cbOutBuffer, lpcbBytesReturned, lpOverlapped, lpCompletionRoutine);
2068 switch( dwIoControlCode )
2071 if (cbInBuffer != sizeof(WS_u_long)) {
2072 WSASetLastError(WSAEFAULT);
2073 return SOCKET_ERROR;
2075 return WS_ioctlsocket( s, WS_FIONBIO, lpvInBuffer);
2078 if (cbOutBuffer != sizeof(WS_u_long)) {
2079 WSASetLastError(WSAEFAULT);
2080 return SOCKET_ERROR;
2082 return WS_ioctlsocket( s, WS_FIONREAD, lpbOutBuffer);
2084 case WS_SIO_GET_INTERFACE_LIST:
2086 INTERFACE_INFO* intArray = (INTERFACE_INFO*)lpbOutBuffer;
2087 DWORD size, numInt, apiReturn;
2090 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
2094 WSASetLastError(WSAEFAULT);
2095 return SOCKET_ERROR;
2097 if (!lpcbBytesReturned)
2099 WSASetLastError(WSAEFAULT);
2100 return SOCKET_ERROR;
2103 fd = get_sock_fd( s, 0, NULL );
2104 if (fd == -1) return SOCKET_ERROR;
2106 apiReturn = GetAdaptersInfo(NULL, &size);
2107 if (apiReturn == ERROR_NO_DATA)
2111 else if (apiReturn == ERROR_BUFFER_OVERFLOW)
2113 PIP_ADAPTER_INFO table = HeapAlloc(GetProcessHeap(),0,size);
2117 if (GetAdaptersInfo(table, &size) == NO_ERROR)
2119 PIP_ADAPTER_INFO ptr;
2121 if (size*sizeof(INTERFACE_INFO)/sizeof(IP_ADAPTER_INFO) > cbOutBuffer)
2123 WARN("Buffer too small = %u, cbOutBuffer = %u\n", size, cbOutBuffer);
2124 HeapFree(GetProcessHeap(),0,table);
2125 release_sock_fd( s, fd );
2126 WSASetLastError(WSAEFAULT);
2127 return SOCKET_ERROR;
2129 for (ptr = table, numInt = 0; ptr;
2130 ptr = ptr->Next, intArray++, numInt++)
2132 unsigned int addr, mask, bcast;
2133 struct ifreq ifInfo;
2135 /* Socket Status Flags */
2136 lstrcpynA(ifInfo.ifr_name, ptr->AdapterName, IFNAMSIZ);
2137 if (ioctl(fd, SIOCGIFFLAGS, &ifInfo) < 0)
2139 ERR("Error obtaining status flags for socket!\n");
2140 HeapFree(GetProcessHeap(),0,table);
2141 release_sock_fd( s, fd );
2142 WSASetLastError(WSAEINVAL);
2143 return SOCKET_ERROR;
2147 /* set flags; the values of IFF_* are not the same
2148 under Linux and Windows, therefore must generate
2150 intArray->iiFlags = 0;
2151 if (ifInfo.ifr_flags & IFF_BROADCAST)
2152 intArray->iiFlags |= WS_IFF_BROADCAST;
2153 #ifdef IFF_POINTOPOINT
2154 if (ifInfo.ifr_flags & IFF_POINTOPOINT)
2155 intArray->iiFlags |= WS_IFF_POINTTOPOINT;
2157 if (ifInfo.ifr_flags & IFF_LOOPBACK)
2158 intArray->iiFlags |= WS_IFF_LOOPBACK;
2159 if (ifInfo.ifr_flags & IFF_UP)
2160 intArray->iiFlags |= WS_IFF_UP;
2161 if (ifInfo.ifr_flags & IFF_MULTICAST)
2162 intArray->iiFlags |= WS_IFF_MULTICAST;
2165 addr = inet_addr(ptr->IpAddressList.IpAddress.String);
2166 mask = inet_addr(ptr->IpAddressList.IpMask.String);
2167 bcast = addr | ~mask;
2168 intArray->iiAddress.AddressIn.sin_family = AF_INET;
2169 intArray->iiAddress.AddressIn.sin_port = 0;
2170 intArray->iiAddress.AddressIn.sin_addr.WS_s_addr =
2172 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
2173 intArray->iiNetmask.AddressIn.sin_port = 0;
2174 intArray->iiNetmask.AddressIn.sin_addr.WS_s_addr =
2176 intArray->iiBroadcastAddress.AddressIn.sin_family =
2178 intArray->iiBroadcastAddress.AddressIn.sin_port = 0;
2179 intArray->iiBroadcastAddress.AddressIn.sin_addr.
2185 ERR("Unable to get interface table!\n");
2186 release_sock_fd( s, fd );
2187 HeapFree(GetProcessHeap(),0,table);
2188 WSASetLastError(WSAEINVAL);
2189 return SOCKET_ERROR;
2191 HeapFree(GetProcessHeap(),0,table);
2195 release_sock_fd( s, fd );
2196 WSASetLastError(WSAEINVAL);
2197 return SOCKET_ERROR;
2202 ERR("Unable to get interface table!\n");
2203 release_sock_fd( s, fd );
2204 WSASetLastError(WSAEINVAL);
2205 return SOCKET_ERROR;
2207 /* Calculate the size of the array being returned */
2208 *lpcbBytesReturned = sizeof(INTERFACE_INFO) * numInt;
2209 release_sock_fd( s, fd );
2213 case WS_SIO_ADDRESS_LIST_CHANGE:
2214 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
2215 /* FIXME: error and return code depend on whether socket was created
2216 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
2219 case WS_SIO_ADDRESS_LIST_QUERY:
2223 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
2225 if (!lpcbBytesReturned)
2227 WSASetLastError(WSAEFAULT);
2228 return SOCKET_ERROR;
2231 if (GetAdaptersInfo(NULL, &size) == ERROR_BUFFER_OVERFLOW)
2233 IP_ADAPTER_INFO *p, *table = HeapAlloc(GetProcessHeap(), 0, size);
2236 if (!table || GetAdaptersInfo(table, &size))
2238 HeapFree(GetProcessHeap(), 0, table);
2239 WSASetLastError(WSAEINVAL);
2240 return SOCKET_ERROR;
2243 for (p = table, num = 0; p; p = p->Next)
2244 if (p->IpAddressList.IpAddress.String[0]) num++;
2246 need = sizeof(SOCKET_ADDRESS_LIST) + sizeof(SOCKET_ADDRESS) * (num - 1);
2247 need += sizeof(SOCKADDR) * num;
2248 *lpcbBytesReturned = need;
2250 if (need > cbOutBuffer)
2252 HeapFree(GetProcessHeap(), 0, table);
2253 WSASetLastError(WSAEFAULT);
2254 return SOCKET_ERROR;
2261 SOCKET_ADDRESS_LIST *sa_list = (SOCKET_ADDRESS_LIST *)lpbOutBuffer;
2262 SOCKADDR_IN *sockaddr;
2264 sa = sa_list->Address;
2265 sockaddr = (SOCKADDR_IN *)((char *)sa + num * sizeof(SOCKET_ADDRESS));
2266 sa_list->iAddressCount = num;
2268 for (p = table, i = 0; p; p = p->Next)
2270 if (!p->IpAddressList.IpAddress.String[0]) continue;
2272 sa[i].lpSockaddr = (SOCKADDR *)&sockaddr[i];
2273 sa[i].iSockaddrLength = sizeof(SOCKADDR);
2275 sockaddr[i].sin_family = AF_INET;
2276 sockaddr[i].sin_port = 0;
2277 sockaddr[i].sin_addr.WS_s_addr = inet_addr(p->IpAddressList.IpAddress.String);
2282 HeapFree(GetProcessHeap(), 0, table);
2287 WARN("unable to get IP address list\n");
2288 WSASetLastError(WSAEINVAL);
2289 return SOCKET_ERROR;
2293 FIXME("SIO_FLUSH: stub.\n");
2297 FIXME("unsupported WS_IOCTL cmd (%08x)\n", dwIoControlCode);
2298 WSASetLastError(WSAEOPNOTSUPP);
2299 return SOCKET_ERROR;
2306 /***********************************************************************
2307 * ioctlsocket (WS2_32.10)
2309 int WINAPI WS_ioctlsocket(SOCKET s, LONG cmd, WS_u_long *argp)
2314 TRACE("socket %04lx, cmd %08x, ptr %p\n", s, cmd, argp);
2323 if( _get_sock_mask(s) )
2325 /* AsyncSelect()'ed sockets are always nonblocking */
2326 if (*argp) return 0;
2327 SetLastError(WSAEINVAL);
2328 return SOCKET_ERROR;
2330 fd = get_sock_fd( s, 0, NULL );
2336 _enable_event(SOCKET2HANDLE(s), 0, FD_WINE_NONBLOCKING, 0);
2337 ret = fcntl( fd, F_SETFL, O_NONBLOCK );
2341 _enable_event(SOCKET2HANDLE(s), 0, 0, FD_WINE_NONBLOCKING);
2342 ret = fcntl( fd, F_SETFL, 0 );
2344 release_sock_fd( s, fd );
2346 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2348 return SOCKET_ERROR;
2355 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
2356 SetLastError(WSAEINVAL);
2357 return SOCKET_ERROR;
2359 case SIOCGIFBRDADDR:
2360 case SIOCGIFNETMASK:
2362 /* These don't need any special handling. They are used by
2363 WsControl, and are here to suppress an unnecessary warning. */
2367 /* Netscape tries hard to use bogus ioctl 0x667e */
2368 /* FIXME: 0x667e above is ('f' << 8) | 126, and is a low word of
2369 * FIONBIO (_IOW('f', 126, u_long)), how that should be handled?
2371 WARN("\tunknown WS_IOCTL cmd (%08x)\n", cmd);
2375 fd = get_sock_fd( s, 0, NULL );
2378 if( ioctl(fd, newcmd, (char*)argp ) == 0 )
2380 release_sock_fd( s, fd );
2383 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2384 release_sock_fd( s, fd );
2386 return SOCKET_ERROR;
2389 /***********************************************************************
2390 * listen (WS2_32.13)
2392 int WINAPI WS_listen(SOCKET s, int backlog)
2394 int fd = get_sock_fd( s, FILE_READ_DATA, NULL );
2396 TRACE("socket %04lx, backlog %d\n", s, backlog);
2399 if (listen(fd, backlog) == 0)
2401 release_sock_fd( s, fd );
2402 _enable_event(SOCKET2HANDLE(s), FD_ACCEPT,
2404 FD_CONNECT|FD_WINE_CONNECTED);
2407 SetLastError(wsaErrno());
2408 release_sock_fd( s, fd );
2410 return SOCKET_ERROR;
2413 /***********************************************************************
2416 int WINAPI WS_recv(SOCKET s, char *buf, int len, int flags)
2418 DWORD n, dwFlags = flags;
2424 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, NULL, NULL, NULL, NULL) == SOCKET_ERROR )
2425 return SOCKET_ERROR;
2430 /***********************************************************************
2431 * recvfrom (WS2_32.17)
2433 int WINAPI WS_recvfrom(SOCKET s, char *buf, INT len, int flags,
2434 struct WS_sockaddr *from, int *fromlen)
2436 DWORD n, dwFlags = flags;
2442 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, from, fromlen, NULL, NULL) == SOCKET_ERROR )
2443 return SOCKET_ERROR;
2448 /* allocate a poll array for the corresponding fd sets */
2449 static struct pollfd *fd_sets_to_poll( const WS_fd_set *readfds, const WS_fd_set *writefds,
2450 const WS_fd_set *exceptfds, int *count_ptr )
2452 int i, j = 0, count = 0;
2455 if (readfds) count += readfds->fd_count;
2456 if (writefds) count += writefds->fd_count;
2457 if (exceptfds) count += exceptfds->fd_count;
2459 if (!count) return NULL;
2460 if (!(fds = HeapAlloc( GetProcessHeap(), 0, count * sizeof(fds[0])))) return NULL;
2462 for (i = 0; i < readfds->fd_count; i++, j++)
2464 fds[j].fd = get_sock_fd( readfds->fd_array[i], FILE_READ_DATA, NULL );
2465 fds[j].events = POLLIN;
2469 for (i = 0; i < writefds->fd_count; i++, j++)
2471 fds[j].fd = get_sock_fd( writefds->fd_array[i], FILE_WRITE_DATA, NULL );
2472 fds[j].events = POLLOUT;
2476 for (i = 0; i < exceptfds->fd_count; i++, j++)
2478 fds[j].fd = get_sock_fd( exceptfds->fd_array[i], 0, NULL );
2479 fds[j].events = POLLHUP;
2485 /* release the file descriptor obtained in fd_sets_to_poll */
2486 /* must be called with the original fd_set arrays, before calling get_poll_results */
2487 static void release_poll_fds( const WS_fd_set *readfds, const WS_fd_set *writefds,
2488 const WS_fd_set *exceptfds, struct pollfd *fds )
2494 for (i = 0; i < readfds->fd_count; i++, j++)
2495 if (fds[j].fd != -1) release_sock_fd( readfds->fd_array[i], fds[j].fd );
2499 for (i = 0; i < writefds->fd_count; i++, j++)
2500 if (fds[j].fd != -1) release_sock_fd( writefds->fd_array[i], fds[j].fd );
2504 for (i = 0; i < exceptfds->fd_count; i++, j++)
2505 if (fds[j].fd != -1)
2507 /* make sure we have a real error before releasing the fd */
2508 if (!sock_error_p( fds[j].fd )) fds[j].revents = 0;
2509 release_sock_fd( exceptfds->fd_array[i], fds[j].fd );
2514 /* map the poll results back into the Windows fd sets */
2515 static int get_poll_results( WS_fd_set *readfds, WS_fd_set *writefds, WS_fd_set *exceptfds,
2516 const struct pollfd *fds )
2518 int i, j = 0, k, total = 0;
2522 for (i = k = 0; i < readfds->fd_count; i++, j++)
2523 if (fds[j].revents) readfds->fd_array[k++] = readfds->fd_array[i];
2524 readfds->fd_count = k;
2529 for (i = k = 0; i < writefds->fd_count; i++, j++)
2530 if (fds[j].revents) writefds->fd_array[k++] = writefds->fd_array[i];
2531 writefds->fd_count = k;
2536 for (i = k = 0; i < exceptfds->fd_count; i++, j++)
2537 if (fds[j].revents) exceptfds->fd_array[k++] = exceptfds->fd_array[i];
2538 exceptfds->fd_count = k;
2545 /***********************************************************************
2546 * select (WS2_32.18)
2548 int WINAPI WS_select(int nfds, WS_fd_set *ws_readfds,
2549 WS_fd_set *ws_writefds, WS_fd_set *ws_exceptfds,
2550 const struct WS_timeval* ws_timeout)
2552 struct pollfd *pollfds;
2553 int count, ret, timeout = -1;
2555 TRACE("read %p, write %p, excp %p timeout %p\n",
2556 ws_readfds, ws_writefds, ws_exceptfds, ws_timeout);
2558 if (!(pollfds = fd_sets_to_poll( ws_readfds, ws_writefds, ws_exceptfds, &count )) && count)
2560 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
2561 return SOCKET_ERROR;
2564 if (ws_timeout) timeout = (ws_timeout->tv_sec * 1000) + (ws_timeout->tv_usec + 999) / 1000;
2566 ret = poll( pollfds, count, timeout );
2567 release_poll_fds( ws_readfds, ws_writefds, ws_exceptfds, pollfds );
2569 if (ret == -1) SetLastError(wsaErrno());
2570 else ret = get_poll_results( ws_readfds, ws_writefds, ws_exceptfds, pollfds );
2571 HeapFree( GetProcessHeap(), 0, pollfds );
2576 /***********************************************************************
2579 int WINAPI WS_send(SOCKET s, const char *buf, int len, int flags)
2585 wsabuf.buf = (char*) buf;
2587 if ( WSASendTo( s, &wsabuf, 1, &n, flags, NULL, 0, NULL, NULL) == SOCKET_ERROR )
2588 return SOCKET_ERROR;
2593 /***********************************************************************
2594 * WSASend (WS2_32.72)
2596 INT WINAPI WSASend( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2597 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2598 LPWSAOVERLAPPED lpOverlapped,
2599 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2601 return WSASendTo( s, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags,
2602 NULL, 0, lpOverlapped, lpCompletionRoutine );
2605 /***********************************************************************
2606 * WSASendDisconnect (WS2_32.73)
2608 INT WINAPI WSASendDisconnect( SOCKET s, LPWSABUF lpBuffers )
2610 return WS_shutdown( s, SD_SEND );
2614 /***********************************************************************
2615 * WSASendTo (WS2_32.74)
2617 INT WINAPI WSASendTo( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2618 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2619 const struct WS_sockaddr *to, int tolen,
2620 LPWSAOVERLAPPED lpOverlapped,
2621 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2623 unsigned int i, options;
2625 struct iovec iovec[WS_MSG_MAXIOVLEN];
2627 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
2628 s, lpBuffers, dwBufferCount, dwFlags,
2629 to, tolen, lpOverlapped, lpCompletionRoutine);
2631 if (dwBufferCount > WS_MSG_MAXIOVLEN)
2633 WSASetLastError( WSAEINVAL );
2634 return SOCKET_ERROR;
2637 fd = get_sock_fd( s, FILE_WRITE_DATA, &options );
2638 TRACE( "fd=%d, options=%x\n", fd, options );
2640 if ( fd == -1 ) return SOCKET_ERROR;
2642 if ( !lpNumberOfBytesSent )
2648 for ( i = 0; i < dwBufferCount; i++ )
2650 iovec[i].iov_base = lpBuffers[i].buf;
2651 iovec[i].iov_len = lpBuffers[i].len;
2656 n = WS2_send( fd, iovec, dwBufferCount, to, tolen, dwFlags );
2657 if (n != -1 || errno != EINTR) break;
2659 if (n == -1 && errno != EAGAIN)
2665 if ((lpOverlapped || lpCompletionRoutine) &&
2666 !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
2668 IO_STATUS_BLOCK *iosb;
2669 struct ws2_async *wsa = HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa) );
2676 release_sock_fd( s, fd );
2678 wsa->hSocket = SOCKET2HANDLE(s);
2679 wsa->addr = (struct WS_sockaddr *)to;
2680 wsa->addrlen.val = tolen;
2682 wsa->user_overlapped = lpOverlapped;
2683 wsa->completion_func = lpCompletionRoutine;
2684 wsa->n_iovecs = dwBufferCount;
2685 memcpy( wsa->iovec, iovec, dwBufferCount * sizeof(*iovec) );
2687 iosb = lpOverlapped ? (IO_STATUS_BLOCK *)lpOverlapped : &wsa->local_iosb;
2690 iosb->u.Status = STATUS_PENDING;
2691 iosb->Information = 0;
2693 SERVER_START_REQ( register_async )
2695 req->handle = wsa->hSocket;
2696 req->type = ASYNC_TYPE_WRITE;
2697 req->async.callback = WS2_async_send;
2698 req->async.iosb = iosb;
2699 req->async.arg = wsa;
2700 req->async.apc = ws2_async_apc;
2701 req->async.event = lpCompletionRoutine ? 0 : lpOverlapped->hEvent;
2702 err = wine_server_call( req );
2706 if (err != STATUS_PENDING) HeapFree( GetProcessHeap(), 0, wsa );
2707 WSASetLastError( NtStatusToWSAError( err ));
2708 return SOCKET_ERROR;
2711 iosb->u.Status = STATUS_SUCCESS;
2712 iosb->Information = n;
2713 *lpNumberOfBytesSent = n;
2714 if (!wsa->completion_func)
2716 SetEvent( lpOverlapped->hEvent );
2717 HeapFree( GetProcessHeap(), 0, wsa );
2719 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)ws2_async_apc,
2720 (ULONG_PTR)wsa, (ULONG_PTR)iosb, 0 );
2724 if ( _is_blocking(s) )
2726 /* On a blocking non-overlapped stream socket,
2727 * sending blocks until the entire buffer is sent. */
2728 DWORD timeout_start = GetTickCount();
2729 unsigned int first_buff = 0;
2731 *lpNumberOfBytesSent = 0;
2733 while (first_buff < dwBufferCount)
2736 int timeout = GET_SNDTIMEO(fd);
2740 *lpNumberOfBytesSent += n;
2741 while (first_buff < dwBufferCount && iovec[first_buff].iov_len <= n)
2742 n -= iovec[first_buff++].iov_len;
2743 if (first_buff >= dwBufferCount) break;
2744 iovec[first_buff].iov_base = (char*)iovec[first_buff].iov_base + n;
2745 iovec[first_buff].iov_len -= n;
2750 timeout -= timeout_start - GetTickCount();
2751 if (timeout < 0) timeout = 0;
2755 pfd.events = POLLOUT;
2757 if (!timeout || !poll( &pfd, 1, timeout ))
2760 goto error; /* msdn says a timeout in send is fatal */
2763 n = WS2_send( fd, iovec + first_buff, dwBufferCount - first_buff, to, tolen, dwFlags );
2764 if (n == -1 && errno != EAGAIN && errno != EINTR)
2771 else /* non-blocking */
2773 _enable_event(SOCKET2HANDLE(s), FD_WRITE, 0, 0);
2776 err = WSAEWOULDBLOCK;
2779 *lpNumberOfBytesSent = n;
2782 TRACE(" -> %i bytes\n", *lpNumberOfBytesSent);
2784 release_sock_fd( s, fd );
2788 release_sock_fd( s, fd );
2789 WARN(" -> ERROR %d\n", err);
2790 WSASetLastError(err);
2791 return SOCKET_ERROR;
2794 /***********************************************************************
2795 * sendto (WS2_32.20)
2797 int WINAPI WS_sendto(SOCKET s, const char *buf, int len, int flags,
2798 const struct WS_sockaddr *to, int tolen)
2804 wsabuf.buf = (char*) buf;
2806 if ( WSASendTo(s, &wsabuf, 1, &n, flags, to, tolen, NULL, NULL) == SOCKET_ERROR )
2807 return SOCKET_ERROR;
2812 /***********************************************************************
2813 * setsockopt (WS2_32.21)
2815 int WINAPI WS_setsockopt(SOCKET s, int level, int optname,
2816 const char *optval, int optlen)
2820 struct linger linger;
2821 struct timeval tval;
2823 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
2824 s, level, optname, optval, optlen);
2831 /* Some options need some conversion before they can be sent to
2832 * setsockopt. The conversions are done here, then they will fall though
2833 * to the general case. Special options that are not passed to
2834 * setsockopt follow below that.*/
2836 case WS_SO_DONTLINGER:
2837 linger.l_onoff = *((const int*)optval) ? 0: 1;
2838 linger.l_linger = 0;
2840 optname = SO_LINGER;
2841 optval = (char*)&linger;
2842 optlen = sizeof(struct linger);
2846 linger.l_onoff = ((LINGER*)optval)->l_onoff;
2847 linger.l_linger = ((LINGER*)optval)->l_linger;
2848 /* FIXME: what is documented behavior if SO_LINGER optval
2851 optname = SO_LINGER;
2852 optval = (char*)&linger;
2853 optlen = sizeof(struct linger);
2857 if (*(const int*)optval < 2048)
2859 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval );
2864 /* The options listed here don't need any special handling. Thanks to
2865 * the conversion happening above, options from there will fall through
2867 case WS_SO_ACCEPTCONN:
2868 case WS_SO_BROADCAST:
2870 case WS_SO_KEEPALIVE:
2871 case WS_SO_OOBINLINE:
2874 convert_sockopt(&level, &optname);
2877 /* SO_DEBUG is a privileged operation, ignore it. */
2879 TRACE("Ignoring SO_DEBUG\n");
2882 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
2883 * socket. According to MSDN, this option is silently ignored.*/
2884 case WS_SO_DONTROUTE:
2885 TRACE("Ignoring SO_DONTROUTE\n");
2888 /* Stops two sockets from being bound to the same port. Always happens
2889 * on unix systems, so just drop it. */
2890 case WS_SO_EXCLUSIVEADDRUSE:
2891 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
2894 /* SO_OPENTYPE does not require a valid socket handle. */
2895 case WS_SO_OPENTYPE:
2896 if (!optlen || optlen < sizeof(int) || !optval)
2898 SetLastError(WSAEFAULT);
2899 return SOCKET_ERROR;
2901 get_per_thread_data()->opentype = *(const int *)optval;
2902 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
2905 /* SO_REUSEADDR allows two applications to bind to the same port at at
2906 * same time. There is no direct way to do that in unix. While Wineserver
2907 * might do this, it does not seem useful for now, so just ignore it.*/
2908 case WS_SO_REUSEADDR:
2909 TRACE("Ignoring SO_REUSEADDR, does not translate\n");
2913 case WS_SO_RCVTIMEO:
2916 case WS_SO_SNDTIMEO:
2918 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
2919 if (optval && optlen == sizeof(UINT32)) {
2920 /* WinSock passes miliseconds instead of struct timeval */
2921 tval.tv_usec = (*(const UINT32*)optval % 1000) * 1000;
2922 tval.tv_sec = *(const UINT32*)optval / 1000;
2923 /* min of 500 milisec */
2924 if (tval.tv_sec == 0 && tval.tv_usec < 500000)
2925 tval.tv_usec = 500000;
2926 optlen = sizeof(struct timeval);
2927 optval = (char*)&tval;
2928 } else if (optlen == sizeof(struct timeval)) {
2929 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen);
2931 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen);
2934 convert_sockopt(&level, &optname);
2939 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname);
2940 SetLastError(WSAENOPROTOOPT);
2941 return SOCKET_ERROR;
2943 break; /* case WS_SOL_SOCKET */
2950 fd = get_sock_fd( s, 0, NULL );
2951 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval, fd);
2953 /* We try to set the ipx type on ipx socket level. */
2955 if(setsockopt(fd, SOL_IPX, IPX_TYPE, optval, optlen) == -1)
2957 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
2958 release_sock_fd( s, fd );
2959 return SOCKET_ERROR;
2964 /* Should we retrieve val using a getsockopt call and then
2965 * set the modified one? */
2966 val.ipx_pt = *optval;
2967 setsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, sizeof(struct ipx));
2970 release_sock_fd( s, fd );
2973 case IPX_FILTERPTYPE:
2974 /* Sets the receive filter packet type, at the moment we don't support it */
2975 FIXME("IPX_FILTERPTYPE: %x\n", *optval);
2976 /* Returning 0 is better for now than returning a SOCKET_ERROR */
2980 FIXME("opt_name:%x\n", optname);
2981 return SOCKET_ERROR;
2983 break; /* case NSPROTO_IPX */
2986 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
2987 case WS_IPPROTO_TCP:
2990 case WS_TCP_NODELAY:
2991 convert_sockopt(&level, &optname);
2994 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname);
2995 return SOCKET_ERROR;
3002 case WS_IP_ADD_MEMBERSHIP:
3003 case WS_IP_DROP_MEMBERSHIP:
3007 case WS_IP_MULTICAST_IF:
3008 case WS_IP_MULTICAST_LOOP:
3009 case WS_IP_MULTICAST_TTL:
3013 convert_sockopt(&level, &optname);
3015 case WS_IP_DONTFRAGMENT:
3016 FIXME("IP_DONTFRAGMENT is silently ignored!\n");
3019 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname);
3020 return SOCKET_ERROR;
3025 FIXME("Unknown level: 0x%08x\n", level);
3026 return SOCKET_ERROR;
3027 } /* end switch(level) */
3029 /* avoid endianness issues if argument is a 16-bit int */
3030 if (optval && optlen < sizeof(int))
3032 woptval= *((const INT16 *) optval);
3033 optval= (char*) &woptval;
3036 fd = get_sock_fd( s, 0, NULL );
3037 if (fd == -1) return SOCKET_ERROR;
3039 if (setsockopt(fd, level, optname, optval, optlen) == 0)
3041 release_sock_fd( s, fd );
3044 TRACE("Setting socket error, %d\n", wsaErrno());
3045 SetLastError(wsaErrno());
3046 release_sock_fd( s, fd );
3048 return SOCKET_ERROR;
3051 /***********************************************************************
3052 * shutdown (WS2_32.22)
3054 int WINAPI WS_shutdown(SOCKET s, int how)
3056 int fd, err = WSAENOTSOCK;
3057 unsigned int options, clear_flags = 0;
3059 fd = get_sock_fd( s, 0, &options );
3060 TRACE("socket %04lx, how %i %x\n", s, how, options );
3063 return SOCKET_ERROR;
3067 case 0: /* drop receives */
3068 clear_flags |= FD_READ;
3070 case 1: /* drop sends */
3071 clear_flags |= FD_WRITE;
3073 case 2: /* drop all */
3074 clear_flags |= FD_READ|FD_WRITE;
3076 clear_flags |= FD_WINE_LISTENING;
3079 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
3084 err = WS2_register_async_shutdown( s, ASYNC_TYPE_READ );
3087 err = WS2_register_async_shutdown( s, ASYNC_TYPE_WRITE );
3091 err = WS2_register_async_shutdown( s, ASYNC_TYPE_READ );
3092 if (!err) err = WS2_register_async_shutdown( s, ASYNC_TYPE_WRITE );
3095 if (err) goto error;
3097 else /* non-overlapped mode */
3099 if ( shutdown( fd, how ) )
3106 release_sock_fd( s, fd );
3107 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
3108 if ( how > 1) WSAAsyncSelect( s, 0, 0, 0 );
3112 release_sock_fd( s, fd );
3113 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
3114 WSASetLastError( err );
3115 return SOCKET_ERROR;
3118 /***********************************************************************
3119 * socket (WS2_32.23)
3121 SOCKET WINAPI WS_socket(int af, int type, int protocol)
3123 TRACE("af=%d type=%d protocol=%d\n", af, type, protocol);
3125 return WSASocketA( af, type, protocol, NULL, 0,
3126 get_per_thread_data()->opentype ? 0 : WSA_FLAG_OVERLAPPED );
3130 /***********************************************************************
3131 * gethostbyaddr (WS2_32.51)
3133 struct WS_hostent* WINAPI WS_gethostbyaddr(const char *addr, int len, int type)
3135 struct WS_hostent *retval = NULL;
3136 struct hostent* host;
3138 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3141 struct hostent hostentry;
3144 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
3146 int res = gethostbyaddr_r(addr, len, type,
3147 &hostentry, extrabuf, ebufsize, &host, &locerr);
3148 if( res != ERANGE) break;
3150 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
3152 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
3154 EnterCriticalSection( &csWSgetXXXbyYYY );
3155 host = gethostbyaddr(addr, len, type);
3156 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
3158 if( host != NULL ) retval = WS_dup_he(host);
3159 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3160 HeapFree(GetProcessHeap(),0,extrabuf);
3162 LeaveCriticalSection( &csWSgetXXXbyYYY );
3164 TRACE("ptr %p, len %d, type %d ret %p\n", addr, len, type, retval);
3168 /***********************************************************************
3169 * gethostbyname (WS2_32.52)
3171 struct WS_hostent* WINAPI WS_gethostbyname(const char* name)
3173 struct WS_hostent *retval = NULL;
3174 struct hostent* host;
3175 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3178 struct hostent hostentry;
3179 int locerr = ENOBUFS;
3182 if( !name || !name[0]) {
3184 if( gethostname( buf, 100) == -1) {
3185 SetLastError( WSAENOBUFS); /* appropriate ? */
3189 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3191 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
3193 int res = gethostbyname_r(name, &hostentry, extrabuf, ebufsize, &host, &locerr);
3194 if( res != ERANGE) break;
3196 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
3198 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
3200 EnterCriticalSection( &csWSgetXXXbyYYY );
3201 host = gethostbyname(name);
3202 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
3204 if (host) retval = WS_dup_he(host);
3205 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3206 HeapFree(GetProcessHeap(),0,extrabuf);
3208 LeaveCriticalSection( &csWSgetXXXbyYYY );
3210 TRACE( "%s ret %p\n", debugstr_a(name), retval );
3215 /***********************************************************************
3216 * getprotobyname (WS2_32.53)
3218 struct WS_protoent* WINAPI WS_getprotobyname(const char* name)
3220 struct WS_protoent* retval = NULL;
3221 #ifdef HAVE_GETPROTOBYNAME
3222 struct protoent* proto;
3223 EnterCriticalSection( &csWSgetXXXbyYYY );
3224 if( (proto = getprotobyname(name)) != NULL )
3226 retval = WS_dup_pe(proto);
3229 MESSAGE("protocol %s not found; You might want to add "
3230 "this to /etc/protocols\n", debugstr_a(name) );
3231 SetLastError(WSANO_DATA);
3233 LeaveCriticalSection( &csWSgetXXXbyYYY );
3235 TRACE( "%s ret %p\n", debugstr_a(name), retval );
3240 /***********************************************************************
3241 * getprotobynumber (WS2_32.54)
3243 struct WS_protoent* WINAPI WS_getprotobynumber(int number)
3245 struct WS_protoent* retval = NULL;
3246 #ifdef HAVE_GETPROTOBYNUMBER
3247 struct protoent* proto;
3248 EnterCriticalSection( &csWSgetXXXbyYYY );
3249 if( (proto = getprotobynumber(number)) != NULL )
3251 retval = WS_dup_pe(proto);
3254 MESSAGE("protocol number %d not found; You might want to add "
3255 "this to /etc/protocols\n", number );
3256 SetLastError(WSANO_DATA);
3258 LeaveCriticalSection( &csWSgetXXXbyYYY );
3260 TRACE("%i ret %p\n", number, retval);
3265 /***********************************************************************
3266 * getservbyname (WS2_32.55)
3268 struct WS_servent* WINAPI WS_getservbyname(const char *name, const char *proto)
3270 struct WS_servent* retval = NULL;
3271 struct servent* serv;
3273 char *proto_str = NULL;
3275 if (!(name_str = strdup_lower(name))) return NULL;
3277 if (proto && *proto)
3279 if (!(proto_str = strdup_lower(proto)))
3281 HeapFree( GetProcessHeap(), 0, name_str );
3286 EnterCriticalSection( &csWSgetXXXbyYYY );
3287 serv = getservbyname(name_str, proto_str);
3290 retval = WS_dup_se(serv);
3292 else SetLastError(WSANO_DATA);
3293 LeaveCriticalSection( &csWSgetXXXbyYYY );
3294 HeapFree( GetProcessHeap(), 0, proto_str );
3295 HeapFree( GetProcessHeap(), 0, name_str );
3296 TRACE( "%s, %s ret %p\n", debugstr_a(name), debugstr_a(proto), retval );
3300 /***********************************************************************
3301 * freeaddrinfo (WS2_32.@)
3303 void WINAPI WS_freeaddrinfo(struct WS_addrinfo *res)
3306 struct WS_addrinfo *next;
3308 HeapFree(GetProcessHeap(),0,res->ai_canonname);
3309 HeapFree(GetProcessHeap(),0,res->ai_addr);
3310 next = res->ai_next;
3311 HeapFree(GetProcessHeap(),0,res);
3316 /* helper functions for getaddrinfo() */
3317 static int convert_aiflag_w2u(int winflags) {
3318 int i, unixflags = 0;
3320 for (i=0;i<sizeof(ws_aiflag_map)/sizeof(ws_aiflag_map[0]);i++)
3321 if (ws_aiflag_map[i][0] & winflags) {
3322 unixflags |= ws_aiflag_map[i][1];
3323 winflags &= ~ws_aiflag_map[i][0];
3326 FIXME("Unhandled windows AI_xxx flags %x\n", winflags);
3330 static int convert_aiflag_u2w(int unixflags) {
3331 int i, winflags = 0;
3333 for (i=0;i<sizeof(ws_aiflag_map)/sizeof(ws_aiflag_map[0]);i++)
3334 if (ws_aiflag_map[i][1] & unixflags) {
3335 winflags |= ws_aiflag_map[i][0];
3336 unixflags &= ~ws_aiflag_map[i][1];
3338 if (unixflags) /* will warn usually */
3339 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags);
3343 static int convert_eai_u2w(int unixret) {
3346 for (i=0;ws_eai_map[i][0];i++)
3347 if (ws_eai_map[i][1] == unixret)
3348 return ws_eai_map[i][0];
3352 /***********************************************************************
3353 * getaddrinfo (WS2_32.@)
3355 int WINAPI WS_getaddrinfo(LPCSTR nodename, LPCSTR servname, const struct WS_addrinfo *hints, struct WS_addrinfo **res)
3357 #if HAVE_GETADDRINFO
3358 struct addrinfo *unixaires = NULL;
3360 struct addrinfo unixhints, *punixhints = NULL;
3361 CHAR *node = NULL, *serv = NULL;
3364 if (!(node = strdup_lower(nodename))) return WSA_NOT_ENOUGH_MEMORY;
3367 if (!(serv = strdup_lower(servname))) {
3368 HeapFree(GetProcessHeap(), 0, node);
3369 return WSA_NOT_ENOUGH_MEMORY;
3374 punixhints = &unixhints;
3376 memset(&unixhints, 0, sizeof(unixhints));
3377 punixhints->ai_flags = convert_aiflag_w2u(hints->ai_flags);
3378 if (hints->ai_family == 0) /* wildcard, specific to getaddrinfo() */
3379 punixhints->ai_family = 0;
3381 punixhints->ai_family = convert_af_w2u(hints->ai_family);
3382 if (hints->ai_socktype == 0) /* wildcard, specific to getaddrinfo() */
3383 punixhints->ai_socktype = 0;
3385 punixhints->ai_socktype = convert_socktype_w2u(hints->ai_socktype);
3386 if (hints->ai_protocol == 0) /* wildcard, specific to getaddrinfo() */
3387 punixhints->ai_protocol = 0;
3389 punixhints->ai_protocol = convert_proto_w2u(hints->ai_protocol);
3392 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
3393 result = getaddrinfo(nodename, servname, punixhints, &unixaires);
3395 TRACE("%s, %s %p -> %p %d\n", nodename, servname, hints, res, result);
3397 HeapFree(GetProcessHeap(), 0, node);
3398 HeapFree(GetProcessHeap(), 0, serv);
3401 struct addrinfo *xuai = unixaires;
3402 struct WS_addrinfo **xai = res;
3406 struct WS_addrinfo *ai = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, sizeof(struct WS_addrinfo));
3412 *xai = ai;xai = &ai->ai_next;
3413 ai->ai_flags = convert_aiflag_u2w(xuai->ai_flags);
3414 ai->ai_family = convert_af_u2w(xuai->ai_family);
3415 ai->ai_socktype = convert_socktype_u2w(xuai->ai_socktype);
3416 ai->ai_protocol = convert_proto_u2w(xuai->ai_protocol);
3417 if (xuai->ai_canonname) {
3418 TRACE("canon name - %s\n",debugstr_a(xuai->ai_canonname));
3419 ai->ai_canonname = HeapAlloc(GetProcessHeap(),0,strlen(xuai->ai_canonname)+1);
3420 if (!ai->ai_canonname)
3422 strcpy(ai->ai_canonname,xuai->ai_canonname);
3424 len = xuai->ai_addrlen;
3425 ai->ai_addr = HeapAlloc(GetProcessHeap(),0,len);
3428 ai->ai_addrlen = len;
3430 int winlen = ai->ai_addrlen;
3432 if (!ws_sockaddr_u2ws(xuai->ai_addr, ai->ai_addr, &winlen)) {
3433 ai->ai_addrlen = winlen;
3437 ai->ai_addr = HeapReAlloc(GetProcessHeap(),0,ai->ai_addr,len);
3440 ai->ai_addrlen = len;
3442 xuai = xuai->ai_next;
3444 freeaddrinfo(unixaires);
3446 result = convert_eai_u2w(result);
3451 if (*res) WS_freeaddrinfo(*res);
3452 if (unixaires) freeaddrinfo(unixaires);
3454 return WSA_NOT_ENOUGH_MEMORY;
3456 FIXME("getaddrinfo() failed, not found during buildtime.\n");
3461 /***********************************************************************
3462 * GetAddrInfoW (WS2_32.@)
3464 int WINAPI GetAddrInfoW(LPCWSTR nodename, LPCWSTR servname, const ADDRINFOW *hints, PADDRINFOW *res)
3466 FIXME("empty stub!\n");
3470 int WINAPI WS_getnameinfo(const SOCKADDR *sa, WS_socklen_t salen, PCHAR host,
3471 DWORD hostlen, PCHAR serv, DWORD servlen, INT flags)
3473 #if HAVE_GETNAMEINFO
3475 union generic_unix_sockaddr sa_u;
3478 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa), salen, host, hostlen,
3479 serv, servlen, flags);
3481 size = ws_sockaddr_ws2u(sa, salen, &sa_u);
3484 WSASetLastError(WSAEFAULT);
3485 return WSA_NOT_ENOUGH_MEMORY;
3487 ret = getnameinfo(&sa_u.addr, size, host, hostlen, serv, servlen, convert_aiflag_w2u(flags));
3488 return convert_eai_u2w(ret);
3490 FIXME("getnameinfo() failed, not found during buildtime.\n");
3495 /***********************************************************************
3496 * getservbyport (WS2_32.56)
3498 struct WS_servent* WINAPI WS_getservbyport(int port, const char *proto)
3500 struct WS_servent* retval = NULL;
3501 #ifdef HAVE_GETSERVBYPORT
3502 struct servent* serv;
3503 char *proto_str = NULL;
3505 if (proto && *proto)
3507 if (!(proto_str = strdup_lower(proto))) return NULL;
3509 EnterCriticalSection( &csWSgetXXXbyYYY );
3510 if( (serv = getservbyport(port, proto_str)) != NULL ) {
3511 retval = WS_dup_se(serv);
3513 else SetLastError(WSANO_DATA);
3514 LeaveCriticalSection( &csWSgetXXXbyYYY );
3515 HeapFree( GetProcessHeap(), 0, proto_str );
3517 TRACE("%d (i.e. port %d), %s ret %p\n", port, (int)ntohl(port), debugstr_a(proto), retval);
3522 /***********************************************************************
3523 * gethostname (WS2_32.57)
3525 int WINAPI WS_gethostname(char *name, int namelen)
3527 TRACE("name %p, len %d\n", name, namelen);
3529 if (gethostname(name, namelen) == 0)
3531 TRACE("<- '%s'\n", name);
3534 SetLastError((errno == EINVAL) ? WSAEFAULT : wsaErrno());
3535 TRACE("<- ERROR !\n");
3536 return SOCKET_ERROR;
3540 /* ------------------------------------- Windows sockets extensions -- *
3542 * ------------------------------------------------------------------- */
3544 /***********************************************************************
3545 * WSAEnumNetworkEvents (WS2_32.36)
3547 int WINAPI WSAEnumNetworkEvents(SOCKET s, WSAEVENT hEvent, LPWSANETWORKEVENTS lpEvent)
3551 TRACE("%08lx, hEvent %p, lpEvent %p\n", s, hEvent, lpEvent );
3553 SERVER_START_REQ( get_socket_event )
3555 req->handle = SOCKET2HANDLE(s);
3556 req->service = TRUE;
3557 req->c_event = hEvent;
3558 wine_server_set_reply( req, lpEvent->iErrorCode, sizeof(lpEvent->iErrorCode) );
3559 if (!(ret = wine_server_call(req))) lpEvent->lNetworkEvents = reply->pmask & reply->mask;
3563 SetLastError(WSAEINVAL);
3564 return SOCKET_ERROR;
3567 /***********************************************************************
3568 * WSAEventSelect (WS2_32.39)
3570 int WINAPI WSAEventSelect(SOCKET s, WSAEVENT hEvent, LONG lEvent)
3574 TRACE("%08lx, hEvent %p, event %08x\n", s, hEvent, lEvent);
3576 SERVER_START_REQ( set_socket_event )
3578 req->handle = SOCKET2HANDLE(s);
3580 req->event = hEvent;
3583 ret = wine_server_call( req );
3587 SetLastError(WSAEINVAL);
3588 return SOCKET_ERROR;
3591 /**********************************************************************
3592 * WSAGetOverlappedResult (WS2_32.40)
3594 BOOL WINAPI WSAGetOverlappedResult( SOCKET s, LPWSAOVERLAPPED lpOverlapped,
3595 LPDWORD lpcbTransfer, BOOL fWait,
3600 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
3601 s, lpOverlapped, lpcbTransfer, fWait, lpdwFlags );
3603 if ( lpOverlapped == NULL )
3605 ERR( "Invalid pointer\n" );
3606 WSASetLastError(WSA_INVALID_PARAMETER);
3610 status = lpOverlapped->Internal;
3611 if (status == STATUS_PENDING)
3615 SetLastError( WSA_IO_INCOMPLETE );
3619 if (WaitForSingleObject( lpOverlapped->hEvent ? lpOverlapped->hEvent : SOCKET2HANDLE(s),
3620 INFINITE ) == WAIT_FAILED)
3622 status = lpOverlapped->Internal;
3626 *lpcbTransfer = lpOverlapped->InternalHigh;
3629 *lpdwFlags = lpOverlapped->u.s.Offset;
3631 if (status) SetLastError( RtlNtStatusToDosError(status) );
3636 /***********************************************************************
3637 * WSAAsyncSelect (WS2_32.101)
3639 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, LONG lEvent)
3643 TRACE("%lx, hWnd %p, uMsg %08x, event %08x\n", s, hWnd, uMsg, lEvent);
3645 SERVER_START_REQ( set_socket_event )
3647 req->handle = SOCKET2HANDLE(s);
3652 ret = wine_server_call( req );
3656 SetLastError(WSAEINVAL);
3657 return SOCKET_ERROR;
3660 /***********************************************************************
3661 * WSACreateEvent (WS2_32.31)
3664 WSAEVENT WINAPI WSACreateEvent(void)
3666 /* Create a manual-reset event, with initial state: unsignaled */
3669 return CreateEventW(NULL, TRUE, FALSE, NULL);
3672 /***********************************************************************
3673 * WSACloseEvent (WS2_32.29)
3676 BOOL WINAPI WSACloseEvent(WSAEVENT event)
3678 TRACE ("event=%p\n", event);
3680 return CloseHandle(event);
3683 /***********************************************************************
3684 * WSASocketA (WS2_32.78)
3687 SOCKET WINAPI WSASocketA(int af, int type, int protocol,
3688 LPWSAPROTOCOL_INFOA lpProtocolInfo,
3689 GROUP g, DWORD dwFlags)
3692 WSAPROTOCOL_INFOW info;
3694 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3695 af, type, protocol, lpProtocolInfo, g, dwFlags);
3697 if (!lpProtocolInfo) return WSASocketW(af, type, protocol, NULL, g, dwFlags);
3699 memcpy(&info, lpProtocolInfo, FIELD_OFFSET(WSAPROTOCOL_INFOW, szProtocol));
3700 len = MultiByteToWideChar(CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
3701 info.szProtocol, WSAPROTOCOL_LEN + 1);
3705 WSASetLastError( WSAEINVAL);
3706 return SOCKET_ERROR;
3709 return WSASocketW(af, type, protocol, &info, g, dwFlags);
3712 /***********************************************************************
3713 * WSASocketW (WS2_32.79)
3716 SOCKET WINAPI WSASocketW(int af, int type, int protocol,
3717 LPWSAPROTOCOL_INFOW lpProtocolInfo,
3718 GROUP g, DWORD dwFlags)
3723 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
3724 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
3727 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3728 af, type, protocol, lpProtocolInfo, g, dwFlags );
3730 /* hack for WSADuplicateSocket */
3731 if (lpProtocolInfo && lpProtocolInfo->dwServiceFlags4 == 0xff00ff00) {
3732 ret = lpProtocolInfo->dwCatalogEntryId;
3733 TRACE("\tgot duplicate %04lx\n", ret);
3737 /* check and convert the socket family */
3738 af = convert_af_w2u(af);
3741 FIXME("Unsupported socket family %d!\n", af);
3742 SetLastError(WSAEAFNOSUPPORT);
3743 return INVALID_SOCKET;
3746 /* check the socket type */
3747 type = convert_socktype_w2u(type);
3750 SetLastError(WSAESOCKTNOSUPPORT);
3751 return INVALID_SOCKET;
3754 /* check the protocol type */
3755 if ( protocol < 0 ) /* don't support negative values */
3757 SetLastError(WSAEPROTONOSUPPORT);
3758 return INVALID_SOCKET;
3761 if ( af == AF_UNSPEC) /* did they not specify the address family? */
3765 if (type == SOCK_STREAM) { af = AF_INET; break; }
3767 if (type == SOCK_DGRAM) { af = AF_INET; break; }
3768 default: SetLastError(WSAEPROTOTYPE); return INVALID_SOCKET;
3771 SERVER_START_REQ( create_socket )
3775 req->protocol = protocol;
3776 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
3777 req->attributes = OBJ_INHERIT;
3778 req->flags = dwFlags;
3779 set_error( wine_server_call( req ) );
3780 ret = HANDLE2SOCKET( reply->handle );
3785 TRACE("\tcreated %04lx\n", ret );
3789 if (GetLastError() == WSAEACCES) /* raw socket denied */
3791 if (type == SOCK_RAW)
3792 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, this"
3793 " will fail unless you have special permissions.\n");
3795 MESSAGE("WS_SOCKET: Failed to create socket, this requires"
3796 " special permissions.\n");
3797 SetLastError(WSAESOCKTNOSUPPORT);
3800 WARN("\t\tfailed!\n");
3801 return INVALID_SOCKET;
3804 /***********************************************************************
3805 * WSAJoinLeaf (WS2_32.58)
3808 SOCKET WINAPI WSAJoinLeaf(
3810 const struct WS_sockaddr *addr,
3812 LPWSABUF lpCallerData,
3813 LPWSABUF lpCalleeData,
3819 return INVALID_SOCKET;
3822 /***********************************************************************
3823 * __WSAFDIsSet (WS2_32.151)
3825 int WINAPI __WSAFDIsSet(SOCKET s, WS_fd_set *set)
3827 int i = set->fd_count;
3829 TRACE("(%ld,%p(%i))\n", s, set, i);
3832 if (set->fd_array[i] == s) return 1;
3836 /***********************************************************************
3837 * WSAIsBlocking (WINSOCK.114)
3838 * WSAIsBlocking (WS2_32.114)
3840 BOOL WINAPI WSAIsBlocking(void)
3842 /* By default WinSock should set all its sockets to non-blocking mode
3843 * and poll in PeekMessage loop when processing "blocking" ones. This
3844 * function is supposed to tell if the program is in this loop. Our
3845 * blocking calls are truly blocking so we always return FALSE.
3847 * Note: It is allowed to call this function without prior WSAStartup().
3854 /***********************************************************************
3855 * WSACancelBlockingCall (WINSOCK.113)
3856 * WSACancelBlockingCall (WS2_32.113)
3858 INT WINAPI WSACancelBlockingCall(void)
3864 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x )
3866 FIXME("How was this called?\n");
3871 /***********************************************************************
3872 * WSASetBlockingHook (WS2_32.109)
3874 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
3876 FARPROC prev = blocking_hook;
3877 blocking_hook = lpBlockFunc;
3878 TRACE("hook %p\n", lpBlockFunc);
3883 /***********************************************************************
3884 * WSAUnhookBlockingHook (WS2_32.110)
3886 INT WINAPI WSAUnhookBlockingHook(void)
3888 blocking_hook = (FARPROC)WSA_DefaultBlockingHook;
3893 /* ----------------------------------- end of API stuff */
3895 /* ----------------------------------- helper functions -
3897 * TODO: Merge WS_dup_..() stuff into one function that
3898 * would operate with a generic structure containing internal
3899 * pointers (via a template of some kind).
3902 static int list_size(char** l, int item_size)
3907 j += (item_size) ? item_size : strlen(l[i]) + 1;
3908 j += (i + 1) * sizeof(char*); }
3912 static int list_dup(char** l_src, char** l_to, int item_size)
3917 for (i = 0; l_src[i]; i++) ;
3918 p = (char *)(l_to + i + 1);
3919 for (i = 0; l_src[i]; i++)
3921 int count = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
3922 memcpy(p, l_src[i], count);
3927 return p - (char *)l_to;
3932 /* duplicate hostent entry
3933 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
3934 * Dito for protoent and servent.
3936 static struct WS_hostent *WS_dup_he(const struct hostent* p_he)
3939 struct WS_hostent *p_to;
3941 int size = (sizeof(*p_he) +
3942 strlen(p_he->h_name) + 1 +
3943 list_size(p_he->h_aliases, 0) +
3944 list_size(p_he->h_addr_list, p_he->h_length));
3946 if (!(p_to = check_buffer_he(size))) return NULL;
3947 p_to->h_addrtype = p_he->h_addrtype;
3948 p_to->h_length = p_he->h_length;
3950 p = (char *)(p_to + 1);
3952 strcpy(p, p_he->h_name);
3955 p_to->h_aliases = (char **)p;
3956 p += list_dup(p_he->h_aliases, p_to->h_aliases, 0);
3958 p_to->h_addr_list = (char **)p;
3959 list_dup(p_he->h_addr_list, p_to->h_addr_list, p_he->h_length);
3963 /* ----- protoent */
3965 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe)
3968 struct WS_protoent *p_to;
3970 int size = (sizeof(*p_pe) +
3971 strlen(p_pe->p_name) + 1 +
3972 list_size(p_pe->p_aliases, 0));
3974 if (!(p_to = check_buffer_pe(size))) return NULL;
3975 p_to->p_proto = p_pe->p_proto;
3977 p = (char *)(p_to + 1);
3979 strcpy(p, p_pe->p_name);
3982 p_to->p_aliases = (char **)p;
3983 list_dup(p_pe->p_aliases, p_to->p_aliases, 0);
3989 static struct WS_servent *WS_dup_se(const struct servent* p_se)
3992 struct WS_servent *p_to;
3994 int size = (sizeof(*p_se) +
3995 strlen(p_se->s_proto) + 1 +
3996 strlen(p_se->s_name) + 1 +
3997 list_size(p_se->s_aliases, 0));
3999 if (!(p_to = check_buffer_se(size))) return NULL;
4000 p_to->s_port = p_se->s_port;
4002 p = (char *)(p_to + 1);
4004 strcpy(p, p_se->s_name);
4008 strcpy(p, p_se->s_proto);
4011 p_to->s_aliases = (char **)p;
4012 list_dup(p_se->s_aliases, p_to->s_aliases, 0);
4016 /* ----------------------------------- error handling */
4020 int loc_errno = errno;
4021 WARN("errno %d, (%s).\n", loc_errno, strerror(loc_errno));
4025 case EINTR: return WSAEINTR;
4026 case EBADF: return WSAEBADF;
4028 case EACCES: return WSAEACCES;
4029 case EFAULT: return WSAEFAULT;
4030 case EINVAL: return WSAEINVAL;
4031 case EMFILE: return WSAEMFILE;
4032 case EWOULDBLOCK: return WSAEWOULDBLOCK;
4033 case EINPROGRESS: return WSAEINPROGRESS;
4034 case EALREADY: return WSAEALREADY;
4035 case ENOTSOCK: return WSAENOTSOCK;
4036 case EDESTADDRREQ: return WSAEDESTADDRREQ;
4037 case EMSGSIZE: return WSAEMSGSIZE;
4038 case EPROTOTYPE: return WSAEPROTOTYPE;
4039 case ENOPROTOOPT: return WSAENOPROTOOPT;
4040 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
4041 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
4042 case EOPNOTSUPP: return WSAEOPNOTSUPP;
4043 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
4044 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
4045 case EADDRINUSE: return WSAEADDRINUSE;
4046 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
4047 case ENETDOWN: return WSAENETDOWN;
4048 case ENETUNREACH: return WSAENETUNREACH;
4049 case ENETRESET: return WSAENETRESET;
4050 case ECONNABORTED: return WSAECONNABORTED;
4052 case ECONNRESET: return WSAECONNRESET;
4053 case ENOBUFS: return WSAENOBUFS;
4054 case EISCONN: return WSAEISCONN;
4055 case ENOTCONN: return WSAENOTCONN;
4056 case ESHUTDOWN: return WSAESHUTDOWN;
4057 case ETOOMANYREFS: return WSAETOOMANYREFS;
4058 case ETIMEDOUT: return WSAETIMEDOUT;
4059 case ECONNREFUSED: return WSAECONNREFUSED;
4060 case ELOOP: return WSAELOOP;
4061 case ENAMETOOLONG: return WSAENAMETOOLONG;
4062 case EHOSTDOWN: return WSAEHOSTDOWN;
4063 case EHOSTUNREACH: return WSAEHOSTUNREACH;
4064 case ENOTEMPTY: return WSAENOTEMPTY;
4066 case EPROCLIM: return WSAEPROCLIM;
4069 case EUSERS: return WSAEUSERS;
4072 case EDQUOT: return WSAEDQUOT;
4075 case ESTALE: return WSAESTALE;
4078 case EREMOTE: return WSAEREMOTE;
4081 /* just in case we ever get here and there are no problems */
4084 WARN("Unknown errno %d!\n", loc_errno);
4085 return WSAEOPNOTSUPP;
4089 UINT wsaHerrno(int loc_errno)
4092 WARN("h_errno %d.\n", loc_errno);
4096 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
4097 case TRY_AGAIN: return WSATRY_AGAIN;
4098 case NO_RECOVERY: return WSANO_RECOVERY;
4099 case NO_DATA: return WSANO_DATA;
4100 case ENOBUFS: return WSAENOBUFS;
4104 WARN("Unknown h_errno %d!\n", loc_errno);
4105 return WSAEOPNOTSUPP;
4110 /***********************************************************************
4111 * WSARecv (WS2_32.67)
4113 int WINAPI WSARecv(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
4114 LPDWORD NumberOfBytesReceived, LPDWORD lpFlags,
4115 LPWSAOVERLAPPED lpOverlapped,
4116 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
4118 return WSARecvFrom(s, lpBuffers, dwBufferCount, NumberOfBytesReceived, lpFlags,
4119 NULL, NULL, lpOverlapped, lpCompletionRoutine);
4122 /***********************************************************************
4123 * WSARecvFrom (WS2_32.69)
4125 INT WINAPI WSARecvFrom( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
4126 LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, struct WS_sockaddr *lpFrom,
4127 LPINT lpFromlen, LPWSAOVERLAPPED lpOverlapped,
4128 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
4131 unsigned int i, options;
4133 DWORD timeout_start = GetTickCount();
4134 struct iovec iovec[WS_MSG_MAXIOVLEN];
4136 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
4137 s, lpBuffers, dwBufferCount, *lpFlags, lpFrom,
4138 (lpFromlen ? *lpFromlen : -1),
4139 lpOverlapped, lpCompletionRoutine);
4141 if (dwBufferCount > WS_MSG_MAXIOVLEN)
4143 WSASetLastError( WSAEINVAL );
4144 return SOCKET_ERROR;
4147 fd = get_sock_fd( s, FILE_READ_DATA, &options );
4148 TRACE( "fd=%d, options=%x\n", fd, options );
4150 if (fd == -1) return SOCKET_ERROR;
4152 for (i = 0; i < dwBufferCount; i++)
4154 iovec[i].iov_base = lpBuffers[i].buf;
4155 iovec[i].iov_len = lpBuffers[i].len;
4160 n = WS2_recv( fd, iovec, dwBufferCount, lpFrom, lpFromlen, lpFlags );
4163 if (errno == EINTR) continue;
4164 if (errno != EAGAIN)
4171 *lpNumberOfBytesRecvd = n;
4173 if ((lpOverlapped || lpCompletionRoutine) &&
4174 !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
4176 IO_STATUS_BLOCK *iosb;
4177 struct ws2_async *wsa = HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa) );
4184 release_sock_fd( s, fd );
4186 wsa->hSocket = SOCKET2HANDLE(s);
4187 wsa->flags = *lpFlags;
4189 wsa->addrlen.ptr = lpFromlen;
4190 wsa->user_overlapped = lpOverlapped;
4191 wsa->completion_func = lpCompletionRoutine;
4192 wsa->n_iovecs = dwBufferCount;
4193 memcpy( wsa->iovec, iovec, dwBufferCount * sizeof(*iovec) );
4195 iosb = lpOverlapped ? (IO_STATUS_BLOCK *)lpOverlapped : &wsa->local_iosb;
4199 iosb->u.Status = STATUS_PENDING;
4200 iosb->Information = 0;
4202 SERVER_START_REQ( register_async )
4204 req->handle = wsa->hSocket;
4205 req->type = ASYNC_TYPE_READ;
4206 req->async.callback = WS2_async_recv;
4207 req->async.iosb = iosb;
4208 req->async.arg = wsa;
4209 req->async.apc = ws2_async_apc;
4210 req->async.event = lpCompletionRoutine ? 0 : lpOverlapped->hEvent;
4211 err = wine_server_call( req );
4215 if (err != STATUS_PENDING) HeapFree( GetProcessHeap(), 0, wsa );
4216 WSASetLastError( NtStatusToWSAError( err ));
4217 return SOCKET_ERROR;
4220 iosb->u.Status = STATUS_SUCCESS;
4221 iosb->Information = n;
4222 if (!wsa->completion_func)
4224 SetEvent( lpOverlapped->hEvent );
4225 HeapFree( GetProcessHeap(), 0, wsa );
4227 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)ws2_async_apc,
4228 (ULONG_PTR)wsa, (ULONG_PTR)iosb, 0 );
4229 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4235 if ( _is_blocking(s) )
4238 int timeout = GET_RCVTIMEO(fd);
4241 timeout -= GetTickCount() - timeout_start;
4242 if (timeout < 0) timeout = 0;
4246 pfd.events = POLLIN;
4247 if (*lpFlags & WS_MSG_OOB) pfd.events |= POLLPRI;
4249 if (!timeout || !poll( &pfd, 1, timeout ))
4252 /* a timeout is not fatal */
4253 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4259 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4260 err = WSAEWOULDBLOCK;
4265 TRACE(" -> %i bytes\n", n);
4266 release_sock_fd( s, fd );
4267 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4272 release_sock_fd( s, fd );
4273 WARN(" -> ERROR %d\n", err);
4274 WSASetLastError( err );
4275 return SOCKET_ERROR;
4278 /***********************************************************************
4279 * WSCInstallProvider (WS2_32.88)
4281 INT WINAPI WSCInstallProvider( const LPGUID lpProviderId,
4282 LPCWSTR lpszProviderDllPath,
4283 const LPWSAPROTOCOL_INFOW lpProtocolInfoList,
4284 DWORD dwNumberOfEntries,
4287 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId),
4288 debugstr_w(lpszProviderDllPath), lpProtocolInfoList,
4289 dwNumberOfEntries, lpErrno);
4295 /***********************************************************************
4296 * WSCDeinstallProvider (WS2_32.83)
4298 INT WINAPI WSCDeinstallProvider(LPGUID lpProviderId, LPINT lpErrno)
4300 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId), lpErrno);
4306 /***********************************************************************
4307 * WSAAccept (WS2_32.26)
4309 SOCKET WINAPI WSAAccept( SOCKET s, struct WS_sockaddr *addr, LPINT addrlen,
4310 LPCONDITIONPROC lpfnCondition, DWORD dwCallbackData)
4313 int ret = 0, size = 0;
4314 WSABUF CallerId, CallerData, CalleeId, CalleeData;
4315 /* QOS SQOS, GQOS; */
4318 SOCKADDR src_addr, dst_addr;
4320 TRACE("Socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %d\n",
4321 s, addr, addrlen, lpfnCondition, dwCallbackData);
4324 size = sizeof(src_addr);
4325 cs = WS_accept(s, &src_addr, &size);
4327 if (cs == SOCKET_ERROR) return SOCKET_ERROR;
4329 CallerId.buf = (char *)&src_addr;
4330 CallerId.len = sizeof(src_addr);
4332 CallerData.buf = NULL;
4333 CallerData.len = (ULONG)NULL;
4335 WS_getsockname(cs, &dst_addr, &size);
4337 CalleeId.buf = (char *)&dst_addr;
4338 CalleeId.len = sizeof(dst_addr);
4341 ret = (*lpfnCondition)(&CallerId, &CallerData, NULL, NULL,
4342 &CalleeId, &CalleeData, &g, dwCallbackData);
4347 if (addr && addrlen)
4348 addr = memcpy(addr, &src_addr, (*addrlen > size) ? size : *addrlen );
4351 SERVER_START_REQ( set_socket_deferred )
4353 req->handle = SOCKET2HANDLE(s);
4354 req->deferred = SOCKET2HANDLE(cs);
4355 if ( !wine_server_call_err ( req ) )
4357 SetLastError( WSATRY_AGAIN );
4358 WS_closesocket( cs );
4362 return SOCKET_ERROR;
4365 SetLastError(WSAECONNREFUSED);
4366 return SOCKET_ERROR;
4368 FIXME("Unknown return type from Condition function\n");
4369 SetLastError(WSAENOTSOCK);
4370 return SOCKET_ERROR;
4374 /***********************************************************************
4375 * WSADuplicateSocketA (WS2_32.32)
4377 int WINAPI WSADuplicateSocketA( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOA lpProtocolInfo )
4381 TRACE("(%ld,%x,%p)\n", s, dwProcessId, lpProtocolInfo);
4382 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
4383 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
4384 /* I don't know what the real Windoze does next, this is a hack */
4385 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
4386 * the target use the global duplicate, or we could copy a reference to us to the structure
4387 * and let the target duplicate it from us, but let's do it as simple as possible */
4388 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
4389 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
4390 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
4391 0, FALSE, DUPLICATE_SAME_ACCESS);
4392 CloseHandle(hProcess);
4393 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
4397 /***********************************************************************
4398 * WSADuplicateSocketW (WS2_32.33)
4400 int WINAPI WSADuplicateSocketW( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOW lpProtocolInfo )
4404 TRACE("(%ld,%x,%p)\n", s, dwProcessId, lpProtocolInfo);
4406 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
4407 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
4408 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
4409 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
4410 0, FALSE, DUPLICATE_SAME_ACCESS);
4411 CloseHandle(hProcess);
4412 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
4416 /***********************************************************************
4417 * WSAInstallServiceClassA (WS2_32.48)
4419 int WINAPI WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info)
4421 FIXME("Request to install service %s\n",debugstr_a(info->lpszServiceClassName));
4422 WSASetLastError(WSAEACCES);
4423 return SOCKET_ERROR;
4426 /***********************************************************************
4427 * WSAInstallServiceClassW (WS2_32.49)
4429 int WINAPI WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info)
4431 FIXME("Request to install service %s\n",debugstr_w(info->lpszServiceClassName));
4432 WSASetLastError(WSAEACCES);
4433 return SOCKET_ERROR;
4436 /***********************************************************************
4437 * WSARemoveServiceClass (WS2_32.70)
4439 int WINAPI WSARemoveServiceClass(LPGUID info)
4441 FIXME("Request to remove service %p\n",info);
4442 WSASetLastError(WSATYPE_NOT_FOUND);
4443 return SOCKET_ERROR;
4446 /***********************************************************************
4447 * WSAStringToAddressA (WS2_32.80)
4449 INT WINAPI WSAStringToAddressA(LPSTR AddressString,
4451 LPWSAPROTOCOL_INFOA lpProtocolInfo,
4452 LPSOCKADDR lpAddress,
4453 LPINT lpAddressLength)
4456 LPSTR workBuffer=NULL,ptrPort;
4458 TRACE( "(%s, %x, %p, %p, %p)\n", AddressString, AddressFamily, lpProtocolInfo,
4459 lpAddress, lpAddressLength );
4461 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
4465 WSASetLastError(WSAEINVAL);
4466 return SOCKET_ERROR;
4470 FIXME("ProtocolInfo not implemented.\n");
4472 workBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
4473 strlen(AddressString) + 1);
4476 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4477 return SOCKET_ERROR;
4480 strcpy(workBuffer, AddressString);
4482 switch(AddressFamily)
4486 struct in_addr inetaddr;
4488 /* If lpAddressLength is too small, tell caller the size we need */
4489 if (*lpAddressLength < sizeof(SOCKADDR_IN))
4491 *lpAddressLength = sizeof(SOCKADDR_IN);
4495 memset(lpAddress, 0, sizeof(SOCKADDR_IN));
4497 ((LPSOCKADDR_IN)lpAddress)->sin_family = AF_INET;
4499 ptrPort = strchr(workBuffer, ':');
4502 ((LPSOCKADDR_IN)lpAddress)->sin_port = (WS_u_short)atoi(ptrPort+1);
4507 ((LPSOCKADDR_IN)lpAddress)->sin_port = 0;
4510 if(inet_aton(workBuffer, &inetaddr) > 0)
4512 ((LPSOCKADDR_IN)lpAddress)->sin_addr.WS_s_addr = inetaddr.s_addr;
4523 struct in6_addr inetaddr;
4524 /* If lpAddressLength is too small, tell caller the size we need */
4525 if (*lpAddressLength < sizeof(SOCKADDR_IN6))
4527 *lpAddressLength = sizeof(SOCKADDR_IN6);
4531 #ifdef HAVE_INET_PTON
4532 memset(lpAddress, 0, sizeof(SOCKADDR_IN6));
4534 ((LPSOCKADDR_IN6)lpAddress)->sin6_family = WS_AF_INET6;
4536 /* This one is a bit tricky. An IPv6 address contains colons, so the
4537 * check from IPv4 doesn't work like that. However, IPv6 addresses that
4538 * contain a port are written with braces like [fd12:3456:7890::1]:12345
4539 * so what we will do is to look for ']', check if the next char is a
4540 * colon, and if it is, parse the port as in IPv4. */
4542 ptrPort = strchr(workBuffer, ']');
4543 if(ptrPort && *(++ptrPort) == ':')
4545 ((LPSOCKADDR_IN6)lpAddress)->sin6_port = (WS_u_short)atoi(ptrPort+1);
4550 ((LPSOCKADDR_IN6)lpAddress)->sin6_port = 0;
4553 if(inet_pton(AF_INET6, workBuffer, &inetaddr) > 0)
4555 memcpy(&((LPSOCKADDR_IN6)lpAddress)->sin6_addr, &inetaddr,
4556 sizeof(struct in6_addr));
4560 #endif /* HAVE_INET_PTON */
4566 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
4567 TRACE("Unsupported address family specified: %d.\n", AddressFamily);
4571 HeapFree(GetProcessHeap(), 0, workBuffer);
4574 WSASetLastError(res);
4575 return SOCKET_ERROR;
4578 /***********************************************************************
4579 * WSAStringToAddressW (WS2_32.81)
4581 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
4582 * If this should be the case, it would be required to map these digits
4583 * to Unicode digits (0-9) using FoldString first.
4585 INT WINAPI WSAStringToAddressW(LPWSTR AddressString,
4587 LPWSAPROTOCOL_INFOW lpProtocolInfo,
4588 LPSOCKADDR lpAddress,
4589 LPINT lpAddressLength)
4592 LPSTR workBuffer=NULL;
4593 WSAPROTOCOL_INFOA infoA;
4594 LPWSAPROTOCOL_INFOA lpProtoInfoA = NULL;
4596 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString), AddressFamily, lpProtocolInfo,
4597 lpAddress, lpAddressLength );
4599 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
4601 /* if ProtocolInfo is available - convert to ANSI variant */
4604 lpProtoInfoA = &infoA;
4605 memcpy( lpProtoInfoA, lpProtocolInfo, FIELD_OFFSET( WSAPROTOCOL_INFOA, szProtocol ) );
4607 if (!WideCharToMultiByte( CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
4608 lpProtoInfoA->szProtocol, WSAPROTOCOL_LEN+1, NULL, NULL ))
4610 WSASetLastError( WSAEINVAL);
4611 return SOCKET_ERROR;
4617 /* Translate AddressString to ANSI code page - assumes that only
4618 standard digits 0-9 are used with this API call */
4619 sBuffer = WideCharToMultiByte( CP_ACP, 0, AddressString, -1, NULL, 0, NULL, NULL );
4620 workBuffer = HeapAlloc( GetProcessHeap(), 0, sBuffer );
4624 WideCharToMultiByte( CP_ACP, 0, AddressString, -1, workBuffer, sBuffer, NULL, NULL );
4625 res = WSAStringToAddressA(workBuffer,AddressFamily,lpProtoInfoA,
4626 lpAddress,lpAddressLength);
4627 HeapFree( GetProcessHeap(), 0, workBuffer );
4631 res = WSA_NOT_ENOUGH_MEMORY;
4636 WSASetLastError(res);
4637 return SOCKET_ERROR;
4640 /***********************************************************************
4641 * WSAAddressToStringA (WS2_32.27)
4643 * See WSAAddressToStringW
4645 INT WINAPI WSAAddressToStringA( LPSOCKADDR sockaddr, DWORD len,
4646 LPWSAPROTOCOL_INFOA info, LPSTR string,
4650 CHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4653 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
4655 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
4656 if (!string || !lenstr) return SOCKET_ERROR;
4658 /* sin_family is guaranteed to be the first u_short */
4659 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
4661 sprintf( buffer, "%u.%u.%u.%u:%u",
4662 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
4663 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
4664 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
4665 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
4666 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
4668 p = strchr( buffer, ':' );
4669 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
4671 size = strlen( buffer );
4676 WSASetLastError(WSAEFAULT);
4677 return SOCKET_ERROR;
4680 strcpy( string, buffer );
4684 /***********************************************************************
4685 * WSAAddressToStringW (WS2_32.28)
4687 * Convert a sockaddr address into a readable address string.
4690 * sockaddr [I] Pointer to a sockaddr structure.
4691 * len [I] Size of the sockaddr structure.
4692 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
4693 * string [I/O] Pointer to a buffer to receive the address string.
4694 * lenstr [I/O] Size of the receive buffer in WCHARs.
4698 * Failure: SOCKET_ERROR
4701 * The 'info' parameter is ignored.
4704 * Only supports AF_INET addresses.
4706 INT WINAPI WSAAddressToStringW( LPSOCKADDR sockaddr, DWORD len,
4707 LPWSAPROTOCOL_INFOW info, LPWSTR string,
4711 WCHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4712 static const WCHAR format[] = { '%','u','.','%','u','.','%','u','.','%','u',':','%','u',0 };
4715 TRACE( "(%p, %x, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
4717 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
4718 if (!string || !lenstr) return SOCKET_ERROR;
4720 /* sin_family is guaranteed to be the first u_short */
4721 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
4723 sprintfW( buffer, format,
4724 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
4725 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
4726 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
4727 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
4728 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
4730 p = strchrW( buffer, ':' );
4731 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
4733 size = lstrlenW( buffer );
4738 return SOCKET_ERROR;
4741 lstrcpyW( string, buffer );
4745 /***********************************************************************
4746 * WSAEnumNameSpaceProvidersA (WS2_32.34)
4748 INT WINAPI WSAEnumNameSpaceProvidersA( LPDWORD len, LPWSANAMESPACE_INFOA buffer )
4750 FIXME( "(%p %p) Stub!\n", len, buffer );
4754 /***********************************************************************
4755 * WSAEnumNameSpaceProvidersW (WS2_32.35)
4757 INT WINAPI WSAEnumNameSpaceProvidersW( LPDWORD len, LPWSANAMESPACE_INFOW buffer )
4759 FIXME( "(%p %p) Stub!\n", len, buffer );
4763 /***********************************************************************
4764 * WSAGetQOSByName (WS2_32.41)
4766 BOOL WINAPI WSAGetQOSByName( SOCKET s, LPWSABUF lpQOSName, LPQOS lpQOS )
4768 FIXME( "(0x%04lx %p %p) Stub!\n", s, lpQOSName, lpQOS );
4772 /***********************************************************************
4773 * WSAGetServiceClassInfoA (WS2_32.42)
4775 INT WINAPI WSAGetServiceClassInfoA( LPGUID provider, LPGUID service, LPDWORD len,
4776 LPWSASERVICECLASSINFOA info )
4778 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider), debugstr_guid(service),
4780 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4781 return SOCKET_ERROR;
4784 /***********************************************************************
4785 * WSAGetServiceClassInfoW (WS2_32.43)
4787 INT WINAPI WSAGetServiceClassInfoW( LPGUID provider, LPGUID service, LPDWORD len,
4788 LPWSASERVICECLASSINFOW info )
4790 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider), debugstr_guid(service),
4792 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4793 return SOCKET_ERROR;
4796 /***********************************************************************
4797 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
4799 INT WINAPI WSAGetServiceClassNameByClassIdA( LPGUID class, LPSTR service, LPDWORD len )
4801 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4802 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4803 return SOCKET_ERROR;
4806 /***********************************************************************
4807 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
4809 INT WINAPI WSAGetServiceClassNameByClassIdW( LPGUID class, LPWSTR service, LPDWORD len )
4811 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4812 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4813 return SOCKET_ERROR;
4816 /***********************************************************************
4817 * WSALookupServiceBeginA (WS2_32.59)
4819 INT WINAPI WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions,
4820 DWORD dwControlFlags,
4823 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4825 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4826 return SOCKET_ERROR;
4829 /***********************************************************************
4830 * WSALookupServiceBeginW (WS2_32.60)
4832 INT WINAPI WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions,
4833 DWORD dwControlFlags,
4836 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4838 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4839 return SOCKET_ERROR;
4842 /***********************************************************************
4843 * WSALookupServiceBeginW (WS2_32.61)
4845 INT WINAPI WSALookupServiceEnd( HANDLE lookup )
4847 FIXME("(%p) Stub!\n", lookup );
4851 /***********************************************************************
4852 * WSALookupServiceNextA (WS2_32.62)
4854 INT WINAPI WSALookupServiceNextA( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETA results )
4856 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup, flags, len, results );
4860 /***********************************************************************
4861 * WSALookupServiceNextW (WS2_32.63)
4863 INT WINAPI WSALookupServiceNextW( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETW results )
4865 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup, flags, len, results );
4869 /***********************************************************************
4870 * WSANtohl (WS2_32.64)
4872 INT WINAPI WSANtohl( SOCKET s, WS_u_long netlong, WS_u_long* lphostlong )
4874 TRACE( "(0x%04lx 0x%08x %p)\n", s, netlong, lphostlong );
4876 if (!lphostlong) return WSAEFAULT;
4878 *lphostlong = ntohl( netlong );
4882 /***********************************************************************
4883 * WSANtohs (WS2_32.65)
4885 INT WINAPI WSANtohs( SOCKET s, WS_u_short netshort, WS_u_short* lphostshort )
4887 TRACE( "(0x%04lx 0x%08x %p)\n", s, netshort, lphostshort );
4889 if (!lphostshort) return WSAEFAULT;
4891 *lphostshort = ntohs( netshort );
4895 /***********************************************************************
4896 * WSAProviderConfigChange (WS2_32.66)
4898 INT WINAPI WSAProviderConfigChange( LPHANDLE handle, LPWSAOVERLAPPED overlapped,
4899 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion )
4901 FIXME( "(%p %p %p) Stub!\n", handle, overlapped, completion );
4902 return SOCKET_ERROR;
4905 /***********************************************************************
4906 * WSARecvDisconnect (WS2_32.68)
4908 INT WINAPI WSARecvDisconnect( SOCKET s, LPWSABUF disconnectdata )
4910 TRACE( "(0x%04lx %p)\n", s, disconnectdata );
4912 return WS_shutdown( s, 0 );
4915 /***********************************************************************
4916 * WSASetServiceA (WS2_32.76)
4918 INT WINAPI WSASetServiceA( LPWSAQUERYSETA query, WSAESETSERVICEOP operation, DWORD flags )
4920 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query, operation, flags );
4924 /***********************************************************************
4925 * WSASetServiceW (WS2_32.77)
4927 INT WINAPI WSASetServiceW( LPWSAQUERYSETW query, WSAESETSERVICEOP operation, DWORD flags )
4929 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query, operation, flags );
4933 /***********************************************************************
4934 * WSCEnableNSProvider (WS2_32.84)
4936 INT WINAPI WSCEnableNSProvider( LPGUID provider, BOOL enable )
4938 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider), enable );
4942 /***********************************************************************
4943 * WSCGetProviderPath (WS2_32.86)
4945 INT WINAPI WSCGetProviderPath( LPGUID provider, LPWSTR path, LPINT len, LPINT errcode )
4947 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider), path, len, errcode );
4949 if (!errcode || !provider || !len) return WSAEFAULT;
4951 *errcode = WSAEINVAL;
4952 return SOCKET_ERROR;
4955 /***********************************************************************
4956 * WSCInstallNameSpace (WS2_32.87)
4958 INT WINAPI WSCInstallNameSpace( LPWSTR identifier, LPWSTR path, DWORD namespace,
4959 DWORD version, LPGUID provider )
4961 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier), debugstr_w(path),
4962 namespace, version, debugstr_guid(provider) );
4966 /***********************************************************************
4967 * WSCUnInstallNameSpace (WS2_32.89)
4969 INT WINAPI WSCUnInstallNameSpace( LPGUID lpProviderId )
4971 FIXME("(%p) Stub!\n", lpProviderId);
4975 /***********************************************************************
4976 * WSCWriteProviderOrder (WS2_32.91)
4978 INT WINAPI WSCWriteProviderOrder( LPDWORD entry, DWORD number )
4980 FIXME("(%p 0x%08x) Stub!\n", entry, number);