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"
31 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #ifdef HAVE_SYS_FILIO_H
37 # include <sys/filio.h>
39 #ifdef HAVE_SYS_SOCKIO_H
40 # include <sys/sockio.h>
44 # include <sys/so_ioctl.h>
47 #ifdef HAVE_SYS_PARAM_H
48 # include <sys/param.h>
54 #ifdef HAVE_SYS_WAIT_H
55 # include <sys/wait.h>
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
60 #ifdef HAVE_NETINET_IN_H
61 # include <netinet/in.h>
63 #ifdef HAVE_NETINET_TCP_H
64 # include <netinet/tcp.h>
66 #ifdef HAVE_ARPA_INET_H
67 # include <arpa/inet.h>
72 #ifdef HAVE_SYS_ERRNO_H
73 #include <sys/errno.h>
80 #ifdef HAVE_ARPA_NAMESER_H
81 # include <arpa/nameser.h>
90 # include <netipx/ipx.h>
94 # include <asm/types.h>
95 # include <linux/ipx.h>
99 #include "wine/winbase16.h"
102 #include "winsock2.h"
103 #include "ws2tcpip.h"
105 #include "wine/winsock16.h"
107 #include "services.h"
108 #include "wine/server.h"
110 #include "wine/debug.h"
113 WINE_DEFAULT_DEBUG_CHANNEL(winsock);
115 /* critical section to protect some non-rentrant net function */
116 extern CRITICAL_SECTION csWSgetXXXbyYYY;
118 #define DEBUG_SOCKADDR 0
119 #define dump_sockaddr(a) \
120 DPRINTF("sockaddr_in: family %d, address %s, port %d\n", \
121 ((struct sockaddr_in *)a)->sin_family, \
122 inet_ntoa(((struct sockaddr_in *)a)->sin_addr), \
123 ntohs(((struct sockaddr_in *)a)->sin_port))
125 /* ----------------------------------- internal data */
127 /* ws_... struct conversion flags */
129 #define WS_DUP_LINEAR 0x0001
130 #define WS_DUP_SEGPTR 0x0002 /* internal pointers are SEGPTRs */
131 /* by default, internal pointers are linear */
132 typedef struct /* WSAAsyncSelect() control struct */
134 HANDLE service, event, sock;
140 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
141 #define WS_MAX_UDP_DATAGRAM 1024
143 #define WS_ACCEPT_QUEUE 6
145 #define PROCFS_NETDEV_FILE "/proc/net/dev" /* Points to the file in the /proc fs
146 that lists the network devices.
147 Do we need an #ifdef LINUX for this? */
149 static volatile HANDLE accept_old[WS_ACCEPT_QUEUE], accept_new[WS_ACCEPT_QUEUE];
151 static void *he_buffer; /* typecast for Win16/32 ws_hostent */
152 static SEGPTR he_buffer_seg;
153 static void *se_buffer; /* typecast for Win16/32 ws_servent */
154 static SEGPTR se_buffer_seg;
155 static void *pe_buffer; /* typecast for Win16/32 ws_protoent */
156 static SEGPTR pe_buffer_seg;
157 static char* local_buffer;
158 static SEGPTR dbuffer_seg;
159 static INT num_startup; /* reference counter */
160 static FARPROC blocking_hook;
162 /* function prototypes */
163 static int WS_dup_he(struct hostent* p_he, int flag);
164 static int WS_dup_pe(struct protoent* p_pe, int flag);
165 static int WS_dup_se(struct servent* p_se, int flag);
167 typedef void WIN_hostent;
168 typedef void WIN_protoent;
169 typedef void WIN_servent;
171 int WSAIOCTL_GetInterfaceCount(void);
172 int WSAIOCTL_GetInterfaceName(int intNumber, char *intName);
174 UINT16 wsaErrno(void);
175 UINT16 wsaHerrno(int errnr);
177 static HANDLE _WSHeap = 0;
179 #define WS_ALLOC(size) \
180 HeapAlloc(_WSHeap, HEAP_ZERO_MEMORY, (size) )
181 #define WS_FREE(ptr) \
182 HeapFree(_WSHeap, 0, (ptr) )
184 static INT _ws_sock_ops[] =
185 { WS_SO_DEBUG, WS_SO_REUSEADDR, WS_SO_KEEPALIVE, WS_SO_DONTROUTE,
186 WS_SO_BROADCAST, WS_SO_LINGER, WS_SO_OOBINLINE, WS_SO_SNDBUF,
187 WS_SO_RCVBUF, WS_SO_ERROR, WS_SO_TYPE,
195 static int _px_sock_ops[] =
196 { SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST,
197 SO_LINGER, SO_OOBINLINE, SO_SNDBUF, SO_RCVBUF, SO_ERROR, SO_TYPE,
206 static INT _ws_tcp_ops[] = {
212 static int _px_tcp_ops[] = {
219 /* Holds value of SO_OPENTYPE socket option. This is essentially a global
220 * variable that Windows uses to affect how new sockets are created. See
221 * <http://support.microsoft.com/default.aspx?scid=kb;EN-US;q181611>. Right
222 * now, Wine does not do anything with this value other than get and set it on
225 static int opentype = 0;
227 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which we post
228 * messages if there are multiple events. Used in WINSOCK_DoAsyncEvent. The
229 * problem is if there is both a FD_CONNECT event and, say, an FD_READ event
230 * available on the same socket, we want to notify the app of the connect event
231 * first. Otherwise it may discard the read event because it thinks it hasn't
234 static const int event_bitorder[FD_MAX_EVENTS] = {
241 6, 7, 8, 9 /* leftovers */
244 /* set last error code from NT status without mapping WSA errors */
245 inline static unsigned int set_error( unsigned int err )
249 /* do not map WSA errors */
250 if ((err < WSABASEERR) || (err >= 0x10000000)) err = RtlNtStatusToDosError(err);
256 static char* check_buffer(int size);
258 static int _get_sock_fd(SOCKET s)
260 int fd = FILE_GetUnixHandle( s, GENERIC_READ );
262 FIXME("handle %d is not a socket (GLE %ld)\n",s,GetLastError());
266 static void _enable_event(SOCKET s, unsigned int event,
267 unsigned int sstate, unsigned int cstate)
269 SERVER_START_REQ( enable_socket_event )
273 req->sstate = sstate;
274 req->cstate = cstate;
275 wine_server_call( req );
280 static int _is_blocking(SOCKET s)
283 SERVER_START_REQ( get_socket_event )
286 req->service = FALSE;
289 wine_server_call( req );
290 ret = (reply->state & FD_WINE_NONBLOCKING) == 0;
296 static unsigned int _get_sock_mask(SOCKET s)
299 SERVER_START_REQ( get_socket_event )
302 req->service = FALSE;
305 wine_server_call( req );
312 static void _sync_sock_state(SOCKET s)
314 /* do a dummy wineserver request in order to let
315 the wineserver run through its select loop once */
316 (void)_is_blocking(s);
319 static int _get_sock_error(SOCKET s, unsigned int bit)
321 int events[FD_MAX_EVENTS];
323 SERVER_START_REQ( get_socket_event )
326 req->service = FALSE;
329 wine_server_set_reply( req, events, sizeof(events) );
330 wine_server_call( req );
336 static void WINSOCK_DeleteIData(void)
338 /* delete scratch buffers */
340 UnMapLS( he_buffer_seg );
341 UnMapLS( se_buffer_seg );
342 UnMapLS( pe_buffer_seg );
343 UnMapLS( dbuffer_seg );
344 if (he_buffer) HeapFree( GetProcessHeap(), 0, he_buffer );
345 if (se_buffer) HeapFree( GetProcessHeap(), 0, se_buffer );
346 if (pe_buffer) HeapFree( GetProcessHeap(), 0, pe_buffer );
347 if (local_buffer) HeapFree( GetProcessHeap(), 0, local_buffer );
359 /***********************************************************************
360 * WS_LibMain (WS2_32.init)
362 BOOL WINAPI WS_LibMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
364 TRACE("0x%x 0x%lx %p\n", hInstDLL, fdwReason, fImpLoad);
366 case DLL_PROCESS_DETACH:
367 WINSOCK_DeleteIData();
373 /***********************************************************************
376 * Converts socket flags from Windows format.
377 * Return 1 if converted, 0 if not (error).
379 static int convert_sockopt(INT *level, INT *optname)
386 for(i=0; _ws_sock_ops[i]; i++)
387 if( _ws_sock_ops[i] == *optname ) break;
388 if( _ws_sock_ops[i] ) {
389 *optname = _px_sock_ops[i];
392 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname);
395 *level = IPPROTO_TCP;
396 for(i=0; _ws_tcp_ops[i]; i++)
397 if ( _ws_tcp_ops[i] == *optname ) break;
398 if( _ws_tcp_ops[i] ) {
399 *optname = _px_tcp_ops[i];
402 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname);
408 /* ----------------------------------- Per-thread info (or per-process?) */
410 static int wsi_strtolo(const char* name, const char* opt)
412 /* Stuff a lowercase copy of the string into the local buffer */
414 int i = strlen(name) + 2;
415 char* p = check_buffer(i + ((opt)?strlen(opt):0));
419 do *p++ = tolower(*name); while(*name++);
420 i = (p - local_buffer);
421 if( opt ) do *p++ = tolower(*opt); while(*opt++);
427 static fd_set* fd_set_import( fd_set* fds, void* wsfds, int* highfd, int lfd[], BOOL b32 )
429 /* translate Winsock fd set into local fd set */
433 #define wsfds16 ((ws_fd_set16*)wsfds)
434 #define wsfds32 ((WS_fd_set*)wsfds)
438 count = b32 ? wsfds32->fd_count : wsfds16->fd_count;
440 for( i = 0; i < count; i++ )
442 int s = (b32) ? wsfds32->fd_array[i]
443 : wsfds16->fd_array[i];
444 int fd = _get_sock_fd(s);
448 if( fd > *highfd ) *highfd = fd;
460 inline static int sock_error_p(int s)
462 unsigned int optval, optlen;
464 optlen = sizeof(optval);
465 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
466 if (optval) WARN("\t[%i] error: %d\n", s, optval);
470 static int fd_set_export( fd_set* fds, fd_set* exceptfds, void* wsfds, int lfd[], BOOL b32 )
474 /* translate local fd set into Winsock fd set, adding
475 * errors to exceptfds (only if app requested it) */
479 #define wsfds16 ((ws_fd_set16*)wsfds)
480 #define wsfds32 ((WS_fd_set*)wsfds)
481 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
483 for( i = 0, j = 0; i < count; i++ )
488 if( FD_ISSET(fd, fds) )
490 if ( exceptfds && sock_error_p(fd) )
492 FD_SET(fd, exceptfds);
496 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
498 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
505 if( b32 ) wsfds32->fd_count = j;
506 else wsfds16->fd_count = j;
515 static void fd_set_unimport( void* wsfds, int lfd[], BOOL b32 )
519 #define wsfds16 ((ws_fd_set16*)wsfds)
520 #define wsfds32 ((WS_fd_set*)wsfds)
521 int i, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
523 for( i = 0; i < count; i++ )
533 static int do_block( int fd, int mask )
544 i = select( fd+1, &fds[0], &fds[1], &fds[2], NULL );
545 if (i <= 0) return -1;
548 if (FD_ISSET(fd, &fds[i]))
553 void* __ws_memalloc( int size )
555 return WS_ALLOC(size);
558 void __ws_memfree(void* ptr)
564 /* ----------------------------------- API -----
566 * Init / cleanup / error checking.
569 /***********************************************************************
570 * WSAStartup (WINSOCK.115)
572 * Create socket control struct, attach it to the global list and
573 * update a pointer in the task struct.
575 INT16 WINAPI WSAStartup16(UINT16 wVersionRequested, LPWSADATA16 lpWSAData)
577 static const WSADATA16 data =
583 #elif defined(__NetBSD__)
587 #elif defined(__FreeBSD__)
589 #elif defined(__OpenBSD__)
594 WS_MAX_SOCKETS_PER_PROCESS,
599 TRACE("verReq=%x\n", wVersionRequested);
601 if (LOBYTE(wVersionRequested) < 1 || (LOBYTE(wVersionRequested) == 1 &&
602 HIBYTE(wVersionRequested) < 1)) return WSAVERNOTSUPPORTED;
604 if (!lpWSAData) return WSAEINVAL;
606 /* initialize socket heap */
610 _WSHeap = HeapCreate(HEAP_ZERO_MEMORY, 8120, 32768);
613 ERR("Fatal: failed to create WinSock heap\n");
617 if( _WSHeap == 0 ) return WSASYSNOTREADY;
621 /* return winsock information */
623 memcpy(lpWSAData, &data, sizeof(data));
625 TRACE("succeeded\n");
629 /***********************************************************************
630 * WSAStartup (WS2_32.115)
632 int WINAPI WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData)
634 static const WSADATA data =
640 #elif defined(__NetBSD__)
644 #elif defined(__FreeBSD__)
646 #elif defined(__OpenBSD__)
651 WS_MAX_SOCKETS_PER_PROCESS,
656 TRACE("verReq=%x\n", wVersionRequested);
658 if (LOBYTE(wVersionRequested) < 1)
659 return WSAVERNOTSUPPORTED;
661 if (!lpWSAData) return WSAEINVAL;
663 /* initialize socket heap */
667 _WSHeap = HeapCreate(HEAP_ZERO_MEMORY, 8120, 32768);
670 ERR("Fatal: failed to create WinSock heap\n");
674 if( _WSHeap == 0 ) return WSASYSNOTREADY;
678 /* return winsock information */
679 memcpy(lpWSAData, &data, sizeof(data));
681 /* that's the whole of the negotiation for now */
682 lpWSAData->wVersion = wVersionRequested;
684 TRACE("succeeded\n");
689 /***********************************************************************
690 * WSACleanup (WINSOCK.116)
691 * WSACleanup (WS2_32.116)
693 INT WINAPI WSACleanup(void)
697 if (--num_startup > 0) return 0;
698 WINSOCK_DeleteIData();
701 SetLastError(WSANOTINITIALISED);
706 /***********************************************************************
707 * WSAGetLastError (WINSOCK.111)
708 * WSAGetLastError (WS2_32.111)
710 INT WINAPI WSAGetLastError(void)
712 return GetLastError();
715 /***********************************************************************
716 * WSASetLastError (WS2_32.112)
718 void WINAPI WSASetLastError(INT iError) {
719 SetLastError(iError);
722 /***********************************************************************
723 * WSASetLastError (WINSOCK.112)
725 void WINAPI WSASetLastError16(INT16 iError)
727 WSASetLastError(iError);
730 static char* check_buffer(int size)
732 static int local_buflen;
736 if (local_buflen >= size ) return local_buffer;
737 HeapFree( GetProcessHeap(), 0, local_buffer );
739 local_buffer = HeapAlloc( GetProcessHeap(), 0, (local_buflen = size) );
743 static struct ws_hostent* check_buffer_he(int size)
748 if (he_len >= size ) return he_buffer;
749 UnMapLS( he_buffer_seg );
750 HeapFree( GetProcessHeap(), 0, he_buffer );
752 he_buffer = HeapAlloc( GetProcessHeap(), 0, (he_len = size) );
753 he_buffer_seg = MapLS( he_buffer );
757 static void* check_buffer_se(int size)
762 if (se_len >= size ) return se_buffer;
763 UnMapLS( se_buffer_seg );
764 HeapFree( GetProcessHeap(), 0, se_buffer );
766 se_buffer = HeapAlloc( GetProcessHeap(), 0, (se_len = size) );
767 se_buffer_seg = MapLS( he_buffer );
771 static struct ws_protoent* check_buffer_pe(int size)
776 if (pe_len >= size ) return pe_buffer;
777 UnMapLS( pe_buffer_seg );
778 HeapFree( GetProcessHeap(), 0, pe_buffer );
780 pe_buffer = HeapAlloc( GetProcessHeap(), 0, (pe_len = size) );
781 pe_buffer_seg = MapLS( he_buffer );
785 /* ----------------------------------- i/o APIs */
788 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf)== WS_AF_IPX)
790 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET)
793 static void ws2_async_accept(SOCKET s, SOCKET as)
796 /* queue socket for WSAAsyncSelect */
797 for (q=0; q<WS_ACCEPT_QUEUE; q++)
798 if (InterlockedCompareExchange((LONG *)&accept_old[q], s, 0) == 0)
800 if (q<WS_ACCEPT_QUEUE)
803 ERR("accept queue too small\n");
804 /* now signal our AsyncSelect handler */
805 _enable_event(s, FD_WINE_SERVEVENT, 0, 0);
808 /**********************************************************************/
810 /* Returns the converted address if successful, NULL if it was too small to
811 * start with. Note that the returned pointer may be the original pointer
812 * if no conversion is necessary.
814 static const struct sockaddr* ws_sockaddr_ws2u(const struct WS_sockaddr* wsaddr, int wsaddrlen, int *uaddrlen)
816 switch (wsaddr->sa_family)
821 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
822 struct sockaddr_ipx* uipx;
824 if (wsaddrlen<sizeof(struct WS_sockaddr_ipx))
827 *uaddrlen=sizeof(struct sockaddr_ipx);
828 uipx=malloc(*uaddrlen);
829 uipx->sipx_family=AF_IPX;
830 uipx->sipx_port=wsipx->sa_socket;
831 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
834 memcpy(&uipx->sipx_network,wsipx->sa_netnum,sizeof(uipx->sipx_network)+sizeof(uipx->sipx_node));
835 uipx->sipx_type=IPX_FRAME_NONE;
837 return (const struct sockaddr*)uipx;
842 if (wsaddrlen<sizeof(struct WS_sockaddr))
845 /* No conversion needed, just return the original address */
847 return (const struct sockaddr*)wsaddr;
852 /* allocates a Unix sockaddr structure to receive the data */
853 inline struct sockaddr* ws_sockaddr_alloc(const struct WS_sockaddr* wsaddr, int* wsaddrlen, int* uaddrlen)
860 *uaddrlen=max(sizeof(struct sockaddr),*wsaddrlen);
862 return malloc(*uaddrlen);
865 /* Returns 0 if successful, -1 if the buffer is too small */
866 static int ws_sockaddr_u2ws(const struct sockaddr* uaddr, int uaddrlen, struct WS_sockaddr* wsaddr, int* wsaddrlen)
870 switch(uaddr->sa_family)
875 struct sockaddr_ipx* uipx=(struct sockaddr_ipx*)uaddr;
876 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
879 switch (*wsaddrlen) /* how much can we copy? */
884 wsipx->sa_socket=uipx->sipx_port;
888 memcpy(wsipx->sa_nodenum,uipx->sipx_node,sizeof(wsipx->sa_nodenum));
896 memcpy(wsipx->sa_netnum,&uipx->sipx_network,sizeof(wsipx->sa_netnum));
902 wsipx->sa_family=WS_AF_IPX;
914 /* No conversion needed */
915 memcpy(wsaddr,uaddr,*wsaddrlen);
916 if (*wsaddrlen<uaddrlen) {
926 /* to be called to free the memory allocated by ws_sockaddr_ws2u or
929 inline void ws_sockaddr_free(const struct sockaddr* uaddr, const struct WS_sockaddr* wsaddr)
931 if (uaddr!=NULL && uaddr!=(const struct sockaddr*)wsaddr)
935 /***********************************************************************
938 SOCKET WINAPI WS_accept(SOCKET s, struct WS_sockaddr *addr,
941 int fd = _get_sock_fd(s);
943 TRACE("socket %04x\n", (UINT16)s );
951 _sync_sock_state(s); /* let wineserver notice connection */
952 /* retrieve any error codes from it */
953 SetLastError(_get_sock_error(s, FD_ACCEPT_BIT));
954 /* FIXME: care about the error? */
957 SERVER_START_REQ( accept_socket )
960 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
962 set_error( wine_server_call( req ) );
963 as = (SOCKET)reply->handle;
968 unsigned omask = _get_sock_mask( s );
969 WS_getpeername(as, addr, addrlen32);
970 if (omask & FD_WINE_SERVEVENT)
971 ws2_async_accept(s, as);
977 SetLastError(WSAENOTSOCK);
979 return INVALID_SOCKET;
982 /***********************************************************************
985 SOCKET16 WINAPI WINSOCK_accept16(SOCKET16 s, struct WS_sockaddr* addr,
988 INT addrlen32 = addrlen16 ? *addrlen16 : 0;
989 SOCKET retSocket = WS_accept( s, addr, &addrlen32 );
990 if( addrlen16 ) *addrlen16 = (INT16)addrlen32;
991 return (SOCKET16)retSocket;
994 /***********************************************************************
997 int WINAPI WS_bind(SOCKET s, const struct WS_sockaddr* name, int namelen)
999 int fd = _get_sock_fd(s);
1002 TRACE("socket %04x, ptr %p, length %d\n", s, name, namelen);
1004 dump_sockaddr(name);
1010 if (!name || !SUPPORTED_PF(name->sa_family))
1012 SetLastError(WSAEAFNOSUPPORT);
1016 const struct sockaddr* uaddr;
1019 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1022 SetLastError(WSAEFAULT);
1026 if (bind(fd, uaddr, uaddrlen) < 0)
1028 int loc_errno = errno;
1029 WARN("\tfailure - errno = %i\n", errno);
1034 SetLastError(WSAENOTSOCK);
1037 SetLastError(WSAEINVAL);
1040 SetLastError(wsaErrno());
1046 res=0; /* success */
1048 ws_sockaddr_free(uaddr,name);
1055 SetLastError(WSAENOTSOCK);
1060 /***********************************************************************
1063 INT16 WINAPI WINSOCK_bind16(SOCKET16 s, struct WS_sockaddr *name, INT16 namelen)
1065 return (INT16)WS_bind( s, name, namelen );
1068 /***********************************************************************
1069 * closesocket (WS2_32.3)
1071 int WINAPI WS_closesocket(SOCKET s)
1073 TRACE("socket %08x\n", s);
1074 if (CloseHandle(s)) return 0;
1075 return SOCKET_ERROR;
1078 /***********************************************************************
1079 * closesocket (WINSOCK.3)
1081 INT16 WINAPI WINSOCK_closesocket16(SOCKET16 s)
1083 return (INT16)WS_closesocket(s);
1086 /***********************************************************************
1087 * connect (WS2_32.4)
1089 int WINAPI WS_connect(SOCKET s, const struct WS_sockaddr* name, int namelen)
1091 int fd = _get_sock_fd(s);
1093 TRACE("socket %04x, ptr %p, length %d\n", s, name, namelen);
1095 dump_sockaddr(name);
1100 const struct sockaddr* uaddr;
1103 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1106 SetLastError(WSAEFAULT);
1112 rc=connect(fd, uaddr, uaddrlen);
1113 ws_sockaddr_free(uaddr,name);
1115 goto connect_success;
1118 if (errno == EINPROGRESS)
1120 /* tell wineserver that a connection is in progress */
1121 _enable_event(s, FD_CONNECT|FD_READ|FD_WRITE,
1122 FD_CONNECT|FD_READ|FD_WRITE,
1123 FD_WINE_CONNECTED|FD_WINE_LISTENING);
1124 if (_is_blocking(s))
1129 _sync_sock_state(s); /* let wineserver notice connection */
1130 /* retrieve any error codes from it */
1131 result = _get_sock_error(s, FD_CONNECT_BIT);
1133 SetLastError(result);
1136 goto connect_success;
1141 SetLastError(WSAEWOULDBLOCK);
1146 SetLastError(wsaErrno());
1152 SetLastError(WSAENOTSOCK);
1154 return SOCKET_ERROR;
1158 _enable_event(s, FD_CONNECT|FD_READ|FD_WRITE,
1159 FD_WINE_CONNECTED|FD_READ|FD_WRITE,
1160 FD_CONNECT|FD_WINE_LISTENING);
1164 /***********************************************************************
1165 * connect (WINSOCK.4)
1167 INT16 WINAPI WINSOCK_connect16(SOCKET16 s, struct WS_sockaddr *name, INT16 namelen)
1169 return (INT16)WS_connect( s, name, namelen );
1172 /***********************************************************************
1173 * getpeername (WS2_32.5)
1175 int WINAPI WS_getpeername(SOCKET s, struct WS_sockaddr *name, int *namelen)
1177 int fd = _get_sock_fd(s);
1180 TRACE("socket: %04x, ptr %p, len %8x\n", s, name, *namelen);
1185 struct sockaddr* uaddr;
1188 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1189 if (getpeername(fd, uaddr, &uaddrlen) != 0)
1191 SetLastError(wsaErrno());
1193 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1195 /* The buffer was too small */
1196 SetLastError(WSAEFAULT);
1202 ws_sockaddr_free(uaddr,name);
1207 SetLastError(WSAENOTSOCK);
1212 /***********************************************************************
1213 * getpeername (WINSOCK.5)
1215 INT16 WINAPI WINSOCK_getpeername16(SOCKET16 s, struct WS_sockaddr *name,
1218 INT namelen32 = *namelen16;
1219 INT retVal = WS_getpeername( s, name, &namelen32 );
1222 dump_sockaddr(name);
1225 *namelen16 = namelen32;
1226 return (INT16)retVal;
1229 /***********************************************************************
1230 * getsockname (WS2_32.6)
1232 int WINAPI WS_getsockname(SOCKET s, struct WS_sockaddr *name, int *namelen)
1234 int fd = _get_sock_fd(s);
1237 TRACE("socket: %04x, ptr %p, len %8x\n", s, name, *namelen);
1242 struct sockaddr* uaddr;
1245 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1246 if (getsockname(fd, uaddr, &uaddrlen) != 0)
1248 SetLastError(wsaErrno());
1250 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1252 /* The buffer was too small */
1253 SetLastError(WSAEFAULT);
1263 SetLastError(WSAENOTSOCK);
1268 /***********************************************************************
1269 * getsockname (WINSOCK.6)
1271 INT16 WINAPI WINSOCK_getsockname16(SOCKET16 s, struct WS_sockaddr *name,
1278 INT namelen32 = *namelen16;
1279 retVal = WS_getsockname( s, name, &namelen32 );
1280 *namelen16 = namelen32;
1283 dump_sockaddr(name);
1287 else retVal = SOCKET_ERROR;
1288 return (INT16)retVal;
1292 /***********************************************************************
1293 * getsockopt (WS2_32.7)
1295 INT WINAPI WS_getsockopt(SOCKET s, INT level,
1296 INT optname, char *optval, INT *optlen)
1300 TRACE("socket: %04x, level 0x%x, name 0x%x, ptr %8x, len %d\n", s, level,
1301 (int) optname, (int) optval, (int) *optlen);
1302 /* SO_OPENTYPE does not require a valid socket handle. */
1303 if (level == WS_SOL_SOCKET && optname == WS_SO_OPENTYPE)
1305 if (!optlen || *optlen < sizeof(int) || !optval)
1307 SetLastError(WSAEFAULT);
1308 return SOCKET_ERROR;
1310 *(int *)optval = opentype;
1311 *optlen = sizeof(int);
1312 TRACE("getting global SO_OPENTYPE = 0x%x\n", opentype);
1316 fd = _get_sock_fd(s);
1319 if (!convert_sockopt(&level, &optname)) {
1320 SetLastError(WSAENOPROTOOPT); /* Unknown option */
1322 if (getsockopt(fd, (int) level, optname, optval, optlen) == 0 )
1327 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1331 return SOCKET_ERROR;
1335 /***********************************************************************
1336 * getsockopt (WINSOCK.7)
1338 INT16 WINAPI WINSOCK_getsockopt16(SOCKET16 s, INT16 level,
1339 INT16 optname, char *optval, INT16 *optlen)
1344 if( optlen ) optlen32 = *optlen; else p = NULL;
1345 retVal = WS_getsockopt( s, (UINT16)level, optname, optval, p );
1346 if( optlen ) *optlen = optlen32;
1347 return (INT16)retVal;
1351 /***********************************************************************
1355 u_long WINAPI WS_htonl(u_long hostlong)
1357 return htonl(hostlong);
1361 /***********************************************************************
1365 u_short WINAPI WS_htons(u_short hostshort)
1367 return htons(hostshort);
1371 /***********************************************************************
1372 * inet_addr (WINSOCK.10)
1373 * inet_addr (WS2_32.11)
1375 u_long WINAPI WS_inet_addr(const char *cp)
1377 return inet_addr(cp);
1381 /***********************************************************************
1382 * ntohl (WINSOCK.14)
1385 u_long WINAPI WS_ntohl(u_long netlong)
1387 return ntohl(netlong);
1391 /***********************************************************************
1392 * ntohs (WINSOCK.15)
1395 u_short WINAPI WS_ntohs(u_short netshort)
1397 return ntohs(netshort);
1401 /***********************************************************************
1402 * inet_ntoa (WS2_32.12)
1404 char* WINAPI WS_inet_ntoa(struct WS_in_addr in)
1406 /* use "buffer for dummies" here because some applications have
1407 * propensity to decode addresses in ws_hostent structure without
1408 * saving them first...
1410 static char dbuffer[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
1412 char* s = inet_ntoa(*((struct in_addr*)&in));
1418 SetLastError(wsaErrno());
1422 /***********************************************************************
1423 * inet_ntoa (WINSOCK.11)
1425 SEGPTR WINAPI WINSOCK_inet_ntoa16(struct in_addr in)
1428 if (!(retVal = WS_inet_ntoa(*((struct WS_in_addr*)&in)))) return 0;
1429 if (!dbuffer_seg) dbuffer_seg = MapLS( retVal );
1434 /**********************************************************************
1435 * WSAIoctl (WS2_32.50)
1438 * FIXME: Only SIO_GET_INTERFACE_LIST option implemented.
1440 INT WINAPI WSAIoctl (SOCKET s,
1441 DWORD dwIoControlCode,
1444 LPVOID lpbOutBuffer,
1446 LPDWORD lpcbBytesReturned,
1447 LPWSAOVERLAPPED lpOverlapped,
1448 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
1450 int fd = _get_sock_fd(s);
1454 switch( dwIoControlCode )
1456 case SIO_GET_INTERFACE_LIST:
1458 INTERFACE_INFO* intArray = (INTERFACE_INFO*)lpbOutBuffer;
1460 struct ifreq ifInfo;
1464 TRACE ("-> SIO_GET_INTERFACE_LIST request\n");
1466 numInt = WSAIOCTL_GetInterfaceCount();
1469 ERR ("Unable to open /proc filesystem to determine number of network interfaces!\n");
1471 WSASetLastError(WSAEINVAL);
1472 return (SOCKET_ERROR);
1475 for (i=0; i<numInt; i++)
1477 if (!WSAIOCTL_GetInterfaceName(i, ifName))
1479 ERR ("Error parsing /proc filesystem!\n");
1481 WSASetLastError(WSAEINVAL);
1482 return (SOCKET_ERROR);
1485 ifInfo.ifr_addr.sa_family = AF_INET;
1488 strcpy (ifInfo.ifr_name, ifName);
1489 if (ioctl(fd, SIOCGIFADDR, &ifInfo) < 0)
1491 ERR ("Error obtaining IP address\n");
1493 WSASetLastError(WSAEINVAL);
1494 return (SOCKET_ERROR);
1498 struct WS_sockaddr_in *ipTemp = (struct WS_sockaddr_in *)&ifInfo.ifr_addr;
1500 intArray->iiAddress.AddressIn.sin_family = AF_INET;
1501 intArray->iiAddress.AddressIn.sin_port = ipTemp->sin_port;
1502 intArray->iiAddress.AddressIn.sin_addr.WS_s_addr = ipTemp->sin_addr.S_un.S_addr;
1505 /* Broadcast Address */
1506 strcpy (ifInfo.ifr_name, ifName);
1507 if (ioctl(fd, SIOCGIFBRDADDR, &ifInfo) < 0)
1509 ERR ("Error obtaining Broadcast IP address\n");
1511 WSASetLastError(WSAEINVAL);
1512 return (SOCKET_ERROR);
1516 struct WS_sockaddr_in *ipTemp = (struct WS_sockaddr_in *)&ifInfo.ifr_broadaddr;
1518 intArray->iiBroadcastAddress.AddressIn.sin_family = AF_INET;
1519 intArray->iiBroadcastAddress.AddressIn.sin_port = ipTemp->sin_port;
1520 intArray->iiBroadcastAddress.AddressIn.sin_addr.WS_s_addr = ipTemp->sin_addr.S_un.S_addr;
1524 strcpy (ifInfo.ifr_name, ifName);
1525 if (ioctl(fd, SIOCGIFNETMASK, &ifInfo) < 0)
1527 ERR ("Error obtaining Subnet IP address\n");
1529 WSASetLastError(WSAEINVAL);
1530 return (SOCKET_ERROR);
1534 /* Trying to avoid some compile problems across platforms.
1535 (Linux, FreeBSD, Solaris...) */
1538 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
1539 intArray->iiNetmask.AddressIn.sin_port = 0;
1540 intArray->iiNetmask.AddressIn.sin_addr.WS_s_addr = 0;
1541 ERR ("Unable to determine Netmask on your platform!\n");
1543 struct WS_sockaddr_in *ipTemp = (struct WS_sockaddr_in *)&ifInfo.ifr_addr;
1545 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
1546 intArray->iiNetmask.AddressIn.sin_port = ipTemp->sin_port;
1547 intArray->iiNetmask.AddressIn.sin_addr.WS_s_addr = ipTemp->sin_addr.S_un.S_addr;
1550 struct WS_sockaddr_in *ipTemp = (struct WS_sockaddr_in *)&ifInfo.ifr_netmask;
1552 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
1553 intArray->iiNetmask.AddressIn.sin_port = ipTemp->sin_port;
1554 intArray->iiNetmask.AddressIn.sin_addr.WS_s_addr = ipTemp->sin_addr.S_un.S_addr;
1558 /* Socket Status Flags */
1559 strcpy(ifInfo.ifr_name, ifName);
1560 if (ioctl(fd, SIOCGIFFLAGS, &ifInfo) < 0)
1562 ERR ("Error obtaining status flags for socket!\n");
1564 WSASetLastError(WSAEINVAL);
1565 return (SOCKET_ERROR);
1569 /* FIXME - Is this the right flag to use? */
1570 intArray->iiFlags = ifInfo.ifr_flags;
1572 intArray++; /* Prepare for another interface */
1575 /* Calculate the size of the array being returned */
1576 *lpcbBytesReturned = sizeof(INTERFACE_INFO) * numInt;
1582 WARN("\tunsupported WS_IOCTL cmd (%08lx)\n", dwIoControlCode);
1584 WSASetLastError(WSAEOPNOTSUPP);
1585 return (SOCKET_ERROR);
1589 /* Function executed with no errors */
1595 WSASetLastError(WSAENOTSOCK);
1596 return (SOCKET_ERROR);
1602 Helper function for WSAIoctl - Get count of the number of interfaces
1603 by parsing /proc filesystem.
1605 int WSAIOCTL_GetInterfaceCount(void)
1608 char buf[512]; /* Size doesn't matter, something big */
1612 /* Open /proc filesystem file for network devices */
1613 procfs = fopen(PROCFS_NETDEV_FILE, "r");
1616 /* If we can't open the file, return an error */
1620 /* Omit first two lines, they are only headers */
1621 fgets(buf, sizeof buf, procfs);
1622 fgets(buf, sizeof buf, procfs);
1624 while (fgets(buf, sizeof buf, procfs))
1626 /* Each line in the file represents a network interface */
1636 Helper function for WSAIoctl - Get name of device from interface number
1637 by parsing /proc filesystem.
1639 int WSAIOCTL_GetInterfaceName(int intNumber, char *intName)
1642 char buf[512]; /* Size doesn't matter, something big */
1645 /* Open /proc filesystem file for network devices */
1646 procfs = fopen(PROCFS_NETDEV_FILE, "r");
1649 /* If we can't open the file, return an error */
1653 /* Omit first two lines, they are only headers */
1654 fgets(buf, sizeof(buf), procfs);
1655 fgets(buf, sizeof(buf), procfs);
1657 for (i=0; i<intNumber; i++)
1659 /* Skip the lines that don't interest us. */
1660 fgets(buf, sizeof(buf), procfs);
1662 fgets(buf, sizeof(buf), procfs); /* This is the line we want */
1665 /* Parse out the line, grabbing only the name of the device
1666 to the intName variable
1668 The Line comes in like this: (we only care about the device name)
1669 lo: 21970 377 0 0 0 0 0 0 21970 377 0 0 0 0 0 0
1672 while (isspace(buf[i])) /* Skip initial space(s) */
1679 if (isspace(buf[i]))
1684 if (buf[i] == ':') /* FIXME: Not sure if this block (alias detection) works properly */
1686 /* This interface could be an alias... */
1688 char *dotname = intName;
1689 *intName++ = buf[i++];
1691 while (isdigit(buf[i]))
1693 *intName++ = buf[i++];
1698 /* ... It wasn't, so back up */
1713 *intName++ = buf[i++];
1722 /***********************************************************************
1723 * ioctlsocket (WS2_32.10)
1725 int WINAPI WS_ioctlsocket(SOCKET s, long cmd, u_long *argp)
1727 int fd = _get_sock_fd(s);
1729 TRACE("socket %04x, cmd %08lx, ptr %8x\n", s, cmd, (unsigned) argp);
1742 if( _get_sock_mask(s) )
1744 /* AsyncSelect()'ed sockets are always nonblocking */
1749 SetLastError(WSAEINVAL);
1751 return SOCKET_ERROR;
1755 _enable_event(s, 0, FD_WINE_NONBLOCKING, 0);
1757 _enable_event(s, 0, 0, FD_WINE_NONBLOCKING);
1764 case WS__IOW('f',125,u_long):
1765 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
1766 SetLastError(WSAEINVAL);
1767 return SOCKET_ERROR;
1769 case SIOCGIFBRDADDR:
1770 case SIOCGIFNETMASK:
1772 /* These don't need any special handling. They are used by
1773 WsControl, and are here to suppress an unecessary warning. */
1778 /* Netscape tries hard to use bogus ioctl 0x667e */
1779 WARN("\tunknown WS_IOCTL cmd (%08lx)\n", cmd);
1781 if( ioctl(fd, newcmd, (char*)argp ) == 0 )
1786 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1789 return SOCKET_ERROR;
1792 /***********************************************************************
1793 * ioctlsocket (WINSOCK.12)
1795 INT16 WINAPI WINSOCK_ioctlsocket16(SOCKET16 s, LONG cmd, ULONG *argp)
1797 return (INT16)WS_ioctlsocket( s, cmd, argp );
1801 /***********************************************************************
1802 * listen (WS2_32.13)
1804 int WINAPI WS_listen(SOCKET s, int backlog)
1806 int fd = _get_sock_fd(s);
1808 TRACE("socket %04x, backlog %d\n", s, backlog);
1811 if (listen(fd, backlog) == 0)
1814 _enable_event(s, FD_ACCEPT,
1816 FD_CONNECT|FD_WINE_CONNECTED);
1819 SetLastError(wsaErrno());
1821 else SetLastError(WSAENOTSOCK);
1822 return SOCKET_ERROR;
1825 /***********************************************************************
1826 * listen (WINSOCK.13)
1828 INT16 WINAPI WINSOCK_listen16(SOCKET16 s, INT16 backlog)
1830 return (INT16)WS_listen( s, backlog );
1834 /***********************************************************************
1837 int WINAPI WS_recv(SOCKET s, char *buf, int len, int flags)
1839 int fd = _get_sock_fd(s);
1841 TRACE("socket %04x, buf %8x, len %d, flags %d\n", s, (unsigned)buf, len, flags);
1847 if (_is_blocking(s))
1850 /* FIXME: OOB and exceptfds? */
1853 if ((length = recv(fd, buf, len, flags)) >= 0)
1855 TRACE(" -> %i bytes\n", length);
1858 _enable_event(s, FD_READ, 0, 0);
1861 SetLastError(wsaErrno());
1864 else SetLastError(WSAENOTSOCK);
1865 WARN(" -> ERROR\n");
1866 return SOCKET_ERROR;
1869 /***********************************************************************
1872 INT16 WINAPI WINSOCK_recv16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1874 return (INT16)WS_recv( s, buf, len, flags );
1878 /***********************************************************************
1879 * recvfrom (WS2_32.17)
1881 int WINAPI WS_recvfrom(SOCKET s, char *buf, INT len, int flags,
1882 struct WS_sockaddr *from, int *fromlen)
1884 int fd = _get_sock_fd(s);
1887 TRACE("socket %04x, ptr %08x, len %d, flags %d\n", s, (unsigned)buf, len, flags);
1890 dump_sockaddr(from);
1892 DPRINTF("from = NULL\n");
1898 struct sockaddr* uaddr;
1902 if (_is_blocking(s))
1905 /* FIXME: OOB and exceptfds */
1909 uaddr=ws_sockaddr_alloc(from,fromlen,&uaddrlen);
1910 length=recvfrom(fd, buf, len, flags, uaddr, &uaddrlen);
1913 SetLastError(wsaErrno());
1914 WARN(" -> ERROR\n");
1916 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,from,fromlen) != 0)
1918 /* The from buffer was too small, but we read the data
1919 * anyway. Is that really bad?
1921 SetLastError(WSAEFAULT);
1922 WARN(" -> WSAEFAULT\n");
1926 TRACE(" -> %i bytes\n", length);
1927 _enable_event(s, FD_READ, 0, 0);
1934 SetLastError(WSAENOTSOCK);
1935 WARN(" -> WSAENOTSOCK\n");
1940 /***********************************************************************
1941 * recvfrom (WINSOCK.17)
1943 INT16 WINAPI WINSOCK_recvfrom16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1944 struct WS_sockaddr *from, INT16 *fromlen16)
1947 INT *p = &fromlen32;
1950 if( fromlen16 ) fromlen32 = *fromlen16; else p = NULL;
1951 retVal = WS_recvfrom( s, buf, len, flags, from, p );
1952 if( fromlen16 ) *fromlen16 = fromlen32;
1953 return (INT16)retVal;
1956 /***********************************************************************
1959 static int __ws_select(BOOL b32,
1960 void *ws_readfds, void *ws_writefds, void *ws_exceptfds,
1961 const struct WS_timeval *ws_timeout)
1964 fd_set readfds, writefds, exceptfds;
1965 fd_set *p_read, *p_write, *p_except;
1966 int readfd[FD_SETSIZE], writefd[FD_SETSIZE], exceptfd[FD_SETSIZE];
1967 struct timeval timeout, *timeoutaddr = NULL;
1969 TRACE("read %p, write %p, excp %p timeout %p\n",
1970 ws_readfds, ws_writefds, ws_exceptfds, ws_timeout);
1972 p_read = fd_set_import(&readfds, ws_readfds, &highfd, readfd, b32);
1973 p_write = fd_set_import(&writefds, ws_writefds, &highfd, writefd, b32);
1974 p_except = fd_set_import(&exceptfds, ws_exceptfds, &highfd, exceptfd, b32);
1977 timeoutaddr = &timeout;
1978 timeout.tv_sec=ws_timeout->tv_sec;
1979 timeout.tv_usec=ws_timeout->tv_usec;
1982 if( (highfd = select(highfd + 1, p_read, p_write, p_except, timeoutaddr)) > 0 )
1984 fd_set_export(&readfds, p_except, ws_readfds, readfd, b32);
1985 fd_set_export(&writefds, p_except, ws_writefds, writefd, b32);
1987 if (p_except && ws_exceptfds)
1989 #define wsfds16 ((ws_fd_set16*)ws_exceptfds)
1990 #define wsfds32 ((WS_fd_set*)ws_exceptfds)
1991 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
1993 for (i = j = 0; i < count; i++)
1995 int fd = exceptfd[i];
1996 if( fd >= 0 && FD_ISSET(fd, &exceptfds) )
1999 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
2001 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
2003 if( fd >= 0 ) close(fd);
2007 wsfds32->fd_count = j;
2009 wsfds16->fd_count = j;
2015 fd_set_unimport(ws_readfds, readfd, b32);
2016 fd_set_unimport(ws_writefds, writefd, b32);
2017 fd_set_unimport(ws_exceptfds, exceptfd, b32);
2018 if( ws_readfds ) ((WS_fd_set*)ws_readfds)->fd_count = 0;
2019 if( ws_writefds ) ((WS_fd_set*)ws_writefds)->fd_count = 0;
2020 if( ws_exceptfds ) ((WS_fd_set*)ws_exceptfds)->fd_count = 0;
2022 if( highfd == 0 ) return 0;
2023 SetLastError(wsaErrno());
2024 return SOCKET_ERROR;
2027 /***********************************************************************
2028 * select (WINSOCK.18)
2030 INT16 WINAPI WINSOCK_select16(INT16 nfds, ws_fd_set16 *ws_readfds,
2031 ws_fd_set16 *ws_writefds, ws_fd_set16 *ws_exceptfds,
2032 struct WS_timeval* timeout)
2034 return (INT16)__ws_select( FALSE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
2037 /***********************************************************************
2038 * select (WS2_32.18)
2040 int WINAPI WS_select(int nfds, WS_fd_set *ws_readfds,
2041 WS_fd_set *ws_writefds, WS_fd_set *ws_exceptfds,
2042 const struct WS_timeval* timeout)
2044 /* struct timeval is the same for both 32- and 16-bit code */
2045 return (INT)__ws_select( TRUE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
2049 /***********************************************************************
2052 int WINAPI WS_send(SOCKET s, const char *buf, int len, int flags)
2054 int fd = _get_sock_fd(s);
2056 TRACE("socket %04x, ptr %p, length %d, flags %d\n", s, buf, len, flags);
2061 if (_is_blocking(s))
2064 /* FIXME: exceptfds */
2067 if ((length = send(fd, buf, len, flags)) < 0 )
2069 SetLastError(wsaErrno());
2070 if( GetLastError() == WSAEWOULDBLOCK )
2071 _enable_event(s, FD_WRITE, 0, 0);
2080 else SetLastError(WSAENOTSOCK);
2081 return SOCKET_ERROR;
2084 /***********************************************************************
2085 * WSASend (WS2_32.72)
2087 INT WINAPI WSASend( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2088 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2089 LPWSAOVERLAPPED lpOverlapped,
2090 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2096 /* Overlapped is not supported or checked for */
2097 FIXME( "(%u,%p,0x%lx,%p,0x%lx,%p,%p): semi stub\n",
2098 s, lpBuffers, dwBufferCount, lpNumberOfBytesSent,
2099 dwFlags, lpOverlapped, lpCompletionRoutine );
2101 /* Convert setup flags */
2102 if( dwFlags & MSG_DONTROUTE )
2104 iFlags |= MSG_DONTROUTE;
2107 if( dwFlags & MSG_OOB )
2112 /* Indicate nothing yet sent */
2113 *lpNumberOfBytesSent = 0;
2115 /* Send all buffers with the same flags */
2116 for(dwCount = 0; dwCount < dwBufferCount; dwCount++ )
2118 if( ( rc = WS_send( s, lpBuffers[ dwCount ].buf,
2119 lpBuffers[ dwCount ].len, iFlags ) ) != 0 )
2124 /* Indicate that we've sent something */
2125 *lpNumberOfBytesSent += lpBuffers[ dwCount ].len;
2131 /***********************************************************************
2134 INT16 WINAPI WINSOCK_send16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
2136 return WS_send( s, buf, len, flags );
2139 /***********************************************************************
2140 * sendto (WS2_32.20)
2142 int WINAPI WS_sendto(SOCKET s, const char *buf, int len, int flags,
2143 const struct WS_sockaddr *to, int tolen)
2145 int fd = _get_sock_fd(s);
2148 TRACE("socket %04x, ptr %p, length %d, flags %d\n", s, buf, len, flags);
2153 const struct sockaddr* uaddr;
2156 uaddr=ws_sockaddr_ws2u(to,tolen,&uaddrlen);
2159 SetLastError(WSAEFAULT);
2163 if (_is_blocking(s))
2166 /* FIXME: exceptfds */
2169 res=sendto(fd, buf, len, flags, uaddr, uaddrlen);
2172 SetLastError(wsaErrno());
2173 if( GetLastError() == WSAEWOULDBLOCK )
2174 _enable_event(s, FD_WRITE, 0, 0);
2176 ws_sockaddr_free(uaddr,to);
2182 SetLastError(WSAENOTSOCK);
2187 /***********************************************************************
2188 * sendto (WINSOCK.20)
2190 INT16 WINAPI WINSOCK_sendto16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
2191 struct WS_sockaddr *to, INT16 tolen)
2193 return (INT16)WS_sendto( s, buf, len, flags, to, tolen );
2196 /***********************************************************************
2197 * setsockopt (WS2_32.21)
2199 int WINAPI WS_setsockopt(SOCKET s, int level, int optname,
2200 const char *optval, int optlen)
2204 TRACE("socket: %04x, level 0x%x, name 0x%x, ptr %8x, len %d\n", s, level,
2205 (int) optname, (int) optval, optlen);
2206 /* SO_OPENTYPE does not require a valid socket handle. */
2207 if (level == WS_SOL_SOCKET && optname == WS_SO_OPENTYPE)
2209 if (optlen < sizeof(int) || !optval)
2211 SetLastError(WSAEFAULT);
2212 return SOCKET_ERROR;
2214 opentype = *(int *)optval;
2215 TRACE("setting global SO_OPENTYPE to 0x%x\n", opentype);
2219 fd = _get_sock_fd(s);
2222 struct linger linger;
2225 /* Is a privileged and useless operation, so we don't. */
2226 if ((optname == WS_SO_DEBUG) && (level == WS_SOL_SOCKET)) {
2227 FIXME("(%d,SOL_SOCKET,SO_DEBUG,%p(%ld)) attempted (is privileged). Ignoring.\n",s,optval,*(DWORD*)optval);
2231 if(optname == WS_SO_DONTLINGER && level == WS_SOL_SOCKET) {
2232 /* This is unique to WinSock and takes special conversion */
2233 linger.l_onoff = *((int*)optval) ? 0: 1;
2234 linger.l_linger = 0;
2236 optval = (char*)&linger;
2237 optlen = sizeof(struct linger);
2240 if (!convert_sockopt(&level, &optname)) {
2241 SetLastError(WSAENOPROTOOPT);
2243 return SOCKET_ERROR;
2245 if (optname == SO_LINGER && optval) {
2246 /* yes, uses unsigned short in both win16/win32 */
2247 linger.l_onoff = ((UINT16*)optval)[0];
2248 linger.l_linger = ((UINT16*)optval)[1];
2249 /* FIXME: what is documented behavior if SO_LINGER optval
2251 optval = (char*)&linger;
2252 optlen = sizeof(struct linger);
2253 } else if (optlen < sizeof(int)){
2254 woptval= *((INT16 *) optval);
2255 optval= (char*) &woptval;
2259 if(optname == SO_RCVBUF && *(int*)optval < 2048) {
2260 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(int*)optval );
2265 if (setsockopt(fd, level, optname, optval, optlen) == 0)
2270 SetLastError(wsaErrno());
2273 else SetLastError(WSAENOTSOCK);
2274 return SOCKET_ERROR;
2277 /***********************************************************************
2278 * setsockopt (WINSOCK.21)
2280 INT16 WINAPI WINSOCK_setsockopt16(SOCKET16 s, INT16 level, INT16 optname,
2281 char *optval, INT16 optlen)
2283 if( !optval ) return SOCKET_ERROR;
2284 return (INT16)WS_setsockopt( s, (UINT16)level, optname, optval, optlen );
2288 /***********************************************************************
2289 * shutdown (WS2_32.22)
2291 int WINAPI WS_shutdown(SOCKET s, int how)
2293 int fd = _get_sock_fd(s);
2295 TRACE("socket %04x, how %i\n", s, how );
2300 case 0: /* drop receives */
2301 _enable_event(s, 0, 0, FD_READ);
2307 case 1: /* drop sends */
2308 _enable_event(s, 0, 0, FD_WRITE);
2314 case 2: /* drop all */
2319 WSAAsyncSelect( s, 0, 0, 0 );
2323 if (shutdown(fd, how) == 0)
2327 _enable_event(s, 0, 0, FD_WINE_CONNECTED|FD_WINE_LISTENING);
2332 SetLastError(wsaErrno());
2335 else SetLastError(WSAENOTSOCK);
2336 return SOCKET_ERROR;
2339 /***********************************************************************
2340 * shutdown (WINSOCK.22)
2342 INT16 WINAPI WINSOCK_shutdown16(SOCKET16 s, INT16 how)
2344 return (INT16)WS_shutdown( s, how );
2348 /***********************************************************************
2349 * socket (WS2_32.23)
2351 SOCKET WINAPI WS_socket(int af, int type, int protocol)
2353 TRACE("af=%d type=%d protocol=%d\n", af, type, protocol);
2355 /* The Winsock2 specification states that socket() always opens sockets
2357 FIXME: is the SO_OPENTYPE behaviour correct? */
2358 return WSASocketA ( af, type, protocol, NULL, 0, (opentype ? 0 : WSA_FLAG_OVERLAPPED) );
2361 /***********************************************************************
2362 * socket (WINSOCK.23)
2364 SOCKET16 WINAPI WINSOCK_socket16(INT16 af, INT16 type, INT16 protocol)
2366 return (SOCKET16)WS_socket( af, type, protocol );
2370 /* ----------------------------------- DNS services
2372 * IMPORTANT: 16-bit API structures have SEGPTR pointers inside them.
2373 * Also, we have to use wsock32 stubs to convert structures and
2374 * error codes from Unix to WSA, hence there is no direct mapping in
2375 * the relay32/wsock32.spec.
2379 /***********************************************************************
2380 * __ws_gethostbyaddr
2382 static WIN_hostent* __ws_gethostbyaddr(const char *addr, int len, int type, int dup_flag)
2384 WIN_hostent *retval = NULL;
2386 struct hostent* host;
2387 #if HAVE_LINUX_GETHOSTBYNAME_R_6
2390 struct hostent hostentry;
2393 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2395 int res = gethostbyaddr_r(addr, len, type,
2396 &hostentry, extrabuf, ebufsize, &host, &locerr);
2397 if( res != ERANGE) break;
2399 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2401 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2403 EnterCriticalSection( &csWSgetXXXbyYYY );
2404 host = gethostbyaddr(addr, len, type);
2405 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2409 if( WS_dup_he(host, dup_flag) )
2412 SetLastError(WSAENOBUFS);
2414 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2415 HeapFree(GetProcessHeap(),0,extrabuf);
2417 LeaveCriticalSection( &csWSgetXXXbyYYY );
2422 /***********************************************************************
2423 * gethostbyaddr (WINSOCK.51)
2425 SEGPTR WINAPI WINSOCK_gethostbyaddr16(const char *addr, INT16 len, INT16 type)
2427 TRACE("ptr %p, len %d, type %d\n", addr, len, type);
2428 if (!__ws_gethostbyaddr( addr, len, type, WS_DUP_SEGPTR )) return 0;
2429 return he_buffer_seg;
2432 /***********************************************************************
2433 * gethostbyaddr (WS2_32.51)
2435 struct WS_hostent* WINAPI WS_gethostbyaddr(const char *addr, int len,
2438 TRACE("ptr %08x, len %d, type %d\n",
2439 (unsigned) addr, len, type);
2440 return __ws_gethostbyaddr(addr, len, type, WS_DUP_LINEAR);
2443 /***********************************************************************
2444 * __ws_gethostbyname
2446 static WIN_hostent * __ws_gethostbyname(const char *name, int dup_flag)
2448 WIN_hostent *retval = NULL;
2449 struct hostent* host;
2450 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2453 struct hostent hostentry;
2454 int locerr = ENOBUFS;
2456 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2458 int res = gethostbyname_r(name, &hostentry, extrabuf, ebufsize, &host, &locerr);
2459 if( res != ERANGE) break;
2461 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2463 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2465 EnterCriticalSection( &csWSgetXXXbyYYY );
2466 host = gethostbyname(name);
2467 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2471 if( WS_dup_he(host, dup_flag) )
2473 else SetLastError(WSAENOBUFS);
2475 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2476 HeapFree(GetProcessHeap(),0,extrabuf);
2478 LeaveCriticalSection( &csWSgetXXXbyYYY );
2483 /***********************************************************************
2484 * gethostbyname (WINSOCK.52)
2486 SEGPTR WINAPI WINSOCK_gethostbyname16(const char *name)
2488 TRACE( "%s\n", debugstr_a(name) );
2489 if (!__ws_gethostbyname( name, WS_DUP_SEGPTR )) return 0;
2490 return he_buffer_seg;
2493 /***********************************************************************
2494 * gethostbyname (WS2_32.52)
2496 struct WS_hostent* WINAPI WS_gethostbyname(const char* name)
2498 TRACE( "%s\n", debugstr_a(name) );
2499 return __ws_gethostbyname( name, WS_DUP_LINEAR );
2503 /***********************************************************************
2504 * __ws_getprotobyname
2506 static WIN_protoent* __ws_getprotobyname(const char *name, int dup_flag)
2508 WIN_protoent* retval = NULL;
2510 struct protoent* proto;
2511 EnterCriticalSection( &csWSgetXXXbyYYY );
2512 if( (proto = getprotobyname(name)) != NULL )
2514 if( WS_dup_pe(proto, dup_flag) )
2516 else SetLastError(WSAENOBUFS);
2519 MESSAGE("protocol %s not found; You might want to add "
2520 "this to /etc/protocols\n", debugstr_a(name) );
2521 SetLastError(WSANO_DATA);
2523 LeaveCriticalSection( &csWSgetXXXbyYYY );
2527 /***********************************************************************
2528 * getprotobyname (WINSOCK.53)
2530 SEGPTR WINAPI WINSOCK_getprotobyname16(const char *name)
2532 TRACE( "%s\n", debugstr_a(name) );
2533 if (!__ws_getprotobyname(name, WS_DUP_SEGPTR)) return 0;
2534 return pe_buffer_seg;
2537 /***********************************************************************
2538 * getprotobyname (WS2_32.53)
2540 struct WS_protoent* WINAPI WS_getprotobyname(const char* name)
2542 TRACE( "%s\n", debugstr_a(name) );
2543 return __ws_getprotobyname(name, WS_DUP_LINEAR);
2547 /***********************************************************************
2548 * __ws_getprotobynumber
2550 static WIN_protoent* __ws_getprotobynumber(int number, int dup_flag)
2552 WIN_protoent* retval = NULL;
2553 struct protoent* proto;
2554 EnterCriticalSection( &csWSgetXXXbyYYY );
2555 if( (proto = getprotobynumber(number)) != NULL )
2557 if( WS_dup_pe(proto, dup_flag) )
2559 else SetLastError(WSAENOBUFS);
2562 MESSAGE("protocol number %d not found; You might want to add "
2563 "this to /etc/protocols\n", number );
2564 SetLastError(WSANO_DATA);
2566 LeaveCriticalSection( &csWSgetXXXbyYYY );
2570 /***********************************************************************
2571 * getprotobynumber (WINSOCK.54)
2573 SEGPTR WINAPI WINSOCK_getprotobynumber16(INT16 number)
2575 TRACE("%i\n", number);
2576 if (!__ws_getprotobynumber(number, WS_DUP_SEGPTR)) return 0;
2577 return pe_buffer_seg;
2580 /***********************************************************************
2581 * getprotobynumber (WS2_32.54)
2583 struct WS_protoent* WINAPI WS_getprotobynumber(int number)
2585 TRACE("%i\n", number);
2586 return __ws_getprotobynumber(number, WS_DUP_LINEAR);
2590 /***********************************************************************
2591 * __ws_getservbyname
2593 static WIN_servent* __ws_getservbyname(const char *name, const char *proto, int dup_flag)
2595 WIN_servent* retval = NULL;
2596 struct servent* serv;
2597 int i = wsi_strtolo( name, proto );
2600 EnterCriticalSection( &csWSgetXXXbyYYY );
2601 serv = getservbyname(local_buffer,
2602 proto ? (local_buffer + i) : NULL);
2605 if( WS_dup_se(serv, dup_flag) )
2607 else SetLastError(WSAENOBUFS);
2610 MESSAGE("service %s protocol %s not found; You might want to add "
2611 "this to /etc/services\n", debugstr_a(local_buffer),
2612 proto ? debugstr_a(local_buffer+i):"*");
2613 SetLastError(WSANO_DATA);
2615 LeaveCriticalSection( &csWSgetXXXbyYYY );
2617 else SetLastError(WSAENOBUFS);
2621 /***********************************************************************
2622 * getservbyname (WINSOCK.55)
2624 SEGPTR WINAPI WINSOCK_getservbyname16(const char *name, const char *proto)
2626 TRACE( "%s, %s\n", debugstr_a(name), debugstr_a(proto) );
2627 if (!__ws_getservbyname(name, proto, WS_DUP_SEGPTR)) return 0;
2628 return se_buffer_seg;
2631 /***********************************************************************
2632 * getservbyname (WS2_32.55)
2634 struct WS_servent* WINAPI WS_getservbyname(const char *name, const char *proto)
2636 TRACE( "%s, %s\n", debugstr_a(name), debugstr_a(proto) );
2637 return __ws_getservbyname(name, proto, WS_DUP_LINEAR);
2641 /***********************************************************************
2642 * __ws_getservbyport
2644 static WIN_servent* __ws_getservbyport(int port, const char* proto, int dup_flag)
2646 WIN_servent* retval = NULL;
2647 struct servent* serv;
2648 if (!proto || wsi_strtolo( proto, NULL )) {
2649 EnterCriticalSection( &csWSgetXXXbyYYY );
2650 if( (serv = getservbyport(port, (proto) ? local_buffer : NULL)) != NULL ) {
2651 if( WS_dup_se(serv, dup_flag) )
2653 else SetLastError(WSAENOBUFS);
2656 MESSAGE("service on port %lu protocol %s not found; You might want to add "
2657 "this to /etc/services\n", (unsigned long)ntohl(port),
2658 proto ? debugstr_a(local_buffer) : "*");
2659 SetLastError(WSANO_DATA);
2661 LeaveCriticalSection( &csWSgetXXXbyYYY );
2663 else SetLastError(WSAENOBUFS);
2667 /***********************************************************************
2668 * getservbyport (WINSOCK.56)
2670 SEGPTR WINAPI WINSOCK_getservbyport16(INT16 port, const char *proto)
2672 TRACE("%d (i.e. port %d), %s\n", (int)port, (int)ntohl(port), debugstr_a(proto));
2673 if (!__ws_getservbyport(port, proto, WS_DUP_SEGPTR)) return 0;
2674 return se_buffer_seg;
2677 /***********************************************************************
2678 * getservbyport (WS2_32.56)
2680 struct WS_servent* WINAPI WS_getservbyport(int port, const char *proto)
2682 TRACE("%d (i.e. port %d), %s\n", (int)port, (int)ntohl(port), debugstr_a(proto));
2683 return __ws_getservbyport(port, proto, WS_DUP_LINEAR);
2687 /***********************************************************************
2688 * gethostname (WS2_32.57)
2690 int WINAPI WS_gethostname(char *name, int namelen)
2692 TRACE("name %p, len %d\n", name, namelen);
2694 if (gethostname(name, namelen) == 0)
2696 TRACE("<- '%s'\n", name);
2699 SetLastError((errno == EINVAL) ? WSAEFAULT : wsaErrno());
2700 TRACE("<- ERROR !\n");
2701 return SOCKET_ERROR;
2704 /***********************************************************************
2705 * gethostname (WINSOCK.57)
2707 INT16 WINAPI WINSOCK_gethostname16(char *name, INT16 namelen)
2709 return (INT16)WS_gethostname(name, namelen);
2713 /* ------------------------------------- Windows sockets extensions -- *
2715 * ------------------------------------------------------------------- */
2717 /***********************************************************************
2718 * WSAEnumNetworkEvents (WS2_32.36)
2720 int WINAPI WSAEnumNetworkEvents(SOCKET s, WSAEVENT hEvent, LPWSANETWORKEVENTS lpEvent)
2724 TRACE("%08x, hEvent %08x, lpEvent %08x\n", s, hEvent, (unsigned)lpEvent );
2726 SERVER_START_REQ( get_socket_event )
2729 req->service = TRUE;
2731 req->c_event = hEvent;
2732 wine_server_set_reply( req, lpEvent->iErrorCode, sizeof(lpEvent->iErrorCode) );
2733 if (!(ret = wine_server_call(req))) lpEvent->lNetworkEvents = reply->pmask & reply->mask;
2737 SetLastError(WSAEINVAL);
2738 return SOCKET_ERROR;
2741 /***********************************************************************
2742 * WSAEventSelect (WS2_32.39)
2744 int WINAPI WSAEventSelect(SOCKET s, WSAEVENT hEvent, LONG lEvent)
2748 TRACE("%08x, hEvent %08x, event %08x\n", s, hEvent, (unsigned)lEvent );
2750 SERVER_START_REQ( set_socket_event )
2754 req->event = hEvent;
2755 ret = wine_server_call( req );
2759 SetLastError(WSAEINVAL);
2760 return SOCKET_ERROR;
2763 /***********************************************************************
2764 * WINSOCK_DoAsyncSelect
2766 VOID CALLBACK WINSOCK_DoAsyncEvent( ULONG_PTR ptr )
2768 ws_select_info *info = (ws_select_info*)ptr;
2769 unsigned int i, j, pmask, orphan = FALSE;
2770 int errors[FD_MAX_EVENTS];
2772 TRACE("socket %08x, event %08x\n", info->sock, info->event);
2774 SERVER_START_REQ( get_socket_event )
2776 req->handle = info->sock;
2777 req->service = TRUE;
2778 req->s_event = info->event; /* <== avoid race conditions */
2779 req->c_event = info->event;
2780 wine_server_set_reply( req, errors, sizeof(errors) );
2781 set_error( wine_server_call(req) );
2782 pmask = reply->pmask;
2785 if ( (GetLastError() == WSAENOTSOCK) || (GetLastError() == WSAEINVAL) )
2787 /* orphaned event (socket closed or something) */
2788 pmask = FD_WINE_SERVEVENT;
2792 /* check for accepted sockets that needs to inherit WSAAsyncSelect */
2793 if (pmask & FD_WINE_SERVEVENT) {
2795 for (q=0; q<WS_ACCEPT_QUEUE; q++)
2796 if (accept_old[q] == info->sock) {
2797 /* there's only one service thread per process, no lock necessary */
2798 HANDLE as = accept_new[q];
2802 WSAAsyncSelect(as, info->hWnd, info->uMsg, info->lEvent);
2805 pmask &= ~FD_WINE_SERVEVENT;
2807 /* dispatch network events, but use the order in the event_bitorder
2810 for (i=0; i<FD_MAX_EVENTS; i++) {
2811 j = event_bitorder[i];
2812 if (pmask & (1<<j)) {
2813 TRACE("post: event bit %d, error %d\n", j, errors[j]);
2814 PostMessageA(info->hWnd, info->uMsg, info->sock,
2815 WSAMAKESELECTREPLY(1<<j, errors[j]));
2821 TRACE("orphaned event, self-destructing\n");
2822 /* SERVICE_Delete closes the event object */
2823 SERVICE_Delete( info->service );
2828 /***********************************************************************
2829 * WSAAsyncSelect (WS2_32.101)
2831 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, LONG lEvent)
2833 int fd = _get_sock_fd(s);
2835 TRACE("%04x, hWnd %04x, uMsg %08x, event %08x\n",
2836 (SOCKET16)s, (HWND16)hWnd, uMsg, (unsigned)lEvent );
2842 ws_select_info *info = (ws_select_info*)WS_ALLOC(sizeof(ws_select_info));
2845 HANDLE hObj = CreateEventA( NULL, TRUE, FALSE, NULL );
2852 info->lEvent = lEvent;
2853 info->service = SERVICE_AddObject( hObj, WINSOCK_DoAsyncEvent, (ULONG_PTR)info );
2855 err = WSAEventSelect( s, hObj, lEvent | FD_WINE_SERVEVENT );
2857 /* SERVICE_Delete closes the event object */
2858 SERVICE_Delete( info->service );
2863 return 0; /* success */
2865 else SetLastError(WSAENOBUFS);
2869 WSAEventSelect(s, 0, 0);
2873 else SetLastError(WSAEINVAL);
2874 return SOCKET_ERROR;
2877 /***********************************************************************
2878 * WSAAsyncSelect (WINSOCK.101)
2880 INT16 WINAPI WSAAsyncSelect16(SOCKET16 s, HWND16 hWnd, UINT16 wMsg, LONG lEvent)
2882 return (INT16)WSAAsyncSelect( s, hWnd, wMsg, lEvent );
2885 /***********************************************************************
2886 * WSARecvEx (WINSOCK.1107)
2888 * See description for WSARecvEx()
2890 INT16 WINAPI WSARecvEx16(SOCKET16 s, char *buf, INT16 len, INT16 *flags)
2892 FIXME("(WSARecvEx16) partial packet return value not set \n");
2894 return WINSOCK_recv16(s, buf, len, *flags);
2898 /***********************************************************************
2899 * WSACreateEvent (WS2_32.31)
2902 WSAEVENT WINAPI WSACreateEvent(void)
2904 /* Create a manual-reset event, with initial state: unsignealed */
2907 return CreateEventA(NULL, TRUE, FALSE, NULL);
2910 /***********************************************************************
2911 * WSACloseEvent (WS2_32.29)
2914 BOOL WINAPI WSACloseEvent(WSAEVENT event)
2916 TRACE ("event=0x%x\n", event);
2918 return CloseHandle(event);
2921 /***********************************************************************
2922 * WSASocketA (WS2_32.78)
2925 SOCKET WINAPI WSASocketA(int af, int type, int protocol,
2926 LPWSAPROTOCOL_INFOA lpProtocolInfo,
2927 GROUP g, DWORD dwFlags)
2932 FIXME: The "advanced" parameters of WSASocketA (lpProtocolInfo,
2933 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
2936 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%lx\n",
2937 af, type, protocol, lpProtocolInfo, g, dwFlags );
2939 /* check the socket family */
2943 case WS_AF_IPX: af = AF_IPX;
2949 SetLastError(WSAEAFNOSUPPORT);
2950 return INVALID_SOCKET;
2953 /* check the socket type */
2956 case WS_SOCK_STREAM:
2966 SetLastError(WSAESOCKTNOSUPPORT);
2967 return INVALID_SOCKET;
2970 /* check the protocol type */
2971 if ( protocol < 0 ) /* don't support negative values */
2973 SetLastError(WSAEPROTONOSUPPORT);
2974 return INVALID_SOCKET;
2977 if ( af == AF_UNSPEC) /* did they not specify the address family? */
2981 if (type == SOCK_STREAM) { af = AF_INET; break; }
2983 if (type == SOCK_DGRAM) { af = AF_INET; break; }
2984 default: SetLastError(WSAEPROTOTYPE); return INVALID_SOCKET;
2987 SERVER_START_REQ( create_socket )
2991 req->protocol = protocol;
2992 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
2993 req->flags = dwFlags;
2994 req->inherit = TRUE;
2995 set_error( wine_server_call( req ) );
2996 ret = (SOCKET)reply->handle;
3001 TRACE("\tcreated %04x\n", ret );
3005 if (GetLastError() == WSAEACCES) /* raw socket denied */
3007 if (type == SOCK_RAW)
3008 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, will fail unless running as root\n");
3010 MESSAGE("WS_SOCKET: not enough privileges to create socket, try running as root\n");
3011 SetLastError(WSAESOCKTNOSUPPORT);
3014 WARN("\t\tfailed!\n");
3015 return INVALID_SOCKET;
3019 /***********************************************************************
3020 * __WSAFDIsSet (WINSOCK.151)
3022 INT16 WINAPI __WSAFDIsSet16(SOCKET16 s, ws_fd_set16 *set)
3024 int i = set->fd_count;
3026 TRACE("(%d,%8lx(%i))\n", s,(unsigned long)set, i);
3029 if (set->fd_array[i] == s) return 1;
3033 /***********************************************************************
3034 * __WSAFDIsSet (WS2_32.151)
3036 int WINAPI __WSAFDIsSet(SOCKET s, WS_fd_set *set)
3038 int i = set->fd_count;
3040 TRACE("(%d,%8lx(%i))\n", s,(unsigned long)set, i);
3043 if (set->fd_array[i] == s) return 1;
3047 /***********************************************************************
3048 * WSAIsBlocking (WINSOCK.114)
3049 * WSAIsBlocking (WS2_32.114)
3051 BOOL WINAPI WSAIsBlocking(void)
3053 /* By default WinSock should set all its sockets to non-blocking mode
3054 * and poll in PeekMessage loop when processing "blocking" ones. This
3055 * function is supposed to tell if the program is in this loop. Our
3056 * blocking calls are truly blocking so we always return FALSE.
3058 * Note: It is allowed to call this function without prior WSAStartup().
3065 /***********************************************************************
3066 * WSACancelBlockingCall (WINSOCK.113)
3067 * WSACancelBlockingCall (WS2_32.113)
3069 INT WINAPI WSACancelBlockingCall(void)
3076 /***********************************************************************
3077 * WSASetBlockingHook (WINSOCK.109)
3079 FARPROC16 WINAPI WSASetBlockingHook16(FARPROC16 lpBlockFunc)
3081 FARPROC16 prev = (FARPROC16)blocking_hook;
3082 blocking_hook = (FARPROC)lpBlockFunc;
3083 TRACE("hook %p\n", lpBlockFunc);
3088 /***********************************************************************
3089 * WSASetBlockingHook (WS2_32.109)
3091 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
3093 FARPROC prev = blocking_hook;
3094 blocking_hook = lpBlockFunc;
3095 TRACE("hook %p\n", lpBlockFunc);
3100 /***********************************************************************
3101 * WSAUnhookBlockingHook (WINSOCK.110)
3103 INT16 WINAPI WSAUnhookBlockingHook16(void)
3105 blocking_hook = NULL;
3110 /***********************************************************************
3111 * WSAUnhookBlockingHook (WS2_32.110)
3113 INT WINAPI WSAUnhookBlockingHook(void)
3115 blocking_hook = NULL;
3120 /* ----------------------------------- end of API stuff */
3122 /* ----------------------------------- helper functions -
3124 * TODO: Merge WS_dup_..() stuff into one function that
3125 * would operate with a generic structure containing internal
3126 * pointers (via a template of some kind).
3129 static int list_size(char** l, int item_size)
3134 j += (item_size) ? item_size : strlen(l[i]) + 1;
3135 j += (i + 1) * sizeof(char*); }
3139 static int list_dup(char** l_src, char* ref, char* base, int item_size)
3141 /* base is either either equal to ref or 0 or SEGPTR */
3144 char** l_to = (char**)ref;
3147 for(j=0;l_src[j];j++) ;
3148 p += (j + 1) * sizeof(char*);
3150 { l_to[i] = base + (p - ref);
3151 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
3152 memcpy(p, l_src[i], k); p += k; }
3159 static int hostent_size(struct hostent* p_he)
3163 { size = sizeof(struct hostent);
3164 size += strlen(p_he->h_name) + 1;
3165 size += list_size(p_he->h_aliases, 0);
3166 size += list_size(p_he->h_addr_list, p_he->h_length ); }
3170 /* duplicate hostent entry
3171 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
3172 * Dito for protoent and servent.
3174 static int WS_dup_he(struct hostent* p_he, int flag)
3176 /* Convert hostent structure into ws_hostent so that the data fits
3177 * into local_buffer. Internal pointers can be linear, SEGPTR, or
3178 * relative to local_buffer depending on "flag" value. Returns size
3179 * of the data copied.
3182 int size = hostent_size(p_he);
3185 char *p_name,*p_aliases,*p_addr,*p_base,*p;
3187 struct ws_hostent16 *p_to16;
3188 struct WS_hostent *p_to32;
3190 check_buffer_he(size);
3196 p_base = (flag & WS_DUP_SEGPTR) ? (char*)he_buffer_seg : he_buffer;
3197 p += (flag & WS_DUP_SEGPTR) ?
3198 sizeof(struct ws_hostent16) : sizeof(struct WS_hostent);
3200 strcpy(p, p_he->h_name); p += strlen(p) + 1;
3202 p += list_dup(p_he->h_aliases, p, p_base + (p - p_to), 0);
3204 list_dup(p_he->h_addr_list, p, p_base + (p - p_to), p_he->h_length);
3206 if (flag & WS_DUP_SEGPTR) /* Win16 */
3208 p_to16->h_addrtype = (INT16)p_he->h_addrtype;
3209 p_to16->h_length = (INT16)p_he->h_length;
3210 p_to16->h_name = (SEGPTR)(p_base + (p_name - p_to));
3211 p_to16->h_aliases = (SEGPTR)(p_base + (p_aliases - p_to));
3212 p_to16->h_addr_list = (SEGPTR)(p_base + (p_addr - p_to));
3213 size += (sizeof(struct ws_hostent16) - sizeof(struct hostent));
3217 p_to32->h_addrtype = p_he->h_addrtype;
3218 p_to32->h_length = p_he->h_length;
3219 p_to32->h_name = (p_base + (p_name - p_to));
3220 p_to32->h_aliases = (char **)(p_base + (p_aliases - p_to));
3221 p_to32->h_addr_list = (char **)(p_base + (p_addr - p_to));
3222 size += (sizeof(struct WS_hostent) - sizeof(struct hostent));
3228 /* ----- protoent */
3230 static int protoent_size(struct protoent* p_pe)
3234 { size = sizeof(struct protoent);
3235 size += strlen(p_pe->p_name) + 1;
3236 size += list_size(p_pe->p_aliases, 0); }
3240 static int WS_dup_pe(struct protoent* p_pe, int flag)
3242 int size = protoent_size(p_pe);
3246 struct ws_protoent16 *p_to16;
3247 struct WS_protoent *p_to32;
3248 char *p_name,*p_aliases,*p_base,*p;
3250 check_buffer_pe(size);
3255 p_base = (flag & WS_DUP_SEGPTR) ? (char*)pe_buffer_seg : pe_buffer;
3256 p += (flag & WS_DUP_SEGPTR) ?
3257 sizeof(struct ws_protoent16) : sizeof(struct WS_protoent);
3259 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
3261 list_dup(p_pe->p_aliases, p, p_base + (p - p_to), 0);
3263 if (flag & WS_DUP_SEGPTR) /* Win16 */
3265 p_to16->p_proto = (INT16)p_pe->p_proto;
3266 p_to16->p_name = (SEGPTR)(p_base) + (p_name - p_to);
3267 p_to16->p_aliases = (SEGPTR)((p_base) + (p_aliases - p_to));
3268 size += (sizeof(struct ws_protoent16) - sizeof(struct protoent));
3272 p_to32->p_proto = p_pe->p_proto;
3273 p_to32->p_name = (p_base) + (p_name - p_to);
3274 p_to32->p_aliases = (char **)((p_base) + (p_aliases - p_to));
3275 size += (sizeof(struct WS_protoent) - sizeof(struct protoent));
3283 static int servent_size(struct servent* p_se)
3287 { size += sizeof(struct servent);
3288 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
3289 size += list_size(p_se->s_aliases, 0); }
3293 static int WS_dup_se(struct servent* p_se, int flag)
3295 int size = servent_size(p_se);
3298 char *p_name,*p_aliases,*p_proto,*p_base,*p;
3300 struct ws_servent16 *p_to16;
3301 struct WS_servent *p_to32;
3303 check_buffer_se(size);
3308 p_base = (flag & WS_DUP_SEGPTR) ? (char*)se_buffer_seg : se_buffer;
3309 p += (flag & WS_DUP_SEGPTR) ?
3310 sizeof(struct ws_servent16) : sizeof(struct WS_servent);
3312 strcpy(p, p_se->s_name); p += strlen(p) + 1;
3314 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
3316 list_dup(p_se->s_aliases, p, p_base + (p - p_to), 0);
3318 if (flag & WS_DUP_SEGPTR) /* Win16 */
3320 p_to16->s_port = (INT16)p_se->s_port;
3321 p_to16->s_name = (SEGPTR)(p_base + (p_name - p_to));
3322 p_to16->s_proto = (SEGPTR)(p_base + (p_proto - p_to));
3323 p_to16->s_aliases = (SEGPTR)(p_base + (p_aliases - p_to));
3324 size += (sizeof(struct ws_servent16) - sizeof(struct servent));
3328 p_to32->s_port = p_se->s_port;
3329 p_to32->s_name = (p_base + (p_name - p_to));
3330 p_to32->s_proto = (p_base + (p_proto - p_to));
3331 p_to32->s_aliases = (char **)(p_base + (p_aliases - p_to));
3332 size += (sizeof(struct WS_servent) - sizeof(struct servent));
3338 /* ----------------------------------- error handling */
3340 UINT16 wsaErrno(void)
3342 int loc_errno = errno;
3343 WARN("errno %d, (%s).\n", loc_errno, strerror(loc_errno));
3347 case EINTR: return WSAEINTR;
3348 case EBADF: return WSAEBADF;
3350 case EACCES: return WSAEACCES;
3351 case EFAULT: return WSAEFAULT;
3352 case EINVAL: return WSAEINVAL;
3353 case EMFILE: return WSAEMFILE;
3354 case EWOULDBLOCK: return WSAEWOULDBLOCK;
3355 case EINPROGRESS: return WSAEINPROGRESS;
3356 case EALREADY: return WSAEALREADY;
3357 case ENOTSOCK: return WSAENOTSOCK;
3358 case EDESTADDRREQ: return WSAEDESTADDRREQ;
3359 case EMSGSIZE: return WSAEMSGSIZE;
3360 case EPROTOTYPE: return WSAEPROTOTYPE;
3361 case ENOPROTOOPT: return WSAENOPROTOOPT;
3362 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
3363 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
3364 case EOPNOTSUPP: return WSAEOPNOTSUPP;
3365 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
3366 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
3367 case EADDRINUSE: return WSAEADDRINUSE;
3368 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
3369 case ENETDOWN: return WSAENETDOWN;
3370 case ENETUNREACH: return WSAENETUNREACH;
3371 case ENETRESET: return WSAENETRESET;
3372 case ECONNABORTED: return WSAECONNABORTED;
3374 case ECONNRESET: return WSAECONNRESET;
3375 case ENOBUFS: return WSAENOBUFS;
3376 case EISCONN: return WSAEISCONN;
3377 case ENOTCONN: return WSAENOTCONN;
3378 case ESHUTDOWN: return WSAESHUTDOWN;
3379 case ETOOMANYREFS: return WSAETOOMANYREFS;
3380 case ETIMEDOUT: return WSAETIMEDOUT;
3381 case ECONNREFUSED: return WSAECONNREFUSED;
3382 case ELOOP: return WSAELOOP;
3383 case ENAMETOOLONG: return WSAENAMETOOLONG;
3384 case EHOSTDOWN: return WSAEHOSTDOWN;
3385 case EHOSTUNREACH: return WSAEHOSTUNREACH;
3386 case ENOTEMPTY: return WSAENOTEMPTY;
3388 case EPROCLIM: return WSAEPROCLIM;
3391 case EUSERS: return WSAEUSERS;
3394 case EDQUOT: return WSAEDQUOT;
3397 case ESTALE: return WSAESTALE;
3400 case EREMOTE: return WSAEREMOTE;
3403 /* just in case we ever get here and there are no problems */
3406 WARN("Unknown errno %d!\n", loc_errno);
3407 return WSAEOPNOTSUPP;
3411 UINT16 wsaHerrno(int loc_errno)
3414 WARN("h_errno %d.\n", loc_errno);
3418 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
3419 case TRY_AGAIN: return WSATRY_AGAIN;
3420 case NO_RECOVERY: return WSANO_RECOVERY;
3421 case NO_DATA: return WSANO_DATA;
3422 case ENOBUFS: return WSAENOBUFS;
3426 WARN("Unknown h_errno %d!\n", loc_errno);
3427 return WSAEOPNOTSUPP;
3432 /***********************************************************************
3433 * WSARecvFrom (WS2_32.69)
3435 INT WINAPI WSARecvFrom( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
3436 LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, struct WS_sockaddr *lpFrom,
3437 LPINT lpFromlen, LPWSAOVERLAPPED lpOverlapped,
3438 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
3443 FIXME( "(%i,%p,%lu,%p,%p,%p,%p,%p,%p: stub\n",
3444 s, lpBuffers, dwBufferCount, lpNumberOfBytesRecvd, lpFlags,
3445 lpFrom, lpFromlen, lpOverlapped, lpCompletionRoutine );
3447 for( dwCount = 0, rc = 0; dwCount < dwBufferCount; dwCount++ )
3450 if( ( rc = WS_recvfrom(s, lpBuffers[ dwCount ].buf, (INT)lpBuffers[ dwCount ].len,
3451 (INT)*lpFlags, lpFrom, lpFromlen ) ) != 0 )
3462 /***********************************************************************
3463 * WSCInstallProvider (WS2_32.88)
3465 INT WINAPI WSCInstallProvider( const LPGUID lpProviderId,
3466 LPCWSTR lpszProviderDllPath,
3467 const LPWSAPROTOCOL_INFOW lpProtocolInfoList,
3468 DWORD dwNumberOfEntries,
3471 FIXME("(%s, %s, %p, %ld, %p): stub !\n", debugstr_guid(lpProviderId),
3472 debugstr_w(lpszProviderDllPath), lpProtocolInfoList,
3473 dwNumberOfEntries, lpErrno);
3479 /***********************************************************************
3480 * WSCDeinstallProvider (WS2_32.83)
3482 INT WINAPI WSCDeinstallProvider(LPGUID lpProviderId, LPINT lpErrno)
3484 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId), lpErrno);
3490 /***********************************************************************
3491 * WSAAccept (WS2_32.26)
3493 SOCKET WINAPI WSAAccept( SOCKET s, struct WS_sockaddr *addr, LPINT addrlen,
3494 LPCONDITIONPROC lpfnCondition, DWORD dwCallbackData)
3497 int ret = 0, size = 0;
3498 WSABUF CallerId, CallerData, CalleeId, CalleeData;
3499 /* QOS SQOS, GQOS; */
3502 SOCKADDR src_addr, dst_addr;
3504 TRACE("Socket %ui, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackD ata %ld\n",
3505 s, addr, addrlen, lpfnCondition, dwCallbackData);
3508 size = sizeof(src_addr);
3509 cs = WS_accept(s, &src_addr, &size);
3511 if (cs == SOCKET_ERROR) return SOCKET_ERROR;
3513 CallerId.buf = (char *)&src_addr;
3514 CallerId.len = sizeof(src_addr);
3516 CallerData.buf = NULL;
3517 CallerData.len = (ULONG)NULL;
3519 WS_getsockname(cs, &dst_addr, &size);
3521 CalleeId.buf = (char *)&dst_addr;
3522 CalleeId.len = sizeof(dst_addr);
3525 ret = (*lpfnCondition)(&CallerId, &CallerData, NULL, NULL,
3526 &CalleeId, &CalleeData, &g, dwCallbackData);
3531 if (addr && addrlen)
3532 addr = memcpy(addr, &src_addr, (*addrlen > size) ? size : *addrlen );
3535 SetLastError(WSATRY_AGAIN);
3536 return SOCKET_ERROR;
3539 SetLastError(WSAECONNREFUSED);
3540 return SOCKET_ERROR;
3542 FIXME("Unknown return type from Condition function\n");
3543 SetLastError(WSAENOTSOCK);
3544 return SOCKET_ERROR;
3547 SetLastError(WSAENOTSOCK);
3548 return SOCKET_ERROR;