3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4 * Copyright 2005 Dominick Meglio
6 * Permission to use, copy, modify, and distribute this
7 * software and its documentation for any purpose and without
8 * fee is hereby granted, provided that the above copyright
9 * notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting
11 * documentation, and that the name of M.I.T. not be used in
12 * advertising or publicity pertaining to distribution of the
13 * software without specific, written prior permission.
14 * M.I.T. makes no representations about the suitability of
15 * this software for any purpose. It is provided "as is"
16 * without express or implied warranty.
21 #ifdef HAVE_SYS_SOCKET_H
22 # include <sys/socket.h>
24 #ifdef HAVE_NETINET_IN_H
25 # include <netinet/in.h>
30 #ifdef HAVE_ARPA_INET_H
31 # include <arpa/inet.h>
33 #ifdef HAVE_ARPA_NAMESER_H
34 # include <arpa/nameser.h>
38 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
39 # include <arpa/nameser_compat.h>
54 #include "inet_net_pton.h"
55 #include "ares_private.h"
57 int ares_parse_aaaa_reply(const unsigned char *abuf, int alen,
58 struct hostent **host, struct addr6ttl *addrttls,
61 unsigned int qdcount, ancount;
62 int status, i, rr_type, rr_class, rr_len, rr_ttl, naddrs;
63 int cname_ttl = INT_MAX; /* the TTL imposed by the CNAME chain */
66 const unsigned char *aptr;
67 char *hostname, *rr_name, *rr_data, **aliases;
68 struct in6_addr *addrs;
69 struct hostent *hostent;
70 const int max_addr_ttls = (addrttls && naddrttls) ? *naddrttls : 0;
72 /* Set *host to NULL for all failure cases. */
75 /* Same with *naddrttls. */
79 /* Give up if abuf doesn't have room for a header. */
83 /* Fetch the question and answer count from the header. */
84 qdcount = DNS_HEADER_QDCOUNT(abuf);
85 ancount = DNS_HEADER_ANCOUNT(abuf);
89 /* Expand the name from the question, and skip past the question. */
90 aptr = abuf + HFIXEDSZ;
91 status = ares__expand_name_for_response(aptr, abuf, alen, &hostname, &len);
92 if (status != ARES_SUCCESS)
94 if (aptr + len + QFIXEDSZ > abuf + alen)
99 aptr += len + QFIXEDSZ;
101 /* Allocate addresses and aliases; ancount gives an upper bound for both. */
104 addrs = malloc(ancount * sizeof(struct in6_addr));
110 aliases = malloc((ancount + 1) * sizeof(char *));
126 /* Examine each answer resource record (RR) in turn. */
127 for (i = 0; i < (int)ancount; i++)
129 /* Decode the RR up to the data field. */
130 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
131 if (status != ARES_SUCCESS)
134 if (aptr + RRFIXEDSZ > abuf + alen)
136 status = ARES_EBADRESP;
139 rr_type = DNS_RR_TYPE(aptr);
140 rr_class = DNS_RR_CLASS(aptr);
141 rr_len = DNS_RR_LEN(aptr);
142 rr_ttl = DNS_RR_TTL(aptr);
145 if (rr_class == C_IN && rr_type == T_AAAA
146 && rr_len == sizeof(struct in6_addr)
147 && strcasecmp(rr_name, hostname) == 0)
151 if (aptr + sizeof(struct in6_addr) > abuf + alen)
153 status = ARES_EBADRESP;
156 memcpy(&addrs[naddrs], aptr, sizeof(struct in6_addr));
158 if (naddrs < max_addr_ttls)
160 struct addr6ttl * const at = &addrttls[naddrs];
161 if (aptr + sizeof(struct in6_addr) > abuf + alen)
163 status = ARES_EBADRESP;
166 memcpy(&at->ip6addr, aptr, sizeof(struct in6_addr));
170 status = ARES_SUCCESS;
173 if (rr_class == C_IN && rr_type == T_CNAME)
175 /* Record the RR name as an alias. */
177 aliases[naliases] = rr_name;
182 /* Decode the RR data and replace the hostname with it. */
183 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
185 if (status != ARES_SUCCESS)
190 /* Take the min of the TTLs we see in the CNAME chain. */
191 if (cname_ttl > rr_ttl)
198 if (aptr > abuf + alen)
200 status = ARES_EBADRESP;
205 if (status == ARES_SUCCESS && naddrs == 0)
206 status = ARES_ENODATA;
207 if (status == ARES_SUCCESS)
209 /* We got our answer. */
212 const int n = naddrs < max_addr_ttls ? naddrs : max_addr_ttls;
213 for (i = 0; i < n; i++)
215 /* Ensure that each A TTL is no larger than the CNAME TTL. */
216 if (addrttls[i].ttl > cname_ttl)
217 addrttls[i].ttl = cname_ttl;
222 aliases[naliases] = NULL;
225 /* Allocate memory to build the host entry. */
226 hostent = malloc(sizeof(struct hostent));
229 hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
230 if (hostent->h_addr_list)
232 /* Fill in the hostent and return successfully. */
233 hostent->h_name = hostname;
234 hostent->h_aliases = aliases;
235 hostent->h_addrtype = AF_INET6;
236 hostent->h_length = sizeof(struct in6_addr);
237 for (i = 0; i < naddrs; i++)
238 hostent->h_addr_list[i] = (char *) &addrs[i];
239 hostent->h_addr_list[naddrs] = NULL;
245 status = ARES_ENOMEM;
250 for (i = 0; i < naliases; i++)