Small fixes.
[wine/multimedia.git] / misc / winsock.c
blob3f14b503585efcfac98fc563da40405ff89b381d
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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_LINUX_IPX_H
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) {
1481 /* yes, uses unsigned short in both win16/win32 */
1482 linger.l_onoff = ((UINT16*)optval)[0];
1483 linger.l_linger = ((UINT16*)optval)[1];
1484 optval = (char*)&linger;
1485 optlen = sizeof(struct linger);
1487 if (setsockopt(pws->fd, level, optname, optval, optlen) == 0) return 0;
1488 pwsi->err = wsaErrno();
1490 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1491 return SOCKET_ERROR;
1494 /***********************************************************************
1495 * setsockopt() (WINSOCK.21)
1497 INT16 WINAPI WINSOCK_setsockopt16(SOCKET16 s, INT16 level, INT16 optname,
1498 char *optval, INT16 optlen)
1500 if( !optval ) return SOCKET_ERROR;
1501 return (INT16)WINSOCK_setsockopt32( s, (UINT16)level, optname, optval, optlen );
1505 /***********************************************************************
1506 * shutdown() (WSOCK32.22)
1508 INT32 WINAPI WINSOCK_shutdown32(SOCKET32 s, INT32 how)
1510 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1511 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1513 TRACE(winsock, "(%08x): socket %04x, how %i\n",
1514 (unsigned)pwsi, s, how );
1515 if( _check_ws(pwsi, pws) )
1517 if( pws->psop )
1518 switch( how )
1520 case 0: /* drop receives */
1521 if( pws->flags & (WS_FD_READ | WS_FD_CLOSE) )
1522 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
1523 pws->flags &= ~(WS_FD_READ | WS_FD_CLOSE);
1524 #ifdef SHUT_RD
1525 how = SHUT_RD;
1526 #endif
1527 break;
1529 case 1: /* drop sends */
1530 if( pws->flags & WS_FD_WRITE )
1531 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
1532 pws->flags &= ~WS_FD_WRITE;
1533 #ifdef SHUT_WR
1534 how = SHUT_WR;
1535 #endif
1536 break;
1538 case 2: /* drop all */
1539 #ifdef SHUT_RDWR
1540 how = SHUT_RDWR;
1541 #endif
1542 default:
1543 WSAAsyncSelect32( s, 0, 0, 0 );
1544 break;
1547 if (shutdown(pws->fd, how) == 0)
1549 if( how > 1 )
1551 pws->flags &= ~(WS_FD_CONNECTED | WS_FD_LISTENING);
1552 pws->flags |= WS_FD_INACTIVE;
1554 return 0;
1556 pwsi->err = wsaErrno();
1558 else if( pwsi ) pwsi->err = WSAENOTSOCK;
1559 return SOCKET_ERROR;
1562 /***********************************************************************
1563 * shutdown() (WINSOCK.22)
1565 INT16 WINAPI WINSOCK_shutdown16(SOCKET16 s, INT16 how)
1567 return (INT16)WINSOCK_shutdown32( s, how );
1571 /***********************************************************************
1572 * socket() (WSOCK32.23)
1574 SOCKET32 WINAPI WINSOCK_socket32(INT32 af, INT32 type, INT32 protocol)
1576 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1578 TRACE(winsock, "(%08x): af=%d type=%d protocol=%d\n",
1579 (unsigned)pwsi, af, type, protocol);
1581 if( pwsi )
1583 int sock;
1585 /* check the socket family */
1586 switch(af)
1588 #ifdef HAVE_LINUX_IPX_H
1589 case WS_AF_IPX: af = AF_IPX;
1590 #endif
1591 case AF_INET:
1592 case AF_UNSPEC: break;
1593 default: pwsi->err = WSAEAFNOSUPPORT;
1594 return INVALID_SOCKET32;
1597 /* check the socket type */
1598 switch(type)
1600 case SOCK_STREAM:
1601 case SOCK_DGRAM:
1602 case SOCK_RAW: break;
1603 default: pwsi->err = WSAESOCKTNOSUPPORT;
1604 return INVALID_SOCKET32;
1607 /* check the protocol type */
1608 if ( protocol < 0 ) /* don't support negative values */
1609 { pwsi->err = WSAEPROTONOSUPPORT; return INVALID_SOCKET32; }
1611 if ( af == AF_UNSPEC) /* did they not specify the address family? */
1612 switch(protocol)
1614 case IPPROTO_TCP:
1615 if (type == SOCK_STREAM) { af = AF_INET; break; }
1616 case IPPROTO_UDP:
1617 if (type == SOCK_DGRAM) { af = AF_INET; break; }
1618 default: pwsi->err = WSAEPROTOTYPE; return INVALID_SOCKET32;
1621 if ((sock = socket(af, type, protocol)) >= 0)
1623 ws_socket* pnew = wsi_alloc_socket(pwsi, sock);
1625 TRACE(winsock,"\tcreated %i (handle %04x)\n", sock, (UINT16)WS_PTR2HANDLE(pnew));
1627 if( pnew )
1629 pnew->flags |= WS_FD_INACTIVE;
1630 return (SOCKET16)WS_PTR2HANDLE(pnew);
1633 close(sock);
1634 pwsi->err = WSAENOBUFS;
1635 return INVALID_SOCKET32;
1638 if (errno == EPERM) /* raw socket denied */
1640 WARN(winsock, "WS_SOCKET: not enough privileges\n");
1641 pwsi->err = WSAESOCKTNOSUPPORT;
1642 } else pwsi->err = wsaErrno();
1645 WARN(winsock, "\t\tfailed!\n");
1646 return INVALID_SOCKET32;
1649 /***********************************************************************
1650 * socket() (WINSOCK.23)
1652 SOCKET16 WINAPI WINSOCK_socket16(INT16 af, INT16 type, INT16 protocol)
1654 return (SOCKET16)WINSOCK_socket32( af, type, protocol );
1658 /* ----------------------------------- DNS services
1660 * IMPORTANT: 16-bit API structures have SEGPTR pointers inside them.
1661 * Also, we have to use wsock32 stubs to convert structures and
1662 * error codes from Unix to WSA, hence there is no direct mapping in
1663 * the relay32/wsock32.spec.
1666 static char* NULL_STRING = "NULL";
1668 /***********************************************************************
1669 * gethostbyaddr() (WINSOCK.51)(WSOCK32.51)
1671 static struct WIN_hostent* __ws_gethostbyaddr(const char *addr, int len, int type, int dup_flag)
1673 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1675 if( pwsi )
1677 struct hostent* host;
1678 if( (host = gethostbyaddr(addr, len, type)) != NULL )
1679 if( WS_dup_he(pwsi, host, dup_flag) )
1680 return (struct WIN_hostent*)(pwsi->he);
1681 else
1682 pwsi->err = WSAENOBUFS;
1683 else
1684 pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1686 return NULL;
1689 SEGPTR WINAPI WINSOCK_gethostbyaddr16(const char *addr, INT16 len, INT16 type)
1691 struct WIN_hostent* retval;
1692 TRACE(winsock, "ptr %08x, len %d, type %d\n",
1693 (unsigned) addr, len, type);
1694 retval = __ws_gethostbyaddr( addr, len, type, WS_DUP_SEGPTR );
1695 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1698 struct WIN_hostent* WINAPI WINSOCK_gethostbyaddr32(const char *addr, INT32 len,
1699 INT32 type)
1701 TRACE(winsock, "ptr %08x, len %d, type %d\n",
1702 (unsigned) addr, len, type);
1703 return __ws_gethostbyaddr(addr, len, type, WS_DUP_LINEAR);
1706 /***********************************************************************
1707 * gethostbyname() (WINSOCK.52)(WSOCK32.52)
1709 static struct WIN_hostent * __ws_gethostbyname(const char *name, int dup_flag)
1711 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1713 if( pwsi )
1715 struct hostent* host;
1716 if( (host = gethostbyname(name)) != NULL )
1717 if( WS_dup_he(pwsi, host, dup_flag) )
1718 return (struct WIN_hostent*)(pwsi->he);
1719 else pwsi->err = WSAENOBUFS;
1720 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1722 return NULL;
1725 SEGPTR WINAPI WINSOCK_gethostbyname16(const char *name)
1727 struct WIN_hostent* retval;
1728 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1729 retval = __ws_gethostbyname( name, WS_DUP_SEGPTR );
1730 return (retval)? SEGPTR_GET(retval) : ((SEGPTR)NULL) ;
1733 struct WIN_hostent* WINAPI WINSOCK_gethostbyname32(const char* name)
1735 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1736 return __ws_gethostbyname( name, WS_DUP_LINEAR );
1740 /***********************************************************************
1741 * getprotobyname() (WINSOCK.53)(WSOCK32.53)
1743 static struct WIN_protoent* __ws_getprotobyname(const char *name, int dup_flag)
1745 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1747 if( pwsi )
1749 struct protoent* proto;
1750 if( (proto = getprotobyname(name)) != NULL )
1751 if( WS_dup_pe(pwsi, proto, dup_flag) )
1752 return (struct WIN_protoent*)(pwsi->pe);
1753 else pwsi->err = WSAENOBUFS;
1754 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1756 return NULL;
1759 SEGPTR WINAPI WINSOCK_getprotobyname16(const char *name)
1761 struct WIN_protoent* retval;
1762 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1763 retval = __ws_getprotobyname(name, WS_DUP_SEGPTR);
1764 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1767 struct WIN_protoent* WINAPI WINSOCK_getprotobyname32(const char* name)
1769 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1770 return __ws_getprotobyname(name, WS_DUP_LINEAR);
1774 /***********************************************************************
1775 * getprotobynumber() (WINSOCK.54)(WSOCK32.54)
1777 static struct WIN_protoent* __ws_getprotobynumber(int number, int dup_flag)
1779 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1781 if( pwsi )
1783 struct protoent* proto;
1784 if( (proto = getprotobynumber(number)) != NULL )
1785 if( WS_dup_pe(pwsi, proto, dup_flag) )
1786 return (struct WIN_protoent*)(pwsi->pe);
1787 else pwsi->err = WSAENOBUFS;
1788 else pwsi->err = WSANO_DATA;
1790 return NULL;
1793 SEGPTR WINAPI WINSOCK_getprotobynumber16(INT16 number)
1795 struct WIN_protoent* retval;
1796 TRACE(winsock, "%i\n", number);
1797 retval = __ws_getprotobynumber(number, WS_DUP_SEGPTR);
1798 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1801 struct WIN_protoent* WINAPI WINSOCK_getprotobynumber32(INT32 number)
1803 TRACE(winsock, "%i\n", number);
1804 return __ws_getprotobynumber(number, WS_DUP_LINEAR);
1808 /***********************************************************************
1809 * getservbyname() (WINSOCK.55)(WSOCK32.55)
1811 struct WIN_servent* __ws_getservbyname(const char *name, const char *proto, int dup_flag)
1813 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1815 if( pwsi )
1817 struct servent* serv;
1818 int i = wsi_strtolo( pwsi, name, proto );
1820 if( i )
1821 if( (serv = getservbyname(pwsi->buffer, pwsi->buffer + i)) != NULL )
1822 if( WS_dup_se(pwsi, serv, dup_flag) )
1823 return (struct WIN_servent*)(pwsi->se);
1824 else pwsi->err = WSAENOBUFS;
1825 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1826 else pwsi->err = WSAENOBUFS;
1828 return NULL;
1831 SEGPTR WINAPI WINSOCK_getservbyname16(const char *name, const char *proto)
1833 struct WIN_servent* retval;
1834 TRACE(winsock, "'%s', '%s'\n",
1835 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
1836 retval = __ws_getservbyname(name, proto, WS_DUP_SEGPTR);
1837 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1840 struct WIN_servent* WINAPI WINSOCK_getservbyname32(const char *name, const char *proto)
1842 TRACE(winsock, "'%s', '%s'\n",
1843 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
1844 return __ws_getservbyname(name, proto, WS_DUP_LINEAR);
1848 /***********************************************************************
1849 * getservbyport() (WINSOCK.56)(WSOCK32.56)
1851 static struct WIN_servent* __ws_getservbyport(int port, const char* proto, int dup_flag)
1853 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1855 if( pwsi )
1857 struct servent* serv;
1858 int i = wsi_strtolo( pwsi, proto, NULL );
1860 if( i )
1861 if( (serv = getservbyport(port, pwsi->buffer)) != NULL )
1862 if( WS_dup_se(pwsi, serv, dup_flag) )
1863 return (struct WIN_servent*)(pwsi->se);
1864 else pwsi->err = WSAENOBUFS;
1865 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1866 else pwsi->err = WSAENOBUFS;
1868 return NULL;
1871 SEGPTR WINAPI WINSOCK_getservbyport16(INT16 port, const char *proto)
1873 struct WIN_servent* retval;
1874 TRACE(winsock, "%i, '%s'\n",
1875 (int)port, (proto)?proto:NULL_STRING);
1876 retval = __ws_getservbyport(port, proto, WS_DUP_SEGPTR);
1877 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1880 struct WIN_servent* WINAPI WINSOCK_getservbyport32(INT32 port, const char *proto)
1882 TRACE(winsock, "%i, '%s'\n",
1883 (int)port, (proto)?proto:NULL_STRING);
1884 return __ws_getservbyport(port, proto, WS_DUP_LINEAR);
1888 /***********************************************************************
1889 * gethostname() (WSOCK32.57)
1891 INT32 WINAPI WINSOCK_gethostname32(char *name, INT32 namelen)
1893 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1895 TRACE(winsock, "(%08x): name %s, len %d\n",
1896 (unsigned)pwsi, (name)?name:NULL_STRING, namelen);
1897 if( pwsi )
1899 if (gethostname(name, namelen) == 0) return 0;
1900 pwsi->err = (errno == EINVAL) ? WSAEFAULT : wsaErrno();
1902 return SOCKET_ERROR;
1905 /***********************************************************************
1906 * gethostname() (WINSOCK.57)
1908 INT16 WINAPI WINSOCK_gethostname16(char *name, INT16 namelen)
1910 return (INT16)WINSOCK_gethostname32(name, namelen);
1914 /* ------------------------------------- Windows sockets extensions -- *
1916 * ------------------------------------------------------------------- */
1919 /***********************************************************************
1920 * WSAAsyncGetHostByAddr() (WINSOCK.102)
1922 HANDLE16 WINAPI WSAAsyncGetHostByAddr16(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,
1923 INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen)
1925 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1927 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, addr %08x[%i]\n",
1928 (unsigned)pwsi, hWnd, uMsg, (unsigned)addr , len );
1930 if( pwsi )
1931 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, type, addr, len,
1932 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYADDR );
1933 return 0;
1936 /***********************************************************************
1937 * WSAAsyncGetHostByAddr() (WSOCK32.102)
1939 HANDLE32 WINAPI WSAAsyncGetHostByAddr32(HWND32 hWnd, UINT32 uMsg, LPCSTR addr,
1940 INT32 len, INT32 type, LPSTR sbuf, INT32 buflen)
1942 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1944 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, addr %08x[%i]\n",
1945 (unsigned)pwsi, (HWND16)hWnd, uMsg, (unsigned)addr , len );
1947 if( pwsi )
1948 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, type, addr, len,
1949 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYADDR | WSMSG_WIN32_AOP);
1950 return 0;
1954 /***********************************************************************
1955 * WSAAsyncGetHostByName() (WINSOCK.103)
1957 HANDLE16 WINAPI WSAAsyncGetHostByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
1958 SEGPTR sbuf, INT16 buflen)
1960 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1962 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, host %s,
1963 buffer %i\n", (unsigned)pwsi, hWnd, uMsg, (name)?name:NULL_STRING, (int)buflen );
1965 if( pwsi )
1966 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
1967 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYNAME );
1968 return 0;
1971 /***********************************************************************
1972 * WSAAsyncGetHostByName32() (WSOCK32.103)
1974 HANDLE32 WINAPI WSAAsyncGetHostByName32(HWND32 hWnd, UINT32 uMsg, LPCSTR name,
1975 LPSTR sbuf, INT32 buflen)
1977 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1978 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, host %s, buffer %i\n",
1979 (unsigned)pwsi, (HWND16)hWnd, uMsg,
1980 (name)?name:NULL_STRING, (int)buflen );
1981 if( pwsi )
1982 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
1983 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_HOSTBYNAME | WSMSG_WIN32_AOP);
1984 return 0;
1988 /***********************************************************************
1989 * WSAAsyncGetProtoByName() (WINSOCK.105)
1991 HANDLE16 WINAPI WSAAsyncGetProtoByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
1992 SEGPTR sbuf, INT16 buflen)
1994 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1996 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, protocol %s\n",
1997 (unsigned)pwsi, (HWND16)hWnd, uMsg, (name)?name:NULL_STRING );
1999 if( pwsi )
2000 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
2001 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNAME );
2002 return 0;
2005 /***********************************************************************
2006 * WSAAsyncGetProtoByName() (WSOCK32.105)
2008 HANDLE32 WINAPI WSAAsyncGetProtoByName32(HWND32 hWnd, UINT32 uMsg, LPCSTR name,
2009 LPSTR sbuf, INT32 buflen)
2011 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2013 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, protocol %s\n",
2014 (unsigned)pwsi, (HWND16)hWnd, uMsg, (name)?name:NULL_STRING );
2016 if( pwsi )
2017 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, name, 0,
2018 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNAME | WSMSG_WIN32_AOP);
2019 return 0;
2023 /***********************************************************************
2024 * WSAAsyncGetProtoByNumber() (WINSOCK.104)
2026 HANDLE16 WINAPI WSAAsyncGetProtoByNumber16(HWND16 hWnd, UINT16 uMsg, INT16 number,
2027 SEGPTR sbuf, INT16 buflen)
2029 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2031 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, num %i\n",
2032 (unsigned)pwsi, hWnd, uMsg, number );
2034 if( pwsi )
2035 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, number, NULL, 0,
2036 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNUM );
2037 return 0;
2040 /***********************************************************************
2041 * WSAAsyncGetProtoByNumber() (WSOCK32.104)
2043 HANDLE32 WINAPI WSAAsyncGetProtoByNumber32(HWND32 hWnd, UINT32 uMsg, INT32 number,
2044 LPSTR sbuf, INT32 buflen)
2046 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2048 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, num %i\n",
2049 (unsigned)pwsi, (HWND16)hWnd, uMsg, number );
2051 if( pwsi )
2052 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, number, NULL, 0,
2053 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_PROTOBYNUM | WSMSG_WIN32_AOP);
2054 return 0;
2058 /***********************************************************************
2059 * WSAAsyncGetServByName() (WINSOCK.107)
2061 HANDLE16 WINAPI WSAAsyncGetServByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
2062 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
2064 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2066 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, name %s, proto %s\n",
2067 (unsigned)pwsi, hWnd, uMsg,
2068 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING );
2070 if( pwsi )
2072 int i = wsi_strtolo( pwsi, name, proto );
2074 if( i )
2075 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, pwsi->buffer, 0,
2076 pwsi->buffer + i, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYNAME );
2078 return 0;
2081 /***********************************************************************
2082 * WSAAsyncGetServByName() (WSOCK32.107)
2084 HANDLE32 WINAPI WSAAsyncGetServByName32(HWND32 hWnd, UINT32 uMsg, LPCSTR name,
2085 LPCSTR proto, LPSTR sbuf, INT32 buflen)
2087 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2089 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, name %s, proto %s\n",
2090 (unsigned)pwsi, (HWND16)hWnd, uMsg,
2091 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING );
2092 if( pwsi )
2094 int i = wsi_strtolo( pwsi, name, proto );
2096 if( i )
2097 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, 0, pwsi->buffer, 0,
2098 pwsi->buffer + i, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYNAME | WSMSG_WIN32_AOP);
2100 return 0;
2104 /***********************************************************************
2105 * WSAAsyncGetServByPort() (WINSOCK.106)
2107 HANDLE16 WINAPI WSAAsyncGetServByPort16(HWND16 hWnd, UINT16 uMsg, INT16 port,
2108 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
2110 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2112 TRACE(winsock, "(%08x): hwnd %04x, msg %04x, port %i, proto %s\n",
2113 (unsigned)pwsi, hWnd, uMsg, port, (proto)?proto:NULL_STRING );
2115 if( pwsi )
2117 int i = wsi_strtolo( pwsi, proto, NULL );
2119 if( i )
2120 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, port, pwsi->buffer, 0,
2121 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYPORT );
2123 return 0;
2126 /***********************************************************************
2127 * WSAAsyncGetServByPort() (WSOCK32.106)
2129 HANDLE32 WINAPI WSAAsyncGetServByPort32(HWND32 hWnd, UINT32 uMsg, INT32 port,
2130 LPCSTR proto, LPSTR sbuf, INT32 buflen)
2132 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2134 TRACE(winsock, "(%08x): hwnd %04x, msg %08x, port %i, proto %s\n",
2135 (unsigned)pwsi, (HWND16)hWnd, uMsg, port, (proto)?proto:NULL_STRING );
2137 if( pwsi )
2139 int i = wsi_strtolo( pwsi, proto, NULL );
2141 if( i )
2142 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, port, pwsi->buffer, 0,
2143 NULL, (void*)sbuf, buflen, WSMSG_ASYNC_SERVBYPORT | WSMSG_WIN32_AOP);
2145 return 0;
2149 /***********************************************************************
2150 * WSACancelAsyncRequest() (WINSOCK.108)(WSOCK32.109)
2152 INT32 WINAPI WSACancelAsyncRequest32(HANDLE32 hAsyncTaskHandle)
2154 INT32 retVal = SOCKET_ERROR;
2155 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2156 ws_async_op* p_aop = (ws_async_op*)WS_HANDLE2PTR(hAsyncTaskHandle);
2158 TRACE(winsock, "(%08x): handle %08x\n",
2159 (unsigned)pwsi, hAsyncTaskHandle);
2160 if( pwsi )
2162 SIGNAL_MaskAsyncEvents( TRUE ); /* block SIGIO */
2163 if( WINSOCK_cancel_async_op(p_aop) )
2165 WS_FREE(p_aop);
2166 pwsi->num_async_rq--;
2167 retVal = 0;
2169 else pwsi->err = WSAEINVAL;
2170 SIGNAL_MaskAsyncEvents( FALSE );
2172 return retVal;
2175 INT16 WINAPI WSACancelAsyncRequest16(HANDLE16 hAsyncTaskHandle)
2177 return (HANDLE16)WSACancelAsyncRequest32((HANDLE32)hAsyncTaskHandle);
2180 /***********************************************************************
2181 * WSAAsyncSelect() (WINSOCK.101)(WSOCK32.101)
2184 static ws_select_op* __ws_select_list = NULL;
2186 BOOL32 WINSOCK_HandleIO( int* max_fd, int num_pending,
2187 fd_set pending_set[3], fd_set event_set[3] )
2189 /* This function is called by the event dispatcher
2190 * with the pending_set[] containing the result of select() and
2191 * the event_set[] containing all fd that are being watched */
2193 ws_select_op* psop = __ws_select_list;
2194 BOOL32 bPost = FALSE;
2195 DWORD dwEvent, dwErrBytes;
2196 int num_posted;
2198 TRACE(winsock,"%i pending descriptors\n", num_pending );
2200 for( num_posted = dwEvent = 0 ; psop; psop = psop->next )
2202 unsigned flags = psop->pws->flags;
2203 int fd = psop->pws->fd;
2204 int r, w, e;
2206 w = 0;
2207 if( (r = FD_ISSET( fd, &pending_set[EVENT_IO_READ] )) ||
2208 (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] )) ||
2209 (e = FD_ISSET( fd, &pending_set[EVENT_IO_EXCEPT] )) )
2211 /* This code removes WS_FD flags on one-shot events (WS_FD_CLOSE,
2212 * WS_FD_CONNECT), otherwise it clears descriptors in the io_set.
2213 * Reenabling calls turn them back on.
2216 TRACE(winsock,"\tchecking psop = 0x%08x\n", (unsigned) psop );
2218 num_pending--;
2220 /* Now figure out what kind of event we've got. The worst problem
2221 * we have to contend with is that some out of control applications
2222 * really want to use mutually exclusive AsyncSelect() flags all at
2223 * the same time.
2226 if((flags & WS_FD_ACCEPT) && (flags & WS_FD_LISTENING))
2228 /* WS_FD_ACCEPT is valid only if the socket is in the
2229 * listening state */
2231 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2232 if( r )
2234 FD_CLR( fd, &event_set[EVENT_IO_READ] ); /* reenabled by the next accept() */
2235 dwEvent = WSAMAKESELECTREPLY( WS_FD_ACCEPT, 0 );
2236 bPost = TRUE;
2238 else continue;
2240 else if( flags & WS_FD_CONNECT )
2242 /* connecting socket */
2244 if( w || (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] )) )
2246 /* ready to write means that socket is connected
2248 * FIXME: Netscape calls AsyncSelect( s, ... WS_FD_CONNECT .. )
2249 * right after s = socket() and somehow "s" becomes writeable
2250 * before it goes through connect()!?!?
2253 psop->pws->flags |= WS_FD_CONNECTED;
2254 psop->pws->flags &= ~(WS_FD_CONNECT | WS_FD_INACTIVE);
2255 dwEvent = WSAMAKESELECTREPLY( WS_FD_CONNECT, 0 );
2257 if( flags & (WS_FD_READ | WS_FD_CLOSE))
2258 FD_SET( fd, &event_set[EVENT_IO_READ] );
2259 else
2260 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2261 if( flags & WS_FD_WRITE )
2262 FD_SET( fd, &event_set[EVENT_IO_WRITE] );
2263 else
2264 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2265 bPost = TRUE;
2267 else if( r )
2269 /* failure - do read() to get correct errno */
2271 if( read( fd, &dwErrBytes, sizeof(dwErrBytes) ) == -1 )
2273 dwEvent = WSAMAKESELECTREPLY( WS_FD_CONNECT, wsaErrno() );
2274 bPost = TRUE;
2277 /* otherwise bPost stays FALSE, should probably clear event_set */
2279 else
2281 /* connected socket, no WS_FD_OOB code for now. */
2283 if( flags & WS_FD_WRITE &&
2284 (w || (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] ))) )
2286 /* this will be reenabled when send() or sendto() fail with
2287 * WSAEWOULDBLOCK */
2289 if( PostMessage32A( psop->hWnd, psop->uMsg, (WPARAM32)WS_PTR2HANDLE(psop->pws),
2290 (LPARAM)WSAMAKESELECTREPLY( WS_FD_WRITE, 0 ) ) )
2292 TRACE(winsock, "\t hwnd %04x - %04x, %08x\n",
2293 psop->hWnd, psop->uMsg, (unsigned)MAKELONG(WS_FD_WRITE, 0) );
2294 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2295 num_posted++;
2299 if( r && (flags & (WS_FD_READ | WS_FD_CLOSE)) )
2301 int val = (flags & WS_FD_RAW);
2303 /* WS_FD_RAW is set by the WSAAsyncSelect() init */
2305 bPost = TRUE;
2306 if( !val && ioctl( fd, FIONREAD, (char*)&dwErrBytes) == -1 )
2308 /* weirdness */
2310 dwEvent = WSAMAKESELECTREPLY( WS_FD_READ, wsaErrno() );
2312 else if( val || dwErrBytes )
2314 /* got pending data, will be reenabled by recv() or recvfrom() */
2316 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2317 dwEvent = WSAMAKESELECTREPLY( WS_FD_READ, 0 );
2319 else
2321 /* 0 bytes to read - connection reset by peer? */
2324 val = read( fd, (char*)&dwErrBytes, sizeof(dwErrBytes));
2325 while( errno == EINTR );
2326 if( errno != EWOULDBLOCK )
2328 switch( val )
2330 case 0: errno = ENETDOWN; /* soft reset, fall through */
2331 case -1: /* hard reset */
2332 dwEvent = WSAMAKESELECTREPLY( WS_FD_CLOSE, wsaErrno() );
2333 break;
2335 default: bPost = FALSE;
2336 continue; /* FIXME: this is real bad */
2339 else { bPost = FALSE; continue; } /* more weirdness */
2341 /* this is it, this socket is closed */
2343 psop->pws->flags &= ~(WS_FD_READ | WS_FD_CLOSE | WS_FD_WRITE);
2344 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2345 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2347 if( *max_fd == (fd + 1) ) (*max_fd)--;
2352 if( bPost )
2354 TRACE(winsock, "\t hwnd %04x - %04x, %08x\n",
2355 psop->hWnd, psop->uMsg, (unsigned)dwEvent );
2356 PostMessage32A( psop->hWnd, psop->uMsg,
2357 (WPARAM32)WS_PTR2HANDLE(psop->pws), (LPARAM)dwEvent );
2358 bPost = FALSE;
2359 num_posted++;
2362 if( num_pending <= 0 ) break;
2365 TRACE(winsock, "\tdone, %i posted events\n", num_posted );
2366 return ( num_posted ) ? TRUE : FALSE;
2369 INT32 WINAPI WSAAsyncSelect32(SOCKET32 s, HWND32 hWnd, UINT32 uMsg, UINT32 lEvent)
2371 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
2372 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2374 TRACE(winsock, "(%08x): %04x, hWnd %04x, uMsg %08x, event %08x\n",
2375 (unsigned)pwsi, (SOCKET16)s, (HWND16)hWnd, uMsg, (unsigned)lEvent );
2376 if( _check_ws(pwsi, pws) )
2378 ws_select_op* psop;
2380 if( (psop = pws->psop) )
2382 /* delete previous control struct */
2384 if( psop == __ws_select_list )
2385 __ws_select_list = psop->next;
2386 else
2387 psop->prev->next = psop->next;
2388 if( psop->next ) psop->next->prev = psop->prev;
2390 if( pws->flags & (WS_FD_ACCEPT | WS_FD_CONNECT | WS_FD_READ | WS_FD_CLOSE) )
2391 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
2392 if( pws->flags & (WS_FD_CONNECT | WS_FD_WRITE) )
2393 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
2395 TRACE(winsock,"\tremoving psop = 0x%08x\n", (unsigned) psop );
2397 WS_FREE( pws->psop );
2398 pws->flags &= ~(WS_FD_RAW | WS_FD_ACCEPT | WS_FD_CONNECT |
2399 WS_FD_READ | WS_FD_WRITE | WS_FD_CLOSE);
2400 pws->psop = NULL;
2403 if( lEvent )
2405 psop = (ws_select_op*)WS_ALLOC(sizeof(ws_select_op));
2406 if( psop )
2408 int sock_type, bytes = sizeof(int);
2410 WINSOCK_unblock_io( pws->fd, TRUE );
2412 psop->prev = NULL;
2413 psop->next = __ws_select_list;
2414 if( __ws_select_list )
2415 __ws_select_list->prev = psop;
2416 __ws_select_list = psop;
2418 psop->pws = pws;
2419 psop->hWnd = hWnd;
2420 psop->uMsg = uMsg;
2422 pws->psop = psop;
2423 pws->flags |= (0x0000FFFF & lEvent);
2424 getsockopt(pws->fd, SOL_SOCKET, SO_TYPE, &sock_type, &bytes);
2425 if( sock_type == SOCK_RAW ) pws->flags |= WS_FD_RAW;
2427 if( lEvent & (WS_FD_ACCEPT | WS_FD_CONNECT | WS_FD_READ | WS_FD_CLOSE) )
2428 EVENT_AddIO( pws->fd, EVENT_IO_READ );
2429 if( lEvent & (WS_FD_CONNECT | WS_FD_WRITE) )
2430 EVENT_AddIO( pws->fd, EVENT_IO_WRITE );
2432 /* TODO: handle WS_FD_ACCEPT right away if the socket is readable */
2434 TRACE(winsock,"\tcreating psop = 0x%08x\n", (unsigned)psop );
2436 return 0; /* success */
2438 else pwsi->err = WSAENOBUFS;
2440 else return 0;
2442 else if( pwsi ) pwsi->err = WSAEINVAL;
2443 return SOCKET_ERROR;
2446 INT16 WINAPI WSAAsyncSelect16(SOCKET16 s, HWND16 hWnd, UINT16 wMsg, UINT32 lEvent)
2448 return (INT16)WSAAsyncSelect32( s, hWnd, wMsg, lEvent );
2452 /***********************************************************************
2453 * __WSAFDIsSet() (WINSOCK.151)
2455 INT16 WINAPI __WSAFDIsSet16(SOCKET16 s, ws_fd_set16 *set)
2457 int i = set->fd_count;
2459 TRACE(winsock, "(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2461 while (i--)
2462 if (set->fd_array[i] == s) return 1;
2463 return 0;
2466 /***********************************************************************
2467 * __WSAFDIsSet() (WSOCK32.151)
2469 INT32 WINAPI __WSAFDIsSet32(SOCKET32 s, ws_fd_set32 *set)
2471 int i = set->fd_count;
2473 TRACE(winsock, "(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2475 while (i--)
2476 if (set->fd_array[i] == s) return 1;
2477 return 0;
2480 /***********************************************************************
2481 * WSAIsBlocking() (WINSOCK.114)(WSOCK32.114)
2483 BOOL32 WINAPI WSAIsBlocking(void)
2485 /* By default WinSock should set all its sockets to non-blocking mode
2486 * and poll in PeekMessage loop when processing "blocking" ones. This
2487 * function is supposed to tell if the program is in this loop. Our
2488 * blocking calls are truly blocking so we always return FALSE.
2490 * Note: It is allowed to call this function without prior WSAStartup().
2493 TRACE(winsock, "\n");
2494 return FALSE;
2497 /***********************************************************************
2498 * WSACancelBlockingCall() (WINSOCK.113)(WSOCK32.113)
2500 INT32 WINAPI WSACancelBlockingCall(void)
2502 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2504 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2506 if( pwsi ) return 0;
2507 return SOCKET_ERROR;
2511 /***********************************************************************
2512 * WSASetBlockingHook16() (WINSOCK.109)
2514 FARPROC16 WINAPI WSASetBlockingHook16(FARPROC16 lpBlockFunc)
2516 FARPROC16 prev;
2517 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2519 TRACE(winsock, "(%08x): hook %08x\n",
2520 (unsigned)pwsi, (unsigned) lpBlockFunc);
2521 if( pwsi )
2523 prev = (FARPROC16)pwsi->blocking_hook;
2524 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2525 pwsi->flags &= ~WSI_BLOCKINGHOOK32;
2526 return prev;
2528 return 0;
2532 /***********************************************************************
2533 * WSASetBlockingHook32()
2535 FARPROC32 WINAPI WSASetBlockingHook32(FARPROC32 lpBlockFunc)
2537 FARPROC32 prev;
2538 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2540 TRACE(winsock, "(%08x): hook %08x\n",
2541 (unsigned)pwsi, (unsigned) lpBlockFunc);
2542 if( pwsi ) {
2543 prev = (FARPROC32)pwsi->blocking_hook;
2544 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2545 pwsi->flags |= WSI_BLOCKINGHOOK32;
2546 return prev;
2548 return NULL;
2552 /***********************************************************************
2553 * WSAUnhookBlockingHook16() (WINSOCK.110)
2555 INT16 WINAPI WSAUnhookBlockingHook16(void)
2557 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2559 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2560 if( pwsi ) return (INT16)(pwsi->blocking_hook = 0);
2561 return SOCKET_ERROR;
2565 /***********************************************************************
2566 * WSAUnhookBlockingHook32()
2568 INT32 WINAPI WSAUnhookBlockingHook32(void)
2570 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2572 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2573 if( pwsi )
2575 pwsi->blocking_hook = 0;
2576 pwsi->flags &= ~WSI_BLOCKINGHOOK32;
2577 return 0;
2579 return SOCKET_ERROR;
2583 * TCP/IP action codes.
2587 #define WSCNTL_TCPIP_QUERY_INFO 0x00000000
2588 #define WSCNTL_TCPIP_SET_INFO 0x00000001
2589 #define WSCNTL_TCPIP_ICMP_ECHO 0x00000002
2590 #define WSCNTL_TCPIP_TEST 0x00000003
2593 /***********************************************************************
2594 * WsControl()
2596 * WsControl seems to be an undocumented Win95 function. A lot of
2597 * discussion about WsControl can be found on the net, e.g.
2598 * Subject: Re: WSOCK32.DLL WsControl Exported Function
2599 * From: "Peter Rindfuss" <rindfuss-s@medea.wz-berlin.de>
2600 * Date: 1997/08/17
2603 DWORD WINAPI WsControl(DWORD protocoll,DWORD action,
2604 LPVOID inbuf,LPDWORD inbuflen,
2605 LPVOID outbuf,LPDWORD outbuflen)
2608 switch (action) {
2609 case WSCNTL_TCPIP_ICMP_ECHO:
2611 unsigned int addr = *(unsigned int*)inbuf;
2612 #if 0
2613 int timeout= *(unsigned int*)(inbuf+4);
2614 short x1 = *(unsigned short*)(inbuf+8);
2615 short sendbufsize = *(unsigned short*)(inbuf+10);
2616 char x2 = *(unsigned char*)(inbuf+12);
2617 char ttl = *(unsigned char*)(inbuf+13);
2618 char service = *(unsigned char*)(inbuf+14);
2619 char type= *(unsigned char*)(inbuf+15); /* 0x2: don't fragment*/
2620 #endif
2622 FIXME(winsock,"(ICMP_ECHO) to 0x%08x stub \n", addr);
2623 break;
2625 default:
2626 FIXME(winsock,"(%lx,%lx,%p,%p,%p,%p) stub\n",
2627 protocoll,action,inbuf,inbuflen,outbuf,outbuflen);
2629 return FALSE;
2631 /*********************************************************
2632 * WS_s_perror WSOCK32.1108
2634 void WINAPI WS_s_perror(LPCSTR message)
2636 FIXME(winsock,"(%s): stub\n",message);
2637 return;
2641 /* ----------------------------------- end of API stuff */
2643 /* ----------------------------------- helper functions -
2645 * TODO: Merge WS_dup_..() stuff into one function that
2646 * would operate with a generic structure containing internal
2647 * pointers (via a template of some kind).
2650 static int list_size(char** l, int item_size)
2652 int i,j = 0;
2653 if(l)
2654 { for(i=0;l[i];i++)
2655 j += (item_size) ? item_size : strlen(l[i]) + 1;
2656 j += (i + 1) * sizeof(char*); }
2657 return j;
2660 static int list_dup(char** l_src, char* ref, char* base, int item_size)
2662 /* base is either either equal to ref or 0 or SEGPTR */
2664 char* p = ref;
2665 char** l_to = (char**)ref;
2666 int i,j,k;
2668 for(j=0;l_src[j];j++) ;
2669 p += (j + 1) * sizeof(char*);
2670 for(i=0;i<j;i++)
2671 { l_to[i] = base + (p - ref);
2672 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
2673 memcpy(p, l_src[i], k); p += k; }
2674 l_to[i] = NULL;
2675 return (p - ref);
2678 /* ----- hostent */
2680 static int hostent_size(struct hostent* p_he)
2682 int size = 0;
2683 if( p_he )
2684 { size = sizeof(struct hostent);
2685 size += strlen(p_he->h_name) + 1;
2686 size += list_size(p_he->h_aliases, 0);
2687 size += list_size(p_he->h_addr_list, p_he->h_length ); }
2688 return size;
2691 int WS_dup_he(LPWSINFO pwsi, struct hostent* p_he, int flag)
2693 /* Convert hostent structure into ws_hostent so that the data fits
2694 * into pwsi->buffer. Internal pointers can be linear, SEGPTR, or
2695 * relative to pwsi->buffer depending on "flag" value. Returns size
2696 * of the data copied (also in the pwsi->buflen).
2699 int size = hostent_size(p_he);
2701 if( size )
2703 struct ws_hostent* p_to;
2704 char* p_name,*p_aliases,*p_addr,*p_base,*p;
2706 _check_buffer_he(pwsi, size);
2707 p_to = (struct ws_hostent*)pwsi->he;
2708 p = (char*)pwsi->he;
2709 p_base = (flag & WS_DUP_OFFSET) ? NULL
2710 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2711 p += sizeof(struct ws_hostent);
2712 p_name = p;
2713 strcpy(p, p_he->h_name); p += strlen(p) + 1;
2714 p_aliases = p;
2715 p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)pwsi->he), 0);
2716 p_addr = p;
2717 list_dup(p_he->h_addr_list, p, p_base + (p - (char*)pwsi->he), p_he->h_length);
2719 p_to->h_addrtype = (INT16)p_he->h_addrtype;
2720 p_to->h_length = (INT16)p_he->h_length;
2721 p_to->h_name = (SEGPTR)(p_base + (p_name - (char*)pwsi->he));
2722 p_to->h_aliases = (SEGPTR)(p_base + (p_aliases - (char*)pwsi->he));
2723 p_to->h_addr_list = (SEGPTR)(p_base + (p_addr - (char*)pwsi->he));
2725 size += (sizeof(struct ws_hostent) - sizeof(struct hostent));
2727 return size;
2730 /* ----- protoent */
2732 static int protoent_size(struct protoent* p_pe)
2734 int size = 0;
2735 if( p_pe )
2736 { size = sizeof(struct protoent);
2737 size += strlen(p_pe->p_name) + 1;
2738 size += list_size(p_pe->p_aliases, 0); }
2739 return size;
2742 int WS_dup_pe(LPWSINFO pwsi, struct protoent* p_pe, int flag)
2744 int size = protoent_size(p_pe);
2745 if( size )
2747 struct ws_protoent* p_to;
2748 char* p_name,*p_aliases,*p_base,*p;
2750 _check_buffer_pe(pwsi, size);
2751 p_to = (struct ws_protoent*)pwsi->pe;
2752 p = (char*)pwsi->pe;
2753 p_base = (flag & WS_DUP_OFFSET) ? NULL
2754 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2755 p += sizeof(struct ws_protoent);
2756 p_name = p;
2757 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
2758 p_aliases = p;
2759 list_dup(p_pe->p_aliases, p, p_base + (p - (char*)pwsi->pe), 0);
2761 p_to->p_proto = (INT16)p_pe->p_proto;
2762 p_to->p_name = (SEGPTR)(p_base) + (p_name - (char*)pwsi->pe);
2763 p_to->p_aliases = (SEGPTR)((p_base) + (p_aliases - (char*)pwsi->pe));
2765 size += (sizeof(struct ws_protoent) - sizeof(struct protoent));
2767 return size;
2770 /* ----- servent */
2772 static int servent_size(struct servent* p_se)
2774 int size = 0;
2775 if( p_se )
2776 { size += sizeof(struct servent);
2777 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
2778 size += list_size(p_se->s_aliases, 0); }
2779 return size;
2782 int WS_dup_se(LPWSINFO pwsi, struct servent* p_se, int flag)
2784 int size = servent_size(p_se);
2785 if( size )
2787 struct ws_servent* p_to;
2788 char* p_name,*p_aliases,*p_proto,*p_base,*p;
2790 _check_buffer_se(pwsi, size);
2791 p_to = (struct ws_servent*)pwsi->se;
2792 p = (char*)pwsi->se;
2793 p_base = (flag & WS_DUP_OFFSET) ? NULL
2794 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2795 p += sizeof(struct ws_servent);
2796 p_name = p;
2797 strcpy(p, p_se->s_name); p += strlen(p) + 1;
2798 p_proto = p;
2799 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
2800 p_aliases = p;
2801 list_dup(p_se->s_aliases, p, p_base + (p - (char*)pwsi->se), 0);
2803 p_to->s_port = (INT16)p_se->s_port;
2804 p_to->s_name = (SEGPTR)(p_base + (p_name - (char*)pwsi->se));
2805 p_to->s_proto = (SEGPTR)(p_base + (p_proto - (char*)pwsi->se));
2806 p_to->s_aliases = (SEGPTR)(p_base + (p_aliases - (char*)pwsi->se));
2808 size += (sizeof(struct ws_servent) - sizeof(struct servent));
2810 return size;
2813 /* ----------------------------------- error handling */
2815 UINT16 wsaErrno(void)
2817 int loc_errno = errno;
2818 #ifdef HAVE_STRERROR
2819 WARN(winsock, "errno %d, (%s).\n", loc_errno, strerror(loc_errno));
2820 #else
2821 WARN(winsock, "errno %d\n", loc_errno);
2822 #endif
2824 switch(loc_errno)
2826 case EINTR: return WSAEINTR;
2827 case EBADF: return WSAEBADF;
2828 case EACCES: return WSAEACCES;
2829 case EFAULT: return WSAEFAULT;
2830 case EINVAL: return WSAEINVAL;
2831 case EMFILE: return WSAEMFILE;
2832 case EWOULDBLOCK: return WSAEWOULDBLOCK;
2833 case EINPROGRESS: return WSAEINPROGRESS;
2834 case EALREADY: return WSAEALREADY;
2835 case ENOTSOCK: return WSAENOTSOCK;
2836 case EDESTADDRREQ: return WSAEDESTADDRREQ;
2837 case EMSGSIZE: return WSAEMSGSIZE;
2838 case EPROTOTYPE: return WSAEPROTOTYPE;
2839 case ENOPROTOOPT: return WSAENOPROTOOPT;
2840 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
2841 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
2842 case EOPNOTSUPP: return WSAEOPNOTSUPP;
2843 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
2844 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
2845 case EADDRINUSE: return WSAEADDRINUSE;
2846 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
2847 case ENETDOWN: return WSAENETDOWN;
2848 case ENETUNREACH: return WSAENETUNREACH;
2849 case ENETRESET: return WSAENETRESET;
2850 case ECONNABORTED: return WSAECONNABORTED;
2851 case EPIPE:
2852 case ECONNRESET: return WSAECONNRESET;
2853 case ENOBUFS: return WSAENOBUFS;
2854 case EISCONN: return WSAEISCONN;
2855 case ENOTCONN: return WSAENOTCONN;
2856 case ESHUTDOWN: return WSAESHUTDOWN;
2857 case ETOOMANYREFS: return WSAETOOMANYREFS;
2858 case ETIMEDOUT: return WSAETIMEDOUT;
2859 case ECONNREFUSED: return WSAECONNREFUSED;
2860 case ELOOP: return WSAELOOP;
2861 case ENAMETOOLONG: return WSAENAMETOOLONG;
2862 case EHOSTDOWN: return WSAEHOSTDOWN;
2863 case EHOSTUNREACH: return WSAEHOSTUNREACH;
2864 case ENOTEMPTY: return WSAENOTEMPTY;
2865 #ifdef EPROCLIM
2866 case EPROCLIM: return WSAEPROCLIM;
2867 #endif
2868 #ifdef EUSERS
2869 case EUSERS: return WSAEUSERS;
2870 #endif
2871 #ifdef EDQUOT
2872 case EDQUOT: return WSAEDQUOT;
2873 #endif
2874 #ifdef ESTALE
2875 case ESTALE: return WSAESTALE;
2876 #endif
2877 #ifdef EREMOTE
2878 case EREMOTE: return WSAEREMOTE;
2879 #endif
2881 /* just in case we ever get here and there are no problems */
2882 case 0: return 0;
2883 default:
2884 WARN(winsock, "Unknown errno %d!\n", loc_errno);
2885 return WSAEOPNOTSUPP;
2889 UINT16 wsaHerrno(void)
2891 int loc_errno = h_errno;
2893 WARN(winsock, "h_errno %d.\n", loc_errno);
2895 switch(loc_errno)
2897 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
2898 case TRY_AGAIN: return WSATRY_AGAIN;
2899 case NO_RECOVERY: return WSANO_RECOVERY;
2900 case NO_DATA: return WSANO_DATA;
2902 case 0: return 0;
2903 default:
2904 WARN(winsock,"Unknown h_errno %d!\n", loc_errno);
2905 return WSAEOPNOTSUPP;