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).
11 * NOTE 2: Many winsock structs such as servent, hostent, protoent, ...
12 * are used with 1-byte alignment for Win16 programs and 4-byte alignment
13 * for Win32 programs in winsock.h. winsock2.h uses forced 4-byte alignment.
14 * So we have non-forced (just as MSDN) ws_XXXXent (winsock.h), 4-byte forced
15 * ws_XXXXent32 (winsock2.h) and 1-byte forced ws_XXXXent16 (winsock16.h).
22 #include <sys/types.h>
26 #include <sys/ioctl.h>
27 #ifdef HAVE_SYS_FILIO_H
28 # include <sys/filio.h>
30 #ifdef HAVE_SYS_SOCKIO_H
31 # include <sys/sockio.h>
35 # include <sys/so_ioctl.h>
38 #ifdef HAVE_SYS_PARAM_H
39 # include <sys/param.h>
45 #ifdef HAVE_SYS_WAIT_H
46 # include <sys/wait.h>
48 #ifdef HAVE_SYS_SOCKET_H
49 #include <sys/socket.h>
51 #ifdef HAVE_NETINET_IN_H
52 # include <netinet/in.h>
54 #ifdef HAVE_NETINET_TCP_H
55 # include <netinet/tcp.h>
57 #ifdef HAVE_ARPA_INET_H
58 # include <arpa/inet.h>
63 #ifdef HAVE_SYS_ERRNO_H
64 #include <sys/errno.h>
71 #ifdef HAVE_ARPA_NAMESER_H
72 # include <arpa/nameser.h>
81 # include <netipx/ipx.h>
85 # include <asm/types.h>
86 # include <linux/ipx.h>
90 #include "wine/winbase16.h"
94 #include "wine/winsock16.h"
97 #include "wine/port.h"
101 #include "debugtools.h"
104 DEFAULT_DEBUG_CHANNEL(winsock);
106 /* critical section to protect some non-rentrant net function */
107 extern CRITICAL_SECTION csWSgetXXXbyYYY;
109 #define DEBUG_SOCKADDR 0
110 #define dump_sockaddr(a) \
111 DPRINTF("sockaddr_in: family %d, address %s, port %d\n", \
112 ((struct sockaddr_in *)a)->sin_family, \
113 inet_ntoa(((struct sockaddr_in *)a)->sin_addr), \
114 ntohs(((struct sockaddr_in *)a)->sin_port))
116 /* ----------------------------------- internal data */
118 /* ws_... struct conversion flags */
120 #define WS_DUP_LINEAR 0x0001
121 #define WS_DUP_NATIVE 0x0000 /* not used anymore */
122 #define WS_DUP_OFFSET 0x0002 /* internal pointers are offsets */
123 #define WS_DUP_SEGPTR 0x0004 /* internal pointers are SEGPTRs */
124 /* by default, internal pointers are linear */
125 typedef struct /* WSAAsyncSelect() control struct */
127 HANDLE service, event, sock;
133 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
134 #define WS_MAX_UDP_DATAGRAM 1024
136 #define WS_ACCEPT_QUEUE 6
138 #define PROCFS_NETDEV_FILE "/proc/net/dev" /* Points to the file in the /proc fs
139 that lists the network devices.
140 Do we need an #ifdef LINUX for this? */
142 static volatile HANDLE accept_old[WS_ACCEPT_QUEUE], accept_new[WS_ACCEPT_QUEUE];
144 static void *he_buffer; /* typecast for Win16/32 ws_hostent */
145 static void *se_buffer; /* typecast for Win16/32 ws_servent */
146 static void *pe_buffer; /* typecast for Win16/32 ws_protoent */
147 static char* local_buffer; /* allocated from SEGPTR heap */
148 static char* dbuffer; /* buffer for dummies (32 bytes) */
149 static INT num_startup; /* reference counter */
150 static FARPROC blocking_hook;
152 /* function prototypes */
153 int WS_dup_he(struct hostent* p_he, int flag);
154 int WS_dup_pe(struct protoent* p_pe, int flag);
155 int WS_dup_se(struct servent* p_se, int flag);
157 typedef void WIN_hostent;
158 typedef void WIN_protoent;
159 typedef void WIN_servent;
161 int WSAIOCTL_GetInterfaceCount(void);
162 int WSAIOCTL_GetInterfaceName(int intNumber, char *intName);
164 UINT16 wsaErrno(void);
165 UINT16 wsaHerrno(int errnr);
167 static HANDLE _WSHeap = 0;
169 #define WS_ALLOC(size) \
170 HeapAlloc(_WSHeap, HEAP_ZERO_MEMORY, (size) )
171 #define WS_FREE(ptr) \
172 HeapFree(_WSHeap, 0, (ptr) )
174 static INT _ws_sock_ops[] =
175 { WS_SO_DEBUG, WS_SO_REUSEADDR, WS_SO_KEEPALIVE, WS_SO_DONTROUTE,
176 WS_SO_BROADCAST, WS_SO_LINGER, WS_SO_OOBINLINE, WS_SO_SNDBUF,
177 WS_SO_RCVBUF, WS_SO_ERROR, WS_SO_TYPE,
185 static int _px_sock_ops[] =
186 { SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST,
187 SO_LINGER, SO_OOBINLINE, SO_SNDBUF, SO_RCVBUF, SO_ERROR, SO_TYPE,
196 static INT _ws_tcp_ops[] = {
202 static int _px_tcp_ops[] = {
209 /* set last error code from NT status without mapping WSA errors */
210 inline static unsigned int set_error( unsigned int err )
214 /* do not map WSA errors */
215 if ((err < WSABASEERR) || (err >= 0x10000000)) err = RtlNtStatusToDosError(err);
221 static char* check_buffer(int size);
223 static int _get_sock_fd(SOCKET s)
225 int fd = FILE_GetUnixHandle( s, GENERIC_READ );
227 FIXME("handle %d is not a socket (GLE %ld)\n",s,GetLastError());
231 static void _enable_event(SOCKET s, unsigned int event,
232 unsigned int sstate, unsigned int cstate)
234 SERVER_START_REQ( enable_socket_event )
238 req->sstate = sstate;
239 req->cstate = cstate;
245 static int _is_blocking(SOCKET s)
248 SERVER_START_REQ( get_socket_event )
251 req->service = FALSE;
255 ret = (req->state & WS_FD_NONBLOCKING) == 0;
261 static unsigned int _get_sock_mask(SOCKET s)
264 SERVER_START_REQ( get_socket_event )
267 req->service = FALSE;
277 static void _sync_sock_state(SOCKET s)
279 /* do a dummy wineserver request in order to let
280 the wineserver run through its select loop once */
281 (void)_is_blocking(s);
284 static int _get_sock_error(SOCKET s, unsigned int bit)
287 SERVER_START_VAR_REQ( get_socket_event, FD_MAX_EVENTS*sizeof(int) )
290 req->service = FALSE;
294 ret = *((int *)server_data_ptr(req) + bit);
300 static void WINSOCK_DeleteIData(void)
302 /* delete scratch buffers */
304 if (he_buffer) SEGPTR_FREE(he_buffer);
305 if (se_buffer) SEGPTR_FREE(se_buffer);
306 if (pe_buffer) SEGPTR_FREE(pe_buffer);
307 if (local_buffer) SEGPTR_FREE(local_buffer);
308 if (dbuffer) SEGPTR_FREE(dbuffer);
317 /***********************************************************************
318 * WSOCK32_LibMain (WS2_32.init)
320 BOOL WINAPI WSOCK32_LibMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
322 TRACE("0x%x 0x%lx %p\n", hInstDLL, fdwReason, fImpLoad);
324 case DLL_PROCESS_DETACH:
325 WINSOCK_DeleteIData();
331 /***********************************************************************
334 * Converts socket flags from Windows format.
335 * Return 1 if converted, 0 if not (error).
337 static int convert_sockopt(INT *level, INT *optname)
344 for(i=0; _ws_sock_ops[i]; i++)
345 if( _ws_sock_ops[i] == *optname ) break;
346 if( _ws_sock_ops[i] ) {
347 *optname = _px_sock_ops[i];
350 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname);
353 *level = IPPROTO_TCP;
354 for(i=0; _ws_tcp_ops[i]; i++)
355 if ( _ws_tcp_ops[i] == *optname ) break;
356 if( _ws_tcp_ops[i] ) {
357 *optname = _px_tcp_ops[i];
360 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname);
366 /* ----------------------------------- Per-thread info (or per-process?) */
368 static int wsi_strtolo(const char* name, const char* opt)
370 /* Stuff a lowercase copy of the string into the local buffer */
372 int i = strlen(name) + 2;
373 char* p = check_buffer(i + ((opt)?strlen(opt):0));
377 do *p++ = tolower(*name); while(*name++);
378 i = (p - local_buffer);
379 if( opt ) do *p++ = tolower(*opt); while(*opt++);
385 static fd_set* fd_set_import( fd_set* fds, void* wsfds, int* highfd, int lfd[], BOOL b32 )
387 /* translate Winsock fd set into local fd set */
391 #define wsfds16 ((ws_fd_set16*)wsfds)
392 #define wsfds32 ((ws_fd_set32*)wsfds)
396 count = b32 ? wsfds32->fd_count : wsfds16->fd_count;
398 for( i = 0; i < count; i++ )
400 int s = (b32) ? wsfds32->fd_array[i]
401 : wsfds16->fd_array[i];
402 int fd = _get_sock_fd(s);
406 if( fd > *highfd ) *highfd = fd;
418 inline static int sock_error_p(int s)
420 unsigned int optval, optlen;
422 optlen = sizeof(optval);
423 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
424 if (optval) WARN("\t[%i] error: %d\n", s, optval);
428 static int fd_set_export( fd_set* fds, fd_set* exceptfds, void* wsfds, int lfd[], BOOL b32 )
432 /* translate local fd set into Winsock fd set, adding
433 * errors to exceptfds (only if app requested it) */
437 #define wsfds16 ((ws_fd_set16*)wsfds)
438 #define wsfds32 ((ws_fd_set32*)wsfds)
439 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
441 for( i = 0, j = 0; i < count; i++ )
446 if( FD_ISSET(fd, fds) )
448 if ( exceptfds && sock_error_p(fd) )
450 FD_SET(fd, exceptfds);
454 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
456 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
463 if( b32 ) wsfds32->fd_count = j;
464 else wsfds16->fd_count = j;
473 static void fd_set_unimport( void* wsfds, int lfd[], BOOL b32 )
477 #define wsfds16 ((ws_fd_set16*)wsfds)
478 #define wsfds32 ((ws_fd_set32*)wsfds)
479 int i, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
481 for( i = 0; i < count; i++ )
491 static int do_block( int fd, int mask )
502 i = select( fd+1, &fds[0], &fds[1], &fds[2], NULL );
503 if (i <= 0) return -1;
506 if (FD_ISSET(fd, &fds[i]))
511 void* __ws_memalloc( int size )
513 return WS_ALLOC(size);
516 void __ws_memfree(void* ptr)
522 /* ----------------------------------- API -----
524 * Init / cleanup / error checking.
527 /***********************************************************************
528 * WSAStartup (WINSOCK.115)
530 * Create socket control struct, attach it to the global list and
531 * update a pointer in the task struct.
533 INT16 WINAPI WSAStartup16(UINT16 wVersionRequested, LPWSADATA lpWSAData)
535 WSADATA WINSOCK_data = { 0x0101, 0x0101,
539 #elif defined(__NetBSD__)
543 #elif defined(__FreeBSD__)
545 #elif defined(__OpenBSD__)
550 WS_MAX_SOCKETS_PER_PROCESS,
551 WS_MAX_UDP_DATAGRAM, (SEGPTR)NULL };
553 TRACE("verReq=%x\n", wVersionRequested);
555 if (LOBYTE(wVersionRequested) < 1 || (LOBYTE(wVersionRequested) == 1 &&
556 HIBYTE(wVersionRequested) < 1)) return WSAVERNOTSUPPORTED;
558 if (!lpWSAData) return WSAEINVAL;
560 /* initialize socket heap */
564 _WSHeap = HeapCreate(HEAP_ZERO_MEMORY, 8120, 32768);
567 ERR("Fatal: failed to create WinSock heap\n");
571 if( _WSHeap == 0 ) return WSASYSNOTREADY;
575 /* return winsock information */
577 memcpy(lpWSAData, &WINSOCK_data, sizeof(WINSOCK_data));
579 TRACE("succeeded\n");
583 /***********************************************************************
584 * WSAStartup (WS2_32.115)
586 INT WINAPI WSAStartup(UINT wVersionRequested, LPWSADATA lpWSAData)
588 WSADATA WINSOCK_data = { 0x0202, 0x0202,
592 #elif defined(__NetBSD__)
596 #elif defined(__FreeBSD__)
598 #elif defined(__OpenBSD__)
603 WS_MAX_SOCKETS_PER_PROCESS,
604 WS_MAX_UDP_DATAGRAM, (SEGPTR)NULL };
606 TRACE("verReq=%x\n", wVersionRequested);
608 if (LOBYTE(wVersionRequested) < 1)
609 return WSAVERNOTSUPPORTED;
611 if (!lpWSAData) return WSAEINVAL;
613 /* initialize socket heap */
617 _WSHeap = HeapCreate(HEAP_ZERO_MEMORY, 8120, 32768);
620 ERR("Fatal: failed to create WinSock heap\n");
624 if( _WSHeap == 0 ) return WSASYSNOTREADY;
628 /* return winsock information */
629 memcpy(lpWSAData, &WINSOCK_data, sizeof(WINSOCK_data));
631 /* that's the whole of the negotiation for now */
632 lpWSAData->wVersion = wVersionRequested;
634 TRACE("succeeded\n");
639 /***********************************************************************
640 * WSACleanup (WINSOCK.116)
642 INT WINAPI WSACleanup(void)
646 if (--num_startup > 0) return 0;
647 WINSOCK_DeleteIData();
650 SetLastError(WSANOTINITIALISED);
655 /***********************************************************************
656 * WSAGetLastError (WINSOCK.111)
657 * WSAGetLastError (WS2_32.111)
659 INT WINAPI WSAGetLastError(void)
661 return GetLastError();
664 /***********************************************************************
665 * WSASetLastError (WS2_32.112)
667 void WINAPI WSASetLastError(INT iError) {
668 SetLastError(iError);
671 /***********************************************************************
672 * WSASetLastError (WINSOCK.112)
674 void WINAPI WSASetLastError16(INT16 iError)
676 WSASetLastError(iError);
679 static char* check_buffer(int size)
681 static int local_buflen;
685 if (local_buflen >= size ) return local_buffer;
686 SEGPTR_FREE(local_buffer);
688 local_buffer = SEGPTR_ALLOC((local_buflen = size));
692 static struct ws_hostent* check_buffer_he(int size)
697 if (he_len >= size ) return he_buffer;
698 SEGPTR_FREE(he_buffer);
700 he_buffer = SEGPTR_ALLOC((he_len = size));
704 static void* check_buffer_se(int size)
709 if (se_len >= size ) return se_buffer;
710 SEGPTR_FREE(se_buffer);
712 se_buffer = SEGPTR_ALLOC((se_len = size));
716 static struct ws_protoent* check_buffer_pe(int size)
721 if (pe_len >= size ) return pe_buffer;
722 SEGPTR_FREE(pe_buffer);
724 pe_buffer = SEGPTR_ALLOC((pe_len = size));
728 /* ----------------------------------- i/o APIs */
730 /***********************************************************************
733 static void WSOCK32_async_accept(SOCKET s, SOCKET as)
736 /* queue socket for WSAAsyncSelect */
737 for (q=0; q<WS_ACCEPT_QUEUE; q++)
738 if (InterlockedCompareExchange((PVOID*)&accept_old[q], (PVOID)s, (PVOID)0) == (PVOID)0)
740 if (q<WS_ACCEPT_QUEUE)
743 ERR("accept queue too small\n");
744 /* now signal our AsyncSelect handler */
745 _enable_event(s, WS_FD_SERVEVENT, 0, 0);
748 /**********************************************************************/
750 SOCKET WINAPI WSOCK32_accept(SOCKET s, struct sockaddr *addr,
754 struct ws_sockaddr_ipx* addr2 = (struct ws_sockaddr_ipx *)addr;
756 int fd = _get_sock_fd(s);
758 TRACE("socket %04x\n", (UINT16)s );
766 _sync_sock_state(s); /* let wineserver notice connection */
767 /* retrieve any error codes from it */
768 SetLastError(_get_sock_error(s, FD_ACCEPT_BIT));
769 /* FIXME: care about the error? */
772 SERVER_START_REQ( accept_socket )
775 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
777 set_error( SERVER_CALL() );
778 as = (SOCKET)req->handle;
783 unsigned omask = _get_sock_mask( s );
784 int fd = _get_sock_fd( as );
785 if( getpeername(fd, addr, addrlen32) != -1 )
788 if (addr && ((struct sockaddr_ipx *)addr)->sipx_family == AF_IPX) {
789 addr = (struct sockaddr *)
790 malloc(addrlen32 ? *addrlen32 : sizeof(*addr2));
792 addrlen32 ? *addrlen32 : sizeof(*addr2));
793 addr2->sipx_family = WS_AF_IPX;
794 addr2->sipx_network = ((struct sockaddr_ipx *)addr)->sipx_network;
795 addr2->sipx_port = ((struct sockaddr_ipx *)addr)->sipx_port;
796 memcpy(addr2->sipx_node,
797 ((struct sockaddr_ipx *)addr)->sipx_node, IPX_NODE_LEN);
801 } else SetLastError(wsaErrno());
803 if (omask & WS_FD_SERVEVENT)
804 WSOCK32_async_accept(s, as);
808 return INVALID_SOCKET;
811 /***********************************************************************
814 SOCKET16 WINAPI WINSOCK_accept16(SOCKET16 s, struct sockaddr* addr,
817 INT addrlen32 = addrlen16 ? *addrlen16 : 0;
818 SOCKET retSocket = WSOCK32_accept( s, addr, &addrlen32 );
819 if( addrlen16 ) *addrlen16 = (INT16)addrlen32;
820 return (SOCKET16)retSocket;
823 /***********************************************************************
826 INT WINAPI WSOCK32_bind(SOCKET s, struct sockaddr *name, INT namelen)
829 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
831 int fd = _get_sock_fd(s);
833 TRACE("socket %04x, ptr %8x, length %d\n", s, (int) name, namelen);
840 /* FIXME: what family does this really map to on the Unix side? */
841 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_PUP)
842 ((struct ws_sockaddr_ipx *)name)->sipx_family = AF_UNSPEC;
845 ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_IPX)
847 name = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
848 memset(name, '\0', sizeof(struct sockaddr_ipx));
849 ((struct sockaddr_ipx *)name)->sipx_family = AF_IPX;
850 ((struct sockaddr_ipx *)name)->sipx_port = name2->sipx_port;
851 ((struct sockaddr_ipx *)name)->sipx_network = name2->sipx_network;
852 memcpy(((struct sockaddr_ipx *)name)->sipx_node,
853 name2->sipx_node, IPX_NODE_LEN);
854 namelen = sizeof(struct sockaddr_ipx);
857 if ( namelen >= sizeof(*name) )
859 if ( name && (((struct ws_sockaddr_in *)name)->sin_family == AF_INET
861 || ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX
865 if ( bind(fd, name, namelen) < 0 )
867 int loc_errno = errno;
868 WARN("\tfailure - errno = %i\n", errno);
872 case EBADF: SetLastError(WSAENOTSOCK); break;
873 case EADDRNOTAVAIL: SetLastError(WSAEINVAL); break;
874 default: SetLastError(wsaErrno());break;
879 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
883 return 0; /* success */
885 } else SetLastError(WSAEAFNOSUPPORT);
886 } else SetLastError(WSAEFAULT);
888 if (name && ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
896 /***********************************************************************
899 INT16 WINAPI WINSOCK_bind16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
901 return (INT16)WSOCK32_bind( s, name, namelen );
904 /***********************************************************************
905 * closesocket (WS2_32.3)
907 INT WINAPI WSOCK32_closesocket(SOCKET s)
909 TRACE("socket %08x\n", s);
910 if (CloseHandle(s)) return 0;
914 /***********************************************************************
915 * closesocket (WINSOCK.3)
917 INT16 WINAPI WINSOCK_closesocket16(SOCKET16 s)
919 return (INT16)WSOCK32_closesocket(s);
922 /***********************************************************************
925 INT WINAPI WSOCK32_connect(SOCKET s, struct sockaddr *name, INT namelen)
928 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
930 int fd = _get_sock_fd(s);
932 TRACE("socket %04x, ptr %8x, length %d\n", s, (int) name, namelen);
939 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_PUP)
940 ((struct ws_sockaddr_ipx *)name)->sipx_family = AF_UNSPEC;
942 else if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_IPX)
944 name = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
945 memset(name, '\0', sizeof(struct sockaddr_ipx));
946 ((struct sockaddr_ipx *)name)->sipx_family = AF_IPX;
947 ((struct sockaddr_ipx *)name)->sipx_port = name2->sipx_port;
948 ((struct sockaddr_ipx *)name)->sipx_network = name2->sipx_network;
949 memcpy(((struct sockaddr_ipx *)name)->sipx_node,
950 name2->sipx_node, IPX_NODE_LEN);
951 namelen = sizeof(struct sockaddr_ipx);
954 if (connect(fd, name, namelen) == 0) {
956 goto connect_success;
958 if (errno == EINPROGRESS)
960 /* tell wineserver that a connection is in progress */
961 _enable_event(s, FD_CONNECT|FD_READ|FD_WRITE,
962 WS_FD_CONNECT|WS_FD_READ|WS_FD_WRITE,
963 WS_FD_CONNECTED|WS_FD_LISTENING);
969 _sync_sock_state(s); /* let wineserver notice connection */
970 /* retrieve any error codes from it */
971 result = _get_sock_error(s, FD_CONNECT_BIT);
973 SetLastError(result);
976 goto connect_success;
979 else SetLastError(WSAEWOULDBLOCK);
984 SetLastError(wsaErrno());
989 if (name && ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
995 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
998 _enable_event(s, FD_CONNECT|FD_READ|FD_WRITE,
999 WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE,
1000 WS_FD_CONNECT|WS_FD_LISTENING);
1004 /***********************************************************************
1005 * connect (WINSOCK.4)
1007 INT16 WINAPI WINSOCK_connect16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
1009 return (INT16)WSOCK32_connect( s, name, namelen );
1012 /***********************************************************************
1013 * getpeername (WS2_32.5)
1015 INT WINAPI WSOCK32_getpeername(SOCKET s, struct sockaddr *name,
1019 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
1021 int fd = _get_sock_fd(s);
1023 TRACE("socket: %04x, ptr %8x, ptr %8x\n", s, (int) name, *namelen);
1026 if (getpeername(fd, name, namelen) == 0) {
1028 if (((struct ws_sockaddr_ipx *)name)->sipx_family == AF_IPX) {
1029 name = (struct sockaddr *)
1030 malloc(namelen ? *namelen : sizeof(*name2));
1031 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
1032 name2->sipx_family = WS_AF_IPX;
1033 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
1034 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
1035 memcpy(name2->sipx_node,
1036 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
1043 SetLastError(wsaErrno());
1046 return SOCKET_ERROR;
1049 /***********************************************************************
1050 * getpeername (WINSOCK.5)
1052 INT16 WINAPI WINSOCK_getpeername16(SOCKET16 s, struct sockaddr *name,
1055 INT namelen32 = *namelen16;
1056 INT retVal = WSOCK32_getpeername( s, name, &namelen32 );
1059 dump_sockaddr(name);
1062 *namelen16 = namelen32;
1063 return (INT16)retVal;
1066 /***********************************************************************
1067 * getsockname (WS2_32.6)
1069 INT WINAPI WSOCK32_getsockname(SOCKET s, struct sockaddr *name,
1073 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
1075 int fd = _get_sock_fd(s);
1077 TRACE("socket: %04x, ptr %8x, ptr %8x\n", s, (int) name, (int) *namelen);
1080 if (getsockname(fd, name, namelen) == 0) {
1082 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX) {
1083 name = (struct sockaddr *)
1084 malloc(namelen ? *namelen : sizeof(*name2));
1085 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
1086 name2->sipx_family = WS_AF_IPX;
1087 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
1088 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
1089 memcpy(name2->sipx_node,
1090 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
1097 SetLastError(wsaErrno());
1100 return SOCKET_ERROR;
1103 /***********************************************************************
1104 * getsockname (WINSOCK.6)
1106 INT16 WINAPI WINSOCK_getsockname16(SOCKET16 s, struct sockaddr *name,
1113 INT namelen32 = *namelen16;
1114 retVal = WSOCK32_getsockname( s, name, &namelen32 );
1115 *namelen16 = namelen32;
1118 dump_sockaddr(name);
1122 else retVal = SOCKET_ERROR;
1123 return (INT16)retVal;
1127 /***********************************************************************
1128 * getsockopt (WS2_32.7)
1130 INT WINAPI WSOCK32_getsockopt(SOCKET s, INT level,
1131 INT optname, char *optval, INT *optlen)
1133 int fd = _get_sock_fd(s);
1135 TRACE("socket: %04x, opt 0x%x, ptr %8x, len %d\n", s, level, (int) optval, (int) *optlen);
1138 if (!convert_sockopt(&level, &optname)) {
1139 SetLastError(WSAENOPROTOOPT); /* Unknown option */
1141 if (getsockopt(fd, (int) level, optname, optval, optlen) == 0 )
1146 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1150 return SOCKET_ERROR;
1153 /***********************************************************************
1154 * getsockopt (WINSOCK.7)
1156 INT16 WINAPI WINSOCK_getsockopt16(SOCKET16 s, INT16 level,
1157 INT16 optname, char *optval, INT16 *optlen)
1162 if( optlen ) optlen32 = *optlen; else p = NULL;
1163 retVal = WSOCK32_getsockopt( s, (UINT16)level, optname, optval, p );
1164 if( optlen ) *optlen = optlen32;
1165 return (INT16)retVal;
1168 /***********************************************************************
1172 u_long WINAPI WINSOCK_htonl(u_long hostlong) { return( htonl(hostlong) ); }
1173 /***********************************************************************
1177 u_short WINAPI WINSOCK_htons(u_short hostshort) { return( htons(hostshort) ); }
1178 /***********************************************************************
1179 * inet_addr (WINSOCK.10)
1180 * inet_addr (WS2_32.11)
1182 u_long WINAPI WINSOCK_inet_addr(char *cp) { return( inet_addr(cp) ); }
1183 /***********************************************************************
1184 * ntohl (WINSOCK.14)
1187 u_long WINAPI WINSOCK_ntohl(u_long netlong) { return( ntohl(netlong) ); }
1188 /***********************************************************************
1189 * ntohs (WINSOCK.15)
1192 u_short WINAPI WINSOCK_ntohs(u_short netshort) { return( ntohs(netshort) ); }
1194 /***********************************************************************
1195 * inet_ntoa (WS2_32.12)
1197 char* WINAPI WSOCK32_inet_ntoa(struct in_addr in)
1199 /* use "buffer for dummies" here because some applications have
1200 * propensity to decode addresses in ws_hostent structure without
1201 * saving them first...
1203 char* s = inet_ntoa(in);
1206 if( dbuffer == NULL ) {
1207 /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
1208 if((dbuffer = (char*) SEGPTR_ALLOC(16)) == NULL )
1210 SetLastError(WSAENOBUFS);
1217 SetLastError(wsaErrno());
1221 /***********************************************************************
1222 * inet_ntoa (WINSOCK.11)
1224 SEGPTR WINAPI WINSOCK_inet_ntoa16(struct in_addr in)
1226 char* retVal = WSOCK32_inet_ntoa(in);
1227 return SEGPTR_GET(retVal);
1231 /**********************************************************************
1232 * WSAIoctl (WS2_32.50)
1235 * FIXME: Only SIO_GET_INTERFACE_LIST option implemented.
1237 INT WINAPI WSAIoctl (SOCKET s,
1238 DWORD dwIoControlCode,
1241 LPVOID lpbOutBuffer,
1243 LPDWORD lpcbBytesReturned,
1244 LPWSAOVERLAPPED lpOverlapped,
1245 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
1247 int fd = _get_sock_fd(s);
1251 switch( dwIoControlCode )
1253 case SIO_GET_INTERFACE_LIST:
1255 INTERFACE_INFO* intArray = (INTERFACE_INFO*)lpbOutBuffer;
1257 struct ifreq ifInfo;
1261 TRACE ("-> SIO_GET_INTERFACE_LIST request\n");
1263 numInt = WSAIOCTL_GetInterfaceCount();
1266 ERR ("Unable to open /proc filesystem to determine number of network interfaces!\n");
1268 WSASetLastError(WSAEINVAL);
1269 return (SOCKET_ERROR);
1272 for (i=0; i<numInt; i++)
1274 if (!WSAIOCTL_GetInterfaceName(i, ifName))
1276 ERR ("Error parsing /proc filesystem!\n");
1278 WSASetLastError(WSAEINVAL);
1279 return (SOCKET_ERROR);
1282 ifInfo.ifr_addr.sa_family = AF_INET;
1285 strcpy (ifInfo.ifr_name, ifName);
1286 if (ioctl(fd, SIOCGIFADDR, &ifInfo) < 0)
1288 ERR ("Error obtaining IP address\n");
1290 WSASetLastError(WSAEINVAL);
1291 return (SOCKET_ERROR);
1295 struct ws_sockaddr_in *ipTemp = (struct ws_sockaddr_in *)&ifInfo.ifr_addr;
1297 intArray->iiAddress.AddressIn.sin_family = AF_INET;
1298 intArray->iiAddress.AddressIn.sin_port = ipTemp->sin_port;
1299 intArray->iiAddress.AddressIn.sin_addr.ws_addr = ipTemp->sin_addr.S_un.S_addr;
1302 /* Broadcast Address */
1303 strcpy (ifInfo.ifr_name, ifName);
1304 if (ioctl(fd, SIOCGIFBRDADDR, &ifInfo) < 0)
1306 ERR ("Error obtaining Broadcast IP address\n");
1308 WSASetLastError(WSAEINVAL);
1309 return (SOCKET_ERROR);
1313 struct ws_sockaddr_in *ipTemp = (struct ws_sockaddr_in *)&ifInfo.ifr_broadaddr;
1315 intArray->iiBroadcastAddress.AddressIn.sin_family = AF_INET;
1316 intArray->iiBroadcastAddress.AddressIn.sin_port = ipTemp->sin_port;
1317 intArray->iiBroadcastAddress.AddressIn.sin_addr.ws_addr = ipTemp->sin_addr.S_un.S_addr;
1321 strcpy (ifInfo.ifr_name, ifName);
1322 if (ioctl(fd, SIOCGIFNETMASK, &ifInfo) < 0)
1324 ERR ("Error obtaining Subnet IP address\n");
1326 WSASetLastError(WSAEINVAL);
1327 return (SOCKET_ERROR);
1331 /* Trying to avoid some compile problems across platforms.
1332 (Linux, FreeBSD, Solaris...) */
1335 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
1336 intArray->iiNetmask.AddressIn.sin_port = 0;
1337 intArray->iiNetmask.AddressIn.sin_addr.ws_addr = 0;
1338 ERR ("Unable to determine Netmask on your platform!\n");
1340 struct ws_sockaddr_in *ipTemp = (struct ws_sockaddr_in *)&ifInfo.ifr_addr;
1342 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
1343 intArray->iiNetmask.AddressIn.sin_port = ipTemp->sin_port;
1344 intArray->iiNetmask.AddressIn.sin_addr.ws_addr = ipTemp->sin_addr.S_un.S_addr;
1347 struct ws_sockaddr_in *ipTemp = (struct ws_sockaddr_in *)&ifInfo.ifr_netmask;
1349 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
1350 intArray->iiNetmask.AddressIn.sin_port = ipTemp->sin_port;
1351 intArray->iiNetmask.AddressIn.sin_addr.ws_addr = ipTemp->sin_addr.S_un.S_addr;
1355 /* Socket Status Flags */
1356 strcpy(ifInfo.ifr_name, ifName);
1357 if (ioctl(fd, SIOCGIFFLAGS, &ifInfo) < 0)
1359 ERR ("Error obtaining status flags for socket!\n");
1361 WSASetLastError(WSAEINVAL);
1362 return (SOCKET_ERROR);
1366 /* FIXME - Is this the right flag to use? */
1367 intArray->iiFlags = ifInfo.ifr_flags;
1369 intArray++; /* Prepare for another interface */
1372 /* Calculate the size of the array being returned */
1373 *lpcbBytesReturned = sizeof(INTERFACE_INFO) * numInt;
1379 WARN("\tunsupported WS_IOCTL cmd (%08lx)\n", dwIoControlCode);
1381 WSASetLastError(WSAEOPNOTSUPP);
1382 return (SOCKET_ERROR);
1386 /* Function executed with no errors */
1392 WSASetLastError(WSAENOTSOCK);
1393 return (SOCKET_ERROR);
1399 Helper function for WSAIoctl - Get count of the number of interfaces
1400 by parsing /proc filesystem.
1402 int WSAIOCTL_GetInterfaceCount(void)
1405 char buf[512]; /* Size doesn't matter, something big */
1409 /* Open /proc filesystem file for network devices */
1410 procfs = fopen(PROCFS_NETDEV_FILE, "r");
1413 /* If we can't open the file, return an error */
1417 /* Omit first two lines, they are only headers */
1418 fgets(buf, sizeof buf, procfs);
1419 fgets(buf, sizeof buf, procfs);
1421 while (fgets(buf, sizeof buf, procfs))
1423 /* Each line in the file represents a network interface */
1433 Helper function for WSAIoctl - Get name of device from interface number
1434 by parsing /proc filesystem.
1436 int WSAIOCTL_GetInterfaceName(int intNumber, char *intName)
1439 char buf[512]; /* Size doesn't matter, something big */
1442 /* Open /proc filesystem file for network devices */
1443 procfs = fopen(PROCFS_NETDEV_FILE, "r");
1446 /* If we can't open the file, return an error */
1450 /* Omit first two lines, they are only headers */
1451 fgets(buf, sizeof(buf), procfs);
1452 fgets(buf, sizeof(buf), procfs);
1454 for (i=0; i<intNumber; i++)
1456 /* Skip the lines that don't interest us. */
1457 fgets(buf, sizeof(buf), procfs);
1459 fgets(buf, sizeof(buf), procfs); /* This is the line we want */
1462 /* Parse out the line, grabbing only the name of the device
1463 to the intName variable
1465 The Line comes in like this: (we only care about the device name)
1466 lo: 21970 377 0 0 0 0 0 0 21970 377 0 0 0 0 0 0
1469 while (isspace(buf[i])) /* Skip initial space(s) */
1476 if (isspace(buf[i]))
1481 if (buf[i] == ':') /* FIXME: Not sure if this block (alias detection) works properly */
1483 /* This interface could be an alias... */
1485 char *dotname = intName;
1486 *intName++ = buf[i++];
1488 while (isdigit(buf[i]))
1490 *intName++ = buf[i++];
1495 /* ... It wasn't, so back up */
1510 *intName++ = buf[i++];
1519 /***********************************************************************
1520 * ioctlsocket (WS2_32.10)
1522 INT WINAPI WSOCK32_ioctlsocket(SOCKET s, LONG cmd, ULONG *argp)
1524 int fd = _get_sock_fd(s);
1526 TRACE("socket %04x, cmd %08lx, ptr %8x\n", s, cmd, (unsigned) argp);
1539 if( _get_sock_mask(s) )
1541 /* AsyncSelect()'ed sockets are always nonblocking */
1546 SetLastError(WSAEINVAL);
1548 return SOCKET_ERROR;
1552 _enable_event(s, 0, WS_FD_NONBLOCKING, 0);
1554 _enable_event(s, 0, 0, WS_FD_NONBLOCKING);
1561 case WS_IOW('f',125,u_long):
1562 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
1563 SetLastError(WSAEINVAL);
1564 return SOCKET_ERROR;
1566 case SIOCGIFBRDADDR:
1567 case SIOCGIFNETMASK:
1569 /* These don't need any special handling. They are used by
1570 WsControl, and are here to suppress an unecessary warning. */
1575 /* Netscape tries hard to use bogus ioctl 0x667e */
1576 WARN("\tunknown WS_IOCTL cmd (%08lx)\n", cmd);
1578 if( ioctl(fd, newcmd, (char*)argp ) == 0 )
1583 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1586 return SOCKET_ERROR;
1589 /***********************************************************************
1590 * ioctlsocket (WINSOCK.12)
1592 INT16 WINAPI WINSOCK_ioctlsocket16(SOCKET16 s, LONG cmd, ULONG *argp)
1594 return (INT16)WSOCK32_ioctlsocket( s, cmd, argp );
1598 /***********************************************************************
1599 * listen (WS2_32.13)
1601 INT WINAPI WSOCK32_listen(SOCKET s, INT backlog)
1603 int fd = _get_sock_fd(s);
1605 TRACE("socket %04x, backlog %d\n", s, backlog);
1608 if (listen(fd, backlog) == 0)
1611 _enable_event(s, FD_ACCEPT,
1613 WS_FD_CONNECT|WS_FD_CONNECTED);
1616 SetLastError(wsaErrno());
1618 else SetLastError(WSAENOTSOCK);
1619 return SOCKET_ERROR;
1622 /***********************************************************************
1623 * listen (WINSOCK.13)
1625 INT16 WINAPI WINSOCK_listen16(SOCKET16 s, INT16 backlog)
1627 return (INT16)WSOCK32_listen( s, backlog );
1631 /***********************************************************************
1634 INT WINAPI WSOCK32_recv(SOCKET s, char *buf, INT len, INT flags)
1636 int fd = _get_sock_fd(s);
1638 TRACE("socket %04x, buf %8x, len %d, flags %d\n", s, (unsigned)buf, len, flags);
1644 if (_is_blocking(s))
1647 /* FIXME: OOB and exceptfds? */
1650 if ((length = recv(fd, buf, len, flags)) >= 0)
1652 TRACE(" -> %i bytes\n", length);
1655 _enable_event(s, FD_READ, 0, 0);
1658 SetLastError(wsaErrno());
1661 else SetLastError(WSAENOTSOCK);
1662 WARN(" -> ERROR\n");
1663 return SOCKET_ERROR;
1666 /***********************************************************************
1669 INT16 WINAPI WINSOCK_recv16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1671 return (INT16)WSOCK32_recv( s, buf, len, flags );
1675 /***********************************************************************
1676 * recvfrom (WS2_32.17)
1678 INT WINAPI WSOCK32_recvfrom(SOCKET s, char *buf, INT len, INT flags,
1679 struct sockaddr *from, INT *fromlen32)
1682 struct ws_sockaddr_ipx* from2 = (struct ws_sockaddr_ipx *)from;
1684 int fd = _get_sock_fd(s);
1686 TRACE("socket %04x, ptr %08x, len %d, flags %d\n", s, (unsigned)buf, len, flags);
1688 if( from ) dump_sockaddr(from);
1689 else DPRINTF("from = NULL\n");
1696 if (_is_blocking(s))
1699 /* FIXME: OOB and exceptfds */
1702 if ((length = recvfrom(fd, buf, len, flags, from, fromlen32)) >= 0)
1704 TRACE(" -> %i bytes\n", length);
1707 if (from && ((struct sockaddr_ipx *)from)->sipx_family == AF_IPX) {
1708 from = (struct sockaddr *)
1709 malloc(fromlen32 ? *fromlen32 : sizeof(*from2));
1710 memcpy(from, from2, fromlen32 ? *fromlen32 : sizeof(*from2));
1711 from2->sipx_family = WS_AF_IPX;
1712 from2->sipx_network = ((struct sockaddr_ipx *)from)->sipx_network;
1713 from2->sipx_port = ((struct sockaddr_ipx *)from)->sipx_port;
1714 memcpy(from2->sipx_node,
1715 ((struct sockaddr_ipx *)from)->sipx_node, IPX_NODE_LEN);
1720 _enable_event(s, FD_READ, 0, 0);
1723 SetLastError(wsaErrno());
1726 else SetLastError(WSAENOTSOCK);
1727 WARN(" -> ERROR\n");
1729 if (from && ((struct sockaddr_ipx *)from)->sipx_family == AF_IPX) {
1730 from = (struct sockaddr *)
1731 malloc(fromlen32 ? *fromlen32 : sizeof(*from2));
1732 memcpy(from, from2, fromlen32 ? *fromlen32 : sizeof(*from2));
1733 from2->sipx_family = WS_AF_IPX;
1734 from2->sipx_network = ((struct sockaddr_ipx *)from)->sipx_network;
1735 from2->sipx_port = ((struct sockaddr_ipx *)from)->sipx_port;
1736 memcpy(from2->sipx_node,
1737 ((struct sockaddr_ipx *)from)->sipx_node, IPX_NODE_LEN);
1741 return SOCKET_ERROR;
1744 /***********************************************************************
1745 * recvfrom (WINSOCK.17)
1747 INT16 WINAPI WINSOCK_recvfrom16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1748 struct sockaddr *from, INT16 *fromlen16)
1751 INT *p = &fromlen32;
1754 if( fromlen16 ) fromlen32 = *fromlen16; else p = NULL;
1755 retVal = WSOCK32_recvfrom( s, buf, len, flags, from, p );
1756 if( fromlen16 ) *fromlen16 = fromlen32;
1757 return (INT16)retVal;
1760 /***********************************************************************
1763 static INT __ws_select( BOOL b32, void *ws_readfds, void *ws_writefds, void *ws_exceptfds,
1764 struct timeval *timeout )
1767 fd_set readfds, writefds, exceptfds;
1768 fd_set *p_read, *p_write, *p_except;
1769 int readfd[FD_SETSIZE], writefd[FD_SETSIZE], exceptfd[FD_SETSIZE];
1771 TRACE("read %p, write %p, excp %p\n", ws_readfds, ws_writefds, ws_exceptfds);
1773 p_read = fd_set_import(&readfds, ws_readfds, &highfd, readfd, b32);
1774 p_write = fd_set_import(&writefds, ws_writefds, &highfd, writefd, b32);
1775 p_except = fd_set_import(&exceptfds, ws_exceptfds, &highfd, exceptfd, b32);
1777 if( (highfd = select(highfd + 1, p_read, p_write, p_except, timeout)) > 0 )
1779 fd_set_export(&readfds, p_except, ws_readfds, readfd, b32);
1780 fd_set_export(&writefds, p_except, ws_writefds, writefd, b32);
1782 if (p_except && ws_exceptfds)
1784 #define wsfds16 ((ws_fd_set16*)ws_exceptfds)
1785 #define wsfds32 ((ws_fd_set32*)ws_exceptfds)
1786 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
1788 for (i = j = 0; i < count; i++)
1790 int fd = exceptfd[i];
1791 if( fd >= 0 && FD_ISSET(fd, &exceptfds) )
1794 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
1796 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
1798 if( fd >= 0 ) close(fd);
1802 wsfds32->fd_count = j;
1804 wsfds16->fd_count = j;
1810 fd_set_unimport(ws_readfds, readfd, b32);
1811 fd_set_unimport(ws_writefds, writefd, b32);
1812 fd_set_unimport(ws_exceptfds, exceptfd, b32);
1813 if( ws_readfds ) ((ws_fd_set32*)ws_readfds)->fd_count = 0;
1814 if( ws_writefds ) ((ws_fd_set32*)ws_writefds)->fd_count = 0;
1815 if( ws_exceptfds ) ((ws_fd_set32*)ws_exceptfds)->fd_count = 0;
1817 if( highfd == 0 ) return 0;
1818 SetLastError(wsaErrno());
1819 return SOCKET_ERROR;
1822 /***********************************************************************
1823 * select (WINSOCK.18)
1825 INT16 WINAPI WINSOCK_select16(INT16 nfds, ws_fd_set16 *ws_readfds,
1826 ws_fd_set16 *ws_writefds, ws_fd_set16 *ws_exceptfds,
1827 struct timeval *timeout)
1829 return (INT16)__ws_select( FALSE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1832 /***********************************************************************
1833 * select (WINSOCK.18)
1835 INT WINAPI WSOCK32_select(INT nfds, ws_fd_set32 *ws_readfds,
1836 ws_fd_set32 *ws_writefds, ws_fd_set32 *ws_exceptfds,
1837 struct timeval *timeout)
1839 /* struct timeval is the same for both 32- and 16-bit code */
1840 return (INT)__ws_select( TRUE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1844 /***********************************************************************
1847 INT WINAPI WSOCK32_send(SOCKET s, char *buf, INT len, INT flags)
1849 int fd = _get_sock_fd(s);
1851 TRACE("socket %04x, ptr %p, length %d, flags %d\n", s, buf, len, flags);
1856 if (_is_blocking(s))
1859 /* FIXME: exceptfds */
1862 if ((length = send(fd, buf, len, flags)) < 0 )
1864 SetLastError(wsaErrno());
1865 if( GetLastError() == WSAEWOULDBLOCK )
1866 _enable_event(s, FD_WRITE, 0, 0);
1875 else SetLastError(WSAENOTSOCK);
1876 return SOCKET_ERROR;
1879 /***********************************************************************
1880 * WSASend (WS2_32.72)
1882 INT WINAPI WSASend( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
1883 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
1884 LPWSAOVERLAPPED lpOverlapped,
1885 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
1891 /* Overlapped is not supported or checked for */
1892 FIXME( "(%u,%p,0x%lx,%p,0x%lx,%p,%p): semi stub\n",
1893 s, lpBuffers, dwBufferCount, lpNumberOfBytesSent,
1894 dwFlags, lpOverlapped, lpCompletionRoutine );
1896 /* Convert setup flags */
1897 if( dwFlags & MSG_DONTROUTE )
1899 iFlags |= MSG_DONTROUTE;
1902 if( dwFlags & MSG_OOB )
1907 /* Indicate nothing yet sent */
1908 *lpNumberOfBytesSent = 0;
1910 /* Send all buffers with the same flags */
1911 for(dwCount = 0; dwCount < dwBufferCount; dwCount++ )
1913 if( ( rc = WSOCK32_send( s, lpBuffers[ dwCount ].buf,
1914 lpBuffers[ dwCount ].len, iFlags ) ) != 0 )
1919 /* Indicate that we've sent something */
1920 *lpNumberOfBytesSent += lpBuffers[ dwCount ].len;
1926 /***********************************************************************
1929 INT16 WINAPI WINSOCK_send16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1931 return WSOCK32_send( s, buf, len, flags );
1934 /***********************************************************************
1935 * sendto (WS2_32.20)
1937 INT WINAPI WSOCK32_sendto(SOCKET s, char *buf, INT len, INT flags,
1938 struct sockaddr *to, INT tolen)
1941 struct ws_sockaddr_ipx* to2 = (struct ws_sockaddr_ipx *)to;
1943 int fd = _get_sock_fd(s);
1945 TRACE("socket %04x, ptr %p, length %d, flags %d\n", s, buf, len, flags);
1950 if (to && ((struct ws_sockaddr_ipx *)to)->sipx_family == WS_AF_PUP)
1951 ((struct ws_sockaddr_ipx *)to)->sipx_family = AF_UNSPEC;
1954 ((struct ws_sockaddr_ipx *)to)->sipx_family == WS_AF_IPX)
1956 to = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
1957 memset(to, '\0', sizeof(struct sockaddr_ipx));
1958 ((struct sockaddr_ipx *)to)->sipx_family = AF_IPX;
1959 ((struct sockaddr_ipx *)to)->sipx_port = to2->sipx_port;
1960 ((struct sockaddr_ipx *)to)->sipx_network = to2->sipx_network;
1961 memcpy(((struct sockaddr_ipx *)to)->sipx_node,
1962 to2->sipx_node, IPX_NODE_LEN);
1963 tolen = sizeof(struct sockaddr_ipx);
1966 if (_is_blocking(s))
1969 /* FIXME: exceptfds */
1972 if ((length = sendto(fd, buf, len, flags, to, tolen)) < 0 )
1974 SetLastError(wsaErrno());
1975 if( GetLastError() == WSAEWOULDBLOCK )
1976 _enable_event(s, FD_WRITE, 0, 0);
1980 if (to && ((struct sockaddr_ipx *)to)->sipx_family == AF_IPX) {
1989 else SetLastError(WSAENOTSOCK);
1991 if (to && ((struct sockaddr_ipx *)to)->sipx_family == AF_IPX) {
1995 return SOCKET_ERROR;
1998 /***********************************************************************
1999 * sendto (WINSOCK.20)
2001 INT16 WINAPI WINSOCK_sendto16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
2002 struct sockaddr *to, INT16 tolen)
2004 return (INT16)WSOCK32_sendto( s, buf, len, flags, to, tolen );
2007 /***********************************************************************
2008 * setsockopt (WS2_32.21)
2010 INT WINAPI WSOCK32_setsockopt(SOCKET16 s, INT level, INT optname,
2011 char *optval, INT optlen)
2013 int fd = _get_sock_fd(s);
2015 TRACE("socket %04x, lev %d, opt 0x%x, ptr %08x, len %d\n",
2016 s, level, optname, (int) optval, optlen);
2019 struct linger linger;
2022 /* Is a privileged and useless operation, so we don't. */
2023 if ((optname == WS_SO_DEBUG) && (level == WS_SOL_SOCKET)) {
2024 FIXME("(%d,SOL_SOCKET,SO_DEBUG,%p(%ld)) attempted (is privileged). Ignoring.\n",s,optval,*(DWORD*)optval);
2028 if(optname == WS_SO_DONTLINGER && level == WS_SOL_SOCKET) {
2029 /* This is unique to WinSock and takes special conversion */
2030 linger.l_onoff = *((int*)optval) ? 0: 1;
2031 linger.l_linger = 0;
2033 optval = (char*)&linger;
2034 optlen = sizeof(struct linger);
2037 if (!convert_sockopt(&level, &optname)) {
2038 SetLastError(WSAENOPROTOOPT);
2040 return SOCKET_ERROR;
2042 if (optname == SO_LINGER && optval) {
2043 /* yes, uses unsigned short in both win16/win32 */
2044 linger.l_onoff = ((UINT16*)optval)[0];
2045 linger.l_linger = ((UINT16*)optval)[1];
2046 /* FIXME: what is documented behavior if SO_LINGER optval
2048 optval = (char*)&linger;
2049 optlen = sizeof(struct linger);
2050 } else if (optlen < sizeof(int)){
2051 woptval= *((INT16 *) optval);
2052 optval= (char*) &woptval;
2056 if(optname == SO_RCVBUF && *(int*)optval < 2048) {
2057 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(int*)optval );
2062 if (setsockopt(fd, level, optname, optval, optlen) == 0)
2067 SetLastError(wsaErrno());
2070 else SetLastError(WSAENOTSOCK);
2071 return SOCKET_ERROR;
2074 /***********************************************************************
2075 * setsockopt (WINSOCK.21)
2077 INT16 WINAPI WINSOCK_setsockopt16(SOCKET16 s, INT16 level, INT16 optname,
2078 char *optval, INT16 optlen)
2080 if( !optval ) return SOCKET_ERROR;
2081 return (INT16)WSOCK32_setsockopt( s, (UINT16)level, optname, optval, optlen );
2085 /***********************************************************************
2086 * shutdown (WS2_32.22)
2088 INT WINAPI WSOCK32_shutdown(SOCKET s, INT how)
2090 int fd = _get_sock_fd(s);
2092 TRACE("socket %04x, how %i\n", s, how );
2097 case 0: /* drop receives */
2098 _enable_event(s, 0, 0, WS_FD_READ);
2104 case 1: /* drop sends */
2105 _enable_event(s, 0, 0, WS_FD_WRITE);
2111 case 2: /* drop all */
2116 WSAAsyncSelect( s, 0, 0, 0 );
2120 if (shutdown(fd, how) == 0)
2124 _enable_event(s, 0, 0, WS_FD_CONNECTED|WS_FD_LISTENING);
2129 SetLastError(wsaErrno());
2132 else SetLastError(WSAENOTSOCK);
2133 return SOCKET_ERROR;
2136 /***********************************************************************
2137 * shutdown (WINSOCK.22)
2139 INT16 WINAPI WINSOCK_shutdown16(SOCKET16 s, INT16 how)
2141 return (INT16)WSOCK32_shutdown( s, how );
2145 /***********************************************************************
2146 * socket (WS2_32.23)
2148 SOCKET WINAPI WSOCK32_socket(INT af, INT type, INT protocol)
2152 TRACE("af=%d type=%d protocol=%d\n", af, type, protocol);
2154 /* check the socket family */
2158 case WS_AF_IPX: af = AF_IPX;
2161 case AF_UNSPEC: break;
2162 default: SetLastError(WSAEAFNOSUPPORT);
2163 return INVALID_SOCKET;
2166 /* check the socket type */
2171 case SOCK_RAW: break;
2172 default: SetLastError(WSAESOCKTNOSUPPORT);
2173 return INVALID_SOCKET;
2176 /* check the protocol type */
2177 if ( protocol < 0 ) /* don't support negative values */
2178 { SetLastError(WSAEPROTONOSUPPORT); return INVALID_SOCKET; }
2180 if ( af == AF_UNSPEC) /* did they not specify the address family? */
2184 if (type == SOCK_STREAM) { af = AF_INET; break; }
2186 if (type == SOCK_DGRAM) { af = AF_INET; break; }
2187 default: SetLastError(WSAEPROTOTYPE); return INVALID_SOCKET;
2190 SERVER_START_REQ( create_socket )
2194 req->protocol = protocol;
2195 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
2196 req->inherit = TRUE;
2197 set_error( SERVER_CALL() );
2198 ret = (SOCKET)req->handle;
2203 TRACE("\tcreated %04x\n", ret );
2207 if (GetLastError() == WSAEACCES) /* raw socket denied */
2209 if (type == SOCK_RAW)
2210 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, will fail unless running as root\n");
2212 MESSAGE("WS_SOCKET: not enough privileges to create socket, try running as root\n");
2213 SetLastError(WSAESOCKTNOSUPPORT);
2216 WARN("\t\tfailed!\n");
2217 return INVALID_SOCKET;
2220 /***********************************************************************
2221 * socket (WINSOCK.23)
2223 SOCKET16 WINAPI WINSOCK_socket16(INT16 af, INT16 type, INT16 protocol)
2225 return (SOCKET16)WSOCK32_socket( af, type, protocol );
2229 /* ----------------------------------- DNS services
2231 * IMPORTANT: 16-bit API structures have SEGPTR pointers inside them.
2232 * Also, we have to use wsock32 stubs to convert structures and
2233 * error codes from Unix to WSA, hence there is no direct mapping in
2234 * the relay32/wsock32.spec.
2237 static char* NULL_STRING = "NULL";
2239 /***********************************************************************
2240 * __ws_gethostbyaddr
2242 static WIN_hostent* __ws_gethostbyaddr(const char *addr, int len, int type, int dup_flag)
2244 WIN_hostent *retval = NULL;
2246 struct hostent* host;
2247 #if HAVE_LINUX_GETHOSTBYNAME_R_6
2250 struct hostent hostentry;
2253 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2255 int res = gethostbyaddr_r(addr, len, type,
2256 &hostentry, extrabuf, ebufsize, &host, &locerr);
2257 if( res != ERANGE) break;
2259 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2261 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2263 EnterCriticalSection( &csWSgetXXXbyYYY );
2264 host = gethostbyaddr(addr, len, type);
2265 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2269 if( WS_dup_he(host, dup_flag) )
2272 SetLastError(WSAENOBUFS);
2274 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2275 HeapFree(GetProcessHeap(),0,extrabuf);
2277 LeaveCriticalSection( &csWSgetXXXbyYYY );
2282 /***********************************************************************
2283 * gethostbyaddr (WINSOCK.51)
2285 SEGPTR WINAPI WINSOCK_gethostbyaddr16(const char *addr, INT16 len, INT16 type)
2287 WIN_hostent* retval;
2288 TRACE("ptr %08x, len %d, type %d\n",
2289 (unsigned) addr, len, type);
2290 retval = __ws_gethostbyaddr( addr, len, type, WS_DUP_SEGPTR );
2291 return SEGPTR_GET(retval);
2294 /***********************************************************************
2295 * gethostbyaddr (WS2_32.51)
2297 WIN_hostent* WINAPI WSOCK32_gethostbyaddr(const char *addr, INT len,
2300 TRACE("ptr %08x, len %d, type %d\n",
2301 (unsigned) addr, len, type);
2302 return __ws_gethostbyaddr(addr, len, type, WS_DUP_LINEAR);
2305 /***********************************************************************
2306 * __ws_gethostbyname
2308 static WIN_hostent * __ws_gethostbyname(const char *name, int dup_flag)
2310 WIN_hostent *retval = NULL;
2311 struct hostent* host;
2312 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2315 struct hostent hostentry;
2316 int locerr = ENOBUFS;
2318 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2320 int res = gethostbyname_r(name, &hostentry, extrabuf, ebufsize, &host, &locerr);
2321 if( res != ERANGE) break;
2323 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2325 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2327 EnterCriticalSection( &csWSgetXXXbyYYY );
2328 host = gethostbyname(name);
2329 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2333 if( WS_dup_he(host, dup_flag) )
2335 else SetLastError(WSAENOBUFS);
2337 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2338 HeapFree(GetProcessHeap(),0,extrabuf);
2340 LeaveCriticalSection( &csWSgetXXXbyYYY );
2345 /***********************************************************************
2346 * gethostbyname (WINSOCK.52)
2348 SEGPTR WINAPI WINSOCK_gethostbyname16(const char *name)
2350 WIN_hostent* retval;
2351 TRACE("%s\n", (name)?name:NULL_STRING);
2352 retval = __ws_gethostbyname( name, WS_DUP_SEGPTR );
2353 return SEGPTR_GET(retval);
2356 /***********************************************************************
2357 * gethostbyname (WS2_32.52)
2359 WIN_hostent* WINAPI WSOCK32_gethostbyname(const char* name)
2361 TRACE("%s\n", (name)?name:NULL_STRING);
2362 return __ws_gethostbyname( name, WS_DUP_LINEAR );
2366 /***********************************************************************
2367 * __ws_getprotobyname
2369 static WIN_protoent* __ws_getprotobyname(const char *name, int dup_flag)
2371 WIN_protoent* retval = NULL;
2373 struct protoent* proto;
2374 EnterCriticalSection( &csWSgetXXXbyYYY );
2375 if( (proto = getprotobyname(name)) != NULL )
2377 if( WS_dup_pe(proto, dup_flag) )
2379 else SetLastError(WSAENOBUFS);
2382 MESSAGE("protocol %s not found; You might want to add "
2383 "this to /etc/protocols\n", debugstr_a(name) );
2384 SetLastError(WSANO_DATA);
2386 LeaveCriticalSection( &csWSgetXXXbyYYY );
2390 /***********************************************************************
2391 * getprotobyname (WINSOCK.53)
2393 SEGPTR WINAPI WINSOCK_getprotobyname16(const char *name)
2395 WIN_protoent* retval;
2396 TRACE("%s\n", (name)?name:NULL_STRING);
2397 retval = __ws_getprotobyname(name, WS_DUP_SEGPTR);
2398 return SEGPTR_GET(retval);
2401 /***********************************************************************
2402 * getprotobyname (WS2_32.53)
2404 WIN_protoent* WINAPI WSOCK32_getprotobyname(const char* name)
2406 TRACE("%s\n", (name)?name:NULL_STRING);
2407 return __ws_getprotobyname(name, WS_DUP_LINEAR);
2411 /***********************************************************************
2412 * __ws_getprotobynumber
2414 static WIN_protoent* __ws_getprotobynumber(int number, int dup_flag)
2416 WIN_protoent* retval = NULL;
2417 struct protoent* proto;
2418 EnterCriticalSection( &csWSgetXXXbyYYY );
2419 if( (proto = getprotobynumber(number)) != NULL )
2421 if( WS_dup_pe(proto, dup_flag) )
2423 else SetLastError(WSAENOBUFS);
2426 MESSAGE("protocol number %d not found; You might want to add "
2427 "this to /etc/protocols\n", number );
2428 SetLastError(WSANO_DATA);
2430 LeaveCriticalSection( &csWSgetXXXbyYYY );
2434 /***********************************************************************
2435 * getprotobynumber (WINSOCK.54)
2437 SEGPTR WINAPI WINSOCK_getprotobynumber16(INT16 number)
2439 WIN_protoent* retval;
2440 TRACE("%i\n", number);
2441 retval = __ws_getprotobynumber(number, WS_DUP_SEGPTR);
2442 return SEGPTR_GET(retval);
2445 /***********************************************************************
2446 * getprotobynumber (WS2_32.54)
2448 WIN_protoent* WINAPI WSOCK32_getprotobynumber(INT number)
2450 TRACE("%i\n", number);
2451 return __ws_getprotobynumber(number, WS_DUP_LINEAR);
2455 /***********************************************************************
2456 * __ws_getservbyname
2458 static WIN_servent* __ws_getservbyname(const char *name, const char *proto, int dup_flag)
2460 WIN_servent* retval = NULL;
2461 struct servent* serv;
2462 int i = wsi_strtolo( name, proto );
2465 EnterCriticalSection( &csWSgetXXXbyYYY );
2466 serv = getservbyname(local_buffer,
2467 proto ? (local_buffer + i) : NULL);
2470 if( WS_dup_se(serv, dup_flag) )
2472 else SetLastError(WSAENOBUFS);
2475 MESSAGE("service %s protocol %s not found; You might want to add "
2476 "this to /etc/services\n", debugstr_a(local_buffer),
2477 proto ? debugstr_a(local_buffer+i):"*");
2478 SetLastError(WSANO_DATA);
2480 LeaveCriticalSection( &csWSgetXXXbyYYY );
2482 else SetLastError(WSAENOBUFS);
2486 /***********************************************************************
2487 * getservbyname (WINSOCK.55)
2489 SEGPTR WINAPI WINSOCK_getservbyname16(const char *name, const char *proto)
2491 WIN_servent* retval;
2492 TRACE("'%s', '%s'\n",
2493 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
2494 retval = __ws_getservbyname(name, proto, WS_DUP_SEGPTR);
2495 return SEGPTR_GET(retval);
2498 /***********************************************************************
2499 * getservbyname (WS2_32.55)
2501 WIN_servent* WINAPI WSOCK32_getservbyname(const char *name, const char *proto)
2503 TRACE("'%s', '%s'\n",
2504 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
2505 return __ws_getservbyname(name, proto, WS_DUP_LINEAR);
2509 /***********************************************************************
2510 * __ws_getservbyport
2512 static WIN_servent* __ws_getservbyport(int port, const char* proto, int dup_flag)
2514 WIN_servent* retval = NULL;
2515 struct servent* serv;
2516 if (!proto || wsi_strtolo( proto, NULL )) {
2517 EnterCriticalSection( &csWSgetXXXbyYYY );
2518 if( (serv = getservbyport(port, (proto) ? local_buffer : NULL)) != NULL ) {
2519 if( WS_dup_se(serv, dup_flag) )
2521 else SetLastError(WSAENOBUFS);
2524 MESSAGE("service on port %lu protocol %s not found; You might want to add "
2525 "this to /etc/services\n", (unsigned long)ntohl(port),
2526 proto ? debugstr_a(local_buffer) : "*");
2527 SetLastError(WSANO_DATA);
2529 LeaveCriticalSection( &csWSgetXXXbyYYY );
2531 else SetLastError(WSAENOBUFS);
2535 /***********************************************************************
2536 * getservbyport (WINSOCK.56)
2538 SEGPTR WINAPI WINSOCK_getservbyport16(INT16 port, const char *proto)
2540 WIN_servent* retval;
2541 TRACE("%d (i.e. port %d), '%s'\n",
2542 (int)port, (int)ntohl(port), (proto)?proto:NULL_STRING);
2543 retval = __ws_getservbyport(port, proto, WS_DUP_SEGPTR);
2544 return SEGPTR_GET(retval);
2547 /***********************************************************************
2548 * getservbyport (WS2_32.56)
2550 WIN_servent* WINAPI WSOCK32_getservbyport(INT port, const char *proto)
2552 TRACE("%d (i.e. port %d), '%s'\n",
2553 (int)port, (int)ntohl(port), (proto)?proto:NULL_STRING);
2554 return __ws_getservbyport(port, proto, WS_DUP_LINEAR);
2558 /***********************************************************************
2559 * gethostname (WS2_32.57)
2561 INT WINAPI WSOCK32_gethostname(char *name, INT namelen)
2563 TRACE("name %p, len %d\n", name, namelen);
2565 if (gethostname(name, namelen) == 0)
2567 TRACE("<- '%s'\n", name);
2570 SetLastError((errno == EINVAL) ? WSAEFAULT : wsaErrno());
2571 TRACE("<- ERROR !\n");
2572 return SOCKET_ERROR;
2575 /***********************************************************************
2576 * gethostname (WINSOCK.57)
2578 INT16 WINAPI WINSOCK_gethostname16(char *name, INT16 namelen)
2580 return (INT16)WSOCK32_gethostname(name, namelen);
2584 /* ------------------------------------- Windows sockets extensions -- *
2586 * ------------------------------------------------------------------- */
2588 /***********************************************************************
2589 * WSAEnumNetworkEvents (WS2_32.36)
2591 int WINAPI WSAEnumNetworkEvents(SOCKET s, WSAEVENT hEvent, LPWSANETWORKEVENTS lpEvent)
2595 TRACE("%08x, hEvent %08x, lpEvent %08x\n", s, hEvent, (unsigned)lpEvent );
2597 SERVER_START_VAR_REQ( get_socket_event, sizeof(lpEvent->iErrorCode) )
2600 req->service = TRUE;
2602 req->c_event = hEvent;
2603 if (!(ret = SERVER_CALL()))
2605 lpEvent->lNetworkEvents = req->pmask;
2606 memcpy(lpEvent->iErrorCode, server_data_ptr(req), server_data_size(req) );
2611 SetLastError(WSAEINVAL);
2612 return SOCKET_ERROR;
2615 /***********************************************************************
2616 * WSAEventSelect (WS2_32.39)
2618 int WINAPI WSAEventSelect(SOCKET s, WSAEVENT hEvent, LONG lEvent)
2622 TRACE("%08x, hEvent %08x, event %08x\n", s, hEvent, (unsigned)lEvent );
2624 SERVER_START_REQ( set_socket_event )
2628 req->event = hEvent;
2629 ret = SERVER_CALL();
2633 SetLastError(WSAEINVAL);
2634 return SOCKET_ERROR;
2637 /***********************************************************************
2638 * WINSOCK_DoAsyncSelect
2640 VOID CALLBACK WINSOCK_DoAsyncEvent( ULONG_PTR ptr )
2642 ws_select_info *info = (ws_select_info*)ptr;
2643 unsigned int i, pmask, orphan = FALSE;
2644 int errors[FD_MAX_EVENTS];
2646 TRACE("socket %08x, event %08x\n", info->sock, info->event);
2648 SERVER_START_VAR_REQ( get_socket_event, sizeof(errors) )
2650 req->handle = info->sock;
2651 req->service = TRUE;
2652 req->s_event = info->event; /* <== avoid race conditions */
2653 req->c_event = info->event;
2654 set_error( SERVER_CALL() );
2656 memcpy( errors, server_data_ptr(req), server_data_size(req) );
2659 if ( (GetLastError() == WSAENOTSOCK) || (GetLastError() == WSAEINVAL) )
2661 /* orphaned event (socket closed or something) */
2662 pmask = WS_FD_SERVEVENT;
2666 /* check for accepted sockets that needs to inherit WSAAsyncSelect */
2667 if (pmask & WS_FD_SERVEVENT) {
2669 for (q=0; q<WS_ACCEPT_QUEUE; q++)
2670 if (accept_old[q] == info->sock) {
2671 /* there's only one service thread per process, no lock necessary */
2672 HANDLE as = accept_new[q];
2676 WSAAsyncSelect(as, info->hWnd, info->uMsg, info->lEvent);
2679 pmask &= ~WS_FD_SERVEVENT;
2681 /* dispatch network events */
2682 for (i=0; i<FD_MAX_EVENTS; i++)
2683 if (pmask & (1<<i)) {
2684 TRACE("post: event bit %d, error %d\n", i, errors[i]);
2685 PostMessageA(info->hWnd, info->uMsg, info->sock,
2686 WSAMAKESELECTREPLY(1<<i, errors[i]));
2691 TRACE("orphaned event, self-destructing\n");
2692 /* SERVICE_Delete closes the event object */
2693 SERVICE_Delete( info->service );
2698 /***********************************************************************
2699 * WSAAsyncSelect (WS2_32.101)
2701 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, LONG lEvent)
2703 int fd = _get_sock_fd(s);
2705 TRACE("%04x, hWnd %04x, uMsg %08x, event %08x\n",
2706 (SOCKET16)s, (HWND16)hWnd, uMsg, (unsigned)lEvent );
2712 ws_select_info *info = (ws_select_info*)WS_ALLOC(sizeof(ws_select_info));
2715 HANDLE hObj = CreateEventA( NULL, TRUE, FALSE, NULL );
2722 info->lEvent = lEvent;
2723 info->service = SERVICE_AddObject( hObj, WINSOCK_DoAsyncEvent, (ULONG_PTR)info );
2725 err = WSAEventSelect( s, hObj, lEvent | WS_FD_SERVEVENT );
2727 /* SERVICE_Delete closes the event object */
2728 SERVICE_Delete( info->service );
2733 return 0; /* success */
2735 else SetLastError(WSAENOBUFS);
2739 WSAEventSelect(s, 0, 0);
2743 else SetLastError(WSAEINVAL);
2744 return SOCKET_ERROR;
2747 /***********************************************************************
2748 * WSAAsyncSelect (WINSOCK.101)
2750 INT16 WINAPI WSAAsyncSelect16(SOCKET16 s, HWND16 hWnd, UINT16 wMsg, LONG lEvent)
2752 return (INT16)WSAAsyncSelect( s, hWnd, wMsg, lEvent );
2755 /***********************************************************************
2756 * WSARecvEx (WINSOCK.1107)
2758 * See description for WSARecvEx()
2760 INT16 WINAPI WSARecvEx16(SOCKET16 s, char *buf, INT16 len, INT16 *flags) {
2761 FIXME("(WSARecvEx16) partial packet return value not set \n");
2763 return WINSOCK_recv16(s, buf, len, *flags);
2767 /***********************************************************************
2768 * WSACreateEvent (WS2_32.31)
2771 WSAEVENT WINAPI WSACreateEvent(void)
2773 /* Create a manual-reset event, with initial state: unsignealed */
2776 return CreateEventA(NULL, TRUE, FALSE, NULL);
2779 /***********************************************************************
2780 * WSACloseEvent (WS2_32.29)
2783 BOOL WINAPI WSACloseEvent(WSAEVENT event)
2785 TRACE ("event=0x%x\n", event);
2787 return CloseHandle(event);
2790 /***********************************************************************
2791 * WSASocketA (WS2_32.78)
2794 SOCKET WINAPI WSASocketA(int af, int type, int protocol,
2795 LPWSAPROTOCOL_INFOA lpProtocolInfo,
2796 GROUP g, DWORD dwFlags)
2799 FIXME: The "advanced" parameters of WSASocketA (lpProtocolInfo,
2800 g, dwFlags) are ignored.
2803 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%lx\n",
2804 af, type, protocol, lpProtocolInfo, g, dwFlags );
2806 return ( WSOCK32_socket (af, type, protocol) );
2810 /***********************************************************************
2811 * __WSAFDIsSet (WINSOCK.151)
2813 INT16 WINAPI __WSAFDIsSet16(SOCKET16 s, ws_fd_set16 *set)
2815 int i = set->fd_count;
2817 TRACE("(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2820 if (set->fd_array[i] == s) return 1;
2824 /***********************************************************************
2825 * __WSAFDIsSet (WS2_32.151)
2827 INT WINAPI __WSAFDIsSet(SOCKET s, ws_fd_set32 *set)
2829 int i = set->fd_count;
2831 TRACE("(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2834 if (set->fd_array[i] == s) return 1;
2838 /***********************************************************************
2839 * WSAIsBlocking (WINSOCK.114)
2840 * WSAIsBlocking (WS2_32.114)
2842 BOOL WINAPI WSAIsBlocking(void)
2844 /* By default WinSock should set all its sockets to non-blocking mode
2845 * and poll in PeekMessage loop when processing "blocking" ones. This
2846 * function is supposed to tell if the program is in this loop. Our
2847 * blocking calls are truly blocking so we always return FALSE.
2849 * Note: It is allowed to call this function without prior WSAStartup().
2856 /***********************************************************************
2857 * WSACancelBlockingCall (WINSOCK.113)
2858 * WSACancelBlockingCall (WS2_32.113)
2860 INT WINAPI WSACancelBlockingCall(void)
2867 /***********************************************************************
2868 * WSASetBlockingHook (WINSOCK.109)
2870 FARPROC16 WINAPI WSASetBlockingHook16(FARPROC16 lpBlockFunc)
2872 FARPROC16 prev = (FARPROC16)blocking_hook;
2873 blocking_hook = (FARPROC)lpBlockFunc;
2874 TRACE("hook %p\n", lpBlockFunc);
2879 /***********************************************************************
2880 * WSASetBlockingHook()
2882 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
2884 FARPROC prev = blocking_hook;
2885 blocking_hook = lpBlockFunc;
2886 TRACE("hook %p\n", lpBlockFunc);
2891 /***********************************************************************
2892 * WSAUnhookBlockingHook (WINSOCK.110)
2894 INT16 WINAPI WSAUnhookBlockingHook16(void)
2896 blocking_hook = NULL;
2901 /***********************************************************************
2902 * WSAUnhookBlockingHook()
2904 INT WINAPI WSAUnhookBlockingHook(void)
2906 blocking_hook = NULL;
2911 /* ----------------------------------- end of API stuff */
2913 /* ----------------------------------- helper functions -
2915 * TODO: Merge WS_dup_..() stuff into one function that
2916 * would operate with a generic structure containing internal
2917 * pointers (via a template of some kind).
2920 static int list_size(char** l, int item_size)
2925 j += (item_size) ? item_size : strlen(l[i]) + 1;
2926 j += (i + 1) * sizeof(char*); }
2930 static int list_dup(char** l_src, char* ref, char* base, int item_size)
2932 /* base is either either equal to ref or 0 or SEGPTR */
2935 char** l_to = (char**)ref;
2938 for(j=0;l_src[j];j++) ;
2939 p += (j + 1) * sizeof(char*);
2941 { l_to[i] = base + (p - ref);
2942 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
2943 memcpy(p, l_src[i], k); p += k; }
2950 static int hostent_size(struct hostent* p_he)
2954 { size = sizeof(struct hostent);
2955 size += strlen(p_he->h_name) + 1;
2956 size += list_size(p_he->h_aliases, 0);
2957 size += list_size(p_he->h_addr_list, p_he->h_length ); }
2961 /* duplicate hostent entry
2962 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
2963 * Dito for protoent and servent.
2965 int WS_dup_he(struct hostent* p_he, int flag)
2967 /* Convert hostent structure into ws_hostent so that the data fits
2968 * into local_buffer. Internal pointers can be linear, SEGPTR, or
2969 * relative to local_buffer depending on "flag" value. Returns size
2970 * of the data copied.
2973 int size = hostent_size(p_he);
2976 char *p_name,*p_aliases,*p_addr,*p_base,*p;
2978 struct ws_hostent16 *p_to16;
2979 struct ws_hostent32 *p_to32;
2981 check_buffer_he(size);
2987 p_base = (flag & WS_DUP_OFFSET) ? NULL
2988 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2989 p += (flag & WS_DUP_SEGPTR) ?
2990 sizeof(struct ws_hostent16) : sizeof(struct ws_hostent32);
2992 strcpy(p, p_he->h_name); p += strlen(p) + 1;
2994 p += list_dup(p_he->h_aliases, p, p_base + (p - p_to), 0);
2996 list_dup(p_he->h_addr_list, p, p_base + (p - p_to), p_he->h_length);
2998 if (flag & WS_DUP_SEGPTR) /* Win16 */
3000 p_to16->h_addrtype = (INT16)p_he->h_addrtype;
3001 p_to16->h_length = (INT16)p_he->h_length;
3002 p_to16->h_name = (SEGPTR)(p_base + (p_name - p_to));
3003 p_to16->h_aliases = (SEGPTR)(p_base + (p_aliases - p_to));
3004 p_to16->h_addr_list = (SEGPTR)(p_base + (p_addr - p_to));
3005 size += (sizeof(struct ws_hostent16) - sizeof(struct hostent));
3009 p_to32->h_addrtype = p_he->h_addrtype;
3010 p_to32->h_length = p_he->h_length;
3011 p_to32->h_name = (p_base + (p_name - p_to));
3012 p_to32->h_aliases = (char **)(p_base + (p_aliases - p_to));
3013 p_to32->h_addr_list = (char **)(p_base + (p_addr - p_to));
3014 size += (sizeof(struct ws_hostent32) - sizeof(struct hostent));
3020 /* ----- protoent */
3022 static int protoent_size(struct protoent* p_pe)
3026 { size = sizeof(struct protoent);
3027 size += strlen(p_pe->p_name) + 1;
3028 size += list_size(p_pe->p_aliases, 0); }
3032 int WS_dup_pe(struct protoent* p_pe, int flag)
3034 int size = protoent_size(p_pe);
3038 struct ws_protoent16 *p_to16;
3039 struct ws_protoent32 *p_to32;
3040 char *p_name,*p_aliases,*p_base,*p;
3042 check_buffer_pe(size);
3047 p_base = (flag & WS_DUP_OFFSET) ? NULL
3048 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
3049 p += (flag & WS_DUP_SEGPTR) ?
3050 sizeof(struct ws_protoent16) : sizeof(struct ws_protoent32);
3052 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
3054 list_dup(p_pe->p_aliases, p, p_base + (p - p_to), 0);
3056 if (flag & WS_DUP_SEGPTR) /* Win16 */
3058 p_to16->p_proto = (INT16)p_pe->p_proto;
3059 p_to16->p_name = (SEGPTR)(p_base) + (p_name - p_to);
3060 p_to16->p_aliases = (SEGPTR)((p_base) + (p_aliases - p_to));
3061 size += (sizeof(struct ws_protoent16) - sizeof(struct protoent));
3065 p_to32->p_proto = p_pe->p_proto;
3066 p_to32->p_name = (p_base) + (p_name - p_to);
3067 p_to32->p_aliases = (char **)((p_base) + (p_aliases - p_to));
3068 size += (sizeof(struct ws_protoent32) - sizeof(struct protoent));
3076 static int servent_size(struct servent* p_se)
3080 { size += sizeof(struct servent);
3081 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
3082 size += list_size(p_se->s_aliases, 0); }
3086 int WS_dup_se(struct servent* p_se, int flag)
3088 int size = servent_size(p_se);
3091 char *p_name,*p_aliases,*p_proto,*p_base,*p;
3093 struct ws_servent16 *p_to16;
3094 struct ws_servent32 *p_to32;
3096 check_buffer_se(size);
3101 p_base = (flag & WS_DUP_OFFSET) ? NULL
3102 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
3103 p += (flag & WS_DUP_SEGPTR) ?
3104 sizeof(struct ws_servent16) : sizeof(struct ws_servent32);
3106 strcpy(p, p_se->s_name); p += strlen(p) + 1;
3108 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
3110 list_dup(p_se->s_aliases, p, p_base + (p - p_to), 0);
3112 if (flag & WS_DUP_SEGPTR) /* Win16 */
3114 p_to16->s_port = (INT16)p_se->s_port;
3115 p_to16->s_name = (SEGPTR)(p_base + (p_name - p_to));
3116 p_to16->s_proto = (SEGPTR)(p_base + (p_proto - p_to));
3117 p_to16->s_aliases = (SEGPTR)(p_base + (p_aliases - p_to));
3118 size += (sizeof(struct ws_servent16) - sizeof(struct servent));
3122 p_to32->s_port = p_se->s_port;
3123 p_to32->s_name = (p_base + (p_name - p_to));
3124 p_to32->s_proto = (p_base + (p_proto - p_to));
3125 p_to32->s_aliases = (char **)(p_base + (p_aliases - p_to));
3126 size += (sizeof(struct ws_servent32) - sizeof(struct servent));
3132 /* ----------------------------------- error handling */
3134 UINT16 wsaErrno(void)
3136 int loc_errno = errno;
3137 WARN("errno %d, (%s).\n", loc_errno, strerror(loc_errno));
3141 case EINTR: return WSAEINTR;
3142 case EBADF: return WSAEBADF;
3144 case EACCES: return WSAEACCES;
3145 case EFAULT: return WSAEFAULT;
3146 case EINVAL: return WSAEINVAL;
3147 case EMFILE: return WSAEMFILE;
3148 case EWOULDBLOCK: return WSAEWOULDBLOCK;
3149 case EINPROGRESS: return WSAEINPROGRESS;
3150 case EALREADY: return WSAEALREADY;
3151 case ENOTSOCK: return WSAENOTSOCK;
3152 case EDESTADDRREQ: return WSAEDESTADDRREQ;
3153 case EMSGSIZE: return WSAEMSGSIZE;
3154 case EPROTOTYPE: return WSAEPROTOTYPE;
3155 case ENOPROTOOPT: return WSAENOPROTOOPT;
3156 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
3157 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
3158 case EOPNOTSUPP: return WSAEOPNOTSUPP;
3159 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
3160 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
3161 case EADDRINUSE: return WSAEADDRINUSE;
3162 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
3163 case ENETDOWN: return WSAENETDOWN;
3164 case ENETUNREACH: return WSAENETUNREACH;
3165 case ENETRESET: return WSAENETRESET;
3166 case ECONNABORTED: return WSAECONNABORTED;
3168 case ECONNRESET: return WSAECONNRESET;
3169 case ENOBUFS: return WSAENOBUFS;
3170 case EISCONN: return WSAEISCONN;
3171 case ENOTCONN: return WSAENOTCONN;
3172 case ESHUTDOWN: return WSAESHUTDOWN;
3173 case ETOOMANYREFS: return WSAETOOMANYREFS;
3174 case ETIMEDOUT: return WSAETIMEDOUT;
3175 case ECONNREFUSED: return WSAECONNREFUSED;
3176 case ELOOP: return WSAELOOP;
3177 case ENAMETOOLONG: return WSAENAMETOOLONG;
3178 case EHOSTDOWN: return WSAEHOSTDOWN;
3179 case EHOSTUNREACH: return WSAEHOSTUNREACH;
3180 case ENOTEMPTY: return WSAENOTEMPTY;
3182 case EPROCLIM: return WSAEPROCLIM;
3185 case EUSERS: return WSAEUSERS;
3188 case EDQUOT: return WSAEDQUOT;
3191 case ESTALE: return WSAESTALE;
3194 case EREMOTE: return WSAEREMOTE;
3197 /* just in case we ever get here and there are no problems */
3200 WARN("Unknown errno %d!\n", loc_errno);
3201 return WSAEOPNOTSUPP;
3205 UINT16 wsaHerrno(int loc_errno)
3208 WARN("h_errno %d.\n", loc_errno);
3212 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
3213 case TRY_AGAIN: return WSATRY_AGAIN;
3214 case NO_RECOVERY: return WSANO_RECOVERY;
3215 case NO_DATA: return WSANO_DATA;
3216 case ENOBUFS: return WSAENOBUFS;
3220 WARN("Unknown h_errno %d!\n", loc_errno);
3221 return WSAEOPNOTSUPP;
3226 /***********************************************************************
3227 * WSARecvFrom (WSOCK32.69)
3229 INT WINAPI WSARecvFrom( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
3230 LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, struct sockaddr *lpFrom,
3231 LPINT lpFromlen, LPWSAOVERLAPPED lpOverlapped,
3232 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
3237 FIXME( "(%i,%p,%lu,%p,%p,%p,%p,%p,%p: stub\n",
3238 s, lpBuffers, dwBufferCount, lpNumberOfBytesRecvd, lpFlags,
3239 lpFrom, lpFromlen, lpOverlapped, lpCompletionRoutine );
3241 for( dwCount = 0, rc = 0; dwCount < dwBufferCount; dwCount++ )
3244 if( ( rc = WSOCK32_recvfrom(s, lpBuffers[ dwCount ].buf, (INT)lpBuffers[ dwCount ].len,
3245 (INT)*lpFlags, lpFrom, lpFromlen ) ) != 0 )