2 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or 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.42.332.5 2009/09/01 23:47:05 tbox Exp $ */
23 * These functions perform thread safe, protocol independent
24 * nodename-to-address and address-to-nodename translation as defined in
25 * RFC2553. This use a struct hostent which is defined in namedb.h:
29 * char *h_name; // official name of host
30 * char **h_aliases; // alias list
31 * int h_addrtype; // host address type
32 * int h_length; // length of address
33 * char **h_addr_list; // list of addresses from name server
35 * #define h_addr h_addr_list[0] // address, for backward compatibility
38 * The members of this structure are:
41 * The official (canonical) name of the host.
44 * A NULL-terminated array of alternate names (nicknames) for the
48 * The type of address being returned - usually PF_INET or
52 * The length of the address in bytes.
55 * A NULL terminated array of network addresses for the host. Host
56 * addresses are returned in network byte order.
58 * lwres_getipnodebyname() looks up addresses of protocol family af for
59 * the hostname name. The flags parameter contains ORed flag bits to
60 * specify the types of addresses that are searched for, and the types of
61 * addresses that are returned. The flag bits are:
64 * This is used with an af of #AF_INET6, and causes IPv4 addresses
65 * to be returned as IPv4-mapped IPv6 addresses.
68 * This is used with an af of #AF_INET6, and causes all known
69 * addresses (IPv6 and IPv4) to be returned. If #AI_V4MAPPED is
70 * also set, the IPv4 addresses are return as mapped IPv6
74 * Only return an IPv6 or IPv4 address if here is an active
75 * network interface of that type. This is not currently
76 * implemented in the BIND 9 lightweight resolver, and the flag is
80 * This default sets the #AI_V4MAPPED and #AI_ADDRCONFIG flag bits.
82 * lwres_getipnodebyaddr() performs a reverse lookup of address src which
83 * is len bytes long. af denotes the protocol family, typically PF_INET
86 * lwres_freehostent() releases all the memory associated with the struct
87 * hostent pointer. Any memory allocated for the h_name, h_addr_list
88 * and h_aliases is freed, as is the memory for the hostent structure
91 * \section getipnode_return Return Values
93 * If an error occurs, lwres_getipnodebyname() and
94 * lwres_getipnodebyaddr() set *error_num to an appropriate error code
95 * and the function returns a NULL pointer. The error codes and their
96 * meanings are defined in \link netdb.h <lwres/netdb.h>\endlink:
98 * \li #HOST_NOT_FOUND:
99 * No such host is known.
102 * The server recognised the request and the name but no address
103 * is available. Another type of request to the name server for
104 * the domain might return an answer.
107 * A temporary and possibly transient error occurred, such as a
108 * failure of a server to respond. The request may succeed if
112 * An unexpected failure occurred, and retrying the request is
115 * lwres_hstrerror() translates these error codes to suitable error
118 * \section getipnode_see See Also
120 * getaddrinfo.c, gethost.c, getnameinfo.c, herror.c, RFC2553
130 #include <lwres/lwres.h>
131 #include <lwres/net.h>
132 #include <lwres/netdb.h> /* XXX #include <netdb.h> */
134 #include "assert_p.h"
143 #ifdef LWRES_PLATFORM_NEEDIN6ADDRANY
144 LIBLWRES_EXTERNAL_DATA const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
147 #ifndef IN6_IS_ADDR_V4COMPAT
148 static const unsigned char in6addr_compat[12] = {
149 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
151 #define IN6_IS_ADDR_V4COMPAT(x) (!memcmp((x)->s6_addr, in6addr_compat, 12) && \
152 ((x)->s6_addr[12] != 0 || \
153 (x)->s6_addr[13] != 0 || \
154 (x)->s6_addr[14] != 0 || \
155 ((x)->s6_addr[15] != 0 && \
156 (x)->s6_addr[15] != 1)))
158 #ifndef IN6_IS_ADDR_V4MAPPED
159 #define IN6_IS_ADDR_V4MAPPED(x) (!memcmp((x)->s6_addr, in6addr_mapped, 12))
162 static const unsigned char in6addr_mapped[12] = {
163 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff
167 *** Forward declarations.
171 scan_interfaces(int *, int *);
173 static struct hostent *
174 copyandmerge(struct hostent *, struct hostent *, int, int *);
176 static struct hostent *
177 hostfromaddr(lwres_gnbaresponse_t *addr, int af, const void *src);
179 static struct hostent *
180 hostfromname(lwres_gabnresponse_t *name, int af);
183 *** Public functions.
187 * AI_V4MAPPED + AF_INET6
188 * If no IPv6 address then a query for IPv4 and map returned values.
190 * AI_ALL + AI_V4MAPPED + AF_INET6
191 * Return IPv6 and IPv4 mapped.
194 * Only return IPv6 / IPv4 address if there is an interface of that
199 lwres_getipnodebyname(const char *name, int af, int flags, int *error_num) {
200 int have_v4 = 1, have_v6 = 1;
203 struct hostent he, *he1 = NULL, *he2 = NULL, *he3 = NULL;
206 lwres_context_t *lwrctx = NULL;
207 lwres_gabnresponse_t *by = NULL;
211 * If we care about active interfaces then check.
213 if ((flags & AI_ADDRCONFIG) != 0)
214 if (scan_interfaces(&have_v4, &have_v6) == -1) {
215 *error_num = NO_RECOVERY;
219 /* Check for literal address. */
220 if ((v4 = lwres_net_pton(AF_INET, name, &in4)) != 1)
221 v6 = lwres_net_pton(AF_INET6, name, &in6);
224 * Impossible combination?
226 if ((af == AF_INET6 && (flags & AI_V4MAPPED) == 0 && v4 == 1) ||
227 (af == AF_INET && v6 == 1) ||
228 (have_v4 == 0 && v4 == 1) ||
229 (have_v6 == 0 && v6 == 1) ||
230 (have_v4 == 0 && af == AF_INET) ||
231 (have_v6 == 0 && af == AF_INET6 &&
232 (((flags & AI_V4MAPPED) != 0 && have_v4) ||
233 (flags & AI_V4MAPPED) == 0))) {
234 *error_num = HOST_NOT_FOUND;
241 if (v4 == 1 || v6 == 1) {
244 char mappedname[sizeof("::ffff:123.123.123.123")];
246 const char *const_name;
251 if (v4 == 1 && af == AF_INET6) {
252 strcpy(mappedname, "::ffff:");
253 lwres_net_ntop(AF_INET, (char *)&in4,
254 mappedname + sizeof("::ffff:") - 1,
255 sizeof(mappedname) - sizeof("::ffff:")
257 he.h_name = mappedname;
259 he.h_name = u.deconst_name;
260 he.h_addr_list = addr_list;
261 he.h_addr_list[0] = (v4 == 1) ? (char *)&in4 : (char *)&in6;
262 he.h_addr_list[1] = NULL;
263 he.h_aliases = aliases;
264 he.h_aliases[0] = NULL;
265 he.h_length = (v4 == 1) ? INADDRSZ : IN6ADDRSZ;
266 he.h_addrtype = (v4 == 1) ? AF_INET : AF_INET6;
267 return (copyandmerge(&he, NULL, af, error_num));
270 n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
272 *error_num = NO_RECOVERY;
275 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
276 tmp_err = NO_RECOVERY;
277 if (have_v6 && af == AF_INET6) {
278 n = lwres_getaddrsbyname(lwrctx, name, LWRES_ADDRTYPE_V6, &by);
280 he1 = hostfromname(by, AF_INET6);
281 lwres_gabnresponse_free(lwrctx, &by);
283 *error_num = NO_RECOVERY;
287 if (n == LWRES_R_NOTFOUND)
288 tmp_err = HOST_NOT_FOUND;
290 *error_num = NO_RECOVERY;
298 (af == AF_INET6 && (flags & AI_V4MAPPED) != 0 &&
299 (he1 == NULL || (flags & AI_ALL) != 0)))) {
300 n = lwres_getaddrsbyname(lwrctx, name, LWRES_ADDRTYPE_V4, &by);
302 he2 = hostfromname(by, AF_INET);
303 lwres_gabnresponse_free(lwrctx, &by);
305 *error_num = NO_RECOVERY;
308 } else if (he1 == NULL) {
309 if (n == LWRES_R_NOTFOUND)
310 *error_num = HOST_NOT_FOUND;
312 *error_num = NO_RECOVERY;
316 *error_num = tmp_err;
318 he3 = copyandmerge(he1, he2, af, error_num);
322 lwres_freehostent(he1);
324 lwres_freehostent(he2);
325 if (lwrctx != NULL) {
326 lwres_conf_clear(lwrctx);
327 lwres_context_destroy(&lwrctx);
332 /*% performs a reverse lookup of address src which is len bytes long. af denotes the protocol family, typically #PF_INET or PF_INET6. */
334 lwres_getipnodebyaddr(const void *src, size_t len, int af, int *error_num) {
335 struct hostent *he1, *he2;
336 lwres_context_t *lwrctx = NULL;
337 lwres_gnbaresponse_t *by = NULL;
341 struct in6_addr *in6;
348 *error_num = NO_RECOVERY;
354 if (len != (unsigned int)INADDRSZ) {
355 *error_num = NO_RECOVERY;
360 if (len != (unsigned int)IN6ADDRSZ) {
361 *error_num = NO_RECOVERY;
366 *error_num = NO_RECOVERY;
371 * The de-"const"-ing game is done because at least one
372 * vendor's system (RedHat 6.0) defines the IN6_IS_ADDR_*
373 * macros in such a way that they discard the const with
374 * internal casting, and gcc ends up complaining. Rather
375 * than replacing their own (possibly optimized) definitions
376 * with our own, cleanly discarding the const is the easiest
382 * Look up IPv4 and IPv4 mapped/compatible addresses.
384 if ((af == AF_INET6 && IN6_IS_ADDR_V4COMPAT(u.in6)) ||
385 (af == AF_INET6 && IN6_IS_ADDR_V4MAPPED(u.in6)) ||
387 const unsigned char *cp = src;
391 n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
392 if (n == LWRES_R_SUCCESS)
393 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
394 if (n == LWRES_R_SUCCESS)
395 n = lwres_getnamebyaddr(lwrctx, LWRES_ADDRTYPE_V4,
397 if (n != LWRES_R_SUCCESS) {
398 lwres_conf_clear(lwrctx);
399 lwres_context_destroy(&lwrctx);
400 if (n == LWRES_R_NOTFOUND)
401 *error_num = HOST_NOT_FOUND;
403 *error_num = NO_RECOVERY;
406 he1 = hostfromaddr(by, AF_INET, cp);
407 lwres_gnbaresponse_free(lwrctx, &by);
408 lwres_conf_clear(lwrctx);
409 lwres_context_destroy(&lwrctx);
414 * Convert from AF_INET to AF_INET6.
416 he2 = copyandmerge(he1, NULL, af, error_num);
417 lwres_freehostent(he1);
421 * Restore original address.
423 memcpy(he2->h_addr, src, len);
428 * Lookup IPv6 address.
430 if (memcmp(src, &in6addr_any, IN6ADDRSZ) == 0) {
431 *error_num = HOST_NOT_FOUND;
435 n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
436 if (n == LWRES_R_SUCCESS)
437 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
438 if (n == LWRES_R_SUCCESS)
439 n = lwres_getnamebyaddr(lwrctx, LWRES_ADDRTYPE_V6, IN6ADDRSZ,
442 lwres_conf_clear(lwrctx);
443 lwres_context_destroy(&lwrctx);
445 if (n == LWRES_R_NOTFOUND)
446 *error_num = HOST_NOT_FOUND;
448 *error_num = NO_RECOVERY;
453 he1 = hostfromaddr(by, AF_INET6, src);
454 lwres_gnbaresponse_free(lwrctx, &by);
456 *error_num = NO_RECOVERY;
457 lwres_conf_clear(lwrctx);
458 lwres_context_destroy(&lwrctx);
462 /*% releases all the memory associated with the struct hostent pointer */
464 lwres_freehostent(struct hostent *he) {
471 cpp = he->h_addr_list;
472 while (*cpp != NULL) {
480 while (*cpp != NULL) {
487 free(he->h_addr_list);
496 * Scan the interface table and set have_v4 and have_v6 depending
497 * upon whether there are IPv4 and IPv6 interface addresses.
504 #if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR) && \
505 !defined(IRIX_EMUL_IOCTL_SIOCGIFCONF)
508 #define lifc_len iflc_len
509 #define lifc_buf iflc_buf
510 #define lifc_req iflc_req
511 #define LIFCONF if_laddrconf
513 #define ISC_HAVE_LIFC_FAMILY 1
514 #define ISC_HAVE_LIFC_FLAGS 1
515 #define LIFCONF lifconf
519 #define lifr_addr iflr_addr
520 #define lifr_name iflr_name
521 #define lifr_dstaddr iflr_dstaddr
522 #define lifr_flags iflr_flags
523 #define ss_family sa_family
524 #define LIFREQ if_laddrreq
526 #define LIFREQ lifreq
530 scan_interfaces6(int *have_v4, int *have_v6) {
532 struct LIFREQ lifreq;
535 char *buf = NULL, *cp, *cplim;
536 static unsigned int bufsiz = 4095;
540 * Set to zero. Used as loop terminators below.
542 *have_v4 = *have_v6 = 0;
545 * Get interface list from system.
547 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) == -1)
551 * Grow buffer until large enough to contain all interface
555 buf = malloc(bufsiz);
558 #ifdef ISC_HAVE_LIFC_FAMILY
559 lifc.lifc_family = AF_UNSPEC; /* request all families */
561 #ifdef ISC_HAVE_LIFC_FLAGS
564 lifc.lifc_len = bufsiz;
566 if ((n = ioctl(s, SIOCGLIFCONF, (char *)&lifc)) != -1) {
568 * Some OS's just return what will fit rather
569 * than set EINVAL if the buffer is too small
570 * to fit all the interfaces in. If
571 * lifc.lifc_len is too near to the end of the
572 * buffer we will grow it just in case and
575 if (lifc.lifc_len + 2 * sizeof(lifreq) < bufsiz)
578 if ((n == -1) && errno != EINVAL)
581 if (bufsiz > 1000000)
589 * Parse system's interface list.
591 cplim = buf + lifc.lifc_len; /* skip over if's with big ifr_addr's */
593 (*have_v4 == 0 || *have_v6 == 0) && cp < cplim;
595 memcpy(&lifreq, cp, sizeof(lifreq));
596 #ifdef LWRES_PLATFORM_HAVESALEN
597 #ifdef FIX_ZERO_SA_LEN
598 if (lifreq.lifr_addr.sa_len == 0)
599 lifreq.lifr_addr.sa_len = 16;
601 #ifdef HAVE_MINIMUM_IFREQ
602 cpsize = sizeof(lifreq);
603 if (lifreq.lifr_addr.sa_len > sizeof(struct sockaddr))
604 cpsize += (int)lifreq.lifr_addr.sa_len -
605 (int)(sizeof(struct sockaddr));
607 cpsize = sizeof(lifreq.lifr_name) + lifreq.lifr_addr.sa_len;
608 #endif /* HAVE_MINIMUM_IFREQ */
609 #elif defined SIOCGIFCONF_ADDR
610 cpsize = sizeof(lifreq);
612 cpsize = sizeof(lifreq.lifr_name);
613 /* XXX maybe this should be a hard error? */
614 if (ioctl(s, SIOCGLIFADDR, (char *)&lifreq) < 0)
617 switch (lifreq.lifr_addr.ss_family) {
621 &((struct sockaddr_in *)
622 &lifreq.lifr_addr)->sin_addr,
624 if (in4.s_addr == INADDR_ANY)
626 n = ioctl(s, SIOCGLIFFLAGS, (char *)&lifreq);
629 if ((lifreq.lifr_flags & IFF_UP) == 0)
637 &((struct sockaddr_in6 *)
638 &lifreq.lifr_addr)->sin6_addr,
640 if (memcmp(&in6, &in6addr_any,
643 n = ioctl(s, SIOCGLIFFLAGS, (char *)&lifreq);
646 if ((lifreq.lifr_flags & IFF_UP) == 0)
667 scan_interfaces(int *have_v4, int *have_v6) {
668 #if !defined(SIOCGIFCONF) || !defined(SIOCGIFADDR)
669 *have_v4 = *have_v6 = 1;
674 char _pad[256]; /* leave space for IPv6 addresses */
679 char *buf = NULL, *cp, *cplim;
680 static unsigned int bufsiz = 4095;
687 #if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR) && \
688 !defined(IRIX_EMUL_IOCTL_SIOCGIFCONF)
690 * Try to scan the interfaces using IPv6 ioctls().
692 if (!scan_interfaces6(have_v4, have_v6)) {
701 * Set to zero. Used as loop terminators below.
703 *have_v4 = *have_v6 = 0;
706 * Get interface list from system.
708 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
712 * Grow buffer until large enough to contain all interface
716 buf = malloc(bufsiz);
719 ifc.ifc_len = bufsiz;
721 #ifdef IRIX_EMUL_IOCTL_SIOCGIFCONF
723 * This is a fix for IRIX OS in which the call to ioctl with
724 * the flag SIOCGIFCONF may not return an entry for all the
725 * interfaces like most flavors of Unix.
727 if (emul_ioctl(&ifc) >= 0)
730 if ((n = ioctl(s, SIOCGIFCONF, (char *)&ifc)) != -1) {
732 * Some OS's just return what will fit rather
733 * than set EINVAL if the buffer is too small
734 * to fit all the interfaces in. If
735 * ifc.ifc_len is too near to the end of the
736 * buffer we will grow it just in case and
739 if (ifc.ifc_len + 2 * sizeof(u.ifreq) < bufsiz)
743 if ((n == -1) && errno != EINVAL)
746 if (bufsiz > 1000000)
754 * Parse system's interface list.
756 cplim = buf + ifc.ifc_len; /* skip over if's with big ifr_addr's */
758 (*have_v4 == 0 || *have_v6 == 0) && cp < cplim;
760 memcpy(&u.ifreq, cp, sizeof(u.ifreq));
761 #ifdef LWRES_PLATFORM_HAVESALEN
762 #ifdef FIX_ZERO_SA_LEN
763 if (u.ifreq.ifr_addr.sa_len == 0)
764 u.ifreq.ifr_addr.sa_len = 16;
766 #ifdef HAVE_MINIMUM_IFREQ
767 cpsize = sizeof(u.ifreq);
768 if (u.ifreq.ifr_addr.sa_len > sizeof(struct sockaddr))
769 cpsize += (int)u.ifreq.ifr_addr.sa_len -
770 (int)(sizeof(struct sockaddr));
772 cpsize = sizeof(u.ifreq.ifr_name) + u.ifreq.ifr_addr.sa_len;
773 #endif /* HAVE_MINIMUM_IFREQ */
774 if (cpsize > sizeof(u.ifreq) && cpsize <= sizeof(u))
775 memcpy(&u.ifreq, cp, cpsize);
776 #elif defined SIOCGIFCONF_ADDR
777 cpsize = sizeof(u.ifreq);
779 cpsize = sizeof(u.ifreq.ifr_name);
780 /* XXX maybe this should be a hard error? */
781 if (ioctl(s, SIOCGIFADDR, (char *)&u.ifreq) < 0)
784 switch (u.ifreq.ifr_addr.sa_family) {
788 &((struct sockaddr_in *)
789 &u.ifreq.ifr_addr)->sin_addr,
791 if (in4.s_addr == INADDR_ANY)
793 n = ioctl(s, SIOCGIFFLAGS, (char *)&u.ifreq);
796 if ((u.ifreq.ifr_flags & IFF_UP) == 0)
804 &((struct sockaddr_in6 *)
805 &u.ifreq.ifr_addr)->sin6_addr,
807 if (memcmp(&in6, &in6addr_any,
810 n = ioctl(s, SIOCGIFFLAGS, (char *)&u.ifreq);
813 if ((u.ifreq.ifr_flags & IFF_UP) == 0)
840 static struct hostent *
841 copyandmerge(struct hostent *he1, struct hostent *he2, int af, int *error_num)
843 struct hostent *he = NULL;
844 int addresses = 1; /* NULL terminator */
845 int names = 1; /* NULL terminator */
850 * Work out array sizes.
853 cpp = he1->h_addr_list;
854 while (*cpp != NULL) {
858 cpp = he1->h_aliases;
859 while (*cpp != NULL) {
866 cpp = he2->h_addr_list;
867 while (*cpp != NULL) {
872 cpp = he2->h_aliases;
873 while (*cpp != NULL) {
880 if (addresses == 1) {
881 *error_num = NO_ADDRESS;
885 he = malloc(sizeof(*he));
889 he->h_addr_list = malloc(sizeof(char *) * (addresses));
890 if (he->h_addr_list == NULL)
892 memset(he->h_addr_list, 0, sizeof(char *) * (addresses));
897 npp = he->h_addr_list;
899 cpp = he1->h_addr_list;
900 while (*cpp != NULL) {
901 *npp = malloc((af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
905 * Convert to mapped if required.
907 if (af == AF_INET6 && he1->h_addrtype == AF_INET) {
908 memcpy(*npp, in6addr_mapped,
909 sizeof(in6addr_mapped));
910 memcpy(*npp + sizeof(in6addr_mapped), *cpp,
914 (af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
922 cpp = he2->h_addr_list;
923 while (*cpp != NULL) {
924 *npp = malloc((af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
928 * Convert to mapped if required.
930 if (af == AF_INET6 && he2->h_addrtype == AF_INET) {
931 memcpy(*npp, in6addr_mapped,
932 sizeof(in6addr_mapped));
933 memcpy(*npp + sizeof(in6addr_mapped), *cpp,
937 (af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
944 he->h_aliases = malloc(sizeof(char *) * (names));
945 if (he->h_aliases == NULL)
947 memset(he->h_aliases, 0, sizeof(char *) * (names));
953 cpp = (he1 != NULL) ? he1->h_aliases : he2->h_aliases;
954 while (*cpp != NULL) {
955 len = strlen (*cpp) + 1;
967 he->h_name = malloc(strlen((he1 != NULL) ?
968 he1->h_name : he2->h_name) + 1);
969 if (he->h_name == NULL)
971 strcpy(he->h_name, (he1 != NULL) ? he1->h_name : he2->h_name);
974 * Set address type and length.
977 he->h_length = (af == AF_INET) ? INADDRSZ : IN6ADDRSZ;
982 while (*cpp != NULL) {
989 cpp = he->h_addr_list;
990 while (*cpp != NULL) {
995 free(he->h_addr_list);
1001 *error_num = NO_RECOVERY;
1005 static struct hostent *
1006 hostfromaddr(lwres_gnbaresponse_t *addr, int af, const void *src) {
1010 he = malloc(sizeof(*he));
1013 memset(he, 0, sizeof(*he));
1016 * Set family and length.
1018 he->h_addrtype = af;
1021 he->h_length = INADDRSZ;
1024 he->h_length = IN6ADDRSZ;
1033 he->h_name = strdup(addr->realname);
1034 if (he->h_name == NULL)
1040 he->h_aliases = malloc(sizeof(char *) * (addr->naliases + 1));
1041 if (he->h_aliases == NULL)
1043 for (i = 0; i < addr->naliases; i++) {
1044 he->h_aliases[i] = strdup(addr->aliases[i]);
1045 if (he->h_aliases[i] == NULL)
1048 he->h_aliases[i] = NULL;
1053 he->h_addr_list = malloc(sizeof(char *) * 2);
1054 if (he->h_addr_list == NULL)
1056 he->h_addr_list[0] = malloc(he->h_length);
1057 if (he->h_addr_list[0] == NULL)
1059 memcpy(he->h_addr_list[0], src, he->h_length);
1060 he->h_addr_list[1] = NULL;
1064 if (he != NULL && he->h_addr_list != NULL) {
1065 for (i = 0; he->h_addr_list[i] != NULL; i++)
1066 free(he->h_addr_list[i]);
1067 free(he->h_addr_list);
1069 if (he != NULL && he->h_aliases != NULL) {
1070 for (i = 0; he->h_aliases[i] != NULL; i++)
1071 free(he->h_aliases[i]);
1072 free(he->h_aliases);
1074 if (he != NULL && he->h_name != NULL)
1081 static struct hostent *
1082 hostfromname(lwres_gabnresponse_t *name, int af) {
1087 he = malloc(sizeof(*he));
1090 memset(he, 0, sizeof(*he));
1093 * Set family and length.
1095 he->h_addrtype = af;
1098 he->h_length = INADDRSZ;
1101 he->h_length = IN6ADDRSZ;
1110 he->h_name = strdup(name->realname);
1111 if (he->h_name == NULL)
1117 he->h_aliases = malloc(sizeof(char *) * (name->naliases + 1));
1118 for (i = 0; i < name->naliases; i++) {
1119 he->h_aliases[i] = strdup(name->aliases[i]);
1120 if (he->h_aliases[i] == NULL)
1123 he->h_aliases[i] = NULL;
1128 he->h_addr_list = malloc(sizeof(char *) * (name->naddrs + 1));
1129 addr = LWRES_LIST_HEAD(name->addrs);
1131 while (addr != NULL) {
1132 he->h_addr_list[i] = malloc(he->h_length);
1133 if (he->h_addr_list[i] == NULL)
1135 memcpy(he->h_addr_list[i], addr->address, he->h_length);
1136 addr = LWRES_LIST_NEXT(addr, link);
1139 he->h_addr_list[i] = NULL;
1143 if (he != NULL && he->h_addr_list != NULL) {
1144 for (i = 0; he->h_addr_list[i] != NULL; i++)
1145 free(he->h_addr_list[i]);
1146 free(he->h_addr_list);
1148 if (he != NULL && he->h_aliases != NULL) {
1149 for (i = 0; he->h_aliases[i] != NULL; i++)
1150 free(he->h_aliases[i]);
1151 free(he->h_aliases);
1153 if (he != NULL && he->h_name != NULL)