2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Copyright 2005 Dominick Meglio
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for any purpose and without
7 * fee is hereby granted, provided that the above copyright
8 * notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting
10 * documentation, and that the name of M.I.T. not be used in
11 * advertising or publicity pertaining to distribution of the
12 * software without specific, written prior permission.
13 * M.I.T. makes no representations about the suitability of
14 * this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
18 #include "ares_setup.h"
20 #ifdef HAVE_NETINET_IN_H
21 # include <netinet/in.h>
26 #ifdef HAVE_ARPA_INET_H
27 # include <arpa/inet.h>
29 #ifdef HAVE_ARPA_NAMESER_H
30 # include <arpa/nameser.h>
34 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
35 # include <arpa/nameser_compat.h>
48 #include "inet_net_pton.h"
49 #include "ares_private.h"
51 int ares_parse_aaaa_reply(const unsigned char *abuf, int alen,
52 struct hostent **host, struct ares_addr6ttl *addrttls,
55 unsigned int qdcount, ancount;
56 int status, i, rr_type, rr_class, rr_len, rr_ttl, naddrs;
57 int cname_ttl = INT_MAX; /* the TTL imposed by the CNAME chain */
60 const unsigned char *aptr;
61 char *hostname, *rr_name, *rr_data, **aliases;
62 struct ares_in6_addr *addrs;
63 struct hostent *hostent;
64 const int max_addr_ttls = (addrttls && naddrttls) ? *naddrttls : 0;
66 /* Set *host to NULL for all failure cases. */
69 /* Same with *naddrttls. */
73 /* Give up if abuf doesn't have room for a header. */
77 /* Fetch the question and answer count from the header. */
78 qdcount = DNS_HEADER_QDCOUNT(abuf);
79 ancount = DNS_HEADER_ANCOUNT(abuf);
83 /* Expand the name from the question, and skip past the question. */
84 aptr = abuf + HFIXEDSZ;
85 status = ares__expand_name_for_response(aptr, abuf, alen, &hostname, &len);
86 if (status != ARES_SUCCESS)
88 if (aptr + len + QFIXEDSZ > abuf + alen)
93 aptr += len + QFIXEDSZ;
95 /* Allocate addresses and aliases; ancount gives an upper bound for both. */
98 addrs = malloc(ancount * sizeof(struct ares_in6_addr));
104 aliases = malloc((ancount + 1) * sizeof(char *));
120 /* Examine each answer resource record (RR) in turn. */
121 for (i = 0; i < (int)ancount; i++)
123 /* Decode the RR up to the data field. */
124 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
125 if (status != ARES_SUCCESS)
128 if (aptr + RRFIXEDSZ > abuf + alen)
131 status = ARES_EBADRESP;
134 rr_type = DNS_RR_TYPE(aptr);
135 rr_class = DNS_RR_CLASS(aptr);
136 rr_len = DNS_RR_LEN(aptr);
137 rr_ttl = DNS_RR_TTL(aptr);
140 if (rr_class == C_IN && rr_type == T_AAAA
141 && rr_len == sizeof(struct ares_in6_addr)
142 && strcasecmp(rr_name, hostname) == 0)
146 if (aptr + sizeof(struct ares_in6_addr) > abuf + alen)
149 status = ARES_EBADRESP;
152 memcpy(&addrs[naddrs], aptr, sizeof(struct ares_in6_addr));
154 if (naddrs < max_addr_ttls)
156 struct ares_addr6ttl * const at = &addrttls[naddrs];
157 if (aptr + sizeof(struct ares_in6_addr) > abuf + alen)
160 status = ARES_EBADRESP;
163 memcpy(&at->ip6addr, aptr, sizeof(struct ares_in6_addr));
167 status = ARES_SUCCESS;
170 if (rr_class == C_IN && rr_type == T_CNAME)
172 /* Record the RR name as an alias. */
174 aliases[naliases] = rr_name;
179 /* Decode the RR data and replace the hostname with it. */
180 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
182 if (status != ARES_SUCCESS)
187 /* Take the min of the TTLs we see in the CNAME chain. */
188 if (cname_ttl > rr_ttl)
195 if (aptr > abuf + alen)
197 status = ARES_EBADRESP;
202 /* the check for naliases to be zero is to make sure CNAME responses
203 don't get caught here */
204 if (status == ARES_SUCCESS && naddrs == 0 && naliases == 0)
205 status = ARES_ENODATA;
206 if (status == ARES_SUCCESS)
208 /* We got our answer. */
211 const int n = naddrs < max_addr_ttls ? naddrs : max_addr_ttls;
212 for (i = 0; i < n; i++)
214 /* Ensure that each A TTL is no larger than the CNAME TTL. */
215 if (addrttls[i].ttl > cname_ttl)
216 addrttls[i].ttl = cname_ttl;
221 aliases[naliases] = NULL;
224 /* Allocate memory to build the host entry. */
225 hostent = malloc(sizeof(struct hostent));
228 hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
229 if (hostent->h_addr_list)
231 /* Fill in the hostent and return successfully. */
232 hostent->h_name = hostname;
233 hostent->h_aliases = aliases;
234 hostent->h_addrtype = AF_INET6;
235 hostent->h_length = sizeof(struct ares_in6_addr);
236 for (i = 0; i < naddrs; i++)
237 hostent->h_addr_list[i] = (char *) &addrs[i];
238 hostent->h_addr_list[naddrs] = NULL;
244 status = ARES_ENOMEM;
249 for (i = 0; i < naliases; i++)