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).
16 #include <sys/types.h>
18 #include <sys/ioctl.h>
19 #ifdef HAVE_SYS_FILIO_H
20 # include <sys/filio.h>
23 #include <sys/ioccom.h>
24 #include <sys/sockio.h>
28 # include <sys/so_ioctl.h>
31 #ifdef HAVE_SYS_PARAM_H
32 # include <sys/param.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
43 #include <sys/errno.h>
47 #ifdef HAVE_ARPA_NAMESER_H
48 # include <arpa/nameser.h>
54 #include "wine/winbase16.h"
63 #define DEBUG_SOCKADDR 0
64 #define dump_sockaddr(a) \
65 DUMP("sockaddr_in: family %d, address %s, port %d\n", \
66 ((struct sockaddr_in *)a)->sin_family, \
67 inet_ntoa(((struct sockaddr_in *)a)->sin_addr), \
68 ntohs(((struct sockaddr_in *)a)->sin_port))
72 /* ----------------------------------- internal data */
74 static HANDLE _WSHeap = 0;
75 static unsigned char* _ws_stub = NULL;
76 static LPWSINFO _wsi_list = NULL;
78 #define WS_ALLOC(size) \
79 HeapAlloc(_WSHeap, HEAP_ZERO_MEMORY, (size) )
80 #define WS_FREE(ptr) \
81 HeapFree(_WSHeap, 0, (ptr) )
83 #define WS_PTR2HANDLE(ptr) \
84 ((short)((int)(ptr) - (int)_ws_stub))
85 #define WS_HANDLE2PTR(handle) \
86 ((unsigned)((int)_ws_stub + (int)(handle)))
88 #define WSI_CHECK_RANGE(pwsi, pws) \
89 ( ((unsigned)(pws) > (unsigned)(pwsi)) && \
90 ((unsigned)(pws) < ((unsigned)(pwsi) + sizeof(WSINFO))) )
92 static INT _ws_sock_ops[] =
93 { WS_SO_DEBUG, WS_SO_REUSEADDR, WS_SO_KEEPALIVE, WS_SO_DONTROUTE,
94 WS_SO_BROADCAST, WS_SO_LINGER, WS_SO_OOBINLINE, WS_SO_SNDBUF,
95 WS_SO_RCVBUF, WS_SO_ERROR, WS_SO_TYPE, WS_SO_DONTLINGER, 0 };
96 static int _px_sock_ops[] =
97 { SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST,
98 SO_LINGER, SO_OOBINLINE, SO_SNDBUF, SO_RCVBUF, SO_ERROR, SO_TYPE,
102 static INT _ws_tcp_ops[] = {
108 static int _px_tcp_ops[] = {
115 static int _check_ws(LPWSINFO pwsi, ws_socket* pws);
116 static char* _check_buffer(LPWSINFO pwsi, int size);
118 /***********************************************************************
121 * Converts socket flags from Windows format.
123 static void convert_sockopt(INT *level, INT *optname)
130 for(i=0; _ws_sock_ops[i]; i++)
131 if( _ws_sock_ops[i] == *optname ) break;
132 if( _ws_sock_ops[i] ) *optname = _px_sock_ops[i];
133 else FIXME(winsock, "Unknown SOL_SOCKET optname %d\n", *optname);
136 *level = IPPROTO_TCP;
137 for(i=0; _ws_tcp_ops[i]; i++)
138 if ( _ws_tcp_ops[i] == *optname ) break;
139 if( _ws_tcp_ops[i] ) *optname = _px_tcp_ops[i];
140 else FIXME(winsock, "Unknown IPPROTO_TCP optname %d\n", *optname);
145 /* ----------------------------------- Per-thread info (or per-process?) */
147 static LPWSINFO wsi_find(HTASK16 hTask)
149 TDB* pTask = (TDB*)GlobalLock16(hTask);
152 if( pTask->pwsi ) return pTask->pwsi;
155 LPWSINFO pwsi = _wsi_list;
156 while( pwsi && pwsi->tid != hTask ) pwsi = pwsi->next;
158 WARN(winsock,"(pwsi=0x%08x,task=0x%04x):Loose wsi struct! \n",
159 (unsigned)pwsi, hTask );
166 static ws_socket* wsi_alloc_socket(LPWSINFO pwsi, int fd)
168 /* Initialize a new entry in the socket table */
170 if( pwsi->last_free >= 0 )
172 int i = pwsi->last_free;
174 pwsi->last_free = pwsi->sock[i].flags; /* free list */
175 pwsi->sock[i].fd = fd;
176 pwsi->sock[i].flags = 0;
177 return &pwsi->sock[i];
182 static int wsi_strtolo(LPWSINFO pwsi, const char* name, const char* opt)
184 /* Stuff a lowercase copy of the string into the local buffer */
186 int i = strlen(name) + 2;
187 char* p = _check_buffer(pwsi, i + ((opt)?strlen(opt):0));
191 do *p++ = tolower(*name); while(*name++);
192 i = (p - (char*)(pwsi->buffer));
193 if( opt ) do *p++ = tolower(*opt); while(*opt++);
199 static fd_set* fd_set_import( fd_set* fds, LPWSINFO pwsi, void* wsfds, int* highfd, BOOL b32 )
201 /* translate Winsock fd set into local fd set */
205 #define wsfds16 ((ws_fd_set16*)wsfds)
206 #define wsfds32 ((ws_fd_set32*)wsfds)
211 count = b32 ? wsfds32->fd_count : wsfds16->fd_count;
213 for( i = 0; i < count; i++ )
215 pws = (b32) ? (ws_socket*)WS_HANDLE2PTR(wsfds32->fd_array[i])
216 : (ws_socket*)WS_HANDLE2PTR(wsfds16->fd_array[i]);
217 if( _check_ws(pwsi, pws) )
219 if( pws->fd > *highfd ) *highfd = pws->fd;
220 FD_SET(pws->fd, fds);
230 __inline__ static int sock_error_p(int s)
232 unsigned int optval, optlen;
234 optlen = sizeof(optval);
235 getsockopt(s, SOL_SOCKET, SO_ERROR, &optval, &optlen);
236 if (optval) WARN(winsock, "\t[%i] error: %d\n", s, optval);
240 static int fd_set_export( LPWSINFO pwsi, fd_set* fds, fd_set* exceptfds, void* wsfds, BOOL b32 )
244 /* translate local fd set into Winsock fd set, adding
245 * errors to exceptfds (only if app requested it) */
249 #define wsfds16 ((ws_fd_set16*)wsfds)
250 #define wsfds32 ((ws_fd_set32*)wsfds)
251 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
253 for( i = 0, j = 0; i < count; i++ )
255 ws_socket *pws = (b32) ? (ws_socket*)WS_HANDLE2PTR(wsfds32->fd_array[i])
256 : (ws_socket*)WS_HANDLE2PTR(wsfds16->fd_array[i]);
257 if( _check_ws(pwsi, pws) )
261 if( FD_ISSET(fd, fds) )
263 if ( exceptfds && sock_error_p(fd) )
265 FD_SET(fd, exceptfds);
269 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
271 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
276 if( b32 ) wsfds32->fd_count = j;
277 else wsfds16->fd_count = j;
279 TRACE(winsock, "\n");
286 HANDLE16 __ws_gethandle( void* ptr )
288 return (HANDLE16)WS_PTR2HANDLE(ptr);
291 void* __ws_memalloc( int size )
293 return WS_ALLOC(size);
296 void __ws_memfree(void* ptr)
301 /* ----------------------------------- API -----
303 * Init / cleanup / error checking.
306 /***********************************************************************
307 * WSAStartup16() (WINSOCK.115)
309 * Create socket control struct, attach it to the global list and
310 * update a pointer in the task struct.
312 INT16 WINAPI WSAStartup16(UINT16 wVersionRequested, LPWSADATA lpWSAData)
314 WSADATA WINSOCK_data = { 0x0101, 0x0101,
318 #elif defined(__NetBSD__)
322 #elif defined(__FreeBSD__)
324 #elif defined(__OpenBSD__)
329 WS_MAX_SOCKETS_PER_PROCESS,
330 WS_MAX_UDP_DATAGRAM, (SEGPTR)NULL };
331 HTASK16 tid = GetCurrentTask();
334 TRACE(winsock, "verReq=%x\n", wVersionRequested);
336 if (LOBYTE(wVersionRequested) < 1 || (LOBYTE(wVersionRequested) == 1 &&
337 HIBYTE(wVersionRequested) < 1)) return WSAVERNOTSUPPORTED;
339 if (!lpWSAData) return WSAEINVAL;
341 /* initialize socket heap */
345 _WSHeap = HeapCreate(HEAP_ZERO_MEMORY, 8120, 32768);
346 if( !(_ws_stub = WS_ALLOC(0x10)) )
348 ERR(winsock,"Fatal: failed to create WinSock heap\n");
352 if( _WSHeap == 0 ) return WSASYSNOTREADY;
354 /* create socket array for this task */
356 pwsi = wsi_find(GetCurrentTask());
359 TDB* pTask = (TDB*)GlobalLock16( tid );
361 if( (pwsi = (LPWSINFO)WS_ALLOC( sizeof(WSINFO))) )
365 for( i = 0; i < WS_MAX_SOCKETS_PER_PROCESS; i++ )
367 pwsi->sock[i].fd = -1;
368 pwsi->sock[i].flags = i + 1;
370 pwsi->sock[WS_MAX_SOCKETS_PER_PROCESS - 1].flags = -1;
372 else return WSASYSNOTREADY;
374 /* add this control struct to the global list */
378 _wsi_list->prev = pwsi;
379 pwsi->next = _wsi_list;
383 else pwsi->num_startup++;
385 /* return winsock information */
387 memcpy(lpWSAData, &WINSOCK_data, sizeof(WINSOCK_data));
389 TRACE(winsock, "succeeded\n");
393 /***********************************************************************
394 * WSAStartup32() (WSOCK32.115)
396 INT WINAPI WSAStartup(UINT wVersionRequested, LPWSADATA lpWSAData)
398 return WSAStartup16( wVersionRequested, lpWSAData );
401 /***********************************************************************
402 * WSACleanup() (WINSOCK.116)
404 * Cleanup functions of varying impact.
406 void WINSOCK_Shutdown()
408 /* Called on exit(), has to remove all outstanding async DNS processes. */
410 WINSOCK_cancel_task_aops( 0, __ws_memfree );
413 INT WINSOCK_DeleteTaskWSI( TDB* pTask, LPWSINFO pwsi )
415 /* WSACleanup() backend, called on task termination as well.
416 * Real DLL would have registered its own signal handler with
417 * TaskSetSignalHandler() and waited until USIG_TERMINATION/USIG_GPF
418 * but this scheme is much more straightforward.
423 if( --pwsi->num_startup > 0 ) return 0;
425 SIGNAL_MaskAsyncEvents( TRUE );
426 WINSOCK_cancel_task_aops( pTask->hSelf, __ws_memfree );
427 SIGNAL_MaskAsyncEvents( FALSE );
429 /* unlink socket control struct */
431 if( pwsi == _wsi_list )
432 _wsi_list = pwsi->next;
434 pwsi->prev->next = pwsi->next;
435 if( pwsi->next ) pwsi->next->prev = pwsi->prev;
437 if( _wsi_list == NULL )
438 WINSOCK_Shutdown(); /* just in case */
440 if( pwsi->flags & WSI_BLOCKINGCALL )
441 TRACE(winsock,"\tinside blocking call!\n");
443 /* FIXME: aop_control() doesn't decrement pwsi->num_async_rq
445 * if( pwsi->num_async_rq )
446 * WARN(winsock,"\thave %i outstanding async ops!\n", pwsi->num_async_rq );
449 for(i = 0, j = 0, n = 0; i < WS_MAX_SOCKETS_PER_PROCESS; i++)
450 if( pwsi->sock[i].fd != -1 )
452 if( pwsi->sock[i].psop )
455 WSAAsyncSelect( (SOCKET16)WS_PTR2HANDLE(pwsi->sock + i), 0, 0, 0 );
457 close(pwsi->sock[i].fd); j++;
460 TRACE(winsock,"\tclosed %i sockets, killed %i async selects!\n", j, n);
462 /* delete scratch buffers */
464 if( pwsi->buffer ) SEGPTR_FREE(pwsi->buffer);
465 if( pwsi->dbuffer ) SEGPTR_FREE(pwsi->dbuffer);
469 memset( pwsi, 0, sizeof(WSINFO) );
474 INT WINAPI WSACleanup(void)
476 HTASK16 hTask = GetCurrentTask();
478 TRACE(winsock, "(%04x)\n", hTask );
481 LPWSINFO pwsi = wsi_find(hTask);
483 return WINSOCK_DeleteTaskWSI( (TDB*)GlobalLock16(hTask), pwsi );
487 WINSOCK_Shutdown(); /* remove all outstanding DNS requests */
492 /***********************************************************************
493 * WSAGetLastError() (WSOCK32.111)(WINSOCK.111)
495 INT WINAPI WSAGetLastError(void)
497 LPWSINFO pwsi = wsi_find(GetCurrentTask());
498 INT16 ret = (pwsi) ? pwsi->err : WSANOTINITIALISED;
500 TRACE(winsock, "(%08x) = %i\n",
501 (unsigned)pwsi, (int)ret);
505 /***********************************************************************
506 * WSASetLastError32() (WSOCK32.112)
508 void WINAPI WSASetLastError(INT iError)
510 LPWSINFO pwsi = wsi_find(GetCurrentTask());
512 TRACE(winsock, "(%08x): %d\n", (unsigned)pwsi, (int)iError);
513 if( pwsi ) pwsi->err = iError;
516 /***********************************************************************
517 * WSASetLastError16() (WINSOCK.112)
519 void WINAPI WSASetLastError16(INT16 iError)
521 WSASetLastError(iError);
524 int _check_ws(LPWSINFO pwsi, ws_socket* pws)
528 if( pwsi->flags & WSI_BLOCKINGCALL ) pwsi->err = WSAEINPROGRESS;
529 else if( WSI_CHECK_RANGE(pwsi, pws) ) return 1;
530 else pwsi->err = WSAENOTSOCK;
535 char* _check_buffer(LPWSINFO pwsi, int size)
537 if( pwsi->buffer && pwsi->buflen >= size ) return pwsi->buffer;
538 else SEGPTR_FREE(pwsi->buffer);
540 pwsi->buffer = (char*)SEGPTR_ALLOC((pwsi->buflen = size));
544 struct ws_hostent* _check_buffer_he(LPWSINFO pwsi, int size)
546 if( pwsi->he && pwsi->helen >= size ) return pwsi->he;
547 else SEGPTR_FREE(pwsi->he);
549 pwsi->he = (struct ws_hostent*)SEGPTR_ALLOC((pwsi->helen = size));
553 struct ws_servent* _check_buffer_se(LPWSINFO pwsi, int size)
555 if( pwsi->se && pwsi->selen >= size ) return pwsi->se;
556 else SEGPTR_FREE(pwsi->se);
558 pwsi->se = (struct ws_servent*)SEGPTR_ALLOC((pwsi->selen = size));
562 struct ws_protoent* _check_buffer_pe(LPWSINFO pwsi, int size)
564 if( pwsi->pe && pwsi->pelen >= size ) return pwsi->pe;
565 else SEGPTR_FREE(pwsi->pe);
567 pwsi->pe = (struct ws_protoent*)SEGPTR_ALLOC((pwsi->pelen = size));
571 /* ----------------------------------- i/o APIs */
573 /***********************************************************************
574 * accept() (WSOCK32.1)
576 SOCKET WINAPI WINSOCK_accept(SOCKET s, struct sockaddr *addr,
579 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR((SOCKET16)s);
580 LPWSINFO pwsi = wsi_find(GetCurrentTask());
582 struct ws_sockaddr_ipx* addr2 = (struct ws_sockaddr_ipx *)addr;
585 TRACE(winsock, "(%08x): socket %04x\n",
586 (unsigned)pwsi, (UINT16)s );
587 if( _check_ws(pwsi, pws) )
591 fd_flags = fcntl(pws->fd, F_GETFL, 0);
593 if( (sock = accept(pws->fd, addr, addrlen32)) >= 0 )
595 ws_socket* pnew = wsi_alloc_socket(pwsi, sock);
598 s = (SOCKET)WS_PTR2HANDLE(pnew);
599 if( pws->psop && pws->flags & WS_FD_ACCEPT )
601 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
603 /* async select the accept()'ed socket */
604 WSAAsyncSelect( s, pws->psop->hWnd, pws->psop->uMsg,
605 pws->flags & ~WS_FD_ACCEPT );
608 if (addr && ((struct sockaddr_ipx *)addr)->sipx_family == AF_IPX) {
609 addr = (struct sockaddr *)
610 malloc(addrlen32 ? *addrlen32 : sizeof(*addr2));
612 addrlen32 ? *addrlen32 : sizeof(*addr2));
613 addr2->sipx_family = WS_AF_IPX;
614 addr2->sipx_network = ((struct sockaddr_ipx *)addr)->sipx_network;
615 addr2->sipx_port = ((struct sockaddr_ipx *)addr)->sipx_port;
616 memcpy(addr2->sipx_node,
617 ((struct sockaddr_ipx *)addr)->sipx_node, IPX_NODE_LEN);
623 else pwsi->err = WSAENOBUFS;
625 else pwsi->err = wsaErrno();
628 if (addr && ((struct sockaddr_ipx *)addr)->sipx_family == AF_IPX) {
629 addr = (struct sockaddr *)
630 malloc(addrlen32 ? *addrlen32 : sizeof(*addr2));
631 memcpy(addr, addr2, addrlen32 ? *addrlen32 : sizeof(*addr2));
632 addr2->sipx_family = WS_AF_IPX;
633 addr2->sipx_network = ((struct sockaddr_ipx *)addr)->sipx_network;
634 addr2->sipx_port = ((struct sockaddr_ipx *)addr)->sipx_port;
635 memcpy(addr2->sipx_node,
636 ((struct sockaddr_ipx *)addr)->sipx_node, IPX_NODE_LEN);
640 return INVALID_SOCKET;
643 /***********************************************************************
644 * accept() (WINSOCK.1)
646 SOCKET16 WINAPI WINSOCK_accept16(SOCKET16 s, struct sockaddr* addr,
649 INT addrlen32 = addrlen16 ? *addrlen16 : 0;
650 SOCKET retSocket = WINSOCK_accept( s, addr, &addrlen32 );
651 if( addrlen16 ) *addrlen16 = (INT16)addrlen32;
652 return (SOCKET16)retSocket;
655 /***********************************************************************
658 INT WINAPI WINSOCK_bind(SOCKET s, struct sockaddr *name, INT namelen)
660 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
661 LPWSINFO pwsi = wsi_find(GetCurrentTask());
663 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
666 TRACE(winsock, "(%08x): socket %04x, ptr %8x, length %d\n",
667 (unsigned)pwsi, s, (int) name, namelen);
672 if ( _check_ws(pwsi, pws) )
674 /* FIXME: what family does this really map to on the Unix side? */
675 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_PUP)
676 ((struct ws_sockaddr_ipx *)name)->sipx_family = AF_UNSPEC;
679 ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_IPX)
681 name = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
682 memset(name, '\0', sizeof(struct sockaddr_ipx));
683 ((struct sockaddr_ipx *)name)->sipx_family = AF_IPX;
684 ((struct sockaddr_ipx *)name)->sipx_port = name2->sipx_port;
685 ((struct sockaddr_ipx *)name)->sipx_network = name2->sipx_network;
686 memcpy(((struct sockaddr_ipx *)name)->sipx_node,
687 name2->sipx_node, IPX_NODE_LEN);
688 namelen = sizeof(struct sockaddr_ipx);
691 if ( namelen >= sizeof(*name) )
693 if ( name && (((struct ws_sockaddr_in *)name)->sin_family == AF_INET
695 || ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX
699 if ( bind(pws->fd, name, namelen) < 0 )
701 int loc_errno = errno;
702 WARN(winsock, "\tfailure - errno = %i\n", errno);
706 case EBADF: pwsi->err = WSAENOTSOCK; break;
707 case EADDRNOTAVAIL: pwsi->err = WSAEINVAL; break;
708 default: pwsi->err = wsaErrno();
713 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
716 return 0; /* success */
719 else pwsi->err = WSAEAFNOSUPPORT;
721 else pwsi->err = WSAEFAULT;
723 if (name && ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
730 /***********************************************************************
733 INT16 WINAPI WINSOCK_bind16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
735 return (INT16)WINSOCK_bind( s, name, namelen );
738 /***********************************************************************
739 * closesocket() (WSOCK32.3)
741 INT WINAPI WINSOCK_closesocket(SOCKET s)
743 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
744 LPWSINFO pwsi = wsi_find(GetCurrentTask());
746 TRACE(winsock, "(%08x): socket %08x\n", (unsigned)pwsi, s);
748 if( _check_ws(pwsi, pws) )
752 if( pws->psop ) WSAAsyncSelect( s, 0, 0, 0 );
755 pws->flags = (unsigned)pwsi->last_free;
756 pwsi->last_free = pws - &pwsi->sock[0]; /* add to free list */
760 pwsi->err = (errno == EBADF) ? WSAENOTSOCK : wsaErrno();
765 /***********************************************************************
766 * closesocket() (WINSOCK.3)
768 INT16 WINAPI WINSOCK_closesocket16(SOCKET16 s)
770 return (INT16)WINSOCK_closesocket(s);
773 /***********************************************************************
774 * connect() (WSOCK32.4)
776 INT WINAPI WINSOCK_connect(SOCKET s, struct sockaddr *name, INT namelen)
778 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
779 LPWSINFO pwsi = wsi_find(GetCurrentTask());
781 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
784 TRACE(winsock, "(%08x): socket %04x, ptr %8x, length %d\n",
785 (unsigned)pwsi, s, (int) name, namelen);
790 if( _check_ws(pwsi, pws) )
792 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_PUP)
793 ((struct ws_sockaddr_ipx *)name)->sipx_family = AF_UNSPEC;
795 else if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_IPX)
797 name = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
798 memset(name, '\0', sizeof(struct sockaddr_ipx));
799 ((struct sockaddr_ipx *)name)->sipx_family = AF_IPX;
800 ((struct sockaddr_ipx *)name)->sipx_port = name2->sipx_port;
801 ((struct sockaddr_ipx *)name)->sipx_network = name2->sipx_network;
802 memcpy(((struct sockaddr_ipx *)name)->sipx_node,
803 name2->sipx_node, IPX_NODE_LEN);
804 namelen = sizeof(struct sockaddr_ipx);
807 if (connect(pws->fd, name, namelen) == 0)
809 if( pws->psop && (pws->flags & WS_FD_CONNECT) )
811 /* application did AsyncSelect() but then went
812 * ahead and called connect() without waiting for
815 * FIXME: Do we have to post a notification message
819 if( !(pws->flags & WS_FD_CONNECTED) )
821 if( pws->flags & (WS_FD_READ | WS_FD_CLOSE) )
822 EVENT_AddIO( pws->fd, EVENT_IO_READ );
824 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
825 if( pws->flags & WS_FD_WRITE )
826 EVENT_AddIO( pws->fd, EVENT_IO_WRITE );
828 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
831 pws->flags |= WS_FD_CONNECTED;
832 pws->flags &= ~(WS_FD_INACTIVE | WS_FD_CONNECT | WS_FD_LISTENING);
834 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
839 pwsi->err = (errno == EINPROGRESS) ? WSAEWOULDBLOCK : wsaErrno();
842 if (name && ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
848 /***********************************************************************
849 * connect() (WINSOCK.4)
851 INT16 WINAPI WINSOCK_connect16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
853 return (INT16)WINSOCK_connect( s, name, namelen );
856 /***********************************************************************
857 * getpeername() (WSOCK32.5)
859 INT WINAPI WINSOCK_getpeername(SOCKET s, struct sockaddr *name,
862 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
863 LPWSINFO pwsi = wsi_find(GetCurrentTask());
865 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
868 TRACE(winsock, "(%08x): socket: %04x, ptr %8x, ptr %8x\n",
869 (unsigned)pwsi, s, (int) name, *namelen);
870 if( _check_ws(pwsi, pws) )
872 if (getpeername(pws->fd, name, namelen) == 0) {
874 if (((struct ws_sockaddr_ipx *)name)->sipx_family == AF_IPX) {
875 name = (struct sockaddr *)
876 malloc(namelen ? *namelen : sizeof(*name2));
877 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
878 name2->sipx_family = WS_AF_IPX;
879 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
880 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
881 memcpy(name2->sipx_node,
882 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
888 pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
891 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == AF_IPX) {
892 name = (struct sockaddr *) malloc(namelen ? *namelen : sizeof(*name2));
893 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
894 name2->sipx_family = WS_AF_IPX;
895 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
896 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
897 memcpy(name2->sipx_node,
898 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
905 /***********************************************************************
906 * getpeername() (WINSOCK.5)
908 INT16 WINAPI WINSOCK_getpeername16(SOCKET16 s, struct sockaddr *name,
911 INT namelen32 = *namelen16;
912 INT retVal = WINSOCK_getpeername( s, name, &namelen32 );
918 *namelen16 = namelen32;
919 return (INT16)retVal;
922 /***********************************************************************
923 * getsockname() (WSOCK32.6)
925 INT WINAPI WINSOCK_getsockname(SOCKET s, struct sockaddr *name,
928 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
929 LPWSINFO pwsi = wsi_find(GetCurrentTask());
931 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
934 TRACE(winsock, "(%08x): socket: %04x, ptr %8x, ptr %8x\n",
935 (unsigned)pwsi, s, (int) name, (int) *namelen);
936 if( _check_ws(pwsi, pws) )
938 if (getsockname(pws->fd, name, namelen) == 0) {
940 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX) {
941 name = (struct sockaddr *)
942 malloc(namelen ? *namelen : sizeof(*name2));
943 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
944 name2->sipx_family = WS_AF_IPX;
945 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
946 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
947 memcpy(name2->sipx_node,
948 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
954 pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
957 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == AF_IPX) {
958 name = (struct sockaddr *) malloc(namelen ? *namelen : sizeof(*name2));
959 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
960 name2->sipx_family = WS_AF_IPX;
961 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
962 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
963 memcpy(name2->sipx_node,
964 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
971 /***********************************************************************
972 * getsockname() (WINSOCK.6)
974 INT16 WINAPI WINSOCK_getsockname16(SOCKET16 s, struct sockaddr *name,
981 INT namelen32 = *namelen16;
982 retVal = WINSOCK_getsockname( s, name, &namelen32 );
983 *namelen16 = namelen32;
990 else retVal = SOCKET_ERROR;
991 return (INT16)retVal;
995 /***********************************************************************
996 * getsockopt() (WSOCK32.7)
998 INT WINAPI WINSOCK_getsockopt(SOCKET s, INT level,
999 INT optname, char *optval, INT *optlen)
1001 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1002 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1004 TRACE(winsock, "(%08x): socket: %04x, opt %d, ptr %8x, ptr %8x\n",
1005 (unsigned)pwsi, s, level, (int) optval, (int) *optlen);
1006 if( _check_ws(pwsi, pws) )
1008 convert_sockopt(&level, &optname);
1009 if (getsockopt(pws->fd, (int) level, optname, optval, optlen) == 0 )
1011 pwsi->err = (errno == EBADF) ? WSAENOTSOCK : wsaErrno();
1013 return SOCKET_ERROR;
1016 /***********************************************************************
1017 * getsockopt() (WINSOCK.7)
1019 INT16 WINAPI WINSOCK_getsockopt16(SOCKET16 s, INT16 level,
1020 INT16 optname, char *optval, INT16 *optlen)
1025 if( optlen ) optlen32 = *optlen; else p = NULL;
1026 retVal = WINSOCK_getsockopt( s, (UINT16)level, optname, optval, p );
1027 if( optlen ) *optlen = optlen32;
1028 return (INT16)retVal;
1031 /***********************************************************************
1032 * htonl() (WINSOCK.8)(WSOCK32.8)
1034 u_long WINAPI WINSOCK_htonl(u_long hostlong) { return( htonl(hostlong) ); }
1035 /***********************************************************************
1036 * htons() (WINSOCK.9)(WSOCK32.9)
1038 u_short WINAPI WINSOCK_htons(u_short hostshort) { return( htons(hostshort) ); }
1039 /***********************************************************************
1040 * inet_addr() (WINSOCK.10)(WSOCK32.10)
1042 u_long WINAPI WINSOCK_inet_addr(char *cp) { return( inet_addr(cp) ); }
1043 /***********************************************************************
1044 * htohl() (WINSOCK.14)(WSOCK32.14)
1046 u_long WINAPI WINSOCK_ntohl(u_long netlong) { return( ntohl(netlong) ); }
1047 /***********************************************************************
1048 * ntohs() (WINSOCK.15)(WSOCK32.15)
1050 u_short WINAPI WINSOCK_ntohs(u_short netshort) { return( ntohs(netshort) ); }
1052 /***********************************************************************
1053 * inet_ntoa() (WINSOCK.11)(WSOCK32.11)
1055 char* WINAPI WINSOCK_inet_ntoa(struct in_addr in)
1057 /* use "buffer for dummies" here because some applications have
1058 * propensity to decode addresses in ws_hostent structure without
1059 * saving them first...
1062 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1066 char* s = inet_ntoa(in);
1069 if( pwsi->dbuffer == NULL )
1070 if((pwsi->dbuffer = (char*) SEGPTR_ALLOC(32)) == NULL )
1072 pwsi->err = WSAENOBUFS;
1075 strncpy(pwsi->dbuffer, s, 32 );
1076 return pwsi->dbuffer;
1078 pwsi->err = wsaErrno();
1083 SEGPTR WINAPI WINSOCK_inet_ntoa16(struct in_addr in)
1085 char* retVal = WINSOCK_inet_ntoa(in);
1086 return retVal ? SEGPTR_GET(retVal) : (SEGPTR)NULL;
1089 /***********************************************************************
1090 * ioctlsocket() (WSOCK32.12)
1092 INT WINAPI WINSOCK_ioctlsocket(SOCKET s, UINT cmd, UINT *argp)
1094 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1095 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1097 TRACE(winsock, "(%08x): socket %04x, cmd %08x, ptr %8x\n",
1098 (unsigned)pwsi, s, cmd, (unsigned) argp);
1099 if( _check_ws(pwsi, pws) )
1111 if( pws->psop && *argp == 0 )
1113 /* AsyncSelect()'ed sockets are always nonblocking */
1114 pwsi->err = WSAEINVAL;
1115 return SOCKET_ERROR;
1123 case WS_IOW('f',125,u_long):
1124 WARN(winsock,"Warning: WS1.1 shouldn't be using async I/O\n");
1125 pwsi->err = WSAEINVAL;
1126 return SOCKET_ERROR;
1129 /* Netscape tries hard to use bogus ioctl 0x667e */
1130 WARN(winsock, "\tunknown WS_IOCTL cmd (%08x)\n", cmd);
1132 if( ioctl(pws->fd, newcmd, (char*)argp ) == 0 ) return 0;
1133 pwsi->err = (errno == EBADF) ? WSAENOTSOCK : wsaErrno();
1135 return SOCKET_ERROR;
1138 /***********************************************************************
1139 * ioctlsocket() (WINSOCK.12)
1141 INT16 WINAPI WINSOCK_ioctlsocket16(SOCKET16 s, UINT cmd, UINT *argp)
1143 return (INT16)WINSOCK_ioctlsocket( s, cmd, argp );
1147 /***********************************************************************
1148 * listen() (WSOCK32.13)
1150 INT WINAPI WINSOCK_listen(SOCKET s, INT backlog)
1152 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1153 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1155 TRACE(winsock, "(%08x): socket %04x, backlog %d\n",
1156 (unsigned)pwsi, s, backlog);
1157 if( _check_ws(pwsi, pws) )
1159 if (listen(pws->fd, backlog) == 0)
1163 int fd_flags = fcntl(pws->fd, F_GETFL, 0);
1164 if( !(fd_flags & O_NONBLOCK) ) pws->flags |= WS_FD_ACCEPT;
1166 pws->flags |= WS_FD_LISTENING;
1167 pws->flags &= ~(WS_FD_INACTIVE | WS_FD_CONNECT | WS_FD_CONNECTED); /* just in case */
1170 pwsi->err = wsaErrno();
1172 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1173 return SOCKET_ERROR;
1176 /***********************************************************************
1177 * listen() (WINSOCK.13)
1179 INT16 WINAPI WINSOCK_listen16(SOCKET16 s, INT16 backlog)
1181 return (INT16)WINSOCK_listen( s, backlog );
1185 /***********************************************************************
1186 * recv() (WSOCK32.16)
1188 INT WINAPI WINSOCK_recv(SOCKET s, char *buf, INT len, INT flags)
1190 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1191 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1193 TRACE(winsock, "(%08x): socket %04x, buf %8x, len %d, "
1194 "flags %d\n", (unsigned)pwsi, s, (unsigned)buf,
1196 if( _check_ws(pwsi, pws) )
1199 if ((length = recv(pws->fd, buf, len, flags)) >= 0)
1201 TRACE(winsock, " -> %i bytes\n", length);
1203 if( pws->psop && (pws->flags & (WS_FD_READ | WS_FD_CLOSE)) )
1204 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
1208 pwsi->err = wsaErrno();
1210 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1211 WARN(winsock, " -> ERROR\n");
1212 return SOCKET_ERROR;
1215 /***********************************************************************
1216 * recv() (WINSOCK.16)
1218 INT16 WINAPI WINSOCK_recv16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1220 return (INT16)WINSOCK_recv( s, buf, len, flags );
1224 /***********************************************************************
1225 * recvfrom() (WSOCK32.17)
1227 INT WINAPI WINSOCK_recvfrom(SOCKET s, char *buf, INT len, INT flags,
1228 struct sockaddr *from, INT *fromlen32)
1230 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1231 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1233 struct ws_sockaddr_ipx* from2 = (struct ws_sockaddr_ipx *)from;
1236 TRACE(winsock, "(%08x): socket %04x, ptr %08x, "
1237 "len %d, flags %d\n", (unsigned)pwsi, s, (unsigned)buf,
1240 if( from ) dump_sockaddr(from);
1241 else DUMP("from = NULL\n");
1244 if( _check_ws(pwsi, pws) )
1248 if ((length = recvfrom(pws->fd, buf, len, flags, from, fromlen32)) >= 0)
1250 TRACE(winsock, " -> %i bytes\n", length);
1252 if( pws->psop && (pws->flags & (WS_FD_READ | WS_FD_CLOSE)) )
1253 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
1256 if (from && ((struct sockaddr_ipx *)from)->sipx_family == AF_IPX) {
1257 from = (struct sockaddr *)
1258 malloc(fromlen32 ? *fromlen32 : sizeof(*from2));
1259 memcpy(from, from2, fromlen32 ? *fromlen32 : sizeof(*from2));
1260 from2->sipx_family = WS_AF_IPX;
1261 from2->sipx_network = ((struct sockaddr_ipx *)from)->sipx_network;
1262 from2->sipx_port = ((struct sockaddr_ipx *)from)->sipx_port;
1263 memcpy(from2->sipx_node,
1264 ((struct sockaddr_ipx *)from)->sipx_node, IPX_NODE_LEN);
1268 return (INT16)length;
1270 pwsi->err = wsaErrno();
1272 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1273 WARN(winsock, " -> ERROR\n");
1275 if (from && ((struct sockaddr_ipx *)from)->sipx_family == AF_IPX) {
1276 from = (struct sockaddr *)
1277 malloc(fromlen32 ? *fromlen32 : sizeof(*from2));
1278 memcpy(from, from2, fromlen32 ? *fromlen32 : sizeof(*from2));
1279 from2->sipx_family = WS_AF_IPX;
1280 from2->sipx_network = ((struct sockaddr_ipx *)from)->sipx_network;
1281 from2->sipx_port = ((struct sockaddr_ipx *)from)->sipx_port;
1282 memcpy(from2->sipx_node,
1283 ((struct sockaddr_ipx *)from)->sipx_node, IPX_NODE_LEN);
1287 return SOCKET_ERROR;
1290 /***********************************************************************
1291 * recvfrom() (WINSOCK.17)
1293 INT16 WINAPI WINSOCK_recvfrom16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1294 struct sockaddr *from, INT16 *fromlen16)
1297 INT *p = &fromlen32;
1300 if( fromlen16 ) fromlen32 = *fromlen16; else p = NULL;
1301 retVal = WINSOCK_recvfrom( s, buf, len, flags, from, p );
1302 if( fromlen16 ) *fromlen16 = fromlen32;
1303 return (INT16)retVal;
1306 /***********************************************************************
1307 * select() (WINSOCK.18)(WSOCK32.18)
1309 static INT __ws_select( BOOL b32, void *ws_readfds, void *ws_writefds, void *ws_exceptfds,
1310 struct timeval *timeout )
1312 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1314 TRACE(winsock, "(%08x): read %8x, write %8x, excp %8x\n",
1315 (unsigned) pwsi, (unsigned) ws_readfds, (unsigned) ws_writefds, (unsigned) ws_exceptfds);
1320 fd_set readfds, writefds, exceptfds;
1321 fd_set *p_read, *p_write, *p_except;
1323 p_read = fd_set_import(&readfds, pwsi, ws_readfds, &highfd, b32);
1324 p_write = fd_set_import(&writefds, pwsi, ws_writefds, &highfd, b32);
1325 p_except = fd_set_import(&exceptfds, pwsi, ws_exceptfds, &highfd, b32);
1327 if( (highfd = select(highfd + 1, p_read, p_write, p_except, timeout)) > 0 )
1329 fd_set_export(pwsi, &readfds, p_except, ws_readfds, b32);
1330 fd_set_export(pwsi, &writefds, p_except, ws_writefds, b32);
1332 if (p_except && ws_exceptfds)
1334 #define wsfds16 ((ws_fd_set16*)ws_exceptfds)
1335 #define wsfds32 ((ws_fd_set32*)ws_exceptfds)
1336 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
1338 for (i = j = 0; i < count; i++)
1340 ws_socket *pws = (b32) ? (ws_socket *)WS_HANDLE2PTR(wsfds32->fd_array[i])
1341 : (ws_socket *)WS_HANDLE2PTR(wsfds16->fd_array[i]);
1342 if( _check_ws(pwsi, pws) && FD_ISSET(pws->fd, &exceptfds) )
1345 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
1347 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
1351 wsfds32->fd_count = j;
1353 wsfds16->fd_count = j;
1359 if( ws_readfds ) ((ws_fd_set32*)ws_readfds)->fd_count = 0;
1360 if( ws_writefds ) ((ws_fd_set32*)ws_writefds)->fd_count = 0;
1361 if( ws_exceptfds ) ((ws_fd_set32*)ws_exceptfds)->fd_count = 0;
1363 if( highfd == 0 ) return 0;
1364 pwsi->err = wsaErrno();
1366 return SOCKET_ERROR;
1369 INT16 WINAPI WINSOCK_select16(INT16 nfds, ws_fd_set16 *ws_readfds,
1370 ws_fd_set16 *ws_writefds, ws_fd_set16 *ws_exceptfds,
1371 struct timeval *timeout)
1373 return (INT16)__ws_select( FALSE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1376 INT WINAPI WINSOCK_select(INT nfds, ws_fd_set32 *ws_readfds,
1377 ws_fd_set32 *ws_writefds, ws_fd_set32 *ws_exceptfds,
1378 struct timeval *timeout)
1380 /* struct timeval is the same for both 32- and 16-bit code */
1381 return (INT)__ws_select( TRUE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1385 /***********************************************************************
1386 * send() (WSOCK32.19)
1388 INT WINAPI WINSOCK_send(SOCKET s, char *buf, INT len, INT flags)
1390 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1391 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1393 TRACE(winsock, "(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
1394 (unsigned)pwsi, s, (unsigned) buf, len, flags);
1395 if( _check_ws(pwsi, pws) )
1399 if ((length = send(pws->fd, buf, len, flags)) < 0 )
1401 pwsi->err = wsaErrno();
1402 if( pwsi->err == WSAEWOULDBLOCK &&
1403 pws->psop && pws->flags & WS_FD_WRITE )
1404 EVENT_AddIO( pws->fd, EVENT_IO_WRITE ); /* reenabler */
1406 else return (INT16)length;
1408 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1409 return SOCKET_ERROR;
1412 /***********************************************************************
1413 * send() (WINSOCK.19)
1415 INT16 WINAPI WINSOCK_send16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1417 return WINSOCK_send( s, buf, len, flags );
1420 /***********************************************************************
1421 * sendto() (WSOCK32.20)
1423 INT WINAPI WINSOCK_sendto(SOCKET s, char *buf, INT len, INT flags,
1424 struct sockaddr *to, INT tolen)
1426 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1427 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1429 struct ws_sockaddr_ipx* to2 = (struct ws_sockaddr_ipx *)to;
1432 TRACE(winsock, "(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
1433 (unsigned)pwsi, s, (unsigned) buf, len, flags);
1434 if( _check_ws(pwsi, pws) )
1438 if (to && ((struct ws_sockaddr_ipx *)to)->sipx_family == WS_AF_PUP)
1439 ((struct ws_sockaddr_ipx *)to)->sipx_family = AF_UNSPEC;
1442 ((struct ws_sockaddr_ipx *)to)->sipx_family == WS_AF_IPX)
1444 to = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
1445 memset(to, '\0', sizeof(struct sockaddr_ipx));
1446 ((struct sockaddr_ipx *)to)->sipx_family = AF_IPX;
1447 ((struct sockaddr_ipx *)to)->sipx_port = to2->sipx_port;
1448 ((struct sockaddr_ipx *)to)->sipx_network = to2->sipx_network;
1449 memcpy(((struct sockaddr_ipx *)to)->sipx_node,
1450 to2->sipx_node, IPX_NODE_LEN);
1451 tolen = sizeof(struct sockaddr_ipx);
1454 if ((length = sendto(pws->fd, buf, len, flags, to, tolen)) < 0 )
1456 pwsi->err = wsaErrno();
1457 if( pwsi->err == WSAEWOULDBLOCK &&
1458 pws->psop && pws->flags & WS_FD_WRITE )
1459 EVENT_AddIO( pws->fd, EVENT_IO_WRITE ); /* reenabler */
1463 if (to && ((struct sockaddr_ipx *)to)->sipx_family == AF_IPX) {
1470 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1472 if (to && ((struct sockaddr_ipx *)to)->sipx_family == AF_IPX) {
1476 return SOCKET_ERROR;
1479 /***********************************************************************
1480 * sendto() (WINSOCK.20)
1482 INT16 WINAPI WINSOCK_sendto16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1483 struct sockaddr *to, INT16 tolen)
1485 return (INT16)WINSOCK_sendto( s, buf, len, flags, to, tolen );
1488 /***********************************************************************
1489 * setsockopt() (WSOCK32.21)
1491 INT WINAPI WINSOCK_setsockopt(SOCKET16 s, INT level, INT optname,
1492 char *optval, INT optlen)
1494 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1495 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1497 TRACE(winsock, "(%08x): socket %04x, lev %d, opt %d, ptr %08x, len %d\n",
1498 (unsigned)pwsi, s, level, optname, (int) optval, optlen);
1499 if( _check_ws(pwsi, pws) )
1501 struct linger linger;
1503 convert_sockopt(&level, &optname);
1504 if (optname == SO_LINGER && optval) {
1505 /* yes, uses unsigned short in both win16/win32 */
1506 linger.l_onoff = ((UINT16*)optval)[0];
1507 linger.l_linger = ((UINT16*)optval)[1];
1508 /* FIXME: what is documented behavior if SO_LINGER optval
1510 optval = (char*)&linger;
1511 optlen = sizeof(struct linger);
1513 if (setsockopt(pws->fd, level, optname, optval, optlen) == 0) return 0;
1514 pwsi->err = wsaErrno();
1516 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1517 return SOCKET_ERROR;
1520 /***********************************************************************
1521 * setsockopt() (WINSOCK.21)
1523 INT16 WINAPI WINSOCK_setsockopt16(SOCKET16 s, INT16 level, INT16 optname,
1524 char *optval, INT16 optlen)
1526 if( !optval ) return SOCKET_ERROR;
1527 return (INT16)WINSOCK_setsockopt( s, (UINT16)level, optname, optval, optlen );
1531 /***********************************************************************
1532 * shutdown() (WSOCK32.22)
1534 INT WINAPI WINSOCK_shutdown(SOCKET s, INT how)
1536 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1537 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1539 TRACE(winsock, "(%08x): socket %04x, how %i\n",
1540 (unsigned)pwsi, s, how );
1541 if( _check_ws(pwsi, pws) )
1546 case 0: /* drop receives */
1547 if( pws->flags & (WS_FD_READ | WS_FD_CLOSE) )
1548 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
1549 pws->flags &= ~(WS_FD_READ | WS_FD_CLOSE);
1555 case 1: /* drop sends */
1556 if( pws->flags & WS_FD_WRITE )
1557 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
1558 pws->flags &= ~WS_FD_WRITE;
1564 case 2: /* drop all */
1569 WSAAsyncSelect( s, 0, 0, 0 );
1573 if (shutdown(pws->fd, how) == 0)
1577 pws->flags &= ~(WS_FD_CONNECTED | WS_FD_LISTENING);
1578 pws->flags |= WS_FD_INACTIVE;
1582 pwsi->err = wsaErrno();
1584 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1585 return SOCKET_ERROR;
1588 /***********************************************************************
1589 * shutdown() (WINSOCK.22)
1591 INT16 WINAPI WINSOCK_shutdown16(SOCKET16 s, INT16 how)
1593 return (INT16)WINSOCK_shutdown( s, how );
1597 /***********************************************************************
1598 * socket() (WSOCK32.23)
1600 SOCKET WINAPI WINSOCK_socket(INT af, INT type, INT protocol)
1602 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1604 TRACE(winsock, "(%08x): af=%d type=%d protocol=%d\n",
1605 (unsigned)pwsi, af, type, protocol);
1611 /* check the socket family */
1615 case WS_AF_IPX: af = AF_IPX;
1618 case AF_UNSPEC: break;
1619 default: pwsi->err = WSAEAFNOSUPPORT;
1620 return INVALID_SOCKET;
1623 /* check the socket type */
1628 case SOCK_RAW: break;
1629 default: pwsi->err = WSAESOCKTNOSUPPORT;
1630 return INVALID_SOCKET;
1633 /* check the protocol type */
1634 if ( protocol < 0 ) /* don't support negative values */
1635 { pwsi->err = WSAEPROTONOSUPPORT; return INVALID_SOCKET; }
1637 if ( af == AF_UNSPEC) /* did they not specify the address family? */
1641 if (type == SOCK_STREAM) { af = AF_INET; break; }
1643 if (type == SOCK_DGRAM) { af = AF_INET; break; }
1644 default: pwsi->err = WSAEPROTOTYPE; return INVALID_SOCKET;
1647 if ((sock = socket(af, type, protocol)) >= 0)
1649 ws_socket* pnew = wsi_alloc_socket(pwsi, sock);
1651 TRACE(winsock,"\tcreated %i (handle %04x)\n", sock, (UINT16)WS_PTR2HANDLE(pnew));
1655 pnew->flags |= WS_FD_INACTIVE;
1656 return (SOCKET16)WS_PTR2HANDLE(pnew);
1660 pwsi->err = WSAENOBUFS;
1661 return INVALID_SOCKET;
1664 if (errno == EPERM) /* raw socket denied */
1666 WARN(winsock, "WS_SOCKET: not enough privileges\n");
1667 pwsi->err = WSAESOCKTNOSUPPORT;
1668 } else pwsi->err = wsaErrno();
1671 WARN(winsock, "\t\tfailed!\n");
1672 return INVALID_SOCKET;
1675 /***********************************************************************
1676 * socket() (WINSOCK.23)
1678 SOCKET16 WINAPI WINSOCK_socket16(INT16 af, INT16 type, INT16 protocol)
1680 return (SOCKET16)WINSOCK_socket( af, type, protocol );
1684 /* ----------------------------------- DNS services
1686 * IMPORTANT: 16-bit API structures have SEGPTR pointers inside them.
1687 * Also, we have to use wsock32 stubs to convert structures and
1688 * error codes from Unix to WSA, hence there is no direct mapping in
1689 * the relay32/wsock32.spec.
1692 static char* NULL_STRING = "NULL";
1694 /***********************************************************************
1695 * gethostbyaddr() (WINSOCK.51)(WSOCK32.51)
1697 static struct WIN_hostent* __ws_gethostbyaddr(const char *addr, int len, int type, int dup_flag)
1699 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1703 struct hostent* host;
1704 if( (host = gethostbyaddr(addr, len, type)) != NULL )
1705 if( WS_dup_he(pwsi, host, dup_flag) )
1706 return (struct WIN_hostent*)(pwsi->he);
1708 pwsi->err = WSAENOBUFS;
1710 pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1715 SEGPTR WINAPI WINSOCK_gethostbyaddr16(const char *addr, INT16 len, INT16 type)
1717 struct WIN_hostent* retval;
1718 TRACE(winsock, "ptr %08x, len %d, type %d\n",
1719 (unsigned) addr, len, type);
1720 retval = __ws_gethostbyaddr( addr, len, type, WS_DUP_SEGPTR );
1721 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1724 struct WIN_hostent* WINAPI WINSOCK_gethostbyaddr(const char *addr, INT len,
1727 TRACE(winsock, "ptr %08x, len %d, type %d\n",
1728 (unsigned) addr, len, type);
1729 return __ws_gethostbyaddr(addr, len, type, WS_DUP_LINEAR);
1732 /***********************************************************************
1733 * gethostbyname() (WINSOCK.52)(WSOCK32.52)
1735 static struct WIN_hostent * __ws_gethostbyname(const char *name, int dup_flag)
1737 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1741 struct hostent* host;
1742 if( (host = gethostbyname(name)) != NULL )
1743 if( WS_dup_he(pwsi, host, dup_flag) )
1744 return (struct WIN_hostent*)(pwsi->he);
1745 else pwsi->err = WSAENOBUFS;
1746 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1751 SEGPTR WINAPI WINSOCK_gethostbyname16(const char *name)
1753 struct WIN_hostent* retval;
1754 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1755 retval = __ws_gethostbyname( name, WS_DUP_SEGPTR );
1756 return (retval)? SEGPTR_GET(retval) : ((SEGPTR)NULL) ;
1759 struct WIN_hostent* WINAPI WINSOCK_gethostbyname(const char* name)
1761 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1762 return __ws_gethostbyname( name, WS_DUP_LINEAR );
1766 /***********************************************************************
1767 * getprotobyname() (WINSOCK.53)(WSOCK32.53)
1769 static struct WIN_protoent* __ws_getprotobyname(const char *name, int dup_flag)
1771 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1775 struct protoent* proto;
1776 if( (proto = getprotobyname(name)) != NULL )
1777 if( WS_dup_pe(pwsi, proto, dup_flag) )
1778 return (struct WIN_protoent*)(pwsi->pe);
1779 else pwsi->err = WSAENOBUFS;
1780 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1785 SEGPTR WINAPI WINSOCK_getprotobyname16(const char *name)
1787 struct WIN_protoent* retval;
1788 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1789 retval = __ws_getprotobyname(name, WS_DUP_SEGPTR);
1790 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1793 struct WIN_protoent* WINAPI WINSOCK_getprotobyname(const char* name)
1795 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1796 return __ws_getprotobyname(name, WS_DUP_LINEAR);
1800 /***********************************************************************
1801 * getprotobynumber() (WINSOCK.54)(WSOCK32.54)
1803 static struct WIN_protoent* __ws_getprotobynumber(int number, int dup_flag)
1805 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1809 struct protoent* proto;
1810 if( (proto = getprotobynumber(number)) != NULL )
1811 if( WS_dup_pe(pwsi, proto, dup_flag) )
1812 return (struct WIN_protoent*)(pwsi->pe);
1813 else pwsi->err = WSAENOBUFS;
1814 else pwsi->err = WSANO_DATA;
1819 SEGPTR WINAPI WINSOCK_getprotobynumber16(INT16 number)
1821 struct WIN_protoent* retval;
1822 TRACE(winsock, "%i\n", number);
1823 retval = __ws_getprotobynumber(number, WS_DUP_SEGPTR);
1824 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1827 struct WIN_protoent* WINAPI WINSOCK_getprotobynumber(INT number)
1829 TRACE(winsock, "%i\n", number);
1830 return __ws_getprotobynumber(number, WS_DUP_LINEAR);
1834 /***********************************************************************
1835 * getservbyname() (WINSOCK.55)(WSOCK32.55)
1837 struct WIN_servent* __ws_getservbyname(const char *name, const char *proto, int dup_flag)
1839 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1843 struct servent* serv;
1844 int i = wsi_strtolo( pwsi, name, proto );
1847 if( (serv = getservbyname(pwsi->buffer, pwsi->buffer + i)) != NULL )
1848 if( WS_dup_se(pwsi, serv, dup_flag) )
1849 return (struct WIN_servent*)(pwsi->se);
1850 else pwsi->err = WSAENOBUFS;
1851 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1852 else pwsi->err = WSAENOBUFS;
1857 SEGPTR WINAPI WINSOCK_getservbyname16(const char *name, const char *proto)
1859 struct WIN_servent* retval;
1860 TRACE(winsock, "'%s', '%s'\n",
1861 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
1862 retval = __ws_getservbyname(name, proto, WS_DUP_SEGPTR);
1863 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1866 struct WIN_servent* WINAPI WINSOCK_getservbyname(const char *name, const char *proto)
1868 TRACE(winsock, "'%s', '%s'\n",
1869 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
1870 return __ws_getservbyname(name, proto, WS_DUP_LINEAR);
1874 /***********************************************************************
1875 * getservbyport() (WINSOCK.56)(WSOCK32.56)
1877 static struct WIN_servent* __ws_getservbyport(int port, const char* proto, int dup_flag)
1879 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1883 struct servent* serv;
1884 int i = wsi_strtolo( pwsi, proto, NULL );
1887 if( (serv = getservbyport(port, pwsi->buffer)) != NULL )
1888 if( WS_dup_se(pwsi, serv, dup_flag) )
1889 return (struct WIN_servent*)(pwsi->se);
1890 else pwsi->err = WSAENOBUFS;
1891 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1892 else pwsi->err = WSAENOBUFS;
1897 SEGPTR WINAPI WINSOCK_getservbyport16(INT16 port, const char *proto)
1899 struct WIN_servent* retval;
1900 TRACE(winsock, "%i, '%s'\n",
1901 (int)port, (proto)?proto:NULL_STRING);
1902 retval = __ws_getservbyport(port, proto, WS_DUP_SEGPTR);
1903 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1906 struct WIN_servent* WINAPI WINSOCK_getservbyport(INT port, const char *proto)
1908 TRACE(winsock, "%i, '%s'\n",
1909 (int)port, (proto)?proto:NULL_STRING);
1910 return __ws_getservbyport(port, proto, WS_DUP_LINEAR);
1914 /***********************************************************************
1915 * gethostname() (WSOCK32.57)
1917 INT WINAPI WINSOCK_gethostname(char *name, INT namelen)
1919 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1921 TRACE(winsock, "(%08x): name %s, len %d\n",
1922 (unsigned)pwsi, (name)?name:NULL_STRING, namelen);
1925 if (gethostname(name, namelen) == 0) return 0;
1926 pwsi->err = (errno == EINVAL) ? WSAEFAULT : wsaErrno();
1928 return SOCKET_ERROR;
1931 /***********************************************************************
1932 * gethostname() (WINSOCK.57)
1934 INT16 WINAPI WINSOCK_gethostname16(char *name, INT16 namelen)
1936 return (INT16)WINSOCK_gethostname(name, namelen);
1940 /* ------------------------------------- Windows sockets extensions -- *
1942 * ------------------------------------------------------------------- */
1945 /***********************************************************************
1946 * WSAAsyncGetHostByAddr() (WINSOCK.102)
1948 HANDLE16 WINAPI WSAAsyncGetHostByAddr16(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,
1949 INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen)
1951 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1953 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, addr %08x[%i]\n",
1954 (unsigned)pwsi, hWnd, uMsg, (unsigned)addr , len );
1957 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, type, addr, len,
1958 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYADDR );
1962 /***********************************************************************
1963 * WSAAsyncGetHostByAddr() (WSOCK32.102)
1965 HANDLE WINAPI WSAAsyncGetHostByAddr(HWND hWnd, UINT uMsg, LPCSTR addr,
1966 INT len, INT type, LPSTR sbuf, INT buflen)
1968 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1970 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, addr %08x[%i]\n",
1971 (unsigned)pwsi, (HWND16)hWnd, uMsg, (unsigned)addr , len );
1974 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, type, addr, len,
1975 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYADDR | WSMSG_WIN32_AOP);
1980 /***********************************************************************
1981 * WSAAsyncGetHostByName() (WINSOCK.103)
1983 HANDLE16 WINAPI WSAAsyncGetHostByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
1984 SEGPTR sbuf, INT16 buflen)
1986 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1988 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, host %s,
1989 buffer %i\n", (unsigned)pwsi, hWnd, uMsg, (name)?name:NULL_STRING, (int)buflen );
1992 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
1993 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYNAME );
1997 /***********************************************************************
1998 * WSAAsyncGetHostByName32() (WSOCK32.103)
2000 HANDLE WINAPI WSAAsyncGetHostByName(HWND hWnd, UINT uMsg, LPCSTR name,
2001 LPSTR sbuf, INT buflen)
2003 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2004 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, host %s, buffer %i\n",
2005 (unsigned)pwsi, (HWND16)hWnd, uMsg,
2006 (name)?name:NULL_STRING, (int)buflen );
2008 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
2009 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYNAME | WSMSG_WIN32_AOP);
2014 /***********************************************************************
2015 * WSAAsyncGetProtoByName() (WINSOCK.105)
2017 HANDLE16 WINAPI WSAAsyncGetProtoByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
2018 SEGPTR sbuf, INT16 buflen)
2020 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2022 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, protocol %s\n",
2023 (unsigned)pwsi, (HWND16)hWnd, uMsg, (name)?name:NULL_STRING );
2026 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
2027 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNAME );
2031 /***********************************************************************
2032 * WSAAsyncGetProtoByName() (WSOCK32.105)
2034 HANDLE WINAPI WSAAsyncGetProtoByName(HWND hWnd, UINT uMsg, LPCSTR name,
2035 LPSTR sbuf, INT buflen)
2037 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2039 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, protocol %s\n",
2040 (unsigned)pwsi, (HWND16)hWnd, uMsg, (name)?name:NULL_STRING );
2043 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
2044 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNAME | WSMSG_WIN32_AOP);
2049 /***********************************************************************
2050 * WSAAsyncGetProtoByNumber() (WINSOCK.104)
2052 HANDLE16 WINAPI WSAAsyncGetProtoByNumber16(HWND16 hWnd, UINT16 uMsg, INT16 number,
2053 SEGPTR sbuf, INT16 buflen)
2055 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2057 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, num %i\n",
2058 (unsigned)pwsi, hWnd, uMsg, number );
2061 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, number, NULL, 0,
2062 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNUM );
2066 /***********************************************************************
2067 * WSAAsyncGetProtoByNumber() (WSOCK32.104)
2069 HANDLE WINAPI WSAAsyncGetProtoByNumber(HWND hWnd, UINT uMsg, INT number,
2070 LPSTR sbuf, INT buflen)
2072 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2074 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, num %i\n",
2075 (unsigned)pwsi, (HWND16)hWnd, uMsg, number );
2078 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, number, NULL, 0,
2079 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNUM | WSMSG_WIN32_AOP);
2084 /***********************************************************************
2085 * WSAAsyncGetServByName() (WINSOCK.107)
2087 HANDLE16 WINAPI WSAAsyncGetServByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
2088 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
2090 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2092 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, name %s, proto %s\n",
2093 (unsigned)pwsi, hWnd, uMsg,
2094 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING );
2098 int i = wsi_strtolo( pwsi, name, proto );
2101 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, pwsi->buffer, 0,
2102 pwsi->buffer + i, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYNAME );
2107 /***********************************************************************
2108 * WSAAsyncGetServByName() (WSOCK32.107)
2110 HANDLE WINAPI WSAAsyncGetServByName(HWND hWnd, UINT uMsg, LPCSTR name,
2111 LPCSTR proto, LPSTR sbuf, INT buflen)
2113 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2115 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, name %s, proto %s\n",
2116 (unsigned)pwsi, (HWND16)hWnd, uMsg,
2117 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING );
2120 int i = wsi_strtolo( pwsi, name, proto );
2123 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, pwsi->buffer, 0,
2124 pwsi->buffer + i, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYNAME | WSMSG_WIN32_AOP);
2130 /***********************************************************************
2131 * WSAAsyncGetServByPort() (WINSOCK.106)
2133 HANDLE16 WINAPI WSAAsyncGetServByPort16(HWND16 hWnd, UINT16 uMsg, INT16 port,
2134 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
2136 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2138 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, port %i, proto %s\n",
2139 (unsigned)pwsi, hWnd, uMsg, port, (proto)?proto:NULL_STRING );
2143 int i = wsi_strtolo( pwsi, proto, NULL );
2146 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, port, pwsi->buffer, 0,
2147 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYPORT );
2152 /***********************************************************************
2153 * WSAAsyncGetServByPort() (WSOCK32.106)
2155 HANDLE WINAPI WSAAsyncGetServByPort(HWND hWnd, UINT uMsg, INT port,
2156 LPCSTR proto, LPSTR sbuf, INT buflen)
2158 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2160 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, port %i, proto %s\n",
2161 (unsigned)pwsi, (HWND16)hWnd, uMsg, port, (proto)?proto:NULL_STRING );
2165 int i = wsi_strtolo( pwsi, proto, NULL );
2168 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, port, pwsi->buffer, 0,
2169 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYPORT | WSMSG_WIN32_AOP);
2175 /***********************************************************************
2176 * WSACancelAsyncRequest() (WINSOCK.108)(WSOCK32.109)
2178 INT WINAPI WSACancelAsyncRequest(HANDLE hAsyncTaskHandle)
2180 INT retVal = SOCKET_ERROR;
2181 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2182 ws_async_op* p_aop = (ws_async_op*)WS_HANDLE2PTR(hAsyncTaskHandle);
2184 TRACE(winsock, "(%08x): handle %08x\n",
2185 (unsigned)pwsi, hAsyncTaskHandle);
2188 SIGNAL_MaskAsyncEvents( TRUE ); /* block SIGIO */
2189 if( WINSOCK_cancel_async_op(p_aop) )
2192 pwsi->num_async_rq--;
2195 else pwsi->err = WSAEINVAL;
2196 SIGNAL_MaskAsyncEvents( FALSE );
2201 INT16 WINAPI WSACancelAsyncRequest16(HANDLE16 hAsyncTaskHandle)
2203 return (HANDLE16)WSACancelAsyncRequest((HANDLE)hAsyncTaskHandle);
2206 /***********************************************************************
2207 * WSAAsyncSelect() (WINSOCK.101)(WSOCK32.101)
2210 static ws_select_op* __ws_select_list = NULL;
2212 BOOL WINSOCK_HandleIO( int* max_fd, int num_pending,
2213 fd_set pending_set[3], fd_set event_set[3] )
2215 /* This function is called by the event dispatcher
2216 * with the pending_set[] containing the result of select() and
2217 * the event_set[] containing all fd that are being watched */
2219 ws_select_op* psop = __ws_select_list;
2221 DWORD dwEvent, dwErrBytes;
2224 TRACE(winsock,"%i pending descriptors\n", num_pending );
2226 for( num_posted = dwEvent = 0 ; psop; psop = psop->next )
2228 unsigned flags = psop->pws->flags;
2229 int fd = psop->pws->fd;
2233 if( (r = FD_ISSET( fd, &pending_set[EVENT_IO_READ] )) ||
2234 (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] )) ||
2235 (e = FD_ISSET( fd, &pending_set[EVENT_IO_EXCEPT] )) )
2237 /* This code removes WS_FD flags on one-shot events (WS_FD_CLOSE,
2238 * WS_FD_CONNECT), otherwise it clears descriptors in the io_set.
2239 * Reenabling calls turn them back on.
2242 TRACE(winsock,"\tchecking psop = 0x%08x\n", (unsigned) psop );
2246 /* Now figure out what kind of event we've got. The worst problem
2247 * we have to contend with is that some out of control applications
2248 * really want to use mutually exclusive AsyncSelect() flags all at
2252 if((flags & WS_FD_ACCEPT) && (flags & WS_FD_LISTENING))
2254 /* WS_FD_ACCEPT is valid only if the socket is in the
2255 * listening state */
2257 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2260 FD_CLR( fd, &event_set[EVENT_IO_READ] ); /* reenabled by the next accept() */
2261 dwEvent = WSAMAKESELECTREPLY( WS_FD_ACCEPT, 0 );
2266 else if( flags & WS_FD_CONNECT )
2268 /* connecting socket */
2270 if( w || (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] )) )
2272 /* ready to write means that socket is connected
2274 * FIXME: Netscape calls AsyncSelect( s, ... WS_FD_CONNECT .. )
2275 * right after s = socket() and somehow "s" becomes writeable
2276 * before it goes through connect()!?!?
2279 psop->pws->flags |= WS_FD_CONNECTED;
2280 psop->pws->flags &= ~(WS_FD_CONNECT | WS_FD_INACTIVE);
2281 dwEvent = WSAMAKESELECTREPLY( WS_FD_CONNECT, 0 );
2283 if( flags & (WS_FD_READ | WS_FD_CLOSE))
2284 FD_SET( fd, &event_set[EVENT_IO_READ] );
2286 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2287 if( flags & WS_FD_WRITE )
2288 FD_SET( fd, &event_set[EVENT_IO_WRITE] );
2290 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2295 /* failure - do read() to get correct errno */
2297 if( read( fd, &dwErrBytes, sizeof(dwErrBytes) ) == -1 )
2299 dwEvent = WSAMAKESELECTREPLY( WS_FD_CONNECT, wsaErrno() );
2303 /* otherwise bPost stays FALSE, should probably clear event_set */
2307 /* connected socket, no WS_FD_OOB code for now. */
2309 if( flags & WS_FD_WRITE &&
2310 (w || (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] ))) )
2312 /* this will be reenabled when send() or sendto() fail with
2315 if( PostMessageA( psop->hWnd, psop->uMsg, (WPARAM)WS_PTR2HANDLE(psop->pws),
2316 (LPARAM)WSAMAKESELECTREPLY( WS_FD_WRITE, 0 ) ) )
2318 TRACE(winsock, "\t hwnd %04x - %04x, %08x\n",
2319 psop->hWnd, psop->uMsg, (unsigned)MAKELONG(WS_FD_WRITE, 0) );
2320 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2325 if( r && (flags & (WS_FD_READ | WS_FD_CLOSE)) )
2327 int val = (flags & WS_FD_RAW);
2329 /* WS_FD_RAW is set by the WSAAsyncSelect() init */
2332 if( !val && ioctl( fd, FIONREAD, (char*)&dwErrBytes) == -1 )
2336 dwEvent = WSAMAKESELECTREPLY( WS_FD_READ, wsaErrno() );
2338 else if( val || dwErrBytes )
2340 /* got pending data, will be reenabled by recv() or recvfrom() */
2342 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2343 dwEvent = WSAMAKESELECTREPLY( WS_FD_READ, 0 );
2347 /* 0 bytes to read - connection reset by peer? */
2350 val = read( fd, (char*)&dwErrBytes, sizeof(dwErrBytes));
2351 while( errno == EINTR );
2352 if( errno != EWOULDBLOCK )
2356 case 0: errno = ENETDOWN; /* soft reset, fall through */
2357 case -1: /* hard reset */
2358 dwEvent = WSAMAKESELECTREPLY( WS_FD_CLOSE, wsaErrno() );
2361 default: bPost = FALSE;
2362 continue; /* FIXME: this is real bad */
2365 else { bPost = FALSE; continue; } /* more weirdness */
2367 /* this is it, this socket is closed */
2369 psop->pws->flags &= ~(WS_FD_READ | WS_FD_CLOSE | WS_FD_WRITE);
2370 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2371 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2373 if( *max_fd == (fd + 1) ) (*max_fd)--;
2380 TRACE(winsock, "\t hwnd %04x - %04x, %08x\n",
2381 psop->hWnd, psop->uMsg, (unsigned)dwEvent );
2382 PostMessageA( psop->hWnd, psop->uMsg,
2383 (WPARAM)WS_PTR2HANDLE(psop->pws), (LPARAM)dwEvent );
2388 if( num_pending <= 0 ) break;
2391 TRACE(winsock, "\tdone, %i posted events\n", num_posted );
2392 return ( num_posted ) ? TRUE : FALSE;
2395 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, UINT lEvent)
2397 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
2398 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2400 TRACE(winsock, "(%08x): %04x, hWnd %04x, uMsg %08x, event %08x\n",
2401 (unsigned)pwsi, (SOCKET16)s, (HWND16)hWnd, uMsg, (unsigned)lEvent );
2402 if( _check_ws(pwsi, pws) )
2406 if( (psop = pws->psop) )
2408 /* delete previous control struct */
2410 if( psop == __ws_select_list )
2411 __ws_select_list = psop->next;
2413 psop->prev->next = psop->next;
2414 if( psop->next ) psop->next->prev = psop->prev;
2416 if( pws->flags & (WS_FD_ACCEPT | WS_FD_CONNECT | WS_FD_READ | WS_FD_CLOSE) )
2417 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
2418 if( pws->flags & (WS_FD_CONNECT | WS_FD_WRITE) )
2419 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
2421 TRACE(winsock,"\tremoving psop = 0x%08x\n", (unsigned) psop );
2423 WS_FREE( pws->psop );
2424 pws->flags &= ~(WS_FD_RAW | WS_FD_ACCEPT | WS_FD_CONNECT |
2425 WS_FD_READ | WS_FD_WRITE | WS_FD_CLOSE);
2431 psop = (ws_select_op*)WS_ALLOC(sizeof(ws_select_op));
2434 int sock_type, bytes = sizeof(int);
2436 WINSOCK_unblock_io( pws->fd, TRUE );
2439 psop->next = __ws_select_list;
2440 if( __ws_select_list )
2441 __ws_select_list->prev = psop;
2442 __ws_select_list = psop;
2449 pws->flags |= (0x0000FFFF & lEvent);
2450 getsockopt(pws->fd, SOL_SOCKET, SO_TYPE, &sock_type, &bytes);
2451 if( sock_type == SOCK_RAW ) pws->flags |= WS_FD_RAW;
2453 if( lEvent & (WS_FD_ACCEPT | WS_FD_CONNECT | WS_FD_READ | WS_FD_CLOSE) )
2454 EVENT_AddIO( pws->fd, EVENT_IO_READ );
2455 if( lEvent & (WS_FD_CONNECT | WS_FD_WRITE) )
2456 EVENT_AddIO( pws->fd, EVENT_IO_WRITE );
2458 /* TODO: handle WS_FD_ACCEPT right away if the socket is readable */
2460 TRACE(winsock,"\tcreating psop = 0x%08x\n", (unsigned)psop );
2462 return 0; /* success */
2464 else pwsi->err = WSAENOBUFS;
2468 else if( pwsi ) pwsi->err = WSAEINVAL;
2469 return SOCKET_ERROR;
2472 INT16 WINAPI WSAAsyncSelect16(SOCKET16 s, HWND16 hWnd, UINT16 wMsg, UINT lEvent)
2474 return (INT16)WSAAsyncSelect( s, hWnd, wMsg, lEvent );
2478 /***********************************************************************
2479 * __WSAFDIsSet() (WINSOCK.151)
2481 INT16 WINAPI __WSAFDIsSet16(SOCKET16 s, ws_fd_set16 *set)
2483 int i = set->fd_count;
2485 TRACE(winsock, "(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2488 if (set->fd_array[i] == s) return 1;
2492 /***********************************************************************
2493 * __WSAFDIsSet() (WSOCK32.151)
2495 INT WINAPI __WSAFDIsSet(SOCKET s, ws_fd_set32 *set)
2497 int i = set->fd_count;
2499 TRACE(winsock, "(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2502 if (set->fd_array[i] == s) return 1;
2506 /***********************************************************************
2507 * WSAIsBlocking() (WINSOCK.114)(WSOCK32.114)
2509 BOOL WINAPI WSAIsBlocking(void)
2511 /* By default WinSock should set all its sockets to non-blocking mode
2512 * and poll in PeekMessage loop when processing "blocking" ones. This
2513 * function is supposed to tell if the program is in this loop. Our
2514 * blocking calls are truly blocking so we always return FALSE.
2516 * Note: It is allowed to call this function without prior WSAStartup().
2519 TRACE(winsock, "\n");
2523 /***********************************************************************
2524 * WSACancelBlockingCall() (WINSOCK.113)(WSOCK32.113)
2526 INT WINAPI WSACancelBlockingCall(void)
2528 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2530 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2532 if( pwsi ) return 0;
2533 return SOCKET_ERROR;
2537 /***********************************************************************
2538 * WSASetBlockingHook16() (WINSOCK.109)
2540 FARPROC16 WINAPI WSASetBlockingHook16(FARPROC16 lpBlockFunc)
2543 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2545 TRACE(winsock, "(%08x): hook %08x\n",
2546 (unsigned)pwsi, (unsigned) lpBlockFunc);
2549 prev = (FARPROC16)pwsi->blocking_hook;
2550 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2551 pwsi->flags &= ~WSI_BLOCKINGHOOK;
2558 /***********************************************************************
2559 * WSASetBlockingHook32()
2561 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
2564 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2566 TRACE(winsock, "(%08x): hook %08x\n",
2567 (unsigned)pwsi, (unsigned) lpBlockFunc);
2569 prev = (FARPROC)pwsi->blocking_hook;
2570 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2571 pwsi->flags |= WSI_BLOCKINGHOOK;
2578 /***********************************************************************
2579 * WSAUnhookBlockingHook16() (WINSOCK.110)
2581 INT16 WINAPI WSAUnhookBlockingHook16(void)
2583 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2585 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2586 if( pwsi ) return (INT16)(pwsi->blocking_hook = 0);
2587 return SOCKET_ERROR;
2591 /***********************************************************************
2592 * WSAUnhookBlockingHook32()
2594 INT WINAPI WSAUnhookBlockingHook(void)
2596 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2598 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2601 pwsi->blocking_hook = 0;
2602 pwsi->flags &= ~WSI_BLOCKINGHOOK;
2605 return SOCKET_ERROR;
2609 * TCP/IP action codes.
2613 #define WSCNTL_TCPIP_QUERY_INFO 0x00000000
2614 #define WSCNTL_TCPIP_SET_INFO 0x00000001
2615 #define WSCNTL_TCPIP_ICMP_ECHO 0x00000002
2616 #define WSCNTL_TCPIP_TEST 0x00000003
2619 /***********************************************************************
2622 * WsControl seems to be an undocumented Win95 function. A lot of
2623 * discussion about WsControl can be found on the net, e.g.
2624 * Subject: Re: WSOCK32.DLL WsControl Exported Function
2625 * From: "Peter Rindfuss" <rindfuss-s@medea.wz-berlin.de>
2629 DWORD WINAPI WsControl(DWORD protocoll,DWORD action,
2630 LPVOID inbuf,LPDWORD inbuflen,
2631 LPVOID outbuf,LPDWORD outbuflen)
2635 case WSCNTL_TCPIP_ICMP_ECHO:
2637 unsigned int addr = *(unsigned int*)inbuf;
2639 int timeout= *(unsigned int*)(inbuf+4);
2640 short x1 = *(unsigned short*)(inbuf+8);
2641 short sendbufsize = *(unsigned short*)(inbuf+10);
2642 char x2 = *(unsigned char*)(inbuf+12);
2643 char ttl = *(unsigned char*)(inbuf+13);
2644 char service = *(unsigned char*)(inbuf+14);
2645 char type= *(unsigned char*)(inbuf+15); /* 0x2: don't fragment*/
2648 FIXME(winsock,"(ICMP_ECHO) to 0x%08x stub \n", addr);
2652 FIXME(winsock,"(%lx,%lx,%p,%p,%p,%p) stub\n",
2653 protocoll,action,inbuf,inbuflen,outbuf,outbuflen);
2657 /*********************************************************
2658 * WS_s_perror WSOCK32.1108
2660 void WINAPI WS_s_perror(LPCSTR message)
2662 FIXME(winsock,"(%s): stub\n",message);
2667 /* ----------------------------------- end of API stuff */
2669 /* ----------------------------------- helper functions -
2671 * TODO: Merge WS_dup_..() stuff into one function that
2672 * would operate with a generic structure containing internal
2673 * pointers (via a template of some kind).
2676 static int list_size(char** l, int item_size)
2681 j += (item_size) ? item_size : strlen(l[i]) + 1;
2682 j += (i + 1) * sizeof(char*); }
2686 static int list_dup(char** l_src, char* ref, char* base, int item_size)
2688 /* base is either either equal to ref or 0 or SEGPTR */
2691 char** l_to = (char**)ref;
2694 for(j=0;l_src[j];j++) ;
2695 p += (j + 1) * sizeof(char*);
2697 { l_to[i] = base + (p - ref);
2698 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
2699 memcpy(p, l_src[i], k); p += k; }
2706 static int hostent_size(struct hostent* p_he)
2710 { size = sizeof(struct hostent);
2711 size += strlen(p_he->h_name) + 1;
2712 size += list_size(p_he->h_aliases, 0);
2713 size += list_size(p_he->h_addr_list, p_he->h_length ); }
2717 int WS_dup_he(LPWSINFO pwsi, struct hostent* p_he, int flag)
2719 /* Convert hostent structure into ws_hostent so that the data fits
2720 * into pwsi->buffer. Internal pointers can be linear, SEGPTR, or
2721 * relative to pwsi->buffer depending on "flag" value. Returns size
2722 * of the data copied (also in the pwsi->buflen).
2725 int size = hostent_size(p_he);
2729 struct ws_hostent* p_to;
2730 char* p_name,*p_aliases,*p_addr,*p_base,*p;
2732 _check_buffer_he(pwsi, size);
2733 p_to = (struct ws_hostent*)pwsi->he;
2734 p = (char*)pwsi->he;
2735 p_base = (flag & WS_DUP_OFFSET) ? NULL
2736 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2737 p += sizeof(struct ws_hostent);
2739 strcpy(p, p_he->h_name); p += strlen(p) + 1;
2741 p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)pwsi->he), 0);
2743 list_dup(p_he->h_addr_list, p, p_base + (p - (char*)pwsi->he), p_he->h_length);
2745 p_to->h_addrtype = (INT16)p_he->h_addrtype;
2746 p_to->h_length = (INT16)p_he->h_length;
2747 p_to->h_name = (SEGPTR)(p_base + (p_name - (char*)pwsi->he));
2748 p_to->h_aliases = (SEGPTR)(p_base + (p_aliases - (char*)pwsi->he));
2749 p_to->h_addr_list = (SEGPTR)(p_base + (p_addr - (char*)pwsi->he));
2751 size += (sizeof(struct ws_hostent) - sizeof(struct hostent));
2756 /* ----- protoent */
2758 static int protoent_size(struct protoent* p_pe)
2762 { size = sizeof(struct protoent);
2763 size += strlen(p_pe->p_name) + 1;
2764 size += list_size(p_pe->p_aliases, 0); }
2768 int WS_dup_pe(LPWSINFO pwsi, struct protoent* p_pe, int flag)
2770 int size = protoent_size(p_pe);
2773 struct ws_protoent* p_to;
2774 char* p_name,*p_aliases,*p_base,*p;
2776 _check_buffer_pe(pwsi, size);
2777 p_to = (struct ws_protoent*)pwsi->pe;
2778 p = (char*)pwsi->pe;
2779 p_base = (flag & WS_DUP_OFFSET) ? NULL
2780 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2781 p += sizeof(struct ws_protoent);
2783 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
2785 list_dup(p_pe->p_aliases, p, p_base + (p - (char*)pwsi->pe), 0);
2787 p_to->p_proto = (INT16)p_pe->p_proto;
2788 p_to->p_name = (SEGPTR)(p_base) + (p_name - (char*)pwsi->pe);
2789 p_to->p_aliases = (SEGPTR)((p_base) + (p_aliases - (char*)pwsi->pe));
2791 size += (sizeof(struct ws_protoent) - sizeof(struct protoent));
2798 static int servent_size(struct servent* p_se)
2802 { size += sizeof(struct servent);
2803 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
2804 size += list_size(p_se->s_aliases, 0); }
2808 int WS_dup_se(LPWSINFO pwsi, struct servent* p_se, int flag)
2810 int size = servent_size(p_se);
2813 struct ws_servent* p_to;
2814 char* p_name,*p_aliases,*p_proto,*p_base,*p;
2816 _check_buffer_se(pwsi, size);
2817 p_to = (struct ws_servent*)pwsi->se;
2818 p = (char*)pwsi->se;
2819 p_base = (flag & WS_DUP_OFFSET) ? NULL
2820 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2821 p += sizeof(struct ws_servent);
2823 strcpy(p, p_se->s_name); p += strlen(p) + 1;
2825 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
2827 list_dup(p_se->s_aliases, p, p_base + (p - (char*)pwsi->se), 0);
2829 p_to->s_port = (INT16)p_se->s_port;
2830 p_to->s_name = (SEGPTR)(p_base + (p_name - (char*)pwsi->se));
2831 p_to->s_proto = (SEGPTR)(p_base + (p_proto - (char*)pwsi->se));
2832 p_to->s_aliases = (SEGPTR)(p_base + (p_aliases - (char*)pwsi->se));
2834 size += (sizeof(struct ws_servent) - sizeof(struct servent));
2839 /* ----------------------------------- error handling */
2841 UINT16 wsaErrno(void)
2843 int loc_errno = errno;
2844 #ifdef HAVE_STRERROR
2845 WARN(winsock, "errno %d, (%s).\n", loc_errno, strerror(loc_errno));
2847 WARN(winsock, "errno %d\n", loc_errno);
2852 case EINTR: return WSAEINTR;
2853 case EBADF: return WSAEBADF;
2854 case EACCES: return WSAEACCES;
2855 case EFAULT: return WSAEFAULT;
2856 case EINVAL: return WSAEINVAL;
2857 case EMFILE: return WSAEMFILE;
2858 case EWOULDBLOCK: return WSAEWOULDBLOCK;
2859 case EINPROGRESS: return WSAEINPROGRESS;
2860 case EALREADY: return WSAEALREADY;
2861 case ENOTSOCK: return WSAENOTSOCK;
2862 case EDESTADDRREQ: return WSAEDESTADDRREQ;
2863 case EMSGSIZE: return WSAEMSGSIZE;
2864 case EPROTOTYPE: return WSAEPROTOTYPE;
2865 case ENOPROTOOPT: return WSAENOPROTOOPT;
2866 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
2867 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
2868 case EOPNOTSUPP: return WSAEOPNOTSUPP;
2869 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
2870 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
2871 case EADDRINUSE: return WSAEADDRINUSE;
2872 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
2873 case ENETDOWN: return WSAENETDOWN;
2874 case ENETUNREACH: return WSAENETUNREACH;
2875 case ENETRESET: return WSAENETRESET;
2876 case ECONNABORTED: return WSAECONNABORTED;
2878 case ECONNRESET: return WSAECONNRESET;
2879 case ENOBUFS: return WSAENOBUFS;
2880 case EISCONN: return WSAEISCONN;
2881 case ENOTCONN: return WSAENOTCONN;
2882 case ESHUTDOWN: return WSAESHUTDOWN;
2883 case ETOOMANYREFS: return WSAETOOMANYREFS;
2884 case ETIMEDOUT: return WSAETIMEDOUT;
2885 case ECONNREFUSED: return WSAECONNREFUSED;
2886 case ELOOP: return WSAELOOP;
2887 case ENAMETOOLONG: return WSAENAMETOOLONG;
2888 case EHOSTDOWN: return WSAEHOSTDOWN;
2889 case EHOSTUNREACH: return WSAEHOSTUNREACH;
2890 case ENOTEMPTY: return WSAENOTEMPTY;
2892 case EPROCLIM: return WSAEPROCLIM;
2895 case EUSERS: return WSAEUSERS;
2898 case EDQUOT: return WSAEDQUOT;
2901 case ESTALE: return WSAESTALE;
2904 case EREMOTE: return WSAEREMOTE;
2907 /* just in case we ever get here and there are no problems */
2910 WARN(winsock, "Unknown errno %d!\n", loc_errno);
2911 return WSAEOPNOTSUPP;
2915 UINT16 wsaHerrno(void)
2917 int loc_errno = h_errno;
2919 WARN(winsock, "h_errno %d.\n", loc_errno);
2923 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
2924 case TRY_AGAIN: return WSATRY_AGAIN;
2925 case NO_RECOVERY: return WSANO_RECOVERY;
2926 case NO_DATA: return WSANO_DATA;
2930 WARN(winsock,"Unknown h_errno %d!\n", loc_errno);
2931 return WSAEOPNOTSUPP;