2 * based on Windows Sockets 1.1 specs
3 * (ftp.microsoft.com:/Advsys/winsock/spec11/WINSOCK.TXT)
5 * (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
7 * NOTE: If you make any changes to fix a particular app, make sure
8 * they don't break something else like Netscape or telnet and ftp
9 * clients and servers (www.winsite.com got a lot of those).
13 #include "wine/port.h"
17 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #ifdef HAVE_SYS_FILIO_H
23 # include <sys/filio.h>
25 #ifdef HAVE_SYS_SOCKIO_H
26 # include <sys/sockio.h>
30 # include <sys/so_ioctl.h>
33 #ifdef HAVE_SYS_PARAM_H
34 # include <sys/param.h>
40 #ifdef HAVE_SYS_WAIT_H
41 # include <sys/wait.h>
43 #ifdef HAVE_SYS_SOCKET_H
44 #include <sys/socket.h>
46 #ifdef HAVE_NETINET_IN_H
47 # include <netinet/in.h>
49 #ifdef HAVE_NETINET_TCP_H
50 # include <netinet/tcp.h>
52 #ifdef HAVE_ARPA_INET_H
53 # include <arpa/inet.h>
58 #ifdef HAVE_SYS_ERRNO_H
59 #include <sys/errno.h>
66 #ifdef HAVE_ARPA_NAMESER_H
67 # include <arpa/nameser.h>
76 # include <netipx/ipx.h>
80 # include <asm/types.h>
81 # include <linux/ipx.h>
85 #include "wine/winbase16.h"
91 #include "wine/winsock16.h"
94 #include "wine/server.h"
96 #include "debugtools.h"
99 DEFAULT_DEBUG_CHANNEL(winsock);
101 /* critical section to protect some non-rentrant net function */
102 extern CRITICAL_SECTION csWSgetXXXbyYYY;
104 #define DEBUG_SOCKADDR 0
105 #define dump_sockaddr(a) \
106 DPRINTF("sockaddr_in: family %d, address %s, port %d\n", \
107 ((struct sockaddr_in *)a)->sin_family, \
108 inet_ntoa(((struct sockaddr_in *)a)->sin_addr), \
109 ntohs(((struct sockaddr_in *)a)->sin_port))
111 /* ----------------------------------- internal data */
113 /* ws_... struct conversion flags */
115 #define WS_DUP_LINEAR 0x0001
116 #define WS_DUP_SEGPTR 0x0002 /* internal pointers are SEGPTRs */
117 /* by default, internal pointers are linear */
118 typedef struct /* WSAAsyncSelect() control struct */
120 HANDLE service, event, sock;
126 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
127 #define WS_MAX_UDP_DATAGRAM 1024
129 #define WS_ACCEPT_QUEUE 6
131 #define PROCFS_NETDEV_FILE "/proc/net/dev" /* Points to the file in the /proc fs
132 that lists the network devices.
133 Do we need an #ifdef LINUX for this? */
135 static volatile HANDLE accept_old[WS_ACCEPT_QUEUE], accept_new[WS_ACCEPT_QUEUE];
137 static void *he_buffer; /* typecast for Win16/32 ws_hostent */
138 static SEGPTR he_buffer_seg;
139 static void *se_buffer; /* typecast for Win16/32 ws_servent */
140 static SEGPTR se_buffer_seg;
141 static void *pe_buffer; /* typecast for Win16/32 ws_protoent */
142 static SEGPTR pe_buffer_seg;
143 static char* local_buffer;
144 static SEGPTR dbuffer_seg;
145 static INT num_startup; /* reference counter */
146 static FARPROC blocking_hook;
148 /* function prototypes */
149 static int WS_dup_he(struct hostent* p_he, int flag);
150 static int WS_dup_pe(struct protoent* p_pe, int flag);
151 static int WS_dup_se(struct servent* p_se, int flag);
153 typedef void WIN_hostent;
154 typedef void WIN_protoent;
155 typedef void WIN_servent;
157 int WSAIOCTL_GetInterfaceCount(void);
158 int WSAIOCTL_GetInterfaceName(int intNumber, char *intName);
160 UINT16 wsaErrno(void);
161 UINT16 wsaHerrno(int errnr);
163 static HANDLE _WSHeap = 0;
165 #define WS_ALLOC(size) \
166 HeapAlloc(_WSHeap, HEAP_ZERO_MEMORY, (size) )
167 #define WS_FREE(ptr) \
168 HeapFree(_WSHeap, 0, (ptr) )
170 static INT _ws_sock_ops[] =
171 { WS_SO_DEBUG, WS_SO_REUSEADDR, WS_SO_KEEPALIVE, WS_SO_DONTROUTE,
172 WS_SO_BROADCAST, WS_SO_LINGER, WS_SO_OOBINLINE, WS_SO_SNDBUF,
173 WS_SO_RCVBUF, WS_SO_ERROR, WS_SO_TYPE,
181 static int _px_sock_ops[] =
182 { SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST,
183 SO_LINGER, SO_OOBINLINE, SO_SNDBUF, SO_RCVBUF, SO_ERROR, SO_TYPE,
192 static INT _ws_tcp_ops[] = {
198 static int _px_tcp_ops[] = {
205 /* Holds value of SO_OPENTYPE socket option. This is essentially a global
206 * variable that Windows uses to affect how new sockets are created. See
207 * <http://support.microsoft.com/default.aspx?scid=kb;EN-US;q181611>. Right
208 * now, Wine does not do anything with this value other than get and set it on
211 static int opentype = 0;
213 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which we post
214 * messages if there are multiple events. Used in WINSOCK_DoAsyncEvent. The
215 * problem is if there is both a FD_CONNECT event and, say, an FD_READ event
216 * available on the same socket, we want to notify the app of the connect event
217 * first. Otherwise it may discard the read event because it thinks it hasn't
220 static const int event_bitorder[FD_MAX_EVENTS] = {
227 6, 7, 8, 9 /* leftovers */
230 /* set last error code from NT status without mapping WSA errors */
231 inline static unsigned int set_error( unsigned int err )
235 /* do not map WSA errors */
236 if ((err < WSABASEERR) || (err >= 0x10000000)) err = RtlNtStatusToDosError(err);
242 static char* check_buffer(int size);
244 static int _get_sock_fd(SOCKET s)
246 int fd = FILE_GetUnixHandle( s, GENERIC_READ );
248 FIXME("handle %d is not a socket (GLE %ld)\n",s,GetLastError());
252 static void _enable_event(SOCKET s, unsigned int event,
253 unsigned int sstate, unsigned int cstate)
255 SERVER_START_REQ( enable_socket_event )
259 req->sstate = sstate;
260 req->cstate = cstate;
261 wine_server_call( req );
266 static int _is_blocking(SOCKET s)
269 SERVER_START_REQ( get_socket_event )
272 req->service = FALSE;
275 wine_server_call( req );
276 ret = (reply->state & FD_WINE_NONBLOCKING) == 0;
282 static unsigned int _get_sock_mask(SOCKET s)
285 SERVER_START_REQ( get_socket_event )
288 req->service = FALSE;
291 wine_server_call( req );
298 static void _sync_sock_state(SOCKET s)
300 /* do a dummy wineserver request in order to let
301 the wineserver run through its select loop once */
302 (void)_is_blocking(s);
305 static int _get_sock_error(SOCKET s, unsigned int bit)
307 int events[FD_MAX_EVENTS];
309 SERVER_START_REQ( get_socket_event )
312 req->service = FALSE;
315 wine_server_set_reply( req, events, sizeof(events) );
316 wine_server_call( req );
322 static void WINSOCK_DeleteIData(void)
324 /* delete scratch buffers */
326 UnMapLS( he_buffer_seg );
327 UnMapLS( se_buffer_seg );
328 UnMapLS( pe_buffer_seg );
329 UnMapLS( dbuffer_seg );
330 if (he_buffer) HeapFree( GetProcessHeap(), 0, he_buffer );
331 if (se_buffer) HeapFree( GetProcessHeap(), 0, se_buffer );
332 if (pe_buffer) HeapFree( GetProcessHeap(), 0, pe_buffer );
333 if (local_buffer) HeapFree( GetProcessHeap(), 0, local_buffer );
345 /***********************************************************************
346 * WS_LibMain (WS2_32.init)
348 BOOL WINAPI WS_LibMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
350 TRACE("0x%x 0x%lx %p\n", hInstDLL, fdwReason, fImpLoad);
352 case DLL_PROCESS_DETACH:
353 WINSOCK_DeleteIData();
359 /***********************************************************************
362 * Converts socket flags from Windows format.
363 * Return 1 if converted, 0 if not (error).
365 static int convert_sockopt(INT *level, INT *optname)
372 for(i=0; _ws_sock_ops[i]; i++)
373 if( _ws_sock_ops[i] == *optname ) break;
374 if( _ws_sock_ops[i] ) {
375 *optname = _px_sock_ops[i];
378 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname);
381 *level = IPPROTO_TCP;
382 for(i=0; _ws_tcp_ops[i]; i++)
383 if ( _ws_tcp_ops[i] == *optname ) break;
384 if( _ws_tcp_ops[i] ) {
385 *optname = _px_tcp_ops[i];
388 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname);
394 /* ----------------------------------- Per-thread info (or per-process?) */
396 static int wsi_strtolo(const char* name, const char* opt)
398 /* Stuff a lowercase copy of the string into the local buffer */
400 int i = strlen(name) + 2;
401 char* p = check_buffer(i + ((opt)?strlen(opt):0));
405 do *p++ = tolower(*name); while(*name++);
406 i = (p - local_buffer);
407 if( opt ) do *p++ = tolower(*opt); while(*opt++);
413 static fd_set* fd_set_import( fd_set* fds, void* wsfds, int* highfd, int lfd[], BOOL b32 )
415 /* translate Winsock fd set into local fd set */
419 #define wsfds16 ((ws_fd_set16*)wsfds)
420 #define wsfds32 ((WS_fd_set*)wsfds)
424 count = b32 ? wsfds32->fd_count : wsfds16->fd_count;
426 for( i = 0; i < count; i++ )
428 int s = (b32) ? wsfds32->fd_array[i]
429 : wsfds16->fd_array[i];
430 int fd = _get_sock_fd(s);
434 if( fd > *highfd ) *highfd = fd;
446 inline static int sock_error_p(int s)
448 unsigned int optval, optlen;
450 optlen = sizeof(optval);
451 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
452 if (optval) WARN("\t[%i] error: %d\n", s, optval);
456 static int fd_set_export( fd_set* fds, fd_set* exceptfds, void* wsfds, int lfd[], BOOL b32 )
460 /* translate local fd set into Winsock fd set, adding
461 * errors to exceptfds (only if app requested it) */
465 #define wsfds16 ((ws_fd_set16*)wsfds)
466 #define wsfds32 ((WS_fd_set*)wsfds)
467 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
469 for( i = 0, j = 0; i < count; i++ )
474 if( FD_ISSET(fd, fds) )
476 if ( exceptfds && sock_error_p(fd) )
478 FD_SET(fd, exceptfds);
482 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
484 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
491 if( b32 ) wsfds32->fd_count = j;
492 else wsfds16->fd_count = j;
501 static void fd_set_unimport( void* wsfds, int lfd[], BOOL b32 )
505 #define wsfds16 ((ws_fd_set16*)wsfds)
506 #define wsfds32 ((WS_fd_set*)wsfds)
507 int i, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
509 for( i = 0; i < count; i++ )
519 static int do_block( int fd, int mask )
530 i = select( fd+1, &fds[0], &fds[1], &fds[2], NULL );
531 if (i <= 0) return -1;
534 if (FD_ISSET(fd, &fds[i]))
539 void* __ws_memalloc( int size )
541 return WS_ALLOC(size);
544 void __ws_memfree(void* ptr)
550 /* ----------------------------------- API -----
552 * Init / cleanup / error checking.
555 /***********************************************************************
556 * WSAStartup (WINSOCK.115)
558 * Create socket control struct, attach it to the global list and
559 * update a pointer in the task struct.
561 INT16 WINAPI WSAStartup16(UINT16 wVersionRequested, LPWSADATA16 lpWSAData)
563 static const WSADATA16 data =
569 #elif defined(__NetBSD__)
573 #elif defined(__FreeBSD__)
575 #elif defined(__OpenBSD__)
580 WS_MAX_SOCKETS_PER_PROCESS,
585 TRACE("verReq=%x\n", wVersionRequested);
587 if (LOBYTE(wVersionRequested) < 1 || (LOBYTE(wVersionRequested) == 1 &&
588 HIBYTE(wVersionRequested) < 1)) return WSAVERNOTSUPPORTED;
590 if (!lpWSAData) return WSAEINVAL;
592 /* initialize socket heap */
596 _WSHeap = HeapCreate(HEAP_ZERO_MEMORY, 8120, 32768);
599 ERR("Fatal: failed to create WinSock heap\n");
603 if( _WSHeap == 0 ) return WSASYSNOTREADY;
607 /* return winsock information */
609 memcpy(lpWSAData, &data, sizeof(data));
611 TRACE("succeeded\n");
615 /***********************************************************************
616 * WSAStartup (WS2_32.115)
618 int WINAPI WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData)
620 static const WSADATA data =
626 #elif defined(__NetBSD__)
630 #elif defined(__FreeBSD__)
632 #elif defined(__OpenBSD__)
637 WS_MAX_SOCKETS_PER_PROCESS,
642 TRACE("verReq=%x\n", wVersionRequested);
644 if (LOBYTE(wVersionRequested) < 1)
645 return WSAVERNOTSUPPORTED;
647 if (!lpWSAData) return WSAEINVAL;
649 /* initialize socket heap */
653 _WSHeap = HeapCreate(HEAP_ZERO_MEMORY, 8120, 32768);
656 ERR("Fatal: failed to create WinSock heap\n");
660 if( _WSHeap == 0 ) return WSASYSNOTREADY;
664 /* return winsock information */
665 memcpy(lpWSAData, &data, sizeof(data));
667 /* that's the whole of the negotiation for now */
668 lpWSAData->wVersion = wVersionRequested;
670 TRACE("succeeded\n");
675 /***********************************************************************
676 * WSACleanup (WINSOCK.116)
677 * WSACleanup (WS2_32.116)
679 INT WINAPI WSACleanup(void)
683 if (--num_startup > 0) return 0;
684 WINSOCK_DeleteIData();
687 SetLastError(WSANOTINITIALISED);
692 /***********************************************************************
693 * WSAGetLastError (WINSOCK.111)
694 * WSAGetLastError (WS2_32.111)
696 INT WINAPI WSAGetLastError(void)
698 return GetLastError();
701 /***********************************************************************
702 * WSASetLastError (WS2_32.112)
704 void WINAPI WSASetLastError(INT iError) {
705 SetLastError(iError);
708 /***********************************************************************
709 * WSASetLastError (WINSOCK.112)
711 void WINAPI WSASetLastError16(INT16 iError)
713 WSASetLastError(iError);
716 static char* check_buffer(int size)
718 static int local_buflen;
722 if (local_buflen >= size ) return local_buffer;
723 HeapFree( GetProcessHeap(), 0, local_buffer );
725 local_buffer = HeapAlloc( GetProcessHeap(), 0, (local_buflen = size) );
729 static struct ws_hostent* check_buffer_he(int size)
734 if (he_len >= size ) return he_buffer;
735 UnMapLS( he_buffer_seg );
736 HeapFree( GetProcessHeap(), 0, he_buffer );
738 he_buffer = HeapAlloc( GetProcessHeap(), 0, (he_len = size) );
739 he_buffer_seg = MapLS( he_buffer );
743 static void* check_buffer_se(int size)
748 if (se_len >= size ) return se_buffer;
749 UnMapLS( se_buffer_seg );
750 HeapFree( GetProcessHeap(), 0, se_buffer );
752 se_buffer = HeapAlloc( GetProcessHeap(), 0, (se_len = size) );
753 se_buffer_seg = MapLS( he_buffer );
757 static struct ws_protoent* check_buffer_pe(int size)
762 if (pe_len >= size ) return pe_buffer;
763 UnMapLS( pe_buffer_seg );
764 HeapFree( GetProcessHeap(), 0, pe_buffer );
766 pe_buffer = HeapAlloc( GetProcessHeap(), 0, (pe_len = size) );
767 pe_buffer_seg = MapLS( he_buffer );
771 /* ----------------------------------- i/o APIs */
774 #define SUPPORTED_PF(pf) ((pf)==AF_INET || (pf)== AF_IPX)
776 #define SUPPORTED_PF(pf) ((pf)==AF_INET)
779 static void ws2_async_accept(SOCKET s, SOCKET as)
782 /* queue socket for WSAAsyncSelect */
783 for (q=0; q<WS_ACCEPT_QUEUE; q++)
784 if (InterlockedCompareExchange((LONG *)&accept_old[q], s, 0) == 0)
786 if (q<WS_ACCEPT_QUEUE)
789 ERR("accept queue too small\n");
790 /* now signal our AsyncSelect handler */
791 _enable_event(s, FD_WINE_SERVEVENT, 0, 0);
794 /**********************************************************************/
796 /* Returns the converted address if successful, NULL if it was too small to
797 * start with. Note that the returned pointer may be the original pointer
798 * if no conversion is necessary.
800 const struct sockaddr* ws_sockaddr_ws2u(const struct WS_sockaddr* wsaddr, int wsaddrlen, int *uaddrlen)
802 switch (wsaddr->sa_family)
807 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
808 struct sockaddr_ipx* uipx;
810 if (wsaddrlen<sizeof(struct WS_sockaddr_ipx))
813 *uaddrlen=sizeof(struct sockaddr_ipx);
814 uipx=malloc(*uaddrlen);
815 uipx->sipx_family=AF_IPX;
816 uipx->sipx_port=wsipx->sa_socket;
817 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
820 memcpy(&uipx->sipx_network,wsipx->sa_netnum,sizeof(uipx->sipx_network)+sizeof(uipx->sipx_node));
821 uipx->sipx_type=IPX_FRAME_NONE;
823 return (const struct sockaddr*)uipx;
828 if (wsaddrlen<sizeof(struct WS_sockaddr))
831 /* No conversion needed, just return the original address */
833 return (const struct sockaddr*)wsaddr;
838 /* allocates a Unix sockaddr structure to receive the data */
839 inline struct sockaddr* ws_sockaddr_alloc(const struct WS_sockaddr* wsaddr, int* wsaddrlen, int* uaddrlen)
844 *uaddrlen=max(sizeof(struct sockaddr),*wsaddrlen);
848 return malloc(*uaddrlen);
851 /* Returns 0 if successful, -1 if the buffer is too small */
852 int ws_sockaddr_u2ws(const struct sockaddr* uaddr, int uaddrlen, struct WS_sockaddr* wsaddr, int* wsaddrlen)
856 switch(uaddr->sa_family)
861 struct sockaddr_ipx* uipx=(struct sockaddr_ipx*)uaddr;
862 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
865 switch (*wsaddrlen) /* how much can we copy? */
869 wsipx->sa_socket=uipx->sipx_port;
873 memcpy(wsipx->sa_nodenum,uipx->sipx_node,sizeof(wsipx->sa_nodenum));
881 memcpy(wsipx->sa_netnum,&uipx->sipx_network,sizeof(wsipx->sa_netnum));
887 wsipx->sa_family=AF_IPX;
898 /* No conversion needed */
899 memcpy(wsaddr,uaddr,*wsaddrlen);
900 res=(*wsaddrlen<uaddrlen?-1:0);
905 /* to be called to free the memory allocated by ws_sockaddr_ws2u or
908 inline void ws_sockaddr_free(const struct sockaddr* uaddr, const struct WS_sockaddr* wsaddr)
910 if (uaddr!=NULL && uaddr!=(const struct sockaddr*)wsaddr)
914 /***********************************************************************
917 SOCKET WINAPI WS_accept(SOCKET s, struct WS_sockaddr *addr,
920 int fd = _get_sock_fd(s);
922 TRACE("socket %04x\n", (UINT16)s );
930 _sync_sock_state(s); /* let wineserver notice connection */
931 /* retrieve any error codes from it */
932 SetLastError(_get_sock_error(s, FD_ACCEPT_BIT));
933 /* FIXME: care about the error? */
936 SERVER_START_REQ( accept_socket )
939 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
941 set_error( wine_server_call( req ) );
942 as = (SOCKET)reply->handle;
947 unsigned omask = _get_sock_mask( s );
948 WS_getpeername(as, addr, addrlen32);
949 if (omask & FD_WINE_SERVEVENT)
950 ws2_async_accept(s, as);
956 SetLastError(WSAENOTSOCK);
958 return INVALID_SOCKET;
961 /***********************************************************************
964 SOCKET16 WINAPI WINSOCK_accept16(SOCKET16 s, struct WS_sockaddr* addr,
967 INT addrlen32 = addrlen16 ? *addrlen16 : 0;
968 SOCKET retSocket = WS_accept( s, addr, &addrlen32 );
969 if( addrlen16 ) *addrlen16 = (INT16)addrlen32;
970 return (SOCKET16)retSocket;
973 /***********************************************************************
976 int WINAPI WS_bind(SOCKET s, const struct WS_sockaddr* name, int namelen)
978 int fd = _get_sock_fd(s);
981 TRACE("socket %04x, ptr %p, length %d\n", s, name, namelen);
989 if (!name || !SUPPORTED_PF(name->sa_family))
991 SetLastError(WSAEAFNOSUPPORT);
995 const struct sockaddr* uaddr;
998 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1001 SetLastError(WSAEFAULT);
1005 if (bind(fd, uaddr, uaddrlen) < 0)
1007 int loc_errno = errno;
1008 WARN("\tfailure - errno = %i\n", errno);
1013 SetLastError(WSAENOTSOCK);
1016 SetLastError(WSAEINVAL);
1019 SetLastError(wsaErrno());
1025 res=0; /* success */
1027 ws_sockaddr_free(uaddr,name);
1034 SetLastError(WSAENOTSOCK);
1039 /***********************************************************************
1042 INT16 WINAPI WINSOCK_bind16(SOCKET16 s, struct WS_sockaddr *name, INT16 namelen)
1044 return (INT16)WS_bind( s, name, namelen );
1047 /***********************************************************************
1048 * closesocket (WS2_32.3)
1050 int WINAPI WS_closesocket(SOCKET s)
1052 TRACE("socket %08x\n", s);
1053 if (CloseHandle(s)) return 0;
1054 return SOCKET_ERROR;
1057 /***********************************************************************
1058 * closesocket (WINSOCK.3)
1060 INT16 WINAPI WINSOCK_closesocket16(SOCKET16 s)
1062 return (INT16)WS_closesocket(s);
1065 /***********************************************************************
1066 * connect (WS2_32.4)
1068 int WINAPI WS_connect(SOCKET s, const struct WS_sockaddr* name, int namelen)
1070 int fd = _get_sock_fd(s);
1072 TRACE("socket %04x, ptr %p, length %d\n", s, name, namelen);
1074 dump_sockaddr(name);
1079 const struct sockaddr* uaddr;
1082 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1085 SetLastError(WSAEFAULT);
1091 rc=connect(fd, uaddr, uaddrlen);
1092 ws_sockaddr_free(uaddr,name);
1094 goto connect_success;
1097 if (errno == EINPROGRESS)
1099 /* tell wineserver that a connection is in progress */
1100 _enable_event(s, FD_CONNECT|FD_READ|FD_WRITE,
1101 FD_CONNECT|FD_READ|FD_WRITE,
1102 FD_WINE_CONNECTED|FD_WINE_LISTENING);
1103 if (_is_blocking(s))
1108 _sync_sock_state(s); /* let wineserver notice connection */
1109 /* retrieve any error codes from it */
1110 result = _get_sock_error(s, FD_CONNECT_BIT);
1112 SetLastError(result);
1115 goto connect_success;
1120 SetLastError(WSAEWOULDBLOCK);
1125 SetLastError(wsaErrno());
1131 SetLastError(WSAENOTSOCK);
1133 return SOCKET_ERROR;
1137 _enable_event(s, FD_CONNECT|FD_READ|FD_WRITE,
1138 FD_WINE_CONNECTED|FD_READ|FD_WRITE,
1139 FD_CONNECT|FD_WINE_LISTENING);
1143 /***********************************************************************
1144 * connect (WINSOCK.4)
1146 INT16 WINAPI WINSOCK_connect16(SOCKET16 s, struct WS_sockaddr *name, INT16 namelen)
1148 return (INT16)WS_connect( s, name, namelen );
1151 /***********************************************************************
1152 * getpeername (WS2_32.5)
1154 int WINAPI WS_getpeername(SOCKET s, struct WS_sockaddr *name, int *namelen)
1156 int fd = _get_sock_fd(s);
1159 TRACE("socket: %04x, ptr %p, len %8x\n", s, name, *namelen);
1164 struct sockaddr* uaddr;
1167 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1168 if (getpeername(fd, uaddr, &uaddrlen) != 0)
1170 SetLastError(wsaErrno());
1172 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1174 /* The buffer was too small */
1175 SetLastError(WSAEFAULT);
1181 ws_sockaddr_free(uaddr,name);
1186 SetLastError(WSAENOTSOCK);
1191 /***********************************************************************
1192 * getpeername (WINSOCK.5)
1194 INT16 WINAPI WINSOCK_getpeername16(SOCKET16 s, struct WS_sockaddr *name,
1197 INT namelen32 = *namelen16;
1198 INT retVal = WS_getpeername( s, name, &namelen32 );
1201 dump_sockaddr(name);
1204 *namelen16 = namelen32;
1205 return (INT16)retVal;
1208 /***********************************************************************
1209 * getsockname (WS2_32.6)
1211 int WINAPI WS_getsockname(SOCKET s, struct WS_sockaddr *name, int *namelen)
1213 int fd = _get_sock_fd(s);
1216 TRACE("socket: %04x, ptr %p, len %8x\n", s, name, *namelen);
1221 struct sockaddr* uaddr;
1224 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1225 if (getsockname(fd, uaddr, &uaddrlen) != 0)
1227 SetLastError(wsaErrno());
1229 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1231 /* The buffer was too small */
1232 SetLastError(WSAEFAULT);
1242 SetLastError(WSAENOTSOCK);
1247 /***********************************************************************
1248 * getsockname (WINSOCK.6)
1250 INT16 WINAPI WINSOCK_getsockname16(SOCKET16 s, struct WS_sockaddr *name,
1257 INT namelen32 = *namelen16;
1258 retVal = WS_getsockname( s, name, &namelen32 );
1259 *namelen16 = namelen32;
1262 dump_sockaddr(name);
1266 else retVal = SOCKET_ERROR;
1267 return (INT16)retVal;
1271 /***********************************************************************
1272 * getsockopt (WS2_32.7)
1274 INT WINAPI WS_getsockopt(SOCKET s, INT level,
1275 INT optname, char *optval, INT *optlen)
1279 TRACE("socket: %04x, level 0x%x, name 0x%x, ptr %8x, len %d\n", s, level,
1280 (int) optname, (int) optval, (int) *optlen);
1281 /* SO_OPENTYPE does not require a valid socket handle. */
1282 if (level == WS_SOL_SOCKET && optname == WS_SO_OPENTYPE)
1284 if (!optlen || *optlen < sizeof(int) || !optval)
1286 SetLastError(WSAEFAULT);
1287 return SOCKET_ERROR;
1289 *(int *)optval = opentype;
1290 *optlen = sizeof(int);
1291 TRACE("getting global SO_OPENTYPE = 0x%x\n", opentype);
1295 fd = _get_sock_fd(s);
1298 if (!convert_sockopt(&level, &optname)) {
1299 SetLastError(WSAENOPROTOOPT); /* Unknown option */
1301 if (getsockopt(fd, (int) level, optname, optval, optlen) == 0 )
1306 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1310 return SOCKET_ERROR;
1314 /***********************************************************************
1315 * getsockopt (WINSOCK.7)
1317 INT16 WINAPI WINSOCK_getsockopt16(SOCKET16 s, INT16 level,
1318 INT16 optname, char *optval, INT16 *optlen)
1323 if( optlen ) optlen32 = *optlen; else p = NULL;
1324 retVal = WS_getsockopt( s, (UINT16)level, optname, optval, p );
1325 if( optlen ) *optlen = optlen32;
1326 return (INT16)retVal;
1330 /***********************************************************************
1334 u_long WINAPI WS_htonl(u_long hostlong)
1336 return htonl(hostlong);
1340 /***********************************************************************
1344 u_short WINAPI WS_htons(u_short hostshort)
1346 return htons(hostshort);
1350 /***********************************************************************
1351 * inet_addr (WINSOCK.10)
1352 * inet_addr (WS2_32.11)
1354 u_long WINAPI WS_inet_addr(const char *cp)
1356 return inet_addr(cp);
1360 /***********************************************************************
1361 * ntohl (WINSOCK.14)
1364 u_long WINAPI WS_ntohl(u_long netlong)
1366 return ntohl(netlong);
1370 /***********************************************************************
1371 * ntohs (WINSOCK.15)
1374 u_short WINAPI WS_ntohs(u_short netshort)
1376 return ntohs(netshort);
1380 /***********************************************************************
1381 * inet_ntoa (WS2_32.12)
1383 char* WINAPI WS_inet_ntoa(struct WS_in_addr in)
1385 /* use "buffer for dummies" here because some applications have
1386 * propensity to decode addresses in ws_hostent structure without
1387 * saving them first...
1389 static char dbuffer[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
1391 char* s = inet_ntoa(*((struct in_addr*)&in));
1397 SetLastError(wsaErrno());
1401 /***********************************************************************
1402 * inet_ntoa (WINSOCK.11)
1404 SEGPTR WINAPI WINSOCK_inet_ntoa16(struct in_addr in)
1407 if (!(retVal = WS_inet_ntoa(*((struct WS_in_addr*)&in)))) return 0;
1408 if (!dbuffer_seg) dbuffer_seg = MapLS( retVal );
1413 /**********************************************************************
1414 * WSAIoctl (WS2_32.50)
1417 * FIXME: Only SIO_GET_INTERFACE_LIST option implemented.
1419 INT WINAPI WSAIoctl (SOCKET s,
1420 DWORD dwIoControlCode,
1423 LPVOID lpbOutBuffer,
1425 LPDWORD lpcbBytesReturned,
1426 LPWSAOVERLAPPED lpOverlapped,
1427 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
1429 int fd = _get_sock_fd(s);
1433 switch( dwIoControlCode )
1435 case SIO_GET_INTERFACE_LIST:
1437 INTERFACE_INFO* intArray = (INTERFACE_INFO*)lpbOutBuffer;
1439 struct ifreq ifInfo;
1443 TRACE ("-> SIO_GET_INTERFACE_LIST request\n");
1445 numInt = WSAIOCTL_GetInterfaceCount();
1448 ERR ("Unable to open /proc filesystem to determine number of network interfaces!\n");
1450 WSASetLastError(WSAEINVAL);
1451 return (SOCKET_ERROR);
1454 for (i=0; i<numInt; i++)
1456 if (!WSAIOCTL_GetInterfaceName(i, ifName))
1458 ERR ("Error parsing /proc filesystem!\n");
1460 WSASetLastError(WSAEINVAL);
1461 return (SOCKET_ERROR);
1464 ifInfo.ifr_addr.sa_family = AF_INET;
1467 strcpy (ifInfo.ifr_name, ifName);
1468 if (ioctl(fd, SIOCGIFADDR, &ifInfo) < 0)
1470 ERR ("Error obtaining IP address\n");
1472 WSASetLastError(WSAEINVAL);
1473 return (SOCKET_ERROR);
1477 struct WS_sockaddr_in *ipTemp = (struct WS_sockaddr_in *)&ifInfo.ifr_addr;
1479 intArray->iiAddress.AddressIn.sin_family = AF_INET;
1480 intArray->iiAddress.AddressIn.sin_port = ipTemp->sin_port;
1481 intArray->iiAddress.AddressIn.sin_addr.WS_s_addr = ipTemp->sin_addr.S_un.S_addr;
1484 /* Broadcast Address */
1485 strcpy (ifInfo.ifr_name, ifName);
1486 if (ioctl(fd, SIOCGIFBRDADDR, &ifInfo) < 0)
1488 ERR ("Error obtaining Broadcast IP address\n");
1490 WSASetLastError(WSAEINVAL);
1491 return (SOCKET_ERROR);
1495 struct WS_sockaddr_in *ipTemp = (struct WS_sockaddr_in *)&ifInfo.ifr_broadaddr;
1497 intArray->iiBroadcastAddress.AddressIn.sin_family = AF_INET;
1498 intArray->iiBroadcastAddress.AddressIn.sin_port = ipTemp->sin_port;
1499 intArray->iiBroadcastAddress.AddressIn.sin_addr.WS_s_addr = ipTemp->sin_addr.S_un.S_addr;
1503 strcpy (ifInfo.ifr_name, ifName);
1504 if (ioctl(fd, SIOCGIFNETMASK, &ifInfo) < 0)
1506 ERR ("Error obtaining Subnet IP address\n");
1508 WSASetLastError(WSAEINVAL);
1509 return (SOCKET_ERROR);
1513 /* Trying to avoid some compile problems across platforms.
1514 (Linux, FreeBSD, Solaris...) */
1517 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
1518 intArray->iiNetmask.AddressIn.sin_port = 0;
1519 intArray->iiNetmask.AddressIn.sin_addr.WS_s_addr = 0;
1520 ERR ("Unable to determine Netmask on your platform!\n");
1522 struct WS_sockaddr_in *ipTemp = (struct WS_sockaddr_in *)&ifInfo.ifr_addr;
1524 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
1525 intArray->iiNetmask.AddressIn.sin_port = ipTemp->sin_port;
1526 intArray->iiNetmask.AddressIn.sin_addr.WS_s_addr = ipTemp->sin_addr.S_un.S_addr;
1529 struct WS_sockaddr_in *ipTemp = (struct WS_sockaddr_in *)&ifInfo.ifr_netmask;
1531 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
1532 intArray->iiNetmask.AddressIn.sin_port = ipTemp->sin_port;
1533 intArray->iiNetmask.AddressIn.sin_addr.WS_s_addr = ipTemp->sin_addr.S_un.S_addr;
1537 /* Socket Status Flags */
1538 strcpy(ifInfo.ifr_name, ifName);
1539 if (ioctl(fd, SIOCGIFFLAGS, &ifInfo) < 0)
1541 ERR ("Error obtaining status flags for socket!\n");
1543 WSASetLastError(WSAEINVAL);
1544 return (SOCKET_ERROR);
1548 /* FIXME - Is this the right flag to use? */
1549 intArray->iiFlags = ifInfo.ifr_flags;
1551 intArray++; /* Prepare for another interface */
1554 /* Calculate the size of the array being returned */
1555 *lpcbBytesReturned = sizeof(INTERFACE_INFO) * numInt;
1561 WARN("\tunsupported WS_IOCTL cmd (%08lx)\n", dwIoControlCode);
1563 WSASetLastError(WSAEOPNOTSUPP);
1564 return (SOCKET_ERROR);
1568 /* Function executed with no errors */
1574 WSASetLastError(WSAENOTSOCK);
1575 return (SOCKET_ERROR);
1581 Helper function for WSAIoctl - Get count of the number of interfaces
1582 by parsing /proc filesystem.
1584 int WSAIOCTL_GetInterfaceCount(void)
1587 char buf[512]; /* Size doesn't matter, something big */
1591 /* Open /proc filesystem file for network devices */
1592 procfs = fopen(PROCFS_NETDEV_FILE, "r");
1595 /* If we can't open the file, return an error */
1599 /* Omit first two lines, they are only headers */
1600 fgets(buf, sizeof buf, procfs);
1601 fgets(buf, sizeof buf, procfs);
1603 while (fgets(buf, sizeof buf, procfs))
1605 /* Each line in the file represents a network interface */
1615 Helper function for WSAIoctl - Get name of device from interface number
1616 by parsing /proc filesystem.
1618 int WSAIOCTL_GetInterfaceName(int intNumber, char *intName)
1621 char buf[512]; /* Size doesn't matter, something big */
1624 /* Open /proc filesystem file for network devices */
1625 procfs = fopen(PROCFS_NETDEV_FILE, "r");
1628 /* If we can't open the file, return an error */
1632 /* Omit first two lines, they are only headers */
1633 fgets(buf, sizeof(buf), procfs);
1634 fgets(buf, sizeof(buf), procfs);
1636 for (i=0; i<intNumber; i++)
1638 /* Skip the lines that don't interest us. */
1639 fgets(buf, sizeof(buf), procfs);
1641 fgets(buf, sizeof(buf), procfs); /* This is the line we want */
1644 /* Parse out the line, grabbing only the name of the device
1645 to the intName variable
1647 The Line comes in like this: (we only care about the device name)
1648 lo: 21970 377 0 0 0 0 0 0 21970 377 0 0 0 0 0 0
1651 while (isspace(buf[i])) /* Skip initial space(s) */
1658 if (isspace(buf[i]))
1663 if (buf[i] == ':') /* FIXME: Not sure if this block (alias detection) works properly */
1665 /* This interface could be an alias... */
1667 char *dotname = intName;
1668 *intName++ = buf[i++];
1670 while (isdigit(buf[i]))
1672 *intName++ = buf[i++];
1677 /* ... It wasn't, so back up */
1692 *intName++ = buf[i++];
1701 /***********************************************************************
1702 * ioctlsocket (WS2_32.10)
1704 int WINAPI WS_ioctlsocket(SOCKET s, long cmd, u_long *argp)
1706 int fd = _get_sock_fd(s);
1708 TRACE("socket %04x, cmd %08lx, ptr %8x\n", s, cmd, (unsigned) argp);
1721 if( _get_sock_mask(s) )
1723 /* AsyncSelect()'ed sockets are always nonblocking */
1728 SetLastError(WSAEINVAL);
1730 return SOCKET_ERROR;
1734 _enable_event(s, 0, FD_WINE_NONBLOCKING, 0);
1736 _enable_event(s, 0, 0, FD_WINE_NONBLOCKING);
1743 case WS__IOW('f',125,u_long):
1744 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
1745 SetLastError(WSAEINVAL);
1746 return SOCKET_ERROR;
1748 case SIOCGIFBRDADDR:
1749 case SIOCGIFNETMASK:
1751 /* These don't need any special handling. They are used by
1752 WsControl, and are here to suppress an unecessary warning. */
1757 /* Netscape tries hard to use bogus ioctl 0x667e */
1758 WARN("\tunknown WS_IOCTL cmd (%08lx)\n", cmd);
1760 if( ioctl(fd, newcmd, (char*)argp ) == 0 )
1765 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1768 return SOCKET_ERROR;
1771 /***********************************************************************
1772 * ioctlsocket (WINSOCK.12)
1774 INT16 WINAPI WINSOCK_ioctlsocket16(SOCKET16 s, LONG cmd, ULONG *argp)
1776 return (INT16)WS_ioctlsocket( s, cmd, argp );
1780 /***********************************************************************
1781 * listen (WS2_32.13)
1783 int WINAPI WS_listen(SOCKET s, int backlog)
1785 int fd = _get_sock_fd(s);
1787 TRACE("socket %04x, backlog %d\n", s, backlog);
1790 if (listen(fd, backlog) == 0)
1793 _enable_event(s, FD_ACCEPT,
1795 FD_CONNECT|FD_WINE_CONNECTED);
1798 SetLastError(wsaErrno());
1800 else SetLastError(WSAENOTSOCK);
1801 return SOCKET_ERROR;
1804 /***********************************************************************
1805 * listen (WINSOCK.13)
1807 INT16 WINAPI WINSOCK_listen16(SOCKET16 s, INT16 backlog)
1809 return (INT16)WS_listen( s, backlog );
1813 /***********************************************************************
1816 int WINAPI WS_recv(SOCKET s, char *buf, int len, int flags)
1818 int fd = _get_sock_fd(s);
1820 TRACE("socket %04x, buf %8x, len %d, flags %d\n", s, (unsigned)buf, len, flags);
1826 if (_is_blocking(s))
1829 /* FIXME: OOB and exceptfds? */
1832 if ((length = recv(fd, buf, len, flags)) >= 0)
1834 TRACE(" -> %i bytes\n", length);
1837 _enable_event(s, FD_READ, 0, 0);
1840 SetLastError(wsaErrno());
1843 else SetLastError(WSAENOTSOCK);
1844 WARN(" -> ERROR\n");
1845 return SOCKET_ERROR;
1848 /***********************************************************************
1851 INT16 WINAPI WINSOCK_recv16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1853 return (INT16)WS_recv( s, buf, len, flags );
1857 /***********************************************************************
1858 * recvfrom (WS2_32.17)
1860 int WINAPI WS_recvfrom(SOCKET s, char *buf, INT len, int flags,
1861 struct WS_sockaddr *from, int *fromlen)
1863 int fd = _get_sock_fd(s);
1866 TRACE("socket %04x, ptr %08x, len %d, flags %d\n", s, (unsigned)buf, len, flags);
1869 dump_sockaddr(from);
1871 DPRINTF("from = NULL\n");
1877 struct sockaddr* uaddr;
1881 if (_is_blocking(s))
1884 /* FIXME: OOB and exceptfds */
1888 uaddr=ws_sockaddr_alloc(from,fromlen,&uaddrlen);
1889 length=recvfrom(fd, buf, len, flags, uaddr, &uaddrlen);
1892 SetLastError(wsaErrno());
1893 WARN(" -> ERROR\n");
1895 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,from,fromlen) != 0)
1897 /* The from buffer was too small, but we read the data
1898 * anyway. Is that really bad?
1900 SetLastError(WSAEFAULT);
1901 WARN(" -> WSAEFAULT\n");
1905 TRACE(" -> %i bytes\n", length);
1906 _enable_event(s, FD_READ, 0, 0);
1913 SetLastError(WSAENOTSOCK);
1914 WARN(" -> WSAENOTSOCK\n");
1919 /***********************************************************************
1920 * recvfrom (WINSOCK.17)
1922 INT16 WINAPI WINSOCK_recvfrom16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1923 struct WS_sockaddr *from, INT16 *fromlen16)
1926 INT *p = &fromlen32;
1929 if( fromlen16 ) fromlen32 = *fromlen16; else p = NULL;
1930 retVal = WS_recvfrom( s, buf, len, flags, from, p );
1931 if( fromlen16 ) *fromlen16 = fromlen32;
1932 return (INT16)retVal;
1935 /***********************************************************************
1938 static int __ws_select(BOOL b32,
1939 void *ws_readfds, void *ws_writefds, void *ws_exceptfds,
1940 const struct WS_timeval *ws_timeout)
1943 fd_set readfds, writefds, exceptfds;
1944 fd_set *p_read, *p_write, *p_except;
1945 int readfd[FD_SETSIZE], writefd[FD_SETSIZE], exceptfd[FD_SETSIZE];
1946 struct timeval timeout, *timeoutaddr = NULL;
1948 TRACE("read %p, write %p, excp %p timeout %p\n",
1949 ws_readfds, ws_writefds, ws_exceptfds, ws_timeout);
1951 p_read = fd_set_import(&readfds, ws_readfds, &highfd, readfd, b32);
1952 p_write = fd_set_import(&writefds, ws_writefds, &highfd, writefd, b32);
1953 p_except = fd_set_import(&exceptfds, ws_exceptfds, &highfd, exceptfd, b32);
1956 timeoutaddr = &timeout;
1957 timeout.tv_sec=ws_timeout->tv_sec;
1958 timeout.tv_usec=ws_timeout->tv_usec;
1961 if( (highfd = select(highfd + 1, p_read, p_write, p_except, timeoutaddr)) > 0 )
1963 fd_set_export(&readfds, p_except, ws_readfds, readfd, b32);
1964 fd_set_export(&writefds, p_except, ws_writefds, writefd, b32);
1966 if (p_except && ws_exceptfds)
1968 #define wsfds16 ((ws_fd_set16*)ws_exceptfds)
1969 #define wsfds32 ((WS_fd_set*)ws_exceptfds)
1970 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
1972 for (i = j = 0; i < count; i++)
1974 int fd = exceptfd[i];
1975 if( fd >= 0 && FD_ISSET(fd, &exceptfds) )
1978 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
1980 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
1982 if( fd >= 0 ) close(fd);
1986 wsfds32->fd_count = j;
1988 wsfds16->fd_count = j;
1994 fd_set_unimport(ws_readfds, readfd, b32);
1995 fd_set_unimport(ws_writefds, writefd, b32);
1996 fd_set_unimport(ws_exceptfds, exceptfd, b32);
1997 if( ws_readfds ) ((WS_fd_set*)ws_readfds)->fd_count = 0;
1998 if( ws_writefds ) ((WS_fd_set*)ws_writefds)->fd_count = 0;
1999 if( ws_exceptfds ) ((WS_fd_set*)ws_exceptfds)->fd_count = 0;
2001 if( highfd == 0 ) return 0;
2002 SetLastError(wsaErrno());
2003 return SOCKET_ERROR;
2006 /***********************************************************************
2007 * select (WINSOCK.18)
2009 INT16 WINAPI WINSOCK_select16(INT16 nfds, ws_fd_set16 *ws_readfds,
2010 ws_fd_set16 *ws_writefds, ws_fd_set16 *ws_exceptfds,
2011 struct WS_timeval* timeout)
2013 return (INT16)__ws_select( FALSE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
2016 /***********************************************************************
2017 * select (WS2_32.18)
2019 int WINAPI WS_select(int nfds, WS_fd_set *ws_readfds,
2020 WS_fd_set *ws_writefds, WS_fd_set *ws_exceptfds,
2021 const struct WS_timeval* timeout)
2023 /* struct timeval is the same for both 32- and 16-bit code */
2024 return (INT)__ws_select( TRUE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
2028 /***********************************************************************
2031 int WINAPI WS_send(SOCKET s, const char *buf, int len, int flags)
2033 int fd = _get_sock_fd(s);
2035 TRACE("socket %04x, ptr %p, length %d, flags %d\n", s, buf, len, flags);
2040 if (_is_blocking(s))
2043 /* FIXME: exceptfds */
2046 if ((length = send(fd, buf, len, flags)) < 0 )
2048 SetLastError(wsaErrno());
2049 if( GetLastError() == WSAEWOULDBLOCK )
2050 _enable_event(s, FD_WRITE, 0, 0);
2059 else SetLastError(WSAENOTSOCK);
2060 return SOCKET_ERROR;
2063 /***********************************************************************
2064 * WSASend (WS2_32.72)
2066 INT WINAPI WSASend( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2067 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2068 LPWSAOVERLAPPED lpOverlapped,
2069 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2075 /* Overlapped is not supported or checked for */
2076 FIXME( "(%u,%p,0x%lx,%p,0x%lx,%p,%p): semi stub\n",
2077 s, lpBuffers, dwBufferCount, lpNumberOfBytesSent,
2078 dwFlags, lpOverlapped, lpCompletionRoutine );
2080 /* Convert setup flags */
2081 if( dwFlags & MSG_DONTROUTE )
2083 iFlags |= MSG_DONTROUTE;
2086 if( dwFlags & MSG_OOB )
2091 /* Indicate nothing yet sent */
2092 *lpNumberOfBytesSent = 0;
2094 /* Send all buffers with the same flags */
2095 for(dwCount = 0; dwCount < dwBufferCount; dwCount++ )
2097 if( ( rc = WS_send( s, lpBuffers[ dwCount ].buf,
2098 lpBuffers[ dwCount ].len, iFlags ) ) != 0 )
2103 /* Indicate that we've sent something */
2104 *lpNumberOfBytesSent += lpBuffers[ dwCount ].len;
2110 /***********************************************************************
2113 INT16 WINAPI WINSOCK_send16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
2115 return WS_send( s, buf, len, flags );
2118 /***********************************************************************
2119 * sendto (WS2_32.20)
2121 int WINAPI WS_sendto(SOCKET s, const char *buf, int len, int flags,
2122 const struct WS_sockaddr *to, int tolen)
2124 int fd = _get_sock_fd(s);
2127 TRACE("socket %04x, ptr %p, length %d, flags %d\n", s, buf, len, flags);
2132 const struct sockaddr* uaddr;
2135 uaddr=ws_sockaddr_ws2u(to,tolen,&uaddrlen);
2138 SetLastError(WSAEFAULT);
2142 if (_is_blocking(s))
2145 /* FIXME: exceptfds */
2148 res=sendto(fd, buf, len, flags, uaddr, uaddrlen);
2151 SetLastError(wsaErrno());
2152 if( GetLastError() == WSAEWOULDBLOCK )
2153 _enable_event(s, FD_WRITE, 0, 0);
2155 ws_sockaddr_free(uaddr,to);
2161 SetLastError(WSAENOTSOCK);
2166 /***********************************************************************
2167 * sendto (WINSOCK.20)
2169 INT16 WINAPI WINSOCK_sendto16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
2170 struct WS_sockaddr *to, INT16 tolen)
2172 return (INT16)WS_sendto( s, buf, len, flags, to, tolen );
2175 /***********************************************************************
2176 * setsockopt (WS2_32.21)
2178 int WINAPI WS_setsockopt(SOCKET s, int level, int optname,
2179 const char *optval, int optlen)
2183 TRACE("socket: %04x, level 0x%x, name 0x%x, ptr %8x, len %d\n", s, level,
2184 (int) optname, (int) optval, optlen);
2185 /* SO_OPENTYPE does not require a valid socket handle. */
2186 if (level == WS_SOL_SOCKET && optname == WS_SO_OPENTYPE)
2188 if (optlen < sizeof(int) || !optval)
2190 SetLastError(WSAEFAULT);
2191 return SOCKET_ERROR;
2193 opentype = *(int *)optval;
2194 TRACE("setting global SO_OPENTYPE to 0x%x\n", opentype);
2198 fd = _get_sock_fd(s);
2201 struct linger linger;
2204 /* Is a privileged and useless operation, so we don't. */
2205 if ((optname == WS_SO_DEBUG) && (level == WS_SOL_SOCKET)) {
2206 FIXME("(%d,SOL_SOCKET,SO_DEBUG,%p(%ld)) attempted (is privileged). Ignoring.\n",s,optval,*(DWORD*)optval);
2210 if(optname == WS_SO_DONTLINGER && level == WS_SOL_SOCKET) {
2211 /* This is unique to WinSock and takes special conversion */
2212 linger.l_onoff = *((int*)optval) ? 0: 1;
2213 linger.l_linger = 0;
2215 optval = (char*)&linger;
2216 optlen = sizeof(struct linger);
2219 if (!convert_sockopt(&level, &optname)) {
2220 SetLastError(WSAENOPROTOOPT);
2222 return SOCKET_ERROR;
2224 if (optname == SO_LINGER && optval) {
2225 /* yes, uses unsigned short in both win16/win32 */
2226 linger.l_onoff = ((UINT16*)optval)[0];
2227 linger.l_linger = ((UINT16*)optval)[1];
2228 /* FIXME: what is documented behavior if SO_LINGER optval
2230 optval = (char*)&linger;
2231 optlen = sizeof(struct WS_linger);
2232 } else if (optlen < sizeof(int)){
2233 woptval= *((INT16 *) optval);
2234 optval= (char*) &woptval;
2238 if(optname == SO_RCVBUF && *(int*)optval < 2048) {
2239 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(int*)optval );
2244 if (setsockopt(fd, level, optname, optval, optlen) == 0)
2249 SetLastError(wsaErrno());
2252 else SetLastError(WSAENOTSOCK);
2253 return SOCKET_ERROR;
2256 /***********************************************************************
2257 * setsockopt (WINSOCK.21)
2259 INT16 WINAPI WINSOCK_setsockopt16(SOCKET16 s, INT16 level, INT16 optname,
2260 char *optval, INT16 optlen)
2262 if( !optval ) return SOCKET_ERROR;
2263 return (INT16)WS_setsockopt( s, (UINT16)level, optname, optval, optlen );
2267 /***********************************************************************
2268 * shutdown (WS2_32.22)
2270 int WINAPI WS_shutdown(SOCKET s, int how)
2272 int fd = _get_sock_fd(s);
2274 TRACE("socket %04x, how %i\n", s, how );
2279 case 0: /* drop receives */
2280 _enable_event(s, 0, 0, FD_READ);
2286 case 1: /* drop sends */
2287 _enable_event(s, 0, 0, FD_WRITE);
2293 case 2: /* drop all */
2298 WSAAsyncSelect( s, 0, 0, 0 );
2302 if (shutdown(fd, how) == 0)
2306 _enable_event(s, 0, 0, FD_WINE_CONNECTED|FD_WINE_LISTENING);
2311 SetLastError(wsaErrno());
2314 else SetLastError(WSAENOTSOCK);
2315 return SOCKET_ERROR;
2318 /***********************************************************************
2319 * shutdown (WINSOCK.22)
2321 INT16 WINAPI WINSOCK_shutdown16(SOCKET16 s, INT16 how)
2323 return (INT16)WS_shutdown( s, how );
2327 /***********************************************************************
2328 * socket (WS2_32.23)
2330 SOCKET WINAPI WS_socket(int af, int type, int protocol)
2334 TRACE("af=%d type=%d protocol=%d\n", af, type, protocol);
2336 /* check the socket family */
2340 case WS_AF_IPX: af = AF_IPX;
2343 case AF_UNSPEC: break;
2344 default: SetLastError(WSAEAFNOSUPPORT);
2345 return INVALID_SOCKET;
2348 /* check the socket type */
2351 case WS_SOCK_STREAM:
2360 default: SetLastError(WSAESOCKTNOSUPPORT);
2361 return INVALID_SOCKET;
2364 /* check the protocol type */
2365 if ( protocol < 0 ) /* don't support negative values */
2366 { SetLastError(WSAEPROTONOSUPPORT); return INVALID_SOCKET; }
2368 if ( af == AF_UNSPEC) /* did they not specify the address family? */
2372 if (type == SOCK_STREAM) { af = AF_INET; break; }
2374 if (type == SOCK_DGRAM) { af = AF_INET; break; }
2375 default: SetLastError(WSAEPROTOTYPE); return INVALID_SOCKET;
2378 SERVER_START_REQ( create_socket )
2382 req->protocol = protocol;
2383 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
2384 req->inherit = TRUE;
2385 set_error( wine_server_call( req ) );
2386 ret = (SOCKET)reply->handle;
2391 TRACE("\tcreated %04x\n", ret );
2395 if (GetLastError() == WSAEACCES) /* raw socket denied */
2397 if (type == SOCK_RAW)
2398 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, will fail unless running as root\n");
2400 MESSAGE("WS_SOCKET: not enough privileges to create socket, try running as root\n");
2401 SetLastError(WSAESOCKTNOSUPPORT);
2404 WARN("\t\tfailed!\n");
2405 return INVALID_SOCKET;
2408 /***********************************************************************
2409 * socket (WINSOCK.23)
2411 SOCKET16 WINAPI WINSOCK_socket16(INT16 af, INT16 type, INT16 protocol)
2413 return (SOCKET16)WS_socket( af, type, protocol );
2417 /* ----------------------------------- DNS services
2419 * IMPORTANT: 16-bit API structures have SEGPTR pointers inside them.
2420 * Also, we have to use wsock32 stubs to convert structures and
2421 * error codes from Unix to WSA, hence there is no direct mapping in
2422 * the relay32/wsock32.spec.
2426 /***********************************************************************
2427 * __ws_gethostbyaddr
2429 static WIN_hostent* __ws_gethostbyaddr(const char *addr, int len, int type, int dup_flag)
2431 WIN_hostent *retval = NULL;
2433 struct hostent* host;
2434 #if HAVE_LINUX_GETHOSTBYNAME_R_6
2437 struct hostent hostentry;
2440 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2442 int res = gethostbyaddr_r(addr, len, type,
2443 &hostentry, extrabuf, ebufsize, &host, &locerr);
2444 if( res != ERANGE) break;
2446 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2448 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2450 EnterCriticalSection( &csWSgetXXXbyYYY );
2451 host = gethostbyaddr(addr, len, type);
2452 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2456 if( WS_dup_he(host, dup_flag) )
2459 SetLastError(WSAENOBUFS);
2461 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2462 HeapFree(GetProcessHeap(),0,extrabuf);
2464 LeaveCriticalSection( &csWSgetXXXbyYYY );
2469 /***********************************************************************
2470 * gethostbyaddr (WINSOCK.51)
2472 SEGPTR WINAPI WINSOCK_gethostbyaddr16(const char *addr, INT16 len, INT16 type)
2474 TRACE("ptr %p, len %d, type %d\n", addr, len, type);
2475 if (!__ws_gethostbyaddr( addr, len, type, WS_DUP_SEGPTR )) return 0;
2476 return he_buffer_seg;
2479 /***********************************************************************
2480 * gethostbyaddr (WS2_32.51)
2482 struct WS_hostent* WINAPI WS_gethostbyaddr(const char *addr, int len,
2485 TRACE("ptr %08x, len %d, type %d\n",
2486 (unsigned) addr, len, type);
2487 return __ws_gethostbyaddr(addr, len, type, WS_DUP_LINEAR);
2490 /***********************************************************************
2491 * __ws_gethostbyname
2493 static WIN_hostent * __ws_gethostbyname(const char *name, int dup_flag)
2495 WIN_hostent *retval = NULL;
2496 struct hostent* host;
2497 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2500 struct hostent hostentry;
2501 int locerr = ENOBUFS;
2503 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2505 int res = gethostbyname_r(name, &hostentry, extrabuf, ebufsize, &host, &locerr);
2506 if( res != ERANGE) break;
2508 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2510 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2512 EnterCriticalSection( &csWSgetXXXbyYYY );
2513 host = gethostbyname(name);
2514 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2518 if( WS_dup_he(host, dup_flag) )
2520 else SetLastError(WSAENOBUFS);
2522 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2523 HeapFree(GetProcessHeap(),0,extrabuf);
2525 LeaveCriticalSection( &csWSgetXXXbyYYY );
2530 /***********************************************************************
2531 * gethostbyname (WINSOCK.52)
2533 SEGPTR WINAPI WINSOCK_gethostbyname16(const char *name)
2535 TRACE( "%s\n", debugstr_a(name) );
2536 if (!__ws_gethostbyname( name, WS_DUP_SEGPTR )) return 0;
2537 return he_buffer_seg;
2540 /***********************************************************************
2541 * gethostbyname (WS2_32.52)
2543 struct WS_hostent* WINAPI WS_gethostbyname(const char* name)
2545 TRACE( "%s\n", debugstr_a(name) );
2546 return __ws_gethostbyname( name, WS_DUP_LINEAR );
2550 /***********************************************************************
2551 * __ws_getprotobyname
2553 static WIN_protoent* __ws_getprotobyname(const char *name, int dup_flag)
2555 WIN_protoent* retval = NULL;
2557 struct protoent* proto;
2558 EnterCriticalSection( &csWSgetXXXbyYYY );
2559 if( (proto = getprotobyname(name)) != NULL )
2561 if( WS_dup_pe(proto, dup_flag) )
2563 else SetLastError(WSAENOBUFS);
2566 MESSAGE("protocol %s not found; You might want to add "
2567 "this to /etc/protocols\n", debugstr_a(name) );
2568 SetLastError(WSANO_DATA);
2570 LeaveCriticalSection( &csWSgetXXXbyYYY );
2574 /***********************************************************************
2575 * getprotobyname (WINSOCK.53)
2577 SEGPTR WINAPI WINSOCK_getprotobyname16(const char *name)
2579 TRACE( "%s\n", debugstr_a(name) );
2580 if (!__ws_getprotobyname(name, WS_DUP_SEGPTR)) return 0;
2581 return pe_buffer_seg;
2584 /***********************************************************************
2585 * getprotobyname (WS2_32.53)
2587 struct WS_protoent* WINAPI WS_getprotobyname(const char* name)
2589 TRACE( "%s\n", debugstr_a(name) );
2590 return __ws_getprotobyname(name, WS_DUP_LINEAR);
2594 /***********************************************************************
2595 * __ws_getprotobynumber
2597 static WIN_protoent* __ws_getprotobynumber(int number, int dup_flag)
2599 WIN_protoent* retval = NULL;
2600 struct protoent* proto;
2601 EnterCriticalSection( &csWSgetXXXbyYYY );
2602 if( (proto = getprotobynumber(number)) != NULL )
2604 if( WS_dup_pe(proto, dup_flag) )
2606 else SetLastError(WSAENOBUFS);
2609 MESSAGE("protocol number %d not found; You might want to add "
2610 "this to /etc/protocols\n", number );
2611 SetLastError(WSANO_DATA);
2613 LeaveCriticalSection( &csWSgetXXXbyYYY );
2617 /***********************************************************************
2618 * getprotobynumber (WINSOCK.54)
2620 SEGPTR WINAPI WINSOCK_getprotobynumber16(INT16 number)
2622 TRACE("%i\n", number);
2623 if (!__ws_getprotobynumber(number, WS_DUP_SEGPTR)) return 0;
2624 return pe_buffer_seg;
2627 /***********************************************************************
2628 * getprotobynumber (WS2_32.54)
2630 struct WS_protoent* WINAPI WS_getprotobynumber(int number)
2632 TRACE("%i\n", number);
2633 return __ws_getprotobynumber(number, WS_DUP_LINEAR);
2637 /***********************************************************************
2638 * __ws_getservbyname
2640 static WIN_servent* __ws_getservbyname(const char *name, const char *proto, int dup_flag)
2642 WIN_servent* retval = NULL;
2643 struct servent* serv;
2644 int i = wsi_strtolo( name, proto );
2647 EnterCriticalSection( &csWSgetXXXbyYYY );
2648 serv = getservbyname(local_buffer,
2649 proto ? (local_buffer + i) : NULL);
2652 if( WS_dup_se(serv, dup_flag) )
2654 else SetLastError(WSAENOBUFS);
2657 MESSAGE("service %s protocol %s not found; You might want to add "
2658 "this to /etc/services\n", debugstr_a(local_buffer),
2659 proto ? debugstr_a(local_buffer+i):"*");
2660 SetLastError(WSANO_DATA);
2662 LeaveCriticalSection( &csWSgetXXXbyYYY );
2664 else SetLastError(WSAENOBUFS);
2668 /***********************************************************************
2669 * getservbyname (WINSOCK.55)
2671 SEGPTR WINAPI WINSOCK_getservbyname16(const char *name, const char *proto)
2673 TRACE( "%s, %s\n", debugstr_a(name), debugstr_a(proto) );
2674 if (!__ws_getservbyname(name, proto, WS_DUP_SEGPTR)) return 0;
2675 return se_buffer_seg;
2678 /***********************************************************************
2679 * getservbyname (WS2_32.55)
2681 struct WS_servent* WINAPI WS_getservbyname(const char *name, const char *proto)
2683 TRACE( "%s, %s\n", debugstr_a(name), debugstr_a(proto) );
2684 return __ws_getservbyname(name, proto, WS_DUP_LINEAR);
2688 /***********************************************************************
2689 * __ws_getservbyport
2691 static WIN_servent* __ws_getservbyport(int port, const char* proto, int dup_flag)
2693 WIN_servent* retval = NULL;
2694 struct servent* serv;
2695 if (!proto || wsi_strtolo( proto, NULL )) {
2696 EnterCriticalSection( &csWSgetXXXbyYYY );
2697 if( (serv = getservbyport(port, (proto) ? local_buffer : NULL)) != NULL ) {
2698 if( WS_dup_se(serv, dup_flag) )
2700 else SetLastError(WSAENOBUFS);
2703 MESSAGE("service on port %lu protocol %s not found; You might want to add "
2704 "this to /etc/services\n", (unsigned long)ntohl(port),
2705 proto ? debugstr_a(local_buffer) : "*");
2706 SetLastError(WSANO_DATA);
2708 LeaveCriticalSection( &csWSgetXXXbyYYY );
2710 else SetLastError(WSAENOBUFS);
2714 /***********************************************************************
2715 * getservbyport (WINSOCK.56)
2717 SEGPTR WINAPI WINSOCK_getservbyport16(INT16 port, const char *proto)
2719 TRACE("%d (i.e. port %d), %s\n", (int)port, (int)ntohl(port), debugstr_a(proto));
2720 if (!__ws_getservbyport(port, proto, WS_DUP_SEGPTR)) return 0;
2721 return se_buffer_seg;
2724 /***********************************************************************
2725 * getservbyport (WS2_32.56)
2727 struct WS_servent* WINAPI WS_getservbyport(int port, const char *proto)
2729 TRACE("%d (i.e. port %d), %s\n", (int)port, (int)ntohl(port), debugstr_a(proto));
2730 return __ws_getservbyport(port, proto, WS_DUP_LINEAR);
2734 /***********************************************************************
2735 * gethostname (WS2_32.57)
2737 int WINAPI WS_gethostname(char *name, int namelen)
2739 TRACE("name %p, len %d\n", name, namelen);
2741 if (gethostname(name, namelen) == 0)
2743 TRACE("<- '%s'\n", name);
2746 SetLastError((errno == EINVAL) ? WSAEFAULT : wsaErrno());
2747 TRACE("<- ERROR !\n");
2748 return SOCKET_ERROR;
2751 /***********************************************************************
2752 * gethostname (WINSOCK.57)
2754 INT16 WINAPI WINSOCK_gethostname16(char *name, INT16 namelen)
2756 return (INT16)WS_gethostname(name, namelen);
2760 /* ------------------------------------- Windows sockets extensions -- *
2762 * ------------------------------------------------------------------- */
2764 /***********************************************************************
2765 * WSAEnumNetworkEvents (WS2_32.36)
2767 int WINAPI WSAEnumNetworkEvents(SOCKET s, WSAEVENT hEvent, LPWSANETWORKEVENTS lpEvent)
2771 TRACE("%08x, hEvent %08x, lpEvent %08x\n", s, hEvent, (unsigned)lpEvent );
2773 SERVER_START_REQ( get_socket_event )
2776 req->service = TRUE;
2778 req->c_event = hEvent;
2779 wine_server_set_reply( req, lpEvent->iErrorCode, sizeof(lpEvent->iErrorCode) );
2780 if (!(ret = wine_server_call(req))) lpEvent->lNetworkEvents = reply->pmask & reply->mask;
2784 SetLastError(WSAEINVAL);
2785 return SOCKET_ERROR;
2788 /***********************************************************************
2789 * WSAEventSelect (WS2_32.39)
2791 int WINAPI WSAEventSelect(SOCKET s, WSAEVENT hEvent, LONG lEvent)
2795 TRACE("%08x, hEvent %08x, event %08x\n", s, hEvent, (unsigned)lEvent );
2797 SERVER_START_REQ( set_socket_event )
2801 req->event = hEvent;
2802 ret = wine_server_call( req );
2806 SetLastError(WSAEINVAL);
2807 return SOCKET_ERROR;
2810 /***********************************************************************
2811 * WINSOCK_DoAsyncSelect
2813 VOID CALLBACK WINSOCK_DoAsyncEvent( ULONG_PTR ptr )
2815 ws_select_info *info = (ws_select_info*)ptr;
2816 unsigned int i, j, pmask, orphan = FALSE;
2817 int errors[FD_MAX_EVENTS];
2819 TRACE("socket %08x, event %08x\n", info->sock, info->event);
2821 SERVER_START_REQ( get_socket_event )
2823 req->handle = info->sock;
2824 req->service = TRUE;
2825 req->s_event = info->event; /* <== avoid race conditions */
2826 req->c_event = info->event;
2827 wine_server_set_reply( req, errors, sizeof(errors) );
2828 set_error( wine_server_call(req) );
2829 pmask = reply->pmask;
2832 if ( (GetLastError() == WSAENOTSOCK) || (GetLastError() == WSAEINVAL) )
2834 /* orphaned event (socket closed or something) */
2835 pmask = FD_WINE_SERVEVENT;
2839 /* check for accepted sockets that needs to inherit WSAAsyncSelect */
2840 if (pmask & FD_WINE_SERVEVENT) {
2842 for (q=0; q<WS_ACCEPT_QUEUE; q++)
2843 if (accept_old[q] == info->sock) {
2844 /* there's only one service thread per process, no lock necessary */
2845 HANDLE as = accept_new[q];
2849 WSAAsyncSelect(as, info->hWnd, info->uMsg, info->lEvent);
2852 pmask &= ~FD_WINE_SERVEVENT;
2854 /* dispatch network events, but use the order in the event_bitorder
2857 for (i=0; i<FD_MAX_EVENTS; i++) {
2858 j = event_bitorder[i];
2859 if (pmask & (1<<j)) {
2860 TRACE("post: event bit %d, error %d\n", j, errors[j]);
2861 PostMessageA(info->hWnd, info->uMsg, info->sock,
2862 WSAMAKESELECTREPLY(1<<j, errors[j]));
2868 TRACE("orphaned event, self-destructing\n");
2869 /* SERVICE_Delete closes the event object */
2870 SERVICE_Delete( info->service );
2875 /***********************************************************************
2876 * WSAAsyncSelect (WS2_32.101)
2878 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, LONG lEvent)
2880 int fd = _get_sock_fd(s);
2882 TRACE("%04x, hWnd %04x, uMsg %08x, event %08x\n",
2883 (SOCKET16)s, (HWND16)hWnd, uMsg, (unsigned)lEvent );
2889 ws_select_info *info = (ws_select_info*)WS_ALLOC(sizeof(ws_select_info));
2892 HANDLE hObj = CreateEventA( NULL, TRUE, FALSE, NULL );
2899 info->lEvent = lEvent;
2900 info->service = SERVICE_AddObject( hObj, WINSOCK_DoAsyncEvent, (ULONG_PTR)info );
2902 err = WSAEventSelect( s, hObj, lEvent | FD_WINE_SERVEVENT );
2904 /* SERVICE_Delete closes the event object */
2905 SERVICE_Delete( info->service );
2910 return 0; /* success */
2912 else SetLastError(WSAENOBUFS);
2916 WSAEventSelect(s, 0, 0);
2920 else SetLastError(WSAEINVAL);
2921 return SOCKET_ERROR;
2924 /***********************************************************************
2925 * WSAAsyncSelect (WINSOCK.101)
2927 INT16 WINAPI WSAAsyncSelect16(SOCKET16 s, HWND16 hWnd, UINT16 wMsg, LONG lEvent)
2929 return (INT16)WSAAsyncSelect( s, hWnd, wMsg, lEvent );
2932 /***********************************************************************
2933 * WSARecvEx (WINSOCK.1107)
2935 * See description for WSARecvEx()
2937 INT16 WINAPI WSARecvEx16(SOCKET16 s, char *buf, INT16 len, INT16 *flags)
2939 FIXME("(WSARecvEx16) partial packet return value not set \n");
2941 return WINSOCK_recv16(s, buf, len, *flags);
2945 /***********************************************************************
2946 * WSACreateEvent (WS2_32.31)
2949 WSAEVENT WINAPI WSACreateEvent(void)
2951 /* Create a manual-reset event, with initial state: unsignealed */
2954 return CreateEventA(NULL, TRUE, FALSE, NULL);
2957 /***********************************************************************
2958 * WSACloseEvent (WS2_32.29)
2961 BOOL WINAPI WSACloseEvent(WSAEVENT event)
2963 TRACE ("event=0x%x\n", event);
2965 return CloseHandle(event);
2968 /***********************************************************************
2969 * WSASocketA (WS2_32.78)
2972 SOCKET WINAPI WSASocketA(int af, int type, int protocol,
2973 LPWSAPROTOCOL_INFOA lpProtocolInfo,
2974 GROUP g, DWORD dwFlags)
2977 FIXME: The "advanced" parameters of WSASocketA (lpProtocolInfo,
2978 g, dwFlags) are ignored.
2981 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%lx\n",
2982 af, type, protocol, lpProtocolInfo, g, dwFlags );
2984 return ( WS_socket (af, type, protocol) );
2988 /***********************************************************************
2989 * __WSAFDIsSet (WINSOCK.151)
2991 INT16 WINAPI __WSAFDIsSet16(SOCKET16 s, ws_fd_set16 *set)
2993 int i = set->fd_count;
2995 TRACE("(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2998 if (set->fd_array[i] == s) return 1;
3002 /***********************************************************************
3003 * __WSAFDIsSet (WS2_32.151)
3005 int WINAPI __WSAFDIsSet(SOCKET s, WS_fd_set *set)
3007 int i = set->fd_count;
3009 TRACE("(%d,%8lx(%i))\n", s,(unsigned long)set, i);
3012 if (set->fd_array[i] == s) return 1;
3016 /***********************************************************************
3017 * WSAIsBlocking (WINSOCK.114)
3018 * WSAIsBlocking (WS2_32.114)
3020 BOOL WINAPI WSAIsBlocking(void)
3022 /* By default WinSock should set all its sockets to non-blocking mode
3023 * and poll in PeekMessage loop when processing "blocking" ones. This
3024 * function is supposed to tell if the program is in this loop. Our
3025 * blocking calls are truly blocking so we always return FALSE.
3027 * Note: It is allowed to call this function without prior WSAStartup().
3034 /***********************************************************************
3035 * WSACancelBlockingCall (WINSOCK.113)
3036 * WSACancelBlockingCall (WS2_32.113)
3038 INT WINAPI WSACancelBlockingCall(void)
3045 /***********************************************************************
3046 * WSASetBlockingHook (WINSOCK.109)
3048 FARPROC16 WINAPI WSASetBlockingHook16(FARPROC16 lpBlockFunc)
3050 FARPROC16 prev = (FARPROC16)blocking_hook;
3051 blocking_hook = (FARPROC)lpBlockFunc;
3052 TRACE("hook %p\n", lpBlockFunc);
3057 /***********************************************************************
3058 * WSASetBlockingHook (WS2_32.109)
3060 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
3062 FARPROC prev = blocking_hook;
3063 blocking_hook = lpBlockFunc;
3064 TRACE("hook %p\n", lpBlockFunc);
3069 /***********************************************************************
3070 * WSAUnhookBlockingHook (WINSOCK.110)
3072 INT16 WINAPI WSAUnhookBlockingHook16(void)
3074 blocking_hook = NULL;
3079 /***********************************************************************
3080 * WSAUnhookBlockingHook (WS2_32.110)
3082 INT WINAPI WSAUnhookBlockingHook(void)
3084 blocking_hook = NULL;
3089 /* ----------------------------------- end of API stuff */
3091 /* ----------------------------------- helper functions -
3093 * TODO: Merge WS_dup_..() stuff into one function that
3094 * would operate with a generic structure containing internal
3095 * pointers (via a template of some kind).
3098 static int list_size(char** l, int item_size)
3103 j += (item_size) ? item_size : strlen(l[i]) + 1;
3104 j += (i + 1) * sizeof(char*); }
3108 static int list_dup(char** l_src, char* ref, char* base, int item_size)
3110 /* base is either either equal to ref or 0 or SEGPTR */
3113 char** l_to = (char**)ref;
3116 for(j=0;l_src[j];j++) ;
3117 p += (j + 1) * sizeof(char*);
3119 { l_to[i] = base + (p - ref);
3120 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
3121 memcpy(p, l_src[i], k); p += k; }
3128 static int hostent_size(struct hostent* p_he)
3132 { size = sizeof(struct hostent);
3133 size += strlen(p_he->h_name) + 1;
3134 size += list_size(p_he->h_aliases, 0);
3135 size += list_size(p_he->h_addr_list, p_he->h_length ); }
3139 /* duplicate hostent entry
3140 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
3141 * Dito for protoent and servent.
3143 static int WS_dup_he(struct hostent* p_he, int flag)
3145 /* Convert hostent structure into ws_hostent so that the data fits
3146 * into local_buffer. Internal pointers can be linear, SEGPTR, or
3147 * relative to local_buffer depending on "flag" value. Returns size
3148 * of the data copied.
3151 int size = hostent_size(p_he);
3154 char *p_name,*p_aliases,*p_addr,*p_base,*p;
3156 struct ws_hostent16 *p_to16;
3157 struct WS_hostent *p_to32;
3159 check_buffer_he(size);
3165 p_base = (flag & WS_DUP_SEGPTR) ? (char*)he_buffer_seg : he_buffer;
3166 p += (flag & WS_DUP_SEGPTR) ?
3167 sizeof(struct ws_hostent16) : sizeof(struct WS_hostent);
3169 strcpy(p, p_he->h_name); p += strlen(p) + 1;
3171 p += list_dup(p_he->h_aliases, p, p_base + (p - p_to), 0);
3173 list_dup(p_he->h_addr_list, p, p_base + (p - p_to), p_he->h_length);
3175 if (flag & WS_DUP_SEGPTR) /* Win16 */
3177 p_to16->h_addrtype = (INT16)p_he->h_addrtype;
3178 p_to16->h_length = (INT16)p_he->h_length;
3179 p_to16->h_name = (SEGPTR)(p_base + (p_name - p_to));
3180 p_to16->h_aliases = (SEGPTR)(p_base + (p_aliases - p_to));
3181 p_to16->h_addr_list = (SEGPTR)(p_base + (p_addr - p_to));
3182 size += (sizeof(struct ws_hostent16) - sizeof(struct hostent));
3186 p_to32->h_addrtype = p_he->h_addrtype;
3187 p_to32->h_length = p_he->h_length;
3188 p_to32->h_name = (p_base + (p_name - p_to));
3189 p_to32->h_aliases = (char **)(p_base + (p_aliases - p_to));
3190 p_to32->h_addr_list = (char **)(p_base + (p_addr - p_to));
3191 size += (sizeof(struct WS_hostent) - sizeof(struct hostent));
3197 /* ----- protoent */
3199 static int protoent_size(struct protoent* p_pe)
3203 { size = sizeof(struct protoent);
3204 size += strlen(p_pe->p_name) + 1;
3205 size += list_size(p_pe->p_aliases, 0); }
3209 static int WS_dup_pe(struct protoent* p_pe, int flag)
3211 int size = protoent_size(p_pe);
3215 struct ws_protoent16 *p_to16;
3216 struct WS_protoent *p_to32;
3217 char *p_name,*p_aliases,*p_base,*p;
3219 check_buffer_pe(size);
3224 p_base = (flag & WS_DUP_SEGPTR) ? (char*)pe_buffer_seg : pe_buffer;
3225 p += (flag & WS_DUP_SEGPTR) ?
3226 sizeof(struct ws_protoent16) : sizeof(struct WS_protoent);
3228 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
3230 list_dup(p_pe->p_aliases, p, p_base + (p - p_to), 0);
3232 if (flag & WS_DUP_SEGPTR) /* Win16 */
3234 p_to16->p_proto = (INT16)p_pe->p_proto;
3235 p_to16->p_name = (SEGPTR)(p_base) + (p_name - p_to);
3236 p_to16->p_aliases = (SEGPTR)((p_base) + (p_aliases - p_to));
3237 size += (sizeof(struct ws_protoent16) - sizeof(struct protoent));
3241 p_to32->p_proto = p_pe->p_proto;
3242 p_to32->p_name = (p_base) + (p_name - p_to);
3243 p_to32->p_aliases = (char **)((p_base) + (p_aliases - p_to));
3244 size += (sizeof(struct WS_protoent) - sizeof(struct protoent));
3252 static int servent_size(struct servent* p_se)
3256 { size += sizeof(struct servent);
3257 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
3258 size += list_size(p_se->s_aliases, 0); }
3262 static int WS_dup_se(struct servent* p_se, int flag)
3264 int size = servent_size(p_se);
3267 char *p_name,*p_aliases,*p_proto,*p_base,*p;
3269 struct ws_servent16 *p_to16;
3270 struct WS_servent *p_to32;
3272 check_buffer_se(size);
3277 p_base = (flag & WS_DUP_SEGPTR) ? (char*)se_buffer_seg : se_buffer;
3278 p += (flag & WS_DUP_SEGPTR) ?
3279 sizeof(struct ws_servent16) : sizeof(struct WS_servent);
3281 strcpy(p, p_se->s_name); p += strlen(p) + 1;
3283 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
3285 list_dup(p_se->s_aliases, p, p_base + (p - p_to), 0);
3287 if (flag & WS_DUP_SEGPTR) /* Win16 */
3289 p_to16->s_port = (INT16)p_se->s_port;
3290 p_to16->s_name = (SEGPTR)(p_base + (p_name - p_to));
3291 p_to16->s_proto = (SEGPTR)(p_base + (p_proto - p_to));
3292 p_to16->s_aliases = (SEGPTR)(p_base + (p_aliases - p_to));
3293 size += (sizeof(struct ws_servent16) - sizeof(struct servent));
3297 p_to32->s_port = p_se->s_port;
3298 p_to32->s_name = (p_base + (p_name - p_to));
3299 p_to32->s_proto = (p_base + (p_proto - p_to));
3300 p_to32->s_aliases = (char **)(p_base + (p_aliases - p_to));
3301 size += (sizeof(struct WS_servent) - sizeof(struct servent));
3307 /* ----------------------------------- error handling */
3309 UINT16 wsaErrno(void)
3311 int loc_errno = errno;
3312 WARN("errno %d, (%s).\n", loc_errno, strerror(loc_errno));
3316 case EINTR: return WSAEINTR;
3317 case EBADF: return WSAEBADF;
3319 case EACCES: return WSAEACCES;
3320 case EFAULT: return WSAEFAULT;
3321 case EINVAL: return WSAEINVAL;
3322 case EMFILE: return WSAEMFILE;
3323 case EWOULDBLOCK: return WSAEWOULDBLOCK;
3324 case EINPROGRESS: return WSAEINPROGRESS;
3325 case EALREADY: return WSAEALREADY;
3326 case ENOTSOCK: return WSAENOTSOCK;
3327 case EDESTADDRREQ: return WSAEDESTADDRREQ;
3328 case EMSGSIZE: return WSAEMSGSIZE;
3329 case EPROTOTYPE: return WSAEPROTOTYPE;
3330 case ENOPROTOOPT: return WSAENOPROTOOPT;
3331 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
3332 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
3333 case EOPNOTSUPP: return WSAEOPNOTSUPP;
3334 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
3335 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
3336 case EADDRINUSE: return WSAEADDRINUSE;
3337 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
3338 case ENETDOWN: return WSAENETDOWN;
3339 case ENETUNREACH: return WSAENETUNREACH;
3340 case ENETRESET: return WSAENETRESET;
3341 case ECONNABORTED: return WSAECONNABORTED;
3343 case ECONNRESET: return WSAECONNRESET;
3344 case ENOBUFS: return WSAENOBUFS;
3345 case EISCONN: return WSAEISCONN;
3346 case ENOTCONN: return WSAENOTCONN;
3347 case ESHUTDOWN: return WSAESHUTDOWN;
3348 case ETOOMANYREFS: return WSAETOOMANYREFS;
3349 case ETIMEDOUT: return WSAETIMEDOUT;
3350 case ECONNREFUSED: return WSAECONNREFUSED;
3351 case ELOOP: return WSAELOOP;
3352 case ENAMETOOLONG: return WSAENAMETOOLONG;
3353 case EHOSTDOWN: return WSAEHOSTDOWN;
3354 case EHOSTUNREACH: return WSAEHOSTUNREACH;
3355 case ENOTEMPTY: return WSAENOTEMPTY;
3357 case EPROCLIM: return WSAEPROCLIM;
3360 case EUSERS: return WSAEUSERS;
3363 case EDQUOT: return WSAEDQUOT;
3366 case ESTALE: return WSAESTALE;
3369 case EREMOTE: return WSAEREMOTE;
3372 /* just in case we ever get here and there are no problems */
3375 WARN("Unknown errno %d!\n", loc_errno);
3376 return WSAEOPNOTSUPP;
3380 UINT16 wsaHerrno(int loc_errno)
3383 WARN("h_errno %d.\n", loc_errno);
3387 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
3388 case TRY_AGAIN: return WSATRY_AGAIN;
3389 case NO_RECOVERY: return WSANO_RECOVERY;
3390 case NO_DATA: return WSANO_DATA;
3391 case ENOBUFS: return WSAENOBUFS;
3395 WARN("Unknown h_errno %d!\n", loc_errno);
3396 return WSAEOPNOTSUPP;
3401 /***********************************************************************
3402 * WSARecvFrom (WS2_32.69)
3404 INT WINAPI WSARecvFrom( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
3405 LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, struct WS_sockaddr *lpFrom,
3406 LPINT lpFromlen, LPWSAOVERLAPPED lpOverlapped,
3407 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
3412 FIXME( "(%i,%p,%lu,%p,%p,%p,%p,%p,%p: stub\n",
3413 s, lpBuffers, dwBufferCount, lpNumberOfBytesRecvd, lpFlags,
3414 lpFrom, lpFromlen, lpOverlapped, lpCompletionRoutine );
3416 for( dwCount = 0, rc = 0; dwCount < dwBufferCount; dwCount++ )
3419 if( ( rc = WS_recvfrom(s, lpBuffers[ dwCount ].buf, (INT)lpBuffers[ dwCount ].len,
3420 (INT)*lpFlags, lpFrom, lpFromlen ) ) != 0 )
3431 /***********************************************************************
3432 * WSCInstallProvider (WS2_32.88)
3434 INT WINAPI WSCInstallProvider( const LPGUID lpProviderId,
3435 LPCWSTR lpszProviderDllPath,
3436 const LPWSAPROTOCOL_INFOW lpProtocolInfoList,
3437 DWORD dwNumberOfEntries,
3440 FIXME("(%s, %s, %p, %ld, %p): stub !\n", debugstr_guid(lpProviderId),
3441 debugstr_w(lpszProviderDllPath), lpProtocolInfoList,
3442 dwNumberOfEntries, lpErrno);
3448 /***********************************************************************
3449 * WSCDeinstallProvider (WS2_32.83)
3451 INT WINAPI WSCDeinstallProvider(LPGUID lpProviderId, LPINT lpErrno)
3453 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId), lpErrno);