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>
39 #include "inet_net_pton.h"
40 #include "ares_private.h"
42 int ares_parse_aaaa_reply(const unsigned char *abuf, int alen,
43 struct hostent **host, struct addr6ttl *addrttls,
46 unsigned int qdcount, ancount;
47 int status, i, rr_type, rr_class, rr_len, rr_ttl, naddrs;
48 int cname_ttl = INT_MAX; /* the TTL imposed by the CNAME chain */
51 const unsigned char *aptr;
52 char *hostname, *rr_name, *rr_data, **aliases;
53 struct in6_addr *addrs;
54 struct hostent *hostent;
55 const int max_addr_ttls = (addrttls && naddrttls) ? *naddrttls : 0;
57 /* Set *host to NULL for all failure cases. */
60 /* Same with *naddrttls. */
64 /* Give up if abuf doesn't have room for a header. */
68 /* Fetch the question and answer count from the header. */
69 qdcount = DNS_HEADER_QDCOUNT(abuf);
70 ancount = DNS_HEADER_ANCOUNT(abuf);
74 /* Expand the name from the question, and skip past the question. */
75 aptr = abuf + HFIXEDSZ;
76 status = ares_expand_name(aptr, abuf, alen, &hostname, &len);
77 if (status != ARES_SUCCESS)
79 if (aptr + len + QFIXEDSZ > abuf + alen)
84 aptr += len + QFIXEDSZ;
86 /* Allocate addresses and aliases; ancount gives an upper bound for both. */
89 addrs = malloc(ancount * sizeof(struct in6_addr));
95 aliases = malloc((ancount + 1) * sizeof(char *));
111 /* Examine each answer resource record (RR) in turn. */
112 for (i = 0; i < (int)ancount; i++)
114 /* Decode the RR up to the data field. */
115 status = ares_expand_name(aptr, abuf, alen, &rr_name, &len);
116 if (status != ARES_SUCCESS)
119 if (aptr + RRFIXEDSZ > abuf + alen)
121 status = ARES_EBADRESP;
124 rr_type = DNS_RR_TYPE(aptr);
125 rr_class = DNS_RR_CLASS(aptr);
126 rr_len = DNS_RR_LEN(aptr);
127 rr_ttl = DNS_RR_TTL(aptr);
130 if (rr_class == C_IN && rr_type == T_AAAA
131 && rr_len == sizeof(struct in6_addr)
132 && strcasecmp(rr_name, hostname) == 0)
136 if (aptr + sizeof(struct in6_addr) > abuf + alen)
138 status = ARES_EBADRESP;
141 memcpy(&addrs[naddrs], aptr, sizeof(struct in6_addr));
143 if (naddrs < max_addr_ttls)
145 struct addr6ttl * const at = &addrttls[naddrs];
146 if (aptr + sizeof(struct in6_addr) > abuf + alen)
148 status = ARES_EBADRESP;
151 memcpy(&at->ip6addr, aptr, sizeof(struct in6_addr));
155 status = ARES_SUCCESS;
158 if (rr_class == C_IN && rr_type == T_CNAME)
160 /* Record the RR name as an alias. */
162 aliases[naliases] = rr_name;
167 /* Decode the RR data and replace the hostname with it. */
168 status = ares_expand_name(aptr, abuf, alen, &rr_data, &len);
169 if (status != ARES_SUCCESS)
174 /* Take the min of the TTLs we see in the CNAME chain. */
175 if (cname_ttl > rr_ttl)
182 if (aptr > abuf + alen)
184 status = ARES_EBADRESP;
189 if (status == ARES_SUCCESS && naddrs == 0)
190 status = ARES_ENODATA;
191 if (status == ARES_SUCCESS)
193 /* We got our answer. */
196 const int n = naddrs < max_addr_ttls ? naddrs : max_addr_ttls;
197 for (i = 0; i < n; i++)
199 /* Ensure that each A TTL is no larger than the CNAME TTL. */
200 if (addrttls[i].ttl > cname_ttl)
201 addrttls[i].ttl = cname_ttl;
206 aliases[naliases] = NULL;
209 /* Allocate memory to build the host entry. */
210 hostent = malloc(sizeof(struct hostent));
213 hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
214 if (hostent->h_addr_list)
216 /* Fill in the hostent and return successfully. */
217 hostent->h_name = hostname;
218 hostent->h_aliases = aliases;
219 hostent->h_addrtype = AF_INET6;
220 hostent->h_length = sizeof(struct in6_addr);
221 for (i = 0; i < naddrs; i++)
222 hostent->h_addr_list[i] = (char *) &addrs[i];
223 hostent->h_addr_list[naddrs] = NULL;
229 status = ARES_ENOMEM;
234 for (i = 0; i < naliases; i++)
238 for (i = 0; i < naliases; i++)