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.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * NOTE: If you make any changes to fix a particular app, make sure
22 * they don't break something else like Netscape or telnet and ftp
23 * clients and servers (www.winsite.com got a lot of those).
27 #include "wine/port.h"
32 #include <sys/types.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 # include <sys/ioctl.h>
39 #ifdef HAVE_SYS_FILIO_H
40 # include <sys/filio.h>
42 #ifdef HAVE_SYS_SOCKIO_H
43 # include <sys/sockio.h>
47 # include <sys/so_ioctl.h>
50 #ifdef HAVE_SYS_PARAM_H
51 # include <sys/param.h>
57 #ifdef HAVE_SYS_WAIT_H
58 # include <sys/wait.h>
63 #ifdef HAVE_SYS_SOCKET_H
64 #include <sys/socket.h>
66 #ifdef HAVE_NETINET_IN_H
67 # include <netinet/in.h>
69 #ifdef HAVE_NETINET_TCP_H
70 # include <netinet/tcp.h>
72 #ifdef HAVE_ARPA_INET_H
73 # include <arpa/inet.h>
78 #ifdef HAVE_SYS_ERRNO_H
79 #include <sys/errno.h>
88 #ifdef HAVE_ARPA_NAMESER_H
89 # include <arpa/nameser.h>
98 # include <netipx/ipx.h>
101 #ifdef HAVE_IPX_LINUX
102 # include <asm/types.h>
103 # include <linux/ipx.h>
110 #ifdef HAVE_SYS_POLL_H
111 # include <sys/poll.h>
113 #ifdef HAVE_SYS_TIME_H
114 # include <sys/time.h>
117 #define NONAMELESSUNION
118 #define NONAMELESSSTRUCT
123 #include "winerror.h"
125 #include "winsock2.h"
127 #include "ws2tcpip.h"
130 #include "iphlpapi.h"
132 #include "wine/server.h"
133 #include "wine/debug.h"
134 #include "ntstatus.h"
135 #include "wine/unicode.h"
138 # include "wsnwlink.h"
143 # define sipx_network sipx_addr.x_net
144 # define sipx_node sipx_addr.x_host.c_host
145 #endif /* __FreeBSD__ */
147 WINE_DEFAULT_DEBUG_CHANNEL(winsock);
149 /* critical section to protect some non-rentrant net function */
150 extern CRITICAL_SECTION csWSgetXXXbyYYY;
152 inline static const char *debugstr_sockaddr( const struct WS_sockaddr *a )
154 if (!a) return "(nil)";
155 return wine_dbg_sprintf("{ family %d, address %s, port %d }",
156 ((struct sockaddr_in *)a)->sin_family,
157 inet_ntoa(((struct sockaddr_in *)a)->sin_addr),
158 ntohs(((struct sockaddr_in *)a)->sin_port));
161 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
162 #define SOCKET2HANDLE(s) ((HANDLE)(s))
163 #define HANDLE2SOCKET(h) ((SOCKET)(h))
165 /****************************************************************
166 * Async IO declarations
167 ****************************************************************/
169 typedef struct ws2_async
172 enum ws2_mode {ws2m_read, ws2m_write, ws2m_sd_read, ws2m_sd_write} mode;
173 LPWSAOVERLAPPED user_overlapped;
174 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func;
177 struct WS_sockaddr *addr;
180 int val; /* for send operations */
181 int *ptr; /* for recv operations */
188 /****************************************************************/
190 /* ----------------------------------- internal data */
192 /* ws_... struct conversion flags */
194 typedef struct /* WSAAsyncSelect() control struct */
196 HANDLE service, event, sock;
202 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
203 #define WS_MAX_UDP_DATAGRAM 1024
204 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x );
206 /* hostent's, servent's and protent's are stored in one buffer per thread,
207 * as documented on MSDN for the functions that return any of the buffers */
208 struct per_thread_data
211 struct WS_hostent *he_buffer;
212 struct WS_servent *se_buffer;
213 struct WS_protoent *pe_buffer;
219 static DWORD tls_index = TLS_OUT_OF_INDEXES; /* TLS index for per-thread data */
220 static INT num_startup; /* reference counter */
221 static FARPROC blocking_hook = WSA_DefaultBlockingHook;
223 /* function prototypes */
224 static struct WS_hostent *WS_dup_he(const struct hostent* p_he);
225 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe);
226 static struct WS_servent *WS_dup_se(const struct servent* p_se);
228 int WSAIOCTL_GetInterfaceCount(void);
229 int WSAIOCTL_GetInterfaceName(int intNumber, char *intName);
232 UINT wsaHerrno(int errnr);
234 #define MAP_OPTION(opt) { WS_##opt, opt }
236 static const int ws_sock_map[][2] =
238 MAP_OPTION( SO_DEBUG ),
239 MAP_OPTION( SO_REUSEADDR ),
240 MAP_OPTION( SO_KEEPALIVE ),
241 MAP_OPTION( SO_DONTROUTE ),
242 MAP_OPTION( SO_BROADCAST ),
243 MAP_OPTION( SO_LINGER ),
244 MAP_OPTION( SO_OOBINLINE ),
245 MAP_OPTION( SO_SNDBUF ),
246 MAP_OPTION( SO_RCVBUF ),
247 MAP_OPTION( SO_ERROR ),
248 MAP_OPTION( SO_TYPE ),
250 MAP_OPTION( SO_RCVTIMEO ),
253 MAP_OPTION( SO_SNDTIMEO ),
258 static const int ws_tcp_map[][2] =
261 MAP_OPTION( TCP_NODELAY ),
266 static const int ws_ip_map[][2] =
268 MAP_OPTION( IP_MULTICAST_IF ),
269 MAP_OPTION( IP_MULTICAST_TTL ),
270 MAP_OPTION( IP_MULTICAST_LOOP ),
271 MAP_OPTION( IP_ADD_MEMBERSHIP ),
272 MAP_OPTION( IP_DROP_MEMBERSHIP ),
273 MAP_OPTION( IP_OPTIONS ),
275 MAP_OPTION( IP_HDRINCL ),
277 MAP_OPTION( IP_TOS ),
278 MAP_OPTION( IP_TTL ),
282 inline static DWORD NtStatusToWSAError( const DWORD status )
284 /* We only need to cover the status codes set by server async request handling */
288 case STATUS_SUCCESS: wserr = 0; break;
289 case STATUS_PENDING: wserr = WSA_IO_PENDING; break;
290 case STATUS_INVALID_HANDLE: wserr = WSAENOTSOCK; break; /* WSAEBADF ? */
291 case STATUS_INVALID_PARAMETER: wserr = WSAEINVAL; break;
292 case STATUS_PIPE_DISCONNECTED: wserr = WSAESHUTDOWN; break;
293 case STATUS_CANCELLED: wserr = WSA_OPERATION_ABORTED; break;
294 case STATUS_TIMEOUT: wserr = WSAETIMEDOUT; break;
295 case STATUS_NO_MEMORY: wserr = WSAEFAULT; break;
297 if ( status >= WSABASEERR && status <= WSABASEERR+1004 )
298 /* It is not a NT status code but a winsock error */
302 wserr = RtlNtStatusToDosError( status );
303 FIXME( "Status code %08lx converted to DOS error code %lx\n", status, wserr );
309 /* set last error code from NT status without mapping WSA errors */
310 inline static unsigned int set_error( unsigned int err )
314 err = NtStatusToWSAError( err );
320 inline static int get_sock_fd( SOCKET s, DWORD access, int *flags )
323 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s), access, &fd, flags ) ))
328 inline static void release_sock_fd( SOCKET s, int fd )
330 wine_server_release_fd( SOCKET2HANDLE(s), fd );
333 static void _enable_event( HANDLE s, unsigned int event,
334 unsigned int sstate, unsigned int cstate )
336 SERVER_START_REQ( enable_socket_event )
340 req->sstate = sstate;
341 req->cstate = cstate;
342 wine_server_call( req );
347 static int _is_blocking(SOCKET s)
350 SERVER_START_REQ( get_socket_event )
352 req->handle = SOCKET2HANDLE(s);
353 req->service = FALSE;
355 wine_server_call( req );
356 ret = (reply->state & FD_WINE_NONBLOCKING) == 0;
362 static unsigned int _get_sock_mask(SOCKET s)
365 SERVER_START_REQ( get_socket_event )
367 req->handle = SOCKET2HANDLE(s);
368 req->service = FALSE;
370 wine_server_call( req );
377 static void _sync_sock_state(SOCKET s)
379 /* do a dummy wineserver request in order to let
380 the wineserver run through its select loop once */
381 (void)_is_blocking(s);
384 static int _get_sock_error(SOCKET s, unsigned int bit)
386 int events[FD_MAX_EVENTS];
388 SERVER_START_REQ( get_socket_event )
390 req->handle = SOCKET2HANDLE(s);
391 req->service = FALSE;
393 wine_server_set_reply( req, events, sizeof(events) );
394 wine_server_call( req );
400 static struct per_thread_data *get_per_thread_data(void)
402 struct per_thread_data * ptb = TlsGetValue( tls_index );
403 /* lazy initialization */
406 ptb = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ptb) );
407 TlsSetValue( tls_index, ptb );
412 static void free_per_thread_data(void)
414 struct per_thread_data * ptb = TlsGetValue( tls_index );
418 /* delete scratch buffers */
419 HeapFree( GetProcessHeap(), 0, ptb->he_buffer );
420 HeapFree( GetProcessHeap(), 0, ptb->se_buffer );
421 HeapFree( GetProcessHeap(), 0, ptb->pe_buffer );
422 ptb->he_buffer = NULL;
423 ptb->se_buffer = NULL;
424 ptb->pe_buffer = NULL;
426 HeapFree( GetProcessHeap(), 0, ptb );
429 /***********************************************************************
430 * DllMain (WS2_32.init)
432 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
434 TRACE("%p 0x%lx %p\n", hInstDLL, fdwReason, fImpLoad);
436 case DLL_PROCESS_ATTACH:
437 tls_index = TlsAlloc();
439 case DLL_PROCESS_DETACH:
440 free_per_thread_data();
441 TlsFree( tls_index );
444 case DLL_THREAD_DETACH:
445 free_per_thread_data();
451 /***********************************************************************
454 * Converts socket flags from Windows format.
455 * Return 1 if converted, 0 if not (error).
457 static int convert_sockopt(INT *level, INT *optname)
464 for(i=0; ws_sock_map[i][0]; i++)
466 if( ws_sock_map[i][0] == *optname )
468 *optname = ws_sock_map[i][1];
472 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname);
475 *level = IPPROTO_TCP;
476 for(i=0; ws_tcp_map[i][0]; i++)
478 if ( ws_tcp_map[i][0] == *optname )
480 *optname = ws_tcp_map[i][1];
484 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname);
488 for(i=0; ws_ip_map[i][0]; i++)
490 if (ws_ip_map[i][0] == *optname )
492 *optname = ws_ip_map[i][1];
496 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname);
498 default: FIXME("Unimplemented or unknown socket level\n");
503 static inline BOOL is_timeout_option( int optname )
506 if (optname == SO_RCVTIMEO) return TRUE;
509 if (optname == SO_SNDTIMEO) return TRUE;
514 /* ----------------------------------- Per-thread info (or per-process?) */
516 static char *strdup_lower(const char *str)
519 char *ret = HeapAlloc( GetProcessHeap(), 0, strlen(str) + 1 );
523 for (i = 0; str[i]; i++) ret[i] = tolower(str[i]);
526 else SetLastError(WSAENOBUFS);
530 static fd_set* fd_set_import( fd_set* fds, const WS_fd_set* wsfds, int access, int* highfd, int lfd[] )
532 /* translate Winsock fd set into local fd set */
538 for( i = 0; i < wsfds->fd_count; i++ )
540 int s = wsfds->fd_array[i];
541 int fd = get_sock_fd( s, access, NULL );
545 if( fd > *highfd ) *highfd = fd;
555 inline static int sock_error_p(int s)
557 unsigned int optval, optlen;
559 optlen = sizeof(optval);
560 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
561 if (optval) WARN("\t[%i] error: %d\n", s, optval);
565 static int fd_set_export( const fd_set* fds, fd_set* exceptfds, WS_fd_set* wsfds, int lfd[] )
569 /* translate local fd set into Winsock fd set, adding
570 * errors to exceptfds (only if app requested it) */
574 int i, j, count = wsfds->fd_count;
576 for( i = 0, j = 0; i < count; i++ )
579 SOCKET s = wsfds->fd_array[i];
580 if (fd == -1) continue;
581 if( FD_ISSET(fd, fds) )
583 if ( exceptfds && sock_error_p(fd) )
585 FD_SET(fd, exceptfds);
588 else wsfds->fd_array[j++] = s;
590 release_sock_fd( s, fd );
597 static void fd_set_unimport( WS_fd_set* wsfds, int lfd[] )
603 for ( i = 0; i < wsfds->fd_count; i++ )
604 if ( lfd[i] >= 0 ) release_sock_fd( wsfds->fd_array[i], lfd[i] );
609 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
610 * from an fd and return the value converted to milli seconds
611 * or -1 if there is an infinite time out */
612 static inline int get_rcvsnd_timeo( int fd, int optname)
615 int len = sizeof(tv);
616 int ret = getsockopt(fd, SOL_SOCKET, optname, &tv, &len);
618 ret = tv.tv_sec * 1000 + tv.tv_usec / 1000;
619 if( ret <= 0 ) /* tv == {0,0} means infinite time out */
624 /* macro wrappers for portability */
626 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
628 #define GET_RCVTIMEO(fd) (-1)
632 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
634 #define GET_SNDTIMEO(fd) (-1)
637 /* utility: given an fd, will block until one of the events occurs */
638 static inline int do_block( int fd, int events, int timeout )
646 while ((ret = poll(&pfd, 1, timeout)) < 0)
657 /* ----------------------------------- API -----
659 * Init / cleanup / error checking.
662 /***********************************************************************
663 * WSAStartup (WS2_32.115)
665 int WINAPI WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData)
667 TRACE("verReq=%x\n", wVersionRequested);
669 if (LOBYTE(wVersionRequested) < 1)
670 return WSAVERNOTSUPPORTED;
672 if (!lpWSAData) return WSAEINVAL;
676 /* that's the whole of the negotiation for now */
677 lpWSAData->wVersion = wVersionRequested;
678 /* return winsock information */
679 lpWSAData->wHighVersion = 0x0202;
680 strcpy(lpWSAData->szDescription, "WinSock 2.0" );
681 strcpy(lpWSAData->szSystemStatus, "Running" );
682 lpWSAData->iMaxSockets = WS_MAX_SOCKETS_PER_PROCESS;
683 lpWSAData->iMaxUdpDg = WS_MAX_UDP_DATAGRAM;
684 /* don't do anything with lpWSAData->lpVendorInfo */
685 /* (some apps don't allocate the space for this field) */
687 TRACE("succeeded\n");
692 /***********************************************************************
693 * WSACleanup (WS2_32.116)
695 INT WINAPI WSACleanup(void)
699 SetLastError(WSANOTINITIALISED);
704 /***********************************************************************
705 * WSAGetLastError (WINSOCK.111)
706 * WSAGetLastError (WS2_32.111)
708 INT WINAPI WSAGetLastError(void)
710 return GetLastError();
713 /***********************************************************************
714 * WSASetLastError (WS2_32.112)
716 void WINAPI WSASetLastError(INT iError) {
717 SetLastError(iError);
720 static struct WS_hostent *check_buffer_he(int size)
722 struct per_thread_data * ptb = get_per_thread_data();
725 if (ptb->he_len >= size ) return ptb->he_buffer;
726 HeapFree( GetProcessHeap(), 0, ptb->he_buffer );
728 ptb->he_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->he_len = size) );
729 if (!ptb->he_buffer) SetLastError(WSAENOBUFS);
730 return ptb->he_buffer;
733 static struct WS_servent *check_buffer_se(int size)
735 struct per_thread_data * ptb = get_per_thread_data();
738 if (ptb->se_len >= size ) return ptb->se_buffer;
739 HeapFree( GetProcessHeap(), 0, ptb->se_buffer );
741 ptb->se_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->se_len = size) );
742 if (!ptb->se_buffer) SetLastError(WSAENOBUFS);
743 return ptb->se_buffer;
746 static struct WS_protoent *check_buffer_pe(int size)
748 struct per_thread_data * ptb = get_per_thread_data();
751 if (ptb->pe_len >= size ) return ptb->pe_buffer;
752 HeapFree( GetProcessHeap(), 0, ptb->pe_buffer );
754 ptb->pe_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->pe_len = size) );
755 if (!ptb->pe_buffer) SetLastError(WSAENOBUFS);
756 return ptb->pe_buffer;
759 /* ----------------------------------- i/o APIs */
762 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf)== WS_AF_IPX)
764 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET)
768 /**********************************************************************/
770 /* Returns the converted address if successful, NULL if it was too small to
771 * start with. Note that the returned pointer may be the original pointer
772 * if no conversion is necessary.
774 static const struct sockaddr* ws_sockaddr_ws2u(const struct WS_sockaddr* wsaddr, int wsaddrlen, int *uaddrlen)
776 switch (wsaddr->sa_family)
781 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
782 struct sockaddr_ipx* uipx;
784 if (wsaddrlen<sizeof(struct WS_sockaddr_ipx))
787 *uaddrlen=sizeof(struct sockaddr_ipx);
788 uipx=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *uaddrlen);
789 uipx->sipx_family=AF_IPX;
790 uipx->sipx_port=wsipx->sa_socket;
791 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
794 memcpy(&uipx->sipx_network,wsipx->sa_netnum,sizeof(uipx->sipx_network)+sizeof(uipx->sipx_node));
795 #ifdef IPX_FRAME_NONE
796 uipx->sipx_type=IPX_FRAME_NONE;
798 return (const struct sockaddr*)uipx;
803 if (wsaddrlen<sizeof(struct WS_sockaddr))
806 /* No conversion needed, just return the original address */
808 return (const struct sockaddr*)wsaddr;
812 /* Allocates a Unix sockaddr structure to receive the data */
813 inline struct sockaddr* ws_sockaddr_alloc(const struct WS_sockaddr* wsaddr, int* wsaddrlen, int* uaddrlen)
817 ERR( "WINE shouldn't pass a NULL wsaddr! Attempting to continue\n" );
819 /* This is not strictly the right thing to do. Hope it works however */
828 *uaddrlen=max(sizeof(struct sockaddr),*wsaddrlen);
830 return HeapAlloc(GetProcessHeap(), 0, *uaddrlen);
833 /* Returns 0 if successful, -1 if the buffer is too small */
834 static int ws_sockaddr_u2ws(const struct sockaddr* uaddr, int uaddrlen, struct WS_sockaddr* wsaddr, int* wsaddrlen)
838 switch(uaddr->sa_family)
843 struct sockaddr_ipx* uipx=(struct sockaddr_ipx*)uaddr;
844 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
847 switch (*wsaddrlen) /* how much can we copy? */
852 wsipx->sa_socket=uipx->sipx_port;
856 memcpy(wsipx->sa_nodenum,uipx->sipx_node,sizeof(wsipx->sa_nodenum));
864 memcpy(wsipx->sa_netnum,&uipx->sipx_network,sizeof(wsipx->sa_netnum));
870 wsipx->sa_family=WS_AF_IPX;
882 /* No conversion needed */
883 memcpy(wsaddr,uaddr,*wsaddrlen);
884 if (*wsaddrlen<uaddrlen) {
894 /* to be called to free the memory allocated by ws_sockaddr_ws2u or
897 inline void ws_sockaddr_free(const struct sockaddr* uaddr, const struct WS_sockaddr* wsaddr)
899 if (uaddr!=(const struct sockaddr*)wsaddr)
900 HeapFree(GetProcessHeap(), 0, (void *)uaddr);
903 /**************************************************************************
904 * Functions for handling overlapped I/O
905 **************************************************************************/
907 static void CALLBACK ws2_async_terminate(ws2_async* as, IO_STATUS_BLOCK* iosb)
909 TRACE( "as: %p uovl %p ovl %p\n", as, as->user_overlapped, iosb );
911 wine_server_release_fd( as->hSocket, as->fd );
912 if ( as->event != INVALID_HANDLE_VALUE )
913 NtSetEvent( as->event, NULL );
915 if (as->completion_func)
916 as->completion_func( NtStatusToWSAError (iosb->u.Status),
917 iosb->Information, as->user_overlapped, as->flags );
918 if ( !as->user_overlapped )
921 /* FIXME: I don't think this is really used */
922 if ( as->overlapped->hEvent != INVALID_HANDLE_VALUE )
923 WSACloseEvent( as->overlapped->hEvent );
925 HeapFree( GetProcessHeap(), 0, iosb );
928 HeapFree( GetProcessHeap(), 0, as->iovec );
929 HeapFree( GetProcessHeap(), 0, as );
932 /***********************************************************************
933 * WS2_make_async (INTERNAL)
936 static void WINAPI WS2_async_recv(void*, IO_STATUS_BLOCK*, ULONG);
937 static void WINAPI WS2_async_send(void*, IO_STATUS_BLOCK*, ULONG);
938 static void WINAPI WS2_async_shutdown( void*, IO_STATUS_BLOCK*, ULONG);
940 inline static struct ws2_async*
941 WS2_make_async(SOCKET s, int fd, enum ws2_mode mode, struct iovec *iovec, DWORD dwBufferCount,
942 LPDWORD lpFlags, struct WS_sockaddr *addr,
943 LPINT addrlen, LPWSAOVERLAPPED lpOverlapped,
944 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
945 IO_STATUS_BLOCK **piosb)
947 struct ws2_async *wsa = HeapAlloc( GetProcessHeap(), 0, sizeof( ws2_async ) );
949 TRACE( "wsa %p\n", wsa );
954 wsa->hSocket = (HANDLE) s;
960 wsa->flags = *lpFlags;
961 wsa->addrlen.ptr = addrlen;
966 wsa->addrlen.val = *addrlen;
969 ERR("Invalid async mode: %d\n", mode);
971 wsa->user_overlapped = lpOverlapped;
972 wsa->completion_func = lpCompletionRoutine;
974 wsa->n_iovecs = dwBufferCount;
977 wsa->event = INVALID_HANDLE_VALUE;
981 *piosb = (IO_STATUS_BLOCK*)lpOverlapped;
982 if (!lpCompletionRoutine)
984 wsa->event = lpOverlapped->hEvent;
985 NtResetEvent(wsa->event, NULL);
988 else if (!(*piosb = HeapAlloc( GetProcessHeap(), 0, sizeof(IO_STATUS_BLOCK))))
991 (*piosb)->Information = 0;
992 (*piosb)->u.Status = STATUS_PENDING;
993 TRACE( "wsa %p, h %p, ev %p, fd %d, iosb %p, uov %p, cfunc %p\n",
994 wsa, wsa->hSocket, wsa->event, wsa->fd,
995 *piosb, wsa->user_overlapped, wsa->completion_func );
1001 HeapFree( GetProcessHeap(), 0, wsa );
1005 static ULONG ws2_queue_async(struct ws2_async* wsa, IO_STATUS_BLOCK* iosb)
1007 PIO_APC_ROUTINE apc;
1013 case ws2m_read: apc = WS2_async_recv; type = ASYNC_TYPE_READ; break;
1014 case ws2m_write: apc = WS2_async_send; type = ASYNC_TYPE_WRITE; break;
1015 case ws2m_sd_read: apc = WS2_async_shutdown; type = ASYNC_TYPE_READ; break;
1016 case ws2m_sd_write: apc = WS2_async_shutdown; type = ASYNC_TYPE_WRITE; break;
1017 default: FIXME("Unknown internal mode (%d)\n", wsa->mode); return STATUS_INVALID_PARAMETER;
1020 SERVER_START_REQ( register_async )
1022 req->handle = wsa->hSocket;
1027 req->count = iosb->Information;
1028 status = wine_server_call( req );
1032 if ( status ) iosb->u.Status = status;
1033 if ( iosb->u.Status != STATUS_PENDING )
1035 (apc)( wsa, iosb, iosb->u.Status );
1036 return iosb->u.Status;
1038 NtCurrentTeb()->num_async_io++;
1039 return STATUS_SUCCESS;
1042 /***********************************************************************
1043 * WS2_recv (INTERNAL)
1045 * Workhorse for both synchronous and asynchronous recv() operations.
1047 static int WS2_recv( int fd, struct iovec* iov, int count,
1048 struct WS_sockaddr *lpFrom, LPINT lpFromlen,
1053 TRACE( "fd %d, iovec %p, count %d addr %s, len %p, flags %lx\n",
1054 fd, iov, count, debugstr_sockaddr(lpFrom), lpFromlen, *lpFlags);
1056 hdr.msg_name = NULL;
1060 hdr.msg_namelen = *lpFromlen;
1061 hdr.msg_name = ws_sockaddr_alloc( lpFrom, lpFromlen, &hdr.msg_namelen );
1062 if ( !hdr.msg_name )
1064 WSASetLastError( WSAEFAULT );
1070 hdr.msg_namelen = 0;
1073 hdr.msg_iovlen = count;
1074 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1075 hdr.msg_accrights = NULL;
1076 hdr.msg_accrightslen = 0;
1078 hdr.msg_control = NULL;
1079 hdr.msg_controllen = 0;
1083 if ( (n = recvmsg(fd, &hdr, *lpFlags)) == -1 )
1085 TRACE( "recvmsg error %d\n", errno);
1090 ws_sockaddr_u2ws( hdr.msg_name, hdr.msg_namelen,
1091 lpFrom, lpFromlen ) != 0 )
1093 /* The from buffer was too small, but we read the data
1094 * anyway. Is that really bad?
1096 WSASetLastError( WSAEFAULT );
1097 WARN( "Address buffer too small\n" );
1102 ws_sockaddr_free( hdr.msg_name, lpFrom );
1103 TRACE("-> %d\n", n);
1107 /***********************************************************************
1108 * WS2_async_recv (INTERNAL)
1110 * Handler for overlapped recv() operations.
1112 static void WINAPI WS2_async_recv( void* ovp, IO_STATUS_BLOCK* iosb, ULONG status)
1114 ws2_async* wsa = (ws2_async*) ovp;
1117 TRACE( "(%p %p %lx)\n", wsa, iosb, status );
1121 case STATUS_ALERTED:
1122 result = WS2_recv( wsa->fd, wsa->iovec, wsa->n_iovecs,
1123 wsa->addr, wsa->addrlen.ptr, &wsa->flags );
1126 iosb->u.Status = STATUS_SUCCESS;
1127 iosb->Information = result;
1128 TRACE( "received %d bytes\n", result );
1129 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1134 if ( err == WSAEINTR || err == WSAEWOULDBLOCK ) /* errno: EINTR / EAGAIN */
1136 iosb->u.Status = STATUS_PENDING;
1137 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1138 TRACE( "still pending\n" );
1142 iosb->u.Status = err; /* FIXME: is this correct ???? */
1143 TRACE( "Error: %x\n", err );
1146 if (iosb->u.Status == STATUS_PENDING)
1147 ws2_queue_async(wsa, iosb);
1149 ws2_async_terminate(wsa, iosb);
1152 FIXME( "status: %ld\n", status );
1153 iosb->u.Status = status;
1154 ws2_async_terminate(wsa, iosb);
1159 /***********************************************************************
1160 * WS2_send (INTERNAL)
1162 * Workhorse for both synchronous and asynchronous send() operations.
1164 static int WS2_send( int fd, struct iovec* iov, int count,
1165 const struct WS_sockaddr *to, INT tolen, DWORD dwFlags )
1169 TRACE( "fd %d, iovec %p, count %d addr %s, len %d, flags %lx\n",
1170 fd, iov, count, debugstr_sockaddr(to), tolen, dwFlags);
1172 hdr.msg_name = NULL;
1176 hdr.msg_name = (struct sockaddr*) ws_sockaddr_ws2u( to, tolen, &hdr.msg_namelen );
1177 if ( !hdr.msg_name )
1179 WSASetLastError( WSAEFAULT );
1184 if(to->sa_family == WS_AF_IPX)
1187 struct sockaddr_ipx* uipx = (struct sockaddr_ipx*)hdr.msg_name;
1189 int len=sizeof(int);
1191 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1192 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1193 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1194 * ipx type in the sockaddr_opx structure with the stored value.
1196 if(getsockopt(fd, SOL_IPX, IPX_TYPE, &val, &len) != -1)
1198 TRACE("ptype: %d (fd:%d)\n", val, fd);
1199 uipx->sipx_type = val;
1207 hdr.msg_namelen = 0;
1210 hdr.msg_iovlen = count;
1211 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1212 hdr.msg_accrights = NULL;
1213 hdr.msg_accrightslen = 0;
1215 hdr.msg_control = NULL;
1216 hdr.msg_controllen = 0;
1220 n = sendmsg(fd, &hdr, dwFlags);
1223 ws_sockaddr_free( hdr.msg_name, to );
1227 /***********************************************************************
1228 * WS2_async_send (INTERNAL)
1230 * Handler for overlapped send() operations.
1232 static void WINAPI WS2_async_send(void* as, IO_STATUS_BLOCK* iosb, ULONG status)
1234 ws2_async* wsa = (ws2_async*) as;
1237 TRACE( "(%p %p %lx)\n", wsa, iosb, status );
1241 case STATUS_ALERTED:
1242 if (iosb->u.Status != STATUS_PENDING) FIXME("wrong %08lx\n", iosb->u.Status);
1243 /* check to see if the data is ready (non-blocking) */
1244 result = WS2_send( wsa->fd, wsa->iovec, wsa->n_iovecs,
1245 wsa->addr, wsa->addrlen.val, wsa->flags );
1249 iosb->u.Status = STATUS_SUCCESS;
1250 iosb->Information = result;
1251 TRACE( "sent %d bytes\n", result );
1252 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1256 int err = wsaErrno();
1257 if ( err == WSAEINTR )
1259 iosb->u.Status = STATUS_PENDING;
1260 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1261 TRACE( "still pending\n" );
1265 /* We set the status to a winsock error code and check for that
1266 later in NtStatusToWSAError () */
1267 iosb->u.Status = err;
1268 TRACE( "Error: %x\n", err );
1271 if (iosb->u.Status == STATUS_PENDING)
1272 ws2_queue_async(wsa, iosb);
1274 ws2_async_terminate(wsa, iosb);
1277 FIXME( "status: %ld\n", status );
1278 iosb->u.Status = status;
1279 ws2_async_terminate(wsa, iosb);
1285 /***********************************************************************
1286 * WS2_async_shutdown (INTERNAL)
1288 * Handler for shutdown() operations on overlapped sockets.
1290 static void WINAPI WS2_async_shutdown( void* as, PIO_STATUS_BLOCK iosb, ULONG status )
1292 ws2_async* wsa = (ws2_async*) as;
1295 TRACE( "async %p %d\n", wsa, wsa->mode );
1298 case STATUS_ALERTED:
1299 switch ( wsa->mode )
1301 case ws2m_sd_read: err = shutdown( wsa->fd, 0 ); break;
1302 case ws2m_sd_write: err = shutdown( wsa->fd, 1 ); break;
1303 default: ERR("invalid mode: %d\n", wsa->mode );
1305 iosb->u.Status = err ? wsaErrno() : STATUS_SUCCESS;
1306 if (iosb->u.Status == STATUS_PENDING)
1307 ws2_queue_async(wsa, iosb);
1309 ws2_async_terminate(wsa, iosb);
1312 iosb->u.Status = status;
1313 ws2_async_terminate(wsa, iosb);
1319 /***********************************************************************
1320 * WS2_register_async_shutdown (INTERNAL)
1322 * Helper function for WS_shutdown() on overlapped sockets.
1324 static int WS2_register_async_shutdown( SOCKET s, int fd, enum ws2_mode mode )
1326 struct ws2_async *wsa;
1327 int ret, err = WSAEFAULT;
1330 LPWSAOVERLAPPED ovl = HeapAlloc(GetProcessHeap(), 0, sizeof( WSAOVERLAPPED ));
1331 IO_STATUS_BLOCK *iosb;
1333 TRACE("s %d fd %d mode %d\n", s, fd, mode);
1337 ovl->hEvent = WSACreateEvent();
1338 if ( ovl->hEvent == WSA_INVALID_EVENT )
1341 wsa = WS2_make_async( s, fd, mode, NULL, 0,
1342 &dwflags, NULL, &len, ovl, NULL, &iosb );
1346 /* Hack: this will cause ws2_async_cleanup() to free the overlapped structure */
1347 wsa->user_overlapped = NULL;
1348 if ( (ret = ws2_queue_async( wsa, iosb )) )
1350 err = NtStatusToWSAError( ret );
1353 /* Try immediate completion */
1354 while ( WaitForSingleObjectEx( ovl->hEvent, 0, TRUE ) == STATUS_USER_APC );
1358 WSACloseEvent( ovl->hEvent );
1360 HeapFree( GetProcessHeap(), 0, iosb );
1361 HeapFree( GetProcessHeap(), 0, ovl );
1366 /***********************************************************************
1369 SOCKET WINAPI WS_accept(SOCKET s, struct WS_sockaddr *addr,
1374 TRACE("socket %04x\n", s );
1375 if (_is_blocking(s))
1377 int fd = get_sock_fd( s, GENERIC_READ, NULL );
1378 if (fd == -1) return INVALID_SOCKET;
1380 do_block(fd, POLLIN, -1);
1381 _sync_sock_state(s); /* let wineserver notice connection */
1382 release_sock_fd( s, fd );
1383 /* retrieve any error codes from it */
1384 SetLastError(_get_sock_error(s, FD_ACCEPT_BIT));
1385 /* FIXME: care about the error? */
1387 SERVER_START_REQ( accept_socket )
1389 req->lhandle = SOCKET2HANDLE(s);
1390 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
1391 req->inherit = TRUE;
1392 set_error( wine_server_call( req ) );
1393 as = HANDLE2SOCKET( reply->handle );
1398 if (addr) WS_getpeername(as, addr, addrlen32);
1401 return INVALID_SOCKET;
1404 /***********************************************************************
1407 int WINAPI WS_bind(SOCKET s, const struct WS_sockaddr* name, int namelen)
1409 int fd = get_sock_fd( s, 0, NULL );
1410 int res = SOCKET_ERROR;
1412 TRACE("socket %04x, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1416 if (!name || !SUPPORTED_PF(name->sa_family))
1418 SetLastError(WSAEAFNOSUPPORT);
1422 const struct sockaddr* uaddr;
1425 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1428 SetLastError(WSAEFAULT);
1432 if (bind(fd, uaddr, uaddrlen) < 0)
1434 int loc_errno = errno;
1435 WARN("\tfailure - errno = %i\n", errno);
1440 SetLastError(WSAENOTSOCK);
1443 SetLastError(WSAEINVAL);
1446 SetLastError(wsaErrno());
1452 res=0; /* success */
1454 ws_sockaddr_free(uaddr,name);
1457 release_sock_fd( s, fd );
1462 /***********************************************************************
1463 * closesocket (WS2_32.3)
1465 int WINAPI WS_closesocket(SOCKET s)
1467 TRACE("socket %04x\n", s);
1468 if (CloseHandle(SOCKET2HANDLE(s))) return 0;
1469 return SOCKET_ERROR;
1472 /***********************************************************************
1473 * connect (WS2_32.4)
1475 int WINAPI WS_connect(SOCKET s, const struct WS_sockaddr* name, int namelen)
1477 int fd = get_sock_fd( s, GENERIC_READ, NULL );
1479 TRACE("socket %04x, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1483 const struct sockaddr* uaddr;
1486 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1489 SetLastError(WSAEFAULT);
1495 rc=connect(fd, uaddr, uaddrlen);
1496 ws_sockaddr_free(uaddr,name);
1498 goto connect_success;
1501 if (errno == EINPROGRESS)
1503 /* tell wineserver that a connection is in progress */
1504 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1505 FD_CONNECT|FD_READ|FD_WRITE,
1506 FD_WINE_CONNECTED|FD_WINE_LISTENING);
1507 if (_is_blocking(s))
1511 do_block(fd, POLLIN | POLLOUT, -1);
1512 _sync_sock_state(s); /* let wineserver notice connection */
1513 /* retrieve any error codes from it */
1514 result = _get_sock_error(s, FD_CONNECT_BIT);
1516 SetLastError(result);
1519 goto connect_success;
1524 SetLastError(WSAEWOULDBLOCK);
1529 SetLastError(wsaErrno());
1531 release_sock_fd( s, fd );
1533 return SOCKET_ERROR;
1536 release_sock_fd( s, fd );
1537 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1538 FD_WINE_CONNECTED|FD_READ|FD_WRITE,
1539 FD_CONNECT|FD_WINE_LISTENING);
1543 /***********************************************************************
1544 * WSAConnect (WS2_32.30)
1546 int WINAPI WSAConnect( SOCKET s, const struct WS_sockaddr* name, int namelen,
1547 LPWSABUF lpCallerData, LPWSABUF lpCalleeData,
1548 LPQOS lpSQOS, LPQOS lpGQOS )
1550 if ( lpCallerData || lpCalleeData || lpSQOS || lpGQOS )
1551 FIXME("unsupported parameters!\n");
1552 return WS_connect( s, name, namelen );
1556 /***********************************************************************
1557 * getpeername (WS2_32.5)
1559 int WINAPI WS_getpeername(SOCKET s, struct WS_sockaddr *name, int *namelen)
1564 TRACE("socket: %04x, ptr %p, len %08x\n", s, name, *namelen);
1566 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1567 if( (name == NULL) || (namelen == NULL) )
1569 SetLastError( WSAEFAULT );
1570 return SOCKET_ERROR;
1573 fd = get_sock_fd( s, 0, NULL );
1578 struct sockaddr* uaddr;
1581 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1582 if (getpeername(fd, uaddr, &uaddrlen) != 0)
1584 SetLastError(wsaErrno());
1586 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1588 /* The buffer was too small */
1589 SetLastError(WSAEFAULT);
1595 ws_sockaddr_free(uaddr,name);
1596 release_sock_fd( s, fd );
1601 /***********************************************************************
1602 * getsockname (WS2_32.6)
1604 int WINAPI WS_getsockname(SOCKET s, struct WS_sockaddr *name, int *namelen)
1609 TRACE("socket: %04x, ptr %p, len %8x\n", s, name, *namelen);
1611 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1612 if( (name == NULL) || (namelen == NULL) )
1614 SetLastError( WSAEFAULT );
1615 return SOCKET_ERROR;
1618 fd = get_sock_fd( s, 0, NULL );
1623 struct sockaddr* uaddr;
1626 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1627 if (getsockname(fd, uaddr, &uaddrlen) != 0)
1629 SetLastError(wsaErrno());
1631 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1633 /* The buffer was too small */
1634 SetLastError(WSAEFAULT);
1640 ws_sockaddr_free(uaddr,name);
1641 release_sock_fd( s, fd );
1646 /***********************************************************************
1647 * getsockopt (WS2_32.7)
1649 INT WINAPI WS_getsockopt(SOCKET s, INT level,
1650 INT optname, char *optval, INT *optlen)
1655 TRACE("socket: %04x, level 0x%x, name 0x%x, ptr %8x, len %d\n", s, level,
1656 (int) optname, (int) optval, (int) *optlen);
1657 /* SO_OPENTYPE does not require a valid socket handle. */
1658 if (level == WS_SOL_SOCKET && optname == WS_SO_OPENTYPE)
1660 if (!optlen || *optlen < sizeof(int) || !optval)
1662 SetLastError(WSAEFAULT);
1663 return SOCKET_ERROR;
1665 *(int *)optval = get_per_thread_data()->opentype;
1666 *optlen = sizeof(int);
1667 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
1672 if(level == NSPROTO_IPX)
1674 struct WS_sockaddr_ipx addr;
1675 IPX_ADDRESS_DATA *data;
1680 fd = get_sock_fd( s, 0, NULL );
1682 if(getsockopt(fd, SOL_IPX, IPX_TYPE, optval, optlen) == -1)
1684 return SOCKET_ERROR;
1689 socklen_t len=sizeof(struct ipx);
1691 if(getsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, &len) == -1 )
1692 return SOCKET_ERROR;
1693 *optval = (int)val.ipx_pt;
1696 TRACE("ptype: %d (fd: %d)\n", *(int*)optval, fd);
1697 release_sock_fd( s, fd );
1702 * On a Win2000 system with one network card there are usually three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
1703 * Using this call you can then retrieve info about this all. In case of Linux it is a bit different. Usually you have
1704 * only "one" device active and further it is not possible to query things like the linkspeed.
1706 FIXME("IPX_ADDRESS\n");
1707 namelen = sizeof(struct WS_sockaddr);
1708 memset(&addr, 0, sizeof(struct WS_sockaddr));
1709 WS_getsockname(s, (struct WS_sockaddr*)&addr, &namelen);
1711 data = (IPX_ADDRESS_DATA*)optval;
1712 memcpy(data->nodenum,&addr.sa_nodenum,sizeof(data->nodenum));
1713 memcpy(data->netnum,&addr.sa_netnum,sizeof(data->netnum));
1714 data->adapternum = 0;
1715 data->wan = FALSE; /* We are not on a wan for now .. */
1716 data->status = FALSE; /* Since we are not on a wan, the wan link isn't up */
1717 data->maxpkt = 1467; /* This value is the default one, at least on Win2k/WinXP */
1718 data->linkspeed = 100000; /* Set the line speed in 100bit/s to 10 Mbit; note 1MB = 1000kB in this case */
1720 case IPX_MAX_ADAPTER_NUM:
1721 FIXME("IPX_MAX_ADAPTER_NUM\n");
1722 *(int*)optval = 1; /* As noted under IPX_ADDRESS we have just one card. */
1726 FIXME("IPX optname:%x\n", optname);
1727 return SOCKET_ERROR;
1732 if( (fd = get_sock_fd( s, 0, NULL )) == -1)
1733 return SOCKET_ERROR;
1735 if (!convert_sockopt(&level, &optname)) {
1736 SetLastError(WSAENOPROTOOPT); /* Unknown option */
1740 struct linger lingval;
1741 INT len, *plen = optlen;
1742 char *pval = optval;
1743 if(level == SOL_SOCKET && is_timeout_option(optname)) {
1746 pval = (char *) &tv;
1747 } else if( level == SOL_SOCKET && optname == SO_LINGER) {
1748 len = sizeof(lingval);
1750 pval = (char *) &lingval;
1752 if (getsockopt(fd, (int) level, optname, pval, plen) != 0 ) {
1753 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1755 } else if(level == SOL_SOCKET && is_timeout_option(optname)) {
1756 if( *optlen >= sizeof(INT) ) {
1757 *optlen = sizeof(INT);
1758 *(INT*)optval = tv.tv_sec * 1000 + tv.tv_usec / 1000;
1760 SetLastError(WSAEFAULT);
1763 } else if( level == SOL_SOCKET && optname == SO_LINGER) {
1764 if( *optlen >= sizeof( LINGER) ) {
1765 (( LINGER *) optval)->l_onoff = lingval.l_onoff;
1766 (( LINGER *) optval)->l_linger = lingval.l_linger;
1768 SetLastError(WSAEFAULT);
1773 release_sock_fd( s, fd );
1778 /***********************************************************************
1782 u_long WINAPI WS_htonl(u_long hostlong)
1784 return htonl(hostlong);
1788 /***********************************************************************
1792 u_short WINAPI WS_htons(u_short hostshort)
1794 return htons(hostshort);
1797 /***********************************************************************
1798 * WSAHtonl (WS2_32.46)
1799 * From MSDN decription of error codes, this function should also
1800 * check if WinSock has been initialized and the socket is a valid
1801 * socket. But why? This function only translates a host byte order
1802 * u_long into a network byte order u_long...
1804 int WINAPI WSAHtonl(SOCKET s, u_long hostlong, u_long *lpnetlong)
1808 *lpnetlong = htonl(hostlong);
1811 WSASetLastError(WSAEFAULT);
1812 return SOCKET_ERROR;
1815 /***********************************************************************
1816 * WSAHtons (WS2_32.47)
1817 * From MSDN decription of error codes, this function should also
1818 * check if WinSock has been initialized and the socket is a valid
1819 * socket. But why? This function only translates a host byte order
1820 * u_short into a network byte order u_short...
1822 int WINAPI WSAHtons(SOCKET s, u_short hostshort, u_short *lpnetshort)
1827 *lpnetshort = htons(hostshort);
1830 WSASetLastError(WSAEFAULT);
1831 return SOCKET_ERROR;
1835 /***********************************************************************
1836 * inet_addr (WINSOCK.10)
1837 * inet_addr (WS2_32.11)
1839 u_long WINAPI WS_inet_addr(const char *cp)
1841 return inet_addr(cp);
1845 /***********************************************************************
1846 * ntohl (WINSOCK.14)
1849 u_long WINAPI WS_ntohl(u_long netlong)
1851 return ntohl(netlong);
1855 /***********************************************************************
1856 * ntohs (WINSOCK.15)
1859 u_short WINAPI WS_ntohs(u_short netshort)
1861 return ntohs(netshort);
1865 /***********************************************************************
1866 * inet_ntoa (WS2_32.12)
1868 char* WINAPI WS_inet_ntoa(struct WS_in_addr in)
1870 /* use "buffer for dummies" here because some applications have a
1871 * propensity to decode addresses in ws_hostent structure without
1872 * saving them first...
1874 static char dbuffer[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
1876 char* s = inet_ntoa(*((struct in_addr*)&in));
1882 SetLastError(wsaErrno());
1886 /**********************************************************************
1887 * WSAIoctl (WS2_32.50)
1890 * FIXME: Only SIO_GET_INTERFACE_LIST option implemented.
1892 INT WINAPI WSAIoctl(SOCKET s,
1893 DWORD dwIoControlCode,
1896 LPVOID lpbOutBuffer,
1898 LPDWORD lpcbBytesReturned,
1899 LPWSAOVERLAPPED lpOverlapped,
1900 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
1902 int fd = get_sock_fd( s, 0, NULL );
1904 if (fd == -1) return SOCKET_ERROR;
1906 TRACE("%d, 0x%08lx, %p, %ld, %p, %ld, %p, %p, %p\n",
1907 s, dwIoControlCode, lpvInBuffer, cbInBuffer, lpbOutBuffer,
1908 cbOutBuffer, lpcbBytesReturned, lpOverlapped, lpCompletionRoutine);
1910 switch( dwIoControlCode )
1912 case SIO_GET_INTERFACE_LIST:
1914 INTERFACE_INFO* intArray = (INTERFACE_INFO*)lpbOutBuffer;
1915 DWORD size, numInt, apiReturn;
1917 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
1921 release_sock_fd( s, fd );
1922 WSASetLastError(WSAEFAULT);
1923 return SOCKET_ERROR;
1925 if (!lpcbBytesReturned)
1927 release_sock_fd( s, fd );
1928 WSASetLastError(WSAEFAULT);
1929 return SOCKET_ERROR;
1932 apiReturn = GetAdaptersInfo(NULL, &size);
1933 if (apiReturn == ERROR_NO_DATA)
1937 else if (apiReturn == ERROR_BUFFER_OVERFLOW)
1939 PIP_ADAPTER_INFO table = (PIP_ADAPTER_INFO)HeapAlloc(GetProcessHeap(),0,size);
1943 if (GetAdaptersInfo(table, &size) == NO_ERROR)
1945 PIP_ADAPTER_INFO ptr;
1947 if (size*sizeof(INTERFACE_INFO)/sizeof(IP_ADAPTER_INFO) > cbOutBuffer)
1949 WARN("Buffer too small = %lu, cbOutBuffer = %lu\n", size, cbOutBuffer);
1950 HeapFree(GetProcessHeap(),0,table);
1951 release_sock_fd( s, fd );
1952 WSASetLastError(WSAEFAULT);
1953 return SOCKET_ERROR;
1955 for (ptr = table, numInt = 0; ptr;
1956 ptr = ptr->Next, intArray++, numInt++)
1958 unsigned int addr, mask, bcast;
1959 struct ifreq ifInfo;
1961 /* Socket Status Flags */
1962 strncpy(ifInfo.ifr_name, ptr->AdapterName, IFNAMSIZ);
1963 ifInfo.ifr_name[IFNAMSIZ-1] = '\0';
1964 if (ioctl(fd, SIOCGIFFLAGS, &ifInfo) < 0)
1966 ERR("Error obtaining status flags for socket!\n");
1967 HeapFree(GetProcessHeap(),0,table);
1968 release_sock_fd( s, fd );
1969 WSASetLastError(WSAEINVAL);
1970 return SOCKET_ERROR;
1974 /* set flags; the values of IFF_* are not the same
1975 under Linux and Windows, therefore must generate
1977 intArray->iiFlags = 0;
1978 if (ifInfo.ifr_flags & IFF_BROADCAST)
1979 intArray->iiFlags |= WS_IFF_BROADCAST;
1980 #ifdef IFF_POINTOPOINT
1981 if (ifInfo.ifr_flags & IFF_POINTOPOINT)
1982 intArray->iiFlags |= WS_IFF_POINTTOPOINT;
1984 if (ifInfo.ifr_flags & IFF_LOOPBACK)
1985 intArray->iiFlags |= WS_IFF_LOOPBACK;
1986 if (ifInfo.ifr_flags & IFF_UP)
1987 intArray->iiFlags |= WS_IFF_UP;
1988 if (ifInfo.ifr_flags & IFF_MULTICAST)
1989 intArray->iiFlags |= WS_IFF_MULTICAST;
1992 addr = inet_addr(ptr->IpAddressList.IpAddress.String);
1993 mask = inet_addr(ptr->IpAddressList.IpMask.String);
1994 bcast = addr | (addr & !mask);
1995 intArray->iiAddress.AddressIn.sin_family = AF_INET;
1996 intArray->iiAddress.AddressIn.sin_port = 0;
1997 intArray->iiAddress.AddressIn.sin_addr.WS_s_addr =
1999 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
2000 intArray->iiNetmask.AddressIn.sin_port = 0;
2001 intArray->iiNetmask.AddressIn.sin_addr.WS_s_addr =
2003 intArray->iiBroadcastAddress.AddressIn.sin_family =
2005 intArray->iiBroadcastAddress.AddressIn.sin_port = 0;
2006 intArray->iiBroadcastAddress.AddressIn.sin_addr.
2012 ERR("Unable to get interface table!\n");
2013 release_sock_fd( s, fd );
2014 HeapFree(GetProcessHeap(),0,table);
2015 WSASetLastError(WSAEINVAL);
2016 return SOCKET_ERROR;
2018 HeapFree(GetProcessHeap(),0,table);
2022 release_sock_fd( s, fd );
2023 WSASetLastError(WSAEINVAL);
2024 return SOCKET_ERROR;
2029 ERR("Unable to get interface table!\n");
2030 release_sock_fd( s, fd );
2031 WSASetLastError(WSAEINVAL);
2032 return SOCKET_ERROR;
2034 /* Calculate the size of the array being returned */
2035 *lpcbBytesReturned = sizeof(INTERFACE_INFO) * numInt;
2039 case SIO_ADDRESS_LIST_CHANGE:
2040 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
2041 /* FIXME: error and return code depend on whether socket was created
2042 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
2046 WARN("\tunsupported WS_IOCTL cmd (%08lx)\n", dwIoControlCode);
2047 release_sock_fd( s, fd );
2048 WSASetLastError(WSAEOPNOTSUPP);
2049 return SOCKET_ERROR;
2052 /* Function executed with no errors */
2053 release_sock_fd( s, fd );
2058 /***********************************************************************
2059 * ioctlsocket (WS2_32.10)
2061 int WINAPI WS_ioctlsocket(SOCKET s, long cmd, u_long *argp)
2066 TRACE("socket %04x, cmd %08lx, ptr %p\n", s, cmd, argp);
2075 if( _get_sock_mask(s) )
2077 /* AsyncSelect()'ed sockets are always nonblocking */
2078 if (*argp) return 0;
2079 SetLastError(WSAEINVAL);
2080 return SOCKET_ERROR;
2082 fd = get_sock_fd( s, 0, NULL );
2088 _enable_event(SOCKET2HANDLE(s), 0, FD_WINE_NONBLOCKING, 0);
2089 ret = fcntl( fd, F_SETFL, O_NONBLOCK );
2093 _enable_event(SOCKET2HANDLE(s), 0, 0, FD_WINE_NONBLOCKING);
2094 ret = fcntl( fd, F_SETFL, 0 );
2096 release_sock_fd( s, fd );
2098 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2100 return SOCKET_ERROR;
2106 case WS__IOW('f',125,u_long):
2107 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
2108 SetLastError(WSAEINVAL);
2109 return SOCKET_ERROR;
2111 case SIOCGIFBRDADDR:
2112 case SIOCGIFNETMASK:
2114 /* These don't need any special handling. They are used by
2115 WsControl, and are here to suppress an unnecessary warning. */
2119 /* Netscape tries hard to use bogus ioctl 0x667e */
2120 WARN("\tunknown WS_IOCTL cmd (%08lx)\n", cmd);
2124 fd = get_sock_fd( s, 0, NULL );
2127 if( ioctl(fd, newcmd, (char*)argp ) == 0 )
2129 release_sock_fd( s, fd );
2132 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2133 release_sock_fd( s, fd );
2135 return SOCKET_ERROR;
2138 /***********************************************************************
2139 * listen (WS2_32.13)
2141 int WINAPI WS_listen(SOCKET s, int backlog)
2143 int fd = get_sock_fd( s, GENERIC_READ, NULL );
2145 TRACE("socket %04x, backlog %d\n", s, backlog);
2148 if (listen(fd, backlog) == 0)
2150 release_sock_fd( s, fd );
2151 _enable_event(SOCKET2HANDLE(s), FD_ACCEPT,
2153 FD_CONNECT|FD_WINE_CONNECTED);
2156 SetLastError(wsaErrno());
2157 release_sock_fd( s, fd );
2159 return SOCKET_ERROR;
2162 /***********************************************************************
2165 int WINAPI WS_recv(SOCKET s, char *buf, int len, int flags)
2167 DWORD n, dwFlags = flags;
2173 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, NULL, NULL, NULL, NULL) == SOCKET_ERROR )
2174 return SOCKET_ERROR;
2179 /***********************************************************************
2180 * recvfrom (WS2_32.17)
2182 int WINAPI WS_recvfrom(SOCKET s, char *buf, INT len, int flags,
2183 struct WS_sockaddr *from, int *fromlen)
2185 DWORD n, dwFlags = flags;
2191 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, from, fromlen, NULL, NULL) == SOCKET_ERROR )
2192 return SOCKET_ERROR;
2197 /***********************************************************************
2198 * select (WS2_32.18)
2200 int WINAPI WS_select(int nfds, WS_fd_set *ws_readfds,
2201 WS_fd_set *ws_writefds, WS_fd_set *ws_exceptfds,
2202 const struct WS_timeval* ws_timeout)
2205 fd_set readfds, writefds, exceptfds;
2206 fd_set *p_read, *p_write, *p_except;
2207 int readfd[FD_SETSIZE], writefd[FD_SETSIZE], exceptfd[FD_SETSIZE];
2208 struct timeval timeout, *timeoutaddr = NULL;
2210 TRACE("read %p, write %p, excp %p timeout %p\n",
2211 ws_readfds, ws_writefds, ws_exceptfds, ws_timeout);
2213 p_read = fd_set_import(&readfds, ws_readfds, GENERIC_READ, &highfd, readfd);
2214 p_write = fd_set_import(&writefds, ws_writefds, GENERIC_WRITE, &highfd, writefd);
2215 p_except = fd_set_import(&exceptfds, ws_exceptfds, 0, &highfd, exceptfd);
2218 timeoutaddr = &timeout;
2219 timeout.tv_sec=ws_timeout->tv_sec;
2220 timeout.tv_usec=ws_timeout->tv_usec;
2223 if( (highfd = select(highfd + 1, p_read, p_write, p_except, timeoutaddr)) > 0 )
2225 fd_set_export(&readfds, p_except, ws_readfds, readfd);
2226 fd_set_export(&writefds, p_except, ws_writefds, writefd);
2228 if (p_except && ws_exceptfds)
2232 for (i = j = 0; i < ws_exceptfds->fd_count; i++)
2234 int fd = exceptfd[i];
2235 SOCKET s = ws_exceptfds->fd_array[i];
2236 if (fd == -1) continue;
2237 if (FD_ISSET(fd, &exceptfds)) ws_exceptfds->fd_array[j++] = s;
2238 release_sock_fd( s, fd );
2240 ws_exceptfds->fd_count = j;
2244 fd_set_unimport(ws_readfds, readfd);
2245 fd_set_unimport(ws_writefds, writefd);
2246 fd_set_unimport(ws_exceptfds, exceptfd);
2248 if( highfd == 0 ) return 0;
2249 SetLastError(wsaErrno());
2250 return SOCKET_ERROR;
2254 /***********************************************************************
2257 int WINAPI WS_send(SOCKET s, const char *buf, int len, int flags)
2263 wsabuf.buf = (char*) buf;
2265 if ( WSASendTo( s, &wsabuf, 1, &n, flags, NULL, 0, NULL, NULL) == SOCKET_ERROR )
2266 return SOCKET_ERROR;
2271 /***********************************************************************
2272 * WSASend (WS2_32.72)
2274 INT WINAPI WSASend( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2275 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2276 LPWSAOVERLAPPED lpOverlapped,
2277 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2279 return WSASendTo( s, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags,
2280 NULL, 0, lpOverlapped, lpCompletionRoutine );
2283 /***********************************************************************
2284 * WSASendDisconnect (WS2_32.73)
2286 INT WINAPI WSASendDisconnect( SOCKET s, LPWSABUF lpBuffers )
2288 return WS_shutdown( s, SD_SEND );
2292 /***********************************************************************
2293 * WSASendTo (WS2_32.74)
2295 INT WINAPI WSASendTo( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2296 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2297 const struct WS_sockaddr *to, int tolen,
2298 LPWSAOVERLAPPED lpOverlapped,
2299 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2302 int n, fd, err = WSAENOTSOCK, flags, ret;
2303 struct iovec* iovec;
2304 struct ws2_async *wsa;
2305 IO_STATUS_BLOCK* iosb;
2307 TRACE("socket %04x, wsabuf %p, nbufs %ld, flags %ld, to %p, tolen %d, ovl %p, func %p\n",
2308 s, lpBuffers, dwBufferCount, dwFlags,
2309 to, tolen, lpOverlapped, lpCompletionRoutine);
2311 fd = get_sock_fd( s, GENERIC_WRITE, &flags );
2312 TRACE( "fd=%d, flags=%x\n", fd, flags );
2314 if ( fd == -1 ) return SOCKET_ERROR;
2316 if (flags & FD_FLAG_SEND_SHUTDOWN)
2318 WSASetLastError( WSAESHUTDOWN );
2322 if ( !lpNumberOfBytesSent )
2328 iovec = HeapAlloc(GetProcessHeap(), 0, dwBufferCount * sizeof(struct iovec) );
2336 for ( i = 0; i < dwBufferCount; i++ )
2338 iovec[i].iov_base = lpBuffers[i].buf;
2339 iovec[i].iov_len = lpBuffers[i].len;
2342 if ( (lpOverlapped || lpCompletionRoutine) && flags & FD_FLAG_OVERLAPPED )
2344 wsa = WS2_make_async( s, fd, ws2m_write, iovec, dwBufferCount,
2345 &dwFlags, (struct WS_sockaddr*) to, &tolen,
2346 lpOverlapped, lpCompletionRoutine, &iosb );
2353 if ( ( ret = ws2_queue_async( wsa, iosb ) ) )
2355 err = NtStatusToWSAError( ret );
2357 if ( !lpOverlapped )
2358 HeapFree( GetProcessHeap(), 0, iosb );
2359 HeapFree( GetProcessHeap(), 0, wsa );
2363 /* Try immediate completion */
2366 if ( WSAGetOverlappedResult( s, lpOverlapped,
2367 lpNumberOfBytesSent, FALSE, &dwFlags) )
2370 if ( (err = WSAGetLastError()) != WSA_IO_INCOMPLETE )
2374 WSASetLastError( WSA_IO_PENDING );
2375 return SOCKET_ERROR;
2378 if (_is_blocking(s))
2380 /* FIXME: exceptfds? */
2381 int timeout = GET_SNDTIMEO(fd);
2382 if( !do_block(fd, POLLOUT, timeout)) {
2384 goto err_free; /* msdn says a timeout in send is fatal */
2388 n = WS2_send( fd, iovec, dwBufferCount, to, tolen, dwFlags );
2392 if ( err == WSAEWOULDBLOCK )
2393 _enable_event(SOCKET2HANDLE(s), FD_WRITE, 0, 0);
2397 TRACE(" -> %i bytes\n", n);
2398 *lpNumberOfBytesSent = n;
2400 HeapFree( GetProcessHeap(), 0, iovec );
2401 release_sock_fd( s, fd );
2405 HeapFree( GetProcessHeap(), 0, iovec );
2408 release_sock_fd( s, fd );
2411 WARN(" -> ERROR %d\n", err);
2412 WSASetLastError(err);
2413 return SOCKET_ERROR;
2416 /***********************************************************************
2417 * sendto (WS2_32.20)
2419 int WINAPI WS_sendto(SOCKET s, const char *buf, int len, int flags,
2420 const struct WS_sockaddr *to, int tolen)
2426 wsabuf.buf = (char*) buf;
2428 if ( WSASendTo(s, &wsabuf, 1, &n, flags, to, tolen, NULL, NULL) == SOCKET_ERROR )
2429 return SOCKET_ERROR;
2434 /***********************************************************************
2435 * setsockopt (WS2_32.21)
2437 int WINAPI WS_setsockopt(SOCKET s, int level, int optname,
2438 const char *optval, int optlen)
2442 struct linger linger;
2443 struct timeval tval;
2445 TRACE("socket: %04x, level %d, name %d, ptr %p, len %d\n",
2446 s, level, optname, optval, optlen);
2448 /* SO_OPENTYPE does not require a valid socket handle. */
2449 if (level == WS_SOL_SOCKET && optname == WS_SO_OPENTYPE)
2451 if (optlen < sizeof(int) || !optval)
2453 SetLastError(WSAEFAULT);
2454 return SOCKET_ERROR;
2456 get_per_thread_data()->opentype = *(int *)optval;
2457 TRACE("setting global SO_OPENTYPE to 0x%x\n", *(int *)optval );
2461 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
2462 * socket. This will either not happen under windows or it is ignored in
2463 * windows (but it works in linux and therefore prevents the game from
2464 * finding games outside the current network) */
2465 if ( level==WS_SOL_SOCKET && optname==WS_SO_DONTROUTE )
2467 FIXME("Does windows ignore SO_DONTROUTE?\n");
2472 if(level == NSPROTO_IPX)
2477 fd = get_sock_fd( s, 0, NULL );
2478 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(int*)optval, fd);
2480 /* We try to set the ipx type on ipx socket level. */
2482 if(setsockopt(fd, SOL_IPX, IPX_TYPE, optval, optlen) == -1)
2484 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
2485 return SOCKET_ERROR;
2490 /* Should we retrieve val using a getsockopt call and then
2491 * set the modified one? */
2492 val.ipx_pt = *optval;
2493 setsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, sizeof(struct ipx));
2496 release_sock_fd( s, fd );
2498 case IPX_FILTERPTYPE:
2499 /* Sets the receive filter packet type, at the moment we don't support it */
2500 FIXME("IPX_FILTERPTYPE: %x\n", *optval);
2502 /* Returning 0 is better for now than returning a SOCKET_ERROR */
2505 FIXME("opt_name:%x\n", optname);
2506 return SOCKET_ERROR;
2512 /* Is a privileged and useless operation, so we don't. */
2513 if ((optname == WS_SO_DEBUG) && (level == WS_SOL_SOCKET))
2515 FIXME("(%d,SOL_SOCKET,SO_DEBUG,%p(%ld)) attempted (is privileged). Ignoring.\n",s,optval,*(DWORD*)optval);
2519 if(optname == WS_SO_DONTLINGER && level == WS_SOL_SOCKET) {
2520 /* This is unique to WinSock and takes special conversion */
2521 linger.l_onoff = *((int*)optval) ? 0: 1;
2522 linger.l_linger = 0;
2524 optval = (char*)&linger;
2525 optlen = sizeof(struct linger);
2530 if (!convert_sockopt(&level, &optname)) {
2531 ERR("Invalid level (%d) or optname (%d)\n", level, optname);
2532 SetLastError(WSAENOPROTOOPT);
2533 return SOCKET_ERROR;
2535 if (optname == SO_LINGER && optval) {
2536 linger.l_onoff = ((LINGER*)optval)->l_onoff;
2537 linger.l_linger = ((LINGER*)optval)->l_linger;
2538 /* FIXME: what is documented behavior if SO_LINGER optval
2540 optval = (char*)&linger;
2541 optlen = sizeof(struct linger);
2543 else if (optval && optlen < sizeof(int))
2545 woptval= *((INT16 *) optval);
2546 optval= (char*) &woptval;
2549 if (level == SOL_SOCKET && is_timeout_option(optname))
2551 if (optlen == sizeof(UINT32)) {
2552 /* WinSock passes miliseconds instead of struct timeval */
2553 tval.tv_usec = (*(PUINT32)optval % 1000) * 1000;
2554 tval.tv_sec = *(PUINT32)optval / 1000;
2555 /* min of 500 milisec */
2556 if (tval.tv_sec == 0 && tval.tv_usec < 500000)
2557 tval.tv_usec = 500000;
2558 optlen = sizeof(struct timeval);
2559 optval = (char*)&tval;
2560 } else if (optlen == sizeof(struct timeval)) {
2561 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen);
2563 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen);
2567 if (level == SOL_SOCKET && optname == SO_RCVBUF && *(int*)optval < 2048)
2569 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(int*)optval );
2575 fd = get_sock_fd( s, 0, NULL );
2576 if (fd == -1) return SOCKET_ERROR;
2578 if (setsockopt(fd, level, optname, optval, optlen) == 0)
2580 release_sock_fd( s, fd );
2583 TRACE("Setting socket error, %d\n", wsaErrno());
2584 SetLastError(wsaErrno());
2585 release_sock_fd( s, fd );
2586 return SOCKET_ERROR;
2589 /***********************************************************************
2590 * shutdown (WS2_32.22)
2592 int WINAPI WS_shutdown(SOCKET s, int how)
2594 int fd, fd0 = -1, fd1 = -1, flags, err = WSAENOTSOCK;
2595 unsigned int clear_flags = 0;
2597 fd = get_sock_fd( s, 0, &flags );
2598 TRACE("socket %04x, how %i %x\n", s, how, flags );
2601 return SOCKET_ERROR;
2605 case 0: /* drop receives */
2606 clear_flags |= FD_READ;
2608 case 1: /* drop sends */
2609 clear_flags |= FD_WRITE;
2611 case 2: /* drop all */
2612 clear_flags |= FD_READ|FD_WRITE;
2614 clear_flags |= FD_WINE_LISTENING;
2617 if ( flags & FD_FLAG_OVERLAPPED ) {
2630 fd1 = get_sock_fd( s, 0, NULL );
2636 err = WS2_register_async_shutdown( s, fd0, ws2m_sd_read );
2639 release_sock_fd( s, fd0 );
2645 err = WS2_register_async_shutdown( s, fd1, ws2m_sd_write );
2648 release_sock_fd( s, fd1 );
2653 else /* non-overlapped mode */
2655 if ( shutdown( fd, how ) )
2658 release_sock_fd( s, fd );
2661 release_sock_fd( s, fd );
2664 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
2665 if ( how > 1) WSAAsyncSelect( s, 0, 0, 0 );
2669 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
2670 WSASetLastError( err );
2671 return SOCKET_ERROR;
2674 /***********************************************************************
2675 * socket (WS2_32.23)
2677 SOCKET WINAPI WS_socket(int af, int type, int protocol)
2679 TRACE("af=%d type=%d protocol=%d\n", af, type, protocol);
2681 return WSASocketA( af, type, protocol, NULL, 0,
2682 get_per_thread_data()->opentype ? 0 : WSA_FLAG_OVERLAPPED );
2686 /***********************************************************************
2687 * gethostbyaddr (WS2_32.51)
2689 struct WS_hostent* WINAPI WS_gethostbyaddr(const char *addr, int len, int type)
2691 struct WS_hostent *retval = NULL;
2692 struct hostent* host;
2694 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2697 struct hostent hostentry;
2700 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2702 int res = gethostbyaddr_r(addr, len, type,
2703 &hostentry, extrabuf, ebufsize, &host, &locerr);
2704 if( res != ERANGE) break;
2706 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2708 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2710 EnterCriticalSection( &csWSgetXXXbyYYY );
2711 host = gethostbyaddr(addr, len, type);
2712 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2714 if( host != NULL ) retval = WS_dup_he(host);
2715 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2716 HeapFree(GetProcessHeap(),0,extrabuf);
2718 LeaveCriticalSection( &csWSgetXXXbyYYY );
2720 TRACE("ptr %p, len %d, type %d ret %p\n", addr, len, type, retval);
2724 /***********************************************************************
2725 * gethostbyname (WS2_32.52)
2727 struct WS_hostent* WINAPI WS_gethostbyname(const char* name)
2729 struct WS_hostent *retval = NULL;
2730 struct hostent* host;
2731 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2734 struct hostent hostentry;
2735 int locerr = ENOBUFS;
2740 if( gethostname( buf, 100) == -1) {
2741 SetLastError( WSAENOBUFS); /* appropriate ? */
2745 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2747 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2749 int res = gethostbyname_r(name, &hostentry, extrabuf, ebufsize, &host, &locerr);
2750 if( res != ERANGE) break;
2752 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2754 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2756 EnterCriticalSection( &csWSgetXXXbyYYY );
2757 host = gethostbyname(name);
2758 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2760 if (host) retval = WS_dup_he(host);
2761 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2762 HeapFree(GetProcessHeap(),0,extrabuf);
2764 LeaveCriticalSection( &csWSgetXXXbyYYY );
2766 TRACE( "%s ret %p\n", debugstr_a(name), retval );
2771 /***********************************************************************
2772 * getprotobyname (WS2_32.53)
2774 struct WS_protoent* WINAPI WS_getprotobyname(const char* name)
2776 struct WS_protoent* retval = NULL;
2777 #ifdef HAVE_GETPROTOBYNAME
2778 struct protoent* proto;
2779 EnterCriticalSection( &csWSgetXXXbyYYY );
2780 if( (proto = getprotobyname(name)) != NULL )
2782 retval = WS_dup_pe(proto);
2785 MESSAGE("protocol %s not found; You might want to add "
2786 "this to /etc/protocols\n", debugstr_a(name) );
2787 SetLastError(WSANO_DATA);
2789 LeaveCriticalSection( &csWSgetXXXbyYYY );
2791 TRACE( "%s ret %p\n", debugstr_a(name), retval );
2796 /***********************************************************************
2797 * getprotobynumber (WS2_32.54)
2799 struct WS_protoent* WINAPI WS_getprotobynumber(int number)
2801 struct WS_protoent* retval = NULL;
2802 #ifdef HAVE_GETPROTOBYNUMBER
2803 struct protoent* proto;
2804 EnterCriticalSection( &csWSgetXXXbyYYY );
2805 if( (proto = getprotobynumber(number)) != NULL )
2807 retval = WS_dup_pe(proto);
2810 MESSAGE("protocol number %d not found; You might want to add "
2811 "this to /etc/protocols\n", number );
2812 SetLastError(WSANO_DATA);
2814 LeaveCriticalSection( &csWSgetXXXbyYYY );
2816 TRACE("%i ret %p\n", number, retval);
2821 /***********************************************************************
2822 * getservbyname (WS2_32.55)
2824 struct WS_servent* WINAPI WS_getservbyname(const char *name, const char *proto)
2826 struct WS_servent* retval = NULL;
2827 struct servent* serv;
2829 char *proto_str = NULL;
2831 if (!(name_str = strdup_lower(name))) return NULL;
2833 if (proto && *proto)
2835 if (!(proto_str = strdup_lower(proto)))
2837 HeapFree( GetProcessHeap(), 0, name_str );
2842 EnterCriticalSection( &csWSgetXXXbyYYY );
2843 serv = getservbyname(name_str, proto_str);
2846 retval = WS_dup_se(serv);
2848 else SetLastError(WSANO_DATA);
2849 LeaveCriticalSection( &csWSgetXXXbyYYY );
2850 HeapFree( GetProcessHeap(), 0, proto_str );
2851 HeapFree( GetProcessHeap(), 0, name_str );
2852 TRACE( "%s, %s ret %p\n", debugstr_a(name), debugstr_a(proto), retval );
2857 /***********************************************************************
2858 * getservbyport (WS2_32.56)
2860 struct WS_servent* WINAPI WS_getservbyport(int port, const char *proto)
2862 struct WS_servent* retval = NULL;
2863 #ifdef HAVE_GETSERVBYPORT
2864 struct servent* serv;
2865 char *proto_str = NULL;
2867 if (proto && *proto)
2869 if (!(proto_str = strdup_lower(proto))) return NULL;
2871 EnterCriticalSection( &csWSgetXXXbyYYY );
2872 if( (serv = getservbyport(port, proto_str)) != NULL ) {
2873 retval = WS_dup_se(serv);
2875 else SetLastError(WSANO_DATA);
2876 LeaveCriticalSection( &csWSgetXXXbyYYY );
2877 HeapFree( GetProcessHeap(), 0, proto_str );
2879 TRACE("%d (i.e. port %d), %s ret %p\n", port, (int)ntohl(port), debugstr_a(proto), retval);
2884 /***********************************************************************
2885 * gethostname (WS2_32.57)
2887 int WINAPI WS_gethostname(char *name, int namelen)
2889 TRACE("name %p, len %d\n", name, namelen);
2891 if (gethostname(name, namelen) == 0)
2893 TRACE("<- '%s'\n", name);
2896 SetLastError((errno == EINVAL) ? WSAEFAULT : wsaErrno());
2897 TRACE("<- ERROR !\n");
2898 return SOCKET_ERROR;
2902 /* ------------------------------------- Windows sockets extensions -- *
2904 * ------------------------------------------------------------------- */
2906 /***********************************************************************
2907 * WSAEnumNetworkEvents (WS2_32.36)
2909 int WINAPI WSAEnumNetworkEvents(SOCKET s, WSAEVENT hEvent, LPWSANETWORKEVENTS lpEvent)
2913 TRACE("%08x, hEvent %p, lpEvent %08x\n", s, hEvent, (unsigned)lpEvent );
2915 SERVER_START_REQ( get_socket_event )
2917 req->handle = SOCKET2HANDLE(s);
2918 req->service = TRUE;
2919 req->c_event = hEvent;
2920 wine_server_set_reply( req, lpEvent->iErrorCode, sizeof(lpEvent->iErrorCode) );
2921 if (!(ret = wine_server_call(req))) lpEvent->lNetworkEvents = reply->pmask & reply->mask;
2925 SetLastError(WSAEINVAL);
2926 return SOCKET_ERROR;
2929 /***********************************************************************
2930 * WSAEventSelect (WS2_32.39)
2932 int WINAPI WSAEventSelect(SOCKET s, WSAEVENT hEvent, long lEvent)
2936 TRACE("%08x, hEvent %p, event %08x\n", s, hEvent, (unsigned)lEvent );
2938 SERVER_START_REQ( set_socket_event )
2940 req->handle = SOCKET2HANDLE(s);
2942 req->event = hEvent;
2945 ret = wine_server_call( req );
2949 SetLastError(WSAEINVAL);
2950 return SOCKET_ERROR;
2953 /**********************************************************************
2954 * WSAGetOverlappedResult (WS2_32.40)
2956 BOOL WINAPI WSAGetOverlappedResult( SOCKET s, LPWSAOVERLAPPED lpOverlapped,
2957 LPDWORD lpcbTransfer, BOOL fWait,
2962 TRACE( "socket %04x ovl %p trans %p, wait %d flags %p\n",
2963 s, lpOverlapped, lpcbTransfer, fWait, lpdwFlags );
2965 if ( lpOverlapped == NULL )
2967 ERR( "Invalid pointer\n" );
2968 WSASetLastError(WSA_INVALID_PARAMETER);
2974 if (lpOverlapped->hEvent)
2975 while ( WaitForSingleObjectEx(lpOverlapped->hEvent,
2976 INFINITE, TRUE) == STATUS_USER_APC );
2977 else /* busy loop */
2978 while ( ((volatile OVERLAPPED*)lpOverlapped)->Internal == STATUS_PENDING )
2982 else if ( lpOverlapped->Internal == STATUS_PENDING )
2984 /* Wait in order to give APCs a chance to run. */
2985 /* This is cheating, so we must set the event again in case of success -
2986 it may be a non-manual reset event. */
2987 while ( (r = WaitForSingleObjectEx(lpOverlapped->hEvent, 0, TRUE)) == STATUS_USER_APC );
2988 if ( r == WAIT_OBJECT_0 && lpOverlapped->hEvent )
2989 NtSetEvent( lpOverlapped->hEvent, NULL );
2993 *lpcbTransfer = lpOverlapped->InternalHigh;
2996 *lpdwFlags = lpOverlapped->u.s.Offset;
2998 switch ( lpOverlapped->Internal )
3000 case STATUS_SUCCESS:
3002 case STATUS_PENDING:
3003 WSASetLastError( WSA_IO_INCOMPLETE );
3004 if (fWait) ERR("PENDING status after waiting!\n");
3007 WSASetLastError( NtStatusToWSAError( lpOverlapped->Internal ));
3013 /***********************************************************************
3014 * WSAAsyncSelect (WS2_32.101)
3016 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, long lEvent)
3020 TRACE("%x, hWnd %p, uMsg %08x, event %08lx\n", s, hWnd, uMsg, lEvent );
3022 SERVER_START_REQ( set_socket_event )
3024 req->handle = SOCKET2HANDLE(s);
3029 ret = wine_server_call( req );
3033 SetLastError(WSAEINVAL);
3034 return SOCKET_ERROR;
3037 /***********************************************************************
3038 * WSACreateEvent (WS2_32.31)
3041 WSAEVENT WINAPI WSACreateEvent(void)
3043 /* Create a manual-reset event, with initial state: unsignaled */
3046 return CreateEventW(NULL, TRUE, FALSE, NULL);
3049 /***********************************************************************
3050 * WSACloseEvent (WS2_32.29)
3053 BOOL WINAPI WSACloseEvent(WSAEVENT event)
3055 TRACE ("event=%p\n", event);
3057 return CloseHandle(event);
3060 /***********************************************************************
3061 * WSASocketA (WS2_32.78)
3064 SOCKET WINAPI WSASocketA(int af, int type, int protocol,
3065 LPWSAPROTOCOL_INFOA lpProtocolInfo,
3066 GROUP g, DWORD dwFlags)
3069 WSAPROTOCOL_INFOW info;
3071 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%lx\n",
3072 af, type, protocol, lpProtocolInfo, g, dwFlags);
3074 if (!lpProtocolInfo) return WSASocketW(af, type, protocol, NULL, g, dwFlags);
3076 memcpy(&info, lpProtocolInfo, FIELD_OFFSET(WSAPROTOCOL_INFOW, szProtocol));
3077 len = MultiByteToWideChar(CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
3078 info.szProtocol, WSAPROTOCOL_LEN * sizeof(WCHAR) + 1);
3082 WSASetLastError( WSAEINVAL);
3083 return SOCKET_ERROR;
3086 return WSASocketW(af, type, protocol, &info, g, dwFlags);
3089 /***********************************************************************
3090 * WSASocketW (WS2_32.79)
3093 SOCKET WINAPI WSASocketW(int af, int type, int protocol,
3094 LPWSAPROTOCOL_INFOW lpProtocolInfo,
3095 GROUP g, DWORD dwFlags)
3100 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
3101 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
3104 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%lx\n",
3105 af, type, protocol, lpProtocolInfo, g, dwFlags );
3107 /* hack for WSADuplicateSocket */
3108 if (lpProtocolInfo && lpProtocolInfo->dwServiceFlags4 == 0xff00ff00) {
3109 ret = lpProtocolInfo->dwCatalogEntryId;
3110 TRACE("\tgot duplicate %04x\n", ret);
3114 /* check the socket family */
3118 case WS_AF_IPX: af = AF_IPX;
3124 SetLastError(WSAEAFNOSUPPORT);
3125 return INVALID_SOCKET;
3128 /* check the socket type */
3131 case WS_SOCK_STREAM:
3141 SetLastError(WSAESOCKTNOSUPPORT);
3142 return INVALID_SOCKET;
3145 /* check the protocol type */
3146 if ( protocol < 0 ) /* don't support negative values */
3148 SetLastError(WSAEPROTONOSUPPORT);
3149 return INVALID_SOCKET;
3152 if ( af == AF_UNSPEC) /* did they not specify the address family? */
3156 if (type == SOCK_STREAM) { af = AF_INET; break; }
3158 if (type == SOCK_DGRAM) { af = AF_INET; break; }
3159 default: SetLastError(WSAEPROTOTYPE); return INVALID_SOCKET;
3162 SERVER_START_REQ( create_socket )
3166 req->protocol = protocol;
3167 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
3168 req->flags = dwFlags;
3169 req->inherit = TRUE;
3170 set_error( wine_server_call( req ) );
3171 ret = HANDLE2SOCKET( reply->handle );
3176 TRACE("\tcreated %04x\n", ret );
3180 if (GetLastError() == WSAEACCES) /* raw socket denied */
3182 if (type == SOCK_RAW)
3183 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, will fail unless running as root\n");
3185 MESSAGE("WS_SOCKET: not enough privileges to create socket, try running as root\n");
3186 SetLastError(WSAESOCKTNOSUPPORT);
3189 WARN("\t\tfailed!\n");
3190 return INVALID_SOCKET;
3193 /***********************************************************************
3194 * WSAJoinLeaf (WS2_32.58)
3197 SOCKET WINAPI WSAJoinLeaf(
3199 const struct WS_sockaddr *addr,
3201 LPWSABUF lpCallerData,
3202 LPWSABUF lpCalleeData,
3208 return INVALID_SOCKET;
3211 /***********************************************************************
3212 * __WSAFDIsSet (WS2_32.151)
3214 int WINAPI __WSAFDIsSet(SOCKET s, WS_fd_set *set)
3216 int i = set->fd_count;
3218 TRACE("(%d,%8lx(%i))\n", s,(unsigned long)set, i);
3221 if (set->fd_array[i] == s) return 1;
3225 /***********************************************************************
3226 * WSAIsBlocking (WINSOCK.114)
3227 * WSAIsBlocking (WS2_32.114)
3229 BOOL WINAPI WSAIsBlocking(void)
3231 /* By default WinSock should set all its sockets to non-blocking mode
3232 * and poll in PeekMessage loop when processing "blocking" ones. This
3233 * function is supposed to tell if the program is in this loop. Our
3234 * blocking calls are truly blocking so we always return FALSE.
3236 * Note: It is allowed to call this function without prior WSAStartup().
3243 /***********************************************************************
3244 * WSACancelBlockingCall (WINSOCK.113)
3245 * WSACancelBlockingCall (WS2_32.113)
3247 INT WINAPI WSACancelBlockingCall(void)
3253 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x )
3255 FIXME("How was this called?\n");
3260 /***********************************************************************
3261 * WSASetBlockingHook (WS2_32.109)
3263 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
3265 FARPROC prev = blocking_hook;
3266 blocking_hook = lpBlockFunc;
3267 TRACE("hook %p\n", lpBlockFunc);
3272 /***********************************************************************
3273 * WSAUnhookBlockingHook (WS2_32.110)
3275 INT WINAPI WSAUnhookBlockingHook(void)
3277 blocking_hook = WSA_DefaultBlockingHook;
3282 /* ----------------------------------- end of API stuff */
3284 /* ----------------------------------- helper functions -
3286 * TODO: Merge WS_dup_..() stuff into one function that
3287 * would operate with a generic structure containing internal
3288 * pointers (via a template of some kind).
3291 static int list_size(char** l, int item_size)
3296 j += (item_size) ? item_size : strlen(l[i]) + 1;
3297 j += (i + 1) * sizeof(char*); }
3301 static int list_dup(char** l_src, char** l_to, int item_size)
3306 for (i = 0; l_src[i]; i++) ;
3307 p = (char *)(l_to + i + 1);
3308 for (i = 0; l_src[i]; i++)
3310 int count = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
3311 memcpy(p, l_src[i], count);
3316 return p - (char *)l_to;
3321 /* duplicate hostent entry
3322 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
3323 * Dito for protoent and servent.
3325 static struct WS_hostent *WS_dup_he(const struct hostent* p_he)
3328 struct WS_hostent *p_to;
3330 int size = (sizeof(*p_he) +
3331 strlen(p_he->h_name) + 1 +
3332 list_size(p_he->h_aliases, 0) +
3333 list_size(p_he->h_addr_list, p_he->h_length));
3335 if (!(p_to = check_buffer_he(size))) return NULL;
3336 p_to->h_addrtype = p_he->h_addrtype;
3337 p_to->h_length = p_he->h_length;
3339 p = (char *)(p_to + 1);
3341 strcpy(p, p_he->h_name);
3344 p_to->h_aliases = (char **)p;
3345 p += list_dup(p_he->h_aliases, p_to->h_aliases, 0);
3347 p_to->h_addr_list = (char **)p;
3348 list_dup(p_he->h_addr_list, p_to->h_addr_list, p_he->h_length);
3352 /* ----- protoent */
3354 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe)
3357 struct WS_protoent *p_to;
3359 int size = (sizeof(*p_pe) +
3360 strlen(p_pe->p_name) + 1 +
3361 list_size(p_pe->p_aliases, 0));
3363 if (!(p_to = check_buffer_pe(size))) return NULL;
3364 p_to->p_proto = p_pe->p_proto;
3366 p = (char *)(p_to + 1);
3368 strcpy(p, p_pe->p_name);
3371 p_to->p_aliases = (char **)p;
3372 list_dup(p_pe->p_aliases, p_to->p_aliases, 0);
3378 static struct WS_servent *WS_dup_se(const struct servent* p_se)
3381 struct WS_servent *p_to;
3383 int size = (sizeof(*p_se) +
3384 strlen(p_se->s_proto) + 1 +
3385 strlen(p_se->s_name) + 1 +
3386 list_size(p_se->s_aliases, 0));
3388 if (!(p_to = check_buffer_se(size))) return NULL;
3389 p_to->s_port = p_se->s_port;
3391 p = (char *)(p_to + 1);
3393 strcpy(p, p_se->s_name);
3397 strcpy(p, p_se->s_proto);
3400 p_to->s_aliases = (char **)p;
3401 list_dup(p_se->s_aliases, p_to->s_aliases, 0);
3405 /* ----------------------------------- error handling */
3409 int loc_errno = errno;
3410 WARN("errno %d, (%s).\n", loc_errno, strerror(loc_errno));
3414 case EINTR: return WSAEINTR;
3415 case EBADF: return WSAEBADF;
3417 case EACCES: return WSAEACCES;
3418 case EFAULT: return WSAEFAULT;
3419 case EINVAL: return WSAEINVAL;
3420 case EMFILE: return WSAEMFILE;
3421 case EWOULDBLOCK: return WSAEWOULDBLOCK;
3422 case EINPROGRESS: return WSAEINPROGRESS;
3423 case EALREADY: return WSAEALREADY;
3424 case ENOTSOCK: return WSAENOTSOCK;
3425 case EDESTADDRREQ: return WSAEDESTADDRREQ;
3426 case EMSGSIZE: return WSAEMSGSIZE;
3427 case EPROTOTYPE: return WSAEPROTOTYPE;
3428 case ENOPROTOOPT: return WSAENOPROTOOPT;
3429 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
3430 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
3431 case EOPNOTSUPP: return WSAEOPNOTSUPP;
3432 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
3433 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
3434 case EADDRINUSE: return WSAEADDRINUSE;
3435 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
3436 case ENETDOWN: return WSAENETDOWN;
3437 case ENETUNREACH: return WSAENETUNREACH;
3438 case ENETRESET: return WSAENETRESET;
3439 case ECONNABORTED: return WSAECONNABORTED;
3441 case ECONNRESET: return WSAECONNRESET;
3442 case ENOBUFS: return WSAENOBUFS;
3443 case EISCONN: return WSAEISCONN;
3444 case ENOTCONN: return WSAENOTCONN;
3445 case ESHUTDOWN: return WSAESHUTDOWN;
3446 case ETOOMANYREFS: return WSAETOOMANYREFS;
3447 case ETIMEDOUT: return WSAETIMEDOUT;
3448 case ECONNREFUSED: return WSAECONNREFUSED;
3449 case ELOOP: return WSAELOOP;
3450 case ENAMETOOLONG: return WSAENAMETOOLONG;
3451 case EHOSTDOWN: return WSAEHOSTDOWN;
3452 case EHOSTUNREACH: return WSAEHOSTUNREACH;
3453 case ENOTEMPTY: return WSAENOTEMPTY;
3455 case EPROCLIM: return WSAEPROCLIM;
3458 case EUSERS: return WSAEUSERS;
3461 case EDQUOT: return WSAEDQUOT;
3464 case ESTALE: return WSAESTALE;
3467 case EREMOTE: return WSAEREMOTE;
3470 /* just in case we ever get here and there are no problems */
3473 WARN("Unknown errno %d!\n", loc_errno);
3474 return WSAEOPNOTSUPP;
3478 UINT wsaHerrno(int loc_errno)
3481 WARN("h_errno %d.\n", loc_errno);
3485 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
3486 case TRY_AGAIN: return WSATRY_AGAIN;
3487 case NO_RECOVERY: return WSANO_RECOVERY;
3488 case NO_DATA: return WSANO_DATA;
3489 case ENOBUFS: return WSAENOBUFS;
3493 WARN("Unknown h_errno %d!\n", loc_errno);
3494 return WSAEOPNOTSUPP;
3499 /***********************************************************************
3500 * WSARecv (WS2_32.67)
3502 int WINAPI WSARecv(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
3503 LPDWORD NumberOfBytesReceived, LPDWORD lpFlags,
3504 LPWSAOVERLAPPED lpOverlapped,
3505 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
3507 return WSARecvFrom(s, lpBuffers, dwBufferCount, NumberOfBytesReceived, lpFlags,
3508 NULL, NULL, lpOverlapped, lpCompletionRoutine);
3511 /***********************************************************************
3512 * WSARecvFrom (WS2_32.69)
3514 INT WINAPI WSARecvFrom( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
3515 LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, struct WS_sockaddr *lpFrom,
3516 LPINT lpFromlen, LPWSAOVERLAPPED lpOverlapped,
3517 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
3521 int n, fd, err = WSAENOTSOCK, flags, ret;
3522 struct iovec* iovec;
3523 struct ws2_async *wsa;
3524 IO_STATUS_BLOCK* iosb;
3526 TRACE("socket %04x, wsabuf %p, nbufs %ld, flags %ld, from %p, fromlen %ld, ovl %p, func %p\n",
3527 s, lpBuffers, dwBufferCount, *lpFlags, lpFrom,
3528 (lpFromlen ? *lpFromlen : -1L),
3529 lpOverlapped, lpCompletionRoutine);
3531 fd = get_sock_fd( s, GENERIC_READ, &flags );
3532 TRACE( "fd=%d, flags=%x\n", fd, flags );
3534 if (fd == -1) return SOCKET_ERROR;
3536 if (flags & FD_FLAG_RECV_SHUTDOWN)
3538 WSASetLastError( WSAESHUTDOWN );
3542 iovec = HeapAlloc( GetProcessHeap(), 0, dwBufferCount * sizeof (struct iovec) );
3549 for (i = 0; i < dwBufferCount; i++)
3551 iovec[i].iov_base = lpBuffers[i].buf;
3552 iovec[i].iov_len = lpBuffers[i].len;
3555 if ( (lpOverlapped || lpCompletionRoutine) && flags & FD_FLAG_OVERLAPPED )
3557 wsa = WS2_make_async( s, fd, ws2m_read, iovec, dwBufferCount,
3558 lpFlags, lpFrom, lpFromlen,
3559 lpOverlapped, lpCompletionRoutine, &iosb );
3567 if ( ( ret = ws2_queue_async( wsa, iosb )) )
3569 err = NtStatusToWSAError( ret );
3571 if ( !lpOverlapped )
3572 HeapFree( GetProcessHeap(), 0, iosb );
3573 HeapFree( GetProcessHeap(), 0, wsa );
3577 /* Try immediate completion */
3580 if ( WSAGetOverlappedResult( s, lpOverlapped,
3581 lpNumberOfBytesRecvd, FALSE, lpFlags) )
3584 if ( (err = WSAGetLastError()) != WSA_IO_INCOMPLETE )
3588 WSASetLastError( WSA_IO_PENDING );
3589 return SOCKET_ERROR;
3592 if ( _is_blocking(s) )
3595 /* FIXME: OOB and exceptfds? */
3596 int timeout = GET_RCVTIMEO(fd);
3597 if( !do_block(fd, POLLIN, timeout)) {
3599 /* a timeout is not fatal */
3600 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
3605 n = WS2_recv( fd, iovec, dwBufferCount, lpFrom, lpFromlen, lpFlags );
3612 TRACE(" -> %i bytes\n", n);
3613 *lpNumberOfBytesRecvd = n;
3615 HeapFree(GetProcessHeap(), 0, iovec);
3616 release_sock_fd( s, fd );
3617 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
3622 HeapFree(GetProcessHeap(), 0, iovec);
3625 release_sock_fd( s, fd );
3628 WARN(" -> ERROR %d\n", err);
3629 WSASetLastError( err );
3630 return SOCKET_ERROR;
3633 /***********************************************************************
3634 * WSCInstallProvider (WS2_32.88)
3636 INT WINAPI WSCInstallProvider( const LPGUID lpProviderId,
3637 LPCWSTR lpszProviderDllPath,
3638 const LPWSAPROTOCOL_INFOW lpProtocolInfoList,
3639 DWORD dwNumberOfEntries,
3642 FIXME("(%s, %s, %p, %ld, %p): stub !\n", debugstr_guid(lpProviderId),
3643 debugstr_w(lpszProviderDllPath), lpProtocolInfoList,
3644 dwNumberOfEntries, lpErrno);
3650 /***********************************************************************
3651 * WSCDeinstallProvider (WS2_32.83)
3653 INT WINAPI WSCDeinstallProvider(LPGUID lpProviderId, LPINT lpErrno)
3655 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId), lpErrno);
3661 /***********************************************************************
3662 * WSAAccept (WS2_32.26)
3664 SOCKET WINAPI WSAAccept( SOCKET s, struct WS_sockaddr *addr, LPINT addrlen,
3665 LPCONDITIONPROC lpfnCondition, DWORD dwCallbackData)
3668 int ret = 0, size = 0;
3669 WSABUF CallerId, CallerData, CalleeId, CalleeData;
3670 /* QOS SQOS, GQOS; */
3673 SOCKADDR src_addr, dst_addr;
3675 TRACE("Socket %04x, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %ld\n",
3676 s, addr, addrlen, lpfnCondition, dwCallbackData);
3679 size = sizeof(src_addr);
3680 cs = WS_accept(s, &src_addr, &size);
3682 if (cs == SOCKET_ERROR) return SOCKET_ERROR;
3684 CallerId.buf = (char *)&src_addr;
3685 CallerId.len = sizeof(src_addr);
3687 CallerData.buf = NULL;
3688 CallerData.len = (ULONG)NULL;
3690 WS_getsockname(cs, &dst_addr, &size);
3692 CalleeId.buf = (char *)&dst_addr;
3693 CalleeId.len = sizeof(dst_addr);
3696 ret = (*lpfnCondition)(&CallerId, &CallerData, NULL, NULL,
3697 &CalleeId, &CalleeData, &g, dwCallbackData);
3702 if (addr && addrlen)
3703 addr = memcpy(addr, &src_addr, (*addrlen > size) ? size : *addrlen );
3706 SERVER_START_REQ( set_socket_deferred )
3708 req->handle = SOCKET2HANDLE(s);
3709 req->deferred = SOCKET2HANDLE(cs);
3710 if ( !wine_server_call_err ( req ) )
3712 SetLastError( WSATRY_AGAIN );
3713 WS_closesocket( cs );
3717 return SOCKET_ERROR;
3720 SetLastError(WSAECONNREFUSED);
3721 return SOCKET_ERROR;
3723 FIXME("Unknown return type from Condition function\n");
3724 SetLastError(WSAENOTSOCK);
3725 return SOCKET_ERROR;
3729 /***********************************************************************
3730 * WSADuplicateSocketA (WS2_32.32)
3732 int WINAPI WSADuplicateSocketA( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOA lpProtocolInfo )
3736 TRACE("(%d,%lx,%p)\n", s, dwProcessId, lpProtocolInfo);
3737 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
3738 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
3739 /* I don't know what the real Windoze does next, this is a hack */
3740 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
3741 * the target use the global duplicate, or we could copy a reference to us to the structure
3742 * and let the target duplicate it from us, but let's do it as simple as possible */
3743 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
3744 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
3745 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
3746 0, FALSE, DUPLICATE_SAME_ACCESS);
3747 CloseHandle(hProcess);
3748 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
3752 /***********************************************************************
3753 * WSADuplicateSocketW (WS2_32.33)
3755 int WINAPI WSADuplicateSocketW( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOW lpProtocolInfo )
3759 TRACE("(%d,%lx,%p)\n", s, dwProcessId, lpProtocolInfo);
3761 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
3762 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
3763 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
3764 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
3765 0, FALSE, DUPLICATE_SAME_ACCESS);
3766 CloseHandle(hProcess);
3767 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
3771 /***********************************************************************
3772 * WSAInstallServiceClassA (WS2_32.48)
3774 int WINAPI WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info)
3776 FIXME("Request to install service %s\n",debugstr_a(info->lpszServiceClassName));
3777 WSASetLastError(WSAEACCES);
3778 return SOCKET_ERROR;
3781 /***********************************************************************
3782 * WSAInstallServiceClassW (WS2_32.49)
3784 int WINAPI WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info)
3786 FIXME("Request to install service %s\n",debugstr_w(info->lpszServiceClassName));
3787 WSASetLastError(WSAEACCES);
3788 return SOCKET_ERROR;
3791 /***********************************************************************
3792 * WSARemoveServiceClass (WS2_32.70)
3794 int WINAPI WSARemoveServiceClass(LPGUID info)
3796 FIXME("Request to remove service %p\n",info);
3797 WSASetLastError(WSATYPE_NOT_FOUND);
3798 return SOCKET_ERROR;
3801 /***********************************************************************
3802 * WSAStringToAddressA (WS2_32.80)
3804 INT WINAPI WSAStringToAddressA(LPSTR AddressString,
3806 LPWSAPROTOCOL_INFOA lpProtocolInfo,
3807 LPSOCKADDR lpAddress,
3808 LPINT lpAddressLength)
3812 LPSTR workBuffer=NULL,ptrPort;
3814 TRACE( "(%s, %x, %p, %p, %p)\n", AddressString, AddressFamily, lpProtocolInfo,
3815 lpAddress, lpAddressLength );
3817 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
3821 workBuffer = HeapAlloc( GetProcessHeap(), 0, strlen(AddressString)+1 );
3824 strcpy(workBuffer,AddressString);
3825 switch (AddressFamily)
3828 /* caller wants to know the size of the socket buffer */
3829 if (*lpAddressLength < sizeof(SOCKADDR_IN))
3831 *lpAddressLength = sizeof(SOCKADDR_IN);
3836 /* caller wants to translate an AdressString into a SOCKADDR */
3839 memset(lpAddress,0,sizeof(SOCKADDR_IN));
3840 ((LPSOCKADDR_IN)lpAddress)->sin_family = AF_INET;
3841 ptrPort = strchr(workBuffer,':');
3844 ((LPSOCKADDR_IN)lpAddress)->sin_port = (u_short)atoi(ptrPort+1);
3848 ((LPSOCKADDR_IN)lpAddress)->sin_port = 0;
3849 inetaddr = inet_addr(workBuffer);
3850 if (inetaddr != INADDR_NONE)
3852 ((LPSOCKADDR_IN)lpAddress)->sin_addr.WS_s_addr = inetaddr;
3860 FIXME("(%s, %x, %p, %p, %p) - ProtocolInfo not implemented!\n",
3861 AddressString, AddressFamily,
3862 lpProtocolInfo, lpAddress, lpAddressLength);
3866 FIXME("(%s, %x, %p, %p, %p) - AddressFamiliy not implemented!\n",
3867 AddressString, AddressFamily,
3868 lpProtocolInfo, lpAddress, lpAddressLength);
3870 HeapFree( GetProcessHeap(), 0, workBuffer );
3873 res = WSA_NOT_ENOUGH_MEMORY;
3879 WSASetLastError(res);
3880 return SOCKET_ERROR;
3883 /***********************************************************************
3884 * WSAStringToAddressW (WS2_32.81)
3886 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
3887 * If this should be the case, it would be required to map these digits
3888 * to Unicode digits (0-9) using FoldString first.
3890 INT WINAPI WSAStringToAddressW(LPWSTR AddressString,
3892 LPWSAPROTOCOL_INFOW lpProtocolInfo,
3893 LPSOCKADDR lpAddress,
3894 LPINT lpAddressLength)
3897 LPSTR workBuffer=NULL;
3898 WSAPROTOCOL_INFOA infoA;
3899 LPWSAPROTOCOL_INFOA lpProtoInfoA = NULL;
3901 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString), AddressFamily, lpProtocolInfo,
3902 lpAddress, lpAddressLength );
3904 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
3906 /* if ProtocolInfo is available - convert to ANSI variant */
3909 lpProtoInfoA = &infoA;
3910 memcpy( lpProtoInfoA, lpProtocolInfo, FIELD_OFFSET( WSAPROTOCOL_INFOA, szProtocol ) );
3912 if (!WideCharToMultiByte( CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
3913 lpProtoInfoA->szProtocol, WSAPROTOCOL_LEN+1, NULL, NULL ))
3915 WSASetLastError( WSAEINVAL);
3916 return SOCKET_ERROR;
3922 /* Translate AddressString to ANSI code page - assumes that only
3923 standard digits 0-9 are used with this API call */
3924 sBuffer = WideCharToMultiByte( CP_ACP, 0, AddressString, -1, NULL, 0, NULL, NULL );
3925 workBuffer = HeapAlloc( GetProcessHeap(), 0, sBuffer );
3929 WideCharToMultiByte( CP_ACP, 0, AddressString, -1, workBuffer, sBuffer, NULL, NULL );
3930 res = WSAStringToAddressA(workBuffer,AddressFamily,lpProtoInfoA,
3931 lpAddress,lpAddressLength);
3932 HeapFree( GetProcessHeap(), 0, workBuffer );
3936 res = WSA_NOT_ENOUGH_MEMORY;
3941 WSASetLastError(res);
3942 return SOCKET_ERROR;
3945 /***********************************************************************
3946 * WSAAddressToStringA (WS2_32.27)
3948 * See WSAAddressToStringW
3950 INT WINAPI WSAAddressToStringA( LPSOCKADDR sockaddr, DWORD len,
3951 LPWSAPROTOCOL_INFOA info, LPSTR string,
3955 CHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
3958 TRACE( "(%p, %lx, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
3960 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
3961 if (!string || !lenstr) return SOCKET_ERROR;
3963 /* sin_family is garanteed to be the first u_short */
3964 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
3966 sprintf( buffer, "%u.%u.%u.%u:%u",
3967 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
3968 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
3969 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
3970 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
3971 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
3973 p = strchr( buffer, ':' );
3974 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
3976 size = strlen( buffer );
3981 return SOCKET_ERROR;
3984 strcpy( string, buffer );
3988 /***********************************************************************
3989 * WSAAddressToStringW (WS2_32.28)
3991 * Convert a sockaddr address into a readable address string.
3994 * sockaddr [I] Pointer to a sockaddr structure.
3995 * len [I] Size of the sockaddr structure.
3996 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
3997 * string [I/O] Pointer to a buffer to receive the address string.
3998 * lenstr [I/O] Size of the receive buffer in WCHARs.
4002 * Failure: SOCKET_ERROR
4005 * The 'info' parameter is ignored.
4008 * Only supports AF_INET addresses.
4010 INT WINAPI WSAAddressToStringW( LPSOCKADDR sockaddr, DWORD len,
4011 LPWSAPROTOCOL_INFOW info, LPWSTR string,
4015 WCHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4016 static const WCHAR format[] = { '%','u','.','%','u','.','%','u','.','%','u',':','%','u',0 };
4019 TRACE( "(%p, %lx, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
4021 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
4022 if (!string || !lenstr) return SOCKET_ERROR;
4024 /* sin_family is garanteed to be the first u_short */
4025 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
4027 sprintfW( buffer, format,
4028 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
4029 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
4030 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
4031 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
4032 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
4034 p = strchrW( buffer, ':' );
4035 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
4037 size = lstrlenW( buffer );
4042 return SOCKET_ERROR;
4045 lstrcpyW( string, buffer );
4049 /***********************************************************************
4050 * WSAEnumNameSpaceProvidersA (WS2_32.34)
4052 INT WINAPI WSAEnumNameSpaceProvidersA( LPDWORD len, LPWSANAMESPACE_INFOA buffer )
4054 FIXME( "(%p %p) Stub!\n", len, buffer );
4058 /***********************************************************************
4059 * WSAEnumNameSpaceProvidersW (WS2_32.35)
4061 INT WINAPI WSAEnumNameSpaceProvidersW( LPDWORD len, LPWSANAMESPACE_INFOW buffer )
4063 FIXME( "(%p %p) Stub!\n", len, buffer );
4067 /***********************************************************************
4068 * WSAGetQOSByName (WS2_32.41)
4070 BOOL WINAPI WSAGetQOSByName( SOCKET s, LPWSABUF lpQOSName, LPQOS lpQOS )
4072 FIXME( "(0x%04x %p %p) Stub!\n", s, lpQOSName, lpQOS );
4076 /***********************************************************************
4077 * WSAGetServiceClassInfoA (WS2_32.42)
4079 INT WINAPI WSAGetServiceClassInfoA( LPGUID provider, LPGUID service, LPDWORD len,
4080 LPWSASERVICECLASSINFOA info )
4082 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider), debugstr_guid(service),
4084 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4085 return SOCKET_ERROR;
4088 /***********************************************************************
4089 * WSAGetServiceClassInfoW (WS2_32.43)
4091 INT WINAPI WSAGetServiceClassInfoW( LPGUID provider, LPGUID service, LPDWORD len,
4092 LPWSASERVICECLASSINFOW info )
4094 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider), debugstr_guid(service),
4096 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4097 return SOCKET_ERROR;
4100 /***********************************************************************
4101 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
4103 INT WINAPI WSAGetServiceClassNameByClassIdA( LPGUID class, LPSTR service, LPDWORD len )
4105 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4106 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4107 return SOCKET_ERROR;
4110 /***********************************************************************
4111 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
4113 INT WINAPI WSAGetServiceClassNameByClassIdW( LPGUID class, LPWSTR service, LPDWORD len )
4115 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4116 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4117 return SOCKET_ERROR;
4120 /***********************************************************************
4121 * WSALookupServiceBeginA (WS2_32.59)
4123 INT WINAPI WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions,
4124 DWORD dwControlFlags,
4127 FIXME("(%p 0x%08lx %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4129 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4130 return SOCKET_ERROR;
4133 /***********************************************************************
4134 * WSALookupServiceBeginW (WS2_32.60)
4136 INT WINAPI WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions,
4137 DWORD dwControlFlags,
4140 FIXME("(%p 0x%08lx %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4142 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4143 return SOCKET_ERROR;
4146 /***********************************************************************
4147 * WSALookupServiceBeginW (WS2_32.61)
4149 INT WINAPI WSALookupServiceEnd( HANDLE lookup )
4151 FIXME("(%p) Stub!\n", lookup );
4155 /***********************************************************************
4156 * WSALookupServiceNextA (WS2_32.62)
4158 INT WINAPI WSALookupServiceNextA( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETA results )
4160 FIXME( "(%p 0x%08lx %p %p) Stub!\n", lookup, flags, len, results );
4164 /***********************************************************************
4165 * WSALookupServiceNextW (WS2_32.63)
4167 INT WINAPI WSALookupServiceNextW( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETW results )
4169 FIXME( "(%p 0x%08lx %p %p) Stub!\n", lookup, flags, len, results );
4173 /***********************************************************************
4174 * WSANtohl (WS2_32.64)
4176 INT WINAPI WSANtohl( SOCKET s, u_long netlong, u_long* lphostlong )
4178 TRACE( "(0x%04x 0x%08lx %p)\n", s, netlong, lphostlong );
4180 if (!lphostlong) return WSAEFAULT;
4182 *lphostlong = ntohl( netlong );
4186 /***********************************************************************
4187 * WSANtohs (WS2_32.65)
4189 INT WINAPI WSANtohs( SOCKET s, u_short netshort, u_short* lphostshort )
4191 TRACE( "(0x%04x 0x%08x %p)\n", s, netshort, lphostshort );
4193 if (!lphostshort) return WSAEFAULT;
4195 *lphostshort = ntohs( netshort );
4199 /***********************************************************************
4200 * WSAProviderConfigChange (WS2_32.66)
4202 INT WINAPI WSAProviderConfigChange( LPHANDLE handle, LPWSAOVERLAPPED overlapped,
4203 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion )
4205 FIXME( "(%p %p %p) Stub!\n", handle, overlapped, completion );
4206 return SOCKET_ERROR;
4209 /***********************************************************************
4210 * WSARecvDisconnect (WS2_32.68)
4212 INT WINAPI WSARecvDisconnect( SOCKET s, LPWSABUF disconnectdata )
4214 TRACE( "(0x%04x %p)\n", s, disconnectdata );
4216 return WS_shutdown( s, 0 );
4219 /***********************************************************************
4220 * WSASetServiceA (WS2_32.76)
4222 INT WINAPI WSASetServiceA( LPWSAQUERYSETA query, WSAESETSERVICEOP operation, DWORD flags )
4224 FIXME( "(%p 0x%08x 0x%08lx) Stub!\n", query, operation, flags );
4228 /***********************************************************************
4229 * WSASetServiceW (WS2_32.77)
4231 INT WINAPI WSASetServiceW( LPWSAQUERYSETW query, WSAESETSERVICEOP operation, DWORD flags )
4233 FIXME( "(%p 0x%08x 0x%08lx) Stub!\n", query, operation, flags );
4237 /***********************************************************************
4238 * WSCEnableNSProvider (WS2_32.84)
4240 INT WINAPI WSCEnableNSProvider( LPGUID provider, BOOL enable )
4242 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider), enable );
4246 /***********************************************************************
4247 * WSCGetProviderPath (WS2_32.86)
4249 INT WINAPI WSCGetProviderPath( LPGUID provider, LPWSTR path, LPINT len, LPINT errno )
4251 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider), path, len, errno );
4253 if (!errno || !provider || !len) return WSAEFAULT;
4256 return SOCKET_ERROR;
4259 /***********************************************************************
4260 * WSCInstallNameSpace (WS2_32.87)
4262 INT WINAPI WSCInstallNameSpace( LPWSTR identifier, LPWSTR path, DWORD namespace,
4263 DWORD version, LPGUID provider )
4265 FIXME( "(%s %s 0x%08lx 0x%08lx %s) Stub!\n", debugstr_w(identifier), debugstr_w(path),
4266 namespace, version, debugstr_guid(provider) );
4270 /***********************************************************************
4271 * WSCUnInstallNameSpace (WS2_32.89)
4273 INT WINAPI WSCUnInstallNameSpace( LPGUID lpProviderId )
4275 FIXME("(%p) Stub!\n", lpProviderId);
4279 /***********************************************************************
4280 * WSCWriteProviderOrder (WS2_32.91)
4282 INT WINAPI WSCWriteProviderOrder( LPDWORD entry, DWORD number )
4284 FIXME("(%p 0x%08lx) Stub!\n", entry, number);