1 /* $KAME: name6.c,v 1.25 2000/06/26 16:44:40 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 * ++Copyright++ 1985, 1988, 1993
34 * Copyright (c) 1985, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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.
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
65 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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
87 * Atsushi Onoe <onoe@sm.sony.co.jp>
91 * TODO for thread safe
92 * use mutex for _hostconf, _hostconf_init.
93 * rewrite resolvers to be thread safe
96 #include <sys/cdefs.h>
97 __FBSDID("$FreeBSD$");
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>
106 #include <arpa/inet.h>
107 #include <arpa/nameser.h>
116 #include <nsswitch.h>
118 #include "un-namespace.h"
121 #define _PATH_HOSTS "/etc/hosts"
125 #define MAXALIASES 10
131 #define MAXDNAME 1025
135 #define ADDRLEN(af) ((af) == AF_INET6 ? sizeof(struct in6_addr) : \
136 sizeof(struct in_addr))
138 #define ADDRLEN(af) sizeof(struct in_addr)
141 #define MAPADDR(ab, ina) \
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)); \
147 #define MAPADDRENABLED(flags) \
148 (((flags) & AI_V4MAPPED) || \
149 (((flags) & AI_V4MAPPED_CFG) && _mapped_addr_enabled()))
152 struct in_addr in_addr;
154 struct in6_addr in6_addr;
159 struct in_addr mau_inaddr;
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
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);
170 static struct hostent *_hpmapv6(struct hostent *hp, int *errp);
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);
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);
183 static int _nis_ghbyname(void *, void *, va_list);
184 static int _nis_ghbyaddr(void *, void *, va_list);
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);
191 static int _icmp_ghbyaddr(void *, void *, va_list);
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 },
199 #define NSSRC_ICMP "icmp"
200 { NSSRC_ICMP, NS_SUCCESS },
206 * Check if kernel supports mapped address.
207 * implementation dependent
210 #include <sys/sysctl.h>
211 #endif /* __KAME__ */
214 _mapped_addr_enabled(void)
216 /* implementation dependent check */
217 #if defined(__KAME__) && defined(IPV6CTL_MAPPED_ADDR)
224 mib[2] = IPPROTO_IPV6;
225 mib[3] = IPV6CTL_MAPPED_ADDR;
227 if (sysctl(mib, 4, &val, &len, 0, 0) == 0 && val != 0)
229 #endif /* __KAME__ && IPV6CTL_MAPPED_ADDR */
234 * Functions defined in RFC2553
235 * getipnodebyname, getipnodebyaddr, freehostent
238 static struct hostent *
239 _ghbyname(const char *name, int af, int flags, int *errp)
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)
251 if (flags & AI_ADDRCONFIG) {
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.
261 if (af == AF_UNSPEC) {
262 if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
266 if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0)
273 if (af != AF_UNSPEC) {
274 if ((s = _socket(af, SOCK_DGRAM, 0)) < 0)
280 rval = nsdispatch(&hp, dtab, NSDB_HOSTS, "ghbyname", default_src,
282 return (rval == NS_SUCCESS) ? hp : NULL;
285 /* getipnodebyname() internal routine for multiple query(PF_UNSPEC) support. */
287 _getipnodebyname_multi(const char *name, int af, int flags, int *errp)
290 union inx_addr addrbuf;
292 /* XXX: PF_UNSPEC is only supposed to be passed from getaddrinfo() */
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;
311 return _hpaddr(af, name, &addrbuf, errp);
314 if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) {
316 if (MAPADDRENABLED(flags)) {
317 MAPADDR(&addrbuf, &addrbuf.in_addr);
319 *errp = HOST_NOT_FOUND;
323 return _hpaddr(af, name, &addrbuf, errp);
326 *errp = HOST_NOT_FOUND;
327 hp = _ghbyname(name, af, flags, errp);
331 && ((flags & AI_ALL) || hp == NULL)
332 && (MAPADDRENABLED(flags))) {
333 struct hostent *hp2 = _ghbyname(name, AF_INET, flags, errp);
335 hp = _hpmapv6(hp2, errp);
337 if (hp2 && strcmp(hp->h_name, hp2->h_name) != 0) {
341 hp = _hpmerge(hp, hp2, errp);
349 getipnodebyname(const char *name, int af, int flags, int *errp)
360 return(_getipnodebyname_multi(name, af ,flags, errp));
364 getipnodebyaddr(const void *src, size_t len, int af, int *errp)
369 struct in6_addr addrbuf;
371 struct in_addr addrbuf;
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)
379 { NSSRC_ICMP, _icmp_ghbyaddr, NULL },
384 *errp = HOST_NOT_FOUND;
388 if (len != sizeof(struct in_addr)) {
392 if ((long)src & ~(sizeof(struct in_addr) - 1)) {
393 memcpy(&addrbuf, src, len);
396 if (((struct in_addr *)src)->s_addr == 0)
401 if (len != sizeof(struct in6_addr)) {
405 if ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) { /*XXX*/
406 memcpy(&addrbuf, src, len);
409 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src))
411 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)
412 || IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) {
414 (sizeof(struct in6_addr) - sizeof(struct in_addr));
416 len = sizeof(struct in_addr);
425 rval = nsdispatch(&hp, dtab, NSDB_HOSTS, "ghbyaddr", default_src,
427 return (rval == NS_SUCCESS) ? hp : NULL;
431 freehostent(struct hostent *ptr)
438 /* XXX: should be deprecated */
440 getnodebyname(const char *name, int af, int flags)
442 return getipnodebyname(name, af, flags, &h_errno);
445 #ifdef __warn_references
446 __warn_references(getnodebyname,
447 "warning: getnodebyname() deprecated, "
448 "should use getaddrinfo() or getipnodebyname()");
452 getnodebyaddr(const void *src, size_t len, int af)
454 return getipnodebyaddr(src, len, af, &h_errno);
457 #ifdef __warn_references
458 __warn_references(getnodebyaddr,
459 "warning: getnodebyaddr() deprecated, "
460 "should use getnameinfo() or getipnodebyaddr()");
466 * Private utility functions
470 * _hpcopy: allocate and copy hostent structure
472 static struct hostent *
473 _hpcopy(struct hostent *hp, int *errp)
478 int nalias = 0, naddr = 0;
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++) {
492 size += strlen(*pp) + 1;
497 /* adjust alignment */
500 size += sizeof(char *) * (nalias + 1);
501 addrsize = ALIGN(hp->h_length);
502 if ((pp = hp->h_addr_list) != NULL) {
503 while (*pp++ != NULL)
506 size += addrsize * naddr;
507 size += sizeof(char *) * (naddr + 1);
510 if ((nhp = (struct hostent *)malloc(size)) == NULL) {
514 cp = (char *)&nhp[1];
515 if (hp->h_name != NULL) {
517 strcpy(cp, hp->h_name);
518 cp += strlen(cp) + 1;
521 nhp->h_aliases = (char **)((char *)nhp + al_off);
522 if ((pp = hp->h_aliases) != NULL) {
523 for (i = 0; *pp != NULL; pp++) {
525 nhp->h_aliases[i++] = cp;
527 cp += strlen(cp) + 1;
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);
544 nhp->h_addr_list[naddr] = NULL;
549 * _hpaddr: construct hostent structure with one address
551 static struct hostent *
552 _hpaddr(int af, const char *name, void *addr, int *errp)
554 struct hostent *hp, hpbuf;
558 hp->h_name = (char *)name;
559 hp->h_aliases = NULL;
561 hp->h_length = ADDRLEN(af);
562 hp->h_addr_list = addrs;
563 addrs[0] = (char *)addr;
565 return _hpcopy(hp, errp);
569 * _hpmerge: merge 2 hostent structure, arguments will be freed
571 static struct hostent *
572 _hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp)
577 struct hostent *hp, hpbuf;
578 char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1];
579 union inx_addr addrbuf[MAXADDRS];
586 #define HP(i) (i == 1 ? hp1 : hp2)
588 hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name);
589 hp->h_aliases = aliases;
591 for (i = 1; i <= 2; i++) {
592 if ((pp = HP(i)->h_aliases) == NULL)
594 for (; nalias < MAXALIASES && *pp != NULL; pp++) {
595 /* check duplicates */
596 for (j = 0; j < nalias; j++)
597 if (strcasecmp(*pp, aliases[j]) == 0)
600 aliases[nalias++] = *pp;
603 aliases[nalias] = NULL;
605 if (hp1->h_length != hp2->h_length) {
606 hp->h_addrtype = AF_INET6;
607 hp->h_length = sizeof(struct in6_addr);
610 hp->h_addrtype = hp1->h_addrtype;
611 hp->h_length = hp1->h_length;
615 hp->h_addr_list = addrs;
617 for (i = 1; i <= 2; i++) {
618 if ((pp = HP(i)->h_addr_list) == NULL)
620 if (HP(i)->h_length == hp->h_length) {
621 while (naddr < MAXADDRS && *pp != NULL)
622 addrs[naddr++] = *pp++;
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];
633 hp = _hpcopy(hp, errp);
640 * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses
643 static struct hostent *
644 _hpmapv6(struct hostent *hp, int *errp)
650 if (hp->h_addrtype == AF_INET6)
653 /* make dummy hostent to convert IPv6 address */
654 if ((hp6 = (struct hostent *)malloc(sizeof(struct hostent))) == 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);
668 * _hpsort: sort address by sortlist
670 static struct hostent *
671 _hpsort(struct hostent *hp)
674 u_char *ap, *sp, *mp, **pp;
676 char order[MAXADDRS];
677 int nsort = _res.nsort;
679 if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0)
681 for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
682 for (j = 0; j < nsort; j++) {
684 if (_res_ext.sort_list[j].af != hp->h_addrtype)
686 sp = (u_char *)&_res_ext.sort_list[j].addr;
687 mp = (u_char *)&_res_ext.sort_list[j].mask;
689 sp = (u_char *)&_res.sort_list[j].addr;
690 mp = (u_char *)&_res.sort_list[j].mask;
692 for (n = 0; n < hp->h_length; n++) {
693 if ((ap[n] & mp[n]) != sp[n])
696 if (n == hp->h_length)
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]) {
723 static const char sep[] = "# \t\n";
726 for (p = *pp; (c = *p) != '\0'; p++) {
727 for (sp = sep; *sp != '\0'; sp++) {
732 p[1] = '\0'; /* ignore rest of line */
744 if (ret == NULL || *ret == '\0')
754 _files_open(int *errp)
757 fp = fopen(_PATH_HOSTS, "r");
764 _files_ghbyname(void *rval, void *cb_data, va_list ap)
770 char *p, *line, *addrstr, *cname;
772 struct hostent *rethp, *hp, hpbuf;
773 char *aliases[MAXALIASES + 1], *addrs[2];
774 union inx_addr addrbuf;
778 name = va_arg(ap, const char *);
779 af = va_arg(ap, int);
780 errp = va_arg(ap, int *);
782 *(struct hostent **)rval = NULL;
784 if ((fp = _files_open(errp)) == NULL)
789 while (fgets(buf, sizeof(buf), fp)) {
791 if ((addrstr = _hgetword(&line)) == NULL
792 || (cname = _hgetword(&line)) == NULL)
794 match = (strcasecmp(cname, name) == 0);
796 while ((p = _hgetword(&line)) != NULL) {
798 match = (strcasecmp(p, name) == 0);
799 if (nalias < MAXALIASES)
800 aliases[nalias++] = p;
806 if (inet_aton(addrstr, (struct in_addr *)&addrbuf)
808 *errp = NO_DATA; /* name found */
815 if (inet_pton(af, addrstr, &addrbuf) != 1) {
816 *errp = NO_DATA; /* name found */
823 if (inet_aton(addrstr, (struct in_addr *)&addrbuf)
829 if (inet_pton(AF_INET6, addrstr, &addrbuf) == 1) {
834 *errp = NO_DATA; /* name found */
840 hp->h_aliases = aliases;
841 aliases[nalias] = NULL;
843 hp->h_length = ADDRLEN(af);
844 hp->h_addr_list = addrs;
845 addrs[0] = (char *)&addrbuf;
847 hp = _hpcopy(hp, errp);
848 rethp = _hpmerge(rethp, hp, errp);
851 *(struct hostent **)rval = rethp;
852 return (rethp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
856 _files_ghbyaddr(void *rval, void *cb_data, va_list ap)
865 struct hostent *hp, hpbuf;
866 char *aliases[MAXALIASES + 1], *addrs[2];
867 union inx_addr addrbuf;
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 *);
875 *(struct hostent**)rval = NULL;
877 if ((fp = _files_open(errp)) == NULL)
880 while (fgets(buf, sizeof(buf), fp)) {
882 if ((p = _hgetword(&line)) == NULL
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)
891 hp->h_aliases = aliases;
893 while ((p = _hgetword(&line)) != NULL) {
894 if (nalias < MAXALIASES)
895 aliases[nalias++] = p;
897 aliases[nalias] = NULL;
899 hp->h_length = addrlen;
900 hp->h_addr_list = addrs;
901 addrs[0] = (char *)&addrbuf;
903 hp = _hpcopy(hp, errp);
907 *(struct hostent **)rval = hp;
908 return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
915 * XXX actually a hack, these are INET4 specific.
918 _nis_ghbyname(void *rval, void *cb_data, va_list ap)
923 struct hostent *hp = NULL;
925 name = va_arg(ap, const char *);
926 af = va_arg(ap, int);
927 errp = va_arg(ap, int *);
932 hp = _gethostbynisname(name, af);
934 hp = _hpcopy(hp, errp);
937 *(struct hostent **)rval = hp;
938 return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
943 _nis_ghbyaddr(void *rval, void *cb_data, va_list ap)
949 struct hostent *hp = NULL;
951 addr = va_arg(ap, const void *);
952 addrlen = va_arg(ap, int);
953 af = va_arg(ap, int);
956 hp = _gethostbynisaddr(addr, addrlen, af);
958 hp = _hpcopy(hp, errp);
960 *(struct hostent **)rval = hp;
961 return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
965 struct __res_type_list {
966 SLIST_ENTRY(__res_type_list) rtl_entry;
971 #define MAXPACKET PACKETSZ
973 #define MAXPACKET 1024
978 u_char buf[MAXPACKET];
981 static struct hostent *getanswer(const querybuf *, int, const char *, int,
982 struct hostent *, int *);
985 * we don't need to take care about sorting, nor IPv4 mapped address here.
987 static struct hostent *
988 getanswer(answer, anslen, qname, qtype, template, errp)
989 const querybuf *answer;
993 struct hostent *template;
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];
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];
1010 #define BOUNDED_INCR(x) \
1014 *errp = NO_RECOVERY; \
1019 #define BOUNDS_CHECK(ptr, count) \
1021 if ((ptr) + (count) > eom) { \
1022 *errp = NO_RECOVERY; \
1027 /* XXX do {} while (0) cannot be put here */
1028 #define DNS_ASSERT(x) \
1036 /* XXX do {} while (0) cannot be put here */
1037 #define DNS_FATAL(x) \
1046 template->h_name = NULL;
1047 eom = answer->buf + anslen;
1057 return (NULL); /* XXX should be abort(); */
1060 * find first satisfactory answer
1063 ancount = ntohs(hp->ancount);
1064 qdcount = ntohs(hp->qdcount);
1066 ep = hostbuf + sizeof hostbuf;
1068 BOUNDED_INCR(HFIXEDSZ);
1070 *errp = NO_RECOVERY;
1073 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1074 if ((n < 0) || !(*name_ok)(bp)) {
1075 *errp = NO_RECOVERY;
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).
1084 n = strlen(bp) + 1; /* for the \0 */
1085 if (n >= MAXHOSTNAMELEN) {
1086 *errp = NO_RECOVERY;
1089 template->h_name = bp;
1091 /* The qname can be abbreviated, but h_name is now absolute. */
1092 qname = template->h_name;
1096 template->h_aliases = host_aliases;
1099 template->h_addr_list = h_addr_ptrs;
1102 while (ancount-- > 0 && cp < eom && !had_error) {
1103 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1105 DNS_FATAL((*name_ok)(bp));
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 */
1113 cp += INT16SZ; /* len */
1114 BOUNDS_CHECK(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])
1120 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1122 DNS_FATAL((*name_ok)(tbuf));
1125 *errp = NO_RECOVERY;
1130 n = strlen(bp) + 1; /* for the \0 */
1131 DNS_FATAL(n < MAXHOSTNAMELEN);
1133 /* Get canonical name. */
1134 n = strlen(tbuf) + 1; /* for the \0 */
1135 DNS_FATAL(n <= ep - bp);
1136 DNS_FATAL(n < MAXHOSTNAMELEN);
1138 template->h_name = bp;
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)) {
1150 *errp = NO_RECOVERY;
1153 /* Get canonical name. */
1154 n = strlen(tbuf) + 1; /* for the \0 */
1155 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1164 DNS_ASSERT(type == qtype);
1167 DNS_ASSERT(strcasecmp(tname, bp) == 0);
1168 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1170 DNS_FATAL(res_hnok(bp));
1171 #if MULTI_PTRS_ARE_ALIASES
1174 *errp = NO_RECOVERY;
1178 template->h_name = bp;
1179 else if (ap < &host_aliases[MAXALIASES-1])
1184 n = strlen(bp) + 1; /* for the \0 */
1185 if (n >= MAXHOSTNAMELEN) {
1193 template->h_name = bp;
1194 *errp = NETDB_SUCCESS;
1199 DNS_ASSERT(strcasecmp(template->h_name, bp) == 0);
1200 DNS_ASSERT(n == template->h_length);
1204 template->h_name = bp;
1205 nn = strlen(bp) + 1; /* for the \0 */
1208 obp = bp; /* ALIGN rounds up */
1209 bp = (char *)ALIGN(bp);
1210 buflen -= (bp - obp);
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);
1221 bcopy(cp, *hap++ = bp, n);
1225 *errp = NO_RECOVERY;
1238 if (!template->h_name) {
1239 n = strlen(qname) + 1; /* for the \0 */
1240 if (n > ep - bp || n >= MAXHOSTNAMELEN)
1243 template->h_name = bp;
1246 *errp = NETDB_SUCCESS;
1250 *errp = NO_RECOVERY;
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 */
1266 const char *cp, * const *domain;
1267 struct hostent *hp0 = NULL, *hp;
1268 struct hostent hpbuf;
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;
1275 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
1276 *errp = NETDB_INTERNAL;
1280 for (cp = name; *cp; cp++)
1281 dots += (*cp == '.');
1283 if (cp > name && *--cp == '.')
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,
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,
1300 hp = _hpcopy(&hpbuf, errp);
1301 hp0 = _hpmerge(hp0, hp, errp);
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.
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));
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,
1325 hp = _hpcopy(&hpbuf, errp);
1326 hp0 = _hpmerge(hp0, hp, errp);
1331 saved_herrno = *errp;
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.
1341 if ((!dots && (_res.options & RES_DEFNAMES)) ||
1342 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
1345 for (domain = (const char * const *)_res.dnsrch;
1349 for(rtl = rtl0; rtl != NULL;
1350 rtl = SLIST_NEXT(rtl, rtl_entry)) {
1351 ret = res_querydomain(name, *domain, C_IN,
1353 buf.buf, sizeof(buf.buf));
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);
1362 hp = _hpcopy(&hpbuf, errp);
1363 hp0 = _hpmerge(hp0, hp, errp);
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
1382 if (errno == ECONNREFUSED) {
1391 case HOST_NOT_FOUND:
1395 if (buf.hdr.rcode == SERVFAIL) {
1396 /* try next search element, if any */
1402 /* anything else implies that we're done */
1406 /* if we got here for some reason other than DNSRCH,
1407 * we only wanted one iteration of the loop, so stop.
1409 if (!(_res.options & RES_DNSRCH))
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.
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));
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,
1432 hp = _hpcopy(&hpbuf, errp);
1433 hp0 = _hpmerge(hp0, hp, errp);
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.
1447 if (saved_herrno != -1)
1448 *errp = saved_herrno;
1449 else if (got_nodata)
1451 else if (got_servfail)
1457 _dns_ghbyname(void *rval, void *cb_data, va_list ap)
1462 struct __res_type_list *rtl, rtl4;
1464 struct __res_type_list rtl6;
1467 name = va_arg(ap, const char *);
1468 af = va_arg(ap, int);
1469 errp = va_arg(ap, int *);
1474 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1475 SLIST_NEXT(&rtl6, rtl_entry) = &rtl4; rtl6.rtl_type = T_AAAA;
1479 SLIST_NEXT(&rtl6, rtl_entry) = NULL; rtl6.rtl_type = T_AAAA;
1483 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1488 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1491 *(struct hostent **)rval = _res_search_multi(name, rtl, errp);
1492 return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
1496 _dns_ghbyaddr(void *rval, void *cb_data, va_list ap)
1506 struct hostent hbuf;
1509 static const char hex[] = "0123456789abcdef";
1512 char qbuf[MAXDNAME+1];
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 *);
1520 *(struct hostent **)rval = NULL;
1524 if (af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr))
1528 if ((_res.options & RES_INIT) == 0) {
1529 if (res_init() < 0) {
1534 memset(&hbuf, 0, sizeof(hbuf));
1536 hbuf.h_addrtype = af;
1537 hbuf.h_length = addrlen;
1540 /* XXX assumes that MAXDNAME is big enough */
1543 cp = (u_char *)addr+addrlen-1;
1547 for (; n < addrlen; n++, cp--) {
1549 *bp++ = hex[c & 0xf];
1551 *bp++ = hex[c >> 4];
1554 strcpy(bp, "ip6.int");
1558 for (; n < addrlen; n++, cp--) {
1561 *bp++ = '0' + c / 100;
1563 *bp++ = '0' + (c % 100) / 10;
1564 *bp++ = '0' + c % 10;
1567 strcpy(bp, "in-addr.arpa");
1571 n = res_query(qbuf, C_IN, T_PTR, buf.buf, sizeof buf.buf);
1576 hp = getanswer(&buf, n, qbuf, T_PTR, &hbuf, errp);
1579 hbuf.h_addrtype = af;
1580 hbuf.h_length = addrlen;
1581 hbuf.h_addr_list = hlist;
1582 hlist[0] = (char *)addr;
1584 *(struct hostent **)rval = _hpcopy(&hbuf, errp);
1589 _dns_shent(int stayopen)
1591 if ((_res.options & RES_INIT) == 0) {
1596 _res.options |= RES_STAYOPEN | RES_USEVC;
1602 _res.options &= ~(RES_STAYOPEN | RES_USEVC);
1610 * draft-ietf-ipngwg-icmp-namelookups-02.txt
1611 * ifindex is assumed to be encoded in addr.
1613 #include <sys/uio.h>
1614 #include <netinet/ip6.h>
1615 #include <netinet/icmp6.h>
1617 struct _icmp_host_cache {
1618 struct _icmp_host_cache *hc_next;
1620 struct in6_addr hc_addr;
1625 _icmp_fqdn_query(const struct in6_addr *addr, int ifindex)
1628 struct icmp6_filter filter;
1630 struct cmsghdr *cmsg;
1631 struct in6_pktinfo *pkt;
1635 struct icmp6_fqdn_query *fq;
1636 struct icmp6_fqdn_reply *fr;
1637 struct _icmp_host_cache *hc;
1638 struct sockaddr_in6 sin6;
1641 struct timeval tout;
1645 static struct _icmp_host_cache *hc_head;
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))
1656 ICMP6_FILTER_SETBLOCKALL(&filter);
1657 ICMP6_FILTER_SETPASS(ICMP6_FQDN_REPLY, &filter);
1661 tout.tv_usec = 200000; /*XXX: 200ms*/
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;
1672 memset(&sin6, 0, sizeof(sin6));
1673 sin6.sin6_family = AF_INET6;
1674 sin6.sin6_addr = *addr;
1676 memset(&msg, 0, sizeof(msg));
1677 msg.msg_name = (caddr_t)&sin6;
1678 msg.msg_namelen = sizeof(sin6);
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);
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;
1700 if ((s = _socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
1702 (void)_setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER,
1703 (char *)&filter, sizeof(filter));
1704 cc = _sendmsg(s, &msg, 0);
1712 if (_select(s + 1, &fds, NULL, NULL, &tout) <= 0) {
1717 cc = _recvfrom(s, buf, sizeof(buf), 0,
1718 (struct sockaddr *)&sin6, &len);
1723 if (cc < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr))
1725 if (!IN6_ARE_ADDR_EQUAL(addr, &sin6.sin6_addr))
1727 fr = (struct icmp6_fqdn_reply *)(buf + sizeof(struct ip6_hdr));
1728 if (fr->icmp6_fqdn_type == ICMP6_FQDN_REPLY)
1732 if (fr->icmp6_fqdn_cookie[1] != 0) {
1734 name = buf + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 4;
1735 len = (buf + cc) - name;
1737 len = fr->icmp6_fqdn_namelen;
1738 name = fr->icmp6_fqdn_name;
1744 if ((hc = (struct _icmp_host_cache *)malloc(sizeof(*hc))) == 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;
1755 static struct hostent *
1756 _icmp_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
1760 struct in6_addr addr6;
1762 if (af != AF_INET6) {
1764 * Note: rfc1788 defines Who Are You for IPv4,
1765 * but no one implements it.
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;
1774 if (!IN6_IS_ADDR_LINKLOCAL(&addr6))
1775 return NULL; /*XXX*/
1777 if ((hname = _icmp_fqdn_query(&addr6, ifindex)) == NULL)
1779 return _hpaddr(af, hname, &addr6, errp);