2 /* Copyright 1998 by the Massachusetts Institute of Technology.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of M.I.T. not be used in
10 * advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.
12 * M.I.T. makes no representations about the suitability of
13 * this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
17 #include "ares_setup.h"
19 #ifdef HAVE_NETINET_IN_H
20 # include <netinet/in.h>
25 #ifdef HAVE_ARPA_INET_H
26 # include <arpa/inet.h>
28 #ifdef HAVE_ARPA_NAMESER_H
29 # include <arpa/nameser.h>
33 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
34 # include <arpa/nameser_compat.h>
47 #include "ares_private.h"
49 int ares_parse_a_reply(const unsigned char *abuf, int alen,
50 struct hostent **host,
51 struct ares_addrttl *addrttls, int *naddrttls)
53 unsigned int qdcount, ancount;
54 int status, i, rr_type, rr_class, rr_len, rr_ttl, naddrs;
55 int cname_ttl = INT_MAX; /* the TTL imposed by the CNAME chain */
58 const unsigned char *aptr;
59 char *hostname, *rr_name, *rr_data, **aliases;
60 struct in_addr *addrs;
61 struct hostent *hostent;
62 const int max_addr_ttls = (addrttls && naddrttls) ? *naddrttls : 0;
64 /* Set *host to NULL for all failure cases. */
67 /* Same with *naddrttls. */
71 /* Give up if abuf doesn't have room for a header. */
75 /* Fetch the question and answer count from the header. */
76 qdcount = DNS_HEADER_QDCOUNT(abuf);
77 ancount = DNS_HEADER_ANCOUNT(abuf);
81 /* Expand the name from the question, and skip past the question. */
82 aptr = abuf + HFIXEDSZ;
83 status = ares__expand_name_for_response(aptr, abuf, alen, &hostname, &len);
84 if (status != ARES_SUCCESS)
86 if (aptr + len + QFIXEDSZ > abuf + alen)
91 aptr += len + QFIXEDSZ;
95 /* Allocate addresses and aliases; ancount gives an upper bound for
97 addrs = malloc(ancount * sizeof(struct in_addr));
103 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);
139 if (aptr + rr_len > abuf + alen)
142 status = ARES_EBADRESP;
146 if (rr_class == C_IN && rr_type == T_A
147 && rr_len == sizeof(struct in_addr)
148 && strcasecmp(rr_name, hostname) == 0)
152 if (aptr + sizeof(struct in_addr) > abuf + alen)
155 status = ARES_EBADRESP;
158 memcpy(&addrs[naddrs], aptr, sizeof(struct in_addr));
160 if (naddrs < max_addr_ttls)
162 struct ares_addrttl * const at = &addrttls[naddrs];
163 if (aptr + sizeof(struct in_addr) > abuf + alen)
166 status = ARES_EBADRESP;
169 memcpy(&at->ipaddr, aptr, sizeof(struct in_addr));
173 status = ARES_SUCCESS;
176 if (rr_class == C_IN && rr_type == T_CNAME)
178 /* Record the RR name as an alias. */
180 aliases[naliases] = rr_name;
185 /* Decode the RR data and replace the hostname with it. */
186 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
188 if (status != ARES_SUCCESS)
193 /* Take the min of the TTLs we see in the CNAME chain. */
194 if (cname_ttl > rr_ttl)
201 if (aptr > abuf + alen)
203 status = ARES_EBADRESP;
208 if (status == ARES_SUCCESS && naddrs == 0 && naliases == 0)
209 /* the check for naliases to be zero is to make sure CNAME responses
210 don't get caught here */
211 status = ARES_ENODATA;
212 if (status == ARES_SUCCESS)
214 /* We got our answer. */
217 const int n = naddrs < max_addr_ttls ? naddrs : max_addr_ttls;
218 for (i = 0; i < n; i++)
220 /* Ensure that each A TTL is no larger than the CNAME TTL. */
221 if (addrttls[i].ttl > cname_ttl)
222 addrttls[i].ttl = cname_ttl;
227 aliases[naliases] = NULL;
230 /* Allocate memory to build the host entry. */
231 hostent = malloc(sizeof(struct hostent));
234 hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
235 if (hostent->h_addr_list)
237 /* Fill in the hostent and return successfully. */
238 hostent->h_name = hostname;
239 hostent->h_aliases = aliases;
240 hostent->h_addrtype = AF_INET;
241 hostent->h_length = sizeof(struct in_addr);
242 for (i = 0; i < naddrs; i++)
243 hostent->h_addr_list[i] = (char *) &addrs[i];
244 hostent->h_addr_list[naddrs] = NULL;
245 if (!naddrs && addrs)
252 status = ARES_ENOMEM;
257 for (i = 0; i < naliases; i++)