|
Packit |
514978 |
|
|
Packit |
514978 |
/* Copyright 1998 by the Massachusetts Institute of Technology.
|
|
Packit |
514978 |
* Copyright 2005 Dominick Meglio
|
|
Packit |
514978 |
*
|
|
Packit |
514978 |
* Permission to use, copy, modify, and distribute this
|
|
Packit |
514978 |
* software and its documentation for any purpose and without
|
|
Packit |
514978 |
* fee is hereby granted, provided that the above copyright
|
|
Packit |
514978 |
* notice appear in all copies and that both that copyright
|
|
Packit |
514978 |
* notice and this permission notice appear in supporting
|
|
Packit |
514978 |
* documentation, and that the name of M.I.T. not be used in
|
|
Packit |
514978 |
* advertising or publicity pertaining to distribution of the
|
|
Packit |
514978 |
* software without specific, written prior permission.
|
|
Packit |
514978 |
* M.I.T. makes no representations about the suitability of
|
|
Packit |
514978 |
* this software for any purpose. It is provided "as is"
|
|
Packit |
514978 |
* without express or implied warranty.
|
|
Packit |
514978 |
*/
|
|
Packit |
514978 |
|
|
Packit |
514978 |
#include "ares_setup.h"
|
|
Packit |
514978 |
|
|
Packit |
514978 |
#ifdef HAVE_NETINET_IN_H
|
|
Packit |
514978 |
# include <netinet/in.h>
|
|
Packit |
514978 |
#endif
|
|
Packit |
514978 |
#ifdef HAVE_NETDB_H
|
|
Packit |
514978 |
# include <netdb.h>
|
|
Packit |
514978 |
#endif
|
|
Packit |
514978 |
#ifdef HAVE_ARPA_INET_H
|
|
Packit |
514978 |
# include <arpa/inet.h>
|
|
Packit |
514978 |
#endif
|
|
Packit |
514978 |
#ifdef HAVE_ARPA_NAMESER_H
|
|
Packit |
514978 |
# include <arpa/nameser.h>
|
|
Packit |
514978 |
#else
|
|
Packit |
514978 |
# include "nameser.h"
|
|
Packit |
514978 |
#endif
|
|
Packit |
514978 |
#ifdef HAVE_ARPA_NAMESER_COMPAT_H
|
|
Packit |
514978 |
# include <arpa/nameser_compat.h>
|
|
Packit |
514978 |
#endif
|
|
Packit |
514978 |
|
|
Packit |
514978 |
#ifdef HAVE_STRINGS_H
|
|
Packit |
514978 |
# include <strings.h>
|
|
Packit |
514978 |
#endif
|
|
Packit |
514978 |
|
|
Packit |
514978 |
#ifdef HAVE_LIMITS_H
|
|
Packit |
514978 |
# include <limits.h>
|
|
Packit |
514978 |
#endif
|
|
Packit |
514978 |
|
|
Packit |
514978 |
#include "ares.h"
|
|
Packit |
514978 |
#include "ares_dns.h"
|
|
Packit |
514978 |
#include "ares_inet_net_pton.h"
|
|
Packit |
514978 |
#include "ares_private.h"
|
|
Packit |
514978 |
|
|
Packit |
514978 |
int ares_parse_aaaa_reply(const unsigned char *abuf, int alen,
|
|
Packit |
514978 |
struct hostent **host, struct ares_addr6ttl *addrttls,
|
|
Packit |
514978 |
int *naddrttls)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
unsigned int qdcount, ancount;
|
|
Packit |
514978 |
int status, i, rr_type, rr_class, rr_len, rr_ttl, naddrs;
|
|
Packit |
514978 |
int cname_ttl = INT_MAX; /* the TTL imposed by the CNAME chain */
|
|
Packit |
514978 |
int naliases;
|
|
Packit |
514978 |
long len;
|
|
Packit |
514978 |
const unsigned char *aptr;
|
|
Packit |
514978 |
char *hostname, *rr_name, *rr_data, **aliases;
|
|
Packit |
514978 |
struct ares_in6_addr *addrs;
|
|
Packit |
514978 |
struct hostent *hostent;
|
|
Packit |
514978 |
const int max_addr_ttls = (addrttls && naddrttls) ? *naddrttls : 0;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* Set *host to NULL for all failure cases. */
|
|
Packit |
514978 |
if (host)
|
|
Packit |
514978 |
*host = NULL;
|
|
Packit |
514978 |
/* Same with *naddrttls. */
|
|
Packit |
514978 |
if (naddrttls)
|
|
Packit |
514978 |
*naddrttls = 0;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* Give up if abuf doesn't have room for a header. */
|
|
Packit |
514978 |
if (alen < HFIXEDSZ)
|
|
Packit |
514978 |
return ARES_EBADRESP;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* Fetch the question and answer count from the header. */
|
|
Packit |
514978 |
qdcount = DNS_HEADER_QDCOUNT(abuf);
|
|
Packit |
514978 |
ancount = DNS_HEADER_ANCOUNT(abuf);
|
|
Packit |
514978 |
if (qdcount != 1)
|
|
Packit |
514978 |
return ARES_EBADRESP;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* Expand the name from the question, and skip past the question. */
|
|
Packit |
514978 |
aptr = abuf + HFIXEDSZ;
|
|
Packit |
514978 |
status = ares__expand_name_for_response(aptr, abuf, alen, &hostname, &len;;
|
|
Packit |
514978 |
if (status != ARES_SUCCESS)
|
|
Packit |
514978 |
return status;
|
|
Packit |
514978 |
if (aptr + len + QFIXEDSZ > abuf + alen)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
ares_free(hostname);
|
|
Packit |
514978 |
return ARES_EBADRESP;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
aptr += len + QFIXEDSZ;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* Allocate addresses and aliases; ancount gives an upper bound for both. */
|
|
Packit |
514978 |
if (host)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
addrs = ares_malloc(ancount * sizeof(struct ares_in6_addr));
|
|
Packit |
514978 |
if (!addrs)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
ares_free(hostname);
|
|
Packit |
514978 |
return ARES_ENOMEM;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
aliases = ares_malloc((ancount + 1) * sizeof(char *));
|
|
Packit |
514978 |
if (!aliases)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
ares_free(hostname);
|
|
Packit |
514978 |
ares_free(addrs);
|
|
Packit |
514978 |
return ARES_ENOMEM;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
else
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
addrs = NULL;
|
|
Packit |
514978 |
aliases = NULL;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
naddrs = 0;
|
|
Packit |
514978 |
naliases = 0;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* Examine each answer resource record (RR) in turn. */
|
|
Packit |
514978 |
for (i = 0; i < (int)ancount; i++)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
/* Decode the RR up to the data field. */
|
|
Packit |
514978 |
status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len;;
|
|
Packit |
514978 |
if (status != ARES_SUCCESS)
|
|
Packit |
514978 |
break;
|
|
Packit |
514978 |
aptr += len;
|
|
Packit |
514978 |
if (aptr + RRFIXEDSZ > abuf + alen)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
ares_free(rr_name);
|
|
Packit |
514978 |
status = ARES_EBADRESP;
|
|
Packit |
514978 |
break;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
rr_type = DNS_RR_TYPE(aptr);
|
|
Packit |
514978 |
rr_class = DNS_RR_CLASS(aptr);
|
|
Packit |
514978 |
rr_len = DNS_RR_LEN(aptr);
|
|
Packit |
514978 |
rr_ttl = DNS_RR_TTL(aptr);
|
|
Packit |
514978 |
aptr += RRFIXEDSZ;
|
|
Packit |
514978 |
if (aptr + rr_len > abuf + alen)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
ares_free(rr_name);
|
|
Packit |
514978 |
status = ARES_EBADRESP;
|
|
Packit |
514978 |
break;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
|
|
Packit |
514978 |
if (rr_class == C_IN && rr_type == T_AAAA
|
|
Packit |
514978 |
&& rr_len == sizeof(struct ares_in6_addr)
|
|
Packit |
514978 |
&& strcasecmp(rr_name, hostname) == 0)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
if (addrs)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
if (aptr + sizeof(struct ares_in6_addr) > abuf + alen)
|
|
Packit |
514978 |
{ /* LCOV_EXCL_START: already checked above */
|
|
Packit |
514978 |
ares_free(rr_name);
|
|
Packit |
514978 |
status = ARES_EBADRESP;
|
|
Packit |
514978 |
break;
|
|
Packit |
514978 |
} /* LCOV_EXCL_STOP */
|
|
Packit |
514978 |
memcpy(&addrs[naddrs], aptr, sizeof(struct ares_in6_addr));
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
if (naddrs < max_addr_ttls)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
struct ares_addr6ttl * const at = &addrttls[naddrs];
|
|
Packit |
514978 |
if (aptr + sizeof(struct ares_in6_addr) > abuf + alen)
|
|
Packit |
514978 |
{ /* LCOV_EXCL_START: already checked above */
|
|
Packit |
514978 |
ares_free(rr_name);
|
|
Packit |
514978 |
status = ARES_EBADRESP;
|
|
Packit |
514978 |
break;
|
|
Packit |
514978 |
} /* LCOV_EXCL_STOP */
|
|
Packit |
514978 |
memcpy(&at->ip6addr, aptr, sizeof(struct ares_in6_addr));
|
|
Packit |
514978 |
at->ttl = rr_ttl;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
naddrs++;
|
|
Packit |
514978 |
status = ARES_SUCCESS;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
|
|
Packit |
514978 |
if (rr_class == C_IN && rr_type == T_CNAME)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
/* Record the RR name as an alias. */
|
|
Packit |
514978 |
if (aliases)
|
|
Packit |
514978 |
aliases[naliases] = rr_name;
|
|
Packit |
514978 |
else
|
|
Packit |
514978 |
ares_free(rr_name);
|
|
Packit |
514978 |
naliases++;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* Decode the RR data and replace the hostname with it. */
|
|
Packit |
514978 |
status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
|
|
Packit |
514978 |
&len;;
|
|
Packit |
514978 |
if (status != ARES_SUCCESS)
|
|
Packit |
514978 |
break;
|
|
Packit |
514978 |
ares_free(hostname);
|
|
Packit |
514978 |
hostname = rr_data;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* Take the min of the TTLs we see in the CNAME chain. */
|
|
Packit |
514978 |
if (cname_ttl > rr_ttl)
|
|
Packit |
514978 |
cname_ttl = rr_ttl;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
else
|
|
Packit |
514978 |
ares_free(rr_name);
|
|
Packit |
514978 |
|
|
Packit |
514978 |
aptr += rr_len;
|
|
Packit |
514978 |
if (aptr > abuf + alen)
|
|
Packit |
514978 |
{ /* LCOV_EXCL_START: already checked above */
|
|
Packit |
514978 |
status = ARES_EBADRESP;
|
|
Packit |
514978 |
break;
|
|
Packit |
514978 |
} /* LCOV_EXCL_STOP */
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* the check for naliases to be zero is to make sure CNAME responses
|
|
Packit |
514978 |
don't get caught here */
|
|
Packit |
514978 |
if (status == ARES_SUCCESS && naddrs == 0 && naliases == 0)
|
|
Packit |
514978 |
status = ARES_ENODATA;
|
|
Packit |
514978 |
if (status == ARES_SUCCESS)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
/* We got our answer. */
|
|
Packit |
514978 |
if (naddrttls)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
const int n = naddrs < max_addr_ttls ? naddrs : max_addr_ttls;
|
|
Packit |
514978 |
for (i = 0; i < n; i++)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
/* Ensure that each A TTL is no larger than the CNAME TTL. */
|
|
Packit |
514978 |
if (addrttls[i].ttl > cname_ttl)
|
|
Packit |
514978 |
addrttls[i].ttl = cname_ttl;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
*naddrttls = n;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
if (aliases)
|
|
Packit |
514978 |
aliases[naliases] = NULL;
|
|
Packit |
514978 |
if (host)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
/* Allocate memory to build the host entry. */
|
|
Packit |
514978 |
hostent = ares_malloc(sizeof(struct hostent));
|
|
Packit |
514978 |
if (hostent)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
hostent->h_addr_list = ares_malloc((naddrs + 1) * sizeof(char *));
|
|
Packit |
514978 |
if (hostent->h_addr_list)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
/* Fill in the hostent and return successfully. */
|
|
Packit |
514978 |
hostent->h_name = hostname;
|
|
Packit |
514978 |
hostent->h_aliases = aliases;
|
|
Packit |
514978 |
hostent->h_addrtype = AF_INET6;
|
|
Packit |
514978 |
hostent->h_length = sizeof(struct ares_in6_addr);
|
|
Packit |
514978 |
for (i = 0; i < naddrs; i++)
|
|
Packit |
514978 |
hostent->h_addr_list[i] = (char *) &addrs[i];
|
|
Packit |
514978 |
hostent->h_addr_list[naddrs] = NULL;
|
|
Packit |
514978 |
if (!naddrs && addrs)
|
|
Packit |
514978 |
ares_free(addrs);
|
|
Packit |
514978 |
*host = hostent;
|
|
Packit |
514978 |
return ARES_SUCCESS;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
ares_free(hostent);
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
status = ARES_ENOMEM;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
if (aliases)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
for (i = 0; i < naliases; i++)
|
|
Packit |
514978 |
ares_free(aliases[i]);
|
|
Packit |
514978 |
ares_free(aliases);
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
ares_free(addrs);
|
|
Packit |
514978 |
ares_free(hostname);
|
|
Packit |
514978 |
return status;
|
|
Packit |
514978 |
}
|