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).
25 #include <sys/types.h>
29 #include <sys/ioctl.h>
30 #ifdef HAVE_SYS_FILIO_H
31 # include <sys/filio.h>
34 #include <sys/ioccom.h>
35 #ifdef HAVE_SYS_SOCKIO_H
36 # include <sys/sockio.h>
41 # include <sys/so_ioctl.h>
44 #ifdef HAVE_SYS_PARAM_H
45 # include <sys/param.h>
51 #ifdef HAVE_SYS_WAIT_H
54 #ifdef HAVE_SYS_SOCKET_H
55 #include <sys/socket.h>
57 #ifdef HAVE_NETINET_IN_H
58 # include <netinet/in.h>
60 #ifdef HAVE_ARPA_INET_H
61 # include <arpa/inet.h>
66 #ifdef HAVE_SYS_ERRNO_H
67 #include <sys/errno.h>
74 #ifdef HAVE_ARPA_NAMESER_H
75 # include <arpa/nameser.h>
81 #include "wine/winbase16.h"
85 #include "wine/winsock16.h"
88 #include "wine/port.h"
89 #include "debugtools.h"
91 DEFAULT_DEBUG_CHANNEL(winsock);
94 /* critical section to protect some non-rentrant net function */
95 CRITICAL_SECTION csWSgetXXXbyYYY = CRITICAL_SECTION_INIT;
97 /* protoptypes of some functions in socket.c
99 UINT16 wsaErrno(void);
100 UINT16 wsaHerrno(int errnr);
102 #define AQ_WIN16 0x00
103 #define AQ_WIN32 0x04
104 #define HB_WIN32(hb) (hb->flags & AQ_WIN32)
105 #define AQ_NUMBER 0x00
107 #define AQ_COPYPTR1 0x10
108 #define AQ_DUPLOWPTR1 0x20
109 #define AQ_MASKPTR1 0x30
110 #define AQ_COPYPTR2 0x40
111 #define AQ_DUPLOWPTR2 0x80
112 #define AQ_MASKPTR2 0xC0
115 #define AQ_GETPROTO 1
119 /* ----------------------------------- helper functions - */
121 static int list_size(char** l, int item_size)
126 j += (item_size) ? item_size : strlen(l[i]) + 1;
127 j += (i + 1) * sizeof(char*); }
131 static int list_dup(char** l_src, char* ref, char* base, int item_size)
133 /* base is either either equal to ref or 0 or SEGPTR */
136 char** l_to = (char**)ref;
139 for(j=0;l_src[j];j++) ;
140 p += (j + 1) * sizeof(char*);
142 { l_to[i] = base + (p - ref);
143 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
144 memcpy(p, l_src[i], k); p += k; }
151 static int hostent_size(struct hostent* p_he)
155 { size = sizeof(struct hostent);
156 size += strlen(p_he->h_name) + 1;
157 size += list_size(p_he->h_aliases, 0);
158 size += list_size(p_he->h_addr_list, p_he->h_length ); }
162 /* Copy hostent to p_to, fix up inside pointers using p_base (different for
163 * Win16 (linear vs. segmented). Return -neededsize on overrun.
165 static int WS_copy_he(char *p_to,char *p_base,int t_size,struct hostent* p_he, int flag)
167 char* p_name,*p_aliases,*p_addr,*p;
168 struct ws_hostent16 *p_to16 = (struct ws_hostent16*)p_to;
169 struct ws_hostent32 *p_to32 = (struct ws_hostent32*)p_to;
170 int size = hostent_size(p_he) +
172 (flag & AQ_WIN16) ? sizeof(struct ws_hostent16) : sizeof(struct ws_hostent32)
173 - sizeof(struct hostent)
179 p += (flag & AQ_WIN16) ?
180 sizeof(struct ws_hostent16) : sizeof(struct ws_hostent32);
182 strcpy(p, p_he->h_name); p += strlen(p) + 1;
184 p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)p_to), 0);
186 list_dup(p_he->h_addr_list, p, p_base + (p - (char*)p_to), p_he->h_length);
190 p_to16->h_addrtype = (INT16)p_he->h_addrtype;
191 p_to16->h_length = (INT16)p_he->h_length;
192 p_to16->h_name = (SEGPTR)(p_base + (p_name - p_to));
193 p_to16->h_aliases = (SEGPTR)(p_base + (p_aliases - p_to));
194 p_to16->h_addr_list = (SEGPTR)(p_base + (p_addr - p_to));
198 p_to32->h_addrtype = p_he->h_addrtype;
199 p_to32->h_length = p_he->h_length;
200 p_to32->h_name = (p_base + (p_name - p_to));
201 p_to32->h_aliases = (char **)(p_base + (p_aliases - p_to));
202 p_to32->h_addr_list = (char **)(p_base + (p_addr - p_to));
210 static int protoent_size(struct protoent* p_pe)
214 { size = sizeof(struct protoent);
215 size += strlen(p_pe->p_name) + 1;
216 size += list_size(p_pe->p_aliases, 0); }
220 /* Copy protoent to p_to, fix up inside pointers using p_base (different for
221 * Win16 (linear vs. segmented). Return -neededsize on overrun.
223 static int WS_copy_pe(char *p_to,char *p_base,int t_size,struct protoent* p_pe, int flag)
225 char* p_name,*p_aliases,*p;
226 struct ws_protoent16 *p_to16 = (struct ws_protoent16*)p_to;
227 struct ws_protoent32 *p_to32 = (struct ws_protoent32*)p_to;
228 int size = protoent_size(p_pe) +
230 (flag & AQ_WIN16) ? sizeof(struct ws_protoent16) : sizeof(struct ws_protoent32)
231 - sizeof(struct protoent)
237 p += (flag & AQ_WIN16) ?
238 sizeof(struct ws_protoent16) : sizeof(struct ws_protoent32);
240 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
242 list_dup(p_pe->p_aliases, p, p_base + (p - (char*)p_to), 0);
246 p_to16->p_proto = (INT16)p_pe->p_proto;
247 p_to16->p_name = (SEGPTR)(p_base) + (p_name - p_to);
248 p_to16->p_aliases = (SEGPTR)((p_base) + (p_aliases - p_to));
252 p_to32->p_proto = p_pe->p_proto;
253 p_to32->p_name = (p_base) + (p_name - p_to);
254 p_to32->p_aliases = (char **)((p_base) + (p_aliases - p_to));
262 static int servent_size(struct servent* p_se)
266 size += sizeof(struct servent);
267 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
268 size += list_size(p_se->s_aliases, 0);
273 /* Copy servent to p_to, fix up inside pointers using p_base (different for
274 * Win16 (linear vs. segmented). Return -neededsize on overrun.
275 * Take care of different Win16/Win32 servent structs (packing !)
277 static int WS_copy_se(char *p_to,char *p_base,int t_size,struct servent* p_se, int flag)
279 char* p_name,*p_aliases,*p_proto,*p;
280 struct ws_servent16 *p_to16 = (struct ws_servent16*)p_to;
281 struct ws_servent32 *p_to32 = (struct ws_servent32*)p_to;
282 int size = servent_size(p_se) +
284 (flag & AQ_WIN16) ? sizeof(struct ws_servent16) : sizeof(struct ws_servent32)
285 - sizeof(struct servent)
291 p += (flag & AQ_WIN16) ?
292 sizeof(struct ws_servent16) : sizeof(struct ws_servent32);
294 strcpy(p, p_se->s_name); p += strlen(p) + 1;
296 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
298 list_dup(p_se->s_aliases, p, p_base + (p - p_to), 0);
302 p_to16->s_port = (INT16)p_se->s_port;
303 p_to16->s_name = (SEGPTR)(p_base + (p_name - p_to));
304 p_to16->s_proto = (SEGPTR)(p_base + (p_proto - p_to));
305 p_to16->s_aliases = (SEGPTR)(p_base + (p_aliases - p_to));
309 p_to32->s_port = p_se->s_port;
310 p_to32->s_name = (p_base + (p_name - p_to));
311 p_to32->s_proto = (p_base + (p_proto - p_to));
312 p_to32->s_aliases = (char **)(p_base + (p_aliases - p_to));
318 static HANDLE __ws_async_handle = 0xdead;
320 /* Generic async query struct. we use symbolic names for the different queries
323 typedef struct _async_query {
327 #define host_name ptr1
328 #define host_addr ptr1
329 #define serv_name ptr1
330 #define proto_name ptr1
332 #define serv_proto ptr2
334 #define host_len int1
335 #define proto_number int1
336 #define serv_port int1
338 #define host_type int2
342 HANDLE16 async_handle;
349 /****************************************************************************
350 * The async query function.
352 * It is either called as a thread startup routine or directly. It has
353 * to free the passed arg from the process heap and PostMessageA the async
354 * result or the error code.
357 * - errorhandling not verified.
359 static DWORD WINAPI _async_queryfun(LPVOID arg) {
360 async_query *aq = (async_query*)arg;
363 char *targetptr = (HB_WIN32(aq)?(char*)aq->sbuf:(char*)MapSL(aq->sbuf));
365 switch (aq->flags & AQ_GETMASK) {
368 char *copy_hostent = targetptr;
369 #if HAVE_LINUX_GETHOSTBYNAME_R_6
372 struct hostent hostentry;
373 int locerr = ENOBUFS;
375 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
377 int res = (aq->flags & AQ_NAME) ?
378 gethostbyname_r(aq->host_name,
379 &hostentry, extrabuf, ebufsize, &he, &locerr):
380 gethostbyaddr_r(aq->host_addr,aq->host_len,aq->host_type,
381 &hostentry, extrabuf, ebufsize, &he, &locerr);
382 if( res != ERANGE) break;
384 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
386 if (!he) fail = ((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
388 EnterCriticalSection( &csWSgetXXXbyYYY );
389 he = (aq->flags & AQ_NAME) ?
390 gethostbyname(aq->host_name):
391 gethostbyaddr(aq->host_addr,aq->host_len,aq->host_type);
392 if (!he) fail = ((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
395 size = WS_copy_he(copy_hostent,(char*)aq->sbuf,aq->sbuflen,he,aq->flags);
401 #if HAVE_LINUX_GETHOSTBYNAME_R_6
402 HeapFree(GetProcessHeap(),0,extrabuf);
404 LeaveCriticalSection( &csWSgetXXXbyYYY );
410 char *copy_protoent = targetptr;
411 EnterCriticalSection( &csWSgetXXXbyYYY );
412 pe = (aq->flags & AQ_NAME)?
413 getprotobyname(aq->proto_name) :
414 getprotobynumber(aq->proto_number);
416 size = WS_copy_pe(copy_protoent,(char*)aq->sbuf,aq->sbuflen,pe,aq->flags);
422 if (aq->flags & AQ_NAME)
423 MESSAGE("protocol %s not found; You might want to add "
424 "this to /etc/protocols\n", debugstr_a(aq->proto_name) );
426 MESSAGE("protocol number %d not found; You might want to add "
427 "this to /etc/protocols\n", aq->proto_number );
430 LeaveCriticalSection( &csWSgetXXXbyYYY );
435 char *copy_servent = targetptr;
436 EnterCriticalSection( &csWSgetXXXbyYYY );
437 se = (aq->flags & AQ_NAME)?
438 getservbyname(aq->serv_name,aq->serv_proto) :
439 getservbyport(aq->serv_port,aq->serv_proto);
441 size = WS_copy_se(copy_servent,(char*)aq->sbuf,aq->sbuflen,se,aq->flags);
447 if (aq->flags & AQ_NAME)
448 MESSAGE("service %s protocol %s not found; You might want to add "
449 "this to /etc/services\n", debugstr_a(aq->serv_name) ,
450 aq->serv_proto ? debugstr_a(aq->serv_proto ):"*");
452 MESSAGE("service on port %d protocol %s not found; You might want to add "
453 "this to /etc/services\n", aq->serv_port,
454 aq->serv_proto ? debugstr_a(aq->serv_proto ):"*");
457 LeaveCriticalSection( &csWSgetXXXbyYYY );
461 PostMessageA(aq->hWnd,aq->uMsg,aq->async_handle,size|(fail<<16));
462 HeapFree(GetProcessHeap(),0,arg);
466 /****************************************************************************
467 * The main async help function.
469 * It either starts a thread or just calls the function directly for platforms
470 * with no thread support. This relies on the fact that PostMessage() does
471 * not actually call the windowproc before the function returns.
473 static HANDLE16 __WSAsyncDBQuery(
474 HWND hWnd, UINT uMsg,INT int1,LPCSTR ptr1, INT int2, LPCSTR ptr2,
475 void *sbuf, INT sbuflen, UINT flags
483 /* allocate buffer to copy protocol- and service name to */
484 /* note: this is done in the calling thread so we can return */
485 /* a decent error code if the Alloc fails */
487 switch (flags & AQ_MASKPTR1) {
489 case AQ_COPYPTR1: xbuflen += int1; break;
490 case AQ_DUPLOWPTR1: xbuflen += strlen(ptr1) + 1; break;
493 switch (flags & AQ_MASKPTR2) {
495 case AQ_COPYPTR2: xbuflen += int2; break;
496 case AQ_DUPLOWPTR2: xbuflen += strlen(ptr2) + 1; break;
499 if(!(aq = HeapAlloc(GetProcessHeap(),0,sizeof(async_query) + xbuflen))) {
500 SetLastError(WSAEWOULDBLOCK); /* insufficient resources */
505 if (ptr1) switch (flags & AQ_MASKPTR1) {
507 case AQ_COPYPTR1: memcpy(pto, ptr1, int1); ptr1 = pto; pto += int1; break;
508 case AQ_DUPLOWPTR1: pfm = ptr1; ptr1 = pto; do *pto++ = tolower(*pfm); while (*pfm++); break;
510 if (ptr2) switch (flags & AQ_MASKPTR2) {
512 case AQ_COPYPTR2: memcpy(pto, ptr2, int2); ptr2 = pto; pto += int2; break;
513 case AQ_DUPLOWPTR2: pfm = ptr2; ptr2 = pto; do *pto++ = tolower(*pfm); while (*pfm++); break;
522 aq->async_handle = ++__ws_async_handle;
524 aq->sbuf = (SEGPTR)sbuf;
525 aq->sbuflen = sbuflen;
528 if (CreateThread(NULL,0,_async_queryfun,aq,0,NULL) == INVALID_HANDLE_VALUE)
531 return __ws_async_handle;
535 /***********************************************************************
536 * WSAAsyncGetHostByAddr() (WINSOCK.102)
538 HANDLE16 WINAPI WSAAsyncGetHostByAddr16(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,
539 INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen)
541 TRACE("hwnd %04x, msg %04x, addr %08x[%i]\n",
542 hWnd, uMsg, (unsigned)addr , len );
543 return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,(void*)sbuf,buflen,
544 AQ_NUMBER|AQ_COPYPTR1|AQ_WIN16|AQ_GETHOST);
547 /***********************************************************************
548 * WSAAsyncGetHostByAddr() (WSOCK32.102)
550 HANDLE WINAPI WSAAsyncGetHostByAddr(HWND hWnd, UINT uMsg, LPCSTR addr,
551 INT len, INT type, LPSTR sbuf, INT buflen)
553 TRACE("hwnd %04x, msg %04x, addr %08x[%i]\n",
554 hWnd, uMsg, (unsigned)addr , len );
555 return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,sbuf,buflen,
556 AQ_NUMBER|AQ_COPYPTR1|AQ_WIN32|AQ_GETHOST);
559 /***********************************************************************
560 * WSAAsyncGetHostByName() (WINSOCK.103)
562 HANDLE16 WINAPI WSAAsyncGetHostByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
563 SEGPTR sbuf, INT16 buflen)
565 TRACE("hwnd %04x, msg %04x, host %s, buffer %i\n",
566 hWnd, uMsg, (name)?name:"<null>", (int)buflen );
567 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,
568 AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN16|AQ_GETHOST);
571 /***********************************************************************
572 * WSAAsyncGetHostByName() (WSOCK32.103)
574 HANDLE WINAPI WSAAsyncGetHostByName(HWND hWnd, UINT uMsg, LPCSTR name,
575 LPSTR sbuf, INT buflen)
577 TRACE("hwnd %04x, msg %08x, host %s, buffer %i\n",
578 (HWND16)hWnd, uMsg, (name)?name:"<null>", (int)buflen );
579 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,
580 AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN32|AQ_GETHOST);
583 /***********************************************************************
584 * WSAAsyncGetProtoByName() (WINSOCK.105)
586 HANDLE16 WINAPI WSAAsyncGetProtoByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
587 SEGPTR sbuf, INT16 buflen)
589 TRACE("hwnd %04x, msg %08x, protocol %s\n",
590 (HWND16)hWnd, uMsg, (name)?name:"<null>" );
591 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,
592 AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN16|AQ_GETPROTO);
595 /***********************************************************************
596 * WSAAsyncGetProtoByName() (WSOCK32.105)
598 HANDLE WINAPI WSAAsyncGetProtoByName(HWND hWnd, UINT uMsg, LPCSTR name,
599 LPSTR sbuf, INT buflen)
601 TRACE("hwnd %04x, msg %08x, protocol %s\n",
602 (HWND16)hWnd, uMsg, (name)?name:"<null>" );
603 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,
604 AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN32|AQ_GETPROTO);
608 /***********************************************************************
609 * WSAAsyncGetProtoByNumber() (WINSOCK.104)
611 HANDLE16 WINAPI WSAAsyncGetProtoByNumber16(HWND16 hWnd,UINT16 uMsg,INT16 number,
612 SEGPTR sbuf, INT16 buflen)
614 TRACE("hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
615 return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,(void*)sbuf,buflen,
616 AQ_GETPROTO|AQ_NUMBER|AQ_WIN16);
619 /***********************************************************************
620 * WSAAsyncGetProtoByNumber() (WSOCK32.104)
622 HANDLE WINAPI WSAAsyncGetProtoByNumber(HWND hWnd, UINT uMsg, INT number,
623 LPSTR sbuf, INT buflen)
625 TRACE("hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
626 return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,sbuf,buflen,
627 AQ_GETPROTO|AQ_NUMBER|AQ_WIN32);
630 /***********************************************************************
631 * WSAAsyncGetServByName() (WINSOCK.107)
633 HANDLE16 WINAPI WSAAsyncGetServByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
634 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
636 TRACE("hwnd %04x, msg %04x, name %s, proto %s\n",
637 hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>");
638 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,(void*)sbuf,buflen,
639 AQ_GETSERV|AQ_NAME|AQ_DUPLOWPTR1|AQ_DUPLOWPTR2|AQ_WIN16);
642 /***********************************************************************
643 * WSAAsyncGetServByName() (WSOCK32.107)
645 HANDLE WINAPI WSAAsyncGetServByName(HWND hWnd, UINT uMsg, LPCSTR name,
646 LPCSTR proto, LPSTR sbuf, INT buflen)
648 TRACE("hwnd %04x, msg %04x, name %s, proto %s\n",
649 hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>");
650 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,sbuf,buflen,
651 AQ_GETSERV|AQ_NAME|AQ_DUPLOWPTR1|AQ_DUPLOWPTR2|AQ_WIN32);
654 /***********************************************************************
655 * WSAAsyncGetServByPort() (WINSOCK.106)
657 HANDLE16 WINAPI WSAAsyncGetServByPort16(HWND16 hWnd, UINT16 uMsg, INT16 port,
658 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
660 TRACE("hwnd %04x, msg %04x, port %i, proto %s\n",
661 hWnd, uMsg, port, (proto)?proto:"<null>" );
662 return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,(void*)sbuf,buflen,
663 AQ_GETSERV|AQ_NUMBER|AQ_DUPLOWPTR2|AQ_WIN16);
666 /***********************************************************************
667 * WSAAsyncGetServByPort() (WSOCK32.106)
669 HANDLE WINAPI WSAAsyncGetServByPort(HWND hWnd, UINT uMsg, INT port,
670 LPCSTR proto, LPSTR sbuf, INT buflen)
672 TRACE("hwnd %04x, msg %04x, port %i, proto %s\n",
673 hWnd, uMsg, port, (proto)?proto:"<null>" );
674 return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,sbuf,buflen,
675 AQ_GETSERV|AQ_NUMBER|AQ_DUPLOWPTR2|AQ_WIN32);
678 /***********************************************************************
679 * WSACancelAsyncRequest() (WINSOCK.108)(WSOCK32.109)
681 INT WINAPI WSACancelAsyncRequest(HANDLE hAsyncTaskHandle)
683 FIXME("(%08x),stub\n", hAsyncTaskHandle);
687 INT16 WINAPI WSACancelAsyncRequest16(HANDLE16 hAsyncTaskHandle)
689 return (HANDLE16)WSACancelAsyncRequest((HANDLE)hAsyncTaskHandle);