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.
20 #if defined(WIN32) && !defined(WATT32)
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
27 #ifdef HAVE_ARPA_NAMESER_H
28 #include <arpa/nameser.h>
30 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
31 #include <arpa/nameser_compat.h>
43 #include "inet_net_pton.h"
44 #include "ares_private.h"
46 int ares_parse_aaaa_reply(const unsigned char *abuf, int alen,
47 struct hostent **host, struct addr6ttl *addrttls,
50 unsigned int qdcount, ancount;
51 int status, i, rr_type, rr_class, rr_len, rr_ttl, naddrs;
52 int cname_ttl = INT_MAX; /* the TTL imposed by the CNAME chain */
55 const unsigned char *aptr;
56 char *hostname, *rr_name, *rr_data, **aliases;
57 struct in6_addr *addrs;
58 struct hostent *hostent;
59 const int max_addr_ttls = (addrttls && naddrttls) ? *naddrttls : 0;
61 /* Set *host to NULL for all failure cases. */
64 /* Same with *naddrttls. */
68 /* Give up if abuf doesn't have room for a header. */
72 /* Fetch the question and answer count from the header. */
73 qdcount = DNS_HEADER_QDCOUNT(abuf);
74 ancount = DNS_HEADER_ANCOUNT(abuf);
78 /* Expand the name from the question, and skip past the question. */
79 aptr = abuf + HFIXEDSZ;
80 status = ares_expand_name(aptr, abuf, alen, &hostname, &len);
81 if (status != ARES_SUCCESS)
83 if (aptr + len + QFIXEDSZ > abuf + alen)
88 aptr += len + QFIXEDSZ;
90 /* Allocate addresses and aliases; ancount gives an upper bound for both. */
93 addrs = malloc(ancount * sizeof(struct in6_addr));
99 aliases = malloc((ancount + 1) * sizeof(char *));
115 /* Examine each answer resource record (RR) in turn. */
116 for (i = 0; i < (int)ancount; i++)
118 /* Decode the RR up to the data field. */
119 status = ares_expand_name(aptr, abuf, alen, &rr_name, &len);
120 if (status != ARES_SUCCESS)
123 if (aptr + RRFIXEDSZ > abuf + alen)
125 status = ARES_EBADRESP;
128 rr_type = DNS_RR_TYPE(aptr);
129 rr_class = DNS_RR_CLASS(aptr);
130 rr_len = DNS_RR_LEN(aptr);
131 rr_ttl = DNS_RR_TTL(aptr);
134 if (rr_class == C_IN && rr_type == T_AAAA
135 && rr_len == sizeof(struct in6_addr)
136 && strcasecmp(rr_name, hostname) == 0)
140 if (aptr + sizeof(struct in6_addr) > abuf + alen)
142 status = ARES_EBADRESP;
145 memcpy(&addrs[naddrs], aptr, sizeof(struct in6_addr));
147 if (naddrs < max_addr_ttls)
149 struct addr6ttl * const at = &addrttls[naddrs];
150 if (aptr + sizeof(struct in6_addr) > abuf + alen)
152 status = ARES_EBADRESP;
155 memcpy(&at->ip6addr, aptr, sizeof(struct in6_addr));
159 status = ARES_SUCCESS;
162 if (rr_class == C_IN && rr_type == T_CNAME)
164 /* Record the RR name as an alias. */
166 aliases[naliases] = rr_name;
171 /* Decode the RR data and replace the hostname with it. */
172 status = ares_expand_name(aptr, abuf, alen, &rr_data, &len);
173 if (status != ARES_SUCCESS)
178 /* Take the min of the TTLs we see in the CNAME chain. */
179 if (cname_ttl > rr_ttl)
186 if (aptr > abuf + alen)
188 status = ARES_EBADRESP;
193 if (status == ARES_SUCCESS && naddrs == 0)
194 status = ARES_ENODATA;
195 if (status == ARES_SUCCESS)
197 /* We got our answer. */
200 const int n = naddrs < max_addr_ttls ? naddrs : max_addr_ttls;
201 for (i = 0; i < n; i++)
203 /* Ensure that each A TTL is no larger than the CNAME TTL. */
204 if (addrttls[i].ttl > cname_ttl)
205 addrttls[i].ttl = cname_ttl;
210 aliases[naliases] = NULL;
213 /* Allocate memory to build the host entry. */
214 hostent = malloc(sizeof(struct hostent));
217 hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
218 if (hostent->h_addr_list)
220 /* Fill in the hostent and return successfully. */
221 hostent->h_name = hostname;
222 hostent->h_aliases = aliases;
223 hostent->h_addrtype = AF_INET6;
224 hostent->h_length = sizeof(struct in6_addr);
225 for (i = 0; i < naddrs; i++)
226 hostent->h_addr_list[i] = (char *) &addrs[i];
227 hostent->h_addr_list[naddrs] = NULL;
233 status = ARES_ENOMEM;
238 for (i = 0; i < naliases; i++)