1 /* $FreeBSD: src/lib/libc/net/name6.c,v 1.6.2.9 2002/11/02 18:54:57 ume Exp $ */
2 /* $KAME: name6.c,v 1.25 2000/06/26 16:44:40 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * ++Copyright++ 1985, 1988, 1993
35 * Copyright (c) 1985, 1988, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
68 * Permission to use, copy, modify, and distribute this software for any
69 * purpose with or without fee is hereby granted, provided that the above
70 * copyright notice and this permission notice appear in all copies, and that
71 * the name of Digital Equipment Corporation not be used in advertising or
72 * publicity pertaining to distribution of the document or software without
73 * specific, written prior permission.
75 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
76 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
77 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
78 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
79 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
80 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
81 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
88 * Atsushi Onoe <onoe@sm.sony.co.jp>
92 * TODO for thread safe
93 * use mutex for _hostconf, _hostconf_init.
94 * rewrite resolvers to be thread safe
97 #include <sys/param.h>
98 #include <sys/socket.h>
100 #include <sys/queue.h>
101 #include <netinet/in.h>
103 #include <arpa/inet.h>
104 #include <arpa/nameser.h>
115 #define _PATH_HOSTS "/etc/hosts"
119 #define MAXALIASES 10
125 #define MAXDNAME 1025
129 #define ADDRLEN(af) ((af) == AF_INET6 ? sizeof(struct in6_addr) : \
130 sizeof(struct in_addr))
132 #define ADDRLEN(af) sizeof(struct in_addr)
135 #define MAPADDR(ab, ina) \
137 memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr)); \
138 memset((ab)->map_zero, 0, sizeof((ab)->map_zero)); \
139 memset((ab)->map_one, 0xff, sizeof((ab)->map_one)); \
141 #define MAPADDRENABLED(flags) \
142 (((flags) & AI_V4MAPPED) || \
143 (((flags) & AI_V4MAPPED_CFG) && _mapped_addr_enabled()))
146 struct in_addr in_addr;
148 struct in6_addr in6_addr;
153 struct in_addr mau_inaddr;
155 #define map_zero map_addr_un.mau_zero
156 #define map_one map_addr_un.mau_one
157 #define map_inaddr map_addr_un.mau_inaddr
160 static struct hostent *_hpcopy(struct hostent *hp, int *errp);
161 static struct hostent *_hpaddr(int af, const char *name, void *addr, int *errp);
162 static struct hostent *_hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp);
164 static struct hostent *_hpmapv6(struct hostent *hp, int *errp);
166 static struct hostent *_hpsort(struct hostent *hp);
167 static struct hostent *_ghbyname(const char *name, int af, int flags, int *errp);
168 static char *_hgetword(char **pp);
169 static int _mapped_addr_enabled(void);
171 static FILE *_files_open(int *errp);
172 static struct hostent *_files_ghbyname(const char *name, int af, int *errp);
173 static struct hostent *_files_ghbyaddr(const void *addr, int addrlen, int af, int *errp);
174 static void _files_shent(int stayopen);
175 static void _files_ehent(void);
177 static struct hostent *_nis_ghbyname(const char *name, int af, int *errp);
178 static struct hostent *_nis_ghbyaddr(const void *addr, int addrlen, int af, int *errp);
180 static struct hostent *_dns_ghbyname(const char *name, int af, int *errp);
181 static struct hostent *_dns_ghbyaddr(const void *addr, int addrlen, int af, int *errp);
182 static void _dns_shent(int stayopen);
183 static void _dns_ehent(void);
185 static struct hostent *_icmp_ghbyaddr(const void *addr, int addrlen, int af, int *errp);
188 /* Make getipnodeby*() thread-safe in libc for use with kernel threads. */
189 #include "libc_private.h"
190 #include "spinlock.h"
192 * XXX: Our res_*() is not thread-safe. So, we share lock between
193 * getaddrinfo() and getipnodeby*(). Still, we cannot use
194 * getaddrinfo() and getipnodeby*() in conjunction with other
195 * functions which call res_*().
197 extern spinlock_t __getaddrinfo_thread_lock;
198 #define THREAD_LOCK() \
199 if (__isthreaded) _SPINLOCK(&__getaddrinfo_thread_lock);
200 #define THREAD_UNLOCK() \
201 if (__isthreaded) _SPINUNLOCK(&__getaddrinfo_thread_lock);
204 * Select order host function.
206 #define MAXHOSTCONF 4
209 # define HOSTCONF "/etc/host.conf"
210 #endif /* !HOSTCONF */
213 struct hostent *(*byname)(const char *name, int af, int *errp);
214 struct hostent *(*byaddr)(const void *addr, int addrlen, int af, int *errp);
218 static struct _hostconf _hostconf[MAXHOSTCONF] = {
219 { _dns_ghbyname, _dns_ghbyaddr },
220 { _files_ghbyname, _files_ghbyaddr },
222 { NULL, _icmp_ghbyaddr },
226 static int _hostconf_init_done;
227 static void _hostconf_init(void);
230 * Initialize hostconf structure.
241 _hostconf_init_done = 1;
244 if ((fp = fopen(p, "r")) == NULL)
246 while (n < MAXHOSTCONF && fgets(buf, sizeof(buf), fp)) {
248 if ((p = _hgetword(&line)) == NULL)
251 if (strcmp(p, "hosts") == 0
252 || strcmp(p, "local") == 0
253 || strcmp(p, "file") == 0
254 || strcmp(p, "files") == 0) {
255 _hostconf[n].byname = _files_ghbyname;
256 _hostconf[n].byaddr = _files_ghbyaddr;
259 else if (strcmp(p, "dns") == 0
260 || strcmp(p, "bind") == 0) {
261 _hostconf[n].byname = _dns_ghbyname;
262 _hostconf[n].byaddr = _dns_ghbyaddr;
266 else if (strcmp(p, "nis") == 0) {
267 _hostconf[n].byname = _nis_ghbyname;
268 _hostconf[n].byaddr = _nis_ghbyaddr;
273 else if (strcmp(p, "icmp") == 0) {
274 _hostconf[n].byname = NULL;
275 _hostconf[n].byaddr = _icmp_ghbyaddr;
279 } while ((p = _hgetword(&line)) != NULL);
283 /* no keyword found. do not change default configuration */
286 for (; n < MAXHOSTCONF; n++) {
287 _hostconf[n].byname = NULL;
288 _hostconf[n].byaddr = NULL;
293 * Check if kernel supports mapped address.
294 * implementation dependent
297 #include <sys/sysctl.h>
298 #endif /* __KAME__ */
301 _mapped_addr_enabled(void)
303 /* implementation dependent check */
304 #if defined(__KAME__) && defined(IPV6CTL_MAPPED_ADDR)
311 mib[2] = IPPROTO_IPV6;
312 mib[3] = IPV6CTL_MAPPED_ADDR;
314 if (sysctl(mib, 4, &val, &len, 0, 0) == 0 && val != 0)
316 #endif /* __KAME__ && IPV6CTL_MAPPED_ADDR */
321 * Functions defined in RFC2553
322 * getipnodebyname, getipnodebyaddr, freehostent
325 static struct hostent *
326 _ghbyname(const char *name, int af, int flags, int *errp)
331 if (flags & AI_ADDRCONFIG) {
336 * Note that implementation dependent test for address
337 * configuration should be done everytime called
338 * (or apropriate interval),
339 * because addresses will be dynamically assigned or deleted.
341 if (af == AF_UNSPEC) {
342 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
346 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
353 if (af != AF_UNSPEC) {
354 if ((s = socket(af, SOCK_DGRAM, 0)) < 0)
361 for (i = 0; i < MAXHOSTCONF; i++) {
362 if (_hostconf[i].byname
363 && (hp = (*_hostconf[i].byname)(name, af, errp)) != NULL) {
373 /* getipnodebyname() internal routine for multiple query(PF_UNSPEC) support. */
375 _getipnodebyname_multi(const char *name, int af, int flags, int *errp)
378 union inx_addr addrbuf;
380 /* XXX: PF_UNSPEC is only supposed to be passed from getaddrinfo() */
393 /* special case for literal address */
394 if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
395 if (af != AF_INET6) {
396 *errp = HOST_NOT_FOUND;
399 return _hpaddr(af, name, &addrbuf, errp);
402 if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) {
404 if (MAPADDRENABLED(flags)) {
405 MAPADDR(&addrbuf, &addrbuf.in_addr);
407 *errp = HOST_NOT_FOUND;
411 return _hpaddr(af, name, &addrbuf, errp);
414 if (!_hostconf_init_done)
417 *errp = HOST_NOT_FOUND;
418 hp = _ghbyname(name, af, flags, errp);
422 && ((flags & AI_ALL) || hp == NULL)
423 && (MAPADDRENABLED(flags))) {
424 struct hostent *hp2 = _ghbyname(name, AF_INET, flags, errp);
426 hp = _hpmapv6(hp2, errp);
428 if (hp2 && strcmp(hp->h_name, hp2->h_name) != 0) {
432 hp = _hpmerge(hp, hp2, errp);
440 getipnodebyname(const char *name, int af, int flags, int *errp)
451 return(_getipnodebyname_multi(name, af ,flags, errp));
455 getipnodebyaddr(const void *src, size_t len, int af, int *errp)
460 struct in6_addr addrbuf;
462 struct in_addr addrbuf;
465 *errp = HOST_NOT_FOUND;
469 if (len != sizeof(struct in_addr)) {
473 if ((long)src & ~(sizeof(struct in_addr) - 1)) {
474 memcpy(&addrbuf, src, len);
477 if (((struct in_addr *)src)->s_addr == 0)
482 if (len != sizeof(struct in6_addr)) {
486 if ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) { /*XXX*/
487 memcpy(&addrbuf, src, len);
490 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src))
492 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)
493 || IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) {
495 (sizeof(struct in6_addr) - sizeof(struct in_addr));
497 len = sizeof(struct in_addr);
506 if (!_hostconf_init_done)
509 for (i = 0; i < MAXHOSTCONF; i++) {
510 if (_hostconf[i].byaddr
511 && (hp = (*_hostconf[i].byaddr)(src, len, af, errp)) != NULL) {
522 freehostent(struct hostent *ptr)
529 /* XXX: should be deprecated */
531 getnodebyname(const char *name, int af, int flags)
533 return getipnodebyname(name, af, flags, &h_errno);
536 #ifdef __warn_references
537 __warn_references(getnodebyname,
538 "warning: getnodebyname() deprecated, "
539 "should use getaddrinfo() or getipnodebyname()");
543 getnodebyaddr(const void *src, size_t len, int af)
545 return getipnodebyaddr(src, len, af, &h_errno);
548 #ifdef __warn_references
549 __warn_references(getnodebyaddr,
550 "warning: getnodebyaddr() deprecated, "
551 "should use getnameinfo() or getipnodebyaddr()");
557 * Private utility functions
561 * _hpcopy: allocate and copy hostent structure
563 static struct hostent *
564 _hpcopy(struct hostent *hp, int *errp)
569 int nalias = 0, naddr = 0;
576 /* count size to be allocated */
577 size = sizeof(struct hostent);
578 if (hp->h_name != NULL)
579 size += strlen(hp->h_name) + 1;
580 if ((pp = hp->h_aliases) != NULL) {
581 for (i = 0; *pp != NULL; i++, pp++) {
583 size += strlen(*pp) + 1;
588 /* adjust alignment */
591 size += sizeof(char *) * (nalias + 1);
592 addrsize = ALIGN(hp->h_length);
593 if ((pp = hp->h_addr_list) != NULL) {
594 while (*pp++ != NULL)
597 size += addrsize * naddr;
598 size += sizeof(char *) * (naddr + 1);
601 if ((nhp = (struct hostent *)malloc(size)) == NULL) {
605 cp = (char *)&nhp[1];
606 if (hp->h_name != NULL) {
608 strcpy(cp, hp->h_name);
609 cp += strlen(cp) + 1;
612 nhp->h_aliases = (char **)((char *)nhp + al_off);
613 if ((pp = hp->h_aliases) != NULL) {
614 for (i = 0; *pp != NULL; pp++) {
616 nhp->h_aliases[i++] = cp;
618 cp += strlen(cp) + 1;
622 nhp->h_aliases[nalias] = NULL;
623 cp = (char *)&nhp->h_aliases[nalias + 1];
624 nhp->h_addrtype = hp->h_addrtype;
625 nhp->h_length = hp->h_length;
626 nhp->h_addr_list = (char **)cp;
627 if ((pp = hp->h_addr_list) != NULL) {
628 cp = (char *)&nhp->h_addr_list[naddr + 1];
629 for (i = 0; *pp != NULL; pp++) {
630 nhp->h_addr_list[i++] = cp;
631 memcpy(cp, *pp, hp->h_length);
635 nhp->h_addr_list[naddr] = NULL;
640 * _hpaddr: construct hostent structure with one address
642 static struct hostent *
643 _hpaddr(int af, const char *name, void *addr, int *errp)
645 struct hostent *hp, hpbuf;
649 hp->h_name = (char *)name;
650 hp->h_aliases = NULL;
652 hp->h_length = ADDRLEN(af);
653 hp->h_addr_list = addrs;
654 addrs[0] = (char *)addr;
656 return _hpcopy(hp, errp);
660 * _hpmerge: merge 2 hostent structure, arguments will be freed
662 static struct hostent *
663 _hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp)
668 struct hostent *hp, hpbuf;
669 char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1];
670 union inx_addr addrbuf[MAXADDRS];
677 #define HP(i) (i == 1 ? hp1 : hp2)
679 hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name);
680 hp->h_aliases = aliases;
682 for (i = 1; i <= 2; i++) {
683 if ((pp = HP(i)->h_aliases) == NULL)
685 for (; nalias < MAXALIASES && *pp != NULL; pp++) {
686 /* check duplicates */
687 for (j = 0; j < nalias; j++)
688 if (strcasecmp(*pp, aliases[j]) == 0)
691 aliases[nalias++] = *pp;
694 aliases[nalias] = NULL;
696 if (hp1->h_length != hp2->h_length) {
697 hp->h_addrtype = AF_INET6;
698 hp->h_length = sizeof(struct in6_addr);
701 hp->h_addrtype = hp1->h_addrtype;
702 hp->h_length = hp1->h_length;
706 hp->h_addr_list = addrs;
708 for (i = 1; i <= 2; i++) {
709 if ((pp = HP(i)->h_addr_list) == NULL)
711 if (HP(i)->h_length == hp->h_length) {
712 while (naddr < MAXADDRS && *pp != NULL)
713 addrs[naddr++] = *pp++;
715 /* copy IPv4 addr as mapped IPv6 addr */
716 while (naddr < MAXADDRS && *pp != NULL) {
717 MAPADDR(&addrbuf[naddr], *pp++);
718 addrs[naddr] = (char *)&addrbuf[naddr];
724 hp = _hpcopy(hp, errp);
731 * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses
734 static struct hostent *
735 _hpmapv6(struct hostent *hp, int *errp)
741 if (hp->h_addrtype == AF_INET6)
744 /* make dummy hostent to convert IPv6 address */
745 if ((hp6 = (struct hostent *)malloc(sizeof(struct hostent))) == NULL) {
750 hp6->h_aliases = NULL;
751 hp6->h_addrtype = AF_INET6;
752 hp6->h_length = sizeof(struct in6_addr);
753 hp6->h_addr_list = NULL;
754 return _hpmerge(hp6, hp, errp);
759 * _hpsort: sort address by sortlist
761 static struct hostent *
762 _hpsort(struct hostent *hp)
765 u_char *ap, *sp, *mp, **pp;
767 char order[MAXADDRS];
768 int nsort = _res.nsort;
770 if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0)
772 for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
773 for (j = 0; j < nsort; j++) {
775 if (_res_ext.sort_list[j].af != hp->h_addrtype)
777 sp = (u_char *)&_res_ext.sort_list[j].addr;
778 mp = (u_char *)&_res_ext.sort_list[j].mask;
780 sp = (u_char *)&_res.sort_list[j].addr;
781 mp = (u_char *)&_res.sort_list[j].mask;
783 for (n = 0; n < hp->h_length; n++) {
784 if ((ap[n] & mp[n]) != sp[n])
787 if (n == hp->h_length)
793 pp = (u_char **)hp->h_addr_list;
794 for (i = 0; i < n - 1; i++) {
795 for (j = i + 1; j < n; j++) {
796 if (order[i] > order[j]) {
814 static const char sep[] = "# \t\n";
817 for (p = *pp; (c = *p) != '\0'; p++) {
818 for (sp = sep; *sp != '\0'; sp++) {
823 p[1] = '\0'; /* ignore rest of line */
835 if (ret == NULL || *ret == '\0')
845 _files_open(int *errp)
848 fp = fopen(_PATH_HOSTS, "r");
854 static struct hostent *
855 _files_ghbyname(const char *name, int af, int *errp)
858 char *p, *line, *addrstr, *cname;
860 struct hostent *rethp, *hp, hpbuf;
861 char *aliases[MAXALIASES + 1], *addrs[2];
862 union inx_addr addrbuf;
866 if ((fp = _files_open(errp)) == NULL)
870 while (fgets(buf, sizeof(buf), fp)) {
872 if ((addrstr = _hgetword(&line)) == NULL
873 || (cname = _hgetword(&line)) == NULL)
875 match = (strcasecmp(cname, name) == 0);
877 while ((p = _hgetword(&line)) != NULL) {
879 match = (strcasecmp(p, name) == 0);
880 if (nalias < MAXALIASES)
881 aliases[nalias++] = p;
887 if (inet_aton(addrstr, (struct in_addr *)&addrbuf)
889 *errp = NO_DATA; /* name found */
896 if (inet_pton(af, addrstr, &addrbuf) != 1) {
897 *errp = NO_DATA; /* name found */
904 if (inet_aton(addrstr, (struct in_addr *)&addrbuf)
910 if (inet_pton(AF_INET6, addrstr, &addrbuf) == 1) {
915 *errp = NO_DATA; /* name found */
921 hp->h_aliases = aliases;
922 aliases[nalias] = NULL;
924 hp->h_length = ADDRLEN(af);
925 hp->h_addr_list = addrs;
926 addrs[0] = (char *)&addrbuf;
928 hp = _hpcopy(hp, errp);
929 rethp = _hpmerge(rethp, hp, errp);
935 static struct hostent *
936 _files_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
941 struct hostent *hp, hpbuf;
942 char *aliases[MAXALIASES + 1], *addrs[2];
943 union inx_addr addrbuf;
946 if ((fp = _files_open(errp)) == NULL)
949 while (fgets(buf, sizeof(buf), fp)) {
951 if ((p = _hgetword(&line)) == NULL
953 ? inet_aton(p, (struct in_addr *)&addrbuf)
954 : inet_pton(af, p, &addrbuf)) != 1
955 || memcmp(addr, &addrbuf, addrlen) != 0
956 || (p = _hgetword(&line)) == NULL)
960 hp->h_aliases = aliases;
962 while ((p = _hgetword(&line)) != NULL) {
963 if (nalias < MAXALIASES)
964 aliases[nalias++] = p;
966 aliases[nalias] = NULL;
968 hp->h_length = addrlen;
969 hp->h_addr_list = addrs;
970 addrs[0] = (char *)&addrbuf;
972 hp = _hpcopy(hp, errp);
983 * XXX actually a hack, these are INET4 specific.
985 static struct hostent *
986 _nis_ghbyname(const char *name, int af, int *errp)
988 struct hostent *hp = NULL;
993 hp = _gethostbynisname(name, af);
995 hp = _hpcopy(hp, errp);
1001 static struct hostent *
1002 _nis_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
1004 struct hostent *hp = NULL;
1006 if (af == AF_INET) {
1007 hp = _gethostbynisaddr(addr, addrlen, af);
1009 hp = _hpcopy(hp, errp);
1015 struct __res_type_list {
1016 SLIST_ENTRY(__res_type_list) rtl_entry;
1020 #define MAXPACKET (64*1024)
1024 u_char buf[MAXPACKET];
1027 static struct hostent *getanswer __P((const querybuf *, int, const char *,
1028 int, struct hostent *, int *));
1031 * we don't need to take care about sorting, nor IPv4 mapped address here.
1033 static struct hostent *
1034 getanswer(answer, anslen, qname, qtype, template, errp)
1035 const querybuf *answer;
1039 struct hostent *template;
1042 register const HEADER *hp;
1043 register const u_char *cp;
1045 const u_char *eom, *erdata;
1046 char *bp, **ap, **hap, *obp;
1047 int type, class, buflen, ancount, qdcount;
1048 int haveanswer, had_error;
1049 char tbuf[MAXDNAME];
1051 int (*name_ok) __P((const char *));
1052 static char *h_addr_ptrs[MAXADDRS + 1];
1053 static char *host_aliases[MAXALIASES];
1054 static char hostbuf[8*1024];
1056 #define BOUNDED_INCR(x) \
1060 *errp = NO_RECOVERY; \
1065 #define BOUNDS_CHECK(ptr, count) \
1067 if ((ptr) + (count) > eom) { \
1068 *errp = NO_RECOVERY; \
1073 /* XXX do {} while (0) cannot be put here */
1074 #define DNS_ASSERT(x) \
1082 /* XXX do {} while (0) cannot be put here */
1083 #define DNS_FATAL(x) \
1092 template->h_name = NULL;
1093 eom = answer->buf + anslen;
1103 return (NULL); /* XXX should be abort(); */
1106 * find first satisfactory answer
1109 ancount = ntohs(hp->ancount);
1110 qdcount = ntohs(hp->qdcount);
1112 buflen = sizeof hostbuf;
1114 BOUNDED_INCR(HFIXEDSZ);
1116 *errp = NO_RECOVERY;
1119 n = dn_expand(answer->buf, eom, cp, bp, buflen);
1120 if ((n < 0) || !(*name_ok)(bp)) {
1121 *errp = NO_RECOVERY;
1124 BOUNDED_INCR(n + QFIXEDSZ);
1125 if (qtype == T_A || qtype == T_AAAA) {
1126 /* res_send() has already verified that the query name is the
1127 * same as the one we sent; this just gets the expanded name
1128 * (i.e., with the succeeding search-domain tacked on).
1130 n = strlen(bp) + 1; /* for the \0 */
1131 if (n >= MAXHOSTNAMELEN) {
1132 *errp = NO_RECOVERY;
1135 template->h_name = bp;
1138 /* The qname can be abbreviated, but h_name is now absolute. */
1139 qname = template->h_name;
1143 template->h_aliases = host_aliases;
1146 template->h_addr_list = h_addr_ptrs;
1149 while (ancount-- > 0 && cp < eom && !had_error) {
1150 n = dn_expand(answer->buf, eom, cp, bp, buflen);
1152 DNS_FATAL((*name_ok)(bp));
1154 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
1155 type = _getshort(cp);
1156 cp += INT16SZ; /* type */
1157 class = _getshort(cp);
1158 cp += INT16SZ + INT32SZ; /* class, TTL */
1160 cp += INT16SZ; /* len */
1161 BOUNDS_CHECK(cp, n);
1163 DNS_ASSERT(class == C_IN);
1164 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
1165 if (ap >= &host_aliases[MAXALIASES-1])
1167 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1169 DNS_FATAL((*name_ok)(tbuf));
1172 *errp = NO_RECOVERY;
1177 n = strlen(bp) + 1; /* for the \0 */
1178 DNS_FATAL(n < MAXHOSTNAMELEN);
1181 /* Get canonical name. */
1182 n = strlen(tbuf) + 1; /* for the \0 */
1183 DNS_FATAL(n <= buflen);
1184 DNS_FATAL(n < MAXHOSTNAMELEN);
1186 template->h_name = bp;
1191 if (qtype == T_PTR && type == T_CNAME) {
1192 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1193 if (n < 0 || !res_dnok(tbuf)) {
1199 *errp = NO_RECOVERY;
1202 /* Get canonical name. */
1203 n = strlen(tbuf) + 1; /* for the \0 */
1204 if (n > buflen || n >= MAXHOSTNAMELEN) {
1214 DNS_ASSERT(type == qtype);
1217 DNS_ASSERT(strcasecmp(tname, bp) == 0);
1218 n = dn_expand(answer->buf, eom, cp, bp, buflen);
1220 DNS_FATAL(res_hnok(bp));
1221 #if MULTI_PTRS_ARE_ALIASES
1224 *errp = NO_RECOVERY;
1228 template->h_name = bp;
1229 else if (ap < &host_aliases[MAXALIASES-1])
1234 n = strlen(bp) + 1; /* for the \0 */
1235 if (n >= MAXHOSTNAMELEN) {
1244 template->h_name = bp;
1245 *errp = NETDB_SUCCESS;
1250 DNS_ASSERT(strcasecmp(template->h_name, bp) == 0);
1251 DNS_ASSERT(n == template->h_length);
1255 template->h_name = bp;
1256 nn = strlen(bp) + 1; /* for the \0 */
1260 obp = bp; /* ALIGN rounds up */
1261 bp = (char *)ALIGN(bp);
1262 buflen -= (bp - obp);
1264 DNS_FATAL(bp + n < &hostbuf[sizeof hostbuf]);
1265 DNS_ASSERT(hap < &h_addr_ptrs[MAXADDRS-1]);
1266 #ifdef FILTER_V4MAPPED
1267 if (type == T_AAAA) {
1268 struct in6_addr in6;
1269 memcpy(&in6, cp, sizeof(in6));
1270 DNS_ASSERT(IN6_IS_ADDR_V4MAPPED(&in6) == 0);
1273 bcopy(cp, *hap++ = bp, n);
1278 *errp = NO_RECOVERY;
1291 if (!template->h_name) {
1292 n = strlen(qname) + 1; /* for the \0 */
1293 if (n > buflen || n >= MAXHOSTNAMELEN)
1296 template->h_name = bp;
1300 *errp = NETDB_SUCCESS;
1304 *errp = NO_RECOVERY;
1313 /* res_search() variant with multiple query support. */
1314 static struct hostent *
1315 _res_search_multi(name, rtl, errp)
1316 const char *name; /* domain name */
1317 struct __res_type_list *rtl; /* list of query types */
1320 const char *cp, * const *domain;
1321 struct hostent *hp0 = NULL, *hp;
1322 struct hostent hpbuf;
1324 int trailing_dot, ret, saved_herrno;
1325 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
1326 struct __res_type_list *rtl0 = rtl;
1329 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
1330 *errp = NETDB_INTERNAL;
1334 for (cp = name; *cp; cp++)
1335 dots += (*cp == '.');
1337 if (cp > name && *--cp == '.')
1340 buf = malloc(sizeof(*buf));
1342 *errp = NETDB_INTERNAL;
1346 /* If there aren't any dots, it could be a user-level alias */
1347 if (!dots && (cp = hostalias(name)) != NULL) {
1348 for(rtl = rtl0; rtl != NULL;
1349 rtl = SLIST_NEXT(rtl, rtl_entry)) {
1350 ret = res_query(cp, C_IN, rtl->rtl_type, buf->buf,
1352 if (ret > 0 && ret < sizeof(buf->buf)) {
1353 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1354 ? AF_INET6 : AF_INET;
1355 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1356 hp = getanswer(buf, ret, name, rtl->rtl_type,
1360 hp = _hpcopy(&hpbuf, errp);
1361 hp0 = _hpmerge(hp0, hp, errp);
1369 * If there are dots in the name already, let's just give it a try
1370 * 'as is'. The threshold can be set with the "ndots" option.
1373 if (dots >= _res.ndots) {
1374 for(rtl = rtl0; rtl != NULL;
1375 rtl = SLIST_NEXT(rtl, rtl_entry)) {
1376 ret = res_querydomain(name, NULL, C_IN, rtl->rtl_type,
1377 buf->buf, sizeof(buf->buf));
1378 if (ret > 0 && ret < sizeof(buf->buf)) {
1379 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1380 ? AF_INET6 : AF_INET;
1381 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1382 hp = getanswer(buf, ret, name, rtl->rtl_type,
1386 hp = _hpcopy(&hpbuf, errp);
1387 hp0 = _hpmerge(hp0, hp, errp);
1394 saved_herrno = *errp;
1399 * We do at least one level of search if
1400 * - there is no dot and RES_DEFNAME is set, or
1401 * - there is at least one dot, there is no trailing dot,
1402 * and RES_DNSRCH is set.
1404 if ((!dots && (_res.options & RES_DEFNAMES)) ||
1405 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
1408 for (domain = (const char * const *)_res.dnsrch;
1412 for(rtl = rtl0; rtl != NULL;
1413 rtl = SLIST_NEXT(rtl, rtl_entry)) {
1414 ret = res_querydomain(name, *domain, C_IN,
1416 buf->buf, sizeof(buf->buf));
1417 if (ret > 0 && ret < sizeof(buf->buf)) {
1418 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1419 ? AF_INET6 : AF_INET;
1420 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1421 hp = getanswer(buf, ret, name,
1422 rtl->rtl_type, &hpbuf, errp);
1425 hp = _hpcopy(&hpbuf, errp);
1426 hp0 = _hpmerge(hp0, hp, errp);
1435 * If no server present, give up.
1436 * If name isn't found in this domain,
1437 * keep trying higher domains in the search list
1438 * (if that's enabled).
1439 * On a NO_DATA error, keep trying, otherwise
1440 * a wildcard entry of another type could keep us
1441 * from finding this entry higher in the domain.
1442 * If we get some other error (negative answer or
1443 * server failure), then stop searching up,
1444 * but try the input name below in case it's
1447 if (errno == ECONNREFUSED) {
1457 case HOST_NOT_FOUND:
1461 if (buf->hdr.rcode == SERVFAIL) {
1462 /* try next search element, if any */
1468 /* anything else implies that we're done */
1472 /* if we got here for some reason other than DNSRCH,
1473 * we only wanted one iteration of the loop, so stop.
1475 if (!(_res.options & RES_DNSRCH))
1481 * If we have not already tried the name "as is", do that now.
1482 * note that we do this regardless of how many dots were in the
1483 * name or whether it ends with a dot unless NOTLDQUERY is set.
1485 if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) {
1486 for(rtl = rtl0; rtl != NULL;
1487 rtl = SLIST_NEXT(rtl, rtl_entry)) {
1488 ret = res_querydomain(name, NULL, C_IN, rtl->rtl_type,
1489 buf->buf, sizeof(buf->buf));
1490 if (ret > 0 && ret < sizeof(buf->buf)) {
1491 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1492 ? AF_INET6 : AF_INET;
1493 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1494 hp = getanswer(buf, ret, name, rtl->rtl_type,
1498 hp = _hpcopy(&hpbuf, errp);
1499 hp0 = _hpmerge(hp0, hp, errp);
1510 /* if we got here, we didn't satisfy the search.
1511 * if we did an initial full query, return that query's h_errno
1512 * (note that we wouldn't be here if that query had succeeded).
1513 * else if we ever got a nodata, send that back as the reason.
1514 * else send back meaningless h_errno, that being the one from
1515 * the last DNSRCH we did.
1517 if (saved_herrno != -1)
1518 *errp = saved_herrno;
1519 else if (got_nodata)
1521 else if (got_servfail)
1526 static struct hostent *
1527 _dns_ghbyname(const char *name, int af, int *errp)
1529 struct __res_type_list *rtl, rtl4;
1531 struct __res_type_list rtl6;
1537 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1538 SLIST_NEXT(&rtl6, rtl_entry) = &rtl4; rtl6.rtl_type = T_AAAA;
1542 SLIST_NEXT(&rtl6, rtl_entry) = NULL; rtl6.rtl_type = T_AAAA;
1546 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1551 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1554 return(_res_search_multi(name, rtl, errp));
1557 static struct hostent *
1558 _dns_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
1564 struct hostent hbuf;
1567 static const char hex[] = "0123456789abcdef";
1570 char qbuf[MAXDNAME+1];
1572 char *tld6[] = { "ip6.arpa", "ip6.int", NULL };
1573 char *tld4[] = { "in-addr.arpa", NULL };
1578 if (af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr))
1595 if ((_res.options & RES_INIT) == 0) {
1596 if (res_init() < 0) {
1601 memset(&hbuf, 0, sizeof(hbuf));
1603 hbuf.h_addrtype = af;
1604 hbuf.h_length = addrlen;
1607 buf = malloc(sizeof(*buf));
1609 *errp = NETDB_INTERNAL;
1612 for (/* nothing */; *tld; tld++) {
1614 * XXX assumes that MAXDNAME is big enough - error checks
1615 * has been made by callers
1619 cp = (u_char *)addr+addrlen-1;
1623 for (; n < addrlen; n++, cp--) {
1625 *bp++ = hex[c & 0xf];
1627 *bp++ = hex[c >> 4];
1634 for (; n < addrlen; n++, cp--) {
1637 *bp++ = '0' + c / 100;
1639 *bp++ = '0' + (c % 100) / 10;
1640 *bp++ = '0' + c % 10;
1647 n = res_query(qbuf, C_IN, T_PTR, buf->buf, sizeof buf->buf);
1651 } else if (n > sizeof(buf->buf)) {
1652 *errp = NETDB_INTERNAL;
1654 errno = ERANGE; /* XXX is it OK to set errno here? */
1658 hp = getanswer(buf, n, qbuf, T_PTR, &hbuf, errp);
1662 hbuf.h_addrtype = af;
1663 hbuf.h_length = addrlen;
1664 hbuf.h_addr_list = hlist;
1665 hlist[0] = (char *)addr;
1667 return _hpcopy(&hbuf, errp);
1674 _dns_shent(int stayopen)
1676 if ((_res.options & RES_INIT) == 0) {
1681 _res.options |= RES_STAYOPEN | RES_USEVC;
1687 _res.options &= ~(RES_STAYOPEN | RES_USEVC);
1695 * draft-ietf-ipngwg-icmp-namelookups-02.txt
1696 * ifindex is assumed to be encoded in addr.
1698 #include <sys/uio.h>
1699 #include <netinet/ip6.h>
1700 #include <netinet/icmp6.h>
1702 struct _icmp_host_cache {
1703 struct _icmp_host_cache *hc_next;
1705 struct in6_addr hc_addr;
1710 _icmp_fqdn_query(const struct in6_addr *addr, int ifindex)
1713 struct icmp6_filter filter;
1715 struct cmsghdr *cmsg;
1716 struct in6_pktinfo *pkt;
1720 struct icmp6_fqdn_query *fq;
1721 struct icmp6_fqdn_reply *fr;
1722 struct _icmp_host_cache *hc;
1723 struct sockaddr_in6 sin6;
1726 struct timeval tout;
1730 static struct _icmp_host_cache *hc_head;
1732 for (hc = hc_head; hc; hc = hc->hc_next) {
1733 if (hc->hc_ifindex == ifindex
1734 && IN6_ARE_ADDR_EQUAL(&hc->hc_addr, addr))
1741 ICMP6_FILTER_SETBLOCKALL(&filter);
1742 ICMP6_FILTER_SETPASS(ICMP6_FQDN_REPLY, &filter);
1746 tout.tv_usec = 200000; /*XXX: 200ms*/
1748 fq = (struct icmp6_fqdn_query *)buf;
1749 fq->icmp6_fqdn_type = ICMP6_FQDN_QUERY;
1750 fq->icmp6_fqdn_code = 0;
1751 fq->icmp6_fqdn_cksum = 0;
1752 fq->icmp6_fqdn_id = (u_short)pid;
1753 fq->icmp6_fqdn_unused = 0;
1754 fq->icmp6_fqdn_cookie[0] = 0;
1755 fq->icmp6_fqdn_cookie[1] = 0;
1757 memset(&sin6, 0, sizeof(sin6));
1758 sin6.sin6_family = AF_INET6;
1759 sin6.sin6_addr = *addr;
1761 memset(&msg, 0, sizeof(msg));
1762 msg.msg_name = (caddr_t)&sin6;
1763 msg.msg_namelen = sizeof(sin6);
1766 msg.msg_control = NULL;
1767 msg.msg_controllen = 0;
1768 iov.iov_base = (caddr_t)buf;
1769 iov.iov_len = sizeof(struct icmp6_fqdn_query);
1772 msg.msg_control = cbuf;
1773 msg.msg_controllen = sizeof(cbuf);
1774 cmsg = CMSG_FIRSTHDR(&msg);
1775 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1776 cmsg->cmsg_level = IPPROTO_IPV6;
1777 cmsg->cmsg_type = IPV6_PKTINFO;
1778 pkt = (struct in6_pktinfo *)&cmsg[1];
1779 memset(&pkt->ipi6_addr, 0, sizeof(struct in6_addr));
1780 pkt->ipi6_ifindex = ifindex;
1781 cmsg = CMSG_NXTHDR(&msg, cmsg);
1782 msg.msg_controllen = (char *)cmsg - cbuf;
1785 if ((s = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
1787 (void)setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER,
1788 (char *)&filter, sizeof(filter));
1789 cc = sendmsg(s, &msg, 0);
1797 if (select(s + 1, &fds, NULL, NULL, &tout) <= 0) {
1802 cc = recvfrom(s, buf, sizeof(buf), 0,
1803 (struct sockaddr *)&sin6, &len);
1808 if (cc < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr))
1810 if (!IN6_ARE_ADDR_EQUAL(addr, &sin6.sin6_addr))
1812 fr = (struct icmp6_fqdn_reply *)(buf + sizeof(struct ip6_hdr));
1813 if (fr->icmp6_fqdn_type == ICMP6_FQDN_REPLY)
1817 if (fr->icmp6_fqdn_cookie[1] != 0) {
1819 name = buf + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 4;
1820 len = (buf + cc) - name;
1822 len = fr->icmp6_fqdn_namelen;
1823 name = fr->icmp6_fqdn_name;
1829 if ((hc = (struct _icmp_host_cache *)malloc(sizeof(*hc))) == NULL)
1831 /* XXX: limit number of cached entries */
1832 hc->hc_ifindex = ifindex;
1833 hc->hc_addr = *addr;
1834 hc->hc_name = strdup(name);
1835 hc->hc_next = hc_head;
1840 static struct hostent *
1841 _icmp_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
1845 struct in6_addr addr6;
1847 if (af != AF_INET6) {
1849 * Note: rfc1788 defines Who Are You for IPv4,
1850 * but no one implements it.
1855 memcpy(&addr6, addr, addrlen);
1856 ifindex = (addr6.s6_addr[2] << 8) | addr6.s6_addr[3];
1857 addr6.s6_addr[2] = addr6.s6_addr[3] = 0;
1859 if (!IN6_IS_ADDR_LINKLOCAL(&addr6))
1860 return NULL; /*XXX*/
1862 if ((hname = _icmp_fqdn_query(&addr6, ifindex)) == NULL)
1864 return _hpaddr(af, hname, &addr6, errp);