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 uipx->sipx_family=AF_IPX;
858 uipx->sipx_port=wsipx->sa_socket;
859 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
862 memcpy(&uipx->sipx_network,wsipx->sa_netnum,sizeof(uipx->sipx_network)+sizeof(uipx->sipx_node));
863 #ifdef IPX_FRAME_NONE
864 uipx->sipx_type=IPX_FRAME_NONE;
870 struct sockaddr_in6* uin6 = (struct sockaddr_in6 *)uaddr;
871 const struct WS_sockaddr_in6* win6 = (const struct WS_sockaddr_in6*)wsaddr;
873 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
874 * scope_id, one without. Check:
875 * http://msdn.microsoft.com/library/en-us/winsock/winsock/sockaddr_2.asp
877 if (wsaddrlen >= sizeof(struct WS_sockaddr_in6_old)) {
878 uaddrlen = sizeof(struct sockaddr_in6);
879 uin6->sin6_family = AF_INET6;
880 uin6->sin6_port = win6->sin6_port;
881 uin6->sin6_flowinfo = win6->sin6_flowinfo;
882 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
883 if (wsaddrlen >= sizeof(struct WS_sockaddr_in6)) uin6->sin6_scope_id = win6->sin6_scope_id;
885 memcpy(&uin6->sin6_addr,&win6->sin6_addr,16); /* 16 bytes = 128 address bits */
888 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen);
892 struct sockaddr_in* uin = (struct sockaddr_in *)uaddr;
893 const struct WS_sockaddr_in* win = (const struct WS_sockaddr_in*)wsaddr;
895 if (wsaddrlen<sizeof(struct WS_sockaddr_in))
897 uaddrlen = sizeof(struct sockaddr_in);
898 uin->sin_family = AF_INET;
899 uin->sin_port = win->sin_port;
900 memcpy(&uin->sin_addr,&win->sin_addr,4); /* 4 bytes = 32 address bits */
904 /* Try to determine the needed space by the passed windows sockaddr space */
906 default: /* likely a ipv4 address */
907 case sizeof(struct WS_sockaddr_in):
908 uaddrlen = sizeof(struct sockaddr_in);
911 case sizeof(struct WS_sockaddr_ipx):
912 uaddrlen = sizeof(struct sockaddr_ipx);
915 case sizeof(struct WS_sockaddr_in6):
916 case sizeof(struct WS_sockaddr_in6_old):
917 uaddrlen = sizeof(struct sockaddr_in6);
920 memset( uaddr, 0, uaddrlen );
924 FIXME("Unknown address family %d, return NULL.\n", wsaddr->sa_family);
930 static BOOL is_sockaddr_bound(const struct sockaddr *uaddr, int uaddrlen)
932 switch (uaddr->sa_family)
936 FIXME("don't know how to tell if IPX socket is bound, assuming it is!\n");
941 static const struct sockaddr_in6 emptyAddr;
942 const struct sockaddr_in6 *in6 = (const struct sockaddr_in6*) uaddr;
943 return in6->sin6_port || memcmp(&in6->sin6_addr, &emptyAddr.sin6_addr, sizeof(struct in6_addr));
947 static const struct sockaddr_in emptyAddr;
948 const struct sockaddr_in *in = (const struct sockaddr_in*) uaddr;
949 return in->sin_port || memcmp(&in->sin_addr, &emptyAddr.sin_addr, sizeof(struct in_addr));
954 FIXME("unknown address family %d\n", uaddr->sa_family);
959 /* Returns 0 if successful, -1 if the buffer is too small */
960 static int ws_sockaddr_u2ws(const struct sockaddr* uaddr, struct WS_sockaddr* wsaddr, int* wsaddrlen)
964 switch(uaddr->sa_family)
969 const struct sockaddr_ipx* uipx=(const struct sockaddr_ipx*)uaddr;
970 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
973 switch (*wsaddrlen) /* how much can we copy? */
977 *wsaddrlen = sizeof(*wsipx);
978 wsipx->sa_socket=uipx->sipx_port;
982 memcpy(wsipx->sa_nodenum,uipx->sipx_node,sizeof(wsipx->sa_nodenum));
990 memcpy(wsipx->sa_netnum,&uipx->sipx_network,sizeof(wsipx->sa_netnum));
996 wsipx->sa_family=WS_AF_IPX;
1007 const struct sockaddr_in6* uin6 = (const struct sockaddr_in6*)uaddr;
1008 struct WS_sockaddr_in6_old* win6old = (struct WS_sockaddr_in6_old*)wsaddr;
1010 if (*wsaddrlen < sizeof(struct WS_sockaddr_in6_old))
1012 win6old->sin6_family = WS_AF_INET6;
1013 win6old->sin6_port = uin6->sin6_port;
1014 win6old->sin6_flowinfo = uin6->sin6_flowinfo;
1015 memcpy(&win6old->sin6_addr,&uin6->sin6_addr,16); /* 16 bytes = 128 address bits */
1016 *wsaddrlen = sizeof(struct WS_sockaddr_in6_old);
1017 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1018 if (*wsaddrlen >= sizeof(struct WS_sockaddr_in6)) {
1019 struct WS_sockaddr_in6* win6 = (struct WS_sockaddr_in6*)wsaddr;
1020 win6->sin6_scope_id = uin6->sin6_scope_id;
1021 *wsaddrlen = sizeof(struct WS_sockaddr_in6);
1027 const struct sockaddr_in* uin = (const struct sockaddr_in*)uaddr;
1028 struct WS_sockaddr_in* win = (struct WS_sockaddr_in*)wsaddr;
1030 if (*wsaddrlen < sizeof(struct WS_sockaddr_in))
1032 win->sin_family = WS_AF_INET;
1033 win->sin_port = uin->sin_port;
1034 memcpy(&win->sin_addr,&uin->sin_addr,4); /* 4 bytes = 32 address bits */
1035 memset(&win->sin_zero, 0, 8); /* Make sure the null padding is null */
1036 *wsaddrlen = sizeof(struct WS_sockaddr_in);
1040 memset(wsaddr,0,*wsaddrlen);
1044 FIXME("Unknown address family %d\n", uaddr->sa_family);
1050 /**************************************************************************
1051 * Functions for handling overlapped I/O
1052 **************************************************************************/
1054 /* user APC called upon async completion */
1055 static void WINAPI ws2_async_apc( void *arg, IO_STATUS_BLOCK *iosb, ULONG reserved )
1057 ws2_async *wsa = arg;
1059 if (wsa->completion_func) wsa->completion_func( NtStatusToWSAError(iosb->u.Status),
1060 iosb->Information, wsa->user_overlapped,
1062 HeapFree( GetProcessHeap(), 0, wsa );
1065 /***********************************************************************
1066 * WS2_recv (INTERNAL)
1068 * Workhorse for both synchronous and asynchronous recv() operations.
1070 static int WS2_recv( int fd, struct iovec* iov, int count,
1071 struct WS_sockaddr *lpFrom, LPINT lpFromlen,
1075 union generic_unix_sockaddr unix_sockaddr;
1078 hdr.msg_name = NULL;
1082 hdr.msg_namelen = sizeof(unix_sockaddr);
1083 hdr.msg_name = &unix_sockaddr;
1086 hdr.msg_namelen = 0;
1089 hdr.msg_iovlen = count;
1090 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1091 hdr.msg_accrights = NULL;
1092 hdr.msg_accrightslen = 0;
1094 hdr.msg_control = NULL;
1095 hdr.msg_controllen = 0;
1099 if ( (n = recvmsg(fd, &hdr, *lpFlags)) == -1 )
1103 ws_sockaddr_u2ws( &unix_sockaddr.addr, lpFrom, lpFromlen ) != 0 )
1105 /* The from buffer was too small, but we read the data
1106 * anyway. Is that really bad?
1113 /***********************************************************************
1114 * WS2_async_recv (INTERNAL)
1116 * Handler for overlapped recv() operations.
1118 static NTSTATUS WS2_async_recv( void* user, IO_STATUS_BLOCK* iosb, NTSTATUS status)
1120 ws2_async* wsa = user;
1125 case STATUS_ALERTED:
1126 if ((status = wine_server_handle_to_fd( wsa->hSocket, FILE_READ_DATA, &fd, NULL ) ))
1129 result = WS2_recv( fd, wsa->iovec, wsa->n_iovecs,
1130 wsa->addr, wsa->addrlen.ptr, &wsa->flags );
1131 wine_server_release_fd( wsa->hSocket, fd );
1134 status = STATUS_SUCCESS;
1135 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1139 if (errno == EINTR || errno == EAGAIN)
1141 status = STATUS_PENDING;
1142 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1147 status = wsaErrno(); /* FIXME: is this correct ???? */
1152 if (status != STATUS_PENDING)
1154 iosb->u.Status = status;
1155 iosb->Information = result;
1160 /***********************************************************************
1161 * WS2_send (INTERNAL)
1163 * Workhorse for both synchronous and asynchronous send() operations.
1165 static int WS2_send( int fd, struct iovec* iov, int count,
1166 const struct WS_sockaddr *to, INT tolen, DWORD dwFlags )
1169 union generic_unix_sockaddr unix_addr;
1172 hdr.msg_name = NULL;
1173 hdr.msg_namelen = 0;
1177 hdr.msg_name = &unix_addr;
1178 hdr.msg_namelen = ws_sockaddr_ws2u( to, tolen, &unix_addr );
1179 if ( !hdr.msg_namelen )
1185 #if defined(HAVE_IPX) && defined(SOL_IPX)
1186 if(to->sa_family == WS_AF_IPX)
1188 struct sockaddr_ipx* uipx = (struct sockaddr_ipx*)hdr.msg_name;
1190 unsigned int len=sizeof(int);
1192 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1193 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1194 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1195 * ipx type in the sockaddr_opx structure with the stored value.
1197 if(getsockopt(fd, SOL_IPX, IPX_TYPE, &val, &len) != -1)
1198 uipx->sipx_type = val;
1204 hdr.msg_iovlen = count;
1205 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1206 hdr.msg_accrights = NULL;
1207 hdr.msg_accrightslen = 0;
1209 hdr.msg_control = NULL;
1210 hdr.msg_controllen = 0;
1214 return sendmsg(fd, &hdr, dwFlags);
1217 /***********************************************************************
1218 * WS2_async_send (INTERNAL)
1220 * Handler for overlapped send() operations.
1222 static NTSTATUS WS2_async_send(void* user, IO_STATUS_BLOCK* iosb, NTSTATUS status)
1224 ws2_async* wsa = user;
1229 case STATUS_ALERTED:
1230 if ((status = wine_server_handle_to_fd( wsa->hSocket, FILE_WRITE_DATA, &fd, NULL ) ))
1233 /* check to see if the data is ready (non-blocking) */
1234 result = WS2_send( fd, wsa->iovec, wsa->n_iovecs, wsa->addr, wsa->addrlen.val, wsa->flags );
1235 wine_server_release_fd( wsa->hSocket, fd );
1239 status = STATUS_SUCCESS;
1240 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1244 if (errno == EINTR || errno == EAGAIN)
1246 status = STATUS_PENDING;
1247 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1251 /* We set the status to a winsock error code and check for that
1252 later in NtStatusToWSAError () */
1253 status = wsaErrno();
1259 if (status != STATUS_PENDING)
1261 iosb->u.Status = status;
1262 iosb->Information = result;
1267 /***********************************************************************
1268 * WS2_async_shutdown (INTERNAL)
1270 * Handler for shutdown() operations on overlapped sockets.
1272 static NTSTATUS WS2_async_shutdown( void* user, PIO_STATUS_BLOCK iosb, NTSTATUS status )
1274 ws2_async* wsa = user;
1279 case STATUS_ALERTED:
1280 if ((status = wine_server_handle_to_fd( wsa->hSocket, 0, &fd, NULL ) ))
1283 switch ( wsa->type )
1285 case ASYNC_TYPE_READ: err = shutdown( fd, 0 ); break;
1286 case ASYNC_TYPE_WRITE: err = shutdown( fd, 1 ); break;
1288 wine_server_release_fd( wsa->hSocket, fd );
1289 status = err ? wsaErrno() : STATUS_SUCCESS;
1292 iosb->u.Status = status;
1296 /***********************************************************************
1297 * WS2_register_async_shutdown (INTERNAL)
1299 * Helper function for WS_shutdown() on overlapped sockets.
1301 static int WS2_register_async_shutdown( SOCKET s, int type )
1303 struct ws2_async *wsa;
1306 TRACE("s %ld type %d\n", s, type);
1308 wsa = HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa) );
1312 wsa->hSocket = SOCKET2HANDLE(s);
1314 wsa->completion_func = NULL;
1316 SERVER_START_REQ( register_async )
1318 req->handle = wsa->hSocket;
1320 req->async.callback = WS2_async_shutdown;
1321 req->async.iosb = &wsa->local_iosb;
1322 req->async.arg = wsa;
1323 req->async.apc = ws2_async_apc;
1324 status = wine_server_call( req );
1328 if (status != STATUS_PENDING)
1330 HeapFree( GetProcessHeap(), 0, wsa );
1331 return NtStatusToWSAError( status );
1336 /***********************************************************************
1339 SOCKET WINAPI WS_accept(SOCKET s, struct WS_sockaddr *addr,
1345 TRACE("socket %04lx\n", s );
1346 is_blocking = _is_blocking(s);
1351 int fd = get_sock_fd( s, FILE_READ_DATA, NULL );
1352 if (fd == -1) return INVALID_SOCKET;
1354 do_block(fd, POLLIN, -1);
1355 _sync_sock_state(s); /* let wineserver notice connection */
1356 release_sock_fd( s, fd );
1357 /* retrieve any error codes from it */
1358 SetLastError(_get_sock_error(s, FD_ACCEPT_BIT));
1359 /* FIXME: care about the error? */
1361 SERVER_START_REQ( accept_socket )
1363 req->lhandle = SOCKET2HANDLE(s);
1364 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
1365 req->attributes = OBJ_INHERIT;
1366 set_error( wine_server_call( req ) );
1367 as = HANDLE2SOCKET( reply->handle );
1372 if (addr) WS_getpeername(as, addr, addrlen32);
1375 } while (is_blocking);
1376 return INVALID_SOCKET;
1379 /***********************************************************************
1382 int WINAPI WS_bind(SOCKET s, const struct WS_sockaddr* name, int namelen)
1384 int fd = get_sock_fd( s, 0, NULL );
1385 int res = SOCKET_ERROR;
1387 TRACE("socket %04lx, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1391 if (!name || (name->sa_family && !SUPPORTED_PF(name->sa_family)))
1393 SetLastError(WSAEAFNOSUPPORT);
1397 union generic_unix_sockaddr uaddr;
1398 unsigned int uaddrlen = ws_sockaddr_ws2u(name, namelen, &uaddr);
1401 SetLastError(WSAEFAULT);
1405 if (bind(fd, &uaddr.addr, uaddrlen) < 0)
1407 int loc_errno = errno;
1408 WARN("\tfailure - errno = %i\n", errno);
1413 SetLastError(WSAENOTSOCK);
1416 SetLastError(WSAEINVAL);
1419 SetLastError(wsaErrno());
1425 res=0; /* success */
1429 release_sock_fd( s, fd );
1434 /***********************************************************************
1435 * closesocket (WS2_32.3)
1437 int WINAPI WS_closesocket(SOCKET s)
1439 TRACE("socket %04lx\n", s);
1440 if (CloseHandle(SOCKET2HANDLE(s))) return 0;
1441 return SOCKET_ERROR;
1444 /***********************************************************************
1445 * connect (WS2_32.4)
1447 int WINAPI WS_connect(SOCKET s, const struct WS_sockaddr* name, int namelen)
1449 int fd = get_sock_fd( s, FILE_READ_DATA, NULL );
1451 TRACE("socket %04lx, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1455 union generic_unix_sockaddr uaddr;
1456 unsigned int uaddrlen = ws_sockaddr_ws2u(name, namelen, &uaddr);
1460 SetLastError(WSAEFAULT);
1464 if (connect(fd, &uaddr.addr, uaddrlen) == 0)
1465 goto connect_success;
1468 if (errno == EINPROGRESS)
1470 /* tell wineserver that a connection is in progress */
1471 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1472 FD_CONNECT|FD_READ|FD_WRITE,
1473 FD_WINE_CONNECTED|FD_WINE_LISTENING);
1474 if (_is_blocking(s))
1478 do_block(fd, POLLIN | POLLOUT, -1);
1479 _sync_sock_state(s); /* let wineserver notice connection */
1480 /* retrieve any error codes from it */
1481 result = _get_sock_error(s, FD_CONNECT_BIT);
1483 SetLastError(result);
1486 goto connect_success;
1491 SetLastError(WSAEWOULDBLOCK);
1496 SetLastError(wsaErrno());
1498 release_sock_fd( s, fd );
1500 return SOCKET_ERROR;
1503 release_sock_fd( s, fd );
1504 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1505 FD_WINE_CONNECTED|FD_READ|FD_WRITE,
1506 FD_CONNECT|FD_WINE_LISTENING);
1510 /***********************************************************************
1511 * WSAConnect (WS2_32.30)
1513 int WINAPI WSAConnect( SOCKET s, const struct WS_sockaddr* name, int namelen,
1514 LPWSABUF lpCallerData, LPWSABUF lpCalleeData,
1515 LPQOS lpSQOS, LPQOS lpGQOS )
1517 if ( lpCallerData || lpCalleeData || lpSQOS || lpGQOS )
1518 FIXME("unsupported parameters!\n");
1519 return WS_connect( s, name, namelen );
1523 /***********************************************************************
1524 * getpeername (WS2_32.5)
1526 int WINAPI WS_getpeername(SOCKET s, struct WS_sockaddr *name, int *namelen)
1531 TRACE("socket: %04lx, ptr %p, len %08x\n", s, name, *namelen);
1533 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1534 if( (name == NULL) || (namelen == NULL) )
1536 SetLastError( WSAEFAULT );
1537 return SOCKET_ERROR;
1540 fd = get_sock_fd( s, 0, NULL );
1545 union generic_unix_sockaddr uaddr;
1546 unsigned int uaddrlen = sizeof(uaddr);
1548 if (getpeername(fd, &uaddr.addr, &uaddrlen) != 0)
1550 SetLastError(wsaErrno());
1552 else if (ws_sockaddr_u2ws(&uaddr.addr, name, namelen) != 0)
1554 /* The buffer was too small */
1555 SetLastError(WSAEFAULT);
1561 release_sock_fd( s, fd );
1566 /***********************************************************************
1567 * getsockname (WS2_32.6)
1569 int WINAPI WS_getsockname(SOCKET s, struct WS_sockaddr *name, int *namelen)
1574 TRACE("socket: %04lx, ptr %p, len %8x\n", s, name, *namelen);
1576 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1577 if( (name == NULL) || (namelen == NULL) )
1579 SetLastError( WSAEFAULT );
1580 return SOCKET_ERROR;
1583 fd = get_sock_fd( s, 0, NULL );
1588 union generic_unix_sockaddr uaddr;
1589 unsigned int uaddrlen = sizeof(uaddr);
1591 if (getsockname(fd, &uaddr.addr, &uaddrlen) != 0)
1593 SetLastError(wsaErrno());
1595 else if (!is_sockaddr_bound(&uaddr.addr, uaddrlen))
1597 SetLastError(WSAEINVAL);
1599 else if (ws_sockaddr_u2ws(&uaddr.addr, name, namelen) != 0)
1601 /* The buffer was too small */
1602 SetLastError(WSAEFAULT);
1608 release_sock_fd( s, fd );
1613 /***********************************************************************
1614 * getsockopt (WS2_32.7)
1616 INT WINAPI WS_getsockopt(SOCKET s, INT level,
1617 INT optname, char *optval, INT *optlen)
1622 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
1623 s, level, optname, optval, *optlen);
1631 /* Handle common cases. The special cases are below, sorted
1633 case WS_SO_ACCEPTCONN:
1634 case WS_SO_BROADCAST:
1637 case WS_SO_KEEPALIVE:
1638 case WS_SO_OOBINLINE:
1642 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1643 return SOCKET_ERROR;
1644 convert_sockopt(&level, &optname);
1645 if (getsockopt(fd,(int) level, optname, optval,
1646 (unsigned int *)optlen) != 0 )
1648 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1651 release_sock_fd( s, fd );
1654 case WS_SO_DONTLINGER:
1656 struct linger lingval;
1657 unsigned int len = sizeof(struct linger);
1659 if (!optlen || *optlen < sizeof(BOOL)|| !optval)
1661 SetLastError(WSAEFAULT);
1662 return SOCKET_ERROR;
1664 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1665 return SOCKET_ERROR;
1667 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &lingval, &len) != 0 )
1669 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1674 *(BOOL *)optval = (lingval.l_onoff) ? FALSE : TRUE;
1675 *optlen = sizeof(BOOL);
1678 release_sock_fd( s, fd );
1682 /* As mentioned in setsockopt, Windows ignores this, so we
1683 * always return true here */
1684 case WS_SO_DONTROUTE:
1685 if (!optlen || *optlen < sizeof(BOOL) || !optval)
1687 SetLastError(WSAEFAULT);
1688 return SOCKET_ERROR;
1690 *(BOOL *)optval = TRUE;
1691 *optlen = sizeof(BOOL);
1696 struct linger lingval;
1697 unsigned int len = sizeof(struct linger);
1699 /* struct linger and LINGER have different sizes */
1700 if (!optlen || *optlen < sizeof(LINGER) || !optval)
1702 SetLastError(WSAEFAULT);
1703 return SOCKET_ERROR;
1705 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1706 return SOCKET_ERROR;
1708 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &lingval, &len) != 0 )
1710 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1715 ((LINGER *)optval)->l_onoff = lingval.l_onoff;
1716 ((LINGER *)optval)->l_linger = lingval.l_linger;
1717 *optlen = sizeof(struct linger);
1720 release_sock_fd( s, fd );
1724 case WS_SO_MAX_MSG_SIZE:
1725 if (!optlen || *optlen < sizeof(int) || !optval)
1727 SetLastError(WSAEFAULT);
1728 return SOCKET_ERROR;
1730 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
1731 *(int *)optval = 65507;
1732 *optlen = sizeof(int);
1735 /* SO_OPENTYPE does not require a valid socket handle. */
1736 case WS_SO_OPENTYPE:
1737 if (!optlen || *optlen < sizeof(int) || !optval)
1739 SetLastError(WSAEFAULT);
1740 return SOCKET_ERROR;
1742 *(int *)optval = get_per_thread_data()->opentype;
1743 *optlen = sizeof(int);
1744 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
1748 case WS_SO_RCVTIMEO:
1751 case WS_SO_SNDTIMEO:
1753 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
1756 unsigned int len = sizeof(struct timeval);
1758 if (!optlen || *optlen < sizeof(int)|| !optval)
1760 SetLastError(WSAEFAULT);
1761 return SOCKET_ERROR;
1763 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1764 return SOCKET_ERROR;
1766 convert_sockopt(&level, &optname);
1767 if (getsockopt(fd,(int) level, optname, &tv, &len) != 0 )
1769 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1774 *(int *)optval = tv.tv_sec * 1000 + tv.tv_usec / 1000;
1775 *optlen = sizeof(int);
1778 release_sock_fd( s, fd );
1782 /* As mentioned in setsockopt, the windows style SO_REUSEADDR is
1783 * not possible in Unix, so always return false here. */
1784 case WS_SO_REUSEADDR:
1785 if (!optlen || *optlen < sizeof(int) || !optval)
1787 SetLastError(WSAEFAULT);
1788 return SOCKET_ERROR;
1791 *optlen = sizeof(int);
1795 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname);
1796 SetLastError(WSAENOPROTOOPT);
1797 return SOCKET_ERROR;
1798 } /* end switch(optname) */
1799 }/* end case WS_SOL_SOCKET */
1803 struct WS_sockaddr_ipx addr;
1804 IPX_ADDRESS_DATA *data;
1809 if ((fd = get_sock_fd( s, 0, NULL )) == -1) return SOCKET_ERROR;
1811 if(getsockopt(fd, SOL_IPX, IPX_TYPE, optval, (unsigned int*)optlen) == -1)
1818 socklen_t len=sizeof(struct ipx);
1819 if(getsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, &len) == -1 )
1822 *optval = (int)val.ipx_pt;
1825 TRACE("ptype: %d (fd: %d)\n", *(int*)optval, fd);
1826 release_sock_fd( s, fd );
1831 * On a Win2000 system with one network card there are usually
1832 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
1833 * Using this call you can then retrieve info about this all.
1834 * In case of Linux it is a bit different. Usually you have
1835 * only "one" device active and further it is not possible to
1836 * query things like the linkspeed.
1838 FIXME("IPX_ADDRESS\n");
1839 namelen = sizeof(struct WS_sockaddr_ipx);
1840 memset(&addr, 0, sizeof(struct WS_sockaddr_ipx));
1841 WS_getsockname(s, (struct WS_sockaddr*)&addr, &namelen);
1843 data = (IPX_ADDRESS_DATA*)optval;
1844 memcpy(data->nodenum,&addr.sa_nodenum,sizeof(data->nodenum));
1845 memcpy(data->netnum,&addr.sa_netnum,sizeof(data->netnum));
1846 data->adapternum = 0;
1847 data->wan = FALSE; /* We are not on a wan for now .. */
1848 data->status = FALSE; /* Since we are not on a wan, the wan link isn't up */
1849 data->maxpkt = 1467; /* This value is the default one, at least on Win2k/WinXP */
1850 data->linkspeed = 100000; /* Set the line speed in 100bit/s to 10 Mbit;
1851 * note 1MB = 1000kB in this case */
1854 case IPX_MAX_ADAPTER_NUM:
1855 FIXME("IPX_MAX_ADAPTER_NUM\n");
1856 *(int*)optval = 1; /* As noted under IPX_ADDRESS we have just one card. */
1860 FIXME("IPX optname:%x\n", optname);
1861 return SOCKET_ERROR;
1862 }/* end switch(optname) */
1863 } /* end case NSPROTO_IPX */
1865 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
1866 case WS_IPPROTO_TCP:
1869 case WS_TCP_NODELAY:
1870 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1871 return SOCKET_ERROR;
1872 convert_sockopt(&level, &optname);
1873 if (getsockopt(fd,(int) level, optname, optval,
1874 (unsigned int *)optlen) != 0 )
1876 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1879 release_sock_fd( s, fd );
1882 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname);
1883 return SOCKET_ERROR;
1888 case WS_IP_ADD_MEMBERSHIP:
1889 case WS_IP_DROP_MEMBERSHIP:
1893 case WS_IP_MULTICAST_IF:
1894 case WS_IP_MULTICAST_LOOP:
1895 case WS_IP_MULTICAST_TTL:
1899 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1900 return SOCKET_ERROR;
1901 convert_sockopt(&level, &optname);
1902 if (getsockopt(fd,(int) level, optname, optval,
1903 (unsigned int *)optlen) != 0 )
1905 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1908 release_sock_fd( s, fd );
1911 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname);
1912 return SOCKET_ERROR;
1915 FIXME("Unknown level: 0x%08x\n", level);
1916 return SOCKET_ERROR;
1917 } /* end switch(level) */
1920 /***********************************************************************
1924 WS_u_long WINAPI WS_htonl(WS_u_long hostlong)
1926 return htonl(hostlong);
1930 /***********************************************************************
1934 WS_u_short WINAPI WS_htons(WS_u_short hostshort)
1936 return htons(hostshort);
1939 /***********************************************************************
1940 * WSAHtonl (WS2_32.46)
1941 * From MSDN decription of error codes, this function should also
1942 * check if WinSock has been initialized and the socket is a valid
1943 * socket. But why? This function only translates a host byte order
1944 * u_long into a network byte order u_long...
1946 int WINAPI WSAHtonl(SOCKET s, WS_u_long hostlong, WS_u_long *lpnetlong)
1950 *lpnetlong = htonl(hostlong);
1953 WSASetLastError(WSAEFAULT);
1954 return SOCKET_ERROR;
1957 /***********************************************************************
1958 * WSAHtons (WS2_32.47)
1959 * From MSDN decription of error codes, this function should also
1960 * check if WinSock has been initialized and the socket is a valid
1961 * socket. But why? This function only translates a host byte order
1962 * u_short into a network byte order u_short...
1964 int WINAPI WSAHtons(SOCKET s, WS_u_short hostshort, WS_u_short *lpnetshort)
1969 *lpnetshort = htons(hostshort);
1972 WSASetLastError(WSAEFAULT);
1973 return SOCKET_ERROR;
1977 /***********************************************************************
1978 * inet_addr (WINSOCK.10)
1979 * inet_addr (WS2_32.11)
1981 WS_u_long WINAPI WS_inet_addr(const char *cp)
1983 if (!cp) return INADDR_NONE;
1984 return inet_addr(cp);
1988 /***********************************************************************
1989 * ntohl (WINSOCK.14)
1992 WS_u_long WINAPI WS_ntohl(WS_u_long netlong)
1994 return ntohl(netlong);
1998 /***********************************************************************
1999 * ntohs (WINSOCK.15)
2002 WS_u_short WINAPI WS_ntohs(WS_u_short netshort)
2004 return ntohs(netshort);
2008 /***********************************************************************
2009 * inet_ntoa (WS2_32.12)
2011 char* WINAPI WS_inet_ntoa(struct WS_in_addr in)
2013 /* use "buffer for dummies" here because some applications have a
2014 * propensity to decode addresses in ws_hostent structure without
2015 * saving them first...
2017 static char dbuffer[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
2019 char* s = inet_ntoa(*((struct in_addr*)&in));
2025 SetLastError(wsaErrno());
2029 /**********************************************************************
2030 * WSAIoctl (WS2_32.50)
2033 INT WINAPI WSAIoctl(SOCKET s,
2034 DWORD dwIoControlCode,
2037 LPVOID lpbOutBuffer,
2039 LPDWORD lpcbBytesReturned,
2040 LPWSAOVERLAPPED lpOverlapped,
2041 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
2043 TRACE("%ld, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
2044 s, dwIoControlCode, lpvInBuffer, cbInBuffer, lpbOutBuffer,
2045 cbOutBuffer, lpcbBytesReturned, lpOverlapped, lpCompletionRoutine);
2047 switch( dwIoControlCode )
2050 if (cbInBuffer != sizeof(WS_u_long)) {
2051 WSASetLastError(WSAEFAULT);
2052 return SOCKET_ERROR;
2054 return WS_ioctlsocket( s, WS_FIONBIO, lpvInBuffer);
2057 if (cbOutBuffer != sizeof(WS_u_long)) {
2058 WSASetLastError(WSAEFAULT);
2059 return SOCKET_ERROR;
2061 return WS_ioctlsocket( s, WS_FIONREAD, lpbOutBuffer);
2063 case WS_SIO_GET_INTERFACE_LIST:
2065 INTERFACE_INFO* intArray = (INTERFACE_INFO*)lpbOutBuffer;
2066 DWORD size, numInt, apiReturn;
2069 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
2073 WSASetLastError(WSAEFAULT);
2074 return SOCKET_ERROR;
2076 if (!lpcbBytesReturned)
2078 WSASetLastError(WSAEFAULT);
2079 return SOCKET_ERROR;
2082 fd = get_sock_fd( s, 0, NULL );
2083 if (fd == -1) return SOCKET_ERROR;
2085 apiReturn = GetAdaptersInfo(NULL, &size);
2086 if (apiReturn == ERROR_NO_DATA)
2090 else if (apiReturn == ERROR_BUFFER_OVERFLOW)
2092 PIP_ADAPTER_INFO table = HeapAlloc(GetProcessHeap(),0,size);
2096 if (GetAdaptersInfo(table, &size) == NO_ERROR)
2098 PIP_ADAPTER_INFO ptr;
2100 if (size*sizeof(INTERFACE_INFO)/sizeof(IP_ADAPTER_INFO) > cbOutBuffer)
2102 WARN("Buffer too small = %u, cbOutBuffer = %u\n", size, cbOutBuffer);
2103 HeapFree(GetProcessHeap(),0,table);
2104 release_sock_fd( s, fd );
2105 WSASetLastError(WSAEFAULT);
2106 return SOCKET_ERROR;
2108 for (ptr = table, numInt = 0; ptr;
2109 ptr = ptr->Next, intArray++, numInt++)
2111 unsigned int addr, mask, bcast;
2112 struct ifreq ifInfo;
2114 /* Socket Status Flags */
2115 lstrcpynA(ifInfo.ifr_name, ptr->AdapterName, IFNAMSIZ);
2116 if (ioctl(fd, SIOCGIFFLAGS, &ifInfo) < 0)
2118 ERR("Error obtaining status flags for socket!\n");
2119 HeapFree(GetProcessHeap(),0,table);
2120 release_sock_fd( s, fd );
2121 WSASetLastError(WSAEINVAL);
2122 return SOCKET_ERROR;
2126 /* set flags; the values of IFF_* are not the same
2127 under Linux and Windows, therefore must generate
2129 intArray->iiFlags = 0;
2130 if (ifInfo.ifr_flags & IFF_BROADCAST)
2131 intArray->iiFlags |= WS_IFF_BROADCAST;
2132 #ifdef IFF_POINTOPOINT
2133 if (ifInfo.ifr_flags & IFF_POINTOPOINT)
2134 intArray->iiFlags |= WS_IFF_POINTTOPOINT;
2136 if (ifInfo.ifr_flags & IFF_LOOPBACK)
2137 intArray->iiFlags |= WS_IFF_LOOPBACK;
2138 if (ifInfo.ifr_flags & IFF_UP)
2139 intArray->iiFlags |= WS_IFF_UP;
2140 if (ifInfo.ifr_flags & IFF_MULTICAST)
2141 intArray->iiFlags |= WS_IFF_MULTICAST;
2144 addr = inet_addr(ptr->IpAddressList.IpAddress.String);
2145 mask = inet_addr(ptr->IpAddressList.IpMask.String);
2146 bcast = addr | (addr & !mask);
2147 intArray->iiAddress.AddressIn.sin_family = AF_INET;
2148 intArray->iiAddress.AddressIn.sin_port = 0;
2149 intArray->iiAddress.AddressIn.sin_addr.WS_s_addr =
2151 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
2152 intArray->iiNetmask.AddressIn.sin_port = 0;
2153 intArray->iiNetmask.AddressIn.sin_addr.WS_s_addr =
2155 intArray->iiBroadcastAddress.AddressIn.sin_family =
2157 intArray->iiBroadcastAddress.AddressIn.sin_port = 0;
2158 intArray->iiBroadcastAddress.AddressIn.sin_addr.
2164 ERR("Unable to get interface table!\n");
2165 release_sock_fd( s, fd );
2166 HeapFree(GetProcessHeap(),0,table);
2167 WSASetLastError(WSAEINVAL);
2168 return SOCKET_ERROR;
2170 HeapFree(GetProcessHeap(),0,table);
2174 release_sock_fd( s, fd );
2175 WSASetLastError(WSAEINVAL);
2176 return SOCKET_ERROR;
2181 ERR("Unable to get interface table!\n");
2182 release_sock_fd( s, fd );
2183 WSASetLastError(WSAEINVAL);
2184 return SOCKET_ERROR;
2186 /* Calculate the size of the array being returned */
2187 *lpcbBytesReturned = sizeof(INTERFACE_INFO) * numInt;
2188 release_sock_fd( s, fd );
2192 case WS_SIO_ADDRESS_LIST_CHANGE:
2193 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
2194 /* FIXME: error and return code depend on whether socket was created
2195 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
2198 case WS_SIO_ADDRESS_LIST_QUERY:
2202 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
2204 if (!lpcbBytesReturned)
2206 WSASetLastError(WSAEFAULT);
2207 return SOCKET_ERROR;
2210 if (GetAdaptersInfo(NULL, &size) == ERROR_BUFFER_OVERFLOW)
2212 IP_ADAPTER_INFO *p, *table = HeapAlloc(GetProcessHeap(), 0, size);
2215 if (!table || GetAdaptersInfo(table, &size))
2217 HeapFree(GetProcessHeap(), 0, table);
2218 WSASetLastError(WSAEINVAL);
2219 return SOCKET_ERROR;
2222 for (p = table, num = 0; p; p = p->Next)
2223 if (p->IpAddressList.IpAddress.String[0]) num++;
2225 need = sizeof(SOCKET_ADDRESS_LIST) + sizeof(SOCKET_ADDRESS) * (num - 1);
2226 need += sizeof(SOCKADDR) * num;
2227 *lpcbBytesReturned = need;
2229 if (need > cbOutBuffer)
2231 HeapFree(GetProcessHeap(), 0, table);
2232 WSASetLastError(WSAEFAULT);
2233 return SOCKET_ERROR;
2240 SOCKET_ADDRESS_LIST *sa_list = (SOCKET_ADDRESS_LIST *)lpbOutBuffer;
2241 SOCKADDR_IN *sockaddr;
2243 sa = sa_list->Address;
2244 sockaddr = (SOCKADDR_IN *)((char *)sa + num * sizeof(SOCKET_ADDRESS));
2245 sa_list->iAddressCount = num;
2247 for (p = table, i = 0; p; p = p->Next)
2249 if (!p->IpAddressList.IpAddress.String[0]) continue;
2251 sa[i].lpSockaddr = (SOCKADDR *)&sockaddr[i];
2252 sa[i].iSockaddrLength = sizeof(SOCKADDR);
2254 sockaddr[i].sin_family = AF_INET;
2255 sockaddr[i].sin_port = 0;
2256 sockaddr[i].sin_addr.WS_s_addr = inet_addr(p->IpAddressList.IpAddress.String);
2261 HeapFree(GetProcessHeap(), 0, table);
2266 WARN("unable to get IP address list\n");
2267 WSASetLastError(WSAEINVAL);
2268 return SOCKET_ERROR;
2272 FIXME("SIO_FLUSH: stub.\n");
2276 FIXME("unsupported WS_IOCTL cmd (%08x)\n", dwIoControlCode);
2277 WSASetLastError(WSAEOPNOTSUPP);
2278 return SOCKET_ERROR;
2285 /***********************************************************************
2286 * ioctlsocket (WS2_32.10)
2288 int WINAPI WS_ioctlsocket(SOCKET s, LONG cmd, WS_u_long *argp)
2293 TRACE("socket %04lx, cmd %08x, ptr %p\n", s, cmd, argp);
2302 if( _get_sock_mask(s) )
2304 /* AsyncSelect()'ed sockets are always nonblocking */
2305 if (*argp) return 0;
2306 SetLastError(WSAEINVAL);
2307 return SOCKET_ERROR;
2309 fd = get_sock_fd( s, 0, NULL );
2315 _enable_event(SOCKET2HANDLE(s), 0, FD_WINE_NONBLOCKING, 0);
2316 ret = fcntl( fd, F_SETFL, O_NONBLOCK );
2320 _enable_event(SOCKET2HANDLE(s), 0, 0, FD_WINE_NONBLOCKING);
2321 ret = fcntl( fd, F_SETFL, 0 );
2323 release_sock_fd( s, fd );
2325 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2327 return SOCKET_ERROR;
2334 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
2335 SetLastError(WSAEINVAL);
2336 return SOCKET_ERROR;
2338 case SIOCGIFBRDADDR:
2339 case SIOCGIFNETMASK:
2341 /* These don't need any special handling. They are used by
2342 WsControl, and are here to suppress an unnecessary warning. */
2346 /* Netscape tries hard to use bogus ioctl 0x667e */
2347 /* FIXME: 0x667e above is ('f' << 8) | 126, and is a low word of
2348 * FIONBIO (_IOW('f', 126, u_long)), how that should be handled?
2350 WARN("\tunknown WS_IOCTL cmd (%08x)\n", cmd);
2354 fd = get_sock_fd( s, 0, NULL );
2357 if( ioctl(fd, newcmd, (char*)argp ) == 0 )
2359 release_sock_fd( s, fd );
2362 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2363 release_sock_fd( s, fd );
2365 return SOCKET_ERROR;
2368 /***********************************************************************
2369 * listen (WS2_32.13)
2371 int WINAPI WS_listen(SOCKET s, int backlog)
2373 int fd = get_sock_fd( s, FILE_READ_DATA, NULL );
2375 TRACE("socket %04lx, backlog %d\n", s, backlog);
2378 if (listen(fd, backlog) == 0)
2380 release_sock_fd( s, fd );
2381 _enable_event(SOCKET2HANDLE(s), FD_ACCEPT,
2383 FD_CONNECT|FD_WINE_CONNECTED);
2386 SetLastError(wsaErrno());
2387 release_sock_fd( s, fd );
2389 return SOCKET_ERROR;
2392 /***********************************************************************
2395 int WINAPI WS_recv(SOCKET s, char *buf, int len, int flags)
2397 DWORD n, dwFlags = flags;
2403 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, NULL, NULL, NULL, NULL) == SOCKET_ERROR )
2404 return SOCKET_ERROR;
2409 /***********************************************************************
2410 * recvfrom (WS2_32.17)
2412 int WINAPI WS_recvfrom(SOCKET s, char *buf, INT len, int flags,
2413 struct WS_sockaddr *from, int *fromlen)
2415 DWORD n, dwFlags = flags;
2421 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, from, fromlen, NULL, NULL) == SOCKET_ERROR )
2422 return SOCKET_ERROR;
2427 /* allocate a poll array for the corresponding fd sets */
2428 static struct pollfd *fd_sets_to_poll( const WS_fd_set *readfds, const WS_fd_set *writefds,
2429 const WS_fd_set *exceptfds, int *count_ptr )
2431 int i, j = 0, count = 0;
2434 if (readfds) count += readfds->fd_count;
2435 if (writefds) count += writefds->fd_count;
2436 if (exceptfds) count += exceptfds->fd_count;
2438 if (!count) return NULL;
2439 if (!(fds = HeapAlloc( GetProcessHeap(), 0, count * sizeof(fds[0])))) return NULL;
2441 for (i = 0; i < readfds->fd_count; i++, j++)
2443 fds[j].fd = get_sock_fd( readfds->fd_array[i], FILE_READ_DATA, NULL );
2444 fds[j].events = POLLIN;
2448 for (i = 0; i < writefds->fd_count; i++, j++)
2450 fds[j].fd = get_sock_fd( writefds->fd_array[i], FILE_WRITE_DATA, NULL );
2451 fds[j].events = POLLOUT;
2455 for (i = 0; i < exceptfds->fd_count; i++, j++)
2457 fds[j].fd = get_sock_fd( exceptfds->fd_array[i], 0, NULL );
2458 fds[j].events = POLLHUP;
2464 /* release the file descriptor obtained in fd_sets_to_poll */
2465 /* must be called with the original fd_set arrays, before calling get_poll_results */
2466 static void release_poll_fds( const WS_fd_set *readfds, const WS_fd_set *writefds,
2467 const WS_fd_set *exceptfds, struct pollfd *fds )
2473 for (i = 0; i < readfds->fd_count; i++, j++)
2474 if (fds[j].fd != -1) release_sock_fd( readfds->fd_array[i], fds[j].fd );
2478 for (i = 0; i < writefds->fd_count; i++, j++)
2479 if (fds[j].fd != -1) release_sock_fd( writefds->fd_array[i], fds[j].fd );
2483 for (i = 0; i < exceptfds->fd_count; i++, j++)
2484 if (fds[j].fd != -1)
2486 /* make sure we have a real error before releasing the fd */
2487 if (!sock_error_p( fds[j].fd )) fds[j].revents = 0;
2488 release_sock_fd( exceptfds->fd_array[i], fds[j].fd );
2493 /* map the poll results back into the Windows fd sets */
2494 static int get_poll_results( WS_fd_set *readfds, WS_fd_set *writefds, WS_fd_set *exceptfds,
2495 const struct pollfd *fds )
2497 int i, j = 0, k, total = 0;
2501 for (i = k = 0; i < readfds->fd_count; i++, j++)
2502 if (fds[j].revents) readfds->fd_array[k++] = readfds->fd_array[i];
2503 readfds->fd_count = k;
2508 for (i = k = 0; i < writefds->fd_count; i++, j++)
2509 if (fds[j].revents) writefds->fd_array[k++] = writefds->fd_array[i];
2510 writefds->fd_count = k;
2515 for (i = k = 0; i < exceptfds->fd_count; i++, j++)
2516 if (fds[j].revents) exceptfds->fd_array[k++] = exceptfds->fd_array[i];
2517 exceptfds->fd_count = k;
2524 /***********************************************************************
2525 * select (WS2_32.18)
2527 int WINAPI WS_select(int nfds, WS_fd_set *ws_readfds,
2528 WS_fd_set *ws_writefds, WS_fd_set *ws_exceptfds,
2529 const struct WS_timeval* ws_timeout)
2531 struct pollfd *pollfds;
2532 int count, ret, timeout = -1;
2534 TRACE("read %p, write %p, excp %p timeout %p\n",
2535 ws_readfds, ws_writefds, ws_exceptfds, ws_timeout);
2537 if (!(pollfds = fd_sets_to_poll( ws_readfds, ws_writefds, ws_exceptfds, &count )) && count)
2539 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
2540 return SOCKET_ERROR;
2543 if (ws_timeout) timeout = (ws_timeout->tv_sec * 1000) + (ws_timeout->tv_usec + 999) / 1000;
2545 ret = poll( pollfds, count, timeout );
2546 release_poll_fds( ws_readfds, ws_writefds, ws_exceptfds, pollfds );
2548 if (ret == -1) SetLastError(wsaErrno());
2549 else ret = get_poll_results( ws_readfds, ws_writefds, ws_exceptfds, pollfds );
2550 HeapFree( GetProcessHeap(), 0, pollfds );
2555 /***********************************************************************
2558 int WINAPI WS_send(SOCKET s, const char *buf, int len, int flags)
2564 wsabuf.buf = (char*) buf;
2566 if ( WSASendTo( s, &wsabuf, 1, &n, flags, NULL, 0, NULL, NULL) == SOCKET_ERROR )
2567 return SOCKET_ERROR;
2572 /***********************************************************************
2573 * WSASend (WS2_32.72)
2575 INT WINAPI WSASend( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2576 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2577 LPWSAOVERLAPPED lpOverlapped,
2578 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2580 return WSASendTo( s, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags,
2581 NULL, 0, lpOverlapped, lpCompletionRoutine );
2584 /***********************************************************************
2585 * WSASendDisconnect (WS2_32.73)
2587 INT WINAPI WSASendDisconnect( SOCKET s, LPWSABUF lpBuffers )
2589 return WS_shutdown( s, SD_SEND );
2593 /***********************************************************************
2594 * WSASendTo (WS2_32.74)
2596 INT WINAPI WSASendTo( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2597 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2598 const struct WS_sockaddr *to, int tolen,
2599 LPWSAOVERLAPPED lpOverlapped,
2600 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2602 unsigned int i, options;
2604 struct iovec iovec[WS_MSG_MAXIOVLEN];
2606 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
2607 s, lpBuffers, dwBufferCount, dwFlags,
2608 to, tolen, lpOverlapped, lpCompletionRoutine);
2610 if (dwBufferCount > WS_MSG_MAXIOVLEN)
2612 WSASetLastError( WSAEINVAL );
2613 return SOCKET_ERROR;
2616 fd = get_sock_fd( s, FILE_WRITE_DATA, &options );
2617 TRACE( "fd=%d, options=%x\n", fd, options );
2619 if ( fd == -1 ) return SOCKET_ERROR;
2621 if ( !lpNumberOfBytesSent )
2627 for ( i = 0; i < dwBufferCount; i++ )
2629 iovec[i].iov_base = lpBuffers[i].buf;
2630 iovec[i].iov_len = lpBuffers[i].len;
2635 n = WS2_send( fd, iovec, dwBufferCount, to, tolen, dwFlags );
2636 if (n != -1 || errno != EINTR) break;
2638 if (n == -1 && errno != EAGAIN)
2644 if ((lpOverlapped || lpCompletionRoutine) &&
2645 !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
2647 IO_STATUS_BLOCK *iosb;
2648 struct ws2_async *wsa = HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa) );
2655 release_sock_fd( s, fd );
2657 wsa->hSocket = SOCKET2HANDLE(s);
2658 wsa->addr = (struct WS_sockaddr *)to;
2659 wsa->addrlen.val = tolen;
2661 wsa->user_overlapped = lpOverlapped;
2662 wsa->completion_func = lpCompletionRoutine;
2663 wsa->n_iovecs = dwBufferCount;
2664 memcpy( wsa->iovec, iovec, dwBufferCount * sizeof(*iovec) );
2666 iosb = lpOverlapped ? (IO_STATUS_BLOCK *)lpOverlapped : &wsa->local_iosb;
2669 iosb->u.Status = STATUS_PENDING;
2670 iosb->Information = 0;
2672 SERVER_START_REQ( register_async )
2674 req->handle = wsa->hSocket;
2675 req->type = ASYNC_TYPE_WRITE;
2676 req->async.callback = WS2_async_send;
2677 req->async.iosb = iosb;
2678 req->async.arg = wsa;
2679 req->async.apc = ws2_async_apc;
2680 req->async.event = lpCompletionRoutine ? 0 : lpOverlapped->hEvent;
2681 err = wine_server_call( req );
2685 if (err != STATUS_PENDING) HeapFree( GetProcessHeap(), 0, wsa );
2686 WSASetLastError( NtStatusToWSAError( err ));
2687 return SOCKET_ERROR;
2690 iosb->u.Status = STATUS_SUCCESS;
2691 iosb->Information = n;
2692 *lpNumberOfBytesSent = n;
2693 if (!wsa->completion_func)
2695 SetEvent( lpOverlapped->hEvent );
2696 HeapFree( GetProcessHeap(), 0, wsa );
2698 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)ws2_async_apc,
2699 (ULONG_PTR)wsa, (ULONG_PTR)iosb, 0 );
2703 if ( _is_blocking(s) )
2705 /* On a blocking non-overlapped stream socket,
2706 * sending blocks until the entire buffer is sent. */
2707 DWORD timeout_start = GetTickCount();
2708 unsigned int first_buff = 0;
2710 *lpNumberOfBytesSent = 0;
2712 while (first_buff < dwBufferCount)
2715 int timeout = GET_SNDTIMEO(fd);
2719 *lpNumberOfBytesSent += n;
2720 if (iovec[first_buff].iov_len > n)
2721 iovec[first_buff].iov_len -= n;
2724 while (n > 0) n -= iovec[first_buff++].iov_len;
2725 if (first_buff >= dwBufferCount) break;
2731 timeout -= timeout_start - GetTickCount();
2732 if (timeout < 0) timeout = 0;
2736 pfd.events = POLLOUT;
2738 if (!timeout || !poll( &pfd, 1, timeout ))
2741 goto error; /* msdn says a timeout in send is fatal */
2744 n = WS2_send( fd, iovec + first_buff, dwBufferCount - first_buff, to, tolen, dwFlags );
2745 if (n == -1 && errno != EAGAIN && errno != EINTR)
2752 else /* non-blocking */
2754 _enable_event(SOCKET2HANDLE(s), FD_WRITE, 0, 0);
2757 err = WSAEWOULDBLOCK;
2760 *lpNumberOfBytesSent = n;
2763 TRACE(" -> %i bytes\n", *lpNumberOfBytesSent);
2765 release_sock_fd( s, fd );
2769 release_sock_fd( s, fd );
2770 WARN(" -> ERROR %d\n", err);
2771 WSASetLastError(err);
2772 return SOCKET_ERROR;
2775 /***********************************************************************
2776 * sendto (WS2_32.20)
2778 int WINAPI WS_sendto(SOCKET s, const char *buf, int len, int flags,
2779 const struct WS_sockaddr *to, int tolen)
2785 wsabuf.buf = (char*) buf;
2787 if ( WSASendTo(s, &wsabuf, 1, &n, flags, to, tolen, NULL, NULL) == SOCKET_ERROR )
2788 return SOCKET_ERROR;
2793 /***********************************************************************
2794 * setsockopt (WS2_32.21)
2796 int WINAPI WS_setsockopt(SOCKET s, int level, int optname,
2797 const char *optval, int optlen)
2801 struct linger linger;
2802 struct timeval tval;
2804 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
2805 s, level, optname, optval, optlen);
2812 /* Some options need some conversion before they can be sent to
2813 * setsockopt. The conversions are done here, then they will fall though
2814 * to the general case. Special options that are not passed to
2815 * setsockopt follow below that.*/
2817 case WS_SO_DONTLINGER:
2818 linger.l_onoff = *((const int*)optval) ? 0: 1;
2819 linger.l_linger = 0;
2821 optname = SO_LINGER;
2822 optval = (char*)&linger;
2823 optlen = sizeof(struct linger);
2827 linger.l_onoff = ((LINGER*)optval)->l_onoff;
2828 linger.l_linger = ((LINGER*)optval)->l_linger;
2829 /* FIXME: what is documented behavior if SO_LINGER optval
2832 optname = SO_LINGER;
2833 optval = (char*)&linger;
2834 optlen = sizeof(struct linger);
2838 if (*(const int*)optval < 2048)
2840 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval );
2845 /* The options listed here don't need any special handling. Thanks to
2846 * the conversion happening above, options from there will fall through
2848 case WS_SO_ACCEPTCONN:
2849 case WS_SO_BROADCAST:
2851 case WS_SO_KEEPALIVE:
2852 case WS_SO_OOBINLINE:
2855 convert_sockopt(&level, &optname);
2858 /* SO_DEBUG is a privileged operation, ignore it. */
2860 TRACE("Ignoring SO_DEBUG\n");
2863 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
2864 * socket. According to MSDN, this option is silently ignored.*/
2865 case WS_SO_DONTROUTE:
2866 TRACE("Ignoring SO_DONTROUTE\n");
2869 /* Stops two sockets from being bound to the same port. Always happens
2870 * on unix systems, so just drop it. */
2871 case WS_SO_EXCLUSIVEADDRUSE:
2872 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
2875 /* SO_OPENTYPE does not require a valid socket handle. */
2876 case WS_SO_OPENTYPE:
2877 if (!optlen || optlen < sizeof(int) || !optval)
2879 SetLastError(WSAEFAULT);
2880 return SOCKET_ERROR;
2882 get_per_thread_data()->opentype = *(const int *)optval;
2883 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
2886 /* SO_REUSEADDR allows two applications to bind to the same port at at
2887 * same time. There is no direct way to do that in unix. While Wineserver
2888 * might do this, it does not seem useful for now, so just ignore it.*/
2889 case WS_SO_REUSEADDR:
2890 TRACE("Ignoring SO_REUSEADDR, does not translate\n");
2894 case WS_SO_RCVTIMEO:
2897 case WS_SO_SNDTIMEO:
2899 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
2900 if (optval && optlen == sizeof(UINT32)) {
2901 /* WinSock passes miliseconds instead of struct timeval */
2902 tval.tv_usec = (*(const UINT32*)optval % 1000) * 1000;
2903 tval.tv_sec = *(const UINT32*)optval / 1000;
2904 /* min of 500 milisec */
2905 if (tval.tv_sec == 0 && tval.tv_usec < 500000)
2906 tval.tv_usec = 500000;
2907 optlen = sizeof(struct timeval);
2908 optval = (char*)&tval;
2909 } else if (optlen == sizeof(struct timeval)) {
2910 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen);
2912 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen);
2915 convert_sockopt(&level, &optname);
2920 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname);
2921 SetLastError(WSAENOPROTOOPT);
2922 return SOCKET_ERROR;
2924 break; /* case WS_SOL_SOCKET */
2931 fd = get_sock_fd( s, 0, NULL );
2932 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval, fd);
2934 /* We try to set the ipx type on ipx socket level. */
2936 if(setsockopt(fd, SOL_IPX, IPX_TYPE, optval, optlen) == -1)
2938 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
2939 release_sock_fd( s, fd );
2940 return SOCKET_ERROR;
2945 /* Should we retrieve val using a getsockopt call and then
2946 * set the modified one? */
2947 val.ipx_pt = *optval;
2948 setsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, sizeof(struct ipx));
2951 release_sock_fd( s, fd );
2954 case IPX_FILTERPTYPE:
2955 /* Sets the receive filter packet type, at the moment we don't support it */
2956 FIXME("IPX_FILTERPTYPE: %x\n", *optval);
2957 /* Returning 0 is better for now than returning a SOCKET_ERROR */
2961 FIXME("opt_name:%x\n", optname);
2962 return SOCKET_ERROR;
2964 break; /* case NSPROTO_IPX */
2967 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
2968 case WS_IPPROTO_TCP:
2971 case WS_TCP_NODELAY:
2972 convert_sockopt(&level, &optname);
2975 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname);
2976 return SOCKET_ERROR;
2983 case WS_IP_ADD_MEMBERSHIP:
2984 case WS_IP_DROP_MEMBERSHIP:
2988 case WS_IP_MULTICAST_IF:
2989 case WS_IP_MULTICAST_LOOP:
2990 case WS_IP_MULTICAST_TTL:
2994 convert_sockopt(&level, &optname);
2997 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname);
2998 return SOCKET_ERROR;
3003 FIXME("Unknown level: 0x%08x\n", level);
3004 return SOCKET_ERROR;
3005 } /* end switch(level) */
3007 /* avoid endianness issues if argument is a 16-bit int */
3008 if (optval && optlen < sizeof(int))
3010 woptval= *((const INT16 *) optval);
3011 optval= (char*) &woptval;
3014 fd = get_sock_fd( s, 0, NULL );
3015 if (fd == -1) return SOCKET_ERROR;
3017 if (setsockopt(fd, level, optname, optval, optlen) == 0)
3019 release_sock_fd( s, fd );
3022 TRACE("Setting socket error, %d\n", wsaErrno());
3023 SetLastError(wsaErrno());
3024 release_sock_fd( s, fd );
3026 return SOCKET_ERROR;
3029 /***********************************************************************
3030 * shutdown (WS2_32.22)
3032 int WINAPI WS_shutdown(SOCKET s, int how)
3034 int fd, err = WSAENOTSOCK;
3035 unsigned int options, clear_flags = 0;
3037 fd = get_sock_fd( s, 0, &options );
3038 TRACE("socket %04lx, how %i %x\n", s, how, options );
3041 return SOCKET_ERROR;
3045 case 0: /* drop receives */
3046 clear_flags |= FD_READ;
3048 case 1: /* drop sends */
3049 clear_flags |= FD_WRITE;
3051 case 2: /* drop all */
3052 clear_flags |= FD_READ|FD_WRITE;
3054 clear_flags |= FD_WINE_LISTENING;
3057 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
3062 err = WS2_register_async_shutdown( s, ASYNC_TYPE_READ );
3065 err = WS2_register_async_shutdown( s, ASYNC_TYPE_WRITE );
3069 err = WS2_register_async_shutdown( s, ASYNC_TYPE_READ );
3070 if (!err) err = WS2_register_async_shutdown( s, ASYNC_TYPE_WRITE );
3073 if (err) goto error;
3075 else /* non-overlapped mode */
3077 if ( shutdown( fd, how ) )
3084 release_sock_fd( s, fd );
3085 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
3086 if ( how > 1) WSAAsyncSelect( s, 0, 0, 0 );
3090 release_sock_fd( s, fd );
3091 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
3092 WSASetLastError( err );
3093 return SOCKET_ERROR;
3096 /***********************************************************************
3097 * socket (WS2_32.23)
3099 SOCKET WINAPI WS_socket(int af, int type, int protocol)
3101 TRACE("af=%d type=%d protocol=%d\n", af, type, protocol);
3103 return WSASocketA( af, type, protocol, NULL, 0,
3104 get_per_thread_data()->opentype ? 0 : WSA_FLAG_OVERLAPPED );
3108 /***********************************************************************
3109 * gethostbyaddr (WS2_32.51)
3111 struct WS_hostent* WINAPI WS_gethostbyaddr(const char *addr, int len, int type)
3113 struct WS_hostent *retval = NULL;
3114 struct hostent* host;
3116 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3119 struct hostent hostentry;
3122 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
3124 int res = gethostbyaddr_r(addr, len, type,
3125 &hostentry, extrabuf, ebufsize, &host, &locerr);
3126 if( res != ERANGE) break;
3128 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
3130 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
3132 EnterCriticalSection( &csWSgetXXXbyYYY );
3133 host = gethostbyaddr(addr, len, type);
3134 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
3136 if( host != NULL ) retval = WS_dup_he(host);
3137 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3138 HeapFree(GetProcessHeap(),0,extrabuf);
3140 LeaveCriticalSection( &csWSgetXXXbyYYY );
3142 TRACE("ptr %p, len %d, type %d ret %p\n", addr, len, type, retval);
3146 /***********************************************************************
3147 * gethostbyname (WS2_32.52)
3149 struct WS_hostent* WINAPI WS_gethostbyname(const char* name)
3151 struct WS_hostent *retval = NULL;
3152 struct hostent* host;
3153 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3156 struct hostent hostentry;
3157 int locerr = ENOBUFS;
3160 if( !name || !name[0]) {
3162 if( gethostname( buf, 100) == -1) {
3163 SetLastError( WSAENOBUFS); /* appropriate ? */
3167 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3169 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
3171 int res = gethostbyname_r(name, &hostentry, extrabuf, ebufsize, &host, &locerr);
3172 if( res != ERANGE) break;
3174 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
3176 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
3178 EnterCriticalSection( &csWSgetXXXbyYYY );
3179 host = gethostbyname(name);
3180 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
3182 if (host) retval = WS_dup_he(host);
3183 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3184 HeapFree(GetProcessHeap(),0,extrabuf);
3186 LeaveCriticalSection( &csWSgetXXXbyYYY );
3188 TRACE( "%s ret %p\n", debugstr_a(name), retval );
3193 /***********************************************************************
3194 * getprotobyname (WS2_32.53)
3196 struct WS_protoent* WINAPI WS_getprotobyname(const char* name)
3198 struct WS_protoent* retval = NULL;
3199 #ifdef HAVE_GETPROTOBYNAME
3200 struct protoent* proto;
3201 EnterCriticalSection( &csWSgetXXXbyYYY );
3202 if( (proto = getprotobyname(name)) != NULL )
3204 retval = WS_dup_pe(proto);
3207 MESSAGE("protocol %s not found; You might want to add "
3208 "this to /etc/protocols\n", debugstr_a(name) );
3209 SetLastError(WSANO_DATA);
3211 LeaveCriticalSection( &csWSgetXXXbyYYY );
3213 TRACE( "%s ret %p\n", debugstr_a(name), retval );
3218 /***********************************************************************
3219 * getprotobynumber (WS2_32.54)
3221 struct WS_protoent* WINAPI WS_getprotobynumber(int number)
3223 struct WS_protoent* retval = NULL;
3224 #ifdef HAVE_GETPROTOBYNUMBER
3225 struct protoent* proto;
3226 EnterCriticalSection( &csWSgetXXXbyYYY );
3227 if( (proto = getprotobynumber(number)) != NULL )
3229 retval = WS_dup_pe(proto);
3232 MESSAGE("protocol number %d not found; You might want to add "
3233 "this to /etc/protocols\n", number );
3234 SetLastError(WSANO_DATA);
3236 LeaveCriticalSection( &csWSgetXXXbyYYY );
3238 TRACE("%i ret %p\n", number, retval);
3243 /***********************************************************************
3244 * getservbyname (WS2_32.55)
3246 struct WS_servent* WINAPI WS_getservbyname(const char *name, const char *proto)
3248 struct WS_servent* retval = NULL;
3249 struct servent* serv;
3251 char *proto_str = NULL;
3253 if (!(name_str = strdup_lower(name))) return NULL;
3255 if (proto && *proto)
3257 if (!(proto_str = strdup_lower(proto)))
3259 HeapFree( GetProcessHeap(), 0, name_str );
3264 EnterCriticalSection( &csWSgetXXXbyYYY );
3265 serv = getservbyname(name_str, proto_str);
3268 retval = WS_dup_se(serv);
3270 else SetLastError(WSANO_DATA);
3271 LeaveCriticalSection( &csWSgetXXXbyYYY );
3272 HeapFree( GetProcessHeap(), 0, proto_str );
3273 HeapFree( GetProcessHeap(), 0, name_str );
3274 TRACE( "%s, %s ret %p\n", debugstr_a(name), debugstr_a(proto), retval );
3278 /***********************************************************************
3279 * freeaddrinfo (WS2_32.@)
3281 void WINAPI WS_freeaddrinfo(struct WS_addrinfo *res)
3284 struct WS_addrinfo *next;
3286 HeapFree(GetProcessHeap(),0,res->ai_canonname);
3287 HeapFree(GetProcessHeap(),0,res->ai_addr);
3288 next = res->ai_next;
3289 HeapFree(GetProcessHeap(),0,res);
3294 /* helper functions for getaddrinfo() */
3295 static int convert_aiflag_w2u(int winflags) {
3296 int i, unixflags = 0;
3298 for (i=0;i<sizeof(ws_aiflag_map)/sizeof(ws_aiflag_map[0]);i++)
3299 if (ws_aiflag_map[i][0] & winflags) {
3300 unixflags |= ws_aiflag_map[i][1];
3301 winflags &= ~ws_aiflag_map[i][0];
3304 FIXME("Unhandled windows AI_xxx flags %x\n", winflags);
3308 static int convert_aiflag_u2w(int unixflags) {
3309 int i, winflags = 0;
3311 for (i=0;i<sizeof(ws_aiflag_map)/sizeof(ws_aiflag_map[0]);i++)
3312 if (ws_aiflag_map[i][1] & unixflags) {
3313 winflags |= ws_aiflag_map[i][0];
3314 unixflags &= ~ws_aiflag_map[i][1];
3316 if (unixflags) /* will warn usually */
3317 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags);
3321 static int convert_eai_u2w(int unixret) {
3324 for (i=0;ws_eai_map[i][0];i++)
3325 if (ws_eai_map[i][1] == unixret)
3326 return ws_eai_map[i][0];
3330 /***********************************************************************
3331 * getaddrinfo (WS2_32.@)
3333 int WINAPI WS_getaddrinfo(LPCSTR nodename, LPCSTR servname, const struct WS_addrinfo *hints, struct WS_addrinfo **res)
3335 #if HAVE_GETADDRINFO
3336 struct addrinfo *unixaires = NULL;
3338 struct addrinfo unixhints, *punixhints = NULL;
3339 CHAR *node = NULL, *serv = NULL;
3342 if (!(node = strdup_lower(nodename))) return WSA_NOT_ENOUGH_MEMORY;
3345 if (!(serv = strdup_lower(servname))) {
3346 HeapFree(GetProcessHeap(), 0, node);
3347 return WSA_NOT_ENOUGH_MEMORY;
3352 punixhints = &unixhints;
3354 memset(&unixhints, 0, sizeof(unixhints));
3355 punixhints->ai_flags = convert_aiflag_w2u(hints->ai_flags);
3356 if (hints->ai_family == 0) /* wildcard, specific to getaddrinfo() */
3357 punixhints->ai_family = 0;
3359 punixhints->ai_family = convert_af_w2u(hints->ai_family);
3360 if (hints->ai_socktype == 0) /* wildcard, specific to getaddrinfo() */
3361 punixhints->ai_socktype = 0;
3363 punixhints->ai_socktype = convert_socktype_w2u(hints->ai_socktype);
3364 if (hints->ai_protocol == 0) /* wildcard, specific to getaddrinfo() */
3365 punixhints->ai_protocol = 0;
3367 punixhints->ai_protocol = convert_proto_w2u(hints->ai_protocol);
3370 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
3371 result = getaddrinfo(nodename, servname, punixhints, &unixaires);
3373 TRACE("%s, %s %p -> %p %d\n", nodename, servname, hints, res, result);
3375 HeapFree(GetProcessHeap(), 0, node);
3376 HeapFree(GetProcessHeap(), 0, serv);
3379 struct addrinfo *xuai = unixaires;
3380 struct WS_addrinfo **xai = res;
3384 struct WS_addrinfo *ai = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, sizeof(struct WS_addrinfo));
3390 *xai = ai;xai = &ai->ai_next;
3391 ai->ai_flags = convert_aiflag_u2w(xuai->ai_flags);
3392 ai->ai_family = convert_af_u2w(xuai->ai_family);
3393 ai->ai_socktype = convert_socktype_u2w(xuai->ai_socktype);
3394 ai->ai_protocol = convert_proto_u2w(xuai->ai_protocol);
3395 if (xuai->ai_canonname) {
3396 TRACE("canon name - %s\n",debugstr_a(xuai->ai_canonname));
3397 ai->ai_canonname = HeapAlloc(GetProcessHeap(),0,strlen(xuai->ai_canonname)+1);
3398 if (!ai->ai_canonname)
3400 strcpy(ai->ai_canonname,xuai->ai_canonname);
3402 len = xuai->ai_addrlen;
3403 ai->ai_addr = HeapAlloc(GetProcessHeap(),0,len);
3406 ai->ai_addrlen = len;
3408 int winlen = ai->ai_addrlen;
3410 if (!ws_sockaddr_u2ws(xuai->ai_addr, ai->ai_addr, &winlen)) {
3411 ai->ai_addrlen = winlen;
3415 ai->ai_addr = HeapReAlloc(GetProcessHeap(),0,ai->ai_addr,len);
3418 ai->ai_addrlen = len;
3420 xuai = xuai->ai_next;
3422 freeaddrinfo(unixaires);
3424 result = convert_eai_u2w(result);
3429 if (*res) WS_freeaddrinfo(*res);
3430 if (unixaires) freeaddrinfo(unixaires);
3432 return WSA_NOT_ENOUGH_MEMORY;
3434 FIXME("getaddrinfo() failed, not found during buildtime.\n");
3439 /***********************************************************************
3440 * GetAddrInfoW (WS2_32.@)
3442 int WINAPI GetAddrInfoW(LPCWSTR nodename, LPCWSTR servname, const ADDRINFOW *hints, PADDRINFOW *res)
3444 FIXME("empty stub!\n");
3448 int WINAPI WS_getnameinfo(const SOCKADDR *sa, WS_socklen_t salen, PCHAR host,
3449 DWORD hostlen, PCHAR serv, DWORD servlen, INT flags)
3451 #if HAVE_GETNAMEINFO
3453 union generic_unix_sockaddr sa_u;
3456 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa), salen, host, hostlen,
3457 serv, servlen, flags);
3459 size = ws_sockaddr_ws2u(sa, salen, &sa_u);
3462 WSASetLastError(WSAEFAULT);
3463 return WSA_NOT_ENOUGH_MEMORY;
3465 ret = getnameinfo(&sa_u.addr, size, host, hostlen, serv, servlen, convert_aiflag_w2u(flags));
3466 return convert_eai_u2w(ret);
3468 FIXME("getnameinfo() failed, not found during buildtime.\n");
3473 /***********************************************************************
3474 * getservbyport (WS2_32.56)
3476 struct WS_servent* WINAPI WS_getservbyport(int port, const char *proto)
3478 struct WS_servent* retval = NULL;
3479 #ifdef HAVE_GETSERVBYPORT
3480 struct servent* serv;
3481 char *proto_str = NULL;
3483 if (proto && *proto)
3485 if (!(proto_str = strdup_lower(proto))) return NULL;
3487 EnterCriticalSection( &csWSgetXXXbyYYY );
3488 if( (serv = getservbyport(port, proto_str)) != NULL ) {
3489 retval = WS_dup_se(serv);
3491 else SetLastError(WSANO_DATA);
3492 LeaveCriticalSection( &csWSgetXXXbyYYY );
3493 HeapFree( GetProcessHeap(), 0, proto_str );
3495 TRACE("%d (i.e. port %d), %s ret %p\n", port, (int)ntohl(port), debugstr_a(proto), retval);
3500 /***********************************************************************
3501 * gethostname (WS2_32.57)
3503 int WINAPI WS_gethostname(char *name, int namelen)
3505 TRACE("name %p, len %d\n", name, namelen);
3507 if (gethostname(name, namelen) == 0)
3509 TRACE("<- '%s'\n", name);
3512 SetLastError((errno == EINVAL) ? WSAEFAULT : wsaErrno());
3513 TRACE("<- ERROR !\n");
3514 return SOCKET_ERROR;
3518 /* ------------------------------------- Windows sockets extensions -- *
3520 * ------------------------------------------------------------------- */
3522 /***********************************************************************
3523 * WSAEnumNetworkEvents (WS2_32.36)
3525 int WINAPI WSAEnumNetworkEvents(SOCKET s, WSAEVENT hEvent, LPWSANETWORKEVENTS lpEvent)
3529 TRACE("%08lx, hEvent %p, lpEvent %p\n", s, hEvent, lpEvent );
3531 SERVER_START_REQ( get_socket_event )
3533 req->handle = SOCKET2HANDLE(s);
3534 req->service = TRUE;
3535 req->c_event = hEvent;
3536 wine_server_set_reply( req, lpEvent->iErrorCode, sizeof(lpEvent->iErrorCode) );
3537 if (!(ret = wine_server_call(req))) lpEvent->lNetworkEvents = reply->pmask & reply->mask;
3541 SetLastError(WSAEINVAL);
3542 return SOCKET_ERROR;
3545 /***********************************************************************
3546 * WSAEventSelect (WS2_32.39)
3548 int WINAPI WSAEventSelect(SOCKET s, WSAEVENT hEvent, LONG lEvent)
3552 TRACE("%08lx, hEvent %p, event %08x\n", s, hEvent, lEvent);
3554 SERVER_START_REQ( set_socket_event )
3556 req->handle = SOCKET2HANDLE(s);
3558 req->event = hEvent;
3561 ret = wine_server_call( req );
3565 SetLastError(WSAEINVAL);
3566 return SOCKET_ERROR;
3569 /**********************************************************************
3570 * WSAGetOverlappedResult (WS2_32.40)
3572 BOOL WINAPI WSAGetOverlappedResult( SOCKET s, LPWSAOVERLAPPED lpOverlapped,
3573 LPDWORD lpcbTransfer, BOOL fWait,
3578 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
3579 s, lpOverlapped, lpcbTransfer, fWait, lpdwFlags );
3581 if ( lpOverlapped == NULL )
3583 ERR( "Invalid pointer\n" );
3584 WSASetLastError(WSA_INVALID_PARAMETER);
3588 status = lpOverlapped->Internal;
3589 if (status == STATUS_PENDING)
3593 SetLastError( WSA_IO_INCOMPLETE );
3597 if (WaitForSingleObject( lpOverlapped->hEvent ? lpOverlapped->hEvent : SOCKET2HANDLE(s),
3598 INFINITE ) == WAIT_FAILED)
3600 status = lpOverlapped->Internal;
3604 *lpcbTransfer = lpOverlapped->InternalHigh;
3607 *lpdwFlags = lpOverlapped->u.s.Offset;
3609 if (status) SetLastError( RtlNtStatusToDosError(status) );
3614 /***********************************************************************
3615 * WSAAsyncSelect (WS2_32.101)
3617 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, LONG lEvent)
3621 TRACE("%lx, hWnd %p, uMsg %08x, event %08x\n", s, hWnd, uMsg, lEvent);
3623 SERVER_START_REQ( set_socket_event )
3625 req->handle = SOCKET2HANDLE(s);
3630 ret = wine_server_call( req );
3634 SetLastError(WSAEINVAL);
3635 return SOCKET_ERROR;
3638 /***********************************************************************
3639 * WSACreateEvent (WS2_32.31)
3642 WSAEVENT WINAPI WSACreateEvent(void)
3644 /* Create a manual-reset event, with initial state: unsignaled */
3647 return CreateEventW(NULL, TRUE, FALSE, NULL);
3650 /***********************************************************************
3651 * WSACloseEvent (WS2_32.29)
3654 BOOL WINAPI WSACloseEvent(WSAEVENT event)
3656 TRACE ("event=%p\n", event);
3658 return CloseHandle(event);
3661 /***********************************************************************
3662 * WSASocketA (WS2_32.78)
3665 SOCKET WINAPI WSASocketA(int af, int type, int protocol,
3666 LPWSAPROTOCOL_INFOA lpProtocolInfo,
3667 GROUP g, DWORD dwFlags)
3670 WSAPROTOCOL_INFOW info;
3672 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3673 af, type, protocol, lpProtocolInfo, g, dwFlags);
3675 if (!lpProtocolInfo) return WSASocketW(af, type, protocol, NULL, g, dwFlags);
3677 memcpy(&info, lpProtocolInfo, FIELD_OFFSET(WSAPROTOCOL_INFOW, szProtocol));
3678 len = MultiByteToWideChar(CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
3679 info.szProtocol, WSAPROTOCOL_LEN + 1);
3683 WSASetLastError( WSAEINVAL);
3684 return SOCKET_ERROR;
3687 return WSASocketW(af, type, protocol, &info, g, dwFlags);
3690 /***********************************************************************
3691 * WSASocketW (WS2_32.79)
3694 SOCKET WINAPI WSASocketW(int af, int type, int protocol,
3695 LPWSAPROTOCOL_INFOW lpProtocolInfo,
3696 GROUP g, DWORD dwFlags)
3701 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
3702 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
3705 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3706 af, type, protocol, lpProtocolInfo, g, dwFlags );
3708 /* hack for WSADuplicateSocket */
3709 if (lpProtocolInfo && lpProtocolInfo->dwServiceFlags4 == 0xff00ff00) {
3710 ret = lpProtocolInfo->dwCatalogEntryId;
3711 TRACE("\tgot duplicate %04lx\n", ret);
3715 /* check and convert the socket family */
3716 af = convert_af_w2u(af);
3719 FIXME("Unsupported socket family %d!\n", af);
3720 SetLastError(WSAEAFNOSUPPORT);
3721 return INVALID_SOCKET;
3724 /* check the socket type */
3725 type = convert_socktype_w2u(type);
3728 SetLastError(WSAESOCKTNOSUPPORT);
3729 return INVALID_SOCKET;
3732 /* check the protocol type */
3733 if ( protocol < 0 ) /* don't support negative values */
3735 SetLastError(WSAEPROTONOSUPPORT);
3736 return INVALID_SOCKET;
3739 if ( af == AF_UNSPEC) /* did they not specify the address family? */
3743 if (type == SOCK_STREAM) { af = AF_INET; break; }
3745 if (type == SOCK_DGRAM) { af = AF_INET; break; }
3746 default: SetLastError(WSAEPROTOTYPE); return INVALID_SOCKET;
3749 SERVER_START_REQ( create_socket )
3753 req->protocol = protocol;
3754 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
3755 req->attributes = OBJ_INHERIT;
3756 req->flags = dwFlags;
3757 set_error( wine_server_call( req ) );
3758 ret = HANDLE2SOCKET( reply->handle );
3763 TRACE("\tcreated %04lx\n", ret );
3767 if (GetLastError() == WSAEACCES) /* raw socket denied */
3769 if (type == SOCK_RAW)
3770 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, this"
3771 " will fail unless you have special permissions.\n");
3773 MESSAGE("WS_SOCKET: Failed to create socket, this requires"
3774 " special permissions.\n");
3775 SetLastError(WSAESOCKTNOSUPPORT);
3778 WARN("\t\tfailed!\n");
3779 return INVALID_SOCKET;
3782 /***********************************************************************
3783 * WSAJoinLeaf (WS2_32.58)
3786 SOCKET WINAPI WSAJoinLeaf(
3788 const struct WS_sockaddr *addr,
3790 LPWSABUF lpCallerData,
3791 LPWSABUF lpCalleeData,
3797 return INVALID_SOCKET;
3800 /***********************************************************************
3801 * __WSAFDIsSet (WS2_32.151)
3803 int WINAPI __WSAFDIsSet(SOCKET s, WS_fd_set *set)
3805 int i = set->fd_count;
3807 TRACE("(%ld,%p(%i))\n", s, set, i);
3810 if (set->fd_array[i] == s) return 1;
3814 /***********************************************************************
3815 * WSAIsBlocking (WINSOCK.114)
3816 * WSAIsBlocking (WS2_32.114)
3818 BOOL WINAPI WSAIsBlocking(void)
3820 /* By default WinSock should set all its sockets to non-blocking mode
3821 * and poll in PeekMessage loop when processing "blocking" ones. This
3822 * function is supposed to tell if the program is in this loop. Our
3823 * blocking calls are truly blocking so we always return FALSE.
3825 * Note: It is allowed to call this function without prior WSAStartup().
3832 /***********************************************************************
3833 * WSACancelBlockingCall (WINSOCK.113)
3834 * WSACancelBlockingCall (WS2_32.113)
3836 INT WINAPI WSACancelBlockingCall(void)
3842 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x )
3844 FIXME("How was this called?\n");
3849 /***********************************************************************
3850 * WSASetBlockingHook (WS2_32.109)
3852 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
3854 FARPROC prev = blocking_hook;
3855 blocking_hook = lpBlockFunc;
3856 TRACE("hook %p\n", lpBlockFunc);
3861 /***********************************************************************
3862 * WSAUnhookBlockingHook (WS2_32.110)
3864 INT WINAPI WSAUnhookBlockingHook(void)
3866 blocking_hook = (FARPROC)WSA_DefaultBlockingHook;
3871 /* ----------------------------------- end of API stuff */
3873 /* ----------------------------------- helper functions -
3875 * TODO: Merge WS_dup_..() stuff into one function that
3876 * would operate with a generic structure containing internal
3877 * pointers (via a template of some kind).
3880 static int list_size(char** l, int item_size)
3885 j += (item_size) ? item_size : strlen(l[i]) + 1;
3886 j += (i + 1) * sizeof(char*); }
3890 static int list_dup(char** l_src, char** l_to, int item_size)
3895 for (i = 0; l_src[i]; i++) ;
3896 p = (char *)(l_to + i + 1);
3897 for (i = 0; l_src[i]; i++)
3899 int count = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
3900 memcpy(p, l_src[i], count);
3905 return p - (char *)l_to;
3910 /* duplicate hostent entry
3911 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
3912 * Dito for protoent and servent.
3914 static struct WS_hostent *WS_dup_he(const struct hostent* p_he)
3917 struct WS_hostent *p_to;
3919 int size = (sizeof(*p_he) +
3920 strlen(p_he->h_name) + 1 +
3921 list_size(p_he->h_aliases, 0) +
3922 list_size(p_he->h_addr_list, p_he->h_length));
3924 if (!(p_to = check_buffer_he(size))) return NULL;
3925 p_to->h_addrtype = p_he->h_addrtype;
3926 p_to->h_length = p_he->h_length;
3928 p = (char *)(p_to + 1);
3930 strcpy(p, p_he->h_name);
3933 p_to->h_aliases = (char **)p;
3934 p += list_dup(p_he->h_aliases, p_to->h_aliases, 0);
3936 p_to->h_addr_list = (char **)p;
3937 list_dup(p_he->h_addr_list, p_to->h_addr_list, p_he->h_length);
3941 /* ----- protoent */
3943 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe)
3946 struct WS_protoent *p_to;
3948 int size = (sizeof(*p_pe) +
3949 strlen(p_pe->p_name) + 1 +
3950 list_size(p_pe->p_aliases, 0));
3952 if (!(p_to = check_buffer_pe(size))) return NULL;
3953 p_to->p_proto = p_pe->p_proto;
3955 p = (char *)(p_to + 1);
3957 strcpy(p, p_pe->p_name);
3960 p_to->p_aliases = (char **)p;
3961 list_dup(p_pe->p_aliases, p_to->p_aliases, 0);
3967 static struct WS_servent *WS_dup_se(const struct servent* p_se)
3970 struct WS_servent *p_to;
3972 int size = (sizeof(*p_se) +
3973 strlen(p_se->s_proto) + 1 +
3974 strlen(p_se->s_name) + 1 +
3975 list_size(p_se->s_aliases, 0));
3977 if (!(p_to = check_buffer_se(size))) return NULL;
3978 p_to->s_port = p_se->s_port;
3980 p = (char *)(p_to + 1);
3982 strcpy(p, p_se->s_name);
3986 strcpy(p, p_se->s_proto);
3989 p_to->s_aliases = (char **)p;
3990 list_dup(p_se->s_aliases, p_to->s_aliases, 0);
3994 /* ----------------------------------- error handling */
3998 int loc_errno = errno;
3999 WARN("errno %d, (%s).\n", loc_errno, strerror(loc_errno));
4003 case EINTR: return WSAEINTR;
4004 case EBADF: return WSAEBADF;
4006 case EACCES: return WSAEACCES;
4007 case EFAULT: return WSAEFAULT;
4008 case EINVAL: return WSAEINVAL;
4009 case EMFILE: return WSAEMFILE;
4010 case EWOULDBLOCK: return WSAEWOULDBLOCK;
4011 case EINPROGRESS: return WSAEINPROGRESS;
4012 case EALREADY: return WSAEALREADY;
4013 case ENOTSOCK: return WSAENOTSOCK;
4014 case EDESTADDRREQ: return WSAEDESTADDRREQ;
4015 case EMSGSIZE: return WSAEMSGSIZE;
4016 case EPROTOTYPE: return WSAEPROTOTYPE;
4017 case ENOPROTOOPT: return WSAENOPROTOOPT;
4018 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
4019 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
4020 case EOPNOTSUPP: return WSAEOPNOTSUPP;
4021 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
4022 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
4023 case EADDRINUSE: return WSAEADDRINUSE;
4024 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
4025 case ENETDOWN: return WSAENETDOWN;
4026 case ENETUNREACH: return WSAENETUNREACH;
4027 case ENETRESET: return WSAENETRESET;
4028 case ECONNABORTED: return WSAECONNABORTED;
4030 case ECONNRESET: return WSAECONNRESET;
4031 case ENOBUFS: return WSAENOBUFS;
4032 case EISCONN: return WSAEISCONN;
4033 case ENOTCONN: return WSAENOTCONN;
4034 case ESHUTDOWN: return WSAESHUTDOWN;
4035 case ETOOMANYREFS: return WSAETOOMANYREFS;
4036 case ETIMEDOUT: return WSAETIMEDOUT;
4037 case ECONNREFUSED: return WSAECONNREFUSED;
4038 case ELOOP: return WSAELOOP;
4039 case ENAMETOOLONG: return WSAENAMETOOLONG;
4040 case EHOSTDOWN: return WSAEHOSTDOWN;
4041 case EHOSTUNREACH: return WSAEHOSTUNREACH;
4042 case ENOTEMPTY: return WSAENOTEMPTY;
4044 case EPROCLIM: return WSAEPROCLIM;
4047 case EUSERS: return WSAEUSERS;
4050 case EDQUOT: return WSAEDQUOT;
4053 case ESTALE: return WSAESTALE;
4056 case EREMOTE: return WSAEREMOTE;
4059 /* just in case we ever get here and there are no problems */
4062 WARN("Unknown errno %d!\n", loc_errno);
4063 return WSAEOPNOTSUPP;
4067 UINT wsaHerrno(int loc_errno)
4070 WARN("h_errno %d.\n", loc_errno);
4074 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
4075 case TRY_AGAIN: return WSATRY_AGAIN;
4076 case NO_RECOVERY: return WSANO_RECOVERY;
4077 case NO_DATA: return WSANO_DATA;
4078 case ENOBUFS: return WSAENOBUFS;
4082 WARN("Unknown h_errno %d!\n", loc_errno);
4083 return WSAEOPNOTSUPP;
4088 /***********************************************************************
4089 * WSARecv (WS2_32.67)
4091 int WINAPI WSARecv(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
4092 LPDWORD NumberOfBytesReceived, LPDWORD lpFlags,
4093 LPWSAOVERLAPPED lpOverlapped,
4094 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
4096 return WSARecvFrom(s, lpBuffers, dwBufferCount, NumberOfBytesReceived, lpFlags,
4097 NULL, NULL, lpOverlapped, lpCompletionRoutine);
4100 /***********************************************************************
4101 * WSARecvFrom (WS2_32.69)
4103 INT WINAPI WSARecvFrom( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
4104 LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, struct WS_sockaddr *lpFrom,
4105 LPINT lpFromlen, LPWSAOVERLAPPED lpOverlapped,
4106 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
4109 unsigned int i, options;
4111 DWORD timeout_start = GetTickCount();
4112 struct iovec iovec[WS_MSG_MAXIOVLEN];
4114 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
4115 s, lpBuffers, dwBufferCount, *lpFlags, lpFrom,
4116 (lpFromlen ? *lpFromlen : -1),
4117 lpOverlapped, lpCompletionRoutine);
4119 if (dwBufferCount > WS_MSG_MAXIOVLEN)
4121 WSASetLastError( WSAEINVAL );
4122 return SOCKET_ERROR;
4125 fd = get_sock_fd( s, FILE_READ_DATA, &options );
4126 TRACE( "fd=%d, options=%x\n", fd, options );
4128 if (fd == -1) return SOCKET_ERROR;
4130 for (i = 0; i < dwBufferCount; i++)
4132 iovec[i].iov_base = lpBuffers[i].buf;
4133 iovec[i].iov_len = lpBuffers[i].len;
4138 n = WS2_recv( fd, iovec, dwBufferCount, lpFrom, lpFromlen, lpFlags );
4141 if (errno == EINTR) continue;
4142 if (errno != EAGAIN)
4149 *lpNumberOfBytesRecvd = n;
4151 if ((lpOverlapped || lpCompletionRoutine) &&
4152 !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
4154 IO_STATUS_BLOCK *iosb;
4155 struct ws2_async *wsa = HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa) );
4162 release_sock_fd( s, fd );
4164 wsa->hSocket = SOCKET2HANDLE(s);
4165 wsa->flags = *lpFlags;
4167 wsa->addrlen.ptr = lpFromlen;
4168 wsa->user_overlapped = lpOverlapped;
4169 wsa->completion_func = lpCompletionRoutine;
4170 wsa->n_iovecs = dwBufferCount;
4171 memcpy( wsa->iovec, iovec, dwBufferCount * sizeof(*iovec) );
4173 iosb = lpOverlapped ? (IO_STATUS_BLOCK *)lpOverlapped : &wsa->local_iosb;
4177 iosb->u.Status = STATUS_PENDING;
4178 iosb->Information = 0;
4180 SERVER_START_REQ( register_async )
4182 req->handle = wsa->hSocket;
4183 req->type = ASYNC_TYPE_READ;
4184 req->async.callback = WS2_async_recv;
4185 req->async.iosb = iosb;
4186 req->async.arg = wsa;
4187 req->async.apc = ws2_async_apc;
4188 req->async.event = lpCompletionRoutine ? 0 : lpOverlapped->hEvent;
4189 err = wine_server_call( req );
4193 if (err != STATUS_PENDING) HeapFree( GetProcessHeap(), 0, wsa );
4194 WSASetLastError( NtStatusToWSAError( err ));
4195 return SOCKET_ERROR;
4198 iosb->u.Status = STATUS_SUCCESS;
4199 iosb->Information = n;
4200 if (!wsa->completion_func)
4202 SetEvent( lpOverlapped->hEvent );
4203 HeapFree( GetProcessHeap(), 0, wsa );
4205 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)ws2_async_apc,
4206 (ULONG_PTR)wsa, (ULONG_PTR)iosb, 0 );
4207 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4213 if ( _is_blocking(s) )
4216 int timeout = GET_RCVTIMEO(fd);
4219 timeout -= GetTickCount() - timeout_start;
4220 if (timeout < 0) timeout = 0;
4224 pfd.events = POLLIN;
4225 if (*lpFlags & WS_MSG_OOB) pfd.events |= POLLPRI;
4227 if (!timeout || !poll( &pfd, 1, timeout ))
4230 /* a timeout is not fatal */
4231 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4237 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4238 err = WSAEWOULDBLOCK;
4243 TRACE(" -> %i bytes\n", n);
4244 release_sock_fd( s, fd );
4245 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4250 release_sock_fd( s, fd );
4251 WARN(" -> ERROR %d\n", err);
4252 WSASetLastError( err );
4253 return SOCKET_ERROR;
4256 /***********************************************************************
4257 * WSCInstallProvider (WS2_32.88)
4259 INT WINAPI WSCInstallProvider( const LPGUID lpProviderId,
4260 LPCWSTR lpszProviderDllPath,
4261 const LPWSAPROTOCOL_INFOW lpProtocolInfoList,
4262 DWORD dwNumberOfEntries,
4265 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId),
4266 debugstr_w(lpszProviderDllPath), lpProtocolInfoList,
4267 dwNumberOfEntries, lpErrno);
4273 /***********************************************************************
4274 * WSCDeinstallProvider (WS2_32.83)
4276 INT WINAPI WSCDeinstallProvider(LPGUID lpProviderId, LPINT lpErrno)
4278 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId), lpErrno);
4284 /***********************************************************************
4285 * WSAAccept (WS2_32.26)
4287 SOCKET WINAPI WSAAccept( SOCKET s, struct WS_sockaddr *addr, LPINT addrlen,
4288 LPCONDITIONPROC lpfnCondition, DWORD dwCallbackData)
4291 int ret = 0, size = 0;
4292 WSABUF CallerId, CallerData, CalleeId, CalleeData;
4293 /* QOS SQOS, GQOS; */
4296 SOCKADDR src_addr, dst_addr;
4298 TRACE("Socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %d\n",
4299 s, addr, addrlen, lpfnCondition, dwCallbackData);
4302 size = sizeof(src_addr);
4303 cs = WS_accept(s, &src_addr, &size);
4305 if (cs == SOCKET_ERROR) return SOCKET_ERROR;
4307 CallerId.buf = (char *)&src_addr;
4308 CallerId.len = sizeof(src_addr);
4310 CallerData.buf = NULL;
4311 CallerData.len = (ULONG)NULL;
4313 WS_getsockname(cs, &dst_addr, &size);
4315 CalleeId.buf = (char *)&dst_addr;
4316 CalleeId.len = sizeof(dst_addr);
4319 ret = (*lpfnCondition)(&CallerId, &CallerData, NULL, NULL,
4320 &CalleeId, &CalleeData, &g, dwCallbackData);
4325 if (addr && addrlen)
4326 addr = memcpy(addr, &src_addr, (*addrlen > size) ? size : *addrlen );
4329 SERVER_START_REQ( set_socket_deferred )
4331 req->handle = SOCKET2HANDLE(s);
4332 req->deferred = SOCKET2HANDLE(cs);
4333 if ( !wine_server_call_err ( req ) )
4335 SetLastError( WSATRY_AGAIN );
4336 WS_closesocket( cs );
4340 return SOCKET_ERROR;
4343 SetLastError(WSAECONNREFUSED);
4344 return SOCKET_ERROR;
4346 FIXME("Unknown return type from Condition function\n");
4347 SetLastError(WSAENOTSOCK);
4348 return SOCKET_ERROR;
4352 /***********************************************************************
4353 * WSADuplicateSocketA (WS2_32.32)
4355 int WINAPI WSADuplicateSocketA( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOA lpProtocolInfo )
4359 TRACE("(%ld,%x,%p)\n", s, dwProcessId, lpProtocolInfo);
4360 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
4361 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
4362 /* I don't know what the real Windoze does next, this is a hack */
4363 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
4364 * the target use the global duplicate, or we could copy a reference to us to the structure
4365 * and let the target duplicate it from us, but let's do it as simple as possible */
4366 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
4367 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
4368 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
4369 0, FALSE, DUPLICATE_SAME_ACCESS);
4370 CloseHandle(hProcess);
4371 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
4375 /***********************************************************************
4376 * WSADuplicateSocketW (WS2_32.33)
4378 int WINAPI WSADuplicateSocketW( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOW lpProtocolInfo )
4382 TRACE("(%ld,%x,%p)\n", s, dwProcessId, lpProtocolInfo);
4384 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
4385 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
4386 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
4387 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
4388 0, FALSE, DUPLICATE_SAME_ACCESS);
4389 CloseHandle(hProcess);
4390 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
4394 /***********************************************************************
4395 * WSAInstallServiceClassA (WS2_32.48)
4397 int WINAPI WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info)
4399 FIXME("Request to install service %s\n",debugstr_a(info->lpszServiceClassName));
4400 WSASetLastError(WSAEACCES);
4401 return SOCKET_ERROR;
4404 /***********************************************************************
4405 * WSAInstallServiceClassW (WS2_32.49)
4407 int WINAPI WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info)
4409 FIXME("Request to install service %s\n",debugstr_w(info->lpszServiceClassName));
4410 WSASetLastError(WSAEACCES);
4411 return SOCKET_ERROR;
4414 /***********************************************************************
4415 * WSARemoveServiceClass (WS2_32.70)
4417 int WINAPI WSARemoveServiceClass(LPGUID info)
4419 FIXME("Request to remove service %p\n",info);
4420 WSASetLastError(WSATYPE_NOT_FOUND);
4421 return SOCKET_ERROR;
4424 /***********************************************************************
4425 * WSAStringToAddressA (WS2_32.80)
4427 INT WINAPI WSAStringToAddressA(LPSTR AddressString,
4429 LPWSAPROTOCOL_INFOA lpProtocolInfo,
4430 LPSOCKADDR lpAddress,
4431 LPINT lpAddressLength)
4434 LPSTR workBuffer=NULL,ptrPort;
4436 TRACE( "(%s, %x, %p, %p, %p)\n", AddressString, AddressFamily, lpProtocolInfo,
4437 lpAddress, lpAddressLength );
4439 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
4443 WSASetLastError(WSAEINVAL);
4444 return SOCKET_ERROR;
4448 FIXME("ProtocolInfo not implemented.\n");
4450 workBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
4451 strlen(AddressString) + 1);
4454 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4455 return SOCKET_ERROR;
4458 strcpy(workBuffer, AddressString);
4460 switch(AddressFamily)
4464 struct in_addr inetaddr;
4466 /* If lpAddressLength is too small, tell caller the size we need */
4467 if (*lpAddressLength < sizeof(SOCKADDR_IN))
4469 *lpAddressLength = sizeof(SOCKADDR_IN);
4473 memset(lpAddress, 0, sizeof(SOCKADDR_IN));
4475 ((LPSOCKADDR_IN)lpAddress)->sin_family = AF_INET;
4477 ptrPort = strchr(workBuffer, ':');
4480 ((LPSOCKADDR_IN)lpAddress)->sin_port = (WS_u_short)atoi(ptrPort+1);
4485 ((LPSOCKADDR_IN)lpAddress)->sin_port = 0;
4488 if(inet_aton(workBuffer, &inetaddr) > 0)
4490 ((LPSOCKADDR_IN)lpAddress)->sin_addr.WS_s_addr = inetaddr.s_addr;
4501 struct in6_addr inetaddr;
4502 /* If lpAddressLength is too small, tell caller the size we need */
4503 if (*lpAddressLength < sizeof(SOCKADDR_IN6))
4505 *lpAddressLength = sizeof(SOCKADDR_IN6);
4509 #ifdef HAVE_INET_PTON
4510 memset(lpAddress, 0, sizeof(SOCKADDR_IN6));
4512 ((LPSOCKADDR_IN6)lpAddress)->sin6_family = WS_AF_INET6;
4514 /* This one is a bit tricky. An IPv6 address contains colons, so the
4515 * check from IPv4 doesn't work like that. However, IPv6 addresses that
4516 * contain a port are written with braces like [fd12:3456:7890::1]:12345
4517 * so what we will do is to look for ']', check if the next char is a
4518 * colon, and if it is, parse the port as in IPv4. */
4520 ptrPort = strchr(workBuffer, ']');
4521 if(ptrPort && *(++ptrPort) == ':')
4523 ((LPSOCKADDR_IN6)lpAddress)->sin6_port = (WS_u_short)atoi(ptrPort+1);
4528 ((LPSOCKADDR_IN6)lpAddress)->sin6_port = 0;
4531 if(inet_pton(AF_INET6, workBuffer, &inetaddr) > 0)
4533 memcpy(&((LPSOCKADDR_IN6)lpAddress)->sin6_addr, &inetaddr,
4534 sizeof(struct in6_addr));
4538 #endif /* HAVE_INET_PTON */
4544 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
4545 TRACE("Unsupported address family specified: %d.\n", AddressFamily);
4549 HeapFree(GetProcessHeap(), 0, workBuffer);
4552 WSASetLastError(res);
4553 return SOCKET_ERROR;
4556 /***********************************************************************
4557 * WSAStringToAddressW (WS2_32.81)
4559 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
4560 * If this should be the case, it would be required to map these digits
4561 * to Unicode digits (0-9) using FoldString first.
4563 INT WINAPI WSAStringToAddressW(LPWSTR AddressString,
4565 LPWSAPROTOCOL_INFOW lpProtocolInfo,
4566 LPSOCKADDR lpAddress,
4567 LPINT lpAddressLength)
4570 LPSTR workBuffer=NULL;
4571 WSAPROTOCOL_INFOA infoA;
4572 LPWSAPROTOCOL_INFOA lpProtoInfoA = NULL;
4574 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString), AddressFamily, lpProtocolInfo,
4575 lpAddress, lpAddressLength );
4577 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
4579 /* if ProtocolInfo is available - convert to ANSI variant */
4582 lpProtoInfoA = &infoA;
4583 memcpy( lpProtoInfoA, lpProtocolInfo, FIELD_OFFSET( WSAPROTOCOL_INFOA, szProtocol ) );
4585 if (!WideCharToMultiByte( CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
4586 lpProtoInfoA->szProtocol, WSAPROTOCOL_LEN+1, NULL, NULL ))
4588 WSASetLastError( WSAEINVAL);
4589 return SOCKET_ERROR;
4595 /* Translate AddressString to ANSI code page - assumes that only
4596 standard digits 0-9 are used with this API call */
4597 sBuffer = WideCharToMultiByte( CP_ACP, 0, AddressString, -1, NULL, 0, NULL, NULL );
4598 workBuffer = HeapAlloc( GetProcessHeap(), 0, sBuffer );
4602 WideCharToMultiByte( CP_ACP, 0, AddressString, -1, workBuffer, sBuffer, NULL, NULL );
4603 res = WSAStringToAddressA(workBuffer,AddressFamily,lpProtoInfoA,
4604 lpAddress,lpAddressLength);
4605 HeapFree( GetProcessHeap(), 0, workBuffer );
4609 res = WSA_NOT_ENOUGH_MEMORY;
4614 WSASetLastError(res);
4615 return SOCKET_ERROR;
4618 /***********************************************************************
4619 * WSAAddressToStringA (WS2_32.27)
4621 * See WSAAddressToStringW
4623 INT WINAPI WSAAddressToStringA( LPSOCKADDR sockaddr, DWORD len,
4624 LPWSAPROTOCOL_INFOA info, LPSTR string,
4628 CHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4631 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
4633 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
4634 if (!string || !lenstr) return SOCKET_ERROR;
4636 /* sin_family is guaranteed to be the first u_short */
4637 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
4639 sprintf( buffer, "%u.%u.%u.%u:%u",
4640 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
4641 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
4642 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
4643 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
4644 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
4646 p = strchr( buffer, ':' );
4647 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
4649 size = strlen( buffer );
4654 WSASetLastError(WSAEFAULT);
4655 return SOCKET_ERROR;
4658 strcpy( string, buffer );
4662 /***********************************************************************
4663 * WSAAddressToStringW (WS2_32.28)
4665 * Convert a sockaddr address into a readable address string.
4668 * sockaddr [I] Pointer to a sockaddr structure.
4669 * len [I] Size of the sockaddr structure.
4670 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
4671 * string [I/O] Pointer to a buffer to receive the address string.
4672 * lenstr [I/O] Size of the receive buffer in WCHARs.
4676 * Failure: SOCKET_ERROR
4679 * The 'info' parameter is ignored.
4682 * Only supports AF_INET addresses.
4684 INT WINAPI WSAAddressToStringW( LPSOCKADDR sockaddr, DWORD len,
4685 LPWSAPROTOCOL_INFOW info, LPWSTR string,
4689 WCHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4690 static const WCHAR format[] = { '%','u','.','%','u','.','%','u','.','%','u',':','%','u',0 };
4693 TRACE( "(%p, %x, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
4695 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
4696 if (!string || !lenstr) return SOCKET_ERROR;
4698 /* sin_family is guaranteed to be the first u_short */
4699 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
4701 sprintfW( buffer, format,
4702 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
4703 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
4704 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
4705 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
4706 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
4708 p = strchrW( buffer, ':' );
4709 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
4711 size = lstrlenW( buffer );
4716 return SOCKET_ERROR;
4719 lstrcpyW( string, buffer );
4723 /***********************************************************************
4724 * WSAEnumNameSpaceProvidersA (WS2_32.34)
4726 INT WINAPI WSAEnumNameSpaceProvidersA( LPDWORD len, LPWSANAMESPACE_INFOA buffer )
4728 FIXME( "(%p %p) Stub!\n", len, buffer );
4732 /***********************************************************************
4733 * WSAEnumNameSpaceProvidersW (WS2_32.35)
4735 INT WINAPI WSAEnumNameSpaceProvidersW( LPDWORD len, LPWSANAMESPACE_INFOW buffer )
4737 FIXME( "(%p %p) Stub!\n", len, buffer );
4741 /***********************************************************************
4742 * WSAGetQOSByName (WS2_32.41)
4744 BOOL WINAPI WSAGetQOSByName( SOCKET s, LPWSABUF lpQOSName, LPQOS lpQOS )
4746 FIXME( "(0x%04lx %p %p) Stub!\n", s, lpQOSName, lpQOS );
4750 /***********************************************************************
4751 * WSAGetServiceClassInfoA (WS2_32.42)
4753 INT WINAPI WSAGetServiceClassInfoA( LPGUID provider, LPGUID service, LPDWORD len,
4754 LPWSASERVICECLASSINFOA info )
4756 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider), debugstr_guid(service),
4758 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4759 return SOCKET_ERROR;
4762 /***********************************************************************
4763 * WSAGetServiceClassInfoW (WS2_32.43)
4765 INT WINAPI WSAGetServiceClassInfoW( LPGUID provider, LPGUID service, LPDWORD len,
4766 LPWSASERVICECLASSINFOW info )
4768 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider), debugstr_guid(service),
4770 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4771 return SOCKET_ERROR;
4774 /***********************************************************************
4775 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
4777 INT WINAPI WSAGetServiceClassNameByClassIdA( LPGUID class, LPSTR service, LPDWORD len )
4779 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4780 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4781 return SOCKET_ERROR;
4784 /***********************************************************************
4785 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
4787 INT WINAPI WSAGetServiceClassNameByClassIdW( LPGUID class, LPWSTR service, LPDWORD len )
4789 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4790 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4791 return SOCKET_ERROR;
4794 /***********************************************************************
4795 * WSALookupServiceBeginA (WS2_32.59)
4797 INT WINAPI WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions,
4798 DWORD dwControlFlags,
4801 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4803 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4804 return SOCKET_ERROR;
4807 /***********************************************************************
4808 * WSALookupServiceBeginW (WS2_32.60)
4810 INT WINAPI WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions,
4811 DWORD dwControlFlags,
4814 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4816 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4817 return SOCKET_ERROR;
4820 /***********************************************************************
4821 * WSALookupServiceBeginW (WS2_32.61)
4823 INT WINAPI WSALookupServiceEnd( HANDLE lookup )
4825 FIXME("(%p) Stub!\n", lookup );
4829 /***********************************************************************
4830 * WSALookupServiceNextA (WS2_32.62)
4832 INT WINAPI WSALookupServiceNextA( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETA results )
4834 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup, flags, len, results );
4838 /***********************************************************************
4839 * WSALookupServiceNextW (WS2_32.63)
4841 INT WINAPI WSALookupServiceNextW( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETW results )
4843 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup, flags, len, results );
4847 /***********************************************************************
4848 * WSANtohl (WS2_32.64)
4850 INT WINAPI WSANtohl( SOCKET s, WS_u_long netlong, WS_u_long* lphostlong )
4852 TRACE( "(0x%04lx 0x%08x %p)\n", s, netlong, lphostlong );
4854 if (!lphostlong) return WSAEFAULT;
4856 *lphostlong = ntohl( netlong );
4860 /***********************************************************************
4861 * WSANtohs (WS2_32.65)
4863 INT WINAPI WSANtohs( SOCKET s, WS_u_short netshort, WS_u_short* lphostshort )
4865 TRACE( "(0x%04lx 0x%08x %p)\n", s, netshort, lphostshort );
4867 if (!lphostshort) return WSAEFAULT;
4869 *lphostshort = ntohs( netshort );
4873 /***********************************************************************
4874 * WSAProviderConfigChange (WS2_32.66)
4876 INT WINAPI WSAProviderConfigChange( LPHANDLE handle, LPWSAOVERLAPPED overlapped,
4877 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion )
4879 FIXME( "(%p %p %p) Stub!\n", handle, overlapped, completion );
4880 return SOCKET_ERROR;
4883 /***********************************************************************
4884 * WSARecvDisconnect (WS2_32.68)
4886 INT WINAPI WSARecvDisconnect( SOCKET s, LPWSABUF disconnectdata )
4888 TRACE( "(0x%04lx %p)\n", s, disconnectdata );
4890 return WS_shutdown( s, 0 );
4893 /***********************************************************************
4894 * WSASetServiceA (WS2_32.76)
4896 INT WINAPI WSASetServiceA( LPWSAQUERYSETA query, WSAESETSERVICEOP operation, DWORD flags )
4898 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query, operation, flags );
4902 /***********************************************************************
4903 * WSASetServiceW (WS2_32.77)
4905 INT WINAPI WSASetServiceW( LPWSAQUERYSETW query, WSAESETSERVICEOP operation, DWORD flags )
4907 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query, operation, flags );
4911 /***********************************************************************
4912 * WSCEnableNSProvider (WS2_32.84)
4914 INT WINAPI WSCEnableNSProvider( LPGUID provider, BOOL enable )
4916 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider), enable );
4920 /***********************************************************************
4921 * WSCGetProviderPath (WS2_32.86)
4923 INT WINAPI WSCGetProviderPath( LPGUID provider, LPWSTR path, LPINT len, LPINT errcode )
4925 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider), path, len, errcode );
4927 if (!errcode || !provider || !len) return WSAEFAULT;
4929 *errcode = WSAEINVAL;
4930 return SOCKET_ERROR;
4933 /***********************************************************************
4934 * WSCInstallNameSpace (WS2_32.87)
4936 INT WINAPI WSCInstallNameSpace( LPWSTR identifier, LPWSTR path, DWORD namespace,
4937 DWORD version, LPGUID provider )
4939 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier), debugstr_w(path),
4940 namespace, version, debugstr_guid(provider) );
4944 /***********************************************************************
4945 * WSCUnInstallNameSpace (WS2_32.89)
4947 INT WINAPI WSCUnInstallNameSpace( LPGUID lpProviderId )
4949 FIXME("(%p) Stub!\n", lpProviderId);
4953 /***********************************************************************
4954 * WSCWriteProviderOrder (WS2_32.91)
4956 INT WINAPI WSCWriteProviderOrder( LPDWORD entry, DWORD number )
4958 FIXME("(%p 0x%08x) Stub!\n", entry, number);