2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: getipnode.c,v 1.30.2.4.2.4 2004/03/06 08:15:31 marka Exp $ */
27 #include <lwres/lwres.h>
28 #include <lwres/net.h>
29 #include <lwres/netdb.h> /* XXX #include <netdb.h> */
40 #ifdef LWRES_PLATFORM_NEEDIN6ADDRANY
41 LIBLWRES_EXTERNAL_DATA const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
44 #ifndef IN6_IS_ADDR_V4COMPAT
45 static const unsigned char in6addr_compat[12] = {
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
48 #define IN6_IS_ADDR_V4COMPAT(x) (!memcmp((x)->s6_addr, in6addr_compat, 12) && \
49 ((x)->s6_addr[12] != 0 || \
50 (x)->s6_addr[13] != 0 || \
51 (x)->s6_addr[14] != 0 || \
52 ((x)->s6_addr[15] != 0 && \
53 (x)->s6_addr[15] != 1)))
55 #ifndef IN6_IS_ADDR_V4MAPPED
56 #define IN6_IS_ADDR_V4MAPPED(x) (!memcmp((x)->s6_addr, in6addr_mapped, 12))
59 static const unsigned char in6addr_mapped[12] = {
60 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff
64 *** Forward declarations.
68 scan_interfaces(int *, int *);
70 static struct hostent *
71 copyandmerge(struct hostent *, struct hostent *, int, int *);
73 static struct hostent *
74 hostfromaddr(lwres_gnbaresponse_t *addr, int af, const void *src);
76 static struct hostent *
77 hostfromname(lwres_gabnresponse_t *name, int af);
84 * AI_V4MAPPED + AF_INET6
85 * If no IPv6 address then a query for IPv4 and map returned values.
87 * AI_ALL + AI_V4MAPPED + AF_INET6
88 * Return IPv6 and IPv4 mapped.
91 * Only return IPv6 / IPv4 address if there is an interface of that
96 lwres_getipnodebyname(const char *name, int af, int flags, int *error_num) {
97 int have_v4 = 1, have_v6 = 1;
100 struct hostent he, *he1 = NULL, *he2 = NULL, *he3 = NULL;
103 lwres_context_t *lwrctx = NULL;
104 lwres_gabnresponse_t *by = NULL;
108 * If we care about active interfaces then check.
110 if ((flags & AI_ADDRCONFIG) != 0)
111 if (scan_interfaces(&have_v4, &have_v6) == -1) {
112 *error_num = NO_RECOVERY;
116 /* Check for literal address. */
117 if ((v4 = lwres_net_pton(AF_INET, name, &in4)) != 1)
118 v6 = lwres_net_pton(AF_INET6, name, &in6);
121 * Impossible combination?
123 if ((af == AF_INET6 && (flags & AI_V4MAPPED) == 0 && v4 == 1) ||
124 (af == AF_INET && v6 == 1) ||
125 (have_v4 == 0 && v4 == 1) ||
126 (have_v6 == 0 && v6 == 1) ||
127 (have_v4 == 0 && af == AF_INET) ||
128 (have_v6 == 0 && af == AF_INET6 &&
129 (((flags & AI_V4MAPPED) != 0 && have_v4) ||
130 (flags & AI_V4MAPPED) == 0))) {
131 *error_num = HOST_NOT_FOUND;
138 if (v4 == 1 || v6 == 1) {
141 char mappedname[sizeof("::ffff:123.123.123.123")];
143 const char *const_name;
148 if (v4 == 1 && af == AF_INET6) {
149 strcpy(mappedname, "::ffff:");
150 lwres_net_ntop(AF_INET, (char *)&in4,
151 mappedname + sizeof("::ffff:") - 1,
152 sizeof(mappedname) - sizeof("::ffff:")
154 he.h_name = mappedname;
156 he.h_name = u.deconst_name;
157 he.h_addr_list = addr_list;
158 he.h_addr_list[0] = (v4 == 1) ? (char *)&in4 : (char *)&in6;
159 he.h_addr_list[1] = NULL;
160 he.h_aliases = aliases;
161 he.h_aliases[0] = NULL;
162 he.h_length = (v4 == 1) ? INADDRSZ : IN6ADDRSZ;
163 he.h_addrtype = (v4 == 1) ? AF_INET : AF_INET6;
164 return (copyandmerge(&he, NULL, af, error_num));
167 n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
169 *error_num = NO_RECOVERY;
172 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
173 tmp_err = NO_RECOVERY;
174 if (have_v6 && af == AF_INET6) {
176 n = lwres_getaddrsbyname(lwrctx, name, LWRES_ADDRTYPE_V6, &by);
178 he1 = hostfromname(by, AF_INET6);
179 lwres_gabnresponse_free(lwrctx, &by);
181 *error_num = NO_RECOVERY;
185 tmp_err = HOST_NOT_FOUND;
191 (af == AF_INET6 && (flags & AI_V4MAPPED) != 0 &&
192 (he1 == NULL || (flags & AI_ALL) != 0)))) {
193 n = lwres_getaddrsbyname(lwrctx, name, LWRES_ADDRTYPE_V4, &by);
195 he2 = hostfromname(by, AF_INET);
196 lwres_gabnresponse_free(lwrctx, &by);
198 *error_num = NO_RECOVERY;
201 } else if (he1 == NULL) {
202 if (n == LWRES_R_NOTFOUND)
203 *error_num = HOST_NOT_FOUND;
205 *error_num = NO_RECOVERY;
209 *error_num = tmp_err;
211 he3 = copyandmerge(he1, he2, af, error_num);
215 lwres_freehostent(he1);
217 lwres_freehostent(he2);
218 if (lwrctx != NULL) {
219 lwres_conf_clear(lwrctx);
220 lwres_context_destroy(&lwrctx);
226 lwres_getipnodebyaddr(const void *src, size_t len, int af, int *error_num) {
227 struct hostent *he1, *he2;
228 lwres_context_t *lwrctx = NULL;
229 lwres_gnbaresponse_t *by = NULL;
233 struct in6_addr *in6;
240 *error_num = NO_RECOVERY;
246 if (len != (unsigned int)INADDRSZ) {
247 *error_num = NO_RECOVERY;
252 if (len != (unsigned int)IN6ADDRSZ) {
253 *error_num = NO_RECOVERY;
258 *error_num = NO_RECOVERY;
263 * The de-"const"-ing game is done because at least one
264 * vendor's system (RedHat 6.0) defines the IN6_IS_ADDR_*
265 * macros in such a way that they discard the const with
266 * internal casting, and gcc ends up complaining. Rather
267 * than replacing their own (possibly optimized) definitions
268 * with our own, cleanly discarding the const is the easiest
274 * Look up IPv4 and IPv4 mapped/compatible addresses.
276 if ((af == AF_INET6 && IN6_IS_ADDR_V4COMPAT(u.in6)) ||
277 (af == AF_INET6 && IN6_IS_ADDR_V4MAPPED(u.in6)) ||
279 const unsigned char *cp = src;
283 n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
284 if (n == LWRES_R_SUCCESS)
285 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
286 if (n == LWRES_R_SUCCESS)
287 n = lwres_getnamebyaddr(lwrctx, LWRES_ADDRTYPE_V4,
289 if (n != LWRES_R_SUCCESS) {
290 lwres_conf_clear(lwrctx);
291 lwres_context_destroy(&lwrctx);
292 if (n == LWRES_R_NOTFOUND)
293 *error_num = HOST_NOT_FOUND;
295 *error_num = NO_RECOVERY;
298 he1 = hostfromaddr(by, AF_INET, cp);
299 lwres_gnbaresponse_free(lwrctx, &by);
300 lwres_conf_clear(lwrctx);
301 lwres_context_destroy(&lwrctx);
306 * Convert from AF_INET to AF_INET6.
308 he2 = copyandmerge(he1, NULL, af, error_num);
309 lwres_freehostent(he1);
313 * Restore original address.
315 memcpy(he2->h_addr, src, len);
320 * Lookup IPv6 address.
322 if (memcmp(src, &in6addr_any, IN6ADDRSZ) == 0) {
323 *error_num = HOST_NOT_FOUND;
327 n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
328 if (n == LWRES_R_SUCCESS)
329 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
330 if (n == LWRES_R_SUCCESS)
331 n = lwres_getnamebyaddr(lwrctx, LWRES_ADDRTYPE_V6, IN6ADDRSZ,
334 *error_num = HOST_NOT_FOUND;
337 he1 = hostfromaddr(by, AF_INET6, src);
338 lwres_gnbaresponse_free(lwrctx, &by);
340 *error_num = NO_RECOVERY;
341 lwres_context_destroy(&lwrctx);
346 lwres_freehostent(struct hostent *he) {
353 cpp = he->h_addr_list;
354 while (*cpp != NULL) {
362 while (*cpp != NULL) {
369 free(he->h_addr_list);
378 * Scan the interface table and set have_v4 and have_v6 depending
379 * upon whether there are IPv4 and IPv6 interface addresses.
386 #if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR) && \
387 !defined(IRIX_EMUL_IOCTL_SIOCGIFCONF)
390 #define lifc_len iflc_len
391 #define lifc_buf iflc_buf
392 #define lifc_req iflc_req
393 #define LIFCONF if_laddrconf
395 #define ISC_HAVE_LIFC_FAMILY 1
396 #define ISC_HAVE_LIFC_FLAGS 1
397 #define LIFCONF lifconf
401 #define lifr_addr iflr_addr
402 #define lifr_name iflr_name
403 #define lifr_dstaddr iflr_dstaddr
404 #define lifr_flags iflr_flags
405 #define ss_family sa_family
406 #define LIFREQ if_laddrreq
408 #define LIFREQ lifreq
412 scan_interfaces6(int *have_v4, int *have_v6) {
414 struct LIFREQ lifreq;
417 char *buf = NULL, *cp, *cplim;
418 static unsigned int bufsiz = 4095;
422 * Set to zero. Used as loop terminators below.
424 *have_v4 = *have_v6 = 0;
427 * Get interface list from system.
429 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) == -1)
433 * Grow buffer until large enough to contain all interface
437 buf = malloc(bufsiz);
440 #ifdef ISC_HAVE_LIFC_FAMILY
441 lifc.lifc_family = AF_UNSPEC; /* request all families */
443 #ifdef ISC_HAVE_LIFC_FLAGS
446 lifc.lifc_len = bufsiz;
448 if ((n = ioctl(s, SIOCGLIFCONF, (char *)&lifc)) != -1) {
450 * Some OS's just return what will fit rather
451 * than set EINVAL if the buffer is too small
452 * to fit all the interfaces in. If
453 * lifc.lifc_len is too near to the end of the
454 * buffer we will grow it just in case and
457 if (lifc.lifc_len + 2 * sizeof(lifreq) < bufsiz)
460 if ((n == -1) && errno != EINVAL)
463 if (bufsiz > 1000000)
471 * Parse system's interface list.
473 cplim = buf + lifc.lifc_len; /* skip over if's with big ifr_addr's */
475 (*have_v4 == 0 || *have_v6 == 0) && cp < cplim;
477 memcpy(&lifreq, cp, sizeof(lifreq));
478 #ifdef LWRES_PLATFORM_HAVESALEN
479 #ifdef FIX_ZERO_SA_LEN
480 if (lifreq.lifr_addr.sa_len == 0)
481 lifreq.lifr_addr.sa_len = 16;
483 #ifdef HAVE_MINIMUM_IFREQ
484 cpsize = sizeof(lifreq);
485 if (lifreq.lifr_addr.sa_len > sizeof(struct sockaddr))
486 cpsize += (int)lifreq.lifr_addr.sa_len -
487 (int)(sizeof(struct sockaddr));
489 cpsize = sizeof(lifreq.lifr_name) + lifreq.lifr_addr.sa_len;
490 #endif /* HAVE_MINIMUM_IFREQ */
491 #elif defined SIOCGIFCONF_ADDR
492 cpsize = sizeof(lifreq);
494 cpsize = sizeof(lifreq.lifr_name);
495 /* XXX maybe this should be a hard error? */
496 if (ioctl(s, SIOCGLIFADDR, (char *)&lifreq) < 0)
499 switch (lifreq.lifr_addr.ss_family) {
503 &((struct sockaddr_in *)
504 &lifreq.lifr_addr)->sin_addr,
506 if (in4.s_addr == INADDR_ANY)
508 n = ioctl(s, SIOCGLIFFLAGS, (char *)&lifreq);
511 if ((lifreq.lifr_flags & IFF_UP) == 0)
519 &((struct sockaddr_in6 *)
520 &lifreq.lifr_addr)->sin6_addr,
522 if (memcmp(&in6, &in6addr_any,
525 n = ioctl(s, SIOCGLIFFLAGS, (char *)&lifreq);
528 if ((lifreq.lifr_flags & IFF_UP) == 0)
549 scan_interfaces(int *have_v4, int *have_v6) {
550 #if !defined(SIOCGIFCONF) || !defined(SIOCGIFADDR)
551 *have_v4 = *have_v6 = 1;
556 char _pad[256]; /* leave space for IPv6 addresses */
561 char *buf = NULL, *cp, *cplim;
562 static unsigned int bufsiz = 4095;
566 #if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR) && \
567 !defined(IRIX_EMUL_IOCTL_SIOCGIFCONF)
569 * Try to scan the interfaces using IPv6 ioctls().
571 if (!scan_interfaces6(have_v4, have_v6))
576 * Set to zero. Used as loop terminators below.
578 *have_v4 = *have_v6 = 0;
581 * Get interface list from system.
583 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
587 * Grow buffer until large enough to contain all interface
591 buf = malloc(bufsiz);
594 ifc.ifc_len = bufsiz;
596 #ifdef IRIX_EMUL_IOCTL_SIOCGIFCONF
598 * This is a fix for IRIX OS in which the call to ioctl with
599 * the flag SIOCGIFCONF may not return an entry for all the
600 * interfaces like most flavors of Unix.
602 if (emul_ioctl(&ifc) >= 0)
605 if ((n = ioctl(s, SIOCGIFCONF, (char *)&ifc)) != -1) {
607 * Some OS's just return what will fit rather
608 * than set EINVAL if the buffer is too small
609 * to fit all the interfaces in. If
610 * ifc.ifc_len is too near to the end of the
611 * buffer we will grow it just in case and
614 if (ifc.ifc_len + 2 * sizeof(u.ifreq) < bufsiz)
618 if ((n == -1) && errno != EINVAL)
621 if (bufsiz > 1000000)
629 * Parse system's interface list.
631 cplim = buf + ifc.ifc_len; /* skip over if's with big ifr_addr's */
633 (*have_v4 == 0 || *have_v6 == 0) && cp < cplim;
635 memcpy(&u.ifreq, cp, sizeof(u.ifreq));
636 #ifdef LWRES_PLATFORM_HAVESALEN
637 #ifdef FIX_ZERO_SA_LEN
638 if (u.ifreq.ifr_addr.sa_len == 0)
639 u.ifreq.ifr_addr.sa_len = 16;
641 #ifdef HAVE_MINIMUM_IFREQ
642 cpsize = sizeof(u.ifreq);
643 if (u.ifreq.ifr_addr.sa_len > sizeof(struct sockaddr))
644 cpsize += (int)u.ifreq.ifr_addr.sa_len -
645 (int)(sizeof(struct sockaddr));
647 cpsize = sizeof(u.ifreq.ifr_name) + u.ifreq.ifr_addr.sa_len;
648 #endif /* HAVE_MINIMUM_IFREQ */
649 if (cpsize > sizeof(u.ifreq) && cpsize <= sizeof(u))
650 memcpy(&u.ifreq, cp, cpsize);
651 #elif defined SIOCGIFCONF_ADDR
652 cpsize = sizeof(u.ifreq);
654 cpsize = sizeof(u.ifreq.ifr_name);
655 /* XXX maybe this should be a hard error? */
656 if (ioctl(s, SIOCGIFADDR, (char *)&u.ifreq) < 0)
659 switch (u.ifreq.ifr_addr.sa_family) {
663 &((struct sockaddr_in *)
664 &u.ifreq.ifr_addr)->sin_addr,
666 if (in4.s_addr == INADDR_ANY)
668 n = ioctl(s, SIOCGIFFLAGS, (char *)&u.ifreq);
671 if ((u.ifreq.ifr_flags & IFF_UP) == 0)
679 &((struct sockaddr_in6 *)
680 &u.ifreq.ifr_addr)->sin6_addr,
682 if (memcmp(&in6, &in6addr_any,
685 n = ioctl(s, SIOCGIFFLAGS, (char *)&u.ifreq);
688 if ((u.ifreq.ifr_flags & IFF_UP) == 0)
708 static struct hostent *
709 copyandmerge(struct hostent *he1, struct hostent *he2, int af, int *error_num)
711 struct hostent *he = NULL;
712 int addresses = 1; /* NULL terminator */
713 int names = 1; /* NULL terminator */
718 * Work out array sizes.
721 cpp = he1->h_addr_list;
722 while (*cpp != NULL) {
726 cpp = he1->h_aliases;
727 while (*cpp != NULL) {
734 cpp = he2->h_addr_list;
735 while (*cpp != NULL) {
740 cpp = he2->h_aliases;
741 while (*cpp != NULL) {
748 if (addresses == 1) {
749 *error_num = NO_ADDRESS;
753 he = malloc(sizeof(*he));
757 he->h_addr_list = malloc(sizeof(char *) * (addresses));
758 if (he->h_addr_list == NULL)
760 memset(he->h_addr_list, 0, sizeof(char *) * (addresses));
765 npp = he->h_addr_list;
767 cpp = he1->h_addr_list;
768 while (*cpp != NULL) {
769 *npp = malloc((af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
773 * Convert to mapped if required.
775 if (af == AF_INET6 && he1->h_addrtype == AF_INET) {
776 memcpy(*npp, in6addr_mapped,
777 sizeof(in6addr_mapped));
778 memcpy(*npp + sizeof(in6addr_mapped), *cpp,
782 (af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
790 cpp = he2->h_addr_list;
791 while (*cpp != NULL) {
792 *npp = malloc((af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
796 * Convert to mapped if required.
798 if (af == AF_INET6 && he2->h_addrtype == AF_INET) {
799 memcpy(*npp, in6addr_mapped,
800 sizeof(in6addr_mapped));
801 memcpy(*npp + sizeof(in6addr_mapped), *cpp,
805 (af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
812 he->h_aliases = malloc(sizeof(char *) * (names));
813 if (he->h_aliases == NULL)
815 memset(he->h_aliases, 0, sizeof(char *) * (names));
821 cpp = (he1 != NULL) ? he1->h_aliases : he2->h_aliases;
822 while (*cpp != NULL) {
823 len = strlen (*cpp) + 1;
835 he->h_name = malloc(strlen((he1 != NULL) ?
836 he1->h_name : he2->h_name) + 1);
837 if (he->h_name == NULL)
839 strcpy(he->h_name, (he1 != NULL) ? he1->h_name : he2->h_name);
842 * Set address type and length.
845 he->h_length = (af == AF_INET) ? INADDRSZ : IN6ADDRSZ;
850 while (*cpp != NULL) {
857 cpp = he->h_addr_list;
858 while (*cpp != NULL) {
863 free(he->h_addr_list);
869 *error_num = NO_RECOVERY;
873 static struct hostent *
874 hostfromaddr(lwres_gnbaresponse_t *addr, int af, const void *src) {
878 he = malloc(sizeof(*he));
881 memset(he, 0, sizeof(*he));
884 * Set family and length.
889 he->h_length = INADDRSZ;
892 he->h_length = IN6ADDRSZ;
901 he->h_name = strdup(addr->realname);
902 if (he->h_name == NULL)
908 he->h_aliases = malloc(sizeof(char *) * (addr->naliases + 1));
909 if (he->h_aliases == NULL)
911 for (i = 0; i < addr->naliases; i++) {
912 he->h_aliases[i] = strdup(addr->aliases[i]);
913 if (he->h_aliases[i] == NULL)
916 he->h_aliases[i] = NULL;
921 he->h_addr_list = malloc(sizeof(char *) * 2);
922 if (he->h_addr_list == NULL)
924 he->h_addr_list[0] = malloc(he->h_length);
925 if (he->h_addr_list[0] == NULL)
927 memcpy(he->h_addr_list[0], src, he->h_length);
928 he->h_addr_list[1] = NULL;
932 if (he != NULL && he->h_addr_list != NULL) {
933 for (i = 0; he->h_addr_list[i] != NULL; i++)
934 free(he->h_addr_list[i]);
935 free(he->h_addr_list);
937 if (he != NULL && he->h_aliases != NULL) {
938 for (i = 0; he->h_aliases[i] != NULL; i++)
939 free(he->h_aliases[i]);
942 if (he != NULL && he->h_name != NULL)
949 static struct hostent *
950 hostfromname(lwres_gabnresponse_t *name, int af) {
955 he = malloc(sizeof(*he));
958 memset(he, 0, sizeof(*he));
961 * Set family and length.
966 he->h_length = INADDRSZ;
969 he->h_length = IN6ADDRSZ;
978 he->h_name = strdup(name->realname);
979 if (he->h_name == NULL)
985 he->h_aliases = malloc(sizeof(char *) * (name->naliases + 1));
986 for (i = 0; i < name->naliases; i++) {
987 he->h_aliases[i] = strdup(name->aliases[i]);
988 if (he->h_aliases[i] == NULL)
991 he->h_aliases[i] = NULL;
996 he->h_addr_list = malloc(sizeof(char *) * (name->naddrs + 1));
997 addr = LWRES_LIST_HEAD(name->addrs);
999 while (addr != NULL) {
1000 he->h_addr_list[i] = malloc(he->h_length);
1001 if (he->h_addr_list[i] == NULL)
1003 memcpy(he->h_addr_list[i], addr->address, he->h_length);
1004 addr = LWRES_LIST_NEXT(addr, link);
1007 he->h_addr_list[i] = NULL;
1011 if (he != NULL && he->h_addr_list != NULL) {
1012 for (i = 0; he->h_addr_list[i] != NULL; i++)
1013 free(he->h_addr_list[i]);
1014 free(he->h_addr_list);
1016 if (he != NULL && he->h_aliases != NULL) {
1017 for (i = 0; he->h_aliases[i] != NULL; i++)
1018 free(he->h_aliases[i]);
1019 free(he->h_aliases);
1021 if (he != NULL && he->h_name != NULL)