configure first checks whether it is possible to compile IPX code with
[wine/multimedia.git] / misc / winsock.c
blob9a7425bde51acfb67c56332654696a8466ace542
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 "config.h"
15 #include <string.h>
16 #include <signal.h>
17 #include <sys/types.h>
18 #include <sys/ipc.h>
19 #include <sys/ioctl.h>
20 #ifdef HAVE_SYS_FILIO_H
21 # include <sys/filio.h>
22 #endif
23 #if defined(__svr4__)
24 #include <sys/ioccom.h>
25 #include <sys/sockio.h>
26 #endif
28 #if defined(__EMX__)
29 # include <sys/so_ioctl.h>
30 #endif
32 #ifdef HAVE_SYS_PARAM_H
33 # include <sys/param.h>
34 #endif
36 #include <sys/msg.h>
37 #include <sys/wait.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <ctype.h>
42 #include <fcntl.h>
43 #include <errno.h>
44 #include <sys/errno.h>
45 #include <netdb.h>
46 #include <unistd.h>
48 #include "winsock.h"
49 #include "windows.h"
50 #include "winnt.h"
51 #include "heap.h"
52 #include "ldt.h"
53 #include "task.h"
54 #include "message.h"
55 #include "miscemu.h"
56 #include "debug.h"
58 #define DEBUG_SOCKADDR 0
59 #define dump_sockaddr(a) \
60 DUMP("sockaddr_in: family %d, address %s, port %d\n", \
61 ((struct sockaddr_in *)a)->sin_family, \
62 inet_ntoa(((struct sockaddr_in *)a)->sin_addr), \
63 ntohs(((struct sockaddr_in *)a)->sin_port))
65 #pragma pack(4)
67 /* ----------------------------------- internal data */
69 static HANDLE32 _WSHeap = 0;
70 static unsigned char* _ws_stub = NULL;
71 static LPWSINFO _wsi_list = NULL;
73 #define WS_ALLOC(size) \
74 HeapAlloc(_WSHeap, HEAP_ZERO_MEMORY, (size) )
75 #define WS_FREE(ptr) \
76 HeapFree(_WSHeap, 0, (ptr) )
78 #define WS_PTR2HANDLE(ptr) \
79 ((short)((int)(ptr) - (int)_ws_stub))
80 #define WS_HANDLE2PTR(handle) \
81 ((unsigned)((int)_ws_stub + (int)(handle)))
83 #define WSI_CHECK_RANGE(pwsi, pws) \
84 ( ((unsigned)(pws) > (unsigned)(pwsi)) && \
85 ((unsigned)(pws) < ((unsigned)(pwsi) + sizeof(WSINFO))) )
87 static INT32 _ws_sock_ops[] =
88 { WS_SO_DEBUG, WS_SO_REUSEADDR, WS_SO_KEEPALIVE, WS_SO_DONTROUTE,
89 WS_SO_BROADCAST, WS_SO_LINGER, WS_SO_OOBINLINE, WS_SO_SNDBUF,
90 WS_SO_RCVBUF, WS_SO_ERROR, WS_SO_TYPE, WS_SO_DONTLINGER, 0 };
91 static int _px_sock_ops[] =
92 { SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST,
93 SO_LINGER, SO_OOBINLINE, SO_SNDBUF, SO_RCVBUF, SO_ERROR, SO_TYPE,
94 SO_LINGER };
96 static int _check_ws(LPWSINFO pwsi, ws_socket* pws);
97 static char* _check_buffer(LPWSINFO pwsi, int size);
99 /***********************************************************************
100 * convert_sockopt()
102 * Converts socket flags from Windows format.
104 static void convert_sockopt(INT32 *level, INT32 *optname)
106 int i;
107 switch (*level)
109 case WS_SOL_SOCKET:
110 *level = SOL_SOCKET;
111 for(i=0; _ws_sock_ops[i]; i++)
112 if( _ws_sock_ops[i] == *optname ) break;
113 if( _ws_sock_ops[i] ) *optname = _px_sock_ops[i];
114 else WARN(winsock, "Unknown optname %d\n", *optname);
115 break;
116 case WS_IPPROTO_TCP:
117 *optname = IPPROTO_TCP;
121 /* ----------------------------------- Per-thread info (or per-process?) */
123 static LPWSINFO wsi_find(HTASK16 hTask)
125 TDB* pTask = (TDB*)GlobalLock16(hTask);
126 if( pTask )
128 if( pTask->pwsi ) return pTask->pwsi;
129 else
131 LPWSINFO pwsi = _wsi_list;
132 while( pwsi && pwsi->tid != hTask ) pwsi = pwsi->next;
133 if( pwsi )
134 WARN(winsock,"(pwsi=0x%08x,task=0x%04x):Loose wsi struct! \n",
135 (unsigned)pwsi, hTask );
136 return pwsi;
139 return NULL;
142 static ws_socket* wsi_alloc_socket(LPWSINFO pwsi, int fd)
144 /* Initialize a new entry in the socket table */
146 if( pwsi->last_free >= 0 )
148 int i = pwsi->last_free;
150 pwsi->last_free = pwsi->sock[i].flags; /* free list */
151 pwsi->sock[i].fd = fd;
152 pwsi->sock[i].flags = 0;
153 return &pwsi->sock[i];
155 return NULL;
158 static int wsi_strtolo(LPWSINFO pwsi, const char* name, const char* opt)
160 /* Stuff a lowercase copy of the string into the local buffer */
162 int i = strlen(name) + 2;
163 char* p = _check_buffer(pwsi, i + ((opt)?strlen(opt):0));
165 if( p )
167 do *p++ = tolower(*name); while(*name++);
168 i = (p - (char*)(pwsi->buffer));
169 if( opt ) do *p++ = tolower(*opt); while(*opt++);
170 return i;
172 return 0;
175 static fd_set* fd_set_import( fd_set* fds, LPWSINFO pwsi, void* wsfds, int* highfd, BOOL32 b32 )
177 /* translate Winsock fd set into local fd set */
179 if( wsfds )
181 #define wsfds16 ((ws_fd_set16*)wsfds)
182 #define wsfds32 ((ws_fd_set32*)wsfds)
183 ws_socket* pws;
184 int i, count;
186 FD_ZERO(fds);
187 count = b32 ? wsfds32->fd_count : wsfds16->fd_count;
189 for( i = 0; i < count; i++ )
191 pws = (b32) ? (ws_socket*)WS_HANDLE2PTR(wsfds32->fd_array[i])
192 : (ws_socket*)WS_HANDLE2PTR(wsfds16->fd_array[i]);
193 if( _check_ws(pwsi, pws) )
195 if( pws->fd > *highfd ) *highfd = pws->fd;
196 FD_SET(pws->fd, fds);
199 #undef wsfds32
200 #undef wsfds16
201 return fds;
203 return NULL;
206 __inline__ static int sock_error_p(int s)
208 unsigned int optval, optlen;
210 optlen = sizeof(optval);
211 getsockopt(s, SOL_SOCKET, SO_ERROR, &optval, &optlen);
212 if (optval) WARN(winsock, "\t[%i] error: %d\n", s, optval);
213 return optval != 0;
216 static int fd_set_export( LPWSINFO pwsi, fd_set* fds, fd_set* exceptfds, void* wsfds, BOOL32 b32 )
218 int num_err = 0;
220 /* translate local fd set into Winsock fd set, adding
221 * errors to exceptfds (only if app requested it) */
223 if( wsfds )
225 #define wsfds16 ((ws_fd_set16*)wsfds)
226 #define wsfds32 ((ws_fd_set32*)wsfds)
227 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
229 for( i = 0, j = 0; i < count; i++ )
231 ws_socket *pws = (b32) ? (ws_socket*)WS_HANDLE2PTR(wsfds32->fd_array[i])
232 : (ws_socket*)WS_HANDLE2PTR(wsfds16->fd_array[i]);
233 if( _check_ws(pwsi, pws) )
235 int fd = pws->fd;
237 if( FD_ISSET(fd, fds) )
239 if ( exceptfds && sock_error_p(fd) )
241 FD_SET(fd, exceptfds);
242 num_err++;
244 else if( b32 )
245 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
246 else
247 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
252 if( b32 ) wsfds32->fd_count = j;
253 else wsfds16->fd_count = j;
255 TRACE(winsock, "\n");
256 #undef wsfds32
257 #undef wsfds16
259 return num_err;
262 HANDLE16 __ws_gethandle( void* ptr )
264 return (HANDLE16)WS_PTR2HANDLE(ptr);
267 void* __ws_memalloc( int size )
269 return WS_ALLOC(size);
272 void __ws_memfree(void* ptr)
274 WS_FREE(ptr);
277 /* ----------------------------------- API -----
279 * Init / cleanup / error checking.
282 /***********************************************************************
283 * WSAStartup16() (WINSOCK.115)
285 * Create socket control struct, attach it to the global list and
286 * update a pointer in the task struct.
288 INT16 WINAPI WSAStartup16(UINT16 wVersionRequested, LPWSADATA lpWSAData)
290 WSADATA WINSOCK_data = { 0x0101, 0x0101,
291 "WINE Sockets 1.1",
292 #ifdef linux
293 "Linux/i386",
294 #elif defined(__NetBSD__)
295 "NetBSD/i386",
296 #elif defined(sunos)
297 "SunOS",
298 #elif defined(__FreeBSD__)
299 "FreeBSD",
300 #elif defined(__OpenBSD__)
301 "OpenBSD/i386",
302 #else
303 "Unknown",
304 #endif
305 WS_MAX_SOCKETS_PER_PROCESS,
306 WS_MAX_UDP_DATAGRAM, (SEGPTR)NULL };
307 HTASK16 tid = GetCurrentTask();
308 LPWSINFO pwsi;
310 TRACE(winsock, "verReq=%x\n", wVersionRequested);
312 if (LOBYTE(wVersionRequested) < 1 || (LOBYTE(wVersionRequested) == 1 &&
313 HIBYTE(wVersionRequested) < 1)) return WSAVERNOTSUPPORTED;
315 if (!lpWSAData) return WSAEINVAL;
317 /* initialize socket heap */
319 if( !_ws_stub )
321 _WSHeap = HeapCreate(HEAP_ZERO_MEMORY, 8120, 32768);
322 if( !(_ws_stub = WS_ALLOC(0x10)) )
324 ERR(winsock,"Fatal: failed to create WinSock heap\n");
325 return 0;
328 if( _WSHeap == 0 ) return WSASYSNOTREADY;
330 /* create socket array for this task */
332 pwsi = wsi_find(GetCurrentTask());
333 if( pwsi == NULL )
335 TDB* pTask = (TDB*)GlobalLock16( tid );
337 if( (pwsi = (LPWSINFO)WS_ALLOC( sizeof(WSINFO))) )
339 int i = 0;
340 pwsi->tid = tid;
341 for( i = 0; i < WS_MAX_SOCKETS_PER_PROCESS; i++ )
343 pwsi->sock[i].fd = -1;
344 pwsi->sock[i].flags = i + 1;
346 pwsi->sock[WS_MAX_SOCKETS_PER_PROCESS - 1].flags = -1;
348 else return WSASYSNOTREADY;
350 /* add this control struct to the global list */
352 pwsi->prev = NULL;
353 if( _wsi_list )
354 _wsi_list->prev = pwsi;
355 pwsi->next = _wsi_list;
356 _wsi_list = pwsi;
357 pTask->pwsi = pwsi;
359 else pwsi->num_startup++;
361 /* return winsock information */
363 memcpy(lpWSAData, &WINSOCK_data, sizeof(WINSOCK_data));
365 TRACE(winsock, "succeeded\n");
366 return 0;
369 /***********************************************************************
370 * WSAStartup32() (WSOCK32.115)
372 INT32 WINAPI WSAStartup32(UINT32 wVersionRequested, LPWSADATA lpWSAData)
374 return WSAStartup16( wVersionRequested, lpWSAData );
377 /***********************************************************************
378 * WSACleanup() (WINSOCK.116)
380 * Cleanup functions of varying impact.
382 void WINSOCK_Shutdown()
384 /* Called on exit(), has to remove all outstanding async DNS processes. */
386 WINSOCK_cancel_task_aops( 0, __ws_memfree );
389 INT32 WINSOCK_DeleteTaskWSI( TDB* pTask, LPWSINFO pwsi )
391 /* WSACleanup() backend, called on task termination as well.
392 * Real DLL would have registered its own signal handler with
393 * TaskSetSignalHandler() and waited until USIG_TERMINATION/USIG_GPF
394 * but this scheme is much more straightforward.
397 int i, j, n;
399 if( --pwsi->num_startup > 0 ) return 0;
401 SIGNAL_MaskAsyncEvents( TRUE );
402 WINSOCK_cancel_task_aops( pTask->hSelf, __ws_memfree );
403 SIGNAL_MaskAsyncEvents( FALSE );
405 /* unlink socket control struct */
407 if( pwsi == _wsi_list )
408 _wsi_list = pwsi->next;
409 else
410 pwsi->prev->next = pwsi->next;
411 if( pwsi->next ) pwsi->next->prev = pwsi->prev;
413 if( _wsi_list == NULL )
414 WINSOCK_Shutdown(); /* just in case */
416 if( pwsi->flags & WSI_BLOCKINGCALL )
417 TRACE(winsock,"\tinside blocking call!\n");
419 /* FIXME: aop_control() doesn't decrement pwsi->num_async_rq
421 * if( pwsi->num_async_rq )
422 * WARN(winsock,"\thave %i outstanding async ops!\n", pwsi->num_async_rq );
425 for(i = 0, j = 0, n = 0; i < WS_MAX_SOCKETS_PER_PROCESS; i++)
426 if( pwsi->sock[i].fd != -1 )
428 if( pwsi->sock[i].psop )
430 n++;
431 WSAAsyncSelect32( (SOCKET16)WS_PTR2HANDLE(pwsi->sock + i), 0, 0, 0 );
433 close(pwsi->sock[i].fd); j++;
435 if( j )
436 TRACE(winsock,"\tclosed %i sockets, killed %i async selects!\n", j, n);
438 /* delete scratch buffers */
440 if( pwsi->buffer ) SEGPTR_FREE(pwsi->buffer);
441 if( pwsi->dbuffer ) SEGPTR_FREE(pwsi->dbuffer);
443 if( pTask )
444 pTask->pwsi = NULL;
445 memset( pwsi, 0, sizeof(WSINFO) );
446 WS_FREE(pwsi);
447 return 0;
450 INT32 WINAPI WSACleanup(void)
452 HTASK16 hTask = GetCurrentTask();
454 TRACE(winsock, "(%04x)\n", hTask );
455 if( hTask )
457 LPWSINFO pwsi = wsi_find(hTask);
458 if( pwsi )
459 return WINSOCK_DeleteTaskWSI( (TDB*)GlobalLock16(hTask), pwsi );
460 return SOCKET_ERROR;
462 else
463 WINSOCK_Shutdown(); /* remove all outstanding DNS requests */
464 return 0;
468 /***********************************************************************
469 * WSAGetLastError() (WSOCK32.111)(WINSOCK.111)
471 INT32 WINAPI WSAGetLastError(void)
473 LPWSINFO pwsi = wsi_find(GetCurrentTask());
474 INT16 ret = (pwsi) ? pwsi->err : WSANOTINITIALISED;
476 TRACE(winsock, "(%08x) = %i\n",
477 (unsigned)pwsi, (int)ret);
478 return ret;
481 /***********************************************************************
482 * WSASetLastError32() (WSOCK32.112)
484 void WINAPI WSASetLastError32(INT32 iError)
486 LPWSINFO pwsi = wsi_find(GetCurrentTask());
488 TRACE(winsock, "(%08x): %d\n", (unsigned)pwsi, (int)iError);
489 if( pwsi ) pwsi->err = iError;
492 /***********************************************************************
493 * WSASetLastError16() (WINSOCK.112)
495 void WINAPI WSASetLastError16(INT16 iError)
497 WSASetLastError32(iError);
500 int _check_ws(LPWSINFO pwsi, ws_socket* pws)
502 if( pwsi )
504 if( pwsi->flags & WSI_BLOCKINGCALL ) pwsi->err = WSAEINPROGRESS;
505 else if( WSI_CHECK_RANGE(pwsi, pws) ) return 1;
506 else pwsi->err = WSAENOTSOCK;
508 return 0;
511 char* _check_buffer(LPWSINFO pwsi, int size)
513 if( pwsi->buffer && pwsi->buflen >= size ) return pwsi->buffer;
514 else SEGPTR_FREE(pwsi->buffer);
516 pwsi->buffer = (char*)SEGPTR_ALLOC((pwsi->buflen = size));
517 return pwsi->buffer;
520 struct ws_hostent* _check_buffer_he(LPWSINFO pwsi, int size)
522 if( pwsi->he && pwsi->helen >= size ) return pwsi->he;
523 else SEGPTR_FREE(pwsi->he);
525 pwsi->he = (struct ws_hostent*)SEGPTR_ALLOC((pwsi->helen = size));
526 return pwsi->he;
529 struct ws_servent* _check_buffer_se(LPWSINFO pwsi, int size)
531 if( pwsi->se && pwsi->selen >= size ) return pwsi->se;
532 else SEGPTR_FREE(pwsi->se);
534 pwsi->se = (struct ws_servent*)SEGPTR_ALLOC((pwsi->selen = size));
535 return pwsi->se;
538 struct ws_protoent* _check_buffer_pe(LPWSINFO pwsi, int size)
540 if( pwsi->pe && pwsi->pelen >= size ) return pwsi->pe;
541 else SEGPTR_FREE(pwsi->pe);
543 pwsi->pe = (struct ws_protoent*)SEGPTR_ALLOC((pwsi->pelen = size));
544 return pwsi->pe;
547 /* ----------------------------------- i/o APIs */
549 /***********************************************************************
550 * accept() (WSOCK32.1)
552 SOCKET32 WINAPI WINSOCK_accept32(SOCKET32 s, struct sockaddr *addr,
553 INT32 *addrlen32)
555 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR((SOCKET16)s);
556 LPWSINFO pwsi = wsi_find(GetCurrentTask());
557 #ifdef HAVE_IPX
558 struct ws_sockaddr_ipx* addr2 = (struct ws_sockaddr_ipx *)addr;
559 #endif
561 TRACE(winsock, "(%08x): socket %04x\n",
562 (unsigned)pwsi, (UINT16)s );
563 if( _check_ws(pwsi, pws) )
565 int sock, fd_flags;
567 fd_flags = fcntl(pws->fd, F_GETFL, 0);
569 if( (sock = accept(pws->fd, addr, addrlen32)) >= 0 )
571 ws_socket* pnew = wsi_alloc_socket(pwsi, sock);
572 if( pnew )
574 s = (SOCKET32)WS_PTR2HANDLE(pnew);
575 if( pws->psop && pws->flags & WS_FD_ACCEPT )
577 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
579 /* async select the accept()'ed socket */
580 WSAAsyncSelect32( s, pws->psop->hWnd, pws->psop->uMsg,
581 pws->flags & ~WS_FD_ACCEPT );
583 #ifdef HAVE_IPX
584 if (addr && ((struct sockaddr_ipx *)addr)->sipx_family == AF_IPX) {
585 addr = (struct sockaddr *)
586 malloc(addrlen32 ? *addrlen32 : sizeof(*addr2));
587 memcpy(addr, addr2,
588 addrlen32 ? *addrlen32 : sizeof(*addr2));
589 addr2->sipx_family = WS_AF_IPX;
590 addr2->sipx_network = ((struct sockaddr_ipx *)addr)->sipx_network;
591 addr2->sipx_port = ((struct sockaddr_ipx *)addr)->sipx_port;
592 memcpy(addr2->sipx_node,
593 ((struct sockaddr_ipx *)addr)->sipx_node, IPX_NODE_LEN);
594 free(addr);
596 #endif
597 return s;
599 else pwsi->err = WSAENOBUFS;
601 else pwsi->err = wsaErrno();
603 #ifdef HAVE_IPX
604 if (addr && ((struct sockaddr_ipx *)addr)->sipx_family == AF_IPX) {
605 addr = (struct sockaddr *)
606 malloc(addrlen32 ? *addrlen32 : sizeof(*addr2));
607 memcpy(addr, addr2, addrlen32 ? *addrlen32 : sizeof(*addr2));
608 addr2->sipx_family = WS_AF_IPX;
609 addr2->sipx_network = ((struct sockaddr_ipx *)addr)->sipx_network;
610 addr2->sipx_port = ((struct sockaddr_ipx *)addr)->sipx_port;
611 memcpy(addr2->sipx_node,
612 ((struct sockaddr_ipx *)addr)->sipx_node, IPX_NODE_LEN);
613 free(addr);
615 #endif
616 return INVALID_SOCKET32;
619 /***********************************************************************
620 * accept() (WINSOCK.1)
622 SOCKET16 WINAPI WINSOCK_accept16(SOCKET16 s, struct sockaddr* addr,
623 INT16* addrlen16 )
625 INT32 addrlen32 = addrlen16 ? *addrlen16 : 0;
626 SOCKET32 retSocket = WINSOCK_accept32( s, addr, &addrlen32 );
627 if( addrlen16 ) *addrlen16 = (INT16)addrlen32;
628 return (SOCKET16)retSocket;
631 /***********************************************************************
632 * bind() (WSOCK32.2)
634 INT32 WINAPI WINSOCK_bind32(SOCKET32 s, struct sockaddr *name, INT32 namelen)
636 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
637 LPWSINFO pwsi = wsi_find(GetCurrentTask());
638 #ifdef HAVE_IPX
639 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
640 #endif
642 TRACE(winsock, "(%08x): socket %04x, ptr %8x, length %d\n",
643 (unsigned)pwsi, s, (int) name, namelen);
644 #if DEBUG_SOCKADDR
645 dump_sockaddr(name);
646 #endif
648 if ( _check_ws(pwsi, pws) )
650 /* FIXME: what family does this really map to on the Unix side? */
651 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_PUP)
652 ((struct ws_sockaddr_ipx *)name)->sipx_family = AF_UNSPEC;
653 #ifdef HAVE_IPX
654 else if (name &&
655 ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_IPX)
657 name = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
658 memset(name, '\0', sizeof(struct sockaddr_ipx));
659 ((struct sockaddr_ipx *)name)->sipx_family = AF_IPX;
660 ((struct sockaddr_ipx *)name)->sipx_port = name2->sipx_port;
661 ((struct sockaddr_ipx *)name)->sipx_network = name2->sipx_network;
662 memcpy(((struct sockaddr_ipx *)name)->sipx_node,
663 name2->sipx_node, IPX_NODE_LEN);
664 namelen = sizeof(struct sockaddr_ipx);
666 #endif
667 if ( namelen >= sizeof(*name) )
669 if ( name && (((struct ws_sockaddr_in *)name)->sin_family == AF_INET
670 #ifdef HAVE_IPX
671 || ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX
672 #endif
675 if ( bind(pws->fd, name, namelen) < 0 )
677 int loc_errno = errno;
678 WARN(winsock, "\tfailure - errno = %i\n", errno);
679 errno = loc_errno;
680 switch(errno)
682 case EBADF: pwsi->err = WSAENOTSOCK; break;
683 case EADDRNOTAVAIL: pwsi->err = WSAEINVAL; break;
684 default: pwsi->err = wsaErrno();
687 else {
688 #ifdef HAVE_IPX
689 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
690 free(name);
691 #endif
692 return 0; /* success */
695 else pwsi->err = WSAEAFNOSUPPORT;
697 else pwsi->err = WSAEFAULT;
698 #ifdef HAVE_IPX
699 if (name && ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
700 free(name);
701 #endif
703 return SOCKET_ERROR;
706 /***********************************************************************
707 * bind() (WINSOCK.2)
709 INT16 WINAPI WINSOCK_bind16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
711 return (INT16)WINSOCK_bind32( s, name, namelen );
714 /***********************************************************************
715 * closesocket() (WSOCK32.3)
717 INT32 WINAPI WINSOCK_closesocket32(SOCKET32 s)
719 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
720 LPWSINFO pwsi = wsi_find(GetCurrentTask());
722 TRACE(winsock, "(%08x): socket %08x\n", (unsigned)pwsi, s);
724 if( _check_ws(pwsi, pws) )
726 int fd = pws->fd;
728 if( pws->psop ) WSAAsyncSelect32( s, 0, 0, 0 );
730 pws->fd = -1;
731 pws->flags = (unsigned)pwsi->last_free;
732 pwsi->last_free = pws - &pwsi->sock[0]; /* add to free list */
734 if( close(fd) == 0 )
735 return 0;
736 pwsi->err = (errno == EBADF) ? WSAENOTSOCK : wsaErrno();
738 return SOCKET_ERROR;
741 /***********************************************************************
742 * closesocket() (WINSOCK.3)
744 INT16 WINAPI WINSOCK_closesocket16(SOCKET16 s)
746 return (INT16)WINSOCK_closesocket32(s);
749 /***********************************************************************
750 * connect() (WSOCK32.4)
752 INT32 WINAPI WINSOCK_connect32(SOCKET32 s, struct sockaddr *name, INT32 namelen)
754 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
755 LPWSINFO pwsi = wsi_find(GetCurrentTask());
756 #ifdef HAVE_IPX
757 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
758 #endif
760 TRACE(winsock, "(%08x): socket %04x, ptr %8x, length %d\n",
761 (unsigned)pwsi, s, (int) name, namelen);
762 #if DEBUG_SOCKADDR
763 dump_sockaddr(name);
764 #endif
766 if( _check_ws(pwsi, pws) )
768 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_PUP)
769 ((struct ws_sockaddr_ipx *)name)->sipx_family = AF_UNSPEC;
770 #ifdef HAVE_IPX
771 else if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_IPX)
773 name = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
774 memset(name, '\0', sizeof(struct sockaddr_ipx));
775 ((struct sockaddr_ipx *)name)->sipx_family = AF_IPX;
776 ((struct sockaddr_ipx *)name)->sipx_port = name2->sipx_port;
777 ((struct sockaddr_ipx *)name)->sipx_network = name2->sipx_network;
778 memcpy(((struct sockaddr_ipx *)name)->sipx_node,
779 name2->sipx_node, IPX_NODE_LEN);
780 namelen = sizeof(struct sockaddr_ipx);
782 #endif
783 if (connect(pws->fd, name, namelen) == 0)
785 if( pws->psop && (pws->flags & WS_FD_CONNECT) )
787 /* application did AsyncSelect() but then went
788 * ahead and called connect() without waiting for
789 * notification.
791 * FIXME: Do we have to post a notification message
792 * in this case?
795 if( !(pws->flags & WS_FD_CONNECTED) )
797 if( pws->flags & (WS_FD_READ | WS_FD_CLOSE) )
798 EVENT_AddIO( pws->fd, EVENT_IO_READ );
799 else
800 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
801 if( pws->flags & WS_FD_WRITE )
802 EVENT_AddIO( pws->fd, EVENT_IO_WRITE );
803 else
804 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
807 pws->flags |= WS_FD_CONNECTED;
808 pws->flags &= ~(WS_FD_INACTIVE | WS_FD_CONNECT | WS_FD_LISTENING);
809 #ifdef HAVE_IPX
810 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
811 free(name);
812 #endif
813 return 0;
815 pwsi->err = (errno == EINPROGRESS) ? WSAEWOULDBLOCK : wsaErrno();
817 #ifdef HAVE_IPX
818 if (name && ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
819 free(name);
820 #endif
821 return SOCKET_ERROR;
824 /***********************************************************************
825 * connect() (WINSOCK.4)
827 INT16 WINAPI WINSOCK_connect16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
829 return (INT16)WINSOCK_connect32( s, name, namelen );
832 /***********************************************************************
833 * getpeername() (WSOCK32.5)
835 INT32 WINAPI WINSOCK_getpeername32(SOCKET32 s, struct sockaddr *name,
836 INT32 *namelen)
838 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
839 LPWSINFO pwsi = wsi_find(GetCurrentTask());
840 #ifdef HAVE_IPX
841 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
842 #endif
844 TRACE(winsock, "(%08x): socket: %04x, ptr %8x, ptr %8x\n",
845 (unsigned)pwsi, s, (int) name, *namelen);
846 if( _check_ws(pwsi, pws) )
848 if (getpeername(pws->fd, name, namelen) == 0) {
849 #ifdef HAVE_IPX
850 if (((struct ws_sockaddr_ipx *)name)->sipx_family == AF_IPX) {
851 name = (struct sockaddr *)
852 malloc(namelen ? *namelen : sizeof(*name2));
853 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
854 name2->sipx_family = WS_AF_IPX;
855 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
856 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
857 memcpy(name2->sipx_node,
858 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
859 free(name);
861 #endif
862 return 0;
864 pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
866 #ifdef HAVE_IPX
867 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == AF_IPX) {
868 name = (struct sockaddr *) malloc(namelen ? *namelen : sizeof(*name2));
869 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
870 name2->sipx_family = WS_AF_IPX;
871 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
872 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
873 memcpy(name2->sipx_node,
874 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
875 free(name);
877 #endif
878 return SOCKET_ERROR;
881 /***********************************************************************
882 * getpeername() (WINSOCK.5)
884 INT16 WINAPI WINSOCK_getpeername16(SOCKET16 s, struct sockaddr *name,
885 INT16 *namelen16)
887 INT32 namelen32 = *namelen16;
888 INT32 retVal = WINSOCK_getpeername32( s, name, &namelen32 );
890 #if DEBUG_SOCKADDR
891 dump_sockaddr(name);
892 #endif
894 *namelen16 = namelen32;
895 return (INT16)retVal;
898 /***********************************************************************
899 * getsockname() (WSOCK32.6)
901 INT32 WINAPI WINSOCK_getsockname32(SOCKET32 s, struct sockaddr *name,
902 INT32 *namelen)
904 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
905 LPWSINFO pwsi = wsi_find(GetCurrentTask());
906 #ifdef HAVE_IPX
907 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
908 #endif
910 TRACE(winsock, "(%08x): socket: %04x, ptr %8x, ptr %8x\n",
911 (unsigned)pwsi, s, (int) name, (int) *namelen);
912 if( _check_ws(pwsi, pws) )
914 if (getsockname(pws->fd, name, namelen) == 0) {
915 #ifdef HAVE_IPX
916 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX) {
917 name = (struct sockaddr *)
918 malloc(namelen ? *namelen : sizeof(*name2));
919 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
920 name2->sipx_family = WS_AF_IPX;
921 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
922 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
923 memcpy(name2->sipx_node,
924 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
925 free(name);
927 #endif
928 return 0;
930 pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
932 #ifdef HAVE_IPX
933 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == AF_IPX) {
934 name = (struct sockaddr *) malloc(namelen ? *namelen : sizeof(*name2));
935 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
936 name2->sipx_family = WS_AF_IPX;
937 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
938 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
939 memcpy(name2->sipx_node,
940 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
941 free(name);
943 #endif
944 return SOCKET_ERROR;
947 /***********************************************************************
948 * getsockname() (WINSOCK.6)
950 INT16 WINAPI WINSOCK_getsockname16(SOCKET16 s, struct sockaddr *name,
951 INT16 *namelen16)
953 INT32 retVal;
955 if( namelen16 )
957 INT32 namelen32 = *namelen16;
958 retVal = WINSOCK_getsockname32( s, name, &namelen32 );
959 *namelen16 = namelen32;
961 #if DEBUG_SOCKADDR
962 dump_sockaddr(name);
963 #endif
966 else retVal = SOCKET_ERROR;
967 return (INT16)retVal;
971 /***********************************************************************
972 * getsockopt() (WSOCK32.7)
974 INT32 WINAPI WINSOCK_getsockopt32(SOCKET32 s, INT32 level,
975 INT32 optname, char *optval, INT32 *optlen)
977 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
978 LPWSINFO pwsi = wsi_find(GetCurrentTask());
980 TRACE(winsock, "(%08x): socket: %04x, opt %d, ptr %8x, ptr %8x\n",
981 (unsigned)pwsi, s, level, (int) optval, (int) *optlen);
982 if( _check_ws(pwsi, pws) )
984 convert_sockopt(&level, &optname);
985 if (getsockopt(pws->fd, (int) level, optname, optval, optlen) == 0 )
986 return 0;
987 pwsi->err = (errno == EBADF) ? WSAENOTSOCK : wsaErrno();
989 return SOCKET_ERROR;
992 /***********************************************************************
993 * getsockopt() (WINSOCK.7)
995 INT16 WINAPI WINSOCK_getsockopt16(SOCKET16 s, INT16 level,
996 INT16 optname, char *optval, INT16 *optlen)
998 INT32 optlen32;
999 INT32 *p = &optlen32;
1000 INT32 retVal;
1001 if( optlen ) optlen32 = *optlen; else p = NULL;
1002 retVal = WINSOCK_getsockopt32( s, (UINT16)level, optname, optval, p );
1003 if( optlen ) *optlen = optlen32;
1004 return (INT16)retVal;
1007 /***********************************************************************
1008 * htonl() (WINSOCK.8)(WSOCK32.8)
1010 u_long WINAPI WINSOCK_htonl(u_long hostlong) { return( htonl(hostlong) ); }
1011 /***********************************************************************
1012 * htons() (WINSOCK.9)(WSOCK32.9)
1014 u_short WINAPI WINSOCK_htons(u_short hostshort) { return( htons(hostshort) ); }
1015 /***********************************************************************
1016 * inet_addr() (WINSOCK.10)(WSOCK32.10)
1018 u_long WINAPI WINSOCK_inet_addr(char *cp) { return( inet_addr(cp) ); }
1019 /***********************************************************************
1020 * htohl() (WINSOCK.14)(WSOCK32.14)
1022 u_long WINAPI WINSOCK_ntohl(u_long netlong) { return( ntohl(netlong) ); }
1023 /***********************************************************************
1024 * ntohs() (WINSOCK.15)(WSOCK32.15)
1026 u_short WINAPI WINSOCK_ntohs(u_short netshort) { return( ntohs(netshort) ); }
1028 /***********************************************************************
1029 * inet_ntoa() (WINSOCK.11)(WSOCK32.11)
1031 char* WINAPI WINSOCK_inet_ntoa32(struct in_addr in)
1033 /* use "buffer for dummies" here because some applications have
1034 * propensity to decode addresses in ws_hostent structure without
1035 * saving them first...
1038 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1040 if( pwsi )
1042 char* s = inet_ntoa(in);
1043 if( s )
1045 if( pwsi->dbuffer == NULL )
1046 if((pwsi->dbuffer = (char*) SEGPTR_ALLOC(32)) == NULL )
1048 pwsi->err = WSAENOBUFS;
1049 return NULL;
1051 strncpy(pwsi->dbuffer, s, 32 );
1052 return pwsi->dbuffer;
1054 pwsi->err = wsaErrno();
1056 return NULL;
1059 SEGPTR WINAPI WINSOCK_inet_ntoa16(struct in_addr in)
1061 char* retVal = WINSOCK_inet_ntoa32(in);
1062 return retVal ? SEGPTR_GET(retVal) : (SEGPTR)NULL;
1065 /***********************************************************************
1066 * ioctlsocket() (WSOCK32.12)
1068 INT32 WINAPI WINSOCK_ioctlsocket32(SOCKET32 s, UINT32 cmd, UINT32 *argp)
1070 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1071 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1073 TRACE(winsock, "(%08x): socket %04x, cmd %08x, ptr %8x\n",
1074 (unsigned)pwsi, s, cmd, (unsigned) argp);
1075 if( _check_ws(pwsi, pws) )
1077 long newcmd = cmd;
1079 switch( cmd )
1081 case WS_FIONREAD:
1082 newcmd=FIONREAD;
1083 break;
1085 case WS_FIONBIO:
1086 newcmd=FIONBIO;
1087 if( pws->psop && *argp == 0 )
1089 /* AsyncSelect()'ed sockets are always nonblocking */
1090 pwsi->err = WSAEINVAL;
1091 return SOCKET_ERROR;
1093 break;
1095 case WS_SIOCATMARK:
1096 newcmd=SIOCATMARK;
1097 break;
1099 case WS_IOW('f',125,u_long):
1100 WARN(winsock,"Warning: WS1.1 shouldn't be using async I/O\n");
1101 pwsi->err = WSAEINVAL;
1102 return SOCKET_ERROR;
1104 default:
1105 /* Netscape tries hard to use bogus ioctl 0x667e */
1106 WARN(winsock, "\tunknown WS_IOCTL cmd (%08x)\n", cmd);
1108 if( ioctl(pws->fd, newcmd, (char*)argp ) == 0 ) return 0;
1109 pwsi->err = (errno == EBADF) ? WSAENOTSOCK : wsaErrno();
1111 return SOCKET_ERROR;
1114 /***********************************************************************
1115 * ioctlsocket() (WINSOCK.12)
1117 INT16 WINAPI WINSOCK_ioctlsocket16(SOCKET16 s, UINT32 cmd, UINT32 *argp)
1119 return (INT16)WINSOCK_ioctlsocket32( s, cmd, argp );
1123 /***********************************************************************
1124 * listen() (WSOCK32.13)
1126 INT32 WINAPI WINSOCK_listen32(SOCKET32 s, INT32 backlog)
1128 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1129 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1131 TRACE(winsock, "(%08x): socket %04x, backlog %d\n",
1132 (unsigned)pwsi, s, backlog);
1133 if( _check_ws(pwsi, pws) )
1135 if (listen(pws->fd, backlog) == 0)
1137 if( !pws->psop )
1139 int fd_flags = fcntl(pws->fd, F_GETFL, 0);
1140 if( !(fd_flags & O_NONBLOCK) ) pws->flags |= WS_FD_ACCEPT;
1142 pws->flags |= WS_FD_LISTENING;
1143 pws->flags &= ~(WS_FD_INACTIVE | WS_FD_CONNECT | WS_FD_CONNECTED); /* just in case */
1144 return 0;
1146 pwsi->err = wsaErrno();
1148 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1149 return SOCKET_ERROR;
1152 /***********************************************************************
1153 * listen() (WINSOCK.13)
1155 INT16 WINAPI WINSOCK_listen16(SOCKET16 s, INT16 backlog)
1157 return (INT16)WINSOCK_listen32( s, backlog );
1161 /***********************************************************************
1162 * recv() (WSOCK32.16)
1164 INT32 WINAPI WINSOCK_recv32(SOCKET32 s, char *buf, INT32 len, INT32 flags)
1166 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1167 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1169 TRACE(winsock, "(%08x): socket %04x, buf %8x, len %d, "
1170 "flags %d\n", (unsigned)pwsi, s, (unsigned)buf,
1171 len, flags);
1172 if( _check_ws(pwsi, pws) )
1174 INT32 length;
1175 if ((length = recv(pws->fd, buf, len, flags)) >= 0)
1177 TRACE(winsock, " -> %i bytes\n", length);
1179 if( pws->psop && (pws->flags & (WS_FD_READ | WS_FD_CLOSE)) )
1180 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
1182 return length;
1184 pwsi->err = wsaErrno();
1186 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1187 WARN(winsock, " -> ERROR\n");
1188 return SOCKET_ERROR;
1191 /***********************************************************************
1192 * recv() (WINSOCK.16)
1194 INT16 WINAPI WINSOCK_recv16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1196 return (INT16)WINSOCK_recv32( s, buf, len, flags );
1200 /***********************************************************************
1201 * recvfrom() (WSOCK32.17)
1203 INT32 WINAPI WINSOCK_recvfrom32(SOCKET32 s, char *buf, INT32 len, INT32 flags,
1204 struct sockaddr *from, INT32 *fromlen32)
1206 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1207 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1208 #ifdef HAVE_IPX
1209 struct ws_sockaddr_ipx* from2 = (struct ws_sockaddr_ipx *)from;
1210 #endif
1212 TRACE(winsock, "(%08x): socket %04x, ptr %08x, "
1213 "len %d, flags %d\n", (unsigned)pwsi, s, (unsigned)buf,
1214 len, flags);
1215 #if DEBUG_SOCKADDR
1216 if( from ) dump_sockaddr(from);
1217 else DUMP("from = NULL\n");
1218 #endif
1220 if( _check_ws(pwsi, pws) )
1222 int length;
1224 if ((length = recvfrom(pws->fd, buf, len, flags, from, fromlen32)) >= 0)
1226 TRACE(winsock, " -> %i bytes\n", length);
1228 if( pws->psop && (pws->flags & (WS_FD_READ | WS_FD_CLOSE)) )
1229 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
1231 #ifdef HAVE_IPX
1232 if (from && ((struct sockaddr_ipx *)from)->sipx_family == AF_IPX) {
1233 from = (struct sockaddr *)
1234 malloc(fromlen32 ? *fromlen32 : sizeof(*from2));
1235 memcpy(from, from2, fromlen32 ? *fromlen32 : sizeof(*from2));
1236 from2->sipx_family = WS_AF_IPX;
1237 from2->sipx_network = ((struct sockaddr_ipx *)from)->sipx_network;
1238 from2->sipx_port = ((struct sockaddr_ipx *)from)->sipx_port;
1239 memcpy(from2->sipx_node,
1240 ((struct sockaddr_ipx *)from)->sipx_node, IPX_NODE_LEN);
1241 free(from);
1243 #endif
1244 return (INT16)length;
1246 pwsi->err = wsaErrno();
1248 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1249 WARN(winsock, " -> ERROR\n");
1250 #ifdef HAVE_IPX
1251 if (from && ((struct sockaddr_ipx *)from)->sipx_family == AF_IPX) {
1252 from = (struct sockaddr *)
1253 malloc(fromlen32 ? *fromlen32 : sizeof(*from2));
1254 memcpy(from, from2, fromlen32 ? *fromlen32 : sizeof(*from2));
1255 from2->sipx_family = WS_AF_IPX;
1256 from2->sipx_network = ((struct sockaddr_ipx *)from)->sipx_network;
1257 from2->sipx_port = ((struct sockaddr_ipx *)from)->sipx_port;
1258 memcpy(from2->sipx_node,
1259 ((struct sockaddr_ipx *)from)->sipx_node, IPX_NODE_LEN);
1260 free(from);
1262 #endif
1263 return SOCKET_ERROR;
1266 /***********************************************************************
1267 * recvfrom() (WINSOCK.17)
1269 INT16 WINAPI WINSOCK_recvfrom16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1270 struct sockaddr *from, INT16 *fromlen16)
1272 INT32 fromlen32;
1273 INT32 *p = &fromlen32;
1274 INT32 retVal;
1276 if( fromlen16 ) fromlen32 = *fromlen16; else p = NULL;
1277 retVal = WINSOCK_recvfrom32( s, buf, len, flags, from, p );
1278 if( fromlen16 ) *fromlen16 = fromlen32;
1279 return (INT16)retVal;
1282 /***********************************************************************
1283 * select() (WINSOCK.18)(WSOCK32.18)
1285 static INT32 __ws_select( BOOL32 b32, void *ws_readfds, void *ws_writefds, void *ws_exceptfds,
1286 struct timeval *timeout )
1288 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1290 TRACE(winsock, "(%08x): read %8x, write %8x, excp %8x\n",
1291 (unsigned) pwsi, (unsigned) ws_readfds, (unsigned) ws_writefds, (unsigned) ws_exceptfds);
1293 if( pwsi )
1295 int highfd = 0;
1296 fd_set readfds, writefds, exceptfds;
1297 fd_set *p_read, *p_write, *p_except;
1299 p_read = fd_set_import(&readfds, pwsi, ws_readfds, &highfd, b32);
1300 p_write = fd_set_import(&writefds, pwsi, ws_writefds, &highfd, b32);
1301 p_except = fd_set_import(&exceptfds, pwsi, ws_exceptfds, &highfd, b32);
1303 if( (highfd = select(highfd + 1, p_read, p_write, p_except, timeout)) > 0 )
1305 fd_set_export(pwsi, &readfds, p_except, ws_readfds, b32);
1306 fd_set_export(pwsi, &writefds, p_except, ws_writefds, b32);
1308 if (p_except && ws_exceptfds)
1310 #define wsfds16 ((ws_fd_set16*)ws_exceptfds)
1311 #define wsfds32 ((ws_fd_set32*)ws_exceptfds)
1312 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
1314 for (i = j = 0; i < count; i++)
1316 ws_socket *pws = (b32) ? (ws_socket *)WS_HANDLE2PTR(wsfds32->fd_array[i])
1317 : (ws_socket *)WS_HANDLE2PTR(wsfds16->fd_array[i]);
1318 if( _check_ws(pwsi, pws) && FD_ISSET(pws->fd, &exceptfds) )
1320 if( b32 )
1321 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
1322 else
1323 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
1326 if( b32 )
1327 wsfds32->fd_count = j;
1328 else
1329 wsfds16->fd_count = j;
1330 #undef wsfds32
1331 #undef wsfds16
1333 return highfd;
1335 if( ws_readfds ) ((ws_fd_set32*)ws_readfds)->fd_count = 0;
1336 if( ws_writefds ) ((ws_fd_set32*)ws_writefds)->fd_count = 0;
1337 if( ws_exceptfds ) ((ws_fd_set32*)ws_exceptfds)->fd_count = 0;
1339 if( highfd == 0 ) return 0;
1340 pwsi->err = wsaErrno();
1342 return SOCKET_ERROR;
1345 INT16 WINAPI WINSOCK_select16(INT16 nfds, ws_fd_set16 *ws_readfds,
1346 ws_fd_set16 *ws_writefds, ws_fd_set16 *ws_exceptfds,
1347 struct timeval *timeout)
1349 return (INT16)__ws_select( FALSE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1352 INT32 WINAPI WINSOCK_select32(INT32 nfds, ws_fd_set32 *ws_readfds,
1353 ws_fd_set32 *ws_writefds, ws_fd_set32 *ws_exceptfds,
1354 struct timeval *timeout)
1356 /* struct timeval is the same for both 32- and 16-bit code */
1357 return (INT32)__ws_select( TRUE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1361 /***********************************************************************
1362 * send() (WSOCK32.19)
1364 INT32 WINAPI WINSOCK_send32(SOCKET32 s, char *buf, INT32 len, INT32 flags)
1366 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1367 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1369 TRACE(winsock, "(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
1370 (unsigned)pwsi, s, (unsigned) buf, len, flags);
1371 if( _check_ws(pwsi, pws) )
1373 int length;
1375 if ((length = send(pws->fd, buf, len, flags)) < 0 )
1377 pwsi->err = wsaErrno();
1378 if( pwsi->err == WSAEWOULDBLOCK &&
1379 pws->psop && pws->flags & WS_FD_WRITE )
1380 EVENT_AddIO( pws->fd, EVENT_IO_WRITE ); /* reenabler */
1382 else return (INT16)length;
1384 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1385 return SOCKET_ERROR;
1388 /***********************************************************************
1389 * send() (WINSOCK.19)
1391 INT16 WINAPI WINSOCK_send16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1393 return WINSOCK_send32( s, buf, len, flags );
1396 /***********************************************************************
1397 * sendto() (WSOCK32.20)
1399 INT32 WINAPI WINSOCK_sendto32(SOCKET32 s, char *buf, INT32 len, INT32 flags,
1400 struct sockaddr *to, INT32 tolen)
1402 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1403 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1404 #ifdef HAVE_IPX
1405 struct ws_sockaddr_ipx* to2 = (struct ws_sockaddr_ipx *)to;
1406 #endif
1408 TRACE(winsock, "(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
1409 (unsigned)pwsi, s, (unsigned) buf, len, flags);
1410 if( _check_ws(pwsi, pws) )
1412 INT32 length;
1414 if (to && ((struct ws_sockaddr_ipx *)to)->sipx_family == WS_AF_PUP)
1415 ((struct ws_sockaddr_ipx *)to)->sipx_family = AF_UNSPEC;
1416 #ifdef HAVE_IPX
1417 else if (to &&
1418 ((struct ws_sockaddr_ipx *)to)->sipx_family == WS_AF_IPX)
1420 to = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
1421 memset(to, '\0', sizeof(struct sockaddr_ipx));
1422 ((struct sockaddr_ipx *)to)->sipx_family = AF_IPX;
1423 ((struct sockaddr_ipx *)to)->sipx_port = to2->sipx_port;
1424 ((struct sockaddr_ipx *)to)->sipx_network = to2->sipx_network;
1425 memcpy(((struct sockaddr_ipx *)to)->sipx_node,
1426 to2->sipx_node, IPX_NODE_LEN);
1427 tolen = sizeof(struct sockaddr_ipx);
1429 #endif
1430 if ((length = sendto(pws->fd, buf, len, flags, to, tolen)) < 0 )
1432 pwsi->err = wsaErrno();
1433 if( pwsi->err == WSAEWOULDBLOCK &&
1434 pws->psop && pws->flags & WS_FD_WRITE )
1435 EVENT_AddIO( pws->fd, EVENT_IO_WRITE ); /* reenabler */
1437 else {
1438 #ifdef HAVE_IPX
1439 if (to && ((struct sockaddr_ipx *)to)->sipx_family == AF_IPX) {
1440 free(to);
1442 #endif
1443 return length;
1446 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1447 #ifdef HAVE_IPX
1448 if (to && ((struct sockaddr_ipx *)to)->sipx_family == AF_IPX) {
1449 free(to);
1451 #endif
1452 return SOCKET_ERROR;
1455 /***********************************************************************
1456 * sendto() (WINSOCK.20)
1458 INT16 WINAPI WINSOCK_sendto16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1459 struct sockaddr *to, INT16 tolen)
1461 return (INT16)WINSOCK_sendto32( s, buf, len, flags, to, tolen );
1464 /***********************************************************************
1465 * setsockopt() (WSOCK32.21)
1467 INT32 WINAPI WINSOCK_setsockopt32(SOCKET16 s, INT32 level, INT32 optname,
1468 char *optval, INT32 optlen)
1470 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1471 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1473 TRACE(winsock, "(%08x): socket %04x, lev %d, opt %d, ptr %08x, len %d\n",
1474 (unsigned)pwsi, s, level, optname, (int) optval, optlen);
1475 if( _check_ws(pwsi, pws) )
1477 struct linger linger;
1479 convert_sockopt(&level, &optname);
1480 if (optname == SO_LINGER && optval) {
1481 /* yes, uses unsigned short in both win16/win32 */
1482 linger.l_onoff = ((UINT16*)optval)[0];
1483 linger.l_linger = ((UINT16*)optval)[1];
1484 /* FIXME: what is documented behavior if SO_LINGER optval
1485 is null?? */
1486 optval = (char*)&linger;
1487 optlen = sizeof(struct linger);
1489 if (setsockopt(pws->fd, level, optname, optval, optlen) == 0) return 0;
1490 pwsi->err = wsaErrno();
1492 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1493 return SOCKET_ERROR;
1496 /***********************************************************************
1497 * setsockopt() (WINSOCK.21)
1499 INT16 WINAPI WINSOCK_setsockopt16(SOCKET16 s, INT16 level, INT16 optname,
1500 char *optval, INT16 optlen)
1502 if( !optval ) return SOCKET_ERROR;
1503 return (INT16)WINSOCK_setsockopt32( s, (UINT16)level, optname, optval, optlen );
1507 /***********************************************************************
1508 * shutdown() (WSOCK32.22)
1510 INT32 WINAPI WINSOCK_shutdown32(SOCKET32 s, INT32 how)
1512 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1513 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1515 TRACE(winsock, "(%08x): socket %04x, how %i\n",
1516 (unsigned)pwsi, s, how );
1517 if( _check_ws(pwsi, pws) )
1519 if( pws->psop )
1520 switch( how )
1522 case 0: /* drop receives */
1523 if( pws->flags & (WS_FD_READ | WS_FD_CLOSE) )
1524 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
1525 pws->flags &= ~(WS_FD_READ | WS_FD_CLOSE);
1526 #ifdef SHUT_RD
1527 how = SHUT_RD;
1528 #endif
1529 break;
1531 case 1: /* drop sends */
1532 if( pws->flags & WS_FD_WRITE )
1533 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
1534 pws->flags &= ~WS_FD_WRITE;
1535 #ifdef SHUT_WR
1536 how = SHUT_WR;
1537 #endif
1538 break;
1540 case 2: /* drop all */
1541 #ifdef SHUT_RDWR
1542 how = SHUT_RDWR;
1543 #endif
1544 default:
1545 WSAAsyncSelect32( s, 0, 0, 0 );
1546 break;
1549 if (shutdown(pws->fd, how) == 0)
1551 if( how > 1 )
1553 pws->flags &= ~(WS_FD_CONNECTED | WS_FD_LISTENING);
1554 pws->flags |= WS_FD_INACTIVE;
1556 return 0;
1558 pwsi->err = wsaErrno();
1560 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1561 return SOCKET_ERROR;
1564 /***********************************************************************
1565 * shutdown() (WINSOCK.22)
1567 INT16 WINAPI WINSOCK_shutdown16(SOCKET16 s, INT16 how)
1569 return (INT16)WINSOCK_shutdown32( s, how );
1573 /***********************************************************************
1574 * socket() (WSOCK32.23)
1576 SOCKET32 WINAPI WINSOCK_socket32(INT32 af, INT32 type, INT32 protocol)
1578 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1580 TRACE(winsock, "(%08x): af=%d type=%d protocol=%d\n",
1581 (unsigned)pwsi, af, type, protocol);
1583 if( pwsi )
1585 int sock;
1587 /* check the socket family */
1588 switch(af)
1590 #ifdef HAVE_IPX
1591 case WS_AF_IPX: af = AF_IPX;
1592 #endif
1593 case AF_INET:
1594 case AF_UNSPEC: break;
1595 default: pwsi->err = WSAEAFNOSUPPORT;
1596 return INVALID_SOCKET32;
1599 /* check the socket type */
1600 switch(type)
1602 case SOCK_STREAM:
1603 case SOCK_DGRAM:
1604 case SOCK_RAW: break;
1605 default: pwsi->err = WSAESOCKTNOSUPPORT;
1606 return INVALID_SOCKET32;
1609 /* check the protocol type */
1610 if ( protocol < 0 ) /* don't support negative values */
1611 { pwsi->err = WSAEPROTONOSUPPORT; return INVALID_SOCKET32; }
1613 if ( af == AF_UNSPEC) /* did they not specify the address family? */
1614 switch(protocol)
1616 case IPPROTO_TCP:
1617 if (type == SOCK_STREAM) { af = AF_INET; break; }
1618 case IPPROTO_UDP:
1619 if (type == SOCK_DGRAM) { af = AF_INET; break; }
1620 default: pwsi->err = WSAEPROTOTYPE; return INVALID_SOCKET32;
1623 if ((sock = socket(af, type, protocol)) >= 0)
1625 ws_socket* pnew = wsi_alloc_socket(pwsi, sock);
1627 TRACE(winsock,"\tcreated %i (handle %04x)\n", sock, (UINT16)WS_PTR2HANDLE(pnew));
1629 if( pnew )
1631 pnew->flags |= WS_FD_INACTIVE;
1632 return (SOCKET16)WS_PTR2HANDLE(pnew);
1635 close(sock);
1636 pwsi->err = WSAENOBUFS;
1637 return INVALID_SOCKET32;
1640 if (errno == EPERM) /* raw socket denied */
1642 WARN(winsock, "WS_SOCKET: not enough privileges\n");
1643 pwsi->err = WSAESOCKTNOSUPPORT;
1644 } else pwsi->err = wsaErrno();
1647 WARN(winsock, "\t\tfailed!\n");
1648 return INVALID_SOCKET32;
1651 /***********************************************************************
1652 * socket() (WINSOCK.23)
1654 SOCKET16 WINAPI WINSOCK_socket16(INT16 af, INT16 type, INT16 protocol)
1656 return (SOCKET16)WINSOCK_socket32( af, type, protocol );
1660 /* ----------------------------------- DNS services
1662 * IMPORTANT: 16-bit API structures have SEGPTR pointers inside them.
1663 * Also, we have to use wsock32 stubs to convert structures and
1664 * error codes from Unix to WSA, hence there is no direct mapping in
1665 * the relay32/wsock32.spec.
1668 static char* NULL_STRING = "NULL";
1670 /***********************************************************************
1671 * gethostbyaddr() (WINSOCK.51)(WSOCK32.51)
1673 static struct WIN_hostent* __ws_gethostbyaddr(const char *addr, int len, int type, int dup_flag)
1675 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1677 if( pwsi )
1679 struct hostent* host;
1680 if( (host = gethostbyaddr(addr, len, type)) != NULL )
1681 if( WS_dup_he(pwsi, host, dup_flag) )
1682 return (struct WIN_hostent*)(pwsi->he);
1683 else
1684 pwsi->err = WSAENOBUFS;
1685 else
1686 pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1688 return NULL;
1691 SEGPTR WINAPI WINSOCK_gethostbyaddr16(const char *addr, INT16 len, INT16 type)
1693 struct WIN_hostent* retval;
1694 TRACE(winsock, "ptr %08x, len %d, type %d\n",
1695 (unsigned) addr, len, type);
1696 retval = __ws_gethostbyaddr( addr, len, type, WS_DUP_SEGPTR );
1697 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1700 struct WIN_hostent* WINAPI WINSOCK_gethostbyaddr32(const char *addr, INT32 len,
1701 INT32 type)
1703 TRACE(winsock, "ptr %08x, len %d, type %d\n",
1704 (unsigned) addr, len, type);
1705 return __ws_gethostbyaddr(addr, len, type, WS_DUP_LINEAR);
1708 /***********************************************************************
1709 * gethostbyname() (WINSOCK.52)(WSOCK32.52)
1711 static struct WIN_hostent * __ws_gethostbyname(const char *name, int dup_flag)
1713 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1715 if( pwsi )
1717 struct hostent* host;
1718 if( (host = gethostbyname(name)) != NULL )
1719 if( WS_dup_he(pwsi, host, dup_flag) )
1720 return (struct WIN_hostent*)(pwsi->he);
1721 else pwsi->err = WSAENOBUFS;
1722 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1724 return NULL;
1727 SEGPTR WINAPI WINSOCK_gethostbyname16(const char *name)
1729 struct WIN_hostent* retval;
1730 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1731 retval = __ws_gethostbyname( name, WS_DUP_SEGPTR );
1732 return (retval)? SEGPTR_GET(retval) : ((SEGPTR)NULL) ;
1735 struct WIN_hostent* WINAPI WINSOCK_gethostbyname32(const char* name)
1737 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1738 return __ws_gethostbyname( name, WS_DUP_LINEAR );
1742 /***********************************************************************
1743 * getprotobyname() (WINSOCK.53)(WSOCK32.53)
1745 static struct WIN_protoent* __ws_getprotobyname(const char *name, int dup_flag)
1747 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1749 if( pwsi )
1751 struct protoent* proto;
1752 if( (proto = getprotobyname(name)) != NULL )
1753 if( WS_dup_pe(pwsi, proto, dup_flag) )
1754 return (struct WIN_protoent*)(pwsi->pe);
1755 else pwsi->err = WSAENOBUFS;
1756 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1758 return NULL;
1761 SEGPTR WINAPI WINSOCK_getprotobyname16(const char *name)
1763 struct WIN_protoent* retval;
1764 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1765 retval = __ws_getprotobyname(name, WS_DUP_SEGPTR);
1766 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1769 struct WIN_protoent* WINAPI WINSOCK_getprotobyname32(const char* name)
1771 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1772 return __ws_getprotobyname(name, WS_DUP_LINEAR);
1776 /***********************************************************************
1777 * getprotobynumber() (WINSOCK.54)(WSOCK32.54)
1779 static struct WIN_protoent* __ws_getprotobynumber(int number, int dup_flag)
1781 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1783 if( pwsi )
1785 struct protoent* proto;
1786 if( (proto = getprotobynumber(number)) != NULL )
1787 if( WS_dup_pe(pwsi, proto, dup_flag) )
1788 return (struct WIN_protoent*)(pwsi->pe);
1789 else pwsi->err = WSAENOBUFS;
1790 else pwsi->err = WSANO_DATA;
1792 return NULL;
1795 SEGPTR WINAPI WINSOCK_getprotobynumber16(INT16 number)
1797 struct WIN_protoent* retval;
1798 TRACE(winsock, "%i\n", number);
1799 retval = __ws_getprotobynumber(number, WS_DUP_SEGPTR);
1800 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1803 struct WIN_protoent* WINAPI WINSOCK_getprotobynumber32(INT32 number)
1805 TRACE(winsock, "%i\n", number);
1806 return __ws_getprotobynumber(number, WS_DUP_LINEAR);
1810 /***********************************************************************
1811 * getservbyname() (WINSOCK.55)(WSOCK32.55)
1813 struct WIN_servent* __ws_getservbyname(const char *name, const char *proto, int dup_flag)
1815 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1817 if( pwsi )
1819 struct servent* serv;
1820 int i = wsi_strtolo( pwsi, name, proto );
1822 if( i )
1823 if( (serv = getservbyname(pwsi->buffer, pwsi->buffer + i)) != NULL )
1824 if( WS_dup_se(pwsi, serv, dup_flag) )
1825 return (struct WIN_servent*)(pwsi->se);
1826 else pwsi->err = WSAENOBUFS;
1827 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1828 else pwsi->err = WSAENOBUFS;
1830 return NULL;
1833 SEGPTR WINAPI WINSOCK_getservbyname16(const char *name, const char *proto)
1835 struct WIN_servent* retval;
1836 TRACE(winsock, "'%s', '%s'\n",
1837 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
1838 retval = __ws_getservbyname(name, proto, WS_DUP_SEGPTR);
1839 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1842 struct WIN_servent* WINAPI WINSOCK_getservbyname32(const char *name, const char *proto)
1844 TRACE(winsock, "'%s', '%s'\n",
1845 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
1846 return __ws_getservbyname(name, proto, WS_DUP_LINEAR);
1850 /***********************************************************************
1851 * getservbyport() (WINSOCK.56)(WSOCK32.56)
1853 static struct WIN_servent* __ws_getservbyport(int port, const char* proto, int dup_flag)
1855 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1857 if( pwsi )
1859 struct servent* serv;
1860 int i = wsi_strtolo( pwsi, proto, NULL );
1862 if( i )
1863 if( (serv = getservbyport(port, pwsi->buffer)) != NULL )
1864 if( WS_dup_se(pwsi, serv, dup_flag) )
1865 return (struct WIN_servent*)(pwsi->se);
1866 else pwsi->err = WSAENOBUFS;
1867 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1868 else pwsi->err = WSAENOBUFS;
1870 return NULL;
1873 SEGPTR WINAPI WINSOCK_getservbyport16(INT16 port, const char *proto)
1875 struct WIN_servent* retval;
1876 TRACE(winsock, "%i, '%s'\n",
1877 (int)port, (proto)?proto:NULL_STRING);
1878 retval = __ws_getservbyport(port, proto, WS_DUP_SEGPTR);
1879 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1882 struct WIN_servent* WINAPI WINSOCK_getservbyport32(INT32 port, const char *proto)
1884 TRACE(winsock, "%i, '%s'\n",
1885 (int)port, (proto)?proto:NULL_STRING);
1886 return __ws_getservbyport(port, proto, WS_DUP_LINEAR);
1890 /***********************************************************************
1891 * gethostname() (WSOCK32.57)
1893 INT32 WINAPI WINSOCK_gethostname32(char *name, INT32 namelen)
1895 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1897 TRACE(winsock, "(%08x): name %s, len %d\n",
1898 (unsigned)pwsi, (name)?name:NULL_STRING, namelen);
1899 if( pwsi )
1901 if (gethostname(name, namelen) == 0) return 0;
1902 pwsi->err = (errno == EINVAL) ? WSAEFAULT : wsaErrno();
1904 return SOCKET_ERROR;
1907 /***********************************************************************
1908 * gethostname() (WINSOCK.57)
1910 INT16 WINAPI WINSOCK_gethostname16(char *name, INT16 namelen)
1912 return (INT16)WINSOCK_gethostname32(name, namelen);
1916 /* ------------------------------------- Windows sockets extensions -- *
1918 * ------------------------------------------------------------------- */
1921 /***********************************************************************
1922 * WSAAsyncGetHostByAddr() (WINSOCK.102)
1924 HANDLE16 WINAPI WSAAsyncGetHostByAddr16(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,
1925 INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen)
1927 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1929 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, addr %08x[%i]\n",
1930 (unsigned)pwsi, hWnd, uMsg, (unsigned)addr , len );
1932 if( pwsi )
1933 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, type, addr, len,
1934 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYADDR );
1935 return 0;
1938 /***********************************************************************
1939 * WSAAsyncGetHostByAddr() (WSOCK32.102)
1941 HANDLE32 WINAPI WSAAsyncGetHostByAddr32(HWND32 hWnd, UINT32 uMsg, LPCSTR addr,
1942 INT32 len, INT32 type, LPSTR sbuf, INT32 buflen)
1944 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1946 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, addr %08x[%i]\n",
1947 (unsigned)pwsi, (HWND16)hWnd, uMsg, (unsigned)addr , len );
1949 if( pwsi )
1950 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, type, addr, len,
1951 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYADDR | WSMSG_WIN32_AOP);
1952 return 0;
1956 /***********************************************************************
1957 * WSAAsyncGetHostByName() (WINSOCK.103)
1959 HANDLE16 WINAPI WSAAsyncGetHostByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
1960 SEGPTR sbuf, INT16 buflen)
1962 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1964 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, host %s,
1965 buffer %i\n", (unsigned)pwsi, hWnd, uMsg, (name)?name:NULL_STRING, (int)buflen );
1967 if( pwsi )
1968 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
1969 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYNAME );
1970 return 0;
1973 /***********************************************************************
1974 * WSAAsyncGetHostByName32() (WSOCK32.103)
1976 HANDLE32 WINAPI WSAAsyncGetHostByName32(HWND32 hWnd, UINT32 uMsg, LPCSTR name,
1977 LPSTR sbuf, INT32 buflen)
1979 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1980 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, host %s, buffer %i\n",
1981 (unsigned)pwsi, (HWND16)hWnd, uMsg,
1982 (name)?name:NULL_STRING, (int)buflen );
1983 if( pwsi )
1984 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
1985 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYNAME | WSMSG_WIN32_AOP);
1986 return 0;
1990 /***********************************************************************
1991 * WSAAsyncGetProtoByName() (WINSOCK.105)
1993 HANDLE16 WINAPI WSAAsyncGetProtoByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
1994 SEGPTR sbuf, INT16 buflen)
1996 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1998 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, protocol %s\n",
1999 (unsigned)pwsi, (HWND16)hWnd, uMsg, (name)?name:NULL_STRING );
2001 if( pwsi )
2002 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
2003 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNAME );
2004 return 0;
2007 /***********************************************************************
2008 * WSAAsyncGetProtoByName() (WSOCK32.105)
2010 HANDLE32 WINAPI WSAAsyncGetProtoByName32(HWND32 hWnd, UINT32 uMsg, LPCSTR name,
2011 LPSTR sbuf, INT32 buflen)
2013 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2015 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, protocol %s\n",
2016 (unsigned)pwsi, (HWND16)hWnd, uMsg, (name)?name:NULL_STRING );
2018 if( pwsi )
2019 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
2020 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNAME | WSMSG_WIN32_AOP);
2021 return 0;
2025 /***********************************************************************
2026 * WSAAsyncGetProtoByNumber() (WINSOCK.104)
2028 HANDLE16 WINAPI WSAAsyncGetProtoByNumber16(HWND16 hWnd, UINT16 uMsg, INT16 number,
2029 SEGPTR sbuf, INT16 buflen)
2031 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2033 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, num %i\n",
2034 (unsigned)pwsi, hWnd, uMsg, number );
2036 if( pwsi )
2037 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, number, NULL, 0,
2038 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNUM );
2039 return 0;
2042 /***********************************************************************
2043 * WSAAsyncGetProtoByNumber() (WSOCK32.104)
2045 HANDLE32 WINAPI WSAAsyncGetProtoByNumber32(HWND32 hWnd, UINT32 uMsg, INT32 number,
2046 LPSTR sbuf, INT32 buflen)
2048 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2050 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, num %i\n",
2051 (unsigned)pwsi, (HWND16)hWnd, uMsg, number );
2053 if( pwsi )
2054 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, number, NULL, 0,
2055 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNUM | WSMSG_WIN32_AOP);
2056 return 0;
2060 /***********************************************************************
2061 * WSAAsyncGetServByName() (WINSOCK.107)
2063 HANDLE16 WINAPI WSAAsyncGetServByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
2064 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
2066 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2068 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, name %s, proto %s\n",
2069 (unsigned)pwsi, hWnd, uMsg,
2070 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING );
2072 if( pwsi )
2074 int i = wsi_strtolo( pwsi, name, proto );
2076 if( i )
2077 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, pwsi->buffer, 0,
2078 pwsi->buffer + i, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYNAME );
2080 return 0;
2083 /***********************************************************************
2084 * WSAAsyncGetServByName() (WSOCK32.107)
2086 HANDLE32 WINAPI WSAAsyncGetServByName32(HWND32 hWnd, UINT32 uMsg, LPCSTR name,
2087 LPCSTR proto, LPSTR sbuf, INT32 buflen)
2089 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2091 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, name %s, proto %s\n",
2092 (unsigned)pwsi, (HWND16)hWnd, uMsg,
2093 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING );
2094 if( pwsi )
2096 int i = wsi_strtolo( pwsi, name, proto );
2098 if( i )
2099 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, pwsi->buffer, 0,
2100 pwsi->buffer + i, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYNAME | WSMSG_WIN32_AOP);
2102 return 0;
2106 /***********************************************************************
2107 * WSAAsyncGetServByPort() (WINSOCK.106)
2109 HANDLE16 WINAPI WSAAsyncGetServByPort16(HWND16 hWnd, UINT16 uMsg, INT16 port,
2110 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
2112 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2114 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, port %i, proto %s\n",
2115 (unsigned)pwsi, hWnd, uMsg, port, (proto)?proto:NULL_STRING );
2117 if( pwsi )
2119 int i = wsi_strtolo( pwsi, proto, NULL );
2121 if( i )
2122 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, port, pwsi->buffer, 0,
2123 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYPORT );
2125 return 0;
2128 /***********************************************************************
2129 * WSAAsyncGetServByPort() (WSOCK32.106)
2131 HANDLE32 WINAPI WSAAsyncGetServByPort32(HWND32 hWnd, UINT32 uMsg, INT32 port,
2132 LPCSTR proto, LPSTR sbuf, INT32 buflen)
2134 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2136 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, port %i, proto %s\n",
2137 (unsigned)pwsi, (HWND16)hWnd, uMsg, port, (proto)?proto:NULL_STRING );
2139 if( pwsi )
2141 int i = wsi_strtolo( pwsi, proto, NULL );
2143 if( i )
2144 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, port, pwsi->buffer, 0,
2145 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYPORT | WSMSG_WIN32_AOP);
2147 return 0;
2151 /***********************************************************************
2152 * WSACancelAsyncRequest() (WINSOCK.108)(WSOCK32.109)
2154 INT32 WINAPI WSACancelAsyncRequest32(HANDLE32 hAsyncTaskHandle)
2156 INT32 retVal = SOCKET_ERROR;
2157 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2158 ws_async_op* p_aop = (ws_async_op*)WS_HANDLE2PTR(hAsyncTaskHandle);
2160 TRACE(winsock, "(%08x): handle %08x\n",
2161 (unsigned)pwsi, hAsyncTaskHandle);
2162 if( pwsi )
2164 SIGNAL_MaskAsyncEvents( TRUE ); /* block SIGIO */
2165 if( WINSOCK_cancel_async_op(p_aop) )
2167 WS_FREE(p_aop);
2168 pwsi->num_async_rq--;
2169 retVal = 0;
2171 else pwsi->err = WSAEINVAL;
2172 SIGNAL_MaskAsyncEvents( FALSE );
2174 return retVal;
2177 INT16 WINAPI WSACancelAsyncRequest16(HANDLE16 hAsyncTaskHandle)
2179 return (HANDLE16)WSACancelAsyncRequest32((HANDLE32)hAsyncTaskHandle);
2182 /***********************************************************************
2183 * WSAAsyncSelect() (WINSOCK.101)(WSOCK32.101)
2186 static ws_select_op* __ws_select_list = NULL;
2188 BOOL32 WINSOCK_HandleIO( int* max_fd, int num_pending,
2189 fd_set pending_set[3], fd_set event_set[3] )
2191 /* This function is called by the event dispatcher
2192 * with the pending_set[] containing the result of select() and
2193 * the event_set[] containing all fd that are being watched */
2195 ws_select_op* psop = __ws_select_list;
2196 BOOL32 bPost = FALSE;
2197 DWORD dwEvent, dwErrBytes;
2198 int num_posted;
2200 TRACE(winsock,"%i pending descriptors\n", num_pending );
2202 for( num_posted = dwEvent = 0 ; psop; psop = psop->next )
2204 unsigned flags = psop->pws->flags;
2205 int fd = psop->pws->fd;
2206 int r, w, e;
2208 w = 0;
2209 if( (r = FD_ISSET( fd, &pending_set[EVENT_IO_READ] )) ||
2210 (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] )) ||
2211 (e = FD_ISSET( fd, &pending_set[EVENT_IO_EXCEPT] )) )
2213 /* This code removes WS_FD flags on one-shot events (WS_FD_CLOSE,
2214 * WS_FD_CONNECT), otherwise it clears descriptors in the io_set.
2215 * Reenabling calls turn them back on.
2218 TRACE(winsock,"\tchecking psop = 0x%08x\n", (unsigned) psop );
2220 num_pending--;
2222 /* Now figure out what kind of event we've got. The worst problem
2223 * we have to contend with is that some out of control applications
2224 * really want to use mutually exclusive AsyncSelect() flags all at
2225 * the same time.
2228 if((flags & WS_FD_ACCEPT) && (flags & WS_FD_LISTENING))
2230 /* WS_FD_ACCEPT is valid only if the socket is in the
2231 * listening state */
2233 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2234 if( r )
2236 FD_CLR( fd, &event_set[EVENT_IO_READ] ); /* reenabled by the next accept() */
2237 dwEvent = WSAMAKESELECTREPLY( WS_FD_ACCEPT, 0 );
2238 bPost = TRUE;
2240 else continue;
2242 else if( flags & WS_FD_CONNECT )
2244 /* connecting socket */
2246 if( w || (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] )) )
2248 /* ready to write means that socket is connected
2250 * FIXME: Netscape calls AsyncSelect( s, ... WS_FD_CONNECT .. )
2251 * right after s = socket() and somehow "s" becomes writeable
2252 * before it goes through connect()!?!?
2255 psop->pws->flags |= WS_FD_CONNECTED;
2256 psop->pws->flags &= ~(WS_FD_CONNECT | WS_FD_INACTIVE);
2257 dwEvent = WSAMAKESELECTREPLY( WS_FD_CONNECT, 0 );
2259 if( flags & (WS_FD_READ | WS_FD_CLOSE))
2260 FD_SET( fd, &event_set[EVENT_IO_READ] );
2261 else
2262 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2263 if( flags & WS_FD_WRITE )
2264 FD_SET( fd, &event_set[EVENT_IO_WRITE] );
2265 else
2266 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2267 bPost = TRUE;
2269 else if( r )
2271 /* failure - do read() to get correct errno */
2273 if( read( fd, &dwErrBytes, sizeof(dwErrBytes) ) == -1 )
2275 dwEvent = WSAMAKESELECTREPLY( WS_FD_CONNECT, wsaErrno() );
2276 bPost = TRUE;
2279 /* otherwise bPost stays FALSE, should probably clear event_set */
2281 else
2283 /* connected socket, no WS_FD_OOB code for now. */
2285 if( flags & WS_FD_WRITE &&
2286 (w || (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] ))) )
2288 /* this will be reenabled when send() or sendto() fail with
2289 * WSAEWOULDBLOCK */
2291 if( PostMessage32A( psop->hWnd, psop->uMsg, (WPARAM32)WS_PTR2HANDLE(psop->pws),
2292 (LPARAM)WSAMAKESELECTREPLY( WS_FD_WRITE, 0 ) ) )
2294 TRACE(winsock, "\t hwnd %04x - %04x, %08x\n",
2295 psop->hWnd, psop->uMsg, (unsigned)MAKELONG(WS_FD_WRITE, 0) );
2296 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2297 num_posted++;
2301 if( r && (flags & (WS_FD_READ | WS_FD_CLOSE)) )
2303 int val = (flags & WS_FD_RAW);
2305 /* WS_FD_RAW is set by the WSAAsyncSelect() init */
2307 bPost = TRUE;
2308 if( !val && ioctl( fd, FIONREAD, (char*)&dwErrBytes) == -1 )
2310 /* weirdness */
2312 dwEvent = WSAMAKESELECTREPLY( WS_FD_READ, wsaErrno() );
2314 else if( val || dwErrBytes )
2316 /* got pending data, will be reenabled by recv() or recvfrom() */
2318 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2319 dwEvent = WSAMAKESELECTREPLY( WS_FD_READ, 0 );
2321 else
2323 /* 0 bytes to read - connection reset by peer? */
2326 val = read( fd, (char*)&dwErrBytes, sizeof(dwErrBytes));
2327 while( errno == EINTR );
2328 if( errno != EWOULDBLOCK )
2330 switch( val )
2332 case 0: errno = ENETDOWN; /* soft reset, fall through */
2333 case -1: /* hard reset */
2334 dwEvent = WSAMAKESELECTREPLY( WS_FD_CLOSE, wsaErrno() );
2335 break;
2337 default: bPost = FALSE;
2338 continue; /* FIXME: this is real bad */
2341 else { bPost = FALSE; continue; } /* more weirdness */
2343 /* this is it, this socket is closed */
2345 psop->pws->flags &= ~(WS_FD_READ | WS_FD_CLOSE | WS_FD_WRITE);
2346 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2347 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2349 if( *max_fd == (fd + 1) ) (*max_fd)--;
2354 if( bPost )
2356 TRACE(winsock, "\t hwnd %04x - %04x, %08x\n",
2357 psop->hWnd, psop->uMsg, (unsigned)dwEvent );
2358 PostMessage32A( psop->hWnd, psop->uMsg,
2359 (WPARAM32)WS_PTR2HANDLE(psop->pws), (LPARAM)dwEvent );
2360 bPost = FALSE;
2361 num_posted++;
2364 if( num_pending <= 0 ) break;
2367 TRACE(winsock, "\tdone, %i posted events\n", num_posted );
2368 return ( num_posted ) ? TRUE : FALSE;
2371 INT32 WINAPI WSAAsyncSelect32(SOCKET32 s, HWND32 hWnd, UINT32 uMsg, UINT32 lEvent)
2373 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
2374 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2376 TRACE(winsock, "(%08x): %04x, hWnd %04x, uMsg %08x, event %08x\n",
2377 (unsigned)pwsi, (SOCKET16)s, (HWND16)hWnd, uMsg, (unsigned)lEvent );
2378 if( _check_ws(pwsi, pws) )
2380 ws_select_op* psop;
2382 if( (psop = pws->psop) )
2384 /* delete previous control struct */
2386 if( psop == __ws_select_list )
2387 __ws_select_list = psop->next;
2388 else
2389 psop->prev->next = psop->next;
2390 if( psop->next ) psop->next->prev = psop->prev;
2392 if( pws->flags & (WS_FD_ACCEPT | WS_FD_CONNECT | WS_FD_READ | WS_FD_CLOSE) )
2393 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
2394 if( pws->flags & (WS_FD_CONNECT | WS_FD_WRITE) )
2395 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
2397 TRACE(winsock,"\tremoving psop = 0x%08x\n", (unsigned) psop );
2399 WS_FREE( pws->psop );
2400 pws->flags &= ~(WS_FD_RAW | WS_FD_ACCEPT | WS_FD_CONNECT |
2401 WS_FD_READ | WS_FD_WRITE | WS_FD_CLOSE);
2402 pws->psop = NULL;
2405 if( lEvent )
2407 psop = (ws_select_op*)WS_ALLOC(sizeof(ws_select_op));
2408 if( psop )
2410 int sock_type, bytes = sizeof(int);
2412 WINSOCK_unblock_io( pws->fd, TRUE );
2414 psop->prev = NULL;
2415 psop->next = __ws_select_list;
2416 if( __ws_select_list )
2417 __ws_select_list->prev = psop;
2418 __ws_select_list = psop;
2420 psop->pws = pws;
2421 psop->hWnd = hWnd;
2422 psop->uMsg = uMsg;
2424 pws->psop = psop;
2425 pws->flags |= (0x0000FFFF & lEvent);
2426 getsockopt(pws->fd, SOL_SOCKET, SO_TYPE, &sock_type, &bytes);
2427 if( sock_type == SOCK_RAW ) pws->flags |= WS_FD_RAW;
2429 if( lEvent & (WS_FD_ACCEPT | WS_FD_CONNECT | WS_FD_READ | WS_FD_CLOSE) )
2430 EVENT_AddIO( pws->fd, EVENT_IO_READ );
2431 if( lEvent & (WS_FD_CONNECT | WS_FD_WRITE) )
2432 EVENT_AddIO( pws->fd, EVENT_IO_WRITE );
2434 /* TODO: handle WS_FD_ACCEPT right away if the socket is readable */
2436 TRACE(winsock,"\tcreating psop = 0x%08x\n", (unsigned)psop );
2438 return 0; /* success */
2440 else pwsi->err = WSAENOBUFS;
2442 else return 0;
2444 else if( pwsi ) pwsi->err = WSAEINVAL;
2445 return SOCKET_ERROR;
2448 INT16 WINAPI WSAAsyncSelect16(SOCKET16 s, HWND16 hWnd, UINT16 wMsg, UINT32 lEvent)
2450 return (INT16)WSAAsyncSelect32( s, hWnd, wMsg, lEvent );
2454 /***********************************************************************
2455 * __WSAFDIsSet() (WINSOCK.151)
2457 INT16 WINAPI __WSAFDIsSet16(SOCKET16 s, ws_fd_set16 *set)
2459 int i = set->fd_count;
2461 TRACE(winsock, "(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2463 while (i--)
2464 if (set->fd_array[i] == s) return 1;
2465 return 0;
2468 /***********************************************************************
2469 * __WSAFDIsSet() (WSOCK32.151)
2471 INT32 WINAPI __WSAFDIsSet32(SOCKET32 s, ws_fd_set32 *set)
2473 int i = set->fd_count;
2475 TRACE(winsock, "(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2477 while (i--)
2478 if (set->fd_array[i] == s) return 1;
2479 return 0;
2482 /***********************************************************************
2483 * WSAIsBlocking() (WINSOCK.114)(WSOCK32.114)
2485 BOOL32 WINAPI WSAIsBlocking(void)
2487 /* By default WinSock should set all its sockets to non-blocking mode
2488 * and poll in PeekMessage loop when processing "blocking" ones. This
2489 * function is supposed to tell if the program is in this loop. Our
2490 * blocking calls are truly blocking so we always return FALSE.
2492 * Note: It is allowed to call this function without prior WSAStartup().
2495 TRACE(winsock, "\n");
2496 return FALSE;
2499 /***********************************************************************
2500 * WSACancelBlockingCall() (WINSOCK.113)(WSOCK32.113)
2502 INT32 WINAPI WSACancelBlockingCall(void)
2504 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2506 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2508 if( pwsi ) return 0;
2509 return SOCKET_ERROR;
2513 /***********************************************************************
2514 * WSASetBlockingHook16() (WINSOCK.109)
2516 FARPROC16 WINAPI WSASetBlockingHook16(FARPROC16 lpBlockFunc)
2518 FARPROC16 prev;
2519 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2521 TRACE(winsock, "(%08x): hook %08x\n",
2522 (unsigned)pwsi, (unsigned) lpBlockFunc);
2523 if( pwsi )
2525 prev = (FARPROC16)pwsi->blocking_hook;
2526 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2527 pwsi->flags &= ~WSI_BLOCKINGHOOK32;
2528 return prev;
2530 return 0;
2534 /***********************************************************************
2535 * WSASetBlockingHook32()
2537 FARPROC32 WINAPI WSASetBlockingHook32(FARPROC32 lpBlockFunc)
2539 FARPROC32 prev;
2540 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2542 TRACE(winsock, "(%08x): hook %08x\n",
2543 (unsigned)pwsi, (unsigned) lpBlockFunc);
2544 if( pwsi ) {
2545 prev = (FARPROC32)pwsi->blocking_hook;
2546 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2547 pwsi->flags |= WSI_BLOCKINGHOOK32;
2548 return prev;
2550 return NULL;
2554 /***********************************************************************
2555 * WSAUnhookBlockingHook16() (WINSOCK.110)
2557 INT16 WINAPI WSAUnhookBlockingHook16(void)
2559 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2561 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2562 if( pwsi ) return (INT16)(pwsi->blocking_hook = 0);
2563 return SOCKET_ERROR;
2567 /***********************************************************************
2568 * WSAUnhookBlockingHook32()
2570 INT32 WINAPI WSAUnhookBlockingHook32(void)
2572 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2574 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2575 if( pwsi )
2577 pwsi->blocking_hook = 0;
2578 pwsi->flags &= ~WSI_BLOCKINGHOOK32;
2579 return 0;
2581 return SOCKET_ERROR;
2585 * TCP/IP action codes.
2589 #define WSCNTL_TCPIP_QUERY_INFO 0x00000000
2590 #define WSCNTL_TCPIP_SET_INFO 0x00000001
2591 #define WSCNTL_TCPIP_ICMP_ECHO 0x00000002
2592 #define WSCNTL_TCPIP_TEST 0x00000003
2595 /***********************************************************************
2596 * WsControl()
2598 * WsControl seems to be an undocumented Win95 function. A lot of
2599 * discussion about WsControl can be found on the net, e.g.
2600 * Subject: Re: WSOCK32.DLL WsControl Exported Function
2601 * From: "Peter Rindfuss" <rindfuss-s@medea.wz-berlin.de>
2602 * Date: 1997/08/17
2605 DWORD WINAPI WsControl(DWORD protocoll,DWORD action,
2606 LPVOID inbuf,LPDWORD inbuflen,
2607 LPVOID outbuf,LPDWORD outbuflen)
2610 switch (action) {
2611 case WSCNTL_TCPIP_ICMP_ECHO:
2613 unsigned int addr = *(unsigned int*)inbuf;
2614 #if 0
2615 int timeout= *(unsigned int*)(inbuf+4);
2616 short x1 = *(unsigned short*)(inbuf+8);
2617 short sendbufsize = *(unsigned short*)(inbuf+10);
2618 char x2 = *(unsigned char*)(inbuf+12);
2619 char ttl = *(unsigned char*)(inbuf+13);
2620 char service = *(unsigned char*)(inbuf+14);
2621 char type= *(unsigned char*)(inbuf+15); /* 0x2: don't fragment*/
2622 #endif
2624 FIXME(winsock,"(ICMP_ECHO) to 0x%08x stub \n", addr);
2625 break;
2627 default:
2628 FIXME(winsock,"(%lx,%lx,%p,%p,%p,%p) stub\n",
2629 protocoll,action,inbuf,inbuflen,outbuf,outbuflen);
2631 return FALSE;
2633 /*********************************************************
2634 * WS_s_perror WSOCK32.1108
2636 void WINAPI WS_s_perror(LPCSTR message)
2638 FIXME(winsock,"(%s): stub\n",message);
2639 return;
2643 /* ----------------------------------- end of API stuff */
2645 /* ----------------------------------- helper functions -
2647 * TODO: Merge WS_dup_..() stuff into one function that
2648 * would operate with a generic structure containing internal
2649 * pointers (via a template of some kind).
2652 static int list_size(char** l, int item_size)
2654 int i,j = 0;
2655 if(l)
2656 { for(i=0;l[i];i++)
2657 j += (item_size) ? item_size : strlen(l[i]) + 1;
2658 j += (i + 1) * sizeof(char*); }
2659 return j;
2662 static int list_dup(char** l_src, char* ref, char* base, int item_size)
2664 /* base is either either equal to ref or 0 or SEGPTR */
2666 char* p = ref;
2667 char** l_to = (char**)ref;
2668 int i,j,k;
2670 for(j=0;l_src[j];j++) ;
2671 p += (j + 1) * sizeof(char*);
2672 for(i=0;i<j;i++)
2673 { l_to[i] = base + (p - ref);
2674 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
2675 memcpy(p, l_src[i], k); p += k; }
2676 l_to[i] = NULL;
2677 return (p - ref);
2680 /* ----- hostent */
2682 static int hostent_size(struct hostent* p_he)
2684 int size = 0;
2685 if( p_he )
2686 { size = sizeof(struct hostent);
2687 size += strlen(p_he->h_name) + 1;
2688 size += list_size(p_he->h_aliases, 0);
2689 size += list_size(p_he->h_addr_list, p_he->h_length ); }
2690 return size;
2693 int WS_dup_he(LPWSINFO pwsi, struct hostent* p_he, int flag)
2695 /* Convert hostent structure into ws_hostent so that the data fits
2696 * into pwsi->buffer. Internal pointers can be linear, SEGPTR, or
2697 * relative to pwsi->buffer depending on "flag" value. Returns size
2698 * of the data copied (also in the pwsi->buflen).
2701 int size = hostent_size(p_he);
2703 if( size )
2705 struct ws_hostent* p_to;
2706 char* p_name,*p_aliases,*p_addr,*p_base,*p;
2708 _check_buffer_he(pwsi, size);
2709 p_to = (struct ws_hostent*)pwsi->he;
2710 p = (char*)pwsi->he;
2711 p_base = (flag & WS_DUP_OFFSET) ? NULL
2712 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2713 p += sizeof(struct ws_hostent);
2714 p_name = p;
2715 strcpy(p, p_he->h_name); p += strlen(p) + 1;
2716 p_aliases = p;
2717 p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)pwsi->he), 0);
2718 p_addr = p;
2719 list_dup(p_he->h_addr_list, p, p_base + (p - (char*)pwsi->he), p_he->h_length);
2721 p_to->h_addrtype = (INT16)p_he->h_addrtype;
2722 p_to->h_length = (INT16)p_he->h_length;
2723 p_to->h_name = (SEGPTR)(p_base + (p_name - (char*)pwsi->he));
2724 p_to->h_aliases = (SEGPTR)(p_base + (p_aliases - (char*)pwsi->he));
2725 p_to->h_addr_list = (SEGPTR)(p_base + (p_addr - (char*)pwsi->he));
2727 size += (sizeof(struct ws_hostent) - sizeof(struct hostent));
2729 return size;
2732 /* ----- protoent */
2734 static int protoent_size(struct protoent* p_pe)
2736 int size = 0;
2737 if( p_pe )
2738 { size = sizeof(struct protoent);
2739 size += strlen(p_pe->p_name) + 1;
2740 size += list_size(p_pe->p_aliases, 0); }
2741 return size;
2744 int WS_dup_pe(LPWSINFO pwsi, struct protoent* p_pe, int flag)
2746 int size = protoent_size(p_pe);
2747 if( size )
2749 struct ws_protoent* p_to;
2750 char* p_name,*p_aliases,*p_base,*p;
2752 _check_buffer_pe(pwsi, size);
2753 p_to = (struct ws_protoent*)pwsi->pe;
2754 p = (char*)pwsi->pe;
2755 p_base = (flag & WS_DUP_OFFSET) ? NULL
2756 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2757 p += sizeof(struct ws_protoent);
2758 p_name = p;
2759 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
2760 p_aliases = p;
2761 list_dup(p_pe->p_aliases, p, p_base + (p - (char*)pwsi->pe), 0);
2763 p_to->p_proto = (INT16)p_pe->p_proto;
2764 p_to->p_name = (SEGPTR)(p_base) + (p_name - (char*)pwsi->pe);
2765 p_to->p_aliases = (SEGPTR)((p_base) + (p_aliases - (char*)pwsi->pe));
2767 size += (sizeof(struct ws_protoent) - sizeof(struct protoent));
2769 return size;
2772 /* ----- servent */
2774 static int servent_size(struct servent* p_se)
2776 int size = 0;
2777 if( p_se )
2778 { size += sizeof(struct servent);
2779 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
2780 size += list_size(p_se->s_aliases, 0); }
2781 return size;
2784 int WS_dup_se(LPWSINFO pwsi, struct servent* p_se, int flag)
2786 int size = servent_size(p_se);
2787 if( size )
2789 struct ws_servent* p_to;
2790 char* p_name,*p_aliases,*p_proto,*p_base,*p;
2792 _check_buffer_se(pwsi, size);
2793 p_to = (struct ws_servent*)pwsi->se;
2794 p = (char*)pwsi->se;
2795 p_base = (flag & WS_DUP_OFFSET) ? NULL
2796 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2797 p += sizeof(struct ws_servent);
2798 p_name = p;
2799 strcpy(p, p_se->s_name); p += strlen(p) + 1;
2800 p_proto = p;
2801 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
2802 p_aliases = p;
2803 list_dup(p_se->s_aliases, p, p_base + (p - (char*)pwsi->se), 0);
2805 p_to->s_port = (INT16)p_se->s_port;
2806 p_to->s_name = (SEGPTR)(p_base + (p_name - (char*)pwsi->se));
2807 p_to->s_proto = (SEGPTR)(p_base + (p_proto - (char*)pwsi->se));
2808 p_to->s_aliases = (SEGPTR)(p_base + (p_aliases - (char*)pwsi->se));
2810 size += (sizeof(struct ws_servent) - sizeof(struct servent));
2812 return size;
2815 /* ----------------------------------- error handling */
2817 UINT16 wsaErrno(void)
2819 int loc_errno = errno;
2820 #ifdef HAVE_STRERROR
2821 WARN(winsock, "errno %d, (%s).\n", loc_errno, strerror(loc_errno));
2822 #else
2823 WARN(winsock, "errno %d\n", loc_errno);
2824 #endif
2826 switch(loc_errno)
2828 case EINTR: return WSAEINTR;
2829 case EBADF: return WSAEBADF;
2830 case EACCES: return WSAEACCES;
2831 case EFAULT: return WSAEFAULT;
2832 case EINVAL: return WSAEINVAL;
2833 case EMFILE: return WSAEMFILE;
2834 case EWOULDBLOCK: return WSAEWOULDBLOCK;
2835 case EINPROGRESS: return WSAEINPROGRESS;
2836 case EALREADY: return WSAEALREADY;
2837 case ENOTSOCK: return WSAENOTSOCK;
2838 case EDESTADDRREQ: return WSAEDESTADDRREQ;
2839 case EMSGSIZE: return WSAEMSGSIZE;
2840 case EPROTOTYPE: return WSAEPROTOTYPE;
2841 case ENOPROTOOPT: return WSAENOPROTOOPT;
2842 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
2843 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
2844 case EOPNOTSUPP: return WSAEOPNOTSUPP;
2845 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
2846 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
2847 case EADDRINUSE: return WSAEADDRINUSE;
2848 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
2849 case ENETDOWN: return WSAENETDOWN;
2850 case ENETUNREACH: return WSAENETUNREACH;
2851 case ENETRESET: return WSAENETRESET;
2852 case ECONNABORTED: return WSAECONNABORTED;
2853 case EPIPE:
2854 case ECONNRESET: return WSAECONNRESET;
2855 case ENOBUFS: return WSAENOBUFS;
2856 case EISCONN: return WSAEISCONN;
2857 case ENOTCONN: return WSAENOTCONN;
2858 case ESHUTDOWN: return WSAESHUTDOWN;
2859 case ETOOMANYREFS: return WSAETOOMANYREFS;
2860 case ETIMEDOUT: return WSAETIMEDOUT;
2861 case ECONNREFUSED: return WSAECONNREFUSED;
2862 case ELOOP: return WSAELOOP;
2863 case ENAMETOOLONG: return WSAENAMETOOLONG;
2864 case EHOSTDOWN: return WSAEHOSTDOWN;
2865 case EHOSTUNREACH: return WSAEHOSTUNREACH;
2866 case ENOTEMPTY: return WSAENOTEMPTY;
2867 #ifdef EPROCLIM
2868 case EPROCLIM: return WSAEPROCLIM;
2869 #endif
2870 #ifdef EUSERS
2871 case EUSERS: return WSAEUSERS;
2872 #endif
2873 #ifdef EDQUOT
2874 case EDQUOT: return WSAEDQUOT;
2875 #endif
2876 #ifdef ESTALE
2877 case ESTALE: return WSAESTALE;
2878 #endif
2879 #ifdef EREMOTE
2880 case EREMOTE: return WSAEREMOTE;
2881 #endif
2883 /* just in case we ever get here and there are no problems */
2884 case 0: return 0;
2885 default:
2886 WARN(winsock, "Unknown errno %d!\n", loc_errno);
2887 return WSAEOPNOTSUPP;
2891 UINT16 wsaHerrno(void)
2893 int loc_errno = h_errno;
2895 WARN(winsock, "h_errno %d.\n", loc_errno);
2897 switch(loc_errno)
2899 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
2900 case TRY_AGAIN: return WSATRY_AGAIN;
2901 case NO_RECOVERY: return WSANO_RECOVERY;
2902 case NO_DATA: return WSANO_DATA;
2904 case 0: return 0;
2905 default:
2906 WARN(winsock,"Unknown h_errno %d!\n", loc_errno);
2907 return WSAEOPNOTSUPP;