1 /* $FreeBSD: src/lib/libc/net/name6.c,v 1.6.2.9 2002/11/02 18:54:57 ume Exp $ */
2 /* $DragonFly: src/lib/libc/net/name6.c,v 1.3 2003/11/12 20:21:24 eirikn Exp $ */
3 /* $KAME: name6.c,v 1.25 2000/06/26 16:44:40 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * ++Copyright++ 1985, 1988, 1993
36 * Copyright (c) 1985, 1988, 1993
37 * The Regents of the University of California. All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
69 * Permission to use, copy, modify, and distribute this software for any
70 * purpose with or without fee is hereby granted, provided that the above
71 * copyright notice and this permission notice appear in all copies, and that
72 * the name of Digital Equipment Corporation not be used in advertising or
73 * publicity pertaining to distribution of the document or software without
74 * specific, written prior permission.
76 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
77 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
78 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
79 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
80 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
81 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
82 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
89 * Atsushi Onoe <onoe@sm.sony.co.jp>
93 * TODO for thread safe
94 * use mutex for _hostconf, _hostconf_init.
95 * rewrite resolvers to be thread safe
98 #include <sys/param.h>
99 #include <sys/socket.h>
100 #include <sys/time.h>
101 #include <sys/queue.h>
102 #include <netinet/in.h>
104 #include <arpa/inet.h>
105 #include <arpa/nameser.h>
116 #define _PATH_HOSTS "/etc/hosts"
120 #define MAXALIASES 10
126 #define MAXDNAME 1025
130 #define ADDRLEN(af) ((af) == AF_INET6 ? sizeof(struct in6_addr) : \
131 sizeof(struct in_addr))
133 #define ADDRLEN(af) sizeof(struct in_addr)
136 #define MAPADDR(ab, ina) \
138 memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr)); \
139 memset((ab)->map_zero, 0, sizeof((ab)->map_zero)); \
140 memset((ab)->map_one, 0xff, sizeof((ab)->map_one)); \
142 #define MAPADDRENABLED(flags) \
143 (((flags) & AI_V4MAPPED) || \
144 (((flags) & AI_V4MAPPED_CFG) && _mapped_addr_enabled()))
147 struct in_addr in_addr;
149 struct in6_addr in6_addr;
154 struct in_addr mau_inaddr;
156 #define map_zero map_addr_un.mau_zero
157 #define map_one map_addr_un.mau_one
158 #define map_inaddr map_addr_un.mau_inaddr
161 static struct hostent *_hpcopy(struct hostent *hp, int *errp);
162 static struct hostent *_hpaddr(int af, const char *name, void *addr, int *errp);
163 static struct hostent *_hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp);
165 static struct hostent *_hpmapv6(struct hostent *hp, int *errp);
167 static struct hostent *_hpsort(struct hostent *hp);
168 static struct hostent *_ghbyname(const char *name, int af, int flags, int *errp);
169 static char *_hgetword(char **pp);
170 static int _mapped_addr_enabled(void);
172 static FILE *_files_open(int *errp);
173 static struct hostent *_files_ghbyname(const char *name, int af, int *errp);
174 static struct hostent *_files_ghbyaddr(const void *addr, int addrlen, int af, int *errp);
175 static void _files_shent(int stayopen);
176 static void _files_ehent(void);
178 static struct hostent *_nis_ghbyname(const char *name, int af, int *errp);
179 static struct hostent *_nis_ghbyaddr(const void *addr, int addrlen, int af, int *errp);
181 static struct hostent *_dns_ghbyname(const char *name, int af, int *errp);
182 static struct hostent *_dns_ghbyaddr(const void *addr, int addrlen, int af, int *errp);
183 static void _dns_shent(int stayopen);
184 static void _dns_ehent(void);
186 static struct hostent *_icmp_ghbyaddr(const void *addr, int addrlen, int af, int *errp);
189 /* Make getipnodeby*() thread-safe in libc for use with kernel threads. */
190 #include "libc_private.h"
191 #include "spinlock.h"
193 * XXX: Our res_*() is not thread-safe. So, we share lock between
194 * getaddrinfo() and getipnodeby*(). Still, we cannot use
195 * getaddrinfo() and getipnodeby*() in conjunction with other
196 * functions which call res_*().
198 extern spinlock_t __getaddrinfo_thread_lock;
199 #define THREAD_LOCK() \
200 if (__isthreaded) _SPINLOCK(&__getaddrinfo_thread_lock);
201 #define THREAD_UNLOCK() \
202 if (__isthreaded) _SPINUNLOCK(&__getaddrinfo_thread_lock);
205 * Select order host function.
207 #define MAXHOSTCONF 4
210 # define HOSTCONF "/etc/host.conf"
211 #endif /* !HOSTCONF */
214 struct hostent *(*byname)(const char *name, int af, int *errp);
215 struct hostent *(*byaddr)(const void *addr, int addrlen, int af, int *errp);
219 static struct _hostconf _hostconf[MAXHOSTCONF] = {
220 { _dns_ghbyname, _dns_ghbyaddr },
221 { _files_ghbyname, _files_ghbyaddr },
223 { NULL, _icmp_ghbyaddr },
227 static int _hostconf_init_done;
228 static void _hostconf_init(void);
231 * Initialize hostconf structure.
242 _hostconf_init_done = 1;
245 if ((fp = fopen(p, "r")) == NULL)
247 while (n < MAXHOSTCONF && fgets(buf, sizeof(buf), fp)) {
249 if ((p = _hgetword(&line)) == NULL)
252 if (strcmp(p, "hosts") == 0
253 || strcmp(p, "local") == 0
254 || strcmp(p, "file") == 0
255 || strcmp(p, "files") == 0) {
256 _hostconf[n].byname = _files_ghbyname;
257 _hostconf[n].byaddr = _files_ghbyaddr;
260 else if (strcmp(p, "dns") == 0
261 || strcmp(p, "bind") == 0) {
262 _hostconf[n].byname = _dns_ghbyname;
263 _hostconf[n].byaddr = _dns_ghbyaddr;
267 else if (strcmp(p, "nis") == 0) {
268 _hostconf[n].byname = _nis_ghbyname;
269 _hostconf[n].byaddr = _nis_ghbyaddr;
274 else if (strcmp(p, "icmp") == 0) {
275 _hostconf[n].byname = NULL;
276 _hostconf[n].byaddr = _icmp_ghbyaddr;
280 } while ((p = _hgetword(&line)) != NULL);
284 /* no keyword found. do not change default configuration */
287 for (; n < MAXHOSTCONF; n++) {
288 _hostconf[n].byname = NULL;
289 _hostconf[n].byaddr = NULL;
294 * Check if kernel supports mapped address.
295 * implementation dependent
298 #include <sys/sysctl.h>
299 #endif /* __KAME__ */
302 _mapped_addr_enabled(void)
304 /* implementation dependent check */
305 #if defined(__KAME__) && defined(IPV6CTL_MAPPED_ADDR)
312 mib[2] = IPPROTO_IPV6;
313 mib[3] = IPV6CTL_MAPPED_ADDR;
315 if (sysctl(mib, 4, &val, &len, 0, 0) == 0 && val != 0)
317 #endif /* __KAME__ && IPV6CTL_MAPPED_ADDR */
322 * Functions defined in RFC2553
323 * getipnodebyname, getipnodebyaddr, freehostent
326 static struct hostent *
327 _ghbyname(const char *name, int af, int flags, int *errp)
332 if (flags & AI_ADDRCONFIG) {
337 * Note that implementation dependent test for address
338 * configuration should be done everytime called
339 * (or apropriate interval),
340 * because addresses will be dynamically assigned or deleted.
342 if (af == AF_UNSPEC) {
343 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
347 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
354 if (af != AF_UNSPEC) {
355 if ((s = socket(af, SOCK_DGRAM, 0)) < 0)
362 for (i = 0; i < MAXHOSTCONF; i++) {
363 if (_hostconf[i].byname
364 && (hp = (*_hostconf[i].byname)(name, af, errp)) != NULL) {
374 /* getipnodebyname() internal routine for multiple query(PF_UNSPEC) support. */
376 _getipnodebyname_multi(const char *name, int af, int flags, int *errp)
379 union inx_addr addrbuf;
381 /* XXX: PF_UNSPEC is only supposed to be passed from getaddrinfo() */
394 /* special case for literal address */
395 if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
396 if (af != AF_INET6) {
397 *errp = HOST_NOT_FOUND;
400 return _hpaddr(af, name, &addrbuf, errp);
403 if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) {
405 if (MAPADDRENABLED(flags)) {
406 MAPADDR(&addrbuf, &addrbuf.in_addr);
408 *errp = HOST_NOT_FOUND;
412 return _hpaddr(af, name, &addrbuf, errp);
415 if (!_hostconf_init_done)
418 *errp = HOST_NOT_FOUND;
419 hp = _ghbyname(name, af, flags, errp);
423 && ((flags & AI_ALL) || hp == NULL)
424 && (MAPADDRENABLED(flags))) {
425 struct hostent *hp2 = _ghbyname(name, AF_INET, flags, errp);
427 hp = _hpmapv6(hp2, errp);
429 if (hp2 && strcmp(hp->h_name, hp2->h_name) != 0) {
433 hp = _hpmerge(hp, hp2, errp);
441 getipnodebyname(const char *name, int af, int flags, int *errp)
452 return(_getipnodebyname_multi(name, af ,flags, errp));
456 getipnodebyaddr(const void *src, size_t len, int af, int *errp)
461 struct in6_addr addrbuf;
463 struct in_addr addrbuf;
466 *errp = HOST_NOT_FOUND;
470 if (len != sizeof(struct in_addr)) {
474 if ((long)src & ~(sizeof(struct in_addr) - 1)) {
475 memcpy(&addrbuf, src, len);
478 if (((struct in_addr *)src)->s_addr == 0)
483 if (len != sizeof(struct in6_addr)) {
487 if ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) { /*XXX*/
488 memcpy(&addrbuf, src, len);
491 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src))
493 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)
494 || IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) {
496 (sizeof(struct in6_addr) - sizeof(struct in_addr));
498 len = sizeof(struct in_addr);
507 if (!_hostconf_init_done)
510 for (i = 0; i < MAXHOSTCONF; i++) {
511 if (_hostconf[i].byaddr
512 && (hp = (*_hostconf[i].byaddr)(src, len, af, errp)) != NULL) {
523 freehostent(struct hostent *ptr)
530 /* XXX: should be deprecated */
532 getnodebyname(const char *name, int af, int flags)
534 return getipnodebyname(name, af, flags, &h_errno);
537 #ifdef __warn_references
538 __warn_references(getnodebyname,
539 "warning: getnodebyname() deprecated, "
540 "should use getaddrinfo() or getipnodebyname()");
544 getnodebyaddr(const void *src, size_t len, int af)
546 return getipnodebyaddr(src, len, af, &h_errno);
549 #ifdef __warn_references
550 __warn_references(getnodebyaddr,
551 "warning: getnodebyaddr() deprecated, "
552 "should use getnameinfo() or getipnodebyaddr()");
558 * Private utility functions
562 * _hpcopy: allocate and copy hostent structure
564 static struct hostent *
565 _hpcopy(struct hostent *hp, int *errp)
570 int nalias = 0, naddr = 0;
577 /* count size to be allocated */
578 size = sizeof(struct hostent);
579 if (hp->h_name != NULL)
580 size += strlen(hp->h_name) + 1;
581 if ((pp = hp->h_aliases) != NULL) {
582 for (i = 0; *pp != NULL; i++, pp++) {
584 size += strlen(*pp) + 1;
589 /* adjust alignment */
592 size += sizeof(char *) * (nalias + 1);
593 addrsize = ALIGN(hp->h_length);
594 if ((pp = hp->h_addr_list) != NULL) {
595 while (*pp++ != NULL)
598 size += addrsize * naddr;
599 size += sizeof(char *) * (naddr + 1);
602 if ((nhp = (struct hostent *)malloc(size)) == NULL) {
606 cp = (char *)&nhp[1];
607 if (hp->h_name != NULL) {
609 strcpy(cp, hp->h_name);
610 cp += strlen(cp) + 1;
613 nhp->h_aliases = (char **)((char *)nhp + al_off);
614 if ((pp = hp->h_aliases) != NULL) {
615 for (i = 0; *pp != NULL; pp++) {
617 nhp->h_aliases[i++] = cp;
619 cp += strlen(cp) + 1;
623 nhp->h_aliases[nalias] = NULL;
624 cp = (char *)&nhp->h_aliases[nalias + 1];
625 nhp->h_addrtype = hp->h_addrtype;
626 nhp->h_length = hp->h_length;
627 nhp->h_addr_list = (char **)cp;
628 if ((pp = hp->h_addr_list) != NULL) {
629 cp = (char *)&nhp->h_addr_list[naddr + 1];
630 for (i = 0; *pp != NULL; pp++) {
631 nhp->h_addr_list[i++] = cp;
632 memcpy(cp, *pp, hp->h_length);
636 nhp->h_addr_list[naddr] = NULL;
641 * _hpaddr: construct hostent structure with one address
643 static struct hostent *
644 _hpaddr(int af, const char *name, void *addr, int *errp)
646 struct hostent *hp, hpbuf;
650 hp->h_name = (char *)name;
651 hp->h_aliases = NULL;
653 hp->h_length = ADDRLEN(af);
654 hp->h_addr_list = addrs;
655 addrs[0] = (char *)addr;
657 return _hpcopy(hp, errp);
661 * _hpmerge: merge 2 hostent structure, arguments will be freed
663 static struct hostent *
664 _hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp)
669 struct hostent *hp, hpbuf;
670 char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1];
671 union inx_addr addrbuf[MAXADDRS];
678 #define HP(i) (i == 1 ? hp1 : hp2)
680 hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name);
681 hp->h_aliases = aliases;
683 for (i = 1; i <= 2; i++) {
684 if ((pp = HP(i)->h_aliases) == NULL)
686 for (; nalias < MAXALIASES && *pp != NULL; pp++) {
687 /* check duplicates */
688 for (j = 0; j < nalias; j++)
689 if (strcasecmp(*pp, aliases[j]) == 0)
692 aliases[nalias++] = *pp;
695 aliases[nalias] = NULL;
697 if (hp1->h_length != hp2->h_length) {
698 hp->h_addrtype = AF_INET6;
699 hp->h_length = sizeof(struct in6_addr);
702 hp->h_addrtype = hp1->h_addrtype;
703 hp->h_length = hp1->h_length;
707 hp->h_addr_list = addrs;
709 for (i = 1; i <= 2; i++) {
710 if ((pp = HP(i)->h_addr_list) == NULL)
712 if (HP(i)->h_length == hp->h_length) {
713 while (naddr < MAXADDRS && *pp != NULL)
714 addrs[naddr++] = *pp++;
716 /* copy IPv4 addr as mapped IPv6 addr */
717 while (naddr < MAXADDRS && *pp != NULL) {
718 MAPADDR(&addrbuf[naddr], *pp++);
719 addrs[naddr] = (char *)&addrbuf[naddr];
725 hp = _hpcopy(hp, errp);
732 * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses
735 static struct hostent *
736 _hpmapv6(struct hostent *hp, int *errp)
742 if (hp->h_addrtype == AF_INET6)
745 /* make dummy hostent to convert IPv6 address */
746 if ((hp6 = (struct hostent *)malloc(sizeof(struct hostent))) == NULL) {
751 hp6->h_aliases = NULL;
752 hp6->h_addrtype = AF_INET6;
753 hp6->h_length = sizeof(struct in6_addr);
754 hp6->h_addr_list = NULL;
755 return _hpmerge(hp6, hp, errp);
760 * _hpsort: sort address by sortlist
762 static struct hostent *
763 _hpsort(struct hostent *hp)
766 u_char *ap, *sp, *mp, **pp;
768 char order[MAXADDRS];
769 int nsort = _res.nsort;
771 if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0)
773 for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
774 for (j = 0; j < nsort; j++) {
776 if (_res_ext.sort_list[j].af != hp->h_addrtype)
778 sp = (u_char *)&_res_ext.sort_list[j].addr;
779 mp = (u_char *)&_res_ext.sort_list[j].mask;
781 sp = (u_char *)&_res.sort_list[j].addr;
782 mp = (u_char *)&_res.sort_list[j].mask;
784 for (n = 0; n < hp->h_length; n++) {
785 if ((ap[n] & mp[n]) != sp[n])
788 if (n == hp->h_length)
794 pp = (u_char **)hp->h_addr_list;
795 for (i = 0; i < n - 1; i++) {
796 for (j = i + 1; j < n; j++) {
797 if (order[i] > order[j]) {
815 static const char sep[] = "# \t\n";
818 for (p = *pp; (c = *p) != '\0'; p++) {
819 for (sp = sep; *sp != '\0'; sp++) {
824 p[1] = '\0'; /* ignore rest of line */
836 if (ret == NULL || *ret == '\0')
846 _files_open(int *errp)
849 fp = fopen(_PATH_HOSTS, "r");
855 static struct hostent *
856 _files_ghbyname(const char *name, int af, int *errp)
859 char *p, *line, *addrstr, *cname;
861 struct hostent *rethp, *hp, hpbuf;
862 char *aliases[MAXALIASES + 1], *addrs[2];
863 union inx_addr addrbuf;
867 if ((fp = _files_open(errp)) == NULL)
871 while (fgets(buf, sizeof(buf), fp)) {
873 if ((addrstr = _hgetword(&line)) == NULL
874 || (cname = _hgetword(&line)) == NULL)
876 match = (strcasecmp(cname, name) == 0);
878 while ((p = _hgetword(&line)) != NULL) {
880 match = (strcasecmp(p, name) == 0);
881 if (nalias < MAXALIASES)
882 aliases[nalias++] = p;
888 if (inet_aton(addrstr, (struct in_addr *)&addrbuf)
890 *errp = NO_DATA; /* name found */
897 if (inet_pton(af, addrstr, &addrbuf) != 1) {
898 *errp = NO_DATA; /* name found */
905 if (inet_aton(addrstr, (struct in_addr *)&addrbuf)
911 if (inet_pton(AF_INET6, addrstr, &addrbuf) == 1) {
916 *errp = NO_DATA; /* name found */
922 hp->h_aliases = aliases;
923 aliases[nalias] = NULL;
925 hp->h_length = ADDRLEN(af);
926 hp->h_addr_list = addrs;
927 addrs[0] = (char *)&addrbuf;
929 hp = _hpcopy(hp, errp);
930 rethp = _hpmerge(rethp, hp, errp);
936 static struct hostent *
937 _files_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
942 struct hostent *hp, hpbuf;
943 char *aliases[MAXALIASES + 1], *addrs[2];
944 union inx_addr addrbuf;
947 if ((fp = _files_open(errp)) == NULL)
950 while (fgets(buf, sizeof(buf), fp)) {
952 if ((p = _hgetword(&line)) == NULL
954 ? inet_aton(p, (struct in_addr *)&addrbuf)
955 : inet_pton(af, p, &addrbuf)) != 1
956 || memcmp(addr, &addrbuf, addrlen) != 0
957 || (p = _hgetword(&line)) == NULL)
961 hp->h_aliases = aliases;
963 while ((p = _hgetword(&line)) != NULL) {
964 if (nalias < MAXALIASES)
965 aliases[nalias++] = p;
967 aliases[nalias] = NULL;
969 hp->h_length = addrlen;
970 hp->h_addr_list = addrs;
971 addrs[0] = (char *)&addrbuf;
973 hp = _hpcopy(hp, errp);
984 * XXX actually a hack, these are INET4 specific.
986 static struct hostent *
987 _nis_ghbyname(const char *name, int af, int *errp)
989 struct hostent *hp = NULL;
994 hp = _gethostbynisname(name, af);
996 hp = _hpcopy(hp, errp);
1002 static struct hostent *
1003 _nis_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
1005 struct hostent *hp = NULL;
1007 if (af == AF_INET) {
1008 hp = _gethostbynisaddr(addr, addrlen, af);
1010 hp = _hpcopy(hp, errp);
1016 struct __res_type_list {
1017 SLIST_ENTRY(__res_type_list) rtl_entry;
1021 #define MAXPACKET (64*1024)
1025 u_char buf[MAXPACKET];
1028 static struct hostent *getanswer (const querybuf *, int, const char *,
1029 int, struct hostent *, int *);
1032 * we don't need to take care about sorting, nor IPv4 mapped address here.
1034 static struct hostent *
1035 getanswer(answer, anslen, qname, qtype, template, errp)
1036 const querybuf *answer;
1040 struct hostent *template;
1043 register const HEADER *hp;
1044 register const u_char *cp;
1046 const u_char *eom, *erdata;
1047 char *bp, **ap, **hap, *obp;
1048 int type, class, buflen, ancount, qdcount;
1049 int haveanswer, had_error;
1050 char tbuf[MAXDNAME];
1052 int (*name_ok) (const char *);
1053 static char *h_addr_ptrs[MAXADDRS + 1];
1054 static char *host_aliases[MAXALIASES];
1055 static char hostbuf[8*1024];
1057 #define BOUNDED_INCR(x) \
1061 *errp = NO_RECOVERY; \
1066 #define BOUNDS_CHECK(ptr, count) \
1068 if ((ptr) + (count) > eom) { \
1069 *errp = NO_RECOVERY; \
1074 /* XXX do {} while (0) cannot be put here */
1075 #define DNS_ASSERT(x) \
1083 /* XXX do {} while (0) cannot be put here */
1084 #define DNS_FATAL(x) \
1093 template->h_name = NULL;
1094 eom = answer->buf + anslen;
1104 return (NULL); /* XXX should be abort(); */
1107 * find first satisfactory answer
1110 ancount = ntohs(hp->ancount);
1111 qdcount = ntohs(hp->qdcount);
1113 buflen = sizeof hostbuf;
1115 BOUNDED_INCR(HFIXEDSZ);
1117 *errp = NO_RECOVERY;
1120 n = dn_expand(answer->buf, eom, cp, bp, buflen);
1121 if ((n < 0) || !(*name_ok)(bp)) {
1122 *errp = NO_RECOVERY;
1125 BOUNDED_INCR(n + QFIXEDSZ);
1126 if (qtype == T_A || qtype == T_AAAA) {
1127 /* res_send() has already verified that the query name is the
1128 * same as the one we sent; this just gets the expanded name
1129 * (i.e., with the succeeding search-domain tacked on).
1131 n = strlen(bp) + 1; /* for the \0 */
1132 if (n >= MAXHOSTNAMELEN) {
1133 *errp = NO_RECOVERY;
1136 template->h_name = bp;
1139 /* The qname can be abbreviated, but h_name is now absolute. */
1140 qname = template->h_name;
1144 template->h_aliases = host_aliases;
1147 template->h_addr_list = h_addr_ptrs;
1150 while (ancount-- > 0 && cp < eom && !had_error) {
1151 n = dn_expand(answer->buf, eom, cp, bp, buflen);
1153 DNS_FATAL((*name_ok)(bp));
1155 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
1156 type = _getshort(cp);
1157 cp += INT16SZ; /* type */
1158 class = _getshort(cp);
1159 cp += INT16SZ + INT32SZ; /* class, TTL */
1161 cp += INT16SZ; /* len */
1162 BOUNDS_CHECK(cp, n);
1164 DNS_ASSERT(class == C_IN);
1165 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
1166 if (ap >= &host_aliases[MAXALIASES-1])
1168 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1170 DNS_FATAL((*name_ok)(tbuf));
1173 *errp = NO_RECOVERY;
1178 n = strlen(bp) + 1; /* for the \0 */
1179 DNS_FATAL(n < MAXHOSTNAMELEN);
1182 /* Get canonical name. */
1183 n = strlen(tbuf) + 1; /* for the \0 */
1184 DNS_FATAL(n <= buflen);
1185 DNS_FATAL(n < MAXHOSTNAMELEN);
1187 template->h_name = bp;
1192 if (qtype == T_PTR && type == T_CNAME) {
1193 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1194 if (n < 0 || !res_dnok(tbuf)) {
1200 *errp = NO_RECOVERY;
1203 /* Get canonical name. */
1204 n = strlen(tbuf) + 1; /* for the \0 */
1205 if (n > buflen || n >= MAXHOSTNAMELEN) {
1215 DNS_ASSERT(type == qtype);
1218 DNS_ASSERT(strcasecmp(tname, bp) == 0);
1219 n = dn_expand(answer->buf, eom, cp, bp, buflen);
1221 DNS_FATAL(res_hnok(bp));
1222 #if MULTI_PTRS_ARE_ALIASES
1225 *errp = NO_RECOVERY;
1229 template->h_name = bp;
1230 else if (ap < &host_aliases[MAXALIASES-1])
1235 n = strlen(bp) + 1; /* for the \0 */
1236 if (n >= MAXHOSTNAMELEN) {
1245 template->h_name = bp;
1246 *errp = NETDB_SUCCESS;
1251 DNS_ASSERT(strcasecmp(template->h_name, bp) == 0);
1252 DNS_ASSERT(n == template->h_length);
1256 template->h_name = bp;
1257 nn = strlen(bp) + 1; /* for the \0 */
1261 obp = bp; /* ALIGN rounds up */
1262 bp = (char *)ALIGN(bp);
1263 buflen -= (bp - obp);
1265 DNS_FATAL(bp + n < &hostbuf[sizeof hostbuf]);
1266 DNS_ASSERT(hap < &h_addr_ptrs[MAXADDRS-1]);
1267 #ifdef FILTER_V4MAPPED
1268 if (type == T_AAAA) {
1269 struct in6_addr in6;
1270 memcpy(&in6, cp, sizeof(in6));
1271 DNS_ASSERT(IN6_IS_ADDR_V4MAPPED(&in6) == 0);
1274 bcopy(cp, *hap++ = bp, n);
1279 *errp = NO_RECOVERY;
1292 if (!template->h_name) {
1293 n = strlen(qname) + 1; /* for the \0 */
1294 if (n > buflen || n >= MAXHOSTNAMELEN)
1297 template->h_name = bp;
1301 *errp = NETDB_SUCCESS;
1305 *errp = NO_RECOVERY;
1314 /* res_search() variant with multiple query support. */
1315 static struct hostent *
1316 _res_search_multi(name, rtl, errp)
1317 const char *name; /* domain name */
1318 struct __res_type_list *rtl; /* list of query types */
1321 const char *cp, * const *domain;
1322 struct hostent *hp0 = NULL, *hp;
1323 struct hostent hpbuf;
1325 int trailing_dot, ret, saved_herrno;
1326 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
1327 struct __res_type_list *rtl0 = rtl;
1330 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
1331 *errp = NETDB_INTERNAL;
1335 for (cp = name; *cp; cp++)
1336 dots += (*cp == '.');
1338 if (cp > name && *--cp == '.')
1341 buf = malloc(sizeof(*buf));
1343 *errp = NETDB_INTERNAL;
1347 /* If there aren't any dots, it could be a user-level alias */
1348 if (!dots && (cp = hostalias(name)) != NULL) {
1349 for(rtl = rtl0; rtl != NULL;
1350 rtl = SLIST_NEXT(rtl, rtl_entry)) {
1351 ret = res_query(cp, C_IN, rtl->rtl_type, buf->buf,
1353 if (ret > 0 && ret < sizeof(buf->buf)) {
1354 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1355 ? AF_INET6 : AF_INET;
1356 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1357 hp = getanswer(buf, ret, name, rtl->rtl_type,
1361 hp = _hpcopy(&hpbuf, errp);
1362 hp0 = _hpmerge(hp0, hp, errp);
1370 * If there are dots in the name already, let's just give it a try
1371 * 'as is'. The threshold can be set with the "ndots" option.
1374 if (dots >= _res.ndots) {
1375 for(rtl = rtl0; rtl != NULL;
1376 rtl = SLIST_NEXT(rtl, rtl_entry)) {
1377 ret = res_querydomain(name, NULL, C_IN, rtl->rtl_type,
1378 buf->buf, sizeof(buf->buf));
1379 if (ret > 0 && ret < sizeof(buf->buf)) {
1380 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1381 ? AF_INET6 : AF_INET;
1382 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1383 hp = getanswer(buf, ret, name, rtl->rtl_type,
1387 hp = _hpcopy(&hpbuf, errp);
1388 hp0 = _hpmerge(hp0, hp, errp);
1395 saved_herrno = *errp;
1400 * We do at least one level of search if
1401 * - there is no dot and RES_DEFNAME is set, or
1402 * - there is at least one dot, there is no trailing dot,
1403 * and RES_DNSRCH is set.
1405 if ((!dots && (_res.options & RES_DEFNAMES)) ||
1406 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
1409 for (domain = (const char * const *)_res.dnsrch;
1413 for(rtl = rtl0; rtl != NULL;
1414 rtl = SLIST_NEXT(rtl, rtl_entry)) {
1415 ret = res_querydomain(name, *domain, C_IN,
1417 buf->buf, sizeof(buf->buf));
1418 if (ret > 0 && ret < sizeof(buf->buf)) {
1419 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1420 ? AF_INET6 : AF_INET;
1421 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1422 hp = getanswer(buf, ret, name,
1423 rtl->rtl_type, &hpbuf, errp);
1426 hp = _hpcopy(&hpbuf, errp);
1427 hp0 = _hpmerge(hp0, hp, errp);
1436 * If no server present, give up.
1437 * If name isn't found in this domain,
1438 * keep trying higher domains in the search list
1439 * (if that's enabled).
1440 * On a NO_DATA error, keep trying, otherwise
1441 * a wildcard entry of another type could keep us
1442 * from finding this entry higher in the domain.
1443 * If we get some other error (negative answer or
1444 * server failure), then stop searching up,
1445 * but try the input name below in case it's
1448 if (errno == ECONNREFUSED) {
1458 case HOST_NOT_FOUND:
1462 if (buf->hdr.rcode == SERVFAIL) {
1463 /* try next search element, if any */
1469 /* anything else implies that we're done */
1473 /* if we got here for some reason other than DNSRCH,
1474 * we only wanted one iteration of the loop, so stop.
1476 if (!(_res.options & RES_DNSRCH))
1482 * If we have not already tried the name "as is", do that now.
1483 * note that we do this regardless of how many dots were in the
1484 * name or whether it ends with a dot unless NOTLDQUERY is set.
1486 if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) {
1487 for(rtl = rtl0; rtl != NULL;
1488 rtl = SLIST_NEXT(rtl, rtl_entry)) {
1489 ret = res_querydomain(name, NULL, C_IN, rtl->rtl_type,
1490 buf->buf, sizeof(buf->buf));
1491 if (ret > 0 && ret < sizeof(buf->buf)) {
1492 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1493 ? AF_INET6 : AF_INET;
1494 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1495 hp = getanswer(buf, ret, name, rtl->rtl_type,
1499 hp = _hpcopy(&hpbuf, errp);
1500 hp0 = _hpmerge(hp0, hp, errp);
1511 /* if we got here, we didn't satisfy the search.
1512 * if we did an initial full query, return that query's h_errno
1513 * (note that we wouldn't be here if that query had succeeded).
1514 * else if we ever got a nodata, send that back as the reason.
1515 * else send back meaningless h_errno, that being the one from
1516 * the last DNSRCH we did.
1518 if (saved_herrno != -1)
1519 *errp = saved_herrno;
1520 else if (got_nodata)
1522 else if (got_servfail)
1527 static struct hostent *
1528 _dns_ghbyname(const char *name, int af, int *errp)
1530 struct __res_type_list *rtl, rtl4;
1532 struct __res_type_list rtl6;
1538 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1539 SLIST_NEXT(&rtl6, rtl_entry) = &rtl4; rtl6.rtl_type = T_AAAA;
1543 SLIST_NEXT(&rtl6, rtl_entry) = NULL; rtl6.rtl_type = T_AAAA;
1547 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1552 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1555 return(_res_search_multi(name, rtl, errp));
1558 static struct hostent *
1559 _dns_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
1565 struct hostent hbuf;
1568 static const char hex[] = "0123456789abcdef";
1571 char qbuf[MAXDNAME+1];
1573 char *tld6[] = { "ip6.arpa", "ip6.int", NULL };
1574 char *tld4[] = { "in-addr.arpa", NULL };
1579 if (af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr))
1596 if ((_res.options & RES_INIT) == 0) {
1597 if (res_init() < 0) {
1602 memset(&hbuf, 0, sizeof(hbuf));
1604 hbuf.h_addrtype = af;
1605 hbuf.h_length = addrlen;
1608 buf = malloc(sizeof(*buf));
1610 *errp = NETDB_INTERNAL;
1613 for (/* nothing */; *tld; tld++) {
1615 * XXX assumes that MAXDNAME is big enough - error checks
1616 * has been made by callers
1620 cp = (u_char *)addr+addrlen-1;
1624 for (; n < addrlen; n++, cp--) {
1626 *bp++ = hex[c & 0xf];
1628 *bp++ = hex[c >> 4];
1635 for (; n < addrlen; n++, cp--) {
1638 *bp++ = '0' + c / 100;
1640 *bp++ = '0' + (c % 100) / 10;
1641 *bp++ = '0' + c % 10;
1648 n = res_query(qbuf, C_IN, T_PTR, buf->buf, sizeof buf->buf);
1652 } else if (n > sizeof(buf->buf)) {
1653 *errp = NETDB_INTERNAL;
1655 errno = ERANGE; /* XXX is it OK to set errno here? */
1659 hp = getanswer(buf, n, qbuf, T_PTR, &hbuf, errp);
1663 hbuf.h_addrtype = af;
1664 hbuf.h_length = addrlen;
1665 hbuf.h_addr_list = hlist;
1666 hlist[0] = (char *)addr;
1668 return _hpcopy(&hbuf, errp);
1675 _dns_shent(int stayopen)
1677 if ((_res.options & RES_INIT) == 0) {
1682 _res.options |= RES_STAYOPEN | RES_USEVC;
1688 _res.options &= ~(RES_STAYOPEN | RES_USEVC);
1696 * draft-ietf-ipngwg-icmp-namelookups-02.txt
1697 * ifindex is assumed to be encoded in addr.
1699 #include <sys/uio.h>
1700 #include <netinet/ip6.h>
1701 #include <netinet/icmp6.h>
1703 struct _icmp_host_cache {
1704 struct _icmp_host_cache *hc_next;
1706 struct in6_addr hc_addr;
1711 _icmp_fqdn_query(const struct in6_addr *addr, int ifindex)
1714 struct icmp6_filter filter;
1716 struct cmsghdr *cmsg;
1717 struct in6_pktinfo *pkt;
1721 struct icmp6_fqdn_query *fq;
1722 struct icmp6_fqdn_reply *fr;
1723 struct _icmp_host_cache *hc;
1724 struct sockaddr_in6 sin6;
1727 struct timeval tout;
1731 static struct _icmp_host_cache *hc_head;
1733 for (hc = hc_head; hc; hc = hc->hc_next) {
1734 if (hc->hc_ifindex == ifindex
1735 && IN6_ARE_ADDR_EQUAL(&hc->hc_addr, addr))
1742 ICMP6_FILTER_SETBLOCKALL(&filter);
1743 ICMP6_FILTER_SETPASS(ICMP6_FQDN_REPLY, &filter);
1747 tout.tv_usec = 200000; /*XXX: 200ms*/
1749 fq = (struct icmp6_fqdn_query *)buf;
1750 fq->icmp6_fqdn_type = ICMP6_FQDN_QUERY;
1751 fq->icmp6_fqdn_code = 0;
1752 fq->icmp6_fqdn_cksum = 0;
1753 fq->icmp6_fqdn_id = (u_short)pid;
1754 fq->icmp6_fqdn_unused = 0;
1755 fq->icmp6_fqdn_cookie[0] = 0;
1756 fq->icmp6_fqdn_cookie[1] = 0;
1758 memset(&sin6, 0, sizeof(sin6));
1759 sin6.sin6_family = AF_INET6;
1760 sin6.sin6_addr = *addr;
1762 memset(&msg, 0, sizeof(msg));
1763 msg.msg_name = (caddr_t)&sin6;
1764 msg.msg_namelen = sizeof(sin6);
1767 msg.msg_control = NULL;
1768 msg.msg_controllen = 0;
1769 iov.iov_base = (caddr_t)buf;
1770 iov.iov_len = sizeof(struct icmp6_fqdn_query);
1773 msg.msg_control = cbuf;
1774 msg.msg_controllen = sizeof(cbuf);
1775 cmsg = CMSG_FIRSTHDR(&msg);
1776 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1777 cmsg->cmsg_level = IPPROTO_IPV6;
1778 cmsg->cmsg_type = IPV6_PKTINFO;
1779 pkt = (struct in6_pktinfo *)&cmsg[1];
1780 memset(&pkt->ipi6_addr, 0, sizeof(struct in6_addr));
1781 pkt->ipi6_ifindex = ifindex;
1782 cmsg = CMSG_NXTHDR(&msg, cmsg);
1783 msg.msg_controllen = (char *)cmsg - cbuf;
1786 if ((s = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
1788 (void)setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER,
1789 (char *)&filter, sizeof(filter));
1790 cc = sendmsg(s, &msg, 0);
1798 if (select(s + 1, &fds, NULL, NULL, &tout) <= 0) {
1803 cc = recvfrom(s, buf, sizeof(buf), 0,
1804 (struct sockaddr *)&sin6, &len);
1809 if (cc < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr))
1811 if (!IN6_ARE_ADDR_EQUAL(addr, &sin6.sin6_addr))
1813 fr = (struct icmp6_fqdn_reply *)(buf + sizeof(struct ip6_hdr));
1814 if (fr->icmp6_fqdn_type == ICMP6_FQDN_REPLY)
1818 if (fr->icmp6_fqdn_cookie[1] != 0) {
1820 name = buf + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 4;
1821 len = (buf + cc) - name;
1823 len = fr->icmp6_fqdn_namelen;
1824 name = fr->icmp6_fqdn_name;
1830 if ((hc = (struct _icmp_host_cache *)malloc(sizeof(*hc))) == NULL)
1832 /* XXX: limit number of cached entries */
1833 hc->hc_ifindex = ifindex;
1834 hc->hc_addr = *addr;
1835 hc->hc_name = strdup(name);
1836 hc->hc_next = hc_head;
1841 static struct hostent *
1842 _icmp_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
1846 struct in6_addr addr6;
1848 if (af != AF_INET6) {
1850 * Note: rfc1788 defines Who Are You for IPv4,
1851 * but no one implements it.
1856 memcpy(&addr6, addr, addrlen);
1857 ifindex = (addr6.s6_addr[2] << 8) | addr6.s6_addr[3];
1858 addr6.s6_addr[2] = addr6.s6_addr[3] = 0;
1860 if (!IN6_IS_ADDR_LINKLOCAL(&addr6))
1861 return NULL; /*XXX*/
1863 if ((hname = _icmp_fqdn_query(&addr6, ifindex)) == NULL)
1865 return _hpaddr(af, hname, &addr6, errp);