3 /* Copyright 1998 by the Massachusetts Institute of Technology.
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 #include <arpa/nameser.h>
28 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
29 #include <arpa/nameser_compat.h>
41 #include "ares_private.h"
43 int ares_parse_a_reply(const unsigned char *abuf, int alen,
44 struct hostent **host,
45 struct addrttl *addrttls, int *naddrttls)
47 unsigned int qdcount, ancount;
48 int status, i, rr_type, rr_class, rr_len, rr_ttl, naddrs;
49 int cname_ttl = INT_MAX; /* the TTL imposed by the CNAME chain */
52 const unsigned char *aptr;
53 char *hostname, *rr_name, *rr_data, **aliases;
54 struct in_addr *addrs;
55 struct hostent *hostent;
56 const int max_addr_ttls = (addrttls && naddrttls) ? *naddrttls : 0;
58 /* Set *host to NULL for all failure cases. */
61 /* Same with *naddrttls. */
65 /* Give up if abuf doesn't have room for a header. */
69 /* Fetch the question and answer count from the header. */
70 qdcount = DNS_HEADER_QDCOUNT(abuf);
71 ancount = DNS_HEADER_ANCOUNT(abuf);
75 /* Expand the name from the question, and skip past the question. */
76 aptr = abuf + HFIXEDSZ;
77 status = ares_expand_name(aptr, abuf, alen, &hostname, &len);
78 if (status != ARES_SUCCESS)
80 if (aptr + len + QFIXEDSZ > abuf + alen)
85 aptr += len + QFIXEDSZ;
89 /* Allocate addresses and aliases; ancount gives an upper bound for both. */
90 addrs = malloc(ancount * sizeof(struct in_addr));
96 aliases = malloc((ancount + 1) * sizeof(char *));
113 /* Examine each answer resource record (RR) in turn. */
114 for (i = 0; i < (int)ancount; i++)
116 /* Decode the RR up to the data field. */
117 status = ares_expand_name(aptr, abuf, alen, &rr_name, &len);
118 if (status != ARES_SUCCESS)
121 if (aptr + RRFIXEDSZ > abuf + alen)
123 status = ARES_EBADRESP;
126 rr_type = DNS_RR_TYPE(aptr);
127 rr_class = DNS_RR_CLASS(aptr);
128 rr_len = DNS_RR_LEN(aptr);
129 rr_ttl = DNS_RR_TTL(aptr);
132 if (rr_class == C_IN && rr_type == T_A
133 && rr_len == sizeof(struct in_addr)
134 && strcasecmp(rr_name, hostname) == 0)
138 if (aptr + sizeof(struct in_addr) > abuf + alen)
140 status = ARES_EBADRESP;
143 memcpy(&addrs[naddrs], aptr, sizeof(struct in_addr));
145 if (naddrs < max_addr_ttls)
147 struct addrttl * const at = &addrttls[naddrs];
148 if (aptr + sizeof(struct in_addr) > abuf + alen)
150 status = ARES_EBADRESP;
153 memcpy(&at->ipaddr, aptr, sizeof(struct in_addr));
157 status = ARES_SUCCESS;
160 if (rr_class == C_IN && rr_type == T_CNAME)
162 /* Record the RR name as an alias. */
164 aliases[naliases] = rr_name;
169 /* Decode the RR data and replace the hostname with it. */
170 status = ares_expand_name(aptr, abuf, alen, &rr_data, &len);
171 if (status != ARES_SUCCESS)
176 /* Take the min of the TTLs we see in the CNAME chain. */
177 if (cname_ttl > rr_ttl)
184 if (aptr > abuf + alen)
186 status = ARES_EBADRESP;
191 if (status == ARES_SUCCESS && naddrs == 0)
192 status = ARES_ENODATA;
193 if (status == ARES_SUCCESS)
195 /* We got our answer. */
198 const int n = naddrs < max_addr_ttls ? naddrs : max_addr_ttls;
199 for (i = 0; i < n; i++)
201 /* Ensure that each A TTL is no larger than the CNAME TTL. */
202 if (addrttls[i].ttl > cname_ttl)
203 addrttls[i].ttl = cname_ttl;
208 aliases[naliases] = NULL;
211 /* Allocate memory to build the host entry. */
212 hostent = malloc(sizeof(struct hostent));
215 hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
216 if (hostent->h_addr_list)
218 /* Fill in the hostent and return successfully. */
219 hostent->h_name = hostname;
220 hostent->h_aliases = aliases;
221 hostent->h_addrtype = AF_INET;
222 hostent->h_length = sizeof(struct in_addr);
223 for (i = 0; i < naddrs; i++)
224 hostent->h_addr_list[i] = (char *) &addrs[i];
225 hostent->h_addr_list[naddrs] = NULL;
231 status = ARES_ENOMEM;
236 for (i = 0; i < naliases; i++)