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.9 2005/11/13 02:04:47 swildner 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. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
65 * Permission to use, copy, modify, and distribute this software for any
66 * purpose with or without fee is hereby granted, provided that the above
67 * copyright notice and this permission notice appear in all copies, and that
68 * the name of Digital Equipment Corporation not be used in advertising or
69 * publicity pertaining to distribution of the document or software without
70 * specific, written prior permission.
72 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
73 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
74 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
75 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
76 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
77 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
78 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
85 * Atsushi Onoe <onoe@sm.sony.co.jp>
89 * TODO for thread safe
90 * use mutex for _hostconf, _hostconf_init.
91 * rewrite resolvers to be thread safe
94 #include "namespace.h"
95 #include <sys/param.h>
96 #include <sys/socket.h>
98 #include <sys/queue.h>
99 #include <netinet/in.h>
101 #include <arpa/inet.h>
102 #include <arpa/nameser.h>
111 #include "un-namespace.h"
114 #define _PATH_HOSTS "/etc/hosts"
118 #define MAXALIASES 10
124 #define MAXDNAME 1025
128 #define ADDRLEN(af) ((af) == AF_INET6 ? sizeof(struct in6_addr) : \
129 sizeof(struct in_addr))
131 #define ADDRLEN(af) sizeof(struct in_addr)
134 #define MAPADDR(ab, ina) \
136 memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr)); \
137 memset((ab)->map_zero, 0, sizeof((ab)->map_zero)); \
138 memset((ab)->map_one, 0xff, sizeof((ab)->map_one)); \
140 #define MAPADDRENABLED(flags) \
141 (((flags) & AI_V4MAPPED) || \
142 (((flags) & AI_V4MAPPED_CFG) && _mapped_addr_enabled()))
145 struct in_addr in_addr;
147 struct in6_addr in6_addr;
152 struct in_addr mau_inaddr;
154 #define map_zero map_addr_un.mau_zero
155 #define map_one map_addr_un.mau_one
156 #define map_inaddr map_addr_un.mau_inaddr
159 static struct hostent *_hpcopy(struct hostent *hp, int *errp);
160 static struct hostent *_hpaddr(int af, const char *name, void *addr, int *errp);
161 static struct hostent *_hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp);
163 static struct hostent *_hpmapv6(struct hostent *hp, int *errp);
165 static struct hostent *_hpsort(struct hostent *hp);
166 static struct hostent *_ghbyname(const char *name, int af, int flags, int *errp);
167 static char *_hgetword(char **pp);
168 static int _mapped_addr_enabled(void);
170 static FILE *_files_open(int *errp);
171 static struct hostent *_files_ghbyname(const char *name, int af, int *errp);
172 static struct hostent *_files_ghbyaddr(const void *addr, int addrlen, int af, int *errp);
174 static struct hostent *_nis_ghbyname(const char *name, int af, int *errp);
175 static struct hostent *_nis_ghbyaddr(const void *addr, int addrlen, int af, int *errp);
177 static struct hostent *_dns_ghbyname(const char *name, int af, int *errp);
178 static struct hostent *_dns_ghbyaddr(const void *addr, int addrlen, int af, int *errp);
180 static struct hostent *_icmp_ghbyaddr(const void *addr, int addrlen, int af, int *errp);
183 /* Make getipnodeby*() thread-safe in libc for use with kernel threads. */
184 #include "libc_private.h"
185 #include "spinlock.h"
187 * XXX: Our res_*() is not thread-safe. So, we share lock between
188 * getaddrinfo() and getipnodeby*(). Still, we cannot use
189 * getaddrinfo() and getipnodeby*() in conjunction with other
190 * functions which call res_*().
192 extern spinlock_t __getaddrinfo_thread_lock;
193 #define THREAD_LOCK() \
194 if (__isthreaded) _SPINLOCK(&__getaddrinfo_thread_lock);
195 #define THREAD_UNLOCK() \
196 if (__isthreaded) _SPINUNLOCK(&__getaddrinfo_thread_lock);
199 * Select order host function.
201 #define MAXHOSTCONF 4
204 # define HOSTCONF "/etc/host.conf"
205 #endif /* !HOSTCONF */
208 struct hostent *(*byname)(const char *name, int af, int *errp);
209 struct hostent *(*byaddr)(const void *addr, int addrlen, int af, int *errp);
213 static struct _hostconf _hostconf[MAXHOSTCONF] = {
214 { _dns_ghbyname, _dns_ghbyaddr },
215 { _files_ghbyname, _files_ghbyaddr },
217 { NULL, _icmp_ghbyaddr },
221 static int _hostconf_init_done;
222 static void _hostconf_init(void);
225 * Initialize hostconf structure.
237 _hostconf_init_done = 1;
240 if ((fp = fopen(p, "r")) == NULL)
242 while (n < MAXHOSTCONF && fgets(buf, sizeof(buf), fp)) {
244 if ((p = _hgetword(&line)) == NULL)
247 if (strcmp(p, "hosts") == 0
248 || strcmp(p, "local") == 0
249 || strcmp(p, "file") == 0
250 || strcmp(p, "files") == 0) {
251 _hostconf[n].byname = _files_ghbyname;
252 _hostconf[n].byaddr = _files_ghbyaddr;
255 else if (strcmp(p, "dns") == 0
256 || strcmp(p, "bind") == 0) {
257 _hostconf[n].byname = _dns_ghbyname;
258 _hostconf[n].byaddr = _dns_ghbyaddr;
262 else if (strcmp(p, "nis") == 0) {
263 _hostconf[n].byname = _nis_ghbyname;
264 _hostconf[n].byaddr = _nis_ghbyaddr;
269 else if (strcmp(p, "icmp") == 0) {
270 _hostconf[n].byname = NULL;
271 _hostconf[n].byaddr = _icmp_ghbyaddr;
275 } while ((p = _hgetword(&line)) != NULL);
279 /* no keyword found. do not change default configuration */
282 for (; n < MAXHOSTCONF; n++) {
283 _hostconf[n].byname = NULL;
284 _hostconf[n].byaddr = NULL;
289 * Check if kernel supports mapped address.
290 * implementation dependent
293 #include <sys/sysctl.h>
294 #endif /* __KAME__ */
297 _mapped_addr_enabled(void)
299 /* implementation dependent check */
300 #if defined(__KAME__) && defined(IPV6CTL_MAPPED_ADDR)
307 mib[2] = IPPROTO_IPV6;
308 mib[3] = IPV6CTL_MAPPED_ADDR;
310 if (sysctl(mib, 4, &val, &len, 0, 0) == 0 && val != 0)
312 #endif /* __KAME__ && IPV6CTL_MAPPED_ADDR */
317 * Functions defined in RFC2553
318 * getipnodebyname, getipnodebyaddr, freehostent
321 static struct hostent *
322 _ghbyname(const char *name, int af, int flags, int *errp)
327 if (flags & AI_ADDRCONFIG) {
332 * Note that implementation dependent test for address
333 * configuration should be done everytime called
334 * (or apropriate interval),
335 * because addresses will be dynamically assigned or deleted.
337 if (af == AF_UNSPEC) {
338 if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
342 if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0)
349 if (af != AF_UNSPEC) {
350 if ((s = _socket(af, SOCK_DGRAM, 0)) < 0)
357 for (i = 0; i < MAXHOSTCONF; i++) {
358 if (_hostconf[i].byname
359 && (hp = (*_hostconf[i].byname)(name, af, errp)) != NULL) {
369 /* getipnodebyname() internal routine for multiple query(PF_UNSPEC) support. */
370 static struct hostent *
371 _getipnodebyname_multi(const char *name, int af, int flags, int *errp)
374 union inx_addr addrbuf;
376 /* XXX: PF_UNSPEC is only supposed to be passed from getaddrinfo() */
389 /* special case for literal address */
390 if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
391 if (af != AF_INET6) {
392 *errp = HOST_NOT_FOUND;
395 return _hpaddr(af, name, &addrbuf, errp);
398 if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) {
400 if (MAPADDRENABLED(flags)) {
401 MAPADDR(&addrbuf, &addrbuf.in_addr);
403 *errp = HOST_NOT_FOUND;
407 return _hpaddr(af, name, &addrbuf, errp);
410 if (!_hostconf_init_done)
413 *errp = HOST_NOT_FOUND;
414 hp = _ghbyname(name, af, flags, errp);
418 && ((flags & AI_ALL) || hp == NULL)
419 && (MAPADDRENABLED(flags))) {
420 struct hostent *hp2 = _ghbyname(name, AF_INET, flags, errp);
422 hp = _hpmapv6(hp2, errp);
424 if (hp2 && strcmp(hp->h_name, hp2->h_name) != 0) {
428 hp = _hpmerge(hp, hp2, errp);
436 getipnodebyname(const char *name, int af, int flags, int *errp)
447 return(_getipnodebyname_multi(name, af ,flags, errp));
451 getipnodebyaddr(const void *src, size_t len, int af, int *errp)
456 struct in6_addr addrbuf;
458 struct in_addr addrbuf;
461 *errp = HOST_NOT_FOUND;
465 if (len != sizeof(struct in_addr)) {
469 if ((long)src & ~(sizeof(struct in_addr) - 1)) {
470 memcpy(&addrbuf, src, len);
473 if (((const struct in_addr *)src)->s_addr == 0)
478 if (len != sizeof(struct in6_addr)) {
482 if ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) { /*XXX*/
483 memcpy(&addrbuf, src, len);
486 if (IN6_IS_ADDR_UNSPECIFIED((const struct in6_addr *)src))
488 if (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)src)
489 || IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)src)) {
490 src = (const char *)src +
491 (sizeof(struct in6_addr) - sizeof(struct in_addr));
493 len = sizeof(struct in_addr);
502 if (!_hostconf_init_done)
505 for (i = 0; i < MAXHOSTCONF; i++) {
506 if (_hostconf[i].byaddr
507 && (hp = (*_hostconf[i].byaddr)(src, len, af, errp)) != NULL) {
518 freehostent(struct hostent *ptr)
525 /* XXX: should be deprecated */
527 getnodebyname(const char *name, int af, int flags)
529 return getipnodebyname(name, af, flags, &h_errno);
532 #ifdef __warn_references
533 __warn_references(getnodebyname,
534 "warning: getnodebyname() deprecated, "
535 "should use getaddrinfo() or getipnodebyname()");
539 getnodebyaddr(const void *src, size_t len, int af)
541 return getipnodebyaddr(src, len, af, &h_errno);
544 #ifdef __warn_references
545 __warn_references(getnodebyaddr,
546 "warning: getnodebyaddr() deprecated, "
547 "should use getnameinfo() or getipnodebyaddr()");
553 * Private utility functions
557 * _hpcopy: allocate and copy hostent structure
559 static struct hostent *
560 _hpcopy(struct hostent *hp, int *errp)
565 int nalias = 0, naddr = 0;
572 /* count size to be allocated */
573 size = sizeof(struct hostent);
574 if (hp->h_name != NULL)
575 size += strlen(hp->h_name) + 1;
576 if ((pp = hp->h_aliases) != NULL) {
577 for (i = 0; *pp != NULL; i++, pp++) {
579 size += strlen(*pp) + 1;
584 /* adjust alignment */
587 size += sizeof(char *) * (nalias + 1);
588 addrsize = ALIGN(hp->h_length);
589 if ((pp = hp->h_addr_list) != NULL) {
590 while (*pp++ != NULL)
593 size += addrsize * naddr;
594 size += sizeof(char *) * (naddr + 1);
597 if ((nhp = (struct hostent *)malloc(size)) == NULL) {
601 cp = (char *)&nhp[1];
602 if (hp->h_name != NULL) {
604 strcpy(cp, hp->h_name);
605 cp += strlen(cp) + 1;
608 nhp->h_aliases = (char **)((char *)nhp + al_off);
609 if ((pp = hp->h_aliases) != NULL) {
610 for (i = 0; *pp != NULL; pp++) {
612 nhp->h_aliases[i++] = cp;
614 cp += strlen(cp) + 1;
618 nhp->h_aliases[nalias] = NULL;
619 cp = (char *)&nhp->h_aliases[nalias + 1];
620 nhp->h_addrtype = hp->h_addrtype;
621 nhp->h_length = hp->h_length;
622 nhp->h_addr_list = (char **)cp;
623 if ((pp = hp->h_addr_list) != NULL) {
624 cp = (char *)&nhp->h_addr_list[naddr + 1];
625 for (i = 0; *pp != NULL; pp++) {
626 nhp->h_addr_list[i++] = cp;
627 memcpy(cp, *pp, hp->h_length);
631 nhp->h_addr_list[naddr] = NULL;
636 * _hpaddr: construct hostent structure with one address
638 static struct hostent *
639 _hpaddr(int af, const char *name, void *addr, int *errp)
641 struct hostent *hp, hpbuf;
646 hp->h_aliases = NULL;
648 hp->h_length = ADDRLEN(af);
649 hp->h_addr_list = addrs;
652 return _hpcopy(hp, errp);
656 * _hpmerge: merge 2 hostent structure, arguments will be freed
658 static struct hostent *
659 _hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp)
664 struct hostent *hp, hpbuf;
665 char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1];
666 union inx_addr addrbuf[MAXADDRS];
673 #define HP(i) (i == 1 ? hp1 : hp2)
675 hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name);
676 hp->h_aliases = aliases;
678 for (i = 1; i <= 2; i++) {
679 if ((pp = HP(i)->h_aliases) == NULL)
681 for (; nalias < MAXALIASES && *pp != NULL; pp++) {
682 /* check duplicates */
683 for (j = 0; j < nalias; j++)
684 if (strcasecmp(*pp, aliases[j]) == 0)
687 aliases[nalias++] = *pp;
690 aliases[nalias] = NULL;
692 if (hp1->h_length != hp2->h_length) {
693 hp->h_addrtype = AF_INET6;
694 hp->h_length = sizeof(struct in6_addr);
697 hp->h_addrtype = hp1->h_addrtype;
698 hp->h_length = hp1->h_length;
702 hp->h_addr_list = addrs;
704 for (i = 1; i <= 2; i++) {
705 if ((pp = HP(i)->h_addr_list) == NULL)
707 if (HP(i)->h_length == hp->h_length) {
708 while (naddr < MAXADDRS && *pp != NULL)
709 addrs[naddr++] = *pp++;
711 /* copy IPv4 addr as mapped IPv6 addr */
712 while (naddr < MAXADDRS && *pp != NULL) {
713 MAPADDR(&addrbuf[naddr], *pp++);
714 addrs[naddr] = (char *)&addrbuf[naddr];
720 hp = _hpcopy(hp, errp);
727 * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses
730 static struct hostent *
731 _hpmapv6(struct hostent *hp, int *errp)
737 if (hp->h_addrtype == AF_INET6)
740 /* make dummy hostent to convert IPv6 address */
741 if ((hp6 = (struct hostent *)malloc(sizeof(struct hostent))) == NULL) {
746 hp6->h_aliases = NULL;
747 hp6->h_addrtype = AF_INET6;
748 hp6->h_length = sizeof(struct in6_addr);
749 hp6->h_addr_list = NULL;
750 return _hpmerge(hp6, hp, errp);
755 * _hpsort: sort address by sortlist
757 static struct hostent *
758 _hpsort(struct hostent *hp)
761 u_char *ap, *sp, *mp, **pp;
763 char order[MAXADDRS];
764 int nsort = _res.nsort;
766 if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0)
768 for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
769 for (j = 0; j < nsort; j++) {
771 if (_res_ext.sort_list[j].af != hp->h_addrtype)
773 sp = (u_char *)&_res_ext.sort_list[j].addr;
774 mp = (u_char *)&_res_ext.sort_list[j].mask;
776 sp = (u_char *)&_res.sort_list[j].addr;
777 mp = (u_char *)&_res.sort_list[j].mask;
779 for (n = 0; n < hp->h_length; n++) {
780 if ((ap[n] & mp[n]) != sp[n])
783 if (n == hp->h_length)
789 pp = (u_char **)hp->h_addr_list;
790 for (i = 0; i < n - 1; i++) {
791 for (j = i + 1; j < n; j++) {
792 if (order[i] > order[j]) {
810 static const char sep[] = "# \t\n";
813 for (p = *pp; (c = *p) != '\0'; p++) {
814 for (sp = sep; *sp != '\0'; sp++) {
819 p[1] = '\0'; /* ignore rest of line */
831 if (ret == NULL || *ret == '\0')
841 _files_open(int *errp)
844 fp = fopen(_PATH_HOSTS, "r");
850 static struct hostent *
851 _files_ghbyname(const char *name, int af, int *errp)
854 char *p, *line, *addrstr, *cname;
856 struct hostent *rethp, *hp, hpbuf;
857 char *aliases[MAXALIASES + 1], *addrs[2];
858 union inx_addr addrbuf;
862 if ((fp = _files_open(errp)) == NULL)
866 while (fgets(buf, sizeof(buf), fp)) {
868 if ((addrstr = _hgetword(&line)) == NULL
869 || (cname = _hgetword(&line)) == NULL)
871 match = (strcasecmp(cname, name) == 0);
873 while ((p = _hgetword(&line)) != NULL) {
875 match = (strcasecmp(p, name) == 0);
876 if (nalias < MAXALIASES)
877 aliases[nalias++] = p;
883 if (inet_aton(addrstr, (struct in_addr *)&addrbuf)
885 *errp = NO_DATA; /* name found */
892 if (inet_pton(af, addrstr, &addrbuf) != 1) {
893 *errp = NO_DATA; /* name found */
900 if (inet_aton(addrstr, (struct in_addr *)&addrbuf)
906 if (inet_pton(AF_INET6, addrstr, &addrbuf) == 1) {
911 *errp = NO_DATA; /* name found */
917 hp->h_aliases = aliases;
918 aliases[nalias] = NULL;
920 hp->h_length = ADDRLEN(af);
921 hp->h_addr_list = addrs;
922 addrs[0] = (char *)&addrbuf;
924 hp = _hpcopy(hp, errp);
925 rethp = _hpmerge(rethp, hp, errp);
931 static struct hostent *
932 _files_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
937 struct hostent *hp, hpbuf;
938 char *aliases[MAXALIASES + 1], *addrs[2];
939 union inx_addr addrbuf;
942 if ((fp = _files_open(errp)) == NULL)
945 while (fgets(buf, sizeof(buf), fp)) {
947 if ((p = _hgetword(&line)) == NULL
949 ? inet_aton(p, (struct in_addr *)&addrbuf)
950 : inet_pton(af, p, &addrbuf)) != 1
951 || memcmp(addr, &addrbuf, addrlen) != 0
952 || (p = _hgetword(&line)) == NULL)
956 hp->h_aliases = aliases;
958 while ((p = _hgetword(&line)) != NULL) {
959 if (nalias < MAXALIASES)
960 aliases[nalias++] = p;
962 aliases[nalias] = NULL;
964 hp->h_length = addrlen;
965 hp->h_addr_list = addrs;
966 addrs[0] = (char *)&addrbuf;
968 hp = _hpcopy(hp, errp);
979 * XXX actually a hack, these are INET4 specific.
981 static struct hostent *
982 _nis_ghbyname(const char *name, int af, int *errp)
984 struct hostent *hp = NULL;
989 hp = _gethostbynisname(name, af);
991 hp = _hpcopy(hp, errp);
997 static struct hostent *
998 _nis_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
1000 struct hostent *hp = NULL;
1002 if (af == AF_INET) {
1003 hp = _gethostbynisaddr(addr, addrlen, af);
1005 hp = _hpcopy(hp, errp);
1011 struct __res_type_list {
1012 SLIST_ENTRY(__res_type_list) rtl_entry;
1016 #define MAXPACKET (64*1024)
1020 u_char buf[MAXPACKET];
1023 static struct hostent *getanswer (const querybuf *, int, const char *,
1024 int, struct hostent *, int *);
1027 * we don't need to take care about sorting, nor IPv4 mapped address here.
1029 static struct hostent *
1030 getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
1031 struct hostent *template, int *errp)
1036 const u_char *eom, *erdata;
1037 char *bp, **ap, **hap, *obp;
1038 int type, class, buflen, ancount, qdcount;
1039 int haveanswer, had_error;
1040 char tbuf[MAXDNAME];
1042 int (*name_ok) (const char *);
1043 static char *h_addr_ptrs[MAXADDRS + 1];
1044 static char *host_aliases[MAXALIASES];
1045 static char hostbuf[8*1024];
1047 #define BOUNDED_INCR(x) \
1051 *errp = NO_RECOVERY; \
1056 #define BOUNDS_CHECK(ptr, count) \
1058 if ((ptr) + (count) > eom) { \
1059 *errp = NO_RECOVERY; \
1064 /* XXX do {} while (0) cannot be put here */
1065 #define DNS_ASSERT(x) \
1073 /* XXX do {} while (0) cannot be put here */
1074 #define DNS_FATAL(x) \
1083 template->h_name = NULL;
1084 eom = answer->buf + anslen;
1094 return (NULL); /* XXX should be abort(); */
1097 * find first satisfactory answer
1100 ancount = ntohs(hp->ancount);
1101 qdcount = ntohs(hp->qdcount);
1103 buflen = sizeof hostbuf;
1105 BOUNDED_INCR(HFIXEDSZ);
1107 *errp = NO_RECOVERY;
1110 n = dn_expand(answer->buf, eom, cp, bp, buflen);
1111 if ((n < 0) || !(*name_ok)(bp)) {
1112 *errp = NO_RECOVERY;
1115 BOUNDED_INCR(n + QFIXEDSZ);
1116 if (qtype == T_A || qtype == T_AAAA) {
1117 /* res_send() has already verified that the query name is the
1118 * same as the one we sent; this just gets the expanded name
1119 * (i.e., with the succeeding search-domain tacked on).
1121 n = strlen(bp) + 1; /* for the \0 */
1122 if (n >= MAXHOSTNAMELEN) {
1123 *errp = NO_RECOVERY;
1126 template->h_name = bp;
1129 /* The qname can be abbreviated, but h_name is now absolute. */
1130 qname = template->h_name;
1134 template->h_aliases = host_aliases;
1137 template->h_addr_list = h_addr_ptrs;
1140 while (ancount-- > 0 && cp < eom && !had_error) {
1141 n = dn_expand(answer->buf, eom, cp, bp, buflen);
1143 DNS_FATAL((*name_ok)(bp));
1145 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
1146 NS_GET16(type, cp); /* type */
1147 NS_GET16(class, cp); /* class */
1148 cp += INT32SZ; /* skip TTL */
1149 NS_GET16(n, cp); /* len */
1150 BOUNDS_CHECK(cp, n);
1152 DNS_ASSERT(class == C_IN);
1153 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
1154 if (ap >= &host_aliases[MAXALIASES-1])
1156 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1158 DNS_FATAL((*name_ok)(tbuf));
1161 *errp = NO_RECOVERY;
1166 n = strlen(bp) + 1; /* for the \0 */
1167 DNS_FATAL(n < MAXHOSTNAMELEN);
1170 /* Get canonical name. */
1171 n = strlen(tbuf) + 1; /* for the \0 */
1172 DNS_FATAL(n <= buflen);
1173 DNS_FATAL(n < MAXHOSTNAMELEN);
1175 template->h_name = bp;
1180 if (qtype == T_PTR && type == T_CNAME) {
1181 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1182 if (n < 0 || !res_dnok(tbuf)) {
1188 *errp = NO_RECOVERY;
1191 /* Get canonical name. */
1192 n = strlen(tbuf) + 1; /* for the \0 */
1193 if (n > buflen || n >= MAXHOSTNAMELEN) {
1203 DNS_ASSERT(type == qtype);
1206 DNS_ASSERT(strcasecmp(tname, bp) == 0);
1207 n = dn_expand(answer->buf, eom, cp, bp, buflen);
1209 DNS_FATAL(res_hnok(bp));
1210 #if MULTI_PTRS_ARE_ALIASES
1213 *errp = NO_RECOVERY;
1217 template->h_name = bp;
1218 else if (ap < &host_aliases[MAXALIASES-1])
1223 n = strlen(bp) + 1; /* for the \0 */
1224 if (n >= MAXHOSTNAMELEN) {
1233 template->h_name = bp;
1234 *errp = NETDB_SUCCESS;
1239 DNS_ASSERT(strcasecmp(template->h_name, bp) == 0);
1240 DNS_ASSERT(n == template->h_length);
1244 template->h_name = bp;
1245 nn = strlen(bp) + 1; /* for the \0 */
1249 obp = bp; /* ALIGN rounds up */
1250 bp = (char *)ALIGN(bp);
1251 buflen -= (bp - obp);
1253 DNS_FATAL(bp + n < &hostbuf[sizeof hostbuf]);
1254 DNS_ASSERT(hap < &h_addr_ptrs[MAXADDRS-1]);
1255 #ifdef FILTER_V4MAPPED
1256 if (type == T_AAAA) {
1257 struct in6_addr in6;
1258 memcpy(&in6, cp, sizeof(in6));
1259 DNS_ASSERT(IN6_IS_ADDR_V4MAPPED(&in6) == 0);
1262 bcopy(cp, *hap++ = bp, n);
1267 *errp = NO_RECOVERY;
1280 if (!template->h_name) {
1281 n = strlen(qname) + 1; /* for the \0 */
1282 if (n > buflen || n >= MAXHOSTNAMELEN)
1285 template->h_name = bp;
1289 *errp = NETDB_SUCCESS;
1293 *errp = NO_RECOVERY;
1302 /* res_search() variant with multiple query support. */
1303 static struct hostent *
1304 _res_search_multi(const char *name, struct __res_type_list *rtl, int *errp)
1306 const char *cp, * const *domain;
1307 struct hostent *hp0 = NULL, *hp;
1308 struct hostent hpbuf;
1310 int trailing_dot, ret, saved_herrno;
1311 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
1312 struct __res_type_list *rtl0 = rtl;
1315 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
1316 *errp = NETDB_INTERNAL;
1320 for (cp = name; *cp; cp++)
1321 dots += (*cp == '.');
1323 if (cp > name && *--cp == '.')
1326 buf = malloc(sizeof(*buf));
1328 *errp = NETDB_INTERNAL;
1332 /* If there aren't any dots, it could be a user-level alias */
1333 if (!dots && (cp = hostalias(name)) != NULL) {
1334 for(rtl = rtl0; rtl != NULL;
1335 rtl = SLIST_NEXT(rtl, rtl_entry)) {
1336 ret = res_query(cp, C_IN, rtl->rtl_type, buf->buf,
1338 if (ret > 0 && (size_t)ret < sizeof(buf->buf)) {
1339 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1340 ? AF_INET6 : AF_INET;
1341 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1342 hp = getanswer(buf, ret, name, rtl->rtl_type,
1346 hp = _hpcopy(&hpbuf, errp);
1347 hp0 = _hpmerge(hp0, hp, errp);
1355 * If there are dots in the name already, let's just give it a try
1356 * 'as is'. The threshold can be set with the "ndots" option.
1359 if (dots >= _res.ndots) {
1360 for(rtl = rtl0; rtl != NULL;
1361 rtl = SLIST_NEXT(rtl, rtl_entry)) {
1362 ret = res_querydomain(name, NULL, C_IN, rtl->rtl_type,
1363 buf->buf, sizeof(buf->buf));
1364 if (ret > 0 && (size_t)ret < sizeof(buf->buf)) {
1365 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1366 ? AF_INET6 : AF_INET;
1367 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1368 hp = getanswer(buf, ret, name, rtl->rtl_type,
1372 hp = _hpcopy(&hpbuf, errp);
1373 hp0 = _hpmerge(hp0, hp, errp);
1380 saved_herrno = *errp;
1385 * We do at least one level of search if
1386 * - there is no dot and RES_DEFNAME is set, or
1387 * - there is at least one dot, there is no trailing dot,
1388 * and RES_DNSRCH is set.
1390 if ((!dots && (_res.options & RES_DEFNAMES)) ||
1391 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
1394 for (domain = (const char * const *)_res.dnsrch;
1398 for(rtl = rtl0; rtl != NULL;
1399 rtl = SLIST_NEXT(rtl, rtl_entry)) {
1400 ret = res_querydomain(name, *domain, C_IN,
1402 buf->buf, sizeof(buf->buf));
1403 if (ret > 0 && (size_t)ret < sizeof(buf->buf)) {
1404 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1405 ? AF_INET6 : AF_INET;
1406 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1407 hp = getanswer(buf, ret, name,
1408 rtl->rtl_type, &hpbuf, errp);
1411 hp = _hpcopy(&hpbuf, errp);
1412 hp0 = _hpmerge(hp0, hp, errp);
1421 * If no server present, give up.
1422 * If name isn't found in this domain,
1423 * keep trying higher domains in the search list
1424 * (if that's enabled).
1425 * On a NO_DATA error, keep trying, otherwise
1426 * a wildcard entry of another type could keep us
1427 * from finding this entry higher in the domain.
1428 * If we get some other error (negative answer or
1429 * server failure), then stop searching up,
1430 * but try the input name below in case it's
1433 if (errno == ECONNREFUSED) {
1443 case HOST_NOT_FOUND:
1447 if (buf->hdr.rcode == SERVFAIL) {
1448 /* try next search element, if any */
1454 /* anything else implies that we're done */
1458 /* if we got here for some reason other than DNSRCH,
1459 * we only wanted one iteration of the loop, so stop.
1461 if (!(_res.options & RES_DNSRCH))
1467 * If we have not already tried the name "as is", do that now.
1468 * note that we do this regardless of how many dots were in the
1469 * name or whether it ends with a dot unless NOTLDQUERY is set.
1471 if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) {
1472 for(rtl = rtl0; rtl != NULL;
1473 rtl = SLIST_NEXT(rtl, rtl_entry)) {
1474 ret = res_querydomain(name, NULL, C_IN, rtl->rtl_type,
1475 buf->buf, sizeof(buf->buf));
1476 if (ret > 0 && (size_t)ret < sizeof(buf->buf)) {
1477 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1478 ? AF_INET6 : AF_INET;
1479 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1480 hp = getanswer(buf, ret, name, rtl->rtl_type,
1484 hp = _hpcopy(&hpbuf, errp);
1485 hp0 = _hpmerge(hp0, hp, errp);
1496 /* if we got here, we didn't satisfy the search.
1497 * if we did an initial full query, return that query's h_errno
1498 * (note that we wouldn't be here if that query had succeeded).
1499 * else if we ever got a nodata, send that back as the reason.
1500 * else send back meaningless h_errno, that being the one from
1501 * the last DNSRCH we did.
1503 if (saved_herrno != -1)
1504 *errp = saved_herrno;
1505 else if (got_nodata)
1507 else if (got_servfail)
1512 static struct hostent *
1513 _dns_ghbyname(const char *name, int af, int *errp)
1515 struct __res_type_list *rtl, rtl4;
1517 struct __res_type_list rtl6;
1523 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1524 SLIST_NEXT(&rtl6, rtl_entry) = &rtl4; rtl6.rtl_type = T_AAAA;
1528 SLIST_NEXT(&rtl6, rtl_entry) = NULL; rtl6.rtl_type = T_AAAA;
1532 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1539 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1542 return(_res_search_multi(name, rtl, errp));
1545 static struct hostent *
1546 _dns_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
1553 struct hostent hbuf;
1556 static const char hex[] = "0123456789abcdef";
1559 char qbuf[MAXDNAME+1];
1561 const char *tld6[] = { "ip6.arpa", "ip6.int", NULL };
1562 const char *tld4[] = { "in-addr.arpa", NULL };
1567 if (af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)addr))
1584 if ((_res.options & RES_INIT) == 0) {
1585 if (res_init() < 0) {
1590 memset(&hbuf, 0, sizeof(hbuf));
1592 hbuf.h_addrtype = af;
1593 hbuf.h_length = addrlen;
1596 buf = malloc(sizeof(*buf));
1598 *errp = NETDB_INTERNAL;
1601 for (/* nothing */; *tld; tld++) {
1603 * XXX assumes that MAXDNAME is big enough - error checks
1604 * has been made by callers
1608 cp = (const uint8_t *)addr+addrlen-1;
1612 for (; n < addrlen; n++, cp--) {
1614 *bp++ = hex[c & 0xf];
1616 *bp++ = hex[c >> 4];
1623 for (; n < addrlen; n++, cp--) {
1626 *bp++ = '0' + c / 100;
1628 *bp++ = '0' + (c % 100) / 10;
1629 *bp++ = '0' + c % 10;
1636 n = res_query(qbuf, C_IN, T_PTR, buf->buf, sizeof buf->buf);
1640 } else if ((size_t)n > sizeof(buf->buf)) {
1641 *errp = NETDB_INTERNAL;
1643 errno = ERANGE; /* XXX is it OK to set errno here? */
1647 hp = getanswer(buf, n, qbuf, T_PTR, &hbuf, errp);
1651 hbuf.h_addrtype = af;
1652 hbuf.h_length = addrlen;
1653 hbuf.h_addr_list = hlist;
1656 return _hpcopy(&hbuf, errp);
1666 * draft-ietf-ipngwg-icmp-namelookups-02.txt
1667 * ifindex is assumed to be encoded in addr.
1669 #include <sys/uio.h>
1670 #include <netinet/ip6.h>
1671 #include <netinet/icmp6.h>
1673 struct _icmp_host_cache {
1674 struct _icmp_host_cache *hc_next;
1676 struct in6_addr hc_addr;
1681 _icmp_fqdn_query(const struct in6_addr *addr, int ifindex)
1684 struct icmp6_filter filter;
1686 struct cmsghdr *cmsg;
1687 struct in6_pktinfo *pkt;
1691 struct icmp6_fqdn_query *fq;
1692 struct icmp6_fqdn_reply *fr;
1693 struct _icmp_host_cache *hc;
1694 struct sockaddr_in6 sin6;
1697 struct timeval tout;
1701 static struct _icmp_host_cache *hc_head;
1703 for (hc = hc_head; hc; hc = hc->hc_next) {
1704 if (hc->hc_ifindex == ifindex
1705 && IN6_ARE_ADDR_EQUAL(&hc->hc_addr, addr))
1712 ICMP6_FILTER_SETBLOCKALL(&filter);
1713 ICMP6_FILTER_SETPASS(ICMP6_FQDN_REPLY, &filter);
1717 tout.tv_usec = 200000; /*XXX: 200ms*/
1719 fq = (struct icmp6_fqdn_query *)buf;
1720 fq->icmp6_fqdn_type = ICMP6_FQDN_QUERY;
1721 fq->icmp6_fqdn_code = 0;
1722 fq->icmp6_fqdn_cksum = 0;
1723 fq->icmp6_fqdn_id = (u_short)pid;
1724 fq->icmp6_fqdn_unused = 0;
1725 fq->icmp6_fqdn_cookie[0] = 0;
1726 fq->icmp6_fqdn_cookie[1] = 0;
1728 memset(&sin6, 0, sizeof(sin6));
1729 sin6.sin6_family = AF_INET6;
1730 sin6.sin6_addr = *addr;
1732 memset(&msg, 0, sizeof(msg));
1733 msg.msg_name = (caddr_t)&sin6;
1734 msg.msg_namelen = sizeof(sin6);
1737 msg.msg_control = NULL;
1738 msg.msg_controllen = 0;
1739 iov.iov_base = (caddr_t)buf;
1740 iov.iov_len = sizeof(struct icmp6_fqdn_query);
1743 msg.msg_control = cbuf;
1744 msg.msg_controllen = sizeof(cbuf);
1745 cmsg = CMSG_FIRSTHDR(&msg);
1746 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1747 cmsg->cmsg_level = IPPROTO_IPV6;
1748 cmsg->cmsg_type = IPV6_PKTINFO;
1749 pkt = (struct in6_pktinfo *)&cmsg[1];
1750 memset(&pkt->ipi6_addr, 0, sizeof(struct in6_addr));
1751 pkt->ipi6_ifindex = ifindex;
1752 cmsg = CMSG_NXTHDR(&msg, cmsg);
1753 msg.msg_controllen = (char *)cmsg - cbuf;
1756 if ((s = _socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
1758 _setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER,
1759 (char *)&filter, sizeof(filter));
1760 cc = _sendmsg(s, &msg, 0);
1768 if (select(s + 1, &fds, NULL, NULL, &tout) <= 0) {
1773 cc = _recvfrom(s, buf, sizeof(buf), 0,
1774 (struct sockaddr *)&sin6, &len);
1779 if (cc < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr))
1781 if (!IN6_ARE_ADDR_EQUAL(addr, &sin6.sin6_addr))
1783 fr = (struct icmp6_fqdn_reply *)(buf + sizeof(struct ip6_hdr));
1784 if (fr->icmp6_fqdn_type == ICMP6_FQDN_REPLY)
1788 if (fr->icmp6_fqdn_cookie[1] != 0) {
1790 name = buf + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 4;
1791 len = (buf + cc) - name;
1793 len = fr->icmp6_fqdn_namelen;
1794 name = fr->icmp6_fqdn_name;
1800 if ((hc = (struct _icmp_host_cache *)malloc(sizeof(*hc))) == NULL)
1802 /* XXX: limit number of cached entries */
1803 hc->hc_ifindex = ifindex;
1804 hc->hc_addr = *addr;
1805 hc->hc_name = strdup(name);
1806 hc->hc_next = hc_head;
1811 static struct hostent *
1812 _icmp_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
1816 struct in6_addr addr6;
1818 if (af != AF_INET6) {
1820 * Note: rfc1788 defines Who Are You for IPv4,
1821 * but no one implements it.
1826 memcpy(&addr6, addr, addrlen);
1827 ifindex = (addr6.s6_addr[2] << 8) | addr6.s6_addr[3];
1828 addr6.s6_addr[2] = addr6.s6_addr[3] = 0;
1830 if (!IN6_IS_ADDR_LINKLOCAL(&addr6))
1831 return NULL; /*XXX*/
1833 if ((hname = _icmp_fqdn_query(&addr6, ifindex)) == NULL)
1835 return _hpaddr(af, hname, &addr6, errp);