2 * based on Windows Sockets 1.1 specs
3 * (ftp.microsoft.com:/Advsys/winsock/spec11/WINSOCK.TXT)
5 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
6 * Copyright (C) 2005 Marcus Meissner
7 * Copyright (C) 2006 Kai Blin
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * NOTE: If you make any changes to fix a particular app, make sure
24 * they don't break something else like Netscape or telnet and ftp
25 * clients and servers (www.winsite.com got a lot of those).
29 #include "wine/port.h"
34 #include <sys/types.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 # include <sys/ioctl.h>
41 #ifdef HAVE_SYS_FILIO_H
42 # include <sys/filio.h>
44 #ifdef HAVE_SYS_SOCKIO_H
45 # include <sys/sockio.h>
49 # include <sys/so_ioctl.h>
52 #ifdef HAVE_SYS_PARAM_H
53 # include <sys/param.h>
59 #ifdef HAVE_SYS_WAIT_H
60 # include <sys/wait.h>
65 #ifdef HAVE_SYS_SOCKET_H
66 #include <sys/socket.h>
68 #ifdef HAVE_NETINET_IN_H
69 # include <netinet/in.h>
71 #ifdef HAVE_NETINET_TCP_H
72 # include <netinet/tcp.h>
74 #ifdef HAVE_ARPA_INET_H
75 # include <arpa/inet.h>
80 #ifdef HAVE_SYS_ERRNO_H
81 #include <sys/errno.h>
90 #ifdef HAVE_ARPA_NAMESER_H
91 # include <arpa/nameser.h>
100 #ifdef HAVE_NETIPX_IPX_H
101 # include <netipx/ipx.h>
103 #elif defined(HAVE_LINUX_IPX_H)
104 # ifdef HAVE_ASM_TYPES_H
105 # include <asm/types.h>
107 # include <linux/ipx.h>
114 #ifdef HAVE_SYS_POLL_H
115 # include <sys/poll.h>
117 #ifdef HAVE_SYS_TIME_H
118 # include <sys/time.h>
121 #define NONAMELESSUNION
122 #define NONAMELESSSTRUCT
123 #include "ntstatus.h"
124 #define WIN32_NO_STATUS
129 #include "winerror.h"
131 #include "winsock2.h"
133 #include "ws2tcpip.h"
137 #include "iphlpapi.h"
139 #include "wine/server.h"
140 #include "wine/debug.h"
141 #include "wine/unicode.h"
144 # include "wsnwlink.h"
148 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
149 # define sipx_network sipx_addr.x_net
150 # define sipx_node sipx_addr.x_host.c_host
151 #endif /* __FreeBSD__ */
154 #define INADDR_NONE ~0UL
157 WINE_DEFAULT_DEBUG_CHANNEL(winsock);
159 /* critical section to protect some non-rentrant net function */
160 extern CRITICAL_SECTION csWSgetXXXbyYYY;
162 inline static const char *debugstr_sockaddr( const struct WS_sockaddr *a )
164 if (!a) return "(nil)";
165 return wine_dbg_sprintf("{ family %d, address %s, port %d }",
166 ((const struct sockaddr_in *)a)->sin_family,
167 inet_ntoa(((const struct sockaddr_in *)a)->sin_addr),
168 ntohs(((const struct sockaddr_in *)a)->sin_port));
171 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
172 #define SOCKET2HANDLE(s) ((HANDLE)(s))
173 #define HANDLE2SOCKET(h) ((SOCKET)(h))
175 /****************************************************************
176 * Async IO declarations
177 ****************************************************************/
179 typedef struct ws2_async
182 enum ws2_mode {ws2m_read, ws2m_write, ws2m_sd_read, ws2m_sd_write} mode;
183 LPWSAOVERLAPPED user_overlapped;
184 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func;
187 struct WS_sockaddr *addr;
190 int val; /* for send operations */
191 int *ptr; /* for recv operations */
197 /****************************************************************/
199 /* ----------------------------------- internal data */
201 /* ws_... struct conversion flags */
203 typedef struct /* WSAAsyncSelect() control struct */
205 HANDLE service, event, sock;
211 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
212 #define WS_MAX_UDP_DATAGRAM 1024
213 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x );
215 /* hostent's, servent's and protent's are stored in one buffer per thread,
216 * as documented on MSDN for the functions that return any of the buffers */
217 struct per_thread_data
220 struct WS_hostent *he_buffer;
221 struct WS_servent *se_buffer;
222 struct WS_protoent *pe_buffer;
228 static INT num_startup; /* reference counter */
229 static FARPROC blocking_hook = WSA_DefaultBlockingHook;
231 /* function prototypes */
232 static struct WS_hostent *WS_dup_he(const struct hostent* p_he);
233 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe);
234 static struct WS_servent *WS_dup_se(const struct servent* p_se);
236 int WSAIOCTL_GetInterfaceCount(void);
237 int WSAIOCTL_GetInterfaceName(int intNumber, char *intName);
240 UINT wsaHerrno(int errnr);
242 #define MAP_OPTION(opt) { WS_##opt, opt }
244 static const int ws_sock_map[][2] =
246 MAP_OPTION( SO_DEBUG ),
247 MAP_OPTION( SO_ACCEPTCONN ),
248 MAP_OPTION( SO_REUSEADDR ),
249 MAP_OPTION( SO_KEEPALIVE ),
250 MAP_OPTION( SO_DONTROUTE ),
251 MAP_OPTION( SO_BROADCAST ),
252 MAP_OPTION( SO_LINGER ),
253 MAP_OPTION( SO_OOBINLINE ),
254 MAP_OPTION( SO_SNDBUF ),
255 MAP_OPTION( SO_RCVBUF ),
256 MAP_OPTION( SO_ERROR ),
257 MAP_OPTION( SO_TYPE ),
259 MAP_OPTION( SO_RCVTIMEO ),
262 MAP_OPTION( SO_SNDTIMEO ),
266 static const int ws_tcp_map[][2] =
269 MAP_OPTION( TCP_NODELAY ),
273 static const int ws_ip_map[][2] =
275 MAP_OPTION( IP_MULTICAST_IF ),
276 MAP_OPTION( IP_MULTICAST_TTL ),
277 MAP_OPTION( IP_MULTICAST_LOOP ),
278 MAP_OPTION( IP_ADD_MEMBERSHIP ),
279 MAP_OPTION( IP_DROP_MEMBERSHIP ),
280 MAP_OPTION( IP_OPTIONS ),
282 MAP_OPTION( IP_HDRINCL ),
284 MAP_OPTION( IP_TOS ),
285 MAP_OPTION( IP_TTL ),
288 static const int ws_af_map[][2] =
290 MAP_OPTION( AF_UNSPEC ),
291 MAP_OPTION( AF_INET ),
292 MAP_OPTION( AF_INET6 ),
294 MAP_OPTION( AF_IPX ),
298 static const int ws_socktype_map[][2] =
300 MAP_OPTION( SOCK_DGRAM ),
301 MAP_OPTION( SOCK_STREAM ),
302 MAP_OPTION( SOCK_RAW ),
305 static const int ws_proto_map[][2] =
307 MAP_OPTION( IPPROTO_IP ),
308 MAP_OPTION( IPPROTO_TCP ),
309 MAP_OPTION( IPPROTO_UDP ),
310 MAP_OPTION( IPPROTO_ICMP ),
311 MAP_OPTION( IPPROTO_IGMP ),
312 MAP_OPTION( IPPROTO_RAW ),
315 static const int ws_aiflag_map[][2] =
317 MAP_OPTION( AI_PASSIVE ),
318 MAP_OPTION( AI_CANONNAME ),
319 MAP_OPTION( AI_NUMERICHOST ),
320 /* Linux/UNIX knows a lot more. But Windows only
321 * has 3 as far as I could see. -Marcus
325 static const int ws_eai_map[][2] =
327 MAP_OPTION( EAI_AGAIN ),
328 MAP_OPTION( EAI_BADFLAGS ),
329 MAP_OPTION( EAI_FAIL ),
330 MAP_OPTION( EAI_FAMILY ),
331 MAP_OPTION( EAI_MEMORY ),
332 /* Note: EAI_NODATA is deprecated, but still
333 * used by Windows and Linux... We map the newer
334 * EAI_NONAME to EAI_NODATA for now until Windows
338 MAP_OPTION( EAI_NODATA ),
341 { WS_EAI_NODATA, EAI_NONAME },
344 MAP_OPTION( EAI_SERVICE ),
345 MAP_OPTION( EAI_SOCKTYPE ),
349 inline static DWORD NtStatusToWSAError( const DWORD status )
351 /* We only need to cover the status codes set by server async request handling */
355 case STATUS_SUCCESS: wserr = 0; break;
356 case STATUS_PENDING: wserr = WSA_IO_PENDING; break;
357 case STATUS_INVALID_HANDLE: wserr = WSAENOTSOCK; break; /* WSAEBADF ? */
358 case STATUS_INVALID_PARAMETER: wserr = WSAEINVAL; break;
359 case STATUS_PIPE_DISCONNECTED: wserr = WSAESHUTDOWN; break;
360 case STATUS_CANCELLED: wserr = WSA_OPERATION_ABORTED; break;
361 case STATUS_TIMEOUT: wserr = WSAETIMEDOUT; break;
362 case STATUS_NO_MEMORY: wserr = WSAEFAULT; break;
364 if ( status >= WSABASEERR && status <= WSABASEERR+1004 )
365 /* It is not an NT status code but a winsock error */
369 wserr = RtlNtStatusToDosError( status );
370 FIXME( "Status code %08x converted to DOS error code %x\n", status, wserr );
376 /* set last error code from NT status without mapping WSA errors */
377 inline static unsigned int set_error( unsigned int err )
381 err = NtStatusToWSAError( err );
387 inline static int get_sock_fd( SOCKET s, DWORD access, int *flags )
390 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s), access, &fd, flags ) ))
395 inline static void release_sock_fd( SOCKET s, int fd )
397 wine_server_release_fd( SOCKET2HANDLE(s), fd );
400 static void _enable_event( HANDLE s, unsigned int event,
401 unsigned int sstate, unsigned int cstate )
403 SERVER_START_REQ( enable_socket_event )
407 req->sstate = sstate;
408 req->cstate = cstate;
409 wine_server_call( req );
414 static int _is_blocking(SOCKET s)
417 SERVER_START_REQ( get_socket_event )
419 req->handle = SOCKET2HANDLE(s);
420 req->service = FALSE;
422 wine_server_call( req );
423 ret = (reply->state & FD_WINE_NONBLOCKING) == 0;
429 static unsigned int _get_sock_mask(SOCKET s)
432 SERVER_START_REQ( get_socket_event )
434 req->handle = SOCKET2HANDLE(s);
435 req->service = FALSE;
437 wine_server_call( req );
444 static void _sync_sock_state(SOCKET s)
446 /* do a dummy wineserver request in order to let
447 the wineserver run through its select loop once */
448 (void)_is_blocking(s);
451 static int _get_sock_error(SOCKET s, unsigned int bit)
453 int events[FD_MAX_EVENTS];
455 SERVER_START_REQ( get_socket_event )
457 req->handle = SOCKET2HANDLE(s);
458 req->service = FALSE;
460 wine_server_set_reply( req, events, sizeof(events) );
461 wine_server_call( req );
467 static struct per_thread_data *get_per_thread_data(void)
469 struct per_thread_data * ptb = NtCurrentTeb()->WinSockData;
470 /* lazy initialization */
473 ptb = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ptb) );
474 NtCurrentTeb()->WinSockData = ptb;
479 static void free_per_thread_data(void)
481 struct per_thread_data * ptb = NtCurrentTeb()->WinSockData;
485 /* delete scratch buffers */
486 HeapFree( GetProcessHeap(), 0, ptb->he_buffer );
487 HeapFree( GetProcessHeap(), 0, ptb->se_buffer );
488 HeapFree( GetProcessHeap(), 0, ptb->pe_buffer );
489 ptb->he_buffer = NULL;
490 ptb->se_buffer = NULL;
491 ptb->pe_buffer = NULL;
493 HeapFree( GetProcessHeap(), 0, ptb );
494 NtCurrentTeb()->WinSockData = NULL;
497 /***********************************************************************
498 * DllMain (WS2_32.init)
500 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
502 TRACE("%p 0x%x %p\n", hInstDLL, fdwReason, fImpLoad);
504 case DLL_PROCESS_ATTACH:
506 case DLL_PROCESS_DETACH:
507 free_per_thread_data();
510 case DLL_THREAD_DETACH:
511 free_per_thread_data();
517 /***********************************************************************
520 * Converts socket flags from Windows format.
521 * Return 1 if converted, 0 if not (error).
523 static int convert_sockopt(INT *level, INT *optname)
530 for(i=0; i<sizeof(ws_sock_map)/sizeof(ws_sock_map[0]); i++) {
531 if( ws_sock_map[i][0] == *optname )
533 *optname = ws_sock_map[i][1];
537 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname);
540 *level = IPPROTO_TCP;
541 for(i=0; i<sizeof(ws_tcp_map)/sizeof(ws_tcp_map[0]); i++) {
542 if ( ws_tcp_map[i][0] == *optname )
544 *optname = ws_tcp_map[i][1];
548 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname);
552 for(i=0; i<sizeof(ws_ip_map)/sizeof(ws_ip_map[0]); i++) {
553 if (ws_ip_map[i][0] == *optname )
555 *optname = ws_ip_map[i][1];
559 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname);
561 default: FIXME("Unimplemented or unknown socket level\n");
566 static inline BOOL is_timeout_option( int optname )
569 if (optname == SO_RCVTIMEO) return TRUE;
572 if (optname == SO_SNDTIMEO) return TRUE;
577 /* ----------------------------------- Per-thread info (or per-process?) */
579 static char *strdup_lower(const char *str)
582 char *ret = HeapAlloc( GetProcessHeap(), 0, strlen(str) + 1 );
586 for (i = 0; str[i]; i++) ret[i] = tolower(str[i]);
589 else SetLastError(WSAENOBUFS);
593 inline static int sock_error_p(int s)
595 unsigned int optval, optlen;
597 optlen = sizeof(optval);
598 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
599 if (optval) WARN("\t[%i] error: %d\n", s, optval);
603 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
604 * from an fd and return the value converted to milli seconds
605 * or -1 if there is an infinite time out */
606 static inline int get_rcvsnd_timeo( int fd, int optname)
609 unsigned int len = sizeof(tv);
610 int ret = getsockopt(fd, SOL_SOCKET, optname, &tv, &len);
612 ret = tv.tv_sec * 1000 + tv.tv_usec / 1000;
613 if( ret <= 0 ) /* tv == {0,0} means infinite time out */
618 /* macro wrappers for portability */
620 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
622 #define GET_RCVTIMEO(fd) (-1)
626 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
628 #define GET_SNDTIMEO(fd) (-1)
631 /* utility: given an fd, will block until one of the events occurs */
632 static inline int do_block( int fd, int events, int timeout )
640 while ((ret = poll(&pfd, 1, timeout)) < 0)
651 convert_af_w2u(int windowsaf) {
654 for (i=0;i<sizeof(ws_af_map)/sizeof(ws_af_map[0]);i++)
655 if (ws_af_map[i][0] == windowsaf)
656 return ws_af_map[i][1];
657 FIXME("unhandled Windows address family %d\n", windowsaf);
662 convert_af_u2w(int unixaf) {
665 for (i=0;i<sizeof(ws_af_map)/sizeof(ws_af_map[0]);i++)
666 if (ws_af_map[i][1] == unixaf)
667 return ws_af_map[i][0];
668 FIXME("unhandled UNIX address family %d\n", unixaf);
673 convert_proto_w2u(int windowsproto) {
676 for (i=0;i<sizeof(ws_proto_map)/sizeof(ws_proto_map[0]);i++)
677 if (ws_proto_map[i][0] == windowsproto)
678 return ws_proto_map[i][1];
679 FIXME("unhandled Windows socket protocol %d\n", windowsproto);
684 convert_proto_u2w(int unixproto) {
687 for (i=0;i<sizeof(ws_proto_map)/sizeof(ws_proto_map[0]);i++)
688 if (ws_proto_map[i][1] == unixproto)
689 return ws_proto_map[i][0];
690 FIXME("unhandled UNIX socket protocol %d\n", unixproto);
695 convert_socktype_w2u(int windowssocktype) {
698 for (i=0;i<sizeof(ws_socktype_map)/sizeof(ws_socktype_map[0]);i++)
699 if (ws_socktype_map[i][0] == windowssocktype)
700 return ws_socktype_map[i][1];
701 FIXME("unhandled Windows socket type %d\n", windowssocktype);
706 convert_socktype_u2w(int unixsocktype) {
709 for (i=0;i<sizeof(ws_socktype_map)/sizeof(ws_socktype_map[0]);i++)
710 if (ws_socktype_map[i][1] == unixsocktype)
711 return ws_socktype_map[i][0];
712 FIXME("unhandled UNIX socket type %d\n", unixsocktype);
716 /* ----------------------------------- API -----
718 * Init / cleanup / error checking.
721 /***********************************************************************
722 * WSAStartup (WS2_32.115)
724 int WINAPI WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData)
726 TRACE("verReq=%x\n", wVersionRequested);
728 if (LOBYTE(wVersionRequested) < 1)
729 return WSAVERNOTSUPPORTED;
731 if (!lpWSAData) return WSAEINVAL;
735 /* that's the whole of the negotiation for now */
736 lpWSAData->wVersion = wVersionRequested;
737 /* return winsock information */
738 lpWSAData->wHighVersion = 0x0202;
739 strcpy(lpWSAData->szDescription, "WinSock 2.0" );
740 strcpy(lpWSAData->szSystemStatus, "Running" );
741 lpWSAData->iMaxSockets = WS_MAX_SOCKETS_PER_PROCESS;
742 lpWSAData->iMaxUdpDg = WS_MAX_UDP_DATAGRAM;
743 /* don't do anything with lpWSAData->lpVendorInfo */
744 /* (some apps don't allocate the space for this field) */
746 TRACE("succeeded\n");
751 /***********************************************************************
752 * WSACleanup (WS2_32.116)
754 INT WINAPI WSACleanup(void)
760 SetLastError(WSANOTINITIALISED);
765 /***********************************************************************
766 * WSAGetLastError (WINSOCK.111)
767 * WSAGetLastError (WS2_32.111)
769 INT WINAPI WSAGetLastError(void)
771 return GetLastError();
774 /***********************************************************************
775 * WSASetLastError (WS2_32.112)
777 void WINAPI WSASetLastError(INT iError) {
778 SetLastError(iError);
781 static struct WS_hostent *check_buffer_he(int size)
783 struct per_thread_data * ptb = get_per_thread_data();
786 if (ptb->he_len >= size ) return ptb->he_buffer;
787 HeapFree( GetProcessHeap(), 0, ptb->he_buffer );
789 ptb->he_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->he_len = size) );
790 if (!ptb->he_buffer) SetLastError(WSAENOBUFS);
791 return ptb->he_buffer;
794 static struct WS_servent *check_buffer_se(int size)
796 struct per_thread_data * ptb = get_per_thread_data();
799 if (ptb->se_len >= size ) return ptb->se_buffer;
800 HeapFree( GetProcessHeap(), 0, ptb->se_buffer );
802 ptb->se_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->se_len = size) );
803 if (!ptb->se_buffer) SetLastError(WSAENOBUFS);
804 return ptb->se_buffer;
807 static struct WS_protoent *check_buffer_pe(int size)
809 struct per_thread_data * ptb = get_per_thread_data();
812 if (ptb->pe_len >= size ) return ptb->pe_buffer;
813 HeapFree( GetProcessHeap(), 0, ptb->pe_buffer );
815 ptb->pe_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->pe_len = size) );
816 if (!ptb->pe_buffer) SetLastError(WSAENOBUFS);
817 return ptb->pe_buffer;
820 /* ----------------------------------- i/o APIs */
823 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf)== WS_AF_IPX || (pf) == WS_AF_INET6)
825 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf) == WS_AF_INET6)
829 /**********************************************************************/
831 /* Returns the converted address if successful, NULL if it was too small to
832 * start with. Note that the returned pointer may be the original pointer
833 * if no conversion is necessary.
835 static struct sockaddr* ws_sockaddr_ws2u(const struct WS_sockaddr* wsaddr, int wsaddrlen, unsigned int *uaddrlen)
837 switch (wsaddr->sa_family)
842 const struct WS_sockaddr_ipx* wsipx=(const struct WS_sockaddr_ipx*)wsaddr;
843 struct sockaddr_ipx* uipx;
845 if (wsaddrlen<sizeof(struct WS_sockaddr_ipx))
848 *uaddrlen=sizeof(struct sockaddr_ipx);
849 uipx=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *uaddrlen);
850 uipx->sipx_family=AF_IPX;
851 uipx->sipx_port=wsipx->sa_socket;
852 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
855 memcpy(&uipx->sipx_network,wsipx->sa_netnum,sizeof(uipx->sipx_network)+sizeof(uipx->sipx_node));
856 #ifdef IPX_FRAME_NONE
857 uipx->sipx_type=IPX_FRAME_NONE;
859 return (struct sockaddr*)uipx;
863 struct sockaddr_in6* uin6;
864 const struct WS_sockaddr_in6* win6 = (const struct WS_sockaddr_in6*)wsaddr;
866 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
867 * scope_id, one without. Check:
868 * http://msdn.microsoft.com/library/en-us/winsock/winsock/sockaddr_2.asp
870 if (wsaddrlen >= sizeof(struct WS_sockaddr_in6_old)) {
871 *uaddrlen=sizeof(struct sockaddr_in6);
872 uin6=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *uaddrlen);
873 uin6->sin6_family = AF_INET6;
874 uin6->sin6_port = win6->sin6_port;
875 uin6->sin6_flowinfo = win6->sin6_flowinfo;
876 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
877 if (wsaddrlen >= sizeof(struct WS_sockaddr_in6)) uin6->sin6_scope_id = win6->sin6_scope_id;
879 memcpy(&uin6->sin6_addr,&win6->sin6_addr,16); /* 16 bytes = 128 address bits */
880 return (struct sockaddr*)uin6;
882 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen);
886 struct sockaddr_in* uin;
887 const struct WS_sockaddr_in* win = (const struct WS_sockaddr_in*)wsaddr;
889 if (wsaddrlen<sizeof(struct WS_sockaddr_in))
891 *uaddrlen=sizeof(struct sockaddr_in);
892 uin = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *uaddrlen);
893 uin->sin_family = AF_INET;
894 uin->sin_port = win->sin_port;
895 memcpy(&uin->sin_addr,&win->sin_addr,4); /* 4 bytes = 32 address bits */
896 return (struct sockaddr*)uin;
899 /* Try to determine the needed space by the passed windows sockaddr space */
901 default: /* likely a ipv4 address */
902 case sizeof(struct WS_sockaddr_in):
903 *uaddrlen = sizeof(struct sockaddr_in);
906 case sizeof(struct WS_sockaddr_ipx):
907 *uaddrlen = sizeof(struct sockaddr_ipx);
910 case sizeof(struct WS_sockaddr_in6):
911 case sizeof(struct WS_sockaddr_in6_old):
912 *uaddrlen = sizeof(struct sockaddr_in6);
915 return HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,*uaddrlen);
918 FIXME("Unknown address family %d, return NULL.\n", wsaddr->sa_family);
923 /* Allocates a Unix sockaddr structure to receive the data */
924 static inline struct sockaddr* ws_sockaddr_alloc(const struct WS_sockaddr* wsaddr, int* wsaddrlen, unsigned int* uaddrlen)
928 ERR( "WINE shouldn't pass a NULL wsaddr! Attempting to continue\n" );
930 /* This is not strictly the right thing to do. Hope it works however */
939 *uaddrlen=max(sizeof(struct sockaddr),*wsaddrlen);
941 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *uaddrlen);
944 /* Returns 0 if successful, -1 if the buffer is too small */
945 static int ws_sockaddr_u2ws(const struct sockaddr* uaddr, int uaddrlen, struct WS_sockaddr* wsaddr, int* wsaddrlen)
949 switch(uaddr->sa_family)
954 const struct sockaddr_ipx* uipx=(const struct sockaddr_ipx*)uaddr;
955 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
958 switch (*wsaddrlen) /* how much can we copy? */
963 wsipx->sa_socket=uipx->sipx_port;
967 memcpy(wsipx->sa_nodenum,uipx->sipx_node,sizeof(wsipx->sa_nodenum));
975 memcpy(wsipx->sa_netnum,&uipx->sipx_network,sizeof(wsipx->sa_netnum));
981 wsipx->sa_family=WS_AF_IPX;
992 const struct sockaddr_in6* uin6 = (const struct sockaddr_in6*)uaddr;
993 struct WS_sockaddr_in6_old* win6old = (struct WS_sockaddr_in6_old*)wsaddr;
995 if (*wsaddrlen < sizeof(struct WS_sockaddr_in6_old))
997 win6old->sin6_family = WS_AF_INET6;
998 win6old->sin6_port = uin6->sin6_port;
999 win6old->sin6_flowinfo = uin6->sin6_flowinfo;
1000 memcpy(&win6old->sin6_addr,&uin6->sin6_addr,16); /* 16 bytes = 128 address bits */
1001 *wsaddrlen = sizeof(struct WS_sockaddr_in6_old);
1002 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1003 if (*wsaddrlen >= sizeof(struct WS_sockaddr_in6)) {
1004 struct WS_sockaddr_in6* win6 = (struct WS_sockaddr_in6*)wsaddr;
1005 win6->sin6_scope_id = uin6->sin6_scope_id;
1006 *wsaddrlen = sizeof(struct WS_sockaddr_in6);
1012 const struct sockaddr_in* uin = (const struct sockaddr_in*)uaddr;
1013 struct WS_sockaddr_in* win = (struct WS_sockaddr_in*)wsaddr;
1015 if (*wsaddrlen < sizeof(struct WS_sockaddr_in))
1017 win->sin_family = WS_AF_INET;
1018 win->sin_port = uin->sin_port;
1019 memcpy(&win->sin_addr,&uin->sin_addr,4); /* 4 bytes = 32 address bits */
1020 memset(&win->sin_zero, 0, 8); /* Make sure the null padding is null */
1021 *wsaddrlen = sizeof(struct WS_sockaddr_in);
1025 memset(wsaddr,0,*wsaddrlen);
1029 FIXME("Unknown address family %d\n", uaddr->sa_family);
1035 /* to be called to free the memory allocated by ws_sockaddr_ws2u or
1038 static inline void ws_sockaddr_free(const struct sockaddr* uaddr, const struct WS_sockaddr* wsaddr)
1040 if (uaddr!=(const struct sockaddr*)wsaddr)
1041 HeapFree(GetProcessHeap(), 0, (void *)uaddr);
1044 /**************************************************************************
1045 * Functions for handling overlapped I/O
1046 **************************************************************************/
1048 static void CALLBACK ws2_async_terminate(ws2_async* as, IO_STATUS_BLOCK* iosb)
1050 TRACE( "as: %p uovl %p ovl %p\n", as, as->user_overlapped, iosb );
1052 if (as->event) NtSetEvent( as->event, NULL );
1054 if (as->completion_func)
1055 as->completion_func( NtStatusToWSAError (iosb->u.Status),
1056 iosb->Information, as->user_overlapped, as->flags );
1057 if ( !as->user_overlapped )
1060 /* FIXME: I don't think this is really used */
1061 if ( as->overlapped->hEvent != INVALID_HANDLE_VALUE )
1062 WSACloseEvent( as->overlapped->hEvent );
1064 HeapFree( GetProcessHeap(), 0, iosb );
1067 HeapFree( GetProcessHeap(), 0, as->iovec );
1068 HeapFree( GetProcessHeap(), 0, as );
1071 /***********************************************************************
1072 * WS2_make_async (INTERNAL)
1075 static void WINAPI WS2_async_recv(void*, IO_STATUS_BLOCK*, ULONG);
1076 static void WINAPI WS2_async_send(void*, IO_STATUS_BLOCK*, ULONG);
1077 static void WINAPI WS2_async_shutdown( void*, IO_STATUS_BLOCK*, ULONG);
1079 inline static struct ws2_async*
1080 WS2_make_async(SOCKET s, enum ws2_mode mode, struct iovec *iovec, DWORD dwBufferCount,
1081 LPDWORD lpFlags, struct WS_sockaddr *addr,
1082 LPINT addrlen, LPWSAOVERLAPPED lpOverlapped,
1083 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
1084 IO_STATUS_BLOCK **piosb)
1086 struct ws2_async *wsa = HeapAlloc( GetProcessHeap(), 0, sizeof( ws2_async ) );
1088 TRACE( "wsa %p\n", wsa );
1093 wsa->hSocket = (HANDLE) s;
1099 wsa->flags = *lpFlags;
1100 wsa->addrlen.ptr = addrlen;
1105 wsa->addrlen.val = *addrlen;
1108 ERR("Invalid async mode: %d\n", mode);
1110 wsa->user_overlapped = lpOverlapped;
1111 wsa->completion_func = lpCompletionRoutine;
1113 wsa->n_iovecs = dwBufferCount;
1119 *piosb = (IO_STATUS_BLOCK*)lpOverlapped;
1120 if (!lpCompletionRoutine)
1122 wsa->event = lpOverlapped->hEvent;
1123 NtResetEvent(wsa->event, NULL);
1126 else if (!(*piosb = HeapAlloc( GetProcessHeap(), 0, sizeof(IO_STATUS_BLOCK))))
1129 (*piosb)->Information = 0;
1130 (*piosb)->u.Status = STATUS_PENDING;
1131 TRACE( "wsa %p, h %p, ev %p, iosb %p, uov %p, cfunc %p\n",
1132 wsa, wsa->hSocket, wsa->event,
1133 *piosb, wsa->user_overlapped, wsa->completion_func );
1139 HeapFree( GetProcessHeap(), 0, wsa );
1143 static ULONG ws2_queue_async(struct ws2_async* wsa, IO_STATUS_BLOCK* iosb)
1145 PIO_APC_ROUTINE apc;
1151 case ws2m_read: apc = WS2_async_recv; type = ASYNC_TYPE_READ; break;
1152 case ws2m_write: apc = WS2_async_send; type = ASYNC_TYPE_WRITE; break;
1153 case ws2m_sd_read: apc = WS2_async_shutdown; type = ASYNC_TYPE_READ; break;
1154 case ws2m_sd_write: apc = WS2_async_shutdown; type = ASYNC_TYPE_WRITE; break;
1155 default: FIXME("Unknown internal mode (%d)\n", wsa->mode); return STATUS_INVALID_PARAMETER;
1158 SERVER_START_REQ( register_async )
1160 req->handle = wsa->hSocket;
1165 req->count = iosb->Information;
1166 status = wine_server_call( req );
1170 if ( status ) iosb->u.Status = status;
1171 if ( iosb->u.Status != STATUS_PENDING )
1173 /* Note: we get here a non zero status when we couldn't queue the async
1174 * in the server. Therefore, we simply terminate the async.
1176 status = iosb->u.Status;
1177 ws2_async_terminate(wsa, iosb);
1180 NtCurrentTeb()->num_async_io++;
1181 return STATUS_SUCCESS;
1184 /***********************************************************************
1185 * WS2_recv (INTERNAL)
1187 * Workhorse for both synchronous and asynchronous recv() operations.
1189 static int WS2_recv( int fd, struct iovec* iov, int count,
1190 struct WS_sockaddr *lpFrom, LPINT lpFromlen,
1195 TRACE( "fd %d, iovec %p, count %d addr %s, len %p, flags %x\n",
1196 fd, iov, count, debugstr_sockaddr(lpFrom), lpFromlen, *lpFlags);
1198 hdr.msg_name = NULL;
1202 hdr.msg_namelen = *lpFromlen;
1203 hdr.msg_name = ws_sockaddr_alloc( lpFrom, lpFromlen, &hdr.msg_namelen );
1204 if ( !hdr.msg_name )
1206 WSASetLastError( WSAEFAULT );
1212 hdr.msg_namelen = 0;
1215 hdr.msg_iovlen = count;
1216 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1217 hdr.msg_accrights = NULL;
1218 hdr.msg_accrightslen = 0;
1220 hdr.msg_control = NULL;
1221 hdr.msg_controllen = 0;
1225 if ( (n = recvmsg(fd, &hdr, *lpFlags)) == -1 )
1227 TRACE( "recvmsg error %d\n", errno);
1232 ws_sockaddr_u2ws( hdr.msg_name, hdr.msg_namelen,
1233 lpFrom, lpFromlen ) != 0 )
1235 /* The from buffer was too small, but we read the data
1236 * anyway. Is that really bad?
1238 WSASetLastError( WSAEFAULT );
1239 WARN( "Address buffer too small\n" );
1244 ws_sockaddr_free( hdr.msg_name, lpFrom );
1245 TRACE("-> %d\n", n);
1249 /***********************************************************************
1250 * WS2_async_recv (INTERNAL)
1252 * Handler for overlapped recv() operations.
1254 static void WINAPI WS2_async_recv( void* ovp, IO_STATUS_BLOCK* iosb, ULONG status)
1256 ws2_async* wsa = (ws2_async*) ovp;
1257 int result, fd, err;
1259 TRACE( "(%p %p %x)\n", wsa, iosb, status );
1263 case STATUS_ALERTED:
1264 if ((iosb->u.Status = wine_server_handle_to_fd( wsa->hSocket, FILE_READ_DATA, &fd, NULL ) ))
1266 ws2_async_terminate(wsa, iosb);
1269 result = WS2_recv( fd, wsa->iovec, wsa->n_iovecs,
1270 wsa->addr, wsa->addrlen.ptr, &wsa->flags );
1271 wine_server_release_fd( wsa->hSocket, fd );
1274 iosb->u.Status = STATUS_SUCCESS;
1275 iosb->Information = result;
1276 TRACE( "received %d bytes\n", result );
1277 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1282 if ( err == WSAEINTR || err == WSAEWOULDBLOCK ) /* errno: EINTR / EAGAIN */
1284 iosb->u.Status = STATUS_PENDING;
1285 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1286 TRACE( "still pending\n" );
1290 iosb->u.Status = err; /* FIXME: is this correct ???? */
1291 TRACE( "Error: %x\n", err );
1294 if (iosb->u.Status == STATUS_PENDING)
1295 ws2_queue_async(wsa, iosb);
1297 ws2_async_terminate(wsa, iosb);
1300 FIXME( "status: %d\n", status );
1301 iosb->u.Status = status;
1302 ws2_async_terminate(wsa, iosb);
1307 /***********************************************************************
1308 * WS2_send (INTERNAL)
1310 * Workhorse for both synchronous and asynchronous send() operations.
1312 static int WS2_send( int fd, struct iovec* iov, int count,
1313 const struct WS_sockaddr *to, INT tolen, DWORD dwFlags )
1317 TRACE( "fd %d, iovec %p, count %d addr %s, len %d, flags %x\n",
1318 fd, iov, count, debugstr_sockaddr(to), tolen, dwFlags);
1320 hdr.msg_name = NULL;
1324 hdr.msg_name = (struct sockaddr*) ws_sockaddr_ws2u( to, tolen, &hdr.msg_namelen );
1325 if ( !hdr.msg_name )
1327 WSASetLastError( WSAEFAULT );
1332 if(to->sa_family == WS_AF_IPX)
1335 struct sockaddr_ipx* uipx = (struct sockaddr_ipx*)hdr.msg_name;
1337 unsigned int len=sizeof(int);
1339 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1340 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1341 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1342 * ipx type in the sockaddr_opx structure with the stored value.
1344 if(getsockopt(fd, SOL_IPX, IPX_TYPE, &val, &len) != -1)
1346 TRACE("ptype: %d (fd:%d)\n", val, fd);
1347 uipx->sipx_type = val;
1355 hdr.msg_namelen = 0;
1358 hdr.msg_iovlen = count;
1359 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1360 hdr.msg_accrights = NULL;
1361 hdr.msg_accrightslen = 0;
1363 hdr.msg_control = NULL;
1364 hdr.msg_controllen = 0;
1368 n = sendmsg(fd, &hdr, dwFlags);
1371 ws_sockaddr_free( hdr.msg_name, to );
1375 /***********************************************************************
1376 * WS2_async_send (INTERNAL)
1378 * Handler for overlapped send() operations.
1380 static void WINAPI WS2_async_send(void* as, IO_STATUS_BLOCK* iosb, ULONG status)
1382 ws2_async* wsa = (ws2_async*) as;
1385 TRACE( "(%p %p %x)\n", wsa, iosb, status );
1389 case STATUS_ALERTED:
1390 if (iosb->u.Status != STATUS_PENDING) FIXME("wrong %08x\n", iosb->u.Status);
1391 if ((iosb->u.Status = wine_server_handle_to_fd( wsa->hSocket, FILE_WRITE_DATA, &fd, NULL ) ))
1393 ws2_async_terminate(wsa, iosb);
1396 /* check to see if the data is ready (non-blocking) */
1397 result = WS2_send( fd, wsa->iovec, wsa->n_iovecs, wsa->addr, wsa->addrlen.val, wsa->flags );
1398 wine_server_release_fd( wsa->hSocket, fd );
1402 iosb->u.Status = STATUS_SUCCESS;
1403 iosb->Information = result;
1404 TRACE( "sent %d bytes\n", result );
1405 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1409 int err = wsaErrno();
1410 if ( err == WSAEINTR )
1412 iosb->u.Status = STATUS_PENDING;
1413 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1414 TRACE( "still pending\n" );
1418 /* We set the status to a winsock error code and check for that
1419 later in NtStatusToWSAError () */
1420 iosb->u.Status = err;
1421 TRACE( "Error: %x\n", err );
1424 if (iosb->u.Status == STATUS_PENDING)
1425 ws2_queue_async(wsa, iosb);
1427 ws2_async_terminate(wsa, iosb);
1430 FIXME( "status: %d\n", status );
1431 iosb->u.Status = status;
1432 ws2_async_terminate(wsa, iosb);
1438 /***********************************************************************
1439 * WS2_async_shutdown (INTERNAL)
1441 * Handler for shutdown() operations on overlapped sockets.
1443 static void WINAPI WS2_async_shutdown( void* as, PIO_STATUS_BLOCK iosb, ULONG status )
1445 ws2_async* wsa = (ws2_async*) as;
1448 TRACE( "async %p %d\n", wsa, wsa->mode );
1451 case STATUS_ALERTED:
1452 if ((iosb->u.Status = wine_server_handle_to_fd( wsa->hSocket, 0, &fd, NULL ) ))
1454 ws2_async_terminate(wsa, iosb);
1457 switch ( wsa->mode )
1459 case ws2m_sd_read: err = shutdown( fd, 0 ); break;
1460 case ws2m_sd_write: err = shutdown( fd, 1 ); break;
1461 default: ERR("invalid mode: %d\n", wsa->mode );
1463 wine_server_release_fd( wsa->hSocket, fd );
1464 iosb->u.Status = err ? wsaErrno() : STATUS_SUCCESS;
1465 if (iosb->u.Status == STATUS_PENDING)
1466 ws2_queue_async(wsa, iosb);
1468 ws2_async_terminate(wsa, iosb);
1471 iosb->u.Status = status;
1472 ws2_async_terminate(wsa, iosb);
1478 /***********************************************************************
1479 * WS2_register_async_shutdown (INTERNAL)
1481 * Helper function for WS_shutdown() on overlapped sockets.
1483 static int WS2_register_async_shutdown( SOCKET s, enum ws2_mode mode )
1485 struct ws2_async *wsa;
1486 int ret, err = WSAEFAULT;
1489 LPWSAOVERLAPPED ovl = HeapAlloc(GetProcessHeap(), 0, sizeof( WSAOVERLAPPED ));
1490 IO_STATUS_BLOCK *iosb = NULL;
1492 TRACE("s %d mode %d\n", s, mode);
1496 ovl->hEvent = WSACreateEvent();
1497 if ( ovl->hEvent == WSA_INVALID_EVENT )
1500 wsa = WS2_make_async( s, mode, NULL, 0, &dwflags, NULL, &len, ovl, NULL, &iosb );
1504 /* Hack: this will cause ws2_async_terminate() to free the overlapped structure */
1505 wsa->user_overlapped = NULL;
1506 if ( (ret = ws2_queue_async( wsa, iosb )) )
1508 err = NtStatusToWSAError( ret );
1511 /* Try immediate completion */
1512 while ( WaitForSingleObjectEx( ovl->hEvent, 0, TRUE ) == STATUS_USER_APC );
1516 WSACloseEvent( ovl->hEvent );
1518 HeapFree( GetProcessHeap(), 0, ovl );
1523 /***********************************************************************
1526 SOCKET WINAPI WS_accept(SOCKET s, struct WS_sockaddr *addr,
1532 TRACE("socket %04x\n", s );
1533 is_blocking = _is_blocking(s);
1538 int fd = get_sock_fd( s, FILE_READ_DATA, NULL );
1539 if (fd == -1) return INVALID_SOCKET;
1541 do_block(fd, POLLIN, -1);
1542 _sync_sock_state(s); /* let wineserver notice connection */
1543 release_sock_fd( s, fd );
1544 /* retrieve any error codes from it */
1545 SetLastError(_get_sock_error(s, FD_ACCEPT_BIT));
1546 /* FIXME: care about the error? */
1548 SERVER_START_REQ( accept_socket )
1550 req->lhandle = SOCKET2HANDLE(s);
1551 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
1552 req->attributes = OBJ_INHERIT;
1553 set_error( wine_server_call( req ) );
1554 as = HANDLE2SOCKET( reply->handle );
1559 if (addr) WS_getpeername(as, addr, addrlen32);
1562 } while (is_blocking);
1563 return INVALID_SOCKET;
1566 /***********************************************************************
1569 int WINAPI WS_bind(SOCKET s, const struct WS_sockaddr* name, int namelen)
1571 int fd = get_sock_fd( s, 0, NULL );
1572 int res = SOCKET_ERROR;
1574 TRACE("socket %04x, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1578 if (!name || (name->sa_family && !SUPPORTED_PF(name->sa_family)))
1580 SetLastError(WSAEAFNOSUPPORT);
1584 const struct sockaddr* uaddr;
1585 unsigned int uaddrlen;
1587 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1590 SetLastError(WSAEFAULT);
1594 if (bind(fd, uaddr, uaddrlen) < 0)
1596 int loc_errno = errno;
1597 WARN("\tfailure - errno = %i\n", errno);
1602 SetLastError(WSAENOTSOCK);
1605 SetLastError(WSAEINVAL);
1608 SetLastError(wsaErrno());
1614 res=0; /* success */
1616 ws_sockaddr_free(uaddr,name);
1619 release_sock_fd( s, fd );
1624 /***********************************************************************
1625 * closesocket (WS2_32.3)
1627 int WINAPI WS_closesocket(SOCKET s)
1629 TRACE("socket %04x\n", s);
1630 if (CloseHandle(SOCKET2HANDLE(s))) return 0;
1631 return SOCKET_ERROR;
1634 /***********************************************************************
1635 * connect (WS2_32.4)
1637 int WINAPI WS_connect(SOCKET s, const struct WS_sockaddr* name, int namelen)
1639 int fd = get_sock_fd( s, FILE_READ_DATA, NULL );
1641 TRACE("socket %04x, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1645 const struct sockaddr* uaddr;
1646 unsigned int uaddrlen;
1648 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1651 SetLastError(WSAEFAULT);
1657 rc=connect(fd, uaddr, uaddrlen);
1658 ws_sockaddr_free(uaddr,name);
1660 goto connect_success;
1663 if (errno == EINPROGRESS)
1665 /* tell wineserver that a connection is in progress */
1666 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1667 FD_CONNECT|FD_READ|FD_WRITE,
1668 FD_WINE_CONNECTED|FD_WINE_LISTENING);
1669 if (_is_blocking(s))
1673 do_block(fd, POLLIN | POLLOUT, -1);
1674 _sync_sock_state(s); /* let wineserver notice connection */
1675 /* retrieve any error codes from it */
1676 result = _get_sock_error(s, FD_CONNECT_BIT);
1678 SetLastError(result);
1681 goto connect_success;
1686 SetLastError(WSAEWOULDBLOCK);
1691 SetLastError(wsaErrno());
1693 release_sock_fd( s, fd );
1695 return SOCKET_ERROR;
1698 release_sock_fd( s, fd );
1699 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1700 FD_WINE_CONNECTED|FD_READ|FD_WRITE,
1701 FD_CONNECT|FD_WINE_LISTENING);
1705 /***********************************************************************
1706 * WSAConnect (WS2_32.30)
1708 int WINAPI WSAConnect( SOCKET s, const struct WS_sockaddr* name, int namelen,
1709 LPWSABUF lpCallerData, LPWSABUF lpCalleeData,
1710 LPQOS lpSQOS, LPQOS lpGQOS )
1712 if ( lpCallerData || lpCalleeData || lpSQOS || lpGQOS )
1713 FIXME("unsupported parameters!\n");
1714 return WS_connect( s, name, namelen );
1718 /***********************************************************************
1719 * getpeername (WS2_32.5)
1721 int WINAPI WS_getpeername(SOCKET s, struct WS_sockaddr *name, int *namelen)
1726 TRACE("socket: %04x, ptr %p, len %08x\n", s, name, *namelen);
1728 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1729 if( (name == NULL) || (namelen == NULL) )
1731 SetLastError( WSAEFAULT );
1732 return SOCKET_ERROR;
1735 fd = get_sock_fd( s, 0, NULL );
1740 struct sockaddr* uaddr;
1741 unsigned int uaddrlen;
1743 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1744 if (getpeername(fd, uaddr, &uaddrlen) != 0)
1746 SetLastError(wsaErrno());
1748 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1750 /* The buffer was too small */
1751 SetLastError(WSAEFAULT);
1757 ws_sockaddr_free(uaddr,name);
1758 release_sock_fd( s, fd );
1763 /***********************************************************************
1764 * getsockname (WS2_32.6)
1766 int WINAPI WS_getsockname(SOCKET s, struct WS_sockaddr *name, int *namelen)
1771 TRACE("socket: %04x, ptr %p, len %8x\n", s, name, *namelen);
1773 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1774 if( (name == NULL) || (namelen == NULL) )
1776 SetLastError( WSAEFAULT );
1777 return SOCKET_ERROR;
1780 fd = get_sock_fd( s, 0, NULL );
1785 struct sockaddr* uaddr;
1786 unsigned int uaddrlen;
1788 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1789 if (getsockname(fd, uaddr, &uaddrlen) != 0)
1791 SetLastError(wsaErrno());
1793 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1795 /* The buffer was too small */
1796 SetLastError(WSAEFAULT);
1802 ws_sockaddr_free(uaddr,name);
1803 release_sock_fd( s, fd );
1808 /***********************************************************************
1809 * getsockopt (WS2_32.7)
1811 INT WINAPI WS_getsockopt(SOCKET s, INT level,
1812 INT optname, char *optval, INT *optlen)
1817 TRACE("socket: %04x, level 0x%x, name 0x%x, ptr %p, len %d\n",
1818 s, level, optname, optval, *optlen);
1826 /* Handle common cases. The special cases are below, sorted
1828 case WS_SO_ACCEPTCONN:
1829 case WS_SO_BROADCAST:
1832 case WS_SO_KEEPALIVE:
1833 case WS_SO_OOBINLINE:
1837 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1838 return SOCKET_ERROR;
1839 convert_sockopt(&level, &optname);
1840 if (getsockopt(fd,(int) level, optname, optval,
1841 (unsigned int *)optlen) != 0 )
1843 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1846 release_sock_fd( s, fd );
1849 case WS_SO_DONTLINGER:
1851 struct linger lingval;
1852 unsigned int len = sizeof(struct linger);
1854 if (!optlen || *optlen < sizeof(BOOL)|| !optval)
1856 SetLastError(WSAEFAULT);
1857 return SOCKET_ERROR;
1859 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1860 return SOCKET_ERROR;
1862 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &lingval, &len) != 0 )
1864 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1869 *(BOOL *)optval = (lingval.l_onoff) ? FALSE : TRUE;
1870 *optlen = sizeof(BOOL);
1873 release_sock_fd( s, fd );
1877 /* As mentioned in setsockopt, Windows ignores this, so we
1878 * always return true here */
1879 case WS_SO_DONTROUTE:
1880 if (!optlen || *optlen < sizeof(BOOL) || !optval)
1882 SetLastError(WSAEFAULT);
1883 return SOCKET_ERROR;
1885 *(BOOL *)optval = TRUE;
1886 *optlen = sizeof(BOOL);
1891 struct linger lingval;
1892 unsigned int len = sizeof(struct linger);
1894 /* struct linger and LINGER have different sizes */
1895 if (!optlen || *optlen < sizeof(LINGER) || !optval)
1897 SetLastError(WSAEFAULT);
1898 return SOCKET_ERROR;
1900 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1901 return SOCKET_ERROR;
1903 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &lingval, &len) != 0 )
1905 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1910 ((LINGER *)optval)->l_onoff = lingval.l_onoff;
1911 ((LINGER *)optval)->l_linger = lingval.l_linger;
1912 *optlen = sizeof(struct linger);
1915 release_sock_fd( s, fd );
1919 case WS_SO_MAX_MSG_SIZE:
1920 if (!optlen || *optlen < sizeof(int) || !optval)
1922 SetLastError(WSAEFAULT);
1923 return SOCKET_ERROR;
1925 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
1926 *(int *)optval = 65507;
1927 *optlen = sizeof(int);
1930 /* SO_OPENTYPE does not require a valid socket handle. */
1931 case WS_SO_OPENTYPE:
1932 if (!optlen || *optlen < sizeof(int) || !optval)
1934 SetLastError(WSAEFAULT);
1935 return SOCKET_ERROR;
1937 *(int *)optval = get_per_thread_data()->opentype;
1938 *optlen = sizeof(int);
1939 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
1943 case WS_SO_RCVTIMEO:
1946 case WS_SO_SNDTIMEO:
1948 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
1951 unsigned int len = sizeof(struct timeval);
1953 if (!optlen || *optlen < sizeof(int)|| !optval)
1955 SetLastError(WSAEFAULT);
1956 return SOCKET_ERROR;
1958 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
1959 return SOCKET_ERROR;
1961 convert_sockopt(&level, &optname);
1962 if (getsockopt(fd,(int) level, optname, &tv, &len) != 0 )
1964 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1969 *(int *)optval = tv.tv_sec * 1000 + tv.tv_usec / 1000;
1970 *optlen = sizeof(int);
1973 release_sock_fd( s, fd );
1977 /* As mentioned in setsockopt, the windows style SO_REUSEADDR is
1978 * not possible in Unix, so always return false here. */
1979 case WS_SO_REUSEADDR:
1980 if (!optlen || *optlen < sizeof(int) || !optval)
1982 SetLastError(WSAEFAULT);
1983 return SOCKET_ERROR;
1986 *optlen = sizeof(int);
1990 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname);
1991 SetLastError(WSAENOPROTOOPT);
1992 return SOCKET_ERROR;
1993 } /* end switch(optname) */
1994 }/* end case WS_SOL_SOCKET */
1998 struct WS_sockaddr_ipx addr;
1999 IPX_ADDRESS_DATA *data;
2004 if ((fd = get_sock_fd( s, 0, NULL )) == -1) return SOCKET_ERROR;
2006 if(getsockopt(fd, SOL_IPX, IPX_TYPE, optval, (unsigned int*)optlen) == -1)
2013 socklen_t len=sizeof(struct ipx);
2014 if(getsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, &len) == -1 )
2017 *optval = (int)val.ipx_pt;
2020 TRACE("ptype: %d (fd: %d)\n", *(int*)optval, fd);
2021 release_sock_fd( s, fd );
2026 * On a Win2000 system with one network card there are usually
2027 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
2028 * Using this call you can then retrieve info about this all.
2029 * In case of Linux it is a bit different. Usually you have
2030 * only "one" device active and further it is not possible to
2031 * query things like the linkspeed.
2033 FIXME("IPX_ADDRESS\n");
2034 namelen = sizeof(struct WS_sockaddr_ipx);
2035 memset(&addr, 0, sizeof(struct WS_sockaddr_ipx));
2036 WS_getsockname(s, (struct WS_sockaddr*)&addr, &namelen);
2038 data = (IPX_ADDRESS_DATA*)optval;
2039 memcpy(data->nodenum,&addr.sa_nodenum,sizeof(data->nodenum));
2040 memcpy(data->netnum,&addr.sa_netnum,sizeof(data->netnum));
2041 data->adapternum = 0;
2042 data->wan = FALSE; /* We are not on a wan for now .. */
2043 data->status = FALSE; /* Since we are not on a wan, the wan link isn't up */
2044 data->maxpkt = 1467; /* This value is the default one, at least on Win2k/WinXP */
2045 data->linkspeed = 100000; /* Set the line speed in 100bit/s to 10 Mbit;
2046 * note 1MB = 1000kB in this case */
2049 case IPX_MAX_ADAPTER_NUM:
2050 FIXME("IPX_MAX_ADAPTER_NUM\n");
2051 *(int*)optval = 1; /* As noted under IPX_ADDRESS we have just one card. */
2055 FIXME("IPX optname:%x\n", optname);
2056 return SOCKET_ERROR;
2057 }/* end switch(optname) */
2058 } /* end case NSPROTO_IPX */
2060 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
2061 case WS_IPPROTO_TCP:
2064 case WS_TCP_NODELAY:
2065 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
2066 return SOCKET_ERROR;
2067 convert_sockopt(&level, &optname);
2068 if (getsockopt(fd,(int) level, optname, optval,
2069 (unsigned int *)optlen) != 0 )
2071 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2074 release_sock_fd( s, fd );
2077 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname);
2078 return SOCKET_ERROR;
2083 case WS_IP_ADD_MEMBERSHIP:
2084 case WS_IP_DROP_MEMBERSHIP:
2088 case WS_IP_MULTICAST_IF:
2089 case WS_IP_MULTICAST_LOOP:
2090 case WS_IP_MULTICAST_TTL:
2094 if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
2095 return SOCKET_ERROR;
2096 convert_sockopt(&level, &optname);
2097 if (getsockopt(fd,(int) level, optname, optval,
2098 (unsigned int *)optlen) != 0 )
2100 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2103 release_sock_fd( s, fd );
2106 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname);
2107 return SOCKET_ERROR;
2110 FIXME("Unknown level: 0x%08x\n", level);
2111 return SOCKET_ERROR;
2112 } /* end switch(level) */
2115 /***********************************************************************
2119 WS_u_long WINAPI WS_htonl(WS_u_long hostlong)
2121 return htonl(hostlong);
2125 /***********************************************************************
2129 WS_u_short WINAPI WS_htons(WS_u_short hostshort)
2131 return htons(hostshort);
2134 /***********************************************************************
2135 * WSAHtonl (WS2_32.46)
2136 * From MSDN decription of error codes, this function should also
2137 * check if WinSock has been initialized and the socket is a valid
2138 * socket. But why? This function only translates a host byte order
2139 * u_long into a network byte order u_long...
2141 int WINAPI WSAHtonl(SOCKET s, WS_u_long hostlong, WS_u_long *lpnetlong)
2145 *lpnetlong = htonl(hostlong);
2148 WSASetLastError(WSAEFAULT);
2149 return SOCKET_ERROR;
2152 /***********************************************************************
2153 * WSAHtons (WS2_32.47)
2154 * From MSDN decription of error codes, this function should also
2155 * check if WinSock has been initialized and the socket is a valid
2156 * socket. But why? This function only translates a host byte order
2157 * u_short into a network byte order u_short...
2159 int WINAPI WSAHtons(SOCKET s, WS_u_short hostshort, WS_u_short *lpnetshort)
2164 *lpnetshort = htons(hostshort);
2167 WSASetLastError(WSAEFAULT);
2168 return SOCKET_ERROR;
2172 /***********************************************************************
2173 * inet_addr (WINSOCK.10)
2174 * inet_addr (WS2_32.11)
2176 WS_u_long WINAPI WS_inet_addr(const char *cp)
2178 if (!cp) return INADDR_NONE;
2179 return inet_addr(cp);
2183 /***********************************************************************
2184 * ntohl (WINSOCK.14)
2187 WS_u_long WINAPI WS_ntohl(WS_u_long netlong)
2189 return ntohl(netlong);
2193 /***********************************************************************
2194 * ntohs (WINSOCK.15)
2197 WS_u_short WINAPI WS_ntohs(WS_u_short netshort)
2199 return ntohs(netshort);
2203 /***********************************************************************
2204 * inet_ntoa (WS2_32.12)
2206 char* WINAPI WS_inet_ntoa(struct WS_in_addr in)
2208 /* use "buffer for dummies" here because some applications have a
2209 * propensity to decode addresses in ws_hostent structure without
2210 * saving them first...
2212 static char dbuffer[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
2214 char* s = inet_ntoa(*((struct in_addr*)&in));
2220 SetLastError(wsaErrno());
2224 /**********************************************************************
2225 * WSAIoctl (WS2_32.50)
2228 INT WINAPI WSAIoctl(SOCKET s,
2229 DWORD dwIoControlCode,
2232 LPVOID lpbOutBuffer,
2234 LPDWORD lpcbBytesReturned,
2235 LPWSAOVERLAPPED lpOverlapped,
2236 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
2238 TRACE("%d, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
2239 s, dwIoControlCode, lpvInBuffer, cbInBuffer, lpbOutBuffer,
2240 cbOutBuffer, lpcbBytesReturned, lpOverlapped, lpCompletionRoutine);
2242 switch( dwIoControlCode )
2245 if (cbInBuffer != sizeof(WS_u_long)) {
2246 WSASetLastError(WSAEFAULT);
2247 return SOCKET_ERROR;
2249 return WS_ioctlsocket( s, WS_FIONBIO, lpvInBuffer);
2252 if (cbOutBuffer != sizeof(WS_u_long)) {
2253 WSASetLastError(WSAEFAULT);
2254 return SOCKET_ERROR;
2256 return WS_ioctlsocket( s, WS_FIONREAD, lpbOutBuffer);
2258 case WS_SIO_GET_INTERFACE_LIST:
2260 INTERFACE_INFO* intArray = (INTERFACE_INFO*)lpbOutBuffer;
2261 DWORD size, numInt, apiReturn;
2264 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
2268 WSASetLastError(WSAEFAULT);
2269 return SOCKET_ERROR;
2271 if (!lpcbBytesReturned)
2273 WSASetLastError(WSAEFAULT);
2274 return SOCKET_ERROR;
2277 fd = get_sock_fd( s, 0, NULL );
2278 if (fd == -1) return SOCKET_ERROR;
2280 apiReturn = GetAdaptersInfo(NULL, &size);
2281 if (apiReturn == ERROR_NO_DATA)
2285 else if (apiReturn == ERROR_BUFFER_OVERFLOW)
2287 PIP_ADAPTER_INFO table = HeapAlloc(GetProcessHeap(),0,size);
2291 if (GetAdaptersInfo(table, &size) == NO_ERROR)
2293 PIP_ADAPTER_INFO ptr;
2295 if (size*sizeof(INTERFACE_INFO)/sizeof(IP_ADAPTER_INFO) > cbOutBuffer)
2297 WARN("Buffer too small = %u, cbOutBuffer = %u\n", size, cbOutBuffer);
2298 HeapFree(GetProcessHeap(),0,table);
2299 release_sock_fd( s, fd );
2300 WSASetLastError(WSAEFAULT);
2301 return SOCKET_ERROR;
2303 for (ptr = table, numInt = 0; ptr;
2304 ptr = ptr->Next, intArray++, numInt++)
2306 unsigned int addr, mask, bcast;
2307 struct ifreq ifInfo;
2309 /* Socket Status Flags */
2310 lstrcpynA(ifInfo.ifr_name, ptr->AdapterName, IFNAMSIZ);
2311 if (ioctl(fd, SIOCGIFFLAGS, &ifInfo) < 0)
2313 ERR("Error obtaining status flags for socket!\n");
2314 HeapFree(GetProcessHeap(),0,table);
2315 release_sock_fd( s, fd );
2316 WSASetLastError(WSAEINVAL);
2317 return SOCKET_ERROR;
2321 /* set flags; the values of IFF_* are not the same
2322 under Linux and Windows, therefore must generate
2324 intArray->iiFlags = 0;
2325 if (ifInfo.ifr_flags & IFF_BROADCAST)
2326 intArray->iiFlags |= WS_IFF_BROADCAST;
2327 #ifdef IFF_POINTOPOINT
2328 if (ifInfo.ifr_flags & IFF_POINTOPOINT)
2329 intArray->iiFlags |= WS_IFF_POINTTOPOINT;
2331 if (ifInfo.ifr_flags & IFF_LOOPBACK)
2332 intArray->iiFlags |= WS_IFF_LOOPBACK;
2333 if (ifInfo.ifr_flags & IFF_UP)
2334 intArray->iiFlags |= WS_IFF_UP;
2335 if (ifInfo.ifr_flags & IFF_MULTICAST)
2336 intArray->iiFlags |= WS_IFF_MULTICAST;
2339 addr = inet_addr(ptr->IpAddressList.IpAddress.String);
2340 mask = inet_addr(ptr->IpAddressList.IpMask.String);
2341 bcast = addr | (addr & !mask);
2342 intArray->iiAddress.AddressIn.sin_family = AF_INET;
2343 intArray->iiAddress.AddressIn.sin_port = 0;
2344 intArray->iiAddress.AddressIn.sin_addr.WS_s_addr =
2346 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
2347 intArray->iiNetmask.AddressIn.sin_port = 0;
2348 intArray->iiNetmask.AddressIn.sin_addr.WS_s_addr =
2350 intArray->iiBroadcastAddress.AddressIn.sin_family =
2352 intArray->iiBroadcastAddress.AddressIn.sin_port = 0;
2353 intArray->iiBroadcastAddress.AddressIn.sin_addr.
2359 ERR("Unable to get interface table!\n");
2360 release_sock_fd( s, fd );
2361 HeapFree(GetProcessHeap(),0,table);
2362 WSASetLastError(WSAEINVAL);
2363 return SOCKET_ERROR;
2365 HeapFree(GetProcessHeap(),0,table);
2369 release_sock_fd( s, fd );
2370 WSASetLastError(WSAEINVAL);
2371 return SOCKET_ERROR;
2376 ERR("Unable to get interface table!\n");
2377 release_sock_fd( s, fd );
2378 WSASetLastError(WSAEINVAL);
2379 return SOCKET_ERROR;
2381 /* Calculate the size of the array being returned */
2382 *lpcbBytesReturned = sizeof(INTERFACE_INFO) * numInt;
2383 release_sock_fd( s, fd );
2387 case WS_SIO_ADDRESS_LIST_CHANGE:
2388 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
2389 /* FIXME: error and return code depend on whether socket was created
2390 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
2393 case WS_SIO_ADDRESS_LIST_QUERY:
2397 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
2399 if (!lpcbBytesReturned)
2401 WSASetLastError(WSAEFAULT);
2402 return SOCKET_ERROR;
2405 if (GetAdaptersInfo(NULL, &size) == ERROR_BUFFER_OVERFLOW)
2407 IP_ADAPTER_INFO *p, *table = HeapAlloc(GetProcessHeap(), 0, size);
2410 if (!table || GetAdaptersInfo(table, &size))
2412 HeapFree(GetProcessHeap(), 0, table);
2413 WSASetLastError(WSAEINVAL);
2414 return SOCKET_ERROR;
2417 for (p = table, num = 0; p; p = p->Next)
2418 if (p->IpAddressList.IpAddress.String[0]) num++;
2420 need = sizeof(SOCKET_ADDRESS_LIST) + sizeof(SOCKET_ADDRESS) * (num - 1);
2421 need += sizeof(SOCKADDR) * num;
2422 *lpcbBytesReturned = need;
2424 if (need > cbOutBuffer)
2426 HeapFree(GetProcessHeap(), 0, table);
2427 WSASetLastError(WSAEFAULT);
2428 return SOCKET_ERROR;
2435 SOCKET_ADDRESS_LIST *sa_list = (SOCKET_ADDRESS_LIST *)lpbOutBuffer;
2436 SOCKADDR_IN *sockaddr;
2438 sa = sa_list->Address;
2439 sockaddr = (SOCKADDR_IN *)((char *)sa + num * sizeof(SOCKET_ADDRESS));
2440 sa_list->iAddressCount = num;
2442 for (p = table, i = 0; p; p = p->Next)
2444 if (!p->IpAddressList.IpAddress.String[0]) continue;
2446 sa[i].lpSockaddr = (SOCKADDR *)&sockaddr[i];
2447 sa[i].iSockaddrLength = sizeof(SOCKADDR);
2449 sockaddr[i].sin_family = AF_INET;
2450 sockaddr[i].sin_port = 0;
2451 sockaddr[i].sin_addr.WS_s_addr = inet_addr(p->IpAddressList.IpAddress.String);
2456 HeapFree(GetProcessHeap(), 0, table);
2461 WARN("unable to get IP address list\n");
2462 WSASetLastError(WSAEINVAL);
2463 return SOCKET_ERROR;
2467 FIXME("SIO_FLUSH: stub.\n");
2471 FIXME("unsupported WS_IOCTL cmd (%08x)\n", dwIoControlCode);
2472 WSASetLastError(WSAEOPNOTSUPP);
2473 return SOCKET_ERROR;
2480 /***********************************************************************
2481 * ioctlsocket (WS2_32.10)
2483 int WINAPI WS_ioctlsocket(SOCKET s, long cmd, WS_u_long *argp)
2488 TRACE("socket %04x, cmd %08lx, ptr %p\n", s, cmd, argp);
2497 if( _get_sock_mask(s) )
2499 /* AsyncSelect()'ed sockets are always nonblocking */
2500 if (*argp) return 0;
2501 SetLastError(WSAEINVAL);
2502 return SOCKET_ERROR;
2504 fd = get_sock_fd( s, 0, NULL );
2510 _enable_event(SOCKET2HANDLE(s), 0, FD_WINE_NONBLOCKING, 0);
2511 ret = fcntl( fd, F_SETFL, O_NONBLOCK );
2515 _enable_event(SOCKET2HANDLE(s), 0, 0, FD_WINE_NONBLOCKING);
2516 ret = fcntl( fd, F_SETFL, 0 );
2518 release_sock_fd( s, fd );
2520 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2522 return SOCKET_ERROR;
2529 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
2530 SetLastError(WSAEINVAL);
2531 return SOCKET_ERROR;
2533 case SIOCGIFBRDADDR:
2534 case SIOCGIFNETMASK:
2536 /* These don't need any special handling. They are used by
2537 WsControl, and are here to suppress an unnecessary warning. */
2541 /* Netscape tries hard to use bogus ioctl 0x667e */
2542 /* FIXME: 0x667e above is ('f' << 8) | 126, and is a low word of
2543 * FIONBIO (_IOW('f', 126, u_long)), how that should be handled?
2545 WARN("\tunknown WS_IOCTL cmd (%08lx)\n", cmd);
2549 fd = get_sock_fd( s, 0, NULL );
2552 if( ioctl(fd, newcmd, (char*)argp ) == 0 )
2554 release_sock_fd( s, fd );
2557 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2558 release_sock_fd( s, fd );
2560 return SOCKET_ERROR;
2563 /***********************************************************************
2564 * listen (WS2_32.13)
2566 int WINAPI WS_listen(SOCKET s, int backlog)
2568 int fd = get_sock_fd( s, FILE_READ_DATA, NULL );
2570 TRACE("socket %04x, backlog %d\n", s, backlog);
2573 if (listen(fd, backlog) == 0)
2575 release_sock_fd( s, fd );
2576 _enable_event(SOCKET2HANDLE(s), FD_ACCEPT,
2578 FD_CONNECT|FD_WINE_CONNECTED);
2581 SetLastError(wsaErrno());
2582 release_sock_fd( s, fd );
2584 return SOCKET_ERROR;
2587 /***********************************************************************
2590 int WINAPI WS_recv(SOCKET s, char *buf, int len, int flags)
2592 DWORD n, dwFlags = flags;
2598 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, NULL, NULL, NULL, NULL) == SOCKET_ERROR )
2599 return SOCKET_ERROR;
2604 /***********************************************************************
2605 * recvfrom (WS2_32.17)
2607 int WINAPI WS_recvfrom(SOCKET s, char *buf, INT len, int flags,
2608 struct WS_sockaddr *from, int *fromlen)
2610 DWORD n, dwFlags = flags;
2616 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, from, fromlen, NULL, NULL) == SOCKET_ERROR )
2617 return SOCKET_ERROR;
2622 /* allocate a poll array for the corresponding fd sets */
2623 static struct pollfd *fd_sets_to_poll( const WS_fd_set *readfds, const WS_fd_set *writefds,
2624 const WS_fd_set *exceptfds, int *count_ptr )
2626 int i, j = 0, count = 0;
2629 if (readfds) count += readfds->fd_count;
2630 if (writefds) count += writefds->fd_count;
2631 if (exceptfds) count += exceptfds->fd_count;
2633 if (!count) return NULL;
2634 if (!(fds = HeapAlloc( GetProcessHeap(), 0, count * sizeof(fds[0])))) return NULL;
2636 for (i = 0; i < readfds->fd_count; i++, j++)
2638 fds[j].fd = get_sock_fd( readfds->fd_array[i], FILE_READ_DATA, NULL );
2639 fds[j].events = POLLIN;
2643 for (i = 0; i < writefds->fd_count; i++, j++)
2645 fds[j].fd = get_sock_fd( writefds->fd_array[i], FILE_WRITE_DATA, NULL );
2646 fds[j].events = POLLOUT;
2650 for (i = 0; i < exceptfds->fd_count; i++, j++)
2652 fds[j].fd = get_sock_fd( exceptfds->fd_array[i], 0, NULL );
2653 fds[j].events = POLLHUP;
2659 /* release the file descriptor obtained in fd_sets_to_poll */
2660 /* must be called with the original fd_set arrays, before calling get_poll_results */
2661 static void release_poll_fds( const WS_fd_set *readfds, const WS_fd_set *writefds,
2662 const WS_fd_set *exceptfds, struct pollfd *fds )
2668 for (i = 0; i < readfds->fd_count; i++, j++)
2669 if (fds[j].fd != -1) release_sock_fd( readfds->fd_array[i], fds[j].fd );
2673 for (i = 0; i < writefds->fd_count; i++, j++)
2674 if (fds[j].fd != -1) release_sock_fd( writefds->fd_array[i], fds[j].fd );
2678 for (i = 0; i < exceptfds->fd_count; i++, j++)
2679 if (fds[j].fd != -1)
2681 /* make sure we have a real error before releasing the fd */
2682 if (!sock_error_p( fds[j].fd )) fds[j].revents = 0;
2683 release_sock_fd( exceptfds->fd_array[i], fds[j].fd );
2688 /* map the poll results back into the Windows fd sets */
2689 static int get_poll_results( WS_fd_set *readfds, WS_fd_set *writefds, WS_fd_set *exceptfds,
2690 const struct pollfd *fds )
2692 int i, j = 0, k, total = 0;
2696 for (i = k = 0; i < readfds->fd_count; i++, j++)
2697 if (fds[j].revents) readfds->fd_array[k++] = readfds->fd_array[i];
2698 readfds->fd_count = k;
2703 for (i = k = 0; i < writefds->fd_count; i++, j++)
2704 if (fds[j].revents) writefds->fd_array[k++] = writefds->fd_array[i];
2705 writefds->fd_count = k;
2710 for (i = k = 0; i < exceptfds->fd_count; i++, j++)
2711 if (fds[j].revents) exceptfds->fd_array[k++] = exceptfds->fd_array[i];
2712 exceptfds->fd_count = k;
2719 /***********************************************************************
2720 * select (WS2_32.18)
2722 int WINAPI WS_select(int nfds, WS_fd_set *ws_readfds,
2723 WS_fd_set *ws_writefds, WS_fd_set *ws_exceptfds,
2724 const struct WS_timeval* ws_timeout)
2726 struct pollfd *pollfds;
2727 int count, ret, timeout = -1;
2729 TRACE("read %p, write %p, excp %p timeout %p\n",
2730 ws_readfds, ws_writefds, ws_exceptfds, ws_timeout);
2732 if (!(pollfds = fd_sets_to_poll( ws_readfds, ws_writefds, ws_exceptfds, &count )) && count)
2734 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
2738 if (ws_timeout) timeout = (ws_timeout->tv_sec * 1000) + (ws_timeout->tv_usec + 999) / 1000;
2740 ret = poll( pollfds, count, timeout );
2741 release_poll_fds( ws_readfds, ws_writefds, ws_exceptfds, pollfds );
2743 if (ret == -1) SetLastError(wsaErrno());
2744 else ret = get_poll_results( ws_readfds, ws_writefds, ws_exceptfds, pollfds );
2745 HeapFree( GetProcessHeap(), 0, pollfds );
2750 /***********************************************************************
2753 int WINAPI WS_send(SOCKET s, const char *buf, int len, int flags)
2759 wsabuf.buf = (char*) buf;
2761 if ( WSASendTo( s, &wsabuf, 1, &n, flags, NULL, 0, NULL, NULL) == SOCKET_ERROR )
2762 return SOCKET_ERROR;
2767 /***********************************************************************
2768 * WSASend (WS2_32.72)
2770 INT WINAPI WSASend( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2771 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2772 LPWSAOVERLAPPED lpOverlapped,
2773 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2775 return WSASendTo( s, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags,
2776 NULL, 0, lpOverlapped, lpCompletionRoutine );
2779 /***********************************************************************
2780 * WSASendDisconnect (WS2_32.73)
2782 INT WINAPI WSASendDisconnect( SOCKET s, LPWSABUF lpBuffers )
2784 return WS_shutdown( s, SD_SEND );
2788 /***********************************************************************
2789 * WSASendTo (WS2_32.74)
2791 INT WINAPI WSASendTo( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2792 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2793 const struct WS_sockaddr *to, int tolen,
2794 LPWSAOVERLAPPED lpOverlapped,
2795 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2798 int n, fd, err = WSAENOTSOCK, flags, ret;
2799 struct iovec* iovec;
2800 struct ws2_async *wsa;
2801 IO_STATUS_BLOCK* iosb;
2803 TRACE("socket %04x, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
2804 s, lpBuffers, dwBufferCount, dwFlags,
2805 to, tolen, lpOverlapped, lpCompletionRoutine);
2807 fd = get_sock_fd( s, FILE_WRITE_DATA, &flags );
2808 TRACE( "fd=%d, flags=%x\n", fd, flags );
2810 if ( fd == -1 ) return SOCKET_ERROR;
2812 if (flags & FD_FLAG_SEND_SHUTDOWN)
2814 WSASetLastError( WSAESHUTDOWN );
2818 if ( !lpNumberOfBytesSent )
2824 iovec = HeapAlloc(GetProcessHeap(), 0, dwBufferCount * sizeof(struct iovec) );
2832 for ( i = 0; i < dwBufferCount; i++ )
2834 iovec[i].iov_base = lpBuffers[i].buf;
2835 iovec[i].iov_len = lpBuffers[i].len;
2838 if ( (lpOverlapped || lpCompletionRoutine) && flags & FD_FLAG_OVERLAPPED )
2840 wsa = WS2_make_async( s, ws2m_write, iovec, dwBufferCount,
2841 &dwFlags, (struct WS_sockaddr*) to, &tolen,
2842 lpOverlapped, lpCompletionRoutine, &iosb );
2849 if ( ( ret = ws2_queue_async( wsa, iosb ) ) )
2851 err = NtStatusToWSAError( ret );
2853 if ( !lpOverlapped )
2854 HeapFree( GetProcessHeap(), 0, iosb );
2855 HeapFree( GetProcessHeap(), 0, wsa );
2858 release_sock_fd( s, fd );
2860 /* Try immediate completion */
2863 if ( WSAGetOverlappedResult( s, lpOverlapped,
2864 lpNumberOfBytesSent, FALSE, &dwFlags) )
2867 if ( (err = WSAGetLastError()) != WSA_IO_INCOMPLETE )
2871 WSASetLastError( WSA_IO_PENDING );
2872 return SOCKET_ERROR;
2875 if (_is_blocking(s))
2877 /* FIXME: exceptfds? */
2878 int timeout = GET_SNDTIMEO(fd);
2879 if( !do_block(fd, POLLOUT, timeout)) {
2881 goto err_free; /* msdn says a timeout in send is fatal */
2885 n = WS2_send( fd, iovec, dwBufferCount, to, tolen, dwFlags );
2889 if ( err == WSAEWOULDBLOCK )
2890 _enable_event(SOCKET2HANDLE(s), FD_WRITE, 0, 0);
2894 TRACE(" -> %i bytes\n", n);
2895 *lpNumberOfBytesSent = n;
2897 HeapFree( GetProcessHeap(), 0, iovec );
2898 release_sock_fd( s, fd );
2902 HeapFree( GetProcessHeap(), 0, iovec );
2905 release_sock_fd( s, fd );
2908 WARN(" -> ERROR %d\n", err);
2909 WSASetLastError(err);
2910 return SOCKET_ERROR;
2913 /***********************************************************************
2914 * sendto (WS2_32.20)
2916 int WINAPI WS_sendto(SOCKET s, const char *buf, int len, int flags,
2917 const struct WS_sockaddr *to, int tolen)
2923 wsabuf.buf = (char*) buf;
2925 if ( WSASendTo(s, &wsabuf, 1, &n, flags, to, tolen, NULL, NULL) == SOCKET_ERROR )
2926 return SOCKET_ERROR;
2931 /***********************************************************************
2932 * setsockopt (WS2_32.21)
2934 int WINAPI WS_setsockopt(SOCKET s, int level, int optname,
2935 const char *optval, int optlen)
2939 struct linger linger;
2940 struct timeval tval;
2942 TRACE("socket: %04x, level 0x%x, name 0x%x, ptr %p, len %d\n",
2943 s, level, optname, optval, optlen);
2950 /* Some options need some conversion before they can be sent to
2951 * setsockopt. The conversions are done here, then they will fall though
2952 * to the general case. Special options that are not passed to
2953 * setsockopt follow below that.*/
2955 case WS_SO_DONTLINGER:
2956 linger.l_onoff = *((const int*)optval) ? 0: 1;
2957 linger.l_linger = 0;
2959 optname = SO_LINGER;
2960 optval = (char*)&linger;
2961 optlen = sizeof(struct linger);
2965 linger.l_onoff = ((LINGER*)optval)->l_onoff;
2966 linger.l_linger = ((LINGER*)optval)->l_linger;
2967 /* FIXME: what is documented behavior if SO_LINGER optval
2970 optname = SO_LINGER;
2971 optval = (char*)&linger;
2972 optlen = sizeof(struct linger);
2976 if (*(const int*)optval < 2048)
2978 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval );
2983 /* The options listed here don't need any special handling. Thanks to
2984 * the conversion happening above, options from there will fall through
2986 case WS_SO_ACCEPTCONN:
2987 case WS_SO_BROADCAST:
2989 case WS_SO_KEEPALIVE:
2990 case WS_SO_OOBINLINE:
2993 convert_sockopt(&level, &optname);
2996 /* SO_DEBUG is a privileged operation, ignore it. */
2998 TRACE("Ignoring SO_DEBUG\n");
3001 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
3002 * socket. According to MSDN, this option is silently ignored.*/
3003 case WS_SO_DONTROUTE:
3004 TRACE("Ignoring SO_DONTROUTE\n");
3007 /* Stops two sockets from being bound to the same port. Always happens
3008 * on unix systems, so just drop it. */
3009 case WS_SO_EXCLUSIVEADDRUSE:
3010 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
3013 /* SO_OPENTYPE does not require a valid socket handle. */
3014 case WS_SO_OPENTYPE:
3015 if (!optlen || optlen < sizeof(int) || !optval)
3017 SetLastError(WSAEFAULT);
3018 return SOCKET_ERROR;
3020 get_per_thread_data()->opentype = *(const int *)optval;
3021 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
3024 /* SO_REUSEADDR allows two applications to bind to the same port at at
3025 * same time. There is no direct way to do that in unix. While Wineserver
3026 * might do this, it does not seem useful for now, so just ignore it.*/
3027 case WS_SO_REUSEADDR:
3028 TRACE("Ignoring SO_REUSEADDR, does not translate\n");
3032 case WS_SO_RCVTIMEO:
3035 case WS_SO_SNDTIMEO:
3037 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
3038 if (optval && optlen == sizeof(UINT32)) {
3039 /* WinSock passes miliseconds instead of struct timeval */
3040 tval.tv_usec = (*(const UINT32*)optval % 1000) * 1000;
3041 tval.tv_sec = *(const UINT32*)optval / 1000;
3042 /* min of 500 milisec */
3043 if (tval.tv_sec == 0 && tval.tv_usec < 500000)
3044 tval.tv_usec = 500000;
3045 optlen = sizeof(struct timeval);
3046 optval = (char*)&tval;
3047 } else if (optlen == sizeof(struct timeval)) {
3048 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen);
3050 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen);
3053 convert_sockopt(&level, &optname);
3058 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname);
3059 SetLastError(WSAENOPROTOOPT);
3060 return SOCKET_ERROR;
3062 break; /* case WS_SOL_SOCKET */
3069 fd = get_sock_fd( s, 0, NULL );
3070 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval, fd);
3072 /* We try to set the ipx type on ipx socket level. */
3074 if(setsockopt(fd, SOL_IPX, IPX_TYPE, optval, optlen) == -1)
3076 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
3077 release_sock_fd( s, fd );
3078 return SOCKET_ERROR;
3083 /* Should we retrieve val using a getsockopt call and then
3084 * set the modified one? */
3085 val.ipx_pt = *optval;
3086 setsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, sizeof(struct ipx));
3089 release_sock_fd( s, fd );
3092 case IPX_FILTERPTYPE:
3093 /* Sets the receive filter packet type, at the moment we don't support it */
3094 FIXME("IPX_FILTERPTYPE: %x\n", *optval);
3095 /* Returning 0 is better for now than returning a SOCKET_ERROR */
3099 FIXME("opt_name:%x\n", optname);
3100 return SOCKET_ERROR;
3102 break; /* case NSPROTO_IPX */
3105 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
3106 case WS_IPPROTO_TCP:
3109 case WS_TCP_NODELAY:
3110 convert_sockopt(&level, &optname);
3113 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname);
3114 return SOCKET_ERROR;
3121 case WS_IP_ADD_MEMBERSHIP:
3122 case WS_IP_DROP_MEMBERSHIP:
3126 case WS_IP_MULTICAST_IF:
3127 case WS_IP_MULTICAST_LOOP:
3128 case WS_IP_MULTICAST_TTL:
3132 convert_sockopt(&level, &optname);
3135 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname);
3136 return SOCKET_ERROR;
3141 FIXME("Unknown level: 0x%08x\n", level);
3142 return SOCKET_ERROR;
3143 } /* end switch(level) */
3145 /* avoid endianness issues if argument is a 16-bit int */
3146 if (optval && optlen < sizeof(int))
3148 woptval= *((const INT16 *) optval);
3149 optval= (char*) &woptval;
3152 fd = get_sock_fd( s, 0, NULL );
3153 if (fd == -1) return SOCKET_ERROR;
3155 if (setsockopt(fd, level, optname, optval, optlen) == 0)
3157 release_sock_fd( s, fd );
3160 TRACE("Setting socket error, %d\n", wsaErrno());
3161 SetLastError(wsaErrno());
3162 release_sock_fd( s, fd );
3164 return SOCKET_ERROR;
3167 /***********************************************************************
3168 * shutdown (WS2_32.22)
3170 int WINAPI WS_shutdown(SOCKET s, int how)
3172 int fd, flags, err = WSAENOTSOCK;
3173 unsigned int clear_flags = 0;
3175 fd = get_sock_fd( s, 0, &flags );
3176 TRACE("socket %04x, how %i %x\n", s, how, flags );
3179 return SOCKET_ERROR;
3183 case 0: /* drop receives */
3184 clear_flags |= FD_READ;
3186 case 1: /* drop sends */
3187 clear_flags |= FD_WRITE;
3189 case 2: /* drop all */
3190 clear_flags |= FD_READ|FD_WRITE;
3192 clear_flags |= FD_WINE_LISTENING;
3195 if ( flags & FD_FLAG_OVERLAPPED ) {
3200 err = WS2_register_async_shutdown( s, ws2m_sd_read );
3203 err = WS2_register_async_shutdown( s, ws2m_sd_write );
3207 err = WS2_register_async_shutdown( s, ws2m_sd_read );
3208 if (!err) err = WS2_register_async_shutdown( s, ws2m_sd_write );
3211 if (err) goto error;
3213 else /* non-overlapped mode */
3215 if ( shutdown( fd, how ) )
3222 release_sock_fd( s, fd );
3223 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
3224 if ( how > 1) WSAAsyncSelect( s, 0, 0, 0 );
3228 release_sock_fd( s, fd );
3229 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
3230 WSASetLastError( err );
3231 return SOCKET_ERROR;
3234 /***********************************************************************
3235 * socket (WS2_32.23)
3237 SOCKET WINAPI WS_socket(int af, int type, int protocol)
3239 TRACE("af=%d type=%d protocol=%d\n", af, type, protocol);
3241 return WSASocketA( af, type, protocol, NULL, 0,
3242 get_per_thread_data()->opentype ? 0 : WSA_FLAG_OVERLAPPED );
3246 /***********************************************************************
3247 * gethostbyaddr (WS2_32.51)
3249 struct WS_hostent* WINAPI WS_gethostbyaddr(const char *addr, int len, int type)
3251 struct WS_hostent *retval = NULL;
3252 struct hostent* host;
3254 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3257 struct hostent hostentry;
3260 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
3262 int res = gethostbyaddr_r(addr, len, type,
3263 &hostentry, extrabuf, ebufsize, &host, &locerr);
3264 if( res != ERANGE) break;
3266 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
3268 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
3270 EnterCriticalSection( &csWSgetXXXbyYYY );
3271 host = gethostbyaddr(addr, len, type);
3272 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
3274 if( host != NULL ) retval = WS_dup_he(host);
3275 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3276 HeapFree(GetProcessHeap(),0,extrabuf);
3278 LeaveCriticalSection( &csWSgetXXXbyYYY );
3280 TRACE("ptr %p, len %d, type %d ret %p\n", addr, len, type, retval);
3284 /***********************************************************************
3285 * gethostbyname (WS2_32.52)
3287 struct WS_hostent* WINAPI WS_gethostbyname(const char* name)
3289 struct WS_hostent *retval = NULL;
3290 struct hostent* host;
3291 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3294 struct hostent hostentry;
3295 int locerr = ENOBUFS;
3300 if( gethostname( buf, 100) == -1) {
3301 SetLastError( WSAENOBUFS); /* appropriate ? */
3305 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3307 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
3309 int res = gethostbyname_r(name, &hostentry, extrabuf, ebufsize, &host, &locerr);
3310 if( res != ERANGE) break;
3312 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
3314 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
3316 EnterCriticalSection( &csWSgetXXXbyYYY );
3317 host = gethostbyname(name);
3318 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
3320 if (host) retval = WS_dup_he(host);
3321 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3322 HeapFree(GetProcessHeap(),0,extrabuf);
3324 LeaveCriticalSection( &csWSgetXXXbyYYY );
3326 TRACE( "%s ret %p\n", debugstr_a(name), retval );
3331 /***********************************************************************
3332 * getprotobyname (WS2_32.53)
3334 struct WS_protoent* WINAPI WS_getprotobyname(const char* name)
3336 struct WS_protoent* retval = NULL;
3337 #ifdef HAVE_GETPROTOBYNAME
3338 struct protoent* proto;
3339 EnterCriticalSection( &csWSgetXXXbyYYY );
3340 if( (proto = getprotobyname(name)) != NULL )
3342 retval = WS_dup_pe(proto);
3345 MESSAGE("protocol %s not found; You might want to add "
3346 "this to /etc/protocols\n", debugstr_a(name) );
3347 SetLastError(WSANO_DATA);
3349 LeaveCriticalSection( &csWSgetXXXbyYYY );
3351 TRACE( "%s ret %p\n", debugstr_a(name), retval );
3356 /***********************************************************************
3357 * getprotobynumber (WS2_32.54)
3359 struct WS_protoent* WINAPI WS_getprotobynumber(int number)
3361 struct WS_protoent* retval = NULL;
3362 #ifdef HAVE_GETPROTOBYNUMBER
3363 struct protoent* proto;
3364 EnterCriticalSection( &csWSgetXXXbyYYY );
3365 if( (proto = getprotobynumber(number)) != NULL )
3367 retval = WS_dup_pe(proto);
3370 MESSAGE("protocol number %d not found; You might want to add "
3371 "this to /etc/protocols\n", number );
3372 SetLastError(WSANO_DATA);
3374 LeaveCriticalSection( &csWSgetXXXbyYYY );
3376 TRACE("%i ret %p\n", number, retval);
3381 /***********************************************************************
3382 * getservbyname (WS2_32.55)
3384 struct WS_servent* WINAPI WS_getservbyname(const char *name, const char *proto)
3386 struct WS_servent* retval = NULL;
3387 struct servent* serv;
3389 char *proto_str = NULL;
3391 if (!(name_str = strdup_lower(name))) return NULL;
3393 if (proto && *proto)
3395 if (!(proto_str = strdup_lower(proto)))
3397 HeapFree( GetProcessHeap(), 0, name_str );
3402 EnterCriticalSection( &csWSgetXXXbyYYY );
3403 serv = getservbyname(name_str, proto_str);
3406 retval = WS_dup_se(serv);
3408 else SetLastError(WSANO_DATA);
3409 LeaveCriticalSection( &csWSgetXXXbyYYY );
3410 HeapFree( GetProcessHeap(), 0, proto_str );
3411 HeapFree( GetProcessHeap(), 0, name_str );
3412 TRACE( "%s, %s ret %p\n", debugstr_a(name), debugstr_a(proto), retval );
3416 /***********************************************************************
3417 * freeaddrinfo (WS2_32.@)
3419 void WINAPI WS_freeaddrinfo(struct WS_addrinfo *res)
3422 struct WS_addrinfo *next;
3424 HeapFree(GetProcessHeap(),0,res->ai_canonname);
3425 HeapFree(GetProcessHeap(),0,res->ai_addr);
3426 next = res->ai_next;
3427 HeapFree(GetProcessHeap(),0,res);
3432 /* helper functions for getaddrinfo() */
3433 static int convert_aiflag_w2u(int winflags) {
3434 int i, unixflags = 0;
3436 for (i=0;i<sizeof(ws_aiflag_map)/sizeof(ws_aiflag_map[0]);i++)
3437 if (ws_aiflag_map[i][0] & winflags) {
3438 unixflags |= ws_aiflag_map[i][1];
3439 winflags &= ~ws_aiflag_map[i][0];
3442 FIXME("Unhandled windows AI_xxx flags %x\n", winflags);
3446 static int convert_aiflag_u2w(int unixflags) {
3447 int i, winflags = 0;
3449 for (i=0;i<sizeof(ws_aiflag_map)/sizeof(ws_aiflag_map[0]);i++)
3450 if (ws_aiflag_map[i][1] & unixflags) {
3451 winflags |= ws_aiflag_map[i][0];
3452 unixflags &= ~ws_aiflag_map[i][1];
3454 if (unixflags) /* will warn usually */
3455 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags);
3459 static int convert_eai_u2w(int unixret) {
3462 for (i=0;ws_eai_map[i][0];i++)
3463 if (ws_eai_map[i][1] == unixret)
3464 return ws_eai_map[i][0];
3468 /***********************************************************************
3469 * getaddrinfo (WS2_32.@)
3471 int WINAPI WS_getaddrinfo(LPCSTR nodename, LPCSTR servname, const struct WS_addrinfo *hints, struct WS_addrinfo **res)
3473 #if HAVE_GETADDRINFO
3474 struct addrinfo *unixaires = NULL;
3476 struct addrinfo unixhints, *punixhints = NULL;
3477 CHAR *node = NULL, *serv = NULL;
3480 if (!(node = strdup_lower(nodename))) return WSA_NOT_ENOUGH_MEMORY;
3483 if (!(serv = strdup_lower(servname))) {
3484 HeapFree(GetProcessHeap(), 0, node);
3485 return WSA_NOT_ENOUGH_MEMORY;
3490 punixhints = &unixhints;
3492 memset(&unixhints, 0, sizeof(unixhints));
3493 punixhints->ai_flags = convert_aiflag_w2u(hints->ai_flags);
3494 if (hints->ai_family == 0) /* wildcard, specific to getaddrinfo() */
3495 punixhints->ai_family = 0;
3497 punixhints->ai_family = convert_af_w2u(hints->ai_family);
3498 if (hints->ai_socktype == 0) /* wildcard, specific to getaddrinfo() */
3499 punixhints->ai_socktype = 0;
3501 punixhints->ai_socktype = convert_socktype_w2u(hints->ai_socktype);
3502 if (hints->ai_protocol == 0) /* wildcard, specific to getaddrinfo() */
3503 punixhints->ai_protocol = 0;
3505 punixhints->ai_protocol = convert_proto_w2u(hints->ai_protocol);
3508 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
3509 result = getaddrinfo(nodename, servname, punixhints, &unixaires);
3511 TRACE("%s, %s %p -> %p %d\n", nodename, servname, hints, res, result);
3513 HeapFree(GetProcessHeap(), 0, node);
3514 HeapFree(GetProcessHeap(), 0, serv);
3517 struct addrinfo *xuai = unixaires;
3518 struct WS_addrinfo **xai = res;
3522 struct WS_addrinfo *ai = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, sizeof(struct WS_addrinfo));
3528 *xai = ai;xai = &ai->ai_next;
3529 ai->ai_flags = convert_aiflag_u2w(xuai->ai_flags);
3530 ai->ai_family = convert_af_u2w(xuai->ai_family);
3531 ai->ai_socktype = convert_socktype_u2w(xuai->ai_socktype);
3532 ai->ai_protocol = convert_proto_u2w(xuai->ai_protocol);
3533 if (xuai->ai_canonname) {
3534 TRACE("canon name - %s\n",debugstr_a(xuai->ai_canonname));
3535 ai->ai_canonname = HeapAlloc(GetProcessHeap(),0,strlen(xuai->ai_canonname)+1);
3536 if (!ai->ai_canonname)
3538 strcpy(ai->ai_canonname,xuai->ai_canonname);
3540 len = xuai->ai_addrlen;
3541 ai->ai_addr = HeapAlloc(GetProcessHeap(),0,len);
3544 ai->ai_addrlen = len;
3546 int winlen = ai->ai_addrlen;
3548 if (!ws_sockaddr_u2ws(xuai->ai_addr, xuai->ai_addrlen, ai->ai_addr, &winlen)) {
3549 ai->ai_addrlen = winlen;
3553 ai->ai_addr = HeapReAlloc(GetProcessHeap(),0,ai->ai_addr,len);
3556 ai->ai_addrlen = len;
3558 xuai = xuai->ai_next;
3560 freeaddrinfo(unixaires);
3562 result = convert_eai_u2w(result);
3567 if (*res) WS_freeaddrinfo(*res);
3568 if (unixaires) freeaddrinfo(unixaires);
3570 return WSA_NOT_ENOUGH_MEMORY;
3572 FIXME("getaddrinfo() failed, not found during buildtime.\n");
3577 /***********************************************************************
3578 * GetAddrInfoW (WS2_32.@)
3580 int WINAPI GetAddrInfoW(LPCWSTR nodename, LPCWSTR servname, const ADDRINFOW *hints, PADDRINFOW *res)
3582 FIXME("empty stub!\n");
3586 int WINAPI WS_getnameinfo(const SOCKADDR *sa, WS_socklen_t salen, PCHAR host,
3587 DWORD hostlen, PCHAR serv, DWORD servlen, INT flags)
3589 #if HAVE_GETNAMEINFO
3591 const struct sockaddr* sa_u;
3594 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa), salen, host, hostlen,
3595 serv, servlen, flags);
3597 sa_u = ws_sockaddr_ws2u(sa, salen, &size);
3600 WSASetLastError(WSAEFAULT);
3601 return WSA_NOT_ENOUGH_MEMORY;
3603 ret = getnameinfo(sa_u, size, host, hostlen, serv, servlen, convert_aiflag_w2u(flags));
3605 ws_sockaddr_free(sa_u, sa);
3606 return convert_eai_u2w(ret);
3608 FIXME("getnameinfo() failed, not found during buildtime.\n");
3613 /***********************************************************************
3614 * getservbyport (WS2_32.56)
3616 struct WS_servent* WINAPI WS_getservbyport(int port, const char *proto)
3618 struct WS_servent* retval = NULL;
3619 #ifdef HAVE_GETSERVBYPORT
3620 struct servent* serv;
3621 char *proto_str = NULL;
3623 if (proto && *proto)
3625 if (!(proto_str = strdup_lower(proto))) return NULL;
3627 EnterCriticalSection( &csWSgetXXXbyYYY );
3628 if( (serv = getservbyport(port, proto_str)) != NULL ) {
3629 retval = WS_dup_se(serv);
3631 else SetLastError(WSANO_DATA);
3632 LeaveCriticalSection( &csWSgetXXXbyYYY );
3633 HeapFree( GetProcessHeap(), 0, proto_str );
3635 TRACE("%d (i.e. port %d), %s ret %p\n", port, (int)ntohl(port), debugstr_a(proto), retval);
3640 /***********************************************************************
3641 * gethostname (WS2_32.57)
3643 int WINAPI WS_gethostname(char *name, int namelen)
3645 TRACE("name %p, len %d\n", name, namelen);
3647 if (gethostname(name, namelen) == 0)
3649 TRACE("<- '%s'\n", name);
3652 SetLastError((errno == EINVAL) ? WSAEFAULT : wsaErrno());
3653 TRACE("<- ERROR !\n");
3654 return SOCKET_ERROR;
3658 /* ------------------------------------- Windows sockets extensions -- *
3660 * ------------------------------------------------------------------- */
3662 /***********************************************************************
3663 * WSAEnumNetworkEvents (WS2_32.36)
3665 int WINAPI WSAEnumNetworkEvents(SOCKET s, WSAEVENT hEvent, LPWSANETWORKEVENTS lpEvent)
3669 TRACE("%08x, hEvent %p, lpEvent %p\n", s, hEvent, lpEvent );
3671 SERVER_START_REQ( get_socket_event )
3673 req->handle = SOCKET2HANDLE(s);
3674 req->service = TRUE;
3675 req->c_event = hEvent;
3676 wine_server_set_reply( req, lpEvent->iErrorCode, sizeof(lpEvent->iErrorCode) );
3677 if (!(ret = wine_server_call(req))) lpEvent->lNetworkEvents = reply->pmask & reply->mask;
3681 SetLastError(WSAEINVAL);
3682 return SOCKET_ERROR;
3685 /***********************************************************************
3686 * WSAEventSelect (WS2_32.39)
3688 int WINAPI WSAEventSelect(SOCKET s, WSAEVENT hEvent, long lEvent)
3692 TRACE("%08x, hEvent %p, event %08x\n", s, hEvent, (unsigned)lEvent );
3694 SERVER_START_REQ( set_socket_event )
3696 req->handle = SOCKET2HANDLE(s);
3698 req->event = hEvent;
3701 ret = wine_server_call( req );
3705 SetLastError(WSAEINVAL);
3706 return SOCKET_ERROR;
3709 /**********************************************************************
3710 * WSAGetOverlappedResult (WS2_32.40)
3712 BOOL WINAPI WSAGetOverlappedResult( SOCKET s, LPWSAOVERLAPPED lpOverlapped,
3713 LPDWORD lpcbTransfer, BOOL fWait,
3718 TRACE( "socket %04x ovl %p trans %p, wait %d flags %p\n",
3719 s, lpOverlapped, lpcbTransfer, fWait, lpdwFlags );
3721 if ( lpOverlapped == NULL )
3723 ERR( "Invalid pointer\n" );
3724 WSASetLastError(WSA_INVALID_PARAMETER);
3730 if (lpOverlapped->hEvent)
3731 while ( WaitForSingleObjectEx(lpOverlapped->hEvent,
3732 INFINITE, TRUE) == STATUS_USER_APC );
3733 else /* busy loop */
3734 while ( ((volatile OVERLAPPED*)lpOverlapped)->Internal == STATUS_PENDING )
3738 else if ( lpOverlapped->Internal == STATUS_PENDING )
3740 /* Wait in order to give APCs a chance to run. */
3741 /* This is cheating, so we must set the event again in case of success -
3742 it may be a non-manual reset event. */
3743 while ( (r = WaitForSingleObjectEx(lpOverlapped->hEvent, 0, TRUE)) == STATUS_USER_APC );
3744 if ( r == WAIT_OBJECT_0 && lpOverlapped->hEvent )
3745 NtSetEvent( lpOverlapped->hEvent, NULL );
3749 *lpcbTransfer = lpOverlapped->InternalHigh;
3752 *lpdwFlags = lpOverlapped->u.s.Offset;
3754 switch ( lpOverlapped->Internal )
3756 case STATUS_SUCCESS:
3758 case STATUS_PENDING:
3759 WSASetLastError( WSA_IO_INCOMPLETE );
3760 if (fWait) ERR("PENDING status after waiting!\n");
3763 WSASetLastError( NtStatusToWSAError( lpOverlapped->Internal ));
3769 /***********************************************************************
3770 * WSAAsyncSelect (WS2_32.101)
3772 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, long lEvent)
3776 TRACE("%x, hWnd %p, uMsg %08x, event %08lx\n", s, hWnd, uMsg, lEvent );
3778 SERVER_START_REQ( set_socket_event )
3780 req->handle = SOCKET2HANDLE(s);
3785 ret = wine_server_call( req );
3789 SetLastError(WSAEINVAL);
3790 return SOCKET_ERROR;
3793 /***********************************************************************
3794 * WSACreateEvent (WS2_32.31)
3797 WSAEVENT WINAPI WSACreateEvent(void)
3799 /* Create a manual-reset event, with initial state: unsignaled */
3802 return CreateEventW(NULL, TRUE, FALSE, NULL);
3805 /***********************************************************************
3806 * WSACloseEvent (WS2_32.29)
3809 BOOL WINAPI WSACloseEvent(WSAEVENT event)
3811 TRACE ("event=%p\n", event);
3813 return CloseHandle(event);
3816 /***********************************************************************
3817 * WSASocketA (WS2_32.78)
3820 SOCKET WINAPI WSASocketA(int af, int type, int protocol,
3821 LPWSAPROTOCOL_INFOA lpProtocolInfo,
3822 GROUP g, DWORD dwFlags)
3825 WSAPROTOCOL_INFOW info;
3827 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3828 af, type, protocol, lpProtocolInfo, g, dwFlags);
3830 if (!lpProtocolInfo) return WSASocketW(af, type, protocol, NULL, g, dwFlags);
3832 memcpy(&info, lpProtocolInfo, FIELD_OFFSET(WSAPROTOCOL_INFOW, szProtocol));
3833 len = MultiByteToWideChar(CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
3834 info.szProtocol, WSAPROTOCOL_LEN + 1);
3838 WSASetLastError( WSAEINVAL);
3839 return SOCKET_ERROR;
3842 return WSASocketW(af, type, protocol, &info, g, dwFlags);
3845 /***********************************************************************
3846 * WSASocketW (WS2_32.79)
3849 SOCKET WINAPI WSASocketW(int af, int type, int protocol,
3850 LPWSAPROTOCOL_INFOW lpProtocolInfo,
3851 GROUP g, DWORD dwFlags)
3856 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
3857 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
3860 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3861 af, type, protocol, lpProtocolInfo, g, dwFlags );
3863 /* hack for WSADuplicateSocket */
3864 if (lpProtocolInfo && lpProtocolInfo->dwServiceFlags4 == 0xff00ff00) {
3865 ret = lpProtocolInfo->dwCatalogEntryId;
3866 TRACE("\tgot duplicate %04x\n", ret);
3870 /* check and convert the socket family */
3871 af = convert_af_w2u(af);
3874 FIXME("Unsupported socket family %d!\n", af);
3875 SetLastError(WSAEAFNOSUPPORT);
3876 return INVALID_SOCKET;
3879 /* check the socket type */
3880 type = convert_socktype_w2u(type);
3883 SetLastError(WSAESOCKTNOSUPPORT);
3884 return INVALID_SOCKET;
3887 /* check the protocol type */
3888 if ( protocol < 0 ) /* don't support negative values */
3890 SetLastError(WSAEPROTONOSUPPORT);
3891 return INVALID_SOCKET;
3894 if ( af == AF_UNSPEC) /* did they not specify the address family? */
3898 if (type == SOCK_STREAM) { af = AF_INET; break; }
3900 if (type == SOCK_DGRAM) { af = AF_INET; break; }
3901 default: SetLastError(WSAEPROTOTYPE); return INVALID_SOCKET;
3904 SERVER_START_REQ( create_socket )
3908 req->protocol = protocol;
3909 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
3910 req->attributes = OBJ_INHERIT;
3911 req->flags = dwFlags;
3912 set_error( wine_server_call( req ) );
3913 ret = HANDLE2SOCKET( reply->handle );
3918 TRACE("\tcreated %04x\n", ret );
3922 if (GetLastError() == WSAEACCES) /* raw socket denied */
3924 if (type == SOCK_RAW)
3925 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, will fail unless running as root\n");
3927 MESSAGE("WS_SOCKET: not enough privileges to create socket, try running as root\n");
3928 SetLastError(WSAESOCKTNOSUPPORT);
3931 WARN("\t\tfailed!\n");
3932 return INVALID_SOCKET;
3935 /***********************************************************************
3936 * WSAJoinLeaf (WS2_32.58)
3939 SOCKET WINAPI WSAJoinLeaf(
3941 const struct WS_sockaddr *addr,
3943 LPWSABUF lpCallerData,
3944 LPWSABUF lpCalleeData,
3950 return INVALID_SOCKET;
3953 /***********************************************************************
3954 * __WSAFDIsSet (WS2_32.151)
3956 int WINAPI __WSAFDIsSet(SOCKET s, WS_fd_set *set)
3958 int i = set->fd_count;
3960 TRACE("(%d,%p(%i))\n", s, set, i);
3963 if (set->fd_array[i] == s) return 1;
3967 /***********************************************************************
3968 * WSAIsBlocking (WINSOCK.114)
3969 * WSAIsBlocking (WS2_32.114)
3971 BOOL WINAPI WSAIsBlocking(void)
3973 /* By default WinSock should set all its sockets to non-blocking mode
3974 * and poll in PeekMessage loop when processing "blocking" ones. This
3975 * function is supposed to tell if the program is in this loop. Our
3976 * blocking calls are truly blocking so we always return FALSE.
3978 * Note: It is allowed to call this function without prior WSAStartup().
3985 /***********************************************************************
3986 * WSACancelBlockingCall (WINSOCK.113)
3987 * WSACancelBlockingCall (WS2_32.113)
3989 INT WINAPI WSACancelBlockingCall(void)
3995 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x )
3997 FIXME("How was this called?\n");
4002 /***********************************************************************
4003 * WSASetBlockingHook (WS2_32.109)
4005 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
4007 FARPROC prev = blocking_hook;
4008 blocking_hook = lpBlockFunc;
4009 TRACE("hook %p\n", lpBlockFunc);
4014 /***********************************************************************
4015 * WSAUnhookBlockingHook (WS2_32.110)
4017 INT WINAPI WSAUnhookBlockingHook(void)
4019 blocking_hook = WSA_DefaultBlockingHook;
4024 /* ----------------------------------- end of API stuff */
4026 /* ----------------------------------- helper functions -
4028 * TODO: Merge WS_dup_..() stuff into one function that
4029 * would operate with a generic structure containing internal
4030 * pointers (via a template of some kind).
4033 static int list_size(char** l, int item_size)
4038 j += (item_size) ? item_size : strlen(l[i]) + 1;
4039 j += (i + 1) * sizeof(char*); }
4043 static int list_dup(char** l_src, char** l_to, int item_size)
4048 for (i = 0; l_src[i]; i++) ;
4049 p = (char *)(l_to + i + 1);
4050 for (i = 0; l_src[i]; i++)
4052 int count = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
4053 memcpy(p, l_src[i], count);
4058 return p - (char *)l_to;
4063 /* duplicate hostent entry
4064 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
4065 * Dito for protoent and servent.
4067 static struct WS_hostent *WS_dup_he(const struct hostent* p_he)
4070 struct WS_hostent *p_to;
4072 int size = (sizeof(*p_he) +
4073 strlen(p_he->h_name) + 1 +
4074 list_size(p_he->h_aliases, 0) +
4075 list_size(p_he->h_addr_list, p_he->h_length));
4077 if (!(p_to = check_buffer_he(size))) return NULL;
4078 p_to->h_addrtype = p_he->h_addrtype;
4079 p_to->h_length = p_he->h_length;
4081 p = (char *)(p_to + 1);
4083 strcpy(p, p_he->h_name);
4086 p_to->h_aliases = (char **)p;
4087 p += list_dup(p_he->h_aliases, p_to->h_aliases, 0);
4089 p_to->h_addr_list = (char **)p;
4090 list_dup(p_he->h_addr_list, p_to->h_addr_list, p_he->h_length);
4094 /* ----- protoent */
4096 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe)
4099 struct WS_protoent *p_to;
4101 int size = (sizeof(*p_pe) +
4102 strlen(p_pe->p_name) + 1 +
4103 list_size(p_pe->p_aliases, 0));
4105 if (!(p_to = check_buffer_pe(size))) return NULL;
4106 p_to->p_proto = p_pe->p_proto;
4108 p = (char *)(p_to + 1);
4110 strcpy(p, p_pe->p_name);
4113 p_to->p_aliases = (char **)p;
4114 list_dup(p_pe->p_aliases, p_to->p_aliases, 0);
4120 static struct WS_servent *WS_dup_se(const struct servent* p_se)
4123 struct WS_servent *p_to;
4125 int size = (sizeof(*p_se) +
4126 strlen(p_se->s_proto) + 1 +
4127 strlen(p_se->s_name) + 1 +
4128 list_size(p_se->s_aliases, 0));
4130 if (!(p_to = check_buffer_se(size))) return NULL;
4131 p_to->s_port = p_se->s_port;
4133 p = (char *)(p_to + 1);
4135 strcpy(p, p_se->s_name);
4139 strcpy(p, p_se->s_proto);
4142 p_to->s_aliases = (char **)p;
4143 list_dup(p_se->s_aliases, p_to->s_aliases, 0);
4147 /* ----------------------------------- error handling */
4151 int loc_errno = errno;
4152 WARN("errno %d, (%s).\n", loc_errno, strerror(loc_errno));
4156 case EINTR: return WSAEINTR;
4157 case EBADF: return WSAEBADF;
4159 case EACCES: return WSAEACCES;
4160 case EFAULT: return WSAEFAULT;
4161 case EINVAL: return WSAEINVAL;
4162 case EMFILE: return WSAEMFILE;
4163 case EWOULDBLOCK: return WSAEWOULDBLOCK;
4164 case EINPROGRESS: return WSAEINPROGRESS;
4165 case EALREADY: return WSAEALREADY;
4166 case ENOTSOCK: return WSAENOTSOCK;
4167 case EDESTADDRREQ: return WSAEDESTADDRREQ;
4168 case EMSGSIZE: return WSAEMSGSIZE;
4169 case EPROTOTYPE: return WSAEPROTOTYPE;
4170 case ENOPROTOOPT: return WSAENOPROTOOPT;
4171 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
4172 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
4173 case EOPNOTSUPP: return WSAEOPNOTSUPP;
4174 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
4175 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
4176 case EADDRINUSE: return WSAEADDRINUSE;
4177 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
4178 case ENETDOWN: return WSAENETDOWN;
4179 case ENETUNREACH: return WSAENETUNREACH;
4180 case ENETRESET: return WSAENETRESET;
4181 case ECONNABORTED: return WSAECONNABORTED;
4183 case ECONNRESET: return WSAECONNRESET;
4184 case ENOBUFS: return WSAENOBUFS;
4185 case EISCONN: return WSAEISCONN;
4186 case ENOTCONN: return WSAENOTCONN;
4187 case ESHUTDOWN: return WSAESHUTDOWN;
4188 case ETOOMANYREFS: return WSAETOOMANYREFS;
4189 case ETIMEDOUT: return WSAETIMEDOUT;
4190 case ECONNREFUSED: return WSAECONNREFUSED;
4191 case ELOOP: return WSAELOOP;
4192 case ENAMETOOLONG: return WSAENAMETOOLONG;
4193 case EHOSTDOWN: return WSAEHOSTDOWN;
4194 case EHOSTUNREACH: return WSAEHOSTUNREACH;
4195 case ENOTEMPTY: return WSAENOTEMPTY;
4197 case EPROCLIM: return WSAEPROCLIM;
4200 case EUSERS: return WSAEUSERS;
4203 case EDQUOT: return WSAEDQUOT;
4206 case ESTALE: return WSAESTALE;
4209 case EREMOTE: return WSAEREMOTE;
4212 /* just in case we ever get here and there are no problems */
4215 WARN("Unknown errno %d!\n", loc_errno);
4216 return WSAEOPNOTSUPP;
4220 UINT wsaHerrno(int loc_errno)
4223 WARN("h_errno %d.\n", loc_errno);
4227 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
4228 case TRY_AGAIN: return WSATRY_AGAIN;
4229 case NO_RECOVERY: return WSANO_RECOVERY;
4230 case NO_DATA: return WSANO_DATA;
4231 case ENOBUFS: return WSAENOBUFS;
4235 WARN("Unknown h_errno %d!\n", loc_errno);
4236 return WSAEOPNOTSUPP;
4241 /***********************************************************************
4242 * WSARecv (WS2_32.67)
4244 int WINAPI WSARecv(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
4245 LPDWORD NumberOfBytesReceived, LPDWORD lpFlags,
4246 LPWSAOVERLAPPED lpOverlapped,
4247 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
4249 return WSARecvFrom(s, lpBuffers, dwBufferCount, NumberOfBytesReceived, lpFlags,
4250 NULL, NULL, lpOverlapped, lpCompletionRoutine);
4253 /***********************************************************************
4254 * WSARecvFrom (WS2_32.69)
4256 INT WINAPI WSARecvFrom( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
4257 LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, struct WS_sockaddr *lpFrom,
4258 LPINT lpFromlen, LPWSAOVERLAPPED lpOverlapped,
4259 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
4263 int n, fd, err = WSAENOTSOCK, flags, ret;
4264 struct iovec* iovec;
4265 struct ws2_async *wsa;
4266 IO_STATUS_BLOCK* iosb;
4268 TRACE("socket %04x, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %ld, ovl %p, func %p\n",
4269 s, lpBuffers, dwBufferCount, *lpFlags, lpFrom,
4270 (lpFromlen ? *lpFromlen : -1L),
4271 lpOverlapped, lpCompletionRoutine);
4273 fd = get_sock_fd( s, FILE_READ_DATA, &flags );
4274 TRACE( "fd=%d, flags=%x\n", fd, flags );
4276 if (fd == -1) return SOCKET_ERROR;
4278 if (flags & FD_FLAG_RECV_SHUTDOWN)
4280 WSASetLastError( WSAESHUTDOWN );
4284 iovec = HeapAlloc( GetProcessHeap(), 0, dwBufferCount * sizeof (struct iovec) );
4291 for (i = 0; i < dwBufferCount; i++)
4293 iovec[i].iov_base = lpBuffers[i].buf;
4294 iovec[i].iov_len = lpBuffers[i].len;
4297 if ( (lpOverlapped || lpCompletionRoutine) && flags & FD_FLAG_OVERLAPPED )
4299 wsa = WS2_make_async( s, ws2m_read, iovec, dwBufferCount,
4300 lpFlags, lpFrom, lpFromlen,
4301 lpOverlapped, lpCompletionRoutine, &iosb );
4309 if ( ( ret = ws2_queue_async( wsa, iosb )) )
4311 err = NtStatusToWSAError( ret );
4313 if ( !lpOverlapped )
4314 HeapFree( GetProcessHeap(), 0, iosb );
4315 HeapFree( GetProcessHeap(), 0, wsa );
4318 release_sock_fd( s, fd );
4320 /* Try immediate completion */
4323 if ( WSAGetOverlappedResult( s, lpOverlapped,
4324 lpNumberOfBytesRecvd, FALSE, lpFlags) )
4327 if ( (err = WSAGetLastError()) != WSA_IO_INCOMPLETE )
4331 WSASetLastError( WSA_IO_PENDING );
4332 return SOCKET_ERROR;
4335 if ( _is_blocking(s) )
4338 /* FIXME: OOB and exceptfds? */
4339 int timeout = GET_RCVTIMEO(fd);
4340 if( !do_block(fd, POLLIN, timeout)) {
4342 /* a timeout is not fatal */
4343 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4348 n = WS2_recv( fd, iovec, dwBufferCount, lpFrom, lpFromlen, lpFlags );
4355 TRACE(" -> %i bytes\n", n);
4356 *lpNumberOfBytesRecvd = n;
4358 HeapFree(GetProcessHeap(), 0, iovec);
4359 release_sock_fd( s, fd );
4360 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
4365 HeapFree(GetProcessHeap(), 0, iovec);
4368 release_sock_fd( s, fd );
4371 WARN(" -> ERROR %d\n", err);
4372 WSASetLastError( err );
4373 return SOCKET_ERROR;
4376 /***********************************************************************
4377 * WSCInstallProvider (WS2_32.88)
4379 INT WINAPI WSCInstallProvider( const LPGUID lpProviderId,
4380 LPCWSTR lpszProviderDllPath,
4381 const LPWSAPROTOCOL_INFOW lpProtocolInfoList,
4382 DWORD dwNumberOfEntries,
4385 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId),
4386 debugstr_w(lpszProviderDllPath), lpProtocolInfoList,
4387 dwNumberOfEntries, lpErrno);
4393 /***********************************************************************
4394 * WSCDeinstallProvider (WS2_32.83)
4396 INT WINAPI WSCDeinstallProvider(LPGUID lpProviderId, LPINT lpErrno)
4398 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId), lpErrno);
4404 /***********************************************************************
4405 * WSAAccept (WS2_32.26)
4407 SOCKET WINAPI WSAAccept( SOCKET s, struct WS_sockaddr *addr, LPINT addrlen,
4408 LPCONDITIONPROC lpfnCondition, DWORD dwCallbackData)
4411 int ret = 0, size = 0;
4412 WSABUF CallerId, CallerData, CalleeId, CalleeData;
4413 /* QOS SQOS, GQOS; */
4416 SOCKADDR src_addr, dst_addr;
4418 TRACE("Socket %04x, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %d\n",
4419 s, addr, addrlen, lpfnCondition, dwCallbackData);
4422 size = sizeof(src_addr);
4423 cs = WS_accept(s, &src_addr, &size);
4425 if (cs == SOCKET_ERROR) return SOCKET_ERROR;
4427 CallerId.buf = (char *)&src_addr;
4428 CallerId.len = sizeof(src_addr);
4430 CallerData.buf = NULL;
4431 CallerData.len = (ULONG)NULL;
4433 WS_getsockname(cs, &dst_addr, &size);
4435 CalleeId.buf = (char *)&dst_addr;
4436 CalleeId.len = sizeof(dst_addr);
4439 ret = (*lpfnCondition)(&CallerId, &CallerData, NULL, NULL,
4440 &CalleeId, &CalleeData, &g, dwCallbackData);
4445 if (addr && addrlen)
4446 addr = memcpy(addr, &src_addr, (*addrlen > size) ? size : *addrlen );
4449 SERVER_START_REQ( set_socket_deferred )
4451 req->handle = SOCKET2HANDLE(s);
4452 req->deferred = SOCKET2HANDLE(cs);
4453 if ( !wine_server_call_err ( req ) )
4455 SetLastError( WSATRY_AGAIN );
4456 WS_closesocket( cs );
4460 return SOCKET_ERROR;
4463 SetLastError(WSAECONNREFUSED);
4464 return SOCKET_ERROR;
4466 FIXME("Unknown return type from Condition function\n");
4467 SetLastError(WSAENOTSOCK);
4468 return SOCKET_ERROR;
4472 /***********************************************************************
4473 * WSADuplicateSocketA (WS2_32.32)
4475 int WINAPI WSADuplicateSocketA( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOA lpProtocolInfo )
4479 TRACE("(%d,%x,%p)\n", s, dwProcessId, lpProtocolInfo);
4480 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
4481 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
4482 /* I don't know what the real Windoze does next, this is a hack */
4483 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
4484 * the target use the global duplicate, or we could copy a reference to us to the structure
4485 * and let the target duplicate it from us, but let's do it as simple as possible */
4486 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
4487 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
4488 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
4489 0, FALSE, DUPLICATE_SAME_ACCESS);
4490 CloseHandle(hProcess);
4491 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
4495 /***********************************************************************
4496 * WSADuplicateSocketW (WS2_32.33)
4498 int WINAPI WSADuplicateSocketW( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOW lpProtocolInfo )
4502 TRACE("(%d,%x,%p)\n", s, dwProcessId, lpProtocolInfo);
4504 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
4505 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
4506 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
4507 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
4508 0, FALSE, DUPLICATE_SAME_ACCESS);
4509 CloseHandle(hProcess);
4510 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
4514 /***********************************************************************
4515 * WSAInstallServiceClassA (WS2_32.48)
4517 int WINAPI WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info)
4519 FIXME("Request to install service %s\n",debugstr_a(info->lpszServiceClassName));
4520 WSASetLastError(WSAEACCES);
4521 return SOCKET_ERROR;
4524 /***********************************************************************
4525 * WSAInstallServiceClassW (WS2_32.49)
4527 int WINAPI WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info)
4529 FIXME("Request to install service %s\n",debugstr_w(info->lpszServiceClassName));
4530 WSASetLastError(WSAEACCES);
4531 return SOCKET_ERROR;
4534 /***********************************************************************
4535 * WSARemoveServiceClass (WS2_32.70)
4537 int WINAPI WSARemoveServiceClass(LPGUID info)
4539 FIXME("Request to remove service %p\n",info);
4540 WSASetLastError(WSATYPE_NOT_FOUND);
4541 return SOCKET_ERROR;
4544 /***********************************************************************
4545 * WSAStringToAddressA (WS2_32.80)
4547 INT WINAPI WSAStringToAddressA(LPSTR AddressString,
4549 LPWSAPROTOCOL_INFOA lpProtocolInfo,
4550 LPSOCKADDR lpAddress,
4551 LPINT lpAddressLength)
4554 struct in_addr inetaddr;
4555 LPSTR workBuffer=NULL,ptrPort;
4557 TRACE( "(%s, %x, %p, %p, %p)\n", AddressString, AddressFamily, lpProtocolInfo,
4558 lpAddress, lpAddressLength );
4560 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
4564 workBuffer = HeapAlloc( GetProcessHeap(), 0, strlen(AddressString)+1 );
4567 strcpy(workBuffer,AddressString);
4568 switch (AddressFamily)
4571 /* caller wants to know the size of the socket buffer */
4572 if (*lpAddressLength < sizeof(SOCKADDR_IN))
4574 *lpAddressLength = sizeof(SOCKADDR_IN);
4579 /* caller wants to translate an AddressString into a SOCKADDR */
4582 memset(lpAddress,0,sizeof(SOCKADDR_IN));
4583 ((LPSOCKADDR_IN)lpAddress)->sin_family = AF_INET;
4584 ptrPort = strchr(workBuffer,':');
4587 ((LPSOCKADDR_IN)lpAddress)->sin_port = (WS_u_short)atoi(ptrPort+1);
4591 ((LPSOCKADDR_IN)lpAddress)->sin_port = 0;
4592 if (inet_aton(workBuffer, &inetaddr) > 0)
4594 ((LPSOCKADDR_IN)lpAddress)->sin_addr.WS_s_addr = inetaddr.s_addr;
4602 FIXME("(%s, %x, %p, %p, %p) - ProtocolInfo not implemented!\n",
4603 AddressString, AddressFamily,
4604 lpProtocolInfo, lpAddress, lpAddressLength);
4608 FIXME("(%s, %x, %p, %p, %p) - AddressFamiliy not implemented!\n",
4609 AddressString, AddressFamily,
4610 lpProtocolInfo, lpAddress, lpAddressLength);
4612 HeapFree( GetProcessHeap(), 0, workBuffer );
4615 res = WSA_NOT_ENOUGH_MEMORY;
4621 WSASetLastError(res);
4622 return SOCKET_ERROR;
4625 /***********************************************************************
4626 * WSAStringToAddressW (WS2_32.81)
4628 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
4629 * If this should be the case, it would be required to map these digits
4630 * to Unicode digits (0-9) using FoldString first.
4632 INT WINAPI WSAStringToAddressW(LPWSTR AddressString,
4634 LPWSAPROTOCOL_INFOW lpProtocolInfo,
4635 LPSOCKADDR lpAddress,
4636 LPINT lpAddressLength)
4639 LPSTR workBuffer=NULL;
4640 WSAPROTOCOL_INFOA infoA;
4641 LPWSAPROTOCOL_INFOA lpProtoInfoA = NULL;
4643 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString), AddressFamily, lpProtocolInfo,
4644 lpAddress, lpAddressLength );
4646 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
4648 /* if ProtocolInfo is available - convert to ANSI variant */
4651 lpProtoInfoA = &infoA;
4652 memcpy( lpProtoInfoA, lpProtocolInfo, FIELD_OFFSET( WSAPROTOCOL_INFOA, szProtocol ) );
4654 if (!WideCharToMultiByte( CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
4655 lpProtoInfoA->szProtocol, WSAPROTOCOL_LEN+1, NULL, NULL ))
4657 WSASetLastError( WSAEINVAL);
4658 return SOCKET_ERROR;
4664 /* Translate AddressString to ANSI code page - assumes that only
4665 standard digits 0-9 are used with this API call */
4666 sBuffer = WideCharToMultiByte( CP_ACP, 0, AddressString, -1, NULL, 0, NULL, NULL );
4667 workBuffer = HeapAlloc( GetProcessHeap(), 0, sBuffer );
4671 WideCharToMultiByte( CP_ACP, 0, AddressString, -1, workBuffer, sBuffer, NULL, NULL );
4672 res = WSAStringToAddressA(workBuffer,AddressFamily,lpProtoInfoA,
4673 lpAddress,lpAddressLength);
4674 HeapFree( GetProcessHeap(), 0, workBuffer );
4678 res = WSA_NOT_ENOUGH_MEMORY;
4683 WSASetLastError(res);
4684 return SOCKET_ERROR;
4687 /***********************************************************************
4688 * WSAAddressToStringA (WS2_32.27)
4690 * See WSAAddressToStringW
4692 INT WINAPI WSAAddressToStringA( LPSOCKADDR sockaddr, DWORD len,
4693 LPWSAPROTOCOL_INFOA info, LPSTR string,
4697 CHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4700 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
4702 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
4703 if (!string || !lenstr) return SOCKET_ERROR;
4705 /* sin_family is guaranteed to be the first u_short */
4706 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
4708 sprintf( buffer, "%u.%u.%u.%u:%u",
4709 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
4710 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
4711 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
4712 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
4713 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
4715 p = strchr( buffer, ':' );
4716 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
4718 size = strlen( buffer );
4723 WSASetLastError(WSAEFAULT);
4724 return SOCKET_ERROR;
4727 strcpy( string, buffer );
4731 /***********************************************************************
4732 * WSAAddressToStringW (WS2_32.28)
4734 * Convert a sockaddr address into a readable address string.
4737 * sockaddr [I] Pointer to a sockaddr structure.
4738 * len [I] Size of the sockaddr structure.
4739 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
4740 * string [I/O] Pointer to a buffer to receive the address string.
4741 * lenstr [I/O] Size of the receive buffer in WCHARs.
4745 * Failure: SOCKET_ERROR
4748 * The 'info' parameter is ignored.
4751 * Only supports AF_INET addresses.
4753 INT WINAPI WSAAddressToStringW( LPSOCKADDR sockaddr, DWORD len,
4754 LPWSAPROTOCOL_INFOW info, LPWSTR string,
4758 WCHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4759 static const WCHAR format[] = { '%','u','.','%','u','.','%','u','.','%','u',':','%','u',0 };
4762 TRACE( "(%p, %x, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
4764 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
4765 if (!string || !lenstr) return SOCKET_ERROR;
4767 /* sin_family is guaranteed to be the first u_short */
4768 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
4770 sprintfW( buffer, format,
4771 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
4772 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
4773 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
4774 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
4775 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
4777 p = strchrW( buffer, ':' );
4778 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
4780 size = lstrlenW( buffer );
4785 return SOCKET_ERROR;
4788 lstrcpyW( string, buffer );
4792 /***********************************************************************
4793 * WSAEnumNameSpaceProvidersA (WS2_32.34)
4795 INT WINAPI WSAEnumNameSpaceProvidersA( LPDWORD len, LPWSANAMESPACE_INFOA buffer )
4797 FIXME( "(%p %p) Stub!\n", len, buffer );
4801 /***********************************************************************
4802 * WSAEnumNameSpaceProvidersW (WS2_32.35)
4804 INT WINAPI WSAEnumNameSpaceProvidersW( LPDWORD len, LPWSANAMESPACE_INFOW buffer )
4806 FIXME( "(%p %p) Stub!\n", len, buffer );
4810 /***********************************************************************
4811 * WSAGetQOSByName (WS2_32.41)
4813 BOOL WINAPI WSAGetQOSByName( SOCKET s, LPWSABUF lpQOSName, LPQOS lpQOS )
4815 FIXME( "(0x%04x %p %p) Stub!\n", s, lpQOSName, lpQOS );
4819 /***********************************************************************
4820 * WSAGetServiceClassInfoA (WS2_32.42)
4822 INT WINAPI WSAGetServiceClassInfoA( LPGUID provider, LPGUID service, LPDWORD len,
4823 LPWSASERVICECLASSINFOA info )
4825 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider), debugstr_guid(service),
4827 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4828 return SOCKET_ERROR;
4831 /***********************************************************************
4832 * WSAGetServiceClassInfoW (WS2_32.43)
4834 INT WINAPI WSAGetServiceClassInfoW( LPGUID provider, LPGUID service, LPDWORD len,
4835 LPWSASERVICECLASSINFOW info )
4837 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider), debugstr_guid(service),
4839 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4840 return SOCKET_ERROR;
4843 /***********************************************************************
4844 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
4846 INT WINAPI WSAGetServiceClassNameByClassIdA( LPGUID class, LPSTR service, LPDWORD len )
4848 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4849 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4850 return SOCKET_ERROR;
4853 /***********************************************************************
4854 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
4856 INT WINAPI WSAGetServiceClassNameByClassIdW( LPGUID class, LPWSTR service, LPDWORD len )
4858 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4859 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4860 return SOCKET_ERROR;
4863 /***********************************************************************
4864 * WSALookupServiceBeginA (WS2_32.59)
4866 INT WINAPI WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions,
4867 DWORD dwControlFlags,
4870 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4872 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4873 return SOCKET_ERROR;
4876 /***********************************************************************
4877 * WSALookupServiceBeginW (WS2_32.60)
4879 INT WINAPI WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions,
4880 DWORD dwControlFlags,
4883 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4885 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4886 return SOCKET_ERROR;
4889 /***********************************************************************
4890 * WSALookupServiceBeginW (WS2_32.61)
4892 INT WINAPI WSALookupServiceEnd( HANDLE lookup )
4894 FIXME("(%p) Stub!\n", lookup );
4898 /***********************************************************************
4899 * WSALookupServiceNextA (WS2_32.62)
4901 INT WINAPI WSALookupServiceNextA( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETA results )
4903 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup, flags, len, results );
4907 /***********************************************************************
4908 * WSALookupServiceNextW (WS2_32.63)
4910 INT WINAPI WSALookupServiceNextW( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETW results )
4912 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup, flags, len, results );
4916 /***********************************************************************
4917 * WSANtohl (WS2_32.64)
4919 INT WINAPI WSANtohl( SOCKET s, WS_u_long netlong, WS_u_long* lphostlong )
4921 TRACE( "(0x%04x 0x%08x %p)\n", s, netlong, lphostlong );
4923 if (!lphostlong) return WSAEFAULT;
4925 *lphostlong = ntohl( netlong );
4929 /***********************************************************************
4930 * WSANtohs (WS2_32.65)
4932 INT WINAPI WSANtohs( SOCKET s, WS_u_short netshort, WS_u_short* lphostshort )
4934 TRACE( "(0x%04x 0x%08x %p)\n", s, netshort, lphostshort );
4936 if (!lphostshort) return WSAEFAULT;
4938 *lphostshort = ntohs( netshort );
4942 /***********************************************************************
4943 * WSAProviderConfigChange (WS2_32.66)
4945 INT WINAPI WSAProviderConfigChange( LPHANDLE handle, LPWSAOVERLAPPED overlapped,
4946 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion )
4948 FIXME( "(%p %p %p) Stub!\n", handle, overlapped, completion );
4949 return SOCKET_ERROR;
4952 /***********************************************************************
4953 * WSARecvDisconnect (WS2_32.68)
4955 INT WINAPI WSARecvDisconnect( SOCKET s, LPWSABUF disconnectdata )
4957 TRACE( "(0x%04x %p)\n", s, disconnectdata );
4959 return WS_shutdown( s, 0 );
4962 /***********************************************************************
4963 * WSASetServiceA (WS2_32.76)
4965 INT WINAPI WSASetServiceA( LPWSAQUERYSETA query, WSAESETSERVICEOP operation, DWORD flags )
4967 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query, operation, flags );
4971 /***********************************************************************
4972 * WSASetServiceW (WS2_32.77)
4974 INT WINAPI WSASetServiceW( LPWSAQUERYSETW query, WSAESETSERVICEOP operation, DWORD flags )
4976 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query, operation, flags );
4980 /***********************************************************************
4981 * WSCEnableNSProvider (WS2_32.84)
4983 INT WINAPI WSCEnableNSProvider( LPGUID provider, BOOL enable )
4985 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider), enable );
4989 /***********************************************************************
4990 * WSCGetProviderPath (WS2_32.86)
4992 INT WINAPI WSCGetProviderPath( LPGUID provider, LPWSTR path, LPINT len, LPINT errcode )
4994 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider), path, len, errcode );
4996 if (!errcode || !provider || !len) return WSAEFAULT;
4998 *errcode = WSAEINVAL;
4999 return SOCKET_ERROR;
5002 /***********************************************************************
5003 * WSCInstallNameSpace (WS2_32.87)
5005 INT WINAPI WSCInstallNameSpace( LPWSTR identifier, LPWSTR path, DWORD namespace,
5006 DWORD version, LPGUID provider )
5008 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier), debugstr_w(path),
5009 namespace, version, debugstr_guid(provider) );
5013 /***********************************************************************
5014 * WSCUnInstallNameSpace (WS2_32.89)
5016 INT WINAPI WSCUnInstallNameSpace( LPGUID lpProviderId )
5018 FIXME("(%p) Stub!\n", lpProviderId);
5022 /***********************************************************************
5023 * WSCWriteProviderOrder (WS2_32.91)
5025 INT WINAPI WSCWriteProviderOrder( LPDWORD entry, DWORD number )
5027 FIXME("(%p 0x%08x) Stub!\n", entry, number);