]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/net/name6.c
Include more robust checking of end of buffer that more completely
[FreeBSD/FreeBSD.git] / lib / libc / net / name6.c
1 /*      $KAME: name6.c,v 1.25 2000/06/26 16:44:40 itojun Exp $  */
2
3 /*
4  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 /*
32  * ++Copyright++ 1985, 1988, 1993
33  * -
34  * Copyright (c) 1985, 1988, 1993
35  *    The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *      This product includes software developed by the University of
48  *      California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  * -
65  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
66  *
67  * Permission to use, copy, modify, and distribute this software for any
68  * purpose with or without fee is hereby granted, provided that the above
69  * copyright notice and this permission notice appear in all copies, and that
70  * the name of Digital Equipment Corporation not be used in advertising or
71  * publicity pertaining to distribution of the document or software without
72  * specific, written prior permission.
73  *
74  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
75  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
77  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
78  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
79  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
80  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
81  * SOFTWARE.
82  * -
83  * --Copyright--
84  */
85
86 /*
87  *      Atsushi Onoe <onoe@sm.sony.co.jp>
88  */
89
90 /*
91  * TODO for thread safe
92  *      use mutex for _hostconf, _hostconf_init.
93  *      rewrite resolvers to be thread safe
94  */
95
96 #include <sys/cdefs.h>
97 __FBSDID("$FreeBSD$");
98
99 #include "namespace.h"
100 #include <sys/param.h>
101 #include <sys/socket.h>
102 #include <sys/time.h>
103 #include <sys/queue.h>
104 #include <netinet/in.h>
105
106 #include <arpa/inet.h>
107 #include <arpa/nameser.h>
108
109 #include <errno.h>
110 #include <netdb.h>
111 #include <resolv.h>
112 #include <stdio.h>
113 #include <stdlib.h>
114 #include <string.h>
115 #include <stdarg.h>
116 #include <nsswitch.h>
117 #include <unistd.h>
118 #include "un-namespace.h"
119
120 #ifndef _PATH_HOSTS
121 #define _PATH_HOSTS     "/etc/hosts"
122 #endif
123
124 #ifndef MAXALIASES
125 #define MAXALIASES      10
126 #endif
127 #ifndef MAXADDRS
128 #define MAXADDRS        20
129 #endif
130 #ifndef MAXDNAME
131 #define MAXDNAME        1025
132 #endif
133
134 #ifdef INET6
135 #define ADDRLEN(af)     ((af) == AF_INET6 ? sizeof(struct in6_addr) : \
136                                             sizeof(struct in_addr))
137 #else
138 #define ADDRLEN(af)     sizeof(struct in_addr)
139 #endif
140
141 #define MAPADDR(ab, ina) \
142 do {                                                                    \
143         memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr));         \
144         memset((ab)->map_zero, 0, sizeof((ab)->map_zero));              \
145         memset((ab)->map_one, 0xff, sizeof((ab)->map_one));             \
146 } while (0)
147 #define MAPADDRENABLED(flags) \
148         (((flags) & AI_V4MAPPED) || \
149          (((flags) & AI_V4MAPPED_CFG) && _mapped_addr_enabled()))
150
151 union inx_addr {
152         struct in_addr  in_addr;
153 #ifdef INET6
154         struct in6_addr in6_addr;
155 #endif
156         struct {
157                 u_char  mau_zero[10];
158                 u_char  mau_one[2];
159                 struct in_addr mau_inaddr;
160         }               map_addr_un;
161 #define map_zero        map_addr_un.mau_zero
162 #define map_one         map_addr_un.mau_one
163 #define map_inaddr      map_addr_un.mau_inaddr
164 };
165
166 static struct    hostent *_hpcopy(struct hostent *hp, int *errp);
167 static struct    hostent *_hpaddr(int af, const char *name, void *addr, int *errp);
168 static struct    hostent *_hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp);
169 #ifdef INET6
170 static struct    hostent *_hpmapv6(struct hostent *hp, int *errp);
171 #endif
172 static struct    hostent *_hpsort(struct hostent *hp);
173 static struct    hostent *_ghbyname(const char *name, int af, int flags, int *errp);
174 static char     *_hgetword(char **pp);
175 static int       _mapped_addr_enabled(void);
176
177 static FILE     *_files_open(int *errp);
178 static int       _files_ghbyname(void *, void *, va_list);
179 static int       _files_ghbyaddr(void *, void *, va_list);
180 static void      _files_shent(int stayopen);
181 static void      _files_ehent(void);
182 #ifdef YP
183 static int       _nis_ghbyname(void *, void *, va_list);
184 static int       _nis_ghbyaddr(void *, void *, va_list);
185 #endif
186 static int       _dns_ghbyname(void *, void *, va_list);
187 static int       _dns_ghbyaddr(void *, void *, va_list);
188 static void      _dns_shent(int stayopen);
189 static void      _dns_ehent(void);
190 #ifdef ICMPNL
191 static int       _icmp_ghbyaddr(void *, void *, va_list);
192 #endif /* ICMPNL */
193
194 /* Host lookup order if nsswitch.conf is broken or nonexistant */
195 static const ns_src default_src[] = { 
196         { NSSRC_FILES, NS_SUCCESS },
197         { NSSRC_DNS, NS_SUCCESS },
198 #ifdef ICMPNL
199 #define NSSRC_ICMP "icmp"
200         { NSSRC_ICMP, NS_SUCCESS },
201 #endif
202         { 0 }
203 };
204
205 /*
206  * Check if kernel supports mapped address.
207  *      implementation dependent
208  */
209 #ifdef __KAME__
210 #include <sys/sysctl.h>
211 #endif /* __KAME__ */
212
213 static int
214 _mapped_addr_enabled(void)
215 {
216         /* implementation dependent check */
217 #if defined(__KAME__) && defined(IPV6CTL_MAPPED_ADDR)
218         int mib[4];
219         size_t len;
220         int val;
221
222         mib[0] = CTL_NET;
223         mib[1] = PF_INET6;
224         mib[2] = IPPROTO_IPV6;
225         mib[3] = IPV6CTL_MAPPED_ADDR;
226         len = sizeof(val);
227         if (sysctl(mib, 4, &val, &len, 0, 0) == 0 && val != 0)
228                 return 1;
229 #endif /* __KAME__ && IPV6CTL_MAPPED_ADDR */
230         return 0;
231 }
232
233 /*
234  * Functions defined in RFC2553
235  *      getipnodebyname, getipnodebyaddr, freehostent
236  */
237
238 static struct hostent *
239 _ghbyname(const char *name, int af, int flags, int *errp)
240 {
241         struct hostent *hp;
242         int rval;
243         
244         static const ns_dtab dtab[] = {
245                 NS_FILES_CB(_files_ghbyname, NULL)
246                 { NSSRC_DNS, _dns_ghbyname, NULL },
247                 NS_NIS_CB(_nis_ghbyname, NULL)
248                 { 0 }
249         };
250
251         if (flags & AI_ADDRCONFIG) {
252                 int s;
253
254                 /*
255                  * TODO:
256                  * Note that implementation dependent test for address
257                  * configuration should be done everytime called
258                  * (or apropriate interval),
259                  * because addresses will be dynamically assigned or deleted.
260                  */
261                 if (af == AF_UNSPEC) {
262                         if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
263                                 af = AF_INET;
264                         else {
265                                 _close(s);
266                                 if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0)
267                                         af = AF_INET6;
268                                 else
269                                 _close(s);
270                         }
271
272                 }
273                 if (af != AF_UNSPEC) {
274                         if ((s = _socket(af, SOCK_DGRAM, 0)) < 0)
275                                 return NULL;
276                         _close(s);
277                 }
278         }
279
280         rval = nsdispatch(&hp, dtab, NSDB_HOSTS, "ghbyname", default_src,
281                           name, af, errp);
282         return (rval == NS_SUCCESS) ? hp : NULL;
283 }
284
285 /* getipnodebyname() internal routine for multiple query(PF_UNSPEC) support. */
286 struct hostent *
287 _getipnodebyname_multi(const char *name, int af, int flags, int *errp)
288 {
289         struct hostent *hp;
290         union inx_addr addrbuf;
291
292         /* XXX: PF_UNSPEC is only supposed to be passed from getaddrinfo() */
293         if (af != AF_INET
294 #ifdef INET6
295             && af != AF_INET6
296 #endif
297             && af != PF_UNSPEC
298                 )
299         {
300                 *errp = NO_RECOVERY;
301                 return NULL;
302         }
303
304 #ifdef INET6
305         /* special case for literal address */
306         if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
307                 if (af != AF_INET6) {
308                         *errp = HOST_NOT_FOUND;
309                         return NULL;
310                 }
311                 return _hpaddr(af, name, &addrbuf, errp);
312         }
313 #endif
314         if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) {
315                 if (af != AF_INET) {
316                         if (MAPADDRENABLED(flags)) {
317                                 MAPADDR(&addrbuf, &addrbuf.in_addr);
318                         } else {
319                                 *errp = HOST_NOT_FOUND;
320                                 return NULL;
321                         }
322                 }
323                 return _hpaddr(af, name, &addrbuf, errp);
324         }
325
326         *errp = HOST_NOT_FOUND;
327         hp = _ghbyname(name, af, flags, errp);
328
329 #ifdef INET6
330         if (af == AF_INET6
331         &&  ((flags & AI_ALL) || hp == NULL)
332         &&  (MAPADDRENABLED(flags))) {
333                 struct hostent *hp2 = _ghbyname(name, AF_INET, flags, errp);
334                 if (hp == NULL)
335                         hp = _hpmapv6(hp2, errp);
336                 else {
337                         if (hp2 && strcmp(hp->h_name, hp2->h_name) != 0) {
338                                 freehostent(hp2);
339                                 hp2 = NULL;
340                         }
341                         hp = _hpmerge(hp, hp2, errp);
342                 }
343         }
344 #endif
345         return _hpsort(hp);
346 }
347
348 struct hostent *
349 getipnodebyname(const char *name, int af, int flags, int *errp)
350 {
351         if (af != AF_INET
352 #ifdef INET6
353             && af != AF_INET6
354 #endif
355                 )
356         {
357                 *errp = NO_RECOVERY;
358                 return NULL;
359         }
360         return(_getipnodebyname_multi(name, af ,flags, errp));
361 }
362
363 struct hostent *
364 getipnodebyaddr(const void *src, size_t len, int af, int *errp)
365 {
366         struct hostent *hp;
367         int rval;
368 #ifdef INET6
369         struct in6_addr addrbuf;
370 #else
371         struct in_addr addrbuf;
372 #endif
373
374         static const ns_dtab dtab[] = {
375                 NS_FILES_CB(_files_ghbyaddr, NULL)
376                 { NSSRC_DNS, _dns_ghbyaddr, NULL },
377                 NS_NIS_CB(_nis_ghbyaddr, NULL)
378 #ifdef ICMPNL
379                 { NSSRC_ICMP, _icmp_ghbyaddr, NULL },
380 #endif
381                 { 0 }
382         };
383
384         *errp = HOST_NOT_FOUND;
385
386         switch (af) {
387         case AF_INET:
388                 if (len != sizeof(struct in_addr)) {
389                         *errp = NO_RECOVERY;
390                         return NULL;
391                 }
392                 if ((long)src & ~(sizeof(struct in_addr) - 1)) {
393                         memcpy(&addrbuf, src, len);
394                         src = &addrbuf;
395                 }
396                 if (((struct in_addr *)src)->s_addr == 0)
397                         return NULL;
398                 break;
399 #ifdef INET6
400         case AF_INET6:
401                 if (len != sizeof(struct in6_addr)) {
402                         *errp = NO_RECOVERY;
403                         return NULL;
404                 }
405                 if ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) {   /*XXX*/
406                         memcpy(&addrbuf, src, len);
407                         src = &addrbuf;
408                 }
409                 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src))
410                         return NULL;
411                 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)
412                 ||  IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) {
413                         src = (char *)src +
414                             (sizeof(struct in6_addr) - sizeof(struct in_addr));
415                         af = AF_INET;
416                         len = sizeof(struct in_addr);
417                 }
418                 break;
419 #endif
420         default:
421                 *errp = NO_RECOVERY;
422                 return NULL;
423         }
424
425         rval = nsdispatch(&hp, dtab, NSDB_HOSTS, "ghbyaddr", default_src,
426                           src, len, af, errp);
427         return (rval == NS_SUCCESS) ? hp : NULL;
428 }
429
430 void
431 freehostent(struct hostent *ptr)
432 {
433         free(ptr);
434 }
435
436 #if 0
437
438 /* XXX: should be deprecated */
439 struct hostent *
440 getnodebyname(const char *name, int af, int flags)
441 {
442         return getipnodebyname(name, af, flags, &h_errno);
443 }
444
445 #ifdef __warn_references
446 __warn_references(getnodebyname,
447         "warning: getnodebyname() deprecated, "
448         "should use getaddrinfo() or getipnodebyname()");
449 #endif
450
451 struct hostent *
452 getnodebyaddr(const void *src, size_t len, int af)
453 {
454         return getipnodebyaddr(src, len, af, &h_errno);
455 }
456
457 #ifdef __warn_references
458 __warn_references(getnodebyaddr,
459         "warning: getnodebyaddr() deprecated, "
460         "should use getnameinfo() or getipnodebyaddr()");
461 #endif
462
463 #endif
464
465 /*
466  * Private utility functions
467  */
468
469 /*
470  * _hpcopy: allocate and copy hostent structure
471  */
472 static struct hostent *
473 _hpcopy(struct hostent *hp, int *errp)
474 {
475         struct hostent *nhp;
476         char *cp, **pp;
477         int size, addrsize;
478         int nalias = 0, naddr = 0;
479         int al_off;
480         int i;
481
482         if (hp == NULL)
483                 return hp;
484
485         /* count size to be allocated */
486         size = sizeof(struct hostent);
487         if (hp->h_name != NULL)
488                 size += strlen(hp->h_name) + 1;
489         if ((pp = hp->h_aliases) != NULL) {
490                 for (i = 0; *pp != NULL; i++, pp++) {
491                         if (**pp != '\0') {
492                                 size += strlen(*pp) + 1;
493                                 nalias++;
494                         }
495                 }
496         }
497         /* adjust alignment */
498         size = ALIGN(size);
499         al_off = size;
500         size += sizeof(char *) * (nalias + 1);
501         addrsize = ALIGN(hp->h_length);
502         if ((pp = hp->h_addr_list) != NULL) {
503                 while (*pp++ != NULL)
504                         naddr++;
505         }
506         size += addrsize * naddr;
507         size += sizeof(char *) * (naddr + 1);
508
509         /* copy */
510         if ((nhp = (struct hostent *)malloc(size)) == NULL) {
511                 *errp = TRY_AGAIN;
512                 return NULL;
513         }
514         cp = (char *)&nhp[1];
515         if (hp->h_name != NULL) {
516                 nhp->h_name = cp;
517                 strcpy(cp, hp->h_name);
518                 cp += strlen(cp) + 1;
519         } else
520                 nhp->h_name = NULL;
521         nhp->h_aliases = (char **)((char *)nhp + al_off);
522         if ((pp = hp->h_aliases) != NULL) {
523                 for (i = 0; *pp != NULL; pp++) {
524                         if (**pp != '\0') {
525                                 nhp->h_aliases[i++] = cp;
526                                 strcpy(cp, *pp);
527                                 cp += strlen(cp) + 1;
528                         }
529                 }
530         }
531         nhp->h_aliases[nalias] = NULL;
532         cp = (char *)&nhp->h_aliases[nalias + 1];
533         nhp->h_addrtype = hp->h_addrtype;
534         nhp->h_length = hp->h_length;
535         nhp->h_addr_list = (char **)cp;
536         if ((pp = hp->h_addr_list) != NULL) {
537                 cp = (char *)&nhp->h_addr_list[naddr + 1];
538                 for (i = 0; *pp != NULL; pp++) {
539                         nhp->h_addr_list[i++] = cp;
540                         memcpy(cp, *pp, hp->h_length);
541                         cp += addrsize;
542                 }
543         }
544         nhp->h_addr_list[naddr] = NULL;
545         return nhp;
546 }
547
548 /*
549  * _hpaddr: construct hostent structure with one address
550  */
551 static struct hostent *
552 _hpaddr(int af, const char *name, void *addr, int *errp)
553 {
554         struct hostent *hp, hpbuf;
555         char *addrs[2];
556
557         hp = &hpbuf;
558         hp->h_name = (char *)name;
559         hp->h_aliases = NULL;
560         hp->h_addrtype = af;
561         hp->h_length = ADDRLEN(af);
562         hp->h_addr_list = addrs;
563         addrs[0] = (char *)addr;
564         addrs[1] = NULL;
565         return _hpcopy(hp, errp);
566 }
567
568 /*
569  * _hpmerge: merge 2 hostent structure, arguments will be freed
570  */
571 static struct hostent *
572 _hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp)
573 {
574         int i, j;
575         int naddr, nalias;
576         char **pp;
577         struct hostent *hp, hpbuf;
578         char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1];
579         union inx_addr addrbuf[MAXADDRS];
580
581         if (hp1 == NULL)
582                 return hp2;
583         if (hp2 == NULL)
584                 return hp1;
585
586 #define HP(i)   (i == 1 ? hp1 : hp2)
587         hp = &hpbuf;
588         hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name);
589         hp->h_aliases = aliases;
590         nalias = 0;
591         for (i = 1; i <= 2; i++) {
592                 if ((pp = HP(i)->h_aliases) == NULL)
593                         continue;
594                 for (; nalias < MAXALIASES && *pp != NULL; pp++) {
595                         /* check duplicates */
596                         for (j = 0; j < nalias; j++)
597                                 if (strcasecmp(*pp, aliases[j]) == 0)
598                                         break;
599                         if (j == nalias)
600                                 aliases[nalias++] = *pp;
601                 }
602         }
603         aliases[nalias] = NULL;
604 #ifdef INET6
605         if (hp1->h_length != hp2->h_length) {
606                 hp->h_addrtype = AF_INET6;
607                 hp->h_length = sizeof(struct in6_addr);
608         } else {
609 #endif
610                 hp->h_addrtype = hp1->h_addrtype;
611                 hp->h_length = hp1->h_length;
612 #ifdef INET6
613         }
614 #endif
615         hp->h_addr_list = addrs;
616         naddr = 0;
617         for (i = 1; i <= 2; i++) {
618                 if ((pp = HP(i)->h_addr_list) == NULL)
619                         continue;
620                 if (HP(i)->h_length == hp->h_length) {
621                         while (naddr < MAXADDRS && *pp != NULL)
622                                 addrs[naddr++] = *pp++;
623                 } else {
624                         /* copy IPv4 addr as mapped IPv6 addr */
625                         while (naddr < MAXADDRS && *pp != NULL) {
626                                 MAPADDR(&addrbuf[naddr], *pp++);
627                                 addrs[naddr] = (char *)&addrbuf[naddr];
628                                 naddr++;
629                         }
630                 }
631         }
632         addrs[naddr] = NULL;
633         hp = _hpcopy(hp, errp);
634         freehostent(hp1);
635         freehostent(hp2);
636         return hp;
637 }
638
639 /*
640  * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses
641  */
642 #ifdef INET6
643 static struct hostent *
644 _hpmapv6(struct hostent *hp, int *errp)
645 {
646         struct hostent *hp6;
647
648         if (hp == NULL)
649                 return NULL;
650         if (hp->h_addrtype == AF_INET6)
651                 return hp;
652
653         /* make dummy hostent to convert IPv6 address */
654         if ((hp6 = (struct hostent *)malloc(sizeof(struct hostent))) == NULL) {
655                 *errp = TRY_AGAIN;
656                 return NULL;
657         }
658         hp6->h_name = NULL;
659         hp6->h_aliases = NULL;
660         hp6->h_addrtype = AF_INET6;
661         hp6->h_length = sizeof(struct in6_addr);
662         hp6->h_addr_list = NULL;
663         return _hpmerge(hp6, hp, errp);
664 }
665 #endif
666
667 /*
668  * _hpsort: sort address by sortlist
669  */
670 static struct hostent *
671 _hpsort(struct hostent *hp)
672 {
673         int i, j, n;
674         u_char *ap, *sp, *mp, **pp;
675         char t;
676         char order[MAXADDRS];
677         int nsort = _res.nsort;
678
679         if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0)
680                 return hp;
681         for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
682                 for (j = 0; j < nsort; j++) {
683 #ifdef INET6
684                         if (_res_ext.sort_list[j].af != hp->h_addrtype)
685                                 continue;
686                         sp = (u_char *)&_res_ext.sort_list[j].addr;
687                         mp = (u_char *)&_res_ext.sort_list[j].mask;
688 #else
689                         sp = (u_char *)&_res.sort_list[j].addr;
690                         mp = (u_char *)&_res.sort_list[j].mask;
691 #endif
692                         for (n = 0; n < hp->h_length; n++) {
693                                 if ((ap[n] & mp[n]) != sp[n])
694                                         break;
695                         }
696                         if (n == hp->h_length)
697                                 break;
698                 }
699                 order[i] = j;
700         }
701         n = i;
702         pp = (u_char **)hp->h_addr_list;
703         for (i = 0; i < n - 1; i++) {
704                 for (j = i + 1; j < n; j++) {
705                         if (order[i] > order[j]) {
706                                 ap = pp[i];
707                                 pp[i] = pp[j];
708                                 pp[j] = ap;
709                                 t = order[i];
710                                 order[i] = order[j];
711                                 order[j] = t;
712                         }
713                 }
714         }
715         return hp;
716 }
717
718 static char *
719 _hgetword(char **pp)
720 {
721         char c, *p, *ret;
722         const char *sp;
723         static const char sep[] = "# \t\n";
724
725         ret = NULL;
726         for (p = *pp; (c = *p) != '\0'; p++) {
727                 for (sp = sep; *sp != '\0'; sp++) {
728                         if (c == *sp)
729                                 break;
730                 }
731                 if (c == '#')
732                         p[1] = '\0';    /* ignore rest of line */
733                 if (ret == NULL) {
734                         if (*sp == '\0')
735                                 ret = p;
736                 } else {
737                         if (*sp != '\0') {
738                                 *p++ = '\0';
739                                 break;
740                         }
741                 }
742         }
743         *pp = p;
744         if (ret == NULL || *ret == '\0')
745                 return NULL;
746         return ret;
747 }
748
749 /*
750  * FILES (/etc/hosts)
751  */
752
753 static FILE *
754 _files_open(int *errp)
755 {
756         FILE *fp;
757         fp = fopen(_PATH_HOSTS, "r");
758         if (fp == NULL)
759                 *errp = NO_RECOVERY;
760         return fp;
761 }
762
763 static int
764 _files_ghbyname(void *rval, void *cb_data, va_list ap)
765 {
766         const char *name;
767         int af; 
768         int *errp;
769         int match, nalias;
770         char *p, *line, *addrstr, *cname;
771         FILE *fp;
772         struct hostent *rethp, *hp, hpbuf;
773         char *aliases[MAXALIASES + 1], *addrs[2];
774         union inx_addr addrbuf;
775         char buf[BUFSIZ];
776         int af0;
777
778         name = va_arg(ap, const char *);
779         af = va_arg(ap, int);
780         errp = va_arg(ap, int *);
781
782         *(struct hostent **)rval = NULL;
783
784         if ((fp = _files_open(errp)) == NULL)
785                 return NS_UNAVAIL;
786         rethp = hp = NULL;
787
788         af0 = af;
789         while (fgets(buf, sizeof(buf), fp)) {
790                 line = buf;
791                 if ((addrstr = _hgetword(&line)) == NULL
792                 ||  (cname = _hgetword(&line)) == NULL)
793                         continue;
794                 match = (strcasecmp(cname, name) == 0);
795                 nalias = 0;
796                 while ((p = _hgetword(&line)) != NULL) {
797                         if (!match)
798                                 match = (strcasecmp(p, name) == 0);
799                         if (nalias < MAXALIASES)
800                                 aliases[nalias++] = p;
801                 }
802                 if (!match)
803                         continue;
804                 switch (af0) {
805                 case AF_INET:
806                         if (inet_aton(addrstr, (struct in_addr *)&addrbuf)
807                             != 1) {
808                                 *errp = NO_DATA;        /* name found */
809                                 continue;
810                         }
811                         af = af0;
812                         break;
813 #ifdef INET6
814                 case AF_INET6:
815                         if (inet_pton(af, addrstr, &addrbuf) != 1) {
816                                 *errp = NO_DATA;        /* name found */
817                                 continue;
818                         }
819                         af = af0;
820                         break;
821 #endif
822                 case AF_UNSPEC:
823                         if (inet_aton(addrstr, (struct in_addr *)&addrbuf)
824                             == 1) {
825                                 af = AF_INET;
826                                 break;
827                         }
828 #ifdef INET6
829                         if (inet_pton(AF_INET6, addrstr, &addrbuf) == 1) {
830                                 af = AF_INET6;
831                                 break; 
832                         }
833 #endif
834                         *errp = NO_DATA;        /* name found */
835                         continue;
836                         /* NOTREACHED */
837                 }
838                 hp = &hpbuf;
839                 hp->h_name = cname;
840                 hp->h_aliases = aliases;
841                 aliases[nalias] = NULL;
842                 hp->h_addrtype = af;
843                 hp->h_length = ADDRLEN(af);
844                 hp->h_addr_list = addrs;
845                 addrs[0] = (char *)&addrbuf;
846                 addrs[1] = NULL;
847                 hp = _hpcopy(hp, errp);
848                 rethp = _hpmerge(rethp, hp, errp);
849         }
850         fclose(fp);
851         *(struct hostent **)rval = rethp;
852         return (rethp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
853 }
854
855 static int
856 _files_ghbyaddr(void *rval, void *cb_data, va_list ap)
857 {
858         const void *addr; 
859         int addrlen; 
860         int af; 
861         int *errp;
862         int nalias;
863         char *p, *line;
864         FILE *fp;
865         struct hostent *hp, hpbuf;
866         char *aliases[MAXALIASES + 1], *addrs[2];
867         union inx_addr addrbuf;
868         char buf[BUFSIZ];
869
870         addr = va_arg(ap, const void *);
871         addrlen = va_arg(ap, int);
872         af = va_arg(ap, int);
873         errp = va_arg(ap, int *);
874
875         *(struct hostent**)rval = NULL;
876
877         if ((fp = _files_open(errp)) == NULL)
878                 return NS_UNAVAIL;
879         hp = NULL;
880         while (fgets(buf, sizeof(buf), fp)) {
881                 line = buf;
882                 if ((p = _hgetword(&line)) == NULL
883                 ||  (af == AF_INET
884                      ? inet_aton(p, (struct in_addr *)&addrbuf)
885                      : inet_pton(af, p, &addrbuf)) != 1
886                 ||  memcmp(addr, &addrbuf, addrlen) != 0
887                 ||  (p = _hgetword(&line)) == NULL)
888                         continue;
889                 hp = &hpbuf;
890                 hp->h_name = p;
891                 hp->h_aliases = aliases;
892                 nalias = 0;
893                 while ((p = _hgetword(&line)) != NULL) {
894                         if (nalias < MAXALIASES)
895                                 aliases[nalias++] = p;
896                 }
897                 aliases[nalias] = NULL;
898                 hp->h_addrtype = af;
899                 hp->h_length = addrlen;
900                 hp->h_addr_list = addrs;
901                 addrs[0] = (char *)&addrbuf;
902                 addrs[1] = NULL;
903                 hp = _hpcopy(hp, errp);
904                 break;
905         }
906         fclose(fp);
907         *(struct hostent **)rval = hp;
908         return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
909 }
910
911 #ifdef YP
912 /*
913  * NIS
914  *
915  * XXX actually a hack, these are INET4 specific.
916  */
917 static int
918 _nis_ghbyname(void *rval, void *cb_data, va_list ap)
919 {
920         const char *name;
921         int af;
922         int *errp;
923         struct hostent *hp = NULL;
924
925         name = va_arg(ap, const char *);
926         af = va_arg(ap, int);
927         errp = va_arg(ap, int *);
928         
929         if (af == AF_UNSPEC)
930                 af = AF_INET;
931         if (af == AF_INET) {
932                 hp = _gethostbynisname(name, af);
933                 if (hp != NULL)
934                         hp = _hpcopy(hp, errp);
935         }
936         
937         *(struct hostent **)rval = hp;
938         return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
939         
940 }
941
942 static int
943 _nis_ghbyaddr(void *rval, void *cb_data, va_list ap)
944 {
945         const void *addr;
946         int addrlen;
947         int af;
948         int *errp;
949         struct hostent *hp = NULL;
950
951         addr = va_arg(ap, const void *);
952         addrlen = va_arg(ap, int);
953         af = va_arg(ap, int);
954
955         if (af == AF_INET) {
956                 hp = _gethostbynisaddr(addr, addrlen, af);
957                 if (hp != NULL)
958                         hp = _hpcopy(hp, errp);
959         }
960         *(struct hostent **)rval = hp;
961         return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
962 }
963 #endif
964
965 struct __res_type_list {
966         SLIST_ENTRY(__res_type_list) rtl_entry;
967         int     rtl_type;
968 };
969
970 #if PACKETSZ > 1024
971 #define MAXPACKET       PACKETSZ
972 #else
973 #define MAXPACKET       1024
974 #endif
975
976 typedef union {
977         HEADER hdr;
978         u_char buf[MAXPACKET];
979 } querybuf;
980
981 static struct hostent *getanswer(const querybuf *, int, const char *, int,
982             struct hostent *, int *);
983
984 /*
985  * we don't need to take care about sorting, nor IPv4 mapped address here.
986  */
987 static struct hostent *
988 getanswer(answer, anslen, qname, qtype, template, errp)
989         const querybuf *answer;
990         int anslen;
991         const char *qname;
992         int qtype;
993         struct hostent *template;
994         int *errp;
995 {
996         const HEADER *hp;
997         const u_char *cp;
998         int n;
999         const u_char *eom, *erdata;
1000         char *bp, *ep, **ap, **hap;
1001         int type, class, ancount, qdcount;
1002         int haveanswer, had_error;
1003         char tbuf[MAXDNAME];
1004         const char *tname;
1005         int (*name_ok)(const char *);
1006         static char *h_addr_ptrs[MAXADDRS + 1];
1007         static char *host_aliases[MAXALIASES];
1008         static char hostbuf[8*1024];
1009
1010 #define BOUNDED_INCR(x) \
1011         do { \
1012                 cp += x; \
1013                 if (cp > eom) { \
1014                         *errp = NO_RECOVERY; \
1015                         return (NULL); \
1016                 } \
1017         } while (0)
1018
1019 #define BOUNDS_CHECK(ptr, count) \
1020         do { \
1021                 if ((ptr) + (count) > eom) { \
1022                         *errp = NO_RECOVERY; \
1023                         return (NULL); \
1024                 } \
1025         } while (0)
1026
1027 /* XXX do {} while (0) cannot be put here */
1028 #define DNS_ASSERT(x) \
1029         {                               \
1030                 if (!(x)) {             \
1031                         cp += n;        \
1032                         continue;       \
1033                 }                       \
1034         }
1035
1036 /* XXX do {} while (0) cannot be put here */
1037 #define DNS_FATAL(x) \
1038         {                               \
1039                 if (!(x)) {             \
1040                         had_error++;    \
1041                         continue;       \
1042                 }                       \
1043         }
1044
1045         tname = qname;
1046         template->h_name = NULL;
1047         eom = answer->buf + anslen;
1048         switch (qtype) {
1049         case T_A:
1050         case T_AAAA:
1051                 name_ok = res_hnok;
1052                 break;
1053         case T_PTR:
1054                 name_ok = res_dnok;
1055                 break;
1056         default:
1057                 return (NULL);  /* XXX should be abort(); */
1058         }
1059         /*
1060          * find first satisfactory answer
1061          */
1062         hp = &answer->hdr;
1063         ancount = ntohs(hp->ancount);
1064         qdcount = ntohs(hp->qdcount);
1065         bp = hostbuf;
1066         ep = hostbuf + sizeof hostbuf;
1067         cp = answer->buf;
1068         BOUNDED_INCR(HFIXEDSZ);
1069         if (qdcount != 1) {
1070                 *errp = NO_RECOVERY;
1071                 return (NULL);
1072         }
1073         n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1074         if ((n < 0) || !(*name_ok)(bp)) {
1075                 *errp = NO_RECOVERY;
1076                 return (NULL);
1077         }
1078         BOUNDED_INCR(n + QFIXEDSZ);
1079         if (qtype == T_A || qtype == T_AAAA) {
1080                 /* res_send() has already verified that the query name is the
1081                  * same as the one we sent; this just gets the expanded name
1082                  * (i.e., with the succeeding search-domain tacked on).
1083                  */
1084                 n = strlen(bp) + 1;             /* for the \0 */
1085                 if (n >= MAXHOSTNAMELEN) {
1086                         *errp = NO_RECOVERY;
1087                         return (NULL);
1088                 }
1089                 template->h_name = bp;
1090                 bp += n;
1091                 /* The qname can be abbreviated, but h_name is now absolute. */
1092                 qname = template->h_name;
1093         }
1094         ap = host_aliases;
1095         *ap = NULL;
1096         template->h_aliases = host_aliases;
1097         hap = h_addr_ptrs;
1098         *hap = NULL;
1099         template->h_addr_list = h_addr_ptrs;
1100         haveanswer = 0;
1101         had_error = 0;
1102         while (ancount-- > 0 && cp < eom && !had_error) {
1103                 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1104                 DNS_FATAL(n >= 0);
1105                 DNS_FATAL((*name_ok)(bp));
1106                 cp += n;                        /* name */
1107                 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
1108                 type = _getshort(cp);
1109                 cp += INT16SZ;                  /* type */
1110                 class = _getshort(cp);
1111                 cp += INT16SZ + INT32SZ;        /* class, TTL */
1112                 n = _getshort(cp);
1113                 cp += INT16SZ;                  /* len */
1114                 BOUNDS_CHECK(cp, n);
1115                 erdata = cp + n;
1116                 DNS_ASSERT(class == C_IN);
1117                 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
1118                         if (ap >= &host_aliases[MAXALIASES-1])
1119                                 continue;
1120                         n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1121                         DNS_FATAL(n >= 0);
1122                         DNS_FATAL((*name_ok)(tbuf));
1123                         cp += n;
1124                         if (cp != erdata) {
1125                                 *errp = NO_RECOVERY;
1126                                 return (NULL);
1127                         }
1128                         /* Store alias. */
1129                         *ap++ = bp;
1130                         n = strlen(bp) + 1;     /* for the \0 */
1131                         DNS_FATAL(n < MAXHOSTNAMELEN);
1132                         bp += n;
1133                         /* Get canonical name. */
1134                         n = strlen(tbuf) + 1;   /* for the \0 */
1135                         DNS_FATAL(n <= ep - bp);
1136                         DNS_FATAL(n < MAXHOSTNAMELEN);
1137                         strcpy(bp, tbuf);
1138                         template->h_name = bp;
1139                         bp += n;
1140                         continue;
1141                 }
1142                 if (qtype == T_PTR && type == T_CNAME) {
1143                         n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1144                         if (n < 0 || !res_dnok(tbuf)) {
1145                                 had_error++;
1146                                 continue;
1147                         }
1148                         cp += n;
1149                         if (cp != erdata) {
1150                                 *errp = NO_RECOVERY;
1151                                 return (NULL);
1152                         }
1153                         /* Get canonical name. */
1154                         n = strlen(tbuf) + 1;   /* for the \0 */
1155                         if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1156                                 had_error++;
1157                                 continue;
1158                         }
1159                         strcpy(bp, tbuf);
1160                         tname = bp;
1161                         bp += n;
1162                         continue;
1163                 }
1164                 DNS_ASSERT(type == qtype);
1165                 switch (type) {
1166                 case T_PTR:
1167                         DNS_ASSERT(strcasecmp(tname, bp) == 0);
1168                         n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1169                         DNS_FATAL(n >= 0);
1170                         DNS_FATAL(res_hnok(bp));
1171 #if MULTI_PTRS_ARE_ALIASES
1172                         cp += n;
1173                         if (cp != erdata) {
1174                                 *errp = NO_RECOVERY;
1175                                 return (NULL);
1176                         }
1177                         if (!haveanswer)
1178                                 template->h_name = bp;
1179                         else if (ap < &host_aliases[MAXALIASES-1])
1180                                 *ap++ = bp;
1181                         else
1182                                 n = -1;
1183                         if (n != -1) {
1184                                 n = strlen(bp) + 1;     /* for the \0 */
1185                                 if (n >= MAXHOSTNAMELEN) {
1186                                         had_error++;
1187                                         break;
1188                                 }
1189                                 bp += n;
1190                         }
1191                         break;
1192 #else
1193                         template->h_name = bp;
1194                         *errp = NETDB_SUCCESS;
1195                         return (template);
1196 #endif
1197                 case T_A:
1198                 case T_AAAA:
1199                         DNS_ASSERT(strcasecmp(template->h_name, bp) == 0);
1200                         DNS_ASSERT(n == template->h_length);
1201                         if (!haveanswer) {
1202                                 int nn;
1203
1204                                 template->h_name = bp;
1205                                 nn = strlen(bp) + 1;    /* for the \0 */
1206                                 bp += nn;
1207                         }
1208                         obp = bp; /* ALIGN rounds up */
1209                         bp = (char *)ALIGN(bp);
1210                         buflen -= (bp - obp);
1211
1212                         DNS_FATAL(bp + n < ep);
1213                         DNS_ASSERT(hap < &h_addr_ptrs[MAXADDRS-1]);
1214 #ifdef FILTER_V4MAPPED
1215                         if (type == T_AAAA) {
1216                                 struct in6_addr in6;
1217                                 memcpy(&in6, cp, sizeof(in6));
1218                                 DNS_ASSERT(IN6_IS_ADDR_V4MAPPED(&in6) == 0);
1219                         }
1220 #endif
1221                         bcopy(cp, *hap++ = bp, n);
1222                         bp += n;
1223                         cp += n;
1224                         if (cp != erdata) {
1225                                 *errp = NO_RECOVERY;
1226                                 return (NULL);
1227                         }
1228                         break;
1229                 default:
1230                         abort();
1231                 }
1232                 if (!had_error)
1233                         haveanswer++;
1234         }
1235         if (haveanswer) {
1236                 *ap = NULL;
1237                 *hap = NULL;
1238                 if (!template->h_name) {
1239                         n = strlen(qname) + 1;  /* for the \0 */
1240                         if (n > ep - bp || n >= MAXHOSTNAMELEN)
1241                                 goto no_recovery;
1242                         strcpy(bp, qname);
1243                         template->h_name = bp;
1244                         bp += n;
1245                 }
1246                 *errp = NETDB_SUCCESS;
1247                 return (template);
1248         }
1249  no_recovery:
1250         *errp = NO_RECOVERY;
1251         return (NULL);
1252
1253 #undef BOUNDED_INCR
1254 #undef BOUNDS_CHECK
1255 #undef DNS_ASSERT
1256 #undef DNS_FATAL
1257 }
1258
1259 /* res_search() variant with multiple query support. */
1260 static struct hostent *
1261 _res_search_multi(name, rtl, errp)
1262         const char *name;       /* domain name */
1263         struct  __res_type_list *rtl; /* list of query types */
1264         int *errp;
1265 {
1266         const char *cp, * const *domain;
1267         struct hostent *hp0 = NULL, *hp;
1268         struct hostent hpbuf;
1269         u_int dots;
1270         int trailing_dot, ret, saved_herrno;
1271         int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
1272         struct __res_type_list *rtl0 = rtl;
1273         querybuf buf;
1274
1275         if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
1276                 *errp = NETDB_INTERNAL;
1277                 return (NULL);
1278         }
1279         dots = 0;
1280         for (cp = name; *cp; cp++)
1281                 dots += (*cp == '.');
1282         trailing_dot = 0;
1283         if (cp > name && *--cp == '.')
1284                 trailing_dot++;
1285
1286         /* If there aren't any dots, it could be a user-level alias */
1287         if (!dots && (cp = hostalias(name)) != NULL) {
1288                 for(rtl = rtl0; rtl != NULL;
1289                     rtl = SLIST_NEXT(rtl, rtl_entry)) {
1290                         ret = res_query(cp, C_IN, rtl->rtl_type, buf.buf,
1291                                              sizeof(buf.buf));
1292                         if (ret > 0) {
1293                                 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1294                                     ? AF_INET6 : AF_INET;
1295                                 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1296                                 hp = getanswer(&buf, ret, name, rtl->rtl_type,
1297                                                     &hpbuf, errp);
1298                                 if (!hp)
1299                                         continue;
1300                                 hp = _hpcopy(&hpbuf, errp);
1301                                 hp0 = _hpmerge(hp0, hp, errp);
1302                         }
1303                 }
1304                 return (hp0);
1305         }
1306
1307         /*
1308          * If there are dots in the name already, let's just give it a try
1309          * 'as is'.  The threshold can be set with the "ndots" option.
1310          */
1311         saved_herrno = -1;
1312         if (dots >= _res.ndots) {
1313                 for(rtl = rtl0; rtl != NULL;
1314                     rtl = SLIST_NEXT(rtl, rtl_entry)) {
1315                         ret = res_querydomain(name, NULL, C_IN, rtl->rtl_type,
1316                                               buf.buf, sizeof(buf.buf));
1317                         if (ret > 0) {
1318                                 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1319                                     ? AF_INET6 : AF_INET;
1320                                 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1321                                 hp = getanswer(&buf, ret, name, rtl->rtl_type,
1322                                                     &hpbuf, errp);
1323                                 if (!hp)
1324                                         continue;
1325                                 hp = _hpcopy(&hpbuf, errp);
1326                                 hp0 = _hpmerge(hp0, hp, errp);
1327                         }
1328                 }
1329                 if (hp0 != NULL)
1330                         return (hp0);
1331                 saved_herrno = *errp;
1332                 tried_as_is++;
1333         }
1334
1335         /*
1336          * We do at least one level of search if
1337          *      - there is no dot and RES_DEFNAME is set, or
1338          *      - there is at least one dot, there is no trailing dot,
1339          *        and RES_DNSRCH is set.
1340          */
1341         if ((!dots && (_res.options & RES_DEFNAMES)) ||
1342             (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
1343                 int done = 0;
1344
1345                 for (domain = (const char * const *)_res.dnsrch;
1346                      *domain && !done;
1347                      domain++) {
1348
1349                         for(rtl = rtl0; rtl != NULL;
1350                             rtl = SLIST_NEXT(rtl, rtl_entry)) {
1351                                 ret = res_querydomain(name, *domain, C_IN,
1352                                                       rtl->rtl_type,
1353                                                       buf.buf, sizeof(buf.buf));
1354                                 if (ret > 0) {
1355                                         hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1356                                             ? AF_INET6 : AF_INET;
1357                                         hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1358                                         hp = getanswer(&buf, ret, name,
1359                                             rtl->rtl_type, &hpbuf, errp);
1360                                         if (!hp)
1361                                                 continue;
1362                                         hp = _hpcopy(&hpbuf, errp);
1363                                         hp0 = _hpmerge(hp0, hp, errp);
1364                                 }
1365                         }
1366                         if (hp0 != NULL)
1367                                 return (hp0);
1368
1369                         /*
1370                          * If no server present, give up.
1371                          * If name isn't found in this domain,
1372                          * keep trying higher domains in the search list
1373                          * (if that's enabled).
1374                          * On a NO_DATA error, keep trying, otherwise
1375                          * a wildcard entry of another type could keep us
1376                          * from finding this entry higher in the domain.
1377                          * If we get some other error (negative answer or
1378                          * server failure), then stop searching up,
1379                          * but try the input name below in case it's
1380                          * fully-qualified.
1381                          */
1382                         if (errno == ECONNREFUSED) {
1383                                 *errp = TRY_AGAIN;
1384                                 return (NULL);
1385                         }
1386
1387                         switch (*errp) {
1388                         case NO_DATA:
1389                                 got_nodata++;
1390                                 /* FALLTHROUGH */
1391                         case HOST_NOT_FOUND:
1392                                 /* keep trying */
1393                                 break;
1394                         case TRY_AGAIN:
1395                                 if (buf.hdr.rcode == SERVFAIL) {
1396                                         /* try next search element, if any */
1397                                         got_servfail++;
1398                                         break;
1399                                 }
1400                                 /* FALLTHROUGH */
1401                         default:
1402                                 /* anything else implies that we're done */
1403                                 done++;
1404                         }
1405
1406                         /* if we got here for some reason other than DNSRCH,
1407                          * we only wanted one iteration of the loop, so stop.
1408                          */
1409                         if (!(_res.options & RES_DNSRCH))
1410                                 done++;
1411                 }
1412         }
1413
1414         /*
1415          * If we have not already tried the name "as is", do that now.
1416          * note that we do this regardless of how many dots were in the
1417          * name or whether it ends with a dot unless NOTLDQUERY is set.
1418          */
1419         if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) {
1420                 for(rtl = rtl0; rtl != NULL;
1421                     rtl = SLIST_NEXT(rtl, rtl_entry)) {
1422                         ret = res_querydomain(name, NULL, C_IN, rtl->rtl_type,
1423                                               buf.buf, sizeof(buf.buf));
1424                         if (ret > 0) {
1425                                 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1426                                     ? AF_INET6 : AF_INET;
1427                                 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1428                                 hp = getanswer(&buf, ret, name, rtl->rtl_type,
1429                                     &hpbuf, errp);
1430                                 if (!hp)
1431                                         continue;
1432                                 hp = _hpcopy(&hpbuf, errp);
1433                                 hp0 = _hpmerge(hp0, hp, errp);
1434                         }
1435                 }
1436                 if (hp0 != NULL)
1437                         return (hp0);
1438         }
1439
1440         /* if we got here, we didn't satisfy the search.
1441          * if we did an initial full query, return that query's h_errno
1442          * (note that we wouldn't be here if that query had succeeded).
1443          * else if we ever got a nodata, send that back as the reason.
1444          * else send back meaningless h_errno, that being the one from
1445          * the last DNSRCH we did.
1446          */
1447         if (saved_herrno != -1)
1448                 *errp = saved_herrno;
1449         else if (got_nodata)
1450                 *errp = NO_DATA;
1451         else if (got_servfail)
1452                 *errp = TRY_AGAIN;
1453         return (NULL);
1454 }
1455
1456 static int
1457 _dns_ghbyname(void *rval, void *cb_data, va_list ap)
1458 {
1459         const char *name;
1460         int af;
1461         int *errp;
1462         struct __res_type_list *rtl, rtl4;
1463 #ifdef INET6
1464         struct __res_type_list rtl6;
1465 #endif
1466
1467         name = va_arg(ap, const char *);
1468         af = va_arg(ap, int);
1469         errp = va_arg(ap, int *);
1470
1471 #ifdef INET6
1472         switch (af) {
1473         case AF_UNSPEC:
1474                 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1475                 SLIST_NEXT(&rtl6, rtl_entry) = &rtl4; rtl6.rtl_type = T_AAAA;
1476                 rtl = &rtl6;
1477                 break;
1478         case AF_INET6:
1479                 SLIST_NEXT(&rtl6, rtl_entry) = NULL; rtl6.rtl_type = T_AAAA;
1480                 rtl = &rtl6;
1481                 break;
1482         case AF_INET:
1483                 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1484                 rtl = &rtl4;
1485                 break;
1486         }
1487 #else
1488         SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1489         rtl = &rtl4;
1490 #endif
1491         *(struct hostent **)rval = _res_search_multi(name, rtl, errp);
1492         return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
1493 }
1494
1495 static int
1496 _dns_ghbyaddr(void *rval, void *cb_data, va_list ap)
1497 {
1498         const void *addr;
1499         int addrlen;
1500         int af;
1501         int *errp;
1502         int n;
1503         struct hostent *hp;
1504         u_char c, *cp;
1505         char *bp;
1506         struct hostent hbuf;
1507         int na;
1508 #ifdef INET6
1509         static const char hex[] = "0123456789abcdef";
1510 #endif
1511         querybuf buf;
1512         char qbuf[MAXDNAME+1];
1513         char *hlist[2];
1514
1515         addr = va_arg(ap, const void *);
1516         addrlen = va_arg(ap, int);
1517         af = va_arg(ap, int);
1518         errp = va_arg(ap, int *);
1519
1520         *(struct hostent **)rval = NULL;
1521
1522 #ifdef INET6
1523         /* XXX */
1524         if (af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr))
1525                 return NS_NOTFOUND;
1526 #endif
1527
1528         if ((_res.options & RES_INIT) == 0) {
1529                 if (res_init() < 0) {
1530                         *errp = h_errno;
1531                         return NS_UNAVAIL;
1532                 }
1533         }
1534         memset(&hbuf, 0, sizeof(hbuf));
1535         hbuf.h_name = NULL;
1536         hbuf.h_addrtype = af;
1537         hbuf.h_length = addrlen;
1538         na = 0;
1539
1540         /* XXX assumes that MAXDNAME is big enough */
1541         n = 0;
1542         bp = qbuf;
1543         cp = (u_char *)addr+addrlen-1;
1544         switch (af) {
1545 #ifdef INET6
1546         case AF_INET6:
1547                 for (; n < addrlen; n++, cp--) {
1548                         c = *cp;
1549                         *bp++ = hex[c & 0xf];
1550                         *bp++ = '.';
1551                         *bp++ = hex[c >> 4];
1552                         *bp++ = '.';
1553                 }
1554                 strcpy(bp, "ip6.int");
1555                 break;
1556 #endif
1557         default:
1558                 for (; n < addrlen; n++, cp--) {
1559                         c = *cp;
1560                         if (c >= 100)
1561                                 *bp++ = '0' + c / 100;
1562                         if (c >= 10)
1563                                 *bp++ = '0' + (c % 100) / 10;
1564                         *bp++ = '0' + c % 10;
1565                         *bp++ = '.';
1566                 }
1567                 strcpy(bp, "in-addr.arpa");
1568                 break;
1569         }
1570
1571         n = res_query(qbuf, C_IN, T_PTR, buf.buf, sizeof buf.buf);
1572         if (n < 0) {
1573                 *errp = h_errno;
1574                 return NS_UNAVAIL;
1575         }
1576         hp = getanswer(&buf, n, qbuf, T_PTR, &hbuf, errp);
1577         if (!hp)
1578                 return NS_NOTFOUND;
1579         hbuf.h_addrtype = af;
1580         hbuf.h_length = addrlen;
1581         hbuf.h_addr_list = hlist;
1582         hlist[0] = (char *)addr;
1583         hlist[1] = NULL;
1584         *(struct hostent **)rval = _hpcopy(&hbuf, errp);
1585         return NS_SUCCESS;
1586 }
1587
1588 static void
1589 _dns_shent(int stayopen)
1590 {
1591         if ((_res.options & RES_INIT) == 0) {
1592                 if (res_init() < 0)
1593                         return;
1594         }
1595         if (stayopen)
1596                 _res.options |= RES_STAYOPEN | RES_USEVC;
1597 }
1598
1599 static void
1600 _dns_ehent(void)
1601 {
1602         _res.options &= ~(RES_STAYOPEN | RES_USEVC);
1603         res_close();
1604 }
1605
1606 #ifdef ICMPNL
1607
1608 /*
1609  * experimental:
1610  *      draft-ietf-ipngwg-icmp-namelookups-02.txt
1611  *      ifindex is assumed to be encoded in addr.
1612  */
1613 #include <sys/uio.h>
1614 #include <netinet/ip6.h>
1615 #include <netinet/icmp6.h>
1616
1617 struct _icmp_host_cache {
1618         struct _icmp_host_cache *hc_next;
1619         int hc_ifindex;
1620         struct in6_addr hc_addr;
1621         char *hc_name;
1622 };
1623
1624 static char *
1625 _icmp_fqdn_query(const struct in6_addr *addr, int ifindex)
1626 {
1627         int s;
1628         struct icmp6_filter filter;
1629         struct msghdr msg;
1630         struct cmsghdr *cmsg;
1631         struct in6_pktinfo *pkt;
1632         char cbuf[256];
1633         char buf[1024];
1634         int cc;
1635         struct icmp6_fqdn_query *fq;
1636         struct icmp6_fqdn_reply *fr;
1637         struct _icmp_host_cache *hc;
1638         struct sockaddr_in6 sin6;
1639         struct iovec iov;
1640         fd_set s_fds, fds;
1641         struct timeval tout;
1642         int len;
1643         char *name;
1644         static int pid;
1645         static struct _icmp_host_cache *hc_head;
1646
1647         for (hc = hc_head; hc; hc = hc->hc_next) {
1648                 if (hc->hc_ifindex == ifindex
1649                 &&  IN6_ARE_ADDR_EQUAL(&hc->hc_addr, addr))
1650                         return hc->hc_name;
1651         }
1652
1653         if (pid == 0)
1654                 pid = getpid();
1655
1656         ICMP6_FILTER_SETBLOCKALL(&filter);
1657         ICMP6_FILTER_SETPASS(ICMP6_FQDN_REPLY, &filter);
1658
1659         FD_ZERO(&s_fds);
1660         tout.tv_sec = 0;
1661         tout.tv_usec = 200000;  /*XXX: 200ms*/
1662
1663         fq = (struct icmp6_fqdn_query *)buf;
1664         fq->icmp6_fqdn_type = ICMP6_FQDN_QUERY;
1665         fq->icmp6_fqdn_code = 0;
1666         fq->icmp6_fqdn_cksum = 0;
1667         fq->icmp6_fqdn_id = (u_short)pid;
1668         fq->icmp6_fqdn_unused = 0;
1669         fq->icmp6_fqdn_cookie[0] = 0;
1670         fq->icmp6_fqdn_cookie[1] = 0;
1671
1672         memset(&sin6, 0, sizeof(sin6));
1673         sin6.sin6_family = AF_INET6;
1674         sin6.sin6_addr = *addr;
1675
1676         memset(&msg, 0, sizeof(msg));
1677         msg.msg_name = (caddr_t)&sin6;
1678         msg.msg_namelen = sizeof(sin6);
1679         msg.msg_iov = &iov;
1680         msg.msg_iovlen = 1;
1681         msg.msg_control = NULL;
1682         msg.msg_controllen = 0;
1683         iov.iov_base = (caddr_t)buf;
1684         iov.iov_len = sizeof(struct icmp6_fqdn_query);
1685
1686         if (ifindex) {
1687                 msg.msg_control = cbuf;
1688                 msg.msg_controllen = sizeof(cbuf);
1689                 cmsg = CMSG_FIRSTHDR(&msg);
1690                 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1691                 cmsg->cmsg_level = IPPROTO_IPV6;
1692                 cmsg->cmsg_type = IPV6_PKTINFO;
1693                 pkt = (struct in6_pktinfo *)&cmsg[1];
1694                 memset(&pkt->ipi6_addr, 0, sizeof(struct in6_addr));
1695                 pkt->ipi6_ifindex = ifindex;
1696                 cmsg = CMSG_NXTHDR(&msg, cmsg);
1697                 msg.msg_controllen = (char *)cmsg - cbuf;
1698         }
1699
1700         if ((s = _socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
1701                 return NULL;
1702         (void)_setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER,
1703                          (char *)&filter, sizeof(filter));
1704         cc = _sendmsg(s, &msg, 0);
1705         if (cc < 0) {
1706                 _close(s);
1707                 return NULL;
1708         }
1709         FD_SET(s, &s_fds);
1710         for (;;) {
1711                 fds = s_fds;
1712                 if (_select(s + 1, &fds, NULL, NULL, &tout) <= 0) {
1713                         _close(s);
1714                         return NULL;
1715                 }
1716                 len = sizeof(sin6);
1717                 cc = _recvfrom(s, buf, sizeof(buf), 0,
1718                               (struct sockaddr *)&sin6, &len);
1719                 if (cc <= 0) {
1720                         _close(s);
1721                         return NULL;
1722                 }
1723                 if (cc < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr))
1724                         continue;
1725                 if (!IN6_ARE_ADDR_EQUAL(addr, &sin6.sin6_addr))
1726                         continue;
1727                 fr = (struct icmp6_fqdn_reply *)(buf + sizeof(struct ip6_hdr));
1728                 if (fr->icmp6_fqdn_type == ICMP6_FQDN_REPLY)
1729                         break;
1730         }
1731         _close(s);
1732         if (fr->icmp6_fqdn_cookie[1] != 0) {
1733                 /* rfc1788 type */
1734                 name = buf + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 4;
1735                 len = (buf + cc) - name;
1736         } else {
1737                 len = fr->icmp6_fqdn_namelen;
1738                 name = fr->icmp6_fqdn_name;
1739         }
1740         if (len <= 0)
1741                 return NULL;
1742         name[len] = 0;
1743
1744         if ((hc = (struct _icmp_host_cache *)malloc(sizeof(*hc))) == NULL)
1745                 return NULL;
1746         /* XXX: limit number of cached entries */
1747         hc->hc_ifindex = ifindex;
1748         hc->hc_addr = *addr;
1749         hc->hc_name = strdup(name);
1750         hc->hc_next = hc_head;
1751         hc_head = hc;
1752         return hc->hc_name;
1753 }
1754
1755 static struct hostent *
1756 _icmp_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
1757 {
1758         char *hname;
1759         int ifindex;
1760         struct in6_addr addr6;
1761
1762         if (af != AF_INET6) {
1763                 /*
1764                  * Note: rfc1788 defines Who Are You for IPv4,
1765                  * but no one implements it.
1766                  */
1767                 return NULL;
1768         }
1769
1770         memcpy(&addr6, addr, addrlen);
1771         ifindex = (addr6.s6_addr[2] << 8) | addr6.s6_addr[3];
1772         addr6.s6_addr[2] = addr6.s6_addr[3] = 0;
1773
1774         if (!IN6_IS_ADDR_LINKLOCAL(&addr6))
1775                 return NULL;    /*XXX*/
1776
1777         if ((hname = _icmp_fqdn_query(&addr6, ifindex)) == NULL)
1778                 return NULL;
1779         return _hpaddr(af, hname, &addr6, errp);
1780 }
1781 #endif /* ICMPNL */