getipnode.c revision ec5347e2c775f027573ce5648b910361aa926c01
/*
* Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1999-2003 Internet Software Consortium.
*
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: getipnode.c,v 1.42 2007/06/18 23:47:51 tbox Exp $ */
/*! \file */
/**
* These functions perform thread safe, protocol independent
* nodename-to-address and address-to-nodename translation as defined in
* RFC2553. This use a struct hostent which is defined in namedb.h:
*
* \code
* struct hostent {
* char *h_name; // official name of host
* char **h_aliases; // alias list
* int h_addrtype; // host address type
* int h_length; // length of address
* char **h_addr_list; // list of addresses from name server
* };
* #define h_addr h_addr_list[0] // address, for backward compatibility
* \endcode
*
* The members of this structure are:
*
* \li h_name:
* The official (canonical) name of the host.
*
* \li h_aliases:
* A NULL-terminated array of alternate names (nicknames) for the
* host.
*
* \li h_addrtype:
* The type of address being returned - usually PF_INET or
* PF_INET6.
*
* \li h_length:
* The length of the address in bytes.
*
* \li h_addr_list:
* A NULL terminated array of network addresses for the host. Host
* addresses are returned in network byte order.
*
* lwres_getipnodebyname() looks up addresses of protocol family af for
* the hostname name. The flags parameter contains ORed flag bits to
* specify the types of addresses that are searched for, and the types of
* addresses that are returned. The flag bits are:
*
* \li #AI_V4MAPPED:
* This is used with an af of #AF_INET6, and causes IPv4 addresses
* to be returned as IPv4-mapped IPv6 addresses.
*
* \li #AI_ALL:
* This is used with an af of #AF_INET6, and causes all known
* addresses (IPv6 and IPv4) to be returned. If #AI_V4MAPPED is
* also set, the IPv4 addresses are return as mapped IPv6
* addresses.
*
* \li #AI_ADDRCONFIG:
* Only return an IPv6 or IPv4 address if here is an active
* network interface of that type. This is not currently
* implemented in the BIND 9 lightweight resolver, and the flag is
* ignored.
*
* \li #AI_DEFAULT:
* This default sets the #AI_V4MAPPED and #AI_ADDRCONFIG flag bits.
*
* lwres_getipnodebyaddr() performs a reverse lookup of address src which
* is len bytes long. af denotes the protocol family, typically PF_INET
* or PF_INET6.
*
* lwres_freehostent() releases all the memory associated with the struct
* hostent pointer. Any memory allocated for the h_name, h_addr_list
* and h_aliases is freed, as is the memory for the hostent structure
* itself.
*
* \section getipnode_return Return Values
*
* If an error occurs, lwres_getipnodebyname() and
* lwres_getipnodebyaddr() set *error_num to an appropriate error code
* and the function returns a NULL pointer. The error codes and their
*
* \li #HOST_NOT_FOUND:
* No such host is known.
*
* \li #NO_ADDRESS:
* The server recognised the request and the name but no address
* is available. Another type of request to the name server for
* the domain might return an answer.
*
* \li #TRY_AGAIN:
* A temporary and possibly transient error occurred, such as a
* failure of a server to respond. The request may succeed if
* retried.
*
* \li #NO_RECOVERY:
* An unexpected failure occurred, and retrying the request is
* pointless.
*
* lwres_hstrerror() translates these error codes to suitable error
* messages.
*
* \section getipnode_see See Also
*
* getaddrinfo.c, gethost.c, getnameinfo.c, herror.c, RFC2553
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "assert_p.h"
#ifndef INADDRSZ
#define INADDRSZ 4
#endif
#ifndef IN6ADDRSZ
#define IN6ADDRSZ 16
#endif
#endif
#ifndef IN6_IS_ADDR_V4COMPAT
static const unsigned char in6addr_compat[12] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
((x)->s6_addr[12] != 0 || \
(x)->s6_addr[13] != 0 || \
(x)->s6_addr[14] != 0 || \
((x)->s6_addr[15] != 0 && \
#endif
#ifndef IN6_IS_ADDR_V4MAPPED
#endif
static const unsigned char in6addr_mapped[12] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff
};
/***
*** Forward declarations.
***/
static int
scan_interfaces(int *, int *);
static struct hostent *
static struct hostent *
static struct hostent *
/***
*** Public functions.
***/
/*!
* AI_V4MAPPED + AF_INET6
* If no IPv6 address then a query for IPv4 and map returned values.
*
* AI_ALL + AI_V4MAPPED + AF_INET6
* Return IPv6 and IPv4 mapped.
*
* AI_ADDRCONFIG
* Only return IPv6 / IPv4 address if there is an interface of that
* type active.
*/
struct hostent *
int tmp_err;
int n;
/*
* If we care about active interfaces then check.
*/
if ((flags & AI_ADDRCONFIG) != 0)
*error_num = NO_RECOVERY;
return (NULL);
}
/* Check for literal address. */
/*
* Impossible combination?
*/
(flags & AI_V4MAPPED) == 0))) {
return (NULL);
}
/*
* Literal address?
*/
char *addr_list[2];
char *aliases[1];
char mappedname[sizeof("::ffff:123.123.123.123")];
union {
const char *const_name;
char *deconst_name;
} u;
u.const_name = name;
sizeof(mappedname) - sizeof("::ffff:")
+ 1);
} else
}
if (n != 0) {
*error_num = NO_RECOVERY;
goto cleanup;
}
if (n == 0) {
*error_num = NO_RECOVERY;
goto cleanup;
}
} else {
}
}
if (have_v4 &&
if (n == 0) {
*error_num = NO_RECOVERY;
goto cleanup;
}
if (n == LWRES_R_NOTFOUND)
else
*error_num = NO_RECOVERY;
goto cleanup;
}
} else
}
return (he3);
}
/*% performs a reverse lookup of address src which is len bytes long. af denotes the protocol family, typically #PF_INET or PF_INET6. */
struct hostent *
union {
const void *konst;
} u;
/*
* Sanity checks.
*/
*error_num = NO_RECOVERY;
return (NULL);
}
switch (af) {
case AF_INET:
*error_num = NO_RECOVERY;
return (NULL);
}
break;
case AF_INET6:
*error_num = NO_RECOVERY;
return (NULL);
}
break;
default:
*error_num = NO_RECOVERY;
return (NULL);
}
/*
* The de-"const"-ing game is done because at least one
* vendor's system (RedHat 6.0) defines the IN6_IS_ADDR_*
* macros in such a way that they discard the const with
* internal casting, and gcc ends up complaining. Rather
* than replacing their own (possibly optimized) definitions
* with our own, cleanly discarding the const is the easiest
* thing to do.
*/
/*
* Look up IPv4 and IPv4 mapped/compatible addresses.
*/
cp += 12;
if (n == LWRES_R_SUCCESS)
if (n == LWRES_R_SUCCESS)
if (n != LWRES_R_SUCCESS) {
if (n == LWRES_R_NOTFOUND)
else
*error_num = NO_RECOVERY;
return (NULL);
}
return (he1);
/*
* Convert from AF_INET to AF_INET6.
*/
return (NULL);
/*
* Restore original address.
*/
return (he2);
}
/*
* Lookup IPv6 address.
*/
return (NULL);
}
if (n == LWRES_R_SUCCESS)
if (n == LWRES_R_SUCCESS)
if (n != 0) {
return (NULL);
}
*error_num = NO_RECOVERY;
return (he1);
}
/*% releases all the memory associated with the struct hostent pointer */
void
char **cpp;
int names = 1;
int addresses = 1;
cpp++;
addresses++;
}
cpp++;
names++;
}
}
/*
* Private
*/
/*
* Scan the interface table and set have_v4 and have_v6 depending
* upon whether there are IPv4 and IPv6 interface addresses.
*
* Returns:
* 0 on success
* -1 on failure.
*/
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR) && \
!defined(IRIX_EMUL_IOCTL_SIOCGIFCONF)
#ifdef __hpux
#define LIFCONF if_laddrconf
#else
#define ISC_HAVE_LIFC_FAMILY 1
#define ISC_HAVE_LIFC_FLAGS 1
#endif
#ifdef __hpux
#define lifr_dstaddr iflr_dstaddr
#define lifr_flags iflr_flags
#define LIFREQ if_laddrreq
#else
#endif
static int
static unsigned int bufsiz = 4095;
int s, cpsize, n;
/*
* Set to zero. Used as loop terminators below.
*/
/*
* Get interface list from system.
*/
goto err_ret;
/*
* Grow buffer until large enough to contain all interface
* descriptions.
*/
for (;;) {
goto err_ret;
#ifdef ISC_HAVE_LIFC_FAMILY
#endif
#ifdef ISC_HAVE_LIFC_FLAGS
lifc.lifc_flags = 0;
#endif
/*
* 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
* lifc.lifc_len is too near to the end of the
* buffer we will grow it just in case and
* retry.
*/
break;
}
goto err_ret;
if (bufsiz > 1000000)
goto err_ret;
bufsiz += 4096;
}
/*
* Parse system's interface list.
*/
#ifdef LWRES_PLATFORM_HAVESALEN
#ifdef FIX_ZERO_SA_LEN
#endif
#ifdef HAVE_MINIMUM_IFREQ
(int)(sizeof(struct sockaddr));
#else
#endif /* HAVE_MINIMUM_IFREQ */
#elif defined SIOCGIFCONF_ADDR
#else
/* XXX maybe this should be a hard error? */
continue;
#endif
case AF_INET:
if (*have_v4 == 0) {
&((struct sockaddr_in *)
sizeof(in4));
break;
if (n < 0)
break;
break;
*have_v4 = 1;
}
break;
case AF_INET6:
if (*have_v6 == 0) {
&((struct sockaddr_in6 *)
sizeof(in6));
sizeof(in6)) == 0)
break;
if (n < 0)
break;
break;
*have_v6 = 1;
}
break;
}
}
close(s);
return (0);
if (s != -1)
close(s);
return (-1);
}
#endif
static int
#if !defined(SIOCGIFCONF) || !defined(SIOCGIFADDR)
return (0);
#else
union {
} u;
static unsigned int bufsiz = 4095;
int s, n;
#ifdef WIN32
InitSockets();
#endif
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR) && \
!defined(IRIX_EMUL_IOCTL_SIOCGIFCONF)
/*
* Try to scan the interfaces using IPv6 ioctls().
*/
#ifdef WIN32
#endif
return (0);
}
#endif
/*
* Set to zero. Used as loop terminators below.
*/
/*
* Get interface list from system.
*/
goto err_ret;
/*
* Grow buffer until large enough to contain all interface
* descriptions.
*/
for (;;) {
goto err_ret;
#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
/*
* 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.
*/
break;
}
#endif
goto err_ret;
if (bufsiz > 1000000)
goto err_ret;
bufsiz += 4096;
}
/*
* Parse system's interface list.
*/
#ifdef LWRES_PLATFORM_HAVESALEN
#ifdef FIX_ZERO_SA_LEN
#endif
#ifdef HAVE_MINIMUM_IFREQ
(int)(sizeof(struct sockaddr));
#else
#endif /* HAVE_MINIMUM_IFREQ */
#elif defined SIOCGIFCONF_ADDR
#else
/* XXX maybe this should be a hard error? */
continue;
#endif
case AF_INET:
if (*have_v4 == 0) {
&((struct sockaddr_in *)
sizeof(in4));
break;
if (n < 0)
break;
break;
*have_v4 = 1;
}
break;
case AF_INET6:
if (*have_v6 == 0) {
&((struct sockaddr_in6 *)
sizeof(in6));
sizeof(in6)) == 0)
break;
if (n < 0)
break;
break;
*have_v6 = 1;
}
break;
}
}
#ifdef WIN32
#endif
close(s);
return (0);
if (s != -1)
close(s);
#ifdef WIN32
#endif
return (-1);
#endif
}
static struct hostent *
{
int len = 0;
/*
* Work out array sizes.
*/
addresses++;
cpp++;
}
names++;
cpp++;
}
}
addresses++;
cpp++;
}
names++;
cpp++;
}
}
}
if (addresses == 1) {
*error_num = NO_ADDRESS;
return (NULL);
}
goto no_recovery;
goto cleanup0;
/*
* Copy addresses.
*/
goto cleanup1;
/*
* Convert to mapped if required.
*/
sizeof(in6addr_mapped));
INADDRSZ);
} else {
}
cpp++;
npp++;
}
}
goto cleanup1;
/*
* Convert to mapped if required.
*/
sizeof(in6addr_mapped));
INADDRSZ);
} else {
}
cpp++;
npp++;
}
}
goto cleanup1;
/*
* Copy aliases.
*/
goto cleanup2;
npp++;
cpp++;
}
/*
* Copy hostname.
*/
goto cleanup2;
/*
* Set address type and length.
*/
return (he);
cpp++;
}
cpp++;
}
*error_num = NO_RECOVERY;
return (NULL);
}
static struct hostent *
int i;
goto cleanup;
/*
* Set family and length.
*/
switch (af) {
case AF_INET:
break;
case AF_INET6:
break;
default:
INSIST(0);
}
/*
* Copy name.
*/
goto cleanup;
/*
* Copy aliases.
*/
goto cleanup;
goto cleanup;
}
/*
* Copy address.
*/
goto cleanup;
goto cleanup;
return (he);
}
}
return (NULL);
}
static struct hostent *
int i;
goto cleanup;
/*
* Set family and length.
*/
switch (af) {
case AF_INET:
break;
case AF_INET6:
break;
default:
INSIST(0);
}
/*
* Copy name.
*/
goto cleanup;
/*
* Copy aliases.
*/
goto cleanup;
}
/*
* Copy addresses.
*/
i = 0;
goto cleanup;
i++;
}
return (he);
}
}
return (NULL);
}