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>
72 #ifdef HAVE_ARPA_NAMESER_H
73 # include <arpa/nameser.h>
79 #include "wine/winbase16.h"
83 #include "wine/winsock16.h"
88 #include "wine/port.h"
89 #include "debugtools.h"
91 DEFAULT_DEBUG_CHANNEL(winsock);
93 /* protoptypes of some functions in socket.c
95 UINT16 wsaErrno(void);
96 UINT16 wsaHerrno(void);
100 #define HB_WIN32(hb) (hb->flags & AQ_WIN32)
101 #define AQ_NUMBER 0x00
103 #define AQ_COPYPTR1 0x10
104 #define AQ_DUPLOWPTR1 0x20
105 #define AQ_MASKPTR1 0x30
106 #define AQ_COPYPTR2 0x40
107 #define AQ_DUPLOWPTR2 0x80
108 #define AQ_MASKPTR2 0xC0
111 #define AQ_GETPROTO 1
115 /* ----------------------------------- helper functions - */
117 static int list_size(char** l, int item_size)
122 j += (item_size) ? item_size : strlen(l[i]) + 1;
123 j += (i + 1) * sizeof(char*); }
127 static int list_dup(char** l_src, char* ref, char* base, int item_size)
129 /* base is either either equal to ref or 0 or SEGPTR */
132 char** l_to = (char**)ref;
135 for(j=0;l_src[j];j++) ;
136 p += (j + 1) * sizeof(char*);
138 { l_to[i] = base + (p - ref);
139 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
140 memcpy(p, l_src[i], k); p += k; }
147 static int hostent_size(struct hostent* p_he)
151 { size = sizeof(struct hostent);
152 size += strlen(p_he->h_name) + 1;
153 size += list_size(p_he->h_aliases, 0);
154 size += list_size(p_he->h_addr_list, p_he->h_length ); }
158 /* Copy hostent to p_to, fix up inside pointers using p_base (different for
159 * Win16 (linear vs. segmented). Return -neededsize on overrun.
161 static int WS_copy_he(char *p_to,char *p_base,int t_size,struct hostent* p_he, int flag)
163 char* p_name,*p_aliases,*p_addr,*p;
164 struct ws_hostent16 *p_to16 = (struct ws_hostent16*)p_to;
165 struct ws_hostent32 *p_to32 = (struct ws_hostent32*)p_to;
166 int size = hostent_size(p_he) +
168 (flag & AQ_WIN16) ? sizeof(struct ws_hostent16) : sizeof(struct ws_hostent32)
169 - sizeof(struct hostent)
175 p += (flag & AQ_WIN16) ?
176 sizeof(struct ws_hostent16) : sizeof(struct ws_hostent32);
178 strcpy(p, p_he->h_name); p += strlen(p) + 1;
180 p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)p_to), 0);
182 list_dup(p_he->h_addr_list, p, p_base + (p - (char*)p_to), p_he->h_length);
186 p_to16->h_addrtype = (INT16)p_he->h_addrtype;
187 p_to16->h_length = (INT16)p_he->h_length;
188 p_to16->h_name = (SEGPTR)(p_base + (p_name - p_to));
189 p_to16->h_aliases = (SEGPTR)(p_base + (p_aliases - p_to));
190 p_to16->h_addr_list = (SEGPTR)(p_base + (p_addr - p_to));
194 p_to32->h_addrtype = p_he->h_addrtype;
195 p_to32->h_length = p_he->h_length;
196 p_to32->h_name = (p_base + (p_name - p_to));
197 p_to32->h_aliases = (char **)(p_base + (p_aliases - p_to));
198 p_to32->h_addr_list = (char **)(p_base + (p_addr - p_to));
206 static int protoent_size(struct protoent* p_pe)
210 { size = sizeof(struct protoent);
211 size += strlen(p_pe->p_name) + 1;
212 size += list_size(p_pe->p_aliases, 0); }
216 /* Copy protoent to p_to, fix up inside pointers using p_base (different for
217 * Win16 (linear vs. segmented). Return -neededsize on overrun.
219 static int WS_copy_pe(char *p_to,char *p_base,int t_size,struct protoent* p_pe, int flag)
221 char* p_name,*p_aliases,*p;
222 struct ws_protoent16 *p_to16 = (struct ws_protoent16*)p_to;
223 struct ws_protoent32 *p_to32 = (struct ws_protoent32*)p_to;
224 int size = protoent_size(p_pe) +
226 (flag & AQ_WIN16) ? sizeof(struct ws_protoent16) : sizeof(struct ws_protoent32)
227 - sizeof(struct protoent)
233 p += (flag & AQ_WIN16) ?
234 sizeof(struct ws_protoent16) : sizeof(struct ws_protoent32);
236 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
238 list_dup(p_pe->p_aliases, p, p_base + (p - (char*)p_to), 0);
242 p_to16->p_proto = (INT16)p_pe->p_proto;
243 p_to16->p_name = (SEGPTR)(p_base) + (p_name - p_to);
244 p_to16->p_aliases = (SEGPTR)((p_base) + (p_aliases - p_to));
248 p_to32->p_proto = p_pe->p_proto;
249 p_to32->p_name = (p_base) + (p_name - p_to);
250 p_to32->p_aliases = (char **)((p_base) + (p_aliases - p_to));
258 static int servent_size(struct servent* p_se)
262 size += sizeof(struct servent);
263 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
264 size += list_size(p_se->s_aliases, 0);
269 /* Copy servent to p_to, fix up inside pointers using p_base (different for
270 * Win16 (linear vs. segmented). Return -neededsize on overrun.
271 * Take care of different Win16/Win32 servent structs (packing !)
273 static int WS_copy_se(char *p_to,char *p_base,int t_size,struct servent* p_se, int flag)
275 char* p_name,*p_aliases,*p_proto,*p;
276 struct ws_servent16 *p_to16 = (struct ws_servent16*)p_to;
277 struct ws_servent32 *p_to32 = (struct ws_servent32*)p_to;
278 int size = servent_size(p_se) +
280 (flag & AQ_WIN16) ? sizeof(struct ws_servent16) : sizeof(struct ws_servent32)
281 - sizeof(struct servent)
287 p += (flag & AQ_WIN16) ?
288 sizeof(struct ws_servent16) : sizeof(struct ws_servent32);
290 strcpy(p, p_se->s_name); p += strlen(p) + 1;
292 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
294 list_dup(p_se->s_aliases, p, p_base + (p - p_to), 0);
298 p_to16->s_port = (INT16)p_se->s_port;
299 p_to16->s_name = (SEGPTR)(p_base + (p_name - p_to));
300 p_to16->s_proto = (SEGPTR)(p_base + (p_proto - p_to));
301 p_to16->s_aliases = (SEGPTR)(p_base + (p_aliases - p_to));
305 p_to32->s_port = p_se->s_port;
306 p_to32->s_name = (p_base + (p_name - p_to));
307 p_to32->s_proto = (p_base + (p_proto - p_to));
308 p_to32->s_aliases = (char **)(p_base + (p_aliases - p_to));
314 static HANDLE __ws_async_handle = 0xdead;
316 /* Generic async query struct. we use symbolic names for the different queries
319 typedef struct _async_query {
323 #define host_name ptr1
324 #define host_addr ptr1
325 #define serv_name ptr1
326 #define proto_name ptr1
328 #define serv_proto ptr2
330 #define host_len int1
331 #define proto_number int1
332 #define serv_port int1
334 #define host_type int2
338 HANDLE16 async_handle;
345 /****************************************************************************
346 * The async query function.
348 * It is either called as a thread startup routine or directly. It has
349 * to free the passed arg from the process heap and PostMessageA the async
350 * result or the error code.
353 * - errorhandling not verified.
355 static DWORD WINAPI _async_queryfun(LPVOID arg) {
356 async_query *aq = (async_query*)arg;
359 char *targetptr = (HB_WIN32(aq)?(char*)aq->sbuf:(char*)PTR_SEG_TO_LIN(aq->sbuf));
361 switch (aq->flags & AQ_GETMASK) {
364 char *copy_hostent = targetptr;
366 he = (aq->flags & AQ_NAME) ?
367 gethostbyname(aq->host_name):
368 gethostbyaddr(aq->host_addr,aq->host_len,aq->host_type);
370 size = WS_copy_he(copy_hostent,(char*)aq->sbuf,aq->sbuflen,he,aq->flags);
376 fail = ((h_errno < 0) ? wsaErrno() : wsaHerrno());
382 char *copy_protoent = targetptr;
383 pe = (aq->flags & AQ_NAME)?
384 getprotobyname(aq->proto_name) :
385 getprotobynumber(aq->proto_number);
387 size = WS_copy_pe(copy_protoent,(char*)aq->sbuf,aq->sbuflen,pe,aq->flags);
393 if (aq->flags & AQ_NAME)
394 MESSAGE("protocol %s not found; You might want to add "
395 "this to /etc/protocols\n", debugstr_a(aq->proto_name) );
397 MESSAGE("protocol number %d not found; You might want to add "
398 "this to /etc/protocols\n", aq->proto_number );
405 char *copy_servent = targetptr;
406 se = (aq->flags & AQ_NAME)?
407 getservbyname(aq->serv_name,aq->serv_proto) :
408 getservbyport(aq->serv_port,aq->serv_proto);
410 size = WS_copy_se(copy_servent,(char*)aq->sbuf,aq->sbuflen,se,aq->flags);
416 if (aq->flags & AQ_NAME)
417 MESSAGE("service %s protocol %s not found; You might want to add "
418 "this to /etc/services\n", debugstr_a(aq->serv_name) ,
419 aq->serv_proto ? debugstr_a(aq->serv_proto ):"*");
421 MESSAGE("service on port %d protocol %s not found; You might want to add "
422 "this to /etc/services\n", aq->serv_port,
423 aq->serv_proto ? debugstr_a(aq->serv_proto ):"*");
429 PostMessageA(aq->hWnd,aq->uMsg,aq->async_handle,size|(fail<<16));
430 HeapFree(GetProcessHeap(),0,arg);
434 /****************************************************************************
435 * The main async help function.
437 * It either starts a thread or just calls the function directly for platforms
438 * with no thread support. This relies on the fact that PostMessage() does
439 * not actually call the windowproc before the function returns.
441 static HANDLE16 __WSAsyncDBQuery(
442 HWND hWnd, UINT uMsg,INT int1,LPCSTR ptr1, INT int2, LPCSTR ptr2,
443 void *sbuf, INT sbuflen, UINT flags
451 /* allocate buffer to copy protocol- and service name to */
452 /* note: this is done in the calling thread so we can return */
453 /* a decent error code if the Alloc fails */
455 switch (flags & AQ_MASKPTR1) {
457 case AQ_COPYPTR1: xbuflen += int1; break;
458 case AQ_DUPLOWPTR1: xbuflen += strlen(ptr1) + 1; break;
461 switch (flags & AQ_MASKPTR2) {
463 case AQ_COPYPTR2: xbuflen += int2; break;
464 case AQ_DUPLOWPTR2: xbuflen += strlen(ptr2) + 1; break;
467 if(!(aq = HeapAlloc(GetProcessHeap(),0,sizeof(async_query) + xbuflen))) {
468 SetLastError(WSAEWOULDBLOCK); /* insufficient resources */
473 if (ptr1) switch (flags & AQ_MASKPTR1) {
475 case AQ_COPYPTR1: memcpy(pto, ptr1, int1); ptr1 = pto; pto += int1; break;
476 case AQ_DUPLOWPTR1: pfm = ptr1; ptr1 = pto; do *pto++ = tolower(*pfm); while (*pfm++); break;
478 if (ptr2) switch (flags & AQ_MASKPTR2) {
480 case AQ_COPYPTR2: memcpy(pto, ptr2, int2); ptr2 = pto; pto += int2; break;
481 case AQ_DUPLOWPTR2: pfm = ptr2; ptr2 = pto; do *pto++ = tolower(*pfm); while (*pfm++); break;
490 aq->async_handle = ++__ws_async_handle;
492 aq->sbuf = (SEGPTR)sbuf;
493 aq->sbuflen = sbuflen;
496 if (CreateThread(NULL,0,_async_queryfun,aq,0,NULL) == INVALID_HANDLE_VALUE)
499 return __ws_async_handle;
503 /***********************************************************************
504 * WSAAsyncGetHostByAddr() (WINSOCK.102)
506 HANDLE16 WINAPI WSAAsyncGetHostByAddr16(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,
507 INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen)
509 TRACE("hwnd %04x, msg %04x, addr %08x[%i]\n",
510 hWnd, uMsg, (unsigned)addr , len );
511 return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,(void*)sbuf,buflen,
512 AQ_NUMBER|AQ_COPYPTR1|AQ_WIN16|AQ_GETHOST);
515 /***********************************************************************
516 * WSAAsyncGetHostByAddr() (WSOCK32.102)
518 HANDLE WINAPI WSAAsyncGetHostByAddr(HWND hWnd, UINT uMsg, LPCSTR addr,
519 INT len, INT type, LPSTR sbuf, INT buflen)
521 TRACE("hwnd %04x, msg %04x, addr %08x[%i]\n",
522 hWnd, uMsg, (unsigned)addr , len );
523 return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,sbuf,buflen,
524 AQ_NUMBER|AQ_COPYPTR1|AQ_WIN32|AQ_GETHOST);
527 /***********************************************************************
528 * WSAAsyncGetHostByName() (WINSOCK.103)
530 HANDLE16 WINAPI WSAAsyncGetHostByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
531 SEGPTR sbuf, INT16 buflen)
533 TRACE("hwnd %04x, msg %04x, host %s, buffer %i\n",
534 hWnd, uMsg, (name)?name:"<null>", (int)buflen );
535 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,
536 AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN16|AQ_GETHOST);
539 /***********************************************************************
540 * WSAAsyncGetHostByName() (WSOCK32.103)
542 HANDLE WINAPI WSAAsyncGetHostByName(HWND hWnd, UINT uMsg, LPCSTR name,
543 LPSTR sbuf, INT buflen)
545 TRACE("hwnd %04x, msg %08x, host %s, buffer %i\n",
546 (HWND16)hWnd, uMsg, (name)?name:"<null>", (int)buflen );
547 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,
548 AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN32|AQ_GETHOST);
551 /***********************************************************************
552 * WSAAsyncGetProtoByName() (WINSOCK.105)
554 HANDLE16 WINAPI WSAAsyncGetProtoByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
555 SEGPTR sbuf, INT16 buflen)
557 TRACE("hwnd %04x, msg %08x, protocol %s\n",
558 (HWND16)hWnd, uMsg, (name)?name:"<null>" );
559 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,
560 AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN16|AQ_GETPROTO);
563 /***********************************************************************
564 * WSAAsyncGetProtoByName() (WSOCK32.105)
566 HANDLE WINAPI WSAAsyncGetProtoByName(HWND hWnd, UINT uMsg, LPCSTR name,
567 LPSTR sbuf, INT buflen)
569 TRACE("hwnd %04x, msg %08x, protocol %s\n",
570 (HWND16)hWnd, uMsg, (name)?name:"<null>" );
571 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,
572 AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN32|AQ_GETPROTO);
576 /***********************************************************************
577 * WSAAsyncGetProtoByNumber() (WINSOCK.104)
579 HANDLE16 WINAPI WSAAsyncGetProtoByNumber16(HWND16 hWnd,UINT16 uMsg,INT16 number,
580 SEGPTR sbuf, INT16 buflen)
582 TRACE("hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
583 return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,(void*)sbuf,buflen,
584 AQ_GETPROTO|AQ_NUMBER|AQ_WIN16);
587 /***********************************************************************
588 * WSAAsyncGetProtoByNumber() (WSOCK32.104)
590 HANDLE WINAPI WSAAsyncGetProtoByNumber(HWND hWnd, UINT uMsg, INT number,
591 LPSTR sbuf, INT buflen)
593 TRACE("hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
594 return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,sbuf,buflen,
595 AQ_GETPROTO|AQ_NUMBER|AQ_WIN32);
598 /***********************************************************************
599 * WSAAsyncGetServByName() (WINSOCK.107)
601 HANDLE16 WINAPI WSAAsyncGetServByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
602 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
604 TRACE("hwnd %04x, msg %04x, name %s, proto %s\n",
605 hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>");
606 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,(void*)sbuf,buflen,
607 AQ_GETSERV|AQ_NAME|AQ_DUPLOWPTR1|AQ_DUPLOWPTR2|AQ_WIN16);
610 /***********************************************************************
611 * WSAAsyncGetServByName() (WSOCK32.107)
613 HANDLE WINAPI WSAAsyncGetServByName(HWND hWnd, UINT uMsg, LPCSTR name,
614 LPCSTR proto, LPSTR sbuf, INT buflen)
616 TRACE("hwnd %04x, msg %04x, name %s, proto %s\n",
617 hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>");
618 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,sbuf,buflen,
619 AQ_GETSERV|AQ_NAME|AQ_DUPLOWPTR1|AQ_DUPLOWPTR2|AQ_WIN32);
622 /***********************************************************************
623 * WSAAsyncGetServByPort() (WINSOCK.106)
625 HANDLE16 WINAPI WSAAsyncGetServByPort16(HWND16 hWnd, UINT16 uMsg, INT16 port,
626 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
628 TRACE("hwnd %04x, msg %04x, port %i, proto %s\n",
629 hWnd, uMsg, port, (proto)?proto:"<null>" );
630 return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,(void*)sbuf,buflen,
631 AQ_GETSERV|AQ_NUMBER|AQ_DUPLOWPTR2|AQ_WIN16);
634 /***********************************************************************
635 * WSAAsyncGetServByPort() (WSOCK32.106)
637 HANDLE WINAPI WSAAsyncGetServByPort(HWND hWnd, UINT uMsg, INT port,
638 LPCSTR proto, LPSTR sbuf, INT buflen)
640 TRACE("hwnd %04x, msg %04x, port %i, proto %s\n",
641 hWnd, uMsg, port, (proto)?proto:"<null>" );
642 return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,sbuf,buflen,
643 AQ_GETSERV|AQ_NUMBER|AQ_DUPLOWPTR2|AQ_WIN32);
646 /***********************************************************************
647 * WSACancelAsyncRequest() (WINSOCK.108)(WSOCK32.109)
649 INT WINAPI WSACancelAsyncRequest(HANDLE hAsyncTaskHandle)
651 FIXME("(%08x),stub\n", hAsyncTaskHandle);
655 INT16 WINAPI WSACancelAsyncRequest16(HANDLE16 hAsyncTaskHandle)
657 return (HANDLE16)WSACancelAsyncRequest((HANDLE)hAsyncTaskHandle);