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_SYS_SOCKET_H
20 # include <sys/socket.h>
22 #ifdef HAVE_NETINET_IN_H
23 # include <netinet/in.h>
28 #ifdef HAVE_ARPA_INET_H
29 # include <arpa/inet.h>
31 #ifdef HAVE_ARPA_NAMESER_H
32 # include <arpa/nameser.h>
36 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
37 # include <arpa/nameser_compat.h>
52 #include "ares_private.h"
54 int ares_parse_a_reply(const unsigned char *abuf, int alen,
55 struct hostent **host,
56 struct ares_addrttl *addrttls, int *naddrttls)
58 unsigned int qdcount, ancount;
59 int status, i, rr_type, rr_class, rr_len, rr_ttl, naddrs;
60 int cname_ttl = INT_MAX; /* the TTL imposed by the CNAME chain */
63 const unsigned char *aptr;
64 char *hostname, *rr_name, *rr_data, **aliases;
65 struct in_addr *addrs;
66 struct hostent *hostent;
67 const int max_addr_ttls = (addrttls && naddrttls) ? *naddrttls : 0;
69 /* Set *host to NULL for all failure cases. */
72 /* Same with *naddrttls. */
76 /* Give up if abuf doesn't have room for a header. */
80 /* Fetch the question and answer count from the header. */
81 qdcount = DNS_HEADER_QDCOUNT(abuf);
82 ancount = DNS_HEADER_ANCOUNT(abuf);
86 /* Expand the name from the question, and skip past the question. */
87 aptr = abuf + HFIXEDSZ;
88 status = ares__expand_name_for_response(aptr, abuf, alen, &hostname, &len);
89 if (status != ARES_SUCCESS)
91 if (aptr + len + QFIXEDSZ > abuf + alen)
96 aptr += len + QFIXEDSZ;
100 /* Allocate addresses and aliases; ancount gives an upper bound for
102 addrs = malloc(ancount * sizeof(struct in_addr));
108 aliases = malloc((ancount + 1) * sizeof(char *));
125 /* Examine each answer resource record (RR) in turn. */
126 for (i = 0; i < (int)ancount; i++)
128 /* Decode the RR up to the data field. */
129 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
130 if (status != ARES_SUCCESS)
133 if (aptr + RRFIXEDSZ > abuf + alen)
135 status = ARES_EBADRESP;
138 rr_type = DNS_RR_TYPE(aptr);
139 rr_class = DNS_RR_CLASS(aptr);
140 rr_len = DNS_RR_LEN(aptr);
141 rr_ttl = DNS_RR_TTL(aptr);
144 if (rr_class == C_IN && rr_type == T_A
145 && rr_len == sizeof(struct in_addr)
146 && strcasecmp(rr_name, hostname) == 0)
150 if (aptr + sizeof(struct in_addr) > abuf + alen)
152 status = ARES_EBADRESP;
155 memcpy(&addrs[naddrs], aptr, sizeof(struct in_addr));
157 if (naddrs < max_addr_ttls)
159 struct ares_addrttl * const at = &addrttls[naddrs];
160 if (aptr + sizeof(struct in_addr) > abuf + alen)
162 status = ARES_EBADRESP;
165 memcpy(&at->ipaddr, aptr, sizeof(struct in_addr));
169 status = ARES_SUCCESS;
172 if (rr_class == C_IN && rr_type == T_CNAME)
174 /* Record the RR name as an alias. */
176 aliases[naliases] = rr_name;
181 /* Decode the RR data and replace the hostname with it. */
182 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
184 if (status != ARES_SUCCESS)
189 /* Take the min of the TTLs we see in the CNAME chain. */
190 if (cname_ttl > rr_ttl)
197 if (aptr > abuf + alen)
199 status = ARES_EBADRESP;
204 if (status == ARES_SUCCESS && naddrs == 0 && naliases == 0)
205 /* the check for naliases to be zero is to make sure CNAME responses
206 don't get caught here */
207 status = ARES_ENODATA;
208 if (status == ARES_SUCCESS)
210 /* We got our answer. */
213 const int n = naddrs < max_addr_ttls ? naddrs : max_addr_ttls;
214 for (i = 0; i < n; i++)
216 /* Ensure that each A TTL is no larger than the CNAME TTL. */
217 if (addrttls[i].ttl > cname_ttl)
218 addrttls[i].ttl = cname_ttl;
223 aliases[naliases] = NULL;
226 /* Allocate memory to build the host entry. */
227 hostent = malloc(sizeof(struct hostent));
230 hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
231 if (hostent->h_addr_list)
233 /* Fill in the hostent and return successfully. */
234 hostent->h_name = hostname;
235 hostent->h_aliases = aliases;
236 hostent->h_addrtype = AF_INET;
237 hostent->h_length = sizeof(struct in_addr);
238 for (i = 0; i < naddrs; i++)
239 hostent->h_addr_list[i] = (char *) &addrs[i];
240 hostent->h_addr_list[naddrs] = NULL;
246 status = ARES_ENOMEM;
251 for (i = 0; i < naliases; i++)