getipnode.c revision 91cd0f93ad34d23e8b09dca337120f64fbe8f0a1
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt/*
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * Copyright (C) 1999-2001 Internet Software Consortium.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * Permission to use, copy, modify, and distribute this software for any
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * purpose with or without fee is hereby granted, provided that the above
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * copyright notice and this permission notice appear in all copies.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt */
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt/* $Id: getipnode.c,v 1.32 2001/11/27 01:56:28 gson Exp $ */
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#include <config.h>
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#include <stdio.h>
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#include <stdlib.h>
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#include <string.h>
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#include <lwres/lwres.h>
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#include <lwres/net.h>
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#include <lwres/netdb.h> /* XXX #include <netdb.h> */
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#include "assert_p.h"
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#ifndef INADDRSZ
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#define INADDRSZ 4
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#endif
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#ifndef IN6ADDRSZ
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#define IN6ADDRSZ 16
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#endif
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#ifdef LWRES_PLATFORM_NEEDIN6ADDRANY
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan HuntLIBLWRES_EXTERNAL_DATA const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#endif
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#ifndef IN6_IS_ADDR_V4COMPAT
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntstatic const unsigned char in6addr_compat[12] = {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt};
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#define IN6_IS_ADDR_V4COMPAT(x) (!memcmp((x)->s6_addr, in6addr_compat, 12) && \
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt ((x)->s6_addr[12] != 0 || \
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (x)->s6_addr[13] != 0 || \
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (x)->s6_addr[14] != 0 || \
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt ((x)->s6_addr[15] != 0 && \
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (x)->s6_addr[15] != 1)))
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#endif
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#ifndef IN6_IS_ADDR_V4MAPPED
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#define IN6_IS_ADDR_V4MAPPED(x) (!memcmp((x)->s6_addr, in6addr_mapped, 12))
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#endif
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntstatic const unsigned char in6addr_mapped[12] = {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt};
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt/***
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *** Forward declarations.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt ***/
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntstatic int
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntscan_interfaces(int *, int *);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntstatic struct hostent *
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntcopyandmerge(struct hostent *, struct hostent *, int, int *);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntstatic struct hostent *
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunthostfromaddr(lwres_gnbaresponse_t *addr, int af, const void *src);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntstatic struct hostent *
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunthostfromname(lwres_gabnresponse_t *name, int af);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt/***
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *** Public functions.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt ***/
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt/*
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * AI_V4MAPPED + AF_INET6
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * If no IPv6 address then a query for IPv4 and map returned values.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * AI_ALL + AI_V4MAPPED + AF_INET6
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * Return IPv6 and IPv4 mapped.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * AI_ADDRCONFIG
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * Only return IPv6 / IPv4 address if there is an interface of that
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * type active.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt */
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntstruct hostent *
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntlwres_getipnodebyname(const char *name, int af, int flags, int *error_num) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt int have_v4 = 1, have_v6 = 1;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt struct in_addr in4;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt struct in6_addr in6;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt struct hostent he, *he1 = NULL, *he2 = NULL, *he3 = NULL;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt int v4 = 0, v6 = 0;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt int tmp_err;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_context_t *lwrctx = NULL;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_gabnresponse_t *by = NULL;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt int n;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt /*
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * If we care about active interfaces then check.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt */
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if ((flags & AI_ADDRCONFIG) != 0)
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (scan_interfaces(&have_v4, &have_v6) == -1) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = NO_RECOVERY;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (NULL);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt /* Check for literal address. */
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if ((v4 = lwres_net_pton(AF_INET, name, &in4)) != 1)
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt v6 = lwres_net_pton(AF_INET6, name, &in6);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt /*
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * Impossible combination?
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt */
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if ((af == AF_INET6 && (flags & AI_V4MAPPED) == 0 && v4 == 1) ||
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (af == AF_INET && v6 == 1) ||
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (have_v4 == 0 && v4 == 1) ||
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (have_v6 == 0 && v6 == 1) ||
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (have_v4 == 0 && af == AF_INET) ||
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (have_v6 == 0 && af == AF_INET6 &&
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (((flags & AI_V4MAPPED) != 0 && have_v4) ||
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (flags & AI_V4MAPPED) == 0))) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = HOST_NOT_FOUND;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (NULL);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt /*
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * Literal address?
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt */
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (v4 == 1 || v6 == 1) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt char *addr_list[2];
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt char *aliases[1];
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt union {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt const char *const_name;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt char *deconst_name;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt } u;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt u.const_name = name;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt he.h_name = u.deconst_name;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt he.h_addr_list = addr_list;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt he.h_addr_list[0] = (v4 == 1) ? (char *)&in4 : (char *)&in6;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt he.h_addr_list[1] = NULL;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt he.h_aliases = aliases;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt he.h_aliases[0] = NULL;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt he.h_length = (v4 == 1) ? INADDRSZ : IN6ADDRSZ;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt he.h_addrtype = (v4 == 1) ? AF_INET : AF_INET6;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (copyandmerge(&he, NULL, af, error_num));
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (n != 0) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = NO_RECOVERY;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt goto cleanup;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt tmp_err = NO_RECOVERY;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (have_v6 && af == AF_INET6) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt n = lwres_getaddrsbyname(lwrctx, name, LWRES_ADDRTYPE_V6, &by);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (n == 0) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt he1 = hostfromname(by, AF_INET6);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_gabnresponse_free(lwrctx, &by);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (he1 == NULL) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = NO_RECOVERY;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt goto cleanup;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt } else {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt tmp_err = HOST_NOT_FOUND;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (have_v4 &&
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt ((af == AF_INET) ||
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (af == AF_INET6 && (flags & AI_V4MAPPED) != 0 &&
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (he1 == NULL || (flags & AI_ALL) != 0)))) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt n = lwres_getaddrsbyname(lwrctx, name, LWRES_ADDRTYPE_V4, &by);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (n == 0) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt he2 = hostfromname(by, AF_INET);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_gabnresponse_free(lwrctx, &by);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (he2 == NULL) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = NO_RECOVERY;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt goto cleanup;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt } else if (he1 == NULL) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (n == LWRES_R_NOTFOUND)
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = HOST_NOT_FOUND;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt else
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = NO_RECOVERY;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt goto cleanup;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt } else
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = tmp_err;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt he3 = copyandmerge(he1, he2, af, error_num);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt cleanup:
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (he1 != NULL)
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_freehostent(he1);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (he2 != NULL)
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_freehostent(he2);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (lwrctx != NULL) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_conf_clear(lwrctx);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_context_destroy(&lwrctx);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (he3);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt}
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntstruct hostent *
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntlwres_getipnodebyaddr(const void *src, size_t len, int af, int *error_num) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt struct hostent *he1, *he2;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_context_t *lwrctx = NULL;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_gnbaresponse_t *by = NULL;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_result_t n;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt union {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt const void *konst;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt struct in6_addr *in6;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt } u;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt /*
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * Sanity checks.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt */
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (src == NULL) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = NO_RECOVERY;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (NULL);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt switch (af) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt case AF_INET:
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (len != INADDRSZ) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = NO_RECOVERY;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (NULL);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt break;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt case AF_INET6:
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (len != IN6ADDRSZ) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = NO_RECOVERY;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (NULL);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt break;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt default:
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = NO_RECOVERY;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (NULL);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt /*
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * The de-"const"-ing game is done because at least one
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * vendor's system (RedHat 6.0) defines the IN6_IS_ADDR_*
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * macros in such a way that they discard the const with
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * internal casting, and gcc ends up complaining. Rather
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * than replacing their own (possibly optimized) definitions
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * with our own, cleanly discarding the const is the easiest
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * thing to do.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt */
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt u.konst = src;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt /*
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * Look up IPv4 and IPv4 mapped/compatible addresses.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt */
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if ((af == AF_INET6 && IN6_IS_ADDR_V4COMPAT(u.in6)) ||
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (af == AF_INET6 && IN6_IS_ADDR_V4MAPPED(u.in6)) ||
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (af == AF_INET)) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt const unsigned char *cp = src;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (af == AF_INET6)
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt cp += 12;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (n == LWRES_R_SUCCESS)
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (n == LWRES_R_SUCCESS)
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt n = lwres_getnamebyaddr(lwrctx, LWRES_ADDRTYPE_V4,
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt INADDRSZ, cp, &by);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (n != LWRES_R_SUCCESS) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_conf_clear(lwrctx);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_context_destroy(&lwrctx);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (n == LWRES_R_NOTFOUND)
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = HOST_NOT_FOUND;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt else
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = NO_RECOVERY;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (NULL);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt he1 = hostfromaddr(by, AF_INET, cp);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_gnbaresponse_free(lwrctx, &by);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_conf_clear(lwrctx);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_context_destroy(&lwrctx);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (af != AF_INET6)
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (he1);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt /*
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * Convert from AF_INET to AF_INET6.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt */
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt he2 = copyandmerge(he1, NULL, af, error_num);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_freehostent(he1);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (he2 == NULL)
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (NULL);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt /*
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * Restore original address.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt */
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt memcpy(he2->h_addr, src, len);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (he2);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt /*
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * Lookup IPv6 address.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt */
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (memcmp(src, &in6addr_any, IN6ADDRSZ) == 0) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = HOST_NOT_FOUND;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (NULL);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (n == LWRES_R_SUCCESS)
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (n == LWRES_R_SUCCESS)
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt n = lwres_getnamebyaddr(lwrctx, LWRES_ADDRTYPE_V6, IN6ADDRSZ,
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt src, &by);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (n != 0) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = HOST_NOT_FOUND;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (NULL);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt he1 = hostfromaddr(by, AF_INET6, src);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_gnbaresponse_free(lwrctx, &by);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt if (he1 == NULL)
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *error_num = NO_RECOVERY;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt lwres_context_destroy(&lwrctx);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (he1);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt}
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntvoid
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntlwres_freehostent(struct hostent *he) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt char **cpp;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt int names = 1;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt int addresses = 1;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt free(he->h_name);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt cpp = he->h_addr_list;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt while (*cpp != NULL) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt free(*cpp);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *cpp = NULL;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt cpp++;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt addresses++;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt cpp = he->h_aliases;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt while (*cpp != NULL) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt free(*cpp);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt cpp++;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt names++;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt }
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt free(he->h_aliases);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt free(he->h_addr_list);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt free(he);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt}
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt/*
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * Private
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt */
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt/*
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * Scan the interface table and set have_v4 and have_v6 depending
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * upon whether there are IPv4 and IPv6 interface addresses.
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt *
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * Returns:
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews * 0 on success
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews * -1 on failure.
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews */
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrewsstatic int
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntscan_interfaces(int *have_v4, int *have_v6) {
#if 1
*have_v4 = *have_v6 = 1;
return (0);
#else
struct ifconf ifc;
struct ifreq ifreq;
struct in_addr in4;
struct in6_addr in6;
char *buf = NULL, *cp, *cplim;
static int bufsiz = 4095;
int s, cpsize, n;
/*
* Set to zero. Used as loop terminators below.
*/
*have_v4 = *have_v6 = 0;
/*
* Get interface list from system.
*/
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
goto err_ret;
/*
* Grow buffer until large enough to contain all interface
* descriptions.
*/
for (;;) {
buf = malloc(bufsiz);
if (buf == NULL)
goto err_ret;
ifc.ifc_len = bufsiz;
ifc.ifc_buf = buf;
#ifdef IRIX_EMUL_IOCTL_SIOCGIFCONF
/*
* This is a fix for IRIX OS in which the call to ioctl with
* the flag SIOCGIFCONF may not return an entry for all the
* interfaces like most flavors of Unix.
*/
if (emul_ioctl(&ifc) >= 0)
break;
#else
if ((n = ioctl(s, SIOCGIFCONF, (char *)&ifc)) != -1) {
/*
* Some OS's just return what will fit rather
* than set EINVAL if the buffer is too small
* to fit all the interfaces in. If
* ifc.ifc_len is too near to the end of the
* buffer we will grow it just in case and
* retry.
*/
if (ifc.ifc_len + 2 * sizeof(ifreq) < bufsiz)
break;
}
#endif
if ((n == -1) && errno != EINVAL)
goto err_ret;
if (bufsiz > 1000000)
goto err_ret;
free(buf);
bufsiz += 4096;
}
/*
* Parse system's interface list.
*/
cplim = buf + ifc.ifc_len; /* skip over if's with big ifr_addr's */
for (cp = buf;
(*have_v4 == 0 || *have_v6 == 0) && cp < cplim;
cp += cpsize) {
memcpy(&ifreq, cp, sizeof(ifreq));
#ifdef LWRES_PLATFORM_HAVESALEN
#ifdef FIX_ZERO_SA_LEN
if (ifreq.ifr_addr.sa_len == 0)
ifreq.ifr_addr.sa_len = IN6ADDRSZ;
#endif
#ifdef HAVE_MINIMUM_IFREQ
cpsize = sizeof(ifreq);
if (ifreq.ifr_addr.sa_len > sizeof(struct sockaddr))
cpsize += (int)ifreq.ifr_addr.sa_len -
(int)(sizeof(struct sockaddr));
#else
cpsize = sizeof(ifreq.ifr_name) + ifreq.ifr_addr.sa_len;
#endif /* HAVE_MINIMUM_IFREQ */
#elif defined SIOCGIFCONF_ADDR
cpsize = sizeof(ifreq);
#else
cpsize = sizeof(ifreq.ifr_name);
/* XXX maybe this should be a hard error? */
if (ioctl(s, SIOCGIFADDR, (char *)&ifreq) < 0)
continue;
#endif /* LWRES_PLATFORM_HAVESALEN */
switch (ifreq.ifr_addr.sa_family) {
case AF_INET:
if (*have_v4 == 0) {
memcpy(&in4,
&((struct sockaddr_in *)
&ifreq.ifr_addr)->sin_addr,
sizeof(in4));
if (in4.s_addr == INADDR_ANY)
break;
n = ioctl(s, SIOCGIFFLAGS, (char *)&ifreq);
if (n < 0)
break;
if ((ifreq.ifr_flags & IFF_UP) == 0)
break;
*have_v4 = 1;
}
break;
case AF_INET6:
if (*have_v6 == 0) {
memcpy(&in6,
&((struct sockaddr_in6 *)
&ifreq.ifr_addr)->sin6_addr,
sizeof(in6));
if (memcmp(&in6, &in6addr_any,
sizeof(in6)) == 0)
break;
n = ioctl(s, SIOCGIFFLAGS, (char *)&ifreq);
if (n < 0)
break;
if ((ifreq.ifr_flags & IFF_UP) == 0)
break;
*have_v6 = 1;
}
break;
}
}
if (buf != NULL)
free(buf);
close(s);
return (0);
err_ret:
if (buf != NULL)
free(buf);
if (s != -1)
close(s);
return (-1);
#endif
}
static struct hostent *
copyandmerge(struct hostent *he1, struct hostent *he2, int af, int *error_num)
{
struct hostent *he = NULL;
int addresses = 1; /* NULL terminator */
int names = 1; /* NULL terminator */
int len = 0;
char **cpp, **npp;
/*
* Work out array sizes.
*/
if (he1 != NULL) {
cpp = he1->h_addr_list;
while (*cpp != NULL) {
addresses++;
cpp++;
}
cpp = he1->h_aliases;
while (*cpp != NULL) {
names++;
cpp++;
}
}
if (he2 != NULL) {
cpp = he2->h_addr_list;
while (*cpp != NULL) {
addresses++;
cpp++;
}
if (he1 == NULL) {
cpp = he2->h_aliases;
while (*cpp != NULL) {
names++;
cpp++;
}
}
}
if (addresses == 1) {
*error_num = NO_ADDRESS;
return (NULL);
}
he = malloc(sizeof(*he));
if (he == NULL)
goto no_recovery;
he->h_addr_list = malloc(sizeof(char *) * (addresses));
if (he->h_addr_list == NULL)
goto cleanup0;
memset(he->h_addr_list, 0, sizeof(char *) * (addresses));
/*
* Copy addresses.
*/
npp = he->h_addr_list;
if (he1 != NULL) {
cpp = he1->h_addr_list;
while (*cpp != NULL) {
*npp = malloc((af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
if (*npp == NULL)
goto cleanup1;
/*
* Convert to mapped if required.
*/
if (af == AF_INET6 && he1->h_addrtype == AF_INET) {
memcpy(*npp, in6addr_mapped,
sizeof(in6addr_mapped));
memcpy(*npp + sizeof(in6addr_mapped), *cpp,
INADDRSZ);
} else {
memcpy(*npp, *cpp,
(af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
}
cpp++;
npp++;
}
}
if (he2 != NULL) {
cpp = he2->h_addr_list;
while (*cpp != NULL) {
*npp = malloc((af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
if (*npp == NULL)
goto cleanup1;
/*
* Convert to mapped if required.
*/
if (af == AF_INET6 && he2->h_addrtype == AF_INET) {
memcpy(*npp, in6addr_mapped,
sizeof(in6addr_mapped));
memcpy(*npp + sizeof(in6addr_mapped), *cpp,
INADDRSZ);
} else {
memcpy(*npp, *cpp,
(af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
}
cpp++;
npp++;
}
}
he->h_aliases = malloc(sizeof(char *) * (names));
if (he->h_aliases == NULL)
goto cleanup1;
memset(he->h_aliases, 0, sizeof(char *) * (names));
/*
* Copy aliases.
*/
npp = he->h_aliases;
cpp = (he1 != NULL) ? he1->h_aliases : he2->h_aliases;
while (*cpp != NULL) {
len = strlen (*cpp) + 1;
*npp = malloc(len);
if (*npp == NULL)
goto cleanup2;
strcpy(*npp, *cpp);
npp++;
cpp++;
}
/*
* Copy hostname.
*/
he->h_name = malloc(strlen((he1 != NULL) ?
he1->h_name : he2->h_name) + 1);
if (he->h_name == NULL)
goto cleanup2;
strcpy(he->h_name, (he1 != NULL) ? he1->h_name : he2->h_name);
/*
* Set address type and length.
*/
he->h_addrtype = af;
he->h_length = (af == AF_INET) ? INADDRSZ : IN6ADDRSZ;
return (he);
cleanup2:
cpp = he->h_aliases;
while (*cpp != NULL) {
free(*cpp);
cpp++;
}
free(he->h_aliases);
cleanup1:
cpp = he->h_addr_list;
while (*cpp != NULL) {
free(*cpp);
*cpp = NULL;
cpp++;
}
free(he->h_addr_list);
cleanup0:
free(he);
no_recovery:
*error_num = NO_RECOVERY;
return (NULL);
}
static struct hostent *
hostfromaddr(lwres_gnbaresponse_t *addr, int af, const void *src) {
struct hostent *he;
int i;
he = malloc(sizeof(*he));
if (he == NULL)
goto cleanup;
memset(he, 0, sizeof(*he));
/*
* Set family and length.
*/
he->h_addrtype = af;
switch (af) {
case AF_INET:
he->h_length = INADDRSZ;
break;
case AF_INET6:
he->h_length = IN6ADDRSZ;
break;
default:
INSIST(0);
}
/*
* Copy name.
*/
he->h_name = strdup(addr->realname);
if (he->h_name == NULL)
goto cleanup;
/*
* Copy aliases.
*/
he->h_aliases = malloc(sizeof(char *) * (addr->naliases + 1));
if (he->h_aliases == NULL)
goto cleanup;
for (i = 0; i < addr->naliases; i++) {
he->h_aliases[i] = strdup(addr->aliases[i]);
if (he->h_aliases[i] == NULL)
goto cleanup;
}
he->h_aliases[i] = NULL;
/*
* Copy address.
*/
he->h_addr_list = malloc(sizeof(char *) * 2);
if (he->h_addr_list == NULL)
goto cleanup;
he->h_addr_list[0] = malloc(he->h_length);
if (he->h_addr_list[0] == NULL)
goto cleanup;
memcpy(he->h_addr_list[0], src, he->h_length);
he->h_addr_list[1] = NULL;
return (he);
cleanup:
if (he != NULL && he->h_addr_list != NULL) {
for (i = 0; he->h_addr_list[i] != NULL; i++)
free(he->h_addr_list[i]);
free(he->h_addr_list);
}
if (he != NULL && he->h_aliases != NULL) {
for (i = 0; he->h_aliases[i] != NULL; i++)
free(he->h_aliases[i]);
free(he->h_aliases);
}
if (he != NULL && he->h_name != NULL)
free(he->h_name);
if (he != NULL)
free(he);
return (NULL);
}
static struct hostent *
hostfromname(lwres_gabnresponse_t *name, int af) {
struct hostent *he;
int i;
lwres_addr_t *addr;
he = malloc(sizeof(*he));
if (he == NULL)
goto cleanup;
memset(he, 0, sizeof(*he));
/*
* Set family and length.
*/
he->h_addrtype = af;
switch (af) {
case AF_INET:
he->h_length = INADDRSZ;
break;
case AF_INET6:
he->h_length = IN6ADDRSZ;
break;
default:
INSIST(0);
}
/*
* Copy name.
*/
he->h_name = strdup(name->realname);
if (he->h_name == NULL)
goto cleanup;
/*
* Copy aliases.
*/
he->h_aliases = malloc(sizeof(char *) * (name->naliases + 1));
for (i = 0; i < name->naliases; i++) {
he->h_aliases[i] = strdup(name->aliases[i]);
if (he->h_aliases[i] == NULL)
goto cleanup;
}
he->h_aliases[i] = NULL;
/*
* Copy addresses.
*/
he->h_addr_list = malloc(sizeof(char *) * (name->naddrs + 1));
addr = LWRES_LIST_HEAD(name->addrs);
i = 0;
while (addr != NULL) {
he->h_addr_list[i] = malloc(he->h_length);
if (he->h_addr_list[i] == NULL)
goto cleanup;
memcpy(he->h_addr_list[i], addr->address, he->h_length);
addr = LWRES_LIST_NEXT(addr, link);
i++;
}
he->h_addr_list[i] = NULL;
return (he);
cleanup:
if (he != NULL && he->h_addr_list != NULL) {
for (i = 0; he->h_addr_list[i] != NULL; i++)
free(he->h_addr_list[i]);
free(he->h_addr_list);
}
if (he != NULL && he->h_aliases != NULL) {
for (i = 0; he->h_aliases[i] != NULL; i++)
free(he->h_aliases[i]);
free(he->h_aliases);
}
if (he != NULL && he->h_name != NULL)
free(he->h_name);
if (he != NULL)
free(he);
return (NULL);
}