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).
17 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #ifdef HAVE_SYS_FILIO_H
21 # include <sys/filio.h>
24 #include <sys/ioccom.h>
25 #include <sys/sockio.h>
29 # include <sys/so_ioctl.h>
32 #ifdef HAVE_SYS_PARAM_H
33 # include <sys/param.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
57 #define DEBUG_SOCKADDR 0
58 #define dump_sockaddr(a) \
59 DUMP("sockaddr_in: family %d, address %s, port %d\n", \
60 ((struct sockaddr_in *)a)->sin_family, \
61 inet_ntoa(((struct sockaddr_in *)a)->sin_addr), \
62 ntohs(((struct sockaddr_in *)a)->sin_port))
66 /* ----------------------------------- internal data */
68 static HANDLE32 _WSHeap = 0;
69 static unsigned char* _ws_stub = NULL;
70 static LPWSINFO _wsi_list = NULL;
72 #define WS_ALLOC(size) \
73 HeapAlloc(_WSHeap, HEAP_ZERO_MEMORY, (size) )
74 #define WS_FREE(ptr) \
75 HeapFree(_WSHeap, 0, (ptr) )
77 #define WS_PTR2HANDLE(ptr) \
78 ((short)((int)(ptr) - (int)_ws_stub))
79 #define WS_HANDLE2PTR(handle) \
80 ((unsigned)((int)_ws_stub + (int)(handle)))
82 #define WSI_CHECK_RANGE(pwsi, pws) \
83 ( ((unsigned)(pws) > (unsigned)(pwsi)) && \
84 ((unsigned)(pws) < ((unsigned)(pwsi) + sizeof(WSINFO))) )
86 static INT32 _ws_sock_ops[] =
87 { WS_SO_DEBUG, WS_SO_REUSEADDR, WS_SO_KEEPALIVE, WS_SO_DONTROUTE,
88 WS_SO_BROADCAST, WS_SO_LINGER, WS_SO_OOBINLINE, WS_SO_SNDBUF,
89 WS_SO_RCVBUF, WS_SO_ERROR, WS_SO_TYPE, WS_SO_DONTLINGER, 0 };
90 static int _px_sock_ops[] =
91 { SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST,
92 SO_LINGER, SO_OOBINLINE, SO_SNDBUF, SO_RCVBUF, SO_ERROR, SO_TYPE,
95 static int _check_ws(LPWSINFO pwsi, ws_socket* pws);
96 static char* _check_buffer(LPWSINFO pwsi, int size);
98 /***********************************************************************
101 * Converts socket flags from Windows format.
103 static void convert_sockopt(INT32 *level, INT32 *optname)
110 for(i=0; _ws_sock_ops[i]; i++)
111 if( _ws_sock_ops[i] == *optname ) break;
112 if( _ws_sock_ops[i] ) *optname = _px_sock_ops[i];
113 else WARN(winsock, "Unknown optname %d\n", *optname);
116 *optname = IPPROTO_TCP;
120 /* ----------------------------------- Per-thread info (or per-process?) */
122 static LPWSINFO wsi_find(HTASK16 hTask)
124 TDB* pTask = (TDB*)GlobalLock16(hTask);
127 if( pTask->pwsi ) return pTask->pwsi;
130 LPWSINFO pwsi = _wsi_list;
131 while( pwsi && pwsi->tid != hTask ) pwsi = pwsi->next;
133 WARN(winsock,"(pwsi=0x%08x,task=0x%04x):Loose wsi struct! \n",
134 (unsigned)pwsi, hTask );
141 static ws_socket* wsi_alloc_socket(LPWSINFO pwsi, int fd)
143 /* Initialize a new entry in the socket table */
145 if( pwsi->last_free >= 0 )
147 int i = pwsi->last_free;
149 pwsi->last_free = pwsi->sock[i].flags; /* free list */
150 pwsi->sock[i].fd = fd;
151 pwsi->sock[i].flags = 0;
152 return &pwsi->sock[i];
157 static int wsi_strtolo(LPWSINFO pwsi, const char* name, const char* opt)
159 /* Stuff a lowercase copy of the string into the local buffer */
161 int i = strlen(name) + 2;
162 char* p = _check_buffer(pwsi, i + ((opt)?strlen(opt):0));
166 do *p++ = tolower(*name); while(*name++);
167 i = (p - (char*)(pwsi->buffer));
168 if( opt ) do *p++ = tolower(*opt); while(*opt++);
174 static fd_set* fd_set_import( fd_set* fds, LPWSINFO pwsi, void* wsfds, int* highfd, BOOL32 b32 )
176 /* translate Winsock fd set into local fd set */
180 #define wsfds16 ((ws_fd_set16*)wsfds)
181 #define wsfds32 ((ws_fd_set32*)wsfds)
186 count = b32 ? wsfds32->fd_count : wsfds16->fd_count;
188 for( i = 0; i < count; i++ )
190 pws = (b32) ? (ws_socket*)WS_HANDLE2PTR(wsfds32->fd_array[i])
191 : (ws_socket*)WS_HANDLE2PTR(wsfds16->fd_array[i]);
192 if( _check_ws(pwsi, pws) )
194 if( pws->fd > *highfd ) *highfd = pws->fd;
195 FD_SET(pws->fd, fds);
205 __inline__ static int sock_error_p(int s)
207 unsigned int optval, optlen;
209 optlen = sizeof(optval);
210 getsockopt(s, SOL_SOCKET, SO_ERROR, &optval, &optlen);
211 if (optval) WARN(winsock, "\t[%i] error: %d\n", s, optval);
215 static int fd_set_export( LPWSINFO pwsi, fd_set* fds, fd_set* exceptfds, void* wsfds, BOOL32 b32 )
219 /* translate local fd set into Winsock fd set, adding
220 * errors to exceptfds (only if app requested it) */
224 #define wsfds16 ((ws_fd_set16*)wsfds)
225 #define wsfds32 ((ws_fd_set32*)wsfds)
226 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
228 for( i = 0, j = 0; i < count; i++ )
230 ws_socket *pws = (b32) ? (ws_socket*)WS_HANDLE2PTR(wsfds32->fd_array[i])
231 : (ws_socket*)WS_HANDLE2PTR(wsfds16->fd_array[i]);
232 if( _check_ws(pwsi, pws) )
236 if( FD_ISSET(fd, fds) )
238 if ( exceptfds && sock_error_p(fd) )
240 FD_SET(fd, exceptfds);
244 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
246 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
251 if( b32 ) wsfds32->fd_count = j;
252 else wsfds16->fd_count = j;
254 TRACE(winsock, "\n");
261 HANDLE16 __ws_gethandle( void* ptr )
263 return (HANDLE16)WS_PTR2HANDLE(ptr);
266 void* __ws_memalloc( int size )
268 return WS_ALLOC(size);
271 void __ws_memfree(void* ptr)
276 /* ----------------------------------- API -----
278 * Init / cleanup / error checking.
281 /***********************************************************************
282 * WSAStartup16() (WINSOCK.115)
284 * Create socket control struct, attach it to the global list and
285 * update a pointer in the task struct.
287 INT16 WINAPI WSAStartup16(UINT16 wVersionRequested, LPWSADATA lpWSAData)
289 WSADATA WINSOCK_data = { 0x0101, 0x0101,
293 #elif defined(__NetBSD__)
297 #elif defined(__FreeBSD__)
299 #elif defined(__OpenBSD__)
304 WS_MAX_SOCKETS_PER_PROCESS,
305 WS_MAX_UDP_DATAGRAM, (SEGPTR)NULL };
306 HTASK16 tid = GetCurrentTask();
309 TRACE(winsock, "verReq=%x\n", wVersionRequested);
311 if (LOBYTE(wVersionRequested) < 1 || (LOBYTE(wVersionRequested) == 1 &&
312 HIBYTE(wVersionRequested) < 1)) return WSAVERNOTSUPPORTED;
314 if (!lpWSAData) return WSAEINVAL;
316 /* initialize socket heap */
320 _WSHeap = HeapCreate(HEAP_ZERO_MEMORY, 8120, 32768);
321 if( !(_ws_stub = WS_ALLOC(0x10)) )
323 ERR(winsock,"Fatal: failed to create WinSock heap\n");
327 if( _WSHeap == 0 ) return WSASYSNOTREADY;
329 /* create socket array for this task */
331 pwsi = wsi_find(GetCurrentTask());
334 TDB* pTask = (TDB*)GlobalLock16( tid );
336 if( (pwsi = (LPWSINFO)WS_ALLOC( sizeof(WSINFO))) )
340 for( i = 0; i < WS_MAX_SOCKETS_PER_PROCESS; i++ )
342 pwsi->sock[i].fd = -1;
343 pwsi->sock[i].flags = i + 1;
345 pwsi->sock[WS_MAX_SOCKETS_PER_PROCESS - 1].flags = -1;
347 else return WSASYSNOTREADY;
349 /* add this control struct to the global list */
353 _wsi_list->prev = pwsi;
354 pwsi->next = _wsi_list;
358 else pwsi->num_startup++;
360 /* return winsock information */
362 memcpy(lpWSAData, &WINSOCK_data, sizeof(WINSOCK_data));
364 TRACE(winsock, "succeeded\n");
368 /***********************************************************************
369 * WSAStartup32() (WSOCK32.115)
371 INT32 WINAPI WSAStartup32(UINT32 wVersionRequested, LPWSADATA lpWSAData)
373 return WSAStartup16( wVersionRequested, lpWSAData );
376 /***********************************************************************
377 * WSACleanup() (WINSOCK.116)
379 * Cleanup functions of varying impact.
381 void WINSOCK_Shutdown()
383 /* Called on exit(), has to remove all outstanding async DNS processes. */
385 WINSOCK_cancel_task_aops( 0, __ws_memfree );
388 INT32 WINSOCK_DeleteTaskWSI( TDB* pTask, LPWSINFO pwsi )
390 /* WSACleanup() backend, called on task termination as well.
391 * Real DLL would have registered its own signal handler with
392 * TaskSetSignalHandler() and waited until USIG_TERMINATION/USIG_GPF
393 * but this scheme is much more straightforward.
398 if( --pwsi->num_startup > 0 ) return 0;
400 SIGNAL_MaskAsyncEvents( TRUE );
401 WINSOCK_cancel_task_aops( pTask->hSelf, __ws_memfree );
402 SIGNAL_MaskAsyncEvents( FALSE );
404 /* unlink socket control struct */
406 if( pwsi == _wsi_list )
407 _wsi_list = pwsi->next;
409 pwsi->prev->next = pwsi->next;
410 if( pwsi->next ) pwsi->next->prev = pwsi->prev;
412 if( _wsi_list == NULL )
413 WINSOCK_Shutdown(); /* just in case */
415 if( pwsi->flags & WSI_BLOCKINGCALL )
416 TRACE(winsock,"\tinside blocking call!\n");
418 /* FIXME: aop_control() doesn't decrement pwsi->num_async_rq
420 * if( pwsi->num_async_rq )
421 * WARN(winsock,"\thave %i outstanding async ops!\n", pwsi->num_async_rq );
424 for(i = 0, j = 0, n = 0; i < WS_MAX_SOCKETS_PER_PROCESS; i++)
425 if( pwsi->sock[i].fd != -1 )
427 if( pwsi->sock[i].psop )
430 WSAAsyncSelect32( (SOCKET16)WS_PTR2HANDLE(pwsi->sock + i), 0, 0, 0 );
432 close(pwsi->sock[i].fd); j++;
435 TRACE(winsock,"\tclosed %i sockets, killed %i async selects!\n", j, n);
437 /* delete scratch buffers */
439 if( pwsi->buffer ) SEGPTR_FREE(pwsi->buffer);
440 if( pwsi->dbuffer ) SEGPTR_FREE(pwsi->dbuffer);
444 memset( pwsi, 0, sizeof(WSINFO) );
449 INT32 WINAPI WSACleanup(void)
451 HTASK16 hTask = GetCurrentTask();
453 TRACE(winsock, "(%04x)\n", hTask );
456 LPWSINFO pwsi = wsi_find(hTask);
458 return WINSOCK_DeleteTaskWSI( (TDB*)GlobalLock16(hTask), pwsi );
462 WINSOCK_Shutdown(); /* remove all outstanding DNS requests */
467 /***********************************************************************
468 * WSAGetLastError() (WSOCK32.111)(WINSOCK.111)
470 INT32 WINAPI WSAGetLastError(void)
472 LPWSINFO pwsi = wsi_find(GetCurrentTask());
473 INT16 ret = (pwsi) ? pwsi->err : WSANOTINITIALISED;
475 TRACE(winsock, "(%08x) = %i\n",
476 (unsigned)pwsi, (int)ret);
480 /***********************************************************************
481 * WSASetLastError32() (WSOCK32.112)
483 void WINAPI WSASetLastError32(INT32 iError)
485 LPWSINFO pwsi = wsi_find(GetCurrentTask());
487 TRACE(winsock, "(%08x): %d\n", (unsigned)pwsi, (int)iError);
488 if( pwsi ) pwsi->err = iError;
491 /***********************************************************************
492 * WSASetLastError16() (WINSOCK.112)
494 void WINAPI WSASetLastError16(INT16 iError)
496 WSASetLastError32(iError);
499 int _check_ws(LPWSINFO pwsi, ws_socket* pws)
502 if( pwsi->flags & WSI_BLOCKINGCALL ) pwsi->err = WSAEINPROGRESS;
503 else if( WSI_CHECK_RANGE(pwsi, pws) ) return 1;
504 else pwsi->err = WSAENOTSOCK;
508 char* _check_buffer(LPWSINFO pwsi, int size)
510 if( pwsi->buffer && pwsi->buflen >= size ) return pwsi->buffer;
511 else SEGPTR_FREE(pwsi->buffer);
513 pwsi->buffer = (char*)SEGPTR_ALLOC((pwsi->buflen = size));
517 struct ws_hostent* _check_buffer_he(LPWSINFO pwsi, int size)
519 if( pwsi->he && pwsi->helen >= size ) return pwsi->he;
520 else SEGPTR_FREE(pwsi->he);
522 pwsi->he = (struct ws_hostent*)SEGPTR_ALLOC((pwsi->helen = size));
526 struct ws_servent* _check_buffer_se(LPWSINFO pwsi, int size)
528 if( pwsi->se && pwsi->selen >= size ) return pwsi->se;
529 else SEGPTR_FREE(pwsi->se);
531 pwsi->se = (struct ws_servent*)SEGPTR_ALLOC((pwsi->selen = size));
535 struct ws_protoent* _check_buffer_pe(LPWSINFO pwsi, int size)
537 if( pwsi->pe && pwsi->pelen >= size ) return pwsi->pe;
538 else SEGPTR_FREE(pwsi->pe);
540 pwsi->pe = (struct ws_protoent*)SEGPTR_ALLOC((pwsi->pelen = size));
544 /* ----------------------------------- i/o APIs */
546 /***********************************************************************
547 * accept() (WSOCK32.1)
549 SOCKET32 WINAPI WINSOCK_accept32(SOCKET32 s, struct sockaddr *addr,
552 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR((SOCKET16)s);
553 LPWSINFO pwsi = wsi_find(GetCurrentTask());
555 TRACE(winsock, "(%08x): socket %04x\n",
556 (unsigned)pwsi, (UINT16)s );
557 if( _check_ws(pwsi, pws) )
561 fd_flags = fcntl(pws->fd, F_GETFL, 0);
563 if( (sock = accept(pws->fd, addr, addrlen32)) >= 0 )
565 ws_socket* pnew = wsi_alloc_socket(pwsi, sock);
568 s = (SOCKET32)WS_PTR2HANDLE(pnew);
569 if( pws->psop && pws->flags & WS_FD_ACCEPT )
571 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
573 /* async select the accept()'ed socket */
574 WSAAsyncSelect32( s, pws->psop->hWnd, pws->psop->uMsg,
575 pws->flags & ~WS_FD_ACCEPT );
579 else pwsi->err = WSAENOBUFS;
581 else pwsi->err = wsaErrno();
583 return INVALID_SOCKET32;
586 /***********************************************************************
587 * accept() (WINSOCK.1)
589 SOCKET16 WINAPI WINSOCK_accept16(SOCKET16 s, struct sockaddr* addr,
592 INT32 addrlen32 = addrlen16 ? *addrlen16 : 0;
593 SOCKET32 retSocket = WINSOCK_accept32( s, addr, &addrlen32 );
594 if( addrlen16 ) *addrlen16 = (INT16)addrlen32;
595 return (SOCKET16)retSocket;
598 /***********************************************************************
601 INT32 WINAPI WINSOCK_bind32(SOCKET32 s, struct sockaddr *name, INT32 namelen)
603 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
604 LPWSINFO pwsi = wsi_find(GetCurrentTask());
606 TRACE(winsock, "(%08x): socket %04x, ptr %8x, length %d\n",
607 (unsigned)pwsi, s, (int) name, namelen);
612 if ( _check_ws(pwsi, pws) )
613 if ( namelen >= sizeof(*name) )
614 if ( ((struct ws_sockaddr_in *)name)->sin_family == AF_INET )
615 if ( bind(pws->fd, name, namelen) < 0 )
617 int loc_errno = errno;
618 WARN(winsock, "\tfailure - errno = %i\n", errno);
622 case EBADF: pwsi->err = WSAENOTSOCK; break;
623 case EADDRNOTAVAIL: pwsi->err = WSAEINVAL; break;
624 default: pwsi->err = wsaErrno();
627 else return 0; /* success */
628 else pwsi->err = WSAEAFNOSUPPORT;
629 else pwsi->err = WSAEFAULT;
633 /***********************************************************************
636 INT16 WINAPI WINSOCK_bind16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
638 return (INT16)WINSOCK_bind32( s, name, namelen );
641 /***********************************************************************
642 * closesocket() (WSOCK32.3)
644 INT32 WINAPI WINSOCK_closesocket32(SOCKET32 s)
646 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
647 LPWSINFO pwsi = wsi_find(GetCurrentTask());
649 TRACE(winsock, "(%08x): socket %08x\n", (unsigned)pwsi, s);
651 if( _check_ws(pwsi, pws) )
655 if( pws->psop ) WSAAsyncSelect32( s, 0, 0, 0 );
658 pws->flags = (unsigned)pwsi->last_free;
659 pwsi->last_free = pws - &pwsi->sock[0]; /* add to free list */
663 pwsi->err = (errno == EBADF) ? WSAENOTSOCK : wsaErrno();
668 /***********************************************************************
669 * closesocket() (WINSOCK.3)
671 INT16 WINAPI WINSOCK_closesocket16(SOCKET16 s)
673 return (INT16)WINSOCK_closesocket32(s);
676 /***********************************************************************
677 * connect() (WSOCK32.4)
679 INT32 WINAPI WINSOCK_connect32(SOCKET32 s, struct sockaddr *name, INT32 namelen)
681 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
682 LPWSINFO pwsi = wsi_find(GetCurrentTask());
684 TRACE(winsock, "(%08x): socket %04x, ptr %8x, length %d\n",
685 (unsigned)pwsi, s, (int) name, namelen);
690 if( _check_ws(pwsi, pws) )
692 if (connect(pws->fd, name, namelen) == 0)
694 if( pws->psop && (pws->flags & WS_FD_CONNECT) )
696 /* application did AsyncSelect() but then went
697 * ahead and called connect() without waiting for
700 * FIXME: Do we have to post a notification message
704 if( !(pws->flags & WS_FD_CONNECTED) )
706 if( pws->flags & (WS_FD_READ | WS_FD_CLOSE) )
707 EVENT_AddIO( pws->fd, EVENT_IO_READ );
709 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
710 if( pws->flags & WS_FD_WRITE )
711 EVENT_AddIO( pws->fd, EVENT_IO_WRITE );
713 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
716 pws->flags |= WS_FD_CONNECTED;
717 pws->flags &= ~(WS_FD_INACTIVE | WS_FD_CONNECT | WS_FD_LISTENING);
720 pwsi->err = (errno == EINPROGRESS) ? WSAEWOULDBLOCK : wsaErrno();
725 /***********************************************************************
726 * connect() (WINSOCK.4)
728 INT16 WINAPI WINSOCK_connect16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
730 return (INT16)WINSOCK_connect32( s, name, namelen );
733 /***********************************************************************
734 * getpeername() (WSOCK32.5)
736 INT32 WINAPI WINSOCK_getpeername32(SOCKET32 s, struct sockaddr *name,
739 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
740 LPWSINFO pwsi = wsi_find(GetCurrentTask());
742 TRACE(winsock, "(%08x): socket: %04x, ptr %8x, ptr %8x\n",
743 (unsigned)pwsi, s, (int) name, *namelen);
744 if( _check_ws(pwsi, pws) )
746 if (getpeername(pws->fd, name, namelen) == 0)
748 pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
753 /***********************************************************************
754 * getpeername() (WINSOCK.5)
756 INT16 WINAPI WINSOCK_getpeername16(SOCKET16 s, struct sockaddr *name,
759 INT32 namelen32 = *namelen16;
760 INT32 retVal = WINSOCK_getpeername32( s, name, &namelen32 );
766 *namelen16 = namelen32;
767 return (INT16)retVal;
770 /***********************************************************************
771 * getsockname() (WSOCK32.6)
773 INT32 WINAPI WINSOCK_getsockname32(SOCKET32 s, struct sockaddr *name,
776 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
777 LPWSINFO pwsi = wsi_find(GetCurrentTask());
779 TRACE(winsock, "(%08x): socket: %04x, ptr %8x, ptr %8x\n",
780 (unsigned)pwsi, s, (int) name, (int) *namelen);
781 if( _check_ws(pwsi, pws) )
783 if (getsockname(pws->fd, name, namelen) == 0)
785 pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
790 /***********************************************************************
791 * getsockname() (WINSOCK.6)
793 INT16 WINAPI WINSOCK_getsockname16(SOCKET16 s, struct sockaddr *name,
800 INT32 namelen32 = *namelen16;
801 retVal = WINSOCK_getsockname32( s, name, &namelen32 );
802 *namelen16 = namelen32;
809 else retVal = SOCKET_ERROR;
810 return (INT16)retVal;
814 /***********************************************************************
815 * getsockopt() (WSOCK32.7)
817 INT32 WINAPI WINSOCK_getsockopt32(SOCKET32 s, INT32 level,
818 INT32 optname, char *optval, INT32 *optlen)
820 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
821 LPWSINFO pwsi = wsi_find(GetCurrentTask());
823 TRACE(winsock, "(%08x): socket: %04x, opt %d, ptr %8x, ptr %8x\n",
824 (unsigned)pwsi, s, level, (int) optval, (int) *optlen);
825 if( _check_ws(pwsi, pws) )
827 convert_sockopt(&level, &optname);
828 if (getsockopt(pws->fd, (int) level, optname, optval, optlen) == 0 )
830 pwsi->err = (errno == EBADF) ? WSAENOTSOCK : wsaErrno();
835 /***********************************************************************
836 * getsockopt() (WINSOCK.7)
838 INT16 WINAPI WINSOCK_getsockopt16(SOCKET16 s, INT16 level,
839 INT16 optname, char *optval, INT16 *optlen)
842 INT32 *p = &optlen32;
844 if( optlen ) optlen32 = *optlen; else p = NULL;
845 retVal = WINSOCK_getsockopt32( s, (UINT16)level, optname, optval, p );
846 if( optlen ) *optlen = optlen32;
847 return (INT16)retVal;
850 /***********************************************************************
851 * htonl() (WINSOCK.8)(WSOCK32.8)
853 u_long WINAPI WINSOCK_htonl(u_long hostlong) { return( htonl(hostlong) ); }
854 /***********************************************************************
855 * htons() (WINSOCK.9)(WSOCK32.9)
857 u_short WINAPI WINSOCK_htons(u_short hostshort) { return( htons(hostshort) ); }
858 /***********************************************************************
859 * inet_addr() (WINSOCK.10)(WSOCK32.10)
861 u_long WINAPI WINSOCK_inet_addr(char *cp) { return( inet_addr(cp) ); }
862 /***********************************************************************
863 * htohl() (WINSOCK.14)(WSOCK32.14)
865 u_long WINAPI WINSOCK_ntohl(u_long netlong) { return( ntohl(netlong) ); }
866 /***********************************************************************
867 * ntohs() (WINSOCK.15)(WSOCK32.15)
869 u_short WINAPI WINSOCK_ntohs(u_short netshort) { return( ntohs(netshort) ); }
871 /***********************************************************************
872 * inet_ntoa() (WINSOCK.11)(WSOCK32.11)
874 char* WINAPI WINSOCK_inet_ntoa32(struct in_addr in)
876 /* use "buffer for dummies" here because some applications have
877 * propensity to decode addresses in ws_hostent structure without
878 * saving them first...
881 LPWSINFO pwsi = wsi_find(GetCurrentTask());
885 char* s = inet_ntoa(in);
888 if( pwsi->dbuffer == NULL )
889 if((pwsi->dbuffer = (char*) SEGPTR_ALLOC(32)) == NULL )
891 pwsi->err = WSAENOBUFS;
894 strncpy(pwsi->dbuffer, s, 32 );
895 return pwsi->dbuffer;
897 pwsi->err = wsaErrno();
902 SEGPTR WINAPI WINSOCK_inet_ntoa16(struct in_addr in)
904 char* retVal = WINSOCK_inet_ntoa32(in);
905 return retVal ? SEGPTR_GET(retVal) : (SEGPTR)NULL;
908 /***********************************************************************
909 * ioctlsocket() (WSOCK32.12)
911 INT32 WINAPI WINSOCK_ioctlsocket32(SOCKET32 s, UINT32 cmd, UINT32 *argp)
913 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
914 LPWSINFO pwsi = wsi_find(GetCurrentTask());
916 TRACE(winsock, "(%08x): socket %04x, cmd %08x, ptr %8x\n",
917 (unsigned)pwsi, s, cmd, (unsigned) argp);
918 if( _check_ws(pwsi, pws) )
930 if( pws->psop && *argp == 0 )
932 /* AsyncSelect()'ed sockets are always nonblocking */
933 pwsi->err = WSAEINVAL;
942 case WS_IOW('f',125,u_long):
943 WARN(winsock,"Warning: WS1.1 shouldn't be using async I/O\n");
944 pwsi->err = WSAEINVAL;
948 /* Netscape tries hard to use bogus ioctl 0x667e */
949 WARN(winsock, "\tunknown WS_IOCTL cmd (%08x)\n", cmd);
951 if( ioctl(pws->fd, newcmd, (char*)argp ) == 0 ) return 0;
952 pwsi->err = (errno == EBADF) ? WSAENOTSOCK : wsaErrno();
957 /***********************************************************************
958 * ioctlsocket() (WINSOCK.12)
960 INT16 WINAPI WINSOCK_ioctlsocket16(SOCKET16 s, UINT32 cmd, UINT32 *argp)
962 return (INT16)WINSOCK_ioctlsocket32( s, cmd, argp );
966 /***********************************************************************
967 * listen() (WSOCK32.13)
969 INT32 WINAPI WINSOCK_listen32(SOCKET32 s, INT32 backlog)
971 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
972 LPWSINFO pwsi = wsi_find(GetCurrentTask());
974 TRACE(winsock, "(%08x): socket %04x, backlog %d\n",
975 (unsigned)pwsi, s, backlog);
976 if( _check_ws(pwsi, pws) )
978 if (listen(pws->fd, backlog) == 0)
982 int fd_flags = fcntl(pws->fd, F_GETFL, 0);
983 if( !(fd_flags & O_NONBLOCK) ) pws->flags |= WS_FD_ACCEPT;
985 pws->flags |= WS_FD_LISTENING;
986 pws->flags &= ~(WS_FD_INACTIVE | WS_FD_CONNECT | WS_FD_CONNECTED); /* just in case */
989 pwsi->err = wsaErrno();
991 else if( pwsi ) pwsi->err = WSAENOTSOCK;
995 /***********************************************************************
996 * listen() (WINSOCK.13)
998 INT16 WINAPI WINSOCK_listen16(SOCKET16 s, INT16 backlog)
1000 return (INT16)WINSOCK_listen32( s, backlog );
1004 /***********************************************************************
1005 * recv() (WSOCK32.16)
1007 INT32 WINAPI WINSOCK_recv32(SOCKET32 s, char *buf, INT32 len, INT32 flags)
1009 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1010 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1012 TRACE(winsock, "(%08x): socket %04x, buf %8x, len %d, "
1013 "flags %d\n", (unsigned)pwsi, s, (unsigned)buf,
1015 if( _check_ws(pwsi, pws) )
1018 if ((length = recv(pws->fd, buf, len, flags)) >= 0)
1020 TRACE(winsock, " -> %i bytes\n", length);
1022 if( pws->psop && (pws->flags & (WS_FD_READ | WS_FD_CLOSE)) )
1023 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
1027 pwsi->err = wsaErrno();
1029 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1030 WARN(winsock, " -> ERROR\n");
1031 return SOCKET_ERROR;
1034 /***********************************************************************
1035 * recv() (WINSOCK.16)
1037 INT16 WINAPI WINSOCK_recv16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1039 return (INT16)WINSOCK_recv32( s, buf, len, flags );
1043 /***********************************************************************
1044 * recvfrom() (WSOCK32.17)
1046 INT32 WINAPI WINSOCK_recvfrom32(SOCKET32 s, char *buf, INT32 len, INT32 flags,
1047 struct sockaddr *from, INT32 *fromlen32)
1049 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1050 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1052 TRACE(winsock, "(%08x): socket %04x, ptr %08x, "
1053 "len %d, flags %d\n", (unsigned)pwsi, s, (unsigned)buf,
1056 if( from ) dump_sockaddr(from);
1057 else DUMP("from = NULL\n");
1060 if( _check_ws(pwsi, pws) )
1064 if ((length = recvfrom(pws->fd, buf, len, flags, from, fromlen32)) >= 0)
1066 TRACE(winsock, " -> %i bytes\n", length);
1068 if( pws->psop && (pws->flags & (WS_FD_READ | WS_FD_CLOSE)) )
1069 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
1071 return (INT16)length;
1073 pwsi->err = wsaErrno();
1075 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1076 WARN(winsock, " -> ERROR\n");
1077 return SOCKET_ERROR;
1080 /***********************************************************************
1081 * recvfrom() (WINSOCK.17)
1083 INT16 WINAPI WINSOCK_recvfrom16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1084 struct sockaddr *from, INT16 *fromlen16)
1087 INT32 *p = &fromlen32;
1090 if( fromlen16 ) fromlen32 = *fromlen16; else p = NULL;
1091 retVal = WINSOCK_recvfrom32( s, buf, len, flags, from, p );
1092 if( fromlen16 ) *fromlen16 = fromlen32;
1093 return (INT16)retVal;
1096 /***********************************************************************
1097 * select() (WINSOCK.18)(WSOCK32.18)
1099 static INT32 __ws_select( BOOL32 b32, void *ws_readfds, void *ws_writefds, void *ws_exceptfds,
1100 struct timeval *timeout )
1102 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1104 TRACE(winsock, "(%08x): read %8x, write %8x, excp %8x\n",
1105 (unsigned) pwsi, (unsigned) ws_readfds, (unsigned) ws_writefds, (unsigned) ws_exceptfds);
1110 fd_set readfds, writefds, exceptfds;
1111 fd_set *p_read, *p_write, *p_except;
1113 p_read = fd_set_import(&readfds, pwsi, ws_readfds, &highfd, b32);
1114 p_write = fd_set_import(&writefds, pwsi, ws_writefds, &highfd, b32);
1115 p_except = fd_set_import(&exceptfds, pwsi, ws_exceptfds, &highfd, b32);
1117 if( (highfd = select(highfd + 1, p_read, p_write, p_except, timeout)) > 0 )
1119 fd_set_export(pwsi, &readfds, p_except, ws_readfds, b32);
1120 fd_set_export(pwsi, &writefds, p_except, ws_writefds, b32);
1122 if (p_except && ws_exceptfds)
1124 #define wsfds16 ((ws_fd_set16*)ws_exceptfds)
1125 #define wsfds32 ((ws_fd_set32*)ws_exceptfds)
1126 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
1128 for (i = j = 0; i < count; i++)
1130 ws_socket *pws = (b32) ? (ws_socket *)WS_HANDLE2PTR(wsfds32->fd_array[i])
1131 : (ws_socket *)WS_HANDLE2PTR(wsfds16->fd_array[i]);
1132 if( _check_ws(pwsi, pws) && FD_ISSET(pws->fd, &exceptfds) )
1135 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
1137 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
1141 wsfds32->fd_count = j;
1143 wsfds16->fd_count = j;
1149 if( ws_readfds ) ((ws_fd_set32*)ws_readfds)->fd_count = 0;
1150 if( ws_writefds ) ((ws_fd_set32*)ws_writefds)->fd_count = 0;
1151 if( ws_exceptfds ) ((ws_fd_set32*)ws_exceptfds)->fd_count = 0;
1153 if( highfd == 0 ) return 0;
1154 pwsi->err = wsaErrno();
1156 return SOCKET_ERROR;
1159 INT16 WINAPI WINSOCK_select16(INT16 nfds, ws_fd_set16 *ws_readfds,
1160 ws_fd_set16 *ws_writefds, ws_fd_set16 *ws_exceptfds,
1161 struct timeval *timeout)
1163 return (INT16)__ws_select( FALSE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1166 INT32 WINAPI WINSOCK_select32(INT32 nfds, ws_fd_set32 *ws_readfds,
1167 ws_fd_set32 *ws_writefds, ws_fd_set32 *ws_exceptfds,
1168 struct timeval *timeout)
1170 /* struct timeval is the same for both 32- and 16-bit code */
1171 return (INT32)__ws_select( TRUE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1175 /***********************************************************************
1176 * send() (WSOCK32.19)
1178 INT32 WINAPI WINSOCK_send32(SOCKET32 s, char *buf, INT32 len, INT32 flags)
1180 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1181 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1183 TRACE(winsock, "(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
1184 (unsigned)pwsi, s, (unsigned) buf, len, flags);
1185 if( _check_ws(pwsi, pws) )
1189 if ((length = send(pws->fd, buf, len, flags)) < 0 )
1191 pwsi->err = wsaErrno();
1192 if( pwsi->err == WSAEWOULDBLOCK &&
1193 pws->psop && pws->flags & WS_FD_WRITE )
1194 EVENT_AddIO( pws->fd, EVENT_IO_WRITE ); /* reenabler */
1196 else return (INT16)length;
1198 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1199 return SOCKET_ERROR;
1202 /***********************************************************************
1203 * send() (WINSOCK.19)
1205 INT16 WINAPI WINSOCK_send16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1207 return WINSOCK_send32( s, buf, len, flags );
1210 /***********************************************************************
1211 * sendto() (WSOCK32.20)
1213 INT32 WINAPI WINSOCK_sendto32(SOCKET32 s, char *buf, INT32 len, INT32 flags,
1214 struct sockaddr *to, INT32 tolen)
1216 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1217 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1219 TRACE(winsock, "(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
1220 (unsigned)pwsi, s, (unsigned) buf, len, flags);
1221 if( _check_ws(pwsi, pws) )
1225 if ((length = sendto(pws->fd, buf, len, flags, to, tolen)) < 0 )
1227 pwsi->err = wsaErrno();
1228 if( pwsi->err == WSAEWOULDBLOCK &&
1229 pws->psop && pws->flags & WS_FD_WRITE )
1230 EVENT_AddIO( pws->fd, EVENT_IO_WRITE ); /* reenabler */
1234 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1235 return SOCKET_ERROR;
1238 /***********************************************************************
1239 * sendto() (WINSOCK.20)
1241 INT16 WINAPI WINSOCK_sendto16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1242 struct sockaddr *to, INT16 tolen)
1244 return (INT16)WINSOCK_sendto32( s, buf, len, flags, to, tolen );
1247 /***********************************************************************
1248 * setsockopt() (WSOCK32.21)
1250 INT32 WINAPI WINSOCK_setsockopt32(SOCKET16 s, INT32 level, INT32 optname,
1251 char *optval, INT32 optlen)
1253 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1254 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1256 TRACE(winsock, "(%08x): socket %04x, lev %d, opt %d, ptr %08x, len %d\n",
1257 (unsigned)pwsi, s, level, optname, (int) optval, optlen);
1258 if( _check_ws(pwsi, pws) )
1260 struct linger linger;
1262 convert_sockopt(&level, &optname);
1263 if (optname == SO_LINGER) {
1264 /* yes, uses unsigned short in both win16/win32 */
1265 linger.l_onoff = ((UINT16*)optval)[0];
1266 linger.l_linger = ((UINT16*)optval)[1];
1267 optval = (char*)&linger;
1268 optlen = sizeof(struct linger);
1270 if (setsockopt(pws->fd, level, optname, optval, optlen) == 0) return 0;
1271 pwsi->err = wsaErrno();
1273 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1274 return SOCKET_ERROR;
1277 /***********************************************************************
1278 * setsockopt() (WINSOCK.21)
1280 INT16 WINAPI WINSOCK_setsockopt16(SOCKET16 s, INT16 level, INT16 optname,
1281 char *optval, INT16 optlen)
1283 if( !optval ) return SOCKET_ERROR;
1284 return (INT16)WINSOCK_setsockopt32( s, (UINT16)level, optname, optval, optlen );
1288 /***********************************************************************
1289 * shutdown() (WSOCK32.22)
1291 INT32 WINAPI WINSOCK_shutdown32(SOCKET32 s, INT32 how)
1293 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1294 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1296 TRACE(winsock, "(%08x): socket %04x, how %i\n",
1297 (unsigned)pwsi, s, how );
1298 if( _check_ws(pwsi, pws) )
1303 case 0: /* drop receives */
1304 if( pws->flags & (WS_FD_READ | WS_FD_CLOSE) )
1305 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
1306 pws->flags &= ~(WS_FD_READ | WS_FD_CLOSE);
1312 case 1: /* drop sends */
1313 if( pws->flags & WS_FD_WRITE )
1314 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
1315 pws->flags &= ~WS_FD_WRITE;
1321 case 2: /* drop all */
1326 WSAAsyncSelect32( s, 0, 0, 0 );
1330 if (shutdown(pws->fd, how) == 0)
1334 pws->flags &= ~(WS_FD_CONNECTED | WS_FD_LISTENING);
1335 pws->flags |= WS_FD_INACTIVE;
1339 pwsi->err = wsaErrno();
1341 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1342 return SOCKET_ERROR;
1345 /***********************************************************************
1346 * shutdown() (WINSOCK.22)
1348 INT16 WINAPI WINSOCK_shutdown16(SOCKET16 s, INT16 how)
1350 return (INT16)WINSOCK_shutdown32( s, how );
1354 /***********************************************************************
1355 * socket() (WSOCK32.23)
1357 SOCKET32 WINAPI WINSOCK_socket32(INT32 af, INT32 type, INT32 protocol)
1359 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1361 TRACE(winsock, "(%08x): af=%d type=%d protocol=%d\n",
1362 (unsigned)pwsi, af, type, protocol);
1368 /* check the socket family */
1372 case AF_UNSPEC: break;
1373 default: pwsi->err = WSAEAFNOSUPPORT;
1374 return INVALID_SOCKET32;
1377 /* check the socket type */
1382 case SOCK_RAW: break;
1383 default: pwsi->err = WSAESOCKTNOSUPPORT;
1384 return INVALID_SOCKET32;
1387 /* check the protocol type */
1388 if ( protocol < 0 ) /* don't support negative values */
1389 { pwsi->err = WSAEPROTONOSUPPORT; return INVALID_SOCKET32; }
1391 if ( af == AF_UNSPEC) /* did they not specify the address family? */
1395 if (type == SOCK_STREAM) { af = AF_INET; break; }
1397 if (type == SOCK_DGRAM) { af = AF_INET; break; }
1398 default: pwsi->err = WSAEPROTOTYPE; return INVALID_SOCKET32;
1401 if ((sock = socket(af, type, protocol)) >= 0)
1403 ws_socket* pnew = wsi_alloc_socket(pwsi, sock);
1405 TRACE(winsock,"\tcreated %i (handle %04x)\n", sock, (UINT16)WS_PTR2HANDLE(pnew));
1409 pnew->flags |= WS_FD_INACTIVE;
1410 return (SOCKET16)WS_PTR2HANDLE(pnew);
1414 pwsi->err = WSAENOBUFS;
1415 return INVALID_SOCKET32;
1418 if (errno == EPERM) /* raw socket denied */
1420 WARN(winsock, "WS_SOCKET: not enough privileges\n");
1421 pwsi->err = WSAESOCKTNOSUPPORT;
1422 } else pwsi->err = wsaErrno();
1425 WARN(winsock, "\t\tfailed!\n");
1426 return INVALID_SOCKET32;
1429 /***********************************************************************
1430 * socket() (WINSOCK.23)
1432 SOCKET16 WINAPI WINSOCK_socket16(INT16 af, INT16 type, INT16 protocol)
1434 return (SOCKET16)WINSOCK_socket32( af, type, protocol );
1438 /* ----------------------------------- DNS services
1440 * IMPORTANT: 16-bit API structures have SEGPTR pointers inside them.
1441 * Also, we have to use wsock32 stubs to convert structures and
1442 * error codes from Unix to WSA, hence there is no direct mapping in
1443 * the relay32/wsock32.spec.
1446 static char* NULL_STRING = "NULL";
1448 /***********************************************************************
1449 * gethostbyaddr() (WINSOCK.51)(WSOCK32.51)
1451 static struct WIN_hostent* __ws_gethostbyaddr(const char *addr, int len, int type, int dup_flag)
1453 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1457 struct hostent* host;
1458 if( (host = gethostbyaddr(addr, len, type)) != NULL )
1459 if( WS_dup_he(pwsi, host, dup_flag) )
1460 return (struct WIN_hostent*)(pwsi->he);
1462 pwsi->err = WSAENOBUFS;
1464 pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1469 SEGPTR WINAPI WINSOCK_gethostbyaddr16(const char *addr, INT16 len, INT16 type)
1471 struct WIN_hostent* retval;
1472 TRACE(winsock, "ptr %08x, len %d, type %d\n",
1473 (unsigned) addr, len, type);
1474 retval = __ws_gethostbyaddr( addr, len, type, WS_DUP_SEGPTR );
1475 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1478 struct WIN_hostent* WINAPI WINSOCK_gethostbyaddr32(const char *addr, INT32 len,
1481 TRACE(winsock, "ptr %08x, len %d, type %d\n",
1482 (unsigned) addr, len, type);
1483 return __ws_gethostbyaddr(addr, len, type, WS_DUP_LINEAR);
1486 /***********************************************************************
1487 * gethostbyname() (WINSOCK.52)(WSOCK32.52)
1489 static struct WIN_hostent * __ws_gethostbyname(const char *name, int dup_flag)
1491 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1495 struct hostent* host;
1496 if( (host = gethostbyname(name)) != NULL )
1497 if( WS_dup_he(pwsi, host, dup_flag) )
1498 return (struct WIN_hostent*)(pwsi->he);
1499 else pwsi->err = WSAENOBUFS;
1500 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1505 SEGPTR WINAPI WINSOCK_gethostbyname16(const char *name)
1507 struct WIN_hostent* retval;
1508 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1509 retval = __ws_gethostbyname( name, WS_DUP_SEGPTR );
1510 return (retval)? SEGPTR_GET(retval) : ((SEGPTR)NULL) ;
1513 struct WIN_hostent* WINAPI WINSOCK_gethostbyname32(const char* name)
1515 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1516 return __ws_gethostbyname( name, WS_DUP_LINEAR );
1520 /***********************************************************************
1521 * getprotobyname() (WINSOCK.53)(WSOCK32.53)
1523 static struct WIN_protoent* __ws_getprotobyname(const char *name, int dup_flag)
1525 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1529 struct protoent* proto;
1530 if( (proto = getprotobyname(name)) != NULL )
1531 if( WS_dup_pe(pwsi, proto, dup_flag) )
1532 return (struct WIN_protoent*)(pwsi->pe);
1533 else pwsi->err = WSAENOBUFS;
1534 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1539 SEGPTR WINAPI WINSOCK_getprotobyname16(const char *name)
1541 struct WIN_protoent* retval;
1542 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1543 retval = __ws_getprotobyname(name, WS_DUP_SEGPTR);
1544 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1547 struct WIN_protoent* WINAPI WINSOCK_getprotobyname32(const char* name)
1549 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1550 return __ws_getprotobyname(name, WS_DUP_LINEAR);
1554 /***********************************************************************
1555 * getprotobynumber() (WINSOCK.54)(WSOCK32.54)
1557 static struct WIN_protoent* __ws_getprotobynumber(int number, int dup_flag)
1559 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1563 struct protoent* proto;
1564 if( (proto = getprotobynumber(number)) != NULL )
1565 if( WS_dup_pe(pwsi, proto, dup_flag) )
1566 return (struct WIN_protoent*)(pwsi->pe);
1567 else pwsi->err = WSAENOBUFS;
1568 else pwsi->err = WSANO_DATA;
1573 SEGPTR WINAPI WINSOCK_getprotobynumber16(INT16 number)
1575 struct WIN_protoent* retval;
1576 TRACE(winsock, "%i\n", number);
1577 retval = __ws_getprotobynumber(number, WS_DUP_SEGPTR);
1578 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1581 struct WIN_protoent* WINAPI WINSOCK_getprotobynumber32(INT32 number)
1583 TRACE(winsock, "%i\n", number);
1584 return __ws_getprotobynumber(number, WS_DUP_LINEAR);
1588 /***********************************************************************
1589 * getservbyname() (WINSOCK.55)(WSOCK32.55)
1591 struct WIN_servent* __ws_getservbyname(const char *name, const char *proto, int dup_flag)
1593 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1597 struct servent* serv;
1598 int i = wsi_strtolo( pwsi, name, proto );
1601 if( (serv = getservbyname(pwsi->buffer, pwsi->buffer + i)) != NULL )
1602 if( WS_dup_se(pwsi, serv, dup_flag) )
1603 return (struct WIN_servent*)(pwsi->se);
1604 else pwsi->err = WSAENOBUFS;
1605 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1606 else pwsi->err = WSAENOBUFS;
1611 SEGPTR WINAPI WINSOCK_getservbyname16(const char *name, const char *proto)
1613 struct WIN_servent* retval;
1614 TRACE(winsock, "'%s', '%s'\n",
1615 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
1616 retval = __ws_getservbyname(name, proto, WS_DUP_SEGPTR);
1617 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1620 struct WIN_servent* WINAPI WINSOCK_getservbyname32(const char *name, const char *proto)
1622 TRACE(winsock, "'%s', '%s'\n",
1623 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
1624 return __ws_getservbyname(name, proto, WS_DUP_LINEAR);
1628 /***********************************************************************
1629 * getservbyport() (WINSOCK.56)(WSOCK32.56)
1631 static struct WIN_servent* __ws_getservbyport(int port, const char* proto, int dup_flag)
1633 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1637 struct servent* serv;
1638 int i = wsi_strtolo( pwsi, proto, NULL );
1641 if( (serv = getservbyport(port, pwsi->buffer)) != NULL )
1642 if( WS_dup_se(pwsi, serv, dup_flag) )
1643 return (struct WIN_servent*)(pwsi->se);
1644 else pwsi->err = WSAENOBUFS;
1645 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1646 else pwsi->err = WSAENOBUFS;
1651 SEGPTR WINAPI WINSOCK_getservbyport16(INT16 port, const char *proto)
1653 struct WIN_servent* retval;
1654 TRACE(winsock, "%i, '%s'\n",
1655 (int)port, (proto)?proto:NULL_STRING);
1656 retval = __ws_getservbyport(port, proto, WS_DUP_SEGPTR);
1657 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1660 struct WIN_servent* WINAPI WINSOCK_getservbyport32(INT32 port, const char *proto)
1662 TRACE(winsock, "%i, '%s'\n",
1663 (int)port, (proto)?proto:NULL_STRING);
1664 return __ws_getservbyport(port, proto, WS_DUP_LINEAR);
1668 /***********************************************************************
1669 * gethostname() (WSOCK32.57)
1671 INT32 WINAPI WINSOCK_gethostname32(char *name, INT32 namelen)
1673 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1675 TRACE(winsock, "(%08x): name %s, len %d\n",
1676 (unsigned)pwsi, (name)?name:NULL_STRING, namelen);
1679 if (gethostname(name, namelen) == 0) return 0;
1680 pwsi->err = (errno == EINVAL) ? WSAEFAULT : wsaErrno();
1682 return SOCKET_ERROR;
1685 /***********************************************************************
1686 * gethostname() (WINSOCK.57)
1688 INT16 WINAPI WINSOCK_gethostname16(char *name, INT16 namelen)
1690 return (INT16)WINSOCK_gethostname32(name, namelen);
1694 /* ------------------------------------- Windows sockets extensions -- *
1696 * ------------------------------------------------------------------- */
1699 /***********************************************************************
1700 * WSAAsyncGetHostByAddr() (WINSOCK.102)
1702 HANDLE16 WINAPI WSAAsyncGetHostByAddr16(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,
1703 INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen)
1705 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1707 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, addr %08x[%i]\n",
1708 (unsigned)pwsi, hWnd, uMsg, (unsigned)addr , len );
1711 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, type, addr, len,
1712 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYADDR );
1716 /***********************************************************************
1717 * WSAAsyncGetHostByAddr() (WSOCK32.102)
1719 HANDLE32 WINAPI WSAAsyncGetHostByAddr32(HWND32 hWnd, UINT32 uMsg, LPCSTR addr,
1720 INT32 len, INT32 type, LPSTR sbuf, INT32 buflen)
1722 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1724 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, addr %08x[%i]\n",
1725 (unsigned)pwsi, (HWND16)hWnd, uMsg, (unsigned)addr , len );
1728 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, type, addr, len,
1729 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYADDR | WSMSG_WIN32_AOP);
1734 /***********************************************************************
1735 * WSAAsyncGetHostByName() (WINSOCK.103)
1737 HANDLE16 WINAPI WSAAsyncGetHostByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
1738 SEGPTR sbuf, INT16 buflen)
1740 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1742 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, host %s,
1743 buffer %i\n", (unsigned)pwsi, hWnd, uMsg, (name)?name:NULL_STRING, (int)buflen );
1746 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
1747 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYNAME );
1751 /***********************************************************************
1752 * WSAAsyncGetHostByName32() (WSOCK32.103)
1754 HANDLE32 WINAPI WSAAsyncGetHostByName32(HWND32 hWnd, UINT32 uMsg, LPCSTR name,
1755 LPSTR sbuf, INT32 buflen)
1757 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1758 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, host %s, buffer %i\n",
1759 (unsigned)pwsi, (HWND16)hWnd, uMsg,
1760 (name)?name:NULL_STRING, (int)buflen );
1762 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
1763 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYNAME | WSMSG_WIN32_AOP);
1768 /***********************************************************************
1769 * WSAAsyncGetProtoByName() (WINSOCK.105)
1771 HANDLE16 WINAPI WSAAsyncGetProtoByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
1772 SEGPTR sbuf, INT16 buflen)
1774 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1776 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, protocol %s\n",
1777 (unsigned)pwsi, (HWND16)hWnd, uMsg, (name)?name:NULL_STRING );
1780 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
1781 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNAME );
1785 /***********************************************************************
1786 * WSAAsyncGetProtoByName() (WSOCK32.105)
1788 HANDLE32 WINAPI WSAAsyncGetProtoByName32(HWND32 hWnd, UINT32 uMsg, LPCSTR name,
1789 LPSTR sbuf, INT32 buflen)
1791 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1793 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, protocol %s\n",
1794 (unsigned)pwsi, (HWND16)hWnd, uMsg, (name)?name:NULL_STRING );
1797 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
1798 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNAME | WSMSG_WIN32_AOP);
1803 /***********************************************************************
1804 * WSAAsyncGetProtoByNumber() (WINSOCK.104)
1806 HANDLE16 WINAPI WSAAsyncGetProtoByNumber16(HWND16 hWnd, UINT16 uMsg, INT16 number,
1807 SEGPTR sbuf, INT16 buflen)
1809 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1811 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, num %i\n",
1812 (unsigned)pwsi, hWnd, uMsg, number );
1815 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, number, NULL, 0,
1816 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNUM );
1820 /***********************************************************************
1821 * WSAAsyncGetProtoByNumber() (WSOCK32.104)
1823 HANDLE32 WINAPI WSAAsyncGetProtoByNumber32(HWND32 hWnd, UINT32 uMsg, INT32 number,
1824 LPSTR sbuf, INT32 buflen)
1826 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1828 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, num %i\n",
1829 (unsigned)pwsi, (HWND16)hWnd, uMsg, number );
1832 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, number, NULL, 0,
1833 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNUM | WSMSG_WIN32_AOP);
1838 /***********************************************************************
1839 * WSAAsyncGetServByName() (WINSOCK.107)
1841 HANDLE16 WINAPI WSAAsyncGetServByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
1842 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
1844 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1846 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, name %s, proto %s\n",
1847 (unsigned)pwsi, hWnd, uMsg,
1848 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING );
1852 int i = wsi_strtolo( pwsi, name, proto );
1855 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, pwsi->buffer, 0,
1856 pwsi->buffer + i, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYNAME );
1861 /***********************************************************************
1862 * WSAAsyncGetServByName() (WSOCK32.107)
1864 HANDLE32 WINAPI WSAAsyncGetServByName32(HWND32 hWnd, UINT32 uMsg, LPCSTR name,
1865 LPCSTR proto, LPSTR sbuf, INT32 buflen)
1867 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1869 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, name %s, proto %s\n",
1870 (unsigned)pwsi, (HWND16)hWnd, uMsg,
1871 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING );
1874 int i = wsi_strtolo( pwsi, name, proto );
1877 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, pwsi->buffer, 0,
1878 pwsi->buffer + i, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYNAME | WSMSG_WIN32_AOP);
1884 /***********************************************************************
1885 * WSAAsyncGetServByPort() (WINSOCK.106)
1887 HANDLE16 WINAPI WSAAsyncGetServByPort16(HWND16 hWnd, UINT16 uMsg, INT16 port,
1888 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
1890 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1892 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, port %i, proto %s\n",
1893 (unsigned)pwsi, hWnd, uMsg, port, (proto)?proto:NULL_STRING );
1897 int i = wsi_strtolo( pwsi, proto, NULL );
1900 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, port, pwsi->buffer, 0,
1901 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYPORT );
1906 /***********************************************************************
1907 * WSAAsyncGetServByPort() (WSOCK32.106)
1909 HANDLE32 WINAPI WSAAsyncGetServByPort32(HWND32 hWnd, UINT32 uMsg, INT32 port,
1910 LPCSTR proto, LPSTR sbuf, INT32 buflen)
1912 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1914 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, port %i, proto %s\n",
1915 (unsigned)pwsi, (HWND16)hWnd, uMsg, port, (proto)?proto:NULL_STRING );
1919 int i = wsi_strtolo( pwsi, proto, NULL );
1922 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, port, pwsi->buffer, 0,
1923 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYPORT | WSMSG_WIN32_AOP);
1929 /***********************************************************************
1930 * WSACancelAsyncRequest() (WINSOCK.108)(WSOCK32.109)
1932 INT32 WINAPI WSACancelAsyncRequest32(HANDLE32 hAsyncTaskHandle)
1934 INT32 retVal = SOCKET_ERROR;
1935 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1936 ws_async_op* p_aop = (ws_async_op*)WS_HANDLE2PTR(hAsyncTaskHandle);
1938 TRACE(winsock, "(%08x): handle %08x\n",
1939 (unsigned)pwsi, hAsyncTaskHandle);
1942 SIGNAL_MaskAsyncEvents( TRUE ); /* block SIGIO */
1943 if( WINSOCK_cancel_async_op(p_aop) )
1946 pwsi->num_async_rq--;
1949 else pwsi->err = WSAEINVAL;
1950 SIGNAL_MaskAsyncEvents( FALSE );
1955 INT16 WINAPI WSACancelAsyncRequest16(HANDLE16 hAsyncTaskHandle)
1957 return (HANDLE16)WSACancelAsyncRequest32((HANDLE32)hAsyncTaskHandle);
1960 /***********************************************************************
1961 * WSAAsyncSelect() (WINSOCK.101)(WSOCK32.101)
1964 static ws_select_op* __ws_select_list = NULL;
1966 BOOL32 WINSOCK_HandleIO( int* max_fd, int num_pending,
1967 fd_set pending_set[3], fd_set event_set[3] )
1969 /* This function is called by the event dispatcher
1970 * with the pending_set[] containing the result of select() and
1971 * the event_set[] containing all fd that are being watched */
1973 ws_select_op* psop = __ws_select_list;
1974 BOOL32 bPost = FALSE;
1975 DWORD dwEvent, dwErrBytes;
1978 TRACE(winsock,"%i pending descriptors\n", num_pending );
1980 for( num_posted = dwEvent = 0 ; psop; psop = psop->next )
1982 unsigned flags = psop->pws->flags;
1983 int fd = psop->pws->fd;
1987 if( (r = FD_ISSET( fd, &pending_set[EVENT_IO_READ] )) ||
1988 (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] )) ||
1989 (e = FD_ISSET( fd, &pending_set[EVENT_IO_EXCEPT] )) )
1991 /* This code removes WS_FD flags on one-shot events (WS_FD_CLOSE,
1992 * WS_FD_CONNECT), otherwise it clears descriptors in the io_set.
1993 * Reenabling calls turn them back on.
1996 TRACE(winsock,"\tchecking psop = 0x%08x\n", (unsigned) psop );
2000 /* Now figure out what kind of event we've got. The worst problem
2001 * we have to contend with is that some out of control applications
2002 * really want to use mutually exclusive AsyncSelect() flags all at
2006 if((flags & WS_FD_ACCEPT) && (flags & WS_FD_LISTENING))
2008 /* WS_FD_ACCEPT is valid only if the socket is in the
2009 * listening state */
2011 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2014 FD_CLR( fd, &event_set[EVENT_IO_READ] ); /* reenabled by the next accept() */
2015 dwEvent = WSAMAKESELECTREPLY( WS_FD_ACCEPT, 0 );
2020 else if( flags & WS_FD_CONNECT )
2022 /* connecting socket */
2024 if( w || (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] )) )
2026 /* ready to write means that socket is connected
2028 * FIXME: Netscape calls AsyncSelect( s, ... WS_FD_CONNECT .. )
2029 * right after s = socket() and somehow "s" becomes writeable
2030 * before it goes through connect()!?!?
2033 psop->pws->flags |= WS_FD_CONNECTED;
2034 psop->pws->flags &= ~(WS_FD_CONNECT | WS_FD_INACTIVE);
2035 dwEvent = WSAMAKESELECTREPLY( WS_FD_CONNECT, 0 );
2037 if( flags & (WS_FD_READ | WS_FD_CLOSE))
2038 FD_SET( fd, &event_set[EVENT_IO_READ] );
2040 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2041 if( flags & WS_FD_WRITE )
2042 FD_SET( fd, &event_set[EVENT_IO_WRITE] );
2044 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2049 /* failure - do read() to get correct errno */
2051 if( read( fd, &dwErrBytes, sizeof(dwErrBytes) ) == -1 )
2053 dwEvent = WSAMAKESELECTREPLY( WS_FD_CONNECT, wsaErrno() );
2057 /* otherwise bPost stays FALSE, should probably clear event_set */
2061 /* connected socket, no WS_FD_OOB code for now. */
2063 if( flags & WS_FD_WRITE &&
2064 (w || (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] ))) )
2066 /* this will be reenabled when send() or sendto() fail with
2069 if( PostMessage32A( psop->hWnd, psop->uMsg, (WPARAM32)WS_PTR2HANDLE(psop->pws),
2070 (LPARAM)WSAMAKESELECTREPLY( WS_FD_WRITE, 0 ) ) )
2072 TRACE(winsock, "\t hwnd %04x - %04x, %08x\n",
2073 psop->hWnd, psop->uMsg, (unsigned)MAKELONG(WS_FD_WRITE, 0) );
2074 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2079 if( r && (flags & (WS_FD_READ | WS_FD_CLOSE)) )
2081 int val = (flags & WS_FD_RAW);
2083 /* WS_FD_RAW is set by the WSAAsyncSelect() init */
2086 if( !val && ioctl( fd, FIONREAD, (char*)&dwErrBytes) == -1 )
2090 dwEvent = WSAMAKESELECTREPLY( WS_FD_READ, wsaErrno() );
2092 else if( val || dwErrBytes )
2094 /* got pending data, will be reenabled by recv() or recvfrom() */
2096 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2097 dwEvent = WSAMAKESELECTREPLY( WS_FD_READ, 0 );
2101 /* 0 bytes to read - connection reset by peer? */
2104 val = read( fd, (char*)&dwErrBytes, sizeof(dwErrBytes));
2105 while( errno == EINTR );
2106 if( errno != EWOULDBLOCK )
2110 case 0: errno = ENETDOWN; /* soft reset, fall through */
2111 case -1: /* hard reset */
2112 dwEvent = WSAMAKESELECTREPLY( WS_FD_CLOSE, wsaErrno() );
2115 default: bPost = FALSE;
2116 continue; /* FIXME: this is real bad */
2119 else { bPost = FALSE; continue; } /* more weirdness */
2121 /* this is it, this socket is closed */
2123 psop->pws->flags &= ~(WS_FD_READ | WS_FD_CLOSE | WS_FD_WRITE);
2124 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2125 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2127 if( *max_fd == (fd + 1) ) (*max_fd)--;
2134 TRACE(winsock, "\t hwnd %04x - %04x, %08x\n",
2135 psop->hWnd, psop->uMsg, (unsigned)dwEvent );
2136 PostMessage32A( psop->hWnd, psop->uMsg,
2137 (WPARAM32)WS_PTR2HANDLE(psop->pws), (LPARAM)dwEvent );
2142 if( num_pending <= 0 ) break;
2145 TRACE(winsock, "\tdone, %i posted events\n", num_posted );
2146 return ( num_posted ) ? TRUE : FALSE;
2149 INT32 WINAPI WSAAsyncSelect32(SOCKET32 s, HWND32 hWnd, UINT32 uMsg, UINT32 lEvent)
2151 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
2152 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2154 TRACE(winsock, "(%08x): %04x, hWnd %04x, uMsg %08x, event %08x\n",
2155 (unsigned)pwsi, (SOCKET16)s, (HWND16)hWnd, uMsg, (unsigned)lEvent );
2156 if( _check_ws(pwsi, pws) )
2160 if( (psop = pws->psop) )
2162 /* delete previous control struct */
2164 if( psop == __ws_select_list )
2165 __ws_select_list = psop->next;
2167 psop->prev->next = psop->next;
2168 if( psop->next ) psop->next->prev = psop->prev;
2170 if( pws->flags & (WS_FD_ACCEPT | WS_FD_CONNECT | WS_FD_READ | WS_FD_CLOSE) )
2171 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
2172 if( pws->flags & (WS_FD_CONNECT | WS_FD_WRITE) )
2173 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
2175 TRACE(winsock,"\tremoving psop = 0x%08x\n", (unsigned) psop );
2177 WS_FREE( pws->psop );
2178 pws->flags &= ~(WS_FD_RAW | WS_FD_ACCEPT | WS_FD_CONNECT |
2179 WS_FD_READ | WS_FD_WRITE | WS_FD_CLOSE);
2185 psop = (ws_select_op*)WS_ALLOC(sizeof(ws_select_op));
2188 int sock_type, bytes = sizeof(int);
2190 WINSOCK_unblock_io( pws->fd, TRUE );
2193 psop->next = __ws_select_list;
2194 if( __ws_select_list )
2195 __ws_select_list->prev = psop;
2196 __ws_select_list = psop;
2203 pws->flags |= (0x0000FFFF & lEvent);
2204 getsockopt(pws->fd, SOL_SOCKET, SO_TYPE, &sock_type, &bytes);
2205 if( sock_type == SOCK_RAW ) pws->flags |= WS_FD_RAW;
2207 if( lEvent & (WS_FD_ACCEPT | WS_FD_CONNECT | WS_FD_READ | WS_FD_CLOSE) )
2208 EVENT_AddIO( pws->fd, EVENT_IO_READ );
2209 if( lEvent & (WS_FD_CONNECT | WS_FD_WRITE) )
2210 EVENT_AddIO( pws->fd, EVENT_IO_WRITE );
2212 /* TODO: handle WS_FD_ACCEPT right away if the socket is readable */
2214 TRACE(winsock,"\tcreating psop = 0x%08x\n", (unsigned)psop );
2216 return 0; /* success */
2218 else pwsi->err = WSAENOBUFS;
2222 else if( pwsi ) pwsi->err = WSAEINVAL;
2223 return SOCKET_ERROR;
2226 INT16 WINAPI WSAAsyncSelect16(SOCKET16 s, HWND16 hWnd, UINT16 wMsg, UINT32 lEvent)
2228 return (INT16)WSAAsyncSelect32( s, hWnd, wMsg, lEvent );
2232 /***********************************************************************
2233 * __WSAFDIsSet() (WINSOCK.151)
2235 INT16 WINAPI __WSAFDIsSet16(SOCKET16 s, ws_fd_set16 *set)
2237 int i = set->fd_count;
2239 TRACE(winsock, "(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2242 if (set->fd_array[i] == s) return 1;
2246 /***********************************************************************
2247 * __WSAFDIsSet() (WSOCK32.151)
2249 INT32 WINAPI __WSAFDIsSet32(SOCKET32 s, ws_fd_set32 *set)
2251 int i = set->fd_count;
2253 TRACE(winsock, "(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2256 if (set->fd_array[i] == s) return 1;
2260 /***********************************************************************
2261 * WSAIsBlocking() (WINSOCK.114)(WSOCK32.114)
2263 BOOL32 WINAPI WSAIsBlocking(void)
2265 /* By default WinSock should set all its sockets to non-blocking mode
2266 * and poll in PeekMessage loop when processing "blocking" ones. This
2267 * function is supposed to tell if the program is in this loop. Our
2268 * blocking calls are truly blocking so we always return FALSE.
2270 * Note: It is allowed to call this function without prior WSAStartup().
2273 TRACE(winsock, "\n");
2277 /***********************************************************************
2278 * WSACancelBlockingCall() (WINSOCK.113)(WSOCK32.113)
2280 INT32 WINAPI WSACancelBlockingCall(void)
2282 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2284 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2286 if( pwsi ) return 0;
2287 return SOCKET_ERROR;
2291 /***********************************************************************
2292 * WSASetBlockingHook16() (WINSOCK.109)
2294 FARPROC16 WINAPI WSASetBlockingHook16(FARPROC16 lpBlockFunc)
2297 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2299 TRACE(winsock, "(%08x): hook %08x\n",
2300 (unsigned)pwsi, (unsigned) lpBlockFunc);
2303 prev = (FARPROC16)pwsi->blocking_hook;
2304 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2305 pwsi->flags &= ~WSI_BLOCKINGHOOK32;
2312 /***********************************************************************
2313 * WSASetBlockingHook32()
2315 FARPROC32 WINAPI WSASetBlockingHook32(FARPROC32 lpBlockFunc)
2318 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2320 TRACE(winsock, "(%08x): hook %08x\n",
2321 (unsigned)pwsi, (unsigned) lpBlockFunc);
2323 prev = (FARPROC32)pwsi->blocking_hook;
2324 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2325 pwsi->flags |= WSI_BLOCKINGHOOK32;
2332 /***********************************************************************
2333 * WSAUnhookBlockingHook16() (WINSOCK.110)
2335 INT16 WINAPI WSAUnhookBlockingHook16(void)
2337 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2339 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2340 if( pwsi ) return (INT16)(pwsi->blocking_hook = 0);
2341 return SOCKET_ERROR;
2345 /***********************************************************************
2346 * WSAUnhookBlockingHook32()
2348 INT32 WINAPI WSAUnhookBlockingHook32(void)
2350 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2352 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2355 pwsi->blocking_hook = 0;
2356 pwsi->flags &= ~WSI_BLOCKINGHOOK32;
2359 return SOCKET_ERROR;
2363 * TCP/IP action codes.
2367 #define WSCNTL_TCPIP_QUERY_INFO 0x00000000
2368 #define WSCNTL_TCPIP_SET_INFO 0x00000001
2369 #define WSCNTL_TCPIP_ICMP_ECHO 0x00000002
2370 #define WSCNTL_TCPIP_TEST 0x00000003
2373 /***********************************************************************
2376 * WsControl seems to be an undocumented Win95 function. A lot of
2377 * discussion about WsControl can be found on the net, e.g.
2378 * Subject: Re: WSOCK32.DLL WsControl Exported Function
2379 * From: "Peter Rindfuss" <rindfuss-s@medea.wz-berlin.de>
2383 DWORD WINAPI WsControl(DWORD protocoll,DWORD action,
2384 LPVOID inbuf,LPDWORD inbuflen,
2385 LPVOID outbuf,LPDWORD outbuflen)
2389 case WSCNTL_TCPIP_ICMP_ECHO:
2391 unsigned int addr = *(unsigned int*)inbuf;
2393 int timeout= *(unsigned int*)(inbuf+4);
2394 short x1 = *(unsigned short*)(inbuf+8);
2395 short sendbufsize = *(unsigned short*)(inbuf+10);
2396 char x2 = *(unsigned char*)(inbuf+12);
2397 char ttl = *(unsigned char*)(inbuf+13);
2398 char service = *(unsigned char*)(inbuf+14);
2399 char type= *(unsigned char*)(inbuf+15); /* 0x2: don't fragment*/
2402 FIXME(winsock,"(ICMP_ECHO) to 0x%08x stub \n", addr);
2406 FIXME(winsock,"(%lx,%lx,%p,%p,%p,%p) stub\n",
2407 protocoll,action,inbuf,inbuflen,outbuf,outbuflen);
2411 /*********************************************************
2412 * WS_s_perror WSOCK32.1108
2414 void WINAPI WS_s_perror(LPCSTR message)
2416 FIXME(winsock,"(%s): stub\n",message);
2421 /* ----------------------------------- end of API stuff */
2423 /* ----------------------------------- helper functions -
2425 * TODO: Merge WS_dup_..() stuff into one function that
2426 * would operate with a generic structure containing internal
2427 * pointers (via a template of some kind).
2430 static int list_size(char** l, int item_size)
2435 j += (item_size) ? item_size : strlen(l[i]) + 1;
2436 j += (i + 1) * sizeof(char*); }
2440 static int list_dup(char** l_src, char* ref, char* base, int item_size)
2442 /* base is either either equal to ref or 0 or SEGPTR */
2445 char** l_to = (char**)ref;
2448 for(j=0;l_src[j];j++) ;
2449 p += (j + 1) * sizeof(char*);
2451 { l_to[i] = base + (p - ref);
2452 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
2453 memcpy(p, l_src[i], k); p += k; }
2460 static int hostent_size(struct hostent* p_he)
2464 { size = sizeof(struct hostent);
2465 size += strlen(p_he->h_name) + 1;
2466 size += list_size(p_he->h_aliases, 0);
2467 size += list_size(p_he->h_addr_list, p_he->h_length ); }
2471 int WS_dup_he(LPWSINFO pwsi, struct hostent* p_he, int flag)
2473 /* Convert hostent structure into ws_hostent so that the data fits
2474 * into pwsi->buffer. Internal pointers can be linear, SEGPTR, or
2475 * relative to pwsi->buffer depending on "flag" value. Returns size
2476 * of the data copied (also in the pwsi->buflen).
2479 int size = hostent_size(p_he);
2483 struct ws_hostent* p_to;
2484 char* p_name,*p_aliases,*p_addr,*p_base,*p;
2486 _check_buffer_he(pwsi, size);
2487 p_to = (struct ws_hostent*)pwsi->he;
2488 p = (char*)pwsi->he;
2489 p_base = (flag & WS_DUP_OFFSET) ? NULL
2490 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2491 p += sizeof(struct ws_hostent);
2493 strcpy(p, p_he->h_name); p += strlen(p) + 1;
2495 p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)pwsi->he), 0);
2497 list_dup(p_he->h_addr_list, p, p_base + (p - (char*)pwsi->he), p_he->h_length);
2499 p_to->h_addrtype = (INT16)p_he->h_addrtype;
2500 p_to->h_length = (INT16)p_he->h_length;
2501 p_to->h_name = (SEGPTR)(p_base + (p_name - (char*)pwsi->he));
2502 p_to->h_aliases = (SEGPTR)(p_base + (p_aliases - (char*)pwsi->he));
2503 p_to->h_addr_list = (SEGPTR)(p_base + (p_addr - (char*)pwsi->he));
2505 size += (sizeof(struct ws_hostent) - sizeof(struct hostent));
2510 /* ----- protoent */
2512 static int protoent_size(struct protoent* p_pe)
2516 { size = sizeof(struct protoent);
2517 size += strlen(p_pe->p_name) + 1;
2518 size += list_size(p_pe->p_aliases, 0); }
2522 int WS_dup_pe(LPWSINFO pwsi, struct protoent* p_pe, int flag)
2524 int size = protoent_size(p_pe);
2527 struct ws_protoent* p_to;
2528 char* p_name,*p_aliases,*p_base,*p;
2530 _check_buffer_pe(pwsi, size);
2531 p_to = (struct ws_protoent*)pwsi->pe;
2532 p = (char*)pwsi->pe;
2533 p_base = (flag & WS_DUP_OFFSET) ? NULL
2534 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2535 p += sizeof(struct ws_protoent);
2537 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
2539 list_dup(p_pe->p_aliases, p, p_base + (p - (char*)pwsi->pe), 0);
2541 p_to->p_proto = (INT16)p_pe->p_proto;
2542 p_to->p_name = (SEGPTR)(p_base) + (p_name - (char*)pwsi->pe);
2543 p_to->p_aliases = (SEGPTR)((p_base) + (p_aliases - (char*)pwsi->pe));
2545 size += (sizeof(struct ws_protoent) - sizeof(struct protoent));
2552 static int servent_size(struct servent* p_se)
2556 { size += sizeof(struct servent);
2557 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
2558 size += list_size(p_se->s_aliases, 0); }
2562 int WS_dup_se(LPWSINFO pwsi, struct servent* p_se, int flag)
2564 int size = servent_size(p_se);
2567 struct ws_servent* p_to;
2568 char* p_name,*p_aliases,*p_proto,*p_base,*p;
2570 _check_buffer_se(pwsi, size);
2571 p_to = (struct ws_servent*)pwsi->se;
2572 p = (char*)pwsi->se;
2573 p_base = (flag & WS_DUP_OFFSET) ? NULL
2574 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2575 p += sizeof(struct ws_servent);
2577 strcpy(p, p_se->s_name); p += strlen(p) + 1;
2579 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
2581 list_dup(p_se->s_aliases, p, p_base + (p - (char*)pwsi->se), 0);
2583 p_to->s_port = (INT16)p_se->s_port;
2584 p_to->s_name = (SEGPTR)(p_base + (p_name - (char*)pwsi->se));
2585 p_to->s_proto = (SEGPTR)(p_base + (p_proto - (char*)pwsi->se));
2586 p_to->s_aliases = (SEGPTR)(p_base + (p_aliases - (char*)pwsi->se));
2588 size += (sizeof(struct ws_servent) - sizeof(struct servent));
2593 /* ----------------------------------- error handling */
2595 UINT16 wsaErrno(void)
2597 int loc_errno = errno;
2598 #ifdef HAVE_STRERROR
2599 WARN(winsock, "errno %d, (%s).\n", loc_errno, strerror(loc_errno));
2601 WARN(winsock, "errno %d\n", loc_errno);
2606 case EINTR: return WSAEINTR;
2607 case EBADF: return WSAEBADF;
2608 case EACCES: return WSAEACCES;
2609 case EFAULT: return WSAEFAULT;
2610 case EINVAL: return WSAEINVAL;
2611 case EMFILE: return WSAEMFILE;
2612 case EWOULDBLOCK: return WSAEWOULDBLOCK;
2613 case EINPROGRESS: return WSAEINPROGRESS;
2614 case EALREADY: return WSAEALREADY;
2615 case ENOTSOCK: return WSAENOTSOCK;
2616 case EDESTADDRREQ: return WSAEDESTADDRREQ;
2617 case EMSGSIZE: return WSAEMSGSIZE;
2618 case EPROTOTYPE: return WSAEPROTOTYPE;
2619 case ENOPROTOOPT: return WSAENOPROTOOPT;
2620 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
2621 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
2622 case EOPNOTSUPP: return WSAEOPNOTSUPP;
2623 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
2624 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
2625 case EADDRINUSE: return WSAEADDRINUSE;
2626 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
2627 case ENETDOWN: return WSAENETDOWN;
2628 case ENETUNREACH: return WSAENETUNREACH;
2629 case ENETRESET: return WSAENETRESET;
2630 case ECONNABORTED: return WSAECONNABORTED;
2632 case ECONNRESET: return WSAECONNRESET;
2633 case ENOBUFS: return WSAENOBUFS;
2634 case EISCONN: return WSAEISCONN;
2635 case ENOTCONN: return WSAENOTCONN;
2636 case ESHUTDOWN: return WSAESHUTDOWN;
2637 case ETOOMANYREFS: return WSAETOOMANYREFS;
2638 case ETIMEDOUT: return WSAETIMEDOUT;
2639 case ECONNREFUSED: return WSAECONNREFUSED;
2640 case ELOOP: return WSAELOOP;
2641 case ENAMETOOLONG: return WSAENAMETOOLONG;
2642 case EHOSTDOWN: return WSAEHOSTDOWN;
2643 case EHOSTUNREACH: return WSAEHOSTUNREACH;
2644 case ENOTEMPTY: return WSAENOTEMPTY;
2646 case EPROCLIM: return WSAEPROCLIM;
2649 case EUSERS: return WSAEUSERS;
2652 case EDQUOT: return WSAEDQUOT;
2655 case ESTALE: return WSAESTALE;
2658 case EREMOTE: return WSAEREMOTE;
2661 /* just in case we ever get here and there are no problems */
2664 WARN(winsock, "Unknown errno %d!\n", loc_errno);
2665 return WSAEOPNOTSUPP;
2669 UINT16 wsaHerrno(void)
2671 int loc_errno = h_errno;
2673 WARN(winsock, "h_errno %d.\n", loc_errno);
2677 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
2678 case TRY_AGAIN: return WSATRY_AGAIN;
2679 case NO_RECOVERY: return WSANO_RECOVERY;
2680 case NO_DATA: return WSANO_DATA;
2684 WARN(winsock,"Unknown h_errno %d!\n", loc_errno);
2685 return WSAEOPNOTSUPP;