1 /* $KAME: name6.c,v 1.25 2000/06/26 16:44:40 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * ++Copyright++ 1985, 1988, 1993
34 * Copyright (c) 1985, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
67 * Permission to use, copy, modify, and distribute this software for any
68 * purpose with or without fee is hereby granted, provided that the above
69 * copyright notice and this permission notice appear in all copies, and that
70 * the name of Digital Equipment Corporation not be used in advertising or
71 * publicity pertaining to distribution of the document or software without
72 * specific, written prior permission.
74 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
75 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
76 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
77 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
78 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
79 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
80 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
87 * Atsushi Onoe <onoe@sm.sony.co.jp>
90 #include <sys/cdefs.h>
91 __FBSDID("$FreeBSD$");
93 #include "namespace.h"
95 #include "reentrant.h"
97 #include <sys/param.h>
98 #include <sys/socket.h>
100 #include <sys/queue.h>
101 #include <netinet/in.h>
104 #include <net/if_var.h>
105 #include <sys/sysctl.h>
106 #include <sys/ioctl.h>
107 #include <netinet6/in6_var.h> /* XXX */
110 #include <arpa/inet.h>
111 #include <arpa/nameser.h>
120 #include <nsswitch.h>
122 #include "un-namespace.h"
123 #include "netdb_private.h"
124 #include "res_config.h"
127 #define _PATH_HOSTS "/etc/hosts"
131 #define MAXALIASES 10
137 #define MAXDNAME 1025
141 #define ADDRLEN(af) ((af) == AF_INET6 ? sizeof(struct in6_addr) : \
142 sizeof(struct in_addr))
144 #define ADDRLEN(af) sizeof(struct in_addr)
147 #define MAPADDR(ab, ina) \
149 memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr)); \
150 memset((ab)->map_zero, 0, sizeof((ab)->map_zero)); \
151 memset((ab)->map_one, 0xff, sizeof((ab)->map_one)); \
153 #define MAPADDRENABLED(flags) \
154 (((flags) & AI_V4MAPPED) || \
155 (((flags) & AI_V4MAPPED_CFG) && _mapped_addr_enabled()))
158 struct in_addr in_addr;
160 struct in6_addr in6_addr;
165 struct in_addr mau_inaddr;
167 #define map_zero map_addr_un.mau_zero
168 #define map_one map_addr_un.mau_one
169 #define map_inaddr map_addr_un.mau_inaddr
173 TAILQ_ENTRY(policyqueue) pc_entry;
175 struct in6_addrpolicy pc_policy;
178 TAILQ_HEAD(policyhead, policyqueue);
180 #define AIO_SRCFLAG_DEPRECATED 0x1
184 struct sockaddr_storage aiou_ss;
185 struct sockaddr aiou_sa;
187 #define aio_srcsa aio_src_un.aiou_sa
188 u_int32_t aio_srcflag;
191 struct policyqueue *aio_srcpolicy;
192 struct policyqueue *aio_dstpolicy;
194 struct sockaddr_storage aiou_ss;
195 struct sockaddr aiou_sa;
197 #define aio_sa aio_un.aiou_sa
202 static struct hostent *_hpcopy(struct hostent *hp, int *errp);
203 static struct hostent *_hpaddr(int af, const char *name, void *addr, int *errp);
204 static struct hostent *_hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp);
206 static struct hostent *_hpmapv6(struct hostent *hp, int *errp);
208 static struct hostent *_hpsort(struct hostent *hp);
209 static struct hostent *_ghbyname(const char *name, int af, int flags, int *errp);
210 static char *_hgetword(char **pp);
211 static int _mapped_addr_enabled(void);
213 static struct hostent *_hpreorder(struct hostent *hp);
214 static int get_addrselectpolicy(struct policyhead *);
215 static void free_addrselectpolicy(struct policyhead *);
216 static struct policyqueue *match_addrselectpolicy(struct sockaddr *,
217 struct policyhead *);
218 static void set_source(struct hp_order *, struct policyhead *);
219 static int matchlen(struct sockaddr *, struct sockaddr *);
220 static int comp_dst(const void *, const void *);
221 static int gai_addr2scopetype(struct sockaddr *);
223 static FILE *_files_open(int *errp);
224 static int _files_ghbyname(void *, void *, va_list);
225 static int _files_ghbyaddr(void *, void *, va_list);
227 static int _nis_ghbyname(void *, void *, va_list);
228 static int _nis_ghbyaddr(void *, void *, va_list);
230 static int _dns_ghbyname(void *, void *, va_list);
231 static int _dns_ghbyaddr(void *, void *, va_list);
232 static void _dns_shent(int stayopen) __unused;
233 static void _dns_ehent(void) __unused;
235 static int _icmp_ghbyaddr(void *, void *, va_list);
239 static mutex_t _getipnodeby_thread_lock = MUTEX_INITIALIZER;
240 #define THREAD_LOCK() mutex_lock(&_getipnodeby_thread_lock);
241 #define THREAD_UNLOCK() mutex_unlock(&_getipnodeby_thread_lock);
244 /* Host lookup order if nsswitch.conf is broken or nonexistant */
245 static const ns_src default_src[] = {
246 { NSSRC_FILES, NS_SUCCESS },
247 { NSSRC_DNS, NS_SUCCESS },
249 #define NSSRC_ICMP "icmp"
250 { NSSRC_ICMP, NS_SUCCESS },
256 * Check if kernel supports mapped address.
257 * implementation dependent
260 #include <sys/sysctl.h>
261 #endif /* __KAME__ */
264 _mapped_addr_enabled(void)
266 /* implementation dependent check */
267 #if defined(__KAME__) && defined(IPV6CTL_MAPPED_ADDR)
274 mib[2] = IPPROTO_IPV6;
275 mib[3] = IPV6CTL_MAPPED_ADDR;
277 if (sysctl(mib, 4, &val, &len, 0, 0) == 0 && val != 0)
279 #endif /* __KAME__ && IPV6CTL_MAPPED_ADDR */
284 * Functions defined in RFC2553
285 * getipnodebyname, getipnodebyaddr, freehostent
288 static struct hostent *
289 _ghbyname(const char *name, int af, int flags, int *errp)
294 static const ns_dtab dtab[] = {
295 NS_FILES_CB(_files_ghbyname, NULL)
296 { NSSRC_DNS, _dns_ghbyname, NULL },
297 NS_NIS_CB(_nis_ghbyname, NULL)
301 if (flags & AI_ADDRCONFIG) {
304 if ((s = _socket(af, SOCK_DGRAM, 0)) < 0)
308 * Note that implementation dependent test for address
309 * configuration should be done everytime called
310 * (or apropriate interval),
311 * because addresses will be dynamically assigned or deleted.
316 rval = _nsdispatch(&hp, dtab, NSDB_HOSTS, "ghbyname", default_src,
318 return (rval == NS_SUCCESS) ? hp : NULL;
322 getipnodebyname(const char *name, int af, int flags, int *errp)
325 union inx_addr addrbuf;
339 /* special case for literal address */
340 if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
341 if (af != AF_INET6) {
342 *errp = HOST_NOT_FOUND;
345 return _hpaddr(af, name, &addrbuf, errp);
348 if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) {
350 if (MAPADDRENABLED(flags)) {
351 MAPADDR(&addrbuf, &addrbuf.in_addr);
353 *errp = HOST_NOT_FOUND;
357 return _hpaddr(af, name, &addrbuf, errp);
360 *errp = HOST_NOT_FOUND;
361 hp = _ghbyname(name, af, flags, errp);
364 if (af == AF_INET6 && ((flags & AI_ALL) || hp == NULL) &&
365 MAPADDRENABLED(flags)) {
366 struct hostent *hp2 = _ghbyname(name, AF_INET, flags, errp);
368 hp = _hpmapv6(hp2, errp);
370 if (hp2 && strcmp(hp->h_name, hp2->h_name) != 0) {
374 hp = _hpmerge(hp, hp2, errp);
378 return _hpreorder(_hpsort(hp));
382 getipnodebyaddr(const void *src, size_t len, int af, int *errp)
387 struct in6_addr addrbuf;
389 struct in_addr addrbuf;
392 static const ns_dtab dtab[] = {
393 NS_FILES_CB(_files_ghbyaddr, NULL)
394 { NSSRC_DNS, _dns_ghbyaddr, NULL },
395 NS_NIS_CB(_nis_ghbyaddr, NULL)
397 { NSSRC_ICMP, _icmp_ghbyaddr, NULL },
402 *errp = HOST_NOT_FOUND;
406 if (len != sizeof(struct in_addr)) {
410 if ((long)src & ~(sizeof(struct in_addr) - 1)) {
411 memcpy(&addrbuf, src, len);
414 if (((struct in_addr *)src)->s_addr == 0)
419 if (len != sizeof(struct in6_addr)) {
423 if ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) { /*XXX*/
424 memcpy(&addrbuf, src, len);
427 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src))
429 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)
430 || IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) {
432 (sizeof(struct in6_addr) - sizeof(struct in_addr));
434 len = sizeof(struct in_addr);
443 rval = _nsdispatch(&hp, dtab, NSDB_HOSTS, "ghbyaddr", default_src,
445 return (rval == NS_SUCCESS) ? hp : NULL;
449 freehostent(struct hostent *ptr)
455 * Private utility functions
459 * _hpcopy: allocate and copy hostent structure
461 static struct hostent *
462 _hpcopy(struct hostent *hp, int *errp)
467 int nalias = 0, naddr = 0;
474 /* count size to be allocated */
475 size = sizeof(struct hostent);
476 if (hp->h_name != NULL)
477 size += strlen(hp->h_name) + 1;
478 if ((pp = hp->h_aliases) != NULL) {
479 for (i = 0; *pp != NULL; i++, pp++) {
481 size += strlen(*pp) + 1;
486 /* adjust alignment */
489 size += sizeof(char *) * (nalias + 1);
490 addrsize = ALIGN(hp->h_length);
491 if ((pp = hp->h_addr_list) != NULL) {
492 while (*pp++ != NULL)
495 size += addrsize * naddr;
496 size += sizeof(char *) * (naddr + 1);
499 if ((nhp = (struct hostent *)malloc(size)) == NULL) {
503 cp = (char *)&nhp[1];
504 if (hp->h_name != NULL) {
506 strcpy(cp, hp->h_name);
507 cp += strlen(cp) + 1;
510 nhp->h_aliases = (char **)((char *)nhp + al_off);
511 if ((pp = hp->h_aliases) != NULL) {
512 for (i = 0; *pp != NULL; pp++) {
514 nhp->h_aliases[i++] = cp;
516 cp += strlen(cp) + 1;
520 nhp->h_aliases[nalias] = NULL;
521 cp = (char *)&nhp->h_aliases[nalias + 1];
522 nhp->h_addrtype = hp->h_addrtype;
523 nhp->h_length = hp->h_length;
524 nhp->h_addr_list = (char **)cp;
525 if ((pp = hp->h_addr_list) != NULL) {
526 cp = (char *)&nhp->h_addr_list[naddr + 1];
527 for (i = 0; *pp != NULL; pp++) {
528 nhp->h_addr_list[i++] = cp;
529 memcpy(cp, *pp, hp->h_length);
533 nhp->h_addr_list[naddr] = NULL;
538 * _hpaddr: construct hostent structure with one address
540 static struct hostent *
541 _hpaddr(int af, const char *name, void *addr, int *errp)
543 struct hostent *hp, hpbuf;
547 hp->h_name = (char *)name;
548 hp->h_aliases = NULL;
550 hp->h_length = ADDRLEN(af);
551 hp->h_addr_list = addrs;
552 addrs[0] = (char *)addr;
554 return _hpcopy(hp, errp);
558 * _hpmerge: merge 2 hostent structure, arguments will be freed
560 static struct hostent *
561 _hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp)
566 struct hostent *hp, hpbuf;
567 char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1];
568 union inx_addr addrbuf[MAXADDRS];
575 #define HP(i) (i == 1 ? hp1 : hp2)
577 hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name);
578 hp->h_aliases = aliases;
580 for (i = 1; i <= 2; i++) {
581 if ((pp = HP(i)->h_aliases) == NULL)
583 for (; nalias < MAXALIASES && *pp != NULL; pp++) {
584 /* check duplicates */
585 for (j = 0; j < nalias; j++)
586 if (strcasecmp(*pp, aliases[j]) == 0)
589 aliases[nalias++] = *pp;
592 aliases[nalias] = NULL;
594 if (hp1->h_length != hp2->h_length) {
595 hp->h_addrtype = AF_INET6;
596 hp->h_length = sizeof(struct in6_addr);
599 hp->h_addrtype = hp1->h_addrtype;
600 hp->h_length = hp1->h_length;
604 hp->h_addr_list = addrs;
606 for (i = 1; i <= 2; i++) {
607 if ((pp = HP(i)->h_addr_list) == NULL)
609 if (HP(i)->h_length == hp->h_length) {
610 while (naddr < MAXADDRS && *pp != NULL)
611 addrs[naddr++] = *pp++;
613 /* copy IPv4 addr as mapped IPv6 addr */
614 while (naddr < MAXADDRS && *pp != NULL) {
615 MAPADDR(&addrbuf[naddr], *pp++);
616 addrs[naddr] = (char *)&addrbuf[naddr];
622 hp = _hpcopy(hp, errp);
629 * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses
632 static struct hostent *
633 _hpmapv6(struct hostent *hp, int *errp)
639 if (hp->h_addrtype == AF_INET6)
642 /* make dummy hostent to convert IPv6 address */
643 if ((hp6 = (struct hostent *)malloc(sizeof(struct hostent))) == NULL) {
648 hp6->h_aliases = NULL;
649 hp6->h_addrtype = AF_INET6;
650 hp6->h_length = sizeof(struct in6_addr);
651 hp6->h_addr_list = NULL;
652 return _hpmerge(hp6, hp, errp);
657 * _hpsort: sort address by sortlist
659 static struct hostent *
660 _hpsort(struct hostent *hp)
663 u_char *ap, *sp, *mp, **pp;
665 char order[MAXADDRS];
666 int nsort = _res.nsort;
668 if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0)
670 for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
671 for (j = 0; j < nsort; j++) {
673 if (_res_ext.sort_list[j].af != hp->h_addrtype)
675 sp = (u_char *)&_res_ext.sort_list[j].addr;
676 mp = (u_char *)&_res_ext.sort_list[j].mask;
678 sp = (u_char *)&_res.sort_list[j].addr;
679 mp = (u_char *)&_res.sort_list[j].mask;
681 for (n = 0; n < hp->h_length; n++) {
682 if ((ap[n] & mp[n]) != sp[n])
685 if (n == hp->h_length)
691 pp = (u_char **)hp->h_addr_list;
692 for (i = 0; i < n - 1; i++) {
693 for (j = i + 1; j < n; j++) {
694 if (order[i] > order[j]) {
712 static const char sep[] = "# \t\n";
715 for (p = *pp; (c = *p) != '\0'; p++) {
716 for (sp = sep; *sp != '\0'; sp++) {
721 p[1] = '\0'; /* ignore rest of line */
733 if (ret == NULL || *ret == '\0')
739 * _hpreorder: sort address by default address selection
741 static struct hostent *
742 _hpreorder(struct hostent *hp)
744 struct hp_order *aio;
748 struct policyhead policyhead;
753 switch (hp->h_addrtype) {
760 free_addrselectpolicy(&policyhead);
764 /* count the number of addrinfo elements for sorting. */
765 for (n = 0; hp->h_addr_list[n] != NULL; n++)
769 * If the number is small enough, we can skip the reordering process.
774 /* allocate a temporary array for sort and initialization of it. */
775 if ((aio = malloc(sizeof(*aio) * n)) == NULL)
776 return hp; /* give up reordering */
777 memset(aio, 0, sizeof(*aio) * n);
779 /* retrieve address selection policy from the kernel */
780 TAILQ_INIT(&policyhead);
781 if (!get_addrselectpolicy(&policyhead)) {
782 /* no policy is installed into kernel, we don't sort. */
787 for (i = 0; i < n; i++) {
788 ap = hp->h_addr_list[i];
789 aio[i].aio_h_addr = ap;
791 switch (hp->h_addrtype) {
793 sa->sa_family = AF_INET;
794 sa->sa_len = sizeof(struct sockaddr_in);
795 memcpy(&((struct sockaddr_in *)sa)->sin_addr, ap,
796 sizeof(struct in_addr));
800 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
801 sa->sa_family = AF_INET;
802 sa->sa_len = sizeof(struct sockaddr_in);
803 memcpy(&((struct sockaddr_in *)sa)->sin_addr,
804 &ap[12], sizeof(struct in_addr));
806 sa->sa_family = AF_INET6;
807 sa->sa_len = sizeof(struct sockaddr_in6);
808 memcpy(&((struct sockaddr_in6 *)sa)->sin6_addr,
809 ap, sizeof(struct in6_addr));
814 aio[i].aio_dstscope = gai_addr2scopetype(sa);
815 aio[i].aio_dstpolicy = match_addrselectpolicy(sa, &policyhead);
816 set_source(&aio[i], &policyhead);
819 /* perform sorting. */
820 qsort(aio, n, sizeof(*aio), comp_dst);
822 /* reorder the h_addr_list. */
823 for (i = 0; i < n; i++)
824 hp->h_addr_list[i] = aio[i].aio_h_addr;
826 /* cleanup and return */
828 free_addrselectpolicy(&policyhead);
833 get_addrselectpolicy(head)
834 struct policyhead *head;
837 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
840 struct in6_addrpolicy *pol, *ep;
842 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
844 if ((buf = malloc(l)) == NULL)
846 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
851 ep = (struct in6_addrpolicy *)(buf + l);
852 for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
853 struct policyqueue *new;
855 if ((new = malloc(sizeof(*new))) == NULL) {
856 free_addrselectpolicy(head); /* make the list empty */
859 new->pc_policy = *pol;
860 TAILQ_INSERT_TAIL(head, new, pc_entry);
871 free_addrselectpolicy(head)
872 struct policyhead *head;
874 struct policyqueue *ent, *nent;
876 for (ent = TAILQ_FIRST(head); ent; ent = nent) {
877 nent = TAILQ_NEXT(ent, pc_entry);
878 TAILQ_REMOVE(head, ent, pc_entry);
883 static struct policyqueue *
884 match_addrselectpolicy(addr, head)
885 struct sockaddr *addr;
886 struct policyhead *head;
889 struct policyqueue *ent, *bestent = NULL;
890 struct in6_addrpolicy *pol;
891 int matchlen, bestmatchlen = -1;
892 u_char *mp, *ep, *k, *p, m;
893 struct sockaddr_in6 key;
895 switch(addr->sa_family) {
897 key = *(struct sockaddr_in6 *)addr;
900 /* convert the address into IPv4-mapped IPv6 address. */
901 memset(&key, 0, sizeof(key));
902 key.sin6_family = AF_INET6;
903 key.sin6_len = sizeof(key);
904 key.sin6_addr.s6_addr[10] = 0xff;
905 key.sin6_addr.s6_addr[11] = 0xff;
906 memcpy(&key.sin6_addr.s6_addr[12],
907 &((struct sockaddr_in *)addr)->sin_addr, 4);
913 for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
914 pol = &ent->pc_policy;
917 mp = (u_char *)&pol->addrmask.sin6_addr;
918 ep = mp + 16; /* XXX: scope field? */
919 k = (u_char *)&key.sin6_addr;
920 p = (u_char *)&pol->addr.sin6_addr;
921 for (; mp < ep && *mp; mp++, k++, p++) {
924 goto next; /* not match */
925 if (m == 0xff) /* short cut for a typical case */
935 /* matched. check if this is better than the current best. */
936 if (matchlen > bestmatchlen) {
938 bestmatchlen = matchlen;
954 struct hp_order *aio;
955 struct policyhead *ph;
957 struct sockaddr_storage ss = aio->aio_un.aiou_ss;
961 /* set unspec ("no source is available"), just in case */
962 aio->aio_srcsa.sa_family = AF_UNSPEC;
963 aio->aio_srcscope = -1;
965 switch(ss.ss_family) {
967 ((struct sockaddr_in *)&ss)->sin_port = htons(1);
971 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(1);
974 default: /* ignore unsupported AFs explicitly */
978 /* open a socket to get the source address for the given dst */
979 if ((s = _socket(ss.ss_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
980 return; /* give up */
981 if (_connect(s, (struct sockaddr *)&ss, ss.ss_len) < 0)
984 if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
985 aio->aio_srcsa.sa_family = AF_UNSPEC;
988 aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
989 aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
990 aio->aio_matchlen = matchlen(&aio->aio_srcsa, (struct sockaddr *)&ss);
992 if (ss.ss_family == AF_INET6) {
993 struct in6_ifreq ifr6;
996 /* XXX: interface name should not be hardcoded */
997 strncpy(ifr6.ifr_name, "lo0", sizeof(ifr6.ifr_name));
998 memset(&ifr6, 0, sizeof(ifr6));
999 memcpy(&ifr6.ifr_addr, &ss, ss.ss_len);
1000 if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
1001 flags6 = ifr6.ifr_ifru.ifru_flags6;
1002 if ((flags6 & IN6_IFF_DEPRECATED))
1003 aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
1015 struct sockaddr *src, *dst;
1022 switch (src->sa_family) {
1025 s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
1026 d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
1027 addrlen = sizeof(struct in6_addr);
1032 s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
1033 d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
1034 addrlen = sizeof(struct in_addr);
1042 if ((r = (*d++ ^ *s++)) != 0) {
1043 while (r < addrlen * 8) {
1054 comp_dst(arg1, arg2)
1055 const void *arg1, *arg2;
1057 const struct hp_order *dst1 = arg1, *dst2 = arg2;
1060 * Rule 1: Avoid unusable destinations.
1061 * XXX: we currently do not consider if an appropriate route exists.
1063 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
1064 dst2->aio_srcsa.sa_family == AF_UNSPEC) {
1067 if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
1068 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
1072 /* Rule 2: Prefer matching scope. */
1073 if (dst1->aio_dstscope == dst1->aio_srcscope &&
1074 dst2->aio_dstscope != dst2->aio_srcscope) {
1077 if (dst1->aio_dstscope != dst1->aio_srcscope &&
1078 dst2->aio_dstscope == dst2->aio_srcscope) {
1082 /* Rule 3: Avoid deprecated addresses. */
1083 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
1084 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
1085 if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
1086 (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
1089 if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
1090 !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
1095 /* Rule 4: Prefer home addresses. */
1096 /* XXX: not implemented yet */
1098 /* Rule 5: Prefer matching label. */
1100 if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
1101 dst1->aio_srcpolicy->pc_policy.label ==
1102 dst1->aio_dstpolicy->pc_policy.label &&
1103 (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
1104 dst2->aio_srcpolicy->pc_policy.label !=
1105 dst2->aio_dstpolicy->pc_policy.label)) {
1108 if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
1109 dst2->aio_srcpolicy->pc_policy.label ==
1110 dst2->aio_dstpolicy->pc_policy.label &&
1111 (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
1112 dst1->aio_srcpolicy->pc_policy.label !=
1113 dst1->aio_dstpolicy->pc_policy.label)) {
1118 /* Rule 6: Prefer higher precedence. */
1120 if (dst1->aio_dstpolicy &&
1121 (dst2->aio_dstpolicy == NULL ||
1122 dst1->aio_dstpolicy->pc_policy.preced >
1123 dst2->aio_dstpolicy->pc_policy.preced)) {
1126 if (dst2->aio_dstpolicy &&
1127 (dst1->aio_dstpolicy == NULL ||
1128 dst2->aio_dstpolicy->pc_policy.preced >
1129 dst1->aio_dstpolicy->pc_policy.preced)) {
1134 /* Rule 7: Prefer native transport. */
1135 /* XXX: not implemented yet */
1137 /* Rule 8: Prefer smaller scope. */
1138 if (dst1->aio_dstscope >= 0 &&
1139 dst1->aio_dstscope < dst2->aio_dstscope) {
1142 if (dst2->aio_dstscope >= 0 &&
1143 dst2->aio_dstscope < dst1->aio_dstscope) {
1148 * Rule 9: Use longest matching prefix.
1149 * We compare the match length in a same AF only.
1151 if (dst1->aio_sa.sa_family == dst2->aio_sa.sa_family) {
1152 if (dst1->aio_matchlen > dst2->aio_matchlen) {
1155 if (dst1->aio_matchlen < dst2->aio_matchlen) {
1160 /* Rule 10: Otherwise, leave the order unchanged. */
1165 * Copy from scope.c.
1166 * XXX: we should standardize the functions and link them as standard
1170 gai_addr2scopetype(sa)
1171 struct sockaddr *sa;
1174 struct sockaddr_in6 *sa6;
1176 struct sockaddr_in *sa4;
1178 switch(sa->sa_family) {
1181 sa6 = (struct sockaddr_in6 *)sa;
1182 if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1183 /* just use the scope field of the multicast address */
1184 return(sa6->sin6_addr.s6_addr[2] & 0x0f);
1187 * Unicast addresses: map scope type to corresponding scope
1188 * value defined for multcast addresses.
1189 * XXX: hardcoded scope type values are bad...
1191 if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1192 return(1); /* node local scope */
1193 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1194 return(2); /* link-local scope */
1195 if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1196 return(5); /* site-local scope */
1197 return(14); /* global scope */
1202 * IPv4 pseudo scoping according to RFC 3484.
1204 sa4 = (struct sockaddr_in *)sa;
1205 /* IPv4 autoconfiguration addresses have link-local scope. */
1206 if (((u_char *)&sa4->sin_addr)[0] == 169 &&
1207 ((u_char *)&sa4->sin_addr)[1] == 254)
1209 /* Private addresses have site-local scope. */
1210 if (((u_char *)&sa4->sin_addr)[0] == 10 ||
1211 (((u_char *)&sa4->sin_addr)[0] == 172 &&
1212 (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
1213 (((u_char *)&sa4->sin_addr)[0] == 192 &&
1214 ((u_char *)&sa4->sin_addr)[1] == 168))
1215 return(14); /* XXX: It should be 5 unless NAT */
1216 /* Loopback addresses have link-local scope. */
1217 if (((u_char *)&sa4->sin_addr)[0] == 127)
1222 errno = EAFNOSUPPORT; /* is this a good error? */
1228 * FILES (/etc/hosts)
1232 _files_open(int *errp)
1235 fp = fopen(_PATH_HOSTS, "r");
1237 *errp = NO_RECOVERY;
1242 _files_ghbyname(void *rval, void *cb_data, va_list ap)
1248 char *p, *line, *addrstr, *cname;
1250 struct hostent *rethp, *hp, hpbuf;
1251 char *aliases[MAXALIASES + 1], *addrs[2];
1252 union inx_addr addrbuf;
1255 name = va_arg(ap, const char *);
1256 af = va_arg(ap, int);
1257 errp = va_arg(ap, int *);
1259 *(struct hostent **)rval = NULL;
1261 if ((fp = _files_open(errp)) == NULL)
1265 while (fgets(buf, sizeof(buf), fp)) {
1267 if ((addrstr = _hgetword(&line)) == NULL
1268 || (cname = _hgetword(&line)) == NULL)
1270 match = (strcasecmp(cname, name) == 0);
1272 while ((p = _hgetword(&line)) != NULL) {
1274 match = (strcasecmp(p, name) == 0);
1275 if (nalias < MAXALIASES)
1276 aliases[nalias++] = p;
1282 if (inet_aton(addrstr, (struct in_addr *)&addrbuf)
1284 *errp = NO_DATA; /* name found */
1290 if (inet_pton(af, addrstr, &addrbuf) != 1) {
1291 *errp = NO_DATA; /* name found */
1299 hp->h_aliases = aliases;
1300 aliases[nalias] = NULL;
1301 hp->h_addrtype = af;
1302 hp->h_length = ADDRLEN(af);
1303 hp->h_addr_list = addrs;
1304 addrs[0] = (char *)&addrbuf;
1306 hp = _hpcopy(hp, errp);
1307 rethp = _hpmerge(rethp, hp, errp);
1310 *(struct hostent **)rval = rethp;
1311 return (rethp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
1315 _files_ghbyaddr(void *rval, void *cb_data, va_list ap)
1324 struct hostent *hp, hpbuf;
1325 char *aliases[MAXALIASES + 1], *addrs[2];
1326 union inx_addr addrbuf;
1329 addr = va_arg(ap, const void *);
1330 addrlen = va_arg(ap, int);
1331 af = va_arg(ap, int);
1332 errp = va_arg(ap, int *);
1334 *(struct hostent**)rval = NULL;
1336 if ((fp = _files_open(errp)) == NULL)
1339 while (fgets(buf, sizeof(buf), fp)) {
1341 if ((p = _hgetword(&line)) == NULL
1343 ? inet_aton(p, (struct in_addr *)&addrbuf)
1344 : inet_pton(af, p, &addrbuf)) != 1
1345 || memcmp(addr, &addrbuf, addrlen) != 0
1346 || (p = _hgetword(&line)) == NULL)
1350 hp->h_aliases = aliases;
1352 while ((p = _hgetword(&line)) != NULL) {
1353 if (nalias < MAXALIASES)
1354 aliases[nalias++] = p;
1356 aliases[nalias] = NULL;
1357 hp->h_addrtype = af;
1358 hp->h_length = addrlen;
1359 hp->h_addr_list = addrs;
1360 addrs[0] = (char *)&addrbuf;
1362 hp = _hpcopy(hp, errp);
1366 *(struct hostent **)rval = hp;
1367 return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
1374 * XXX actually a hack.
1377 _nis_ghbyname(void *rval, void *cb_data, va_list ap)
1382 struct hostent *hp = NULL;
1384 name = va_arg(ap, const char *);
1385 af = va_arg(ap, int);
1386 errp = va_arg(ap, int *);
1388 hp = _gethostbynisname(name, af);
1390 hp = _hpcopy(hp, errp);
1392 *(struct hostent **)rval = hp;
1393 return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
1397 _nis_ghbyaddr(void *rval, void *cb_data, va_list ap)
1403 struct hostent *hp = NULL;
1405 addr = va_arg(ap, const void *);
1406 addrlen = va_arg(ap, int);
1407 af = va_arg(ap, int);
1409 hp = _gethostbynisaddr(addr, addrlen, af);
1411 hp = _hpcopy(hp, errp);
1412 *(struct hostent **)rval = hp;
1413 return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
1417 #define MAXPACKET (64*1024)
1421 u_char buf[MAXPACKET];
1424 static struct hostent *getanswer(const querybuf *, int, const char *, int,
1425 struct hostent *, int *);
1428 * we don't need to take care about sorting, nor IPv4 mapped address here.
1430 static struct hostent *
1431 getanswer(answer, anslen, qname, qtype, template, errp)
1432 const querybuf *answer;
1436 struct hostent *template;
1442 const u_char *eom, *erdata;
1443 char *bp, *ep, **ap, **hap;
1444 int type, class, ancount, qdcount;
1445 int haveanswer, had_error;
1446 char tbuf[MAXDNAME];
1448 int (*name_ok)(const char *);
1449 static char *h_addr_ptrs[MAXADDRS + 1];
1450 static char *host_aliases[MAXALIASES];
1451 static char hostbuf[8*1024];
1453 #define BOUNDED_INCR(x) \
1457 *errp = NO_RECOVERY; \
1462 #define BOUNDS_CHECK(ptr, count) \
1464 if ((ptr) + (count) > eom) { \
1465 *errp = NO_RECOVERY; \
1470 /* XXX do {} while (0) cannot be put here */
1471 #define DNS_ASSERT(x) \
1479 /* XXX do {} while (0) cannot be put here */
1480 #define DNS_FATAL(x) \
1489 template->h_name = NULL;
1490 eom = answer->buf + anslen;
1500 return (NULL); /* XXX should be abort(); */
1503 * find first satisfactory answer
1506 ancount = ntohs(hp->ancount);
1507 qdcount = ntohs(hp->qdcount);
1509 ep = hostbuf + sizeof hostbuf;
1511 BOUNDED_INCR(HFIXEDSZ);
1513 *errp = NO_RECOVERY;
1516 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1517 if ((n < 0) || !(*name_ok)(bp)) {
1518 *errp = NO_RECOVERY;
1521 BOUNDED_INCR(n + QFIXEDSZ);
1522 if (qtype == T_A || qtype == T_AAAA) {
1523 /* res_send() has already verified that the query name is the
1524 * same as the one we sent; this just gets the expanded name
1525 * (i.e., with the succeeding search-domain tacked on).
1527 n = strlen(bp) + 1; /* for the \0 */
1528 if (n >= MAXHOSTNAMELEN) {
1529 *errp = NO_RECOVERY;
1532 template->h_name = bp;
1534 /* The qname can be abbreviated, but h_name is now absolute. */
1535 qname = template->h_name;
1539 template->h_aliases = host_aliases;
1542 template->h_addr_list = h_addr_ptrs;
1545 while (ancount-- > 0 && cp < eom && !had_error) {
1546 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1548 DNS_FATAL((*name_ok)(bp));
1550 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
1551 type = _getshort(cp);
1552 cp += INT16SZ; /* type */
1553 class = _getshort(cp);
1554 cp += INT16SZ + INT32SZ; /* class, TTL */
1556 cp += INT16SZ; /* len */
1557 BOUNDS_CHECK(cp, n);
1559 DNS_ASSERT(class == C_IN);
1560 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
1561 if (ap >= &host_aliases[MAXALIASES-1])
1563 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1565 DNS_FATAL((*name_ok)(tbuf));
1568 *errp = NO_RECOVERY;
1573 n = strlen(bp) + 1; /* for the \0 */
1574 DNS_FATAL(n < MAXHOSTNAMELEN);
1576 /* Get canonical name. */
1577 n = strlen(tbuf) + 1; /* for the \0 */
1578 DNS_FATAL(n <= ep - bp);
1579 DNS_FATAL(n < MAXHOSTNAMELEN);
1581 template->h_name = bp;
1585 if (qtype == T_PTR && type == T_CNAME) {
1586 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1587 if (n < 0 || !res_dnok(tbuf)) {
1593 *errp = NO_RECOVERY;
1596 /* Get canonical name. */
1597 n = strlen(tbuf) + 1; /* for the \0 */
1598 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1607 DNS_ASSERT(type == qtype);
1610 DNS_ASSERT(strcasecmp(tname, bp) == 0);
1611 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1613 DNS_FATAL(res_hnok(bp));
1614 #if MULTI_PTRS_ARE_ALIASES
1617 *errp = NO_RECOVERY;
1621 template->h_name = bp;
1622 else if (ap < &host_aliases[MAXALIASES-1])
1627 n = strlen(bp) + 1; /* for the \0 */
1628 if (n >= MAXHOSTNAMELEN) {
1636 template->h_name = bp;
1637 *errp = NETDB_SUCCESS;
1642 DNS_ASSERT(strcasecmp(template->h_name, bp) == 0);
1643 DNS_ASSERT(n == template->h_length);
1647 template->h_name = bp;
1648 nn = strlen(bp) + 1; /* for the \0 */
1651 bp = (char *)ALIGN(bp);
1653 DNS_FATAL(bp + n < ep);
1654 DNS_ASSERT(hap < &h_addr_ptrs[MAXADDRS-1]);
1655 #ifdef FILTER_V4MAPPED
1656 if (type == T_AAAA) {
1657 struct in6_addr in6;
1658 memcpy(&in6, cp, sizeof(in6));
1659 DNS_ASSERT(IN6_IS_ADDR_V4MAPPED(&in6) == 0);
1662 bcopy(cp, *hap++ = bp, n);
1666 *errp = NO_RECOVERY;
1679 if (!template->h_name) {
1680 n = strlen(qname) + 1; /* for the \0 */
1681 if (n > ep - bp || n >= MAXHOSTNAMELEN)
1684 template->h_name = bp;
1687 *errp = NETDB_SUCCESS;
1691 *errp = NO_RECOVERY;
1701 _dns_ghbyname(void *rval, void *cb_data, va_list ap)
1709 struct hostent hbuf;
1712 name = va_arg(ap, const char *);
1713 af = va_arg(ap, int);
1714 errp = va_arg(ap, int *);
1716 if ((_res.options & RES_INIT) == 0) {
1717 if (res_init() < 0) {
1722 memset(&hbuf, 0, sizeof(hbuf));
1723 hbuf.h_addrtype = af;
1724 hbuf.h_length = ADDRLEN(af);
1736 *errp = NO_RECOVERY;
1739 buf = malloc(sizeof(*buf));
1741 *errp = NETDB_INTERNAL;
1744 n = res_search(name, C_IN, qtype, buf->buf, sizeof(buf->buf));
1750 hp = getanswer(buf, n, name, qtype, &hbuf, errp);
1753 *errp = NO_RECOVERY;
1756 *(struct hostent **)rval = _hpcopy(&hbuf, errp);
1757 if (*(struct hostent **)rval != NULL)
1759 else if (*errp == TRY_AGAIN)
1766 _dns_ghbyaddr(void *rval, void *cb_data, va_list ap)
1777 struct hostent hbuf;
1779 static const char hex[] = "0123456789abcdef";
1782 char qbuf[MAXDNAME+1];
1784 char *tld6[] = { "ip6.arpa", NULL };
1785 char *tld4[] = { "in-addr.arpa", NULL };
1788 addr = va_arg(ap, const void *);
1789 addrlen = va_arg(ap, int);
1790 af = va_arg(ap, int);
1791 errp = va_arg(ap, int *);
1793 *(struct hostent **)rval = NULL;
1797 if (af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr))
1814 if ((_res.options & RES_INIT) == 0) {
1815 if (res_init() < 0) {
1820 memset(&hbuf, 0, sizeof(hbuf));
1822 hbuf.h_addrtype = af;
1823 hbuf.h_length = addrlen;
1825 buf = malloc(sizeof(*buf));
1827 *errp = NETDB_INTERNAL;
1831 for (/* nothing */; *tld; tld++) {
1833 * XXX assumes that MAXDNAME is big enough - error checks
1834 * has been made by callers
1838 cp = (u_char *)addr+addrlen-1;
1842 for (; n < addrlen; n++, cp--) {
1844 *bp++ = hex[c & 0xf];
1846 *bp++ = hex[c >> 4];
1853 for (; n < addrlen; n++, cp--) {
1856 *bp++ = '0' + c / 100;
1858 *bp++ = '0' + (c % 100) / 10;
1859 *bp++ = '0' + c % 10;
1866 n = res_query(qbuf, C_IN, T_PTR, buf->buf, sizeof buf->buf);
1871 } else if (n > sizeof(buf->buf)) {
1873 errno = ERANGE; /* XXX is it OK to set errno here? */
1875 *errp = NETDB_INTERNAL;
1879 hp = getanswer(buf, n, qbuf, T_PTR, &hbuf, errp);
1885 hbuf.h_addrtype = af;
1886 hbuf.h_length = addrlen;
1887 hbuf.h_addr_list = hlist;
1888 hlist[0] = (char *)addr;
1890 *(struct hostent **)rval = _hpcopy(&hbuf, errp);
1898 _dns_shent(int stayopen)
1900 if ((_res.options & RES_INIT) == 0) {
1905 _res.options |= RES_STAYOPEN | RES_USEVC;
1911 _res.options &= ~(RES_STAYOPEN | RES_USEVC);
1919 * draft-ietf-ipngwg-icmp-namelookups-02.txt
1920 * ifindex is assumed to be encoded in addr.
1922 #include <sys/uio.h>
1923 #include <netinet/ip6.h>
1924 #include <netinet/icmp6.h>
1926 struct _icmp_host_cache {
1927 struct _icmp_host_cache *hc_next;
1929 struct in6_addr hc_addr;
1934 _icmp_fqdn_query(const struct in6_addr *addr, int ifindex)
1937 struct icmp6_filter filter;
1939 struct cmsghdr *cmsg;
1940 struct in6_pktinfo *pkt;
1944 struct icmp6_fqdn_query *fq;
1945 struct icmp6_fqdn_reply *fr;
1946 struct _icmp_host_cache *hc;
1947 struct sockaddr_in6 sin6;
1950 struct timeval tout;
1953 static struct _icmp_host_cache *hc_head;
1956 for (hc = hc_head; hc; hc = hc->hc_next) {
1957 if (hc->hc_ifindex == ifindex
1958 && IN6_ARE_ADDR_EQUAL(&hc->hc_addr, addr)) {
1960 return hc->hc_name; /* XXX: never freed */
1965 ICMP6_FILTER_SETBLOCKALL(&filter);
1966 ICMP6_FILTER_SETPASS(ICMP6_FQDN_REPLY, &filter);
1970 tout.tv_usec = 200000; /*XXX: 200ms*/
1972 fq = (struct icmp6_fqdn_query *)buf;
1973 fq->icmp6_fqdn_type = ICMP6_FQDN_QUERY;
1974 fq->icmp6_fqdn_code = 0;
1975 fq->icmp6_fqdn_cksum = 0;
1976 fq->icmp6_fqdn_id = (u_short)getpid();
1977 fq->icmp6_fqdn_unused = 0;
1978 fq->icmp6_fqdn_cookie[0] = 0;
1979 fq->icmp6_fqdn_cookie[1] = 0;
1981 memset(&sin6, 0, sizeof(sin6));
1982 sin6.sin6_family = AF_INET6;
1983 sin6.sin6_addr = *addr;
1985 memset(&msg, 0, sizeof(msg));
1986 msg.msg_name = (caddr_t)&sin6;
1987 msg.msg_namelen = sizeof(sin6);
1990 msg.msg_control = NULL;
1991 msg.msg_controllen = 0;
1992 iov.iov_base = (caddr_t)buf;
1993 iov.iov_len = sizeof(struct icmp6_fqdn_query);
1996 msg.msg_control = cbuf;
1997 msg.msg_controllen = sizeof(cbuf);
1998 cmsg = CMSG_FIRSTHDR(&msg);
1999 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
2000 cmsg->cmsg_level = IPPROTO_IPV6;
2001 cmsg->cmsg_type = IPV6_PKTINFO;
2002 pkt = (struct in6_pktinfo *)&cmsg[1];
2003 memset(&pkt->ipi6_addr, 0, sizeof(struct in6_addr));
2004 pkt->ipi6_ifindex = ifindex;
2005 cmsg = CMSG_NXTHDR(&msg, cmsg);
2006 msg.msg_controllen = (char *)cmsg - cbuf;
2009 if ((s = _socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
2011 (void)_setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER,
2012 (char *)&filter, sizeof(filter));
2013 cc = _sendmsg(s, &msg, 0);
2021 if (_select(s + 1, &fds, NULL, NULL, &tout) <= 0) {
2026 cc = _recvfrom(s, buf, sizeof(buf), 0,
2027 (struct sockaddr *)&sin6, &len);
2032 if (cc < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr))
2034 if (!IN6_ARE_ADDR_EQUAL(addr, &sin6.sin6_addr))
2036 fr = (struct icmp6_fqdn_reply *)(buf + sizeof(struct ip6_hdr));
2037 if (fr->icmp6_fqdn_type == ICMP6_FQDN_REPLY)
2041 if (fr->icmp6_fqdn_cookie[1] != 0) {
2043 name = buf + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 4;
2044 len = (buf + cc) - name;
2046 len = fr->icmp6_fqdn_namelen;
2047 name = fr->icmp6_fqdn_name;
2053 if ((hc = (struct _icmp_host_cache *)malloc(sizeof(*hc))) == NULL)
2055 /* XXX: limit number of cached entries */
2056 hc->hc_ifindex = ifindex;
2057 hc->hc_addr = *addr;
2058 hc->hc_name = strdup(name);
2060 hc->hc_next = hc_head;
2066 static struct hostent *
2067 _icmp_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
2071 struct in6_addr addr6;
2073 if (af != AF_INET6) {
2075 * Note: rfc1788 defines Who Are You for IPv4,
2076 * but no one implements it.
2081 memcpy(&addr6, addr, addrlen);
2082 ifindex = (addr6.s6_addr[2] << 8) | addr6.s6_addr[3];
2083 addr6.s6_addr[2] = addr6.s6_addr[3] = 0;
2085 if (!IN6_IS_ADDR_LINKLOCAL(&addr6))
2086 return NULL; /*XXX*/
2088 if ((hname = _icmp_fqdn_query(&addr6, ifindex)) == NULL)
2090 return _hpaddr(af, hname, &addr6, errp);