Release 980315
[wine/multimedia.git] / misc / winsock.c
blobec78b4b0cd30ba92e8924a90f09f4478cfbbf806
1 /*
2 * based on Windows Sockets 1.1 specs
3 * (ftp.microsoft.com:/Advsys/winsock/spec11/WINSOCK.TXT)
4 *
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).
13 #include <stdio.h>
14 #include <string.h>
15 #include <signal.h>
16 #include <sys/types.h>
17 #include <sys/ipc.h>
18 #include <sys/ioctl.h>
19 #if defined(__svr4__)
20 #include <sys/filio.h>
21 #include <sys/ioccom.h>
22 #include <sys/sockio.h>
23 #endif
24 #if defined(__EMX__)
25 #include <sys/so_ioctl.h>
26 #include <sys/param.h>
27 #endif
28 #include <sys/msg.h>
29 #include <sys/wait.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <ctype.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <netdb.h>
37 #include <unistd.h>
39 #include "winsock.h"
40 #include "windows.h"
41 #include "winnt.h"
42 #include "heap.h"
43 #include "ldt.h"
44 #include "task.h"
45 #include "message.h"
46 #include "miscemu.h"
47 #include "debug.h"
49 #define DEBUG_SOCKADDR 0
50 #define dump_sockaddr(a) \
51 fprintf(stderr, "sockaddr_in: family %d, address %s, port %d\n", \
52 ((struct sockaddr_in *)a)->sin_family, \
53 inet_ntoa(((struct sockaddr_in *)a)->sin_addr), \
54 ntohs(((struct sockaddr_in *)a)->sin_port))
56 #pragma pack(4)
58 /* ----------------------------------- internal data */
60 extern int h_errno;
62 static HANDLE32 _WSHeap = 0;
63 static unsigned char* _ws_stub = NULL;
64 static LPWSINFO _wsi_list = NULL;
66 #define WS_ALLOC(size) \
67 HeapAlloc(_WSHeap, HEAP_ZERO_MEMORY, (size) )
68 #define WS_FREE(ptr) \
69 HeapFree(_WSHeap, 0, (ptr) )
71 #define WS_PTR2HANDLE(ptr) \
72 ((short)((int)(ptr) - (int)_ws_stub))
73 #define WS_HANDLE2PTR(handle) \
74 ((unsigned)((int)_ws_stub + (int)(handle)))
76 #define WSI_CHECK_RANGE(pwsi, pws) \
77 ( ((unsigned)(pws) > (unsigned)(pwsi)) && \
78 ((unsigned)(pws) < ((unsigned)(pwsi) + sizeof(WSINFO))) )
80 static INT32 _ws_sock_ops[] =
81 { WS_SO_DEBUG, WS_SO_REUSEADDR, WS_SO_KEEPALIVE, WS_SO_DONTROUTE,
82 WS_SO_BROADCAST, WS_SO_LINGER, WS_SO_OOBINLINE, WS_SO_SNDBUF,
83 WS_SO_RCVBUF, WS_SO_ERROR, WS_SO_TYPE, WS_SO_DONTLINGER, 0 };
84 static int _px_sock_ops[] =
85 { SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST,
86 SO_LINGER, SO_OOBINLINE, SO_SNDBUF, SO_RCVBUF, SO_ERROR, SO_TYPE,
87 SO_LINGER };
89 static int _check_ws(LPWSINFO pwsi, ws_socket* pws);
90 static char* _check_buffer(LPWSINFO pwsi, int size);
92 extern void EVENT_AddIO( int fd, unsigned flag );
93 extern void EVENT_DeleteIO( int fd, unsigned flag );
95 /***********************************************************************
96 * convert_sockopt()
98 * Converts socket flags from Windows format.
100 static void convert_sockopt(INT32 *level, INT32 *optname)
102 int i;
103 switch (*level)
105 case WS_SOL_SOCKET:
106 *level = SOL_SOCKET;
107 for(i=0; _ws_sock_ops[i]; i++)
108 if( _ws_sock_ops[i] == *optname ) break;
109 if( _ws_sock_ops[i] ) *optname = _px_sock_ops[i];
110 else fprintf(stderr, "convert_sockopt() unknown optname %d\n", *optname);
111 break;
112 case WS_IPPROTO_TCP:
113 *optname = IPPROTO_TCP;
117 /* ----------------------------------- Per-thread info (or per-process?) */
119 static LPWSINFO wsi_find(HTASK16 hTask)
121 TDB* pTask = (TDB*)GlobalLock16(hTask);
122 if( pTask )
124 if( pTask->pwsi ) return pTask->pwsi;
125 else
127 LPWSINFO pwsi = _wsi_list;
128 while( pwsi && pwsi->tid != hTask ) pwsi = pwsi->next;
129 if( pwsi )
130 fprintf(stderr,"loose wsi struct! pwsi=0x%08x, task=0x%04x\n",
131 (unsigned)pwsi, hTask );
132 return pwsi;
135 return NULL;
138 static ws_socket* wsi_alloc_socket(LPWSINFO pwsi, int fd)
140 /* Initialize a new entry in the socket table */
142 if( pwsi->last_free >= 0 )
144 int i = pwsi->last_free;
146 pwsi->last_free = pwsi->sock[i].flags; /* free list */
147 pwsi->sock[i].fd = fd;
148 pwsi->sock[i].flags = 0;
149 return &pwsi->sock[i];
151 return NULL;
154 static int wsi_strtolo(LPWSINFO pwsi, const char* name, const char* opt)
156 /* Stuff a lowercase copy of the string into the local buffer */
158 int i = strlen(name) + 2;
159 char* p = _check_buffer(pwsi, i + ((opt)?strlen(opt):0));
161 if( p )
163 do *p++ = tolower(*name); while(*name++);
164 i = (p - (char*)(pwsi->buffer));
165 if( opt ) do *p++ = tolower(*opt); while(*opt++);
166 return i;
168 return 0;
171 static fd_set* fd_set_import( fd_set* fds, LPWSINFO pwsi, void* wsfds, int* highfd, BOOL32 b32 )
173 /* translate Winsock fd set into local fd set */
175 if( wsfds )
177 #define wsfds16 ((ws_fd_set16*)wsfds)
178 #define wsfds32 ((ws_fd_set32*)wsfds)
179 ws_socket* pws;
180 int i, count;
182 FD_ZERO(fds);
183 count = b32 ? wsfds32->fd_count : wsfds16->fd_count;
185 for( i = 0; i < count; i++ )
187 pws = (b32) ? (ws_socket*)WS_HANDLE2PTR(wsfds32->fd_array[i])
188 : (ws_socket*)WS_HANDLE2PTR(wsfds16->fd_array[i]);
189 if( _check_ws(pwsi, pws) )
191 if( pws->fd > *highfd ) *highfd = pws->fd;
192 FD_SET(pws->fd, fds);
195 #undef wsfds32
196 #undef wsfds16
197 return fds;
199 return NULL;
202 __inline__ static int sock_error_p(int s)
204 unsigned int optval, optlen;
206 optlen = sizeof(optval);
207 getsockopt(s, SOL_SOCKET, SO_ERROR, &optval, &optlen);
208 if (optval) WARN(winsock, "\t[%i] error: %d\n", s, optval);
209 return optval != 0;
212 static int fd_set_export( LPWSINFO pwsi, fd_set* fds, fd_set* exceptfds, void* wsfds, BOOL32 b32 )
214 int num_err = 0;
216 /* translate local fd set into Winsock fd set, adding
217 * errors to exceptfds (only if app requested it) */
219 if( wsfds )
221 #define wsfds16 ((ws_fd_set16*)wsfds)
222 #define wsfds32 ((ws_fd_set32*)wsfds)
223 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
225 for( i = 0, j = 0; i < count; i++ )
227 ws_socket *pws = (b32) ? (ws_socket*)WS_HANDLE2PTR(wsfds32->fd_array[i])
228 : (ws_socket*)WS_HANDLE2PTR(wsfds16->fd_array[i]);
229 if( _check_ws(pwsi, pws) )
231 int fd = pws->fd;
233 if( FD_ISSET(fd, fds) )
235 if ( exceptfds && sock_error_p(fd) )
237 FD_SET(fd, exceptfds);
238 num_err++;
240 else if( b32 )
241 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
242 else
243 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
248 if( b32 ) wsfds32->fd_count = j;
249 else wsfds16->fd_count = j;
251 TRACE(winsock, "\n");
252 #undef wsfds32
253 #undef wsfds16
255 return num_err;
258 HANDLE16 __ws_gethandle( void* ptr )
260 return (HANDLE16)WS_PTR2HANDLE(ptr);
263 void* __ws_memalloc( int size )
265 return WS_ALLOC(size);
268 void __ws_memfree(void* ptr)
270 WS_FREE(ptr);
273 /* ----------------------------------- API -----
275 * Init / cleanup / error checking.
278 /***********************************************************************
279 * WSAStartup16() (WINSOCK.115)
281 * Create socket control struct, attach it to the global list and
282 * update a pointer in the task struct.
284 INT16 WINAPI WSAStartup16(UINT16 wVersionRequested, LPWSADATA lpWSAData)
286 WSADATA WINSOCK_data = { 0x0101, 0x0101,
287 "WINE Sockets 1.1",
288 #ifdef linux
289 "Linux/i386",
290 #elif defined(__NetBSD__)
291 "NetBSD/i386",
292 #elif defined(sunos)
293 "SunOS",
294 #elif defined(__FreeBSD__)
295 "FreeBSD",
296 #elif defined(__OpenBSD__)
297 "OpenBSD/i386",
298 #else
299 "Unknown",
300 #endif
301 WS_MAX_SOCKETS_PER_PROCESS,
302 WS_MAX_UDP_DATAGRAM, (SEGPTR)NULL };
303 HTASK16 tid = GetCurrentTask();
304 LPWSINFO pwsi;
306 TRACE(winsock, "verReq=%x\n", wVersionRequested);
308 if (LOBYTE(wVersionRequested) < 1 || (LOBYTE(wVersionRequested) == 1 &&
309 HIBYTE(wVersionRequested) < 1)) return WSAVERNOTSUPPORTED;
311 if (!lpWSAData) return WSAEINVAL;
313 /* initialize socket heap */
315 if( !_ws_stub )
317 _WSHeap = HeapCreate(HEAP_ZERO_MEMORY, 8120, 32768);
318 if( !(_ws_stub = WS_ALLOC(0x10)) )
320 fprintf(stderr,"Fatal: failed to create WinSock heap\n");
321 return 0;
324 if( _WSHeap == 0 ) return WSASYSNOTREADY;
326 /* create socket array for this task */
328 pwsi = wsi_find(GetCurrentTask());
329 if( pwsi == NULL )
331 TDB* pTask = (TDB*)GlobalLock16( tid );
333 if( (pwsi = (LPWSINFO)WS_ALLOC( sizeof(WSINFO))) )
335 int i = 0;
336 pwsi->tid = tid;
337 for( i = 0; i < WS_MAX_SOCKETS_PER_PROCESS; i++ )
339 pwsi->sock[i].fd = -1;
340 pwsi->sock[i].flags = i + 1;
342 pwsi->sock[WS_MAX_SOCKETS_PER_PROCESS - 1].flags = -1;
344 else return WSASYSNOTREADY;
346 /* add this control struct to the global list */
348 pwsi->prev = NULL;
349 if( _wsi_list )
350 _wsi_list->prev = pwsi;
351 pwsi->next = _wsi_list;
352 _wsi_list = pwsi;
353 pTask->pwsi = pwsi;
355 else pwsi->num_startup++;
357 /* return winsock information */
359 memcpy(lpWSAData, &WINSOCK_data, sizeof(WINSOCK_data));
361 TRACE(winsock, "succeeded\n");
362 return 0;
365 /***********************************************************************
366 * WSAStartup32() (WSOCK32.115)
368 INT32 WINAPI WSAStartup32(UINT32 wVersionRequested, LPWSADATA lpWSAData)
370 return WSAStartup16( wVersionRequested, lpWSAData );
373 /***********************************************************************
374 * WSACleanup() (WINSOCK.116)
376 * Cleanup functions of varying impact.
378 void WINSOCK_Shutdown()
380 /* Called on exit(), has to remove all outstanding async DNS processes. */
382 WINSOCK_cancel_task_aops( 0, __ws_memfree );
385 INT32 WINSOCK_DeleteTaskWSI( TDB* pTask, LPWSINFO pwsi )
387 /* WSACleanup() backend, called on task termination as well.
388 * Real DLL would have registered its own signal handler with
389 * TaskSetSignalHandler() and waited until USIG_TERMINATION/USIG_GPF
390 * but this scheme is much more straightforward.
393 int i, j, n;
395 if( --pwsi->num_startup > 0 ) return 0;
397 SIGNAL_MaskAsyncEvents( TRUE );
398 WINSOCK_cancel_task_aops( pTask->hSelf, __ws_memfree );
399 SIGNAL_MaskAsyncEvents( FALSE );
401 /* unlink socket control struct */
403 if( pwsi == _wsi_list )
404 _wsi_list = pwsi->next;
405 else
406 pwsi->prev->next = pwsi->next;
407 if( pwsi->next ) pwsi->next->prev = pwsi->prev;
409 if( _wsi_list == NULL )
410 WINSOCK_Shutdown(); /* just in case */
412 if( pwsi->flags & WSI_BLOCKINGCALL )
413 TRACE(winsock,"\tinside blocking call!\n");
415 /* FIXME: aop_control() doesn't decrement pwsi->num_async_rq
417 * if( pwsi->num_async_rq )
418 * WARN(winsock,"\thave %i outstanding async ops!\n", pwsi->num_async_rq );
421 for(i = 0, j = 0, n = 0; i < WS_MAX_SOCKETS_PER_PROCESS; i++)
422 if( pwsi->sock[i].fd != -1 )
424 if( pwsi->sock[i].psop )
426 n++;
427 WSAAsyncSelect32( (SOCKET16)WS_PTR2HANDLE(pwsi->sock + i), 0, 0, 0 );
429 close(pwsi->sock[i].fd); j++;
431 if( j )
432 TRACE(winsock,"\tclosed %i sockets, killed %i async selects!\n", j, n);
434 /* delete scratch buffers */
436 if( pwsi->buffer ) SEGPTR_FREE(pwsi->buffer);
437 if( pwsi->dbuffer ) SEGPTR_FREE(pwsi->dbuffer);
439 if( pTask )
440 pTask->pwsi = NULL;
441 memset( pwsi, 0, sizeof(WSINFO) );
442 WS_FREE(pwsi);
443 return 0;
446 INT32 WINAPI WSACleanup(void)
448 HTASK16 hTask = GetCurrentTask();
450 TRACE(winsock, "(%04x)\n", hTask );
451 if( hTask )
453 LPWSINFO pwsi = wsi_find(hTask);
454 if( pwsi )
455 return WINSOCK_DeleteTaskWSI( (TDB*)GlobalLock16(hTask), pwsi );
456 return SOCKET_ERROR;
458 else
459 WINSOCK_Shutdown(); /* remove all outstanding DNS requests */
460 return 0;
464 /***********************************************************************
465 * WSAGetLastError() (WSOCK32.111)(WINSOCK.111)
467 INT32 WINAPI WSAGetLastError(void)
469 LPWSINFO pwsi = wsi_find(GetCurrentTask());
470 INT16 ret = (pwsi) ? pwsi->err : WSANOTINITIALISED;
472 TRACE(winsock, "(%08x) = %i\n",
473 (unsigned)pwsi, (int)ret);
474 return ret;
477 /***********************************************************************
478 * WSASetLastError32() (WSOCK32.112)
480 void WINAPI WSASetLastError32(INT32 iError)
482 LPWSINFO pwsi = wsi_find(GetCurrentTask());
484 TRACE(winsock, "(%08x): %d\n", (unsigned)pwsi, (int)iError);
485 if( pwsi ) pwsi->err = iError;
488 /***********************************************************************
489 * WSASetLastError16() (WINSOCK.112)
491 void WINAPI WSASetLastError16(INT16 iError)
493 WSASetLastError32(iError);
496 int _check_ws(LPWSINFO pwsi, ws_socket* pws)
498 if( pwsi )
499 if( pwsi->flags & WSI_BLOCKINGCALL ) pwsi->err = WSAEINPROGRESS;
500 else if( WSI_CHECK_RANGE(pwsi, pws) ) return 1;
501 else pwsi->err = WSAENOTSOCK;
502 return 0;
505 char* _check_buffer(LPWSINFO pwsi, int size)
507 if( pwsi->buffer && pwsi->buflen >= size ) return pwsi->buffer;
508 else SEGPTR_FREE(pwsi->buffer);
510 pwsi->buffer = (char*)SEGPTR_ALLOC((pwsi->buflen = size));
511 return pwsi->buffer;
514 /* ----------------------------------- i/o APIs */
516 /***********************************************************************
517 * accept() (WSOCK32.1)
519 SOCKET32 WINAPI WINSOCK_accept32(SOCKET32 s, struct sockaddr *addr,
520 INT32 *addrlen32)
522 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR((SOCKET16)s);
523 LPWSINFO pwsi = wsi_find(GetCurrentTask());
525 TRACE(winsock, "(%08x): socket %04x\n",
526 (unsigned)pwsi, (UINT16)s );
527 if( _check_ws(pwsi, pws) )
529 int sock, fd_flags;
531 fd_flags = fcntl(pws->fd, F_GETFL, 0);
533 if( (sock = accept(pws->fd, addr, addrlen32)) >= 0 )
535 ws_socket* pnew = wsi_alloc_socket(pwsi, sock);
536 if( pnew )
538 s = (SOCKET32)WS_PTR2HANDLE(pnew);
539 if( pws->psop && pws->flags & WS_FD_ACCEPT )
541 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
543 /* async select the accept()'ed socket */
544 WSAAsyncSelect32( s, pws->psop->hWnd, pws->psop->uMsg,
545 pws->flags & ~WS_FD_ACCEPT );
547 return s;
549 else pwsi->err = WSAENOBUFS;
551 else pwsi->err = wsaErrno();
553 return INVALID_SOCKET32;
556 /***********************************************************************
557 * accept() (WINSOCK.1)
559 SOCKET16 WINAPI WINSOCK_accept16(SOCKET16 s, struct sockaddr* addr,
560 INT16* addrlen16 )
562 INT32 addrlen32 = addrlen16 ? *addrlen16 : 0;
563 SOCKET32 retSocket = WINSOCK_accept32( s, addr, &addrlen32 );
564 if( addrlen16 ) *addrlen16 = (INT16)addrlen32;
565 return (SOCKET16)retSocket;
568 /***********************************************************************
569 * bind() (WSOCK32.2)
571 INT32 WINAPI WINSOCK_bind32(SOCKET32 s, struct sockaddr *name, INT32 namelen)
573 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
574 LPWSINFO pwsi = wsi_find(GetCurrentTask());
576 TRACE(winsock, "(%08x): socket %04x, ptr %8x, length %d\n",
577 (unsigned)pwsi, s, (int) name, namelen);
578 #if DEBUG_SOCKADDR
579 dump_sockaddr(name);
580 #endif
582 if ( _check_ws(pwsi, pws) )
583 if ( namelen >= sizeof(*name) )
584 if ( ((struct ws_sockaddr_in *)name)->sin_family == AF_INET )
585 if ( bind(pws->fd, name, namelen) < 0 )
587 int loc_errno = errno;
588 WARN(winsock, "\tfailure - errno = %i\n", errno);
589 errno = loc_errno;
590 switch(errno)
592 case EBADF: pwsi->err = WSAENOTSOCK; break;
593 case EADDRNOTAVAIL: pwsi->err = WSAEINVAL; break;
594 default: pwsi->err = wsaErrno();
597 else return 0; /* success */
598 else pwsi->err = WSAEAFNOSUPPORT;
599 else pwsi->err = WSAEFAULT;
600 return SOCKET_ERROR;
603 /***********************************************************************
604 * bind() (WINSOCK.2)
606 INT16 WINAPI WINSOCK_bind16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
608 return (INT16)WINSOCK_bind32( s, name, namelen );
611 /***********************************************************************
612 * closesocket() (WSOCK32.3)
614 INT32 WINAPI WINSOCK_closesocket32(SOCKET32 s)
616 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
617 LPWSINFO pwsi = wsi_find(GetCurrentTask());
619 TRACE(winsock, "(%08x): socket %08x\n", (unsigned)pwsi, s);
621 if( _check_ws(pwsi, pws) )
623 int fd = pws->fd;
625 if( pws->psop ) WSAAsyncSelect32( s, 0, 0, 0 );
627 pws->fd = -1;
628 pws->flags = (unsigned)pwsi->last_free;
629 pwsi->last_free = pws - &pwsi->sock[0]; /* add to free list */
631 if( close(fd) == 0 )
632 return 0;
633 pwsi->err = (errno == EBADF) ? WSAENOTSOCK : wsaErrno();
635 return SOCKET_ERROR;
638 /***********************************************************************
639 * closesocket() (WINSOCK.3)
641 INT16 WINAPI WINSOCK_closesocket16(SOCKET16 s)
643 return (INT16)WINSOCK_closesocket32(s);
646 /***********************************************************************
647 * connect() (WSOCK32.4)
649 INT32 WINAPI WINSOCK_connect32(SOCKET32 s, struct sockaddr *name, INT32 namelen)
651 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
652 LPWSINFO pwsi = wsi_find(GetCurrentTask());
654 TRACE(winsock, "(%08x): socket %04x, ptr %8x, length %d\n",
655 (unsigned)pwsi, s, (int) name, namelen);
656 #if DEBUG_SOCKADDR
657 dump_sockaddr(name);
658 #endif
660 if( _check_ws(pwsi, pws) )
662 if (connect(pws->fd, name, namelen) == 0)
664 if( pws->psop && (pws->flags & WS_FD_CONNECT) )
666 /* application did AsyncSelect() but then went
667 * ahead and called connect() without waiting for
668 * notification.
670 * FIXME: Do we have to post a notification message
671 * in this case?
674 if( !(pws->flags & WS_FD_CONNECTED) )
676 if( pws->flags & (WS_FD_READ | WS_FD_CLOSE) )
677 EVENT_AddIO( pws->fd, EVENT_IO_READ );
678 else
679 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
680 if( pws->flags & WS_FD_WRITE )
681 EVENT_AddIO( pws->fd, EVENT_IO_WRITE );
682 else
683 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
686 pws->flags |= WS_FD_CONNECTED;
687 pws->flags &= ~(WS_FD_INACTIVE | WS_FD_CONNECT | WS_FD_LISTENING);
688 return 0;
690 pwsi->err = (errno == EINPROGRESS) ? WSAEWOULDBLOCK : wsaErrno();
692 return SOCKET_ERROR;
695 /***********************************************************************
696 * connect() (WINSOCK.4)
698 INT16 WINAPI WINSOCK_connect16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
700 return (INT16)WINSOCK_connect32( s, name, namelen );
703 /***********************************************************************
704 * getpeername() (WSOCK32.5)
706 INT32 WINAPI WINSOCK_getpeername32(SOCKET32 s, struct sockaddr *name,
707 INT32 *namelen)
709 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
710 LPWSINFO pwsi = wsi_find(GetCurrentTask());
712 TRACE(winsock, "(%08x): socket: %04x, ptr %8x, ptr %8x\n",
713 (unsigned)pwsi, s, (int) name, *namelen);
714 if( _check_ws(pwsi, pws) )
716 if (getpeername(pws->fd, name, namelen) == 0)
717 return 0;
718 pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
720 return SOCKET_ERROR;
723 /***********************************************************************
724 * getpeername() (WINSOCK.5)
726 INT16 WINAPI WINSOCK_getpeername16(SOCKET16 s, struct sockaddr *name,
727 INT16 *namelen16)
729 INT32 namelen32 = *namelen16;
730 INT32 retVal = WINSOCK_getpeername32( s, name, &namelen32 );
732 #if DEBUG_SOCKADDR
733 dump_sockaddr(name);
734 #endif
736 *namelen16 = namelen32;
737 return (INT16)retVal;
740 /***********************************************************************
741 * getsockname() (WSOCK32.6)
743 INT32 WINAPI WINSOCK_getsockname32(SOCKET32 s, struct sockaddr *name,
744 INT32 *namelen)
746 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
747 LPWSINFO pwsi = wsi_find(GetCurrentTask());
749 TRACE(winsock, "(%08x): socket: %04x, ptr %8x, ptr %8x\n",
750 (unsigned)pwsi, s, (int) name, (int) *namelen);
751 if( _check_ws(pwsi, pws) )
753 if (getsockname(pws->fd, name, namelen) == 0)
754 return 0;
755 pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
757 return SOCKET_ERROR;
760 /***********************************************************************
761 * getsockname() (WINSOCK.6)
763 INT16 WINAPI WINSOCK_getsockname16(SOCKET16 s, struct sockaddr *name,
764 INT16 *namelen16)
766 INT32 retVal;
768 if( namelen16 )
770 INT32 namelen32 = *namelen16;
771 retVal = WINSOCK_getsockname32( s, name, &namelen32 );
772 *namelen16 = namelen32;
774 #if DEBUG_SOCKADDR
775 dump_sockaddr(name);
776 #endif
779 else retVal = SOCKET_ERROR;
780 return (INT16)retVal;
784 /***********************************************************************
785 * getsockopt() (WSOCK32.7)
787 INT32 WINAPI WINSOCK_getsockopt32(SOCKET32 s, INT32 level,
788 INT32 optname, char *optval, INT32 *optlen)
790 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
791 LPWSINFO pwsi = wsi_find(GetCurrentTask());
793 TRACE(winsock, "(%08x): socket: %04x, opt %d, ptr %8x, ptr %8x\n",
794 (unsigned)pwsi, s, level, (int) optval, (int) *optlen);
795 if( _check_ws(pwsi, pws) )
797 convert_sockopt(&level, &optname);
798 if (getsockopt(pws->fd, (int) level, optname, optval, optlen) == 0 )
799 return 0;
800 pwsi->err = (errno == EBADF) ? WSAENOTSOCK : wsaErrno();
802 return SOCKET_ERROR;
805 /***********************************************************************
806 * getsockopt() (WINSOCK.7)
808 INT16 WINAPI WINSOCK_getsockopt16(SOCKET16 s, INT16 level,
809 INT16 optname, char *optval, INT16 *optlen)
811 INT32 optlen32;
812 INT32 *p = &optlen32;
813 INT32 retVal;
814 if( optlen ) optlen32 = *optlen; else p = NULL;
815 retVal = WINSOCK_getsockopt32( s, (UINT16)level, optname, optval, p );
816 if( optlen ) *optlen = optlen32;
817 return (INT16)retVal;
820 /***********************************************************************
821 * htonl() (WINSOCK.8)(WSOCK32.8)
823 u_long WINAPI WINSOCK_htonl(u_long hostlong) { return( htonl(hostlong) ); }
824 /***********************************************************************
825 * htons() (WINSOCK.9)(WSOCK32.9)
827 u_short WINAPI WINSOCK_htons(u_short hostshort) { return( htons(hostshort) ); }
828 /***********************************************************************
829 * inet_addr() (WINSOCK.10)(WSOCK32.10)
831 u_long WINAPI WINSOCK_inet_addr(char *cp) { return( inet_addr(cp) ); }
832 /***********************************************************************
833 * htohl() (WINSOCK.14)(WSOCK32.14)
835 u_long WINAPI WINSOCK_ntohl(u_long netlong) { return( ntohl(netlong) ); }
836 /***********************************************************************
837 * ntohs() (WINSOCK.15)(WSOCK32.15)
839 u_short WINAPI WINSOCK_ntohs(u_short netshort) { return( ntohs(netshort) ); }
841 /***********************************************************************
842 * inet_ntoa() (WINSOCK.11)(WSOCK32.11)
844 char* WINAPI WINSOCK_inet_ntoa32(struct in_addr in)
846 /* use "buffer for dummies" here because some applications have
847 * propensity to decode addresses in ws_hostent structure without
848 * saving them first...
851 LPWSINFO pwsi = wsi_find(GetCurrentTask());
853 if( pwsi )
855 char* s = inet_ntoa(in);
856 if( s )
858 if( pwsi->dbuffer == NULL )
859 if((pwsi->dbuffer = (char*) SEGPTR_ALLOC(32)) == NULL )
861 pwsi->err = WSAENOBUFS;
862 return NULL;
864 strncpy(pwsi->dbuffer, s, 32 );
865 return pwsi->dbuffer;
867 pwsi->err = wsaErrno();
869 return NULL;
872 SEGPTR WINAPI WINSOCK_inet_ntoa16(struct in_addr in)
874 char* retVal = WINSOCK_inet_ntoa32(in);
875 return retVal ? SEGPTR_GET(retVal) : (SEGPTR)NULL;
878 /***********************************************************************
879 * ioctlsocket() (WSOCK32.12)
881 INT32 WINAPI WINSOCK_ioctlsocket32(SOCKET32 s, UINT32 cmd, UINT32 *argp)
883 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
884 LPWSINFO pwsi = wsi_find(GetCurrentTask());
886 TRACE(winsock, "(%08x): socket %04x, cmd %08x, ptr %8x\n",
887 (unsigned)pwsi, s, cmd, (unsigned) argp);
888 if( _check_ws(pwsi, pws) )
890 long newcmd = cmd;
892 switch( cmd )
894 case WS_FIONREAD:
895 newcmd=FIONREAD;
896 break;
898 case WS_FIONBIO:
899 newcmd=FIONBIO;
900 if( pws->psop && *argp == 0 )
902 /* AsyncSelect()'ed sockets are always nonblocking */
903 pwsi->err = WSAEINVAL;
904 return SOCKET_ERROR;
906 break;
908 case WS_SIOCATMARK:
909 newcmd=SIOCATMARK;
910 break;
912 case WS_IOW('f',125,u_long):
913 fprintf(stderr,"Warning: WS1.1 shouldn't be using async I/O\n");
914 pwsi->err = WSAEINVAL;
915 return SOCKET_ERROR;
917 default:
918 /* Netscape tries hard to use bogus ioctl 0x667e */
919 WARN(winsock, "\tunknown WS_IOCTL cmd (%08x)\n", cmd);
921 if( ioctl(pws->fd, newcmd, (char*)argp ) == 0 ) return 0;
922 pwsi->err = (errno == EBADF) ? WSAENOTSOCK : wsaErrno();
924 return SOCKET_ERROR;
927 /***********************************************************************
928 * ioctlsocket() (WINSOCK.12)
930 INT16 WINAPI WINSOCK_ioctlsocket16(SOCKET16 s, UINT32 cmd, UINT32 *argp)
932 return (INT16)WINSOCK_ioctlsocket32( s, cmd, argp );
936 /***********************************************************************
937 * listen() (WSOCK32.13)
939 INT32 WINAPI WINSOCK_listen32(SOCKET32 s, INT32 backlog)
941 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
942 LPWSINFO pwsi = wsi_find(GetCurrentTask());
944 TRACE(winsock, "(%08x): socket %04x, backlog %d\n",
945 (unsigned)pwsi, s, backlog);
946 if( _check_ws(pwsi, pws) )
948 if (listen(pws->fd, backlog) == 0)
950 if( !pws->psop )
952 int fd_flags = fcntl(pws->fd, F_GETFL, 0);
953 if( !(fd_flags & O_NONBLOCK) ) pws->flags |= WS_FD_ACCEPT;
955 pws->flags |= WS_FD_LISTENING;
956 pws->flags &= ~(WS_FD_INACTIVE | WS_FD_CONNECT | WS_FD_CONNECTED); /* just in case */
957 return 0;
959 pwsi->err = wsaErrno();
961 else if( pwsi ) pwsi->err = WSAENOTSOCK;
962 return SOCKET_ERROR;
965 /***********************************************************************
966 * listen() (WINSOCK.13)
968 INT16 WINAPI WINSOCK_listen16(SOCKET16 s, INT16 backlog)
970 return (INT16)WINSOCK_listen32( s, backlog );
974 /***********************************************************************
975 * recv() (WSOCK32.16)
977 INT32 WINAPI WINSOCK_recv32(SOCKET32 s, char *buf, INT32 len, INT32 flags)
979 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
980 LPWSINFO pwsi = wsi_find(GetCurrentTask());
982 TRACE(winsock, "(%08x): socket %04x, buf %8x, len %d, "
983 "flags %d\n", (unsigned)pwsi, s, (unsigned)buf,
984 len, flags);
985 if( _check_ws(pwsi, pws) )
987 INT32 length;
988 if ((length = recv(pws->fd, buf, len, flags)) >= 0)
990 TRACE(winsock, " -> %i bytes\n", length);
992 if( pws->psop && (pws->flags & (WS_FD_READ | WS_FD_CLOSE)) )
993 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
995 return length;
997 pwsi->err = wsaErrno();
999 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1000 WARN(winsock, " -> ERROR\n");
1001 return SOCKET_ERROR;
1004 /***********************************************************************
1005 * recv() (WINSOCK.16)
1007 INT16 WINAPI WINSOCK_recv16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1009 return (INT16)WINSOCK_recv32( s, buf, len, flags );
1013 /***********************************************************************
1014 * recvfrom() (WSOCK32.17)
1016 INT32 WINAPI WINSOCK_recvfrom32(SOCKET32 s, char *buf, INT32 len, INT32 flags,
1017 struct sockaddr *from, INT32 *fromlen32)
1019 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1020 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1022 TRACE(winsock, "(%08x): socket %04x, ptr %08x, "
1023 "len %d, flags %d\n", (unsigned)pwsi, s, (unsigned)buf,
1024 len, flags);
1025 #if DEBUG_SOCKADDR
1026 if( from ) dump_sockaddr(from);
1027 else fprintf(stderr, "\tfrom = NULL\n");
1028 #endif
1030 if( _check_ws(pwsi, pws) )
1032 int length;
1034 if ((length = recvfrom(pws->fd, buf, len, flags, from, fromlen32)) >= 0)
1036 TRACE(winsock, " -> %i bytes\n", length);
1038 if( pws->psop && (pws->flags & (WS_FD_READ | WS_FD_CLOSE)) )
1039 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
1041 return (INT16)length;
1043 pwsi->err = wsaErrno();
1045 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1046 WARN(winsock, " -> ERROR\n");
1047 return SOCKET_ERROR;
1050 /***********************************************************************
1051 * recvfrom() (WINSOCK.17)
1053 INT16 WINAPI WINSOCK_recvfrom16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1054 struct sockaddr *from, INT16 *fromlen16)
1056 INT32 fromlen32;
1057 INT32 *p = &fromlen32;
1058 INT32 retVal;
1060 if( fromlen16 ) fromlen32 = *fromlen16; else p = NULL;
1061 retVal = WINSOCK_recvfrom32( s, buf, len, flags, from, p );
1062 if( fromlen16 ) *fromlen16 = fromlen32;
1063 return (INT16)retVal;
1066 /***********************************************************************
1067 * select() (WINSOCK.18)(WSOCK32.18)
1069 static INT32 __ws_select( BOOL32 b32, void *ws_readfds, void *ws_writefds, void *ws_exceptfds,
1070 struct timeval *timeout )
1072 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1074 TRACE(winsock, "(%08x): read %8x, write %8x, excp %8x\n",
1075 (unsigned) pwsi, (unsigned) ws_readfds, (unsigned) ws_writefds, (unsigned) ws_exceptfds);
1077 if( pwsi )
1079 int highfd = 0;
1080 fd_set readfds, writefds, exceptfds;
1081 fd_set *p_read, *p_write, *p_except;
1083 p_read = fd_set_import(&readfds, pwsi, ws_readfds, &highfd, b32);
1084 p_write = fd_set_import(&writefds, pwsi, ws_writefds, &highfd, b32);
1085 p_except = fd_set_import(&exceptfds, pwsi, ws_exceptfds, &highfd, b32);
1087 if( (highfd = select(highfd + 1, p_read, p_write, p_except, timeout)) > 0 )
1089 fd_set_export(pwsi, &readfds, p_except, ws_readfds, b32);
1090 fd_set_export(pwsi, &writefds, p_except, ws_writefds, b32);
1092 if (p_except && ws_exceptfds)
1094 #define wsfds16 ((ws_fd_set16*)ws_exceptfds)
1095 #define wsfds32 ((ws_fd_set32*)ws_exceptfds)
1096 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
1098 for (i = j = 0; i < count; i++)
1100 ws_socket *pws = (b32) ? (ws_socket *)WS_HANDLE2PTR(wsfds32->fd_array[i])
1101 : (ws_socket *)WS_HANDLE2PTR(wsfds16->fd_array[i]);
1102 if( _check_ws(pwsi, pws) && FD_ISSET(pws->fd, &exceptfds) )
1104 if( b32 )
1105 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
1106 else
1107 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
1110 if( b32 )
1111 wsfds32->fd_count = j;
1112 else
1113 wsfds16->fd_count = j;
1114 #undef wsfds32
1115 #undef wsfds16
1117 return highfd;
1119 if( ws_readfds ) ((ws_fd_set32*)ws_readfds)->fd_count = 0;
1120 if( ws_writefds ) ((ws_fd_set32*)ws_writefds)->fd_count = 0;
1121 if( ws_exceptfds ) ((ws_fd_set32*)ws_exceptfds)->fd_count = 0;
1123 if( highfd == 0 ) return 0;
1124 pwsi->err = wsaErrno();
1126 return SOCKET_ERROR;
1129 INT16 WINAPI WINSOCK_select16(INT16 nfds, ws_fd_set16 *ws_readfds,
1130 ws_fd_set16 *ws_writefds, ws_fd_set16 *ws_exceptfds,
1131 struct timeval *timeout)
1133 return (INT16)__ws_select( FALSE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1136 INT32 WINAPI WINSOCK_select32(INT32 nfds, ws_fd_set32 *ws_readfds,
1137 ws_fd_set32 *ws_writefds, ws_fd_set32 *ws_exceptfds,
1138 struct timeval *timeout)
1140 /* struct timeval is the same for both 32- and 16-bit code */
1141 return (INT32)__ws_select( TRUE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1145 /***********************************************************************
1146 * send() (WSOCK32.19)
1148 INT32 WINAPI WINSOCK_send32(SOCKET32 s, char *buf, INT32 len, INT32 flags)
1150 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1151 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1153 TRACE(winsock, "(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
1154 (unsigned)pwsi, s, (unsigned) buf, len, flags);
1155 if( _check_ws(pwsi, pws) )
1157 int length;
1159 if ((length = send(pws->fd, buf, len, flags)) < 0 )
1161 pwsi->err = wsaErrno();
1162 if( pwsi->err == WSAEWOULDBLOCK &&
1163 pws->psop && pws->flags & WS_FD_WRITE )
1164 EVENT_AddIO( pws->fd, EVENT_IO_WRITE ); /* reenabler */
1166 else return (INT16)length;
1168 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1169 return SOCKET_ERROR;
1172 /***********************************************************************
1173 * send() (WINSOCK.19)
1175 INT16 WINAPI WINSOCK_send16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1177 return WINSOCK_send32( s, buf, len, flags );
1180 /***********************************************************************
1181 * sendto() (WSOCK32.20)
1183 INT32 WINAPI WINSOCK_sendto32(SOCKET32 s, char *buf, INT32 len, INT32 flags,
1184 struct sockaddr *to, INT32 tolen)
1186 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1187 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1189 TRACE(winsock, "(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
1190 (unsigned)pwsi, s, (unsigned) buf, len, flags);
1191 if( _check_ws(pwsi, pws) )
1193 INT32 length;
1195 if ((length = sendto(pws->fd, buf, len, flags, to, tolen)) < 0 )
1197 pwsi->err = wsaErrno();
1198 if( pwsi->err == WSAEWOULDBLOCK &&
1199 pws->psop && pws->flags & WS_FD_WRITE )
1200 EVENT_AddIO( pws->fd, EVENT_IO_WRITE ); /* reenabler */
1202 else return length;
1204 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1205 return SOCKET_ERROR;
1208 /***********************************************************************
1209 * sendto() (WINSOCK.20)
1211 INT16 WINAPI WINSOCK_sendto16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1212 struct sockaddr *to, INT16 tolen)
1214 return (INT16)WINSOCK_sendto32( s, buf, len, flags, to, tolen );
1217 /***********************************************************************
1218 * setsockopt() (WSOCK32.21)
1220 INT32 WINAPI WINSOCK_setsockopt32(SOCKET16 s, INT32 level, INT32 optname,
1221 char *optval, INT32 optlen)
1223 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1224 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1226 TRACE(winsock, "(%08x): socket %04x, lev %d, opt %d, ptr %08x, len %d\n",
1227 (unsigned)pwsi, s, level, optname, (int) optval, optlen);
1228 if( _check_ws(pwsi, pws) )
1230 convert_sockopt(&level, &optname);
1231 if (setsockopt(pws->fd, level, optname, optval, optlen) == 0) return 0;
1232 pwsi->err = wsaErrno();
1234 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1235 return SOCKET_ERROR;
1238 /***********************************************************************
1239 * setsockopt() (WINSOCK.21)
1241 INT16 WINAPI WINSOCK_setsockopt16(SOCKET16 s, INT16 level, INT16 optname,
1242 char *optval, INT16 optlen)
1244 INT32 linger32[2];
1245 if( !optval ) return SOCKET_ERROR;
1246 if( optname == SO_LINGER )
1248 INT16* ptr = (INT16*)optval;
1249 linger32[0] = ptr[0];
1250 linger32[1] = ptr[1];
1251 optval = (char*)&linger32;
1252 optlen = sizeof(linger32);
1254 return (INT16)WINSOCK_setsockopt32( s, (UINT16)level, optname, optval, optlen );
1258 /***********************************************************************
1259 * shutdown() (WSOCK32.22)
1261 INT32 WINAPI WINSOCK_shutdown32(SOCKET32 s, INT32 how)
1263 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1264 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1266 TRACE(winsock, "(%08x): socket %04x, how %i\n",
1267 (unsigned)pwsi, s, how );
1268 if( _check_ws(pwsi, pws) )
1270 if( pws->psop )
1271 switch( how )
1273 case 0: /* drop receives */
1274 if( pws->flags & (WS_FD_READ | WS_FD_CLOSE) )
1275 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
1276 pws->flags &= ~(WS_FD_READ | WS_FD_CLOSE);
1277 #ifdef SHUT_RD
1278 how = SHUT_RD;
1279 #endif
1280 break;
1282 case 1: /* drop sends */
1283 if( pws->flags & WS_FD_WRITE )
1284 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
1285 pws->flags &= ~WS_FD_WRITE;
1286 #ifdef SHUT_WR
1287 how = SHUT_WR;
1288 #endif
1289 break;
1291 case 2: /* drop all */
1292 #ifdef SHUT_RDWR
1293 how = SHUT_RDWR;
1294 #endif
1295 default:
1296 WSAAsyncSelect32( s, 0, 0, 0 );
1297 break;
1300 if (shutdown(pws->fd, how) == 0)
1302 if( how > 1 )
1304 pws->flags &= ~(WS_FD_CONNECTED | WS_FD_LISTENING);
1305 pws->flags |= WS_FD_INACTIVE;
1307 return 0;
1309 pwsi->err = wsaErrno();
1311 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1312 return SOCKET_ERROR;
1315 /***********************************************************************
1316 * shutdown() (WINSOCK.22)
1318 INT16 WINAPI WINSOCK_shutdown16(SOCKET16 s, INT16 how)
1320 return (INT16)WINSOCK_shutdown32( s, how );
1324 /***********************************************************************
1325 * socket() (WSOCK32.23)
1327 SOCKET32 WINAPI WINSOCK_socket32(INT32 af, INT32 type, INT32 protocol)
1329 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1331 TRACE(winsock, "(%08x): af=%d type=%d protocol=%d\n",
1332 (unsigned)pwsi, af, type, protocol);
1334 if( pwsi )
1336 int sock;
1338 /* check the socket family */
1339 switch(af)
1341 case AF_INET:
1342 case AF_UNSPEC: break;
1343 default: pwsi->err = WSAEAFNOSUPPORT;
1344 return INVALID_SOCKET32;
1347 /* check the socket type */
1348 switch(type)
1350 case SOCK_STREAM:
1351 case SOCK_DGRAM:
1352 case SOCK_RAW: break;
1353 default: pwsi->err = WSAESOCKTNOSUPPORT;
1354 return INVALID_SOCKET32;
1357 /* check the protocol type */
1358 if ( protocol < 0 ) /* don't support negative values */
1359 { pwsi->err = WSAEPROTONOSUPPORT; return INVALID_SOCKET32; }
1361 if ( af == AF_UNSPEC) /* did they not specify the address family? */
1362 switch(protocol)
1364 case IPPROTO_TCP:
1365 if (type == SOCK_STREAM) { af = AF_INET; break; }
1366 case IPPROTO_UDP:
1367 if (type == SOCK_DGRAM) { af = AF_INET; break; }
1368 default: pwsi->err = WSAEPROTOTYPE; return INVALID_SOCKET32;
1371 if ((sock = socket(af, type, protocol)) >= 0)
1373 ws_socket* pnew = wsi_alloc_socket(pwsi, sock);
1375 TRACE(winsock,"\tcreated %i (handle %04x)\n", sock, (UINT16)WS_PTR2HANDLE(pnew));
1377 if( pnew )
1379 pnew->flags |= WS_FD_INACTIVE;
1380 return (SOCKET16)WS_PTR2HANDLE(pnew);
1383 close(sock);
1384 pwsi->err = WSAENOBUFS;
1385 return INVALID_SOCKET32;
1388 if (errno == EPERM) /* raw socket denied */
1390 fprintf(stderr, "WS_SOCKET: not enough privileges\n");
1391 pwsi->err = WSAESOCKTNOSUPPORT;
1392 } else pwsi->err = wsaErrno();
1395 WARN(winsock, "\t\tfailed!\n");
1396 return INVALID_SOCKET32;
1399 /***********************************************************************
1400 * socket() (WINSOCK.23)
1402 SOCKET16 WINAPI WINSOCK_socket16(INT16 af, INT16 type, INT16 protocol)
1404 return (SOCKET16)WINSOCK_socket32( af, type, protocol );
1408 /* ----------------------------------- DNS services
1410 * IMPORTANT: 16-bit API structures have SEGPTR pointers inside them.
1411 * Also, we have to use wsock32 stubs to convert structures and
1412 * error codes from Unix to WSA, hence there is no direct mapping in
1413 * the relay32/wsock32.spec.
1416 static char* NULL_STRING = "NULL";
1418 /***********************************************************************
1419 * gethostbyaddr() (WINSOCK.51)(WSOCK32.51)
1421 static struct WIN_hostent* __ws_gethostbyaddr(const char *addr, int len, int type, int dup_flag)
1423 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1425 if( pwsi )
1427 struct hostent* host;
1428 if( (host = gethostbyaddr(addr, len, type)) != NULL )
1429 if( WS_dup_he(pwsi, host, dup_flag) )
1430 return (struct WIN_hostent*)(pwsi->buffer);
1431 else
1432 pwsi->err = WSAENOBUFS;
1433 else
1434 pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1436 return NULL;
1439 SEGPTR WINAPI WINSOCK_gethostbyaddr16(const char *addr, INT16 len, INT16 type)
1441 struct WIN_hostent* retval;
1442 TRACE(winsock, "ptr %08x, len %d, type %d\n",
1443 (unsigned) addr, len, type);
1444 retval = __ws_gethostbyaddr( addr, len, type, WS_DUP_SEGPTR );
1445 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1448 struct WIN_hostent* WINAPI WINSOCK_gethostbyaddr32(const char *addr, INT32 len,
1449 INT32 type)
1451 TRACE(winsock, "ptr %08x, len %d, type %d\n",
1452 (unsigned) addr, len, type);
1453 return __ws_gethostbyaddr(addr, len, type, WS_DUP_LINEAR);
1456 /***********************************************************************
1457 * gethostbyname() (WINSOCK.52)(WSOCK32.52)
1459 static struct WIN_hostent * __ws_gethostbyname(const char *name, int dup_flag)
1461 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1463 if( pwsi )
1465 struct hostent* host;
1466 if( (host = gethostbyname(name)) != NULL )
1467 if( WS_dup_he(pwsi, host, dup_flag) )
1468 return (struct WIN_hostent*)(pwsi->buffer);
1469 else pwsi->err = WSAENOBUFS;
1470 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1472 return NULL;
1475 SEGPTR WINAPI WINSOCK_gethostbyname16(const char *name)
1477 struct WIN_hostent* retval;
1478 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1479 retval = __ws_gethostbyname( name, WS_DUP_SEGPTR );
1480 return (retval)? SEGPTR_GET(retval) : ((SEGPTR)NULL) ;
1483 struct WIN_hostent* WINAPI WINSOCK_gethostbyname32(const char* name)
1485 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1486 return __ws_gethostbyname( name, WS_DUP_LINEAR );
1490 /***********************************************************************
1491 * getprotobyname() (WINSOCK.53)(WSOCK32.53)
1493 static struct WIN_protoent* __ws_getprotobyname(const char *name, int dup_flag)
1495 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1497 if( pwsi )
1499 struct protoent* proto;
1500 if( (proto = getprotobyname(name)) != NULL )
1501 if( WS_dup_pe(pwsi, proto, dup_flag) )
1502 return (struct WIN_protoent*)(pwsi->buffer);
1503 else pwsi->err = WSAENOBUFS;
1504 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1506 return NULL;
1509 SEGPTR WINAPI WINSOCK_getprotobyname16(const char *name)
1511 struct WIN_protoent* retval;
1512 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1513 retval = __ws_getprotobyname(name, WS_DUP_SEGPTR);
1514 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1517 struct WIN_protoent* WINAPI WINSOCK_getprotobyname32(const char* name)
1519 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1520 return __ws_getprotobyname(name, WS_DUP_LINEAR);
1524 /***********************************************************************
1525 * getprotobynumber() (WINSOCK.54)(WSOCK32.54)
1527 static struct WIN_protoent* __ws_getprotobynumber(int number, int dup_flag)
1529 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1531 if( pwsi )
1533 struct protoent* proto;
1534 if( (proto = getprotobynumber(number)) != NULL )
1535 if( WS_dup_pe(pwsi, proto, dup_flag) )
1536 return (struct WIN_protoent*)(pwsi->buffer);
1537 else pwsi->err = WSAENOBUFS;
1538 else pwsi->err = WSANO_DATA;
1540 return NULL;
1543 SEGPTR WINAPI WINSOCK_getprotobynumber16(INT16 number)
1545 struct WIN_protoent* retval;
1546 TRACE(winsock, "%i\n", number);
1547 retval = __ws_getprotobynumber(number, WS_DUP_SEGPTR);
1548 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1551 struct WIN_protoent* WINAPI WINSOCK_getprotobynumber32(INT32 number)
1553 TRACE(winsock, "%i\n", number);
1554 return __ws_getprotobynumber(number, WS_DUP_LINEAR);
1558 /***********************************************************************
1559 * getservbyname() (WINSOCK.55)(WSOCK32.55)
1561 struct WIN_servent* __ws_getservbyname(const char *name, const char *proto, int dup_flag)
1563 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1565 if( pwsi )
1567 struct servent* serv;
1568 int i = wsi_strtolo( pwsi, name, proto );
1570 if( i )
1571 if( (serv = getservbyname(pwsi->buffer, pwsi->buffer + i)) != NULL )
1572 if( WS_dup_se(pwsi, serv, dup_flag) )
1573 return (struct WIN_servent*)(pwsi->buffer);
1574 else pwsi->err = WSAENOBUFS;
1575 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1576 else pwsi->err = WSAENOBUFS;
1578 return NULL;
1581 SEGPTR WINAPI WINSOCK_getservbyname16(const char *name, const char *proto)
1583 struct WIN_servent* retval;
1584 TRACE(winsock, "'%s', '%s'\n",
1585 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
1586 retval = __ws_getservbyname(name, proto, WS_DUP_SEGPTR);
1587 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1590 struct WIN_servent* WINAPI WINSOCK_getservbyname32(const char *name, const char *proto)
1592 TRACE(winsock, "'%s', '%s'\n",
1593 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
1594 return __ws_getservbyname(name, proto, WS_DUP_LINEAR);
1598 /***********************************************************************
1599 * getservbyport() (WINSOCK.56)(WSOCK32.56)
1601 static struct WIN_servent* __ws_getservbyport(int port, const char* proto, int dup_flag)
1603 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1605 if( pwsi )
1607 struct servent* serv;
1608 int i = wsi_strtolo( pwsi, proto, NULL );
1610 if( i )
1611 if( (serv = getservbyport(port, pwsi->buffer)) != NULL )
1612 if( WS_dup_se(pwsi, serv, dup_flag) )
1613 return (struct WIN_servent*)(pwsi->buffer);
1614 else pwsi->err = WSAENOBUFS;
1615 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1616 else pwsi->err = WSAENOBUFS;
1618 return NULL;
1621 SEGPTR WINAPI WINSOCK_getservbyport16(INT16 port, const char *proto)
1623 struct WIN_servent* retval;
1624 TRACE(winsock, "%i, '%s'\n",
1625 (int)port, (proto)?proto:NULL_STRING);
1626 retval = __ws_getservbyport(port, proto, WS_DUP_SEGPTR);
1627 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1630 struct WIN_servent* WINAPI WINSOCK_getservbyport32(INT32 port, const char *proto)
1632 TRACE(winsock, "%i, '%s'\n",
1633 (int)port, (proto)?proto:NULL_STRING);
1634 return __ws_getservbyport(port, proto, WS_DUP_LINEAR);
1638 /***********************************************************************
1639 * gethostname() (WSOCK32.57)
1641 INT32 WINAPI WINSOCK_gethostname32(char *name, INT32 namelen)
1643 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1645 TRACE(winsock, "(%08x): name %s, len %d\n",
1646 (unsigned)pwsi, (name)?name:NULL_STRING, namelen);
1647 if( pwsi )
1649 if (gethostname(name, namelen) == 0) return 0;
1650 pwsi->err = (errno == EINVAL) ? WSAEFAULT : wsaErrno();
1652 return SOCKET_ERROR;
1655 /***********************************************************************
1656 * gethostname() (WINSOCK.57)
1658 INT16 WINAPI WINSOCK_gethostname16(char *name, INT16 namelen)
1660 return (INT16)WINSOCK_gethostname32(name, namelen);
1664 /* ------------------------------------- Windows sockets extensions -- *
1666 * ------------------------------------------------------------------- */
1669 /***********************************************************************
1670 * Asynchronous DNS services
1673 /* winsock_dns.c */
1674 extern HANDLE16 __WSAsyncDBQuery(LPWSINFO pwsi, HWND32 hWnd, UINT32 uMsg, INT32 type, LPCSTR init,
1675 INT32 len, LPCSTR proto, void* sbuf, INT32 buflen, UINT32 flag);
1677 /***********************************************************************
1678 * WSAAsyncGetHostByAddr() (WINSOCK.102)
1680 HANDLE16 WINAPI WSAAsyncGetHostByAddr16(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,
1681 INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen)
1683 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1685 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, addr %08x[%i]\n",
1686 (unsigned)pwsi, hWnd, uMsg, (unsigned)addr , len );
1688 if( pwsi )
1689 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, type, addr, len,
1690 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYADDR );
1691 return 0;
1694 /***********************************************************************
1695 * WSAAsyncGetHostByAddr() (WSOCK32.102)
1697 HANDLE32 WINAPI WSAAsyncGetHostByAddr32(HWND32 hWnd, UINT32 uMsg, LPCSTR addr,
1698 INT32 len, INT32 type, LPSTR sbuf, INT32 buflen)
1700 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1702 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, addr %08x[%i]\n",
1703 (unsigned)pwsi, (HWND16)hWnd, uMsg, (unsigned)addr , len );
1705 if( pwsi )
1706 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, type, addr, len,
1707 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYADDR | WSMSG_ASYNC_WIN32);
1708 return 0;
1712 /***********************************************************************
1713 * WSAAsyncGetHostByName() (WINSOCK.103)
1715 HANDLE16 WINAPI WSAAsyncGetHostByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
1716 SEGPTR sbuf, INT16 buflen)
1718 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1720 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, host %s,
1721 buffer %i\n", (unsigned)pwsi, hWnd, uMsg, (name)?name:NULL_STRING, (int)buflen );
1723 if( pwsi )
1724 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
1725 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYNAME );
1726 return 0;
1729 /***********************************************************************
1730 * WSAAsyncGetHostByName32() (WSOCK32.103)
1732 HANDLE32 WINAPI WSAAsyncGetHostByName32(HWND32 hWnd, UINT32 uMsg, LPCSTR name,
1733 LPSTR sbuf, INT32 buflen)
1735 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1736 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, host %s, buffer %i\n",
1737 (unsigned)pwsi, (HWND16)hWnd, uMsg,
1738 (name)?name:NULL_STRING, (int)buflen );
1739 if( pwsi )
1740 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
1741 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYNAME | WSMSG_ASYNC_WIN32);
1742 return 0;
1746 /***********************************************************************
1747 * WSAAsyncGetProtoByName() (WINSOCK.105)
1749 HANDLE16 WINAPI WSAAsyncGetProtoByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
1750 SEGPTR sbuf, INT16 buflen)
1752 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1754 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, protocol %s\n",
1755 (unsigned)pwsi, (HWND16)hWnd, uMsg, (name)?name:NULL_STRING );
1757 if( pwsi )
1758 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
1759 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNAME );
1760 return 0;
1763 /***********************************************************************
1764 * WSAAsyncGetProtoByName() (WSOCK32.105)
1766 HANDLE32 WINAPI WSAAsyncGetProtoByName32(HWND32 hWnd, UINT32 uMsg, LPCSTR name,
1767 LPSTR sbuf, INT32 buflen)
1769 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1771 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, protocol %s\n",
1772 (unsigned)pwsi, (HWND16)hWnd, uMsg, (name)?name:NULL_STRING );
1774 if( pwsi )
1775 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
1776 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNAME | WSMSG_ASYNC_WIN32);
1777 return 0;
1781 /***********************************************************************
1782 * WSAAsyncGetProtoByNumber() (WINSOCK.104)
1784 HANDLE16 WINAPI WSAAsyncGetProtoByNumber16(HWND16 hWnd, UINT16 uMsg, INT16 number,
1785 SEGPTR sbuf, INT16 buflen)
1787 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1789 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, num %i\n",
1790 (unsigned)pwsi, hWnd, uMsg, number );
1792 if( pwsi )
1793 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, number, NULL, 0,
1794 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNUM );
1795 return 0;
1798 /***********************************************************************
1799 * WSAAsyncGetProtoByNumber() (WSOCK32.104)
1801 HANDLE32 WINAPI WSAAsyncGetProtoByNumber32(HWND32 hWnd, UINT32 uMsg, INT32 number,
1802 LPSTR sbuf, INT32 buflen)
1804 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1806 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, num %i\n",
1807 (unsigned)pwsi, (HWND16)hWnd, uMsg, number );
1809 if( pwsi )
1810 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, number, NULL, 0,
1811 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNUM | WSMSG_ASYNC_WIN32);
1812 return 0;
1816 /***********************************************************************
1817 * WSAAsyncGetServByName() (WINSOCK.107)
1819 HANDLE16 WINAPI WSAAsyncGetServByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
1820 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
1822 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1824 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, name %s, proto %s\n",
1825 (unsigned)pwsi, hWnd, uMsg,
1826 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING );
1828 if( pwsi )
1830 int i = wsi_strtolo( pwsi, name, proto );
1832 if( i )
1833 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, pwsi->buffer, 0,
1834 pwsi->buffer + i, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYNAME );
1836 return 0;
1839 /***********************************************************************
1840 * WSAAsyncGetServByName() (WSOCK32.107)
1842 HANDLE32 WINAPI WSAAsyncGetServByName32(HWND32 hWnd, UINT32 uMsg, LPCSTR name,
1843 LPCSTR proto, LPSTR sbuf, INT32 buflen)
1845 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1847 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, name %s, proto %s\n",
1848 (unsigned)pwsi, (HWND16)hWnd, uMsg,
1849 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING );
1850 if( pwsi )
1852 int i = wsi_strtolo( pwsi, name, proto );
1854 if( i )
1855 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, pwsi->buffer, 0,
1856 pwsi->buffer + i, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYNAME | WSMSG_ASYNC_WIN32);
1858 return 0;
1862 /***********************************************************************
1863 * WSAAsyncGetServByPort() (WINSOCK.106)
1865 HANDLE16 WINAPI WSAAsyncGetServByPort16(HWND16 hWnd, UINT16 uMsg, INT16 port,
1866 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
1868 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1870 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, port %i, proto %s\n",
1871 (unsigned)pwsi, hWnd, uMsg, port, (proto)?proto:NULL_STRING );
1873 if( pwsi )
1875 int i = wsi_strtolo( pwsi, proto, NULL );
1877 if( i )
1878 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, port, pwsi->buffer, 0,
1879 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYPORT );
1881 return 0;
1884 /***********************************************************************
1885 * WSAAsyncGetServByPort() (WSOCK32.106)
1887 HANDLE32 WINAPI WSAAsyncGetServByPort32(HWND32 hWnd, UINT32 uMsg, INT32 port,
1888 LPCSTR proto, LPSTR sbuf, INT32 buflen)
1890 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1892 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, port %i, proto %s\n",
1893 (unsigned)pwsi, (HWND16)hWnd, uMsg, port, (proto)?proto:NULL_STRING );
1895 if( pwsi )
1897 int i = wsi_strtolo( pwsi, proto, NULL );
1899 if( i )
1900 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, port, pwsi->buffer, 0,
1901 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYPORT | WSMSG_ASYNC_WIN32);
1903 return 0;
1907 /***********************************************************************
1908 * WSACancelAsyncRequest() (WINSOCK.108)(WSOCK32.109)
1910 INT32 WINAPI WSACancelAsyncRequest32(HANDLE32 hAsyncTaskHandle)
1912 INT32 retVal = SOCKET_ERROR;
1913 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1914 ws_async_op* p_aop = (ws_async_op*)WS_HANDLE2PTR(hAsyncTaskHandle);
1916 TRACE(winsock, "(%08x): handle %08x\n",
1917 (unsigned)pwsi, hAsyncTaskHandle);
1918 if( pwsi )
1920 SIGNAL_MaskAsyncEvents( TRUE ); /* block SIGIO */
1921 if( WINSOCK_cancel_async_op(p_aop) )
1923 WS_FREE(p_aop);
1924 pwsi->num_async_rq--;
1925 retVal = 0;
1927 else pwsi->err = WSAEINVAL;
1928 SIGNAL_MaskAsyncEvents( FALSE );
1930 return retVal;
1933 INT16 WINAPI WSACancelAsyncRequest16(HANDLE16 hAsyncTaskHandle)
1935 return (HANDLE16)WSACancelAsyncRequest16((HANDLE32)hAsyncTaskHandle);
1938 /***********************************************************************
1939 * WSAAsyncSelect() (WINSOCK.101)(WSOCK32.101)
1942 static ws_select_op* __ws_select_list = NULL;
1944 BOOL32 WINSOCK_HandleIO( int* max_fd, int num_pending,
1945 fd_set pending_set[3], fd_set event_set[3] )
1947 /* This function is called by the event dispatcher
1948 * with the pending_set[] containing the result of select() and
1949 * the event_set[] containing all fd that are being watched */
1951 ws_select_op* psop = __ws_select_list;
1952 BOOL32 bPost = FALSE;
1953 DWORD dwEvent, dwErrBytes;
1954 int num_posted;
1956 TRACE(winsock,"%i pending descriptors\n", num_pending );
1958 for( num_posted = dwEvent = 0 ; psop; psop = psop->next )
1960 unsigned flags = psop->pws->flags;
1961 int fd = psop->pws->fd;
1962 int r, w, e;
1964 w = 0;
1965 if( (r = FD_ISSET( fd, &pending_set[EVENT_IO_READ] )) ||
1966 (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] )) ||
1967 (e = FD_ISSET( fd, &pending_set[EVENT_IO_EXCEPT] )) )
1969 /* This code removes WS_FD flags on one-shot events (WS_FD_CLOSE,
1970 * WS_FD_CONNECT), otherwise it clears descriptors in the io_set.
1971 * Reenabling calls turn them back on.
1974 TRACE(winsock,"\tchecking psop = 0x%08x\n", (unsigned) psop );
1976 num_pending--;
1978 /* Now figure out what kind of event we've got. The worst problem
1979 * we have to contend with is that some out of control applications
1980 * really want to use mutually exclusive AsyncSelect() flags all at
1981 * the same time.
1984 if((flags & WS_FD_ACCEPT) && (flags & WS_FD_LISTENING))
1986 /* WS_FD_ACCEPT is valid only if the socket is in the
1987 * listening state */
1989 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
1990 if( r )
1992 FD_CLR( fd, &event_set[EVENT_IO_READ] ); /* reenabled by the next accept() */
1993 dwEvent = WSAMAKESELECTREPLY( WS_FD_ACCEPT, 0 );
1994 bPost = TRUE;
1996 else continue;
1998 else if( flags & WS_FD_CONNECT )
2000 /* connecting socket */
2002 if( w || (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] )) )
2004 /* ready to write means that socket is connected
2006 * FIXME: Netscape calls AsyncSelect( s, ... WS_FD_CONNECT .. )
2007 * right after s = socket() and somehow "s" becomes writeable
2008 * before it goes through connect()!?!?
2011 psop->pws->flags |= WS_FD_CONNECTED;
2012 psop->pws->flags &= ~(WS_FD_CONNECT | WS_FD_INACTIVE);
2013 dwEvent = WSAMAKESELECTREPLY( WS_FD_CONNECT, 0 );
2015 if( flags & (WS_FD_READ | WS_FD_CLOSE))
2016 FD_SET( fd, &event_set[EVENT_IO_READ] );
2017 else
2018 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2019 if( flags & WS_FD_WRITE )
2020 FD_SET( fd, &event_set[EVENT_IO_WRITE] );
2021 else
2022 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2023 bPost = TRUE;
2025 else if( r )
2027 /* failure - do read() to get correct errno */
2029 if( read( fd, &dwErrBytes, sizeof(dwErrBytes) ) == -1 )
2031 dwEvent = WSAMAKESELECTREPLY( WS_FD_CONNECT, wsaErrno() );
2032 bPost = TRUE;
2035 /* otherwise bPost stays FALSE, should probably clear event_set */
2037 else
2039 /* connected socket, no WS_FD_OOB code for now. */
2041 if( flags & WS_FD_WRITE &&
2042 (w || (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] ))) )
2044 /* this will be reenabled when send() or sendto() fail with
2045 * WSAEWOULDBLOCK */
2047 if( PostMessage32A( psop->hWnd, psop->uMsg, (WPARAM32)WS_PTR2HANDLE(psop->pws),
2048 (LPARAM)WSAMAKESELECTREPLY( WS_FD_WRITE, 0 ) ) )
2050 TRACE(winsock, "\t hwnd %04x - %04x, %08x\n",
2051 psop->hWnd, psop->uMsg, (unsigned)MAKELONG(WS_FD_WRITE, 0) );
2052 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2053 num_posted++;
2057 if( r && (flags & (WS_FD_READ | WS_FD_CLOSE)) )
2059 int val = (flags & WS_FD_RAW);
2061 /* WS_FD_RAW is set by the WSAAsyncSelect() init */
2063 bPost = TRUE;
2064 if( !val && ioctl( fd, FIONREAD, (char*)&dwErrBytes) == -1 )
2066 /* weirdness */
2068 dwEvent = WSAMAKESELECTREPLY( WS_FD_READ, wsaErrno() );
2070 else if( val || dwErrBytes )
2072 /* got pending data, will be reenabled by recv() or recvfrom() */
2074 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2075 dwEvent = WSAMAKESELECTREPLY( WS_FD_READ, 0 );
2077 else
2079 /* 0 bytes to read - connection reset by peer? */
2082 val = read( fd, (char*)&dwErrBytes, sizeof(dwErrBytes));
2083 while( errno == EINTR );
2084 if( errno != EWOULDBLOCK )
2086 switch( val )
2088 case 0: errno = ENETDOWN; /* soft reset, fall through */
2089 case -1: /* hard reset */
2090 dwEvent = WSAMAKESELECTREPLY( WS_FD_CLOSE, wsaErrno() );
2091 break;
2093 default: bPost = FALSE;
2094 continue; /* FIXME: this is real bad */
2097 else { bPost = FALSE; continue; } /* more weirdness */
2099 /* this is it, this socket is closed */
2101 psop->pws->flags &= ~(WS_FD_READ | WS_FD_CLOSE | WS_FD_WRITE);
2102 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2103 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2105 if( *max_fd == (fd + 1) ) (*max_fd)--;
2110 if( bPost )
2112 TRACE(winsock, "\t hwnd %04x - %04x, %08x\n",
2113 psop->hWnd, psop->uMsg, (unsigned)dwEvent );
2114 PostMessage32A( psop->hWnd, psop->uMsg,
2115 (WPARAM32)WS_PTR2HANDLE(psop->pws), (LPARAM)dwEvent );
2116 bPost = FALSE;
2117 num_posted++;
2120 if( num_pending <= 0 ) break;
2123 TRACE(winsock, "\tdone, %i posted events\n", num_posted );
2124 return ( num_posted ) ? TRUE : FALSE;
2127 INT32 WINAPI WSAAsyncSelect32(SOCKET32 s, HWND32 hWnd, UINT32 uMsg, UINT32 lEvent)
2129 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
2130 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2132 TRACE(winsock, "(%08x): %04x, hWnd %04x, uMsg %08x, event %08x\n",
2133 (unsigned)pwsi, (SOCKET16)s, (HWND16)hWnd, uMsg, (unsigned)lEvent );
2134 if( _check_ws(pwsi, pws) )
2136 ws_select_op* psop;
2138 if( (psop = pws->psop) )
2140 /* delete previous control struct */
2142 if( psop == __ws_select_list )
2143 __ws_select_list = psop->next;
2144 else
2145 psop->prev->next = psop->next;
2146 if( psop->next ) psop->next->prev = psop->prev;
2148 if( pws->flags & (WS_FD_ACCEPT | WS_FD_CONNECT | WS_FD_READ | WS_FD_CLOSE) )
2149 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
2150 if( pws->flags & (WS_FD_CONNECT | WS_FD_WRITE) )
2151 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
2153 TRACE(winsock,"\tremoving psop = 0x%08x\n", (unsigned) psop );
2155 WS_FREE( pws->psop );
2156 pws->flags &= ~(WS_FD_RAW | WS_FD_ACCEPT | WS_FD_CONNECT |
2157 WS_FD_READ | WS_FD_WRITE | WS_FD_CLOSE);
2158 pws->psop = NULL;
2161 if( lEvent )
2163 psop = (ws_select_op*)WS_ALLOC(sizeof(ws_select_op));
2164 if( psop )
2166 int sock_type, bytes = sizeof(int);
2168 WINSOCK_unblock_io( pws->fd, TRUE );
2170 psop->prev = NULL;
2171 psop->next = __ws_select_list;
2172 if( __ws_select_list )
2173 __ws_select_list->prev = psop;
2174 __ws_select_list = psop;
2176 psop->pws = pws;
2177 psop->hWnd = hWnd;
2178 psop->uMsg = uMsg;
2180 pws->psop = psop;
2181 pws->flags |= (0x0000FFFF & lEvent);
2182 getsockopt(pws->fd, SOL_SOCKET, SO_TYPE, &sock_type, &bytes);
2183 if( sock_type == SOCK_RAW ) pws->flags |= WS_FD_RAW;
2185 if( lEvent & (WS_FD_ACCEPT | WS_FD_CONNECT | WS_FD_READ | WS_FD_CLOSE) )
2186 EVENT_AddIO( pws->fd, EVENT_IO_READ );
2187 if( lEvent & (WS_FD_CONNECT | WS_FD_WRITE) )
2188 EVENT_AddIO( pws->fd, EVENT_IO_WRITE );
2190 /* TODO: handle WS_FD_ACCEPT right away if the socket is readable */
2192 TRACE(winsock,"\tcreating psop = 0x%08x\n", (unsigned)psop );
2194 return 0; /* success */
2196 else pwsi->err = WSAENOBUFS;
2198 else return 0;
2200 else if( pwsi ) pwsi->err = WSAEINVAL;
2201 return SOCKET_ERROR;
2204 INT16 WINAPI WSAAsyncSelect16(SOCKET16 s, HWND16 hWnd, UINT16 wMsg, UINT32 lEvent)
2206 return (INT16)WSAAsyncSelect32( s, hWnd, wMsg, lEvent );
2210 /***********************************************************************
2211 * __WSAFDIsSet() (WINSOCK.151)
2213 INT16 WINAPI __WSAFDIsSet16(SOCKET16 s, ws_fd_set16 *set)
2215 int i = set->fd_count;
2217 TRACE(winsock, "(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2219 while (i--)
2220 if (set->fd_array[i] == s) return 1;
2221 return 0;
2224 /***********************************************************************
2225 * __WSAFDIsSet() (WSOCK32.151)
2227 INT32 WINAPI __WSAFDIsSet32(SOCKET32 s, ws_fd_set32 *set)
2229 int i = set->fd_count;
2231 TRACE(winsock, "(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2233 while (i--)
2234 if (set->fd_array[i] == s) return 1;
2235 return 0;
2238 /***********************************************************************
2239 * WSAIsBlocking() (WINSOCK.114)(WSOCK32.114)
2241 BOOL32 WINAPI WSAIsBlocking(void)
2243 /* By default WinSock should set all its sockets to non-blocking mode
2244 * and poll in PeekMessage loop when processing "blocking" ones. This
2245 * function is supposed to tell if the program is in this loop. Our
2246 * blocking calls are truly blocking so we always return FALSE.
2248 * Note: It is allowed to call this function without prior WSAStartup().
2251 TRACE(winsock, "\n");
2252 return FALSE;
2255 /***********************************************************************
2256 * WSACancelBlockingCall() (WINSOCK.113)(WSOCK32.113)
2258 INT32 WINAPI WSACancelBlockingCall(void)
2260 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2262 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2264 if( pwsi ) return 0;
2265 return SOCKET_ERROR;
2269 /***********************************************************************
2270 * WSASetBlockingHook16() (WINSOCK.109)
2272 FARPROC16 WINAPI WSASetBlockingHook16(FARPROC16 lpBlockFunc)
2274 FARPROC16 prev;
2275 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2277 TRACE(winsock, "(%08x): hook %08x\n",
2278 (unsigned)pwsi, (unsigned) lpBlockFunc);
2279 if( pwsi )
2281 prev = (FARPROC16)pwsi->blocking_hook;
2282 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2283 pwsi->flags &= ~WSI_BLOCKINGHOOK32;
2284 return prev;
2286 return 0;
2290 /***********************************************************************
2291 * WSASetBlockingHook32()
2293 FARPROC32 WINAPI WSASetBlockingHook32(FARPROC32 lpBlockFunc)
2295 FARPROC32 prev;
2296 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2298 TRACE(winsock, "(%08x): hook %08x\n",
2299 (unsigned)pwsi, (unsigned) lpBlockFunc);
2300 if( pwsi ) {
2301 prev = (FARPROC32)pwsi->blocking_hook;
2302 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2303 pwsi->flags |= WSI_BLOCKINGHOOK32;
2304 return prev;
2306 return NULL;
2310 /***********************************************************************
2311 * WSAUnhookBlockingHook16() (WINSOCK.110)
2313 INT16 WINAPI WSAUnhookBlockingHook16(void)
2315 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2317 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2318 if( pwsi ) return (INT16)(pwsi->blocking_hook = 0);
2319 return SOCKET_ERROR;
2323 /***********************************************************************
2324 * WSAUnhookBlockingHook32()
2326 INT32 WINAPI WSAUnhookBlockingHook32(void)
2328 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2330 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2331 if( pwsi )
2333 pwsi->blocking_hook = 0;
2334 pwsi->flags &= ~WSI_BLOCKINGHOOK32;
2335 return 0;
2337 return SOCKET_ERROR;
2341 * TCP/IP action codes.
2345 #define WSCNTL_TCPIP_QUERY_INFO 0x00000000
2346 #define WSCNTL_TCPIP_SET_INFO 0x00000001
2347 #define WSCNTL_TCPIP_ICMP_ECHO 0x00000002
2348 #define WSCNTL_TCPIP_TEST 0x00000003
2351 /***********************************************************************
2352 * WsControl()
2354 * WsControl seems to be an undocumented Win95 function. A lot of
2355 * discussion about WsControl can be found on the net, e.g.
2356 * Subject: Re: WSOCK32.DLL WsControl Exported Function
2357 * From: "Peter Rindfuss" <rindfuss-s@medea.wz-berlin.de>
2358 * Date: 1997/08/17
2361 DWORD WINAPI WsControl(DWORD protocoll,DWORD action,
2362 LPVOID inbuf,LPDWORD inbuflen,
2363 LPVOID outbuf,LPDWORD outbuflen)
2366 switch (action) {
2367 case WSCNTL_TCPIP_ICMP_ECHO:
2369 unsigned int addr = *(unsigned int*)inbuf;
2370 #if 0
2371 int timeout= *(unsigned int*)(inbuf+4);
2372 short x1 = *(unsigned short*)(inbuf+8);
2373 short sendbufsize = *(unsigned short*)(inbuf+10);
2374 char x2 = *(unsigned char*)(inbuf+12);
2375 char ttl = *(unsigned char*)(inbuf+13);
2376 char service = *(unsigned char*)(inbuf+14);
2377 char type= *(unsigned char*)(inbuf+15); /* 0x2: don't fragment*/
2378 #endif
2380 fprintf(stdnimp,"WsControl(ICMP_ECHO) to 0x%08x stub \n",
2381 addr);
2382 break;
2384 default:
2385 fprintf(stdnimp,"WsControl(%lx,%lx,%p,%p,%p,%p) stub\n",
2386 protocoll,action,inbuf,inbuflen,outbuf,outbuflen);
2388 return FALSE;
2390 /*********************************************************
2391 * WS_s_perror WSOCK32.1108
2393 void WINAPI WS_s_perror(LPCSTR message)
2395 fprintf(stdnimp,"s_perror %s stub\n",message);
2396 return;
2400 /* ----------------------------------- end of API stuff */
2402 /* ----------------------------------- helper functions -
2404 * TODO: Merge WS_dup_..() stuff into one function that
2405 * would operate with a generic structure containing internal
2406 * pointers (via a template of some kind).
2409 static int list_size(char** l, int item_size)
2411 int i,j = 0;
2412 if(l)
2413 { for(i=0;l[i];i++)
2414 j += (item_size) ? item_size : strlen(l[i]) + 1;
2415 j += (i + 1) * sizeof(char*); }
2416 return j;
2419 static int list_dup(char** l_src, char* ref, char* base, int item_size)
2421 /* base is either either equal to ref or 0 or SEGPTR */
2423 char* p = ref;
2424 char** l_to = (char**)ref;
2425 int i,j,k;
2427 for(j=0;l_src[j];j++) ;
2428 p += (j + 1) * sizeof(char*);
2429 for(i=0;i<j;i++)
2430 { l_to[i] = base + (p - ref);
2431 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
2432 memcpy(p, l_src[i], k); p += k; }
2433 l_to[i] = NULL;
2434 return (p - ref);
2437 /* ----- hostent */
2439 static int hostent_size(struct hostent* p_he)
2441 int size = 0;
2442 if( p_he )
2443 { size = sizeof(struct hostent);
2444 size += strlen(p_he->h_name) + 1;
2445 size += list_size(p_he->h_aliases, 0);
2446 size += list_size(p_he->h_addr_list, p_he->h_length ); }
2447 return size;
2450 int WS_dup_he(LPWSINFO pwsi, struct hostent* p_he, int flag)
2452 /* Convert hostent structure into ws_hostent so that the data fits
2453 * into pwsi->buffer. Internal pointers can be linear, SEGPTR, or
2454 * relative to pwsi->buffer depending on "flag" value. Returns size
2455 * of the data copied (also in the pwsi->buflen).
2458 int size = hostent_size(p_he);
2459 if( size )
2461 struct ws_hostent* p_to;
2462 char* p_name,*p_aliases,*p_addr,*p_base,*p;
2464 _check_buffer(pwsi, size);
2465 p_to = (struct ws_hostent*)pwsi->buffer;
2466 p = pwsi->buffer;
2467 p_base = (flag & WS_DUP_OFFSET) ? NULL
2468 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2469 p += sizeof(struct ws_hostent);
2470 p_name = p;
2471 strcpy(p, p_he->h_name); p += strlen(p) + 1;
2472 p_aliases = p;
2473 p += list_dup(p_he->h_aliases, p, p_base + (p - pwsi->buffer), 0);
2474 p_addr = p;
2475 list_dup(p_he->h_addr_list, p, p_base + (p - pwsi->buffer), p_he->h_length);
2477 p_to->h_addrtype = (INT16)p_he->h_addrtype;
2478 p_to->h_length = (INT16)p_he->h_length;
2479 p_to->h_name = (SEGPTR)(p_base + (p_name - pwsi->buffer));
2480 p_to->h_aliases = (SEGPTR)(p_base + (p_aliases - pwsi->buffer));
2481 p_to->h_addr_list = (SEGPTR)(p_base + (p_addr - pwsi->buffer));
2483 size += (sizeof(struct ws_hostent) - sizeof(struct hostent));
2485 return size;
2488 /* ----- protoent */
2490 static int protoent_size(struct protoent* p_pe)
2492 int size = 0;
2493 if( p_pe )
2494 { size = sizeof(struct protoent);
2495 size += strlen(p_pe->p_name) + 1;
2496 size += list_size(p_pe->p_aliases, 0); }
2497 return size;
2500 int WS_dup_pe(LPWSINFO pwsi, struct protoent* p_pe, int flag)
2502 int size = protoent_size(p_pe);
2503 if( size )
2505 struct ws_protoent* p_to;
2506 char* p_name,*p_aliases,*p_base,*p;
2508 _check_buffer(pwsi, size);
2509 p_to = (struct ws_protoent*)pwsi->buffer;
2510 p = pwsi->buffer;
2511 p_base = (flag & WS_DUP_OFFSET) ? NULL
2512 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2513 p += sizeof(struct ws_protoent);
2514 p_name = p;
2515 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
2516 p_aliases = p;
2517 list_dup(p_pe->p_aliases, p, p_base + (p - pwsi->buffer), 0);
2519 p_to->p_proto = (INT16)p_pe->p_proto;
2520 p_to->p_name = (SEGPTR)(p_base) + (p_name - pwsi->buffer);
2521 p_to->p_aliases = (SEGPTR)((p_base) + (p_aliases - pwsi->buffer));
2523 size += (sizeof(struct ws_protoent) - sizeof(struct protoent));
2525 return size;
2528 /* ----- servent */
2530 static int servent_size(struct servent* p_se)
2532 int size = 0;
2533 if( p_se )
2534 { size += sizeof(struct servent);
2535 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
2536 size += list_size(p_se->s_aliases, 0); }
2537 return size;
2540 int WS_dup_se(LPWSINFO pwsi, struct servent* p_se, int flag)
2542 int size = servent_size(p_se);
2543 if( size )
2545 struct ws_servent* p_to;
2546 char* p_name,*p_aliases,*p_proto,*p_base,*p;
2548 _check_buffer(pwsi, size);
2549 p_to = (struct ws_servent*)pwsi->buffer;
2550 p = pwsi->buffer;
2551 p_base = (flag & WS_DUP_OFFSET) ? NULL
2552 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2553 p += sizeof(struct ws_servent);
2554 p_name = p;
2555 strcpy(p, p_se->s_name); p += strlen(p) + 1;
2556 p_proto = p;
2557 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
2558 p_aliases = p;
2559 list_dup(p_se->s_aliases, p, p_base + (p - pwsi->buffer), 0);
2561 p_to->s_port = (INT16)p_se->s_port;
2562 p_to->s_name = (SEGPTR)(p_base + (p_name - pwsi->buffer));
2563 p_to->s_proto = (SEGPTR)(p_base + (p_proto - pwsi->buffer));
2564 p_to->s_aliases = (SEGPTR)(p_base + (p_aliases - pwsi->buffer));
2566 size += (sizeof(struct ws_servent) - sizeof(struct servent));
2568 return size;
2571 /* ----------------------------------- error handling */
2573 UINT16 wsaErrno(void)
2575 int loc_errno = errno;
2576 #if defined(__FreeBSD__)
2577 ERR(winsock, "errno %d, (%s).\n",
2578 errno, strerror(errno));
2579 #else
2580 ERR(winsock, "errno %d\n", errno);
2581 #endif
2583 switch(loc_errno)
2585 case EINTR: return WSAEINTR;
2586 case EBADF: return WSAEBADF;
2587 case EACCES: return WSAEACCES;
2588 case EFAULT: return WSAEFAULT;
2589 case EINVAL: return WSAEINVAL;
2590 case EMFILE: return WSAEMFILE;
2591 case EWOULDBLOCK: return WSAEWOULDBLOCK;
2592 case EINPROGRESS: return WSAEINPROGRESS;
2593 case EALREADY: return WSAEALREADY;
2594 case ENOTSOCK: return WSAENOTSOCK;
2595 case EDESTADDRREQ: return WSAEDESTADDRREQ;
2596 case EMSGSIZE: return WSAEMSGSIZE;
2597 case EPROTOTYPE: return WSAEPROTOTYPE;
2598 case ENOPROTOOPT: return WSAENOPROTOOPT;
2599 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
2600 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
2601 case EOPNOTSUPP: return WSAEOPNOTSUPP;
2602 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
2603 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
2604 case EADDRINUSE: return WSAEADDRINUSE;
2605 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
2606 case ENETDOWN: return WSAENETDOWN;
2607 case ENETUNREACH: return WSAENETUNREACH;
2608 case ENETRESET: return WSAENETRESET;
2609 case ECONNABORTED: return WSAECONNABORTED;
2610 case ECONNRESET: return WSAECONNRESET;
2611 case ENOBUFS: return WSAENOBUFS;
2612 case EISCONN: return WSAEISCONN;
2613 case ENOTCONN: return WSAENOTCONN;
2614 case ESHUTDOWN: return WSAESHUTDOWN;
2615 case ETOOMANYREFS: return WSAETOOMANYREFS;
2616 case ETIMEDOUT: return WSAETIMEDOUT;
2617 case ECONNREFUSED: return WSAECONNREFUSED;
2618 case ELOOP: return WSAELOOP;
2619 case ENAMETOOLONG: return WSAENAMETOOLONG;
2620 case EHOSTDOWN: return WSAEHOSTDOWN;
2621 case EHOSTUNREACH: return WSAEHOSTUNREACH;
2622 case ENOTEMPTY: return WSAENOTEMPTY;
2623 #ifdef EPROCLIM
2624 case EPROCLIM: return WSAEPROCLIM;
2625 #endif
2626 #ifdef EUSERS
2627 case EUSERS: return WSAEUSERS;
2628 #endif
2629 #ifdef EDQUOT
2630 case EDQUOT: return WSAEDQUOT;
2631 #endif
2632 #ifdef ESTALE
2633 case ESTALE: return WSAESTALE;
2634 #endif
2635 #ifdef EREMOTE
2636 case EREMOTE: return WSAEREMOTE;
2637 #endif
2639 /* just in case we ever get here and there are no problems */
2640 case 0: return 0;
2641 default:
2642 fprintf(stderr, "winsock: unknown errno %d!\n", errno);
2643 return WSAEOPNOTSUPP;
2647 UINT16 wsaHerrno(void)
2649 int loc_errno = h_errno;
2651 #if defined(__FreeBSD__)
2652 ERR(winsock, "h_errno %d, (%s).\n",
2653 h_errno, strerror(h_errno));
2654 #else
2655 ERR(winsock, "h_errno %d.\n", h_errno);
2656 #endif
2658 switch(loc_errno)
2660 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
2661 case TRY_AGAIN: return WSATRY_AGAIN;
2662 case NO_RECOVERY: return WSANO_RECOVERY;
2663 case NO_DATA: return WSANO_DATA;
2665 case 0: return 0;
2666 default:
2667 fprintf(stderr, "winsock: unknown h_errno %d!\n", h_errno);
2668 return WSAEOPNOTSUPP;