1 /* Async WINSOCK DNS services
3 * (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
4 * (C) 1999 Marcus Meissner
6 * NOTE: If you make any changes to fix a particular app, make sure
7 * they don't break something else like Netscape or telnet and ftp
8 * clients and servers (www.winsite.com got a lot of those).
11 * - Add WSACancel* and correct handle management. (works rather well for
13 * - Verify & Check all calls for correctness
14 * (currently only WSAGetHostByName*, WSAGetServByPort* calls)
15 * - Check error returns.
16 * - mirc/mirc32 Finger @linux.kernel.org sometimes fails in threaded mode.
18 * - This implementation did ignore the "NOTE:" section above (since the
19 * whole stuff did not work anyway to other changes).
23 #include "wine/port.h"
26 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #ifdef HAVE_SYS_FILIO_H
32 # include <sys/filio.h>
35 #include <sys/ioccom.h>
36 #ifdef HAVE_SYS_SOCKIO_H
37 # include <sys/sockio.h>
42 # include <sys/so_ioctl.h>
45 #ifdef HAVE_SYS_PARAM_H
46 # include <sys/param.h>
52 #ifdef HAVE_SYS_WAIT_H
55 #ifdef HAVE_SYS_SOCKET_H
56 #include <sys/socket.h>
58 #ifdef HAVE_NETINET_IN_H
59 # include <netinet/in.h>
61 #ifdef HAVE_ARPA_INET_H
62 # include <arpa/inet.h>
67 #ifdef HAVE_SYS_ERRNO_H
68 #include <sys/errno.h>
75 #ifdef HAVE_ARPA_NAMESER_H
76 # include <arpa/nameser.h>
82 #include "wine/winbase16.h"
87 #include "wine/winsock16.h"
90 #include "debugtools.h"
92 DEFAULT_DEBUG_CHANNEL(winsock);
95 /* critical section to protect some non-rentrant net function */
96 CRITICAL_SECTION csWSgetXXXbyYYY = CRITICAL_SECTION_INIT("csWSgetXXXbyYYY");
98 /* protoptypes of some functions in socket.c
100 UINT16 wsaErrno(void);
101 UINT16 wsaHerrno(int errnr);
103 #define AQ_WIN16 0x00
104 #define AQ_WIN32 0x04
105 #define HB_WIN32(hb) (hb->flags & AQ_WIN32)
106 #define AQ_NUMBER 0x00
108 #define AQ_COPYPTR1 0x10
109 #define AQ_DUPLOWPTR1 0x20
110 #define AQ_MASKPTR1 0x30
111 #define AQ_COPYPTR2 0x40
112 #define AQ_DUPLOWPTR2 0x80
113 #define AQ_MASKPTR2 0xC0
116 #define AQ_GETPROTO 1
120 /* ----------------------------------- helper functions - */
122 static int list_size(char** l, int item_size)
127 j += (item_size) ? item_size : strlen(l[i]) + 1;
128 j += (i + 1) * sizeof(char*); }
132 static int list_dup(char** l_src, char* ref, char* base, int item_size)
134 /* base is either either equal to ref or 0 or SEGPTR */
137 char** l_to = (char**)ref;
140 for(j=0;l_src[j];j++) ;
141 p += (j + 1) * sizeof(char*);
143 { l_to[i] = base + (p - ref);
144 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
145 memcpy(p, l_src[i], k); p += k; }
152 static int hostent_size(struct hostent* p_he)
156 { size = sizeof(struct hostent);
157 size += strlen(p_he->h_name) + 1;
158 size += list_size(p_he->h_aliases, 0);
159 size += list_size(p_he->h_addr_list, p_he->h_length ); }
163 /* Copy hostent to p_to, fix up inside pointers using p_base (different for
164 * Win16 (linear vs. segmented). Return -neededsize on overrun.
166 static int WS_copy_he(char *p_to,char *p_base,int t_size,struct hostent* p_he, int flag)
168 char* p_name,*p_aliases,*p_addr,*p;
169 struct ws_hostent16 *p_to16 = (struct ws_hostent16*)p_to;
170 struct WS_hostent *p_to32 = (struct WS_hostent*)p_to;
171 int size = hostent_size(p_he) +
173 (flag & AQ_WIN16) ? sizeof(struct ws_hostent16) : sizeof(struct WS_hostent)
174 - sizeof(struct hostent)
180 p += (flag & AQ_WIN16) ?
181 sizeof(struct ws_hostent16) : sizeof(struct WS_hostent);
183 strcpy(p, p_he->h_name); p += strlen(p) + 1;
185 p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)p_to), 0);
187 list_dup(p_he->h_addr_list, p, p_base + (p - (char*)p_to), p_he->h_length);
191 p_to16->h_addrtype = (INT16)p_he->h_addrtype;
192 p_to16->h_length = (INT16)p_he->h_length;
193 p_to16->h_name = (SEGPTR)(p_base + (p_name - p_to));
194 p_to16->h_aliases = (SEGPTR)(p_base + (p_aliases - p_to));
195 p_to16->h_addr_list = (SEGPTR)(p_base + (p_addr - p_to));
199 p_to32->h_addrtype = p_he->h_addrtype;
200 p_to32->h_length = p_he->h_length;
201 p_to32->h_name = (p_base + (p_name - p_to));
202 p_to32->h_aliases = (char **)(p_base + (p_aliases - p_to));
203 p_to32->h_addr_list = (char **)(p_base + (p_addr - p_to));
211 static int protoent_size(struct protoent* p_pe)
215 { size = sizeof(struct protoent);
216 size += strlen(p_pe->p_name) + 1;
217 size += list_size(p_pe->p_aliases, 0); }
221 /* Copy protoent to p_to, fix up inside pointers using p_base (different for
222 * Win16 (linear vs. segmented). Return -neededsize on overrun.
224 static int WS_copy_pe(char *p_to,char *p_base,int t_size,struct protoent* p_pe, int flag)
226 char* p_name,*p_aliases,*p;
227 struct ws_protoent16 *p_to16 = (struct ws_protoent16*)p_to;
228 struct WS_protoent *p_to32 = (struct WS_protoent*)p_to;
229 int size = protoent_size(p_pe) +
231 (flag & AQ_WIN16) ? sizeof(struct ws_protoent16) : sizeof(struct WS_protoent)
232 - sizeof(struct protoent)
238 p += (flag & AQ_WIN16) ?
239 sizeof(struct ws_protoent16) : sizeof(struct WS_protoent);
241 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
243 list_dup(p_pe->p_aliases, p, p_base + (p - (char*)p_to), 0);
247 p_to16->p_proto = (INT16)p_pe->p_proto;
248 p_to16->p_name = (SEGPTR)(p_base) + (p_name - p_to);
249 p_to16->p_aliases = (SEGPTR)((p_base) + (p_aliases - p_to));
253 p_to32->p_proto = p_pe->p_proto;
254 p_to32->p_name = (p_base) + (p_name - p_to);
255 p_to32->p_aliases = (char **)((p_base) + (p_aliases - p_to));
263 static int servent_size(struct servent* p_se)
267 size += sizeof(struct servent);
268 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
269 size += list_size(p_se->s_aliases, 0);
274 /* Copy servent to p_to, fix up inside pointers using p_base (different for
275 * Win16 (linear vs. segmented). Return -neededsize on overrun.
276 * Take care of different Win16/Win32 servent structs (packing !)
278 static int WS_copy_se(char *p_to,char *p_base,int t_size,struct servent* p_se, int flag)
280 char* p_name,*p_aliases,*p_proto,*p;
281 struct ws_servent16 *p_to16 = (struct ws_servent16*)p_to;
282 struct WS_servent *p_to32 = (struct WS_servent*)p_to;
283 int size = servent_size(p_se) +
285 (flag & AQ_WIN16) ? sizeof(struct ws_servent16) : sizeof(struct WS_servent)
286 - sizeof(struct servent)
292 p += (flag & AQ_WIN16) ?
293 sizeof(struct ws_servent16) : sizeof(struct WS_servent);
295 strcpy(p, p_se->s_name); p += strlen(p) + 1;
297 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
299 list_dup(p_se->s_aliases, p, p_base + (p - p_to), 0);
303 p_to16->s_port = (INT16)p_se->s_port;
304 p_to16->s_name = (SEGPTR)(p_base + (p_name - p_to));
305 p_to16->s_proto = (SEGPTR)(p_base + (p_proto - p_to));
306 p_to16->s_aliases = (SEGPTR)(p_base + (p_aliases - p_to));
310 p_to32->s_port = p_se->s_port;
311 p_to32->s_name = (p_base + (p_name - p_to));
312 p_to32->s_proto = (p_base + (p_proto - p_to));
313 p_to32->s_aliases = (char **)(p_base + (p_aliases - p_to));
319 static HANDLE __ws_async_handle = 0xdead;
321 /* Generic async query struct. we use symbolic names for the different queries
324 typedef struct _async_query {
328 #define host_name ptr1
329 #define host_addr ptr1
330 #define serv_name ptr1
331 #define proto_name ptr1
333 #define serv_proto ptr2
335 #define host_len int1
336 #define proto_number int1
337 #define serv_port int1
339 #define host_type int2
343 HANDLE16 async_handle;
350 /****************************************************************************
351 * The async query function.
353 * It is either called as a thread startup routine or directly. It has
354 * to free the passed arg from the process heap and PostMessageA the async
355 * result or the error code.
358 * - errorhandling not verified.
360 static DWORD WINAPI _async_queryfun(LPVOID arg) {
361 async_query *aq = (async_query*)arg;
364 char *targetptr = (HB_WIN32(aq)?(char*)aq->sbuf:(char*)MapSL(aq->sbuf));
366 switch (aq->flags & AQ_GETMASK) {
369 char *copy_hostent = targetptr;
370 #if HAVE_LINUX_GETHOSTBYNAME_R_6
373 struct hostent hostentry;
374 int locerr = ENOBUFS;
376 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
378 int res = (aq->flags & AQ_NAME) ?
379 gethostbyname_r(aq->host_name,
380 &hostentry, extrabuf, ebufsize, &he, &locerr):
381 gethostbyaddr_r(aq->host_addr,aq->host_len,aq->host_type,
382 &hostentry, extrabuf, ebufsize, &he, &locerr);
383 if( res != ERANGE) break;
385 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
387 if (!he) fail = ((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
389 EnterCriticalSection( &csWSgetXXXbyYYY );
390 he = (aq->flags & AQ_NAME) ?
391 gethostbyname(aq->host_name):
392 gethostbyaddr(aq->host_addr,aq->host_len,aq->host_type);
393 if (!he) fail = ((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
396 size = WS_copy_he(copy_hostent,(char*)aq->sbuf,aq->sbuflen,he,aq->flags);
402 #if HAVE_LINUX_GETHOSTBYNAME_R_6
403 HeapFree(GetProcessHeap(),0,extrabuf);
405 LeaveCriticalSection( &csWSgetXXXbyYYY );
411 char *copy_protoent = targetptr;
412 EnterCriticalSection( &csWSgetXXXbyYYY );
413 pe = (aq->flags & AQ_NAME)?
414 getprotobyname(aq->proto_name) :
415 getprotobynumber(aq->proto_number);
417 size = WS_copy_pe(copy_protoent,(char*)aq->sbuf,aq->sbuflen,pe,aq->flags);
423 if (aq->flags & AQ_NAME)
424 MESSAGE("protocol %s not found; You might want to add "
425 "this to /etc/protocols\n", debugstr_a(aq->proto_name) );
427 MESSAGE("protocol number %d not found; You might want to add "
428 "this to /etc/protocols\n", aq->proto_number );
431 LeaveCriticalSection( &csWSgetXXXbyYYY );
436 char *copy_servent = targetptr;
437 EnterCriticalSection( &csWSgetXXXbyYYY );
438 se = (aq->flags & AQ_NAME)?
439 getservbyname(aq->serv_name,aq->serv_proto) :
440 getservbyport(aq->serv_port,aq->serv_proto);
442 size = WS_copy_se(copy_servent,(char*)aq->sbuf,aq->sbuflen,se,aq->flags);
448 if (aq->flags & AQ_NAME)
449 MESSAGE("service %s protocol %s not found; You might want to add "
450 "this to /etc/services\n", debugstr_a(aq->serv_name) ,
451 aq->serv_proto ? debugstr_a(aq->serv_proto ):"*");
453 MESSAGE("service on port %d protocol %s not found; You might want to add "
454 "this to /etc/services\n", aq->serv_port,
455 aq->serv_proto ? debugstr_a(aq->serv_proto ):"*");
458 LeaveCriticalSection( &csWSgetXXXbyYYY );
462 PostMessageA(aq->hWnd,aq->uMsg,aq->async_handle,size|(fail<<16));
463 HeapFree(GetProcessHeap(),0,arg);
467 /****************************************************************************
468 * The main async help function.
470 * It either starts a thread or just calls the function directly for platforms
471 * with no thread support. This relies on the fact that PostMessage() does
472 * not actually call the windowproc before the function returns.
474 static HANDLE16 __WSAsyncDBQuery(
475 HWND hWnd, UINT uMsg,INT int1,LPCSTR ptr1, INT int2, LPCSTR ptr2,
476 void *sbuf, INT sbuflen, UINT flags
484 /* allocate buffer to copy protocol- and service name to */
485 /* note: this is done in the calling thread so we can return */
486 /* a decent error code if the Alloc fails */
488 switch (flags & AQ_MASKPTR1) {
490 case AQ_COPYPTR1: xbuflen += int1; break;
491 case AQ_DUPLOWPTR1: xbuflen += strlen(ptr1) + 1; break;
494 switch (flags & AQ_MASKPTR2) {
496 case AQ_COPYPTR2: xbuflen += int2; break;
497 case AQ_DUPLOWPTR2: xbuflen += strlen(ptr2) + 1; break;
500 if(!(aq = HeapAlloc(GetProcessHeap(),0,sizeof(async_query) + xbuflen))) {
501 SetLastError(WSAEWOULDBLOCK); /* insufficient resources */
506 if (ptr1) switch (flags & AQ_MASKPTR1) {
508 case AQ_COPYPTR1: memcpy(pto, ptr1, int1); ptr1 = pto; pto += int1; break;
509 case AQ_DUPLOWPTR1: pfm = ptr1; ptr1 = pto; do *pto++ = tolower(*pfm); while (*pfm++); break;
511 if (ptr2) switch (flags & AQ_MASKPTR2) {
513 case AQ_COPYPTR2: memcpy(pto, ptr2, int2); ptr2 = pto; pto += int2; break;
514 case AQ_DUPLOWPTR2: pfm = ptr2; ptr2 = pto; do *pto++ = tolower(*pfm); while (*pfm++); break;
523 aq->async_handle = ++__ws_async_handle;
525 aq->sbuf = (SEGPTR)sbuf;
526 aq->sbuflen = sbuflen;
529 if (CreateThread(NULL,0,_async_queryfun,aq,0,NULL) == INVALID_HANDLE_VALUE)
532 return __ws_async_handle;
536 /***********************************************************************
537 * WSAAsyncGetHostByAddr (WINSOCK.102)
539 HANDLE16 WINAPI WSAAsyncGetHostByAddr16(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,
540 INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen)
542 TRACE("hwnd %04x, msg %04x, addr %08x[%i]\n",
543 hWnd, uMsg, (unsigned)addr , len );
544 return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,(void*)sbuf,buflen,
545 AQ_NUMBER|AQ_COPYPTR1|AQ_WIN16|AQ_GETHOST);
548 /***********************************************************************
549 * WSAAsyncGetHostByAddr (WS2_32.102)
551 HANDLE WINAPI WSAAsyncGetHostByAddr(HWND hWnd, UINT uMsg, LPCSTR addr,
552 INT len, INT type, LPSTR sbuf, INT buflen)
554 TRACE("hwnd %04x, msg %04x, addr %08x[%i]\n",
555 hWnd, uMsg, (unsigned)addr , len );
556 return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,sbuf,buflen,
557 AQ_NUMBER|AQ_COPYPTR1|AQ_WIN32|AQ_GETHOST);
560 /***********************************************************************
561 * WSAAsyncGetHostByName (WINSOCK.103)
563 HANDLE16 WINAPI WSAAsyncGetHostByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
564 SEGPTR sbuf, INT16 buflen)
566 TRACE("hwnd %04x, msg %04x, host %s, buffer %i\n",
567 hWnd, uMsg, (name)?name:"<null>", (int)buflen );
568 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,
569 AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN16|AQ_GETHOST);
572 /***********************************************************************
573 * WSAAsyncGetHostByName (WS2_32.103)
575 HANDLE WINAPI WSAAsyncGetHostByName(HWND hWnd, UINT uMsg, LPCSTR name,
576 LPSTR sbuf, INT buflen)
578 TRACE("hwnd %04x, msg %08x, host %s, buffer %i\n",
579 (HWND16)hWnd, uMsg, (name)?name:"<null>", (int)buflen );
580 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,
581 AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN32|AQ_GETHOST);
584 /***********************************************************************
585 * WSAAsyncGetProtoByName (WINSOCK.105)
587 HANDLE16 WINAPI WSAAsyncGetProtoByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
588 SEGPTR sbuf, INT16 buflen)
590 TRACE("hwnd %04x, msg %08x, protocol %s\n",
591 (HWND16)hWnd, uMsg, (name)?name:"<null>" );
592 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,
593 AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN16|AQ_GETPROTO);
596 /***********************************************************************
597 * WSAAsyncGetProtoByName (WS2_32.105)
599 HANDLE WINAPI WSAAsyncGetProtoByName(HWND hWnd, UINT uMsg, LPCSTR name,
600 LPSTR sbuf, INT buflen)
602 TRACE("hwnd %04x, msg %08x, protocol %s\n",
603 (HWND16)hWnd, uMsg, (name)?name:"<null>" );
604 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,
605 AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN32|AQ_GETPROTO);
609 /***********************************************************************
610 * WSAAsyncGetProtoByNumber (WINSOCK.104)
612 HANDLE16 WINAPI WSAAsyncGetProtoByNumber16(HWND16 hWnd,UINT16 uMsg,INT16 number,
613 SEGPTR sbuf, INT16 buflen)
615 TRACE("hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
616 return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,(void*)sbuf,buflen,
617 AQ_GETPROTO|AQ_NUMBER|AQ_WIN16);
620 /***********************************************************************
621 * WSAAsyncGetProtoByNumber (WS2_32.104)
623 HANDLE WINAPI WSAAsyncGetProtoByNumber(HWND hWnd, UINT uMsg, INT number,
624 LPSTR sbuf, INT buflen)
626 TRACE("hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
627 return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,sbuf,buflen,
628 AQ_GETPROTO|AQ_NUMBER|AQ_WIN32);
631 /***********************************************************************
632 * WSAAsyncGetServByName (WINSOCK.107)
634 HANDLE16 WINAPI WSAAsyncGetServByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
635 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
637 TRACE("hwnd %04x, msg %04x, name %s, proto %s\n",
638 hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>");
639 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,(void*)sbuf,buflen,
640 AQ_GETSERV|AQ_NAME|AQ_DUPLOWPTR1|AQ_DUPLOWPTR2|AQ_WIN16);
643 /***********************************************************************
644 * WSAAsyncGetServByName (WS2_32.107)
646 HANDLE WINAPI WSAAsyncGetServByName(HWND hWnd, UINT uMsg, LPCSTR name,
647 LPCSTR proto, LPSTR sbuf, INT buflen)
649 TRACE("hwnd %04x, msg %04x, name %s, proto %s\n",
650 hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>");
651 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,sbuf,buflen,
652 AQ_GETSERV|AQ_NAME|AQ_DUPLOWPTR1|AQ_DUPLOWPTR2|AQ_WIN32);
655 /***********************************************************************
656 * WSAAsyncGetServByPort (WINSOCK.106)
658 HANDLE16 WINAPI WSAAsyncGetServByPort16(HWND16 hWnd, UINT16 uMsg, INT16 port,
659 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
661 TRACE("hwnd %04x, msg %04x, port %i, proto %s\n",
662 hWnd, uMsg, port, (proto)?proto:"<null>" );
663 return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,(void*)sbuf,buflen,
664 AQ_GETSERV|AQ_NUMBER|AQ_DUPLOWPTR2|AQ_WIN16);
667 /***********************************************************************
668 * WSAAsyncGetServByPort (WS2_32.106)
670 HANDLE WINAPI WSAAsyncGetServByPort(HWND hWnd, UINT uMsg, INT port,
671 LPCSTR proto, LPSTR sbuf, INT buflen)
673 TRACE("hwnd %04x, msg %04x, port %i, proto %s\n",
674 hWnd, uMsg, port, (proto)?proto:"<null>" );
675 return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,sbuf,buflen,
676 AQ_GETSERV|AQ_NUMBER|AQ_DUPLOWPTR2|AQ_WIN32);
679 /***********************************************************************
680 * WSACancelAsyncRequest (WS2_32.108)
682 INT WINAPI WSACancelAsyncRequest(HANDLE hAsyncTaskHandle)
684 FIXME("(%08x),stub\n", hAsyncTaskHandle);
688 /***********************************************************************
689 * WSACancelAsyncRequest (WINSOCK.108)
691 INT16 WINAPI WSACancelAsyncRequest16(HANDLE16 hAsyncTaskHandle)
693 return (HANDLE16)WSACancelAsyncRequest((HANDLE)hAsyncTaskHandle);
696 /***********************************************************************
697 * WSApSetPostRoutine (WS2_32.24)
699 INT WINAPI WSApSetPostRoutine(LPWPUPOSTMESSAGE lpPostRoutine)
701 FIXME("(%p), stub !\n", lpPostRoutine);