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"
98 #include "wine/port.h"
102 #include "debugtools.h"
105 DEFAULT_DEBUG_CHANNEL(winsock);
107 /* critical section to protect some non-rentrant net function */
108 extern CRITICAL_SECTION csWSgetXXXbyYYY;
110 #define DEBUG_SOCKADDR 0
111 #define dump_sockaddr(a) \
112 DPRINTF("sockaddr_in: family %d, address %s, port %d\n", \
113 ((struct sockaddr_in *)a)->sin_family, \
114 inet_ntoa(((struct sockaddr_in *)a)->sin_addr), \
115 ntohs(((struct sockaddr_in *)a)->sin_port))
117 /* ----------------------------------- internal data */
119 /* ws_... struct conversion flags */
121 #define WS_DUP_LINEAR 0x0001
122 #define WS_DUP_NATIVE 0x0000 /* not used anymore */
123 #define WS_DUP_OFFSET 0x0002 /* internal pointers are offsets */
124 #define WS_DUP_SEGPTR 0x0004 /* internal pointers are SEGPTRs */
125 /* by default, internal pointers are linear */
126 typedef struct /* WSAAsyncSelect() control struct */
128 HANDLE service, event, sock;
132 struct _WSINFO *pwsi;
135 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
136 #define WS_MAX_UDP_DATAGRAM 1024
138 #define WS_ACCEPT_QUEUE 6
140 #define WSI_BLOCKINGCALL 0x00000001 /* per-thread info flags */
141 #define WSI_BLOCKINGHOOK 0x00000002 /* 32-bit callback */
143 #define PROCFS_NETDEV_FILE "/proc/net/dev" /* Points to the file in the /proc fs
144 that lists the network devices.
145 Do we need an #ifdef LINUX for this? */
147 typedef struct _WSINFO
150 INT16 num_startup; /* reference counter */
152 INT16 last_free; /* entry in the socket table */
154 char* buffer; /* allocated from SEGPTR heap */
155 void *he; /* typecast for Win16/32 ws_hostent */
157 void *se; /* typecast for Win16/32 ws_servent */
159 void *pe; /* typecast for Win16/32 ws_protoent */
161 char* dbuffer; /* buffer for dummies (32 bytes) */
165 volatile HANDLE accept_old[WS_ACCEPT_QUEUE], accept_new[WS_ACCEPT_QUEUE];
168 /* function prototypes */
169 int WS_dup_he(LPWSINFO pwsi, struct hostent* p_he, int flag);
170 int WS_dup_pe(LPWSINFO pwsi, struct protoent* p_pe, int flag);
171 int WS_dup_se(LPWSINFO pwsi, struct servent* p_se, int flag);
173 typedef void WIN_hostent;
174 typedef void WIN_protoent;
175 typedef void WIN_servent;
177 int WSAIOCTL_GetInterfaceCount(void);
178 int WSAIOCTL_GetInterfaceName(int intNumber, char *intName);
180 UINT16 wsaErrno(void);
181 UINT16 wsaHerrno(int errnr);
183 static HANDLE _WSHeap = 0;
185 #define WS_ALLOC(size) \
186 HeapAlloc(_WSHeap, HEAP_ZERO_MEMORY, (size) )
187 #define WS_FREE(ptr) \
188 HeapFree(_WSHeap, 0, (ptr) )
190 static INT _ws_sock_ops[] =
191 { WS_SO_DEBUG, WS_SO_REUSEADDR, WS_SO_KEEPALIVE, WS_SO_DONTROUTE,
192 WS_SO_BROADCAST, WS_SO_LINGER, WS_SO_OOBINLINE, WS_SO_SNDBUF,
193 WS_SO_RCVBUF, WS_SO_ERROR, WS_SO_TYPE,
201 static int _px_sock_ops[] =
202 { SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST,
203 SO_LINGER, SO_OOBINLINE, SO_SNDBUF, SO_RCVBUF, SO_ERROR, SO_TYPE,
212 static INT _ws_tcp_ops[] = {
218 static int _px_tcp_ops[] = {
225 /* we need a special routine to handle WSA* errors */
226 static inline int sock_server_call( enum request req )
228 unsigned int res = server_call_noerr( req );
231 /* do not map WSA errors */
232 if ((res < WSABASEERR) || (res >= 0x10000000)) res = RtlNtStatusToDosError(res);
238 static int _check_ws(LPWSINFO pwsi, SOCKET s);
239 static char* _check_buffer(LPWSINFO pwsi, int size);
241 static int _get_sock_fd(SOCKET s)
243 int fd = FILE_GetUnixHandle( s, GENERIC_READ );
245 FIXME("handle %d is not a socket (GLE %ld)\n",s,GetLastError());
249 static void _enable_event(SOCKET s, unsigned int event,
250 unsigned int sstate, unsigned int cstate)
254 struct enable_socket_event_request *req = server_alloc_req( sizeof(*req), 0 );
258 req->sstate = sstate;
259 req->cstate = cstate;
260 sock_server_call( REQ_ENABLE_SOCKET_EVENT );
265 static int _is_blocking(SOCKET s)
270 struct get_socket_event_request *req = server_alloc_req( sizeof(*req), 0 );
273 req->service = FALSE;
276 sock_server_call( REQ_GET_SOCKET_EVENT );
277 ret = (req->state & WS_FD_NONBLOCKING) == 0;
283 static unsigned int _get_sock_mask(SOCKET s)
288 struct get_socket_event_request *req = server_alloc_req( sizeof(*req), 0 );
291 req->service = FALSE;
294 sock_server_call( REQ_GET_SOCKET_EVENT );
301 static void _sync_sock_state(SOCKET s)
303 /* do a dummy wineserver request in order to let
304 the wineserver run through its select loop once */
305 (void)_is_blocking(s);
308 static int _get_sock_error(SOCKET s, unsigned int bit)
313 struct get_socket_event_request *req = server_alloc_req( sizeof(*req),
314 FD_MAX_EVENTS*sizeof(int) );
316 req->service = FALSE;
319 sock_server_call( REQ_GET_SOCKET_EVENT );
320 ret = *((int *)server_data_ptr(req) + bit);
326 static WSINFO wsinfo;
328 inline static LPWSINFO WINSOCK_GetIData(void)
333 static void WINSOCK_DeleteIData(void)
335 LPWSINFO iData = WINSOCK_GetIData();
337 if( iData->flags & WSI_BLOCKINGCALL )
338 TRACE("\tinside blocking call!\n");
340 /* delete scratch buffers */
342 if( iData->buffer ) SEGPTR_FREE(iData->buffer);
343 if( iData->dbuffer ) SEGPTR_FREE(iData->dbuffer);
345 HeapFree(GetProcessHeap(), 0, iData);
348 /***********************************************************************
349 * WSOCK32_LibMain (WSOCK32.init)
351 BOOL WINAPI WSOCK32_LibMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
353 TRACE("0x%x 0x%lx %p\n", hInstDLL, fdwReason, fImpLoad);
355 case DLL_PROCESS_DETACH:
356 WINSOCK_DeleteIData();
362 /***********************************************************************
365 * Converts socket flags from Windows format.
366 * Return 1 if converted, 0 if not (error).
368 static int convert_sockopt(INT *level, INT *optname)
375 for(i=0; _ws_sock_ops[i]; i++)
376 if( _ws_sock_ops[i] == *optname ) break;
377 if( _ws_sock_ops[i] ) {
378 *optname = _px_sock_ops[i];
381 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname);
384 *level = IPPROTO_TCP;
385 for(i=0; _ws_tcp_ops[i]; i++)
386 if ( _ws_tcp_ops[i] == *optname ) break;
387 if( _ws_tcp_ops[i] ) {
388 *optname = _px_tcp_ops[i];
391 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname);
397 /* ----------------------------------- Per-thread info (or per-process?) */
399 static int wsi_strtolo(LPWSINFO pwsi, const char* name, const char* opt)
401 /* Stuff a lowercase copy of the string into the local buffer */
403 int i = strlen(name) + 2;
404 char* p = _check_buffer(pwsi, i + ((opt)?strlen(opt):0));
408 do *p++ = tolower(*name); while(*name++);
409 i = (p - (char*)(pwsi->buffer));
410 if( opt ) do *p++ = tolower(*opt); while(*opt++);
416 static fd_set* fd_set_import( fd_set* fds, LPWSINFO pwsi, void* wsfds, int* highfd, int lfd[], BOOL b32 )
418 /* translate Winsock fd set into local fd set */
422 #define wsfds16 ((ws_fd_set16*)wsfds)
423 #define wsfds32 ((ws_fd_set32*)wsfds)
427 count = b32 ? wsfds32->fd_count : wsfds16->fd_count;
429 for( i = 0; i < count; i++ )
431 int s = (b32) ? wsfds32->fd_array[i]
432 : wsfds16->fd_array[i];
433 if( _check_ws(pwsi, s) )
435 int fd = _get_sock_fd(s);
437 if( fd > *highfd ) *highfd = fd;
449 inline static int sock_error_p(int s)
451 unsigned int optval, optlen;
453 optlen = sizeof(optval);
454 getsockopt(s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
455 if (optval) WARN("\t[%i] error: %d\n", s, optval);
459 static int fd_set_export( LPWSINFO pwsi, fd_set* fds, fd_set* exceptfds, void* wsfds, int lfd[], BOOL b32 )
463 /* translate local fd set into Winsock fd set, adding
464 * errors to exceptfds (only if app requested it) */
468 #define wsfds16 ((ws_fd_set16*)wsfds)
469 #define wsfds32 ((ws_fd_set32*)wsfds)
470 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
472 for( i = 0, j = 0; i < count; i++ )
477 if( FD_ISSET(fd, fds) )
479 if ( exceptfds && sock_error_p(fd) )
481 FD_SET(fd, exceptfds);
485 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
487 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
494 if( b32 ) wsfds32->fd_count = j;
495 else wsfds16->fd_count = j;
504 static void fd_set_unimport( void* wsfds, int lfd[], BOOL b32 )
508 #define wsfds16 ((ws_fd_set16*)wsfds)
509 #define wsfds32 ((ws_fd_set32*)wsfds)
510 int i, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
512 for( i = 0; i < count; i++ )
522 static int do_block( int fd, int mask )
533 i = select( fd+1, &fds[0], &fds[1], &fds[2], NULL );
534 if (i <= 0) return -1;
537 if (FD_ISSET(fd, &fds[i]))
542 void* __ws_memalloc( int size )
544 return WS_ALLOC(size);
547 void __ws_memfree(void* ptr)
553 /* ----------------------------------- API -----
555 * Init / cleanup / error checking.
558 /***********************************************************************
559 * WSAStartup16() (WINSOCK.115)
561 * Create socket control struct, attach it to the global list and
562 * update a pointer in the task struct.
564 INT16 WINAPI WSAStartup16(UINT16 wVersionRequested, LPWSADATA lpWSAData)
566 WSADATA WINSOCK_data = { 0x0101, 0x0101,
570 #elif defined(__NetBSD__)
574 #elif defined(__FreeBSD__)
576 #elif defined(__OpenBSD__)
581 WS_MAX_SOCKETS_PER_PROCESS,
582 WS_MAX_UDP_DATAGRAM, (SEGPTR)NULL };
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;
605 pwsi = WINSOCK_GetIData();
608 /* return winsock information */
610 memcpy(lpWSAData, &WINSOCK_data, sizeof(WINSOCK_data));
612 TRACE("succeeded\n");
616 /***********************************************************************
617 * WSAStartup() (WS2_32.115)
619 INT WINAPI WSAStartup(UINT wVersionRequested, LPWSADATA lpWSAData)
621 WSADATA WINSOCK_data = { 0x0202, 0x0202,
625 #elif defined(__NetBSD__)
629 #elif defined(__FreeBSD__)
631 #elif defined(__OpenBSD__)
636 WS_MAX_SOCKETS_PER_PROCESS,
637 WS_MAX_UDP_DATAGRAM, (SEGPTR)NULL };
640 TRACE("verReq=%x\n", wVersionRequested);
642 if (LOBYTE(wVersionRequested) < 1)
643 return WSAVERNOTSUPPORTED;
645 if (!lpWSAData) return WSAEINVAL;
647 /* initialize socket heap */
651 _WSHeap = HeapCreate(HEAP_ZERO_MEMORY, 8120, 32768);
654 ERR("Fatal: failed to create WinSock heap\n");
658 if( _WSHeap == 0 ) return WSASYSNOTREADY;
660 pwsi = WINSOCK_GetIData();
663 /* return winsock information */
664 memcpy(lpWSAData, &WINSOCK_data, sizeof(WINSOCK_data));
666 /* that's the whole of the negotiation for now */
667 lpWSAData->wVersion = wVersionRequested;
669 TRACE("succeeded\n");
674 /***********************************************************************
675 * WSACleanup() (WINSOCK.116)
677 INT WINAPI WSACleanup(void)
679 LPWSINFO pwsi = WINSOCK_GetIData();
681 if( --pwsi->num_startup > 0 ) return 0;
683 WINSOCK_DeleteIData();
686 SetLastError(WSANOTINITIALISED);
691 /***********************************************************************
692 * WSAGetLastError() (WSOCK32.111)(WINSOCK.111)
694 INT WINAPI WSAGetLastError(void)
696 return GetLastError();
699 /***********************************************************************
700 * WSASetLastError() (WSOCK32.112)
702 void WINAPI WSASetLastError(INT iError) {
703 SetLastError(iError);
706 /***********************************************************************
707 * WSASetLastError16() (WINSOCK.112)
709 void WINAPI WSASetLastError16(INT16 iError)
711 WSASetLastError(iError);
714 int _check_ws(LPWSINFO pwsi, SOCKET s)
719 if( pwsi->flags & WSI_BLOCKINGCALL ) SetLastError(WSAEINPROGRESS);
720 if ( (fd = _get_sock_fd(s)) < 0 ) {
721 SetLastError(WSAENOTSOCK);
724 /* FIXME: maybe check whether fd is really a socket? */
731 char* _check_buffer(LPWSINFO pwsi, int size)
733 if( pwsi->buffer && pwsi->buflen >= size ) return pwsi->buffer;
734 else SEGPTR_FREE(pwsi->buffer);
736 pwsi->buffer = (char*)SEGPTR_ALLOC((pwsi->buflen = size));
740 struct ws_hostent* _check_buffer_he(LPWSINFO pwsi, int size)
742 if( pwsi->he && pwsi->helen >= size ) return pwsi->he;
743 else SEGPTR_FREE(pwsi->he);
745 pwsi->he = SEGPTR_ALLOC((pwsi->helen = size));
749 void* _check_buffer_se(LPWSINFO pwsi, int size)
751 if( pwsi->se && pwsi->selen >= size ) return pwsi->se;
752 else SEGPTR_FREE(pwsi->se);
754 pwsi->se = SEGPTR_ALLOC((pwsi->selen = size));
758 struct ws_protoent* _check_buffer_pe(LPWSINFO pwsi, int size)
760 if( pwsi->pe && pwsi->pelen >= size ) return pwsi->pe;
761 else SEGPTR_FREE(pwsi->pe);
763 pwsi->pe = SEGPTR_ALLOC((pwsi->pelen = size));
767 /* ----------------------------------- i/o APIs */
769 /***********************************************************************
770 * accept() (WSOCK32.1)
772 static void WSOCK32_async_accept(LPWSINFO pwsi, SOCKET s, SOCKET as)
775 /* queue socket for WSAAsyncSelect */
776 for (q=0; q<WS_ACCEPT_QUEUE; q++)
777 if (InterlockedCompareExchange((PVOID*)&pwsi->accept_old[q], (PVOID)s, (PVOID)0) == (PVOID)0)
779 if (q<WS_ACCEPT_QUEUE)
780 pwsi->accept_new[q] = as;
782 ERR("accept queue too small\n");
783 /* now signal our AsyncSelect handler */
784 _enable_event(s, WS_FD_SERVEVENT, 0, 0);
787 /**********************************************************************/
789 SOCKET WINAPI WSOCK32_accept(SOCKET s, struct sockaddr *addr,
792 LPWSINFO pwsi = WINSOCK_GetIData();
794 struct ws_sockaddr_ipx* addr2 = (struct ws_sockaddr_ipx *)addr;
797 TRACE("(%08x): socket %04x\n",
798 (unsigned)pwsi, (UINT16)s );
799 if( _check_ws(pwsi, s) )
805 int fd = _get_sock_fd(s);
808 _sync_sock_state(s); /* let wineserver notice connection */
809 /* retrieve any error codes from it */
810 SetLastError(_get_sock_error(s, FD_ACCEPT_BIT));
811 /* FIXME: care about the error? */
815 struct accept_socket_request *req = server_alloc_req( sizeof(*req), 0 );
818 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
820 sock_server_call( REQ_ACCEPT_SOCKET );
826 unsigned omask = _get_sock_mask( s );
827 int fd = _get_sock_fd( as );
828 if( getpeername(fd, addr, addrlen32) != -1 )
831 if (addr && ((struct sockaddr_ipx *)addr)->sipx_family == AF_IPX) {
832 addr = (struct sockaddr *)
833 malloc(addrlen32 ? *addrlen32 : sizeof(*addr2));
835 addrlen32 ? *addrlen32 : sizeof(*addr2));
836 addr2->sipx_family = WS_AF_IPX;
837 addr2->sipx_network = ((struct sockaddr_ipx *)addr)->sipx_network;
838 addr2->sipx_port = ((struct sockaddr_ipx *)addr)->sipx_port;
839 memcpy(addr2->sipx_node,
840 ((struct sockaddr_ipx *)addr)->sipx_node, IPX_NODE_LEN);
844 } else SetLastError(wsaErrno());
846 if (omask & WS_FD_SERVEVENT)
847 WSOCK32_async_accept(pwsi, s, as);
851 return INVALID_SOCKET;
854 /***********************************************************************
855 * accept() (WINSOCK.1)
857 SOCKET16 WINAPI WINSOCK_accept16(SOCKET16 s, struct sockaddr* addr,
860 INT addrlen32 = addrlen16 ? *addrlen16 : 0;
861 SOCKET retSocket = WSOCK32_accept( s, addr, &addrlen32 );
862 if( addrlen16 ) *addrlen16 = (INT16)addrlen32;
863 return (SOCKET16)retSocket;
866 /***********************************************************************
869 INT WINAPI WSOCK32_bind(SOCKET s, struct sockaddr *name, INT namelen)
871 LPWSINFO pwsi = WINSOCK_GetIData();
873 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
876 TRACE("(%08x): socket %04x, ptr %8x, length %d\n",
877 (unsigned)pwsi, s, (int) name, namelen);
882 if ( _check_ws(pwsi, s) )
884 int fd = _get_sock_fd(s);
885 /* FIXME: what family does this really map to on the Unix side? */
886 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_PUP)
887 ((struct ws_sockaddr_ipx *)name)->sipx_family = AF_UNSPEC;
890 ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_IPX)
892 name = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
893 memset(name, '\0', sizeof(struct sockaddr_ipx));
894 ((struct sockaddr_ipx *)name)->sipx_family = AF_IPX;
895 ((struct sockaddr_ipx *)name)->sipx_port = name2->sipx_port;
896 ((struct sockaddr_ipx *)name)->sipx_network = name2->sipx_network;
897 memcpy(((struct sockaddr_ipx *)name)->sipx_node,
898 name2->sipx_node, IPX_NODE_LEN);
899 namelen = sizeof(struct sockaddr_ipx);
902 if ( namelen >= sizeof(*name) )
904 if ( name && (((struct ws_sockaddr_in *)name)->sin_family == AF_INET
906 || ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX
910 if ( bind(fd, name, namelen) < 0 )
912 int loc_errno = errno;
913 WARN("\tfailure - errno = %i\n", errno);
917 case EBADF: SetLastError(WSAENOTSOCK); break;
918 case EADDRNOTAVAIL: SetLastError(WSAEINVAL); break;
919 default: SetLastError(wsaErrno());break;
924 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
928 return 0; /* success */
930 } else SetLastError(WSAEAFNOSUPPORT);
931 } else SetLastError(WSAEFAULT);
933 if (name && ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
941 /***********************************************************************
944 INT16 WINAPI WINSOCK_bind16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
946 return (INT16)WSOCK32_bind( s, name, namelen );
949 /***********************************************************************
950 * closesocket() (WSOCK32.3)
952 INT WINAPI WSOCK32_closesocket(SOCKET s)
954 LPWSINFO pwsi = WINSOCK_GetIData();
956 TRACE("(%08x): socket %08x\n", (unsigned)pwsi, s);
958 if( _check_ws(pwsi, s) )
966 /***********************************************************************
967 * closesocket() (WINSOCK.3)
969 INT16 WINAPI WINSOCK_closesocket16(SOCKET16 s)
971 return (INT16)WSOCK32_closesocket(s);
974 /***********************************************************************
975 * connect() (WSOCK32.4)
977 INT WINAPI WSOCK32_connect(SOCKET s, struct sockaddr *name, INT namelen)
979 LPWSINFO pwsi = WINSOCK_GetIData();
981 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
984 TRACE("(%08x): socket %04x, ptr %8x, length %d\n",
985 (unsigned)pwsi, s, (int) name, namelen);
990 if( _check_ws(pwsi, s) )
992 int fd = _get_sock_fd(s);
993 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_PUP)
994 ((struct ws_sockaddr_ipx *)name)->sipx_family = AF_UNSPEC;
996 else if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_IPX)
998 name = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
999 memset(name, '\0', sizeof(struct sockaddr_ipx));
1000 ((struct sockaddr_ipx *)name)->sipx_family = AF_IPX;
1001 ((struct sockaddr_ipx *)name)->sipx_port = name2->sipx_port;
1002 ((struct sockaddr_ipx *)name)->sipx_network = name2->sipx_network;
1003 memcpy(((struct sockaddr_ipx *)name)->sipx_node,
1004 name2->sipx_node, IPX_NODE_LEN);
1005 namelen = sizeof(struct sockaddr_ipx);
1008 if (connect(fd, name, namelen) == 0) {
1010 goto connect_success;
1012 if (errno == EINPROGRESS)
1014 /* tell wineserver that a connection is in progress */
1015 _enable_event(s, FD_CONNECT|FD_READ|FD_WRITE,
1016 WS_FD_CONNECT|WS_FD_READ|WS_FD_WRITE,
1017 WS_FD_CONNECTED|WS_FD_LISTENING);
1018 if (_is_blocking(s))
1023 _sync_sock_state(s); /* let wineserver notice connection */
1024 /* retrieve any error codes from it */
1025 result = _get_sock_error(s, FD_CONNECT_BIT);
1027 SetLastError(result);
1030 goto connect_success;
1033 else SetLastError(WSAEWOULDBLOCK);
1038 SetLastError(wsaErrno());
1043 if (name && ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
1046 return SOCKET_ERROR;
1049 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
1052 _enable_event(s, FD_CONNECT|FD_READ|FD_WRITE,
1053 WS_FD_CONNECTED|WS_FD_READ|WS_FD_WRITE,
1054 WS_FD_CONNECT|WS_FD_LISTENING);
1058 /***********************************************************************
1059 * connect() (WINSOCK.4)
1061 INT16 WINAPI WINSOCK_connect16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
1063 return (INT16)WSOCK32_connect( s, name, namelen );
1066 /***********************************************************************
1067 * getpeername() (WSOCK32.5)
1069 INT WINAPI WSOCK32_getpeername(SOCKET s, struct sockaddr *name,
1072 LPWSINFO pwsi = WINSOCK_GetIData();
1074 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
1077 TRACE("(%08x): socket: %04x, ptr %8x, ptr %8x\n",
1078 (unsigned)pwsi, s, (int) name, *namelen);
1079 if( _check_ws(pwsi, s) )
1081 int fd = _get_sock_fd(s);
1082 if (getpeername(fd, name, namelen) == 0) {
1084 if (((struct ws_sockaddr_ipx *)name)->sipx_family == AF_IPX) {
1085 name = (struct sockaddr *)
1086 malloc(namelen ? *namelen : sizeof(*name2));
1087 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
1088 name2->sipx_family = WS_AF_IPX;
1089 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
1090 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
1091 memcpy(name2->sipx_node,
1092 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
1099 SetLastError(wsaErrno());
1102 return SOCKET_ERROR;
1105 /***********************************************************************
1106 * getpeername() (WINSOCK.5)
1108 INT16 WINAPI WINSOCK_getpeername16(SOCKET16 s, struct sockaddr *name,
1111 INT namelen32 = *namelen16;
1112 INT retVal = WSOCK32_getpeername( s, name, &namelen32 );
1115 dump_sockaddr(name);
1118 *namelen16 = namelen32;
1119 return (INT16)retVal;
1122 /***********************************************************************
1123 * getsockname() (WSOCK32.6)
1125 INT WINAPI WSOCK32_getsockname(SOCKET s, struct sockaddr *name,
1128 LPWSINFO pwsi = WINSOCK_GetIData();
1130 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
1133 TRACE("(%08x): socket: %04x, ptr %8x, ptr %8x\n",
1134 (unsigned)pwsi, s, (int) name, (int) *namelen);
1135 if( _check_ws(pwsi, s) )
1137 int fd = _get_sock_fd(s);
1138 if (getsockname(fd, name, namelen) == 0) {
1140 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX) {
1141 name = (struct sockaddr *)
1142 malloc(namelen ? *namelen : sizeof(*name2));
1143 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
1144 name2->sipx_family = WS_AF_IPX;
1145 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
1146 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
1147 memcpy(name2->sipx_node,
1148 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
1155 SetLastError(wsaErrno());
1158 return SOCKET_ERROR;
1161 /***********************************************************************
1162 * getsockname() (WINSOCK.6)
1164 INT16 WINAPI WINSOCK_getsockname16(SOCKET16 s, struct sockaddr *name,
1171 INT namelen32 = *namelen16;
1172 retVal = WSOCK32_getsockname( s, name, &namelen32 );
1173 *namelen16 = namelen32;
1176 dump_sockaddr(name);
1180 else retVal = SOCKET_ERROR;
1181 return (INT16)retVal;
1185 /***********************************************************************
1186 * getsockopt() (WSOCK32.7)
1188 INT WINAPI WSOCK32_getsockopt(SOCKET s, INT level,
1189 INT optname, char *optval, INT *optlen)
1191 LPWSINFO pwsi = WINSOCK_GetIData();
1193 TRACE("(%08x): socket: %04x, opt 0x%x, ptr %8x, len %d\n",
1194 (unsigned)pwsi, s, level, (int) optval, (int) *optlen);
1195 if( _check_ws(pwsi, s) )
1197 int fd = _get_sock_fd(s);
1198 if (!convert_sockopt(&level, &optname)) {
1199 SetLastError(WSAENOPROTOOPT); /* Unknown option */
1201 if (getsockopt(fd, (int) level, optname, optval, optlen) == 0 )
1206 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1210 return SOCKET_ERROR;
1213 /***********************************************************************
1214 * getsockopt() (WINSOCK.7)
1216 INT16 WINAPI WINSOCK_getsockopt16(SOCKET16 s, INT16 level,
1217 INT16 optname, char *optval, INT16 *optlen)
1222 if( optlen ) optlen32 = *optlen; else p = NULL;
1223 retVal = WSOCK32_getsockopt( s, (UINT16)level, optname, optval, p );
1224 if( optlen ) *optlen = optlen32;
1225 return (INT16)retVal;
1228 /***********************************************************************
1229 * htonl() (WINSOCK.8)(WSOCK32.8)
1231 u_long WINAPI WINSOCK_htonl(u_long hostlong) { return( htonl(hostlong) ); }
1232 /***********************************************************************
1233 * htons() (WINSOCK.9)(WSOCK32.9)
1235 u_short WINAPI WINSOCK_htons(u_short hostshort) { return( htons(hostshort) ); }
1236 /***********************************************************************
1237 * inet_addr() (WINSOCK.10)(WSOCK32.10)
1239 u_long WINAPI WINSOCK_inet_addr(char *cp) { return( inet_addr(cp) ); }
1240 /***********************************************************************
1241 * ntohl() (WINSOCK.14)(WSOCK32.14)
1243 u_long WINAPI WINSOCK_ntohl(u_long netlong) { return( ntohl(netlong) ); }
1244 /***********************************************************************
1245 * ntohs() (WINSOCK.15)(WSOCK32.15)
1247 u_short WINAPI WINSOCK_ntohs(u_short netshort) { return( ntohs(netshort) ); }
1249 /***********************************************************************
1250 * inet_ntoa() (WINSOCK.11)(WSOCK32.11)
1252 char* WINAPI WSOCK32_inet_ntoa(struct in_addr in)
1254 /* use "buffer for dummies" here because some applications have
1255 * propensity to decode addresses in ws_hostent structure without
1256 * saving them first...
1259 LPWSINFO pwsi = WINSOCK_GetIData();
1263 char* s = inet_ntoa(in);
1266 if( pwsi->dbuffer == NULL ) {
1267 /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
1268 if((pwsi->dbuffer = (char*) SEGPTR_ALLOC(16)) == NULL )
1270 SetLastError(WSAENOBUFS);
1274 strcpy(pwsi->dbuffer, s);
1275 return pwsi->dbuffer;
1277 SetLastError(wsaErrno());
1282 /**********************************************************************/
1284 SEGPTR WINAPI WINSOCK_inet_ntoa16(struct in_addr in)
1286 char* retVal = WSOCK32_inet_ntoa(in);
1287 return SEGPTR_GET(retVal);
1291 /**********************************************************************
1295 * FIXME: Only SIO_GET_INTERFACE_LIST option implemented.
1297 INT WINAPI WSAIoctl (SOCKET s,
1298 DWORD dwIoControlCode,
1301 LPVOID lpbOutBuffer,
1303 LPDWORD lpcbBytesReturned,
1304 LPWSAOVERLAPPED lpOverlapped,
1305 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
1307 LPWSINFO pwsi = WINSOCK_GetIData();
1309 if( _check_ws(pwsi, s) )
1311 int fd = _get_sock_fd(s);
1313 switch( dwIoControlCode )
1315 case SIO_GET_INTERFACE_LIST:
1317 INTERFACE_INFO* intArray = (INTERFACE_INFO*)lpbOutBuffer;
1319 struct ifreq ifInfo;
1323 TRACE ("-> SIO_GET_INTERFACE_LIST request\n");
1325 numInt = WSAIOCTL_GetInterfaceCount();
1328 ERR ("Unable to open /proc filesystem to determine number of network interfaces!\n");
1330 WSASetLastError(WSAEINVAL);
1331 return (SOCKET_ERROR);
1334 for (i=0; i<numInt; i++)
1336 if (!WSAIOCTL_GetInterfaceName(i, ifName))
1338 ERR ("Error parsing /proc filesystem!\n");
1340 WSASetLastError(WSAEINVAL);
1341 return (SOCKET_ERROR);
1344 ifInfo.ifr_addr.sa_family = AF_INET;
1347 strcpy (ifInfo.ifr_name, ifName);
1348 if (ioctl(fd, SIOCGIFADDR, &ifInfo) < 0)
1350 ERR ("Error obtaining IP address\n");
1352 WSASetLastError(WSAEINVAL);
1353 return (SOCKET_ERROR);
1357 struct ws_sockaddr_in *ipTemp = (struct ws_sockaddr_in *)&ifInfo.ifr_addr;
1359 intArray->iiAddress.AddressIn.sin_family = AF_INET;
1360 intArray->iiAddress.AddressIn.sin_port = ipTemp->sin_port;
1361 intArray->iiAddress.AddressIn.sin_addr.ws_addr = ipTemp->sin_addr.S_un.S_addr;
1364 /* Broadcast Address */
1365 strcpy (ifInfo.ifr_name, ifName);
1366 if (ioctl(fd, SIOCGIFBRDADDR, &ifInfo) < 0)
1368 ERR ("Error obtaining Broadcast IP address\n");
1370 WSASetLastError(WSAEINVAL);
1371 return (SOCKET_ERROR);
1375 struct ws_sockaddr_in *ipTemp = (struct ws_sockaddr_in *)&ifInfo.ifr_broadaddr;
1377 intArray->iiBroadcastAddress.AddressIn.sin_family = AF_INET;
1378 intArray->iiBroadcastAddress.AddressIn.sin_port = ipTemp->sin_port;
1379 intArray->iiBroadcastAddress.AddressIn.sin_addr.ws_addr = ipTemp->sin_addr.S_un.S_addr;
1383 strcpy (ifInfo.ifr_name, ifName);
1384 if (ioctl(fd, SIOCGIFNETMASK, &ifInfo) < 0)
1386 ERR ("Error obtaining Subnet IP address\n");
1388 WSASetLastError(WSAEINVAL);
1389 return (SOCKET_ERROR);
1393 /* Trying to avoid some compile problems across platforms.
1394 (Linux, FreeBSD, Solaris...) */
1397 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
1398 intArray->iiNetmask.AddressIn.sin_port = 0;
1399 intArray->iiNetmask.AddressIn.sin_addr.ws_addr = 0;
1400 ERR ("Unable to determine Netmask on your platform!\n");
1402 struct ws_sockaddr_in *ipTemp = (struct ws_sockaddr_in *)&ifInfo.ifr_addr;
1404 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
1405 intArray->iiNetmask.AddressIn.sin_port = ipTemp->sin_port;
1406 intArray->iiNetmask.AddressIn.sin_addr.ws_addr = ipTemp->sin_addr.S_un.S_addr;
1409 struct ws_sockaddr_in *ipTemp = (struct ws_sockaddr_in *)&ifInfo.ifr_netmask;
1411 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
1412 intArray->iiNetmask.AddressIn.sin_port = ipTemp->sin_port;
1413 intArray->iiNetmask.AddressIn.sin_addr.ws_addr = ipTemp->sin_addr.S_un.S_addr;
1417 /* Socket Status Flags */
1418 strcpy(ifInfo.ifr_name, ifName);
1419 if (ioctl(fd, SIOCGIFFLAGS, &ifInfo) < 0)
1421 ERR ("Error obtaining status flags for socket!\n");
1423 WSASetLastError(WSAEINVAL);
1424 return (SOCKET_ERROR);
1428 /* FIXME - Is this the right flag to use? */
1429 intArray->iiFlags = ifInfo.ifr_flags;
1431 intArray++; /* Prepare for another interface */
1434 /* Calculate the size of the array being returned */
1435 *lpcbBytesReturned = sizeof(INTERFACE_INFO) * numInt;
1441 WARN("\tunsupported WS_IOCTL cmd (%08lx)\n", dwIoControlCode);
1443 WSASetLastError(WSAEOPNOTSUPP);
1444 return (SOCKET_ERROR);
1448 /* Function executed with no errors */
1454 WSASetLastError(WSAENOTSOCK);
1455 return (SOCKET_ERROR);
1461 Helper function for WSAIoctl - Get count of the number of interfaces
1462 by parsing /proc filesystem.
1464 int WSAIOCTL_GetInterfaceCount(void)
1467 char buf[512]; /* Size doesn't matter, something big */
1471 /* Open /proc filesystem file for network devices */
1472 procfs = fopen(PROCFS_NETDEV_FILE, "r");
1475 /* If we can't open the file, return an error */
1479 /* Omit first two lines, they are only headers */
1480 fgets(buf, sizeof buf, procfs);
1481 fgets(buf, sizeof buf, procfs);
1483 while (fgets(buf, sizeof buf, procfs))
1485 /* Each line in the file represents a network interface */
1495 Helper function for WSAIoctl - Get name of device from interface number
1496 by parsing /proc filesystem.
1498 int WSAIOCTL_GetInterfaceName(int intNumber, char *intName)
1501 char buf[512]; /* Size doesn't matter, something big */
1504 /* Open /proc filesystem file for network devices */
1505 procfs = fopen(PROCFS_NETDEV_FILE, "r");
1508 /* If we can't open the file, return an error */
1512 /* Omit first two lines, they are only headers */
1513 fgets(buf, sizeof(buf), procfs);
1514 fgets(buf, sizeof(buf), procfs);
1516 for (i=0; i<intNumber; i++)
1518 /* Skip the lines that don't interest us. */
1519 fgets(buf, sizeof(buf), procfs);
1521 fgets(buf, sizeof(buf), procfs); /* This is the line we want */
1524 /* Parse out the line, grabbing only the name of the device
1525 to the intName variable
1527 The Line comes in like this: (we only care about the device name)
1528 lo: 21970 377 0 0 0 0 0 0 21970 377 0 0 0 0 0 0
1531 while (isspace(buf[i])) /* Skip initial space(s) */
1538 if (isspace(buf[i]))
1543 if (buf[i] == ':') /* FIXME: Not sure if this block (alias detection) works properly */
1545 /* This interface could be an alias... */
1547 char *dotname = intName;
1548 *intName++ = buf[i++];
1550 while (isdigit(buf[i]))
1552 *intName++ = buf[i++];
1557 /* ... It wasn't, so back up */
1572 *intName++ = buf[i++];
1581 /***********************************************************************
1582 * ioctlsocket() (WSOCK32.12)
1584 INT WINAPI WSOCK32_ioctlsocket(SOCKET s, LONG cmd, ULONG *argp)
1586 LPWSINFO pwsi = WINSOCK_GetIData();
1588 TRACE("(%08x): socket %04x, cmd %08lx, ptr %8x\n",
1589 (unsigned)pwsi, s, cmd, (unsigned) argp);
1590 if( _check_ws(pwsi, s) )
1592 int fd = _get_sock_fd(s);
1603 if( _get_sock_mask(s) )
1605 /* AsyncSelect()'ed sockets are always nonblocking */
1610 SetLastError(WSAEINVAL);
1612 return SOCKET_ERROR;
1616 _enable_event(s, 0, WS_FD_NONBLOCKING, 0);
1618 _enable_event(s, 0, 0, WS_FD_NONBLOCKING);
1625 case WS_IOW('f',125,u_long):
1626 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
1627 SetLastError(WSAEINVAL);
1628 return SOCKET_ERROR;
1631 /* Netscape tries hard to use bogus ioctl 0x667e */
1632 WARN("\tunknown WS_IOCTL cmd (%08lx)\n", cmd);
1634 if( ioctl(fd, newcmd, (char*)argp ) == 0 )
1639 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1642 return SOCKET_ERROR;
1645 /***********************************************************************
1646 * ioctlsocket() (WINSOCK.12)
1648 INT16 WINAPI WINSOCK_ioctlsocket16(SOCKET16 s, LONG cmd, ULONG *argp)
1650 return (INT16)WSOCK32_ioctlsocket( s, cmd, argp );
1654 /***********************************************************************
1655 * listen() (WSOCK32.13)
1657 INT WINAPI WSOCK32_listen(SOCKET s, INT backlog)
1659 LPWSINFO pwsi = WINSOCK_GetIData();
1661 TRACE("(%08x): socket %04x, backlog %d\n",
1662 (unsigned)pwsi, s, backlog);
1663 if( _check_ws(pwsi, s) )
1665 int fd = _get_sock_fd(s);
1666 if (listen(fd, backlog) == 0)
1669 _enable_event(s, FD_ACCEPT,
1671 WS_FD_CONNECT|WS_FD_CONNECTED);
1674 SetLastError(wsaErrno());
1676 else SetLastError(WSAENOTSOCK);
1677 return SOCKET_ERROR;
1680 /***********************************************************************
1681 * listen() (WINSOCK.13)
1683 INT16 WINAPI WINSOCK_listen16(SOCKET16 s, INT16 backlog)
1685 return (INT16)WSOCK32_listen( s, backlog );
1689 /***********************************************************************
1690 * recv() (WSOCK32.16)
1692 INT WINAPI WSOCK32_recv(SOCKET s, char *buf, INT len, INT flags)
1694 LPWSINFO pwsi = WINSOCK_GetIData();
1696 TRACE("(%08x): socket %04x, buf %8x, len %d, "
1697 "flags %d\n", (unsigned)pwsi, s, (unsigned)buf,
1699 if( _check_ws(pwsi, s) )
1701 int fd = _get_sock_fd(s);
1704 if (_is_blocking(s))
1707 /* FIXME: OOB and exceptfds? */
1710 if ((length = recv(fd, buf, len, flags)) >= 0)
1712 TRACE(" -> %i bytes\n", length);
1715 _enable_event(s, FD_READ, 0, 0);
1718 SetLastError(wsaErrno());
1721 else SetLastError(WSAENOTSOCK);
1722 WARN(" -> ERROR\n");
1723 return SOCKET_ERROR;
1726 /***********************************************************************
1727 * recv() (WINSOCK.16)
1729 INT16 WINAPI WINSOCK_recv16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1731 return (INT16)WSOCK32_recv( s, buf, len, flags );
1735 /***********************************************************************
1736 * recvfrom() (WSOCK32.17)
1738 INT WINAPI WSOCK32_recvfrom(SOCKET s, char *buf, INT len, INT flags,
1739 struct sockaddr *from, INT *fromlen32)
1741 LPWSINFO pwsi = WINSOCK_GetIData();
1743 struct ws_sockaddr_ipx* from2 = (struct ws_sockaddr_ipx *)from;
1746 TRACE("(%08x): socket %04x, ptr %08x, "
1747 "len %d, flags %d\n", (unsigned)pwsi, s, (unsigned)buf,
1750 if( from ) dump_sockaddr(from);
1751 else DPRINTF("from = NULL\n");
1754 if( _check_ws(pwsi, s) )
1756 int fd = _get_sock_fd(s);
1759 if (_is_blocking(s))
1762 /* FIXME: OOB and exceptfds */
1765 if ((length = recvfrom(fd, buf, len, flags, from, fromlen32)) >= 0)
1767 TRACE(" -> %i bytes\n", length);
1770 if (from && ((struct sockaddr_ipx *)from)->sipx_family == AF_IPX) {
1771 from = (struct sockaddr *)
1772 malloc(fromlen32 ? *fromlen32 : sizeof(*from2));
1773 memcpy(from, from2, fromlen32 ? *fromlen32 : sizeof(*from2));
1774 from2->sipx_family = WS_AF_IPX;
1775 from2->sipx_network = ((struct sockaddr_ipx *)from)->sipx_network;
1776 from2->sipx_port = ((struct sockaddr_ipx *)from)->sipx_port;
1777 memcpy(from2->sipx_node,
1778 ((struct sockaddr_ipx *)from)->sipx_node, IPX_NODE_LEN);
1783 _enable_event(s, FD_READ, 0, 0);
1784 return (INT16)length;
1786 SetLastError(wsaErrno());
1789 else SetLastError(WSAENOTSOCK);
1790 WARN(" -> ERROR\n");
1792 if (from && ((struct sockaddr_ipx *)from)->sipx_family == AF_IPX) {
1793 from = (struct sockaddr *)
1794 malloc(fromlen32 ? *fromlen32 : sizeof(*from2));
1795 memcpy(from, from2, fromlen32 ? *fromlen32 : sizeof(*from2));
1796 from2->sipx_family = WS_AF_IPX;
1797 from2->sipx_network = ((struct sockaddr_ipx *)from)->sipx_network;
1798 from2->sipx_port = ((struct sockaddr_ipx *)from)->sipx_port;
1799 memcpy(from2->sipx_node,
1800 ((struct sockaddr_ipx *)from)->sipx_node, IPX_NODE_LEN);
1804 return SOCKET_ERROR;
1807 /***********************************************************************
1808 * recvfrom() (WINSOCK.17)
1810 INT16 WINAPI WINSOCK_recvfrom16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1811 struct sockaddr *from, INT16 *fromlen16)
1814 INT *p = &fromlen32;
1817 if( fromlen16 ) fromlen32 = *fromlen16; else p = NULL;
1818 retVal = WSOCK32_recvfrom( s, buf, len, flags, from, p );
1819 if( fromlen16 ) *fromlen16 = fromlen32;
1820 return (INT16)retVal;
1823 /***********************************************************************
1824 * select() (WINSOCK.18)(WSOCK32.18)
1826 static INT __ws_select( BOOL b32, void *ws_readfds, void *ws_writefds, void *ws_exceptfds,
1827 struct timeval *timeout )
1829 LPWSINFO pwsi = WINSOCK_GetIData();
1831 TRACE("(%08x): read %8x, write %8x, excp %8x\n",
1832 (unsigned) pwsi, (unsigned) ws_readfds, (unsigned) ws_writefds, (unsigned) ws_exceptfds);
1837 fd_set readfds, writefds, exceptfds;
1838 fd_set *p_read, *p_write, *p_except;
1839 int readfd[FD_SETSIZE], writefd[FD_SETSIZE], exceptfd[FD_SETSIZE];
1841 p_read = fd_set_import(&readfds, pwsi, ws_readfds, &highfd, readfd, b32);
1842 p_write = fd_set_import(&writefds, pwsi, ws_writefds, &highfd, writefd, b32);
1843 p_except = fd_set_import(&exceptfds, pwsi, ws_exceptfds, &highfd, exceptfd, b32);
1845 if( (highfd = select(highfd + 1, p_read, p_write, p_except, timeout)) > 0 )
1847 fd_set_export(pwsi, &readfds, p_except, ws_readfds, readfd, b32);
1848 fd_set_export(pwsi, &writefds, p_except, ws_writefds, writefd, b32);
1850 if (p_except && ws_exceptfds)
1852 #define wsfds16 ((ws_fd_set16*)ws_exceptfds)
1853 #define wsfds32 ((ws_fd_set32*)ws_exceptfds)
1854 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
1856 for (i = j = 0; i < count; i++)
1858 int fd = exceptfd[i];
1859 if( fd >= 0 && FD_ISSET(fd, &exceptfds) )
1862 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
1864 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
1866 if( fd >= 0 ) close(fd);
1870 wsfds32->fd_count = j;
1872 wsfds16->fd_count = j;
1878 fd_set_unimport(ws_readfds, readfd, b32);
1879 fd_set_unimport(ws_writefds, writefd, b32);
1880 fd_set_unimport(ws_exceptfds, exceptfd, b32);
1881 if( ws_readfds ) ((ws_fd_set32*)ws_readfds)->fd_count = 0;
1882 if( ws_writefds ) ((ws_fd_set32*)ws_writefds)->fd_count = 0;
1883 if( ws_exceptfds ) ((ws_fd_set32*)ws_exceptfds)->fd_count = 0;
1885 if( highfd == 0 ) return 0;
1886 SetLastError(wsaErrno());
1888 return SOCKET_ERROR;
1891 INT16 WINAPI WINSOCK_select16(INT16 nfds, ws_fd_set16 *ws_readfds,
1892 ws_fd_set16 *ws_writefds, ws_fd_set16 *ws_exceptfds,
1893 struct timeval *timeout)
1895 return (INT16)__ws_select( FALSE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1898 INT WINAPI WSOCK32_select(INT nfds, ws_fd_set32 *ws_readfds,
1899 ws_fd_set32 *ws_writefds, ws_fd_set32 *ws_exceptfds,
1900 struct timeval *timeout)
1902 /* struct timeval is the same for both 32- and 16-bit code */
1903 return (INT)__ws_select( TRUE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1907 /***********************************************************************
1908 * send() (WSOCK32.19)
1910 INT WINAPI WSOCK32_send(SOCKET s, char *buf, INT len, INT flags)
1912 LPWSINFO pwsi = WINSOCK_GetIData();
1914 TRACE("(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
1915 (unsigned)pwsi, s, (unsigned) buf, len, flags);
1916 if( _check_ws(pwsi, s) )
1918 int fd = _get_sock_fd(s);
1921 if (_is_blocking(s))
1924 /* FIXME: exceptfds */
1927 if ((length = send(fd, buf, len, flags)) < 0 )
1929 SetLastError(wsaErrno());
1930 if( GetLastError() == WSAEWOULDBLOCK )
1931 _enable_event(s, FD_WRITE, 0, 0);
1936 return (INT16)length;
1940 else SetLastError(WSAENOTSOCK);
1941 return SOCKET_ERROR;
1944 /***********************************************************************
1945 * send() (WINSOCK.19)
1947 INT16 WINAPI WINSOCK_send16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1949 return WSOCK32_send( s, buf, len, flags );
1952 /***********************************************************************
1953 * sendto() (WSOCK32.20)
1955 INT WINAPI WSOCK32_sendto(SOCKET s, char *buf, INT len, INT flags,
1956 struct sockaddr *to, INT tolen)
1958 LPWSINFO pwsi = WINSOCK_GetIData();
1960 struct ws_sockaddr_ipx* to2 = (struct ws_sockaddr_ipx *)to;
1963 TRACE("(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
1964 (unsigned)pwsi, s, (unsigned) buf, len, flags);
1965 if( _check_ws(pwsi, s) )
1967 int fd = _get_sock_fd(s);
1970 if (to && ((struct ws_sockaddr_ipx *)to)->sipx_family == WS_AF_PUP)
1971 ((struct ws_sockaddr_ipx *)to)->sipx_family = AF_UNSPEC;
1974 ((struct ws_sockaddr_ipx *)to)->sipx_family == WS_AF_IPX)
1976 to = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
1977 memset(to, '\0', sizeof(struct sockaddr_ipx));
1978 ((struct sockaddr_ipx *)to)->sipx_family = AF_IPX;
1979 ((struct sockaddr_ipx *)to)->sipx_port = to2->sipx_port;
1980 ((struct sockaddr_ipx *)to)->sipx_network = to2->sipx_network;
1981 memcpy(((struct sockaddr_ipx *)to)->sipx_node,
1982 to2->sipx_node, IPX_NODE_LEN);
1983 tolen = sizeof(struct sockaddr_ipx);
1986 if (_is_blocking(s))
1989 /* FIXME: exceptfds */
1992 if ((length = sendto(fd, buf, len, flags, to, tolen)) < 0 )
1994 SetLastError(wsaErrno());
1995 if( GetLastError() == WSAEWOULDBLOCK )
1996 _enable_event(s, FD_WRITE, 0, 0);
2000 if (to && ((struct sockaddr_ipx *)to)->sipx_family == AF_IPX) {
2009 else SetLastError(WSAENOTSOCK);
2011 if (to && ((struct sockaddr_ipx *)to)->sipx_family == AF_IPX) {
2015 return SOCKET_ERROR;
2018 /***********************************************************************
2019 * sendto() (WINSOCK.20)
2021 INT16 WINAPI WINSOCK_sendto16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
2022 struct sockaddr *to, INT16 tolen)
2024 return (INT16)WSOCK32_sendto( s, buf, len, flags, to, tolen );
2027 /***********************************************************************
2028 * setsockopt() (WSOCK32.21)
2030 INT WINAPI WSOCK32_setsockopt(SOCKET16 s, INT level, INT optname,
2031 char *optval, INT optlen)
2033 LPWSINFO pwsi = WINSOCK_GetIData();
2035 TRACE("(%08x): socket %04x, lev %d, opt 0x%x, ptr %08x, len %d\n",
2036 (unsigned)pwsi, s, level, optname, (int) optval, optlen);
2037 if( _check_ws(pwsi, s) )
2039 struct linger linger;
2040 int fd = _get_sock_fd(s);
2043 /* Is a privileged and useless operation, so we don't. */
2044 if ((optname == WS_SO_DEBUG) && (level == WS_SOL_SOCKET)) {
2045 FIXME("(%d,SOL_SOCKET,SO_DEBUG,%p(%ld)) attempted (is privileged). Ignoring.\n",s,optval,*(DWORD*)optval);
2049 if(optname == WS_SO_DONTLINGER && level == WS_SOL_SOCKET) {
2050 /* This is unique to WinSock and takes special conversion */
2051 linger.l_onoff = *((int*)optval) ? 0: 1;
2052 linger.l_linger = 0;
2054 optval = (char*)&linger;
2055 optlen = sizeof(struct linger);
2058 if (!convert_sockopt(&level, &optname)) {
2059 SetLastError(WSAENOPROTOOPT);
2061 return SOCKET_ERROR;
2063 if (optname == SO_LINGER && optval) {
2064 /* yes, uses unsigned short in both win16/win32 */
2065 linger.l_onoff = ((UINT16*)optval)[0];
2066 linger.l_linger = ((UINT16*)optval)[1];
2067 /* FIXME: what is documented behavior if SO_LINGER optval
2069 optval = (char*)&linger;
2070 optlen = sizeof(struct linger);
2071 } else if (optlen < sizeof(int)){
2072 woptval= *((INT16 *) optval);
2073 optval= (char*) &woptval;
2077 if(optname == SO_RCVBUF && *(int*)optval < 2048) {
2078 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(int*)optval );
2083 if (setsockopt(fd, level, optname, optval, optlen) == 0)
2088 SetLastError(wsaErrno());
2091 else SetLastError(WSAENOTSOCK);
2092 return SOCKET_ERROR;
2095 /***********************************************************************
2096 * setsockopt() (WINSOCK.21)
2098 INT16 WINAPI WINSOCK_setsockopt16(SOCKET16 s, INT16 level, INT16 optname,
2099 char *optval, INT16 optlen)
2101 if( !optval ) return SOCKET_ERROR;
2102 return (INT16)WSOCK32_setsockopt( s, (UINT16)level, optname, optval, optlen );
2106 /***********************************************************************
2107 * shutdown() (WSOCK32.22)
2109 INT WINAPI WSOCK32_shutdown(SOCKET s, INT how)
2111 LPWSINFO pwsi = WINSOCK_GetIData();
2113 TRACE("(%08x): socket %04x, how %i\n",
2114 (unsigned)pwsi, s, how );
2115 if( _check_ws(pwsi, s) )
2117 int fd = _get_sock_fd(s);
2120 case 0: /* drop receives */
2121 _enable_event(s, 0, 0, WS_FD_READ);
2127 case 1: /* drop sends */
2128 _enable_event(s, 0, 0, WS_FD_WRITE);
2134 case 2: /* drop all */
2139 WSAAsyncSelect( s, 0, 0, 0 );
2143 if (shutdown(fd, how) == 0)
2147 _enable_event(s, 0, 0, WS_FD_CONNECTED|WS_FD_LISTENING);
2152 SetLastError(wsaErrno());
2155 else SetLastError(WSAENOTSOCK);
2156 return SOCKET_ERROR;
2159 /***********************************************************************
2160 * shutdown() (WINSOCK.22)
2162 INT16 WINAPI WINSOCK_shutdown16(SOCKET16 s, INT16 how)
2164 return (INT16)WSOCK32_shutdown( s, how );
2168 /***********************************************************************
2169 * socket() (WSOCK32.23)
2171 SOCKET WINAPI WSOCK32_socket(INT af, INT type, INT protocol)
2173 LPWSINFO pwsi = WINSOCK_GetIData();
2176 TRACE("(%08x): af=%d type=%d protocol=%d\n",
2177 (unsigned)pwsi, af, type, protocol);
2181 /* check the socket family */
2185 case WS_AF_IPX: af = AF_IPX;
2188 case AF_UNSPEC: break;
2189 default: SetLastError(WSAEAFNOSUPPORT);
2190 return INVALID_SOCKET;
2193 /* check the socket type */
2198 case SOCK_RAW: break;
2199 default: SetLastError(WSAESOCKTNOSUPPORT);
2200 return INVALID_SOCKET;
2203 /* check the protocol type */
2204 if ( protocol < 0 ) /* don't support negative values */
2205 { SetLastError(WSAEPROTONOSUPPORT); return INVALID_SOCKET; }
2207 if ( af == AF_UNSPEC) /* did they not specify the address family? */
2211 if (type == SOCK_STREAM) { af = AF_INET; break; }
2213 if (type == SOCK_DGRAM) { af = AF_INET; break; }
2214 default: SetLastError(WSAEPROTOTYPE); return INVALID_SOCKET;
2219 struct create_socket_request *req = server_alloc_req( sizeof(*req), 0 );
2222 req->protocol = protocol;
2223 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
2224 req->inherit = TRUE;
2225 sock_server_call( REQ_CREATE_SOCKET );
2229 if ( ((int) ret) >= 0)
2231 TRACE("\tcreated %04x\n", ret );
2235 if (GetLastError() == WSAEACCES) /* raw socket denied */
2237 if (type == SOCK_RAW)
2238 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, will fail unless running as root\n");
2240 MESSAGE("WS_SOCKET: not enough privileges to create socket, try running as root\n");
2241 SetLastError(WSAESOCKTNOSUPPORT);
2245 WARN("\t\tfailed!\n");
2246 return INVALID_SOCKET;
2249 /***********************************************************************
2250 * socket() (WINSOCK.23)
2252 SOCKET16 WINAPI WINSOCK_socket16(INT16 af, INT16 type, INT16 protocol)
2254 return (SOCKET16)WSOCK32_socket( af, type, protocol );
2258 /* ----------------------------------- DNS services
2260 * IMPORTANT: 16-bit API structures have SEGPTR pointers inside them.
2261 * Also, we have to use wsock32 stubs to convert structures and
2262 * error codes from Unix to WSA, hence there is no direct mapping in
2263 * the relay32/wsock32.spec.
2266 static char* NULL_STRING = "NULL";
2268 /***********************************************************************
2269 * gethostbyaddr() (WINSOCK.51)(WSOCK32.51)
2271 static WIN_hostent* __ws_gethostbyaddr(const char *addr, int len, int type, int dup_flag)
2273 WIN_hostent *retval = NULL;
2274 LPWSINFO pwsi = WINSOCK_GetIData();
2278 struct hostent* host;
2279 #if HAVE_LINUX_GETHOSTBYNAME_R_6
2282 struct hostent hostentry;
2285 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2287 int res = gethostbyaddr_r(addr, len, type,
2288 &hostentry, extrabuf, ebufsize, &host, &locerr);
2289 if( res != ERANGE) break;
2291 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2293 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2295 EnterCriticalSection( &csWSgetXXXbyYYY );
2296 host = gethostbyaddr(addr, len, type);
2297 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2301 if( WS_dup_he(pwsi, host, dup_flag) )
2302 retval = (WIN_hostent*)(pwsi->he);
2304 SetLastError(WSAENOBUFS);
2306 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2307 HeapFree(GetProcessHeap(),0,extrabuf);
2309 LeaveCriticalSection( &csWSgetXXXbyYYY );
2315 SEGPTR WINAPI WINSOCK_gethostbyaddr16(const char *addr, INT16 len, INT16 type)
2317 WIN_hostent* retval;
2318 TRACE("ptr %08x, len %d, type %d\n",
2319 (unsigned) addr, len, type);
2320 retval = __ws_gethostbyaddr( addr, len, type, WS_DUP_SEGPTR );
2321 return SEGPTR_GET(retval);
2324 WIN_hostent* WINAPI WSOCK32_gethostbyaddr(const char *addr, INT len,
2327 TRACE("ptr %08x, len %d, type %d\n",
2328 (unsigned) addr, len, type);
2329 return __ws_gethostbyaddr(addr, len, type, WS_DUP_LINEAR);
2332 /***********************************************************************
2333 * gethostbyname() (WINSOCK.52)(WSOCK32.52)
2335 static WIN_hostent * __ws_gethostbyname(const char *name, int dup_flag)
2337 WIN_hostent *retval = NULL;
2338 LPWSINFO pwsi = WINSOCK_GetIData();
2342 struct hostent* host;
2343 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2346 struct hostent hostentry;
2347 int locerr = ENOBUFS;
2349 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2351 int res = gethostbyname_r(name, &hostentry, extrabuf, ebufsize, &host, &locerr);
2352 if( res != ERANGE) break;
2354 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2356 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2358 EnterCriticalSection( &csWSgetXXXbyYYY );
2359 host = gethostbyname(name);
2360 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2364 if( WS_dup_he(pwsi, host, dup_flag) )
2365 retval = (WIN_hostent*)(pwsi->he);
2366 else SetLastError(WSAENOBUFS);
2368 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2369 HeapFree(GetProcessHeap(),0,extrabuf);
2371 LeaveCriticalSection( &csWSgetXXXbyYYY );
2377 SEGPTR WINAPI WINSOCK_gethostbyname16(const char *name)
2379 WIN_hostent* retval;
2380 TRACE("%s\n", (name)?name:NULL_STRING);
2381 retval = __ws_gethostbyname( name, WS_DUP_SEGPTR );
2382 return SEGPTR_GET(retval);
2385 WIN_hostent* WINAPI WSOCK32_gethostbyname(const char* name)
2387 TRACE("%s\n", (name)?name:NULL_STRING);
2388 return __ws_gethostbyname( name, WS_DUP_LINEAR );
2392 /***********************************************************************
2393 * getprotobyname() (WINSOCK.53)(WSOCK32.53)
2395 static WIN_protoent* __ws_getprotobyname(const char *name, int dup_flag)
2397 WIN_protoent* retval = NULL;
2398 LPWSINFO pwsi = WINSOCK_GetIData();
2402 struct protoent* proto;
2403 EnterCriticalSection( &csWSgetXXXbyYYY );
2404 if( (proto = getprotobyname(name)) != NULL )
2406 if( WS_dup_pe(pwsi, proto, dup_flag) )
2407 retval = (WIN_protoent*)(pwsi->pe);
2408 else SetLastError(WSAENOBUFS);
2411 MESSAGE("protocol %s not found; You might want to add "
2412 "this to /etc/protocols\n", debugstr_a(name) );
2413 SetLastError(WSANO_DATA);
2415 LeaveCriticalSection( &csWSgetXXXbyYYY );
2416 } else SetLastError(WSANOTINITIALISED);
2420 SEGPTR WINAPI WINSOCK_getprotobyname16(const char *name)
2422 WIN_protoent* retval;
2423 TRACE("%s\n", (name)?name:NULL_STRING);
2424 retval = __ws_getprotobyname(name, WS_DUP_SEGPTR);
2425 return SEGPTR_GET(retval);
2428 WIN_protoent* WINAPI WSOCK32_getprotobyname(const char* name)
2430 TRACE("%s\n", (name)?name:NULL_STRING);
2431 return __ws_getprotobyname(name, WS_DUP_LINEAR);
2435 /***********************************************************************
2436 * getprotobynumber() (WINSOCK.54)(WSOCK32.54)
2438 static WIN_protoent* __ws_getprotobynumber(int number, int dup_flag)
2440 WIN_protoent* retval = NULL;
2441 LPWSINFO pwsi = WINSOCK_GetIData();
2445 struct protoent* proto;
2446 EnterCriticalSection( &csWSgetXXXbyYYY );
2447 if( (proto = getprotobynumber(number)) != NULL )
2449 if( WS_dup_pe(pwsi, proto, dup_flag) )
2450 retval = (WIN_protoent*)(pwsi->pe);
2451 else SetLastError(WSAENOBUFS);
2454 MESSAGE("protocol number %d not found; You might want to add "
2455 "this to /etc/protocols\n", number );
2456 SetLastError(WSANO_DATA);
2458 LeaveCriticalSection( &csWSgetXXXbyYYY );
2459 } else SetLastError(WSANOTINITIALISED);
2463 SEGPTR WINAPI WINSOCK_getprotobynumber16(INT16 number)
2465 WIN_protoent* retval;
2466 TRACE("%i\n", number);
2467 retval = __ws_getprotobynumber(number, WS_DUP_SEGPTR);
2468 return SEGPTR_GET(retval);
2471 WIN_protoent* WINAPI WSOCK32_getprotobynumber(INT number)
2473 TRACE("%i\n", number);
2474 return __ws_getprotobynumber(number, WS_DUP_LINEAR);
2478 /***********************************************************************
2479 * getservbyname() (WINSOCK.55)(WSOCK32.55)
2481 static WIN_servent* __ws_getservbyname(const char *name, const char *proto, int dup_flag)
2483 WIN_servent* retval = NULL;
2484 LPWSINFO pwsi = WINSOCK_GetIData();
2488 struct servent* serv;
2489 int i = wsi_strtolo( pwsi, name, proto );
2492 EnterCriticalSection( &csWSgetXXXbyYYY );
2493 serv = getservbyname(pwsi->buffer,
2494 proto ? (pwsi->buffer + i) : NULL);
2497 if( WS_dup_se(pwsi, serv, dup_flag) )
2498 retval = (WIN_servent*)(pwsi->se);
2499 else SetLastError(WSAENOBUFS);
2502 MESSAGE("service %s protocol %s not found; You might want to add "
2503 "this to /etc/services\n", debugstr_a(pwsi->buffer),
2504 proto ? debugstr_a(pwsi->buffer+i):"*");
2505 SetLastError(WSANO_DATA);
2507 LeaveCriticalSection( &csWSgetXXXbyYYY );
2509 else SetLastError(WSAENOBUFS);
2510 } else SetLastError(WSANOTINITIALISED);
2514 SEGPTR WINAPI WINSOCK_getservbyname16(const char *name, const char *proto)
2516 WIN_servent* retval;
2517 TRACE("'%s', '%s'\n",
2518 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
2519 retval = __ws_getservbyname(name, proto, WS_DUP_SEGPTR);
2520 return SEGPTR_GET(retval);
2523 WIN_servent* WINAPI WSOCK32_getservbyname(const char *name, const char *proto)
2525 TRACE("'%s', '%s'\n",
2526 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
2527 return __ws_getservbyname(name, proto, WS_DUP_LINEAR);
2531 /***********************************************************************
2532 * getservbyport() (WINSOCK.56)(WSOCK32.56)
2534 static WIN_servent* __ws_getservbyport(int port, const char* proto, int dup_flag)
2536 WIN_servent* retval = NULL;
2537 LPWSINFO pwsi = WINSOCK_GetIData();
2541 struct servent* serv;
2542 if (!proto || wsi_strtolo( pwsi, proto, NULL )) {
2543 EnterCriticalSection( &csWSgetXXXbyYYY );
2544 if( (serv = getservbyport(port, (proto) ? pwsi->buffer : NULL)) != NULL ) {
2545 if( WS_dup_se(pwsi, serv, dup_flag) )
2546 retval = (WIN_servent*)(pwsi->se);
2547 else SetLastError(WSAENOBUFS);
2550 MESSAGE("service on port %lu protocol %s not found; You might want to add "
2551 "this to /etc/services\n", (unsigned long)ntohl(port),
2552 proto ? debugstr_a(pwsi->buffer) : "*");
2553 SetLastError(WSANO_DATA);
2555 LeaveCriticalSection( &csWSgetXXXbyYYY );
2557 else SetLastError(WSAENOBUFS);
2558 } else SetLastError(WSANOTINITIALISED);
2562 SEGPTR WINAPI WINSOCK_getservbyport16(INT16 port, const char *proto)
2564 WIN_servent* retval;
2565 TRACE("%d (i.e. port %d), '%s'\n",
2566 (int)port, (int)ntohl(port), (proto)?proto:NULL_STRING);
2567 retval = __ws_getservbyport(port, proto, WS_DUP_SEGPTR);
2568 return SEGPTR_GET(retval);
2571 WIN_servent* WINAPI WSOCK32_getservbyport(INT port, const char *proto)
2573 TRACE("%d (i.e. port %d), '%s'\n",
2574 (int)port, (int)ntohl(port), (proto)?proto:NULL_STRING);
2575 return __ws_getservbyport(port, proto, WS_DUP_LINEAR);
2579 /***********************************************************************
2580 * gethostname() (WSOCK32.57)
2582 INT WINAPI WSOCK32_gethostname(char *name, INT namelen)
2584 LPWSINFO pwsi = WINSOCK_GetIData();
2586 TRACE("(%08x): name %s, len %d\n",
2587 (unsigned)pwsi, (name)?name:NULL_STRING, namelen);
2590 if (gethostname(name, namelen) == 0) return 0;
2591 SetLastError((errno == EINVAL) ? WSAEFAULT : wsaErrno());
2593 return SOCKET_ERROR;
2596 /***********************************************************************
2597 * gethostname() (WINSOCK.57)
2599 INT16 WINAPI WINSOCK_gethostname16(char *name, INT16 namelen)
2601 return (INT16)WSOCK32_gethostname(name, namelen);
2605 /* ------------------------------------- Windows sockets extensions -- *
2607 * ------------------------------------------------------------------- */
2609 /***********************************************************************
2610 * WSAEnumNetworkEvents
2612 int WINAPI WSAEnumNetworkEvents(SOCKET s, WSAEVENT hEvent, LPWSANETWORKEVENTS lpEvent)
2614 LPWSINFO pwsi = WINSOCK_GetIData();
2616 TRACE("(%08x): %08x, hEvent %08x, lpEvent %08x\n",
2617 (unsigned)pwsi, s, hEvent, (unsigned)lpEvent );
2618 if( _check_ws(pwsi, s) )
2622 struct get_socket_event_request *req = server_alloc_req( sizeof(*req),
2623 sizeof(lpEvent->iErrorCode) );
2625 req->service = TRUE;
2627 req->c_event = hEvent;
2628 sock_server_call( REQ_GET_SOCKET_EVENT );
2629 lpEvent->lNetworkEvents = req->pmask;
2630 memcpy(lpEvent->iErrorCode, server_data_ptr(req), server_data_size(req) );
2635 else SetLastError(WSAEINVAL);
2636 return SOCKET_ERROR;
2639 /***********************************************************************
2642 int WINAPI WSAEventSelect(SOCKET s, WSAEVENT hEvent, LONG lEvent)
2644 LPWSINFO pwsi = WINSOCK_GetIData();
2646 TRACE("(%08x): %08x, hEvent %08x, event %08x\n",
2647 (unsigned)pwsi, s, hEvent, (unsigned)lEvent );
2648 if( _check_ws(pwsi, s) )
2652 struct set_socket_event_request *req = server_alloc_req( sizeof(*req), 0 );
2655 req->event = hEvent;
2656 sock_server_call( REQ_SET_SOCKET_EVENT );
2661 else SetLastError(WSAEINVAL);
2662 return SOCKET_ERROR;
2665 /***********************************************************************
2666 * WSAAsyncSelect() (WINSOCK.101)(WSOCK32.101)
2669 VOID CALLBACK WINSOCK_DoAsyncEvent( ULONG_PTR ptr )
2671 ws_select_info *info = (ws_select_info*)ptr;
2672 LPWSINFO pwsi = info->pwsi;
2673 unsigned int i, pmask, orphan = FALSE;
2674 int errors[FD_MAX_EVENTS];
2676 TRACE("socket %08x, event %08x\n", info->sock, info->event);
2680 struct get_socket_event_request *req = server_alloc_req( sizeof(*req), sizeof(errors) );
2681 req->handle = info->sock;
2682 req->service = TRUE;
2683 req->s_event = info->event; /* <== avoid race conditions */
2684 req->c_event = info->event;
2685 sock_server_call( REQ_GET_SOCKET_EVENT );
2687 memcpy( errors, server_data_ptr(req), server_data_size(req) );
2690 if ( (GetLastError() == WSAENOTSOCK) || (GetLastError() == WSAEINVAL) )
2692 /* orphaned event (socket closed or something) */
2693 pmask = WS_FD_SERVEVENT;
2697 /* check for accepted sockets that needs to inherit WSAAsyncSelect */
2698 if (pmask & WS_FD_SERVEVENT) {
2700 for (q=0; q<WS_ACCEPT_QUEUE; q++)
2701 if (pwsi->accept_old[q] == info->sock) {
2702 /* there's only one service thread per pwsi, no lock necessary */
2703 HANDLE as = pwsi->accept_new[q];
2705 pwsi->accept_new[q] = 0;
2706 pwsi->accept_old[q] = 0;
2707 WSAAsyncSelect(as, info->hWnd, info->uMsg, info->lEvent);
2710 pmask &= ~WS_FD_SERVEVENT;
2712 /* dispatch network events */
2713 for (i=0; i<FD_MAX_EVENTS; i++)
2714 if (pmask & (1<<i)) {
2715 TRACE("post: event bit %d, error %d\n", i, errors[i]);
2716 PostMessageA(info->hWnd, info->uMsg, info->sock,
2717 WSAMAKESELECTREPLY(1<<i, errors[i]));
2722 TRACE("orphaned event, self-destructing\n");
2723 /* SERVICE_Delete closes the event object */
2724 SERVICE_Delete( info->service );
2729 /**********************************************************************/
2731 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, LONG lEvent)
2733 LPWSINFO pwsi = WINSOCK_GetIData();
2735 TRACE("(%08x): %04x, hWnd %04x, uMsg %08x, event %08x\n",
2736 (unsigned)pwsi, (SOCKET16)s, (HWND16)hWnd, uMsg, (unsigned)lEvent );
2737 if( _check_ws(pwsi, s) )
2741 ws_select_info *info = (ws_select_info*)WS_ALLOC(sizeof(ws_select_info));
2744 HANDLE hObj = CreateEventA( NULL, TRUE, FALSE, NULL );
2751 info->lEvent = lEvent;
2753 info->service = SERVICE_AddObject( hObj, WINSOCK_DoAsyncEvent, (ULONG_PTR)info );
2755 err = WSAEventSelect( s, hObj, lEvent | WS_FD_SERVEVENT );
2757 /* SERVICE_Delete closes the event object */
2758 SERVICE_Delete( info->service );
2763 return 0; /* success */
2765 else SetLastError(WSAENOBUFS);
2769 WSAEventSelect(s, 0, 0);
2773 else SetLastError(WSAEINVAL);
2774 return SOCKET_ERROR;
2777 /**********************************************************************/
2779 INT16 WINAPI WSAAsyncSelect16(SOCKET16 s, HWND16 hWnd, UINT16 wMsg, LONG lEvent)
2781 return (INT16)WSAAsyncSelect( s, hWnd, wMsg, lEvent );
2784 /***********************************************************************
2785 * WSARecvEx16() (WINSOCK.1107)
2787 * See description for WSARecvEx()
2789 INT16 WINAPI WSARecvEx16(SOCKET16 s, char *buf, INT16 len, INT16 *flags) {
2790 FIXME("(WSARecvEx16) partial packet return value not set \n");
2792 return WINSOCK_recv16(s, buf, len, *flags);
2796 /***********************************************************************
2797 * WSACreateEvent() (WS2_32.???)
2800 WSAEVENT WINAPI WSACreateEvent(void)
2802 /* Create a manual-reset event, with initial state: unsignealed */
2805 return CreateEventA(NULL, TRUE, FALSE, NULL);
2808 /***********************************************************************
2809 * WSACloseEvent() (WS2_32.???)
2812 BOOL WINAPI WSACloseEvent(WSAEVENT event)
2814 TRACE ("event=0x%x\n", event);
2816 return CloseHandle(event);
2819 /***********************************************************************
2820 * WSASocketA() (WS2_32.???)
2823 SOCKET WINAPI WSASocketA(int af, int type, int protocol,
2824 LPWSAPROTOCOL_INFOA lpProtocolInfo,
2825 GROUP g, DWORD dwFlags)
2828 FIXME: The "advanced" parameters of WSASocketA (lpProtocolInfo,
2829 g, dwFlags) are ignored.
2832 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%lx\n",
2833 af, type, protocol, lpProtocolInfo, g, dwFlags );
2835 return ( WSOCK32_socket (af, type, protocol) );
2839 /***********************************************************************
2840 * __WSAFDIsSet() (WINSOCK.151)
2842 INT16 WINAPI __WSAFDIsSet16(SOCKET16 s, ws_fd_set16 *set)
2844 int i = set->fd_count;
2846 TRACE("(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2849 if (set->fd_array[i] == s) return 1;
2853 /***********************************************************************
2854 * __WSAFDIsSet() (WSOCK32.151)
2856 INT WINAPI __WSAFDIsSet(SOCKET s, ws_fd_set32 *set)
2858 int i = set->fd_count;
2860 TRACE("(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2863 if (set->fd_array[i] == s) return 1;
2867 /***********************************************************************
2868 * WSAIsBlocking() (WINSOCK.114)(WSOCK32.114)
2870 BOOL WINAPI WSAIsBlocking(void)
2872 /* By default WinSock should set all its sockets to non-blocking mode
2873 * and poll in PeekMessage loop when processing "blocking" ones. This
2874 * function is supposed to tell if the program is in this loop. Our
2875 * blocking calls are truly blocking so we always return FALSE.
2877 * Note: It is allowed to call this function without prior WSAStartup().
2884 /***********************************************************************
2885 * WSACancelBlockingCall() (WINSOCK.113)(WSOCK32.113)
2887 INT WINAPI WSACancelBlockingCall(void)
2889 LPWSINFO pwsi = WINSOCK_GetIData();
2891 TRACE("(%08x)\n", (unsigned)pwsi);
2893 if( pwsi ) return 0;
2894 return SOCKET_ERROR;
2898 /***********************************************************************
2899 * WSASetBlockingHook16() (WINSOCK.109)
2901 FARPROC16 WINAPI WSASetBlockingHook16(FARPROC16 lpBlockFunc)
2904 LPWSINFO pwsi = WINSOCK_GetIData();
2906 TRACE("(%08x): hook %08x\n",
2907 (unsigned)pwsi, (unsigned) lpBlockFunc);
2910 prev = (FARPROC16)pwsi->blocking_hook;
2911 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2912 pwsi->flags &= ~WSI_BLOCKINGHOOK;
2919 /***********************************************************************
2920 * WSASetBlockingHook()
2922 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
2925 LPWSINFO pwsi = WINSOCK_GetIData();
2927 TRACE("(%08x): hook %08x\n",
2928 (unsigned)pwsi, (unsigned) lpBlockFunc);
2930 prev = (FARPROC)pwsi->blocking_hook;
2931 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2932 pwsi->flags |= WSI_BLOCKINGHOOK;
2939 /***********************************************************************
2940 * WSAUnhookBlockingHook16() (WINSOCK.110)
2942 INT16 WINAPI WSAUnhookBlockingHook16(void)
2944 LPWSINFO pwsi = WINSOCK_GetIData();
2946 TRACE("(%08x)\n", (unsigned)pwsi);
2947 if( pwsi ) return (INT16)(pwsi->blocking_hook = 0);
2948 return SOCKET_ERROR;
2952 /***********************************************************************
2953 * WSAUnhookBlockingHook()
2955 INT WINAPI WSAUnhookBlockingHook(void)
2957 LPWSINFO pwsi = WINSOCK_GetIData();
2959 TRACE("(%08x)\n", (unsigned)pwsi);
2962 pwsi->blocking_hook = 0;
2963 pwsi->flags &= ~WSI_BLOCKINGHOOK;
2966 return SOCKET_ERROR;
2970 /* ----------------------------------- end of API stuff */
2972 /* ----------------------------------- helper functions -
2974 * TODO: Merge WS_dup_..() stuff into one function that
2975 * would operate with a generic structure containing internal
2976 * pointers (via a template of some kind).
2979 static int list_size(char** l, int item_size)
2984 j += (item_size) ? item_size : strlen(l[i]) + 1;
2985 j += (i + 1) * sizeof(char*); }
2989 static int list_dup(char** l_src, char* ref, char* base, int item_size)
2991 /* base is either either equal to ref or 0 or SEGPTR */
2994 char** l_to = (char**)ref;
2997 for(j=0;l_src[j];j++) ;
2998 p += (j + 1) * sizeof(char*);
3000 { l_to[i] = base + (p - ref);
3001 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
3002 memcpy(p, l_src[i], k); p += k; }
3009 static int hostent_size(struct hostent* p_he)
3013 { size = sizeof(struct hostent);
3014 size += strlen(p_he->h_name) + 1;
3015 size += list_size(p_he->h_aliases, 0);
3016 size += list_size(p_he->h_addr_list, p_he->h_length ); }
3020 /* duplicate hostent entry
3021 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
3022 * Dito for protoent and servent.
3024 int WS_dup_he(LPWSINFO pwsi, struct hostent* p_he, int flag)
3026 /* Convert hostent structure into ws_hostent so that the data fits
3027 * into pwsi->buffer. Internal pointers can be linear, SEGPTR, or
3028 * relative to pwsi->buffer depending on "flag" value. Returns size
3029 * of the data copied (also in the pwsi->buflen).
3032 int size = hostent_size(p_he);
3035 char *p_name,*p_aliases,*p_addr,*p_base,*p;
3037 struct ws_hostent16 *p_to16;
3038 struct ws_hostent32 *p_to32;
3040 _check_buffer_he(pwsi, size);
3041 p_to = (char *)pwsi->he;
3042 p_to16 = (struct ws_hostent16*)pwsi->he;
3043 p_to32 = (struct ws_hostent32*)pwsi->he;
3046 p_base = (flag & WS_DUP_OFFSET) ? NULL
3047 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
3048 p += (flag & WS_DUP_SEGPTR) ?
3049 sizeof(struct ws_hostent16) : sizeof(struct ws_hostent32);
3051 strcpy(p, p_he->h_name); p += strlen(p) + 1;
3053 p += list_dup(p_he->h_aliases, p, p_base + (p - p_to), 0);
3055 list_dup(p_he->h_addr_list, p, p_base + (p - p_to), p_he->h_length);
3057 if (flag & WS_DUP_SEGPTR) /* Win16 */
3059 p_to16->h_addrtype = (INT16)p_he->h_addrtype;
3060 p_to16->h_length = (INT16)p_he->h_length;
3061 p_to16->h_name = (SEGPTR)(p_base + (p_name - p_to));
3062 p_to16->h_aliases = (SEGPTR)(p_base + (p_aliases - p_to));
3063 p_to16->h_addr_list = (SEGPTR)(p_base + (p_addr - p_to));
3064 size += (sizeof(struct ws_hostent16) - sizeof(struct hostent));
3068 p_to32->h_addrtype = p_he->h_addrtype;
3069 p_to32->h_length = p_he->h_length;
3070 p_to32->h_name = (p_base + (p_name - p_to));
3071 p_to32->h_aliases = (char **)(p_base + (p_aliases - p_to));
3072 p_to32->h_addr_list = (char **)(p_base + (p_addr - p_to));
3073 size += (sizeof(struct ws_hostent32) - sizeof(struct hostent));
3079 /* ----- protoent */
3081 static int protoent_size(struct protoent* p_pe)
3085 { size = sizeof(struct protoent);
3086 size += strlen(p_pe->p_name) + 1;
3087 size += list_size(p_pe->p_aliases, 0); }
3091 int WS_dup_pe(LPWSINFO pwsi, struct protoent* p_pe, int flag)
3093 int size = protoent_size(p_pe);
3097 struct ws_protoent16 *p_to16;
3098 struct ws_protoent32 *p_to32;
3099 char *p_name,*p_aliases,*p_base,*p;
3101 _check_buffer_pe(pwsi, size);
3102 p_to = (char *)pwsi->pe;
3103 p_to16 = (struct ws_protoent16*)pwsi->pe;
3104 p_to32 = (struct ws_protoent32*)pwsi->pe;
3106 p_base = (flag & WS_DUP_OFFSET) ? NULL
3107 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
3108 p += (flag & WS_DUP_SEGPTR) ?
3109 sizeof(struct ws_protoent16) : sizeof(struct ws_protoent32);
3111 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
3113 list_dup(p_pe->p_aliases, p, p_base + (p - p_to), 0);
3115 if (flag & WS_DUP_SEGPTR) /* Win16 */
3117 p_to16->p_proto = (INT16)p_pe->p_proto;
3118 p_to16->p_name = (SEGPTR)(p_base) + (p_name - p_to);
3119 p_to16->p_aliases = (SEGPTR)((p_base) + (p_aliases - p_to));
3120 size += (sizeof(struct ws_protoent16) - sizeof(struct protoent));
3124 p_to32->p_proto = p_pe->p_proto;
3125 p_to32->p_name = (p_base) + (p_name - p_to);
3126 p_to32->p_aliases = (char **)((p_base) + (p_aliases - p_to));
3127 size += (sizeof(struct ws_protoent32) - sizeof(struct protoent));
3135 static int servent_size(struct servent* p_se)
3139 { size += sizeof(struct servent);
3140 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
3141 size += list_size(p_se->s_aliases, 0); }
3145 int WS_dup_se(LPWSINFO pwsi, struct servent* p_se, int flag)
3147 int size = servent_size(p_se);
3150 char *p_name,*p_aliases,*p_proto,*p_base,*p;
3152 struct ws_servent16 *p_to16;
3153 struct ws_servent32 *p_to32;
3155 _check_buffer_se(pwsi, size);
3156 p_to = (char *)pwsi->se;
3157 p_to16 = (struct ws_servent16*)pwsi->se;
3158 p_to32 = (struct ws_servent32*)pwsi->se;
3160 p_base = (flag & WS_DUP_OFFSET) ? NULL
3161 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
3162 p += (flag & WS_DUP_SEGPTR) ?
3163 sizeof(struct ws_servent16) : sizeof(struct ws_servent32);
3165 strcpy(p, p_se->s_name); p += strlen(p) + 1;
3167 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
3169 list_dup(p_se->s_aliases, p, p_base + (p - p_to), 0);
3171 if (flag & WS_DUP_SEGPTR) /* Win16 */
3173 p_to16->s_port = (INT16)p_se->s_port;
3174 p_to16->s_name = (SEGPTR)(p_base + (p_name - p_to));
3175 p_to16->s_proto = (SEGPTR)(p_base + (p_proto - p_to));
3176 p_to16->s_aliases = (SEGPTR)(p_base + (p_aliases - p_to));
3177 size += (sizeof(struct ws_servent16) - sizeof(struct servent));
3181 p_to32->s_port = p_se->s_port;
3182 p_to32->s_name = (p_base + (p_name - p_to));
3183 p_to32->s_proto = (p_base + (p_proto - p_to));
3184 p_to32->s_aliases = (char **)(p_base + (p_aliases - p_to));
3185 size += (sizeof(struct ws_servent32) - sizeof(struct servent));
3191 /* ----------------------------------- error handling */
3193 UINT16 wsaErrno(void)
3195 int loc_errno = errno;
3196 WARN("errno %d, (%s).\n", loc_errno, strerror(loc_errno));
3200 case EINTR: return WSAEINTR;
3201 case EBADF: return WSAEBADF;
3203 case EACCES: return WSAEACCES;
3204 case EFAULT: return WSAEFAULT;
3205 case EINVAL: return WSAEINVAL;
3206 case EMFILE: return WSAEMFILE;
3207 case EWOULDBLOCK: return WSAEWOULDBLOCK;
3208 case EINPROGRESS: return WSAEINPROGRESS;
3209 case EALREADY: return WSAEALREADY;
3210 case ENOTSOCK: return WSAENOTSOCK;
3211 case EDESTADDRREQ: return WSAEDESTADDRREQ;
3212 case EMSGSIZE: return WSAEMSGSIZE;
3213 case EPROTOTYPE: return WSAEPROTOTYPE;
3214 case ENOPROTOOPT: return WSAENOPROTOOPT;
3215 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
3216 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
3217 case EOPNOTSUPP: return WSAEOPNOTSUPP;
3218 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
3219 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
3220 case EADDRINUSE: return WSAEADDRINUSE;
3221 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
3222 case ENETDOWN: return WSAENETDOWN;
3223 case ENETUNREACH: return WSAENETUNREACH;
3224 case ENETRESET: return WSAENETRESET;
3225 case ECONNABORTED: return WSAECONNABORTED;
3227 case ECONNRESET: return WSAECONNRESET;
3228 case ENOBUFS: return WSAENOBUFS;
3229 case EISCONN: return WSAEISCONN;
3230 case ENOTCONN: return WSAENOTCONN;
3231 case ESHUTDOWN: return WSAESHUTDOWN;
3232 case ETOOMANYREFS: return WSAETOOMANYREFS;
3233 case ETIMEDOUT: return WSAETIMEDOUT;
3234 case ECONNREFUSED: return WSAECONNREFUSED;
3235 case ELOOP: return WSAELOOP;
3236 case ENAMETOOLONG: return WSAENAMETOOLONG;
3237 case EHOSTDOWN: return WSAEHOSTDOWN;
3238 case EHOSTUNREACH: return WSAEHOSTUNREACH;
3239 case ENOTEMPTY: return WSAENOTEMPTY;
3241 case EPROCLIM: return WSAEPROCLIM;
3244 case EUSERS: return WSAEUSERS;
3247 case EDQUOT: return WSAEDQUOT;
3250 case ESTALE: return WSAESTALE;
3253 case EREMOTE: return WSAEREMOTE;
3256 /* just in case we ever get here and there are no problems */
3259 WARN("Unknown errno %d!\n", loc_errno);
3260 return WSAEOPNOTSUPP;
3264 UINT16 wsaHerrno(int loc_errno)
3267 WARN("h_errno %d.\n", loc_errno);
3271 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
3272 case TRY_AGAIN: return WSATRY_AGAIN;
3273 case NO_RECOVERY: return WSANO_RECOVERY;
3274 case NO_DATA: return WSANO_DATA;
3275 case ENOBUFS: return WSAENOBUFS;
3279 WARN("Unknown h_errno %d!\n", loc_errno);
3280 return WSAEOPNOTSUPP;