2 * based on Windows Sockets 1.1 specs
3 * (ftp.microsoft.com:/Advsys/winsock/spec11/WINSOCK.TXT)
5 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * NOTE: If you make any changes to fix a particular app, make sure
22 * they don't break something else like Netscape or telnet and ftp
23 * clients and servers (www.winsite.com got a lot of those).
27 #include "wine/port.h"
32 #include <sys/types.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 # include <sys/ioctl.h>
39 #ifdef HAVE_SYS_FILIO_H
40 # include <sys/filio.h>
42 #ifdef HAVE_SYS_SOCKIO_H
43 # include <sys/sockio.h>
47 # include <sys/so_ioctl.h>
50 #ifdef HAVE_SYS_PARAM_H
51 # include <sys/param.h>
57 #ifdef HAVE_SYS_WAIT_H
58 # include <sys/wait.h>
63 #ifdef HAVE_SYS_SOCKET_H
64 #include <sys/socket.h>
66 #ifdef HAVE_NETINET_IN_H
67 # include <netinet/in.h>
69 #ifdef HAVE_NETINET_TCP_H
70 # include <netinet/tcp.h>
72 #ifdef HAVE_ARPA_INET_H
73 # include <arpa/inet.h>
78 #ifdef HAVE_SYS_ERRNO_H
79 #include <sys/errno.h>
88 #ifdef HAVE_ARPA_NAMESER_H
89 # include <arpa/nameser.h>
98 # include <netipx/ipx.h>
101 #ifdef HAVE_IPX_LINUX
102 # include <asm/types.h>
103 # include <linux/ipx.h>
107 #ifdef HAVE_SYS_POLL_H
108 # include <sys/poll.h>
110 #ifdef HAVE_SYS_TIME_H
111 # include <sys/time.h>
114 #define NONAMELESSUNION
115 #define NONAMELESSSTRUCT
120 #include "winerror.h"
121 #include "winsock2.h"
122 #include "ws2tcpip.h"
125 #include "iphlpapi.h"
127 #include "wine/server.h"
128 #include "wine/debug.h"
131 # define sipx_network sipx_addr.x_net
132 # define sipx_node sipx_addr.x_host.c_host
133 #endif /* __FreeBSD__ */
135 WINE_DEFAULT_DEBUG_CHANNEL(winsock);
137 /* critical section to protect some non-rentrant net function */
138 extern CRITICAL_SECTION csWSgetXXXbyYYY;
140 inline static const char *debugstr_sockaddr( const struct WS_sockaddr *a )
142 if (!a) return "(nil)";
143 return wine_dbg_sprintf("{ family %d, address %s, port %d }",
144 ((struct sockaddr_in *)a)->sin_family,
145 inet_ntoa(((struct sockaddr_in *)a)->sin_addr),
146 ntohs(((struct sockaddr_in *)a)->sin_port));
149 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
150 #define SOCKET2HANDLE(s) ((HANDLE)(s))
151 #define HANDLE2SOCKET(h) ((SOCKET)(h))
153 /****************************************************************
154 * Async IO declarations
155 ****************************************************************/
158 static DWORD ws2_async_get_count (const struct async_private *ovp);
159 static void CALLBACK ws2_async_call_completion (ULONG_PTR data);
160 static void ws2_async_cleanup ( struct async_private *ovp );
162 static struct async_ops ws2_async_ops =
165 ws2_async_call_completion,
169 static struct async_ops ws2_nocomp_async_ops =
172 NULL, /* call_completion */
176 typedef struct ws2_async
179 LPWSAOVERLAPPED user_overlapped;
180 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func;
183 struct WS_sockaddr *addr;
185 int val; /* for send operations */
186 int *ptr; /* for recv operations */
191 /****************************************************************/
193 /* ----------------------------------- internal data */
195 /* ws_... struct conversion flags */
197 typedef struct /* WSAAsyncSelect() control struct */
199 HANDLE service, event, sock;
205 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
206 #define WS_MAX_UDP_DATAGRAM 1024
207 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x );
209 static struct WS_hostent *he_buffer; /* typecast for Win32 ws_hostent */
210 static struct WS_servent *se_buffer; /* typecast for Win32 ws_servent */
211 static struct WS_protoent *pe_buffer; /* typecast for Win32 ws_protoent */
212 static INT num_startup; /* reference counter */
213 static FARPROC blocking_hook = WSA_DefaultBlockingHook;
215 /* function prototypes */
216 static struct WS_hostent *WS_dup_he(const struct hostent* p_he);
217 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe);
218 static struct WS_servent *WS_dup_se(const struct servent* p_se);
220 int WSAIOCTL_GetInterfaceCount(void);
221 int WSAIOCTL_GetInterfaceName(int intNumber, char *intName);
224 UINT wsaHerrno(int errnr);
226 #define MAP_OPTION(opt) { WS_##opt, opt }
228 static const int ws_sock_map[][2] =
230 MAP_OPTION( SO_DEBUG ),
231 MAP_OPTION( SO_REUSEADDR ),
232 MAP_OPTION( SO_KEEPALIVE ),
233 MAP_OPTION( SO_DONTROUTE ),
234 MAP_OPTION( SO_BROADCAST ),
235 MAP_OPTION( SO_LINGER ),
236 MAP_OPTION( SO_OOBINLINE ),
237 MAP_OPTION( SO_SNDBUF ),
238 MAP_OPTION( SO_RCVBUF ),
239 MAP_OPTION( SO_ERROR ),
240 MAP_OPTION( SO_TYPE ),
242 MAP_OPTION( SO_RCVTIMEO ),
245 MAP_OPTION( SO_SNDTIMEO ),
250 static const int ws_tcp_map[][2] =
253 MAP_OPTION( TCP_NODELAY ),
258 static const int ws_ip_map[][2] =
260 MAP_OPTION( IP_MULTICAST_IF ),
261 MAP_OPTION( IP_MULTICAST_TTL ),
262 MAP_OPTION( IP_MULTICAST_LOOP ),
263 MAP_OPTION( IP_ADD_MEMBERSHIP ),
264 MAP_OPTION( IP_DROP_MEMBERSHIP ),
265 MAP_OPTION( IP_OPTIONS ),
267 MAP_OPTION( IP_HDRINCL ),
269 MAP_OPTION( IP_TOS ),
270 MAP_OPTION( IP_TTL ),
274 static DWORD opentype_tls_index = TLS_OUT_OF_INDEXES; /* TLS index for SO_OPENTYPE flag */
276 inline static DWORD NtStatusToWSAError ( const DWORD status )
278 /* We only need to cover the status codes set by server async request handling */
282 case STATUS_SUCCESS: wserr = 0; break;
283 case STATUS_PENDING: wserr = WSA_IO_PENDING; break;
284 case STATUS_INVALID_HANDLE: wserr = WSAENOTSOCK; break; /* WSAEBADF ? */
285 case STATUS_INVALID_PARAMETER: wserr = WSAEINVAL; break;
286 case STATUS_PIPE_DISCONNECTED: wserr = WSAESHUTDOWN; break;
287 case STATUS_CANCELLED: wserr = WSA_OPERATION_ABORTED; break;
288 case STATUS_TIMEOUT: wserr = WSAETIMEDOUT; break;
289 case STATUS_NO_MEMORY: wserr = WSAEFAULT; break;
291 if ( status >= WSABASEERR && status <= WSABASEERR+1004 )
292 /* It is not a NT status code but a winsock error */
296 wserr = RtlNtStatusToDosError( status );
297 FIXME ( "Status code %08lx converted to DOS error code %lx\n", status, wserr );
303 /* set last error code from NT status without mapping WSA errors */
304 inline static unsigned int set_error( unsigned int err )
308 err = NtStatusToWSAError ( err );
314 inline static int get_sock_fd( SOCKET s, DWORD access, int *flags )
317 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s), access, &fd, NULL, flags ) ))
322 inline static void release_sock_fd( SOCKET s, int fd )
324 wine_server_release_fd( SOCKET2HANDLE(s), fd );
327 static void _enable_event( HANDLE s, unsigned int event,
328 unsigned int sstate, unsigned int cstate )
330 SERVER_START_REQ( enable_socket_event )
334 req->sstate = sstate;
335 req->cstate = cstate;
336 wine_server_call( req );
341 static int _is_blocking(SOCKET s)
344 SERVER_START_REQ( get_socket_event )
346 req->handle = SOCKET2HANDLE(s);
347 req->service = FALSE;
349 wine_server_call( req );
350 ret = (reply->state & FD_WINE_NONBLOCKING) == 0;
356 static unsigned int _get_sock_mask(SOCKET s)
359 SERVER_START_REQ( get_socket_event )
361 req->handle = SOCKET2HANDLE(s);
362 req->service = FALSE;
364 wine_server_call( req );
371 static void _sync_sock_state(SOCKET s)
373 /* do a dummy wineserver request in order to let
374 the wineserver run through its select loop once */
375 (void)_is_blocking(s);
378 static int _get_sock_error(SOCKET s, unsigned int bit)
380 int events[FD_MAX_EVENTS];
382 SERVER_START_REQ( get_socket_event )
384 req->handle = SOCKET2HANDLE(s);
385 req->service = FALSE;
387 wine_server_set_reply( req, events, sizeof(events) );
388 wine_server_call( req );
394 static void WINSOCK_DeleteIData(void)
396 /* delete scratch buffers */
398 if (he_buffer) HeapFree( GetProcessHeap(), 0, he_buffer );
399 if (se_buffer) HeapFree( GetProcessHeap(), 0, se_buffer );
400 if (pe_buffer) HeapFree( GetProcessHeap(), 0, pe_buffer );
407 /***********************************************************************
408 * DllMain (WS2_32.init)
410 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
412 TRACE("%p 0x%lx %p\n", hInstDLL, fdwReason, fImpLoad);
414 case DLL_PROCESS_ATTACH:
415 DisableThreadLibraryCalls(hInstDLL);
416 opentype_tls_index = TlsAlloc();
418 case DLL_PROCESS_DETACH:
419 TlsFree( opentype_tls_index );
420 WINSOCK_DeleteIData();
426 /***********************************************************************
429 * Converts socket flags from Windows format.
430 * Return 1 if converted, 0 if not (error).
432 static int convert_sockopt(INT *level, INT *optname)
439 for(i=0; ws_sock_map[i][0]; i++)
441 if( ws_sock_map[i][0] == *optname )
443 *optname = ws_sock_map[i][1];
447 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname);
450 *level = IPPROTO_TCP;
451 for(i=0; ws_tcp_map[i][0]; i++)
453 if ( ws_tcp_map[i][0] == *optname )
455 *optname = ws_tcp_map[i][1];
459 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname);
463 for(i=0; ws_ip_map[i][0]; i++)
465 if (ws_ip_map[i][0] == *optname )
467 *optname = ws_ip_map[i][1];
471 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname);
473 default: FIXME("Unimplemented or unknown socket level\n");
478 static inline BOOL is_timeout_option( int optname )
481 if (optname == SO_RCVTIMEO) return TRUE;
484 if (optname == SO_SNDTIMEO) return TRUE;
489 /* ----------------------------------- Per-thread info (or per-process?) */
491 static char *strdup_lower(const char *str)
494 char *ret = HeapAlloc( GetProcessHeap(), 0, strlen(str) + 1 );
498 for (i = 0; str[i]; i++) ret[i] = tolower(str[i]);
501 else SetLastError(WSAENOBUFS);
505 static fd_set* fd_set_import( fd_set* fds, const WS_fd_set* wsfds, int access, int* highfd, int lfd[] )
507 /* translate Winsock fd set into local fd set */
513 for( i = 0; i < wsfds->fd_count; i++ )
515 int s = wsfds->fd_array[i];
516 int fd = get_sock_fd( s, access, NULL );
520 if( fd > *highfd ) *highfd = fd;
530 inline static int sock_error_p(int s)
532 unsigned int optval, optlen;
534 optlen = sizeof(optval);
535 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
536 if (optval) WARN("\t[%i] error: %d\n", s, optval);
540 static int fd_set_export( const fd_set* fds, fd_set* exceptfds, WS_fd_set* wsfds, int lfd[] )
544 /* translate local fd set into Winsock fd set, adding
545 * errors to exceptfds (only if app requested it) */
549 int i, j, count = wsfds->fd_count;
551 for( i = 0, j = 0; i < count; i++ )
554 SOCKET s = wsfds->fd_array[i];
555 if (fd == -1) continue;
556 if( FD_ISSET(fd, fds) )
558 if ( exceptfds && sock_error_p(fd) )
560 FD_SET(fd, exceptfds);
563 else wsfds->fd_array[j++] = s;
565 release_sock_fd( s, fd );
572 static void fd_set_unimport( WS_fd_set* wsfds, int lfd[] )
578 for( i = 0; i < wsfds->fd_count; i++ )
579 if ( lfd[i] >= 0 ) release_sock_fd( wsfds->fd_array[i], lfd[i] );
584 /* utility: given an fd, will block until one of the events occurs */
585 static inline int do_block( int fd, int events )
596 /* ----------------------------------- API -----
598 * Init / cleanup / error checking.
601 /***********************************************************************
602 * WSAStartup (WS2_32.115)
604 int WINAPI WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData)
606 TRACE("verReq=%x\n", wVersionRequested);
608 if (LOBYTE(wVersionRequested) < 1)
609 return WSAVERNOTSUPPORTED;
611 if (!lpWSAData) return WSAEINVAL;
615 /* that's the whole of the negotiation for now */
616 lpWSAData->wVersion = wVersionRequested;
617 /* return winsock information */
618 lpWSAData->wHighVersion = 0x0202;
619 strcpy(lpWSAData->szDescription, "WinSock 2.0" );
620 strcpy(lpWSAData->szSystemStatus, "Running" );
621 lpWSAData->iMaxSockets = WS_MAX_SOCKETS_PER_PROCESS;
622 lpWSAData->iMaxUdpDg = WS_MAX_UDP_DATAGRAM;
623 /* don't do anything with lpWSAData->lpVendorInfo */
624 /* (some apps don't allocate the space for this field) */
626 TRACE("succeeded\n");
631 /***********************************************************************
632 * WSACleanup (WS2_32.116)
634 INT WINAPI WSACleanup(void)
638 if (--num_startup > 0) return 0;
639 WINSOCK_DeleteIData();
642 SetLastError(WSANOTINITIALISED);
647 /***********************************************************************
648 * WSAGetLastError (WINSOCK.111)
649 * WSAGetLastError (WS2_32.111)
651 INT WINAPI WSAGetLastError(void)
653 return GetLastError();
656 /***********************************************************************
657 * WSASetLastError (WS2_32.112)
659 void WINAPI WSASetLastError(INT iError) {
660 SetLastError(iError);
663 static struct WS_hostent *check_buffer_he(int size)
668 if (he_len >= size ) return he_buffer;
669 HeapFree( GetProcessHeap(), 0, he_buffer );
671 he_buffer = HeapAlloc( GetProcessHeap(), 0, (he_len = size) );
672 if (!he_buffer) SetLastError(WSAENOBUFS);
676 static struct WS_servent *check_buffer_se(int size)
681 if (se_len >= size ) return se_buffer;
682 HeapFree( GetProcessHeap(), 0, se_buffer );
684 se_buffer = HeapAlloc( GetProcessHeap(), 0, (se_len = size) );
685 if (!se_buffer) SetLastError(WSAENOBUFS);
689 static struct WS_protoent *check_buffer_pe(int size)
694 if (pe_len >= size ) return pe_buffer;
695 HeapFree( GetProcessHeap(), 0, pe_buffer );
697 pe_buffer = HeapAlloc( GetProcessHeap(), 0, (pe_len = size) );
698 if (!pe_buffer) SetLastError(WSAENOBUFS);
702 /* ----------------------------------- i/o APIs */
705 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf)== WS_AF_IPX)
707 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET)
711 /**********************************************************************/
713 /* Returns the converted address if successful, NULL if it was too small to
714 * start with. Note that the returned pointer may be the original pointer
715 * if no conversion is necessary.
717 static const struct sockaddr* ws_sockaddr_ws2u(const struct WS_sockaddr* wsaddr, int wsaddrlen, int *uaddrlen)
719 switch (wsaddr->sa_family)
724 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
725 struct sockaddr_ipx* uipx;
727 if (wsaddrlen<sizeof(struct WS_sockaddr_ipx))
730 *uaddrlen=sizeof(struct sockaddr_ipx);
731 uipx=malloc(*uaddrlen);
732 memset(uipx,0,sizeof(*uipx));
733 uipx->sipx_family=AF_IPX;
734 uipx->sipx_port=wsipx->sa_socket;
735 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
738 memcpy(&uipx->sipx_network,wsipx->sa_netnum,sizeof(uipx->sipx_network)+sizeof(uipx->sipx_node));
739 #ifdef IPX_FRAME_NONE
740 uipx->sipx_type=IPX_FRAME_NONE;
742 return (const struct sockaddr*)uipx;
747 if (wsaddrlen<sizeof(struct WS_sockaddr))
750 /* No conversion needed, just return the original address */
752 return (const struct sockaddr*)wsaddr;
757 /* Allocates a Unix sockaddr structure to receive the data */
758 inline struct sockaddr* ws_sockaddr_alloc(const struct WS_sockaddr* wsaddr, int* wsaddrlen, int* uaddrlen)
762 ERR( "WINE shouldn't pass a NULL wsaddr! Attempting to continue\n" );
764 /* This is not strictly the right thing to do. Hope it works however */
773 *uaddrlen=max(sizeof(struct sockaddr),*wsaddrlen);
775 return malloc(*uaddrlen);
778 /* Returns 0 if successful, -1 if the buffer is too small */
779 static int ws_sockaddr_u2ws(const struct sockaddr* uaddr, int uaddrlen, struct WS_sockaddr* wsaddr, int* wsaddrlen)
783 switch(uaddr->sa_family)
788 struct sockaddr_ipx* uipx=(struct sockaddr_ipx*)uaddr;
789 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
792 switch (*wsaddrlen) /* how much can we copy? */
797 wsipx->sa_socket=uipx->sipx_port;
801 memcpy(wsipx->sa_nodenum,uipx->sipx_node,sizeof(wsipx->sa_nodenum));
809 memcpy(wsipx->sa_netnum,&uipx->sipx_network,sizeof(wsipx->sa_netnum));
815 wsipx->sa_family=WS_AF_IPX;
827 /* No conversion needed */
828 memcpy(wsaddr,uaddr,*wsaddrlen);
829 if (*wsaddrlen<uaddrlen) {
839 /* to be called to free the memory allocated by ws_sockaddr_ws2u or
842 inline void ws_sockaddr_free(const struct sockaddr* uaddr, const struct WS_sockaddr* wsaddr)
844 if (uaddr!=NULL && uaddr!=(const struct sockaddr*)wsaddr)
848 /**************************************************************************
849 * Functions for handling overlapped I/O
850 **************************************************************************/
852 static DWORD ws2_async_get_count (const struct async_private *ovp)
854 return ovp->iosb->Information;
857 static void ws2_async_cleanup ( struct async_private *ap )
859 struct ws2_async *as = (struct ws2_async*) ap;
861 TRACE ( "as: %p uovl %p ovl %p\n", as, as->user_overlapped, as->async.iosb );
862 if ( !as->user_overlapped )
865 /* FIXME: I don't think this is really used */
866 if ( as->overlapped->hEvent != INVALID_HANDLE_VALUE )
867 WSACloseEvent ( as->overlapped->hEvent );
869 HeapFree ( GetProcessHeap(), 0, as->async.iosb );
873 HeapFree ( GetProcessHeap(), 0, as->iovec );
875 HeapFree ( GetProcessHeap(), 0, as );
878 static void CALLBACK ws2_async_call_completion (ULONG_PTR data)
880 ws2_async* as = (ws2_async*) data;
882 TRACE ("data: %p\n", as);
884 as->completion_func ( NtStatusToWSAError (as->async.iosb->u.Status),
885 as->async.iosb->Information,
888 ws2_async_cleanup ( &as->async );
891 /***********************************************************************
892 * WS2_make_async (INTERNAL)
895 static void WS2_async_recv (async_private *as);
896 static void WS2_async_send (async_private *as);
898 inline static struct ws2_async*
899 WS2_make_async (SOCKET s, int fd, int type, struct iovec *iovec, DWORD dwBufferCount,
900 LPDWORD lpFlags, struct WS_sockaddr *addr,
901 LPINT addrlen, LPWSAOVERLAPPED lpOverlapped,
902 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
904 struct ws2_async *wsa = HeapAlloc ( GetProcessHeap(), 0, sizeof ( ws2_async ) );
906 TRACE ( "wsa %p\n", wsa );
911 wsa->async.ops = ( lpCompletionRoutine ? &ws2_async_ops : &ws2_nocomp_async_ops );
912 wsa->async.handle = (HANDLE) s;
914 wsa->async.type = type;
917 case ASYNC_TYPE_READ:
918 wsa->flags = *lpFlags;
919 wsa->async.func = WS2_async_recv;
920 wsa->addrlen.ptr = addrlen;
922 case ASYNC_TYPE_WRITE:
924 wsa->async.func = WS2_async_send;
925 wsa->addrlen.val = *addrlen;
928 ERR ("Invalid async type: %d\n", type);
930 wsa->user_overlapped = lpOverlapped;
931 wsa->completion_func = lpCompletionRoutine;
933 wsa->n_iovecs = dwBufferCount;
938 wsa->async.iosb = (IO_STATUS_BLOCK*)lpOverlapped;
939 wsa->async.event = ( lpCompletionRoutine ? INVALID_HANDLE_VALUE : lpOverlapped->hEvent );
943 wsa->async.iosb = HeapAlloc ( GetProcessHeap(), 0,
944 sizeof (IO_STATUS_BLOCK) );
945 if ( !wsa->async.iosb )
947 wsa->async.event = INVALID_HANDLE_VALUE;
950 wsa->async.iosb->Information = 0;
951 TRACE ( "wsa %p, ops %p, h %p, ev %p, fd %d, func %p, iosb %p, uov %p, cfunc %p\n",
952 wsa, wsa->async.ops, wsa->async.handle, wsa->async.event, wsa->async.fd, wsa->async.func,
953 wsa->async.iosb, wsa->user_overlapped, wsa->completion_func );
959 HeapFree ( GetProcessHeap(), 0, wsa );
963 /***********************************************************************
964 * WS2_recv (INTERNAL)
966 * Work horse for both synchronous and asynchronous recv() operations.
968 static int WS2_recv ( int fd, struct iovec* iov, int count,
969 struct WS_sockaddr *lpFrom, LPINT lpFromlen,
974 TRACE ( "fd %d, iovec %p, count %d addr %s, len %p, flags %lx\n",
975 fd, iov, count, debugstr_sockaddr(lpFrom), lpFromlen, *lpFlags);
981 hdr.msg_namelen = *lpFromlen;
982 hdr.msg_name = ws_sockaddr_alloc ( lpFrom, lpFromlen, &hdr.msg_namelen );
985 WSASetLastError ( WSAEFAULT );
994 hdr.msg_iovlen = count;
995 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
996 hdr.msg_accrights = NULL;
997 hdr.msg_accrightslen = 0;
999 hdr.msg_control = NULL;
1000 hdr.msg_controllen = 0;
1004 if ( (n = recvmsg (fd, &hdr, *lpFlags)) == -1 )
1006 TRACE ( "recvmsg error %d\n", errno);
1011 ws_sockaddr_u2ws ( hdr.msg_name, hdr.msg_namelen,
1012 lpFrom, lpFromlen ) != 0 )
1014 /* The from buffer was too small, but we read the data
1015 * anyway. Is that really bad?
1017 WSASetLastError ( WSAEFAULT );
1018 WARN ( "Address buffer too small\n" );
1023 ws_sockaddr_free ( hdr.msg_name, lpFrom );
1024 TRACE ("-> %d\n", n);
1028 /***********************************************************************
1029 * WS2_async_recv (INTERNAL)
1031 * Handler for overlapped recv() operations.
1033 static void WS2_async_recv ( async_private *as )
1035 ws2_async* wsa = (ws2_async*) as;
1038 TRACE ( "async %p\n", wsa );
1040 if ( wsa->async.iosb->u.Status != STATUS_PENDING )
1042 TRACE ( "status: %ld\n", wsa->async.iosb->u.Status );
1046 result = WS2_recv ( wsa->async.fd, wsa->iovec, wsa->n_iovecs,
1047 wsa->addr, wsa->addrlen.ptr, &wsa->flags );
1051 wsa->async.iosb->u.Status = STATUS_SUCCESS;
1052 wsa->async.iosb->Information = result;
1053 TRACE ( "received %d bytes\n", result );
1054 _enable_event ( wsa->async.handle, FD_READ, 0, 0 );
1059 if ( err == WSAEINTR || err == WSAEWOULDBLOCK ) /* errno: EINTR / EAGAIN */
1061 wsa->async.iosb->u.Status = STATUS_PENDING;
1062 _enable_event ( wsa->async.handle, FD_READ, 0, 0 );
1063 TRACE ( "still pending\n" );
1067 wsa->async.iosb->u.Status = err;
1068 TRACE ( "Error: %x\n", err );
1072 /***********************************************************************
1073 * WS2_send (INTERNAL)
1075 * Work horse for both synchronous and asynchronous send() operations.
1077 static int WS2_send ( int fd, struct iovec* iov, int count,
1078 const struct WS_sockaddr *to, INT tolen, DWORD dwFlags )
1082 TRACE ( "fd %d, iovec %p, count %d addr %s, len %d, flags %lx\n",
1083 fd, iov, count, debugstr_sockaddr(to), tolen, dwFlags);
1085 hdr.msg_name = NULL;
1089 hdr.msg_name = (struct sockaddr*) ws_sockaddr_ws2u ( to, tolen, &hdr.msg_namelen );
1090 if ( !hdr.msg_name )
1092 WSASetLastError ( WSAEFAULT );
1097 hdr.msg_namelen = 0;
1100 hdr.msg_iovlen = count;
1101 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1102 hdr.msg_accrights = NULL;
1103 hdr.msg_accrightslen = 0;
1105 hdr.msg_control = NULL;
1106 hdr.msg_controllen = 0;
1110 n = sendmsg (fd, &hdr, dwFlags);
1113 ws_sockaddr_free ( hdr.msg_name, to );
1117 /***********************************************************************
1118 * WS2_async_send (INTERNAL)
1120 * Handler for overlapped send() operations.
1122 static void WS2_async_send ( async_private *as )
1124 ws2_async* wsa = (ws2_async*) as;
1127 TRACE ( "async %p\n", wsa );
1129 if ( wsa->async.iosb->u.Status != STATUS_PENDING )
1131 TRACE ( "status: %ld\n", wsa->async.iosb->u.Status );
1135 result = WS2_send ( wsa->async.fd, wsa->iovec, wsa->n_iovecs,
1136 wsa->addr, wsa->addrlen.val, wsa->flags );
1140 wsa->async.iosb->u.Status = STATUS_SUCCESS;
1141 wsa->async.iosb->Information = result;
1142 TRACE ( "sent %d bytes\n", result );
1143 _enable_event ( wsa->async.handle, FD_WRITE, 0, 0 );
1148 if ( err == WSAEINTR )
1150 wsa->async.iosb->u.Status = STATUS_PENDING;
1151 _enable_event ( wsa->async.handle, FD_WRITE, 0, 0 );
1152 TRACE ( "still pending\n" );
1156 /* We set the status to a winsock error code and check for that
1157 later in NtStatusToWSAError () */
1158 wsa->async.iosb->u.Status = err;
1159 TRACE ( "Error: %x\n", err );
1163 /***********************************************************************
1164 * WS2_async_shutdown (INTERNAL)
1166 * Handler for shutdown() operations on overlapped sockets.
1168 static void WS2_async_shutdown ( async_private *as )
1170 ws2_async* wsa = (ws2_async*) as;
1173 TRACE ( "async %p %d\n", wsa, wsa->async.type );
1174 switch ( wsa->async.type )
1176 case ASYNC_TYPE_READ:
1177 err = shutdown ( wsa->async.fd, 0 );
1179 case ASYNC_TYPE_WRITE:
1180 err = shutdown ( wsa->async.fd, 1 );
1183 ERR ("invalid type: %d\n", wsa->async.type );
1187 wsa->async.iosb->u.Status = wsaErrno ();
1189 wsa->async.iosb->u.Status = STATUS_SUCCESS;
1192 /***********************************************************************
1193 * WS2_register_async_shutdown (INTERNAL)
1195 * Helper function for WS_shutdown() on overlapped sockets.
1197 static int WS2_register_async_shutdown ( SOCKET s, int fd, int type )
1199 struct ws2_async *wsa;
1200 int ret, err = WSAEFAULT;
1203 LPWSAOVERLAPPED ovl = HeapAlloc (GetProcessHeap(), 0, sizeof ( WSAOVERLAPPED ));
1205 TRACE ("s %d fd %d type %d\n", s, fd, type);
1209 ovl->hEvent = WSACreateEvent ();
1210 if ( ovl->hEvent == WSA_INVALID_EVENT )
1213 wsa = WS2_make_async ( s, fd, type, NULL, 0,
1214 &dwflags, NULL, &len, ovl, NULL );
1218 /* Hack: this will cause ws2_async_cleanup() to free the overlapped structure */
1219 wsa->user_overlapped = NULL;
1220 wsa->async.func = WS2_async_shutdown;
1221 if ( (ret = register_new_async ( &wsa->async )) )
1223 err = NtStatusToWSAError ( ret );
1226 /* Try immediate completion */
1227 while ( WaitForSingleObjectEx ( ovl->hEvent, 0, TRUE ) == STATUS_USER_APC );
1231 WSACloseEvent ( ovl->hEvent );
1233 HeapFree ( GetProcessHeap(), 0, ovl );
1238 /***********************************************************************
1241 SOCKET WINAPI WS_accept(SOCKET s, struct WS_sockaddr *addr,
1246 TRACE("socket %04x\n", s );
1247 if (_is_blocking(s))
1249 int fd = get_sock_fd( s, GENERIC_READ, NULL );
1250 if (fd == -1) return INVALID_SOCKET;
1252 do_block(fd, POLLIN);
1253 _sync_sock_state(s); /* let wineserver notice connection */
1254 release_sock_fd( s, fd );
1255 /* retrieve any error codes from it */
1256 SetLastError(_get_sock_error(s, FD_ACCEPT_BIT));
1257 /* FIXME: care about the error? */
1259 SERVER_START_REQ( accept_socket )
1261 req->lhandle = SOCKET2HANDLE(s);
1262 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
1263 req->inherit = TRUE;
1264 set_error( wine_server_call( req ) );
1265 as = HANDLE2SOCKET( reply->handle );
1270 if (addr) WS_getpeername(as, addr, addrlen32);
1273 return INVALID_SOCKET;
1276 /***********************************************************************
1279 int WINAPI WS_bind(SOCKET s, const struct WS_sockaddr* name, int namelen)
1281 int fd = get_sock_fd( s, 0, NULL );
1282 int res = SOCKET_ERROR;
1284 TRACE("socket %04x, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1288 if (!name || !SUPPORTED_PF(name->sa_family))
1290 SetLastError(WSAEAFNOSUPPORT);
1294 const struct sockaddr* uaddr;
1297 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1300 SetLastError(WSAEFAULT);
1304 if (bind(fd, uaddr, uaddrlen) < 0)
1306 int loc_errno = errno;
1307 WARN("\tfailure - errno = %i\n", errno);
1312 SetLastError(WSAENOTSOCK);
1315 SetLastError(WSAEINVAL);
1318 SetLastError(wsaErrno());
1324 res=0; /* success */
1326 ws_sockaddr_free(uaddr,name);
1329 release_sock_fd( s, fd );
1334 /***********************************************************************
1335 * closesocket (WS2_32.3)
1337 int WINAPI WS_closesocket(SOCKET s)
1339 TRACE("socket %08x\n", s);
1340 if (CloseHandle(SOCKET2HANDLE(s))) return 0;
1341 return SOCKET_ERROR;
1344 /***********************************************************************
1345 * connect (WS2_32.4)
1347 int WINAPI WS_connect(SOCKET s, const struct WS_sockaddr* name, int namelen)
1349 int fd = get_sock_fd( s, GENERIC_READ, NULL );
1351 TRACE("socket %04x, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1355 const struct sockaddr* uaddr;
1358 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1361 SetLastError(WSAEFAULT);
1367 rc=connect(fd, uaddr, uaddrlen);
1368 ws_sockaddr_free(uaddr,name);
1370 goto connect_success;
1373 if (errno == EINPROGRESS)
1375 /* tell wineserver that a connection is in progress */
1376 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1377 FD_CONNECT|FD_READ|FD_WRITE,
1378 FD_WINE_CONNECTED|FD_WINE_LISTENING);
1379 if (_is_blocking(s))
1383 do_block(fd, POLLIN | POLLOUT );
1384 _sync_sock_state(s); /* let wineserver notice connection */
1385 /* retrieve any error codes from it */
1386 result = _get_sock_error(s, FD_CONNECT_BIT);
1388 SetLastError(result);
1391 goto connect_success;
1396 SetLastError(WSAEWOULDBLOCK);
1401 SetLastError(wsaErrno());
1403 release_sock_fd( s, fd );
1405 return SOCKET_ERROR;
1408 release_sock_fd( s, fd );
1409 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1410 FD_WINE_CONNECTED|FD_READ|FD_WRITE,
1411 FD_CONNECT|FD_WINE_LISTENING);
1415 /***********************************************************************
1416 * WSAConnect (WS2_32.30)
1418 int WINAPI WSAConnect ( SOCKET s, const struct WS_sockaddr* name, int namelen,
1419 LPWSABUF lpCallerData, LPWSABUF lpCalleeData,
1420 LPQOS lpSQOS, LPQOS lpGQOS )
1422 if ( lpCallerData || lpCalleeData || lpSQOS || lpGQOS )
1423 FIXME ("unsupported parameters!\n");
1424 return WS_connect ( s, name, namelen );
1428 /***********************************************************************
1429 * getpeername (WS2_32.5)
1431 int WINAPI WS_getpeername(SOCKET s, struct WS_sockaddr *name, int *namelen)
1436 TRACE("socket: %04x, ptr %p, len %8x\n", s, name, *namelen);
1438 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1439 if( (name == NULL) || (namelen == NULL) )
1441 SetLastError( WSAEFAULT );
1442 return SOCKET_ERROR;
1445 fd = get_sock_fd( s, 0, NULL );
1450 struct sockaddr* uaddr;
1453 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1454 if (getpeername(fd, uaddr, &uaddrlen) != 0)
1456 SetLastError(wsaErrno());
1458 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1460 /* The buffer was too small */
1461 SetLastError(WSAEFAULT);
1467 ws_sockaddr_free(uaddr,name);
1468 release_sock_fd( s, fd );
1473 /***********************************************************************
1474 * getsockname (WS2_32.6)
1476 int WINAPI WS_getsockname(SOCKET s, struct WS_sockaddr *name, int *namelen)
1481 TRACE("socket: %04x, ptr %p, len %8x\n", s, name, *namelen);
1483 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1484 if( (name == NULL) || (namelen == NULL) )
1486 SetLastError( WSAEFAULT );
1487 return SOCKET_ERROR;
1490 fd = get_sock_fd( s, 0, NULL );
1495 struct sockaddr* uaddr;
1498 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1499 if (getsockname(fd, uaddr, &uaddrlen) != 0)
1501 SetLastError(wsaErrno());
1503 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1505 /* The buffer was too small */
1506 SetLastError(WSAEFAULT);
1512 release_sock_fd( s, fd );
1517 /***********************************************************************
1518 * getsockopt (WS2_32.7)
1520 INT WINAPI WS_getsockopt(SOCKET s, INT level,
1521 INT optname, char *optval, INT *optlen)
1525 TRACE("socket: %04x, level 0x%x, name 0x%x, ptr %8x, len %d\n", s, level,
1526 (int) optname, (int) optval, (int) *optlen);
1527 /* SO_OPENTYPE does not require a valid socket handle. */
1528 if (level == WS_SOL_SOCKET && optname == WS_SO_OPENTYPE)
1530 if (!optlen || *optlen < sizeof(int) || !optval)
1532 SetLastError(WSAEFAULT);
1533 return SOCKET_ERROR;
1535 *(int *)optval = (int)TlsGetValue( opentype_tls_index );
1536 *optlen = sizeof(int);
1537 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
1541 fd = get_sock_fd( s, 0, NULL );
1544 if (!convert_sockopt(&level, &optname)) {
1545 SetLastError(WSAENOPROTOOPT); /* Unknown option */
1547 if (getsockopt(fd, (int) level, optname, optval, optlen) == 0 )
1549 release_sock_fd( s, fd );
1552 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1554 release_sock_fd( s, fd );
1556 return SOCKET_ERROR;
1560 /***********************************************************************
1564 u_long WINAPI WS_htonl(u_long hostlong)
1566 return htonl(hostlong);
1570 /***********************************************************************
1574 u_short WINAPI WS_htons(u_short hostshort)
1576 return htons(hostshort);
1579 /***********************************************************************
1580 * WSAHtonl (WS2_32.46)
1582 int WINAPI WSAHtonl(SOCKET s, u_long hostlong, u_long *lpnetlong)
1585 return INVALID_SOCKET;
1588 /***********************************************************************
1589 * WSAHtons (WS2_32.47)
1591 int WINAPI WSAHtons(SOCKET s, u_short hostshort, u_short *lpnetshort)
1594 return INVALID_SOCKET;
1598 /***********************************************************************
1599 * inet_addr (WINSOCK.10)
1600 * inet_addr (WS2_32.11)
1602 u_long WINAPI WS_inet_addr(const char *cp)
1604 return inet_addr(cp);
1608 /***********************************************************************
1609 * ntohl (WINSOCK.14)
1612 u_long WINAPI WS_ntohl(u_long netlong)
1614 return ntohl(netlong);
1618 /***********************************************************************
1619 * ntohs (WINSOCK.15)
1622 u_short WINAPI WS_ntohs(u_short netshort)
1624 return ntohs(netshort);
1628 /***********************************************************************
1629 * inet_ntoa (WS2_32.12)
1631 char* WINAPI WS_inet_ntoa(struct WS_in_addr in)
1633 /* use "buffer for dummies" here because some applications have
1634 * propensity to decode addresses in ws_hostent structure without
1635 * saving them first...
1637 static char dbuffer[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
1639 char* s = inet_ntoa(*((struct in_addr*)&in));
1645 SetLastError(wsaErrno());
1649 /**********************************************************************
1650 * WSAIoctl (WS2_32.50)
1653 * FIXME: Only SIO_GET_INTERFACE_LIST option implemented.
1655 INT WINAPI WSAIoctl (SOCKET s,
1656 DWORD dwIoControlCode,
1659 LPVOID lpbOutBuffer,
1661 LPDWORD lpcbBytesReturned,
1662 LPWSAOVERLAPPED lpOverlapped,
1663 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
1665 int fd = get_sock_fd( s, 0, NULL );
1667 if (fd == -1) return SOCKET_ERROR;
1669 switch( dwIoControlCode )
1671 case SIO_GET_INTERFACE_LIST:
1673 INTERFACE_INFO* intArray = (INTERFACE_INFO*)lpbOutBuffer;
1674 DWORD size, numInt, apiReturn;
1676 TRACE ("-> SIO_GET_INTERFACE_LIST request\n");
1680 release_sock_fd( s, fd );
1681 WSASetLastError(WSAEFAULT);
1682 return SOCKET_ERROR;
1684 if (!lpcbBytesReturned)
1686 release_sock_fd( s, fd );
1687 WSASetLastError(WSAEFAULT);
1688 return SOCKET_ERROR;
1691 apiReturn = GetAdaptersInfo(NULL, &size);
1692 if (apiReturn == ERROR_NO_DATA)
1696 else if (apiReturn == ERROR_BUFFER_OVERFLOW)
1698 PIP_ADAPTER_INFO table = (PIP_ADAPTER_INFO)HeapAlloc(GetProcessHeap(),0,size);
1702 if (GetAdaptersInfo(table, &size) == NO_ERROR)
1704 PIP_ADAPTER_INFO ptr;
1706 if (size > cbOutBuffer)
1708 HeapFree(GetProcessHeap(),0,table);
1709 release_sock_fd( s, fd );
1710 WSASetLastError(WSAEFAULT);
1711 return (SOCKET_ERROR);
1713 for (ptr = table, numInt = 0; ptr;
1714 ptr = ptr->Next, intArray++, numInt++)
1716 unsigned int addr, mask, bcast;
1717 struct ifreq ifInfo;
1719 /* Socket Status Flags */
1720 strncpy(ifInfo.ifr_name, ptr->AdapterName, IFNAMSIZ);
1721 ifInfo.ifr_name[IFNAMSIZ-1] = '\0';
1722 if (ioctl(fd, SIOCGIFFLAGS, &ifInfo) < 0)
1724 ERR ("Error obtaining status flags for socket!\n");
1725 HeapFree(GetProcessHeap(),0,table);
1726 release_sock_fd( s, fd );
1727 WSASetLastError(WSAEINVAL);
1728 return (SOCKET_ERROR);
1732 /* set flags; the values of IFF_* are not the same
1733 under Linux and Windows, therefore must generate
1735 intArray->iiFlags = 0;
1736 if (ifInfo.ifr_flags & IFF_BROADCAST)
1737 intArray->iiFlags |= WS_IFF_BROADCAST;
1738 #ifdef IFF_POINTOPOINT
1739 if (ifInfo.ifr_flags & IFF_POINTOPOINT)
1740 intArray->iiFlags |= WS_IFF_POINTTOPOINT;
1742 if (ifInfo.ifr_flags & IFF_LOOPBACK)
1743 intArray->iiFlags |= WS_IFF_LOOPBACK;
1744 if (ifInfo.ifr_flags & IFF_UP)
1745 intArray->iiFlags |= WS_IFF_UP;
1748 addr = inet_addr(ptr->IpAddressList.IpAddress.String);
1749 mask = inet_addr(ptr->IpAddressList.IpMask.String);
1750 bcast = addr | (addr & !mask);
1751 intArray->iiAddress.AddressIn.sin_family = AF_INET;
1752 intArray->iiAddress.AddressIn.sin_port = 0;
1753 intArray->iiAddress.AddressIn.sin_addr.WS_s_addr =
1755 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
1756 intArray->iiNetmask.AddressIn.sin_port = 0;
1757 intArray->iiNetmask.AddressIn.sin_addr.WS_s_addr =
1759 intArray->iiBroadcastAddress.AddressIn.sin_family =
1761 intArray->iiBroadcastAddress.AddressIn.sin_port = 0;
1762 intArray->iiBroadcastAddress.AddressIn.sin_addr.
1768 ERR ("Unable to get interface table!\n");
1769 release_sock_fd( s, fd );
1770 HeapFree(GetProcessHeap(),0,table);
1771 WSASetLastError(WSAEINVAL);
1772 return (SOCKET_ERROR);
1774 HeapFree(GetProcessHeap(),0,table);
1778 release_sock_fd( s, fd );
1779 WSASetLastError(WSAEINVAL);
1780 return (SOCKET_ERROR);
1785 ERR ("Unable to get interface table!\n");
1786 release_sock_fd( s, fd );
1787 WSASetLastError(WSAEINVAL);
1788 return (SOCKET_ERROR);
1790 /* Calculate the size of the array being returned */
1791 *lpcbBytesReturned = sizeof(INTERFACE_INFO) * numInt;
1796 WARN("\tunsupported WS_IOCTL cmd (%08lx)\n", dwIoControlCode);
1797 release_sock_fd( s, fd );
1798 WSASetLastError(WSAEOPNOTSUPP);
1799 return (SOCKET_ERROR);
1802 /* Function executed with no errors */
1803 release_sock_fd( s, fd );
1808 /***********************************************************************
1809 * ioctlsocket (WS2_32.10)
1811 int WINAPI WS_ioctlsocket(SOCKET s, long cmd, u_long *argp)
1816 TRACE("socket %04x, cmd %08lx, ptr %p\n", s, cmd, argp);
1826 if( _get_sock_mask(s) )
1828 /* AsyncSelect()'ed sockets are always nonblocking */
1829 if (*argp) return 0;
1830 SetLastError(WSAEINVAL);
1831 return SOCKET_ERROR;
1834 _enable_event(SOCKET2HANDLE(s), 0, FD_WINE_NONBLOCKING, 0);
1836 _enable_event(SOCKET2HANDLE(s), 0, 0, FD_WINE_NONBLOCKING);
1843 case WS__IOW('f',125,u_long):
1844 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
1845 SetLastError(WSAEINVAL);
1846 return SOCKET_ERROR;
1848 case SIOCGIFBRDADDR:
1849 case SIOCGIFNETMASK:
1851 /* These don't need any special handling. They are used by
1852 WsControl, and are here to suppress an unecessary warning. */
1856 /* Netscape tries hard to use bogus ioctl 0x667e */
1857 WARN("\tunknown WS_IOCTL cmd (%08lx)\n", cmd);
1861 fd = get_sock_fd( s, 0, NULL );
1864 if( ioctl(fd, newcmd, (char*)argp ) == 0 )
1866 release_sock_fd( s, fd );
1869 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1870 release_sock_fd( s, fd );
1872 return SOCKET_ERROR;
1875 /***********************************************************************
1876 * listen (WS2_32.13)
1878 int WINAPI WS_listen(SOCKET s, int backlog)
1880 int fd = get_sock_fd( s, GENERIC_READ, NULL );
1882 TRACE("socket %04x, backlog %d\n", s, backlog);
1885 if (listen(fd, backlog) == 0)
1887 release_sock_fd( s, fd );
1888 _enable_event(SOCKET2HANDLE(s), FD_ACCEPT,
1890 FD_CONNECT|FD_WINE_CONNECTED);
1893 SetLastError(wsaErrno());
1894 release_sock_fd( s, fd );
1896 return SOCKET_ERROR;
1899 /***********************************************************************
1902 int WINAPI WS_recv(SOCKET s, char *buf, int len, int flags)
1904 DWORD n, dwFlags = flags;
1910 if ( WSARecvFrom (s, &wsabuf, 1, &n, &dwFlags, NULL, NULL, NULL, NULL) == SOCKET_ERROR )
1911 return SOCKET_ERROR;
1916 /***********************************************************************
1917 * recvfrom (WS2_32.17)
1919 int WINAPI WS_recvfrom(SOCKET s, char *buf, INT len, int flags,
1920 struct WS_sockaddr *from, int *fromlen)
1922 DWORD n, dwFlags = flags;
1928 if ( WSARecvFrom (s, &wsabuf, 1, &n, &dwFlags, from, fromlen, NULL, NULL) == SOCKET_ERROR )
1929 return SOCKET_ERROR;
1934 /***********************************************************************
1935 * select (WS2_32.18)
1937 int WINAPI WS_select(int nfds, WS_fd_set *ws_readfds,
1938 WS_fd_set *ws_writefds, WS_fd_set *ws_exceptfds,
1939 const struct WS_timeval* ws_timeout)
1942 fd_set readfds, writefds, exceptfds;
1943 fd_set *p_read, *p_write, *p_except;
1944 int readfd[FD_SETSIZE], writefd[FD_SETSIZE], exceptfd[FD_SETSIZE];
1945 struct timeval timeout, *timeoutaddr = NULL;
1947 TRACE("read %p, write %p, excp %p timeout %p\n",
1948 ws_readfds, ws_writefds, ws_exceptfds, ws_timeout);
1950 p_read = fd_set_import(&readfds, ws_readfds, GENERIC_READ, &highfd, readfd);
1951 p_write = fd_set_import(&writefds, ws_writefds, GENERIC_WRITE, &highfd, writefd);
1952 p_except = fd_set_import(&exceptfds, ws_exceptfds, 0, &highfd, exceptfd);
1955 timeoutaddr = &timeout;
1956 timeout.tv_sec=ws_timeout->tv_sec;
1957 timeout.tv_usec=ws_timeout->tv_usec;
1960 if( (highfd = select(highfd + 1, p_read, p_write, p_except, timeoutaddr)) > 0 )
1962 fd_set_export(&readfds, p_except, ws_readfds, readfd);
1963 fd_set_export(&writefds, p_except, ws_writefds, writefd);
1965 if (p_except && ws_exceptfds)
1969 for (i = j = 0; i < ws_exceptfds->fd_count; i++)
1971 int fd = exceptfd[i];
1972 SOCKET s = ws_exceptfds->fd_array[i];
1973 if (fd == -1) continue;
1974 if (FD_ISSET(fd, &exceptfds)) ws_exceptfds->fd_array[j++] = s;
1975 release_sock_fd( s, fd );
1977 ws_exceptfds->fd_count = j;
1981 fd_set_unimport(ws_readfds, readfd);
1982 fd_set_unimport(ws_writefds, writefd);
1983 fd_set_unimport(ws_exceptfds, exceptfd);
1985 if( highfd == 0 ) return 0;
1986 SetLastError(wsaErrno());
1987 return SOCKET_ERROR;
1991 /***********************************************************************
1994 int WINAPI WS_send(SOCKET s, const char *buf, int len, int flags)
2000 wsabuf.buf = (char*) buf;
2002 if ( WSASendTo ( s, &wsabuf, 1, &n, flags, NULL, 0, NULL, NULL) == SOCKET_ERROR )
2003 return SOCKET_ERROR;
2008 /***********************************************************************
2009 * WSASend (WS2_32.72)
2011 INT WINAPI WSASend( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2012 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2013 LPWSAOVERLAPPED lpOverlapped,
2014 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2016 return WSASendTo ( s, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags,
2017 NULL, 0, lpOverlapped, lpCompletionRoutine );
2020 /***********************************************************************
2021 * WSASendDisconnect (WS2_32.73)
2023 INT WINAPI WSASendDisconnect( SOCKET s, LPWSABUF lpBuffers )
2025 return WS_shutdown ( s, SD_SEND );
2029 /***********************************************************************
2030 * WSASendTo (WS2_32.74)
2032 INT WINAPI WSASendTo( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2033 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2034 const struct WS_sockaddr *to, int tolen,
2035 LPWSAOVERLAPPED lpOverlapped,
2036 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2038 int i, n, fd, err = WSAENOTSOCK, flags, ret;
2039 struct iovec* iovec;
2040 struct ws2_async *wsa;
2042 TRACE ("socket %04x, wsabuf %p, nbufs %ld, flags %ld, to %p, tolen %d, ovl %p, func %p\n",
2043 s, lpBuffers, dwBufferCount, dwFlags,
2044 to, tolen, lpOverlapped, lpCompletionRoutine);
2046 fd = get_sock_fd( s, GENERIC_WRITE, &flags );
2047 TRACE ( "fd=%d, flags=%x\n", fd, flags );
2049 if ( fd == -1 ) return SOCKET_ERROR;
2051 if (flags & FD_FLAG_SEND_SHUTDOWN)
2053 WSASetLastError ( WSAESHUTDOWN );
2057 if ( !lpNumberOfBytesSent )
2063 iovec = HeapAlloc (GetProcessHeap(), 0, dwBufferCount * sizeof (struct iovec) );
2071 for ( i = 0; i < dwBufferCount; i++ )
2073 iovec[i].iov_base = lpBuffers[i].buf;
2074 iovec[i].iov_len = lpBuffers[i].len;
2077 if ( (lpOverlapped || lpCompletionRoutine) && flags & FD_FLAG_OVERLAPPED )
2079 wsa = WS2_make_async ( s, fd, ASYNC_TYPE_WRITE, iovec, dwBufferCount,
2080 &dwFlags, (struct WS_sockaddr*) to, &tolen,
2081 lpOverlapped, lpCompletionRoutine );
2088 if ( ( ret = register_new_async ( &wsa->async )) )
2090 err = NtStatusToWSAError ( ret );
2092 if ( !lpOverlapped )
2093 HeapFree ( GetProcessHeap(), 0, wsa->async.iosb );
2094 HeapFree ( GetProcessHeap(), 0, wsa );
2098 /* Try immediate completion */
2099 if ( lpOverlapped && !NtResetEvent( lpOverlapped->hEvent, NULL ) )
2101 if ( WSAGetOverlappedResult ( s, lpOverlapped,
2102 lpNumberOfBytesSent, FALSE, &dwFlags) )
2105 if ( (err = WSAGetLastError ()) != WSA_IO_INCOMPLETE )
2109 WSASetLastError ( WSA_IO_PENDING );
2110 return SOCKET_ERROR;
2113 if (_is_blocking(s))
2115 /* FIXME: exceptfds? */
2116 do_block(fd, POLLOUT);
2119 n = WS2_send ( fd, iovec, dwBufferCount, to, tolen, dwFlags );
2123 if ( err == WSAEWOULDBLOCK )
2124 _enable_event (SOCKET2HANDLE(s), FD_WRITE, 0, 0);
2128 TRACE(" -> %i bytes\n", n);
2129 *lpNumberOfBytesSent = n;
2131 HeapFree ( GetProcessHeap(), 0, iovec );
2132 release_sock_fd( s, fd );
2136 HeapFree ( GetProcessHeap(), 0, iovec );
2139 release_sock_fd( s, fd );
2142 WARN (" -> ERROR %d\n", err);
2143 WSASetLastError (err);
2144 return SOCKET_ERROR;
2147 /***********************************************************************
2148 * sendto (WS2_32.20)
2150 int WINAPI WS_sendto(SOCKET s, const char *buf, int len, int flags,
2151 const struct WS_sockaddr *to, int tolen)
2157 wsabuf.buf = (char*) buf;
2159 if ( WSASendTo (s, &wsabuf, 1, &n, flags, to, tolen, NULL, NULL) == SOCKET_ERROR )
2160 return SOCKET_ERROR;
2165 /***********************************************************************
2166 * setsockopt (WS2_32.21)
2168 int WINAPI WS_setsockopt(SOCKET s, int level, int optname,
2169 const char *optval, int optlen)
2173 struct linger linger;
2174 struct timeval tval;
2176 TRACE("socket: %04x, level %d, name %d, ptr %p, len %d\n",
2177 s, level, optname, optval, optlen);
2179 /* SO_OPENTYPE does not require a valid socket handle. */
2180 if (level == WS_SOL_SOCKET && optname == WS_SO_OPENTYPE)
2182 if (optlen < sizeof(int) || !optval)
2184 SetLastError(WSAEFAULT);
2185 return SOCKET_ERROR;
2187 TlsSetValue( opentype_tls_index, (LPVOID)*(int *)optval );
2188 TRACE("setting global SO_OPENTYPE to 0x%x\n", *(int *)optval );
2192 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
2193 * socket. This will either not happen under windows or it is ignored in
2194 * windows (but it works in linux and therefor prevents the game to find
2195 * games outsite the current network) */
2196 if ( level==WS_SOL_SOCKET && optname==WS_SO_DONTROUTE )
2198 FIXME("Does windows ignore SO_DONTROUTE?\n");
2202 /* Is a privileged and useless operation, so we don't. */
2203 if ((optname == WS_SO_DEBUG) && (level == WS_SOL_SOCKET))
2205 FIXME("(%d,SOL_SOCKET,SO_DEBUG,%p(%ld)) attempted (is privileged). Ignoring.\n",s,optval,*(DWORD*)optval);
2209 if(optname == WS_SO_DONTLINGER && level == WS_SOL_SOCKET) {
2210 /* This is unique to WinSock and takes special conversion */
2211 linger.l_onoff = *((int*)optval) ? 0: 1;
2212 linger.l_linger = 0;
2214 optval = (char*)&linger;
2215 optlen = sizeof(struct linger);
2220 if (!convert_sockopt(&level, &optname)) {
2221 ERR("Invalid level (%d) or optname (%d)\n", level, optname);
2222 SetLastError(WSAENOPROTOOPT);
2223 return SOCKET_ERROR;
2225 if (optname == SO_LINGER && optval) {
2226 /* yes, uses unsigned short in both win16/win32 */
2227 linger.l_onoff = ((UINT16*)optval)[0];
2228 linger.l_linger = ((UINT16*)optval)[1];
2229 /* FIXME: what is documented behavior if SO_LINGER optval
2231 optval = (char*)&linger;
2232 optlen = sizeof(struct linger);
2234 else if (optval && optlen < sizeof(int))
2236 woptval= *((INT16 *) optval);
2237 optval= (char*) &woptval;
2240 if (level == SOL_SOCKET && is_timeout_option(optname))
2242 if (optlen == sizeof(UINT32)) {
2243 /* WinSock passes miliseconds instead of struct timeval */
2244 tval.tv_usec = *(PUINT32)optval % 1000;
2245 tval.tv_sec = *(PUINT32)optval / 1000;
2246 /* min of 500 milisec */
2247 if (tval.tv_sec == 0 && tval.tv_usec < 500) tval.tv_usec = 500;
2248 optlen = sizeof(struct timeval);
2249 optval = (char*)&tval;
2250 } else if (optlen == sizeof(struct timeval)) {
2251 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen);
2253 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen);
2257 if (level == SOL_SOCKET && optname == SO_RCVBUF && *(int*)optval < 2048)
2259 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(int*)optval );
2265 fd = get_sock_fd( s, 0, NULL );
2266 if (fd == -1) return SOCKET_ERROR;
2268 if (setsockopt(fd, level, optname, optval, optlen) == 0)
2270 release_sock_fd( s, fd );
2273 TRACE("Setting socket error, %d\n", wsaErrno());
2274 SetLastError(wsaErrno());
2275 release_sock_fd( s, fd );
2276 return SOCKET_ERROR;
2279 /***********************************************************************
2280 * shutdown (WS2_32.22)
2282 int WINAPI WS_shutdown(SOCKET s, int how)
2284 int fd, fd0 = -1, fd1 = -1, flags, err = WSAENOTSOCK;
2285 unsigned int clear_flags = 0;
2287 fd = get_sock_fd( s, 0, &flags );
2288 TRACE("socket %04x, how %i %x\n", s, how, flags );
2291 return SOCKET_ERROR;
2295 case 0: /* drop receives */
2296 clear_flags |= FD_READ;
2298 case 1: /* drop sends */
2299 clear_flags |= FD_WRITE;
2301 case 2: /* drop all */
2302 clear_flags |= FD_READ|FD_WRITE;
2304 clear_flags |= FD_WINE_LISTENING;
2307 if ( flags & FD_FLAG_OVERLAPPED ) {
2320 fd1 = get_sock_fd ( s, 0, NULL );
2326 err = WS2_register_async_shutdown ( s, fd0, ASYNC_TYPE_READ );
2329 release_sock_fd( s, fd0 );
2335 err = WS2_register_async_shutdown ( s, fd1, ASYNC_TYPE_WRITE );
2338 release_sock_fd( s, fd1 );
2343 else /* non-overlapped mode */
2345 if ( shutdown( fd, how ) )
2348 release_sock_fd( s, fd );
2351 release_sock_fd( s, fd );
2354 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
2355 if ( how > 1) WSAAsyncSelect( s, 0, 0, 0 );
2359 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
2360 WSASetLastError ( err );
2361 return SOCKET_ERROR;
2364 /***********************************************************************
2365 * socket (WS2_32.23)
2367 SOCKET WINAPI WS_socket(int af, int type, int protocol)
2369 TRACE("af=%d type=%d protocol=%d\n", af, type, protocol);
2371 return WSASocketA ( af, type, protocol, NULL, 0,
2372 (TlsGetValue(opentype_tls_index) ? 0 : WSA_FLAG_OVERLAPPED) );
2376 /***********************************************************************
2377 * gethostbyaddr (WS2_32.51)
2379 struct WS_hostent* WINAPI WS_gethostbyaddr(const char *addr, int len, int type)
2381 struct WS_hostent *retval = NULL;
2382 struct hostent* host;
2384 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2387 struct hostent hostentry;
2390 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2392 int res = gethostbyaddr_r(addr, len, type,
2393 &hostentry, extrabuf, ebufsize, &host, &locerr);
2394 if( res != ERANGE) break;
2396 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2398 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2400 EnterCriticalSection( &csWSgetXXXbyYYY );
2401 host = gethostbyaddr(addr, len, type);
2402 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2404 if( host != NULL ) retval = WS_dup_he(host);
2405 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2406 HeapFree(GetProcessHeap(),0,extrabuf);
2408 LeaveCriticalSection( &csWSgetXXXbyYYY );
2410 TRACE("ptr %p, len %d, type %d ret %p\n", addr, len, type, retval);
2414 /***********************************************************************
2415 * gethostbyname (WS2_32.52)
2417 struct WS_hostent* WINAPI WS_gethostbyname(const char* name)
2419 struct WS_hostent *retval = NULL;
2420 struct hostent* host;
2421 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2424 struct hostent hostentry;
2425 int locerr = ENOBUFS;
2430 if( gethostname( buf, 100) == -1) {
2431 SetLastError( WSAENOBUFS); /* appropriate ? */
2435 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2437 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2439 int res = gethostbyname_r(name, &hostentry, extrabuf, ebufsize, &host, &locerr);
2440 if( res != ERANGE) break;
2442 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2444 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2446 EnterCriticalSection( &csWSgetXXXbyYYY );
2447 host = gethostbyname(name);
2448 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2450 if (host) retval = WS_dup_he(host);
2451 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2452 HeapFree(GetProcessHeap(),0,extrabuf);
2454 LeaveCriticalSection( &csWSgetXXXbyYYY );
2456 TRACE( "%s ret %p\n", debugstr_a(name), retval );
2461 /***********************************************************************
2462 * getprotobyname (WS2_32.53)
2464 struct WS_protoent* WINAPI WS_getprotobyname(const char* name)
2466 struct WS_protoent* retval = NULL;
2467 #ifdef HAVE_GETPROTOBYNAME
2468 struct protoent* proto;
2469 EnterCriticalSection( &csWSgetXXXbyYYY );
2470 if( (proto = getprotobyname(name)) != NULL )
2472 retval = WS_dup_pe(proto);
2475 MESSAGE("protocol %s not found; You might want to add "
2476 "this to /etc/protocols\n", debugstr_a(name) );
2477 SetLastError(WSANO_DATA);
2479 LeaveCriticalSection( &csWSgetXXXbyYYY );
2481 TRACE( "%s ret %p\n", debugstr_a(name), retval );
2486 /***********************************************************************
2487 * getprotobynumber (WS2_32.54)
2489 struct WS_protoent* WINAPI WS_getprotobynumber(int number)
2491 struct WS_protoent* retval = NULL;
2492 #ifdef HAVE_GETPROTOBYNUMBER
2493 struct protoent* proto;
2494 EnterCriticalSection( &csWSgetXXXbyYYY );
2495 if( (proto = getprotobynumber(number)) != NULL )
2497 retval = WS_dup_pe(proto);
2500 MESSAGE("protocol number %d not found; You might want to add "
2501 "this to /etc/protocols\n", number );
2502 SetLastError(WSANO_DATA);
2504 LeaveCriticalSection( &csWSgetXXXbyYYY );
2506 TRACE("%i ret %p\n", number, retval);
2511 /***********************************************************************
2512 * getservbyname (WS2_32.55)
2514 struct WS_servent* WINAPI WS_getservbyname(const char *name, const char *proto)
2516 struct WS_servent* retval = NULL;
2517 struct servent* serv;
2519 char *proto_str = NULL;
2521 if (!(name_str = strdup_lower(name))) return NULL;
2523 if (proto && *proto)
2525 if (!(proto_str = strdup_lower(proto)))
2527 HeapFree( GetProcessHeap(), 0, name_str );
2532 EnterCriticalSection( &csWSgetXXXbyYYY );
2533 serv = getservbyname(name_str, proto_str);
2536 retval = WS_dup_se(serv);
2538 else SetLastError(WSANO_DATA);
2539 LeaveCriticalSection( &csWSgetXXXbyYYY );
2540 if (proto_str) HeapFree( GetProcessHeap(), 0, proto_str );
2541 HeapFree( GetProcessHeap(), 0, name_str );
2542 TRACE( "%s, %s ret %p\n", debugstr_a(name), debugstr_a(proto), retval );
2547 /***********************************************************************
2548 * getservbyport (WS2_32.56)
2550 struct WS_servent* WINAPI WS_getservbyport(int port, const char *proto)
2552 struct WS_servent* retval = NULL;
2553 #ifdef HAVE_GETSERVBYPORT
2554 struct servent* serv;
2555 char *proto_str = NULL;
2557 if (proto && *proto)
2559 if (!(proto_str = strdup_lower(proto))) return NULL;
2561 EnterCriticalSection( &csWSgetXXXbyYYY );
2562 if( (serv = getservbyport(port, proto_str)) != NULL ) {
2563 retval = WS_dup_se(serv);
2565 else SetLastError(WSANO_DATA);
2566 LeaveCriticalSection( &csWSgetXXXbyYYY );
2567 if (proto_str) HeapFree( GetProcessHeap(), 0, proto_str );
2569 TRACE("%d (i.e. port %d), %s ret %p\n", port, (int)ntohl(port), debugstr_a(proto), retval);
2574 /***********************************************************************
2575 * gethostname (WS2_32.57)
2577 int WINAPI WS_gethostname(char *name, int namelen)
2579 TRACE("name %p, len %d\n", name, namelen);
2581 if (gethostname(name, namelen) == 0)
2583 TRACE("<- '%s'\n", name);
2586 SetLastError((errno == EINVAL) ? WSAEFAULT : wsaErrno());
2587 TRACE("<- ERROR !\n");
2588 return SOCKET_ERROR;
2592 /* ------------------------------------- Windows sockets extensions -- *
2594 * ------------------------------------------------------------------- */
2596 /***********************************************************************
2597 * WSAEnumNetworkEvents (WS2_32.36)
2599 int WINAPI WSAEnumNetworkEvents(SOCKET s, WSAEVENT hEvent, LPWSANETWORKEVENTS lpEvent)
2603 TRACE("%08x, hEvent %p, lpEvent %08x\n", s, hEvent, (unsigned)lpEvent );
2605 SERVER_START_REQ( get_socket_event )
2607 req->handle = SOCKET2HANDLE(s);
2608 req->service = TRUE;
2609 req->c_event = hEvent;
2610 wine_server_set_reply( req, lpEvent->iErrorCode, sizeof(lpEvent->iErrorCode) );
2611 if (!(ret = wine_server_call(req))) lpEvent->lNetworkEvents = reply->pmask & reply->mask;
2615 SetLastError(WSAEINVAL);
2616 return SOCKET_ERROR;
2619 /***********************************************************************
2620 * WSAEventSelect (WS2_32.39)
2622 int WINAPI WSAEventSelect(SOCKET s, WSAEVENT hEvent, LONG lEvent)
2626 TRACE("%08x, hEvent %p, event %08x\n", s, hEvent, (unsigned)lEvent );
2628 SERVER_START_REQ( set_socket_event )
2630 req->handle = SOCKET2HANDLE(s);
2632 req->event = hEvent;
2635 ret = wine_server_call( req );
2639 SetLastError(WSAEINVAL);
2640 return SOCKET_ERROR;
2643 /**********************************************************************
2644 * WSAGetOverlappedResult (WS2_32.40)
2646 BOOL WINAPI WSAGetOverlappedResult ( SOCKET s, LPWSAOVERLAPPED lpOverlapped,
2647 LPDWORD lpcbTransfer, BOOL fWait,
2652 TRACE ( "socket %d ovl %p trans %p, wait %d flags %p\n",
2653 s, lpOverlapped, lpcbTransfer, fWait, lpdwFlags );
2655 if ( !(lpOverlapped && lpOverlapped->hEvent) )
2657 ERR ( "Invalid pointer\n" );
2658 WSASetLastError (WSA_INVALID_PARAMETER);
2664 while ( WaitForSingleObjectEx (lpOverlapped->hEvent, INFINITE, TRUE) == STATUS_USER_APC );
2666 else if ( lpOverlapped->Internal == STATUS_PENDING )
2668 /* Wait in order to give APCs a chance to run. */
2669 /* This is cheating, so we must set the event again in case of success -
2670 it may be a non-manual reset event. */
2671 while ( (r = WaitForSingleObjectEx (lpOverlapped->hEvent, 0, TRUE)) == STATUS_USER_APC );
2672 if ( r == WAIT_OBJECT_0 )
2673 NtSetEvent ( lpOverlapped->hEvent, NULL );
2677 *lpcbTransfer = lpOverlapped->InternalHigh;
2680 *lpdwFlags = lpOverlapped->Offset;
2682 switch ( lpOverlapped->Internal )
2684 case STATUS_SUCCESS:
2686 case STATUS_PENDING:
2687 WSASetLastError ( WSA_IO_INCOMPLETE );
2688 if (fWait) ERR ("PENDING status after waiting!\n");
2691 WSASetLastError ( NtStatusToWSAError ( lpOverlapped->Internal ));
2697 /***********************************************************************
2698 * WSAAsyncSelect (WS2_32.101)
2700 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, LONG lEvent)
2704 TRACE("%x, hWnd %p, uMsg %08x, event %08lx\n", s, hWnd, uMsg, lEvent );
2706 SERVER_START_REQ( set_socket_event )
2708 req->handle = SOCKET2HANDLE(s);
2713 ret = wine_server_call( req );
2717 SetLastError(WSAEINVAL);
2718 return SOCKET_ERROR;
2721 /***********************************************************************
2722 * WSACreateEvent (WS2_32.31)
2725 WSAEVENT WINAPI WSACreateEvent(void)
2727 /* Create a manual-reset event, with initial state: unsignealed */
2730 return CreateEventA(NULL, TRUE, FALSE, NULL);
2733 /***********************************************************************
2734 * WSACloseEvent (WS2_32.29)
2737 BOOL WINAPI WSACloseEvent(WSAEVENT event)
2739 TRACE ("event=%p\n", event);
2741 return CloseHandle(event);
2744 /***********************************************************************
2745 * WSASocketA (WS2_32.78)
2748 SOCKET WINAPI WSASocketA(int af, int type, int protocol,
2749 LPWSAPROTOCOL_INFOA lpProtocolInfo,
2750 GROUP g, DWORD dwFlags)
2755 FIXME: The "advanced" parameters of WSASocketA (lpProtocolInfo,
2756 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
2759 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%lx\n",
2760 af, type, protocol, lpProtocolInfo, g, dwFlags );
2762 /* hack for WSADuplicateSocket */
2763 if (lpProtocolInfo && lpProtocolInfo->dwServiceFlags4 == 0xff00ff00) {
2764 ret = lpProtocolInfo->dwCatalogEntryId;
2765 TRACE("\tgot duplicate %04x\n", ret);
2769 /* check the socket family */
2773 case WS_AF_IPX: af = AF_IPX;
2779 SetLastError(WSAEAFNOSUPPORT);
2780 return INVALID_SOCKET;
2783 /* check the socket type */
2786 case WS_SOCK_STREAM:
2796 SetLastError(WSAESOCKTNOSUPPORT);
2797 return INVALID_SOCKET;
2800 /* check the protocol type */
2801 if ( protocol < 0 ) /* don't support negative values */
2803 SetLastError(WSAEPROTONOSUPPORT);
2804 return INVALID_SOCKET;
2807 if ( af == AF_UNSPEC) /* did they not specify the address family? */
2811 if (type == SOCK_STREAM) { af = AF_INET; break; }
2813 if (type == SOCK_DGRAM) { af = AF_INET; break; }
2814 default: SetLastError(WSAEPROTOTYPE); return INVALID_SOCKET;
2817 SERVER_START_REQ( create_socket )
2821 req->protocol = protocol;
2822 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
2823 req->flags = dwFlags;
2824 req->inherit = TRUE;
2825 set_error( wine_server_call( req ) );
2826 ret = HANDLE2SOCKET( reply->handle );
2831 TRACE("\tcreated %04x\n", ret );
2835 if (GetLastError() == WSAEACCES) /* raw socket denied */
2837 if (type == SOCK_RAW)
2838 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, will fail unless running as root\n");
2840 MESSAGE("WS_SOCKET: not enough privileges to create socket, try running as root\n");
2841 SetLastError(WSAESOCKTNOSUPPORT);
2844 WARN("\t\tfailed!\n");
2845 return INVALID_SOCKET;
2848 /***********************************************************************
2849 * WSAJoinLeaf (WS2_32.58)
2852 SOCKET WINAPI WSAJoinLeaf(
2854 const struct WS_sockaddr *addr,
2856 LPWSABUF lpCallerData,
2857 LPWSABUF lpCalleeData,
2863 return INVALID_SOCKET;
2866 /***********************************************************************
2867 * __WSAFDIsSet (WS2_32.151)
2869 int WINAPI __WSAFDIsSet(SOCKET s, WS_fd_set *set)
2871 int i = set->fd_count;
2873 TRACE("(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2876 if (set->fd_array[i] == s) return 1;
2880 /***********************************************************************
2881 * WSAIsBlocking (WINSOCK.114)
2882 * WSAIsBlocking (WS2_32.114)
2884 BOOL WINAPI WSAIsBlocking(void)
2886 /* By default WinSock should set all its sockets to non-blocking mode
2887 * and poll in PeekMessage loop when processing "blocking" ones. This
2888 * function is supposed to tell if the program is in this loop. Our
2889 * blocking calls are truly blocking so we always return FALSE.
2891 * Note: It is allowed to call this function without prior WSAStartup().
2898 /***********************************************************************
2899 * WSACancelBlockingCall (WINSOCK.113)
2900 * WSACancelBlockingCall (WS2_32.113)
2902 INT WINAPI WSACancelBlockingCall(void)
2908 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x )
2910 FIXME("How was this called?\n");
2915 /***********************************************************************
2916 * WSASetBlockingHook (WS2_32.109)
2918 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
2920 FARPROC prev = blocking_hook;
2921 blocking_hook = lpBlockFunc;
2922 TRACE("hook %p\n", lpBlockFunc);
2927 /***********************************************************************
2928 * WSAUnhookBlockingHook (WS2_32.110)
2930 INT WINAPI WSAUnhookBlockingHook(void)
2932 blocking_hook = WSA_DefaultBlockingHook;
2937 /* ----------------------------------- end of API stuff */
2939 /* ----------------------------------- helper functions -
2941 * TODO: Merge WS_dup_..() stuff into one function that
2942 * would operate with a generic structure containing internal
2943 * pointers (via a template of some kind).
2946 static int list_size(char** l, int item_size)
2951 j += (item_size) ? item_size : strlen(l[i]) + 1;
2952 j += (i + 1) * sizeof(char*); }
2956 static int list_dup(char** l_src, char** l_to, int item_size)
2961 for (i = 0; l_src[i]; i++) ;
2962 p = (char *)(l_to + i + 1);
2963 for (i = 0; l_src[i]; i++)
2965 int count = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
2966 memcpy(p, l_src[i], count);
2971 return (p - (char *)l_to);
2976 /* duplicate hostent entry
2977 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
2978 * Dito for protoent and servent.
2980 static struct WS_hostent *WS_dup_he(const struct hostent* p_he)
2983 struct WS_hostent *p_to;
2985 int size = (sizeof(*p_he) +
2986 strlen(p_he->h_name) + 1 +
2987 list_size(p_he->h_aliases, 0) +
2988 list_size(p_he->h_addr_list, p_he->h_length));
2990 if (!(p_to = check_buffer_he(size))) return NULL;
2991 p_to->h_addrtype = p_he->h_addrtype;
2992 p_to->h_length = p_he->h_length;
2994 p = (char *)(p_to + 1);
2996 strcpy(p, p_he->h_name);
2999 p_to->h_aliases = (char **)p;
3000 p += list_dup(p_he->h_aliases, p_to->h_aliases, 0);
3002 p_to->h_addr_list = (char **)p;
3003 list_dup(p_he->h_addr_list, p_to->h_addr_list, p_he->h_length);
3007 /* ----- protoent */
3009 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe)
3012 struct WS_protoent *p_to;
3014 int size = (sizeof(*p_pe) +
3015 strlen(p_pe->p_name) + 1 +
3016 list_size(p_pe->p_aliases, 0));
3018 if (!(p_to = check_buffer_pe(size))) return NULL;
3019 p_to->p_proto = p_pe->p_proto;
3021 p = (char *)(p_to + 1);
3023 strcpy(p, p_pe->p_name);
3026 p_to->p_aliases = (char **)p;
3027 list_dup(p_pe->p_aliases, p_to->p_aliases, 0);
3033 static struct WS_servent *WS_dup_se(const struct servent* p_se)
3036 struct WS_servent *p_to;
3038 int size = (sizeof(*p_se) +
3039 strlen(p_se->s_proto) + 1 +
3040 strlen(p_se->s_name) + 1 +
3041 list_size(p_se->s_aliases, 0));
3043 if (!(p_to = check_buffer_se(size))) return NULL;
3044 p_to->s_port = p_se->s_port;
3046 p = (char *)(p_to + 1);
3048 strcpy(p, p_se->s_name);
3052 strcpy(p, p_se->s_proto);
3055 p_to->s_aliases = (char **)p;
3056 list_dup(p_se->s_aliases, p_to->s_aliases, 0);
3060 /* ----------------------------------- error handling */
3064 int loc_errno = errno;
3065 WARN("errno %d, (%s).\n", loc_errno, strerror(loc_errno));
3069 case EINTR: return WSAEINTR;
3070 case EBADF: return WSAEBADF;
3072 case EACCES: return WSAEACCES;
3073 case EFAULT: return WSAEFAULT;
3074 case EINVAL: return WSAEINVAL;
3075 case EMFILE: return WSAEMFILE;
3076 case EWOULDBLOCK: return WSAEWOULDBLOCK;
3077 case EINPROGRESS: return WSAEINPROGRESS;
3078 case EALREADY: return WSAEALREADY;
3079 case ENOTSOCK: return WSAENOTSOCK;
3080 case EDESTADDRREQ: return WSAEDESTADDRREQ;
3081 case EMSGSIZE: return WSAEMSGSIZE;
3082 case EPROTOTYPE: return WSAEPROTOTYPE;
3083 case ENOPROTOOPT: return WSAENOPROTOOPT;
3084 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
3085 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
3086 case EOPNOTSUPP: return WSAEOPNOTSUPP;
3087 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
3088 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
3089 case EADDRINUSE: return WSAEADDRINUSE;
3090 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
3091 case ENETDOWN: return WSAENETDOWN;
3092 case ENETUNREACH: return WSAENETUNREACH;
3093 case ENETRESET: return WSAENETRESET;
3094 case ECONNABORTED: return WSAECONNABORTED;
3096 case ECONNRESET: return WSAECONNRESET;
3097 case ENOBUFS: return WSAENOBUFS;
3098 case EISCONN: return WSAEISCONN;
3099 case ENOTCONN: return WSAENOTCONN;
3100 case ESHUTDOWN: return WSAESHUTDOWN;
3101 case ETOOMANYREFS: return WSAETOOMANYREFS;
3102 case ETIMEDOUT: return WSAETIMEDOUT;
3103 case ECONNREFUSED: return WSAECONNREFUSED;
3104 case ELOOP: return WSAELOOP;
3105 case ENAMETOOLONG: return WSAENAMETOOLONG;
3106 case EHOSTDOWN: return WSAEHOSTDOWN;
3107 case EHOSTUNREACH: return WSAEHOSTUNREACH;
3108 case ENOTEMPTY: return WSAENOTEMPTY;
3110 case EPROCLIM: return WSAEPROCLIM;
3113 case EUSERS: return WSAEUSERS;
3116 case EDQUOT: return WSAEDQUOT;
3119 case ESTALE: return WSAESTALE;
3122 case EREMOTE: return WSAEREMOTE;
3125 /* just in case we ever get here and there are no problems */
3128 WARN("Unknown errno %d!\n", loc_errno);
3129 return WSAEOPNOTSUPP;
3133 UINT wsaHerrno(int loc_errno)
3136 WARN("h_errno %d.\n", loc_errno);
3140 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
3141 case TRY_AGAIN: return WSATRY_AGAIN;
3142 case NO_RECOVERY: return WSANO_RECOVERY;
3143 case NO_DATA: return WSANO_DATA;
3144 case ENOBUFS: return WSAENOBUFS;
3148 WARN("Unknown h_errno %d!\n", loc_errno);
3149 return WSAEOPNOTSUPP;
3154 /***********************************************************************
3155 * WSARecv (WS2_32.67)
3157 int WINAPI WSARecv (SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
3158 LPDWORD NumberOfBytesReceived, LPDWORD lpFlags,
3159 LPWSAOVERLAPPED lpOverlapped,
3160 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
3162 return WSARecvFrom (s, lpBuffers, dwBufferCount, NumberOfBytesReceived, lpFlags,
3163 NULL, NULL, lpOverlapped, lpCompletionRoutine);
3166 /***********************************************************************
3167 * WSARecvFrom (WS2_32.69)
3169 INT WINAPI WSARecvFrom( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
3170 LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, struct WS_sockaddr *lpFrom,
3171 LPINT lpFromlen, LPWSAOVERLAPPED lpOverlapped,
3172 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
3175 int i, n, fd, err = WSAENOTSOCK, flags, ret;
3176 struct iovec* iovec;
3177 struct ws2_async *wsa;
3179 TRACE("socket %04x, wsabuf %p, nbufs %ld, flags %ld, from %p, fromlen %ld, ovl %p, func %p\n",
3180 s, lpBuffers, dwBufferCount, *lpFlags, lpFrom,
3181 (lpFromlen ? *lpFromlen : -1L),
3182 lpOverlapped, lpCompletionRoutine);
3184 fd = get_sock_fd( s, GENERIC_READ, &flags );
3185 TRACE ( "fd=%d, flags=%x\n", fd, flags );
3187 if (fd == -1) return SOCKET_ERROR;
3189 if (flags & FD_FLAG_RECV_SHUTDOWN)
3191 WSASetLastError ( WSAESHUTDOWN );
3195 iovec = HeapAlloc ( GetProcessHeap(), 0, dwBufferCount * sizeof (struct iovec) );
3202 for (i = 0; i < dwBufferCount; i++)
3204 iovec[i].iov_base = lpBuffers[i].buf;
3205 iovec[i].iov_len = lpBuffers[i].len;
3208 if ( (lpOverlapped || lpCompletionRoutine) && flags & FD_FLAG_OVERLAPPED )
3210 wsa = WS2_make_async ( s, fd, ASYNC_TYPE_READ, iovec, dwBufferCount,
3211 lpFlags, lpFrom, lpFromlen,
3212 lpOverlapped, lpCompletionRoutine );
3220 if ( ( ret = register_new_async ( &wsa->async )) )
3222 err = NtStatusToWSAError ( ret );
3224 if ( !lpOverlapped )
3225 HeapFree ( GetProcessHeap(), 0, wsa->async.iosb );
3226 HeapFree ( GetProcessHeap(), 0, wsa );
3230 /* Try immediate completion */
3231 if ( lpOverlapped && !NtResetEvent( lpOverlapped->hEvent, NULL ) )
3233 if ( WSAGetOverlappedResult ( s, lpOverlapped,
3234 lpNumberOfBytesRecvd, FALSE, lpFlags) )
3237 if ( (err = WSAGetLastError ()) != WSA_IO_INCOMPLETE )
3241 WSASetLastError ( WSA_IO_PENDING );
3242 return SOCKET_ERROR;
3245 if ( _is_blocking(s) )
3248 /* FIXME: OOB and exceptfds? */
3249 do_block(fd, POLLIN);
3252 n = WS2_recv ( fd, iovec, dwBufferCount, lpFrom, lpFromlen, lpFlags );
3259 TRACE(" -> %i bytes\n", n);
3260 *lpNumberOfBytesRecvd = n;
3262 HeapFree (GetProcessHeap(), 0, iovec);
3263 release_sock_fd( s, fd );
3264 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
3269 HeapFree (GetProcessHeap(), 0, iovec);
3272 release_sock_fd( s, fd );
3275 WARN(" -> ERROR %d\n", err);
3276 WSASetLastError ( err );
3277 return SOCKET_ERROR;
3280 /***********************************************************************
3281 * WSCInstallProvider (WS2_32.88)
3283 INT WINAPI WSCInstallProvider( const LPGUID lpProviderId,
3284 LPCWSTR lpszProviderDllPath,
3285 const LPWSAPROTOCOL_INFOW lpProtocolInfoList,
3286 DWORD dwNumberOfEntries,
3289 FIXME("(%s, %s, %p, %ld, %p): stub !\n", debugstr_guid(lpProviderId),
3290 debugstr_w(lpszProviderDllPath), lpProtocolInfoList,
3291 dwNumberOfEntries, lpErrno);
3297 /***********************************************************************
3298 * WSCDeinstallProvider (WS2_32.83)
3300 INT WINAPI WSCDeinstallProvider(LPGUID lpProviderId, LPINT lpErrno)
3302 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId), lpErrno);
3308 /***********************************************************************
3309 * WSAAccept (WS2_32.26)
3311 SOCKET WINAPI WSAAccept( SOCKET s, struct WS_sockaddr *addr, LPINT addrlen,
3312 LPCONDITIONPROC lpfnCondition, DWORD dwCallbackData)
3315 int ret = 0, size = 0;
3316 WSABUF CallerId, CallerData, CalleeId, CalleeData;
3317 /* QOS SQOS, GQOS; */
3320 SOCKADDR src_addr, dst_addr;
3322 TRACE("Socket %u, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %ld\n",
3323 s, addr, addrlen, lpfnCondition, dwCallbackData);
3326 size = sizeof(src_addr);
3327 cs = WS_accept(s, &src_addr, &size);
3329 if (cs == SOCKET_ERROR) return SOCKET_ERROR;
3331 CallerId.buf = (char *)&src_addr;
3332 CallerId.len = sizeof(src_addr);
3334 CallerData.buf = NULL;
3335 CallerData.len = (ULONG)NULL;
3337 WS_getsockname(cs, &dst_addr, &size);
3339 CalleeId.buf = (char *)&dst_addr;
3340 CalleeId.len = sizeof(dst_addr);
3343 ret = (*lpfnCondition)(&CallerId, &CallerData, NULL, NULL,
3344 &CalleeId, &CalleeData, &g, dwCallbackData);
3349 if (addr && addrlen)
3350 addr = memcpy(addr, &src_addr, (*addrlen > size) ? size : *addrlen );
3353 SERVER_START_REQ ( set_socket_deferred )
3355 req->handle = SOCKET2HANDLE (s);
3356 req->deferred = SOCKET2HANDLE (cs);
3357 if ( !wine_server_call_err ( req ) )
3359 SetLastError ( WSATRY_AGAIN );
3360 WS_closesocket ( cs );
3364 return SOCKET_ERROR;
3367 SetLastError(WSAECONNREFUSED);
3368 return SOCKET_ERROR;
3370 FIXME("Unknown return type from Condition function\n");
3371 SetLastError(WSAENOTSOCK);
3372 return SOCKET_ERROR;
3376 /***********************************************************************
3377 * WSAEnumProtocolsA (WS2_32.37)
3379 int WINAPI WSAEnumProtocolsA(LPINT lpiProtocols, LPWSAPROTOCOL_INFOA lpProtocolBuffer, LPDWORD lpdwBufferLength)
3381 FIXME("(%p,%p,%p): stub\n", lpiProtocols,lpProtocolBuffer, lpdwBufferLength);
3385 /***********************************************************************
3386 * WSAEnumProtocolsW (WS2_32.38)
3388 int WINAPI WSAEnumProtocolsW(LPINT lpiProtocols, LPWSAPROTOCOL_INFOW lpProtocolBuffer, LPDWORD lpdwBufferLength)
3390 FIXME("(%p,%p,%p): stub\n", lpiProtocols,lpProtocolBuffer, lpdwBufferLength);
3394 /***********************************************************************
3395 * WSADuplicateSocketA (WS2_32.32)
3397 int WINAPI WSADuplicateSocketA( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOA lpProtocolInfo )
3401 TRACE("(%d,%lx,%p)\n", s, dwProcessId, lpProtocolInfo);
3402 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
3403 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
3404 /* I don't know what the real Windoze does next, this is a hack */
3405 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
3406 * the target use the global duplicate, or we could copy a reference to us to the structure
3407 * and let the target duplicate it from us, but let's do it as simple as possible */
3408 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
3409 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
3410 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
3411 0, FALSE, DUPLICATE_SAME_ACCESS);
3412 CloseHandle(hProcess);
3413 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
3417 /***********************************************************************
3418 * WSAInstallServiceClassA (WS2_32.48)
3420 int WINAPI WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info)
3422 FIXME("Request to install service %s\n",debugstr_a(info->lpszServiceClassName));
3423 WSASetLastError(WSAEACCES);
3424 return SOCKET_ERROR;
3427 /***********************************************************************
3428 * WSAInstallServiceClassW (WS2_32.49)
3430 int WINAPI WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info)
3432 FIXME("Request to install service %s\n",debugstr_w(info->lpszServiceClassName));
3433 WSASetLastError(WSAEACCES);
3434 return SOCKET_ERROR;
3437 /***********************************************************************
3438 * WSARemoveServiceClass (WS2_32.70)
3440 int WINAPI WSARemoveServiceClass(LPGUID info)
3442 FIXME("Request to remove service %p\n",info);
3443 WSASetLastError(WSATYPE_NOT_FOUND);
3444 return SOCKET_ERROR;
3447 /***********************************************************************
3448 * WSAStringToAddressA (WS2_32.80)
3450 INT WINAPI WSAStringToAddressA(LPSTR AddressString,
3452 LPWSAPROTOCOL_INFOA lpProtocolInfo,
3453 LPSOCKADDR lpAddress,
3454 LPINT lpAddressLength)
3456 FIXME("(%s, %x, %p, %p, %p) Stub!\n", AddressString, AddressFamily, lpProtocolInfo, lpAddress, lpAddressLength);