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>
22 #if defined(__svr4__) || defined(__sun)
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 DEFAULT_DEBUG_CHANNEL(winsock)
65 #define DEBUG_SOCKADDR 0
66 #define dump_sockaddr(a) \
67 DUMP("sockaddr_in: family %d, address %s, port %d\n", \
68 ((struct sockaddr_in *)a)->sin_family, \
69 inet_ntoa(((struct sockaddr_in *)a)->sin_addr), \
70 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,
100 static int _px_sock_ops[] =
101 { SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST,
102 SO_LINGER, SO_OOBINLINE, SO_SNDBUF, SO_RCVBUF, SO_ERROR, SO_TYPE,
109 static INT _ws_tcp_ops[] = {
115 static int _px_tcp_ops[] = {
122 static int _check_ws(LPWSINFO pwsi, ws_socket* pws);
123 static char* _check_buffer(LPWSINFO pwsi, int size);
125 /* ----------------------------------- non-blocking I/O */
127 static int WINSOCK_unblock_io(int fd, int noblock)
131 fd_flags = fcntl(fd, F_GETFL, 0);
132 if (fcntl(fd, F_SETFL, (noblock)? fd_flags | O_NONBLOCK
133 : fd_flags & ~O_NONBLOCK ) != -1) return 0;
137 /***********************************************************************
140 * Converts socket flags from Windows format.
142 static void convert_sockopt(INT *level, INT *optname)
149 for(i=0; _ws_sock_ops[i]; i++)
150 if( _ws_sock_ops[i] == *optname ) break;
151 if( _ws_sock_ops[i] ) *optname = _px_sock_ops[i];
152 else FIXME(winsock, "Unknown SOL_SOCKET optname %d\n", *optname);
155 *level = IPPROTO_TCP;
156 for(i=0; _ws_tcp_ops[i]; i++)
157 if ( _ws_tcp_ops[i] == *optname ) break;
158 if( _ws_tcp_ops[i] ) *optname = _px_tcp_ops[i];
159 else FIXME(winsock, "Unknown IPPROTO_TCP optname %d\n", *optname);
164 /* ----------------------------------- Per-thread info (or per-process?) */
166 static LPWSINFO wsi_find(HTASK16 hTask)
168 TDB* pTask = (TDB*)GlobalLock16(hTask);
171 if( pTask->pwsi ) return pTask->pwsi;
174 LPWSINFO pwsi = _wsi_list;
175 while( pwsi && pwsi->tid != hTask ) pwsi = pwsi->next;
177 WARN(winsock,"(pwsi=0x%08x,task=0x%04x):Loose wsi struct! \n",
178 (unsigned)pwsi, hTask );
185 static ws_socket* wsi_alloc_socket(LPWSINFO pwsi, int fd)
187 /* Initialize a new entry in the socket table */
189 if( pwsi->last_free >= 0 )
191 int i = pwsi->last_free;
193 pwsi->last_free = pwsi->sock[i].flags; /* free list */
194 pwsi->sock[i].fd = fd;
195 pwsi->sock[i].flags = 0;
196 return &pwsi->sock[i];
201 static int wsi_strtolo(LPWSINFO pwsi, const char* name, const char* opt)
203 /* Stuff a lowercase copy of the string into the local buffer */
205 int i = strlen(name) + 2;
206 char* p = _check_buffer(pwsi, i + ((opt)?strlen(opt):0));
210 do *p++ = tolower(*name); while(*name++);
211 i = (p - (char*)(pwsi->buffer));
212 if( opt ) do *p++ = tolower(*opt); while(*opt++);
218 static fd_set* fd_set_import( fd_set* fds, LPWSINFO pwsi, void* wsfds, int* highfd, BOOL b32 )
220 /* translate Winsock fd set into local fd set */
224 #define wsfds16 ((ws_fd_set16*)wsfds)
225 #define wsfds32 ((ws_fd_set32*)wsfds)
230 count = b32 ? wsfds32->fd_count : wsfds16->fd_count;
232 for( i = 0; i < count; i++ )
234 pws = (b32) ? (ws_socket*)WS_HANDLE2PTR(wsfds32->fd_array[i])
235 : (ws_socket*)WS_HANDLE2PTR(wsfds16->fd_array[i]);
236 if( _check_ws(pwsi, pws) )
238 if( pws->fd > *highfd ) *highfd = pws->fd;
239 FD_SET(pws->fd, fds);
249 inline static int sock_error_p(int s)
251 unsigned int optval, optlen;
253 optlen = sizeof(optval);
254 getsockopt(s, SOL_SOCKET, SO_ERROR, &optval, &optlen);
255 if (optval) WARN(winsock, "\t[%i] error: %d\n", s, optval);
259 static int fd_set_export( LPWSINFO pwsi, fd_set* fds, fd_set* exceptfds, void* wsfds, BOOL b32 )
263 /* translate local fd set into Winsock fd set, adding
264 * errors to exceptfds (only if app requested it) */
268 #define wsfds16 ((ws_fd_set16*)wsfds)
269 #define wsfds32 ((ws_fd_set32*)wsfds)
270 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
272 for( i = 0, j = 0; i < count; i++ )
274 ws_socket *pws = (b32) ? (ws_socket*)WS_HANDLE2PTR(wsfds32->fd_array[i])
275 : (ws_socket*)WS_HANDLE2PTR(wsfds16->fd_array[i]);
276 if( _check_ws(pwsi, pws) )
280 if( FD_ISSET(fd, fds) )
282 if ( exceptfds && sock_error_p(fd) )
284 FD_SET(fd, exceptfds);
288 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
290 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
295 if( b32 ) wsfds32->fd_count = j;
296 else wsfds16->fd_count = j;
298 TRACE(winsock, "\n");
305 HANDLE16 __ws_gethandle( void* ptr )
307 return (HANDLE16)WS_PTR2HANDLE(ptr);
310 void* __ws_memalloc( int size )
312 return WS_ALLOC(size);
315 void __ws_memfree(void* ptr)
320 /* ----------------------------------- API -----
322 * Init / cleanup / error checking.
325 /***********************************************************************
326 * WSAStartup16() (WINSOCK.115)
328 * Create socket control struct, attach it to the global list and
329 * update a pointer in the task struct.
331 INT16 WINAPI WSAStartup16(UINT16 wVersionRequested, LPWSADATA lpWSAData)
333 WSADATA WINSOCK_data = { 0x0101, 0x0101,
337 #elif defined(__NetBSD__)
341 #elif defined(__FreeBSD__)
343 #elif defined(__OpenBSD__)
348 WS_MAX_SOCKETS_PER_PROCESS,
349 WS_MAX_UDP_DATAGRAM, (SEGPTR)NULL };
350 HTASK16 tid = GetCurrentTask();
353 TRACE(winsock, "verReq=%x\n", wVersionRequested);
355 if (LOBYTE(wVersionRequested) < 1 || (LOBYTE(wVersionRequested) == 1 &&
356 HIBYTE(wVersionRequested) < 1)) return WSAVERNOTSUPPORTED;
358 if (!lpWSAData) return WSAEINVAL;
360 /* initialize socket heap */
364 _WSHeap = HeapCreate(HEAP_ZERO_MEMORY, 8120, 32768);
365 if( !(_ws_stub = WS_ALLOC(0x10)) )
367 ERR(winsock,"Fatal: failed to create WinSock heap\n");
371 if( _WSHeap == 0 ) return WSASYSNOTREADY;
373 /* create socket array for this task */
375 pwsi = wsi_find(GetCurrentTask());
378 TDB* pTask = (TDB*)GlobalLock16( tid );
380 if( (pwsi = (LPWSINFO)WS_ALLOC( sizeof(WSINFO))) )
384 for( i = 0; i < WS_MAX_SOCKETS_PER_PROCESS; i++ )
386 pwsi->sock[i].fd = -1;
387 pwsi->sock[i].flags = i + 1;
389 pwsi->sock[WS_MAX_SOCKETS_PER_PROCESS - 1].flags = -1;
391 else return WSASYSNOTREADY;
393 /* add this control struct to the global list */
397 _wsi_list->prev = pwsi;
398 pwsi->next = _wsi_list;
402 else pwsi->num_startup++;
404 /* return winsock information */
406 memcpy(lpWSAData, &WINSOCK_data, sizeof(WINSOCK_data));
408 TRACE(winsock, "succeeded\n");
412 /***********************************************************************
413 * WSAStartup32() (WSOCK32.115)
415 INT WINAPI WSAStartup(UINT wVersionRequested, LPWSADATA lpWSAData)
417 return WSAStartup16( wVersionRequested, lpWSAData );
420 /***********************************************************************
421 * WSACleanup() (WINSOCK.116)
423 * Cleanup functions of varying impact.
425 void WINSOCK_Shutdown()
427 /* Called on exit(), has to remove all outstanding async DNS processes. */
430 INT WINSOCK_DeleteTaskWSI( TDB* pTask, LPWSINFO pwsi )
432 /* WSACleanup() backend, called on task termination as well.
433 * Real DLL would have registered its own signal handler with
434 * TaskSetSignalHandler() and waited until USIG_TERMINATION/USIG_GPF
435 * but this scheme is much more straightforward.
440 if( --pwsi->num_startup > 0 ) return 0;
442 /* unlink socket control struct */
444 if( pwsi == _wsi_list )
445 _wsi_list = pwsi->next;
447 pwsi->prev->next = pwsi->next;
448 if( pwsi->next ) pwsi->next->prev = pwsi->prev;
450 if( _wsi_list == NULL )
451 WINSOCK_Shutdown(); /* just in case */
453 if( pwsi->flags & WSI_BLOCKINGCALL )
454 TRACE(winsock,"\tinside blocking call!\n");
456 /* FIXME: aop_control() doesn't decrement pwsi->num_async_rq
458 * if( pwsi->num_async_rq )
459 * WARN(winsock,"\thave %i outstanding async ops!\n", pwsi->num_async_rq );
462 for(i = 0, j = 0, n = 0; i < WS_MAX_SOCKETS_PER_PROCESS; i++)
463 if( pwsi->sock[i].fd != -1 )
465 if( pwsi->sock[i].psop )
468 WSAAsyncSelect( (SOCKET16)WS_PTR2HANDLE(pwsi->sock + i), 0, 0, 0 );
470 close(pwsi->sock[i].fd); j++;
473 TRACE(winsock,"\tclosed %i sockets, killed %i async selects!\n", j, n);
475 /* delete scratch buffers */
477 if( pwsi->buffer ) SEGPTR_FREE(pwsi->buffer);
478 if( pwsi->dbuffer ) SEGPTR_FREE(pwsi->dbuffer);
482 memset( pwsi, 0, sizeof(WSINFO) );
487 INT WINAPI WSACleanup(void)
489 HTASK16 hTask = GetCurrentTask();
491 TRACE(winsock, "(%04x)\n", hTask );
494 LPWSINFO pwsi = wsi_find(hTask);
496 return WINSOCK_DeleteTaskWSI( (TDB*)GlobalLock16(hTask), pwsi );
500 WINSOCK_Shutdown(); /* remove all outstanding DNS requests */
505 /***********************************************************************
506 * WSAGetLastError() (WSOCK32.111)(WINSOCK.111)
508 INT WINAPI WSAGetLastError(void)
510 return GetLastError();
513 /***********************************************************************
514 * WSASetLastError32() (WSOCK32.112)
516 void WINAPI WSASetLastError(INT iError) {
517 SetLastError(iError);
520 /***********************************************************************
521 * WSASetLastError16() (WINSOCK.112)
523 void WINAPI WSASetLastError16(INT16 iError)
525 WSASetLastError(iError);
528 int _check_ws(LPWSINFO pwsi, ws_socket* pws)
532 if( pwsi->flags & WSI_BLOCKINGCALL ) SetLastError(WSAEINPROGRESS);
533 else if( WSI_CHECK_RANGE(pwsi, pws) ) return 1;
534 else SetLastError(WSAENOTSOCK);
539 char* _check_buffer(LPWSINFO pwsi, int size)
541 if( pwsi->buffer && pwsi->buflen >= size ) return pwsi->buffer;
542 else SEGPTR_FREE(pwsi->buffer);
544 pwsi->buffer = (char*)SEGPTR_ALLOC((pwsi->buflen = size));
548 struct ws_hostent* _check_buffer_he(LPWSINFO pwsi, int size)
550 if( pwsi->he && pwsi->helen >= size ) return pwsi->he;
551 else SEGPTR_FREE(pwsi->he);
553 pwsi->he = (struct ws_hostent*)SEGPTR_ALLOC((pwsi->helen = size));
557 struct ws_servent* _check_buffer_se(LPWSINFO pwsi, int size)
559 if( pwsi->se && pwsi->selen >= size ) return pwsi->se;
560 else SEGPTR_FREE(pwsi->se);
562 pwsi->se = (struct ws_servent*)SEGPTR_ALLOC((pwsi->selen = size));
566 struct ws_protoent* _check_buffer_pe(LPWSINFO pwsi, int size)
568 if( pwsi->pe && pwsi->pelen >= size ) return pwsi->pe;
569 else SEGPTR_FREE(pwsi->pe);
571 pwsi->pe = (struct ws_protoent*)SEGPTR_ALLOC((pwsi->pelen = size));
575 /* ----------------------------------- i/o APIs */
577 /***********************************************************************
578 * accept() (WSOCK32.1)
580 SOCKET WINAPI WINSOCK_accept(SOCKET s, struct sockaddr *addr,
583 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR((SOCKET16)s);
584 LPWSINFO pwsi = wsi_find(GetCurrentTask());
586 struct ws_sockaddr_ipx* addr2 = (struct ws_sockaddr_ipx *)addr;
589 TRACE(winsock, "(%08x): socket %04x\n",
590 (unsigned)pwsi, (UINT16)s );
591 if( _check_ws(pwsi, pws) )
595 fd_flags = fcntl(pws->fd, F_GETFL, 0);
597 if( (sock = accept(pws->fd, addr, addrlen32)) >= 0 )
599 ws_socket* pnew = wsi_alloc_socket(pwsi, sock);
602 s = (SOCKET)WS_PTR2HANDLE(pnew);
603 if( pws->psop && pws->flags & WS_FD_ACCEPT )
605 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
607 /* async select the accept()'ed socket */
608 WSAAsyncSelect( s, pws->psop->hWnd, pws->psop->uMsg,
609 pws->flags & ~WS_FD_ACCEPT );
612 if (addr && ((struct sockaddr_ipx *)addr)->sipx_family == AF_IPX) {
613 addr = (struct sockaddr *)
614 malloc(addrlen32 ? *addrlen32 : sizeof(*addr2));
616 addrlen32 ? *addrlen32 : sizeof(*addr2));
617 addr2->sipx_family = WS_AF_IPX;
618 addr2->sipx_network = ((struct sockaddr_ipx *)addr)->sipx_network;
619 addr2->sipx_port = ((struct sockaddr_ipx *)addr)->sipx_port;
620 memcpy(addr2->sipx_node,
621 ((struct sockaddr_ipx *)addr)->sipx_node, IPX_NODE_LEN);
627 else SetLastError(WSAENOBUFS);
628 } else SetLastError(wsaErrno());
631 if (addr && ((struct sockaddr_ipx *)addr)->sipx_family == AF_IPX) {
632 addr = (struct sockaddr *)
633 malloc(addrlen32 ? *addrlen32 : sizeof(*addr2));
634 memcpy(addr, addr2, addrlen32 ? *addrlen32 : sizeof(*addr2));
635 addr2->sipx_family = WS_AF_IPX;
636 addr2->sipx_network = ((struct sockaddr_ipx *)addr)->sipx_network;
637 addr2->sipx_port = ((struct sockaddr_ipx *)addr)->sipx_port;
638 memcpy(addr2->sipx_node,
639 ((struct sockaddr_ipx *)addr)->sipx_node, IPX_NODE_LEN);
643 return INVALID_SOCKET;
646 /***********************************************************************
647 * accept() (WINSOCK.1)
649 SOCKET16 WINAPI WINSOCK_accept16(SOCKET16 s, struct sockaddr* addr,
652 INT addrlen32 = addrlen16 ? *addrlen16 : 0;
653 SOCKET retSocket = WINSOCK_accept( s, addr, &addrlen32 );
654 if( addrlen16 ) *addrlen16 = (INT16)addrlen32;
655 return (SOCKET16)retSocket;
658 /***********************************************************************
661 INT WINAPI WINSOCK_bind(SOCKET s, struct sockaddr *name, INT namelen)
663 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
664 LPWSINFO pwsi = wsi_find(GetCurrentTask());
666 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
669 TRACE(winsock, "(%08x): socket %04x, ptr %8x, length %d\n",
670 (unsigned)pwsi, s, (int) name, namelen);
675 if ( _check_ws(pwsi, pws) )
677 /* FIXME: what family does this really map to on the Unix side? */
678 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_PUP)
679 ((struct ws_sockaddr_ipx *)name)->sipx_family = AF_UNSPEC;
682 ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_IPX)
684 name = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
685 memset(name, '\0', sizeof(struct sockaddr_ipx));
686 ((struct sockaddr_ipx *)name)->sipx_family = AF_IPX;
687 ((struct sockaddr_ipx *)name)->sipx_port = name2->sipx_port;
688 ((struct sockaddr_ipx *)name)->sipx_network = name2->sipx_network;
689 memcpy(((struct sockaddr_ipx *)name)->sipx_node,
690 name2->sipx_node, IPX_NODE_LEN);
691 namelen = sizeof(struct sockaddr_ipx);
694 if ( namelen >= sizeof(*name) )
696 if ( name && (((struct ws_sockaddr_in *)name)->sin_family == AF_INET
698 || ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX
702 if ( bind(pws->fd, name, namelen) < 0 )
704 int loc_errno = errno;
705 WARN(winsock, "\tfailure - errno = %i\n", errno);
709 case EBADF: SetLastError(WSAENOTSOCK); break;
710 case EADDRNOTAVAIL: SetLastError(WSAEINVAL); break;
711 default: SetLastError(wsaErrno());break;
716 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
719 return 0; /* success */
721 } else SetLastError(WSAEAFNOSUPPORT);
722 } else SetLastError(WSAEFAULT);
724 if (name && ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
731 /***********************************************************************
734 INT16 WINAPI WINSOCK_bind16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
736 return (INT16)WINSOCK_bind( s, name, namelen );
739 /***********************************************************************
740 * closesocket() (WSOCK32.3)
742 INT WINAPI WINSOCK_closesocket(SOCKET s)
744 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
745 LPWSINFO pwsi = wsi_find(GetCurrentTask());
747 TRACE(winsock, "(%08x): socket %08x\n", (unsigned)pwsi, s);
749 if( _check_ws(pwsi, pws) )
753 if( pws->psop ) WSAAsyncSelect( s, 0, 0, 0 );
756 pws->flags = (unsigned)pwsi->last_free;
757 pwsi->last_free = pws - &pwsi->sock[0]; /* add to free list */
761 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
766 /***********************************************************************
767 * closesocket() (WINSOCK.3)
769 INT16 WINAPI WINSOCK_closesocket16(SOCKET16 s)
771 return (INT16)WINSOCK_closesocket(s);
774 /***********************************************************************
775 * connect() (WSOCK32.4)
777 INT WINAPI WINSOCK_connect(SOCKET s, struct sockaddr *name, INT namelen)
779 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
780 LPWSINFO pwsi = wsi_find(GetCurrentTask());
782 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
785 TRACE(winsock, "(%08x): socket %04x, ptr %8x, length %d\n",
786 (unsigned)pwsi, s, (int) name, namelen);
791 if( _check_ws(pwsi, pws) )
793 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_PUP)
794 ((struct ws_sockaddr_ipx *)name)->sipx_family = AF_UNSPEC;
796 else if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_IPX)
798 name = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
799 memset(name, '\0', sizeof(struct sockaddr_ipx));
800 ((struct sockaddr_ipx *)name)->sipx_family = AF_IPX;
801 ((struct sockaddr_ipx *)name)->sipx_port = name2->sipx_port;
802 ((struct sockaddr_ipx *)name)->sipx_network = name2->sipx_network;
803 memcpy(((struct sockaddr_ipx *)name)->sipx_node,
804 name2->sipx_node, IPX_NODE_LEN);
805 namelen = sizeof(struct sockaddr_ipx);
808 if (connect(pws->fd, name, namelen) == 0)
810 if( pws->psop && (pws->flags & WS_FD_CONNECT) )
812 /* application did AsyncSelect() but then went
813 * ahead and called connect() without waiting for
816 * FIXME: Do we have to post a notification message
820 if( !(pws->flags & WS_FD_CONNECTED) )
822 if( pws->flags & (WS_FD_READ | WS_FD_CLOSE) )
823 EVENT_AddIO( pws->fd, EVENT_IO_READ );
825 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
826 if( pws->flags & WS_FD_WRITE )
827 EVENT_AddIO( pws->fd, EVENT_IO_WRITE );
829 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
832 pws->flags |= WS_FD_CONNECTED;
833 pws->flags &= ~(WS_FD_INACTIVE | WS_FD_CONNECT | WS_FD_LISTENING);
835 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
840 SetLastError((errno == EINPROGRESS) ? WSAEWOULDBLOCK : wsaErrno());
843 if (name && ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
849 /***********************************************************************
850 * connect() (WINSOCK.4)
852 INT16 WINAPI WINSOCK_connect16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
854 return (INT16)WINSOCK_connect( s, name, namelen );
857 /***********************************************************************
858 * getpeername() (WSOCK32.5)
860 INT WINAPI WINSOCK_getpeername(SOCKET s, struct sockaddr *name,
863 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
864 LPWSINFO pwsi = wsi_find(GetCurrentTask());
866 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
869 TRACE(winsock, "(%08x): socket: %04x, ptr %8x, ptr %8x\n",
870 (unsigned)pwsi, s, (int) name, *namelen);
871 if( _check_ws(pwsi, pws) )
873 if (getpeername(pws->fd, name, namelen) == 0) {
875 if (((struct ws_sockaddr_ipx *)name)->sipx_family == AF_IPX) {
876 name = (struct sockaddr *)
877 malloc(namelen ? *namelen : sizeof(*name2));
878 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
879 name2->sipx_family = WS_AF_IPX;
880 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
881 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
882 memcpy(name2->sipx_node,
883 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
889 SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno());
892 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == AF_IPX) {
893 name = (struct sockaddr *) malloc(namelen ? *namelen : sizeof(*name2));
894 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
895 name2->sipx_family = WS_AF_IPX;
896 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
897 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
898 memcpy(name2->sipx_node,
899 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
906 /***********************************************************************
907 * getpeername() (WINSOCK.5)
909 INT16 WINAPI WINSOCK_getpeername16(SOCKET16 s, struct sockaddr *name,
912 INT namelen32 = *namelen16;
913 INT retVal = WINSOCK_getpeername( s, name, &namelen32 );
919 *namelen16 = namelen32;
920 return (INT16)retVal;
923 /***********************************************************************
924 * getsockname() (WSOCK32.6)
926 INT WINAPI WINSOCK_getsockname(SOCKET s, struct sockaddr *name,
929 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
930 LPWSINFO pwsi = wsi_find(GetCurrentTask());
932 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
935 TRACE(winsock, "(%08x): socket: %04x, ptr %8x, ptr %8x\n",
936 (unsigned)pwsi, s, (int) name, (int) *namelen);
937 if( _check_ws(pwsi, pws) )
939 if (getsockname(pws->fd, name, namelen) == 0) {
941 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX) {
942 name = (struct sockaddr *)
943 malloc(namelen ? *namelen : sizeof(*name2));
944 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
945 name2->sipx_family = WS_AF_IPX;
946 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
947 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
948 memcpy(name2->sipx_node,
949 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
955 SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno());
958 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == AF_IPX) {
959 name = (struct sockaddr *) malloc(namelen ? *namelen : sizeof(*name2));
960 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
961 name2->sipx_family = WS_AF_IPX;
962 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
963 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
964 memcpy(name2->sipx_node,
965 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
972 /***********************************************************************
973 * getsockname() (WINSOCK.6)
975 INT16 WINAPI WINSOCK_getsockname16(SOCKET16 s, struct sockaddr *name,
982 INT namelen32 = *namelen16;
983 retVal = WINSOCK_getsockname( s, name, &namelen32 );
984 *namelen16 = namelen32;
991 else retVal = SOCKET_ERROR;
992 return (INT16)retVal;
996 /***********************************************************************
997 * getsockopt() (WSOCK32.7)
999 INT WINAPI WINSOCK_getsockopt(SOCKET s, INT level,
1000 INT optname, char *optval, INT *optlen)
1002 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1003 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1005 TRACE(winsock, "(%08x): socket: %04x, opt %d, ptr %8x, ptr %8x\n",
1006 (unsigned)pwsi, s, level, (int) optval, (int) *optlen);
1007 if( _check_ws(pwsi, pws) )
1009 convert_sockopt(&level, &optname);
1010 if (getsockopt(pws->fd, (int) level, optname, optval, optlen) == 0 )
1012 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1014 return SOCKET_ERROR;
1017 /***********************************************************************
1018 * getsockopt() (WINSOCK.7)
1020 INT16 WINAPI WINSOCK_getsockopt16(SOCKET16 s, INT16 level,
1021 INT16 optname, char *optval, INT16 *optlen)
1026 if( optlen ) optlen32 = *optlen; else p = NULL;
1027 retVal = WINSOCK_getsockopt( s, (UINT16)level, optname, optval, p );
1028 if( optlen ) *optlen = optlen32;
1029 return (INT16)retVal;
1032 /***********************************************************************
1033 * htonl() (WINSOCK.8)(WSOCK32.8)
1035 u_long WINAPI WINSOCK_htonl(u_long hostlong) { return( htonl(hostlong) ); }
1036 /***********************************************************************
1037 * htons() (WINSOCK.9)(WSOCK32.9)
1039 u_short WINAPI WINSOCK_htons(u_short hostshort) { return( htons(hostshort) ); }
1040 /***********************************************************************
1041 * inet_addr() (WINSOCK.10)(WSOCK32.10)
1043 u_long WINAPI WINSOCK_inet_addr(char *cp) { return( inet_addr(cp) ); }
1044 /***********************************************************************
1045 * htohl() (WINSOCK.14)(WSOCK32.14)
1047 u_long WINAPI WINSOCK_ntohl(u_long netlong) { return( ntohl(netlong) ); }
1048 /***********************************************************************
1049 * ntohs() (WINSOCK.15)(WSOCK32.15)
1051 u_short WINAPI WINSOCK_ntohs(u_short netshort) { return( ntohs(netshort) ); }
1053 /***********************************************************************
1054 * inet_ntoa() (WINSOCK.11)(WSOCK32.11)
1056 char* WINAPI WINSOCK_inet_ntoa(struct in_addr in)
1058 /* use "buffer for dummies" here because some applications have
1059 * propensity to decode addresses in ws_hostent structure without
1060 * saving them first...
1063 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1067 char* s = inet_ntoa(in);
1070 if( pwsi->dbuffer == NULL )
1071 if((pwsi->dbuffer = (char*) SEGPTR_ALLOC(32)) == NULL )
1073 SetLastError(WSAENOBUFS);
1076 strncpy(pwsi->dbuffer, s, 32 );
1077 return pwsi->dbuffer;
1079 SetLastError(wsaErrno());
1084 SEGPTR WINAPI WINSOCK_inet_ntoa16(struct in_addr in)
1086 char* retVal = WINSOCK_inet_ntoa(in);
1087 return retVal ? SEGPTR_GET(retVal) : (SEGPTR)NULL;
1090 /***********************************************************************
1091 * ioctlsocket() (WSOCK32.12)
1093 INT WINAPI WINSOCK_ioctlsocket(SOCKET s, UINT cmd, UINT *argp)
1095 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1096 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1098 TRACE(winsock, "(%08x): socket %04x, cmd %08x, ptr %8x\n",
1099 (unsigned)pwsi, s, cmd, (unsigned) argp);
1100 if( _check_ws(pwsi, pws) )
1112 if( pws->psop && *argp == 0 )
1114 /* AsyncSelect()'ed sockets are always nonblocking */
1115 SetLastError(WSAEINVAL);
1116 return SOCKET_ERROR;
1124 case WS_IOW('f',125,u_long):
1125 WARN(winsock,"Warning: WS1.1 shouldn't be using async I/O\n");
1126 SetLastError(WSAEINVAL);
1127 return SOCKET_ERROR;
1130 /* Netscape tries hard to use bogus ioctl 0x667e */
1131 WARN(winsock, "\tunknown WS_IOCTL cmd (%08x)\n", cmd);
1133 if( ioctl(pws->fd, newcmd, (char*)argp ) == 0 ) return 0;
1134 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1136 return SOCKET_ERROR;
1139 /***********************************************************************
1140 * ioctlsocket() (WINSOCK.12)
1142 INT16 WINAPI WINSOCK_ioctlsocket16(SOCKET16 s, UINT cmd, UINT *argp)
1144 return (INT16)WINSOCK_ioctlsocket( s, cmd, argp );
1148 /***********************************************************************
1149 * listen() (WSOCK32.13)
1151 INT WINAPI WINSOCK_listen(SOCKET s, INT backlog)
1153 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1154 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1156 TRACE(winsock, "(%08x): socket %04x, backlog %d\n",
1157 (unsigned)pwsi, s, backlog);
1158 if( _check_ws(pwsi, pws) )
1160 if (listen(pws->fd, backlog) == 0)
1164 int fd_flags = fcntl(pws->fd, F_GETFL, 0);
1165 if( !(fd_flags & O_NONBLOCK) ) pws->flags |= WS_FD_ACCEPT;
1167 pws->flags |= WS_FD_LISTENING;
1168 pws->flags &= ~(WS_FD_INACTIVE | WS_FD_CONNECT | WS_FD_CONNECTED); /* just in case */
1171 SetLastError(wsaErrno());
1173 else SetLastError(WSAENOTSOCK);
1174 return SOCKET_ERROR;
1177 /***********************************************************************
1178 * listen() (WINSOCK.13)
1180 INT16 WINAPI WINSOCK_listen16(SOCKET16 s, INT16 backlog)
1182 return (INT16)WINSOCK_listen( s, backlog );
1186 /***********************************************************************
1187 * recv() (WSOCK32.16)
1189 INT WINAPI WINSOCK_recv(SOCKET s, char *buf, INT len, INT flags)
1191 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1192 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1194 TRACE(winsock, "(%08x): socket %04x, buf %8x, len %d, "
1195 "flags %d\n", (unsigned)pwsi, s, (unsigned)buf,
1197 if( _check_ws(pwsi, pws) )
1200 if ((length = recv(pws->fd, buf, len, flags)) >= 0)
1202 TRACE(winsock, " -> %i bytes\n", length);
1204 if( pws->psop && (pws->flags & (WS_FD_READ | WS_FD_CLOSE)) )
1205 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
1209 SetLastError(wsaErrno());
1211 else SetLastError(WSAENOTSOCK);
1212 WARN(winsock, " -> ERROR\n");
1213 return SOCKET_ERROR;
1216 /***********************************************************************
1217 * recv() (WINSOCK.16)
1219 INT16 WINAPI WINSOCK_recv16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1221 return (INT16)WINSOCK_recv( s, buf, len, flags );
1225 /***********************************************************************
1226 * recvfrom() (WSOCK32.17)
1228 INT WINAPI WINSOCK_recvfrom(SOCKET s, char *buf, INT len, INT flags,
1229 struct sockaddr *from, INT *fromlen32)
1231 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1232 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1234 struct ws_sockaddr_ipx* from2 = (struct ws_sockaddr_ipx *)from;
1237 TRACE(winsock, "(%08x): socket %04x, ptr %08x, "
1238 "len %d, flags %d\n", (unsigned)pwsi, s, (unsigned)buf,
1241 if( from ) dump_sockaddr(from);
1242 else DUMP("from = NULL\n");
1245 if( _check_ws(pwsi, pws) )
1249 if ((length = recvfrom(pws->fd, buf, len, flags, from, fromlen32)) >= 0)
1251 TRACE(winsock, " -> %i bytes\n", length);
1253 if( pws->psop && (pws->flags & (WS_FD_READ | WS_FD_CLOSE)) )
1254 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
1257 if (from && ((struct sockaddr_ipx *)from)->sipx_family == AF_IPX) {
1258 from = (struct sockaddr *)
1259 malloc(fromlen32 ? *fromlen32 : sizeof(*from2));
1260 memcpy(from, from2, fromlen32 ? *fromlen32 : sizeof(*from2));
1261 from2->sipx_family = WS_AF_IPX;
1262 from2->sipx_network = ((struct sockaddr_ipx *)from)->sipx_network;
1263 from2->sipx_port = ((struct sockaddr_ipx *)from)->sipx_port;
1264 memcpy(from2->sipx_node,
1265 ((struct sockaddr_ipx *)from)->sipx_node, IPX_NODE_LEN);
1269 return (INT16)length;
1271 SetLastError(wsaErrno());
1273 else SetLastError(WSAENOTSOCK);
1274 WARN(winsock, " -> ERROR\n");
1276 if (from && ((struct sockaddr_ipx *)from)->sipx_family == AF_IPX) {
1277 from = (struct sockaddr *)
1278 malloc(fromlen32 ? *fromlen32 : sizeof(*from2));
1279 memcpy(from, from2, fromlen32 ? *fromlen32 : sizeof(*from2));
1280 from2->sipx_family = WS_AF_IPX;
1281 from2->sipx_network = ((struct sockaddr_ipx *)from)->sipx_network;
1282 from2->sipx_port = ((struct sockaddr_ipx *)from)->sipx_port;
1283 memcpy(from2->sipx_node,
1284 ((struct sockaddr_ipx *)from)->sipx_node, IPX_NODE_LEN);
1288 return SOCKET_ERROR;
1291 /***********************************************************************
1292 * recvfrom() (WINSOCK.17)
1294 INT16 WINAPI WINSOCK_recvfrom16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1295 struct sockaddr *from, INT16 *fromlen16)
1298 INT *p = &fromlen32;
1301 if( fromlen16 ) fromlen32 = *fromlen16; else p = NULL;
1302 retVal = WINSOCK_recvfrom( s, buf, len, flags, from, p );
1303 if( fromlen16 ) *fromlen16 = fromlen32;
1304 return (INT16)retVal;
1307 /***********************************************************************
1308 * select() (WINSOCK.18)(WSOCK32.18)
1310 static INT __ws_select( BOOL b32, void *ws_readfds, void *ws_writefds, void *ws_exceptfds,
1311 struct timeval *timeout )
1313 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1315 TRACE(winsock, "(%08x): read %8x, write %8x, excp %8x\n",
1316 (unsigned) pwsi, (unsigned) ws_readfds, (unsigned) ws_writefds, (unsigned) ws_exceptfds);
1321 fd_set readfds, writefds, exceptfds;
1322 fd_set *p_read, *p_write, *p_except;
1324 p_read = fd_set_import(&readfds, pwsi, ws_readfds, &highfd, b32);
1325 p_write = fd_set_import(&writefds, pwsi, ws_writefds, &highfd, b32);
1326 p_except = fd_set_import(&exceptfds, pwsi, ws_exceptfds, &highfd, b32);
1328 if( (highfd = select(highfd + 1, p_read, p_write, p_except, timeout)) > 0 )
1330 fd_set_export(pwsi, &readfds, p_except, ws_readfds, b32);
1331 fd_set_export(pwsi, &writefds, p_except, ws_writefds, b32);
1333 if (p_except && ws_exceptfds)
1335 #define wsfds16 ((ws_fd_set16*)ws_exceptfds)
1336 #define wsfds32 ((ws_fd_set32*)ws_exceptfds)
1337 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
1339 for (i = j = 0; i < count; i++)
1341 ws_socket *pws = (b32) ? (ws_socket *)WS_HANDLE2PTR(wsfds32->fd_array[i])
1342 : (ws_socket *)WS_HANDLE2PTR(wsfds16->fd_array[i]);
1343 if( _check_ws(pwsi, pws) && FD_ISSET(pws->fd, &exceptfds) )
1346 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
1348 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
1352 wsfds32->fd_count = j;
1354 wsfds16->fd_count = j;
1360 if( ws_readfds ) ((ws_fd_set32*)ws_readfds)->fd_count = 0;
1361 if( ws_writefds ) ((ws_fd_set32*)ws_writefds)->fd_count = 0;
1362 if( ws_exceptfds ) ((ws_fd_set32*)ws_exceptfds)->fd_count = 0;
1364 if( highfd == 0 ) return 0;
1365 SetLastError(wsaErrno());
1367 return SOCKET_ERROR;
1370 INT16 WINAPI WINSOCK_select16(INT16 nfds, ws_fd_set16 *ws_readfds,
1371 ws_fd_set16 *ws_writefds, ws_fd_set16 *ws_exceptfds,
1372 struct timeval *timeout)
1374 return (INT16)__ws_select( FALSE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1377 INT WINAPI WINSOCK_select(INT nfds, ws_fd_set32 *ws_readfds,
1378 ws_fd_set32 *ws_writefds, ws_fd_set32 *ws_exceptfds,
1379 struct timeval *timeout)
1381 /* struct timeval is the same for both 32- and 16-bit code */
1382 return (INT)__ws_select( TRUE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1386 /***********************************************************************
1387 * send() (WSOCK32.19)
1389 INT WINAPI WINSOCK_send(SOCKET s, char *buf, INT len, INT flags)
1391 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1392 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1394 TRACE(winsock, "(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
1395 (unsigned)pwsi, s, (unsigned) buf, len, flags);
1396 if( _check_ws(pwsi, pws) )
1400 if ((length = send(pws->fd, buf, len, flags)) < 0 )
1402 SetLastError(wsaErrno());
1403 if( GetLastError() == WSAEWOULDBLOCK &&
1404 pws->psop && pws->flags & WS_FD_WRITE )
1405 EVENT_AddIO( pws->fd, EVENT_IO_WRITE ); /* reenabler */
1407 else return (INT16)length;
1409 else SetLastError(WSAENOTSOCK);
1410 return SOCKET_ERROR;
1413 /***********************************************************************
1414 * send() (WINSOCK.19)
1416 INT16 WINAPI WINSOCK_send16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1418 return WINSOCK_send( s, buf, len, flags );
1421 /***********************************************************************
1422 * sendto() (WSOCK32.20)
1424 INT WINAPI WINSOCK_sendto(SOCKET s, char *buf, INT len, INT flags,
1425 struct sockaddr *to, INT tolen)
1427 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1428 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1430 struct ws_sockaddr_ipx* to2 = (struct ws_sockaddr_ipx *)to;
1433 TRACE(winsock, "(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
1434 (unsigned)pwsi, s, (unsigned) buf, len, flags);
1435 if( _check_ws(pwsi, pws) )
1439 if (to && ((struct ws_sockaddr_ipx *)to)->sipx_family == WS_AF_PUP)
1440 ((struct ws_sockaddr_ipx *)to)->sipx_family = AF_UNSPEC;
1443 ((struct ws_sockaddr_ipx *)to)->sipx_family == WS_AF_IPX)
1445 to = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
1446 memset(to, '\0', sizeof(struct sockaddr_ipx));
1447 ((struct sockaddr_ipx *)to)->sipx_family = AF_IPX;
1448 ((struct sockaddr_ipx *)to)->sipx_port = to2->sipx_port;
1449 ((struct sockaddr_ipx *)to)->sipx_network = to2->sipx_network;
1450 memcpy(((struct sockaddr_ipx *)to)->sipx_node,
1451 to2->sipx_node, IPX_NODE_LEN);
1452 tolen = sizeof(struct sockaddr_ipx);
1455 if ((length = sendto(pws->fd, buf, len, flags, to, tolen)) < 0 )
1457 SetLastError(wsaErrno());
1458 if( GetLastError() == WSAEWOULDBLOCK &&
1459 pws->psop && pws->flags & WS_FD_WRITE )
1460 EVENT_AddIO( pws->fd, EVENT_IO_WRITE ); /* reenabler */
1464 if (to && ((struct sockaddr_ipx *)to)->sipx_family == AF_IPX) {
1471 else SetLastError(WSAENOTSOCK);
1473 if (to && ((struct sockaddr_ipx *)to)->sipx_family == AF_IPX) {
1477 return SOCKET_ERROR;
1480 /***********************************************************************
1481 * sendto() (WINSOCK.20)
1483 INT16 WINAPI WINSOCK_sendto16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1484 struct sockaddr *to, INT16 tolen)
1486 return (INT16)WINSOCK_sendto( s, buf, len, flags, to, tolen );
1489 /***********************************************************************
1490 * setsockopt() (WSOCK32.21)
1492 INT WINAPI WINSOCK_setsockopt(SOCKET16 s, INT level, INT optname,
1493 char *optval, INT optlen)
1495 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1496 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1498 TRACE(winsock, "(%08x): socket %04x, lev %d, opt %d, ptr %08x, len %d\n",
1499 (unsigned)pwsi, s, level, optname, (int) optval, optlen);
1500 if( _check_ws(pwsi, pws) )
1502 struct linger linger;
1504 convert_sockopt(&level, &optname);
1505 if (optname == SO_LINGER && optval) {
1506 /* yes, uses unsigned short in both win16/win32 */
1507 linger.l_onoff = ((UINT16*)optval)[0];
1508 linger.l_linger = ((UINT16*)optval)[1];
1509 /* FIXME: what is documented behavior if SO_LINGER optval
1511 optval = (char*)&linger;
1512 optlen = sizeof(struct linger);
1514 if (setsockopt(pws->fd, level, optname, optval, optlen) == 0) return 0;
1515 SetLastError(wsaErrno());
1517 else SetLastError(WSAENOTSOCK);
1518 return SOCKET_ERROR;
1521 /***********************************************************************
1522 * setsockopt() (WINSOCK.21)
1524 INT16 WINAPI WINSOCK_setsockopt16(SOCKET16 s, INT16 level, INT16 optname,
1525 char *optval, INT16 optlen)
1527 if( !optval ) return SOCKET_ERROR;
1528 return (INT16)WINSOCK_setsockopt( s, (UINT16)level, optname, optval, optlen );
1532 /***********************************************************************
1533 * shutdown() (WSOCK32.22)
1535 INT WINAPI WINSOCK_shutdown(SOCKET s, INT how)
1537 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1538 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1540 TRACE(winsock, "(%08x): socket %04x, how %i\n",
1541 (unsigned)pwsi, s, how );
1542 if( _check_ws(pwsi, pws) )
1547 case 0: /* drop receives */
1548 if( pws->flags & (WS_FD_READ | WS_FD_CLOSE) )
1549 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
1550 pws->flags &= ~(WS_FD_READ | WS_FD_CLOSE);
1556 case 1: /* drop sends */
1557 if( pws->flags & WS_FD_WRITE )
1558 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
1559 pws->flags &= ~WS_FD_WRITE;
1565 case 2: /* drop all */
1570 WSAAsyncSelect( s, 0, 0, 0 );
1574 if (shutdown(pws->fd, how) == 0)
1578 pws->flags &= ~(WS_FD_CONNECTED | WS_FD_LISTENING);
1579 pws->flags |= WS_FD_INACTIVE;
1583 SetLastError(wsaErrno());
1585 else SetLastError(WSAENOTSOCK);
1586 return SOCKET_ERROR;
1589 /***********************************************************************
1590 * shutdown() (WINSOCK.22)
1592 INT16 WINAPI WINSOCK_shutdown16(SOCKET16 s, INT16 how)
1594 return (INT16)WINSOCK_shutdown( s, how );
1598 /***********************************************************************
1599 * socket() (WSOCK32.23)
1601 SOCKET WINAPI WINSOCK_socket(INT af, INT type, INT protocol)
1603 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1605 TRACE(winsock, "(%08x): af=%d type=%d protocol=%d\n",
1606 (unsigned)pwsi, af, type, protocol);
1612 /* check the socket family */
1616 case WS_AF_IPX: af = AF_IPX;
1619 case AF_UNSPEC: break;
1620 default: SetLastError(WSAEAFNOSUPPORT);
1621 return INVALID_SOCKET;
1624 /* check the socket type */
1629 case SOCK_RAW: break;
1630 default: SetLastError(WSAESOCKTNOSUPPORT);
1631 return INVALID_SOCKET;
1634 /* check the protocol type */
1635 if ( protocol < 0 ) /* don't support negative values */
1636 { SetLastError(WSAEPROTONOSUPPORT); return INVALID_SOCKET; }
1638 if ( af == AF_UNSPEC) /* did they not specify the address family? */
1642 if (type == SOCK_STREAM) { af = AF_INET; break; }
1644 if (type == SOCK_DGRAM) { af = AF_INET; break; }
1645 default: SetLastError(WSAEPROTOTYPE); return INVALID_SOCKET;
1648 if ((sock = socket(af, type, protocol)) >= 0)
1650 ws_socket* pnew = wsi_alloc_socket(pwsi, sock);
1652 TRACE(winsock,"\tcreated %i (handle %04x)\n", sock, (UINT16)WS_PTR2HANDLE(pnew));
1656 pnew->flags |= WS_FD_INACTIVE;
1657 return (SOCKET16)WS_PTR2HANDLE(pnew);
1661 SetLastError(WSAENOBUFS);
1662 return INVALID_SOCKET;
1665 if (errno == EPERM) /* raw socket denied */
1667 if (type == SOCK_RAW)
1668 MSG("WARNING: Trying to create a socket of type SOCK_RAW, will fail unless running as root\n");
1670 MSG("WS_SOCKET: not enough privileges to create socket, try running as root\n");
1671 SetLastError(WSAESOCKTNOSUPPORT);
1672 } else SetLastError(wsaErrno());
1675 WARN(winsock, "\t\tfailed!\n");
1676 return INVALID_SOCKET;
1679 /***********************************************************************
1680 * socket() (WINSOCK.23)
1682 SOCKET16 WINAPI WINSOCK_socket16(INT16 af, INT16 type, INT16 protocol)
1684 return (SOCKET16)WINSOCK_socket( af, type, protocol );
1688 /* ----------------------------------- DNS services
1690 * IMPORTANT: 16-bit API structures have SEGPTR pointers inside them.
1691 * Also, we have to use wsock32 stubs to convert structures and
1692 * error codes from Unix to WSA, hence there is no direct mapping in
1693 * the relay32/wsock32.spec.
1696 static char* NULL_STRING = "NULL";
1698 /***********************************************************************
1699 * gethostbyaddr() (WINSOCK.51)(WSOCK32.51)
1701 static struct WIN_hostent* __ws_gethostbyaddr(const char *addr, int len, int type, int dup_flag)
1703 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1707 struct hostent* host;
1708 if( (host = gethostbyaddr(addr, len, type)) != NULL )
1709 if( WS_dup_he(pwsi, host, dup_flag) )
1710 return (struct WIN_hostent*)(pwsi->he);
1712 SetLastError(WSAENOBUFS);
1714 SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno());
1719 SEGPTR WINAPI WINSOCK_gethostbyaddr16(const char *addr, INT16 len, INT16 type)
1721 struct WIN_hostent* retval;
1722 TRACE(winsock, "ptr %08x, len %d, type %d\n",
1723 (unsigned) addr, len, type);
1724 retval = __ws_gethostbyaddr( addr, len, type, WS_DUP_SEGPTR );
1725 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1728 struct WIN_hostent* WINAPI WINSOCK_gethostbyaddr(const char *addr, INT len,
1731 TRACE(winsock, "ptr %08x, len %d, type %d\n",
1732 (unsigned) addr, len, type);
1733 return __ws_gethostbyaddr(addr, len, type, WS_DUP_LINEAR);
1736 /***********************************************************************
1737 * gethostbyname() (WINSOCK.52)(WSOCK32.52)
1739 static struct WIN_hostent * __ws_gethostbyname(const char *name, int dup_flag)
1741 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1745 struct hostent* host;
1746 if( (host = gethostbyname(name)) != NULL )
1747 if( WS_dup_he(pwsi, host, dup_flag) )
1748 return (struct WIN_hostent*)(pwsi->he);
1749 else SetLastError(WSAENOBUFS);
1750 else SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno());
1755 SEGPTR WINAPI WINSOCK_gethostbyname16(const char *name)
1757 struct WIN_hostent* retval;
1758 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1759 retval = __ws_gethostbyname( name, WS_DUP_SEGPTR );
1760 return (retval)? SEGPTR_GET(retval) : ((SEGPTR)NULL) ;
1763 struct WIN_hostent* WINAPI WINSOCK_gethostbyname(const char* name)
1765 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1766 return __ws_gethostbyname( name, WS_DUP_LINEAR );
1770 /***********************************************************************
1771 * getprotobyname() (WINSOCK.53)(WSOCK32.53)
1773 static struct WIN_protoent* __ws_getprotobyname(const char *name, int dup_flag)
1775 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1779 struct protoent* proto;
1780 if( (proto = getprotobyname(name)) != NULL )
1781 if( WS_dup_pe(pwsi, proto, dup_flag) )
1782 return (struct WIN_protoent*)(pwsi->pe);
1783 else SetLastError(WSAENOBUFS);
1784 else SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno());
1789 SEGPTR WINAPI WINSOCK_getprotobyname16(const char *name)
1791 struct WIN_protoent* retval;
1792 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1793 retval = __ws_getprotobyname(name, WS_DUP_SEGPTR);
1794 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1797 struct WIN_protoent* WINAPI WINSOCK_getprotobyname(const char* name)
1799 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1800 return __ws_getprotobyname(name, WS_DUP_LINEAR);
1804 /***********************************************************************
1805 * getprotobynumber() (WINSOCK.54)(WSOCK32.54)
1807 static struct WIN_protoent* __ws_getprotobynumber(int number, int dup_flag)
1809 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1813 struct protoent* proto;
1814 if( (proto = getprotobynumber(number)) != NULL )
1815 if( WS_dup_pe(pwsi, proto, dup_flag) )
1816 return (struct WIN_protoent*)(pwsi->pe);
1817 else SetLastError(WSAENOBUFS);
1818 else SetLastError(WSANO_DATA);
1823 SEGPTR WINAPI WINSOCK_getprotobynumber16(INT16 number)
1825 struct WIN_protoent* retval;
1826 TRACE(winsock, "%i\n", number);
1827 retval = __ws_getprotobynumber(number, WS_DUP_SEGPTR);
1828 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1831 struct WIN_protoent* WINAPI WINSOCK_getprotobynumber(INT number)
1833 TRACE(winsock, "%i\n", number);
1834 return __ws_getprotobynumber(number, WS_DUP_LINEAR);
1838 /***********************************************************************
1839 * getservbyname() (WINSOCK.55)(WSOCK32.55)
1841 struct WIN_servent* __ws_getservbyname(const char *name, const char *proto, int dup_flag)
1843 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1847 struct servent* serv;
1848 int i = wsi_strtolo( pwsi, name, proto );
1851 if( (serv = getservbyname(pwsi->buffer, pwsi->buffer + i)) != NULL )
1852 if( WS_dup_se(pwsi, serv, dup_flag) )
1853 return (struct WIN_servent*)(pwsi->se);
1854 else SetLastError(WSAENOBUFS);
1855 else SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno());
1856 else SetLastError(WSAENOBUFS);
1861 SEGPTR WINAPI WINSOCK_getservbyname16(const char *name, const char *proto)
1863 struct WIN_servent* retval;
1864 TRACE(winsock, "'%s', '%s'\n",
1865 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
1866 retval = __ws_getservbyname(name, proto, WS_DUP_SEGPTR);
1867 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1870 struct WIN_servent* WINAPI WINSOCK_getservbyname(const char *name, const char *proto)
1872 TRACE(winsock, "'%s', '%s'\n",
1873 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
1874 return __ws_getservbyname(name, proto, WS_DUP_LINEAR);
1878 /***********************************************************************
1879 * getservbyport() (WINSOCK.56)(WSOCK32.56)
1881 static struct WIN_servent* __ws_getservbyport(int port, const char* proto, int dup_flag)
1883 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1887 struct servent* serv;
1888 int i = wsi_strtolo( pwsi, proto, NULL );
1891 if( (serv = getservbyport(port, pwsi->buffer)) != NULL )
1892 if( WS_dup_se(pwsi, serv, dup_flag) )
1893 return (struct WIN_servent*)(pwsi->se);
1894 else SetLastError(WSAENOBUFS);
1895 else SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno());
1896 else SetLastError(WSAENOBUFS);
1901 SEGPTR WINAPI WINSOCK_getservbyport16(INT16 port, const char *proto)
1903 struct WIN_servent* retval;
1904 TRACE(winsock, "%i, '%s'\n",
1905 (int)port, (proto)?proto:NULL_STRING);
1906 retval = __ws_getservbyport(port, proto, WS_DUP_SEGPTR);
1907 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1910 struct WIN_servent* WINAPI WINSOCK_getservbyport(INT port, const char *proto)
1912 TRACE(winsock, "%i, '%s'\n",
1913 (int)port, (proto)?proto:NULL_STRING);
1914 return __ws_getservbyport(port, proto, WS_DUP_LINEAR);
1918 /***********************************************************************
1919 * gethostname() (WSOCK32.57)
1921 INT WINAPI WINSOCK_gethostname(char *name, INT namelen)
1923 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1925 TRACE(winsock, "(%08x): name %s, len %d\n",
1926 (unsigned)pwsi, (name)?name:NULL_STRING, namelen);
1929 if (gethostname(name, namelen) == 0) return 0;
1930 SetLastError((errno == EINVAL) ? WSAEFAULT : wsaErrno());
1932 return SOCKET_ERROR;
1935 /***********************************************************************
1936 * gethostname() (WINSOCK.57)
1938 INT16 WINAPI WINSOCK_gethostname16(char *name, INT16 namelen)
1940 return (INT16)WINSOCK_gethostname(name, namelen);
1944 /* ------------------------------------- Windows sockets extensions -- *
1946 * ------------------------------------------------------------------- */
1947 /***********************************************************************
1948 * WSAAsyncSelect() (WINSOCK.101)(WSOCK32.101)
1951 static ws_select_op* __ws_select_list = NULL;
1953 BOOL WINSOCK_HandleIO( int* max_fd, int num_pending,
1954 fd_set pending_set[3], fd_set event_set[3] )
1956 /* This function is called by the event dispatcher
1957 * with the pending_set[] containing the result of select() and
1958 * the event_set[] containing all fd that are being watched */
1960 ws_select_op* psop = __ws_select_list;
1962 DWORD dwEvent, dwErrBytes;
1965 TRACE(winsock,"%i pending descriptors\n", num_pending );
1967 for( num_posted = dwEvent = 0 ; psop; psop = psop->next )
1969 unsigned flags = psop->pws->flags;
1970 int fd = psop->pws->fd;
1974 if( (r = FD_ISSET( fd, &pending_set[EVENT_IO_READ] )) ||
1975 (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] )) ||
1976 (e = FD_ISSET( fd, &pending_set[EVENT_IO_EXCEPT] )) )
1978 /* This code removes WS_FD flags on one-shot events (WS_FD_CLOSE,
1979 * WS_FD_CONNECT), otherwise it clears descriptors in the io_set.
1980 * Reenabling calls turn them back on.
1983 TRACE(winsock,"\tchecking psop = 0x%08x\n", (unsigned) psop );
1987 /* Now figure out what kind of event we've got. The worst problem
1988 * we have to contend with is that some out of control applications
1989 * really want to use mutually exclusive AsyncSelect() flags all at
1993 if((flags & WS_FD_ACCEPT) && (flags & WS_FD_LISTENING))
1995 /* WS_FD_ACCEPT is valid only if the socket is in the
1996 * listening state */
1998 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2001 FD_CLR( fd, &event_set[EVENT_IO_READ] ); /* reenabled by the next accept() */
2002 dwEvent = WSAMAKESELECTREPLY( WS_FD_ACCEPT, 0 );
2007 else if( flags & WS_FD_CONNECT )
2009 /* connecting socket */
2011 if( w || (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] )) )
2013 /* ready to write means that socket is connected
2015 * FIXME: Netscape calls AsyncSelect( s, ... WS_FD_CONNECT .. )
2016 * right after s = socket() and somehow "s" becomes writeable
2017 * before it goes through connect()!?!?
2020 psop->pws->flags |= WS_FD_CONNECTED;
2021 psop->pws->flags &= ~(WS_FD_CONNECT | WS_FD_INACTIVE);
2022 dwEvent = WSAMAKESELECTREPLY( WS_FD_CONNECT, 0 );
2024 if( flags & (WS_FD_READ | WS_FD_CLOSE))
2025 FD_SET( fd, &event_set[EVENT_IO_READ] );
2027 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2028 if( flags & WS_FD_WRITE )
2029 FD_SET( fd, &event_set[EVENT_IO_WRITE] );
2031 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2036 /* failure - do read() to get correct errno */
2038 if( read( fd, &dwErrBytes, sizeof(dwErrBytes) ) == -1 )
2040 dwEvent = WSAMAKESELECTREPLY( WS_FD_CONNECT, wsaErrno() );
2044 /* otherwise bPost stays FALSE, should probably clear event_set */
2048 /* connected socket, no WS_FD_OOB code for now. */
2050 if( flags & WS_FD_WRITE &&
2051 (w || (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] ))) )
2053 /* this will be reenabled when send() or sendto() fail with
2056 if( PostMessageA( psop->hWnd, psop->uMsg, (WPARAM)WS_PTR2HANDLE(psop->pws),
2057 (LPARAM)WSAMAKESELECTREPLY( WS_FD_WRITE, 0 ) ) )
2059 TRACE(winsock, "\t hwnd %04x - %04x, %08x\n",
2060 psop->hWnd, psop->uMsg, (unsigned)MAKELONG(WS_FD_WRITE, 0) );
2061 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2066 if( r && (flags & (WS_FD_READ | WS_FD_CLOSE)) )
2068 int val = (flags & WS_FD_RAW);
2070 /* WS_FD_RAW is set by the WSAAsyncSelect() init */
2073 if( !val && ioctl( fd, FIONREAD, (char*)&dwErrBytes) == -1 )
2077 dwEvent = WSAMAKESELECTREPLY( WS_FD_READ, wsaErrno() );
2079 else if( val || dwErrBytes )
2081 /* got pending data, will be reenabled by recv() or recvfrom() */
2083 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2084 dwEvent = WSAMAKESELECTREPLY( WS_FD_READ, 0 );
2088 /* 0 bytes to read - connection reset by peer? */
2091 val = read( fd, (char*)&dwErrBytes, sizeof(dwErrBytes));
2092 while( errno == EINTR );
2093 if( errno != EWOULDBLOCK )
2097 case 0: errno = ENETDOWN; /* soft reset, fall through */
2098 case -1: /* hard reset */
2099 dwEvent = WSAMAKESELECTREPLY( WS_FD_CLOSE, wsaErrno() );
2102 default: bPost = FALSE;
2103 continue; /* FIXME: this is real bad */
2106 else { bPost = FALSE; continue; } /* more weirdness */
2108 /* this is it, this socket is closed */
2110 psop->pws->flags &= ~(WS_FD_READ | WS_FD_CLOSE | WS_FD_WRITE);
2111 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2112 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2114 if( *max_fd == (fd + 1) ) (*max_fd)--;
2121 TRACE(winsock, "\t hwnd %04x - %04x, %08x\n",
2122 psop->hWnd, psop->uMsg, (unsigned)dwEvent );
2123 PostMessageA( psop->hWnd, psop->uMsg,
2124 (WPARAM)WS_PTR2HANDLE(psop->pws), (LPARAM)dwEvent );
2129 if( num_pending <= 0 ) break;
2132 TRACE(winsock, "\tdone, %i posted events\n", num_posted );
2133 return ( num_posted ) ? TRUE : FALSE;
2136 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, UINT lEvent)
2138 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
2139 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2141 TRACE(winsock, "(%08x): %04x, hWnd %04x, uMsg %08x, event %08x\n",
2142 (unsigned)pwsi, (SOCKET16)s, (HWND16)hWnd, uMsg, (unsigned)lEvent );
2143 if( _check_ws(pwsi, pws) )
2147 if( (psop = pws->psop) )
2149 /* delete previous control struct */
2151 if( psop == __ws_select_list )
2152 __ws_select_list = psop->next;
2154 psop->prev->next = psop->next;
2155 if( psop->next ) psop->next->prev = psop->prev;
2157 if( pws->flags & (WS_FD_ACCEPT | WS_FD_CONNECT | WS_FD_READ | WS_FD_CLOSE) )
2158 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
2159 if( pws->flags & (WS_FD_CONNECT | WS_FD_WRITE) )
2160 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
2162 TRACE(winsock,"\tremoving psop = 0x%08x\n", (unsigned) psop );
2164 WS_FREE( pws->psop );
2165 pws->flags &= ~(WS_FD_RAW | WS_FD_ACCEPT | WS_FD_CONNECT |
2166 WS_FD_READ | WS_FD_WRITE | WS_FD_CLOSE);
2172 psop = (ws_select_op*)WS_ALLOC(sizeof(ws_select_op));
2175 int sock_type, bytes = sizeof(int);
2177 WINSOCK_unblock_io( pws->fd, TRUE );
2180 psop->next = __ws_select_list;
2181 if( __ws_select_list )
2182 __ws_select_list->prev = psop;
2183 __ws_select_list = psop;
2190 pws->flags |= (0x0000FFFF & lEvent);
2191 getsockopt(pws->fd, SOL_SOCKET, SO_TYPE, &sock_type, &bytes);
2192 if( sock_type == SOCK_RAW ) pws->flags |= WS_FD_RAW;
2194 if( lEvent & (WS_FD_ACCEPT | WS_FD_CONNECT | WS_FD_READ | WS_FD_CLOSE) )
2195 EVENT_AddIO( pws->fd, EVENT_IO_READ );
2196 if( lEvent & (WS_FD_CONNECT | WS_FD_WRITE) )
2197 EVENT_AddIO( pws->fd, EVENT_IO_WRITE );
2199 /* TODO: handle WS_FD_ACCEPT right away if the socket is readable */
2201 TRACE(winsock,"\tcreating psop = 0x%08x\n", (unsigned)psop );
2203 return 0; /* success */
2205 else SetLastError(WSAENOBUFS);
2209 else SetLastError(WSAEINVAL);
2210 return SOCKET_ERROR;
2213 INT16 WINAPI WSAAsyncSelect16(SOCKET16 s, HWND16 hWnd, UINT16 wMsg, UINT lEvent)
2215 return (INT16)WSAAsyncSelect( s, hWnd, wMsg, lEvent );
2219 /***********************************************************************
2220 * __WSAFDIsSet() (WINSOCK.151)
2222 INT16 WINAPI __WSAFDIsSet16(SOCKET16 s, ws_fd_set16 *set)
2224 int i = set->fd_count;
2226 TRACE(winsock, "(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2229 if (set->fd_array[i] == s) return 1;
2233 /***********************************************************************
2234 * __WSAFDIsSet() (WSOCK32.151)
2236 INT WINAPI __WSAFDIsSet(SOCKET s, ws_fd_set32 *set)
2238 int i = set->fd_count;
2240 TRACE(winsock, "(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2243 if (set->fd_array[i] == s) return 1;
2247 /***********************************************************************
2248 * WSAIsBlocking() (WINSOCK.114)(WSOCK32.114)
2250 BOOL WINAPI WSAIsBlocking(void)
2252 /* By default WinSock should set all its sockets to non-blocking mode
2253 * and poll in PeekMessage loop when processing "blocking" ones. This
2254 * function is supposed to tell if the program is in this loop. Our
2255 * blocking calls are truly blocking so we always return FALSE.
2257 * Note: It is allowed to call this function without prior WSAStartup().
2260 TRACE(winsock, "\n");
2264 /***********************************************************************
2265 * WSACancelBlockingCall() (WINSOCK.113)(WSOCK32.113)
2267 INT WINAPI WSACancelBlockingCall(void)
2269 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2271 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2273 if( pwsi ) return 0;
2274 return SOCKET_ERROR;
2278 /***********************************************************************
2279 * WSASetBlockingHook16() (WINSOCK.109)
2281 FARPROC16 WINAPI WSASetBlockingHook16(FARPROC16 lpBlockFunc)
2284 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2286 TRACE(winsock, "(%08x): hook %08x\n",
2287 (unsigned)pwsi, (unsigned) lpBlockFunc);
2290 prev = (FARPROC16)pwsi->blocking_hook;
2291 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2292 pwsi->flags &= ~WSI_BLOCKINGHOOK;
2299 /***********************************************************************
2300 * WSASetBlockingHook32()
2302 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
2305 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2307 TRACE(winsock, "(%08x): hook %08x\n",
2308 (unsigned)pwsi, (unsigned) lpBlockFunc);
2310 prev = (FARPROC)pwsi->blocking_hook;
2311 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2312 pwsi->flags |= WSI_BLOCKINGHOOK;
2319 /***********************************************************************
2320 * WSAUnhookBlockingHook16() (WINSOCK.110)
2322 INT16 WINAPI WSAUnhookBlockingHook16(void)
2324 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2326 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2327 if( pwsi ) return (INT16)(pwsi->blocking_hook = 0);
2328 return SOCKET_ERROR;
2332 /***********************************************************************
2333 * WSAUnhookBlockingHook32()
2335 INT WINAPI WSAUnhookBlockingHook(void)
2337 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2339 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2342 pwsi->blocking_hook = 0;
2343 pwsi->flags &= ~WSI_BLOCKINGHOOK;
2346 return SOCKET_ERROR;
2350 * TCP/IP action codes.
2354 #define WSCNTL_TCPIP_QUERY_INFO 0x00000000
2355 #define WSCNTL_TCPIP_SET_INFO 0x00000001
2356 #define WSCNTL_TCPIP_ICMP_ECHO 0x00000002
2357 #define WSCNTL_TCPIP_TEST 0x00000003
2360 /***********************************************************************
2363 * WsControl seems to be an undocumented Win95 function. A lot of
2364 * discussion about WsControl can be found on the net, e.g.
2365 * Subject: Re: WSOCK32.DLL WsControl Exported Function
2366 * From: "Peter Rindfuss" <rindfuss-s@medea.wz-berlin.de>
2370 DWORD WINAPI WsControl(DWORD protocoll,DWORD action,
2371 LPVOID inbuf,LPDWORD inbuflen,
2372 LPVOID outbuf,LPDWORD outbuflen)
2376 case WSCNTL_TCPIP_ICMP_ECHO:
2378 unsigned int addr = *(unsigned int*)inbuf;
2380 int timeout= *(unsigned int*)(inbuf+4);
2381 short x1 = *(unsigned short*)(inbuf+8);
2382 short sendbufsize = *(unsigned short*)(inbuf+10);
2383 char x2 = *(unsigned char*)(inbuf+12);
2384 char ttl = *(unsigned char*)(inbuf+13);
2385 char service = *(unsigned char*)(inbuf+14);
2386 char type= *(unsigned char*)(inbuf+15); /* 0x2: don't fragment*/
2389 FIXME(winsock,"(ICMP_ECHO) to 0x%08x stub \n", addr);
2393 FIXME(winsock,"(%lx,%lx,%p,%p,%p,%p) stub\n",
2394 protocoll,action,inbuf,inbuflen,outbuf,outbuflen);
2398 /*********************************************************
2399 * WS_s_perror WSOCK32.1108
2401 void WINAPI WS_s_perror(LPCSTR message)
2403 FIXME(winsock,"(%s): stub\n",message);
2408 /* ----------------------------------- end of API stuff */
2410 /* ----------------------------------- helper functions -
2412 * TODO: Merge WS_dup_..() stuff into one function that
2413 * would operate with a generic structure containing internal
2414 * pointers (via a template of some kind).
2417 static int list_size(char** l, int item_size)
2422 j += (item_size) ? item_size : strlen(l[i]) + 1;
2423 j += (i + 1) * sizeof(char*); }
2427 static int list_dup(char** l_src, char* ref, char* base, int item_size)
2429 /* base is either either equal to ref or 0 or SEGPTR */
2432 char** l_to = (char**)ref;
2435 for(j=0;l_src[j];j++) ;
2436 p += (j + 1) * sizeof(char*);
2438 { l_to[i] = base + (p - ref);
2439 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
2440 memcpy(p, l_src[i], k); p += k; }
2447 static int hostent_size(struct hostent* p_he)
2451 { size = sizeof(struct hostent);
2452 size += strlen(p_he->h_name) + 1;
2453 size += list_size(p_he->h_aliases, 0);
2454 size += list_size(p_he->h_addr_list, p_he->h_length ); }
2458 int WS_dup_he(LPWSINFO pwsi, struct hostent* p_he, int flag)
2460 /* Convert hostent structure into ws_hostent so that the data fits
2461 * into pwsi->buffer. Internal pointers can be linear, SEGPTR, or
2462 * relative to pwsi->buffer depending on "flag" value. Returns size
2463 * of the data copied (also in the pwsi->buflen).
2466 int size = hostent_size(p_he);
2470 struct ws_hostent* p_to;
2471 char* p_name,*p_aliases,*p_addr,*p_base,*p;
2473 _check_buffer_he(pwsi, size);
2474 p_to = (struct ws_hostent*)pwsi->he;
2475 p = (char*)pwsi->he;
2476 p_base = (flag & WS_DUP_OFFSET) ? NULL
2477 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2478 p += sizeof(struct ws_hostent);
2480 strcpy(p, p_he->h_name); p += strlen(p) + 1;
2482 p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)pwsi->he), 0);
2484 list_dup(p_he->h_addr_list, p, p_base + (p - (char*)pwsi->he), p_he->h_length);
2486 p_to->h_addrtype = (INT16)p_he->h_addrtype;
2487 p_to->h_length = (INT16)p_he->h_length;
2488 p_to->h_name = (SEGPTR)(p_base + (p_name - (char*)pwsi->he));
2489 p_to->h_aliases = (SEGPTR)(p_base + (p_aliases - (char*)pwsi->he));
2490 p_to->h_addr_list = (SEGPTR)(p_base + (p_addr - (char*)pwsi->he));
2492 size += (sizeof(struct ws_hostent) - sizeof(struct hostent));
2497 /* ----- protoent */
2499 static int protoent_size(struct protoent* p_pe)
2503 { size = sizeof(struct protoent);
2504 size += strlen(p_pe->p_name) + 1;
2505 size += list_size(p_pe->p_aliases, 0); }
2509 int WS_dup_pe(LPWSINFO pwsi, struct protoent* p_pe, int flag)
2511 int size = protoent_size(p_pe);
2514 struct ws_protoent* p_to;
2515 char* p_name,*p_aliases,*p_base,*p;
2517 _check_buffer_pe(pwsi, size);
2518 p_to = (struct ws_protoent*)pwsi->pe;
2519 p = (char*)pwsi->pe;
2520 p_base = (flag & WS_DUP_OFFSET) ? NULL
2521 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2522 p += sizeof(struct ws_protoent);
2524 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
2526 list_dup(p_pe->p_aliases, p, p_base + (p - (char*)pwsi->pe), 0);
2528 p_to->p_proto = (INT16)p_pe->p_proto;
2529 p_to->p_name = (SEGPTR)(p_base) + (p_name - (char*)pwsi->pe);
2530 p_to->p_aliases = (SEGPTR)((p_base) + (p_aliases - (char*)pwsi->pe));
2532 size += (sizeof(struct ws_protoent) - sizeof(struct protoent));
2539 static int servent_size(struct servent* p_se)
2543 { size += sizeof(struct servent);
2544 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
2545 size += list_size(p_se->s_aliases, 0); }
2549 int WS_dup_se(LPWSINFO pwsi, struct servent* p_se, int flag)
2551 int size = servent_size(p_se);
2554 struct ws_servent* p_to;
2555 char* p_name,*p_aliases,*p_proto,*p_base,*p;
2557 _check_buffer_se(pwsi, size);
2558 p_to = (struct ws_servent*)pwsi->se;
2559 p = (char*)pwsi->se;
2560 p_base = (flag & WS_DUP_OFFSET) ? NULL
2561 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2562 p += sizeof(struct ws_servent);
2564 strcpy(p, p_se->s_name); p += strlen(p) + 1;
2566 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
2568 list_dup(p_se->s_aliases, p, p_base + (p - (char*)pwsi->se), 0);
2570 p_to->s_port = (INT16)p_se->s_port;
2571 p_to->s_name = (SEGPTR)(p_base + (p_name - (char*)pwsi->se));
2572 p_to->s_proto = (SEGPTR)(p_base + (p_proto - (char*)pwsi->se));
2573 p_to->s_aliases = (SEGPTR)(p_base + (p_aliases - (char*)pwsi->se));
2575 size += (sizeof(struct ws_servent) - sizeof(struct servent));
2580 /* ----------------------------------- error handling */
2582 UINT16 wsaErrno(void)
2584 int loc_errno = errno;
2585 #ifdef HAVE_STRERROR
2586 WARN(winsock, "errno %d, (%s).\n", loc_errno, strerror(loc_errno));
2588 WARN(winsock, "errno %d\n", loc_errno);
2593 case EINTR: return WSAEINTR;
2594 case EBADF: return WSAEBADF;
2595 case EACCES: return WSAEACCES;
2596 case EFAULT: return WSAEFAULT;
2597 case EINVAL: return WSAEINVAL;
2598 case EMFILE: return WSAEMFILE;
2599 case EWOULDBLOCK: return WSAEWOULDBLOCK;
2600 case EINPROGRESS: return WSAEINPROGRESS;
2601 case EALREADY: return WSAEALREADY;
2602 case ENOTSOCK: return WSAENOTSOCK;
2603 case EDESTADDRREQ: return WSAEDESTADDRREQ;
2604 case EMSGSIZE: return WSAEMSGSIZE;
2605 case EPROTOTYPE: return WSAEPROTOTYPE;
2606 case ENOPROTOOPT: return WSAENOPROTOOPT;
2607 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
2608 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
2609 case EOPNOTSUPP: return WSAEOPNOTSUPP;
2610 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
2611 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
2612 case EADDRINUSE: return WSAEADDRINUSE;
2613 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
2614 case ENETDOWN: return WSAENETDOWN;
2615 case ENETUNREACH: return WSAENETUNREACH;
2616 case ENETRESET: return WSAENETRESET;
2617 case ECONNABORTED: return WSAECONNABORTED;
2619 case ECONNRESET: return WSAECONNRESET;
2620 case ENOBUFS: return WSAENOBUFS;
2621 case EISCONN: return WSAEISCONN;
2622 case ENOTCONN: return WSAENOTCONN;
2623 case ESHUTDOWN: return WSAESHUTDOWN;
2624 case ETOOMANYREFS: return WSAETOOMANYREFS;
2625 case ETIMEDOUT: return WSAETIMEDOUT;
2626 case ECONNREFUSED: return WSAECONNREFUSED;
2627 case ELOOP: return WSAELOOP;
2628 case ENAMETOOLONG: return WSAENAMETOOLONG;
2629 case EHOSTDOWN: return WSAEHOSTDOWN;
2630 case EHOSTUNREACH: return WSAEHOSTUNREACH;
2631 case ENOTEMPTY: return WSAENOTEMPTY;
2633 case EPROCLIM: return WSAEPROCLIM;
2636 case EUSERS: return WSAEUSERS;
2639 case EDQUOT: return WSAEDQUOT;
2642 case ESTALE: return WSAESTALE;
2645 case EREMOTE: return WSAEREMOTE;
2648 /* just in case we ever get here and there are no problems */
2651 WARN(winsock, "Unknown errno %d!\n", loc_errno);
2652 return WSAEOPNOTSUPP;
2656 UINT16 wsaHerrno(void)
2658 int loc_errno = h_errno;
2660 WARN(winsock, "h_errno %d.\n", loc_errno);
2664 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
2665 case TRY_AGAIN: return WSATRY_AGAIN;
2666 case NO_RECOVERY: return WSANO_RECOVERY;
2667 case NO_DATA: return WSANO_DATA;
2671 WARN(winsock,"Unknown h_errno %d!\n", loc_errno);
2672 return WSAEOPNOTSUPP;