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 #ifdef HAVE_NETIPX_IPX_H
99 # include <netipx/ipx.h>
101 #elif defined(HAVE_LINUX_IPX_H)
102 # ifdef HAVE_ASM_TYPES_H
103 # include <asm/types.h>
105 # include <linux/ipx.h>
112 #ifdef HAVE_SYS_POLL_H
113 # include <sys/poll.h>
115 #ifdef HAVE_SYS_TIME_H
116 # include <sys/time.h>
119 #define NONAMELESSUNION
120 #define NONAMELESSSTRUCT
125 #include "winerror.h"
127 #include "winsock2.h"
129 #include "ws2tcpip.h"
133 #include "iphlpapi.h"
135 #include "wine/server.h"
136 #include "wine/debug.h"
137 #include "ntstatus.h"
138 #include "wine/unicode.h"
141 # include "wsnwlink.h"
146 # define sipx_network sipx_addr.x_net
147 # define sipx_node sipx_addr.x_host.c_host
148 #endif /* __FreeBSD__ */
151 #define INADDR_NONE ~0UL
154 WINE_DEFAULT_DEBUG_CHANNEL(winsock);
156 /* critical section to protect some non-rentrant net function */
157 extern CRITICAL_SECTION csWSgetXXXbyYYY;
159 inline static const char *debugstr_sockaddr( const struct WS_sockaddr *a )
161 if (!a) return "(nil)";
162 return wine_dbg_sprintf("{ family %d, address %s, port %d }",
163 ((const struct sockaddr_in *)a)->sin_family,
164 inet_ntoa(((const struct sockaddr_in *)a)->sin_addr),
165 ntohs(((const struct sockaddr_in *)a)->sin_port));
168 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
169 #define SOCKET2HANDLE(s) ((HANDLE)(s))
170 #define HANDLE2SOCKET(h) ((SOCKET)(h))
172 /****************************************************************
173 * Async IO declarations
174 ****************************************************************/
176 typedef struct ws2_async
179 enum ws2_mode {ws2m_read, ws2m_write, ws2m_sd_read, ws2m_sd_write} mode;
180 LPWSAOVERLAPPED user_overlapped;
181 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func;
184 struct WS_sockaddr *addr;
187 int val; /* for send operations */
188 int *ptr; /* for recv operations */
195 /****************************************************************/
197 /* ----------------------------------- internal data */
199 /* ws_... struct conversion flags */
201 typedef struct /* WSAAsyncSelect() control struct */
203 HANDLE service, event, sock;
209 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
210 #define WS_MAX_UDP_DATAGRAM 1024
211 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x );
213 /* hostent's, servent's and protent's are stored in one buffer per thread,
214 * as documented on MSDN for the functions that return any of the buffers */
215 struct per_thread_data
218 struct WS_hostent *he_buffer;
219 struct WS_servent *se_buffer;
220 struct WS_protoent *pe_buffer;
226 static INT num_startup; /* reference counter */
227 static FARPROC blocking_hook = WSA_DefaultBlockingHook;
229 /* function prototypes */
230 static struct WS_hostent *WS_dup_he(const struct hostent* p_he);
231 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe);
232 static struct WS_servent *WS_dup_se(const struct servent* p_se);
234 int WSAIOCTL_GetInterfaceCount(void);
235 int WSAIOCTL_GetInterfaceName(int intNumber, char *intName);
238 UINT wsaHerrno(int errnr);
240 #define MAP_OPTION(opt) { WS_##opt, opt }
242 static const int ws_sock_map[][2] =
244 MAP_OPTION( SO_DEBUG ),
245 MAP_OPTION( SO_REUSEADDR ),
246 MAP_OPTION( SO_KEEPALIVE ),
247 MAP_OPTION( SO_DONTROUTE ),
248 MAP_OPTION( SO_BROADCAST ),
249 MAP_OPTION( SO_LINGER ),
250 MAP_OPTION( SO_OOBINLINE ),
251 MAP_OPTION( SO_SNDBUF ),
252 MAP_OPTION( SO_RCVBUF ),
253 MAP_OPTION( SO_ERROR ),
254 MAP_OPTION( SO_TYPE ),
256 MAP_OPTION( SO_RCVTIMEO ),
259 MAP_OPTION( SO_SNDTIMEO ),
264 static const int ws_tcp_map[][2] =
267 MAP_OPTION( TCP_NODELAY ),
272 static const int ws_ip_map[][2] =
274 MAP_OPTION( IP_MULTICAST_IF ),
275 MAP_OPTION( IP_MULTICAST_TTL ),
276 MAP_OPTION( IP_MULTICAST_LOOP ),
277 MAP_OPTION( IP_ADD_MEMBERSHIP ),
278 MAP_OPTION( IP_DROP_MEMBERSHIP ),
279 MAP_OPTION( IP_OPTIONS ),
281 MAP_OPTION( IP_HDRINCL ),
283 MAP_OPTION( IP_TOS ),
284 MAP_OPTION( IP_TTL ),
288 inline static DWORD NtStatusToWSAError( const DWORD status )
290 /* We only need to cover the status codes set by server async request handling */
294 case STATUS_SUCCESS: wserr = 0; break;
295 case STATUS_PENDING: wserr = WSA_IO_PENDING; break;
296 case STATUS_INVALID_HANDLE: wserr = WSAENOTSOCK; break; /* WSAEBADF ? */
297 case STATUS_INVALID_PARAMETER: wserr = WSAEINVAL; break;
298 case STATUS_PIPE_DISCONNECTED: wserr = WSAESHUTDOWN; break;
299 case STATUS_CANCELLED: wserr = WSA_OPERATION_ABORTED; break;
300 case STATUS_TIMEOUT: wserr = WSAETIMEDOUT; break;
301 case STATUS_NO_MEMORY: wserr = WSAEFAULT; break;
303 if ( status >= WSABASEERR && status <= WSABASEERR+1004 )
304 /* It is not a NT status code but a winsock error */
308 wserr = RtlNtStatusToDosError( status );
309 FIXME( "Status code %08lx converted to DOS error code %lx\n", status, wserr );
315 /* set last error code from NT status without mapping WSA errors */
316 inline static unsigned int set_error( unsigned int err )
320 err = NtStatusToWSAError( err );
326 inline static int get_sock_fd( SOCKET s, DWORD access, int *flags )
329 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s), access, &fd, flags ) ))
334 inline static void release_sock_fd( SOCKET s, int fd )
336 wine_server_release_fd( SOCKET2HANDLE(s), fd );
339 static void _enable_event( HANDLE s, unsigned int event,
340 unsigned int sstate, unsigned int cstate )
342 SERVER_START_REQ( enable_socket_event )
346 req->sstate = sstate;
347 req->cstate = cstate;
348 wine_server_call( req );
353 static int _is_blocking(SOCKET s)
356 SERVER_START_REQ( get_socket_event )
358 req->handle = SOCKET2HANDLE(s);
359 req->service = FALSE;
361 wine_server_call( req );
362 ret = (reply->state & FD_WINE_NONBLOCKING) == 0;
368 static unsigned int _get_sock_mask(SOCKET s)
371 SERVER_START_REQ( get_socket_event )
373 req->handle = SOCKET2HANDLE(s);
374 req->service = FALSE;
376 wine_server_call( req );
383 static void _sync_sock_state(SOCKET s)
385 /* do a dummy wineserver request in order to let
386 the wineserver run through its select loop once */
387 (void)_is_blocking(s);
390 static int _get_sock_error(SOCKET s, unsigned int bit)
392 int events[FD_MAX_EVENTS];
394 SERVER_START_REQ( get_socket_event )
396 req->handle = SOCKET2HANDLE(s);
397 req->service = FALSE;
399 wine_server_set_reply( req, events, sizeof(events) );
400 wine_server_call( req );
406 static struct per_thread_data *get_per_thread_data(void)
408 struct per_thread_data * ptb = NtCurrentTeb()->WinSockData;
409 /* lazy initialization */
412 ptb = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ptb) );
413 NtCurrentTeb()->WinSockData = ptb;
418 static void free_per_thread_data(void)
420 struct per_thread_data * ptb = NtCurrentTeb()->WinSockData;
424 /* delete scratch buffers */
425 HeapFree( GetProcessHeap(), 0, ptb->he_buffer );
426 HeapFree( GetProcessHeap(), 0, ptb->se_buffer );
427 HeapFree( GetProcessHeap(), 0, ptb->pe_buffer );
428 ptb->he_buffer = NULL;
429 ptb->se_buffer = NULL;
430 ptb->pe_buffer = NULL;
432 HeapFree( GetProcessHeap(), 0, ptb );
433 NtCurrentTeb()->WinSockData = NULL;
436 /***********************************************************************
437 * DllMain (WS2_32.init)
439 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
441 TRACE("%p 0x%lx %p\n", hInstDLL, fdwReason, fImpLoad);
443 case DLL_PROCESS_ATTACH:
445 case DLL_PROCESS_DETACH:
446 free_per_thread_data();
449 case DLL_THREAD_DETACH:
450 free_per_thread_data();
456 /***********************************************************************
459 * Converts socket flags from Windows format.
460 * Return 1 if converted, 0 if not (error).
462 static int convert_sockopt(INT *level, INT *optname)
469 for(i=0; ws_sock_map[i][0]; i++)
471 if( ws_sock_map[i][0] == *optname )
473 *optname = ws_sock_map[i][1];
477 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname);
480 *level = IPPROTO_TCP;
481 for(i=0; ws_tcp_map[i][0]; i++)
483 if ( ws_tcp_map[i][0] == *optname )
485 *optname = ws_tcp_map[i][1];
489 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname);
493 for(i=0; ws_ip_map[i][0]; i++)
495 if (ws_ip_map[i][0] == *optname )
497 *optname = ws_ip_map[i][1];
501 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname);
503 default: FIXME("Unimplemented or unknown socket level\n");
508 static inline BOOL is_timeout_option( int optname )
511 if (optname == SO_RCVTIMEO) return TRUE;
514 if (optname == SO_SNDTIMEO) return TRUE;
519 /* ----------------------------------- Per-thread info (or per-process?) */
521 static char *strdup_lower(const char *str)
524 char *ret = HeapAlloc( GetProcessHeap(), 0, strlen(str) + 1 );
528 for (i = 0; str[i]; i++) ret[i] = tolower(str[i]);
531 else SetLastError(WSAENOBUFS);
535 static fd_set* fd_set_import( fd_set* fds, const WS_fd_set* wsfds, int access, int* highfd, int lfd[] )
537 /* translate Winsock fd set into local fd set */
543 for( i = 0; i < wsfds->fd_count; i++ )
545 int s = wsfds->fd_array[i];
546 int fd = get_sock_fd( s, access, NULL );
550 if( fd > *highfd ) *highfd = fd;
560 inline static int sock_error_p(int s)
562 unsigned int optval, optlen;
564 optlen = sizeof(optval);
565 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
566 if (optval) WARN("\t[%i] error: %d\n", s, optval);
570 static int fd_set_export( const fd_set* fds, fd_set* exceptfds, WS_fd_set* wsfds, int lfd[] )
574 /* translate local fd set into Winsock fd set, adding
575 * errors to exceptfds (only if app requested it) */
579 int i, j, count = wsfds->fd_count;
581 for( i = 0, j = 0; i < count; i++ )
584 SOCKET s = wsfds->fd_array[i];
585 if (fd == -1) continue;
586 if( FD_ISSET(fd, fds) )
588 if ( exceptfds && sock_error_p(fd) )
590 FD_SET(fd, exceptfds);
593 else wsfds->fd_array[j++] = s;
595 release_sock_fd( s, fd );
602 static void fd_set_unimport( WS_fd_set* wsfds, int lfd[] )
608 for ( i = 0; i < wsfds->fd_count; i++ )
609 if ( lfd[i] >= 0 ) release_sock_fd( wsfds->fd_array[i], lfd[i] );
614 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
615 * from an fd and return the value converted to milli seconds
616 * or -1 if there is an infinite time out */
617 static inline int get_rcvsnd_timeo( int fd, int optname)
620 unsigned int len = sizeof(tv);
621 int ret = getsockopt(fd, SOL_SOCKET, optname, &tv, &len);
623 ret = tv.tv_sec * 1000 + tv.tv_usec / 1000;
624 if( ret <= 0 ) /* tv == {0,0} means infinite time out */
629 /* macro wrappers for portability */
631 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
633 #define GET_RCVTIMEO(fd) (-1)
637 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
639 #define GET_SNDTIMEO(fd) (-1)
642 /* utility: given an fd, will block until one of the events occurs */
643 static inline int do_block( int fd, int events, int timeout )
651 while ((ret = poll(&pfd, 1, timeout)) < 0)
662 /* ----------------------------------- API -----
664 * Init / cleanup / error checking.
667 /***********************************************************************
668 * WSAStartup (WS2_32.115)
670 int WINAPI WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData)
672 TRACE("verReq=%x\n", wVersionRequested);
674 if (LOBYTE(wVersionRequested) < 1)
675 return WSAVERNOTSUPPORTED;
677 if (!lpWSAData) return WSAEINVAL;
681 /* that's the whole of the negotiation for now */
682 lpWSAData->wVersion = wVersionRequested;
683 /* return winsock information */
684 lpWSAData->wHighVersion = 0x0202;
685 strcpy(lpWSAData->szDescription, "WinSock 2.0" );
686 strcpy(lpWSAData->szSystemStatus, "Running" );
687 lpWSAData->iMaxSockets = WS_MAX_SOCKETS_PER_PROCESS;
688 lpWSAData->iMaxUdpDg = WS_MAX_UDP_DATAGRAM;
689 /* don't do anything with lpWSAData->lpVendorInfo */
690 /* (some apps don't allocate the space for this field) */
692 TRACE("succeeded\n");
697 /***********************************************************************
698 * WSACleanup (WS2_32.116)
700 INT WINAPI WSACleanup(void)
704 SetLastError(WSANOTINITIALISED);
709 /***********************************************************************
710 * WSAGetLastError (WINSOCK.111)
711 * WSAGetLastError (WS2_32.111)
713 INT WINAPI WSAGetLastError(void)
715 return GetLastError();
718 /***********************************************************************
719 * WSASetLastError (WS2_32.112)
721 void WINAPI WSASetLastError(INT iError) {
722 SetLastError(iError);
725 static struct WS_hostent *check_buffer_he(int size)
727 struct per_thread_data * ptb = get_per_thread_data();
730 if (ptb->he_len >= size ) return ptb->he_buffer;
731 HeapFree( GetProcessHeap(), 0, ptb->he_buffer );
733 ptb->he_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->he_len = size) );
734 if (!ptb->he_buffer) SetLastError(WSAENOBUFS);
735 return ptb->he_buffer;
738 static struct WS_servent *check_buffer_se(int size)
740 struct per_thread_data * ptb = get_per_thread_data();
743 if (ptb->se_len >= size ) return ptb->se_buffer;
744 HeapFree( GetProcessHeap(), 0, ptb->se_buffer );
746 ptb->se_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->se_len = size) );
747 if (!ptb->se_buffer) SetLastError(WSAENOBUFS);
748 return ptb->se_buffer;
751 static struct WS_protoent *check_buffer_pe(int size)
753 struct per_thread_data * ptb = get_per_thread_data();
756 if (ptb->pe_len >= size ) return ptb->pe_buffer;
757 HeapFree( GetProcessHeap(), 0, ptb->pe_buffer );
759 ptb->pe_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->pe_len = size) );
760 if (!ptb->pe_buffer) SetLastError(WSAENOBUFS);
761 return ptb->pe_buffer;
764 /* ----------------------------------- i/o APIs */
767 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf)== WS_AF_IPX)
769 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET)
773 /**********************************************************************/
775 /* Returns the converted address if successful, NULL if it was too small to
776 * start with. Note that the returned pointer may be the original pointer
777 * if no conversion is necessary.
779 static const struct sockaddr* ws_sockaddr_ws2u(const struct WS_sockaddr* wsaddr, int wsaddrlen, unsigned int *uaddrlen)
781 switch (wsaddr->sa_family)
786 const struct WS_sockaddr_ipx* wsipx=(const struct WS_sockaddr_ipx*)wsaddr;
787 struct sockaddr_ipx* uipx;
789 if (wsaddrlen<sizeof(struct WS_sockaddr_ipx))
792 *uaddrlen=sizeof(struct sockaddr_ipx);
793 uipx=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *uaddrlen);
794 uipx->sipx_family=AF_IPX;
795 uipx->sipx_port=wsipx->sa_socket;
796 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
799 memcpy(&uipx->sipx_network,wsipx->sa_netnum,sizeof(uipx->sipx_network)+sizeof(uipx->sipx_node));
800 #ifdef IPX_FRAME_NONE
801 uipx->sipx_type=IPX_FRAME_NONE;
803 return (const struct sockaddr*)uipx;
808 if (wsaddrlen<sizeof(struct WS_sockaddr))
811 /* No conversion needed, just return the original address */
813 return (const struct sockaddr*)wsaddr;
817 /* Allocates a Unix sockaddr structure to receive the data */
818 static inline struct sockaddr* ws_sockaddr_alloc(const struct WS_sockaddr* wsaddr, int* wsaddrlen, unsigned int* uaddrlen)
822 ERR( "WINE shouldn't pass a NULL wsaddr! Attempting to continue\n" );
824 /* This is not strictly the right thing to do. Hope it works however */
833 *uaddrlen=max(sizeof(struct sockaddr),*wsaddrlen);
835 return HeapAlloc(GetProcessHeap(), 0, *uaddrlen);
838 /* Returns 0 if successful, -1 if the buffer is too small */
839 static int ws_sockaddr_u2ws(const struct sockaddr* uaddr, int uaddrlen, struct WS_sockaddr* wsaddr, int* wsaddrlen)
843 switch(uaddr->sa_family)
848 const struct sockaddr_ipx* uipx=(const struct sockaddr_ipx*)uaddr;
849 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
852 switch (*wsaddrlen) /* how much can we copy? */
857 wsipx->sa_socket=uipx->sipx_port;
861 memcpy(wsipx->sa_nodenum,uipx->sipx_node,sizeof(wsipx->sa_nodenum));
869 memcpy(wsipx->sa_netnum,&uipx->sipx_network,sizeof(wsipx->sa_netnum));
875 wsipx->sa_family=WS_AF_IPX;
887 /* No conversion needed */
888 memcpy(wsaddr,uaddr,*wsaddrlen);
889 if (*wsaddrlen<uaddrlen) {
899 /* to be called to free the memory allocated by ws_sockaddr_ws2u or
902 static inline void ws_sockaddr_free(const struct sockaddr* uaddr, const struct WS_sockaddr* wsaddr)
904 if (uaddr!=(const struct sockaddr*)wsaddr)
905 HeapFree(GetProcessHeap(), 0, (void *)uaddr);
908 /**************************************************************************
909 * Functions for handling overlapped I/O
910 **************************************************************************/
912 static void CALLBACK ws2_async_terminate(ws2_async* as, IO_STATUS_BLOCK* iosb)
914 TRACE( "as: %p uovl %p ovl %p\n", as, as->user_overlapped, iosb );
916 wine_server_release_fd( as->hSocket, as->fd );
917 if ( as->event != INVALID_HANDLE_VALUE )
918 NtSetEvent( as->event, NULL );
920 if (as->completion_func)
921 as->completion_func( NtStatusToWSAError (iosb->u.Status),
922 iosb->Information, as->user_overlapped, as->flags );
923 if ( !as->user_overlapped )
926 /* FIXME: I don't think this is really used */
927 if ( as->overlapped->hEvent != INVALID_HANDLE_VALUE )
928 WSACloseEvent( as->overlapped->hEvent );
930 HeapFree( GetProcessHeap(), 0, iosb );
933 HeapFree( GetProcessHeap(), 0, as->iovec );
934 HeapFree( GetProcessHeap(), 0, as );
937 /***********************************************************************
938 * WS2_make_async (INTERNAL)
941 static void WINAPI WS2_async_recv(void*, IO_STATUS_BLOCK*, ULONG);
942 static void WINAPI WS2_async_send(void*, IO_STATUS_BLOCK*, ULONG);
943 static void WINAPI WS2_async_shutdown( void*, IO_STATUS_BLOCK*, ULONG);
945 inline static struct ws2_async*
946 WS2_make_async(SOCKET s, int fd, enum ws2_mode mode, struct iovec *iovec, DWORD dwBufferCount,
947 LPDWORD lpFlags, struct WS_sockaddr *addr,
948 LPINT addrlen, LPWSAOVERLAPPED lpOverlapped,
949 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
950 IO_STATUS_BLOCK **piosb)
952 struct ws2_async *wsa = HeapAlloc( GetProcessHeap(), 0, sizeof( ws2_async ) );
954 TRACE( "wsa %p\n", wsa );
959 wsa->hSocket = (HANDLE) s;
965 wsa->flags = *lpFlags;
966 wsa->addrlen.ptr = addrlen;
971 wsa->addrlen.val = *addrlen;
974 ERR("Invalid async mode: %d\n", mode);
976 wsa->user_overlapped = lpOverlapped;
977 wsa->completion_func = lpCompletionRoutine;
979 wsa->n_iovecs = dwBufferCount;
982 wsa->event = INVALID_HANDLE_VALUE;
986 *piosb = (IO_STATUS_BLOCK*)lpOverlapped;
987 if (!lpCompletionRoutine)
989 wsa->event = lpOverlapped->hEvent;
990 NtResetEvent(wsa->event, NULL);
993 else if (!(*piosb = HeapAlloc( GetProcessHeap(), 0, sizeof(IO_STATUS_BLOCK))))
996 (*piosb)->Information = 0;
997 (*piosb)->u.Status = STATUS_PENDING;
998 TRACE( "wsa %p, h %p, ev %p, fd %d, iosb %p, uov %p, cfunc %p\n",
999 wsa, wsa->hSocket, wsa->event, wsa->fd,
1000 *piosb, wsa->user_overlapped, wsa->completion_func );
1006 HeapFree( GetProcessHeap(), 0, wsa );
1010 static ULONG ws2_queue_async(struct ws2_async* wsa, IO_STATUS_BLOCK* iosb)
1012 PIO_APC_ROUTINE apc;
1018 case ws2m_read: apc = WS2_async_recv; type = ASYNC_TYPE_READ; break;
1019 case ws2m_write: apc = WS2_async_send; type = ASYNC_TYPE_WRITE; break;
1020 case ws2m_sd_read: apc = WS2_async_shutdown; type = ASYNC_TYPE_READ; break;
1021 case ws2m_sd_write: apc = WS2_async_shutdown; type = ASYNC_TYPE_WRITE; break;
1022 default: FIXME("Unknown internal mode (%d)\n", wsa->mode); return STATUS_INVALID_PARAMETER;
1025 SERVER_START_REQ( register_async )
1027 req->handle = wsa->hSocket;
1032 req->count = iosb->Information;
1033 status = wine_server_call( req );
1037 if ( status ) iosb->u.Status = status;
1038 if ( iosb->u.Status != STATUS_PENDING )
1040 /* Note: we get here a non zero status when we couldn't queue the async
1041 * in the server. Therefore, we simply terminate the async.
1043 status = iosb->u.Status;
1044 ws2_async_terminate(wsa, iosb);
1047 NtCurrentTeb()->num_async_io++;
1048 return STATUS_SUCCESS;
1051 /***********************************************************************
1052 * WS2_recv (INTERNAL)
1054 * Workhorse for both synchronous and asynchronous recv() operations.
1056 static int WS2_recv( int fd, struct iovec* iov, int count,
1057 struct WS_sockaddr *lpFrom, LPINT lpFromlen,
1062 TRACE( "fd %d, iovec %p, count %d addr %s, len %p, flags %lx\n",
1063 fd, iov, count, debugstr_sockaddr(lpFrom), lpFromlen, *lpFlags);
1065 hdr.msg_name = NULL;
1069 hdr.msg_namelen = *lpFromlen;
1070 hdr.msg_name = ws_sockaddr_alloc( lpFrom, lpFromlen, &hdr.msg_namelen );
1071 if ( !hdr.msg_name )
1073 WSASetLastError( WSAEFAULT );
1079 hdr.msg_namelen = 0;
1082 hdr.msg_iovlen = count;
1083 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1084 hdr.msg_accrights = NULL;
1085 hdr.msg_accrightslen = 0;
1087 hdr.msg_control = NULL;
1088 hdr.msg_controllen = 0;
1092 if ( (n = recvmsg(fd, &hdr, *lpFlags)) == -1 )
1094 TRACE( "recvmsg error %d\n", errno);
1099 ws_sockaddr_u2ws( hdr.msg_name, hdr.msg_namelen,
1100 lpFrom, lpFromlen ) != 0 )
1102 /* The from buffer was too small, but we read the data
1103 * anyway. Is that really bad?
1105 WSASetLastError( WSAEFAULT );
1106 WARN( "Address buffer too small\n" );
1111 ws_sockaddr_free( hdr.msg_name, lpFrom );
1112 TRACE("-> %d\n", n);
1116 /***********************************************************************
1117 * WS2_async_recv (INTERNAL)
1119 * Handler for overlapped recv() operations.
1121 static void WINAPI WS2_async_recv( void* ovp, IO_STATUS_BLOCK* iosb, ULONG status)
1123 ws2_async* wsa = (ws2_async*) ovp;
1126 TRACE( "(%p %p %lx)\n", wsa, iosb, status );
1130 case STATUS_ALERTED:
1131 result = WS2_recv( wsa->fd, wsa->iovec, wsa->n_iovecs,
1132 wsa->addr, wsa->addrlen.ptr, &wsa->flags );
1135 iosb->u.Status = STATUS_SUCCESS;
1136 iosb->Information = result;
1137 TRACE( "received %d bytes\n", result );
1138 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1143 if ( err == WSAEINTR || err == WSAEWOULDBLOCK ) /* errno: EINTR / EAGAIN */
1145 iosb->u.Status = STATUS_PENDING;
1146 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1147 TRACE( "still pending\n" );
1151 iosb->u.Status = err; /* FIXME: is this correct ???? */
1152 TRACE( "Error: %x\n", err );
1155 if (iosb->u.Status == STATUS_PENDING)
1156 ws2_queue_async(wsa, iosb);
1158 ws2_async_terminate(wsa, iosb);
1161 FIXME( "status: %ld\n", status );
1162 iosb->u.Status = status;
1163 ws2_async_terminate(wsa, iosb);
1168 /***********************************************************************
1169 * WS2_send (INTERNAL)
1171 * Workhorse for both synchronous and asynchronous send() operations.
1173 static int WS2_send( int fd, struct iovec* iov, int count,
1174 const struct WS_sockaddr *to, INT tolen, DWORD dwFlags )
1178 TRACE( "fd %d, iovec %p, count %d addr %s, len %d, flags %lx\n",
1179 fd, iov, count, debugstr_sockaddr(to), tolen, dwFlags);
1181 hdr.msg_name = NULL;
1185 hdr.msg_name = (struct sockaddr*) ws_sockaddr_ws2u( to, tolen, &hdr.msg_namelen );
1186 if ( !hdr.msg_name )
1188 WSASetLastError( WSAEFAULT );
1193 if(to->sa_family == WS_AF_IPX)
1196 struct sockaddr_ipx* uipx = (struct sockaddr_ipx*)hdr.msg_name;
1198 unsigned int len=sizeof(int);
1200 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1201 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1202 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1203 * ipx type in the sockaddr_opx structure with the stored value.
1205 if(getsockopt(fd, SOL_IPX, IPX_TYPE, &val, &len) != -1)
1207 TRACE("ptype: %d (fd:%d)\n", val, fd);
1208 uipx->sipx_type = val;
1216 hdr.msg_namelen = 0;
1219 hdr.msg_iovlen = count;
1220 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1221 hdr.msg_accrights = NULL;
1222 hdr.msg_accrightslen = 0;
1224 hdr.msg_control = NULL;
1225 hdr.msg_controllen = 0;
1229 n = sendmsg(fd, &hdr, dwFlags);
1232 ws_sockaddr_free( hdr.msg_name, to );
1236 /***********************************************************************
1237 * WS2_async_send (INTERNAL)
1239 * Handler for overlapped send() operations.
1241 static void WINAPI WS2_async_send(void* as, IO_STATUS_BLOCK* iosb, ULONG status)
1243 ws2_async* wsa = (ws2_async*) as;
1246 TRACE( "(%p %p %lx)\n", wsa, iosb, status );
1250 case STATUS_ALERTED:
1251 if (iosb->u.Status != STATUS_PENDING) FIXME("wrong %08lx\n", iosb->u.Status);
1252 /* check to see if the data is ready (non-blocking) */
1253 result = WS2_send( wsa->fd, wsa->iovec, wsa->n_iovecs,
1254 wsa->addr, wsa->addrlen.val, wsa->flags );
1258 iosb->u.Status = STATUS_SUCCESS;
1259 iosb->Information = result;
1260 TRACE( "sent %d bytes\n", result );
1261 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1265 int err = wsaErrno();
1266 if ( err == WSAEINTR )
1268 iosb->u.Status = STATUS_PENDING;
1269 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1270 TRACE( "still pending\n" );
1274 /* We set the status to a winsock error code and check for that
1275 later in NtStatusToWSAError () */
1276 iosb->u.Status = err;
1277 TRACE( "Error: %x\n", err );
1280 if (iosb->u.Status == STATUS_PENDING)
1281 ws2_queue_async(wsa, iosb);
1283 ws2_async_terminate(wsa, iosb);
1286 FIXME( "status: %ld\n", status );
1287 iosb->u.Status = status;
1288 ws2_async_terminate(wsa, iosb);
1294 /***********************************************************************
1295 * WS2_async_shutdown (INTERNAL)
1297 * Handler for shutdown() operations on overlapped sockets.
1299 static void WINAPI WS2_async_shutdown( void* as, PIO_STATUS_BLOCK iosb, ULONG status )
1301 ws2_async* wsa = (ws2_async*) as;
1304 TRACE( "async %p %d\n", wsa, wsa->mode );
1307 case STATUS_ALERTED:
1308 switch ( wsa->mode )
1310 case ws2m_sd_read: err = shutdown( wsa->fd, 0 ); break;
1311 case ws2m_sd_write: err = shutdown( wsa->fd, 1 ); break;
1312 default: ERR("invalid mode: %d\n", wsa->mode );
1314 iosb->u.Status = err ? wsaErrno() : STATUS_SUCCESS;
1315 if (iosb->u.Status == STATUS_PENDING)
1316 ws2_queue_async(wsa, iosb);
1318 ws2_async_terminate(wsa, iosb);
1321 iosb->u.Status = status;
1322 ws2_async_terminate(wsa, iosb);
1328 /***********************************************************************
1329 * WS2_register_async_shutdown (INTERNAL)
1331 * Helper function for WS_shutdown() on overlapped sockets.
1333 static int WS2_register_async_shutdown( SOCKET s, int fd, enum ws2_mode mode )
1335 struct ws2_async *wsa;
1336 int ret, err = WSAEFAULT;
1339 LPWSAOVERLAPPED ovl = HeapAlloc(GetProcessHeap(), 0, sizeof( WSAOVERLAPPED ));
1340 IO_STATUS_BLOCK *iosb = NULL;
1342 TRACE("s %d fd %d mode %d\n", s, fd, mode);
1346 ovl->hEvent = WSACreateEvent();
1347 if ( ovl->hEvent == WSA_INVALID_EVENT )
1350 wsa = WS2_make_async( s, fd, mode, NULL, 0,
1351 &dwflags, NULL, &len, ovl, NULL, &iosb );
1355 /* Hack: this will cause ws2_async_terminate() to free the overlapped structure */
1356 wsa->user_overlapped = NULL;
1357 if ( (ret = ws2_queue_async( wsa, iosb )) )
1359 err = NtStatusToWSAError( ret );
1362 /* Try immediate completion */
1363 while ( WaitForSingleObjectEx( ovl->hEvent, 0, TRUE ) == STATUS_USER_APC );
1367 WSACloseEvent( ovl->hEvent );
1369 HeapFree( GetProcessHeap(), 0, ovl );
1374 /***********************************************************************
1377 SOCKET WINAPI WS_accept(SOCKET s, struct WS_sockaddr *addr,
1383 TRACE("socket %04x\n", s );
1384 is_blocking = _is_blocking(s);
1389 int fd = get_sock_fd( s, GENERIC_READ, NULL );
1390 if (fd == -1) return INVALID_SOCKET;
1392 do_block(fd, POLLIN, -1);
1393 _sync_sock_state(s); /* let wineserver notice connection */
1394 release_sock_fd( s, fd );
1395 /* retrieve any error codes from it */
1396 SetLastError(_get_sock_error(s, FD_ACCEPT_BIT));
1397 /* FIXME: care about the error? */
1399 SERVER_START_REQ( accept_socket )
1401 req->lhandle = SOCKET2HANDLE(s);
1402 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
1403 req->inherit = TRUE;
1404 set_error( wine_server_call( req ) );
1405 as = HANDLE2SOCKET( reply->handle );
1410 if (addr) WS_getpeername(as, addr, addrlen32);
1413 } while (is_blocking);
1414 return INVALID_SOCKET;
1417 /***********************************************************************
1420 int WINAPI WS_bind(SOCKET s, const struct WS_sockaddr* name, int namelen)
1422 int fd = get_sock_fd( s, 0, NULL );
1423 int res = SOCKET_ERROR;
1425 TRACE("socket %04x, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1429 if (!name || (name->sa_family && !SUPPORTED_PF(name->sa_family)))
1431 SetLastError(WSAEAFNOSUPPORT);
1435 const struct sockaddr* uaddr;
1436 unsigned int uaddrlen;
1438 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1441 SetLastError(WSAEFAULT);
1445 if (bind(fd, uaddr, uaddrlen) < 0)
1447 int loc_errno = errno;
1448 WARN("\tfailure - errno = %i\n", errno);
1453 SetLastError(WSAENOTSOCK);
1456 SetLastError(WSAEINVAL);
1459 SetLastError(wsaErrno());
1465 res=0; /* success */
1467 ws_sockaddr_free(uaddr,name);
1470 release_sock_fd( s, fd );
1475 /***********************************************************************
1476 * closesocket (WS2_32.3)
1478 int WINAPI WS_closesocket(SOCKET s)
1480 TRACE("socket %04x\n", s);
1481 if (CloseHandle(SOCKET2HANDLE(s))) return 0;
1482 return SOCKET_ERROR;
1485 /***********************************************************************
1486 * connect (WS2_32.4)
1488 int WINAPI WS_connect(SOCKET s, const struct WS_sockaddr* name, int namelen)
1490 int fd = get_sock_fd( s, GENERIC_READ, NULL );
1492 TRACE("socket %04x, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1496 const struct sockaddr* uaddr;
1497 unsigned int uaddrlen;
1499 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1502 SetLastError(WSAEFAULT);
1508 rc=connect(fd, uaddr, uaddrlen);
1509 ws_sockaddr_free(uaddr,name);
1511 goto connect_success;
1514 if (errno == EINPROGRESS)
1516 /* tell wineserver that a connection is in progress */
1517 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1518 FD_CONNECT|FD_READ|FD_WRITE,
1519 FD_WINE_CONNECTED|FD_WINE_LISTENING);
1520 if (_is_blocking(s))
1524 do_block(fd, POLLIN | POLLOUT, -1);
1525 _sync_sock_state(s); /* let wineserver notice connection */
1526 /* retrieve any error codes from it */
1527 result = _get_sock_error(s, FD_CONNECT_BIT);
1529 SetLastError(result);
1532 goto connect_success;
1537 SetLastError(WSAEWOULDBLOCK);
1542 SetLastError(wsaErrno());
1544 release_sock_fd( s, fd );
1546 return SOCKET_ERROR;
1549 release_sock_fd( s, fd );
1550 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1551 FD_WINE_CONNECTED|FD_READ|FD_WRITE,
1552 FD_CONNECT|FD_WINE_LISTENING);
1556 /***********************************************************************
1557 * WSAConnect (WS2_32.30)
1559 int WINAPI WSAConnect( SOCKET s, const struct WS_sockaddr* name, int namelen,
1560 LPWSABUF lpCallerData, LPWSABUF lpCalleeData,
1561 LPQOS lpSQOS, LPQOS lpGQOS )
1563 if ( lpCallerData || lpCalleeData || lpSQOS || lpGQOS )
1564 FIXME("unsupported parameters!\n");
1565 return WS_connect( s, name, namelen );
1569 /***********************************************************************
1570 * getpeername (WS2_32.5)
1572 int WINAPI WS_getpeername(SOCKET s, struct WS_sockaddr *name, int *namelen)
1577 TRACE("socket: %04x, ptr %p, len %08x\n", s, name, *namelen);
1579 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1580 if( (name == NULL) || (namelen == NULL) )
1582 SetLastError( WSAEFAULT );
1583 return SOCKET_ERROR;
1586 fd = get_sock_fd( s, 0, NULL );
1591 struct sockaddr* uaddr;
1592 unsigned int uaddrlen;
1594 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1595 if (getpeername(fd, uaddr, &uaddrlen) != 0)
1597 SetLastError(wsaErrno());
1599 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1601 /* The buffer was too small */
1602 SetLastError(WSAEFAULT);
1608 ws_sockaddr_free(uaddr,name);
1609 release_sock_fd( s, fd );
1614 /***********************************************************************
1615 * getsockname (WS2_32.6)
1617 int WINAPI WS_getsockname(SOCKET s, struct WS_sockaddr *name, int *namelen)
1622 TRACE("socket: %04x, ptr %p, len %8x\n", s, name, *namelen);
1624 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1625 if( (name == NULL) || (namelen == NULL) )
1627 SetLastError( WSAEFAULT );
1628 return SOCKET_ERROR;
1631 fd = get_sock_fd( s, 0, NULL );
1636 struct sockaddr* uaddr;
1637 unsigned int uaddrlen;
1639 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1640 if (getsockname(fd, uaddr, &uaddrlen) != 0)
1642 SetLastError(wsaErrno());
1644 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1646 /* The buffer was too small */
1647 SetLastError(WSAEFAULT);
1653 ws_sockaddr_free(uaddr,name);
1654 release_sock_fd( s, fd );
1659 /***********************************************************************
1660 * getsockopt (WS2_32.7)
1662 INT WINAPI WS_getsockopt(SOCKET s, INT level,
1663 INT optname, char *optval, INT *optlen)
1668 TRACE("socket: %04x, level 0x%x, name 0x%x, ptr %p, len %d\n",
1669 s, level, optname, optval, *optlen);
1670 /* SO_OPENTYPE does not require a valid socket handle. */
1671 if (level == WS_SOL_SOCKET && optname == WS_SO_OPENTYPE)
1673 if (!optlen || *optlen < sizeof(int) || !optval)
1675 SetLastError(WSAEFAULT);
1676 return SOCKET_ERROR;
1678 *(int *)optval = get_per_thread_data()->opentype;
1679 *optlen = sizeof(int);
1680 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
1685 if(level == NSPROTO_IPX)
1687 struct WS_sockaddr_ipx addr;
1688 IPX_ADDRESS_DATA *data;
1693 fd = get_sock_fd( s, 0, NULL );
1695 if(getsockopt(fd, SOL_IPX, IPX_TYPE, optval, (unsigned int*)optlen) == -1)
1697 return SOCKET_ERROR;
1702 socklen_t len=sizeof(struct ipx);
1704 if(getsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, &len) == -1 )
1705 return SOCKET_ERROR;
1706 *optval = (int)val.ipx_pt;
1709 TRACE("ptype: %d (fd: %d)\n", *(int*)optval, fd);
1710 release_sock_fd( s, fd );
1715 * On a Win2000 system with one network card there are usually three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
1716 * Using this call you can then retrieve info about this all. In case of Linux it is a bit different. Usually you have
1717 * only "one" device active and further it is not possible to query things like the linkspeed.
1719 FIXME("IPX_ADDRESS\n");
1720 namelen = sizeof(struct WS_sockaddr_ipx);
1721 memset(&addr, 0, sizeof(struct WS_sockaddr_ipx));
1722 WS_getsockname(s, (struct WS_sockaddr*)&addr, &namelen);
1724 data = (IPX_ADDRESS_DATA*)optval;
1725 memcpy(data->nodenum,&addr.sa_nodenum,sizeof(data->nodenum));
1726 memcpy(data->netnum,&addr.sa_netnum,sizeof(data->netnum));
1727 data->adapternum = 0;
1728 data->wan = FALSE; /* We are not on a wan for now .. */
1729 data->status = FALSE; /* Since we are not on a wan, the wan link isn't up */
1730 data->maxpkt = 1467; /* This value is the default one, at least on Win2k/WinXP */
1731 data->linkspeed = 100000; /* Set the line speed in 100bit/s to 10 Mbit; note 1MB = 1000kB in this case */
1733 case IPX_MAX_ADAPTER_NUM:
1734 FIXME("IPX_MAX_ADAPTER_NUM\n");
1735 *(int*)optval = 1; /* As noted under IPX_ADDRESS we have just one card. */
1739 FIXME("IPX optname:%x\n", optname);
1740 return SOCKET_ERROR;
1745 if( (fd = get_sock_fd( s, 0, NULL )) == -1)
1746 return SOCKET_ERROR;
1748 if (!convert_sockopt(&level, &optname)) {
1749 SetLastError(WSAENOPROTOOPT); /* Unknown option */
1753 struct linger lingval;
1754 unsigned int len, *plen = (unsigned int*)optlen;
1755 char *pval = optval;
1756 if(level == SOL_SOCKET && is_timeout_option(optname)) {
1759 pval = (char *) &tv;
1760 } else if( level == SOL_SOCKET && optname == SO_LINGER) {
1761 len = sizeof(lingval);
1763 pval = (char *) &lingval;
1765 if (getsockopt(fd, (int) level, optname, pval, plen) != 0 ) {
1766 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1768 } else if(level == SOL_SOCKET && is_timeout_option(optname)) {
1769 if( *optlen >= sizeof(INT) ) {
1770 *optlen = sizeof(INT);
1771 *(INT*)optval = tv.tv_sec * 1000 + tv.tv_usec / 1000;
1773 SetLastError(WSAEFAULT);
1776 } else if( level == SOL_SOCKET && optname == SO_LINGER) {
1777 if( *optlen >= sizeof( LINGER) ) {
1778 (( LINGER *) optval)->l_onoff = lingval.l_onoff;
1779 (( LINGER *) optval)->l_linger = lingval.l_linger;
1781 SetLastError(WSAEFAULT);
1786 release_sock_fd( s, fd );
1791 /***********************************************************************
1795 u_long WINAPI WS_htonl(u_long hostlong)
1797 return htonl(hostlong);
1801 /***********************************************************************
1805 u_short WINAPI WS_htons(u_short hostshort)
1807 return htons(hostshort);
1810 /***********************************************************************
1811 * WSAHtonl (WS2_32.46)
1812 * From MSDN decription of error codes, this function should also
1813 * check if WinSock has been initialized and the socket is a valid
1814 * socket. But why? This function only translates a host byte order
1815 * u_long into a network byte order u_long...
1817 int WINAPI WSAHtonl(SOCKET s, u_long hostlong, u_long *lpnetlong)
1821 *lpnetlong = htonl(hostlong);
1824 WSASetLastError(WSAEFAULT);
1825 return SOCKET_ERROR;
1828 /***********************************************************************
1829 * WSAHtons (WS2_32.47)
1830 * From MSDN decription of error codes, this function should also
1831 * check if WinSock has been initialized and the socket is a valid
1832 * socket. But why? This function only translates a host byte order
1833 * u_short into a network byte order u_short...
1835 int WINAPI WSAHtons(SOCKET s, u_short hostshort, u_short *lpnetshort)
1840 *lpnetshort = htons(hostshort);
1843 WSASetLastError(WSAEFAULT);
1844 return SOCKET_ERROR;
1848 /***********************************************************************
1849 * inet_addr (WINSOCK.10)
1850 * inet_addr (WS2_32.11)
1852 u_long WINAPI WS_inet_addr(const char *cp)
1854 return inet_addr(cp);
1858 /***********************************************************************
1859 * ntohl (WINSOCK.14)
1862 u_long WINAPI WS_ntohl(u_long netlong)
1864 return ntohl(netlong);
1868 /***********************************************************************
1869 * ntohs (WINSOCK.15)
1872 u_short WINAPI WS_ntohs(u_short netshort)
1874 return ntohs(netshort);
1878 /***********************************************************************
1879 * inet_ntoa (WS2_32.12)
1881 char* WINAPI WS_inet_ntoa(struct WS_in_addr in)
1883 /* use "buffer for dummies" here because some applications have a
1884 * propensity to decode addresses in ws_hostent structure without
1885 * saving them first...
1887 static char dbuffer[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
1889 char* s = inet_ntoa(*((struct in_addr*)&in));
1895 SetLastError(wsaErrno());
1899 /**********************************************************************
1900 * WSAIoctl (WS2_32.50)
1903 * FIXME: Only SIO_GET_INTERFACE_LIST option implemented.
1905 INT WINAPI WSAIoctl(SOCKET s,
1906 DWORD dwIoControlCode,
1909 LPVOID lpbOutBuffer,
1911 LPDWORD lpcbBytesReturned,
1912 LPWSAOVERLAPPED lpOverlapped,
1913 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
1915 int fd = get_sock_fd( s, 0, NULL );
1917 if (fd == -1) return SOCKET_ERROR;
1919 TRACE("%d, 0x%08lx, %p, %ld, %p, %ld, %p, %p, %p\n",
1920 s, dwIoControlCode, lpvInBuffer, cbInBuffer, lpbOutBuffer,
1921 cbOutBuffer, lpcbBytesReturned, lpOverlapped, lpCompletionRoutine);
1923 switch( dwIoControlCode )
1925 case SIO_GET_INTERFACE_LIST:
1927 INTERFACE_INFO* intArray = (INTERFACE_INFO*)lpbOutBuffer;
1928 DWORD size, numInt, apiReturn;
1930 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
1934 release_sock_fd( s, fd );
1935 WSASetLastError(WSAEFAULT);
1936 return SOCKET_ERROR;
1938 if (!lpcbBytesReturned)
1940 release_sock_fd( s, fd );
1941 WSASetLastError(WSAEFAULT);
1942 return SOCKET_ERROR;
1945 apiReturn = GetAdaptersInfo(NULL, &size);
1946 if (apiReturn == ERROR_NO_DATA)
1950 else if (apiReturn == ERROR_BUFFER_OVERFLOW)
1952 PIP_ADAPTER_INFO table = HeapAlloc(GetProcessHeap(),0,size);
1956 if (GetAdaptersInfo(table, &size) == NO_ERROR)
1958 PIP_ADAPTER_INFO ptr;
1960 if (size*sizeof(INTERFACE_INFO)/sizeof(IP_ADAPTER_INFO) > cbOutBuffer)
1962 WARN("Buffer too small = %lu, cbOutBuffer = %lu\n", size, cbOutBuffer);
1963 HeapFree(GetProcessHeap(),0,table);
1964 release_sock_fd( s, fd );
1965 WSASetLastError(WSAEFAULT);
1966 return SOCKET_ERROR;
1968 for (ptr = table, numInt = 0; ptr;
1969 ptr = ptr->Next, intArray++, numInt++)
1971 unsigned int addr, mask, bcast;
1972 struct ifreq ifInfo;
1974 /* Socket Status Flags */
1975 lstrcpynA(ifInfo.ifr_name, ptr->AdapterName, IFNAMSIZ);
1976 if (ioctl(fd, SIOCGIFFLAGS, &ifInfo) < 0)
1978 ERR("Error obtaining status flags for socket!\n");
1979 HeapFree(GetProcessHeap(),0,table);
1980 release_sock_fd( s, fd );
1981 WSASetLastError(WSAEINVAL);
1982 return SOCKET_ERROR;
1986 /* set flags; the values of IFF_* are not the same
1987 under Linux and Windows, therefore must generate
1989 intArray->iiFlags = 0;
1990 if (ifInfo.ifr_flags & IFF_BROADCAST)
1991 intArray->iiFlags |= WS_IFF_BROADCAST;
1992 #ifdef IFF_POINTOPOINT
1993 if (ifInfo.ifr_flags & IFF_POINTOPOINT)
1994 intArray->iiFlags |= WS_IFF_POINTTOPOINT;
1996 if (ifInfo.ifr_flags & IFF_LOOPBACK)
1997 intArray->iiFlags |= WS_IFF_LOOPBACK;
1998 if (ifInfo.ifr_flags & IFF_UP)
1999 intArray->iiFlags |= WS_IFF_UP;
2000 if (ifInfo.ifr_flags & IFF_MULTICAST)
2001 intArray->iiFlags |= WS_IFF_MULTICAST;
2004 addr = inet_addr(ptr->IpAddressList.IpAddress.String);
2005 mask = inet_addr(ptr->IpAddressList.IpMask.String);
2006 bcast = addr | (addr & !mask);
2007 intArray->iiAddress.AddressIn.sin_family = AF_INET;
2008 intArray->iiAddress.AddressIn.sin_port = 0;
2009 intArray->iiAddress.AddressIn.sin_addr.WS_s_addr =
2011 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
2012 intArray->iiNetmask.AddressIn.sin_port = 0;
2013 intArray->iiNetmask.AddressIn.sin_addr.WS_s_addr =
2015 intArray->iiBroadcastAddress.AddressIn.sin_family =
2017 intArray->iiBroadcastAddress.AddressIn.sin_port = 0;
2018 intArray->iiBroadcastAddress.AddressIn.sin_addr.
2024 ERR("Unable to get interface table!\n");
2025 release_sock_fd( s, fd );
2026 HeapFree(GetProcessHeap(),0,table);
2027 WSASetLastError(WSAEINVAL);
2028 return SOCKET_ERROR;
2030 HeapFree(GetProcessHeap(),0,table);
2034 release_sock_fd( s, fd );
2035 WSASetLastError(WSAEINVAL);
2036 return SOCKET_ERROR;
2041 ERR("Unable to get interface table!\n");
2042 release_sock_fd( s, fd );
2043 WSASetLastError(WSAEINVAL);
2044 return SOCKET_ERROR;
2046 /* Calculate the size of the array being returned */
2047 *lpcbBytesReturned = sizeof(INTERFACE_INFO) * numInt;
2051 case SIO_ADDRESS_LIST_CHANGE:
2052 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
2053 /* FIXME: error and return code depend on whether socket was created
2054 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
2058 WARN("\tunsupported WS_IOCTL cmd (%08lx)\n", dwIoControlCode);
2059 release_sock_fd( s, fd );
2060 WSASetLastError(WSAEOPNOTSUPP);
2061 return SOCKET_ERROR;
2064 /* Function executed with no errors */
2065 release_sock_fd( s, fd );
2070 /***********************************************************************
2071 * ioctlsocket (WS2_32.10)
2073 int WINAPI WS_ioctlsocket(SOCKET s, long cmd, u_long *argp)
2078 TRACE("socket %04x, cmd %08lx, ptr %p\n", s, cmd, argp);
2087 if( _get_sock_mask(s) )
2089 /* AsyncSelect()'ed sockets are always nonblocking */
2090 if (*argp) return 0;
2091 SetLastError(WSAEINVAL);
2092 return SOCKET_ERROR;
2094 fd = get_sock_fd( s, 0, NULL );
2100 _enable_event(SOCKET2HANDLE(s), 0, FD_WINE_NONBLOCKING, 0);
2101 ret = fcntl( fd, F_SETFL, O_NONBLOCK );
2105 _enable_event(SOCKET2HANDLE(s), 0, 0, FD_WINE_NONBLOCKING);
2106 ret = fcntl( fd, F_SETFL, 0 );
2108 release_sock_fd( s, fd );
2110 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2112 return SOCKET_ERROR;
2118 case WS__IOW('f',125,u_long):
2119 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
2120 SetLastError(WSAEINVAL);
2121 return SOCKET_ERROR;
2123 case SIOCGIFBRDADDR:
2124 case SIOCGIFNETMASK:
2126 /* These don't need any special handling. They are used by
2127 WsControl, and are here to suppress an unnecessary warning. */
2131 /* Netscape tries hard to use bogus ioctl 0x667e */
2132 WARN("\tunknown WS_IOCTL cmd (%08lx)\n", cmd);
2136 fd = get_sock_fd( s, 0, NULL );
2139 if( ioctl(fd, newcmd, (char*)argp ) == 0 )
2141 release_sock_fd( s, fd );
2144 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2145 release_sock_fd( s, fd );
2147 return SOCKET_ERROR;
2150 /***********************************************************************
2151 * listen (WS2_32.13)
2153 int WINAPI WS_listen(SOCKET s, int backlog)
2155 int fd = get_sock_fd( s, GENERIC_READ, NULL );
2157 TRACE("socket %04x, backlog %d\n", s, backlog);
2160 if (listen(fd, backlog) == 0)
2162 release_sock_fd( s, fd );
2163 _enable_event(SOCKET2HANDLE(s), FD_ACCEPT,
2165 FD_CONNECT|FD_WINE_CONNECTED);
2168 SetLastError(wsaErrno());
2169 release_sock_fd( s, fd );
2171 return SOCKET_ERROR;
2174 /***********************************************************************
2177 int WINAPI WS_recv(SOCKET s, char *buf, int len, int flags)
2179 DWORD n, dwFlags = flags;
2185 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, NULL, NULL, NULL, NULL) == SOCKET_ERROR )
2186 return SOCKET_ERROR;
2191 /***********************************************************************
2192 * recvfrom (WS2_32.17)
2194 int WINAPI WS_recvfrom(SOCKET s, char *buf, INT len, int flags,
2195 struct WS_sockaddr *from, int *fromlen)
2197 DWORD n, dwFlags = flags;
2203 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, from, fromlen, NULL, NULL) == SOCKET_ERROR )
2204 return SOCKET_ERROR;
2209 /***********************************************************************
2210 * select (WS2_32.18)
2212 int WINAPI WS_select(int nfds, WS_fd_set *ws_readfds,
2213 WS_fd_set *ws_writefds, WS_fd_set *ws_exceptfds,
2214 const struct WS_timeval* ws_timeout)
2217 fd_set readfds, writefds, exceptfds;
2218 fd_set *p_read, *p_write, *p_except;
2219 int readfd[FD_SETSIZE], writefd[FD_SETSIZE], exceptfd[FD_SETSIZE];
2220 struct timeval timeout, *timeoutaddr = NULL;
2222 TRACE("read %p, write %p, excp %p timeout %p\n",
2223 ws_readfds, ws_writefds, ws_exceptfds, ws_timeout);
2225 p_read = fd_set_import(&readfds, ws_readfds, GENERIC_READ, &highfd, readfd);
2226 p_write = fd_set_import(&writefds, ws_writefds, GENERIC_WRITE, &highfd, writefd);
2227 p_except = fd_set_import(&exceptfds, ws_exceptfds, 0, &highfd, exceptfd);
2230 timeoutaddr = &timeout;
2231 timeout.tv_sec=ws_timeout->tv_sec;
2232 timeout.tv_usec=ws_timeout->tv_usec;
2235 if( (highfd = select(highfd + 1, p_read, p_write, p_except, timeoutaddr)) > 0 )
2237 fd_set_export(&readfds, p_except, ws_readfds, readfd);
2238 fd_set_export(&writefds, p_except, ws_writefds, writefd);
2240 if (p_except && ws_exceptfds)
2244 for (i = j = 0; i < ws_exceptfds->fd_count; i++)
2246 int fd = exceptfd[i];
2247 SOCKET s = ws_exceptfds->fd_array[i];
2248 if (fd == -1) continue;
2249 if (FD_ISSET(fd, &exceptfds)) ws_exceptfds->fd_array[j++] = s;
2250 release_sock_fd( s, fd );
2252 ws_exceptfds->fd_count = j;
2256 fd_set_unimport(ws_readfds, readfd);
2257 fd_set_unimport(ws_writefds, writefd);
2258 fd_set_unimport(ws_exceptfds, exceptfd);
2260 if( highfd == 0 ) return 0;
2261 SetLastError(wsaErrno());
2262 return SOCKET_ERROR;
2266 /***********************************************************************
2269 int WINAPI WS_send(SOCKET s, const char *buf, int len, int flags)
2275 wsabuf.buf = (char*) buf;
2277 if ( WSASendTo( s, &wsabuf, 1, &n, flags, NULL, 0, NULL, NULL) == SOCKET_ERROR )
2278 return SOCKET_ERROR;
2283 /***********************************************************************
2284 * WSASend (WS2_32.72)
2286 INT WINAPI WSASend( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2287 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2288 LPWSAOVERLAPPED lpOverlapped,
2289 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2291 return WSASendTo( s, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags,
2292 NULL, 0, lpOverlapped, lpCompletionRoutine );
2295 /***********************************************************************
2296 * WSASendDisconnect (WS2_32.73)
2298 INT WINAPI WSASendDisconnect( SOCKET s, LPWSABUF lpBuffers )
2300 return WS_shutdown( s, SD_SEND );
2304 /***********************************************************************
2305 * WSASendTo (WS2_32.74)
2307 INT WINAPI WSASendTo( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2308 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2309 const struct WS_sockaddr *to, int tolen,
2310 LPWSAOVERLAPPED lpOverlapped,
2311 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2314 int n, fd, err = WSAENOTSOCK, flags, ret;
2315 struct iovec* iovec;
2316 struct ws2_async *wsa;
2317 IO_STATUS_BLOCK* iosb;
2319 TRACE("socket %04x, wsabuf %p, nbufs %ld, flags %ld, to %p, tolen %d, ovl %p, func %p\n",
2320 s, lpBuffers, dwBufferCount, dwFlags,
2321 to, tolen, lpOverlapped, lpCompletionRoutine);
2323 fd = get_sock_fd( s, GENERIC_WRITE, &flags );
2324 TRACE( "fd=%d, flags=%x\n", fd, flags );
2326 if ( fd == -1 ) return SOCKET_ERROR;
2328 if (flags & FD_FLAG_SEND_SHUTDOWN)
2330 WSASetLastError( WSAESHUTDOWN );
2334 if ( !lpNumberOfBytesSent )
2340 iovec = HeapAlloc(GetProcessHeap(), 0, dwBufferCount * sizeof(struct iovec) );
2348 for ( i = 0; i < dwBufferCount; i++ )
2350 iovec[i].iov_base = lpBuffers[i].buf;
2351 iovec[i].iov_len = lpBuffers[i].len;
2354 if ( (lpOverlapped || lpCompletionRoutine) && flags & FD_FLAG_OVERLAPPED )
2356 wsa = WS2_make_async( s, fd, ws2m_write, iovec, dwBufferCount,
2357 &dwFlags, (struct WS_sockaddr*) to, &tolen,
2358 lpOverlapped, lpCompletionRoutine, &iosb );
2365 if ( ( ret = ws2_queue_async( wsa, iosb ) ) )
2367 err = NtStatusToWSAError( ret );
2369 if ( !lpOverlapped )
2370 HeapFree( GetProcessHeap(), 0, iosb );
2371 HeapFree( GetProcessHeap(), 0, wsa );
2375 /* Try immediate completion */
2378 if ( WSAGetOverlappedResult( s, lpOverlapped,
2379 lpNumberOfBytesSent, FALSE, &dwFlags) )
2382 if ( (err = WSAGetLastError()) != WSA_IO_INCOMPLETE )
2386 WSASetLastError( WSA_IO_PENDING );
2387 return SOCKET_ERROR;
2390 if (_is_blocking(s))
2392 /* FIXME: exceptfds? */
2393 int timeout = GET_SNDTIMEO(fd);
2394 if( !do_block(fd, POLLOUT, timeout)) {
2396 goto err_free; /* msdn says a timeout in send is fatal */
2400 n = WS2_send( fd, iovec, dwBufferCount, to, tolen, dwFlags );
2404 if ( err == WSAEWOULDBLOCK )
2405 _enable_event(SOCKET2HANDLE(s), FD_WRITE, 0, 0);
2409 TRACE(" -> %i bytes\n", n);
2410 *lpNumberOfBytesSent = n;
2412 HeapFree( GetProcessHeap(), 0, iovec );
2413 release_sock_fd( s, fd );
2417 HeapFree( GetProcessHeap(), 0, iovec );
2420 release_sock_fd( s, fd );
2423 WARN(" -> ERROR %d\n", err);
2424 WSASetLastError(err);
2425 return SOCKET_ERROR;
2428 /***********************************************************************
2429 * sendto (WS2_32.20)
2431 int WINAPI WS_sendto(SOCKET s, const char *buf, int len, int flags,
2432 const struct WS_sockaddr *to, int tolen)
2438 wsabuf.buf = (char*) buf;
2440 if ( WSASendTo(s, &wsabuf, 1, &n, flags, to, tolen, NULL, NULL) == SOCKET_ERROR )
2441 return SOCKET_ERROR;
2446 /***********************************************************************
2447 * setsockopt (WS2_32.21)
2449 int WINAPI WS_setsockopt(SOCKET s, int level, int optname,
2450 const char *optval, int optlen)
2454 struct linger linger;
2455 struct timeval tval;
2457 TRACE("socket: %04x, level %d, name %d, ptr %p, len %d\n",
2458 s, level, optname, optval, optlen);
2460 /* SO_OPENTYPE does not require a valid socket handle. */
2461 if (level == WS_SOL_SOCKET && optname == WS_SO_OPENTYPE)
2463 if (optlen < sizeof(int) || !optval)
2465 SetLastError(WSAEFAULT);
2466 return SOCKET_ERROR;
2468 get_per_thread_data()->opentype = *(int *)optval;
2469 TRACE("setting global SO_OPENTYPE to 0x%x\n", *(int *)optval );
2473 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
2474 * socket. This will either not happen under windows or it is ignored in
2475 * windows (but it works in linux and therefore prevents the game from
2476 * finding games outside the current network) */
2477 if ( level==WS_SOL_SOCKET && optname==WS_SO_DONTROUTE )
2479 FIXME("Does windows ignore SO_DONTROUTE?\n");
2484 if(level == NSPROTO_IPX)
2489 fd = get_sock_fd( s, 0, NULL );
2490 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(int*)optval, fd);
2492 /* We try to set the ipx type on ipx socket level. */
2494 if(setsockopt(fd, SOL_IPX, IPX_TYPE, optval, optlen) == -1)
2496 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
2497 return SOCKET_ERROR;
2502 /* Should we retrieve val using a getsockopt call and then
2503 * set the modified one? */
2504 val.ipx_pt = *optval;
2505 setsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, sizeof(struct ipx));
2508 release_sock_fd( s, fd );
2510 case IPX_FILTERPTYPE:
2511 /* Sets the receive filter packet type, at the moment we don't support it */
2512 FIXME("IPX_FILTERPTYPE: %x\n", *optval);
2514 /* Returning 0 is better for now than returning a SOCKET_ERROR */
2517 FIXME("opt_name:%x\n", optname);
2518 return SOCKET_ERROR;
2524 /* Is a privileged and useless operation, so we don't. */
2525 if ((optname == WS_SO_DEBUG) && (level == WS_SOL_SOCKET))
2527 FIXME("(%d,SOL_SOCKET,SO_DEBUG,%p(%ld)) attempted (is privileged). Ignoring.\n",s,optval,*(DWORD*)optval);
2531 if(optname == WS_SO_DONTLINGER && level == WS_SOL_SOCKET) {
2532 /* This is unique to WinSock and takes special conversion */
2533 linger.l_onoff = *((int*)optval) ? 0: 1;
2534 linger.l_linger = 0;
2536 optval = (char*)&linger;
2537 optlen = sizeof(struct linger);
2542 if (!convert_sockopt(&level, &optname)) {
2543 ERR("Invalid level (%d) or optname (%d)\n", level, optname);
2544 SetLastError(WSAENOPROTOOPT);
2545 return SOCKET_ERROR;
2547 if (optname == SO_LINGER && optval) {
2548 linger.l_onoff = ((LINGER*)optval)->l_onoff;
2549 linger.l_linger = ((LINGER*)optval)->l_linger;
2550 /* FIXME: what is documented behavior if SO_LINGER optval
2552 optval = (char*)&linger;
2553 optlen = sizeof(struct linger);
2555 else if (optval && optlen < sizeof(int))
2557 woptval= *((INT16 *) optval);
2558 optval= (char*) &woptval;
2561 if (level == SOL_SOCKET && is_timeout_option(optname))
2563 if (optlen == sizeof(UINT32)) {
2564 /* WinSock passes miliseconds instead of struct timeval */
2565 tval.tv_usec = (*(PUINT32)optval % 1000) * 1000;
2566 tval.tv_sec = *(PUINT32)optval / 1000;
2567 /* min of 500 milisec */
2568 if (tval.tv_sec == 0 && tval.tv_usec < 500000)
2569 tval.tv_usec = 500000;
2570 optlen = sizeof(struct timeval);
2571 optval = (char*)&tval;
2572 } else if (optlen == sizeof(struct timeval)) {
2573 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen);
2575 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen);
2579 if (level == SOL_SOCKET && optname == SO_RCVBUF && *(int*)optval < 2048)
2581 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(int*)optval );
2587 fd = get_sock_fd( s, 0, NULL );
2588 if (fd == -1) return SOCKET_ERROR;
2590 if (setsockopt(fd, level, optname, optval, optlen) == 0)
2592 release_sock_fd( s, fd );
2595 TRACE("Setting socket error, %d\n", wsaErrno());
2596 SetLastError(wsaErrno());
2597 release_sock_fd( s, fd );
2598 return SOCKET_ERROR;
2601 /***********************************************************************
2602 * shutdown (WS2_32.22)
2604 int WINAPI WS_shutdown(SOCKET s, int how)
2606 int fd, fd0 = -1, fd1 = -1, flags, err = WSAENOTSOCK;
2607 unsigned int clear_flags = 0;
2609 fd = get_sock_fd( s, 0, &flags );
2610 TRACE("socket %04x, how %i %x\n", s, how, flags );
2613 return SOCKET_ERROR;
2617 case 0: /* drop receives */
2618 clear_flags |= FD_READ;
2620 case 1: /* drop sends */
2621 clear_flags |= FD_WRITE;
2623 case 2: /* drop all */
2624 clear_flags |= FD_READ|FD_WRITE;
2626 clear_flags |= FD_WINE_LISTENING;
2629 if ( flags & FD_FLAG_OVERLAPPED ) {
2642 fd1 = get_sock_fd( s, 0, NULL );
2648 err = WS2_register_async_shutdown( s, fd0, ws2m_sd_read );
2651 release_sock_fd( s, fd0 );
2657 err = WS2_register_async_shutdown( s, fd1, ws2m_sd_write );
2660 release_sock_fd( s, fd1 );
2665 else /* non-overlapped mode */
2667 if ( shutdown( fd, how ) )
2670 release_sock_fd( s, fd );
2673 release_sock_fd( s, fd );
2676 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
2677 if ( how > 1) WSAAsyncSelect( s, 0, 0, 0 );
2681 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
2682 WSASetLastError( err );
2683 return SOCKET_ERROR;
2686 /***********************************************************************
2687 * socket (WS2_32.23)
2689 SOCKET WINAPI WS_socket(int af, int type, int protocol)
2691 TRACE("af=%d type=%d protocol=%d\n", af, type, protocol);
2693 return WSASocketA( af, type, protocol, NULL, 0,
2694 get_per_thread_data()->opentype ? 0 : WSA_FLAG_OVERLAPPED );
2698 /***********************************************************************
2699 * gethostbyaddr (WS2_32.51)
2701 struct WS_hostent* WINAPI WS_gethostbyaddr(const char *addr, int len, int type)
2703 struct WS_hostent *retval = NULL;
2704 struct hostent* host;
2706 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2709 struct hostent hostentry;
2712 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2714 int res = gethostbyaddr_r(addr, len, type,
2715 &hostentry, extrabuf, ebufsize, &host, &locerr);
2716 if( res != ERANGE) break;
2718 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2720 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2722 EnterCriticalSection( &csWSgetXXXbyYYY );
2723 host = gethostbyaddr(addr, len, type);
2724 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2726 if( host != NULL ) retval = WS_dup_he(host);
2727 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2728 HeapFree(GetProcessHeap(),0,extrabuf);
2730 LeaveCriticalSection( &csWSgetXXXbyYYY );
2732 TRACE("ptr %p, len %d, type %d ret %p\n", addr, len, type, retval);
2736 /***********************************************************************
2737 * gethostbyname (WS2_32.52)
2739 struct WS_hostent* WINAPI WS_gethostbyname(const char* name)
2741 struct WS_hostent *retval = NULL;
2742 struct hostent* host;
2743 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2746 struct hostent hostentry;
2747 int locerr = ENOBUFS;
2752 if( gethostname( buf, 100) == -1) {
2753 SetLastError( WSAENOBUFS); /* appropriate ? */
2757 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2759 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2761 int res = gethostbyname_r(name, &hostentry, extrabuf, ebufsize, &host, &locerr);
2762 if( res != ERANGE) break;
2764 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2766 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2768 EnterCriticalSection( &csWSgetXXXbyYYY );
2769 host = gethostbyname(name);
2770 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2772 if (host) retval = WS_dup_he(host);
2773 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2774 HeapFree(GetProcessHeap(),0,extrabuf);
2776 LeaveCriticalSection( &csWSgetXXXbyYYY );
2778 TRACE( "%s ret %p\n", debugstr_a(name), retval );
2783 /***********************************************************************
2784 * getprotobyname (WS2_32.53)
2786 struct WS_protoent* WINAPI WS_getprotobyname(const char* name)
2788 struct WS_protoent* retval = NULL;
2789 #ifdef HAVE_GETPROTOBYNAME
2790 struct protoent* proto;
2791 EnterCriticalSection( &csWSgetXXXbyYYY );
2792 if( (proto = getprotobyname(name)) != NULL )
2794 retval = WS_dup_pe(proto);
2797 MESSAGE("protocol %s not found; You might want to add "
2798 "this to /etc/protocols\n", debugstr_a(name) );
2799 SetLastError(WSANO_DATA);
2801 LeaveCriticalSection( &csWSgetXXXbyYYY );
2803 TRACE( "%s ret %p\n", debugstr_a(name), retval );
2808 /***********************************************************************
2809 * getprotobynumber (WS2_32.54)
2811 struct WS_protoent* WINAPI WS_getprotobynumber(int number)
2813 struct WS_protoent* retval = NULL;
2814 #ifdef HAVE_GETPROTOBYNUMBER
2815 struct protoent* proto;
2816 EnterCriticalSection( &csWSgetXXXbyYYY );
2817 if( (proto = getprotobynumber(number)) != NULL )
2819 retval = WS_dup_pe(proto);
2822 MESSAGE("protocol number %d not found; You might want to add "
2823 "this to /etc/protocols\n", number );
2824 SetLastError(WSANO_DATA);
2826 LeaveCriticalSection( &csWSgetXXXbyYYY );
2828 TRACE("%i ret %p\n", number, retval);
2833 /***********************************************************************
2834 * getservbyname (WS2_32.55)
2836 struct WS_servent* WINAPI WS_getservbyname(const char *name, const char *proto)
2838 struct WS_servent* retval = NULL;
2839 struct servent* serv;
2841 char *proto_str = NULL;
2843 if (!(name_str = strdup_lower(name))) return NULL;
2845 if (proto && *proto)
2847 if (!(proto_str = strdup_lower(proto)))
2849 HeapFree( GetProcessHeap(), 0, name_str );
2854 EnterCriticalSection( &csWSgetXXXbyYYY );
2855 serv = getservbyname(name_str, proto_str);
2858 retval = WS_dup_se(serv);
2860 else SetLastError(WSANO_DATA);
2861 LeaveCriticalSection( &csWSgetXXXbyYYY );
2862 HeapFree( GetProcessHeap(), 0, proto_str );
2863 HeapFree( GetProcessHeap(), 0, name_str );
2864 TRACE( "%s, %s ret %p\n", debugstr_a(name), debugstr_a(proto), retval );
2869 /***********************************************************************
2870 * getservbyport (WS2_32.56)
2872 struct WS_servent* WINAPI WS_getservbyport(int port, const char *proto)
2874 struct WS_servent* retval = NULL;
2875 #ifdef HAVE_GETSERVBYPORT
2876 struct servent* serv;
2877 char *proto_str = NULL;
2879 if (proto && *proto)
2881 if (!(proto_str = strdup_lower(proto))) return NULL;
2883 EnterCriticalSection( &csWSgetXXXbyYYY );
2884 if( (serv = getservbyport(port, proto_str)) != NULL ) {
2885 retval = WS_dup_se(serv);
2887 else SetLastError(WSANO_DATA);
2888 LeaveCriticalSection( &csWSgetXXXbyYYY );
2889 HeapFree( GetProcessHeap(), 0, proto_str );
2891 TRACE("%d (i.e. port %d), %s ret %p\n", port, (int)ntohl(port), debugstr_a(proto), retval);
2896 /***********************************************************************
2897 * gethostname (WS2_32.57)
2899 int WINAPI WS_gethostname(char *name, int namelen)
2901 TRACE("name %p, len %d\n", name, namelen);
2903 if (gethostname(name, namelen) == 0)
2905 TRACE("<- '%s'\n", name);
2908 SetLastError((errno == EINVAL) ? WSAEFAULT : wsaErrno());
2909 TRACE("<- ERROR !\n");
2910 return SOCKET_ERROR;
2914 /* ------------------------------------- Windows sockets extensions -- *
2916 * ------------------------------------------------------------------- */
2918 /***********************************************************************
2919 * WSAEnumNetworkEvents (WS2_32.36)
2921 int WINAPI WSAEnumNetworkEvents(SOCKET s, WSAEVENT hEvent, LPWSANETWORKEVENTS lpEvent)
2925 TRACE("%08x, hEvent %p, lpEvent %p\n", s, hEvent, lpEvent );
2927 SERVER_START_REQ( get_socket_event )
2929 req->handle = SOCKET2HANDLE(s);
2930 req->service = TRUE;
2931 req->c_event = hEvent;
2932 wine_server_set_reply( req, lpEvent->iErrorCode, sizeof(lpEvent->iErrorCode) );
2933 if (!(ret = wine_server_call(req))) lpEvent->lNetworkEvents = reply->pmask & reply->mask;
2937 SetLastError(WSAEINVAL);
2938 return SOCKET_ERROR;
2941 /***********************************************************************
2942 * WSAEventSelect (WS2_32.39)
2944 int WINAPI WSAEventSelect(SOCKET s, WSAEVENT hEvent, long lEvent)
2948 TRACE("%08x, hEvent %p, event %08x\n", s, hEvent, (unsigned)lEvent );
2950 SERVER_START_REQ( set_socket_event )
2952 req->handle = SOCKET2HANDLE(s);
2954 req->event = hEvent;
2957 ret = wine_server_call( req );
2961 SetLastError(WSAEINVAL);
2962 return SOCKET_ERROR;
2965 /**********************************************************************
2966 * WSAGetOverlappedResult (WS2_32.40)
2968 BOOL WINAPI WSAGetOverlappedResult( SOCKET s, LPWSAOVERLAPPED lpOverlapped,
2969 LPDWORD lpcbTransfer, BOOL fWait,
2974 TRACE( "socket %04x ovl %p trans %p, wait %d flags %p\n",
2975 s, lpOverlapped, lpcbTransfer, fWait, lpdwFlags );
2977 if ( lpOverlapped == NULL )
2979 ERR( "Invalid pointer\n" );
2980 WSASetLastError(WSA_INVALID_PARAMETER);
2986 if (lpOverlapped->hEvent)
2987 while ( WaitForSingleObjectEx(lpOverlapped->hEvent,
2988 INFINITE, TRUE) == STATUS_USER_APC );
2989 else /* busy loop */
2990 while ( ((volatile OVERLAPPED*)lpOverlapped)->Internal == STATUS_PENDING )
2994 else if ( lpOverlapped->Internal == STATUS_PENDING )
2996 /* Wait in order to give APCs a chance to run. */
2997 /* This is cheating, so we must set the event again in case of success -
2998 it may be a non-manual reset event. */
2999 while ( (r = WaitForSingleObjectEx(lpOverlapped->hEvent, 0, TRUE)) == STATUS_USER_APC );
3000 if ( r == WAIT_OBJECT_0 && lpOverlapped->hEvent )
3001 NtSetEvent( lpOverlapped->hEvent, NULL );
3005 *lpcbTransfer = lpOverlapped->InternalHigh;
3008 *lpdwFlags = lpOverlapped->u.s.Offset;
3010 switch ( lpOverlapped->Internal )
3012 case STATUS_SUCCESS:
3014 case STATUS_PENDING:
3015 WSASetLastError( WSA_IO_INCOMPLETE );
3016 if (fWait) ERR("PENDING status after waiting!\n");
3019 WSASetLastError( NtStatusToWSAError( lpOverlapped->Internal ));
3025 /***********************************************************************
3026 * WSAAsyncSelect (WS2_32.101)
3028 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, long lEvent)
3032 TRACE("%x, hWnd %p, uMsg %08x, event %08lx\n", s, hWnd, uMsg, lEvent );
3034 SERVER_START_REQ( set_socket_event )
3036 req->handle = SOCKET2HANDLE(s);
3041 ret = wine_server_call( req );
3045 SetLastError(WSAEINVAL);
3046 return SOCKET_ERROR;
3049 /***********************************************************************
3050 * WSACreateEvent (WS2_32.31)
3053 WSAEVENT WINAPI WSACreateEvent(void)
3055 /* Create a manual-reset event, with initial state: unsignaled */
3058 return CreateEventW(NULL, TRUE, FALSE, NULL);
3061 /***********************************************************************
3062 * WSACloseEvent (WS2_32.29)
3065 BOOL WINAPI WSACloseEvent(WSAEVENT event)
3067 TRACE ("event=%p\n", event);
3069 return CloseHandle(event);
3072 /***********************************************************************
3073 * WSASocketA (WS2_32.78)
3076 SOCKET WINAPI WSASocketA(int af, int type, int protocol,
3077 LPWSAPROTOCOL_INFOA lpProtocolInfo,
3078 GROUP g, DWORD dwFlags)
3081 WSAPROTOCOL_INFOW info;
3083 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%lx\n",
3084 af, type, protocol, lpProtocolInfo, g, dwFlags);
3086 if (!lpProtocolInfo) return WSASocketW(af, type, protocol, NULL, g, dwFlags);
3088 memcpy(&info, lpProtocolInfo, FIELD_OFFSET(WSAPROTOCOL_INFOW, szProtocol));
3089 len = MultiByteToWideChar(CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
3090 info.szProtocol, WSAPROTOCOL_LEN + 1);
3094 WSASetLastError( WSAEINVAL);
3095 return SOCKET_ERROR;
3098 return WSASocketW(af, type, protocol, &info, g, dwFlags);
3101 /***********************************************************************
3102 * WSASocketW (WS2_32.79)
3105 SOCKET WINAPI WSASocketW(int af, int type, int protocol,
3106 LPWSAPROTOCOL_INFOW lpProtocolInfo,
3107 GROUP g, DWORD dwFlags)
3112 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
3113 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
3116 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%lx\n",
3117 af, type, protocol, lpProtocolInfo, g, dwFlags );
3119 /* hack for WSADuplicateSocket */
3120 if (lpProtocolInfo && lpProtocolInfo->dwServiceFlags4 == 0xff00ff00) {
3121 ret = lpProtocolInfo->dwCatalogEntryId;
3122 TRACE("\tgot duplicate %04x\n", ret);
3126 /* check the socket family */
3130 case WS_AF_IPX: af = AF_IPX;
3136 SetLastError(WSAEAFNOSUPPORT);
3137 return INVALID_SOCKET;
3140 /* check the socket type */
3143 case WS_SOCK_STREAM:
3153 SetLastError(WSAESOCKTNOSUPPORT);
3154 return INVALID_SOCKET;
3157 /* check the protocol type */
3158 if ( protocol < 0 ) /* don't support negative values */
3160 SetLastError(WSAEPROTONOSUPPORT);
3161 return INVALID_SOCKET;
3164 if ( af == AF_UNSPEC) /* did they not specify the address family? */
3168 if (type == SOCK_STREAM) { af = AF_INET; break; }
3170 if (type == SOCK_DGRAM) { af = AF_INET; break; }
3171 default: SetLastError(WSAEPROTOTYPE); return INVALID_SOCKET;
3174 SERVER_START_REQ( create_socket )
3178 req->protocol = protocol;
3179 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
3180 req->flags = dwFlags;
3181 req->inherit = TRUE;
3182 set_error( wine_server_call( req ) );
3183 ret = HANDLE2SOCKET( reply->handle );
3188 TRACE("\tcreated %04x\n", ret );
3192 if (GetLastError() == WSAEACCES) /* raw socket denied */
3194 if (type == SOCK_RAW)
3195 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, will fail unless running as root\n");
3197 MESSAGE("WS_SOCKET: not enough privileges to create socket, try running as root\n");
3198 SetLastError(WSAESOCKTNOSUPPORT);
3201 WARN("\t\tfailed!\n");
3202 return INVALID_SOCKET;
3205 /***********************************************************************
3206 * WSAJoinLeaf (WS2_32.58)
3209 SOCKET WINAPI WSAJoinLeaf(
3211 const struct WS_sockaddr *addr,
3213 LPWSABUF lpCallerData,
3214 LPWSABUF lpCalleeData,
3220 return INVALID_SOCKET;
3223 /***********************************************************************
3224 * __WSAFDIsSet (WS2_32.151)
3226 int WINAPI __WSAFDIsSet(SOCKET s, WS_fd_set *set)
3228 int i = set->fd_count;
3230 TRACE("(%d,%8lx(%i))\n", s,(unsigned long)set, i);
3233 if (set->fd_array[i] == s) return 1;
3237 /***********************************************************************
3238 * WSAIsBlocking (WINSOCK.114)
3239 * WSAIsBlocking (WS2_32.114)
3241 BOOL WINAPI WSAIsBlocking(void)
3243 /* By default WinSock should set all its sockets to non-blocking mode
3244 * and poll in PeekMessage loop when processing "blocking" ones. This
3245 * function is supposed to tell if the program is in this loop. Our
3246 * blocking calls are truly blocking so we always return FALSE.
3248 * Note: It is allowed to call this function without prior WSAStartup().
3255 /***********************************************************************
3256 * WSACancelBlockingCall (WINSOCK.113)
3257 * WSACancelBlockingCall (WS2_32.113)
3259 INT WINAPI WSACancelBlockingCall(void)
3265 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x )
3267 FIXME("How was this called?\n");
3272 /***********************************************************************
3273 * WSASetBlockingHook (WS2_32.109)
3275 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
3277 FARPROC prev = blocking_hook;
3278 blocking_hook = lpBlockFunc;
3279 TRACE("hook %p\n", lpBlockFunc);
3284 /***********************************************************************
3285 * WSAUnhookBlockingHook (WS2_32.110)
3287 INT WINAPI WSAUnhookBlockingHook(void)
3289 blocking_hook = WSA_DefaultBlockingHook;
3294 /* ----------------------------------- end of API stuff */
3296 /* ----------------------------------- helper functions -
3298 * TODO: Merge WS_dup_..() stuff into one function that
3299 * would operate with a generic structure containing internal
3300 * pointers (via a template of some kind).
3303 static int list_size(char** l, int item_size)
3308 j += (item_size) ? item_size : strlen(l[i]) + 1;
3309 j += (i + 1) * sizeof(char*); }
3313 static int list_dup(char** l_src, char** l_to, int item_size)
3318 for (i = 0; l_src[i]; i++) ;
3319 p = (char *)(l_to + i + 1);
3320 for (i = 0; l_src[i]; i++)
3322 int count = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
3323 memcpy(p, l_src[i], count);
3328 return p - (char *)l_to;
3333 /* duplicate hostent entry
3334 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
3335 * Dito for protoent and servent.
3337 static struct WS_hostent *WS_dup_he(const struct hostent* p_he)
3340 struct WS_hostent *p_to;
3342 int size = (sizeof(*p_he) +
3343 strlen(p_he->h_name) + 1 +
3344 list_size(p_he->h_aliases, 0) +
3345 list_size(p_he->h_addr_list, p_he->h_length));
3347 if (!(p_to = check_buffer_he(size))) return NULL;
3348 p_to->h_addrtype = p_he->h_addrtype;
3349 p_to->h_length = p_he->h_length;
3351 p = (char *)(p_to + 1);
3353 strcpy(p, p_he->h_name);
3356 p_to->h_aliases = (char **)p;
3357 p += list_dup(p_he->h_aliases, p_to->h_aliases, 0);
3359 p_to->h_addr_list = (char **)p;
3360 list_dup(p_he->h_addr_list, p_to->h_addr_list, p_he->h_length);
3364 /* ----- protoent */
3366 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe)
3369 struct WS_protoent *p_to;
3371 int size = (sizeof(*p_pe) +
3372 strlen(p_pe->p_name) + 1 +
3373 list_size(p_pe->p_aliases, 0));
3375 if (!(p_to = check_buffer_pe(size))) return NULL;
3376 p_to->p_proto = p_pe->p_proto;
3378 p = (char *)(p_to + 1);
3380 strcpy(p, p_pe->p_name);
3383 p_to->p_aliases = (char **)p;
3384 list_dup(p_pe->p_aliases, p_to->p_aliases, 0);
3390 static struct WS_servent *WS_dup_se(const struct servent* p_se)
3393 struct WS_servent *p_to;
3395 int size = (sizeof(*p_se) +
3396 strlen(p_se->s_proto) + 1 +
3397 strlen(p_se->s_name) + 1 +
3398 list_size(p_se->s_aliases, 0));
3400 if (!(p_to = check_buffer_se(size))) return NULL;
3401 p_to->s_port = p_se->s_port;
3403 p = (char *)(p_to + 1);
3405 strcpy(p, p_se->s_name);
3409 strcpy(p, p_se->s_proto);
3412 p_to->s_aliases = (char **)p;
3413 list_dup(p_se->s_aliases, p_to->s_aliases, 0);
3417 /* ----------------------------------- error handling */
3421 int loc_errno = errno;
3422 WARN("errno %d, (%s).\n", loc_errno, strerror(loc_errno));
3426 case EINTR: return WSAEINTR;
3427 case EBADF: return WSAEBADF;
3429 case EACCES: return WSAEACCES;
3430 case EFAULT: return WSAEFAULT;
3431 case EINVAL: return WSAEINVAL;
3432 case EMFILE: return WSAEMFILE;
3433 case EWOULDBLOCK: return WSAEWOULDBLOCK;
3434 case EINPROGRESS: return WSAEINPROGRESS;
3435 case EALREADY: return WSAEALREADY;
3436 case ENOTSOCK: return WSAENOTSOCK;
3437 case EDESTADDRREQ: return WSAEDESTADDRREQ;
3438 case EMSGSIZE: return WSAEMSGSIZE;
3439 case EPROTOTYPE: return WSAEPROTOTYPE;
3440 case ENOPROTOOPT: return WSAENOPROTOOPT;
3441 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
3442 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
3443 case EOPNOTSUPP: return WSAEOPNOTSUPP;
3444 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
3445 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
3446 case EADDRINUSE: return WSAEADDRINUSE;
3447 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
3448 case ENETDOWN: return WSAENETDOWN;
3449 case ENETUNREACH: return WSAENETUNREACH;
3450 case ENETRESET: return WSAENETRESET;
3451 case ECONNABORTED: return WSAECONNABORTED;
3453 case ECONNRESET: return WSAECONNRESET;
3454 case ENOBUFS: return WSAENOBUFS;
3455 case EISCONN: return WSAEISCONN;
3456 case ENOTCONN: return WSAENOTCONN;
3457 case ESHUTDOWN: return WSAESHUTDOWN;
3458 case ETOOMANYREFS: return WSAETOOMANYREFS;
3459 case ETIMEDOUT: return WSAETIMEDOUT;
3460 case ECONNREFUSED: return WSAECONNREFUSED;
3461 case ELOOP: return WSAELOOP;
3462 case ENAMETOOLONG: return WSAENAMETOOLONG;
3463 case EHOSTDOWN: return WSAEHOSTDOWN;
3464 case EHOSTUNREACH: return WSAEHOSTUNREACH;
3465 case ENOTEMPTY: return WSAENOTEMPTY;
3467 case EPROCLIM: return WSAEPROCLIM;
3470 case EUSERS: return WSAEUSERS;
3473 case EDQUOT: return WSAEDQUOT;
3476 case ESTALE: return WSAESTALE;
3479 case EREMOTE: return WSAEREMOTE;
3482 /* just in case we ever get here and there are no problems */
3485 WARN("Unknown errno %d!\n", loc_errno);
3486 return WSAEOPNOTSUPP;
3490 UINT wsaHerrno(int loc_errno)
3493 WARN("h_errno %d.\n", loc_errno);
3497 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
3498 case TRY_AGAIN: return WSATRY_AGAIN;
3499 case NO_RECOVERY: return WSANO_RECOVERY;
3500 case NO_DATA: return WSANO_DATA;
3501 case ENOBUFS: return WSAENOBUFS;
3505 WARN("Unknown h_errno %d!\n", loc_errno);
3506 return WSAEOPNOTSUPP;
3511 /***********************************************************************
3512 * WSARecv (WS2_32.67)
3514 int WINAPI WSARecv(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
3515 LPDWORD NumberOfBytesReceived, LPDWORD lpFlags,
3516 LPWSAOVERLAPPED lpOverlapped,
3517 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
3519 return WSARecvFrom(s, lpBuffers, dwBufferCount, NumberOfBytesReceived, lpFlags,
3520 NULL, NULL, lpOverlapped, lpCompletionRoutine);
3523 /***********************************************************************
3524 * WSARecvFrom (WS2_32.69)
3526 INT WINAPI WSARecvFrom( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
3527 LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, struct WS_sockaddr *lpFrom,
3528 LPINT lpFromlen, LPWSAOVERLAPPED lpOverlapped,
3529 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
3533 int n, fd, err = WSAENOTSOCK, flags, ret;
3534 struct iovec* iovec;
3535 struct ws2_async *wsa;
3536 IO_STATUS_BLOCK* iosb;
3538 TRACE("socket %04x, wsabuf %p, nbufs %ld, flags %ld, from %p, fromlen %ld, ovl %p, func %p\n",
3539 s, lpBuffers, dwBufferCount, *lpFlags, lpFrom,
3540 (lpFromlen ? *lpFromlen : -1L),
3541 lpOverlapped, lpCompletionRoutine);
3543 fd = get_sock_fd( s, GENERIC_READ, &flags );
3544 TRACE( "fd=%d, flags=%x\n", fd, flags );
3546 if (fd == -1) return SOCKET_ERROR;
3548 if (flags & FD_FLAG_RECV_SHUTDOWN)
3550 WSASetLastError( WSAESHUTDOWN );
3554 iovec = HeapAlloc( GetProcessHeap(), 0, dwBufferCount * sizeof (struct iovec) );
3561 for (i = 0; i < dwBufferCount; i++)
3563 iovec[i].iov_base = lpBuffers[i].buf;
3564 iovec[i].iov_len = lpBuffers[i].len;
3567 if ( (lpOverlapped || lpCompletionRoutine) && flags & FD_FLAG_OVERLAPPED )
3569 wsa = WS2_make_async( s, fd, ws2m_read, iovec, dwBufferCount,
3570 lpFlags, lpFrom, lpFromlen,
3571 lpOverlapped, lpCompletionRoutine, &iosb );
3579 if ( ( ret = ws2_queue_async( wsa, iosb )) )
3581 err = NtStatusToWSAError( ret );
3583 if ( !lpOverlapped )
3584 HeapFree( GetProcessHeap(), 0, iosb );
3585 HeapFree( GetProcessHeap(), 0, wsa );
3589 /* Try immediate completion */
3592 if ( WSAGetOverlappedResult( s, lpOverlapped,
3593 lpNumberOfBytesRecvd, FALSE, lpFlags) )
3596 if ( (err = WSAGetLastError()) != WSA_IO_INCOMPLETE )
3600 WSASetLastError( WSA_IO_PENDING );
3601 return SOCKET_ERROR;
3604 if ( _is_blocking(s) )
3607 /* FIXME: OOB and exceptfds? */
3608 int timeout = GET_RCVTIMEO(fd);
3609 if( !do_block(fd, POLLIN, timeout)) {
3611 /* a timeout is not fatal */
3612 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
3617 n = WS2_recv( fd, iovec, dwBufferCount, lpFrom, lpFromlen, lpFlags );
3624 TRACE(" -> %i bytes\n", n);
3625 *lpNumberOfBytesRecvd = n;
3627 HeapFree(GetProcessHeap(), 0, iovec);
3628 release_sock_fd( s, fd );
3629 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
3634 HeapFree(GetProcessHeap(), 0, iovec);
3637 release_sock_fd( s, fd );
3640 WARN(" -> ERROR %d\n", err);
3641 WSASetLastError( err );
3642 return SOCKET_ERROR;
3645 /***********************************************************************
3646 * WSCInstallProvider (WS2_32.88)
3648 INT WINAPI WSCInstallProvider( const LPGUID lpProviderId,
3649 LPCWSTR lpszProviderDllPath,
3650 const LPWSAPROTOCOL_INFOW lpProtocolInfoList,
3651 DWORD dwNumberOfEntries,
3654 FIXME("(%s, %s, %p, %ld, %p): stub !\n", debugstr_guid(lpProviderId),
3655 debugstr_w(lpszProviderDllPath), lpProtocolInfoList,
3656 dwNumberOfEntries, lpErrno);
3662 /***********************************************************************
3663 * WSCDeinstallProvider (WS2_32.83)
3665 INT WINAPI WSCDeinstallProvider(LPGUID lpProviderId, LPINT lpErrno)
3667 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId), lpErrno);
3673 /***********************************************************************
3674 * WSAAccept (WS2_32.26)
3676 SOCKET WINAPI WSAAccept( SOCKET s, struct WS_sockaddr *addr, LPINT addrlen,
3677 LPCONDITIONPROC lpfnCondition, DWORD dwCallbackData)
3680 int ret = 0, size = 0;
3681 WSABUF CallerId, CallerData, CalleeId, CalleeData;
3682 /* QOS SQOS, GQOS; */
3685 SOCKADDR src_addr, dst_addr;
3687 TRACE("Socket %04x, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %ld\n",
3688 s, addr, addrlen, lpfnCondition, dwCallbackData);
3691 size = sizeof(src_addr);
3692 cs = WS_accept(s, &src_addr, &size);
3694 if (cs == SOCKET_ERROR) return SOCKET_ERROR;
3696 CallerId.buf = (char *)&src_addr;
3697 CallerId.len = sizeof(src_addr);
3699 CallerData.buf = NULL;
3700 CallerData.len = (ULONG)NULL;
3702 WS_getsockname(cs, &dst_addr, &size);
3704 CalleeId.buf = (char *)&dst_addr;
3705 CalleeId.len = sizeof(dst_addr);
3708 ret = (*lpfnCondition)(&CallerId, &CallerData, NULL, NULL,
3709 &CalleeId, &CalleeData, &g, dwCallbackData);
3714 if (addr && addrlen)
3715 addr = memcpy(addr, &src_addr, (*addrlen > size) ? size : *addrlen );
3718 SERVER_START_REQ( set_socket_deferred )
3720 req->handle = SOCKET2HANDLE(s);
3721 req->deferred = SOCKET2HANDLE(cs);
3722 if ( !wine_server_call_err ( req ) )
3724 SetLastError( WSATRY_AGAIN );
3725 WS_closesocket( cs );
3729 return SOCKET_ERROR;
3732 SetLastError(WSAECONNREFUSED);
3733 return SOCKET_ERROR;
3735 FIXME("Unknown return type from Condition function\n");
3736 SetLastError(WSAENOTSOCK);
3737 return SOCKET_ERROR;
3741 /***********************************************************************
3742 * WSADuplicateSocketA (WS2_32.32)
3744 int WINAPI WSADuplicateSocketA( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOA lpProtocolInfo )
3748 TRACE("(%d,%lx,%p)\n", s, dwProcessId, lpProtocolInfo);
3749 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
3750 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
3751 /* I don't know what the real Windoze does next, this is a hack */
3752 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
3753 * the target use the global duplicate, or we could copy a reference to us to the structure
3754 * and let the target duplicate it from us, but let's do it as simple as possible */
3755 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
3756 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
3757 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
3758 0, FALSE, DUPLICATE_SAME_ACCESS);
3759 CloseHandle(hProcess);
3760 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
3764 /***********************************************************************
3765 * WSADuplicateSocketW (WS2_32.33)
3767 int WINAPI WSADuplicateSocketW( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOW lpProtocolInfo )
3771 TRACE("(%d,%lx,%p)\n", s, dwProcessId, lpProtocolInfo);
3773 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
3774 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
3775 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
3776 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
3777 0, FALSE, DUPLICATE_SAME_ACCESS);
3778 CloseHandle(hProcess);
3779 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
3783 /***********************************************************************
3784 * WSAInstallServiceClassA (WS2_32.48)
3786 int WINAPI WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info)
3788 FIXME("Request to install service %s\n",debugstr_a(info->lpszServiceClassName));
3789 WSASetLastError(WSAEACCES);
3790 return SOCKET_ERROR;
3793 /***********************************************************************
3794 * WSAInstallServiceClassW (WS2_32.49)
3796 int WINAPI WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info)
3798 FIXME("Request to install service %s\n",debugstr_w(info->lpszServiceClassName));
3799 WSASetLastError(WSAEACCES);
3800 return SOCKET_ERROR;
3803 /***********************************************************************
3804 * WSARemoveServiceClass (WS2_32.70)
3806 int WINAPI WSARemoveServiceClass(LPGUID info)
3808 FIXME("Request to remove service %p\n",info);
3809 WSASetLastError(WSATYPE_NOT_FOUND);
3810 return SOCKET_ERROR;
3813 /***********************************************************************
3814 * WSAStringToAddressA (WS2_32.80)
3816 INT WINAPI WSAStringToAddressA(LPSTR AddressString,
3818 LPWSAPROTOCOL_INFOA lpProtocolInfo,
3819 LPSOCKADDR lpAddress,
3820 LPINT lpAddressLength)
3823 struct in_addr inetaddr;
3824 LPSTR workBuffer=NULL,ptrPort;
3826 TRACE( "(%s, %x, %p, %p, %p)\n", AddressString, AddressFamily, lpProtocolInfo,
3827 lpAddress, lpAddressLength );
3829 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
3833 workBuffer = HeapAlloc( GetProcessHeap(), 0, strlen(AddressString)+1 );
3836 strcpy(workBuffer,AddressString);
3837 switch (AddressFamily)
3840 /* caller wants to know the size of the socket buffer */
3841 if (*lpAddressLength < sizeof(SOCKADDR_IN))
3843 *lpAddressLength = sizeof(SOCKADDR_IN);
3848 /* caller wants to translate an AdressString into a SOCKADDR */
3851 memset(lpAddress,0,sizeof(SOCKADDR_IN));
3852 ((LPSOCKADDR_IN)lpAddress)->sin_family = AF_INET;
3853 ptrPort = strchr(workBuffer,':');
3856 ((LPSOCKADDR_IN)lpAddress)->sin_port = (u_short)atoi(ptrPort+1);
3860 ((LPSOCKADDR_IN)lpAddress)->sin_port = 0;
3861 if (inet_aton(workBuffer, &inetaddr) > 0)
3863 ((LPSOCKADDR_IN)lpAddress)->sin_addr.WS_s_addr = inetaddr.s_addr;
3871 FIXME("(%s, %x, %p, %p, %p) - ProtocolInfo not implemented!\n",
3872 AddressString, AddressFamily,
3873 lpProtocolInfo, lpAddress, lpAddressLength);
3877 FIXME("(%s, %x, %p, %p, %p) - AddressFamiliy not implemented!\n",
3878 AddressString, AddressFamily,
3879 lpProtocolInfo, lpAddress, lpAddressLength);
3881 HeapFree( GetProcessHeap(), 0, workBuffer );
3884 res = WSA_NOT_ENOUGH_MEMORY;
3890 WSASetLastError(res);
3891 return SOCKET_ERROR;
3894 /***********************************************************************
3895 * WSAStringToAddressW (WS2_32.81)
3897 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
3898 * If this should be the case, it would be required to map these digits
3899 * to Unicode digits (0-9) using FoldString first.
3901 INT WINAPI WSAStringToAddressW(LPWSTR AddressString,
3903 LPWSAPROTOCOL_INFOW lpProtocolInfo,
3904 LPSOCKADDR lpAddress,
3905 LPINT lpAddressLength)
3908 LPSTR workBuffer=NULL;
3909 WSAPROTOCOL_INFOA infoA;
3910 LPWSAPROTOCOL_INFOA lpProtoInfoA = NULL;
3912 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString), AddressFamily, lpProtocolInfo,
3913 lpAddress, lpAddressLength );
3915 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
3917 /* if ProtocolInfo is available - convert to ANSI variant */
3920 lpProtoInfoA = &infoA;
3921 memcpy( lpProtoInfoA, lpProtocolInfo, FIELD_OFFSET( WSAPROTOCOL_INFOA, szProtocol ) );
3923 if (!WideCharToMultiByte( CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
3924 lpProtoInfoA->szProtocol, WSAPROTOCOL_LEN+1, NULL, NULL ))
3926 WSASetLastError( WSAEINVAL);
3927 return SOCKET_ERROR;
3933 /* Translate AddressString to ANSI code page - assumes that only
3934 standard digits 0-9 are used with this API call */
3935 sBuffer = WideCharToMultiByte( CP_ACP, 0, AddressString, -1, NULL, 0, NULL, NULL );
3936 workBuffer = HeapAlloc( GetProcessHeap(), 0, sBuffer );
3940 WideCharToMultiByte( CP_ACP, 0, AddressString, -1, workBuffer, sBuffer, NULL, NULL );
3941 res = WSAStringToAddressA(workBuffer,AddressFamily,lpProtoInfoA,
3942 lpAddress,lpAddressLength);
3943 HeapFree( GetProcessHeap(), 0, workBuffer );
3947 res = WSA_NOT_ENOUGH_MEMORY;
3952 WSASetLastError(res);
3953 return SOCKET_ERROR;
3956 /***********************************************************************
3957 * WSAAddressToStringA (WS2_32.27)
3959 * See WSAAddressToStringW
3961 INT WINAPI WSAAddressToStringA( LPSOCKADDR sockaddr, DWORD len,
3962 LPWSAPROTOCOL_INFOA info, LPSTR string,
3966 CHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
3969 TRACE( "(%p, %ld, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
3971 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
3972 if (!string || !lenstr) return SOCKET_ERROR;
3974 /* sin_family is garanteed to be the first u_short */
3975 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
3977 sprintf( buffer, "%u.%u.%u.%u:%u",
3978 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
3979 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
3980 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
3981 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
3982 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
3984 p = strchr( buffer, ':' );
3985 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
3987 size = strlen( buffer );
3992 WSASetLastError(WSAEFAULT);
3993 return SOCKET_ERROR;
3996 strcpy( string, buffer );
4000 /***********************************************************************
4001 * WSAAddressToStringW (WS2_32.28)
4003 * Convert a sockaddr address into a readable address string.
4006 * sockaddr [I] Pointer to a sockaddr structure.
4007 * len [I] Size of the sockaddr structure.
4008 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
4009 * string [I/O] Pointer to a buffer to receive the address string.
4010 * lenstr [I/O] Size of the receive buffer in WCHARs.
4014 * Failure: SOCKET_ERROR
4017 * The 'info' parameter is ignored.
4020 * Only supports AF_INET addresses.
4022 INT WINAPI WSAAddressToStringW( LPSOCKADDR sockaddr, DWORD len,
4023 LPWSAPROTOCOL_INFOW info, LPWSTR string,
4027 WCHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4028 static const WCHAR format[] = { '%','u','.','%','u','.','%','u','.','%','u',':','%','u',0 };
4031 TRACE( "(%p, %lx, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
4033 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
4034 if (!string || !lenstr) return SOCKET_ERROR;
4036 /* sin_family is garanteed to be the first u_short */
4037 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
4039 sprintfW( buffer, format,
4040 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
4041 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
4042 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
4043 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
4044 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
4046 p = strchrW( buffer, ':' );
4047 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
4049 size = lstrlenW( buffer );
4054 return SOCKET_ERROR;
4057 lstrcpyW( string, buffer );
4061 /***********************************************************************
4062 * WSAEnumNameSpaceProvidersA (WS2_32.34)
4064 INT WINAPI WSAEnumNameSpaceProvidersA( LPDWORD len, LPWSANAMESPACE_INFOA buffer )
4066 FIXME( "(%p %p) Stub!\n", len, buffer );
4070 /***********************************************************************
4071 * WSAEnumNameSpaceProvidersW (WS2_32.35)
4073 INT WINAPI WSAEnumNameSpaceProvidersW( LPDWORD len, LPWSANAMESPACE_INFOW buffer )
4075 FIXME( "(%p %p) Stub!\n", len, buffer );
4079 /***********************************************************************
4080 * WSAGetQOSByName (WS2_32.41)
4082 BOOL WINAPI WSAGetQOSByName( SOCKET s, LPWSABUF lpQOSName, LPQOS lpQOS )
4084 FIXME( "(0x%04x %p %p) Stub!\n", s, lpQOSName, lpQOS );
4088 /***********************************************************************
4089 * WSAGetServiceClassInfoA (WS2_32.42)
4091 INT WINAPI WSAGetServiceClassInfoA( LPGUID provider, LPGUID service, LPDWORD len,
4092 LPWSASERVICECLASSINFOA 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 * WSAGetServiceClassInfoW (WS2_32.43)
4103 INT WINAPI WSAGetServiceClassInfoW( LPGUID provider, LPGUID service, LPDWORD len,
4104 LPWSASERVICECLASSINFOW info )
4106 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider), debugstr_guid(service),
4108 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4109 return SOCKET_ERROR;
4112 /***********************************************************************
4113 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
4115 INT WINAPI WSAGetServiceClassNameByClassIdA( LPGUID class, LPSTR service, LPDWORD len )
4117 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4118 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4119 return SOCKET_ERROR;
4122 /***********************************************************************
4123 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
4125 INT WINAPI WSAGetServiceClassNameByClassIdW( LPGUID class, LPWSTR service, LPDWORD len )
4127 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4128 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4129 return SOCKET_ERROR;
4132 /***********************************************************************
4133 * WSALookupServiceBeginA (WS2_32.59)
4135 INT WINAPI WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions,
4136 DWORD dwControlFlags,
4139 FIXME("(%p 0x%08lx %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4141 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4142 return SOCKET_ERROR;
4145 /***********************************************************************
4146 * WSALookupServiceBeginW (WS2_32.60)
4148 INT WINAPI WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions,
4149 DWORD dwControlFlags,
4152 FIXME("(%p 0x%08lx %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4154 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4155 return SOCKET_ERROR;
4158 /***********************************************************************
4159 * WSALookupServiceBeginW (WS2_32.61)
4161 INT WINAPI WSALookupServiceEnd( HANDLE lookup )
4163 FIXME("(%p) Stub!\n", lookup );
4167 /***********************************************************************
4168 * WSALookupServiceNextA (WS2_32.62)
4170 INT WINAPI WSALookupServiceNextA( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETA results )
4172 FIXME( "(%p 0x%08lx %p %p) Stub!\n", lookup, flags, len, results );
4176 /***********************************************************************
4177 * WSALookupServiceNextW (WS2_32.63)
4179 INT WINAPI WSALookupServiceNextW( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETW results )
4181 FIXME( "(%p 0x%08lx %p %p) Stub!\n", lookup, flags, len, results );
4185 /***********************************************************************
4186 * WSANtohl (WS2_32.64)
4188 INT WINAPI WSANtohl( SOCKET s, u_long netlong, u_long* lphostlong )
4190 TRACE( "(0x%04x 0x%08lx %p)\n", s, netlong, lphostlong );
4192 if (!lphostlong) return WSAEFAULT;
4194 *lphostlong = ntohl( netlong );
4198 /***********************************************************************
4199 * WSANtohs (WS2_32.65)
4201 INT WINAPI WSANtohs( SOCKET s, u_short netshort, u_short* lphostshort )
4203 TRACE( "(0x%04x 0x%08x %p)\n", s, netshort, lphostshort );
4205 if (!lphostshort) return WSAEFAULT;
4207 *lphostshort = ntohs( netshort );
4211 /***********************************************************************
4212 * WSAProviderConfigChange (WS2_32.66)
4214 INT WINAPI WSAProviderConfigChange( LPHANDLE handle, LPWSAOVERLAPPED overlapped,
4215 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion )
4217 FIXME( "(%p %p %p) Stub!\n", handle, overlapped, completion );
4218 return SOCKET_ERROR;
4221 /***********************************************************************
4222 * WSARecvDisconnect (WS2_32.68)
4224 INT WINAPI WSARecvDisconnect( SOCKET s, LPWSABUF disconnectdata )
4226 TRACE( "(0x%04x %p)\n", s, disconnectdata );
4228 return WS_shutdown( s, 0 );
4231 /***********************************************************************
4232 * WSASetServiceA (WS2_32.76)
4234 INT WINAPI WSASetServiceA( LPWSAQUERYSETA query, WSAESETSERVICEOP operation, DWORD flags )
4236 FIXME( "(%p 0x%08x 0x%08lx) Stub!\n", query, operation, flags );
4240 /***********************************************************************
4241 * WSASetServiceW (WS2_32.77)
4243 INT WINAPI WSASetServiceW( LPWSAQUERYSETW query, WSAESETSERVICEOP operation, DWORD flags )
4245 FIXME( "(%p 0x%08x 0x%08lx) Stub!\n", query, operation, flags );
4249 /***********************************************************************
4250 * WSCEnableNSProvider (WS2_32.84)
4252 INT WINAPI WSCEnableNSProvider( LPGUID provider, BOOL enable )
4254 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider), enable );
4258 /***********************************************************************
4259 * WSCGetProviderPath (WS2_32.86)
4261 INT WINAPI WSCGetProviderPath( LPGUID provider, LPWSTR path, LPINT len, LPINT errcode )
4263 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider), path, len, errcode );
4265 if (!errcode || !provider || !len) return WSAEFAULT;
4267 *errcode = WSAEINVAL;
4268 return SOCKET_ERROR;
4271 /***********************************************************************
4272 * WSCInstallNameSpace (WS2_32.87)
4274 INT WINAPI WSCInstallNameSpace( LPWSTR identifier, LPWSTR path, DWORD namespace,
4275 DWORD version, LPGUID provider )
4277 FIXME( "(%s %s 0x%08lx 0x%08lx %s) Stub!\n", debugstr_w(identifier), debugstr_w(path),
4278 namespace, version, debugstr_guid(provider) );
4282 /***********************************************************************
4283 * WSCUnInstallNameSpace (WS2_32.89)
4285 INT WINAPI WSCUnInstallNameSpace( LPGUID lpProviderId )
4287 FIXME("(%p) Stub!\n", lpProviderId);
4291 /***********************************************************************
4292 * WSCWriteProviderOrder (WS2_32.91)
4294 INT WINAPI WSCWriteProviderOrder( LPDWORD entry, DWORD number )
4296 FIXME("(%p 0x%08lx) Stub!\n", entry, number);